Polish SSL

This commit is contained in:
Moritz Halbritter
2023-11-02 08:51:42 +01:00
parent a35fb7505f
commit d3f177be71
9 changed files with 12 additions and 21 deletions

View File

@@ -26,6 +26,8 @@ import java.security.cert.Certificate;
import java.util.List;
import java.util.Objects;
import org.springframework.util.Assert;
/**
* Helper used to match certificates against a {@link PrivateKey}.
*
@@ -48,14 +50,16 @@ class CertificateMatcher {
private final byte[] generatedSignature;
CertificateMatcher(PrivateKey privateKey) {
Assert.notNull(privateKey, "Private key must not be null");
this.privateKey = privateKey;
this.signature = createSignature(privateKey);
Assert.notNull(this.signature, "Failed to create signature");
this.generatedSignature = sign(this.signature, privateKey);
}
private Signature createSignature(PrivateKey privateKey) {
try {
String algorithm = getSignatureAlgorithm(this.privateKey);
String algorithm = getSignatureAlgorithm(privateKey);
return (algorithm != null) ? Signature.getInstance(algorithm) : null;
}
catch (NoSuchAlgorithmException ex) {

View File

@@ -120,7 +120,7 @@ public final class PropertiesSslBundle implements SslBundle {
if (properties.isVerifyKeys()) {
CertificateMatcher certificateMatcher = new CertificateMatcher(pemSslStore.privateKey());
Assert.state(certificateMatcher.matchesAny(pemSslStore.certificates()),
"Private key matches none of the certificates in the chain");
"Private key in %s matches none of the certificates in the chain".formatted(propertyName));
}
return pemSslStore;
}

View File

@@ -134,7 +134,7 @@ class PropertiesSslBundleTests {
properties.getKeystore().setVerifyKeys(true);
properties.getKey().setAlias("test-alias");
assertThatIllegalStateException().isThrownBy(() -> PropertiesSslBundle.get(properties))
.withMessageContaining("Private key matches none of the certificates");
.withMessageContaining("Private key in keystore matches none of the certificates");
}
private Consumer<KeyStore> storeContainingCertAndKey(String keyAlias) {