Package com.unboundid.util.ssl.cert
Class X509Certificate
java.lang.Object
com.unboundid.util.ssl.cert.X509Certificate
- All Implemented Interfaces:
Serializable
@NotMutable
@ThreadSafety(level=COMPLETELY_THREADSAFE)
public final class X509Certificate
extends Object
implements Serializable
This class provides support for decoding an X.509 certificate as defined in
RFC 5280. The certificate
is encoded using the ASN.1 Distinguished Encoding Rules (DER), which is a
subset of BER, and is supported by the code in the
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
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionX509Certificate(byte[] encodedCertificate) Decodes the contents of the provided byte array as an X.509 certificate. -
Method Summary
Modifier and TypeMethodDescriptionbooleanIndicates whether the provided object is considered equal to this X.509 certificate.static X509CertificategenerateIssuerSignedCertificate(SignatureAlgorithmIdentifier signatureAlgorithm, X509Certificate issuerCertificate, 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 ObjectPair<X509Certificate, 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.static X509CertificategenerateSelfSignedCertificate(SignatureAlgorithmIdentifier signatureAlgorithm, KeyPair keyPair, DN subjectDN, long notBefore, long notAfter, X509CertificateExtension... extensions) Generates a self-signed X.509 certificate with the provided information.Retrieves a decoded representation of the public key, if available.Retrieves the encoded public key as a bit string.Retrieves the list of certificate extensions.Retrieves the certificate issuer DN.Retrieves the issuer unique identifier for the certificate, if any.Retrieves the certificate validity end time as aDate.longRetrieves the certificate validity end time as the number of milliseconds since the epoch (January 1, 1970 UTC).Retrieves the certificate validity start time as aDate.longRetrieves the certificate validity start time as the number of milliseconds since the epoch (January 1, 1970 UTC).Retrieves the certificate public key algorithm name, if available.Retrieves the public key algorithm name if it is available, or the string representation of the public key algorithm OID if not.Retrieves the certificate public key algorithm OID.Retrieves the encoded public key algorithm parameters, if present.Retrieves the certificate serial number.byte[]Retrieves the bytes that comprise a SHA-1 fingerprint of this certificate.byte[]Retrieves the bytes that comprise a 256-bit SHA-2 fingerprint of this certificate.Retrieves the certificate signature algorithm name, if available.Retrieves the signature algorithm name if it is available, or the string representation of the signature algorithm OID if not.Retrieves the certificate signature algorithm OID.Retrieves the encoded signature algorithm parameters, if present.Retrieves the signature value for the certificate.Retrieves the certificate subject DN.Retrieves the subject unique identifier for the certificate, if any.Retrieves the certificate version.byte[]Retrieves the bytes that comprise the encoded representation of this X.509 certificate.inthashCode()Retrieves a hash code for this certificate.booleanIndicates whether this certificate is the issuer for the provided certificate.booleanisIssuerFor(X509Certificate c, StringBuilder nonMatchReason) Indicates whether this certificate is the issuer for the provided certificate.booleanIndicates whether this certificate is self-signed.booleanIndicates whether the current time is within the certificate's validity window.booleanisWithinValidityWindow(long time) Indicates whether the specified time is within the certificate's validity window.booleanisWithinValidityWindow(Date date) Indicates whether the providedDaterepresents a time within the certificate's validity window.Converts this X.509 certificate object to a JavaCertificateobject.toPEM()Retrieves a list of the lines that comprise a PEM representation of this X.509 certificate.Retrieves a multi-line string containing a PEM representation of this X.509 certificate.toString()Retrieves a string representation of the decoded X.509 certificate.voidtoString(StringBuilder buffer) Appends a string representation of the decoded X.509 certificate to the provided buffer.voidverifySignature(X509Certificate issuerCertificate) Verifies the signature for this certificate.
-
Constructor Details
-
X509Certificate
Decodes the contents of the provided byte array as an X.509 certificate.- Parameters:
encodedCertificate- The byte array containing the encoded X.509 certificate. This must not benull.- Throws:
CertException- If the contents of the provided byte array could not be decoded as a valid X.509 certificate.
-
-
Method Details
-
generateSelfSignedCertificate
@NotNull public static ObjectPair<X509Certificate,KeyPair> generateSelfSignedCertificate(@NotNull SignatureAlgorithmIdentifier signatureAlgorithm, @NotNull PublicKeyAlgorithmIdentifier publicKeyAlgorithm, int keySizeBits, @NotNull DN subjectDN, long notBefore, long notAfter, @Nullable X509CertificateExtension... extensions) throws CertException Generates a self-signed X.509 certificate with the provided information.- Parameters:
signatureAlgorithm- The algorithm to use to generate the signature. This must not benull.publicKeyAlgorithm- The algorithm to use to generate the key pair. This must not benull.keySizeBits- The size of the key to generate, in bits.subjectDN- The subject DN for the certificate. This must not benull.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 benullor empty if the certificate should not include any custom extensions. Note that the generated certificate will automatically include aSubjectKeyIdentifierExtension, so that should not be provided.- Returns:
- An
ObjectPairthat contains both the self-signed certificate and its corresponding key pair. - Throws:
CertException- If a problem is encountered while creating the certificate.
-
generateSelfSignedCertificate
@NotNull public static X509Certificate generateSelfSignedCertificate(@NotNull SignatureAlgorithmIdentifier signatureAlgorithm, @NotNull KeyPair keyPair, @NotNull DN subjectDN, long notBefore, long notAfter, @Nullable X509CertificateExtension... extensions) throws CertException Generates a self-signed X.509 certificate with the provided information.- Parameters:
signatureAlgorithm- The algorithm to use to generate the signature. This must not benull.keyPair- The key pair for the certificate. This must not benull.subjectDN- The subject DN for the certificate. This must not benull.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 benullor empty if the certificate should not include any custom extensions. Note that the generated certificate will automatically include aSubjectKeyIdentifierExtension, so that should not be provided.- Returns:
- An
ObjectPairthat contains both the self-signed certificate and its corresponding key pair. - Throws:
CertException- If a problem is encountered while creating the certificate.
-
generateIssuerSignedCertificate
@NotNull public static X509Certificate generateIssuerSignedCertificate(@NotNull SignatureAlgorithmIdentifier signatureAlgorithm, @NotNull X509Certificate issuerCertificate, @NotNull 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 Generates an issuer-signed X.509 certificate with the provided information.- Parameters:
signatureAlgorithm- The algorithm to use to generate the signature. This must not benull.issuerCertificate- The certificate for the issuer. This must not benull.issuerPrivateKey- The private key for the issuer. This must not benull.publicKeyAlgorithmOID- The OID for the certificate's public key algorithm. This must not benull.publicKeyAlgorithmParameters- The encoded public key algorithm parameters for the certificate. This may benullif there are no parameters.encodedPublicKey- The encoded public key for the certificate. This must not benull.decodedPublicKey- The decoded public key for the certificate. This may benullif it is not available.subjectDN- The subject DN for the certificate. This must not benull.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 benullor empty if the certificate should not include any custom extensions. Note that the generated certificate will automatically include aSubjectKeyIdentifierExtension, so that should not be provided. In addition, if the issuer certificate includes its ownSubjectKeyIdentifierExtension, then its value will be used to generate anAuthorityKeyIdentifierExtension.- Returns:
- The issuer-signed certificate.
- Throws:
CertException- If a problem is encountered while creating the certificate.
-
getX509CertificateBytes
Retrieves the bytes that comprise the encoded representation of this X.509 certificate.- Returns:
- The bytes that comprise the encoded representation of this X.509 certificate.
-
getVersion
Retrieves the certificate version.- Returns:
- The certificate version.
-
getSerialNumber
Retrieves the certificate serial number.- Returns:
- The certificate serial number.
-
getSignatureAlgorithmOID
Retrieves the certificate signature algorithm OID.- Returns:
- The certificate signature algorithm OID.
-
getSignatureAlgorithmName
Retrieves the certificate signature algorithm name, if available.- Returns:
- The certificate signature algorithm name, or
nullif the signature algorithm OID does not correspond to any known algorithm name.
-
getSignatureAlgorithmNameOrOID
Retrieves the signature algorithm name if it is available, or the string representation of the signature algorithm OID if not.- Returns:
- The signature algorithm name or OID.
-
getSignatureAlgorithmParameters
Retrieves the encoded signature algorithm parameters, if present.- Returns:
- The encoded signature algorithm parameters, or
nullif there are no signature algorithm parameters.
-
getIssuerDN
Retrieves the certificate issuer DN.- Returns:
- The certificate issuer DN.
-
getNotBeforeTime
Retrieves the certificate validity start time as the number of milliseconds since the epoch (January 1, 1970 UTC).- Returns:
- The certificate validity start time as the number of milliseconds since the epoch.
-
getNotBeforeDate
Retrieves the certificate validity start time as aDate.- Returns:
- The certificate validity start time as a
Date.
-
getNotAfterTime
Retrieves the certificate validity end time as the number of milliseconds since the epoch (January 1, 1970 UTC).- Returns:
- The certificate validity end time as the number of milliseconds since the epoch.
-
getNotAfterDate
Retrieves the certificate validity end time as aDate.- Returns:
- The certificate validity end time as a
Date.
-
isWithinValidityWindow
Indicates whether the current time is within the certificate's validity window.- Returns:
trueif the current time is within the certificate's validity window, orfalseif not.
-
isWithinValidityWindow
Indicates whether the providedDaterepresents a time within the certificate's validity window.- Parameters:
date- TheDatefor which to make the determination. It must not benull.- Returns:
trueif the providedDateis within the certificate's validity window, orfalseif not.
-
isWithinValidityWindow
Indicates whether the specified time is within the certificate's validity window.- Parameters:
time- The time to for which to make the determination.- Returns:
trueif the specified time is within the certificate's validity window, orfalseif not.
-
getSubjectDN
Retrieves the certificate subject DN.- Returns:
- The certificate subject DN.
-
getPublicKeyAlgorithmOID
Retrieves the certificate public key algorithm OID.- Returns:
- The certificate public key algorithm OID.
-
getPublicKeyAlgorithmName
Retrieves the certificate public key algorithm name, if available.- Returns:
- The certificate public key algorithm name, or
nullif the public key algorithm OID does not correspond to any known algorithm name.
-
getPublicKeyAlgorithmNameOrOID
Retrieves the public key algorithm name if it is available, or the string representation of the public key algorithm OID if not.- Returns:
- The signature algorithm name or OID.
-
getPublicKeyAlgorithmParameters
Retrieves the encoded public key algorithm parameters, if present.- Returns:
- The encoded public key algorithm parameters, or
nullif there are no public key algorithm parameters.
-
getEncodedPublicKey
Retrieves the encoded public key as a bit string.- Returns:
- The encoded public key as a bit string.
-
getDecodedPublicKey
Retrieves a decoded representation of the public key, if available.- Returns:
- A decoded representation of the public key, or
nullif the public key could not be decoded.
-
getIssuerUniqueID
Retrieves the issuer unique identifier for the certificate, if any.- Returns:
- The issuer unique identifier for the certificate, or
nullif there is none.
-
getSubjectUniqueID
Retrieves the subject unique identifier for the certificate, if any.- Returns:
- The subject unique identifier for the certificate, or
nullif there is none.
-
getExtensions
Retrieves the list of certificate extensions.- Returns:
- The list of certificate extensions.
-
getSignatureValue
Retrieves the signature value for the certificate.- Returns:
- The signature value for the certificate.
-
verifySignature
Verifies the signature for this certificate.- Parameters:
issuerCertificate- The issuer certificate for this certificate. It may benullif this is a self-signed certificate. It must not benullif it is not a self-signed certificate.- Throws:
CertException- If the certificate signature could not be verified.
-
getSHA1Fingerprint
Retrieves the bytes that comprise a SHA-1 fingerprint of this certificate.- Returns:
- The bytes that comprise a SHA-1 fingerprint of this certificate.
- Throws:
CertException- If a problem is encountered while computing the fingerprint.
-
getSHA256Fingerprint
Retrieves the bytes that comprise a 256-bit SHA-2 fingerprint of this certificate.- Returns:
- The bytes that comprise a 256-bit SHA-2 fingerprint of this certificate.
- Throws:
CertException- If a problem is encountered while computing the fingerprint.
-
isSelfSigned
Indicates whether this certificate is self-signed. The following criteria will be used to make the determination:- If the certificate has both subject key identifier and authority key identifier extensions, then it will be considered self-signed if and only if the subject key identifier matches the authority key identifier.
- If the certificate does not have both a subject key identifier and an authority key identifier, then it will be considered self-signed if and only if its subject DN matches its issuer DN.
- Returns:
trueif this certificate is self-signed, orfalseif it is not.
-
isIssuerFor
Indicates whether this certificate is the issuer for the provided certificate. In order for this to be true, the following conditions must be met:- The subject DN of this certificate must match the issuer DN for the provided certificate.
- If the provided certificate has an authority key identifier extension, then this certificate must have a subject key identifier extension with a matching identifier value.
- Parameters:
c- The certificate for which to make the determination. This must not benull.- Returns:
trueif this certificate is considered the issuer for the provided certificate, or false if not.
-
isIssuerFor
Indicates whether this certificate is the issuer for the provided certificate. In order for this to be true, the following conditions must be met:- The subject DN of this certificate must match the issuer DN for the provided certificate.
- If the provided certificate has an authority key identifier extension, then this certificate must have a subject key identifier extension with a matching identifier value.
- Parameters:
c- The certificate for which to make the determination. This must not benull.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 benullif the caller does not require a reason.- Returns:
trueif this certificate is considered the issuer for the provided certificate, or false if not.
-
toCertificate
Converts this X.509 certificate object to a JavaCertificateobject.- Returns:
- The Java
Certificateobject that corresponds to this X.509 certificate. - Throws:
CertificateException- If a problem is encountered while performing the conversion.
-
hashCode
Retrieves a hash code for this certificate. -
equals
Indicates whether the provided object is considered equal to this X.509 certificate. -
toString
Retrieves a string representation of the decoded X.509 certificate. -
toString
Appends a string representation of the decoded X.509 certificate to the provided buffer.- Parameters:
buffer- The buffer to which the information should be appended.
-
toPEM
Retrieves a list of the lines that comprise a PEM representation of this X.509 certificate.- Returns:
- A list of the lines that comprise a PEM representation of this X.509 certificate.
-
toPEMString
Retrieves a multi-line string containing a PEM representation of this X.509 certificate.- Returns:
- A multi-line string containing a PEM representation of this X.509 certificate.
-