341d0ac9f86b03a11b10815d7e7b8c22fb5e2851
[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-2010 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 <openssl/evp.h>
24 #include <openssl/err.h>
25 #include <openssl/rand.h>
26
27 #include "avl_tree.h"
28 #include "connection.h"
29 #include "logger.h"
30 #include "net.h"
31 #include "netutl.h"
32 #include "node.h"
33 #include "protocol.h"
34 #include "utils.h"
35 #include "xalloc.h"
36
37 bool mykeyused = false;
38
39 bool send_key_changed() {
40         /* Only send this message if some other daemon requested our key previously.
41            This reduces unnecessary key_changed broadcasts.
42          */
43
44         if(!mykeyused)
45                 return true;
46
47         return send_request(broadcast, "%d %x %s", KEY_CHANGED, rand(), myself->name);
48 }
49
50 bool key_changed_h(connection_t *c) {
51         char name[MAX_STRING_SIZE];
52         node_t *n;
53
54         if(sscanf(c->buffer, "%*d %*x " MAX_STRING, name) != 1) {
55                 logger(LOG_ERR, "Got bad %s from %s (%s)", "KEY_CHANGED",
56                            c->name, c->hostname);
57                 return false;
58         }
59
60         if(!check_id(name)) {
61                 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "KEY_CHANGED", c->name, c->hostname, "invalid name");
62                 return false;
63         }
64
65         if(seen_request(c->buffer))
66                 return true;
67
68         n = lookup_node(name);
69
70         if(!n) {
71                 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
72                            "KEY_CHANGED", c->name, c->hostname, name);
73                 return true;
74         }
75
76         n->status.validkey = false;
77         n->last_req_key = 0;
78
79         /* Tell the others */
80
81         if(!tunnelserver)
82                 forward_request(c);
83
84         return true;
85 }
86
87 bool send_req_key(node_t *to) {
88         return send_request(to->nexthop->connection, "%d %s %s", REQ_KEY, myself->name, to->name);
89 }
90
91 bool req_key_h(connection_t *c) {
92         char from_name[MAX_STRING_SIZE];
93         char to_name[MAX_STRING_SIZE];
94         node_t *from, *to;
95
96         if(sscanf(c->buffer, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 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                 send_ans_key(from);
127         } else {
128                 if(tunnelserver)
129                         return true;
130
131                 if(!to->status.reachable) {
132                         logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
133                                 "REQ_KEY", c->name, c->hostname, to_name);
134                         return true;
135                 }
136
137                 send_request(to->nexthop->connection, "%s", c->buffer);
138         }
139
140         return true;
141 }
142
143 bool send_ans_key(node_t *to) {
144         char *key;
145
146         // Set key parameters
147         to->incipher = myself->incipher;
148         to->inkeylength = myself->inkeylength;
149         to->indigest = myself->indigest;
150         to->inmaclength = myself->inmaclength;
151         to->incompression = myself->incompression;
152
153         // Allocate memory for key
154         to->inkey = xrealloc(to->inkey, to->inkeylength);
155
156         // Create a new key
157         RAND_pseudo_bytes((unsigned char *)to->inkey, to->inkeylength);
158         if(to->incipher)
159                 EVP_DecryptInit_ex(&to->inctx, to->incipher, NULL, (unsigned char *)to->inkey, (unsigned char *)to->inkey + to->incipher->key_len);
160
161         // Reset sequence number and late packet window
162         mykeyused = true;
163         to->received_seqno = 0;
164         memset(to->late, 0, sizeof(to->late));
165
166         // Convert to hexadecimal and send
167         key = alloca(2 * to->inkeylength + 1);
168         bin2hex(to->inkey, key, to->inkeylength);
169         key[to->inkeylength * 2] = '\0';
170
171         return send_request(to->nexthop->connection, "%d %s %s %s %d %d %d %d", ANS_KEY,
172                         myself->name, to->name, key,
173                         to->incipher ? to->incipher->nid : 0,
174                         to->indigest ? to->indigest->type : 0, to->inmaclength,
175                         to->incompression);
176 }
177
178 bool ans_key_h(connection_t *c) {
179         char from_name[MAX_STRING_SIZE];
180         char to_name[MAX_STRING_SIZE];
181         char key[MAX_STRING_SIZE];
182         char address[MAX_STRING_SIZE] = "";
183         char port[MAX_STRING_SIZE] = "";
184         int cipher, digest, maclength, compression;
185         node_t *from, *to;
186
187         if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
188                 from_name, to_name, key, &cipher, &digest, &maclength,
189                 &compression, address, port) < 7) {
190                 logger(LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
191                            c->hostname);
192                 return false;
193         }
194
195         if(!check_id(from_name) || !check_id(to_name)) {
196                 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
197                 return false;
198         }
199
200         from = lookup_node(from_name);
201
202         if(!from) {
203                 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
204                            "ANS_KEY", c->name, c->hostname, from_name);
205                 return true;
206         }
207
208         to = lookup_node(to_name);
209
210         if(!to) {
211                 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
212                            "ANS_KEY", c->name, c->hostname, to_name);
213                 return true;
214         }
215
216         /* Forward it if necessary */
217
218         if(to != myself) {
219                 if(tunnelserver)
220                         return true;
221
222                 if(!to->status.reachable) {
223                         logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
224                                 "ANS_KEY", c->name, c->hostname, to_name);
225                         return true;
226                 }
227
228                 if(!*address) {
229                         char *address, *port;
230                         ifdebug(PROTOCOL) logger(LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
231                         sockaddr2str(&from->address, &address, &port);
232                         send_request(to->nexthop->connection, "%s %s %s", c->buffer, address, port);
233                         free(address);
234                         free(port);
235                         return true;
236                 }
237
238                 return send_request(to->nexthop->connection, "%s", c->buffer);
239         }
240
241         /* Update our copy of the origin's packet key */
242         from->outkey = xrealloc(from->outkey, strlen(key) / 2);
243
244         from->outkey = xstrdup(key);
245         from->outkeylength = strlen(key) / 2;
246         hex2bin(key, from->outkey, from->outkeylength);
247
248         /* Check and lookup cipher and digest algorithms */
249
250         if(cipher) {
251                 from->outcipher = EVP_get_cipherbynid(cipher);
252
253                 if(!from->outcipher) {
254                         logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name,
255                                    from->hostname);
256                         return true;
257                 }
258
259                 if(from->outkeylength != from->outcipher->key_len + from->outcipher->iv_len) {
260                         logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name,
261                                    from->hostname);
262                         return true;
263                 }
264         } else {
265                 from->outcipher = NULL;
266         }
267
268         from->outmaclength = maclength;
269
270         if(digest) {
271                 from->outdigest = EVP_get_digestbynid(digest);
272
273                 if(!from->outdigest) {
274                         logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name,
275                                    from->hostname);
276                         return true;
277                 }
278
279                 if(from->outmaclength > from->outdigest->md_size || from->outmaclength < 0) {
280                         logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!",
281                                    from->name, from->hostname);
282                         return true;
283                 }
284         } else {
285                 from->outdigest = NULL;
286         }
287
288         if(compression < 0 || compression > 11) {
289                 logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
290                 return true;
291         }
292         
293         from->outcompression = compression;
294
295         if(from->outcipher)
296                 if(!EVP_EncryptInit_ex(&from->outctx, from->outcipher, NULL, (unsigned char *)from->outkey, (unsigned char *)from->outkey + from->outcipher->key_len)) {
297                         logger(LOG_ERR, "Error during initialisation of key from %s (%s): %s",
298                                         from->name, from->hostname, ERR_error_string(ERR_get_error(), NULL));
299                         return true;
300                 }
301
302         from->status.validkey = true;
303         from->sent_seqno = 0;
304
305         if(*address && *port) {
306                 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
307                 sockaddr_t sa = str2sockaddr(address, port);
308                 update_node_udp(from, &sa);
309         }
310
311         if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuprobes)
312                 send_mtu_probe(from);
313
314         return true;
315 }