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