2 protocol_auth.c -- handle the meta-protocol, authentication
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2012 Guus Sliepen <guus@tinc-vpn.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "splay_tree.h"
25 #include "connection.h"
27 #include "control_common.h"
45 static bool send_proxyrequest(connection_t *c) {
51 sockaddr2str(&c->address, &host, &port);
52 send_request(c, "CONNECT %s:%s HTTP/1.1\r\n\r", host, port);
58 if(c->address.sa.sa_family != AF_INET) {
59 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot connect to an IPv6 host through a SOCKS 4 proxy!");
62 char s4req[9 + (proxyuser ? strlen(proxyuser) : 0)];
65 memcpy(s4req + 2, &c->address.in.sin_port, 2);
66 memcpy(s4req + 4, &c->address.in.sin_addr, 4);
68 memcpy(s4req + 8, proxyuser, strlen(proxyuser));
69 s4req[sizeof s4req - 1] = 0;
71 return send_meta(c, s4req, sizeof s4req);
74 int len = 3 + 6 + (c->address.sa.sa_family == AF_INET ? 4 : 16);
77 len += 3 + strlen(proxyuser) + strlen(proxypass);
85 s5req[i++] = strlen(proxyuser);
86 memcpy(s5req + i, proxyuser, strlen(proxyuser));
87 i += strlen(proxyuser);
88 s5req[i++] = strlen(proxypass);
89 memcpy(s5req + i, proxypass, strlen(proxypass));
90 i += strlen(proxypass);
98 if(c->address.sa.sa_family == AF_INET) {
100 memcpy(s5req + i, &c->address.in.sin_addr, 4);
102 memcpy(s5req + i, &c->address.in.sin_port, 2);
105 } else if(c->address.sa.sa_family == AF_INET6) {
107 memcpy(s5req + i, &c->address.in6.sin6_addr, 16);
109 memcpy(s5req + i, &c->address.in6.sin6_port, 2);
113 logger(DEBUG_ALWAYS, LOG_ERR, "Address family %hx not supported for SOCKS 5 proxies!", c->address.sa.sa_family);
118 return send_meta(c, s5req, sizeof s5req);
121 logger(DEBUG_ALWAYS, LOG_ERR, "Proxy type not implemented yet");
126 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type");
131 bool send_id(connection_t *c) {
132 gettimeofday(&c->start, NULL);
137 if(c->config_tree && !read_ecdsa_public_key(c))
140 minor = myself->connection->protocol_minor;
144 if(!send_proxyrequest(c))
147 return send_request(c, "%d %s %d.%d", ID, myself->connection->name, myself->connection->protocol_major, minor);
150 bool id_h(connection_t *c, const char *request) {
151 char name[MAX_STRING_SIZE];
153 if(sscanf(request, "%*d " MAX_STRING " %d.%d", name, &c->protocol_major, &c->protocol_minor) < 2) {
154 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ID", c->name,
159 /* Check if this is a control connection */
161 if(name[0] == '^' && !strcmp(name + 1, controlcookie)) {
162 c->status.control = true;
163 c->allow_request = CONTROL;
164 c->last_ping_time = time(NULL) + 3600;
167 c->name = xstrdup("<control>");
169 return send_request(c, "%d %d %d", ACK, TINC_CTL_VERSION_CURRENT, getpid());
172 /* Check if identity is a valid name */
174 if(!check_id(name)) {
175 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ID", c->name,
176 c->hostname, "invalid name");
180 /* If this is an outgoing connection, make sure we are connected to the right host */
183 if(strcmp(c->name, name)) {
184 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s is %s instead of %s", c->hostname, name,
191 c->name = xstrdup(name);
194 /* Check if version matches */
196 if(c->protocol_major != myself->connection->protocol_major) {
197 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) uses incompatible version %d.%d",
198 c->name, c->hostname, c->protocol_major, c->protocol_minor);
202 if(bypass_security) {
204 init_configuration(&c->config_tree);
205 c->allow_request = ACK;
210 c->protocol_minor = 0;
212 if(!c->config_tree) {
213 init_configuration(&c->config_tree);
215 if(!read_host_config(c->config_tree, c->name)) {
216 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s had unknown identity (%s)", c->hostname, c->name);
220 if(experimental && c->protocol_minor >= 2) {
221 if(!read_ecdsa_public_key(c))
225 if(c->protocol_minor && !ecdsa_active(&c->ecdsa))
226 c->protocol_minor = 1;
229 c->allow_request = METAKEY;
231 if(c->protocol_minor >= 2) {
232 c->allow_request = ACK;
233 char label[25 + strlen(myself->name) + strlen(c->name)];
236 snprintf(label, sizeof label, "tinc TCP key expansion %s %s", myself->name, c->name);
238 snprintf(label, sizeof label, "tinc TCP key expansion %s %s", c->name, myself->name);
240 return sptps_start(&c->sptps, c, c->outgoing, false, myself->connection->ecdsa, c->ecdsa, label, sizeof label, send_meta_sptps, receive_meta_sptps);
242 return send_metakey(c);
246 bool send_metakey(connection_t *c) {
247 if(!read_rsa_public_key(c))
250 if(!cipher_open_blowfish_ofb(&c->outcipher))
253 if(!digest_open_sha1(&c->outdigest, -1))
256 size_t len = rsa_size(&c->rsa);
259 char hexkey[2 * len + 1];
261 /* Create a random key */
265 /* The message we send must be smaller than the modulus of the RSA key.
266 By definition, for a key of k bits, the following formula holds:
268 2^(k-1) <= modulus < 2^(k)
270 Where ^ means "to the power of", not "xor".
271 This means that to be sure, we must choose our message < 2^(k-1).
272 This can be done by setting the most significant bit to zero.
277 cipher_set_key_from_rsa(&c->outcipher, key, len, true);
279 if(debug_level >= DEBUG_SCARY_THINGS) {
280 bin2hex(key, hexkey, len);
281 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Generated random meta key (unencrypted): %s", hexkey);
284 /* Encrypt the random data
286 We do not use one of the PKCS padding schemes here.
287 This is allowed, because we encrypt a totally random string
288 with a length equal to that of the modulus of the RSA key.
291 if(!rsa_public_encrypt(&c->rsa, key, len, enckey)) {
292 logger(DEBUG_ALWAYS, LOG_ERR, "Error during encryption of meta key for %s (%s)", c->name, c->hostname);
296 /* Convert the encrypted random data to a hexadecimal formatted string */
298 bin2hex(enckey, hexkey, len);
300 /* Send the meta key */
302 bool result = send_request(c, "%d %d %d %d %d %s", METAKEY,
303 cipher_get_nid(&c->outcipher),
304 digest_get_nid(&c->outdigest), c->outmaclength,
305 c->outcompression, hexkey);
307 c->status.encryptout = true;
311 bool metakey_h(connection_t *c, const char *request) {
312 char hexkey[MAX_STRING_SIZE];
313 int cipher, digest, maclength, compression;
314 size_t len = rsa_size(&myself->connection->rsa);
318 if(sscanf(request, "%*d %d %d %d %d " MAX_STRING, &cipher, &digest, &maclength, &compression, hexkey) != 5) {
319 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "METAKEY", c->name, c->hostname);
323 /* Convert the challenge from hexadecimal back to binary */
325 int inlen = hex2bin(hexkey, enckey, sizeof enckey);
327 /* Check if the length of the meta key is all right */
330 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong keylength");
334 /* Decrypt the meta key */
336 if(!rsa_private_decrypt(&myself->connection->rsa, enckey, len, key)) {
337 logger(DEBUG_ALWAYS, LOG_ERR, "Error during decryption of meta key for %s (%s)", c->name, c->hostname);
341 if(debug_level >= DEBUG_SCARY_THINGS) {
342 bin2hex(key, hexkey, len);
343 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Received random meta key (unencrypted): %s", hexkey);
346 /* Check and lookup cipher and digest algorithms */
348 if(!cipher_open_by_nid(&c->incipher, cipher) || !cipher_set_key_from_rsa(&c->incipher, key, len, false)) {
349 logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of cipher from %s (%s)", c->name, c->hostname);
353 if(!digest_open_by_nid(&c->indigest, digest, -1)) {
354 logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of digest from %s (%s)", c->name, c->hostname);
358 c->status.decryptin = true;
360 c->allow_request = CHALLENGE;
362 return send_challenge(c);
365 bool send_challenge(connection_t *c) {
366 size_t len = rsa_size(&c->rsa);
367 char buffer[len * 2 + 1];
370 c->hischallenge = xrealloc(c->hischallenge, len);
372 /* Copy random data to the buffer */
374 randomize(c->hischallenge, len);
378 bin2hex(c->hischallenge, buffer, len);
380 /* Send the challenge */
382 return send_request(c, "%d %s", CHALLENGE, buffer);
385 bool challenge_h(connection_t *c, const char *request) {
386 char buffer[MAX_STRING_SIZE];
387 size_t len = rsa_size(&myself->connection->rsa);
388 size_t digestlen = digest_length(&c->indigest);
389 char digest[digestlen];
391 if(sscanf(request, "%*d " MAX_STRING, buffer) != 1) {
392 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHALLENGE", c->name, c->hostname);
396 /* Convert the challenge from hexadecimal back to binary */
398 int inlen = hex2bin(buffer, buffer, sizeof buffer);
400 /* Check if the length of the challenge is all right */
403 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge length");
407 c->allow_request = CHAL_REPLY;
409 /* Calculate the hash from the challenge we received */
411 digest_create(&c->indigest, buffer, len, digest);
413 /* Convert the hash to a hexadecimal formatted string */
415 bin2hex(digest, buffer, digestlen);
419 return send_request(c, "%d %s", CHAL_REPLY, buffer);
422 bool chal_reply_h(connection_t *c, const char *request) {
423 char hishash[MAX_STRING_SIZE];
425 if(sscanf(request, "%*d " MAX_STRING, hishash) != 1) {
426 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHAL_REPLY", c->name,
431 /* Convert the hash to binary format */
433 int inlen = hex2bin(hishash, hishash, sizeof hishash);
435 /* Check if the length of the hash is all right */
437 if(inlen != digest_length(&c->outdigest)) {
438 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply length");
443 /* Verify the hash */
445 if(!digest_verify(&c->outdigest, c->hischallenge, rsa_size(&c->rsa), hishash)) {
446 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply");
450 /* Identity has now been positively verified.
451 Send an acknowledgement with the rest of the information needed.
454 free(c->hischallenge);
455 c->hischallenge = NULL;
456 c->allow_request = ACK;
461 static bool send_upgrade(connection_t *c) {
462 /* Special case when protocol_minor is 1: the other end is ECDSA capable,
463 * but doesn't know our key yet. So send it now. */
465 char *pubkey = ecdsa_get_base64_public_key(&myself->connection->ecdsa);
470 bool result = send_request(c, "%d %s", ACK, pubkey);
475 bool send_ack(connection_t *c) {
476 if(c->protocol_minor == 1)
477 return send_upgrade(c);
479 /* ACK message contains rest of the information the other end needs
480 to create node_t and edge_t structures. */
485 /* Estimate weight */
487 gettimeofday(&now, NULL);
488 c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
490 /* Check some options */
492 if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &choice) && choice) || myself->options & OPTION_INDIRECT)
493 c->options |= OPTION_INDIRECT;
495 if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &choice) && choice) || myself->options & OPTION_TCPONLY)
496 c->options |= OPTION_TCPONLY | OPTION_INDIRECT;
498 if(myself->options & OPTION_PMTU_DISCOVERY)
499 c->options |= OPTION_PMTU_DISCOVERY;
501 choice = myself->options & OPTION_CLAMP_MSS;
502 get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice);
504 c->options |= OPTION_CLAMP_MSS;
506 get_config_int(lookup_config(c->config_tree, "Weight"), &c->estimated_weight);
508 return send_request(c, "%d %s %d %x", ACK, myport, c->estimated_weight, (c->options & 0xffffff) | (experimental ? (PROT_MINOR << 24) : 0));
511 static void send_everything(connection_t *c) {
512 splay_node_t *node, *node2;
517 /* Send all known subnets and edges */
520 for(node = myself->subnet_tree->head; node; node = node->next) {
522 send_add_subnet(c, s);
528 for(node = node_tree->head; node; node = node->next) {
531 for(node2 = n->subnet_tree->head; node2; node2 = node2->next) {
533 send_add_subnet(c, s);
536 for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
543 static bool upgrade_h(connection_t *c, const char *request) {
544 char pubkey[MAX_STRING_SIZE];
546 if(sscanf(request, "%*d " MAX_STRING, pubkey) != 1) {
547 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name, c->hostname);
551 if(ecdsa_active(&c->ecdsa) || read_ecdsa_public_key(c)) {
552 logger(DEBUG_ALWAYS, LOG_INFO, "Already have ECDSA public key from %s (%s), not upgrading.", c->name, c->hostname);
556 logger(DEBUG_ALWAYS, LOG_INFO, "Got ECDSA public key from %s (%s), upgrading!", c->name, c->hostname);
557 append_config_file(c->name, "ECDSAPublicKey", pubkey);
558 c->allow_request = TERMREQ;
559 return send_termreq(c);
562 bool ack_h(connection_t *c, const char *request) {
563 if(c->protocol_minor == 1)
564 return upgrade_h(c, request);
566 char hisport[MAX_STRING_SIZE];
573 if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &weight, &options) != 3) {
574 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name,
579 /* Check if we already have a node_t for him */
581 n = lookup_node(c->name);
585 n->name = xstrdup(c->name);
589 /* Oh dear, we already have a connection to this node. */
590 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Established a second connection with %s (%s), closing old connection", n->connection->name, n->connection->hostname);
592 if(n->connection->outgoing) {
594 logger(DEBUG_ALWAYS, LOG_WARNING, "Two outgoing connections to the same node!");
596 c->outgoing = n->connection->outgoing;
598 n->connection->outgoing = NULL;
601 terminate_connection(n->connection, false);
602 /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
609 if(!(c->options & options & OPTION_PMTU_DISCOVERY)) {
610 c->options &= ~OPTION_PMTU_DISCOVERY;
611 options &= ~OPTION_PMTU_DISCOVERY;
613 c->options |= options;
615 if(get_config_int(lookup_config(c->config_tree, "PMTU"), &mtu) && mtu < n->mtu)
618 if(get_config_int(lookup_config(config_tree, "PMTU"), &mtu) && mtu < n->mtu)
621 if(get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice)) {
623 c->options |= OPTION_CLAMP_MSS;
625 c->options &= ~OPTION_CLAMP_MSS;
628 /* Activate this connection */
630 c->allow_request = ALL;
631 c->status.active = true;
633 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection with %s (%s) activated", c->name,
636 /* Send him everything we know */
640 /* Create an edge_t for this connection */
642 c->edge = new_edge();
643 c->edge->from = myself;
645 sockaddr2str(&c->address, &hisaddress, NULL);
646 c->edge->address = str2sockaddr(hisaddress, hisport);
648 c->edge->weight = (weight + c->estimated_weight) / 2;
649 c->edge->connection = c;
650 c->edge->options = c->options;
654 /* Notify everyone of the new edge */
657 send_add_edge(c, c->edge);
659 send_add_edge(everyone, c->edge);
661 /* Run MST and SSSP algorithms */