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.4 2002/04/28 12:46:26 zarq Exp $
36 #include <openssl/sha.h>
37 #include <openssl/rand.h>
38 #include <openssl/evp.h>
45 #ifndef HAVE_RAND_PSEUDO_BYTES
46 #define RAND_pseudo_bytes RAND_bytes
54 #include "connection.h"
62 int send_id(connection_t *c)
65 return send_request(c, "%d %s %d", ID, myself->connection->name, myself->connection->protocol_version);
68 int id_h(connection_t *c)
70 char name[MAX_STRING_SIZE];
73 if(sscanf(c->buffer, "%*d "MAX_STRING" %d", name, &c->protocol_version) != 2)
75 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ID", c->name, c->hostname);
79 /* Check if identity is a valid name */
83 syslog(LOG_ERR, _("Got bad %s from %s (%s): %s"), "ID", c->name, c->hostname, "invalid name");
87 /* If we set c->name in advance, make sure we are connected to the right host */
91 if(strcmp(c->name, name))
93 syslog(LOG_ERR, _("Peer %s is %s instead of %s"), c->hostname, name, c->name);
98 c->name = xstrdup(name);
100 /* Check if version matches */
102 if(c->protocol_version != myself->connection->protocol_version)
104 syslog(LOG_ERR, _("Peer %s (%s) uses incompatible version %d"),
105 c->name, c->hostname, c->protocol_version);
112 init_configuration(&c->config_tree);
113 c->allow_request = ACK;
119 init_configuration(&c->config_tree);
121 if((bla = read_connection_config(c)))
123 syslog(LOG_ERR, _("Peer %s had unknown identity (%s)"), c->hostname, c->name);
128 if(read_rsa_public_key(c))
133 /* Check some options */
135 if((get_config_bool(lookup_config(c->config_tree, "IndirectData"), &bla) && bla) || myself->options & OPTION_INDIRECT)
136 c->options |= OPTION_INDIRECT;
138 if((get_config_bool(lookup_config(c->config_tree, "TCPOnly"), &bla) && bla) || myself->options & OPTION_TCPONLY)
139 c->options |= OPTION_TCPONLY | OPTION_INDIRECT;
141 c->allow_request = METAKEY;
143 return send_metakey(c);
146 int send_metakey(connection_t *c)
148 char buffer[MAX_STRING_SIZE];
152 len = RSA_size(c->rsa_key);
154 /* Allocate buffers for the meta key */
157 c->outkey = xmalloc(len);
160 c->outctx = xmalloc(sizeof(*c->outctx));
162 /* Copy random data to the buffer */
164 RAND_bytes(c->outkey, len);
168 len = 123; /* FIXME: RSA key length */
169 c->outkey = gcry_random_bytes(len, GCRY_WEAK_RANDOM);
172 /* The message we send must be smaller than the modulus of the RSA key.
173 By definition, for a key of k bits, the following formula holds:
175 2^(k-1) <= modulus < 2^(k)
177 Where ^ means "to the power of", not "xor".
178 This means that to be sure, we must choose our message < 2^(k-1).
179 This can be done by setting the most significant bit to zero.
182 c->outkey[0] &= 0x7F;
184 if(debug_lvl >= DEBUG_SCARY_THINGS)
186 bin2hex(c->outkey, buffer, len);
187 buffer[len*2] = '\0';
188 syslog(LOG_DEBUG, _("Generated random meta key (unencrypted): %s"), buffer);
191 /* Encrypt the random data
193 We do not use one of the PKCS padding schemes here.
194 This is allowed, because we encrypt a totally random string
195 with a length equal to that of the modulus of the RSA key.
199 if(RSA_public_encrypt(len, c->outkey, buffer, c->rsa_key, RSA_NO_PADDING) != len)
201 syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
206 /* Convert the encrypted random data to a hexadecimal formatted string */
209 bin2hex(buffer, buffer, len);
211 buffer[len*2] = '\0';
213 /* Send the meta key */
216 x = send_request(c, "%d %d %d %d %d %s", METAKEY,
217 c->outcipher?c->outcipher->nid:0, c->outdigest?c->outdigest->type:0,
218 c->outmaclength, c->outcompression, buffer);
221 /* Further outgoing requests are encrypted with the key we just generated */
226 EVP_EncryptInit(c->outctx, c->outcipher,
227 c->outkey + len - c->outcipher->key_len,
228 c->outkey + len - c->outcipher->key_len - c->outcipher->iv_len);
230 c->status.encryptout = 1;
237 int metakey_h(connection_t *c)
239 char buffer[MAX_STRING_SIZE];
240 int cipher, digest, maclength, compression;
243 if(sscanf(c->buffer, "%*d %d %d %d %d "MAX_STRING, &cipher, &digest, &maclength, &compression, buffer) != 5)
245 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "METAKEY", c->name, c->hostname);
250 len = RSA_size(myself->connection->rsa_key);
253 /* Check if the length of the meta key is all right */
255 if(strlen(buffer) != len*2)
257 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong keylength");
261 /* Allocate buffers for the meta key */
264 c->inkey = xmalloc(len);
268 c->inctx = xmalloc(sizeof(*c->inctx));
271 /* Convert the challenge from hexadecimal back to binary */
273 hex2bin(buffer,buffer,len);
275 /* Decrypt the meta key */
278 if(RSA_private_decrypt(len, buffer, c->inkey, myself->connection->rsa_key, RSA_NO_PADDING) != len) /* See challenge() */
280 syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), c->name, c->hostname);
285 if(debug_lvl >= DEBUG_SCARY_THINGS)
287 bin2hex(c->inkey, buffer, len);
288 buffer[len*2] = '\0';
289 syslog(LOG_DEBUG, _("Received random meta key (unencrypted): %s"), buffer);
292 /* All incoming requests will now be encrypted. */
294 /* Check and lookup cipher and digest algorithms */
299 c->incipher = EVP_get_cipherbynid(cipher);
302 syslog(LOG_ERR, _("%s (%s) uses unknown cipher!"), c->name, c->hostname);
306 EVP_DecryptInit(c->inctx, c->incipher,
307 c->inkey + len - c->incipher->key_len,
308 c->inkey + len - c->incipher->key_len - c->incipher->iv_len);
310 c->status.decryptin = 1;
318 c->inmaclength = maclength;
323 c->indigest = EVP_get_digestbynid(digest);
326 syslog(LOG_ERR, _("Node %s (%s) uses unknown digest!"), c->name, c->hostname);
330 if(c->inmaclength > c->indigest->md_size || c->inmaclength < 0)
332 syslog(LOG_ERR, _("%s (%s) uses bogus MAC length!"), c->name, c->hostname);
342 c->incompression = compression;
344 c->allow_request = CHALLENGE;
346 return send_challenge(c);
349 int send_challenge(connection_t *c)
351 char buffer[MAX_STRING_SIZE];
354 /* CHECKME: what is most reasonable value for len? */
357 len = RSA_size(c->rsa_key);
360 /* Allocate buffers for the challenge */
363 c->hischallenge = xmalloc(len);
365 /* Copy random data to the buffer */
368 RAND_bytes(c->hischallenge, len);
374 bin2hex(c->hischallenge, buffer, len);
375 buffer[len*2] = '\0';
378 /* Send the challenge */
380 x = send_request(c, "%d %s", CHALLENGE, buffer);
385 int challenge_h(connection_t *c)
387 char buffer[MAX_STRING_SIZE];
390 if(sscanf(c->buffer, "%*d "MAX_STRING, buffer) != 1)
392 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "CHALLENGE", c->name, c->hostname);
397 len = RSA_size(myself->connection->rsa_key);
400 /* Check if the length of the challenge is all right */
402 if(strlen(buffer) != len*2)
404 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, "wrong challenge length");
408 /* Allocate buffers for the challenge */
411 c->mychallenge = xmalloc(len);
413 /* Convert the challenge from hexadecimal back to binary */
415 hex2bin(buffer,c->mychallenge,len);
417 c->allow_request = CHAL_REPLY;
419 /* Rest is done by send_chal_reply() */
421 return send_chal_reply(c);
424 int send_chal_reply(connection_t *c)
427 char hash[EVP_MAX_MD_SIZE*2+1];
430 /* Calculate the hash from the challenge we received */
432 EVP_DigestInit(&ctx, c->indigest);
433 EVP_DigestUpdate(&ctx, c->mychallenge, RSA_size(myself->connection->rsa_key));
434 EVP_DigestFinal(&ctx, hash, NULL);
436 /* Convert the hash to a hexadecimal formatted string */
438 bin2hex(hash,hash,c->indigest->md_size);
439 hash[c->indigest->md_size*2] = '\0';
444 return send_request(c, "%d %s", CHAL_REPLY, hash);
451 int chal_reply_h(connection_t *c)
454 char hishash[MAX_STRING_SIZE];
455 char myhash[EVP_MAX_MD_SIZE];
458 if(sscanf(c->buffer, "%*d "MAX_STRING, hishash) != 1)
460 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "CHAL_REPLY", c->name, c->hostname);
464 /* Check if the length of the hash is all right */
466 if(strlen(hishash) != c->outdigest->md_size*2)
468 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply length"));
472 /* Convert the hash to binary format */
474 hex2bin(hishash, hishash, c->outdigest->md_size);
476 /* Calculate the hash from the challenge we sent */
478 EVP_DigestInit(&ctx, c->outdigest);
479 EVP_DigestUpdate(&ctx, c->hischallenge, RSA_size(c->rsa_key));
480 EVP_DigestFinal(&ctx, myhash, NULL);
482 /* Verify the incoming hash with the calculated hash */
484 if(memcmp(hishash, myhash, c->outdigest->md_size))
486 syslog(LOG_ERR, _("Possible intruder %s (%s): %s"), c->name, c->hostname, _("wrong challenge reply"));
487 if(debug_lvl >= DEBUG_SCARY_THINGS)
489 bin2hex(myhash, hishash, SHA_DIGEST_LENGTH);
490 hishash[SHA_DIGEST_LENGTH*2] = '\0';
491 syslog(LOG_DEBUG, _("Expected challenge reply: %s"), hishash);
496 /* Identity has now been positively verified.
497 Send an acknowledgement with the rest of the information needed.
502 c->allow_request = ACK;
507 int send_ack(connection_t *c)
509 /* ACK message contains rest of the information the other end needs
510 to create node_t and edge_t structures. */
513 char *address, *port;
516 /* Estimate weight */
518 gettimeofday(&now, NULL);
519 c->estimated_weight = (now.tv_sec - c->start.tv_sec) * 1000 + (now.tv_usec - c->start.tv_usec) / 1000;
520 sockaddr2str(&c->address, &address, &port);
521 x = send_request(c, "%d %s %s %d %lx", ACK, myport, address, c->estimated_weight, c->options);
528 void send_everything(connection_t *c)
530 avl_node_t *node, *node2;
535 /* Send all known subnets */
537 for(node = node_tree->head; node; node = node->next)
539 n = (node_t *)node->data;
541 for(node2 = n->subnet_tree->head; node2; node2 = node2->next)
543 s = (subnet_t *)node2->data;
544 send_add_subnet(c, s);
548 /* Send all known edges */
550 for(node = edge_tree->head; node; node = node->next)
552 e = (edge_t *)node->data;
561 int ack_h(connection_t *c)
563 char myaddress[MAX_STRING_SIZE];
564 char hisport[MAX_STRING_SIZE];
565 char *hisaddress, *dummy;
572 if(sscanf(c->buffer, "%*d "MAX_STRING" "MAX_STRING" %d %lx", hisport, myaddress, &weight, &options) != 4)
574 syslog(LOG_ERR, _("Got bad %s from %s (%s)"), "ACK", c->name, c->hostname);
578 /* Check if we already have a node_t for him */
580 n = lookup_node(c->name);
585 n->name = xstrdup(c->name);
592 /* Oh dear, we already have a connection to this node. */
593 if(debug_lvl >= DEBUG_CONNECTIONS)
594 syslog(LOG_DEBUG, _("Established a second connection with %s (%s), closing old connection"), n->name, n->hostname);
595 terminate_connection(n->connection, 0);
598 /* FIXME: check if information in existing node matches that of the other end of this connection */
603 c->options |= options;
605 /* Create an edge_t for this connection */
607 c->edge = new_edge();
609 c->edge->from.node = myself;
610 c->edge->from.udpaddress = str2sockaddr(myaddress, myport);
611 c->edge->to.node = n;
612 sockaddr2str(&c->address, &hisaddress, &dummy);
613 c->edge->to.udpaddress = str2sockaddr(hisaddress, hisport);
616 c->edge->weight = (weight + c->estimated_weight) / 2;
617 c->edge->connection = c;
618 c->edge->options = c->options;
622 /* Activate this connection */
624 c->allow_request = ALL;
625 c->status.active = 1;
627 if(debug_lvl >= DEBUG_CONNECTIONS)
628 syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), c->name, c->hostname);
631 /* Send him everything we know */
635 /* Notify others of this connection */
637 for(node = connection_tree->head; node; node = node->next)
639 other = (connection_t *)node->data;
641 if(other->status.active && other != c)
642 send_add_edge(other, c->edge);
645 /* Run MST and SSSP algorithms */