-
Notifications
You must be signed in to change notification settings - Fork 40.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add property to enable key verification on PEM SSL bundles
Closes gh-37727
- Loading branch information
1 parent
85aeede
commit 0a16ec1
Showing
13 changed files
with
494 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
...-boot-project/spring-boot/src/main/java/org/springframework/boot/ssl/pem/KeyVerifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/* | ||
* 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.ssl.pem; | ||
|
||
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 | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
...-project/spring-boot/src/test/java/org/springframework/boot/ssl/pem/KeyVerifierTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* 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.ssl.pem; | ||
|
||
import java.security.InvalidAlgorithmParameterException; | ||
import java.security.KeyPair; | ||
import java.security.KeyPairGenerator; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.security.PrivateKey; | ||
import java.security.PublicKey; | ||
import java.security.spec.AlgorithmParameterSpec; | ||
import java.security.spec.ECGenParameterSpec; | ||
import java.util.LinkedList; | ||
import java.util.List; | ||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.Named; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import org.springframework.boot.ssl.pem.KeyVerifier.Result; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Tests for {@link KeyVerifier}. | ||
* | ||
* @author Moritz Halbritter | ||
*/ | ||
class KeyVerifierTests { | ||
|
||
private static final List<Algorithm> ALGORITHMS = List.of(Algorithm.of("RSA"), Algorithm.of("DSA"), | ||
Algorithm.of("ed25519"), Algorithm.of("ed448"), Algorithm.ec("secp256r1"), Algorithm.ec("secp521r1")); | ||
|
||
private final KeyVerifier keyVerifier = new KeyVerifier(); | ||
|
||
@ParameterizedTest(name = "{0}") | ||
@MethodSource("arguments") | ||
void test(PrivateKey privateKey, PublicKey publicKey, List<PublicKey> invalidPublicKeys) { | ||
assertThat(this.keyVerifier.matches(privateKey, publicKey)).isEqualTo(Result.YES); | ||
for (PublicKey invalidPublicKey : invalidPublicKeys) { | ||
assertThat(this.keyVerifier.matches(privateKey, invalidPublicKey)).isEqualTo(Result.NO); | ||
} | ||
} | ||
|
||
static Stream<Arguments> arguments() throws NoSuchAlgorithmException, InvalidAlgorithmParameterException { | ||
List<KeyPair> keyPairs = new LinkedList<>(); | ||
for (Algorithm algorithm : ALGORITHMS) { | ||
KeyPairGenerator generator = KeyPairGenerator.getInstance(algorithm.name()); | ||
if (algorithm.spec() != null) { | ||
generator.initialize(algorithm.spec()); | ||
} | ||
keyPairs.add(generator.generateKeyPair()); | ||
keyPairs.add(generator.generateKeyPair()); | ||
} | ||
return keyPairs.stream() | ||
.map((kp) -> Arguments.arguments(Named.named(kp.getPrivate().getAlgorithm(), kp.getPrivate()), | ||
kp.getPublic(), without(keyPairs, kp).map(KeyPair::getPublic).toList())); | ||
} | ||
|
||
private static Stream<KeyPair> without(List<KeyPair> keyPairs, KeyPair without) { | ||
return keyPairs.stream().filter((kp) -> !kp.equals(without)); | ||
} | ||
|
||
private record Algorithm(String name, AlgorithmParameterSpec spec) { | ||
static Algorithm of(String name) { | ||
return new Algorithm(name, null); | ||
} | ||
|
||
static Algorithm ec(String curve) { | ||
return new Algorithm("EC", new ECGenParameterSpec(curve)); | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.