PageRenderTime 59ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/php5-3/core/tools/secure/crypt/RSA.class.php

https://bitbucket.org/ronaldobrandini/framework
PHP | 2652 lines | 1477 code | 282 blank | 893 comment | 235 complexity | 55deb97840ebe742c5152ac7fd5c73d9 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. namespace core\tools\secure\crypt;
  3. /*
  4. * To change this license header, choose License Headers in Project Properties.
  5. * To change this template file, choose Tools | Templates
  6. * and open the template in the editor.
  7. */
  8. /**
  9. * Description of RSA
  10. *
  11. * @author ronaldo.silva
  12. * @version 1.0 22/08/2014
  13. */
  14. class RSA{
  15. /**
  16. * Precomputed Zero
  17. *
  18. * @var Array
  19. * @access private
  20. */
  21. private $zero;
  22. /**
  23. * Precomputed One
  24. *
  25. * @var Array
  26. * @access private
  27. */
  28. private $one;
  29. /**
  30. * Private Key Format
  31. *
  32. * @var Integer
  33. * @access private
  34. */
  35. private $privateKeyFormat = CRYPT_RSA_PRIVATE_FORMAT_PKCS1;
  36. /**
  37. * Public Key Format
  38. *
  39. * @var Integer
  40. * @access public
  41. */
  42. public $publicKeyFormat = CRYPT_RSA_PUBLIC_FORMAT_PKCS8;
  43. /**
  44. * Modulus (ie. n)
  45. *
  46. * @var \core\tools\secure\math\BigInteger
  47. * @access private
  48. */
  49. private $modulus;
  50. /**
  51. * Modulus length
  52. *
  53. * @var \core\tools\secure\math\BigInteger
  54. * @access private
  55. */
  56. private $k;
  57. /**
  58. * Exponent (ie. e or d)
  59. *
  60. * @var \core\tools\secure\math\BigInteger
  61. * @access private
  62. */
  63. private $exponent;
  64. /**
  65. * Primes for Chinese Remainder Theorem (ie. p and q)
  66. *
  67. * @var Array
  68. * @access private
  69. */
  70. private $primes;
  71. /**
  72. * Exponents for Chinese Remainder Theorem (ie. dP and dQ)
  73. *
  74. * @var Array
  75. * @access private
  76. */
  77. private $exponents;
  78. /**
  79. * Coefficients for Chinese Remainder Theorem (ie. qInv)
  80. *
  81. * @var Array
  82. * @access private
  83. */
  84. private $coefficients;
  85. /**
  86. * Hash name
  87. *
  88. * @var String
  89. * @access private
  90. */
  91. private $hashName;
  92. /**
  93. * Hash function
  94. *
  95. * @var Hash
  96. * @access private
  97. */
  98. private $hash;
  99. /**
  100. * Length of hash function output
  101. *
  102. * @var Integer
  103. * @access private
  104. */
  105. private $hLen;
  106. /**
  107. * Length of salt
  108. *
  109. * @var Integer
  110. * @access private
  111. */
  112. private $sLen;
  113. /**
  114. * Hash function for the Mask Generation Function
  115. *
  116. * @var Hash
  117. * @access private
  118. */
  119. private $mgfHash;
  120. /**
  121. * Length of MGF hash function output
  122. *
  123. * @var Integer
  124. * @access private
  125. */
  126. private $mgfHLen;
  127. /**
  128. * Encryption mode
  129. *
  130. * @var Integer
  131. * @access private
  132. */
  133. private $encryptionMode = CRYPT_RSA_ENCRYPTION_OAEP;
  134. /**
  135. * Signature mode
  136. *
  137. * @var Integer
  138. * @access private
  139. */
  140. private $signatureMode = CRYPT_RSA_SIGNATURE_PSS;
  141. /**
  142. * Public Exponent
  143. *
  144. * @var Mixed
  145. * @access private
  146. */
  147. private $publicExponent = false;
  148. /**
  149. * Password
  150. *
  151. * @var String
  152. * @access private
  153. */
  154. private $password = false;
  155. /**
  156. * Components
  157. *
  158. * For use with parsing XML formatted keys. PHP's XML Parser functions use utilized - instead of PHP's DOM functions -
  159. * because PHP's XML Parser functions work on PHP4 whereas PHP's DOM functions - although surperior - don't.
  160. *
  161. * @see Crypt_RSA::_start_element_handler()
  162. * @var Array
  163. * @access private
  164. */
  165. private $components = array();
  166. /**
  167. * Current String
  168. *
  169. * For use with parsing XML formatted keys.
  170. *
  171. * @see Crypt_RSA::_character_handler()
  172. * @see Crypt_RSA::_stop_element_handler()
  173. * @var Mixed
  174. * @access private
  175. */
  176. private $current;
  177. /**
  178. * OpenSSL configuration file name.
  179. *
  180. * Set to null to use system configuration file.
  181. * @see Crypt_RSA::createKey()
  182. * @var Mixed
  183. * @Access public
  184. */
  185. public $configFile;
  186. /**
  187. * Public key comment field.
  188. *
  189. * @var String
  190. * @access private
  191. */
  192. private $comment = 'phpseclib-generated-key';
  193. /**
  194. * The constructor
  195. *
  196. * If you want to make use of the openssl extension, you'll need to set the mode manually, yourself. The reason
  197. * Crypt_RSA doesn't do it is because OpenSSL doesn't fail gracefully. openssl_pkey_new(), in particular, requires
  198. * openssl.cnf be present somewhere and, unfortunately, the only real way to find out is too late.
  199. *
  200. * @return Crypt_RSA
  201. * @access public
  202. */
  203. public function __construct(){
  204. $this->configFile = CRYPT_RSA_OPENSSL_CONFIG;
  205. if(!defined('CRYPT_RSA_MODE')){
  206. // Math/\core\tools\secure\math\BigInteger's openssl requirements are a little less stringent than Crypt/RSA's. in particular,
  207. // Math/\core\tools\secure\math\BigInteger doesn't require an openssl.cfg file whereas Crypt/RSA does. so if Math/\core\tools\secure\math\BigInteger
  208. // can't use OpenSSL it can be pretty trivially assumed, then, that Crypt/RSA can't either.
  209. if(defined('MATH_BIGINTEGER_OPENSSL_DISABLE')){
  210. define('CRYPT_RSA_MODE', CRYPT_RSA_MODE_INTERNAL);
  211. }
  212. switch(!defined('CRYPT_RSA_MODE')){ // ie. only run this if the above didn't set CRYPT_RSA_MODE already
  213. // openssl_pkey_get_details - which is used in the only place Crypt/RSA.php uses OpenSSL - was introduced in PHP 5.2.0
  214. case!function_exists('openssl_pkey_get_details'):
  215. define('CRYPT_RSA_MODE', CRYPT_RSA_MODE_INTERNAL);
  216. break;
  217. case extension_loaded('openssl') && version_compare(PHP_VERSION, '4.2.0', '>=') && file_exists($this->configFile):
  218. // some versions of XAMPP have mismatched versions of OpenSSL which causes it not to work
  219. ob_start();
  220. @phpinfo();
  221. $content = ob_get_contents();
  222. ob_end_clean();
  223. preg_match_all('#OpenSSL (Header|Library) Version(.*)#im', $content, $matches);
  224. $versions = array();
  225. if(!empty($matches[1])){
  226. for($i = 0; $i < count($matches[1]); $i++){
  227. $versions[$matches[1][$i]] = trim(str_replace('=>', '', strip_tags($matches[2][$i])));
  228. }
  229. }
  230. // it doesn't appear that OpenSSL versions were reported upon until PHP 5.3+
  231. switch(true){
  232. case!isset($versions['Header']):
  233. case!isset($versions['Library']):
  234. case $versions['Header'] == $versions['Library']:
  235. define('CRYPT_RSA_MODE', CRYPT_RSA_MODE_OPENSSL);
  236. break;
  237. default:
  238. define('CRYPT_RSA_MODE', CRYPT_RSA_MODE_INTERNAL);
  239. define('MATH_BIGINTEGER_OPENSSL_DISABLE', true);
  240. }
  241. break;
  242. case true:
  243. define('CRYPT_RSA_MODE', CRYPT_RSA_MODE_INTERNAL);
  244. }
  245. }
  246. $this->zero = new \core\tools\secure\math\BigInteger();
  247. $this->one = new \core\tools\secure\math\BigInteger(1);
  248. $this->hash = new Hash('sha1');
  249. $this->hLen = $this->hash->getLength();
  250. $this->hashName = 'sha1';
  251. $this->mgfHash = new Hash('sha1');
  252. $this->mgfHLen = $this->mgfHash->getLength();
  253. }
  254. /**
  255. * Create public / private key pair
  256. *
  257. * Returns an array with the following three elements:
  258. * - 'privatekey': The private key.
  259. * - 'publickey': The public key.
  260. * - 'partialkey': A partially computed key (if the execution time exceeded $timeout).
  261. * Will need to be passed back to Crypt_RSA::createKey() as the third parameter for further processing.
  262. *
  263. * @access public
  264. * @param optional Integer $bits
  265. * @param optional Integer $timeout
  266. * @param optional \core\tools\secure\math\BigInteger $p
  267. */
  268. public function createKey($bits = 1024, $timeout = false, $partial = array()){
  269. if(!defined('CRYPT_RSA_EXPONENT')){
  270. // http://en.wikipedia.org/wiki/65537_%28number%29
  271. define('CRYPT_RSA_EXPONENT', '65537');
  272. }
  273. // per <http://cseweb.ucsd.edu/~hovav/dist/survey.pdf#page=5>, this number ought not result in primes smaller
  274. // than 256 bits. as a consequence if the key you're trying to create is 1024 bits and you've set CRYPT_RSA_SMALLEST_PRIME
  275. // to 384 bits then you're going to get a 384 bit prime and a 640 bit prime (384 + 1024 % 384). at least if
  276. // CRYPT_RSA_MODE is set to CRYPT_RSA_MODE_INTERNAL. if CRYPT_RSA_MODE is set to CRYPT_RSA_MODE_OPENSSL then
  277. // CRYPT_RSA_SMALLEST_PRIME is ignored (ie. multi-prime RSA support is more intended as a way to speed up RSA key
  278. // generation when there's a chance neither gmp nor OpenSSL are installed)
  279. if(!defined('CRYPT_RSA_SMALLEST_PRIME')){
  280. define('CRYPT_RSA_SMALLEST_PRIME', 4096);
  281. }
  282. // OpenSSL uses 65537 as the exponent and requires RSA keys be 384 bits minimum
  283. if(CRYPT_RSA_MODE == CRYPT_RSA_MODE_OPENSSL && $bits >= 384 && CRYPT_RSA_EXPONENT == 65537){
  284. $config = array();
  285. if(isset($this->configFile)){
  286. $config['config'] = $this->configFile;
  287. }
  288. $rsa = openssl_pkey_new(array('private_key_bits' => $bits) + $config);
  289. openssl_pkey_export($rsa, $privatekey, null, $config);
  290. $publickey = openssl_pkey_get_details($rsa);
  291. $publickey = $publickey['key'];
  292. $privatekey = call_user_func_array(array($this, '_convertPrivateKey'), array_values($this->_parseKey($privatekey, CRYPT_RSA_PRIVATE_FORMAT_PKCS1)));
  293. $publickey = call_user_func_array(array($this, '_convertPublicKey'), array_values($this->_parseKey($publickey, CRYPT_RSA_PUBLIC_FORMAT_PKCS1)));
  294. // clear the buffer of error strings stemming from a minimalistic openssl.cnf
  295. while(openssl_error_string() !== false);
  296. return array(
  297. 'privatekey' => $privatekey,
  298. 'publickey' => $publickey,
  299. 'partialkey' => false
  300. );
  301. }
  302. static $e;
  303. if(!isset($e)){
  304. $e = new \core\tools\secure\math\BigInteger(CRYPT_RSA_EXPONENT);
  305. }
  306. extract($this->_generateMinMax($bits));
  307. $absoluteMin = $min;
  308. $temp = $bits >> 1; // divide by two to see how many bits P and Q would be
  309. if($temp > CRYPT_RSA_SMALLEST_PRIME){
  310. $num_primes = floor($bits / CRYPT_RSA_SMALLEST_PRIME);
  311. $temp = CRYPT_RSA_SMALLEST_PRIME;
  312. }else{
  313. $num_primes = 2;
  314. }
  315. extract($this->_generateMinMax($temp + $bits % $temp));
  316. $finalMax = $max;
  317. extract($this->_generateMinMax($temp));
  318. $generator = new \core\tools\secure\math\BigInteger();
  319. $n = $this->one->copy();
  320. if(!empty($partial)){
  321. extract(unserialize($partial));
  322. }else{
  323. $exponents = $coefficients = $primes = array();
  324. $lcm = array(
  325. 'top' => $this->one->copy(),
  326. 'bottom' => false
  327. );
  328. }
  329. $start = time();
  330. $i0 = count($primes) + 1;
  331. do{
  332. for($i = $i0; $i <= $num_primes; $i++){
  333. if($timeout !== false){
  334. $timeout-= time() - $start;
  335. $start = time();
  336. if($timeout <= 0){
  337. return array(
  338. 'privatekey' => '',
  339. 'publickey' => '',
  340. 'partialkey' => serialize(array(
  341. 'primes' => $primes,
  342. 'coefficients' => $coefficients,
  343. 'lcm' => $lcm,
  344. 'exponents' => $exponents
  345. ))
  346. );
  347. }
  348. }
  349. if($i == $num_primes){
  350. list($min, $temp) = $absoluteMin->divide($n);
  351. if(!$temp->equals($this->zero)){
  352. $min = $min->add($this->one); // ie. ceil()
  353. }
  354. $primes[$i] = $generator->randomPrime($min, $finalMax, $timeout);
  355. }else{
  356. $primes[$i] = $generator->randomPrime($min, $max, $timeout);
  357. }
  358. if($primes[$i] === false){ // if we've reached the timeout
  359. if(count($primes) > 1){
  360. $partialkey = '';
  361. }else{
  362. array_pop($primes);
  363. $partialkey = serialize(array(
  364. 'primes' => $primes,
  365. 'coefficients' => $coefficients,
  366. 'lcm' => $lcm,
  367. 'exponents' => $exponents
  368. ));
  369. }
  370. return array(
  371. 'privatekey' => '',
  372. 'publickey' => '',
  373. 'partialkey' => $partialkey
  374. );
  375. }
  376. // the first coefficient is calculated differently from the rest
  377. // ie. instead of being $primes[1]->modInverse($primes[2]), it's $primes[2]->modInverse($primes[1])
  378. if($i > 2){
  379. $coefficients[$i] = $n->modInverse($primes[$i]);
  380. }
  381. $n = $n->multiply($primes[$i]);
  382. $temp = $primes[$i]->subtract($this->one);
  383. // textbook RSA implementations use Euler's totient function instead of the least common multiple.
  384. // see http://en.wikipedia.org/wiki/Euler%27s_totient_function
  385. $lcm['top'] = $lcm['top']->multiply($temp);
  386. $lcm['bottom'] = $lcm['bottom'] === false ? $temp : $lcm['bottom']->gcd($temp);
  387. $exponents[$i] = $e->modInverse($temp);
  388. }
  389. list($temp) = $lcm['top']->divide($lcm['bottom']);
  390. $gcd = $temp->gcd($e);
  391. $i0 = 1;
  392. }while(!$gcd->equals($this->one));
  393. $d = $e->modInverse($temp);
  394. $coefficients[2] = $primes[2]->modInverse($primes[1]);
  395. // from <http://tools.ietf.org/html/rfc3447#appendix-A.1.2>:
  396. // RSAPrivateKey ::= SEQUENCE {
  397. // version Version,
  398. // modulus INTEGER, -- n
  399. // publicExponent INTEGER, -- e
  400. // privateExponent INTEGER, -- d
  401. // prime1 INTEGER, -- p
  402. // prime2 INTEGER, -- q
  403. // exponent1 INTEGER, -- d mod (p-1)
  404. // exponent2 INTEGER, -- d mod (q-1)
  405. // coefficient INTEGER, -- (inverse of q) mod p
  406. // otherPrimeInfos OtherPrimeInfos OPTIONAL
  407. // }
  408. return array(
  409. 'privatekey' => $this->_convertPrivateKey($n, $e, $d, $primes, $exponents, $coefficients),
  410. 'publickey' => $this->_convertPublicKey($n, $e),
  411. 'partialkey' => false
  412. );
  413. }
  414. /**
  415. * Convert a private key to the appropriate format.
  416. *
  417. * @access private
  418. * @see setPrivateKeyFormat()
  419. * @param String $RSAPrivateKey
  420. * @return String
  421. */
  422. private function _convertPrivateKey($n, $e, $d, $primes, $exponents, $coefficients){
  423. $num_primes = count($primes);
  424. $raw = array(
  425. 'version' => $num_primes == 2 ? chr(0) : chr(1), // two-prime vs. multi
  426. 'modulus' => $n->toBytes(true),
  427. 'publicExponent' => $e->toBytes(true),
  428. 'privateExponent' => $d->toBytes(true),
  429. 'prime1' => $primes[1]->toBytes(true),
  430. 'prime2' => $primes[2]->toBytes(true),
  431. 'exponent1' => $exponents[1]->toBytes(true),
  432. 'exponent2' => $exponents[2]->toBytes(true),
  433. 'coefficient' => $coefficients[2]->toBytes(true)
  434. );
  435. // if the format in question does not support multi-prime rsa and multi-prime rsa was used,
  436. // call _convertPublicKey() instead.
  437. switch($this->privateKeyFormat){
  438. case CRYPT_RSA_PRIVATE_FORMAT_XML:
  439. if($num_primes != 2){
  440. return false;
  441. }
  442. return "<RSAKeyValue>\r\n" .
  443. ' <Modulus>' . base64_encode($raw['modulus']) . "</Modulus>\r\n" .
  444. ' <Exponent>' . base64_encode($raw['publicExponent']) . "</Exponent>\r\n" .
  445. ' <P>' . base64_encode($raw['prime1']) . "</P>\r\n" .
  446. ' <Q>' . base64_encode($raw['prime2']) . "</Q>\r\n" .
  447. ' <DP>' . base64_encode($raw['exponent1']) . "</DP>\r\n" .
  448. ' <DQ>' . base64_encode($raw['exponent2']) . "</DQ>\r\n" .
  449. ' <InverseQ>' . base64_encode($raw['coefficient']) . "</InverseQ>\r\n" .
  450. ' <D>' . base64_encode($raw['privateExponent']) . "</D>\r\n" .
  451. '</RSAKeyValue>';
  452. break;
  453. case CRYPT_RSA_PRIVATE_FORMAT_PUTTY:
  454. if($num_primes != 2){
  455. return false;
  456. }
  457. $key = "PuTTY-User-Key-File-2: ssh-rsa\r\nEncryption: ";
  458. $encryption = (!empty($this->password) || is_string($this->password)) ? 'aes256-cbc' : 'none';
  459. $key.= $encryption;
  460. $key.= "\r\nComment: " . $this->comment . "\r\n";
  461. $public = pack('Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($raw['publicExponent']), $raw['publicExponent'], strlen($raw['modulus']), $raw['modulus']
  462. );
  463. $source = pack('Na*Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($encryption), $encryption, strlen($this->comment), $this->comment, strlen($public), $public
  464. );
  465. $public = base64_encode($public);
  466. $key.= "Public-Lines: " . ((strlen($public) + 63) >> 6) . "\r\n";
  467. $key.= chunk_split($public, 64);
  468. $private = pack('Na*Na*Na*Na*', strlen($raw['privateExponent']), $raw['privateExponent'], strlen($raw['prime1']), $raw['prime1'], strlen($raw['prime2']), $raw['prime2'], strlen($raw['coefficient']), $raw['coefficient']
  469. );
  470. if(empty($this->password) && !is_string($this->password)){
  471. $source.= pack('Na*', strlen($private), $private);
  472. $hashkey = 'putty-private-key-file-mac-key';
  473. }else{
  474. $private.= crypt_random_string(16 - (strlen($private) & 15));
  475. $source.= pack('Na*', strlen($private), $private);
  476. if(!class_exists('Crypt_AES')){
  477. include_once 'Crypt/AES.php';
  478. }
  479. $sequence = 0;
  480. $symkey = '';
  481. while(strlen($symkey) < 32){
  482. $temp = pack('Na*', $sequence++, $this->password);
  483. $symkey.= pack('H*', sha1($temp));
  484. }
  485. $symkey = substr($symkey, 0, 32);
  486. $crypto = new Crypt_AES();
  487. $crypto->setKey($symkey);
  488. $crypto->disablePadding();
  489. $private = $crypto->encrypt($private);
  490. $hashkey = 'putty-private-key-file-mac-key' . $this->password;
  491. }
  492. $private = base64_encode($private);
  493. $key.= 'Private-Lines: ' . ((strlen($private) + 63) >> 6) . "\r\n";
  494. $key.= chunk_split($private, 64);
  495. if(!class_exists('Hash')){
  496. include_once 'Crypt/Hash.php';
  497. }
  498. $hash = new Hash('sha1');
  499. $hash->setKey(pack('H*', sha1($hashkey)));
  500. $key.= 'Private-MAC: ' . bin2hex($hash->hash($source)) . "\r\n";
  501. return $key;
  502. default: // eg. CRYPT_RSA_PRIVATE_FORMAT_PKCS1
  503. $components = array();
  504. foreach($raw as $name => $value){
  505. $components[$name] = pack('Ca*a*', CRYPT_RSA_ASN1_INTEGER, $this->_encodeLength(strlen($value)), $value);
  506. }
  507. $RSAPrivateKey = implode('', $components);
  508. if($num_primes > 2){
  509. $OtherPrimeInfos = '';
  510. for($i = 3; $i <= $num_primes; $i++){
  511. // OtherPrimeInfos ::= SEQUENCE SIZE(1..MAX) OF OtherPrimeInfo
  512. //
  513. // OtherPrimeInfo ::= SEQUENCE {
  514. // prime INTEGER, -- ri
  515. // exponent INTEGER, -- di
  516. // coefficient INTEGER -- ti
  517. // }
  518. $OtherPrimeInfo = pack('Ca*a*', CRYPT_RSA_ASN1_INTEGER, $this->_encodeLength(strlen($primes[$i]->toBytes(true))), $primes[$i]->toBytes(true));
  519. $OtherPrimeInfo.= pack('Ca*a*', CRYPT_RSA_ASN1_INTEGER, $this->_encodeLength(strlen($exponents[$i]->toBytes(true))), $exponents[$i]->toBytes(true));
  520. $OtherPrimeInfo.= pack('Ca*a*', CRYPT_RSA_ASN1_INTEGER, $this->_encodeLength(strlen($coefficients[$i]->toBytes(true))), $coefficients[$i]->toBytes(true));
  521. $OtherPrimeInfos.= pack('Ca*a*', CRYPT_RSA_ASN1_SEQUENCE, $this->_encodeLength(strlen($OtherPrimeInfo)), $OtherPrimeInfo);
  522. }
  523. $RSAPrivateKey.= pack('Ca*a*', CRYPT_RSA_ASN1_SEQUENCE, $this->_encodeLength(strlen($OtherPrimeInfos)), $OtherPrimeInfos);
  524. }
  525. $RSAPrivateKey = pack('Ca*a*', CRYPT_RSA_ASN1_SEQUENCE, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey);
  526. if($this->privateKeyFormat == CRYPT_RSA_PRIVATE_FORMAT_PKCS8){
  527. $rsaOID = pack('H*', '300d06092a864886f70d0101010500'); // hex version of MA0GCSqGSIb3DQEBAQUA
  528. $RSAPrivateKey = pack('Ca*a*Ca*a*', CRYPT_RSA_ASN1_INTEGER, "\01\00", $rsaOID, 4, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey
  529. );
  530. $RSAPrivateKey = pack('Ca*a*', CRYPT_RSA_ASN1_SEQUENCE, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey);
  531. if(!empty($this->password) || is_string($this->password)){
  532. $salt = crypt_random_string(8);
  533. $iterationCount = 2048;
  534. if(!class_exists('Crypt_DES')){
  535. include_once 'Crypt/DES.php';
  536. }
  537. $crypto = new Crypt_DES();
  538. $crypto->setPassword($this->password, 'pbkdf1', 'md5', $salt, $iterationCount);
  539. $RSAPrivateKey = $crypto->encrypt($RSAPrivateKey);
  540. $parameters = pack('Ca*a*Ca*N', CRYPT_RSA_ASN1_OCTETSTRING, $this->_encodeLength(strlen($salt)), $salt, CRYPT_RSA_ASN1_INTEGER, $this->_encodeLength(4), $iterationCount
  541. );
  542. $pbeWithMD5AndDES_CBC = "\x2a\x86\x48\x86\xf7\x0d\x01\x05\x03";
  543. $encryptionAlgorithm = pack('Ca*a*Ca*a*', CRYPT_RSA_ASN1_OBJECT, $this->_encodeLength(strlen($pbeWithMD5AndDES_CBC)), $pbeWithMD5AndDES_CBC, CRYPT_RSA_ASN1_SEQUENCE, $this->_encodeLength(strlen($parameters)), $parameters
  544. );
  545. $RSAPrivateKey = pack('Ca*a*Ca*a*', CRYPT_RSA_ASN1_SEQUENCE, $this->_encodeLength(strlen($encryptionAlgorithm)), $encryptionAlgorithm, CRYPT_RSA_ASN1_OCTETSTRING, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey
  546. );
  547. $RSAPrivateKey = pack('Ca*a*', CRYPT_RSA_ASN1_SEQUENCE, $this->_encodeLength(strlen($RSAPrivateKey)), $RSAPrivateKey);
  548. $RSAPrivateKey = "-----BEGIN ENCRYPTED PRIVATE KEY-----\r\n" .
  549. chunk_split(base64_encode($RSAPrivateKey), 64) .
  550. '-----END ENCRYPTED PRIVATE KEY-----';
  551. }else{
  552. $RSAPrivateKey = "-----BEGIN PRIVATE KEY-----\r\n" .
  553. chunk_split(base64_encode($RSAPrivateKey), 64) .
  554. '-----END PRIVATE KEY-----';
  555. }
  556. return $RSAPrivateKey;
  557. }
  558. if(!empty($this->password) || is_string($this->password)){
  559. $iv = crypt_random_string(8);
  560. $symkey = pack('H*', md5($this->password . $iv)); // symkey is short for symmetric key
  561. $symkey.= substr(pack('H*', md5($symkey . $this->password . $iv)), 0, 8);
  562. if(!class_exists('Crypt_TripleDES')){
  563. include_once 'Crypt/TripleDES.php';
  564. }
  565. $des = new Crypt_TripleDES();
  566. $des->setKey($symkey);
  567. $des->setIV($iv);
  568. $iv = strtoupper(bin2hex($iv));
  569. $RSAPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\r\n" .
  570. "Proc-Type: 4,ENCRYPTED\r\n" .
  571. "DEK-Info: DES-EDE3-CBC,$iv\r\n" .
  572. "\r\n" .
  573. chunk_split(base64_encode($des->encrypt($RSAPrivateKey)), 64) .
  574. '-----END RSA PRIVATE KEY-----';
  575. }else{
  576. $RSAPrivateKey = "-----BEGIN RSA PRIVATE KEY-----\r\n" .
  577. chunk_split(base64_encode($RSAPrivateKey), 64) .
  578. '-----END RSA PRIVATE KEY-----';
  579. }
  580. return $RSAPrivateKey;
  581. }
  582. }
  583. /**
  584. * Convert a public key to the appropriate format
  585. *
  586. * @access private
  587. * @see setPublicKeyFormat()
  588. * @param String $RSAPrivateKey
  589. * @return String
  590. */
  591. private function _convertPublicKey($n, $e){
  592. $modulus = $n->toBytes(true);
  593. $publicExponent = $e->toBytes(true);
  594. switch($this->publicKeyFormat){
  595. case CRYPT_RSA_PUBLIC_FORMAT_RAW:
  596. return array('e' => $e->copy(), 'n' => $n->copy());
  597. case CRYPT_RSA_PUBLIC_FORMAT_XML:
  598. return "<RSAKeyValue>\r\n" .
  599. ' <Modulus>' . base64_encode($modulus) . "</Modulus>\r\n" .
  600. ' <Exponent>' . base64_encode($publicExponent) . "</Exponent>\r\n" .
  601. '</RSAKeyValue>';
  602. break;
  603. case CRYPT_RSA_PUBLIC_FORMAT_OPENSSH:
  604. // from <http://tools.ietf.org/html/rfc4253#page-15>:
  605. // string "ssh-rsa"
  606. // mpint e
  607. // mpint n
  608. $RSAPublicKey = pack('Na*Na*Na*', strlen('ssh-rsa'), 'ssh-rsa', strlen($publicExponent), $publicExponent, strlen($modulus), $modulus);
  609. $RSAPublicKey = 'ssh-rsa ' . base64_encode($RSAPublicKey) . ' ' . $this->comment;
  610. return $RSAPublicKey;
  611. default: // eg. CRYPT_RSA_PUBLIC_FORMAT_PKCS1_RAW or CRYPT_RSA_PUBLIC_FORMAT_PKCS1
  612. // from <http://tools.ietf.org/html/rfc3447#appendix-A.1.1>:
  613. // RSAPublicKey ::= SEQUENCE {
  614. // modulus INTEGER, -- n
  615. // publicExponent INTEGER -- e
  616. // }
  617. $components = array(
  618. 'modulus' => pack('Ca*a*', CRYPT_RSA_ASN1_INTEGER, $this->_encodeLength(strlen($modulus)), $modulus),
  619. 'publicExponent' => pack('Ca*a*', CRYPT_RSA_ASN1_INTEGER, $this->_encodeLength(strlen($publicExponent)), $publicExponent)
  620. );
  621. $RSAPublicKey = pack('Ca*a*a*', CRYPT_RSA_ASN1_SEQUENCE, $this->_encodeLength(strlen($components['modulus']) + strlen($components['publicExponent'])), $components['modulus'], $components['publicExponent']
  622. );
  623. if($this->publicKeyFormat == CRYPT_RSA_PUBLIC_FORMAT_PKCS1_RAW){
  624. $RSAPublicKey = "-----BEGIN RSA PUBLIC KEY-----\r\n" .
  625. chunk_split(base64_encode($RSAPublicKey), 64) .
  626. '-----END RSA PUBLIC KEY-----';
  627. }else{
  628. // sequence(oid(1.2.840.113549.1.1.1), null)) = rsaEncryption.
  629. $rsaOID = pack('H*', '300d06092a864886f70d0101010500'); // hex version of MA0GCSqGSIb3DQEBAQUA
  630. $RSAPublicKey = chr(0) . $RSAPublicKey;
  631. $RSAPublicKey = chr(3) . $this->_encodeLength(strlen($RSAPublicKey)) . $RSAPublicKey;
  632. $RSAPublicKey = pack('Ca*a*', CRYPT_RSA_ASN1_SEQUENCE, $this->_encodeLength(strlen($rsaOID . $RSAPublicKey)), $rsaOID . $RSAPublicKey
  633. );
  634. $RSAPublicKey = "-----BEGIN PUBLIC KEY-----\r\n" .
  635. chunk_split(base64_encode($RSAPublicKey), 64) .
  636. '-----END PUBLIC KEY-----';
  637. }
  638. return $RSAPublicKey;
  639. }
  640. }
  641. /**
  642. * Break a public or private key down into its constituant components
  643. *
  644. * @access private
  645. * @see _convertPublicKey()
  646. * @see _convertPrivateKey()
  647. * @param String $key
  648. * @param Integer $type
  649. * @return Array
  650. */
  651. private function _parseKey($key, $type){
  652. if($type != CRYPT_RSA_PUBLIC_FORMAT_RAW && !is_string($key)){
  653. return false;
  654. }
  655. switch($type){
  656. case CRYPT_RSA_PUBLIC_FORMAT_RAW:
  657. if(!is_array($key)){
  658. return false;
  659. }
  660. $components = array();
  661. switch(true){
  662. case isset($key['e']):
  663. $components['publicExponent'] = $key['e']->copy();
  664. break;
  665. case isset($key['exponent']):
  666. $components['publicExponent'] = $key['exponent']->copy();
  667. break;
  668. case isset($key['publicExponent']):
  669. $components['publicExponent'] = $key['publicExponent']->copy();
  670. break;
  671. case isset($key[0]):
  672. $components['publicExponent'] = $key[0]->copy();
  673. }
  674. switch(true){
  675. case isset($key['n']):
  676. $components['modulus'] = $key['n']->copy();
  677. break;
  678. case isset($key['modulo']):
  679. $components['modulus'] = $key['modulo']->copy();
  680. break;
  681. case isset($key['modulus']):
  682. $components['modulus'] = $key['modulus']->copy();
  683. break;
  684. case isset($key[1]):
  685. $components['modulus'] = $key[1]->copy();
  686. }
  687. return isset($components['modulus']) && isset($components['publicExponent']) ? $components : false;
  688. case CRYPT_RSA_PRIVATE_FORMAT_PKCS1:
  689. case CRYPT_RSA_PRIVATE_FORMAT_PKCS8:
  690. case CRYPT_RSA_PUBLIC_FORMAT_PKCS1:
  691. /* Although PKCS#1 proposes a format that public and private keys can use, encrypting them is
  692. "outside the scope" of PKCS#1. PKCS#1 then refers you to PKCS#12 and PKCS#15 if you're wanting to
  693. protect private keys, however, that's not what OpenSSL* does. OpenSSL protects private keys by adding
  694. two new "fields" to the key - DEK-Info and Proc-Type. These fields are discussed here:
  695. http://tools.ietf.org/html/rfc1421#section-4.6.1.1
  696. http://tools.ietf.org/html/rfc1421#section-4.6.1.3
  697. DES-EDE3-CBC as an algorithm, however, is not discussed anywhere, near as I can tell.
  698. DES-CBC and DES-EDE are discussed in RFC1423, however, DES-EDE3-CBC isn't, nor is its key derivation
  699. function. As is, the definitive authority on this encoding scheme isn't the IETF but rather OpenSSL's
  700. own implementation. ie. the implementation *is* the standard and any bugs that may exist in that
  701. implementation are part of the standard, as well.
  702. * OpenSSL is the de facto standard. It's utilized by OpenSSH and other projects */
  703. if(preg_match('#DEK-Info: (.+),(.+)#', $key, $matches)){
  704. $iv = pack('H*', trim($matches[2]));
  705. $symkey = pack('H*', md5($this->password . substr($iv, 0, 8))); // symkey is short for symmetric key
  706. $symkey.= pack('H*', md5($symkey . $this->password . substr($iv, 0, 8)));
  707. // remove the Proc-Type / DEK-Info sections as they're no longer needed
  708. $key = preg_replace('#^(?:Proc-Type|DEK-Info): .*#m', '', $key);
  709. $ciphertext = $this->_extractBER($key);
  710. if($ciphertext === false){
  711. $ciphertext = $key;
  712. }
  713. switch($matches[1]){
  714. case 'AES-256-CBC':
  715. if(!class_exists('Crypt_AES')){
  716. include_once 'Crypt/AES.php';
  717. }
  718. $crypto = new Crypt_AES();
  719. break;
  720. case 'AES-128-CBC':
  721. if(!class_exists('Crypt_AES')){
  722. include_once 'Crypt/AES.php';
  723. }
  724. $symkey = substr($symkey, 0, 16);
  725. $crypto = new Crypt_AES();
  726. break;
  727. case 'DES-EDE3-CFB':
  728. if(!class_exists('Crypt_TripleDES')){
  729. include_once 'Crypt/TripleDES.php';
  730. }
  731. $crypto = new Crypt_TripleDES(CRYPT_DES_MODE_CFB);
  732. break;
  733. case 'DES-EDE3-CBC':
  734. if(!class_exists('Crypt_TripleDES')){
  735. include_once 'Crypt/TripleDES.php';
  736. }
  737. $symkey = substr($symkey, 0, 24);
  738. $crypto = new Crypt_TripleDES();
  739. break;
  740. case 'DES-CBC':
  741. if(!class_exists('Crypt_DES')){
  742. include_once 'Crypt/DES.php';
  743. }
  744. $crypto = new Crypt_DES();
  745. break;
  746. default:
  747. return false;
  748. }
  749. $crypto->setKey($symkey);
  750. $crypto->setIV($iv);
  751. $decoded = $crypto->decrypt($ciphertext);
  752. }else{
  753. $decoded = $this->_extractBER($key);
  754. }
  755. if($decoded !== false){
  756. $key = $decoded;
  757. }
  758. $components = array();
  759. if(ord($this->_string_shift($key)) != CRYPT_RSA_ASN1_SEQUENCE){
  760. return false;
  761. }
  762. if($this->_decodeLength($key) != strlen($key)){
  763. return false;
  764. }
  765. $tag = ord($this->_string_shift($key));
  766. /* intended for keys for which OpenSSL's asn1parse returns the following:
  767. 0:d=0 hl=4 l= 631 cons: SEQUENCE
  768. 4:d=1 hl=2 l= 1 prim: INTEGER :00
  769. 7:d=1 hl=2 l= 13 cons: SEQUENCE
  770. 9:d=2 hl=2 l= 9 prim: OBJECT :rsaEncryption
  771. 20:d=2 hl=2 l= 0 prim: NULL
  772. 22:d=1 hl=4 l= 609 prim: OCTET STRING
  773. ie. PKCS8 keys */
  774. if($tag == CRYPT_RSA_ASN1_INTEGER && substr($key, 0, 3) == "\x01\x00\x30"){
  775. $this->_string_shift($key, 3);
  776. $tag = CRYPT_RSA_ASN1_SEQUENCE;
  777. }
  778. if($tag == CRYPT_RSA_ASN1_SEQUENCE){
  779. $temp = $this->_string_shift($key, $this->_decodeLength($key));
  780. if(ord($this->_string_shift($temp)) != CRYPT_RSA_ASN1_OBJECT){
  781. return false;
  782. }
  783. $length = $this->_decodeLength($temp);
  784. switch($this->_string_shift($temp, $length)){
  785. case "\x2a\x86\x48\x86\xf7\x0d\x01\x01\x01": // rsaEncryption
  786. break;
  787. case "\x2a\x86\x48\x86\xf7\x0d\x01\x05\x03": // pbeWithMD5AndDES-CBC
  788. /*
  789. PBEParameter ::= SEQUENCE {
  790. salt OCTET STRING (SIZE(8)),
  791. iterationCount INTEGER }
  792. */
  793. if(ord($this->_string_shift($temp)) != CRYPT_RSA_ASN1_SEQUENCE){
  794. return false;
  795. }
  796. if($this->_decodeLength($temp) != strlen($temp)){
  797. return false;
  798. }
  799. $this->_string_shift($temp); // assume it's an octet string
  800. $salt = $this->_string_shift($temp, $this->_decodeLength($temp));
  801. if(ord($this->_string_shift($temp)) != CRYPT_RSA_ASN1_INTEGER){
  802. return false;
  803. }
  804. $this->_decodeLength($temp);
  805. list(, $iterationCount) = unpack('N', str_pad($temp, 4, chr(0), STR_PAD_LEFT));
  806. $this->_string_shift($key); // assume it's an octet string
  807. $length = $this->_decodeLength($key);
  808. if(strlen($key) != $length){
  809. return false;
  810. }
  811. if(!class_exists('Crypt_DES')){
  812. include_once 'Crypt/DES.php';
  813. }
  814. $crypto = new Crypt_DES();
  815. $crypto->setPassword($this->password, 'pbkdf1', 'md5', $salt, $iterationCount);
  816. $key = $crypto->decrypt($key);
  817. if($key === false){
  818. return false;
  819. }
  820. return $this->_parseKey($key, CRYPT_RSA_PRIVATE_FORMAT_PKCS1);
  821. default:
  822. return false;
  823. }
  824. /* intended for keys for which OpenSSL's asn1parse returns the following:
  825. 0:d=0 hl=4 l= 290 cons: SEQUENCE
  826. 4:d=1 hl=2 l= 13 cons: SEQUENCE
  827. 6:d=2 hl=2 l= 9 prim: OBJECT :rsaEncryption
  828. 17:d=2 hl=2 l= 0 prim: NULL
  829. 19:d=1 hl=4 l= 271 prim: BIT STRING */
  830. $tag = ord($this->_string_shift($key)); // skip over the BIT STRING / OCTET STRING tag
  831. $this->_decodeLength($key); // skip over the BIT STRING / OCTET STRING length
  832. // "The initial octet shall encode, as an unsigned binary integer wtih bit 1 as the least significant bit, the number of
  833. // unused bits in the final subsequent octet. The number shall be in the range zero to seven."
  834. // -- http://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf (section 8.6.2.2)
  835. if($tag == CRYPT_RSA_ASN1_BITSTRING){
  836. $this->_string_shift($key);
  837. }
  838. if(ord($this->_string_shift($key)) != CRYPT_RSA_ASN1_SEQUENCE){
  839. return false;
  840. }
  841. if($this->_decodeLength($key) != strlen($key)){
  842. return false;
  843. }
  844. $tag = ord($this->_string_shift($key));
  845. }
  846. if($tag != CRYPT_RSA_ASN1_INTEGER){
  847. return false;
  848. }
  849. $length = $this->_decodeLength($key);
  850. $temp = $this->_string_shift($key, $length);
  851. if(strlen($temp) != 1 || ord($temp) > 2){
  852. $components['modulus'] = new \core\tools\secure\math\BigInteger($temp, 256);
  853. $this->_string_shift($key); // skip over CRYPT_RSA_ASN1_INTEGER
  854. $length = $this->_decodeLength($key);
  855. $components[$type == CRYPT_RSA_PUBLIC_FORMAT_PKCS1 ? 'publicExponent' : 'privateExponent'] = new \core\tools\secure\math\BigInteger($this->_string_shift($key, $length), 256);
  856. return $components;
  857. }
  858. if(ord($this->_string_shift($key)) != CRYPT_RSA_ASN1_INTEGER){
  859. return false;
  860. }
  861. $length = $this->_decodeLength($key);
  862. $components['modulus'] = new \core\tools\secure\math\BigInteger($this->_string_shift($key, $length), 256);
  863. $this->_string_shift($key);
  864. $length = $this->_decodeLength($key);
  865. $components['publicExponent'] = new \core\tools\secure\math\BigInteger($this->_string_shift($key, $length), 256);
  866. $this->_string_shift($key);
  867. $length = $this->_decodeLength($key);
  868. $components['privateExponent'] = new \core\tools\secure\math\BigInteger($this->_string_shift($key, $length), 256);
  869. $this->_string_shift($key);
  870. $length = $this->_decodeLength($key);
  871. $components['primes'] = array(1 => new \core\tools\secure\math\BigInteger($this->_string_shift($key, $length), 256));
  872. $this->_string_shift($key);
  873. $length = $this->_decodeLength($key);
  874. $components['primes'][] = new \core\tools\secure\math\BigInteger($this->_string_shift($key, $length), 256);
  875. $this->_string_shift($key);
  876. $length = $this->_decodeLength($key);
  877. $components['exponents'] = array(1 => new \core\tools\secure\math\BigInteger($this->_string_shift($key, $length), 256));
  878. $this->_string_shift($key);
  879. $length = $this->_decodeLength($key);
  880. $components['exponents'][] = new \core\tools\secure\math\BigInteger($this->_string_shift($key, $length), 256);
  881. $this->_string_shift($key);
  882. $length = $this->_decodeLength($key);
  883. $components['coefficients'] = array(2 => new \core\tools\secure\math\BigInteger($this->_string_shift($key, $length), 256));
  884. if(!empty($key)){
  885. if(ord($this->_string_shift($key)) != CRYPT_RSA_ASN1_SEQUENCE){
  886. return false;
  887. }
  888. $this->_decodeLength($key);
  889. while(!empty($key)){
  890. if(ord($this->_string_shift($key)) != CRYPT_RSA_ASN1_SEQUENCE){
  891. return false;
  892. }
  893. $this->_decodeLength($key);
  894. $key = substr($key, 1);
  895. $length = $this->_decodeLength($key);
  896. $components['primes'][] = new \core\tools\secure\math\BigInteger($this->_string_shift($key, $length), 256);
  897. $this->_string_shift($key);
  898. $length = $this->_decodeLength($key);
  899. $components['exponents'][] = new \core\tools\secure\math\BigInteger($this->_string_shift($key, $length), 256);
  900. $this->_string_shift($key);
  901. $length = $this->_decodeLength($key);
  902. $components['coefficients'][] = new \core\tools\secure\math\BigInteger($this->_string_shift($key, $length), 256);
  903. }
  904. }
  905. return $components;
  906. case CRYPT_RSA_PUBLIC_FORMAT_OPENSSH:
  907. $parts = explode(' ', $key, 3);
  908. $key = isset($parts[1]) ? base64_decode($parts[1]) : false;
  909. if($key === false){
  910. return false;
  911. }
  912. $comment = isset($parts[2]) ? $parts[2] : false;
  913. $cleanup = substr($key, 0, 11) == "\0\0\0\7ssh-rsa";
  914. if(strlen($key) <= 4){
  915. return false;
  916. }
  917. extract(unpack('Nlength', $this->_string_shift($key, 4)));
  918. $publicExponent = new \core\tools\secure\math\BigInteger($this->_string_shift($key, $length), -256);
  919. if(strlen($key) <= 4){
  920. return false;
  921. }
  922. extract(unpack('Nlength', $this->_string_shift($key, 4)));
  923. $modulus = new \core\tools\secure\math\BigInteger($this->_string_shift($key, $length), -256);
  924. if($cleanup && strlen($key)){
  925. if(strlen($key) <= 4){
  926. return false;
  927. }
  928. extract(unpack('Nlength', $this->_string_shift($key, 4)));
  929. $realModulus = new \core\tools\secure\math\BigInteger($this->_string_shift($key, $length), -256);
  930. return strlen($key) ? false : array(
  931. 'modulus' => $realModulus,
  932. 'publicExponent' => $modulus,
  933. 'comment' => $comment
  934. );
  935. }else{
  936. return strlen($key) ? false : array(
  937. 'modulus' => $modulus,
  938. 'publicExponent' => $publicExponent,
  939. 'comment' => $comment
  940. );
  941. }
  942. // http://www.w3.org/TR/xmldsig-core/#sec-RSAKeyValue
  943. // http://en.wikipedia.org/wiki/XML_Signature
  944. case CRYPT_RSA_PRIVATE_FORMAT_XML:
  945. case CRYPT_RSA_PUBLIC_FORMAT_XML:
  946. $this->components = array();
  947. $xml = xml_parser_create('UTF-8');
  948. xml_set_object($xml, $this);
  949. xml_set_element_handler($xml, '_start_element_handler', '_stop_element_handler');
  950. xml_set_character_data_handler($xml, '_data_handler');
  951. // add <xml></xml> to account for "dangling" tags like <BitStrength>...</BitStrength> that are sometimes added
  952. if(!xml_parse($xml, '<xml>' . $key . '</xml>')){
  953. return false;
  954. }
  955. return isset($this->components['modulus']) && isset($this->components['publicExponent']) ? $this->components : false;
  956. // from PuTTY's SSHPUBK.C
  957. case CRYPT_RSA_PRIVATE_FORMAT_PUTTY:
  958. $components = array();
  959. $key = preg_split('#\r\n|\r|\n#', $key);
  960. $type = trim(preg_replace('#PuTTY-User-Key-File-2: (.+)#', '$1', $key[0]));
  961. if($type != 'ssh-rsa'){
  962. return false;
  963. }
  964. $encryption = trim(preg_replace('#Encryption: (.+)#', '$1', $key[1]));
  965. $comment = trim(preg_replace('#Comment: (.+)#', '$1', $key[2]));
  966. $publicLength = trim(preg_replace('#Public-Lines: (\d+)#', '$1', $key[3]));
  967. $public = base64_decode(implode('', array_map('trim', array_slice($key, 4, $publicLength))));
  968. $public = substr($public, 11);
  969. extract(unpack('Nlength', $this->_string_shift($public, 4)));
  970. $components['publicExponent'] = new \core\tools\secure\math\BigInteger($this->_string_shift($public, $length), -256);
  971. extract(unpack('Nlength', $this->_string_shift($public, 4)));
  972. $components['modulus'] = new \core\tools\secure\math\BigInteger($this->_string_shift($public, $length), -256);
  973. $privateLength = trim(preg_replace('#Private-Lines: (\d+)#', '$1', $key[$publicLength + 4]));
  974. $private = base64_decode(implode('', array_map('trim', array_slice($key, $publicLength + 5, $privateLength))));
  975. switch($encryption){
  976. case 'aes256-cbc':
  977. if(!class_exists('Crypt_AES')){
  978. include_once 'Crypt/AES.php';
  979. }
  980. $symkey = '';
  981. $sequence = 0;
  982. while(strlen($symkey) < 32){
  983. $temp = pack('Na*', $sequence++, $this->password);
  984. $symkey.= pack('H*', sha1($temp));
  985. }
  986. $symkey = substr($symkey, 0, 32);
  987. $crypto = new Crypt_AES();
  988. }
  989. if($encryption != 'none'){
  990. $crypto->setKey($symkey);
  991. $crypto->disablePadding();
  992. $private = $crypto->decrypt($private);
  993. if($private === false){
  994. return false;
  995. }
  996. }
  997. extract(unpack('Nlength', $this->_string_shift($private, 4)));

Large files files are truncated, but you can click here to view the full file