Merge branch 'master' into 1.1
[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 "splay_tree.h"
26 #include "cipher.h"
27 #include "connection.h"
28 #include "crypto.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 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 %lx %s", KEY_CHANGED, random(), myself->name);
50 }
51
52 bool key_changed_h(connection_t *c, char *request) {
53         char name[MAX_STRING_SIZE];
54         node_t *n;
55
56         cp();
57
58         if(sscanf(request, "%*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(request))
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, request);
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, char *request) {
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(request, "%*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
125                 send_ans_key(from);
126         } else {
127                 if(tunnelserver)
128                         return false;
129
130                 if(!to->status.reachable) {
131                         logger(LOG_WARNING, _("Got %s from %s (%s) destination %s which is not reachable"),
132                                 "REQ_KEY", c->name, c->hostname, to_name);
133                         return true;
134                 }
135
136                 send_request(to->nexthop->connection, "%s", request);
137         }
138
139         return true;
140 }
141
142 bool send_ans_key(node_t *to) {
143         size_t keylen = cipher_keylength(&myself->incipher);
144         char key[keylen * 2 + 1];
145
146         cp();
147
148         cipher_open_by_nid(&to->incipher, cipher_get_nid(&myself->incipher));
149         digest_open_by_nid(&to->indigest, digest_get_nid(&myself->indigest));
150         to->inmaclength = myself->inmaclength;
151         to->incompression = myself->incompression;
152
153         randomize(key, keylen);
154         cipher_set_key(&to->incipher, key, true);
155
156         bin2hex(key, key, keylen);
157         key[keylen * 2] = '\0';
158
159         // Reset sequence number and late packet window
160         mykeyused = true;
161         to->received_seqno = 0;
162         memset(to->late, 0, sizeof(to->late));
163
164         return send_request(to->nexthop->connection, "%d %s %s %s %d %d %d %d", ANS_KEY,
165                                                 myself->name, to->name, key,
166                                                 cipher_get_nid(&to->incipher),
167                                                 digest_get_nid(&to->indigest), to->inmaclength,
168                                                 to->incompression);
169 }
170
171 bool ans_key_h(connection_t *c, char *request) {
172         char from_name[MAX_STRING_SIZE];
173         char to_name[MAX_STRING_SIZE];
174         char key[MAX_STRING_SIZE];
175         int cipher, digest, maclength, compression;
176         node_t *from, *to;
177
178         cp();
179
180         if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d",
181                 from_name, to_name, key, &cipher, &digest, &maclength,
182                 &compression) != 7) {
183                 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ANS_KEY", c->name,
184                            c->hostname);
185                 return false;
186         }
187
188         from = lookup_node(from_name);
189
190         if(!from) {
191                 logger(LOG_ERR, _("Got %s from %s (%s) origin %s which does not exist in our connection list"),
192                            "ANS_KEY", c->name, c->hostname, from_name);
193                 return false;
194         }
195
196         to = lookup_node(to_name);
197
198         if(!to) {
199                 logger(LOG_ERR, _("Got %s from %s (%s) destination %s which does not exist in our connection list"),
200                            "ANS_KEY", c->name, c->hostname, to_name);
201                 return false;
202         }
203
204         /* Forward it if necessary */
205
206         if(to != myself) {
207                 if(tunnelserver)
208                         return false;
209
210                 if(!to->status.reachable) {
211                         logger(LOG_WARNING, _("Got %s from %s (%s) destination %s which is not reachable"),
212                                    "ANS_KEY", c->name, c->hostname, to_name);
213                         return true;
214                 }
215
216                 return send_request(to->nexthop->connection, "%s", request);
217         }
218
219         /* Check and lookup cipher and digest algorithms */
220
221         if(!cipher_open_by_nid(&from->outcipher, cipher)) {
222                 logger(LOG_ERR, _("Node %s (%s) uses unknown cipher!"), from->name, from->hostname);
223                 return false;
224         }
225
226         if(strlen(key) / 2 != cipher_keylength(&from->outcipher)) {
227                 logger(LOG_ERR, _("Node %s (%s) uses wrong keylength!"), from->name, from->hostname);
228                 return false;
229         }
230
231         from->outmaclength = maclength;
232
233         if(!digest_open_by_nid(&from->outdigest, digest)) {
234                 logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), from->name, from->hostname);
235                 return false;
236         }
237
238         if(from->outmaclength > digest_length(&from->outdigest) || from->outmaclength < 0) {
239                 logger(LOG_ERR, _("Node %s (%s) uses bogus MAC length!"), from->name, from->hostname);
240                 return false;
241         }
242
243         if(compression < 0 || compression > 11) {
244                 logger(LOG_ERR, _("Node %s (%s) uses bogus compression level!"), from->name, from->hostname);
245                 return false;
246         }
247         
248         from->outcompression = compression;
249
250         /* Update our copy of the origin's packet key */
251
252         hex2bin(key, key, cipher_keylength(&from->outcipher));
253         cipher_set_key(&from->outcipher, key, false);
254
255         from->status.validkey = true;
256         from->status.waitingforkey = false;
257         from->sent_seqno = 0;
258
259         if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuprobes)
260                 send_mtu_probe(from);
261
262         return true;
263 }