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) {
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;
58 // Remember its current address
59 node_add_recent_address(mesh, n, &c->address);
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);
70 logger(mesh, MESHLINK_INFO, "Key successfully received from %s", c->name);
72 //TODO: callback to application to inform of an accepted invitation
74 sptps_send_record(&c->sptps, 1, "", 0);
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);
83 char hashbuf[18 + strlen(fingerprint)];
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);
93 if(!invitation_read(mesh, "current", cookie, &config, mesh->config_key)) {
94 logger(mesh, MESHLINK_ERROR, "Error while trying to read invitation file\n");
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
102 c->name = packmsg_get_str_dup(&in);
104 // Check if the file contains Sub-Mesh information
105 char *submesh_name = packmsg_get_str_dup(&in);
107 if(!strcmp(submesh_name, CORE_MESH)) {
111 if(!check_id(submesh_name)) {
112 logger(mesh, MESHLINK_ERROR, "Invalid invitation file %s\n", cookie);
117 c->submesh = lookup_or_create_submesh(mesh, submesh_name);
121 logger(mesh, MESHLINK_ERROR, "Unknown submesh in invitation file %s\n", cookie);
126 if(mesh->inviter_commits_first && !commit_invitation(mesh, c, (const char *)data + 18)) {
130 if(mesh->inviter_commits_first) {
131 devtool_set_inviter_commits_first(true);
134 // Send the node the contents of the invitation file
135 sptps_send_record(&c->sptps, 0, config.buf, config.len);
137 config_free(&config);
139 c->status.invitation_used = true;
141 logger(mesh, MESHLINK_INFO, "Invitation %s successfully sent to %s", cookie, c->name);
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;
149 // Extend the time for the invitation exchange upon receiving a valid message
150 c->last_ping_time = mesh->loop.now.tv_sec;
152 if(type == SPTPS_HANDSHAKE) {
153 // The peer should send its cookie first.
157 if(mesh->inviter_commits_first) {
158 if(type == 2 && len == 18 + 32 && !c->status.invitation_used) {
159 return process_invitation(mesh, c, data);
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);
172 bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
176 char name[MAX_STRING_SIZE];
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);
183 /* Check if this is an invitation */
186 if(!mesh->invitation_key) {
187 logger(mesh, MESHLINK_ERROR, "Got invitation from %s but we don't have an invitation key", c->name);
191 c->ecdsa = ecdsa_set_base64_public_key(name + 1);
194 logger(mesh, MESHLINK_ERROR, "Got bad invitation from %s", c->name);
198 c->status.invitation = true;
199 char *mykey = ecdsa_get_base64_public_key(mesh->invitation_key);
205 if(!send_request(mesh, c, NULL, "%d %s", ACK, mykey)) {
211 c->protocol_minor = 2;
212 c->allow_request = 1;
213 c->last_ping_time = mesh->loop.now.tv_sec;
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);
218 /* Check if identity is a valid name */
220 if(!check_id(name)) {
221 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ID", c->name, "invalid name");
225 /* If this is an outgoing connection, make sure we are connected to the right host */
228 if(strcmp(c->name, name)) {
229 logger(mesh, MESHLINK_ERROR, "Peer is %s instead of %s", name, c->name);
237 c->name = xstrdup(name);
240 /* Check if version matches */
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);
248 /* Check if we know this node */
250 node_t *n = lookup_node(mesh, c->name);
253 logger(mesh, MESHLINK_ERROR, "Peer %s has unknown identity", c->name);
257 if(n->status.blacklisted) {
258 logger(mesh, MESHLINK_WARNING, "Peer %s is blacklisted", c->name);
262 if(!node_read_public_key(mesh, n)) {
263 logger(mesh, MESHLINK_ERROR, "No key known for peer %s", c->name);
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);
273 /* Forbid version rollback for nodes whose ECDSA key we know */
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);
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];
286 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, mesh->self->name, c->name);
288 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, c->name, mesh->self->name);
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);
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));
303 static void send_everything(meshlink_handle_t *mesh, connection_t *c) {
304 /* Send all known subnets and edges */
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);
313 bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
317 char hisport[MAX_STRING_SIZE];
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);
327 if(devclass < 0 || devclass >= DEV_CLASS_COUNT) {
328 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ACK", c->name, "devclass invalid");
332 /* Check if we already have a node_t for him */
334 n = lookup_node(mesh, c->name);
338 n->name = xstrdup(c->name);
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);
345 if(n->connection->outgoing) {
347 logger(mesh, MESHLINK_WARNING, "Two outgoing connections to the same node!");
349 c->outgoing = n->connection->outgoing;
352 n->connection->outgoing = NULL;
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;
359 terminate_connection(mesh, n->connection, false);
363 n->devclass = devclass;
364 n->status.dirty = true;
366 n->last_successfull_connection = mesh->loop.now.tv_sec;
372 /* Activate this connection */
374 c->allow_request = ALL;
375 c->last_key_renewal = mesh->loop.now.tv_sec;
376 c->status.active = true;
378 logger(mesh, MESHLINK_INFO, "Connection with %s activated", c->name);
380 if(mesh->meta_status_cb) {
381 mesh->meta_status_cb(mesh, (meshlink_node_t *)n, true);
384 /* Send him everything we know */
386 send_everything(mesh, c);
388 /* Create an edge_t for this connection */
390 assert(devclass >= 0 && devclass < DEV_CLASS_COUNT);
392 c->edge = new_edge();
393 c->edge->from = mesh->self;
395 sockaddrcpy_setport(&c->edge->address, &c->address, atoi(hisport));
396 c->edge->weight = mesh->dev_class_traits[devclass].edge_weight;
397 c->edge->connection = c;
399 node_add_recent_address(mesh, n, &c->address);
400 edge_add(mesh, c->edge);
402 /* Notify everyone of the new edge */
404 send_add_edge(mesh, mesh->everyone, c->edge, 0);
406 /* Run MST and SSSP algorithms */
410 /* Request a session key to jump start UDP traffic */
412 if(c->status.initiator) {
413 send_req_key(mesh, n);