From: Christian Grothoff Date: Wed, 26 Jun 2013 17:17:35 +0000 (+0000) Subject: -introducing convenience function to load private key of peer X-Git-Tag: initial-import-from-subversion-38251~8610 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=abc1a0ea19b7dc199b83749d31e32622c7469ded;p=oweals%2Fgnunet.git -introducing convenience function to load private key of peer --- diff --git a/src/include/gnunet_crypto_lib.h b/src/include/gnunet_crypto_lib.h index 693cfcf12..8592f0da5 100644 --- a/src/include/gnunet_crypto_lib.h +++ b/src/include/gnunet_crypto_lib.h @@ -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. diff --git a/src/util/crypto_ecc.c b/src/util/crypto_ecc.c index b5a057aed..498de59df 100644 --- a/src/util/crypto_ecc.c +++ b/src/util/crypto_ecc.c @@ -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);