os/security/cryptoservices/certificateandkeymgmt/tpkcs7/tpkcs7step.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) 2002-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 "tpkcs7step.h"
sl@0
    20
#include <test/testexecutelog.h>
sl@0
    21
#include "pkcs7signedobject.h"
sl@0
    22
#include <pkcs7contentinfo_v2.h>
sl@0
    23
#include <pkcs7digestinfo.h>
sl@0
    24
#include <pkcs7encrypteddataobject.h>
sl@0
    25
#include <pkixcertchain.h>
sl@0
    26
#include <pbe.h>
sl@0
    27
#include <pbedata.h>
sl@0
    28
#include <asn1dec.h>
sl@0
    29
sl@0
    30
#include "validate.h"
sl@0
    31
sl@0
    32
CTPKCS7CorruptTest::CTPKCS7CorruptTest()
sl@0
    33
	{
sl@0
    34
	SetTestStepName(KTPKCS7CorruptTest);
sl@0
    35
	}
sl@0
    36
sl@0
    37
CTPKCS7ValidTest::CTPKCS7ValidTest()
sl@0
    38
	{
sl@0
    39
	SetTestStepName(KTPKCS7ValidTest);
sl@0
    40
	}
sl@0
    41
sl@0
    42
CTPKCS7ContentTest::CTPKCS7ContentTest()
sl@0
    43
	{
sl@0
    44
	SetTestStepName(KTPKCS7ContentTest);
sl@0
    45
	}
sl@0
    46
sl@0
    47
CTPKCS7CertificateTest::CTPKCS7CertificateTest()
sl@0
    48
	{
sl@0
    49
	SetTestStepName(KTPKCS7CertificateTest);
sl@0
    50
	}
sl@0
    51
sl@0
    52
CTPKCS7SignerTest::CTPKCS7SignerTest()
sl@0
    53
	{
sl@0
    54
	SetTestStepName(KTPKCS7SignerTest);
sl@0
    55
	}
sl@0
    56
CTPKCS7EncryptedDataTest::CTPKCS7EncryptedDataTest()
sl@0
    57
	{
sl@0
    58
	SetTestStepName(KTPKCS7EncryptedDataTest);
sl@0
    59
	}
sl@0
    60
sl@0
    61
CTPKCS7DigestInfoTest::CTPKCS7DigestInfoTest()
sl@0
    62
	{
sl@0
    63
	SetTestStepName(KTPKCS7DigestInfoTest);
sl@0
    64
	}
sl@0
    65
	
sl@0
    66
CTPKCS7EncryptedDataCorruptTest::CTPKCS7EncryptedDataCorruptTest()
sl@0
    67
	{
sl@0
    68
	SetTestStepName(KTPKCS7EncryptedDataCorruptTest);
sl@0
    69
	}
sl@0
    70
	
sl@0
    71
CTPKCS7DigestInfoCorruptTest::CTPKCS7DigestInfoCorruptTest()
sl@0
    72
	{
sl@0
    73
	SetTestStepName(KTPKCS7DigestInfoCorruptTest);
sl@0
    74
	}
sl@0
    75
sl@0
    76
CTPKCS7OOMTest::CTPKCS7OOMTest()
sl@0
    77
	{
sl@0
    78
	SetTestStepName(KTPKCS7OOMTest);
sl@0
    79
	}
sl@0
    80
sl@0
    81
TVerdict CTPKCS7OOMTest::doTestStepL()
sl@0
    82
	{
sl@0
    83
	TVerdict verdict = EFail;
sl@0
    84
 	TInt countAfter = 0;
sl@0
    85
	TInt countBefore = 0;
sl@0
    86
 	for (TInt oomCount = 0; ; oomCount++)
sl@0
    87
 		{
sl@0
    88
 		__UHEAP_RESET;
sl@0
    89
 		__UHEAP_SETFAIL(RHeap::EDeterministic, oomCount);
sl@0
    90
 		countBefore = User::CountAllocCells();
sl@0
    91
 		TRAPD(error, doTestOOML());
sl@0
    92
 		countAfter = User::CountAllocCells();
sl@0
    93
 		__UHEAP_RESET;
sl@0
    94
 		if (error != KErrNoMemory)
sl@0
    95
 			{
sl@0
    96
 			verdict = EPass;
sl@0
    97
 			INFO_PRINTF2(_L("OOM Status %d"),error);
sl@0
    98
			INFO_PRINTF1(_L("Test outcome : Passed"));
sl@0
    99
 			break;
sl@0
   100
 			}
sl@0
   101
 		else
sl@0
   102
 			{
sl@0
   103
 			if (countBefore != countAfter)
sl@0
   104
 				{
sl@0
   105
 				INFO_PRINTF2(_L("OOM Status %d"),error);
sl@0
   106
 				INFO_PRINTF2(_L("OOM Failed at %d"), oomCount);
sl@0
   107
 				verdict = EFail;
sl@0
   108
 				break;
sl@0
   109
 				}
sl@0
   110
 			}
sl@0
   111
 		INFO_PRINTF2(_L("OOM Failed Point status %d"), error);
sl@0
   112
		}
sl@0
   113
	INFO_PRINTF3(_L("Heap alloc count ok: %d final vs %d initial"), countAfter,countBefore);
sl@0
   114
 	SetTestStepResult(verdict);
sl@0
   115
 	return verdict;
sl@0
   116
	}
sl@0
   117
sl@0
   118
