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