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