From 27f578797dce5c366ec585a90d25e733b31e5cdf Mon Sep 17 00:00:00 2001 From: terrafrost Date: Mon, 14 Feb 2022 23:20:29 -0600 Subject: [PATCH] XML Key loading tweaks --- phpseclib/Crypt/DSA/Formats/Keys/XML.php | 6 ++++++ phpseclib/Crypt/EC/Formats/Keys/XML.php | 5 +++++ phpseclib/Crypt/RSA/Formats/Keys/XML.php | 6 ++++++ 3 files changed, 17 insertions(+) diff --git a/phpseclib/Crypt/DSA/Formats/Keys/XML.php b/phpseclib/Crypt/DSA/Formats/Keys/XML.php index bdb3b159..a50fdced 100644 --- a/phpseclib/Crypt/DSA/Formats/Keys/XML.php +++ b/phpseclib/Crypt/DSA/Formats/Keys/XML.php @@ -23,6 +23,7 @@ namespace phpseclib3\Crypt\DSA\Formats\Keys; use ParagonIE\ConstantTime\Base64; use phpseclib3\Common\Functions\Strings; +use phpseclib3\Exception\BadConfigurationException; use phpseclib3\Math\BigInteger; /** @@ -48,6 +49,10 @@ abstract class XML throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key)); } + if (!class_exists('DOMDocument')) { + throw new BadConfigurationException('The dom extension is not setup correctly on this system'); + } + $use_errors = libxml_use_internal_errors(true); $dom = new \DOMDocument(); @@ -55,6 +60,7 @@ abstract class XML $key = '' . $key . ''; } if (!$dom->loadXML($key)) { + libxml_use_internal_errors($use_errors); throw new \UnexpectedValueException('Key does not appear to contain XML'); } $xpath = new \DOMXPath($dom); diff --git a/phpseclib/Crypt/EC/Formats/Keys/XML.php b/phpseclib/Crypt/EC/Formats/Keys/XML.php index 9d399d72..7d7528fd 100644 --- a/phpseclib/Crypt/EC/Formats/Keys/XML.php +++ b/phpseclib/Crypt/EC/Formats/Keys/XML.php @@ -26,6 +26,7 @@ use phpseclib3\Crypt\EC\BaseCurves\Base as BaseCurve; use phpseclib3\Crypt\EC\BaseCurves\Montgomery as MontgomeryCurve; use phpseclib3\Crypt\EC\BaseCurves\Prime as PrimeCurve; use phpseclib3\Crypt\EC\BaseCurves\TwistedEdwards as TwistedEdwardsCurve; +use phpseclib3\Exception\BadConfigurationException; use phpseclib3\Exception\UnsupportedCurveException; use phpseclib3\Math\BigInteger; @@ -70,6 +71,10 @@ abstract class XML throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key)); } + if (!class_exists('DOMDocument')) { + throw new BadConfigurationException('The dom extension is not setup correctly on this system'); + } + $use_errors = libxml_use_internal_errors(true); $temp = self::isolateNamespace($key, 'http://www.w3.org/2009/xmldsig11#'); diff --git a/phpseclib/Crypt/RSA/Formats/Keys/XML.php b/phpseclib/Crypt/RSA/Formats/Keys/XML.php index f5b4bfde..54ca27df 100644 --- a/phpseclib/Crypt/RSA/Formats/Keys/XML.php +++ b/phpseclib/Crypt/RSA/Formats/Keys/XML.php @@ -24,6 +24,7 @@ namespace phpseclib3\Crypt\RSA\Formats\Keys; use ParagonIE\ConstantTime\Base64; use phpseclib3\Common\Functions\Strings; +use phpseclib3\Exception\BadConfigurationException; use phpseclib3\Exception\UnsupportedFormatException; use phpseclib3\Math\BigInteger; @@ -50,6 +51,10 @@ abstract class XML throw new \UnexpectedValueException('Key should be a string - not a ' . gettype($key)); } + if (!class_exists('DOMDocument')) { + throw new BadConfigurationException('The dom extension is not setup correctly on this system'); + } + $components = [ 'isPublicKey' => false, 'primes' => [], @@ -64,6 +69,7 @@ abstract class XML $key = '' . $key . ''; } if (!$dom->loadXML($key)) { + libxml_use_internal_errors($use_errors); throw new \UnexpectedValueException('Key does not appear to contain XML'); } $xpath = new \DOMXPath($dom);