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