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\Database\DBA;
25 use Friendica\Core\Logger;
27 use Friendica\Model\User;
28 use Friendica\Model\APContact;
31 * Implements HTTP Signatures per draft-cavage-http-signatures-07.
33 * Ported from Hubzilla: https://framagit.org/hubzilla/core/blob/master/Zotlabs/Web/HTTPSig.php
35 * Other parts of the code for HTTP signing are taken from the Osada project.
36 * https://framagit.org/macgirvin/osada
38 * @see https://tools.ietf.org/html/draft-cavage-http-signatures-07
43 // See draft-cavage-http-signatures-08
45 * Verifies a magic request
49 * @return array with verification data
50 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
52 public static function verifyMagic($key)
58 'header_signed' => false,
59 'header_valid' => false
62 // Decide if $data arrived via controller submission or curl.
64 $headers['(request-target)'] = strtolower($_SERVER['REQUEST_METHOD']).' '.$_SERVER['REQUEST_URI'];
66 foreach ($_SERVER as $k => $v) {
67 if (strpos($k, 'HTTP_') === 0) {
68 $field = str_replace('_', '-', strtolower(substr($k, 5)));
69 $headers[$field] = $v;
75 $sig_block = self::parseSigheader($headers['authorization']);
78 Logger::log('no signature provided.');
82 $result['header_signed'] = true;
84 $signed_headers = $sig_block['headers'];
85 if (!$signed_headers) {
86 $signed_headers = ['date'];
90 foreach ($signed_headers as $h) {
91 if (array_key_exists($h, $headers)) {
92 $signed_data .= $h . ': ' . $headers[$h] . "\n";
94 if (strpos($h, '.')) {
99 $signed_data = rtrim($signed_data, "\n");
101 $algorithm = 'sha512';
103 if ($key && function_exists($key)) {
104 $result['signer'] = $sig_block['keyId'];
105 $key = $key($sig_block['keyId']);
108 Logger::log('Got keyID ' . $sig_block['keyId'], Logger::DEBUG);
114 $x = Crypto::rsaVerify($signed_data, $sig_block['signature'], $key, $algorithm);
116 Logger::log('verified: ' . $x, Logger::DEBUG);
123 $result['header_valid'] = true;
131 * @param string $prvkey
132 * @param string $keyid (optional, default 'Key')
136 public static function createSig($head, $prvkey, $keyid = 'Key')
138 $return_headers = [];
141 $algorithm = 'rsa-sha512';
143 $x = self::sign($head, $prvkey, $alg);
145 $headerval = 'keyId="' . $keyid . '",algorithm="' . $algorithm
146 . '",headers="' . $x['headers'] . '",signature="' . $x['signature'] . '"';
148 $sighead = 'Authorization: Signature ' . $headerval;
151 foreach ($head as $k => $v) {
152 $return_headers[] = $k . ': ' . $v;
156 $return_headers[] = $sighead;
158 return $return_headers;
163 * @param string $prvkey
164 * @param string $alg (optional) default 'sha256'
168 private static function sign($head, $prvkey, $alg = 'sha256')
174 foreach ($head as $k => $v) {
175 $headers .= strtolower($k) . ': ' . trim($v) . "\n";
179 $fields .= strtolower($k);
181 // strip the trailing linefeed
182 $headers = rtrim($headers, "\n");
184 $sig = base64_encode(Crypto::rsaSign($headers, $prvkey, $alg));
186 $ret['headers'] = $fields;
187 $ret['signature'] = $sig;
193 * @param string $header
194 * @return array associate array with
195 * - \e string \b keyID
196 * - \e string \b algorithm
197 * - \e array \b headers
198 * - \e string \b signature
199 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
201 public static function parseSigheader($header)
206 // if the header is encrypted, decrypt with (default) site private key and continue
207 if (preg_match('/iv="(.*?)"/ism', $header, $matches)) {
208 $header = self::decryptSigheader($header);
211 if (preg_match('/keyId="(.*?)"/ism', $header, $matches)) {
212 $ret['keyId'] = $matches[1];
215 if (preg_match('/algorithm="(.*?)"/ism', $header, $matches)) {
216 $ret['algorithm'] = $matches[1];
218 $ret['algorithm'] = 'rsa-sha256';
221 if (preg_match('/headers="(.*?)"/ism', $header, $matches)) {
222 $ret['headers'] = explode(' ', $matches[1]);
225 if (preg_match('/signature="(.*?)"/ism', $header, $matches)) {
226 $ret['signature'] = base64_decode(preg_replace('/\s+/', '', $matches[1]));
229 if (!empty($ret['signature']) && !empty($ret['algorithm']) && empty($ret['headers'])) {
230 $ret['headers'] = ['date'];
237 * @param string $header
238 * @param string $prvkey (optional), if not set use site private key
240 * @return array|string associative array, empty string if failue
244 * - \e string \b data
245 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
247 private static function decryptSigheader($header, $prvkey = null)
249 $iv = $key = $alg = $data = null;
252 $prvkey = DI::config()->get('system', 'prvkey');
257 if (preg_match('/iv="(.*?)"/ism', $header, $matches)) {
261 if (preg_match('/key="(.*?)"/ism', $header, $matches)) {
265 if (preg_match('/alg="(.*?)"/ism', $header, $matches)) {
269 if (preg_match('/data="(.*?)"/ism', $header, $matches)) {
273 if ($iv && $key && $alg && $data) {
274 return Crypto::unencapsulate(['iv' => $iv, 'key' => $key, 'alg' => $alg, 'data' => $data], $prvkey);
281 * Functions for ActivityPub
285 * Transmit given data to a target for a user
287 * @param array $data Data that is about to be send
288 * @param string $target The URL of the inbox
289 * @param integer $uid User id of the sender
291 * @return boolean Was the transmission successful?
292 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
294 public static function transmit($data, $target, $uid)
296 $owner = User::getOwnerDataById($uid);
302 $content = json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
304 // Header data that is about to be signed.
305 $host = parse_url($target, PHP_URL_HOST);
306 $path = parse_url($target, PHP_URL_PATH);
307 $digest = 'SHA-256=' . base64_encode(hash('sha256', $content, true));
308 $content_length = strlen($content);
309 $date = DateTimeFormat::utcNow(DateTimeFormat::HTTP);
311 $headers = ['Date: ' . $date, 'Content-Length: ' . $content_length, 'Digest: ' . $digest, 'Host: ' . $host];
313 $signed_data = "(request-target): post " . $path . "\ndate: ". $date . "\ncontent-length: " . $content_length . "\ndigest: " . $digest . "\nhost: " . $host;
315 $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
317 $headers[] = 'Signature: keyId="' . $owner['url'] . '#main-key' . '",algorithm="rsa-sha256",headers="(request-target) date content-length digest host",signature="' . $signature . '"';
319 $headers[] = 'Content-Type: application/activity+json';
321 $postResult = Network::post($target, $content, $headers);
322 $return_code = $postResult->getReturnCode();
324 Logger::log('Transmit to ' . $target . ' returned ' . $return_code, Logger::DEBUG);
326 $success = ($return_code >= 200) && ($return_code <= 299);
328 self::setInboxStatus($target, $success);
334 * Set the delivery status for a given inbox
336 * @param string $url The URL of the inbox
337 * @param boolean $success Transmission status
339 static private function setInboxStatus($url, $success)
341 $now = DateTimeFormat::utcNow();
343 $status = DBA::selectFirst('inbox-status', [], ['url' => $url]);
344 if (!DBA::isResult($status)) {
345 DBA::insert('inbox-status', ['url' => $url, 'created' => $now]);
346 $status = DBA::selectFirst('inbox-status', [], ['url' => $url]);
350 $fields = ['success' => $now];
352 $fields = ['failure' => $now];
355 if ($status['failure'] > DBA::NULL_DATETIME) {
356 $new_previous_stamp = strtotime($status['failure']);
357 $old_previous_stamp = strtotime($status['previous']);
359 // Only set "previous" with at least one day difference.
360 // We use this to assure to not accidentally archive too soon.
361 if (($new_previous_stamp - $old_previous_stamp) >= 86400) {
362 $fields['previous'] = $status['failure'];
367 if ($status['success'] <= DBA::NULL_DATETIME) {
368 $stamp1 = strtotime($status['created']);
370 $stamp1 = strtotime($status['success']);
373 $stamp2 = strtotime($now);
374 $previous_stamp = strtotime($status['previous']);
376 // Archive the inbox when there had been failures for five days.
377 // Additionally ensure that at least one previous attempt has to be in between.
378 if ((($stamp2 - $stamp1) >= 86400 * 5) && ($previous_stamp > $stamp1)) {
379 $fields['archive'] = true;
382 $fields['archive'] = false;
385 DBA::update('inbox-status', $fields, ['url' => $url]);
389 * Fetches JSON data for a user
391 * @param string $request request url
392 * @param integer $uid User id of the requester
394 * @return array JSON array
395 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
397 public static function fetch($request, $uid)
399 $opts = ['accept_content' => 'application/activity+json, application/ld+json'];
400 $curlResult = self::fetchRaw($request, $uid, false, $opts);
402 if (empty($curlResult)) {
406 if (!$curlResult->isSuccess() || empty($curlResult->getBody())) {
410 $content = json_decode($curlResult->getBody(), true);
411 if (empty($content) || !is_array($content)) {
419 * Fetches raw data for a user
421 * @param string $request request url
422 * @param integer $uid User id of the requester
423 * @param boolean $binary TRUE if asked to return binary results (file download) (default is "false")
424 * @param array $opts (optional parameters) assoziative array with:
425 * 'accept_content' => supply Accept: header with 'accept_content' as the value
426 * 'timeout' => int Timeout in seconds, default system config value or 60 seconds
427 * 'http_auth' => username:password
428 * 'novalidate' => do not validate SSL certs, default is to validate using our CA list
429 * 'nobody' => only return the header
430 * 'cookiejar' => path to cookie jar file
432 * @return object CurlResult
433 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
435 public static function fetchRaw($request, $uid = 0, $binary = false, $opts = [])
438 $owner = User::getOwnerDataById($uid);
443 // Header data that is about to be signed.
444 $host = parse_url($request, PHP_URL_HOST);
445 $path = parse_url($request, PHP_URL_PATH);
446 $date = DateTimeFormat::utcNow(DateTimeFormat::HTTP);
448 $headers = ['Date: ' . $date, 'Host: ' . $host];
450 $signed_data = "(request-target): get " . $path . "\ndate: ". $date . "\nhost: " . $host;
452 $signature = base64_encode(Crypto::rsaSign($signed_data, $owner['uprvkey'], 'sha256'));
454 $headers[] = 'Signature: keyId="' . $owner['url'] . '#main-key' . '",algorithm="rsa-sha256",headers="(request-target) date host",signature="' . $signature . '"';
459 if (!empty($opts['accept_content'])) {
460 $headers[] = 'Accept: ' . $opts['accept_content'];
464 $curl_opts['header'] = $headers;
466 $curlResult = Network::curl($request, false, $curl_opts);
467 $return_code = $curlResult->getReturnCode();
469 Logger::log('Fetched for user ' . $uid . ' from ' . $request . ' returned ' . $return_code, Logger::DEBUG);
475 * Gets a signer from a given HTTP request
478 * @param $http_headers
480 * @return string Signer
481 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
483 public static function getSigner($content, $http_headers)
485 if (empty($http_headers['HTTP_SIGNATURE'])) {
489 if (!empty($content)) {
490 $object = json_decode($content, true);
491 if (empty($object)) {
495 $actor = JsonLD::fetchElement($object, 'actor', 'id');
501 $headers['(request-target)'] = strtolower($http_headers['REQUEST_METHOD']) . ' ' . $http_headers['REQUEST_URI'];
503 // First take every header
504 foreach ($http_headers as $k => $v) {
505 $field = str_replace('_', '-', strtolower($k));
506 $headers[$field] = $v;
509 // Now add every http header
510 foreach ($http_headers as $k => $v) {
511 if (strpos($k, 'HTTP_') === 0) {
512 $field = str_replace('_', '-', strtolower(substr($k, 5)));
513 $headers[$field] = $v;
517 $sig_block = self::parseSigHeader($http_headers['HTTP_SIGNATURE']);
519 if (empty($sig_block) || empty($sig_block['headers']) || empty($sig_block['keyId'])) {
524 foreach ($sig_block['headers'] as $h) {
525 if (array_key_exists($h, $headers)) {
526 $signed_data .= $h . ': ' . $headers[$h] . "\n";
529 $signed_data = rtrim($signed_data, "\n");
531 if (empty($signed_data)) {
537 if ($sig_block['algorithm'] === 'rsa-sha256') {
538 $algorithm = 'sha256';
541 if ($sig_block['algorithm'] === 'rsa-sha512') {
542 $algorithm = 'sha512';
545 if (empty($algorithm)) {
549 $key = self::fetchKey($sig_block['keyId'], $actor);
555 if (!Crypto::rsaVerify($signed_data, $sig_block['signature'], $key['pubkey'], $algorithm)) {
559 $hasGoodSignedContent = false;
561 // Check the digest when it is part of the signed data
562 if (!empty($content) && in_array('digest', $sig_block['headers'])) {
563 $digest = explode('=', $headers['digest'], 2);
564 if ($digest[0] === 'SHA-256') {
567 if ($digest[0] === 'SHA-512') {
571 /// @todo add all hashes from the rfc
573 if (!empty($hashalg) && base64_encode(hash($hashalg, $content, true)) != $digest[1]) {
577 $hasGoodSignedContent = true;
580 // Check if the signed date field is in an acceptable range
581 if (in_array('date', $sig_block['headers'])) {
582 $diff = abs(strtotime($headers['date']) - time());
584 Logger::log("Header date '" . $headers['date'] . "' is with " . $diff . " seconds out of the 300 second frame. The signature is invalid.");
587 $hasGoodSignedContent = true;
590 // Check the content-length when it is part of the signed data
591 if (in_array('content-length', $sig_block['headers'])) {
592 if (strlen($content) != $headers['content-length']) {
597 // Ensure that the authentication had been done with some content
598 // Without this check someone could authenticate with fakeable data
599 if (!$hasGoodSignedContent) {
607 * fetches a key for a given id and actor
612 * @return array with actor url and public key
615 private static function fetchKey($id, $actor)
617 $url = (strpos($id, '#') ? substr($id, 0, strpos($id, '#')) : $id);
619 $profile = APContact::getByURL($url);
620 if (!empty($profile)) {
621 Logger::log('Taking key from id ' . $id, Logger::DEBUG);
622 return ['url' => $url, 'pubkey' => $profile['pubkey']];
623 } elseif ($url != $actor) {
624 $profile = APContact::getByURL($actor);
625 if (!empty($profile)) {
626 Logger::log('Taking key from actor ' . $actor, Logger::DEBUG);
627 return ['url' => $actor, 'pubkey' => $profile['pubkey']];