os/security/cryptoservices/certificateandkeymgmt/tpkcs7/tcmsstep.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) 2006-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
sl@0
    20
#include "tcmsstep.h"
sl@0
    21
#include <test/testexecutelog.h>
sl@0
    22
#include <asnpkcs.h>
sl@0
    23
#include "pkcs7signedobject.h"
sl@0
    24
#include <asn1enc.h>
sl@0
    25
#include <cmssignedobject.h>
sl@0
    26
#include <cmsdefs.h>
sl@0
    27
#include <hash.h>
sl@0
    28
#include <asymmetric.h>
sl@0
    29
#include <cmssigneridentifier.h>
sl@0
    30
#include <x509certext.h>
sl@0
    31
#include <pkixcertchain.h>
sl@0
    32
#include <cmscontentinfo.h>
sl@0
    33
#include "validate.h"
sl@0
    34
sl@0
    35
sl@0
    36
CTCmsBaseStep::CTCmsBaseStep()
sl@0
    37
	{
sl@0
    38
	}
sl@0
    39
sl@0
    40
CTCmsBaseStep::~CTCmsBaseStep()
sl@0
    41
	{
sl@0
    42
	iFs.Close ();
sl@0
    43
	delete iDataContent;
sl@0
    44
	delete iExpectedEncoding;
sl@0
    45
	__UHEAP_MARKEND;
sl@0
    46
	}
sl@0
    47
sl@0
    48
TVerdict CTCmsBaseStep::doTestStepPreambleL()
sl@0
    49
	{
sl@0
    50
	__UHEAP_MARK;	
sl@0
    51
	User::LeaveIfError (iFs.Connect());	
sl@0
    52
	
sl@0
    53
	//Read the data to be signed
sl@0
    54
	iDataContent = readFileL(_L("Data"));
sl@0
    55
	
sl@0
    56
	if (iDataContent == NULL)
sl@0
    57
		{
sl@0
    58
		iDataContent=KNullDesC8().AllocL();
sl@0
    59
		}
sl@0
    60
	
sl@0
    61
	//Read the expected data type
sl@0
    62
	TPtrC contentDataType;
sl@0
    63
	if (GetStringFromConfig(ConfigSection(), _L("ExpectedDataType"), contentDataType))
sl@0
    64
		{
sl@0
    65
		iExpectedDataType=CovertContentDataTypeNameToDataType(contentDataType);			
sl@0
    66
		}
sl@0
    67
sl@0
    68
	//Read the expected result
sl@0
    69
	iExpectedEncoding = readFileL(_L("Result"));
sl@0
    70
	if (!iExpectedEncoding)
sl@0
    71
		{
sl@0
    72
		INFO_PRINTF1(_L("Failed to read 'Result' section of script"));
sl@0
    73
		SetTestStepResult(ETestSuiteError);			
sl@0
    74
		}
sl@0
    75
sl@0
    76
	GetIntFromConfig(ConfigSection(), _L("ExpectedResult"), iExpectedResult);	
sl@0
    77
	GetBoolFromConfig(ConfigSection(), _L("IsOOMTest"), iIsOOMTest);
sl@0
    78
	return TestStepResult();	
sl@0
    79
	}
sl@0
    80
sl@0
    81
TInt CTCmsBaseStep::CovertContentDataTypeNameToDataType(const TDesC& aDataTypeName)
sl@0
    82
	{
sl@0
    83
	if (aDataTypeName.Compare(_L("DATA"))==0)
sl@0
    84
		{
sl@0
    85
		return EContentTypeData;	
sl@0
    86
		}
sl@0
    87
	else	if (aDataTypeName.Compare(_L("SIGNEDDATA"))==0)
sl@0
    88
				{
sl@0
    89
				return EContentTypeSignedData;
sl@0
    90
				}
sl@0
    91
		 else	if (aDataTypeName.Compare(_L("ENVELOPEDDATA"))==0)
sl@0
    92
					 {
sl@0
    93
					 return EContentTypeEnvelopedData;	
sl@0
    94
					 }
sl@0
    95
			  else	if (aDataTypeName.Compare(_L("DIGESTEDDATA"))==0)
sl@0
    96
						{
sl@0
    97
						return EContentTypeDigestedData;
sl@0
    98
						}
sl@0
    99
					else 	if (aDataTypeName.Compare(_L("ENCRYPTEDDATA"))==0)
sl@0
   100
								{
sl@0
   101
								return EContentTypeEncryptedData;
sl@0
   102
								}
sl@0
   103
							else 	if (aDataTypeName.Compare(_L("SIGNEDANDENVELOPEDDATA"))==0)
sl@0
   104
										{
sl@0
   105
										return CPKCS7ContentInfo::EContentTypeSignedAndEnvelopedData;	
sl@0
   106
										}
sl@0
   107
									else	if (aDataTypeName.Compare(_L("AUTHDATA"))==0)
sl@0
   108
												{
sl@0
   109
												return EContentTypeAuthenticatedData;	
sl@0
   110
												}
sl@0
   111
											else
sl@0
   112
												{
sl@0
   113
												User::Leave(KErrArgument);
sl@0
   114
												return EContentTypeData;
sl@0
   115
												}
sl@0
   116
	}
sl@0
   117
sl@0
   118
HBufC8* CTCmsBaseStep::readFileL (TPtrC tag)
sl@0
   119
	{
sl@0
   120
	TPtrC fileName;
sl@0
   121
	if (GetStringFromConfig(ConfigSection(), tag, fileName) == EFalse)
sl@0
   122
		{
sl@0
   123
		return NULL;
sl@0
   124
		}
sl@0
   125
sl@0
   126
	RFile file;
sl@0
   127
	if (file.Open(iFs, fileName, EFileRead) != KErrNone)
sl@0
   128
		{
sl@0
   129
		INFO_PRINTF2(_L("Cannot open file %S for reading"), &fileName);
sl@0
   130
		return NULL;
sl@0
   131
		}
sl@0
   132
	CleanupClosePushL(file);
sl@0
   133
	TInt fileSize = 0;
sl@0
   134
	User::LeaveIfError(file.Size(fileSize));
sl@0
   135
	HBufC8* result = HBufC8::NewMaxL(fileSize);
sl@0
   136
	TPtr8 rawDataPtr(result->Des());
sl@0
   137
	file.Read (rawDataPtr);
sl@0
   138
	CleanupStack::PopAndDestroy (&file);
sl@0
   139
	INFO_PRINTF3(_L("Read %d octets from %S"), result->Size(), &fileName);
sl@0
   140
	return result;
sl@0
   141
	}
sl@0
   142
sl@0
   143
void CTCmsBaseStep::OutputResultToFileL(const TDesC8& aSignature)
sl@0
   144
	{
sl@0
   145
	TDriveUnit sysDrive (RFs::GetSystemDrive());
sl@0
   146
	TBuf<128> rName (sysDrive.Name());;
sl@0
   147
	rName.Append(_L("\\tpkcs7\\myresults\\"));
sl@0
   148
			
sl@0
   149
	TInt err=iFs.MkDir(rName);
sl@0
   150
	if (err!=KErrNone && err!=KErrAlreadyExists)
sl@0
   151
		{
sl@0
   152
		User::Leave(err);	
sl@0
   153
		}
sl@0
   154
				
sl@0
   155
	RFile file;
sl@0
   156
	CleanupClosePushL(file);
sl@0
   157
	
sl@0
   158
	_LIT(KExtension, ".der");
sl@0
   159
	rName.Append(ConfigSection());
sl@0
   160
	rName.Append(KExtension);
sl@0
   161
	rName.LowerCase();
sl@0
   162
	User::LeaveIfError(file.Replace(iFs, rName, EFileWrite | EFileStream));
sl@0
   163
	User::LeaveIfError(file.Write(aSignature));
sl@0
   164
	CleanupStack::PopAndDestroy(&file);
sl@0
   165
	}
