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 bool send_id(connection_t *c) {
46 gettimeofday(&c->start, NULL);
51 if(c->config_tree && !read_ecdsa_public_key(c))
54 minor = myself->connection->protocol_minor;
57 return send_request(c, "%d %s %d.%d", ID, myself->connection->name, myself->connection->protocol_major, minor);
60 bool id_h(connection_t *c, char *request) {
61 char name[MAX_STRING_SIZE];
63 if(sscanf(request, "%*d " MAX_STRING " %d.%d", name, &c->protocol_major, &c->protocol_minor) < 2) {
64 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ID", c->name,
69 /* Check if this is a control connection */
71 if(name[0] == '^' && !strcmp(name + 1, controlcookie)) {
72 c->status.control = true;
73 c->allow_request = CONTROL;
74 c->last_ping_time = time(NULL) + 3600;
77 c->name = xstrdup("<control>");
79 return send_request(c, "%d %d %d", ACK, TINC_CTL_VERSION_CURRENT, getpid());
82 /* Check if identity is a valid name */
85 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s): %s", "ID", c->name,
86 c->hostname, "invalid name");
90 /* If this is an outgoing connection, make sure we are connected to the right host */
93 if(strcmp(c->name, name)) {
94 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s is %s instead of %s", c->hostname, name,
101 c->name = xstrdup(name);
104 /* Check if version matches */
106 if(c->protocol_major != myself->connection->protocol_major) {
107 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s (%s) uses incompatible version %d.%d",
108 c->name, c->hostname, c->protocol_major, c->protocol_minor);
112 if(bypass_security) {
114 init_configuration(&c->config_tree);
115 c->allow_request = ACK;
120 c->protocol_minor = 0;
122 if(!c->config_tree) {
123 init_configuration(&c->config_tree);
125 if(!read_connection_config(c)) {
126 logger(DEBUG_ALWAYS, LOG_ERR, "Peer %s had unknown identity (%s)", c->hostname,
131 if(experimental && c->protocol_minor >= 2) {
132 if(!read_ecdsa_public_key(c))
136 if(c->protocol_minor && !ecdsa_active(&c->ecdsa))
137 c->protocol_minor = 1;
140 c->allow_request = METAKEY;
142 if(c->protocol_minor >= 2) {
143 c->allow_request = ACK;
144 char label[25 + strlen(myself->name) + strlen(c->name)];
147 snprintf(label, sizeof label, "tinc TCP key expansion %s %s", myself->name, c->name);
149 snprintf(label, sizeof label, "tinc TCP key expansion %s %s", c->name, myself->name);
151 return sptps_start(&c->sptps, c, c->outgoing, false, myself->connection->ecdsa, c->ecdsa, label, sizeof label, send_meta_sptps, receive_meta_sptps);
153 return send_metakey(c);
157 bool send_metakey(connection_t *c) {
158 if(!read_rsa_public_key(c))
161 if(!cipher_open_blowfish_ofb(&c->outcipher))
164 if(!digest_open_sha1(&c->outdigest, -1))
167 size_t len = rsa_size(&c->rsa);
170 char hexkey[2 * len + 1];
172 /* Create a random key */
176 /* The message we send must be smaller than the modulus of the RSA key.
177 By definition, for a key of k bits, the following formula holds:
179 2^(k-1) <= modulus < 2^(k)
181 Where ^ means "to the power of", not "xor".
182 This means that to be sure, we must choose our message < 2^(k-1).
183 This can be done by setting the most significant bit to zero.
188 cipher_set_key_from_rsa(&c->outcipher, key, len, true);
190 if(debug_level >= DEBUG_SCARY_THINGS) {
191 bin2hex(key, hexkey, len);
192 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Generated random meta key (unencrypted): %s", hexkey);
195 /* Encrypt the random data
197 We do not use one of the PKCS padding schemes here.
198 This is allowed, because we encrypt a totally random string
199 with a length equal to that of the modulus of the RSA key.
202 if(!rsa_public_encrypt(&c->rsa, key, len, enckey)) {
203 logger(DEBUG_ALWAYS, LOG_ERR, "Error during encryption of meta key for %s (%s)", c->name, c->hostname);
207 /* Convert the encrypted random data to a hexadecimal formatted string */
209 bin2hex(enckey, hexkey, len);
211 /* Send the meta key */
213 bool result = send_request(c, "%d %d %d %d %d %s", METAKEY,
214 cipher_get_nid(&c->outcipher),
215 digest_get_nid(&c->outdigest), c->outmaclength,
216 c->outcompression, hexkey);
218 c->status.encryptout = true;
222 bool metakey_h(connection_t *c, char *request) {
223 char hexkey[MAX_STRING_SIZE];
224 int cipher, digest, maclength, compression;
225 size_t len = rsa_size(&myself->connection->rsa);
229 if(sscanf(request, "%*d %d %d %d %d " MAX_STRING, &cipher, &digest, &maclength, &compression, hexkey) != 5) {
230 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "METAKEY", c->name, c->hostname);
234 /* Convert the challenge from hexadecimal back to binary */
236 int inlen = hex2bin(hexkey, enckey, sizeof enckey);
238 /* Check if the length of the meta key is all right */
241 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong keylength");
245 /* Decrypt the meta key */
247 if(!rsa_private_decrypt(&myself->connection->rsa, enckey, len, key)) {
248 logger(DEBUG_ALWAYS, LOG_ERR, "Error during decryption of meta key for %s (%s)", c->name, c->hostname);
252 if(debug_level >= DEBUG_SCARY_THINGS) {
253 bin2hex(key, hexkey, len);
254 logger(DEBUG_SCARY_THINGS, LOG_DEBUG, "Received random meta key (unencrypted): %s", hexkey);
257 /* Check and lookup cipher and digest algorithms */
259 if(!cipher_open_by_nid(&c->incipher, cipher) || !cipher_set_key_from_rsa(&c->incipher, key, len, false)) {
260 logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of cipher from %s (%s)", c->name, c->hostname);
264 if(!digest_open_by_nid(&c->indigest, digest, -1)) {
265 logger(DEBUG_ALWAYS, LOG_ERR, "Error during initialisation of digest from %s (%s)", c->name, c->hostname);
269 c->status.decryptin = true;
271 c->allow_request = CHALLENGE;
273 return send_challenge(c);
276 bool send_challenge(connection_t *c) {
277 size_t len = rsa_size(&c->rsa);
278 char buffer[len * 2 + 1];
281 c->hischallenge = xrealloc(c->hischallenge, len);
283 /* Copy random data to the buffer */
285 randomize(c->hischallenge, len);
289 bin2hex(c->hischallenge, buffer, len);
291 /* Send the challenge */
293 return send_request(c, "%d %s", CHALLENGE, buffer);
296 bool challenge_h(connection_t *c, char *request) {
297 char buffer[MAX_STRING_SIZE];
298 size_t len = rsa_size(&myself->connection->rsa);
299 size_t digestlen = digest_length(&c->indigest);
300 char digest[digestlen];
302 if(sscanf(request, "%*d " MAX_STRING, buffer) != 1) {
303 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHALLENGE", c->name, c->hostname);
307 /* Convert the challenge from hexadecimal back to binary */
309 int inlen = hex2bin(buffer, buffer, sizeof buffer);
311 /* Check if the length of the challenge is all right */
314 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge length");
318 c->allow_request = CHAL_REPLY;
320 /* Calculate the hash from the challenge we received */
322 digest_create(&c->indigest, buffer, len, digest);
324 /* Convert the hash to a hexadecimal formatted string */
326 bin2hex(digest, buffer, digestlen);
330 return send_request(c, "%d %s", CHAL_REPLY, buffer);
333 bool chal_reply_h(connection_t *c, char *request) {
334 char hishash[MAX_STRING_SIZE];
336 if(sscanf(request, "%*d " MAX_STRING, hishash) != 1) {
337 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "CHAL_REPLY", c->name,
342 /* Convert the hash to binary format */
344 int inlen = hex2bin(hishash, hishash, sizeof hishash);
346 /* Check if the length of the hash is all right */
348 if(inlen != digest_length(&c->outdigest)) {
349 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply length");
354 /* Verify the hash */
356 if(!digest_verify(&c->outdigest, c->hischallenge, rsa_size(&c->rsa), hishash)) {
357 logger(DEBUG_ALWAYS, LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply");
361 /* Identity has now been positively verified.
362 Send an acknowledgement with the rest of the information needed.
365 free(c->hischallenge);
366 c->hischallenge = NULL;
367 c->allow_request = ACK;
372 static bool send_upgrade(connection_t *c) {
373 /* Special case when protocol_minor is 1: the other end is ECDSA capable,
374 * but doesn't know our key yet. So send it now. */
376 char *pubkey = ecdsa_get_base64_public_key(&myself->connection->ecdsa);
381 bool result = send_request(c, "%d %s", ACK, pubkey);
386 bool send_ack(connection_t *c) {
387 if(c->protocol_minor == 1)
388 return send_upgrade(c);
390 /* ACK message contains rest of the information the other end needs
391 to create node_t and edge_t structures. */
396 /* Estimate weight */
398 gettimeofday(&now, NULL);
399 c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
401 /* Check some options */
403 if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &choice) && choice) || myself->options & OPTION_INDIRECT)
404 c->options |= OPTION_INDIRECT;
406 if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &choice) && choice) || myself->options & OPTION_TCPONLY)
407 c->options |= OPTION_TCPONLY | OPTION_INDIRECT;
409 if(myself->options & OPTION_PMTU_DISCOVERY)
410 c->options |= OPTION_PMTU_DISCOVERY;
412 choice = myself->options & OPTION_CLAMP_MSS;
413 get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice);
415 c->options |= OPTION_CLAMP_MSS;
417 get_config_int(lookup_config(c->config_tree, "Weight"), &c->estimated_weight);
419 return send_request(c, "%d %s %d %x", ACK, myport, c->estimated_weight, c->options);
422 static void send_everything(connection_t *c) {
423 splay_node_t *node, *node2;
428 /* Send all known subnets and edges */
431 for(node = myself->subnet_tree->head; node; node = node->next) {
433 send_add_subnet(c, s);
439 for(node = node_tree->head; node; node = node->next) {
442 for(node2 = n->subnet_tree->head; node2; node2 = node2->next) {
444 send_add_subnet(c, s);
447 for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
454 static bool upgrade_h(connection_t *c, char *request) {
455 char pubkey[MAX_STRING_SIZE];
457 if(sscanf(request, "%*d " MAX_STRING, pubkey) != 1) {
458 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name, c->hostname);
462 if(ecdsa_active(&c->ecdsa) || read_ecdsa_public_key(c)) {
463 logger(DEBUG_ALWAYS, LOG_INFO, "Already have ECDSA public key from %s (%s), not upgrading.", c->name, c->hostname);
467 logger(DEBUG_ALWAYS, LOG_INFO, "Got ECDSA public key from %s (%s), upgrading!", c->name, c->hostname);
468 append_config_file(c->name, "ECDSAPublicKey", pubkey);
469 c->allow_request = TERMREQ;
470 return send_termreq(c);
473 bool ack_h(connection_t *c, char *request) {
474 if(c->protocol_minor == 1)
475 return upgrade_h(c, request);
477 char hisport[MAX_STRING_SIZE];
484 if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &weight, &options) != 3) {
485 logger(DEBUG_ALWAYS, LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name,
490 /* Check if we already have a node_t for him */
492 n = lookup_node(c->name);
496 n->name = xstrdup(c->name);
500 /* Oh dear, we already have a connection to this node. */
501 logger(DEBUG_CONNECTIONS, LOG_DEBUG, "Established a second connection with %s (%s), closing old connection", n->connection->name, n->connection->hostname);
503 if(n->connection->outgoing) {
505 logger(DEBUG_ALWAYS, LOG_WARNING, "Two outgoing connections to the same node!");
507 c->outgoing = n->connection->outgoing;
509 n->connection->outgoing = NULL;
512 terminate_connection(n->connection, false);
513 /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
520 if(!(c->options & options & OPTION_PMTU_DISCOVERY)) {
521 c->options &= ~OPTION_PMTU_DISCOVERY;
522 options &= ~OPTION_PMTU_DISCOVERY;
524 c->options |= options;
526 if(get_config_int(lookup_config(c->config_tree, "PMTU"), &mtu) && mtu < n->mtu)
529 if(get_config_int(lookup_config(config_tree, "PMTU"), &mtu) && mtu < n->mtu)
532 if(get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice)) {
534 c->options |= OPTION_CLAMP_MSS;
536 c->options &= ~OPTION_CLAMP_MSS;
539 if(c->protocol_minor > 0)
540 c->node->status.ecdh = true;
542 /* Activate this connection */
544 c->allow_request = ALL;
545 c->status.active = true;
547 logger(DEBUG_CONNECTIONS, LOG_NOTICE, "Connection with %s (%s) activated", c->name,
550 /* Send him everything we know */
554 /* Create an edge_t for this connection */
556 c->edge = new_edge();
557 c->edge->from = myself;
559 sockaddr2str(&c->address, &hisaddress, NULL);
560 c->edge->address = str2sockaddr(hisaddress, hisport);
562 c->edge->weight = (weight + c->estimated_weight) / 2;
563 c->edge->connection = c;
564 c->edge->options = c->options;
568 /* Notify everyone of the new edge */
571 send_add_edge(c, c->edge);
573 send_add_edge(everyone, c->edge);
575 /* Run MST and SSSP algorithms */