Skip to content

Commit 4215134

Browse files
authored
Merge pull request #493 from sigstore/parse-pkcs-rsa-from-tuf
Handle pkcs1 rsa keys in trsuted_root
2 parents 802ca36 + b4d68e9 commit 4215134

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

sigstore-java/src/main/java/dev/sigstore/encryption/Keys.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,7 @@ public static PublicKey parsePublicKey(byte[] keyBytes)
8080
throw new InvalidKeySpecException("Invalid key, empty PEM section");
8181
}
8282
if (section.getType().equals("RSA PUBLIC KEY")) {
83-
ASN1Sequence sequence = ASN1Sequence.getInstance(section.getContent());
84-
ASN1Integer modulus = ASN1Integer.getInstance(sequence.getObjectAt(0));
85-
ASN1Integer exponent = ASN1Integer.getInstance(sequence.getObjectAt(1));
86-
RSAPublicKeySpec keySpec =
87-
new RSAPublicKeySpec(modulus.getPositiveValue(), exponent.getPositiveValue());
88-
KeyFactory factory = KeyFactory.getInstance("RSA");
89-
return factory.generatePublic(keySpec);
83+
return parsePkcs1RsaPublicKey(section.getContent());
9084
}
9185

9286
// otherwise, we are dealing with PKIX X509 encoded keys
@@ -128,6 +122,17 @@ public static PublicKey parsePkixPublicKey(byte[] contents, String algorithm)
128122
return factory.generatePublic(spec);
129123
}
130124

125+
public static PublicKey parsePkcs1RsaPublicKey(byte[] contents)
126+
throws NoSuchAlgorithmException, InvalidKeySpecException {
127+
ASN1Sequence sequence = ASN1Sequence.getInstance(contents);
128+
ASN1Integer modulus = ASN1Integer.getInstance(sequence.getObjectAt(0));
129+
ASN1Integer exponent = ASN1Integer.getInstance(sequence.getObjectAt(1));
130+
RSAPublicKeySpec keySpec =
131+
new RSAPublicKeySpec(modulus.getPositiveValue(), exponent.getPositiveValue());
132+
KeyFactory factory = KeyFactory.getInstance("RSA");
133+
return factory.generatePublic(keySpec);
134+
}
135+
131136
/**
132137
* Valid values for scheme are:
133138
*

sigstore-java/src/main/java/dev/sigstore/trustroot/PublicKey.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ public abstract class PublicKey {
3131

3232
@Lazy
3333
public java.security.PublicKey toJavaPublicKey()
34-
throws InvalidKeySpecException, NoSuchAlgorithmException {
35-
if (!getKeyDetails().equals("PKIX_ECDSA_P256_SHA_256")) {
36-
throw new InvalidKeySpecException("Unsupported key algorithm: " + getKeyDetails());
34+
throws NoSuchAlgorithmException, InvalidKeySpecException {
35+
if (getKeyDetails().equals("PKIX_ECDSA_P256_SHA_256")) {
36+
return Keys.parsePkixPublicKey(getRawBytes(), "EC");
3737
}
38-
return Keys.parsePkixPublicKey(getRawBytes(), "EC");
38+
if (getKeyDetails().equals("PKCS1_RSA_PKCS1V5")) {
39+
return Keys.parsePkcs1RsaPublicKey(getRawBytes());
40+
}
41+
throw new InvalidKeySpecException("Unsupported key algorithm: " + getKeyDetails());
3942
}
4043

4144
public static PublicKey from(dev.sigstore.proto.common.v1.PublicKey proto) {

sigstore-java/src/test/java/dev/sigstore/tuf/SigstoreTufClientTest.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import com.google.protobuf.util.JsonFormat;
1919
import dev.sigstore.proto.trustroot.v1.TrustedRoot;
20+
import dev.sigstore.trustroot.SigstoreTrustedRoot;
2021
import java.io.IOException;
2122
import java.nio.charset.StandardCharsets;
2223
import java.nio.file.Path;
@@ -38,23 +39,32 @@ public void testUpdate_publicGoodHasTrustedRootJson() throws Exception {
3839
.tufCacheLocation(localStorePath)
3940
.build();
4041
client.forceUpdate();
41-
Assertions.assertNotNull(client.getSigstoreTrustedRoot());
4242

43-
Assertions.assertDoesNotThrow(() -> client.getSigstoreTrustedRoot().getTLogs().current());
44-
Assertions.assertDoesNotThrow(() -> client.getSigstoreTrustedRoot().getCTLogs().current());
45-
Assertions.assertDoesNotThrow(() -> client.getSigstoreTrustedRoot().getCAs().current());
43+
assertTrustedRootValid(client.getSigstoreTrustedRoot());
4644
}
4745

4846
@Test
4947
public void testUpdate_stagingHasTrustedRootJson() throws Exception {
5048
var client =
5149
SigstoreTufClient.builder().useStagingInstance().tufCacheLocation(localStorePath).build();
5250
client.forceUpdate();
53-
Assertions.assertNotNull(client.getSigstoreTrustedRoot());
5451

55-
Assertions.assertDoesNotThrow(() -> client.getSigstoreTrustedRoot().getTLogs().current());
56-
Assertions.assertDoesNotThrow(() -> client.getSigstoreTrustedRoot().getCTLogs().current());
57-
Assertions.assertDoesNotThrow(() -> client.getSigstoreTrustedRoot().getCAs().current());
52+
assertTrustedRootValid(client.getSigstoreTrustedRoot());
53+
}
54+
55+
private void assertTrustedRootValid(SigstoreTrustedRoot trustedRoot) throws Exception {
56+
Assertions.assertNotNull(trustedRoot);
57+
Assertions.assertDoesNotThrow(() -> trustedRoot.getTLogs().current());
58+
Assertions.assertDoesNotThrow(() -> trustedRoot.getCTLogs().current());
59+
Assertions.assertDoesNotThrow(() -> trustedRoot.getCAs().current());
60+
61+
for (var tlog : trustedRoot.getTLogs()) {
62+
Assertions.assertDoesNotThrow(() -> tlog.getPublicKey().toJavaPublicKey());
63+
}
64+
65+
for (var ctlog : trustedRoot.getCTLogs()) {
66+
Assertions.assertDoesNotThrow(() -> ctlog.getPublicKey().toJavaPublicKey());
67+
}
5868
}
5969

6070
@Test

0 commit comments

Comments
 (0)