2525import eu .webeid .security .exceptions .AuthTokenParseException ;
2626import eu .webeid .security .exceptions .AuthTokenSignatureValidationException ;
2727import eu .webeid .security .exceptions .ChallengeNullOrEmptyException ;
28- import io .jsonwebtoken .SignatureAlgorithm ;
29- import io .jsonwebtoken .impl .crypto . DefaultSignatureValidatorFactory ;
30- import io .jsonwebtoken .impl . crypto . SignatureValidator ;
31- import io .jsonwebtoken .security .SignatureException ;
28+ import io .jsonwebtoken .Jwts ;
29+ import io .jsonwebtoken .impl .security . DefaultVerifySecureDigestRequest ;
30+ import io .jsonwebtoken .security . SignatureAlgorithm ;
31+ import io .jsonwebtoken .security .VerifySecureDigestRequest ;
3232
33+ import java .io .ByteArrayInputStream ;
3334import java .net .URI ;
3435import java .nio .charset .StandardCharsets ;
3536import java .security .MessageDigest ;
@@ -73,25 +74,20 @@ public void validate(String algorithm, String signature, PublicKey publicKey, St
7374 throw new AuthTokenParseException ("Unsupported signature algorithm" );
7475 }
7576
76- SignatureAlgorithm signatureAlgorithm ;
77+ final SignatureAlgorithm signatureAlgorithm = (SignatureAlgorithm ) Jwts .SIG .get ().forKey (algorithm );
78+ if (signatureAlgorithm == null ) {
79+ // Should not happen, see ALLOWED_SIGNATURE_ALGORITHMS check above.
80+ throw new AuthTokenParseException ("JJWT does not support signature algorithm: " + algorithm );
81+ }
7782 MessageDigest hashAlgorithm ;
7883 try {
79- signatureAlgorithm = SignatureAlgorithm .forName (algorithm );
8084 hashAlgorithm = hashAlgorithmForName (algorithm );
81- } catch (SignatureException e ) {
82- // Should not happen, see ALLOWED_SIGNATURE_ALGORITHMS check above.
83- throw new AuthTokenParseException ("Invalid signature algorithm" , e );
8485 } catch (NoSuchAlgorithmException e ) {
85- throw new AuthTokenParseException ("Invalid hash algorithm" , e );
86- }
87- if (signatureAlgorithm == null || signatureAlgorithm == SignatureAlgorithm .NONE ) {
8886 // Should not happen, see ALLOWED_SIGNATURE_ALGORITHMS check above.
89- throw new AuthTokenParseException ("Invalid signature algorithm" );
87+ throw new AuthTokenParseException ("Invalid hash algorithm" , e );
9088 }
9189 Objects .requireNonNull (hashAlgorithm , "hashAlgorithm" );
92- signatureAlgorithm .assertValidVerificationKey (publicKey );
93- final SignatureValidator signatureValidator = DefaultSignatureValidatorFactory .INSTANCE
94- .createSignatureValidator (signatureAlgorithm , publicKey );
90+
9591 final byte [] decodedSignature = decodeBase64 (signature );
9692
9793 final byte [] originHash = hashAlgorithm .digest (originBytes );
@@ -100,9 +96,13 @@ public void validate(String algorithm, String signature, PublicKey publicKey, St
10096
10197 // Note that in case of ECDSA, the eID card outputs raw R||S, but JCA's SHA384withECDSA signature
10298 // validation implementation requires the signature in DER encoding.
103- // JJWT's EllipticCurveProvider.transcodeSignatureToDER() internally takes care of transcoding
104- // raw R||S to DER as needed inside EllipticCurveProvider.isValid().
105- if (!signatureValidator .isValid (concatSignedFields , decodedSignature )) {
99+ // JJWT internally takes care of transcoding raw R||S to DER as needed.
100+ final VerifySecureDigestRequest <PublicKey > verificationRequest =
101+ new DefaultVerifySecureDigestRequest <>(
102+ new ByteArrayInputStream (concatSignedFields ),
103+ null , null ,
104+ publicKey , decodedSignature );
105+ if (!signatureAlgorithm .verify (verificationRequest )) {
106106 throw new AuthTokenSignatureValidationException ();
107107 }
108108 }
0 commit comments