Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Zend_Filter_Encrypt_Openssl Class Reference
Inheritance diagram for Zend_Filter_Encrypt_Openssl:
Zend_Filter_Encrypt_Interface

Public Member Functions

 __construct ($options=array())
 
 getPublicKey ()
 
 setPublicKey ($key)
 
 getPrivateKey ()
 
 setPrivateKey ($key, $passphrase=null)
 
 getEnvelopeKey ()
 
 setEnvelopeKey ($key)
 
 getPassphrase ()
 
 setPassphrase ($passphrase)
 
 getCompression ()
 
 setCompression ($compression)
 
 getPackage ()
 
 setPackage ($package)
 
 encrypt ($value)
 
 decrypt ($value)
 
 toString ()
 

Protected Member Functions

 _setKeys ($keys)
 

Protected Attributes

 $_keys
 
 $_passphrase
 
 $_compression
 
 $_package = false
 

Detailed Description

Definition at line 35 of file Openssl.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $options = array())

Class constructor Available options 'public' => public key 'private' => private key 'envelope' => envelope key 'passphrase' => passphrase 'compression' => compress value with this compression adapter 'package' => pack envelope keys into encrypted string, simplifies decryption

Parameters
string | array$optionsOptions for this adapter

Definition at line 84 of file Openssl.php.

85  {
86  if (!extension_loaded('openssl')) {
87  #require_once 'Zend/Filter/Exception.php';
88  throw new Zend_Filter_Exception('This filter needs the openssl extension');
89  }
90 
91  if ($options instanceof Zend_Config) {
92  $options = $options->toArray();
93  }
94 
95  if (!is_array($options)) {
96  $options = array('public' => $options);
97  }
98 
99  if (array_key_exists('passphrase', $options)) {
100  $this->setPassphrase($options['passphrase']);
101  unset($options['passphrase']);
102  }
103 
104  if (array_key_exists('compression', $options)) {
105  $this->setCompression($options['compression']);
106  unset($options['compress']);
107  }
108 
109  if (array_key_exists('package', $options)) {
110  $this->setPackage($options['package']);
111  unset($options['package']);
112  }
113 
114  $this->_setKeys($options);
115  }
setCompression($compression)
Definition: Openssl.php:314
setPassphrase($passphrase)
Definition: Openssl.php:292

Member Function Documentation

◆ _setKeys()

_setKeys (   $keys)
protected

Sets the encryption keys

Parameters
string | array$keysKey with type association
Returns
Zend_Filter_Encrypt_Openssl

Definition at line 123 of file Openssl.php.

124  {
125  if (!is_array($keys)) {
126  #require_once 'Zend/Filter/Exception.php';
127  throw new Zend_Filter_Exception('Invalid options argument provided to filter');
128  }
129 
130  foreach ($keys as $type => $key) {
131  if (ctype_print($key) && is_file(realpath($key)) && is_readable($key)) {
132  $file = fopen($key, 'r');
133  $cert = fread($file, 8192);
134  fclose($file);
135  } else {
136  $cert = $key;
137  $key = count($this->_keys[$type]);
138  }
139 
140  switch ($type) {
141  case 'public':
142  $test = openssl_pkey_get_public($cert);
143  if ($test === false) {
144  #require_once 'Zend/Filter/Exception.php';
145  throw new Zend_Filter_Exception("Public key '{$cert}' not valid");
146  }
147 
148  openssl_free_key($test);
149  $this->_keys['public'][$key] = $cert;
150  break;
151  case 'private':
152  $test = openssl_pkey_get_private($cert, $this->_passphrase);
153  if ($test === false) {
154  #require_once 'Zend/Filter/Exception.php';
155  throw new Zend_Filter_Exception("Private key '{$cert}' not valid");
156  }
157 
158  openssl_free_key($test);
159  $this->_keys['private'][$key] = $cert;
160  break;
161  case 'envelope':
162  $this->_keys['envelope'][$key] = $cert;
163  break;
164  default:
165  break;
166  }
167  }
168 
169  return $this;
170  }
$type
Definition: item.phtml:13

◆ decrypt()

decrypt (   $value)

Defined by Zend_Filter_Interface

Decrypts $value with the defined settings

Parameters
string$valueContent to decrypt
Returns
string The decrypted content
Exceptions
Zend_Filter_Exception

Implements Zend_Filter_Encrypt_Interface.

Definition at line 421 of file Openssl.php.

