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