PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/library/Zend/Crypt/Rsa.php

https://gitlab.com/israel.correa/Expresso
PHP | 298 lines | 199 code | 33 blank | 66 comment | 20 complexity | 7ec51c253c7de1e5be4fa331b92169af MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-3.0, LGPL-2.1, LGPL-3.0, JSON, Apache-2.0
  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-2009 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 10020 2009-08-18 14:34:09Z j.fischer@metaways.de $
  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-2009 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 (!is_null($configargs)) {
  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. $this->_privateKey = new Zend_Crypt_Rsa_Key_Private($this->_pemString, $this->_passPhrase);
  200. $this->_publicKey = $this->_privateKey->getPublicKey();
  201. }
  202. public function setPemPath($value)
  203. {
  204. $this->_pemPath = $value;
  205. $this->setPemString(file_get_contents($this->_pemPath));
  206. }
  207. public function setCertificateString($value)
  208. {
  209. $this->_certificateString = $value;
  210. $this->_publicKey = new Zend_Crypt_Rsa_Key_Public($this->_certificateString, $this->_passPhrase);
  211. }
  212. public function setCertificatePath($value)
  213. {
  214. $this->_certificatePath = $value;
  215. $this->setCertificateString(file_get_contents($this->_certificatePath));
  216. }
  217. public function setHashAlgorithm($name)
  218. {
  219. switch ($name) {
  220. case 'md2':
  221. $this->_hashAlgorithm = OPENSSL_ALGO_MD2;
  222. break;
  223. case 'md4':
  224. $this->_hashAlgorithm = OPENSSL_ALGO_MD4;
  225. break;
  226. case 'md5':
  227. $this->_hashAlgorithm = OPENSSL_ALGO_MD5;
  228. break;
  229. }
  230. }
  231. /**
  232. * @return string
  233. */
  234. public function getPemString()
  235. {
  236. return $this->_pemString;
  237. }
  238. public function getPemPath()
  239. {
  240. return $this->_pemPath;
  241. }
  242. public function getCertificateString()
  243. {
  244. return $this->_certificateString;
  245. }
  246. public function getCertificatePath()
  247. {
  248. return $this->_certificatePath;
  249. }
  250. public function getHashAlgorithm()
  251. {
  252. return $this->_hashAlgorithm;
  253. }
  254. protected function _parseConfigArgs(array $config = null)
  255. {
  256. $configs = array();
  257. if (isset($config['privateKeyBits'])) {
  258. $configs['private_key_bits'] = $config['privateKeyBits'];
  259. }
  260. if (!empty($configs)) {
  261. return $configs;
  262. }
  263. return null;
  264. }
  265. }