422  {
423  $decrypted = "";
424  $envelope = current($this->getEnvelopeKey());
425 
426  if (count($this->_keys['private']) !== 1) {
427  #require_once 'Zend/Filter/Exception.php';
428  throw new Zend_Filter_Exception('Please give a private key for decryption with Openssl');
429  }
430 
431  if (!$this->_package && empty($envelope)) {
432  #require_once 'Zend/Filter/Exception.php';
433  throw new Zend_Filter_Exception('Please give a envelope key for decryption with Openssl');
434  }
435 
436  foreach($this->_keys['private'] as $key => $cert) {
437  $keys = openssl_pkey_get_private($cert, $this->getPassphrase());
438  }
439 
440  if ($this->_package) {
441  $details = openssl_pkey_get_details($keys);
442  if ($details !== false) {
443  $fingerprint = md5($details['key']);
444  } else {
445  $fingerprint = md5("ZendFramework");
446  }
447 
448  $count = unpack('ncount', $value);
449  $count = $count['count'];
450  $length = 2;
451  for($i = $count; $i > 0; --$i) {
452  $header = unpack('H32print/nsize', substr($value, $length, 18));
453  $length += 18;
454  if ($header['print'] == $fingerprint) {
455  $envelope = substr($value, $length, $header['size']);
456  }
457 
458  $length += $header['size'];
459  }
460 
461  // remainder of string is the value to decrypt
462  $value = substr($value, $length);
463  }
464 
465  $crypt = openssl_open($value, $decrypted, $envelope, $keys);
466  openssl_free_key($keys);
467 
468  if ($crypt === false) {
469  #require_once 'Zend/Filter/Exception.php';
470  throw new Zend_Filter_Exception('Openssl was not able to decrypt you content with the given options');
471  }
472 
473  // decompress after decryption
474  if (!empty($this->_compression)) {
475  #require_once 'Zend/Filter/Decompress.php';
476  $decompress = new Zend_Filter_Decompress($this->_compression);
477  $decrypted = $decompress->filter($decrypted);
478  }
479 
480  return $decrypted;
481  }
$details
Definition: vault.phtml:10
$count
Definition: recent.phtml:13
$value
Definition: gender.phtml:16
$i
Definition: gallery.phtml:31

◆ encrypt()

encrypt (   $value)

Encrypts $value with the defined settings Note that you also need the "encrypted" keys to be able to decrypt

Parameters
string$valueContent to encrypt
Returns
string The encrypted content
Exceptions
Zend_Filter_Exception

Implements Zend_Filter_Encrypt_Interface.

Definition at line 354 of file Openssl.php.

355  {
356  $encrypted = array();
357  $encryptedkeys = array();
358 
359  if (count($this->_keys['public']) == 0) {
360  #require_once 'Zend/Filter/Exception.php';
361  throw new Zend_Filter_Exception('Openssl can not encrypt without public keys');
362  }
363 
364  $keys = array();
365  $fingerprints = array();
366  $count = -1;
367  foreach($this->_keys['public'] as $key => $cert) {
368  $keys[$key] = openssl_pkey_get_public($cert);
369  if ($this->_package) {
370  $details = openssl_pkey_get_details($keys[$key]);
371  if ($details === false) {
372  $details = array('key' => 'ZendFramework');
373  }
374 
375  ++$count;
376  $fingerprints[$count] = md5($details['key']);
377  }
378  }
379 
380  // compress prior to encryption
381  if (!empty($this->_compression)) {
382  #require_once 'Zend/Filter/Compress.php';
383  $compress = new Zend_Filter_Compress($this->_compression);
384  $value = $compress->filter($value);
385  }
386 
387  $crypt = openssl_seal($value, $encrypted, $encryptedkeys, $keys);
388  foreach ($keys as $key) {
389  openssl_free_key($key);
390  }
391 
392  if ($crypt === false) {
393  #require_once 'Zend/Filter/Exception.php';
394  throw new Zend_Filter_Exception('Openssl was not able to encrypt your content with the given options');
395  }
396 
397  $this->_keys['envelope'] = $encryptedkeys;
398 
399  // Pack data and envelope keys into single string
400  if ($this->_package) {
401  $header = pack('n', count($this->_keys['envelope']));
402  foreach($this->_keys['envelope'] as $key => $envKey) {
403  $header .= pack('H32n', $fingerprints[$key], strlen($envKey)) . $envKey;
404  }
405 
406  $encrypted = $header . $encrypted;
407  }
408 
409  return $encrypted;
410  }
$details
Definition: vault.phtml:10
$count
Definition: recent.phtml:13
$value
Definition: gender.phtml:16

◆ getCompression()

getCompression ( )

Returns the compression

Returns
array

Definition at line 303 of file Openssl.php.

304  {
305  return $this->_compression;
306  }

◆ getEnvelopeKey()

getEnvelopeKey ( )

Returns all envelope keys

Returns
array

Definition at line 248 of file Openssl.php.

