1515 */
1616package io .vertx .ext .auth .impl .jose ;
1717
18+ import io .vertx .core .VertxException ;
1819import io .vertx .core .buffer .Buffer ;
1920import io .vertx .core .internal .logging .Logger ;
2021import io .vertx .core .internal .logging .LoggerFactory ;
@@ -82,6 +83,22 @@ private static PubKeySigningAlgorithm createPubKeySigningAlgorithm(Algorithm alg
8283 return PubKeySigningAlgorithm .createPubKeySigningAlgorithm (alg .name (), privateKey , null , null , alg .signatureProvider , length );
8384 }
8485
86+ private static char [] password (String keyStorePassword , Map <String , String > passwordProtection , String alias ) {
87+ String password ;
88+ if (passwordProtection == null || (password = passwordProtection .get (alias )) == null ) {
89+ password = keyStorePassword ;
90+ }
91+ return password .toCharArray ();
92+ }
93+
94+ private static boolean invalidAlgAlias (String alg , String alias ) {
95+ try {
96+ Algorithm algo = Algorithm .valueOf (alias );
97+ return !alg .equalsIgnoreCase (algo .jce ) && !alg .equalsIgnoreCase (algo .oid );
98+ } catch (IllegalArgumentException e ) {
99+ return true ;
100+ }
101+ }
85102
86103 static final Logger LOG = LoggerFactory .getLogger (JWK .class );
87104
@@ -102,30 +119,40 @@ private static PubKeySigningAlgorithm createPubKeySigningAlgorithm(Algorithm alg
102119 private final Algorithm algo ;
103120
104121 public static List <JWK > load (KeyStore keyStore , String keyStorePassword , Map <String , String > passwordProtection ) {
105- final List <Callable <SigningAlgorithm >> keys = SigningAlgorithm .create (keyStore , keyStorePassword , passwordProtection );
106- return keys .stream ().flatMap (fact -> {
122+ List <JWK > keys = new ArrayList <>();
123+ // load MACs
124+ for (String alias : Arrays .asList ("HS256" , "HS384" , "HS512" )) {
125+ // algorithm is valid
107126 try {
108- SigningAlgorithm algo = fact .call ();
109- if (algo instanceof PubKeySigningAlgorithm ) {
110- PubKeySigningAlgorithm psa = (PubKeySigningAlgorithm ) algo ;
111- return Stream .of (new JWK (psa .name (), psa .id (), psa ));
112- } else {
113- MacSigningAlgorithm msa = (MacSigningAlgorithm ) algo ;
114- return Stream .of (new JWK (msa ));
127+ char [] password = password (keyStorePassword , passwordProtection , alias );
128+ MacSigningAlgorithm a = (MacSigningAlgorithm ) SigningAlgorithm .create (keyStore , alias , password );
129+ // key store does not have the requested algorithm
130+ if (a != null ) {
131+ // the algorithm cannot be null, and it cannot be different from the alias list
132+ if (invalidAlgAlias (a .name (), alias )) {
133+ throw new Exception ("The key algorithm does not match: {" + alias + ": " + a .name () + "}" );
134+ }
135+ keys .add (new JWK (a ));
115136 }
116137 } catch (Exception e ) {
117138 LOG .warn ("Failed to load key for algorithm" , e );
118- return Stream .empty ();
119139 }
120- }).collect (Collectors .toList ());
121- }
122-
123- private static char [] password (String keyStorePassword , Map <String , String > passwordProtection , String alias ) {
124- String password ;
125- if (passwordProtection == null || (password = passwordProtection .get (alias )) == null ) {
126- password = keyStorePassword ;
127140 }
128- return password .toCharArray ();
141+ for (String alias : Arrays .asList ("RS256" , "RS384" , "RS512" , "ES256K" , "ES256" , "ES384" , "ES512" )) {
142+ try {
143+ char [] password = password (keyStorePassword , passwordProtection , alias );
144+ PubKeySigningAlgorithm a = (PubKeySigningAlgorithm ) SigningAlgorithm .create (keyStore , alias , password );
145+ if (a != null ) {
146+ if (invalidAlgAlias (a .signature ().getAlgorithm (), alias )) {
147+ throw new Exception ("The key algorithm does not match: {" + alias + ": " + a .signature ().getAlgorithm () + "}" );
148+ }
149+ keys .add (new JWK (a ));
150+ }
151+ } catch (Exception e ) {
152+ LOG .warn ("Failed to load key for algorithm" , e );
153+ }
154+ }
155+ return keys ;
129156 }
130157
131158 /**
@@ -283,7 +310,9 @@ private JWK(MacSigningAlgorithm signingAlgo) throws NoSuchAlgorithmException, In
283310 }
284311 }
285312
286- private JWK (String algorithm , String id , PubKeySigningAlgorithm signingAlg ) throws NoSuchAlgorithmException {
313+ private JWK (PubKeySigningAlgorithm signingAlgo ) throws NoSuchAlgorithmException {
314+
315+ String algorithm = signingAlgo .name ();
287316
288317 try {
289318 algo = Algorithm .valueOf (algorithm );
@@ -292,28 +321,28 @@ private JWK(String algorithm, String id, PubKeySigningAlgorithm signingAlg) thro
292321 }
293322
294323 kid = null ;
295- label = signingAlg .canSign () ? algorithm + '#' + id + "-" + signingAlg .privateKey ().hashCode () : algorithm + '#' + id ;
324+ label = signingAlgo .canSign () ? algorithm + '#' + signingAlgo . id () + "-" + signingAlgo .privateKey ().hashCode () : algorithm + '#' + signingAlgo . id () ;
296325 use = null ;
297326
298327 switch (algo ) {
299328 case RS256 :
300329 case RS384 :
301330 case RS512 :
302331 kty = "RSA" ;
303- signingAlgorithm = signingAlg ;
332+ signingAlgorithm = signingAlgo ;
304333 break ;
305334 case PS256 :
306335 case PS384 :
307336 case PS512 :
308337 kty = "RSASSA" ;
309- signingAlgorithm = signingAlg ;
338+ signingAlgorithm = signingAlgo ;
310339 break ;
311340 case ES256 :
312341 case ES384 :
313342 case ES512 :
314343 case ES256K :
315344 kty = "EC" ;
316- signingAlgorithm = wrapECAlgo (signingAlg );
345+ signingAlgorithm = wrapECAlgo (signingAlgo );
317346 break ;
318347 default :
319348 throw new NoSuchAlgorithmException ("Unknown algorithm: " + algorithm );
0 commit comments