public final class CertificateBuilder extends Object
CertificateBuilder produce X509Bundle instances, where the keys use the specified
algorithm, and the certificate have the specified data.
The builder can make self-signed bundles, or can make bundles that are signed by other bundles to build a verified certificate path.
The builder can also make certificate that are invalid in various ways, for testing purpose. The most typical application is to make a certificate that has already expired or is not yet valid.
See RFC 5280 for the details of X.509 certificate contents.
Here is an example where a leaf certificate is created and signed by a self-signed issuer certificate:
Instant now = Instant.now();
CertificateBuilder template = new CertificateBuilder()
.notBefore(now.minus(1, DAYS))
.notAfter(now.plus(1, DAYS));
X509Bundle issuer = template.copy()
.subject("CN=testca, OU=dept, O=your-org")
.setKeyUsage(true, KeyUsage.digitalSignature, KeyUsage.keyCertSign)
.setIsCertificateAuthority(true)
.buildSelfSigned();
X509Bundle leaf = template.copy()
.subject("CN=leaf, OU=dept, O=your-org")
.setKeyUsage(true, KeyUsage.digitalSignature)
.addExtendedKeyUsage(ExtendedKeyUsage.PKIX_KP_SERVER_AUTH)
.addSanDnsName("san-1.leaf.dept.your-org.com")
.buildIssuedBy(issuer);
| Modifier and Type | Class and Description |
|---|---|
static class |
CertificateBuilder.Algorithm
The
CertificateBuilder.Algorithm enum encapsulates both the key type, key generation parameters, and the signature
algorithm to use. |
static class |
CertificateBuilder.ExtendedKeyUsage
The extended key usage field specify what the certificate and key is allowed to be used for.
|
static class |
CertificateBuilder.KeyUsage
The key usage field specify what the certificate and key is allowed to be used for.
|
| Constructor and Description |
|---|
CertificateBuilder()
Create a new certificate builder with a default configuration.
|
| Modifier and Type | Method and Description |
|---|---|
CertificateBuilder |
addCrlDistributionPoint(URI uri)
Add a URI distribution point for a certificate revocation list.
|
CertificateBuilder |
addCrlDistributionPoint(URI uri,
X500Principal issuer)
Add a URI distribution point for a certificate revocation list.
|
CertificateBuilder |
addExtendedKeyUsage(CertificateBuilder.ExtendedKeyUsage keyUsage)
Add the given
CertificateBuilder.ExtendedKeyUsage to the list of extended key usages. |
CertificateBuilder |
addExtendedKeyUsage(String oid)
Add the given OID to the list of extended key usages.
|
CertificateBuilder |
addExtendedKeyUsageClientAuth()
Add client-authentication to the list of extended key usages.
|
CertificateBuilder |
addExtendedKeyUsageCodeSigning()
Add code signing to the list of extended key usages.
|
CertificateBuilder |
addExtendedKeyUsageEmailProtection()
Add email protection to the list of extended key usages.
|
CertificateBuilder |
addExtendedKeyUsageKerberosClientAuth()
Add Kerberos client authentication to the list of extended key usages.
|
CertificateBuilder |
addExtendedKeyUsageMicrosoftSmartcardLogin()
Add Microsoft smartcard login to the list of extended key usages.
|
CertificateBuilder |
addExtendedKeyUsageOcspSigning()
Add OCSP signing to the list of extended key usages.
|
CertificateBuilder |
addExtendedKeyUsageServerAuth()
Add server-authentication to the list of extended key usages.
|
CertificateBuilder |
addExtendedKeyUsageTimeStamping()
Add time-stamping to the list of extended key usages.
|
CertificateBuilder |
addExtensionAsciiString(String identifierOID,
boolean critical,
String value)
Add a custom DER-encoded ASN.1 IA5String (an ASCII string) extension to the certificate, with the given OID,
criticality, and string value.
|
CertificateBuilder |
addExtensionOctetString(String identifierOID,
boolean critical,
byte[] contents)
Add a custom extension to the certificate, with the given OID, criticality flag, and DER-encoded contents.
|
CertificateBuilder |
addExtensionUtf8String(String identifierOID,
boolean critical,
String value)
Add a custom DER-encoded ASN.1 UTF-8 string extension to the certificate, with the given OID, criticality,
and string value.
|
CertificateBuilder |
addSanDirectoryName(String dirName)
Add a Directory Name to the Subject Alternative Names.
|
CertificateBuilder |
addSanDnsName(String dns)
Add a DNS name to the Subject Alternate Names.
|
CertificateBuilder |
addSanIpAddress(InetAddress ipAddress)
Add an IP address to the Subject Alternative Names.
|
CertificateBuilder |
addSanIpAddress(String ipAddress)
Add an IP address to the Subject Alternative Names.
|
CertificateBuilder |
addSanOtherName(String typeOid,
byte[] encodedValue)
Add an Other Name to the Subject Alternative Names, of the given OID type, and with the given encoded value.
|
CertificateBuilder |
addSanRegisteredId(String oid)
Add a registeredID to the Subject Alternative Names.
|
CertificateBuilder |
addSanRfc822Name(String name)
Add an RFC 822 name to the Subject Alternative Names.
|
CertificateBuilder |
addSanUriName(String uri)
Add a URI name to the Subject Alternative Names.
|
CertificateBuilder |
addSanUriName(URI uri)
Add a URI name to the Subject Alternative Names.
|
CertificateBuilder |
algorithm(CertificateBuilder.Algorithm algorithm)
Set the key algorithm to use.
|
X509Bundle |
buildIssuedBy(X509Bundle issuerBundle)
Build a
X509Bundle with a certificate signed by the given issuer bundle. |
X509Bundle |
buildIssuedBy(X509Bundle issuerBundle,
String signAlg)
Build a
X509Bundle with a certificate signed by the given issuer bundle, using the specified
signing algorithm. |
X509Bundle |
buildSelfSigned()
Build a
X509Bundle with a self-signed certificate. |
CertificateBuilder |
copy()
Produce a copy of the current state in this certificate builder.
|
CertificateBuilder |
ecp256()
Make this certificate builder use the NIST EC-P 256 elliptic curve key algorithm.
|
CertificateBuilder |
notAfter(Instant instant)
Set the not-after field of the certificate.
|
CertificateBuilder |
notBefore(Instant instant)
Set the not-before field of the certificate.
|
CertificateBuilder |
publicKey(PublicKey key)
Instruct the certificate builder to not generate its own key pair, but to instead create a certificate that
uses the given public key.
|
CertificateBuilder |
rsa2048()
Make this certificate builder use the 2048-bit RSA encryption and signing
algorithm.
|
CertificateBuilder |
secureRandom(SecureRandom secureRandom)
Set the
SecureRandom instance to use when generating keys. |
CertificateBuilder |
serial(BigInteger serial)
Set the specific serial number to use in the certificate.
|
CertificateBuilder |
setIsCertificateAuthority(boolean isCA)
Set the certificate authority field.
|
CertificateBuilder |
setKeyUsage(boolean critical,
CertificateBuilder.KeyUsage... keyUsages)
The key usage specify the intended usages for which the certificate has been issued.
|
CertificateBuilder |
setPathLengthConstraint(OptionalInt pathLengthConstraint)
Certificate Authority certificates may impose a limit to the length of the verified certificate path they permit.
|
CertificateBuilder |
subject(String fqdn)
Set the fully-qualified domain name (an X.500 name) as the subject of the certificate.
|
CertificateBuilder |
subject(X500Principal name)
Set the subject name of the certificate to the given
X500Principal. |
public CertificateBuilder()
public CertificateBuilder copy()
public CertificateBuilder secureRandom(SecureRandom secureRandom)
SecureRandom instance to use when generating keys.secureRandom - The secure random instance to use.public CertificateBuilder notBefore(Instant instant)
instant - The not-before time.public CertificateBuilder notAfter(Instant instant)
instant - The not-after time.public CertificateBuilder serial(BigInteger serial)
serial - The serial number to use, or null.public CertificateBuilder subject(String fqdn)
fqdn - The subject name to use.public CertificateBuilder subject(X500Principal name)
X500Principal.name - The subject name to use.public CertificateBuilder addSanOtherName(String typeOid, byte[] encodedValue)
typeOid - The OID type of the Other Name value.encodedValue - The encoded Other Name value.public CertificateBuilder addSanRfc822Name(String name)
name - The email address to add to the SANs.public CertificateBuilder addSanDnsName(String dns)
dns - The DNS name to add.public CertificateBuilder addSanDirectoryName(String dirName)
dirName - The directory name to add to the SANs.public CertificateBuilder addSanUriName(String uri) throws URISyntaxException
uri - The URI to add to the SANs.URISyntaxExceptionpublic CertificateBuilder addSanUriName(URI uri)
uri - The URI to add to the SANs.public CertificateBuilder addSanIpAddress(String ipAddress)
ipAddress - The IP address to add to the SANs.public CertificateBuilder addSanIpAddress(InetAddress ipAddress)
ipAddress - The IP address to add to the SANs.public CertificateBuilder addSanRegisteredId(String oid)
oid - The OID to add to the SANs.public CertificateBuilder addCrlDistributionPoint(URI uri)
If you are testing certificate revocation using the RevocationServer,
you would obtain this URI from RevocationServer.getCrlUri(X509Bundle) with your intended issuer
certificate bundle.
uri - The URI for the CRL file.public CertificateBuilder addCrlDistributionPoint(URI uri, X500Principal issuer)
If you are testing certificate revocation using the RevocationServer,
you would obtain this URI from RevocationServer.getCrlUri(X509Bundle) with your intended issuer
certificate bundle.
uri - The URI for the CRL file.issuer - The issuer that signs the CRL file.
This MUST be null if the CRL issuer is also the issuer of the certificate being built.
Otherwise, if this certificate and the CRL will be signed by different issuers, then this MUST be the subject
name of the CRL signing certificate.public CertificateBuilder setIsCertificateAuthority(boolean isCA)
true, then this builder can build self-signed certificates, and those certifiactes
can be used to sign other certificates.isCA - true if this builder should make CA certificates.public CertificateBuilder setPathLengthConstraint(OptionalInt pathLengthConstraint)
pathLengthConstraint - The maximum verified path length, if any.public CertificateBuilder algorithm(CertificateBuilder.Algorithm algorithm)
algorithm - The algorithm to use when generating the private key.public CertificateBuilder ecp256()
ecp256()public CertificateBuilder rsa2048()
rsa2048()public CertificateBuilder publicKey(PublicKey key)
This method is useful if you want to use an existing key-pair, e.g. to emulate a certificate authority responding to a Certificate Signing Request (CSR).
If the given public key is null (the default) then a new key-pair will be generated instead.
key - The public key to wrap in a certificate.public CertificateBuilder addExtensionOctetString(String identifierOID, boolean critical, byte[] contents)
identifierOID - The OID identifying the extension.critical - true if the extension is critical, otherwise false.
Certificate systems MUST reject certificates with critical extensions they don't recognize.contents - The DER-encoded extension contents.public CertificateBuilder addExtensionUtf8String(String identifierOID, boolean critical, String value)
identifierOID - The OID identifying the extension.critical - true if the extension is critical, otherwise false.
Certificate systems MUST reject certificates with critical extensions they don't recognize.value - The string value.public CertificateBuilder addExtensionAsciiString(String identifierOID, boolean critical, String value)
identifierOID - The OID identifying the extension.critical - true if the extension is critical, otherwise false.
Certificate systems MUST reject certificates with critical extensions they don't recognize.value - The string value.public CertificateBuilder setKeyUsage(boolean critical, CertificateBuilder.KeyUsage... keyUsages)
For Certificate Authority usage, the important ones are CertificateBuilder.KeyUsage.keyCertSign
and CertificateBuilder.KeyUsage.cRLSign.
Any certificate that has CertificateBuilder.KeyUsage.keyCertSign must also have setIsCertificateAuthority(boolean)
set to true.
critical - true if certificate recipients are required to understand all the set bits,
otherwise false.keyUsages - The key usages to set.public CertificateBuilder addExtendedKeyUsage(String oid)
oid - The OID to add.CertificateBuilder.ExtendedKeyUsage,
addExtendedKeyUsage(ExtendedKeyUsage)public CertificateBuilder addExtendedKeyUsage(CertificateBuilder.ExtendedKeyUsage keyUsage)
CertificateBuilder.ExtendedKeyUsage to the list of extended key usages.keyUsage - The extended key usage to add.public CertificateBuilder addExtendedKeyUsageServerAuth()
public CertificateBuilder addExtendedKeyUsageClientAuth()
public CertificateBuilder addExtendedKeyUsageCodeSigning()
public CertificateBuilder addExtendedKeyUsageEmailProtection()
public CertificateBuilder addExtendedKeyUsageTimeStamping()
public CertificateBuilder addExtendedKeyUsageOcspSigning()
public CertificateBuilder addExtendedKeyUsageKerberosClientAuth()
public CertificateBuilder addExtendedKeyUsageMicrosoftSmartcardLogin()
public X509Bundle buildSelfSigned() throws Exception
X509Bundle with a self-signed certificate.Exception - If something went wrong in the process.public X509Bundle buildIssuedBy(X509Bundle issuerBundle) throws Exception
X509Bundle with a certificate signed by the given issuer bundle.
The signing algorithm used will be derived from the issuers public key.Exception - If something went wrong in the process.public X509Bundle buildIssuedBy(X509Bundle issuerBundle, String signAlg) throws Exception
X509Bundle with a certificate signed by the given issuer bundle, using the specified
signing algorithm.Exception - If something went wrong in the process.Copyright © 2008–2024 The Netty Project. All rights reserved.