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