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