sl@0
   166
sl@0
   167
sl@0
   168
HBufC8* CTCmsBaseStep::CreateDEREncodingLC(const CASN1EncBase& aEncoding)
sl@0
   169
	{	
sl@0
   170
	TUint len = aEncoding.LengthDER();
sl@0
   171
	HBufC8* buf = HBufC8::NewMaxLC(len);
sl@0
   172
	TUint pos = 0;
sl@0
   173
	TPtr8 bufptr(buf->Des());
sl@0
   174
	aEncoding.WriteDERL(bufptr, pos);
sl@0
   175
	return buf;
sl@0
   176
	}
sl@0
   177
sl@0
   178
TVerdict CTCmsBaseStep::doTestStepL()
sl@0
   179
	{
sl@0
   180
	if (!iIsOOMTest)
sl@0
   181
		{
sl@0
   182
		TRAPD(err, doTestL();)
sl@0
   183
		if (err!=iExpectedResult)
sl@0
   184
			{
sl@0
   185
			SetTestStepResult(EFail);
sl@0
   186
			User::Leave(err);
sl@0
   187
			}
sl@0
   188
		return TestStepResult();
sl@0
   189
		}
sl@0
   190
	else
sl@0
   191
		{
sl@0
   192
		return doOOMTestL();	
sl@0
   193
		}	
sl@0
   194
	}
sl@0
   195
sl@0
   196
TVerdict CTCmsBaseStep::doOOMTestL()
sl@0
   197
	{
sl@0
   198
	TVerdict verdict = EFail;
sl@0
   199
 	TInt countAfter = 0;
sl@0
   200
	TInt countBefore = 0;
sl@0
   201
 	for (TInt oomCount = 0; ; oomCount++)
sl@0
   202
 		{
sl@0
   203
 		__UHEAP_RESET;
sl@0
   204
 		__UHEAP_SETFAIL(RHeap::EDeterministic, oomCount);
sl@0
   205
 		countBefore = User::CountAllocCells();
sl@0
   206
 		TRAPD(error, doTestL());
sl@0
   207
 		countAfter = User::CountAllocCells();
sl@0
   208
 		__UHEAP_RESET;
sl@0
   209
 		if (error != KErrNoMemory)
sl@0
   210
 			{
sl@0
   211
 			verdict = EPass;
sl@0
   212
 			INFO_PRINTF2(_L("OOM Status %d"),error);
sl@0
   213
			INFO_PRINTF1(_L("Test outcome : Passed"));
sl@0
   214
 			break;
sl@0
   215
 			}
sl@0
   216
 		else
sl@0
   217
 			{
sl@0
   218
 			if (countBefore != countAfter)
sl@0
   219
 				{
sl@0
   220
 				INFO_PRINTF2(_L("OOM Status %d"),error);
sl@0
   221
 				INFO_PRINTF2(_L("OOM Failed at %d"), oomCount);
sl@0
   222
 				SetTestStepResult(EFail);
sl@0
   223
 				break;
sl@0
   224
 				}
sl@0
   225
 			}
sl@0
   226
 		INFO_PRINTF2(_L("OOM Failed Point status %d"), error);
sl@0
   227
		}
sl@0
   228
	INFO_PRINTF3(_L("Heap alloc count ok: %d final vs %d initial"), countAfter,countBefore);
sl@0
   229
 	SetTestStepResult(verdict);
sl@0
   230
 	if (verdict==EFail)
sl@0
   231
	 	{
sl@0
   232
 		User::Leave(KErrGeneral);	 		
sl@0
   233
	 	}	 	
sl@0
   234
 	return verdict;
sl@0
   235
	}
sl@0
   236
sl@0
   237
sl@0
   238
//
sl@0
   239
// Implementation of CMS Data Test Step
sl@0
   240
//
sl@0
   241
CTCmsDataStep::CTCmsDataStep()
sl@0
   242
	{
sl@0
   243
	}
sl@0
   244
sl@0
   245
CTCmsDataStep::~CTCmsDataStep()
sl@0
   246
	{
sl@0
   247
	}
sl@0
   248
sl@0
   249
	
sl@0
   250
void CTCmsDataStep::doTestL()
sl@0
   251
	{
sl@0
   252
	__UHEAP_MARK;
sl@0
   253
	CCmsContentInfo* content=CCmsContentInfo::NewL(EContentTypeData, *iDataContent);
sl@0
   254
	CleanupStack::PushL(content);
sl@0
   255
	CASN1EncSequence* contentSeq=content->EncodeASN1DERLC();
sl@0
   256
	HBufC8* signature=CreateDEREncodingLC(*contentSeq);
sl@0
   257
	CleanupStack::Pop(signature);
sl@0
   258
	CleanupStack::PopAndDestroy(2, content);
sl@0
   259
	CleanupStack::PushL(signature);
sl@0
   260
	//OutputResultToFileL(signature->Des());
sl@0
   261
sl@0
   262
	TBool r=signature->Compare(*iExpectedEncoding);
sl@0
   263
	if (r!=0 && !iIsOOMTest)
sl@0
   264
		{
sl@0
   265
		INFO_PRINTF1(_L("CMS Data Type Encoding Error"));
sl@0
   266
		User::Leave(KErrGeneral);
sl@0
   267
		}
sl@0
   268
	else
sl@0
   269
		{
sl@0
   270
		DecodingAndCheckL(*iExpectedEncoding);
sl@0
   271
		}
sl@0
   272
sl@0
   273
	CleanupStack::PopAndDestroy(signature);	
sl@0
   274
	}
sl@0
   275
void CTCmsDataStep::DecodingAndCheckL(TDesC8& aRawData)
sl@0
   276
	{
sl@0
   277
	INFO_PRINTF1(_L("Start CMS Data Type Decoding"));
sl@0
   278
	CCmsContentInfo* content=CCmsContentInfo::NewL(aRawData);
sl@0
   279
	CleanupStack::PushL(content);
sl@0
   280
	if (content->ContentType()!=EContentTypeData)
sl@0
   281
		{
sl@0
   282
		INFO_PRINTF1(_L("CMS Data Type is not as expected"));
sl@0
   283
		User::Leave(KErrGeneral);
sl@0
   284
		}
sl@0
   285
	else
sl@0
   286
		{
sl@0
   287
		if (content->ContentData()!=iDataContent->Des())
sl@0
   288
			{
sl@0
   289
			INFO_PRINTF1(_L("CMS Data Content is not as expected"));
sl@0
   290
			User::Leave(KErrGeneral);					
sl@0
   291
			}
sl@0
   292
		}
sl@0
   293
	CleanupStack::PopAndDestroy(content);		
sl@0
   294
	}
sl@0
   295
//
sl@0
   296
// Implementation of CMS Data Test Step
sl@0
   297
//
sl@0
   298
CTCmsDataDecodingStep::CTCmsDataDecodingStep()
sl@0
   299
	{
sl@0
   300
	}
sl@0
   301
sl@0
   302
CTCmsDataDecodingStep::~CTCmsDataDecodingStep()
sl@0
   303
	{
sl@0
   304
	}
sl@0
   305
sl@0
   306
void CTCmsDataDecodingStep::doTestL()
sl@0
   307
	{
sl@0
   308
	DecodingAndCheckL(*iExpectedEncoding);	
sl@0
   309
	}
sl@0
   310
sl@0
   311
sl@0
   312
//
sl@0
   313
// Implementation of Signed Data Test Base Step
sl@0
   314
//
sl@0
   315
