updates. New data types.
[silc.git] / apps / silc / clientconfig.c
index b6f5b150a832b58c814b063752ea0ff355e1660a..0bd31ce8faaddd545fe94f018e19aae6f11cc125 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
@@ -32,6 +32,8 @@ SilcClientConfigSection silc_client_config_sections[] = {
     SILC_CLIENT_CONFIG_SECTION_TYPE_PKCS, 2 },
   { "[hash]", 
     SILC_CLIENT_CONFIG_SECTION_TYPE_HASH_FUNCTION, 4 },
+  { "[hmac]", 
+    SILC_CLIENT_CONFIG_SECTION_TYPE_HMAC, 3 },
   { "[connection]", 
     SILC_CLIENT_CONFIG_SECTION_TYPE_CONNECTION, 4 },
   { "[commands]", 
@@ -95,7 +97,7 @@ int silc_client_config_parse(SilcClientConfig config, SilcBuffer buffer,
                             SilcClientConfigParse *return_config)
 {
   int i, begin;
-  unsigned int linenum;
+  uint32 linenum;
   char line[1024], *cp;
   SilcClientConfigSection *cptr = NULL;
   SilcClientConfigParse parse = *return_config, first = NULL;
@@ -272,28 +274,28 @@ int silc_client_config_parse_lines(SilcClientConfig config,
       if (ret < 0)
        break;
 
-      /* Get block length */
+      /* Get key length */
       ret = silc_config_get_token(line, &tmp);
       if (ret < 0)
        break;
       if (ret == 0) {
-       fprintf(stderr, "%s:%d: Cipher block length not defined\n",
+       fprintf(stderr, "%s:%d: Cipher key length not defined\n",
                config->filename, pc->linenum);
        break;
       }
-      config->cipher->block_len = atoi(tmp);
+      config->cipher->key_len = atoi(tmp);
       silc_free(tmp);
 
-      /* Get key length */
+      /* Get block length */
       ret = silc_config_get_token(line, &tmp);
       if (ret < 0)
        break;
       if (ret == 0) {
-       fprintf(stderr, "%s:%d: Cipher key length not defined\n",
+       fprintf(stderr, "%s:%d: Cipher block length not defined\n",
                config->filename, pc->linenum);
        break;
       }
-      config->cipher->key_len = atoi(tmp);
+      config->cipher->block_len = atoi(tmp);
       silc_free(tmp);
 
       check = TRUE;
@@ -399,6 +401,57 @@ int silc_client_config_parse_lines(SilcClientConfig config,
       check = TRUE;
       break;
 
+    case SILC_CLIENT_CONFIG_SECTION_TYPE_HMAC:
+
+      if (!config->hmac) {
+       config->hmac = silc_calloc(1, sizeof(*config->hmac));
+       config->hmac->next = NULL;
+       config->hmac->prev = NULL;
+      } else {
+       if (!config->hmac->next) {
+         config->hmac->next = 
+           silc_calloc(1, sizeof(*config->hmac->next));
+         config->hmac->next->next = NULL;
+         config->hmac->next->prev = config->hmac;
+         config->hmac = config->hmac->next;
+       }
+      }
+
+      /* Get HMAC name */
+      ret = silc_config_get_token(line, &config->hmac->alg_name);
+      if (ret < 0)
+       break;
+      if (ret == 0) {
+       fprintf(stderr, "%s:%d: HMAC name not defined\n",
+               config->filename, pc->linenum);
+       break;
+      }
+      
+      /* Get Hash function name */
+      ret = silc_config_get_token(line, &config->hmac->sim_name);
+      if (ret < 0)
+       break;
+      if (ret == 0) {
+       fprintf(stderr, "%s:%d: Hash function name not defined\n",
+               config->filename, pc->linenum);
+       break;
+      }
+
+      /* Get MAC length */
+      ret = silc_config_get_token(line, &tmp);
+      if (ret < 0)
+       break;
+      if (ret == 0) {
+       fprintf(stderr, "%s:%d: HMAC's MAC length not defined\n",
+               config->filename, pc->linenum);
+       break;
+      }
+      config->hmac->key_len = atoi(tmp);
+      silc_free(tmp);
+
+      check = TRUE;
+      break;
+
     case SILC_CLIENT_CONFIG_SECTION_TYPE_CONNECTION:
 
       if (!config->conns) {
@@ -435,10 +488,10 @@ int silc_client_config_parse_lines(SilcClientConfig config,
        }
 
        if (!strcmp(tmp, SILC_CLIENT_CONFIG_AUTH_METH_PASSWD))
-         config->conns->auth_meth = SILC_PROTOCOL_CONN_AUTH_PASSWORD;
+         config->conns->auth_meth = SILC_AUTH_PASSWORD;
 
        if (!strcmp(tmp, SILC_CLIENT_CONFIG_AUTH_METH_PUBKEY))
-         config->conns->auth_meth = SILC_PROTOCOL_CONN_AUTH_PUBLIC_KEY;
+         config->conns->auth_meth = SILC_AUTH_PUBLIC_KEY;
 
        silc_free(tmp);
       }
@@ -513,6 +566,8 @@ int silc_client_config_parse_lines(SilcClientConfig config,
     config->pkcs = config->pkcs->prev;
   while (config->hash_func && config->hash_func->prev)
     config->hash_func = config->hash_func->prev;
+  while (config->hmac && config->hmac->prev)
+    config->hmac = config->hmac->prev;
   while (config->conns && config->conns->prev)
     config->conns = config->conns->prev;
   while (config->commands && config->commands->prev)
@@ -554,6 +609,7 @@ void silc_client_config_register_ciphers(SilcClientConfig config)
       /* Load (try at least) the crypto SIM module */
       SilcCipherObject cipher;
       SilcSimContext *sim;
+      char *alg_name;
 
       memset(&cipher, 0, sizeof(cipher));
       cipher.name = alg->alg_name;
@@ -564,25 +620,29 @@ void silc_client_config_register_ciphers(SilcClientConfig config)
       sim->type = SILC_SIM_CIPHER;
       sim->libname = alg->sim_name;
 
+      alg_name = strdup(alg->alg_name);
+      if (strchr(alg_name, '-'))
+       *strchr(alg_name, '-') = '\0';
+
       if ((silc_sim_load(sim))) {
        cipher.set_key = 
-         silc_sim_getsym(sim, silc_sim_symname(alg->alg_name, 
+         silc_sim_getsym(sim, silc_sim_symname(alg_name, 
                                                SILC_CIPHER_SIM_SET_KEY));
        SILC_LOG_DEBUG(("set_key=%p", cipher.set_key));
        cipher.set_key_with_string = 
-         silc_sim_getsym(sim, silc_sim_symname(alg->alg_name, 
+         silc_sim_getsym(sim, silc_sim_symname(alg_name, 
                                                SILC_CIPHER_SIM_SET_KEY_WITH_STRING));
        SILC_LOG_DEBUG(("set_key_with_string=%p", cipher.set_key_with_string));
        cipher.encrypt = 
-         silc_sim_getsym(sim, silc_sim_symname(alg->alg_name,
+         silc_sim_getsym(sim, silc_sim_symname(alg_name,
                                                SILC_CIPHER_SIM_ENCRYPT_CBC));
        SILC_LOG_DEBUG(("encrypt_cbc=%p", cipher.encrypt));
         cipher.decrypt = 
-         silc_sim_getsym(sim, silc_sim_symname(alg->alg_name,
+         silc_sim_getsym(sim, silc_sim_symname(alg_name,
                                                SILC_CIPHER_SIM_DECRYPT_CBC));
        SILC_LOG_DEBUG(("decrypt_cbc=%p", cipher.decrypt));
         cipher.context_len = 
-         silc_sim_getsym(sim, silc_sim_symname(alg->alg_name,
+         silc_sim_getsym(sim, silc_sim_symname(alg_name,
                                                SILC_CIPHER_SIM_CONTEXT_LEN));
        SILC_LOG_DEBUG(("context_len=%p", cipher.context_len));
 
@@ -592,6 +652,8 @@ void silc_client_config_register_ciphers(SilcClientConfig config)
                                   (app->sim_count + 1));
        app->sim[app->sim_count] = sim;
        app->sim_count++;
+
+       silc_free(alg_name);
       } else {
        SILC_LOG_ERROR(("Error configuring ciphers"));
        silc_client_stop(client);
@@ -635,7 +697,7 @@ void silc_client_config_register_pkcs(SilcClientConfig config)
   }
 }
 
-/* Registers configured hash functions. These can then be allocated by the
+/* Registers configured hash funtions. These can then be allocated by the
    client when needed. */
 
 void silc_client_config_register_hashfuncs(SilcClientConfig config)
@@ -648,74 +710,57 @@ void silc_client_config_register_hashfuncs(SilcClientConfig config)
 
   alg = config->hash_func;
   while(alg) {
-
     if (!alg->sim_name) {
-      /* Hash module is supposed to be built in. Nothing to be done
-        here except to test that the hash function really is built in. */
-      SilcHash tmp = NULL;
-
-      if (silc_hash_alloc(alg->alg_name, &tmp) == FALSE) {
-       SILC_LOG_ERROR(("Unsupported hash function `%s'", alg->alg_name));
+      if (!silc_hash_is_supported(alg->alg_name)) {
+       SILC_LOG_ERROR(("Unsupported hash function `%s'", 
+                       alg->alg_name));
        silc_client_stop(client);
        exit(1);
       }
-      silc_free(tmp);
-
-#ifdef SILC_SIM
-    } else {
-      /* Load (try at least) the hash SIM module */
-      SilcHashObject hash;
-      SilcSimContext *sim;
+    }
+    alg = alg->next;
+  }
+}
 
-      memset(&hash, 0, sizeof(hash));
-      hash.name = alg->alg_name;
-      hash.block_len = alg->block_len;
-      hash.hash_len = alg->key_len;
+/* Registers configured HMACs. These can then be allocated by the
+   client when needed. */
 
-      sim = silc_sim_alloc();
-      sim->type = SILC_SIM_HASH;
-      sim->libname = alg->sim_name;
+void silc_client_config_register_hmacs(SilcClientConfig config)
+{
+  SilcClientConfigSectionAlg *alg;
+  SilcClientInternal app = (SilcClientInternal)config->client;
+  SilcClient client = app->client;
 
-      if ((silc_sim_load(sim))) {
-       hash.init = 
-         silc_sim_getsym(sim, silc_sim_symname(alg->alg_name, 
-                                               SILC_HASH_SIM_INIT));
-       SILC_LOG_DEBUG(("init=%p", hash.init));
-       hash.update = 
-         silc_sim_getsym(sim, silc_sim_symname(alg->alg_name,
-                                               SILC_HASH_SIM_UPDATE));
-       SILC_LOG_DEBUG(("update=%p", hash.update));
-        hash.final = 
-         silc_sim_getsym(sim, silc_sim_symname(alg->alg_name,
-                                               SILC_HASH_SIM_FINAL));
-       SILC_LOG_DEBUG(("final=%p", hash.final));
-        hash.context_len = 
-         silc_sim_getsym(sim, silc_sim_symname(alg->alg_name,
-                                               SILC_HASH_SIM_CONTEXT_LEN));
-       SILC_LOG_DEBUG(("context_len=%p", hash.context_len));
+  SILC_LOG_DEBUG(("Registering configured HMACs"));
 
-       /* Put the SIM to the table of all SIM's in client */
-       app->sim = silc_realloc(app->sim,
-                                  sizeof(*app->sim) * 
-                                  (app->sim_count + 1));
-       app->sim[app->sim_count] = sim;
-       app->sim_count++;
-      } else {
-       SILC_LOG_ERROR(("Error configuring hash functions"));
-       silc_client_stop(client);
-       exit(1);
-      }
+  if (!config->hmac) {
+    SILC_LOG_ERROR(("HMACs are not configured. SILC cannot work without "
+                   "HMACs"));
+    silc_client_stop(client);
+    exit(1);
+  }
 
-      /* Register the cipher */
-      silc_hash_register(&hash);
-#endif
+  alg = config->hmac;
+  while(alg) {
+    SilcHmacObject hmac;
+    
+    if (!silc_hash_is_supported(alg->sim_name)) {
+      SILC_LOG_ERROR(("Unsupported hash function `%s'", 
+                     alg->sim_name));
+      silc_client_stop(client);
+      exit(1);
     }
+    
+    /* Register the HMAC */
+    memset(&hmac, 0, sizeof(hmac));
+    hmac.name = alg->alg_name;
+    hmac.len = alg->key_len;
+    silc_hmac_register(&hmac);
 
     alg = alg->next;
   }
 }
 
-
 SilcClientConfigSectionConnection *
 silc_client_config_find_connection(SilcClientConfig config, 
                                   char *host, int port)