.htaccess
1 year ago
JWK.php
1 year ago
MSBLOB.php
1 year ago
OpenSSH.php
1 year ago
PKCS1.php
1 year ago
PKCS8.php
1 year ago
PSS.php
1 year ago
PuTTY.php
1 year ago
Raw.php
1 year ago
XML.php
1 year ago
index.html
1 year ago
web.config
1 year ago
PKCS1.php
153 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * PKCS#1 Formatted RSA 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 RSA PRIVATE KEY----- |
| 13 | * -----BEGIN RSA PUBLIC KEY----- |
| 14 | * |
| 15 | * Analogous to ssh-keygen's pem format (as specified by -m) |
| 16 | * |
| 17 | * @author Jim Wigginton <terrafrost@php.net> |
| 18 | * @copyright 2015 Jim Wigginton |
| 19 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
| 20 | * @link http://phpseclib.sourceforge.net |
| 21 | */ |
| 22 | |
| 23 | declare(strict_types=1); |
| 24 | |
| 25 | namespace phpseclib3\Crypt\RSA\Formats\Keys; |
| 26 | |
| 27 | use phpseclib3\Common\Functions\Strings; |
| 28 | use phpseclib3\Crypt\Common\Formats\Keys\PKCS1 as Progenitor; |
| 29 | use phpseclib3\Exception\RuntimeException; |
| 30 | use phpseclib3\Exception\UnexpectedValueException; |
| 31 | use phpseclib3\File\ASN1; |
| 32 | use phpseclib3\File\ASN1\Maps; |
| 33 | use phpseclib3\Math\BigInteger; |
| 34 | |
| 35 | /** |
| 36 | * PKCS#1 Formatted RSA Key Handler |
| 37 | * |
| 38 | * @author Jim Wigginton <terrafrost@php.net> |
| 39 | */ |
| 40 | abstract class PKCS1 extends Progenitor |
| 41 | { |
| 42 | /** |
| 43 | * Break a public or private key down into its constituent components |
| 44 | * |
| 45 | * @param string|array $key |
| 46 | * @param string|false $password |
| 47 | */ |
| 48 | public static function load($key, ?string $password = null): array |
| 49 | { |
| 50 | if (!Strings::is_stringable($key)) { |
| 51 | throw new UnexpectedValueException('Key should be a string - not a ' . gettype($key)); |
| 52 | } |
| 53 | |
| 54 | if (str_contains($key, 'PUBLIC')) { |
| 55 | $components = ['isPublicKey' => true]; |
| 56 | } elseif (str_contains($key, 'PRIVATE')) { |
| 57 | $components = ['isPublicKey' => false]; |
| 58 | } else { |
| 59 | $components = []; |
| 60 | } |
| 61 | |
| 62 | $key = parent::load($key, $password); |
| 63 | |
| 64 | $decoded = ASN1::decodeBER($key); |
| 65 | if (!$decoded) { |
| 66 | throw new RuntimeException('Unable to decode BER'); |
| 67 | } |
| 68 | |
| 69 | $key = ASN1::asn1map($decoded[0], Maps\RSAPrivateKey::MAP); |
| 70 | if (is_array($key)) { |
| 71 | $components += [ |
| 72 | 'modulus' => $key['modulus'], |
| 73 | 'publicExponent' => $key['publicExponent'], |
| 74 | 'privateExponent' => $key['privateExponent'], |
| 75 | 'primes' => [1 => $key['prime1'], $key['prime2']], |
| 76 | 'exponents' => [1 => $key['exponent1'], $key['exponent2']], |
| 77 | 'coefficients' => [2 => $key['coefficient']], |
| 78 | ]; |
| 79 | if ($key['version'] == 'multi') { |
| 80 | foreach ($key['otherPrimeInfos'] as $primeInfo) { |
| 81 | $components['primes'][] = $primeInfo['prime']; |
| 82 | $components['exponents'][] = $primeInfo['exponent']; |
| 83 | $components['coefficients'][] = $primeInfo['coefficient']; |
| 84 | } |
| 85 | } |
| 86 | if (!isset($components['isPublicKey'])) { |
| 87 | $components['isPublicKey'] = false; |
| 88 | } |
| 89 | return $components; |
| 90 | } |
| 91 | |
| 92 | $key = ASN1::asn1map($decoded[0], Maps\RSAPublicKey::MAP); |
| 93 | |
| 94 | if (!is_array($key)) { |
| 95 | throw new RuntimeException('Unable to perform ASN1 mapping'); |
| 96 | } |
| 97 | |
| 98 | if (!isset($components['isPublicKey'])) { |
| 99 | $components['isPublicKey'] = true; |
| 100 | } |
| 101 | |
| 102 | return $components + $key; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Convert a private key to the appropriate format. |
| 107 | * |
| 108 | * @param string|false $password |
| 109 | * @param array $options optional |
| 110 | */ |
| 111 | public static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, array $primes, array $exponents, array $coefficients, ?string $password = null, array $options = []): string |
| 112 | { |
| 113 | $num_primes = count($primes); |
| 114 | $key = [ |
| 115 | 'version' => $num_primes == 2 ? 'two-prime' : 'multi', |
| 116 | 'modulus' => $n, |
| 117 | 'publicExponent' => $e, |
| 118 | 'privateExponent' => $d, |
| 119 | 'prime1' => $primes[1], |
| 120 | 'prime2' => $primes[2], |
| 121 | 'exponent1' => $exponents[1], |
| 122 | 'exponent2' => $exponents[2], |
| 123 | 'coefficient' => $coefficients[2], |
| 124 | ]; |
| 125 | for ($i = 3; $i <= $num_primes; $i++) { |
| 126 | $key['otherPrimeInfos'][] = [ |
| 127 | 'prime' => $primes[$i], |
| 128 | 'exponent' => $exponents[$i], |
| 129 | 'coefficient' => $coefficients[$i], |
| 130 | ]; |
| 131 | } |
| 132 | |
| 133 | $key = ASN1::encodeDER($key, Maps\RSAPrivateKey::MAP); |
| 134 | |
| 135 | return self::wrapPrivateKey($key, 'RSA', $password, $options); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Convert a public key to the appropriate format |
| 140 | */ |
| 141 | public static function savePublicKey(BigInteger $n, BigInteger $e): string |
| 142 | { |
| 143 | $key = [ |
| 144 | 'modulus' => $n, |
| 145 | 'publicExponent' => $e, |
| 146 | ]; |
| 147 | |
| 148 | $key = ASN1::encodeDER($key, Maps\RSAPublicKey::MAP); |
| 149 | |
| 150 | return self::wrapPublicKey($key, 'RSA'); |
| 151 | } |
| 152 | } |
| 153 |