Skip to content

Commit 4d461e0

Browse files
committed
Fix implicit tag collision
1 parent cd265f1 commit 4d461e0

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

lib/X509/Certificate/Extension/NameConstraints/GeneralSubtree.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,22 @@ public static function fromASN1(Sequence $seq): self
6565
$base = GeneralName::fromASN1($seq->at(0)->asTagged());
6666
$min = 0;
6767
$max = null;
68-
if ($seq->hasTagged(0)) {
69-
$min = $seq->getTagged(0)->asImplicit(Element::TYPE_INTEGER)
70-
->asInteger()->intNumber();
71-
}
72-
if ($seq->hasTagged(1)) {
73-
$max = $seq->getTagged(1)->asImplicit(Element::TYPE_INTEGER)
74-
->asInteger()->intNumber();
68+
// GeneralName is a CHOICE, which may be tagged as otherName [0]
69+
// or rfc822Name [1]. As minimum and maximum are also implicitly tagged,
70+
// we have to iterate the remaining elements instead of just checking
71+
// for tagged types.
72+
for ($i = 1; $i < count($seq); ++$i) {
73+
$el = $seq->at($i)->expectTagged();
74+
switch ($el->tag()) {
75+
case 0:
76+
$min = $el->asImplicit(Element::TYPE_INTEGER)
77+
->asInteger()->intNumber();
78+
break;
79+
case 1:
80+
$max = $el->asImplicit(Element::TYPE_INTEGER)
81+
->asInteger()->intNumber();
82+
break;
83+
}
7584
}
7685
return new self($base, $min, $max);
7786
}

test/unit/certificate/extension/name-constraints/GeneralSubtreeTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Sop\ASN1\Type\Constructed\Sequence;
77
use Sop\X509\Certificate\Extension\NameConstraints\GeneralSubtree;
88
use Sop\X509\GeneralName\GeneralName;
9+
use Sop\X509\GeneralName\RFC822Name;
910
use Sop\X509\GeneralName\UniformResourceIdentifier;
1011

1112
/**
@@ -116,4 +117,15 @@ public function testRecodedWithAll(GeneralSubtree $ref, GeneralSubtree $new)
116117
{
117118
$this->assertEquals($ref, $new);
118119
}
120+
121+
/**
122+
* Test for GeneralName tag that collide with other GeneralSubtree tags.
123+
*/
124+
public function testCollidingTag()
125+
{
126+
$subtree = new GeneralSubtree(new RFC822Name('test'));
127+
$asn1 = $subtree->toASN1();
128+
$result = GeneralSubtree::fromASN1($asn1);
129+
$this->assertInstanceOf(GeneralSubtree::class, $result);
130+
}
119131
}

0 commit comments

Comments
 (0)