]> git.meshlink.io Git - meshlink/blob - src/protocol_key.c
Speed up initial autoconnect after joining a mesh.
[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 void send_key_changed(meshlink_handle_t *mesh) {
35         send_request(mesh, mesh->everyone, NULL, "%d %x %s", KEY_CHANGED, rand(), mesh->self->name);
36
37         /* Force key exchange for connections using SPTPS */
38
39         for splay_each(node_t, n, mesh->nodes)
40                 if(n->status.reachable && n->status.validkey) {
41                         sptps_force_kex(&n->sptps);
42                 }
43 }
44
45 bool key_changed_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
46         char name[MAX_STRING_SIZE];
47         node_t *n;
48
49         if(sscanf(request, "%*d %*x " MAX_STRING, name) != 1) {
50                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "KEY_CHANGED", c->name);
51                 return false;
52         }
53
54         if(seen_request(mesh, request)) {
55                 return true;
56         }
57
58         n = lookup_node(mesh, name);
59
60         if(!n) {
61                 logger(mesh, MESHLINK_ERROR, "Got %s from %s origin %s which does not exist", "KEY_CHANGED", c->name, name);
62                 return true;
63         }
64
65         /* Tell the others */
66
67         forward_request(mesh, c, NULL, request);
68
69         return true;
70 }
71
72 static bool send_initial_sptps_data(void *handle, uint8_t type, const void *data, size_t len) {
73         (void)type;
74         node_t *to = handle;
75         meshlink_handle_t *mesh = to->mesh;
76         to->sptps.send_data = send_sptps_data;
77         char buf[len * 4 / 3 + 5];
78         b64encode(data, buf, len);
79         return send_request(mesh, to->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_KEY, buf);
80 }
81
82 bool send_req_key(meshlink_handle_t *mesh, node_t *to) {
83         if(!node_read_public_key(mesh, to)) {
84                 logger(mesh, MESHLINK_DEBUG, "No ECDSA key known for %s", to->name);
85                 send_request(mesh, to->nexthop->connection, NULL, "%d %s %s %d", REQ_KEY, mesh->self->name, to->name, REQ_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         to->incompression = mesh->self->incompression;
100         return sptps_start(&to->sptps, to, true, true, mesh->private_key, to->ecdsa, label, sizeof(label) - 1, send_initial_sptps_data, receive_sptps_record);
101 }
102
103 /* REQ_KEY is overloaded to allow arbitrary requests to be routed between two nodes. */
104
105 static bool req_key_ext_h(meshlink_handle_t *mesh, connection_t *c, const char *request, node_t *from, int reqno) {
106         (void)c;
107
108         switch(reqno) {
109         case REQ_PUBKEY: {
110                 char *pubkey = ecdsa_get_base64_public_key(mesh->private_key);
111
112                 if(!from->nexthop || !from->nexthop->connection) {
113                         return false;
114                 }
115
116                 send_request(mesh, from->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, from->name, ANS_PUBKEY, pubkey);
117                 free(pubkey);
118                 return true;
119         }
120
121         case ANS_PUBKEY: {
122                 if(node_read_public_key(mesh, from)) {
123                         logger(mesh, MESHLINK_WARNING, "Got ANS_PUBKEY from %s even though we already have his pubkey", from->name);
124                         return true;
125                 }
126
127                 char pubkey[MAX_STRING_SIZE];
128
129                 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, pubkey) != 1 || !(from->ecdsa = ecdsa_set_base64_public_key(pubkey))) {
130                         logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ANS_PUBKEY", from->name, "invalid pubkey");
131                         return true;
132                 }
133
134                 logger(mesh, MESHLINK_INFO, "Learned ECDSA public key from %s", from->name);
135                 from->status.dirty = true;
136
137                 /* If we are trying to form an outgoing connection to this node, retry immediately */
138                 for list_each(outgoing_t, outgoing, mesh->outgoings) {
139                         if(outgoing->node == from && outgoing->ev.cb) {
140                                 outgoing->timeout = 0;
141                                 timeout_set(&mesh->loop, &outgoing->ev, &(struct timeval) {
142                                         0, 0
143                                 });
144                         }
145                 }
146
147                 return true;
148         }
149
150         case REQ_KEY: {
151                 if(!node_read_public_key(mesh, from)) {
152                         logger(mesh, MESHLINK_DEBUG, "No ECDSA key known for %s", from->name);
153                         send_request(mesh, from->nexthop->connection, NULL, "%d %s %s %d", REQ_KEY, mesh->self->name, from->name, REQ_PUBKEY);
154                         return true;
155                 }
156
157                 if(from->sptps.label) {
158                         logger(mesh, MESHLINK_DEBUG, "Got REQ_KEY from %s while we already started a SPTPS session!", from->name);
159
160                         if(strcmp(mesh->self->name, from->name) < 0) {
161                                 logger(mesh, MESHLINK_DEBUG, "Ignoring REQ_KEY from %s.", from->name);
162                                 return true;
163                         }
164                 }
165
166                 char buf[MAX_STRING_SIZE];
167                 int len;
168
169                 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
170                         logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_SPTPS_START", from->name, "invalid SPTPS data");
171                         return true;
172                 }
173
174                 char label[sizeof(meshlink_udp_label) + strlen(from->name) + strlen(mesh->self->name) + 2];
175                 snprintf(label, sizeof(label), "%s %s %s", meshlink_udp_label, from->name, mesh->self->name);
176                 sptps_stop(&from->sptps);
177                 from->status.validkey = false;
178                 from->status.waitingforkey = true;
179                 from->last_req_key = mesh->loop.now.tv_sec;
180                 sptps_start(&from->sptps, from, false, true, mesh->private_key, from->ecdsa, label, sizeof(label) - 1, send_sptps_data, receive_sptps_record);
181                 sptps_receive_data(&from->sptps, buf, len);
182                 return true;
183         }
184
185         case REQ_SPTPS: {
186                 if(!from->status.validkey) {
187                         logger(mesh, MESHLINK_ERROR, "Got REQ_SPTPS from %s but we don't have a valid key yet", from->name);
188                         return true;
189                 }
190
191                 char buf[MAX_STRING_SIZE];
192                 int len;
193
194                 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
195                         logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_SPTPS", from->name, "invalid SPTPS data");
196                         return true;
197                 }
198
199                 sptps_receive_data(&from->sptps, buf, len);
200                 return true;
201         }
202
203         default:
204                 logger(mesh, MESHLINK_ERROR, "Unknown extended REQ_KEY request from %s: %s", from->name, request);
205                 return true;
206         }
207 }
208
209 bool req_key_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
210         char from_name[MAX_STRING_SIZE];
211         char to_name[MAX_STRING_SIZE];
212         node_t *from, *to;
213         int reqno = 0;
214
215         if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING " %d", from_name, to_name, &reqno) < 2) {
216                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "REQ_KEY", c->name);
217                 return false;
218         }
219
220         if(!check_id(from_name) || !check_id(to_name)) {
221                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_KEY", c->name, "invalid name");
222                 return false;
223         }
224
225         from = lookup_node(mesh, from_name);
226
227         if(!from) {
228                 logger(mesh, MESHLINK_ERROR, "Got %s from %s origin %s which does not exist in our connection list",
229                        "REQ_KEY", c->name, from_name);
230                 return true;
231         }
232
233         to = lookup_node(mesh, to_name);
234
235         if(!to) {
236                 logger(mesh, MESHLINK_ERROR, "Got %s from %s destination %s which does not exist in our connection list",
237                        "REQ_KEY", c->name, to_name);
238                 return true;
239         }
240
241         /* Check if this key request is for us */
242
243         if(to == mesh->self) {                      /* Yes */
244                 /* Is this an extended REQ_KEY message? */
245                 if(reqno) {
246                         return req_key_ext_h(mesh, c, request, from, reqno);
247                 }
248
249                 /* This should never happen. Ignore it, unless it came directly from the connected peer, in which case we disconnect. */
250                 return from->connection != c;
251         } else {
252                 if(!to->status.reachable) {
253                         logger(mesh, MESHLINK_WARNING, "Got %s from %s destination %s which is not reachable",
254                                "REQ_KEY", c->name, to_name);
255                         return true;
256                 }
257
258                 send_request(mesh, to->nexthop->connection, NULL, "%s", request);
259         }
260
261         return true;
262 }
263
264 bool send_ans_key(meshlink_handle_t *mesh, node_t *to) {
265         (void)mesh;
266         (void)to;
267         abort();
268 }
269
270 bool ans_key_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
271         char from_name[MAX_STRING_SIZE];
272         char to_name[MAX_STRING_SIZE];
273         char key[MAX_STRING_SIZE];
274         char address[MAX_STRING_SIZE] = "";
275         char port[MAX_STRING_SIZE] = "";
276         int cipher, digest, maclength, compression;
277         node_t *from, *to;
278
279         if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
280                         from_name, to_name, key, &cipher, &digest, &maclength,
281                         &compression, address, port) < 7) {
282                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ANS_KEY", c->name);
283                 return false;
284         }
285
286         if(!check_id(from_name) || !check_id(to_name)) {
287                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ANS_KEY", c->name, "invalid name");
288                 return false;
289         }
290
291         from = lookup_node(mesh, from_name);
292
293         if(!from) {
294                 logger(mesh, MESHLINK_ERROR, "Got %s from %s origin %s which does not exist in our connection list",
295                        "ANS_KEY", c->name, from_name);
296                 return true;
297         }
298
299         to = lookup_node(mesh, to_name);
300
301         if(!to) {
302                 logger(mesh, MESHLINK_ERROR, "Got %s from %s destination %s which does not exist in our connection list",
303                        "ANS_KEY", c->name, to_name);
304                 return true;
305         }
306
307         /* Forward it if necessary */
308
309         if(to != mesh->self) {
310                 if(!to->status.reachable) {
311                         logger(mesh, MESHLINK_WARNING, "Got %s from %s destination %s which is not reachable",
312                                "ANS_KEY", c->name, to_name);
313                         return true;
314                 }
315
316                 if(!*address && from->address.sa.sa_family != AF_UNSPEC) {
317                         char *address, *port;
318                         logger(mesh, MESHLINK_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
319                         sockaddr2str(&from->address, &address, &port);
320                         send_request(mesh, to->nexthop->connection, NULL, "%s %s %s", request, address, port);
321                         free(address);
322                         free(port);
323                         return true;
324                 }
325
326                 return send_request(mesh, to->nexthop->connection, NULL, "%s", request);
327         }
328
329         /* Don't use key material until every check has passed. */
330         from->status.validkey = false;
331
332         /* Compression is not supported. */
333         if(compression != 0) {
334                 logger(mesh, MESHLINK_ERROR, "Node %s uses bogus compression level!", from->name);
335                 return true;
336         }
337
338         from->outcompression = compression;
339
340         /* SPTPS or old-style key exchange? */
341
342         char buf[strlen(key)];
343         int len = b64decode(key, buf, strlen(key));
344
345         if(!len || !sptps_receive_data(&from->sptps, buf, len)) {
346                 logger(mesh, MESHLINK_ERROR, "Error processing SPTPS data from %s", from->name);
347         }
348
349         if(from->status.validkey) {
350                 if(*address && *port) {
351                         logger(mesh, MESHLINK_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
352                         sockaddr_t sa = str2sockaddr(address, port);
353                         update_node_udp(mesh, from, &sa);
354                 }
355
356                 if(from->options & OPTION_PMTU_DISCOVERY && !(from->options & OPTION_TCPONLY)) {
357                         send_mtu_probe(mesh, from);
358                 }
359         }
360
361         return true;
362 }