2 protocol_key.c -- handle the meta-protocol, key exchange
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2011 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "splay_tree.h"
25 #include "connection.h"
37 static bool mykeyused = false;
39 void send_key_changed(void) {
43 send_request(broadcast, "%d %x %s", KEY_CHANGED, rand(), myself->name);
45 /* Immediately send new keys to directly connected nodes to keep UDP mappings alive */
47 for(node = connection_tree->head; node; node = node->next) {
49 if(c->status.active && c->node && c->node->status.reachable)
50 send_ans_key(c->node);
54 bool key_changed_h(connection_t *c, char *request) {
55 char name[MAX_STRING_SIZE];
58 if(sscanf(request, "%*d %*x " MAX_STRING, name) != 1) {
59 logger(LOG_ERR, "Got bad %s from %s (%s)", "KEY_CHANGED",
60 c->name, c->hostname);
64 if(seen_request(request))
67 n = lookup_node(name);
70 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
71 "KEY_CHANGED", c->name, c->hostname, name);
75 n->status.validkey = false;
81 forward_request(c, request);
86 bool send_req_key(node_t *to) {
87 return send_request(to->nexthop->connection, "%d %s %s 1", REQ_KEY, myself->name, to->name);
90 bool req_key_h(connection_t *c, char *request) {
91 char from_name[MAX_STRING_SIZE];
92 char to_name[MAX_STRING_SIZE];
96 if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING " %d", from_name, to_name, &kx_version) < 2) {
97 logger(LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
102 if(!check_id(from_name) || !check_id(to_name)) {
103 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", c->name, c->hostname, "invalid name");
107 from = lookup_node(from_name);
110 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
111 "REQ_KEY", c->name, c->hostname, from_name);
115 to = lookup_node(to_name);
118 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
119 "REQ_KEY", c->name, c->hostname, to_name);
123 /* Check if this key request is for us */
125 if(to == myself) { /* Yes, send our own key back */
127 logger(LOG_DEBUG, "Got ECDH key request from %s", from->name);
128 from->status.ecdh = true;
135 if(!to->status.reachable) {
136 logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
137 "REQ_KEY", c->name, c->hostname, to_name);
141 send_request(to->nexthop->connection, "%s", request);
147 bool send_ans_key_ecdh(node_t *to) {
148 char key[ECDH_SIZE * 2 + 1];
150 ecdh_generate_public(&to->ecdh, key);
152 bin2hex(key, key, ECDH_SIZE);
153 key[ECDH_SIZE * 2] = '\0';
155 return send_request(to->nexthop->connection, "%d %s %s ECDH:%s %d %d %zu %d", ANS_KEY,
156 myself->name, to->name, key,
157 cipher_get_nid(&myself->incipher),
158 digest_get_nid(&myself->indigest),
159 digest_length(&myself->indigest),
160 myself->incompression);
163 bool send_ans_key(node_t *to) {
165 return send_ans_key_ecdh(to);
167 size_t keylen = cipher_keylength(&myself->incipher);
168 char key[keylen * 2 + 1];
170 cipher_open_by_nid(&to->incipher, cipher_get_nid(&myself->incipher));
171 digest_open_by_nid(&to->indigest, digest_get_nid(&myself->indigest), digest_length(&myself->indigest));
172 to->incompression = myself->incompression;
174 randomize(key, keylen);
175 cipher_set_key(&to->incipher, key, true);
176 digest_set_key(&to->indigest, key, keylen);
178 bin2hex(key, key, keylen);
179 key[keylen * 2] = '\0';
181 // Reset sequence number and late packet window
183 to->received_seqno = 0;
184 if(replaywin) memset(to->late, 0, replaywin);
186 return send_request(to->nexthop->connection, "%d %s %s %s %d %d %zu %d", ANS_KEY,
187 myself->name, to->name, key,
188 cipher_get_nid(&to->incipher),
189 digest_get_nid(&to->indigest),
190 digest_length(&to->indigest),
194 bool ans_key_h(connection_t *c, char *request) {
195 char from_name[MAX_STRING_SIZE];
196 char to_name[MAX_STRING_SIZE];
197 char key[MAX_STRING_SIZE];
198 char address[MAX_STRING_SIZE] = "";
199 char port[MAX_STRING_SIZE] = "";
200 int cipher, digest, maclength, compression, keylen;
203 if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING" %d",
204 from_name, to_name, key, &cipher, &digest, &maclength,
205 &compression, address, port) < 7) {
206 logger(LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
211 if(!check_id(from_name) || !check_id(to_name)) {
212 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
216 from = lookup_node(from_name);
219 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
220 "ANS_KEY", c->name, c->hostname, from_name);
224 to = lookup_node(to_name);
227 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
228 "ANS_KEY", c->name, c->hostname, to_name);
232 /* Forward it if necessary */
238 if(!to->status.reachable) {
239 logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
240 "ANS_KEY", c->name, c->hostname, to_name);
244 if(!*address && from->address.sa.sa_family != AF_UNSPEC) {
245 char *address, *port;
246 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
247 sockaddr2str(&from->address, &address, &port);
248 send_request(to->nexthop->connection, "%s %s %s", request, address, port);
254 return send_request(to->nexthop->connection, "%s", request);
257 /* Check and lookup cipher and digest algorithms */
259 if(!cipher_open_by_nid(&from->outcipher, cipher)) {
260 logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name, from->hostname);
264 if(!digest_open_by_nid(&from->outdigest, digest, maclength)) {
265 logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name, from->hostname);
269 if(maclength != digest_length(&from->outdigest)) {
270 logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!", from->name, from->hostname);
274 if(compression < 0 || compression > 11) {
275 logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
279 from->outcompression = compression;
281 /* ECDH or old-style key exchange? */
283 if(!strncmp(key, "ECDH:", 5)) {
284 keylen = (strlen(key) - 5) / 2;
286 if(keylen != ECDH_SIZE) {
287 logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname);
291 if(ECDH_SHARED_SIZE < cipher_keylength(&from->outcipher)) {
292 logger(LOG_ERR, "ECDH key too short for cipher of %s!", from->name);
297 from->status.ecdh = true;
298 if(!send_ans_key(from))
302 char shared[ECDH_SHARED_SIZE * 2 + 1];
303 char hex[ECDH_SHARED_SIZE * 2 + 1];
304 hex2bin(key + 5, key + 5, keylen);
306 if(!ecdh_compute_shared(&from->ecdh, key + 5, shared))
309 /* Update our crypto end */
311 size_t mykeylen = cipher_keylength(&myself->incipher);
312 size_t hiskeylen = cipher_keylength(&from->outcipher);
318 if(strcmp(myself->name, from->name) < 0) {
320 hiskey = key + mykeylen * 2;
321 xasprintf(&seed, "tinc UDP key expansion %s %s", myself->name, from->name);
323 mykey = key + hiskeylen * 2;
325 xasprintf(&seed, "tinc UDP key expansion %s %s", from->name, myself->name);
328 if(!prf(shared, ECDH_SHARED_SIZE, seed, strlen(seed), key, hiskeylen * 2 + mykeylen * 2))
333 cipher_open_by_nid(&from->incipher, cipher_get_nid(&myself->incipher));
334 digest_open_by_nid(&from->indigest, digest_get_nid(&myself->indigest), digest_length(&myself->indigest));
335 from->incompression = myself->incompression;
337 cipher_set_key(&from->incipher, mykey, true);
338 digest_set_key(&from->indigest, mykey + mykeylen, mykeylen);
340 cipher_set_key(&from->outcipher, hiskey, false);
341 digest_set_key(&from->outdigest, hiskey + hiskeylen, hiskeylen);
343 // Reset sequence number and late packet window
345 from->received_seqno = 0;
347 memset(from->late, 0, replaywin);
349 if(strcmp(myself->name, from->name) < 0)
350 memmove(key, key + mykeylen * 2, hiskeylen * 2);
352 keylen = strlen(key) / 2;
353 hex2bin(key, key, keylen);
355 if(keylen != cipher_keylength(&from->outcipher)) {
356 logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname);
360 /* Update our copy of the origin's packet key */
362 cipher_set_key(&from->outcipher, key, false);
363 digest_set_key(&from->outdigest, key, keylen);
366 from->status.validkey = true;
367 from->sent_seqno = 0;
369 if(*address && *port) {
370 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
371 sockaddr_t sa = str2sockaddr(address, port);
372 update_node_udp(from, &sa);
375 if(from->options & OPTION_PMTU_DISCOVERY)
376 send_mtu_probe(from);