Get rid of a warning when compiling tinc using MinGW.
[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                 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                 if (!send_ans_key(from))
131                         return false;
132         } else {
133                 if(tunnelserver)
134                         return true;
135
136                 if(!to->status.reachable) {
137                         logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
138                                 "REQ_KEY", c->name, c->hostname, to_name);
139                         return true;
140                 }
141
142                 send_request(to->nexthop->connection, "%s", c->buffer);
143         }
144
145         return true;
146 }
147
148 bool send_ans_key(node_t *to) {
149         // Set key parameters
150         to->incipher = myself->incipher;
151         to->inkeylength = myself->inkeylength;
152         to->indigest = myself->indigest;
153         to->inmaclength = myself->inmaclength;
154         to->incompression = myself->incompression;
155
156         // Allocate memory for key
157         to->inkey = xrealloc(to->inkey, to->inkeylength);
158
159         // Create a new key
160         if (1 != RAND_bytes((unsigned char *)to->inkey, to->inkeylength)) {
161                 int err = ERR_get_error();
162                 logger(LOG_ERR, "Failed to generate random for key (%s)", ERR_error_string(err, NULL));
163                 return false; // Do not send insecure keys, let connection attempt fail.
164         }
165
166         if(to->incipher)
167                 EVP_DecryptInit_ex(&to->inctx, to->incipher, NULL, (unsigned char *)to->inkey, (unsigned char *)to->inkey + to->incipher->key_len);
168
169         // Reset sequence number and late packet window
170         mykeyused = true;
171         to->received_seqno = 0;
172         if(replaywin) memset(to->late, 0, replaywin);
173
174         // Convert to hexadecimal and send
175         char key[2 * to->inkeylength + 1];
176         bin2hex(to->inkey, key, to->inkeylength);
177         key[to->inkeylength * 2] = '\0';
178
179         return send_request(to->nexthop->connection, "%d %s %s %s %d %d %d %d", ANS_KEY,
180                         myself->name, to->name, key,
181                         to->incipher ? to->incipher->nid : 0,
182                         to->indigest ? to->indigest->type : 0, to->inmaclength,
183                         to->incompression);
184 }
185
186 bool ans_key_h(connection_t *c) {
187         char from_name[MAX_STRING_SIZE];
188         char to_name[MAX_STRING_SIZE];
189         char key[MAX_STRING_SIZE];
190         char address[MAX_STRING_SIZE] = "";
191         char port[MAX_STRING_SIZE] = "";
192         int cipher, digest, maclength, compression;
193         node_t *from, *to;
194
195         if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
196                 from_name, to_name, key, &cipher, &digest, &maclength,
197                 &compression, address, port) < 7) {
198                 logger(LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
199                            c->hostname);
200                 return false;
201         }
202
203         if(!check_id(from_name) || !check_id(to_name)) {
204                 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
205                 return false;
206         }
207
208         from = lookup_node(from_name);
209
210         if(!from) {
211                 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
212                            "ANS_KEY", c->name, c->hostname, from_name);
213                 return true;
214         }
215
216         to = lookup_node(to_name);
217
218         if(!to) {
219                 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
220                            "ANS_KEY", c->name, c->hostname, to_name);
221                 return true;
222         }
223
224         /* Forward it if necessary */
225
226         if(to != myself) {
227                 if(tunnelserver)
228                         return true;
229
230                 if(!to->status.reachable) {
231                         logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
232                                 "ANS_KEY", c->name, c->hostname, to_name);
233                         return true;
234                 }
235
236                 if(!*address && from->address.sa.sa_family != AF_UNSPEC && to->minmtu) {
237                         char *address, *port;
238                         ifdebug(PROTOCOL) logger(LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
239                         sockaddr2str(&from->address, &address, &port);
240                         send_request(to->nexthop->connection, "%s %s %s", c->buffer, address, port);
241                         free(address);
242                         free(port);
243                         return true;
244                 }
245
246                 return send_request(to->nexthop->connection, "%s", c->buffer);
247         }
248
249         /* Don't use key material until every check has passed. */
250         from->status.validkey = false;
251
252         /* Update our copy of the origin's packet key */
253         from->outkey = xrealloc(from->outkey, strlen(key) / 2);
254         from->outkeylength = strlen(key) / 2;
255         if(!hex2bin(key, from->outkey, from->outkeylength)) {
256                 logger(LOG_ERR, "Got bad %s from %s(%s): %s", "ANS_KEY", from->name, from->hostname, "invalid key");
257                 return true;
258         }
259
260         /* Check and lookup cipher and digest algorithms */
261
262         if(cipher) {
263                 from->outcipher = EVP_get_cipherbynid(cipher);
264
265                 if(!from->outcipher) {
266                         logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name,
267                                    from->hostname);
268                         return true;
269                 }
270
271                 if(from->outkeylength != from->outcipher->key_len + from->outcipher->iv_len) {
272                         logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name,
273                                    from->hostname);
274                         return true;
275                 }
276         } else {
277                 from->outcipher = NULL;
278         }
279
280         from->outmaclength = maclength;
281
282         if(digest) {
283                 from->outdigest = EVP_get_digestbynid(digest);
284
285                 if(!from->outdigest) {
286                         logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name,
287                                    from->hostname);
288                         return true;
289                 }
290
291                 if(from->outmaclength > from->outdigest->md_size || from->outmaclength < 0) {
292                         logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!",
293                                    from->name, from->hostname);
294                         return true;
295                 }
296         } else {
297                 from->outdigest = NULL;
298         }
299
300         if(compression < 0 || compression > 11) {
301                 logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
302                 return true;
303         }
304         
305         from->outcompression = compression;
306
307         if(from->outcipher)
308                 if(!EVP_EncryptInit_ex(&from->outctx, from->outcipher, NULL, (unsigned char *)from->outkey, (unsigned char *)from->outkey + from->outcipher->key_len)) {
309                         logger(LOG_ERR, "Error during initialisation of key from %s (%s): %s",
310                                         from->name, from->hostname, ERR_error_string(ERR_get_error(), NULL));
311                         return true;
312                 }
313
314         from->status.validkey = true;
315         from->sent_seqno = 0;
316
317         if(*address && *port) {
318                 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
319                 sockaddr_t sa = str2sockaddr(address, port);
320                 update_node_udp(from, &sa);
321         }
322
323         if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuevent)
324                 send_mtu_probe(from);
325
326         return true;
327 }