[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Bug#851092: patch



Control: tags -1 + patch
Control: forwarded -1 https://github.com/boxbackup/boxbackup/issues/16

The above patch changes a single object into a pointer, and changes
init/cleanup into new/free.

Chris.

commit 9d2bf90676206957a502e9ec1c3cfe4f4b40b0cc
Author: Chris West (Faux) <git@goeswhere.com>
Date:   Thu Jun 1 17:01:16 2017 +0000

    dynamically allocate EVP_CTX

diff --git a/boxbackup-0.11.1~r2837/debian/control b/boxbackup-0.11.1~r2837/debian/control
index 5cbdba6..d422125 100644
--- a/boxbackup-0.11.1~r2837/debian/control
+++ b/boxbackup-0.11.1~r2837/debian/control
@@ -12,7 +12,7 @@ Build-Depends:
  docbook-xsl,
  libdb-dev (>= 4.7),
  libedit-dev,
- libssl1.0-dev,
+ libssl-dev,
  libtest-lwp-useragent-perl,
  xsltproc,
  zlib1g-dev
diff --git a/boxbackup-0.11.1~r2837/lib/crypto/CipherContext.cpp b/boxbackup-0.11.1~r2837/lib/crypto/CipherContext.cpp
index e5cd9b0..f23317f 100644
--- a/boxbackup-0.11.1~r2837/lib/crypto/CipherContext.cpp
+++ b/boxbackup-0.11.1~r2837/lib/crypto/CipherContext.cpp
@@ -49,7 +49,7 @@ CipherContext::~CipherContext()
 	if(mInitialised)
 	{
 		// Clean up
-		EVP_CIPHER_CTX_cleanup(&ctx);
+		EVP_CIPHER_CTX_free(ctx);
 		mInitialised = false;
 	}
 #ifdef HAVE_OLD_SSL
@@ -84,9 +84,9 @@ void CipherContext::Init(CipherContext::CipherFunction Function, const CipherDes
 	
 	// Initialise the cipher
 #ifndef HAVE_OLD_SSL
-	EVP_CIPHER_CTX_init(&ctx); // no error return code, even though the docs says it does
+	ctx = EVP_CIPHER_CTX_new();
 
-	if(EVP_CipherInit_ex(&ctx, rDescription.GetCipher(), NULL, NULL, NULL, Function) != 1)
+	if(EVP_CipherInit_ex(ctx, rDescription.GetCipher(), NULL, NULL, NULL, Function) != 1)
 #else
 	// Store function for later
 	mFunction = Function;
@@ -102,19 +102,19 @@ void CipherContext::Init(CipherContext::CipherFunction Function, const CipherDes
 	{
 #ifndef HAVE_OLD_SSL
 		// Let the description set up everything else
-		rDescription.SetupParameters(&ctx);
+		rDescription.SetupParameters(ctx);
 #else
 		// With the old version, a copy needs to be taken first.
 		mpDescription = rDescription.Clone();
 		// Mark it as not a leak, otherwise static cipher contexts
 		// cause spurious memory leaks to be reported
 		MEMLEAKFINDER_NOT_A_LEAK(mpDescription);
-		mpDescription->SetupParameters(&ctx);
+		mpDescription->SetupParameters(ctx);
 #endif
 	}
 	catch(...)
 	{
-		EVP_CIPHER_CTX_cleanup(&ctx);
+		EVP_CIPHER_CTX_free(ctx);
 		throw;
 	}
 
@@ -135,7 +135,7 @@ void CipherContext::Reset()
 	if(mInitialised)
 	{
 		// Clean up
-		EVP_CIPHER_CTX_cleanup(&ctx);
+		EVP_CIPHER_CTX_cleanup(ctx);
 		mInitialised = false;
 	}
 #ifdef HAVE_OLD_SSL
@@ -172,7 +172,7 @@ void CipherContext::Begin()
 	}
 
 	// Initialise the cipher context again
-	if(EVP_CipherInit(&ctx, NULL, NULL, NULL, -1) != 1)
+	if(EVP_CipherInit(ctx, NULL, NULL, NULL, -1) != 1)
 	{
 		THROW_EXCEPTION(CipherException, EVPInitFailure)
 	}
@@ -218,14 +218,14 @@ int CipherContext::Transform(void *pOutBuffer, int OutLength, const void *pInBuf
 	}
 	
 	// Check output buffer size
-	if(OutLength < (InLength + EVP_CIPHER_CTX_block_size(&ctx)))
+	if(OutLength < (InLength + EVP_CIPHER_CTX_block_size(ctx)))
 	{
 		THROW_EXCEPTION(CipherException, OutputBufferTooSmall);
 	}
 	
 	// Do the transform
 	int outLength = OutLength;
-	if(EVP_CipherUpdate(&ctx, (unsigned char*)pOutBuffer, &outLength, (unsigned char*)pInBuffer, InLength) != 1)
+	if(EVP_CipherUpdate(ctx, (unsigned char*)pOutBuffer, &outLength, (unsigned char*)pInBuffer, InLength) != 1)
 	{
 		THROW_EXCEPTION(CipherException, EVPUpdateFailure)
 	}
@@ -265,7 +265,7 @@ int CipherContext::Final(void *pOutBuffer, int OutLength)
 	}
 
 	// Check output buffer size
-	if(OutLength < (2 * EVP_CIPHER_CTX_block_size(&ctx)))
+	if(OutLength < (2 * EVP_CIPHER_CTX_block_size(ctx)))
 	{
 		THROW_EXCEPTION(CipherException, OutputBufferTooSmall);
 	}
@@ -273,7 +273,7 @@ int CipherContext::Final(void *pOutBuffer, int OutLength)
 	// Do the transform
 	int outLength = OutLength;
 #ifndef HAVE_OLD_SSL
-	if(EVP_CipherFinal_ex(&ctx, (unsigned char*)pOutBuffer, &outLength) != 1)
+	if(EVP_CipherFinal_ex(ctx, (unsigned char*)pOutBuffer, &outLength) != 1)
 	{
 		THROW_EXCEPTION(CipherException, EVPFinalFailure)
 	}
@@ -302,11 +302,11 @@ void CipherContext::OldOpenSSLFinal(unsigned char *Buffer, int &rOutLengthOut)
 	// Old version needs to use a different form, and then set up the cipher again for next time around
 	int outLength = rOutLengthOut;
 	// Have to emulate padding off...
-	int blockSize = EVP_CIPHER_CTX_block_size(&ctx);
+	int blockSize = EVP_CIPHER_CTX_block_size(ctx);
 	if(mPaddingOn)
 	{
 		// Just use normal final call
-		if(EVP_CipherFinal(&ctx, Buffer, &outLength) != 1)
+		if(EVP_CipherFinal(ctx, Buffer, &outLength) != 1)
 		{
 			THROW_EXCEPTION(CipherException, EVPFinalFailure)
 		}
@@ -319,13 +319,13 @@ void CipherContext::OldOpenSSLFinal(unsigned char *Buffer, int &rOutLengthOut)
 		{
 			// NASTY -- fiddling around with internals like this is bad.
 			// But only way to get this working on old versions of OpenSSL.
-			if(!EVP_EncryptUpdate(&ctx,Buffer,&outLength,ctx.buf,0)
+			if(!EVP_EncryptUpdate(ctx,Buffer,&outLength,ctx.buf,0)
 				|| outLength != blockSize)
 			{
 				THROW_EXCEPTION(CipherException, EVPFinalFailure)
 			}
 			// Clean up
-			EVP_CIPHER_CTX_cleanup(&ctx);
+			EVP_CIPHER_CTX_free(ctx);
 		}
 		else
 		{
@@ -353,11 +353,11 @@ void CipherContext::OldOpenSSLFinal(unsigned char *Buffer, int &rOutLengthOut)
 		}
 	}
 	// Reinitialise the cipher for the next time around
-	if(EVP_CipherInit(&ctx, mpDescription->GetCipher(), NULL, NULL, mFunction) != 1)
+	if(EVP_CipherInit(ctx, mpDescription->GetCipher(), NULL, NULL, mFunction) != 1)
 	{
 		THROW_EXCEPTION(CipherException, EVPInitFailure)
 	}
-	mpDescription->SetupParameters(&ctx);
+	mpDescription->SetupParameters(ctx);
 
 	// Update length for caller
 	rOutLengthOut = outLength;
@@ -382,7 +382,7 @@ int CipherContext::InSizeForOutBufferSize(int OutLength)
 
 	// Strictly speaking, the *2 is unnecessary. However... 
 	// Final() is paranoid, and requires two input blocks of space to work.
-	return OutLength - (EVP_CIPHER_CTX_block_size(&ctx) * 2);
+	return OutLength - (EVP_CIPHER_CTX_block_size(ctx) * 2);
 }
 
 // --------------------------------------------------------------------------
@@ -403,7 +403,7 @@ int CipherContext::MaxOutSizeForInBufferSize(int InLength)
 
 	// Final() is paranoid, and requires two input blocks of space to work, and so we need to add
 	// three blocks on to be absolutely sure.
-	return InLength + (EVP_CIPHER_CTX_block_size(&ctx) * 3);
+	return InLength + (EVP_CIPHER_CTX_block_size(ctx) * 3);
 }
 
 
