]> git.meshlink.io Git - meshlink/blob - src/protocol_auth.c
Also renew SPTPS keys for meta-connections.
[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         if(type == SPTPS_HANDSHAKE) {
150                 // The peer should send its cookie first.
151                 return true;
152         }
153
154         if(mesh->inviter_commits_first) {
155                 if(type == 2 && len == 18 + 32 && !c->status.invitation_used) {
156                         return process_invitation(mesh, c, data);
157                 }
158         } else {
159                 if(type == 0 && len == 18 && !c->status.invitation_used) {
160                         return process_invitation(mesh, c, data);
161                 } else if(type == 1 && len == 32 && c->status.invitation_used) {
162                         return commit_invitation(mesh, c, data);
163                 }
164         }
165
166         return false;
167 }
168
169 bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
170         assert(request);
171         assert(*request);
172
173         char name[MAX_STRING_SIZE];
174
175         if(sscanf(request, "%*d " MAX_STRING " %d.%d", name, &c->protocol_major, &c->protocol_minor) < 2) {
176                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ID", c->name);
177                 return false;
178         }
179
180         /* Check if this is an invitation  */
181
182         if(name[0] == '?') {
183                 if(!mesh->invitation_key) {
184                         logger(mesh, MESHLINK_ERROR, "Got invitation from %s but we don't have an invitation key", c->name);
185                         return false;
186                 }
187
188                 c->ecdsa = ecdsa_set_base64_public_key(name + 1);
189
190                 if(!c->ecdsa) {
191                         logger(mesh, MESHLINK_ERROR, "Got bad invitation from %s", c->name);
192                         return false;
193                 }
194
195                 c->status.invitation = true;
196                 char *mykey = ecdsa_get_base64_public_key(mesh->invitation_key);
197
198                 if(!mykey) {
199                         return false;
200                 }
201
202                 if(!send_request(mesh, c, NULL, "%d %s", ACK, mykey)) {
203                         return false;
204                 }
205
206                 free(mykey);
207
208                 c->protocol_minor = 2;
209                 c->allow_request = 1;
210
211                 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);
212         }
213
214         /* Check if identity is a valid name */
215
216         if(!check_id(name)) {
217                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ID", c->name, "invalid name");
218                 return false;
219         }
220
221         /* If this is an outgoing connection, make sure we are connected to the right host */
222
223         if(c->outgoing) {
224                 if(strcmp(c->name, name)) {
225                         logger(mesh, MESHLINK_ERROR, "Peer is %s instead of %s", name, c->name);
226                         return false;
227                 }
228         } else {
229                 if(c->name) {
230                         free(c->name);
231                 }
232
233                 c->name = xstrdup(name);
234         }
235
236         /* Check if version matches */
237
238         if(c->protocol_major != PROT_MAJOR) {
239                 logger(mesh, MESHLINK_ERROR, "Peer %s uses incompatible version %d.%d",
240                        c->name, c->protocol_major, c->protocol_minor);
241                 return false;
242         }
243
244         /* Check if we know this node */
245
246         node_t *n = lookup_node(mesh, c->name);
247
248         if(!n) {
249                 logger(mesh, MESHLINK_ERROR, "Peer %s has unknown identity", c->name);
250                 return false;
251         }
252
253         if(n->status.blacklisted) {
254                 logger(mesh, MESHLINK_WARNING, "Peer %s is blacklisted", c->name);
255                 return false;
256         }
257
258         if(!node_read_public_key(mesh, n)) {
259                 logger(mesh, MESHLINK_ERROR, "No key known for peer %s", c->name);
260
261                 if(n->status.reachable && !n->status.waitingforkey) {
262                         logger(mesh, MESHLINK_INFO, "Requesting key from peer %s", c->name);
263                         send_req_key(mesh, n);
264                 }
265
266                 return false;
267         }
268
269         /* Forbid version rollback for nodes whose ECDSA key we know */
270
271         if(ecdsa_active(c->ecdsa) && c->protocol_minor < 2) {
272                 logger(mesh, MESHLINK_ERROR, "Peer %s tries to roll back protocol version to %d.%d",
273                        c->name, c->protocol_major, c->protocol_minor);
274                 return false;
275         }
276
277         c->allow_request = ACK;
278         char label[sizeof(meshlink_tcp_label) + strlen(mesh->self->name) + strlen(c->name) + 2];
279
280         if(c->outgoing) {
281                 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, mesh->self->name, c->name);
282         } else {
283                 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, c->name, mesh->self->name);
284         }
285
286         char buf1[1024], buf2[1024];
287         bin2hex((uint8_t *)mesh->private_key + 64, buf1, 32);
288         bin2hex((uint8_t *)n->ecdsa + 64, buf2, 32);
289         logger(mesh, MESHLINK_DEBUG, "Connection to %s mykey %s hiskey %s", c->name, buf1, buf2);
290         return sptps_start(&c->sptps, c, c->outgoing, false, mesh->private_key, n->ecdsa, label, sizeof(label) - 1, send_meta_sptps, receive_meta_sptps);
291 }
292
293 bool send_ack(meshlink_handle_t *mesh, connection_t *c) {
294         return send_request(mesh, c, NULL, "%d %s %d %x", ACK, mesh->myport, mesh->devclass, OPTION_PMTU_DISCOVERY | (PROT_MINOR << 24));
295 }
296
297 static void send_everything(meshlink_handle_t *mesh, connection_t *c) {
298         /* Send all known subnets and edges */
299
300         for splay_each(node_t, n, mesh->nodes) {
301                 for splay_each(edge_t, e, n->edge_tree) {
302                         send_add_edge(mesh, c, e, 0);
303                 }
304         }
305 }
306
307 bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
308         assert(request);
309         assert(*request);
310
311         char hisport[MAX_STRING_SIZE];
312         int devclass;
313         uint32_t options;
314         node_t *n;
315
316         if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &devclass, &options) != 3) {
317                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ACK", c->name);
318                 return false;
319         }
320
321         if(devclass < 0 || devclass >= DEV_CLASS_COUNT) {
322                 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ACK", c->name, "devclass invalid");
323                 return false;
324         }
325
326         /* Check if we already have a node_t for him */
327
328         n = lookup_node(mesh, c->name);
329
330         if(!n) {
331                 n = new_node();
332                 n->name = xstrdup(c->name);
333                 node_add(mesh, n);
334         } else {
335                 if(n->connection) {
336                         /* Oh dear, we already have a connection to this node. */
337                         logger(mesh, MESHLINK_DEBUG, "Established a second connection with %s, closing old connection", n->connection->name);
338
339                         if(n->connection->outgoing) {
340                                 if(c->outgoing) {
341                                         logger(mesh, MESHLINK_WARNING, "Two outgoing connections to the same node!");
342                                 } else {
343                                         c->outgoing = n->connection->outgoing;
344                                 }
345
346                                 n->connection->outgoing = NULL;
347                         }
348
349                         /* Remove the edge before terminating the connection, to prevent a graph update. */
350                         edge_del(mesh, n->connection->edge);
351                         n->connection->edge = NULL;
352
353                         terminate_connection(mesh, n->connection, false);
354                 }
355         }
356
357         n->devclass = devclass;
358         n->status.dirty = true;
359
360         n->last_successfull_connection = mesh->loop.now.tv_sec;
361
362         n->connection = c;
363         c->node = n;
364
365         /* Activate this connection */
366
367         c->allow_request = ALL;
368         c->last_key_renewal = mesh->loop.now.tv_sec;
369         c->status.active = true;
370
371         logger(mesh, MESHLINK_INFO, "Connection with %s activated", c->name);
372
373         /* Send him everything we know */
374
375         send_everything(mesh, c);
376
377         /* Create an edge_t for this connection */
378
379         assert(devclass >= 0 && devclass < DEV_CLASS_COUNT);
380
381         c->edge = new_edge();
382         c->edge->from = mesh->self;
383         c->edge->to = n;
384         sockaddrcpy_setport(&c->edge->address, &c->address, atoi(hisport));
385         c->edge->weight = mesh->dev_class_traits[devclass].edge_weight;
386         c->edge->connection = c;
387
388         edge_add(mesh, c->edge);
389
390         /* Notify everyone of the new edge */
391
392         send_add_edge(mesh, mesh->everyone, c->edge, 0);
393
394         /* Run MST and SSSP algorithms */
395
396         graph(mesh);
397
398         return true;
399 }