os/security/crypto/weakcrypto/test/tpadding/tpaddingSSLv3.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
     3 * All rights reserved.
     4 * This component and the accompanying materials are made available
     5 * under the terms of the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 *
    16 */
    17 
    18 
    19 #include <padding.h>
    20 #include "tpaddingSSLv3.h"
    21 #include <securityerr.h>
    22 
    23 CTestPadSSLv3::CTestPadSSLv3()
    24 	{
    25    SetTestStepName(KPadSSLv3);
    26 	}
    27 
    28 CTestPadSSLv3::~CTestPadSSLv3()
    29 	{
    30 	}
    31 
    32 TVerdict CTestPadSSLv3::doTestStepL()
    33 	{
    34     SetTestStepResult(EPass);
    35     __UHEAP_MARK;
    36 
    37 	INFO_PRINTF1(_L("Test of padding with type SSLv3"));
    38 
    39 	TInt blockSize;
    40     TInt textSize;
    41 
    42 	if (!GetStringFromConfig(ConfigSection(), _L("TestCaseName"), iTestCaseName))
    43  		{
    44  		INFO_PRINTF1(_L("Could not find TestCaseName in tpadSSLv3.ini"));
    45       	return EFail;
    46  		}
    47    
    48     if (!GetIntFromConfig(ConfigSection(), _L("BlockSize"), blockSize))
    49  		{
    50  		INFO_PRINTF1(_L("Could not find blocksize in tpadSSLv3.ini"));
    51       	return EFail;
    52  		}
    53  		
    54   	if(iTestCaseName.Compare(_L("CipherAES_CBC"))==0 || iTestCaseName.Compare(_L("CipherDES_CBC"))==0 || iTestCaseName.Compare(_L("CipherRC2_CBC"))==0)
    55  		{
    56  		TestSSLv3Padding(blockSize);
    57  		}
    58  	else
    59  		{
    60  		if (!GetIntFromConfig(ConfigSection(), _L("TextSize"), textSize))
    61    			{
    62       		INFO_PRINTF1(_L("Could not find TextSize in tpadSSLv3.ini"));
    63       		return EFail;
    64    			}
    65    		if (!GetStringFromConfig(ConfigSection(), _L("ExpectedResult"), iExpectedResult))
    66  			{
    67  			INFO_PRINTF1(_L("Could not find TextSize in tpadSSLv3.ini"));
    68       		return EFail;
    69  			}
    70  		TestSSLv3CorruptPadding(blockSize, textSize);	
    71  		}
    72 	__UHEAP_MARKEND;
    73 	return TestStepResult();
    74 	}
    75 
    76 void CTestPadSSLv3::TestSSLv3Padding(TInt aBlockSize)
    77 	{
    78 	CPaddingSSLv3 *padding = CPaddingSSLv3::NewLC(aBlockSize);
    79 
    80 	for (TInt i = 0 ; i <= aBlockSize; i++)
    81 		{
    82 		HBufC8 *padInData = HBufC8::NewLC(i);
    83 		HBufC8 *padOutData = HBufC8::NewLC(i+(aBlockSize-i%aBlockSize));
    84 		TPtr8 in(padInData->Des());
    85 		TPtr8 out(padOutData->Des());
    86 		TInt j;
    87       
    88 		for (j = 0; j < i; j++)
    89 			{
    90 			TInt text('a'+j%25);
    91 			in.Append(text);
    92 			}
    93 		TRAPD(err, padding->DoPadL(in, out));
    94 		INFO_PRINTF3(_L("The error returned for input size %d is %d"), i, err);
    95 		TEST(err == KErrNone);
    96 
    97 		TInt totalLength = out.Length();
    98 		TInt paddingLength = aBlockSize - in.Length()%aBlockSize;
    99 		// Test that the total length is a multiple of blockSize
   100 		TEST((totalLength % aBlockSize) == 0);
   101       
   102 		// Test that the padding bytes are equal in value to the paddingLength,
   103 		// ie, if padding length is 5 the 5 last octets in the out array should be 0x05
   104 		// This is according to RFC2246 (TLS1.0). The padding content in SSL3.0 is arbitrary.
   105 		for (TInt i = paddingLength; i > 0 ; i--)
   106 			{
   107 			TEST(out[out.Length()-i] == paddingLength - 1);
   108 			}
   109 
   110 		// Test that the data has not been corrupted
   111 		TEST(in == out.Left(out.Length() - paddingLength));
   112       
   113 		CleanupStack::PopAndDestroy(2, padInData); // padInData, padOutData
   114 		}
   115 	CleanupStack::PopAndDestroy(padding);
   116 	}
   117 
   118 void CTestPadSSLv3::TestSSLv3CorruptPadding(TInt aBlockSize, TInt aTextSize)
   119 	{
   120 	CPaddingSSLv3 *padding = CPaddingSSLv3::NewLC(aBlockSize);
   121 
   122 	HBufC8 *padInData = HBufC8::NewLC(aTextSize);
   123 	TInt paddingBytes = 0;
   124 	//If BlockSize is 0, Divide by 0 is undefined
   125 	if(aBlockSize != 0)
   126 		{
   127 		paddingBytes = (aBlockSize - aTextSize % aBlockSize);
   128 		}
   129 	HBufC8 *padOutData = HBufC8::NewLC(aTextSize + paddingBytes);
   130 	TPtr8 in(padInData->Des());
   131 	TPtr8 out(padOutData->Des());
   132 	TInt j;
   133       
   134 	for (j = 0; j < aTextSize; j++)
   135    		{
   136 	 	TInt text('a'+j%25);
   137 	 	in.Append(text);
   138       	}
   139 	TRAPD(err, padding->DoPadL(in, out));
   140   
   141 	if(iExpectedResult.Compare(_L("CorruptBlockSize")) ==0)
   142  		{
   143  		TEST(err == KErrArgument);
   144  		}
   145 	else if(iExpectedResult.Compare(_L("Valid")) ==0)
   146  		{
   147  		TEST(err == KErrNone);
   148  		}
   149 	else if(iExpectedResult.Compare(_L("InvalidPadding")) == 0)
   150 		{
   151 		TEST(err == KErrInvalidPadding);
   152 		}	
   153 
   154 	TInt totalLength = out.Length();
   155 	TInt paddingLength = 0;
   156 	//If BlockSize is 0, Divide by 0 is undefined
   157 	if(aBlockSize != 0)
   158 		{
   159       	paddingLength = aBlockSize - in.Length()%aBlockSize;
   160 		}
   161 	//If BlockSize is 0, Divide by 0 is undefined
   162 	if(aBlockSize != 0)
   163 		{
   164    		// Test that the total length is a multiple of blockSize
   165 		TEST((totalLength % aBlockSize) == 0);
   166 		}
   167       
   168 	// Test that the padding bytes are equal in value to the paddingLength,
   169 	// ie, if padding length is 5 the 5 last octets in the out array should be 0x05
   170 	// This is according to RFC2246 (TLS1.0). The padding content in SSL3.0 is arbitrary.
   171 	for (TInt i = paddingLength; i > 0 ; i--)
   172 		{
   173 	 	TEST(out[out.Length()-i] == paddingLength - 1);
   174       	}
   175 
   176 	if(aBlockSize > 0)
   177    		{
   178    		// Test that the data has not been corrupted
   179    		TEST(in == out.Left(out.Length() - paddingLength));
   180    		}
   181           
   182 	CleanupStack::PopAndDestroy(2, padInData); // padInData, padOutData
   183 	CleanupStack::PopAndDestroy(padding);
   184 	}
   185 
   186 CTestUnpadSSLv3::CTestUnpadSSLv3()
   187 	{
   188 	SetTestStepName(KUnpadSSLv3);
   189 	}
   190 
   191 CTestUnpadSSLv3::~CTestUnpadSSLv3()
   192 	{
   193 	}
   194 
   195 TVerdict CTestUnpadSSLv3::doTestStepL()
   196 	{
   197 	SetTestStepResult(EPass);
   198 	__UHEAP_MARK;
   199 
   200 	INFO_PRINTF1(_L("Test of unpadding with type SSLv3"));
   201 
   202 	TInt blockSize;
   203 	TInt textSize;
   204 	if (!GetIntFromConfig(ConfigSection(), _L("BlockSize"), blockSize))
   205  		{
   206  		INFO_PRINTF1(_L("Could not find blocksize in tpadSSLv3.ini"));
   207       	return EFail;
   208  		}
   209  		
   210  	if (!GetStringFromConfig(ConfigSection(), _L("TestCaseName"), iTestCaseName))
   211  		{
   212  		INFO_PRINTF1(_L("Could not find TestCaseName in tpadSSLv3.ini"));
   213       	return EFail;
   214  		}	
   215  	
   216  	if(iTestCaseName.Compare(_L("CipherAES_CBC"))==0 || iTestCaseName.Compare(_L("CipherDES_CBC"))==0 || iTestCaseName.Compare(_L("CipherRC2_CBC"))==0)
   217  		{
   218  		TestSSLv3Unpadding(blockSize);
   219  		}
   220  	else
   221  		{
   222  		if (!GetIntFromConfig(ConfigSection(), _L("TextSize"), textSize))
   223    			{
   224       		INFO_PRINTF1(_L("Could not find TextSize in tpadSSLv3.ini"));
   225       		return EFail;
   226    			}
   227    			
   228    		if (!GetStringFromConfig(ConfigSection(), _L("ExpectedResult"), iExpectedResult))
   229  			{
   230  			INFO_PRINTF1(_L("Could not find TextSize in tpadSSLv3.ini"));
   231       		return EFail;
   232  			}
   233  		TestSSLv3CorruptUnpadding(blockSize, textSize);	
   234  		}
   235 
   236 	__UHEAP_MARKEND;
   237 	return TestStepResult();
   238 	}
   239 
   240 void CTestUnpadSSLv3::TestSSLv3Unpadding(TInt aBlockSize)
   241 	{
   242 	CPaddingSSLv3 *padding = CPaddingSSLv3::NewLC(aBlockSize);
   243 
   244 	for (TInt i = 0 ; i <= aBlockSize; i++)
   245 		{
   246 		HBufC8 *padInData = HBufC8::NewLC(i+(aBlockSize - i%aBlockSize));
   247 		HBufC8 *padOutData = HBufC8::NewLC(i);
   248 		HBufC8 *padCompareData = HBufC8::NewLC(i);
   249 		TPtr8 in(padInData->Des());
   250 		TPtr8 out(padOutData->Des());
   251 		TPtr8 comp(padCompareData->Des());
   252 		TInt j;
   253       
   254 		// build up a padded string here
   255 		for (j = 0; j < i; j++)
   256 			{
   257 			TInt text('a'+j%25);
   258 			in.Append(text);
   259 			}
   260 		comp.Append(in);
   261       
   262 		TInt paddingBytes = aBlockSize - i%aBlockSize;
   263 		in.SetLength(in.Length() + paddingBytes);
   264 
   265 		// pad with arbitary data, to test unpadding of SSL 3.0 padded data
   266 		in[in.Length()-1] = (TUint8) (paddingBytes - 1);
   267 		for (j = 2; j <= paddingBytes; j++)
   268 			{
   269 			in[in.Length()-j] = (TUint8) ('a' + j%25);
   270 			}
   271 		TRAPD(err, padding->UnPadL(in, out));
   272 		INFO_PRINTF3(_L("The error returned for input size %d is %d"), i, err);
   273 		TEST(err == KErrNone);
   274 
   275 		// test if the unpadding was correct.
   276 		TEST(out == comp);
   277       
   278 		CleanupStack::PopAndDestroy(3, padInData); // padInData, padOutData, padCompareData
   279 		}
   280 	CleanupStack::PopAndDestroy(padding);
   281 	}
   282 
   283 void CTestUnpadSSLv3::TestSSLv3CorruptUnpadding(TInt aBlockSize, TInt aTextSize)
   284 	{
   285 	CPaddingSSLv3 *padding = CPaddingSSLv3::NewLC(aBlockSize);
   286    
   287 	TInt paddingBytes = 0;
   288 	//If BlockSize is 0, Divide by 0 is undefined
   289 	if(aBlockSize != 0)
   290    		{
   291    		paddingBytes = aBlockSize - aTextSize%aBlockSize;
   292    		}
   293 	HBufC8 *padInData = HBufC8::NewLC(aTextSize+ paddingBytes);
   294 	HBufC8 *padOutData = HBufC8::NewLC(aTextSize);
   295 	HBufC8 *padCompareData = HBufC8::NewLC(aTextSize);
   296 	TPtr8 in(padInData->Des());
   297 	TPtr8 out(padOutData->Des());
   298 	TPtr8 comp(padCompareData->Des());
   299 	TInt j;
   300    
   301 	if(in.Length() < aTextSize)
   302 		{
   303 		for (j = 0; j < in.Length(); j++)
   304    			{
   305 	 		TInt text('a'+j%25);
   306 	 		in.Append(text);
   307       		}
   308 		}
   309 	else
   310 		{
   311 		// build up a padded string here
   312 		for (j = 0; j < aTextSize; j++)
   313    			{
   314 	 		TInt text('a'+j%25);
   315 	 		in.Append(text);
   316       		}
   317 		}
   318 	comp.Append(in);
   319    
   320 	in.SetLength(in.Length() + paddingBytes);
   321 
   322 	if(aBlockSize > 0)
   323   		{
   324    		// pad with arbitary data, to test unpadding of SSL 3.0 padded data
   325    		in[in.Length()-1] = (TUint8) (paddingBytes - 1);
   326    		for (j = 2; j <= paddingBytes; j++)
   327    			{
   328 	 		in[in.Length()-j] = (TUint8) ('a' + j%25);
   329       		}
   330   		 }  
   331 	TRAPD(err, padding->UnPadL(in, out));
   332    
   333     if(iExpectedResult.Compare(_L("CorruptBlockSize")) ==0)
   334  		{
   335  		TEST(err == KErrArgument);
   336  		}
   337 	else if(iExpectedResult.Compare(_L("Valid")) ==0)
   338  		{
   339  		TEST(err == KErrNone);
   340  		}
   341 	else if(iExpectedResult.Compare(_L("InvalidPadding")) == 0)
   342 		{
   343 		TEST(err == KErrInvalidPadding);
   344 		}
   345 	// test if the unpadding was correct.
   346 	TEST(out == comp);
   347 	CleanupStack::PopAndDestroy(3, padInData); // padInData, padOutData, padCompareData
   348 	CleanupStack::PopAndDestroy(padding);
   349 	}