CTSignedDataBaseStep::CTSignedDataBaseStep() : iContentType(EContentTypeData), iRsaAlgorithm(ETrue) 
sl@0
   316
	{
sl@0
   317
	}
sl@0
   318
sl@0
   319
CTSignedDataBaseStep::~CTSignedDataBaseStep()
sl@0
   320
	{
sl@0
   321
	iDecPKCS8Data.ResetAndDestroy();
sl@0
   322
	iCertificates.ResetAndDestroy();
sl@0
   323
	iSignerInfoVersion.Close();
sl@0
   324
	iHashName.Close();
sl@0
   325
	iValidateResults.Close();
sl@0
   326
	iSignedAttributePresent.Close();
sl@0
   327
	iUnSignedAttributePresent.Close();
sl@0
   328
	delete iRootCertificate;
sl@0
   329
	delete iAdditionalCertificate;
sl@0
   330
	delete iAdditionalEncodedCertificate;
sl@0
   331
	}
sl@0
   332
sl@0
   333
sl@0
   334
TInt CTSignedDataBaseStep::CovertHashNameToAlgorithmId(const TDesC& aHashName)
sl@0
   335
	{
sl@0
   336
	if (aHashName.Compare(_L("SHA1"))==0)
sl@0
   337
		{
sl@0
   338
		return ESHA1;	
sl@0
   339
		}
sl@0
   340
	else if (aHashName.Compare(_L("MD5"))==0)
sl@0
   341
			{
sl@0
   342
			return EMD5;
sl@0
   343
			}
sl@0
   344
		 else
sl@0
   345
			 {
sl@0
   346
			 return EMD2;	
sl@0
   347
			 }
sl@0
   348
	}
sl@0
   349
sl@0
   350
TInt CTSignedDataBaseStep::CovertCertificateNameToCertificateType(const TDesC& aCertificateName)
sl@0
   351
	{
sl@0
   352
	if (aCertificateName.Compare(_L("X509"))==0)
sl@0
   353
		{
sl@0
   354
		return CCmsCertificateChoice::ECertificateX509;	
sl@0
   355
		}
sl@0
   356
	else if (aCertificateName.Compare(_L("Attribute"))==0)
sl@0
   357
			{
sl@0
   358
			return CCmsCertificateChoice::ECertificateAttribute;
sl@0
   359
			}
sl@0
   360
		 else
sl@0
   361
			 {
sl@0
   362
			 return CCmsCertificateChoice::ECertificateExtendedCerificate;	
sl@0
   363
			 }
sl@0
   364
	}
sl@0
   365
sl@0
   366
sl@0
   367
TVerdict CTSignedDataBaseStep::doTestStepPreambleL()
sl@0
   368
	{
sl@0
   369
	if (CTCmsBaseStep::doTestStepPreambleL()==EFail)
sl@0
   370
		{
sl@0
   371
		SetTestStepResult(EFail);
sl@0
   372
		}
sl@0
   373
	else
sl@0
   374
		{
sl@0
   375
		//Read the configurations
sl@0
   376
		GetBoolFromConfig(ConfigSection(), _L("HashAvailable"), iIsHashAvailable);
sl@0
   377
		GetBoolFromConfig(ConfigSection(), _L("DataDetached"), iIsDetached);
sl@0
   378
		GetBoolFromConfig(ConfigSection(), _L("CertificateSetPresent"), iCertificateSetPresent);
sl@0
   379
		GetBoolFromConfig(ConfigSection(), _L("CRLsSetPresent"), iCRLsSetPresent);
sl@0
   380
		GetIntFromConfig(ConfigSection(), _L("SignedDataVersion"), iSignedDataVersion);
sl@0
   381
		GetIntFromConfig(ConfigSection(), _L("AlgorithmCount"), iAlgorithmCount);
sl@0
   382
		GetIntFromConfig(ConfigSection(), _L("CertsCount"), iCertsCount);
sl@0
   383
		GetIntFromConfig(ConfigSection(), _L("SignerCount"), iSignerCount);
sl@0
   384
		GetBoolFromConfig(ConfigSection(), _L("NoCertSet"), iNoCertSet);
sl@0
   385
		GetBoolFromConfig(ConfigSection(), _L("ValidateUsingUserCerts"), iValidateUsingUserCerts);
sl@0
   386
		GetBoolFromConfig(ConfigSection(), _L("NoSigning"), iNoSigning);
sl@0
   387
		GetBoolFromConfig(ConfigSection(), _L("NoValidationTest"), iNoValidationTest);
sl@0
   388
		GetBoolFromConfig(ConfigSection(), _L("TwoStepCreation"), iTwoStepCreation);
sl@0
   389
		GetBoolFromConfig(ConfigSection(), _L("ValidationDetachedWithoutInput"), iValidationDetachedWithoutInput);
sl@0
   390
		
sl@0
   391
sl@0
   392
		HBufC8* certificate = readFileL(_L("RootCertificate"));
sl@0
   393
		if (certificate)
sl@0
   394
			{
sl@0
   395
			CleanupStack::PushL(certificate);
sl@0
   396
			iRootCertificate = CX509Certificate::NewL(*certificate);
sl@0
   397
			CleanupStack::PopAndDestroy (certificate);
sl@0
   398
			}
sl@0
   399
sl@0
   400
		certificate = readFileL(_L("AddtionalCertificate"));
sl@0
   401
		if (certificate)
sl@0
   402
			{
sl@0
   403
			CleanupStack::PushL(certificate);
sl@0
   404
			iAdditionalCertificate = CX509Certificate::NewL(*certificate);
sl@0
   405
			CleanupStack::PopAndDestroy (certificate);			
sl@0
   406
			}
sl@0
   407
		TPtrC certTypeName;
sl@0
   408
		if (GetStringFromConfig(ConfigSection(), _L("AdditionalEncodedCertificateType"), certTypeName))
sl@0
   409
			{
sl@0
   410
			iAdditionalEncodedCertificateType=CovertCertificateNameToCertificateType(certTypeName);
sl@0
   411
			iAdditionalEncodedCertificate=readFileL (_L("AdditionalEncodedCertificate"));				
sl@0
   412
			}
sl@0
   413
					
sl@0
   414
		//Read the certificates, private keys and hash algorithm
sl@0
   415
		TInt index(0);
sl@0
   416
		
sl@0
   417
		TName fKeyName;
sl@0
   418
		fKeyName.Format(_L("PrivateKey_%d"), index);
sl@0
   419
		
sl@0
   420
		TName fCertName;
sl@0
   421
		fCertName.Format(_L("Certificate_%d"), index);
sl@0
   422
		
sl@0
   423
		TName fHashAlgorithmName;
sl@0
   424
		fHashAlgorithmName.Format(_L("HashAlgorithm_%d"), index);
sl@0
   425
sl@0
   426
		TName fValidationResult;
sl@0
   427
		fValidationResult.Format(_L("ValidationResult_%d"), index);
sl@0
   428
		
sl@0
   429
		TName fSignedAttributePresent;
sl@0
   430
		fSignedAttributePresent.Format(_L("SignedAttributePresent_%d"), index);
sl@0
   431
		
sl@0
   432
		TName fUnSignedAttributePresent;
sl@0
   433
		fUnSignedAttributePresent.Format(_L("UnSignedAttributePresent_%d"), index);
sl@0
   434
sl@0
   435
		TName fSignerInfoVersion;
sl@0
   436
		fSignerInfoVersion.Format(_L("SignerInfoVersion_%d"), index);
sl@0
   437
sl@0
   438
		TPtrC hashName;
sl@0
   439
		TBool vResult(EFalse);
sl@0
   440
		TBool sAP(EFalse);
sl@0
   441
		TBool uSAP(EFalse);
sl@0
   442
		TInt signerInfoVersion;
sl@0
   443
		TPtrC keyName;
sl@0
   444
		TPtrC certName;
sl@0
   445
		
sl@0
   446
		while ( GetStringFromConfig(ConfigSection(), fKeyName, keyName)
sl@0
   447
				&& GetStringFromConfig(ConfigSection(), fCertName, certName)
sl@0
   448
				&& GetStringFromConfig(ConfigSection(), fHashAlgorithmName, hashName)
sl@0
   449
				&& GetBoolFromConfig(ConfigSection(), fValidationResult, vResult)
sl@0
   450
				&& GetBoolFromConfig(ConfigSection(), fSignedAttributePresent, sAP)
sl@0
   451
				&& GetBoolFromConfig(ConfigSection(), fUnSignedAttributePresent, uSAP)
sl@0
   452
				&& GetIntFromConfig(ConfigSection(), fSignerInfoVersion, signerInfoVersion) )
sl@0
   453
			{
sl@0
   454
			//Construct private keys
sl@0
   455
			HBufC8* privateKey(NULL);
sl@0
   456
			if ((privateKey=readFileL(fKeyName))!=NULL)
sl@0
   457
				{
sl@0
   458
				CleanupStack::PushL (privateKey);
sl@0
   459
				CDecPKCS8Data* pkcs8Data=TASN1DecPKCS8::DecodeDERL(privateKey->Des());
sl@0
   460
				CleanupStack::PushL (pkcs8Data);
sl@0
   461
				iDecPKCS8Data.AppendL(pkcs8Data);
sl@0
   462
				CleanupStack::Pop(pkcs8Data);
sl@0
   463
				CleanupStack::PopAndDestroy(privateKey);	
sl@0
   464
				}
sl@0
   465
			
sl@0
   466
			//Construct X509 certificate
sl@0
   467
			HBufC8* cert(NULL);
sl@0
   468
			if ((cert=readFileL(fCertName))!=NULL)
sl@0
   469
				{
sl@0
   470
				CleanupStack::PushL (cert);
sl@0
   471
				CX509Certificate* x509cert=CX509Certificate::NewLC(cert->Des());		
sl@0
   472
				iCertificates.AppendL(x509cert);
sl@0
   473
				CleanupStack::Pop(x509cert);
sl@0
   474
				CleanupStack::PopAndDestroy(cert);				
sl@0
   475
				}
sl@0
   476
				
sl@0
   477
			TInt hashId=CovertHashNameToAlgorithmId(hashName);		
sl@0
   478
			iHashName.AppendL(hashId);
sl@0
   479
					
sl@0
   480
			iValidateResults.AppendL(vResult);
sl@0
   481
			iSignedAttributePresent.AppendL(sAP);
sl@0
   482
			iUnSignedAttributePresent.AppendL(uSAP);
sl@0
   483
			iSignerInfoVersion.AppendL(signerInfoVersion);
sl@0
   484
			
sl@0
   485
			//for next pair
sl@0
   486
			index++;
sl@0
   487
			fKeyName.Format(_L("PrivateKey_%d"), index);
sl@0
   488
			fCertName.Format(_L("Certificate_%d"), index);
sl@0
   489
			fHashAlgorithmName.Format(_L("HashAlgorithm_%d"), index);
sl@0
   490
			fValidationResult.Format(_L("ValidationResult_%d"), index);
sl@0
   491
			fSignedAttributePresent.Format(_L("SignedAttributePresent_%d"), index);
sl@0
   492
			fUnSignedAttributePresent.Format(_L("UnSignedAttributePresent_%d"), index);
sl@0
   493
			fSignerInfoVersion.Format(_L("SignerInfoVersion_%d"), index);
sl@0
   494
			}			
sl@0
   495
		}
sl@0
   496
	return TestStepResult();
sl@0
   497
	}
