From c6ad2b69baf4d5916b9ab0de10488db8ae58d823 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Mon, 22 Sep 2014 01:01:34 -0500 Subject: [PATCH 1/3] ASN1: fix issue with non-constructed context-specific tags --- phpseclib/File/ASN1.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/phpseclib/File/ASN1.php b/phpseclib/File/ASN1.php index 4bdf13ea..f4a7591e 100644 --- a/phpseclib/File/ASN1.php +++ b/phpseclib/File/ASN1.php @@ -347,10 +347,15 @@ class File_ASN1 case FILE_ASN1_CLASS_APPLICATION: case FILE_ASN1_CLASS_PRIVATE: case FILE_ASN1_CLASS_CONTEXT_SPECIFIC: - $newcontent = $this->_decode_ber($content, $start); - $length = $newcontent['length']; - if (substr($content, $length, 2) == "\0\0") { - $length+= 2; + if ($constructed) { + $newcontent = $this->_decode_ber($content, $start); + $length = $newcontent['length']; + if (substr($content, $length, 2) == "\0\0") { + $length+= 2; + } + + // the array encapsulation is for BC with the old format + $content = array($newcontent); } $start+= $length; @@ -359,7 +364,7 @@ class File_ASN1 'type' => $class, 'constant' => $tag, // the array encapsulation is for BC with the old format - 'content' => array($newcontent), + 'content' => $content, // the only time when $content['headerlength'] isn't defined is when the length is indefinite. // the absence of $content['headerlength'] is how we know if something is indefinite or not. // technically, it could be defined to be 2 and then another indicator could be used but whatever. @@ -1343,4 +1348,4 @@ class File_ASN1 } return $out; } -} +} \ No newline at end of file From a2f4a2cbbafa7b8beb061fa3d69d9f6bf5e73012 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Mon, 22 Sep 2014 23:03:06 -0500 Subject: [PATCH 2/3] ASN1: add unit test for non-constructed context-specific change --- phpseclib/File/ASN1.php | 2 +- tests/Unit/File/ASN1Test.php | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/phpseclib/File/ASN1.php b/phpseclib/File/ASN1.php index f4a7591e..eaa47eff 100644 --- a/phpseclib/File/ASN1.php +++ b/phpseclib/File/ASN1.php @@ -1348,4 +1348,4 @@ class File_ASN1 } return $out; } -} \ No newline at end of file +} diff --git a/tests/Unit/File/ASN1Test.php b/tests/Unit/File/ASN1Test.php index 4565edbd..fe35f688 100644 --- a/tests/Unit/File/ASN1Test.php +++ b/tests/Unit/File/ASN1Test.php @@ -268,4 +268,13 @@ class Unit_File_ASN1Test extends PhpseclibTestCase $decoded = $asn1->decodeBER(base64_decode($str)); $this->assertCount(3, $decoded[0]['content']); } + + /** + * @group github477 + */ + public function contextSpecificNonConstructed() + { + $decoded = $asn1->decodeBER(base64_decode('MBaAFJtUo7c00HsI5EPZ4bkICfkOY2Pv')); + $this->assertInternalType('string', $decoded[0]['content'][0]['content']); + } } From 3785bebccec82017356bed962e43ba451bd680f8 Mon Sep 17 00:00:00 2001 From: terrafrost Date: Wed, 24 Sep 2014 00:07:49 -0500 Subject: [PATCH 3/3] ASN1: fix unit test --- tests/Unit/File/ASN1Test.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Unit/File/ASN1Test.php b/tests/Unit/File/ASN1Test.php index fe35f688..5a5e6efb 100644 --- a/tests/Unit/File/ASN1Test.php +++ b/tests/Unit/File/ASN1Test.php @@ -272,8 +272,9 @@ class Unit_File_ASN1Test extends PhpseclibTestCase /** * @group github477 */ - public function contextSpecificNonConstructed() + public function testContextSpecificNonConstructed() { + $asn1 = new File_ASN1(); $decoded = $asn1->decodeBER(base64_decode('MBaAFJtUo7c00HsI5EPZ4bkICfkOY2Pv')); $this->assertInternalType('string', $decoded[0]['content'][0]['content']); }