Skip to content

Commit c66885c

Browse files
authored
Add support for ED25519 in trusted_root (#983)
Signed-off-by: Appu Goundan <[email protected]>
1 parent 1267a52 commit c66885c

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.gradle
2+
.kotlin
3+
/**/.kotlin
24
/build
35
/**/build
46
/out
@@ -32,4 +34,4 @@
3234
!/.idea/icon.png
3335

3436
# vscode java output directories
35-
/**/bin
37+
/**/bin

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ private static PublicKey parse(byte[] contents, String type) throws InvalidKeySp
9797
var keySpec = new X509EncodedKeySpec(contents);
9898
var factory = KeyFactory.getInstance(type, BouncyCastleProvider.PROVIDER_NAME);
9999
return factory.generatePublic(keySpec);
100+
} catch (ArrayIndexOutOfBoundsException aoe) {
101+
throw new InvalidKeySpecException(aoe);
100102
} catch (NoSuchProviderException | NoSuchAlgorithmException e) {
101103
throw new RuntimeException(e);
102104
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public java.security.PublicKey toJavaPublicKey()
4141
if (getKeyDetails().equals("PKCS1_RSA_PKCS1V5")) {
4242
return Keys.parseRsaPkcs1(getRawBytes());
4343
}
44+
if (getKeyDetails().equals("PKIX_ED25519")) {
45+
return Keys.parseEd25519(getRawBytes());
46+
}
4447
throw new InvalidKeySpecException("Unsupported key algorithm: " + getKeyDetails());
4548
}
4649

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 2025 The Sigstore Authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package dev.sigstore.trustroot;
17+
18+
import static org.junit.jupiter.api.Assertions.*;
19+
20+
import java.security.spec.InvalidKeySpecException;
21+
import java.time.Instant;
22+
import org.bouncycastle.util.encoders.Base64;
23+
import org.junit.jupiter.api.Assertions;
24+
import org.junit.jupiter.api.Test;
25+
26+
class PublicKeyTest {
27+
28+
@Test
29+
void toJavaPublicKey_edsa() throws Exception {
30+
var pk =
31+
makeKey(
32+
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEDODRU688UYGuy54mNUlaEBiQdTE9nYLr0lg6RXowI/QV/RE1azBn4Eg5/2uTOMbhB1/gfcHzijzFi9Tk+g1Prg==",
33+
"PKIX_ECDSA_P256_SHA_256");
34+
Assertions.assertEquals("ECDSA", pk.toJavaPublicKey().getAlgorithm());
35+
}
36+
37+
@Test
38+
void toJavaPublicKey_edsaFail() {
39+
var pk = makeKey("eA==", "PKIX_ECDSA_P256_SHA_256");
40+
Assertions.assertThrows(InvalidKeySpecException.class, pk::toJavaPublicKey);
41+
}
42+
43+
@Test
44+
void toJavaPublicKey_ed25519() throws Exception {
45+
var pk =
46+
makeKey("MCowBQYDK2VwAyEAPn+AREHoBaZ7wgS1zBqpxmLSGnyhxXj4lFxSdWVB8o8=", "PKIX_ED25519");
47+
Assertions.assertEquals("Ed25519", pk.toJavaPublicKey().getAlgorithm());
48+
}
49+
50+
@Test
51+
void toJavaPublicKey_ed25519Fail() {
52+
var pk = makeKey("eA==", "PKIX_ED25519");
53+
Assertions.assertThrows(InvalidKeySpecException.class, pk::toJavaPublicKey);
54+
}
55+
56+
@Test
57+
void toJavaPublicKey_rsa() throws Exception {
58+
var pk =
59+
makeKey(
60+
"MIICCgKCAgEA27A2MPQXm0I0v7/Ly5BIauDjRZF5Jor9vU+QheoE2UIIsZHcyYq3slHzSSHy2lLj1ZD2d91CtJ492ZXqnBmsr4TwZ9jQ05tW2mGIRI8u2DqN8LpuNYZGz/f9SZrjhQQmUttqWmtu3UoLfKz6NbNXUnoo+NhZFcFRLXJ8VporVhuiAmL7zqT53cXR3yQfFPCUDeGnRksnlhVIAJc3AHZZSHQJ8DEXMhh35TVv2nYhTI3rID7GwjXXw4ocz7RGDD37ky6p39Tl5NB71gT1eSqhZhGHEYHIPXraEBd5+3w9qIuLWlp5Ej/K6Mu4ELioXKCUimCbwy+Cs8UhHFlqcyg4AysOHJwIadXIa8LsY51jnVSGrGOEBZevopmQPNPtyfFY3dmXSS+6Z3RD2Gd6oDnNGJzpSyEk410Ag5uvNDfYzJLCWX9tU8lIxNwdFYmIwpd89HijyRyoGnoJ3entd63cvKfuuix5r+GHyKp1Xm1L5j5AWM6P+z0xigwkiXnt+adexAl1J9wdDxv/pUFEESRF4DG8DFGVtbdH6aR1A5/vD4krO4tC1QYUSeyL5Mvsw8WRqIFHcXtgybtxylljvNcGMV1KXQC8UFDmpGZVDSHx6v3e/BHMrZ7gjoCCfVMZ/cFcQi0W2AIHPYEMH/C95J2r4XbHMRdYXpovpOoT5Ca78gsCAwEAAQ==",
61+
"PKCS1_RSA_PKCS1V5");
62+
Assertions.assertEquals("RSA", pk.toJavaPublicKey().getAlgorithm());
63+
}
64+
65+
@Test
66+
void toJavaPublicKey_rsaFail() {
67+
var pk = makeKey("eA==", "PKCS1_RSA_PKCS1V5");
68+
Assertions.assertThrows(InvalidKeySpecException.class, pk::toJavaPublicKey);
69+
}
70+
71+
static PublicKey makeKey(String b64, String keyDetails) {
72+
return ImmutablePublicKey.builder()
73+
.rawBytes(Base64.decode(b64))
74+
.keyDetails(keyDetails)
75+
.validFor(ImmutableValidFor.builder().start(Instant.now()).build())
76+
.build();
77+
}
78+
}

0 commit comments

Comments
 (0)