@@ -430,7 +430,7 @@ int CipherContext::TransformBlock(void *pOutBuffer, int OutLength, const void *p
 	}
 
 	// Check output buffer size
-	if(OutLength < (InLength + EVP_CIPHER_CTX_block_size(&ctx)))
+	if(OutLength < (InLength + EVP_CIPHER_CTX_block_size(ctx)))
 	{
 		// Check if padding is off, in which case the buffer can be smaller
 		if(!mPaddingOn && OutLength <= InLength)
@@ -444,7 +444,7 @@ int CipherContext::TransformBlock(void *pOutBuffer, int OutLength, const void *p
 	}
 	
 	// Initialise the cipher context again
-	if(EVP_CipherInit(&ctx, NULL, NULL, NULL, -1) != 1)
+	if(EVP_CipherInit(ctx, NULL, NULL, NULL, -1) != 1)
 	{
 		THROW_EXCEPTION(CipherException, EVPInitFailure)
 	}
@@ -455,14 +455,14 @@ int CipherContext::TransformBlock(void *pOutBuffer, int OutLength, const void *p
 	{
 		// Update
 		outLength = OutLength;
-		if(EVP_CipherUpdate(&ctx, (unsigned char*)pOutBuffer, &outLength, (unsigned char*)pInBuffer, InLength) != 1)
+		if(EVP_CipherUpdate(ctx, (unsigned char*)pOutBuffer, &outLength, (unsigned char*)pInBuffer, InLength) != 1)
 		{
 			THROW_EXCEPTION(CipherException, EVPUpdateFailure)
 		}
 		// Finalise
 		int outLength2 = OutLength - outLength;
 #ifndef HAVE_OLD_SSL
-		if(EVP_CipherFinal_ex(&ctx, ((unsigned char*)pOutBuffer) + outLength, &outLength2) != 1)
+		if(EVP_CipherFinal_ex(ctx, ((unsigned char*)pOutBuffer) + outLength, &outLength2) != 1)
 		{
 			THROW_EXCEPTION(CipherException, EVPFinalFailure)
 		}
@@ -476,7 +476,7 @@ int CipherContext::TransformBlock(void *pOutBuffer, int OutLength, const void *p
 		// Finalise the context, so definately ready for the next caller
 		int outs = OutLength;
 #ifndef HAVE_OLD_SSL
-		EVP_CipherFinal_ex(&ctx, (unsigned char*)pOutBuffer, &outs);
+		EVP_CipherFinal_ex(ctx, (unsigned char*)pOutBuffer, &outs);
 #else
 		OldOpenSSLFinal((unsigned char*)pOutBuffer, outs);
 #endif
@@ -502,7 +502,7 @@ int CipherContext::GetIVLength()
 		THROW_EXCEPTION(CipherException, NotInitialised)
 	}
 	
-	return EVP_CIPHER_CTX_iv_length(&ctx);
+	return EVP_CIPHER_CTX_iv_length(ctx);
 }
 
 
@@ -529,7 +529,7 @@ void CipherContext::SetIV(const void *pIV)
 	}
 
 	// Set IV
-	if(EVP_CipherInit(&ctx, NULL, NULL, (unsigned char *)pIV, -1) != 1)
+	if(EVP_CipherInit(ctx, NULL, NULL, (unsigned char *)pIV, -1) != 1)
 	{
 		THROW_EXCEPTION(CipherException, EVPInitFailure)
 	}
@@ -568,7 +568,7 @@ const void *CipherContext::SetRandomIV(int &rLengthOut)
 	}
 
 	// Get length of IV
