|
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\ParseException; |
|
17
|
|
|
use Badcow\DNS\Parser\Tokens; |
|
18
|
|
|
use Badcow\DNS\Validator; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* {@link https://tools.ietf.org/html/rfc6698}. |
|
22
|
|
|
*/ |
|
23
|
|
|
class TLSA implements RdataInterface |
|
24
|
|
|
{ |
|
25
|
|
|
use RdataTrait; |
|
26
|
|
|
|
|
27
|
|
|
const TYPE = 'TLSA'; |
|
28
|
|
|
const TYPE_CODE = 52; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* A one-octet value, called "certificate usage", specifies the provided |
|
32
|
|
|
* association that will be used to match the certificate presented in |
|
33
|
|
|
* the TLS handshake. |
|
34
|
|
|
* |
|
35
|
|
|
* @var int uint8 |
|
36
|
|
|
*/ |
|
37
|
|
|
private $certificateUsage; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* A one-octet value, called "selector", specifies which part of the TLS |
|
41
|
|
|
* certificate presented by the server will be matched against the |
|
42
|
|
|
* association data. |
|
43
|
|
|
* |
|
44
|
|
|
* @var int uint8 |
|
45
|
|
|
*/ |
|
46
|
|
|
private $selector; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* A one-octet value, called "matching type", specifies how the |
|
50
|
|
|
* certificate association is presented. |
|
51
|
|
|
* |
|
52
|
|
|
* @var int uint8 |
|
53
|
|
|
*/ |
|
54
|
|
|
private $matchingType; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* This field specifies the "certificate association data" to be |
|
58
|
|
|
* matched. These bytes are either raw data (that is, the full |
|
59
|
|
|
* certificate or its SubjectPublicKeyInfo, depending on the selector) |
|
60
|
|
|
* for matching type 0, or the hash of the raw data for matching types 1 |
|
61
|
|
|
* and 2. The data refers to the certificate in the association, not to |
|
62
|
|
|
* the TLS ASN.1 Certificate object. |
|
63
|
|
|
* |
|
64
|
|
|
* @var string |
|
65
|
|
|
*/ |
|
66
|
|
|
private $certificateAssociationData; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* @return int |
|
70
|
|
|
*/ |
|
71
|
3 |
|
public function getCertificateUsage(): int |
|
72
|
|
|
{ |
|
73
|
3 |
|
return $this->certificateUsage; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @param int $certificateUsage |
|
78
|
|
|
*/ |
|
79
|
7 |
|
public function setCertificateUsage(int $certificateUsage): void |
|
80
|
|
|
{ |
|
81
|
7 |
|
if (!Validator::isUnsignedInteger($certificateUsage, 8)) { |
|
82
|
|
|
throw new \InvalidArgumentException('Certificate usage must be an 8-bit integer.'); |
|
83
|
|
|
} |
|
84
|
7 |
|
$this->certificateUsage = $certificateUsage; |
|
85
|
7 |
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return int |
|
89
|
|
|
*/ |
|
90
|
3 |
|
public function getSelector(): int |
|
91
|
|
|
{ |
|
92
|
3 |
|
return $this->selector; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param int $selector |
|
97
|
|
|
*/ |
|
98
|
7 |
|
public function setSelector(int $selector): void |
|
99
|
|
|
{ |
|
100
|
7 |
|
if (!Validator::isUnsignedInteger($selector, 8)) { |
|
101
|
|
|
throw new \InvalidArgumentException('Selector must be an 8-bit integer.'); |
|
102
|
|
|
} |
|
103
|
7 |
|
$this->selector = $selector; |
|
104
|
7 |
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return int |
|
108
|
|
|
*/ |
|
109
|
3 |
|
public function getMatchingType(): int |
|
110
|
|
|
{ |
|
111
|
3 |
|
return $this->matchingType; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param int $matchingType |
|
116
|
|
|
*/ |
|
117
|
7 |
|
public function setMatchingType(int $matchingType): void |
|
118
|
|
|
{ |
|
119
|
7 |
|
if (!Validator::isUnsignedInteger($matchingType, 8)) { |
|
120
|
1 |
|
throw new \InvalidArgumentException('Matching type must be an 8-bit integer.'); |
|
121
|
|
|
} |
|
122
|
6 |
|
$this->matchingType = $matchingType; |
|
123
|
6 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
3 |
|
public function getCertificateAssociationData(): string |
|
129
|
|
|
{ |
|
130
|
3 |
|
return $this->certificateAssociationData; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param string $certificateAssociationData |
|
135
|
|
|
*/ |
|
136
|
5 |
|
public function setCertificateAssociationData(string $certificateAssociationData): void |
|
137
|
|
|
{ |
|
138
|
5 |
|
$this->certificateAssociationData = $certificateAssociationData; |
|
139
|
5 |
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* {@inheritdoc} |
|
143
|
|
|
*/ |
|
144
|
1 |
|
public function toText(): string |
|
145
|
|
|
{ |
|
146
|
1 |
|
return sprintf('%d %d %d %s', $this->certificateUsage, $this->selector, $this->matchingType, bin2hex($this->certificateAssociationData)); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* {@inheritdoc} |
|
151
|
|
|
*/ |
|
152
|
1 |
|
public function toWire(): string |
|
153
|
|
|
{ |
|
154
|
1 |
|
return pack('CCC', $this->certificateUsage, $this->selector, $this->matchingType).$this->certificateAssociationData; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* {@inheritdoc} |
|
159
|
|
|
* |
|
160
|
|
|
* @return TLSA |
|
161
|
|
|
* |
|
162
|
|
|
* @throws ParseException |
|
163
|
|
|
*/ |
|
164
|
2 |
|
public static function fromText(string $text): RdataInterface |
|
165
|
|
|
{ |
|
166
|
2 |
|
$rdata = explode(Tokens::SPACE, $text); |
|
167
|
2 |
|
$tlsa = new self(); |
|
168
|
2 |
|
$tlsa->setCertificateUsage((int) array_shift($rdata)); |
|
169
|
2 |
|
$tlsa->setSelector((int) array_shift($rdata)); |
|
170
|
2 |
|
$tlsa->setMatchingType((int) array_shift($rdata)); |
|
171
|
2 |
|
if (false === $certificateAssociationData = @hex2bin(implode('', $rdata))) { |
|
172
|
1 |
|
throw new ParseException('Unable to parse certificate association data of TLSA record. Malformed hex value.'); |
|
173
|
|
|
} |
|
174
|
1 |
|
$tlsa->setCertificateAssociationData($certificateAssociationData); |
|
175
|
|
|
|
|
176
|
1 |
|
return $tlsa; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* {@inheritdoc} |
|
181
|
|
|
* |
|
182
|
|
|
* @return TLSA |
|
183
|
|
|
*/ |
|
184
|
1 |
|
public static function fromWire(string $rdata): RdataInterface |
|
185
|
|
|
{ |
|
186
|
1 |
|
$offset = 0; |
|
187
|
1 |
|
$integers = unpack('C<certUsage>/C<selector>/C<matchingType>', $rdata, $offset); |
|
188
|
1 |
|
$offset += 3; |
|
189
|
1 |
|
$tlsa = new self(); |
|
190
|
1 |
|
$tlsa->setCertificateUsage($integers['<certUsage>']); |
|
191
|
1 |
|
$tlsa->setSelector($integers['<selector>']); |
|
192
|
1 |
|
$tlsa->setMatchingType($integers['<matchingType>']); |
|
193
|
1 |
|
$tlsa->setCertificateAssociationData(substr($rdata, $offset)); |
|
194
|
|
|
|
|
195
|
1 |
|
return $tlsa; |
|
196
|
|
|
} |
|
197
|
|
|
} |
|
198
|
|
|
|