sl@0
   498
sl@0
   499
CMessageDigest* CTSignedDataBaseStep::CreateHashLC(TAlgorithmId aAlgorithmId)
sl@0
   500
	{
sl@0
   501
	CMessageDigest* hash(NULL);
sl@0
   502
	switch (aAlgorithmId)
sl@0
   503
		{
sl@0
   504
	case EMD2:
sl@0
   505
		hash=CMD2::NewL();
sl@0
   506
		break;
sl@0
   507
		
sl@0
   508
	case EMD5:
sl@0
   509
		hash=CMD5::NewL();
sl@0
   510
		break;
sl@0
   511
		
sl@0
   512
	case ESHA1:
sl@0
   513
		hash=CSHA1::NewL();
sl@0
   514
		break;
sl@0
   515
		
sl@0
   516
	default:
sl@0
   517
		User::Leave(KErrNotSupported);
sl@0
   518
		}
sl@0
   519
	CleanupStack::PushL(hash);
sl@0
   520
	return hash;		
sl@0
   521
	}
sl@0
   522
sl@0
   523
sl@0
   524
//
sl@0
   525
// Implementation of CMS Signed Data Test Step
sl@0
   526
//
sl@0
   527
CTCmsSignedDataStep::CTCmsSignedDataStep()
sl@0
   528
	{
sl@0
   529
	}
sl@0
   530
sl@0
   531
CTCmsSignedDataStep::~CTCmsSignedDataStep()
sl@0
   532
	{
sl@0
   533
	}
sl@0
   534
sl@0
   535
sl@0
   536