void CTPKCS7OOMTest::doTestOOML()
sl@0
   119
	{
sl@0
   120
	TInt OOMTest;
sl@0
   121
	GetIntFromConfig(ConfigSection(),_L("OOMTestNo"),OOMTest);
sl@0
   122
	__UHEAP_MARK;
sl@0
   123
			
sl@0
   124
	if (OOMTest == 1)
sl@0
   125
		{
sl@0
   126
		CPKCS7ContentInfo* theTest = NULL;
sl@0
   127
		theTest = CPKCS7ContentInfo::NewL(iRawData->Des());
sl@0
   128
		delete theTest;
sl@0
   129
		}
sl@0
   130
	// Digest Info
sl@0
   131
	else if (OOMTest == 2)
sl@0
   132
		{
sl@0
   133
		CPKCS7DigestInfo* theTest0 = NULL;
sl@0
   134
		theTest0 = CPKCS7DigestInfo::NewL(iRawData->Des());
sl@0
   135
		delete theTest0;
sl@0
   136
		}
sl@0
   137
	// Encrypted Data
sl@0
   138
	else if (OOMTest == 3)
sl@0
   139
		{
sl@0
   140
		CPKCS7ContentInfo* theTest1 = NULL;
sl@0
   141
		theTest1 = CPKCS7ContentInfo::NewL(iRawData->Des());
sl@0
   142
			
sl@0
   143
		CleanupStack::PushL(theTest1);
sl@0
   144
		CPKCS7EncryptedDataObject* theTest2 = NULL;
sl@0
   145
		theTest2 = CPKCS7EncryptedDataObject::NewL(*theTest1);
sl@0
   146
				
sl@0
   147
		CleanupStack::PopAndDestroy(theTest1);
sl@0
   148
		delete theTest2;
sl@0
   149
		}
sl@0
   150
	
sl@0
   151
	__UHEAP_MARKEND;
sl@0
   152
}
sl@0
   153
sl@0
   154
//	Step 1	Corrupt
sl@0
   155
TVerdict CTPKCS7CorruptTest::doTestStepL()
sl@0
   156
	{
sl@0
   157
	if (TestStepResult() != EPass)
sl@0
   158
		{
sl@0
   159
		return TestStepResult();
sl@0
   160
		}
sl@0
   161
sl@0
   162
	TInt reason;
sl@0
   163
	if (GetIntFromConfig(ConfigSection(),_L("Reason"),reason) == EFalse)
sl@0
   164
		{
sl@0
   165
		reason = KErrNone;
sl@0
   166
		}
sl@0
   167
	
sl@0
   168
	TInt err;
sl@0
   169
	
sl@0
   170
    //Use CPKCS7ContentInfo class in order to obtain the ContentType and ContentData.
sl@0
   171
    //Depending on the ContentType the corresponding Object of that ContentType is created.
sl@0
   172
	CPKCS7ContentInfo * contentInfo = NULL;
sl@0
   173
	TRAP (err, contentInfo = CPKCS7ContentInfo::NewL(iRawData->Des()));
sl@0
   174
sl@0
   175
	//Checks if the file is empty, Corrupt
sl@0
   176
	if (err == reason)
sl@0
   177
		{
sl@0
   178
		delete contentInfo;
sl@0
   179
		SetTestStepResult(EPass);
sl@0
   180
		INFO_PRINTF3(_L("Expected result %d, got %d"), reason, err);
sl@0
   181
		return TestStepResult();
sl@0
   182
		}
sl@0
   183
	
sl@0
   184
	if( err == KErrNone)
sl@0
   185
		{
sl@0
   186
		//ContentType is SignedData
sl@0
   187
		if( contentInfo->ContentType() == KPkcs7SignedData)
sl@0
   188
			{
sl@0
   189
			CPKCS7SignedObject * p7 = NULL;
sl@0
   190
			TRAP (err, p7 = CPKCS7SignedObject::NewL(*contentInfo));
sl@0
   191
			delete p7;
sl@0
   192
			}
sl@0
   193
		}
sl@0
   194
	delete contentInfo;
sl@0
   195
sl@0
   196
	if (err != reason)
sl@0
   197
		{
sl@0
   198
		SetTestStepResult(EFail);
sl@0
   199
		INFO_PRINTF3(_L("Expected result %d, got %d"), reason, err);
sl@0
   200
		}
sl@0
   201
	//Not corrupt , repeat files
sl@0
   202
	return TestStepResult();
sl@0
   203
	}
sl@0
   204
	
sl@0
   205
	
sl@0
   206
