1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.ssl.util;
17
18 import io.netty.pkitesting.CertificateBuilder;
19 import io.netty.pkitesting.X509Bundle;
20
21 import java.security.SecureRandom;
22 import java.time.Instant;
23 import java.util.Date;
24
25 import static io.netty.handler.ssl.util.SelfSignedCertificate.newSelfSignedCertificate;
26
27 final class CertificateBuilderCertGenerator {
28 private CertificateBuilderCertGenerator() {
29 }
30
31 static boolean isAvailable() {
32 try {
33 new CertificateBuilder();
34 return true;
35 } catch (Throwable ignore) {
36 return false;
37 }
38 }
39
40 static void generate(SelfSignedCertificate.Builder config) throws Exception {
41 String fqdn = config.fqdn;
42 Date notBefore = config.notBefore;
43 Date notAfter = config.notAfter;
44 String algorithm = config.algorithm;
45 SecureRandom random = config.random;
46 int bits = config.bits;
47 CertificateBuilder builder = new CertificateBuilder();
48 builder.setIsCertificateAuthority(true);
49 if (fqdn.contains("=")) {
50 builder.subject(fqdn);
51 } else {
52 builder.subject("CN=" + fqdn);
53 }
54 builder.notBefore(Instant.ofEpochMilli(notBefore.getTime()));
55 builder.notAfter(Instant.ofEpochMilli(notAfter.getTime()));
56 if (random != null) {
57 builder.secureRandom(random);
58 }
59 if ("RSA".equals(algorithm)) {
60 CertificateBuilder.Algorithm alg;
61 switch (bits) {
62 case 2048: alg = CertificateBuilder.Algorithm.rsa2048; break;
63 case 3072: alg = CertificateBuilder.Algorithm.rsa3072; break;
64 case 4096: alg = CertificateBuilder.Algorithm.rsa4096; break;
65 case 8192: alg = CertificateBuilder.Algorithm.rsa8192; break;
66 default:
67 throw new IllegalArgumentException("Unsupported RSA bit-width: " + bits);
68 }
69 builder.algorithm(alg);
70 } else if ("EC".equals(algorithm)) {
71 if (bits == 256) {
72 builder.algorithm(CertificateBuilder.Algorithm.ecp256);
73 } else if (bits == 384) {
74 builder.algorithm(CertificateBuilder.Algorithm.ecp384);
75 } else {
76 throw new IllegalArgumentException("Unsupported EC-P bit-width: " + bits);
77 }
78 }
79 X509Bundle bundle = builder.buildSelfSigned();
80 config.paths = newSelfSignedCertificate(fqdn, bundle.getKeyPair().getPrivate(), bundle.getCertificate());
81 config.keypair = bundle.getKeyPair();
82 config.privateKey = bundle.getKeyPair().getPrivate();
83 }
84 }