2 protocol_auth.c -- handle the meta-protocol, authentication
3 Copyright (C) 2014-2017 Guus Sliepen <guus@meshlink.io>
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.
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.
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.
23 #include "connection.h"
29 #include "meshlink_internal.h"
40 #include "ed25519/sha512.h"
44 extern bool node_write_devclass(meshlink_handle_t *mesh, node_t *n);
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);
50 static bool commit_invitation(meshlink_handle_t *mesh, connection_t *c, const void *data) {
51 // Check if the node is known
52 node_t *n = lookup_node(mesh, c->name);
55 if(n->status.blacklisted) {
56 logger(mesh, MESHLINK_ERROR, "Invitee %s is blacklisted", c->name);
58 logger(mesh, MESHLINK_ERROR, "Invitee %s already known", c->name);
66 n->name = xstrdup(c->name);
67 n->devclass = DEV_CLASS_UNKNOWN;
68 n->ecdsa = ecdsa_set_public_key(data);
69 n->submesh = c->submesh;
71 // Remember its current address
72 node_add_recent_address(mesh, n, &c->address);
74 if(!node_write_config(mesh, n, true) || !config_sync(mesh, "current")) {
75 logger(mesh, MESHLINK_ERROR, "Error writing configuration file for invited node %s!\n", c->name);
83 logger(mesh, MESHLINK_INFO, "Key successfully received from %s", c->name);
85 //TODO: callback to application to inform of an accepted invitation
87 sptps_send_record(&c->sptps, 1, "", 0);
92 static bool process_invitation(meshlink_handle_t *mesh, connection_t *c, const void *data) {
93 // Recover the filename from the cookie and the key
94 char *fingerprint = ecdsa_get_base64_public_key(mesh->invitation_key);
96 char hashbuf[18 + strlen(fingerprint)];
98 memcpy(hashbuf, data, 18);
99 memcpy(hashbuf + 18, fingerprint, sizeof(hashbuf) - 18);
100 sha512(hashbuf, sizeof(hashbuf), hash);
101 b64encode_urlsafe(hash, cookie, 18);
106 if(!invitation_read(mesh, "current", cookie, &config, mesh->config_key)) {
107 logger(mesh, MESHLINK_ERROR, "Error while trying to read invitation file\n");
111 // Read the new node's Name from the file
112 packmsg_input_t in = {config.buf, config.len};
113 packmsg_get_uint32(&in); // skip version
115 c->name = packmsg_get_str_dup(&in);
117 // Check if the file contains Sub-Mesh information
118 char *submesh_name = packmsg_get_str_dup(&in);
120 if(!strcmp(submesh_name, CORE_MESH)) {
124 if(!check_id(submesh_name)) {
125 logger(mesh, MESHLINK_ERROR, "Invalid invitation file %s\n", cookie);
130 c->submesh = lookup_or_create_submesh(mesh, submesh_name);
134 logger(mesh, MESHLINK_ERROR, "Unknown submesh in invitation file %s\n", cookie);
139 if(mesh->inviter_commits_first && !commit_invitation(mesh, c, (const char *)data + 18)) {
143 if(mesh->inviter_commits_first) {
144 devtool_set_inviter_commits_first(true);
147 // Send the node the contents of the invitation file
148 sptps_send_record(&c->sptps, 0, config.buf, config.len);
150 config_free(&config);
152 c->status.invitation_used = true;
154 logger(mesh, MESHLINK_INFO, "Invitation %s successfully sent to %s", cookie, c->name);
158 static bool receive_invitation_sptps(void *handle, uint8_t type, const void *data, uint16_t len) {
159 connection_t *c = handle;
160 meshlink_handle_t *mesh = c->mesh;
162 // Extend the time for the invitation exchange upon receiving a valid message
163 c->last_ping_time = mesh->loop.now.tv_sec;
165 if(type == SPTPS_HANDSHAKE) {
166 // The peer should send its cookie first.
170 if(mesh->inviter_commits_first) {
171 if(type == 2 && len == 18 + 32 && !c->status.invitation_used) {
172 return process_invitation(mesh, c, data);
175 if(type == 0 && len == 18 && !c->status.invitation_used) {
176 return process_invitation(mesh, c, data);
177 } else if(type == 1 && len == 32 && c->status.invitation_used) {
178 return commit_invitation(mesh, c, data);
185 bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
189 char name[MAX_STRING_SIZE];
191 if(sscanf(request, "%*d " MAX_STRING " %d.%d", name, &c->protocol_major, &c->protocol_minor) < 2) {
192 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ID", c->name);
196 /* Check if this is an invitation */
199 if(!mesh->invitation_key) {
200 logger(mesh, MESHLINK_ERROR, "Got invitation from %s but we don't have an invitation key", c->name);
204 c->ecdsa = ecdsa_set_base64_public_key(name + 1);
207 logger(mesh, MESHLINK_ERROR, "Got bad invitation from %s", c->name);
211 c->status.invitation = true;
212 char *mykey = ecdsa_get_base64_public_key(mesh->invitation_key);
218 if(!send_request(mesh, c, NULL, "%d %s", ACK, mykey)) {
224 c->protocol_minor = 2;
225 c->allow_request = 1;
226 c->last_ping_time = mesh->loop.now.tv_sec;
228 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);
231 /* Check if identity is a valid name */
233 if(!check_id(name)) {
234 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ID", c->name, "invalid name");
238 /* If this is an outgoing connection, make sure we are connected to the right host */
241 if(strcmp(c->name, name)) {
242 logger(mesh, MESHLINK_ERROR, "Peer is %s instead of %s", name, c->name);
250 c->name = xstrdup(name);
253 /* Check if version matches */
255 if(c->protocol_major != PROT_MAJOR) {
256 logger(mesh, MESHLINK_ERROR, "Peer %s uses incompatible version %d.%d",
257 c->name, c->protocol_major, c->protocol_minor);
261 /* Check if we know this node */
263 node_t *n = lookup_node(mesh, c->name);
266 logger(mesh, MESHLINK_ERROR, "Peer %s has unknown identity", c->name);
270 if(!node_read_public_key(mesh, n)) {
271 logger(mesh, MESHLINK_ERROR, "No key known for peer %s", c->name);
273 if(n->status.reachable && !n->status.waitingforkey) {
274 logger(mesh, MESHLINK_INFO, "Requesting key from peer %s", c->name);
275 send_req_key(mesh, n);
281 /* Forbid version rollback for nodes whose ECDSA key we know */
283 if(ecdsa_active(c->ecdsa) && c->protocol_minor < 2) {
284 logger(mesh, MESHLINK_ERROR, "Peer %s tries to roll back protocol version to %d.%d",
285 c->name, c->protocol_major, c->protocol_minor);
289 c->allow_request = ACK;
290 c->last_ping_time = mesh->loop.now.tv_sec;
291 char label[sizeof(meshlink_tcp_label) + strlen(mesh->self->name) + strlen(c->name) + 2];
294 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, mesh->self->name, c->name);
296 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, c->name, mesh->self->name);
299 if(mesh->log_level <= MESHLINK_DEBUG) {
300 char buf1[1024], buf2[1024];
301 bin2hex((uint8_t *)mesh->private_key + 64, buf1, 32);
302 bin2hex((uint8_t *)n->ecdsa + 64, buf2, 32);
303 logger(mesh, MESHLINK_DEBUG, "Connection to %s mykey %s hiskey %s", c->name, buf1, buf2);
306 return sptps_start(&c->sptps, c, c->outgoing, false, mesh->private_key, n->ecdsa, label, sizeof(label) - 1, send_meta_sptps, receive_meta_sptps);
309 bool send_ack(meshlink_handle_t *mesh, connection_t *c) {
310 node_t *n = lookup_node(mesh, c->name);
312 if(n && n->status.blacklisted) {
313 logger(mesh, MESHLINK_WARNING, "Peer %s is blacklisted", c->name);
314 return send_error(mesh, c, BLACKLISTED, "blacklisted");
317 c->last_ping_time = mesh->loop.now.tv_sec;
318 return send_request(mesh, c, NULL, "%d %s %d %x", ACK, mesh->myport, mesh->devclass, OPTION_PMTU_DISCOVERY | (PROT_MINOR << 24));
321 static void send_everything(meshlink_handle_t *mesh, connection_t *c) {
322 /* Send all known subnets and edges */
324 for splay_each(node_t, n, mesh->nodes) {
325 for inner_splay_each(edge_t, e, n->edge_tree) {
326 send_add_edge(mesh, c, e, 0);
331 bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
335 char hisport[MAX_STRING_SIZE];
340 if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &devclass, &options) != 3) {
341 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ACK", c->name);
345 if(devclass < 0 || devclass >= DEV_CLASS_COUNT) {
346 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ACK", c->name, "devclass invalid");
350 /* Check if we already have a node_t for him */
352 n = lookup_node(mesh, c->name);
356 n->name = xstrdup(c->name);
360 /* Oh dear, we already have a connection to this node. */
361 logger(mesh, MESHLINK_INFO, "Established a second connection with %s, closing old connection", n->connection->name);
363 if(n->connection->outgoing) {
365 logger(mesh, MESHLINK_WARNING, "Two outgoing connections to the same node!");
367 c->outgoing = n->connection->outgoing;
370 n->connection->outgoing = NULL;
373 /* Remove the edge before terminating the connection, to prevent a graph update. */
374 edge_del(mesh, n->connection->edge);
375 n->connection->edge = NULL;
377 terminate_connection(mesh, n->connection, false);
381 n->devclass = devclass;
382 n->status.dirty = true;
384 n->last_successfull_connection = mesh->loop.now.tv_sec;
390 /* Activate this connection */
392 c->allow_request = ALL;
393 c->last_key_renewal = mesh->loop.now.tv_sec;
394 c->status.active = true;
396 logger(mesh, MESHLINK_INFO, "Connection with %s activated", c->name);
398 if(mesh->meta_status_cb) {
399 mesh->meta_status_cb(mesh, (meshlink_node_t *)n, true);
402 /* Terminate any connections to this node that are not activated yet */
404 for list_each(connection_t, other, mesh->connections) {
405 if(!other->status.active && !strcmp(other->name, c->name)) {
406 if(other->outgoing) {
408 logger(mesh, MESHLINK_WARNING, "Two outgoing connections to the same node!");
410 c->outgoing = other->outgoing;
413 other->outgoing = NULL;
416 logger(mesh, MESHLINK_DEBUG, "Terminating pending second connection with %s", n->name);
417 terminate_connection(mesh, other, false);
421 /* Send him everything we know */
423 send_everything(mesh, c);
425 /* Create an edge_t for this connection */
427 assert(devclass >= 0 && devclass < DEV_CLASS_COUNT);
429 c->edge = new_edge();
430 c->edge->from = mesh->self;
432 sockaddrcpy_setport(&c->edge->address, &c->address, atoi(hisport));
433 c->edge->weight = mesh->dev_class_traits[devclass].edge_weight;
434 c->edge->connection = c;
436 node_add_recent_address(mesh, n, &c->address);
437 edge_add(mesh, c->edge);
439 /* Notify everyone of the new edge */
441 send_add_edge(mesh, mesh->everyone, c->edge, 0);
443 /* Run MST and SSSP algorithms */
447 /* Request a session key to jump start UDP traffic */
449 if(c->status.initiator) {
450 send_req_key(mesh, n);