-introducing convenience function to load private key of peer
authorChristian Grothoff <christian@grothoff.org>
Wed, 26 Jun 2013 17:17:35 +0000 (17:17 +0000)
committerChristian Grothoff <christian@grothoff.org>
Wed, 26 Jun 2013 17:17:35 +0000 (17:17 +0000)
src/include/gnunet_crypto_lib.h
src/util/crypto_ecc.c

index 693cfcf1258d52d43cfb35213231f62cbc8c8164..8592f0da5d249fdef131acb67befda565c5b246b 100644 (file)
@@ -267,7 +267,7 @@ struct GNUNET_CRYPTO_EccSignature
   /**
    * Overall size of the signature data.
    */
-  uint16_t size;
+  uint16_t size GNUNET_PACKED;
 
   /**
    * S-expression, padded with zeros.
@@ -285,12 +285,12 @@ struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded
   /**
    * Size of the encoding, in network byte order.
    */
-  uint16_t size;
+  uint16_t size GNUNET_PACKED;
 
   /**
    * Actual length of the q-point binary encoding.
    */
-  uint16_t len;
+  uint16_t len GNUNET_PACKED;
 
   /**
    * 0-padded q-point in binary encoding (GCRYPT_MPI_FMT_USG).
@@ -1276,6 +1276,17 @@ struct GNUNET_CRYPTO_EccPrivateKey *
 GNUNET_CRYPTO_ecc_key_create_from_file (const char *filename);
 
 
+/**
+ * Create a new private key by reading our peer's key from
+ * the file specified in the configuration.
+ *
+ * @return new private key, NULL on error (for example,
+ *   permission denied)
+ */
+struct GNUNET_CRYPTO_EccPrivateKey *
+GNUNET_CRYPTO_ecc_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg);
+
+
 /**
  * Handle to cancel private key generation and state for the
  * key generation operation.
index b5a057aed560a876b6e22a5c3d1de3052fb49caa..498de59df04813d61c8b900b5782518cb7c376ca 100644 (file)
@@ -889,6 +889,28 @@ GNUNET_CRYPTO_ecc_key_create_start (const char *filename,
 }
 
 
+/**
+ * Create a new private key by reading our peer's key from
+ * the file specified in the configuration.
+ *
+ * @return new private key, NULL on error (for example,
+ *   permission denied)
+ */
+struct GNUNET_CRYPTO_EccPrivateKey *
+GNUNET_CRYPTO_ecc_key_create_from_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
+{
+  struct GNUNET_CRYPTO_EccPrivateKey *pk;
+  char *fn;
+
+  if (GNUNET_OK != 
+      GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY", &fn))
+    return NULL;
+  pk = GNUNET_CRYPTO_ecc_key_create_from_file (fn);
+  GNUNET_free (fn);
+  return pk;
+}
+
+
 /**
  * Setup a key file for a peer given the name of the
  * configuration file (!).  This function is used so that
@@ -902,18 +924,12 @@ GNUNET_CRYPTO_ecc_setup_key (const char *cfg_name)
 {
   struct GNUNET_CONFIGURATION_Handle *cfg;
   struct GNUNET_CRYPTO_EccPrivateKey *pk;
-  char *fn;
 
   cfg = GNUNET_CONFIGURATION_create ();
   (void) GNUNET_CONFIGURATION_load (cfg, cfg_name);
-  if (GNUNET_OK == 
-      GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY", &fn))
-  {
-    pk = GNUNET_CRYPTO_ecc_key_create_from_file (fn);
-    if (NULL != pk)
-      GNUNET_CRYPTO_ecc_key_free (pk);
-    GNUNET_free (fn);
-  }
+  pk = GNUNET_CRYPTO_ecc_key_create_from_configuration (cfg);
+  if (NULL != pk)
+    GNUNET_CRYPTO_ecc_key_free (pk);
   GNUNET_CONFIGURATION_destroy (cfg);
 }
 
@@ -932,24 +948,13 @@ GNUNET_CRYPTO_get_host_identity (const struct GNUNET_CONFIGURATION_Handle *cfg,
 {
   struct GNUNET_CRYPTO_EccPrivateKey *my_private_key;
   struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded my_public_key;
-  char *keyfile;
 
-  if (GNUNET_OK !=
-      GNUNET_CONFIGURATION_get_value_filename (cfg, "PEER", "PRIVATE_KEY",
-                                               &keyfile))
-  {
-    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Lacking key configuration settings.\n"));
-    return GNUNET_SYSERR;
-  }
-  if (NULL == (my_private_key = GNUNET_CRYPTO_ecc_key_create_from_file (keyfile)))
+  if (NULL == (my_private_key = GNUNET_CRYPTO_ecc_key_create_from_configuration (cfg)))
   {
     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                _("Could not access hostkey file `%s'.\n"), keyfile);
-    GNUNET_free (keyfile);
+                _("Could not load peer's private key\n"));
     return GNUNET_SYSERR;
   }
-  GNUNET_free (keyfile);
   GNUNET_CRYPTO_ecc_key_get_public (my_private_key, &my_public_key);
   GNUNET_CRYPTO_ecc_key_free (my_private_key);
   GNUNET_CRYPTO_hash (&my_public_key, sizeof (my_public_key), &dst->hashPubKey);