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