|
|
|
|
@@ -28,7 +28,6 @@ import org.junit.jupiter.params.provider.CsvSource;
|
|
|
|
|
import org.junit.jupiter.params.provider.ValueSource;
|
|
|
|
|
|
|
|
|
|
import org.springframework.core.io.ClassPathResource;
|
|
|
|
|
import org.springframework.util.ObjectUtils;
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
|
|
|
|
@@ -51,7 +50,7 @@ class PemPrivateKeyParserTests {
|
|
|
|
|
})
|
|
|
|
|
// @formatter:on
|
|
|
|
|
void shouldParseTraditionalPkcs8(String file, String algorithm) throws IOException {
|
|
|
|
|
PrivateKey privateKey = parse(read("org/springframework/boot/web/server/pkcs8/" + file));
|
|
|
|
|
PrivateKey privateKey = PemPrivateKeyParser.parse(read("org/springframework/boot/web/server/pkcs8/" + file));
|
|
|
|
|
assertThat(privateKey).isNotNull();
|
|
|
|
|
assertThat(privateKey.getFormat()).isEqualTo("PKCS#8");
|
|
|
|
|
assertThat(privateKey.getAlgorithm()).isEqualTo(algorithm);
|
|
|
|
|
@@ -64,7 +63,7 @@ class PemPrivateKeyParserTests {
|
|
|
|
|
})
|
|
|
|
|
// @formatter:on
|
|
|
|
|
void shouldParseTraditionalPkcs1(String file, String algorithm) throws IOException {
|
|
|
|
|
PrivateKey privateKey = parse(read("org/springframework/boot/web/server/pkcs1/" + file));
|
|
|
|
|
PrivateKey privateKey = PemPrivateKeyParser.parse(read("org/springframework/boot/web/server/pkcs1/" + file));
|
|
|
|
|
assertThat(privateKey).isNotNull();
|
|
|
|
|
assertThat(privateKey.getFormat()).isEqualTo("PKCS#8");
|
|
|
|
|
assertThat(privateKey.getAlgorithm()).isEqualTo(algorithm);
|
|
|
|
|
@@ -78,11 +77,11 @@ class PemPrivateKeyParserTests {
|
|
|
|
|
// @formatter:on
|
|
|
|
|
void shouldNotParseUnsupportedTraditionalPkcs1(String file) {
|
|
|
|
|
assertThatIllegalStateException()
|
|
|
|
|
.isThrownBy(() -> parse(read("org/springframework/boot/web/server/pkcs1/" + file)))
|
|
|
|
|
.isThrownBy(() -> PemPrivateKeyParser.parse(read("org/springframework/boot/web/server/pkcs1/" + file)))
|
|
|
|
|
.withMessageContaining("Error loading private key file")
|
|
|
|
|
.withCauseInstanceOf(IllegalStateException.class)
|
|
|
|
|
.havingCause()
|
|
|
|
|
.withMessageContaining("Unsupported private key format");
|
|
|
|
|
.withMessageContaining("Unrecognized private key format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
|
@@ -101,7 +100,7 @@ class PemPrivateKeyParserTests {
|
|
|
|
|
})
|
|
|
|
|
// @formatter:on
|
|
|
|
|
void shouldParseEcPkcs8(String file, String curveName, String oid) throws IOException {
|
|
|
|
|
PrivateKey privateKey = parse(read("org/springframework/boot/web/server/pkcs8/" + file));
|
|
|
|
|
PrivateKey privateKey = PemPrivateKeyParser.parse(read("org/springframework/boot/web/server/pkcs8/" + file));
|
|
|
|
|
assertThat(privateKey).isNotNull();
|
|
|
|
|
assertThat(privateKey.getFormat()).isEqualTo("PKCS#8");
|
|
|
|
|
assertThat(privateKey.getAlgorithm()).isEqualTo("EC");
|
|
|
|
|
@@ -136,7 +135,7 @@ class PemPrivateKeyParserTests {
|
|
|
|
|
})
|
|
|
|
|
// @formatter:on
|
|
|
|
|
void shouldParseEdDsaPkcs8(String file) throws IOException {
|
|
|
|
|
PrivateKey privateKey = parse(read("org/springframework/boot/web/server/pkcs8/" + file));
|
|
|
|
|
PrivateKey privateKey = PemPrivateKeyParser.parse(read("org/springframework/boot/web/server/pkcs8/" + file));
|
|
|
|
|
assertThat(privateKey).isNotNull();
|
|
|
|
|
assertThat(privateKey.getFormat()).isEqualTo("PKCS#8");
|
|
|
|
|
assertThat(privateKey.getAlgorithm()).isEqualTo("EdDSA");
|
|
|
|
|
@@ -150,7 +149,7 @@ class PemPrivateKeyParserTests {
|
|
|
|
|
})
|
|
|
|
|
// @formatter:on
|
|
|
|
|
void shouldParseXdhPkcs8(String file) throws IOException {
|
|
|
|
|
PrivateKey privateKey = parse(read("org/springframework/boot/web/server/pkcs8/" + file));
|
|
|
|
|
PrivateKey privateKey = PemPrivateKeyParser.parse(read("org/springframework/boot/web/server/pkcs8/" + file));
|
|
|
|
|
assertThat(privateKey).isNotNull();
|
|
|
|
|
assertThat(privateKey.getFormat()).isEqualTo("PKCS#8");
|
|
|
|
|
assertThat(privateKey.getAlgorithm()).isEqualTo("XDH");
|
|
|
|
|
@@ -172,7 +171,7 @@ class PemPrivateKeyParserTests {
|
|
|
|
|
})
|
|
|
|
|
// @formatter:on
|
|
|
|
|
void shouldParseEcSec1(String file, String curveName, String oid) throws IOException {
|
|
|
|
|
PrivateKey privateKey = parse(read("org/springframework/boot/web/server/sec1/" + file));
|
|
|
|
|
PrivateKey privateKey = PemPrivateKeyParser.parse(read("org/springframework/boot/web/server/sec1/" + file));
|
|
|
|
|
assertThat(privateKey).isNotNull();
|
|
|
|
|
assertThat(privateKey.getFormat()).isEqualTo("PKCS#8");
|
|
|
|
|
assertThat(privateKey.getAlgorithm()).isEqualTo("EC");
|
|
|
|
|
@@ -200,8 +199,8 @@ class PemPrivateKeyParserTests {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void parseWithNonKeyTextWillReturnEmptyArray() throws Exception {
|
|
|
|
|
assertThat(PemPrivateKeyParser.parse(read("test-banner.txt"))).isEmpty();
|
|
|
|
|
void parseWithNonKeyTextWillThrowException() {
|
|
|
|
|
assertThatIllegalStateException().isThrownBy(() -> PemPrivateKeyParser.parse(read("test-banner.txt")));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@ParameterizedTest
|
|
|
|
|
@@ -219,10 +218,16 @@ class PemPrivateKeyParserTests {
|
|
|
|
|
// openssl pkcs8 -topk8 -in <input file> -out <output file> -v2 <algorithm>
|
|
|
|
|
// -passout pass:test
|
|
|
|
|
// where <algorithm> is aes128 or aes256
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
String content = read("org/springframework/boot/web/server/pkcs8/" + file);
|
|
|
|
|
List<PrivateKey> privateKeys = PemPrivateKeyParser.parse(content, "test");
|
|
|
|
|
assertThat(privateKeys).isNotEmpty();
|
|
|
|
|
PrivateKey privateKey = privateKeys.get(0);
|
|
|
|
|
=======
|
|
|
|
|
PrivateKey privateKey = PemPrivateKeyParser.parse(read("org/springframework/boot/web/server/pkcs8/" + file),
|
|
|
|
|
"test");
|
|
|
|
|
assertThat(privateKey).isNotNull();
|
|
|
|
|
>>>>>>> parent of 32e6ce210e1 (Allow PemPrivateKeyParser to parse multiple keys)
|
|
|
|
|
assertThat(privateKey.getFormat()).isEqualTo("PKCS#8");
|
|
|
|
|
assertThat(privateKey.getAlgorithm()).isEqualTo(algorithm);
|
|
|
|
|
}
|
|
|
|
|
@@ -251,18 +256,21 @@ class PemPrivateKeyParserTests {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void shouldNotParseEncryptedSec1() throws Exception {
|
|
|
|
|
void shouldNotParseEncryptedSec1() {
|
|
|
|
|
// created with:
|
|
|
|
|
// openssl ecparam -genkey -name prime256v1 | openssl ec -aes-128-cbc -out
|
|
|
|
|
// prime256v1-aes-128-cbc.key
|
|
|
|
|
assertThat(PemPrivateKeyParser
|
|
|
|
|
.parse(read("org/springframework/boot/web/server/sec1/prime256v1-aes-128-cbc.key"), "test")).isEmpty();
|
|
|
|
|
assertThatIllegalStateException()
|
|
|
|
|
.isThrownBy(() -> PemPrivateKeyParser
|
|
|
|
|
.parse(read("org/springframework/boot/web/server/sec1/prime256v1-aes-128-cbc.key"), "test"))
|
|
|
|
|
.withMessageContaining("Unrecognized private key format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
void shouldNotParseEncryptedPkcs1() throws Exception {
|
|
|
|
|
// created with:
|
|
|
|
|
// openssl genrsa -aes-256-cbc -out rsa-aes-256-cbc.key
|
|
|
|
|
<<<<<<< HEAD
|
|
|
|
|
assertThat(PemPrivateKeyParser.parse(read("org/springframework/boot/web/server/pkcs1/rsa-aes-256-cbc.key"),
|
|
|
|
|
"test"))
|
|
|
|
|
.isEmpty();
|
|
|
|
|
@@ -271,6 +279,12 @@ class PemPrivateKeyParserTests {
|
|
|
|
|
private PrivateKey parse(String key) {
|
|
|
|
|
List<PrivateKey> keys = PemPrivateKeyParser.parse(key);
|
|
|
|
|
return (!ObjectUtils.isEmpty(keys)) ? keys.get(0) : null;
|
|
|
|
|
=======
|
|
|
|
|
assertThatIllegalStateException()
|
|
|
|
|
.isThrownBy(() -> PemPrivateKeyParser
|
|
|
|
|
.parse(read("org/springframework/boot/web/server/pkcs1/rsa-aes-256-cbc.key"), "test"))
|
|
|
|
|
.withMessageContaining("Unrecognized private key format");
|
|
|
|
|
>>>>>>> parent of 32e6ce210e1 (Allow PemPrivateKeyParser to parse multiple keys)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String read(String path) throws IOException {
|
|
|
|
|
|