Interface CertificateBuilder
public interface CertificateBuilder
A fluent builder for an unsigned X.509 v3 certificate body or a signed standard
X509Certificate.
The builder collects the certificate fields and extensions, then produces the
final certificate by streaming the tbsCertificate through a caller
supplied ContentSigner — so the private key never enters the builder.
Typed setters are provided for the common extensions; arbitrary extensions can
be added with addExtension(CertificateExtension).
For deferred signing, buildTbs(AlgorithmIdentifier) returns the exact
canonical unsigned body without a signer invocation.
Instances are single-use and not thread-safe.
-
Method Summary
Modifier and TypeMethodDescriptionaddExtension(CertificateExtension extension) Adds an arbitrary extension.authorityKeyIdentifier(byte[] keyIdentifier) Adds anAuthorityKeyIdentifiercarrying the given key identifier.basicConstraints(BasicConstraints basicConstraints, boolean critical) Adds aBasicConstraintsextension.build(ContentSigner signer) Signs the assembledtbsCertificatewith the given signer and returns the resulting certificate.default byte[]Encodes the standardized alternative-signature input, omitting both theTBSCertificate.signaturefield and theAltSignatureValueextension.byte[]buildTbs(AlgorithmIdentifier signatureAlgorithm) Encodes the configured unsigned X.509 v3TBSCertificateas canonical DER.extendedKeyUsage(ExtendedKeyUsage extendedKeyUsage, boolean critical) Adds anExtendedKeyUsageextension.issuer(X500Principal issuer) Sets the issuer distinguished name (required).Adds aKeyUsageextension.Sets the subject public key (required, unlesspublicKeyInfo(byte[])is used).publicKeyInfo(byte[] subjectPublicKeyInfoDer) Sets the subject public key from the exact DER of aSubjectPublicKeyInfo, preserving the original encoding byte-for-byte (required, unlesspublicKey(PublicKey)is used).serialNumber(BigInteger serialNumber) Sets the certificate serial number (required, positive).subject(X500Principal subject) Sets the subject distinguished name (required).subjectAlternativeNames(List<GeneralName> names, boolean critical) Adds aSubjectAlternativeNameextension.Adds aSubjectKeyIdentifierderived from the subject public key.Sets the validity window (required).
-
Method Details
-
serialNumber
Sets the certificate serial number (required, positive).- Parameters:
serialNumber- the serial number- Returns:
- this builder
-
subject
Sets the subject distinguished name (required).- Parameters:
subject- the subject DN- Returns:
- this builder
-
issuer
Sets the issuer distinguished name (required).- Parameters:
issuer- the issuer DN- Returns:
- this builder
-
validity
Sets the validity window (required).- Parameters:
notBefore- inclusive start of validitynotAfter- inclusive end of validity- Returns:
- this builder
-
publicKey
Sets the subject public key (required, unlesspublicKeyInfo(byte[])is used).- Parameters:
subjectPublicKey- the subject public key- Returns:
- this builder
-
publicKeyInfo
Sets the subject public key from the exact DER of aSubjectPublicKeyInfo, preserving the original encoding byte-for-byte (required, unlesspublicKey(PublicKey)is used).- Parameters:
subjectPublicKeyInfoDer- the exact SPKI DER- Returns:
- this builder
-
basicConstraints
Adds aBasicConstraintsextension.- Parameters:
basicConstraints- the basic-constraints valuecritical- whether the extension is critical- Returns:
- this builder
-
keyUsage
Adds aKeyUsageextension.- Parameters:
keyUsage- the set of key-usage bitscritical- whether the extension is critical- Returns:
- this builder
-
extendedKeyUsage
Adds anExtendedKeyUsageextension.- Parameters:
extendedKeyUsage- the extended key usage valuecritical- whether the extension is critical- Returns:
- this builder
-
subjectAlternativeNames
Adds aSubjectAlternativeNameextension.- Parameters:
names- the general namescritical- whether the extension is critical- Returns:
- this builder
-
subjectKeyIdentifier
CertificateBuilder subjectKeyIdentifier()Adds aSubjectKeyIdentifierderived from the subject public key.- Returns:
- this builder
-
authorityKeyIdentifier
Adds anAuthorityKeyIdentifiercarrying the given key identifier.- Parameters:
keyIdentifier- the issuer subject-key-identifier bytes- Returns:
- this builder
-
addExtension
Adds an arbitrary extension.- Parameters:
extension- the extension to add- Returns:
- this builder
-
buildTbs
Encodes the configured unsigned X.509 v3TBSCertificateas canonical DER. The supplied identifier is embedded exactly as the certificate signature algorithm. This method performs no signing and invokes no content signer.Required fields and extensions are the same as for
build(ContentSigner). Local time and auxiliary memory are linear in the resulting TBS byte size.- Parameters:
signatureAlgorithm- exact signature algorithm and parameter representation- Returns:
- newly allocated canonical DER of the unsigned certificate body
- Throws:
NullPointerException- if the algorithm or a required field is missingIllegalArgumentException- if fields or extensions cannot form a certificate bodyIllegalStateException- if DER encoding fails
-
buildAlternativeSignatureTbs
default byte[] buildAlternativeSignatureTbs()Encodes the standardized alternative-signature input, omitting both theTBSCertificate.signaturefield and theAltSignatureValueextension. Other configured fields and extension order are preserved. The alternative algorithm and subject alternative public key extensions must be supplied by the caller. No signer is invoked. Time and auxiliary memory are linear in the encoded body size.- Returns:
- newly allocated canonical DER pre-TBS sequence
- Throws:
UnsupportedOperationException- if this provider lacks this capabilityIllegalArgumentException- if configured fields or extensions are invalidNullPointerException- if a required field is missingIllegalStateException- if encoding fails
-
build
Signs the assembledtbsCertificatewith the given signer and returns the resulting certificate.- Parameters:
signer- the content signer supplying the signature and algorithm- Returns:
- the signed certificate
-