PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Crypt/Rsa.php

https://bitbucket.org/bigstylee/zend-framework
PHP | 338 lines | 222 code | 35 blank | 81 comment | 23 complexity | d21973cd48397a8829848abc6a3040f4 MD5 | raw file
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Crypt
  17. * @subpackage Rsa
  18. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Rsa.php 24808 2012-05-17 19:56:09Z rob $
  21. */
  22. /**
  23. * @see Zend_Crypt_Rsa_Key_Private
  24. */
  25. require_once 'Zend/Crypt/Rsa/Key/Private.php';
  26. /**
  27. * @see Zend_Crypt_Rsa_Key_Public
  28. */
  29. require_once 'Zend/Crypt/Rsa/Key/Public.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Crypt
  33. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Zend_Crypt_Rsa
  37. {
  38. const BINARY = 'binary';
  39. const BASE64 = 'base64';
  40. protected $_privateKey;
  41. protected $_publicKey;
  42. /**
  43. * @var string
  44. */
  45. protected $_pemString;
  46. protected $_pemPath;
  47. protected $_certificateString;
  48. protected $_certificatePath;
  49. protected $_hashAlgorithm;
  50. protected $_passPhrase;
  51. /**
  52. * Class constructor
  53. *
  54. * @param array $options
  55. * @throws Zend_Crypt_Rsa_Exception
  56. */
  57. public function __construct(array $options = null)
  58. {
  59. if (!extension_loaded('openssl')) {
  60. require_once 'Zend/Crypt/Rsa/Exception.php';
  61. throw new Zend_Crypt_Rsa_Exception('Zend_Crypt_Rsa requires openssl extension to be loaded.');
  62. }
  63. // Set _hashAlgorithm property when we are sure, that openssl extension is loaded
  64. // and OPENSSL_ALGO_SHA1 constant is available
  65. $this->_hashAlgorithm = OPENSSL_ALGO_SHA1;
  66. if (isset($options)) {
  67. $this->setOptions($options);
  68. }
  69. }
  70. public function setOptions(array $options)
  71. {
  72. if (isset($options['passPhrase'])) {
  73. $this->_passPhrase = $options['passPhrase'];
  74. }
  75. foreach ($options as $option=>$value) {
  76. switch ($option) {
  77. case 'pemString':
  78. $this->setPemString($value);
  79. break;
  80. case 'pemPath':
  81. $this->setPemPath($value);
  82. break;
  83. case 'certificateString':
  84. $this->setCertificateString($value);
  85. break;
  86. case 'certificatePath':
  87. $this->setCertificatePath($value);
  88. break;
  89. case 'hashAlgorithm':
  90. $this->setHashAlgorithm($value);
  91. break;
  92. }
  93. }
  94. }
  95. public function getPrivateKey()
  96. {
  97. return $this->_privateKey;
  98. }
  99. public function getPublicKey()
  100. {
  101. return $this->_publicKey;
  102. }
  103. /**
  104. * @param string $data
  105. * @param Zend_Crypt_Rsa_Key_Private $privateKey
  106. * @param string $format
  107. * @return string
  108. */
  109. public function sign($data, Zend_Crypt_Rsa_Key_Private $privateKey = null, $format = null)
  110. {
  111. $signature = '';
  112. if (isset($privateKey)) {
  113. $opensslKeyResource = $privateKey->getOpensslKeyResource();
  114. } else {
  115. $opensslKeyResource = $this->_privateKey->getOpensslKeyResource();
  116. }
  117. $result = openssl_sign(
  118. $data, $signature,
  119. $opensslKeyResource,
  120. $this->getHashAlgorithm()
  121. );
  122. if ($format == self::BASE64) {
  123. return base64_encode($signature);
  124. }
  125. return $signature;
  126. }
  127. /**
  128. * @param string $data
  129. * @param string $signature
  130. * @param string $format
  131. * @return string
  132. */
  133. public function verifySignature($data, $signature, $format = null)
  134. {
  135. if ($format == self::BASE64) {
  136. $signature = base64_decode($signature);
  137. }
  138. $result = openssl_verify($data, $signature,
  139. $this->getPublicKey()->getOpensslKeyResource(),
  140. $this->getHashAlgorithm());
  141. return $result;
  142. }
  143. /**
  144. * @param string $data
  145. * @param Zend_Crypt_Rsa_Key $key
  146. * @param string $format
  147. * @return string
  148. */
  149. public function encrypt($data, Zend_Crypt_Rsa_Key $key, $format = null)
  150. {
  151. $encrypted = '';
  152. $function = 'openssl_public_encrypt';
  153. if ($key instanceof Zend_Crypt_Rsa_Key_Private) {
  154. $function = 'openssl_private_encrypt';
  155. }
  156. $function($data, $encrypted, $key->getOpensslKeyResource());
  157. if ($format == self::BASE64) {
  158. return base64_encode($encrypted);
  159. }
  160. return $encrypted;
  161. }
  162. /**
  163. * @param string $data
  164. * @param Zend_Crypt_Rsa_Key $key
  165. * @param string $format
  166. * @return string
  167. */
  168. public function decrypt($data, Zend_Crypt_Rsa_Key $key, $format = null)
  169. {
  170. $decrypted = '';
  171. if ($format == self::BASE64) {
  172. $data = base64_decode($data);
  173. }
  174. $function = 'openssl_private_decrypt';
  175. if ($key instanceof Zend_Crypt_Rsa_Key_Public) {
  176. $function = 'openssl_public_decrypt';
  177. }
  178. $function($data, $decrypted, $key->getOpensslKeyResource());
  179. return $decrypted;
  180. }
  181. /**
  182. * @param array $configargs
  183. *
  184. * @throws Zend_Crypt_Rsa_Exception
  185. *
  186. * @return ArrayObject
  187. */
  188. public function generateKeys(array $configargs = null)
  189. {
  190. $config = null;
  191. $passPhrase = null;
  192. if ($configargs !== null) {
  193. if (isset($configargs['passPhrase'])) {
  194. $passPhrase = $configargs['passPhrase'];
  195. unset($configargs['passPhrase']);
  196. }
  197. $config = $this->_parseConfigArgs($configargs);
  198. }
  199. $privateKey = null;
  200. $publicKey = null;
  201. $resource = openssl_pkey_new($config);
  202. if (!$resource) {
  203. require_once 'Zend/Crypt/Rsa/Exception.php';
  204. throw new Zend_Crypt_Rsa_Exception('Failed to generate a new private key');
  205. }
  206. // above fails on PHP 5.3
  207. openssl_pkey_export($resource, $private, $passPhrase);
  208. $privateKey = new Zend_Crypt_Rsa_Key_Private($private, $passPhrase);
  209. $details = openssl_pkey_get_details($resource);
  210. $publicKey = new Zend_Crypt_Rsa_Key_Public($details['key']);
  211. $return = new ArrayObject(array(
  212. 'privateKey'=>$privateKey,
  213. 'publicKey'=>$publicKey
  214. ), ArrayObject::ARRAY_AS_PROPS);
  215. return $return;
  216. }
  217. /**
  218. * @param string $value
  219. */
  220. public function setPemString($value)
  221. {
  222. $this->_pemString = $value;
  223. try {
  224. $this->_privateKey = new Zend_Crypt_Rsa_Key_Private($this->_pemString, $this->_passPhrase);
  225. $this->_publicKey = $this->_privateKey->getPublicKey();
  226. } catch (Zend_Crypt_Exception $e) {
  227. $this->_privateKey = null;
  228. $this->_publicKey = new Zend_Crypt_Rsa_Key_Public($this->_pemString);
  229. }
  230. }
  231. public function setPemPath($value)
  232. {
  233. $this->_pemPath = $value;
  234. $this->setPemString(file_get_contents($this->_pemPath));
  235. }
  236. public function setCertificateString($value)
  237. {
  238. $this->_certificateString = $value;
  239. $this->_publicKey = new Zend_Crypt_Rsa_Key_Public($this->_certificateString, $this->_passPhrase);
  240. }
  241. public function setCertificatePath($value)
  242. {
  243. $this->_certificatePath = $value;
  244. $this->setCertificateString(file_get_contents($this->_certificatePath));
  245. }
  246. public function setHashAlgorithm($name)
  247. {
  248. switch (strtolower($name)) {
  249. case 'md2':
  250. $this->_hashAlgorithm = OPENSSL_ALGO_MD2;
  251. break;
  252. case 'md4':
  253. $this->_hashAlgorithm = OPENSSL_ALGO_MD4;
  254. break;
  255. case 'md5':
  256. $this->_hashAlgorithm = OPENSSL_ALGO_MD5;
  257. break;
  258. case 'sha1':
  259. $this->_hashAlgorithm = OPENSSL_ALGO_SHA1;
  260. break;
  261. case 'dss1':
  262. $this->_hashAlgorithm = OPENSSL_ALGO_DSS1;
  263. break;
  264. }
  265. }
  266. /**
  267. * @return string
  268. */
  269. public function getPemString()
  270. {
  271. return $this->_pemString;
  272. }
  273. public function getPemPath()
  274. {
  275. return $this->_pemPath;
  276. }
  277. public function getCertificateString()
  278. {
  279. return $this->_certificateString;
  280. }
  281. public function getCertificatePath()
  282. {
  283. return $this->_certificatePath;
  284. }
  285. public function getHashAlgorithm()
  286. {
  287. return $this->_hashAlgorithm;
  288. }
  289. protected function _parseConfigArgs(array $config = null)
  290. {
  291. $configs = array();
  292. if (isset($config['private_key_bits'])) {
  293. $configs['private_key_bits'] = $config['private_key_bits'];
  294. }
  295. if (isset($config['privateKeyBits'])) {
  296. $configs['private_key_bits'] = $config['privateKeyBits'];
  297. }
  298. if (!empty($configs)) {
  299. return $configs;
  300. }
  301. return null;
  302. }
  303. }