]> git.mxchange.org Git - friendica.git/blob - src/Util/HTTPSignature.php
Merge pull request #10633 from nupplaphil/task/httprequest_optimiziations
[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\CurlResult;
32 use Friendica\Network\IHTTPResult;
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($_SERVER['REQUEST_METHOD']).' '.$_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::log('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::log('Got keyID ' . $sig_block['keyId'], Logger::DEBUG);
113
114                 if (!$key) {
115                         return $result;
116                 }
117
118                 $x = Crypto::rsaVerify($signed_data, $sig_block['signature'], $key, $algorithm);
119
120                 Logger::log('verified: ' . $x, Logger::DEBUG);
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::httpRequest()->post($target, $content, $headers);
308                 $return_code = $postResult->getReturnCode();
309
310                 Logger::log('Transmit to ' . $target . ' returned ' . $return_code, Logger::DEBUG);
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 IHTTPResult CurlResult
417          * @throws \Friendica\Network\HTTPException\InternalServerErrorException
418          */
419         public static function fetchRaw($request, $uid = 0, $opts = ['accept_content' => ['application/activity+json', 'application/ld+json']])
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['header'] = $header;
453
454                 if (!empty($opts['nobody'])) {
455                         $curlResult = DI::httpRequest()->head($request, $curl_opts);
456                 } else {
457                         $curlResult = DI::httpRequest()->get($request, $curl_opts);
458                 }
459                 $return_code = $curlResult->getReturnCode();
460
461                 Logger::log('Fetched for user ' . $uid . ' from ' . $request . ' returned ' . $return_code, Logger::DEBUG);
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::info('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($http_headers['REQUEST_METHOD']) . ' ' . 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                 if (empty($sig_block) || empty($sig_block['headers']) || empty($sig_block['keyId'])) {
514                         Logger::info('No headers or keyId');
515                         return false;
516                 }
517
518                 $signed_data = '';
519                 foreach ($sig_block['headers'] as $h) {
520                         if (array_key_exists($h, $headers)) {
521                                 $signed_data .= $h . ': ' . $headers[$h] . "\n";
522                         }
523                 }
524                 $signed_data = rtrim($signed_data, "\n");
525
526                 if (empty($signed_data)) {
527                         Logger::info('Signed data is empty');
528                         return false;
529                 }
530
531                 $algorithm = null;
532
533                 // Wildcard value where signing algorithm should be derived from keyId
534                 // @see https://tools.ietf.org/html/draft-ietf-httpbis-message-signatures-00#section-4.1
535                 // Defaulting to SHA256 as it seems to be the prevalent implementation
536                 // @see https://arewehs2019yet.vpzom.click
537                 if ($sig_block['algorithm'] === 'hs2019') {
538                         $algorithm = 'sha256';
539                 }
540
541                 if ($sig_block['algorithm'] === 'rsa-sha256') {
542                         $algorithm = 'sha256';
543                 }
544
545                 if ($sig_block['algorithm'] === 'rsa-sha512') {
546                         $algorithm = 'sha512';
547                 }
548
549                 if (empty($algorithm)) {
550                         Logger::info('No alagorithm');
551                         return false;
552                 }
553
554                 $key = self::fetchKey($sig_block['keyId'], $actor);
555                 if (empty($key)) {
556                         Logger::info('Empty key');
557                         return false;
558                 }
559
560                 if (!empty($key['url']) && !empty($key['type']) && ($key['type'] == 'Tombstone')) {
561                         Logger::info('Actor is a tombstone', ['key' => $key]);
562
563                         if (!Contact::isLocal($key['url'])) {
564                                 // We now delete everything that we possibly knew from this actor
565                                 Contact::deleteContactByUrl($key['url']);
566                         }
567                         return null;
568                 }
569
570                 if (empty($key['pubkey'])) {
571                         Logger::info('Empty pubkey');
572                         return false;
573                 }
574
575                 if (!Crypto::rsaVerify($signed_data, $sig_block['signature'], $key['pubkey'], $algorithm)) {
576                         Logger::info('Verification failed');
577                         return false;
578                 }
579
580                 $hasGoodSignedContent = false;
581
582                 // Check the digest when it is part of the signed data
583                 if (!empty($content) && in_array('digest', $sig_block['headers'])) {
584                         $digest = explode('=', $headers['digest'], 2);
585                         if ($digest[0] === 'SHA-256') {
586                                 $hashalg = 'sha256';
587                         }
588                         if ($digest[0] === 'SHA-512') {
589                                 $hashalg = 'sha512';
590                         }
591
592                         /// @todo add all hashes from the rfc
593
594                         if (!empty($hashalg) && base64_encode(hash($hashalg, $content, true)) != $digest[1]) {
595                                 Logger::info('Digest does not match');
596                                 return false;
597                         }
598
599                         $hasGoodSignedContent = true;
600                 }
601
602                 //  Check if the signed date field is in an acceptable range
603                 if (in_array('date', $sig_block['headers'])) {
604                         $diff = abs(strtotime($headers['date']) - time());
605                         if ($diff > 300) {
606                                 Logger::log("Header date '" . $headers['date'] . "' is with " . $diff . " seconds out of the 300 second frame. The signature is invalid.");
607                                 return false;
608                         }
609                         $hasGoodSignedContent = true;
610                 }
611
612                 // Check the content-length when it is part of the signed data
613                 if (in_array('content-length', $sig_block['headers'])) {
614                         if (strlen($content) != $headers['content-length']) {
615                                 Logger::info('Content length does not match');
616                                 return false;
617                         }
618                 }
619
620                 // Ensure that the authentication had been done with some content
621                 // Without this check someone could authenticate with fakeable data
622                 if (!$hasGoodSignedContent) {
623                         Logger::info('No good signed content');
624                         return false;
625                 }
626
627                 return $key['url'];
628         }
629
630         /**
631          * fetches a key for a given id and actor
632          *
633          * @param $id
634          * @param $actor
635          *
636          * @return array with actor url and public key
637          * @throws \Exception
638          */
639         private static function fetchKey($id, $actor)
640         {
641                 $url = (strpos($id, '#') ? substr($id, 0, strpos($id, '#')) : $id);
642
643                 $profile = APContact::getByURL($url);
644                 if (!empty($profile)) {
645                         Logger::info('Taking key from id', ['id' => $id]);
646                         return ['url' => $url, 'pubkey' => $profile['pubkey'], 'type' => $profile['type']];
647                 } elseif ($url != $actor) {
648                         $profile = APContact::getByURL($actor);
649                         if (!empty($profile)) {
650                                 Logger::info('Taking key from actor', ['actor' => $actor]);
651                                 return ['url' => $actor, 'pubkey' => $profile['pubkey'], 'type' => $profile['type']];
652                         }
653                 }
654
655                 Logger::notice('Key could not be fetched', ['url' => $url, 'actor' => $actor]);
656                 return false;
657         }
658 }