MSBLOB.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * Miccrosoft BLOB Formatted RSA Key Handler
  4. *
  5. * More info:
  6. *
  7. * https://msdn.microsoft.com/en-us/library/windows/desktop/aa375601(v=vs.85).aspx
  8. *
  9. * PHP version 5
  10. *
  11. * @category Crypt
  12. * @package RSA
  13. * @author Jim Wigginton <terrafrost@php.net>
  14. * @copyright 2015 Jim Wigginton
  15. * @license http://www.opensource.org/licenses/mit-license.html MIT License
  16. * @link http://phpseclib.sourceforge.net
  17. */
  18. namespace phpseclib\Crypt\RSA\Keys;
  19. use ParagonIE\ConstantTime\Base64;
  20. use phpseclib\Math\BigInteger;
  21. use phpseclib\Common\Functions\Strings;
  22. /**
  23. * Microsoft BLOB Formatted RSA Key Handler
  24. *
  25. * @package RSA
  26. * @author Jim Wigginton <terrafrost@php.net>
  27. * @access public
  28. */
  29. abstract class MSBLOB
  30. {
  31. /**#@+
  32. * @access private
  33. */
  34. /**
  35. * Public/Private Key Pair
  36. */
  37. const PRIVATEKEYBLOB = 0x7;
  38. /**
  39. * Public Key
  40. */
  41. const PUBLICKEYBLOB = 0x6;
  42. /**
  43. * Public Key
  44. */
  45. const PUBLICKEYBLOBEX = 0xA;
  46. /**
  47. * RSA public key exchange algorithm
  48. */
  49. const CALG_RSA_KEYX = 0x0000A400;
  50. /**
  51. * RSA public key exchange algorithm
  52. */
  53. const CALG_RSA_SIGN = 0x00002400;
  54. /**
  55. * Public Key
  56. */
  57. const RSA1 = 0x31415352;
  58. /**
  59. * Private Key
  60. */
  61. const RSA2 = 0x32415352;
  62. /**#@-*/
  63. /**
  64. * Break a public or private key down into its constituent components
  65. *
  66. * @access public
  67. * @param string $key
  68. * @param string $password optional
  69. * @return array
  70. */
  71. public static function load($key, $password = '')
  72. {
  73. if (!is_string($key)) {
  74. return false;
  75. }
  76. $key = Base64::decode($key);
  77. if (!is_string($key) || strlen($key) < 20) {
  78. return false;
  79. }
  80. // PUBLICKEYSTRUC publickeystruc
  81. // https://msdn.microsoft.com/en-us/library/windows/desktop/aa387453(v=vs.85).aspx
  82. extract(unpack('atype/aversion/vreserved/Valgo', Strings::shift($key, 8)));
  83. switch (ord($type)) {
  84. case self::PUBLICKEYBLOB:
  85. case self::PUBLICKEYBLOBEX:
  86. $publickey = true;
  87. break;
  88. case self::PRIVATEKEYBLOB:
  89. $publickey = false;
  90. break;
  91. default:
  92. return false;
  93. }
  94. $components = ['isPublicKey' => $publickey];
  95. // https://msdn.microsoft.com/en-us/library/windows/desktop/aa375549(v=vs.85).aspx
  96. switch ($algo) {
  97. case self::CALG_RSA_KEYX:
  98. case self::CALG_RSA_SIGN:
  99. break;
  100. default:
  101. return false;
  102. }
  103. // RSAPUBKEY rsapubkey
  104. // https://msdn.microsoft.com/en-us/library/windows/desktop/aa387685(v=vs.85).aspx
  105. // could do V for pubexp but that's unsigned 32-bit whereas some PHP installs only do signed 32-bit
  106. extract(unpack('Vmagic/Vbitlen/a4pubexp', Strings::shift($key, 12)));
  107. switch ($magic) {
  108. case self::RSA2:
  109. $components['isPublicKey'] = false;
  110. case self::RSA1:
  111. break;
  112. default:
  113. return false;
  114. }
  115. $baseLength = $bitlen / 16;
  116. if (strlen($key) != 2 * $baseLength && strlen($key) != 9 * $baseLength) {
  117. return false;
  118. }
  119. $components[$components['isPublicKey'] ? 'publicExponent' : 'privateExponent'] = new BigInteger(strrev($pubexp), 256);
  120. // BYTE modulus[rsapubkey.bitlen/8]
  121. $components['modulus'] = new BigInteger(strrev(Strings::shift($key, $bitlen / 8)), 256);
  122. if ($publickey) {
  123. return $components;
  124. }
  125. $components['isPublicKey'] = false;
  126. // BYTE prime1[rsapubkey.bitlen/16]
  127. $components['primes'] = [1 => new BigInteger(strrev(Strings::shift($key, $bitlen / 16)), 256)];
  128. // BYTE prime2[rsapubkey.bitlen/16]
  129. $components['primes'][] = new BigInteger(strrev(Strings::shift($key, $bitlen / 16)), 256);
  130. // BYTE exponent1[rsapubkey.bitlen/16]
  131. $components['exponents'] = [1 => new BigInteger(strrev(Strings::shift($key, $bitlen / 16)), 256)];
  132. // BYTE exponent2[rsapubkey.bitlen/16]
  133. $components['exponents'][] = new BigInteger(strrev(Strings::shift($key, $bitlen / 16)), 256);
  134. // BYTE coefficient[rsapubkey.bitlen/16]
  135. $components['coefficients'] = [2 => new BigInteger(strrev(Strings::shift($key, $bitlen / 16)), 256)];
  136. if (isset($components['privateExponent'])) {
  137. $components['publicExponent'] = $components['privateExponent'];
  138. }
  139. // BYTE privateExponent[rsapubkey.bitlen/8]
  140. $components['privateExponent'] = new BigInteger(strrev(Strings::shift($key, $bitlen / 8)), 256);
  141. return $components;
  142. }
  143. /**
  144. * Convert a private key to the appropriate format.
  145. *
  146. * @access public
  147. * @param \phpseclib\Math\BigInteger $n
  148. * @param \phpseclib\Math\BigInteger $e
  149. * @param \phpseclib\Math\BigInteger $d
  150. * @param array $primes
  151. * @param array $exponents
  152. * @param array $coefficients
  153. * @param string $password optional
  154. * @return string
  155. */
  156. public static function savePrivateKey($n, $e, $d, $primes, $exponents, $coefficients, $password = '')
  157. {
  158. if (count($primes) != 2) {
  159. throw new \InvalidArgumentException('MSBLOB does not support multi-prime RSA keys');
  160. }
  161. $n = strrev($n->toBytes());
  162. $e = str_pad(strrev($e->toBytes()), 4, "\0");
  163. $key = pack('aavV', chr(self::PRIVATEKEYBLOB), chr(2), 0, self::CALG_RSA_KEYX);
  164. $key.= pack('VVa*', self::RSA2, 8 * strlen($n), $e);
  165. $key.= $n;
  166. $key.= strrev($primes[1]->toBytes());
  167. $key.= strrev($primes[2]->toBytes());
  168. $key.= strrev($exponents[1]->toBytes());
  169. $key.= strrev($exponents[2]->toBytes());
  170. $key.= strrev($coefficients[1]->toBytes());
  171. $key.= strrev($d->toBytes());
  172. return Base64::encode($key);
  173. }
  174. /**
  175. * Convert a public key to the appropriate format
  176. *
  177. * @access public
  178. * @param \phpseclib\Math\BigInteger $n
  179. * @param \phpseclib\Math\BigInteger $e
  180. * @return string
  181. */
  182. public static function savePublicKey($n, $e)
  183. {
  184. $n = strrev($n->toBytes());
  185. $e = str_pad(strrev($e->toBytes()), 4, "\0");
  186. $key = pack('aavV', chr(self::PUBLICKEYBLOB), chr(2), 0, self::CALG_RSA_KEYX);
  187. $key.= pack('VVa*', self::RSA1, 8 * strlen($n), $e);
  188. $key.= $n;
  189. return Base64::encode($key);
  190. }
  191. }