Rsa.php 8.4 KB

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