This repository was archived by the owner on Jan 13, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathVerifier.php
More file actions
325 lines (299 loc) · 12.4 KB
/
Verifier.php
File metadata and controls
325 lines (299 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
<?php
namespace Drupal\Signify;
class Verifier
{
const COMMENTHDR = 'untrusted comment: ';
const COMMENTHDRLEN = 19;
const COMMENTMAXLEN = 1024;
/**
* @var string
*/
protected $publicKeyRaw;
/**
* @var VerifierB64Data
*/
protected $publicKey;
/**
* @var string $now
*/
protected $now;
/**
* Verifier constructor.
*
* @param string $public_key
* A public key generated by the BSD signify application.
*/
function __construct($public_key_raw) {
$this->publicKeyRaw = $public_key_raw;
}
/**
* Get the raw public key in use.
*
* @return string
* The public key.
*/
public function getPublicKeyRaw(){
return $this->publicKeyRaw;
}
/**
* Get the public key data.
*
* @return \Drupal\Signify\VerifierB64Data
* An object with the validated and decoded public key data.
*
* @throws \Drupal\Signify\VerifierException
*/
public function getPublicKey() {
if (!$this->publicKey) {
$this->publicKey = $this->parseB64String($this->publicKeyRaw, SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES);
}
return $this->publicKey;
}
/**
* Gets a \DateTime object modeling "now".
*
* @return \DateTime
* An object representing the current date in UTC for checking expiration.
*/
public function getNow(\DateTime $now = NULL)
{
$date_format = 'Y-m-d';
if (empty($now)) {
$now = gmdate($date_format);
}
else {
$now = $now->format($date_format);
}
$now_dt = \DateTime::createFromFormat($date_format, $now, new \DateTimeZone('UTC'));
if (!$now_dt instanceof \DateTime) {
throw new VerifierException('Unexpected date format of current date.');
}
return $now_dt;
}
/**
* Parse the contents of a base 64 encoded file.
*
* @param string $b64
* The file contents.
* @param int $length
* The length of the data, either 32 or 64 bytes.
*
* @return \Drupal\Signify\VerifierB64Data
* An object with the validated and decoded data.
*
* @throws \Drupal\Signify\VerifierException
*/
public function parseB64String($b64, $length) {
$parts = explode("\n", $b64);
if (count($parts) !== 3) {
throw new VerifierException("Invalid format; must contain two newlines, one after comment and one after base64");
}
$comment = $parts[0];
if (substr($comment, 0, self::COMMENTHDRLEN) !== self::COMMENTHDR) {
throw new VerifierException(sprintf("Invalid format; comment must start with '%s'", self::COMMENTHDR));
}
if (strlen($comment) > self::COMMENTHDRLEN + self::COMMENTMAXLEN) {
throw new VerifierException(sprintf("Invalid format; comment longer than %d bytes", self::COMMENTMAXLEN));
}
return new VerifierB64Data($parts[1], $length);
}
/**
* Verify a string message signed with plain Signify format.
*
* @param string $signed_message
* The string contents of the signify signature and message (e.g. the contents of a .sig file.)
*
* @return string
* The message if the verification passed.
*
* @throws \SodiumException
* Thrown when there is an unexpected crypto error or missing library.
* @throws \Drupal\Signify\VerifierException
* Thrown when the message was not verified by the signature.
*/
public function verifyMessage($signed_message) {
$pubkey = $this->getPublicKey();
// Simple split of signify signature and embedded message; input
// validation occurs in parseB64String().
$embedded_message_index = 0;
for($i = 1; $i <= 2 && $embedded_message_index !== false; $i++) {
$embedded_message_index = strpos($signed_message, "\n", $embedded_message_index + 1);
}
$signature = substr($signed_message, 0, $embedded_message_index + 1);
$message = substr($signed_message, $embedded_message_index + 1);
if ($message === false) {
$message = '';
}
$sig = $this->parseB64String($signature, SODIUM_CRYPTO_SIGN_BYTES);
if ($pubkey->keyNum !== $sig->keyNum) {
throw new VerifierException('verification failed: checked against wrong key');
}
$valid = sodium_crypto_sign_verify_detached($sig->data, $message, $pubkey->data);
if (!$valid) {
throw new VerifierException('Signature did not match');
}
return $message;
}
/**
* Verify a signed checksum list, and then verify the checksum for each file in the list.
*
* @param string $signed_checksum_list
* Contents of a signify signature file whose message is a file checksum list.
* @param string $working_directory
* A directory on the filesystem that the file checksum list is relative to.
*
* @return int
* The number of files verified.
*
* @throws \SodiumException
* @throws \Drupal\Signify\VerifierException
* Thrown when the checksum list could not be verified by the signature, or a listed file could not be verified.
*/
public function verifyChecksumList($signed_checksum_list, $working_directory)
{
$checksum_list_raw = $this->verifyMessage($signed_checksum_list);
return $this->verifyTrustedChecksumList($checksum_list_raw, $working_directory);
}
protected function verifyTrustedChecksumList($checksum_list_raw, $working_directory) {
$checksum_list = new ChecksumList($checksum_list_raw, true);
$failed_checksum_list = new FailedCheckumFilter($checksum_list, $working_directory);
foreach ($failed_checksum_list as $file_checksum)
{
// Don't just rely on a list of failed checksums, throw a more
// specific exception.
$actual_hash = @hash_file(strtolower($file_checksum->algorithm), $working_directory . DIRECTORY_SEPARATOR . $file_checksum->filename);
// If file doesn't exist or isn't readable, hash_file returns false.
if ($actual_hash === false) {
throw new VerifierException("File \"$file_checksum->filename\" in the checksum list could not be read.");
}
// Any hash less than 64 is not secure.
if (empty($actual_hash) || strlen($actual_hash) < 64) {
throw new VerifierException("Failure computing hash for file \"$file_checksum->filename\" in the checksum list.");
}
// This method is used because hash_equals was added in PHP 5.6.
// And we don't need timing safe comparisons.
if ($actual_hash !== $file_checksum->hex_hash)
{
throw new VerifierException("File \"$file_checksum->filename\" does not pass checksum verification.");
}
}
return $checksum_list->count();
}
/**
* Verify the a signed checksum list file, and then verify the checksum for each file in the list.
*
* @param string $checksum_file
* The filename of a signed checksum list file.
* @return int
* The number of files that were successfully verified.
* @throws \SodiumException
* @throws \Drupal\Signify\VerifierException
* Thrown when the checksum list could not be verified by the signature, or a listed file could not be verified.
*/
public function verifyChecksumFile($checksum_file) {
$absolute_path = realpath($checksum_file);
if (empty($absolute_path))
{
throw new VerifierException("The real path of checksum list file at \"$checksum_file\" could not be determined.");
}
$working_directory = dirname($absolute_path);
if (is_dir($absolute_path)) {
throw new VerifierException("The checksum list file at \"$checksum_file\" is a directory, not a file.");
}
$signed_checksum_list = @file_get_contents($absolute_path);
if (empty($signed_checksum_list))
{
throw new VerifierException("The checksum list file at \"$checksum_file\" could not be read.");
}
return $this->verifyChecksumList($signed_checksum_list, $working_directory);
}
/**
* Verify a string message signed with CSIG chained-signature extended Signify format.
*
* @param string $chained_signed_message
* The string contents of the root/intermediate chained signify signature and message (e.g. the contents of a .csig file.)
* @param \DateTime $now
* If provided, a \DateTime object modeling "now".
*
* @return string
* The message if the verification passed.
* @throws \SodiumException
* @throws \Drupal\Signify\VerifierException
* Thrown when the message was not verified.
*/
public function verifyCsigMessage($chained_signed_message, \DateTime $now = NULL)
{
$csig_lines = explode("\n", $chained_signed_message, 6);
$root_signed_intermediate_key_and_validity = implode("\n", array_slice($csig_lines, 0, 5)) . "\n";
$this->verifyMessage($root_signed_intermediate_key_and_validity);
$valid_through_dt = \DateTime::createFromFormat('Y-m-d', $csig_lines[2], new \DateTimeZone('UTC'));
if (! $valid_through_dt instanceof \DateTime)
{
throw new VerifierException('Unexpected valid-through date format.');
}
$now_dt = $this->getNow($now);
$diff = $now_dt->diff($valid_through_dt);
if ($diff->invert) {
throw new VerifierException(sprintf('The intermediate key expired %d day(s) ago.', $diff->days));
}
$intermediate_pubkey = implode("\n", array_slice($csig_lines, 3, 2)) . "\n";
$chained_verifier = new self($intermediate_pubkey);
$signed_message = implode("\n", array_slice($csig_lines, 5));
return $chained_verifier->verifyMessage($signed_message);
}
/**
* Verify a signed checksum list, and then verify the checksum for each file in the list.
*
* @param string $csig_signed_checksum_list
* Contents of a CSIG signature file whose message is a file checksum list.
* @param string $working_directory
* A directory on the filesystem that the file checksum list is relative to.
* @param \DateTime $now
* If provided, a \DateTime object modeling "now".
*
* @return int
* The number of files verified.
*
* @throws \SodiumException
* @throws \Drupal\Signify\VerifierException
* Thrown when the checksum list could not be verified by the signature, or a listed file could not be verified.
*/
public function verifyCsigChecksumList($csig_signed_checksum_list, $working_directory, \DateTime $now = NULL)
{
$checksum_list_raw = $this->verifyCsigMessage($csig_signed_checksum_list, $now);
return $this->verifyTrustedChecksumList($checksum_list_raw, $working_directory);
}
/**
* Verify the a signed checksum list file, and then verify the checksum for each file in the list.
*
* @param string $checksum_file
* The filename of a .csig signed checksum list file.
* @param \DateTime $now
* If provided, a \DateTime object modeling "now".
*
* @return int
* The number of files that were successfully verified.
*
* @throws \SodiumException
* @throws \Drupal\Signify\VerifierException
* Thrown when the checksum list could not be verified by the signature, or a listed file could not be verified.
*/
public function verifyCsigChecksumFile($csig_checksum_file, \DateTime $now = NULL)
{
$absolute_path = realpath($csig_checksum_file);
if (empty($absolute_path))
{
throw new VerifierException("The real path of checksum list file at \"$csig_checksum_file\" could not be determined.");
}
$working_directory = dirname($absolute_path);
if (is_dir($absolute_path)) {
throw new VerifierException("The checksum list file at \"$csig_checksum_file\" is a directory, not a file.");
}
$signed_checksum_list = file_get_contents($absolute_path);
if (empty($signed_checksum_list))
{
throw new VerifierException("The checksum list file at \"$csig_checksum_file\" could not be read.");
}
return $this->verifyCsigChecksumList($signed_checksum_list, $working_directory, $now);
}
}