|
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/rfc4255}. |
|
21
|
|
|
*/ |
|
22
|
|
|
class SSHFP implements RdataInterface |
|
23
|
|
|
{ |
|
24
|
|
|
use RdataTrait; |
|
25
|
|
|
|
|
26
|
|
|
const TYPE = 'SSHFP'; |
|
27
|
|
|
const TYPE_CODE = 44; |
|
28
|
|
|
const ALGORITHM_RSA = 1; |
|
29
|
|
|
const ALGORITHM_DSA = 2; |
|
30
|
|
|
const FP_TYPE_SHA1 = 1; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* 8-bit algorithm designate. |
|
34
|
|
|
* |
|
35
|
|
|
* @var int |
|
36
|
|
|
*/ |
|
37
|
|
|
private $algorithm; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* 8-bit Fingerprint type. |
|
41
|
|
|
* |
|
42
|
|
|
* @var int |
|
43
|
|
|
*/ |
|
44
|
|
|
private $fingerprintType = self::FP_TYPE_SHA1; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Hexadecimal string. |
|
48
|
|
|
* |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
private $fingerprint; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @return int |
|
55
|
|
|
*/ |
|
56
|
2 |
|
public function getAlgorithm(): int |
|
57
|
|
|
{ |
|
58
|
2 |
|
return $this->algorithm; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param int $algorithm |
|
63
|
|
|
* |
|
64
|
|
|
* @throws \InvalidArgumentException |
|
65
|
|
|
*/ |
|
66
|
11 |
|
public function setAlgorithm(int $algorithm): void |
|
67
|
|
|
{ |
|
68
|
11 |
|
if (!Validator::isUnsignedInteger($algorithm, 8)) { |
|
69
|
2 |
|
throw new \InvalidArgumentException('Algorithm must be an 8-bit integer between 0 and 255.'); |
|
70
|
|
|
} |
|
71
|
9 |
|
$this->algorithm = $algorithm; |
|
72
|
9 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return int |
|
76
|
|
|
*/ |
|
77
|
2 |
|
public function getFingerprintType(): int |
|
78
|
|
|
{ |
|
79
|
2 |
|
return $this->fingerprintType; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param int $fingerprintType |
|
84
|
|
|
* |
|
85
|
|
|
* @throws \InvalidArgumentException |
|
86
|
|
|
*/ |
|
87
|
9 |
|
public function setFingerprintType(int $fingerprintType): void |
|
88
|
|
|
{ |
|
89
|
9 |
|
if (!Validator::isUnsignedInteger($fingerprintType, 8)) { |
|
90
|
2 |
|
throw new \InvalidArgumentException('Fingerprint type must be an 8-bit integer between 0 and 255.'); |
|
91
|
|
|
} |
|
92
|
7 |
|
$this->fingerprintType = $fingerprintType; |
|
93
|
7 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return string |
|
97
|
|
|
*/ |
|
98
|
2 |
|
public function getFingerprint(): string |
|
99
|
|
|
{ |
|
100
|
2 |
|
return bin2hex($this->fingerprint); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param string $fingerprint |
|
105
|
|
|
*/ |
|
106
|
7 |
|
public function setFingerprint(string $fingerprint): void |
|
107
|
|
|
{ |
|
108
|
7 |
|
if (!Validator::isBase16Encoded($fingerprint) || false === $fp = @hex2bin($fingerprint)) { |
|
109
|
1 |
|
throw new \InvalidArgumentException('The fingerprint MUST be a hexadecimal value.'); |
|
110
|
|
|
} |
|
111
|
6 |
|
$this->fingerprint = $fp; |
|
112
|
6 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* {@inheritdoc} |
|
116
|
|
|
*/ |
|
117
|
2 |
|
public function toText(): string |
|
118
|
|
|
{ |
|
119
|
2 |
|
return sprintf('%d %d %s', $this->algorithm, $this->fingerprintType, bin2hex($this->fingerprint)); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* {@inheritdoc} |
|
124
|
|
|
*/ |
|
125
|
1 |
|
public function toWire(): string |
|
126
|
|
|
{ |
|
127
|
1 |
|
return pack('CC', $this->algorithm, $this->fingerprintType).$this->fingerprint; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
* |
|
133
|
|
|
* @return SSHFP |
|
134
|
|
|
*/ |
|
135
|
2 |
|
public static function fromText(string $text): RdataInterface |
|
136
|
|
|
{ |
|
137
|
2 |
|
$rdata = explode(Tokens::SPACE, $text); |
|
138
|
|
|
|
|
139
|
2 |
|
return Factory::SSHFP((int) array_shift($rdata), (int) array_shift($rdata), (string) array_shift($rdata)); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* {@inheritdoc} |
|
144
|
|
|
* |
|
145
|
|
|
* @return SSHFP |
|
146
|
|
|
*/ |
|
147
|
1 |
|
public static function fromWire(string $rdata): RdataInterface |
|
148
|
|
|
{ |
|
149
|
1 |
|
$integers = unpack('C<algorithm>/C<fpType>', $rdata); |
|
150
|
1 |
|
$sshfp = new self(); |
|
151
|
1 |
|
$sshfp->setAlgorithm($integers['<algorithm>']); |
|
152
|
1 |
|
$sshfp->setFingerprintType($integers['<fpType>']); |
|
153
|
1 |
|
$sshfp->setFingerprint(bin2hex(substr($rdata, 2))); |
|
154
|
|
|
|
|
155
|
1 |
|
return $sshfp; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|