diff options
-rw-r--r-- | src/common/crypto.c | 44 | ||||
-rw-r--r-- | src/common/util.c | 13 | ||||
-rw-r--r-- | src/common/util.h | 2 |
3 files changed, 24 insertions, 35 deletions
diff --git a/src/common/crypto.c b/src/common/crypto.c index fe54dea83..818c1e09b 100644 --- a/src/common/crypto.c +++ b/src/common/crypto.c @@ -91,9 +91,7 @@ crypto_pk_env_t *crypto_new_pk_env(int type) { crypto_pk_env_t *env; - env = (crypto_pk_env_t *)malloc(sizeof(crypto_pk_env_t)); - if (!env) - return 0; + env = (crypto_pk_env_t *)tor_malloc(sizeof(crypto_pk_env_t)); env->type = type; env->refs = 1; @@ -185,9 +183,7 @@ crypto_cipher_env_t *crypto_new_cipher_env(int type) crypto_cipher_env_t *env; int iv_len, key_len; - env = (crypto_cipher_env_t *)malloc(sizeof(crypto_cipher_env_t)); - if (!env) - return NULL; + env = (crypto_cipher_env_t *)tor_malloc(sizeof(crypto_cipher_env_t)); env->type = type; env->key = NULL; @@ -201,15 +197,15 @@ crypto_cipher_env_t *crypto_new_cipher_env(int type) /* This is not an openssl cipher */ goto err; else { - env->aux = (unsigned char *)malloc(sizeof(EVP_CIPHER_CTX)); + env->aux = (unsigned char *)tor_malloc(sizeof(EVP_CIPHER_CTX)); EVP_CIPHER_CTX_init((EVP_CIPHER_CTX *)env->aux); } - if (iv_len && !(env->iv = (unsigned char *)malloc(iv_len))) - goto err; + if(iv_len) + env->iv = (unsigned char *)tor_malloc(iv_len); - if (key_len && !(env->key = (unsigned char *)malloc(key_len))) - goto err; + if(key_len) + env->key = (unsigned char *)tor_malloc(key_len); return env; err: @@ -367,9 +363,7 @@ int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env, char **dest, int BIO_set_close(b, BIO_NOCLOSE); /* so BIO_free doesn't free buf */ BIO_free(b); - *dest = malloc(buf->length+1); - if(!*dest) - return -1; + *dest = tor_malloc(buf->length+1); memcpy(*dest, buf->data, buf->length); (*dest)[buf->length] = 0; /* null terminate it */ *len = buf->length; @@ -455,25 +449,6 @@ int crypto_pk_check_key(crypto_pk_env_t *env) } } -int crypto_pk_set_key(crypto_pk_env_t *env, unsigned char *key) -{ - assert(env && key); - - switch(env->type) { - case CRYPTO_PK_RSA: - if (!env->key) - return -1; - /* XXX BUG XXX you can't memcpy an RSA, it's got a bunch of subpointers */ - assert(0); - memcpy((void *)env->key, (void *)key, sizeof(RSA)); - break; - default : - return -1; - } - - return 0; -} - int crypto_pk_cmp_keys(crypto_pk_env_t *a, crypto_pk_env_t *b) { int result; @@ -734,8 +709,7 @@ crypto_dh_env_t *crypto_dh_new() if (!dh_param_p) init_dh_param(); - if (!(res = malloc(sizeof(crypto_dh_env_t)))) - goto err; + res = tor_malloc(sizeof(crypto_dh_env_t)); res->dh = NULL; if (!(res->dh = DH_new())) diff --git a/src/common/util.c b/src/common/util.c index e5e83532d..191fa84cd 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -7,6 +7,19 @@ #include "util.h" #include "log.h" +void *tor_malloc(size_t size) { + void *result; + + result = malloc(size); + + if(!result) { + log(LOG_ERR,"tor_malloc(): Out of memory. Dying."); + exit(1); + } + + return result; +} + void my_gettimeofday(struct timeval *timeval) { diff --git a/src/common/util.h b/src/common/util.h index bc880d24e..6b238bf8c 100644 --- a/src/common/util.h +++ b/src/common/util.h @@ -7,6 +7,8 @@ #include <sys/time.h> +void *tor_malloc(size_t size); + /* Same as gettimeofday, but no need to check exit value. */ void my_gettimeofday(struct timeval *timeval); /* Returns the number of microseconds between start and end. Requires that |