updates.
[silc.git] / apps / silcd / protocol.c
index 60d8b0a142e347b6943c5118a565fb8301bdce84..ad86e001293603e345f9931b3bab9928a28dc100 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
@@ -54,13 +54,13 @@ 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)
+static int silc_server_protocol_ke_set_keys(SilcSKE ske,
+                                           SilcSocketConnection sock,
+                                           SilcSKEKeyMaterial *keymat,
+                                           SilcCipher cipher,
+                                           SilcPKCS pkcs,
+                                           SilcHash hash,
+                                           int is_responder)
 {
   SilcUnknownEntry conn_data;
   SilcIDListData idata;
@@ -72,8 +72,14 @@ static void silc_server_protocol_ke_set_keys(SilcSKE ske,
   idata = (SilcIDListData)conn_data;
 
   /* Allocate cipher to be used in the communication */
-  silc_cipher_alloc(cipher->cipher->name, &idata->send_key);
-  silc_cipher_alloc(cipher->cipher->name, &idata->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) {
     idata->send_key->cipher->set_key(idata->send_key->context, 
@@ -108,11 +114,18 @@ static void silc_server_protocol_ke_set_keys(SilcSKE ske,
 #endif
 
   /* Save HMAC key to be used in the communication. */
-  silc_hash_alloc(hash->hash->name, &nhash);
+  if (!silc_hash_alloc(hash->hash->name, &nhash)) {
+    silc_cipher_free(idata->send_key);
+    silc_cipher_free(idata->receive_key);
+    silc_free(conn_data);
+    return FALSE;
+  }
   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;
+
+  return TRUE;
 }
 
 /* Check remote host version string */
@@ -333,6 +346,8 @@ SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
        * End protocol
        */
       SilcSKEKeyMaterial *keymat;
+      int key_len = silc_cipher_get_key_len(ctx->ske->prop->cipher, NULL);
+      int hash_len = ctx->ske->prop->hash->hash->hash_len;
 
       /* Send Ok to the other end if we are responder. If we are 
         initiator we have sent this already. */
@@ -341,14 +356,22 @@ SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
 
       /* Process the key material */
       keymat = silc_calloc(1, sizeof(*keymat));
-      silc_ske_process_key_material(ctx->ske, 16, (16 * 8), 16, keymat);
+      silc_ske_process_key_material(ctx->ske, 16, key_len, hash_len,
+                                   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);
+      if (!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)) {
+       protocol->state = SILC_PROTOCOL_STATE_ERROR;
+       protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
+       silc_ske_free_key_material(keymat);
+       return;
+      }
+
+      silc_ske_free_key_material(keymat);
 
       /* Unregister the timeout task since the protocol has ended. 
         This was the timeout task to be executed if the protocol is
@@ -519,13 +542,18 @@ 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->buffer,
-                            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->buffer->len) {
-         SILC_LOG_ERROR(("Bad payload in authentication packet"));
          SILC_LOG_DEBUG(("Bad payload in authentication packet"));
          protocol->state = SILC_PROTOCOL_STATE_ERROR;
          protocol->execute(server->timeout_queue, 0, protocol, fd, 0, 300000);
@@ -537,7 +565,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;
@@ -546,10 +573,17 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
        if (payload_len > 0) {
          /* Get authentication data */
          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);
+         ret = silc_buffer_unformat(ctx->packet->buffer,
+                                    SILC_STR_UI_XNSTRING_ALLOC(&auth_data, 
+                                                               payload_len),
+                                    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;
+         }
        } else {
          auth_data = NULL;
        }
@@ -564,25 +598,25 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
 
        /* Remote end is client */
        if (conn_type == SILC_SOCKET_TYPE_CLIENT) {
-         SilcConfigServerSectionClientConnection *client = NULL;
+         SilcServerConfigSectionClientConnection *client = NULL;
          client = 
-           silc_config_server_find_client_conn(server->config,
+           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,
+             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,
@@ -604,7 +638,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, 
@@ -643,25 +677,25 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
        
        /* Remote end is server */
        if (conn_type == SILC_SOCKET_TYPE_SERVER) {
-         SilcConfigServerSectionServerConnection *serv = NULL;
+         SilcServerConfigSectionServerConnection *serv = NULL;
          serv = 
-           silc_config_server_find_server_conn(server->config,
+           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,
+             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,
@@ -683,7 +717,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, 
@@ -722,25 +756,25 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
        
        /* Remote end is router */
        if (conn_type == SILC_SOCKET_TYPE_ROUTER) {
-         SilcConfigServerSectionServerConnection *serv = NULL;
+         SilcServerConfigSectionServerConnection *serv = NULL;
          serv = 
-           silc_config_server_find_router_conn(server->config,
+           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,
+             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,
@@ -762,7 +796,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, 
@@ -824,11 +858,11 @@ 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;
@@ -841,7 +875,7 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
 
          break;
          
-       case SILC_PROTOCOL_CONN_AUTH_PUBLIC_KEY:
+       case SILC_AUTH_PUBLIC_KEY:
          /* Public key authentication */
          /* XXX TODO */
          break;
@@ -883,7 +917,7 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
        */
       unsigned char ok[4];
 
-      SILC_PUT32_MSB(SILC_CONN_AUTH_OK, ok);
+      SILC_PUT32_MSB(SILC_AUTH_OK, ok);
 
       /* Authentication failed */
       silc_server_packet_send(server, ctx->sock, SILC_PACKET_SUCCESS,
@@ -909,7 +943,7 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
        */
       unsigned char error[4];
 
-      SILC_PUT32_MSB(SILC_CONN_AUTH_FAILED, error);
+      SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
 
       /* Authentication failed */
       silc_server_packet_send(server, ctx->sock, SILC_PACKET_FAILURE,