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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
|
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OC\Security\Signature;
use NCU\Security\Signature\Exceptions\IdentityNotFoundException;
use NCU\Security\Signature\Exceptions\IncomingRequestException;
use NCU\Security\Signature\Exceptions\InvalidKeyOriginException;
use NCU\Security\Signature\Exceptions\InvalidSignatureException;
use NCU\Security\Signature\Exceptions\SignatoryConflictException;
use NCU\Security\Signature\Exceptions\SignatoryException;
use NCU\Security\Signature\Exceptions\SignatoryNotFoundException;
use NCU\Security\Signature\Exceptions\SignatureElementNotFoundException;
use NCU\Security\Signature\Exceptions\SignatureException;
use NCU\Security\Signature\Exceptions\SignatureNotFoundException;
use NCU\Security\Signature\ISignatoryManager;
use NCU\Security\Signature\ISignatureManager;
use NCU\Security\Signature\Model\IIncomingSignedRequest;
use NCU\Security\Signature\Model\IOutgoingSignedRequest;
use NCU\Security\Signature\Model\ISignatory;
use NCU\Security\Signature\Model\SignatoryType;
use NCU\Security\Signature\SignatureAlgorithm;
use OC\Security\Signature\Model\IncomingSignedRequest;
use OC\Security\Signature\Model\OutgoingSignedRequest;
use OC\Security\Signature\Model\Signatory;
use OCP\DB\Exception as DBException;
use OCP\IAppConfig;
use OCP\IDBConnection;
use OCP\IRequest;
use Psr\Log\LoggerInterface;
/**
* ISignatureManager is a service integrated to core that provide tools
* to set/get authenticity of/from outgoing/incoming request.
*
* Quick description of the signature, added to the headers
* {
* "(request-target)": "post /path",
* "content-length": 385,
* "date": "Mon, 08 Jul 2024 14:16:20 GMT",
* "digest": "SHA-256=U7gNVUQiixe5BRbp4Tg0xCZMTcSWXXUZI2\\/xtHM40S0=",
* "host": "hostname.of.the.recipient",
* "Signature": "keyId=\"https://author.hostname/key\",algorithm=\"sha256\",headers=\"content-length
* date digest host\",signature=\"DzN12OCS1rsA[...]o0VmxjQooRo6HHabg==\""
* }
*
* 'content-length' is the total length of the data/content
* 'date' is the datetime the request have been initiated
* 'digest' is a checksum of the data/content
* 'host' is the hostname of the recipient of the request (remote when signing outgoing request, local on
* incoming request)
* 'Signature' contains the signature generated using the private key, and metadata:
* - 'keyId' is a unique id, formatted as an url. hostname is used to retrieve the public key via custom
* discovery
* - 'algorithm' define the algorithm used to generate signature
* - 'headers' contains a list of element used during the generation of the signature
* - 'signature' is the encrypted string, using local private key, of an array containing elements
* listed in 'headers' and their value. Some elements (content-length date digest host) are mandatory
* to ensure authenticity override protection.
*
* @since 31.0.0
*/
class SignatureManager implements ISignatureManager {
public const DATE_HEADER = 'D, d M Y H:i:s T';
public const DATE_TTL = 300;
public const SIGNATORY_TTL = 86400 * 3;
public const TABLE_SIGNATORIES = 'sec_signatory';
public const BODY_MAXSIZE = 50000; // max size of the payload of the request
public const APPCONFIG_IDENTITY = 'security.signature.identity';
public function __construct(
private readonly IRequest $request,
private readonly IDBConnection $connection,
private readonly IAppConfig $appConfig,
private readonly LoggerInterface $logger,
) {
}
/**
* @inheritDoc
*
* @param ISignatoryManager $signatoryManager used to get details about remote instance
* @param string|null $body if NULL, body will be extracted from php://input
*
* @return IIncomingSignedRequest
* @throws IncomingRequestException if anything looks wrong with the incoming request
* @throws SignatureNotFoundException if incoming request is not signed
* @throws SignatureException if signature could not be confirmed
* @since 31.0.0
*/
public function getIncomingSignedRequest(
ISignatoryManager $signatoryManager,
?string $body = null,
): IIncomingSignedRequest {
$body = $body ?? file_get_contents('php://input');
$options = $signatoryManager->getOptions();
if (strlen($body) > ($options['bodyMaxSize'] ?? self::BODY_MAXSIZE)) {
throw new IncomingRequestException('content of request is too big');
}
// generate IncomingSignedRequest based on body and request
$signedRequest = new IncomingSignedRequest($body, $this->request, $options);
try {
// we set origin based on the keyId defined in the Signature header of the request
$signedRequest->setOrigin($this->extractIdentityFromUri($signedRequest->getSignatureElement('keyId')));
} catch (IdentityNotFoundException $e) {
throw new IncomingRequestException($e->getMessage());
}
try {
// confirm the validity of content and identity of the incoming request
$this->generateExpectedClearSignatureFromRequest($signedRequest, $options['extraSignatureHeaders'] ?? []);
$this->confirmIncomingRequestSignature($signedRequest, $signatoryManager, $options['ttlSignatory'] ?? self::SIGNATORY_TTL);
} catch (SignatureException $e) {
$this->logger->warning(
'signature could not be verified', [
'exception' => $e,
'signedRequest' => $signedRequest,
'signatoryManager' => get_class($signatoryManager)
]
);
throw $e;
}
return $signedRequest;
}
/**
* generating the expected signature (clear version) sent by the remote instance
* based on the data available in the Signature header.
*
* @param IIncomingSignedRequest $signedRequest
* @param array $extraSignatureHeaders
*
* @throws SignatureException
*/
private function generateExpectedClearSignatureFromRequest(
IIncomingSignedRequest $signedRequest,
array $extraSignatureHeaders = [],
): void {
$request = $signedRequest->getRequest();
$usedHeaders = explode(' ', $signedRequest->getSignatureElement('headers'));
$neededHeaders = array_merge(['date', 'host', 'content-length', 'digest'], array_keys($extraSignatureHeaders));
$missingHeaders = array_diff($neededHeaders, $usedHeaders);
if ($missingHeaders !== []) {
throw new SignatureException('missing entries in Signature.headers: ' . json_encode($missingHeaders));
}
$estimated = ['(request-target): ' . strtolower($request->getMethod()) . ' ' . $request->getRequestUri()];
foreach ($usedHeaders as $key) {
if ($key === '(request-target)') {
continue;
}
$value = (strtolower($key) === 'host') ? $request->getServerHost() : $request->getHeader($key);
if ($value === '') {
throw new SignatureException('missing header ' . $key . ' in request');
}
$estimated[] = $key . ': ' . $value;
}
$signedRequest->setClearSignature(implode("\n", $estimated));
}
/**
* confirm that the Signature is signed using the correct private key, using
* clear version of the Signature and the public key linked to the keyId
*
* @param IIncomingSignedRequest $signedRequest
* @param ISignatoryManager $signatoryManager
*
* @throws SignatoryNotFoundException
* @throws SignatureException
*/
private function confirmIncomingRequestSignature(
IIncomingSignedRequest $signedRequest,
ISignatoryManager $signatoryManager,
int $ttlSignatory,
): void {
$knownSignatory = null;
try {
$knownSignatory = $this->getStoredSignatory($signedRequest->getKeyId());
// refreshing ttl and compare with previous public key
if ($ttlSignatory > 0 && $knownSignatory->getLastUpdated() < (time() - $ttlSignatory)) {
$signatory = $this->getSaneRemoteSignatory($signatoryManager, $signedRequest);
$this->updateSignatoryMetadata($signatory);
$knownSignatory->setMetadata($signatory->getMetadata());
}
$signedRequest->setSignatory($knownSignatory);
$this->verifySignedRequest($signedRequest);
} catch (InvalidKeyOriginException $e) {
throw $e; // issue while requesting remote instance also means there is no 2nd try
} catch (SignatoryNotFoundException) {
// if no signatory in cache, we retrieve the one from the remote instance (using
// $signatoryManager), check its validity with current signature and store it
$signatory = $this->getSaneRemoteSignatory($signatoryManager, $signedRequest);
$signedRequest->setSignatory($signatory);
$this->verifySignedRequest($signedRequest);
$this->storeSignatory($signatory);
} catch (SignatureException) {
// if public key (from cache) is not valid, we try to refresh it (based on SignatoryType)
try {
$signatory = $this->getSaneRemoteSignatory($signatoryManager, $signedRequest);
} catch (SignatoryNotFoundException $e) {
$this->manageDeprecatedSignatory($knownSignatory);
throw $e;
}
$signedRequest->setSignatory($signatory);
$this->verifySignedRequest($signedRequest);
$this->storeSignatory($signatory);
}
}
/**
* @inheritDoc
*
* @param ISignatoryManager $signatoryManager
* @param string $content body to be signed
* @param string $method needed in the signature
* @param string $uri needed in the signature
*
* @return IOutgoingSignedRequest
* @throws IdentityNotFoundException
* @throws SignatoryException
* @throws SignatoryNotFoundException
* @since 31.0.0
*/
public function getOutgoingSignedRequest(
ISignatoryManager $signatoryManager,
string $content,
string $method,
string $uri,
): IOutgoingSignedRequest {
$signedRequest = new OutgoingSignedRequest(
$content,
$signatoryManager,
$this->extractIdentityFromUri($uri),
$method,
parse_url($uri, PHP_URL_PATH) ?? '/'
);
$this->signOutgoingRequest($signedRequest);
return $signedRequest;
}
/**
* signing clear version of the Signature header
*
* @param IOutgoingSignedRequest $signedRequest
*
* @throws SignatoryException
* @throws SignatoryNotFoundException
*/
private function signOutgoingRequest(IOutgoingSignedRequest $signedRequest): void {
$clear = $signedRequest->getClearSignature();
$signed = $this->signString($clear, $signedRequest->getSignatory()->getPrivateKey(), $signedRequest->getAlgorithm());
$signatory = $signedRequest->getSignatory();
$signatureElements = [
'keyId="' . $signatory->getKeyId() . '"',
'algorithm="' . $signedRequest->getAlgorithm()->value . '"',
'headers="' . implode(' ', $signedRequest->getHeaderList()) . '"',
'signature="' . $signed . '"'
];
$signedRequest->setSignedSignature($signed);
$signedRequest->addHeader('Signature', implode(',', $signatureElements));
}
/**
* @inheritDoc
*
* @param ISignatoryManager $signatoryManager
* @param array $payload original payload, will be used to sign and completed with new headers with
* signature elements
* @param string $method needed in the signature
* @param string $uri needed in the signature
*
* @return array new payload to be sent, including original payload and signature elements in headers
* @since 31.0.0
*/
public function signOutgoingRequestIClientPayload(
ISignatoryManager $signatoryManager,
array $payload,
string $method,
string $uri,
): array {
$signedRequest = $this->getOutgoingSignedRequest($signatoryManager, $payload['body'], $method, $uri);
$payload['headers'] = array_merge($payload['headers'], $signedRequest->getHeaders());
return $payload;
}
/**
* @inheritDoc
*
* @param string $host remote host
* @param string $account linked account, should be used when multiple signature can exist for the same
* host
*
* @return ISignatory
* @throws SignatoryNotFoundException if entry does not exist in local database
* @since 31.0.0
*/
public function searchSignatory(string $host, string $account = ''): ISignatory {
$qb = $this->connection->getQueryBuilder();
$qb->select(
'id', 'provider_id', 'host', 'account', 'key_id', 'key_id_sum', 'public_key', 'metadata', 'type',
'status', 'creation', 'last_updated'
);
$qb->from(self::TABLE_SIGNATORIES);
$qb->where($qb->expr()->eq('host', $qb->createNamedParameter($host)));
$qb->andWhere($qb->expr()->eq('account', $qb->createNamedParameter($account)));
$result = $qb->executeQuery();
$row = $result->fetch();
$result->closeCursor();
if (!$row) {
throw new SignatoryNotFoundException('no signatory found');
}
$signature = new Signatory($row['key_id'], $row['public_key']);
return $signature->importFromDatabase($row);
}
/**
* @inheritDoc
*
* keyId is set using app config 'core/security.signature.identity'
*
* @param string $path
*
* @return string
* @throws IdentityNotFoundException is identity is not set in app config
* @since 31.0.0
*/
public function generateKeyIdFromConfig(string $path): string {
if (!$this->appConfig->hasKey('core', self::APPCONFIG_IDENTITY, true)) {
throw new IdentityNotFoundException(self::APPCONFIG_IDENTITY . ' not set');
}
$identity = trim($this->appConfig->getValueString('core', self::APPCONFIG_IDENTITY, lazy: true), '/');
return 'https://' . $identity . '/' . ltrim($path, '/');
}
/**
* @inheritDoc
*
* @param string $uri
*
* @return string
* @throws IdentityNotFoundException if identity cannot be extracted
* @since 31.0.0
*/
public function extractIdentityFromUri(string $uri): string {
$identity = parse_url($uri, PHP_URL_HOST);
$port = parse_url($uri, PHP_URL_PORT);
if ($identity === null || $identity === false) {
throw new IdentityNotFoundException('cannot extract identity from ' . $uri);
}
if ($port !== null && $port !== false) {
$identity .= ':' . $port;
}
return $identity;
}
/**
* get remote signatory using the ISignatoryManager
* and confirm the validity of the keyId
*
* @param ISignatoryManager $signatoryManager
* @param IIncomingSignedRequest $signedRequest
*
* @return ISignatory
* @throws InvalidKeyOriginException
* @throws SignatoryNotFoundException
* @see ISignatoryManager::getRemoteSignatory
*/
private function getSaneRemoteSignatory(
ISignatoryManager $signatoryManager,
IIncomingSignedRequest $signedRequest,
): ISignatory {
$signatory = $signatoryManager->getRemoteSignatory($signedRequest->getOrigin());
if ($signatory === null) {
throw new SignatoryNotFoundException('empty result from getRemoteSignatory');
}
try {
if ($signatory->getKeyId() !== $signedRequest->getKeyId()) {
throw new InvalidKeyOriginException('keyId from signatory not related to the one from request');
}
} catch (SignatureElementNotFoundException) {
throw new InvalidKeyOriginException('missing keyId');
}
return $signatory->setProviderId($signatoryManager->getProviderId());
}
/**
* @param IIncomingSignedRequest $signedRequest
*
* @return void
* @throws SignatureException
* @throws SignatoryNotFoundException
*/
private function verifySignedRequest(IIncomingSignedRequest $signedRequest): void {
$publicKey = $signedRequest->getSignatory()->getPublicKey();
if ($publicKey === '') {
throw new SignatoryNotFoundException('empty public key');
}
try {
$this->verifyString(
$signedRequest->getClearSignature(),
$signedRequest->getSignedSignature(),
$publicKey,
SignatureAlgorithm::tryFrom($signedRequest->getSignatureElement('algorithm')) ?? SignatureAlgorithm::SHA256
);
} catch (InvalidSignatureException $e) {
$this->logger->debug('signature issue', ['signed' => $signedRequest, 'exception' => $e]);
throw $e;
}
}
/**
* @param string $clear
* @param string $privateKey
* @param SignatureAlgorithm $algorithm
*
* @return string
* @throws SignatoryException
*/
private function signString(string $clear, string $privateKey, SignatureAlgorithm $algorithm): string {
if ($privateKey === '') {
throw new SignatoryException('empty private key');
}
openssl_sign($clear, $signed, $privateKey, $algorithm->value);
return base64_encode($signed);
}
/**
* @param string $clear
* @param string $encoded
* @param string $publicKey
* @param SignatureAlgorithm $algorithm
*
* @throws InvalidSignatureException
*/
private function verifyString(
string $clear,
string $encoded,
string $publicKey,
SignatureAlgorithm $algorithm = SignatureAlgorithm::SHA256,
): void {
$signed = base64_decode($encoded);
if (openssl_verify($clear, $signed, $publicKey, $algorithm->value) !== 1) {
throw new InvalidSignatureException('signature issue');
}
}
/**
* @param string $keyId
*
* @return ISignatory
* @throws SignatoryNotFoundException
*/
private function getStoredSignatory(string $keyId): ISignatory {
$qb = $this->connection->getQueryBuilder();
$qb->select(
'id', 'provider_id', 'host', 'account', 'key_id', 'key_id_sum', 'public_key', 'metadata', 'type',
'status', 'creation', 'last_updated'
);
$qb->from(self::TABLE_SIGNATORIES);
$qb->where($qb->expr()->eq('key_id_sum', $qb->createNamedParameter($this->hashKeyId($keyId))));
$result = $qb->executeQuery();
$row = $result->fetch();
$result->closeCursor();
if (!$row) {
throw new SignatoryNotFoundException('no signatory found in local');
}
$signature = new Signatory($row['key_id'], $row['public_key']);
$signature->importFromDatabase($row);
return $signature;
}
/**
* @param ISignatory $signatory
*/
private function storeSignatory(ISignatory $signatory): void {
try {
$this->insertSignatory($signatory);
} catch (DBException $e) {
if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) {
$this->logger->warning('exception while storing signature', ['exception' => $e]);
throw $e;
}
try {
$this->updateKnownSignatory($signatory);
} catch (SignatoryNotFoundException $e) {
$this->logger->warning('strange behavior, signatory not found ?', ['exception' => $e]);
}
}
}
/**
* @param ISignatory $signatory
* @throws DBException
*/
private function insertSignatory(ISignatory $signatory): void {
$qb = $this->connection->getQueryBuilder();
$qb->insert(self::TABLE_SIGNATORIES)
->setValue('provider_id', $qb->createNamedParameter($signatory->getProviderId()))
->setValue('host', $qb->createNamedParameter($this->extractIdentityFromUri($signatory->getKeyId())))
->setValue('account', $qb->createNamedParameter($signatory->getAccount()))
->setValue('key_id', $qb->createNamedParameter($signatory->getKeyId()))
->setValue('key_id_sum', $qb->createNamedParameter($this->hashKeyId($signatory->getKeyId())))
->setValue('public_key', $qb->createNamedParameter($signatory->getPublicKey()))
->setValue('metadata', $qb->createNamedParameter(json_encode($signatory->getMetadata())))
->setValue('type', $qb->createNamedParameter($signatory->getType()->value))
->setValue('status', $qb->createNamedParameter($signatory->getStatus()->value))
->setValue('creation', $qb->createNamedParameter(time()))
->setValue('last_updated', $qb->createNamedParameter(time()));
$qb->executeStatement();
}
/**
* @param ISignatory $signatory
*
* @throws SignatoryNotFoundException
* @throws SignatoryConflictException
*/
private function updateKnownSignatory(ISignatory $signatory): void {
$knownSignatory = $this->getStoredSignatory($signatory->getKeyId());
switch ($signatory->getType()) {
case SignatoryType::FORGIVABLE:
$this->deleteSignatory($knownSignatory->getKeyId());
$this->insertSignatory($signatory);
return;
case SignatoryType::REFRESHABLE:
$this->updateSignatoryPublicKey($signatory);
$this->updateSignatoryMetadata($signatory);
break;
case SignatoryType::TRUSTED:
// TODO: send notice to admin
throw new SignatoryConflictException();
case SignatoryType::STATIC:
// TODO: send warning to admin
throw new SignatoryConflictException();
}
}
/**
* This is called when a remote signatory does not exist anymore
*
* @param ISignatory|null $knownSignatory NULL is not known
*
* @throws SignatoryConflictException
* @throws SignatoryNotFoundException
*/
private function manageDeprecatedSignatory(?ISignatory $knownSignatory): void {
switch ($knownSignatory?->getType()) {
case null: // unknown in local database
case SignatoryType::FORGIVABLE: // who cares ?
throw new SignatoryNotFoundException(); // meaning we just return the correct exception
case SignatoryType::REFRESHABLE:
// TODO: send notice to admin
throw new SignatoryConflictException(); // while it can be refreshed, it must exist
case SignatoryType::TRUSTED:
case SignatoryType::STATIC:
// TODO: send warning to admin
throw new SignatoryConflictException(); // no way.
}
}
private function updateSignatoryPublicKey(ISignatory $signatory): void {
$qb = $this->connection->getQueryBuilder();
$qb->update(self::TABLE_SIGNATORIES)
->set('signatory', $qb->createNamedParameter($signatory->getPublicKey()))
->set('last_updated', $qb->createNamedParameter(time()));
$qb->where(
$qb->expr()->eq('key_id_sum', $qb->createNamedParameter($this->hashKeyId($signatory->getKeyId())))
);
$qb->executeStatement();
}
private function updateSignatoryMetadata(ISignatory $signatory): void {
$qb = $this->connection->getQueryBuilder();
$qb->update(self::TABLE_SIGNATORIES)
->set('metadata', $qb->createNamedParameter(json_encode($signatory->getMetadata())))
->set('last_updated', $qb->createNamedParameter(time()));
$qb->where(
$qb->expr()->eq('key_id_sum', $qb->createNamedParameter($this->hashKeyId($signatory->getKeyId())))
);
$qb->executeStatement();
}
private function deleteSignatory(string $keyId): void {
$qb = $this->connection->getQueryBuilder();
$qb->delete(self::TABLE_SIGNATORIES)
->where($qb->expr()->eq('key_id_sum', $qb->createNamedParameter($this->hashKeyId($keyId))));
$qb->executeStatement();
}
private function hashKeyId(string $keyId): string {
return hash('sha256', $keyId);
}
}
|