String getCrv(JsonWebKey jwk) { try { return ((EllipticCurveJsonWebKey) jwk).getCurveName(); } catch (ClassCastException e) { return null; } }
/** * Analyzes the key used in the {@link JsonWebKey}, and returns the key algorithm * identifier for {@link JsonWebSignature}. * * @param jwk * {@link JsonWebKey} to analyze * @return algorithm identifier * @throws IllegalArgumentException * there is no corresponding algorithm identifier for the key */ public static String keyAlgorithm(JsonWebKey jwk) { if (jwk instanceof EllipticCurveJsonWebKey) { EllipticCurveJsonWebKey ecjwk = (EllipticCurveJsonWebKey) jwk; switch (ecjwk.getCurveName()) { case "P-256": return AlgorithmIdentifiers.ECDSA_USING_P256_CURVE_AND_SHA256; case "P-384": return AlgorithmIdentifiers.ECDSA_USING_P384_CURVE_AND_SHA384; case "P-521": return AlgorithmIdentifiers.ECDSA_USING_P521_CURVE_AND_SHA512; default: throw new IllegalArgumentException("Unknown EC name " + ecjwk.getCurveName()); } } else if (jwk instanceof RsaJsonWebKey) { return AlgorithmIdentifiers.RSA_USING_SHA256; } else { throw new IllegalArgumentException("Unknown algorithm " + jwk.getAlgorithm()); } }
/** * Analyzes the key used in the {@link JsonWebKey}, and returns the key algorithm * identifier for {@link JsonWebSignature}. * * @param jwk * {@link JsonWebKey} to analyze * @return algorithm identifier * @throws IllegalArgumentException * there is no corresponding algorithm identifier for the key */ public static String keyAlgorithm(JsonWebKey jwk) { if (jwk instanceof EllipticCurveJsonWebKey) { EllipticCurveJsonWebKey ecjwk = (EllipticCurveJsonWebKey) jwk; switch (ecjwk.getCurveName()) { case "P-256": return AlgorithmIdentifiers.ECDSA_USING_P256_CURVE_AND_SHA256; case "P-384": return AlgorithmIdentifiers.ECDSA_USING_P384_CURVE_AND_SHA384; case "P-521": return AlgorithmIdentifiers.ECDSA_USING_P521_CURVE_AND_SHA512; default: throw new IllegalArgumentException("Unknown EC name " + ecjwk.getCurveName()); } } else if (jwk instanceof RsaJsonWebKey) { return AlgorithmIdentifiers.RSA_USING_SHA256; } else { throw new IllegalArgumentException("Unknown algorithm " + jwk.getAlgorithm()); } }
private int getCoordinateByteLength() { ECParameterSpec spec = EllipticCurves.getSpec(getCurveName()); return (int) Math.ceil(spec.getCurve().getField().getFieldSize() / 8d); }
protected void fillPublicTypeSpecificParams(Map<String,Object> params) { ECPublicKey ecPublicKey = getECPublicKey(); ECPoint w = ecPublicKey.getW(); int coordinateByteLength = getCoordinateByteLength(); putBigIntAsBase64UrlEncodedParam(params, X_MEMBER_NAME, w.getAffineX(), coordinateByteLength); putBigIntAsBase64UrlEncodedParam(params, Y_MEMBER_NAME, w.getAffineY(), coordinateByteLength); params.put(CURVE_MEMBER_NAME, getCurveName()); }