diff mbox series

[V2] corelib: print both error code and reason string for decrypt functions

Message ID 84e18739-deee-1501-81a0-b7150270ae14@streamunlimited.com
State Accepted
Headers show
Series [V2] corelib: print both error code and reason string for decrypt functions | expand

Commit Message

Martin Geier March 25, 2018, 1:04 p.m. UTC
From 05f0b52dbdb4973b51e0fd2694f7e966b7f7f6a3 Mon Sep 17 00:00:00 2001
From: Martin Geier <martin.geier@streamunlimited.com>
Date: Sun, 25 Mar 2018 15:00:22 +0200
Subject: [V2] corelib: print both error code and reason string for decrypt
  functions

To register reason strings for error codes ERR_load_crypto_strings
has to be called for openssl version < 1.1.0

Signed-off-by: Martin Geier <martin.geier@streamunlimited.com>
---
  corelib/swupdate_decrypt.c | 13 +++++++++----
  include/sslapi.h           |  1 +
  2 files changed, 10 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/corelib/swupdate_decrypt.c b/corelib/swupdate_decrypt.c
index 3238cce..1360aec 100644
--- a/corelib/swupdate_decrypt.c
+++ b/corelib/swupdate_decrypt.c
@@ -70,7 +70,9 @@  struct swupdate_digest *swupdate_DECRYPT_init(unsigned char *key, unsigned char
  	 */
  	ret = EVP_DecryptInit_ex(SSL_GET_CTXDEC(dgst), cipher, NULL, key, iv);
  	if (ret != 1) {
-		ERROR("Decrypt Engine not initialized, error 0x%lx\n", ERR_get_error());
+		const char *reason = ERR_reason_error_string(ERR_peek_error());
+		ERROR("Decrypt Engine not initialized, error 0x%lx, reason: %s\n", ERR_get_error(),
+			reason != NULL ? reason : "unknown");
  		free(dgst);
  		return NULL;
  	}
@@ -82,7 +84,9 @@  int swupdate_DECRYPT_update(struct swupdate_digest *dgst, unsigned char *buf,
  				int *outlen, unsigned char *cryptbuf, int inlen)
  {
  	if (EVP_DecryptUpdate(SSL_GET_CTXDEC(dgst), buf, outlen, cryptbuf, inlen) != 1) {
-		ERROR("Update: Decryption error 0x%lx\n", ERR_get_error());
+		const char *reason = ERR_reason_error_string(ERR_peek_error());
+		ERROR("Update: Decryption error 0x%lx, reason: %s\n", ERR_get_error(),
+			reason != NULL ? reason : "unknown");
  		return -EFAULT;
  	}
  
@@ -96,8 +100,9 @@  int swupdate_DECRYPT_final(struct swupdate_digest *dgst, unsigned char *buf,
  		return -EINVAL;
  
  	if (EVP_DecryptFinal_ex(SSL_GET_CTXDEC(dgst), buf, outlen) != 1) {
-		ERROR("Decryption error 0x%s\n",
-				ERR_reason_error_string(ERR_get_error()));
+		const char *reason = ERR_reason_error_string(ERR_peek_error());
+		ERROR("Final: Decryption error 0x%lx, reason: %s\n", ERR_get_error(),
+			reason != NULL ? reason : "unknown");
  		return -EFAULT;
  	}
  
diff --git a/include/sslapi.h b/include/sslapi.h
index 213478e..1d24dfb 100644
--- a/include/sslapi.h
+++ b/include/sslapi.h
@@ -63,6 +63,7 @@  struct swupdate_digest {
  	do { \
  		CRYPTO_malloc_init(); \
  		OpenSSL_add_all_algorithms(); \
+		ERR_load_crypto_strings(); \
  	} while (0); \
  }
  #else