Merge branch 'topic/mm-fixes' of git://208.110.73.182/silc into silc.1.1.branch
[silc.git] / lib / silccrypt / silcpk.c
index a485327f41f14c2b235b52d7eff9f5a65041eaec..e4a000788c79f71fdae9da4e7516470d498d612f 100644 (file)
@@ -124,7 +124,7 @@ SilcBool silc_pkcs_silc_decode_identifier(const char *identifier,
   int len;
 
   /* Protocol says that at least UN and HN must be provided as identifier */
-  if (!strstr(identifier, "UN=") && !strstr(identifier, "HN=")) {
+  if (!strstr(identifier, "UN=") || !strstr(identifier, "HN=")) {
     SILC_LOG_DEBUG(("The public does not have the required UN= and HN= "
                    "identifiers"));
     return FALSE;
@@ -206,7 +206,7 @@ char *silc_pkcs_silc_encode_identifier(char *username, char *host,
 
   if (!username || !host)
     return NULL;
-  if (strlen(username) < 3 || strlen(host) < 3)
+  if (strlen(username) < 1 || strlen(host) < 1)
     return NULL;
 
   memset(&buf, 0, sizeof(buf));
@@ -323,11 +323,15 @@ SilcBool silc_pkcs_silc_import_public_key_file(unsigned char *filedata,
 
   /* Check start of file and remove header from the data. */
   len = strlen(SILC_PKCS_PUBLIC_KEYFILE_BEGIN);
-  if (filedata_len < len + strlen(SILC_PKCS_PUBLIC_KEYFILE_END))
+  if (filedata_len < len + strlen(SILC_PKCS_PUBLIC_KEYFILE_END)) {
+    SILC_LOG_ERROR(("Malformed SILC public key header"));
     return FALSE;
+  }
   for (i = 0; i < len; i++) {
-    if (*filedata != SILC_PKCS_PUBLIC_KEYFILE_BEGIN[i])
+    if (*filedata != SILC_PKCS_PUBLIC_KEYFILE_BEGIN[i]) {
+      SILC_LOG_ERROR(("Malformed SILC public key header"));
       return FALSE;
+    }
     filedata++;
   }
   filedata_len -= (strlen(SILC_PKCS_PUBLIC_KEYFILE_BEGIN) +
@@ -425,7 +429,7 @@ int silc_pkcs_silc_import_public_key(unsigned char *key,
     goto err;
 
   SILC_LOG_DEBUG(("Public key version %s",
-                 (!silc_pubkey->identifier.version ? " 1" :
+                 (!silc_pubkey->identifier.version ? "1" :
                   silc_pubkey->identifier.version)));
 
   if (!strcmp(pkcs_name, "rsa")) {
@@ -444,7 +448,7 @@ int silc_pkcs_silc_import_public_key(unsigned char *key,
       pkcs = silc_pkcs_find_algorithm(pkcs_name, "pkcs1");
     }
     if (!pkcs) {
-      SILC_LOG_DEBUG(("Unsupported PKCS algorithm"));
+      SILC_LOG_DEBUG(("Unsupported PKCS algorithm: rsa"));
       goto err;
     }
     silc_pubkey->pkcs = pkcs;
@@ -591,8 +595,10 @@ unsigned char *silc_pkcs_silc_export_public_key(void *public_key,
   /* Export PKCS algorithm public key */
   if (pkcs->export_public_key)
     pk = pkcs->export_public_key(silc_pubkey->public_key, &pk_len);
-  if (!pk)
+  if (!pk) {
+    SILC_LOG_ERROR(("Error exporting PKCS algorithm key"));
     return NULL;
+  }
   silc_buffer_set(&alg_key, pk, pk_len);
 
   /* Encode identifier */
@@ -604,8 +610,10 @@ unsigned char *silc_pkcs_silc_export_public_key(void *public_key,
                                     silc_pubkey->identifier.org,
                                     silc_pubkey->identifier.country,
                                     silc_pubkey->identifier.version);
-  if (!identifier)
+  if (!identifier) {
+    SILC_LOG_ERROR(("Error encoding SILC public key identifier"));
     goto err;
+  }
 
   asn1 = silc_asn1_alloc();
   if (!asn1)
@@ -654,7 +662,7 @@ unsigned char *silc_pkcs_silc_export_public_key(void *public_key,
     goto err;
 
   } else {
-    SILC_LOG_DEBUG(("Unsupported PKCS algorithm"));
+    SILC_LOG_ERROR(("Unsupported PKCS algorithm: %s", pkcs->name));
     goto err;
   }
 
@@ -706,6 +714,7 @@ SilcUInt32 silc_pkcs_silc_public_key_bitlen(void *public_key)
 void *silc_pkcs_silc_public_key_copy(void *public_key)
 {
   SilcSILCPublicKey silc_pubkey = public_key, new_pubkey;
+  SilcPublicKeyIdentifier ident = &silc_pubkey->identifier;
 
   new_pubkey = silc_calloc(1, sizeof(*new_pubkey));
   if (!new_pubkey)
@@ -719,6 +728,28 @@ void *silc_pkcs_silc_public_key_copy(void *public_key)
     return NULL;
   }
 
+  if (ident->username)
+    new_pubkey->identifier.username =
+      silc_memdup(ident->username, strlen(ident->username));
+  if (ident->host)
+    new_pubkey->identifier.host =
+      silc_memdup(ident->host, strlen(ident->host));
+  if (ident->realname)
+    new_pubkey->identifier.realname =
+      silc_memdup(ident->realname, strlen(ident->realname));
+  if (ident->email)
+    new_pubkey->identifier.email =
+      silc_memdup(ident->email, strlen(ident->email));
+  if (ident->org)
+    new_pubkey->identifier.org =
+      silc_memdup(ident->org, strlen(ident->org));
+  if (ident->country)
+    new_pubkey->identifier.country =
+      silc_memdup(ident->country, strlen(ident->country));
+  if (ident->version)
+    new_pubkey->identifier.version =
+      silc_memdup(ident->version, strlen(ident->version));
+
   return new_pubkey;
 }
 
@@ -821,11 +852,15 @@ SilcBool silc_pkcs_silc_import_private_key_file(unsigned char *filedata,
 
   /* Check start of file and remove header from the data. */
   len = strlen(SILC_PKCS_PRIVATE_KEYFILE_BEGIN);
-  if (filedata_len < len + strlen(SILC_PKCS_PRIVATE_KEYFILE_END))
+  if (filedata_len < len + strlen(SILC_PKCS_PRIVATE_KEYFILE_END)) {
+    SILC_LOG_ERROR(("Malformed SILC private key header"));
     return FALSE;
+  }
   for (i = 0; i < len; i++) {
-    if (*filedata != SILC_PKCS_PRIVATE_KEYFILE_BEGIN[i])
+    if (*filedata != SILC_PKCS_PRIVATE_KEYFILE_BEGIN[i]) {
+      SILC_LOG_ERROR(("Malformed SILC private key header"));
       return FALSE;
+    }
     filedata++;
   }
 
@@ -1244,6 +1279,7 @@ int silc_pkcs_silc_import_private_key(unsigned char *key,
   silc_free(silc_privkey);
   if (asn1)
     silc_asn1_free(asn1);
+  SILC_LOG_ERROR(("Malformed SILC private key "));
   return 0;
 }