streams/Code/Nomad/Receiver.php

234 lines
6.9 KiB
PHP
Raw Normal View History

2018-04-23 01:51:46 +00:00
<?php
2022-02-16 04:50:35 +00:00
namespace Code\Nomad;
2018-04-23 01:51:46 +00:00
2022-02-16 04:08:28 +00:00
use Code\Lib\Config;
use Code\Lib\Libzot;
use Code\Lib\Crypto;
use Code\Web\HTTPSig;
2018-04-23 01:51:46 +00:00
2021-12-02 23:02:31 +00:00
class Receiver
{
protected $data;
protected $encrypted;
protected $error;
protected $messagetype;
protected $sender;
protected $site_id;
protected $validated;
protected $recipients;
protected $response;
protected $handler;
protected $prvkey;
protected $rawdata;
protected $sigdata;
2022-08-15 10:33:19 +00:00
/**
* @var array|mixed
*/
private mixed $hub;
2021-12-02 23:02:31 +00:00
public function __construct($handler, $localdata = null)
{
$this->error = false;
$this->validated = false;
$this->messagetype = '';
$this->response = ['success' => false];
$this->handler = $handler;
$this->data = null;
$this->rawdata = null;
$this->site_id = null;
2022-09-24 21:37:08 +00:00
$this->prvkey = Config::Get('system', 'prvkey');
2021-12-02 23:02:31 +00:00
if ($localdata) {
$this->rawdata = $localdata;
} else {
$this->rawdata = file_get_contents('php://input');
2023-08-15 10:11:46 +00:00
// All access to the nomad endpoint must use http signatures
2021-12-02 23:02:31 +00:00
if (!$this->Valid_Httpsig()) {
logger('signature failed');
$this->error = true;
$this->response['message'] = 'signature invalid';
return;
}
}
logger('received raw: ' . print_r($this->rawdata, true), LOGGER_DATA);
if ($this->rawdata) {
$this->data = json_decode($this->rawdata, true);
2022-09-24 21:37:08 +00:00
if (($this->data) && (!is_array($this->data)) && (str_starts_with($this->data, "{"))) {
2021-12-02 23:02:31 +00:00
// Multiple json encoding has been seen in the wild and needs to be fixed on the sending side.
// Proceed anyway and log the event with a backtrace.
btlogger('multiple encoding detected');
$this->data = json_decode($this->data, true);
}
} else {
$this->error = true;
$this->response['message'] = 'no data';
}
logger('received_json: ' . json_encode($this->data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES), LOGGER_DATA);
if ($this->data && is_array($this->data)) {
2022-08-15 10:33:19 +00:00
$this->encrypted = array_key_exists('encrypted', $this->data) && intval($this->data['encrypted']);
2021-12-02 23:02:31 +00:00
if ($this->encrypted && $this->prvkey) {
$uncrypted = Crypto::unencapsulate($this->data, $this->prvkey);
if ($uncrypted) {
$this->data = json_decode($uncrypted, true);
} else {
$this->error = true;
$this->response['message'] = 'no data';
}
}
}
2018-04-23 01:51:46 +00:00
}
2018-05-30 04:08:52 +00:00
2021-12-02 23:02:31 +00:00
public function run()
{
if ($this->error) {
// make timing attacks on the decryption engine a bit more difficult
usleep(mt_rand(10000, 100000));
return ($this->response);
}
if ($this->data) {
if (array_key_exists('type', $this->data)) {
$this->messagetype = $this->data['type'];
}
if (!$this->messagetype) {
$this->error = true;
$this->response['message'] = 'no datatype';
return $this->response;
}
$this->sender = ((array_key_exists('sender', $this->data)) ? $this->data['sender'] : null);
$this->recipients = ((array_key_exists('recipients', $this->data)) ? $this->data['recipients'] : null);
$this->site_id = ((array_key_exists('site_id', $this->data)) ? $this->data['site_id'] : null);
}
if ($this->sender) {
$result = $this->ValidateSender();
if (!$result) {
$this->error = true;
return $this->response;
}
}
return $this->Dispatch();
}
public function ValidateSender()
{
$hub = Libzot::valid_hub($this->sender, $this->site_id);
if (!$hub) {
$x = Libzot::register_hub($this->sigdata['signer']);
if ($x['success']) {
$hub = Libzot::valid_hub($this->sender, $this->site_id);
}
if (!$hub) {
$this->response['message'] = 'sender unknown';
return false;
}
}
if (!check_siteallowed($hub['hubloc_url'])) {
$this->response['message'] = 'forbidden';
return false;
}
if (!check_channelallowed($this->sender)) {
$this->response['message'] = 'forbidden';
return false;
}
Libzot::update_hub_connected($hub, $this->site_id);
$this->validated = true;
$this->hub = $hub;
return true;
}
public function Valid_Httpsig()
{
2018-05-30 04:08:52 +00:00
2021-12-02 23:02:31 +00:00
$result = false;
2018-05-30 04:08:52 +00:00
2021-12-02 23:02:31 +00:00
$this->sigdata = HTTPSig::verify($this->rawdata, EMPTY_STR, 'zot6');
2018-07-30 03:19:02 +00:00
2021-12-02 23:02:31 +00:00
if ($this->sigdata && $this->sigdata['header_signed'] && $this->sigdata['header_valid']) {
$result = true;
2018-05-30 06:52:08 +00:00
2021-12-02 23:02:31 +00:00
// It is OK to not have signed content - not all messages provide content.
// But if it is signed, it has to be valid
2018-05-30 06:52:08 +00:00
2021-12-02 23:02:31 +00:00
if (($this->sigdata['content_signed']) && (!$this->sigdata['content_valid'])) {
$result = false;
}
}
return $result;
}
public function Dispatch()
{
2018-04-23 01:51:46 +00:00
2021-12-02 23:02:31 +00:00
switch ($this->messagetype) {
case 'purge':
$this->response = $this->handler->Purge($this->sender, $this->recipients, $this->hub);
break;
2018-04-23 01:51:46 +00:00
2021-12-02 23:02:31 +00:00
case 'refresh':
$this->response = $this->handler->Refresh($this->sender, $this->recipients, $this->hub, false);
break;
2018-04-23 01:51:46 +00:00
2021-12-02 23:02:31 +00:00
case 'force_refresh':
$this->response = $this->handler->Refresh($this->sender, $this->recipients, $this->hub, true);
break;
2018-04-23 01:51:46 +00:00
2021-12-02 23:02:31 +00:00
case 'rekey':
$this->response = $this->handler->Rekey($this->sender, $this->data, $this->hub);
break;
2018-06-26 03:55:53 +00:00
2021-12-02 23:02:31 +00:00
case 'activity':
case 'response': // upstream message
case 'sync':
default:
// Only accept these message types with a valid sender
if ($this->sender) {
$this->response = $this->handler->Notify($this->data, $this->hub);
}
break;
}
2018-07-30 06:49:37 +00:00
2021-12-02 23:02:31 +00:00
logger('response_to_return: ' . print_r($this->response, true), LOGGER_DATA);
2018-06-25 01:54:29 +00:00
2021-12-02 23:02:31 +00:00
if ($this->encrypted) {
$this->EncryptResponse();
}
2018-06-25 01:54:29 +00:00
2021-12-02 23:02:31 +00:00
return ($this->response);
}
public function EncryptResponse()
{
if (!empty($this->hub)) {
$algorithm = Libzot::best_algorithm($this->hub['site_crypto']);
if ($algorithm) {
$this->response = Crypto::encapsulate(json_encode($this->response), $this->hub['hubloc_sitekey'], $algorithm);
}
2021-12-02 23:02:31 +00:00
}
}
2018-04-23 01:51:46 +00:00
}