Create tbsCertificate/extensions if missing

Fix #1642
Create tbsCertificate/extensions if missing when extensions values are proceeded
This commit is contained in:
Kyle 2021-04-20 14:28:47 +02:00 committed by terrafrost
parent d9615a6fb9
commit c596078d7a
2 changed files with 35 additions and 1 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, true);
foreach ($this->extensionValues as $id => $data) { foreach ($this->extensionValues as $id => $data) {
extract($data); extract($data);

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']
);
}
} }