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 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 "splay_tree.h"
24 #include "cipher.h"
25 #include "connection.h"
26 #include "crypto.h"
27 #include "logger.h"
28 #include "net.h"
29 #include "netutl.h"
30 #include "node.h"
31 #include "protocol.h"
32 #include "utils.h"
33 #include "xalloc.h"
34
35 static bool mykeyused = false;
36
37 bool send_key_changed() {
38         /* Only send this message if some other daemon requested our key previously.
39            This reduces unnecessary key_changed broadcasts.
40          */
41
42         if(!mykeyused)
43                 return true;
44
45         return send_request(broadcast, "%d %x %s", KEY_CHANGED, rand(), myself->name);
46 }
47
48 bool key_changed_h(connection_t *c, char *request) {
49         char name[MAX_STRING_SIZE];
50         node_t *n;
51
52         if(sscanf(request, "%*d %*x " MAX_STRING, name) != 1) {
53                 logger(LOG_ERR, "Got bad %s from %s (%s)", "KEY_CHANGED",
54                            c->name, c->hostname);
55                 return false;
56         }
57
58         if(seen_request(request))
59                 return true;
60
61         n = lookup_node(name);
62
63         if(!n) {
64                 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
65                            "KEY_CHANGED", c->name, c->hostname, name);
66                 return false;
67         }
68
69         n->status.validkey = false;
70         n->status.waitingforkey = false;
71
72         /* Tell the others */
73
74         if(!tunnelserver)
75                 forward_request(c, request);
76
77         return true;
78 }
79
80 bool send_req_key(node_t *to) {
81         return send_request(to->nexthop->connection, "%d %s %s", REQ_KEY, myself->name, to->name);
82 }
83
84 bool req_key_h(connection_t *c, char *request) {
85         char from_name[MAX_STRING_SIZE];
86         char to_name[MAX_STRING_SIZE];
87         node_t *from, *to;
88
89         if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) {
90                 logger(LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
91                            c->hostname);
92                 return false;
93         }
94
95         from = lookup_node(from_name);
96
97         if(!from) {
98                 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
99                            "REQ_KEY", c->name, c->hostname, from_name);
100                 return false;
101         }
102
103         to = lookup_node(to_name);
104
105         if(!to) {
106                 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
107                            "REQ_KEY", c->name, c->hostname, to_name);
108                 return false;
109         }
110
111         /* Check if this key request is for us */
112
113         if(to == myself) {                      /* Yes, send our own key back */
114
115                 send_ans_key(from);
116         } else {
117                 if(tunnelserver)
118                         return false;
119
120                 if(!to->status.reachable) {
121                         logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
122                                 "REQ_KEY", c->name, c->hostname, to_name);
123                         return true;
124                 }
125
126                 send_request(to->nexthop->connection, "%s", request);
127         }
128
129         return true;
130 }
131
132 bool send_ans_key(node_t *to) {
133         size_t keylen = cipher_keylength(&myself->incipher);
134         char key[keylen * 2 + 1];
135
136         cipher_open_by_nid(&to->incipher, cipher_get_nid(&myself->incipher));
137         digest_open_by_nid(&to->indigest, digest_get_nid(&myself->indigest), digest_length(&myself->indigest));
138         to->incompression = myself->incompression;
139
140         randomize(key, keylen);
141         cipher_set_key(&to->incipher, key, true);
142
143         bin2hex(key, key, keylen);
144         key[keylen * 2] = '\0';
145
146         // Reset sequence number and late packet window
147         mykeyused = true;
148         to->received_seqno = 0;
149         memset(to->late, 0, sizeof(to->late));
150
151         return send_request(to->nexthop->connection, "%d %s %s %s %d %d %zu %d", ANS_KEY,
152                                                 myself->name, to->name, key,
153                                                 cipher_get_nid(&to->incipher),
154                                                 digest_get_nid(&to->indigest),
155                                                 digest_length(&to->indigest),
156                                                 to->incompression);
157 }
158
159 bool ans_key_h(connection_t *c, char *request) {
160         char from_name[MAX_STRING_SIZE];
161         char to_name[MAX_STRING_SIZE];
162         char key[MAX_STRING_SIZE];
163         int cipher, digest, maclength, compression;
164         node_t *from, *to;
165
166         if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d",
167                 from_name, to_name, key, &cipher, &digest, &maclength,
168                 &compression) != 7) {
169                 logger(LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
170                            c->hostname);
171                 return false;
172         }
173
174         from = lookup_node(from_name);
175
176         if(!from) {
177                 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
178                            "ANS_KEY", c->name, c->hostname, from_name);
179                 return false;
180         }
181
182         to = lookup_node(to_name);
183
184         if(!to) {
185                 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
186                            "ANS_KEY", c->name, c->hostname, to_name);
187                 return false;
188         }
189
190         /* Forward it if necessary */
191
192         if(to != myself) {
193                 if(tunnelserver)
194                         return false;
195
196                 if(!to->status.reachable) {
197                         logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
198                                    "ANS_KEY", c->name, c->hostname, to_name);
199                         return true;
200                 }
201
202                 return send_request(to->nexthop->connection, "%s", request);
203         }
204
205         /* Check and lookup cipher and digest algorithms */
206
207         if(!cipher_open_by_nid(&from->outcipher, cipher)) {
208                 logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name, from->hostname);
209                 return false;
210         }
211
212         if(strlen(key) / 2 != cipher_keylength(&from->outcipher)) {
213                 logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname);
214                 return false;
215         }
216
217         if(!digest_open_by_nid(&from->outdigest, digest, maclength)) {
218                 logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name, from->hostname);
219                 return false;
220         }
221
222         if(maclength != digest_length(&from->outdigest)) {
223                 logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!", from->name, from->hostname);
224                 return false;
225         }
226
227         if(compression < 0 || compression > 11) {
228                 logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
229                 return false;
230         }
231         
232         from->outcompression = compression;
233
234         /* Update our copy of the origin's packet key */
235
236         hex2bin(key, key, cipher_keylength(&from->outcipher));
237         cipher_set_key(&from->outcipher, key, false);
238
239         from->status.validkey = true;
240         from->status.waitingforkey = false;
241         from->sent_seqno = 0;
242
243         if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuprobes)
244                 send_mtu_probe(from);
245
246         return true;
247 }