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