- Lots of small changes.
[tinc] / src / protocol.c
index 8c6ce92..0b76cc3 100644 (file)
@@ -17,7 +17,7 @@
     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.27 2000/09/10 22:49:46 guus Exp $
+    $Id: protocol.c,v 1.28.4.30 2000/09/14 11:54:51 guus Exp $
 */
 
 #include "config.h"
@@ -67,7 +67,7 @@ cp
   if(debug_lvl >= DEBUG_META)
     syslog(LOG_DEBUG, _("Sending meta data to %s (%s): %s"), cl->id, cl->hostname, buffer);
 
-  if(cl->status.encryptout)
+  if(cl->status.encrypted)
     {
       /* FIXME: Do encryption */
     }
@@ -83,16 +83,22 @@ cp
 /* Connection protocol:
 
    Client               Server
-   send_id(*)
-                        send_challenge
-   send_chal_reply(*)                   
-                        send_id
-   send_challenge
-                        send_chal_reply
-   send_ack
-                       send_ack
-
-   (*) Unencrypted.
+   send_id(u)
+                        send_challenge(R)
+   send_chal_reply(BH)                   
+                        send_id(B)
+   send_challenge(BR)
+                        send_chal_reply(BH)
+   send_ack(B)
+                       send_ack(B)
+
+   (u) Unencrypted,
+   (R) RSA,
+   (H) SHA1,
+   (B) Blowfish.
+
+   Part of the challenge is directly used to set the blowfish key and the initial vector.
+   (Twee vliegen in één klap!)
 */      
 
 int send_id(conn_list_t *cl)
@@ -163,10 +169,6 @@ cp
         }
     }
 
-  /* Since we know the identity now, we can encrypt the meta channel */
-  
-  cl->status.encryptout = 1;
-
   /* Send a challenge to verify the identity */
 
   cl->allow_request = CHAL_REPLY;
@@ -186,13 +188,20 @@ cp
   /* Allocate buffers for the challenge and the hash */
   
   cl->chal_hash = xmalloc(SHA_DIGEST_LEN);
-  keylength = BN_num_bytes(cl->metakey.n);
+  keylength = BN_num_bytes(cl->rsakey.n);
   buffer = xmalloc(keylength*2);
 
   /* Copy random data and the public key to the buffer */
   
   RAND_bytes(buffer, keylength);
-  BN_bn2bin(cl->metakey.n, buffer+keylength);
+  BN_bn2bin(cl->rsakey.n, buffer+keylength);
+
+  /* If we don't have a blowfish key set yet, use the random data from the challenge to do so. */
+  
+  if(!cl->status.encrypted)
+    {
+      set_metakey(cl, buffer, keylength);
+    }
 
   /* Calculate the hash from that */
 
@@ -208,6 +217,7 @@ cp
   cl->allow_request = CHAL_REPLY;
   x = send_request(cl, "%d %s", CHALLENGE, buffer);
   free(buffer);
+  cl->status.encrypted = 1;
 cp
   return x;
 }
@@ -237,7 +247,7 @@ int send_chal_reply(conn_list_t *cl, char *challenge)
   char *hash;
   int x;
 cp
-  keylength = BN_num_bytes(myself->meyakey.n);
+  keylength = BN_num_bytes(myself->rsakey.n);
 
   /* Check if the length of the challenge is all right */
 
@@ -255,11 +265,20 @@ cp
   /* Copy the incoming random data and our public key to the buffer */
 
   hex2bin(challenge, buffer, keylength); 
-  BN_bn2bin(myself->metakey.n, buffer+keylength);
+  BN_bn2bin(myself->rsakey.n, buffer+keylength);
 
   /* Calculate the hash from that */
   
   SHA1(buffer, keylength*2, hash);
+
+  /* If we don't have a blowfish key set yet, use the random data from the challenge to do so. */
+  
+  if(!cl->status.encrypted)
+    {
+      set_metakey(cl, buffer, keylength);
+      cl->status.encrypted = 1;
+    }
+
   free(buffer);
 
   /* Convert the hash to a hexadecimal formatted string */