249  {
250  $key = $this->_keys['envelope'];
251  return $key;
252  }

◆ getPackage()

getPackage ( )

Returns if header should be packaged

Returns
boolean

Definition at line 329 of file Openssl.php.

330  {
331  return $this->_package;
332  }

◆ getPassphrase()

getPassphrase ( )

Returns the passphrase

Returns
string

Definition at line 281 of file Openssl.php.

282  {
283  return $this->_passphrase;
284  }

◆ getPrivateKey()

getPrivateKey ( )

Returns all private keys

Returns
array

Definition at line 210 of file Openssl.php.

211  {
212  $key = $this->_keys['private'];
213  return $key;
214  }

◆ getPublicKey()

getPublicKey ( )

Returns all public keys

Returns
array

Definition at line 177 of file Openssl.php.

178  {
179  $key = $this->_keys['public'];
180  return $key;
181  }

◆ setCompression()

setCompression (   $compression)

Sets a internal compression for values to encrypt

Parameters
string | array$compression
Returns
Zend_Filter_Encrypt_Openssl

Definition at line 314 of file Openssl.php.

315  {
316  if (is_string($this->_compression)) {
317  $compression = array('adapter' => $compression);
318  }
319 
320  $this->_compression = $compression;
321  return $this;
322  }

◆ setEnvelopeKey()

setEnvelopeKey (   $key)

Sets envelope keys

Parameters
string | array$optionsEnvelope keys
Returns
Zend_Filter_Encrypt_Openssl

Definition at line 260 of file Openssl.php.

261  {
262  if (is_array($key)) {
263  foreach($key as $type => $option) {
264  if ($type !== 'envelope') {
265  $key['envelope'] = $option;
266  unset($key[$type]);
267  }
268  }
269  } else {
270  $key = array('envelope' => $key);
271  }
272 
273  return $this->_setKeys($key);
274  }
$type
Definition: item.phtml:13

◆ setPackage()

setPackage (   $package)

Sets if the envelope keys should be included in the encrypted value

Parameters
boolean$package
Returns
Zend_Filter_Encrypt_Openssl

Definition at line 340 of file Openssl.php.

341  {
342  $this->_package = (boolean) $package;
343  return $this;
344  }

◆ setPassphrase()

setPassphrase (   $passphrase)

Sets a new passphrase

Parameters
string$passphrase
Returns
Zend_Filter_Encrypt_Openssl

Definition at line 292 of file Openssl.php.

293  {
294  $this->_passphrase = $passphrase;
295  return $this;
296  }

◆ setPrivateKey()

setPrivateKey (   $key,
  $passphrase = null 
)

Sets private keys

Parameters
string$keyPrivate key
string$passphrase
Returns
Zend_Filter_Encrypt_Openssl

Definition at line 223 of file Openssl.php.

224  {
225  if (is_array($key)) {
226  foreach($key as $type => $option) {
227  if ($type !== 'private') {
228  $key['private'] = $option;
229  unset($key[$type]);
230  }
231  }
232  } else {
233  $key = array('private' => $key);
234  }
235 
236  if ($passphrase !== null) {
237  $this->setPassphrase($passphrase);
238  }
239 
240  return $this->_setKeys($key);
241  }
$type
Definition: item.phtml:13
setPassphrase($passphrase)
Definition: Openssl.php:292

◆ setPublicKey()

setPublicKey (   $key)

Sets public keys

Parameters
string | array$keyPublic keys
Returns
Zend_Filter_Encrypt_Openssl

Definition at line 189 of file Openssl.php.

190  {
191  if (is_array($key)) {
192  foreach($key as $type => $option) {
193  if ($type !== 'public') {
194  $key['public'] = $option;
195  unset($key[$type]);
196  }
197  }
198  } else {
199  $key = array('public' => $key);
200  }
201 
202  return $this->_setKeys($key);
203  }
$type
Definition: item.phtml:13

◆ toString()

toString ( )

Returns the adapter name

Returns
string

Definition at line 488 of file Openssl.php.

489  {
490  return 'Openssl';
491  }

Field Documentation

◆ $_compression

$_compression
protected

Definition at line 63 of file Openssl.php.

◆ $_keys

$_keys
protected
Initial value:
= array(
'public' => array(),
'private' => array(),
'envelope' => array()
)

Definitions for encryption array( 'public' => public keys 'private' => private keys 'envelope' => resulting envelope keys )

Definition at line 45 of file Openssl.php.

◆ $_package

$_package = false
protected

Definition at line 70 of file Openssl.php.

◆ $_passphrase

$_passphrase
protected

Definition at line 56 of file Openssl.php.


The documentation for this class was generated from the following file: