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