updates.
[silc.git] / apps / silcd / protocol.c
index ad86e001293603e345f9931b3bab9928a28dc100..cea428ca50164802dec07327ac020aec9b868d6d 100644 (file)
@@ -54,17 +54,17 @@ static void silc_server_protocol_ke_send_packet(SilcSKE ske,
 
 /* Sets the negotiated key material into use for particular connection. */
 
-static int 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)
 {
   SilcUnknownEntry conn_data;
   SilcIDListData idata;
-  SilcHash nhash;
 
   SILC_LOG_DEBUG(("Setting new key into use"));
 
@@ -102,25 +102,26 @@ static int silc_server_protocol_ke_set_keys(SilcSKE ske,
     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, &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 the remote host's public key */
+  silc_pkcs_public_key_decode(ske->ke1_payload->pk_data, 
+                             ske->ke1_payload->pk_len, &idata->public_key);
+
+  /* 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. */
-  if (!silc_hash_alloc(hash->hash->name, &nhash)) {
+  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_alloc(nhash, &idata->hmac);
   silc_hmac_set_key(idata->hmac, keymat->hmac_key, keymat->hmac_key_len);
 
   sock->user_data = (void *)conn_data;
@@ -135,6 +136,9 @@ SilcSKEStatus silc_ske_check_version(SilcSKE ske, unsigned char *version,
 {
   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;
@@ -187,7 +191,8 @@ 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->buffer, NULL, NULL);
+                                         ctx->packet->buffer, FALSE,
+                                         NULL, NULL);
       } else {
        SilcSKEStartPayload *start_payload;
 
@@ -269,7 +274,7 @@ SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
           the initiator. This also creates our parts of the Diffie
           Hellman algorithm. */
        status = silc_ske_responder_phase_2(ctx->ske, ctx->packet->buffer, 
-                                           NULL, NULL);
+                                           NULL, NULL, NULL, NULL);
       } else {
        /* Call the Phase-2 function. This creates Diffie Hellman
           key exchange parameters and sends our public part inside
@@ -277,6 +282,7 @@ SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
        status = 
          silc_ske_initiator_phase_2(ctx->ske,
                                     server->public_key,
+                                    server->private_key,
                                     silc_server_protocol_ke_send_packet,
                                     context);
       }
@@ -346,32 +352,25 @@ 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 key_len = silc_cipher_get_key_len(ctx->ske->prop->cipher);
       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. */
-      if (ctx->responder == TRUE)
-       silc_ske_end(ctx->ske, silc_server_protocol_ke_send_packet, context);
-
       /* Process the key material */
       keymat = silc_calloc(1, sizeof(*keymat));
-      silc_ske_process_key_material(ctx->ske, 16, key_len, hash_len,
-                                   keymat);
-
-      /* Take the new keys into use. */
-      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)) {
+      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;
 
-      silc_ske_free_key_material(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);
 
       /* Unregister the timeout task since the protocol has ended. 
         This was the timeout task to be executed if the protocol is
@@ -437,10 +436,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;
@@ -451,25 +449,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);
@@ -488,17 +481,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;
 }
@@ -535,7 +566,7 @@ 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 (%s)",
                       ctx->sock->hostname, ctx->sock->ip));
@@ -584,8 +615,6 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
                              protocol, fd, 0, 300000);
            return;
          }
-       } else {
-         auth_data = NULL;
        }
 
        /* 
@@ -599,15 +628,13 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
        /* Remote end is client */
        if (conn_type == SILC_SOCKET_TYPE_CLIENT) {
          SilcServerConfigSectionClientConnection *client = NULL;
-         client = 
-           silc_server_config_find_client_conn(server->config,
-                                               ctx->sock->ip,
-                                               ctx->sock->port);
+         client = silc_server_config_find_client_conn(server->config,
+                                                      ctx->sock->ip,
+                                                      ctx->sock->port);
          if (!client)
-           client = 
-             silc_server_config_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) {
@@ -622,16 +649,13 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
              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"));
              SILC_LOG_DEBUG(("Authentication failed"));
+             silc_free(auth_data);
              protocol->state = SILC_PROTOCOL_STATE_ERROR;
              protocol->execute(server->timeout_queue, 0, 
                                protocol, fd, 0, 300000);
@@ -646,16 +670,13 @@ 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"));
+             silc_free(auth_data);
              protocol->state = SILC_PROTOCOL_STATE_ERROR;
              protocol->execute(server->timeout_queue, 0, 
                                protocol, fd, 0, 300000);
@@ -665,9 +686,7 @@ 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);
@@ -678,16 +697,14 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
        /* Remote end is server */
        if (conn_type == SILC_SOCKET_TYPE_SERVER) {
          SilcServerConfigSectionServerConnection *serv = NULL;
-         serv = 
-           silc_server_config_find_server_conn(server->config,
-                                               ctx->sock->ip,
-                                               ctx->sock->port);
+         serv = silc_server_config_find_server_conn(server->config,
+                                                    ctx->sock->ip,
+                                                    ctx->sock->port);
          if (!serv)
-           serv = 
-             silc_server_config_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_AUTH_NONE:
@@ -701,22 +718,19 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
              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"));
              SILC_LOG_DEBUG(("Authentication failed"));
+             silc_free(auth_data);
              protocol->state = SILC_PROTOCOL_STATE_ERROR;
              protocol->execute(server->timeout_queue, 0, 
                                protocol, fd, 0, 300000);
              return;
              break;
-             
+
            case SILC_AUTH_PUBLIC_KEY:
              /* Public key authentication */
              SILC_LOG_DEBUG(("Public key authentication"));
@@ -726,15 +740,12 @@ 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"));
+             silc_free(auth_data);
              protocol->state = SILC_PROTOCOL_STATE_ERROR;
              protocol->execute(server->timeout_queue, 0, 
                                protocol, fd, 0, 300000);
@@ -744,12 +755,10 @@ 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);
+           silc_free(auth_data);
            return;
          }
        }
