/Zend/Crypt/Rsa/Rsa.php

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