Require OpenSSL 1.1.0 or later.
[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-2016 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 static bool mykeyused = false;
38
39 void send_key_changed(void) {
40         avl_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
50                 if(c->status.active && c->node && c->node->status.reachable) {
51                         send_ans_key(c->node);
52                 }
53         }
54 }
55
56 bool key_changed_h(connection_t *c) {
57         char name[MAX_STRING_SIZE];
58         node_t *n;
59
60         if(sscanf(c->buffer, "%*d %*x " MAX_STRING, name) != 1) {
61                 logger(LOG_ERR, "Got bad %s from %s (%s)", "KEY_CHANGED",
62                        c->name, c->hostname);
63                 return false;
64         }
65
66         if(!check_id(name)) {
67                 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "KEY_CHANGED", c->name, c->hostname, "invalid name");
68                 return false;
69         }
70
71         if(seen_request(c->buffer)) {
72                 return true;
73         }
74
75         n = lookup_node(name);
76
77         if(!n) {
78                 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
79                        "KEY_CHANGED", c->name, c->hostname, name);
80                 return true;
81         }
82
83         n->status.validkey = false;
84         n->last_req_key = 0;
85
86         /* Tell the others */
87
88         if(!tunnelserver) {
89                 forward_request(c);
90         }
91
92         return true;
93 }
94
95 bool send_req_key(node_t *to) {
96         return send_request(to->nexthop->connection, "%d %s %s", REQ_KEY, myself->name, to->name);
97 }
98
99 bool req_key_h(connection_t *c) {
100         char from_name[MAX_STRING_SIZE];
101         char to_name[MAX_STRING_SIZE];
102         node_t *from, *to;
103
104         if(sscanf(c->buffer, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) {
105                 logger(LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
106                        c->hostname);
107                 return false;
108         }
109
110         if(!check_id(from_name) || !check_id(to_name)) {
111                 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", c->name, c->hostname, "invalid name");
112                 return false;
113         }
114
115         from = lookup_node(from_name);
116
117         if(!from) {
118                 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
119                        "REQ_KEY", c->name, c->hostname, from_name);
120                 return true;
121         }
122
123         to = lookup_node(to_name);
124
125         if(!to) {
126                 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
127                        "REQ_KEY", c->name, c->hostname, to_name);
128                 return true;
129         }
130
131         /* Check if this key request is for us */
132
133         if(to == myself) {                      /* Yes, send our own key back */
134                 if(!from->status.reachable) {
135                         logger(LOG_WARNING, "Got %s from %s (%s) origin %s which is not reachable",
136                                "REQ_KEY", c->name, c->hostname, from_name);
137                         return true;
138                 }
139
140                 if(!send_ans_key(from)) {
141                         return false;
142                 }
143         } else {
144                 if(tunnelserver) {
145                         return true;
146                 }
147
148                 if(!to->status.reachable) {
149                         logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
150                                "REQ_KEY", c->name, c->hostname, to_name);
151                         return true;
152                 }
153
154                 send_request(to->nexthop->connection, "%s", c->buffer);
155         }
156
157         return true;
158 }
159
160 bool send_ans_key(node_t *to) {
161         // Set key parameters
162         to->incipher = myself->incipher;
163         to->inkeylength = myself->inkeylength;
164         to->indigest = myself->indigest;
165         to->inmaclength = myself->inmaclength;
166         to->incompression = myself->incompression;
167
168         // Allocate memory for key
169         to->inkey = xrealloc(to->inkey, to->inkeylength);
170
171         // Create a new key
172         if(1 != RAND_bytes((unsigned char *)to->inkey, to->inkeylength)) {
173                 int err = ERR_get_error();
174                 logger(LOG_ERR, "Failed to generate random for key (%s)", ERR_error_string(err, NULL));
175                 return false; // Do not send insecure keys, let connection attempt fail.
176         }
177
178         if(to->incipher) {
179                 EVP_DecryptInit_ex(to->inctx, to->incipher, NULL, (unsigned char *)to->inkey, (unsigned char *)to->inkey + EVP_CIPHER_key_length(to->incipher));
180         }
181
182         // Reset sequence number and late packet window
183         mykeyused = true;
184         to->received_seqno = 0;
185
186         if(replaywin) {
187                 memset(to->late, 0, replaywin);
188         }
189
190         // Convert to hexadecimal and send
191         char key[2 * to->inkeylength + 1];
192         bin2hex(to->inkey, key, to->inkeylength);
193         key[to->inkeylength * 2] = '\0';
194
195         return send_request(to->nexthop->connection, "%d %s %s %s %d %d %d %d", ANS_KEY,
196                             myself->name, to->name, key,
197                             to->incipher ? EVP_CIPHER_nid(to->incipher) : 0,
198                             to->indigest ? EVP_MD_type(to->indigest) : 0, to->inmaclength,
199                             to->incompression);
200 }
201
202 bool ans_key_h(connection_t *c) {
203         char from_name[MAX_STRING_SIZE];
204         char to_name[MAX_STRING_SIZE];
205         char key[MAX_STRING_SIZE];
206         char address[MAX_STRING_SIZE] = "";
207         char port[MAX_STRING_SIZE] = "";
208         int cipher, digest, maclength, compression;
209         node_t *from, *to;
210
211         if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
212                         from_name, to_name, key, &cipher, &digest, &maclength,
213                         &compression, address, port) < 7) {
214                 logger(LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
215                        c->hostname);
216                 return false;
217         }
218
219         if(!check_id(from_name) || !check_id(to_name)) {
220                 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
221                 return false;
222         }
223
224         from = lookup_node(from_name);
225
226         if(!from) {
227                 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
228                        "ANS_KEY", c->name, c->hostname, from_name);
229                 return true;
230         }
231
232         to = lookup_node(to_name);
233
234         if(!to) {
235                 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
236                        "ANS_KEY", c->name, c->hostname, to_name);
237                 return true;
238         }
239
240         /* Forward it if necessary */
241
242         if(to != myself) {
243                 if(tunnelserver) {
244                         return true;
245                 }
246
247                 if(!to->status.reachable) {
248                         logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
249                                "ANS_KEY", c->name, c->hostname, to_name);
250                         return true;
251                 }
252
253                 if(!*address && from->address.sa.sa_family != AF_UNSPEC && to->minmtu) {
254                         char *address, *port;
255                         ifdebug(PROTOCOL) logger(LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
256                         sockaddr2str(&from->address, &address, &port);
257                         send_request(to->nexthop->connection, "%s %s %s", c->buffer, address, port);
258                         free(address);
259                         free(port);
260                         return true;
261                 }
262
263                 return send_request(to->nexthop->connection, "%s", c->buffer);
264         }
265
266         /* Don't use key material until every check has passed. */
267         from->status.validkey = false;
268
269         /* Update our copy of the origin's packet key */
270         from->outkey = xrealloc(from->outkey, strlen(key) / 2);
271         from->outkeylength = strlen(key) / 2;
272
273         if(!hex2bin(key, from->outkey, from->outkeylength)) {
274                 logger(LOG_ERR, "Got bad %s from %s(%s): %s", "ANS_KEY", from->name, from->hostname, "invalid key");
275                 return true;
276         }
277
278         /* Check and lookup cipher and digest algorithms */
279
280         if(cipher) {
281                 from->outcipher = EVP_get_cipherbynid(cipher);
282
283                 if(!from->outcipher) {
284                         logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name,
285                                from->hostname);
286                         return true;
287                 }
288
289                 if(from->outkeylength != EVP_CIPHER_key_length(from->outcipher) + EVP_CIPHER_iv_length(from->outcipher)) {
290                         logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name,
291                                from->hostname);
292                         return true;
293                 }
294         } else {
295                 if(from->outkeylength != 1) {
296                         logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname);
297                         return true;
298                 }
299
300                 from->outcipher = NULL;
301         }
302
303         from->outmaclength = maclength;
304
305         if(digest) {
306                 from->outdigest = EVP_get_digestbynid(digest);
307
308                 if(!from->outdigest) {
309                         logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name,
310                                from->hostname);
311                         return true;
312                 }
313
314                 if(from->outmaclength > EVP_MD_size(from->outdigest) || from->outmaclength < 0) {
315                         logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!",
316                                from->name, from->hostname);
317                         return true;
318                 }
319         } else {
320                 from->outdigest = NULL;
321         }
322
323         if(compression < 0 || compression > 11) {
324                 logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
325                 return true;
326         }
327
328         from->outcompression = compression;
329
330         if(from->outcipher)
331                 if(!EVP_EncryptInit_ex(from->outctx, from->outcipher, NULL, (unsigned char *)from->outkey, (unsigned char *)from->outkey + EVP_CIPHER_key_length(from->outcipher))) {
332                         logger(LOG_ERR, "Error during initialisation of key from %s (%s): %s",
333                                from->name, from->hostname, ERR_error_string(ERR_get_error(), NULL));
334                         return true;
335                 }
336
337         from->status.validkey = true;
338         from->sent_seqno = 0;
339
340         if(*address && *port) {
341                 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
342                 sockaddr_t sa = str2sockaddr(address, port);
343                 update_node_udp(from, &sa);
344         }
345
346         if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuevent) {
347                 send_mtu_probe(from);
348         }
349
350         return true;
351 }