3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Util;
24 use Friendica\Core\Logger;
25 use Friendica\Core\Protocol;
26 use Friendica\Database\Database;
27 use Friendica\Database\DBA;
29 use Friendica\Model\APContact;
30 use Friendica\Model\Contact;
31 use Friendica\Model\GServer;
32 use Friendica\Model\ItemURI;
33 use Friendica\Model\User;
34 use Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses;
35 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
36 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
39 * Implements HTTP Signatures per draft-cavage-http-signatures-07.
41 * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Web/HTTPSig.php
43 * Other parts of the code for HTTP signing are taken from the Osada project.
44 * https://framagit.org/macgirvin/osada
46 * @see https://tools.ietf.org/html/draft-cavage-http-signatures-07
51 // See draft-cavage-http-signatures-08
53 * Verifies a magic request
57 * @return array with verification data
58 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
60 public static function verifyMagic(string $key): array
66 'header_signed' => false,
67 'header_valid' => false
70 // Decide if $data arrived via controller submission or curl.
72 $headers['(request-target)'] = strtolower(DI::args()->getMethod()).' '.$_SERVER['REQUEST_URI'];
74 foreach ($_SERVER as $k => $v) {
75 if (strpos($k, 'HTTP_') === 0) {
76 $field = str_replace('_', '-', strtolower(substr($k, 5)));
77 $headers[$field] = $v;
83 $sig_block = self::parseSigheader($headers['authorization']);
86 Logger::notice('no signature provided.');
90 $result['header_signed'] = true;
92 $signed_headers = $sig_block['headers'];
93 if (!$signed_headers) {
94 $signed_headers = ['date'];
98 foreach ($signed_headers as $h) {
99 if (array_key_exists($h, $headers)) {
100 $signed_data .= $h . ': ' . $headers[$h] . "\n";
102 if (strpos($h, '.')) {
107 $signed_data = rtrim($signed_data, "\n");
109 $algorithm = 'sha512';
111 if ($key && function_exists($key)) {
112 $result['signer'] = $sig_block['keyId'];
113 $key = $key($sig_block['keyId']);
116 Logger::info('Got keyID ' . $sig_block['keyId']);
122 $x = Crypto::rsaVerify($signed_data, $sig_block['signature'], $key, $algorithm);
124 Logger::info('verified: ' . $x);
131 $result['header_valid'] = true;
139 * @param string $prvkey
140 * @param string $keyid (optional, default 'Key')
144 public static function createSig(array $head, string $prvkey, string $keyid = 'Key'): array
146 $return_headers = [];
148 $return_headers = $head;
152 $algorithm = 'rsa-sha512';
154 $x = self::sign($head, $prvkey, $alg);
156 $headerval = 'keyId="' . $keyid . '",algorithm="' . $algorithm
157 . '",headers="' . $x['headers'] . '",signature="' . $x['signature'] . '"';
159 $return_headers['Authorization'] = ['Signature ' . $headerval];
161 return $return_headers;
166 * @param string $prvkey
167 * @param string $alg (optional) default 'sha256'
171 private static function sign(array $head, string $prvkey, string $alg = 'sha256'): array
177 foreach ($head as $k => $v) {
179 $v = implode(', ', $v);
181 $headers .= strtolower($k) . ': ' . trim($v) . "\n";
185 $fields .= strtolower($k);
187 // strip the trailing linefeed
188 $headers = rtrim($headers, "\n");
190 $sig = base64_encode(Crypto::rsaSign($headers, $prvkey, $alg));
192 $ret['headers'] = $fields;
193 $ret['signature'] = $sig;
199 * @param string $header
200 * @return array associative array with
201 * - \e string \b keyID
202 * - \e string \b created
203 * - \e string \b expires
204 * - \e string \b algorithm
205 * - \e array \b headers
206 * - \e string \b signature
207 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
209 public static function parseSigheader(string $header): array
211 // Remove obsolete folds
212 $header = preg_replace('/\n\s+/', ' ', $header);
214 $token = "[!#$%&'*+.^_`|~0-9A-Za-z-]";
216 $quotedString = '"(?:\\\\.|[^"\\\\])*"';
218 $regex = "/($token+)=($quotedString|$token+)/ism";
221 preg_match_all($regex, $header, $matches, PREG_SET_ORDER);
224 foreach ($matches as $match) {
225 $headers[$match[1]] = trim($match[2] ?: $match[3], '"');
228 // if the header is encrypted, decrypt with (default) site private key and continue
229 if (!empty($headers['iv'])) {
230 $header = self::decryptSigheader($headers, DI::config()->get('system', 'prvkey'));
231 return self::parseSigheader($header);
235 'keyId' => $headers['keyId'] ?? '',
236 'algorithm' => $headers['algorithm'] ?? 'rsa-sha256',
237 'created' => $headers['created'] ?? null,
238 'expires' => $headers['expires'] ?? null,
239 'headers' => explode(' ', $headers['headers'] ?? ''),
240 'signature' => base64_decode(preg_replace('/\s+/', '', $headers['signature'] ?? '')),
243 if (!empty($return['signature']) && !empty($return['algorithm']) && empty($return['headers'])) {
244 $return['headers'] = ['date'];
251 * @param array $headers Signature headers
252 * @param string $prvkey The site private key
253 * @return string Decrypted signature string
254 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
256 private static function decryptSigheader(array $headers, string $prvkey): string
258 if (!empty($headers['iv']) && !empty($headers['key']) && !empty($headers['data'])) {
259 return Crypto::unencapsulate($headers, $prvkey);
266 * Functions for ActivityPub
270 * Post given data to a target for a user, returns the result class
272 * @param array $data Data that is about to be sent
273 * @param string $target The URL of the inbox
274 * @param array $owner Sender owner-view record
276 * @return ICanHandleHttpResponses
278 public static function post(array $data, string $target, array $owner): ICanHandleHttpResponses
280 $content = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
282 // Header data that is about to be signed.
283 $host = parse_url($target, PHP_URL_HOST);
284 $path = parse_url($target, PHP_URL_PATH);
285 $digest = 'SHA-256=' . base64_encode(hash('sha256', $content, true));
286 $content_length = strlen($content);
287 $date = DateTimeFormat::utcNow(DateTimeFormat::HTTP);
291 'Content-Length' => $content_length,
296 $signed_data = "(request-target): post " . $path . "\ndate: ". $date . "\ncontent-length: " . $content_length . "\ndigest: " . $digest . "\nhost: " . $host;
298 $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
300 $headers['Signature'] = 'keyId="' . $owner['url'] . '#main-key' . '",algorithm="rsa-sha256",headers="(request-target) date content-length digest host",signature="' . $signature . '"';
302 $headers['Content-Type'] = 'application/activity+json';
304 $postResult = DI::httpClient()->post($target, $content, $headers, DI::config()->get('system', 'curl_timeout'));
305 $return_code = $postResult->getReturnCode();
307 Logger::info('Transmit to ' . $target . ' returned ' . $return_code);
309 self::setInboxStatus($target, ($return_code >= 200) && ($return_code <= 299));
315 * Transmit given data to a target for a user
317 * @param array $data Data that is about to be sent
318 * @param string $target The URL of the inbox
319 * @param array $owner Sender owner-vew record
321 * @return boolean Was the transmission successful?
323 public static function transmit(array $data, string $target, array $owner): bool
325 $postResult = self::post($data, $target, $owner);
326 $return_code = $postResult->getReturnCode();
328 return ($return_code >= 200) && ($return_code <= 299);
332 * Set the delivery status for a given inbox
334 * @param string $url The URL of the inbox
335 * @param boolean $success Transmission status
336 * @param boolean $shared The inbox is a shared inbox
337 * @param int $gsid Server ID
340 static public function setInboxStatus(string $url, bool $success, bool $shared = false, int $gsid = null)
342 $now = DateTimeFormat::utcNow();
344 $status = DBA::selectFirst('inbox-status', [], ['url' => $url]);
345 if (!DBA::isResult($status)) {
346 $insertFields = ['url' => $url, 'uri-id' => ItemURI::getIdByURI($url), 'created' => $now, 'shared' => $shared];
348 $insertFields['gsid'] = $gsid;
350 if (!DBA::insert('inbox-status', $insertFields, Database::INSERT_IGNORE)) {
351 Logger::warning('Unable to insert inbox-status row', $insertFields);
355 $status = DBA::selectFirst('inbox-status', [], ['url' => $url]);
359 $fields = ['success' => $now];
361 $fields = ['failure' => $now];
365 $fields['gsid'] = $gsid;
368 if ($status['failure'] > DBA::NULL_DATETIME) {
369 $new_previous_stamp = strtotime($status['failure']);
370 $old_previous_stamp = strtotime($status['previous']);
372 // Only set "previous" with at least one day difference.
373 // We use this to assure to not accidentally archive too soon.
374 if (($new_previous_stamp - $old_previous_stamp) >= 86400) {
375 $fields['previous'] = $status['failure'];
380 if ($status['success'] <= DBA::NULL_DATETIME) {
381 $stamp1 = strtotime($status['created']);
383 $stamp1 = strtotime($status['success']);
386 $stamp2 = strtotime($now);
387 $previous_stamp = strtotime($status['previous']);
389 // Archive the inbox when there had been failures for five days.
390 // Additionally ensure that at least one previous attempt has to be in between.
391 if ((($stamp2 - $stamp1) >= 86400 * 5) && ($previous_stamp > $stamp1)) {
392 $fields['archive'] = true;
395 $fields['archive'] = false;
398 if (empty($status['uri-id'])) {
399 $fields['uri-id'] = ItemURI::getIdByURI($url);
402 DBA::update('inbox-status', $fields, ['url' => $url]);
404 if (!empty($status['gsid'])) {
406 GServer::setReachableById($status['gsid'], Protocol::ACTIVITYPUB);
407 } elseif ($status['shared']) {
408 GServer::setFailureById($status['gsid']);
414 * Fetches JSON data for a user
416 * @param string $request request url
417 * @param integer $uid User id of the requester
419 * @return array JSON array
420 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
422 public static function fetch(string $request, int $uid): array
424 $curlResult = self::fetchRaw($request, $uid);
426 if (empty($curlResult)) {
430 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
434 $content = json_decode($curlResult->getBody(), true);
435 if (empty($content) || !is_array($content)) {
443 * Fetches raw data for a user
445 * @param string $request request url
446 * @param integer $uid User id of the requester
447 * @param boolean $binary TRUE if asked to return binary results (file download) (default is "false")
448 * @param array $opts (optional parameters) assoziative array with:
449 * 'accept_content' => supply Accept: header with 'accept_content' as the value
450 * 'timeout' => int Timeout in seconds, default system config value or 60 seconds
451 * 'nobody' => only return the header
452 * 'cookiejar' => path to cookie jar file
454 * @return \Friendica\Network\HTTPClient\Capability\ICanHandleHttpResponses CurlResult
455 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
457 public static function fetchRaw(string $request, int $uid = 0, array $opts = [HttpClientOptions::ACCEPT_CONTENT => [HttpClientAccept::JSON_AS]])
462 $owner = User::getOwnerDataById($uid);
467 $owner = User::getSystemAccount();
473 if (!empty($owner['uprvkey'])) {
474 // Header data that is about to be signed.
475 $host = parse_url($request, PHP_URL_HOST);
476 $path = parse_url($request, PHP_URL_PATH);
477 $date = DateTimeFormat::utcNow(DateTimeFormat::HTTP);
479 $header['Date'] = $date;
480 $header['Host'] = $host;
482 $signed_data = "(request-target): get " . $path . "\ndate: ". $date . "\nhost: " . $host;
484 $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
486 $header['Signature'] = 'keyId="' . $owner['url'] . '#main-key' . '",algorithm="rsa-sha256",headers="(request-target) date host",signature="' . $signature . '"';
490 $curl_opts[HttpClientOptions::HEADERS] = $header;
492 if (!empty($opts['nobody'])) {
493 $curlResult = DI::httpClient()->head($request, $curl_opts);
495 $curlResult = DI::httpClient()->get($request, HttpClientAccept::JSON_AS, $curl_opts);
497 $return_code = $curlResult->getReturnCode();
499 Logger::info('Fetched for user ' . $uid . ' from ' . $request . ' returned ' . $return_code);
505 * Fetch the apcontact entry of the keyId in the given header
507 * @param array $http_headers
509 * @return array APContact entry
511 public static function getKeyIdContact(array $http_headers): array
513 if (empty($http_headers['HTTP_SIGNATURE'])) {
514 Logger::debug('No HTTP_SIGNATURE header', ['header' => $http_headers]);
518 $sig_block = self::parseSigHeader($http_headers['HTTP_SIGNATURE']);
520 if (empty($sig_block['keyId'])) {
521 Logger::debug('No keyId', ['sig_block' => $sig_block]);
525 $url = (strpos($sig_block['keyId'], '#') ? substr($sig_block['keyId'], 0, strpos($sig_block['keyId'], '#')) : $sig_block['keyId']);
526 return APContact::getByURL($url);
530 * Gets a signer from a given HTTP request
532 * @param string $content
533 * @param array $http_headers
535 * @return string|null|false Signer
536 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
538 public static function getSigner(string $content, array $http_headers)
540 if (empty($http_headers['HTTP_SIGNATURE'])) {
541 Logger::debug('No HTTP_SIGNATURE header');
545 if (!empty($content)) {
546 $object = json_decode($content, true);
547 if (empty($object)) {
548 Logger::info('No object');
552 $actor = JsonLD::fetchElement($object, 'actor', 'id') ?? '';
558 $headers['(request-target)'] = strtolower(DI::args()->getMethod()) . ' ' . parse_url($http_headers['REQUEST_URI'], PHP_URL_PATH);
560 // First take every header
561 foreach ($http_headers as $k => $v) {
562 $field = str_replace('_', '-', strtolower($k));
563 $headers[$field] = $v;
566 // Now add every http header
567 foreach ($http_headers as $k => $v) {
568 if (strpos($k, 'HTTP_') === 0) {
569 $field = str_replace('_', '-', strtolower(substr($k, 5)));
570 $headers[$field] = $v;
574 $sig_block = self::parseSigHeader($http_headers['HTTP_SIGNATURE']);
576 // Add fields from the signature block to the header. See issue 8845
577 if (!empty($sig_block['created']) && empty($headers['(created)'])) {
578 $headers['(created)'] = $sig_block['created'];
581 if (!empty($sig_block['expires']) && empty($headers['(expires)'])) {
582 $headers['(expires)'] = $sig_block['expires'];
585 if (empty($sig_block) || empty($sig_block['headers']) || empty($sig_block['keyId'])) {
586 Logger::info('No headers or keyId');
591 foreach ($sig_block['headers'] as $h) {
592 if (array_key_exists($h, $headers)) {
593 $signed_data .= $h . ': ' . $headers[$h] . "\n";
595 Logger::info('Requested header field not found', ['field' => $h, 'header' => $headers]);
598 $signed_data = rtrim($signed_data, "\n");
600 if (empty($signed_data)) {
601 Logger::info('Signed data is empty');
607 // Wildcard value where signing algorithm should be derived from keyId
608 // @see https://tools.ietf.org/html/draft-ietf-httpbis-message-signatures-00#section-4.1
609 // Defaulting to SHA256 as it seems to be the prevalent implementation
610 // @see https://arewehs2019yet.vpzom.click
611 if ($sig_block['algorithm'] === 'hs2019') {
612 $algorithm = 'sha256';
615 if ($sig_block['algorithm'] === 'rsa-sha256') {
616 $algorithm = 'sha256';
619 if ($sig_block['algorithm'] === 'rsa-sha512') {
620 $algorithm = 'sha512';
623 if (empty($algorithm)) {
624 Logger::info('No alagorithm');
628 $key = self::fetchKey($sig_block['keyId'], $actor);
630 Logger::info('Empty key');
634 if (!empty($key['url']) && !empty($key['type']) && ($key['type'] == 'Tombstone')) {
635 Logger::info('Actor is a tombstone', ['key' => $key]);
637 if (!Contact::isLocal($key['url'])) {
638 // We now delete everything that we possibly knew from this actor
639 Contact::deleteContactByUrl($key['url']);
644 if (empty($key['pubkey'])) {
645 Logger::info('Empty pubkey');
649 if (!Crypto::rsaVerify($signed_data, $sig_block['signature'], $key['pubkey'], $algorithm)) {
650 Logger::info('Verification failed', ['signed_data' => $signed_data, 'algorithm' => $algorithm, 'header' => $sig_block['headers'], 'http_headers' => $http_headers]);
654 $hasGoodSignedContent = false;
656 // Check the digest when it is part of the signed data
657 if (!empty($content) && in_array('digest', $sig_block['headers'])) {
658 $digest = explode('=', $headers['digest'], 2);
659 if ($digest[0] === 'SHA-256') {
662 if ($digest[0] === 'SHA-512') {
666 /// @todo add all hashes from the rfc
668 if (!empty($hashalg) && base64_encode(hash($hashalg, $content, true)) != $digest[1]) {
669 Logger::info('Digest does not match');
673 $hasGoodSignedContent = true;
676 if (in_array('date', $sig_block['headers']) && !empty($headers['date'])) {
677 $created = strtotime($headers['date']);
678 } elseif (in_array('(created)', $sig_block['headers']) && !empty($sig_block['created'])) {
679 $created = $sig_block['created'];
684 if (in_array('(expires)', $sig_block['headers']) && !empty($sig_block['expires'])) {
685 $expired = min($sig_block['expires'], $created + 300);
687 $expired = $created + 300;
690 // Check if the signed date field is in an acceptable range
691 if (!empty($created)) {
694 // Calculate with a grace period of 60 seconds to avoid slight time differences between the servers
695 if (($created - 60) > $current) {
696 Logger::notice('Signature created in the future', ['created' => date(DateTimeFormat::MYSQL, $created), 'expired' => date(DateTimeFormat::MYSQL, $expired), 'current' => date(DateTimeFormat::MYSQL, $current)]);
700 if ($current > $expired) {
701 Logger::notice('Signature expired', ['created' => date(DateTimeFormat::MYSQL, $created), 'expired' => date(DateTimeFormat::MYSQL, $expired), 'current' => date(DateTimeFormat::MYSQL, $current)]);
705 Logger::debug('Valid creation date', ['created' => date(DateTimeFormat::MYSQL, $created), 'expired' => date(DateTimeFormat::MYSQL, $expired), 'current' => date(DateTimeFormat::MYSQL, $current)]);
706 $hasGoodSignedContent = true;
709 // Check the content-length when it is part of the signed data
710 if (in_array('content-length', $sig_block['headers'])) {
711 if (strlen($content) != $headers['content-length']) {
712 Logger::info('Content length does not match');
717 // Ensure that the authentication had been done with some content
718 // Without this check someone could authenticate with fakeable data
719 if (!$hasGoodSignedContent) {
720 Logger::info('No good signed content');
728 * fetches a key for a given id and actor
731 * @param string $actor
733 * @return array with actor url and public key
736 private static function fetchKey(string $id, string $actor): array
738 $url = (strpos($id, '#') ? substr($id, 0, strpos($id, '#')) : $id);
740 $profile = APContact::getByURL($url);
741 if (!empty($profile)) {
742 Logger::info('Taking key from id', ['id' => $id]);
743 return ['url' => $url, 'pubkey' => $profile['pubkey'], 'type' => $profile['type']];
744 } elseif ($url != $actor) {
745 $profile = APContact::getByURL($actor);
746 if (!empty($profile)) {
747 Logger::info('Taking key from actor', ['actor' => $actor]);
748 return ['url' => $actor, 'pubkey' => $profile['pubkey'], 'type' => $profile['type']];
752 Logger::notice('Key could not be fetched', ['url' => $url, 'actor' => $actor]);