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