1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Badcow DNS Library. |
7
|
|
|
* |
8
|
|
|
* (c) Samuel Williams <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Badcow\DNS\Rdata; |
15
|
|
|
|
16
|
|
|
use Badcow\DNS\Parser\Tokens; |
17
|
|
|
use Badcow\DNS\Validator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class CaaRdata. |
21
|
|
|
* |
22
|
|
|
* CAA is defined in RFC 6844 |
23
|
|
|
* |
24
|
|
|
* @see https://tools.ietf.org/html/rfc6844 |
25
|
|
|
* |
26
|
|
|
* @author Samuel Williams <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class CAA implements RdataInterface |
29
|
|
|
{ |
30
|
|
|
use RdataTrait; |
31
|
|
|
|
32
|
|
|
const TYPE = 'CAA'; |
33
|
|
|
const TYPE_CODE = 257; |
34
|
|
|
|
35
|
|
|
const TAG_ISSUE = 'issue'; |
36
|
|
|
const TAG_ISSUEWILD = 'issuewild'; |
37
|
|
|
const TAG_IODEF = 'iodef'; |
38
|
|
|
const ALLOWED_TAGS = [self::TAG_ISSUE, self::TAG_ISSUEWILD, self::TAG_IODEF]; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* It is currently used to represent the critical flag. |
42
|
|
|
* |
43
|
|
|
* @var int |
44
|
|
|
*/ |
45
|
|
|
private $flag; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* An ASCII string that represents the identifier of the property represented by the record. |
49
|
|
|
* The RFC currently defines 3 available tags: |
50
|
|
|
* - issue: explicitly authorizes a single certificate authority to issue a certificate (any type) for the hostname. |
51
|
|
|
* - issuewild: explicitly authorizes a single certificate authority to issue a wildcard certificate (and only wildcard) for the hostname. |
52
|
|
|
* - iodef: specifies a URL to which a certificate authority may report policy violations. |
53
|
|
|
* |
54
|
|
|
* @var string |
55
|
|
|
*/ |
56
|
|
|
private $tag; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
private $value; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return int |
65
|
|
|
*/ |
66
|
3 |
|
public function getFlag(): ?int |
67
|
|
|
{ |
68
|
3 |
|
return $this->flag; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param int $flag |
73
|
|
|
* |
74
|
|
|
* @throws \InvalidArgumentException |
75
|
|
|
*/ |
76
|
6 |
|
public function setFlag(int $flag): void |
77
|
|
|
{ |
78
|
6 |
|
if (!Validator::isUnsignedInteger($flag, 8)) { |
79
|
1 |
|
throw new \InvalidArgumentException('Flag must be an unsigned 8-bit integer.'); |
80
|
|
|
} |
81
|
|
|
|
82
|
5 |
|
$this->flag = $flag; |
83
|
5 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
3 |
|
public function getTag(): ?string |
89
|
|
|
{ |
90
|
3 |
|
return $this->tag; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $tag |
95
|
|
|
* |
96
|
|
|
* @throws \InvalidArgumentException |
97
|
|
|
*/ |
98
|
7 |
|
public function setTag(string $tag): void |
99
|
|
|
{ |
100
|
7 |
|
$tag = strtolower($tag); |
101
|
7 |
|
if (!in_array($tag, static::ALLOWED_TAGS)) { |
102
|
1 |
|
throw new \InvalidArgumentException('Tag can be one of this type "issue", "issuewild", or "iodef".'); |
103
|
|
|
} |
104
|
|
|
|
105
|
6 |
|
$this->tag = $tag; |
106
|
6 |
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return string|null |
110
|
|
|
*/ |
111
|
2 |
|
public function getValue(): ?string |
112
|
|
|
{ |
113
|
2 |
|
return $this->value; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $value |
118
|
|
|
*/ |
119
|
5 |
|
public function setValue(string $value): void |
120
|
|
|
{ |
121
|
5 |
|
$this->value = $value; |
122
|
5 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
2 |
|
public function toText(): string |
128
|
|
|
{ |
129
|
2 |
|
if (!isset($this->tag) || !isset($this->flag) || !isset($this->value)) { |
130
|
1 |
|
throw new \InvalidArgumentException('All CAA parameters must be set.'); |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
return sprintf('%d %s "%s"', |
134
|
1 |
|
$this->flag, |
135
|
1 |
|
$this->tag ?? '', |
136
|
1 |
|
$this->value ?? '' |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
2 |
|
public function toWire(): string |
144
|
|
|
{ |
145
|
2 |
|
if (!isset($this->tag) || !isset($this->flag) || !isset($this->value)) { |
146
|
1 |
|
throw new \InvalidArgumentException('All CAA parameters must be set.'); |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
return chr($this->flag). |
150
|
1 |
|
chr(strlen($this->tag)). |
151
|
1 |
|
$this->tag. |
152
|
1 |
|
$this->value; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* {@inheritdoc} |
157
|
|
|
* |
158
|
|
|
* @return CAA |
159
|
|
|
*/ |
160
|
1 |
|
public static function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): RdataInterface |
161
|
|
|
{ |
162
|
1 |
|
$caa = new self(); |
163
|
|
|
|
164
|
1 |
|
$caa->setFlag(ord($rdata[$offset])); |
165
|
1 |
|
++$offset; |
166
|
|
|
|
167
|
1 |
|
$tagLen = ord($rdata[$offset]); |
168
|
1 |
|
++$offset; |
169
|
|
|
|
170
|
1 |
|
$caa->setTag(substr($rdata, $offset, $tagLen)); |
171
|
1 |
|
$offset += $tagLen; |
172
|
|
|
|
173
|
1 |
|
$valueLen = ($rdLength ?? strlen($rdata)) - 2 - $tagLen; |
174
|
1 |
|
$caa->setValue(substr($rdata, $offset, $valueLen)); |
175
|
|
|
|
176
|
1 |
|
$offset = $offset += $valueLen; |
177
|
|
|
|
178
|
1 |
|
return $caa; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* {@inheritdoc} |
183
|
|
|
* |
184
|
|
|
* @return CAA |
185
|
|
|
*/ |
186
|
2 |
|
public static function fromText(string $string): RdataInterface |
187
|
|
|
{ |
188
|
2 |
|
$caa = new self(); |
189
|
2 |
|
$rdata = explode(Tokens::SPACE, $string); |
190
|
2 |
|
$caa->setFlag((int) array_shift($rdata)); |
191
|
2 |
|
$caa->setTag((string) array_shift($rdata)); |
192
|
2 |
|
$rdata = implode('', $rdata); |
193
|
2 |
|
$caa->setValue(trim($rdata, '"')); |
194
|
|
|
|
195
|
2 |
|
return $caa; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|