void CTCmsSignedDataStep::CheckAndValidateSignedDataL(TDesC8& aRawData)
sl@0
   537
	{
sl@0
   538
	//Decode the content info encoding read from predefined file	
sl@0
   539
	CCmsContentInfo* content=CCmsContentInfo::NewL(aRawData);
sl@0
   540
	CleanupStack::PushL(content);
sl@0
   541
	if (content->ContentType()!=EContentTypeSignedData)
sl@0
   542
		{
sl@0
   543
		INFO_PRINTF1(_L("Content Type is not Signed Data"));
sl@0
   544
		User::Leave(KErrGeneral);
sl@0
   545
		}
sl@0
   546
	else
sl@0
   547
		{
sl@0
   548
		//Decode the signed data and check the fields
sl@0
   549
		CCmsSignedObject* signedData=CCmsSignedObject::NewL(*content);
sl@0
   550
		CleanupStack::PushL(signedData);	
sl@0
   551
		CheckSignedDataFieldsL(*signedData);
sl@0
   552
		
sl@0
   553
		//Validate the signatures
sl@0
   554
		const RPointerArray<CCmsSignerInfo>& signerInfos=signedData->SignerInfo();
sl@0
   555
		CheckSignerInfoFieldsL(signerInfos);
sl@0
   556
		
sl@0
   557
		if (!iNoValidationTest)
sl@0
   558
			{
sl@0
   559
			TInt count=signerInfos.Count();
sl@0
   560
			for (TInt i=0;i<count;i++)
sl@0
   561
				{
sl@0
   562
				HBufC8* certificateEncoding = NULL;
sl@0
   563
				
sl@0
   564
				CMessageDigest* hash(NULL);
sl@0
   565
				if (iIsHashAvailable)
sl@0
   566
					{
sl@0
   567
					hash=CreateHashLC((TAlgorithmId)iHashName[i]);
sl@0
   568
					}
sl@0
   569
					
sl@0
   570
				TBool isValid(EFalse);
sl@0
   571
				if (iValidateUsingUserCerts)
sl@0
   572
					{
sl@0
   573
					INFO_PRINTF1(_L("Test validation by using given certificates"));
sl@0
   574
					if (iIsHashAvailable)
sl@0
   575
						{
sl@0
   576
						isValid = signedData->ValidateSignerLC(*signerInfos[i], iCertificates, certificateEncoding, ETrue, hash->Hash(iDataContent->Des()));
sl@0
   577
						}
sl@0
   578
					else 
sl@0
   579
						{
sl@0
   580
						if (iIsDetached)
sl@0
   581
							{
sl@0
   582
							if (!iValidationDetachedWithoutInput)
sl@0
   583
								{
sl@0
   584
								isValid = signedData->ValidateSignerLC(*signerInfos[i], iCertificates, certificateEncoding, EFalse, iDataContent->Des());
sl@0
   585
								}
sl@0
   586
							else
sl@0
   587
								{
sl@0
   588
								isValid = signedData->ValidateSignerLC(*signerInfos[i], iCertificates, certificateEncoding);	
sl@0
   589
								}							
sl@0
   590
							}
sl@0
   591
						else
sl@0
   592
							{
sl@0
   593
							isValid = signedData->ValidateSignerLC(*signerInfos[i], iCertificates, certificateEncoding);	
sl@0
   594
							}
sl@0
   595
						}					
sl@0
   596
					}
sl@0
   597
				else
sl@0
   598
					{
sl@0
   599
					INFO_PRINTF1(_L("Test validation by using the embedded certificates"));
sl@0
   600
					
sl@0
   601
					if (iIsHashAvailable)
sl@0
   602
						{
sl@0
   603
						isValid = signedData->ValidateSignerLC(*signerInfos[i], certificateEncoding, ETrue, hash->Hash(iDataContent->Des()));
sl@0
   604
						}
sl@0
   605
					else 
sl@0
   606
						{
sl@0
   607
						if (iIsDetached)
sl@0
   608
							{
sl@0
   609
							if (!iValidationDetachedWithoutInput)
sl@0
   610
								{
sl@0
   611
								isValid = signedData->ValidateSignerLC(*signerInfos[i], certificateEncoding, EFalse, iDataContent->Des());	
sl@0
   612
								}
sl@0
   613
							else
sl@0
   614
								{
sl@0
   615
								isValid = signedData->ValidateSignerLC(*signerInfos[i], certificateEncoding);	
sl@0
   616
								}
sl@0
   617
							}
sl@0
   618
						else
sl@0
   619
							{
sl@0
   620
							isValid = signedData->ValidateSignerLC(*signerInfos[i], certificateEncoding);	
sl@0
   621
							}
sl@0
   622
						}					
sl@0
   623
					}
sl@0
   624
									
sl@0
   625
				if (!isValid)
sl@0
   626
					{
sl@0
   627
					INFO_PRINTF1(_L("Couldn't validate signer"));
sl@0
   628
					}
sl@0
   629
				else
sl@0
   630
					{
sl@0
   631
					CActiveScheduler* sched = NULL;
sl@0
   632
					if (CActiveScheduler::Current() == NULL)
sl@0
   633
						{
sl@0
   634
						INFO_PRINTF1(_L("Installing scheduler"));
sl@0
   635
						sched = new (ELeave) CActiveScheduler();
sl@0
   636
						CleanupStack::PushL (sched);
sl@0
   637
						CActiveScheduler::Install (sched);
sl@0
   638
						}
sl@0
   639
					RPointerArray<CX509Certificate> roots (&iRootCertificate, 1);
sl@0
   640
					CPKIXCertChain * chain = CPKIXCertChain::NewLC(iFs, *certificateEncoding, roots);
sl@0
   641
					
sl@0
   642
					TTime tm;
sl@0
   643
					_LIT(KDateCorrect1,"20061128:"); 
sl@0
   644
					TBuf <24> theDate(KDateCorrect1); 
sl@0
   645
					TInt err=tm.Set(theDate); 
sl@0
   646
					if(err)
sl@0
   647
						{
sl@0
   648
						tm.HomeTime();
sl@0
   649
						}
sl@0
   650
						
sl@0
   651
					CPKIXValidationResult* result = CPKIXValidationResult::NewLC();
sl@0
   652
					CTPKCS7Validator* validator = new (ELeave) CTPKCS7Validator (chain, result, &tm);
sl@0
   653
					validator->doValidate ();
sl@0
   654
					sched->Start ();
sl@0
   655
					if (result->Error().iReason == EValidatedOK)
sl@0
   656
						{
sl@0
   657
						isValid = ETrue;
sl@0
   658
						INFO_PRINTF1(_L("Validation success"));
sl@0
   659
						}
sl@0
   660
					else
sl@0
   661
						{
sl@0
   662
						isValid = EFalse;
sl@0
   663
						INFO_PRINTF2(_L("Validation failed: %d"), result->Error().iReason);
sl@0
   664
						}
sl@0
   665
	        		delete validator;
sl@0
   666
					CleanupStack::PopAndDestroy(result);
sl@0
   667
					CleanupStack::PopAndDestroy(chain);
sl@0
   668
					if (sched)
sl@0
   669
						{
sl@0
   670
						CActiveScheduler::Install (NULL);
sl@0
   671
						CleanupStack::PopAndDestroy (sched);
sl@0
   672
						}
sl@0
   673
					}
sl@0
   674
				
sl@0
   675
				CleanupStack::PopAndDestroy(certificateEncoding);				
sl@0
   676
				if (hash)
sl@0
   677
					{
sl@0
   678
					CleanupStack::PopAndDestroy(hash);	
sl@0
   679
					}				
sl@0
   680
				
sl@0
   681
				if (isValid!=iValidateResults[i])
sl@0
   682
					{
sl@0
   683
					INFO_PRINTF1(_L("validate result not as expected"));
sl@0
   684
					User::Leave(KErrGeneral);
sl@0
   685
					}
sl@0
   686
				}			
sl@0
   687
			}
sl@0
   688
		CleanupStack::PopAndDestroy(signedData);
sl@0
   689
		}
sl@0
   690
	CleanupStack::PopAndDestroy(content);						
sl@0
   691
	}
sl@0
   692
sl@0
   693
void CTCmsSignedDataStep::CheckEncapsulatedContentFieldsL(const CEncapsulatedContentInfo& aEncapContentInfo)
sl@0
   694
	{
sl@0
   695
	if (aEncapContentInfo.ContentType()!=EContentTypeData)
sl@0
   696
		{
sl@0
   697
		INFO_PRINTF1(_L("Encapsulated data Content is not data content type"));
sl@0
   698
		User::Leave(KErrGeneral);
sl@0
   699
		}
sl@0
   700
	else
sl@0
   701
		{
sl@0
   702
		if (aEncapContentInfo.IsContentDataPresent() == iIsDetached)
sl@0
   703
			{
sl@0
   704
			INFO_PRINTF1(_L("Encapsulated data Content attachment not as expected"));
sl@0
   705
			User::Leave(KErrGeneral);
sl@0
   706
			}
sl@0
   707
		else
sl@0
   708
			{
sl@0
   709
			if (aEncapContentInfo.IsContentDataPresent() && aEncapContentInfo.ContentData()!=*iDataContent)
sl@0
   710
				{
sl@0
   711
				INFO_PRINTF1(_L("Encapsulated data Content not as expected"));
sl@0
   712
				User::Leave(KErrGeneral);
sl@0
   713
				}
sl@0
   714
			}
sl@0
   715
		}		
sl@0
   716
	}
sl@0
   717
sl@0
   718
void CTCmsSignedDataStep::CheckAlgorithmSetFieldsL(const RPointerArray<CX509AlgorithmIdentifier>& aAlgorithms)
sl@0
   719
	{
sl@0
   720
	if (iAlgorithmCount!=aAlgorithms.Count())
sl@0
   721
		{
sl@0
   722
		INFO_PRINTF1(_L("Number of Algorithm ID is not as expected"));
sl@0
   723
		User::Leave(KErrGeneral);
sl@0
   724
		}
sl@0
   725
	}
sl@0
   726
sl@0
   727
