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.
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->config_tree && !read_ecdsa_public_key(c))
139 minor = myself->connection->protocol_minor;
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 = time(NULL) + 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(!cipher_open_blowfish_ofb(&c->outcipher))
252 if(!digest_open_sha1(&c->outdigest, -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 cipher_set_key_from_rsa(&c->outcipher, key, len, true);
278 if(debug_level >= DEBUG_SCARY_THINGS) {
279 bin2hex(key, hexkey, len);
280 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Generated random meta key (unencrypted): %s", hexkey);
283 /* Encrypt the random data
285 We do not use one of the PKCS padding schemes here.
286 This is allowed, because we encrypt a totally random string
287 with a length equal to that of the modulus of the RSA key.
290 if(!rsa_public_encrypt(&c->rsa, key, len, enckey)) {
291 logger(DEBUG_ALWAYS, LOG_ERR, "Error during encryption of meta key for %s (%s)", c->name, c->hostname);
295 /* Convert the encrypted random data to a hexadecimal formatted string */
297 bin2hex(enckey, hexkey, len);
299 /* Send the meta key */
301 bool result = send_request(c, "%d %d %d %d %d %s", METAKEY,
302 cipher_get_nid(&c->outcipher),
303 digest_get_nid(&c->outdigest), c->outmaclength,
304 c->outcompression, hexkey);
306 c->status.encryptout = true;
310 bool metakey_h(connection_t *c, const char *request) {
311 char hexkey[MAX_STRING_SIZE];
312 int cipher, digest, maclength, compression;
313 size_t len = rsa_size(&myself->connection->rsa);
317 if(sscanf(request, "%*d %d %d %d %d " MAX_STRING, &cipher, &digest, &maclength, &compression, hexkey) != 5) {
318 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "METAKEY", c->name, c->hostname);
322 /* Convert the challenge from hexadecimal back to binary */
324 int inlen = hex2bin(hexkey, enckey, sizeof enckey);
326 /* Check if the length of the meta key is all right */
329 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong keylength");
333 /* Decrypt the meta key */
335 if(!rsa_private_decrypt(&myself->connection->rsa, enckey, len, key)) {
336 logger(DEBUG_ALWAYS, LOG_ERR, "Error during decryption of meta key for %s (%s)", c->name, c->hostname);
340 if(debug_level >= DEBUG_SCARY_THINGS) {
341 bin2hex(key, hexkey, len);
342 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Received random meta key (unencrypted): %s", hexkey);
345 /* Check and lookup cipher and digest algorithms */
347 if(!cipher_open_by_nid(&c->incipher, cipher) || !cipher_set_key_from_rsa(&c->incipher, key, len, false)) {
348 logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of cipher from %s (%s)", c->name, c->hostname);
352 if(!digest_open_by_nid(&c->indigest, digest, -1)) {
353 logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of digest from %s (%s)", c->name, c->hostname);
357 c->status.decryptin = true;
359 c->allow_request = CHALLENGE;
361 return send_challenge(c);
364 bool send_challenge(connection_t *c) {
365 size_t len = rsa_size(&c->rsa);
366 char buffer[len * 2 + 1];
369 c->hischallenge = xrealloc(c->hischallenge, len);
371 /* Copy random data to the buffer */
373 randomize(c->hischallenge, len);
377 bin2hex(c->hischallenge, buffer, len);
379 /* Send the challenge */
381 return send_request(c, "%d %s", CHALLENGE, buffer);
384 bool challenge_h(connection_t *c, const char *request) {
385 char buffer[MAX_STRING_SIZE];
386 size_t len = rsa_size(&myself->connection->rsa);
387 size_t digestlen = digest_length(&c->indigest);
388 char digest[digestlen];
390 if(sscanf(request, "%*d " MAX_STRING, buffer) != 1) {
391 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHALLENGE", c->name, c->hostname);
395 /* Convert the challenge from hexadecimal back to binary */
397 int inlen = hex2bin(buffer, buffer, sizeof buffer);
399 /* Check if the length of the challenge is all right */
402 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge length");
406 c->allow_request = CHAL_REPLY;
408 /* Calculate the hash from the challenge we received */
410 digest_create(&c->indigest, buffer, len, digest);
412 /* Convert the hash to a hexadecimal formatted string */
414 bin2hex(digest, buffer, digestlen);
418 return send_request(c, "%d %s", CHAL_REPLY, buffer);
421 bool chal_reply_h(connection_t *c, const char *request) {
422 char hishash[MAX_STRING_SIZE];
424 if(sscanf(request, "%*d " MAX_STRING, hishash) != 1) {
425 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHAL_REPLY", c->name,
430 /* Convert the hash to binary format */
432 int inlen = hex2bin(hishash, hishash, sizeof hishash);
434 /* Check if the length of the hash is all right */
436 if(inlen != digest_length(&c->outdigest)) {
437 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply length");
442 /* Verify the hash */
444 if(!digest_verify(&c->outdigest, c->hischallenge, rsa_size(&c->rsa), hishash)) {
445 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply");
449 /* Identity has now been positively verified.
450 Send an acknowledgement with the rest of the information needed.
453 free(c->hischallenge);
454 c->hischallenge = NULL;
455 c->allow_request = ACK;
460 static bool send_upgrade(connection_t *c) {
461 /* Special case when protocol_minor is 1: the other end is ECDSA capable,
462 * but doesn't know our key yet. So send it now. */
464 char *pubkey = ecdsa_get_base64_public_key(&myself->connection->ecdsa);
469 bool result = send_request(c, "%d %s", ACK, pubkey);
474 bool send_ack(connection_t *c) {
475 if(c->protocol_minor == 1)
476 return send_upgrade(c);
478 /* ACK message contains rest of the information the other end needs
479 to create node_t and edge_t structures. */
484 /* Estimate weight */
486 gettimeofday(&now, NULL);
487 c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
489 /* Check some options */
491 if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &choice) && choice) || myself->options & OPTION_INDIRECT)
492 c->options |= OPTION_INDIRECT;
494 if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &choice) && choice) || myself->options & OPTION_TCPONLY)
495 c->options |= OPTION_TCPONLY | OPTION_INDIRECT;
497 if(myself->options & OPTION_PMTU_DISCOVERY)
498 c->options |= OPTION_PMTU_DISCOVERY;
500 choice = myself->options & OPTION_CLAMP_MSS;
501 get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice);
503 c->options |= OPTION_CLAMP_MSS;
505 get_config_int(lookup_config(c->config_tree, "Weight"), &c->estimated_weight);
507 return send_request(c, "%d %s %d %x", ACK, myport, c->estimated_weight, (c->options & 0xffffff) | (experimental ? (PROT_MINOR << 24) : 0));
510 static void send_everything(connection_t *c) {
511 /* Send all known subnets and edges */
514 for splay_each(subnet_t, s, myself->subnet_tree)
515 send_add_subnet(c, s);
520 for splay_each(node_t, n, node_tree) {
521 for splay_each(subnet_t, s, n->subnet_tree)
522 send_add_subnet(c, s);
524 for splay_each(edge_t, e, n->edge_tree)
529 static bool upgrade_h(connection_t *c, const char *request) {
530 char pubkey[MAX_STRING_SIZE];
532 if(sscanf(request, "%*d " MAX_STRING, pubkey) != 1) {
533 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name, c->hostname);
537 if(ecdsa_active(&c->ecdsa) || read_ecdsa_public_key(c)) {
538 logger(DEBUG_ALWAYS, LOG_INFO, "Already have ECDSA public key from %s (%s), not upgrading.", c->name, c->hostname);
542 logger(DEBUG_ALWAYS, LOG_INFO, "Got ECDSA public key from %s (%s), upgrading!", c->name, c->hostname);
543 append_config_file(c->name, "ECDSAPublicKey", pubkey);
544 c->allow_request = TERMREQ;
545 return send_termreq(c);
548 bool ack_h(connection_t *c, const char *request) {
549 if(c->protocol_minor == 1)
550 return upgrade_h(c, request);
552 char hisport[MAX_STRING_SIZE];
559 if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &weight, &options) != 3) {
560 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name,
565 /* Check if we already have a node_t for him */
567 n = lookup_node(c->name);
571 n->name = xstrdup(c->name);
575 /* Oh dear, we already have a connection to this node. */
576 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Established a second connection with %s (%s), closing old connection", n->connection->name, n->connection->hostname);
578 if(n->connection->outgoing) {
580 logger(DEBUG_ALWAYS, LOG_WARNING, "Two outgoing connections to the same node!");
582 c->outgoing = n->connection->outgoing;
584 n->connection->outgoing = NULL;
587 terminate_connection(n->connection, false);
588 /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
595 if(!(c->options & options & OPTION_PMTU_DISCOVERY)) {
596 c->options &= ~OPTION_PMTU_DISCOVERY;
597 options &= ~OPTION_PMTU_DISCOVERY;
599 c->options |= options;
601 if(get_config_int(lookup_config(c->config_tree, "PMTU"), &mtu) && mtu < n->mtu)
604 if(get_config_int(lookup_config(config_tree, "PMTU"), &mtu) && mtu < n->mtu)
607 if(get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice)) {
609 c->options |= OPTION_CLAMP_MSS;
611 c->options &= ~OPTION_CLAMP_MSS;
614 /* Activate this connection */
616 c->allow_request = ALL;
617 c->status.active = true;
619 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection with %s (%s) activated", c->name,
622 /* Send him everything we know */
626 /* Create an edge_t for this connection */
628 c->edge = new_edge();
629 c->edge->from = myself;
631 sockaddr2str(&c->address, &hisaddress, NULL);
632 c->edge->address = str2sockaddr(hisaddress, hisport);
634 c->edge->weight = (weight + c->estimated_weight) / 2;
635 c->edge->connection = c;
636 c->edge->options = c->options;
640 /* Notify everyone of the new edge */
643 send_add_edge(c, c->edge);
645 send_add_edge(everyone, c->edge);
647 /* Run MST and SSSP algorithms */