Rename KeyVerifier to CertificateMatcher

Rename `KeyVerifier` to `CertificateMatcher` and refactor some
of the internals. This commit also adds test helper classes to
help simplify some of the tests.

See gh-38173
This commit is contained in:
Phillip Webb
2023-10-30 17:16:06 -07:00
parent 1b61bc1f20
commit 5dc5c2a4bc
8 changed files with 345 additions and 207 deletions

View File

@@ -0,0 +1,113 @@
/*
* Copyright 2012-2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.ssl;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
import java.security.cert.Certificate;
import java.util.List;
import java.util.Objects;
/**
* Helper used to match certificates against a {@link PrivateKey}.
*
* @author Moritz Halbritter
* @author Phillip Webb
*/
class CertificateMatcher {
private static final byte[] DATA = new byte[256];
static {
for (int i = 0; i < DATA.length; i++) {
DATA[i] = (byte) i;
}
}
private final PrivateKey privateKey;
private final Signature signature;
private final byte[] generatedSignature;
CertificateMatcher(PrivateKey privateKey) {
this.privateKey = privateKey;
this.signature = createSignature(privateKey);
this.generatedSignature = sign(this.signature, privateKey);
}
private Signature createSignature(PrivateKey privateKey) {
try {
String algorithm = getSignatureAlgorithm(this.privateKey);
return (algorithm != null) ? Signature.getInstance(algorithm) : null;
}
catch (NoSuchAlgorithmException ex) {
return null;
}
}
private static String getSignatureAlgorithm(PrivateKey privateKey) {
// https://docs.oracle.com/en/java/javase/17/docs/specs/security/standard-names.html#signature-algorithms
// https://docs.oracle.com/en/java/javase/17/docs/specs/security/standard-names.html#keypairgenerator-algorithms
return switch (privateKey.getAlgorithm()) {
case "RSA" -> "SHA256withRSA";
case "DSA" -> "SHA256withDSA";
case "EC" -> "SHA256withECDSA";
case "EdDSA" -> "EdDSA";
default -> null;
};
}
boolean matchesAny(List<? extends Certificate> certificates) {
return (this.generatedSignature != null) && certificates.stream().anyMatch(this::matches);
}
boolean matches(Certificate certificate) {
return matches(certificate.getPublicKey());
}
private boolean matches(PublicKey publicKey) {
return (this.generatedSignature != null)
&& Objects.equals(this.privateKey.getAlgorithm(), publicKey.getAlgorithm()) && verify(publicKey);
}
private boolean verify(PublicKey publicKey) {
try {
this.signature.initVerify(publicKey);
this.signature.update(DATA);
return this.signature.verify(this.generatedSignature);
}
catch (InvalidKeyException | SignatureException ex) {
return false;
}
}
private static byte[] sign(Signature signature, PrivateKey privateKey) {
try {
signature.initSign(privateKey);
signature.update(DATA);
return signature.sign();
}
catch (InvalidKeyException | SignatureException ex) {
return null;
}
}
}

View File

@@ -1,104 +0,0 @@
/*
* Copyright 2012-2023 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.ssl;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
import java.security.SignatureException;
/**
* Performs checks on keys, e.g., if a public key and a private key belong together.
*
* @author Moritz Halbritter
*/
class KeyVerifier {
private static final byte[] DATA = "Just some piece of data which gets signed".getBytes(StandardCharsets.UTF_8);
/**
* Checks if the given private key belongs to the given public key.
* @param privateKey the private key
* @param publicKey the public key
* @return whether the keys belong together
*/
Result matches(PrivateKey privateKey, PublicKey publicKey) {
try {
if (!privateKey.getAlgorithm().equals(publicKey.getAlgorithm())) {
// Keys are of different type
return Result.NO;
}
String algorithm = getSignatureAlgorithm(privateKey.getAlgorithm());
if (algorithm == null) {
return Result.UNKNOWN;
}
byte[] signature = createSignature(privateKey, algorithm);
return verifySignature(publicKey, algorithm, signature);
}
catch (NoSuchAlgorithmException | InvalidKeyException | SignatureException ex) {
return Result.UNKNOWN;
}
}
private static byte[] createSignature(PrivateKey privateKey, String algorithm)
throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
Signature signer = Signature.getInstance(algorithm);
signer.initSign(privateKey);
signer.update(DATA);
return signer.sign();
}
private static Result verifySignature(PublicKey publicKey, String algorithm, byte[] signature)
throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
Signature verifier = Signature.getInstance(algorithm);
verifier.initVerify(publicKey);
verifier.update(DATA);
try {
if (verifier.verify(signature)) {
return Result.YES;
}
else {
return Result.NO;
}
}
catch (SignatureException ex) {
return Result.NO;
}
}
private static String getSignatureAlgorithm(String keyAlgorithm) {
// https://docs.oracle.com/en/java/javase/17/docs/specs/security/standard-names.html#signature-algorithms
// https://docs.oracle.com/en/java/javase/17/docs/specs/security/standard-names.html#keypairgenerator-algorithms
return switch (keyAlgorithm) {
case "RSA" -> "SHA256withRSA";
case "DSA" -> "SHA256withDSA";
case "EC" -> "SHA256withECDSA";
case "EdDSA" -> "EdDSA";
default -> null;
};
}
enum Result {
YES, NO, UNKNOWN
}
}

View File

@@ -18,7 +18,6 @@ package org.springframework.boot.autoconfigure.ssl;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.security.cert.X509Certificate;
import org.springframework.boot.autoconfigure.ssl.SslBundleProperties.Key;
import org.springframework.boot.ssl.SslBundle;
@@ -31,6 +30,7 @@ import org.springframework.boot.ssl.jks.JksSslStoreDetails;
import org.springframework.boot.ssl.pem.PemSslStore;
import org.springframework.boot.ssl.pem.PemSslStoreBundle;
import org.springframework.boot.ssl.pem.PemSslStoreDetails;
import org.springframework.util.Assert;
/**
* {@link SslBundle} backed by {@link JksSslBundleProperties} or
@@ -122,7 +122,9 @@ public final class PropertiesSslBundle implements SslBundle {
PemSslStoreDetails details = asStoreDetails(properties, alias);
PemSslStore pemSslStore = PemSslStore.load(details);
if (properties.isVerifyKeys()) {
verifyPemSslStoreKeys(pemSslStore);
CertificateMatcher certificateMatcher = new CertificateMatcher(pemSslStore.privateKey());
Assert.state(certificateMatcher.matchesAny(pemSslStore.certificates()),
"Private key matches none of the certificates in the chain");
}
return pemSslStore;
}
@@ -131,17 +133,6 @@ public final class PropertiesSslBundle implements SslBundle {
}
}
private static void verifyPemSslStoreKeys(PemSslStore pemSslStore) {
KeyVerifier keyVerifier = new KeyVerifier();
for (X509Certificate certificate : pemSslStore.certificates()) {
KeyVerifier.Result result = keyVerifier.matches(pemSslStore.privateKey(), certificate.getPublicKey());
if (result == KeyVerifier.Result.YES) {
return;
}
}
throw new IllegalStateException("Private key matches none of the certificates in the chain");
}
private static PemSslStoreDetails asStoreDetails(PemSslBundleProperties.Store properties, String alias) {
return new PemSslStoreDetails(properties.getType(), alias, null, properties.getCertificate(),
properties.getPrivateKey(), properties.getPrivateKeyPassword());