PageRenderTime 54ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins_repo/openXWorkflow/www/admin/plugins/openXWorkflow/library/Zend/Filter/Encrypt/Openssl.php

https://github.com/orchestra-io/sample-openx
PHP | 345 lines | 181 code | 38 blank | 126 comment | 24 complexity | cbf5920f47d4dcc8c33744e0f757ee02 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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: $
  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-2008 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. * 'key' => encryption key string
  39. * 'algorithm' => algorithm to use
  40. * 'algorithm_directory' => directory where to find the algorithm
  41. * 'mode' => encryption mode to use
  42. * 'modedirectory' => directory where to find the mode
  43. * ))
  44. */
  45. protected $_keys = array(
  46. 'public' => array(),
  47. 'private' => array(),
  48. 'envelope' => array()
  49. );
  50. /**
  51. * Internal passphrase
  52. *
  53. * @var string
  54. */
  55. protected $_passphrase;
  56. /**
  57. * Class constructor
  58. *
  59. * @param string|array $oldfile File which should be renamed/moved
  60. * @param string|array $newfile New filename, when not set $oldfile will be used as new filename
  61. * for $value when filtering
  62. * @param boolean $overwrite If set to true, it will overwrite existing files
  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. $this->setPublicKey($options);
  74. }
  75. /**
  76. * Returns the set encryption options
  77. *
  78. * @param string|array $keys Key with type association
  79. * @return Zend_Filter_Encrypt_Openssl
  80. */
  81. protected function setKeys($keys)
  82. {
  83. if (!is_array($keys)) {
  84. require_once 'Zend/Filter/Exception.php';
  85. throw new Zend_Filter_Exception('Invalid options argument provided to filter');
  86. }
  87. foreach ($keys as $type => $key) {
  88. if (is_file($key) and is_readable($key)) {
  89. $file = fopen($key, 'r');
  90. $cert = fread($file, 8192);
  91. fclose($file);
  92. } else {
  93. $cert = $key;
  94. $key = count($this->_keys[$type]);
  95. }
  96. switch ($type) {
  97. case 'public':
  98. $test = openssl_pkey_get_public($cert);
  99. if ($test === false) {
  100. require_once 'Zend/Filter/Exception.php';
  101. throw new Zend_Filter_Exception("Public key '{$cert}' not valid");
  102. }
  103. openssl_free_key($test);
  104. $this->_keys['public'][$key] = $cert;
  105. break;
  106. case 'private':
  107. $test = openssl_pkey_get_private($cert, $this->_passphrase);
  108. if ($test === false) {
  109. require_once 'Zend/Filter/Exception.php';
  110. throw new Zend_Filter_Exception("Private key '{$cert}' not valid");
  111. }
  112. openssl_free_key($test);
  113. $this->_keys['private'][$key] = $cert;
  114. break;
  115. case 'envelope':
  116. $this->_keys['envelope'][$key] = $cert;
  117. break;
  118. default:
  119. require_once 'Zend/Filter/Exception.php';
  120. throw new Zend_Filter_Exception("Unknown key type '{$type}'");
  121. }
  122. }
  123. return $this;
  124. }
  125. /**
  126. * Returns all public keys
  127. *
  128. * @return array
  129. */
  130. public function getPublicKey()
  131. {
  132. return $this->_keys['public'];
  133. }
  134. /**
  135. * Sets public keys
  136. *
  137. * @param string|array $key Public keys
  138. * @return Zend_Filter_Encrypt_Openssl
  139. */
  140. public function setPublicKey($key)
  141. {
  142. if (is_array($key)) {
  143. foreach($key as $type => $option) {
  144. if ($type !== 'public') {
  145. $key['public'] = $option;
  146. unset($key[$type]);
  147. }
  148. }
  149. } else {
  150. $key = array('public' => $key);
  151. }
  152. return $this->setKeys($key);
  153. }
  154. /**
  155. * Returns all private keys
  156. *
  157. * @return array
  158. */
  159. public function getPrivateKey()
  160. {
  161. return $this->_keys['private'];
  162. }
  163. /**
  164. * Sets private keys
  165. *
  166. * @param string $key Private key
  167. * @param string $passphrase
  168. * @return Zend_Filter_Encrypt_Openssl
  169. */
  170. public function setPrivateKey($key, $passphrase = null)
  171. {
  172. if (is_array($key)) {
  173. foreach($key as $type => $option) {
  174. if ($type !== 'private') {
  175. $key['private'] = $option;
  176. unset($key[$type]);
  177. }
  178. }
  179. } else {
  180. $key = array('private' => $key);
  181. }
  182. if ($passphrase !== null) {
  183. $this->setPassphrase($passphrase);
  184. }
  185. return $this->setKeys($key);
  186. }
  187. /**
  188. * Returns all envelope keys
  189. *
  190. * @return array
  191. */
  192. public function getEnvelopeKey()
  193. {
  194. return $this->_keys['envelope'];
  195. }
  196. /**
  197. * Sets envelope keys
  198. *
  199. * @param string|array $options Envelope keys
  200. * @return Zend_Filter_Encrypt_Openssl
  201. */
  202. public function setEnvelopeKey($key)
  203. {
  204. if (is_array($key)) {
  205. foreach($key as $type => $option) {
  206. if ($type !== 'envelope') {
  207. $key['envelope'] = $option;
  208. unset($key[$type]);
  209. }
  210. }
  211. } else {
  212. $key = array('envelope' => $key);
  213. }
  214. return $this->setKeys($key);
  215. }
  216. /**
  217. * Returns the passphrase
  218. *
  219. * @return string
  220. */
  221. public function getPassphrase()
  222. {
  223. return $this->_passphrase;
  224. }
  225. /**
  226. * Sets a new passphrase
  227. *
  228. * @param string $passphrase
  229. * @return Zend_Filter_Encrypt_Openssl
  230. */
  231. public function setPassphrase($passphrase)
  232. {
  233. $this->_passphrase = $passphrase;
  234. return $this;
  235. }
  236. /**
  237. * Encrypts the file $value with the defined settings
  238. * Note that you also need the "encrypted" keys to be able to decrypt
  239. *
  240. * @param string $value Content to encrypt
  241. * @return string The encrypted content
  242. * @throws Zend_Filter_Exception
  243. */
  244. public function encrypt($value)
  245. {
  246. $encrypted = array();
  247. $encryptedkeys = array();
  248. if (count($this->_keys['public']) == 0) {
  249. require_once 'Zend/Filter/Exception.php';
  250. throw new Zend_Filter_Exception('Openssl can not encrypt without public keys');
  251. }
  252. foreach($this->_keys['public'] as $key => $cert) {
  253. $keys[$key] = openssl_pkey_get_public($cert);
  254. }
  255. $crypt = openssl_seal($value, $encrypted, $encryptedkeys, $keys);
  256. foreach ($keys as $key) {
  257. openssl_free_key($key);
  258. }
  259. if ($crypt === false) {
  260. require_once 'Zend/Filter/Exception.php';
  261. throw new Zend_Filter_Exception('Openssl was not able to encrypt you content with the given options');
  262. }
  263. $this->_keys['envelope'] = $encryptedkeys;
  264. return $encrypted;
  265. }
  266. /**
  267. * Defined by Zend_Filter_Interface
  268. *
  269. * Decrypts the file $value with the defined settings
  270. *
  271. * @param string $value Content to decrypt
  272. * @return string The decrypted content
  273. * @throws Zend_Filter_Exception
  274. */
  275. public function decrypt($value)
  276. {
  277. $decrypted = "";
  278. $envelope = current($this->getEnvelopeKey());
  279. if (count($this->_keys['private']) !== 1) {
  280. require_once 'Zend/Filter/Exception.php';
  281. throw new Zend_Filter_Exception('Openssl can only decrypt with one private key');
  282. }
  283. if (empty($envelope)) {
  284. require_once 'Zend/Filter/Exception.php';
  285. throw new Zend_Filter_Exception('Openssl can only decrypt with one envelope key');
  286. }
  287. foreach($this->_keys['private'] as $key => $cert) {
  288. $keys = openssl_pkey_get_private($cert, $this->getPassphrase());
  289. }
  290. $crypt = openssl_open($value, $decrypted, $envelope, $keys);
  291. openssl_free_key($keys);
  292. if ($crypt === false) {
  293. require_once 'Zend/Filter/Exception.php';
  294. throw new Zend_Filter_Exception('Openssl was not able to decrypt you content with the given options');
  295. }
  296. return $decrypted;
  297. }
  298. /**
  299. * Returns the adapter name
  300. *
  301. * @return string
  302. */
  303. public function toString()
  304. {
  305. return 'Openssl';
  306. }
  307. }