-	unsigned int ivLen = EVP_CIPHER_CTX_iv_length(&ctx);
+	unsigned int ivLen = EVP_CIPHER_CTX_iv_length(ctx);
 	if(ivLen > sizeof(mGeneratedIV))
 	{
 		THROW_EXCEPTION(CipherException, IVSizeImplementationLimitExceeded)
@@ -578,7 +578,7 @@ const void *CipherContext::SetRandomIV(int &rLengthOut)
 	Random::Generate(mGeneratedIV, ivLen);
 
 	// Set IV
-	if(EVP_CipherInit(&ctx, NULL, NULL, mGeneratedIV, -1) != 1)
+	if(EVP_CipherInit(ctx, NULL, NULL, mGeneratedIV, -1) != 1)
 	{
 		THROW_EXCEPTION(CipherException, EVPInitFailure)
 	}	
@@ -608,7 +608,7 @@ const void *CipherContext::SetRandomIV(int &rLengthOut)
 void CipherContext::UsePadding(bool Padding)
 {
 #ifndef HAVE_OLD_SSL
-	if(EVP_CIPHER_CTX_set_padding(&ctx, Padding) != 1)
+	if(EVP_CIPHER_CTX_set_padding(ctx, Padding) != 1)
 	{
 		THROW_EXCEPTION(CipherException, EVPSetPaddingFailure)
 	}
diff --git a/boxbackup-0.11.1~r2837/lib/crypto/CipherContext.h b/boxbackup-0.11.1~r2837/lib/crypto/CipherContext.h
index 64ce52d..10043c2 100644
--- a/boxbackup-0.11.1~r2837/lib/crypto/CipherContext.h
+++ b/boxbackup-0.11.1~r2837/lib/crypto/CipherContext.h
@@ -67,7 +67,7 @@ public:
 #endif
 	
 private:
-	EVP_CIPHER_CTX ctx;
+	EVP_CIPHER_CTX *ctx;
 	bool mInitialised;
 	bool mWithinTransform;
 	bool mPaddingOn;

Reply to: