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