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