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