|
15 | 15 | */
|
16 | 16 | package dev.sigstore.encryption.signers;
|
17 | 17 |
|
| 18 | +import java.nio.charset.StandardCharsets; |
| 19 | +import java.security.KeyPair; |
18 | 20 | import java.security.KeyPairGenerator;
|
19 | 21 | import java.security.NoSuchAlgorithmException;
|
| 22 | +import java.security.Security; |
| 23 | +import java.security.Signature; |
| 24 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; |
20 | 25 | import org.junit.jupiter.api.Assertions;
|
21 | 26 | import org.junit.jupiter.api.Test;
|
| 27 | +import org.junit.jupiter.api.condition.EnabledForJreRange; |
| 28 | +import org.junit.jupiter.api.condition.JRE; |
22 | 29 |
|
23 | 30 | /** VerifiersTest for failure cases, passing cases are handled in {@link SignerTest}. */
|
24 | 31 | public class VerifiersTest {
|
| 32 | + private static final byte[] CONTENT = "abcdef".getBytes(StandardCharsets.UTF_8); |
25 | 33 |
|
26 | 34 | @Test
|
27 |
| - public void signatureAlgorithm_unknown() throws Exception { |
28 |
| - var kp = KeyPairGenerator.getInstance("DSA").generateKeyPair(); |
| 35 | + public void verify_ed25519_withBcProvider() throws Exception { |
| 36 | + var kp = genKeyPairWithBcProvider("ed25519"); |
| 37 | + var signature = genSignature(kp, "ed25519"); |
| 38 | + var verifier = Verifiers.newVerifier(kp.getPublic()); |
| 39 | + Assertions.assertTrue(verifier.verify(CONTENT, signature)); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void verify_ed25519_withoutBcProvider() throws Exception { |
| 44 | + var kp = genKeyPair("ed25519"); |
| 45 | + var signature = genSignature(kp, "ed25519"); |
| 46 | + var verifier = Verifiers.newVerifier(kp.getPublic()); |
| 47 | + Assertions.assertTrue(verifier.verify(CONTENT, signature)); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void verify_ed448_withBcProvider() throws Exception { |
| 52 | + var kp = genKeyPairWithBcProvider("ed448"); |
| 53 | + var signature = genSignature(kp, "ed448"); |
29 | 54 | var exception =
|
30 | 55 | Assertions.assertThrows(
|
31 | 56 | NoSuchAlgorithmException.class, () -> Verifiers.newVerifier(kp.getPublic()));
|
32 | 57 | Assertions.assertEquals(
|
33 |
| - exception.getMessage(), |
34 |
| - "Cannot verify signatures for key type 'DSA', this client only supports RSA and ECDSA verification"); |
| 58 | + "Cannot verify signatures for key type 'Ed448', this client only supports RSA, ECDSA, and Ed25519 verification", |
| 59 | + exception.getMessage()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + @EnabledForJreRange(min = JRE.JAVA_15) |
| 64 | + public void verify_ed448_withoutBcProvider() throws Exception { |
| 65 | + var kp = genKeyPair("ed448"); |
| 66 | + var signature = genSignature(kp, "ed448"); |
| 67 | + var exception = |
| 68 | + Assertions.assertThrows( |
| 69 | + NoSuchAlgorithmException.class, () -> Verifiers.newVerifier(kp.getPublic())); |
| 70 | + Assertions.assertEquals( |
| 71 | + "Cannot verify signatures for non-Ed25519 EdDSA key types, this client only supports RSA, ECDSA, and Ed25519 verification", |
| 72 | + exception.getMessage()); |
35 | 73 | }
|
36 | 74 |
|
37 | 75 | @Test
|
38 |
| - public void signatureAlgorithmForDigests_unknown() throws Exception { |
| 76 | + public void verify_unknown() throws Exception { |
39 | 77 | var kp = KeyPairGenerator.getInstance("DSA").generateKeyPair();
|
40 | 78 | var exception =
|
41 | 79 | Assertions.assertThrows(
|
42 | 80 | NoSuchAlgorithmException.class, () -> Verifiers.newVerifier(kp.getPublic()));
|
43 | 81 | Assertions.assertEquals(
|
44 | 82 | exception.getMessage(),
|
45 |
| - "Cannot verify signatures for key type 'DSA', this client only supports RSA and ECDSA verification"); |
| 83 | + "Cannot verify signatures for key type 'DSA', this client only supports RSA, ECDSA, and Ed25519 verification"); |
| 84 | + } |
| 85 | + |
| 86 | + private KeyPair genKeyPair(String algorithm) throws Exception { |
| 87 | + KeyPairGenerator kpGen = KeyPairGenerator.getInstance(algorithm); |
| 88 | + return kpGen.generateKeyPair(); |
| 89 | + } |
| 90 | + |
| 91 | + private KeyPair genKeyPairWithBcProvider(String algorithm) throws Exception { |
| 92 | + Security.addProvider(new BouncyCastleProvider()); |
| 93 | + |
| 94 | + KeyPairGenerator kpGen = KeyPairGenerator.getInstance(algorithm, "BC"); |
| 95 | + return kpGen.generateKeyPair(); |
| 96 | + } |
| 97 | + |
| 98 | + private byte[] genSignature(KeyPair keyPair, String algorithm) throws Exception { |
| 99 | + Signature signature = Signature.getInstance(algorithm); |
| 100 | + signature.initSign(keyPair.getPrivate()); |
| 101 | + signature.update(CONTENT); |
| 102 | + return signature.sign(); |
46 | 103 | }
|
47 | 104 | }
|
0 commit comments