* Encode and decode X.509 certificates.
* The extensions are from {@link http://tools.ietf.org/html/rfc5280 RFC5280} and
* {@link http://web.archive.org/web/19961027104704/http://www3.netscape.com/eng/security/cert-exts.html Netscape Certificate Extensions}.
* Note that loading an X.509 certificate and resaving it may invalidate the signature. The reason being that the signature is based on a
* portion of the certificate that contains optional parameters with default values. ie. if the parameter isn't there the default value is
* used. Problem is, if the parameter is there and it just so happens to have the default value there are two ways that that parameter can
* be encoded. It can be encoded explicitly or left out all together. This would effect the signature value and thus may invalidate the
* the certificate all together unless the certificate is re-signed.
* LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* @author Jim Wigginton <terrafrost@php.net>
* @copyright MMXII Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
if (!class_exists('File_ASN1')) {
require_once dirname(__FILE__).'/ASN1.php';
* Flag to only accept signatures signed by certificate authorities
* Not really used anymore but retained all the same to suppress E_NOTICEs from old installs
define('FILE_X509_VALIDATE_SIGNATURE_BY_CA', 1);
* @see File_X509::getDN()
* Return internal array representation
define('FILE_X509_DN_ARRAY', 0);
define('FILE_X509_DN_STRING', 1);
* Return ASN.1 name string
define('FILE_X509_DN_ASN1', 2);
* Return OpenSSL compatible array
define('FILE_X509_DN_OPENSSL', 3);
* Return canonical ASN.1 RDNs string
define('FILE_X509_DN_CANON', 4);
* Return name hash for file indexing
define('FILE_X509_DN_HASH', 5);
* @see File_X509::saveX509()
* @see File_X509::saveCSR()
* @see File_X509::saveCRL()
* ie. a base64-encoded PEM with a header and a footer
define('FILE_X509_FORMAT_PEM', 0);
define('FILE_X509_FORMAT_DER', 1);
* Only works on CSRs. Not currently supported.
define('FILE_X509_FORMAT_SPKAC', 2);
* Attribute value disposition.
* If disposition is >= 0, this is the index of the target value.
define('FILE_X509_ATTR_ALL', -1); // All attribute values (array).
define('FILE_X509_ATTR_APPEND', -2); // Add a value.
define('FILE_X509_ATTR_REPLACE', -3); // Clear first, then add a value.
* @author Jim Wigginton <terrafrost@php.net>
* ASN.1 syntax for X.509 certificates
* ASN.1 syntax for various extensions
public $ExtKeyUsageSyntax;
public $BasicConstraints;
public $CRLDistributionPoints;
public $AuthorityKeyIdentifier;
public $CertificatePolicies;
public $AuthorityInfoAccessSyntax;
public $PrivateKeyUsagePeriod;
public $netscape_cert_type;
public $netscape_comment;
public $netscape_ca_policy_url;
public $RelativeDistinguishedName;
public $IssuingDistributionPoint;
public $CertificateIssuer;
public $HoldInstructionCode;
public $SignedPublicKeyAndChallenge;
* ASN.1 syntax for Certificate Signing Requests (RFC2986)
public $CertificationRequest;
* ASN.1 syntax for Certificate Revocation Lists (RFC5280)
* Object identifiers for X.509 certificates
* @link http://en.wikipedia.org/wiki/Object_identifier
* The certificate authorities
* The currently loaded certificate
* There's no guarantee File_X509 is going to reencode an X.509 cert in the same way it was originally
* encoded so we take save the portion of the original cert that the signature would have made for.
public $signatureSubject;
* See {@link http://tools.ietf.org/html/rfc5280#section-4.2.1.1 RFC5280#section-4.2.1.1} and
* {@link http://tools.ietf.org/html/rfc5280#section-4.2.1.2 RFC5280#section-4.2.1.2}.
public $currentKeyIdentifier;
public function __construct()
if (!class_exists('Math_BigInteger')) {
require_once dirname(__FILE__).'/../Math/BigInteger.php';
// Explicitly Tagged Module, 1988 Syntax
// http://tools.ietf.org/html/rfc5280#appendix-A.1
$this->DirectoryString = array(
'type' => FILE_ASN1_TYPE_CHOICE,
'teletexString' => array('type' => FILE_ASN1_TYPE_TELETEX_STRING),
'printableString' => array('type' => FILE_ASN1_TYPE_PRINTABLE_STRING),
'universalString' => array('type' => FILE_ASN1_TYPE_UNIVERSAL_STRING),
'utf8String' => array('type' => FILE_ASN1_TYPE_UTF8_STRING),
'bmpString' => array('type' => FILE_ASN1_TYPE_BMP_STRING),
$this->PKCS9String = array(
'type' => FILE_ASN1_TYPE_CHOICE,
'ia5String' => array('type' => FILE_ASN1_TYPE_IA5_STRING),
'directoryString' => $this->DirectoryString,
$this->AttributeValue = array('type' => FILE_ASN1_TYPE_ANY);
$AttributeType = array('type' => FILE_ASN1_TYPE_OBJECT_IDENTIFIER);
$AttributeTypeAndValue = array(
'type' => FILE_ASN1_TYPE_SEQUENCE,
'type' => $AttributeType,
'value' => $this->AttributeValue,
In practice, RDNs containing multiple name-value pairs (called "multivalued RDNs") are rare,
but they can be useful at times when either there is no unique attribute in the entry or you
want to ensure that the entry's DN contains some useful identifying information.
- https://www.opends.org/wiki/page/DefinitionRelativeDistinguishedName
$this->RelativeDistinguishedName = array(
'type' => FILE_ASN1_TYPE_SET,
'children' => $AttributeTypeAndValue,
// http://tools.ietf.org/html/rfc5280#section-4.1.2.4
'type' => FILE_ASN1_TYPE_SEQUENCE,
// RDNSequence does not define a min or a max, which means it doesn't have one
'children' => $this->RelativeDistinguishedName,
'type' => FILE_ASN1_TYPE_CHOICE,
'rdnSequence' => $RDNSequence,
// http://tools.ietf.org/html/rfc5280#section-4.1.1.2
$AlgorithmIdentifier = array(
'type' => FILE_ASN1_TYPE_SEQUENCE,
'algorithm' => array('type' => FILE_ASN1_TYPE_OBJECT_IDENTIFIER),
'type' => FILE_ASN1_TYPE_ANY,
A certificate using system MUST reject the certificate if it encounters
a critical extension it does not recognize; however, a non-critical
extension may be ignored if it is not recognized.
http://tools.ietf.org/html/rfc5280#section-4.2
'type' => FILE_ASN1_TYPE_SEQUENCE,
'extnId' => array('type' => FILE_ASN1_TYPE_OBJECT_IDENTIFIER),
'type' => FILE_ASN1_TYPE_BOOLEAN,
'extnValue' => array('type' => FILE_ASN1_TYPE_OCTET_STRING),
$this->Extensions = array(
'type' => FILE_ASN1_TYPE_SEQUENCE,
// technically, it's MAX, but we'll assume anything < 0 is MAX
// if 'children' isn't an array then 'min' and 'max' must be defined
'children' => $Extension,
$SubjectPublicKeyInfo = array(
'type' => FILE_ASN1_TYPE_SEQUENCE,
'algorithm' => $AlgorithmIdentifier,
'subjectPublicKey' => array('type' => FILE_ASN1_TYPE_BIT_STRING),
$UniqueIdentifier = array('type' => FILE_ASN1_TYPE_BIT_STRING);
'type' => FILE_ASN1_TYPE_CHOICE,
'utcTime' => array('type' => FILE_ASN1_TYPE_UTC_TIME),
'generalTime' => array('type' => FILE_ASN1_TYPE_GENERALIZED_TIME),
// http://tools.ietf.org/html/rfc5280#section-4.1.2.5
'type' => FILE_ASN1_TYPE_SEQUENCE,
$CertificateSerialNumber = array('type' => FILE_ASN1_TYPE_INTEGER);
'type' => FILE_ASN1_TYPE_INTEGER,
'mapping' => array('v1', 'v2', 'v3'),
// assert($TBSCertificate['children']['signature'] == $Certificate['children']['signatureAlgorithm'])
'type' => FILE_ASN1_TYPE_SEQUENCE,
// technically, default implies optional, but we'll define it as being optional, none-the-less, just to
'serialNumber' => $CertificateSerialNumber,
'signature' => $AlgorithmIdentifier,
'subject' => $this->Name,
'subjectPublicKeyInfo' => $SubjectPublicKeyInfo,
// implicit means that the T in the TLV structure is to be rewritten, regardless of the type
'issuerUniqueID' => array(
'subjectUniqueID' => array(
// <http://tools.ietf.org/html/rfc2459#page-74> doesn't use the EXPLICIT keyword but if
// it's not IMPLICIT, it's EXPLICIT
$this->Certificate = array(
'type' => FILE_ASN1_TYPE_SEQUENCE,