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
|
|
|
* {@link https://tools.ietf.org/html/rfc5155#section-4}. |
21
|
|
|
*/ |
22
|
|
|
class NSEC3PARAM implements RdataInterface |
23
|
|
|
{ |
24
|
|
|
use RdataTrait; |
25
|
|
|
|
26
|
|
|
const TYPE = 'NSEC3PARAM'; |
27
|
|
|
const TYPE_CODE = 51; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var int |
31
|
|
|
*/ |
32
|
|
|
private $hashAlgorithm; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
private $flags = 0; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var int |
41
|
|
|
*/ |
42
|
|
|
private $iterations; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var string Binary encoded string |
46
|
|
|
*/ |
47
|
|
|
private $salt; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @return int |
51
|
|
|
*/ |
52
|
1 |
|
public function getHashAlgorithm(): int |
53
|
|
|
{ |
54
|
1 |
|
return $this->hashAlgorithm; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param int $hashAlgorithm |
59
|
|
|
* |
60
|
|
|
* @throws \InvalidArgumentException |
61
|
|
|
*/ |
62
|
6 |
|
public function setHashAlgorithm(int $hashAlgorithm): void |
63
|
|
|
{ |
64
|
6 |
|
if (!Validator::isUnsignedInteger($hashAlgorithm, 8)) { |
65
|
|
|
throw new \InvalidArgumentException('Hash algorithm must be 8-bit integer.'); |
66
|
|
|
} |
67
|
6 |
|
$this->hashAlgorithm = $hashAlgorithm; |
68
|
6 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return int |
72
|
|
|
*/ |
73
|
1 |
|
public function getFlags(): int |
74
|
|
|
{ |
75
|
1 |
|
return $this->flags; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param int $flags |
80
|
|
|
* |
81
|
|
|
* @throws \InvalidArgumentException |
82
|
|
|
*/ |
83
|
6 |
|
public function setFlags(int $flags): void |
84
|
|
|
{ |
85
|
6 |
|
if (!Validator::isUnsignedInteger($flags, 8)) { |
86
|
|
|
throw new \InvalidArgumentException('Flags must be an 8-bit unsigned integer.'); |
87
|
|
|
} |
88
|
6 |
|
$this->flags = $flags; |
89
|
6 |
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return int |
93
|
|
|
*/ |
94
|
1 |
|
public function getIterations(): int |
95
|
|
|
{ |
96
|
1 |
|
return $this->iterations; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param int $iterations |
101
|
|
|
*/ |
102
|
6 |
|
public function setIterations(int $iterations): void |
103
|
|
|
{ |
104
|
6 |
|
if (!Validator::isUnsignedInteger($iterations, 16)) { |
105
|
|
|
throw new \InvalidArgumentException('Hash algorithm must be 16-bit integer.'); |
106
|
|
|
} |
107
|
6 |
|
$this->iterations = $iterations; |
108
|
6 |
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return string Base16 string |
112
|
|
|
*/ |
113
|
1 |
|
public function getSalt(): string |
114
|
|
|
{ |
115
|
1 |
|
return bin2hex($this->salt); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param string $salt Hexadecimal string |
120
|
|
|
*/ |
121
|
6 |
|
public function setSalt(string $salt): void |
122
|
|
|
{ |
123
|
6 |
|
if (false === $bin = @hex2bin($salt)) { |
124
|
|
|
throw new \InvalidArgumentException('Salt must be a hexadecimal string.'); |
125
|
|
|
} |
126
|
6 |
|
$this->salt = $bin; |
127
|
6 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
2 |
|
public function toText(): string |
133
|
|
|
{ |
134
|
2 |
|
return sprintf('%d %d %d %s', $this->hashAlgorithm, $this->flags, $this->iterations, bin2hex($this->salt)); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
1 |
|
public function toWire(): string |
141
|
|
|
{ |
142
|
1 |
|
return pack('CCnC', $this->hashAlgorithm, $this->flags, $this->iterations, strlen($this->salt)).$this->salt; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* {@inheritdoc} |
147
|
|
|
*/ |
148
|
1 |
|
public function fromText(string $text): void |
149
|
|
|
{ |
150
|
1 |
|
$rdata = explode(Tokens::SPACE, $text); |
151
|
1 |
|
$this->setHashAlgorithm((int) array_shift($rdata)); |
152
|
1 |
|
$this->setFlags((int) array_shift($rdata)); |
153
|
1 |
|
$this->setIterations((int) array_shift($rdata)); |
154
|
1 |
|
$this->setSalt((string) array_shift($rdata)); |
155
|
1 |
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
1 |
|
public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void |
161
|
|
|
{ |
162
|
1 |
|
$integers = unpack('C<algorithm>/C<flags>/n<iterations>/C<saltLen>', $rdata, $offset); |
163
|
1 |
|
$saltLen = (int) $integers['<saltLen>']; |
164
|
1 |
|
$offset += 5; |
165
|
1 |
|
$this->setHashAlgorithm($integers['<algorithm>']); |
166
|
1 |
|
$this->setFlags($integers['<flags>']); |
167
|
1 |
|
$this->setIterations($integers['<iterations>']); |
168
|
|
|
|
169
|
1 |
|
$saltBin = substr($rdata, $offset, $saltLen); |
170
|
1 |
|
$this->setSalt(bin2hex($saltBin)); |
171
|
1 |
|
$offset += $saltLen; |
172
|
1 |
|
} |
173
|
|
|
} |
174
|
|
|
|