1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types = 1); |
4
|
|
|
|
5
|
|
|
namespace Sop\X509\GeneralName; |
6
|
|
|
|
7
|
|
|
use Sop\ASN1\Type\Primitive\OctetString; |
8
|
|
|
use Sop\ASN1\Type\Tagged\ImplicitlyTaggedType; |
9
|
|
|
use Sop\ASN1\Type\TaggedType; |
10
|
|
|
use Sop\ASN1\Type\UnspecifiedType; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Implements <i>iPAddress</i> CHOICE type of <i>GeneralName</i>. |
14
|
|
|
* |
15
|
|
|
* Concrete classes <code>IPv4Address</code> and <code>IPv6Address</code> |
16
|
|
|
* furthermore implement the parsing semantics. |
17
|
|
|
* |
18
|
|
|
* @see https://tools.ietf.org/html/rfc5280#section-4.2.1.6 |
19
|
|
|
*/ |
20
|
|
|
abstract class IPAddress extends GeneralName |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* IP address. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $_ip; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Subnet mask. |
31
|
|
|
* |
32
|
|
|
* @var null|string |
33
|
|
|
*/ |
34
|
|
|
protected $_mask; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructor. |
38
|
|
|
* |
39
|
|
|
* @param string $ip |
40
|
|
|
* @param null|string $mask |
41
|
|
|
*/ |
42
|
16 |
|
public function __construct(string $ip, ?string $mask = null) |
43
|
|
|
{ |
44
|
16 |
|
$this->_tag = self::TAG_IP_ADDRESS; |
45
|
16 |
|
$this->_ip = $ip; |
46
|
16 |
|
$this->_mask = $mask; |
47
|
16 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritdoc} |
51
|
|
|
* |
52
|
|
|
* @return self |
53
|
|
|
*/ |
54
|
13 |
|
public static function fromChosenASN1(UnspecifiedType $el): GeneralName |
55
|
|
|
{ |
56
|
13 |
|
$octets = $el->asOctetString()->string(); |
57
|
13 |
|
switch (strlen($octets)) { |
58
|
13 |
|
case 4: |
59
|
12 |
|
case 8: |
60
|
10 |
|
return IPv4Address::fromOctets($octets); |
61
|
11 |
|
case 16: |
62
|
2 |
|
case 32: |
63
|
10 |
|
return IPv6Address::fromOctets($octets); |
64
|
|
|
default: |
65
|
1 |
|
throw new \UnexpectedValueException( |
66
|
1 |
|
'Invalid octet length for IP address.'); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
1 |
|
public function string(): string |
74
|
|
|
{ |
75
|
1 |
|
return $this->_ip . (isset($this->_mask) ? '/' . $this->_mask : ''); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Get IP address as a string. |
80
|
|
|
* |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
3 |
|
public function address(): string |
84
|
|
|
{ |
85
|
3 |
|
return $this->_ip; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Check whether mask is present. |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
3 |
|
public function hasMask(): bool |
94
|
|
|
{ |
95
|
3 |
|
return isset($this->_mask); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Get subnet mask as a string. |
100
|
|
|
* |
101
|
|
|
* @throws \LogicException If not set |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
3 |
|
public function mask(): string |
106
|
|
|
{ |
107
|
3 |
|
if (!$this->hasMask()) { |
108
|
1 |
|
throw new \LogicException('mask is not set.'); |
109
|
|
|
} |
110
|
2 |
|
return $this->_mask; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get octet representation of the IP address. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
abstract protected function _octets(): string; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
18 |
|
protected function _choiceASN1(): TaggedType |
124
|
|
|
{ |
125
|
18 |
|
return new ImplicitlyTaggedType($this->_tag, |
126
|
18 |
|
new OctetString($this->_octets())); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|