void CTCmsSignedDataStep::CheckCertificateSetFieldsL(const CCmsSignedObject& aSignedData)
sl@0
   728
	{
sl@0
   729
	if (aSignedData.IsCertificateSetPresent())
sl@0
   730
		{
sl@0
   731
		const RPointerArray<CCmsCertificateChoice>& certSet=aSignedData.Certificates();
sl@0
   732
		if (iCertsCount!=certSet.Count())
sl@0
   733
			{
sl@0
   734
			INFO_PRINTF1(_L("Number of Certificates is not as expected"));
sl@0
   735
			User::Leave(KErrGeneral);
sl@0
   736
			}
sl@0
   737
		else
sl@0
   738
			{
sl@0
   739
			//Signer Certificate is in the Signed data
sl@0
   740
			if (!iNoCertSet)
sl@0
   741
				{
sl@0
   742
				TInt count = iCertificates.Count();
sl@0
   743
				for (TInt i=0;i<count;i++)
sl@0
   744
					{
sl@0
   745
					if (certSet[i]->CertificateType()==CCmsCertificateChoice::ECertificateX509 && !iCertificates[i]->IsEqualL(certSet[i]->Certificate()))
sl@0
   746
						{
sl@0
   747
						INFO_PRINTF2(_L("X509 Certificates %d is not as expected"), i);
sl@0
   748
						User::Leave(KErrGeneral);						
sl@0
   749
						}
sl@0
   750
					}
sl@0
   751
				}
sl@0
   752
			
sl@0
   753
			if (iAdditionalCertificate || iAdditionalEncodedCertificate)
sl@0
   754
				{
sl@0
   755
				if (certSet[iCertsCount-1]->CertificateType()==CCmsCertificateChoice::ECertificateAttribute && 
sl@0
   756
						certSet[iCertsCount-1]->AttributeCertificate()->Compare(*iAdditionalEncodedCertificate)!=0)
sl@0
   757
					{
sl@0
   758
					INFO_PRINTF1(_L("Additional Attribute Certificates is not as expected"));
sl@0
   759
					User::Leave(KErrGeneral);						
sl@0
   760
					}
sl@0
   761
				else if (certSet[iCertsCount-1]->CertificateType()==CCmsCertificateChoice::ECertificateX509)
sl@0
   762
						{
sl@0
   763
						if (iAdditionalCertificate && !certSet[iCertsCount-1]->Certificate().IsEqualL(*iAdditionalCertificate))
sl@0
   764
							{
sl@0
   765
							INFO_PRINTF1(_L("Additional X509 Certificates is not as expected"));
sl@0
   766
							User::Leave(KErrGeneral);																					
sl@0
   767
							}
sl@0
   768
						else
sl@0
   769
							{
sl@0
   770
							if (iAdditionalEncodedCertificate)
sl@0
   771
								{
sl@0
   772
								CX509Certificate* addX509Cert=CX509Certificate::NewLC(*iAdditionalEncodedCertificate);
sl@0
   773
								if (!certSet[iCertsCount-1]->Certificate().IsEqualL(*addX509Cert))
sl@0
   774
									{
sl@0
   775
									INFO_PRINTF1(_L("Additional X509 Certificates is not as expected"));
sl@0
   776
									User::Leave(KErrGeneral);										
sl@0
   777
									}
sl@0
   778
								CleanupStack::PopAndDestroy(addX509Cert);
sl@0
   779
								}
sl@0
   780
							}
sl@0
   781
						}
sl@0
   782
				}
sl@0
   783
			}
sl@0
   784
		}
sl@0
   785
	}
sl@0
   786
sl@0
   787
void CTCmsSignedDataStep::CheckSignerInfoFieldsL(const RPointerArray<CCmsSignerInfo>& signerInfos)
sl@0
   788
	{
sl@0
   789
	TInt count=signerInfos.Count();
sl@0
   790
	if (iDecPKCS8Data.Count()!=count && iSignerCount!=count)
sl@0
   791
		{
sl@0
   792
		INFO_PRINTF1(_L("Number of Signer Info is not as expected"));
sl@0
   793
		User::Leave(KErrGeneral);
sl@0
   794
		}
sl@0
   795
	for (TInt i=0;i<count;i++)
sl@0
   796
		{
sl@0
   797
		if (signerInfos[i]->IsSignedAttributesPresent()!=iSignedAttributePresent[i]
sl@0
   798
			|| signerInfos[i]->IsUnsignedAttributesPresent()!=iUnSignedAttributePresent[i]
sl@0
   799
			|| signerInfos[i]->Version()!=iSignerInfoVersion[i])
sl@0
   800
			{
sl@0
   801
			INFO_PRINTF1(_L("Signed or Unsigned Attribute presence or Signer Version is not as expected"));
sl@0
   802
			User::Leave(KErrGeneral);
sl@0
   803
			}
sl@0
   804
			
sl@0
   805
		const CX509AlgorithmIdentifier& digestId=signerInfos[i]->DigestAlgorithm();
sl@0
   806
		if (digestId.Algorithm()!=(TAlgorithmId)iHashName[i])
sl@0
   807
			{
sl@0
   808
			INFO_PRINTF1(_L("Digest Algorithm ID is not as expected"));
sl@0
   809
			User::Leave(KErrGeneral);
sl@0
   810
			}
sl@0
   811
			
sl@0
   812
		const CX509AlgorithmIdentifier& sigId=signerInfos[i]->SignatureAlgorithm();
sl@0
   813
		if (iDecPKCS8Data[i]->Algorithm()!=sigId.Algorithm())
sl@0
   814
			{
sl@0
   815
			INFO_PRINTF1(_L("Signature Algorithm ID is not as expected"));
sl@0
   816
			User::Leave(KErrGeneral);
sl@0
   817
			}
sl@0
   818
			
sl@0
   819
		const CCmsSignerIdentifier& signerId=signerInfos[i]->SignerIdentifier();
sl@0
   820
		if (signerId.SignerIdentifierType()==CCmsSignerIdentifier::EIssuerAndSerialNumber)
sl@0
   821
			{
sl@0
   822
			if (!iCertificates[i]->IssuerName().ExactMatchL(signerId.IssuerAndSerialNumber()->IssuerName()))
sl@0
   823
				{
sl@0
   824
				INFO_PRINTF1(_L("Issuer name is not as expected"));
sl@0
   825
				User::Leave(KErrGeneral);
sl@0
   826
				}
sl@0
   827
			else
sl@0
   828
				{
sl@0
   829
				RInteger sn1=RInteger::NewL(iCertificates[i]->SerialNumber());
sl@0
   830
				CleanupClosePushL(sn1);
sl@0
   831
				RInteger sn2=RInteger::NewL(signerId.IssuerAndSerialNumber()->SerialNumber());
sl@0
   832
				CleanupClosePushL(sn2);
sl@0
   833
				if (sn1!=sn2)
sl@0
   834
					{
sl@0
   835
					INFO_PRINTF1(_L("Serial number is not as expected"));
sl@0
   836
					User::Leave(KErrGeneral);					
sl@0
   837
					}
sl@0
   838
				CleanupStack::PopAndDestroy(2, &sn1);//sn2, sn1
sl@0
   839
					
sl@0
   840
				}
sl@0
   841
			}
sl@0
   842
		else if (signerId.SignerIdentifierType()==CCmsSignerIdentifier::ESubjectKeyIdentifier)
sl@0
   843
				{
sl@0
   844
				const CX509CertExtension* certExt = iCertificates[i]->Extension(KSubjectKeyId);
sl@0
   845
				if (certExt)
sl@0
   846
					{
sl@0
   847
					CX509SubjectKeyIdExt* ext=CX509SubjectKeyIdExt::NewLC(certExt->Data());
sl@0
   848
					if (signerId.SubjectKeyIdentifier().Compare(ext->KeyId())!=0)
sl@0
   849
						{
sl@0
   850
						INFO_PRINTF1(_L("Subject Key Id is not as expected"));
sl@0
   851
						User::Leave(KErrGeneral);
sl@0
   852
						}
sl@0
   853
					CleanupStack::PopAndDestroy(ext);
sl@0
   854
					}
sl@0
   855
				}
sl@0
   856
		}
sl@0
   857
	}
