[ Index ] |
PHP Cross Reference of DokuWiki |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * PKCS#1 Formatted DSA Key Handler 5 * 6 * PHP version 5 7 * 8 * Used by File/X509.php 9 * 10 * Processes keys with the following headers: 11 * 12 * -----BEGIN DSA PRIVATE KEY----- 13 * -----BEGIN DSA PUBLIC KEY----- 14 * -----BEGIN DSA PARAMETERS----- 15 * 16 * Analogous to ssh-keygen's pem format (as specified by -m) 17 * 18 * Also, technically, PKCS1 decribes RSA but I am not aware of a formal specification for DSA. 19 * The DSA private key format seems to have been adapted from the RSA private key format so 20 * we're just re-using that as the name. 21 * 22 * @author Jim Wigginton <terrafrost@php.net> 23 * @copyright 2015 Jim Wigginton 24 * @license http://www.opensource.org/licenses/mit-license.html MIT License 25 * @link http://phpseclib.sourceforge.net 26 */ 27 28 namespace phpseclib3\Crypt\DSA\Formats\Keys; 29 30 use phpseclib3\Common\Functions\Strings; 31 use phpseclib3\Crypt\Common\Formats\Keys\PKCS1 as Progenitor; 32 use phpseclib3\File\ASN1; 33 use phpseclib3\File\ASN1\Maps; 34 use phpseclib3\Math\BigInteger; 35 36 /** 37 * PKCS#1 Formatted DSA Key Handler 38 * 39 * @author Jim Wigginton <terrafrost@php.net> 40 */ 41 abstract class PKCS1 extends Progenitor 42 { 43 /** 44 * Break a public or private key down into its constituent components 45 * 46 * @param string $key 47 * @param string $password optional 48 * @return array 49 */ 50 public static function load($key, $password = '') 51 { 52 $key = parent::load($key, $password); 53 54 $decoded = ASN1::decodeBER($key); 55 if (!$decoded) { 56 throw new \RuntimeException('Unable to decode BER'); 57 } 58 59 $key = ASN1::asn1map($decoded[0], Maps\DSAParams::MAP); 60 if (is_array($key)) { 61 return $key; 62 } 63 64 $key = ASN1::asn1map($decoded[0], Maps\DSAPrivateKey::MAP); 65 if (is_array($key)) { 66 return $key; 67 } 68 69 $key = ASN1::asn1map($decoded[0], Maps\DSAPublicKey::MAP); 70 if (is_array($key)) { 71 return $key; 72 } 73 74 throw new \RuntimeException('Unable to perform ASN1 mapping'); 75 } 76 77 /** 78 * Convert DSA parameters to the appropriate format 79 * 80 * @param \phpseclib3\Math\BigInteger $p 81 * @param \phpseclib3\Math\BigInteger $q 82 * @param \phpseclib3\Math\BigInteger $g 83 * @return string 84 */ 85 public static function saveParameters(BigInteger $p, BigInteger $q, BigInteger $g) 86 { 87 $key = [ 88 'p' => $p, 89 'q' => $q, 90 'g' => $g 91 ]; 92 93 $key = ASN1::encodeDER($key, Maps\DSAParams::MAP); 94 95 return "-----BEGIN DSA PARAMETERS-----\r\n" . 96 chunk_split(Strings::base64_encode($key), 64) . 97 "-----END DSA PARAMETERS-----\r\n"; 98 } 99 100 /** 101 * Convert a private key to the appropriate format. 102 * 103 * @param \phpseclib3\Math\BigInteger $p 104 * @param \phpseclib3\Math\BigInteger $q 105 * @param \phpseclib3\Math\BigInteger $g 106 * @param \phpseclib3\Math\BigInteger $y 107 * @param \phpseclib3\Math\BigInteger $x 108 * @param string $password optional 109 * @param array $options optional 110 * @return string 111 */ 112 public static function savePrivateKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y, BigInteger $x, $password = '', array $options = []) 113 { 114 $key = [ 115 'version' => 0, 116 'p' => $p, 117 'q' => $q, 118 'g' => $g, 119 'y' => $y, 120 'x' => $x 121 ]; 122 123 $key = ASN1::encodeDER($key, Maps\DSAPrivateKey::MAP); 124 125 return self::wrapPrivateKey($key, 'DSA', $password, $options); 126 } 127 128 /** 129 * Convert a public key to the appropriate format 130 * 131 * @param \phpseclib3\Math\BigInteger $p 132 * @param \phpseclib3\Math\BigInteger $q 133 * @param \phpseclib3\Math\BigInteger $g 134 * @param \phpseclib3\Math\BigInteger $y 135 * @return string 136 */ 137 public static function savePublicKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y) 138 { 139 $key = ASN1::encodeDER($y, Maps\DSAPublicKey::MAP); 140 141 return self::wrapPublicKey($key, 'DSA'); 142 } 143 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body