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
|
1 |
|
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
|
3 |
|
public function getCertificateUsage(): int |
69
|
|
|
{ |
70
|
3 |
|
return $this->certificateUsage; |
71
|
|
|
} |
72
|
|
|
|
73
|
7 |
|
public function setCertificateUsage(int $certificateUsage): void |
74
|
|
|
{ |
75
|
7 |
|
if (!Validator::isUnsignedInteger($certificateUsage, 8)) { |
76
|
|
|
throw new \InvalidArgumentException('Certificate usage must be an 8-bit integer.'); |
77
|
|
|
} |
78
|
7 |
|
$this->certificateUsage = $certificateUsage; |
79
|
7 |
|
} |
80
|
|
|
|
81
|
3 |
|
public function getSelector(): int |
82
|
|
|
{ |
83
|
3 |
|
return $this->selector; |
84
|
|
|
} |
85
|
|
|
|
86
|
7 |
|
public function setSelector(int $selector): void |
87
|
|
|
{ |
88
|
7 |
|
if (!Validator::isUnsignedInteger($selector, 8)) { |
89
|
|
|
throw new \InvalidArgumentException('Selector must be an 8-bit integer.'); |
90
|
|
|
} |
91
|
7 |
|
$this->selector = $selector; |
92
|
7 |
|
} |
93
|
|
|
|
94
|
3 |
|
public function getMatchingType(): int |
95
|
|
|
{ |
96
|
3 |
|
return $this->matchingType; |
97
|
|
|
} |
98
|
|
|
|
99
|
7 |
|
public function setMatchingType(int $matchingType): void |
100
|
|
|
{ |
101
|
7 |
|
if (!Validator::isUnsignedInteger($matchingType, 8)) { |
102
|
1 |
|
throw new \InvalidArgumentException('Matching type must be an 8-bit integer.'); |
103
|
|
|
} |
104
|
6 |
|
$this->matchingType = $matchingType; |
105
|
6 |
|
} |
106
|
|
|
|
107
|
3 |
|
public function getCertificateAssociationData(): string |
108
|
|
|
{ |
109
|
3 |
|
return $this->certificateAssociationData; |
110
|
|
|
} |
111
|
|
|
|
112
|
5 |
|
public function setCertificateAssociationData(string $certificateAssociationData): void |
113
|
|
|
{ |
114
|
5 |
|
$this->certificateAssociationData = $certificateAssociationData; |
115
|
5 |
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
1 |
|
public function toText(): string |
121
|
|
|
{ |
122
|
1 |
|
return sprintf('%d %d %d %s', $this->certificateUsage, $this->selector, $this->matchingType, bin2hex($this->certificateAssociationData)); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* {@inheritdoc} |
127
|
|
|
*/ |
128
|
1 |
|
public function toWire(): string |
129
|
|
|
{ |
130
|
1 |
|
return pack('CCC', $this->certificateUsage, $this->selector, $this->matchingType).$this->certificateAssociationData; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
* |
136
|
|
|
* @throws ParseException |
137
|
|
|
*/ |
138
|
2 |
|
public function fromText(string $text): void |
139
|
|
|
{ |
140
|
2 |
|
$rdata = explode(Tokens::SPACE, $text); |
141
|
2 |
|
$this->setCertificateUsage((int) array_shift($rdata)); |
142
|
2 |
|
$this->setSelector((int) array_shift($rdata)); |
143
|
2 |
|
$this->setMatchingType((int) array_shift($rdata)); |
144
|
2 |
|
if (false === $certificateAssociationData = @hex2bin(implode('', $rdata))) { |
145
|
1 |
|
throw new ParseException('Unable to parse certificate association data of TLSA record. Malformed hex value.'); |
146
|
|
|
} |
147
|
1 |
|
$this->setCertificateAssociationData($certificateAssociationData); |
148
|
1 |
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
1 |
|
public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void |
154
|
|
|
{ |
155
|
1 |
|
$integers = unpack('C<certUsage>/C<selector>/C<matchingType>', $rdata, $offset); |
156
|
1 |
|
$offset += 3; |
157
|
1 |
|
$this->setCertificateUsage($integers['<certUsage>']); |
158
|
1 |
|
$this->setSelector($integers['<selector>']); |
159
|
1 |
|
$this->setMatchingType($integers['<matchingType>']); |
160
|
1 |
|
$cadLen = ($rdLength ?? strlen($rdata)) - 3; |
161
|
1 |
|
$this->setCertificateAssociationData(substr($rdata, $offset, $cadLen)); |
162
|
1 |
|
$offset += $cadLen; |
163
|
1 |
|
} |
164
|
|
|
} |
165
|
|
|
|