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"
28 #include "meshlink_internal.h"
39 #include "ed25519/sha512.h"
43 extern bool node_write_devclass(meshlink_handle_t *mesh, node_t *n);
45 static bool send_proxyrequest(meshlink_handle_t *mesh, connection_t *c) {
46 switch(mesh->proxytype) {
51 sockaddr2str(&c->address, &host, &port);
52 send_request(mesh, c, NULL, "CONNECT %s:%s HTTP/1.1\r\n\r", host, port);
59 if(c->address.sa.sa_family != AF_INET) {
60 logger(mesh, MESHLINK_ERROR, "Cannot connect to an IPv6 host through a SOCKS 4 proxy!");
64 char s4req[9 + (mesh->proxyuser ? strlen(mesh->proxyuser) : 0)];
67 memcpy(s4req + 2, &c->address.in.sin_port, 2);
68 memcpy(s4req + 4, &c->address.in.sin_addr, 4);
71 memcpy(s4req + 8, mesh->proxyuser, strlen(mesh->proxyuser));
74 s4req[sizeof(s4req) - 1] = 0;
76 return send_meta(mesh, c, s4req, sizeof(s4req));
80 int len = 3 + 6 + (c->address.sa.sa_family == AF_INET ? 4 : 16);
84 len += 3 + strlen(mesh->proxyuser) + strlen(mesh->proxypass);
95 s5req[i++] = strlen(mesh->proxyuser);
96 memcpy(s5req + i, mesh->proxyuser, strlen(mesh->proxyuser));
97 i += strlen(mesh->proxyuser);
98 s5req[i++] = strlen(mesh->proxypass);
99 memcpy(s5req + i, mesh->proxypass, strlen(mesh->proxypass));
100 i += strlen(mesh->proxypass);
110 if(c->address.sa.sa_family == AF_INET) {
112 memcpy(s5req + i, &c->address.in.sin_addr, 4);
114 memcpy(s5req + i, &c->address.in.sin_port, 2);
117 } else if(c->address.sa.sa_family == AF_INET6) {
119 memcpy(s5req + i, &c->address.in6.sin6_addr, 16);
121 memcpy(s5req + i, &c->address.in6.sin6_port, 2);
125 logger(mesh, MESHLINK_ERROR, "Address family %hx not supported for SOCKS 5 proxies!", c->address.sa.sa_family);
133 return send_meta(mesh, c, s5req, sizeof(s5req));
137 logger(mesh, MESHLINK_ERROR, "Proxy type not implemented yet");
141 logger(mesh, MESHLINK_ERROR, "Unknown proxy type");
146 bool send_id(meshlink_handle_t *mesh, connection_t *c) {
147 if(mesh->proxytype && c->outgoing)
148 if(!send_proxyrequest(mesh, c)) {
152 return send_request(mesh, c, NULL, "%d %s %d.%d %s", ID, mesh->self->name, PROT_MAJOR, PROT_MINOR, mesh->appname);
155 static bool finalize_invitation(meshlink_handle_t *mesh, connection_t *c, const void *data, uint16_t len) {
157 logger(mesh, MESHLINK_ERROR, "Received invalid key from invited node %s!\n", c->name);
162 node_t *n = new_node();
163 n->name = xstrdup(c->name);
164 n->devclass = DEV_CLASS_UNKNOWN;
165 n->ecdsa = ecdsa_set_public_key(data);
166 n->submesh = c->submesh;
167 n->status.dirty = true;
169 // TODO: immediately write the config file?
171 logger(mesh, MESHLINK_INFO, "Key successfully received from %s", c->name);
173 //TODO: callback to application to inform of an accepted invitation
175 sptps_send_record(&c->sptps, 1, "", 0);
180 static bool receive_invitation_sptps(void *handle, uint8_t type, const void *data, uint16_t len) {
181 connection_t *c = handle;
182 meshlink_handle_t *mesh = c->mesh;
188 if(type == 1 && c->status.invitation_used) {
189 return finalize_invitation(mesh, c, data, len);
192 if(type != 0 || len != 18 || c->status.invitation_used) {
196 // Recover the filename from the cookie and the key
197 char *fingerprint = ecdsa_get_base64_public_key(mesh->invitation_key);
199 char hashbuf[18 + strlen(fingerprint)];
201 memcpy(hashbuf, data, 18);
202 memcpy(hashbuf + 18, fingerprint, sizeof(hashbuf) - 18);
203 sha512(hashbuf, sizeof(hashbuf), hash);
204 b64encode_urlsafe(hash, cookie, 18);
209 if(!invitation_read(mesh, cookie, &config)) {
210 logger(mesh, MESHLINK_ERROR, "Error while trying to read invitation file\n");
214 // Read the new node's Name from the file
215 packmsg_input_t in = {config.buf, config.len};
216 packmsg_get_uint32(&in); // skip version
218 c->name = packmsg_get_str_dup(&in);
220 // Check if the file contains Sub-Mesh information
221 char *submesh_name = packmsg_get_str_dup(&in);
223 if(!strcmp(submesh_name, CORE_MESH)) {
226 if(!check_id(submesh_name)) {
227 logger(mesh, MESHLINK_ERROR, "Invalid invitation file %s\n", cookie);
232 c->submesh = lookup_or_create_submesh(mesh, submesh_name);
239 // Send the node the contents of the invitation file
240 sptps_send_record(&c->sptps, 0, config.buf, config.len);
242 c->status.invitation_used = true;
244 logger(mesh, MESHLINK_INFO, "Invitation %s successfully sent to %s", cookie, c->name);
248 bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
249 char name[MAX_STRING_SIZE];
251 if(sscanf(request, "%*d " MAX_STRING " %d.%d", name, &c->protocol_major, &c->protocol_minor) < 2) {
252 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ID", c->name);
256 /* Check if this is an invitation */
259 if(!mesh->invitation_key) {
260 logger(mesh, MESHLINK_ERROR, "Got invitation from %s but we don't have an invitation key", c->name);
264 c->ecdsa = ecdsa_set_base64_public_key(name + 1);
267 logger(mesh, MESHLINK_ERROR, "Got bad invitation from %s", c->name);
271 c->status.invitation = true;
272 char *mykey = ecdsa_get_base64_public_key(mesh->invitation_key);
278 if(!send_request(mesh, c, NULL, "%d %s", ACK, mykey)) {
284 c->protocol_minor = 2;
285 c->allow_request = 1;
287 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);
290 /* Check if identity is a valid name */
292 if(!check_id(name)) {
293 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ID", c->name, "invalid name");
297 /* If this is an outgoing connection, make sure we are connected to the right host */
300 if(strcmp(c->name, name)) {
301 logger(mesh, MESHLINK_ERROR, "Peer is %s instead of %s", name, c->name);
309 c->name = xstrdup(name);
312 /* Check if version matches */
314 if(c->protocol_major != PROT_MAJOR) {
315 logger(mesh, MESHLINK_ERROR, "Peer %s uses incompatible version %d.%d",
316 c->name, c->protocol_major, c->protocol_minor);
320 /* Check if we know this node */
322 node_t *n = lookup_node(mesh, c->name);
325 logger(mesh, MESHLINK_ERROR, "Peer %s has unknown identity", c->name);
329 if(n->status.blacklisted) {
330 logger(mesh, MESHLINK_EPEER, "Peer %s is blacklisted", c->name);
334 node_read_public_key(mesh, n);
336 if(!ecdsa_active(n->ecdsa)) {
337 logger(mesh, MESHLINK_ERROR, "No key known for peer %s", c->name);
339 if(n->status.reachable && !n->status.waitingforkey) {
340 logger(mesh, MESHLINK_INFO, "Requesting key from peer %s", c->name);
341 send_req_key(mesh, n);
347 /* Forbid version rollback for nodes whose ECDSA key we know */
349 if(ecdsa_active(c->ecdsa) && c->protocol_minor < 2) {
350 logger(mesh, MESHLINK_ERROR, "Peer %s tries to roll back protocol version to %d.%d",
351 c->name, c->protocol_major, c->protocol_minor);
355 c->allow_request = ACK;
356 char label[sizeof(meshlink_tcp_label) + strlen(mesh->self->name) + strlen(c->name) + 2];
359 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, mesh->self->name, c->name);
361 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, c->name, mesh->self->name);
364 char buf1[1024], buf2[1024];
365 bin2hex((uint8_t *)mesh->private_key + 64, buf1, 32);
366 bin2hex((uint8_t *)n->ecdsa + 64, buf2, 32);
367 logger(mesh, MESHLINK_DEBUG, "Connection to %s mykey %s hiskey %s", c->name, buf1, buf2);
368 return sptps_start(&c->sptps, c, c->outgoing, false, mesh->private_key, n->ecdsa, label, sizeof(label) - 1, send_meta_sptps, receive_meta_sptps);
371 bool send_ack(meshlink_handle_t *mesh, connection_t *c) {
373 /* Check some options */
375 if(mesh->self->options & OPTION_PMTU_DISCOVERY) {
376 c->options |= OPTION_PMTU_DISCOVERY;
379 return send_request(mesh, c, NULL, "%d %s %d %x", ACK, mesh->myport, mesh->devclass, (c->options & 0xffffff) | (PROT_MINOR << 24));
382 static void send_everything(meshlink_handle_t *mesh, connection_t *c) {
383 /* Send all known subnets and edges */
385 for splay_each(node_t, n, mesh->nodes) {
386 for splay_each(edge_t, e, n->edge_tree) {
387 send_add_edge(mesh, c, e, 0);
392 bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
393 char hisport[MAX_STRING_SIZE];
398 if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &devclass, &options) != 3) {
399 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ACK", c->name);
403 if(devclass < 0 || devclass > _DEV_CLASS_MAX) {
404 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ACK", c->name, "devclass invalid");
408 /* Check if we already have a node_t for him */
410 n = lookup_node(mesh, c->name);
414 n->name = xstrdup(c->name);
418 /* Oh dear, we already have a connection to this node. */
419 logger(mesh, MESHLINK_DEBUG, "Established a second connection with %s, closing old connection", n->connection->name);
421 if(n->connection->outgoing) {
423 logger(mesh, MESHLINK_WARNING, "Two outgoing connections to the same node!");
425 c->outgoing = n->connection->outgoing;
428 n->connection->outgoing = NULL;
431 terminate_connection(mesh, n->connection, false);
432 /* Run graph algorithm to keep things in sync */
437 n->devclass = devclass;
438 n->status.dirty = true;
440 n->last_successfull_connection = time(NULL);
445 if(!(c->options & options & OPTION_PMTU_DISCOVERY)) {
446 c->options &= ~OPTION_PMTU_DISCOVERY;
447 options &= ~OPTION_PMTU_DISCOVERY;
450 c->options |= options;
452 /* Activate this connection */
454 c->allow_request = ALL;
455 c->status.active = true;
457 logger(mesh, MESHLINK_INFO, "Connection with %s activated", c->name);
459 /* Send him everything we know */
461 send_everything(mesh, c);
463 /* Create an edge_t for this connection */
465 assert(devclass >= 0 && devclass <= _DEV_CLASS_MAX);
467 c->edge = new_edge();
468 c->edge->from = mesh->self;
470 sockaddrcpy_setport(&c->edge->address, &c->address, atoi(hisport));
471 c->edge->weight = dev_class_traits[devclass].edge_weight;
472 c->edge->connection = c;
473 c->edge->options = c->options;
475 edge_add(mesh, c->edge);
477 /* Notify everyone of the new edge */
479 send_add_edge(mesh, mesh->everyone, c->edge, 0);
481 /* Run MST and SSSP algorithms */