silc_pem_* routines renamed to silc_base64_*
[silc.git] / lib / silccrypt / silcpk.c
index b0fd72835fcab0a660545a286e0b0117e8193d2e..b934bdc89297362743319fbab47ca7b71f02fc50 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2006 Pekka Riikonen
+  Copyright (C) 1997 - 2007 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
@@ -303,7 +303,7 @@ SilcBool silc_pkcs_silc_import_public_key_file(unsigned char *filedata,
 {
   SilcUInt32 i, len;
   unsigned char *data = NULL;
-  SilcBool ret;
+  int ret;
 
   SILC_LOG_DEBUG(("Parsing SILC public key file"));
 
@@ -327,7 +327,7 @@ SilcBool silc_pkcs_silc_import_public_key_file(unsigned char *filedata,
     break;
 
   case SILC_PKCS_FILE_BASE64:
-    data = silc_pem_decode(filedata, filedata_len, &filedata_len);
+    data = silc_base64_decode(filedata, filedata_len, &filedata_len);
     if (!data)
       return FALSE;
     filedata = data;
@@ -338,14 +338,14 @@ SilcBool silc_pkcs_silc_import_public_key_file(unsigned char *filedata,
                                         ret_public_key);
   silc_free(data);
 
-  return ret;
+  return ret ? TRUE : FALSE;
 }
 
 /* Imports SILC protocol style public key */
 
-SilcBool silc_pkcs_silc_import_public_key(unsigned char *key,
-                                         SilcUInt32 key_len,
-                                         void **ret_public_key)
+int silc_pkcs_silc_import_public_key(unsigned char *key,
+                                    SilcUInt32 key_len,
+                                    void **ret_public_key)
 {
   const SilcPKCSAlgorithm *pkcs;
   SilcBufferStruct buf, alg_key;
@@ -359,12 +359,13 @@ SilcBool silc_pkcs_silc_import_public_key(unsigned char *key,
   SILC_LOG_DEBUG(("Parsing SILC public key"));
 
   if (!ret_public_key)
-    return FALSE;
+    return 0;
 
   silc_buffer_set(&buf, key, key_len);
 
   /* Get length */
   ret = silc_buffer_unformat(&buf,
+                            SILC_STR_ADVANCE,
                             SILC_STR_UI_INT(&totlen),
                             SILC_STR_END);
   if (ret == -1)
@@ -380,7 +381,7 @@ SilcBool silc_pkcs_silc_import_public_key(unsigned char *key,
   /* Get algorithm name and identifier */
   ret =
     silc_buffer_unformat(&buf,
-                        SILC_STR_OFFSET(4),
+                        SILC_STR_ADVANCE,
                         SILC_STR_UI16_NSTRING_ALLOC(&pkcs_name, &pkcs_len),
                         SILC_STR_UI16_NSTRING_ALLOC(&ident, &identifier_len),
                         SILC_STR_END);
@@ -392,11 +393,9 @@ SilcBool silc_pkcs_silc_import_public_key(unsigned char *key,
     goto err;
 
   /* Get key data */
-  silc_buffer_pull(&buf, 4 + 2 + pkcs_len + 2 + identifier_len);
   keydata_len = silc_buffer_len(&buf);
   ret = silc_buffer_unformat(&buf,
-                            SILC_STR_UI_XNSTRING(&key_data,
-                                                 keydata_len),
+                            SILC_STR_DATA(&key_data, keydata_len),
                             SILC_STR_END);
   if (ret == -1)
     goto err;
@@ -471,10 +470,9 @@ SilcBool silc_pkcs_silc_import_public_key(unsigned char *key,
   }
 
   /* Import PKCS algorithm public key */
-  if (pkcs->import_public_key)
-    if (!pkcs->import_public_key(alg_key.data, silc_buffer_len(&alg_key),
-                                &silc_pubkey->public_key))
-      goto err;
+  if (!pkcs->import_public_key(alg_key.data, silc_buffer_len(&alg_key),
+                              &silc_pubkey->public_key))
+    goto err;
 
   silc_free(pkcs_name);
   silc_free(ident);
@@ -482,7 +480,7 @@ SilcBool silc_pkcs_silc_import_public_key(unsigned char *key,
 
   *ret_public_key = silc_pubkey;
 
-  return TRUE;
+  return key_len;
 
  err:
   silc_free(pkcs_name);
@@ -490,7 +488,7 @@ SilcBool silc_pkcs_silc_import_public_key(unsigned char *key,
   silc_free(silc_pubkey);
   if (asn1)
     silc_asn1_free(asn1);
-  return FALSE;
+  return 0;
 }
 
 /* Exports public key as SILC protocol style public key file */
@@ -516,7 +514,7 @@ silc_pkcs_silc_export_public_key_file(void *public_key,
     break;
 
   case SILC_PKCS_FILE_BASE64:
-    data = silc_pem_encode_file(key, key_len);
+    data = silc_base64_encode_file(key, key_len);
     if (!data)
       return NULL;
     silc_free(key);
@@ -785,7 +783,7 @@ SilcBool silc_pkcs_silc_import_private_key_file(unsigned char *filedata,
   SilcUInt32 blocklen;
   unsigned char tmp[32], keymat[64], *data = NULL;
   SilcUInt32 i, len, magic, mac_len;
-  SilcBool ret;
+  int ret;
 
   SILC_LOG_DEBUG(("Parsing SILC private key file"));
 
@@ -807,7 +805,7 @@ SilcBool silc_pkcs_silc_import_private_key_file(unsigned char *filedata,
     break;
 
   case SILC_PKCS_FILE_BASE64:
-    data = silc_pem_decode(filedata, filedata_len, &len);
+    data = silc_base64_decode(filedata, filedata_len, &len);
     if (!data)
       return FALSE;
     filedata = data;
@@ -863,7 +861,7 @@ SilcBool silc_pkcs_silc_import_private_key_file(unsigned char *filedata,
   silc_hash_final(sha1, keymat + 16);
 
   /* Set the key to the cipher */
-  silc_cipher_set_key(aes, keymat, 256);
+  silc_cipher_set_key(aes, keymat, 256, FALSE);
 
   /* First, verify the MAC of the private key data */
   mac_len = silc_hmac_len(sha1hmac);
@@ -909,7 +907,7 @@ SilcBool silc_pkcs_silc_import_private_key_file(unsigned char *filedata,
 
   silc_free(data);
 
-  return ret;
+  return ret ? TRUE : FALSE;
 }
 
 /* Private key version */
@@ -917,9 +915,9 @@ SilcBool silc_pkcs_silc_import_private_key_file(unsigned char *filedata,
 
 /* Imports SILC implementation style private key */
 
-SilcBool silc_pkcs_silc_import_private_key(unsigned char *key,
-                                          SilcUInt32 key_len,
-                                          void **ret_private_key)
+int silc_pkcs_silc_import_private_key(unsigned char *key,
+                                     SilcUInt32 key_len,
+                                     void **ret_private_key)
 {
   SilcBufferStruct buf;
   const SilcPKCSAlgorithm *pkcs;
@@ -934,7 +932,7 @@ SilcBool silc_pkcs_silc_import_private_key(unsigned char *key,
   SILC_LOG_DEBUG(("Parsing SILC private key"));
 
   if (!ret_private_key)
-    return FALSE;
+    return 0;
 
   silc_buffer_set(&buf, key, key_len);
 
@@ -1185,24 +1183,23 @@ SilcBool silc_pkcs_silc_import_private_key(unsigned char *key,
   }
 
   /* Import PKCS algorithm private key */
-  if (pkcs->import_private_key)
-    if (!pkcs->import_private_key(alg_key.data, silc_buffer_len(&alg_key),
-                                 &silc_privkey->private_key))
-      goto err;
+  if (!pkcs->import_private_key(alg_key.data, silc_buffer_len(&alg_key),
+                               &silc_privkey->private_key))
+    goto err;
 
   silc_free(pkcs_name);
   silc_asn1_free(asn1);
 
   *ret_private_key = silc_privkey;
 
-  return TRUE;
+  return key_len;
 
  err:
   silc_free(pkcs_name);
   silc_free(silc_privkey);
   if (asn1)
     silc_asn1_free(asn1);
-  return FALSE;
+  return 0;
 }
 
 /* Exports private key as SILC implementation style private key file */
@@ -1275,7 +1272,7 @@ silc_pkcs_silc_export_private_key_file(void *private_key,
   silc_hash_final(sha1, keymat + 16);
 
   /* Set the key to the cipher */
-  silc_cipher_set_key(aes, keymat, 256);
+  silc_cipher_set_key(aes, keymat, 256, TRUE);
 
   /* Encode the buffer to be encrypted.  Add padding to it too, at least
      block size of the cipher. */
@@ -1334,7 +1331,7 @@ silc_pkcs_silc_export_private_key_file(void *private_key,
     break;
 
   case SILC_PKCS_FILE_BASE64:
-    data = silc_pem_encode_file(enc->data, silc_buffer_len(enc));
+    data = silc_base64_encode_file(enc->data, silc_buffer_len(enc));
     if (!data) {
       silc_buffer_clear(enc);
       silc_buffer_free(enc);
@@ -1529,7 +1526,8 @@ SilcBool silc_pkcs_silc_encrypt(void *public_key,
                                SilcUInt32 src_len,
                                unsigned char *dst,
                                SilcUInt32 dst_size,
-                               SilcUInt32 *ret_dst_len)
+                               SilcUInt32 *ret_dst_len,
+                               SilcRng rng)
 {
   SilcSILCPublicKey silc_pubkey = public_key;
 
@@ -1538,7 +1536,7 @@ SilcBool silc_pkcs_silc_encrypt(void *public_key,
 
   return silc_pubkey->pkcs->encrypt(silc_pubkey->public_key,
                                    src, src_len,
-                                   dst, dst_size, ret_dst_len);
+                                   dst, dst_size, ret_dst_len, rng);
 }
 
 /* Decrypts as specified in SILC protocol specification */