Subversion Repositories oidplus

Rev

Rev 1042 | Rev 1427 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1042 Rev 1114
Line 313... Line 313...
313
     * @param string $password optional
313
     * @param string $password optional
314
     * @return array
314
     * @return array
315
     */
315
     */
316
    protected static function load($key, $password = '')
316
    protected static function load($key, $password = '')
317
    {
317
    {
-
 
318
        if (!Strings::is_stringable($key)) {
-
 
319
            throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
-
 
320
        }
-
 
321
 
-
 
322
        $isPublic = strpos($key, 'PUBLIC') !== false;
-
 
323
        $isPrivate = strpos($key, 'PRIVATE') !== false;
-
 
324
 
318
        $decoded = self::preParse($key);
325
        $decoded = self::preParse($key);
319
 
326
 
320
        $meta = [];
327
        $meta = [];
321
 
328
 
322
        $decrypted = ASN1::asn1map($decoded[0], Maps\EncryptedPrivateKeyInfo::MAP);
329
        $decrypted = ASN1::asn1map($decoded[0], Maps\EncryptedPrivateKeyInfo::MAP);
Line 443... Line 450...
443
            }
450
            }
444
        }
451
        }
445
 
452
 
446
        $private = ASN1::asn1map($decoded[0], Maps\OneAsymmetricKey::MAP);
453
        $private = ASN1::asn1map($decoded[0], Maps\OneAsymmetricKey::MAP);
447
        if (is_array($private)) {
454
        if (is_array($private)) {
-
 
455
            if ($isPublic) {
-
 
456
                throw new \UnexpectedValueException('Human readable string claims public key but DER encoded string claims private key');
-
 
457
            }
-
 
458
 
448
            if (isset($private['privateKeyAlgorithm']['parameters']) && !$private['privateKeyAlgorithm']['parameters'] instanceof ASN1\Element && isset($decoded[0]['content'][1]['content'][1])) {
459
            if (isset($private['privateKeyAlgorithm']['parameters']) && !$private['privateKeyAlgorithm']['parameters'] instanceof ASN1\Element && isset($decoded[0]['content'][1]['content'][1])) {
449
                $temp = $decoded[0]['content'][1]['content'][1];
460
                $temp = $decoded[0]['content'][1]['content'][1];
450
                $private['privateKeyAlgorithm']['parameters'] = new ASN1\Element(substr($key, $temp['start'], $temp['length']));
461
                $private['privateKeyAlgorithm']['parameters'] = new ASN1\Element(substr($key, $temp['start'], $temp['length']));
451
            }
462
            }
452
            if (is_array(static::OID_NAME)) {
463
            if (is_array(static::OID_NAME)) {
Line 472... Line 483...
472
        // string represents the number of bits in the last byte that are to be ignored but, currently,
483
        // string represents the number of bits in the last byte that are to be ignored but, currently,
473
        // bit strings wanting a non-zero amount of bits trimmed are not supported
484
        // bit strings wanting a non-zero amount of bits trimmed are not supported
474
        $public = ASN1::asn1map($decoded[0], Maps\PublicKeyInfo::MAP);
485
        $public = ASN1::asn1map($decoded[0], Maps\PublicKeyInfo::MAP);
475
 
486
 
476
        if (is_array($public)) {
487
        if (is_array($public)) {
-
 
488
            if ($isPrivate) {
-
 
489
                throw new \UnexpectedValueException('Human readable string claims private key but DER encoded string claims public key');
-
 
490
            }
-
 
491
 
477
            if ($public['publicKey'][0] != "\0") {
492
            if ($public['publicKey'][0] != "\0") {
478
                throw new \UnexpectedValueException('The first byte of the public key should be null - not ' . bin2hex($public['publicKey'][0]));
493
                throw new \UnexpectedValueException('The first byte of the public key should be null - not ' . bin2hex($public['publicKey'][0]));
479
            }
494
            }
480
            if (is_array(static::OID_NAME)) {
495
            if (is_array(static::OID_NAME)) {
481
                if (!in_array($public['publicKeyAlgorithm']['algorithm'], static::OID_NAME)) {
496
                if (!in_array($public['publicKeyAlgorithm']['algorithm'], static::OID_NAME)) {
Line 648... Line 663...
648
     */
663
     */
649
    private static function preParse(&$key)
664
    private static function preParse(&$key)
650
    {
665
    {
651
        self::initialize_static_variables();
666
        self::initialize_static_variables();
652
 
667
 
653
        if (!Strings::is_stringable($key)) {
-
 
654
            throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
-
 
655
        }
-
 
656
 
-
 
657
        if (self::$format != self::MODE_DER) {
668
        if (self::$format != self::MODE_DER) {
658
            $decoded = ASN1::extractBER($key);
669
            $decoded = ASN1::extractBER($key);
659
            if ($decoded !== false) {
670
            if ($decoded !== false) {
660
                $key = $decoded;
671
                $key = $decoded;
661
            } elseif (self::$format == self::MODE_PEM) {
672
            } elseif (self::$format == self::MODE_PEM) {
Line 677... Line 688...
677
     * @param string $key
688
     * @param string $key
678
     * @return array
689
     * @return array
679
     */
690
     */
680
    public static function extractEncryptionAlgorithm($key)
691
    public static function extractEncryptionAlgorithm($key)
681
    {
692
    {
-
 
693
        if (!Strings::is_stringable($key)) {
-
 
694
            throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key));
-
 
695
        }
-
 
696
 
682
        $decoded = self::preParse($key);
697
        $decoded = self::preParse($key);
683
 
698
 
684
        $r = ASN1::asn1map($decoded[0], ASN1\Maps\EncryptedPrivateKeyInfo::MAP);
699
        $r = ASN1::asn1map($decoded[0], ASN1\Maps\EncryptedPrivateKeyInfo::MAP);
685
        if (!is_array($r)) {
700
        if (!is_array($r)) {
686
            throw new \RuntimeException('Unable to parse using EncryptedPrivateKeyInfo map');
701
            throw new \RuntimeException('Unable to parse using EncryptedPrivateKeyInfo map');