php8.5
Home/ Manual/ openssl / functions/ openssl_x509_parse

openssl_x509_parse

PHP function Edit on GitHub ✎

(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

Parse an X509 certificate and return the information as an array

Description

openssl_x509_parse(OpenSSLCertificate|string $certificate, bool $short_names = true): array|false

openssl_x509_parse() returns information about the supplied certificate, including fields such as subject name, issuer name, purposes, valid from and valid to dates etc.

Parameters

certificate

X509 certificate. See Key/Certificate parameters for a list of valid values.

short_names

short_names controls how the data is indexed in the array - if short_names is true (the default) then fields will be indexed with the short name form, otherwise, the long name form will be used - e.g.: CN is the shortname form of commonName.

Return Values

Returns an Array of certificate fields on success, or false on failure.

alias is present only when the certificate has an alias. Every other key listed below is always present.

KeyTypeDescription
namestringSubject distinguished name as a single string, with components separated by slashes. For example, /C=US/O=Example/CN=example.com.
subjectarraySubject distinguished name. Keys are short names such as CN when short_names is true, and long names such as commonName otherwise. Each value is a UTF-8 String, or an Array of UTF-8 Strings when that component occurs more than once.
hashstringLowercase hexadecimal hash of the subject name, as used to look up a certificate in an OpenSSL CA directory. This is an 8-digit value. It is not the certificate fingerprint; see openssl_x509_fingerprint().
issuerarrayIssuer distinguished name, in the same form as subject.
versionintRaw version field stored in the certificate. This value is one less than the certificate version usually displayed: a version 1 certificate is 0, a version 2 certificate is 1, and a version 3 certificate is 2.
serialNumberstringDecimal representation of the serial number. Large serial numbers are returned as a hexadecimal string prefixed with 0x instead.
serialNumberHexstringUppercase hexadecimal representation of the serial number.
validFromstringNot-before time in ASN.1 form, either UTCTime (YYMMDDHHMMSSZ) or GeneralizedTime (YYYYMMDDHHMMSSZ).
validTostringNot-after time, in the same form as validFrom.
validFrom_time_tintNot-before time as a Unix timestamp, or -1 if the time could not be parsed.
validTo_time_tintNot-after time as a Unix timestamp, or -1 if the time could not be parsed.
aliasstringCertificate alias (friendly name). Only present when one is set.
signatureTypeSNstringShort name of the signature algorithm.
signatureTypeLNstringLong name of the signature algorithm.
signatureTypeNIDintNumeric signature-algorithm identifier (OpenSSL NID).
purposesarrayPurpose checks, indexed by purpose id. The ids are the X509_PURPOSE_* constants. Each value is an Array described below.
extensionsarrayCertificate extensions, indexed by short name (for example subjectAltName) or by OID when the extension has no short name. Each value is text describing that extension, or the raw extension data when it cannot be printed. Which keys are present depends on the certificate. This array does not follow short_names.

Each element of purposes has the following keys:

KeyTypeDescription
0booltrue if the certificate can be used for this purpose as an end-entity certificate.
1booltrue if the certificate can be used for this purpose as a CA certificate.
2stringPurpose name. A short name such as sslclient when short_names is true, and a long name such as SSL client otherwise.

Changelog

VersionDescription
8.4.0Parsing certificate with no seconds in UTCTime is no longer allowed for any OpenSSL version. It was already disallowed for OpenSSL version 3.3+.
8.0.0certificate accepts an OpenSSLCertificate instance now; previously, a Resource of type OpenSSL X.509 was accepted.

Source: reference/openssl/functions/openssl-x509-parse.xml · from the official PHP manual (php/doc-en)