3 * @copyright Copyright (C) 2020, Friendica
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\Database\Database;
26 use Friendica\Database\DBA;
28 use Friendica\Model\APContact;
29 use Friendica\Model\Contact;
30 use Friendica\Model\User;
33 * Implements HTTP Signatures per draft-cavage-http-signatures-07.
35 * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Web/HTTPSig.php
37 * Other parts of the code for HTTP signing are taken from the Osada project.
38 * https://framagit.org/macgirvin/osada
40 * @see https://tools.ietf.org/html/draft-cavage-http-signatures-07
45 // See draft-cavage-http-signatures-08
47 * Verifies a magic request
51 * @return array with verification data
52 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
54 public static function verifyMagic($key)
60 'header_signed' => false,
61 'header_valid' => false
64 // Decide if $data arrived via controller submission or curl.
66 $headers['(request-target)'] = strtolower($_SERVER['REQUEST_METHOD']).' '.$_SERVER['REQUEST_URI'];
68 foreach ($_SERVER as $k => $v) {
69 if (strpos($k, 'HTTP_') === 0) {
70 $field = str_replace('_', '-', strtolower(substr($k, 5)));
71 $headers[$field] = $v;
77 $sig_block = self::parseSigheader($headers['authorization']);
80 Logger::log('no signature provided.');
84 $result['header_signed'] = true;
86 $signed_headers = $sig_block['headers'];
87 if (!$signed_headers) {
88 $signed_headers = ['date'];
92 foreach ($signed_headers as $h) {
93 if (array_key_exists($h, $headers)) {
94 $signed_data .= $h . ': ' . $headers[$h] . "\n";
96 if (strpos($h, '.')) {
101 $signed_data = rtrim($signed_data, "\n");
103 $algorithm = 'sha512';
105 if ($key && function_exists($key)) {
106 $result['signer'] = $sig_block['keyId'];
107 $key = $key($sig_block['keyId']);
110 Logger::log('Got keyID ' . $sig_block['keyId'], Logger::DEBUG);
116 $x = Crypto::rsaVerify($signed_data, $sig_block['signature'], $key, $algorithm);
118 Logger::log('verified: ' . $x, Logger::DEBUG);
125 $result['header_valid'] = true;
133 * @param string $prvkey
134 * @param string $keyid (optional, default 'Key')
138 public static function createSig($head, $prvkey, $keyid = 'Key')
140 $return_headers = [];
143 $algorithm = 'rsa-sha512';
145 $x = self::sign($head, $prvkey, $alg);
147 $headerval = 'keyId="' . $keyid . '",algorithm="' . $algorithm
148 . '",headers="' . $x['headers'] . '",signature="' . $x['signature'] . '"';
150 $sighead = 'Authorization: Signature ' . $headerval;
153 foreach ($head as $k => $v) {
154 $return_headers[] = $k . ': ' . $v;
158 $return_headers[] = $sighead;
160 return $return_headers;
165 * @param string $prvkey
166 * @param string $alg (optional) default 'sha256'
170 private static function sign($head, $prvkey, $alg = 'sha256')
176 foreach ($head as $k => $v) {
177 $headers .= strtolower($k) . ': ' . trim($v) . "\n";
181 $fields .= strtolower($k);
183 // strip the trailing linefeed
184 $headers = rtrim($headers, "\n");
186 $sig = base64_encode(Crypto::rsaSign($headers, $prvkey, $alg));
188 $ret['headers'] = $fields;
189 $ret['signature'] = $sig;
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
205 public static function parseSigheader($header)
207 // Remove obsolete folds
208 $header = preg_replace('/\n\s+/', ' ', $header);
210 $token = "[!#$%&'*+.^_`|~0-9A-Za-z-]";
212 $quotedString = '"(?:\\\\.|[^"\\\\])*"';
214 $regex = "/($token+)=($quotedString|$token+)/ism";
217 preg_match_all($regex, $header, $matches, PREG_SET_ORDER);
220 foreach ($matches as $match) {
221 $headers[$match[1]] = trim($match[2] ?: $match[3], '"');
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);
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'] ?? '')),
239 if (!empty($return['signature']) && !empty($return['algorithm']) && empty($return['headers'])) {
240 $return['headers'] = ['date'];
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
252 private static function decryptSigheader(array $headers, string $prvkey)
254 if (!empty($headers['iv']) && !empty($headers['key']) && !empty($headers['data'])) {
255 return Crypto::unencapsulate($headers, $prvkey);
262 * Functions for ActivityPub
266 * Transmit given data to a target for a user
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
272 * @return boolean Was the transmission successful?
273 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
275 public static function transmit($data, $target, $uid)
277 $owner = User::getOwnerDataById($uid);
283 $content = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
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);
292 $headers = ['Date: ' . $date, 'Content-Length: ' . $content_length, 'Digest: ' . $digest, 'Host: ' . $host];
294 $signed_data = "(request-target): post " . $path . "\ndate: ". $date . "\ncontent-length: " . $content_length . "\ndigest: " . $digest . "\nhost: " . $host;
296 $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
298 $headers[] = 'Signature: keyId="' . $owner['url'] . '#main-key' . '",algorithm="rsa-sha256",headers="(request-target) date content-length digest host",signature="' . $signature . '"';
300 $headers[] = 'Content-Type: application/activity+json';
302 $postResult = DI::httpRequest()->post($target, $content, $headers);
303 $return_code = $postResult->getReturnCode();
305 Logger::log('Transmit to ' . $target . ' returned ' . $return_code, Logger::DEBUG);
307 $success = ($return_code >= 200) && ($return_code <= 299);
309 self::setInboxStatus($target, $success);
315 * Set the delivery status for a given inbox
317 * @param string $url The URL of the inbox
318 * @param boolean $success Transmission status
319 * @param boolean $shared The inbox is a shared inbox
321 static public function setInboxStatus($url, $success, $shared = false)
323 $now = DateTimeFormat::utcNow();
325 $status = DBA::selectFirst('inbox-status', [], ['url' => $url]);
326 if (!DBA::isResult($status)) {
327 DBA::insert('inbox-status', ['url' => $url, 'created' => $now, 'shared' => $shared], Database::INSERT_IGNORE);
328 $status = DBA::selectFirst('inbox-status', [], ['url' => $url]);
332 $fields = ['success' => $now];
334 $fields = ['failure' => $now];
337 if ($status['failure'] > DBA::NULL_DATETIME) {
338 $new_previous_stamp = strtotime($status['failure']);
339 $old_previous_stamp = strtotime($status['previous']);
341 // Only set "previous" with at least one day difference.
342 // We use this to assure to not accidentally archive too soon.
343 if (($new_previous_stamp - $old_previous_stamp) >= 86400) {
344 $fields['previous'] = $status['failure'];
349 if ($status['success'] <= DBA::NULL_DATETIME) {
350 $stamp1 = strtotime($status['created']);
352 $stamp1 = strtotime($status['success']);
355 $stamp2 = strtotime($now);
356 $previous_stamp = strtotime($status['previous']);
358 // Archive the inbox when there had been failures for five days.
359 // Additionally ensure that at least one previous attempt has to be in between.
360 if ((($stamp2 - $stamp1) >= 86400 * 5) && ($previous_stamp > $stamp1)) {
361 $fields['archive'] = true;
364 $fields['archive'] = false;
367 DBA::update('inbox-status', $fields, ['url' => $url]);
371 * Fetches JSON data for a user
373 * @param string $request request url
374 * @param integer $uid User id of the requester
376 * @return array JSON array
377 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
379 public static function fetch($request, $uid)
381 $curlResult = self::fetchRaw($request, $uid);
383 if (empty($curlResult)) {
387 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
391 $content = json_decode($curlResult->getBody(), true);
392 if (empty($content) || !is_array($content)) {
400 * Fetches raw data for a user
402 * @param string $request request url
403 * @param integer $uid User id of the requester
404 * @param boolean $binary TRUE if asked to return binary results (file download) (default is "false")
405 * @param array $opts (optional parameters) assoziative array with:
406 * 'accept_content' => supply Accept: header with 'accept_content' as the value
407 * 'timeout' => int Timeout in seconds, default system config value or 60 seconds
408 * 'nobody' => only return the header
409 * 'cookiejar' => path to cookie jar file
411 * @return object CurlResult
412 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
414 public static function fetchRaw($request, $uid = 0, $opts = ['accept_content' => 'application/activity+json, application/ld+json'])
419 $owner = User::getOwnerDataById($uid);
424 $owner = User::getSystemAccount();
430 if (!empty($owner['uprvkey'])) {
431 // Header data that is about to be signed.
432 $host = parse_url($request, PHP_URL_HOST);
433 $path = parse_url($request, PHP_URL_PATH);
434 $date = DateTimeFormat::utcNow(DateTimeFormat::HTTP);
436 $header = ['Date: ' . $date, 'Host: ' . $host];
438 $signed_data = "(request-target): get " . $path . "\ndate: ". $date . "\nhost: " . $host;
440 $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
442 $header[] = 'Signature: keyId="' . $owner['url'] . '#main-key' . '",algorithm="rsa-sha256",headers="(request-target) date host",signature="' . $signature . '"';
445 if (!empty($opts['accept_content'])) {
446 $header[] = 'Accept: ' . $opts['accept_content'];
450 $curl_opts['header'] = $header;
452 if (!empty($opts['nobody'])) {
453 $curlResult = DI::httpRequest()->head($request, $curl_opts);
455 $curlResult = DI::httpRequest()->get($request, $curl_opts);
457 $return_code = $curlResult->getReturnCode();
459 Logger::log('Fetched for user ' . $uid . ' from ' . $request . ' returned ' . $return_code, Logger::DEBUG);
465 * Gets a signer from a given HTTP request
468 * @param $http_headers
470 * @return string Signer
471 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
473 public static function getSigner($content, $http_headers)
475 if (empty($http_headers['HTTP_SIGNATURE'])) {
479 if (!empty($content)) {
480 $object = json_decode($content, true);
481 if (empty($object)) {
485 $actor = JsonLD::fetchElement($object, 'actor', 'id');
491 $headers['(request-target)'] = strtolower($http_headers['REQUEST_METHOD']) . ' ' . parse_url($http_headers['REQUEST_URI'], PHP_URL_PATH);
493 // First take every header
494 foreach ($http_headers as $k => $v) {
495 $field = str_replace('_', '-', strtolower($k));
496 $headers[$field] = $v;
499 // Now add every http header
500 foreach ($http_headers as $k => $v) {
501 if (strpos($k, 'HTTP_') === 0) {
502 $field = str_replace('_', '-', strtolower(substr($k, 5)));
503 $headers[$field] = $v;
507 $sig_block = self::parseSigHeader($http_headers['HTTP_SIGNATURE']);
509 if (empty($sig_block) || empty($sig_block['headers']) || empty($sig_block['keyId'])) {
514 foreach ($sig_block['headers'] as $h) {
515 if (array_key_exists($h, $headers)) {
516 $signed_data .= $h . ': ' . $headers[$h] . "\n";
519 $signed_data = rtrim($signed_data, "\n");
521 if (empty($signed_data)) {
527 // Wildcard value where signing algorithm should be derived from keyId
528 // @see https://tools.ietf.org/html/draft-ietf-httpbis-message-signatures-00#section-4.1
529 // Defaulting to SHA256 as it seems to be the prevalent implementation
530 // @see https://arewehs2019yet.vpzom.click
531 if ($sig_block['algorithm'] === 'hs2019') {
532 $algorithm = 'sha256';
535 if ($sig_block['algorithm'] === 'rsa-sha256') {
536 $algorithm = 'sha256';
539 if ($sig_block['algorithm'] === 'rsa-sha512') {
540 $algorithm = 'sha512';
543 if (empty($algorithm)) {
547 $key = self::fetchKey($sig_block['keyId'], $actor);
552 if (!empty($key['url']) && !empty($key['type']) && ($key['type'] == 'Tombstone')) {
553 Logger::info('Actor is a tombstone', ['key' => $key]);
555 // We now delete everything that we possibly knew from this actor
556 Contact::deleteContactByUrl($key['url']);
560 if (empty($key['pubkey'])) {
564 if (!Crypto::rsaVerify($signed_data, $sig_block['signature'], $key['pubkey'], $algorithm)) {
568 $hasGoodSignedContent = false;
570 // Check the digest when it is part of the signed data
571 if (!empty($content) && in_array('digest', $sig_block['headers'])) {
572 $digest = explode('=', $headers['digest'], 2);
573 if ($digest[0] === 'SHA-256') {
576 if ($digest[0] === 'SHA-512') {
580 /// @todo add all hashes from the rfc
582 if (!empty($hashalg) && base64_encode(hash($hashalg, $content, true)) != $digest[1]) {
586 $hasGoodSignedContent = true;
589 // Check if the signed date field is in an acceptable range
590 if (in_array('date', $sig_block['headers'])) {
591 $diff = abs(strtotime($headers['date']) - time());
593 Logger::log("Header date '" . $headers['date'] . "' is with " . $diff . " seconds out of the 300 second frame. The signature is invalid.");
596 $hasGoodSignedContent = true;
599 // Check the content-length when it is part of the signed data
600 if (in_array('content-length', $sig_block['headers'])) {
601 if (strlen($content) != $headers['content-length']) {
606 // Ensure that the authentication had been done with some content
607 // Without this check someone could authenticate with fakeable data
608 if (!$hasGoodSignedContent) {
616 * fetches a key for a given id and actor
621 * @return array with actor url and public key
624 private static function fetchKey($id, $actor)
626 $url = (strpos($id, '#') ? substr($id, 0, strpos($id, '#')) : $id);
628 $profile = APContact::getByURL($url);
629 if (!empty($profile)) {
630 Logger::log('Taking key from id ' . $id, Logger::DEBUG);
631 return ['url' => $url, 'pubkey' => $profile['pubkey'], 'type' => $profile['type']];
632 } elseif ($url != $actor) {
633 $profile = APContact::getByURL($actor);
634 if (!empty($profile)) {
635 Logger::log('Taking key from actor ' . $actor, Logger::DEBUG);
636 return ['url' => $actor, 'pubkey' => $profile['pubkey'], 'type' => $profile['type']];