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/rfc2535#section-3.1}. |
21
|
|
|
*/ |
22
|
|
|
class KEY implements RdataInterface |
23
|
|
|
{ |
24
|
|
|
use RdataTrait; |
25
|
|
|
|
26
|
|
|
const TYPE = 'KEY'; |
27
|
|
|
const TYPE_CODE = 25; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@link https://tools.ietf.org/html/rfc4034#section-2.1.1}. |
31
|
|
|
* |
32
|
|
|
* @var int |
33
|
|
|
*/ |
34
|
|
|
protected $flags; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var int |
38
|
|
|
*/ |
39
|
|
|
protected $protocol; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* The Algorithm field identifies the public key's cryptographic |
43
|
|
|
* algorithm and determines the format of the Public Key field. |
44
|
|
|
* {@link https://tools.ietf.org/html/rfc4034#section-2.1.3}. |
45
|
|
|
* |
46
|
|
|
* @var int |
47
|
|
|
*/ |
48
|
|
|
protected $algorithm; |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* The Public Key field is a Base64 encoding of the Public Key. |
52
|
|
|
* Whitespace is allowed within the Base64 text. |
53
|
|
|
* {@link https://tools.ietf.org/html/rfc4034#section-2.1.4}. |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $publicKey; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param int $flags |
61
|
|
|
*/ |
62
|
4 |
|
public function setFlags(int $flags): void |
63
|
|
|
{ |
64
|
4 |
|
$this->flags = $flags; |
65
|
4 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param int $protocol |
69
|
|
|
*/ |
70
|
2 |
|
public function setProtocol(int $protocol): void |
71
|
|
|
{ |
72
|
2 |
|
$this->protocol = $protocol; |
73
|
2 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param int $algorithm |
77
|
|
|
*/ |
78
|
4 |
|
public function setAlgorithm(int $algorithm): void |
79
|
|
|
{ |
80
|
4 |
|
$this->algorithm = $algorithm; |
81
|
4 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $publicKey |
85
|
|
|
* |
86
|
|
|
* @throws \InvalidArgumentException |
87
|
|
|
*/ |
88
|
4 |
|
public function setPublicKey(string $publicKey): void |
89
|
|
|
{ |
90
|
4 |
|
if (!Validator::isBase64Encoded($publicKey)) { |
91
|
|
|
throw new \InvalidArgumentException('The public key must be a valid base64 encoded string.'); |
92
|
|
|
} |
93
|
|
|
|
94
|
4 |
|
$this->publicKey = (string) preg_replace('/[^a-zA-Z0-9\/+=]/', '', $publicKey); |
95
|
4 |
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return int |
99
|
|
|
*/ |
100
|
1 |
|
public function getFlags(): int |
101
|
|
|
{ |
102
|
1 |
|
return $this->flags; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return int |
107
|
|
|
*/ |
108
|
1 |
|
public function getProtocol(): int |
109
|
|
|
{ |
110
|
1 |
|
return $this->protocol; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return int |
115
|
|
|
*/ |
116
|
1 |
|
public function getAlgorithm(): int |
117
|
|
|
{ |
118
|
1 |
|
return $this->algorithm; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
1 |
|
public function getPublicKey(): string |
125
|
|
|
{ |
126
|
1 |
|
return $this->publicKey; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
2 |
|
public function toText(): string |
133
|
|
|
{ |
134
|
2 |
|
return sprintf('%d %d %d %s', $this->flags, $this->protocol, $this->algorithm, $this->publicKey); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* {@inheritdoc} |
139
|
|
|
*/ |
140
|
1 |
|
public function toWire(): string |
141
|
|
|
{ |
142
|
1 |
|
$encoded = pack('nCC', $this->flags, $this->protocol, $this->algorithm); |
143
|
1 |
|
$encoded .= $this->publicKey; |
144
|
|
|
|
145
|
1 |
|
return $encoded; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* {@inheritdoc} |
150
|
|
|
*/ |
151
|
1 |
|
public static function fromText(string $text): RdataInterface |
152
|
|
|
{ |
153
|
1 |
|
$rdata = explode(Tokens::SPACE, $text); |
154
|
1 |
|
$key = new static(); |
155
|
1 |
|
$key->setFlags((int) array_shift($rdata)); |
156
|
1 |
|
$key->setProtocol((int) array_shift($rdata)); |
157
|
1 |
|
$key->setAlgorithm((int) array_shift($rdata)); |
158
|
1 |
|
$key->setPublicKey(implode('', $rdata)); |
159
|
|
|
|
160
|
1 |
|
return $key; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* {@inheritdoc} |
165
|
|
|
*/ |
166
|
1 |
|
public static function fromWire(string $rdata): RdataInterface |
167
|
|
|
{ |
168
|
1 |
|
$integers = unpack('nflags/Cprotocol/Calgorithm', $rdata); |
169
|
1 |
|
$key = new static(); |
170
|
1 |
|
$key->setFlags((int) $integers['flags']); |
171
|
1 |
|
$key->setProtocol((int) $integers['protocol']); |
172
|
1 |
|
$key->setAlgorithm((int) $integers['algorithm']); |
173
|
1 |
|
$key->setPublicKey(substr($rdata, 4)); |
174
|
|
|
|
175
|
1 |
|
return $key; |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|