Skip to content

Commit d3f177b

Browse files
committed
Polish SSL
1 parent a35fb75 commit d3f177b

File tree

9 files changed

+12
-21
lines changed

9 files changed

+12
-21
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ssl/CertificateMatcher.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.util.List;
2727
import java.util.Objects;
2828

29+
import org.springframework.util.Assert;
30+
2931
/**
3032
* Helper used to match certificates against a {@link PrivateKey}.
3133
*
@@ -48,14 +50,16 @@ class CertificateMatcher {
4850
private final byte[] generatedSignature;
4951

5052
CertificateMatcher(PrivateKey privateKey) {
53+
Assert.notNull(privateKey, "Private key must not be null");
5154
this.privateKey = privateKey;
5255
this.signature = createSignature(privateKey);
56+
Assert.notNull(this.signature, "Failed to create signature");
5357
this.generatedSignature = sign(this.signature, privateKey);
5458
}
5559

5660
private Signature createSignature(PrivateKey privateKey) {
5761
try {
58-
String algorithm = getSignatureAlgorithm(this.privateKey);
62+
String algorithm = getSignatureAlgorithm(privateKey);
5963
return (algorithm != null) ? Signature.getInstance(algorithm) : null;
6064
}
6165
catch (NoSuchAlgorithmException ex) {

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/ssl/PropertiesSslBundle.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ private static PemSslStore getPemSslStore(String propertyName, PemSslBundlePrope
120120
if (properties.isVerifyKeys()) {
121121
CertificateMatcher certificateMatcher = new CertificateMatcher(pemSslStore.privateKey());
122122
Assert.state(certificateMatcher.matchesAny(pemSslStore.certificates()),
123-
"Private key matches none of the certificates in the chain");
123+
"Private key in %s matches none of the certificates in the chain".formatted(propertyName));
124124
}
125125
return pemSslStore;
126126
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/ssl/PropertiesSslBundleTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ void getWithPemSslBundlePropertiesWhenVerifyKeyStoreWithNoMatchThrowsException()
134134
properties.getKeystore().setVerifyKeys(true);
135135
properties.getKey().setAlias("test-alias");
136136
assertThatIllegalStateException().isThrownBy(() -> PropertiesSslBundle.get(properties))
137-
.withMessageContaining("Private key matches none of the certificates");
137+
.withMessageContaining("Private key in keystore matches none of the certificates");
138138
}
139139

140140
private Consumer<KeyStore> storeContainingCertAndKey(String keyAlias) {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/pem/PemContent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public final class PemContent {
4848

4949
private static final Pattern PEM_FOOTER = Pattern.compile("-+END\\s+[^-]*-+", Pattern.CASE_INSENSITIVE);
5050

51-
private String text;
51+
private final String text;
5252

5353
private PemContent(String text) {
5454
this.text = text;

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/pem/PemPrivateKeyParser.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ private static int[] getEcParameters(DerElement parameters) {
130130
}
131131
Assert.state(parameters.isType(ValueType.ENCODED), "Key spec should contain encoded parameters");
132132
DerElement contents = DerElement.of(parameters.getContents());
133-
Assert.state(contents.isType(ValueType.PRIMITIVE, TagType.OBJECT_IDENTIFIER),
133+
Assert.state(contents != null && contents.isType(ValueType.PRIMITIVE, TagType.OBJECT_IDENTIFIER),
134134
"Key spec parameters should contain object identifier");
135135
return getEcParameters(contents.getContents());
136136
}
@@ -237,6 +237,7 @@ private PrivateKey parse(byte[] bytes, String password) {
237237
return keyFactory.generatePrivate(keySpec);
238238
}
239239
catch (InvalidKeySpecException | NoSuchAlgorithmException ex) {
240+
// Ignore
240241
}
241242
}
242243
return null;
@@ -264,10 +265,6 @@ void octetString(byte[] bytes) throws IOException {
264265
codeLengthBytes(0x04, bytes);
265266
}
266267

267-
void sequence(int... elements) throws IOException {
268-
sequence(bytes(elements));
269-
}
270-
271268
void sequence(byte[] bytes) throws IOException {
272269
codeLengthBytes(0x30, bytes);
273270
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/pem/PemSslStore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public interface PemSslStore {
4848
String alias();
4949

5050
/**
51-
* the password used
51+
* The password used when
5252
* {@link KeyStore#setKeyEntry(String, java.security.Key, char[], java.security.cert.Certificate[])
5353
* setting key entries} in the {@link KeyStore}.
5454
* @return the password

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/pem/PemSslStoreBundle.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ public class PemSslStoreBundle implements SslStoreBundle {
5151
* @param keyStoreDetails the key store details
5252
* @param trustStoreDetails the trust store details
5353
*/
54-
@SuppressWarnings("removal")
5554
public PemSslStoreBundle(PemSslStoreDetails keyStoreDetails, PemSslStoreDetails trustStoreDetails) {
5655
this(keyStoreDetails, trustStoreDetails, null);
5756
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/pem/PemSslStoreDetails.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public record PemSslStoreDetails(String type, String alias, String password, Str
7373
* @param privateKeyPassword a password used to decrypt an encrypted private key
7474
*/
7575
public PemSslStoreDetails(String type, String certificate, String privateKey, String privateKeyPassword) {
76-
this(type, null, null, certificate, privateKey, null);
76+
this(type, null, null, certificate, privateKey, privateKeyPassword);
7777
}
7878

7979
/**

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/ssl/pem/PemContentTests.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,4 @@ void ofReturnsContent() {
154154
assertThat(PemContent.of("test")).hasToString("test");
155155
}
156156

157-
@Test
158-
void hashCodeAndEquals() {
159-
PemContent a = PemContent.of("1");
160-
PemContent b = PemContent.of("1");
161-
PemContent c = PemContent.of("2");
162-
assertThat(a.hashCode()).isEqualTo(b.hashCode());
163-
assertThat(a).isEqualTo(a).isEqualTo(b).isNotEqualTo(c);
164-
}
165-
166157
}

0 commit comments

Comments
 (0)