TVerdict CTPKCS7ValidTest::doTestStepL()
sl@0
   207
	{
sl@0
   208
	if (TestStepResult() != EPass)
sl@0
   209
		{
sl@0
   210
		return TestStepResult();
sl@0
   211
		}
sl@0
   212
sl@0
   213
	__UHEAP_MARK;
sl@0
   214
	TBool expectedValid;
sl@0
   215
	if (GetBoolFromConfig(ConfigSection(),_L("IsValid"), expectedValid) == EFalse)
sl@0
   216
		{
sl@0
   217
		expectedValid = ETrue;
sl@0
   218
		}
sl@0
   219
	
sl@0
   220
	TInt err;
sl@0
   221
sl@0
   222
	CPKCS7ContentInfo * contentInfo = NULL;
sl@0
   223
	TRAP (err, contentInfo = CPKCS7ContentInfo::NewL(iRawData->Des()));
sl@0
   224
	
sl@0
   225
	if(err == KErrNone)
sl@0
   226
		{
sl@0
   227
		CPKCS7SignedObject * p7 = NULL;
sl@0
   228
		if( contentInfo->ContentType() == KPkcs7SignedData)
sl@0
   229
			{
sl@0
   230
			TRAP (err, p7 = CPKCS7SignedObject::NewL(*contentInfo));
sl@0
   231
			//expired, and the case where certificate chain root is not on the device	
sl@0
   232
			if (!expectedValid)
sl@0
   233
				{
sl@0
   234
				if (err != KErrNone)
sl@0
   235
					{
sl@0
   236
					SetTestStepResult(EPass);
sl@0
   237
					INFO_PRINTF2(_L("Got %d building PKCS7 object"), err);
sl@0
   238
					return TestStepResult();
sl@0
   239
					}
sl@0
   240
				}
sl@0
   241
sl@0
   242
			if (err != KErrNone)
sl@0
   243
				{
sl@0
   244
				SetTestStepResult(EFail);
sl@0
   245
				INFO_PRINTF2(_L("Got %d building PKCS7 object"), err);
sl@0
   246
				}
sl@0
   247
			else
sl@0
   248
				{
sl@0
   249
				CleanupStack::PushL (p7);
sl@0
   250
		
sl@0
   251
				const RPointerArray<CPKCS7SignerInfo>& signers = p7->SignerInfo();
sl@0
   252
				TBool isValid = EFalse;
sl@0
   253
				HBufC8* certificateEncoding = NULL;
sl@0
   254
				if(!p7->ValidateSignerL(*signers[0], certificateEncoding))
sl@0
   255
					{
sl@0
   256
					INFO_PRINTF1(_L("Couldn't validate signer"));
sl@0
   257
					}
sl@0
   258
				else
sl@0
   259
					{
sl@0
   260
					CActiveScheduler* sched = NULL;
sl@0
   261
					if (CActiveScheduler::Current() == NULL)
sl@0
   262
						{
sl@0
   263
						INFO_PRINTF1(_L("Installing scheduler"));
sl@0
   264
						sched = new (ELeave) CActiveScheduler();
sl@0
   265
						CleanupStack::PushL (sched);
sl@0
   266
						__UHEAP_MARK;
sl@0
   267
						CActiveScheduler::Install (sched);
sl@0
   268
						}
sl@0
   269
					RPointerArray<CX509Certificate> roots (&iRootCertificate, 1);
sl@0
   270
					CPKIXCertChain * chain = CPKIXCertChain::NewLC(iFs, *certificateEncoding, roots);
sl@0
   271
					TTime tm;
sl@0
   272
					_LIT(KDateCorrect1,"20040801:"); 
sl@0
   273
					TBuf <24> theDate(KDateCorrect1); 
sl@0
   274
					TInt err=tm.Set(theDate); 
sl@0
   275
					if(err)
sl@0
   276
						{
sl@0
   277
						tm.HomeTime();
sl@0
   278
						}
sl@0
   279
					CPKIXValidationResult* result = CPKIXValidationResult::NewLC();
sl@0
   280
					CTPKCS7Validator* validator = new (ELeave) CTPKCS7Validator (chain, result, &tm);
sl@0
   281
					CleanupStack::PushL (validator);
sl@0
   282
					validator->doValidate ();
sl@0
   283
					sched->Start ();
sl@0
   284
					if (result->Error().iReason == EValidatedOK)
sl@0
   285
						{
sl@0
   286
						isValid = ETrue;
sl@0
   287
						INFO_PRINTF1(_L("Validation success"));
sl@0
   288
						}
sl@0
   289
					else
sl@0
   290
						{
sl@0
   291
						INFO_PRINTF2(_L("Validation failed: %d"), result->Error().iReason);
sl@0
   292
						}
sl@0
   293
            				
sl@0
   294
					CleanupStack::PopAndDestroy(validator);
sl@0
   295
					CleanupStack::PopAndDestroy(result);
sl@0
   296
					CleanupStack::PopAndDestroy(chain);
sl@0
   297
					if (sched)
sl@0
   298
						{
sl@0
   299
						CActiveScheduler::Install (NULL);
sl@0
   300
						CleanupStack::PopAndDestroy (sched);
sl@0
   301
						}
sl@0
   302
					}
sl@0
   303
				if (certificateEncoding)
sl@0
   304
					{
sl@0
   305
					CleanupStack::PopAndDestroy(certificateEncoding);
sl@0
   306
					}
sl@0
   307
				CleanupStack::PopAndDestroy (p7);
sl@0
   308
				if (isValid != expectedValid)
sl@0
   309
					{
sl@0
   310
					SetTestStepResult(EFail);
sl@0
   311
					if (expectedValid)
sl@0
   312
						{
sl@0
   313
						INFO_PRINTF1(_L("Expected valid, got invalid"));
sl@0
   314
						}
sl@0
   315
					else
sl@0
   316
						{
sl@0
   317
						INFO_PRINTF1(_L("Expected invalid, got valid"));
sl@0
   318
						}
sl@0
   319
					}
sl@0
   320
				}
sl@0
   321
sl@0
   322
			}
sl@0
   323
		}
sl@0
   324
	else
sl@0
   325
		{
sl@0
   326
		SetTestStepResult(EFail);
sl@0
   327
		}
sl@0
   328
sl@0
   329
	
sl@0
   330
	__UHEAP_MARKEND;
sl@0
   331
	return TestStepResult();
sl@0
   332
	}
sl@0
   333
	
sl@0
   334
	
sl@0
   335
//	Step 3	Content
sl@0
   336
