1
0
mirror of https://github.com/danog/phpseclib.git synced 2025-01-05 20:28:27 +01:00

Merge branch '3.0'

This commit is contained in:
terrafrost 2021-04-20 16:12:40 -05:00
commit 1642985976
2 changed files with 43 additions and 10 deletions

View File

@ -670,7 +670,7 @@ class X509
*/ */
private function mapOutExtensions(&$root, $path) private function mapOutExtensions(&$root, $path)
{ {
$extensions = &$this->subArray($root, $path); $extensions = &$this->subArray($root, $path, !empty($this->extensionValues));
foreach ($this->extensionValues as $id => $data) { foreach ($this->extensionValues as $id => $data) {
extract($data); extract($data);
@ -679,16 +679,15 @@ class X509
'extnValue' => $value, 'extnValue' => $value,
'critical' => $critical 'critical' => $critical
]; ];
if (!$replace) { if ($replace) {
$extensions[] = $newext; foreach ($extensions as $key => $value) {
continue; if ($value['extnId'] == $id) {
} $extensions[$key] = $newext;
foreach ($extensions as $key => $value) { continue 2;
if ($value['extnId'] == $id) { }
$extensions[$key] = $newext; }
break;
}
} }
$extensions[] = $newext;
} }
if (is_array($extensions)) { if (is_array($extensions)) {

View File

@ -5,6 +5,7 @@
* @license http://www.opensource.org/licenses/mit-license.html MIT License * @license http://www.opensource.org/licenses/mit-license.html MIT License
*/ */
use phpseclib3\Crypt\EC;
use phpseclib3\Crypt\RSA; use phpseclib3\Crypt\RSA;
use phpseclib3\File\ASN1; use phpseclib3\File\ASN1;
use phpseclib3\File\X509; use phpseclib3\File\X509;
@ -114,4 +115,37 @@ class Unit_File_X509_X509ExtensionTest extends PhpseclibTestCase
X509::registerExtension('bar', ['type' => ASN1::TYPE_OCTET_STRING]); X509::registerExtension('bar', ['type' => ASN1::TYPE_OCTET_STRING]);
X509::registerExtension('bar', ['type' => ASN1::TYPE_ANY]); X509::registerExtension('bar', ['type' => ASN1::TYPE_ANY]);
} }
public function testExtensionsAreInitializedIfMissing()
{
$issuerKey = EC::createKey('ed25519');
$subjectKey = EC::createKey('ed25519')->getPublicKey();
$subject = new X509();
$subject->setPublicKey($subjectKey);
$subject->setDN(['commonName' => 'subject']);
$issuer = new X509();
$issuer->setPrivateKey($issuerKey);
$issuer->setDN(['commonName' => 'issuer']);
$authority = new X509();
$authority->setExtensionValue('id-ce-keyUsage', ['digitalSignature']);
$cert = $authority->saveX509($authority->sign($issuer, $subject));
$loader = new X509();
$this->assertSame(
[
[
'extnId' => 'id-ce-keyUsage',
'critical' => false,
'extnValue' => ['digitalSignature'],
],
],
$loader->loadX509($cert)['tbsCertificate']['extensions']
);
}
} }