|
20 | 20 | import java.security.interfaces.ECPrivateKey;
|
21 | 21 |
|
22 | 22 | import org.junit.jupiter.api.Test;
|
| 23 | +import org.junit.jupiter.api.condition.EnabledForJreRange; |
| 24 | +import org.junit.jupiter.api.condition.JRE; |
23 | 25 | import org.junit.jupiter.params.ParameterizedTest;
|
| 26 | +import org.junit.jupiter.params.provider.CsvSource; |
24 | 27 | import org.junit.jupiter.params.provider.ValueSource;
|
25 | 28 |
|
26 | 29 | import static org.assertj.core.api.Assertions.assertThat;
|
27 | 30 | import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
| 31 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
28 | 32 |
|
29 | 33 | /**
|
30 | 34 | * Tests for {@link PrivateKeyParser}.
|
|
33 | 37 | * @author Moritz Halbritter
|
34 | 38 | * @author Phillip Webb
|
35 | 39 | */
|
| 40 | +// https://docs.oracle.com/en/java/javase/17/security/oracle-providers.html#GUID-091BF58C-82AB-4C9C-850F-1660824D5254 |
36 | 41 | class PrivateKeyParserTests {
|
37 | 42 |
|
38 |
| - @Test |
39 |
| - void parsePkcs8RsaKeyFile() { |
40 |
| - PrivateKey privateKey = PrivateKeyParser.parse("classpath:ssl/pkcs8/key-rsa.pem"); |
| 43 | + @ParameterizedTest |
| 44 | + // @formatter:off |
| 45 | + @CsvSource({ |
| 46 | + "dsa.key, DSA", |
| 47 | + "rsa.key, RSA", |
| 48 | + "rsa-pss.key, RSASSA-PSS" |
| 49 | + }) |
| 50 | + // @formatter:on |
| 51 | + void shouldParseTraditionalPkcs8(String file, String algorithm) { |
| 52 | + PrivateKey privateKey = PrivateKeyParser.parse("classpath:org/springframework/boot/web/server/pkcs8/" + file); |
| 53 | + assertThat(privateKey).isNotNull(); |
| 54 | + assertThat(privateKey.getFormat()).isEqualTo("PKCS#8"); |
| 55 | + assertThat(privateKey.getAlgorithm()).isEqualTo(algorithm); |
| 56 | + } |
| 57 | + |
| 58 | + @ParameterizedTest |
| 59 | + // @formatter:off |
| 60 | + @CsvSource({ |
| 61 | + "rsa.key, RSA" |
| 62 | + }) |
| 63 | + // @formatter:on |
| 64 | + void shouldParseTraditionalPkcs1(String file, String algorithm) { |
| 65 | + PrivateKey privateKey = PrivateKeyParser.parse("classpath:org/springframework/boot/web/server/pkcs1/" + file); |
41 | 66 | assertThat(privateKey).isNotNull();
|
42 | 67 | assertThat(privateKey.getFormat()).isEqualTo("PKCS#8");
|
43 |
| - assertThat(privateKey.getAlgorithm()).isEqualTo("RSA"); |
| 68 | + assertThat(privateKey.getAlgorithm()).isEqualTo(algorithm); |
| 69 | + } |
| 70 | + |
| 71 | + @ParameterizedTest |
| 72 | + // @formatter:off |
| 73 | + @ValueSource(strings = { |
| 74 | + "dsa.key" |
| 75 | + }) |
| 76 | + // @formatter:on |
| 77 | + void shouldNotParseUnsupportedTraditionalPkcs1(String file) { |
| 78 | + assertThatThrownBy(() -> PrivateKeyParser.parse("classpath:org/springframework/boot/web/server/pkcs1/" + file)) |
| 79 | + .isInstanceOf(IllegalStateException.class) |
| 80 | + .hasMessageContaining("Error loading private key file") |
| 81 | + .hasCauseInstanceOf(IllegalStateException.class) |
| 82 | + .getCause() |
| 83 | + .hasMessageContaining("Unrecognized private key format"); |
44 | 84 | }
|
45 | 85 |
|
46 | 86 | @ParameterizedTest
|
47 |
| - @ValueSource(strings = { "key-ec-nist-p256.pem", "key-ec-nist-p384.pem", "key-ec-prime256v1.pem", |
48 |
| - "key-ec-secp256r1.pem" }) |
49 |
| - void parsePkcs8EcKeyFile(String fileName) { |
50 |
| - PrivateKey privateKey = PrivateKeyParser.parse("classpath:ssl/pkcs8/" + fileName); |
| 87 | + // @formatter:off |
| 88 | + @CsvSource({ |
| 89 | + "brainpoolP256r1.key, brainpoolP256r1, 1.3.36.3.3.2.8.1.1.7", |
| 90 | + "brainpoolP320r1.key, brainpoolP320r1, 1.3.36.3.3.2.8.1.1.9", |
| 91 | + "brainpoolP384r1.key, brainpoolP384r1, 1.3.36.3.3.2.8.1.1.11", |
| 92 | + "brainpoolP512r1.key, brainpoolP512r1, 1.3.36.3.3.2.8.1.1.13", |
| 93 | + "prime256v1.key, secp256r1, 1.2.840.10045.3.1.7", |
| 94 | + "secp224r1.key, secp224r1, 1.3.132.0.33", |
| 95 | + "secp256k1.key, secp256k1, 1.3.132.0.10", |
| 96 | + "secp256r1.key, secp256r1, 1.2.840.10045.3.1.7", |
| 97 | + "secp384r1.key, secp384r1, 1.3.132.0.34", |
| 98 | + "secp521r1.key, secp521r1, 1.3.132.0.35" |
| 99 | + }) |
| 100 | + // @formatter:on |
| 101 | + void shouldParseEcPkcs8(String file, String curveName, String oid) { |
| 102 | + PrivateKey privateKey = PrivateKeyParser.parse("classpath:org/springframework/boot/web/server/pkcs8/" + file); |
51 | 103 | assertThat(privateKey).isNotNull();
|
52 | 104 | assertThat(privateKey.getFormat()).isEqualTo("PKCS#8");
|
53 | 105 | assertThat(privateKey.getAlgorithm()).isEqualTo("EC");
|
| 106 | + assertThat(privateKey).isInstanceOf(ECPrivateKey.class); |
| 107 | + ECPrivateKey ecPrivateKey = (ECPrivateKey) privateKey; |
| 108 | + assertThat(ecPrivateKey.getParams().toString()).contains(curveName).contains(oid); |
54 | 109 | }
|
55 | 110 |
|
56 |
| - @Test |
57 |
| - void parsePkcs8DsaKeyFile() { |
58 |
| - PrivateKey privateKey = PrivateKeyParser.parse("classpath:ssl/pkcs8/key-dsa.pem"); |
| 111 | + @ParameterizedTest |
| 112 | + // @formatter:off |
| 113 | + @ValueSource(strings = { |
| 114 | + "brainpoolP256t1.key", |
| 115 | + "brainpoolP320t1.key", |
| 116 | + "brainpoolP384t1.key", |
| 117 | + "brainpoolP512t1.key" |
| 118 | + }) |
| 119 | + // @formatter:on |
| 120 | + void shouldNotParseUnsupportedEcPkcs8(String file) { |
| 121 | + assertThatThrownBy(() -> PrivateKeyParser.parse("classpath:org/springframework/boot/web/server/pkcs8/" + file)) |
| 122 | + .isInstanceOf(IllegalStateException.class) |
| 123 | + .hasMessageContaining("Error loading private key file") |
| 124 | + .hasCauseInstanceOf(IllegalStateException.class) |
| 125 | + .getCause() |
| 126 | + .hasMessageContaining("Unrecognized private key format"); |
| 127 | + } |
| 128 | + |
| 129 | + @EnabledForJreRange(min = JRE.JAVA_17, disabledReason = "EdDSA is only supported since Java 17") |
| 130 | + @ParameterizedTest |
| 131 | + // @formatter:off |
| 132 | + @ValueSource(strings = { |
| 133 | + "ed448.key", |
| 134 | + "ed25519.key" |
| 135 | + }) |
| 136 | + // @formatter:on |
| 137 | + void shouldParseEdDsaPkcs8(String file) { |
| 138 | + PrivateKey privateKey = PrivateKeyParser.parse("classpath:org/springframework/boot/web/server/pkcs8/" + file); |
59 | 139 | assertThat(privateKey).isNotNull();
|
60 | 140 | assertThat(privateKey.getFormat()).isEqualTo("PKCS#8");
|
61 |
| - assertThat(privateKey.getAlgorithm()).isEqualTo("DSA"); |
| 141 | + assertThat(privateKey.getAlgorithm()).isEqualTo("EdDSA"); |
62 | 142 | }
|
63 | 143 |
|
64 |
| - @Test |
65 |
| - void parsePemKeyFileWithEcdsa() { |
66 |
| - ECPrivateKey privateKey = (ECPrivateKey) PrivateKeyParser.parse("classpath:test-ec-key.pem"); |
| 144 | + @EnabledForJreRange(min = JRE.JAVA_17, disabledReason = "XDH is only supported since Java 17") |
| 145 | + @ParameterizedTest |
| 146 | + // @formatter:off |
| 147 | + @ValueSource(strings = { |
| 148 | + "x448.key", |
| 149 | + "x25519.key" |
| 150 | + }) |
| 151 | + // @formatter:on |
| 152 | + void shouldParseXdhPkcs8(String file) { |
| 153 | + PrivateKey privateKey = PrivateKeyParser.parse("classpath:org/springframework/boot/web/server/pkcs8/" + file); |
67 | 154 | assertThat(privateKey).isNotNull();
|
68 | 155 | assertThat(privateKey.getFormat()).isEqualTo("PKCS#8");
|
69 |
| - assertThat(privateKey.getAlgorithm()).isEqualTo("EC"); |
70 |
| - assertThat(privateKey.getParams().toString()).contains("1.3.132.0.34").doesNotContain("prime256v1"); |
| 156 | + assertThat(privateKey.getAlgorithm()).isEqualTo("XDH"); |
71 | 157 | }
|
72 | 158 |
|
73 |
| - @Test |
74 |
| - void parsePemKeyFileWithEcdsaPrime256v1() { |
75 |
| - ECPrivateKey privateKey = (ECPrivateKey) PrivateKeyParser.parse("classpath:test-ec-key-prime256v1.pem"); |
| 159 | + @ParameterizedTest |
| 160 | + // @formatter:off |
| 161 | + @CsvSource({ |
| 162 | + "brainpoolP256r1.key, brainpoolP256r1, 1.3.36.3.3.2.8.1.1.7", |
| 163 | + "brainpoolP320r1.key, brainpoolP320r1, 1.3.36.3.3.2.8.1.1.9", |
| 164 | + "brainpoolP384r1.key, brainpoolP384r1, 1.3.36.3.3.2.8.1.1.11", |
| 165 | + "brainpoolP512r1.key, brainpoolP512r1, 1.3.36.3.3.2.8.1.1.13", |
| 166 | + "prime256v1.key, secp256r1, 1.2.840.10045.3.1.7", |
| 167 | + "secp224r1.key, secp224r1, 1.3.132.0.33", |
| 168 | + "secp256k1.key, secp256k1, 1.3.132.0.10", |
| 169 | + "secp256r1.key, secp256r1, 1.2.840.10045.3.1.7", |
| 170 | + "secp384r1.key, secp384r1, 1.3.132.0.34", |
| 171 | + "secp521r1.key, secp521r1, 1.3.132.0.35" |
| 172 | + }) |
| 173 | + // @formatter:on |
| 174 | + void shouldParseEcSec1(String file, String curveName, String oid) { |
| 175 | + PrivateKey privateKey = PrivateKeyParser.parse("classpath:org/springframework/boot/web/server/sec1/" + file); |
76 | 176 | assertThat(privateKey).isNotNull();
|
77 | 177 | assertThat(privateKey.getFormat()).isEqualTo("PKCS#8");
|
78 | 178 | assertThat(privateKey.getAlgorithm()).isEqualTo("EC");
|
79 |
| - assertThat(privateKey.getParams().toString()).contains("prime256v1").doesNotContain("1.3.132.0.34"); |
| 179 | + assertThat(privateKey).isInstanceOf(ECPrivateKey.class); |
| 180 | + ECPrivateKey ecPrivateKey = (ECPrivateKey) privateKey; |
| 181 | + assertThat(ecPrivateKey.getParams().toString()).contains(curveName).contains(oid); |
| 182 | + } |
| 183 | + |
| 184 | + @ParameterizedTest |
| 185 | + // @formatter:off |
| 186 | + @ValueSource(strings = { |
| 187 | + "brainpoolP256t1.key", |
| 188 | + "brainpoolP320t1.key", |
| 189 | + "brainpoolP384t1.key", |
| 190 | + "brainpoolP512t1.key" |
| 191 | + }) |
| 192 | + // @formatter:on |
| 193 | + void shouldNotParseUnsupportedEcSec1(String file) { |
| 194 | + assertThatThrownBy(() -> PrivateKeyParser.parse("classpath:org/springframework/boot/web/server/sec1/" + file)) |
| 195 | + .isInstanceOf(IllegalStateException.class) |
| 196 | + .hasMessageContaining("Error loading private key file") |
| 197 | + .hasCauseInstanceOf(IllegalStateException.class) |
| 198 | + .getCause() |
| 199 | + .hasMessageContaining("Unrecognized private key format"); |
80 | 200 | }
|
81 | 201 |
|
82 | 202 | @Test
|
|
0 commit comments