TVerdict CTPKCS7ContentTest::doTestStepL()
sl@0
   337
	{
sl@0
   338
	if (TestStepResult() != EPass)
sl@0
   339
		{
sl@0
   340
		return TestStepResult();
sl@0
   341
		}
sl@0
   342
sl@0
   343
	TBool checkType;
sl@0
   344
	TBool checkLength;
sl@0
   345
	TBool checkContent;
sl@0
   346
	TInt expectedType;
sl@0
   347
	TInt expectedLength;
sl@0
   348
	TPtrC expectedString;
sl@0
   349
	HBufC8 * expectedContent = NULL;
sl@0
   350
sl@0
   351
	checkType    = GetIntFromConfig   (ConfigSection(),_L("ContentType"),   expectedType);
sl@0
   352
	checkLength  = GetIntFromConfig   (ConfigSection(),_L("ContentLength"), expectedLength);
sl@0
   353
	checkContent = GetStringFromConfig(ConfigSection(),_L("ContentString"), expectedString);
sl@0
   354
sl@0
   355
	if (checkContent)
sl@0
   356
		{
sl@0
   357
		if (expectedString.Length() & 1)
sl@0
   358
			{
sl@0
   359
			SetTestStepResult(ETestSuiteError);
sl@0
   360
			INFO_PRINTF1(_L("ContentString is not a multiple of 2 long"));
sl@0
   361
			checkContent = EFalse;
sl@0
   362
			}
sl@0
   363
		else
sl@0
   364
			{
sl@0
   365
			// workaround to parse the expectedstring 
sl@0
   366
			expectedContent = HBufC8::NewLC (expectedString.Length()/2);
sl@0
   367
			TPtr8 des = expectedContent->Des ();
sl@0
   368
			for (TInt i = 0; i < expectedString.Length()/2; i++)
sl@0
   369
				{
sl@0
   370
				TInt top = expectedString[2*i];
sl@0
   371
				TInt low = expectedString[2*i+1];
sl@0
   372
				if (top >= 0x61) top -= (0x61-10);
sl@0
   373
				else top -= 0x30;
sl@0
   374
				if (low >= 0x61) low -= (0x61-10);
sl@0
   375
				else low -= 0x30;
sl@0
   376
				des.Append (top*16+low);
sl@0
   377
				}
sl@0
   378
			}
sl@0
   379
		}
sl@0
   380
	TInt err;
sl@0
   381
sl@0
   382
    CPKCS7ContentInfo * contentInfo = NULL;
sl@0
   383
	TRAP (err, contentInfo = CPKCS7ContentInfo::NewL(iRawData->Des()));
sl@0
   384
    if(err == KErrNone)
sl@0
   385
		{
sl@0
   386
		CPKCS7SignedObject * p7 = NULL;
sl@0
   387
		if( contentInfo->ContentType() == 2)
sl@0
   388
			{
sl@0
   389
			TRAPD (err, p7 = CPKCS7SignedObject::NewL(*contentInfo));
sl@0
   390
sl@0
   391
			if (err != KErrNone)
sl@0
   392
				{
sl@0
   393
				SetTestStepResult(EFail);
sl@0
   394
				INFO_PRINTF2(_L("Got %d building PKCS7 object"), err);
sl@0
   395
				}
sl@0
   396
			else
sl@0
   397
				{
sl@0
   398
				CleanupStack::PushL (p7);
sl@0
   399
				const CPKCS7ContentInfo& p7info = p7->ContentInfo ();
sl@0
   400
				if (checkType)
sl@0
   401
					{
sl@0
   402
					if (p7info.ContentType() != expectedType)
sl@0
   403
						{
sl@0
   404
						SetTestStepResult(EFail);
sl@0
   405
						INFO_PRINTF3(_L("Expected ContentType %d, got %d"), expectedType, p7info.ContentType());
sl@0
   406
						}
sl@0
   407
					}
sl@0
   408
				const TDesC8& content = p7info.ContentData();
sl@0
   409
		
sl@0
   410
				if (checkLength)
sl@0
   411
					{
sl@0
   412
					if (content.Length() != expectedLength)
sl@0
   413
						{
sl@0
   414
						SetTestStepResult(EFail);
sl@0
   415
						INFO_PRINTF3(_L("Expected ContentLength %d, got %d"), expectedLength, content.Length());
sl@0
   416
						}
sl@0
   417
					}
sl@0
   418
				if (checkContent)
sl@0
   419
					{
sl@0
   420
					if (content.Length() != expectedContent->Length())
sl@0
   421
						{
sl@0
   422
						SetTestStepResult(EFail);
sl@0
   423
						INFO_PRINTF3(_L("Expected ContentString length %d does not correspond to PKCS7 data length %d"),
sl@0
   424
						expectedContent->Length(), content.Length());
sl@0
   425
						}
sl@0
   426
					else
sl@0
   427
						{
sl@0
   428
						if (expectedContent->Compare(content) != 0)
sl@0
   429
							{
sl@0
   430
							SetTestStepResult(EFail);
sl@0
   431
							INFO_PRINTF1(_L("Expected ContentString does not match PKCS7 content"));
sl@0
   432
							}
sl@0
   433
						}
sl@0
   434
					}
sl@0
   435
				CleanupStack::PopAndDestroy (p7);
sl@0
   436
				}
sl@0
   437
sl@0
   438
			}
sl@0
   439
		}
sl@0
   440
	else
sl@0
   441
		{
sl@0
   442
		SetTestStepResult(EFail);
sl@0
   443
		}
sl@0
   444
sl@0
   445
		if (expectedContent)
sl@0
   446
			{
sl@0
   447
			CleanupStack::PopAndDestroy (expectedContent);
sl@0
   448
			}
sl@0
   449
sl@0
   450
		if (!checkType && !checkLength && !checkContent && (TestStepResult()==EPass))
sl@0
   451
			{
sl@0
   452
			SetTestStepResult(EInconclusive);
sl@0
   453
			INFO_PRINTF1(_L("No test performed in this step"));
sl@0
   454
			}
sl@0
   455
sl@0
   456
	return TestStepResult();
sl@0
   457
	}
sl@0
   458
sl@0
   459
//	Step 4	Certificate
sl@0
   460
TVerdict CTPKCS7CertificateTest::doTestStepL()
sl@0
   461
	{
sl@0
   462
	if (TestStepResult() != EPass)
sl@0
   463
		{
sl@0
   464
		return TestStepResult();
sl@0
   465
		}
sl@0
   466
sl@0
   467
	TBool checkCount;
sl@0
   468
	TInt expectedCount;
sl@0
   469
sl@0
   470
	checkCount = GetIntFromConfig (ConfigSection(),_L("CertificateCount"), expectedCount);
sl@0
   471
	
sl@0
   472
	TInt err;
sl@0
   473
	
sl@0
   474
sl@0
   475
	CPKCS7ContentInfo * contentInfo = NULL;
sl@0
   476
	TRAP (err, contentInfo = CPKCS7ContentInfo::NewL(iRawData->Des()));
sl@0
   477
    
sl@0
   478
	if(err == KErrNone)
sl@0
   479
		{
sl@0
   480
		CPKCS7SignedObject * p7 = NULL;
sl@0
   481
		if( contentInfo->ContentType() == 2)
sl@0
   482
			{
sl@0
   483
			TRAP (err, p7 = CPKCS7SignedObject::NewL(*contentInfo));
sl@0
   484
			
sl@0
   485
			if (err != KErrNone)
sl@0
   486
				{
sl@0
   487
				SetTestStepResult(EFail);
sl@0
   488
				INFO_PRINTF2(_L("Got %d building PKCS7 object"), err);
sl@0
   489
				}
sl@0
   490
			else
sl@0
   491
				{
sl@0
   492
				CleanupStack::PushL (p7);
sl@0
   493
				const RPointerArray<CPKCS7ExtendedCertificateOrCertificate>& certificates = p7->Certificates();
sl@0
   494
				if (checkCount)
sl@0
   495
					{
sl@0
   496
					if (certificates.Count() != expectedCount)
sl@0
   497
						{
sl@0
   498
						SetTestStepResult(EFail);
sl@0
   499
						INFO_PRINTF3(_L("Expected %d certificates, got %d"), expectedCount, certificates.Count());
sl@0
   500
						}
sl@0
   501
					}
sl@0
   502
				CleanupStack::PopAndDestroy (p7);
sl@0
   503
				}
sl@0
   504
sl@0
   505
			}
sl@0
   506
		}
sl@0
   507
	else
sl@0
   508
		{
sl@0
   509
		SetTestStepResult(EFail);
sl@0
   510
		}
sl@0
   511
		
sl@0
   512
	if (!checkCount && (TestStepResult()==EPass))
sl@0
   513
		{
sl@0
   514
		SetTestStepResult(EInconclusive);
sl@0
   515
		INFO_PRINTF1(_L("No test performed in this step"));
sl@0
   516
		}
sl@0
   517
sl@0
   518
	return TestStepResult();
sl@0
   519
	}
