Support DateTimeImmutable

This commit is contained in:
Bastien Miclo 2020-12-23 14:23:15 +01:00 committed by terrafrost
parent 224abbc1f9
commit ba89cdbf0f

View File

@ -26,22 +26,23 @@
namespace phpseclib3\File;
use DateTimeImmutable;
use DateTimeInterface;
use DateTimeZone;
use ParagonIE\ConstantTime\Base64;
use ParagonIE\ConstantTime\Hex;
use phpseclib3\Crypt\Common\PrivateKey;
use phpseclib3\Crypt\Common\PublicKey;
use phpseclib3\Crypt\DSA;
use phpseclib3\Crypt\EC;
use phpseclib3\Crypt\Hash;
use phpseclib3\Crypt\Random;
use phpseclib3\Crypt\RSA;
use phpseclib3\Crypt\DSA;
use phpseclib3\Crypt\EC;
use phpseclib3\Crypt\Common\PublicKey;
use phpseclib3\Crypt\Common\PrivateKey;
use phpseclib3\Crypt\RSA\Formats\Keys\PSS;
use phpseclib3\Exception\UnsupportedAlgorithmException;
use phpseclib3\File\ASN1\Element;
use phpseclib3\Math\BigInteger;
use phpseclib3\File\ASN1\Maps;
use phpseclib3\Crypt\RSA\Formats\Keys\PSS;
use DateTime;
use DateTimeZone;
use phpseclib3\Math\BigInteger;
/**
* Pure-PHP X.509 Parser
@ -1046,7 +1047,7 @@ class X509
*
* If $date isn't defined it is assumed to be the current date.
*
* @param \DateTime|string $date optional
* @param DateTimeInterface|string $date optional
* @access public
* @return boolean
*/
@ -1057,7 +1058,7 @@ class X509
}
if (!isset($date)) {
$date = new DateTime(null, new DateTimeZone(@date_default_timezone_get()));
$date = new DateTimeImmutable(null, new DateTimeZone(@date_default_timezone_get()));
}
$notBefore = $this->currentCert['tbsCertificate']['validity']['notBefore'];
@ -1067,19 +1068,13 @@ class X509
$notAfter = isset($notAfter['generalTime']) ? $notAfter['generalTime'] : $notAfter['utcTime'];
if (is_string($date)) {
$date = new DateTime($date, new DateTimeZone(@date_default_timezone_get()));
$date = new DateTimeImmutable($date, new DateTimeZone(@date_default_timezone_get()));
}
$notBefore = new DateTime($notBefore, new DateTimeZone(@date_default_timezone_get()));
$notAfter = new DateTime($notAfter, new DateTimeZone(@date_default_timezone_get()));
$notBefore = new DateTimeImmutable($notBefore, new DateTimeZone(@date_default_timezone_get()));
$notAfter = new DateTimeImmutable($notAfter, new DateTimeZone(@date_default_timezone_get()));
switch (true) {
case $date < $notBefore:
case $date > $notAfter:
return false;
}
return true;
return $date >= $notBefore && $date<= $notAfter;
}
/**
@ -2495,7 +2490,7 @@ class X509
if ($date instanceof Element) {
return $date;
}
$dateObj = new DateTime($date, new DateTimeZone('GMT'));
$dateObj = new DateTimeImmutable($date, new DateTimeZone('GMT'));
$year = $dateObj->format('Y'); // the same way ASN1.php parses this
if ($year < 2050) {
return ['utcTime' => $date];
@ -2571,10 +2566,10 @@ class X509
return false;
}
$startDate = new DateTime('now', new DateTimeZone(@date_default_timezone_get()));
$startDate = new DateTimeImmutable('now', new DateTimeZone(@date_default_timezone_get()));
$startDate = !empty($this->startDate) ? $this->startDate : $startDate->format('D, d M Y H:i:s O');
$endDate = new DateTime('+1 year', new DateTimeZone(@date_default_timezone_get()));
$endDate = new DateTimeImmutable('+1 year', new DateTimeZone(@date_default_timezone_get()));
$endDate = !empty($this->endDate) ? $this->endDate : $endDate->format('D, d M Y H:i:s O');
/* "The serial number MUST be a positive integer"
@ -2839,7 +2834,7 @@ class X509
$signatureSubject = isset($this->signatureSubject) ? $this->signatureSubject : null;
$signatureAlgorithm = self::identifySignatureAlgorithm($issuer->privateKey);
$thisUpdate = new DateTime('now', new DateTimeZone(@date_default_timezone_get()));
$thisUpdate = new DateTimeImmutable('now', new DateTimeZone(@date_default_timezone_get()));
$thisUpdate = !empty($this->startDate) ? $this->startDate : $thisUpdate->format('D, d M Y H:i:s O');
if (isset($crl->currentCert) && is_array($crl->currentCert) && isset($crl->currentCert['tbsCertList'])) {
@ -3013,13 +3008,13 @@ class X509
/**
* Set certificate start date
*
* @param string $date
* @param DateTimeInterface|string $date
* @access public
*/
public function setStartDate($date)
{
if (!is_object($date) || !is_a($date, 'DateTime')) {
$date = new DateTime($date, new DateTimeZone(@date_default_timezone_get()));
if (!is_object($date) || !($date instanceof DateTimeInterface)) {
$date = new DateTimeImmutable($date, new DateTimeZone(@date_default_timezone_get()));
}
$this->startDate = $date->format('D, d M Y H:i:s O');
@ -3028,7 +3023,7 @@ class X509
/**
* Set certificate end date
*
* @param string $date
* @param DateTimeInterface|string $date
* @access public
*/
public function setEndDate($date)
@ -3045,8 +3040,8 @@ class X509
$temp = chr(ASN1::TYPE_GENERALIZED_TIME) . ASN1::encodeLength(strlen($temp)) . $temp;
$this->endDate = new Element($temp);
} else {
if (!is_object($date) || !is_a($date, 'DateTime')) {
$date = new DateTime($date, new DateTimeZone(@date_default_timezone_get()));
if (!is_object($date) || !($date instanceof DateTimeInterface)) {
$date = new DateTimeImmutable($date, new DateTimeZone(@date_default_timezone_get()));
}
$this->endDate = $date->format('D, d M Y H:i:s O');
@ -3785,7 +3780,7 @@ class X509
}
$i = count($rclist);
$revocationDate = new DateTime('now', new DateTimeZone(@date_default_timezone_get()));
$revocationDate = new DateTimeImmutable('now', new DateTimeZone(@date_default_timezone_get()));
$rclist[] = ['userCertificate' => $serial,
'revocationDate' => $this->timeField($revocationDate->format('D, d M Y H:i:s O'))];
return $i;