PageRenderTime 56ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/Zend/Crypt/Rsa.php

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