Skip to content

Commit

Permalink
Make sure we compile in the absence of APU_HAVE_CRYPTO_PRNG. Make sur…
Browse files Browse the repository at this point in the history
…e we don't

segfault if the PRNG does not initialise.


git-svn-id: https://svn.apache.org/repos/asf/apr/apr/trunk@1836438 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
minfrin committed Jul 22, 2018
1 parent 117be8f commit 3c05269
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
6 changes: 5 additions & 1 deletion crypto/apr_crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,11 @@ static apr_status_t apr_crypto_term(void *ptr)

APR_DECLARE(apr_status_t) apr_crypto_init(apr_pool_t *pool)
{
apr_status_t rv;
apr_pool_t *rootp;
#if APU_HAVE_CRYPTO_PRNG
apr_status_t rv;
int flags = 0;
#endif

if (drivers != NULL) {
return APR_SUCCESS;
Expand All @@ -109,6 +111,7 @@ APR_DECLARE(apr_status_t) apr_crypto_init(apr_pool_t *pool)
apr_pool_cleanup_register(rootp, NULL, apr_crypto_term,
apr_pool_cleanup_null);

#if APU_HAVE_CRYPTO_PRNG
/* apr_crypto_prng_init() may already have been called with
* non-default parameters, so ignore APR_EREINIT.
*/
Expand All @@ -119,6 +122,7 @@ APR_DECLARE(apr_status_t) apr_crypto_init(apr_pool_t *pool)
if (rv != APR_SUCCESS && rv != APR_EREINIT) {
return rv;
}
#endif

return APR_SUCCESS;
}
Expand Down
18 changes: 13 additions & 5 deletions test/testcrypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -1455,6 +1455,7 @@ static void test_crypto_equals(abts_case *tc, void *data)
TEST_SCALAR_MATCH(6, p, 0);
}

#if APU_HAVE_CRYPTO_PRNG
#if APU_HAVE_OPENSSL
#include <openssl/obj_mac.h> /* for NID_* */
#endif
Expand Down Expand Up @@ -1523,7 +1524,7 @@ static const unsigned char test_PRNG_kat0[128] = {
static void test_crypto_prng(abts_case *tc, void *data)
{
unsigned char randbytes[128], seed[APR_CRYPTO_PRNG_SEED_SIZE];
apr_crypto_prng_t *cprng;
apr_crypto_prng_t *cprng = NULL;
apr_pool_t *pool = NULL;
apr_status_t rv;
int i;
Expand Down Expand Up @@ -1552,8 +1553,10 @@ static void test_crypto_prng(abts_case *tc, void *data)
rv == APR_SUCCESS);
}

rv = apr_crypto_prng_bytes(cprng, randbytes, 128 - 32);
ABTS_ASSERT(tc, "apr_crypto_prng_bytes failed", rv == APR_SUCCESS);
if (cprng) {
rv = apr_crypto_prng_bytes(cprng, randbytes, 128 - 32);
ABTS_ASSERT(tc, "apr_crypto_prng_bytes failed", rv == APR_SUCCESS);
}

/* Should match the first time only */
if (i != 0) {
Expand All @@ -1567,8 +1570,10 @@ static void test_crypto_prng(abts_case *tc, void *data)
memcmp(randbytes, test_PRNG_kat0 + 32, 128 - 32) == 0);
}

rv = apr_crypto_prng_destroy(cprng);
ABTS_ASSERT(tc, "apr_crypto_prng_destroy failed", rv == APR_SUCCESS);
if (cprng) {
rv = apr_crypto_prng_destroy(cprng);
ABTS_ASSERT(tc, "apr_crypto_prng_destroy failed", rv == APR_SUCCESS);
}
}

apr_pool_destroy(pool);
Expand Down Expand Up @@ -1688,6 +1693,7 @@ static void test_crypto_thread_random(abts_case *tc, void *data)
apr_pool_destroy(pool);
}
#endif
#endif

abts_suite *testcrypto(abts_suite *suite)
{
Expand Down Expand Up @@ -1768,12 +1774,14 @@ abts_suite *testcrypto(abts_suite *suite)
abts_run_test(suite, test_crypto_memzero, NULL);
abts_run_test(suite, test_crypto_equals, NULL);

#if APU_HAVE_CRYPTO_PRNG
abts_run_test(suite, test_crypto_prng, NULL);
#if APR_HAS_FORK
abts_run_test(suite, test_crypto_fork_random, NULL);
#endif
#if APR_HAS_THREADS
abts_run_test(suite, test_crypto_thread_random, NULL);
#endif
#endif

return suite;
Expand Down

0 comments on commit 3c05269

Please sign in to comment.