2 protocol_key.c -- handle the meta-protocol, key exchange
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.
22 #include "connection.h"
24 #include "meshlink_internal.h"
34 static const int req_key_timeout = 2;
36 bool key_changed_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
40 char name[MAX_STRING_SIZE];
43 if(sscanf(request, "%*d %*x " MAX_STRING, name) != 1) {
44 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "KEY_CHANGED", c->name);
48 if(seen_request(mesh, request)) {
52 n = lookup_node(mesh, name);
55 logger(mesh, MESHLINK_ERROR, "Got %s from %s origin %s which does not exist", "KEY_CHANGED", c->name, name);
61 forward_request(mesh, c, NULL, request);
66 static bool send_initial_sptps_data(void *handle, uint8_t type, const void *data, size_t len) {
73 meshlink_handle_t *mesh = to->mesh;
75 if(!to->nexthop || !to->nexthop->connection) {
76 logger(mesh, MESHLINK_WARNING, "Cannot send SPTPS data to %s via %s", to->name, to->nexthop ? to->nexthop->name : to->name);
80 to->sptps.send_data = send_sptps_data;
81 char buf[len * 4 / 3 + 5];
82 b64encode(data, buf, len);
83 return send_request(mesh, to->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_KEY, buf);
86 bool send_canonical_address(meshlink_handle_t *mesh, node_t *to) {
87 if(!mesh->self->canonical_address) {
91 return send_request(mesh, to->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_CANONICAL, mesh->self->canonical_address);
94 bool send_req_key(meshlink_handle_t *mesh, node_t *to) {
95 if(!node_read_public_key(mesh, to)) {
96 logger(mesh, MESHLINK_DEBUG, "No ECDSA key known for %s", to->name);
98 if(!to->nexthop || !to->nexthop->connection) {
99 logger(mesh, MESHLINK_WARNING, "Cannot send REQ_PUBKEY to %s via %s", to->name, to->nexthop ? to->nexthop->name : to->name);
103 char *pubkey = ecdsa_get_base64_public_key(mesh->private_key);
104 send_request(mesh, to->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_PUBKEY, pubkey);
109 if(to->sptps.label) {
110 logger(mesh, MESHLINK_DEBUG, "send_req_key(%s) called while sptps->label != NULL!", to->name);
113 /* Send our canonical address to help with UDP hole punching */
114 send_canonical_address(mesh, to);
116 char label[sizeof(meshlink_udp_label) + strlen(mesh->self->name) + strlen(to->name) + 2];
117 snprintf(label, sizeof(label), "%s %s %s", meshlink_udp_label, mesh->self->name, to->name);
118 sptps_stop(&to->sptps);
119 to->status.validkey = false;
120 to->status.waitingforkey = true;
121 to->last_req_key = mesh->loop.now.tv_sec;
122 return sptps_start(&to->sptps, to, true, true, mesh->private_key, to->ecdsa, label, sizeof(label) - 1, send_initial_sptps_data, receive_sptps_record);
125 /* REQ_KEY is overloaded to allow arbitrary requests to be routed between two nodes. */
127 static bool req_key_ext_h(meshlink_handle_t *mesh, connection_t *c, const char *request, node_t *from, int reqno) {
130 if(!from->nexthop || !from->nexthop->connection) {
131 logger(mesh, MESHLINK_WARNING, "Cannot answer REQ_KEY from %s via %s", from->name, from->nexthop ? from->nexthop->name : from->name);
137 char *pubkey = ecdsa_get_base64_public_key(mesh->private_key);
139 if(!node_read_public_key(mesh, from)) {
140 char hiskey[MAX_STRING_SIZE];
142 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, hiskey) == 1) {
143 from->ecdsa = ecdsa_set_base64_public_key(hiskey);
146 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_PUBKEY", from->name, "invalid pubkey");
152 send_request(mesh, from->nexthop->connection, NULL, "%d %s %s %d %s", REQ_KEY, mesh->self->name, from->name, ANS_PUBKEY, pubkey);
158 if(node_read_public_key(mesh, from)) {
159 logger(mesh, MESHLINK_WARNING, "Got ANS_PUBKEY from %s even though we already have his pubkey", from->name);
163 char pubkey[MAX_STRING_SIZE];
165 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, pubkey) != 1 || !(from->ecdsa = ecdsa_set_base64_public_key(pubkey))) {
166 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ANS_PUBKEY", from->name, "invalid pubkey");
170 logger(mesh, MESHLINK_INFO, "Learned ECDSA public key from %s", from->name);
171 from->status.dirty = true;
173 if(!node_write_config(mesh, from, true)) {
177 /* If we are trying to form an outgoing connection to this node, retry immediately */
178 for list_each(outgoing_t, outgoing, mesh->outgoings) {
179 if(outgoing->node == from && outgoing->ev.cb) {
180 outgoing->timeout = 0;
181 timeout_set(&mesh->loop, &outgoing->ev, &(struct timespec) {
187 /* Also reset any UTCP timers */
188 utcp_reset_timers(from->utcp);
194 if(!node_read_public_key(mesh, from)) {
195 logger(mesh, MESHLINK_DEBUG, "No ECDSA key known for %s", from->name);
196 send_request(mesh, from->nexthop->connection, NULL, "%d %s %s %d", REQ_KEY, mesh->self->name, from->name, REQ_PUBKEY);
200 if(from->sptps.label) {
201 logger(mesh, MESHLINK_DEBUG, "Got REQ_KEY from %s while we already started a SPTPS session!", from->name);
203 if(mesh->loop.now.tv_sec < from->last_req_key + req_key_timeout && strcmp(mesh->self->name, from->name) < 0) {
204 logger(mesh, MESHLINK_DEBUG, "Ignoring REQ_KEY from %s.", from->name);
209 char buf[MAX_STRING_SIZE];
212 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
213 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_SPTPS_START", from->name, "invalid SPTPS data");
217 char label[sizeof(meshlink_udp_label) + strlen(from->name) + strlen(mesh->self->name) + 2];
218 snprintf(label, sizeof(label), "%s %s %s", meshlink_udp_label, from->name, mesh->self->name);
219 sptps_stop(&from->sptps);
220 from->status.validkey = false;
221 from->status.waitingforkey = true;
222 from->last_req_key = mesh->loop.now.tv_sec;
224 /* Send our canonical address to help with UDP hole punching */
225 send_canonical_address(mesh, from);
227 if(!sptps_start(&from->sptps, from, false, true, mesh->private_key, from->ecdsa, label, sizeof(label) - 1, send_sptps_data, receive_sptps_record)) {
228 logger(mesh, MESHLINK_ERROR, "Could not start SPTPS session with %s: %s", from->name, strerror(errno));
232 if(!sptps_receive_data(&from->sptps, buf, len)) {
233 logger(mesh, MESHLINK_ERROR, "Could not process SPTPS data from %s: %s", from->name, strerror(errno));
241 if(!from->status.validkey) {
242 logger(mesh, MESHLINK_ERROR, "Got REQ_SPTPS from %s but we don't have a valid key yet", from->name);
246 char buf[MAX_STRING_SIZE];
249 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
250 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_SPTPS", from->name, "invalid SPTPS data");
254 if(!sptps_receive_data(&from->sptps, buf, len)) {
255 logger(mesh, MESHLINK_ERROR, "Could not process SPTPS data from %s: %s", from->name, strerror(errno));
262 case REQ_CANONICAL: {
263 char host[MAX_STRING_SIZE];
264 char port[MAX_STRING_SIZE];
266 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING " " MAX_STRING, host, port) != 2) {
267 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_CANONICAL", from->name, "invalid canonical address");
271 strncat(host, " ", MAX_STRING_SIZE - 1);
272 strncat(host, port, MAX_STRING_SIZE - 1);
274 if(from->canonical_address && !strcmp(from->canonical_address, host)) {
278 logger(mesh, MESHLINK_DEBUG, "Updating canonical address of %s to %s", from->name, host);
279 free(from->canonical_address);
280 from->canonical_address = xstrdup(host);
285 logger(mesh, MESHLINK_ERROR, "Unknown extended REQ_KEY request from %s: %s", from->name, request);
290 bool req_key_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
294 char from_name[MAX_STRING_SIZE];
295 char to_name[MAX_STRING_SIZE];
299 if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING " %d", from_name, to_name, &reqno) < 2) {
300 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "REQ_KEY", c->name);
304 if(!check_id(from_name) || !check_id(to_name)) {
305 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "REQ_KEY", c->name, "invalid name");
309 from = lookup_node(mesh, from_name);
312 logger(mesh, MESHLINK_ERROR, "Got %s from %s origin %s which does not exist in our connection list",
313 "REQ_KEY", c->name, from_name);
317 to = lookup_node(mesh, to_name);
320 logger(mesh, MESHLINK_ERROR, "Got %s from %s destination %s which does not exist in our connection list",
321 "REQ_KEY", c->name, to_name);
325 /* Check if this key request is for us */
327 if(to == mesh->self) { /* Yes */
328 /* Is this an extended REQ_KEY message? */
330 return req_key_ext_h(mesh, c, request, from, reqno);
333 /* This should never happen. Ignore it, unless it came directly from the connected peer, in which case we disconnect. */
334 return from->connection != c;
336 if(!to->status.reachable || !to->nexthop || !to->nexthop->connection) {
337 logger(mesh, MESHLINK_WARNING, "Got %s from %s destination %s which is not reachable",
338 "REQ_KEY", c->name, to_name);
342 send_request(mesh, to->nexthop->connection, NULL, "%s", request);
348 bool ans_key_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
352 char from_name[MAX_STRING_SIZE];
353 char to_name[MAX_STRING_SIZE];
354 char key[MAX_STRING_SIZE];
355 char address[MAX_STRING_SIZE] = "";
356 char port[MAX_STRING_SIZE] = "";
357 int cipher, digest, maclength, compression;
360 if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
361 from_name, to_name, key, &cipher, &digest, &maclength,
362 &compression, address, port) < 7) {
363 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s", "ANS_KEY", c->name);
367 if(!check_id(from_name) || !check_id(to_name)) {
368 logger(mesh, MESHLINK_ERROR, "Got bad %s from %s: %s", "ANS_KEY", c->name, "invalid name");
372 from = lookup_node(mesh, from_name);
375 logger(mesh, MESHLINK_ERROR, "Got %s from %s origin %s which does not exist in our connection list",
376 "ANS_KEY", c->name, from_name);
380 to = lookup_node(mesh, to_name);
383 logger(mesh, MESHLINK_ERROR, "Got %s from %s destination %s which does not exist in our connection list",
384 "ANS_KEY", c->name, to_name);
388 /* Forward it if necessary */
390 if(to != mesh->self) {
391 if(!to->status.reachable) {
392 logger(mesh, MESHLINK_WARNING, "Got %s from %s destination %s which is not reachable",
393 "ANS_KEY", c->name, to_name);
398 logger(mesh, MESHLINK_WARNING, "Got %s from %s from %s to %s",
399 "ANS_KEY", c->name, from_name, to_name);
403 if(!to->nexthop || !to->nexthop->connection) {
404 logger(mesh, MESHLINK_WARNING, "Cannot forward ANS_KEY to %s via %s", to->name, to->nexthop ? to->nexthop->name : to->name);
408 /* Append the known UDP address of the from node, if we have a confirmed one */
409 if(!*address && from->status.udp_confirmed && from->address.sa.sa_family != AF_UNSPEC) {
410 char *reflexive_address, *reflexive_port;
411 logger(mesh, MESHLINK_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
412 sockaddr2str(&from->address, &reflexive_address, &reflexive_port);
413 send_request(mesh, to->nexthop->connection, NULL, "%s %s %s", request, reflexive_address, reflexive_port);
414 free(reflexive_address);
415 free(reflexive_port);
419 return send_request(mesh, to->nexthop->connection, NULL, "%s", request);
422 /* Is this an ANS_KEY informing us of our own reflexive UDP address? */
424 if(from == mesh->self) {
425 if(*key == '.' && *address && *port) {
426 logger(mesh, MESHLINK_DEBUG, "Learned our own reflexive UDP address from %s: %s port %s", c->name, address, port);
428 /* Inform all other nodes we want to communicate with and which are reachable via this connection */
429 for splay_each(node_t, n, mesh->nodes) {
430 if(n->nexthop != c->node) {
434 if(n->status.udp_confirmed) {
438 if(!n->status.waitingforkey && !n->status.validkey) {
442 if(!n->nexthop->connection) {
446 logger(mesh, MESHLINK_DEBUG, "Forwarding our own reflexive UDP address to %s", n->name);
447 send_request(mesh, c, NULL, "%d %s %s . -1 -1 -1 0 %s %s", ANS_KEY, mesh->self->name, n->name, address, port);
450 logger(mesh, MESHLINK_WARNING, "Got %s from %s from %s to %s",
451 "ANS_KEY", c->name, from_name, to_name);
457 /* Process SPTPS data if present */
460 /* Don't use key material until every check has passed. */
461 from->status.validkey = false;
463 /* Compression is not supported. */
464 if(compression != 0) {
465 logger(mesh, MESHLINK_ERROR, "Node %s uses bogus compression level!", from->name);
469 char buf[strlen(key)];
470 int len = b64decode(key, buf, strlen(key));
472 if(!len || !sptps_receive_data(&from->sptps, buf, len)) {
473 logger(mesh, MESHLINK_ERROR, "Error processing SPTPS data from %s", from->name);
477 if(from->status.validkey) {
478 if(*address && *port) {
479 logger(mesh, MESHLINK_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
480 sockaddr_t sa = str2sockaddr(address, port);
481 update_node_udp(mesh, from, &sa);
484 send_mtu_probe(mesh, from);