1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\X509\GeneralName; |
6
|
|
|
|
7
|
|
|
use Sop\ASN1\Element; |
8
|
|
|
use Sop\ASN1\Type\TaggedType; |
9
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Implements *GeneralName* CHOICE with implicit tagging. |
13
|
|
|
* |
14
|
|
|
* @see https://tools.ietf.org/html/rfc5280#section-4.2.1.6 |
15
|
|
|
*/ |
16
|
|
|
abstract class GeneralName |
17
|
|
|
{ |
18
|
|
|
// GeneralName CHOICE tags |
19
|
|
|
const TAG_OTHER_NAME = 0; |
20
|
|
|
const TAG_RFC822_NAME = 1; |
21
|
|
|
const TAG_DNS_NAME = 2; |
22
|
|
|
const TAG_X400_ADDRESS = 3; |
23
|
|
|
const TAG_DIRECTORY_NAME = 4; |
24
|
|
|
const TAG_EDI_PARTY_NAME = 5; |
25
|
|
|
const TAG_URI = 6; |
26
|
|
|
const TAG_IP_ADDRESS = 7; |
27
|
|
|
const TAG_REGISTERED_ID = 8; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Chosen tag. |
31
|
|
|
* |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $_tag; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get general name as a string. |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
8 |
|
public function __toString(): string |
42
|
|
|
{ |
43
|
8 |
|
return $this->string(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get string value of the type. |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
abstract public function string(): string; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Initialize concrete object from the chosen ASN.1 element. |
55
|
|
|
* |
56
|
|
|
* @param UnspecifiedType $el |
57
|
|
|
* |
58
|
|
|
* @return self |
59
|
|
|
*/ |
60
|
1 |
|
public static function fromChosenASN1(UnspecifiedType $el): GeneralName |
61
|
|
|
{ |
62
|
1 |
|
throw new \BadMethodCallException( |
63
|
1 |
|
__FUNCTION__ . ' must be implemented in the derived class.'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Initialize from ASN.1. |
68
|
|
|
* |
69
|
|
|
* @param TaggedType $el |
70
|
|
|
* |
71
|
|
|
* @throws \UnexpectedValueException |
72
|
|
|
* |
73
|
|
|
* @return self |
74
|
|
|
*/ |
75
|
68 |
|
public static function fromASN1(TaggedType $el): self |
76
|
|
|
{ |
77
|
68 |
|
switch ($el->tag()) { |
78
|
|
|
// otherName |
79
|
68 |
|
case self::TAG_OTHER_NAME: |
80
|
9 |
|
return OtherName::fromChosenASN1( |
81
|
9 |
|
$el->asImplicit(Element::TYPE_SEQUENCE)); |
82
|
|
|
// rfc822Name |
83
|
67 |
|
case self::TAG_RFC822_NAME: |
84
|
10 |
|
return RFC822Name::fromChosenASN1( |
85
|
10 |
|
$el->asImplicit(Element::TYPE_IA5_STRING)); |
86
|
|
|
// dNSName |
87
|
65 |
|
case self::TAG_DNS_NAME: |
88
|
13 |
|
return DNSName::fromChosenASN1( |
89
|
13 |
|
$el->asImplicit(Element::TYPE_IA5_STRING)); |
90
|
|
|
// x400Address |
91
|
63 |
|
case self::TAG_X400_ADDRESS: |
92
|
2 |
|
return X400Address::fromChosenASN1( |
93
|
2 |
|
$el->asImplicit(Element::TYPE_SEQUENCE)); |
94
|
|
|
// directoryName |
95
|
61 |
|
case self::TAG_DIRECTORY_NAME: |
96
|
|
|
// because Name is a CHOICE, albeit having only one option, |
97
|
|
|
// explicit tagging must be used |
98
|
|
|
// (see X.680 07/2002 30.6.c) |
99
|
37 |
|
return DirectoryName::fromChosenASN1($el->asExplicit()); |
100
|
|
|
// ediPartyName |
101
|
43 |
|
case self::TAG_EDI_PARTY_NAME: |
102
|
2 |
|
return EDIPartyName::fromChosenASN1( |
103
|
2 |
|
$el->asImplicit(Element::TYPE_SEQUENCE)); |
104
|
|
|
// uniformResourceIdentifier |
105
|
41 |
|
case self::TAG_URI: |
106
|
34 |
|
return UniformResourceIdentifier::fromChosenASN1( |
107
|
34 |
|
$el->asImplicit(Element::TYPE_IA5_STRING)); |
108
|
|
|
// iPAddress |
109
|
15 |
|
case self::TAG_IP_ADDRESS: |
110
|
13 |
|
return IPAddress::fromChosenASN1( |
111
|
13 |
|
$el->asImplicit(Element::TYPE_OCTET_STRING)); |
112
|
|
|
// registeredID |
113
|
10 |
|
case self::TAG_REGISTERED_ID: |
114
|
9 |
|
return RegisteredID::fromChosenASN1( |
115
|
9 |
|
$el->asImplicit(Element::TYPE_OBJECT_IDENTIFIER)); |
116
|
|
|
} |
117
|
1 |
|
throw new \UnexpectedValueException( |
118
|
1 |
|
'GeneralName type ' . $el->tag() . ' not supported.'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get type tag. |
123
|
|
|
* |
124
|
|
|
* @return int |
125
|
|
|
*/ |
126
|
58 |
|
public function tag(): int |
127
|
|
|
{ |
128
|
58 |
|
return $this->_tag; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Generate ASN.1 element. |
133
|
|
|
* |
134
|
|
|
* @return Element |
135
|
|
|
*/ |
136
|
90 |
|
public function toASN1(): Element |
137
|
|
|
{ |
138
|
90 |
|
return $this->_choiceASN1(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Check whether GeneralName is equal to other. |
143
|
|
|
* |
144
|
|
|
* @param GeneralName $other GeneralName to compare to |
145
|
|
|
* |
146
|
|
|
* @return bool True if names are equal |
147
|
|
|
*/ |
148
|
3 |
|
public function equals(GeneralName $other): bool |
149
|
|
|
{ |
150
|
3 |
|
if ($this->_tag !== $other->_tag) { |
151
|
1 |
|
return false; |
152
|
|
|
} |
153
|
2 |
|
if ($this->_choiceASN1()->toDER() !== $other->_choiceASN1()->toDER()) { |
154
|
1 |
|
return false; |
155
|
|
|
} |
156
|
1 |
|
return true; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Get ASN.1 value in GeneralName CHOICE context. |
161
|
|
|
* |
162
|
|
|
* @return TaggedType |
163
|
|
|
*/ |
164
|
|
|
abstract protected function _choiceASN1(): TaggedType; |
165
|
|
|
} |
166
|
|
|
|