4343import java .security .interfaces .RSAPrivateKey ;
4444import java .security .interfaces .RSAPublicKey ;
4545import java .security .spec .X509EncodedKeySpec ;
46+ import java .util .ArrayList ;
4647import java .util .Arrays ;
48+ import java .util .List ;
4749
4850class RSAAlgorithm extends Algorithm {
4951
@@ -65,8 +67,65 @@ class RSAAlgorithm extends Algorithm {
6567 this (new CryptoHelper (), id , algorithm , keyProvider );
6668 }
6769
70+ @ Override
71+ public void verifyWithX509 (DecodedJWT jwt , EncodeType encodeType , String jwksFile , String pemFile ) throws Exception {
72+ List <byte []> byteArrayList = decode (jwt , encodeType );
73+ byte [] contentBytes = byteArrayList .get (0 );
74+ byte [] signatureBytes = byteArrayList .get (1 );
75+ try {
76+ PublicKey publicKey ;
77+ if (jwksFile != null && !jwksFile .isEmpty () && jwksFile .endsWith (".json" )) {
78+ String kid = jwt .getKeyId ();
79+ JwkProvider provider = new UrlJwkProvider (new File (jwksFile ).toURI ().toURL ());
80+ Jwk jwk = provider .get (kid );
81+ String cert = jwk .getCertificateChain ().get (0 );
82+ try (Writer writer = new BufferedWriter (new OutputStreamWriter (
83+ new FileOutputStream ("./jwks.cert" ), "utf-8" ))) {
84+ writer .write ("-----BEGIN CERTIFICATE-----" );
85+ writer .append ("\n " + cert + "\n " );
86+ writer .append ("-----END CERTIFICATE-----" );
87+ }
88+
89+ FileReader file = new FileReader (pemFile );
90+ PemReader reader = new PemReader (file );
91+ X509EncodedKeySpec caKeySpec = new X509EncodedKeySpec (reader .readPemObject ().getContent ());
92+ KeyFactory kf = KeyFactory .getInstance ("RSA" );
93+ publicKey = kf .generatePublic (caKeySpec );
94+ } else {
95+ throw new IllegalArgumentException ("Not a proper jwks file" );
96+ }
97+ if (publicKey == null ) {
98+ throw new IllegalStateException ("The given Public Key is null." );
99+ }
100+ boolean valid = crypto .verifySignatureFor (getDescription (), publicKey , contentBytes , signatureBytes );
101+ if (!valid ) {
102+ throw new SignatureVerificationException (this );
103+ }
104+ } catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalStateException e ) {
105+ throw new SignatureVerificationException (this , e );
106+ }
107+ }
108+
68109 @ Override
69110 public void verify (DecodedJWT jwt , EncodeType encodeType ) throws Exception {
111+ List <byte []> byteArrayList = decode (jwt , encodeType );
112+ byte [] contentBytes = byteArrayList .get (0 );
113+ byte [] signatureBytes = byteArrayList .get (1 );
114+ try {
115+ PublicKey publicKey = keyProvider .getPublicKeyById (jwt .getKeyId ());
116+ if (publicKey == null ) {
117+ throw new IllegalStateException ("The given Public Key is null." );
118+ }
119+ boolean valid = crypto .verifySignatureFor (getDescription (), publicKey , contentBytes , signatureBytes );
120+ if (!valid ) {
121+ throw new SignatureVerificationException (this );
122+ }
123+ } catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalStateException e ) {
124+ throw new SignatureVerificationException (this , e );
125+ }
126+ }
127+
128+ private List <byte []> decode (DecodedJWT jwt , EncodeType encodeType ) throws Exception {
70129 byte [] contentBytes = String .format ("%s.%s" , jwt .getHeader (), jwt .getPayload ()).getBytes (StandardCharsets .UTF_8 );
71130 byte [] signatureBytes = null ;
72131 String signature = jwt .getSignature ();
@@ -80,45 +139,13 @@ public void verify(DecodedJWT jwt, EncodeType encodeType) throws Exception {
80139 Base32 base32 = new Base32 ();
81140 urlDecoded = URLDecoder .decode (signature , "UTF-8" );
82141 signatureBytes = base32 .decode (urlDecoded );
83- System .out .println ("signature bytes after being decoded: " + Arrays .toString (signatureBytes ));
84- bytesAfterBeingDecoded = signatureBytes ;
85- System .out .println ("Are they equal? " + Arrays .equals (JWTCreator .bytesBeforeBeingDecoded , bytesAfterBeingDecoded ));
86142 break ;
87143 case Base64 :
88144 signatureBytes = Base64 .decodeBase64 (signature );
89- System .out .println ("signature bytes after being decoded: " + Arrays .toString (signatureBytes ));
90- bytesAfterBeingDecoded = signatureBytes ;
91- System .out .println ("Are they equal? " + Arrays .equals (JWTCreator .bytesBeforeBeingDecoded , bytesAfterBeingDecoded ));
92145 break ;
93146 }
94147
95- try {
96- String kid = jwt .getKeyId ();
97- JwkProvider provider = new UrlJwkProvider (new File ("./jwksRSA.json" ).toURI ().toURL ());
98- Jwk jwk = provider .get (kid );
99- String cert = jwk .getCertificateChain ().get (0 );
100- try (Writer writer = new BufferedWriter (new OutputStreamWriter (
101- new FileOutputStream ("./jwks.cert" ), "utf-8" ))) {
102- writer .write ("-----BEGIN CERTIFICATE-----" );
103- writer .append ("\n " + cert + "\n " );
104- writer .append ("-----END CERTIFICATE-----" );
105- }
106-
107- FileReader file = new FileReader ("./src/main/java/com/auth0/jwt/algorithms/jwks.pem" );
108- PemReader reader = new PemReader (file );
109- X509EncodedKeySpec caKeySpec = new X509EncodedKeySpec (reader .readPemObject ().getContent ());
110- KeyFactory kf = KeyFactory .getInstance ("RSA" );
111- PublicKey publicKey = kf .generatePublic (caKeySpec );
112- if (publicKey == null ) {
113- throw new IllegalStateException ("The given Public Key is null." );
114- }
115- boolean valid = crypto .verifySignatureFor (getDescription (), publicKey , contentBytes , signatureBytes );
116- if (!valid ) {
117- throw new SignatureVerificationException (this );
118- }
119- } catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException | IllegalStateException e ) {
120- throw new SignatureVerificationException (this , e );
121- }
148+ return new ArrayList <>(Arrays .asList (contentBytes , signatureBytes ));
122149 }
123150
124151 @ Override
0 commit comments