[ Index ] |
PHP Cross Reference of DokuWiki |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Raw RSA Key Handler 5 * 6 * PHP version 5 7 * 8 * An array containing two \phpseclib3\Math\BigInteger objects. 9 * 10 * The exponent can be indexed with any of the following: 11 * 12 * 0, e, exponent, publicExponent 13 * 14 * The modulus can be indexed with any of the following: 15 * 16 * 1, n, modulo, modulus 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\RSA\Formats\Keys; 25 26 use phpseclib3\Math\BigInteger; 27 28 /** 29 * Raw RSA Key Handler 30 * 31 * @author Jim Wigginton <terrafrost@php.net> 32 */ 33 abstract class Raw 34 { 35 /** 36 * Break a public or private key down into its constituent components 37 * 38 * @param string $key 39 * @param string $password optional 40 * @return array 41 */ 42 public static function load($key, $password = '') 43 { 44 if (!is_array($key)) { 45 throw new \UnexpectedValueException('Key should be a array - not a ' . gettype($key)); 46 } 47 48 $key = array_change_key_case($key, CASE_LOWER); 49 50 $components = ['isPublicKey' => false]; 51 52 foreach (['e', 'exponent', 'publicexponent', 0, 'privateexponent', 'd'] as $index) { 53 if (isset($key[$index])) { 54 $components['publicExponent'] = $key[$index]; 55 break; 56 } 57 } 58 59 foreach (['n', 'modulo', 'modulus', 1] as $index) { 60 if (isset($key[$index])) { 61 $components['modulus'] = $key[$index]; 62 break; 63 } 64 } 65 66 if (!isset($components['publicExponent']) || !isset($components['modulus'])) { 67 throw new \UnexpectedValueException('Modulus / exponent not present'); 68 } 69 70 if (isset($key['primes'])) { 71 $components['primes'] = $key['primes']; 72 } elseif (isset($key['p']) && isset($key['q'])) { 73 $indices = [ 74 ['p', 'q'], 75 ['prime1', 'prime2'] 76 ]; 77 foreach ($indices as $index) { 78 list($i0, $i1) = $index; 79 if (isset($key[$i0]) && isset($key[$i1])) { 80 $components['primes'] = [1 => $key[$i0], $key[$i1]]; 81 } 82 } 83 } 84 85 if (isset($key['exponents'])) { 86 $components['exponents'] = $key['exponents']; 87 } else { 88 $indices = [ 89 ['dp', 'dq'], 90 ['exponent1', 'exponent2'] 91 ]; 92 foreach ($indices as $index) { 93 list($i0, $i1) = $index; 94 if (isset($key[$i0]) && isset($key[$i1])) { 95 $components['exponents'] = [1 => $key[$i0], $key[$i1]]; 96 } 97 } 98 } 99 100 if (isset($key['coefficients'])) { 101 $components['coefficients'] = $key['coefficients']; 102 } else { 103 foreach (['inverseq', 'q\'', 'coefficient'] as $index) { 104 if (isset($key[$index])) { 105 $components['coefficients'] = [2 => $key[$index]]; 106 } 107 } 108 } 109 110 if (!isset($components['primes'])) { 111 $components['isPublicKey'] = true; 112 return $components; 113 } 114 115 if (!isset($components['exponents'])) { 116 $one = new BigInteger(1); 117 $temp = $components['primes'][1]->subtract($one); 118 $exponents = [1 => $components['publicExponent']->modInverse($temp)]; 119 $temp = $components['primes'][2]->subtract($one); 120 $exponents[] = $components['publicExponent']->modInverse($temp); 121 $components['exponents'] = $exponents; 122 } 123 124 if (!isset($components['coefficients'])) { 125 $components['coefficients'] = [2 => $components['primes'][2]->modInverse($components['primes'][1])]; 126 } 127 128 foreach (['privateexponent', 'd'] as $index) { 129 if (isset($key[$index])) { 130 $components['privateExponent'] = $key[$index]; 131 break; 132 } 133 } 134 135 return $components; 136 } 137 138 /** 139 * Convert a private key to the appropriate format. 140 * 141 * @param \phpseclib3\Math\BigInteger $n 142 * @param \phpseclib3\Math\BigInteger $e 143 * @param \phpseclib3\Math\BigInteger $d 144 * @param array $primes 145 * @param array $exponents 146 * @param array $coefficients 147 * @param string $password optional 148 * @param array $options optional 149 * @return array 150 */ 151 public static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, array $primes, array $exponents, array $coefficients, $password = '', array $options = []) 152 { 153 if (!empty($password) && is_string($password)) { 154 throw new UnsupportedFormatException('Raw private keys do not support encryption'); 155 } 156 157 return [ 158 'e' => clone $e, 159 'n' => clone $n, 160 'd' => clone $d, 161 'primes' => array_map(function ($var) { 162 return clone $var; 163 }, $primes), 164 'exponents' => array_map(function ($var) { 165 return clone $var; 166 }, $exponents), 167 'coefficients' => array_map(function ($var) { 168 return clone $var; 169 }, $coefficients) 170 ]; 171 } 172 173 /** 174 * Convert a public key to the appropriate format 175 * 176 * @param \phpseclib3\Math\BigInteger $n 177 * @param \phpseclib3\Math\BigInteger $e 178 * @return array 179 */ 180 public static function savePublicKey(BigInteger $n, BigInteger $e) 181 { 182 return ['e' => clone $e, 'n' => clone $n]; 183 } 184 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body