updates.
[silc.git] / apps / silcd / protocol.c
index d6daeba98b1ea3e14ae3fb35d43fe97c35f90689..cba75ccf25327259ff0fa0f8c62dd6bd24e86f90 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
 
-  Copyright (C) 1997 - 2000 Pekka Riikonen
+  Copyright (C) 1997 - 2001 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
 /*
  * Server side of the protocols.
  */
-/*
- * $Id$
- * $Log$
- * 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"
 SILC_TASK_CALLBACK(silc_server_protocol_connection_auth);
 SILC_TASK_CALLBACK(silc_server_protocol_key_exchange);
 
-/* SILC client protocol list */
-const SilcProtocolObject silc_protocol_list[] =
-{
-  { SILC_PROTOCOL_SERVER_CONNECTION_AUTH, 
-    silc_server_protocol_connection_auth },
-  { SILC_PROTOCOL_SERVER_KEY_EXCHANGE, 
-    silc_server_protocol_key_exchange },
-
-  { SILC_PROTOCOL_SERVER_NONE, NULL },
-};
+extern char *silc_version_string;
 
 /*
  * Key Exhange protocol functions
@@ -84,66 +54,100 @@ static void silc_server_protocol_ke_send_packet(SilcSKE ske,
 
 /* Sets the negotiated key material into use for particular connection. */
 
-static void silc_server_protocol_ke_set_keys(SilcSKE ske,
-                                            SilcSocketConnection sock,
-                                            SilcSKEKeyMaterial *keymat,
-                                            SilcCipher cipher,
-                                            SilcPKCS pkcs,
-                                            SilcHash hash,
-                                            int is_responder)
+int silc_server_protocol_ke_set_keys(SilcSKE ske,
+                                    SilcSocketConnection sock,
+                                    SilcSKEKeyMaterial *keymat,
+                                    SilcCipher cipher,
+                                    SilcPKCS pkcs,
+                                    SilcHash hash,
+                                    SilcHmac hmac,
+                                    int is_responder)
 {
-  SilcIDListUnknown *conn_data;
-  SilcHash nhash;
+  SilcUnknownEntry conn_data;
+  SilcIDListData idata;
 
   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);
+  if (!silc_cipher_alloc(cipher->cipher->name, &idata->send_key)) {
+    silc_free(conn_data);
+    return FALSE;
+  }
+  if (!silc_cipher_alloc(cipher->cipher->name, &idata->receive_key)) {
+    silc_free(conn_data);
+    return FALSE;
+  }
   
   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 */
-#if 0
-  /* 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, 
-                          ske->ke2_payload->pk_len);
-#endif
+  /* Save the hash */
+  if (!silc_hash_alloc(hash->hash->name, &idata->hash)) {
+    silc_cipher_free(idata->send_key);
+    silc_cipher_free(idata->receive_key);
+    silc_free(conn_data);
+    return FALSE;
+  }
 
   /* Save HMAC key to be used in the communication. */
-  silc_hash_alloc(hash->hash->name, &nhash);
-  silc_hmac_alloc(nhash, &conn_data->hmac);
-  conn_data->hmac_key_len = keymat->hmac_key_len;
-  conn_data->hmac_key = silc_calloc(conn_data->hmac_key_len,
-                                   sizeof(unsigned char));
-  memcpy(conn_data->hmac_key, keymat->hmac_key, keymat->hmac_key_len);
+  if (!silc_hmac_alloc(hmac->hmac->name, NULL, &idata->hmac)) {
+    silc_cipher_free(idata->send_key);
+    silc_cipher_free(idata->receive_key);
+    silc_hash_free(idata->hash);
+    silc_free(conn_data);
+    return FALSE;
+  }
+  silc_hmac_set_key(idata->hmac, keymat->hmac_key, keymat->hmac_key_len);
 
   sock->user_data = (void *)conn_data;
