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