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
|
|
|
* @param string $publicKey the public key in its raw (binary) representation |
75
|
|
|
*/ |
76
|
9 |
|
public function setPublicKey(string $publicKey): void |
77
|
|
|
{ |
78
|
9 |
|
$this->publicKey = $publicKey; |
79
|
9 |
|
} |
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
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
5 |
|
public function toText(): string |
105
|
|
|
{ |
106
|
5 |
|
return sprintf('%d %d %d %s', $this->flags, $this->protocol, $this->algorithm, base64_encode($this->publicKey)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritdoc} |
111
|
|
|
*/ |
112
|
2 |
|
public function toWire(): string |
113
|
|
|
{ |
114
|
2 |
|
return pack('nCC', $this->flags, $this->protocol, $this->algorithm).$this->publicKey; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
*/ |
120
|
2 |
|
public function fromText(string $text): void |
121
|
|
|
{ |
122
|
2 |
|
$rdata = explode(Tokens::SPACE, $text); |
123
|
2 |
|
$this->setFlags((int) array_shift($rdata)); |
124
|
2 |
|
$this->setProtocol((int) array_shift($rdata)); |
125
|
2 |
|
$this->setAlgorithm((int) array_shift($rdata)); |
126
|
2 |
|
$this->setPublicKey(base64_decode(implode('', $rdata))); |
127
|
2 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* {@inheritdoc} |
131
|
|
|
*/ |
132
|
2 |
|
public function fromWire(string $rdata, int &$offset = 0, ?int $rdLength = null): void |
133
|
|
|
{ |
134
|
2 |
|
$rdLength = $rdLength ?? strlen($rdata); |
135
|
2 |
|
$integers = unpack('nflags/Cprotocol/Calgorithm', $rdata, $offset); |
136
|
2 |
|
$offset += 4; |
137
|
2 |
|
$this->setFlags((int) $integers['flags']); |
138
|
2 |
|
$this->setProtocol((int) $integers['protocol']); |
139
|
2 |
|
$this->setAlgorithm((int) $integers['algorithm']); |
140
|
2 |
|
$this->setPublicKey(substr($rdata, $offset, $rdLength - 4)); |
141
|
2 |
|
$offset += $rdLength - 4; |
142
|
2 |
|
} |
143
|
|
|
} |
144
|
|
|
|