Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DiffieHellman.php
Go to the documentation of this file.
1 <?php
34 {
35 
42  public static $useOpenssl = true;
43 
49  private $_prime = null;
50 
57  private $_generator = null;
58 
65  private $_privateKey = null;
66 
72  private $_math = null;
73 
79  private $_publicKey = null;
80 
87  private $_secretKey = null;
88 
92  const BINARY = 'binary';
93  const NUMBER = 'number';
94  const BTWOC = 'btwoc';
95 
106  public function __construct($prime, $generator, $privateKey = null, $privateKeyType = self::NUMBER)
107  {
108  $this->setPrime($prime);
109  $this->setGenerator($generator);
110  if ($privateKey !== null) {
111  $this->setPrivateKey($privateKey, $privateKeyType);
112  }
113  $this->setBigIntegerMath();
114  }
115 
122  public function generateKeys()
123  {
124  if (function_exists('openssl_dh_compute_key') && self::$useOpenssl !== false) {
125  $details = array();
126  $details['p'] = $this->getPrime();
127  $details['g'] = $this->getGenerator();
128  if ($this->hasPrivateKey()) {
129  $details['priv_key'] = $this->getPrivateKey();
130  }
131  $opensslKeyResource = openssl_pkey_new( array('dh' => $details) );
132  $data = openssl_pkey_get_details($opensslKeyResource);
133  $this->setPrivateKey($data['dh']['priv_key'], self::BINARY);
134  $this->setPublicKey($data['dh']['pub_key'], self::BINARY);
135  } else {
136  // Private key is lazy generated in the absence of PHP 5.3's ext/openssl
137  $publicKey = $this->_math->powmod($this->getGenerator(), $this->getPrivateKey(), $this->getPrime());
138  $this->setPublicKey($publicKey);
139  }
140  return $this;
141  }
142 
151  public function setPublicKey($number, $type = self::NUMBER)
152  {
153  if ($type == self::BINARY) {
154  $number = $this->_math->fromBinary($number);
155  }
156  if (!preg_match("/^\d+$/", $number)) {
157  #require_once('Zend/Crypt/DiffieHellman/Exception.php');
158  throw new Zend_Crypt_DiffieHellman_Exception('invalid parameter; not a positive natural number');
159  }
160  $this->_publicKey = (string) $number;
161  return $this;
162  }
163 
172  public function getPublicKey($type = self::NUMBER)
173  {
174  if ($this->_publicKey === null) {
175  #require_once 'Zend/Crypt/DiffieHellman/Exception.php';
176  throw new Zend_Crypt_DiffieHellman_Exception('A public key has not yet been generated using a prior call to generateKeys()');
177  }
178  if ($type == self::BINARY) {
179  return $this->_math->toBinary($this->_publicKey);
180  } elseif ($type == self::BTWOC) {
181  return $this->_math->btwoc($this->_math->toBinary($this->_publicKey));
182  }
183  return $this->_publicKey;
184  }
185 
203  public function computeSecretKey($publicKey, $type = self::NUMBER, $output = self::NUMBER)
204  {
205  if ($type == self::BINARY) {
206  $publicKey = $this->_math->fromBinary($publicKey);
207  }
208  if (!preg_match("/^\d+$/", $publicKey)) {
209  #require_once('Zend/Crypt/DiffieHellman/Exception.php');
210  throw new Zend_Crypt_DiffieHellman_Exception('invalid parameter; not a positive natural number');
211  }
212  if (function_exists('openssl_dh_compute_key') && self::$useOpenssl !== false) {
213  $this->_secretKey = openssl_dh_compute_key($publicKey, $this->getPublicKey());
214  } else {
215  $this->_secretKey = $this->_math->powmod($publicKey, $this->getPrivateKey(), $this->getPrime());
216  }
217  return $this->getSharedSecretKey($output);
218  }
219 
227  public function getSharedSecretKey($type = self::NUMBER)
228  {
229  if (!isset($this->_secretKey)) {
230  #require_once('Zend/Crypt/DiffieHellman/Exception.php');
231  throw new Zend_Crypt_DiffieHellman_Exception('A secret key has not yet been computed; call computeSecretKey()');
232  }
233  if ($type == self::BINARY) {
234  return $this->_math->toBinary($this->_secretKey);
235  } elseif ($type == self::BTWOC) {
236  return $this->_math->btwoc($this->_math->toBinary($this->_secretKey));
237  }
238  return $this->_secretKey;
239  }
240 
248  public function setPrime($number)
249  {
250  if (!preg_match("/^\d+$/", $number) || $number < 11) {
251  #require_once('Zend/Crypt/DiffieHellman/Exception.php');
252  throw new Zend_Crypt_DiffieHellman_Exception('invalid parameter; not a positive natural number or too small: should be a large natural number prime');
253  }
254  $this->_prime = (string) $number;
255  return $this;
256  }
257 
264  public function getPrime()
265  {
266  if (!isset($this->_prime)) {
267  #require_once('Zend/Crypt/DiffieHellman/Exception.php');
268  throw new Zend_Crypt_DiffieHellman_Exception('No prime number has been set');
269  }
270  return $this->_prime;
271  }
272 
280  public function setGenerator($number)
281  {
282  if (!preg_match("/^\d+$/", $number) || $number < 2) {
283  #require_once('Zend/Crypt/DiffieHellman/Exception.php');
284  throw new Zend_Crypt_DiffieHellman_Exception('invalid parameter; not a positive natural number greater than 1');
285  }
286  $this->_generator = (string) $number;
287  return $this;
288  }
289 
296  public function getGenerator()
297  {
298  if (!isset($this->_generator)) {
299  #require_once('Zend/Crypt/DiffieHellman/Exception.php');
300  throw new Zend_Crypt_DiffieHellman_Exception('No generator number has been set');
301  }
302  return $this->_generator;
303  }
304 
313  public function setPrivateKey($number, $type = self::NUMBER)
314  {
315  if ($type == self::BINARY) {
316  $number = $this->_math->fromBinary($number);
317  }
318  if (!preg_match("/^\d+$/", $number)) {
319  #require_once('Zend/Crypt/DiffieHellman/Exception.php');
320  throw new Zend_Crypt_DiffieHellman_Exception('invalid parameter; not a positive natural number');
321  }
322  $this->_privateKey = (string) $number;
323  return $this;
324  }
325 
332  public function getPrivateKey($type = self::NUMBER)
333  {
334  if (!$this->hasPrivateKey()) {
335  $this->setPrivateKey($this->_generatePrivateKey(), self::BINARY);
336  }
337  if ($type == self::BINARY) {
338  return $this->_math->toBinary($this->_privateKey);
339  } elseif ($type == self::BTWOC) {
340  return $this->_math->btwoc($this->_math->toBinary($this->_privateKey));
341  }
342  return $this->_privateKey;
343  }
344 
350  public function hasPrivateKey()
351  {
352  return isset($this->_privateKey);
353  }
354 
364  public function setBigIntegerMath($extension = null)
365  {
369  #require_once 'Zend/Crypt/Math.php';
370  $this->_math = new Zend_Crypt_Math($extension);
371  }
372 
382  protected function _generatePrivateKey()
383  {
384  $rand = $this->_math->rand($this->getGenerator(), $this->getPrime());
385  return $rand;
386  }
387 
388 }
setBigIntegerMath($extension=null)
getPrivateKey($type=self::NUMBER)
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$number
Definition: details.phtml:22
getPublicKey($type=self::NUMBER)
$details
Definition: vault.phtml:10
computeSecretKey($publicKey, $type=self::NUMBER, $output=self::NUMBER)
$type
Definition: item.phtml:13
setPrivateKey($number, $type=self::NUMBER)
__construct($prime, $generator, $privateKey=null, $privateKeyType=self::NUMBER)
setPublicKey($number, $type=self::NUMBER)
getSharedSecretKey($type=self::NUMBER)