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 if(type == SPTPS_HANDSHAKE) {
150 // The peer should send its cookie first.
154 if(mesh->inviter_commits_first) {
155 if(type == 2 && len == 18 + 32 && !c->status.invitation_used) {
156 return process_invitation(mesh, c, data);
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);
169 bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
173 char name[MAX_STRING_SIZE];
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);
180 /* Check if this is an invitation */
183 if(!mesh->invitation_key) {
184 logger(mesh, MESHLINK_ERROR, "Got invitation from %s but we don't have an invitation key", c->name);
188 c->ecdsa = ecdsa_set_base64_public_key(name + 1);
191 logger(mesh, MESHLINK_ERROR, "Got bad invitation from %s", c->name);
195 c->status.invitation = true;
196 char *mykey = ecdsa_get_base64_public_key(mesh->invitation_key);
202 if(!send_request(mesh, c, NULL, "%d %s", ACK, mykey)) {
208 c->protocol_minor = 2;
209 c->allow_request = 1;
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);
214 /* Check if identity is a valid name */
216 if(!check_id(name)) {
217 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ID", c->name, "invalid name");
221 /* If this is an outgoing connection, make sure we are connected to the right host */
224 if(strcmp(c->name, name)) {
225 logger(mesh, MESHLINK_ERROR, "Peer is %s instead of %s", name, c->name);
233 c->name = xstrdup(name);
236 /* Check if version matches */
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);
244 /* Check if we know this node */
246 node_t *n = lookup_node(mesh, c->name);
249 logger(mesh, MESHLINK_ERROR, "Peer %s has unknown identity", c->name);
253 if(n->status.blacklisted) {
254 logger(mesh, MESHLINK_WARNING, "Peer %s is blacklisted", c->name);
258 if(!node_read_public_key(mesh, n)) {
259 logger(mesh, MESHLINK_ERROR, "No key known for peer %s", c->name);
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);
269 /* Forbid version rollback for nodes whose ECDSA key we know */
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);
277 c->allow_request = ACK;
278 char label[sizeof(meshlink_tcp_label) + strlen(mesh->self->name) + strlen(c->name) + 2];
281 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, mesh->self->name, c->name);
283 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, c->name, mesh->self->name);
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);
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));
297 static void send_everything(meshlink_handle_t *mesh, connection_t *c) {
298 /* Send all known subnets and edges */
300 for splay_each(node_t, n, mesh->nodes) {
301 for inner_splay_each(edge_t, e, n->edge_tree) {
302 send_add_edge(mesh, c, e, 0);
307 bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
311 char hisport[MAX_STRING_SIZE];
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);
321 if(devclass < 0 || devclass >= DEV_CLASS_COUNT) {
322 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ACK", c->name, "devclass invalid");
326 /* Check if we already have a node_t for him */
328 n = lookup_node(mesh, c->name);
332 n->name = xstrdup(c->name);
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);
339 if(n->connection->outgoing) {
341 logger(mesh, MESHLINK_WARNING, "Two outgoing connections to the same node!");
343 c->outgoing = n->connection->outgoing;
346 n->connection->outgoing = NULL;
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;
353 terminate_connection(mesh, n->connection, false);
357 n->devclass = devclass;
358 n->status.dirty = true;
360 n->last_successfull_connection = mesh->loop.now.tv_sec;
365 /* Activate this connection */
367 c->allow_request = ALL;
368 c->last_key_renewal = mesh->loop.now.tv_sec;
369 c->status.active = true;
371 logger(mesh, MESHLINK_INFO, "Connection with %s activated", c->name);
373 /* Send him everything we know */
375 send_everything(mesh, c);
377 /* Create an edge_t for this connection */
379 assert(devclass >= 0 && devclass < DEV_CLASS_COUNT);
381 c->edge = new_edge();
382 c->edge->from = mesh->self;
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;
388 edge_add(mesh, c->edge);
390 /* Notify everyone of the new edge */
392 send_add_edge(mesh, mesh->everyone, c->edge, 0);
394 /* Run MST and SSSP algorithms */