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 InvalidArgumentException; |
18
|
|
|
|
19
|
|
|
class DS implements RdataInterface |
20
|
|
|
{ |
21
|
1 |
|
use RdataTrait; |
22
|
|
|
|
23
|
|
|
const TYPE = 'DS'; |
24
|
|
|
const TYPE_CODE = 43; |
25
|
|
|
const DIGEST_SHA1 = 1; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var int |
29
|
|
|
*/ |
30
|
|
|
private $keyTag; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The Algorithm field lists the algorithm number of the DNSKEY RR |
34
|
|
|
* referred to by the DS record. |
35
|
|
|
* {@link https://tools.ietf.org/html/rfc4034#section-5.1.2}. |
36
|
|
|
* |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
private $algorithm; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var int |
43
|
|
|
*/ |
44
|
|
|
private $digestType = self::DIGEST_SHA1; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var string |
48
|
|
|
*/ |
49
|
|
|
private $digest; |
50
|
|
|
|
51
|
4 |
|
public function getKeyTag(): int |
52
|
|
|
{ |
53
|
4 |
|
return $this->keyTag; |
54
|
|
|
} |
55
|
|
|
|
56
|
14 |
|
public function setKeyTag(int $keyTag): void |
57
|
|
|
{ |
58
|
14 |
|
$this->keyTag = $keyTag; |
59
|
14 |
|
} |
60
|
|
|
|
61
|
4 |
|
public function getAlgorithm(): int |
62
|
|
|
{ |
63
|
4 |
|
return $this->algorithm; |
64
|
|
|
} |
65
|
|
|
|
66
|
14 |
|
public function setAlgorithm(int $algorithm): void |
67
|
|
|
{ |
68
|
14 |
|
$this->algorithm = $algorithm; |
69
|
14 |
|
} |
70
|
|
|
|
71
|
4 |
|
public function getDigestType(): int |
72
|
|
|
{ |
73
|
4 |
|
return $this->digestType; |
74
|
|
|
} |
75
|
|
|
|
76
|
14 |
|
public function setDigestType(int $digestType): void |
77
|
|
|
{ |
78
|
14 |
|
$this->digestType = $digestType; |
79
|
14 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return string the digest in its binary representation |
83
|
|
|
*/ |
84
|
4 |
|
public function getDigest(): string |
85
|
|
|
{ |
86
|
4 |
|
return $this->digest; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param string $digest the digest in its binary representation |
91
|
|
|
*/ |
92
|
14 |
|
public function setDigest(string $digest): void |
93
|
|
|
{ |
94
|
14 |
|
$this->digest = $digest; |
95
|
14 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
2 |
|
public function toText(): string |
101
|
|
|
{ |
102
|
2 |
|
return sprintf( |
103
|
2 |
|
'%s %s %s %s', |
104
|
2 |
|
$this->keyTag, |
105
|
2 |
|
$this->algorithm, |
106
|
2 |
|
$this->digestType, |
107
|
2 |
|
strtoupper(bin2hex($this->digest)) |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
*/ |
114
|
4 |
|
public function toWire(): string |
115
|
|
|
{ |
116
|
4 |
|
return pack('nCC', $this->keyTag, $this->algorithm, $this->digestType).$this->digest; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
4 |
|
public function fromText(string $text): void |
123
|
|
|
{ |
124
|
4 |
|
$rdata = explode(Tokens::SPACE, $text); |
125
|
4 |
|
$this->setKeyTag((int) array_shift($rdata)); |
126
|
4 |
|
$this->setAlgorithm((int) array_shift($rdata)); |
127
|
4 |
|
$this->setDigestType((int) array_shift($rdata)); |
128
|
4 |
|
if (false === $digest = hex2bin((string) array_shift($rdata))) { |
129
|
|
|
throw new InvalidArgumentException(sprintf('The digest is not a valid hexadecimal string.')); |
130
|
|
|
} |
131
|
4 |
|
$this->setDigest($digest); |
132
|
4 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* {@inheritdoc} |
136
|
|
|
*/ |
137
|
4 |
|
public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void |
138
|
|
|
{ |
139
|
4 |
|
$digestLen = ($rdLength ?? strlen($rdata)) - 4; |
140
|
|
|
|
141
|
4 |
|
$integers = unpack('ntag/Calgorithm/Cdtype', $rdata, $offset); |
142
|
4 |
|
$offset += 4; |
143
|
|
|
|
144
|
4 |
|
$this->setKeyTag($integers['tag']); |
145
|
4 |
|
$this->setAlgorithm($integers['algorithm']); |
146
|
4 |
|
$this->setDigestType($integers['dtype']); |
147
|
4 |
|
$this->setDigest(substr($rdata, $offset, $digestLen)); |
148
|
|
|
|
149
|
4 |
|
$offset += $digestLen; |
150
|
4 |
|
} |
151
|
|
|
} |
152
|
|
|
|