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"
36 static bool mykeyused = false;
38 void send_key_changed(void) {
42 send_request(broadcast, "%d %x %s", KEY_CHANGED, rand(), myself->name);
44 /* Immediately send new keys to directly connected nodes to keep UDP mappings alive */
46 for(node = connection_tree->head; node; node = node->next) {
48 if(c->status.active && c->node && c->node->status.reachable)
49 send_ans_key(c->node);
53 bool key_changed_h(connection_t *c, char *request) {
54 char name[MAX_STRING_SIZE];
57 if(sscanf(request, "%*d %*x " MAX_STRING, name) != 1) {
58 logger(LOG_ERR, "Got bad %s from %s (%s)", "KEY_CHANGED",
59 c->name, c->hostname);
63 if(seen_request(request))
66 n = lookup_node(name);
69 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
70 "KEY_CHANGED", c->name, c->hostname, name);
74 n->status.validkey = false;
80 forward_request(c, request);
85 bool send_req_key(node_t *to) {
86 return send_request(to->nexthop->connection, "%d %s %s 1", REQ_KEY, myself->name, to->name);
89 bool req_key_h(connection_t *c, char *request) {
90 char from_name[MAX_STRING_SIZE];
91 char to_name[MAX_STRING_SIZE];
95 if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING " %d", from_name, to_name, &kx_version) < 2) {
96 logger(LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
101 if(!check_id(from_name) || !check_id(to_name)) {
102 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", c->name, c->hostname, "invalid name");
106 from = lookup_node(from_name);
109 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
110 "REQ_KEY", c->name, c->hostname, from_name);
114 to = lookup_node(to_name);
117 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
118 "REQ_KEY", c->name, c->hostname, to_name);
122 /* Check if this key request is for us */
124 if(to == myself) { /* Yes, send our own key back */
126 logger(LOG_DEBUG, "Got ECDH key request from %s", from->name);
127 from->status.ecdh = true;
134 if(!to->status.reachable) {
135 logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
136 "REQ_KEY", c->name, c->hostname, to_name);
140 send_request(to->nexthop->connection, "%s", request);
146 bool send_ans_key_ecdh(node_t *to) {
147 char key[ECDH_SIZE * 2 + 1];
149 ecdh_generate_public(&to->ecdh, key);
151 bin2hex(key, key, ECDH_SIZE);
152 key[ECDH_SIZE * 2] = '\0';
154 return send_request(to->nexthop->connection, "%d %s %s ECDH:%s %d %d %zu %d", ANS_KEY,
155 myself->name, to->name, key,
156 cipher_get_nid(&myself->incipher),
157 digest_get_nid(&myself->indigest),
158 digest_length(&myself->indigest),
159 myself->incompression);
162 bool send_ans_key(node_t *to) {
164 return send_ans_key_ecdh(to);
166 size_t keylen = cipher_keylength(&myself->incipher);
167 char key[keylen * 2 + 1];
169 cipher_open_by_nid(&to->incipher, cipher_get_nid(&myself->incipher));
170 digest_open_by_nid(&to->indigest, digest_get_nid(&myself->indigest), digest_length(&myself->indigest));
171 to->incompression = myself->incompression;
173 randomize(key, keylen);
174 cipher_set_key(&to->incipher, key, true);
175 digest_set_key(&to->indigest, key, keylen);
177 bin2hex(key, key, keylen);
178 key[keylen * 2] = '\0';
180 // Reset sequence number and late packet window
182 to->received_seqno = 0;
183 if(replaywin) memset(to->late, 0, replaywin);
185 return send_request(to->nexthop->connection, "%d %s %s %s %d %d %zu %d", ANS_KEY,
186 myself->name, to->name, key,
187 cipher_get_nid(&to->incipher),
188 digest_get_nid(&to->indigest),
189 digest_length(&to->indigest),
193 bool ans_key_h(connection_t *c, char *request) {
194 char from_name[MAX_STRING_SIZE];
195 char to_name[MAX_STRING_SIZE];
196 char key[MAX_STRING_SIZE];
197 char address[MAX_STRING_SIZE] = "";
198 char port[MAX_STRING_SIZE] = "";
199 int cipher, digest, maclength, compression, keylen;
202 if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING" %d",
203 from_name, to_name, key, &cipher, &digest, &maclength,
204 &compression, address, port) < 7) {
205 logger(LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
210 if(!check_id(from_name) || !check_id(to_name)) {
211 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
215 from = lookup_node(from_name);
218 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
219 "ANS_KEY", c->name, c->hostname, from_name);
223 to = lookup_node(to_name);
226 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
227 "ANS_KEY", c->name, c->hostname, to_name);
231 /* Forward it if necessary */
237 if(!to->status.reachable) {
238 logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
239 "ANS_KEY", c->name, c->hostname, to_name);
243 if(!*address && from->address.sa.sa_family != AF_UNSPEC) {
244 char *address, *port;
245 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
246 sockaddr2str(&from->address, &address, &port);
247 send_request(to->nexthop->connection, "%s %s %s", request, address, port);
253 return send_request(to->nexthop->connection, "%s", request);
256 /* Check and lookup cipher and digest algorithms */
258 if(!cipher_open_by_nid(&from->outcipher, cipher)) {
259 logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name, from->hostname);
263 if(!digest_open_by_nid(&from->outdigest, digest, maclength)) {
264 logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name, from->hostname);
268 if(maclength != digest_length(&from->outdigest)) {
269 logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!", from->name, from->hostname);
273 if(compression < 0 || compression > 11) {
274 logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
278 from->outcompression = compression;
280 /* ECDH or old-style key exchange? */
281 /* TODO: look at SSH and TLS to see how they derive cipher and HMAC keys from shared secret properly */
283 if(!strncmp(key, "ECDH:", 5)) {
284 logger(LOG_DEBUG, "Got ECDH key from %s", from->name);
286 keylen = (strlen(key) - 5) / 2;
288 if(keylen != ECDH_SIZE) {
289 logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname);
293 if(ECDH_SHARED_SIZE < cipher_keylength(&from->outcipher)) {
294 logger(LOG_ERR, "ECDH key too short for cipher of %s!", from->name);
299 logger(LOG_DEBUG, "Woops, we didn't generate our public key yet");
300 from->status.ecdh = true;
301 if(!send_ans_key(from))
305 char shared[ECDH_SHARED_SIZE * 2 + 1];
306 char hex[ECDH_SHARED_SIZE * 2 + 1];
307 hex2bin(key + 5, key + 5, keylen);
309 if(!ecdh_compute_shared(&from->ecdh, key + 5, shared))
312 /* Update our crypto end */
315 size_t mykeylen = cipher_keylength(&myself->incipher);
316 keylen = cipher_keylength(&from->outcipher);
318 if(ECDH_SHARED_SIZE < mykeylen) {
319 logger(LOG_ERR, "ECDH key too short for cipher of MYSELF!");
323 if(strcmp(myself->name, from->name) < 0) {
324 logger(LOG_DEBUG, "Using left half of shared secret");
326 memcpy(key, shared + ECDH_SHARED_SIZE - keylen, keylen);
328 logger(LOG_DEBUG, "Using right half of shared secret");
329 mykey = shared + ECDH_SHARED_SIZE - mykeylen;
330 memcpy(key, shared, keylen);
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);
340 // Reset sequence number and late packet window
342 from->received_seqno = 0;
344 memset(from->late, 0, replaywin);
346 bin2hex(shared, hex, ECDH_SHARED_SIZE);
347 hex[ECDH_SHARED_SIZE * 2] = 0;
348 logger(LOG_DEBUG, "Shared secret was %s", hex);
350 bin2hex(mykey, hex, mykeylen);
351 hex[mykeylen * 2] = 0;
352 logger(LOG_DEBUG, "My part is: %s (%d)", hex, mykeylen);
354 bin2hex(key, hex, keylen);
356 logger(LOG_DEBUG, "His part is: %s (%d)", hex, keylen);
358 keylen = strlen(key) / 2;
359 hex2bin(key, key, keylen);
362 /* Update our copy of the origin's packet key */
364 if(keylen != cipher_keylength(&from->outcipher)) {
365 logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname);
369 cipher_set_key(&from->outcipher, key, false);
370 digest_set_key(&from->outdigest, key, keylen);
372 from->status.validkey = true;
373 from->sent_seqno = 0;
375 if(*address && *port) {
376 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
377 sockaddr_t sa = str2sockaddr(address, port);
378 update_node_udp(from, &sa);
381 if(from->options & OPTION_PMTU_DISCOVERY)
382 send_mtu_probe(from);