]> git.meshlink.io Git - meshlink/blob - src/protocol_key.c
Merge branch 'master' into saverio
[meshlink] / src / protocol_key.c
1 /*
2     protocol_key.c -- handle the meta-protocol, key exchange
3     Copyright (C) 2014 Guus Sliepen <guus@meshlink.io>
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "system.h"
21
22 #include "cipher.h"
23 #include "connection.h"
24 #include "crypto.h"
25 #include "logger.h"
26 #include "net.h"
27 #include "netutl.h"
28 #include "node.h"
29 #include "prf.h"
30 #include "protocol.h"
31 #include "sptps.h"
32 #include "utils.h"
33 #include "xalloc.h"
34
35 static bool mykeyused = false;
36
37 void send_key_changed(void) {
38         send_request(everyone, "%d %x %s", KEY_CHANGED, rand(), myself->name);
39
40         /* Force key exchange for connections using SPTPS */
41
42         for splay_each(node_t, n, node_tree)
43                 if(n->status.reachable && n->status.validkey)
44                         sptps_force_kex(&n->sptps);
45 }
46
47 bool key_changed_h(connection_t *c, const char *request) {
48         char name[MAX_STRING_SIZE];
49         node_t *n;
50
51         if(sscanf(request, "%*d %*x " MAX_STRING, name) != 1) {
52                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "KEY_CHANGED",
53                            c->name, c->hostname);
54                 return false;
55         }
56
57         if(seen_request(request))
58                 return true;
59
60         n = lookup_node(name);
61
62         if(!n) {
63                 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist",
64                            "KEY_CHANGED", c->name, c->hostname, name);
65                 return true;
66         }
67
68         /* Tell the others */
69
70         forward_request(c, request);
71
72         return true;
73 }
74
75 static bool send_initial_sptps_data(void *handle, uint8_t type, const char *data, size_t len) {
76         node_t *to = handle;
77         to->sptps.send_data = send_sptps_data;
78         char buf[len * 4 / 3 + 5];
79         b64encode(data, buf, len);
80         return send_request(to->nexthop->connection, "%d %s %s %d %s", REQ_KEY, myself->name, to->name, REQ_KEY, buf);
81 }
82
83 bool send_req_key(node_t *to) {
84         if(!node_read_ecdsa_public_key(to)) {
85                 logger(DEBUG_PROTOCOL, LOG_DEBUG, "No ECDSA key known for %s (%s)", to->name, to->hostname);
86                 send_request(to->nexthop->connection, "%d %s %s %d", REQ_KEY, myself->name, to->name, REQ_PUBKEY);
87                 return true;
88         }
89
90         if(to->sptps.label)
91                 logger(DEBUG_ALWAYS, LOG_DEBUG, "send_req_key(%s) called while sptps->label != NULL!", to->name);
92
93         char label[25 + strlen(myself->name) + strlen(to->name)];
94         snprintf(label, sizeof label, "tinc UDP key expansion %s %s", myself->name, to->name);
95         sptps_stop(&to->sptps);
96         to->status.validkey = false;
97         to->status.waitingforkey = true;
98         to->last_req_key = now.tv_sec;
99         to->incompression = myself->incompression;
100         return sptps_start(&to->sptps, to, true, true, myself->connection->ecdsa, to->ecdsa, label, sizeof label, send_initial_sptps_data, receive_sptps_record);
101 }
102
103 /* REQ_KEY is overloaded to allow arbitrary requests to be routed between two nodes. */
104
105 static bool req_key_ext_h(connection_t *c, const char *request, node_t *from, int reqno) {
106         switch(reqno) {
107                 case REQ_PUBKEY: {
108                         char *pubkey = ecdsa_get_base64_public_key(myself->connection->ecdsa);
109                         send_request(from->nexthop->connection, "%d %s %s %d %s", REQ_KEY, myself->name, from->name, ANS_PUBKEY, pubkey);
110                         free(pubkey);
111                         return true;
112                 }
113
114                 case ANS_PUBKEY: {
115                         if(node_read_ecdsa_public_key(from)) {
116                                 logger(DEBUG_PROTOCOL, LOG_WARNING, "Got ANS_PUBKEY from %s (%s) even though we already have his pubkey", from->name, from->hostname);
117                                 return true;
118                         }
119
120                         char pubkey[MAX_STRING_SIZE];
121                         if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, pubkey) != 1 || !(from->ecdsa = ecdsa_set_base64_public_key(pubkey))) {
122                                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_PUBKEY", from->name, from->hostname, "invalid pubkey");
123                                 return true;
124                         }
125
126                         logger(DEBUG_PROTOCOL, LOG_INFO, "Learned ECDSA public key from %s (%s)", from->name, from->hostname);
127                         append_config_file(from->name, "ECDSAPublicKey", pubkey);
128                         return true;
129                 }
130
131                 case REQ_KEY: {
132                         if(!node_read_ecdsa_public_key(from)) {
133                                 logger(DEBUG_PROTOCOL, LOG_DEBUG, "No ECDSA key known for %s (%s)", from->name, from->hostname);
134                                 send_request(from->nexthop->connection, "%d %s %s %d", REQ_KEY, myself->name, from->name, REQ_PUBKEY);
135                                 return true;
136                         }
137
138                         if(from->sptps.label)
139                                 logger(DEBUG_ALWAYS, LOG_DEBUG, "Got REQ_KEY from %s while we already started a SPTPS session!", from->name);
140
141                         char buf[MAX_STRING_SIZE];
142                         int len;
143
144                         if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
145                                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_SPTPS_START", from->name, from->hostname, "invalid SPTPS data");
146                                 return true;
147                         }
148
149                         char label[25 + strlen(from->name) + strlen(myself->name)];
150                         snprintf(label, sizeof label, "tinc UDP key expansion %s %s", from->name, myself->name);
151                         sptps_stop(&from->sptps);
152                         from->status.validkey = false;
153                         from->status.waitingforkey = true;
154                         from->last_req_key = now.tv_sec;
155                         sptps_start(&from->sptps, from, false, true, myself->connection->ecdsa, from->ecdsa, label, sizeof label, send_sptps_data, receive_sptps_record);
156                         sptps_receive_data(&from->sptps, buf, len);
157                         return true;
158                 }
159
160                 case REQ_SPTPS: {
161                         if(!from->status.validkey) {
162                                 logger(DEBUG_PROTOCOL, LOG_ERR, "Got REQ_SPTPS from %s (%s) but we don't have a valid key yet", from->name, from->hostname);
163                                 return true;
164                         }
165
166                         char buf[MAX_STRING_SIZE];
167                         int len;
168                         if(sscanf(request, "%*d %*s %*s %*d " MAX_STRING, buf) != 1 || !(len = b64decode(buf, buf, strlen(buf)))) {
169                                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_SPTPS", from->name, from->hostname, "invalid SPTPS data");
170                                 return true;
171                         }
172                         sptps_receive_data(&from->sptps, buf, len);
173                         return true;
174                 }
175
176                 default:
177                         logger(DEBUG_ALWAYS, LOG_ERR, "Unknown extended REQ_KEY request from %s (%s): %s", from->name, from->hostname, request);
178                         return true;
179         }
180 }
181
182 bool req_key_h(connection_t *c, const char *request) {
183         char from_name[MAX_STRING_SIZE];
184         char to_name[MAX_STRING_SIZE];
185         node_t *from, *to;
186         int reqno = 0;
187
188         if(sscanf(request, "%*d " MAX_STRING " " MAX_STRING " %d", from_name, to_name, &reqno) < 2) {
189                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "REQ_KEY", c->name,
190                            c->hostname);
191                 return false;
192         }
193
194         if(!check_id(from_name) || !check_id(to_name)) {
195                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "REQ_KEY", c->name, c->hostname, "invalid name");
196                 return false;
197         }
198
199         from = lookup_node(from_name);
200
201         if(!from) {
202                 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
203                            "REQ_KEY", c->name, c->hostname, from_name);
204                 return true;
205         }
206
207         to = lookup_node(to_name);
208
209         if(!to) {
210                 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
211                            "REQ_KEY", c->name, c->hostname, to_name);
212                 return true;
213         }
214
215         /* Check if this key request is for us */
216
217         if(to == myself) {                      /* Yes */
218                 /* Is this an extended REQ_KEY message? */
219                 if(reqno)
220                         return req_key_ext_h(c, request, from, reqno);
221
222                 /* No, just send our key back */
223                 send_ans_key(from);
224         } else {
225                 if(!to->status.reachable) {
226                         logger(DEBUG_PROTOCOL, LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
227                                 "REQ_KEY", c->name, c->hostname, to_name);
228                         return true;
229                 }
230
231                 send_request(to->nexthop->connection, "%s", request);
232         }
233
234         return true;
235 }
236
237 bool send_ans_key(node_t *to) {
238         abort();
239 }
240
241 bool ans_key_h(connection_t *c, const char *request) {
242         char from_name[MAX_STRING_SIZE];
243         char to_name[MAX_STRING_SIZE];
244         char key[MAX_STRING_SIZE];
245         char address[MAX_STRING_SIZE] = "";
246         char port[MAX_STRING_SIZE] = "";
247         int cipher, digest, maclength, compression, keylen;
248         node_t *from, *to;
249
250         if(sscanf(request, "%*d "MAX_STRING" "MAX_STRING" "MAX_STRING" %d %d %d %d "MAX_STRING" "MAX_STRING,
251                 from_name, to_name, key, &cipher, &digest, &maclength,
252                 &compression, address, port) < 7) {
253                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ANS_KEY", c->name,
254                            c->hostname);
255                 return false;
256         }
257
258         if(!check_id(from_name) || !check_id(to_name)) {
259                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ANS_KEY", c->name, c->hostname, "invalid name");
260                 return false;
261         }
262
263         from = lookup_node(from_name);
264
265         if(!from) {
266                 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) origin %s which does not exist in our connection list",
267                            "ANS_KEY", c->name, c->hostname, from_name);
268                 return true;
269         }
270
271         to = lookup_node(to_name);
272
273         if(!to) {
274                 logger(DEBUG_ALWAYS, LOG_ERR, "Got %s from %s (%s) destination %s which does not exist in our connection list",
275                            "ANS_KEY", c->name, c->hostname, to_name);
276                 return true;
277         }
278
279         /* Forward it if necessary */
280
281         if(to != myself) {
282                 if(!to->status.reachable) {
283                         logger(DEBUG_ALWAYS, LOG_WARNING, "Got %s from %s (%s) destination %s which is not reachable",
284                                    "ANS_KEY", c->name, c->hostname, to_name);
285                         return true;
286                 }
287
288                 if(!*address && from->address.sa.sa_family != AF_UNSPEC) {
289                         char *address, *port;
290                         logger(DEBUG_PROTOCOL, LOG_DEBUG, "Appending reflexive UDP address to ANS_KEY from %s to %s", from->name, to->name);
291                         sockaddr2str(&from->address, &address, &port);
292                         send_request(to->nexthop->connection, "%s %s %s", request, address, port);
293                         free(address);
294                         free(port);
295                         return true;
296                 }
297
298                 return send_request(to->nexthop->connection, "%s", request);
299         }
300
301         /* Don't use key material until every check has passed. */
302         from->status.validkey = false;
303
304         if(compression < 0 || compression > 11) {
305                 logger(DEBUG_ALWAYS, LOG_ERR, "Node %s (%s) uses bogus compression level!", from->name, from->hostname);
306                 return true;
307         }
308
309         from->outcompression = compression;
310
311         /* SPTPS or old-style key exchange? */
312
313         char buf[strlen(key)];
314         int len = b64decode(key, buf, strlen(key));
315
316         if(!len || !sptps_receive_data(&from->sptps, buf, len))
317                 logger(DEBUG_ALWAYS, LOG_ERR, "Error processing SPTPS data from %s (%s)", from->name, from->hostname);
318
319         if(from->status.validkey) {
320                 if(*address && *port) {
321                         logger(DEBUG_PROTOCOL, LOG_DEBUG, "Using reflexive UDP address from %s: %s port %s", from->name, address, port);
322                         sockaddr_t sa = str2sockaddr(address, port);
323                         update_node_udp(from, &sa);
324                 }
325
326                 if(from->options & OPTION_PMTU_DISCOVERY && !(from->options & OPTION_TCPONLY))
327                         send_mtu_probe(from);
328         }
329
330         return true;
331 }