aboutsummaryrefslogtreecommitdiffstats
path: root/Zotlabs/Zot6/Receiver.php
blob: b276cbe31604fe45f0a2d1932cece8f6a9e11f1b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php

namespace Zotlabs\Zot6;

use Zotlabs\Lib\Config;
use Zotlabs\Lib\Crypto;
use Zotlabs\Lib\Libzot;
use Zotlabs\Web\HTTPSig;

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;

	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;
		$this->prvkey      = Config::get('system','prvkey');
		$this->hub         = null;

		if($localdata) {
			$this->rawdata = $localdata;
		}
		else {
			$this->rawdata = file_get_contents('php://input');

			// All access to the zot endpoint must use http signatures

			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);
		}
		else {
			$this->error = true;
			$this->response['message'] = 'no data';
		}

		logger('received_json: ' . json_encode($this->data,JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES), LOGGER_DATA);

		logger('received: ' . print_r($this->data,true), LOGGER_DATA);

		if ($this->data && is_array($this->data)) {
			$this->encrypted = ((array_key_exists('encrypted',$this->data) && intval($this->data['encrypted'])) ? true : false);

			if ($this->encrypted && $this->prvkey) {
				$uncrypted = Crypto::unencapsulate($this->data,$this->prvkey);

				// openssl_decrypt() will sometimes return garbage instead of false when
				// a wrong key is used. This can happen in case of hub re-installs.
				// Hence also check with str_starts_with().
				if ($uncrypted && str_starts_with($uncrypted, '{')) {
					$this->data = json_decode($uncrypted,true);
				}
				else {
					$this->error = true;
					$this->response['message'] = 'no data (decryption failed)';
				}
			}
		}
	}


	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();
	}

	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;
    }


	function Valid_Httpsig() {

		$result = false;

		$this->sigdata = HTTPSig::verify($this->rawdata, EMPTY_STR, 'zot6');

		if ($this->sigdata && $this->sigdata['header_signed'] && $this->sigdata['header_valid']) {
			$result = true;

			// It is OK to not have signed content - not all messages provide content.
			// But if it is signed, it has to be valid

			if (($this->sigdata['content_signed']) && (! $this->sigdata['content_valid'])) {
					$result = false;
			}
		}
		return $result;
	}

	function Dispatch() {

		switch ($this->messagetype) {
			case 'purge':
				$this->response = $this->handler->Purge($this->sender, $this->recipients, $this->hub);
				break;

			case 'refresh':
				$this->response = $this->handler->Refresh($this->sender, $this->recipients, $this->hub, false);
				break;

			case 'force_refresh':
				$this->response = $this->handler->Refresh($this->sender, $this->recipients, $this->hub, true);
				break;

			case 'rekey':
				$this->response = $this->handler->Rekey($this->sender, $this->data, $this->hub);
				break;

			case 'activity':
			case 'response': // upstream message
			case 'sync':
			default:
				if ($this->sender) {
					$this->response = $this->handler->Notify($this->data, $this->hub);
				}
				break;

		}

		logger('response_to_return: ' . print_r($this->response,true),LOGGER_DATA);

		if ($this->encrypted) {
			$this->EncryptResponse();
		}

		return($this->response);
	}

	function EncryptResponse() {
		$algorithm = Libzot::best_algorithm($this->hub['site_crypto']);
		if ($algorithm) {
			$this->response = Crypto::encapsulate(json_encode($this->response),$this->hub['hubloc_sitekey'], $algorithm);
		}
	}

}