.htaccess
9 months ago
JWK.php
9 months ago
MSBLOB.php
9 months ago
OpenSSH.php
9 months ago
PKCS1.php
9 months ago
PKCS8.php
9 months ago
PSS.php
9 months ago
PuTTY.php
9 months ago
Raw.php
9 months ago
XML.php
9 months ago
index.html
9 months ago
web.config
9 months ago
Raw.php
167 lines
| 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 | declare(strict_types=1); |
| 25 | |
| 26 | namespace phpseclib3\Crypt\RSA\Formats\Keys; |
| 27 | |
| 28 | use phpseclib3\Exception\UnexpectedValueException; |
| 29 | use phpseclib3\Exception\UnsupportedFormatException; |
| 30 | use phpseclib3\Math\BigInteger; |
| 31 | |
| 32 | /** |
| 33 | * Raw RSA Key Handler |
| 34 | * |
| 35 | * @author Jim Wigginton <terrafrost@php.net> |
| 36 | */ |
| 37 | abstract class Raw |
| 38 | { |
| 39 | /** |
| 40 | * Break a public or private key down into its constituent components |
| 41 | * |
| 42 | * @param string|array $key |
| 43 | */ |
| 44 | public static function load($key, ?string $password = null): array |
| 45 | { |
| 46 | if (!is_array($key)) { |
| 47 | throw new UnexpectedValueException('Key should be a array - not a ' . gettype($key)); |
| 48 | } |
| 49 | |
| 50 | $key = array_change_key_case($key, CASE_LOWER); |
| 51 | |
| 52 | $components = ['isPublicKey' => false]; |
| 53 | |
| 54 | foreach (['e', 'exponent', 'publicexponent', 0, 'privateexponent', 'd'] as $index) { |
| 55 | if (isset($key[$index])) { |
| 56 | $components['publicExponent'] = $key[$index]; |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | foreach (['n', 'modulo', 'modulus', 1] as $index) { |
| 62 | if (isset($key[$index])) { |
| 63 | $components['modulus'] = $key[$index]; |
| 64 | break; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (!isset($components['publicExponent']) || !isset($components['modulus'])) { |
| 69 | throw new UnexpectedValueException('Modulus / exponent not present'); |
| 70 | } |
| 71 | |
| 72 | if (isset($key['primes'])) { |
| 73 | $components['primes'] = $key['primes']; |
| 74 | } elseif (isset($key['p']) && isset($key['q'])) { |
| 75 | $indices = [ |
| 76 | ['p', 'q'], |
| 77 | ['prime1', 'prime2'], |
| 78 | ]; |
| 79 | foreach ($indices as $index) { |
| 80 | [$i0, $i1] = $index; |
| 81 | if (isset($key[$i0]) && isset($key[$i1])) { |
| 82 | $components['primes'] = [1 => $key[$i0], $key[$i1]]; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (isset($key['exponents'])) { |
| 88 | $components['exponents'] = $key['exponents']; |
| 89 | } else { |
| 90 | $indices = [ |
| 91 | ['dp', 'dq'], |
| 92 | ['exponent1', 'exponent2'], |
| 93 | ]; |
| 94 | foreach ($indices as $index) { |
| 95 | [$i0, $i1] = $index; |
| 96 | if (isset($key[$i0]) && isset($key[$i1])) { |
| 97 | $components['exponents'] = [1 => $key[$i0], $key[$i1]]; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | if (isset($key['coefficients'])) { |
| 103 | $components['coefficients'] = $key['coefficients']; |
| 104 | } else { |
| 105 | foreach (['inverseq', 'q\'', 'coefficient'] as $index) { |
| 106 | if (isset($key[$index])) { |
| 107 | $components['coefficients'] = [2 => $key[$index]]; |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (!isset($components['primes'])) { |
| 113 | $components['isPublicKey'] = true; |
| 114 | return $components; |
| 115 | } |
| 116 | |
| 117 | if (!isset($components['exponents'])) { |
| 118 | $one = new BigInteger(1); |
| 119 | $temp = $components['primes'][1]->subtract($one); |
| 120 | $exponents = [1 => $components['publicExponent']->modInverse($temp)]; |
| 121 | $temp = $components['primes'][2]->subtract($one); |
| 122 | $exponents[] = $components['publicExponent']->modInverse($temp); |
| 123 | $components['exponents'] = $exponents; |
| 124 | } |
| 125 | |
| 126 | if (!isset($components['coefficients'])) { |
| 127 | $components['coefficients'] = [2 => $components['primes'][2]->modInverse($components['primes'][1])]; |
| 128 | } |
| 129 | |
| 130 | foreach (['privateexponent', 'd'] as $index) { |
| 131 | if (isset($key[$index])) { |
| 132 | $components['privateExponent'] = $key[$index]; |
| 133 | break; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | return $components; |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Convert a private key to the appropriate format. |
| 142 | */ |
| 143 | public static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, array $primes, array $exponents, array $coefficients, ?string $password = null, array $options = []): string |
| 144 | { |
| 145 | if (!empty($password) && is_string($password)) { |
| 146 | throw new UnsupportedFormatException('Raw private keys do not support encryption'); |
| 147 | } |
| 148 | |
| 149 | return serialize([ |
| 150 | 'e' => clone $e, |
| 151 | 'n' => clone $n, |
| 152 | 'd' => clone $d, |
| 153 | 'primes' => array_map(fn ($var) => clone $var, $primes), |
| 154 | 'exponents' => array_map(fn ($var) => clone $var, $exponents), |
| 155 | 'coefficients' => array_map(fn ($var) => clone $var, $coefficients), |
| 156 | ]); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Convert a public key to the appropriate format |
| 161 | */ |
| 162 | public static function savePublicKey(BigInteger $n, BigInteger $e): array |
| 163 | { |
| 164 | return ['e' => clone $e, 'n' => clone $n]; |
| 165 | } |
| 166 | } |
| 167 |