@@ -674,9 +693,11 @@ cp
   if(!(from = lookup_id(from_id)))
     {
       syslog(LOG_ERR, _("Got KEY_CHANGED from %s (%s) origin %s which does not exist in our connection list"), cl->id, cl->hostname, from_id);
-      free(from);
+      free(from_id);
       return -1;
     }
+
+  free(from_id);
     
   from->status.validkey = 0;
   from->status.waitingforkey = 0;
@@ -706,7 +727,7 @@ cp
   if(!(from = lookup_id(from_id)))
     {
       syslog(LOG_ERR, _("Got REQ_KEY from %s (%s) origin %s which does not exist in our connection list"), cl->id, cl->hostname, from_id);
-      free(from); free(to);
+      free(from_id); free(to_id);
       return -1;
     }
 
@@ -721,13 +742,13 @@ cp
       if(!(to = lookup_id(to_id)))
         {
           syslog(LOG_ERR, _("Got REQ_KEY from %s (%s) destination %s which does not exist in our connection list"), cl->id, cl->hostname, to_id);
-          free(from); free(to);
+          free(from_id); free(to_id);
           return -1;
         }
       send_req_key(from, to);
     }
 
-  free(from); free(to);
+  free(from_id); free(to_id);
 cp
   return 0;
 }
@@ -741,6 +762,7 @@ cp
 int ans_key_h(conn_list_t *cl)
 {
   char *from_id, *to_id, *datakey;
+  int keylength;
   conn_list_t *from, *to;
 cp
   if(sscanf(cl->buffer, "%*d %as %as %as", &from_id, &to_id, &datakey) != 3)
@@ -752,7 +774,7 @@ cp
   if(!(from = lookup_id(from_id)))
     {
       syslog(LOG_ERR, _("Got ANS_KEY from %s (%s) origin %s which does not exist in our connection list"), cl->id, cl->hostname, from_id);
-      free(from); free(to); free(datakey);
+      free(from_id); free(to_id); free(datakey);
       return -1;
     }
 
@@ -760,56 +782,38 @@ cp
 
   if(!strcmp(id, myself->strcmp))
     {
-      send_ans_key(myself, from);
+      /* It is for us, convert it to binary and set the key with it. */
+      
+      keylength = strlen(datakey);
+      
+      if((keylength%2) || (keylength <= 0))
+        {
+          syslog(LOG_ERR, _("Got bad ANS_KEY from %s (%s) origin %s: invalid key"), cl->id, cl->hostname, from->id);
+          free(from_id); free(to_id); free(datakey);
+          return -1;
+        }
+      keylength /= 2;
+      hex2bin(datakey, datakey, keylength);
+      BF_set_key(cl->datakey, keylength, datakey);
     }
   else
     {
       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->id, cl->hostname, to_id);
-          free(from); free(to); free(datakey);
+          free(from_id); free(to_id); free(datakey);
           return -1;
         }
       send_ans_key(from, to, datakey);
     }
 
-  free(from); free(to); free(datakey);
+  free(from_id); free(to_id); free(datakey);
 cp
   return 0;
 }
 
-
-
-
 /* Old routines */
 
