diff options
author | Graham Leggett <minfrin@apache.org> | 2011-12-05 16:51:07 +0000 |
---|---|---|
committer | Graham Leggett <minfrin@apache.org> | 2011-12-05 16:51:07 +0000 |
commit | 025e42fb765874f715344559946fcd25fe234b8e (patch) | |
tree | 0bc6e3ee18b035214ab253e4dd29edf6ae93583e /crypto | |
parent | 4d1d087bd5fe04a7dec08f5335c1b8f3f0bafd56 (diff) | |
download | apr-025e42fb765874f715344559946fcd25fe234b8e.tar.gz |
apr_crypto: Clear out buffers that are allocated by us when the pool from
which the memory was allocated from is cleaned up.
git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1210524 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'crypto')
-rw-r--r-- | crypto/apr_crypto.c | 30 | ||||
-rw-r--r-- | crypto/apr_crypto_nss.c | 3 | ||||
-rw-r--r-- | crypto/apr_crypto_openssl.c | 4 |
3 files changed, 37 insertions, 0 deletions
diff --git a/crypto/apr_crypto.c b/crypto/apr_crypto.c index 4b28dd2e2..4484cae3b 100644 --- a/crypto/apr_crypto.c +++ b/crypto/apr_crypto.c @@ -56,6 +56,11 @@ APR_TYPEDEF_STRUCT(apr_crypto_block_t, const apr_crypto_t *f; ) +typedef struct apr_crypto_clear_t { + void *buffer; + apr_size_t size; +} apr_crypto_clear_t; + #if !APR_HAVE_MODULAR_DSO #define DRIVER_LOAD(name,driver,pool,params) \ { \ @@ -118,6 +123,31 @@ APR_DECLARE(apr_status_t) apr_crypto_init(apr_pool_t *pool) return ret; } +static apr_status_t crypto_clear(void *ptr) +{ + apr_crypto_clear_t *clear = (apr_crypto_clear_t *)ptr; + + memset(clear->buffer, 0, clear->size); + clear->buffer = NULL; + clear->size = 0; + + return APR_SUCCESS; +} + +APR_DECLARE(apr_status_t) apr_crypto_clear(apr_pool_t *pool, + void *buffer, apr_size_t size) +{ + apr_crypto_clear_t *clear = apr_palloc(pool, sizeof(apr_crypto_clear_t)); + + clear->buffer = buffer; + clear->size = size; + + apr_pool_cleanup_register(pool, clear, crypto_clear, + apr_pool_cleanup_null); + + return APR_SUCCESS; +} + APR_DECLARE(apr_status_t) apr_crypto_get_driver( const apr_crypto_driver_t **driver, const char *name, const char *params, const apu_err_t **result, apr_pool_t *pool) diff --git a/crypto/apr_crypto_nss.c b/crypto/apr_crypto_nss.c index bb5c3756d..e52fe99bd 100644 --- a/crypto/apr_crypto_nss.c +++ b/crypto/apr_crypto_nss.c @@ -551,6 +551,7 @@ static apr_status_t crypto_block_encrypt_init(apr_crypto_block_t **ctx, if (!usedIv) { return APR_ENOMEM; } + apr_crypto_clear(p, usedIv, key->ivSize); s = PK11_GenerateRandom(usedIv, key->ivSize); if (s != SECSuccess) { return APR_ENOIV; @@ -622,6 +623,7 @@ static apr_status_t crypto_block_encrypt(unsigned char **out, if (!buffer) { return APR_ENOMEM; } + apr_crypto_clear(block->pool, buffer, inlen + block->blockSize); *out = buffer; } @@ -785,6 +787,7 @@ static apr_status_t crypto_block_decrypt(unsigned char **out, if (!buffer) { return APR_ENOMEM; } + apr_crypto_clear(block->pool, buffer, inlen + block->blockSize); *out = buffer; } diff --git a/crypto/apr_crypto_openssl.c b/crypto/apr_crypto_openssl.c index 4708ecd36..993fa9f50 100644 --- a/crypto/apr_crypto_openssl.c +++ b/crypto/apr_crypto_openssl.c @@ -431,6 +431,7 @@ static apr_status_t crypto_passphrase(apr_crypto_key_t **k, apr_size_t *ivSize, if (!key->key) { return APR_ENOMEM; } + apr_crypto_clear(p, key->key, key->keyLen); /* generate the key */ if (PKCS5_PBKDF2_HMAC_SHA1(pass, passLen, (unsigned char *) salt, saltLen, @@ -504,6 +505,7 @@ static apr_status_t crypto_block_encrypt_init(apr_crypto_block_t **ctx, if (!usedIv) { return APR_ENOMEM; } + apr_crypto_clear(p, usedIv, key->ivSize); if (!((RAND_status() == 1) && (RAND_bytes(usedIv, key->ivSize) == 1))) { return APR_ENOIV; @@ -575,6 +577,7 @@ static apr_status_t crypto_block_encrypt(unsigned char **out, if (!buffer) { return APR_ENOMEM; } + apr_crypto_clear(ctx->pool, buffer, inlen + EVP_MAX_BLOCK_LENGTH); *out = buffer; } @@ -729,6 +732,7 @@ static apr_status_t crypto_block_decrypt(unsigned char **out, if (!buffer) { return APR_ENOMEM; } + apr_crypto_clear(ctx->pool, buffer, inlen + EVP_MAX_BLOCK_LENGTH); *out = buffer; } |