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

/library/Zend/Crypt/Rsa.php

https://bitbucket.org/luizbrandaoj/mini-blog
PHP | 324 lines | 215 code | 35 blank | 74 comment | 21 complexity | 1865597d6fc33814b2179930494f2097 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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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 extention 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. public function generateKeys(array $configargs = null)
  182. {
  183. $config = null;
  184. $passPhrase = null;
  185. if ($configargs !== null) {
  186. if (isset($configargs['passPhrase'])) {
  187. $passPhrase = $configargs['passPhrase'];
  188. unset($configargs['passPhrase']);
  189. }
  190. $config = $this->_parseConfigArgs($configargs);
  191. }
  192. $privateKey = null;
  193. $publicKey = null;
  194. $resource = openssl_pkey_new($config);
  195. // above fails on PHP 5.3
  196. openssl_pkey_export($resource, $private, $passPhrase);
  197. $privateKey = new Zend_Crypt_Rsa_Key_Private($private, $passPhrase);
  198. $details = openssl_pkey_get_details($resource);
  199. $publicKey = new Zend_Crypt_Rsa_Key_Public($details['key']);
  200. $return = new ArrayObject(array(
  201. 'privateKey'=>$privateKey,
  202. 'publicKey'=>$publicKey
  203. ), ArrayObject::ARRAY_AS_PROPS);
  204. return $return;
  205. }
  206. /**
  207. * @param string $value
  208. */
  209. public function setPemString($value)
  210. {
  211. $this->_pemString = $value;
  212. try {
  213. $this->_privateKey = new Zend_Crypt_Rsa_Key_Private($this->_pemString, $this->_passPhrase);
  214. $this->_publicKey = $this->_privateKey->getPublicKey();
  215. } catch (Zend_Crypt_Exception $e) {
  216. $this->_privateKey = null;
  217. $this->_publicKey = new Zend_Crypt_Rsa_Key_Public($this->_pemString);
  218. }
  219. }
  220. public function setPemPath($value)
  221. {
  222. $this->_pemPath = $value;
  223. $this->setPemString(file_get_contents($this->_pemPath));
  224. }
  225. public function setCertificateString($value)
  226. {
  227. $this->_certificateString = $value;
  228. $this->_publicKey = new Zend_Crypt_Rsa_Key_Public($this->_certificateString, $this->_passPhrase);
  229. }
  230. public function setCertificatePath($value)
  231. {
  232. $this->_certificatePath = $value;
  233. $this->setCertificateString(file_get_contents($this->_certificatePath));
  234. }
  235. public function setHashAlgorithm($name)
  236. {
  237. switch (strtolower($name)) {
  238. case 'md2':
  239. $this->_hashAlgorithm = OPENSSL_ALGO_MD2;
  240. break;
  241. case 'md4':
  242. $this->_hashAlgorithm = OPENSSL_ALGO_MD4;
  243. break;
  244. case 'md5':
  245. $this->_hashAlgorithm = OPENSSL_ALGO_MD5;
  246. break;
  247. case 'sha1':
  248. $this->_hashAlgorithm = OPENSSL_ALGO_SHA1;
  249. break;
  250. case 'dss1':
  251. $this->_hashAlgorithm = OPENSSL_ALGO_DSS1;
  252. break;
  253. }
  254. }
  255. /**
  256. * @return string
  257. */
  258. public function getPemString()
  259. {
  260. return $this->_pemString;
  261. }
  262. public function getPemPath()
  263. {
  264. return $this->_pemPath;
  265. }
  266. public function getCertificateString()
  267. {
  268. return $this->_certificateString;
  269. }
  270. public function getCertificatePath()
  271. {
  272. return $this->_certificatePath;
  273. }
  274. public function getHashAlgorithm()
  275. {
  276. return $this->_hashAlgorithm;
  277. }
  278. protected function _parseConfigArgs(array $config = null)
  279. {
  280. $configs = array();
  281. if (isset($config['privateKeyBits'])) {
  282. $configs['private_key_bits'] = $config['privateKeyBits'];
  283. }
  284. if (!empty($configs)) {
  285. return $configs;
  286. }
  287. return null;
  288. }
  289. }