[ Index ] |
PHP Cross Reference of DokuWiki |
[Summary view] [Print] [Text view]
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 namespace phpseclib3\Crypt\RSA\Formats\Keys; 17 18 use phpseclib3\Common\Functions\Strings; 19 use phpseclib3\Crypt\Common\Formats\Keys\OpenSSH as Progenitor; 20 use phpseclib3\Math\BigInteger; 21 22 /** 23 * OpenSSH Formatted RSA Key Handler 24 * 25 * @author Jim Wigginton <terrafrost@php.net> 26 */ 27 abstract class OpenSSH extends Progenitor 28 { 29 /** 30 * Supported Key Types 31 * 32 * @var array 33 */ 34 protected static $types = ['ssh-rsa']; 35 36 /** 37 * Break a public or private key down into its constituent components 38 * 39 * @param string $key 40 * @param string $password optional 41 * @return array 42 */ 43 public static function load($key, $password = '') 44 { 45 static $one; 46 if (!isset($one)) { 47 $one = new BigInteger(1); 48 } 49 50 $parsed = parent::load($key, $password); 51 52 if (isset($parsed['paddedKey'])) { 53 list($type) = Strings::unpackSSH2('s', $parsed['paddedKey']); 54 if ($type != $parsed['type']) { 55 throw new \RuntimeException("The public and private keys are not of the same type ($type vs $parsed[type])"); 56 } 57 58 $primes = $coefficients = []; 59 60 list( 61 $modulus, 62 $publicExponent, 63 $privateExponent, 64 $coefficients[2], 65 $primes[1], 66 $primes[2], 67 $comment, 68 ) = Strings::unpackSSH2('i6s', $parsed['paddedKey']); 69 70 $temp = $primes[1]->subtract($one); 71 $exponents = [1 => $publicExponent->modInverse($temp)]; 72 $temp = $primes[2]->subtract($one); 73 $exponents[] = $publicExponent->modInverse($temp); 74 75 $isPublicKey = false; 76 77 return compact('publicExponent', 'modulus', 'privateExponent', 'primes', 'coefficients', 'exponents', 'comment', 'isPublicKey'); 78 } 79 80 list($publicExponent, $modulus) = Strings::unpackSSH2('ii', $parsed['publicKey']); 81 82 return [ 83 'isPublicKey' => true, 84 'modulus' => $modulus, 85 'publicExponent' => $publicExponent, 86 'comment' => $parsed['comment'] 87 ]; 88 } 89 90 /** 91 * Convert a public key to the appropriate format 92 * 93 * @param \phpseclib3\Math\BigInteger $n 94 * @param \phpseclib3\Math\BigInteger $e 95 * @param array $options optional 96 * @return string 97 */ 98 public static function savePublicKey(BigInteger $n, BigInteger $e, array $options = []) 99 { 100 $RSAPublicKey = Strings::packSSH2('sii', 'ssh-rsa', $e, $n); 101 102 if (isset($options['binary']) ? $options['binary'] : self::$binary) { 103 return $RSAPublicKey; 104 } 105 106 $comment = isset($options['comment']) ? $options['comment'] : self::$comment; 107 $RSAPublicKey = 'ssh-rsa ' . base64_encode($RSAPublicKey) . ' ' . $comment; 108 109 return $RSAPublicKey; 110 } 111 112 /** 113 * Convert a private key to the appropriate format. 114 * 115 * @param \phpseclib3\Math\BigInteger $n 116 * @param \phpseclib3\Math\BigInteger $e 117 * @param \phpseclib3\Math\BigInteger $d 118 * @param array $primes 119 * @param array $exponents 120 * @param array $coefficients 121 * @param string $password optional 122 * @param array $options optional 123 * @return string 124 */ 125 public static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, array $primes, array $exponents, array $coefficients, $password = '', array $options = []) 126 { 127 $publicKey = self::savePublicKey($n, $e, ['binary' => true]); 128 $privateKey = Strings::packSSH2('si6', 'ssh-rsa', $n, $e, $d, $coefficients[2], $primes[1], $primes[2]); 129 130 return self::wrapPrivateKey($publicKey, $privateKey, $password, $options); 131 } 132 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body