Skip to content

Commit 40e4197

Browse files
committed
Make signing algo final
1 parent 23fb5df commit 40e4197

File tree

2 files changed

+37
-26
lines changed

2 files changed

+37
-26
lines changed

vertx-auth-common/src/main/java/io/vertx/ext/auth/impl/jose/JWK.java

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ private static boolean invalidAlgAlias(String alg, String alias) {
122122

123123
// JSON JWK properties
124124
private final String kid;
125-
private final String alg;
126125
private final String use;
127126

128127
// the label is a synthetic id that allows comparing 2 keys
@@ -133,9 +132,6 @@ private static boolean invalidAlgAlias(String alg, String alias) {
133132
private final String label;
134133

135134
// the cryptography objects, not all will be initialized
136-
// private PrivateKey privateKey;
137-
// private PublicKey publicKey;
138-
// private Mac mac;
139135
private SigningAlgorithm signingAlgorithm;
140136

141137
public static List<JWK> load(KeyStore keyStore, String keyStorePassword, Map<String, String> passwordProtection) {
@@ -211,7 +207,7 @@ private static char[] password(String keyStorePassword, Map<String, String> pass
211207
*/
212208
public JWK(PubSecKeyOptions options) {
213209

214-
alg = options.getAlgorithm();
210+
String alg = options.getAlgorithm();
215211
kid = options.getId();
216212
use = null;
217213

@@ -230,7 +226,7 @@ public JWK(PubSecKeyOptions options) {
230226
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
231227
throw new RuntimeException(e);
232228
}
233-
signingAlgorithm = new MacSigningAlgorithm(mac);
229+
signingAlgorithm = new MacSigningAlgorithm(alg, mac);
234230
return;
235231
case "HS384":
236232
try {
@@ -239,7 +235,7 @@ public JWK(PubSecKeyOptions options) {
239235
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
240236
throw new RuntimeException(e);
241237
}
242-
signingAlgorithm = new MacSigningAlgorithm(mac);
238+
signingAlgorithm = new MacSigningAlgorithm(alg, mac);
243239
return;
244240
case "HS512":
245241
try {
@@ -248,7 +244,7 @@ public JWK(PubSecKeyOptions options) {
248244
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
249245
throw new RuntimeException(e);
250246
}
251-
signingAlgorithm = new MacSigningAlgorithm(mac);
247+
signingAlgorithm = new MacSigningAlgorithm(alg, mac);
252248
return;
253249
}
254250

@@ -343,11 +339,17 @@ private static SigningAlgorithm parsePEM(String alg, String kty, KeyFactory kf,
343339
}
344340

345341
static class MacSigningAlgorithm implements SigningAlgorithm {
342+
private final String name;
346343
private final Mac mac;
347-
public MacSigningAlgorithm(Mac mac) {
344+
public MacSigningAlgorithm(String name, Mac mac) {
345+
this.name = name;
348346
this.mac = mac;
349347
}
350348
@Override
349+
public String name() {
350+
return name;
351+
}
352+
@Override
351353
public Signer signer() {
352354
return new Signer() {
353355
@Override
@@ -380,6 +382,11 @@ public PubKeySigningAlgorithm(String kty, String alg, PrivateKey privateKey, Pub
380382
this.alg = alg;
381383
}
382384

385+
@Override
386+
public String name() {
387+
return alg;
388+
}
389+
383390
@Override
384391
public Signer signer() throws GeneralSecurityException {
385392
Signature signature = JWS.getSignature(alg);
@@ -435,16 +442,15 @@ public synchronized boolean verify(byte[] expected, byte[] payload) throws Gener
435442

436443
private JWK(String algorithm, Mac mac) throws NoSuchAlgorithmException {
437444

438-
alg = algorithm;
439445
kid = null;
440-
label = alg + "#" + mac.hashCode();
446+
label = algorithm + "#" + mac.hashCode();
441447
use = null;
442448

443-
switch (alg) {
449+
switch (algorithm) {
444450
case "HS256":
445451
case "HS384":
446452
case "HS512":
447-
this.signingAlgorithm = new MacSigningAlgorithm(mac);
453+
this.signingAlgorithm = new MacSigningAlgorithm(algorithm, mac);
448454
break;
449455
default:
450456
throw new NoSuchAlgorithmException("Unknown algorithm: " + algorithm);
@@ -453,7 +459,6 @@ private JWK(String algorithm, Mac mac) throws NoSuchAlgorithmException {
453459

454460
private JWK(String algorithm, X509Certificate certificate, PrivateKey privateKey) throws NoSuchAlgorithmException {
455461

456-
alg = algorithm;
457462
kid = null;
458463
label = privateKey != null ? algorithm + '#' + certificate.hashCode() + "-" + privateKey.hashCode() : algorithm + '#' + certificate.hashCode();
459464
use = null;
@@ -488,6 +493,7 @@ public JWK(JsonObject json) {
488493
kid = json.getString("kid");
489494
use = json.getString("use");
490495

496+
String alg;
491497
try {
492498
String kty;
493499
switch (json.getString("kty")) {
@@ -506,7 +512,7 @@ public JWK(JsonObject json) {
506512
case "PS256":
507513
case "PS384":
508514
case "PS512":
509-
createRSA(alg, kty, json);
515+
signingAlgorithm = createRSA(alg, kty, json);
510516
break;
511517
default:
512518
throw new NoSuchAlgorithmException(alg);
@@ -522,7 +528,7 @@ public JWK(JsonObject json) {
522528
case "ES256K":
523529
case "ES512":
524530
case "ES384":
525-
createEC(alg, kty, json);
531+
signingAlgorithm = createEC(alg, kty, json);
526532
break;
527533
default:
528534
throw new NoSuchAlgorithmException(alg);
@@ -541,13 +547,13 @@ public JWK(JsonObject json) {
541547

542548
switch (alg) {
543549
case "HS256":
544-
signingAlgorithm = createOCT("HMacSHA256", json);
550+
signingAlgorithm = createOCT("HS256", "HMacSHA256", json);
545551
break;
546552
case "HS384":
547-
signingAlgorithm = createOCT("HMacSHA384", json);
553+
signingAlgorithm = createOCT("HS384", "HMacSHA384", json);
548554
break;
549555
case "HS512":
550-
signingAlgorithm = createOCT("HMacSHA512", json);
556+
signingAlgorithm = createOCT("HS512", "HMacSHA512", json);
551557
break;
552558
default:
553559
throw new NoSuchAlgorithmException(alg);
@@ -566,7 +572,7 @@ public JWK(JsonObject json) {
566572
}
567573
}
568574

569-
private void createRSA(String alg, String kty, JsonObject json) throws NoSuchAlgorithmException, InvalidKeySpecException, CertificateException, InvalidKeyException, NoSuchProviderException, SignatureException {
575+
private static PubKeySigningAlgorithm createRSA(String alg, String kty, JsonObject json) throws NoSuchAlgorithmException, InvalidKeySpecException, CertificateException, InvalidKeyException, NoSuchProviderException, SignatureException {
570576
PublicKey publicKey = null;
571577
PrivateKey privateKey = null;
572578
// public key
@@ -606,11 +612,12 @@ private void createRSA(String alg, String kty, JsonObject json) throws NoSuchAlg
606612
}
607613

608614
if (publicKey != null || privateKey != null) {
609-
signingAlgorithm = new PubKeySigningAlgorithm(kty, alg, privateKey, publicKey);
615+
return new PubKeySigningAlgorithm(kty, alg, privateKey, publicKey);
610616
}
617+
return null;
611618
}
612619

613-
private void createEC(String alg, String kty, JsonObject json) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidParameterSpecException {
620+
private static SigningAlgorithm createEC(String alg, String kty, JsonObject json) throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidParameterSpecException {
614621
AlgorithmParameters parameters = AlgorithmParameters.getInstance("EC");
615622
parameters.init(new ECGenParameterSpec(translateECCrv(json.getString("crv"))));
616623

@@ -630,8 +637,10 @@ private void createEC(String alg, String kty, JsonObject json) throws NoSuchAlgo
630637
}
631638

632639
if (publicKey != null || privateKey != null) {
633-
signingAlgorithm = new PubKeySigningAlgorithm(kty, alg, privateKey, publicKey);
640+
return new PubKeySigningAlgorithm(kty, alg, privateKey, publicKey);
634641
}
642+
643+
return null;
635644
}
636645

637646
private static SigningAlgorithm createOKP(String alg, String kty, JsonObject json) throws NoSuchAlgorithmException, InvalidKeySpecException {
@@ -700,14 +709,14 @@ private static SigningAlgorithm createOKP(String alg, String kty, JsonObject jso
700709
return null;
701710
}
702711

703-
private static SigningAlgorithm createOCT(String alias, JsonObject json) throws NoSuchAlgorithmException, InvalidKeyException {
712+
private static SigningAlgorithm createOCT(String name, String alias, JsonObject json) throws NoSuchAlgorithmException, InvalidKeyException {
704713
Mac mac = Mac.getInstance(alias);
705714
mac.init(new SecretKeySpec(base64UrlDecode(json.getString("k")), alias));
706-
return new MacSigningAlgorithm(mac);
715+
return new MacSigningAlgorithm(name, mac);
707716
}
708717

709718
public String getAlgorithm() {
710-
return alg;
719+
return signingAlgorithm != null ? signingAlgorithm.name() : null;
711720
}
712721

713722
public String getId() {

vertx-auth-common/src/main/java/io/vertx/ext/auth/impl/jose/SigningAlgorithm.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
public interface SigningAlgorithm {
66

7+
String name();
8+
79
Signer signer() throws GeneralSecurityException;
810

911
}

0 commit comments

Comments
 (0)