Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -238,13 +239,23 @@ private static JwtBundle toJwtBundle(TrustDomain trustDomain,

final Map<String, PublicKey> 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);
}
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
Loading