Merge branch '3.0-type-hinting-backport' into 3.0

This commit is contained in:
terrafrost 2022-07-02 21:49:55 -05:00
commit 1fd995abdf
25 changed files with 129 additions and 96 deletions

View File

@ -344,12 +344,15 @@ abstract class PKCS8 extends PKCS
$meta['meta']['algorithm'] = $algorithm; $meta['meta']['algorithm'] = $algorithm;
$temp = ASN1::decodeBER($decrypted['encryptionAlgorithm']['parameters']); $temp = ASN1::decodeBER($decrypted['encryptionAlgorithm']['parameters']);
if (!$temp) {
throw new \RuntimeException('Unable to decode BER');
}
extract(ASN1::asn1map($temp[0], Maps\PBEParameter::MAP)); extract(ASN1::asn1map($temp[0], Maps\PBEParameter::MAP));
$iterationCount = (int) $iterationCount->toString(); $iterationCount = (int) $iterationCount->toString();
$cipher->setPassword($password, $kdf, $hash, $salt, $iterationCount); $cipher->setPassword($password, $kdf, $hash, $salt, $iterationCount);
$key = $cipher->decrypt($decrypted['encryptedData']); $key = $cipher->decrypt($decrypted['encryptedData']);
$decoded = ASN1::decodeBER($key); $decoded = ASN1::decodeBER($key);
if (empty($decoded)) { if (!$decoded) {
throw new \RuntimeException('Unable to decode BER 2'); throw new \RuntimeException('Unable to decode BER 2');
} }
@ -358,6 +361,9 @@ abstract class PKCS8 extends PKCS
$meta['meta']['algorithm'] = $algorithm; $meta['meta']['algorithm'] = $algorithm;
$temp = ASN1::decodeBER($decrypted['encryptionAlgorithm']['parameters']); $temp = ASN1::decodeBER($decrypted['encryptionAlgorithm']['parameters']);
if (!$temp) {
throw new \RuntimeException('Unable to decode BER');
}
$temp = ASN1::asn1map($temp[0], Maps\PBES2params::MAP); $temp = ASN1::asn1map($temp[0], Maps\PBES2params::MAP);
extract($temp); extract($temp);
@ -365,6 +371,9 @@ abstract class PKCS8 extends PKCS
$meta['meta']['cipher'] = $encryptionScheme['algorithm']; $meta['meta']['cipher'] = $encryptionScheme['algorithm'];
$temp = ASN1::decodeBER($decrypted['encryptionAlgorithm']['parameters']); $temp = ASN1::decodeBER($decrypted['encryptionAlgorithm']['parameters']);
if (!$temp) {
throw new \RuntimeException('Unable to decode BER');
}
$temp = ASN1::asn1map($temp[0], Maps\PBES2params::MAP); $temp = ASN1::asn1map($temp[0], Maps\PBES2params::MAP);
extract($temp); extract($temp);
@ -372,6 +381,9 @@ abstract class PKCS8 extends PKCS
$cipher->setIV($encryptionScheme['parameters']['octetString']); $cipher->setIV($encryptionScheme['parameters']['octetString']);
} else { } else {
$temp = ASN1::decodeBER($encryptionScheme['parameters']); $temp = ASN1::decodeBER($encryptionScheme['parameters']);
if (!$temp) {
throw new \RuntimeException('Unable to decode BER');
}
extract(ASN1::asn1map($temp[0], Maps\RC2CBCParameter::MAP)); extract(ASN1::asn1map($temp[0], Maps\RC2CBCParameter::MAP));
$effectiveKeyLength = (int) $rc2ParametersVersion->toString(); $effectiveKeyLength = (int) $rc2ParametersVersion->toString();
switch ($effectiveKeyLength) { switch ($effectiveKeyLength) {
@ -394,6 +406,9 @@ abstract class PKCS8 extends PKCS
switch ($keyDerivationFunc['algorithm']) { switch ($keyDerivationFunc['algorithm']) {
case 'id-PBKDF2': case 'id-PBKDF2':
$temp = ASN1::decodeBER($keyDerivationFunc['parameters']); $temp = ASN1::decodeBER($keyDerivationFunc['parameters']);
if (!$temp) {
throw new \RuntimeException('Unable to decode BER');
}
$prf = ['algorithm' => 'id-hmacWithSHA1']; $prf = ['algorithm' => 'id-hmacWithSHA1'];
$params = ASN1::asn1map($temp[0], Maps\PBKDF2params::MAP); $params = ASN1::asn1map($temp[0], Maps\PBKDF2params::MAP);
extract($params); extract($params);
@ -412,7 +427,7 @@ abstract class PKCS8 extends PKCS
$cipher->setPassword(...$params); $cipher->setPassword(...$params);
$key = $cipher->decrypt($decrypted['encryptedData']); $key = $cipher->decrypt($decrypted['encryptedData']);
$decoded = ASN1::decodeBER($key); $decoded = ASN1::decodeBER($key);
if (empty($decoded)) { if (!$decoded) {
throw new \RuntimeException('Unable to decode BER 3'); throw new \RuntimeException('Unable to decode BER 3');
} }
break; break;
@ -647,7 +662,7 @@ abstract class PKCS8 extends PKCS
} }
$decoded = ASN1::decodeBER($key); $decoded = ASN1::decodeBER($key);
if (empty($decoded)) { if (!$decoded) {
throw new \RuntimeException('Unable to decode BER'); throw new \RuntimeException('Unable to decode BER');
} }
@ -671,12 +686,18 @@ abstract class PKCS8 extends PKCS
if ($r['encryptionAlgorithm']['algorithm'] == 'id-PBES2') { if ($r['encryptionAlgorithm']['algorithm'] == 'id-PBES2') {
$decoded = ASN1::decodeBER($r['encryptionAlgorithm']['parameters']->element); $decoded = ASN1::decodeBER($r['encryptionAlgorithm']['parameters']->element);
if (!$decoded) {
throw new \RuntimeException('Unable to decode BER');
}
$r['encryptionAlgorithm']['parameters'] = ASN1::asn1map($decoded[0], ASN1\Maps\PBES2params::MAP); $r['encryptionAlgorithm']['parameters'] = ASN1::asn1map($decoded[0], ASN1\Maps\PBES2params::MAP);
$kdf = &$r['encryptionAlgorithm']['parameters']['keyDerivationFunc']; $kdf = &$r['encryptionAlgorithm']['parameters']['keyDerivationFunc'];
switch ($kdf['algorithm']) { switch ($kdf['algorithm']) {
case 'id-PBKDF2': case 'id-PBKDF2':
$decoded = ASN1::decodeBER($kdf['parameters']->element); $decoded = ASN1::decodeBER($kdf['parameters']->element);
if (!$decoded) {
throw new \RuntimeException('Unable to decode BER');
}
$kdf['parameters'] = ASN1::asn1map($decoded[0], Maps\PBKDF2params::MAP); $kdf['parameters'] = ASN1::asn1map($decoded[0], Maps\PBKDF2params::MAP);
} }
} }

View File

@ -326,9 +326,8 @@ abstract class DH extends AsymmetricKey
* OnLoad Handler * OnLoad Handler
* *
* @return bool * @return bool
* @param array $components
*/ */
protected static function onLoad($components) protected static function onLoad(array $components)
{ {
if (!isset($components['privateKey']) && !isset($components['publicKey'])) { if (!isset($components['privateKey']) && !isset($components['publicKey'])) {
$new = new Parameters(); $new = new Parameters();

View File

@ -45,7 +45,7 @@ abstract class PKCS1 extends Progenitor
$key = parent::load($key, $password); $key = parent::load($key, $password);
$decoded = ASN1::decodeBER($key); $decoded = ASN1::decodeBER($key);
if (empty($decoded)) { if (!$decoded) {
throw new \RuntimeException('Unable to decode BER'); throw new \RuntimeException('Unable to decode BER');
} }

View File

@ -90,8 +90,7 @@ abstract class PKCS8 extends Progenitor
$decoded = ASN1::decodeBER($key[$type]); $decoded = ASN1::decodeBER($key[$type]);
switch (true) { switch (true) {
case empty($decoded): case !isset($decoded):
case !is_array($decoded):
case !isset($decoded[0]['content']): case !isset($decoded[0]['content']):
case !$decoded[0]['content'] instanceof BigInteger: case !$decoded[0]['content'] instanceof BigInteger:
throw new \RuntimeException('Unable to decode BER of parameters'); throw new \RuntimeException('Unable to decode BER of parameters');

View File

@ -214,9 +214,8 @@ abstract class DSA extends AsymmetricKey
* OnLoad Handler * OnLoad Handler
* *
* @return bool * @return bool
* @param array $components
*/ */
protected static function onLoad($components) protected static function onLoad(array $components)
{ {
if (!isset(self::$engines['PHP'])) { if (!isset(self::$engines['PHP'])) {
self::useBestEngine(); self::useBestEngine();

View File

@ -52,7 +52,7 @@ abstract class PKCS1 extends Progenitor
$key = parent::load($key, $password); $key = parent::load($key, $password);
$decoded = ASN1::decodeBER($key); $decoded = ASN1::decodeBER($key);
if (empty($decoded)) { if (!$decoded) {
throw new \RuntimeException('Unable to decode BER'); throw new \RuntimeException('Unable to decode BER');
} }

View File

@ -84,7 +84,7 @@ abstract class PKCS8 extends Progenitor
} }
$decoded = ASN1::decodeBER($key[$type . 'Algorithm']['parameters']->element); $decoded = ASN1::decodeBER($key[$type . 'Algorithm']['parameters']->element);
if (empty($decoded)) { if (!$decoded) {
throw new \RuntimeException('Unable to decode BER of parameters'); throw new \RuntimeException('Unable to decode BER of parameters');
} }
$components = ASN1::asn1map($decoded[0], Maps\DSAParams::MAP); $components = ASN1::asn1map($decoded[0], Maps\DSAParams::MAP);

View File

@ -199,9 +199,8 @@ abstract class EC extends AsymmetricKey
* OnLoad Handler * OnLoad Handler
* *
* @return bool * @return bool
* @param array $components
*/ */
protected static function onLoad($components) protected static function onLoad(array $components)
{ {
if (!isset(self::$engines['PHP'])) { if (!isset(self::$engines['PHP'])) {
self::useBestEngine(); self::useBestEngine();

View File

@ -645,7 +645,7 @@ class Prime extends Base
* *
* @return int[] * @return int[]
*/ */
private function getNAFPoints($point, $wnd) private function getNAFPoints(array $point, $wnd)
{ {
if (isset($point['naf'])) { if (isset($point['naf'])) {
return $point['naf']; return $point['naf'];

View File

@ -66,7 +66,7 @@ abstract class PKCS1 extends Progenitor
preg_match('#-*BEGIN EC PRIVATE KEY-*[^-]*-*END EC PRIVATE KEY-*#s', $key, $matches); preg_match('#-*BEGIN EC PRIVATE KEY-*[^-]*-*END EC PRIVATE KEY-*#s', $key, $matches);
$decoded = parent::load($matches[0], $password); $decoded = parent::load($matches[0], $password);
$decoded = ASN1::decodeBER($decoded); $decoded = ASN1::decodeBER($decoded);
if (empty($decoded)) { if (!$decoded) {
throw new \RuntimeException('Unable to decode BER'); throw new \RuntimeException('Unable to decode BER');
} }
@ -82,7 +82,7 @@ abstract class PKCS1 extends Progenitor
preg_match('#-*BEGIN EC PARAMETERS-*[^-]*-*END EC PARAMETERS-*#s', $key, $matches); preg_match('#-*BEGIN EC PARAMETERS-*[^-]*-*END EC PARAMETERS-*#s', $key, $matches);
$decoded = parent::load($matches[0], ''); $decoded = parent::load($matches[0], '');
$decoded = ASN1::decodeBER($decoded); $decoded = ASN1::decodeBER($decoded);
if (empty($decoded)) { if (!$decoded) {
throw new \RuntimeException('Unable to decode BER'); throw new \RuntimeException('Unable to decode BER');
} }
$ecParams = ASN1::asn1map($decoded[0], Maps\ECParameters::MAP); $ecParams = ASN1::asn1map($decoded[0], Maps\ECParameters::MAP);
@ -113,7 +113,7 @@ abstract class PKCS1 extends Progenitor
$key = parent::load($key, $password); $key = parent::load($key, $password);
$decoded = ASN1::decodeBER($key); $decoded = ASN1::decodeBER($key);
if (empty($decoded)) { if (!$decoded) {
throw new \RuntimeException('Unable to decode BER'); throw new \RuntimeException('Unable to decode BER');
} }

View File

@ -98,6 +98,9 @@ abstract class PKCS8 extends Progenitor
} }
$decoded = ASN1::decodeBER($key[$type . 'Algorithm']['parameters']->element); $decoded = ASN1::decodeBER($key[$type . 'Algorithm']['parameters']->element);
if (!$decoded) {
throw new \RuntimeException('Unable to decode BER');
}
$params = ASN1::asn1map($decoded[0], Maps\ECParameters::MAP); $params = ASN1::asn1map($decoded[0], Maps\ECParameters::MAP);
if (!$params) { if (!$params) {
throw new \RuntimeException('Unable to decode the parameters using Maps\ECParameters'); throw new \RuntimeException('Unable to decode the parameters using Maps\ECParameters');
@ -113,6 +116,9 @@ abstract class PKCS8 extends Progenitor
} }
$decoded = ASN1::decodeBER($key['privateKey']); $decoded = ASN1::decodeBER($key['privateKey']);
if (!$decoded) {
throw new \RuntimeException('Unable to decode BER');
}
$key = ASN1::asn1map($decoded[0], Maps\ECPrivateKey::MAP); $key = ASN1::asn1map($decoded[0], Maps\ECPrivateKey::MAP);
if (isset($key['parameters']) && $params != $key['parameters']) { if (isset($key['parameters']) && $params != $key['parameters']) {
throw new \RuntimeException('The PKCS8 parameter field does not match the private key parameter field'); throw new \RuntimeException('The PKCS8 parameter field does not match the private key parameter field');

View File

@ -115,7 +115,7 @@ abstract class XML
* @param bool $decode optional * @param bool $decode optional
* @return \DOMNodeList * @return \DOMNodeList
*/ */
private static function query($xpath, $name, $error = null, $decode = true) private static function query(\DOMXPath $xpath, $name, $error = null, $decode = true)
{ {
$query = '/'; $query = '/';
$names = explode('/', $name); $names = explode('/', $name);

View File

@ -422,9 +422,8 @@ abstract class RSA extends AsymmetricKey
* OnLoad Handler * OnLoad Handler
* *
* @return bool * @return bool
* @param array $components
*/ */
protected static function onLoad($components) protected static function onLoad(array $components)
{ {
$key = $components['isPublicKey'] ? $key = $components['isPublicKey'] ?
new PublicKey() : new PublicKey() :

View File

@ -59,7 +59,7 @@ abstract class PKCS1 extends Progenitor
$key = parent::load($key, $password); $key = parent::load($key, $password);
$decoded = ASN1::decodeBER($key); $decoded = ASN1::decodeBER($key);
if (empty($decoded)) { if (!$decoded) {
throw new \RuntimeException('Unable to decode BER'); throw new \RuntimeException('Unable to decode BER');
} }

View File

@ -60,10 +60,9 @@ class PrivateKey extends RSA implements Common\PrivateKey
* *
* See {@link http://tools.ietf.org/html/rfc3447#section-5.1.2 RFC3447#section-5.1.2}. * See {@link http://tools.ietf.org/html/rfc3447#section-5.1.2 RFC3447#section-5.1.2}.
* *
* @param \phpseclib3\Math\BigInteger $c
* @return bool|\phpseclib3\Math\BigInteger * @return bool|\phpseclib3\Math\BigInteger
*/ */
private function rsadp($c) private function rsadp(BigInteger $c)
{ {
if ($c->compare(self::$zero) < 0 || $c->compare($this->modulus) > 0) { if ($c->compare(self::$zero) < 0 || $c->compare($this->modulus) > 0) {
throw new \OutOfRangeException('Ciphertext representative out of range'); throw new \OutOfRangeException('Ciphertext representative out of range');
@ -76,10 +75,9 @@ class PrivateKey extends RSA implements Common\PrivateKey
* *
* See {@link http://tools.ietf.org/html/rfc3447#section-5.2.1 RFC3447#section-5.2.1}. * See {@link http://tools.ietf.org/html/rfc3447#section-5.2.1 RFC3447#section-5.2.1}.
* *
* @param \phpseclib3\Math\BigInteger $m
* @return bool|\phpseclib3\Math\BigInteger * @return bool|\phpseclib3\Math\BigInteger
*/ */
private function rsasp1($m) private function rsasp1(BigInteger $m)
{ {
if ($m->compare(self::$zero) < 0 || $m->compare($this->modulus) > 0) { if ($m->compare(self::$zero) < 0 || $m->compare($this->modulus) > 0) {
throw new \OutOfRangeException('Signature representative out of range'); throw new \OutOfRangeException('Signature representative out of range');
@ -176,7 +174,7 @@ class PrivateKey extends RSA implements Common\PrivateKey
* @param int $i * @param int $i
* @return \phpseclib3\Math\BigInteger * @return \phpseclib3\Math\BigInteger
*/ */
private function blind($x, $r, $i) private function blind(BigInteger $x, BigInteger $r, $i)
{ {
$x = $x->multiply($r->modPow($this->publicExponent, $this->primes[$i])); $x = $x->multiply($r->modPow($this->publicExponent, $this->primes[$i]));
$x = $x->modPow($this->exponents[$i], $this->primes[$i]); $x = $x->modPow($this->exponents[$i], $this->primes[$i]);

View File

@ -440,7 +440,7 @@ class ANSI
* @param string $char * @param string $char
* @return string * @return string
*/ */
private function processCoordinate($last_attr, $cur_attr, $char) private function processCoordinate(\stdClass $last_attr, \stdClass $cur_attr, $char)
{ {
$output = ''; $output = '';

View File

@ -191,7 +191,7 @@ abstract class ASN1
* Serves a similar purpose to openssl's asn1parse * Serves a similar purpose to openssl's asn1parse
* *
* @param Element|string $encoded * @param Element|string $encoded
* @return array * @return ?array
*/ */
public static function decodeBER($encoded) public static function decodeBER($encoded)
{ {
@ -201,10 +201,12 @@ abstract class ASN1
self::$encoded = $encoded; self::$encoded = $encoded;
$decoded = [self::decode_ber($encoded)]; $decoded = self::decode_ber($encoded);
if ($decoded === false) {
return null;
}
// encapsulate in an array for BC with the old decodeBER return [self::decode_ber($encoded)];
return $decoded;
} }
/** /**
@ -516,12 +518,8 @@ abstract class ASN1
* @param array $special * @param array $special
* @return array|bool|Element|string|null * @return array|bool|Element|string|null
*/ */
public static function asn1map($decoded, $mapping, $special = []) public static function asn1map(array $decoded, $mapping, $special = [])
{ {
if (!is_array($decoded)) {
return false;
}
if (isset($mapping['explicit']) && is_array($decoded['content'])) { if (isset($mapping['explicit']) && is_array($decoded['content'])) {
$decoded = $decoded['content'][0]; $decoded = $decoded['content'][0];
} }
@ -854,7 +852,7 @@ abstract class ASN1
* @param array $special * @param array $special
* @return string * @return string
*/ */
private static function encode_der($source, $mapping, $idx = null, $special = []) private static function encode_der($source, array $mapping, $idx = null, array $special = [])
{ {
if ($source instanceof Element) { if ($source instanceof Element) {
return $source->element; return $source->element;
@ -1310,7 +1308,7 @@ abstract class ASN1
* *
* @param array $oids * @param array $oids
*/ */
public static function loadOIDs($oids) public static function loadOIDs(array $oids)
{ {
self::$reverseOIDs += $oids; self::$reverseOIDs += $oids;
self::$oids = array_flip(self::$reverseOIDs); self::$oids = array_flip(self::$reverseOIDs);
@ -1324,7 +1322,7 @@ abstract class ASN1
* *
* @param array $filters * @param array $filters
*/ */
public static function setFilters($filters) public static function setFilters(array $filters)
{ {
self::$filters = $filters; self::$filters = $filters;
} }

View File

@ -430,7 +430,7 @@ class X509
* *
* Returns an associative array describing the X.509 cert or a false if the cert failed to load * Returns an associative array describing the X.509 cert or a false if the cert failed to load
* *
* @param string $cert * @param array|string $cert
* @param int $mode * @param int $mode
* @return mixed * @return mixed
*/ */
@ -468,7 +468,7 @@ class X509
$decoded = ASN1::decodeBER($cert); $decoded = ASN1::decodeBER($cert);
if (!empty($decoded)) { if ($decoded) {
$x509 = ASN1::asn1map($decoded[0], Maps\Certificate::MAP); $x509 = ASN1::asn1map($decoded[0], Maps\Certificate::MAP);
} }
if (!isset($x509) || $x509 === false) { if (!isset($x509) || $x509 === false) {
@ -507,7 +507,7 @@ class X509
* @param int $format optional * @param int $format optional
* @return string * @return string
*/ */
public function saveX509($cert, $format = self::FORMAT_PEM) public function saveX509(array $cert, $format = self::FORMAT_PEM)
{ {
if (!is_array($cert) || !isset($cert['tbsCertificate'])) { if (!is_array($cert) || !isset($cert['tbsCertificate'])) {
return false; return false;
@ -577,7 +577,7 @@ class X509
* @param array $root (by reference) * @param array $root (by reference)
* @param string $path * @param string $path
*/ */
private function mapInExtensions(&$root, $path) private function mapInExtensions(array &$root, $path)
{ {
$extensions = &$this->subArrayUnchecked($root, $path); $extensions = &$this->subArrayUnchecked($root, $path);
@ -593,6 +593,9 @@ class X509
[static::class, 'decodeNameConstraintIP'] : [static::class, 'decodeNameConstraintIP'] :
[static::class, 'decodeIP']; [static::class, 'decodeIP'];
$decoded = ASN1::decodeBER($value); $decoded = ASN1::decodeBER($value);
if (!$decoded) {
continue;
}
$mapped = ASN1::asn1map($decoded[0], $map, ['iPAddress' => $decoder]); $mapped = ASN1::asn1map($decoded[0], $map, ['iPAddress' => $decoder]);
$value = $mapped === false ? $decoded[0] : $mapped; $value = $mapped === false ? $decoded[0] : $mapped;
@ -607,6 +610,9 @@ class X509
$subvalue = &$value[$j]['policyQualifiers'][$k]['qualifier']; $subvalue = &$value[$j]['policyQualifiers'][$k]['qualifier'];
if ($map !== false) { if ($map !== false) {
$decoded = ASN1::decodeBER($subvalue); $decoded = ASN1::decodeBER($subvalue);
if (!$decoded) {
continue;
}
$mapped = ASN1::asn1map($decoded[0], $map); $mapped = ASN1::asn1map($decoded[0], $map);
$subvalue = $mapped === false ? $decoded[0] : $mapped; $subvalue = $mapped === false ? $decoded[0] : $mapped;
} }
@ -625,7 +631,7 @@ class X509
* @param array $root (by reference) * @param array $root (by reference)
* @param string $path * @param string $path
*/ */
private function mapOutExtensions(&$root, $path) private function mapOutExtensions(array &$root, $path)
{ {
$extensions = &$this->subArray($root, $path, !empty($this->extensionValues)); $extensions = &$this->subArray($root, $path, !empty($this->extensionValues));
@ -722,6 +728,9 @@ class X509
$value = ASN1::encodeDER($values[$j], Maps\AttributeValue::MAP); $value = ASN1::encodeDER($values[$j], Maps\AttributeValue::MAP);
$decoded = ASN1::decodeBER($value); $decoded = ASN1::decodeBER($value);
if (!is_bool($map)) { if (!is_bool($map)) {
if (!$decoded) {
continue;
}
$mapped = ASN1::asn1map($decoded[0], $map); $mapped = ASN1::asn1map($decoded[0], $map);
if ($mapped !== false) { if ($mapped !== false) {
$values[$j] = $mapped; $values[$j] = $mapped;
@ -771,6 +780,9 @@ class X509
if (!is_bool($map)) { if (!is_bool($map)) {
$temp = ASN1::encodeDER($values[$j], $map); $temp = ASN1::encodeDER($values[$j], $map);
$decoded = ASN1::decodeBER($temp); $decoded = ASN1::decodeBER($temp);
if (!$decoded) {
continue;
}
$values[$j] = ASN1::asn1map($decoded[0], Maps\AttributeValue::MAP); $values[$j] = ASN1::asn1map($decoded[0], Maps\AttributeValue::MAP);
} }
} }
@ -786,7 +798,7 @@ class X509
* @param array $root (by reference) * @param array $root (by reference)
* @param string $path * @param string $path
*/ */
private function mapInDNs(&$root, $path) private function mapInDNs(array &$root, $path)
{ {
$dns = &$this->subArray($root, $path); $dns = &$this->subArray($root, $path);
@ -799,6 +811,9 @@ class X509
$map = $this->getMapping($type); $map = $this->getMapping($type);
if (!is_bool($map)) { if (!is_bool($map)) {
$decoded = ASN1::decodeBER($value); $decoded = ASN1::decodeBER($value);
if (!$decoded) {
continue;
}
$value = ASN1::asn1map($decoded[0], $map); $value = ASN1::asn1map($decoded[0], $map);
} }
} }
@ -814,7 +829,7 @@ class X509
* @param array $root (by reference) * @param array $root (by reference)
* @param string $path * @param string $path
*/ */
private function mapOutDNs(&$root, $path) private function mapOutDNs(array &$root, $path)
{ {
$dns = &$this->subArray($root, $path); $dns = &$this->subArray($root, $path);
@ -1679,7 +1694,7 @@ class X509
* @param bool $withType optional * @param bool $withType optional
* @return mixed * @return mixed
*/ */
public function getDNProp($propName, $dn = null, $withType = false) public function getDNProp($propName, array $dn = null, $withType = false)
{ {
if (!isset($dn)) { if (!isset($dn)) {
$dn = $this->dn; $dn = $this->dn;
@ -1721,6 +1736,9 @@ class X509
$map = $this->getMapping($propName); $map = $this->getMapping($propName);
if (!is_bool($map)) { if (!is_bool($map)) {
$decoded = ASN1::decodeBER($v); $decoded = ASN1::decodeBER($v);
if (!$decoded) {
return false;
}
$v = ASN1::asn1map($decoded[0], $map); $v = ASN1::asn1map($decoded[0], $map);
} }
} }
@ -1781,7 +1799,7 @@ class X509
* @param array $dn optional * @param array $dn optional
* @return array|bool|string * @return array|bool|string
*/ */
public function getDN($format = self::DN_ARRAY, $dn = null) public function getDN($format = self::DN_ARRAY, array $dn = null)
{ {
if (!isset($dn)) { if (!isset($dn)) {
$dn = isset($this->currentCert['tbsCertList']) ? $this->currentCert['tbsCertList']['issuer'] : $this->dn; $dn = isset($this->currentCert['tbsCertList']) ? $this->currentCert['tbsCertList']['issuer'] : $this->dn;
@ -2182,7 +2200,7 @@ class X509
$decoded = ASN1::decodeBER($csr); $decoded = ASN1::decodeBER($csr);
if (empty($decoded)) { if (!$decoded) {
$this->currentCert = false; $this->currentCert = false;
return false; return false;
} }
@ -2223,7 +2241,7 @@ class X509
* @param int $format optional * @param int $format optional
* @return string * @return string
*/ */
public function saveCSR($csr, $format = self::FORMAT_PEM) public function saveCSR(array $csr, $format = self::FORMAT_PEM)
{ {
if (!is_array($csr) || !isset($csr['certificationRequestInfo'])) { if (!is_array($csr) || !isset($csr['certificationRequestInfo'])) {
return false; return false;
@ -2295,7 +2313,7 @@ class X509
$decoded = ASN1::decodeBER($spkac); $decoded = ASN1::decodeBER($spkac);
if (empty($decoded)) { if (!$decoded) {
$this->currentCert = false; $this->currentCert = false;
return false; return false;
} }
@ -2332,7 +2350,7 @@ class X509
* @param int $format optional * @param int $format optional
* @return string * @return string
*/ */
public function saveSPKAC($spkac, $format = self::FORMAT_PEM) public function saveSPKAC(array $spkac, $format = self::FORMAT_PEM)
{ {
if (!is_array($spkac) || !isset($spkac['publicKeyAndChallenge'])) { if (!is_array($spkac) || !isset($spkac['publicKeyAndChallenge'])) {
return false; return false;
@ -2393,7 +2411,7 @@ class X509
$decoded = ASN1::decodeBER($crl); $decoded = ASN1::decodeBER($crl);
if (empty($decoded)) { if (!$decoded) {
$this->currentCert = false; $this->currentCert = false;
return false; return false;
} }
@ -2435,7 +2453,7 @@ class X509
* @param int $format optional * @param int $format optional
* @return string * @return string
*/ */
public function saveCRL($crl, $format = self::FORMAT_PEM) public function saveCRL(array $crl, $format = self::FORMAT_PEM)
{ {
if (!is_array($crl) || !isset($crl['tbsCertList'])) { if (!is_array($crl) || !isset($crl['tbsCertList'])) {
return false; return false;
@ -2513,11 +2531,9 @@ class X509
* $subject can be either an existing X.509 cert (if you want to resign it), * $subject can be either an existing X.509 cert (if you want to resign it),
* a CSR or something with the DN and public key explicitly set. * a CSR or something with the DN and public key explicitly set.
* *
* @param \phpseclib3\File\X509 $issuer
* @param \phpseclib3\File\X509 $subject
* @return mixed * @return mixed
*/ */
public function sign($issuer, $subject) public function sign(X509 $issuer, X509 $subject)
{ {
if (!is_object($issuer->privateKey) || empty($issuer->dn)) { if (!is_object($issuer->privateKey) || empty($issuer->dn)) {
return false; return false;
@ -2823,11 +2839,9 @@ class X509
* *
* $issuer's private key needs to be loaded. * $issuer's private key needs to be loaded.
* *
* @param \phpseclib3\File\X509 $issuer
* @param \phpseclib3\File\X509 $crl
* @return mixed * @return mixed
*/ */
public function signCRL($issuer, $crl) public function signCRL(X509 $issuer, X509 $crl)
{ {
if (!is_object($issuer->privateKey) || empty($issuer->dn)) { if (!is_object($issuer->privateKey) || empty($issuer->dn)) {
return false; return false;
@ -3079,7 +3093,7 @@ class X509
* @param string $path * @param string $path
* @return boolean * @return boolean
*/ */
private function isSubArrayValid($root, $path) private function isSubArrayValid(array $root, $path)
{ {
if (!is_array($root)) { if (!is_array($root)) {
return false; return false;
@ -3115,7 +3129,7 @@ class X509
* @param bool $create optional * @param bool $create optional
* @return array|false * @return array|false
*/ */
private function &subArrayUnchecked(&$root, $path, $create = false) private function &subArrayUnchecked(array &$root, $path, $create = false)
{ {
$false = false; $false = false;
@ -3142,7 +3156,7 @@ class X509
* @param bool $create optional * @param bool $create optional
* @return array|false * @return array|false
*/ */
private function &subArray(&$root, $path, $create = false) private function &subArray(array &$root = null, $path, $create = false)
{ {
$false = false; $false = false;
@ -3177,7 +3191,7 @@ class X509
* @param bool $create optional * @param bool $create optional
* @return array|false * @return array|false
*/ */
private function &extensions(&$root, $path = null, $create = false) private function &extensions(array &$root = null, $path = null, $create = false)
{ {
if (!isset($root)) { if (!isset($root)) {
$root = $this->currentCert; $root = $this->currentCert;
@ -3264,7 +3278,7 @@ class X509
* @param string $path optional * @param string $path optional
* @return mixed * @return mixed
*/ */
private function getExtensionHelper($id, $cert = null, $path = null) private function getExtensionHelper($id, array $cert = null, $path = null)
{ {
$extensions = $this->extensions($cert, $path); $extensions = $this->extensions($cert, $path);
@ -3288,7 +3302,7 @@ class X509
* @param string $path optional * @param string $path optional
* @return array * @return array
*/ */
private function getExtensionsHelper($cert = null, $path = null) private function getExtensionsHelper(array $cert = null, $path = null)
{ {
$exts = $this->extensions($cert, $path); $exts = $this->extensions($cert, $path);
$extensions = []; $extensions = [];
@ -3358,7 +3372,7 @@ class X509
* @param string $path * @param string $path
* @return mixed * @return mixed
*/ */
public function getExtension($id, $cert = null, $path = null) public function getExtension($id, array $cert = null, $path = null)
{ {
return $this->getExtensionHelper($id, $cert, $path); return $this->getExtensionHelper($id, $cert, $path);
} }
@ -3370,7 +3384,7 @@ class X509
* @param string $path optional * @param string $path optional
* @return array * @return array
*/ */
public function getExtensions($cert = null, $path = null) public function getExtensions(array $cert = null, $path = null)
{ {
return $this->getExtensionsHelper($cert, $path); return $this->getExtensionsHelper($cert, $path);
} }
@ -3446,7 +3460,7 @@ class X509
* @param array $csr optional * @param array $csr optional
* @return mixed * @return mixed
*/ */
public function getAttribute($id, $disposition = self::ATTR_ALL, $csr = null) public function getAttribute($id, $disposition = self::ATTR_ALL, array $csr = null)
{ {
if (empty($csr)) { if (empty($csr)) {
$csr = $this->currentCert; $csr = $this->currentCert;
@ -3485,7 +3499,7 @@ class X509
* @param array $csr optional * @param array $csr optional
* @return array * @return array
*/ */
public function getAttributes($csr = null) public function getAttributes(array $csr = null)
{ {
if (empty($csr)) { if (empty($csr)) {
$csr = $this->currentCert; $csr = $this->currentCert;
@ -3610,7 +3624,7 @@ class X509
case $key instanceof Element: case $key instanceof Element:
// Assume the element is a bitstring-packed key. // Assume the element is a bitstring-packed key.
$decoded = ASN1::decodeBER($key->element); $decoded = ASN1::decodeBER($key->element);
if (empty($decoded)) { if (!$decoded) {
return false; return false;
} }
$raw = ASN1::asn1map($decoded[0], ['type' => ASN1::TYPE_BIT_STRING]); $raw = ASN1::asn1map($decoded[0], ['type' => ASN1::TYPE_BIT_STRING]);
@ -3669,6 +3683,9 @@ class X509
$publicKey = base64_decode(preg_replace('#-.+-|[\r\n]#', '', $this->publicKey->toString($format))); $publicKey = base64_decode(preg_replace('#-.+-|[\r\n]#', '', $this->publicKey->toString($format)));
$decoded = ASN1::decodeBER($publicKey); $decoded = ASN1::decodeBER($publicKey);
if (!$decoded) {
return false;
}
$mapped = ASN1::asn1map($decoded[0], Maps\SubjectPublicKeyInfo::MAP); $mapped = ASN1::asn1map($decoded[0], Maps\SubjectPublicKeyInfo::MAP);
if (!is_array($mapped)) { if (!is_array($mapped)) {
return false; return false;
@ -3740,7 +3757,7 @@ class X509
* @param bool $create optional * @param bool $create optional
* @return int|false * @return int|false
*/ */
private function revokedCertificate(&$rclist, $serial, $create = false) private function revokedCertificate(array &$rclist, $serial, $create = false)
{ {
$serial = new BigInteger($serial); $serial = new BigInteger($serial);
@ -3829,7 +3846,7 @@ class X509
* @param array $crl optional * @param array $crl optional
* @return array|bool * @return array|bool
*/ */
public function listRevoked($crl = null) public function listRevoked(array $crl = null)
{ {
if (!isset($crl)) { if (!isset($crl)) {
$crl = $this->currentCert; $crl = $this->currentCert;
@ -3878,7 +3895,7 @@ class X509
* @param array $crl optional * @param array $crl optional
* @return mixed * @return mixed
*/ */
public function getRevokedCertificateExtension($serial, $id, $crl = null) public function getRevokedCertificateExtension($serial, $id, array $crl = null)
{ {
if (!isset($crl)) { if (!isset($crl)) {
$crl = $this->currentCert; $crl = $this->currentCert;
@ -3900,7 +3917,7 @@ class X509
* @param array $crl optional * @param array $crl optional
* @return array|bool * @return array|bool
*/ */
public function getRevokedCertificateExtensions($serial, $crl = null) public function getRevokedCertificateExtensions($serial, array $crl = null)
{ {
if (!isset($crl)) { if (!isset($crl)) {
$crl = $this->currentCert; $crl = $this->currentCert;

View File

@ -1150,7 +1150,7 @@ class SFTP extends SSH2
* @param array $b * @param array $b
* @return int * @return int
*/ */
private function comparator($a, $b) private function comparator(array $a, array $b)
{ {
switch (true) { switch (true) {
case $a['filename'] === '.' || $b['filename'] === '.': case $a['filename'] === '.' || $b['filename'] === '.':

View File

@ -734,7 +734,7 @@ class Stream
* @param array $arguments * @param array $arguments
* @return mixed * @return mixed
*/ */
public function __call($name, $arguments) public function __call($name, array $arguments)
{ {
if (defined('NET_SFTP_STREAM_LOGGING')) { if (defined('NET_SFTP_STREAM_LOGGING')) {
echo $name . '('; echo $name . '(';

View File

@ -2402,7 +2402,7 @@ class SSH2
* @return bool * @return bool
* @throws \RuntimeException on connection error * @throws \RuntimeException on connection error
*/ */
private function keyboard_interactive_process(...$responses) private function keyboard_interactive_process(array ...$responses)
{ {
if (strlen($this->last_interactive_response)) { if (strlen($this->last_interactive_response)) {
$response = $this->last_interactive_response; $response = $this->last_interactive_response;
@ -4376,7 +4376,7 @@ class SSH2
* @param array $message_number_log * @param array $message_number_log
* @return string * @return string
*/ */
protected function format_log($message_log, $message_number_log) protected function format_log(array $message_log, array $message_number_log)
{ {
$output = ''; $output = '';
for ($i = 0; $i < count($message_log); $i++) { for ($i = 0; $i < count($message_log); $i++) {
@ -4427,7 +4427,7 @@ class SSH2
* @param array $array2 * @param array $array2
* @return mixed False if intersection is empty, else intersected value. * @return mixed False if intersection is empty, else intersected value.
*/ */
private static function array_intersect_first($array1, $array2) private static function array_intersect_first(array $array1, array $array2)
{ {
foreach ($array1 as $value) { foreach ($array1 as $value) {
if (in_array($value, $array2)) { if (in_array($value, $array2)) {

View File

@ -36,6 +36,7 @@ use phpseclib3\Common\Functions\Strings;
use phpseclib3\Crypt\PublicKeyLoader; use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Crypt\RSA; use phpseclib3\Crypt\RSA;
use phpseclib3\Exception\BadConfigurationException; use phpseclib3\Exception\BadConfigurationException;
use phpseclib3\Net\SSH2;
use phpseclib3\System\SSH\Agent\Identity; use phpseclib3\System\SSH\Agent\Identity;
/** /**
@ -215,7 +216,7 @@ class Agent
* @param \phpseclib3\Net\SSH2 $ssh * @param \phpseclib3\Net\SSH2 $ssh
* @return bool * @return bool
*/ */
private function request_forwarding($ssh) private function request_forwarding(SSH2 $ssh)
{ {
if (!$ssh->requestAgentForwarding()) { if (!$ssh->requestAgentForwarding()) {
return false; return false;
@ -235,7 +236,7 @@ class Agent
* *
* @param \phpseclib3\Net\SSH2 $ssh * @param \phpseclib3\Net\SSH2 $ssh
*/ */
public function registerChannelOpen($ssh) public function registerChannelOpen(SSH2 $ssh)
{ {
if ($this->forward_status == self::FORWARD_REQUEST) { if ($this->forward_status == self::FORWARD_REQUEST) {
$this->request_forwarding($ssh); $this->request_forwarding($ssh);

View File

@ -108,7 +108,7 @@ class Identity implements PrivateKey
* *
* @param \phpseclib3\Crypt\Common\PublicKey $key * @param \phpseclib3\Crypt\Common\PublicKey $key
*/ */
public function withPublicKey($key) public function withPublicKey(PublicKey $key)
{ {
if ($key instanceof EC) { if ($key instanceof EC) {
if (is_array($key->getCurve()) || !isset(self::$curveAliases[$key->getCurve()])) { if (is_array($key->getCurve()) || !isset(self::$curveAliases[$key->getCurve()])) {

View File

@ -407,47 +407,47 @@ class ASN1Test extends PhpseclibTestCase
{ {
$em = pack('H*', '3080305c0609608648016503040201054f8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888804207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9'); $em = pack('H*', '3080305c0609608648016503040201054f8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888804207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9');
$decoded = ASN1::decodeBER($em); $decoded = ASN1::decodeBER($em);
$this->assertFalse($decoded[0]); $this->assertNull($decoded);
$em = pack('H*', '3080307f0609608648016503040201057288888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888804207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca90000'); $em = pack('H*', '3080307f0609608648016503040201057288888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888804207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca90000');
$decoded = ASN1::decodeBER($em); $decoded = ASN1::decodeBER($em);
$this->assertFalse($decoded[0]); $this->assertNull($decoded);
} }
public function testOIDGarbage() public function testOIDGarbage()
{ {
$em = pack('H*', '3080305c065860864801650304020188888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888050004207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9'); $em = pack('H*', '3080305c065860864801650304020188888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888050004207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9');
$decoded = ASN1::decodeBER($em); $decoded = ASN1::decodeBER($em);
$this->assertFalse($decoded[0]); $this->assertNull($decoded);
$em = pack('H*', '3080307f067d608648016503040201888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888804207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9'); $em = pack('H*', '3080307f067d608648016503040201888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888804207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9');
$decoded = ASN1::decodeBER($em); $decoded = ASN1::decodeBER($em);
$this->assertFalse($decoded[0]); $this->assertNull($decoded);
} }
public function testConstructedMismatch() public function testConstructedMismatch()
{ {
$em = pack('H*', '1031300d0609608648016503040201050004207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9'); $em = pack('H*', '1031300d0609608648016503040201050004207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9');
$decoded = ASN1::decodeBER($em); $decoded = ASN1::decodeBER($em);
$this->assertFalse($decoded[0]); $this->assertNull($decoded);
$em = pack('H*', '3031100d0609608648016503040201050004207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9'); $em = pack('H*', '3031100d0609608648016503040201050004207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9');
$decoded = ASN1::decodeBER($em); $decoded = ASN1::decodeBER($em);
$this->assertFalse($decoded[0]); $this->assertNull($decoded);
$em = pack('H*', '3031300d2609608648016503040201050004207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9'); $em = pack('H*', '3031300d2609608648016503040201050004207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9');
$decoded = ASN1::decodeBER($em); $decoded = ASN1::decodeBER($em);
$this->assertFalse($decoded[0]); $this->assertNull($decoded);
$em = pack('H*', '3031300d06096086480165030402012d0004207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9'); $em = pack('H*', '3031300d06096086480165030402012d0004207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9');
$decoded = ASN1::decodeBER($em); $decoded = ASN1::decodeBER($em);
$this->assertFalse($decoded[0]); $this->assertNull($decoded);
} }
public function testBadTagSecondOctet() public function testBadTagSecondOctet()
{ {
$em = pack('H*', '3033300f1f808080060960864801650304020104207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9'); $em = pack('H*', '3033300f1f808080060960864801650304020104207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9');
$decoded = ASN1::decodeBER($em); $decoded = ASN1::decodeBER($em);
$this->assertFalse($decoded[0]); $this->assertNull($decoded);
} }
} }

View File

@ -382,9 +382,6 @@ abstract class TestCase extends PhpseclibTestCase
); );
} }
/**
* @requires PHP 5.6
*/
public function testDebugInfo() public function testDebugInfo()
{ {
$num = $this->getInstance(50); $num = $this->getInstance(50);