Implemented version string checking to client and server.
[silc.git] / apps / silcd / protocol.c
index cf4e07fe2de6af98238b3305d242f51e9fb7c1e3..fbe1f566d0a15b8a24baea01817dfb488cc05e92 100644 (file)
 /*
  * Server side of the protocols.
  */
-/*
- * $Id$
- * $Log$
- * Revision 1.10  2000/10/09 11:41:17  priikone
- *     Changed to use new generic payloads.
- *     Implemented new protocol compliant notify messages.
- *     Implemented protocol compliant channel messages.
- *     Bugfixes.
- *
- * Revision 1.9  2000/07/20 10:17:25  priikone
- *     Added dynamic protocol registering/unregistering support.  The
- *     patch was provided by cras.
- *
- * Revision 1.8  2000/07/19 07:08:09  priikone
- *     Added version detection support to SKE.
- *
- * Revision 1.7  2000/07/14 06:14:20  priikone
- *     Put the HMAC keys into the HMAC object instead of having them
- *     saved elsewhere; now we can use silc_hmac_make insteaad of
- *     silc_hmac_make_with_key.
- *
- * Revision 1.6  2000/07/12 05:59:41  priikone
- *     Major rewrite of ID Cache system. Support added for the new
- *     ID cache system. Major rewrite of ID List stuff on server.  All
- *     SilcXXXList's are now called SilcXXXEntry's and they are pointers
- *     by default. A lot rewritten ID list functions.
- *
- * Revision 1.5  2000/07/10 05:42:14  priikone
- *     Support for public key encoding functions added.
- *
- * Revision 1.4  2000/07/07 06:55:59  priikone
- *     Added SILC style public key support and made server to use
- *     it at all time.
- *
- * Revision 1.3  2000/07/06 07:15:31  priikone
- *     Cleaner code fro password and public key authentication.
- *     Deprecated old `channel_auth' protocol.
- *
- * Revision 1.2  2000/07/05 06:13:04  priikone
- *     Support for SILC style public keys added.
- *
- * Revision 1.1.1.1  2000/06/27 11:36:56  priikone
- *     Imported from internal CVS/Added Log headers.
- *
- *
- */
+/* $Id$ */
 
 #include "serverincludes.h"
 #include "server_internal.h"