sl@0
   858
sl@0
   859
void CTCmsSignedDataStep::CheckSignedDataFieldsL(const CCmsSignedObject& aSignedData)
sl@0
   860
	{		
sl@0
   861
	if (aSignedData.IsCertificateSetPresent()!=iCertificateSetPresent ||
sl@0
   862
		aSignedData.IsCertificateRevocationListsPresent()!=iCRLsSetPresent ||
sl@0
   863
		aSignedData.Version()!=iSignedDataVersion)
sl@0
   864
		{
sl@0
   865
		INFO_PRINTF1(_L("cert present or CRL present or version not as expected"));
sl@0
   866
		User::Leave(KErrGeneral);
sl@0
   867
		}
sl@0
   868
	else
sl@0
   869
		{
sl@0
   870
		const CEncapsulatedContentInfo& encapContentInfo=aSignedData.ContentInfo();
sl@0
   871
		CheckEncapsulatedContentFieldsL(encapContentInfo);
sl@0
   872
		const RPointerArray<CX509AlgorithmIdentifier>& algorithms=aSignedData.DigestAlgorithms();
sl@0
   873
		CheckAlgorithmSetFieldsL(algorithms);
sl@0
   874
		CheckCertificateSetFieldsL(aSignedData);
sl@0
   875
		}
sl@0
   876
	}
sl@0
   877
sl@0
   878
void CTCmsSignedDataStep::doTestL()
sl@0
   879
	{
sl@0
   880
	__UHEAP_MARK;
sl@0
   881
	
sl@0
   882
	CCmsSignedObject* signedData(NULL);
sl@0
   883
	TInt count=iDecPKCS8Data.Count();
sl@0
   884
	
sl@0
   885
	//Create Signed Object
sl@0
   886
	for (TInt i=0;i<count;i++)
sl@0
   887
		{
sl@0
   888
		//Get the key pair
sl@0
   889
		CDecPKCS8Data* decPKCS8Data=iDecPKCS8Data[i];
sl@0
   890
		MPKCS8DecodedKeyPairData* keyPair = decPKCS8Data->KeyPairData();
sl@0
   891
		
sl@0
   892
		CMessageDigest* hash(NULL);
sl@0
   893
		TPtrC8 hashValue;
sl@0
   894
		if (iIsHashAvailable)
sl@0
   895
			{
sl@0
   896
			hash=CreateHashLC((TAlgorithmId)iHashName[i]);
sl@0
   897
			hashValue.Set(hash->Hash(iDataContent->Des()));
sl@0
   898
			}
sl@0
   899
		
sl@0
   900
		//If it is the first time, a signed object needs to be created
sl@0
   901
		if (i==0)
sl@0
   902
			{
sl@0
   903
			if (iIsHashAvailable)
sl@0
   904
				{
sl@0
   905
				if (decPKCS8Data->Algorithm()==ERSA)
sl@0
   906
					{
sl@0
   907
					const CRSAPrivateKey& RSAPrivateKey=static_cast<CPKCS8KeyPairRSA*>(keyPair)->PrivateKey();
sl@0
   908
					if (!iTwoStepCreation)
sl@0
   909
						{
sl@0
   910
						signedData=CCmsSignedObject::NewL((TCmsContentInfoType)iContentType,
sl@0
   911
															hashValue,
sl@0
   912
															(TAlgorithmId)iHashName[i],
sl@0
   913
															RSAPrivateKey,
sl@0
   914
															*iCertificates[i],
sl@0
   915
															!iNoCertSet);
sl@0
   916
						CleanupStack::PushL(signedData);																
sl@0
   917
						}
sl@0
   918
					else
sl@0
   919
						{
sl@0
   920
						signedData=CCmsSignedObject::NewL((TCmsContentInfoType)iContentType, iIsDetached, iDataContent->Des());
sl@0
   921
						CleanupStack::PushL(signedData);
sl@0
   922
						signedData->SignL(hashValue, (TAlgorithmId)iHashName[i], RSAPrivateKey, *iCertificates[i], !iNoCertSet);
sl@0
   923
						}
sl@0
   924
					}
sl@0
   925
				else
sl@0
   926
					{
sl@0
   927
					const CDSAPrivateKey& DSAPrivateKey=static_cast<CPKCS8KeyPairDSA*>(keyPair)->PrivateKey();
sl@0
   928
					if (!iTwoStepCreation)
sl@0
   929
						{
sl@0
   930
						signedData=CCmsSignedObject::NewL((TCmsContentInfoType)iContentType,
sl@0
   931
															hashValue,
sl@0
   932
															(TAlgorithmId)iHashName[i],
sl@0
   933
															DSAPrivateKey,
sl@0
   934
															*iCertificates[i],
sl@0
   935
															!iNoCertSet);
sl@0
   936
						CleanupStack::PushL(signedData);																	
sl@0
   937
						}
sl@0
   938
					else
sl@0
   939
						{
sl@0
   940
						signedData=CCmsSignedObject::NewL((TCmsContentInfoType)iContentType, iIsDetached, iDataContent->Des());
sl@0
   941
						CleanupStack::PushL(signedData);
sl@0
   942
						signedData->SignL(hashValue, (TAlgorithmId)iHashName[i], DSAPrivateKey, *iCertificates[i], !iNoCertSet);
sl@0
   943
						}
sl@0
   944
					iRsaAlgorithm=EFalse;			
sl@0
   945
					}
sl@0
   946
				CleanupStack::Pop(signedData);	
sl@0
   947
				CleanupStack::PopAndDestroy(hash);
sl@0
   948
				CleanupStack::PushL(signedData);
sl@0
   949
				}		
sl@0
   950
			else
sl@0
   951
				{
sl@0
   952
				signedData=CCmsSignedObject::NewL((TCmsContentInfoType)iContentType, iIsDetached, iDataContent->Des());
sl@0
   953
				CleanupStack::PushL(signedData);
sl@0
   954
				if (!iNoSigning)
sl@0
   955
					{
sl@0
   956
					if (decPKCS8Data->Algorithm()==ERSA)
sl@0
   957
						{
sl@0
   958
						const CRSAPrivateKey& RSAPrivateKey=static_cast<CPKCS8KeyPairRSA*>(keyPair)->PrivateKey();
sl@0
   959
						signedData->SignL(KNullDesC8, (TAlgorithmId)iHashName[i], RSAPrivateKey, *iCertificates[i], !iNoCertSet);
sl@0
   960
						}
sl@0
   961
					else
sl@0
   962
						{
sl@0
   963
						const CDSAPrivateKey& DSAPrivateKey=static_cast<CPKCS8KeyPairDSA*>(keyPair)->PrivateKey();
sl@0
   964
						signedData->SignL(KNullDesC8, (TAlgorithmId)iHashName[i], DSAPrivateKey, *iCertificates[i], !iNoCertSet);
sl@0
   965
						iRsaAlgorithm=EFalse;
sl@0
   966
						}												
sl@0
   967
					}
sl@0
   968
				}
sl@0
   969
			}
sl@0
   970
		else
sl@0
   971
			{
sl@0
   972
			//multiple signatures
sl@0
   973
			if (iIsHashAvailable)
sl@0
   974
				{
sl@0
   975
				if (decPKCS8Data->Algorithm()==ERSA)
sl@0
   976
					{
sl@0
   977
					const CRSAPrivateKey& RSAPrivateKey=static_cast<CPKCS8KeyPairRSA*>(keyPair)->PrivateKey();
sl@0
   978
					signedData->SignL(hashValue, (TAlgorithmId)iHashName[i], RSAPrivateKey, *iCertificates[i], !iNoCertSet);
sl@0
   979
					}
sl@0
   980
				else
sl@0
   981
					{
sl@0
   982
					const CDSAPrivateKey& DSAPrivateKey=static_cast<CPKCS8KeyPairDSA*>(keyPair)->PrivateKey();
sl@0
   983
					signedData->SignL(hashValue, (TAlgorithmId)iHashName[i], DSAPrivateKey, *iCertificates[i], !iNoCertSet);
sl@0
   984
					iRsaAlgorithm=EFalse;
sl@0
   985
					}
sl@0
   986
				CleanupStack::PopAndDestroy(hash);	
sl@0
   987
				}
sl@0
   988
			else
sl@0
   989
				{
sl@0
   990
				if (decPKCS8Data->Algorithm()==ERSA)
sl@0
   991
					{
sl@0
   992
					const CRSAPrivateKey& RSAPrivateKey=static_cast<CPKCS8KeyPairRSA*>(keyPair)->PrivateKey();
sl@0
   993
					signedData->SignL(KNullDesC8, (TAlgorithmId)iHashName[i], RSAPrivateKey, *iCertificates[i], !iNoCertSet);
sl@0
   994
					}
sl@0
   995
				else
sl@0
   996
					{
sl@0
   997
					const CDSAPrivateKey& DSAPrivateKey=static_cast<CPKCS8KeyPairDSA*>(keyPair)->PrivateKey();
sl@0
   998
					signedData->SignL(KNullDesC8, (TAlgorithmId)iHashName[i], DSAPrivateKey, *iCertificates[i], !iNoCertSet);
sl@0
   999
					iRsaAlgorithm=EFalse;
sl@0
  1000
					}						
sl@0
  1001
				}
sl@0
  1002
			}	
sl@0
  1003
		}
sl@0
  1004
	
sl@0
  1005
	if (iAdditionalCertificate)
sl@0
  1006
		{
sl@0
  1007
		signedData->AddCertificateL(*iAdditionalCertificate);	
sl@0
  1008
		}
sl@0
  1009
sl@0
  1010
 	if (iAdditionalEncodedCertificate)
sl@0
  1011
 		{
sl@0
  1012
 		signedData->AddCertificateL(*iAdditionalEncodedCertificate, (CCmsCertificateChoice::TCertificateType)iAdditionalEncodedCertificateType);
sl@0
  1013
 		}
sl@0
  1014
		
sl@0
  1015
	//Encoding the Signed object
sl@0
  1016
	CASN1EncSequence* signedObjectSeq=signedData->EncodeASN1DERLC();
sl@0
  1017
	HBufC8* buf=CreateDEREncodingLC(*signedObjectSeq);
sl@0
  1018
sl@0
  1019
	//Encoding the wrapper Content Info
sl@0
  1020
	CCmsContentInfo* content=CCmsContentInfo::NewL(EContentTypeSignedData, *buf);
sl@0
  1021
	CleanupStack::PushL(content);
sl@0
  1022
	CASN1EncSequence* contentSeq=content->EncodeASN1DERLC();
sl@0
  1023
	HBufC8* signature=CreateDEREncodingLC(*contentSeq);
sl@0
  1024
	CleanupStack::Pop(signature);
sl@0
  1025
	CleanupStack::PopAndDestroy(5, signedData);	//contentSeq,content,buf,signedObjectSeq,signedData
sl@0
  1026
	CleanupStack::PushL(signature);
sl@0
  1027
sl@0
  1028
sl@0
  1029
	//write the result to a file, for initial debuging
sl@0
  1030
	//OutputResultToFileL(signature->Des());
sl@0
  1031
	
sl@0
  1032
	//Compare the result with the expected result, if the signature algorithms are RSA
sl@0
  1033
		
sl@0
  1034
	if (iRsaAlgorithm)
sl@0
  1035
		{
sl@0
  1036
		
sl@0
  1037
		//Check if the signature is the same as expected
sl@0
  1038
		TBool r=signature->Compare(*iExpectedEncoding);
sl@0
  1039
		if (r!=0 && !iIsOOMTest)
sl@0
  1040
			{
sl@0
  1041
			INFO_PRINTF1(_L("RSA Signature Encoding Error"));
sl@0
  1042
			User::Leave(KErrGeneral);
sl@0
  1043
			}
sl@0
  1044
		else
sl@0
  1045
			{
sl@0
  1046
			CheckAndValidateSignedDataL(*iExpectedEncoding);
sl@0
  1047
			}
sl@0
  1048
		}
sl@0
  1049
	else	
sl@0
  1050
		{
sl@0
  1051
		CheckAndValidateSignedDataL(*iExpectedEncoding);
sl@0
  1052
		CheckAndValidateSignedDataL(*signature);		
sl@0
  1053
		}	
sl@0
  1054
			
sl@0
  1055
	CleanupStack::PopAndDestroy(signature);
sl@0
  1056
	__UHEAP_MARKEND;
sl@0
  1057
	}
