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