2 protocol_auth.c -- handle the meta-protocol, authentication
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2013 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.
24 #include "connection.h"
26 #include "control_common.h"
44 static bool send_proxyrequest(connection_t *c) {
50 sockaddr2str(&c->address, &host, &port);
51 send_request(c, "CONNECT %s:%s HTTP/1.1\r\n\r", host, port);
57 if(c->address.sa.sa_family != AF_INET) {
58 logger(DEBUG_ALWAYS, LOG_ERR, "Cannot connect to an IPv6 host through a SOCKS 4 proxy!");
61 char s4req[9 + (proxyuser ? strlen(proxyuser) : 0)];
64 memcpy(s4req + 2, &c->address.in.sin_port, 2);
65 memcpy(s4req + 4, &c->address.in.sin_addr, 4);
67 memcpy(s4req + 8, proxyuser, strlen(proxyuser));
68 s4req[sizeof s4req - 1] = 0;
70 return send_meta(c, s4req, sizeof s4req);
73 int len = 3 + 6 + (c->address.sa.sa_family == AF_INET ? 4 : 16);
76 len += 3 + strlen(proxyuser) + strlen(proxypass);
84 s5req[i++] = strlen(proxyuser);
85 memcpy(s5req + i, proxyuser, strlen(proxyuser));
86 i += strlen(proxyuser);
87 s5req[i++] = strlen(proxypass);
88 memcpy(s5req + i, proxypass, strlen(proxypass));
89 i += strlen(proxypass);
97 if(c->address.sa.sa_family == AF_INET) {
99 memcpy(s5req + i, &c->address.in.sin_addr, 4);
101 memcpy(s5req + i, &c->address.in.sin_port, 2);
104 } else if(c->address.sa.sa_family == AF_INET6) {
106 memcpy(s5req + i, &c->address.in6.sin6_addr, 16);
108 memcpy(s5req + i, &c->address.in6.sin6_port, 2);
112 logger(DEBUG_ALWAYS, LOG_ERR, "Address family %hx not supported for SOCKS 5 proxies!", c->address.sa.sa_family);
117 return send_meta(c, s5req, sizeof s5req);
120 logger(DEBUG_ALWAYS, LOG_ERR, "Proxy type not implemented yet");
125 logger(DEBUG_ALWAYS, LOG_ERR, "Unknown proxy type");
130 bool send_id(connection_t *c) {
131 gettimeofday(&c->start, NULL);
136 if(c->outgoing && !read_ecdsa_public_key(c))
139 minor = myself->connection->protocol_minor;
142 if(proxytype && c->outgoing)
143 if(!send_proxyrequest(c))
146 return send_request(c, "%d %s %d.%d", ID, myself->connection->name, myself->connection->protocol_major, minor);
149 bool id_h(connection_t *c, const char *request) {
150 char name[MAX_STRING_SIZE];
152 if(sscanf(request, "%*d " MAX_STRING " %d.%d", name, &c->protocol_major, &c->protocol_minor) < 2) {
153 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ID", c->name,
158 /* Check if this is a control connection */
160 if(name[0] == '^' && !strcmp(name + 1, controlcookie)) {
161 c->status.control = true;
162 c->allow_request = CONTROL;
163 c->last_ping_time = now.tv_sec + 3600;
166 c->name = xstrdup("<control>");
168 return send_request(c, "%d %d %d", ACK, TINC_CTL_VERSION_CURRENT, getpid());
171 /* Check if identity is a valid name */
173 if(!check_id(name)) {
174 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ID", c->name,
175 c->hostname, "invalid name");
179 /* If this is an outgoing connection, make sure we are connected to the right host */
182 if(strcmp(c->name, name)) {
183 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s is %s instead of %s", c->hostname, name,
190 c->name = xstrdup(name);
193 /* Check if version matches */
195 if(c->protocol_major != myself->connection->protocol_major) {
196 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) uses incompatible version %d.%d",
197 c->name, c->hostname, c->protocol_major, c->protocol_minor);
201 if(bypass_security) {
203 init_configuration(&c->config_tree);
204 c->allow_request = ACK;
209 c->protocol_minor = 0;
211 if(!c->config_tree) {
212 init_configuration(&c->config_tree);
214 if(!read_host_config(c->config_tree, c->name)) {
215 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s had unknown identity (%s)", c->hostname, c->name);
219 if(experimental && c->protocol_minor >= 2) {
220 if(!read_ecdsa_public_key(c))
224 if(c->protocol_minor && !ecdsa_active(c->ecdsa))
225 c->protocol_minor = 1;
228 c->allow_request = METAKEY;
230 if(c->protocol_minor >= 2) {
231 c->allow_request = ACK;
232 char label[25 + strlen(myself->name) + strlen(c->name)];
235 snprintf(label, sizeof label, "tinc TCP key expansion %s %s", myself->name, c->name);
237 snprintf(label, sizeof label, "tinc TCP key expansion %s %s", c->name, myself->name);
239 return sptps_start(&c->sptps, c, c->outgoing, false, myself->connection->ecdsa, c->ecdsa, label, sizeof label, send_meta_sptps, receive_meta_sptps);
241 return send_metakey(c);
245 bool send_metakey(connection_t *c) {
246 if(!read_rsa_public_key(c))
249 if(!(c->outcipher = cipher_open_blowfish_ofb()))
252 if(!(c->outdigest = digest_open_sha1(-1)))
255 size_t len = rsa_size(c->rsa);
258 char hexkey[2 * len + 1];
260 /* Create a random key */
264 /* The message we send must be smaller than the modulus of the RSA key.
265 By definition, for a key of k bits, the following formula holds:
267 2^(k-1) <= modulus < 2^(k)
269 Where ^ means "to the power of", not "xor".
270 This means that to be sure, we must choose our message < 2^(k-1).
271 This can be done by setting the most significant bit to zero.
276 if(!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(!(c->incipher = cipher_open_by_nid(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(!(c->indigest = digest_open_by_nid(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 /* Calculate the hash from the challenge we received */
409 if(!digest_create(c->indigest, buffer, len, digest))
412 /* Convert the hash to a hexadecimal formatted string */
414 bin2hex(digest, buffer, digestlen);
418 c->allow_request = CHAL_REPLY;
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) | (experimental ? (PROT_MINOR << 24) : 0));
512 static void send_everything(connection_t *c) {
513 /* Send all known subnets and edges */
515 if(disablebuggypeers) {
518 char pad[MAXBUFSIZE - MAXSIZE];
521 memset(&zeropkt, 0, sizeof zeropkt);
522 zeropkt.pkt.len = MAXBUFSIZE;
523 send_tcppacket(c, &zeropkt.pkt);
527 for splay_each(subnet_t, s, myself->subnet_tree)
528 send_add_subnet(c, s);
533 for splay_each(node_t, n, node_tree) {
534 for splay_each(subnet_t, s, n->subnet_tree)
535 send_add_subnet(c, s);
537 for splay_each(edge_t, e, n->edge_tree)
542 static bool upgrade_h(connection_t *c, const char *request) {
543 char pubkey[MAX_STRING_SIZE];
545 if(sscanf(request, "%*d " MAX_STRING, pubkey) != 1) {
546 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name, c->hostname);
550 if(ecdsa_active(c->ecdsa) || read_ecdsa_public_key(c)) {
551 logger(DEBUG_ALWAYS, LOG_INFO, "Already have ECDSA public key from %s (%s), not upgrading.", c->name, c->hostname);
555 logger(DEBUG_ALWAYS, LOG_INFO, "Got ECDSA public key from %s (%s), upgrading!", c->name, c->hostname);
556 append_config_file(c->name, "ECDSAPublicKey", pubkey);
557 c->allow_request = TERMREQ;
558 return send_termreq(c);
561 bool ack_h(connection_t *c, const char *request) {
562 if(c->protocol_minor == 1)
563 return upgrade_h(c, request);
565 char hisport[MAX_STRING_SIZE];
572 if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &weight, &options) != 3) {
573 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name,
578 /* Check if we already have a node_t for him */
580 n = lookup_node(c->name);
584 n->name = xstrdup(c->name);
588 /* Oh dear, we already have a connection to this node. */
589 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Established a second connection with %s (%s), closing old connection", n->connection->name, n->connection->hostname);
591 if(n->connection->outgoing) {
593 logger(DEBUG_ALWAYS, LOG_WARNING, "Two outgoing connections to the same node!");
595 c->outgoing = n->connection->outgoing;
597 n->connection->outgoing = NULL;
600 terminate_connection(n->connection, false);
601 /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
608 if(!(c->options & options & OPTION_PMTU_DISCOVERY)) {
609 c->options &= ~OPTION_PMTU_DISCOVERY;
610 options &= ~OPTION_PMTU_DISCOVERY;
612 c->options |= options;
614 if(get_config_int(lookup_config(c->config_tree, "PMTU"), &mtu) && mtu < n->mtu)
617 if(get_config_int(lookup_config(config_tree, "PMTU"), &mtu) && mtu < n->mtu)
620 if(get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice)) {
622 c->options |= OPTION_CLAMP_MSS;
624 c->options &= ~OPTION_CLAMP_MSS;
627 /* Activate this connection */
629 c->allow_request = ALL;
630 c->status.active = true;
632 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection with %s (%s) activated", c->name,
635 /* Send him everything we know */
639 /* Create an edge_t for this connection */
641 c->edge = new_edge();
642 c->edge->from = myself;
644 sockaddr2str(&c->address, &hisaddress, NULL);
645 c->edge->address = str2sockaddr(hisaddress, hisport);
647 c->edge->weight = (weight + c->estimated_weight) / 2;
648 c->edge->connection = c;
649 c->edge->options = c->options;
653 /* Notify everyone of the new edge */
656 send_add_edge(c, c->edge);
658 send_add_edge(everyone, c->edge);
660 /* Run MST and SSSP algorithms */