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