+
+  return TRUE;
+}
+
+/* Check remote host version string */
+
+SilcSKEStatus silc_ske_check_version(SilcSKE ske, unsigned char *version,
+                                    unsigned int len)
+{
+  SilcSKEStatus status = SILC_SKE_STATUS_OK;
+
+  SILC_LOG_INFO(("%s (%s) is version %s", ske->sock->hostname,
+                ske->sock->ip, version));
+
+  /* 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
@@ -176,17 +180,21 @@ SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
       /* Allocate Key Exchange object */
       ske = silc_ske_alloc();
       ctx->ske = ske;
+      ske->rng = server->rng;
       
       if (ctx->responder == TRUE) {
        /* Start the key exchange by processing the received security
           properties packet from initiator. */
        status = silc_ske_responder_start(ske, ctx->rng, ctx->sock,
-                                         ctx->packet, NULL, NULL);
+                                         silc_version_string,
+                                         ctx->packet->buffer, NULL, NULL);
       } else {
        SilcSKEStartPayload *start_payload;
 
        /* Assemble security properties. */
-       silc_ske_assemble_security_properties(ske, &start_payload);
+       silc_ske_assemble_security_properties(ske, SILC_SKE_SP_FLAG_NONE, 
+                                             silc_version_string,
+                                             &start_payload);
 
        /* Start the key exchange by sending our security properties
           to the remote end. */
@@ -230,10 +238,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) {
@@ -262,14 +268,15 @@ 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
           Key Exhange 1 Payload to the responder. */
        status = 
          silc_ske_initiator_phase_2(ctx->ske,
+                                    server->public_key,
                                     silc_server_protocol_ke_send_packet,
                                     context);
       }
@@ -308,9 +315,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) {
@@ -333,28 +339,32 @@ SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
       protocol->state = SILC_PROTOCOL_STATE_END;
     }
     break;
