Skip to content

Commit 64d2a0f

Browse files
authored
Users can easily create a certificate chain for testing using SignedCertificateExtension (#6519)
Motivation: This is the first of a series of PRs included by #6516. Currently, our mTLS tests are not realistic in that: - Certificate chains aren't verified - We set TLS configurations to not verify the peer, which means in reality there's no point in using mTLS. For this, I propose that a `SignedCertificate` is introduced. This is a certificate which is signed by another certificate (another `SignedCertificate` or `SelfSignedCertificate`). A JUnit `Extension` has also been introduced so users can easily test this behavior. In the process, I found that `SelfSingedCertificateNameType` has a typo, and isn't adding much value. This enum has been removed. Modifications: - Introduced `SignedCertificate`, `SignedCertificateExtension` - `SelfSignedCertificate`, `SelfSignedCertificateExtension` inherits the newly introduced constructs - Default behavior has been modified so that all generated certificates can act as a ca. - `SelfSingedCertificateNameType` has been removed Result: - Users can easily create a certificate chain for testing using `SignedCertificateExtension` <!-- Visit this URL to learn more about how to write a pull request description: https://armeria.dev/community/developer-guide#how-to-write-pull-request-description -->
1 parent e40f4eb commit 64d2a0f

File tree

10 files changed

+800
-546
lines changed

10 files changed

+800
-546
lines changed
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2025 LY Corporation
3+
*
4+
* LY Corporation licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package com.linecorp.armeria.internal.common.util;
18+
19+
import java.security.PrivateKey;
20+
import java.security.cert.CertificateException;
21+
import java.security.cert.X509Certificate;
22+
import java.util.Date;
23+
import java.util.Random;
24+
25+
import org.bouncycastle.asn1.x500.X500Name;
26+
import org.bouncycastle.cert.jcajce.JcaX509CertificateHolder;
27+
28+
import com.linecorp.armeria.common.annotation.Nullable;
29+
30+
final class CertificateParams {
31+
32+
private final String fqdn;
33+
private final X500Name ownerName;
34+
private final Random random;
35+
private final int bits;
36+
private final Date notBefore;
37+
private final Date notAfter;
38+
private final String algorithm;
39+
40+
@Nullable
41+
private final X509Certificate issuerCert;
42+
@Nullable
43+
private final PrivateKey issuerPrivateKey;
44+
private final X500Name issuerName;
45+
46+
CertificateParams(String fqdn, Random random, int bits, Date notBefore, Date notAfter,
47+
String algorithm) {
48+
this.fqdn = fqdn;
49+
this.random = random;
50+
this.bits = bits;
51+
this.notBefore = notBefore;
52+
this.notAfter = notAfter;
53+
this.algorithm = algorithm;
54+
issuerCert = null;
55+
issuerPrivateKey = null;
56+
57+
ownerName = new X500Name("CN=" + fqdn);
58+
issuerName = ownerName;
59+
}
60+
61+
CertificateParams(String fqdn, Random random, int bits, Date notBefore, Date notAfter,
62+
SignedCertificate issuer)
63+
throws CertificateException {
64+
this.fqdn = fqdn;
65+
ownerName = new X500Name("CN=" + fqdn);
66+
67+
this.random = random;
68+
this.bits = bits;
69+
this.notBefore = notBefore;
70+
this.notAfter = notAfter;
71+
algorithm = issuer.key().getAlgorithm();
72+
issuerCert = issuer.cert();
73+
issuerPrivateKey = issuer.key();
74+
issuerName = new JcaX509CertificateHolder(issuerCert).getSubject();
75+
}
76+
77+
String fqdn() {
78+
return fqdn;
79+
}
80+
81+
Random random() {
82+
return random;
83+
}
84+
85+
int bits() {
86+
return bits;
87+
}
88+
89+
Date notBefore() {
90+
return notBefore;
91+
}
92+
93+
Date notAfter() {
94+
return notAfter;
95+
}
96+
97+
String algorithm() {
98+
return algorithm;
99+
}
100+
101+
@Nullable PrivateKey issuerPrivateKey() {
102+
return issuerPrivateKey;
103+
}
104+
105+
@Nullable X509Certificate issuerCert() {
106+
return issuerCert;
107+
}
108+
109+
X500Name issuerName() {
110+
return issuerName;
111+
}
112+
113+
X500Name ownerName() {
114+
return ownerName;
115+
}
116+
}

0 commit comments

Comments
 (0)