Class CertificateBuilder
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);
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumTheCertificateBuilder.Algorithmenum encapsulates both the key type, key generation parameters, and the signature algorithm to use.static enumThe extended key usage field specify what the certificate and key is allowed to be used for.static enumThe key usage field specify what the certificate and key is allowed to be used for. -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new certificate builder with a default configuration. -
Method Summary
Modifier and TypeMethodDescriptionAdd a URI distribution point for a certificate revocation list.addCrlDistributionPoint(URI uri, X500Principal issuer) Add a URI distribution point for a certificate revocation list.Add the givenCertificateBuilder.ExtendedKeyUsageto the list of extended key usages.Add the given OID to the list of extended key usages.Add client-authentication to the list of extended key usages.Add code signing to the list of extended key usages.Add email protection to the list of extended key usages.Add Kerberos client authentication to the list of extended key usages.Add Microsoft smartcard login to the list of extended key usages.Add OCSP signing to the list of extended key usages.Add server-authentication to the list of extended key usages.Add time-stamping to the list of extended key usages.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.addExtensionOctetString(String identifierOID, boolean critical, byte[] contents) Add a custom extension to the certificate, with the given OID, criticality flag, and DER-encoded contents.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.addSanDirectoryName(String dirName) Add a Directory Name to the Subject Alternative Names.addSanDnsName(String dns) Add a DNS name to the Subject Alternate Names.addSanIpAddress(String ipAddress) Add an IP address to the Subject Alternative Names.addSanIpAddress(InetAddress ipAddress) Add an IP address to the Subject Alternative Names.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.addSanRegisteredId(String oid) Add a registeredID to the Subject Alternative Names.addSanRfc822Name(String name) Add an RFC 822 name to the Subject Alternative Names.addSanUriName(String uri) Add a URI name to the Subject Alternative Names.addSanUriName(URI uri) Add a URI name to the Subject Alternative Names.algorithm(CertificateBuilder.Algorithm algorithm) Set the key algorithm to use.buildIssuedBy(X509Bundle issuerBundle) Build aX509Bundlewith a certificate signed by the given issuer bundle.buildIssuedBy(X509Bundle issuerBundle, String signAlg) Build aX509Bundlewith a certificate signed by the given issuer bundle, using the specified signing algorithm.Build aX509Bundlewith a self-signed certificate.copy()Produce a copy of the current state in this certificate builder.ecp256()Make this certificate builder use the NIST EC-P 256 elliptic curve key algorithm.Instruct the certificate builder to not generate its own key pair, but to instead create a certificate that uses the given key pair.Set the not-after field of the certificate.Set the not-before field of the certificate.Set theProviderinstance to use when generating keys.Instruct the certificate builder to not generate its own key pair, but to instead create a certificate that uses the given public key.rsa2048()Make this certificate builder use the 2048-bit RSA encryption and signing algorithm.secureRandom(SecureRandom secureRandom) Set theSecureRandominstance to use when generating keys.serial(BigInteger serial) Set the specific serial number to use in the certificate.setIsCertificateAuthority(boolean isCA) Set the certificate authority field.setKeyUsage(boolean critical, CertificateBuilder.KeyUsage... keyUsages) The key usage specify the intended usages for which the certificate has been issued.setPathLengthConstraint(OptionalInt pathLengthConstraint) Certificate Authority certificates may impose a limit to the length of the verified certificate path they permit.Set the fully-qualified domain name (an X.500 name) as the subject of the certificate.subject(X500Principal name) Set the subject name of the certificate to the givenX500Principal.
-
Constructor Details
-
CertificateBuilder
public CertificateBuilder()Create a new certificate builder with a default configuration. Unless specified otherwise, the builder will produce bundles that use the NIST EC-P 256 key algorithm, and the certificates will be valid as of yesterday and expire tomorrow.
-
-
Method Details
-
copy
Produce a copy of the current state in this certificate builder.- Returns:
- A copy of this certificate builder.
-
provider
Set theProviderinstance to use when generating keys.- Parameters:
provider- The provider instance to use.- Returns:
- This certificate builder.
-
secureRandom
Set theSecureRandominstance to use when generating keys.- Parameters:
secureRandom- The secure random instance to use.- Returns:
- This certificate builder.
-
notBefore
Set the not-before field of the certificate. The certificate will not be valid before this time.- Parameters:
instant- The not-before time.- Returns:
- This certificate builder.
-
notAfter
Set the not-after field of the certificate. The certificate will not be valid after this time.- Parameters:
instant- The not-after time.- Returns:
- This certificate builder.
-
serial
Set the specific serial number to use in the certificate. One will be generated randomly, if none is specified.- Parameters:
serial- The serial number to use, ornull.- Returns:
- This certificate builder.
-
subject
Set the fully-qualified domain name (an X.500 name) as the subject of the certificate.- Parameters:
fqdn- The subject name to use.- Returns:
- This certificate builder.
-
subject
Set the subject name of the certificate to the givenX500Principal.- Parameters:
name- The subject name to use.- Returns:
- This certificate builder.
-
addSanOtherName
Add an Other Name to the Subject Alternative Names, of the given OID type, and with the given encoded value. The type and value will be wrapped in a SEQUENCE.- Parameters:
typeOid- The OID type of the Other Name value.encodedValue- The encoded Other Name value.- Returns:
- This certificate builder.
-
addSanRfc822Name
Add an RFC 822 name to the Subject Alternative Names. The RFC 822 standard is the obsolete specification for email, so these SANs are email addresses.- Parameters:
name- The email address to add to the SANs.- Returns:
- This certificate builder.
-
addSanDnsName
Add a DNS name to the Subject Alternate Names.- Parameters:
dns- The DNS name to add.- Returns:
- This certificate builder.
-
addSanDirectoryName
Add a Directory Name to the Subject Alternative Names. These are LDAP directory paths.- Parameters:
dirName- The directory name to add to the SANs.- Returns:
- This certificate builder.
-
addSanUriName
Add a URI name to the Subject Alternative Names.- Parameters:
uri- The URI to add to the SANs.- Returns:
- This certificate builder.
- Throws:
URISyntaxException
-
addSanUriName
Add a URI name to the Subject Alternative Names.- Parameters:
uri- The URI to add to the SANs.- Returns:
- This certificate builder.
-
addSanIpAddress
Add an IP address to the Subject Alternative Names. IPv4 and IPv6 addresses are both supported and converted to their correct encoding.- Parameters:
ipAddress- The IP address to add to the SANs.- Returns:
- This certificate builder.
-
addSanIpAddress
Add an IP address to the Subject Alternative Names. IPv4 and IPv6 addresses are both supported and converted to their correct encoding.- Parameters:
ipAddress- The IP address to add to the SANs.- Returns:
- This certificate builder.
-
addSanRegisteredId
Add a registeredID to the Subject Alternative Names. A registeredID is an OBJECT IDENTIFIER, or OID, in ASN.1 speak.- Parameters:
oid- The OID to add to the SANs.- Returns:
- This certificate builder.
-
addCrlDistributionPoint
Add a URI distribution point for a certificate revocation list.If you are testing certificate revocation using the
RevocationServer, you would obtain this URI fromRevocationServer.getCrlUri(X509Bundle)with your intended issuer certificate bundle.- Parameters:
uri- The URI for the CRL file.- Returns:
- This certificate builder.
-
addCrlDistributionPoint
Add a URI distribution point for a certificate revocation list.If you are testing certificate revocation using the
RevocationServer, you would obtain this URI fromRevocationServer.getCrlUri(X509Bundle)with your intended issuer certificate bundle.- Parameters:
uri- The URI for the CRL file.issuer- The issuer that signs the CRL file. This MUST benullif 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.- Returns:
- This certificate builder.
-
setIsCertificateAuthority
Set the certificate authority field. If this is set totrue, then this builder can build self-signed certificates, and those certifiactes can be used to sign other certificates.- Parameters:
isCA-trueif this builder should make CA certificates.- Returns:
- This certificate builder.
-
setPathLengthConstraint
Certificate Authority certificates may impose a limit to the length of the verified certificate path they permit.- Parameters:
pathLengthConstraint- The maximum verified path length, if any.- Returns:
- This certificate builder.
-
algorithm
Set the key algorithm to use. This also determines how certificates are signed.- Parameters:
algorithm- The algorithm to use when generating the private key.- Returns:
- This certificate builder.
-
ecp256
Make this certificate builder use the NIST EC-P 256 elliptic curve key algorithm. This algorithm provides a good balance between security, compatibility, performance, and key invalid input: '&' signature sizes.- Returns:
- This certificate builder.
- See Also:
-
rsa2048
Make this certificate builder use the 2048-bit RSA encryption and signing algorithm. This algorithm provides maximum compatibility, but keys are large and slow to generate.- Returns:
- This certificate builder.
- See Also:
-
publicKey
Instruct the certificate builder to not generate its own key pair, but to instead create a certificate that uses the given public 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.- Parameters:
publicKey- The public key to wrap in a certificate.- Returns:
- This certificate builder.
-
keyPair
Instruct the certificate builder to not generate its own key pair, but to instead create a certificate that uses the given key pair.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), or when creating cross-signed certificates.
Cross-signing is when two certificates have the same subject and public key, but are signed by different keys. In effect, it's the same logical certificate, but manifest as two different "concrete" certificate objects, i.e. two different
X509Bundleobjects. Cross-signing can be done to both leaf certificates and to issuers, and can create complicated certificate graphs. The technique is used for introducing new roots and issuers to a PKI system, in a backwards compatible way where certificates can be trusted by peers that aren't familiar with the new roots or issuers.If the given key pair is
null(the default) then a new key-pair will be generated instead.- Parameters:
keyPair- The key pair to use when creating a certificate.- Returns:
- This certificate builder.
-
addExtensionOctetString
public 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.- Parameters:
identifierOID- The OID identifying the extension.critical-trueif the extension is critical, otherwisefalse. Certificate systems MUST reject certificates with critical extensions they don't recognize.contents- The DER-encoded extension contents.- Returns:
- This certificate builder.
-
addExtensionUtf8String
public 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. The string will be converted to its proper binary encoding by this method.- Parameters:
identifierOID- The OID identifying the extension.critical-trueif the extension is critical, otherwisefalse. Certificate systems MUST reject certificates with critical extensions they don't recognize.value- The string value.- Returns:
- This certificate builder.
-
addExtensionAsciiString
public 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. The string will be converted to its proper binary encoding by this method.- Parameters:
identifierOID- The OID identifying the extension.critical-trueif the extension is critical, otherwisefalse. Certificate systems MUST reject certificates with critical extensions they don't recognize.value- The string value.- Returns:
- This certificate builder.
-
setKeyUsage
The key usage specify the intended usages for which the certificate has been issued. Some are overlapping, some are deprecated, and some are implied by usage.For Certificate Authority usage, the important ones are
CertificateBuilder.KeyUsage.keyCertSignandCertificateBuilder.KeyUsage.cRLSign.Any certificate that has
CertificateBuilder.KeyUsage.keyCertSignmust also havesetIsCertificateAuthority(boolean)set totrue.- Parameters:
critical-trueif certificate recipients are required to understand all the set bits, otherwisefalse.keyUsages- The key usages to set.- Returns:
- This certificate builder.
-
addExtendedKeyUsage
Add the given OID to the list of extended key usages.- Parameters:
oid- The OID to add.- Returns:
- This certificate builder.
- See Also:
-
addExtendedKeyUsage
Add the givenCertificateBuilder.ExtendedKeyUsageto the list of extended key usages.- Parameters:
keyUsage- The extended key usage to add.- Returns:
- This certificate builder.
-
addExtendedKeyUsageServerAuth
Add server-authentication to the list of extended key usages.- Returns:
- This certificate builder.
-
addExtendedKeyUsageClientAuth
Add client-authentication to the list of extended key usages.- Returns:
- This certificate builder.
-
addExtendedKeyUsageCodeSigning
Add code signing to the list of extended key usages.- Returns:
- This certificate builder.
-
addExtendedKeyUsageEmailProtection
Add email protection to the list of extended key usages.- Returns:
- This certificate builder.
-
addExtendedKeyUsageTimeStamping
Add time-stamping to the list of extended key usages.- Returns:
- This certificate builder.
-
addExtendedKeyUsageOcspSigning
Add OCSP signing to the list of extended key usages.- Returns:
- This certificate builder.
-
addExtendedKeyUsageKerberosClientAuth
Add Kerberos client authentication to the list of extended key usages.- Returns:
- This certificate builder.
-
addExtendedKeyUsageMicrosoftSmartcardLogin
Add Microsoft smartcard login to the list of extended key usages.- Returns:
- This certificate builder.
-
buildSelfSigned
Build aX509Bundlewith a self-signed certificate.- Returns:
- The newly created bundle.
- Throws:
Exception- If something went wrong in the process.
-
buildIssuedBy
Build aX509Bundlewith a certificate signed by the given issuer bundle. The signing algorithm used will be derived from the issuers public key.- Returns:
- The newly created bundle.
- Throws:
Exception- If something went wrong in the process.
-
buildIssuedBy
Build aX509Bundlewith a certificate signed by the given issuer bundle, using the specified signing algorithm.- Returns:
- The newly created bundle.
- Throws:
Exception- If something went wrong in the process.
-