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