Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Rsa.php
Go to the documentation of this file.
1 <?php
26 #require_once 'Zend/Crypt/Rsa/Key/Private.php';
27 
31 #require_once 'Zend/Crypt/Rsa/Key/Public.php';
32 
40 {
41 
42  const BINARY = 'binary';
43  const BASE64 = 'base64';
44 
45  protected $_privateKey;
46 
47  protected $_publicKey;
48 
52  protected $_pemString;
53 
54  protected $_pemPath;
55 
57 
58  protected $_certificatePath;
59 
60  protected $_hashAlgorithm;
61 
62  protected $_passPhrase;
63 
70  public function __construct(array $options = null)
71  {
72  if (!extension_loaded('openssl')) {
73  #require_once 'Zend/Crypt/Rsa/Exception.php';
74  throw new Zend_Crypt_Rsa_Exception('Zend_Crypt_Rsa requires openssl extension to be loaded.');
75  }
76 
77  // Set _hashAlgorithm property when we are sure, that openssl extension is loaded
78  // and OPENSSL_ALGO_SHA1 constant is available
79  $this->_hashAlgorithm = OPENSSL_ALGO_SHA1;
80 
81  if (isset($options)) {
82  $this->setOptions($options);
83  }
84  }
85 
86  public function setOptions(array $options)
87  {
88  if (isset($options['passPhrase'])) {
89  $this->_passPhrase = $options['passPhrase'];
90  }
91  foreach ($options as $option=>$value) {
92  switch ($option) {
93  case 'pemString':
94  $this->setPemString($value);
95  break;
96  case 'pemPath':
97  $this->setPemPath($value);
98  break;
99  case 'certificateString':
101  break;
102  case 'certificatePath':
103  $this->setCertificatePath($value);
104  break;
105  case 'hashAlgorithm':
106  $this->setHashAlgorithm($value);
107  break;
108  }
109  }
110  }
111 
112  public function getPrivateKey()
113  {
114  return $this->_privateKey;
115  }
116 
117  public function getPublicKey()
118  {
119  return $this->_publicKey;
120  }
121 
128  public function sign($data, Zend_Crypt_Rsa_Key_Private $privateKey = null, $format = null)
129  {
130  $signature = '';
131  if (isset($privateKey)) {
132  $opensslKeyResource = $privateKey->getOpensslKeyResource();
133  } else {
134  $opensslKeyResource = $this->_privateKey->getOpensslKeyResource();
135  }
136  $result = openssl_sign(
137  $data, $signature,
138  $opensslKeyResource,
139  $this->getHashAlgorithm()
140  );
141  if ($format == self::BASE64) {
142  return base64_encode($signature);
143  }
144  return $signature;
145  }
146 
153  public function verifySignature($data, $signature, $format = null)
154  {
155  if ($format == self::BASE64) {
156  $signature = base64_decode($signature);
157  }
158  $result = openssl_verify($data, $signature,
159  $this->getPublicKey()->getOpensslKeyResource(),
160  $this->getHashAlgorithm());
161  return $result;
162  }
163 
170  public function encrypt($data, Zend_Crypt_Rsa_Key $key, $format = null)
171  {
172  $encrypted = '';
173  $function = 'openssl_public_encrypt';
174  if ($key instanceof Zend_Crypt_Rsa_Key_Private) {
175  $function = 'openssl_private_encrypt';
176  }
177  $function($data, $encrypted, $key->getOpensslKeyResource());
178  if ($format == self::BASE64) {
179  return base64_encode($encrypted);
180  }
181  return $encrypted;
182  }
183 
190  public function decrypt($data, Zend_Crypt_Rsa_Key $key, $format = null)
191  {
192  $decrypted = '';
193  if ($format == self::BASE64) {
194  $data = base64_decode($data);
195  }
196  $function = 'openssl_private_decrypt';
197  if ($key instanceof Zend_Crypt_Rsa_Key_Public) {
198  $function = 'openssl_public_decrypt';
199  }
200  $function($data, $decrypted, $key->getOpensslKeyResource());
201  return $decrypted;
202  }
203 
211  public function generateKeys(array $configargs = null)
212  {
213  $config = null;
214  $passPhrase = null;
215  if ($configargs !== null) {
216  if (isset($configargs['passPhrase'])) {
217  $passPhrase = $configargs['passPhrase'];
218  unset($configargs['passPhrase']);
219  }
220  $config = $this->_parseConfigArgs($configargs);
221  }
222  $privateKey = null;
223  $publicKey = null;
224  $resource = openssl_pkey_new($config);
225  if (!$resource) {
226  #require_once 'Zend/Crypt/Rsa/Exception.php';
227  throw new Zend_Crypt_Rsa_Exception('Failed to generate a new private key');
228  }
229  // above fails on PHP 5.3
230  openssl_pkey_export($resource, $private, $passPhrase);
231  $privateKey = new Zend_Crypt_Rsa_Key_Private($private, $passPhrase);
232  $details = openssl_pkey_get_details($resource);
233  $publicKey = new Zend_Crypt_Rsa_Key_Public($details['key']);
234  $return = new ArrayObject(array(
235  'privateKey'=>$privateKey,
236  'publicKey'=>$publicKey
237  ), ArrayObject::ARRAY_AS_PROPS);
238  return $return;
239  }
240 
244  public function setPemString($value)
245  {
246  $this->_pemString = $value;
247  try {
248  $this->_privateKey = new Zend_Crypt_Rsa_Key_Private($this->_pemString, $this->_passPhrase);
249  $this->_publicKey = $this->_privateKey->getPublicKey();
250  } catch (Zend_Crypt_Exception $e) {
251  $this->_privateKey = null;
252  $this->_publicKey = new Zend_Crypt_Rsa_Key_Public($this->_pemString);
253  }
254  }
255 
256  public function setPemPath($value)
257  {
258  $this->_pemPath = $value;
259  $this->setPemString(file_get_contents($this->_pemPath));
260  }
261 
262  public function setCertificateString($value)
263  {
264  $this->_certificateString = $value;
265  $this->_publicKey = new Zend_Crypt_Rsa_Key_Public($this->_certificateString, $this->_passPhrase);
266  }
267 
268  public function setCertificatePath($value)
269  {
270  $this->_certificatePath = $value;
271  $this->setCertificateString(file_get_contents($this->_certificatePath));
272  }
273 
274  public function setHashAlgorithm($name)
275  {
276  switch (strtolower($name)) {
277  case 'md2':
278  $this->_hashAlgorithm = OPENSSL_ALGO_MD2;
279  break;
280  case 'md4':
281  $this->_hashAlgorithm = OPENSSL_ALGO_MD4;
282  break;
283  case 'md5':
284  $this->_hashAlgorithm = OPENSSL_ALGO_MD5;
285  break;
286  case 'sha1':
287  $this->_hashAlgorithm = OPENSSL_ALGO_SHA1;
288  break;
289  case 'dss1':
290  $this->_hashAlgorithm = OPENSSL_ALGO_DSS1;
291  break;
292  }
293  }
294 
298  public function getPemString()
299  {
300  return $this->_pemString;
301  }
302 
303  public function getPemPath()
304  {
305  return $this->_pemPath;
306  }
307 
308  public function getCertificateString()
309  {
311  }
312 
313  public function getCertificatePath()
314  {
316  }
317 
318  public function getHashAlgorithm()
319  {
320  return $this->_hashAlgorithm;
321  }
322 
323  protected function _parseConfigArgs(array $config = null)
324  {
325  $configs = array();
326  if (isset($config['private_key_bits'])) {
327  $configs['private_key_bits'] = $config['private_key_bits'];
328  }
329  if (isset($config['privateKeyBits'])) {
330  $configs['private_key_bits'] = $config['privateKeyBits'];
331  }
332  if (!empty($configs)) {
333  return $configs;
334  }
335  return null;
336  }
337 
338 }
setCertificateString($value)
Definition: Rsa.php:262
decrypt($data, Zend_Crypt_Rsa_Key $key, $format=null)
Definition: Rsa.php:190
getHashAlgorithm()
Definition: Rsa.php:318
$config
Definition: fraud_order.php:17
const BASE64
Definition: Rsa.php:43
getPublicKey()
Definition: Rsa.php:117
$details
Definition: vault.phtml:10
setPemString($value)
Definition: Rsa.php:244
$resource
Definition: bulk.php:12
getPrivateKey()
Definition: Rsa.php:112
setOptions(array $options)
Definition: Rsa.php:86
setCertificatePath($value)
Definition: Rsa.php:268
getCertificatePath()
Definition: Rsa.php:313
sign($data, Zend_Crypt_Rsa_Key_Private $privateKey=null, $format=null)
Definition: Rsa.php:128
setPemPath($value)
Definition: Rsa.php:256
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
encrypt($data, Zend_Crypt_Rsa_Key $key, $format=null)
Definition: Rsa.php:170
setHashAlgorithm($name)
Definition: Rsa.php:274
$_hashAlgorithm
Definition: Rsa.php:60
getCertificateString()
Definition: Rsa.php:308
$_certificateString
Definition: Rsa.php:56
__construct(array $options=null)
Definition: Rsa.php:70
const BINARY
Definition: Rsa.php:42
generateKeys(array $configargs=null)
Definition: Rsa.php:211
verifySignature($data, $signature, $format=null)
Definition: Rsa.php:153
_parseConfigArgs(array $config=null)
Definition: Rsa.php:323
getOpensslKeyResource()
Definition: Key.php:55
$_certificatePath
Definition: Rsa.php:58
getPemString()
Definition: Rsa.php:298
getPemPath()
Definition: Rsa.php:303
if(!isset($_GET['name'])) $name
Definition: log.php:14