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.
23 #include "connection.h"
26 #include "meshlink_internal.h"
36 static bool mykeyused = false;
38 void send_key_changed(void) {
39 send_request(everyone, "%d %x %s", KEY_CHANGED, rand(), mesh->self->name);
41 /* Force key exchange for connections using SPTPS */
43 for splay_each(node_t, n, mesh->nodes)
44 if(n->status.reachable && n->status.validkey)
45 sptps_force_kex(&n->sptps);
48 bool key_changed_h(connection_t *c, const char *request) {
49 char name[MAX_STRING_SIZE];
52 if(sscanf(request, "%*d %*x " MAX_STRING, name) != 1) {
53 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "KEY_CHANGED",
54 c->name, c->hostname);
58 if(seen_request(request))
61 n = lookup_node(name);
64 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
65 "KEY_CHANGED", c->name, c->hostname, name);
71 forward_request(c, request);
76 static bool send_initial_sptps_data(void *handle, uint8_t type, const char *data, size_t len) {
78 to->sptps.send_data = send_sptps_data;
79 char buf[len * 4 / 3 + 5];
80 b64encode(data, buf, len);
81 return send_request(to->nexthop->connection, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_KEY, buf);
84 bool send_req_key(node_t *to) {
85 if(!node_read_ecdsa_public_key(to)) {
86 logger(DEBUG_PROTOCOL, LOG_DEBUG, "No ECDSA key known for %s (%s)", to->name, to->hostname);
87 send_request(to->nexthop->connection, "%d %s %s %d", REQ_KEY, mesh->self->name, to->name, REQ_PUBKEY);
92 logger(DEBUG_ALWAYS, LOG_DEBUG, "send_req_key(%s) called while sptps->label != NULL!", to->name);
94 char label[25 + strlen(mesh->self->name) + strlen(to->name)];
95 snprintf(label, sizeof label, "tinc UDP key expansion %s %s", mesh->self->name, to->name);
96 sptps_stop(&to->sptps);
97 to->status.validkey = false;
98 to->status.waitingforkey = true;
99 to->last_req_key = now.tv_sec;
100 to->incompression = mesh->self->incompression;
101 return sptps_start(&to->sptps, to, true, true, mesh->self->connection->ecdsa, to->ecdsa, label, sizeof label, send_initial_sptps_data, receive_sptps_record);
104 /* REQ_KEY is overloaded to allow arbitrary requests to be routed between two nodes. */
106 static bool req_key_ext_h(connection_t *c, const char *request, node_t *from, int reqno) {
109 char *pubkey = ecdsa_get_base64_public_key(mesh->self->connection->ecdsa);
110 send_request(from->nexthop->connection, "%d %s %s %d %s", REQ_KEY, mesh->self->name, from->name, ANS_PUBKEY, pubkey);
116 if(node_read_ecdsa_public_key(from)) {
117 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got ANS_PUBKEY from %s (%s) even though we already have his pubkey", from->name, from->hostname);
121 char pubkey[MAX_STRING_SIZE];
122 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, pubkey) != 1 || !(from->ecdsa = ecdsa_set_base64_public_key(pubkey))) {
123 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_PUBKEY", from->name, from->hostname, "invalid pubkey");
127 logger(DEBUG_PROTOCOL, LOG_INFO, "Learned ECDSA public key from %s (%s)", from->name, from->hostname);
128 append_config_file(from->name, "ECDSAPublicKey", pubkey);
133 if(!node_read_ecdsa_public_key(from)) {
134 logger(DEBUG_PROTOCOL, LOG_DEBUG, "No ECDSA key known for %s (%s)", from->name, from->hostname);
135 send_request(from->nexthop->connection, "%d %s %s %d", REQ_KEY, mesh->self->name, from->name, REQ_PUBKEY);
139 if(from->sptps.label)
140 logger(DEBUG_ALWAYS, LOG_DEBUG, "Got REQ_KEY from %s while we already started a SPTPS session!", from->name);
142 char buf[MAX_STRING_SIZE];
145 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
146 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_SPTPS_START", from->name, from->hostname, "invalid SPTPS data");
150 char label[25 + strlen(from->name) + strlen(mesh->self->name)];
151 snprintf(label, sizeof label, "tinc UDP key expansion %s %s", from->name, mesh->self->name);
152 sptps_stop(&from->sptps);
153 from->status.validkey = false;
154 from->status.waitingforkey = true;
155 from->last_req_key = now.tv_sec;
156 sptps_start(&from->sptps, from, false, true, mesh->self->connection->ecdsa, from->ecdsa, label, sizeof label, send_sptps_data, receive_sptps_record);
157 sptps_receive_data(&from->sptps, buf, len);
162 if(!from->status.validkey) {
163 logger(DEBUG_PROTOCOL, LOG_ERR, "Got REQ_SPTPS from %s (%s) but we don't have a valid key yet", from->name, from->hostname);
167 char buf[MAX_STRING_SIZE];
169 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
170 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_SPTPS", from->name, from->hostname, "invalid SPTPS data");
173 sptps_receive_data(&from->sptps, buf, len);
178 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown extended REQ_KEY request from %s (%s): %s", from->name, from->hostname, request);
183 bool req_key_h(connection_t *c, const char *request) {
184 char from_name[MAX_STRING_SIZE];
185 char to_name[MAX_STRING_SIZE];
189 if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING " %d", from_name, to_name, &reqno) < 2) {
190 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
195 if(!check_id(from_name) || !check_id(to_name)) {
196 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", c->name, c->hostname, "invalid name");
200 from = lookup_node(from_name);
203 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
204 "REQ_KEY", c->name, c->hostname, from_name);
208 to = lookup_node(to_name);
211 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
212 "REQ_KEY", c->name, c->hostname, to_name);
216 /* Check if this key request is for us */
218 if(to == mesh->self) { /* Yes */
219 /* Is this an extended REQ_KEY message? */
221 return req_key_ext_h(c, request, from, reqno);
223 /* No, just send our key back */
226 if(!to->status.reachable) {
227 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
228 "REQ_KEY", c->name, c->hostname, to_name);
232 send_request(to->nexthop->connection, "%s", request);
238 bool send_ans_key(node_t *to) {
242 bool ans_key_h(connection_t *c, const char *request) {
243 char from_name[MAX_STRING_SIZE];
244 char to_name[MAX_STRING_SIZE];
245 char key[MAX_STRING_SIZE];
246 char address[MAX_STRING_SIZE] = "";
247 char port[MAX_STRING_SIZE] = "";
248 int cipher, digest, maclength, compression, keylen;
251 if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
252 from_name, to_name, key, &cipher, &digest, &maclength,
253 &compression, address, port) < 7) {
254 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
259 if(!check_id(from_name) || !check_id(to_name)) {
260 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
264 from = lookup_node(from_name);
267 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
268 "ANS_KEY", c->name, c->hostname, from_name);
272 to = lookup_node(to_name);
275 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
276 "ANS_KEY", c->name, c->hostname, to_name);
280 /* Forward it if necessary */
282 if(to != mesh->self) {
283 if(!to->status.reachable) {
284 logger(DEBUG_ALWAYS, LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
285 "ANS_KEY", c->name, c->hostname, to_name);
289 if(!*address && from->address.sa.sa_family != AF_UNSPEC) {
290 char *address, *port;
291 logger(DEBUG_PROTOCOL, LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
292 sockaddr2str(&from->address, &address, &port);
293 send_request(to->nexthop->connection, "%s %s %s", request, address, port);
299 return send_request(to->nexthop->connection, "%s", request);
302 /* Don't use key material until every check has passed. */
303 from->status.validkey = false;
305 if(compression < 0 || compression > 11) {
306 logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
310 from->outcompression = compression;
312 /* SPTPS or old-style key exchange? */
314 char buf[strlen(key)];
315 int len = b64decode(key, buf, strlen(key));
317 if(!len || !sptps_receive_data(&from->sptps, buf, len))
318 logger(DEBUG_ALWAYS, LOG_ERR, "Error processing SPTPS data from %s (%s)", from->name, from->hostname);
320 if(from->status.validkey) {
321 if(*address && *port) {
322 logger(DEBUG_PROTOCOL, LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
323 sockaddr_t sa = str2sockaddr(address, port);
324 update_node_udp(from, &sa);
327 if(from->options & OPTION_PMTU_DISCOVERY && !(from->options & OPTION_TCPONLY))
328 send_mtu_probe(from);