2014-04-16 22:18:45 +02:00
|
|
|
<?php
|
2022-02-17 03:25:59 +01:00
|
|
|
|
2014-04-16 22:18:45 +02:00
|
|
|
/**
|
2014-04-18 06:53:33 +02:00
|
|
|
* @author Jim Wigginton <terrafrost@php.net>
|
2014-12-10 00:02:44 +01:00
|
|
|
* @copyright 2014 Jim Wigginton
|
2014-04-16 22:18:45 +02:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
|
|
|
*/
|
|
|
|
|
2022-06-04 17:31:21 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-02-23 03:48:51 +01:00
|
|
|
namespace phpseclib3\Tests\Unit\File;
|
|
|
|
|
2019-11-07 06:41:40 +01:00
|
|
|
use phpseclib3\File\ASN1;
|
2022-02-23 03:48:51 +01:00
|
|
|
use phpseclib3\Tests\PhpseclibTestCase;
|
2014-04-16 22:18:45 +02:00
|
|
|
|
2022-02-23 03:48:51 +01:00
|
|
|
class ASN1Test extends PhpseclibTestCase
|
2014-04-16 22:18:45 +02:00
|
|
|
{
|
2014-04-18 06:53:33 +02:00
|
|
|
/**
|
2019-11-07 06:41:40 +01:00
|
|
|
* on older versions of \phpseclib3\File\ASN1 this would yield a PHP Warning
|
2015-03-29 18:07:17 +02:00
|
|
|
* @group github275
|
|
|
|
*/
|
2022-06-04 17:31:21 +02:00
|
|
|
public function testAnyString(): void
|
2014-04-16 22:18:45 +02:00
|
|
|
{
|
2017-11-27 09:30:14 +01:00
|
|
|
$KDC_REP = [
|
2014-12-10 01:53:05 +01:00
|
|
|
'type' => ASN1::TYPE_SEQUENCE,
|
2017-11-27 09:30:14 +01:00
|
|
|
'children' => [
|
|
|
|
'pvno' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 0,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2017-11-27 09:30:14 +01:00
|
|
|
'type' => ASN1::TYPE_ANY],
|
|
|
|
'msg-type' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 1,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2017-11-27 09:30:14 +01:00
|
|
|
'type' => ASN1::TYPE_ANY],
|
|
|
|
'padata' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 2,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2017-11-27 09:30:14 +01:00
|
|
|
'type' => ASN1::TYPE_ANY],
|
|
|
|
'crealm' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 3,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2017-11-27 09:30:14 +01:00
|
|
|
'type' => ASN1::TYPE_ANY],
|
|
|
|
'cname' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 4,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2017-11-27 09:30:14 +01:00
|
|
|
'type' => ASN1::TYPE_ANY],
|
|
|
|
'ticket' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 5,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2017-11-27 09:30:14 +01:00
|
|
|
'type' => ASN1::TYPE_ANY],
|
|
|
|
'enc-part' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 6,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2017-11-27 09:30:14 +01:00
|
|
|
'type' => ASN1::TYPE_ANY]
|
|
|
|
]
|
|
|
|
];
|
2014-04-16 22:18:45 +02:00
|
|
|
|
2017-11-27 09:30:14 +01:00
|
|
|
$AS_REP = [
|
2014-12-10 01:53:05 +01:00
|
|
|
'class' => ASN1::CLASS_APPLICATION,
|
2014-04-16 22:18:45 +02:00
|
|
|
'cast' => 11,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true
|
2017-11-27 09:30:14 +01:00
|
|
|
] + $KDC_REP;
|
2014-04-16 22:18:45 +02:00
|
|
|
|
2014-04-18 17:47:05 +02:00
|
|
|
$str = 'a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uNkEDwiqM7i1Ekw3V+' .
|
|
|
|
'+zoEIxqgXjGgPdrWkzU/H6rnXiqMtiZZqUXwWY0zkCmy';
|
|
|
|
|
2016-11-30 16:15:07 +01:00
|
|
|
$decoded = ASN1::decodeBER(base64_decode($str));
|
|
|
|
$result = ASN1::asn1map($decoded[0], $AS_REP);
|
2014-04-16 22:18:45 +02:00
|
|
|
|
2020-12-12 22:11:04 +01:00
|
|
|
$this->assertIsArray($result);
|
2014-04-16 22:18:45 +02:00
|
|
|
}
|
|
|
|
|
2014-04-18 06:53:33 +02:00
|
|
|
/**
|
2019-11-07 06:41:40 +01:00
|
|
|
* on older versions of \phpseclib3\File\ASN1 this would produce a null instead of an array
|
2015-03-29 18:07:17 +02:00
|
|
|
* @group github275
|
|
|
|
*/
|
2022-06-04 17:31:21 +02:00
|
|
|
public function testIncorrectString(): void
|
2014-04-16 22:18:45 +02:00
|
|
|
{
|
2017-11-27 09:30:14 +01:00
|
|
|
$PA_DATA = [
|
2014-12-10 01:53:05 +01:00
|
|
|
'type' => ASN1::TYPE_SEQUENCE,
|
2017-11-27 09:30:14 +01:00
|
|
|
'children' => [
|
|
|
|
'padata-type' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 1,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2014-12-10 01:53:05 +01:00
|
|
|
'type' => ASN1::TYPE_INTEGER
|
2017-11-27 09:30:14 +01:00
|
|
|
],
|
|
|
|
'padata-value' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 2,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2014-12-10 01:53:05 +01:00
|
|
|
'type' => ASN1::TYPE_OCTET_STRING
|
2017-11-27 09:30:14 +01:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
2014-04-16 22:18:45 +02:00
|
|
|
|
2017-11-27 09:30:14 +01:00
|
|
|
$PrincipalName = [
|
2014-12-10 01:53:05 +01:00
|
|
|
'type' => ASN1::TYPE_SEQUENCE,
|
2017-11-27 09:30:14 +01:00
|
|
|
'children' => [
|
|
|
|
'name-type' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 0,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2014-12-10 01:53:05 +01:00
|
|
|
'type' => ASN1::TYPE_INTEGER
|
2017-11-27 09:30:14 +01:00
|
|
|
],
|
|
|
|
'name-string' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 1,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
|
|
|
'min' => 0,
|
|
|
|
'max' => -1,
|
2014-12-10 01:53:05 +01:00
|
|
|
'type' => ASN1::TYPE_SEQUENCE,
|
2019-11-07 06:41:40 +01:00
|
|
|
'children' => ['type' => ASN1::TYPE_IA5_STRING] // should be \phpseclib3\File\ASN1::TYPE_GENERAL_STRING
|
2017-11-27 09:30:14 +01:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
2014-04-16 22:18:45 +02:00
|
|
|
|
2017-11-27 09:30:14 +01:00
|
|
|
$Ticket = [
|
2014-12-10 01:53:05 +01:00
|
|
|
'class' => ASN1::CLASS_APPLICATION,
|
2014-04-16 22:18:45 +02:00
|
|
|
'cast' => 1,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2014-12-10 01:53:05 +01:00
|
|
|
'type' => ASN1::TYPE_SEQUENCE,
|
2017-11-27 09:30:14 +01:00
|
|
|
'children' => [
|
|
|
|
'tkt-vno' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 0,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2014-12-10 01:53:05 +01:00
|
|
|
'type' => ASN1::TYPE_INTEGER
|
2017-11-27 09:30:14 +01:00
|
|
|
],
|
|
|
|
'realm' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 1,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2014-12-10 01:53:05 +01:00
|
|
|
'type' => ASN1::TYPE_ANY
|
2017-11-27 09:30:14 +01:00
|
|
|
],
|
|
|
|
'sname' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 2,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2014-12-10 01:53:05 +01:00
|
|
|
'type' => ASN1::TYPE_ANY
|
2017-11-27 09:30:14 +01:00
|
|
|
],
|
|
|
|
'enc-part' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 3,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2014-12-10 01:53:05 +01:00
|
|
|
'type' => ASN1::TYPE_ANY
|
2017-11-27 09:30:14 +01:00
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
2014-04-16 22:18:45 +02:00
|
|
|
|
2017-11-27 09:30:14 +01:00
|
|
|
$KDC_REP = [
|
2014-12-10 01:53:05 +01:00
|
|
|
'type' => ASN1::TYPE_SEQUENCE,
|
2017-11-27 09:30:14 +01:00
|
|
|
'children' => [
|
|
|
|
'pvno' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 0,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2017-11-27 09:30:14 +01:00
|
|
|
'type' => ASN1::TYPE_INTEGER],
|
|
|
|
'msg-type' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 1,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2017-11-27 09:30:14 +01:00
|
|
|
'type' => ASN1::TYPE_INTEGER],
|
|
|
|
'padata' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 2,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
|
|
|
'min' => 0,
|
|
|
|
'max' => -1,
|
2014-12-10 01:53:05 +01:00
|
|
|
'type' => ASN1::TYPE_SEQUENCE,
|
2017-11-27 09:30:14 +01:00
|
|
|
'children' => $PA_DATA],
|
|
|
|
'crealm' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 3,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2017-11-27 09:30:14 +01:00
|
|
|
'type' => ASN1::TYPE_OCTET_STRING],
|
|
|
|
'cname' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 4,
|
|
|
|
'optional' => true,
|
2017-11-27 09:30:14 +01:00
|
|
|
'explicit' => true] + $PrincipalName,
|
2014-12-10 01:53:05 +01:00
|
|
|
//'type' => ASN1::TYPE_ANY),
|
2017-11-27 09:30:14 +01:00
|
|
|
'ticket' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 5,
|
|
|
|
'optional' => true,
|
|
|
|
'implicit' => true,
|
|
|
|
'min' => 0,
|
|
|
|
'max' => 1,
|
2014-12-10 01:53:05 +01:00
|
|
|
'type' => ASN1::TYPE_SEQUENCE,
|
2017-11-27 09:30:14 +01:00
|
|
|
'children' => $Ticket],
|
|
|
|
'enc-part' => [
|
2014-04-16 22:18:45 +02:00
|
|
|
'constant' => 6,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
2017-11-27 09:30:14 +01:00
|
|
|
'type' => ASN1::TYPE_ANY]
|
|
|
|
]
|
|
|
|
];
|
2014-04-16 22:18:45 +02:00
|
|
|
|
2017-11-27 09:30:14 +01:00
|
|
|
$AS_REP = [
|
2014-12-10 01:53:05 +01:00
|
|
|
'class' => ASN1::CLASS_APPLICATION,
|
2014-04-16 22:18:45 +02:00
|
|
|
'cast' => 11,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true
|
2017-11-27 09:30:14 +01:00
|
|
|
] + $KDC_REP;
|
2014-04-16 22:18:45 +02:00
|
|
|
|
2014-04-18 17:47:05 +02:00
|
|
|
$str = 'a4IC3jCCAtqgAwIBBaEDAgELoi8wLTAroQMCAROiJAQiMCAwHqADAgEXoRcbFUNSRUFUVUlUWS5ORVR0ZXN0dXNlcqMPGw' .
|
2014-04-16 22:18:45 +02:00
|
|
|
'1DU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eBER(base64_decode($str));
|
|
|
|
$result = ASN1::asn1map($decoded[0], $AS_REP);
|
2014-04-16 22:18:45 +02:00
|
|
|
|
2020-12-12 22:11:04 +01:00
|
|
|
$this->assertIsArray($result);
|
2014-04-16 22:18:45 +02:00
|
|
|
}
|
2014-09-03 07:29:04 +02:00
|
|
|
|
|
|
|
/**
|
2015-04-16 07:42:53 +02:00
|
|
|
* older versions of ASN1 didn't handle indefinite length tags very well
|
2015-03-29 18:07:17 +02:00
|
|
|
*/
|
2022-06-04 17:31:21 +02:00
|
|
|
public function testIndefiniteLength(): void
|
2014-09-03 07:29:04 +02:00
|
|
|
{
|
2016-11-30 16:15:07 +01:00
|
|
|
$decoded = ASN1::decodeBER(file_get_contents(dirname(__FILE__) . '/ASN1/FE.pdf.p7m'));
|
2014-09-04 22:33:41 +02:00
|
|
|
$this->assertCount(5, $decoded[0]['content'][1]['content'][0]['content']); // older versions would have returned 3
|
2014-09-03 07:29:04 +02:00
|
|
|
}
|
|
|
|
|
2022-06-04 17:31:21 +02:00
|
|
|
public function testDefiniteLength(): void
|
2014-09-03 07:29:04 +02:00
|
|
|
{
|
|
|
|
// the following base64-encoded string is the X.509 cert from <http://phpseclib.sourceforge.net/x509/decoder.php>
|
|
|
|
$str = 'MIIDITCCAoqgAwIBAgIQT52W2WawmStUwpV8tBV9TTANBgkqhkiG9w0BAQUFADBM' .
|
|
|
|
'MQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhhd3RlIENvbnN1bHRpbmcgKFB0eSkg' .
|
|
|
|
'THRkLjEWMBQGA1UEAxMNVGhhd3RlIFNHQyBDQTAeFw0xMTEwMjYwMDAwMDBaFw0x' .
|
|
|
|
'MzA5MzAyMzU5NTlaMGgxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlh' .
|
|
|
|
'MRYwFAYDVQQHFA1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKFApHb29nbGUgSW5jMRcw' .
|
|
|
|
'FQYDVQQDFA53d3cuZ29vZ2xlLmNvbTCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkC' .
|
|
|
|
'gYEA3rcmQ6aZhc04pxUJuc8PycNVjIjujI0oJyRLKl6g2Bb6YRhLz21ggNM1QDJy' .
|
|
|
|
'wI8S2OVOj7my9tkVXlqGMaO6hqpryNlxjMzNJxMenUJdOPanrO/6YvMYgdQkRn8B' .
|
|
|
|
'd3zGKokUmbuYOR2oGfs5AER9G5RqeC1prcB6LPrQ2iASmNMCAwEAAaOB5zCB5DAM' .
|
|
|
|
'BgNVHRMBAf8EAjAAMDYGA1UdHwQvMC0wK6ApoCeGJWh0dHA6Ly9jcmwudGhhd3Rl' .
|
|
|
|
'LmNvbS9UaGF3dGVTR0NDQS5jcmwwKAYDVR0lBCEwHwYIKwYBBQUHAwEGCCsGAQUF' .
|
|
|
|
'BwMCBglghkgBhvhCBAEwcgYIKwYBBQUHAQEEZjBkMCIGCCsGAQUFBzABhhZodHRw' .
|
|
|
|
'Oi8vb2NzcC50aGF3dGUuY29tMD4GCCsGAQUFBzAChjJodHRwOi8vd3d3LnRoYXd0' .
|
|
|
|
'ZS5jb20vcmVwb3NpdG9yeS9UaGF3dGVfU0dDX0NBLmNydDANBgkqhkiG9w0BAQUF' .
|
|
|
|
'AAOBgQAhrNWuyjSJWsKrUtKyNGadeqvu5nzVfsJcKLt0AMkQH0IT/GmKHiSgAgDp' .
|
|
|
|
'ulvKGQSy068Bsn5fFNum21K5mvMSf3yinDtvmX3qUA12IxL/92ZzKbeVCq3Yi7Le' .
|
|
|
|
'IOkKcGQRCMha8X2e7GmlpdWC1ycenlbN0nbVeSv3JUMcafC4+Q==';
|
2016-11-30 16:15:07 +01:00
|
|
|
$decoded = ASN1::decodeBER(base64_decode($str));
|
2014-09-04 22:33:41 +02:00
|
|
|
$this->assertCount(3, $decoded[0]['content']);
|
2014-09-03 07:29:04 +02:00
|
|
|
}
|
2014-09-23 06:03:06 +02:00
|
|
|
|
|
|
|
/**
|
2015-03-29 18:07:17 +02:00
|
|
|
* @group github477
|
|
|
|
*/
|
2022-06-04 17:31:21 +02:00
|
|
|
public function testContextSpecificNonConstructed(): void
|
2014-09-23 06:03:06 +02:00
|
|
|
{
|
2016-11-30 16:15:07 +01:00
|
|
|
$decoded = ASN1::decodeBER(base64_decode('MBaAFJtUo7c00HsI5EPZ4bkICfkOY2Pv'));
|
2020-12-12 22:11:04 +01:00
|
|
|
$this->assertIsString($decoded[0]['content'][0]['content']);
|
2014-09-23 06:03:06 +02:00
|
|
|
}
|
2015-01-11 06:58:50 +01:00
|
|
|
|
|
|
|
/**
|
2015-03-29 18:07:17 +02:00
|
|
|
* @group github602
|
|
|
|
*/
|
2022-06-04 17:31:21 +02:00
|
|
|
public function testEmptyContextTag(): void
|
2015-01-11 06:58:50 +01:00
|
|
|
{
|
2016-11-30 16:15:07 +01:00
|
|
|
$decoded = ASN1::decodeBER("\xa0\x00");
|
2020-12-12 22:11:04 +01:00
|
|
|
$this->assertIsArray($decoded);
|
2015-01-11 06:58:50 +01:00
|
|
|
$this->assertCount(0, $decoded[0]['content']);
|
|
|
|
}
|
2016-08-28 17:38:01 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group github1027
|
|
|
|
*/
|
2022-06-04 17:31:21 +02:00
|
|
|
public function testInfiniteLoop(): void
|
2016-08-28 17:38:01 +02:00
|
|
|
{
|
|
|
|
$data = base64_decode('MD6gJQYKKwYBBAGCNxQCA6AXDBVvZmZpY2VAY2VydGRpZ2l0YWwucm+BFW9mZmljZUBjZXJ0ZGlnaXRhbC5ybw==');
|
2022-01-31 03:10:21 +01:00
|
|
|
self::assertSame(
|
|
|
|
'a:1:{i:0;a:5:{s:5:"start";i:0;s:12:"headerlength";i:2;s:4:"type";i:16;s:7:"content";a:2:{i:0;a:6:{s:4:"type";i:2;s:8:"constant";i:0;s:7:"content";a:2:{i:0;a:5:{s:5:"start";i:4;s:12:"headerlength";i:2;s:4:"type";i:6;s:7:"content";s:22:"1.3.6.1.4.1.311.20.2.3";s:6:"length";i:12;}i:1;a:6:{s:4:"type";i:2;s:8:"constant";i:0;s:7:"content";a:1:{i:0;a:5:{s:5:"start";i:18;s:12:"headerlength";i:2;s:4:"type";i:12;s:7:"content";s:21:"office@certdigital.ro";s:6:"length";i:23;}}s:6:"length";i:25;s:5:"start";i:16;s:12:"headerlength";i:2;}}s:6:"length";i:39;s:5:"start";i:2;s:12:"headerlength";i:2;}i:1;a:6:{s:4:"type";i:2;s:8:"constant";i:1;s:7:"content";s:21:"office@certdigital.ro";s:6:"length";i:23;s:5:"start";i:41;s:12:"headerlength";i:2;}}s:6:"length";i:64;}}',
|
|
|
|
serialize(ASN1::decodeBER($data))
|
|
|
|
);
|
2016-08-28 17:38:01 +02:00
|
|
|
}
|
2016-11-23 06:55:33 +01:00
|
|
|
|
2022-06-04 17:31:21 +02:00
|
|
|
public function testMaps(): void
|
2016-11-23 06:55:33 +01:00
|
|
|
{
|
2022-06-04 17:31:21 +02:00
|
|
|
$files = scandir(__DIR__ . '/../../../phpseclib/File/ASN1/Maps');
|
2022-01-31 03:10:21 +01:00
|
|
|
self::assertNotEmpty($files);
|
2016-11-23 06:55:33 +01:00
|
|
|
foreach ($files as $file) {
|
|
|
|
if ($file == '.' || $file == '..') {
|
|
|
|
continue;
|
|
|
|
}
|
2022-01-31 03:10:21 +01:00
|
|
|
self::assertTrue(defined('phpseclib3\\File\\ASN1\\Maps\\' . basename($file, '.php') . '::MAP'));
|
2016-11-23 06:55:33 +01:00
|
|
|
}
|
|
|
|
}
|
2018-04-15 15:30:02 +02:00
|
|
|
|
2022-06-04 17:31:21 +02:00
|
|
|
public function testApplicationTag(): void
|
2018-04-15 15:30:02 +02:00
|
|
|
{
|
2022-01-30 16:34:42 +01:00
|
|
|
$map = [
|
2018-04-15 17:27:46 +02:00
|
|
|
'type' => ASN1::TYPE_SEQUENCE,
|
2022-01-30 16:34:42 +01:00
|
|
|
'children' => [
|
2018-04-15 15:30:02 +02:00
|
|
|
// technically, default implies optional, but we'll define it as being optional, none-the-less, just to
|
|
|
|
// reenforce that fact
|
2022-01-30 16:34:42 +01:00
|
|
|
'version' => [
|
2018-04-15 17:27:46 +02:00
|
|
|
// if class isn't present it's assumed to be ASN1::CLASS_UNIVERSAL or
|
|
|
|
// (if constant is present) ASN1::CLASS_CONTEXT_SPECIFIC
|
|
|
|
'class' => ASN1::CLASS_APPLICATION,
|
2018-04-15 15:30:02 +02:00
|
|
|
'cast' => 2,
|
|
|
|
'optional' => true,
|
|
|
|
'explicit' => true,
|
|
|
|
'default' => 'v1',
|
2018-04-15 17:27:46 +02:00
|
|
|
'type' => ASN1::TYPE_INTEGER,
|
2022-01-30 16:34:42 +01:00
|
|
|
'mapping' => ['v1', 'v2', 'v3']
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
2018-04-15 15:30:02 +02:00
|
|
|
|
2022-01-30 16:34:42 +01:00
|
|
|
$data = ['version' => 'v3'];
|
2018-04-15 15:30:02 +02:00
|
|
|
|
2018-04-15 17:31:12 +02:00
|
|
|
$str = ASN1::encodeDER($data, $map);
|
2018-04-15 15:30:02 +02:00
|
|
|
|
2018-04-15 17:31:12 +02:00
|
|
|
$decoded = ASN1::decodeBER($str);
|
|
|
|
$arr = ASN1::asn1map($decoded[0], $map);
|
2018-04-15 15:30:02 +02:00
|
|
|
|
|
|
|
$this->assertSame($data, $arr);
|
|
|
|
}
|
2018-09-11 11:16:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group github1296
|
|
|
|
*/
|
2022-06-04 17:31:21 +02:00
|
|
|
public function testInvalidCertificate(): void
|
2018-09-11 11:16:19 +02:00
|
|
|
{
|
|
|
|
$data = 'a' . base64_decode('MD6gJQYKKwYBBAGCNxQCA6AXDBVvZmZpY2VAY2VydGRpZ2l0YWwucm+BFW9mZmljZUBjZXJ0ZGlnaXRhbC5ybw==');
|
2022-01-31 03:10:21 +01:00
|
|
|
self::assertSame(
|
|
|
|
'a:1:{i:0;a:6:{s:4:"type";i:1;s:8:"constant";i:1;s:7:"content";a:0:{}s:6:"length";i:2;s:5:"start";i:0;s:12:"headerlength";i:2;}}',
|
|
|
|
serialize(ASN1::decodeBER($data))
|
|
|
|
);
|
2018-09-11 11:16:19 +02:00
|
|
|
}
|
2019-05-01 05:37:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group github1367
|
|
|
|
*/
|
2022-06-04 17:31:21 +02:00
|
|
|
public function testOIDs(): void
|
2019-05-01 05:37:19 +02:00
|
|
|
{
|
|
|
|
// from the example in 8.19.5 in the following:
|
|
|
|
// https://www.itu.int/ITU-T/studygroups/com17/languages/X.690-0207.pdf#page=22
|
|
|
|
$orig = pack('H*', '813403');
|
2019-05-02 14:28:27 +02:00
|
|
|
$new = ASN1::decodeOID($orig);
|
2019-05-01 05:37:19 +02:00
|
|
|
$this->assertSame('2.100.3', $new);
|
2019-05-02 14:28:27 +02:00
|
|
|
$this->assertSame($orig, ASN1::encodeOID($new));
|
2019-05-01 05:37:19 +02:00
|
|
|
|
|
|
|
// UUID OID from the following:
|
|
|
|
// https://healthcaresecprivacy.blogspot.com/2011/02/creating-and-using-unique-id-uuid-oid.html
|
|
|
|
$orig = '2.25.329800735698586629295641978511506172918';
|
2019-05-02 14:28:27 +02:00
|
|
|
$new = ASN1::encodeOID($orig);
|
2019-05-01 05:37:19 +02:00
|
|
|
$this->assertSame(pack('H*', '6983f09da7ebcfdee0c7a1a7b2c0948cc8f9d776'), $new);
|
2019-05-02 14:28:27 +02:00
|
|
|
$this->assertSame($orig, ASN1::decodeOID($new));
|
2019-05-01 05:37:19 +02:00
|
|
|
}
|
2019-08-03 04:51:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @group github1388
|
|
|
|
*/
|
2022-06-04 17:31:21 +02:00
|
|
|
public function testExplicitImplicitDate(): void
|
2019-08-03 04:51:06 +02:00
|
|
|
{
|
2022-01-30 16:34:42 +01:00
|
|
|
$map = [
|
2019-08-03 05:55:22 +02:00
|
|
|
'type' => ASN1::TYPE_SEQUENCE,
|
2022-01-30 16:34:42 +01:00
|
|
|
'children' => [
|
|
|
|
'notBefore' => [
|
2019-08-03 04:51:06 +02:00
|
|
|
'constant' => 0,
|
|
|
|
'optional' => true,
|
|
|
|
'implicit' => true,
|
2022-01-30 16:34:42 +01:00
|
|
|
'type' => ASN1::TYPE_GENERALIZED_TIME],
|
|
|
|
'notAfter' => [
|
2019-08-03 04:51:06 +02:00
|
|
|
'constant' => 1,
|
|
|
|
'optional' => true,
|
|
|
|
'implicit' => true,
|
2022-01-30 16:34:42 +01:00
|
|
|
'type' => ASN1::TYPE_GENERALIZED_TIME]
|
|
|
|
]
|
|
|
|
];
|
2019-08-03 04:51:06 +02:00
|
|
|
|
|
|
|
$a = pack('H*', '3026a011180f32303137303432313039303535305aa111180f32303138303432313230353935395a');
|
2019-08-03 06:41:40 +02:00
|
|
|
$a = ASN1::decodeBER($a);
|
|
|
|
$a = ASN1::asn1map($a[0], $map);
|
2019-08-03 04:51:06 +02:00
|
|
|
|
2020-12-12 22:11:04 +01:00
|
|
|
$this->assertIsArray($a);
|
2019-08-03 04:51:06 +02:00
|
|
|
}
|
2021-04-03 18:38:11 +02:00
|
|
|
|
2022-06-04 17:31:21 +02:00
|
|
|
public function testNullGarbage(): void
|
2021-04-03 18:38:11 +02:00
|
|
|
{
|
|
|
|
$em = pack('H*', '3080305c0609608648016503040201054f8888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888804207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9');
|
2021-04-03 20:28:23 +02:00
|
|
|
$decoded = ASN1::decodeBER($em);
|
2022-06-22 14:57:03 +02:00
|
|
|
$this->assertNull($decoded);
|
2021-04-03 18:38:11 +02:00
|
|
|
|
|
|
|
$em = pack('H*', '3080307f0609608648016503040201057288888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888804207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca90000');
|
2021-04-03 20:28:23 +02:00
|
|
|
$decoded = ASN1::decodeBER($em);
|
2022-06-22 14:57:03 +02:00
|
|
|
$this->assertNull($decoded);
|
2021-04-03 18:38:11 +02:00
|
|
|
}
|
2021-04-03 19:04:07 +02:00
|
|
|
|
2022-06-04 17:31:21 +02:00
|
|
|
public function testOIDGarbage(): void
|
2021-04-03 19:04:07 +02:00
|
|
|
{
|
|
|
|
$em = pack('H*', '3080305c065860864801650304020188888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888050004207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9');
|
2021-04-03 20:28:23 +02:00
|
|
|
$decoded = ASN1::decodeBER($em);
|
2022-06-22 14:57:03 +02:00
|
|
|
$this->assertNull($decoded);
|
2021-04-03 19:04:07 +02:00
|
|
|
|
|
|
|
$em = pack('H*', '3080307f067d608648016503040201888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888888804207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9');
|
2021-04-03 20:28:23 +02:00
|
|
|
$decoded = ASN1::decodeBER($em);
|
2022-06-22 14:57:03 +02:00
|
|
|
$this->assertNull($decoded);
|
2021-04-03 19:04:07 +02:00
|
|
|
}
|
2021-04-03 20:44:29 +02:00
|
|
|
|
2022-06-04 17:31:21 +02:00
|
|
|
public function testConstructedMismatch(): void
|
2021-04-03 20:44:29 +02:00
|
|
|
{
|
|
|
|
$em = pack('H*', '1031300d0609608648016503040201050004207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9');
|
2021-04-04 00:12:38 +02:00
|
|
|
$decoded = ASN1::decodeBER($em);
|
2022-06-22 14:57:03 +02:00
|
|
|
$this->assertNull($decoded);
|
2021-04-03 20:44:29 +02:00
|
|
|
|
|
|
|
$em = pack('H*', '3031100d0609608648016503040201050004207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9');
|
2021-04-04 00:12:38 +02:00
|
|
|
$decoded = ASN1::decodeBER($em);
|
2022-06-22 14:57:03 +02:00
|
|
|
$this->assertNull($decoded);
|
2021-04-03 20:44:29 +02:00
|
|
|
|
|
|
|
$em = pack('H*', '3031300d2609608648016503040201050004207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9');
|
2021-04-04 00:12:38 +02:00
|
|
|
$decoded = ASN1::decodeBER($em);
|
2022-06-22 14:57:03 +02:00
|
|
|
$this->assertNull($decoded);
|
2021-04-03 20:44:29 +02:00
|
|
|
|
|
|
|
$em = pack('H*', '3031300d06096086480165030402012d0004207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9');
|
2021-04-04 00:12:38 +02:00
|
|
|
$decoded = ASN1::decodeBER($em);
|
2022-06-22 14:57:03 +02:00
|
|
|
$this->assertNull($decoded);
|
2021-04-03 20:44:29 +02:00
|
|
|
}
|
2021-04-03 20:53:20 +02:00
|
|
|
|
2022-06-04 17:31:21 +02:00
|
|
|
public function testBadTagSecondOctet(): void
|
2021-04-03 20:53:20 +02:00
|
|
|
{
|
|
|
|
$em = pack('H*', '3033300f1f808080060960864801650304020104207509e5bda0c762d2bac7f90d758b5b2263fa01ccbc542ab5e3df163be08e6ca9');
|
2021-04-04 00:12:38 +02:00
|
|
|
$decoded = ASN1::decodeBER($em);
|
2022-06-22 14:57:03 +02:00
|
|
|
$this->assertNull($decoded);
|
2021-04-03 20:53:20 +02:00
|
|
|
}
|
2014-09-03 07:36:43 +02:00
|
|
|
}
|