along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- $Id: protocol.c,v 1.28.4.48 2000/10/29 00:24:31 guus Exp $
+ $Id: protocol.c,v 1.28.4.61 2000/11/15 13:33:27 guus Exp $
*/
#include "config.h"
#include <sys/socket.h>
#include <unistd.h>
#include <stdio.h>
+#include <stdarg.h>
#include <utils.h>
#include <xalloc.h>
#include <netinet/in.h>
-#include <openssl/sha.h>
-#include <openssl/rand.h>
+#ifdef HAVE_OPENSSL_SHA_H
+# include <openssl/sha.h>
+#else
+# include <sha.h>
+#endif
+
+#ifdef HAVE_OPENSSL_RAND_H
+# include <openssl/rand.h>
+#else
+# include <rand.h>
+#endif
+
+#ifdef HAVE_OPENSSL_EVP_H
+# include <openssl/evp.h>
+#else
+# include <evp.h>
+#endif
+
#include "conf.h"
-#include "encr.h"
#include "net.h"
#include "netutl.h"
#include "protocol.h"
request_name[request], cl->name, cl->hostname);
}
+ if((cl->allow_request != ALL) && (cl->allow_request != request))
+ {
+ syslog(LOG_ERR, _("Unauthorized request from %s (%s)"), cl->name, cl->hostname);
+ return -1;
+ }
+
if(request_handlers[request](cl))
/* Something went wrong. Probably scriptkiddies. Terminate. */
{
send_challenge(R)
send_chal_reply(H)
---------------------------------------
- Any negotations about the meta protocol
- encryption go here(u).
+ send_metakey(R)
+ send_metakey(R)
---------------------------------------
send_ack(u)
send_ack(u)
int id_h(conn_list_t *cl)
{
conn_list_t *old;
- config_t *cfg;
+ config_t const *cfg;
cp
if(sscanf(cl->buffer, "%*d %as %d %lx %hd", &cl->name, &cl->protocol_version, &cl->options, &cl->port) != 4)
{
}
}
cp
- if((cfg = get_config_val(cl->config, publickey)))
+ if((cfg = get_config_val(cl->config, config_publickey)))
{
cl->rsa_key = RSA_new();
BN_hex2bn(&cl->rsa_key->n, cfg->data.ptr);
cl->hischallenge = xmalloc(len);
cp
- /* Seed the PRNG with urandom (can't afford to block) */
-
- RAND_load_file("/dev/urandom", 1024);
-
/* Copy random data to the buffer */
RAND_bytes(cl->hischallenge, len);
if(cl->status.outgoing)
cl->allow_request = ID;
else
- cl->allow_request = ACK;
+ cl->allow_request = METAKEY;
cp
return send_request(cl, "%d %s", CHAL_REPLY, hash);
*/
cp
if(cl->status.outgoing)
- return send_ack(cl);
+ return send_metakey(cl);
else
return send_id(cl);
}
+int send_metakey(conn_list_t *cl)
+{
+ char *buffer;
+ int len, x;
+cp
+ len = RSA_size(cl->rsa_key);
+
+ /* Allocate buffers for the meta key */
+
+ buffer = xmalloc(len*2+1);
+
+ if(!cl->cipher_outkey)
+ cl->cipher_outkey = xmalloc(len);
+
+ if(!cl->cipher_outctx)
+ cl->cipher_outctx = xmalloc(sizeof(*cl->cipher_outctx));
+cp
+ /* Copy random data to the buffer */
+
+ RAND_bytes(cl->cipher_outkey, len);
+
+ cl->cipher_outkey[0] &= 0x7F; /* FIXME: Somehow if the first byte is more than 0xD0 or something like that, decryption fails... */
+
+ if(debug_lvl >= DEBUG_SCARY_THINGS)
+ {
+ bin2hex(cl->cipher_outkey, buffer, len);
+ buffer[len*2] = '\0';
+ syslog(LOG_DEBUG, _("Generated random meta key (unencrypted): %s"), buffer);
+ }
+
+ /* Encrypt the random data */
+
+ if(RSA_public_encrypt(len, cl->cipher_outkey, buffer, cl->rsa_key, RSA_NO_PADDING) != len) /* NO_PADDING because the message size equals the RSA key size and it is totally random */
+ {
+ syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), cl->name, cl->hostname);
+ free(buffer);
+ return -1;
+ }
+cp
+ /* Convert the encrypted random data to a hexadecimal formatted string */
+
+ bin2hex(buffer, buffer, len);
+ buffer[len*2] = '\0';
+
+ /* Send the meta key */
+
+ if(cl->status.outgoing)
+ cl->allow_request = METAKEY;
+ else
+ cl->allow_request = ACK;
+
+ x = send_request(cl, "%d %s", METAKEY, buffer);
+ free(buffer);
+
+ EVP_EncryptInit(cl->cipher_outctx, EVP_bf_cfb(), cl->cipher_outkey, cl->cipher_outkey + EVP_bf_cfb()->key_len);
+cp
+ return x;
+}
+
+int metakey_h(conn_list_t *cl)
+{
+ char *buffer;
+ int len;
+cp
+ if(sscanf(cl->buffer, "%*d %as", &buffer) != 1)
+ {
+ syslog(LOG_ERR, _("Got bad METAKEY from %s (%s)"), cl->name, cl->hostname);
+ return -1;
+ }
+
+ len = RSA_size(myself->rsa_key);
+
+ /* Check if the length of the meta key is all right */
+
+ if(strlen(buffer) != len*2)
+ {
+ syslog(LOG_ERR, _("Intruder: wrong meta key length from %s (%s)"), cl->name, cl->hostname);
+ free(buffer);
+ return -1;
+ }
+
+ /* Allocate buffers for the meta key */
+
+ if(!cl->cipher_inkey)
+ cl->cipher_inkey = xmalloc(len);
+
+ if(!cl->cipher_inctx)
+ cl->cipher_inctx = xmalloc(sizeof(*cl->cipher_inctx));
+
+ /* Convert the challenge from hexadecimal back to binary */
+
+ hex2bin(buffer,buffer,len);
+
+ /* Decrypt the meta key */
+
+ if(RSA_private_decrypt(len, buffer, cl->cipher_inkey, myself->rsa_key, RSA_NO_PADDING) != len) /* See challenge() */
+ {
+ syslog(LOG_ERR, _("Error during encryption of meta key for %s (%s)"), cl->name, cl->hostname);
+ free(buffer);
+ return -1;
+ }
+
+ if(debug_lvl >= DEBUG_SCARY_THINGS)
+ {
+ bin2hex(cl->cipher_inkey, buffer, len);
+ buffer[len*2] = '\0';
+ syslog(LOG_DEBUG, _("Received random meta key (unencrypted): %s"), buffer);
+ }
+
+ free(buffer);
+
+ EVP_DecryptInit(cl->cipher_inctx, EVP_bf_cfb(), cl->cipher_inkey, cl->cipher_inkey + EVP_bf_cfb()->key_len);
+
+cp
+ if(cl->status.outgoing)
+ return send_ack(cl);
+ else
+ return send_metakey(cl);
+}
+
int send_ack(conn_list_t *cl)
{
+ int x;
cp
- cl->allow_request = ACK;
+ if(cl->status.outgoing)
+ cl->allow_request = ACK;
+
+ setup_vpn_connection(cl);
+
+ x = send_request(cl, "%d", ACK);
+ cl->status.encryptout = 1;
cp
- return send_request(cl, "%d", ACK);
+ return x;
}
int ack_h(conn_list_t *cl)
cl->allow_request = ALL;
cl->status.active = 1;
+ cl->status.decryptin = 1;
cl->nexthop = cl;
+ cl->cipher_pkttype = EVP_bf_cfb();
+ cl->cipher_pktkeylength = cl->cipher_pkttype->key_len + cl->cipher_pkttype->iv_len;
if(debug_lvl >= DEBUG_CONNECTIONS)
syslog(LOG_NOTICE, _("Connection with %s (%s) activated"), cl->name, cl->hostname);
{
conn_list_t *old, *new;
conn_list_t *p;
+
cp
new = new_conn_list();
new->nexthop = cl;
new->status.active = 1;
+ new->cipher_pkttype = EVP_bf_cfb();
+ new->cipher_pktkeylength = cl->cipher_pkttype->key_len + cl->cipher_pkttype->iv_len;
+ /* Okay this is a bit ugly... it would be better to setup UDP sockets dynamically, or
+ * perhaps just one UDP socket... but then again, this has benefits too...
+ */
+
+ setup_vpn_connection(new);
cp
return 0;
}
int send_ping(conn_list_t *cl)
{
+cp
cl->status.pinged = 1;
+ cl->last_ping_time = time(NULL);
cp
return send_request(cl, "%d", PING);
}
int pong_h(conn_list_t *cl)
{
cp
- cl->status.got_pong = 1;
+ cl->status.pinged = 0;
cp
return 0;
}
{
char *from_id, *to_id;
conn_list_t *from, *to;
+ char pktkey[129];
cp
if(sscanf(cl->buffer, "%*d %as %as", &from_id, &to_id) != 2)
{
if(!strcmp(to_id, myself->name))
{
- send_ans_key(myself, from, myself->cipher_pktkey);
+ bin2hex(myself->cipher_pktkey, pktkey, myself->cipher_pktkeylength);
+ pktkey[myself->cipher_pktkeylength*2] = '\0';
+ send_ans_key(myself, from, pktkey);
}
else
{
free(from_id); free(to_id);
return -1;
}
- send_req_key(from, to);
+
+ if(to->status.validkey) /* Proxy keys */
+ {
+ bin2hex(to->cipher_pktkey, pktkey, to->cipher_pktkeylength);
+ pktkey[to->cipher_pktkeylength*2] = '\0';
+ send_ans_key(to, from, pktkey);
+ }
+ else
+ send_req_key(from, to);
}
free(from_id); free(to_id);
return -1;
}
- /* Check if this key request is for us */
+ /* Check correctness of packet key */
- if(!strcmp(to_id, myself->name))
- {
- /* It is for us, convert it to binary and set the key with it. */
+ keylength = strlen(pktkey);
- keylength = strlen(pktkey);
+ if(keylength != from->cipher_pktkeylength*2)
+ {
+ syslog(LOG_ERR, _("Got bad ANS_KEY from %s (%s) origin %s: invalid key length"),
+ cl->name, cl->hostname, from->name);
+ free(from_id); free(to_id); free(pktkey);
+ return -1;
+ }
-/* Don't do this... yet
- if((keylength%2) || (keylength <= 0))
- {
- syslog(LOG_ERR, _("Got bad ANS_KEY from %s (%s) origin %s: invalid key"),
- cl->name, cl->hostname, from->name);
- free(from_id); free(to_id); free(pktkey);
- return -1;
- }
- keylength /= 2;
- hex2bin(pktkey, pktkey, keylength);
- BF_set_key(cl->cipher_pktkey, keylength, pktkey);
-*/
+ /* Forward it if necessary */
- from->status.validkey = 1;
- from->status.waitingforkey = 0;
- }
- else
+ if(strcmp(to_id, myself->name))
{
if(!(to = lookup_id(to_id)))
{
syslog(LOG_ERR, _("Got ANS_KEY from %s (%s) destination %s which does not exist in our connection list"),
cl->name, cl->hostname, to_id);
- free(from_id); free(to_id); free(pktkey);
+ free(from_id); free(to_id);
return -1;
}
send_ans_key(from, to, pktkey);
}
- free(from_id); free(to_id); free(pktkey);
+ /* Update our copy of the origin's packet key */
+
+ if(from->cipher_pktkey)
+ free(from->cipher_pktkey);
+
+ keylength /= 2;
+ hex2bin(pktkey, pktkey, keylength);
+ pktkey[keylength] = '\0';
+ from->cipher_pktkey = pktkey;
+
+ from->status.validkey = 1;
+ from->status.waitingforkey = 0;
+
+ free(from_id); free(to_id);
cp
return 0;
}
/* Jumptable for the request handlers */
int (*request_handlers[])(conn_list_t*) = {
- id_h, challenge_h, chal_reply_h, ack_h,
+ id_h, challenge_h, chal_reply_h, metakey_h, ack_h,
status_h, error_h, termreq_h,
ping_h, pong_h,
add_host_h, del_host_h,
/* Request names */
char (*request_name[]) = {
- "ID", "CHALLENGE", "CHAL_REPLY", "ACK",
+ "ID", "CHALLENGE", "CHAL_REPLY", "METAKEY", "ACK",
"STATUS", "ERROR", "TERMREQ",
"PING", "PONG",
"ADD_HOST", "DEL_HOST",