Free resources in rsa_t.
[oweals/tinc.git] / src / protocol_key.c
1 /*
2     protocol_key.c -- handle the meta-protocol, key exchange
3     Copyright (C) 1999-2005 Ivo Timmermans,
4                   2000-2010 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 void send_key_changed() {
38         splay_node_t *node;
39         connection_t *c;
40
41         send_request(broadcast, "%d %x %s", KEY_CHANGED, rand(), myself->name);
42
43         /* Immediately send new keys to directly connected nodes to keep UDP mappings alive */
44
45         for(node = connection_tree->head; node; node = node->next) {
46                 c = node->data;
47                 if(c->status.active && c->node && c->node->status.reachable)
48                         send_ans_key(c->node);
49         }
50 }
51
52 bool key_changed_h(connection_t *c, char *request) {
53         char name[MAX_STRING_SIZE];
54         node_t *n;
55
56         if(sscanf(request, "%*d %*x " MAX_STRING, name) != 1) {
57                 logger(LOG_ERR, "Got bad %s from %s (%s)", "KEY_CHANGED",
58                            c->name, c->hostname);
59                 return false;
60         }
61
62         if(seen_request(request))
63                 return true;
64
65         n = lookup_node(name);
66
67         if(!n) {
68                 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
69                            "KEY_CHANGED", c->name, c->hostname, name);
70                 return true;
71         }
72
73         n->status.validkey = false;
74         n->last_req_key = 0;
75
76         /* Tell the others */
77
78         if(!tunnelserver)
79                 forward_request(c, request);
80
81         return true;
82 }
83
84 bool send_req_key(node_t *to) {
85         return send_request(to->nexthop->connection, "%d %s %s", REQ_KEY, myself->name, to->name);
86 }
87
88 bool req_key_h(connection_t *c, char *request) {
89         char from_name[MAX_STRING_SIZE];
90         char to_name[MAX_STRING_SIZE];
91         node_t *from, *to;
92
93         if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING, from_name, to_name) != 2) {
94                 logger(LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
95                            c->hostname);
96                 return false;
97         }
98
99         if(!check_id(from_name) || !check_id(to_name)) {
100                 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", c->name, c->hostname, "invalid name");
101                 return false;
102         }
103
104         from = lookup_node(from_name);
105
106         if(!from) {
107                 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
108                            "REQ_KEY", c->name, c->hostname, from_name);
109                 return true;
110         }
111
112         to = lookup_node(to_name);
113
114         if(!to) {
115                 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
116                            "REQ_KEY", c->name, c->hostname, to_name);
117                 return true;
118         }
119
120         /* Check if this key request is for us */
121
122         if(to == myself) {                      /* Yes, send our own key back */
123
124                 send_ans_key(from);
125         } else {
126                 if(tunnelserver)
127                         return true;
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", request);
136         }
137
138         return true;
139 }
140
141 bool send_ans_key(node_t *to) {
142         size_t keylen = cipher_keylength(&myself->incipher);
143         char key[keylen * 2 + 1];
144
145         cipher_open_by_nid(&to->incipher, cipher_get_nid(&myself->incipher));
146         digest_open_by_nid(&to->indigest, digest_get_nid(&myself->indigest), digest_length(&myself->indigest));
147         to->incompression = myself->incompression;
148
149         randomize(key, keylen);
150         cipher_set_key(&to->incipher, key, true);
151         digest_set_key(&to->indigest, key, keylen);
152
153         bin2hex(key, key, keylen);
154         key[keylen * 2] = '\0';
155
156         // Reset sequence number and late packet window
157         mykeyused = true;
158         to->received_seqno = 0;
159         if(replaywin) memset(to->late, 0, replaywin);
160
161         return send_request(to->nexthop->connection, "%d %s %s %s %d %d %zu %d", ANS_KEY,
162                                                 myself->name, to->name, key,
163                                                 cipher_get_nid(&to->incipher),
164                                                 digest_get_nid(&to->indigest),
165                                                 digest_length(&to->indigest),
166                                                 to->incompression);
167 }
168
169 bool ans_key_h(connection_t *c, char *request) {
170         char from_name[MAX_STRING_SIZE];
171         char to_name[MAX_STRING_SIZE];
172         char key[MAX_STRING_SIZE];
173         char address[MAX_STRING_SIZE] = "";
174         char port[MAX_STRING_SIZE] = "";
175         int cipher, digest, maclength, compression, keylen;
176         node_t *from, *to;
177
178         if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
179                 from_name, to_name, key, &cipher, &digest, &maclength,
180                 &compression, address, port) < 7) {
181                 logger(LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
182                            c->hostname);
183                 return false;
184         }
185
186         if(!check_id(from_name) || !check_id(to_name)) {
187                 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
188                 return false;
189         }
190
191         from = lookup_node(from_name);
192
193         if(!from) {
194                 logger(LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
195                            "ANS_KEY", c->name, c->hostname, from_name);
196                 return true;
197         }
198
199         to = lookup_node(to_name);
200
201         if(!to) {
202                 logger(LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
203                            "ANS_KEY", c->name, c->hostname, to_name);
204                 return true;
205         }
206
207         /* Forward it if necessary */
208
209         if(to != myself) {
210                 if(tunnelserver)
211                         return true;
212
213                 if(!to->status.reachable) {
214                         logger(LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
215                                    "ANS_KEY", c->name, c->hostname, to_name);
216                         return true;
217                 }
218
219                 if(!*address && from->address.sa.sa_family != AF_UNSPEC) {
220                         char *address, *port;
221                         ifdebug(PROTOCOL) logger(LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
222                         sockaddr2str(&from->address, &address, &port);
223                         send_request(to->nexthop->connection, "%s %s %s", request, address, port);
224                         free(address);
225                         free(port);
226                         return true;
227                 }
228
229                 return send_request(to->nexthop->connection, "%s", request);
230         }
231
232         /* Check and lookup cipher and digest algorithms */
233
234         if(!cipher_open_by_nid(&from->outcipher, cipher)) {
235                 logger(LOG_ERR, "Node %s (%s) uses unknown cipher!", from->name, from->hostname);
236                 return false;
237         }
238
239         keylen = strlen(key) / 2;
240
241         if(keylen != cipher_keylength(&from->outcipher)) {
242                 logger(LOG_ERR, "Node %s (%s) uses wrong keylength!", from->name, from->hostname);
243                 return false;
244         }
245
246         if(!digest_open_by_nid(&from->outdigest, digest, maclength)) {
247                 logger(LOG_ERR, "Node %s (%s) uses unknown digest!", from->name, from->hostname);
248                 return false;
249         }
250
251         if(maclength != digest_length(&from->outdigest)) {
252                 logger(LOG_ERR, "Node %s (%s) uses bogus MAC length!", from->name, from->hostname);
253                 return false;
254         }
255
256         if(compression < 0 || compression > 11) {
257                 logger(LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
258                 return true;
259         }
260         
261         from->outcompression = compression;
262
263         /* Update our copy of the origin's packet key */
264
265         hex2bin(key, key, keylen);
266         cipher_set_key(&from->outcipher, key, false);
267         digest_set_key(&from->outdigest, key, keylen);
268
269         from->status.validkey = true;
270         from->sent_seqno = 0;
271
272         if(*address && *port) {
273                 ifdebug(PROTOCOL) logger(LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
274                 sockaddr_t sa = str2sockaddr(address, port);
275                 update_node_udp(from, &sa);
276         }
277
278         if(from->options & OPTION_PMTU_DISCOVERY && !from->mtuprobes)
279                 send_mtu_probe(from);
280
281         return true;
282 }