1 # commit f163d8b5af9185de80d24b4dd13951dd64872aa6
2 # Author: Rainer Jung <rjung@apache.org>
3 # Date: Sun Feb 7 14:40:46 2016 +0000
5 # Add support for OpenSSL 1.1.0:
6 # - Switch configure test for OpenSSL libcrypto
7 # from BN_init() to BN_new().
8 # - BN_init() is gone in OpenSSL 1.1.0.
9 # BN_new() exists at least since 0.9.8.
10 # - use OPENSSL_malloc_init() instead of
12 # - make cipherCtx a pointer. Type EVP_CIPHER_CTX
14 # - use EVP_CIPHER_CTX_new() in init() functions
15 # if initialised flag is not set (and set flag)
16 # - use EVP_CIPHER_CTX_free() in cleanup function
17 # - Improve reuse cleanup
18 # - call EVP_CIPHER_CTX_reset() resp.
19 # EVP_CIPHER_CTX_cleanup() in finish functions
20 # - call EVP_CIPHER_CTX_reset() resp.
21 # EVP_CIPHER_CTX_cleanup() when Update fails
22 # Backport of r1728958 and r1728963 from trunk.
25 # git-svn-id: https://svn.apache.org/repos/asf/apr/apr-util/branches/1.5.x@1728969 13f79535-47bb-0310-9956-ffa450edef68
27 diff --git a/build/crypto.m4 b/build/crypto.m4
28 index 9f9be6f..57884e3 100644
31 @@ -88,7 +88,7 @@ AC_DEFUN([APU_CHECK_CRYPTO_OPENSSL], [
33 if test "$withval" = "yes"; then
34 AC_CHECK_HEADERS(openssl/x509.h, [openssl_have_headers=1])
35 - AC_CHECK_LIB(crypto, BN_init, AC_CHECK_LIB(ssl, SSL_accept, [openssl_have_libs=1],,-lcrypto))
36 + AC_CHECK_LIB(crypto, BN_new, AC_CHECK_LIB(ssl, SSL_accept, [openssl_have_libs=1],,-lcrypto))
37 if test "$openssl_have_headers" != "0" && test "$openssl_have_libs" != "0"; then
40 @@ -104,7 +104,7 @@ AC_DEFUN([APU_CHECK_CRYPTO_OPENSSL], [
42 AC_MSG_NOTICE(checking for openssl in $withval)
43 AC_CHECK_HEADERS(openssl/x509.h, [openssl_have_headers=1])
44 - AC_CHECK_LIB(crypto, BN_init, AC_CHECK_LIB(ssl, SSL_accept, [openssl_have_libs=1],,-lcrypto))
45 + AC_CHECK_LIB(crypto, BN_new, AC_CHECK_LIB(ssl, SSL_accept, [openssl_have_libs=1],,-lcrypto))
46 if test "$openssl_have_headers" != "0" && test "$openssl_have_libs" != "0"; then
48 APR_ADDTO(APRUTIL_LDFLAGS, [-L$withval/lib])
49 @@ -113,7 +113,7 @@ AC_DEFUN([APU_CHECK_CRYPTO_OPENSSL], [
51 if test "$apu_have_openssl" != "1"; then
52 AC_CHECK_HEADERS(openssl/x509.h, [openssl_have_headers=1])
53 - AC_CHECK_LIB(crypto, BN_init, AC_CHECK_LIB(ssl, SSL_accept, [openssl_have_libs=1],,-lcrypto))
54 + AC_CHECK_LIB(crypto, BN_new, AC_CHECK_LIB(ssl, SSL_accept, [openssl_have_libs=1],,-lcrypto))
55 if test "$openssl_have_headers" != "0" && test "$openssl_have_libs" != "0"; then
57 APR_ADDTO(APRUTIL_LDFLAGS, [-L$withval/lib])
58 diff --git a/crypto/apr_crypto_openssl.c b/crypto/apr_crypto_openssl.c
59 index 0740f93..7d61fca 100644
60 --- a/crypto/apr_crypto_openssl.c
61 +++ b/crypto/apr_crypto_openssl.c
62 @@ -64,7 +64,7 @@ struct apr_crypto_block_t {
64 const apr_crypto_driver_t *provider;
65 const apr_crypto_t *f;
66 - EVP_CIPHER_CTX cipherCtx;
67 + EVP_CIPHER_CTX *cipherCtx;
71 @@ -111,7 +111,11 @@ static apr_status_t crypto_shutdown_helper(void *data)
72 static apr_status_t crypto_init(apr_pool_t *pool, const char *params,
73 const apu_err_t **result)
75 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
78 + OPENSSL_malloc_init();
80 ERR_load_crypto_strings();
81 /* SSL_load_error_strings(); */
82 OpenSSL_add_all_algorithms();
83 @@ -134,7 +138,7 @@ static apr_status_t crypto_block_cleanup(apr_crypto_block_t *ctx)
86 if (ctx->initialised) {
87 - EVP_CIPHER_CTX_cleanup(&ctx->cipherCtx);
88 + EVP_CIPHER_CTX_free(ctx->cipherCtx);
92 @@ -491,8 +495,10 @@ static apr_status_t crypto_block_encrypt_init(apr_crypto_block_t **ctx,
93 apr_pool_cleanup_null);
95 /* create a new context for encryption */
96 - EVP_CIPHER_CTX_init(&block->cipherCtx);
97 - block->initialised = 1;
98 + if (!block->initialised) {
99 + block->cipherCtx = EVP_CIPHER_CTX_new();
100 + block->initialised = 1;
103 /* generate an IV, if necessary */
105 @@ -519,16 +525,16 @@ static apr_status_t crypto_block_encrypt_init(apr_crypto_block_t **ctx,
107 /* set up our encryption context */
108 #if CRYPTO_OPENSSL_CONST_BUFFERS
109 - if (!EVP_EncryptInit_ex(&block->cipherCtx, key->cipher, config->engine,
110 + if (!EVP_EncryptInit_ex(block->cipherCtx, key->cipher, config->engine,
113 - if (!EVP_EncryptInit_ex(&block->cipherCtx, key->cipher, config->engine, (unsigned char *) key->key, (unsigned char *) usedIv)) {
114 + if (!EVP_EncryptInit_ex(block->cipherCtx, key->cipher, config->engine, (unsigned char *) key->key, (unsigned char *) usedIv)) {
119 /* Clear up any read padding */
120 - if (!EVP_CIPHER_CTX_set_padding(&block->cipherCtx, key->doPad)) {
121 + if (!EVP_CIPHER_CTX_set_padding(block->cipherCtx, key->doPad)) {
125 @@ -582,11 +588,16 @@ static apr_status_t crypto_block_encrypt(unsigned char **out,
128 #if CRYPT_OPENSSL_CONST_BUFFERS
129 - if (!EVP_EncryptUpdate(&ctx->cipherCtx, (*out), &outl, in, inlen)) {
130 + if (!EVP_EncryptUpdate(ctx->cipherCtx, (*out), &outl, in, inlen)) {
132 - if (!EVP_EncryptUpdate(&ctx->cipherCtx, (*out), &outl,
133 + if (!EVP_EncryptUpdate(ctx->cipherCtx, (*out), &outl,
134 (unsigned char *) in, inlen)) {
136 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
137 + EVP_CIPHER_CTX_cleanup(ctx->cipherCtx);
139 + EVP_CIPHER_CTX_reset(ctx->cipherCtx);
144 @@ -616,14 +627,22 @@ static apr_status_t crypto_block_encrypt(unsigned char **out,
145 static apr_status_t crypto_block_encrypt_finish(unsigned char *out,
146 apr_size_t *outlen, apr_crypto_block_t *ctx)
148 + apr_status_t rc = APR_SUCCESS;
151 - if (EVP_EncryptFinal_ex(&ctx->cipherCtx, out, &len) == 0) {
152 - return APR_EPADDING;
153 + if (EVP_EncryptFinal_ex(ctx->cipherCtx, out, &len) == 0) {
160 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
161 + EVP_CIPHER_CTX_cleanup(ctx->cipherCtx);
163 + EVP_CIPHER_CTX_reset(ctx->cipherCtx);
166 - return APR_SUCCESS;
171 @@ -662,8 +681,10 @@ static apr_status_t crypto_block_decrypt_init(apr_crypto_block_t **ctx,
172 apr_pool_cleanup_null);
174 /* create a new context for encryption */
175 - EVP_CIPHER_CTX_init(&block->cipherCtx);
176 - block->initialised = 1;
177 + if (!block->initialised) {
178 + block->cipherCtx = EVP_CIPHER_CTX_new();
179 + block->initialised = 1;
182 /* generate an IV, if necessary */
184 @@ -674,16 +695,16 @@ static apr_status_t crypto_block_decrypt_init(apr_crypto_block_t **ctx,
186 /* set up our encryption context */
187 #if CRYPTO_OPENSSL_CONST_BUFFERS
188 - if (!EVP_DecryptInit_ex(&block->cipherCtx, key->cipher, config->engine,
189 + if (!EVP_DecryptInit_ex(block->cipherCtx, key->cipher, config->engine,
192 - if (!EVP_DecryptInit_ex(&block->cipherCtx, key->cipher, config->engine, (unsigned char *) key->key, (unsigned char *) iv)) {
193 + if (!EVP_DecryptInit_ex(block->cipherCtx, key->cipher, config->engine, (unsigned char *) key->key, (unsigned char *) iv)) {
198 /* Clear up any read padding */
199 - if (!EVP_CIPHER_CTX_set_padding(&block->cipherCtx, key->doPad)) {
200 + if (!EVP_CIPHER_CTX_set_padding(block->cipherCtx, key->doPad)) {
204 @@ -737,11 +758,16 @@ static apr_status_t crypto_block_decrypt(unsigned char **out,
207 #if CRYPT_OPENSSL_CONST_BUFFERS
208 - if (!EVP_DecryptUpdate(&ctx->cipherCtx, *out, &outl, in, inlen)) {
209 + if (!EVP_DecryptUpdate(ctx->cipherCtx, *out, &outl, in, inlen)) {
211 - if (!EVP_DecryptUpdate(&ctx->cipherCtx, *out, &outl, (unsigned char *) in,
212 + if (!EVP_DecryptUpdate(ctx->cipherCtx, *out, &outl, (unsigned char *) in,
215 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
216 + EVP_CIPHER_CTX_cleanup(ctx->cipherCtx);
218 + EVP_CIPHER_CTX_reset(ctx->cipherCtx);
223 @@ -771,15 +797,22 @@ static apr_status_t crypto_block_decrypt(unsigned char **out,
224 static apr_status_t crypto_block_decrypt_finish(unsigned char *out,
225 apr_size_t *outlen, apr_crypto_block_t *ctx)
228 + apr_status_t rc = APR_SUCCESS;
231 - if (EVP_DecryptFinal_ex(&ctx->cipherCtx, out, &len) == 0) {
232 - return APR_EPADDING;
233 + if (EVP_DecryptFinal_ex(ctx->cipherCtx, out, &len) == 0) {
240 +#if OPENSSL_VERSION_NUMBER < 0x10100000L
241 + EVP_CIPHER_CTX_cleanup(ctx->cipherCtx);
243 + EVP_CIPHER_CTX_reset(ctx->cipherCtx);
246 - return APR_SUCCESS;