| Methods |
public
|
__construct(string $cert)
Create a new X509Cert object.
Create a new X509Cert object.
Parameters
| $cert |
An X.509 certificate (public key) in PEM format.
|
Throws
|
#
|
public
|
getPem(): String
Get the certificate (public key) in PEM format.
Get the certificate (public key) in PEM format.
Returns
The certificate (public key) in PEM format.
|
#
|
public
|
getFingerprint(string $algorithm = 'sha256'): string
Get the fingerprint of the certificate: the hash of its DER encoding.
Get 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 |
The hash algorithm. Default: 'sha256'.
|
Returns
The fingerprint, as a lowercase hexadecimal string.
Throws
|
#
|
public
|
getIssuer(): array
Get the issuer information of the certificate.
Get the issuer information of the certificate.
Returns
The issuer information of the certificate.
|
#
|
public
|
getSubject(): array
Get the subject information of the certificate.
Get the subject information of the certificate.
Returns
The subject information of the certificate.
|
#
|
public
|
getDN(): string
Get the Distinguished Name of the certificate.
Get the Distinguished Name of the certificate.
Returns
The Distinguished Name of the certificate.
|
#
|
public
|
getValidFrom(string $dateFormat = 'ymdHise'): string|false
Get the UTC date from which the certificate is valid.
Get the UTC date from which the certificate is valid.
Parameters
| $dateFormat |
The format of the returned date.
Default: 'ymdHise'
|
Returns
The date from which the certificate is valid,
FALSE if $dateFormat is invalid.
|
#
|
public
|
getValidTo(string $dateFormat = 'ymdHise'): string|false
Get the UTC date to which the certificate is valid.
Get the UTC date to which the certificate is valid.
Parameters
| $dateFormat |
The format of the returned date.
Default: 'ymdHise'
|
Returns
The date to which the certificate is valid,
FALSE if $dateFormat is invalid.
|
#
|
public
|
isOutOfDate(): boolean
Indicates whether the certificate is out of date or not.
Indicates whether the certificate is out of date or not.
Returns
TRUE if embedded certificate is out of date,
FALSE otherwise.
|
#
|
public
|
canSign(): boolean
Indicates whether the key usage of the certificate allows it to sign
data.
Indicates whether the key usage of the certificate allows it to sign
data.
The "keyUsage" extension, when the certificate has one, must allow
"digitalSignature" or "nonRepudiation". A certificate without this
extension is not restricted.
Returns
TRUE if the certificate may sign data, FALSE otherwise.
|
#
|
public
|
isTrustedBy(string|array $caCertPaths): boolean
Check that the certificate is trusted: OpenSSL builds and verifies its
chain up to a certificate of the given CA files.
Check that the certificate is trusted: OpenSSL builds and verifies its
chain up to a certificate of the given CA files.
Every certificate of the given files is trusted, and the chain is built
from these certificates only. OpenSSL verifies the signature of each
link, the validity dates of every certificate of the chain at the
current date, that each issuer is a CA (basic constraints, key usage,
path length) and that the chain ends at a self-signed certificate of
the given files. 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 (in one file or in
several). 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.
Parameters
| $caCertPaths |
The path, or the list of paths, of PEM
files that hold the trusted CA certificates.
|
Returns
TRUE if the chain of the certificate has been verified,
FALSE otherwise.
Throws
|
#
|
public
|
isValidCA(string $caCert): boolean
Check the origin of the certificate.
Check the origin of the certificate.
Verify that the X.509 certificate has been issued by the given CA
certificate: OpenSSL verifies the signature of the certificate with the
public key of the CA certificate, the issuer name of the certificate
must be the subject name of the CA certificate, and the CA certificate
must be a CA ("CA:TRUE" basic constraint and, if it has a "keyUsage"
extension, "keyCertSign").
Only this link is checked: neither the validity dates nor the chain of
the CA certificate. Use isTrustedBy() to verify the whole chain.
NOTE: More than one CA certificate can give a positive result,
some CAs re-issue signing certificates after having only
changed the expiration dates.
Note that it also works with self-signed CA certificates. In this
case, pass the X.509 certificate itself to the function.
Parameters
| $caCert |
The certificate (in PEM format) corresponding to
the private key that is supposed to be used by the CA to sign the X.509
certificate.
|
Returns
TRUE if origin of the X.509 certificate has been
validated, FALSE otherwise.
Throws
|
#
|
public
static
|
pemToDer(string $pemCertificate): string|false
Convert PEM encoded X.509 certificate to DER encoding.
Convert PEM encoded X.509 certificate to DER encoding.
Parameters
| $pemCertificate |
PEM encoded X.509 certificate.
|
Returns
DER encoded X.509 certificate on success,
FALSE on failure.
|
#
|
public
static
|
derToPem(string $derCertificate): string|false
Convert DER encoded X.509 certificate to PEM encoding.
Convert DER encoded X.509 certificate to PEM encoding.
The base64 is written in lines of 64 characters (RFC 7468): the PEM is
the one OpenSSL writes for the same certificate.
Parameters
| $derCertificate |
DER encoded X.509 certificate.
|
Returns
PEM encoded X.509 certificate on success,
FALSE on failure.
|
#
|
public
|
getDer(): string
The DER encoding of the certificate.
The DER encoding of the certificate.
Returns
|
#
|
public
|
getSerialNumber(): string
The serial number, as unsigned big-endian bytes.
The serial number, as unsigned big-endian bytes.
Returns
|
#
|
public
|
getRawIssuer(): string
The DER encoding of the issuer name, as a CRL names its issuer.
The DER encoding of the issuer name, as a CRL names its issuer.
Returns
|
#
|
public
|
getRawSubject(): string
The DER encoding of the subject name.
The DER encoding of the subject name.
Returns
|
#
|
public
|
getExtension(string $oid): array|null
An extension, by its OID.
An extension, by its OID.
Parameters
| $oid |
The OID, in its dotted form.
|
Returns
'critical' (boolean) and 'value' (the DER of its
value, the content of its OCTET STRING), or NULL.
|
#
|
public
|
canSignCrls(): boolean
Indicates whether the key usage of the certificate, if it has one,
allows it to sign CRLs.
Indicates whether the key usage of the certificate, if it has one,
allows it to sign CRLs.
Returns
TRUE if it may sign CRLs.
|
#
|
public
|
isCA(): boolean
Indicates whether the certificate is a CA by its basic constraints.
Indicates whether the certificate is a CA by its basic constraints.
Returns
|
#
|
public
|
getTrustedChain(string|array $caCerts, array $untrusted = [], int|null $at = null): array|null
The chain of the certificate, from it to its root, built and verified
from the given CA certificates as OpenSSL does for isTrustedBy(), at
the current date or at another one; NULL if the certificate is not
trusted.
The chain of the certificate, from it to its root, built and verified
from the given CA certificates as OpenSSL does for isTrustedBy(), at
the current date or at another one; NULL if the certificate is not
trusted.
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
date of the verification;
- 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: the root must be
one of the CA certificates. Name constraints are not enforced: a CA
certificate that has them is rejected, where OpenSSL would enforce
them.
Parameters
| $caCerts |
The CA certificates: paths of PEM files,
or PEM contents, each of which may hold several certificates.
|
| $untrusted |
Certificates (X509Cert) the chain may be built
with, after the CA certificates, without being trusted: the
intermediate CAs a signature embeds.
|
| $at |
The UNIX time the certificates must be valid at;
NULL for the current time.
|
Returns
The chain (X509Cert), or NULL.
Throws
Exception |
If a CA certificate file cannot be read.
|
|
#
|
| Constants |
public
|
|
OID_KEY_USAGE = '2.5.29.15'
|
#
|
public
|
|
OID_BASIC_CONSTRAINTS = '2.5.29.19'
|
#
|
public
|
|
OID_NAME_CONSTRAINTS = '2.5.29.30'
|
#
|
public
|
|
OID_SUBJECT_KEY_ID = '2.5.29.14'
|
#
|
public
|
|
OID_AUTHORITY_KEY_ID = '2.5.29.35'
|
#
|
public
|
|
KEY_USAGE_DIGITAL_SIGNATURE = 0
The key usages, by their bit in the extension.
The key usages, by their bit in the extension.
|
#
|
public
|
|
KEY_USAGE_NON_REPUDIATION = 1
|
#
|
public
|
|
KEY_USAGE_KEY_CERT_SIGN = 5
|
#
|
public
|
|
KEY_USAGE_CRL_SIGN = 6
|
#
|
public
|
|
HANDLED_CRITICAL_EXTENSIONS = [
'2.16.840.1.113730.1.1',
self::OID_KEY_USAGE,
'2.5.29.17',
self::OID_BASIC_CONSTRAINTS,
'2.5.29.32',
'2.5.29.31',
'2.5.29.37',
'1.3.6.1.5.5.7.48.1.5',
'2.5.29.36',
'2.5.29.33',
'2.5.29.54',
]
The extensions whose criticality the verification of a chain accepts.
They are those OpenSSL handles (X509_supported_extension()), but name
constraints, which it enforces and this library does not: a CA
certificate that has them is refused (see getTrustedChain()), and the
extensions of IP addresses and AS numbers (RFC 3779) and of proxy
certificates, which it does not handle either.
The extensions whose criticality the verification of a chain accepts.
They are those OpenSSL handles (X509_supported_extension()), but name
constraints, which it enforces and this library does not: a CA
certificate that has them is refused (see getTrustedChain()), and the
extensions of IP addresses and AS numbers (RFC 3779) and of proxy
certificates, which it does not handle either.
|
#
|
public
|
|
SIGNATURE_KEY_TYPES = [
'1.2.840.113549.1.1.4' => ['rsa'],
'1.2.840.113549.1.1.5' => ['rsa'],
'1.2.840.113549.1.1.11' => ['rsa'],
'1.2.840.113549.1.1.12' => ['rsa'],
'1.2.840.113549.1.1.13' => ['rsa'],
'1.2.840.113549.1.1.14' => ['rsa'],
'1.2.840.113549.1.1.10' => ['rsa'],
'1.2.840.10045.4.1' => ['ec'],
'1.2.840.10045.4.3.1' => ['ec'],
'1.2.840.10045.4.3.2' => ['ec'],
'1.2.840.10045.4.3.3' => ['ec'],
'1.2.840.10045.4.3.4' => ['ec'],
'1.2.840.10040.4.3' => ['dsa'],
'2.16.840.1.101.3.4.3.1' => ['dsa'],
'2.16.840.1.101.3.4.3.2' => ['dsa'],
]
The key types that may sign with each signature algorithm. OpenSSL
takes a certificate whose signature algorithm does not suit the key of
a certificate as not issued by it.
The key types that may sign with each signature algorithm. OpenSSL
takes a certificate whose signature algorithm does not suit the key of
a certificate as not issued by it.
|
#
|
public
|
|
CANONICAL_STRING_TAGS = [0x0c, 0x1e, 0x1c, 0x13, 0x14, 0x16, 0x1a]
The string types OpenSSL compares names by in canonical form
(ASN1_MASK_CANON): UTF8String, BMPString, UniversalString,
PrintableString, T61String, IA5String, VisibleString.
The string types OpenSSL compares names by in canonical form
(ASN1_MASK_CANON): UTF8String, BMPString, UniversalString,
PrintableString, T61String, IA5String, VisibleString.
|
#
|
public
|
|
MAX_CHAIN_LENGTH = 100
The deepest chain OpenSSL builds by default.
The deepest chain OpenSSL builds by default.
|
#
|