@@ -108,35 +63,37 @@ static void silc_server_protocol_ke_set_keys(SilcSKE ske,
                                             int is_responder)
 {
   SilcUnknownEntry conn_data;
+  SilcIDListData idata;
   SilcHash nhash;
 
   SILC_LOG_DEBUG(("Setting new key into use"));
 
   conn_data = silc_calloc(1, sizeof(*conn_data));
+  idata = (SilcIDListData)conn_data;
 
   /* Allocate cipher to be used in the communication */
-  silc_cipher_alloc(cipher->cipher->name, &conn_data->send_key);
-  silc_cipher_alloc(cipher->cipher->name, &conn_data->receive_key);
+  silc_cipher_alloc(cipher->cipher->name, &idata->send_key);
+  silc_cipher_alloc(cipher->cipher->name, &idata->receive_key);
   
   if (is_responder == TRUE) {
-    conn_data->send_key->cipher->set_key(conn_data->send_key->context, 
-                                        keymat->receive_enc_key, 
-                                        keymat->enc_key_len);
-    conn_data->send_key->set_iv(conn_data->send_key, keymat->receive_iv);
-    conn_data->receive_key->cipher->set_key(conn_data->receive_key->context, 
-                                           keymat->send_enc_key, 
-                                           keymat->enc_key_len);
-    conn_data->receive_key->set_iv(conn_data->receive_key, keymat->send_iv);
+    idata->send_key->cipher->set_key(idata->send_key->context, 
+                                    keymat->receive_enc_key, 
+                                    keymat->enc_key_len);
+    idata->send_key->set_iv(idata->send_key, keymat->receive_iv);
+    idata->receive_key->cipher->set_key(idata->receive_key->context, 
+                                       keymat->send_enc_key, 
+                                       keymat->enc_key_len);
+    idata->receive_key->set_iv(idata->receive_key, keymat->send_iv);
     
   } else {
-    conn_data->send_key->cipher->set_key(conn_data->send_key->context, 
-                                        keymat->send_enc_key, 
-                                        keymat->enc_key_len);
-    conn_data->send_key->set_iv(conn_data->send_key, keymat->send_iv);
-    conn_data->receive_key->cipher->set_key(conn_data->receive_key->context, 
-                                           keymat->receive_enc_key, 
-                                           keymat->enc_key_len);
-    conn_data->receive_key->set_iv(conn_data->receive_key, keymat->receive_iv);
+    idata->send_key->cipher->set_key(idata->send_key->context, 
+                                    keymat->send_enc_key, 
+                                    keymat->enc_key_len);
+    idata->send_key->set_iv(idata->send_key, keymat->send_iv);
+    idata->receive_key->cipher->set_key(idata->receive_key->context, 
+                                       keymat->receive_enc_key, 
+                                       keymat->enc_key_len);
+    idata->receive_key->set_iv(idata->receive_key, keymat->receive_iv);
   }
 
   /* Allocate PKCS to be used */
@@ -144,20 +101,42 @@ static void silc_server_protocol_ke_set_keys(SilcSKE ske,
   /* XXX Do we ever need to allocate PKCS for the connection??
      If yes, we need to change KE protocol to get the initiators
      public key. */
-  silc_pkcs_alloc(pkcs->pkcs->name, &conn_data->pkcs);
-  conn_data->public_key = silc_pkcs_public_key_alloc(XXX);
-  silc_pkcs_set_public_key(conn_data->pkcs, ske->ke2_payload->pk_data, 
+  silc_pkcs_alloc(pkcs->pkcs->name, &idata->pkcs);
+  idata->public_key = silc_pkcs_public_key_alloc(XXX);
+  silc_pkcs_set_public_key(idata->pkcs, ske->ke2_payload->pk_data, 
                           ske->ke2_payload->pk_len);
 #endif
 
   /* Save HMAC key to be used in the communication. */
   silc_hash_alloc(hash->hash->name, &nhash);
-  silc_hmac_alloc(nhash, &conn_data->hmac);
-  silc_hmac_set_key(conn_data->hmac, keymat->hmac_key, keymat->hmac_key_len);
+  silc_hmac_alloc(nhash, &idata->hmac);
+  silc_hmac_set_key(idata->hmac, keymat->hmac_key, keymat->hmac_key_len);
 
   sock->user_data = (void *)conn_data;
 }
 
+/* Check remote host version string */
+
+SilcSKEStatus silc_ske_check_version(SilcSKE ske, unsigned char *version,
+                                    unsigned int len)
+{
+  SilcSKEStatus status = SILC_SKE_STATUS_OK;
+
+  /* Check for initial version string */
+  if (!strstr(version, "SILC-1.0-"))
+    status = SILC_SKE_STATUS_BAD_VERSION;
+
+  /* Check software version */
+
+  if (len < strlen(silc_version_string))
+    status = SILC_SKE_STATUS_BAD_VERSION;
+
+  /* XXX for now there is no other tests due to the abnormal version
+     string that is used */
+
+  return status;
+}
+
 /* Performs key exchange protocol. This is used for both initiator
    and responder key exchange. This is performed always when accepting
    new connection to the server. This may be called recursively. */
@@ -195,7 +174,7 @@ SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
           properties packet from initiator. */
        status = silc_ske_responder_start(ske, ctx->rng, ctx->sock,
                                          silc_version_string,
-                                         ctx->packet, NULL, NULL);
+                                         ctx->packet->buffer, NULL, NULL);
       } else {
        SilcSKEStartPayload *start_payload;
 
@@ -246,10 +225,8 @@ SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
           paylaod reply we just got from the responder. The callback
           function will receive the processed payload where we will
           save it. */
-       status = 
-         silc_ske_initiator_phase_1(ctx->ske,
-                                    ctx->packet,
-                                    NULL, NULL);
+       status = silc_ske_initiator_phase_1(ctx->ske, ctx->packet->buffer,
+                                           NULL, NULL);
       }
 
       if (status != SILC_SKE_STATUS_OK) {
@@ -278,8 +255,8 @@ SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
        /* Process the received Key Exchange 1 Payload packet from
           the initiator. This also creates our parts of the Diffie
           Hellman algorithm. */
-       status = 
-         silc_ske_responder_phase_2(ctx->ske, ctx->packet, NULL, NULL);
+       status = silc_ske_responder_phase_2(ctx->ske, ctx->packet->buffer, 
+                                           NULL, NULL);
       } else {
        /* Call the Phase-2 function. This creates Diffie Hellman
           key exchange parameters and sends our public part inside
@@ -325,9 +302,8 @@ SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
       } else {
        /* Finish the protocol. This verifies the Key Exchange 2 payload
           sent by responder. */
-       status = 
-         silc_ske_initiator_finish(ctx->ske,
-                                   ctx->packet, NULL, NULL, NULL, NULL);
+       status = silc_ske_initiator_finish(ctx->ske, ctx->packet->buffer, 
+                                          NULL, NULL, NULL, NULL);
       }
 
       if (status != SILC_SKE_STATUS_OK) {
@@ -350,6 +326,7 @@ SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
       protocol->state = SILC_PROTOCOL_STATE_END;
     }
     break;
