* @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 ParagonIE\ConstantTime\Base64; use phpseclib\Math\BigInteger; /** * XML Formatted RSA Key Handler * * @package RSA * @author Jim Wigginton * @access public */ class XML { /** * 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 = '') { if (!is_string($key)) { return false; } $components = array( 'isPublicKey' => false, 'primes' => array(), 'exponents' => array(), 'coefficients' => array() ); $use_errors = libxml_use_internal_errors(true); $dom = new \DOMDocument(); if (!$dom->loadXML('' . $key . '')) { return false; } $xpath = new \DOMXPath($dom); $keys = array('modulus', 'exponent', 'p', 'q', 'dp', 'dq', 'inverseq', 'd'); foreach ($keys as $key) { // $dom->getElementsByTagName($key) is case-sensitive $temp = $xpath->query("//*[translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz')='$key']"); if (!$temp->length) { continue; } $value = new BigInteger(Base64::decode($temp->item(0)->nodeValue), 256); switch ($key) { case 'modulus': $components['modulus'] = $value; break; case 'exponent': $components['publicExponent'] = $value; break; case 'p': $components['primes'][1] = $value; break; case 'q': $components['primes'][2] = $value; break; case 'dp': $components['exponents'][1] = $value; break; case 'dq': $components['exponents'][2] = $value; break; case 'inverseq': $components['coefficients'][2] = $value; break; case 'd': $components['privateExponent'] = $value; } } libxml_use_internal_errors($use_errors); return isset($components['modulus']) && isset($components['publicExponent']) ? $components : false; } /** * Convert a private key to the appropriate format. * * @access public * @param \phpseclib\Math\BigInteger $n * @param \phpseclib\Math\BigInteger $e * @param \phpseclib\Math\BigInteger $d * @param array $primes * @param array $exponents * @param array $coefficients * @param string $password optional * @return string */ static function savePrivateKey(BigInteger $n, BigInteger $e, BigInteger $d, $primes, $exponents, $coefficients, $password = '') { if (count($primes) != 2) { return false; } return "\r\n" . ' ' . Base64::encode($n->toBytes()) . "\r\n" . ' ' . Base64::encode($e->toBytes()) . "\r\n" . '

' . Base64::encode($primes[1]->toBytes()) . "

\r\n" . ' ' . Base64::encode($primes[2]->toBytes()) . "\r\n" . ' ' . Base64::encode($exponents[1]->toBytes()) . "\r\n" . ' ' . Base64::encode($exponents[2]->toBytes()) . "\r\n" . ' ' . Base64::encode($coefficients[2]->toBytes()) . "\r\n" . ' ' . Base64::encode($d->toBytes()) . "\r\n" . '
'; } /** * Convert a public key to the appropriate format * * @access public * @param \phpseclib\Math\BigInteger $n * @param \phpseclib\Math\BigInteger $e * @return string */ static function savePublicKey(BigInteger $n, BigInteger $e) { return "\r\n" . ' ' . Base64::encode($n->toBytes()) . "\r\n" . ' ' . Base64::encode($e->toBytes()) . "\r\n" . ''; } }