+
   case SILC_PROTOCOL_STATE_END:
     {
       /* 
        * End protocol
        */
       SilcSKEKeyMaterial *keymat;
-
-      /* Send Ok to the other end if we are responder. If we are 
-        initiator we have sent this already. */
-      if (ctx->responder == TRUE)
-       silc_ske_end(ctx->ske, silc_server_protocol_ke_send_packet, context);
+      int key_len = silc_cipher_get_key_len(ctx->ske->prop->cipher);
+      int hash_len = ctx->ske->prop->hash->hash->hash_len;
 
       /* Process the key material */
       keymat = silc_calloc(1, sizeof(*keymat));
-      silc_ske_process_key_material(ctx->ske, 16, (16 * 8), 16, keymat);
+      status = silc_ske_process_key_material(ctx->ske, 16, key_len, hash_len,
+                                            keymat);
+      if (status != SILC_SKE_STATUS_OK) {
+       protocol->state = SILC_PROTOCOL_STATE_ERROR;
+       protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
+       silc_ske_free_key_material(keymat);
+       return;
+      }
+      ctx->keymat = keymat;
 
-      /* Take the new keys into use. */
-      silc_server_protocol_ke_set_keys(ctx->ske, ctx->sock, keymat,
-                                      ctx->ske->prop->cipher,
-                                      ctx->ske->prop->pkcs,
-                                      ctx->ske->prop->hash,
-                                      ctx->responder);
+      /* Send Ok to the other end if we are responder. If we are initiator
+        we have sent this already. */
+      if (ctx->responder == TRUE)
+       silc_ske_end(ctx->ske, 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
@@ -369,11 +379,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. */
@@ -386,6 +402,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;
   }
@@ -395,10 +430,9 @@ SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
  * Connection Authentication protocol functions
  */
 
-/* XXX move these to somehwere else */
-
-int silc_server_password_authentication(SilcServer server, char *auth1, 
-                                       char *auth2)
+static int 
+silc_server_password_authentication(SilcServer server, char *auth1, 
+                                   char *auth2)
 {
   if (!auth1 || !auth2)
     return FALSE;
@@ -409,25 +443,20 @@ int silc_server_password_authentication(SilcServer server, char *auth1,
   return FALSE;
 }
 
-int silc_server_public_key_authentication(SilcServer server,
-                                         char *pkfile,
-                                         unsigned char *sign,
-                                         unsigned int sign_len,
-                                         SilcSKE ske)
+static int
+silc_server_public_key_authentication(SilcServer server,
+                                     SilcPublicKey pub_key,
+                                     unsigned char *sign,
+                                     unsigned int sign_len,
+                                     SilcSKE ske)
 {
-  SilcPublicKey pub_key;
   SilcPKCS pkcs;
   int len;
   SilcBuffer auth;
 
-  if (!pkfile || !sign)
+  if (!pub_key || !sign)
     return FALSE;
 
-  /* Load public key from file */
-  if (!silc_pkcs_load_public_key(pkfile, &pub_key, SILC_PKCS_FILE_PEM))
-    if (!silc_pkcs_load_public_key(pkfile, &pub_key, SILC_PKCS_FILE_BIN))
-      return FALSE;
-
   silc_pkcs_alloc(pub_key->name, &pkcs);
   if (!silc_pkcs_public_key_set(pkcs, pub_key)) {
     silc_pkcs_free(pkcs);
@@ -446,17 +475,55 @@ int silc_server_public_key_authentication(SilcServer server,
                     SILC_STR_END);
 
   /* Verify signature */
-  if (pkcs->pkcs->verify(pkcs->context, sign, sign_len,
-                        auth->data, auth->len))
-    {
-      silc_pkcs_free(pkcs);
-      silc_pkcs_public_key_free(pub_key);
-      silc_buffer_free(auth);
-      return TRUE;
-    }
+  if (silc_pkcs_verify(pkcs, sign, sign_len, auth->data, auth->len)) {
+    silc_pkcs_free(pkcs);
+    silc_buffer_free(auth);
+    return TRUE;
+  }
+
+  silc_pkcs_free(pkcs);
+  silc_buffer_free(auth);
+  return FALSE;
+}
+
+static int
+silc_server_get_public_key_auth(SilcServer server,
+                               SilcPublicKey pub_key,
+                               unsigned char *auth_data,
+                               unsigned int *auth_data_len,
+                               SilcSKE ske)
+{
+  int len;
+  SilcPKCS pkcs;
+  SilcBuffer auth;
+
+  if (!pub_key)
+    return FALSE;
+
+  silc_pkcs_alloc(pub_key->name, &pkcs);
+  if (!silc_pkcs_public_key_set(pkcs, pub_key)) {
+    silc_pkcs_free(pkcs);
+    return FALSE;
+  }
+
+  /* Make the authentication data. Protocol says it is HASH plus
+     KE Start Payload. */
+  len = ske->hash_len + ske->start_payload_copy->len;
+  auth = silc_buffer_alloc(len);
+  silc_buffer_pull_tail(auth, len);
+  silc_buffer_format(auth,
+                    SILC_STR_UI_XNSTRING(ske->hash, ske->hash_len),
+                    SILC_STR_UI_XNSTRING(ske->start_payload_copy->data,
+                                         ske->start_payload_copy->len),
+                    SILC_STR_END);
+
+  if (silc_pkcs_sign(pkcs, auth->data, auth->len, auth_data, auth_data_len)) {
+    silc_pkcs_free(pkcs);
+    silc_buffer_free(auth);
+    return TRUE;
+  }
 
   silc_pkcs_free(pkcs);
-  silc_pkcs_public_key_free(pub_key);
   silc_buffer_free(auth);
   return FALSE;
 }
@@ -493,21 +560,25 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
        int ret;
        unsigned short payload_len;
        unsigned short conn_type;
-       unsigned char *auth_data;
+       unsigned char *auth_data = NULL;
 
-       SILC_LOG_INFO(("Performing authentication protocol for %s",
-                      ctx->sock->hostname ? ctx->sock->hostname :
-                      ctx->sock->ip));
+       SILC_LOG_INFO(("Performing authentication protocol for %s (%s)",
+                      ctx->sock->hostname, ctx->sock->ip));
 
        /* Parse the received authentication data packet. The received
           payload is Connection Auth Payload. */
-       silc_buffer_unformat(ctx->packet,
-                            SILC_STR_UI_SHORT(&payload_len),
-                            SILC_STR_UI_SHORT(&conn_type),
-                            SILC_STR_END);
+       ret = silc_buffer_unformat(ctx->packet->buffer,
+                                  SILC_STR_UI_SHORT(&payload_len),
+                                  SILC_STR_UI_SHORT(&conn_type),
+                                  SILC_STR_END);
+       if (ret == -1) {
+         SILC_LOG_DEBUG(("Bad payload in authentication packet"));
+         protocol->state = SILC_PROTOCOL_STATE_ERROR;
+         protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
+         return;
+       }
        
-       if (payload_len != ctx->packet->len) {
-         SILC_LOG_ERROR(("Bad payload in authentication packet"));
+       if (payload_len != ctx->packet->buffer->len) {
          SILC_LOG_DEBUG(("Bad payload in authentication packet"));
          protocol->state = SILC_PROTOCOL_STATE_ERROR;
          protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
@@ -519,7 +590,6 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
        if (conn_type < SILC_SOCKET_TYPE_CLIENT || 
            conn_type > SILC_SOCKET_TYPE_ROUTER) {
          SILC_LOG_ERROR(("Bad connection type %d", conn_type));
-         SILC_LOG_DEBUG(("Bad connection type %d", conn_type));
          protocol->state = SILC_PROTOCOL_STATE_ERROR;
          protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
          return;
@@ -527,13 +597,18 @@ 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_STR_UI_XNSTRING_ALLOC(&auth_data, 
+         silc_buffer_pull(ctx->packet->buffer, 4);
+         ret = silc_buffer_unformat(ctx->packet->buffer,
+                                    SILC_STR_UI_XNSTRING(&auth_data, 
                                                          payload_len),
-                              SILC_STR_END);
-       } else {
-         auth_data = NULL;
+                                    SILC_STR_END);
+         if (ret == -1) {
+           SILC_LOG_DEBUG(("Bad payload in authentication packet"));
+           protocol->state = SILC_PROTOCOL_STATE_ERROR;
+           protocol->execute(server->timeout_queue, 0, 
+                             protocol, fd, 0, 300000);
+           return;
+         }
        }
 
        /* 
@@ -546,36 +621,30 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
 
        /* Remote end is client */
        if (conn_type == SILC_SOCKET_TYPE_CLIENT) {
-         SilcConfigServerSectionClientConnection *client = NULL;
-         client = 
-           silc_config_server_find_client_conn(server->config,
-                                               ctx->sock->ip,
-                                               ctx->sock->port);
+         SilcServerConfigSectionClientConnection *client = NULL;
+         client = silc_server_config_find_client_conn(server->config,
+                                                      ctx->sock->ip,
+                                                      ctx->sock->port);
          if (!client)
-           client = 
-             silc_config_server_find_client_conn(server->config,
-                                                 ctx->sock->hostname,
-                                                 ctx->sock->port);
+           client = silc_server_config_find_client_conn(server->config,
+                                                        ctx->sock->hostname,
+                                                        ctx->sock->port);
          
          if (client) {
            switch(client->auth_meth) {
-           case SILC_PROTOCOL_CONN_AUTH_NONE:
+           case SILC_AUTH_NONE:
              /* No authentication required */
              SILC_LOG_DEBUG(("No authentication required"));
              break;
              
-           case SILC_PROTOCOL_CONN_AUTH_PASSWORD:
+           case SILC_AUTH_PASSWORD:
              /* Password authentication */
              SILC_LOG_DEBUG(("Password authentication"));
              ret = silc_server_password_authentication(server, auth_data,
                                                        client->auth_data);
 
-             if (ret) {
-               memset(auth_data, 0, payload_len);
-               silc_free(auth_data);
-               auth_data = NULL;
+             if (ret)
                break;
-             }
 
              /* Authentication failed */
              SILC_LOG_ERROR(("Authentication failed"));
@@ -586,7 +655,7 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
              return;
              break;
              
-           case SILC_PROTOCOL_CONN_AUTH_PUBLIC_KEY:
+           case SILC_AUTH_PUBLIC_KEY:
              /* Public key authentication */
              SILC_LOG_DEBUG(("Public key authentication"));
              ret = silc_server_public_key_authentication(server, 
@@ -594,13 +663,9 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
                                                          auth_data,
                                                          payload_len, 
                                                          ctx->ske);
-                                                         
-             if (ret) {
-               memset(auth_data, 0, payload_len);
-               silc_free(auth_data);
-               auth_data = NULL;
+
+             if (ret)
                break;
-             }
 
              SILC_LOG_ERROR(("Authentication failed"));
              SILC_LOG_DEBUG(("Authentication failed"));
@@ -613,9 +678,6 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
            SILC_LOG_DEBUG(("No configuration for remote connection"));
            SILC_LOG_ERROR(("Remote connection not configured"));
            SILC_LOG_ERROR(("Authentication failed"));
-           memset(auth_data, 0, payload_len);
-           silc_free(auth_data);
-           auth_data = NULL;
            protocol->state = SILC_PROTOCOL_STATE_ERROR;
            protocol->execute(server->timeout_queue, 0, 
                              protocol, fd, 0, 300000);
@@ -625,36 +687,30 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
        
        /* Remote end is server */
        if (conn_type == SILC_SOCKET_TYPE_SERVER) {
-         SilcConfigServerSectionServerConnection *serv = NULL;
-         serv = 
-           silc_config_server_find_server_conn(server->config,
-                                               ctx->sock->ip,
-                                               ctx->sock->port);
+         SilcServerConfigSectionServerConnection *serv = NULL;
+         serv = silc_server_config_find_server_conn(server->config,
+                                                    ctx->sock->ip,
+                                                    ctx->sock->port);
          if (!serv)
-           serv = 
-             silc_config_server_find_server_conn(server->config,
-                                                 ctx->sock->hostname,
-                                                 ctx->sock->port);
-         
+           serv = silc_server_config_find_server_conn(server->config,
+                                                      ctx->sock->hostname,
+                                                      ctx->sock->port);
+
          if (serv) {
            switch(serv->auth_meth) {
-           case SILC_PROTOCOL_CONN_AUTH_NONE:
+           case SILC_AUTH_NONE:
              /* No authentication required */
              SILC_LOG_DEBUG(("No authentication required"));
              break;
              
-           case SILC_PROTOCOL_CONN_AUTH_PASSWORD:
+           case SILC_AUTH_PASSWORD:
              /* Password authentication */
              SILC_LOG_DEBUG(("Password authentication"));
              ret = silc_server_password_authentication(server, auth_data,
                                                        serv->auth_data);
 
-             if (ret) {
-               memset(auth_data, 0, payload_len);
-               silc_free(auth_data);
-               auth_data = NULL;
+             if (ret)
                break;
-             }
              
              /* Authentication failed */
              SILC_LOG_ERROR(("Authentication failed"));
@@ -665,7 +721,7 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
              return;
              break;
              
-           case SILC_PROTOCOL_CONN_AUTH_PUBLIC_KEY:
+           case SILC_AUTH_PUBLIC_KEY:
              /* Public key authentication */
              SILC_LOG_DEBUG(("Public key authentication"));
              ret = silc_server_public_key_authentication(server, 
@@ -674,12 +730,8 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
                                                          payload_len, 
                                                          ctx->ske);
                                                          
-             if (ret) {
-               memset(auth_data, 0, payload_len);
-               silc_free(auth_data);
-               auth_data = NULL;
+             if (ret)
                break;
-             }
 
              SILC_LOG_ERROR(("Authentication failed"));
              SILC_LOG_DEBUG(("Authentication failed"));
@@ -692,9 +744,6 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
            SILC_LOG_DEBUG(("No configuration for remote connection"));
            SILC_LOG_ERROR(("Remote connection not configured"));
            SILC_LOG_ERROR(("Authentication failed"));
-           memset(auth_data, 0, payload_len);
-           silc_free(auth_data);
-           auth_data = NULL;
            protocol->state = SILC_PROTOCOL_STATE_ERROR;
            protocol->execute(server->timeout_queue, 0, 
                              protocol, fd, 0, 300000);
@@ -704,36 +753,30 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
        
        /* Remote end is router */
        if (conn_type == SILC_SOCKET_TYPE_ROUTER) {
-         SilcConfigServerSectionServerConnection *serv = NULL;
-         serv = 
-           silc_config_server_find_router_conn(server->config,
-                                               ctx->sock->ip,
-                                               ctx->sock->port);
+         SilcServerConfigSectionServerConnection *serv = NULL;
+         serv = silc_server_config_find_router_conn(server->config,
+                                                    ctx->sock->ip,
+                                                    ctx->sock->port);
          if (!serv)
-           serv = 
-             silc_config_server_find_router_conn(server->config,
-                                                 ctx->sock->hostname,
-                                                 ctx->sock->port);
+           serv = silc_server_config_find_router_conn(server->config,
+                                                      ctx->sock->hostname,
+                                                      ctx->sock->port);
          
          if (serv) {
            switch(serv->auth_meth) {
-           case SILC_PROTOCOL_CONN_AUTH_NONE:
+           case SILC_AUTH_NONE:
              /* No authentication required */
              SILC_LOG_DEBUG(("No authentication required"));
              break;
              
-           case SILC_PROTOCOL_CONN_AUTH_PASSWORD:
+           case SILC_AUTH_PASSWORD:
              /* Password authentication */
              SILC_LOG_DEBUG(("Password authentication"));
              ret = silc_server_password_authentication(server, auth_data,
                                                        serv->auth_data);
 
-             if (ret) {
-               memset(auth_data, 0, payload_len);
-               silc_free(auth_data);
-               auth_data = NULL;
+             if (ret)
                break;
-             }
              
              /* Authentication failed */
              SILC_LOG_ERROR(("Authentication failed"));
@@ -744,7 +787,7 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
              return;
              break;
              
-           case SILC_PROTOCOL_CONN_AUTH_PUBLIC_KEY:
+           case SILC_AUTH_PUBLIC_KEY:
              /* Public key authentication */
              SILC_LOG_DEBUG(("Public key authentication"));
              ret = silc_server_public_key_authentication(server, 
@@ -753,12 +796,8 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
                                                          payload_len, 
                                                          ctx->ske);
                                                          
-             if (ret) {
-               memset(auth_data, 0, payload_len);
-               silc_free(auth_data);
-               auth_data = NULL;
+             if (ret)
                break;
-             }
 
              SILC_LOG_ERROR(("Authentication failed"));
              SILC_LOG_DEBUG(("Authentication failed"));
@@ -771,9 +810,6 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
            SILC_LOG_DEBUG(("No configuration for remote connection"));
            SILC_LOG_ERROR(("Remote connection not configured"));
            SILC_LOG_ERROR(("Authentication failed"));
-           memset(auth_data, 0, payload_len);
-           silc_free(auth_data);
-           auth_data = NULL;
            protocol->state = SILC_PROTOCOL_STATE_ERROR;
            protocol->execute(server->timeout_queue, 0, 
                              protocol, fd, 0, 300000);
@@ -781,11 +817,6 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
          }
        }
        
-       if (auth_data) {
-         memset(auth_data, 0, payload_len);
-         silc_free(auth_data);
-       }
-       
        /* Save connection type. This is later used to create the
           ID for the connection. */
        ctx->conn_type = conn_type;
@@ -806,27 +837,29 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
        unsigned int auth_data_len = 0;
        
        switch(ctx->auth_meth) {
-       case SILC_PROTOCOL_CONN_AUTH_NONE:
+       case SILC_AUTH_NONE:
          /* No authentication required */
          break;
          
-       case SILC_PROTOCOL_CONN_AUTH_PASSWORD:
+       case SILC_AUTH_PASSWORD:
          /* Password authentication */
          if (ctx->auth_data && ctx->auth_data_len) {
-           auth_data = ctx->auth_data;
+           auth_data = strdup(ctx->auth_data);
            auth_data_len = ctx->auth_data_len;
            break;
          }
-
-         /* No authentication data exits. Ask interactively from user. */
-         /* XXX */
-
          break;
          
-       case SILC_PROTOCOL_CONN_AUTH_PUBLIC_KEY:
-         /* Public key authentication */
-         /* XXX TODO */
-         break;
+       case SILC_AUTH_PUBLIC_KEY:
+         {
+           unsigned char sign[1024];
+
+           /* Public key authentication */
+           silc_server_get_public_key_auth(server, ctx->auth_data,
+                                           sign, &auth_data_len,
+                                           ctx->ske);
+           break;
+         }
        }
        
        payload_len = 4 + auth_data_len;
@@ -863,10 +896,13 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
       /* 
        * End protocol
        */
+      unsigned char ok[4];
 
-      /* Succesfully authenticated */
-      silc_server_packet_send(server, ctx->sock, SILC_PACKET_SUCCESS, 
-                             0, NULL, 0, TRUE);
+      SILC_PUT32_MSB(SILC_AUTH_OK, ok);
+
+      /* Authentication failed */
+      silc_server_packet_send(server, ctx->sock, SILC_PACKET_SUCCESS,
+                             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
@@ -884,12 +920,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_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
@@ -904,7 +943,46 @@ 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;
   }
 }
+
+/* Registers protocols used in server. */
+
+void silc_server_protocols_register(void)
+{
+  silc_protocol_register(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
+                        silc_server_protocol_connection_auth);
+  silc_protocol_register(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
+                        silc_server_protocol_key_exchange);
+}
+
+/* Unregisters protocols */
+
+void silc_server_protocols_unregister(void)
+{
+  silc_protocol_unregister(SILC_PROTOCOL_SERVER_CONNECTION_AUTH,
+                          silc_server_protocol_connection_auth);
+  silc_protocol_unregister(SILC_PROTOCOL_SERVER_KEY_EXCHANGE,
+                          silc_server_protocol_key_exchange);
+}