2 protocol_auth.c -- handle the meta-protocol, authentication
3 Copyright (C) 1999-2002 Ivo Timmermans <itimmermans@bigfoot.com>,
4 2000-2002 Guus Sliepen <guus@sliepen.warande.net>
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
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 $Id: protocol_auth.c,v 1.1.4.1 2002/02/11 10:05:58 guus Exp $
36 #include <openssl/sha.h>
37 #include <openssl/rand.h>
38 #include <openssl/evp.h>
40 #ifndef HAVE_RAND_PSEUDO_BYTES
41 #define RAND_pseudo_bytes RAND_bytes
49 #include "connection.h"
56 int send_id(connection_t *c)
59 return send_request(c, "%d %s %d", ID, myself->connection->name, myself->connection->protocol_version);
62 int id_h(connection_t *c)
64 char name[MAX_STRING_SIZE];
67 if(sscanf(c->buffer, "%*d "MAX_STRING" %d", name, &c->protocol_version) != 2)
69 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ID", c->name, c->hostname);
73 /* Check if identity is a valid name */
77 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ID", c->name, c->hostname, "invalid name");
81 /* If we set c->name in advance, make sure we are connected to the right host */
85 if(strcmp(c->name, name))
87 syslog(LOG_ERR, _("Peer %s is %s instead of %s"), c->hostname, name, c->name);
92 c->name = xstrdup(name);
94 /* Check if version matches */
96 if(c->protocol_version != myself->connection->protocol_version)
98 syslog(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
99 c->name, c->hostname, c->protocol_version);
106 init_configuration(&c->config_tree);
107 c->allow_request = ACK;
113 init_configuration(&c->config_tree);
115 if((bla = read_connection_config(c)))
117 syslog(LOG_ERR, _("Peer %s had unknown identity (%s)"), c->hostname, c->name);
122 if(read_rsa_public_key(c))
127 /* Check some options */
129 if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &bla) && bla) || myself->options & OPTION_INDIRECT)
130 c->options |= OPTION_INDIRECT;
132 if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &bla) && bla) || myself->options & OPTION_TCPONLY)
133 c->options |= OPTION_TCPONLY | OPTION_INDIRECT;
135 c->allow_request = METAKEY;
137 return send_metakey(c);
140 int send_metakey(connection_t *c)
142 char buffer[MAX_STRING_SIZE];
145 len = RSA_size(c->rsa_key);
147 /* Allocate buffers for the meta key */
150 c->outkey = xmalloc(len);
153 c->outctx = xmalloc(sizeof(*c->outctx));
155 /* Copy random data to the buffer */
157 RAND_bytes(c->outkey, len);
159 /* The message we send must be smaller than the modulus of the RSA key.
160 By definition, for a key of k bits, the following formula holds:
162 2^(k-1) <= modulus < 2^(k)
164 Where ^ means "to the power of", not "xor".
165 This means that to be sure, we must choose our message < 2^(k-1).
166 This can be done by setting the most significant bit to zero.
169 c->outkey[0] &= 0x7F;
171 if(debug_lvl >= DEBUG_SCARY_THINGS)
173 bin2hex(c->outkey, buffer, len);
174 buffer[len*2] = '\0';
175 syslog(LOG_DEBUG, _("Generated random meta key (unencrypted): %s"), buffer);
178 /* Encrypt the random data
180 We do not use one of the PKCS padding schemes here.
181 This is allowed, because we encrypt a totally random string
182 with a length equal to that of the modulus of the RSA key.
185 if(RSA_public_encrypt(len, c->outkey, buffer, c->rsa_key, RSA_NO_PADDING) != len)
187 syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
191 /* Convert the encrypted random data to a hexadecimal formatted string */
193 bin2hex(buffer, buffer, len);
194 buffer[len*2] = '\0';
196 /* Send the meta key */
198 x = send_request(c, "%d %s", METAKEY, buffer);
200 /* Further outgoing requests are encrypted with the key we just generated */
202 EVP_EncryptInit(c->outctx, EVP_bf_cfb(),
203 c->outkey + len - EVP_bf_cfb()->key_len,
204 c->outkey + len - EVP_bf_cfb()->key_len - EVP_bf_cfb()->iv_len);
206 c->status.encryptout = 1;
211 int metakey_h(connection_t *c)
213 char buffer[MAX_STRING_SIZE];
216 if(sscanf(c->buffer, "%*d "MAX_STRING, buffer) != 1)
218 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "METAKEY", c->name, c->hostname);
222 len = RSA_size(myself->connection->rsa_key);
224 /* Check if the length of the meta key is all right */
226 if(strlen(buffer) != len*2)
228 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong keylength");
232 /* Allocate buffers for the meta key */
235 c->inkey = xmalloc(len);
238 c->inctx = xmalloc(sizeof(*c->inctx));
240 /* Convert the challenge from hexadecimal back to binary */
242 hex2bin(buffer,buffer,len);
244 /* Decrypt the meta key */
246 if(RSA_private_decrypt(len, buffer, c->inkey, myself->connection->rsa_key, RSA_NO_PADDING) != len) /* See challenge() */
248 syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
252 if(debug_lvl >= DEBUG_SCARY_THINGS)
254 bin2hex(c->inkey, buffer, len);
255 buffer[len*2] = '\0';
256 syslog(LOG_DEBUG, _("Received random meta key (unencrypted): %s"), buffer);
259 /* All incoming requests will now be encrypted. */
261 EVP_DecryptInit(c->inctx, EVP_bf_cfb(),
262 c->inkey + len - EVP_bf_cfb()->key_len,
263 c->inkey + len - EVP_bf_cfb()->key_len - EVP_bf_cfb()->iv_len);
265 c->status.decryptin = 1;
267 c->allow_request = CHALLENGE;
269 return send_challenge(c);
272 int send_challenge(connection_t *c)
274 char buffer[MAX_STRING_SIZE];
277 /* CHECKME: what is most reasonable value for len? */
279 len = RSA_size(c->rsa_key);
281 /* Allocate buffers for the challenge */
284 c->hischallenge = xmalloc(len);
286 /* Copy random data to the buffer */
288 RAND_bytes(c->hischallenge, len);
293 bin2hex(c->hischallenge, buffer, len);
294 buffer[len*2] = '\0';
297 /* Send the challenge */
299 x = send_request(c, "%d %s", CHALLENGE, buffer);
304 int challenge_h(connection_t *c)
306 char buffer[MAX_STRING_SIZE];
309 if(sscanf(c->buffer, "%*d "MAX_STRING, buffer) != 1)
311 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "CHALLENGE", c->name, c->hostname);
315 len = RSA_size(myself->connection->rsa_key);
317 /* Check if the length of the challenge is all right */
319 if(strlen(buffer) != len*2)
321 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong challenge length");
325 /* Allocate buffers for the challenge */
328 c->mychallenge = xmalloc(len);
330 /* Convert the challenge from hexadecimal back to binary */
332 hex2bin(buffer,c->mychallenge,len);
334 c->allow_request = CHAL_REPLY;
336 /* Rest is done by send_chal_reply() */
338 return send_chal_reply(c);
341 int send_chal_reply(connection_t *c)
343 char hash[SHA_DIGEST_LENGTH*2+1];
345 /* Calculate the hash from the challenge we received */
347 SHA1(c->mychallenge, RSA_size(myself->connection->rsa_key), hash);
349 /* Convert the hash to a hexadecimal formatted string */
351 bin2hex(hash,hash,SHA_DIGEST_LENGTH);
352 hash[SHA_DIGEST_LENGTH*2] = '\0';
357 return send_request(c, "%d %s", CHAL_REPLY, hash);
360 int chal_reply_h(connection_t *c)
362 char hishash[MAX_STRING_SIZE];
363 char myhash[SHA_DIGEST_LENGTH];
365 if(sscanf(c->buffer, "%*d "MAX_STRING, hishash) != 1)
367 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "CHAL_REPLY", c->name, c->hostname);
371 /* Check if the length of the hash is all right */
373 if(strlen(hishash) != SHA_DIGEST_LENGTH*2)
375 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply length"));
379 /* Convert the hash to binary format */
381 hex2bin(hishash, hishash, SHA_DIGEST_LENGTH);
383 /* Calculate the hash from the challenge we sent */
385 SHA1(c->hischallenge, RSA_size(c->rsa_key), myhash);
387 /* Verify the incoming hash with the calculated hash */
389 if(memcmp(hishash, myhash, SHA_DIGEST_LENGTH))
391 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply"));
392 if(debug_lvl >= DEBUG_SCARY_THINGS)
394 bin2hex(myhash, hishash, SHA_DIGEST_LENGTH);
395 hishash[SHA_DIGEST_LENGTH*2] = '\0';
396 syslog(LOG_DEBUG, _("Expected challenge reply: %s"), hishash);
401 /* Identity has now been positively verified.
402 Send an acknowledgement with the rest of the information needed.
405 c->allow_request = ACK;
410 int send_ack(connection_t *c)
412 /* ACK message contains rest of the information the other end needs
413 to create node_t and edge_t structures. */
419 /* Estimate weight */
421 gettimeofday(&now, NULL);
422 c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
423 addrstr = address2str(c->address);
424 x = send_request(c, "%d %hd %s %d %d", ACK, myself->port, addrstr, c->estimated_weight, c->options);
430 void send_everything(connection_t *c)
432 avl_node_t *node, *node2;
437 /* Send all known subnets */
439 for(node = node_tree->head; node; node = node->next)
441 n = (node_t *)node->data;
443 for(node2 = n->subnet_tree->head; node2; node2 = node2->next)
445 s = (subnet_t *)node2->data;
446 send_add_subnet(c, s);
450 /* Send all known edges */
452 for(node = edge_tree->head; node; node = node->next)
454 e = (edge_t *)node->data;
463 int ack_h(connection_t *c)
466 char addrstr[MAX_STRING_SIZE];
473 if(sscanf(c->buffer, "%*d %hd "MAX_STRING" %d %d", &hisport, addrstr, &weight, &options) != 4)
475 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ACK", c->name, c->hostname);
479 /* Check if we already have a node_t for him */
481 n = lookup_node(c->name);
486 n->name = xstrdup(c->name);
487 n->address = c->address;
488 n->hostname = xstrdup(c->hostname);
491 /* FIXME: Also check if no other tinc daemon uses the same IP and port for UDP traffic */
499 /* Oh dear, we already have a connection to this node. */
500 if(debug_lvl >= DEBUG_CONNECTIONS)
501 syslog(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"), n->name, n->hostname);
502 terminate_connection(n->connection, 0);
505 /* FIXME: check if information in existing node matches that of the other end of this connection */
510 c->options |= options;
512 /* Create an edge_t for this connection */
514 c->edge = new_edge();
516 c->edge->from.node = myself;
517 c->edge->from.address = str2address(addrstr);
518 c->edge->from.port = myself->port;
519 c->edge->to.node = n;
520 c->edge->to.address = c->address;
521 c->edge->to.port = hisport;
522 c->edge->weight = (weight + c->estimated_weight) / 2;
523 c->edge->connection = c;
524 c->edge->options = c->options;
528 /* Activate this connection */
530 c->allow_request = ALL;
531 c->status.active = 1;
533 if(debug_lvl >= DEBUG_CONNECTIONS)
534 syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name, c->hostname);
537 /* Send him everything we know */
541 /* Notify others of this connection */
543 for(node = connection_tree->head; node; node = node->next)
545 other = (connection_t *)node->data;
547 if(other->status.active && other != c)
548 send_add_edge(other, c->edge);
551 /* Run MST and SSSP algorithms */
555 /* Succesful connection, reset timeout if this is an outgoing connection. */
558 c->outgoing->timeout = 0;