]> git.mxchange.org Git - friendica.git/blob - src/Util/HTTPSignature.php
2a48267191d4163d907cf9fe8984e32fc34ccf16
[friendica.git] / src / Util / HTTPSignature.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Util;
23
24 use Friendica\Core\Logger;
25 use Friendica\Database\Database;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Model\APContact;
29 use Friendica\Model\Contact;
30 use Friendica\Model\User;
31 use Friendica\Network\HTTPClient\Client\HttpClient;
32 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
33
34 /**
35  * Implements HTTP Signatures per draft-cavage-http-signatures-07.
36  *
37  * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Web/HTTPSig.php
38  *
39  * Other parts of the code for HTTP signing are taken from the Osada project.
40  * https://framagit.org/macgirvin/osada
41  *
42  * @see https://tools.ietf.org/html/draft-cavage-http-signatures-07
43  */
44
45 class HTTPSignature
46 {
47         // See draft-cavage-http-signatures-08
48         /**
49          * Verifies a magic request
50          *
51          * @param $key
52          *
53          * @return array with verification data
54          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
55          */
56         public static function verifyMagic($key)
57         {
58                 $headers   = null;
59                 $spoofable = false;
60                 $result = [
61                         'signer'         => '',
62                         'header_signed'  => false,
63                         'header_valid'   => false
64                 ];
65
66                 // Decide if $data arrived via controller submission or curl.
67                 $headers = [];
68                 $headers['(request-target)'] = strtolower(DI::args()->getMethod()).' '.$_SERVER['REQUEST_URI'];
69
70                 foreach ($_SERVER as $k => $v) {
71                         if (strpos($k, 'HTTP_') === 0) {
72                                 $field = str_replace('_', '-', strtolower(substr($k, 5)));
73                                 $headers[$field] = $v;
74                         }
75                 }
76
77                 $sig_block = null;
78
79                 $sig_block = self::parseSigheader($headers['authorization']);
80
81                 if (!$sig_block) {
82                         Logger::notice('no signature provided.');
83                         return $result;
84                 }
85
86                 $result['header_signed'] = true;
87
88                 $signed_headers = $sig_block['headers'];
89                 if (!$signed_headers) {
90                         $signed_headers = ['date'];
91                 }
92
93                 $signed_data = '';
94                 foreach ($signed_headers as $h) {
95                         if (array_key_exists($h, $headers)) {
96                                 $signed_data .= $h . ': ' . $headers[$h] . "\n";
97                         }
98                         if (strpos($h, '.')) {
99                                 $spoofable = true;
100                         }
101                 }
102
103                 $signed_data = rtrim($signed_data, "\n");
104
105                 $algorithm = 'sha512';
106
107                 if ($key && function_exists($key)) {
108                         $result['signer'] = $sig_block['keyId'];
109                         $key = $key($sig_block['keyId']);
110                 }
111
112                 Logger::info('Got keyID ' . $sig_block['keyId']);
113
114                 if (!$key) {
115                         return $result;
116                 }
117
118                 $x = Crypto::rsaVerify($signed_data, $sig_block['signature'], $key, $algorithm);
119
120                 Logger::info('verified: ' . $x);
121
122                 if (!$x) {
123                         return $result;
124                 }
125
126                 if (!$spoofable) {
127                         $result['header_valid'] = true;
128                 }
129
130                 return $result;
131         }
132
133         /**
134          * @param array   $head
135          * @param string  $prvkey
136          * @param string  $keyid (optional, default 'Key')
137          *
138          * @return array
139          */
140         public static function createSig($head, $prvkey, $keyid = 'Key')
141         {
142                 $return_headers = [];
143                 if (!empty($head)) {
144                         $return_headers = $head;
145                 }
146
147                 $alg = 'sha512';
148                 $algorithm = 'rsa-sha512';
149
150                 $x = self::sign($head, $prvkey, $alg);
151
152                 $headerval = 'keyId="' . $keyid . '",algorithm="' . $algorithm
153                         . '",headers="' . $x['headers'] . '",signature="' . $x['signature'] . '"';
154
155                 $return_headers['Authorization'] = ['Signature ' . $headerval];
156
157                 return $return_headers;
158         }
159
160         /**
161          * @param array  $head
162          * @param string $prvkey
163          * @param string $alg (optional) default 'sha256'
164          *
165          * @return array
166          */
167         private static function sign($head, $prvkey, $alg = 'sha256')
168         {
169                 $ret = [];
170                 $headers = '';
171                 $fields  = '';
172
173                 foreach ($head as $k => $v) {
174                         if (is_array($v)) {
175                                 $v = implode(', ', $v);
176                         }
177                         $headers .= strtolower($k) . ': ' . trim($v) . "\n";
178                         if ($fields) {
179                                 $fields .= ' ';
180                         }
181                         $fields .= strtolower($k);
182                 }
183                 // strip the trailing linefeed
184                 $headers = rtrim($headers, "\n");
185
186                 $sig = base64_encode(Crypto::rsaSign($headers, $prvkey, $alg));
187
188                 $ret['headers']   = $fields;
189                 $ret['signature'] = $sig;
190
191                 return $ret;
192         }
193
194         /**
195          * @param string $header
196          * @return array associative array with
197          *   - \e string \b keyID
198          *   - \e string \b created
199          *   - \e string \b expires
200          *   - \e string \b algorithm
201          *   - \e array  \b headers
202          *   - \e string \b signature
203          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
204          */
205         public static function parseSigheader($header)
206         {
207                 // Remove obsolete folds
208                 $header = preg_replace('/\n\s+/', ' ', $header);
209
210                 $token = "[!#$%&'*+.^_`|~0-9A-Za-z-]";
211
212                 $quotedString = '"(?:\\\\.|[^"\\\\])*"';
213
214                 $regex = "/($token+)=($quotedString|$token+)/ism";
215
216                 $matches = [];
217                 preg_match_all($regex, $header, $matches, PREG_SET_ORDER);
218
219                 $headers = [];
220                 foreach ($matches as $match) {
221                         $headers[$match[1]] = trim($match[2] ?: $match[3], '"');
222                 }
223
224                 // if the header is encrypted, decrypt with (default) site private key and continue
225                 if (!empty($headers['iv'])) {
226                         $header = self::decryptSigheader($headers, DI::config()->get('system', 'prvkey'));
227                         return self::parseSigheader($header);
228                 }
229
230                 $return = [
231                         'keyId'     => $headers['keyId'] ?? '',
232                         'algorithm' => $headers['algorithm'] ?? 'rsa-sha256',
233                         'created'   => $headers['created'] ?? null,
234                         'expires'   => $headers['expires'] ?? null,
235                         'headers'   => explode(' ', $headers['headers'] ?? ''),
236                         'signature' => base64_decode(preg_replace('/\s+/', '', $headers['signature'] ?? '')),
237                 ];
238
239                 if (!empty($return['signature']) && !empty($return['algorithm']) && empty($return['headers'])) {
240                         $return['headers'] = ['date'];
241                 }
242
243                 return $return;
244         }
245
246         /**
247          * @param array  $headers Signature headers
248          * @param string $prvkey  The site private key
249          * @return string Decrypted signature string
250          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
251          */
252         private static function decryptSigheader(array $headers, string $prvkey)
253         {
254                 if (!empty($headers['iv']) && !empty($headers['key']) && !empty($headers['data'])) {
255                         return Crypto::unencapsulate($headers, $prvkey);
256                 }
257
258                 return '';
259         }
260
261         /*
262          * Functions for ActivityPub
263          */
264
265         /**
266          * Transmit given data to a target for a user
267          *
268          * @param array   $data   Data that is about to be send
269          * @param string  $target The URL of the inbox
270          * @param integer $uid    User id of the sender
271          *
272          * @return boolean Was the transmission successful?
273          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
274          */
275         public static function transmit($data, $target, $uid)
276         {
277                 $owner = User::getOwnerDataById($uid);
278
279                 if (!$owner) {
280                         return;
281                 }
282
283                 $content = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
284
285                 // Header data that is about to be signed.
286                 $host = parse_url($target, PHP_URL_HOST);
287                 $path = parse_url($target, PHP_URL_PATH);
288                 $digest = 'SHA-256=' . base64_encode(hash('sha256', $content, true));
289                 $content_length = strlen($content);
290                 $date = DateTimeFormat::utcNow(DateTimeFormat::HTTP);
291
292                 $headers = [
293                         'Date' => $date,
294                         'Content-Length' => $content_length,
295                         'Digest' => $digest,
296                         'Host' => $host
297                 ];
298
299                 $signed_data = "(request-target): post " . $path . "\ndate: ". $date . "\ncontent-length: " . $content_length . "\ndigest: " . $digest . "\nhost: " . $host;
300
301                 $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
302
303                 $headers['Signature'] = 'keyId="' . $owner['url'] . '#main-key' . '",algorithm="rsa-sha256",headers="(request-target) date content-length digest host",signature="' . $signature . '"';
304
305                 $headers['Content-Type'] = 'application/activity+json';
306
307                 $postResult = DI::httpClient()->post($target, $content, $headers);
308                 $return_code = $postResult->getReturnCode();
309
310                 Logger::info('Transmit to ' . $target . ' returned ' . $return_code);
311
312                 $success = ($return_code >= 200) && ($return_code <= 299);
313
314                 self::setInboxStatus($target, $success);
315
316                 return $success;
317         }
318
319         /**
320          * Set the delivery status for a given inbox
321          *
322          * @param string  $url     The URL of the inbox
323          * @param boolean $success Transmission status
324          * @param boolean $shared  The inbox is a shared inbox
325          */
326         static public function setInboxStatus($url, $success, $shared = false)
327         {
328                 $now = DateTimeFormat::utcNow();
329
330                 $status = DBA::selectFirst('inbox-status', [], ['url' => $url]);
331                 if (!DBA::isResult($status)) {
332                         DBA::insert('inbox-status', ['url' => $url, 'created' => $now, 'shared' => $shared], Database::INSERT_IGNORE);
333                         $status = DBA::selectFirst('inbox-status', [], ['url' => $url]);
334                 }
335
336                 if ($success) {
337                         $fields = ['success' => $now];
338                 } else {
339                         $fields = ['failure' => $now];
340                 }
341
342                 if ($status['failure'] > DBA::NULL_DATETIME) {
343                         $new_previous_stamp = strtotime($status['failure']);
344                         $old_previous_stamp = strtotime($status['previous']);
345
346                         // Only set "previous" with at least one day difference.
347                         // We use this to assure to not accidentally archive too soon.
348                         if (($new_previous_stamp - $old_previous_stamp) >= 86400) {
349                                 $fields['previous'] = $status['failure'];
350                         }
351                 }
352
353                 if (!$success) {
354                         if ($status['success'] <= DBA::NULL_DATETIME) {
355                                 $stamp1 = strtotime($status['created']);
356                         } else {
357                                 $stamp1 = strtotime($status['success']);
358                         }
359
360                         $stamp2 = strtotime($now);
361                         $previous_stamp = strtotime($status['previous']);
362
363                         // Archive the inbox when there had been failures for five days.
364                         // Additionally ensure that at least one previous attempt has to be in between.
365                         if ((($stamp2 - $stamp1) >= 86400 * 5) && ($previous_stamp > $stamp1)) {
366                                 $fields['archive'] = true;
367                         }
368                 } else {
369                         $fields['archive'] = false;
370                 }
371
372                 DBA::update('inbox-status', $fields, ['url' => $url]);
373         }
374
375         /**
376          * Fetches JSON data for a user
377          *
378          * @param string  $request request url
379          * @param integer $uid     User id of the requester
380          *
381          * @return array JSON array
382          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
383          */
384         public static function fetch($request, $uid)
385         {
386                 $curlResult = self::fetchRaw($request, $uid);
387
388                 if (empty($curlResult)) {
389                         return false;
390                 }
391
392                 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
393                         return false;
394                 }
395
396                 $content = json_decode($curlResult->getBody(), true);
397                 if (empty($content) || !is_array($content)) {
398                         return false;
399                 }
400
401                 return $content;
402         }
403
404         /**
405          * Fetches raw data for a user
406          *
407          * @param string  $request request url
408          * @param integer $uid     User id of the requester
409          * @param boolean $binary  TRUE if asked to return binary results (file download) (default is "false")
410          * @param array   $opts    (optional parameters) assoziative array with:
411          *                         'accept_content' => supply Accept: header with 'accept_content' as the value
412          *                         'timeout' => int Timeout in seconds, default system config value or 60 seconds
413          *                         'nobody' => only return the header
414          *                         'cookiejar' => path to cookie jar file
415          *
416          * @return \Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses CurlResult
417          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
418          */
419         public static function fetchRaw($request, $uid = 0, $opts = [HttpClientOptions::ACCEPT_CONTENT => [HttpClient::ACCEPT_JSON_AS]])
420         {
421                 $header = [];
422
423                 if (!empty($uid)) {
424                         $owner = User::getOwnerDataById($uid);
425                         if (!$owner) {
426                                 return;
427                         }
428                 } else {
429                         $owner = User::getSystemAccount();
430                         if (!$owner) {
431                                 return;
432                         }
433                 }
434
435                 if (!empty($owner['uprvkey'])) {
436                         // Header data that is about to be signed.
437                         $host = parse_url($request, PHP_URL_HOST);
438                         $path = parse_url($request, PHP_URL_PATH);
439                         $date = DateTimeFormat::utcNow(DateTimeFormat::HTTP);
440
441                         $header['Date'] = $date;
442                         $header['Host'] = $host;
443
444                         $signed_data = "(request-target): get " . $path . "\ndate: ". $date . "\nhost: " . $host;
445
446                         $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
447
448                         $header['Signature'] = 'keyId="' . $owner['url'] . '#main-key' . '",algorithm="rsa-sha256",headers="(request-target) date host",signature="' . $signature . '"';
449                 }
450
451                 $curl_opts                             = $opts;
452                 $curl_opts[HttpClientOptions::HEADERS] = $header;
453
454                 if (!empty($opts['nobody'])) {
455                         $curlResult = DI::httpClient()->head($request, $curl_opts);
456                 } else {
457                         $curlResult = DI::httpClient()->get($request, $curl_opts);
458                 }
459                 $return_code = $curlResult->getReturnCode();
460
461                 Logger::info('Fetched for user ' . $uid . ' from ' . $request . ' returned ' . $return_code);
462
463                 return $curlResult;
464         }
465
466         /**
467          * Gets a signer from a given HTTP request
468          *
469          * @param $content
470          * @param $http_headers
471          *
472          * @return string Signer
473          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
474          */
475         public static function getSigner($content, $http_headers)
476         {
477                 if (empty($http_headers['HTTP_SIGNATURE'])) {
478                         Logger::debug('No HTTP_SIGNATURE header');
479                         return false;
480                 }
481
482                 if (!empty($content)) {
483                         $object = json_decode($content, true);
484                         if (empty($object)) {
485                                 Logger::info('No object');
486                                 return false;
487                         }
488
489                         $actor = JsonLD::fetchElement($object, 'actor', 'id');
490                 } else {
491                         $actor = '';
492                 }
493
494                 $headers = [];
495                 $headers['(request-target)'] = strtolower(DI::args()->getMethod()) . ' ' . parse_url($http_headers['REQUEST_URI'], PHP_URL_PATH);
496
497                 // First take every header
498                 foreach ($http_headers as $k => $v) {
499                         $field = str_replace('_', '-', strtolower($k));
500                         $headers[$field] = $v;
501                 }
502
503                 // Now add every http header
504                 foreach ($http_headers as $k => $v) {
505                         if (strpos($k, 'HTTP_') === 0) {
506                                 $field = str_replace('_', '-', strtolower(substr($k, 5)));
507                                 $headers[$field] = $v;
508                         }
509                 }
510
511                 $sig_block = self::parseSigHeader($http_headers['HTTP_SIGNATURE']);
512
513                 // Add fields from the signature block to the header. See issue 8845
514                 if (!empty($sig_block['created']) && empty($headers['(created)'])) {
515                         $headers['(created)'] = $sig_block['created'];
516                 }
517
518                 if (!empty($sig_block['expires']) && empty($headers['(expires)'])) {
519                         $headers['(expires)'] = $sig_block['expires'];
520                 }
521
522                 if (empty($sig_block) || empty($sig_block['headers']) || empty($sig_block['keyId'])) {
523                         Logger::info('No headers or keyId');
524                         return false;
525                 }
526
527                 $signed_data = '';
528                 foreach ($sig_block['headers'] as $h) {
529                         if (array_key_exists($h, $headers)) {
530                                 $signed_data .= $h . ': ' . $headers[$h] . "\n";
531                         } else {
532                                 Logger::info('Requested header field not found', ['field' => $h, 'header' => $headers]);
533                         }
534                 }
535                 $signed_data = rtrim($signed_data, "\n");
536
537                 if (empty($signed_data)) {
538                         Logger::info('Signed data is empty');
539                         return false;
540                 }
541
542                 $algorithm = null;
543
544                 // Wildcard value where signing algorithm should be derived from keyId
545                 // @see https://tools.ietf.org/html/draft-ietf-httpbis-message-signatures-00#section-4.1
546                 // Defaulting to SHA256 as it seems to be the prevalent implementation
547                 // @see https://arewehs2019yet.vpzom.click
548                 if ($sig_block['algorithm'] === 'hs2019') {
549                         $algorithm = 'sha256';
550                 }
551
552                 if ($sig_block['algorithm'] === 'rsa-sha256') {
553                         $algorithm = 'sha256';
554                 }
555
556                 if ($sig_block['algorithm'] === 'rsa-sha512') {
557                         $algorithm = 'sha512';
558                 }
559
560                 if (empty($algorithm)) {
561                         Logger::info('No alagorithm');
562                         return false;
563                 }
564
565                 $key = self::fetchKey($sig_block['keyId'], $actor);
566                 if (empty($key)) {
567                         Logger::info('Empty key');
568                         return false;
569                 }
570
571                 if (!empty($key['url']) && !empty($key['type']) && ($key['type'] == 'Tombstone')) {
572                         Logger::info('Actor is a tombstone', ['key' => $key]);
573
574                         if (!Contact::isLocal($key['url'])) {
575                                 // We now delete everything that we possibly knew from this actor
576                                 Contact::deleteContactByUrl($key['url']);
577                         }
578                         return null;
579                 }
580
581                 if (empty($key['pubkey'])) {
582                         Logger::info('Empty pubkey');
583                         return false;
584                 }
585
586                 if (!Crypto::rsaVerify($signed_data, $sig_block['signature'], $key['pubkey'], $algorithm)) {
587                         Logger::info('Verification failed', ['signed_data' => $signed_data, 'algorithm' => $algorithm, 'header' => $sig_block['headers'], 'http_headers' => $http_headers]);
588                         return false;
589                 }
590
591                 $hasGoodSignedContent = false;
592
593                 // Check the digest when it is part of the signed data
594                 if (!empty($content) && in_array('digest', $sig_block['headers'])) {
595                         $digest = explode('=', $headers['digest'], 2);
596                         if ($digest[0] === 'SHA-256') {
597                                 $hashalg = 'sha256';
598                         }
599                         if ($digest[0] === 'SHA-512') {
600                                 $hashalg = 'sha512';
601                         }
602
603                         /// @todo add all hashes from the rfc
604
605                         if (!empty($hashalg) && base64_encode(hash($hashalg, $content, true)) != $digest[1]) {
606                                 Logger::info('Digest does not match');
607                                 return false;
608                         }
609
610                         $hasGoodSignedContent = true;
611                 }
612
613                 if (in_array('date', $sig_block['headers']) && !empty($headers['date'])) {
614                         $created = strtotime($headers['date']);
615                 } elseif (in_array('(created)', $sig_block['headers']) && !empty($sig_block['created'])) {
616                         $created = $sig_block['created'];
617                 } else {
618                         $created = 0;
619                 }
620
621                 if (in_array('(expires)', $sig_block['headers']) && !empty($sig_block['expires'])) {
622                         $expired = min($sig_block['expires'], $created + 300);
623                 } else {
624                         $expired = $created + 300;
625                 }
626
627                 //  Check if the signed date field is in an acceptable range
628                 if (!empty($created)) {
629                         $current = time();
630
631                         // Calculate with a grace period of 60 seconds to avoid slight time differences between the servers
632                         if (($created - 60) > $current) {
633                                 Logger::notice('Signature created in the future', ['created' => date(DateTimeFormat::MYSQL, $created), 'expired' => date(DateTimeFormat::MYSQL, $expired), 'current' => date(DateTimeFormat::MYSQL, $current)]);
634                                 return false;
635                         }
636
637                         if ($current > $expired) {
638                                 Logger::notice('Signature expired', ['created' => date(DateTimeFormat::MYSQL, $created), 'expired' => date(DateTimeFormat::MYSQL, $expired), 'current' => date(DateTimeFormat::MYSQL, $current)]);
639                                 return false;
640                         }
641
642                         Logger::debug('Valid creation date', ['created' => date(DateTimeFormat::MYSQL, $created), 'expired' => date(DateTimeFormat::MYSQL, $expired), 'current' => date(DateTimeFormat::MYSQL, $current)]);
643                         $hasGoodSignedContent = true;
644                 }
645
646                 // Check the content-length when it is part of the signed data
647                 if (in_array('content-length', $sig_block['headers'])) {
648                         if (strlen($content) != $headers['content-length']) {
649                                 Logger::info('Content length does not match');
650                                 return false;
651                         }
652                 }
653
654                 // Ensure that the authentication had been done with some content
655                 // Without this check someone could authenticate with fakeable data
656                 if (!$hasGoodSignedContent) {
657                         Logger::info('No good signed content');
658                         return false;
659                 }
660
661                 return $key['url'];
662         }
663
664         /**
665          * fetches a key for a given id and actor
666          *
667          * @param $id
668          * @param $actor
669          *
670          * @return array with actor url and public key
671          * @throws \Exception
672          */
673         private static function fetchKey($id, $actor)
674         {
675                 $url = (strpos($id, '#') ? substr($id, 0, strpos($id, '#')) : $id);
676
677                 $profile = APContact::getByURL($url);
678                 if (!empty($profile)) {
679                         Logger::info('Taking key from id', ['id' => $id]);
680                         return ['url' => $url, 'pubkey' => $profile['pubkey'], 'type' => $profile['type']];
681                 } elseif ($url != $actor) {
682                         $profile = APContact::getByURL($actor);
683                         if (!empty($profile)) {
684                                 Logger::info('Taking key from actor', ['actor' => $actor]);
685                                 return ['url' => $actor, 'pubkey' => $profile['pubkey'], 'type' => $profile['type']];
686                         }
687                 }
688
689                 Logger::notice('Key could not be fetched', ['url' => $url, 'actor' => $actor]);
690                 return false;
691         }
692 }