Fix packet authentication.
[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         digest_set_key(&to->indigest, key, keylen);
143
144         bin2hex(key, key, keylen);
145         key[keylen * 2] = '\0';
146
147         // Reset sequence number and late packet window
148         mykeyused = true;
149         to->received_seqno = 0;
150         memset(to->late, 0, sizeof(to->late));
151
152         return send_request(to->nexthop->connection, "%d %s %s %s %d %d %zu %d", ANS_KEY,
153                                                 myself->name, to->name, key,
154                                                 cipher_get_nid(&to->incipher),
155                                                 digest_get_nid(&to->indigest),
156                                                 digest_length(&to->indigest),
157                                                 to->incompression);
158 }
159
160 bool ans_key_h(connection_t *c, char *request) {
161         char from_name[MAX_STRING_SIZE];
162         char to_name[MAX_STRING_SIZE];
163         char key[MAX_STRING_SIZE];
164         int cipher, digest, maclength, compression, keylen;
165         node_t *from, *to;
166
167         if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d",
168                 from_name, to_name, key, &cipher, &digest, &maclength,
169                 &compression) != 7) {
170                 logger(LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
171                            c->hostname);
172                 return false;
173         }
174
175         from = lookup_node(from_name);
176
177         if(!from) {
178                 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
179                            "ANS_KEY", c->name, c->hostname, from_name);
180                 return false;
181         }
182
183         to = lookup_node(to_name);
184
185         if(!to) {
186                 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
187                            "ANS_KEY", c->name, c->hostname, to_name);
188                 return false;
189         }
190
191         /* Forward it if necessary */
192
193         if(to != myself) {
194                 if(tunnelserver)
195                         return false;
196
197                 if(!to->status.reachable) {
198                         logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
199                                    "ANS_KEY", c->name, c->hostname, to_name);
200                         return true;
201                 }
202
203                 return send_request(to->nexthop->connection, "%s", request);
204         }
205
206         /* Check and lookup cipher and digest algorithms */
207
208         if(!cipher_open_by_nid(&from->outcipher, cipher)) {
209                 logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name, from->hostname);
210                 return false;
211         }
212
213         keylen = strlen(key) / 2;
214
215         if(keylen != cipher_keylength(&from->outcipher)) {
216                 logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname);
217                 return false;
218         }
219
220         if(!digest_open_by_nid(&from->outdigest, digest, maclength)) {
221                 logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name, from->hostname);
222                 return false;
223         }
224
225         if(maclength != digest_length(&from->outdigest)) {
226                 logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!", from->name, from->hostname);
227                 return false;
228         }
229
230         if(compression < 0 || compression > 11) {
231                 logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
232                 return false;
233         }
234         
235         from->outcompression = compression;
236
237         /* Update our copy of the origin's packet key */
238
239         hex2bin(key, key, keylen);
240         cipher_set_key(&from->outcipher, key, false);
241         digest_set_key(&from->outdigest, key, keylen);
242
243         from->status.validkey = true;
244         from->status.waitingforkey = false;
245         from->sent_seqno = 0;
246
247         if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuprobes)
248                 send_mtu_probe(from);
249
250         return true;
251 }