22692bb614e97632d2224bedbd77b1fb28647976
[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 void send_key_changed() {
40         avl_node_t *node;
41         connection_t *c;
42
43         send_request(broadcast, "%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) {
55         char name[MAX_STRING_SIZE];
56         node_t *n;
57
58         if(sscanf(c->buffer, "%*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(!check_id(name)) {
65                 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "KEY_CHANGED", c->name, c->hostname, "invalid name");
66                 return false;
67         }
68
69         if(seen_request(c->buffer))
70                 return true;
71
72         n = lookup_node(name);
73
74         if(!n) {
75                 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
76                            "KEY_CHANGED", c->name, c->hostname, name);
77                 return true;
78         }
79
80         n->status.validkey = false;
81         n->last_req_key = 0;
82
83         /* Tell the others */
84
85         if(!tunnelserver)
86                 forward_request(c);
87
88         return true;
89 }
90
91 bool send_req_key(node_t *to) {
92         return send_request(to->nexthop->connection, "%d %s %s", REQ_KEY, myself->name, to->name);
93 }
94
95 bool req_key_h(connection_t *c) {
96         char from_name[MAX_STRING_SIZE];
97         char to_name[MAX_STRING_SIZE];
98         node_t *from, *to;
99
100         if(sscanf(c->buffer, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) {
101                 logger(LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
102                            c->hostname);
103                 return false;
104         }
105
106         if(!check_id(from_name) || !check_id(to_name)) {
107                 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", c->name, c->hostname, "invalid name");
108                 return false;
109         }
110
111         from = lookup_node(from_name);
112
113         if(!from) {
114                 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
115                            "REQ_KEY", c->name, c->hostname, from_name);
116                 return true;
117         }
118
119         to = lookup_node(to_name);
120
121         if(!to) {
122                 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
123                            "REQ_KEY", c->name, c->hostname, to_name);
124                 return true;
125         }
126
127         /* Check if this key request is for us */
128
129         if(to == myself) {                      /* Yes, send our own key back */
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", c->buffer);
142         }
143
144         return true;
145 }
146
147 bool send_ans_key(node_t *to) {
148         char *key;
149
150         // Set key parameters
151         to->incipher = myself->incipher;
152         to->inkeylength = myself->inkeylength;
153         to->indigest = myself->indigest;
154         to->inmaclength = myself->inmaclength;
155         to->incompression = myself->incompression;
156
157         // Allocate memory for key
158         to->inkey = xrealloc(to->inkey, to->inkeylength);
159
160         // Create a new key
161         RAND_pseudo_bytes((unsigned char *)to->inkey, to->inkeylength);
162         if(to->incipher)
163                 EVP_DecryptInit_ex(&to->inctx, to->incipher, NULL, (unsigned char *)to->inkey, (unsigned char *)to->inkey + to->incipher->key_len);
164
165         // Reset sequence number and late packet window
166         mykeyused = true;
167         to->received_seqno = 0;
168         memset(to->late, 0, sizeof(to->late));
169
170         // Convert to hexadecimal and send
171         key = alloca(2 * to->inkeylength + 1);
172         bin2hex(to->inkey, key, to->inkeylength);
173         key[to->inkeylength * 2] = '\0';
174
175         return send_request(to->nexthop->connection, "%d %s %s %s %d %d %d %d", ANS_KEY,
176                         myself->name, to->name, key,
177                         to->incipher ? to->incipher->nid : 0,
178                         to->indigest ? to->indigest->type : 0, to->inmaclength,
179                         to->incompression);
180 }
181
182 bool ans_key_h(connection_t *c) {
183         char from_name[MAX_STRING_SIZE];
184         char to_name[MAX_STRING_SIZE];
185         char key[MAX_STRING_SIZE];
186         char address[MAX_STRING_SIZE] = "";
187         char port[MAX_STRING_SIZE] = "";
188         int cipher, digest, maclength, compression;
189         node_t *from, *to;
190
191         if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
192                 from_name, to_name, key, &cipher, &digest, &maclength,
193                 &compression, address, port) < 7) {
194                 logger(LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
195                            c->hostname);
196                 return false;
197         }
198
199         if(!check_id(from_name) || !check_id(to_name)) {
200                 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
201                 return false;
202         }
203
204         from = lookup_node(from_name);
205
206         if(!from) {
207                 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
208                            "ANS_KEY", c->name, c->hostname, from_name);
209                 return true;
210         }
211
212         to = lookup_node(to_name);
213
214         if(!to) {
215                 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
216                            "ANS_KEY", c->name, c->hostname, to_name);
217                 return true;
218         }
219
220         /* Forward it if necessary */
221
222         if(to != myself) {
223                 if(tunnelserver)
224                         return true;
225
226                 if(!to->status.reachable) {
227                         logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
228                                 "ANS_KEY", c->name, c->hostname, to_name);
229                         return true;
230                 }
231
232                 if(!*address && from->address.sa.sa_family != AF_UNSPEC) {
233                         char *address, *port;
234                         ifdebug(PROTOCOL) logger(LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
235                         sockaddr2str(&from->address, &address, &port);
236                         send_request(to->nexthop->connection, "%s %s %s", c->buffer, address, port);
237                         free(address);
238                         free(port);
239                         return true;
240                 }
241
242                 return send_request(to->nexthop->connection, "%s", c->buffer);
243         }
244
245         /* Update our copy of the origin's packet key */
246         from->outkey = xrealloc(from->outkey, strlen(key) / 2);
247
248         from->outkey = xstrdup(key);
249         from->outkeylength = strlen(key) / 2;
250         hex2bin(key, from->outkey, from->outkeylength);
251
252         /* Check and lookup cipher and digest algorithms */
253
254         if(cipher) {
255                 from->outcipher = EVP_get_cipherbynid(cipher);
256
257                 if(!from->outcipher) {
258                         logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name,
259                                    from->hostname);
260                         return true;
261                 }
262
263                 if(from->outkeylength != from->outcipher->key_len + from->outcipher->iv_len) {
264                         logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name,
265                                    from->hostname);
266                         return true;
267                 }
268         } else {
269                 from->outcipher = NULL;
270         }
271
272         from->outmaclength = maclength;
273
274         if(digest) {
275                 from->outdigest = EVP_get_digestbynid(digest);
276
277                 if(!from->outdigest) {
278                         logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name,
279                                    from->hostname);
280                         return true;
281                 }
282
283                 if(from->outmaclength > from->outdigest->md_size || from->outmaclength < 0) {
284                         logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!",
285                                    from->name, from->hostname);
286                         return true;
287                 }
288         } else {
289                 from->outdigest = NULL;
290         }
291
292         if(compression < 0 || compression > 11) {
293                 logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
294                 return true;
295         }
296         
297         from->outcompression = compression;
298
299         if(from->outcipher)
300                 if(!EVP_EncryptInit_ex(&from->outctx, from->outcipher, NULL, (unsigned char *)from->outkey, (unsigned char *)from->outkey + from->outcipher->key_len)) {
301                         logger(LOG_ERR, "Error during initialisation of key from %s (%s): %s",
302                                         from->name, from->hostname, ERR_error_string(ERR_get_error(), NULL));
303                         return true;
304                 }
305
306         from->status.validkey = true;
307         from->sent_seqno = 0;
308
309         if(*address && *port) {
310                 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
311                 sockaddr_t sa = str2sockaddr(address, port);
312                 update_node_udp(from, &sa);
313         }
314
315         if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuprobes)
316                 send_mtu_probe(from);
317
318         return true;
319 }