]> git.meshlink.io Git - meshlink-tiny/blob - src/protocol_key.c
c2d6036b5853da0e978fe0b463626831e26c0cd2
[meshlink-tiny] / src / protocol_key.c
1 /*
2     protocol_key.c -- handle the meta-protocol, key exchange
3     Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "connection.h"
23 #include "logger.h"
24 #include "meshlink_internal.h"
25 #include "net.h"
26 #include "netutl.h"
27 #include "node.h"
28 #include "prf.h"
29 #include "protocol.h"
30 #include "sptps.h"
31 #include "utils.h"
32 #include "xalloc.h"
33
34 static const int req_key_timeout = 2;
35
36 bool key_changed_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
37         assert(request);
38         assert(*request);
39
40         char name[MAX_STRING_SIZE];
41         node_t *n;
42
43         if(sscanf(request, "%*d %*x " MAX_STRING, name) != 1) {
44                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "KEY_CHANGED", c->name);
45                 return false;
46         }
47
48         n = lookup_node(mesh, name);
49
50         if(!n) {
51                 logger(mesh, MESHLINK_ERROR, "Got %s from %s origin %s which does not exist", "KEY_CHANGED", c->name, name);
52                 return true;
53         }
54
55         return true;
56 }
57
58 static bool send_initial_sptps_data(void *handle, uint8_t type, const void *data, size_t len) {
59         (void)type;
60
61         assert(data);
62         assert(len);
63
64         node_t *to = handle;
65         meshlink_handle_t *mesh = to->mesh;
66
67         if(!to->nexthop || !to->nexthop->connection) {
68                 logger(mesh, MESHLINK_WARNING, "Cannot send SPTPS data to %s via %s", to->name, to->nexthop ? to->nexthop->name : to->name);
69                 return false;
70         }
71
72         to->sptps.send_data = send_sptps_data;
73         char buf[len * 4 / 3 + 5];
74         b64encode(data, buf, len);
75         return send_request(mesh, to->nexthop->connection, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_KEY, buf);
76 }
77
78 bool send_canonical_address(meshlink_handle_t *mesh, node_t *to) {
79         if(!mesh->self->canonical_address) {
80                 return true;
81         }
82
83         return send_request(mesh, to->nexthop->connection, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_CANONICAL, mesh->self->canonical_address);
84 }
85
86 bool send_req_key(meshlink_handle_t *mesh, node_t *to) {
87         if(!node_read_public_key(mesh, to)) {
88                 logger(mesh, MESHLINK_DEBUG, "No ECDSA key known for %s", to->name);
89
90                 if(!to->nexthop || !to->nexthop->connection) {
91                         logger(mesh, MESHLINK_WARNING, "Cannot send REQ_PUBKEY to %s via %s", to->name, to->nexthop ? to->nexthop->name : to->name);
92                         return true;
93                 }
94
95                 char *pubkey = ecdsa_get_base64_public_key(mesh->private_key);
96                 send_request(mesh, to->nexthop->connection, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_PUBKEY, pubkey);
97                 free(pubkey);
98                 return true;
99         }
100
101         if(to->sptps.label) {
102                 logger(mesh, MESHLINK_DEBUG, "send_req_key(%s) called while sptps->label != NULL!", to->name);
103         }
104
105         /* Send our canonical address to help with UDP hole punching */
106         send_canonical_address(mesh, to);
107
108         char label[sizeof(meshlink_udp_label) + strlen(mesh->self->name) + strlen(to->name) + 2];
109         snprintf(label, sizeof(label), "%s %s %s", meshlink_udp_label, mesh->self->name, to->name);
110         sptps_stop(&to->sptps);
111         to->status.validkey = false;
112         to->status.waitingforkey = true;
113         to->last_req_key = mesh->loop.now.tv_sec;
114         return sptps_start(&to->sptps, to, true, true, mesh->private_key, to->ecdsa, label, sizeof(label) - 1, send_initial_sptps_data, receive_sptps_record);
115 }
116
117 /* REQ_KEY is overloaded to allow arbitrary requests to be routed between two nodes. */
118
119 static bool req_key_ext_h(meshlink_handle_t *mesh, connection_t *c, const char *request, node_t *from, int reqno) {
120         (void)c;
121
122         if(!from->nexthop || !from->nexthop->connection) {
123                 logger(mesh, MESHLINK_WARNING, "Cannot answer REQ_KEY from %s via %s", from->name, from->nexthop ? from->nexthop->name : from->name);
124                 return true;
125         }
126
127         switch(reqno) {
128         case REQ_PUBKEY: {
129                 char *pubkey = ecdsa_get_base64_public_key(mesh->private_key);
130
131                 if(!node_read_public_key(mesh, from)) {
132                         char hiskey[MAX_STRING_SIZE];
133
134                         if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, hiskey) == 1) {
135                                 from->ecdsa = ecdsa_set_base64_public_key(hiskey);
136
137                                 if(!from->ecdsa) {
138                                         logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_PUBKEY", from->name, "invalid pubkey");
139                                         return true;
140                                 }
141
142                                 logger(mesh, MESHLINK_INFO, "Learned ECDSA public key from %s", from->name);
143                                 from->status.dirty = true;
144
145                                 if(!node_write_config(mesh, from, true)) {
146                                         // ignore
147                                 }
148                         }
149                 }
150
151                 send_request(mesh, from->nexthop->connection, "%d %s %s %d %s", REQ_KEY, mesh->self->name, from->name, ANS_PUBKEY, pubkey);
152                 free(pubkey);
153                 return true;
154         }
155
156         case ANS_PUBKEY: {
157                 if(node_read_public_key(mesh, from)) {
158                         logger(mesh, MESHLINK_WARNING, "Got ANS_PUBKEY from %s even though we already have his pubkey", from->name);
159                         return true;
160                 }
161
162                 char pubkey[MAX_STRING_SIZE];
163
164                 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, pubkey) != 1 || !(from->ecdsa = ecdsa_set_base64_public_key(pubkey))) {
165                         logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ANS_PUBKEY", from->name, "invalid pubkey");
166                         return true;
167                 }
168
169                 logger(mesh, MESHLINK_INFO, "Learned ECDSA public key from %s", from->name);
170                 from->status.dirty = true;
171
172                 if(!node_write_config(mesh, from, true)) {
173                         // ignore
174                 }
175
176                 /* If we are trying to form an outgoing connection to this node, retry immediately */
177                 if(mesh->outgoing) {
178                         if(mesh->outgoing->node == from && mesh->outgoing->ev.cb) {
179                                 mesh->outgoing->timeout = 0;
180                                 timeout_set(&mesh->loop, &mesh->outgoing->ev, &(struct timespec) {
181                                         0, 0
182                                 });
183                         }
184                 }
185
186                 /* Also reset any UTCP timers */
187                 utcp_reset_timers(from->utcp);
188
189                 return true;
190         }
191
192         case REQ_KEY: {
193                 if(!node_read_public_key(mesh, from)) {
194                         logger(mesh, MESHLINK_DEBUG, "No ECDSA key known for %s", from->name);
195                         send_request(mesh, from->nexthop->connection, "%d %s %s %d", REQ_KEY, mesh->self->name, from->name, REQ_PUBKEY);
196                         return true;
197                 }
198
199                 if(from->sptps.label) {
200                         logger(mesh, MESHLINK_DEBUG, "Got REQ_KEY from %s while we already started a SPTPS session!", from->name);
201
202                         if(mesh->loop.now.tv_sec < from->last_req_key + req_key_timeout && strcmp(mesh->self->name, from->name) < 0) {
203                                 logger(mesh, MESHLINK_DEBUG, "Ignoring REQ_KEY from %s.", from->name);
204                                 return true;
205                         }
206                 }
207
208                 char buf[MAX_STRING_SIZE];
209                 int len;
210
211                 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
212                         logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_SPTPS_START", from->name, "invalid SPTPS data");
213                         return true;
214                 }
215
216                 char label[sizeof(meshlink_udp_label) + strlen(from->name) + strlen(mesh->self->name) + 2];
217                 snprintf(label, sizeof(label), "%s %s %s", meshlink_udp_label, from->name, mesh->self->name);
218                 sptps_stop(&from->sptps);
219                 from->status.validkey = false;
220                 from->status.waitingforkey = true;
221                 from->last_req_key = mesh->loop.now.tv_sec;
222
223                 /* Send our canonical address to help with UDP hole punching */
224                 send_canonical_address(mesh, from);
225
226                 if(!sptps_start(&from->sptps, from, false, true, mesh->private_key, from->ecdsa, label, sizeof(label) - 1, send_sptps_data, receive_sptps_record)) {
227                         logger(mesh, MESHLINK_ERROR, "Could not start SPTPS session with %s: %s", from->name, strerror(errno));
228                         return true;
229                 }
230
231                 if(!sptps_receive_data(&from->sptps, buf, len)) {
232                         logger(mesh, MESHLINK_ERROR, "Could not process SPTPS data from %s: %s", from->name, strerror(errno));
233                         return true;
234                 }
235
236                 return true;
237         }
238
239         case REQ_SPTPS: {
240                 if(!from->status.validkey) {
241                         logger(mesh, MESHLINK_ERROR, "Got REQ_SPTPS from %s but we don't have a valid key yet", from->name);
242                         return true;
243                 }
244
245                 char buf[MAX_STRING_SIZE];
246                 int len;
247
248                 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
249                         logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_SPTPS", from->name, "invalid SPTPS data");
250                         return true;
251                 }
252
253                 if(!sptps_receive_data(&from->sptps, buf, len)) {
254                         logger(mesh, MESHLINK_ERROR, "Could not process SPTPS data from %s: %s", from->name, strerror(errno));
255                         return true;
256                 }
257
258                 return true;
259         }
260
261         case REQ_CANONICAL: {
262                 char host[MAX_STRING_SIZE];
263                 char port[MAX_STRING_SIZE];
264
265                 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING " " MAX_STRING, host, port) != 2) {
266                         logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_CANONICAL", from->name, "invalid canonical address");
267                         return true;
268                 }
269
270                 char *canonical_address;
271                 xasprintf(&canonical_address, "%s %s", host, port);
272
273                 if(mesh->log_level <= MESHLINK_DEBUG && (!from->canonical_address || strcmp(from->canonical_address, canonical_address))) {
274                         logger(mesh, MESHLINK_DEBUG, "Updating canonical address of %s to %s", from->name, canonical_address);
275                 }
276
277                 free(from->canonical_address);
278                 from->canonical_address = canonical_address;
279                 return true;
280         }
281
282         default:
283                 logger(mesh, MESHLINK_ERROR, "Unknown extended REQ_KEY request from %s: %s", from->name, request);
284                 return true;
285         }
286 }
287
288 bool req_key_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
289         assert(request);
290         assert(*request);
291
292         char from_name[MAX_STRING_SIZE];
293         char to_name[MAX_STRING_SIZE];
294         node_t *from, *to;
295         int reqno = 0;
296
297         if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING " %d", from_name, to_name, &reqno) < 2) {
298                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "REQ_KEY", c->name);
299                 return false;
300         }
301
302         if(!check_id(from_name) || !check_id(to_name)) {
303                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_KEY", c->name, "invalid name");
304                 return false;
305         }
306
307         from = lookup_node(mesh, from_name);
308
309         if(!from) {
310                 logger(mesh, MESHLINK_ERROR, "Got %s from %s origin %s which does not exist in our connection list",
311                        "REQ_KEY", c->name, from_name);
312                 return true;
313         }
314
315         to = lookup_node(mesh, to_name);
316
317         if(!to) {
318                 logger(mesh, MESHLINK_ERROR, "Got %s from %s destination %s which does not exist in our connection list",
319                        "REQ_KEY", c->name, to_name);
320                 return true;
321         }
322
323         /* Check if this key request is for us */
324
325         if(to == mesh->self) {                      /* Yes */
326                 /* Is this an extended REQ_KEY message? */
327                 if(reqno) {
328                         return req_key_ext_h(mesh, c, request, from, reqno);
329                 }
330
331                 /* This should never happen. Ignore it, unless it came directly from the connected peer, in which case we disconnect. */
332                 return from->connection != c;
333         } else {
334                 if(!to->status.reachable || !to->nexthop || !to->nexthop->connection) {
335                         logger(mesh, MESHLINK_WARNING, "Got %s from %s destination %s which is not reachable",
336                                "REQ_KEY", c->name, to_name);
337                         return true;
338                 }
339
340                 send_request(mesh, to->nexthop->connection, "%s", request);
341         }
342
343         return true;
344 }
345
346 bool ans_key_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
347         assert(request);
348         assert(*request);
349
350         char from_name[MAX_STRING_SIZE];
351         char to_name[MAX_STRING_SIZE];
352         char key[MAX_STRING_SIZE];
353         char address[MAX_STRING_SIZE] = "";
354         char port[MAX_STRING_SIZE] = "";
355         int cipher, digest, maclength, compression;
356         node_t *from, *to;
357
358         if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
359                         from_name, to_name, key, &cipher, &digest, &maclength,
360                         &compression, address, port) < 7) {
361                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ANS_KEY", c->name);
362                 return false;
363         }
364
365         if(!check_id(from_name) || !check_id(to_name)) {
366                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ANS_KEY", c->name, "invalid name");
367                 return false;
368         }
369
370         from = lookup_node(mesh, from_name);
371
372         if(!from) {
373                 logger(mesh, MESHLINK_ERROR, "Got %s from %s origin %s which does not exist in our connection list",
374                        "ANS_KEY", c->name, from_name);
375                 return true;
376         }
377
378         to = lookup_node(mesh, to_name);
379
380         if(!to) {
381                 logger(mesh, MESHLINK_ERROR, "Got %s from %s destination %s which does not exist in our connection list",
382                        "ANS_KEY", c->name, to_name);
383                 return true;
384         }
385
386         /* Forward it if necessary */
387
388         if(to != mesh->self) {
389                 logger(mesh, MESHLINK_WARNING, "Got %s from %s destination %s which is not for us",
390                        "ANS_KEY", c->name, to_name);
391                 return true;
392         }
393
394         /* Is this an ANS_KEY informing us of our own reflexive UDP address? */
395
396         if(from == mesh->self) {
397                 if(*key == '.' && *address && *port) {
398                         /* Ignore reflexive UDP address */
399                 } else {
400                         logger(mesh, MESHLINK_WARNING, "Got %s from %s from %s to %s",
401                                "ANS_KEY", c->name, from_name, to_name);
402                 }
403
404                 return true;
405         }
406
407         /* Process SPTPS data if present */
408
409         if(*key != '.') {
410                 /* Don't use key material until every check has passed. */
411                 from->status.validkey = false;
412
413                 /* Compression is not supported. */
414                 if(compression != 0) {
415                         logger(mesh, MESHLINK_ERROR, "Node %s uses bogus compression level!", from->name);
416                         return true;
417                 }
418
419                 char buf[strlen(key)];
420                 int len = b64decode(key, buf, strlen(key));
421
422                 if(!len || !sptps_receive_data(&from->sptps, buf, len)) {
423                         logger(mesh, MESHLINK_ERROR, "Error processing SPTPS data from %s", from->name);
424                 }
425         }
426
427         if(from->status.validkey) {
428                 if(*address && *port) {
429                         /* Ignore reflexive UDP address */
430                 }
431         }
432
433         return true;
434 }