.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
JWK.php
131 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * JSON Web Key (RFC7517) Formatted RSA Handler |
| 5 | * |
| 6 | * PHP version 5 |
| 7 | * |
| 8 | * @author Jim Wigginton <terrafrost@php.net> |
| 9 | * @copyright 2015 Jim Wigginton |
| 10 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
| 11 | * @link http://phpseclib.sourceforge.net |
| 12 | */ |
| 13 | |
| 14 | declare(strict_types=1); |
| 15 | |
| 16 | namespace phpseclib3\Crypt\RSA\Formats\Keys; |
| 17 | |
| 18 | use phpseclib3\Common\Functions\Strings; |
| 19 | use phpseclib3\Crypt\Common\Formats\Keys\JWK as Progenitor; |
| 20 | use phpseclib3\Math\BigInteger; |
| 21 | |
| 22 | /** |
| 23 | * JWK Formatted RSA Handler |
| 24 | * |
| 25 | * @author Jim Wigginton <terrafrost@php.net> |
| 26 | */ |
| 27 | abstract class JWK extends Progenitor |
| 28 | { |
| 29 | /** |
| 30 | * Break a public or private key down into its constituent components |
| 31 | * |
| 32 | * @param string|array $key |
| 33 | */ |
| 34 | public static function load($key, ?string $password = null): array |
| 35 | { |
| 36 | $key = parent::loadHelper($key); |
| 37 | |
| 38 | if ($key->kty != 'RSA') { |
| 39 | throw new \RuntimeException('Only RSA JWK keys are supported'); |
| 40 | } |
| 41 | |
| 42 | $count = $publicCount = 0; |
| 43 | $vars = ['n', 'e', 'd', 'p', 'q', 'dp', 'dq', 'qi']; |
| 44 | foreach ($vars as $var) { |
| 45 | if (!isset($key->$var) || !is_string($key->$var)) { |
| 46 | continue; |
| 47 | } |
| 48 | $count++; |
| 49 | $value = new BigInteger(Strings::base64url_decode($key->$var), 256); |
| 50 | switch ($var) { |
| 51 | case 'n': |
| 52 | $publicCount++; |
| 53 | $components['modulus'] = $value; |
| 54 | break; |
| 55 | case 'e': |
| 56 | $publicCount++; |
| 57 | $components['publicExponent'] = $value; |
| 58 | break; |
| 59 | case 'd': |
| 60 | $components['privateExponent'] = $value; |
| 61 | break; |
| 62 | case 'p': |
| 63 | $components['primes'][1] = $value; |
| 64 | break; |
| 65 | case 'q': |
| 66 | $components['primes'][2] = $value; |
| 67 | break; |
| 68 | case 'dp': |
| 69 | $components['exponents'][1] = $value; |
| 70 | break; |
| 71 | case 'dq': |
| 72 | $components['exponents'][2] = $value; |
| 73 | break; |
| 74 | case 'qi': |
| 75 | $components['coefficients'][2] = $value; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if ($count == count($vars)) { |
| 80 | return $components + ['isPublicKey' => false]; |
| 81 | } |
| 82 | |
| 83 | if ($count == 2 && $publicCount == 2) { |
| 84 | return $components + ['isPublicKey' => true]; |
| 85 | } |
| 86 | |
| 87 | throw new \UnexpectedValueException('Key does not have an appropriate number of RSA parameters'); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Convert a private key to the appropriate format. |
| 92 | * |
| 93 | * @param string $password optional |
| 94 | * @param array $options optional |
| 95 | */ |
| 96 | public static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, array $primes, array $exponents, array $coefficients, ?string $password = null, array $options = []): string |
| 97 | { |
| 98 | if (count($primes) != 2) { |
| 99 | throw new \InvalidArgumentException('JWK does not support multi-prime RSA keys'); |
| 100 | } |
| 101 | |
| 102 | $key = [ |
| 103 | 'kty' => 'RSA', |
| 104 | 'n' => Strings::base64url_encode($n->toBytes()), |
| 105 | 'e' => Strings::base64url_encode($e->toBytes()), |
| 106 | 'd' => Strings::base64url_encode($d->toBytes()), |
| 107 | 'p' => Strings::base64url_encode($primes[1]->toBytes()), |
| 108 | 'q' => Strings::base64url_encode($primes[2]->toBytes()), |
| 109 | 'dp' => Strings::base64url_encode($exponents[1]->toBytes()), |
| 110 | 'dq' => Strings::base64url_encode($exponents[2]->toBytes()), |
| 111 | 'qi' => Strings::base64url_encode($coefficients[2]->toBytes()), |
| 112 | ]; |
| 113 | |
| 114 | return self::wrapKey($key, $options); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Convert a public key to the appropriate format |
| 119 | */ |
| 120 | public static function savePublicKey(BigInteger $n, BigInteger $e, array $options = []): string |
| 121 | { |
| 122 | $key = [ |
| 123 | 'kty' => 'RSA', |
| 124 | 'n' => Strings::base64url_encode($n->toBytes()), |
| 125 | 'e' => Strings::base64url_encode($e->toBytes()), |
| 126 | ]; |
| 127 | |
| 128 | return self::wrapKey($key, $options); |
| 129 | } |
| 130 | } |
| 131 |