[ Index ] |
PHP Cross Reference of DokuWiki |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * PKCS#8 Formatted DSA Key Handler 5 * 6 * PHP version 5 7 * 8 * Processes keys with the following headers: 9 * 10 * -----BEGIN ENCRYPTED PRIVATE KEY----- 11 * -----BEGIN PRIVATE KEY----- 12 * -----BEGIN PUBLIC KEY----- 13 * 14 * Analogous to ssh-keygen's pkcs8 format (as specified by -m). Although PKCS8 15 * is specific to private keys it's basically creating a DER-encoded wrapper 16 * for keys. This just extends that same concept to public keys (much like ssh-keygen) 17 * 18 * @author Jim Wigginton <terrafrost@php.net> 19 * @copyright 2015 Jim Wigginton 20 * @license http://www.opensource.org/licenses/mit-license.html MIT License 21 * @link http://phpseclib.sourceforge.net 22 */ 23 24 namespace phpseclib3\Crypt\DSA\Formats\Keys; 25 26 use phpseclib3\Crypt\Common\Formats\Keys\PKCS8 as Progenitor; 27 use phpseclib3\File\ASN1; 28 use phpseclib3\File\ASN1\Maps; 29 use phpseclib3\Math\BigInteger; 30 31 /** 32 * PKCS#8 Formatted DSA Key Handler 33 * 34 * @author Jim Wigginton <terrafrost@php.net> 35 */ 36 abstract class PKCS8 extends Progenitor 37 { 38 /** 39 * OID Name 40 * 41 * @var string 42 */ 43 const OID_NAME = 'id-dsa'; 44 45 /** 46 * OID Value 47 * 48 * @var string 49 */ 50 const OID_VALUE = '1.2.840.10040.4.1'; 51 52 /** 53 * Child OIDs loaded 54 * 55 * @var bool 56 */ 57 protected static $childOIDsLoaded = false; 58 59 /** 60 * Break a public or private key down into its constituent components 61 * 62 * @param string $key 63 * @param string $password optional 64 * @return array 65 */ 66 public static function load($key, $password = '') 67 { 68 $key = parent::load($key, $password); 69 70 $type = isset($key['privateKey']) ? 'privateKey' : 'publicKey'; 71 72 $decoded = ASN1::decodeBER($key[$type . 'Algorithm']['parameters']->element); 73 if (!$decoded) { 74 throw new \RuntimeException('Unable to decode BER of parameters'); 75 } 76 $components = ASN1::asn1map($decoded[0], Maps\DSAParams::MAP); 77 if (!is_array($components)) { 78 throw new \RuntimeException('Unable to perform ASN1 mapping on parameters'); 79 } 80 81 $decoded = ASN1::decodeBER($key[$type]); 82 if (empty($decoded)) { 83 throw new \RuntimeException('Unable to decode BER'); 84 } 85 86 $var = $type == 'privateKey' ? 'x' : 'y'; 87 $components[$var] = ASN1::asn1map($decoded[0], Maps\DSAPublicKey::MAP); 88 if (!$components[$var] instanceof BigInteger) { 89 throw new \RuntimeException('Unable to perform ASN1 mapping'); 90 } 91 92 if (isset($key['meta'])) { 93 $components['meta'] = $key['meta']; 94 } 95 96 return $components; 97 } 98 99 /** 100 * Convert a private key to the appropriate format. 101 * 102 * @param \phpseclib3\Math\BigInteger $p 103 * @param \phpseclib3\Math\BigInteger $q 104 * @param \phpseclib3\Math\BigInteger $g 105 * @param \phpseclib3\Math\BigInteger $y 106 * @param \phpseclib3\Math\BigInteger $x 107 * @param string $password optional 108 * @param array $options optional 109 * @return string 110 */ 111 public static function savePrivateKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y, BigInteger $x, $password = '', array $options = []) 112 { 113 $params = [ 114 'p' => $p, 115 'q' => $q, 116 'g' => $g 117 ]; 118 $params = ASN1::encodeDER($params, Maps\DSAParams::MAP); 119 $params = new ASN1\Element($params); 120 $key = ASN1::encodeDER($x, Maps\DSAPublicKey::MAP); 121 return self::wrapPrivateKey($key, [], $params, $password, null, '', $options); 122 } 123 124 /** 125 * Convert a public key to the appropriate format 126 * 127 * @param \phpseclib3\Math\BigInteger $p 128 * @param \phpseclib3\Math\BigInteger $q 129 * @param \phpseclib3\Math\BigInteger $g 130 * @param \phpseclib3\Math\BigInteger $y 131 * @param array $options optional 132 * @return string 133 */ 134 public static function savePublicKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y, array $options = []) 135 { 136 $params = [ 137 'p' => $p, 138 'q' => $q, 139 'g' => $g 140 ]; 141 $params = ASN1::encodeDER($params, Maps\DSAParams::MAP); 142 $params = new ASN1\Element($params); 143 $key = ASN1::encodeDER($y, Maps\DSAPublicKey::MAP); 144 return self::wrapPublicKey($key, $params); 145 } 146 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body