PageRenderTime 66ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/vendors/phpseclib/File/X509.php

https://bitbucket.org/ttalov/fgcu_pci
PHP | 2582 lines | 1773 code | 245 blank | 564 comment | 152 complexity | f1a7938a31c368b2a98cd292c03d0a64 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Pure-PHP X.509 Parser
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * Encode and decode X.509 certificates.
  9. *
  10. * The extensions are from {@link http://tools.ietf.org/html/rfc5280 RFC5280} and
  11. * {@link http://web.archive.org/web/19961027104704/http://www3.netscape.com/eng/security/cert-exts.html Netscape Certificate Extensions}.
  12. *
  13. * Note that loading an X.509 certificate and resaving it may invalidate the signature. The reason being that the signature is based on a
  14. * portion of the certificate that contains optional parameters with default values. ie. if the parameter isn't there the default value is
  15. * 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
  16. * be encoded. It can be encoded explicitly or left out all together. This would effect the signature value and thus may invalidate the
  17. * the certificate all together unless the certificate is re-signed.
  18. *
  19. * LICENSE: Permission is hereby granted, free of charge, to any person obtaining a copy
  20. * of this software and associated documentation files (the "Software"), to deal
  21. * in the Software without restriction, including without limitation the rights
  22. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  23. * copies of the Software, and to permit persons to whom the Software is
  24. * furnished to do so, subject to the following conditions:
  25. *
  26. * The above copyright notice and this permission notice shall be included in
  27. * all copies or substantial portions of the Software.
  28. *
  29. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  30. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  32. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  33. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  34. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  35. * THE SOFTWARE.
  36. *
  37. * @category File
  38. * @package File_X509
  39. * @author Jim Wigginton <terrafrost@php.net>
  40. * @copyright MMXII Jim Wigginton
  41. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  42. * @version $Id$
  43. * @link htp://phpseclib.sourceforge.net
  44. */
  45. /**
  46. * Include File_ASN1
  47. */
  48. if (!class_exists('File_ASN1')) {
  49. require_once('File/ASN1.php');
  50. }
  51. /**
  52. * Flag to only accept signatures signed by certificate authorities
  53. *
  54. * @access public
  55. * @see File_X509::validateSignature()
  56. */
  57. define('FILE_X509_VALIDATE_SIGNATURE_BY_CA', 1);
  58. /**
  59. * Pure-PHP X.509 Parser
  60. *
  61. * @author Jim Wigginton <terrafrost@php.net>
  62. * @version 0.3.0
  63. * @access public
  64. * @package File_X509
  65. */
  66. class File_X509 {
  67. /**
  68. * ASN.1 syntax for X.509 certificates
  69. *
  70. * @var Array
  71. * @access private
  72. */
  73. var $Certificate;
  74. /**#@+
  75. * ASN.1 syntax for various extensions
  76. *
  77. * @access private
  78. */
  79. var $KeyUsage;
  80. var $ExtKeyUsageSyntax;
  81. var $BasicConstraints;
  82. var $KeyIdentifier;
  83. var $CRLDistributionPoints;
  84. var $AuthorityKeyIdentifier;
  85. var $CertificatePolicies;
  86. var $AuthorityInfoAccessSyntax;
  87. var $SubjectAltName;
  88. var $PrivateKeyUsagePeriod;
  89. var $IssuerAltName;
  90. var $PolicyMappings;
  91. var $NameConstraints;
  92. var $CPSuri;
  93. var $UserNotice;
  94. var $netscape_cert_type;
  95. var $netscape_comment;
  96. /**#@-*/
  97. /**
  98. * ASN.1 syntax for Certificate Signing Requests (RFC2986)
  99. *
  100. * @var Array
  101. * @access private
  102. */
  103. var $CertificationRequest;
  104. /**
  105. * Distinguished Name
  106. *
  107. * @var Array
  108. * @access private
  109. */
  110. var $dn;
  111. /**
  112. * Public key
  113. *
  114. * @var String
  115. * @access private
  116. */
  117. var $publicKey;
  118. /**
  119. * Private key
  120. *
  121. * @var String
  122. * @access private
  123. */
  124. var $privateKey;
  125. /**
  126. * Object identifiers for X.509 certificates
  127. *
  128. * @var Array
  129. * @access private
  130. * @link http://en.wikipedia.org/wiki/Object_identifier
  131. */
  132. var $oids;
  133. /**
  134. * The certificate authorities
  135. *
  136. * @var Array
  137. * @access private
  138. */
  139. var $CAs;
  140. /**
  141. * The currently loaded certificate
  142. *
  143. * @var Array
  144. * @access private
  145. */
  146. var $currentCert;
  147. /**
  148. * The signature subject
  149. *
  150. * There's no guarantee File_X509 is going to reencode an X.509 cert in the same way it was originally
  151. * encoded so we take save the portion of the original cert that the signature would have made for.
  152. *
  153. * @var String
  154. * @access private
  155. */
  156. var $signatureSubject;
  157. /**
  158. * Certificate Start Date
  159. *
  160. * @var String
  161. * @access private
  162. */
  163. var $startDate;
  164. /**
  165. * Certificate End Date
  166. *
  167. * @var String
  168. * @access private
  169. */
  170. var $endDate;
  171. /**
  172. * Serial Number
  173. *
  174. * @var String
  175. * @access private
  176. */
  177. var $serialNumber;
  178. /**
  179. * Key Identifier
  180. *
  181. * See {@link http://tools.ietf.org/html/rfc5280#section-4.2.1.1 RFC5280#section-4.2.1.1} and
  182. * {@link http://tools.ietf.org/html/rfc5280#section-4.2.1.2 RFC5280#section-4.2.1.2}.
  183. *
  184. * @var String
  185. * @access private
  186. */
  187. var $keyIdentifier;
  188. /**
  189. * CA Flag
  190. *
  191. * @var Boolean
  192. * @access private
  193. */
  194. var $caFlag = false;
  195. /**
  196. * Default Constructor.
  197. *
  198. * @return File_X509
  199. * @access public
  200. */
  201. function File_X509()
  202. {
  203. // Explicitly Tagged Module, 1988 Syntax
  204. // http://tools.ietf.org/html/rfc5280#appendix-A.1
  205. $temp = array('min' => 1, 'max' => -1);
  206. $DirectoryString = array(
  207. 'type' => FILE_ASN1_TYPE_CHOICE,
  208. 'children' => array(
  209. 'teletexString' => $temp + array('type' => FILE_ASN1_TYPE_TELETEX_STRING),
  210. 'printableString' => $temp + array('type' => FILE_ASN1_TYPE_PRINTABLE_STRING),
  211. 'universalString' => $temp + array('type' => FILE_ASN1_TYPE_UNIVERSAL_STRING),
  212. 'utf8String' => $temp + array('type' => FILE_ASN1_TYPE_UTF8_STRING),
  213. 'bmpString' => $temp + array('type' => FILE_ASN1_TYPE_BMP_STRING)
  214. )
  215. );
  216. $AttributeValue = array('type' => FILE_ASN1_TYPE_ANY);
  217. $AttributeType = array('type' => FILE_ASN1_TYPE_OBJECT_IDENTIFIER);
  218. $AttributeTypeAndValue = array(
  219. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  220. 'children' => array(
  221. 'type' => $AttributeType,
  222. 'value'=> $AttributeValue
  223. )
  224. );
  225. /*
  226. In practice, RDNs containing multiple name-value pairs (called "multivalued RDNs") are rare,
  227. but they can be useful at times when either there is no unique attribute in the entry or you
  228. want to ensure that the entry's DN contains some useful identifying information.
  229. - https://www.opends.org/wiki/page/DefinitionRelativeDistinguishedName
  230. */
  231. $RelativeDistinguishedName = array(
  232. 'type' => FILE_ASN1_TYPE_SET,
  233. 'min' => 1,
  234. 'max' => -1,
  235. 'children' => $AttributeTypeAndValue
  236. );
  237. // http://tools.ietf.org/html/rfc5280#section-4.1.2.4
  238. $RDNSequence = array(
  239. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  240. // RDNSequence does not define a min or a max, which means it doesn't have one
  241. 'min' => 0,
  242. 'max' => -1,
  243. 'children' => $RelativeDistinguishedName
  244. );
  245. $Name = array(
  246. 'type' => FILE_ASN1_TYPE_CHOICE,
  247. 'children' => array(
  248. 'rdnSequence' => $RDNSequence
  249. )
  250. );
  251. // http://tools.ietf.org/html/rfc5280#section-4.1.1.2
  252. $AlgorithmIdentifier = array(
  253. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  254. 'children' => array(
  255. 'algorithm' => array('type' => FILE_ASN1_TYPE_OBJECT_IDENTIFIER),
  256. 'parameters' => array(
  257. 'type' => FILE_ASN1_TYPE_ANY,
  258. 'optional' => true
  259. )
  260. )
  261. );
  262. /*
  263. A certificate using system MUST reject the certificate if it encounters
  264. a critical extension it does not recognize; however, a non-critical
  265. extension may be ignored if it is not recognized.
  266. http://tools.ietf.org/html/rfc5280#section-4.2
  267. */
  268. $Extension = array(
  269. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  270. 'children' => array(
  271. 'extnId' => array('type' => FILE_ASN1_TYPE_OBJECT_IDENTIFIER),
  272. 'critical' => array(
  273. 'type' => FILE_ASN1_TYPE_BOOLEAN,
  274. 'optional' => true,
  275. 'default' => false
  276. ),
  277. 'extnValue' => array('type' => FILE_ASN1_TYPE_OCTET_STRING)
  278. )
  279. );
  280. $Extensions = array(
  281. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  282. 'min' => 1,
  283. // technically, it's MAX, but we'll assume anything < 0 is MAX
  284. 'max' => -1,
  285. // if 'children' isn't an array then 'min' and 'max' must be defined
  286. 'children' => $Extension
  287. );
  288. $SubjectPublicKeyInfo = array(
  289. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  290. 'children' => array(
  291. 'algorithm' => $AlgorithmIdentifier,
  292. 'subjectPublicKey' => array('type' => FILE_ASN1_TYPE_BIT_STRING)
  293. )
  294. );
  295. $UniqueIdentifier = array('type' => FILE_ASN1_TYPE_BIT_STRING);
  296. $Time = array(
  297. 'type' => FILE_ASN1_TYPE_CHOICE,
  298. 'children' => array(
  299. 'utcTime' => array('type' => FILE_ASN1_TYPE_UTC_TIME),
  300. 'generalTime' => array('type' => FILE_ASN1_TYPE_GENERALIZED_TIME)
  301. )
  302. );
  303. // http://tools.ietf.org/html/rfc5280#section-4.1.2.5
  304. $Validity = array(
  305. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  306. 'children' => array(
  307. 'notBefore' => $Time,
  308. 'notAfter' => $Time
  309. )
  310. );
  311. $CertificateSerialNumber = array('type' => FILE_ASN1_TYPE_INTEGER);
  312. $Version = array(
  313. 'type' => FILE_ASN1_TYPE_INTEGER,
  314. 'mapping' => array('v1', 'v2', 'v3')
  315. );
  316. // assert($TBSCertificate['children']['signature'] == $Certificate['children']['signatureAlgorithm'])
  317. $TBSCertificate = array(
  318. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  319. 'children' => array(
  320. // technically, default implies optional, but we'll define it as being optional, none-the-less, just to
  321. // reenforce that fact
  322. 'version' => array(
  323. 'constant' => 0,
  324. 'optional' => true,
  325. 'explicit' => true,
  326. 'default' => 'v1'
  327. ) + $Version,
  328. 'serialNumber' => $CertificateSerialNumber,
  329. 'signature' => $AlgorithmIdentifier,
  330. 'issuer' => $Name,
  331. 'validity' => $Validity,
  332. 'subject' => $Name,
  333. 'subjectPublicKeyInfo' => $SubjectPublicKeyInfo,
  334. // implicit means that the T in the TLV structure is to be rewritten, regardless of the type
  335. 'issuerUniqueID' => array(
  336. 'constant' => 1,
  337. 'optional' => true,
  338. 'implicit' => true
  339. ) + $UniqueIdentifier,
  340. 'subjectUniqueID' => array(
  341. 'constant' => 2,
  342. 'optional' => true,
  343. 'implicit' => true
  344. ) + $UniqueIdentifier,
  345. // <http://tools.ietf.org/html/rfc2459#page-74> doesn't use the EXPLICIT keyword but if
  346. // it's not IMPLICIT, it's EXPLICIT
  347. 'extensions' => array(
  348. 'constant' => 3,
  349. 'optional' => true,
  350. 'explicit' => true
  351. ) + $Extensions
  352. )
  353. );
  354. $this->Certificate = array(
  355. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  356. 'children' => array(
  357. 'tbsCertificate' => $TBSCertificate,
  358. 'signatureAlgorithm' => $AlgorithmIdentifier,
  359. 'signature' => array('type' => FILE_ASN1_TYPE_BIT_STRING)
  360. )
  361. );
  362. $this->KeyUsage = array(
  363. 'type' => FILE_ASN1_TYPE_BIT_STRING,
  364. 'mapping' => array(
  365. 'digitalSignature',
  366. 'nonRepudiation',
  367. 'keyEncipherment',
  368. 'dataEncipherment',
  369. 'keyAgreement',
  370. 'keyCertSign',
  371. 'cRLSign',
  372. 'encipherOnly',
  373. 'decipherOnly'
  374. )
  375. );
  376. $this->BasicConstraints = array(
  377. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  378. 'children' => array(
  379. 'cA' => array(
  380. 'type' => FILE_ASN1_TYPE_BOOLEAN,
  381. 'optional' => true,
  382. 'default' => false
  383. ),
  384. 'pathLenConstraint' => array(
  385. 'type' => FILE_ASN1_TYPE_INTEGER,
  386. 'optional' => true
  387. )
  388. )
  389. );
  390. $this->KeyIdentifier = array('type' => FILE_ASN1_TYPE_OCTET_STRING);
  391. $OrganizationalUnitNames = array(
  392. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  393. 'min' => 1,
  394. 'max' => 4, // ub-organizational-units
  395. 'children' => array('type' => FILE_ASN1_TYPE_PRINTABLE_STRING)
  396. );
  397. $PersonalName = array(
  398. 'type' => FILE_ASN1_TYPE_SET,
  399. 'children' => array(
  400. 'surname' => array(
  401. 'type' => FILE_ASN1_TYPE_PRINTABLE_STRING,
  402. 'constant' => 0,
  403. 'optional' => true,
  404. 'implicit' => true
  405. ),
  406. 'given-name' => array(
  407. 'type' => FILE_ASN1_TYPE_PRINTABLE_STRING,
  408. 'constant' => 1,
  409. 'optional' => true,
  410. 'implicit' => true
  411. ),
  412. 'initials' => array(
  413. 'type' => FILE_ASN1_TYPE_PRINTABLE_STRING,
  414. 'constant' => 2,
  415. 'optional' => true,
  416. 'implicit' => true
  417. ),
  418. 'generation-qualifier' => array(
  419. 'type' => FILE_ASN1_TYPE_PRINTABLE_STRING,
  420. 'constant' => 3,
  421. 'optional' => true,
  422. 'implicit' => true
  423. )
  424. )
  425. );
  426. $NumericUserIdentifier = array('type' => FILE_ASN1_TYPE_NUMERIC_STRING);
  427. $OrganizationName = array('type' => FILE_ASN1_TYPE_PRINTABLE_STRING);
  428. $PrivateDomainName = array(
  429. 'type' => FILE_ASN1_TYPE_CHOICE,
  430. 'children' => array(
  431. 'numeric' => array('type' => FILE_ASN1_TYPE_NUMERIC_STRING),
  432. 'printable' => array('type' => FILE_ASN1_TYPE_PRINTABLE_STRING)
  433. )
  434. );
  435. $TerminalIdentifier = array('type' => FILE_ASN1_TYPE_PRINTABLE_STRING);
  436. $NetworkAddress = array('type' => FILE_ASN1_TYPE_NUMERIC_STRING);
  437. $AdministrationDomainName = array(
  438. 'type' => FILE_ASN1_TYPE_CHOICE,
  439. // if class isn't present it's assumed to be FILE_ASN1_CLASS_UNIVERSAL or
  440. // (if constant is present) FILE_ASN1_CLASS_CONTEXT_SPECIFIC
  441. 'class' => FILE_ASN1_CLASS_APPLICATION,
  442. 'cast' => 2,
  443. 'children' => array(
  444. 'numeric' => array('type' => FILE_ASN1_TYPE_NUMERIC_STRING),
  445. 'printable' => array('type' => FILE_ASN1_TYPE_PRINTABLE_STRING)
  446. )
  447. );
  448. $CountryName = array(
  449. 'type' => FILE_ASN1_TYPE_CHOICE,
  450. // if class isn't present it's assumed to be FILE_ASN1_CLASS_UNIVERSAL or
  451. // (if constant is present) FILE_ASN1_CLASS_CONTEXT_SPECIFIC
  452. 'class' => FILE_ASN1_CLASS_APPLICATION,
  453. 'cast' => 1,
  454. 'children' => array(
  455. 'x121-dcc-code' => array('type' => FILE_ASN1_TYPE_NUMERIC_STRING),
  456. 'iso-3166-alpha2-code' => array('type' => FILE_ASN1_TYPE_PRINTABLE_STRING)
  457. )
  458. );
  459. $AnotherName = array(
  460. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  461. 'children' => array(
  462. 'type-id' => array('type' => FILE_ASN1_TYPE_OBJECT_IDENTIFIER),
  463. 'value' => array(
  464. 'type' => FILE_ASN1_TYPE_ANY,
  465. 'constant' => 0,
  466. 'optional' => true,
  467. 'explicit' => true
  468. )
  469. )
  470. );
  471. $ExtensionAttribute = array(
  472. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  473. 'children' => array(
  474. 'extension-attribute-type' => array(
  475. 'type' => FILE_ASN1_TYPE_PRINTABLE_STRING,
  476. 'constant' => 0,
  477. 'optional' => true,
  478. 'implicit' => true
  479. ),
  480. 'extension-attribute-value' => array(
  481. 'type' => FILE_ASN1_TYPE_ANY,
  482. 'constant' => 1,
  483. 'optional' => true,
  484. 'explicit' => true
  485. )
  486. )
  487. );
  488. $ExtensionAttributes = array(
  489. 'type' => FILE_ASN1_TYPE_SET,
  490. 'min' => 1,
  491. 'max' => 256, // ub-extension-attributes
  492. 'children' => $ExtensionAttribute
  493. );
  494. $BuiltInDomainDefinedAttribute = array(
  495. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  496. 'children' => array(
  497. 'type' => array('type' => FILE_ASN1_TYPE_PRINTABLE_STRING),
  498. 'value' => array('type' => FILE_ASN1_TYPE_PRINTABLE_STRING)
  499. )
  500. );
  501. $BuiltInDomainDefinedAttributes = array(
  502. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  503. 'min' => 1,
  504. 'max' => 4, // ub-domain-defined-attributes
  505. 'children' => $BuiltInDomainDefinedAttribute
  506. );
  507. $BuiltInStandardAttributes = array(
  508. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  509. 'children' => array(
  510. 'country-name' => array('optional' => true) + $CountryName,
  511. 'administration-domain-name' => array('optional' => true) + $AdministrationDomainName,
  512. 'network-address' => array(
  513. 'constant' => 0,
  514. 'optional' => true,
  515. 'implicit' => true
  516. ) + $NetworkAddress,
  517. 'terminal-identifier' => array(
  518. 'constant' => 1,
  519. 'optional' => true,
  520. 'implicit' => true
  521. ) + $TerminalIdentifier,
  522. 'private-domain-name' => array(
  523. 'constant' => 2,
  524. 'optional' => true,
  525. 'explicit' => true
  526. ) + $PrivateDomainName,
  527. 'organization-name' => array(
  528. 'constant' => 3,
  529. 'optional' => true,
  530. 'implicit' => true
  531. ) + $OrganizationName,
  532. 'numeric-user-identifier' => array(
  533. 'constant' => 4,
  534. 'optional' => true,
  535. 'implicit' => true
  536. ) + $NumericUserIdentifier,
  537. 'personal-name' => array(
  538. 'constant' => 5,
  539. 'optional' => true,
  540. 'implicit' => true
  541. ) + $PersonalName,
  542. 'organizational-unit-names' => array(
  543. 'constant' => 6,
  544. 'optional' => true,
  545. 'implicit' => true
  546. ) + $OrganizationalUnitNames
  547. )
  548. );
  549. $ORAddress = array(
  550. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  551. 'children' => array(
  552. 'built-in-standard-attributes' => $BuiltInStandardAttributes,
  553. 'built-in-domain-defined-attributes' => array('optional' => true) + $BuiltInDomainDefinedAttributes,
  554. 'extension-attributes' => array('optional' => true) + $ExtensionAttributes
  555. )
  556. );
  557. $EDIPartyName = array(
  558. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  559. 'children' => array(
  560. 'nameAssigner' => array(
  561. 'constant' => 0,
  562. 'optional' => true,
  563. 'implicit' => true
  564. ) + $DirectoryString,
  565. // partyName is technically required but File_ASN1 doesn't currently support non-optional constants and
  566. // setting it to optional gets the job done in any event.
  567. 'partyName' => array(
  568. 'constant' => 1,
  569. 'optional' => true,
  570. 'implicit' => true
  571. ) + $DirectoryString
  572. )
  573. );
  574. $GeneralName = array(
  575. 'type' => FILE_ASN1_TYPE_CHOICE,
  576. 'children' => array(
  577. 'otherName' => array(
  578. 'constant' => 0,
  579. 'optional' => true,
  580. 'implicit' => true
  581. ) + $AnotherName,
  582. 'rfc822Name' => array(
  583. 'type' => FILE_ASN1_TYPE_IA5_STRING,
  584. 'constant' => 1,
  585. 'optional' => true,
  586. 'implicit' => true
  587. ),
  588. 'dNSName' => array(
  589. 'type' => FILE_ASN1_TYPE_IA5_STRING,
  590. 'constant' => 2,
  591. 'optional' => true,
  592. 'implicit' => true
  593. ),
  594. 'x400Address' => array(
  595. 'constant' => 3,
  596. 'optional' => true,
  597. 'implicit' => true
  598. ) + $ORAddress,
  599. 'directoryName' => array(
  600. 'constant' => 4,
  601. 'optional' => true,
  602. 'explicit' => true
  603. ) + $Name,
  604. 'ediPartyName' => array(
  605. 'constant' => 5,
  606. 'optional' => true,
  607. 'implicit' => true
  608. ) + $EDIPartyName,
  609. 'uniformResourceIdentifier' => array(
  610. 'type' => FILE_ASN1_TYPE_IA5_STRING,
  611. 'constant' => 6,
  612. 'optional' => true,
  613. 'implicit' => true
  614. ),
  615. 'iPAddress' => array(
  616. 'type' => FILE_ASN1_TYPE_OCTET_STRING,
  617. 'constant' => 7,
  618. 'optional' => true,
  619. 'implicit' => true
  620. ),
  621. 'registeredID' => array(
  622. 'type' => FILE_ASN1_TYPE_OBJECT_IDENTIFIER,
  623. 'constant' => 8,
  624. 'optional' => true,
  625. 'implicit' => true
  626. )
  627. )
  628. );
  629. $GeneralNames = array(
  630. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  631. 'min' => 1,
  632. 'max' => -1,
  633. 'children' => $GeneralName
  634. );
  635. $this->IssuerAltName = $GeneralNames;
  636. $ReasonFlags = array(
  637. 'type' => FILE_ASN1_TYPE_BIT_STRING,
  638. 'mapping' => array(
  639. 'unused',
  640. 'keyCompromise',
  641. 'cACompromise',
  642. 'affiliationChanged',
  643. 'superseded',
  644. 'cessationOfOperation',
  645. 'certificateHold',
  646. 'privilegeWithdrawn',
  647. 'aACompromise'
  648. )
  649. );
  650. $DistributionPointName = array(
  651. 'type' => FILE_ASN1_TYPE_CHOICE,
  652. 'children' => array(
  653. 'fullName' => array(
  654. 'constant' => 0,
  655. 'optional' => true,
  656. 'implicit' => true
  657. ) + $GeneralNames,
  658. 'nameRelativeToCRLIssuer' => array(
  659. 'constant' => 1,
  660. 'optional' => true,
  661. 'implicit' => true
  662. ) + $RelativeDistinguishedName
  663. )
  664. );
  665. $DistributionPoint = array(
  666. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  667. 'children' => array(
  668. 'distributionPoint' => array(
  669. 'constant' => 0,
  670. 'optional' => true,
  671. 'explicit' => true
  672. ) + $DistributionPointName,
  673. 'reasons' => array(
  674. 'constant' => 1,
  675. 'optional' => true,
  676. 'implicit' => true
  677. ) + $ReasonFlags,
  678. 'cRLIssuer' => array(
  679. 'constant' => 2,
  680. 'optional' => true,
  681. 'implicit' => true
  682. ) + $GeneralNames
  683. )
  684. );
  685. $this->CRLDistributionPoints = array(
  686. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  687. 'min' => 1,
  688. 'max' => -1,
  689. 'children' => $DistributionPoint
  690. );
  691. $this->AuthorityKeyIdentifier = array(
  692. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  693. 'children' => array(
  694. 'keyIdentifier' => array(
  695. 'constant' => 0,
  696. 'optional' => true,
  697. 'implicit' => true
  698. ) + $this->KeyIdentifier,
  699. 'authorityCertIssuer' => array(
  700. 'constant' => 1,
  701. 'optional' => true,
  702. 'implicit' => true
  703. ) + $GeneralNames,
  704. 'authorityCertSerialNumber' => array(
  705. 'constant' => 2,
  706. 'optional' => true,
  707. 'implicit' => true
  708. ) + $CertificateSerialNumber
  709. )
  710. );
  711. $PolicyQualifierId = array('type' => FILE_ASN1_TYPE_OBJECT_IDENTIFIER);
  712. $PolicyQualifierInfo = array(
  713. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  714. 'children' => array(
  715. 'policyQualifierId' => $PolicyQualifierId,
  716. 'qualifier' => array('type' => FILE_ASN1_TYPE_ANY)
  717. )
  718. );
  719. $CertPolicyId = array('type' => FILE_ASN1_TYPE_OBJECT_IDENTIFIER);
  720. $PolicyInformation = array(
  721. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  722. 'children' => array(
  723. 'policyIdentifier' => $CertPolicyId,
  724. 'policyQualifiers' => array(
  725. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  726. 'min' => 0,
  727. 'max' => -1,
  728. 'optional' => true,
  729. 'children' => $PolicyQualifierInfo
  730. )
  731. )
  732. );
  733. $this->CertificatePolicies = array(
  734. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  735. 'min' => 1,
  736. 'max' => -1,
  737. 'children' => $PolicyInformation
  738. );
  739. $this->PolicyMappings = array(
  740. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  741. 'min' => 1,
  742. 'max' => -1,
  743. 'children' => array(
  744. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  745. 'children' => array(
  746. 'issuerDomainPolicy' => $CertPolicyId,
  747. 'subjectDomainPolicy' => $CertPolicyId
  748. )
  749. )
  750. );
  751. $KeyPurposeId = array('type' => FILE_ASN1_TYPE_OBJECT_IDENTIFIER);
  752. $this->ExtKeyUsageSyntax = array(
  753. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  754. 'min' => 1,
  755. 'max' => -1,
  756. 'children' => $KeyPurposeId
  757. );
  758. $AccessDescription = array(
  759. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  760. 'children' => array(
  761. 'accessMethod' => array('type' => FILE_ASN1_TYPE_OBJECT_IDENTIFIER),
  762. 'accessLocation' => $GeneralName
  763. )
  764. );
  765. $this->AuthorityInfoAccessSyntax = array(
  766. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  767. 'min' => 1,
  768. 'max' => -1,
  769. 'children' => $AccessDescription
  770. );
  771. $this->SubjectAltName = $GeneralNames;
  772. $this->PrivateKeyUsagePeriod = array(
  773. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  774. 'children' => array(
  775. 'notBefore' => array(
  776. 'constant' => 0,
  777. 'optional' => true,
  778. 'implicit' => true,
  779. 'type' => FILE_ASN1_TYPE_GENERALIZED_TIME),
  780. 'notAfter' => array(
  781. 'constant' => 1,
  782. 'optional' => true,
  783. 'implicit' => true,
  784. 'type' => FILE_ASN1_TYPE_GENERALIZED_TIME)
  785. )
  786. );
  787. $BaseDistance = array('type' => FILE_ASN1_TYPE_INTEGER);
  788. $GeneralSubtree = array(
  789. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  790. 'children' => array(
  791. 'base' => $GeneralName,
  792. 'minimum' => array(
  793. 'constant' => 0,
  794. 'optional' => true,
  795. 'implicit' => true,
  796. 'default' => new Math_BigInteger(0)
  797. ) + $BaseDistance,
  798. 'maximum' => array(
  799. 'constant' => 1,
  800. 'optional' => true,
  801. 'implicit' => true,
  802. ) + $BaseDistance
  803. )
  804. );
  805. $GeneralSubtrees = array(
  806. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  807. 'min' => 1,
  808. 'max' => -1,
  809. 'children' => $GeneralSubtree
  810. );
  811. $this->NameConstraints = array(
  812. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  813. 'children' => array(
  814. 'permittedSubtrees' => array(
  815. 'constant' => 0,
  816. 'optional' => true,
  817. 'implicit' => true
  818. ) + $GeneralSubtrees,
  819. 'excludedSubtrees' => array(
  820. 'constant' => 1,
  821. 'optional' => true,
  822. 'implicit' => true
  823. ) + $GeneralSubtrees
  824. )
  825. );
  826. $this->CPSuri = array('type' => FILE_ASN1_TYPE_IA5_STRING);
  827. $DisplayText = array(
  828. 'type' => FILE_ASN1_TYPE_CHOICE,
  829. 'children' => array(
  830. 'ia5String' => array('type' => FILE_ASN1_TYPE_IA5_STRING),
  831. 'visibleString' => array('type' => FILE_ASN1_TYPE_VISIBLE_STRING),
  832. 'bmpString' => array('type' => FILE_ASN1_TYPE_BMP_STRING),
  833. 'utf8String' => array('type' => FILE_ASN1_TYPE_UTF8_STRING)
  834. )
  835. );
  836. $NoticeReference = array(
  837. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  838. 'children' => array(
  839. 'organization' => $DisplayText,
  840. 'noticeNumbers' => array(
  841. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  842. 'min' => 1,
  843. 'max' => 200,
  844. 'children' => array('type' => FILE_ASN1_TYPE_INTEGER)
  845. )
  846. )
  847. );
  848. $this->UserNotice = array(
  849. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  850. 'children' => array(
  851. 'noticeRef' => array(
  852. 'optional' => true,
  853. 'implicit' => true
  854. ) + $NoticeReference,
  855. 'explicitText' => array(
  856. 'optional' => true,
  857. 'implicit' => true
  858. ) + $DisplayText
  859. )
  860. );
  861. // mapping is from <http://www.mozilla.org/projects/security/pki/nss/tech-notes/tn3.html>
  862. $this->netscape_cert_type = array(
  863. 'type' => FILE_ASN1_TYPE_BIT_STRING,
  864. 'mapping' => array(
  865. 'SSLClient',
  866. 'SSLServer',
  867. 'Email',
  868. 'ObjectSigning',
  869. 'Reserved',
  870. 'SSLCA',
  871. 'EmailCA',
  872. 'ObjectSigningCA'
  873. )
  874. );
  875. $this->netscape_comment = array('type' => FILE_ASN1_TYPE_IA5_STRING);
  876. // attribute is used in RFC2986 but we're using the RFC5280 definition
  877. $Attribute = array(
  878. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  879. 'children' => array(
  880. 'type' => $AttributeType,
  881. 'value'=> array(
  882. 'type' => FILE_ASN1_TYPE_SET,
  883. 'min' => 1,
  884. 'max' => -1,
  885. 'children' => $AttributeValue
  886. )
  887. )
  888. );
  889. // adapted from <http://tools.ietf.org/html/rfc2986>
  890. $Attributes = array(
  891. 'type' => FILE_ASN1_TYPE_SET,
  892. 'min' => 1,
  893. 'max' => -1,
  894. 'children' => $Attribute
  895. );
  896. $CertificationRequestInfo = array(
  897. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  898. 'children' => array(
  899. 'version' => array(
  900. 'type' => FILE_ASN1_TYPE_INTEGER,
  901. 'mapping' => array('v1')
  902. ),
  903. 'subject' => $Name,
  904. 'subjectPKInfo' => $SubjectPublicKeyInfo,
  905. 'attributes' => array(
  906. 'constant' => 0,
  907. 'optional' => true,
  908. 'implicit' => true
  909. ) + $Attributes,
  910. )
  911. );
  912. $this->CertificationRequest = array(
  913. 'type' => FILE_ASN1_TYPE_SEQUENCE,
  914. 'children' => array(
  915. 'certificationRequestInfo' => $CertificationRequestInfo,
  916. 'signatureAlgorithm' => $AlgorithmIdentifier,
  917. 'signature' => array('type' => FILE_ASN1_TYPE_BIT_STRING)
  918. )
  919. );
  920. // OIDs from RFC5280 and those RFCs mentioned in RFC5280#section-4.1.1.2
  921. $this->oids = array(
  922. '1.3.6.1.5.5.7' => 'id-pkix',
  923. '1.3.6.1.5.5.7.1' => 'id-pe',
  924. '1.3.6.1.5.5.7.2' => 'id-qt',
  925. '1.3.6.1.5.5.7.3' => 'id-kp',
  926. '1.3.6.1.5.5.7.48' => 'id-ad',
  927. '1.3.6.1.5.5.7.2.1' => 'id-qt-cps',
  928. '1.3.6.1.5.5.7.2.2' => 'id-qt-unotice',
  929. '1.3.6.1.5.5.7.48.1' =>'id-ad-ocsp',
  930. '1.3.6.1.5.5.7.48.2' => 'id-ad-caIssuers',
  931. '1.3.6.1.5.5.7.48.3' => 'id-ad-timeStamping',
  932. '1.3.6.1.5.5.7.48.5' => 'id-ad-caRepository',
  933. '2.5.4' => 'id-at',
  934. '2.5.4.41' => 'id-at-name',
  935. '2.5.4.4' => 'id-at-surname',
  936. '2.5.4.42' => 'id-at-givenName',
  937. '2.5.4.43' => 'id-at-initials',
  938. '2.5.4.44' => 'id-at-generationQualifier',
  939. '2.5.4.3' => 'id-at-commonName',
  940. '2.5.4.7' => 'id-at-localityName',
  941. '2.5.4.8' => 'id-at-stateOrProvinceName',
  942. '2.5.4.10' => 'id-at-organizationName',
  943. '2.5.4.11' => 'id-at-organizationalUnitName',
  944. '2.5.4.12' => 'id-at-title',
  945. '2.5.4.46' => 'id-at-dnQualifier',
  946. '2.5.4.6' => 'id-at-countryName',
  947. '2.5.4.5' => 'id-at-serialNumber',
  948. '2.5.4.65' => 'id-at-pseudonym',
  949. '2.5.4.17' => 'id-at-postalCode',
  950. '2.5.4.9' => 'id-at-streetAddress',
  951. '0.9.2342.19200300.100.1.25' => 'id-domainComponent',
  952. '1.2.840.113549.1.9' => 'pkcs-9',
  953. '1.2.840.113549.1.9.1' => 'id-emailAddress',
  954. '2.5.29' => 'id-ce',
  955. '2.5.29.35' => 'id-ce-authorityKeyIdentifier',
  956. '2.5.29.14' => 'id-ce-subjectKeyIdentifier',
  957. '2.5.29.15' => 'id-ce-keyUsage',
  958. '2.5.29.16' => 'id-ce-privateKeyUsagePeriod',
  959. '2.5.29.32' => 'id-ce-certificatePolicies',
  960. '2.5.29.32.0' => 'anyPolicy',
  961. '2.5.29.33' => 'id-ce-policyMappings',
  962. '2.5.29.17' => 'id-ce-subjectAltName',
  963. '2.5.29.18' => 'id-ce-issuerAltName',
  964. '2.5.29.9' => 'id-ce-subjectDirectoryAttributes',
  965. '2.5.29.19' => 'id-ce-basicConstraints',
  966. '2.5.29.30' => 'id-ce-nameConstraints',
  967. '2.5.29.36' => 'id-ce-policyConstraints',
  968. '2.5.29.31' => 'id-ce-cRLDistributionPoints',
  969. '2.5.29.37' => 'id-ce-extKeyUsage',
  970. '2.5.29.37.0' => 'anyExtendedKeyUsage',
  971. '1.3.6.1.5.5.7.3.1' => 'id-kp-serverAuth',
  972. '1.3.6.1.5.5.7.3.2' => 'id-kp-clientAuth',
  973. '1.3.6.1.5.5.7.3.3' => 'id-kp-codeSigning',
  974. '1.3.6.1.5.5.7.3.4' => 'id-kp-emailProtection',
  975. '1.3.6.1.5.5.7.3.8' => 'id-kp-timeStamping',
  976. '1.3.6.1.5.5.7.3.9' => 'id-kp-OCSPSigning',
  977. '2.5.29.54' => 'id-ce-inhibitAnyPolicy',
  978. '2.5.29.46' => 'id-ce-freshestCRL',
  979. '1.3.6.1.5.5.7.1.1' => 'id-pe-authorityInfoAccess',
  980. '1.3.6.1.5.5.7.1.11' => 'id-pe-subjectInfoAccess',
  981. '2.5.29.20' => 'id-ce-cRLNumber',
  982. '2.5.29.28' => 'id-ce-issuingDistributionPoint',
  983. '2.5.29.27' => 'id-ce-deltaCRLIndicator',
  984. '2.5.29.21' => 'id-ce-cRLReasons',
  985. '2.5.29.29' => 'id-ce-certificateIssuer',
  986. '2.5.29.23' => 'id-ce-holdInstructionCode',
  987. '2.2.840.10040.2' => 'holdInstruction',
  988. '2.2.840.10040.2.1' => 'id-holdinstruction-none',
  989. '2.2.840.10040.2.2' => 'id-holdinstruction-callissuer',
  990. '2.2.840.10040.2.3' => 'id-holdinstruction-reject',
  991. '2.5.29.24' => 'id-ce-invalidityDate',
  992. '1.2.840.113549.2.2' => 'md2',
  993. '1.2.840.113549.2.5' => 'md5',
  994. '1.3.14.3.2.26' => 'id-sha1',
  995. '1.2.840.10040.4.1' => 'id-dsa',
  996. '1.2.840.10040.4.3' => 'id-dsa-with-sha1',
  997. '1.2.840.113549.1.1' => 'pkcs-1',
  998. '1.2.840.113549.1.1.1' => 'rsaEncryption',
  999. '1.2.840.113549.1.1.2' => 'md2WithRSAEncryption',
  1000. '1.2.840.113549.1.1.4' => 'md5WithRSAEncryption',
  1001. '1.2.840.113549.1.1.5' => 'sha1WithRSAEncryption',
  1002. '1.2.840.10046.2.1' => 'dhpublicnumber',
  1003. '2.16.840.1.101.2.1.1.22' => 'id-keyExchangeAlgorithm',
  1004. '1.2.840.10045' => 'ansi-X9-62',
  1005. '1.2.840.10045.4' => 'id-ecSigType',
  1006. '1.2.840.10045.4.1' => 'ecdsa-with-SHA1',
  1007. '1.2.840.10045.1' => 'id-fieldType',
  1008. '1.2.840.10045.1.1' => 'prime-field',
  1009. '1.2.840.10045.1.2' => 'characteristic-two-field',
  1010. '1.2.840.10045.1.2.3' => 'id-characteristic-two-basis',
  1011. '1.2.840.10045.1.2.3.1' => 'gnBasis',
  1012. '1.2.840.10045.1.2.3.2' => 'tpBasis',
  1013. '1.2.840.10045.1.2.3.3' => 'ppBasis',
  1014. '1.2.840.10045.2' => 'id-publicKeyType',
  1015. '1.2.840.10045.2.1' => 'id-ecPublicKey',
  1016. '1.2.840.10045.3' => 'ellipticCurve',
  1017. '1.2.840.10045.3.0' => 'c-TwoCurve',
  1018. '1.2.840.10045.3.0.1' => 'c2pnb163v1',
  1019. '1.2.840.10045.3.0.2' => 'c2pnb163v2',
  1020. '1.2.840.10045.3.0.3' => 'c2pnb163v3',
  1021. '1.2.840.10045.3.0.4' => 'c2pnb176w1',
  1022. '1.2.840.10045.3.0.5' => 'c2pnb191v1',
  1023. '1.2.840.10045.3.0.6' => 'c2pnb191v2',
  1024. '1.2.840.10045.3.0.7' => 'c2pnb191v3',
  1025. '1.2.840.10045.3.0.8' => 'c2pnb191v4',
  1026. '1.2.840.10045.3.0.9' => 'c2pnb191v5',
  1027. '1.2.840.10045.3.0.10' => 'c2pnb208w1',
  1028. '1.2.840.10045.3.0.11' => 'c2pnb239v1',
  1029. '1.2.840.10045.3.0.12' => 'c2pnb239v2',
  1030. '1.2.840.10045.3.0.13' => 'c2pnb239v3',
  1031. '1.2.840.10045.3.0.14' => 'c2pnb239v4',
  1032. '1.2.840.10045.3.0.15' => 'c2pnb239v5',
  1033. '1.2.840.10045.3.0.16' => 'c2pnb272w1',
  1034. '1.2.840.10045.3.0.17' => 'c2pnb304w1',
  1035. '1.2.840.10045.3.0.18' => 'c2pnb359v1',
  1036. '1.2.840.10045.3.0.19' => 'c2pnb368w1',
  1037. '1.2.840.10045.3.0.20' => 'c2pnb431r1',
  1038. '1.2.840.10045.3.1' => 'primeCurve',
  1039. '1.2.840.10045.3.1.1' => 'prime192v1',
  1040. '1.2.840.10045.3.1.2' => 'prime192v2',
  1041. '1.2.840.10045.3.1.3' => 'prime192v3',
  1042. '1.2.840.10045.3.1.4' => 'prime239v1',
  1043. '1.2.840.10045.3.1.5' => 'prime239v2',
  1044. '1.2.840.10045.3.1.6' => 'prime239v3',
  1045. '1.2.840.10045.3.1.7' => 'prime256v1',
  1046. '1.2.840.113549.1.1.7' => 'id-RSAES-OAEP',
  1047. '1.2.840.113549.1.1.9' => 'id-pSpecified',
  1048. '1.2.840.113549.1.1.10' => 'id-RSASSA-PSS',
  1049. '1.2.840.113549.1.1.8' => 'id-mgf1',
  1050. '1.2.840.113549.1.1.14' => 'sha224WithRSAEncryption',
  1051. '1.2.840.113549.1.1.11' => 'sha256WithRSAEncryption',
  1052. '1.2.840.113549.1.1.12' => 'sha384WithRSAEncryption',
  1053. '1.2.840.113549.1.1.13' => 'sha512WithRSAEncryption',
  1054. '2.16.840.1.101.3.4.2.4' => 'id-sha224',
  1055. '2.16.840.1.101.3.4.2.1' => 'id-sha256',
  1056. '2.16.840.1.101.3.4.2.2' => 'id-sha384',
  1057. '2.16.840.1.101.3.4.2.3' => 'id-sha512',
  1058. '1.2.643.2.2.4' => 'id-GostR3411-94-with-GostR3410-94',
  1059. '1.2.643.2.2.3' => 'id-GostR3411-94-with-GostR3410-2001',
  1060. '1.2.643.2.2.20' => 'id-GostR3410-2001',
  1061. '1.2.643.2.2.19' => 'id-GostR3410-94',
  1062. // Netscape Object Identifiers from "Netscape Certificate Extensions"
  1063. '2.16.840.1.113730' => 'netscape',
  1064. '2.16.840.1.113730.1' => 'netscape-cert-extension',
  1065. '2.16.840.1.113730.1.1' => 'netscape-cert-type',
  1066. '2.16.840.1.113730.1.13' => 'netscape-comment',
  1067. // the following are X.509 extensions not supported by phpseclib
  1068. '1.3.6.1.5.5.7.1.12' => 'id-pe-logotype',
  1069. '1.2.840.113533.7.65.0' => 'entrustVersInfo',
  1070. '2.16.840.1.113733.1.6.9' => 'verisignPrivate',
  1071. // for Certificate Signing Requests
  1072. // see http://tools.ietf.org/html/rfc2985
  1073. '1.2.840.113549.1.9.2' => 'unstructuredName', // PKCS #9 unstructured name
  1074. '1.2.840.113549.1.9.7' => 'challengePassword' // Challenge password for certificate revocations
  1075. );
  1076. }
  1077. /**
  1078. * Load X.509 certificate
  1079. *
  1080. * Returns an associative array describing the X.509 cert or a false if the cert failed to load
  1081. *
  1082. * @param String $cert
  1083. * @access public
  1084. * @return Mixed
  1085. */
  1086. function loadX509($cert)
  1087. {
  1088. if (is_array($cert) && isset($cert['tbsCertificate'])) {
  1089. $this->currentCert = $cert;
  1090. unset($this->signatureSubject);
  1091. return false;
  1092. }
  1093. $asn1 = new File_ASN1();
  1094. /*
  1095. X.509 certs are assumed to be base64 encoded but sometimes they'll have additional things in them abo…

Large files files are truncated, but you can click here to view the full file