Fixed to not allow duplicate PKCS registering. Bug #17.
[silc.git] / lib / silccrypt / silcpkcs.c
index 8d409e35ec8792198c54171d0a362ca500b6b79f..e079e40aa4be939b0bc7fe0cdd5dd41b4703425e 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;
@@ -65,6 +71,16 @@ bool silc_pkcs_register(const SilcPKCSObject *pkcs)
 
   SILC_LOG_DEBUG(("Registering new PKCS `%s'", pkcs->name));
 
+  /* Check if exists already */
+  if (silc_pkcs_list) {
+    SilcPKCSObject *entry;
+    silc_dlist_start(silc_pkcs_list);
+    while ((entry = silc_dlist_get(silc_pkcs_list)) != SILC_LIST_END) {
+      if (!strcmp(entry->name, pkcs->name))
+        return FALSE;
+    }
+  }
+
   new = silc_calloc(1, sizeof(*new));
   new->name = strdup(pkcs->name);
   new->init = pkcs->init;
@@ -104,6 +120,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);
@@ -134,6 +152,24 @@ bool silc_pkcs_register_default(void)
   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 +204,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 +303,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 +401,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 +425,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);
 
@@ -406,17 +446,16 @@ char *silc_pkcs_encode_identifier(char *username, char *host, char *realname,
   SilcBuffer buf;
   char *identifier;
   SilcUInt32 len, tlen = 0;
-  char utf8[256 + 1];
 
   if (!username || !host)
     return NULL;
 
-  len = (username ? silc_utf8_encoded_len(username, strlen(username), 0) : 0) +
-       (host     ? silc_utf8_encoded_len(host, strlen(host), 0) : 0) +
-       (realname ? silc_utf8_encoded_len(realname, strlen(realname), 0) : 0) +
-       (email    ? silc_utf8_encoded_len(email, strlen(email), 0) : 0) +
-       (org      ? silc_utf8_encoded_len(org, strlen(org), 0) : 0) +
-       (country  ? silc_utf8_encoded_len(country, strlen(country), 0) : 0);
+  len = (username ? strlen(username) : 0) +
+       (host     ? strlen(host)     : 0) +
+       (realname ? strlen(realname) : 0) +
+       (email    ? strlen(email)    : 0) +
+       (org      ? strlen(org)      : 0) +
+       (country  ? strlen(country)  : 0);
   
   if (len < 3)
     return NULL;
@@ -426,77 +465,62 @@ char *silc_pkcs_encode_identifier(char *username, char *host, char *realname,
   silc_buffer_pull_tail(buf, len);
 
   if (username) {
-    memset(utf8, 0, sizeof(utf8));
-    len = silc_utf8_encode(username, strlen(username), 0, utf8,
-                          sizeof(utf8) - 1);
     silc_buffer_format(buf,
                       SILC_STR_UI32_STRING("UN="),
-                      SILC_STR_UI32_STRING(utf8),
+                      SILC_STR_UI32_STRING(username),
                       SILC_STR_END);
-    silc_buffer_pull(buf, 3 + len);
-    tlen = 3 + len
+    silc_buffer_pull(buf, 3 + strlen(username));
+    tlen = 3 + strlen(username)
   }
     
   if (host) {
-    memset(utf8, 0, sizeof(utf8));
-    len = silc_utf8_encode(host, strlen(host), 0, utf8, sizeof(utf8) - 1);
     silc_buffer_format(buf,
                       SILC_STR_UI32_STRING(", "),
                       SILC_STR_UI32_STRING("HN="),
-                      SILC_STR_UI32_STRING(utf8),
+                      SILC_STR_UI32_STRING(host),
                       SILC_STR_END);
-    silc_buffer_pull(buf, 5 + len);
-    tlen += 5 + len
+    silc_buffer_pull(buf, 5 + strlen(host));
+    tlen += 5 + strlen(host)
   }
 
   if (realname) {
-    memset(utf8, 0, sizeof(utf8));
-    len = silc_utf8_encode(realname, strlen(realname), 0, utf8,
-                          sizeof(utf8) - 1);
     silc_buffer_format(buf,
                       SILC_STR_UI32_STRING(", "),
                       SILC_STR_UI32_STRING("RN="),
-                      SILC_STR_UI32_STRING(utf8),
+                      SILC_STR_UI32_STRING(realname),
                       SILC_STR_END);
-    silc_buffer_pull(buf, 5 + len);
-    tlen += 5 + len
+    silc_buffer_pull(buf, 5 + strlen(realname));
+    tlen += 5 + strlen(realname)
   }
 
   if (email) {
-    memset(utf8, 0, sizeof(utf8));
-    len = silc_utf8_encode(email, strlen(email), 0, utf8, sizeof(utf8) - 1);
     silc_buffer_format(buf,
                       SILC_STR_UI32_STRING(", "),
                       SILC_STR_UI32_STRING("E="),
-                      SILC_STR_UI32_STRING(utf8),
+                      SILC_STR_UI32_STRING(email),
                       SILC_STR_END);
-    silc_buffer_pull(buf, 4 + len);
-    tlen += 4 + len
+    silc_buffer_pull(buf, 4 + strlen(email));
+    tlen += 4 + strlen(email)
   }
 
   if (org) {
-    memset(utf8, 0, sizeof(utf8));
-    len = silc_utf8_encode(org, strlen(org), 0, utf8, sizeof(utf8) - 1);
     silc_buffer_format(buf,
                       SILC_STR_UI32_STRING(", "),
                       SILC_STR_UI32_STRING("O="),
-                      SILC_STR_UI32_STRING(utf8),
+                      SILC_STR_UI32_STRING(org),
                       SILC_STR_END);
-    silc_buffer_pull(buf, 4 + len);
-    tlen += 4 + len
+    silc_buffer_pull(buf, 4 + strlen(org));
+    tlen += 4 + strlen(org)
   }
 
   if (country) {
-    memset(utf8, 0, sizeof(utf8));
-    len = silc_utf8_encode(country, strlen(country), 0, utf8,
-                          sizeof(utf8) - 1);
     silc_buffer_format(buf,
                       SILC_STR_UI32_STRING(", "),
                       SILC_STR_UI32_STRING("C="),
-                      SILC_STR_UI32_STRING(utf8),
+                      SILC_STR_UI32_STRING(country),
                       SILC_STR_END);
-    silc_buffer_pull(buf, 4 + len);
-    tlen += 4 + len
+    silc_buffer_pull(buf, 4 + strlen(country));
+    tlen += 4 + strlen(country)
   }
 
   silc_buffer_push(buf, buf->data - buf->head);
@@ -576,20 +600,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;
 }
 
@@ -608,7 +643,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;
@@ -817,6 +853,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 *