Merge branch '2.6.x' into 2.7.x

Closes gh-32681
This commit is contained in:
Phillip Webb
2022-10-11 23:19:34 -07:00
5 changed files with 232 additions and 80 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -64,10 +64,10 @@ class KeyStoreFactoryTests {
}
@Test
void createKeyStoreWithCertChainAndPrivateKey()
void createKeyStoreWithCertChainAndRsaPrivateKey()
throws IOException, KeyStoreException, UnrecoverableKeyException, NoSuchAlgorithmException {
Path certPath = this.fileWriter.writeFile("cert.pem", PemFileWriter.CA_CERTIFICATE, PemFileWriter.CERTIFICATE);
Path keyPath = this.fileWriter.writeFile("key.pem", PemFileWriter.PRIVATE_KEY);
Path keyPath = this.fileWriter.writeFile("key.pem", PemFileWriter.PRIVATE_RSA_KEY);
KeyStore keyStore = KeyStoreFactory.create(certPath, keyPath, "test-alias");
assertThat(keyStore.containsAlias("test-alias")).isTrue();
assertThat(keyStore.getCertificate("test-alias")).isNotNull();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -81,7 +81,7 @@ public class PemFileWriter {
+ "a/OsPdOw0j+NqFDBd3mSMhSVgfvXdK6j9WaxY1VGXyaidLARgvn63wfzgr857sQW\n"
+ "c8eSxbwEQxwlMvVxW6Os4VhCfUQr8VrBrvPa2zs+6IlK+Ug=\n" + "-----END CERTIFICATE-----\n";
public static final String PRIVATE_KEY = EXAMPLE_SECRET_QUALIFIER + "-----BEGIN RSA PRIVATE KEY-----\n"
public static final String PRIVATE_RSA_KEY = EXAMPLE_SECRET_QUALIFIER + "-----BEGIN RSA PRIVATE KEY-----\n"
+ "MIICXAIBAAKBgQDO5HdnIxePcJtfjkO0ORhPenF7nljT6/zcHou9bX+huAZhNAfr\n"
+ "LlxxL5y6R04/aXV/2cwSCvIyGf8fEZ0nbivysvs5tUDGFFmbVggIAlvFs4AevQp/\n"
+ "4UBvCkl90vW6jg7kALz+eBkCQ+/uJFbjQRoxW+A2g+J7zJj7QC0mX/GQmQIDAQAB\n"
@@ -96,6 +96,11 @@ public class PemFileWriter {
+ "8bbHWILSIhFJHg0V7skCQDa8/YkRWF/3pwIZNWQr4ce4OzvYsFMkRvGRdX8B2a0p\n"
+ "wSKcVTdEdO2DhBlYddN0zG0rjq4vDMtdmldEl4BdldQ=\n" + "-----END RSA PRIVATE KEY-----\n";
public static final String PRIVATE_EC_KEY = EXAMPLE_SECRET_QUALIFIER + "-----BEGIN EC PRIVATE KEY-----\n"
+ "MHcCAQEEIIwZkO8Zjbggzi8wwrk5rzSPzUX31gqTRhBYw4AL6w44oAoGCCqGSM49\n"
+ "AwEHoUQDQgAE8y28khug747bA68M90IAMCPHAYyen+RsN6i84LORpNDUhv00QZWd\n"
+ "hOhjWFCQjnewR98Y8pEb1fnORll4LhHPlQ==\n" + "-----END EC PRIVATE KEY-----";
private final Path tempDir;
public PemFileWriter() throws IOException {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,8 +26,11 @@ import java.security.PrivateKey;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.boot.buildpack.platform.docker.ssl.PrivateKeyParser.DerEncoder;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
@@ -35,6 +38,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
* Tests for {@link PrivateKeyParser}.
*
* @author Scott Frederick
* @author Phillip Webb
*/
class PrivateKeyParserTests {
@@ -60,8 +64,18 @@ class PrivateKeyParserTests {
}
@Test
void parsePkcs1KeyFile() throws IOException {
Path path = this.fileWriter.writeFile("key.pem", PemFileWriter.PRIVATE_KEY);
void parsePkcs1RsaKeyFile() throws IOException {
Path path = this.fileWriter.writeFile("key.pem", PemFileWriter.PRIVATE_RSA_KEY);
PrivateKey privateKey = PrivateKeyParser.parse(path);
assertThat(privateKey).isNotNull();
// keys in PKCS#1 format are converted to PKCS#8 for parsing
assertThat(privateKey.getFormat()).isEqualTo("PKCS#8");
Files.delete(path);
}
@Test
void parsePkcs1EcKeyFile() throws IOException {
Path path = this.fileWriter.writeFile("key.pem", PemFileWriter.PRIVATE_EC_KEY);
PrivateKey privateKey = PrivateKeyParser.parse(path);
assertThat(privateKey).isNotNull();
// keys in PKCS#1 format are converted to PKCS#8 for parsing
@@ -84,4 +98,30 @@ class PrivateKeyParserTests {
.withMessageContaining(path.toString());
}
@Nested
class DerEncoderTests {
@Test
void codeLengthBytesShort() throws Exception {
DerEncoder encoder = new DerEncoder();
encoder.codeLengthBytes(0, new byte[127]);
assertThat(encoder.toByteArray()).startsWith(0x0, 0x7F);
}
@Test
void codeLengthBytesMedium() throws Exception {
DerEncoder encoder = new DerEncoder();
encoder.codeLengthBytes(0, new byte[130]);
assertThat(encoder.toByteArray()).startsWith(0x0, 0x81, 0x82);
}
@Test
void codeLengthBytesLong() throws Exception {
DerEncoder encoder = new DerEncoder();
encoder.codeLengthBytes(0, new byte[258]);
assertThat(encoder.toByteArray()).startsWith(0x0, 0x82, 0x01, 0x02);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -48,7 +48,7 @@ class SslContextFactoryTests {
@Test
void createKeyStoreWithCertChain() throws IOException {
this.fileWriter.writeFile("cert.pem", PemFileWriter.CERTIFICATE);
this.fileWriter.writeFile("key.pem", PemFileWriter.PRIVATE_KEY);
this.fileWriter.writeFile("key.pem", PemFileWriter.PRIVATE_RSA_KEY);
this.fileWriter.writeFile("ca.pem", PemFileWriter.CA_CERTIFICATE);
SSLContext sslContext = new SslContextFactory().forDirectory(this.fileWriter.getTempDir().toString());
assertThat(sslContext).isNotNull();