sl@0
   520
sl@0
   521
//	Step 5	Signer
sl@0
   522
TVerdict CTPKCS7SignerTest::doTestStepL()
sl@0
   523
	{
sl@0
   524
	if (TestStepResult() != EPass)
sl@0
   525
		{
sl@0
   526
		return TestStepResult();
sl@0
   527
		}
sl@0
   528
sl@0
   529
	TBool checkCount;
sl@0
   530
	TInt expectedCount;
sl@0
   531
sl@0
   532
	checkCount = GetIntFromConfig (ConfigSection(),_L("SignerCount"), expectedCount);
sl@0
   533
sl@0
   534
	TInt err;
sl@0
   535
sl@0
   536
	CPKCS7ContentInfo * contentInfo = NULL;
sl@0
   537
	TRAP (err, contentInfo = CPKCS7ContentInfo::NewL(iRawData->Des()));
sl@0
   538
    if(err == KErrNone)
sl@0
   539
		{
sl@0
   540
		CPKCS7SignedObject * p7 = NULL;
sl@0
   541
		if( contentInfo->ContentType() == 2)
sl@0
   542
			{
sl@0
   543
			TRAPD (err, p7 = CPKCS7SignedObject::NewL(*contentInfo));
sl@0
   544
sl@0
   545
			if (err != KErrNone)
sl@0
   546
				{
sl@0
   547
				SetTestStepResult(EFail);
sl@0
   548
				INFO_PRINTF2(_L("Got %d building PKCS7 object"), err);
sl@0
   549
				}
sl@0
   550
			else
sl@0
   551
				{
sl@0
   552
				CleanupStack::PushL (p7);
sl@0
   553
				const RPointerArray<CPKCS7SignerInfo>& signers = p7->SignerInfo();
sl@0
   554
				if (checkCount)
sl@0
   555
					{
sl@0
   556
					if (signers.Count() != expectedCount)
sl@0
   557
						{
sl@0
   558
						SetTestStepResult(EFail);
sl@0
   559
						INFO_PRINTF3(_L("Expected %d signers, got %d"), expectedCount, signers.Count());
sl@0
   560
						}
sl@0
   561
					}
sl@0
   562
				CleanupStack::PopAndDestroy (p7);
sl@0
   563
				}
sl@0
   564
sl@0
   565
			}
sl@0
   566
		}
sl@0
   567
sl@0
   568
	
sl@0
   569
	if (!checkCount && (TestStepResult()==EPass))
sl@0
   570
		{
sl@0
   571
		SetTestStepResult(EInconclusive);
sl@0
   572
		INFO_PRINTF1(_L("No test performed in this step"));
sl@0
   573
		}
sl@0
   574
sl@0
   575
	return TestStepResult();
sl@0
   576
	}
sl@0
   577
sl@0
   578
	
sl@0
   579
	
sl@0
   580
//Encrypted Data
sl@0
   581