+
   case SILC_PROTOCOL_STATE_END:
     {
       /* 
@@ -386,11 +363,17 @@ SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
        silc_protocol_free(protocol);
     }
     break;
+
   case SILC_PROTOCOL_STATE_ERROR:
     /*
      * Error occured
      */
 
+    /* Send abort notification */
+    silc_ske_abort(ctx->ske, ctx->ske->status, 
+                  silc_server_protocol_ke_send_packet,
+                  context);
+
     /* Unregister the timeout task since the protocol has ended. 
        This was the timeout task to be executed if the protocol is
        not completed fast enough. */
@@ -403,6 +386,25 @@ SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
     else
       silc_protocol_free(protocol);
     break;
+
+  case SILC_PROTOCOL_STATE_FAILURE:
+    /*
+     * We have received failure from remote
+     */
+
+    /* Unregister the timeout task since the protocol has ended. 
+       This was the timeout task to be executed if the protocol is
+       not completed fast enough. */
+    if (ctx->timeout_task)
+      silc_task_unregister(server->timeout_queue, ctx->timeout_task);
+
+    /* On error the final callback is always called. */
+    if (protocol->final_callback)
+      protocol->execute_final(server->timeout_queue, 0, protocol, fd);
+    else
+      silc_protocol_free(protocol);
+    break;
+
   case SILC_PROTOCOL_STATE_UNKNOWN:
     break;
   }
@@ -518,12 +520,12 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
 
        /* Parse the received authentication data packet. The received
           payload is Connection Auth Payload. */
-       silc_buffer_unformat(ctx->packet,
+       silc_buffer_unformat(ctx->packet->buffer,
                             SILC_STR_UI_SHORT(&payload_len),
                             SILC_STR_UI_SHORT(&conn_type),
                             SILC_STR_END);
        
-       if (payload_len != ctx->packet->len) {
+       if (payload_len != ctx->packet->buffer->len) {
          SILC_LOG_ERROR(("Bad payload in authentication packet"));
          SILC_LOG_DEBUG(("Bad payload in authentication packet"));
          protocol->state = SILC_PROTOCOL_STATE_ERROR;
@@ -544,8 +546,8 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
        
        if (payload_len > 0) {
          /* Get authentication data */
-         silc_buffer_pull(ctx->packet, 4);
-         silc_buffer_unformat(ctx->packet,
+         silc_buffer_pull(ctx->packet->buffer, 4);
+         silc_buffer_unformat(ctx->packet->buffer,
                               SILC_STR_UI_XNSTRING_ALLOC(&auth_data, 
                                                          payload_len),
                               SILC_STR_END);
@@ -880,10 +882,13 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
       /* 
        * End protocol
        */
+      unsigned char ok[4];
+
+      SILC_PUT32_MSB(SILC_CONN_AUTH_OK, ok);
 
-      /* Succesfully authenticated */
-      silc_server_packet_send(server, ctx->sock, SILC_PACKET_SUCCESS, 
-                             0, NULL, 0, TRUE);
+      /* Authentication failed */
+      silc_server_packet_send(server, ctx->sock, SILC_PACKET_FAILURE,
+                             0, ok, 4, TRUE);
 
       /* Unregister the timeout task since the protocol has ended. 
         This was the timeout task to be executed if the protocol is
@@ -901,12 +906,15 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
   case SILC_PROTOCOL_STATE_ERROR:
     {
       /*
-       * Error 
+       * Error. Send notify to remote.
        */
+      unsigned char error[4];
+
+      SILC_PUT32_MSB(SILC_CONN_AUTH_FAILED, error);
 
       /* Authentication failed */
       silc_server_packet_send(server, ctx->sock, SILC_PACKET_FAILURE,
-                             0, NULL, 0, TRUE);
+                             0, error, 4, TRUE);
 
       /* Unregister the timeout task since the protocol has ended. 
         This was the timeout task to be executed if the protocol is
@@ -921,6 +929,25 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
        silc_protocol_free(protocol);
     }
     break;
+
+  case SILC_PROTOCOL_STATE_FAILURE:
+    /*
+     * We have received failure from remote
+     */
+
+    /* Unregister the timeout task since the protocol has ended. 
+       This was the timeout task to be executed if the protocol is
+       not completed fast enough. */
+    if (ctx->timeout_task)
+      silc_task_unregister(server->timeout_queue, ctx->timeout_task);
+
+    /* On error the final callback is always called. */
+    if (protocol->final_callback)
+      protocol->execute_final(server->timeout_queue, 0, protocol, fd);
+    else
+      silc_protocol_free(protocol);
+    break;
+
   case SILC_PROTOCOL_STATE_UNKNOWN:
     break;
   }