| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | namespace webcoder31\ezxmldsig\core; |
| 41: | |
| 42: | |
| 43: | use DateTime; |
| 44: | use DateTimeZone; |
| 45: | use Exception; |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | |
| 60: | class X509Cert |
| 61: | { |
| 62: | |
| 63: | private $cert; |
| 64: | |
| 65: | |
| 66: | private $x509Info; |
| 67: | |
| 68: | |
| 69: | private $der = null; |
| 70: | |
| 71: | |
| 72: | const OID_KEY_USAGE = '2.5.29.15'; |
| 73: | const OID_BASIC_CONSTRAINTS = '2.5.29.19'; |
| 74: | const OID_NAME_CONSTRAINTS = '2.5.29.30'; |
| 75: | const OID_SUBJECT_KEY_ID = '2.5.29.14'; |
| 76: | const OID_AUTHORITY_KEY_ID = '2.5.29.35'; |
| 77: | |
| 78: | |
| 79: | const KEY_USAGE_DIGITAL_SIGNATURE = 0; |
| 80: | const KEY_USAGE_NON_REPUDIATION = 1; |
| 81: | const KEY_USAGE_KEY_CERT_SIGN = 5; |
| 82: | const KEY_USAGE_CRL_SIGN = 6; |
| 83: | |
| 84: | |
| 85: | |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | const HANDLED_CRITICAL_EXTENSIONS = [ |
| 93: | '2.16.840.1.113730.1.1', |
| 94: | self::OID_KEY_USAGE, |
| 95: | '2.5.29.17', |
| 96: | self::OID_BASIC_CONSTRAINTS, |
| 97: | '2.5.29.32', |
| 98: | '2.5.29.31', |
| 99: | '2.5.29.37', |
| 100: | '1.3.6.1.5.5.7.48.1.5', |
| 101: | '2.5.29.36', |
| 102: | '2.5.29.33', |
| 103: | '2.5.29.54', |
| 104: | ]; |
| 105: | |
| 106: | |
| 107: | |
| 108: | |
| 109: | |
| 110: | |
| 111: | const SIGNATURE_KEY_TYPES = [ |
| 112: | '1.2.840.113549.1.1.4' => ['rsa'], |
| 113: | '1.2.840.113549.1.1.5' => ['rsa'], |
| 114: | '1.2.840.113549.1.1.11' => ['rsa'], |
| 115: | '1.2.840.113549.1.1.12' => ['rsa'], |
| 116: | '1.2.840.113549.1.1.13' => ['rsa'], |
| 117: | '1.2.840.113549.1.1.14' => ['rsa'], |
| 118: | '1.2.840.113549.1.1.10' => ['rsa'], |
| 119: | '1.2.840.10045.4.1' => ['ec'], |
| 120: | '1.2.840.10045.4.3.1' => ['ec'], |
| 121: | '1.2.840.10045.4.3.2' => ['ec'], |
| 122: | '1.2.840.10045.4.3.3' => ['ec'], |
| 123: | '1.2.840.10045.4.3.4' => ['ec'], |
| 124: | '1.2.840.10040.4.3' => ['dsa'], |
| 125: | '2.16.840.1.101.3.4.3.1' => ['dsa'], |
| 126: | '2.16.840.1.101.3.4.3.2' => ['dsa'], |
| 127: | ]; |
| 128: | |
| 129: | |
| 130: | |
| 131: | |
| 132: | const CANONICAL_STRING_TAGS = [0x0c, 0x1e, 0x1c, 0x13, 0x14, 0x16, 0x1a]; |
| 133: | |
| 134: | |
| 135: | const MAX_CHAIN_LENGTH = 100; |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | public function __construct($cert) |
| 146: | { |
| 147: | |
| 148: | if (!is_string($cert) || false === @openssl_x509_read($cert)) |
| 149: | { |
| 150: | throw new Exception("Invalid certificate!"); |
| 151: | } |
| 152: | |
| 153: | |
| 154: | $x509Info = openssl_x509_parse($cert); |
| 155: | if ($x509Info === false) |
| 156: | { |
| 157: | $msg = "Failed to extract X.509 information from certificate!"; |
| 158: | throw new Exception($msg); |
| 159: | } |
| 160: | |
| 161: | |
| 162: | $this->cert = $cert; |
| 163: | |
| 164: | |
| 165: | $this->x509Info = $x509Info; |
| 166: | } |
| 167: | |
| 168: | |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | public function getPem() |
| 175: | { |
| 176: | return $this->cert; |
| 177: | } |
| 178: | |
| 179: | |
| 180: | |
| 181: | |
| 182: | |
| 183: | |
| 184: | |
| 185: | |
| 186: | |
| 187: | |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | public function getFingerprint($algorithm = 'sha256') |
| 193: | { |
| 194: | $fingerprint = openssl_x509_fingerprint($this->cert, $algorithm); |
| 195: | if (false === $fingerprint) |
| 196: | { |
| 197: | throw new Exception("Failed to compute the fingerprint of the certificate!"); |
| 198: | } |
| 199: | |
| 200: | return $fingerprint; |
| 201: | } |
| 202: | |
| 203: | |
| 204: | |
| 205: | |
| 206: | |
| 207: | |
| 208: | |
| 209: | public function getIssuer() |
| 210: | { |
| 211: | return $this->x509Info['issuer']; |
| 212: | } |
| 213: | |
| 214: | |
| 215: | |
| 216: | |
| 217: | |
| 218: | |
| 219: | |
| 220: | public function getSubject() |
| 221: | { |
| 222: | return $this->x509Info['subject']; |
| 223: | } |
| 224: | |
| 225: | |
| 226: | |
| 227: | |
| 228: | |
| 229: | |
| 230: | |
| 231: | public function getDN() |
| 232: | { |
| 233: | return $this->x509Info['name']; |
| 234: | } |
| 235: | |
| 236: | |
| 237: | |
| 238: | |
| 239: | |
| 240: | |
| 241: | |
| 242: | |
| 243: | |
| 244: | |
| 245: | |
| 246: | public function getValidFrom($dateFormat = 'ymdHise') |
| 247: | { |
| 248: | return $this->_date('validFrom_time_t')->format($dateFormat); |
| 249: | } |
| 250: | |
| 251: | |
| 252: | |
| 253: | |
| 254: | |
| 255: | |
| 256: | |
| 257: | |
| 258: | |
| 259: | |
| 260: | |
| 261: | public function getValidTo($dateFormat = 'ymdHise') |
| 262: | { |
| 263: | return $this->_date('validTo_time_t')->format($dateFormat); |
| 264: | } |
| 265: | |
| 266: | |
| 267: | |
| 268: | |
| 269: | |
| 270: | |
| 271: | |
| 272: | |
| 273: | |
| 274: | |
| 275: | |
| 276: | |
| 277: | |
| 278: | |
| 279: | |
| 280: | |
| 281: | private function _date($field) |
| 282: | { |
| 283: | $date = new DateTime('@' . (int) $this->x509Info[$field]); |
| 284: | $date->setTimezone(new DateTimeZone('UTC')); |
| 285: | |
| 286: | return $date; |
| 287: | } |
| 288: | |
| 289: | |
| 290: | |
| 291: | |
| 292: | |
| 293: | |
| 294: | |
| 295: | |
| 296: | public function isOutOfDate() |
| 297: | { |
| 298: | |
| 299: | $now = time(); |
| 300: | |
| 301: | return $now < $this->_date('validFrom_time_t')->getTimestamp() |
| 302: | || $now > $this->_date('validTo_time_t')->getTimestamp(); |
| 303: | } |
| 304: | |
| 305: | |
| 306: | |
| 307: | |
| 308: | |
| 309: | |
| 310: | |
| 311: | |
| 312: | |
| 313: | |
| 314: | |
| 315: | |
| 316: | public function canSign() |
| 317: | { |
| 318: | if (!isset($this->x509Info['extensions']['keyUsage'])) |
| 319: | { |
| 320: | return true; |
| 321: | } |
| 322: | |
| 323: | $keyUsage = array_map('trim', explode(',', $this->x509Info['extensions']['keyUsage'])); |
| 324: | |
| 325: | return in_array('Digital Signature', $keyUsage, true) |
| 326: | || in_array('Non Repudiation', $keyUsage, true); |
| 327: | } |
| 328: | |
| 329: | |
| 330: | |
| 331: | |
| 332: | |
| 333: | |
| 334: | |
| 335: | |
| 336: | |
| 337: | |
| 338: | |
| 339: | |
| 340: | |
| 341: | |
| 342: | |
| 343: | |
| 344: | |
| 345: | |
| 346: | |
| 347: | |
| 348: | |
| 349: | |
| 350: | |
| 351: | |
| 352: | |
| 353: | |
| 354: | public function isTrustedBy($caCertPaths) |
| 355: | { |
| 356: | $caCertPaths = (array) $caCertPaths; |
| 357: | if (empty($caCertPaths)) |
| 358: | { |
| 359: | throw new Exception("No CA certificate file to verify the certificate chain!"); |
| 360: | } |
| 361: | foreach ($caCertPaths as $caCertPath) |
| 362: | { |
| 363: | if (!is_string($caCertPath) || !is_file($caCertPath) || !is_readable($caCertPath)) |
| 364: | { |
| 365: | throw new Exception("Cannot read CA certificate file! File: " . (is_string($caCertPath) ? $caCertPath : gettype($caCertPath))); |
| 366: | } |
| 367: | } |
| 368: | |
| 369: | |
| 370: | |
| 371: | |
| 372: | |
| 373: | |
| 374: | |
| 375: | |
| 376: | |
| 377: | $emptyDir = self::createEmptyDirectory(); |
| 378: | try |
| 379: | { |
| 380: | |
| 381: | |
| 382: | |
| 383: | |
| 384: | $caInfo = array_merge(array_values($caCertPaths), [$emptyDir]); |
| 385: | return true === openssl_x509_checkpurpose($this->cert, X509_PURPOSE_ANY, $caInfo); |
| 386: | } |
| 387: | finally |
| 388: | { |
| 389: | @rmdir($emptyDir); |
| 390: | } |
| 391: | } |
| 392: | |
| 393: | |
| 394: | |
| 395: | |
| 396: | |
| 397: | |
| 398: | |
| 399: | |
| 400: | |
| 401: | |
| 402: | static private function createEmptyDirectory() |
| 403: | { |
| 404: | |
| 405: | |
| 406: | $path = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . 'ezxmldsig-ca-' . bin2hex(random_bytes(16)); |
| 407: | if (!@mkdir($path, 0700)) |
| 408: | { |
| 409: | throw new Exception("Cannot create an empty directory to verify the certificate chain in " . sys_get_temp_dir() . "!"); |
| 410: | } |
| 411: | |
| 412: | return $path; |
| 413: | } |
| 414: | |
| 415: | |
| 416: | |
| 417: | |
| 418: | |
| 419: | |
| 420: | |
| 421: | |
| 422: | |
| 423: | |
| 424: | |
| 425: | |
| 426: | |
| 427: | |
| 428: | |
| 429: | |
| 430: | |
| 431: | |
| 432: | |
| 433: | |
| 434: | |
| 435: | |
| 436: | |
| 437: | |
| 438: | |
| 439: | |
| 440: | |
| 441: | |
| 442: | |
| 443: | |
| 444: | public function isValidCA($caCert) |
| 445: | { |
| 446: | if (!is_string($caCert) || false === @openssl_x509_read($caCert)) |
| 447: | { |
| 448: | throw new Exception("Invalid CA certificate!"); |
| 449: | } |
| 450: | |
| 451: | $caInfo = openssl_x509_parse($caCert); |
| 452: | if ($caInfo === false) |
| 453: | { |
| 454: | throw new Exception("Failed to extract X.509 information from CA certificate!"); |
| 455: | } |
| 456: | |
| 457: | |
| 458: | $basicConstraints = isset($caInfo['extensions']['basicConstraints']) ? $caInfo['extensions']['basicConstraints'] : ''; |
| 459: | if (!in_array('CA:TRUE', array_map('trim', explode(',', $basicConstraints)), true)) |
| 460: | { |
| 461: | return false; |
| 462: | } |
| 463: | if (isset($caInfo['extensions']['keyUsage'])) |
| 464: | { |
| 465: | $keyUsage = array_map('trim', explode(',', $caInfo['extensions']['keyUsage'])); |
| 466: | if (!in_array('Certificate Sign', $keyUsage, true)) |
| 467: | { |
| 468: | return false; |
| 469: | } |
| 470: | } |
| 471: | |
| 472: | |
| 473: | if ($this->x509Info['issuer'] !== $caInfo['subject']) |
| 474: | { |
| 475: | return false; |
| 476: | } |
| 477: | |
| 478: | |
| 479: | |
| 480: | |
| 481: | return 1 === openssl_x509_verify($this->cert, $caCert); |
| 482: | } |
| 483: | |
| 484: | |
| 485: | |
| 486: | |
| 487: | |
| 488: | |
| 489: | |
| 490: | |
| 491: | |
| 492: | |
| 493: | static public function pemToDer($pemCertificate) |
| 494: | { |
| 495: | if (!is_string($pemCertificate)) |
| 496: | { |
| 497: | |
| 498: | return false; |
| 499: | } |
| 500: | |
| 501: | $pattern = '/(-----((BEGIN)|(END)) CERTIFICATE-----)/'; |
| 502: | $base64Content = preg_split($pattern, $pemCertificate); |
| 503: | if (!isset($base64Content[1])) |
| 504: | { |
| 505: | |
| 506: | return false; |
| 507: | } |
| 508: | |
| 509: | |
| 510: | return base64_decode($base64Content[1]); |
| 511: | } |
| 512: | |
| 513: | |
| 514: | |
| 515: | |
| 516: | |
| 517: | |
| 518: | |
| 519: | |
| 520: | |
| 521: | |
| 522: | |
| 523: | |
| 524: | |
| 525: | static public function derToPem($derCertificate) |
| 526: | { |
| 527: | if (!is_string($derCertificate)) |
| 528: | { |
| 529: | |
| 530: | return false; |
| 531: | } |
| 532: | |
| 533: | $b64DerCert = base64_encode($derCertificate); |
| 534: | $pem = "-----BEGIN CERTIFICATE-----\n"; |
| 535: | |
| 536: | |
| 537: | $pem .= implode("\n", str_split($b64DerCert, 64)) . "\n"; |
| 538: | $pem .= "-----END CERTIFICATE-----\n"; |
| 539: | |
| 540: | return $pem; |
| 541: | } |
| 542: | |
| 543: | |
| 544: | |
| 545: | |
| 546: | |
| 547: | |
| 548: | |
| 549: | |
| 550: | |
| 551: | |
| 552: | |
| 553: | |
| 554: | |
| 555: | |
| 556: | |
| 557: | |
| 558: | |
| 559: | |
| 560: | public static function matchesName($name, $expected) |
| 561: | { |
| 562: | foreach ($expected as $key => $value) |
| 563: | { |
| 564: | if (!isset($name[$key]) || $name[$key] !== $value) |
| 565: | { |
| 566: | return false; |
| 567: | } |
| 568: | } |
| 569: | |
| 570: | return true; |
| 571: | } |
| 572: | |
| 573: | |
| 574: | |
| 575: | |
| 576: | |
| 577: | |
| 578: | |
| 579: | public function getDer() |
| 580: | { |
| 581: | return self::pemToDer($this->cert); |
| 582: | } |
| 583: | |
| 584: | |
| 585: | |
| 586: | |
| 587: | |
| 588: | |
| 589: | |
| 590: | public function getSerialNumber() |
| 591: | { |
| 592: | $serial = $this->der()['serial']; |
| 593: | |
| 594: | return strlen($serial) > 1 && "\0" === $serial[0] ? substr($serial, 1) : $serial; |
| 595: | } |
| 596: | |
| 597: | |
| 598: | |
| 599: | |
| 600: | |
| 601: | |
| 602: | |
| 603: | public function getRawIssuer() |
| 604: | { |
| 605: | return $this->der()['rawIssuer']; |
| 606: | } |
| 607: | |
| 608: | |
| 609: | |
| 610: | |
| 611: | |
| 612: | |
| 613: | |
| 614: | public function getRawSubject() |
| 615: | { |
| 616: | return $this->der()['rawSubject']; |
| 617: | } |
| 618: | |
| 619: | |
| 620: | |
| 621: | |
| 622: | |
| 623: | |
| 624: | |
| 625: | |
| 626: | |
| 627: | |
| 628: | public function getExtension($oid) |
| 629: | { |
| 630: | $extensions = $this->der()['extensions']; |
| 631: | |
| 632: | return isset($extensions[$oid]) ? $extensions[$oid] : null; |
| 633: | } |
| 634: | |
| 635: | |
| 636: | |
| 637: | |
| 638: | |
| 639: | |
| 640: | |
| 641: | |
| 642: | public function canSignCrls() |
| 643: | { |
| 644: | $keyUsage = $this->der()['keyUsage']; |
| 645: | |
| 646: | return null === $keyUsage || !empty($keyUsage[self::KEY_USAGE_CRL_SIGN]); |
| 647: | } |
| 648: | |
| 649: | |
| 650: | |
| 651: | |
| 652: | |
| 653: | |
| 654: | |
| 655: | public function isCA() |
| 656: | { |
| 657: | $basicConstraints = $this->der()['basicConstraints']; |
| 658: | |
| 659: | return null !== $basicConstraints && $basicConstraints['ca']; |
| 660: | } |
| 661: | |
| 662: | |
| 663: | |
| 664: | |
| 665: | |
| 666: | |
| 667: | |
| 668: | |
| 669: | |
| 670: | |
| 671: | |
| 672: | |
| 673: | |
| 674: | |
| 675: | |
| 676: | |
| 677: | |
| 678: | |
| 679: | |
| 680: | |
| 681: | |
| 682: | |
| 683: | |
| 684: | |
| 685: | |
| 686: | |
| 687: | |
| 688: | |
| 689: | |
| 690: | |
| 691: | |
| 692: | |
| 693: | |
| 694: | |
| 695: | |
| 696: | |
| 697: | |
| 698: | |
| 699: | |
| 700: | |
| 701: | |
| 702: | |
| 703: | |
| 704: | |
| 705: | |
| 706: | public function getTrustedChain($caCerts, array $untrusted = [], $at = null) |
| 707: | { |
| 708: | $sources = (array) $caCerts; |
| 709: | if (empty($sources)) |
| 710: | { |
| 711: | throw new Exception("No CA certificate file to verify the certificate chain!"); |
| 712: | } |
| 713: | $trusted = []; |
| 714: | foreach ($sources as $source) |
| 715: | { |
| 716: | $pem = self::readPemSource($source); |
| 717: | if (null === $pem) |
| 718: | { |
| 719: | throw new Exception("Cannot read CA certificate file! File: " . (is_string($source) ? $source : gettype($source))); |
| 720: | } |
| 721: | |
| 722: | if (!preg_match_all('/-----BEGIN CERTIFICATE-----.*?-----END CERTIFICATE-----/s', $pem, $blocks)) |
| 723: | { |
| 724: | return null; |
| 725: | } |
| 726: | foreach ($blocks[0] as $block) |
| 727: | { |
| 728: | try |
| 729: | { |
| 730: | $trusted[] = new X509Cert($block); |
| 731: | } |
| 732: | catch (Exception $e) |
| 733: | { |
| 734: | return null; |
| 735: | } |
| 736: | } |
| 737: | } |
| 738: | |
| 739: | $time = null === $at ? time() : (int) floor($at); |
| 740: | $chain = $this->buildChain($trusted, $untrusted, $time); |
| 741: | |
| 742: | return null !== $chain && self::verifyChain($chain, $time) ? $chain : null; |
| 743: | } |
| 744: | |
| 745: | |
| 746: | |
| 747: | |
| 748: | |
| 749: | |
| 750: | |
| 751: | private static function readPemSource($source) |
| 752: | { |
| 753: | if (!is_string($source)) |
| 754: | { |
| 755: | return null; |
| 756: | } |
| 757: | if (false !== strpos($source, '-----BEGIN')) |
| 758: | { |
| 759: | return $source; |
| 760: | } |
| 761: | if (!is_file($source) || !is_readable($source)) |
| 762: | { |
| 763: | return null; |
| 764: | } |
| 765: | $content = file_get_contents($source); |
| 766: | |
| 767: | return false === $content ? null : $content; |
| 768: | } |
| 769: | |
| 770: | |
| 771: | |
| 772: | |
| 773: | |
| 774: | |
| 775: | |
| 776: | |
| 777: | |
| 778: | |
| 779: | |
| 780: | private function buildChain(array $trusted, array $untrusted, $time) |
| 781: | { |
| 782: | $known = array_merge($trusted, $untrusted); |
| 783: | $chain = [$this]; |
| 784: | for (;;) |
| 785: | { |
| 786: | $last = $chain[count($chain) - 1]; |
| 787: | if ($last->isSelfSigned()) |
| 788: | { |
| 789: | foreach ($trusted as $cert) |
| 790: | { |
| 791: | if ($cert->getDer() === $last->getDer()) |
| 792: | { |
| 793: | return $chain; |
| 794: | } |
| 795: | } |
| 796: | return null; |
| 797: | } |
| 798: | if (count($chain) >= self::MAX_CHAIN_LENGTH) |
| 799: | { |
| 800: | return null; |
| 801: | } |
| 802: | $issuer = null; |
| 803: | $lastCandidate = null; |
| 804: | foreach ($known as $cert) |
| 805: | { |
| 806: | if ($last->mayBeIssuedBy($cert)) |
| 807: | { |
| 808: | $lastCandidate = $cert; |
| 809: | if (null === $issuer && $cert->isValidAt($time)) |
| 810: | { |
| 811: | $issuer = $cert; |
| 812: | } |
| 813: | } |
| 814: | } |
| 815: | $issuer = null !== $issuer ? $issuer : $lastCandidate; |
| 816: | if (null === $issuer) |
| 817: | { |
| 818: | return null; |
| 819: | } |
| 820: | $chain[] = $issuer; |
| 821: | } |
| 822: | } |
| 823: | |
| 824: | |
| 825: | |
| 826: | |
| 827: | |
| 828: | |
| 829: | private static function verifyChain(array $chain, $time) |
| 830: | { |
| 831: | $pathLength = 0; |
| 832: | foreach ($chain as $i => $cert) |
| 833: | { |
| 834: | if (!$cert->isValidAt($time)) |
| 835: | { |
| 836: | return false; |
| 837: | } |
| 838: | |
| 839: | |
| 840: | if (isset($chain[$i + 1]) && 1 !== openssl_x509_verify($cert->cert, $chain[$i + 1]->cert)) |
| 841: | { |
| 842: | return false; |
| 843: | } |
| 844: | |
| 845: | $der = $cert->der(); |
| 846: | if ($der['invalidExtension']) |
| 847: | { |
| 848: | return false; |
| 849: | } |
| 850: | foreach ($der['extensions'] as $oid => $extension) |
| 851: | { |
| 852: | if ($extension['critical'] && !in_array($oid, self::HANDLED_CRITICAL_EXTENSIONS, true)) |
| 853: | { |
| 854: | return false; |
| 855: | } |
| 856: | } |
| 857: | if (0 === $i) |
| 858: | { |
| 859: | continue; |
| 860: | } |
| 861: | |
| 862: | if (isset($der['extensions'][self::OID_NAME_CONSTRAINTS])) |
| 863: | { |
| 864: | return false; |
| 865: | } |
| 866: | |
| 867: | |
| 868: | $kind = $cert->caKind(); |
| 869: | if (0 === $kind || (isset($chain[$i + 1]) && 1 !== $kind)) |
| 870: | { |
| 871: | return false; |
| 872: | } |
| 873: | |
| 874: | |
| 875: | $limit = null === $der['basicConstraints'] ? null : $der['basicConstraints']['pathLength']; |
| 876: | if ($i > 1 && null !== $limit && $pathLength > $limit) |
| 877: | { |
| 878: | return false; |
| 879: | } |
| 880: | if (!$cert->isSelfIssued()) |
| 881: | { |
| 882: | $pathLength++; |
| 883: | } |
| 884: | } |
| 885: | |
| 886: | return true; |
| 887: | } |
| 888: | |
| 889: | |
| 890: | |
| 891: | |
| 892: | |
| 893: | |
| 894: | |
| 895: | |
| 896: | |
| 897: | |
| 898: | private function mayBeIssuedBy(X509Cert $issuer) |
| 899: | { |
| 900: | $der = $this->der(); |
| 901: | $issuerDer = $issuer->der(); |
| 902: | if ($der['invalidExtension'] || $issuerDer['invalidExtension']) |
| 903: | { |
| 904: | return false; |
| 905: | } |
| 906: | if (!self::sameName($der['issuer'], $issuerDer['subject'])) |
| 907: | { |
| 908: | return false; |
| 909: | } |
| 910: | $akid = $der['authorityKeyId']; |
| 911: | if (null !== $akid) |
| 912: | { |
| 913: | if (null !== $akid['keyId'] && null !== $issuerDer['subjectKeyId'] && $akid['keyId'] !== $issuerDer['subjectKeyId']) |
| 914: | { |
| 915: | return false; |
| 916: | } |
| 917: | if (null !== $akid['serial'] && $akid['serial'] !== $issuerDer['serial']) |
| 918: | { |
| 919: | return false; |
| 920: | } |
| 921: | if (null !== $akid['issuer'] && !self::sameName($akid['issuer'], $issuerDer['issuer'])) |
| 922: | { |
| 923: | return false; |
| 924: | } |
| 925: | } |
| 926: | $keyTypes = isset(self::SIGNATURE_KEY_TYPES[$der['signatureAlgorithm']]) ? self::SIGNATURE_KEY_TYPES[$der['signatureAlgorithm']] : []; |
| 927: | |
| 928: | return in_array($issuer->keyType(), $keyTypes, true); |
| 929: | } |
| 930: | |
| 931: | |
| 932: | |
| 933: | |
| 934: | |
| 935: | |
| 936: | private function isSelfSigned() |
| 937: | { |
| 938: | return $this->mayBeIssuedBy($this); |
| 939: | } |
| 940: | |
| 941: | |
| 942: | |
| 943: | |
| 944: | |
| 945: | |
| 946: | private function isSelfIssued() |
| 947: | { |
| 948: | $der = $this->der(); |
| 949: | |
| 950: | return self::sameName($der['issuer'], $der['subject']); |
| 951: | } |
| 952: | |
| 953: | |
| 954: | |
| 955: | |
| 956: | |
| 957: | |
| 958: | private function isValidAt($time) |
| 959: | { |
| 960: | return $time >= (int) $this->x509Info['validFrom_time_t'] && $time < (int) $this->x509Info['validTo_time_t']; |
| 961: | } |
| 962: | |
| 963: | |
| 964: | |
| 965: | |
| 966: | |
| 967: | |
| 968: | |
| 969: | private function caKind() |
| 970: | { |
| 971: | $der = $this->der(); |
| 972: | if (null !== $der['keyUsage'] && empty($der['keyUsage'][self::KEY_USAGE_KEY_CERT_SIGN])) |
| 973: | { |
| 974: | return 0; |
| 975: | } |
| 976: | if (null !== $der['basicConstraints']) |
| 977: | { |
| 978: | return $der['basicConstraints']['ca'] ? 1 : 0; |
| 979: | } |
| 980: | if (1 === $der['version'] && $this->isSelfSigned()) |
| 981: | { |
| 982: | return 3; |
| 983: | } |
| 984: | |
| 985: | return null !== $der['keyUsage'] ? 4 : 0; |
| 986: | } |
| 987: | |
| 988: | |
| 989: | |
| 990: | |
| 991: | |
| 992: | |
| 993: | private function keyType() |
| 994: | { |
| 995: | $key = @openssl_pkey_get_public($this->cert); |
| 996: | $details = false === $key ? false : @openssl_pkey_get_details($key); |
| 997: | if (false === $details) |
| 998: | { |
| 999: | return null; |
| 1000: | } |
| 1001: | switch ($details['type']) |
| 1002: | { |
| 1003: | case OPENSSL_KEYTYPE_RSA: |
| 1004: | return 'rsa'; |
| 1005: | case OPENSSL_KEYTYPE_EC: |
| 1006: | return 'ec'; |
| 1007: | case OPENSSL_KEYTYPE_DSA: |
| 1008: | return 'dsa'; |
| 1009: | default: |
| 1010: | return null; |
| 1011: | } |
| 1012: | } |
| 1013: | |
| 1014: | |
| 1015: | |
| 1016: | |
| 1017: | |
| 1018: | |
| 1019: | |
| 1020: | |
| 1021: | |
| 1022: | |
| 1023: | |
| 1024: | |
| 1025: | |
| 1026: | |
| 1027: | private function der() |
| 1028: | { |
| 1029: | if (null !== $this->der) |
| 1030: | { |
| 1031: | return $this->der; |
| 1032: | } |
| 1033: | try |
| 1034: | { |
| 1035: | $tbs = Der::childrenOf(Der::childrenOf(Der::readElement($this->getDer()), Der::SEQUENCE)[0], Der::SEQUENCE); |
| 1036: | $version = 1; |
| 1037: | if (isset($tbs[0]) && 0xa0 === $tbs[0]['tag']) |
| 1038: | { |
| 1039: | $version = Der::decodeSmallInteger(Der::readElement(array_shift($tbs)['content'])) + 1; |
| 1040: | } |
| 1041: | if (count($tbs) < 6 || Der::INTEGER !== $tbs[0]['tag']) |
| 1042: | { |
| 1043: | throw new Exception('DER: malformed certificate.'); |
| 1044: | } |
| 1045: | $info = [ |
| 1046: | 'version' => $version, |
| 1047: | 'serial' => $tbs[0]['content'], |
| 1048: | 'signatureAlgorithm' => Der::decodeOid(Der::childrenOf($tbs[1], Der::SEQUENCE)[0]), |
| 1049: | 'rawIssuer' => Der::reencode($tbs[2]), |
| 1050: | 'issuer' => self::readName($tbs[2]), |
| 1051: | 'rawSubject' => Der::reencode($tbs[4]), |
| 1052: | 'subject' => self::readName($tbs[4]), |
| 1053: | ]; |
| 1054: | $extensionsElement = null; |
| 1055: | foreach (array_slice($tbs, 6) as $element) |
| 1056: | { |
| 1057: | if (0xa3 === $element['tag']) |
| 1058: | { |
| 1059: | $extensionsElement = $element; |
| 1060: | } |
| 1061: | } |
| 1062: | list($info['extensions'], $info['invalidExtension']) = self::readExtensions($extensionsElement); |
| 1063: | } |
| 1064: | catch (Exception $e) |
| 1065: | { |
| 1066: | throw new Exception("Failed to extract X.509 information from certificate!"); |
| 1067: | } |
| 1068: | |
| 1069: | $info += ['keyUsage' => null, 'basicConstraints' => null, 'subjectKeyId' => null, 'authorityKeyId' => null]; |
| 1070: | $extensions = $info['extensions']; |
| 1071: | try |
| 1072: | { |
| 1073: | if (isset($extensions[self::OID_KEY_USAGE])) |
| 1074: | { |
| 1075: | $info['keyUsage'] = Der::decodeBits(Der::readElement($extensions[self::OID_KEY_USAGE]['value'])); |
| 1076: | } |
| 1077: | if (isset($extensions[self::OID_BASIC_CONSTRAINTS])) |
| 1078: | { |
| 1079: | $info['basicConstraints'] = self::readBasicConstraints($extensions[self::OID_BASIC_CONSTRAINTS]['value']); |
| 1080: | |
| 1081: | if (null !== $info['basicConstraints']['pathLength'] && !$info['basicConstraints']['ca']) |
| 1082: | { |
| 1083: | $info['invalidExtension'] = true; |
| 1084: | } |
| 1085: | } |
| 1086: | if (isset($extensions[self::OID_SUBJECT_KEY_ID])) |
| 1087: | { |
| 1088: | $element = Der::readElement($extensions[self::OID_SUBJECT_KEY_ID]['value']); |
| 1089: | if (Der::OCTET_STRING !== $element['tag']) |
| 1090: | { |
| 1091: | throw new Exception('DER: malformed subject key identifier.'); |
| 1092: | } |
| 1093: | $info['subjectKeyId'] = $element['content']; |
| 1094: | } |
| 1095: | if (isset($extensions[self::OID_AUTHORITY_KEY_ID])) |
| 1096: | { |
| 1097: | $info['authorityKeyId'] = self::readAuthorityKeyId($extensions[self::OID_AUTHORITY_KEY_ID]['value']); |
| 1098: | } |
| 1099: | } |
| 1100: | catch (Exception $e) |
| 1101: | { |
| 1102: | $info['invalidExtension'] = true; |
| 1103: | } |
| 1104: | |
| 1105: | return $this->der = $info; |
| 1106: | } |
| 1107: | |
| 1108: | |
| 1109: | |
| 1110: | |
| 1111: | |
| 1112: | |
| 1113: | private static function readExtensions($element) |
| 1114: | { |
| 1115: | $extensions = []; |
| 1116: | $invalid = false; |
| 1117: | if (null === $element) |
| 1118: | { |
| 1119: | return [$extensions, $invalid]; |
| 1120: | } |
| 1121: | foreach (Der::childrenOf(Der::readElement($element['content']), Der::SEQUENCE) as $extension) |
| 1122: | { |
| 1123: | $fields = Der::childrenOf($extension, Der::SEQUENCE); |
| 1124: | if (count($fields) < 2 || count($fields) > 3 || Der::OCTET_STRING !== $fields[count($fields) - 1]['tag']) |
| 1125: | { |
| 1126: | throw new Exception('DER: malformed extension.'); |
| 1127: | } |
| 1128: | $oid = Der::decodeOid($fields[0]); |
| 1129: | if (isset($extensions[$oid])) |
| 1130: | { |
| 1131: | $invalid = true; |
| 1132: | } |
| 1133: | $extensions[$oid] = [ |
| 1134: | 'critical' => 3 === count($fields) ? Der::decodeBoolean($fields[1]) : false, |
| 1135: | 'value' => $fields[count($fields) - 1]['content'], |
| 1136: | ]; |
| 1137: | } |
| 1138: | |
| 1139: | return [$extensions, $invalid]; |
| 1140: | } |
| 1141: | |
| 1142: | |
| 1143: | |
| 1144: | |
| 1145: | |
| 1146: | private static function readBasicConstraints($value) |
| 1147: | { |
| 1148: | $ca = false; |
| 1149: | $pathLength = null; |
| 1150: | foreach (Der::childrenOf(Der::readElement($value), Der::SEQUENCE) as $field) |
| 1151: | { |
| 1152: | if (Der::BOOLEAN === $field['tag'] && null === $pathLength) |
| 1153: | { |
| 1154: | $ca = Der::decodeBoolean($field); |
| 1155: | } |
| 1156: | else |
| 1157: | { |
| 1158: | $pathLength = Der::decodeSmallInteger($field); |
| 1159: | } |
| 1160: | } |
| 1161: | |
| 1162: | return ['ca' => $ca, 'pathLength' => $pathLength]; |
| 1163: | } |
| 1164: | |
| 1165: | |
| 1166: | |
| 1167: | |
| 1168: | |
| 1169: | |
| 1170: | |
| 1171: | private static function readAuthorityKeyId($value) |
| 1172: | { |
| 1173: | $akid = ['keyId' => null, 'issuer' => null, 'serial' => null]; |
| 1174: | foreach (Der::childrenOf(Der::readElement($value), Der::SEQUENCE) as $field) |
| 1175: | { |
| 1176: | if (0x80 === $field['tag']) |
| 1177: | { |
| 1178: | $akid['keyId'] = $field['content']; |
| 1179: | } |
| 1180: | elseif (0xa1 === $field['tag']) |
| 1181: | { |
| 1182: | foreach (Der::readElements($field['content']) as $name) |
| 1183: | { |
| 1184: | if (0xa4 === $name['tag']) |
| 1185: | { |
| 1186: | $akid['issuer'] = self::readName(Der::readElement($name['content'])); |
| 1187: | break; |
| 1188: | } |
| 1189: | } |
| 1190: | } |
| 1191: | elseif (0x82 === $field['tag']) |
| 1192: | { |
| 1193: | $akid['serial'] = $field['content']; |
| 1194: | } |
| 1195: | else |
| 1196: | { |
| 1197: | throw new Exception('DER: malformed authority key identifier.'); |
| 1198: | } |
| 1199: | } |
| 1200: | |
| 1201: | return $akid; |
| 1202: | } |
| 1203: | |
| 1204: | |
| 1205: | |
| 1206: | |
| 1207: | |
| 1208: | |
| 1209: | |
| 1210: | |
| 1211: | |
| 1212: | |
| 1213: | |
| 1214: | private static function readName($element) |
| 1215: | { |
| 1216: | $rdns = []; |
| 1217: | foreach (Der::childrenOf($element, Der::SEQUENCE) as $set => $rdn) |
| 1218: | { |
| 1219: | foreach (Der::childrenOf($rdn, Der::SET) as $attribute) |
| 1220: | { |
| 1221: | $parts = Der::childrenOf($attribute, Der::SEQUENCE); |
| 1222: | if (2 !== count($parts)) |
| 1223: | { |
| 1224: | throw new Exception('DER: malformed name.'); |
| 1225: | } |
| 1226: | $oid = Der::decodeOid($parts[0]); |
| 1227: | $value = $parts[1]; |
| 1228: | if (in_array($value['tag'], self::CANONICAL_STRING_TAGS, true)) |
| 1229: | { |
| 1230: | $utf8 = self::toUtf8($value); |
| 1231: | if (null === $utf8) |
| 1232: | { |
| 1233: | return null; |
| 1234: | } |
| 1235: | $text = 's:' . bin2hex(self::canonicalString($utf8)); |
| 1236: | } |
| 1237: | else |
| 1238: | { |
| 1239: | $text = $value['tag'] . ':' . bin2hex($value['content']); |
| 1240: | } |
| 1241: | $rdns[$set][] = $oid . '=' . $text; |
| 1242: | } |
| 1243: | } |
| 1244: | $canonical = []; |
| 1245: | foreach ($rdns as $rdn) |
| 1246: | { |
| 1247: | sort($rdn, SORT_STRING); |
| 1248: | $canonical[] = implode('+', $rdn); |
| 1249: | } |
| 1250: | |
| 1251: | return implode('/', $canonical); |
| 1252: | } |
| 1253: | |
| 1254: | |
| 1255: | |
| 1256: | |
| 1257: | |
| 1258: | |
| 1259: | private static function sameName($a, $b) |
| 1260: | { |
| 1261: | return null !== $a && $a === $b; |
| 1262: | } |
| 1263: | |
| 1264: | |
| 1265: | |
| 1266: | |
| 1267: | |
| 1268: | |
| 1269: | |
| 1270: | private static function toUtf8($value) |
| 1271: | { |
| 1272: | $content = $value['content']; |
| 1273: | switch ($value['tag']) |
| 1274: | { |
| 1275: | case 0x0c: |
| 1276: | return preg_match('//u', $content) ? $content : null; |
| 1277: | case 0x12: |
| 1278: | case 0x13: |
| 1279: | case 0x14: |
| 1280: | case 0x16: |
| 1281: | case 0x1a: |
| 1282: | |
| 1283: | return self::codePointsToUtf8('' === $content ? [] : array_values(unpack('C*', $content))); |
| 1284: | case 0x1e: |
| 1285: | if (0 !== strlen($content) % 2) |
| 1286: | { |
| 1287: | return null; |
| 1288: | } |
| 1289: | |
| 1290: | $units = '' === $content ? [] : array_values(unpack('n*', $content)); |
| 1291: | $codePoints = []; |
| 1292: | for ($i = 0, $n = count($units); $i < $n; $i++) |
| 1293: | { |
| 1294: | $unit = $units[$i]; |
| 1295: | if ($unit >= 0xd800 && $unit <= 0xdbff && $i + 1 < $n && $units[$i + 1] >= 0xdc00 && $units[$i + 1] <= 0xdfff) |
| 1296: | { |
| 1297: | $codePoints[] = 0x10000 + (($unit - 0xd800) << 10) + ($units[++$i] - 0xdc00); |
| 1298: | } |
| 1299: | else |
| 1300: | { |
| 1301: | $codePoints[] = $unit; |
| 1302: | } |
| 1303: | } |
| 1304: | return self::codePointsToUtf8($codePoints); |
| 1305: | case 0x1c: |
| 1306: | if (0 !== strlen($content) % 4) |
| 1307: | { |
| 1308: | return null; |
| 1309: | } |
| 1310: | return self::codePointsToUtf8('' === $content ? [] : array_values(unpack('N*', $content))); |
| 1311: | default: |
| 1312: | return null; |
| 1313: | } |
| 1314: | } |
| 1315: | |
| 1316: | |
| 1317: | |
| 1318: | |
| 1319: | |
| 1320: | |
| 1321: | private static function codePointsToUtf8(array $codePoints) |
| 1322: | { |
| 1323: | $utf8 = ''; |
| 1324: | foreach ($codePoints as $codePoint) |
| 1325: | { |
| 1326: | if ($codePoint < 0 || $codePoint > 0x10ffff || ($codePoint >= 0xd800 && $codePoint <= 0xdfff)) |
| 1327: | { |
| 1328: | return null; |
| 1329: | } |
| 1330: | if ($codePoint < 0x80) |
| 1331: | { |
| 1332: | $utf8 .= chr($codePoint); |
| 1333: | } |
| 1334: | elseif ($codePoint < 0x800) |
| 1335: | { |
| 1336: | $utf8 .= chr(0xc0 | ($codePoint >> 6)) . chr(0x80 | ($codePoint & 0x3f)); |
| 1337: | } |
| 1338: | elseif ($codePoint < 0x10000) |
| 1339: | { |
| 1340: | $utf8 .= chr(0xe0 | ($codePoint >> 12)) . chr(0x80 | (($codePoint >> 6) & 0x3f)) . chr(0x80 | ($codePoint & 0x3f)); |
| 1341: | } |
| 1342: | else |
| 1343: | { |
| 1344: | $utf8 .= chr(0xf0 | ($codePoint >> 18)) . chr(0x80 | (($codePoint >> 12) & 0x3f)) . chr(0x80 | (($codePoint >> 6) & 0x3f)) . chr(0x80 | ($codePoint & 0x3f)); |
| 1345: | } |
| 1346: | } |
| 1347: | |
| 1348: | return $utf8; |
| 1349: | } |
| 1350: | |
| 1351: | |
| 1352: | |
| 1353: | |
| 1354: | |
| 1355: | |
| 1356: | private static function canonicalString($value) |
| 1357: | { |
| 1358: | $value = preg_replace('/\A[ \t\n\v\f\r]+|[ \t\n\v\f\r]+\z/', '', $value); |
| 1359: | $value = preg_replace('/[ \t\n\v\f\r]+/', ' ', $value); |
| 1360: | |
| 1361: | return strtr($value, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'); |
| 1362: | } |
| 1363: | } |
| 1364: | |