Move store_dynamic_prime() to crypto.c.

This commit is contained in:
George Kadianakis 2011-11-24 22:32:10 +01:00
parent cabb8e54c7
commit 2ef68980a7
4 changed files with 44 additions and 51 deletions

View File

@ -1849,10 +1849,44 @@ crypto_generate_dynamic_prime(void)
return dynamic_prime;
}
BIGNUM *
crypto_get_tls_dh_prime(void)
/** Store our dynamic prime to <b>fname</b> for future use. */
int
router_store_dynamic_prime(const char *fname)
{
return dh_param_p_tls;
FILE *fp = NULL;
int retval = -1;
file_status_t fname_status = file_status(fname);
tor_assert(fname);
if (fname_status == FN_FILE) {
/* If the fname is a file, then the dynamic prime is already stored. */
retval = 0;
goto done;
} else if (fname_status != FN_NOENT) {
log_info(LD_GENERAL, "Dynamic prime filename is occupied.");
goto done;
}
tor_assert(fname_status == FN_NOENT);
if (!(fp = fopen(fname, "w"))) {
log_notice(LD_GENERAL, "Error while creating dynamic prime file.");
goto done;
}
if (BN_print_fp(fp, dh_param_p_tls) == 0) {
log_warn(LD_GENERAL, "Error while printing dynamic prime to file.");
goto done;
}
retval = 0;
done:
if (fp)
fclose(fp);
return retval;
}
/** Set the global TLS Diffie-Hellman modulus.

View File

@ -97,7 +97,7 @@ void crypto_free_pk_env(crypto_pk_env_t *env);
void crypto_set_tls_dh_prime(int use_dynamic_primes,
BIGNUM *stored_dynamic_prime);
BIGNUM * crypto_get_tls_dh_prime(void);
int router_store_dynamic_prime(const char *fname);
/* convenience function: wraps crypto_create_crypto_env, set_key, and init. */
crypto_cipher_env_t *crypto_create_init_cipher(const char *key,

View File

@ -485,46 +485,6 @@ v3_authority_check_key_expiry(void)
}
/** Store <b>dynamic_prime</b> to disk for future use. */
int
router_store_dynamic_prime(const BIGNUM *dynamic_prime)
{
FILE *fp = NULL;
char *fname = get_datadir_fname2("keys", "dynamic_prime");
int retval = -1;
file_status_t fname_status = file_status(fname);
if (fname_status == FN_FILE) {
/* If the fname is a file, then the dynamic prime is already stored. */
retval = 0;
goto done;
} else if (fname_status != FN_NOENT) {
log_info(LD_GENERAL, "Dynamic prime filename is occupied.");
goto done;
}
tor_assert(fname_status == FN_NOENT);
if (!(fp = fopen(fname, "w"))) {
log_notice(LD_GENERAL, "Error while creating dynamic prime file.");
goto done;
}
if (BN_print_fp(fp, dynamic_prime) == 0) {
log_warn(LD_GENERAL, "Error while printing dynamic prime to file.");
goto done;
}
retval = 0;
done:
if (fp)
fclose(fp);
tor_free(fname);
return retval;
}
/** Return the dynamic prime stored in the disk. If there is no
dynamic prime stored in the disk, return NULL. */
BIGNUM *
@ -722,12 +682,12 @@ init_keys(void)
/** 3b. If we use a dynamic prime, store it to disk. */
if (get_options()->DynamicPrimes) {
BIGNUM *dynamic_prime = crypto_get_tls_dh_prime();
if (dynamic_prime) {
if (router_store_dynamic_prime(dynamic_prime) < 0)
log_notice(LD_GENERAL, "Failed while storing dynamic prime. "
"Make sure your data directory is sane.");
}
const char *fname = get_datadir_fname2("keys", "dynamic_prime");
if (crypto_store_dynamic_prime(fname)) {
log_notice(LD_GENERAL, "Failed while storing dynamic prime. "
"Make sure your data directory is sane.");
}
tor_free(fname);
}
/* 4. Build our router descriptor. */

View File

@ -30,7 +30,6 @@ crypto_pk_env_t *init_key_from_file(const char *fname, int generate,
int severity);
BIGNUM *router_get_stored_dynamic_prime(void);
int router_store_dynamic_prime(const BIGNUM *dynamic_prime);
void v3_authority_check_key_expiry(void);