ROBODoc documented the SILC Hash Interface and improved
[silc.git] / apps / silcd / protocol.c
index fb2d41d371021fe807bf55f71507ee64e738c602..75eb86ed70906707402d6143279145f87e9b5082 100644 (file)
@@ -291,13 +291,14 @@ int silc_server_protocol_ke_set_keys(SilcServer server,
   idata->rekey->ske_group = silc_ske_group_get_number(group);
 
   /* Save the hash */
-  if (!silc_hash_alloc(hash->hash->name, &idata->hash)) {
+  if (!silc_hash_alloc(silc_hash_get_name(hash), &idata->hash)) {
     silc_cipher_free(idata->send_key);
     silc_cipher_free(idata->receive_key);
     silc_hmac_free(idata->hmac_send);
     silc_hmac_free(idata->hmac_receive);
     silc_free(conn_data);
-    SILC_LOG_ERROR(("Cannot allocate algorithm: %s", hash->hash->name));
+    SILC_LOG_ERROR(("Cannot allocate algorithm: %s", 
+                   silc_hash_get_name(hash)));
     return FALSE;
   }
 
@@ -310,11 +311,12 @@ int silc_server_protocol_ke_set_keys(SilcServer server,
 
   sock->user_data = (void *)conn_data;
 
-  SILC_LOG_INFO(("%s (%s) security properties: %s %s %s", 
+  SILC_LOG_INFO(("%s (%s) security properties: %s %s %s %s", 
                 sock->hostname, sock->ip,
                 idata->send_key->cipher->name,
                 (char *)silc_hmac_get_name(idata->hmac_send),
-                idata->hash->hash->name));
+                silc_hash_get_name(idata->hash),
+                ske->prop->flags & SILC_SKE_SP_FLAG_PFS ? "PFS" : ""));
 
   return TRUE;
 }
@@ -324,60 +326,36 @@ int silc_server_protocol_ke_set_keys(SilcServer server,
 SilcSKEStatus silc_ske_check_version(SilcSKE ske, unsigned char *version,
                                     SilcUInt32 len, void *context)
 {
-  SilcSKEStatus status = SILC_SKE_STATUS_OK;
-  char *cp;
-  int maj = 0, min = 0, build = 0, maj2 = 0, min2 = 0, build2 = 0;
+  SilcUInt32 l_protocol_version = 0, r_protocol_version = 0;
 
   SILC_LOG_INFO(("%s (%s) is version %s", ske->sock->hostname,
                 ske->sock->ip, version));
 
-  /* Check for initial version string. Allowed "SILC-x.x-". More 
-     specific protocol version is checked later in session. */
-  if (!strstr(version, "SILC-"))
-    status = SILC_SKE_STATUS_BAD_VERSION;
-
-  /* Check software version */
-
-  cp = version + 9;
-  if (!cp)
-    status = SILC_SKE_STATUS_BAD_VERSION;
-
-  maj = atoi(cp);
-  cp = strchr(cp, '.');
-  if (cp) {
-    min = atoi(cp + 1);
-    cp++;
-  }
-  if (cp) {
-    cp = strchr(cp, '.');
-    if (cp)
-      build = atoi(cp + 1);
+  if (!silc_parse_version_string(version, &r_protocol_version, NULL, NULL,
+                                NULL, NULL)) {
+    SILC_LOG_ERROR(("%s (%s) %s is not allowed/supported version", 
+                   ske->sock->hostname, ske->sock->ip, version));
+    return SILC_SKE_STATUS_BAD_VERSION;
   }
 
-  cp = silc_version_string + 9;
-  if (!cp)
-    status = SILC_SKE_STATUS_BAD_VERSION;
-
-  maj2 = atoi(cp);
-  cp = strchr(cp, '.');
-  if (cp) {
-    min2 = atoi(cp + 1);
-    cp++;
-  }
-  if (cp) {
-    cp = strchr(cp, '.');
-    if (cp)
-      build2 = atoi(cp + 1);
+  if (!silc_parse_version_string(silc_version_string, 
+                                &l_protocol_version, NULL, NULL,
+                                NULL, NULL)) {
+    SILC_LOG_ERROR(("%s (%s) %s is not allowed/supported version", 
+                   ske->sock->hostname, ske->sock->ip, version));
+    return SILC_SKE_STATUS_BAD_VERSION;
   }
 
-  if (maj != maj2)
-    status = SILC_SKE_STATUS_BAD_VERSION;
-
-  if (status == SILC_SKE_STATUS_BAD_VERSION)
+  /* If remote is too new, don't connect */
+  if (l_protocol_version < r_protocol_version) {
     SILC_LOG_ERROR(("%s (%s) %s is not allowed/supported version", 
                    ske->sock->hostname, ske->sock->ip, version));
+    return SILC_SKE_STATUS_BAD_VERSION;
+  }
+
+  ske->sock->version = r_protocol_version;
 
-  return status;
+  return SILC_SKE_STATUS_OK;
 }
 
 /* Callback that is called by the SKE to indicate that it is safe to
@@ -613,7 +591,7 @@ SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
        */
       SilcSKEKeyMaterial *keymat;
       int key_len = silc_cipher_get_key_len(ctx->ske->prop->cipher);
-      int hash_len = ctx->ske->prop->hash->hash->hash_len;
+      int hash_len = silc_hash_len(ctx->ske->prop->hash);
 
       /* Process the key material */
       keymat = silc_calloc(1, sizeof(*keymat));
@@ -713,10 +691,10 @@ SILC_TASK_CALLBACK(silc_server_protocol_key_exchange)
  */
 
 static int 
-silc_server_password_authentication(SilcServer server, char *remote_auth, 
-                                   char *local_auth)
+silc_server_password_authentication(SilcServer server, char *local_auth, 
+                                   char *remote_auth)
 {
-  if (!remote_auth || !local_auth)
+  if (!remote_auth || !local_auth || strlen(local_auth) != strlen(remote_auth))
     return FALSE;
 
   if (!memcmp(remote_auth, local_auth, strlen(local_auth)))
@@ -1259,7 +1237,7 @@ void silc_server_protocol_rekey_generate(SilcServer server,
   SilcIDListData idata = (SilcIDListData)ctx->sock->user_data;
   SilcSKEKeyMaterial *keymat;
   SilcUInt32 key_len = silc_cipher_get_key_len(idata->send_key);
-  SilcUInt32 hash_len = idata->hash->hash->hash_len;
+  SilcUInt32 hash_len = silc_hash_len(idata->hash);
 
   SILC_LOG_DEBUG(("Generating new %s session keys (no PFS)",
                  send ? "sending" : "receiving"));
@@ -1288,7 +1266,7 @@ silc_server_protocol_rekey_generate_pfs(SilcServer server,
   SilcIDListData idata = (SilcIDListData)ctx->sock->user_data;
   SilcSKEKeyMaterial *keymat;
   SilcUInt32 key_len = silc_cipher_get_key_len(idata->send_key);
-  SilcUInt32 hash_len = idata->hash->hash->hash_len;
+  SilcUInt32 hash_len = silc_hash_len(idata->hash);
   unsigned char *tmpbuf;
   SilcUInt32 klen;
 
@@ -1367,9 +1345,11 @@ SILC_TASK_CALLBACK(silc_server_protocol_rekey)
           */
 
          if (ctx->packet->type != SILC_PACKET_KEY_EXCHANGE_1) {
-           SILC_LOG_ERROR(("Error during Re-key (PFS): re-key state is "
-                           "incorrect (received %d, expected %d packet)",
-                           ctx->packet->type, SILC_PACKET_KEY_EXCHANGE_1));
+           SILC_LOG_ERROR(("Error during Re-key (R PFS): re-key state is "
+                           "incorrect (received %d, expected %d packet), "
+                           "with %s (%s)", ctx->packet->type, 
+                           SILC_PACKET_KEY_EXCHANGE_1, ctx->sock->hostname,
+                           ctx->sock->ip));
            protocol->state = SILC_PROTOCOL_STATE_ERROR;
            silc_protocol_execute(protocol, server->schedule, 0, 300000);
            return;
@@ -1387,8 +1367,9 @@ SILC_TASK_CALLBACK(silc_server_protocol_rekey)
       
          status = silc_ske_responder_phase_2(ctx->ske, ctx->packet->buffer);
          if (status != SILC_SKE_STATUS_OK) {
-           SILC_LOG_ERROR(("Error (%s) during Re-key (PFS)",
-                           silc_ske_map_status(status)));
+           SILC_LOG_ERROR(("Error (%s) during Re-key (R PFS), with %s (%s)",
+                           silc_ske_map_status(status), ctx->sock->hostname,
+                           ctx->sock->ip));
            protocol->state = SILC_PROTOCOL_STATE_ERROR;
            silc_protocol_execute(protocol, server->schedule, 0, 300000);
            return;
@@ -1441,8 +1422,9 @@ SILC_TASK_CALLBACK(silc_server_protocol_rekey)
       
          status = silc_ske_initiator_phase_2(ctx->ske, NULL, NULL, 0);
          if (status != SILC_SKE_STATUS_OK) {
-           SILC_LOG_ERROR(("Error (%s) during Re-key (PFS)",
-                           silc_ske_map_status(status)));
+           SILC_LOG_ERROR(("Error (%s) during Re-key (I PFS), with %s (%s)",
+                           silc_ske_map_status(status), ctx->sock->hostname,
+                           ctx->sock->ip));
            protocol->state = SILC_PROTOCOL_STATE_ERROR;
            silc_protocol_execute(protocol, server->schedule, 0, 300000);
            return;
@@ -1485,8 +1467,9 @@ SILC_TASK_CALLBACK(silc_server_protocol_rekey)
        status = silc_ske_responder_finish(ctx->ske, NULL, NULL, 
                                           SILC_SKE_PK_TYPE_SILC);
        if (status != SILC_SKE_STATUS_OK) {
-         SILC_LOG_ERROR(("Error (%s) during Re-key (PFS)",
-                         silc_ske_map_status(status)));
+         SILC_LOG_ERROR(("Error (%s) during Re-key (R PFS), with %s (%s)",
+                         silc_ske_map_status(status), ctx->sock->hostname,
+                         ctx->sock->ip));
          protocol->state = SILC_PROTOCOL_STATE_ERROR;
          silc_protocol_execute(protocol, server->schedule, 0, 300000);
          return;
@@ -1499,9 +1482,11 @@ SILC_TASK_CALLBACK(silc_server_protocol_rekey)
         * The packet type must be KE packet
         */
        if (ctx->packet->type != SILC_PACKET_KEY_EXCHANGE_2) {
-         SILC_LOG_ERROR(("Error during Re-key (PFS): re-key state is "
-                         "incorrect (received %d, expected %d packet)",
-                         ctx->packet->type, SILC_PACKET_KEY_EXCHANGE_2));
+         SILC_LOG_ERROR(("Error during Re-key (I PFS): re-key state is "
+                         "incorrect (received %d, expected %d packet), "
+                         "with %s (%s)", ctx->packet->type, 
+                         SILC_PACKET_KEY_EXCHANGE_2, ctx->sock->hostname,
+                         ctx->sock->ip));
          protocol->state = SILC_PROTOCOL_STATE_ERROR;
          silc_protocol_execute(protocol, server->schedule, 0, 300000);
          return;
@@ -1509,8 +1494,9 @@ SILC_TASK_CALLBACK(silc_server_protocol_rekey)
        
        status = silc_ske_initiator_finish(ctx->ske, ctx->packet->buffer);
        if (status != SILC_SKE_STATUS_OK) {
-         SILC_LOG_ERROR(("Error (%s) during Re-key (PFS)",
-                         silc_ske_map_status(status)));
+         SILC_LOG_ERROR(("Error (%s) during Re-key (I PFS), with %s (%s)",
+                         silc_ske_map_status(status), ctx->sock->hostname,
+                         ctx->sock->ip));
          protocol->state = SILC_PROTOCOL_STATE_ERROR;
          silc_protocol_execute(protocol, server->schedule, 0, 300000);
          return;
@@ -1538,9 +1524,11 @@ SILC_TASK_CALLBACK(silc_server_protocol_rekey)
      */
 
     if (ctx->packet->type != SILC_PACKET_REKEY_DONE) {
-      SILC_LOG_ERROR(("Error during Re-key (PFS): re-key state is "
-                     "incorrect (received %d, expected %d packet)",
-                     ctx->packet->type, SILC_PACKET_REKEY_DONE));
+      SILC_LOG_ERROR(("Error during Re-key (%s PFS): re-key state is "
+                     "incorrect (received %d, expected %d packet), "
+                     "with %s (%s)", ctx->responder ? "R" : "I",
+                     ctx->packet->type, SILC_PACKET_REKEY_DONE,
+                     ctx->sock->hostname, ctx->sock->ip));
       protocol->state = SILC_PROTOCOL_STATE_ERROR;
       silc_protocol_execute(protocol, server->schedule, 0, 300000);
       return;
@@ -1548,7 +1536,10 @@ SILC_TASK_CALLBACK(silc_server_protocol_rekey)
 
     /* We received the REKEY_DONE packet and all packets after this is
        encrypted with the new key so set the decryption key to the new key */
-    silc_server_protocol_rekey_generate(server, ctx, FALSE);
+    if (ctx->pfs == TRUE)
+      silc_server_protocol_rekey_generate_pfs(server, ctx, FALSE);
+    else
+      silc_server_protocol_rekey_generate(server, ctx, FALSE);
 
     /* Assure that after calling final callback there cannot be pending
        executions for this protocol anymore. This just unregisters any