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"
27 #include "meshlink_internal.h"
38 #include "ed25519/sha512.h"
42 extern bool node_write_devclass(meshlink_handle_t *mesh, node_t *n);
44 bool send_id(meshlink_handle_t *mesh, connection_t *c) {
45 return send_request(mesh, c, "%d %s %d.%d %s %u", ID, mesh->self->name, PROT_MAJOR, PROT_MINOR, mesh->appname, PROTOCOL_TINY);
48 bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
52 char name[MAX_STRING_SIZE];
55 if(sscanf(request, "%*d " MAX_STRING " %d.%d %*s %u", name, &c->protocol_major, &c->protocol_minor, &flags) < 4) {
56 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ID", c->name);
60 /* Check if identity is a valid name */
63 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ID", c->name, "invalid name");
67 /* If this is an outgoing connection, make sure we are connected to the right host */
70 if(strcmp(c->name, name)) {
71 logger(mesh, MESHLINK_ERROR, "Peer is %s instead of %s", name, c->name);
79 c->name = xstrdup(name);
82 /* Check if version matches */
84 if(c->protocol_major != PROT_MAJOR) {
85 logger(mesh, MESHLINK_ERROR, "Peer %s uses incompatible version %d.%d",
86 c->name, c->protocol_major, c->protocol_minor);
90 /* Check if we know this node */
92 node_t *n = lookup_node(mesh, c->name);
95 logger(mesh, MESHLINK_ERROR, "Peer %s has unknown identity", c->name);
99 if(!node_read_public_key(mesh, n)) {
100 logger(mesh, MESHLINK_ERROR, "No key known for peer %s", c->name);
104 /* Forbid version rollback for nodes whose ECDSA key we know */
106 if(ecdsa_active(c->ecdsa) && c->protocol_minor < 2) {
107 logger(mesh, MESHLINK_ERROR, "Peer %s tries to roll back protocol version to %d.%d",
108 c->name, c->protocol_major, c->protocol_minor);
112 c->allow_request = ACK;
113 c->last_ping_time = mesh->loop.now.tv_sec;
114 char label[sizeof(meshlink_tcp_label) + strlen(mesh->self->name) + strlen(c->name) + 2];
117 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, mesh->self->name, c->name);
119 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, c->name, mesh->self->name);
122 if(mesh->log_level <= MESHLINK_DEBUG) {
123 char buf1[1024], buf2[1024];
124 bin2hex((uint8_t *)mesh->private_key + 64, buf1, 32);
125 bin2hex((uint8_t *)n->ecdsa + 64, buf2, 32);
126 logger(mesh, MESHLINK_DEBUG, "Connection to %s mykey %s hiskey %s", c->name, buf1, buf2);
129 return sptps_start(&c->sptps, c, c->outgoing, false, mesh->private_key, n->ecdsa, label, sizeof(label) - 1, send_meta_sptps, receive_meta_sptps);
132 bool send_ack(meshlink_handle_t *mesh, connection_t *c) {
133 node_t *n = lookup_node(mesh, c->name);
135 if(n && n->status.blacklisted) {
136 logger(mesh, MESHLINK_WARNING, "Peer %s is blacklisted", c->name);
137 return send_error(mesh, c, BLACKLISTED, "blacklisted");
140 c->last_ping_time = mesh->loop.now.tv_sec;
141 return send_request(mesh, c, "%d %s %d %x", ACK, mesh->myport, mesh->devclass, OPTION_PMTU_DISCOVERY | (PROT_MINOR << 24));
144 bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
148 char hisport[MAX_STRING_SIZE];
153 if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &devclass, &options) != 3) {
154 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ACK", c->name);
158 if(devclass < 0 || devclass >= DEV_CLASS_COUNT) {
159 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ACK", c->name, "devclass invalid");
163 /* Check if we already have a node_t for him */
165 n = lookup_node(mesh, c->name);
169 n->name = xstrdup(c->name);
173 n->devclass = devclass;
174 n->status.dirty = true;
176 n->last_successfull_connection = mesh->loop.now.tv_sec;
182 /* Activate this connection */
184 c->allow_request = ALL;
185 c->last_key_renewal = mesh->loop.now.tv_sec;
186 c->status.active = true;
188 logger(mesh, MESHLINK_INFO, "Connection with %s activated", c->name);
190 if(mesh->meta_status_cb) {
191 mesh->meta_status_cb(mesh, (meshlink_node_t *)n, true);
194 send_add_edge(mesh, c, 0);
195 n->status.reachable = true;
196 update_node_status(mesh, c->node);