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"
40 bool send_id(connection_t *c) {
41 gettimeofday(&c->start, NULL);
43 return send_request(c, "%d %s %d", ID, myself->connection->name,
44 myself->connection->protocol_version);
47 bool id_h(connection_t *c, char *request) {
48 char name[MAX_STRING_SIZE];
50 if(sscanf(request, "%*d " MAX_STRING " %d", name, &c->protocol_version) != 2) {
51 logger(LOG_ERR, "Got bad %s from %s (%s)", "ID", c->name,
56 /* Check if this is a control connection */
58 if(name[0] == '^' && !strcmp(name + 1, controlcookie)) {
59 c->status.control = true;
60 c->allow_request = CONTROL;
61 c->last_ping_time = time(NULL) + 3600;
62 return send_request(c, "%d %d %d", ACK, TINC_CTL_VERSION_CURRENT, getpid());
65 /* Check if identity is a valid name */
68 logger(LOG_ERR, "Got bad %s from %s (%s): %s", "ID", c->name,
69 c->hostname, "invalid name");
73 /* If this is an outgoing connection, make sure we are connected to the right host */
76 if(strcmp(c->name, name)) {
77 logger(LOG_ERR, "Peer %s is %s instead of %s", c->hostname, name,
84 c->name = xstrdup(name);
87 /* Check if version matches */
89 if(c->protocol_version != myself->connection->protocol_version) {
90 logger(LOG_ERR, "Peer %s (%s) uses incompatible version %d",
91 c->name, c->hostname, c->protocol_version);
97 init_configuration(&c->config_tree);
98 c->allow_request = ACK;
102 if(!c->config_tree) {
103 init_configuration(&c->config_tree);
105 if(!read_connection_config(c)) {
106 logger(LOG_ERR, "Peer %s had unknown identity (%s)", c->hostname,
112 if(!read_rsa_public_key(c)) {
116 c->allow_request = METAKEY;
118 return send_metakey(c);
121 bool send_metakey(connection_t *c) {
122 size_t len = rsa_size(&c->rsa);
125 char hexkey[2 * len + 1];
127 if(!cipher_open_blowfish_ofb(&c->outcipher))
130 if(!digest_open_sha1(&c->outdigest, -1))
133 /* Create a random key */
137 /* The message we send must be smaller than the modulus of the RSA key.
138 By definition, for a key of k bits, the following formula holds:
140 2^(k-1) <= modulus < 2^(k)
142 Where ^ means "to the power of", not "xor".
143 This means that to be sure, we must choose our message < 2^(k-1).
144 This can be done by setting the most significant bit to zero.
149 cipher_set_key_from_rsa(&c->outcipher, key, len, true);
151 ifdebug(SCARY_THINGS) {
152 bin2hex(key, hexkey, len);
153 hexkey[len * 2] = '\0';
154 logger(LOG_DEBUG, "Generated random meta key (unencrypted): %s", hexkey);
157 /* Encrypt the random data
159 We do not use one of the PKCS padding schemes here.
160 This is allowed, because we encrypt a totally random string
161 with a length equal to that of the modulus of the RSA key.
164 if(!rsa_public_encrypt(&c->rsa, key, len, enckey)) {
165 logger(LOG_ERR, "Error during encryption of meta key for %s (%s)", c->name, c->hostname);
169 /* Convert the encrypted random data to a hexadecimal formatted string */
171 bin2hex(enckey, hexkey, len);
172 hexkey[len * 2] = '\0';
174 /* Send the meta key */
176 bool result = send_request(c, "%d %d %d %d %d %s", METAKEY,
177 cipher_get_nid(&c->outcipher),
178 digest_get_nid(&c->outdigest), c->outmaclength,
179 c->outcompression, hexkey);
181 c->status.encryptout = true;
185 bool metakey_h(connection_t *c, char *request) {
186 char hexkey[MAX_STRING_SIZE];
187 int cipher, digest, maclength, compression;
188 size_t len = rsa_size(&myself->connection->rsa);
192 if(sscanf(request, "%*d %d %d %d %d " MAX_STRING, &cipher, &digest, &maclength, &compression, hexkey) != 5) {
193 logger(LOG_ERR, "Got bad %s from %s (%s)", "METAKEY", c->name, c->hostname);
197 /* Check if the length of the meta key is all right */
199 if(strlen(hexkey) != len * 2) {
200 logger(LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong keylength");
204 /* Convert the challenge from hexadecimal back to binary */
206 hex2bin(hexkey, enckey, len);
208 /* Decrypt the meta key */
210 if(!rsa_private_decrypt(&myself->connection->rsa, enckey, len, key)) {
211 logger(LOG_ERR, "Error during decryption of meta key for %s (%s)", c->name, c->hostname);
215 ifdebug(SCARY_THINGS) {
216 bin2hex(key, hexkey, len);
217 hexkey[len * 2] = '\0';
218 logger(LOG_DEBUG, "Received random meta key (unencrypted): %s", hexkey);
221 /* Check and lookup cipher and digest algorithms */
223 if(!cipher_open_by_nid(&c->incipher, cipher) || !cipher_set_key_from_rsa(&c->incipher, key, len, false)) {
224 logger(LOG_ERR, "Error during initialisation of cipher from %s (%s)", c->name, c->hostname);
228 if(!digest_open_by_nid(&c->indigest, digest, -1)) {
229 logger(LOG_ERR, "Error during initialisation of digest from %s (%s)", c->name, c->hostname);
233 c->status.decryptin = true;
235 c->allow_request = CHALLENGE;
237 return send_challenge(c);
240 bool send_challenge(connection_t *c) {
241 size_t len = rsa_size(&c->rsa);
242 char buffer[len * 2 + 1];
245 c->hischallenge = xrealloc(c->hischallenge, len);
247 /* Copy random data to the buffer */
249 randomize(c->hischallenge, len);
253 bin2hex(c->hischallenge, buffer, len);
254 buffer[len * 2] = '\0';
256 /* Send the challenge */
258 return send_request(c, "%d %s", CHALLENGE, buffer);
261 bool challenge_h(connection_t *c, char *request) {
262 char buffer[MAX_STRING_SIZE];
263 size_t len = rsa_size(&myself->connection->rsa);
264 size_t digestlen = digest_length(&c->indigest);
265 char digest[digestlen];
267 if(sscanf(request, "%*d " MAX_STRING, buffer) != 1) {
268 logger(LOG_ERR, "Got bad %s from %s (%s)", "CHALLENGE", c->name, c->hostname);
272 /* Check if the length of the challenge is all right */
274 if(strlen(buffer) != len * 2) {
275 logger(LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge length");
279 /* Convert the challenge from hexadecimal back to binary */
281 hex2bin(buffer, buffer, len);
283 c->allow_request = CHAL_REPLY;
285 /* Calculate the hash from the challenge we received */
287 digest_create(&c->indigest, buffer, len, digest);
289 /* Convert the hash to a hexadecimal formatted string */
291 bin2hex(digest, buffer, digestlen);
292 buffer[digestlen * 2] = '\0';
296 return send_request(c, "%d %s", CHAL_REPLY, buffer);
299 bool chal_reply_h(connection_t *c, char *request) {
300 char hishash[MAX_STRING_SIZE];
302 if(sscanf(request, "%*d " MAX_STRING, hishash) != 1) {
303 logger(LOG_ERR, "Got bad %s from %s (%s)", "CHAL_REPLY", c->name,
308 /* Check if the length of the hash is all right */
310 if(strlen(hishash) != digest_length(&c->outdigest) * 2) {
311 logger(LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply length");
315 /* Convert the hash to binary format */
317 hex2bin(hishash, hishash, digest_length(&c->outdigest));
319 /* Verify the hash */
321 if(!digest_verify(&c->outdigest, c->hischallenge, rsa_size(&c->rsa), hishash)) {
322 logger(LOG_ERR, "Possible intruder %s (%s): %s", c->name, c->hostname, "wrong challenge reply");
326 /* Identity has now been positively verified.
327 Send an acknowledgement with the rest of the information needed.
330 free(c->hischallenge);
331 c->hischallenge = NULL;
332 c->allow_request = ACK;
337 bool send_ack(connection_t *c) {
338 /* ACK message contains rest of the information the other end needs
339 to create node_t and edge_t structures. */
344 /* Estimate weight */
346 gettimeofday(&now, NULL);
347 c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
349 /* Check some options */
351 if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &choice) && choice) || myself->options & OPTION_INDIRECT)
352 c->options |= OPTION_INDIRECT;
354 if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &choice) && choice) || myself->options & OPTION_TCPONLY)
355 c->options |= OPTION_TCPONLY | OPTION_INDIRECT;
357 if(myself->options & OPTION_PMTU_DISCOVERY)
358 c->options |= OPTION_PMTU_DISCOVERY;
360 choice = myself->options & OPTION_CLAMP_MSS;
361 get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice);
363 c->options |= OPTION_CLAMP_MSS;
365 get_config_int(lookup_config(c->config_tree, "Weight"), &c->estimated_weight);
367 return send_request(c, "%d %s %d %x", ACK, myport, c->estimated_weight, c->options);
370 static void send_everything(connection_t *c) {
371 splay_node_t *node, *node2;
376 /* Send all known subnets and edges */
379 for(node = myself->subnet_tree->head; node; node = node->next) {
381 send_add_subnet(c, s);
387 for(node = node_tree->head; node; node = node->next) {
390 for(node2 = n->subnet_tree->head; node2; node2 = node2->next) {
392 send_add_subnet(c, s);
395 for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
402 bool ack_h(connection_t *c, char *request) {
403 char hisport[MAX_STRING_SIZE];
410 if(sscanf(request, "%*d " MAX_STRING " %d %x", hisport, &weight, &options) != 3) {
411 logger(LOG_ERR, "Got bad %s from %s (%s)", "ACK", c->name,
416 /* Check if we already have a node_t for him */
418 n = lookup_node(c->name);
422 n->name = xstrdup(c->name);
426 /* Oh dear, we already have a connection to this node. */
427 ifdebug(CONNECTIONS) logger(LOG_DEBUG, "Established a second connection with %s (%s), closing old connection", n->connection->name, n->connection->hostname);
429 if(n->connection->outgoing) {
431 logger(LOG_WARNING, "Two outgoing connections to the same node!");
433 c->outgoing = n->connection->outgoing;
435 n->connection->outgoing = NULL;
438 terminate_connection(n->connection, false);
439 /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
446 if(!(c->options & options & OPTION_PMTU_DISCOVERY)) {
447 c->options &= ~OPTION_PMTU_DISCOVERY;
448 options &= ~OPTION_PMTU_DISCOVERY;
450 c->options |= options;
452 if(get_config_int(lookup_config(c->config_tree, "PMTU"), &mtu) && mtu < n->mtu)
455 if(get_config_int(lookup_config(config_tree, "PMTU"), &mtu) && mtu < n->mtu)
458 if(get_config_bool(lookup_config(c->config_tree, "ClampMSS"), &choice)) {
460 c->options |= OPTION_CLAMP_MSS;
462 c->options &= ~OPTION_CLAMP_MSS;
465 /* Activate this connection */
467 c->allow_request = ALL;
468 c->status.active = true;
470 ifdebug(CONNECTIONS) logger(LOG_NOTICE, "Connection with %s (%s) activated", c->name,
473 /* Send him everything we know */
477 /* Create an edge_t for this connection */
479 c->edge = new_edge();
480 c->edge->from = myself;
482 sockaddr2str(&c->address, &hisaddress, NULL);
483 c->edge->address = str2sockaddr(hisaddress, hisport);
485 c->edge->weight = (weight + c->estimated_weight) / 2;
486 c->edge->connection = c;
487 c->edge->options = c->options;
491 /* Notify everyone of the new edge */
494 send_add_edge(c, c->edge);
496 send_add_edge(broadcast, c->edge);
498 /* Run MST and SSSP algorithms */