]> git.meshlink.io Git - meshlink/blob - src/protocol_auth.c
7bf121989b308558a0305d29cb0478a943818e07
[meshlink] / src / protocol_auth.c
1 /*
2     protocol_auth.c -- handle the meta-protocol, authentication
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 "conf.h"
23 #include "connection.h"
24 #include "devtools.h"
25 #include "ecdsa.h"
26 #include "edge.h"
27 #include "graph.h"
28 #include "logger.h"
29 #include "meshlink_internal.h"
30 #include "meta.h"
31 #include "net.h"
32 #include "netutl.h"
33 #include "node.h"
34 #include "packmsg.h"
35 #include "prf.h"
36 #include "protocol.h"
37 #include "sptps.h"
38 #include "utils.h"
39 #include "xalloc.h"
40 #include "ed25519/sha512.h"
41
42 #include <assert.h>
43
44 extern bool node_write_devclass(meshlink_handle_t *mesh, node_t *n);
45
46 bool send_id(meshlink_handle_t *mesh, connection_t *c) {
47         return send_request(mesh, c, NULL, "%d %s %d.%d %s", ID, mesh->self->name, PROT_MAJOR, PROT_MINOR, mesh->appname);
48 }
49
50 static bool commit_invitation(meshlink_handle_t *mesh, connection_t *c, const void *data) {
51         // Create a new node
52         node_t *n = new_node();
53         n->name = xstrdup(c->name);
54         n->devclass = DEV_CLASS_UNKNOWN;
55         n->ecdsa = ecdsa_set_public_key(data);
56         n->submesh = c->submesh;
57
58         // Remember its current address
59         node_add_recent_address(mesh, n, &c->address);
60
61         if(!node_write_config(mesh, n) || !config_sync(mesh, "current")) {
62                 logger(mesh, MESHLINK_ERROR, "Error writing configuration file for invited node %s!\n", c->name);
63                 free_node(n);
64                 return false;
65
66         }
67
68         node_add(mesh, n);
69
70         logger(mesh, MESHLINK_INFO, "Key successfully received from %s", c->name);
71
72         //TODO: callback to application to inform of an accepted invitation
73
74         sptps_send_record(&c->sptps, 1, "", 0);
75
76         return true;
77 }
78
79 static bool process_invitation(meshlink_handle_t *mesh, connection_t *c, const void *data) {
80         // Recover the filename from the cookie and the key
81         char *fingerprint = ecdsa_get_base64_public_key(mesh->invitation_key);
82         char hash[64];
83         char hashbuf[18 + strlen(fingerprint)];
84         char cookie[25];
85         memcpy(hashbuf, data, 18);
86         memcpy(hashbuf + 18, fingerprint, sizeof(hashbuf) - 18);
87         sha512(hashbuf, sizeof(hashbuf), hash);
88         b64encode_urlsafe(hash, cookie, 18);
89         free(fingerprint);
90
91         config_t config;
92
93         if(!invitation_read(mesh, "current", cookie, &config, mesh->config_key)) {
94                 logger(mesh, MESHLINK_ERROR, "Error while trying to read invitation file\n");
95                 return false;
96         }
97
98         // Read the new node's Name from the file
99         packmsg_input_t in = {config.buf, config.len};
100         packmsg_get_uint32(&in); // skip version
101         free(c->name);
102         c->name = packmsg_get_str_dup(&in);
103
104         // Check if the file contains Sub-Mesh information
105         char *submesh_name = packmsg_get_str_dup(&in);
106
107         if(!strcmp(submesh_name, CORE_MESH)) {
108                 free(submesh_name);
109                 c->submesh = NULL;
110         } else {
111                 if(!check_id(submesh_name)) {
112                         logger(mesh, MESHLINK_ERROR, "Invalid invitation file %s\n", cookie);
113                         free(submesh_name);
114                         return false;
115                 }
116
117                 c->submesh = lookup_or_create_submesh(mesh, submesh_name);
118                 free(submesh_name);
119
120                 if(!c->submesh) {
121                         logger(mesh, MESHLINK_ERROR, "Unknown submesh in invitation file %s\n", cookie);
122                         return false;
123                 }
124         }
125
126         if(mesh->inviter_commits_first && !commit_invitation(mesh, c, (const char *)data + 18)) {
127                 return false;
128         }
129
130         if(mesh->inviter_commits_first) {
131                 devtool_set_inviter_commits_first(true);
132         }
133
134         // Send the node the contents of the invitation file
135         sptps_send_record(&c->sptps, 0, config.buf, config.len);
136
137         config_free(&config);
138
139         c->status.invitation_used = true;
140
141         logger(mesh, MESHLINK_INFO, "Invitation %s successfully sent to %s", cookie, c->name);
142         return true;
143 }
144
145 static bool receive_invitation_sptps(void *handle, uint8_t type, const void *data, uint16_t len) {
146         connection_t *c = handle;
147         meshlink_handle_t *mesh = c->mesh;
148
149         // Extend the time for the invitation exchange upon receiving a valid message
150         c->last_ping_time = mesh->loop.now.tv_sec;
151
152         if(type == SPTPS_HANDSHAKE) {
153                 // The peer should send its cookie first.
154                 return true;
155         }
156
157         if(mesh->inviter_commits_first) {
158                 if(type == 2 && len == 18 + 32 && !c->status.invitation_used) {
159                         return process_invitation(mesh, c, data);
160                 }
161         } else {
162                 if(type == 0 && len == 18 && !c->status.invitation_used) {
163                         return process_invitation(mesh, c, data);
164                 } else if(type == 1 && len == 32 && c->status.invitation_used) {
165                         return commit_invitation(mesh, c, data);
166                 }
167         }
168
169         return false;
170 }
171
172 bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
173         assert(request);
174         assert(*request);
175
176         char name[MAX_STRING_SIZE];
177
178         if(sscanf(request, "%*d " MAX_STRING " %d.%d", name, &c->protocol_major, &c->protocol_minor) < 2) {
179                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ID", c->name);
180                 return false;
181         }
182
183         /* Check if this is an invitation  */
184
185         if(name[0] == '?') {
186                 if(!mesh->invitation_key) {
187                         logger(mesh, MESHLINK_ERROR, "Got invitation from %s but we don't have an invitation key", c->name);
188                         return false;
189                 }
190
191                 c->ecdsa = ecdsa_set_base64_public_key(name + 1);
192
193                 if(!c->ecdsa) {
194                         logger(mesh, MESHLINK_ERROR, "Got bad invitation from %s", c->name);
195                         return false;
196                 }
197
198                 c->status.invitation = true;
199                 char *mykey = ecdsa_get_base64_public_key(mesh->invitation_key);
200
201                 if(!mykey) {
202                         return false;
203                 }
204
205                 if(!send_request(mesh, c, NULL, "%d %s", ACK, mykey)) {
206                         return false;
207                 }
208
209                 free(mykey);
210
211                 c->protocol_minor = 2;
212                 c->allow_request = 1;
213                 c->last_ping_time = mesh->loop.now.tv_sec;
214
215                 return sptps_start(&c->sptps, c, false, false, mesh->invitation_key, c->ecdsa, meshlink_invitation_label, sizeof(meshlink_invitation_label), send_meta_sptps, receive_invitation_sptps);
216         }
217
218         /* Check if identity is a valid name */
219
220         if(!check_id(name)) {
221                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ID", c->name, "invalid name");
222                 return false;
223         }
224
225         /* If this is an outgoing connection, make sure we are connected to the right host */
226
227         if(c->outgoing) {
228                 if(strcmp(c->name, name)) {
229                         logger(mesh, MESHLINK_ERROR, "Peer is %s instead of %s", name, c->name);
230                         return false;
231                 }
232         } else {
233                 if(c->name) {
234                         free(c->name);
235                 }
236
237                 c->name = xstrdup(name);
238         }
239
240         /* Check if version matches */
241
242         if(c->protocol_major != PROT_MAJOR) {
243                 logger(mesh, MESHLINK_ERROR, "Peer %s uses incompatible version %d.%d",
244                        c->name, c->protocol_major, c->protocol_minor);
245                 return false;
246         }
247
248         /* Check if we know this node */
249
250         node_t *n = lookup_node(mesh, c->name);
251
252         if(!n) {
253                 logger(mesh, MESHLINK_ERROR, "Peer %s has unknown identity", c->name);
254                 return false;
255         }
256
257         if(n->status.blacklisted) {
258                 logger(mesh, MESHLINK_WARNING, "Peer %s is blacklisted", c->name);
259                 return false;
260         }
261
262         if(!node_read_public_key(mesh, n)) {
263                 logger(mesh, MESHLINK_ERROR, "No key known for peer %s", c->name);
264
265                 if(n->status.reachable && !n->status.waitingforkey) {
266                         logger(mesh, MESHLINK_INFO, "Requesting key from peer %s", c->name);
267                         send_req_key(mesh, n);
268                 }
269
270                 return false;
271         }
272
273         /* Forbid version rollback for nodes whose ECDSA key we know */
274
275         if(ecdsa_active(c->ecdsa) && c->protocol_minor < 2) {
276                 logger(mesh, MESHLINK_ERROR, "Peer %s tries to roll back protocol version to %d.%d",
277                        c->name, c->protocol_major, c->protocol_minor);
278                 return false;
279         }
280
281         c->allow_request = ACK;
282         c->last_ping_time = mesh->loop.now.tv_sec;
283         char label[sizeof(meshlink_tcp_label) + strlen(mesh->self->name) + strlen(c->name) + 2];
284
285         if(c->outgoing) {
286                 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, mesh->self->name, c->name);
287         } else {
288                 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, c->name, mesh->self->name);
289         }
290
291         char buf1[1024], buf2[1024];
292         bin2hex((uint8_t *)mesh->private_key + 64, buf1, 32);
293         bin2hex((uint8_t *)n->ecdsa + 64, buf2, 32);
294         logger(mesh, MESHLINK_DEBUG, "Connection to %s mykey %s hiskey %s", c->name, buf1, buf2);
295         return sptps_start(&c->sptps, c, c->outgoing, false, mesh->private_key, n->ecdsa, label, sizeof(label) - 1, send_meta_sptps, receive_meta_sptps);
296 }
297
298 bool send_ack(meshlink_handle_t *mesh, connection_t *c) {
299         c->last_ping_time = mesh->loop.now.tv_sec;
300         return send_request(mesh, c, NULL, "%d %s %d %x", ACK, mesh->myport, mesh->devclass, OPTION_PMTU_DISCOVERY | (PROT_MINOR << 24));
301 }
302
303 static void send_everything(meshlink_handle_t *mesh, connection_t *c) {
304         /* Send all known subnets and edges */
305
306         for splay_each(node_t, n, mesh->nodes) {
307                 for inner_splay_each(edge_t, e, n->edge_tree) {
308                         send_add_edge(mesh, c, e, 0);
309                 }
310         }
311 }
312
313 bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
314         assert(request);
315         assert(*request);
316
317         char hisport[MAX_STRING_SIZE];
318         int devclass;
319         uint32_t options;
320         node_t *n;
321
322         if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &devclass, &options) != 3) {
323                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ACK", c->name);
324                 return false;
325         }
326
327         if(devclass < 0 || devclass >= DEV_CLASS_COUNT) {
328                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ACK", c->name, "devclass invalid");
329                 return false;
330         }
331
332         /* Check if we already have a node_t for him */
333
334         n = lookup_node(mesh, c->name);
335
336         if(!n) {
337                 n = new_node();
338                 n->name = xstrdup(c->name);
339                 node_add(mesh, n);
340         } else {
341                 if(n->connection) {
342                         /* Oh dear, we already have a connection to this node. */
343                         logger(mesh, MESHLINK_DEBUG, "Established a second connection with %s, closing old connection", n->connection->name);
344
345                         if(n->connection->outgoing) {
346                                 if(c->outgoing) {
347                                         logger(mesh, MESHLINK_WARNING, "Two outgoing connections to the same node!");
348                                 } else {
349                                         c->outgoing = n->connection->outgoing;
350                                 }
351
352                                 n->connection->outgoing = NULL;
353                         }
354
355                         /* Remove the edge before terminating the connection, to prevent a graph update. */
356                         edge_del(mesh, n->connection->edge);
357                         n->connection->edge = NULL;
358
359                         terminate_connection(mesh, n->connection, false);
360                 }
361         }
362
363         n->devclass = devclass;
364         n->status.dirty = true;
365
366         n->last_successfull_connection = mesh->loop.now.tv_sec;
367
368         n->connection = c;
369         n->nexthop = n;
370         c->node = n;
371
372         /* Activate this connection */
373
374         c->allow_request = ALL;
375         c->last_key_renewal = mesh->loop.now.tv_sec;
376         c->status.active = true;
377
378         logger(mesh, MESHLINK_INFO, "Connection with %s activated", c->name);
379
380         if(mesh->meta_status_cb) {
381                 mesh->meta_status_cb(mesh, (meshlink_node_t *)n, true);
382         }
383
384         /*  Terminate any connections to this node that are not activated yet */
385
386         for list_each(connection_t, other, mesh->connections) {
387                 if(!other->status.active && !strcmp(other->name, c->name)) {
388                         if(other->outgoing) {
389                                 if(c->outgoing) {
390                                         logger(mesh, MESHLINK_WARNING, "Two outgoing connections to the same node!");
391                                 } else {
392                                         c->outgoing = other->outgoing;
393                                 }
394
395                                 other->outgoing = NULL;
396                         }
397
398                         logger(mesh, MESHLINK_DEBUG, "Terminating pending second connection with %s", n->name);
399                         terminate_connection(mesh, other, false);
400                 }
401         }
402
403         /* Send him everything we know */
404
405         send_everything(mesh, c);
406
407         /* Create an edge_t for this connection */
408
409         assert(devclass >= 0 && devclass < DEV_CLASS_COUNT);
410
411         c->edge = new_edge();
412         c->edge->from = mesh->self;
413         c->edge->to = n;
414         sockaddrcpy_setport(&c->edge->address, &c->address, atoi(hisport));
415         c->edge->weight = mesh->dev_class_traits[devclass].edge_weight;
416         c->edge->connection = c;
417
418         node_add_recent_address(mesh, n, &c->address);
419         edge_add(mesh, c->edge);
420
421         /* Notify everyone of the new edge */
422
423         send_add_edge(mesh, mesh->everyone, c->edge, 0);
424
425         /* Run MST and SSSP algorithms */
426
427         graph(mesh);
428
429         /* Request a session key to jump start UDP traffic */
430
431         if(c->status.initiator) {
432                 send_req_key(mesh, n);
433         }
434
435         return true;
436 }