-fixes
[oweals/gnunet.git] / src / util / crypto_rsa.c
index 0b1c9a1282648254594b947a59784c5319fa5043..ee7e5e9f8238acdac54b7836feba200367dacfe2 100644 (file)
@@ -36,6 +36,7 @@
 #include "gnunet_common.h"
 #include "gnunet_crypto_lib.h"
 #include "gnunet_disk_lib.h"
+#include "gnunet_strings_lib.h"
 
 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
 
@@ -214,6 +215,70 @@ GNUNET_CRYPTO_rsa_key_get_public (const struct GNUNET_CRYPTO_RsaPrivateKey
 }
 
 
+/**
+ * Convert a public key to a string.
+ *
+ * @param pub key to convert
+ * @return string representing  'pub'
+ */
+char *
+GNUNET_CRYPTO_rsa_public_key_to_string (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pub)
+{
+  char *pubkeybuf;
+  size_t keylen = (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)) * 8;
+  char *end;
+
+  if (keylen % 5 > 0)
+    keylen += 5 - keylen % 5;
+  keylen /= 5;
+  pubkeybuf = GNUNET_malloc (keylen + 1);
+  end = GNUNET_STRINGS_data_to_string ((unsigned char *) pub, 
+                                      sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), 
+                                      pubkeybuf, 
+                                      keylen);
+  if (NULL == end)
+  {
+    GNUNET_free (pubkeybuf);
+    return NULL;
+  }
+  *end = '\0';
+  return pubkeybuf;
+}
+
+
+/**
+ * Convert a string representing a public key to a public key.
+ *
+ * @param enc encoded public key
+ * @param enclen number of bytes in enc (without 0-terminator)
+ * @param pub where to store the public key
+ * @return GNUNET_OK on success
+ */
+int
+GNUNET_CRYPTO_rsa_public_key_from_string (const char *enc, 
+                                         size_t enclen,
+                                         struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *pub)
+{
+  size_t keylen = (sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)) * 8;
+
+  if (keylen % 5 > 0)
+    keylen += 5 - keylen % 5;
+  keylen /= 5;
+  if (enclen != keylen)
+    return GNUNET_SYSERR;
+
+  if (GNUNET_OK != GNUNET_STRINGS_string_to_data (enc, enclen,
+                                                (unsigned char*) pub,
+                                                sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)))
+    return GNUNET_SYSERR;
+  if ( (ntohs (pub->len) != sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded)) ||
+       (ntohs (pub->padding) != 0) ||
+       (ntohs (pub->sizen) != GNUNET_CRYPTO_RSA_DATA_ENCODING_LENGTH) )
+    return GNUNET_SYSERR;
+  return GNUNET_OK;
+}
+
+
 /**
  * Internal: publicKey => RSA-Key.
  *
@@ -271,6 +336,7 @@ public2PrivateKey (const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
   return ret;
 }
 
+
 /**
  * Encode the private key in a format suitable for
  * storing it into a file.
@@ -359,6 +425,7 @@ GNUNET_CRYPTO_rsa_encode_key (const struct GNUNET_CRYPTO_RsaPrivateKey *hostkey)
   return retval;
 }
 
+
 /**
  * Decode the private key from the file-format back
  * to the "normal", internal format.
@@ -741,6 +808,7 @@ GNUNET_CRYPTO_setup_hostkey (const char *cfg_name)
     pk = GNUNET_CRYPTO_rsa_key_create_from_file (fn);
     if (NULL != pk)
       GNUNET_CRYPTO_rsa_key_free (pk);
+    GNUNET_free (fn);
   }
   GNUNET_CONFIGURATION_destroy (cfg);
 }
@@ -797,6 +865,7 @@ GNUNET_CRYPTO_rsa_encrypt (const void *block, size_t size,
   return GNUNET_OK;
 }
 
+
 /**
  * Decrypt a given block with the hostkey.
  *