2 protocol_key.c -- handle the meta-protocol, key exchange
3 Copyright (C) 2014 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 void send_key_changed(meshlink_handle_t *mesh) {
35 send_request(mesh, mesh->everyone, "%d %x %s", KEY_CHANGED, rand(), mesh->self->name);
37 /* Force key exchange for connections using SPTPS */
39 for splay_each(node_t, n, mesh->nodes)
40 if(n->status.reachable && n->status.validkey)
41 sptps_force_kex(&n->sptps);
44 bool key_changed_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
45 char name[MAX_STRING_SIZE];
48 if(sscanf(request, "%*d %*x " MAX_STRING, name) != 1) {
49 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "KEY_CHANGED",
50 c->name, c->hostname);
54 if(seen_request(mesh, request))
57 n = lookup_node(mesh, name);
60 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
61 "KEY_CHANGED", c->name, c->hostname, name);
67 forward_request(mesh, c, request);
72 static bool send_initial_sptps_data(void *handle, uint8_t type, const void *data, size_t len) {
74 meshlink_handle_t *mesh = to->mesh;
75 to->sptps.send_data = send_sptps_data;
76 char buf[len * 4 / 3 + 5];
77 b64encode(data, buf, len);
78 return send_request(mesh, to->nexthop->connection, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_KEY, buf);
81 bool send_req_key(meshlink_handle_t *mesh, node_t *to) {
82 if(!node_read_ecdsa_public_key(mesh, to)) {
83 logger(DEBUG_PROTOCOL, LOG_DEBUG, "No ECDSA key known for %s (%s)", to->name, to->hostname);
84 send_request(mesh, to->nexthop->connection, "%d %s %s %d", REQ_KEY, mesh->self->name, to->name, REQ_PUBKEY);
89 logger(DEBUG_ALWAYS, LOG_DEBUG, "send_req_key(%s) called while sptps->label != NULL!", to->name);
91 char label[25 + strlen(mesh->self->name) + strlen(to->name)];
92 snprintf(label, sizeof label, "MeshLink UDP key expansion %s %s", mesh->self->name, to->name);
93 sptps_stop(&to->sptps);
94 to->status.validkey = false;
95 to->status.waitingforkey = true;
96 to->last_req_key = mesh->loop.now.tv_sec;
97 to->incompression = mesh->self->incompression;
98 return sptps_start(&to->sptps, to, true, true, mesh->self->connection->ecdsa, to->ecdsa, label, sizeof label, send_initial_sptps_data, receive_sptps_record);
101 /* REQ_KEY is overloaded to allow arbitrary requests to be routed between two nodes. */
103 static bool req_key_ext_h(meshlink_handle_t *mesh, connection_t *c, const char *request, node_t *from, int reqno) {
106 char *pubkey = ecdsa_get_base64_public_key(mesh->self->connection->ecdsa);
107 send_request(mesh, from->nexthop->connection, "%d %s %s %d %s", REQ_KEY, mesh->self->name, from->name, ANS_PUBKEY, pubkey);
113 if(node_read_ecdsa_public_key(mesh, from)) {
114 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got ANS_PUBKEY from %s (%s) even though we already have his pubkey", from->name, from->hostname);
118 char pubkey[MAX_STRING_SIZE];
119 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, pubkey) != 1 || !(from->ecdsa = ecdsa_set_base64_public_key(pubkey))) {
120 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_PUBKEY", from->name, from->hostname, "invalid pubkey");
124 logger(DEBUG_PROTOCOL, LOG_INFO, "Learned ECDSA public key from %s (%s)", from->name, from->hostname);
125 append_config_file(mesh, from->name, "ECDSAPublicKey", pubkey);
130 if(!node_read_ecdsa_public_key(mesh, from)) {
131 logger(DEBUG_PROTOCOL, LOG_DEBUG, "No ECDSA key known for %s (%s)", from->name, from->hostname);
132 send_request(mesh, from->nexthop->connection, "%d %s %s %d", REQ_KEY, mesh->self->name, from->name, REQ_PUBKEY);
136 if(from->sptps.label)
137 logger(DEBUG_ALWAYS, LOG_DEBUG, "Got REQ_KEY from %s while we already started a SPTPS session!", from->name);
139 char buf[MAX_STRING_SIZE];
142 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
143 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_SPTPS_START", from->name, from->hostname, "invalid SPTPS data");
147 char label[25 + strlen(from->name) + strlen(mesh->self->name)];
148 snprintf(label, sizeof label, "MeshLink UDP key expansion %s %s", from->name, mesh->self->name);
149 sptps_stop(&from->sptps);
150 from->status.validkey = false;
151 from->status.waitingforkey = true;
152 from->last_req_key = mesh->loop.now.tv_sec;
153 sptps_start(&from->sptps, from, false, true, mesh->self->connection->ecdsa, from->ecdsa, label, sizeof label, send_sptps_data, receive_sptps_record);
154 sptps_receive_data(&from->sptps, buf, len);
159 if(!from->status.validkey) {
160 logger(DEBUG_PROTOCOL, LOG_ERR, "Got REQ_SPTPS from %s (%s) but we don't have a valid key yet", from->name, from->hostname);
164 char buf[MAX_STRING_SIZE];
166 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
167 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_SPTPS", from->name, from->hostname, "invalid SPTPS data");
170 sptps_receive_data(&from->sptps, buf, len);
175 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown extended REQ_KEY request from %s (%s): %s", from->name, from->hostname, request);
180 bool req_key_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
181 char from_name[MAX_STRING_SIZE];
182 char to_name[MAX_STRING_SIZE];
186 if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING " %d", from_name, to_name, &reqno) < 2) {
187 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
192 if(!check_id(from_name) || !check_id(to_name)) {
193 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", c->name, c->hostname, "invalid name");
197 from = lookup_node(mesh, from_name);
200 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
201 "REQ_KEY", c->name, c->hostname, from_name);
205 to = lookup_node(mesh, to_name);
208 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
209 "REQ_KEY", c->name, c->hostname, to_name);
213 /* Check if this key request is for us */
215 if(to == mesh->self) { /* Yes */
216 /* Is this an extended REQ_KEY message? */
218 return req_key_ext_h(mesh, c, request, from, reqno);
220 /* No, just send our key back */
221 send_ans_key(mesh, from);
223 if(!to->status.reachable) {
224 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
225 "REQ_KEY", c->name, c->hostname, to_name);
229 send_request(mesh, to->nexthop->connection, "%s", request);
235 bool send_ans_key(meshlink_handle_t *mesh, node_t *to) {
239 bool ans_key_h(meshlink_handle_t *mesh, connection_t *c, const char *request) {
240 char from_name[MAX_STRING_SIZE];
241 char to_name[MAX_STRING_SIZE];
242 char key[MAX_STRING_SIZE];
243 char address[MAX_STRING_SIZE] = "";
244 char port[MAX_STRING_SIZE] = "";
245 int cipher, digest, maclength, compression;
248 if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
249 from_name, to_name, key, &cipher, &digest, &maclength,
250 &compression, address, port) < 7) {
251 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
256 if(!check_id(from_name) || !check_id(to_name)) {
257 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
261 from = lookup_node(mesh, from_name);
264 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
265 "ANS_KEY", c->name, c->hostname, from_name);
269 to = lookup_node(mesh, to_name);
272 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
273 "ANS_KEY", c->name, c->hostname, to_name);
277 /* Forward it if necessary */
279 if(to != mesh->self) {
280 if(!to->status.reachable) {
281 logger(DEBUG_ALWAYS, LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
282 "ANS_KEY", c->name, c->hostname, to_name);
286 if(!*address && from->address.sa.sa_family != AF_UNSPEC) {
287 char *address, *port;
288 logger(DEBUG_PROTOCOL, LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
289 sockaddr2str(&from->address, &address, &port);
290 send_request(mesh, to->nexthop->connection, "%s %s %s", request, address, port);
296 return send_request(mesh, to->nexthop->connection, "%s", request);
299 /* Don't use key material until every check has passed. */
300 from->status.validkey = false;
302 if(compression < 0 || compression > 11) {
303 logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
307 from->outcompression = compression;
309 /* SPTPS or old-style key exchange? */
311 char buf[strlen(key)];
312 int len = b64decode(key, buf, strlen(key));
314 if(!len || !sptps_receive_data(&from->sptps, buf, len))
315 logger(DEBUG_ALWAYS, LOG_ERR, "Error processing SPTPS data from %s (%s)", from->name, from->hostname);
317 if(from->status.validkey) {
318 if(*address && *port) {
319 logger(DEBUG_PROTOCOL, LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
320 sockaddr_t sa = str2sockaddr(address, port);
321 update_node_udp(mesh, from, &sa);
324 if(from->options & OPTION_PMTU_DISCOVERY && !(from->options & OPTION_TCPONLY))
325 send_mtu_probe(mesh, from);