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 static bool mykeyused = false;
36 void send_key_changed(void) {
37 send_request(mesh->everyone, "%d %x %s", KEY_CHANGED, rand(), mesh->self->name);
39 /* Force key exchange for connections using SPTPS */
41 for splay_each(node_t, n, mesh->nodes)
42 if(n->status.reachable && n->status.validkey)
43 sptps_force_kex(&n->sptps);
46 bool key_changed_h(connection_t *c, const char *request) {
47 char name[MAX_STRING_SIZE];
50 if(sscanf(request, "%*d %*x " MAX_STRING, name) != 1) {
51 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "KEY_CHANGED",
52 c->name, c->hostname);
56 if(seen_request(request))
59 n = lookup_node(name);
62 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
63 "KEY_CHANGED", c->name, c->hostname, name);
69 forward_request(c, request);
74 static bool send_initial_sptps_data(void *handle, uint8_t type, const char *data, size_t len) {
76 to->sptps.send_data = send_sptps_data;
77 char buf[len * 4 / 3 + 5];
78 b64encode(data, buf, len);
79 return send_request(to->nexthop->connection, "%d %s %s %d %s", REQ_KEY, mesh->self->name, to->name, REQ_KEY, buf);
82 bool send_req_key(node_t *to) {
83 if(!node_read_ecdsa_public_key(to)) {
84 logger(DEBUG_PROTOCOL, LOG_DEBUG, "No ECDSA key known for %s (%s)", to->name, to->hostname);
85 send_request(to->nexthop->connection, "%d %s %s %d", REQ_KEY, mesh->self->name, to->name, REQ_PUBKEY);
90 logger(DEBUG_ALWAYS, LOG_DEBUG, "send_req_key(%s) called while sptps->label != NULL!", to->name);
92 char label[25 + strlen(mesh->self->name) + strlen(to->name)];
93 snprintf(label, sizeof label, "tinc UDP key expansion %s %s", mesh->self->name, to->name);
94 sptps_stop(&to->sptps);
95 to->status.validkey = false;
96 to->status.waitingforkey = true;
97 to->last_req_key = now.tv_sec;
98 to->incompression = mesh->self->incompression;
99 return sptps_start(&to->sptps, to, true, true, mesh->self->connection->ecdsa, to->ecdsa, label, sizeof label, send_initial_sptps_data, receive_sptps_record);
102 /* REQ_KEY is overloaded to allow arbitrary requests to be routed between two nodes. */
104 static bool req_key_ext_h(connection_t *c, const char *request, node_t *from, int reqno) {
107 char *pubkey = ecdsa_get_base64_public_key(mesh->self->connection->ecdsa);
108 send_request(from->nexthop->connection, "%d %s %s %d %s", REQ_KEY, mesh->self->name, from->name, ANS_PUBKEY, pubkey);
114 if(node_read_ecdsa_public_key(from)) {
115 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got ANS_PUBKEY from %s (%s) even though we already have his pubkey", from->name, from->hostname);
119 char pubkey[MAX_STRING_SIZE];
120 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, pubkey) != 1 || !(from->ecdsa = ecdsa_set_base64_public_key(pubkey))) {
121 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_PUBKEY", from->name, from->hostname, "invalid pubkey");
125 logger(DEBUG_PROTOCOL, LOG_INFO, "Learned ECDSA public key from %s (%s)", from->name, from->hostname);
126 append_config_file(from->name, "ECDSAPublicKey", pubkey);
131 if(!node_read_ecdsa_public_key(from)) {
132 logger(DEBUG_PROTOCOL, LOG_DEBUG, "No ECDSA key known for %s (%s)", from->name, from->hostname);
133 send_request(from->nexthop->connection, "%d %s %s %d", REQ_KEY, mesh->self->name, from->name, REQ_PUBKEY);
137 if(from->sptps.label)
138 logger(DEBUG_ALWAYS, LOG_DEBUG, "Got REQ_KEY from %s while we already started a SPTPS session!", from->name);
140 char buf[MAX_STRING_SIZE];
143 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
144 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_SPTPS_START", from->name, from->hostname, "invalid SPTPS data");
148 char label[25 + strlen(from->name) + strlen(mesh->self->name)];
149 snprintf(label, sizeof label, "tinc UDP key expansion %s %s", from->name, mesh->self->name);
150 sptps_stop(&from->sptps);
151 from->status.validkey = false;
152 from->status.waitingforkey = true;
153 from->last_req_key = now.tv_sec;
154 sptps_start(&from->sptps, from, false, true, mesh->self->connection->ecdsa, from->ecdsa, label, sizeof label, send_sptps_data, receive_sptps_record);
155 sptps_receive_data(&from->sptps, buf, len);
160 if(!from->status.validkey) {
161 logger(DEBUG_PROTOCOL, LOG_ERR, "Got REQ_SPTPS from %s (%s) but we don't have a valid key yet", from->name, from->hostname);
165 char buf[MAX_STRING_SIZE];
167 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
168 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_SPTPS", from->name, from->hostname, "invalid SPTPS data");
171 sptps_receive_data(&from->sptps, buf, len);
176 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown extended REQ_KEY request from %s (%s): %s", from->name, from->hostname, request);
181 bool req_key_h(connection_t *c, const char *request) {
182 char from_name[MAX_STRING_SIZE];
183 char to_name[MAX_STRING_SIZE];
187 if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING " %d", from_name, to_name, &reqno) < 2) {
188 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
193 if(!check_id(from_name) || !check_id(to_name)) {
194 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", c->name, c->hostname, "invalid name");
198 from = lookup_node(from_name);
201 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
202 "REQ_KEY", c->name, c->hostname, from_name);
206 to = lookup_node(to_name);
209 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
210 "REQ_KEY", c->name, c->hostname, to_name);
214 /* Check if this key request is for us */
216 if(to == mesh->self) { /* Yes */
217 /* Is this an extended REQ_KEY message? */
219 return req_key_ext_h(c, request, from, reqno);
221 /* No, just send our key back */
224 if(!to->status.reachable) {
225 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
226 "REQ_KEY", c->name, c->hostname, to_name);
230 send_request(to->nexthop->connection, "%s", request);
236 bool send_ans_key(node_t *to) {
240 bool ans_key_h(connection_t *c, const char *request) {
241 char from_name[MAX_STRING_SIZE];
242 char to_name[MAX_STRING_SIZE];
243 char key[MAX_STRING_SIZE];
244 char address[MAX_STRING_SIZE] = "";
245 char port[MAX_STRING_SIZE] = "";
246 int cipher, digest, maclength, compression, keylen;
249 if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
250 from_name, to_name, key, &cipher, &digest, &maclength,
251 &compression, address, port) < 7) {
252 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
257 if(!check_id(from_name) || !check_id(to_name)) {
258 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
262 from = lookup_node(from_name);
265 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
266 "ANS_KEY", c->name, c->hostname, from_name);
270 to = lookup_node(to_name);
273 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
274 "ANS_KEY", c->name, c->hostname, to_name);
278 /* Forward it if necessary */
280 if(to != mesh->self) {
281 if(!to->status.reachable) {
282 logger(DEBUG_ALWAYS, LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
283 "ANS_KEY", c->name, c->hostname, to_name);
287 if(!*address && from->address.sa.sa_family != AF_UNSPEC) {
288 char *address, *port;
289 logger(DEBUG_PROTOCOL, LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
290 sockaddr2str(&from->address, &address, &port);
291 send_request(to->nexthop->connection, "%s %s %s", request, address, port);
297 return send_request(to->nexthop->connection, "%s", request);
300 /* Don't use key material until every check has passed. */
301 from->status.validkey = false;
303 if(compression < 0 || compression > 11) {
304 logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
308 from->outcompression = compression;
310 /* SPTPS or old-style key exchange? */
312 char buf[strlen(key)];
313 int len = b64decode(key, buf, strlen(key));
315 if(!len || !sptps_receive_data(&from->sptps, buf, len))
316 logger(DEBUG_ALWAYS, LOG_ERR, "Error processing SPTPS data from %s (%s)", from->name, from->hostname);
318 if(from->status.validkey) {
319 if(*address && *port) {
320 logger(DEBUG_PROTOCOL, LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
321 sockaddr_t sa = str2sockaddr(address, port);
322 update_node_udp(from, &sa);
325 if(from->options & OPTION_PMTU_DISCOVERY && !(from->options & OPTION_TCPONLY))
326 send_mtu_probe(from);