Skip to content

Commit

Permalink
Review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mnylen committed Apr 22, 2024
1 parent eb703c3 commit 1fa8ccb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,11 @@ private void assertAlgorithmName(SecretKey key, boolean signing) {
}

// We can ignore key name assertions for generic secrets, because HSM module key algorithm names
// don't always align with JCA standard algorithm names:
boolean pkcs11Key = KeysBridge.isGenericSecret(key);
// don't always align with JCA standard algorithm names
boolean generic = KeysBridge.isGenericSecret(key);

//assert key's jca name is valid if it's a JWA standard algorithm:
if (!pkcs11Key && isJwaStandard() && !isJwaStandardJcaName(name)) {
if (!generic && isJwaStandard() && !isJwaStandardJcaName(name)) {
throw new InvalidKeyException("The " + keyType(signing) + " key's algorithm '" + name +
"' does not equal a valid HmacSHA* algorithm name or PKCS12 OID and cannot be used with " +
getId() + ".");
Expand Down
14 changes: 6 additions & 8 deletions impl/src/main/java/io/jsonwebtoken/impl/security/KeysBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
@SuppressWarnings({"unused"}) // reflection bridge class for the io.jsonwebtoken.security.Keys implementation
public final class KeysBridge {

private static final String SUNPKCS11_GENERIC_SECRET_CLASSNAME = "sun.security.pkcs11.P11Key$P11SecretKey";
private static final String SUNPKCS11_GENERIC_SECRET_ALGNAME = "Generic Secret"; // https://github.com/openjdk/jdk/blob/4f90abaf17716493bad740dcef76d49f16d69379/src/jdk.crypto.cryptoki/share/classes/sun/security/pkcs11/P11KeyStore.java#L1292
private static final String GENERIC_SECRET_ALGNAME = "GenericSecret"; // AWS CloudHSM JCE provider and possibly other HSMs
// Some HSMs use generic secrets. This prefix matches the generic secret algorithm name
// used by SUN PKCS#11 provider, AWS CloudHSM JCE provider and possibly other HSMs
private static final String GENERIC_SECRET_ALG_PREFIX = "Generic";

// prevent instantiation
private KeysBridge() {
Expand Down Expand Up @@ -99,12 +99,10 @@ public static byte[] findEncoded(Key key) {
public static boolean isGenericSecret(Key key) {
if (!(key instanceof SecretKey)) {
return false;
} else if (key.getClass().getName().equals(SUNPKCS11_GENERIC_SECRET_CLASSNAME) &&
SUNPKCS11_GENERIC_SECRET_ALGNAME.equals(key.getAlgorithm())) {
return true;
} else {
return GENERIC_SECRET_ALGNAME.equals(key.getAlgorithm());
}

String algName = Assert.hasText(key.getAlgorithm(), "Key algorithm cannot be null or empty.");
return algName.startsWith(GENERIC_SECRET_ALG_PREFIX);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,32 +63,34 @@ class KeysBridgeTest {

@Test
void testIsGenericSecret() {
def genericSecret = new SecretKey() {
@Override
String getAlgorithm() {
return "GenericSecret" ;
}

@Override
String getFormat() {
return null
def secretKeyWithAlg = { alg ->
new SecretKey() {
@Override
String getAlgorithm() {
return alg
}

@Override
String getFormat() {
return 'RAW'
}

@Override
byte[] getEncoded() {
return new byte[0]
}
}
}

@Override
byte[] getEncoded() {
return null;
}
};

def genericPrivateKey = new PrivateKey() {
PrivateKey genericPrivateKey = new PrivateKey() {
@Override
String getAlgorithm() {
return "GenericSecret";
return "Generic"
}

@Override
String getFormat() {
return null
return "RAW"
}

@Override
Expand All @@ -97,7 +99,9 @@ class KeysBridgeTest {
}
}

assertTrue KeysBridge.isGenericSecret(genericSecret)
assertTrue KeysBridge.isGenericSecret(secretKeyWithAlg("GenericSecret"))
assertTrue KeysBridge.isGenericSecret(secretKeyWithAlg("Generic Secret"))
assertFalse KeysBridge.isGenericSecret(secretKeyWithAlg(" Generic"))
assertFalse KeysBridge.isGenericSecret(TestKeys.HS256)
assertFalse KeysBridge.isGenericSecret(TestKeys.A256GCM)
assertFalse KeysBridge.isGenericSecret(genericPrivateKey)
Expand Down

0 comments on commit 1fa8ccb

Please sign in to comment.