TVerdict CTPKCS7EncryptedDataTest::doTestStepL()
sl@0
   582
	{
sl@0
   583
sl@0
   584
	if (TestStepResult() != EPass)
sl@0
   585
		{
sl@0
   586
		return TestStepResult();
sl@0
   587
		}
sl@0
   588
		
sl@0
   589
	TInt reason;
sl@0
   590
	// Read from INI file.
sl@0
   591
	GetIntFromConfig(ConfigSection(),_L("Reason"),reason);
sl@0
   592
sl@0
   593
	//Is Valid	
sl@0
   594
  	TBool expectedValid;
sl@0
   595
  	TBool checkValid;
sl@0
   596
      
sl@0
   597
  	checkValid = GetBoolFromConfig(ConfigSection(),_L("IsValid"), expectedValid);
sl@0
   598
 	
sl@0
   599
	 __UHEAP_MARK;
sl@0
   600
    CPKCS7ContentInfo *contentInfo = NULL;
sl@0
   601
	TRAPD (err, contentInfo = CPKCS7ContentInfo::NewL(iRawData->Des()));
sl@0
   602
	CleanupStack::PushL(contentInfo);
sl@0
   603
    if(err == KErrNone)
sl@0
   604
		{
sl@0
   605
	     
sl@0
   606
		CPKCS7EncryptedDataObject* p7 = NULL;
sl@0
   607
		TInt type = contentInfo->ContentType();
sl@0
   608
		if( contentInfo->ContentType() == CPKCS7ContentInfo::EContentTypeEncryptedData)
sl@0
   609
			{
sl@0
   610
			TRAPD (err, p7 = CPKCS7EncryptedDataObject::NewL(*contentInfo));
sl@0
   611
		    CleanupStack::PushL(p7);
sl@0
   612
		    __UHEAP_MARK;
sl@0
   613
sl@0
   614
			if (checkValid)
sl@0
   615
  				{
sl@0
   616
  				if(!expectedValid && err == reason)
sl@0
   617
  		 			{ 
sl@0
   618
					CleanupStack::PopAndDestroy(2,contentInfo);//p7, contentInfo
sl@0
   619
  		 			return TestStepResult();
sl@0
   620
  		 			}
sl@0
   621
  				if(!expectedValid && contentInfo->ContentType() == 1)	
sl@0
   622
           			{
sl@0
   623
           			CleanupStack::PopAndDestroy(2,contentInfo);//p7, contentInfo
sl@0
   624
           		 	return TestStepResult();
sl@0
   625
           			}
sl@0
   626
  				}
sl@0
   627
sl@0
   628
			if (err != KErrNone)
sl@0
   629
				{
sl@0
   630
				SetTestStepResult(EFail);
sl@0
   631
				ERR_PRINTF2(_L("Got %d building PKCS7 object"), err);
sl@0
   632
				}
sl@0
   633
    	
sl@0
   634
			//CONTENT TYPE
sl@0
   635
			CPKCS7EncryptedDataObject::TContentType contentType = p7->ContentType();
sl@0
   636
			if(contentType != CPKCS7EncryptedDataObject::EPkcs7Data)
sl@0
   637
    			{
sl@0
   638
     			SetTestStepResult(EFail);
sl@0
   639
     			ERR_PRINTF1(_L("ContentType does not match"));
sl@0
   640
     			CleanupStack::PopAndDestroy(3,contentInfo);//expectedContentType, p7, contentInfo
sl@0
   641
     			return TestStepResult();	
sl@0
   642
     			}
sl@0
   643
			 else
sl@0
   644
     			{
sl@0
   645
     			INFO_PRINTF1(_L("ContentType matches"));	
sl@0
   646
     			}
sl@0
   647
	  
sl@0
   648
			//ENCRYPTEDCONTENT DATA
sl@0
   649
			const TDesC8& contentData = p7->EncryptedContentInfoData();
sl@0
   650
			if(contentData.Compare(*iContentData) != 0)
sl@0
   651
				{
sl@0
   652
	   			SetTestStepResult(EFail);
sl@0
   653
	   			ERR_PRINTF1(_L("EncryptedContentData does not match"));
sl@0
   654
	   			CleanupStack::PopAndDestroy(2,contentInfo);	//p7, contentInfo
sl@0
   655
	   			return TestStepResult();
sl@0
   656
	  			}
sl@0
   657
			else
sl@0
   658
	  			{
sl@0
   659
	  			INFO_PRINTF1(_L("EncryptedContentData matches")); 	
sl@0
   660
	  			}
sl@0
   661
		    
sl@0
   662
	  
sl@0
   663
			//SALT
sl@0
   664
			const CPBEncryptParms& encryptParams = p7->EncryptParams();
sl@0
   665
			if((encryptParams.Salt()).Compare(*iSaltValue) != 0)
sl@0
   666
				{
sl@0
   667
	   			SetTestStepResult(EFail);
sl@0
   668
	   			ERR_PRINTF1(_L("Salt Value does not match"));	
sl@0
   669
	   			CleanupStack::PopAndDestroy(2, contentInfo);//p7,contentInfo
sl@0
   670
	   			return TestStepResult();
sl@0
   671
	  			}
sl@0
   672
			else
sl@0
   673
				{
sl@0
   674
				INFO_PRINTF1(_L("Salt Value matches"));	
sl@0
   675
				}  	
sl@0
   676
	        
sl@0
   677
	
sl@0
   678
			//ITERATION COUNT
sl@0
   679
			TBool checkIteration;
sl@0
   680
			TInt expectedIterationCount;
sl@0
   681
	
sl@0
   682
			checkIteration = GetIntFromConfig(ConfigSection(),_L("Iteration"), expectedIterationCount);
sl@0
   683
			if(checkIteration)
sl@0
   684
				{
sl@0
   685
	   			if(encryptParams.Iterations() != expectedIterationCount)
sl@0
   686
	   				{
sl@0
   687
	   				SetTestStepResult(EFail);
sl@0
   688
	   				ERR_PRINTF1(_L("Iterations does not match"));
sl@0
   689
	   				CleanupStack::PopAndDestroy(2, contentInfo);//p7, contentInfo
sl@0
   690
	   				return TestStepResult();	
sl@0
   691
	   				}
sl@0
   692
	   			else
sl@0
   693
	   				{
sl@0
   694
	   				INFO_PRINTF1(_L("Iteration matches")); 
sl@0
   695
	   				}
sl@0
   696
	  			}		
sl@0
   697
	
sl@0
   698
			//CIPHER
sl@0
   699
			TBool checkCipher;
sl@0
   700
			TPtrC expectedCipher;
sl@0
   701
			TPBECipher cipherValue = (TPBECipher) -1;//assign to impossible value
sl@0
   702
	
sl@0
   703
			checkCipher = GetStringFromConfig(ConfigSection(),_L("Algorithm"),  expectedCipher);
sl@0
   704
		
sl@0
   705
			if(checkCipher)
sl@0
   706
				{
sl@0
   707
				if(expectedCipher.Compare(_L("pbeWithSHA1And40BitRC2_CBC")) == 0)
sl@0
   708
	   	 			{
sl@0
   709
	   	  			cipherValue = ECipherRC2_CBC_40_5;
sl@0
   710
	     			}
sl@0
   711
				else if(expectedCipher.Compare(_L("pbeWithSHA1And128BitRC4")) == 0)
sl@0
   712
	   	 			{
sl@0
   713
	   	  			cipherValue = ECipherARC4_128;
sl@0
   714
	     			}
sl@0
   715
	            else if(expectedCipher.Compare(_L("pbeWithSHA1AndDES-CBC")) == 0)
sl@0
   716
	   	 			{
sl@0
   717
	   	  			cipherValue = ECipherDES_CBC;
sl@0
   718
	     			}
sl@0
   719
	     		else if(expectedCipher.Compare(_L("pbeWithSHAAnd3_KeyTripleDES_CBC")) == 0)
sl@0
   720
	   	 			{
sl@0
   721
	   	  			cipherValue = ECipher3DES_CBC;
sl@0
   722
	     			}
sl@0
   723
	     		else if(expectedCipher.Compare(_L("pbeWithSHAAnd2-KeyTripleDES-CBC"))==0)
sl@0
   724
	     			{
sl@0
   725
	     			cipherValue = ECipher2Key3DES_CBC;	
sl@0
   726
	     			}
sl@0
   727
	     		else if(expectedCipher.Compare(_L("pbeWithSHA1And40BitRC4")) == 0)
sl@0
   728
	   	 			{
sl@0
   729
	   	  			cipherValue = ECipherARC4_40;
sl@0
   730
	     			}
sl@0
   731
	     		else if(expectedCipher.Compare(_L("pbewithSHAAnd128BitRC2CBC")) == 0)
sl@0
   732
	   	 			{
sl@0
   733
	   	  			cipherValue = ECipherRC2_CBC_128_16;
sl@0
   734
	     			}
sl@0
   735
	     		else if(expectedCipher.Compare(_L("pbeWithSHA1AndRC2_CBC")) == 0)
sl@0
   736
	     			{
sl@0
   737
	     			cipherValue = ECipherRC2_CBC_40;
sl@0
   738
	     			}
sl@0
   739
	     					
sl@0
   740
	   			if(encryptParams.Cipher() != cipherValue)
sl@0
   741
	     			{
sl@0
   742
	     			SetTestStepResult(EFail);
sl@0
   743
	     			ERR_PRINTF1(_L("Cipher does not match"));
sl@0
   744
	     			CleanupStack::PopAndDestroy(3, contentInfo);//expectedCipher,p7,contentInfo
sl@0
   745
	     			return TestStepResult();	
sl@0
   746
	     			} 
sl@0
   747
				else 
sl@0
   748
	    			{
sl@0
   749
	   				INFO_PRINTF1(_L("Cipher matches")); 
sl@0
   750
	    			}
sl@0
   751
				}
sl@0
   752
					
sl@0
   753
			if(contentData.Length() != 0)
sl@0
   754
				{
sl@0
   755
				//Decryption
sl@0
   756
				TBool checkPassword;
sl@0
   757
				TPtrC password;
sl@0
   758
				
sl@0
   759
				checkPassword = GetStringFromConfig(ConfigSection(),_L("Password"), password);
sl@0
   760
					
sl@0
   761
				if(checkPassword)
sl@0
   762
					{
sl@0
   763
			    	HBufC8* plainText = p7->DecryptDataL(password);
sl@0
   764
			    	delete plainText;
sl@0
   765
			    	SetTestStepResult(EPass);
sl@0
   766
	  				} 
sl@0
   767
				}
sl@0
   768
	  		CleanupStack::PopAndDestroy(p7);
sl@0
   769
            }
sl@0
   770
        //If ContentType is not Encrypted Data
sl@0
   771
        else
sl@0
   772
        	{
sl@0
   773
        		
sl@0
   774
        	if(checkValid)
sl@0
   775
          		{
sl@0
   776
          		if(!expectedValid && err == reason)
sl@0
   777
 		 			{
sl@0
   778
  		 			CleanupStack::PopAndDestroy(contentInfo);
sl@0
   779
  		 			return TestStepResult();
sl@0
   780
  		 			}
sl@0
   781
          		else
sl@0
   782
          			{
sl@0
   783
          			SetTestStepResult(EFail);	
sl@0
   784
          			}
sl@0
   785
          		}
sl@0
   786
sl@0
   787
       		}
sl@0
   788
		}
sl@0
   789
	else if(err != reason)
sl@0
   790
		{
sl@0
   791
		SetTestStepResult(EFail);
sl@0
   792
		}
sl@0
   793
	CleanupStack::PopAndDestroy(contentInfo);
sl@0
   794
	__UHEAP_MARKEND;
sl@0
   795
	
sl@0
   796
	return TestStepResult();
sl@0
   797
	}
