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_connection_config(c)) {
216 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s had unknown identity (%s)", c->hostname,
221 if(experimental && c->protocol_minor >= 2) {
222 if(!read_ecdsa_public_key(c))
226 if(c->protocol_minor && !ecdsa_active(&c->ecdsa))
227 c->protocol_minor = 1;
230 c->allow_request = METAKEY;
232 if(c->protocol_minor >= 2) {
233 c->allow_request = ACK;
234 char label[25 + strlen(myself->name) + strlen(c->name)];
237 snprintf(label, sizeof label, "tinc TCP key expansion %s %s", myself->name, c->name);
239 snprintf(label, sizeof label, "tinc TCP key expansion %s %s", c->name, myself->name);
241 return sptps_start(&c->sptps, c, c->outgoing, false, myself->connection->ecdsa, c->ecdsa, label, sizeof label, send_meta_sptps, receive_meta_sptps);
243 return send_metakey(c);
247 bool send_metakey(connection_t *c) {
248 if(!read_rsa_public_key(c))
251 if(!cipher_open_blowfish_ofb(&c->outcipher))
254 if(!digest_open_sha1(&c->outdigest, -1))
257 size_t len = rsa_size(&c->rsa);
260 char hexkey[2 * len + 1];
262 /* Create a random key */
266 /* The message we send must be smaller than the modulus of the RSA key.
267 By definition, for a key of k bits, the following formula holds:
269 2^(k-1) <= modulus < 2^(k)
271 Where ^ means "to the power of", not "xor".
272 This means that to be sure, we must choose our message < 2^(k-1).
273 This can be done by setting the most significant bit to zero.
278 cipher_set_key_from_rsa(&c->outcipher, key, len, true);
280 if(debug_level >= DEBUG_SCARY_THINGS) {
281 bin2hex(key, hexkey, len);
282 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Generated random meta key (unencrypted): %s", hexkey);
285 /* Encrypt the random data
287 We do not use one of the PKCS padding schemes here.
288 This is allowed, because we encrypt a totally random string
289 with a length equal to that of the modulus of the RSA key.
292 if(!rsa_public_encrypt(&c->rsa, key, len, enckey)) {
293 logger(DEBUG_ALWAYS, LOG_ERR, "Error during encryption of meta key for %s (%s)", c->name, c->hostname);
297 /* Convert the encrypted random data to a hexadecimal formatted string */
299 bin2hex(enckey, hexkey, len);
301 /* Send the meta key */
303 bool result = send_request(c, "%d %d %d %d %d %s", METAKEY,
304 cipher_get_nid(&c->outcipher),
305 digest_get_nid(&c->outdigest), c->outmaclength,
306 c->outcompression, hexkey);
308 c->status.encryptout = true;
312 bool metakey_h(connection_t *c, const char *request) {
313 char hexkey[MAX_STRING_SIZE];
314 int cipher, digest, maclength, compression;
315 size_t len = rsa_size(&myself->connection->rsa);
319 if(sscanf(request, "%*d %d %d %d %d " MAX_STRING, &cipher, &digest, &maclength, &compression, hexkey) != 5) {
320 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "METAKEY", c->name, c->hostname);
324 /* Convert the challenge from hexadecimal back to binary */
326 int inlen = hex2bin(hexkey, enckey, sizeof enckey);
328 /* Check if the length of the meta key is all right */
331 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong keylength");
335 /* Decrypt the meta key */
337 if(!rsa_private_decrypt(&myself->connection->rsa, enckey, len, key)) {
338 logger(DEBUG_ALWAYS, LOG_ERR, "Error during decryption of meta key for %s (%s)", c->name, c->hostname);
342 if(debug_level >= DEBUG_SCARY_THINGS) {
343 bin2hex(key, hexkey, len);
344 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Received random meta key (unencrypted): %s", hexkey);
347 /* Check and lookup cipher and digest algorithms */
349 if(!cipher_open_by_nid(&c->incipher, cipher) || !cipher_set_key_from_rsa(&c->incipher, key, len, false)) {
350 logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of cipher from %s (%s)", c->name, c->hostname);
354 if(!digest_open_by_nid(&c->indigest, digest, -1)) {
355 logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of digest from %s (%s)", c->name, c->hostname);
359 c->status.decryptin = true;
361 c->allow_request = CHALLENGE;
363 return send_challenge(c);
366 bool send_challenge(connection_t *c) {
367 size_t len = rsa_size(&c->rsa);
368 char buffer[len * 2 + 1];
371 c->hischallenge = xrealloc(c->hischallenge, len);
373 /* Copy random data to the buffer */
375 randomize(c->hischallenge, len);
379 bin2hex(c->hischallenge, buffer, len);
381 /* Send the challenge */
383 return send_request(c, "%d %s", CHALLENGE, buffer);
386 bool challenge_h(connection_t *c, const char *request) {
387 char buffer[MAX_STRING_SIZE];
388 size_t len = rsa_size(&myself->connection->rsa);
389 size_t digestlen = digest_length(&c->indigest);
390 char digest[digestlen];
392 if(sscanf(request, "%*d " MAX_STRING, buffer) != 1) {
393 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHALLENGE", c->name, c->hostname);
397 /* Convert the challenge from hexadecimal back to binary */
399 int inlen = hex2bin(buffer, buffer, sizeof buffer);
401 /* Check if the length of the challenge is all right */
404 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge length");
408 c->allow_request = CHAL_REPLY;
410 /* Calculate the hash from the challenge we received */
412 digest_create(&c->indigest, buffer, len, digest);
414 /* Convert the hash to a hexadecimal formatted string */
416 bin2hex(digest, buffer, digestlen);
420 return send_request(c, "%d %s", CHAL_REPLY, buffer);
423 bool chal_reply_h(connection_t *c, const char *request) {
424 char hishash[MAX_STRING_SIZE];
426 if(sscanf(request, "%*d " MAX_STRING, hishash) != 1) {
427 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHAL_REPLY", c->name,
432 /* Convert the hash to binary format */
434 int inlen = hex2bin(hishash, hishash, sizeof hishash);
436 /* Check if the length of the hash is all right */
438 if(inlen != digest_length(&c->outdigest)) {
439 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply length");
444 /* Verify the hash */
446 if(!digest_verify(&c->outdigest, c->hischallenge, rsa_size(&c->rsa), hishash)) {
447 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply");
451 /* Identity has now been positively verified.
452 Send an acknowledgement with the rest of the information needed.
455 free(c->hischallenge);
456 c->hischallenge = NULL;
457 c->allow_request = ACK;
462 static bool send_upgrade(connection_t *c) {
463 /* Special case when protocol_minor is 1: the other end is ECDSA capable,
464 * but doesn't know our key yet. So send it now. */
466 char *pubkey = ecdsa_get_base64_public_key(&myself->connection->ecdsa);
471 bool result = send_request(c, "%d %s", ACK, pubkey);
476 bool send_ack(connection_t *c) {
477 if(c->protocol_minor == 1)
478 return send_upgrade(c);
480 /* ACK message contains rest of the information the other end needs
481 to create node_t and edge_t structures. */
486 /* Estimate weight */
488 gettimeofday(&now, NULL);
489 c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
491 /* Check some options */
493 if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &choice) && choice) || myself->options & OPTION_INDIRECT)
494 c->options |= OPTION_INDIRECT;
496 if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &choice) && choice) || myself->options & OPTION_TCPONLY)
497 c->options |= OPTION_TCPONLY | OPTION_INDIRECT;
499 if(myself->options & OPTION_PMTU_DISCOVERY)
500 c->options |= OPTION_PMTU_DISCOVERY;
502 choice = myself->options & OPTION_CLAMP_MSS;
503 get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice);
505 c->options |= OPTION_CLAMP_MSS;
507 get_config_int(lookup_config(c->config_tree, "Weight"), &c->estimated_weight);
509 return send_request(c, "%d %s %d %x", ACK, myport, c->estimated_weight, (c->options & 0xffffff) | (PROT_MINOR << 24));
512 static void send_everything(connection_t *c) {
513 splay_node_t *node, *node2;
518 /* Send all known subnets and edges */
521 for(node = myself->subnet_tree->head; node; node = node->next) {
523 send_add_subnet(c, s);
529 for(node = node_tree->head; node; node = node->next) {
532 for(node2 = n->subnet_tree->head; node2; node2 = node2->next) {
534 send_add_subnet(c, s);
537 for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
544 static bool upgrade_h(connection_t *c, const char *request) {
545 char pubkey[MAX_STRING_SIZE];
547 if(sscanf(request, "%*d " MAX_STRING, pubkey) != 1) {
548 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name, c->hostname);
552 if(ecdsa_active(&c->ecdsa) || read_ecdsa_public_key(c)) {
553 logger(DEBUG_ALWAYS, LOG_INFO, "Already have ECDSA public key from %s (%s), not upgrading.", c->name, c->hostname);
557 logger(DEBUG_ALWAYS, LOG_INFO, "Got ECDSA public key from %s (%s), upgrading!", c->name, c->hostname);
558 append_config_file(c->name, "ECDSAPublicKey", pubkey);
559 c->allow_request = TERMREQ;
560 return send_termreq(c);
563 bool ack_h(connection_t *c, const char *request) {
564 if(c->protocol_minor == 1)
565 return upgrade_h(c, request);
567 char hisport[MAX_STRING_SIZE];
574 if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &weight, &options) != 3) {
575 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name,
580 /* Check if we already have a node_t for him */
582 n = lookup_node(c->name);
586 n->name = xstrdup(c->name);
590 /* Oh dear, we already have a connection to this node. */
591 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Established a second connection with %s (%s), closing old connection", n->connection->name, n->connection->hostname);
593 if(n->connection->outgoing) {
595 logger(DEBUG_ALWAYS, LOG_WARNING, "Two outgoing connections to the same node!");
597 c->outgoing = n->connection->outgoing;
599 n->connection->outgoing = NULL;
602 terminate_connection(n->connection, false);
603 /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
610 if(!(c->options & options & OPTION_PMTU_DISCOVERY)) {
611 c->options &= ~OPTION_PMTU_DISCOVERY;
612 options &= ~OPTION_PMTU_DISCOVERY;
614 c->options |= options;
616 if(get_config_int(lookup_config(c->config_tree, "PMTU"), &mtu) && mtu < n->mtu)
619 if(get_config_int(lookup_config(config_tree, "PMTU"), &mtu) && mtu < n->mtu)
622 if(get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice)) {
624 c->options |= OPTION_CLAMP_MSS;
626 c->options &= ~OPTION_CLAMP_MSS;
629 /* Activate this connection */
631 c->allow_request = ALL;
632 c->status.active = true;
634 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection with %s (%s) activated", c->name,
637 /* Send him everything we know */
641 /* Create an edge_t for this connection */
643 c->edge = new_edge();
644 c->edge->from = myself;
646 sockaddr2str(&c->address, &hisaddress, NULL);
647 c->edge->address = str2sockaddr(hisaddress, hisport);
649 c->edge->weight = (weight + c->estimated_weight) / 2;
650 c->edge->connection = c;
651 c->edge->options = c->options;
655 /* Notify everyone of the new edge */
658 send_add_edge(c, c->edge);
660 send_add_edge(everyone, c->edge);
662 /* Run MST and SSSP algorithms */