sl@0
  1058
sl@0
  1059
//
sl@0
  1060
// Implementation of CMS Signed Data Decoding Test Step
sl@0
  1061
//
sl@0
  1062
sl@0
  1063
CTCmsSignedDataDecodingStep::CTCmsSignedDataDecodingStep()
sl@0
  1064
	{
sl@0
  1065
	}
sl@0
  1066
sl@0
  1067
CTCmsSignedDataDecodingStep::~CTCmsSignedDataDecodingStep()
sl@0
  1068
	{
sl@0
  1069
	}
sl@0
  1070
sl@0
  1071
void CTCmsSignedDataDecodingStep::doTestL()
sl@0
  1072
	{
sl@0
  1073
	__UHEAP_MARK;
sl@0
  1074
	CheckAndValidateSignedDataL(*iExpectedEncoding);
sl@0
  1075
	__UHEAP_MARKEND;
sl@0
  1076
	}
sl@0
  1077
sl@0
  1078
//
sl@0
  1079
// Implementation of CMS Content Info Test step
sl@0
  1080
//
sl@0
  1081
CTCmsContentInfoDecodingStep::CTCmsContentInfoDecodingStep()
sl@0
  1082
	{	
sl@0
  1083
	}
sl@0
  1084
sl@0
  1085
CTCmsContentInfoDecodingStep::~CTCmsContentInfoDecodingStep()
sl@0
  1086
	{	
sl@0
  1087
	}
sl@0
  1088
	
sl@0
  1089
void CTCmsContentInfoDecodingStep::doTestL()
sl@0
  1090
	{
sl@0
  1091
	INFO_PRINTF1(_L("Start CMS Data Type Decoding"));
sl@0
  1092
	CCmsContentInfo* content=CCmsContentInfo::NewL(*iExpectedEncoding);
sl@0
  1093
	CleanupStack::PushL(content);
sl@0
  1094
	if (content->ContentType()!=iExpectedDataType)
sl@0
  1095
		{
sl@0
  1096
		INFO_PRINTF1(_L("CMS Data Type is not as expected"));
sl@0
  1097
		User::Leave(KErrGeneral);
sl@0
  1098
		}
sl@0
  1099
	CleanupStack::PopAndDestroy(content);		
sl@0
  1100
	}
sl@0
  1101
sl@0
  1102
sl@0
  1103
sl@0
  1104
sl@0
  1105
sl@0
  1106
sl@0
  1107
sl@0
  1108
sl@0
  1109