sl@0
   798
	
sl@0
   799
	
sl@0
   800
//Digest Info
sl@0
   801
TVerdict CTPKCS7DigestInfoTest::doTestStepL()
sl@0
   802
	{
sl@0
   803
	if (TestStepResult() != EPass)
sl@0
   804
		{
sl@0
   805
		return TestStepResult();
sl@0
   806
		}
sl@0
   807
	__UHEAP_MARK;
sl@0
   808
	
sl@0
   809
	TInt reason;
sl@0
   810
	// Read from INI file.
sl@0
   811
	GetIntFromConfig(ConfigSection(),_L("Reason"),reason);
sl@0
   812
			
sl@0
   813
	CPKCS7DigestInfo* p7 = NULL;
sl@0
   814
	TRAPD (err, p7 = CPKCS7DigestInfo::NewL(iRawData->Des()));
sl@0
   815
	CleanupStack::PushL(p7);
sl@0
   816
	
sl@0
   817
	if (err != KErrNone)
sl@0
   818
		{
sl@0
   819
		if(err == reason)
sl@0
   820
			{
sl@0
   821
			CleanupStack::PopAndDestroy(p7);
sl@0
   822
			SetTestStepResult(EPass);
sl@0
   823
			return TestStepResult();
sl@0
   824
			}
sl@0
   825
		else
sl@0
   826
			{
sl@0
   827
			SetTestStepResult(EFail);
sl@0
   828
			ERR_PRINTF2(_L("Got %d building PKCS7 object"), err);
sl@0
   829
			}
sl@0
   830
		}
sl@0
   831
	
sl@0
   832
	//DIGEST	
sl@0
   833
    const TDesC8& digest = p7->Digest();
sl@0
   834
    if(digest.Compare(*iDigest) != 0)		
sl@0
   835
    	{
sl@0
   836
       	SetTestStepResult(EFail);
sl@0
   837
	   	ERR_PRINTF1(_L("Digest does not match"));
sl@0
   838
      	}
sl@0
   839
    else
sl@0
   840
		{
sl@0
   841
		INFO_PRINTF1(_L("Digest matches"));		
sl@0
   842
		}
sl@0
   843
     
sl@0
   844
    //ALGORITHM ID
sl@0
   845
    TBool checkAlgorithm;
sl@0
   846
    TPtrC expectedAlgorithm;
sl@0
   847
    TAlgorithmId algorithmId = (TAlgorithmId) -1;
sl@0
   848
   	
sl@0
   849
	checkAlgorithm = GetStringFromConfig(ConfigSection(),_L("AlgorithmId"), expectedAlgorithm);
sl@0
   850
sl@0
   851
	if(checkAlgorithm)
sl@0
   852
		{
sl@0
   853
	   	if(expectedAlgorithm.Compare(_L("SHA1")) == 0)
sl@0
   854
	   	 	{
sl@0
   855
	   	  	algorithmId = ESHA1;
sl@0
   856
	     	}
sl@0
   857
      	else if(expectedAlgorithm.Compare(_L("MD5")) == 0)
sl@0
   858
	   	 	{
sl@0
   859
	   	  	algorithmId = EMD5;
sl@0
   860
	     	}
sl@0
   861
      
sl@0
   862
    	if(algorithmId != p7->Algorithm())  
sl@0
   863
        	{
sl@0
   864
       		SetTestStepResult(EFail);
sl@0
   865
	   		ERR_PRINTF1(_L("Algorithm Id does not match"));	
sl@0
   866
      		}
sl@0
   867
      	else
sl@0
   868
      		{
sl@0
   869
      		INFO_PRINTF1(_L("Algorithm Id matches"));	
sl@0
   870
      		}	
sl@0
   871
    	}
sl@0
   872
    
sl@0
   873
    //ENCODED PARAMS
sl@0
   874
    const TDesC8& encodedParams= p7->EncodedParams();
sl@0
   875
    
sl@0
   876
    if(encodedParams.Length() != 0)
sl@0
   877
    	{
sl@0
   878
    	if(encodedParams.Compare(*iEncodedParams))  
sl@0
   879
    		{
sl@0
   880
       		SetTestStepResult(EFail);
sl@0
   881
	   		ERR_PRINTF1(_L("EncodedParams does not match"));		
sl@0
   882
      		}
sl@0
   883
    	else
sl@0
   884
    		{
sl@0
   885
    		INFO_PRINTF1(_L("EncodedParams match"));	
sl@0
   886
    		}  	
sl@0
   887
    	}
sl@0
   888
    else if(!iEncodedParams)
sl@0
   889
        {
sl@0
   890
        INFO_PRINTF1(_L("EncodedParams not present"));	
sl@0
   891
        }
sl@0
   892
    
sl@0
   893
    
sl@0
   894
    CleanupStack::PopAndDestroy(p7);
sl@0
   895
    __UHEAP_MARKEND;	
sl@0
   896
	return TestStepResult();
sl@0
   897
	}
