os/security/crypto/weakcryptospi/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->PadL(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->PadL(in, out));
   140 	
   141   
   142 	if(iExpectedResult.Compare(_L("CorruptBlockSize")) ==0)
   143  		{
   144  		TEST(err == KErrArgument);
   145  		}
   146 	else if(iExpectedResult.Compare(_L("Valid")) ==0)
   147  		{
   148  		TEST(err == KErrNone);
   149  		}
   150 	else if(iExpectedResult.Compare(_L("InvalidPadding")) == 0)
   151 		{
   152 		TEST(err == KErrInvalidPadding);
   153 		}	
   154 
   155 	//Skip the check of padded data if the padding is unsuccessful(no padding is done),
   156 	//otherwise we'll get panic on erroneous operations on output descriptor.
   157 	if(err != KErrNone)
   158 		{
   159 		CleanupStack::PopAndDestroy(2, padInData); // padInData, padOutData
   160 		CleanupStack::PopAndDestroy(padding);
   161 		return;
   162 		}
   163 	
   164 	TInt totalLength = out.Length();
   165 	TInt paddingLength = 0;
   166 	//If BlockSize is 0, Divide by 0 is undefined
   167 	if(aBlockSize != 0)
   168 		{
   169       	paddingLength = aBlockSize - in.Length()%aBlockSize;
   170 		}
   171 	//If BlockSize is 0, Divide by 0 is undefined
   172 	if(aBlockSize != 0)
   173 		{
   174    		// Test that the total length is a multiple of blockSize
   175 		TEST((totalLength % aBlockSize) == 0);
   176 		}
   177       
   178 	// Test that the padding bytes are equal in value to the paddingLength,
   179 	// ie, if padding length is 5 the 5 last octets in the out array should be 0x05
   180 	// This is according to RFC2246 (TLS1.0). The padding content in SSL3.0 is arbitrary.
   181 	for (TInt i = paddingLength; i > 0 ; i--)
   182 		{
   183 	 	TEST(out[out.Length()-i] == paddingLength - 1);
   184       	}
   185 
   186 	if(aBlockSize > 0)
   187    		{
   188    		// Test that the data has not been corrupted
   189    		TEST(in == out.Left(out.Length() - paddingLength));
   190    		}
   191           
   192 	CleanupStack::PopAndDestroy(2, padInData); // padInData, padOutData
   193 	CleanupStack::PopAndDestroy(padding);
   194 	}
   195 
   196 CTestUnpadSSLv3::CTestUnpadSSLv3()
   197 	{
   198 	SetTestStepName(KUnpadSSLv3);
   199 	}
   200 
   201 CTestUnpadSSLv3::~CTestUnpadSSLv3()
   202 	{
   203 	}
   204 
   205 TVerdict CTestUnpadSSLv3::doTestStepL()
   206 	{
   207 	SetTestStepResult(EPass);
   208 	__UHEAP_MARK;
   209 
   210 	INFO_PRINTF1(_L("Test of unpadding with type SSLv3"));
   211 
   212 	TInt blockSize;
   213 	TInt textSize;
   214 	if (!GetIntFromConfig(ConfigSection(), _L("BlockSize"), blockSize))
   215  		{
   216  		INFO_PRINTF1(_L("Could not find blocksize in tpadSSLv3.ini"));
   217       	return EFail;
   218  		}
   219  		
   220  	if (!GetStringFromConfig(ConfigSection(), _L("TestCaseName"), iTestCaseName))
   221  		{
   222  		INFO_PRINTF1(_L("Could not find TestCaseName in tpadSSLv3.ini"));
   223       	return EFail;
   224  		}	
   225  	
   226  	if(iTestCaseName.Compare(_L("CipherAES_CBC"))==0 || iTestCaseName.Compare(_L("CipherDES_CBC"))==0 || iTestCaseName.Compare(_L("CipherRC2_CBC"))==0)
   227  		{
   228  		TestSSLv3Unpadding(blockSize);
   229  		}
   230  	else
   231  		{
   232  		if (!GetIntFromConfig(ConfigSection(), _L("TextSize"), textSize))
   233    			{
   234       		INFO_PRINTF1(_L("Could not find TextSize in tpadSSLv3.ini"));
   235       		return EFail;
   236    			}
   237    			
   238    		if (!GetStringFromConfig(ConfigSection(), _L("ExpectedResult"), iExpectedResult))
   239  			{
   240  			INFO_PRINTF1(_L("Could not find TextSize in tpadSSLv3.ini"));
   241       		return EFail;
   242  			}
   243  		TestSSLv3CorruptUnpadding(blockSize, textSize);	
   244  		}
   245 
   246 	__UHEAP_MARKEND;
   247 	return TestStepResult();
   248 	}
   249 
   250 void CTestUnpadSSLv3::TestSSLv3Unpadding(TInt aBlockSize)
   251 	{
   252 	CPaddingSSLv3 *padding = CPaddingSSLv3::NewLC(aBlockSize);
   253 
   254 	for (TInt i = 0 ; i <= aBlockSize; i++)
   255 		{
   256 		HBufC8 *padInData = HBufC8::NewLC(i+(aBlockSize - i%aBlockSize));
   257 		HBufC8 *padOutData = HBufC8::NewLC(i);
   258 		HBufC8 *padCompareData = HBufC8::NewLC(i);
   259 		TPtr8 in(padInData->Des());
   260 		TPtr8 out(padOutData->Des());
   261 		TPtr8 comp(padCompareData->Des());
   262 		TInt j;
   263       
   264 		// build up a padded string here
   265 		for (j = 0; j < i; j++)
   266 			{
   267 			TInt text('a'+j%25);
   268 			in.Append(text);
   269 			}
   270 		comp.Append(in);
   271       
   272 		TInt paddingBytes = aBlockSize - i%aBlockSize;
   273 		in.SetLength(in.Length() + paddingBytes);
   274 
   275 		// pad with arbitary data, to test unpadding of SSL 3.0 padded data
   276 		in[in.Length()-1] = (TUint8) (paddingBytes - 1);
   277 		for (j = 2; j <= paddingBytes; j++)
   278 			{
   279 			in[in.Length()-j] = (TUint8) ('a' + j%25);
   280 			}
   281 		TRAPD(err, padding->UnPadL(in, out));
   282 		INFO_PRINTF3(_L("The error returned for input size %d is %d"), i, err);
   283 		TEST(err == KErrNone);
   284 
   285 		// test if the unpadding was correct.
   286 		TEST(out == comp);
   287       
   288 		CleanupStack::PopAndDestroy(3, padInData); // padInData, padOutData, padCompareData
   289 		}
   290 	CleanupStack::PopAndDestroy(padding);
   291 	}
   292 
   293 void CTestUnpadSSLv3::TestSSLv3CorruptUnpadding(TInt aBlockSize, TInt aTextSize)
   294 	{
   295 	CPaddingSSLv3 *padding = CPaddingSSLv3::NewLC(aBlockSize);
   296    
   297 	TInt paddingBytes = 0;
   298 	//If BlockSize is 0, Divide by 0 is undefined
   299 	if(aBlockSize != 0)
   300    		{
   301    		paddingBytes = aBlockSize - aTextSize%aBlockSize;
   302    		}
   303 	HBufC8 *padInData = HBufC8::NewLC(aTextSize+ paddingBytes);
   304 	HBufC8 *padOutData = HBufC8::NewLC(aTextSize);
   305 	HBufC8 *padCompareData = HBufC8::NewLC(aTextSize);
   306 	TPtr8 in(padInData->Des());
   307 	TPtr8 out(padOutData->Des());
   308 	TPtr8 comp(padCompareData->Des());
   309 	TInt j;
   310    
   311 	if(in.Length() < aTextSize)
   312 		{
   313 		for (j = 0; j < in.Length(); j++)
   314    			{
   315 	 		TInt text('a'+j%25);
   316 	 		in.Append(text);
   317       		}
   318 		}
   319 	else
   320 		{
   321 		// build up a padded string here
   322 		for (j = 0; j < aTextSize; j++)
   323    			{
   324 	 		TInt text('a'+j%25);
   325 	 		in.Append(text);
   326       		}
   327 		}
   328 	comp.Append(in);
   329    
   330 	in.SetLength(in.Length() + paddingBytes);
   331 
   332 	if(aBlockSize > 0)
   333   		{
   334    		// pad with arbitary data, to test unpadding of SSL 3.0 padded data
   335    		in[in.Length()-1] = (TUint8) (paddingBytes - 1);
   336    		for (j = 2; j <= paddingBytes; j++)
   337    			{
   338 	 		in[in.Length()-j] = (TUint8) ('a' + j%25);
   339       		}
   340   		 }  
   341 	TRAPD(err, padding->UnPadL(in, out));
   342    
   343     if(iExpectedResult.Compare(_L("CorruptBlockSize")) ==0)
   344  		{
   345  		TEST(err == KErrArgument);
   346  		}
   347 	else if(iExpectedResult.Compare(_L("Valid")) ==0)
   348  		{
   349  		TEST(err == KErrNone);
   350  		}
   351 	else if(iExpectedResult.Compare(_L("InvalidPadding")) == 0)
   352 		{
   353 		TEST(err == KErrInvalidPadding);
   354 		}
   355 	// test if the unpadding was correct.
   356 	TEST(out == comp);
   357 	CleanupStack::PopAndDestroy(3, padInData); // padInData, padOutData, padCompareData
   358 	CleanupStack::PopAndDestroy(padding);
   359 	}