.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
OpenSSH.php
121 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * OpenSSH Formatted RSA Key Handler |
| 5 | * |
| 6 | * PHP version 5 |
| 7 | * |
| 8 | * Place in $HOME/.ssh/authorized_keys |
| 9 | * |
| 10 | * @author Jim Wigginton <terrafrost@php.net> |
| 11 | * @copyright 2015 Jim Wigginton |
| 12 | * @license http://www.opensource.org/licenses/mit-license.html MIT License |
| 13 | * @link http://phpseclib.sourceforge.net |
| 14 | */ |
| 15 | |
| 16 | declare(strict_types=1); |
| 17 | |
| 18 | namespace phpseclib3\Crypt\RSA\Formats\Keys; |
| 19 | |
| 20 | use phpseclib3\Common\Functions\Strings; |
| 21 | use phpseclib3\Crypt\Common\Formats\Keys\OpenSSH as Progenitor; |
| 22 | use phpseclib3\Exception\RuntimeException; |
| 23 | use phpseclib3\Math\BigInteger; |
| 24 | |
| 25 | /** |
| 26 | * OpenSSH Formatted RSA Key Handler |
| 27 | * |
| 28 | * @author Jim Wigginton <terrafrost@php.net> |
| 29 | */ |
| 30 | abstract class OpenSSH extends Progenitor |
| 31 | { |
| 32 | /** |
| 33 | * Supported Key Types |
| 34 | * |
| 35 | * @var array |
| 36 | */ |
| 37 | protected static $types = ['ssh-rsa']; |
| 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 | static $one; |
| 47 | if (!isset($one)) { |
| 48 | $one = new BigInteger(1); |
| 49 | } |
| 50 | |
| 51 | $parsed = parent::load($key, $password); |
| 52 | |
| 53 | if (isset($parsed['paddedKey'])) { |
| 54 | [$type] = Strings::unpackSSH2('s', $parsed['paddedKey']); |
| 55 | if ($type != $parsed['type']) { |
| 56 | throw new RuntimeException("The public and private keys are not of the same type ($type vs $parsed[type])"); |
| 57 | } |
| 58 | |
| 59 | $primes = $coefficients = []; |
| 60 | |
| 61 | [ |
| 62 | $modulus, |
| 63 | $publicExponent, |
| 64 | $privateExponent, |
| 65 | $coefficients[2], |
| 66 | $primes[1], |
| 67 | $primes[2], |
| 68 | $comment, |
| 69 | ] = Strings::unpackSSH2('i6s', $parsed['paddedKey']); |
| 70 | |
| 71 | $temp = $primes[1]->subtract($one); |
| 72 | $exponents = [1 => $publicExponent->modInverse($temp)]; |
| 73 | $temp = $primes[2]->subtract($one); |
| 74 | $exponents[] = $publicExponent->modInverse($temp); |
| 75 | |
| 76 | $isPublicKey = false; |
| 77 | |
| 78 | return compact('publicExponent', 'modulus', 'privateExponent', 'primes', 'coefficients', 'exponents', 'comment', 'isPublicKey'); |
| 79 | } |
| 80 | |
| 81 | [$publicExponent, $modulus] = Strings::unpackSSH2('ii', $parsed['publicKey']); |
| 82 | |
| 83 | return [ |
| 84 | 'isPublicKey' => true, |
| 85 | 'modulus' => $modulus, |
| 86 | 'publicExponent' => $publicExponent, |
| 87 | 'comment' => $parsed['comment'], |
| 88 | ]; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Convert a public key to the appropriate format |
| 93 | * |
| 94 | * @param array $options optional |
| 95 | */ |
| 96 | public static function savePublicKey(BigInteger $n, BigInteger $e, array $options = []): string |
| 97 | { |
| 98 | $RSAPublicKey = Strings::packSSH2('sii', 'ssh-rsa', $e, $n); |
| 99 | |
| 100 | if ($options['binary'] ?? self::$binary) { |
| 101 | return $RSAPublicKey; |
| 102 | } |
| 103 | |
| 104 | $comment = $options['comment'] ?? self::$comment; |
| 105 | $RSAPublicKey = 'ssh-rsa ' . base64_encode($RSAPublicKey) . ' ' . $comment; |
| 106 | |
| 107 | return $RSAPublicKey; |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Convert a private key to the appropriate format. |
| 112 | */ |
| 113 | public static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, array $primes, array $exponents, array $coefficients, ?string $password = null, array $options = []): string |
| 114 | { |
| 115 | $publicKey = self::savePublicKey($n, $e, ['binary' => true]); |
| 116 | $privateKey = Strings::packSSH2('si6', 'ssh-rsa', $n, $e, $d, $coefficients[2], $primes[1], $primes[2]); |
| 117 | |
| 118 | return self::wrapPrivateKey($publicKey, $privateKey, $password, $options); |
| 119 | } |
| 120 | } |
| 121 |