]> git.meshlink.io Git - meshlink/blob - src/protocol_auth.c
Don't use SPTPS to decrypt the ID request.
[meshlink] / src / protocol_auth.c
1 /*
2     protocol_auth.c -- handle the meta-protocol, authentication
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 "conf.h"
23 #include "connection.h"
24 #include "cipher.h"
25 #include "crypto.h"
26 #include "digest.h"
27 #include "ecdsa.h"
28 #include "edge.h"
29 #include "graph.h"
30 #include "logger.h"
31 #include "meta.h"
32 #include "net.h"
33 #include "netutl.h"
34 #include "node.h"
35 #include "prf.h"
36 #include "protocol.h"
37 #include "sptps.h"
38 #include "utils.h"
39 #include "xalloc.h"
40
41 ecdsa_t *invitation_key = NULL;
42
43 static bool send_proxyrequest(connection_t *c) {
44         switch(proxytype) {
45                 case PROXY_HTTP: {
46                         char *host;
47                         char *port;
48
49                         sockaddr2str(&c->address, &host, &port);
50                         send_request(c, "CONNECT %s:%s HTTP/1.1\r\n\r", host, port);
51                         free(host);
52                         free(port);
53                         return true;
54                 }
55                 case PROXY_SOCKS4: {
56                         if(c->address.sa.sa_family != AF_INET) {
57                                 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot connect to an IPv6 host through a SOCKS 4 proxy!");
58                                 return false;
59                         }
60                         char s4req[9 + (proxyuser ? strlen(proxyuser) : 0)];
61                         s4req[0] = 4;
62                         s4req[1] = 1;
63                         memcpy(s4req + 2, &c->address.in.sin_port, 2);
64                         memcpy(s4req + 4, &c->address.in.sin_addr, 4);
65                         if(proxyuser)
66                                 memcpy(s4req + 8, proxyuser, strlen(proxyuser));
67                         s4req[sizeof s4req - 1] = 0;
68                         c->tcplen = 8;
69                         return send_meta(c, s4req, sizeof s4req);
70                 }
71                 case PROXY_SOCKS5: {
72                         int len = 3 + 6 + (c->address.sa.sa_family == AF_INET ? 4 : 16);
73                         c->tcplen = 2;
74                         if(proxypass)
75                                 len += 3 + strlen(proxyuser) + strlen(proxypass);
76                         char s5req[len];
77                         int i = 0;
78                         s5req[i++] = 5;
79                         s5req[i++] = 1;
80                         if(proxypass) {
81                                 s5req[i++] = 2;
82                                 s5req[i++] = 1;
83                                 s5req[i++] = strlen(proxyuser);
84                                 memcpy(s5req + i, proxyuser, strlen(proxyuser));
85                                 i += strlen(proxyuser);
86                                 s5req[i++] = strlen(proxypass);
87                                 memcpy(s5req + i, proxypass, strlen(proxypass));
88                                 i += strlen(proxypass);
89                                 c->tcplen += 2;
90                         } else {
91                                 s5req[i++] = 0;
92                         }
93                         s5req[i++] = 5;
94                         s5req[i++] = 1;
95                         s5req[i++] = 0;
96                         if(c->address.sa.sa_family == AF_INET) {
97                                 s5req[i++] = 1;
98                                 memcpy(s5req + i, &c->address.in.sin_addr, 4);
99                                 i += 4;
100                                 memcpy(s5req + i, &c->address.in.sin_port, 2);
101                                 i += 2;
102                                 c->tcplen += 10;
103                         } else if(c->address.sa.sa_family == AF_INET6) {
104                                 s5req[i++] = 3;
105                                 memcpy(s5req + i, &c->address.in6.sin6_addr, 16);
106                                 i += 16;
107                                 memcpy(s5req + i, &c->address.in6.sin6_port, 2);
108                                 i += 2;
109                                 c->tcplen += 22;
110                         } else {
111                                 logger(DEBUG_ALWAYS, LOG_ERR, "Address family %hx not supported for SOCKS 5 proxies!", c->address.sa.sa_family);
112                                 return false;
113                         }
114                         if(i > len)
115                                 abort();
116                         return send_meta(c, s5req, sizeof s5req);
117                 }
118                 case PROXY_SOCKS4A:
119                         logger(DEBUG_ALWAYS, LOG_ERR, "Proxy type not implemented yet");
120                         return false;
121                 case PROXY_EXEC:
122                         return true;
123                 default:
124                         logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type");
125                         return false;
126         }
127 }
128
129 bool send_id(connection_t *c) {
130         gettimeofday(&c->start, NULL);
131
132         int minor = myself->connection->protocol_minor;
133
134         if(proxytype && c->outgoing)
135                 if(!send_proxyrequest(c))
136                         return false;
137
138         return send_request(c, "%d %s %d.%d", ID, myself->connection->name, myself->connection->protocol_major, minor);
139 }
140
141 static bool finalize_invitation(connection_t *c, const char *data, uint16_t len) {
142         if(strchr(data, '\n')) {
143                 logger(DEBUG_ALWAYS, LOG_ERR, "Received invalid key from invited node %s (%s)!\n", c->name, c->hostname);
144                 return false;
145         }
146
147         // Create a new host config file
148         char filename[PATH_MAX];
149         snprintf(filename, sizeof filename, "%s" SLASH "hosts" SLASH "%s", confbase, c->name);
150         if(!access(filename, F_OK)) {
151                 logger(DEBUG_ALWAYS, LOG_ERR, "Host config file for %s (%s) already exists!\n", c->name, c->hostname);
152                 return false;
153         }
154
155         FILE *f = fopen(filename, "w");
156         if(!f) {
157                 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to create %s: %s\n", filename, strerror(errno));
158                 return false;
159         }
160
161         fprintf(f, "ECDSAPublicKey = %s\n", data);
162         fclose(f);
163
164         logger(DEBUG_CONNECTIONS, LOG_INFO, "Key succesfully received from %s (%s)", c->name, c->hostname);
165
166         //TODO: callback to application to inform of an accepted invitation
167
168         sptps_send_record(&c->sptps, 2, data, 0);
169         return true;
170 }
171
172 static bool receive_invitation_sptps(void *handle, uint8_t type, const char *data, uint16_t len) {
173         connection_t *c = handle;
174
175         if(type == 128)
176                 return true;
177
178         if(type == 1 && c->status.invitation_used)
179                 return finalize_invitation(c, data, len);
180
181         if(type != 0 || len != 18 || c->status.invitation_used)
182                 return false;
183
184         // Recover the filename from the cookie and the key
185         digest_t *digest = digest_open_by_name("sha256", 18);
186         if(!digest)
187                 abort();
188         char *fingerprint = ecdsa_get_base64_public_key(invitation_key);
189         char hashbuf[18 + strlen(fingerprint)];
190         char cookie[25];
191         memcpy(hashbuf, data, 18);
192         memcpy(hashbuf + 18, fingerprint, sizeof hashbuf - 18);
193         digest_create(digest, hashbuf, sizeof hashbuf, cookie);
194         b64encode_urlsafe(cookie, cookie, 18);
195         digest_close(digest);
196         free(fingerprint);
197
198         char filename[PATH_MAX], usedname[PATH_MAX];
199         snprintf(filename, sizeof filename, "%s" SLASH "invitations" SLASH "%s", confbase, cookie);
200         snprintf(usedname, sizeof usedname, "%s" SLASH "invitations" SLASH "%s.used", confbase, cookie);
201
202         // Atomically rename the invitation file
203         if(rename(filename, usedname)) {
204                 if(errno == ENOENT)
205                         logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s tried to use non-existing invitation %s\n", c->hostname, cookie);
206                 else
207                         logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to rename invitation %s\n", cookie);
208                 return false;
209         }
210
211         // Open the renamed file
212         FILE *f = fopen(usedname, "r");
213         if(!f) {
214                 logger(DEBUG_ALWAYS, LOG_ERR, "Error trying to open invitation %s\n", cookie);
215                 return false;
216         }
217
218         // Read the new node's Name from the file
219         char buf[1024];
220         fgets(buf, sizeof buf, f);
221         if(*buf)
222                 buf[strlen(buf) - 1] = 0;
223
224         len = strcspn(buf, " \t=");
225         char *name = buf + len;
226         name += strspn(name, " \t");
227         if(*name == '=') {
228                 name++;
229                 name += strspn(name, " \t");
230         }
231         buf[len] = 0;
232
233         if(!*buf || !*name || strcasecmp(buf, "Name") || !check_id(name)) {
234                 logger(DEBUG_ALWAYS, LOG_ERR, "Invalid invitation file %s\n", cookie);
235                 fclose(f);
236                 return false;
237         }
238
239         free(c->name);
240         c->name = xstrdup(name);
241
242         // Send the node the contents of the invitation file
243         rewind(f);
244         size_t result;
245         while((result = fread(buf, 1, sizeof buf, f)))
246                 sptps_send_record(&c->sptps, 0, buf, result);
247         sptps_send_record(&c->sptps, 1, buf, 0);
248         fclose(f);
249         unlink(usedname);
250
251         c->status.invitation_used = true;
252
253         logger(DEBUG_CONNECTIONS, LOG_INFO, "Invitation %s succesfully sent to %s (%s)", cookie, c->name, c->hostname);
254         return true;
255 }
256
257 bool id_h(connection_t *c, const char *request) {
258         char name[MAX_STRING_SIZE];
259
260         if(sscanf(request, "%*d " MAX_STRING " %d.%d", name, &c->protocol_major, &c->protocol_minor) < 2) {
261                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ID", c->name,
262                            c->hostname);
263                 return false;
264         }
265
266         /* Check if this is an invitation  */
267
268         if(name[0] == '?') {
269                 if(!invitation_key) {
270                         logger(DEBUG_ALWAYS, LOG_ERR, "Got invitation from %s but we don't have an invitation key", c->hostname);
271                         return false;
272                 }
273
274                 c->ecdsa = ecdsa_set_base64_public_key(name + 1);
275                 if(!c->ecdsa) {
276                         logger(DEBUG_ALWAYS, LOG_ERR, "Got bad invitation from %s", c->hostname);
277                         return false;
278                 }
279
280                 c->status.invitation = true;
281                 char *mykey = ecdsa_get_base64_public_key(invitation_key);
282                 if(!mykey)
283                         return false;
284                 if(!send_request(c, "%d %s", ACK, mykey))
285                         return false;
286                 free(mykey);
287
288                 c->protocol_minor = 2;
289
290                 return sptps_start(&c->sptps, c, false, false, invitation_key, c->ecdsa, "tinc invitation", 15, send_meta_sptps, receive_invitation_sptps);
291         }
292
293         /* Check if identity is a valid name */
294
295         if(!check_id(name)) {
296                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ID", c->name,
297                            c->hostname, "invalid name");
298                 return false;
299         }
300
301         /* If this is an outgoing connection, make sure we are connected to the right host */
302
303         if(c->outgoing) {
304                 if(strcmp(c->name, name)) {
305                         logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s is %s instead of %s", c->hostname, name,
306                                    c->name);
307                         return false;
308                 }
309         } else {
310                 if(c->name)
311                         free(c->name);
312                 c->name = xstrdup(name);
313         }
314
315         /* Check if version matches */
316
317         if(c->protocol_major != myself->connection->protocol_major) {
318                 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) uses incompatible version %d.%d",
319                         c->name, c->hostname, c->protocol_major, c->protocol_minor);
320                 return false;
321         }
322
323         if(bypass_security) {
324                 if(!c->config_tree)
325                         init_configuration(&c->config_tree);
326                 c->allow_request = ACK;
327                 return send_ack(c);
328         }
329
330         if(!c->config_tree) {
331                 init_configuration(&c->config_tree);
332
333                 if(!read_host_config(c->config_tree, c->name)) {
334                         logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s had unknown identity (%s)", c->hostname, c->name);
335                         return false;
336                 }
337
338                 read_ecdsa_public_key(c);
339         } else {
340                 if(c->protocol_minor && !ecdsa_active(c->ecdsa))
341                         c->protocol_minor = 1;
342         }
343
344         /* Forbid version rollback for nodes whose ECDSA key we know */
345
346         if(ecdsa_active(c->ecdsa) && c->protocol_minor < 2) {
347                 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) tries to roll back protocol version to %d.%d",
348                         c->name, c->hostname, c->protocol_major, c->protocol_minor);
349                 return false;
350         }
351
352         c->allow_request = ACK;
353         char label[25 + strlen(myself->name) + strlen(c->name)];
354
355         if(c->outgoing)
356                 snprintf(label, sizeof label, "tinc TCP key expansion %s %s", myself->name, c->name);
357         else
358                 snprintf(label, sizeof label, "tinc TCP key expansion %s %s", c->name, myself->name);
359
360         return sptps_start(&c->sptps, c, c->outgoing, false, myself->connection->ecdsa, c->ecdsa, label, sizeof label, send_meta_sptps, receive_meta_sptps);
361 }
362
363 bool send_ack(connection_t *c) {
364         /* ACK message contains rest of the information the other end needs
365            to create node_t and edge_t structures. */
366
367         struct timeval now;
368         bool choice;
369
370         /* Estimate weight */
371
372         gettimeofday(&now, NULL);
373         c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
374
375         /* Check some options */
376
377         if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &choice) && choice) || myself->options & OPTION_INDIRECT)
378                 c->options |= OPTION_INDIRECT;
379
380         if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &choice) && choice) || myself->options & OPTION_TCPONLY)
381                 c->options |= OPTION_TCPONLY | OPTION_INDIRECT;
382
383         if(myself->options & OPTION_PMTU_DISCOVERY)
384                 c->options |= OPTION_PMTU_DISCOVERY;
385
386         choice = myself->options & OPTION_CLAMP_MSS;
387         get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice);
388         if(choice)
389                 c->options |= OPTION_CLAMP_MSS;
390
391         if(!get_config_int(lookup_config(c->config_tree, "Weight"), &c->estimated_weight))
392                 get_config_int(lookup_config(config_tree, "Weight"), &c->estimated_weight);
393
394         return send_request(c, "%d %s %d %x", ACK, myport, c->estimated_weight, (c->options & 0xffffff) | (PROT_MINOR << 24));
395 }
396
397 static void send_everything(connection_t *c) {
398         /* Send all known subnets and edges */
399
400         // TODO: remove this
401         if(disablebuggypeers) {
402                 static struct {
403                         vpn_packet_t pkt;
404                         char pad[MAXBUFSIZE - MAXSIZE];
405                 } zeropkt;
406
407                 memset(&zeropkt, 0, sizeof zeropkt);
408                 zeropkt.pkt.len = MAXBUFSIZE;
409                 send_tcppacket(c, &zeropkt.pkt);
410         }
411
412         for splay_each(node_t, n, node_tree) {
413                 for splay_each(edge_t, e, n->edge_tree)
414                         send_add_edge(c, e);
415         }
416 }
417
418 bool ack_h(connection_t *c, const char *request) {
419         char hisport[MAX_STRING_SIZE];
420         char *hisaddress;
421         int weight, mtu;
422         uint32_t options;
423         node_t *n;
424         bool choice;
425
426         if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &weight, &options) != 3) {
427                 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name,
428                            c->hostname);
429                 return false;
430         }
431
432         /* Check if we already have a node_t for him */
433
434         n = lookup_node(c->name);
435
436         if(!n) {
437                 n = new_node();
438                 n->name = xstrdup(c->name);
439                 node_add(n);
440         } else {
441                 if(n->connection) {
442                         /* Oh dear, we already have a connection to this node. */
443                         logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Established a second connection with %s (%s), closing old connection", n->connection->name, n->connection->hostname);
444
445                         if(n->connection->outgoing) {
446                                 if(c->outgoing)
447                                         logger(DEBUG_ALWAYS, LOG_WARNING, "Two outgoing connections to the same node!");
448                                 else
449                                         c->outgoing = n->connection->outgoing;
450
451                                 n->connection->outgoing = NULL;
452                         }
453
454                         terminate_connection(n->connection, false);
455                         /* Run graph algorithm to keep things in sync */
456                         graph();
457                 }
458         }
459
460         n->connection = c;
461         c->node = n;
462         if(!(c->options & options & OPTION_PMTU_DISCOVERY)) {
463                 c->options &= ~OPTION_PMTU_DISCOVERY;
464                 options &= ~OPTION_PMTU_DISCOVERY;
465         }
466         c->options |= options;
467
468         if(get_config_int(lookup_config(c->config_tree, "PMTU"), &mtu) && mtu < n->mtu)
469                 n->mtu = mtu;
470
471         if(get_config_int(lookup_config(config_tree, "PMTU"), &mtu) && mtu < n->mtu)
472                 n->mtu = mtu;
473
474         if(get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice)) {
475                 if(choice)
476                         c->options |= OPTION_CLAMP_MSS;
477                 else
478                         c->options &= ~OPTION_CLAMP_MSS;
479         }
480
481         /* Activate this connection */
482
483         c->allow_request = ALL;
484         c->status.active = true;
485
486         logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection with %s (%s) activated", c->name,
487                            c->hostname);
488
489         /* Send him everything we know */
490
491         send_everything(c);
492
493         /* Create an edge_t for this connection */
494
495         c->edge = new_edge();
496         c->edge->from = myself;
497         c->edge->to = n;
498         sockaddr2str(&c->address, &hisaddress, NULL);
499         c->edge->address = str2sockaddr(hisaddress, hisport);
500         free(hisaddress);
501         c->edge->weight = (weight + c->estimated_weight) / 2;
502         c->edge->connection = c;
503         c->edge->options = c->options;
504
505         edge_add(c->edge);
506
507         /* Notify everyone of the new edge */
508
509         send_add_edge(everyone, c->edge);
510
511         /* Run MST and SSSP algorithms */
512
513         graph();
514
515         return true;
516 }