2019-05-19 22:35:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DSA Public Key
|
|
|
|
*
|
|
|
|
* @category Crypt
|
|
|
|
* @package DSA
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @copyright 2015 Jim Wigginton
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
* @link http://phpseclib.sourceforge.net
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace phpseclib\Crypt\DSA;
|
|
|
|
|
|
|
|
use phpseclib\Crypt\DSA;
|
2019-06-28 02:10:40 +02:00
|
|
|
use phpseclib\Crypt\DSA\Formats\Signature\ASN1 as ASN1Signature;
|
2019-05-19 22:35:29 +02:00
|
|
|
use phpseclib\Crypt\Common;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DSA Public Key
|
|
|
|
*
|
|
|
|
* @package DSA
|
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
class PublicKey extends DSA implements Common\PublicKey
|
|
|
|
{
|
2019-06-26 06:20:37 +02:00
|
|
|
use Common\Traits\Fingerprint;
|
2019-05-19 22:35:29 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify a signature
|
|
|
|
*
|
|
|
|
* @see self::verify()
|
|
|
|
* @access public
|
|
|
|
* @param string $message
|
|
|
|
* @param string $signature
|
|
|
|
* @param string $format optional
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function verify($message, $signature)
|
|
|
|
{
|
|
|
|
$format = $this->format;
|
|
|
|
|
|
|
|
$params = $format::load($signature);
|
|
|
|
if ($params === false || count($params) != 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
extract($params);
|
|
|
|
|
|
|
|
if (self::$engines['OpenSSL'] && in_array($this->hash->getHash(), openssl_get_md_methods())) {
|
|
|
|
$sig = $format != 'ASN1' ? ASN1Signature::save($r, $s) : $signature;
|
|
|
|
|
|
|
|
$result = openssl_verify($message, $sig, $this->toString('PKCS8'), $this->hash->getHash());
|
|
|
|
|
|
|
|
if ($result != -1) {
|
|
|
|
return (bool) $result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$q_1 = $this->q->subtract(self::$one);
|
|
|
|
if (!$r->between(self::$one, $q_1) || !$s->between(self::$one, $q_1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$w = $s->modInverse($this->q);
|
|
|
|
$h = $this->hash->hash($message);
|
|
|
|
$h = $this->bits2int($h);
|
|
|
|
list(, $u1) = $h->multiply($w)->divide($this->q);
|
|
|
|
list(, $u2) = $r->multiply($w)->divide($this->q);
|
|
|
|
$v1 = $this->g->powMod($u1, $this->p);
|
|
|
|
$v2 = $this->y->powMod($u2, $this->p);
|
|
|
|
list(, $v) = $v1->multiply($v2)->divide($this->p);
|
|
|
|
list(, $v) = $v->divide($this->q);
|
|
|
|
|
|
|
|
return $v->equals($r);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the public key
|
|
|
|
*
|
|
|
|
* @param string $type
|
2019-06-01 20:23:11 +02:00
|
|
|
* @param array $options optional
|
2019-05-19 22:35:29 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-06-02 17:02:30 +02:00
|
|
|
public function toString($type, array $options = [])
|
2019-05-19 22:35:29 +02:00
|
|
|
{
|
|
|
|
$type = self::validatePlugin('Keys', $type, 'savePublicKey');
|
|
|
|
|
2019-06-01 20:23:11 +02:00
|
|
|
return $type::savePublicKey($this->p, $this->q, $this->g, $this->y, $options);
|
2019-05-19 22:35:29 +02:00
|
|
|
}
|
|
|
|
}
|