]> git.meshlink.io Git - meshlink/blob - src/protocol_key.c
2dcb5e8d66debc9d8e814e53d3e624598e01fbbf
[meshlink] / 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         if(seen_request(mesh, request)) {
49                 return true;
50         }
51
52         n = lookup_node(mesh, name);
53
54         if(!n) {
55                 logger(mesh, MESHLINK_ERROR, "Got %s from %s origin %s which does not exist", "KEY_CHANGED", c->name, name);
56                 return true;
57         }
58
59         /* Tell the others */
60
61         forward_request(mesh, c, NULL, request);
62
63         return true;
64 }
65
66 static bool send_initial_sptps_data(void *handle, uint8_t type, const void *data, size_t len) {
67         (void)type;
68
69         assert(data);
70         assert(len);
71
72         node_t *to = handle;
73         meshlink_handle_t *mesh = to->mesh;
74
75         if(!to->nexthop || !to->nexthop->connection) {
76                 logger(mesh, MESHLINK_WARNING, "Cannot send SPTPS data to %s via %s", to->name, to->nexthop ? to->nexthop->name : to->name);
77                 return false;
78         }
79
80         to->sptps.send_data = send_sptps_data;
81         char buf[len * 4 / 3 + 5];
82         b64encode(data, buf, len);
83         return send_request(mesh, to->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_KEY, buf);
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, NULL, "%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         char label[sizeof(meshlink_udp_label) + strlen(mesh->self->name) + strlen(to->name) + 2];
106         snprintf(label, sizeof(label), "%s %s %s", meshlink_udp_label, mesh->self->name, to->name);
107         sptps_stop(&to->sptps);
108         to->status.validkey = false;
109         to->status.waitingforkey = true;
110         to->last_req_key = mesh->loop.now.tv_sec;
111         return sptps_start(&to->sptps, to, true, true, mesh->private_key, to->ecdsa, label, sizeof(label) - 1, send_initial_sptps_data, receive_sptps_record);
112 }
113
114 /* REQ_KEY is overloaded to allow arbitrary requests to be routed between two nodes. */
115
116 static bool req_key_ext_h(meshlink_handle_t *mesh, connection_t *c, const char *request, node_t *from, int reqno) {
117         (void)c;
118
119         if(!from->nexthop || !from->nexthop->connection) {
120                 logger(mesh, MESHLINK_WARNING, "Cannot answer REQ_KEY from %s via %s", from->name, from->nexthop ? from->nexthop->name : from->name);
121                 return true;
122         }
123
124         switch(reqno) {
125         case REQ_PUBKEY: {
126                 char *pubkey = ecdsa_get_base64_public_key(mesh->private_key);
127
128                 if(!node_read_public_key(mesh, from)) {
129                         char hiskey[MAX_STRING_SIZE];
130
131                         if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, hiskey) == 1) {
132                                 from->ecdsa = ecdsa_set_base64_public_key(hiskey);
133
134                                 if(!from->ecdsa) {
135                                         logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_PUBKEY", from->name, "invalid pubkey");
136                                         return true;
137                                 }
138                         }
139                 }
140
141                 send_request(mesh, from->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, from->name, ANS_PUBKEY, pubkey);
142                 free(pubkey);
143                 return true;
144         }
145
146         case ANS_PUBKEY: {
147                 if(node_read_public_key(mesh, from)) {
148                         logger(mesh, MESHLINK_WARNING, "Got ANS_PUBKEY from %s even though we already have his pubkey", from->name);
149                         return true;
150                 }
151
152                 char pubkey[MAX_STRING_SIZE];
153
154                 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, pubkey) != 1 || !(from->ecdsa = ecdsa_set_base64_public_key(pubkey))) {
155                         logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ANS_PUBKEY", from->name, "invalid pubkey");
156                         return true;
157                 }
158
159                 logger(mesh, MESHLINK_INFO, "Learned ECDSA public key from %s", from->name);
160                 from->status.dirty = true;
161
162                 /* If we are trying to form an outgoing connection to this node, retry immediately */
163                 for list_each(outgoing_t, outgoing, mesh->outgoings) {
164                         if(outgoing->node == from && outgoing->ev.cb) {
165                                 outgoing->timeout = 0;
166                                 timeout_set(&mesh->loop, &outgoing->ev, &(struct timespec) {
167                                         0, 0
168                                 });
169                         }
170                 }
171
172                 /* Also reset any UTCP timers */
173                 utcp_reset_timers(from->utcp);
174
175                 return true;
176         }
177
178         case REQ_KEY: {
179                 if(!node_read_public_key(mesh, from)) {
180                         logger(mesh, MESHLINK_DEBUG, "No ECDSA key known for %s", from->name);
181                         send_request(mesh, from->nexthop->connection, NULL, "%d %s %s %d", REQ_KEY, mesh->self->name, from->name, REQ_PUBKEY);
182                         return true;
183                 }
184
185                 if(from->sptps.label) {
186                         logger(mesh, MESHLINK_DEBUG, "Got REQ_KEY from %s while we already started a SPTPS session!", from->name);
187
188                         if(mesh->loop.now.tv_sec < from->last_req_key + req_key_timeout && strcmp(mesh->self->name, from->name) < 0) {
189                                 logger(mesh, MESHLINK_DEBUG, "Ignoring REQ_KEY from %s.", from->name);
190                                 return true;
191                         }
192                 }
193
194                 char buf[MAX_STRING_SIZE];
195                 int len;
196
197                 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
198                         logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_SPTPS_START", from->name, "invalid SPTPS data");
199                         return true;
200                 }
201
202                 char label[sizeof(meshlink_udp_label) + strlen(from->name) + strlen(mesh->self->name) + 2];
203                 snprintf(label, sizeof(label), "%s %s %s", meshlink_udp_label, from->name, mesh->self->name);
204                 sptps_stop(&from->sptps);
205                 from->status.validkey = false;
206                 from->status.waitingforkey = true;
207                 from->last_req_key = mesh->loop.now.tv_sec;
208
209                 if(!sptps_start(&from->sptps, from, false, true, mesh->private_key, from->ecdsa, label, sizeof(label) - 1, send_sptps_data, receive_sptps_record)) {
210                         logger(mesh, MESHLINK_ERROR, "Could not start SPTPS session with %s: %s", from->name, strerror(errno));
211                         return true;
212                 }
213
214                 if(!sptps_receive_data(&from->sptps, buf, len)) {
215                         logger(mesh, MESHLINK_ERROR, "Could not process SPTPS data from %s: %s", from->name, strerror(errno));
216                         return true;
217                 }
218
219                 return true;
220         }
221
222         case REQ_SPTPS: {
223                 if(!from->status.validkey) {
224                         logger(mesh, MESHLINK_ERROR, "Got REQ_SPTPS from %s but we don't have a valid key yet", from->name);
225                         return true;
226                 }
227
228                 char buf[MAX_STRING_SIZE];
229                 int len;
230
231                 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
232                         logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_SPTPS", from->name, "invalid SPTPS data");
233                         return true;
234                 }
235
236                 if(!sptps_receive_data(&from->sptps, buf, len)) {
237                         logger(mesh, MESHLINK_ERROR, "Could not process SPTPS data from %s: %s", from->name, strerror(errno));
238                         return true;
239                 }
240
241                 return true;
242         }
243
244         default:
245                 logger(mesh, MESHLINK_ERROR, "Unknown extended REQ_KEY request from %s: %s", from->name, request);
246                 return true;
247         }
248 }
249
250 bool req_key_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
251         assert(request);
252         assert(*request);
253
254         char from_name[MAX_STRING_SIZE];
255         char to_name[MAX_STRING_SIZE];
256         node_t *from, *to;
257         int reqno = 0;
258
259         if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING " %d", from_name, to_name, &reqno) < 2) {
260                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "REQ_KEY", c->name);
261                 return false;
262         }
263
264         if(!check_id(from_name) || !check_id(to_name)) {
265                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_KEY", c->name, "invalid name");
266                 return false;
267         }
268
269         from = lookup_node(mesh, from_name);
270
271         if(!from) {
272                 logger(mesh, MESHLINK_ERROR, "Got %s from %s origin %s which does not exist in our connection list",
273                        "REQ_KEY", c->name, from_name);
274                 return true;
275         }
276
277         to = lookup_node(mesh, to_name);
278
279         if(!to) {
280                 logger(mesh, MESHLINK_ERROR, "Got %s from %s destination %s which does not exist in our connection list",
281                        "REQ_KEY", c->name, to_name);
282                 return true;
283         }
284
285         /* Check if this key request is for us */
286
287         if(to == mesh->self) {                      /* Yes */
288                 /* Is this an extended REQ_KEY message? */
289                 if(reqno) {
290                         return req_key_ext_h(mesh, c, request, from, reqno);
291                 }
292
293                 /* This should never happen. Ignore it, unless it came directly from the connected peer, in which case we disconnect. */
294                 return from->connection != c;
295         } else {
296                 if(!to->status.reachable || !to->nexthop || !to->nexthop->connection) {
297                         logger(mesh, MESHLINK_WARNING, "Got %s from %s destination %s which is not reachable",
298                                "REQ_KEY", c->name, to_name);
299                         return true;
300                 }
301
302                 send_request(mesh, to->nexthop->connection, NULL, "%s", request);
303         }
304
305         return true;
306 }
307
308 bool ans_key_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
309         assert(request);
310         assert(*request);
311
312         char from_name[MAX_STRING_SIZE];
313         char to_name[MAX_STRING_SIZE];
314         char key[MAX_STRING_SIZE];
315         char address[MAX_STRING_SIZE] = "";
316         char port[MAX_STRING_SIZE] = "";
317         int cipher, digest, maclength, compression;
318         node_t *from, *to;
319
320         if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
321                         from_name, to_name, key, &cipher, &digest, &maclength,
322                         &compression, address, port) < 7) {
323                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ANS_KEY", c->name);
324                 return false;
325         }
326
327         if(!check_id(from_name) || !check_id(to_name)) {
328                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ANS_KEY", c->name, "invalid name");
329                 return false;
330         }
331
332         from = lookup_node(mesh, from_name);
333
334         if(!from) {
335                 logger(mesh, MESHLINK_ERROR, "Got %s from %s origin %s which does not exist in our connection list",
336                        "ANS_KEY", c->name, from_name);
337                 return true;
338         }
339
340         to = lookup_node(mesh, to_name);
341
342         if(!to) {
343                 logger(mesh, MESHLINK_ERROR, "Got %s from %s destination %s which does not exist in our connection list",
344                        "ANS_KEY", c->name, to_name);
345                 return true;
346         }
347
348         /* Forward it if necessary */
349
350         if(to != mesh->self) {
351                 if(!to->status.reachable) {
352                         logger(mesh, MESHLINK_WARNING, "Got %s from %s destination %s which is not reachable",
353                                "ANS_KEY", c->name, to_name);
354                         return true;
355                 }
356
357                 if(from == to) {
358                         logger(mesh, MESHLINK_WARNING, "Got %s from %s from %s to %s",
359                                "ANS_KEY", c->name, from_name, to_name);
360                         return true;
361                 }
362
363                 if(!to->nexthop || !to->nexthop->connection) {
364                         logger(mesh, MESHLINK_WARNING, "Cannot forward ANS_KEY to %s via %s", to->name, to->nexthop ? to->nexthop->name : to->name);
365                         return false;
366                 }
367
368                 /* Append the known UDP address of the from node, if we have a confirmed one */
369                 if(!*address && from->status.udp_confirmed && from->address.sa.sa_family != AF_UNSPEC) {
370                         char *reflexive_address, *reflexive_port;
371                         logger(mesh, MESHLINK_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
372                         sockaddr2str(&from->address, &reflexive_address, &reflexive_port);
373                         send_request(mesh, to->nexthop->connection, NULL, "%s %s %s", request, reflexive_address, reflexive_port);
374                         free(reflexive_address);
375                         free(reflexive_port);
376                         return true;
377                 }
378
379                 return send_request(mesh, to->nexthop->connection, NULL, "%s", request);
380         }
381
382         /* Is this an ANS_KEY informing us of our own reflexive UDP address? */
383
384         if(from == mesh->self) {
385                 if(*key == '.' && *address && *port) {
386                         logger(mesh, MESHLINK_DEBUG, "Learned our own reflexive UDP address from %s: %s port %s", c->name, address, port);
387
388                         /* Inform all other nodes we want to communicate with and which are reachable via this connection */
389                         for splay_each(node_t, n, mesh->nodes) {
390                                 if(n->nexthop != c->node) {
391                                         continue;
392                                 }
393
394                                 if(n->status.udp_confirmed) {
395                                         continue;
396                                 }
397
398                                 if(!n->status.waitingforkey && !n->status.validkey) {
399                                         continue;
400                                 }
401
402                                 if(!n->nexthop->connection) {
403                                         continue;
404                                 }
405
406                                 logger(mesh, MESHLINK_DEBUG, "Forwarding our own reflexive UDP address to %s", n->name);
407                                 send_request(mesh, c, NULL, "%d %s %s . -1 -1 -1 0 %s %s", ANS_KEY, mesh->self->name, n->name, address, port);
408                         }
409                 } else {
410                         logger(mesh, MESHLINK_WARNING, "Got %s from %s from %s to %s",
411                                "ANS_KEY", c->name, from_name, to_name);
412                 }
413
414                 return true;
415         }
416
417         /* Process SPTPS data if present */
418
419         if(*key != '.') {
420                 /* Don't use key material until every check has passed. */
421                 from->status.validkey = false;
422
423                 /* Compression is not supported. */
424                 if(compression != 0) {
425                         logger(mesh, MESHLINK_ERROR, "Node %s uses bogus compression level!", from->name);
426                         return true;
427                 }
428
429                 char buf[strlen(key)];
430                 int len = b64decode(key, buf, strlen(key));
431
432                 if(!len || !sptps_receive_data(&from->sptps, buf, len)) {
433                         logger(mesh, MESHLINK_ERROR, "Error processing SPTPS data from %s", from->name);
434                 }
435         }
436
437         if(from->status.validkey) {
438                 if(*address && *port) {
439                         logger(mesh, MESHLINK_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
440                         sockaddr_t sa = str2sockaddr(address, port);
441                         update_node_udp(mesh, from, &sa);
442                 }
443
444                 send_mtu_probe(mesh, from);
445         }
446
447         return true;
448 }