[ Index ] |
PHP Cross Reference of DokuWiki |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * OpenSSH Formatted DSA 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\DSA\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 DSA 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-dss']; 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 $parsed = parent::load($key, $password); 46 47 if (isset($parsed['paddedKey'])) { 48 list($type) = Strings::unpackSSH2('s', $parsed['paddedKey']); 49 if ($type != $parsed['type']) { 50 throw new \RuntimeException("The public and private keys are not of the same type ($type vs $parsed[type])"); 51 } 52 53 list($p, $q, $g, $y, $x, $comment) = Strings::unpackSSH2('i5s', $parsed['paddedKey']); 54 55 return compact('p', 'q', 'g', 'y', 'x', 'comment'); 56 } 57 58 list($p, $q, $g, $y) = Strings::unpackSSH2('iiii', $parsed['publicKey']); 59 60 $comment = $parsed['comment']; 61 62 return compact('p', 'q', 'g', 'y', 'comment'); 63 } 64 65 /** 66 * Convert a public key to the appropriate format 67 * 68 * @param \phpseclib3\Math\BigInteger $p 69 * @param \phpseclib3\Math\BigInteger $q 70 * @param \phpseclib3\Math\BigInteger $g 71 * @param \phpseclib3\Math\BigInteger $y 72 * @param array $options optional 73 * @return string 74 */ 75 public static function savePublicKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y, array $options = []) 76 { 77 if ($q->getLength() != 160) { 78 throw new \InvalidArgumentException('SSH only supports keys with an N (length of Group Order q) of 160'); 79 } 80 81 // from <http://tools.ietf.org/html/rfc4253#page-15>: 82 // string "ssh-dss" 83 // mpint p 84 // mpint q 85 // mpint g 86 // mpint y 87 $DSAPublicKey = Strings::packSSH2('siiii', 'ssh-dss', $p, $q, $g, $y); 88 89 if (isset($options['binary']) ? $options['binary'] : self::$binary) { 90 return $DSAPublicKey; 91 } 92 93 $comment = isset($options['comment']) ? $options['comment'] : self::$comment; 94 $DSAPublicKey = 'ssh-dss ' . base64_encode($DSAPublicKey) . ' ' . $comment; 95 96 return $DSAPublicKey; 97 } 98 99 /** 100 * Convert a private key to the appropriate format. 101 * 102 * @param \phpseclib3\Math\BigInteger $p 103 * @param \phpseclib3\Math\BigInteger $q 104 * @param \phpseclib3\Math\BigInteger $g 105 * @param \phpseclib3\Math\BigInteger $y 106 * @param \phpseclib3\Math\BigInteger $x 107 * @param string $password optional 108 * @param array $options optional 109 * @return string 110 */ 111 public static function savePrivateKey(BigInteger $p, BigInteger $q, BigInteger $g, BigInteger $y, BigInteger $x, $password = '', array $options = []) 112 { 113 $publicKey = self::savePublicKey($p, $q, $g, $y, ['binary' => true]); 114 $privateKey = Strings::packSSH2('si5', 'ssh-dss', $p, $q, $g, $y, $x); 115 116 return self::wrapPrivateKey($publicKey, $privateKey, $password, $options); 117 } 118 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body