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