Added stacktrace support with --enable-stack-trace option.
[silc.git] / lib / silccrypt / silcpkcs.c
index d61ed0c826db0ca0e7933954661cc7bd36acd35b..d9662663de4bae0e377b9daf27482ebd0cc4fddc 100644 (file)
@@ -1,16 +1,15 @@
 /*
 
-  silcpkcs.c
+  silcpkcs.c 
 
-  Author: Pekka Riikonen <priikone@poseidon.pspt.fi>
+  Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2001 Pekka Riikonen
+  Copyright (C) 1997 - 2002 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
-  the Free Software Foundation; either version 2 of the License, or
-  (at your option) any later version.
-  
+  the Free Software Foundation; version 2 of the License.
+
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 #include "rsa.h"
 #include "pkcs1.h"
 
+/* The main SILC PKCS structure. */
+struct SilcPKCSStruct {
+  void *context;
+  SilcPKCSObject *pkcs;
+  SilcUInt32 key_len;
+};
+
 #ifndef SILC_EPOC
 /* Dynamically registered list of PKCS. */
 SilcDList silc_pkcs_list = NULL;
@@ -58,7 +64,7 @@ const SilcPKCSObject silc_default_pkcs[] =
 /* Register a new PKCS into SILC. This is used at the initialization of
    the SILC. */
 
