|
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
|
10 |
|
public function setFlags(int $flags): void |
|
59
|
|
|
{ |
|
60
|
10 |
|
$this->flags = $flags; |
|
61
|
10 |
|
} |
|
62
|
|
|
|
|
63
|
6 |
|
public function setProtocol(int $protocol): void |
|
64
|
|
|
{ |
|
65
|
6 |
|
$this->protocol = $protocol; |
|
66
|
6 |
|
} |
|
67
|
|
|
|
|
68
|
10 |
|
public function setAlgorithm(int $algorithm): void |
|
69
|
|
|
{ |
|
70
|
10 |
|
$this->algorithm = $algorithm; |
|
71
|
10 |
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param string $publicKey the public key in its raw (binary) representation |
|
75
|
|
|
*/ |
|
76
|
10 |
|
public function setPublicKey(string $publicKey): void |
|
77
|
|
|
{ |
|
78
|
10 |
|
$this->publicKey = $publicKey; |
|
79
|
10 |
|
} |
|
80
|
|
|
|
|
81
|
3 |
|
public function getFlags(): int |
|
82
|
|
|
{ |
|
83
|
3 |
|
return $this->flags; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
3 |
|
public function getProtocol(): int |
|
87
|
|
|
{ |
|
88
|
3 |
|
return $this->protocol; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
3 |
|
public function getAlgorithm(): int |
|
92
|
|
|
{ |
|
93
|
3 |
|
return $this->algorithm; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
3 |
|
public function getPublicKey(): string |
|
97
|
|
|
{ |
|
98
|
3 |
|
return $this->publicKey; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
5 |
|
public function toText(): string |
|
102
|
|
|
{ |
|
103
|
5 |
|
return sprintf('%d %d %d %s', $this->flags, $this->protocol, $this->algorithm, base64_encode($this->publicKey)); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
3 |
|
public function toWire(): string |
|
107
|
|
|
{ |
|
108
|
3 |
|
return pack('nCC', $this->flags, $this->protocol, $this->algorithm).$this->publicKey; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
2 |
|
public function fromText(string $text): void |
|
112
|
|
|
{ |
|
113
|
2 |
|
$rdata = explode(Tokens::SPACE, $text); |
|
114
|
2 |
|
$this->setFlags((int) array_shift($rdata)); |
|
115
|
2 |
|
$this->setProtocol((int) array_shift($rdata)); |
|
116
|
2 |
|
$this->setAlgorithm((int) array_shift($rdata)); |
|
117
|
2 |
|
$this->setPublicKey(base64_decode(implode('', $rdata))); |
|
118
|
2 |
|
} |
|
119
|
|
|
|
|
120
|
2 |
|
public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void |
|
121
|
|
|
{ |
|
122
|
2 |
|
$rdLength = $rdLength ?? strlen($rdata); |
|
123
|
2 |
|
if (false === $integers = unpack('nflags/Cprotocol/Calgorithm', $rdata, $offset)) { |
|
124
|
2 |
|
throw new DecodeException(static::TYPE, $rdata); |
|
125
|
2 |
|
} |
|
126
|
2 |
|
$offset += 4; |
|
127
|
2 |
|
$this->setFlags((int) $integers['flags']); |
|
128
|
2 |
|
$this->setProtocol((int) $integers['protocol']); |
|
129
|
2 |
|
$this->setAlgorithm((int) $integers['algorithm']); |
|
130
|
2 |
|
$this->setPublicKey(substr($rdata, $offset, $rdLength - 4)); |
|
131
|
|
|
$offset += $rdLength - 4; |
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|