sl@0
   898
	
sl@0
   899
	
sl@0
   900
//EncryptedDataCorrupt
sl@0
   901
//Step8
sl@0
   902
TVerdict CTPKCS7EncryptedDataCorruptTest::doTestStepL()
sl@0
   903
	{
sl@0
   904
	if (TestStepResult() != EPass)
sl@0
   905
		{
sl@0
   906
		return TestStepResult();
sl@0
   907
		}
sl@0
   908
    __UHEAP_MARK;	
sl@0
   909
	TInt reason;
sl@0
   910
	GetIntFromConfig(ConfigSection(),_L("Reason"),reason);
sl@0
   911
		
sl@0
   912
	CPKCS7ContentInfo *contentInfo = NULL;
sl@0
   913
	TRAPD (err, contentInfo = CPKCS7ContentInfo::NewL(iRawData->Des()));
sl@0
   914
	CleanupStack::PushL(contentInfo);
sl@0
   915
	
sl@0
   916
	if(err != KErrNone)
sl@0
   917
		{
sl@0
   918
		if (err != reason)
sl@0
   919
			{
sl@0
   920
			SetTestStepResult(EFail);
sl@0
   921
			INFO_PRINTF3(_L("Expected result %d, got %d"), reason, err);
sl@0
   922
			}
sl@0
   923
		}
sl@0
   924
	
sl@0
   925
		
sl@0
   926
	if (err == reason)
sl@0
   927
		{
sl@0
   928
		CleanupStack::PopAndDestroy(contentInfo);
sl@0
   929
		return TestStepResult();
sl@0
   930
		}
sl@0
   931
sl@0
   932
    CPKCS7EncryptedDataObject* p7 = NULL;
sl@0
   933
	TRAPD (err1, p7 = CPKCS7EncryptedDataObject::NewL(*contentInfo));
sl@0
   934
	CleanupStack::PushL(p7);
sl@0
   935
	
sl@0
   936
	if (err1 != reason)
sl@0
   937
		{
sl@0
   938
		SetTestStepResult(EFail);
sl@0
   939
		INFO_PRINTF3(_L("Expected result %d, got %d"), reason, err);
sl@0
   940
		}
sl@0
   941
	CleanupStack::PopAndDestroy(2,contentInfo);//p7, contentInfo
sl@0
   942
	__UHEAP_MARKEND;
sl@0
   943
	return TestStepResult();
sl@0
   944
	}
sl@0
   945
sl@0
   946
//DigestInfoCorrupt
sl@0
   947
//Step 9
sl@0
   948
TVerdict CTPKCS7DigestInfoCorruptTest::doTestStepL()
sl@0
   949
	{
sl@0
   950
	if (TestStepResult() != EPass)
sl@0
   951
		{
sl@0
   952
		return TestStepResult();
sl@0
   953
		}
sl@0
   954
    __UHEAP_MARK;
sl@0
   955
	TInt reason;
sl@0
   956
	GetIntFromConfig(ConfigSection(),_L("Reason"),reason);
sl@0
   957
	
sl@0
   958
	CPKCS7DigestInfo* p7 = NULL;
sl@0
   959
	TRAPD (err, p7 = CPKCS7DigestInfo::NewL(iRawData->Des()));
sl@0
   960
	delete p7;
sl@0
   961
	
sl@0
   962
	if (err != reason)
sl@0
   963
		{
sl@0
   964
		SetTestStepResult(EFail);
sl@0
   965
		INFO_PRINTF3(_L("Expected result %d, got %d"), reason, err);
sl@0
   966
		}
sl@0
   967
	__UHEAP_MARKEND;
sl@0
   968
	return TestStepResult();
sl@0
   969
	}
sl@0
   970