diff --git a/CHANGELOG.md b/CHANGELOG.md index e2245f17..3854f7c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ ### Fixed * Reject X.509-SVID leaf certificates with a root SPIFFE ID path in `X509SvidValidator.verifyChain`, which `SpiffeTrustManager` uses for peer certificates (#510) +* Ignore JWKs with an unsupported key type when parsing a JWT bundle instead of rejecting the whole bundle, per the SPIFFE bundle spec (#512) ### Build diff --git a/java-spiffe-core/src/main/java/io/spiffe/bundle/jwtbundle/JwtBundle.java b/java-spiffe-core/src/main/java/io/spiffe/bundle/jwtbundle/JwtBundle.java index 007fd17f..42ec2d54 100644 --- a/java-spiffe-core/src/main/java/io/spiffe/bundle/jwtbundle/JwtBundle.java +++ b/java-spiffe-core/src/main/java/io/spiffe/bundle/jwtbundle/JwtBundle.java @@ -5,6 +5,7 @@ import com.nimbusds.jose.jwk.ECKey; import com.nimbusds.jose.jwk.JWK; import com.nimbusds.jose.jwk.JWKSet; +import com.nimbusds.jose.jwk.KeyType; import com.nimbusds.jose.jwk.RSAKey; import io.spiffe.internal.JwtSignatureAlgorithm; import io.spiffe.bundle.BundleSource; @@ -238,6 +239,11 @@ private static JwtBundle toJwtBundle(TrustDomain trustDomain, final Map authorities = new ConcurrentHashMap<>(); for (JWK jwk : jwkSet.getKeys()) { + // The SPIFFE bundle spec says JWKs with a key type the client doesn't + // understand must be ignored, so they shouldn't fail the whole bundle. + if (!isSupportedKeyType(jwk)) { + continue; + } String keyId = getKeyId(jwk); PublicKey publicKey = getPublicKey(jwk); authorities.put(keyId, publicKey); @@ -245,6 +251,11 @@ private static JwtBundle toJwtBundle(TrustDomain trustDomain, return new JwtBundle(trustDomain, authorities); } + private static boolean isSupportedKeyType(final JWK jwk) { + KeyType keyType = jwk.getKeyType(); + return KeyType.EC.equals(keyType) || KeyType.RSA.equals(keyType); + } + private static String getKeyId(final JWK jwk) throws JwtBundleException { Objects.requireNonNull(jwk, "jwk must not be null"); String keyId = jwk.getKeyID(); diff --git a/java-spiffe-core/src/test/java/io/spiffe/bundle/jwtbundle/JwtBundleTest.java b/java-spiffe-core/src/test/java/io/spiffe/bundle/jwtbundle/JwtBundleTest.java index 4d525453..d7bb2be3 100644 --- a/java-spiffe-core/src/test/java/io/spiffe/bundle/jwtbundle/JwtBundleTest.java +++ b/java-spiffe-core/src/test/java/io/spiffe/bundle/jwtbundle/JwtBundleTest.java @@ -171,16 +171,26 @@ void testLoadFile_MissingKid_ThrowsJwtBundleException() throws URISyntaxExceptio } @Test - void testLoadFile_InvalidKeyType_ThrowsKeyException() throws URISyntaxException, KeyException { + void testLoadFile_UnsupportedKeyType_IsIgnored() throws URISyntaxException, JwtBundleException, KeyException { Path path = Paths.get(toUri("testdata/jwtbundle/jwks_invalid_keytype.json")); TrustDomain trustDomain = TrustDomain.parse("domain.test"); - try { - JwtBundle.load(trustDomain, path); - fail("should have thrown exception"); - } catch (JwtBundleException e) { - assertEquals("Unsupported JWT family algorithm: OKP", e.getCause().getMessage()); - } + JwtBundle jwtBundle = JwtBundle.load(trustDomain, path); + + assertEquals(trustDomain, jwtBundle.getTrustDomain()); + assertTrue(jwtBundle.getJwtAuthorities().isEmpty()); + } + + @Test + void testLoadFile_UnsupportedKeyTypeNextToECKey_KeepsECKey() throws URISyntaxException, JwtBundleException, KeyException { + Path path = Paths.get(toUri("testdata/jwtbundle/jwks_EC_and_OKP.json")); + TrustDomain trustDomain = TrustDomain.parse("domain.test"); + + JwtBundle jwtBundle = JwtBundle.load(trustDomain, path); + + assertEquals(1, jwtBundle.getJwtAuthorities().size()); + assertNotNull(jwtBundle.getJwtAuthorities().get("C6vs25welZOx6WksNYfbMfiw9l96pMnD")); + assertFalse(jwtBundle.hasJwtAuthority("okp-key")); } @Test @@ -267,18 +277,16 @@ void testParse_MissingKid_Fails() throws URISyntaxException, IOException { } @Test - void testParse_InvalidKeyType_ThrowsJwtBundleException() throws URISyntaxException, IOException { - Path path = Paths.get(toUri("testdata/jwtbundle/jwks_invalid_keytype.json")); + void testParse_UnsupportedKeyTypeNextToECKey_KeepsECKey() throws URISyntaxException, IOException, JwtBundleException { + Path path = Paths.get(toUri("testdata/jwtbundle/jwks_EC_and_OKP.json")); byte[] bundleBytes = Files.readAllBytes(path); TrustDomain trustDomain = TrustDomain.parse("domain.test"); - try { - JwtBundle.parse(trustDomain, bundleBytes); - fail("should have thrown exception"); - } catch (JwtBundleException e) { - assertEquals("Could not parse bundle from bytes", e.getMessage()); - assertEquals("Unsupported JWT family algorithm: OKP", e.getCause().getMessage()); - } + JwtBundle jwtBundle = JwtBundle.parse(trustDomain, bundleBytes); + + assertEquals(1, jwtBundle.getJwtAuthorities().size()); + assertTrue(jwtBundle.hasJwtAuthority("C6vs25welZOx6WksNYfbMfiw9l96pMnD")); + assertFalse(jwtBundle.hasJwtAuthority("okp-key")); } @Test diff --git a/java-spiffe-core/src/test/java/io/spiffe/workloadapi/GrpcConversionUtilsTest.java b/java-spiffe-core/src/test/java/io/spiffe/workloadapi/GrpcConversionUtilsTest.java index cd52f44d..5bfd475e 100644 --- a/java-spiffe-core/src/test/java/io/spiffe/workloadapi/GrpcConversionUtilsTest.java +++ b/java-spiffe-core/src/test/java/io/spiffe/workloadapi/GrpcConversionUtilsTest.java @@ -1,6 +1,8 @@ package io.spiffe.workloadapi; import com.google.protobuf.ByteString; +import io.spiffe.bundle.jwtbundle.JwtBundle; +import io.spiffe.bundle.jwtbundle.JwtBundleSet; import io.spiffe.bundle.x509bundle.X509Bundle; import io.spiffe.bundle.x509bundle.X509BundleSet; import io.spiffe.exception.BundleNotFoundException; @@ -26,6 +28,7 @@ import static io.spiffe.utils.TestUtils.toUri; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; class GrpcConversionUtilsTest { @@ -54,19 +57,16 @@ void test_toJwtBundleSet_emtpyResponse() { } @Test - void test_toJwtBundleSet_unsupportedKeyType() throws URISyntaxException, IOException { + void test_toJwtBundleSet_unsupportedKeyType() throws Exception { byte[] bundleBytes = Files.readAllBytes(Paths.get(toUri("testdata/jwtbundle/jwks_invalid_keytype.json"))); Workload.JWTBundlesResponse response = Workload.JWTBundlesResponse .newBuilder() .putBundles("example.org", ByteString.copyFrom(bundleBytes)) .build(); - try { - GrpcConversionUtils.toJwtBundleSet(response); - fail("should have thrown exception"); - } catch (JwtBundleException e) { - assertEquals("Could not parse bundle from bytes", e.getMessage()); - } + JwtBundleSet bundleSet = GrpcConversionUtils.toJwtBundleSet(response); + JwtBundle bundle = bundleSet.getBundleForTrustDomain(TrustDomain.parse("example.org")); + assertTrue(bundle.getJwtAuthorities().isEmpty()); } @Test diff --git a/java-spiffe-core/src/test/resources/testdata/jwtbundle/jwks_EC_and_OKP.json b/java-spiffe-core/src/test/resources/testdata/jwtbundle/jwks_EC_and_OKP.json new file mode 100644 index 00000000..a2b1353f --- /dev/null +++ b/java-spiffe-core/src/test/resources/testdata/jwtbundle/jwks_EC_and_OKP.json @@ -0,0 +1,18 @@ +{ + "keys": [ + { + "kty": "OKP", + "crv": "Ed25519", + "kid": "okp-key", + "x": "c2Rmc2Zk", + "alg": "EdDSA" + }, + { + "kty": "EC", + "kid": "C6vs25welZOx6WksNYfbMfiw9l96pMnD", + "crv": "P-256", + "x": "ngLYQnlfF6GsojUwqtcEE3WgTNG2RUlsGhK73RNEl5k", + "y": "tKbiDSUSsQ3F1P7wteeHNXIcU-cx6CgSbroeQrQHTLM" + } + ] +}