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", ID, mesh->self->name, PROT_MAJOR, PROT_MINOR, mesh->appname);
48 bool id_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
52 char name[MAX_STRING_SIZE];
54 if(sscanf(request, "%*d " MAX_STRING " %d.%d", name, &c->protocol_major, &c->protocol_minor) < 2) {
55 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ID", c->name);
59 /* Check if identity is a valid name */
62 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ID", c->name, "invalid name");
66 /* If this is an outgoing connection, make sure we are connected to the right host */
69 if(strcmp(c->name, name)) {
70 logger(mesh, MESHLINK_ERROR, "Peer is %s instead of %s", name, c->name);
78 c->name = xstrdup(name);
81 /* Check if version matches */
83 if(c->protocol_major != PROT_MAJOR) {
84 logger(mesh, MESHLINK_ERROR, "Peer %s uses incompatible version %d.%d",
85 c->name, c->protocol_major, c->protocol_minor);
89 /* Check if we know this node */
91 node_t *n = lookup_node(mesh, c->name);
94 logger(mesh, MESHLINK_ERROR, "Peer %s has unknown identity", c->name);
98 if(!node_read_public_key(mesh, n)) {
99 logger(mesh, MESHLINK_ERROR, "No key known for peer %s", c->name);
101 if(n->status.reachable && !n->status.waitingforkey) {
102 logger(mesh, MESHLINK_INFO, "Requesting key from peer %s", c->name);
103 send_req_key(mesh, n);
109 /* Forbid version rollback for nodes whose ECDSA key we know */
111 if(ecdsa_active(c->ecdsa) && c->protocol_minor < 2) {
112 logger(mesh, MESHLINK_ERROR, "Peer %s tries to roll back protocol version to %d.%d",
113 c->name, c->protocol_major, c->protocol_minor);
117 c->allow_request = ACK;
118 c->last_ping_time = mesh->loop.now.tv_sec;
119 char label[sizeof(meshlink_tcp_label) + strlen(mesh->self->name) + strlen(c->name) + 2];
122 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, mesh->self->name, c->name);
124 snprintf(label, sizeof(label), "%s %s %s", meshlink_tcp_label, c->name, mesh->self->name);
127 if(mesh->log_level <= MESHLINK_DEBUG) {
128 char buf1[1024], buf2[1024];
129 bin2hex((uint8_t *)mesh->private_key + 64, buf1, 32);
130 bin2hex((uint8_t *)n->ecdsa + 64, buf2, 32);
131 logger(mesh, MESHLINK_DEBUG, "Connection to %s mykey %s hiskey %s", c->name, buf1, buf2);
134 return sptps_start(&c->sptps, c, c->outgoing, false, mesh->private_key, n->ecdsa, label, sizeof(label) - 1, send_meta_sptps, receive_meta_sptps);
137 bool send_ack(meshlink_handle_t *mesh, connection_t *c) {
138 node_t *n = lookup_node(mesh, c->name);
140 if(n && n->status.blacklisted) {
141 logger(mesh, MESHLINK_WARNING, "Peer %s is blacklisted", c->name);
142 return send_error(mesh, c, BLACKLISTED, "blacklisted");
145 c->last_ping_time = mesh->loop.now.tv_sec;
146 return send_request(mesh, c, "%d %s %d %x", ACK, mesh->myport, mesh->devclass, OPTION_PMTU_DISCOVERY | (PROT_MINOR << 24));
149 bool ack_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
153 char hisport[MAX_STRING_SIZE];
158 if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &devclass, &options) != 3) {
159 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ACK", c->name);
163 if(devclass < 0 || devclass >= DEV_CLASS_COUNT) {
164 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ACK", c->name, "devclass invalid");
168 /* Check if we already have a node_t for him */
170 n = lookup_node(mesh, c->name);
174 n->name = xstrdup(c->name);
178 n->devclass = devclass;
179 n->status.dirty = true;
181 n->last_successfull_connection = mesh->loop.now.tv_sec;
187 /* Activate this connection */
189 c->allow_request = ALL;
190 c->last_key_renewal = mesh->loop.now.tv_sec;
191 c->status.active = true;
193 logger(mesh, MESHLINK_INFO, "Connection with %s activated", c->name);
195 if(mesh->meta_status_cb) {
196 mesh->meta_status_cb(mesh, (meshlink_node_t *)n, true);
199 /* TODO: Create an edge_t for this connection, send it */
201 /* Request a session key to jump start UDP traffic */
203 if(c->status.initiator) {
204 send_req_key(mesh, n);