-
-
-
-int send_tcppacket(conn_list_t *cl, void *data, int len)
-{
-cp
-  if(debug_lvl > 3)
-    syslog(LOG_DEBUG, _("Sending PACKET to %s (%s)"),
-          cl->vpn_hostname, cl->real_hostname);
-
-  buflen = snprintf(buffer, MAXBUFSIZE, "%d %d\n", PACKET, len);
-
-  if((write(cl->meta_socket, buffer, buflen)) < 0)
-    {
-      syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
-      return -1;
-    }
-    
-  if((write(cl->meta_socket, data, len)) < 0)
-    {
-      syslog(LOG_ERR, _("Send failed: %s:%d: %m"), __FILE__, __LINE__);
-      return -1;
-    }
-cp
-  return 0;
-}
-
 /*
   Notify all my direct connections of a new host
   that was added to the vpn, with the exception
@@ -844,120 +848,6 @@ cp
   return 0;
 }
 
-/*
-  The incoming request handlers
-*/
-
-int tcppacket_h(conn_list_t *cl)
-{
-  int len;
-cp
-  if(!cl->status.active)
-    {
-      syslog(LOG_ERR, _("Got unauthorized PACKET from %s (%s)"),
-              cl->vpn_hostname, cl->real_hostname);
-      return -1;
-    }
-
-  if(sscanf(cl->buffer, "%*d %d", &len) != 1)
-    {
-       syslog(LOG_ERR, _("Got bad PACKET from %s (%s)"),
-              cl->vpn_hostname, cl->real_hostname);
-       return -1;
-    }  
-
-  if(len > MTU)
-    {
-       syslog(LOG_ERR, _("Got too big PACKET from %s (%s)"),
-              cl->vpn_hostname, cl->real_hostname);
-       return -1;
-    }  
-
-  if(debug_lvl > 3)
-    syslog(LOG_DEBUG, _("Got PACKET length %d from %s (%s)"), len,
-              cl->vpn_hostname, cl->real_hostname);
-
-  cl->tcppacket=len;
-cp
-  return 0;
-}
-
-void set_keys(conn_list_t *cl, int expiry, char *key)
-{
-  char *ek;
-cp
-  if(!cl->public_key)
-    {
-      cl->public_key = xmalloc(sizeof(*cl->key));
-      cl->public_key->key = NULL;
-    }
-    
-  if(cl->public_key->key)
-    free(cl->public_key->key);
-  cl->public_key->length = strlen(key);
-  cl->public_key->expiry = expiry;
-  cl->public_key->key = xmalloc(cl->public_key->length + 1);
-  strcpy(cl->public_key->key, key);
-
-  ek = make_shared_key(key);
-  
-  if(!cl->key)
-    {
-      cl->key = xmalloc(sizeof(*cl->key));
-      cl->key->key = NULL;
-    }
-
-  if(cl->key->key)
-    free(cl->key->key);
-
-  cl->key->length = strlen(ek);
-  cl->key->expiry = expiry;
-  cl->key->key = xmalloc(cl->key->length + 1);
-  strcpy(cl->key->key, ek);
-cp
-}
-
-
-int key_changed_h(conn_list_t *cl)
-{
-  ip_t from;
-  conn_list_t *ik;
-cp
-  if(!cl->status.active)
-    {
-      syslog(LOG_ERR, _("Got unauthorized KEY_CHANGED from %s (%s)"),
-              cl->vpn_hostname, cl->real_hostname);
-      return -1;
-    }
-
-  if(sscanf(cl->buffer, "%*d %lx", &from) != 1)
-    {
-       syslog(LOG_ERR, _("Got bad KEY_CHANGED from %s (%s)"),
-              cl->vpn_hostname, cl->real_hostname);
-       return -1;
-    }  
-
-  ik = lookup_conn(from);
-
-  if(!ik)
-    {
-      syslog(LOG_ERR, _("Got KEY_CHANGED origin %d.%d.%d.%d from %s (%s), which does not exist?"),
-            IP_ADDR_V(from), cl->vpn_hostname, cl->real_hostname);
-      return -1;
-    }
-
-  if(debug_lvl > 1)
-    syslog(LOG_DEBUG, _("Got KEY_CHANGED origin %s from %s (%s)"),
-            ik->vpn_hostname, cl->vpn_hostname, cl->real_hostname);
-
-  ik->status.validkey = 0;
-  ik->status.waitingforkey = 0;
-
-  notify_others(ik, cl, send_key_changed);
-cp
-  return 0;
-}
-
 /* "Complete overhaul". */
 
 int (*request_handlers[])(conn_list_t*) = {