From 701881d3de21cb5e96a5d2bac82e0615f3a1bda6 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Sun, 18 Apr 2021 09:57:39 -0500 Subject: [PATCH] X509: add replace parameter to setExtensionValue --- phpseclib/File/X509.php | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/phpseclib/File/X509.php b/phpseclib/File/X509.php index 1e333293..38e0a97b 100644 --- a/phpseclib/File/X509.php +++ b/phpseclib/File/X509.php @@ -670,16 +670,27 @@ class X509 */ private function mapOutExtensions(&$root, $path) { - foreach ($this->extensionValues as $id => $value) { - $root['tbsCertificate']['extensions'][] = [ - 'extnId' => $id, - 'extnValue' => $value[1], - 'critical' => $value[0], - ]; - } - $extensions = &$this->subArray($root, $path); + foreach ($this->extensionValues as $id => $data) { + extract($data); + $newext = [ + 'extnId' => $id, + 'extnValue' => $value, + 'critical' => $critical + ]; + if (!$replace) { + $extensions[] = $newext; + continue; + } + foreach ($extensions as $key => $value) { + if ($value['extnId'] == $id) { + $extensions[$key] = $newext; + break; + } + } + } + if (is_array($extensions)) { $size = count($extensions); for ($i = 0; $i < $size; $i++) { @@ -4081,9 +4092,10 @@ class X509 * @param string $id * @param mixed $value * @param bool $critical + * @param bool $replace */ - public function setExtensionValue($id, $value, $critical = false) + public function setExtensionValue($id, $value, $critical = false, $replace = false) { - $this->extensionValues[$id] = [$critical, $value]; + $this->extensionValues[$id] = compact('critical', 'replace', 'value'); } }