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

/src/frapi/library/Zend/Filter/Encrypt/Openssl.php

https://github.com/Martin1982/IBMessagingWorkshopServer
PHP | 353 lines | 177 code | 40 blank | 136 comment | 26 complexity | 5e48023e817705e2857f09e6217e415e 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_Filter
  17. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Openssl.php 20288 2010-01-14 20:15:43Z thomas $
  20. */
  21. /**
  22. * @see Zend_Filter_Encrypt_Interface
  23. */
  24. // require_once 'Zend/Filter/Encrypt/Interface.php';
  25. /**
  26. * Encryption adapter for openssl
  27. *
  28. * @category Zend
  29. * @package Zend_Filter
  30. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. */
  33. class Zend_Filter_Encrypt_Openssl implements Zend_Filter_Encrypt_Interface
  34. {
  35. /**
  36. * Definitions for encryption
  37. * array(
  38. * 'public' => public keys
  39. * 'private' => private keys
  40. * 'envelope' => resulting envelope keys
  41. * )
  42. */
  43. protected $_keys = array(
  44. 'public' => array(),
  45. 'private' => array(),
  46. 'envelope' => array()
  47. );
  48. /**
  49. * Internal passphrase
  50. *
  51. * @var string
  52. */
  53. protected $_passphrase;
  54. /**
  55. * Class constructor
  56. * Available options
  57. * 'public' => public key
  58. * 'private' => private key
  59. * 'envelope' => envelope key
  60. * 'passphrase' => passphrase
  61. *
  62. * @param string|array $options Options for this adapter
  63. */
  64. public function __construct($options = array())
  65. {
  66. if (!extension_loaded('openssl')) {
  67. // require_once 'Zend/Filter/Exception.php';
  68. throw new Zend_Filter_Exception('This filter needs the openssl extension');
  69. }
  70. if ($options instanceof Zend_Config) {
  71. $options = $options->toArray();
  72. }
  73. if (!is_array($options)) {
  74. $options = array('public' => $options);
  75. }
  76. if (array_key_exists('passphrase', $options)) {
  77. $this->setPassphrase($options['passphrase']);
  78. unset($options['passphrase']);
  79. }
  80. $this->_setKeys($options);
  81. }
  82. /**
  83. * Sets the encryption keys
  84. *
  85. * @param string|array $keys Key with type association
  86. * @return Zend_Filter_Encrypt_Openssl
  87. */
  88. protected function _setKeys($keys)
  89. {
  90. if (!is_array($keys)) {
  91. // require_once 'Zend/Filter/Exception.php';
  92. throw new Zend_Filter_Exception('Invalid options argument provided to filter');
  93. }
  94. foreach ($keys as $type => $key) {
  95. if (is_file($key) and is_readable($key)) {
  96. $file = fopen($key, 'r');
  97. $cert = fread($file, 8192);
  98. fclose($file);
  99. } else {
  100. $cert = $key;
  101. $key = count($this->_keys[$type]);
  102. }
  103. switch ($type) {
  104. case 'public':
  105. $test = openssl_pkey_get_public($cert);
  106. if ($test === false) {
  107. // require_once 'Zend/Filter/Exception.php';
  108. throw new Zend_Filter_Exception("Public key '{$cert}' not valid");
  109. }
  110. openssl_free_key($test);
  111. $this->_keys['public'][$key] = $cert;
  112. break;
  113. case 'private':
  114. $test = openssl_pkey_get_private($cert, $this->_passphrase);
  115. if ($test === false) {
  116. // require_once 'Zend/Filter/Exception.php';
  117. throw new Zend_Filter_Exception("Private key '{$cert}' not valid");
  118. }
  119. openssl_free_key($test);
  120. $this->_keys['private'][$key] = $cert;
  121. break;
  122. case 'envelope':
  123. $this->_keys['envelope'][$key] = $cert;
  124. break;
  125. default:
  126. break;
  127. }
  128. }
  129. return $this;
  130. }
  131. /**
  132. * Returns all public keys
  133. *
  134. * @return array
  135. */
  136. public function getPublicKey()
  137. {
  138. return $this->_keys['public'];
  139. }
  140. /**
  141. * Sets public keys
  142. *
  143. * @param string|array $key Public keys
  144. * @return Zend_Filter_Encrypt_Openssl
  145. */
  146. public function setPublicKey($key)
  147. {
  148. if (is_array($key)) {
  149. foreach($key as $type => $option) {
  150. if ($type !== 'public') {
  151. $key['public'] = $option;
  152. unset($key[$type]);
  153. }
  154. }
  155. } else {
  156. $key = array('public' => $key);
  157. }
  158. return $this->_setKeys($key);
  159. }
  160. /**
  161. * Returns all private keys
  162. *
  163. * @return array
  164. */
  165. public function getPrivateKey()
  166. {
  167. return $this->_keys['private'];
  168. }
  169. /**
  170. * Sets private keys
  171. *
  172. * @param string $key Private key
  173. * @param string $passphrase
  174. * @return Zend_Filter_Encrypt_Openssl
  175. */
  176. public function setPrivateKey($key, $passphrase = null)
  177. {
  178. if (is_array($key)) {
  179. foreach($key as $type => $option) {
  180. if ($type !== 'private') {
  181. $key['private'] = $option;
  182. unset($key[$type]);
  183. }
  184. }
  185. } else {
  186. $key = array('private' => $key);
  187. }
  188. if ($passphrase !== null) {
  189. $this->setPassphrase($passphrase);
  190. }
  191. return $this->_setKeys($key);
  192. }
  193. /**
  194. * Returns all envelope keys
  195. *
  196. * @return array
  197. */
  198. public function getEnvelopeKey()
  199. {
  200. return $this->_keys['envelope'];
  201. }
  202. /**
  203. * Sets envelope keys
  204. *
  205. * @param string|array $options Envelope keys
  206. * @return Zend_Filter_Encrypt_Openssl
  207. */
  208. public function setEnvelopeKey($key)
  209. {
  210. if (is_array($key)) {
  211. foreach($key as $type => $option) {
  212. if ($type !== 'envelope') {
  213. $key['envelope'] = $option;
  214. unset($key[$type]);
  215. }
  216. }
  217. } else {
  218. $key = array('envelope' => $key);
  219. }
  220. return $this->_setKeys($key);
  221. }
  222. /**
  223. * Returns the passphrase
  224. *
  225. * @return string
  226. */
  227. public function getPassphrase()
  228. {
  229. return $this->_passphrase;
  230. }
  231. /**
  232. * Sets a new passphrase
  233. *
  234. * @param string $passphrase
  235. * @return Zend_Filter_Encrypt_Openssl
  236. */
  237. public function setPassphrase($passphrase)
  238. {
  239. $this->_passphrase = $passphrase;
  240. return $this;
  241. }
  242. /**
  243. * Encrypts the file $value with the defined settings
  244. * Note that you also need the "encrypted" keys to be able to decrypt
  245. *
  246. * @param string $value Content to encrypt
  247. * @return string The encrypted content
  248. * @throws Zend_Filter_Exception
  249. */
  250. public function encrypt($value)
  251. {
  252. $encrypted = array();
  253. $encryptedkeys = array();
  254. if (count($this->_keys['public']) == 0) {
  255. // require_once 'Zend/Filter/Exception.php';
  256. throw new Zend_Filter_Exception('Openssl can not encrypt without public keys');
  257. }
  258. foreach($this->_keys['public'] as $key => $cert) {
  259. $keys[$key] = openssl_pkey_get_public($cert);
  260. }
  261. $crypt = openssl_seal($value, $encrypted, $encryptedkeys, $keys);
  262. foreach ($keys as $key) {
  263. openssl_free_key($key);
  264. }
  265. if ($crypt === false) {
  266. // require_once 'Zend/Filter/Exception.php';
  267. throw new Zend_Filter_Exception('Openssl was not able to encrypt you content with the given options');
  268. }
  269. $this->_keys['envelope'] = $encryptedkeys;
  270. return $encrypted;
  271. }
  272. /**
  273. * Defined by Zend_Filter_Interface
  274. *
  275. * Decrypts the file $value with the defined settings
  276. *
  277. * @param string $value Content to decrypt
  278. * @return string The decrypted content
  279. * @throws Zend_Filter_Exception
  280. */
  281. public function decrypt($value)
  282. {
  283. $decrypted = "";
  284. $envelope = current($this->getEnvelopeKey());
  285. if (count($this->_keys['private']) !== 1) {
  286. // require_once 'Zend/Filter/Exception.php';
  287. throw new Zend_Filter_Exception('Openssl can only decrypt with one private key');
  288. }
  289. if (empty($envelope)) {
  290. // require_once 'Zend/Filter/Exception.php';
  291. throw new Zend_Filter_Exception('Openssl can only decrypt with one envelope key');
  292. }
  293. foreach($this->_keys['private'] as $key => $cert) {
  294. $keys = openssl_pkey_get_private($cert, $this->getPassphrase());
  295. }
  296. $crypt = openssl_open($value, $decrypted, $envelope, $keys);
  297. openssl_free_key($keys);
  298. if ($crypt === false) {
  299. // require_once 'Zend/Filter/Exception.php';
  300. throw new Zend_Filter_Exception('Openssl was not able to decrypt you content with the given options');
  301. }
  302. return $decrypted;
  303. }
  304. /**
  305. * Returns the adapter name
  306. *
  307. * @return string
  308. */
  309. public function toString()
  310. {
  311. return 'Openssl';
  312. }
  313. }