Replace the connection_tree with a connection_list.
[tinc] / src / protocol_key.c
1 /*
2     protocol_key.c -- handle the meta-protocol, key exchange
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2012 Guus Sliepen <guus@tinc-vpn.org>
5
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.
10
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.
15
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.
19 */
20
21 #include "system.h"
22
23 #include "cipher.h"
24 #include "connection.h"
25 #include "crypto.h"
26 #include "logger.h"
27 #include "net.h"
28 #include "netutl.h"
29 #include "node.h"
30 #include "prf.h"
31 #include "protocol.h"
32 #include "sptps.h"
33 #include "utils.h"
34 #include "xalloc.h"
35
36 static bool mykeyused = false;
37
38 void send_key_changed(void) {
39         send_request(everyone, "%d %x %s", KEY_CHANGED, rand(), myself->name);
40
41         /* Immediately send new keys to directly connected nodes to keep UDP mappings alive */
42
43         for(list_node_t *node = connection_list->head, *next; node; node = next) {
44                 next = node->next;
45                 connection_t *c = node->data;
46                 if(c->status.active && c->node && c->node->status.reachable) {
47                         if(!c->node->status.sptps)
48                                 send_ans_key(c->node);
49                 }
50         }
51
52         /* Force key exchange for connections using SPTPS */
53
54         if(experimental) {
55                 for(splay_node_t *node = node_tree->head; node; node = node->next) {
56                         node_t *n = node->data;
57                         if(n->status.reachable && n->status.validkey && n->status.sptps)
58                                 sptps_force_kex(&n->sptps);
59                 }
60         }
61 }
62
63 bool key_changed_h(connection_t *c, const char *request) {
64         char name[MAX_STRING_SIZE];
65         node_t *n;
66
67         if(sscanf(request, "%*d %*x " MAX_STRING, name) != 1) {
68                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "KEY_CHANGED",
69                            c->name, c->hostname);
70                 return false;
71         }
72
73         if(seen_request(request))
74                 return true;
75
76         n = lookup_node(name);
77
78         if(!n) {
79                 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
80                            "KEY_CHANGED", c->name, c->hostname, name);
81                 return true;
82         }
83
84         if(!n->status.sptps) {
85                 n->status.validkey = false;
86                 n->last_req_key = 0;
87         }
88
89         /* Tell the others */
90
91         if(!tunnelserver)
92                 forward_request(c, request);
93
94         return true;
95 }
96
97 static bool send_initial_sptps_data(void *handle, uint8_t type, const char *data, size_t len) {
98         node_t *to = handle;
99         to->sptps.send_data = send_sptps_data;
100         char buf[len * 4 / 3 + 5];
101         b64encode(data, buf, len);
102         return send_request(to->nexthop->connection, "%d %s %s %d %s", REQ_KEY, myself->name, to->name, REQ_KEY, buf);
103 }
104
105 bool send_req_key(node_t *to) {
106         if(to->status.sptps) {
107                 if(!node_read_ecdsa_public_key(to)) {
108                         logger(DEBUG_PROTOCOL, LOG_DEBUG, "No ECDSA key known for %s (%s)", to->name, to->hostname);
109                         send_request(to->nexthop->connection, "%d %s %s %d", REQ_KEY, myself->name, to->name, REQ_PUBKEY);
110                         return true;
111                 }
112                 char label[25 + strlen(myself->name) + strlen(to->name)];
113                 snprintf(label, sizeof label, "tinc UDP key expansion %s %s", myself->name, to->name);
114                 sptps_stop(&to->sptps);
115                 to->status.validkey = false;
116                 to->status.waitingforkey = true;
117                 to->last_req_key = time(NULL);
118                 to->incompression = myself->incompression;
119                 return sptps_start(&to->sptps, to, true, true, myself->connection->ecdsa, to->ecdsa, label, sizeof label, send_initial_sptps_data, receive_sptps_record); 
120         }
121
122         return send_request(to->nexthop->connection, "%d %s %s", REQ_KEY, myself->name, to->name);
123 }
124
125 /* REQ_KEY is overloaded to allow arbitrary requests to be routed between two nodes. */
126
127 static bool req_key_ext_h(connection_t *c, const char *request, node_t *from, int reqno) {
128         switch(reqno) {
129                 case REQ_PUBKEY: {
130                         char *pubkey = ecdsa_get_base64_public_key(&myself->connection->ecdsa);
131                         send_request(from->nexthop->connection, "%d %s %s %d %s", REQ_KEY, myself->name, from->name, ANS_PUBKEY, pubkey);
132                         free(pubkey);
133                         return true;
134                 }
135
136                 case ANS_PUBKEY: {
137                         if(node_read_ecdsa_public_key(from)) {
138                                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got ANS_PUBKEY from %s (%s) even though we already have his pubkey", from->name, from->hostname);
139                                 return true;
140                         }
141
142                         char pubkey[MAX_STRING_SIZE];
143                         if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, pubkey) != 1 || !ecdsa_set_base64_public_key(&from->ecdsa, pubkey)) {
144                                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_PUBKEY", from->name, from->hostname, "invalid pubkey");
145                                 return true;
146                         }
147
148                         logger(DEBUG_PROTOCOL, LOG_INFO, "Learned ECDSA public key from %s (%s)", from->name, from->hostname);
149                         append_config_file(from->name, "ECDSAPublicKey", pubkey);
150                         return true;
151                 }
152
153                 case REQ_KEY: {
154                         if(!node_read_ecdsa_public_key(from)) {
155                                 logger(DEBUG_PROTOCOL, LOG_DEBUG, "No ECDSA key known for %s (%s)", from->name, from->hostname);
156                                 send_request(from->nexthop->connection, "%d %s %s %d", REQ_KEY, myself->name, from->name, REQ_PUBKEY);
157                                 return true;
158                         }
159                 }
160
161                 case REQ_SPTPS: {
162                         char buf[MAX_STRING_SIZE];
163                         if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1) {
164                                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", from->name, from->hostname, "invalid SPTPS data");
165                                 return true;
166                         }
167                         int len = b64decode(buf, buf, strlen(buf));
168
169
170                         char label[25 + strlen(from->name) + strlen(myself->name)];
171                         snprintf(label, sizeof label, "tinc UDP key expansion %s %s", from->name, myself->name);
172                         sptps_stop(&from->sptps);
173                         from->status.validkey = false;
174                         from->status.waitingforkey = true;
175                         from->last_req_key = time(NULL);
176                         sptps_start(&from->sptps, from, false, true, myself->connection->ecdsa, from->ecdsa, label, sizeof label, send_sptps_data, receive_sptps_record); 
177                         sptps_receive_data(&from->sptps, buf, len);
178                         return true;
179                 }
180
181                 case REQ_PACKET: {
182                         if(!from->status.validkey) {
183                                 logger(DEBUG_PROTOCOL, LOG_ERR, "Got REQ_PACKET from %s (%s) but we don't have a valid key yet", from->name, from->hostname);
184                                 return true;
185                         }
186
187                         char buf[MAX_STRING_SIZE];
188                         if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1) {
189                                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", from->name, from->hostname, "invalid SPTPS data");
190                                 return true;
191                         }
192                         int len = b64decode(buf, buf, strlen(buf));
193                         sptps_receive_data(&from->sptps, buf, len);
194                         return true;
195                 }
196
197                 default:
198                         logger(DEBUG_ALWAYS, LOG_ERR, "Unknown extended REQ_KEY request from %s (%s): %s", from->name, from->hostname, request);
199                         return true;
200         }
201 }
202
203 bool req_key_h(connection_t *c, const char *request) {
204         char from_name[MAX_STRING_SIZE];
205         char to_name[MAX_STRING_SIZE];
206         node_t *from, *to;
207         int reqno = 0;
208
209         if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING " %d", from_name, to_name, &reqno) < 2) {
210                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
211                            c->hostname);
212                 return false;
213         }
214
215         if(!check_id(from_name) || !check_id(to_name)) {
216                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", c->name, c->hostname, "invalid name");
217                 return false;
218         }
219
220         from = lookup_node(from_name);
221
222         if(!from) {
223                 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
224                            "REQ_KEY", c->name, c->hostname, from_name);
225                 return true;
226         }
227
228         to = lookup_node(to_name);
229
230         if(!to) {
231                 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
232                            "REQ_KEY", c->name, c->hostname, to_name);
233                 return true;
234         }
235
236         /* Check if this key request is for us */
237
238         if(to == myself) {                      /* Yes */
239                 /* Is this an extended REQ_KEY message? */
240                 if(experimental && reqno)
241                         return req_key_ext_h(c, request, from, reqno);
242
243                 /* No, just send our key back */
244                 send_ans_key(from);
245         } else {
246                 if(tunnelserver)
247                         return true;
248
249                 if(!to->status.reachable) {
250                         logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
251                                 "REQ_KEY", c->name, c->hostname, to_name);
252                         return true;
253                 }
254
255                 send_request(to->nexthop->connection, "%s", request);
256         }
257
258         return true;
259 }
260
261 bool send_ans_key(node_t *to) {
262         if(to->status.sptps)
263                 abort();
264
265         size_t keylen = cipher_keylength(&myself->incipher);
266         char key[keylen * 2 + 1];
267
268         cipher_open_by_nid(&to->incipher, cipher_get_nid(&myself->incipher));
269         digest_open_by_nid(&to->indigest, digest_get_nid(&myself->indigest), digest_length(&myself->indigest));
270         to->incompression = myself->incompression;
271
272         randomize(key, keylen);
273         cipher_set_key(&to->incipher, key, false);
274         digest_set_key(&to->indigest, key, keylen);
275
276         bin2hex(key, key, keylen);
277
278         // Reset sequence number and late packet window
279         mykeyused = true;
280         to->received_seqno = 0;
281         if(replaywin) memset(to->late, 0, replaywin);
282
283         return send_request(to->nexthop->connection, "%d %s %s %s %d %d %d %d", ANS_KEY,
284                                                 myself->name, to->name, key,
285                                                 cipher_get_nid(&to->incipher),
286                                                 digest_get_nid(&to->indigest),
287                                                 (int)digest_length(&to->indigest),
288                                                 to->incompression);
289 }
290
291 bool ans_key_h(connection_t *c, const char *request) {
292         char from_name[MAX_STRING_SIZE];
293         char to_name[MAX_STRING_SIZE];
294         char key[MAX_STRING_SIZE];
295         char address[MAX_STRING_SIZE] = "";
296         char port[MAX_STRING_SIZE] = "";
297         int cipher, digest, maclength, compression, keylen;
298         node_t *from, *to;
299
300         if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
301                 from_name, to_name, key, &cipher, &digest, &maclength,
302                 &compression, address, port) < 7) {
303                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
304                            c->hostname);
305                 return false;
306         }
307
308         if(!check_id(from_name) || !check_id(to_name)) {
309                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
310                 return false;
311         }
312
313         from = lookup_node(from_name);
314
315         if(!from) {
316                 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
317                            "ANS_KEY", c->name, c->hostname, from_name);
318                 return true;
319         }
320
321         to = lookup_node(to_name);
322
323         if(!to) {
324                 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
325                            "ANS_KEY", c->name, c->hostname, to_name);
326                 return true;
327         }
328
329         /* Forward it if necessary */
330
331         if(to != myself) {
332                 if(tunnelserver)
333                         return true;
334
335                 if(!to->status.reachable) {
336                         logger(DEBUG_ALWAYS, LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
337                                    "ANS_KEY", c->name, c->hostname, to_name);
338                         return true;
339                 }
340
341                 if(!*address && from->address.sa.sa_family != AF_UNSPEC) {
342                         char *address, *port;
343                         logger(DEBUG_PROTOCOL, LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
344                         sockaddr2str(&from->address, &address, &port);
345                         send_request(to->nexthop->connection, "%s %s %s", request, address, port);
346                         free(address);
347                         free(port);
348                         return true;
349                 }
350
351                 return send_request(to->nexthop->connection, "%s", request);
352         }
353
354         /* Don't use key material until every check has passed. */
355         from->status.validkey = false;
356
357         if(compression < 0 || compression > 11) {
358                 logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
359                 return true;
360         }
361
362         from->outcompression = compression;
363
364         /* SPTPS or old-style key exchange? */
365
366         if(from->status.sptps) {
367                 char buf[strlen(key)];
368                 int len = b64decode(key, buf, strlen(key));
369
370                 if(!sptps_receive_data(&from->sptps, buf, len))
371                         logger(DEBUG_ALWAYS, LOG_ERR, "Error processing SPTPS data from %s (%s)", from->name, from->hostname);
372
373                 if(from->status.validkey) {
374                         if(*address && *port) {
375                                 logger(DEBUG_PROTOCOL, LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
376                                 sockaddr_t sa = str2sockaddr(address, port);
377                                 update_node_udp(from, &sa);
378                         }
379
380                         if(from->options & OPTION_PMTU_DISCOVERY)
381                                 send_mtu_probe(from);
382                 }
383
384                 return true;
385         }
386
387         /* Check and lookup cipher and digest algorithms */
388
389         if(!cipher_open_by_nid(&from->outcipher, cipher)) {
390                 logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name, from->hostname);
391                 return false;
392         }
393
394         if(!digest_open_by_nid(&from->outdigest, digest, maclength)) {
395                 logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses unknown digest!", from->name, from->hostname);
396                 return false;
397         }
398
399         if(maclength != digest_length(&from->outdigest)) {
400                 logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses bogus MAC length!", from->name, from->hostname);
401                 return false;
402         }
403
404         /* Process key */
405
406         keylen = hex2bin(key, key, sizeof key);
407
408         if(keylen != cipher_keylength(&from->outcipher)) {
409                 logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname);
410                 return true;
411         }
412
413         /* Update our copy of the origin's packet key */
414
415         cipher_set_key(&from->outcipher, key, true);
416         digest_set_key(&from->outdigest, key, keylen);
417
418         from->status.validkey = true;
419         from->sent_seqno = 0;
420
421         if(*address && *port) {
422                 logger(DEBUG_PROTOCOL, LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
423                 sockaddr_t sa = str2sockaddr(address, port);
424                 update_node_udp(from, &sa);
425         }
426
427         if(from->options & OPTION_PMTU_DISCOVERY)
428                 send_mtu_probe(from);
429
430         return true;
431 }