2 protocol_auth.c -- handle the meta-protocol, authentication
3 Copyright (C) 1999-2003 Ivo Timmermans <ivo@o2w.nl>,
4 2000-2003 Guus Sliepen <guus@sliepen.eu.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
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.5 2003/08/24 20:38:27 guus Exp $
25 #include <openssl/sha.h>
26 #include <openssl/rand.h>
27 #include <openssl/evp.h>
31 #include "connection.h"
42 bool send_id(connection_t *c)
46 return send_request(c, "%d %s %d", ID, myself->connection->name,
47 myself->connection->protocol_version);
50 bool id_h(connection_t *c)
52 char name[MAX_STRING_SIZE];
57 if(sscanf(c->buffer, "%*d " MAX_STRING " %d", name, &c->protocol_version) != 2) {
58 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ID", c->name,
63 /* Check if identity is a valid name */
66 logger(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ID", c->name,
67 c->hostname, "invalid name");
71 /* If we set c->name in advance, make sure we are connected to the right host */
74 if(strcmp(c->name, name)) {
75 logger(LOG_ERR, _("Peer %s is %s instead of %s"), c->hostname, name,
80 c->name = xstrdup(name);
82 /* Check if version matches */
84 if(c->protocol_version != myself->connection->protocol_version) {
85 logger(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
86 c->name, c->hostname, c->protocol_version);
92 init_configuration(&c->config_tree);
93 c->allow_request = ACK;
98 init_configuration(&c->config_tree);
100 if(!read_connection_config(c)) {
101 logger(LOG_ERR, _("Peer %s had unknown identity (%s)"), c->hostname,
107 if(!read_rsa_public_key(c)) {
111 /* Check some options */
113 if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &choice) && choice) || myself->options & OPTION_INDIRECT)
114 c->options |= OPTION_INDIRECT;
116 if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &choice) && choice) || myself->options & OPTION_TCPONLY)
117 c->options |= OPTION_TCPONLY | OPTION_INDIRECT;
119 c->allow_request = METAKEY;
121 return send_metakey(c);
124 bool send_metakey(connection_t *c)
126 char buffer[MAX_STRING_SIZE];
132 len = RSA_size(c->rsa_key);
134 /* Allocate buffers for the meta key */
137 c->outkey = xmalloc(len);
140 c->outctx = xmalloc_and_zero(sizeof(*c->outctx));
142 /* Copy random data to the buffer */
144 RAND_bytes(c->outkey, len);
146 /* The message we send must be smaller than the modulus of the RSA key.
147 By definition, for a key of k bits, the following formula holds:
149 2^(k-1) <= modulus < 2^(k)
151 Where ^ means "to the power of", not "xor".
152 This means that to be sure, we must choose our message < 2^(k-1).
153 This can be done by setting the most significant bit to zero.
156 c->outkey[0] &= 0x7F;
158 ifdebug(SCARY_THINGS) {
159 bin2hex(c->outkey, buffer, len);
160 buffer[len * 2] = '\0';
161 logger(LOG_DEBUG, _("Generated random meta key (unencrypted): %s"),
165 /* Encrypt the random data
167 We do not use one of the PKCS padding schemes here.
168 This is allowed, because we encrypt a totally random string
169 with a length equal to that of the modulus of the RSA key.
172 if(RSA_public_encrypt(len, c->outkey, buffer, c->rsa_key, RSA_NO_PADDING) != len) {
173 logger(LOG_ERR, _("Error during encryption of meta key for %s (%s)"),
174 c->name, c->hostname);
178 /* Convert the encrypted random data to a hexadecimal formatted string */
180 bin2hex(buffer, buffer, len);
181 buffer[len * 2] = '\0';
183 /* Send the meta key */
185 x = send_request(c, "%d %d %d %d %d %s", METAKEY,
186 c->outcipher ? c->outcipher->nid : 0,
187 c->outdigest ? c->outdigest->type : 0, c->outmaclength,
188 c->outcompression, buffer);
190 /* Further outgoing requests are encrypted with the key we just generated */
193 EVP_EncryptInit(c->outctx, c->outcipher,
194 c->outkey + len - c->outcipher->key_len,
195 c->outkey + len - c->outcipher->key_len -
196 c->outcipher->iv_len);
198 c->status.encryptout = true;
204 bool metakey_h(connection_t *c)
206 char buffer[MAX_STRING_SIZE];
207 int cipher, digest, maclength, compression;
212 if(sscanf(c->buffer, "%*d %d %d %d %d " MAX_STRING, &cipher, &digest, &maclength, &compression, buffer) != 5) {
213 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "METAKEY", c->name,
218 len = RSA_size(myself->connection->rsa_key);
220 /* Check if the length of the meta key is all right */
222 if(strlen(buffer) != len * 2) {
223 logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong keylength");
227 /* Allocate buffers for the meta key */
230 c->inkey = xmalloc(len);
233 c->inctx = xmalloc_and_zero(sizeof(*c->inctx));
235 /* Convert the challenge from hexadecimal back to binary */
237 hex2bin(buffer, buffer, len);
239 /* Decrypt the meta key */
241 if(RSA_private_decrypt(len, buffer, c->inkey, myself->connection->rsa_key, RSA_NO_PADDING) != len) { /* See challenge() */
242 logger(LOG_ERR, _("Error during encryption of meta key for %s (%s)"),
243 c->name, c->hostname);
247 ifdebug(SCARY_THINGS) {
248 bin2hex(c->inkey, buffer, len);
249 buffer[len * 2] = '\0';
250 logger(LOG_DEBUG, _("Received random meta key (unencrypted): %s"), buffer);
253 /* All incoming requests will now be encrypted. */
255 /* Check and lookup cipher and digest algorithms */
258 c->incipher = EVP_get_cipherbynid(cipher);
261 logger(LOG_ERR, _("%s (%s) uses unknown cipher!"), c->name, c->hostname);
265 EVP_DecryptInit(c->inctx, c->incipher,
266 c->inkey + len - c->incipher->key_len,
267 c->inkey + len - c->incipher->key_len -
268 c->incipher->iv_len);
270 c->status.decryptin = true;
275 c->inmaclength = maclength;
278 c->indigest = EVP_get_digestbynid(digest);
281 logger(LOG_ERR, _("Node %s (%s) uses unknown digest!"), c->name, c->hostname);
285 if(c->inmaclength > c->indigest->md_size || c->inmaclength < 0) {
286 logger(LOG_ERR, _("%s (%s) uses bogus MAC length!"), c->name, c->hostname);
293 c->incompression = compression;
295 c->allow_request = CHALLENGE;
297 return send_challenge(c);
300 bool send_challenge(connection_t *c)
302 char buffer[MAX_STRING_SIZE];
307 /* CHECKME: what is most reasonable value for len? */
309 len = RSA_size(c->rsa_key);
311 /* Allocate buffers for the challenge */
314 c->hischallenge = xmalloc(len);
316 /* Copy random data to the buffer */
318 RAND_bytes(c->hischallenge, len);
322 bin2hex(c->hischallenge, buffer, len);
323 buffer[len * 2] = '\0';
325 /* Send the challenge */
327 return send_request(c, "%d %s", CHALLENGE, buffer);
330 bool challenge_h(connection_t *c)
332 char buffer[MAX_STRING_SIZE];
337 if(sscanf(c->buffer, "%*d " MAX_STRING, buffer) != 1) {
338 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "CHALLENGE", c->name,
343 len = RSA_size(myself->connection->rsa_key);
345 /* Check if the length of the challenge is all right */
347 if(strlen(buffer) != len * 2) {
348 logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name,
349 c->hostname, "wrong challenge length");
353 /* Allocate buffers for the challenge */
356 c->mychallenge = xmalloc(len);
358 /* Convert the challenge from hexadecimal back to binary */
360 hex2bin(buffer, c->mychallenge, len);
362 c->allow_request = CHAL_REPLY;
364 /* Rest is done by send_chal_reply() */
366 return send_chal_reply(c);
369 bool send_chal_reply(connection_t *c)
371 char hash[EVP_MAX_MD_SIZE * 2 + 1];
376 /* Calculate the hash from the challenge we received */
378 EVP_DigestInit(&ctx, c->indigest);
379 EVP_DigestUpdate(&ctx, c->mychallenge,
380 RSA_size(myself->connection->rsa_key));
381 EVP_DigestFinal(&ctx, hash, NULL);
383 /* Convert the hash to a hexadecimal formatted string */
385 bin2hex(hash, hash, c->indigest->md_size);
386 hash[c->indigest->md_size * 2] = '\0';
390 return send_request(c, "%d %s", CHAL_REPLY, hash);
393 bool chal_reply_h(connection_t *c)
395 char hishash[MAX_STRING_SIZE];
396 char myhash[EVP_MAX_MD_SIZE];
401 if(sscanf(c->buffer, "%*d " MAX_STRING, hishash) != 1) {
402 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "CHAL_REPLY", c->name,
407 /* Check if the length of the hash is all right */
409 if(strlen(hishash) != c->outdigest->md_size * 2) {
410 logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name,
411 c->hostname, _("wrong challenge reply length"));
415 /* Convert the hash to binary format */
417 hex2bin(hishash, hishash, c->outdigest->md_size);
419 /* Calculate the hash from the challenge we sent */
421 EVP_DigestInit(&ctx, c->outdigest);
422 EVP_DigestUpdate(&ctx, c->hischallenge, RSA_size(c->rsa_key));
423 EVP_DigestFinal(&ctx, myhash, NULL);
425 /* Verify the incoming hash with the calculated hash */
427 if(memcmp(hishash, myhash, c->outdigest->md_size)) {
428 logger(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name,
429 c->hostname, _("wrong challenge reply"));
431 ifdebug(SCARY_THINGS) {
432 bin2hex(myhash, hishash, SHA_DIGEST_LENGTH);
433 hishash[SHA_DIGEST_LENGTH * 2] = '\0';
434 logger(LOG_DEBUG, _("Expected challenge reply: %s"), hishash);
440 /* Identity has now been positively verified.
441 Send an acknowledgement with the rest of the information needed.
444 c->allow_request = ACK;
449 bool send_ack(connection_t *c)
451 /* ACK message contains rest of the information the other end needs
452 to create node_t and edge_t structures. */
458 /* Estimate weight */
460 gettimeofday(&now, NULL);
461 c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
463 return send_request(c, "%d %s %d %lx", ACK, myport, c->estimated_weight, c->options);
466 static void send_everything(connection_t *c)
468 avl_node_t *node, *node2;
473 /* Send all known subnets and edges */
475 for(node = node_tree->head; node; node = node->next) {
476 n = (node_t *) node->data;
478 for(node2 = n->subnet_tree->head; node2; node2 = node2->next) {
479 s = (subnet_t *) node2->data;
480 send_add_subnet(c, s);
483 for(node2 = n->edge_tree->head; node2; node2 = node2->next) {
484 e = (edge_t *) node2->data;
490 bool ack_h(connection_t *c)
492 char hisport[MAX_STRING_SIZE];
493 char *hisaddress, *dummy;
500 if(sscanf(c->buffer, "%*d " MAX_STRING " %d %lx", hisport, &weight, &options) != 3) {
501 logger(LOG_ERR, _("Got bad %s from %s (%s)"), "ACK", c->name,
506 /* Check if we already have a node_t for him */
508 n = lookup_node(c->name);
512 n->name = xstrdup(c->name);
516 /* Oh dear, we already have a connection to this node. */
517 ifdebug(CONNECTIONS) logger(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"),
518 n->name, n->hostname);
519 terminate_connection(n->connection, false);
520 /* Run graph algorithm to purge key and make sure up/down scripts are rerun with new IP addresses and stuff */
527 c->options |= options;
529 /* Activate this connection */
531 c->allow_request = ALL;
532 c->status.active = true;
534 ifdebug(CONNECTIONS) logger(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name,
537 /* Send him everything we know */
541 /* Create an edge_t for this connection */
543 c->edge = new_edge();
545 c->edge->from = myself;
547 sockaddr2str(&c->address, &hisaddress, &dummy);
548 c->edge->address = str2sockaddr(hisaddress, hisport);
551 c->edge->weight = (weight + c->estimated_weight) / 2;
552 c->edge->connection = c;
553 c->edge->options = c->options;
557 /* Notify everyone of the new edge */
559 send_add_edge(broadcast, c->edge);
561 /* Run MST and SSSP algorithms */