changeset 21704:312b8a2b09df

lib-dcrypt: Move most of the OpenSSL #if handling to macros This avoids #if calls being littered all over the file. This change can cause HMAC_CTX_free(NULL) to be called, but that seems to work fine without crashing.
author Timo Sirainen <timo.sirainen@dovecot.fi>
date Sat, 25 Feb 2017 21:41:03 +0200
parents f17b7e5e532a
children 041cd0683ad3
files src/lib-dcrypt/dcrypt-openssl.c
diffstat 1 files changed, 18 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/src/lib-dcrypt/dcrypt-openssl.c	Sun Feb 26 15:37:36 2017 +0200
+++ b/src/lib-dcrypt/dcrypt-openssl.c	Sat Feb 25 21:41:03 2017 +0200
@@ -75,6 +75,22 @@
 #define OBJ_length(o) ((o)->length)
 #endif
 
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+#  define EVP_MD_CTX_new() EVP_MD_CTX_create()
+#  define EVP_MD_CTX_free(ctx) EVP_MD_CTX_destroy(ctx)
+#endif
+
+#if OPENSSL_VERSION_NUMBER < 0x10100000L
+#  define HMAC_Init_ex(ctx, key, key_len, md, impl) \
+	HMAC_Init_ex(&(ctx), key, key_len, md, impl)
+#  define HMAC_Update(ctx, data, len) HMAC_Update(&(ctx), data, len)
+#  define HMAC_Final(ctx, md, len) HMAC_Final(&(ctx), md, len)
+#  define HMAC_CTX_free(ctx) HMAC_cleanup(&(ctx))
+#else
+#  define HMAC_CTX_free(ctx) \
+	STMT_START { HMAC_CTX_free(ctx); (ctx) = NULL; } STMT_END
+#endif
+
 struct dcrypt_context_symmetric {
 	pool_t pool;
 	const EVP_CIPHER *cipher;
@@ -429,11 +445,7 @@
 void dcrypt_openssl_ctx_hmac_destroy(struct dcrypt_context_hmac **ctx)
 {
 	pool_t pool = (*ctx)->pool;
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
-	if ((*ctx)->ctx) HMAC_CTX_free((*ctx)->ctx);
-#else
-	HMAC_cleanup(&((*ctx)->ctx));
-#endif
+	HMAC_CTX_free((*ctx)->ctx);
 	pool_unref(&pool);
 	*ctx = NULL;
 }
@@ -475,10 +487,8 @@
 #if OPENSSL_VERSION_NUMBER >= 0x10100000L
 	ctx->ctx = HMAC_CTX_new();
 	if (ctx->ctx == NULL) return dcrypt_openssl_error(error_r);
+#endif
 	ec = HMAC_Init_ex(ctx->ctx, ctx->key, ctx->klen, ctx->md, NULL);
-#else
-	ec = HMAC_Init_ex(&(ctx->ctx), ctx->key, ctx->klen, ctx->md, NULL);
-#endif
 	if (ec != 1) return dcrypt_openssl_error(error_r);
 	return TRUE;
 }
@@ -486,11 +496,7 @@
 bool dcrypt_openssl_ctx_hmac_update(struct dcrypt_context_hmac *ctx, const unsigned char *data, size_t data_len, const char **error_r)
 {
 	int ec;
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
 	ec = HMAC_Update(ctx->ctx, data, data_len);
-#else
-	ec = HMAC_Update(&(ctx->ctx), data, data_len);
-#endif
 	if (ec != 1) return dcrypt_openssl_error(error_r);
 	return TRUE;
 }
@@ -500,14 +506,8 @@
 	int ec;
 	unsigned char buf[HMAC_MAX_MD_CBLOCK];
 	unsigned int outl;
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
 	ec = HMAC_Final(ctx->ctx, buf, &outl);
 	HMAC_CTX_free(ctx->ctx);
-	ctx->ctx = NULL;
-#else
-	ec = HMAC_Final(&(ctx->ctx), buf, &outl);
-	HMAC_cleanup(&(ctx->ctx));
-#endif
 	if (ec == 1) {
 		buffer_append(result, buf, outl);
 	} else return dcrypt_openssl_error(error_r);
@@ -2135,11 +2135,7 @@
 	long len = BIO_get_mem_data(b, &ptr);
 	unsigned int hlen = sizeof(buf);
 	/* then hash it */
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
 	EVP_MD_CTX *ctx = EVP_MD_CTX_new();
-#else
-	EVP_MD_CTX *ctx = EVP_MD_CTX_create();
-#endif
 	if (ctx == NULL ||
 	    EVP_DigestInit_ex(ctx, md, NULL) < 1 ||
 	    EVP_DigestUpdate(ctx, (const unsigned char*)ptr, len) < 1 ||
@@ -2149,11 +2145,7 @@
 		buffer_append(result, buf, hlen);
 		res = TRUE;
 	}
-#if OPENSSL_VERSION_NUMBER >= 0x10100000L
 	EVP_MD_CTX_free(ctx);
-#else
-	EVP_MD_CTX_destroy(ctx);
-#endif
 	BIO_vfree(b);
 
 	return res;