ezxmldsig for Node.js - v2.0.0
    Preparing search index...

    Class X509Cert

    Analyzes an X.509 certificate: the counterpart of the class X509Cert of the PHP library.

    It extracts the information of the certificate (subject, issuer, validity dates, fingerprint) and makes the checks the analysis of a token needs: the validity at the current date, the key usage, and the chain of the certificate up to trusted CA certificates (isTrustedBy()), which follows the rules OpenSSL applies for the PHP library. The certificate is parsed by OpenSSL (node:crypto), which also verifies the signature of each link of a chain; the fields node:crypto does not expose are read from the DER encoding of the certificate.

    Index
    • Parameters

      • cert: string | Uint8Array<ArrayBufferLike>

        An X.509 certificate in PEM format (its content).

      Returns X509Cert

      If the certificate cannot be read.

    • Indicates whether the key usage of the certificate allows it to sign data: the key usage extension, when the certificate has one, must allow "digitalSignature" or "nonRepudiation". A certificate without this extension is not restricted.

      Returns boolean

    • Whether the key usage of the certificate, if it has one, allows it to sign CRLs.

      Returns boolean

    • The DER encoding of the certificate.

      Returns Buffer

    • Gets the Distinguished Name of the subject of the certificate, in the OpenSSL "oneline" format, as the PHP library gives it: every field, in the order of the certificate, written /<short name>=<value>, the fields of one RDN joined by +, with / and + escaped by a backslash and every byte out of printable ASCII written \xHH.

      Returns string

    • An extension, by its OID: its criticality and its value (the content of its OCTET STRING), or null.

      Parameters

      • oid: string

      Returns { critical: boolean; value: Buffer } | null

      • { critical: boolean; value: Buffer }
        • critical: boolean

          Whether the extension is critical.

        • value: Buffer

          The DER of its value, the content of its OCTET STRING.

      • null
    • Gets the fingerprint of the certificate: the hash of its DER encoding. Two certificates are the same certificate if, and only if, their fingerprints are equal.

      Parameters

      • algorithm: string = 'sha256'

        The hash algorithm. Default: 'sha256'.

      Returns string

      The fingerprint, as a lowercase hexadecimal string.

    • Gets the certificate in PEM format.

      Returns string

    • The public key of the certificate.

      Returns KeyObject

    • The DER encoding of the issuer name, as a CRL names its issuer.

      Returns Buffer

    • The DER encoding of the subject name.

      Returns Buffer

    • The serial number, as unsigned big-endian bytes.

      Returns Buffer

    • The chain isTrustedBy() verifies, from this certificate to its root, or null if the certificate is not trusted.

      Parameters

      Returns X509Cert[] | null

      If a CA certificate file cannot be read.

    • Gets the date from which the certificate is valid, in UTC (YYYY-MM-DDThh:mm:ssZ).

      Returns string

    • Gets the date until which the certificate is valid, in UTC (YYYY-MM-DDThh:mm:ssZ).

      Returns string

    • Whether the certificate is a CA by its basic constraints.

      Returns boolean

    • Indicates whether the certificate is out of date at the current date.

      Returns boolean

    • Checks that the certificate is trusted: its chain is built and verified up to a self-signed certificate of the given CA certificates, with the rules OpenSSL applies for the PHP library (openssl_x509_checkpurpose(), purpose "any").

      Every given certificate is trusted, and the chain is built from these certificates only (and the untrusted ones), never from those of the system. A certificate is taken as the issuer of another one when its subject is the issuer of the other one (names compared in the canonical form of OpenSSL), when the authority key identifier of the other one matches it, and when its key suits the signature algorithm of the other one. Then:

      • OpenSSL verifies the signature of each link (but the root's own);
      • every certificate of the chain, the root included, is valid at the current date (or at at);
      • every issuer is a CA: basic constraints with "CA:TRUE" and, if it has a key usage, "keyCertSign"; the root may also be a CA by its key usage alone, or by being self-signed and of version 1;
      • the path length of each CA is respected;
      • no certificate has a critical extension the verification does not handle, nor a repeated or malformed extension.

      A chain that stops at an intermediate CA is rejected: to trust the certificates issued by an intermediate CA, give its certificate and the certificates up to its root. A certificate issued by another intermediate CA of the same root is then rejected, since the chain cannot be built without that other intermediate certificate.

      Name constraints are not enforced: a CA certificate that has them is rejected, where OpenSSL would enforce them.

      Parameters

      • caCerts: PemSource | PemSource[]

        The CA certificates: paths of PEM files, or PEM contents, each of which may hold several certificates.

      • options: { at?: Date; untrusted?: readonly X509Cert[] } = {}

        untrusted: certificates the chain may be built with, after the CA certificates, without being trusted (the intermediate CAs a signature embeds): the root must still be one of the CA certificates. at: the date the certificates must be valid at, instead of the current date.

      Returns boolean

      True if the chain of the certificate has been verified.

      If a CA certificate file cannot be read.

    • Checks the origin of the certificate: it was issued by the given CA certificate. The CA certificate must be a CA ("CA:TRUE" basic constraint and, if it has a key usage, "keyCertSign"), the issuer of the certificate must have the fields of the subject of the CA certificate, and OpenSSL must verify the signature of the certificate with the key of the CA certificate.

      Only this link is checked: neither the validity dates nor the chain of the CA certificate. Use isTrustedBy() to verify the whole chain.

      Parameters

      • caCert: string | Uint8Array<ArrayBufferLike>

        The CA certificate, in PEM format (its content).

      Returns boolean

      If the CA certificate cannot be read.

    • Converts a DER encoded X.509 certificate to its PEM encoding, with lines of 64 base64 characters (RFC 7468, as OpenSSL writes them).

      Parameters

      • derCertificate: unknown

        DER encoded X.509 certificate.

      Returns string | null

      The PEM encoded certificate, or null if the parameter is not bytes.

    • Converts a PEM encoded X.509 certificate to its DER encoding: the base64 content of its first CERTIFICATE block, decoded. Characters out of the base64 alphabet are ignored, as by the PHP library.

      Parameters

      • pemCertificate: unknown

        PEM encoded X.509 certificate.

      Returns Buffer<ArrayBufferLike> | null

      The DER encoded certificate, or null if the parameter is not a string or holds no -----BEGIN CERTIFICATE----- line.