Encapsulates an X509 certificate and provides read-only access to its information.
const { X509Certificate } = await import('node:crypto'); const x509 = new X509Certificate('{... pem encoded cert ...}'); console.log(x509.subject);
const { X509Certificate } = require('node:crypto'); const x509 = new X509Certificate('{... pem encoded cert ...}'); console.log(x509.subject);
new X509Certificate(buffer): X509Certificate
string | TypedArray | Buffer | DataViewbooleantrue if this is a Certificate Authority (CA)
certificate.x509.checkEmail(email, options?): string | undefined
Checks whether the certificate matches the given email address.
If the 'subject' option is undefined or set to 'default', the certificate
subject is considered according to OpenSSL's default behavior.
If the 'subject' option is set to 'always' and if the subject alternative
name extension either does not exist or does not contain a matching email
address, the certificate subject is considered.
If the 'subject' option is set to 'never', the certificate subject is never
considered, even if the certificate contains no subject alternative names.
x509.checkHost(name, options?): string | undefined
Checks whether the certificate matches the given host name.
If the certificate matches the given host name, the matching subject name is
returned. The returned name might be an exact match (e.g., foo.example.com)
or it might contain wildcards (e.g., *.example.com). Because host name
comparisons are case-insensitive, the returned subject name might also differ
from the given name in capitalization.
If the 'subject' option is undefined or set to 'default', the certificate
subject is considered according to OpenSSL's default behavior.
If the 'subject' option is set to 'always' and if the subject alternative
name extension either does not exist or does not contain a matching DNS name,
the certificate subject is considered.
If the 'subject' option is set to 'never', the certificate subject is never
considered, even if the certificate contains no subject alternative names.
x509.checkIP
History
options argument has been removed since it had no effect.x509.checkIP(ip): string | undefined
Checks whether the certificate matches the given IP address (IPv4 or IPv6).
Only RFC 5280 iPAddress subject alternative names are considered, and they
must match the given ip address exactly. Other subject alternative names as
well as the subject field of the certificate are ignored.
x509.checkIssued(otherCert): boolean
X509CertificatebooleanChecks whether this certificate was potentially issued by the given otherCert
by comparing the certificate metadata.
This is useful for pruning a list of possible issuer certificates which have been selected using a more rudimentary filtering routine, i.e. just based on subject and issuer names.
Finally, to verify that this certificate's signature was produced by a private key
corresponding to otherCert's public key use x509.verify(publicKey)
with otherCert's public key represented as a KeyObject
like so
if (!x509.verify(otherCert.publicKey)) { throw new Error('otherCert did not issue x509'); }
x509.checkPrivateKey(privateKey): boolean
Checks whether the public key for this certificate is consistent with the given private key.
One entry is returned for every extension, in the order in which it appears in
the certificate. Unknown extensions and repeated OIDs are included. The
contents of value are not decoded according to oid. A certificate with no
extensions produces an empty array.
Unlike x509.subjectAltName, x509.infoAccess, and
x509.keyUsage (Extended Key Usage), which decode or format selected
extension types, this property exposes raw extension contents. value contains
the exact extension value bytes, not a complete encoded Extension record.
The extension-specific syntax is not validated.
Each access returns a new array containing new objects and Buffers. Modifying
them does not affect the certificate or later accesses.
For example, a Basic Constraints extension allowing a certificate to act as a CA can appear as:
console.log(x509.extensions[0]); // { // oid: '2.5.29.19', // critical: true, // value: <Buffer 30 03 01 01 ff> // }
Accessing this property throws ERR_CRYPTO_INVALID_X509_CERTIFICATE if the
extensions cannot be inspected.
stringThe SHA-1 fingerprint of this certificate.
Because SHA-1 is cryptographically broken and because the security of SHA-1 is
significantly worse than that of algorithms that are commonly used to sign
certificates, consider using x509.fingerprint256 instead.
stringThe SHA-256 fingerprint of this certificate.
stringThe SHA-512 fingerprint of this certificate.
Because computing the SHA-256 fingerprint is usually faster and because it is
only half the size of the SHA-512 fingerprint, x509.fingerprint256 may be
a better choice. While SHA-512 presumably provides a higher level of security in
general, the security of SHA-256 matches that of most algorithms that are
commonly used to sign certificates.
stringA textual representation of the certificate's authority information access extension.
This is a line feed separated list of access descriptions. Each line begins with the access method and the kind of the access location, followed by a colon and the value associated with the access location.
After the prefix denoting the access method and the kind of the access location, the remainder of each line might be enclosed in quotes to indicate that the value is a JSON string literal. For backward compatibility, Node.js only uses JSON string literals within this property when necessary to avoid ambiguity. Third-party code should be prepared to handle both possible entry formats.
stringThe issuer identification included in this certificate.
X509CertificateThe issuer certificate or undefined if the issuer certificate is not
available.
The issuer distinguished name, also exposed as a formatted string by
x509.issuer. The outer rdns array contains the relative distinguished
names in encoded order. Each inner array represents one relative distinguished
name and contains its attributes in encoded order. A valid empty name has an
empty rdns array.
rdns is a decoded view and does not include ASN.1 value types. Use raw for a
lossless representation of the complete name, including attribute values whose
value property is omitted from rdns.
Each access returns newly allocated objects, arrays, and a Buffer.
Modifying them does not affect the certificate or later accesses.
For example:
console.log(x509.issuerName.rdns); // [ // [ { oid: '2.5.4.6', value: 'US' } ], // [ { oid: '2.5.4.10', value: 'Example CA' } ] // ]
Accessing this property throws ERR_CRYPTO_INVALID_X509_CERTIFICATE if the
issuer name cannot be inspected.
string[]An array detailing the key extended usages for this certificate.
KeyObjectThe public key KeyObject for this certificate.
BufferA Buffer containing the DER encoding of this certificate.
stringThe serial number of this certificate.
Serial numbers are assigned by certificate authorities and do not uniquely
identify certificates. Consider using x509.fingerprint256 as a unique
identifier instead.
stringThe complete subject of this certificate.
stringThe subject alternative name specified for this certificate.
This is a comma-separated list of subject alternative names. Each entry begins with a string identifying the kind of the subject alternative name followed by a colon and the value associated with the entry.
Earlier versions of Node.js incorrectly assumed that it is safe to split this
property at the two-character sequence ', ' (see CVE-2021-44532). However,
both malicious and legitimate certificates can contain subject alternative names
that include this sequence when represented as a string.
After the prefix denoting the type of the entry, the remainder of each entry might be enclosed in quotes to indicate that the value is a JSON string literal. For backward compatibility, Node.js only uses JSON string literals within this property when necessary to avoid ambiguity. Third-party code should be prepared to handle both possible entry formats.
The subject distinguished name, also exposed as a formatted string by
x509.subject. The outer rdns array contains the relative distinguished
names in encoded order. Each inner array represents one relative distinguished
name and contains its attributes in encoded order. A valid empty name has an
empty rdns array.
rdns is a decoded view and does not include ASN.1 value types. Use raw for a
lossless representation of the complete name, including attribute values whose
value property is omitted from rdns.
Each access returns newly allocated objects, arrays, and a Buffer.
Modifying them does not affect the certificate or later accesses.
For example, attributes in the same inner array belong to one multi-valued relative distinguished name:
console.log(x509.subjectName.rdns); // [ // [ { oid: '2.5.4.6', value: 'US' } ], // [ // { oid: '2.5.4.11', value: 'Security' }, // { oid: '2.5.4.10', value: 'Example Corp' } // ], // [ { oid: '2.5.4.3', value: 'example.com' } ] // ]
Accessing this property throws ERR_CRYPTO_INVALID_X509_CERTIFICATE if the
subject name cannot be inspected.
BufferUnlike x509.raw, which contains the whole certificate, this property
contains only the complete encoded TBSCertificate: the exact bytes covered
by the certificate's signature. They are copied from the certificate encoding,
not reconstructed from decoded fields.
Each access returns a new Buffer. Modifying it does not affect the certificate
or later accesses.
Accessing this property throws ERR_CRYPTO_INVALID_X509_CERTIFICATE if the
TBSCertificate cannot be inspected.
x509.toJSON(): void
stringThere is no standard JSON encoding for X509 certificates. The
toJSON() method returns a string containing the PEM encoded
certificate.
x509.toLegacyObject(): void
ObjectReturns information about this certificate using the legacy certificate object encoding.
x509.toString(): void
stringReturns the PEM-encoded certificate.
stringThe date/time from which this certificate is valid.
If the certificate's notBefore time cannot be parsed, this is the string
'Bad time value'.
DateThe date/time from which this certificate is valid, encapsulated in a Date object.
If the certificate's notBefore time cannot be parsed, this property is an
invalid Date whose getTime() method returns NaN.
Relational comparisons with an invalid Date evaluate to false. To check
whether the current time is within the certificate's validity period, use
now >= x509.validFromDate && now <= x509.validToDate. This expression
evaluates to false if either date is invalid. Checking
now < x509.validFromDate || now > x509.validToDate instead can fail to
reject certificates with invalid dates.
stringThe date/time until which this certificate is valid.
If the certificate's notAfter time cannot be parsed, this is the string
'Bad time value'.
DateThe date/time until which this certificate is valid, encapsulated in a Date object.
If the certificate's notAfter time cannot be parsed, this property is an
invalid Date whose getTime() method returns NaN.
Relational comparisons with an invalid Date evaluate to false. To check
whether the current time is within the certificate's validity period, use
now >= x509.validFromDate && now <= x509.validToDate. This expression
evaluates to false if either date is invalid. Checking
now < x509.validFromDate || now > x509.validToDate instead can fail to
reject certificates with invalid dates.
algorithm.oid identifies the same certificate-level algorithm as
x509.signatureAlgorithmOid. In addition to the algorithm identification
provided by that property and x509.signatureAlgorithm, this property
exposes the algorithm parameters and signature bytes.
The parameters are DER-encoded independently; the complete encoded
AlgorithmIdentifier and BIT STRING wrappers are not returned. Accessing this
property does not verify the signature.
Each access returns new outer and algorithm objects, with new Buffers for
value and parameters when present. Modifying them does not affect the
certificate or later accesses.
For example, a certificate signed using RSA with SHA-256 can produce:
console.log(x509.signature); // { // algorithm: { // oid: '1.2.840.113549.1.1.11', // parameters: <Buffer 05 00> // }, // value: <Buffer ...> // }
Accessing this property throws ERR_CRYPTO_INVALID_X509_CERTIFICATE if the
signature cannot be inspected.
The algorithm used to sign the certificate or undefined if the signature algorithm is unknown by OpenSSL.
stringThe OID of the algorithm used to sign the certificate.
numberThe certificate version: 1 for X.509 v1, 2 for X.509 v2, or 3 for X.509
v3. A certificate with no encoded version field has version 1.
x509.verify(publicKey): boolean
Verifies that this certificate was signed by the given public key. Does not perform any other validation checks on the certificate.