Skip to content

Commit

Permalink
RSA: move key format handling to plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
terrafrost committed Oct 1, 2015
1 parent 1f47dae commit ec3fe72
Show file tree
Hide file tree
Showing 16 changed files with 1,768 additions and 1,161 deletions.
1,392 changes: 270 additions & 1,122 deletions phpseclib/Crypt/RSA.php

Large diffs are not rendered by default.

142 changes: 142 additions & 0 deletions phpseclib/Crypt/RSA/OpenSSH.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php
/**
* OpenSSH Formatted RSA Key Handler
*
* PHP version 5
*
* Place in $HOME/.ssh/authorized_keys
*
* @category Crypt
* @package RSA
* @author Jim Wigginton <terrafrost@php.net>
* @copyright 2015 Jim Wigginton
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link http://phpseclib.sourceforge.net
*/

namespace phpseclib\Crypt\RSA;

use phpseclib\Math\BigInteger;

/**
* XML Formatted RSA Key Handler
*
* @package RSA
* @author Jim Wigginton <terrafrost@php.net>
* @access public
*/
class OpenSSH
{
/**
* Default comment
*
* @var string
* @access private
*/
static $comment = 'phpseclib-generated-key';

/**
* Sets the default comment
*
* @access public
* @param string $comment
*/
static function setEncryptionAlgorithm($comment)
{
self::$comment = $comment;
}

/**
* Break a public or private key down into its constituent components
*
* @access public
* @param string $key
* @param string $password optional
* @return array
*/
static function load($key, $password = '')
{
$parts = explode(' ', $key, 3);

$key = isset($parts[1]) ? base64_decode($parts[1]) : false;
if ($key === false) {
return false;
}

$comment = isset($parts[2]) ? $parts[2] : false;

$cleanup = substr($key, 0, 11) == "\0\0\0\7ssh-rsa";

if (strlen($key) <= 4) {
return false;
}
extract(unpack('Nlength', self::_string_shift($key, 4)));
$publicExponent = new BigInteger(self::_string_shift($key, $length), -256);
if (strlen($key) <= 4) {
return false;
}
extract(unpack('Nlength', self::_string_shift($key, 4)));
$modulus = new BigInteger(self::_string_shift($key, $length), -256);

if ($cleanup && strlen($key)) {
if (strlen($key) <= 4) {
return false;
}
extract(unpack('Nlength', self::_string_shift($key, 4)));
$realModulus = new BigInteger(self::_string_shift($key, $length), -256);
return strlen($key) ? false : array(
'isPublicKey' => true,
'modulus' => $realModulus,
'publicExponent' => $modulus,
'comment' => $comment
);
} else {
return strlen($key) ? false : array(
'isPublicKey' => true,
'modulus' => $modulus,
'publicExponent' => $publicExponent,
'comment' => $comment
);
}
}

/**
* Convert a public key to the appropriate format
*
* @access public
* @param \phpseclib\Math\BigInteger $n
* @param \phpseclib\Math\BigInteger $e
* @return string
*/
function savePublicKey(BigInteger $n, BigInteger $e)
{
$publicExponent = $e->toBytes(true);
$modulus = $n->toBytes(true);

// from <http://tools.ietf.org/html/rfc4253#page-15>:
// string "ssh-rsa"
// mpint e
// mpint n
$RSAPublicKey = pack('Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($publicExponent), $publicExponent, strlen($modulus), $modulus);
$RSAPublicKey = 'ssh-rsa ' . base64_encode($RSAPublicKey) . ' ' . self::$comment;

return $RSAPublicKey;
}

/**
* String Shift
*
* Inspired by array_shift
*
* @param string $string
* @param int $index
* @return string
* @access private
*/
static function _string_shift(&$string, $index = 1)
{
$substr = substr($string, 0, $index);
$string = substr($string, $index);
return $substr;
}
}
Loading

0 comments on commit ec3fe72

Please sign in to comment.