pgbackrest.git / summary / log / commit / refs

commit    2cb312ef5aa9f57da607229f9fe22ce5d7aa0352
Author:   David Steele <david@pgmasters.net>
Date:     Wed Nov 07 00:16:00 2018 +0000

    Add cryptoError() and update crypto code to use it.
    
    This adds detail to error messages when available and improves code coverage.


doc/xml/release.xml | 4 ++++ src/Makefile | 2 +- src/crypto/cipherBlock.c | 18 ++++++++---------- src/crypto/crypto.c | 26 ++++++++++++++++++++++++-- src/crypto/crypto.h | 1 + src/crypto/hash.c | 24 +++++++----------------- test/define.yaml | 4 ++-- test/src/module/crypto/cryptoTest.c | 16 ++++++++++++++++ 8 files changed, 63 insertions(+), 32 deletions(-) diff --git a/doc/xml/release.xml b/doc/xml/release.xml index 7fcbab8be..d6668a334 100644 --- a/doc/xml/release.xml +++ b/doc/xml/release.xml @@ -89,6 +89,10 @@ <p>Change <code>infoArchiveCheckPg()</code> to display the <postgres/> version as a string (e.g. 9.4) instead of the integer representation (e.g. 90400) when throwing an error.</p> </release-item> + + <release-item> + <p>Add <code>cryptoError()</code> and update crypto code to use it.</p> + </release-item> </release-development-list> </release-core-list> diff --git a/src/Makefile b/src/Makefile index d1e9fd88c..14b3ffe8d 100644 --- a/src/Makefile +++ b/src/Makefile @@ -296,7 +296,7 @@ config/parse.o: config/parse.c common/assert.h common/debug.h common/error.auto. crypto/cipherBlock.o: crypto/cipherBlock.c common/debug.h common/error.auto.h common/error.h common/log.h common/logLevel.h common/memContext.h common/stackTrace.h common/type/convert.h crypto/cipher.h crypto/cipherBlock.h crypto/crypto.h crypto/random.h $(CC) $(CFLAGS) -c crypto/cipherBlock.c -o crypto/cipherBlock.o -crypto/crypto.o: crypto/crypto.c common/debug.h common/log.h common/logLevel.h common/stackTrace.h common/type/convert.h crypto/crypto.h +crypto/crypto.o: crypto/crypto.c common/debug.h common/error.auto.h common/error.h common/log.h common/logLevel.h common/stackTrace.h common/type/convert.h crypto/crypto.h $(CC) $(CFLAGS) -c crypto/crypto.c -o crypto/crypto.o crypto/hash.o: crypto/hash.c common/assert.h common/debug.h common/error.auto.h common/error.h common/io/filter/filter.h common/io/filter/filter.intern.h common/log.h common/logLevel.h common/memContext.h common/stackTrace.h common/type/buffer.h common/type/convert.h common/type/keyValue.h common/type/string.h common/type/variant.h common/type/variantList.h crypto/crypto.h crypto/hash.h diff --git a/src/crypto/cipherBlock.c b/src/crypto/cipherBlock.c index b05053aa2..66344f6af 100644 --- a/src/crypto/cipherBlock.c +++ b/src/crypto/cipherBlock.c @@ -211,18 +211,16 @@ cipherBlockProcess(CipherBlock *this, const unsigned char *source, size_t source this->cipher, this->digest, salt, (unsigned char *)this->pass, (int)this->passSize, 1, key, initVector); // Create context to track cipher - if (!(this->cipherContext = EVP_CIPHER_CTX_new())) // {uncoverable - no failure condition known} - THROW(MemoryError, "unable to create context"); // {+uncoverable} + cryptoError(!(this->cipherContext = EVP_CIPHER_CTX_new()), "unable to create context"); // Set free callback to ensure cipher context is freed memContextCallback(this->memContext, (MemContextCallback)cipherBlockFree, this); // Initialize cipher - if (EVP_CipherInit_ex( // {uncoverable - no failure condition known} - this->cipherContext, this->cipher, NULL, key, initVector, this->mode == cipherModeEncrypt) != 1) - { - THROW(MemoryError, "unable to initialize cipher"); // {+uncoverable} - } + cryptoError( + !EVP_CipherInit_ex( + this->cipherContext, this->cipher, NULL, key, initVector, this->mode == cipherModeEncrypt), + "unable to initialize cipher"); this->saltDone = true; } @@ -234,9 +232,9 @@ cipherBlockProcess(CipherBlock *this, const unsigned char *source, size_t source // Process the data size_t destinationUpdateSize = 0; - if (!EVP_CipherUpdate( // {uncoverable - no failure condition known} - this->cipherContext, destination, (int *)&destinationUpdateSize, source, (int)sourceSize)) - THROW(CipherError, "unable to process"); // {+uncoverable} + cryptoError( + !EVP_CipherUpdate(this->cipherContext, destination, (int *)&destinationUpdateSize, source, (int)sourceSize), + "unable to process cipher"); destinationSize += destinationUpdateSize; diff --git a/src/crypto/crypto.c b/src/crypto/crypto.c index d6b37c920..59b6501a6 100644 --- a/src/crypto/crypto.c +++ b/src/crypto/crypto.c @@ -6,6 +6,7 @@ Crypto Common #include <openssl/err.h> #include "common/debug.h" +#include "common/error.h" #include "common/log.h" #include "crypto/crypto.h" @@ -15,7 +16,27 @@ Flag to indicate if OpenSSL has already been initialized static bool cryptoInitDone = false; /*********************************************************************************************************************************** -Initialize ciphers +Throw crypto errors +***********************************************************************************************************************************/ +void +cryptoError(bool error, const char *description) +{ + FUNCTION_TEST_BEGIN(); + FUNCTION_TEST_PARAM(BOOL, error); + FUNCTION_TEST_PARAM(STRINGZ, description); + FUNCTION_TEST_END(); + + if (error) + { + const char *errorMessage = ERR_reason_error_string(ERR_get_error()); + THROW_FMT(CipherError, "%s: %s", description, errorMessage == NULL ? "no details available" : errorMessage); + } + + FUNCTION_TEST_RESULT_VOID(); +} + +/*********************************************************************************************************************************** +Initialize crypto ***********************************************************************************************************************************/ void cryptoInit(void) @@ -24,6 +45,7 @@ cryptoInit(void) if (!cryptoInitDone) { + // Load crypto strings and algorithms ERR_load_crypto_strings(); OpenSSL_add_all_algorithms(); @@ -34,7 +56,7 @@ cryptoInit(void) } /*********************************************************************************************************************************** -Have the ciphers been initialized? +Has crypto been initialized? ***********************************************************************************************************************************/ bool cryptoIsInit(void) diff --git a/src/crypto/crypto.h b/src/crypto/crypto.h index 49f16e53a..a60e331da 100644 --- a/src/crypto/crypto.h +++ b/src/crypto/crypto.h @@ -9,6 +9,7 @@ Crypto Common /*********************************************************************************************************************************** Functions ***********************************************************************************************************************************/ +void cryptoError(bool error, const char *description); void cryptoInit(void); bool cryptoIsInit(void); diff --git a/src/crypto/hash.c b/src/crypto/hash.c index 0487b1f17..0d2b6cad1 100644 --- a/src/crypto/hash.c +++ b/src/crypto/hash.c @@ -61,16 +61,15 @@ cryptoHashNew(const String *type) if ((this->hashType = EVP_get_digestbyname(strPtr(type))) == NULL) THROW_FMT(AssertError, "unable to load hash '%s'", strPtr(type)); - // Create and initialize the context - if ((this->hashContext = EVP_MD_CTX_create()) == NULL) // {uncoverable - no failure condition known} - THROW(MemoryError, "unable to create hash context"); // {+uncoverable} - - if (EVP_DigestInit_ex(this->hashContext, this->hashType, NULL) != 1) // {uncoverable - no failure condition known} - THROW(MemoryError, "unable to initialize hash context"); // {+uncoverable} + // Create context + cryptoError((this->hashContext = EVP_MD_CTX_create()) == NULL, "unable to create hash context"); // Set free callback to ensure hash context is freed memContextCallback(this->memContext, (MemContextCallback)cryptoHashFree, this); + // Initialize context + cryptoError(!EVP_DigestInit_ex(this->hashContext, this->hashType, NULL), "unable to initialize hash context"); + // Create filter interface this->filter = ioFilterNewP( strNew(CRYPTO_HASH_FILTER_TYPE), this, .in = (IoFilterInterfaceProcessIn)cryptoHashProcess, @@ -97,11 +96,7 @@ cryptoHashProcessC(CryptoHash *this, const unsigned char *message, size_t messag FUNCTION_DEBUG_ASSERT(message != NULL); FUNCTION_DEBUG_END(); - if (EVP_DigestUpdate( // {uncoverable - no failure condition known} - this->hashContext, message, messageSize) != 1) - { - THROW(MemoryError, "unable to process message hash"); // {+uncoverable} - } + cryptoError(!EVP_DigestUpdate(this->hashContext, message, messageSize), "unable to process message hash"); FUNCTION_DEBUG_RESULT_VOID(); } @@ -161,13 +156,8 @@ cryptoHash(CryptoHash *this) MEM_CONTEXT_BEGIN(this->memContext) { this->hash = bufNew((size_t)EVP_MD_size(this->hashType)); - unsigned int hashSize; - if (EVP_DigestFinal_ex( // {uncoverable - no failure condition known} - this->hashContext, bufPtr(this->hash), &hashSize) != 1) - { - THROW(MemoryError, "unable to finalize message hash"); // {+uncoverable} - } + cryptoError(!EVP_DigestFinal_ex(this->hashContext, bufPtr(this->hash), NULL), "unable to finalize message hash"); // Free the context EVP_MD_CTX_destroy(this->hashContext); diff --git a/test/define.yaml b/test/define.yaml index 0ee170974..ec828900f 100644 --- a/test/define.yaml +++ b/test/define.yaml @@ -292,7 +292,7 @@ unit: test: # ---------------------------------------------------------------------------------------------------------------------------- - name: crypto - total: 1 + total: 2 coverage: crypto/crypto: full @@ -312,7 +312,7 @@ unit: crypto/hash: full # ---------------------------------------------------------------------------------------------------------------------------- - - name: cipherBlock + - name: cipher-block total: 2 coverage: diff --git a/test/src/module/crypto/cryptoTest.c b/test/src/module/crypto/cryptoTest.c index 794fc00d8..dac9e0c92 100644 --- a/test/src/module/crypto/cryptoTest.c +++ b/test/src/module/crypto/cryptoTest.c @@ -19,5 +19,21 @@ testRun(void) TEST_RESULT_VOID(cryptoInit(), "initialize crypto again"); } + // ***************************************************************************************************************************** + if (testBegin("cryptoError")) + { + cryptoInit(); + + TEST_RESULT_VOID(cryptoError(false, "no error here"), "no error"); + + EVP_MD_CTX *context = EVP_MD_CTX_create(); + TEST_ERROR( + cryptoError(EVP_DigestInit_ex(context, NULL, NULL) != 1, "unable to initialize hash context"), CipherError, + "unable to initialize hash context: no digest set"); + EVP_MD_CTX_destroy(context); + + TEST_ERROR(cryptoError(true, "no error"), CipherError, "no error: no details available"); + } + FUNCTION_HARNESS_RESULT_VOID(); } [parent: bef31f1802f2]