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