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"
35 static bool mykeyused = false;
37 void send_key_changed(void) {
38 send_request(everyone, "%d %x %s", KEY_CHANGED, rand(), myself->name);
40 /* Force key exchange for connections using SPTPS */
42 for splay_each(node_t, n, node_tree)
43 if(n->status.reachable && n->status.validkey)
44 sptps_force_kex(&n->sptps);
47 bool key_changed_h(connection_t *c, const char *request) {
48 char name[MAX_STRING_SIZE];
51 if(sscanf(request, "%*d %*x " MAX_STRING, name) != 1) {
52 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "KEY_CHANGED",
53 c->name, c->hostname);
57 if(seen_request(request))
60 n = lookup_node(name);
63 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
64 "KEY_CHANGED", c->name, c->hostname, name);
70 forward_request(c, request);
75 static bool send_initial_sptps_data(void *handle, uint8_t type, const char *data, size_t len) {
77 to->sptps.send_data = send_sptps_data;
78 char buf[len * 4 / 3 + 5];
79 b64encode(data, buf, len);
80 return send_request(to->nexthop->connection, "%d %s %s %d %s", REQ_KEY, myself->name, to->name, REQ_KEY, buf);
83 bool send_req_key(node_t *to) {
84 if(!node_read_ecdsa_public_key(to)) {
85 logger(DEBUG_PROTOCOL, LOG_DEBUG, "No ECDSA key known for %s (%s)", to->name, to->hostname);
86 send_request(to->nexthop->connection, "%d %s %s %d", REQ_KEY, myself->name, to->name, REQ_PUBKEY);
91 logger(DEBUG_ALWAYS, LOG_DEBUG, "send_req_key(%s) called while sptps->label != NULL!", to->name);
93 char label[25 + strlen(myself->name) + strlen(to->name)];
94 snprintf(label, sizeof label, "tinc UDP key expansion %s %s", myself->name, to->name);
95 sptps_stop(&to->sptps);
96 to->status.validkey = false;
97 to->status.waitingforkey = true;
98 to->last_req_key = now.tv_sec;
99 to->incompression = myself->incompression;
100 return sptps_start(&to->sptps, to, true, true, myself->connection->ecdsa, to->ecdsa, label, sizeof label, send_initial_sptps_data, receive_sptps_record);
103 /* REQ_KEY is overloaded to allow arbitrary requests to be routed between two nodes. */
105 static bool req_key_ext_h(connection_t *c, const char *request, node_t *from, int reqno) {
108 char *pubkey = ecdsa_get_base64_public_key(myself->connection->ecdsa);
109 send_request(from->nexthop->connection, "%d %s %s %d %s", REQ_KEY, myself->name, from->name, ANS_PUBKEY, pubkey);
115 if(node_read_ecdsa_public_key(from)) {
116 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got ANS_PUBKEY from %s (%s) even though we already have his pubkey", from->name, from->hostname);
120 char pubkey[MAX_STRING_SIZE];
121 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, pubkey) != 1 || !(from->ecdsa = ecdsa_set_base64_public_key(pubkey))) {
122 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_PUBKEY", from->name, from->hostname, "invalid pubkey");
126 logger(DEBUG_PROTOCOL, LOG_INFO, "Learned ECDSA public key from %s (%s)", from->name, from->hostname);
127 append_config_file(from->name, "ECDSAPublicKey", pubkey);
132 if(!node_read_ecdsa_public_key(from)) {
133 logger(DEBUG_PROTOCOL, LOG_DEBUG, "No ECDSA key known for %s (%s)", from->name, from->hostname);
134 send_request(from->nexthop->connection, "%d %s %s %d", REQ_KEY, myself->name, from->name, REQ_PUBKEY);
138 if(from->sptps.label)
139 logger(DEBUG_ALWAYS, LOG_DEBUG, "Got REQ_KEY from %s while we already started a SPTPS session!", from->name);
141 char buf[MAX_STRING_SIZE];
144 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
145 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_SPTPS_START", from->name, from->hostname, "invalid SPTPS data");
149 char label[25 + strlen(from->name) + strlen(myself->name)];
150 snprintf(label, sizeof label, "tinc UDP key expansion %s %s", from->name, myself->name);
151 sptps_stop(&from->sptps);
152 from->status.validkey = false;
153 from->status.waitingforkey = true;
154 from->last_req_key = now.tv_sec;
155 sptps_start(&from->sptps, from, false, true, myself->connection->ecdsa, from->ecdsa, label, sizeof label, send_sptps_data, receive_sptps_record);
156 sptps_receive_data(&from->sptps, buf, len);
161 if(!from->status.validkey) {
162 logger(DEBUG_PROTOCOL, LOG_ERR, "Got REQ_SPTPS from %s (%s) but we don't have a valid key yet", from->name, from->hostname);
166 char buf[MAX_STRING_SIZE];
168 if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
169 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_SPTPS", from->name, from->hostname, "invalid SPTPS data");
172 sptps_receive_data(&from->sptps, buf, len);
177 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown extended REQ_KEY request from %s (%s): %s", from->name, from->hostname, request);
182 bool req_key_h(connection_t *c, const char *request) {
183 char from_name[MAX_STRING_SIZE];
184 char to_name[MAX_STRING_SIZE];
188 if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING " %d", from_name, to_name, &reqno) < 2) {
189 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
194 if(!check_id(from_name) || !check_id(to_name)) {
195 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", c->name, c->hostname, "invalid name");
199 from = lookup_node(from_name);
202 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
203 "REQ_KEY", c->name, c->hostname, from_name);
207 to = lookup_node(to_name);
210 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
211 "REQ_KEY", c->name, c->hostname, to_name);
215 /* Check if this key request is for us */
217 if(to == myself) { /* Yes */
218 /* Is this an extended REQ_KEY message? */
220 return req_key_ext_h(c, request, from, reqno);
222 /* No, just send our key back */
225 if(!to->status.reachable) {
226 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
227 "REQ_KEY", c->name, c->hostname, to_name);
231 send_request(to->nexthop->connection, "%s", request);
237 bool send_ans_key(node_t *to) {
241 bool ans_key_h(connection_t *c, const char *request) {
242 char from_name[MAX_STRING_SIZE];
243 char to_name[MAX_STRING_SIZE];
244 char key[MAX_STRING_SIZE];
245 char address[MAX_STRING_SIZE] = "";
246 char port[MAX_STRING_SIZE] = "";
247 int cipher, digest, maclength, compression, keylen;
250 if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
251 from_name, to_name, key, &cipher, &digest, &maclength,
252 &compression, address, port) < 7) {
253 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
258 if(!check_id(from_name) || !check_id(to_name)) {
259 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
263 from = lookup_node(from_name);
266 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
267 "ANS_KEY", c->name, c->hostname, from_name);
271 to = lookup_node(to_name);
274 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
275 "ANS_KEY", c->name, c->hostname, to_name);
279 /* Forward it if necessary */
282 if(!to->status.reachable) {
283 logger(DEBUG_ALWAYS, LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
284 "ANS_KEY", c->name, c->hostname, to_name);
288 if(!*address && from->address.sa.sa_family != AF_UNSPEC) {
289 char *address, *port;
290 logger(DEBUG_PROTOCOL, LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
291 sockaddr2str(&from->address, &address, &port);
292 send_request(to->nexthop->connection, "%s %s %s", request, address, port);
298 return send_request(to->nexthop->connection, "%s", request);
301 /* Don't use key material until every check has passed. */
302 from->status.validkey = false;
304 if(compression < 0 || compression > 11) {
305 logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
309 from->outcompression = compression;
311 /* SPTPS or old-style key exchange? */
313 char buf[strlen(key)];
314 int len = b64decode(key, buf, strlen(key));
316 if(!len || !sptps_receive_data(&from->sptps, buf, len))
317 logger(DEBUG_ALWAYS, LOG_ERR, "Error processing SPTPS data from %s (%s)", from->name, from->hostname);
319 if(from->status.validkey) {
320 if(*address && *port) {
321 logger(DEBUG_PROTOCOL, LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
322 sockaddr_t sa = str2sockaddr(address, port);
323 update_node_udp(from, &sa);
326 if(from->options & OPTION_PMTU_DISCOVERY && !(from->options & OPTION_TCPONLY))
327 send_mtu_probe(from);