@NotMutable @ThreadSafety(level=COMPLETELY_THREADSAFE) public final class X509Certificate extends java.lang.Object implements java.io.Serializable
com.unboundid.asn1
package. The ASN.1 specification is as follows:
Certificate ::= SEQUENCE { tbsCertificate TBSCertificate, signatureAlgorithm AlgorithmIdentifier, signatureValue BIT STRING } TBSCertificate ::= SEQUENCE { version [0] EXPLICIT Version DEFAULT v1, serialNumber CertificateSerialNumber, signature AlgorithmIdentifier, issuer Name, validity Validity, subject Name, subjectPublicKeyInfo SubjectPublicKeyInfo, issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL, -- If present, version MUST be v2 or v3 subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL, -- If present, version MUST be v2 or v3 extensions [3] EXPLICIT Extensions OPTIONAL -- If present, version MUST be v3 } Version ::= INTEGER { v1(0), v2(1), v3(2) } CertificateSerialNumber ::= INTEGER Validity ::= SEQUENCE { notBefore Time, notAfter Time } Time ::= CHOICE { utcTime UTCTime, generalTime GeneralizedTime } UniqueIdentifier ::= BIT STRING SubjectPublicKeyInfo ::= SEQUENCE { algorithm AlgorithmIdentifier, subjectPublicKey BIT STRING } Extensions ::= SEQUENCE SIZE (1..MAX) OF Extension Extension ::= SEQUENCE { extnID OBJECT IDENTIFIER, critical BOOLEAN DEFAULT FALSE, extnValue OCTET STRING -- contains the DER encoding of an ASN.1 value -- corresponding to the extension type identified -- by extnID } AlgorithmIdentifier ::= SEQUENCE { algorithm OBJECT IDENTIFIER, parameters ANY DEFINED BY algorithm OPTIONAL } Name ::= CHOICE { -- only one possibility for now -- rdnSequence RDNSequence } RDNSequence ::= SEQUENCE OF RelativeDistinguishedName RelativeDistinguishedName ::= SET SIZE (1..MAX) OF AttributeTypeAndValue AttributeTypeAndValue ::= SEQUENCE { type AttributeType, value AttributeValue } AttributeType ::= OBJECT IDENTIFIER AttributeValue ::= ANY -- DEFINED BY AttributeType
Constructor and Description |
---|
X509Certificate(byte[] encodedCertificate)
Decodes the contents of the provided byte array as an X.509 certificate.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(java.lang.Object o)
Indicates whether the provided object is considered equal to this X.509
certificate.
|
static X509Certificate |
generateIssuerSignedCertificate(SignatureAlgorithmIdentifier signatureAlgorithm,
X509Certificate issuerCertificate,
java.security.PrivateKey issuerPrivateKey,
OID publicKeyAlgorithmOID,
ASN1Element publicKeyAlgorithmParameters,
ASN1BitString encodedPublicKey,
DecodedPublicKey decodedPublicKey,
DN subjectDN,
long notBefore,
long notAfter,
X509CertificateExtension... extensions)
Generates an issuer-signed X.509 certificate with the provided information.
|
static X509Certificate |
generateSelfSignedCertificate(SignatureAlgorithmIdentifier signatureAlgorithm,
java.security.KeyPair keyPair,
DN subjectDN,
long notBefore,
long notAfter,
X509CertificateExtension... extensions)
Generates a self-signed X.509 certificate with the provided information.
|
static ObjectPair<X509Certificate,java.security.KeyPair> |
generateSelfSignedCertificate(SignatureAlgorithmIdentifier signatureAlgorithm,
PublicKeyAlgorithmIdentifier publicKeyAlgorithm,
int keySizeBits,
DN subjectDN,
long notBefore,
long notAfter,
X509CertificateExtension... extensions)
Generates a self-signed X.509 certificate with the provided information.
|
DecodedPublicKey |
getDecodedPublicKey()
Retrieves a decoded representation of the public key, if available.
|
ASN1BitString |
getEncodedPublicKey()
Retrieves the encoded public key as a bit string.
|
java.util.List<X509CertificateExtension> |
getExtensions()
Retrieves the list of certificate extensions.
|
DN |
getIssuerDN()
Retrieves the certificate issuer DN.
|
ASN1BitString |
getIssuerUniqueID()
Retrieves the issuer unique identifier for the certificate, if any.
|
java.util.Date |
getNotAfterDate()
Retrieves the certificate validity end time as a
Date . |
long |
getNotAfterTime()
Retrieves the certificate validity end time as the number of milliseconds
since the epoch (January 1, 1970 UTC).
|
java.util.Date |
getNotBeforeDate()
Retrieves the certificate validity start time as a
Date . |
long |
getNotBeforeTime()
Retrieves the certificate validity start time as the number of milliseconds
since the epoch (January 1, 1970 UTC).
|
java.lang.String |
getPublicKeyAlgorithmName()
Retrieves the certificate public key algorithm name, if available.
|
java.lang.String |
getPublicKeyAlgorithmNameOrOID()
Retrieves the public key algorithm name if it is available, or the string
representation of the public key algorithm OID if not.
|
OID |
getPublicKeyAlgorithmOID()
Retrieves the certificate public key algorithm OID.
|
ASN1Element |
getPublicKeyAlgorithmParameters()
Retrieves the encoded public key algorithm parameters, if present.
|
java.math.BigInteger |
getSerialNumber()
Retrieves the certificate serial number.
|
byte[] |
getSHA1Fingerprint()
Retrieves the bytes that comprise a SHA-1 fingerprint of this certificate.
|
byte[] |
getSHA256Fingerprint()
Retrieves the bytes that comprise a 256-bit SHA-2 fingerprint of this
certificate.
|
java.lang.String |
getSignatureAlgorithmName()
Retrieves the certificate signature algorithm name, if available.
|
java.lang.String |
getSignatureAlgorithmNameOrOID()
Retrieves the signature algorithm name if it is available, or the string
representation of the signature algorithm OID if not.
|
OID |
getSignatureAlgorithmOID()
Retrieves the certificate signature algorithm OID.
|
ASN1Element |
getSignatureAlgorithmParameters()
Retrieves the encoded signature algorithm parameters, if present.
|
ASN1BitString |
getSignatureValue()
Retrieves the signature value for the certificate.
|
DN |
getSubjectDN()
Retrieves the certificate subject DN.
|
ASN1BitString |
getSubjectUniqueID()
Retrieves the subject unique identifier for the certificate, if any.
|
X509CertificateVersion |
getVersion()
Retrieves the certificate version.
|
byte[] |
getX509CertificateBytes()
Retrieves the bytes that comprise the encoded representation of this X.509
certificate.
|
int |
hashCode()
Retrieves a hash code for this certificate.
|
boolean |
isIssuerFor(X509Certificate c)
Indicates whether this certificate is the issuer for the provided
certificate.
|
boolean |
isIssuerFor(X509Certificate c,
java.lang.StringBuilder nonMatchReason)
Indicates whether this certificate is the issuer for the provided
certificate.
|
boolean |
isSelfSigned()
Indicates whether this certificate is self-signed.
|
boolean |
isWithinValidityWindow()
Indicates whether the current time is within the certificate's validity
window.
|
boolean |
isWithinValidityWindow(java.util.Date date)
Indicates whether the provided
Date represents a time within the
certificate's validity window. |
boolean |
isWithinValidityWindow(long time)
Indicates whether the specified time is within the certificate's validity
window.
|
java.security.cert.Certificate |
toCertificate()
Converts this X.509 certificate object to a Java
Certificate
object. |
java.util.List<java.lang.String> |
toPEM()
Retrieves a list of the lines that comprise a PEM representation of this
X.509 certificate.
|
java.lang.String |
toPEMString()
Retrieves a multi-line string containing a PEM representation of this X.509
certificate.
|
java.lang.String |
toString()
Retrieves a string representation of the decoded X.509 certificate.
|
void |
toString(java.lang.StringBuilder buffer)
Appends a string representation of the decoded X.509 certificate to the
provided buffer.
|
void |
verifySignature(X509Certificate issuerCertificate)
Verifies the signature for this certificate.
|
public X509Certificate(@NotNull byte[] encodedCertificate) throws CertException
encodedCertificate
- The byte array containing the encoded X.509
certificate. This must not be null
.CertException
- If the contents of the provided byte array could
not be decoded as a valid X.509 certificate.@NotNull public static ObjectPair<X509Certificate,java.security.KeyPair> generateSelfSignedCertificate(@NotNull SignatureAlgorithmIdentifier signatureAlgorithm, @NotNull PublicKeyAlgorithmIdentifier publicKeyAlgorithm, int keySizeBits, @NotNull DN subjectDN, long notBefore, long notAfter, @Nullable X509CertificateExtension... extensions) throws CertException
signatureAlgorithm
- The algorithm to use to generate the signature.
This must not be null
.publicKeyAlgorithm
- The algorithm to use to generate the key pair.
This must not be null
.keySizeBits
- The size of the key to generate, in bits.subjectDN
- The subject DN for the certificate. This must
not be null
.notBefore
- The validity start time for the certificate.notAfter
- The validity end time for the certificate.extensions
- The set of extensions to include in the
certificate. This may be null
or empty
if the certificate should not include any
custom extensions. Note that the generated
certificate will automatically include a
SubjectKeyIdentifierExtension
, so that
should not be provided.ObjectPair
that contains both the self-signed
certificate and its corresponding key pair.CertException
- If a problem is encountered while creating the
certificate.@NotNull public static X509Certificate generateSelfSignedCertificate(@NotNull SignatureAlgorithmIdentifier signatureAlgorithm, @NotNull java.security.KeyPair keyPair, @NotNull DN subjectDN, long notBefore, long notAfter, @Nullable X509CertificateExtension... extensions) throws CertException
signatureAlgorithm
- The algorithm to use to generate the signature.
This must not be null
.keyPair
- The key pair for the certificate. This must
not be null
.subjectDN
- The subject DN for the certificate. This must
not be null
.notBefore
- The validity start time for the certificate.notAfter
- The validity end time for the certificate.extensions
- The set of extensions to include in the
certificate. This may be null
or empty
if the certificate should not include any
custom extensions. Note that the generated
certificate will automatically include a
SubjectKeyIdentifierExtension
, so that
should not be provided.ObjectPair
that contains both the self-signed
certificate and its corresponding key pair.CertException
- If a problem is encountered while creating the
certificate.@NotNull public static X509Certificate generateIssuerSignedCertificate(@NotNull SignatureAlgorithmIdentifier signatureAlgorithm, @NotNull X509Certificate issuerCertificate, @NotNull java.security.PrivateKey issuerPrivateKey, @NotNull OID publicKeyAlgorithmOID, @Nullable ASN1Element publicKeyAlgorithmParameters, @NotNull ASN1BitString encodedPublicKey, @Nullable DecodedPublicKey decodedPublicKey, @NotNull DN subjectDN, long notBefore, long notAfter, @NotNull X509CertificateExtension... extensions) throws CertException
signatureAlgorithm
- The algorithm to use to generate the signature. This must not
be null
.issuerCertificate
- The certificate for the issuer. This must not be
null
.issuerPrivateKey
- The private key for the issuer. This must not be
null
.publicKeyAlgorithmOID
- The OID for the certificate's public key algorithm. This must
not be null
.publicKeyAlgorithmParameters
- The encoded public key algorithm parameters for the
certificate. This may be null
if there are no
parameters.encodedPublicKey
- The encoded public key for the certificate. This must not be
null
.decodedPublicKey
- The decoded public key for the certificate. This may be
null
if it is not available.subjectDN
- The subject DN for the certificate. This must not be
null
.notBefore
- The validity start time for the certificate.notAfter
- The validity end time for the certificate.extensions
- The set of extensions to include in the certificate. This
may be null
or empty if the certificate should not
include any custom extensions. Note that the generated
certificate will automatically include a
SubjectKeyIdentifierExtension
, so that should not be
provided. In addition, if the issuer certificate includes its
own SubjectKeyIdentifierExtension
, then its value will
be used to generate an
AuthorityKeyIdentifierExtension
.CertException
- If a problem is encountered while creating the
certificate.@NotNull public byte[] getX509CertificateBytes()
@NotNull public X509CertificateVersion getVersion()
@NotNull public java.math.BigInteger getSerialNumber()
@NotNull public OID getSignatureAlgorithmOID()
@Nullable public java.lang.String getSignatureAlgorithmName()
null
if the
signature algorithm OID does not correspond to any known algorithm
name.@NotNull public java.lang.String getSignatureAlgorithmNameOrOID()
@Nullable public ASN1Element getSignatureAlgorithmParameters()
null
if
there are no signature algorithm parameters.@NotNull public DN getIssuerDN()
public long getNotBeforeTime()
@NotNull public java.util.Date getNotBeforeDate()
Date
.Date
.public long getNotAfterTime()
@NotNull public java.util.Date getNotAfterDate()
Date
.Date
.public boolean isWithinValidityWindow()
true
if the current time is within the certificate's
validity window, or false
if not.public boolean isWithinValidityWindow(@NotNull java.util.Date date)
Date
represents a time within the
certificate's validity window.date
- The Date
for which to make the determination. It
must not be null
.true
if the provided Date
is within the
certificate's validity window, or false
if not.public boolean isWithinValidityWindow(long time)
time
- The time to for which to make the determination.true
if the specified time is within the certificate's
validity window, or false
if not.@NotNull public DN getSubjectDN()
@NotNull public OID getPublicKeyAlgorithmOID()
@Nullable public java.lang.String getPublicKeyAlgorithmName()
null
if the
public key algorithm OID does not correspond to any known
algorithm name.@NotNull public java.lang.String getPublicKeyAlgorithmNameOrOID()
@Nullable public ASN1Element getPublicKeyAlgorithmParameters()
null
if
there are no public key algorithm parameters.@NotNull public ASN1BitString getEncodedPublicKey()
@Nullable public DecodedPublicKey getDecodedPublicKey()
null
if the
public key could not be decoded.@Nullable public ASN1BitString getIssuerUniqueID()
null
if there is none.@Nullable public ASN1BitString getSubjectUniqueID()
null
if there is none.@NotNull public java.util.List<X509CertificateExtension> getExtensions()
@NotNull public ASN1BitString getSignatureValue()
public void verifySignature(@Nullable X509Certificate issuerCertificate) throws CertException
issuerCertificate
- The issuer certificate for this certificate. It
may be null
if this is a self-signed
certificate. It must not be null
if it
is not a self-signed certificate.CertException
- If the certificate signature could not be verified.@NotNull public byte[] getSHA1Fingerprint() throws CertException
CertException
- If a problem is encountered while computing the
fingerprint.@NotNull public byte[] getSHA256Fingerprint() throws CertException
CertException
- If a problem is encountered while computing the
fingerprint.public boolean isSelfSigned()
true
if this certificate is self-signed, or false
if it is not.public boolean isIssuerFor(@NotNull X509Certificate c)
c
- The certificate for which to make the determination. This must
not be null
.true
if this certificate is considered the issuer for the
provided certificate, or false if not.public boolean isIssuerFor(@NotNull X509Certificate c, @Nullable java.lang.StringBuilder nonMatchReason)
c
- The certificate for which to make the
determination. This must not be null
.nonMatchReason
- An optional buffer that may be updated with the
reason that this certificate is not considered the
issuer for the provided certificate. This may be
null
if the caller does not require a
reason.true
if this certificate is considered the issuer for the
provided certificate, or false if not.@NotNull public java.security.cert.Certificate toCertificate() throws java.security.cert.CertificateException
Certificate
object.Certificate
object that corresponds to this
X.509 certificate.java.security.cert.CertificateException
- If a problem is encountered while performing
the conversion.public int hashCode()
hashCode
in class java.lang.Object
public boolean equals(@Nullable java.lang.Object o)
equals
in class java.lang.Object
o
- The object for which to make the determination.true
if the provided object is considered equal to this
X.509 certificate, or false
if not.@NotNull public java.lang.String toString()
toString
in class java.lang.Object
public void toString(@NotNull java.lang.StringBuilder buffer)
buffer
- The buffer to which the information should be appended.@NotNull public java.util.List<java.lang.String> toPEM()
@NotNull public java.lang.String toPEMString()