Merge branch 'master' of git://tinc-vpn.org/tinc into 1.1
[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-2011 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 "ecdh.h"
28 #include "logger.h"
29 #include "net.h"
30 #include "netutl.h"
31 #include "node.h"
32 #include "prf.h"
33 #include "protocol.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                         send_ans_key(c->node);
51         }
52 }
53
54 bool key_changed_h(connection_t *c, char *request) {
55         char name[MAX_STRING_SIZE];
56         node_t *n;
57
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);
61                 return false;
62         }
63
64         if(seen_request(request))
65                 return true;
66
67         n = lookup_node(name);
68
69         if(!n) {
70                 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
71                            "KEY_CHANGED", c->name, c->hostname, name);
72                 return true;
73         }
74
75         n->status.validkey = false;
76         n->last_req_key = 0;
77
78         /* Tell the others */
79
80         if(!tunnelserver)
81                 forward_request(c, request);
82
83         return true;
84 }
85
86 bool send_req_key(node_t *to) {
87         return send_request(to->nexthop->connection, "%d %s %s %d", REQ_KEY, myself->name, to->name, experimental ? 1 : 0);
88 }
89
90 bool req_key_h(connection_t *c, char *request) {
91         char from_name[MAX_STRING_SIZE];
92         char to_name[MAX_STRING_SIZE];
93         node_t *from, *to;
94         int kx_version = 0;
95
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,
98                            c->hostname);
99                 return false;
100         }
101
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");
104                 return false;
105         }
106
107         from = lookup_node(from_name);
108
109         if(!from) {
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);
112                 return true;
113         }
114
115         to = lookup_node(to_name);
116
117         if(!to) {
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);
120                 return true;
121         }
122
123         /* Check if this key request is for us */
124
125         if(to == myself) {                      /* Yes, send our own key back */
126                 if(experimental && kx_version >= 1) {
127                         logger(LOG_DEBUG, "Got ECDH key request from %s", from->name);
128                         from->status.ecdh = true;
129                 }
130                 send_ans_key(from);
131         } else {
132                 if(tunnelserver)
133                         return true;
134
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);
138                         return true;
139                 }
140
141                 send_request(to->nexthop->connection, "%s", request);
142         }
143
144         return true;
145 }
146
147 bool send_ans_key_ecdh(node_t *to) {
148         int siglen = ecdsa_size(&myself->connection->ecdsa);
149         char key[(ECDH_SIZE + siglen) * 2 + 1];
150
151         if(!ecdh_generate_public(&to->ecdh, key))
152                 return false;
153
154         if(!ecdsa_sign(&myself->connection->ecdsa, key, ECDH_SIZE, key + ECDH_SIZE))
155                 return false;
156
157         b64encode(key, key, ECDH_SIZE + siglen);
158
159         char *pubkey = ecdsa_get_base64_public_key(&myself->connection->ecdsa);
160
161         if(!pubkey)
162                 return false;
163
164         int result = send_request(to->nexthop->connection, "%d %s %s ECDH:%s:%s %d %d %zu %d", ANS_KEY,
165                                                 myself->name, to->name, key, pubkey,
166                                                 cipher_get_nid(&myself->incipher),
167                                                 digest_get_nid(&myself->indigest),
168                                                 digest_length(&myself->indigest),
169                                                 myself->incompression);
170
171         free(pubkey);
172         return result;
173 }
174
175 bool send_ans_key(node_t *to) {
176         if(experimental && to->status.ecdh)
177                 return send_ans_key_ecdh(to);
178
179         size_t keylen = cipher_keylength(&myself->incipher);
180         char key[keylen * 2 + 1];
181
182         cipher_open_by_nid(&to->incipher, cipher_get_nid(&myself->incipher));
183         digest_open_by_nid(&to->indigest, digest_get_nid(&myself->indigest), digest_length(&myself->indigest));
184         to->incompression = myself->incompression;
185
186         randomize(key, keylen);
187         cipher_set_key(&to->incipher, key, false);
188         digest_set_key(&to->indigest, key, keylen);
189
190         bin2hex(key, key, keylen);
191
192         // Reset sequence number and late packet window
193         mykeyused = true;
194         to->received_seqno = 0;
195         if(replaywin) memset(to->late, 0, replaywin);
196
197         return send_request(to->nexthop->connection, "%d %s %s %s %d %d %zu %d", ANS_KEY,
198                                                 myself->name, to->name, key,
199                                                 cipher_get_nid(&to->incipher),
200                                                 digest_get_nid(&to->indigest),
201                                                 digest_length(&to->indigest),
202                                                 to->incompression);
203 }
204
205 bool ans_key_h(connection_t *c, char *request) {
206         char from_name[MAX_STRING_SIZE];
207         char to_name[MAX_STRING_SIZE];
208         char key[MAX_STRING_SIZE];
209         char address[MAX_STRING_SIZE] = "";
210         char port[MAX_STRING_SIZE] = "";
211         int cipher, digest, maclength, compression, keylen;
212         node_t *from, *to;
213
214         if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
215                 from_name, to_name, key, &cipher, &digest, &maclength,
216                 &compression, address, port) < 7) {
217                 logger(LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
218                            c->hostname);
219                 return false;
220         }
221
222         if(!check_id(from_name) || !check_id(to_name)) {
223                 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
224                 return false;
225         }
226
227         from = lookup_node(from_name);
228
229         if(!from) {
230                 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
231                            "ANS_KEY", c->name, c->hostname, from_name);
232                 return true;
233         }
234
235         to = lookup_node(to_name);
236
237         if(!to) {
238                 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
239                            "ANS_KEY", c->name, c->hostname, to_name);
240                 return true;
241         }
242
243         /* Forward it if necessary */
244
245         if(to != myself) {
246                 if(tunnelserver)
247                         return true;
248
249                 if(!to->status.reachable) {
250                         logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
251                                    "ANS_KEY", c->name, c->hostname, to_name);
252                         return true;
253                 }
254
255                 if(!*address && from->address.sa.sa_family != AF_UNSPEC) {
256                         char *address, *port;
257                         ifdebug(PROTOCOL) logger(LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
258                         sockaddr2str(&from->address, &address, &port);
259                         send_request(to->nexthop->connection, "%s %s %s", request, address, port);
260                         free(address);
261                         free(port);
262                         return true;
263                 }
264
265                 return send_request(to->nexthop->connection, "%s", request);
266         }
267
268         /* Check and lookup cipher and digest algorithms */
269
270         if(!cipher_open_by_nid(&from->outcipher, cipher)) {
271                 logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name, from->hostname);
272                 return false;
273         }
274
275         if(!digest_open_by_nid(&from->outdigest, digest, maclength)) {
276                 logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name, from->hostname);
277                 return false;
278         }
279
280         if(maclength != digest_length(&from->outdigest)) {
281                 logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!", from->name, from->hostname);
282                 return false;
283         }
284
285         if(compression < 0 || compression > 11) {
286                 logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
287                 return true;
288         }
289
290         from->outcompression = compression;
291
292         /* ECDH or old-style key exchange? */
293         
294         if(experimental && !strncmp(key, "ECDH:", 5)) {
295                 char *pubkey = strchr(key + 5, ':');
296                 if(pubkey)
297                         *pubkey++ = 0;
298                         
299                 /* Check if we already have an ECDSA public key for this node.
300                  * If not, use the one from the key exchange, and store it. */
301
302                 if(!node_read_ecdsa_public_key(from)) {
303                         if(!pubkey) {
304                                 logger(LOG_ERR, "No ECDSA public key known for %s (%s), cannot verify ECDH key exchange!", from->name, from->hostname);
305                                 return true;
306                         }
307
308                         if(!ecdsa_set_base64_public_key(&from->ecdsa, pubkey))
309                                 return true;
310
311                         append_config_file(from->name, "ECDSAPublicKey", pubkey);
312                 }
313
314                 int siglen = ecdsa_size(&from->ecdsa);
315                 int keylen = b64decode(key + 5, key + 5, sizeof key - 5);
316
317                 if(keylen != ECDH_SIZE + siglen) {
318                         logger(LOG_ERR, "Node %s (%s) uses wrong keylength! %d != %d", from->name, from->hostname, keylen, ECDH_SIZE + siglen);
319                         return true;
320                 }
321
322                 if(ECDH_SHARED_SIZE < cipher_keylength(&from->outcipher)) {
323                         logger(LOG_ERR, "ECDH key too short for cipher of %s!", from->name);
324                         return true;
325                 }
326
327                 if(!ecdsa_verify(&from->ecdsa, key + 5, ECDH_SIZE, key + 5 + ECDH_SIZE)) {
328                         logger(LOG_ERR, "Possible intruder %s (%s): %s", from->name, from->hostname, "invalid ECDSA signature");
329                         return true;
330                 }
331
332                 if(!from->ecdh) {
333                         from->status.ecdh = true;
334                         if(!send_ans_key(from))
335                                 return true;
336                 }
337
338                 char shared[ECDH_SHARED_SIZE * 2 + 1];
339
340                 if(!ecdh_compute_shared(&from->ecdh, key + 5, shared))
341                         return true;
342
343                 /* Update our crypto end */
344
345                 size_t mykeylen = cipher_keylength(&myself->incipher);
346                 size_t hiskeylen = cipher_keylength(&from->outcipher);
347
348                 char *mykey;
349                 char *hiskey;
350                 char *seed;
351                 
352                 if(strcmp(myself->name, from->name) < 0) {
353                         mykey = key;
354                         hiskey = key + mykeylen * 2;
355                         xasprintf(&seed, "tinc UDP key expansion %s %s", myself->name, from->name);
356                 } else {
357                         mykey = key + hiskeylen * 2;
358                         hiskey = key;
359                         xasprintf(&seed, "tinc UDP key expansion %s %s", from->name, myself->name);
360                 }
361
362                 if(!prf(shared, ECDH_SHARED_SIZE, seed, strlen(seed), key, hiskeylen * 2 + mykeylen * 2))
363                         return true;
364
365                 free(seed);
366
367                 cipher_open_by_nid(&from->incipher, cipher_get_nid(&myself->incipher));
368                 digest_open_by_nid(&from->indigest, digest_get_nid(&myself->indigest), digest_length(&myself->indigest));
369                 from->incompression = myself->incompression;
370
371                 cipher_set_key(&from->incipher, mykey, false);
372                 digest_set_key(&from->indigest, mykey + mykeylen, mykeylen);
373
374                 cipher_set_key(&from->outcipher, hiskey, true);
375                 digest_set_key(&from->outdigest, hiskey + hiskeylen, hiskeylen);
376
377                 // Reset sequence number and late packet window
378                 mykeyused = true;
379                 from->received_seqno = 0;
380                 if(replaywin)
381                         memset(from->late, 0, replaywin);
382
383                 if(strcmp(myself->name, from->name) < 0)
384                         memmove(key, key + mykeylen * 2, hiskeylen * 2);
385         } else {
386                 keylen = hex2bin(key, key, sizeof key);
387
388                 if(keylen != cipher_keylength(&from->outcipher)) {
389                         logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname);
390                         return true;
391                 }
392
393                 /* Update our copy of the origin's packet key */
394
395                 cipher_set_key(&from->outcipher, key, true);
396                 digest_set_key(&from->outdigest, key, keylen);
397         }
398
399         from->status.validkey = true;
400         from->sent_seqno = 0;
401
402         if(*address && *port) {
403                 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
404                 sockaddr_t sa = str2sockaddr(address, port);
405                 update_node_udp(from, &sa);
406         }
407
408         if(from->options & OPTION_PMTU_DISCOVERY)
409                 send_mtu_probe(from);
410
411         return true;
412 }