From 3c0526902c4468bcf39adc8a0340d9c19553c6f2 Mon Sep 17 00:00:00 2001 From: Graham Leggett Date: Sun, 22 Jul 2018 13:03:42 +0000 Subject: [PATCH] Make sure we compile in the absence of APU_HAVE_CRYPTO_PRNG. Make sure 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 --- crypto/apr_crypto.c | 6 +++++- test/testcrypto.c | 18 +++++++++++++----- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/crypto/apr_crypto.c b/crypto/apr_crypto.c index e7c11b73435..bda84b2a267 100644 --- a/crypto/apr_crypto.c +++ b/crypto/apr_crypto.c @@ -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; @@ -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. */ @@ -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; } diff --git a/test/testcrypto.c b/test/testcrypto.c index 9d95e10c2b9..bfcb7366571 100644 --- a/test/testcrypto.c +++ b/test/testcrypto.c @@ -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 /* for NID_* */ #endif @@ -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; @@ -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) { @@ -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); @@ -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) { @@ -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;