os/security/crypto/weakcryptospi/test/tpadding/tpaddingSSLv3.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/crypto/weakcryptospi/test/tpadding/tpaddingSSLv3.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,359 @@
     1.4 +/*
     1.5 +* Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include <padding.h>
    1.23 +#include "tpaddingSSLv3.h"
    1.24 +#include <securityerr.h>
    1.25 +
    1.26 +CTestPadSSLv3::CTestPadSSLv3()
    1.27 +	{
    1.28 +   SetTestStepName(KPadSSLv3);
    1.29 +	}
    1.30 +
    1.31 +CTestPadSSLv3::~CTestPadSSLv3()
    1.32 +	{
    1.33 +	}
    1.34 +
    1.35 +TVerdict CTestPadSSLv3::doTestStepL()
    1.36 +	{
    1.37 +    SetTestStepResult(EPass);
    1.38 +    __UHEAP_MARK;
    1.39 +
    1.40 +	INFO_PRINTF1(_L("Test of padding with type SSLv3"));
    1.41 +
    1.42 +	TInt blockSize;
    1.43 +    TInt textSize;
    1.44 +
    1.45 +	if (!GetStringFromConfig(ConfigSection(), _L("TestCaseName"), iTestCaseName))
    1.46 + 		{
    1.47 + 		INFO_PRINTF1(_L("Could not find TestCaseName in tpadSSLv3.ini"));
    1.48 +      	return EFail;
    1.49 + 		}
    1.50 +   
    1.51 +    if (!GetIntFromConfig(ConfigSection(), _L("BlockSize"), blockSize))
    1.52 + 		{
    1.53 + 		INFO_PRINTF1(_L("Could not find blocksize in tpadSSLv3.ini"));
    1.54 +      	return EFail;
    1.55 + 		}
    1.56 + 		
    1.57 +  	if(iTestCaseName.Compare(_L("CipherAES_CBC"))==0 || iTestCaseName.Compare(_L("CipherDES_CBC"))==0 || iTestCaseName.Compare(_L("CipherRC2_CBC"))==0)
    1.58 + 		{
    1.59 + 		TestSSLv3Padding(blockSize);
    1.60 + 		}
    1.61 + 	else
    1.62 + 		{
    1.63 + 		if (!GetIntFromConfig(ConfigSection(), _L("TextSize"), textSize))
    1.64 +   			{
    1.65 +      		INFO_PRINTF1(_L("Could not find TextSize in tpadSSLv3.ini"));
    1.66 +      		return EFail;
    1.67 +   			}
    1.68 +   		if (!GetStringFromConfig(ConfigSection(), _L("ExpectedResult"), iExpectedResult))
    1.69 + 			{
    1.70 + 			INFO_PRINTF1(_L("Could not find TextSize in tpadSSLv3.ini"));
    1.71 +      		return EFail;
    1.72 + 			}
    1.73 + 		TestSSLv3CorruptPadding(blockSize, textSize);	
    1.74 + 		}
    1.75 +	__UHEAP_MARKEND;
    1.76 +	return TestStepResult();
    1.77 +	}
    1.78 +
    1.79 +void CTestPadSSLv3::TestSSLv3Padding(TInt aBlockSize)
    1.80 +	{
    1.81 +	CPaddingSSLv3 *padding = CPaddingSSLv3::NewLC(aBlockSize);
    1.82 +
    1.83 +	for (TInt i = 0 ; i <= aBlockSize; i++)
    1.84 +		{
    1.85 +		HBufC8 *padInData = HBufC8::NewLC(i);
    1.86 +		HBufC8 *padOutData = HBufC8::NewLC(i+(aBlockSize-i%aBlockSize));
    1.87 +		TPtr8 in(padInData->Des());
    1.88 +		TPtr8 out(padOutData->Des());
    1.89 +		TInt j;
    1.90 +      
    1.91 +		for (j = 0; j < i; j++)
    1.92 +			{
    1.93 +			TInt text('a'+j%25);
    1.94 +			in.Append(text);
    1.95 +			}
    1.96 +		TRAPD(err, padding->PadL(in, out));
    1.97 +		INFO_PRINTF3(_L("The error returned for input size %d is %d"), i, err);
    1.98 +		TEST(err == KErrNone);
    1.99 +
   1.100 +		TInt totalLength = out.Length();
   1.101 +		TInt paddingLength = aBlockSize - in.Length()%aBlockSize;
   1.102 +		// Test that the total length is a multiple of blockSize
   1.103 +		TEST((totalLength % aBlockSize) == 0);
   1.104 +      
   1.105 +		// Test that the padding bytes are equal in value to the paddingLength,
   1.106 +		// ie, if padding length is 5 the 5 last octets in the out array should be 0x05
   1.107 +		// This is according to RFC2246 (TLS1.0). The padding content in SSL3.0 is arbitrary.
   1.108 +		for (TInt i = paddingLength; i > 0 ; i--)
   1.109 +			{
   1.110 +			TEST(out[out.Length()-i] == paddingLength - 1);
   1.111 +			}
   1.112 +
   1.113 +		// Test that the data has not been corrupted
   1.114 +		TEST(in == out.Left(out.Length() - paddingLength));
   1.115 +      
   1.116 +		CleanupStack::PopAndDestroy(2, padInData); // padInData, padOutData
   1.117 +		}
   1.118 +	CleanupStack::PopAndDestroy(padding);
   1.119 +	}
   1.120 +
   1.121 +void CTestPadSSLv3::TestSSLv3CorruptPadding(TInt aBlockSize, TInt aTextSize)
   1.122 +	{
   1.123 +	CPaddingSSLv3 *padding = CPaddingSSLv3::NewLC(aBlockSize);
   1.124 +
   1.125 +	HBufC8 *padInData = HBufC8::NewLC(aTextSize);
   1.126 +	TInt paddingBytes = 0;
   1.127 +	//If BlockSize is 0, Divide by 0 is undefined
   1.128 +	if(aBlockSize != 0)
   1.129 +		{
   1.130 +		paddingBytes = (aBlockSize - aTextSize % aBlockSize);
   1.131 +		}
   1.132 +	HBufC8 *padOutData = HBufC8::NewLC(aTextSize + paddingBytes);
   1.133 +	TPtr8 in(padInData->Des());
   1.134 +	TPtr8 out(padOutData->Des());
   1.135 +	TInt j;
   1.136 +      
   1.137 +	for (j = 0; j < aTextSize; j++)
   1.138 +   		{
   1.139 +	 	TInt text('a'+j%25);
   1.140 +	 	in.Append(text);
   1.141 +      	}
   1.142 +	TRAPD(err, padding->PadL(in, out));
   1.143 +	
   1.144 +  
   1.145 +	if(iExpectedResult.Compare(_L("CorruptBlockSize")) ==0)
   1.146 + 		{
   1.147 + 		TEST(err == KErrArgument);
   1.148 + 		}
   1.149 +	else if(iExpectedResult.Compare(_L("Valid")) ==0)
   1.150 + 		{
   1.151 + 		TEST(err == KErrNone);
   1.152 + 		}
   1.153 +	else if(iExpectedResult.Compare(_L("InvalidPadding")) == 0)
   1.154 +		{
   1.155 +		TEST(err == KErrInvalidPadding);
   1.156 +		}	
   1.157 +
   1.158 +	//Skip the check of padded data if the padding is unsuccessful(no padding is done),
   1.159 +	//otherwise we'll get panic on erroneous operations on output descriptor.
   1.160 +	if(err != KErrNone)
   1.161 +		{
   1.162 +		CleanupStack::PopAndDestroy(2, padInData); // padInData, padOutData
   1.163 +		CleanupStack::PopAndDestroy(padding);
   1.164 +		return;
   1.165 +		}
   1.166 +	
   1.167 +	TInt totalLength = out.Length();
   1.168 +	TInt paddingLength = 0;
   1.169 +	//If BlockSize is 0, Divide by 0 is undefined
   1.170 +	if(aBlockSize != 0)
   1.171 +		{
   1.172 +      	paddingLength = aBlockSize - in.Length()%aBlockSize;
   1.173 +		}
   1.174 +	//If BlockSize is 0, Divide by 0 is undefined
   1.175 +	if(aBlockSize != 0)
   1.176 +		{
   1.177 +   		// Test that the total length is a multiple of blockSize
   1.178 +		TEST((totalLength % aBlockSize) == 0);
   1.179 +		}
   1.180 +      
   1.181 +	// Test that the padding bytes are equal in value to the paddingLength,
   1.182 +	// ie, if padding length is 5 the 5 last octets in the out array should be 0x05
   1.183 +	// This is according to RFC2246 (TLS1.0). The padding content in SSL3.0 is arbitrary.
   1.184 +	for (TInt i = paddingLength; i > 0 ; i--)
   1.185 +		{
   1.186 +	 	TEST(out[out.Length()-i] == paddingLength - 1);
   1.187 +      	}
   1.188 +
   1.189 +	if(aBlockSize > 0)
   1.190 +   		{
   1.191 +   		// Test that the data has not been corrupted
   1.192 +   		TEST(in == out.Left(out.Length() - paddingLength));
   1.193 +   		}
   1.194 +          
   1.195 +	CleanupStack::PopAndDestroy(2, padInData); // padInData, padOutData
   1.196 +	CleanupStack::PopAndDestroy(padding);
   1.197 +	}
   1.198 +
   1.199 +CTestUnpadSSLv3::CTestUnpadSSLv3()
   1.200 +	{
   1.201 +	SetTestStepName(KUnpadSSLv3);
   1.202 +	}
   1.203 +
   1.204 +CTestUnpadSSLv3::~CTestUnpadSSLv3()
   1.205 +	{
   1.206 +	}
   1.207 +
   1.208 +TVerdict CTestUnpadSSLv3::doTestStepL()
   1.209 +	{
   1.210 +	SetTestStepResult(EPass);
   1.211 +	__UHEAP_MARK;
   1.212 +
   1.213 +	INFO_PRINTF1(_L("Test of unpadding with type SSLv3"));
   1.214 +
   1.215 +	TInt blockSize;
   1.216 +	TInt textSize;
   1.217 +	if (!GetIntFromConfig(ConfigSection(), _L("BlockSize"), blockSize))
   1.218 + 		{
   1.219 + 		INFO_PRINTF1(_L("Could not find blocksize in tpadSSLv3.ini"));
   1.220 +      	return EFail;
   1.221 + 		}
   1.222 + 		
   1.223 + 	if (!GetStringFromConfig(ConfigSection(), _L("TestCaseName"), iTestCaseName))
   1.224 + 		{
   1.225 + 		INFO_PRINTF1(_L("Could not find TestCaseName in tpadSSLv3.ini"));
   1.226 +      	return EFail;
   1.227 + 		}	
   1.228 + 	
   1.229 + 	if(iTestCaseName.Compare(_L("CipherAES_CBC"))==0 || iTestCaseName.Compare(_L("CipherDES_CBC"))==0 || iTestCaseName.Compare(_L("CipherRC2_CBC"))==0)
   1.230 + 		{
   1.231 + 		TestSSLv3Unpadding(blockSize);
   1.232 + 		}
   1.233 + 	else
   1.234 + 		{
   1.235 + 		if (!GetIntFromConfig(ConfigSection(), _L("TextSize"), textSize))
   1.236 +   			{
   1.237 +      		INFO_PRINTF1(_L("Could not find TextSize in tpadSSLv3.ini"));
   1.238 +      		return EFail;
   1.239 +   			}
   1.240 +   			
   1.241 +   		if (!GetStringFromConfig(ConfigSection(), _L("ExpectedResult"), iExpectedResult))
   1.242 + 			{
   1.243 + 			INFO_PRINTF1(_L("Could not find TextSize in tpadSSLv3.ini"));
   1.244 +      		return EFail;
   1.245 + 			}
   1.246 + 		TestSSLv3CorruptUnpadding(blockSize, textSize);	
   1.247 + 		}
   1.248 +
   1.249 +	__UHEAP_MARKEND;
   1.250 +	return TestStepResult();
   1.251 +	}
   1.252 +
   1.253 +void CTestUnpadSSLv3::TestSSLv3Unpadding(TInt aBlockSize)
   1.254 +	{
   1.255 +	CPaddingSSLv3 *padding = CPaddingSSLv3::NewLC(aBlockSize);
   1.256 +
   1.257 +	for (TInt i = 0 ; i <= aBlockSize; i++)
   1.258 +		{
   1.259 +		HBufC8 *padInData = HBufC8::NewLC(i+(aBlockSize - i%aBlockSize));
   1.260 +		HBufC8 *padOutData = HBufC8::NewLC(i);
   1.261 +		HBufC8 *padCompareData = HBufC8::NewLC(i);
   1.262 +		TPtr8 in(padInData->Des());
   1.263 +		TPtr8 out(padOutData->Des());
   1.264 +		TPtr8 comp(padCompareData->Des());
   1.265 +		TInt j;
   1.266 +      
   1.267 +		// build up a padded string here
   1.268 +		for (j = 0; j < i; j++)
   1.269 +			{
   1.270 +			TInt text('a'+j%25);
   1.271 +			in.Append(text);
   1.272 +			}
   1.273 +		comp.Append(in);
   1.274 +      
   1.275 +		TInt paddingBytes = aBlockSize - i%aBlockSize;
   1.276 +		in.SetLength(in.Length() + paddingBytes);
   1.277 +
   1.278 +		// pad with arbitary data, to test unpadding of SSL 3.0 padded data
   1.279 +		in[in.Length()-1] = (TUint8) (paddingBytes - 1);
   1.280 +		for (j = 2; j <= paddingBytes; j++)
   1.281 +			{
   1.282 +			in[in.Length()-j] = (TUint8) ('a' + j%25);
   1.283 +			}
   1.284 +		TRAPD(err, padding->UnPadL(in, out));
   1.285 +		INFO_PRINTF3(_L("The error returned for input size %d is %d"), i, err);
   1.286 +		TEST(err == KErrNone);
   1.287 +
   1.288 +		// test if the unpadding was correct.
   1.289 +		TEST(out == comp);
   1.290 +      
   1.291 +		CleanupStack::PopAndDestroy(3, padInData); // padInData, padOutData, padCompareData
   1.292 +		}
   1.293 +	CleanupStack::PopAndDestroy(padding);
   1.294 +	}
   1.295 +
   1.296 +void CTestUnpadSSLv3::TestSSLv3CorruptUnpadding(TInt aBlockSize, TInt aTextSize)
   1.297 +	{
   1.298 +	CPaddingSSLv3 *padding = CPaddingSSLv3::NewLC(aBlockSize);
   1.299 +   
   1.300 +	TInt paddingBytes = 0;
   1.301 +	//If BlockSize is 0, Divide by 0 is undefined
   1.302 +	if(aBlockSize != 0)
   1.303 +   		{
   1.304 +   		paddingBytes = aBlockSize - aTextSize%aBlockSize;
   1.305 +   		}
   1.306 +	HBufC8 *padInData = HBufC8::NewLC(aTextSize+ paddingBytes);
   1.307 +	HBufC8 *padOutData = HBufC8::NewLC(aTextSize);
   1.308 +	HBufC8 *padCompareData = HBufC8::NewLC(aTextSize);
   1.309 +	TPtr8 in(padInData->Des());
   1.310 +	TPtr8 out(padOutData->Des());
   1.311 +	TPtr8 comp(padCompareData->Des());
   1.312 +	TInt j;
   1.313 +   
   1.314 +	if(in.Length() < aTextSize)
   1.315 +		{
   1.316 +		for (j = 0; j < in.Length(); j++)
   1.317 +   			{
   1.318 +	 		TInt text('a'+j%25);
   1.319 +	 		in.Append(text);
   1.320 +      		}
   1.321 +		}
   1.322 +	else
   1.323 +		{
   1.324 +		// build up a padded string here
   1.325 +		for (j = 0; j < aTextSize; j++)
   1.326 +   			{
   1.327 +	 		TInt text('a'+j%25);
   1.328 +	 		in.Append(text);
   1.329 +      		}
   1.330 +		}
   1.331 +	comp.Append(in);
   1.332 +   
   1.333 +	in.SetLength(in.Length() + paddingBytes);
   1.334 +
   1.335 +	if(aBlockSize > 0)
   1.336 +  		{
   1.337 +   		// pad with arbitary data, to test unpadding of SSL 3.0 padded data
   1.338 +   		in[in.Length()-1] = (TUint8) (paddingBytes - 1);
   1.339 +   		for (j = 2; j <= paddingBytes; j++)
   1.340 +   			{
   1.341 +	 		in[in.Length()-j] = (TUint8) ('a' + j%25);
   1.342 +      		}
   1.343 +  		 }  
   1.344 +	TRAPD(err, padding->UnPadL(in, out));
   1.345 +   
   1.346 +    if(iExpectedResult.Compare(_L("CorruptBlockSize")) ==0)
   1.347 + 		{
   1.348 + 		TEST(err == KErrArgument);
   1.349 + 		}
   1.350 +	else if(iExpectedResult.Compare(_L("Valid")) ==0)
   1.351 + 		{
   1.352 + 		TEST(err == KErrNone);
   1.353 + 		}
   1.354 +	else if(iExpectedResult.Compare(_L("InvalidPadding")) == 0)
   1.355 +		{
   1.356 +		TEST(err == KErrInvalidPadding);
   1.357 +		}
   1.358 +	// test if the unpadding was correct.
   1.359 +	TEST(out == comp);
   1.360 +	CleanupStack::PopAndDestroy(3, padInData); // padInData, padOutData, padCompareData
   1.361 +	CleanupStack::PopAndDestroy(padding);
   1.362 +	}