PageRenderTime 43ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Crypt/Rsa.php

https://github.com/MarcelloDuarte/zf2
PHP | 306 lines | 209 code | 34 blank | 63 comment | 20 complexity | 437063b0b9db6c57b6936e708781d6a9 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. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * @namespace
  22. */
  23. namespace Zend\Crypt;
  24. /**
  25. * @uses Zend\Crypt\Rsa\PrivateKey
  26. * @uses Zend\Crypt\Rsa\PublicKey
  27. * @category Zend
  28. * @package Zend_Crypt
  29. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  30. * @license http://framework.zend.com/license/new-bsd New BSD License
  31. */
  32. class Rsa
  33. {
  34. const BINARY = 'binary';
  35. const BASE64 = 'base64';
  36. protected $_privateKey = null;
  37. protected $_publicKey = null;
  38. /**
  39. * @var string
  40. */
  41. protected $_pemString = null;
  42. protected $_pemPath = null;
  43. protected $_certificateString = null;
  44. protected $_certificatePath = null;
  45. protected $_hashAlgorithm = OPENSSL_ALGO_SHA1;
  46. protected $_passPhrase = null;
  47. public function __construct(array $options = null)
  48. {
  49. if (isset($options)) {
  50. $this->setOptions($options);
  51. }
  52. }
  53. public function setOptions(array $options)
  54. {
  55. if (isset($options['passPhrase'])) {
  56. $this->_passPhrase = $options['passPhrase'];
  57. }
  58. foreach ($options as $option=>$value) {
  59. switch ($option) {
  60. case 'pemString':
  61. $this->setPemString($value);
  62. break;
  63. case 'pemPath':
  64. $this->setPemPath($value);
  65. break;
  66. case 'certificateString':
  67. $this->setCertificateString($value);
  68. break;
  69. case 'certificatePath':
  70. $this->setCertificatePath($value);
  71. break;
  72. case 'hashAlgorithm':
  73. $this->setHashAlgorithm($value);
  74. break;
  75. }
  76. }
  77. }
  78. public function getPrivateKey()
  79. {
  80. return $this->_privateKey;
  81. }
  82. public function getPublicKey()
  83. {
  84. return $this->_publicKey;
  85. }
  86. /**
  87. * @param string $data
  88. * @param Zend\Crypt\Rsa\PrivateKey $privateKey
  89. * @param string $format
  90. * @return string
  91. */
  92. public function sign($data, Rsa\PrivateKey $privateKey = null, $format = null)
  93. {
  94. $signature = '';
  95. if (isset($privateKey)) {
  96. $opensslKeyResource = $privateKey->getOpensslKeyResource();
  97. } else {
  98. $opensslKeyResource = $this->_privateKey->getOpensslKeyResource();
  99. }
  100. $result = openssl_sign(
  101. $data, $signature,
  102. $opensslKeyResource,
  103. $this->getHashAlgorithm()
  104. );
  105. if ($format == self::BASE64) {
  106. return base64_encode($signature);
  107. }
  108. return $signature;
  109. }
  110. /**
  111. * @param string $data
  112. * @param string $signature
  113. * @param string $format
  114. * @return string
  115. */
  116. public function verifySignature($data, $signature, $format = null)
  117. {
  118. if ($format == self::BASE64) {
  119. $signature = base64_decode($signature);
  120. }
  121. $result = openssl_verify($data, $signature,
  122. $this->getPublicKey()->getOpensslKeyResource(),
  123. $this->getHashAlgorithm());
  124. return $result;
  125. }
  126. /**
  127. * @param string $data
  128. * @param Zend\Crypt\Rsa\Key $key
  129. * @param string $format
  130. * @return string
  131. */
  132. public function encrypt($data, Rsa\Key $key, $format = null)
  133. {
  134. $encrypted = '';
  135. $function = 'openssl_public_encrypt';
  136. if ($key instanceof Rsa\PrivateKey) {
  137. $function = 'openssl_private_encrypt';
  138. }
  139. $function($data, $encrypted, $key->getOpensslKeyResource());
  140. if ($format == self::BASE64) {
  141. return base64_encode($encrypted);
  142. }
  143. return $encrypted;
  144. }
  145. /**
  146. * @param string $data
  147. * @param \Zend\Crypt\Rsa\Key $key
  148. * @param string $format
  149. * @return string
  150. */
  151. public function decrypt($data, Rsa\Key $key, $format = null)
  152. {
  153. $decrypted = '';
  154. if ($format == self::BASE64) {
  155. $data = base64_decode($data);
  156. }
  157. $function = 'openssl_private_decrypt';
  158. if ($key instanceof Rsa\PublicKey) {
  159. $function = 'openssl_public_decrypt';
  160. }
  161. $function($data, $decrypted, $key->getOpensslKeyResource());
  162. return $decrypted;
  163. }
  164. public function generateKeys(array $configargs = null)
  165. {
  166. $config = null;
  167. $passPhrase = null;
  168. if ($configargs !== null) {
  169. if (isset($configargs['passPhrase'])) {
  170. $passPhrase = $configargs['passPhrase'];
  171. unset($configargs['passPhrase']);
  172. }
  173. $config = $this->_parseConfigArgs($configargs);
  174. }
  175. $privateKey = null;
  176. $publicKey = null;
  177. $resource = openssl_pkey_new($config);
  178. // above fails on PHP 5.3
  179. openssl_pkey_export($resource, $private, $passPhrase);
  180. $privateKey = new Rsa\PrivateKey($private, $passPhrase);
  181. $details = openssl_pkey_get_details($resource);
  182. $publicKey = new Rsa\PublicKey($details['key']);
  183. $return = new \ArrayObject(array(
  184. 'privateKey' => $privateKey,
  185. 'publicKey' => $publicKey
  186. ), \ArrayObject::ARRAY_AS_PROPS);
  187. return $return;
  188. }
  189. /**
  190. * @param string $value
  191. */
  192. public function setPemString($value)
  193. {
  194. $this->_pemString = $value;
  195. try {
  196. $this->_privateKey = new Rsa\PrivateKey($this->_pemString, $this->_passPhrase);
  197. $this->_publicKey = $this->_privateKey->getPublicKey();
  198. } catch (Exception $e) {
  199. $this->_privateKey = null;
  200. $this->_publicKey = new Rsa\PublicKey($this->_pemString);
  201. }
  202. }
  203. public function setPemPath($value)
  204. {
  205. $this->_pemPath = $value;
  206. $this->setPemString(file_get_contents($this->_pemPath));
  207. }
  208. public function setCertificateString($value)
  209. {
  210. $this->_certificateString = $value;
  211. $this->_publicKey = new Rsa\PublicKey($this->_certificateString, $this->_passPhrase);
  212. }
  213. public function setCertificatePath($value)
  214. {
  215. $this->_certificatePath = $value;
  216. $this->setCertificateString(file_get_contents($this->_certificatePath));
  217. }
  218. public function setHashAlgorithm($name)
  219. {
  220. switch (strtolower($name)) {
  221. case 'md2':
  222. $this->_hashAlgorithm = OPENSSL_ALGO_MD2;
  223. break;
  224. case 'md4':
  225. $this->_hashAlgorithm = OPENSSL_ALGO_MD4;
  226. break;
  227. case 'md5':
  228. $this->_hashAlgorithm = OPENSSL_ALGO_MD5;
  229. break;
  230. case 'sha1':
  231. $this->_hashAlgorithm = OPENSSL_ALGO_SHA1;
  232. break;
  233. case 'dss1':
  234. $this->_hashAlgorithm = OPENSSL_ALGO_DSS1;
  235. break;
  236. }
  237. }
  238. /**
  239. * @return string
  240. */
  241. public function getPemString()
  242. {
  243. return $this->_pemString;
  244. }
  245. public function getPemPath()
  246. {
  247. return $this->_pemPath;
  248. }
  249. public function getCertificateString()
  250. {
  251. return $this->_certificateString;
  252. }
  253. public function getCertificatePath()
  254. {
  255. return $this->_certificatePath;
  256. }
  257. public function getHashAlgorithm()
  258. {
  259. return $this->_hashAlgorithm;
  260. }
  261. protected function _parseConfigArgs(array $config = null)
  262. {
  263. $configs = array();
  264. if (isset($config['privateKeyBits'])) {
  265. $configs['private_key_bits'] = $config['privateKeyBits'];
  266. }
  267. if (!empty($configs)) {
  268. return $configs;
  269. }
  270. return null;
  271. }
  272. }