-bool silc_pkcs_register(SilcPKCSObject *pkcs)
+bool silc_pkcs_register(const SilcPKCSObject *pkcs)
 {
 #ifndef SILC_EPOC
   SilcPKCSObject *new;
@@ -104,6 +110,8 @@ bool silc_pkcs_unregister(SilcPKCSObject *pkcs)
   while ((entry = silc_dlist_get(silc_pkcs_list)) != SILC_LIST_END) {
     if (pkcs == SILC_ALL_PKCS || entry == pkcs) {
       silc_dlist_del(silc_pkcs_list, entry);
+      silc_free(entry->name);
+      silc_free(entry);
 
       if (silc_dlist_count(silc_pkcs_list) == 0) {
        silc_dlist_uninit(silc_pkcs_list);
@@ -128,12 +136,30 @@ bool silc_pkcs_register_default(void)
   int i;
 
   for (i = 0; silc_default_pkcs[i].name; i++)
-    silc_pkcs_register((SilcPKCSObject *)&(silc_default_pkcs[i]));
+    silc_pkcs_register(&(silc_default_pkcs[i]));
 
 #endif /* SILC_EPOC */
   return TRUE;
 }
 
+bool silc_pkcs_unregister_all(void)
+{
+#ifndef SILC_EPOC
+  SilcPKCSObject *entry;
+
+  if (!silc_pkcs_list)
+    return FALSE;
+
+  silc_dlist_start(silc_pkcs_list);
+  while ((entry = silc_dlist_get(silc_pkcs_list)) != SILC_LIST_END) {
+    silc_pkcs_unregister(entry);
+    if (!silc_pkcs_list)
+      break;
+  }
+#endif /* SILC_EPOC */
+  return TRUE;
+}
+
 /* Allocates a new SilcPKCS object. The new allocated object is returned
    to the 'new_pkcs' argument. */
 
@@ -168,7 +194,6 @@ bool silc_pkcs_alloc(const unsigned char *name, SilcPKCS *new_pkcs)
     *new_pkcs = silc_calloc(1, sizeof(**new_pkcs));
     (*new_pkcs)->pkcs = entry;
     (*new_pkcs)->context = silc_calloc(1, entry->context_len());
-    (*new_pkcs)->get_key_len = silc_pkcs_get_key_len;
     return TRUE;
   }
 
@@ -268,6 +293,11 @@ SilcUInt32 silc_pkcs_get_key_len(SilcPKCS pkcs)
   return pkcs->key_len;
 }
 
+const char *silc_pkcs_get_name(SilcPKCS pkcs)
+{
+  return pkcs->pkcs->name;
+}
+
 /* Returns SILC style public key */
 
 unsigned char *silc_pkcs_get_public_key(SilcPKCS pkcs, SilcUInt32 *len)
@@ -361,7 +391,7 @@ int silc_pkcs_sign_with_hash(SilcPKCS pkcs, SilcHash hash,
   int ret;
 
   silc_hash_make(hash, src, src_len, hashr);
-  hash_len = hash->hash->hash_len;
+  hash_len = silc_hash_len(hash);
 
   SILC_LOG_HEXDUMP(("Hash"), hashr, hash_len);
 
@@ -385,7 +415,7 @@ int silc_pkcs_verify_with_hash(SilcPKCS pkcs, SilcHash hash,
   int ret;
 
   silc_hash_make(hash, data, data_len, hashr);
-  hash_len = hash->hash->hash_len;
+  hash_len = silc_hash_len(hash);
 
   SILC_LOG_HEXDUMP(("Hash"), hashr, hash_len);
 
@@ -560,20 +590,31 @@ void silc_pkcs_free_identifier(SilcPublicKeyIdentifier identifier)
 /* Allocates SILC style public key formed from sent arguments. All data
    is duplicated. */
 
-SilcPublicKey silc_pkcs_public_key_alloc(char *name, char *identifier,
-                                        unsigned char *pk, 
+SilcPublicKey silc_pkcs_public_key_alloc(const char *name, 
+                                        const char *identifier,
+                                        const unsigned char *pk, 
                                         SilcUInt32 pk_len)
 {
   SilcPublicKey public_key;
+  char *tmp = NULL;
 
   public_key = silc_calloc(1, sizeof(*public_key));
-  public_key->len = 4 + 2 + strlen(name) + 2 + strlen(identifier) + pk_len;
   public_key->name = strdup(name);
-  public_key->identifier = strdup(identifier);
   public_key->pk_len = pk_len;
   public_key->pk = silc_calloc(pk_len, sizeof(*public_key->pk));
   memcpy(public_key->pk, pk, pk_len);
 
+  if (!silc_utf8_valid(identifier, strlen(identifier))) {
+    int len = silc_utf8_encoded_len(identifier, strlen(identifier), 0);
+    tmp = silc_calloc(len + 1, sizeof(*tmp));
+    silc_utf8_encode(identifier, strlen(identifier), 0, tmp, len);
+    identifier = tmp;
+  }
+
+  public_key->identifier = strdup(identifier);
+  public_key->len = 4 + 2 + strlen(name) + 2 + strlen(identifier) + pk_len;
+  silc_free(tmp);
+
   return public_key;
 }
 
@@ -592,7 +633,8 @@ void silc_pkcs_public_key_free(SilcPublicKey public_key)
 /* Allocates SILC private key formed from sent arguments. All data is
    duplicated. */
 
-SilcPrivateKey silc_pkcs_private_key_alloc(char *name, unsigned char *prv,
+SilcPrivateKey silc_pkcs_private_key_alloc(const char *name,
+                                          const unsigned char *prv,
                                           SilcUInt32 prv_len)
 {
   SilcPrivateKey private_key;
@@ -801,6 +843,25 @@ bool silc_pkcs_public_key_compare(SilcPublicKey key1, SilcPublicKey key2)
   return FALSE;
 }
 
+/* Copies the public key indicated by `public_key' and returns new allocated
+   public key which is indentical to the `public_key'. */
+
+SilcPublicKey silc_pkcs_public_key_copy(SilcPublicKey public_key)
+{
+  SilcPublicKey key = silc_calloc(1, sizeof(*key));
+  if (!key)
+    return NULL;
+
+  key->len = public_key->len;
+  key->name = silc_memdup(public_key->name, strlen(public_key->name));
+  key->identifier = silc_memdup(public_key->identifier,
+                               strlen(public_key->identifier));
+  key->pk = silc_memdup(public_key->pk, public_key->pk_len);
+  key->pk_len = public_key->pk_len;
+
+  return key;
+}
+
 /* Encodes SILC private key from SilcPrivateKey. Returns the encoded data. */
 
 unsigned char *
@@ -945,7 +1006,7 @@ static int silc_pkcs_save_public_key_internal(char *filename,
   case SILC_PKCS_FILE_BIN:
     break;
   case SILC_PKCS_FILE_PEM:
-    data = silc_encode_pem_file(data, data_len);
+    data = silc_pem_encode_file(data, data_len);
     data_len = strlen(data);
     break;
   }
@@ -1008,7 +1069,7 @@ static int silc_pkcs_save_private_key_internal(char *filename,
   case SILC_PKCS_FILE_BIN:
     break;
   case SILC_PKCS_FILE_PEM:
-    data = silc_encode_pem_file(data, data_len);
+    data = silc_pem_encode_file(data, data_len);
     data_len = strlen(data);
     break;
   }
@@ -1097,7 +1158,7 @@ int silc_pkcs_load_public_key(char *filename, SilcPublicKey *public_key,
     case SILC_PKCS_FILE_BIN:
       break;
     case SILC_PKCS_FILE_PEM:
-      data = silc_decode_pem(data, len, &len);
+      data = silc_pem_decode(data, len, &len);
       memset(old, 0, data_len);
       silc_free(old);
       old = data; 
@@ -1154,7 +1215,7 @@ int silc_pkcs_load_private_key(char *filename, SilcPrivateKey *private_key,
     case SILC_PKCS_FILE_BIN:
       break;
     case SILC_PKCS_FILE_PEM:
-      data = silc_decode_pem(data, len, &len);
+      data = silc_pem_decode(data, len, &len);
       break;
     }