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
|
1 |
|
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
|
1 |
|
public function getHashAlgorithm(): int |
50
|
|
|
{ |
51
|
1 |
|
return $this->hashAlgorithm; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @throws \InvalidArgumentException |
56
|
|
|
*/ |
57
|
6 |
|
public function setHashAlgorithm(int $hashAlgorithm): void |
58
|
|
|
{ |
59
|
6 |
|
if (!Validator::isUnsignedInteger($hashAlgorithm, 8)) { |
60
|
|
|
throw new \InvalidArgumentException('Hash algorithm must be 8-bit integer.'); |
61
|
|
|
} |
62
|
6 |
|
$this->hashAlgorithm = $hashAlgorithm; |
63
|
6 |
|
} |
64
|
|
|
|
65
|
1 |
|
public function getFlags(): int |
66
|
|
|
{ |
67
|
1 |
|
return $this->flags; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @throws \InvalidArgumentException |
72
|
|
|
*/ |
73
|
6 |
|
public function setFlags(int $flags): void |
74
|
|
|
{ |
75
|
6 |
|
if (!Validator::isUnsignedInteger($flags, 8)) { |
76
|
|
|
throw new \InvalidArgumentException('Flags must be an 8-bit unsigned integer.'); |
77
|
|
|
} |
78
|
6 |
|
$this->flags = $flags; |
79
|
6 |
|
} |
80
|
|
|
|
81
|
1 |
|
public function getIterations(): int |
82
|
|
|
{ |
83
|
1 |
|
return $this->iterations; |
84
|
|
|
} |
85
|
|
|
|
86
|
6 |
|
public function setIterations(int $iterations): void |
87
|
|
|
{ |
88
|
6 |
|
if (!Validator::isUnsignedInteger($iterations, 16)) { |
89
|
|
|
throw new \InvalidArgumentException('Hash algorithm must be 16-bit integer.'); |
90
|
|
|
} |
91
|
6 |
|
$this->iterations = $iterations; |
92
|
6 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return string Base16 string |
96
|
|
|
*/ |
97
|
1 |
|
public function getSalt(): string |
98
|
|
|
{ |
99
|
1 |
|
return bin2hex($this->salt); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $salt Hexadecimal string |
104
|
|
|
*/ |
105
|
6 |
|
public function setSalt(string $salt): void |
106
|
|
|
{ |
107
|
6 |
|
if (false === $bin = @hex2bin($salt)) { |
108
|
|
|
throw new \InvalidArgumentException('Salt must be a hexadecimal string.'); |
109
|
|
|
} |
110
|
6 |
|
$this->salt = $bin; |
111
|
6 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritdoc} |
115
|
|
|
*/ |
116
|
2 |
|
public function toText(): string |
117
|
|
|
{ |
118
|
2 |
|
return sprintf('%d %d %d %s', $this->hashAlgorithm, $this->flags, $this->iterations, bin2hex($this->salt)); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
*/ |
124
|
1 |
|
public function toWire(): string |
125
|
|
|
{ |
126
|
1 |
|
return pack('CCnC', $this->hashAlgorithm, $this->flags, $this->iterations, strlen($this->salt)).$this->salt; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
1 |
|
public function fromText(string $text): void |
133
|
|
|
{ |
134
|
1 |
|
$rdata = explode(Tokens::SPACE, $text); |
135
|
1 |
|
$this->setHashAlgorithm((int) array_shift($rdata)); |
136
|
1 |
|
$this->setFlags((int) array_shift($rdata)); |
137
|
1 |
|
$this->setIterations((int) array_shift($rdata)); |
138
|
1 |
|
$this->setSalt((string) array_shift($rdata)); |
139
|
1 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
1 |
|
public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void |
145
|
|
|
{ |
146
|
1 |
|
$integers = unpack('C<algorithm>/C<flags>/n<iterations>/C<saltLen>', $rdata, $offset); |
147
|
1 |
|
$saltLen = (int) $integers['<saltLen>']; |
148
|
1 |
|
$offset += 5; |
149
|
1 |
|
$this->setHashAlgorithm($integers['<algorithm>']); |
150
|
1 |
|
$this->setFlags($integers['<flags>']); |
151
|
1 |
|
$this->setIterations($integers['<iterations>']); |
152
|
|
|
|
153
|
1 |
|
$saltBin = substr($rdata, $offset, $saltLen); |
154
|
1 |
|
$this->setSalt(bin2hex($saltBin)); |
155
|
1 |
|
$offset += $saltLen; |
156
|
1 |
|
} |
157
|
|
|
} |
158
|
|
|
|