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