44import com .auth0 .exception .PublicKeyProviderException ;
55import com .auth0 .jwt .exceptions .AlgorithmMismatchException ;
66import com .auth0 .jwt .interfaces .DecodedJWT ;
7- import org .bouncycastle .util .io .pem .PemReader ;
87import org .junit .jupiter .api .Test ;
98
9+ import java .io .BufferedReader ;
1010import java .io .FileInputStream ;
1111import java .io .FileReader ;
1212import java .io .IOException ;
1818import java .security .interfaces .RSAPublicKey ;
1919import java .security .spec .EncodedKeySpec ;
2020import java .security .spec .X509EncodedKeySpec ;
21+ import java .util .Base64 ;
2122import java .util .Scanner ;
2223
2324import static com .auth0 .AssertsUtil .verifyThrows ;
@@ -144,7 +145,7 @@ public RSAPublicKey getPublicKeyById(String keyId) throws PublicKeyProviderExcep
144145
145146 private static RSAPublicKey readPublicKeyFromFile (final String path ) throws IOException {
146147 Scanner scanner = null ;
147- PemReader pemReader = null ;
148+ BufferedReader reader = null ;
148149 try {
149150 scanner = new Scanner (Paths .get (path ));
150151 if (scanner .hasNextLine () && scanner .nextLine ().startsWith ("-----BEGIN CERTIFICATE-----" )) {
@@ -155,8 +156,15 @@ private static RSAPublicKey readPublicKeyFromFile(final String path) throws IOEx
155156 fs .close ();
156157 return (RSAPublicKey ) key ;
157158 } else {
158- pemReader = new PemReader (new FileReader (path ));
159- byte [] keyBytes = pemReader .readPemObject ().getContent ();
159+ reader = new BufferedReader (new FileReader (path ));
160+ StringBuilder pemContent = new StringBuilder ();
161+ String line ;
162+ while ((line = reader .readLine ()) != null ) {
163+ if (!line .startsWith ("-----BEGIN" ) && !line .startsWith ("-----END" )) {
164+ pemContent .append (line );
165+ }
166+ }
167+ byte [] keyBytes = Base64 .getDecoder ().decode (pemContent .toString ());
160168 KeyFactory kf = KeyFactory .getInstance ("RSA" );
161169 EncodedKeySpec keySpec = new X509EncodedKeySpec (keyBytes );
162170 return (RSAPublicKey ) kf .generatePublic (keySpec );
@@ -167,8 +175,8 @@ private static RSAPublicKey readPublicKeyFromFile(final String path) throws IOEx
167175 if (scanner != null ) {
168176 scanner .close ();
169177 }
170- if (pemReader != null ) {
171- pemReader .close ();
178+ if (reader != null ) {
179+ reader .close ();
172180 }
173181 }
174182 }
0 commit comments