@@ -757,15 +766,13 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
        /* Remote end is router */
        if (conn_type == SILC_SOCKET_TYPE_ROUTER) {
          SilcServerConfigSectionServerConnection *serv = NULL;
-         serv = 
-           silc_server_config_find_router_conn(server->config,
-                                               ctx->sock->ip,
-                                               ctx->sock->port);
+         serv = silc_server_config_find_router_conn(server->config,
+                                                    ctx->sock->ip,
+                                                    ctx->sock->port);
          if (!serv)
-           serv = 
-             silc_server_config_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) {
@@ -780,16 +787,13 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
              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"));
              SILC_LOG_DEBUG(("Authentication failed"));
+             silc_free(auth_data);
              protocol->state = SILC_PROTOCOL_STATE_ERROR;
              protocol->execute(server->timeout_queue, 0, 
                                protocol, fd, 0, 300000);
@@ -805,15 +809,12 @@ 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"));
+             silc_free(auth_data);
              protocol->state = SILC_PROTOCOL_STATE_ERROR;
              protocol->execute(server->timeout_queue, 0, 
                                protocol, fd, 0, 300000);
@@ -823,9 +824,7 @@ 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);
@@ -833,11 +832,8 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
          }
        }
        
-       if (auth_data) {
-         memset(auth_data, 0, payload_len);
-         silc_free(auth_data);
-       }
-       
+       silc_free(auth_data);
+
        /* Save connection type. This is later used to create the
           ID for the connection. */
        ctx->conn_type = conn_type;
@@ -865,20 +861,22 @@ SILC_TASK_CALLBACK(silc_server_protocol_connection_auth)
        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_AUTH_PUBLIC_KEY:
-         /* Public key authentication */
-         /* XXX TODO */
-         break;
+         {
+           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;