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