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