2 protocol_auth.c -- handle the meta-protocol, authentication
3 Copyright (C) 1999-2005 Ivo Timmermans,
4 2000-2010 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"
43 bool send_id(connection_t *c) {
44 gettimeofday(&c->start, NULL);
49 if(c->config_tree && !read_ecdsa_public_key(c))
52 minor = myself->connection->protocol_minor;
55 return send_request(c, "%d %s %d.%d", ID, myself->connection->name, myself->connection->protocol_major, minor);
58 bool id_h(connection_t *c, char *request) {
59 char name[MAX_STRING_SIZE];
61 if(sscanf(request, "%*d " MAX_STRING " %d.%d", name, &c->protocol_major, &c->protocol_minor) < 2) {
62 logger(LOG_ERR, "Got bad %s from %s (%s)", "ID", c->name,
67 /* Check if this is a control connection */
69 if(name[0] == '^' && !strcmp(name + 1, controlcookie)) {
70 c->status.control = true;
71 c->allow_request = CONTROL;
72 c->last_ping_time = time(NULL) + 3600;
73 return send_request(c, "%d %d %d", ACK, TINC_CTL_VERSION_CURRENT, getpid());
76 /* Check if identity is a valid name */
79 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ID", c->name,
80 c->hostname, "invalid name");
84 /* If this is an outgoing connection, make sure we are connected to the right host */
87 if(strcmp(c->name, name)) {
88 logger(LOG_ERR, "Peer %s is %s instead of %s", c->hostname, name,
95 c->name = xstrdup(name);
98 /* Check if version matches */
100 if(c->protocol_major != myself->connection->protocol_major) {
101 logger(LOG_ERR, "Peer %s (%s) uses incompatible version %d.%d",
102 c->name, c->hostname, c->protocol_major, c->protocol_minor);
106 if(bypass_security) {
108 init_configuration(&c->config_tree);
109 c->allow_request = ACK;
113 if(!c->config_tree) {
114 init_configuration(&c->config_tree);
116 if(!read_connection_config(c)) {
117 logger(LOG_ERR, "Peer %s had unknown identity (%s)", c->hostname,
122 if(experimental && c->protocol_minor >= 2)
123 if(!read_ecdsa_public_key(c))
126 if(!ecdsa_active(&c->ecdsa))
127 c->protocol_minor = 1;
131 c->protocol_minor = 0;
133 c->allow_request = METAKEY;
135 if(c->protocol_minor >= 2)
136 return send_metakey_ec(c);
138 return send_metakey(c);
141 bool send_metakey_ec(connection_t *c) {
142 logger(LOG_DEBUG, "Sending ECDH metakey to %s", c->name);
144 size_t siglen = ecdsa_size(&myself->connection->ecdsa);
146 char key[(ECDH_SIZE + siglen) * 2 + 1];
148 // TODO: include nonce? Use relevant parts of SSH or TLS protocol
150 if(!ecdh_generate_public(&c->ecdh, key))
153 if(!ecdsa_sign(&myself->connection->ecdsa, key, ECDH_SIZE, key + ECDH_SIZE))
156 b64encode(key, key, ECDH_SIZE + siglen);
158 return send_request(c, "%d %s", METAKEY, key);
161 bool send_metakey(connection_t *c) {
162 if(!read_rsa_public_key(c))
165 if(!cipher_open_blowfish_ofb(&c->outcipher))
168 if(!digest_open_sha1(&c->outdigest, -1))
171 size_t len = rsa_size(&c->rsa);
174 char hexkey[2 * len + 1];
176 /* Create a random key */
180 /* The message we send must be smaller than the modulus of the RSA key.
181 By definition, for a key of k bits, the following formula holds:
183 2^(k-1) <= modulus < 2^(k)
185 Where ^ means "to the power of", not "xor".
186 This means that to be sure, we must choose our message < 2^(k-1).
187 This can be done by setting the most significant bit to zero.
192 cipher_set_key_from_rsa(&c->outcipher, key, len, true);
194 ifdebug(SCARY_THINGS) {
195 bin2hex(key, hexkey, len);
196 logger(LOG_DEBUG, "Generated random meta key (unencrypted): %s", hexkey);
199 /* Encrypt the random data
201 We do not use one of the PKCS padding schemes here.
202 This is allowed, because we encrypt a totally random string
203 with a length equal to that of the modulus of the RSA key.
206 if(!rsa_public_encrypt(&c->rsa, key, len, enckey)) {
207 logger(LOG_ERR, "Error during encryption of meta key for %s (%s)", c->name, c->hostname);
211 /* Convert the encrypted random data to a hexadecimal formatted string */
213 bin2hex(enckey, hexkey, len);
215 /* Send the meta key */
217 bool result = send_request(c, "%d %d %d %d %d %s", METAKEY,
218 cipher_get_nid(&c->outcipher),
219 digest_get_nid(&c->outdigest), c->outmaclength,
220 c->outcompression, hexkey);
222 c->status.encryptout = true;
226 static bool metakey_ec_h(connection_t *c, const char *request) {
227 size_t siglen = ecdsa_size(&c->ecdsa);
228 char key[MAX_STRING_SIZE];
231 logger(LOG_DEBUG, "Got ECDH metakey from %s", c->name);
233 if(sscanf(request, "%*d " MAX_STRING, key) != 1) {
234 logger(LOG_ERR, "Got bad %s from %s (%s)", "METAKEY", c->name, c->hostname);
238 int inlen = b64decode(key, key, sizeof key);
240 if(inlen != (ECDH_SIZE + siglen)) {
241 logger(LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong keylength");
245 if(!ecdsa_verify(&c->ecdsa, key, ECDH_SIZE, key + ECDH_SIZE)) {
246 logger(LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "invalid ECDSA signature");
250 char shared[ECDH_SHARED_SIZE];
252 if(!ecdh_compute_shared(&c->ecdh, key, shared))
255 /* Update our crypto end */
257 if(!cipher_open_by_name(&c->incipher, "aes-256-ofb"))
259 if(!digest_open_by_name(&c->indigest, "sha512", -1))
261 if(!cipher_open_by_name(&c->outcipher, "aes-256-ofb"))
263 if(!digest_open_by_name(&c->outdigest, "sha512", -1))
266 size_t mykeylen = cipher_keylength(&c->incipher);
267 size_t hiskeylen = cipher_keylength(&c->outcipher);
273 if(strcmp(myself->name, c->name) < 0) {
275 hiskey = key + mykeylen * 2;
276 xasprintf(&seed, "tinc TCP key expansion %s %s", myself->name, c->name);
278 mykey = key + hiskeylen * 2;
280 xasprintf(&seed, "tinc TCP key expansion %s %s", c->name, myself->name);
283 if(!prf(shared, ECDH_SHARED_SIZE, seed, strlen(seed), key, hiskeylen * 2 + mykeylen * 2))
288 cipher_set_key(&c->incipher, mykey, true);
289 digest_set_key(&c->indigest, mykey + mykeylen, mykeylen);
291 cipher_set_key(&c->outcipher, hiskey, false);
292 digest_set_key(&c->outdigest, hiskey + hiskeylen, hiskeylen);
294 c->status.decryptin = true;
295 c->status.encryptout = true;
296 c->allow_request = CHALLENGE;
298 return send_challenge(c);
301 bool metakey_h(connection_t *c, char *request) {
302 if(c->protocol_minor >= 2)
303 return metakey_ec_h(c, request);
305 char hexkey[MAX_STRING_SIZE];
306 int cipher, digest, maclength, compression;
307 size_t len = rsa_size(&myself->connection->rsa);
311 if(sscanf(request, "%*d %d %d %d %d " MAX_STRING, &cipher, &digest, &maclength, &compression, hexkey) != 5) {
312 logger(LOG_ERR, "Got bad %s from %s (%s)", "METAKEY", c->name, c->hostname);
316 /* Convert the challenge from hexadecimal back to binary */
318 int inlen = hex2bin(hexkey, enckey, sizeof enckey);
320 /* Check if the length of the meta key is all right */
323 logger(LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong keylength");
327 /* Decrypt the meta key */
329 if(!rsa_private_decrypt(&myself->connection->rsa, enckey, len, key)) {
330 logger(LOG_ERR, "Error during decryption of meta key for %s (%s)", c->name, c->hostname);
334 ifdebug(SCARY_THINGS) {
335 bin2hex(key, hexkey, len);
336 logger(LOG_DEBUG, "Received random meta key (unencrypted): %s", hexkey);
339 /* Check and lookup cipher and digest algorithms */
341 if(!cipher_open_by_nid(&c->incipher, cipher) || !cipher_set_key_from_rsa(&c->incipher, key, len, false)) {
342 logger(LOG_ERR, "Error during initialisation of cipher from %s (%s)", c->name, c->hostname);
346 if(!digest_open_by_nid(&c->indigest, digest, -1)) {
347 logger(LOG_ERR, "Error during initialisation of digest from %s (%s)", c->name, c->hostname);
351 c->status.decryptin = true;
353 c->allow_request = CHALLENGE;
355 return send_challenge(c);
358 bool send_challenge(connection_t *c) {
359 size_t len = c->protocol_minor >= 2 ? ECDH_SIZE : rsa_size(&c->rsa);
360 char buffer[len * 2 + 1];
363 c->hischallenge = xrealloc(c->hischallenge, len);
365 /* Copy random data to the buffer */
367 randomize(c->hischallenge, len);
371 bin2hex(c->hischallenge, buffer, len);
373 /* Send the challenge */
375 return send_request(c, "%d %s", CHALLENGE, buffer);
378 bool challenge_h(connection_t *c, char *request) {
379 char buffer[MAX_STRING_SIZE];
380 size_t len = c->protocol_minor >= 2 ? ECDH_SIZE : rsa_size(&myself->connection->rsa);
381 size_t digestlen = digest_length(&c->indigest);
382 char digest[digestlen];
384 if(sscanf(request, "%*d " MAX_STRING, buffer) != 1) {
385 logger(LOG_ERR, "Got bad %s from %s (%s)", "CHALLENGE", c->name, c->hostname);
389 /* Convert the challenge from hexadecimal back to binary */
391 int inlen = hex2bin(buffer, buffer, sizeof buffer);
393 /* Check if the length of the challenge is all right */
396 logger(LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge length");
400 c->allow_request = CHAL_REPLY;
402 /* Calculate the hash from the challenge we received */
404 digest_create(&c->indigest, buffer, len, digest);
406 /* Convert the hash to a hexadecimal formatted string */
408 bin2hex(digest, buffer, digestlen);
412 return send_request(c, "%d %s", CHAL_REPLY, buffer);
415 bool chal_reply_h(connection_t *c, char *request) {
416 char hishash[MAX_STRING_SIZE];
418 if(sscanf(request, "%*d " MAX_STRING, hishash) != 1) {
419 logger(LOG_ERR, "Got bad %s from %s (%s)", "CHAL_REPLY", c->name,
424 /* Convert the hash to binary format */
426 int inlen = hex2bin(hishash, hishash, sizeof hishash);
428 /* Check if the length of the hash is all right */
430 if(inlen != digest_length(&c->outdigest)) {
431 logger(LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply length");
436 /* Verify the hash */
438 if(!digest_verify(&c->outdigest, c->hischallenge, c->protocol_minor >= 2 ? ECDH_SIZE : rsa_size(&c->rsa), hishash)) {
439 logger(LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply");
443 /* Identity has now been positively verified.
444 Send an acknowledgement with the rest of the information needed.
447 free(c->hischallenge);
448 c->hischallenge = NULL;
449 c->allow_request = ACK;
454 static bool send_upgrade(connection_t *c) {
455 /* Special case when protocol_minor is 1: the other end is ECDSA capable,
456 * but doesn't know our key yet. So send it now. */
458 char *pubkey = ecdsa_get_base64_public_key(&myself->connection->ecdsa);
463 bool result = send_request(c, "%d %s", ACK, pubkey);
468 bool send_ack(connection_t *c) {
469 if(c->protocol_minor == 1)
470 return send_upgrade(c);
472 /* ACK message contains rest of the information the other end needs
473 to create node_t and edge_t structures. */
478 /* Estimate weight */
480 gettimeofday(&now, NULL);
481 c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
483 /* Check some options */
485 if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &choice) && choice) || myself->options & OPTION_INDIRECT)
486 c->options |= OPTION_INDIRECT;
488 if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &choice) && choice) || myself->options & OPTION_TCPONLY)
489 c->options |= OPTION_TCPONLY | OPTION_INDIRECT;
491 if(myself->options & OPTION_PMTU_DISCOVERY)
492 c->options |= OPTION_PMTU_DISCOVERY;
494 choice = myself->options & OPTION_CLAMP_MSS;
495 get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice);
497 c->options |= OPTION_CLAMP_MSS;
499 get_config_int(lookup_config(c->config_tree, "Weight"), &c->estimated_weight);
501 return send_request(c, "%d %s %d %x", ACK, myport, c->estimated_weight, c->options);
504 static void send_everything(connection_t *c) {
505 splay_node_t *node, *node2;
510 /* Send all known subnets and edges */
513 for(node = myself->subnet_tree->head; node; node = node->next) {
515 send_add_subnet(c, s);
521 for(node = node_tree->head; node; node = node->next) {
524 for(node2 = n->subnet_tree->head; node2; node2 = node2->next) {
526 send_add_subnet(c, s);
529 for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
536 static bool upgrade_h(connection_t *c, char *request) {
537 char pubkey[MAX_STRING_SIZE];
539 if(sscanf(request, "%*d " MAX_STRING, pubkey) != 1) {
540 logger(LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name, c->hostname);
544 if(ecdsa_active(&c->ecdsa) || read_ecdsa_public_key(c)) {
545 logger(LOG_INFO, "Already have ECDSA public key from %s (%s), not upgrading.", c->name, c->hostname);
549 logger(LOG_INFO, "Got ECDSA public key from %s (%s), upgrading!", c->name, c->hostname);
550 append_connection_config(c, "ECDSAPublicKey", pubkey);
551 c->allow_request = TERMREQ;
552 return send_termreq(c);
555 bool ack_h(connection_t *c, char *request) {
556 if(c->protocol_minor == 1)
557 return upgrade_h(c, request);
559 char hisport[MAX_STRING_SIZE];
566 if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &weight, &options) != 3) {
567 logger(LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name,
572 /* Check if we already have a node_t for him */
574 n = lookup_node(c->name);
578 n->name = xstrdup(c->name);
582 /* Oh dear, we already have a connection to this node. */
583 ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Established a second connection with %s (%s), closing old connection", n->connection->name, n->connection->hostname);
585 if(n->connection->outgoing) {
587 logger(LOG_WARNING, "Two outgoing connections to the same node!");
589 c->outgoing = n->connection->outgoing;
591 n->connection->outgoing = NULL;
594 terminate_connection(n->connection, false);
595 /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
602 if(!(c->options & options & OPTION_PMTU_DISCOVERY)) {
603 c->options &= ~OPTION_PMTU_DISCOVERY;
604 options &= ~OPTION_PMTU_DISCOVERY;
606 c->options |= options;
608 if(get_config_int(lookup_config(c->config_tree, "PMTU"), &mtu) && mtu < n->mtu)
611 if(get_config_int(lookup_config(config_tree, "PMTU"), &mtu) && mtu < n->mtu)
614 if(get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice)) {
616 c->options |= OPTION_CLAMP_MSS;
618 c->options &= ~OPTION_CLAMP_MSS;
621 if(c->protocol_minor > 0)
622 c->node->status.ecdh = true;
624 /* Activate this connection */
626 c->allow_request = ALL;
627 c->status.active = true;
629 ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection with %s (%s) activated", c->name,
632 /* Send him everything we know */
636 /* Create an edge_t for this connection */
638 c->edge = new_edge();
639 c->edge->from = myself;
641 sockaddr2str(&c->address, &hisaddress, NULL);
642 c->edge->address = str2sockaddr(hisaddress, hisport);
644 c->edge->weight = (weight + c->estimated_weight) / 2;
645 c->edge->connection = c;
646 c->edge->options = c->options;
650 /* Notify everyone of the new edge */
653 send_add_edge(c, c->edge);
655 send_add_edge(broadcast, c->edge);
657 /* Run MST and SSSP algorithms */