|
8 | 8 | import static org.assertj.core.api.Assertions.assertThatCode;
|
9 | 9 |
|
10 | 10 | import com.linecorp.armeria.internal.common.util.SelfSignedCertificate;
|
| 11 | +import java.io.File; |
| 12 | +import java.io.IOException; |
| 13 | +import java.nio.charset.StandardCharsets; |
| 14 | +import java.nio.file.Files; |
| 15 | +import java.nio.file.Path; |
| 16 | +import java.nio.file.StandardOpenOption; |
11 | 17 | import java.security.KeyFactory;
|
12 | 18 | import java.security.cert.CertificateException;
|
13 | 19 | import java.security.spec.PKCS8EncodedKeySpec;
|
14 | 20 | import java.time.Instant;
|
15 | 21 | import java.util.Collections;
|
16 | 22 | import java.util.Date;
|
| 23 | +import java.util.stream.Stream; |
17 | 24 | import javax.net.ssl.SSLException;
|
18 | 25 | import org.junit.jupiter.api.BeforeEach;
|
19 | 26 | import org.junit.jupiter.api.Test;
|
| 27 | +import org.junit.jupiter.api.io.TempDir; |
| 28 | +import org.junit.jupiter.params.ParameterizedTest; |
| 29 | +import org.junit.jupiter.params.provider.Arguments; |
| 30 | +import org.junit.jupiter.params.provider.MethodSource; |
20 | 31 |
|
21 | 32 | class TlsUtilTest {
|
22 | 33 |
|
| 34 | + @TempDir private Path tempDir; |
| 35 | + |
| 36 | + private static final String EXPLANATORY_TEXT = |
| 37 | + "Subject: CN=Foo\n" |
| 38 | + + "Issuer: CN=Foo\n" |
| 39 | + + "Validity: from 7/9/2012 3:10:38 AM UTC to 7/9/2013 3:10:37 AM UTC\n"; |
| 40 | + |
23 | 41 | private SelfSignedCertificate rsaCertificate;
|
24 | 42 | private SelfSignedCertificate ecCertificate;
|
25 | 43 |
|
@@ -60,4 +78,36 @@ void generatePrivateKey_Invalid() {
|
60 | 78 | .isInstanceOf(SSLException.class)
|
61 | 79 | .hasMessage("Unable to generate key from supported algorithms: [EC]");
|
62 | 80 | }
|
| 81 | + |
| 82 | + /** |
| 83 | + * Append <a href="https://datatracker.ietf.org/doc/html/rfc7468#section-5.2">explanatory text</a> |
| 84 | + * prefix and verify {@link TlsUtil#keyManager(byte[], byte[])} succeeds. |
| 85 | + */ |
| 86 | + @ParameterizedTest |
| 87 | + @MethodSource("keyManagerArgs") |
| 88 | + void keyManager_CertWithExplanatoryText(SelfSignedCertificate selfSignedCertificate) |
| 89 | + throws IOException { |
| 90 | + Path certificate = tempDir.resolve("certificate"); |
| 91 | + Files.write(certificate, EXPLANATORY_TEXT.getBytes(StandardCharsets.UTF_8)); |
| 92 | + Files.write( |
| 93 | + certificate, |
| 94 | + com.google.common.io.Files.toByteArray(selfSignedCertificate.certificate()), |
| 95 | + StandardOpenOption.APPEND); |
| 96 | + Files.write(certificate, "\n".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND); |
| 97 | + |
| 98 | + assertThatCode( |
| 99 | + () -> |
| 100 | + TlsUtil.keyManager( |
| 101 | + com.google.common.io.Files.toByteArray(selfSignedCertificate.privateKey()), |
| 102 | + com.google.common.io.Files.toByteArray(new File(certificate.toString())))) |
| 103 | + .doesNotThrowAnyException(); |
| 104 | + } |
| 105 | + |
| 106 | + private static Stream<Arguments> keyManagerArgs() throws CertificateException { |
| 107 | + Instant now = Instant.now(); |
| 108 | + return Stream.of( |
| 109 | + Arguments.of( |
| 110 | + new SelfSignedCertificate(Date.from(now), Date.from(now), "RSA", 2048), |
| 111 | + new SelfSignedCertificate(Date.from(now), Date.from(now), "EC", 256))); |
| 112 | + } |
63 | 113 | }
|
0 commit comments