os/security/cryptoservices/certificateandkeymgmt/tpkcs7/tcmsstep.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/tpkcs7/tcmsstep.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,1109 @@
     1.4 +/*
     1.5 +* Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +
    1.23 +#include "tcmsstep.h"
    1.24 +#include <test/testexecutelog.h>
    1.25 +#include <asnpkcs.h>
    1.26 +#include "pkcs7signedobject.h"
    1.27 +#include <asn1enc.h>
    1.28 +#include <cmssignedobject.h>
    1.29 +#include <cmsdefs.h>
    1.30 +#include <hash.h>
    1.31 +#include <asymmetric.h>
    1.32 +#include <cmssigneridentifier.h>
    1.33 +#include <x509certext.h>
    1.34 +#include <pkixcertchain.h>
    1.35 +#include <cmscontentinfo.h>
    1.36 +#include "validate.h"
    1.37 +
    1.38 +
    1.39 +CTCmsBaseStep::CTCmsBaseStep()
    1.40 +	{
    1.41 +	}
    1.42 +
    1.43 +CTCmsBaseStep::~CTCmsBaseStep()
    1.44 +	{
    1.45 +	iFs.Close ();
    1.46 +	delete iDataContent;
    1.47 +	delete iExpectedEncoding;
    1.48 +	__UHEAP_MARKEND;
    1.49 +	}
    1.50 +
    1.51 +TVerdict CTCmsBaseStep::doTestStepPreambleL()
    1.52 +	{
    1.53 +	__UHEAP_MARK;	
    1.54 +	User::LeaveIfError (iFs.Connect());	
    1.55 +	
    1.56 +	//Read the data to be signed
    1.57 +	iDataContent = readFileL(_L("Data"));
    1.58 +	
    1.59 +	if (iDataContent == NULL)
    1.60 +		{
    1.61 +		iDataContent=KNullDesC8().AllocL();
    1.62 +		}
    1.63 +	
    1.64 +	//Read the expected data type
    1.65 +	TPtrC contentDataType;
    1.66 +	if (GetStringFromConfig(ConfigSection(), _L("ExpectedDataType"), contentDataType))
    1.67 +		{
    1.68 +		iExpectedDataType=CovertContentDataTypeNameToDataType(contentDataType);			
    1.69 +		}
    1.70 +
    1.71 +	//Read the expected result
    1.72 +	iExpectedEncoding = readFileL(_L("Result"));
    1.73 +	if (!iExpectedEncoding)
    1.74 +		{
    1.75 +		INFO_PRINTF1(_L("Failed to read 'Result' section of script"));
    1.76 +		SetTestStepResult(ETestSuiteError);			
    1.77 +		}
    1.78 +
    1.79 +	GetIntFromConfig(ConfigSection(), _L("ExpectedResult"), iExpectedResult);	
    1.80 +	GetBoolFromConfig(ConfigSection(), _L("IsOOMTest"), iIsOOMTest);
    1.81 +	return TestStepResult();	
    1.82 +	}
    1.83 +
    1.84 +TInt CTCmsBaseStep::CovertContentDataTypeNameToDataType(const TDesC& aDataTypeName)
    1.85 +	{
    1.86 +	if (aDataTypeName.Compare(_L("DATA"))==0)
    1.87 +		{
    1.88 +		return EContentTypeData;	
    1.89 +		}
    1.90 +	else	if (aDataTypeName.Compare(_L("SIGNEDDATA"))==0)
    1.91 +				{
    1.92 +				return EContentTypeSignedData;
    1.93 +				}
    1.94 +		 else	if (aDataTypeName.Compare(_L("ENVELOPEDDATA"))==0)
    1.95 +					 {
    1.96 +					 return EContentTypeEnvelopedData;	
    1.97 +					 }
    1.98 +			  else	if (aDataTypeName.Compare(_L("DIGESTEDDATA"))==0)
    1.99 +						{
   1.100 +						return EContentTypeDigestedData;
   1.101 +						}
   1.102 +					else 	if (aDataTypeName.Compare(_L("ENCRYPTEDDATA"))==0)
   1.103 +								{
   1.104 +								return EContentTypeEncryptedData;
   1.105 +								}
   1.106 +							else 	if (aDataTypeName.Compare(_L("SIGNEDANDENVELOPEDDATA"))==0)
   1.107 +										{
   1.108 +										return CPKCS7ContentInfo::EContentTypeSignedAndEnvelopedData;	
   1.109 +										}
   1.110 +									else	if (aDataTypeName.Compare(_L("AUTHDATA"))==0)
   1.111 +												{
   1.112 +												return EContentTypeAuthenticatedData;	
   1.113 +												}
   1.114 +											else
   1.115 +												{
   1.116 +												User::Leave(KErrArgument);
   1.117 +												return EContentTypeData;
   1.118 +												}
   1.119 +	}
   1.120 +
   1.121 +HBufC8* CTCmsBaseStep::readFileL (TPtrC tag)
   1.122 +	{
   1.123 +	TPtrC fileName;
   1.124 +	if (GetStringFromConfig(ConfigSection(), tag, fileName) == EFalse)
   1.125 +		{
   1.126 +		return NULL;
   1.127 +		}
   1.128 +
   1.129 +	RFile file;
   1.130 +	if (file.Open(iFs, fileName, EFileRead) != KErrNone)
   1.131 +		{
   1.132 +		INFO_PRINTF2(_L("Cannot open file %S for reading"), &fileName);
   1.133 +		return NULL;
   1.134 +		}
   1.135 +	CleanupClosePushL(file);
   1.136 +	TInt fileSize = 0;
   1.137 +	User::LeaveIfError(file.Size(fileSize));
   1.138 +	HBufC8* result = HBufC8::NewMaxL(fileSize);
   1.139 +	TPtr8 rawDataPtr(result->Des());
   1.140 +	file.Read (rawDataPtr);
   1.141 +	CleanupStack::PopAndDestroy (&file);
   1.142 +	INFO_PRINTF3(_L("Read %d octets from %S"), result->Size(), &fileName);
   1.143 +	return result;
   1.144 +	}
   1.145 +
   1.146 +void CTCmsBaseStep::OutputResultToFileL(const TDesC8& aSignature)
   1.147 +	{
   1.148 +	TDriveUnit sysDrive (RFs::GetSystemDrive());
   1.149 +	TBuf<128> rName (sysDrive.Name());;
   1.150 +	rName.Append(_L("\\tpkcs7\\myresults\\"));
   1.151 +			
   1.152 +	TInt err=iFs.MkDir(rName);
   1.153 +	if (err!=KErrNone && err!=KErrAlreadyExists)
   1.154 +		{
   1.155 +		User::Leave(err);	
   1.156 +		}
   1.157 +				
   1.158 +	RFile file;
   1.159 +	CleanupClosePushL(file);
   1.160 +	
   1.161 +	_LIT(KExtension, ".der");
   1.162 +	rName.Append(ConfigSection());
   1.163 +	rName.Append(KExtension);
   1.164 +	rName.LowerCase();
   1.165 +	User::LeaveIfError(file.Replace(iFs, rName, EFileWrite | EFileStream));
   1.166 +	User::LeaveIfError(file.Write(aSignature));
   1.167 +	CleanupStack::PopAndDestroy(&file);
   1.168 +	}
   1.169 +
   1.170 +
   1.171 +HBufC8* CTCmsBaseStep::CreateDEREncodingLC(const CASN1EncBase& aEncoding)
   1.172 +	{	
   1.173 +	TUint len = aEncoding.LengthDER();
   1.174 +	HBufC8* buf = HBufC8::NewMaxLC(len);
   1.175 +	TUint pos = 0;
   1.176 +	TPtr8 bufptr(buf->Des());
   1.177 +	aEncoding.WriteDERL(bufptr, pos);
   1.178 +	return buf;
   1.179 +	}
   1.180 +
   1.181 +TVerdict CTCmsBaseStep::doTestStepL()
   1.182 +	{
   1.183 +	if (!iIsOOMTest)
   1.184 +		{
   1.185 +		TRAPD(err, doTestL();)
   1.186 +		if (err!=iExpectedResult)
   1.187 +			{
   1.188 +			SetTestStepResult(EFail);
   1.189 +			User::Leave(err);
   1.190 +			}
   1.191 +		return TestStepResult();
   1.192 +		}
   1.193 +	else
   1.194 +		{
   1.195 +		return doOOMTestL();	
   1.196 +		}	
   1.197 +	}
   1.198 +
   1.199 +TVerdict CTCmsBaseStep::doOOMTestL()
   1.200 +	{
   1.201 +	TVerdict verdict = EFail;
   1.202 + 	TInt countAfter = 0;
   1.203 +	TInt countBefore = 0;
   1.204 + 	for (TInt oomCount = 0; ; oomCount++)
   1.205 + 		{
   1.206 + 		__UHEAP_RESET;
   1.207 + 		__UHEAP_SETFAIL(RHeap::EDeterministic, oomCount);
   1.208 + 		countBefore = User::CountAllocCells();
   1.209 + 		TRAPD(error, doTestL());
   1.210 + 		countAfter = User::CountAllocCells();
   1.211 + 		__UHEAP_RESET;
   1.212 + 		if (error != KErrNoMemory)
   1.213 + 			{
   1.214 + 			verdict = EPass;
   1.215 + 			INFO_PRINTF2(_L("OOM Status %d"),error);
   1.216 +			INFO_PRINTF1(_L("Test outcome : Passed"));
   1.217 + 			break;
   1.218 + 			}
   1.219 + 		else
   1.220 + 			{
   1.221 + 			if (countBefore != countAfter)
   1.222 + 				{
   1.223 + 				INFO_PRINTF2(_L("OOM Status %d"),error);
   1.224 + 				INFO_PRINTF2(_L("OOM Failed at %d"), oomCount);
   1.225 + 				SetTestStepResult(EFail);
   1.226 + 				break;
   1.227 + 				}
   1.228 + 			}
   1.229 + 		INFO_PRINTF2(_L("OOM Failed Point status %d"), error);
   1.230 +		}
   1.231 +	INFO_PRINTF3(_L("Heap alloc count ok: %d final vs %d initial"), countAfter,countBefore);
   1.232 + 	SetTestStepResult(verdict);
   1.233 + 	if (verdict==EFail)
   1.234 +	 	{
   1.235 + 		User::Leave(KErrGeneral);	 		
   1.236 +	 	}	 	
   1.237 + 	return verdict;
   1.238 +	}
   1.239 +
   1.240 +
   1.241 +//
   1.242 +// Implementation of CMS Data Test Step
   1.243 +//
   1.244 +CTCmsDataStep::CTCmsDataStep()
   1.245 +	{
   1.246 +	}
   1.247 +
   1.248 +CTCmsDataStep::~CTCmsDataStep()
   1.249 +	{
   1.250 +	}
   1.251 +
   1.252 +	
   1.253 +void CTCmsDataStep::doTestL()
   1.254 +	{
   1.255 +	__UHEAP_MARK;
   1.256 +	CCmsContentInfo* content=CCmsContentInfo::NewL(EContentTypeData, *iDataContent);
   1.257 +	CleanupStack::PushL(content);
   1.258 +	CASN1EncSequence* contentSeq=content->EncodeASN1DERLC();
   1.259 +	HBufC8* signature=CreateDEREncodingLC(*contentSeq);
   1.260 +	CleanupStack::Pop(signature);
   1.261 +	CleanupStack::PopAndDestroy(2, content);
   1.262 +	CleanupStack::PushL(signature);
   1.263 +	//OutputResultToFileL(signature->Des());
   1.264 +
   1.265 +	TBool r=signature->Compare(*iExpectedEncoding);
   1.266 +	if (r!=0 && !iIsOOMTest)
   1.267 +		{
   1.268 +		INFO_PRINTF1(_L("CMS Data Type Encoding Error"));
   1.269 +		User::Leave(KErrGeneral);
   1.270 +		}
   1.271 +	else
   1.272 +		{
   1.273 +		DecodingAndCheckL(*iExpectedEncoding);
   1.274 +		}
   1.275 +
   1.276 +	CleanupStack::PopAndDestroy(signature);	
   1.277 +	}
   1.278 +void CTCmsDataStep::DecodingAndCheckL(TDesC8& aRawData)
   1.279 +	{
   1.280 +	INFO_PRINTF1(_L("Start CMS Data Type Decoding"));
   1.281 +	CCmsContentInfo* content=CCmsContentInfo::NewL(aRawData);
   1.282 +	CleanupStack::PushL(content);
   1.283 +	if (content->ContentType()!=EContentTypeData)
   1.284 +		{
   1.285 +		INFO_PRINTF1(_L("CMS Data Type is not as expected"));
   1.286 +		User::Leave(KErrGeneral);
   1.287 +		}
   1.288 +	else
   1.289 +		{
   1.290 +		if (content->ContentData()!=iDataContent->Des())
   1.291 +			{
   1.292 +			INFO_PRINTF1(_L("CMS Data Content is not as expected"));
   1.293 +			User::Leave(KErrGeneral);					
   1.294 +			}
   1.295 +		}
   1.296 +	CleanupStack::PopAndDestroy(content);		
   1.297 +	}
   1.298 +//
   1.299 +// Implementation of CMS Data Test Step
   1.300 +//
   1.301 +CTCmsDataDecodingStep::CTCmsDataDecodingStep()
   1.302 +	{
   1.303 +	}
   1.304 +
   1.305 +CTCmsDataDecodingStep::~CTCmsDataDecodingStep()
   1.306 +	{
   1.307 +	}
   1.308 +
   1.309 +void CTCmsDataDecodingStep::doTestL()
   1.310 +	{
   1.311 +	DecodingAndCheckL(*iExpectedEncoding);	
   1.312 +	}
   1.313 +
   1.314 +
   1.315 +//
   1.316 +// Implementation of Signed Data Test Base Step
   1.317 +//
   1.318 +CTSignedDataBaseStep::CTSignedDataBaseStep() : iContentType(EContentTypeData), iRsaAlgorithm(ETrue) 
   1.319 +	{
   1.320 +	}
   1.321 +
   1.322 +CTSignedDataBaseStep::~CTSignedDataBaseStep()
   1.323 +	{
   1.324 +	iDecPKCS8Data.ResetAndDestroy();
   1.325 +	iCertificates.ResetAndDestroy();
   1.326 +	iSignerInfoVersion.Close();
   1.327 +	iHashName.Close();
   1.328 +	iValidateResults.Close();
   1.329 +	iSignedAttributePresent.Close();
   1.330 +	iUnSignedAttributePresent.Close();
   1.331 +	delete iRootCertificate;
   1.332 +	delete iAdditionalCertificate;
   1.333 +	delete iAdditionalEncodedCertificate;
   1.334 +	}
   1.335 +
   1.336 +
   1.337 +TInt CTSignedDataBaseStep::CovertHashNameToAlgorithmId(const TDesC& aHashName)
   1.338 +	{
   1.339 +	if (aHashName.Compare(_L("SHA1"))==0)
   1.340 +		{
   1.341 +		return ESHA1;	
   1.342 +		}
   1.343 +	else if (aHashName.Compare(_L("MD5"))==0)
   1.344 +			{
   1.345 +			return EMD5;
   1.346 +			}
   1.347 +		 else
   1.348 +			 {
   1.349 +			 return EMD2;	
   1.350 +			 }
   1.351 +	}
   1.352 +
   1.353 +TInt CTSignedDataBaseStep::CovertCertificateNameToCertificateType(const TDesC& aCertificateName)
   1.354 +	{
   1.355 +	if (aCertificateName.Compare(_L("X509"))==0)
   1.356 +		{
   1.357 +		return CCmsCertificateChoice::ECertificateX509;	
   1.358 +		}
   1.359 +	else if (aCertificateName.Compare(_L("Attribute"))==0)
   1.360 +			{
   1.361 +			return CCmsCertificateChoice::ECertificateAttribute;
   1.362 +			}
   1.363 +		 else
   1.364 +			 {
   1.365 +			 return CCmsCertificateChoice::ECertificateExtendedCerificate;	
   1.366 +			 }
   1.367 +	}
   1.368 +
   1.369 +
   1.370 +TVerdict CTSignedDataBaseStep::doTestStepPreambleL()
   1.371 +	{
   1.372 +	if (CTCmsBaseStep::doTestStepPreambleL()==EFail)
   1.373 +		{
   1.374 +		SetTestStepResult(EFail);
   1.375 +		}
   1.376 +	else
   1.377 +		{
   1.378 +		//Read the configurations
   1.379 +		GetBoolFromConfig(ConfigSection(), _L("HashAvailable"), iIsHashAvailable);
   1.380 +		GetBoolFromConfig(ConfigSection(), _L("DataDetached"), iIsDetached);
   1.381 +		GetBoolFromConfig(ConfigSection(), _L("CertificateSetPresent"), iCertificateSetPresent);
   1.382 +		GetBoolFromConfig(ConfigSection(), _L("CRLsSetPresent"), iCRLsSetPresent);
   1.383 +		GetIntFromConfig(ConfigSection(), _L("SignedDataVersion"), iSignedDataVersion);
   1.384 +		GetIntFromConfig(ConfigSection(), _L("AlgorithmCount"), iAlgorithmCount);
   1.385 +		GetIntFromConfig(ConfigSection(), _L("CertsCount"), iCertsCount);
   1.386 +		GetIntFromConfig(ConfigSection(), _L("SignerCount"), iSignerCount);
   1.387 +		GetBoolFromConfig(ConfigSection(), _L("NoCertSet"), iNoCertSet);
   1.388 +		GetBoolFromConfig(ConfigSection(), _L("ValidateUsingUserCerts"), iValidateUsingUserCerts);
   1.389 +		GetBoolFromConfig(ConfigSection(), _L("NoSigning"), iNoSigning);
   1.390 +		GetBoolFromConfig(ConfigSection(), _L("NoValidationTest"), iNoValidationTest);
   1.391 +		GetBoolFromConfig(ConfigSection(), _L("TwoStepCreation"), iTwoStepCreation);
   1.392 +		GetBoolFromConfig(ConfigSection(), _L("ValidationDetachedWithoutInput"), iValidationDetachedWithoutInput);
   1.393 +		
   1.394 +
   1.395 +		HBufC8* certificate = readFileL(_L("RootCertificate"));
   1.396 +		if (certificate)
   1.397 +			{
   1.398 +			CleanupStack::PushL(certificate);
   1.399 +			iRootCertificate = CX509Certificate::NewL(*certificate);
   1.400 +			CleanupStack::PopAndDestroy (certificate);
   1.401 +			}
   1.402 +
   1.403 +		certificate = readFileL(_L("AddtionalCertificate"));
   1.404 +		if (certificate)
   1.405 +			{
   1.406 +			CleanupStack::PushL(certificate);
   1.407 +			iAdditionalCertificate = CX509Certificate::NewL(*certificate);
   1.408 +			CleanupStack::PopAndDestroy (certificate);			
   1.409 +			}
   1.410 +		TPtrC certTypeName;
   1.411 +		if (GetStringFromConfig(ConfigSection(), _L("AdditionalEncodedCertificateType"), certTypeName))
   1.412 +			{
   1.413 +			iAdditionalEncodedCertificateType=CovertCertificateNameToCertificateType(certTypeName);
   1.414 +			iAdditionalEncodedCertificate=readFileL (_L("AdditionalEncodedCertificate"));				
   1.415 +			}
   1.416 +					
   1.417 +		//Read the certificates, private keys and hash algorithm
   1.418 +		TInt index(0);
   1.419 +		
   1.420 +		TName fKeyName;
   1.421 +		fKeyName.Format(_L("PrivateKey_%d"), index);
   1.422 +		
   1.423 +		TName fCertName;
   1.424 +		fCertName.Format(_L("Certificate_%d"), index);
   1.425 +		
   1.426 +		TName fHashAlgorithmName;
   1.427 +		fHashAlgorithmName.Format(_L("HashAlgorithm_%d"), index);
   1.428 +
   1.429 +		TName fValidationResult;
   1.430 +		fValidationResult.Format(_L("ValidationResult_%d"), index);
   1.431 +		
   1.432 +		TName fSignedAttributePresent;
   1.433 +		fSignedAttributePresent.Format(_L("SignedAttributePresent_%d"), index);
   1.434 +		
   1.435 +		TName fUnSignedAttributePresent;
   1.436 +		fUnSignedAttributePresent.Format(_L("UnSignedAttributePresent_%d"), index);
   1.437 +
   1.438 +		TName fSignerInfoVersion;
   1.439 +		fSignerInfoVersion.Format(_L("SignerInfoVersion_%d"), index);
   1.440 +
   1.441 +		TPtrC hashName;
   1.442 +		TBool vResult(EFalse);
   1.443 +		TBool sAP(EFalse);
   1.444 +		TBool uSAP(EFalse);
   1.445 +		TInt signerInfoVersion;
   1.446 +		TPtrC keyName;
   1.447 +		TPtrC certName;
   1.448 +		
   1.449 +		while ( GetStringFromConfig(ConfigSection(), fKeyName, keyName)
   1.450 +				&& GetStringFromConfig(ConfigSection(), fCertName, certName)
   1.451 +				&& GetStringFromConfig(ConfigSection(), fHashAlgorithmName, hashName)
   1.452 +				&& GetBoolFromConfig(ConfigSection(), fValidationResult, vResult)
   1.453 +				&& GetBoolFromConfig(ConfigSection(), fSignedAttributePresent, sAP)
   1.454 +				&& GetBoolFromConfig(ConfigSection(), fUnSignedAttributePresent, uSAP)
   1.455 +				&& GetIntFromConfig(ConfigSection(), fSignerInfoVersion, signerInfoVersion) )
   1.456 +			{
   1.457 +			//Construct private keys
   1.458 +			HBufC8* privateKey(NULL);
   1.459 +			if ((privateKey=readFileL(fKeyName))!=NULL)
   1.460 +				{
   1.461 +				CleanupStack::PushL (privateKey);
   1.462 +				CDecPKCS8Data* pkcs8Data=TASN1DecPKCS8::DecodeDERL(privateKey->Des());
   1.463 +				CleanupStack::PushL (pkcs8Data);
   1.464 +				iDecPKCS8Data.AppendL(pkcs8Data);
   1.465 +				CleanupStack::Pop(pkcs8Data);
   1.466 +				CleanupStack::PopAndDestroy(privateKey);	
   1.467 +				}
   1.468 +			
   1.469 +			//Construct X509 certificate
   1.470 +			HBufC8* cert(NULL);
   1.471 +			if ((cert=readFileL(fCertName))!=NULL)
   1.472 +				{
   1.473 +				CleanupStack::PushL (cert);
   1.474 +				CX509Certificate* x509cert=CX509Certificate::NewLC(cert->Des());		
   1.475 +				iCertificates.AppendL(x509cert);
   1.476 +				CleanupStack::Pop(x509cert);
   1.477 +				CleanupStack::PopAndDestroy(cert);				
   1.478 +				}
   1.479 +				
   1.480 +			TInt hashId=CovertHashNameToAlgorithmId(hashName);		
   1.481 +			iHashName.AppendL(hashId);
   1.482 +					
   1.483 +			iValidateResults.AppendL(vResult);
   1.484 +			iSignedAttributePresent.AppendL(sAP);
   1.485 +			iUnSignedAttributePresent.AppendL(uSAP);
   1.486 +			iSignerInfoVersion.AppendL(signerInfoVersion);
   1.487 +			
   1.488 +			//for next pair
   1.489 +			index++;
   1.490 +			fKeyName.Format(_L("PrivateKey_%d"), index);
   1.491 +			fCertName.Format(_L("Certificate_%d"), index);
   1.492 +			fHashAlgorithmName.Format(_L("HashAlgorithm_%d"), index);
   1.493 +			fValidationResult.Format(_L("ValidationResult_%d"), index);
   1.494 +			fSignedAttributePresent.Format(_L("SignedAttributePresent_%d"), index);
   1.495 +			fUnSignedAttributePresent.Format(_L("UnSignedAttributePresent_%d"), index);
   1.496 +			fSignerInfoVersion.Format(_L("SignerInfoVersion_%d"), index);
   1.497 +			}			
   1.498 +		}
   1.499 +	return TestStepResult();
   1.500 +	}
   1.501 +
   1.502 +CMessageDigest* CTSignedDataBaseStep::CreateHashLC(TAlgorithmId aAlgorithmId)
   1.503 +	{
   1.504 +	CMessageDigest* hash(NULL);
   1.505 +	switch (aAlgorithmId)
   1.506 +		{
   1.507 +	case EMD2:
   1.508 +		hash=CMD2::NewL();
   1.509 +		break;
   1.510 +		
   1.511 +	case EMD5:
   1.512 +		hash=CMD5::NewL();
   1.513 +		break;
   1.514 +		
   1.515 +	case ESHA1:
   1.516 +		hash=CSHA1::NewL();
   1.517 +		break;
   1.518 +		
   1.519 +	default:
   1.520 +		User::Leave(KErrNotSupported);
   1.521 +		}
   1.522 +	CleanupStack::PushL(hash);
   1.523 +	return hash;		
   1.524 +	}
   1.525 +
   1.526 +
   1.527 +//
   1.528 +// Implementation of CMS Signed Data Test Step
   1.529 +//
   1.530 +CTCmsSignedDataStep::CTCmsSignedDataStep()
   1.531 +	{
   1.532 +	}
   1.533 +
   1.534 +CTCmsSignedDataStep::~CTCmsSignedDataStep()
   1.535 +	{
   1.536 +	}
   1.537 +
   1.538 +
   1.539 +void CTCmsSignedDataStep::CheckAndValidateSignedDataL(TDesC8& aRawData)
   1.540 +	{
   1.541 +	//Decode the content info encoding read from predefined file	
   1.542 +	CCmsContentInfo* content=CCmsContentInfo::NewL(aRawData);
   1.543 +	CleanupStack::PushL(content);
   1.544 +	if (content->ContentType()!=EContentTypeSignedData)
   1.545 +		{
   1.546 +		INFO_PRINTF1(_L("Content Type is not Signed Data"));
   1.547 +		User::Leave(KErrGeneral);
   1.548 +		}
   1.549 +	else
   1.550 +		{
   1.551 +		//Decode the signed data and check the fields
   1.552 +		CCmsSignedObject* signedData=CCmsSignedObject::NewL(*content);
   1.553 +		CleanupStack::PushL(signedData);	
   1.554 +		CheckSignedDataFieldsL(*signedData);
   1.555 +		
   1.556 +		//Validate the signatures
   1.557 +		const RPointerArray<CCmsSignerInfo>& signerInfos=signedData->SignerInfo();
   1.558 +		CheckSignerInfoFieldsL(signerInfos);
   1.559 +		
   1.560 +		if (!iNoValidationTest)
   1.561 +			{
   1.562 +			TInt count=signerInfos.Count();
   1.563 +			for (TInt i=0;i<count;i++)
   1.564 +				{
   1.565 +				HBufC8* certificateEncoding = NULL;
   1.566 +				
   1.567 +				CMessageDigest* hash(NULL);
   1.568 +				if (iIsHashAvailable)
   1.569 +					{
   1.570 +					hash=CreateHashLC((TAlgorithmId)iHashName[i]);
   1.571 +					}
   1.572 +					
   1.573 +				TBool isValid(EFalse);
   1.574 +				if (iValidateUsingUserCerts)
   1.575 +					{
   1.576 +					INFO_PRINTF1(_L("Test validation by using given certificates"));
   1.577 +					if (iIsHashAvailable)
   1.578 +						{
   1.579 +						isValid = signedData->ValidateSignerLC(*signerInfos[i], iCertificates, certificateEncoding, ETrue, hash->Hash(iDataContent->Des()));
   1.580 +						}
   1.581 +					else 
   1.582 +						{
   1.583 +						if (iIsDetached)
   1.584 +							{
   1.585 +							if (!iValidationDetachedWithoutInput)
   1.586 +								{
   1.587 +								isValid = signedData->ValidateSignerLC(*signerInfos[i], iCertificates, certificateEncoding, EFalse, iDataContent->Des());
   1.588 +								}
   1.589 +							else
   1.590 +								{
   1.591 +								isValid = signedData->ValidateSignerLC(*signerInfos[i], iCertificates, certificateEncoding);	
   1.592 +								}							
   1.593 +							}
   1.594 +						else
   1.595 +							{
   1.596 +							isValid = signedData->ValidateSignerLC(*signerInfos[i], iCertificates, certificateEncoding);	
   1.597 +							}
   1.598 +						}					
   1.599 +					}
   1.600 +				else
   1.601 +					{
   1.602 +					INFO_PRINTF1(_L("Test validation by using the embedded certificates"));
   1.603 +					
   1.604 +					if (iIsHashAvailable)
   1.605 +						{
   1.606 +						isValid = signedData->ValidateSignerLC(*signerInfos[i], certificateEncoding, ETrue, hash->Hash(iDataContent->Des()));
   1.607 +						}
   1.608 +					else 
   1.609 +						{
   1.610 +						if (iIsDetached)
   1.611 +							{
   1.612 +							if (!iValidationDetachedWithoutInput)
   1.613 +								{
   1.614 +								isValid = signedData->ValidateSignerLC(*signerInfos[i], certificateEncoding, EFalse, iDataContent->Des());	
   1.615 +								}
   1.616 +							else
   1.617 +								{
   1.618 +								isValid = signedData->ValidateSignerLC(*signerInfos[i], certificateEncoding);	
   1.619 +								}
   1.620 +							}
   1.621 +						else
   1.622 +							{
   1.623 +							isValid = signedData->ValidateSignerLC(*signerInfos[i], certificateEncoding);	
   1.624 +							}
   1.625 +						}					
   1.626 +					}
   1.627 +									
   1.628 +				if (!isValid)
   1.629 +					{
   1.630 +					INFO_PRINTF1(_L("Couldn't validate signer"));
   1.631 +					}
   1.632 +				else
   1.633 +					{
   1.634 +					CActiveScheduler* sched = NULL;
   1.635 +					if (CActiveScheduler::Current() == NULL)
   1.636 +						{
   1.637 +						INFO_PRINTF1(_L("Installing scheduler"));
   1.638 +						sched = new (ELeave) CActiveScheduler();
   1.639 +						CleanupStack::PushL (sched);
   1.640 +						CActiveScheduler::Install (sched);
   1.641 +						}
   1.642 +					RPointerArray<CX509Certificate> roots (&iRootCertificate, 1);
   1.643 +					CPKIXCertChain * chain = CPKIXCertChain::NewLC(iFs, *certificateEncoding, roots);
   1.644 +					
   1.645 +					TTime tm;
   1.646 +					_LIT(KDateCorrect1,"20061128:"); 
   1.647 +					TBuf <24> theDate(KDateCorrect1); 
   1.648 +					TInt err=tm.Set(theDate); 
   1.649 +					if(err)
   1.650 +						{
   1.651 +						tm.HomeTime();
   1.652 +						}
   1.653 +						
   1.654 +					CPKIXValidationResult* result = CPKIXValidationResult::NewLC();
   1.655 +					CTPKCS7Validator* validator = new (ELeave) CTPKCS7Validator (chain, result, &tm);
   1.656 +					validator->doValidate ();
   1.657 +					sched->Start ();
   1.658 +					if (result->Error().iReason == EValidatedOK)
   1.659 +						{
   1.660 +						isValid = ETrue;
   1.661 +						INFO_PRINTF1(_L("Validation success"));
   1.662 +						}
   1.663 +					else
   1.664 +						{
   1.665 +						isValid = EFalse;
   1.666 +						INFO_PRINTF2(_L("Validation failed: %d"), result->Error().iReason);
   1.667 +						}
   1.668 +	        		delete validator;
   1.669 +					CleanupStack::PopAndDestroy(result);
   1.670 +					CleanupStack::PopAndDestroy(chain);
   1.671 +					if (sched)
   1.672 +						{
   1.673 +						CActiveScheduler::Install (NULL);
   1.674 +						CleanupStack::PopAndDestroy (sched);
   1.675 +						}
   1.676 +					}
   1.677 +				
   1.678 +				CleanupStack::PopAndDestroy(certificateEncoding);				
   1.679 +				if (hash)
   1.680 +					{
   1.681 +					CleanupStack::PopAndDestroy(hash);	
   1.682 +					}				
   1.683 +				
   1.684 +				if (isValid!=iValidateResults[i])
   1.685 +					{
   1.686 +					INFO_PRINTF1(_L("validate result not as expected"));
   1.687 +					User::Leave(KErrGeneral);
   1.688 +					}
   1.689 +				}			
   1.690 +			}
   1.691 +		CleanupStack::PopAndDestroy(signedData);
   1.692 +		}
   1.693 +	CleanupStack::PopAndDestroy(content);						
   1.694 +	}
   1.695 +
   1.696 +void CTCmsSignedDataStep::CheckEncapsulatedContentFieldsL(const CEncapsulatedContentInfo& aEncapContentInfo)
   1.697 +	{
   1.698 +	if (aEncapContentInfo.ContentType()!=EContentTypeData)
   1.699 +		{
   1.700 +		INFO_PRINTF1(_L("Encapsulated data Content is not data content type"));
   1.701 +		User::Leave(KErrGeneral);
   1.702 +		}
   1.703 +	else
   1.704 +		{
   1.705 +		if (aEncapContentInfo.IsContentDataPresent() == iIsDetached)
   1.706 +			{
   1.707 +			INFO_PRINTF1(_L("Encapsulated data Content attachment not as expected"));
   1.708 +			User::Leave(KErrGeneral);
   1.709 +			}
   1.710 +		else
   1.711 +			{
   1.712 +			if (aEncapContentInfo.IsContentDataPresent() && aEncapContentInfo.ContentData()!=*iDataContent)
   1.713 +				{
   1.714 +				INFO_PRINTF1(_L("Encapsulated data Content not as expected"));
   1.715 +				User::Leave(KErrGeneral);
   1.716 +				}
   1.717 +			}
   1.718 +		}		
   1.719 +	}
   1.720 +
   1.721 +void CTCmsSignedDataStep::CheckAlgorithmSetFieldsL(const RPointerArray<CX509AlgorithmIdentifier>& aAlgorithms)
   1.722 +	{
   1.723 +	if (iAlgorithmCount!=aAlgorithms.Count())
   1.724 +		{
   1.725 +		INFO_PRINTF1(_L("Number of Algorithm ID is not as expected"));
   1.726 +		User::Leave(KErrGeneral);
   1.727 +		}
   1.728 +	}
   1.729 +
   1.730 +void CTCmsSignedDataStep::CheckCertificateSetFieldsL(const CCmsSignedObject& aSignedData)
   1.731 +	{
   1.732 +	if (aSignedData.IsCertificateSetPresent())
   1.733 +		{
   1.734 +		const RPointerArray<CCmsCertificateChoice>& certSet=aSignedData.Certificates();
   1.735 +		if (iCertsCount!=certSet.Count())
   1.736 +			{
   1.737 +			INFO_PRINTF1(_L("Number of Certificates is not as expected"));
   1.738 +			User::Leave(KErrGeneral);
   1.739 +			}
   1.740 +		else
   1.741 +			{
   1.742 +			//Signer Certificate is in the Signed data
   1.743 +			if (!iNoCertSet)
   1.744 +				{
   1.745 +				TInt count = iCertificates.Count();
   1.746 +				for (TInt i=0;i<count;i++)
   1.747 +					{
   1.748 +					if (certSet[i]->CertificateType()==CCmsCertificateChoice::ECertificateX509 && !iCertificates[i]->IsEqualL(certSet[i]->Certificate()))
   1.749 +						{
   1.750 +						INFO_PRINTF2(_L("X509 Certificates %d is not as expected"), i);
   1.751 +						User::Leave(KErrGeneral);						
   1.752 +						}
   1.753 +					}
   1.754 +				}
   1.755 +			
   1.756 +			if (iAdditionalCertificate || iAdditionalEncodedCertificate)
   1.757 +				{
   1.758 +				if (certSet[iCertsCount-1]->CertificateType()==CCmsCertificateChoice::ECertificateAttribute && 
   1.759 +						certSet[iCertsCount-1]->AttributeCertificate()->Compare(*iAdditionalEncodedCertificate)!=0)
   1.760 +					{
   1.761 +					INFO_PRINTF1(_L("Additional Attribute Certificates is not as expected"));
   1.762 +					User::Leave(KErrGeneral);						
   1.763 +					}
   1.764 +				else if (certSet[iCertsCount-1]->CertificateType()==CCmsCertificateChoice::ECertificateX509)
   1.765 +						{
   1.766 +						if (iAdditionalCertificate && !certSet[iCertsCount-1]->Certificate().IsEqualL(*iAdditionalCertificate))
   1.767 +							{
   1.768 +							INFO_PRINTF1(_L("Additional X509 Certificates is not as expected"));
   1.769 +							User::Leave(KErrGeneral);																					
   1.770 +							}
   1.771 +						else
   1.772 +							{
   1.773 +							if (iAdditionalEncodedCertificate)
   1.774 +								{
   1.775 +								CX509Certificate* addX509Cert=CX509Certificate::NewLC(*iAdditionalEncodedCertificate);
   1.776 +								if (!certSet[iCertsCount-1]->Certificate().IsEqualL(*addX509Cert))
   1.777 +									{
   1.778 +									INFO_PRINTF1(_L("Additional X509 Certificates is not as expected"));
   1.779 +									User::Leave(KErrGeneral);										
   1.780 +									}
   1.781 +								CleanupStack::PopAndDestroy(addX509Cert);
   1.782 +								}
   1.783 +							}
   1.784 +						}
   1.785 +				}
   1.786 +			}
   1.787 +		}
   1.788 +	}
   1.789 +
   1.790 +void CTCmsSignedDataStep::CheckSignerInfoFieldsL(const RPointerArray<CCmsSignerInfo>& signerInfos)
   1.791 +	{
   1.792 +	TInt count=signerInfos.Count();
   1.793 +	if (iDecPKCS8Data.Count()!=count && iSignerCount!=count)
   1.794 +		{
   1.795 +		INFO_PRINTF1(_L("Number of Signer Info is not as expected"));
   1.796 +		User::Leave(KErrGeneral);
   1.797 +		}
   1.798 +	for (TInt i=0;i<count;i++)
   1.799 +		{
   1.800 +		if (signerInfos[i]->IsSignedAttributesPresent()!=iSignedAttributePresent[i]
   1.801 +			|| signerInfos[i]->IsUnsignedAttributesPresent()!=iUnSignedAttributePresent[i]
   1.802 +			|| signerInfos[i]->Version()!=iSignerInfoVersion[i])
   1.803 +			{
   1.804 +			INFO_PRINTF1(_L("Signed or Unsigned Attribute presence or Signer Version is not as expected"));
   1.805 +			User::Leave(KErrGeneral);
   1.806 +			}
   1.807 +			
   1.808 +		const CX509AlgorithmIdentifier& digestId=signerInfos[i]->DigestAlgorithm();
   1.809 +		if (digestId.Algorithm()!=(TAlgorithmId)iHashName[i])
   1.810 +			{
   1.811 +			INFO_PRINTF1(_L("Digest Algorithm ID is not as expected"));
   1.812 +			User::Leave(KErrGeneral);
   1.813 +			}
   1.814 +			
   1.815 +		const CX509AlgorithmIdentifier& sigId=signerInfos[i]->SignatureAlgorithm();
   1.816 +		if (iDecPKCS8Data[i]->Algorithm()!=sigId.Algorithm())
   1.817 +			{
   1.818 +			INFO_PRINTF1(_L("Signature Algorithm ID is not as expected"));
   1.819 +			User::Leave(KErrGeneral);
   1.820 +			}
   1.821 +			
   1.822 +		const CCmsSignerIdentifier& signerId=signerInfos[i]->SignerIdentifier();
   1.823 +		if (signerId.SignerIdentifierType()==CCmsSignerIdentifier::EIssuerAndSerialNumber)
   1.824 +			{
   1.825 +			if (!iCertificates[i]->IssuerName().ExactMatchL(signerId.IssuerAndSerialNumber()->IssuerName()))
   1.826 +				{
   1.827 +				INFO_PRINTF1(_L("Issuer name is not as expected"));
   1.828 +				User::Leave(KErrGeneral);
   1.829 +				}
   1.830 +			else
   1.831 +				{
   1.832 +				RInteger sn1=RInteger::NewL(iCertificates[i]->SerialNumber());
   1.833 +				CleanupClosePushL(sn1);
   1.834 +				RInteger sn2=RInteger::NewL(signerId.IssuerAndSerialNumber()->SerialNumber());
   1.835 +				CleanupClosePushL(sn2);
   1.836 +				if (sn1!=sn2)
   1.837 +					{
   1.838 +					INFO_PRINTF1(_L("Serial number is not as expected"));
   1.839 +					User::Leave(KErrGeneral);					
   1.840 +					}
   1.841 +				CleanupStack::PopAndDestroy(2, &sn1);//sn2, sn1
   1.842 +					
   1.843 +				}
   1.844 +			}
   1.845 +		else if (signerId.SignerIdentifierType()==CCmsSignerIdentifier::ESubjectKeyIdentifier)
   1.846 +				{
   1.847 +				const CX509CertExtension* certExt = iCertificates[i]->Extension(KSubjectKeyId);
   1.848 +				if (certExt)
   1.849 +					{
   1.850 +					CX509SubjectKeyIdExt* ext=CX509SubjectKeyIdExt::NewLC(certExt->Data());
   1.851 +					if (signerId.SubjectKeyIdentifier().Compare(ext->KeyId())!=0)
   1.852 +						{
   1.853 +						INFO_PRINTF1(_L("Subject Key Id is not as expected"));
   1.854 +						User::Leave(KErrGeneral);
   1.855 +						}
   1.856 +					CleanupStack::PopAndDestroy(ext);
   1.857 +					}
   1.858 +				}
   1.859 +		}
   1.860 +	}
   1.861 +
   1.862 +void CTCmsSignedDataStep::CheckSignedDataFieldsL(const CCmsSignedObject& aSignedData)
   1.863 +	{		
   1.864 +	if (aSignedData.IsCertificateSetPresent()!=iCertificateSetPresent ||
   1.865 +		aSignedData.IsCertificateRevocationListsPresent()!=iCRLsSetPresent ||
   1.866 +		aSignedData.Version()!=iSignedDataVersion)
   1.867 +		{
   1.868 +		INFO_PRINTF1(_L("cert present or CRL present or version not as expected"));
   1.869 +		User::Leave(KErrGeneral);
   1.870 +		}
   1.871 +	else
   1.872 +		{
   1.873 +		const CEncapsulatedContentInfo& encapContentInfo=aSignedData.ContentInfo();
   1.874 +		CheckEncapsulatedContentFieldsL(encapContentInfo);
   1.875 +		const RPointerArray<CX509AlgorithmIdentifier>& algorithms=aSignedData.DigestAlgorithms();
   1.876 +		CheckAlgorithmSetFieldsL(algorithms);
   1.877 +		CheckCertificateSetFieldsL(aSignedData);
   1.878 +		}
   1.879 +	}
   1.880 +
   1.881 +void CTCmsSignedDataStep::doTestL()
   1.882 +	{
   1.883 +	__UHEAP_MARK;
   1.884 +	
   1.885 +	CCmsSignedObject* signedData(NULL);
   1.886 +	TInt count=iDecPKCS8Data.Count();
   1.887 +	
   1.888 +	//Create Signed Object
   1.889 +	for (TInt i=0;i<count;i++)
   1.890 +		{
   1.891 +		//Get the key pair
   1.892 +		CDecPKCS8Data* decPKCS8Data=iDecPKCS8Data[i];
   1.893 +		MPKCS8DecodedKeyPairData* keyPair = decPKCS8Data->KeyPairData();
   1.894 +		
   1.895 +		CMessageDigest* hash(NULL);
   1.896 +		TPtrC8 hashValue;
   1.897 +		if (iIsHashAvailable)
   1.898 +			{
   1.899 +			hash=CreateHashLC((TAlgorithmId)iHashName[i]);
   1.900 +			hashValue.Set(hash->Hash(iDataContent->Des()));
   1.901 +			}
   1.902 +		
   1.903 +		//If it is the first time, a signed object needs to be created
   1.904 +		if (i==0)
   1.905 +			{
   1.906 +			if (iIsHashAvailable)
   1.907 +				{
   1.908 +				if (decPKCS8Data->Algorithm()==ERSA)
   1.909 +					{
   1.910 +					const CRSAPrivateKey& RSAPrivateKey=static_cast<CPKCS8KeyPairRSA*>(keyPair)->PrivateKey();
   1.911 +					if (!iTwoStepCreation)
   1.912 +						{
   1.913 +						signedData=CCmsSignedObject::NewL((TCmsContentInfoType)iContentType,
   1.914 +															hashValue,
   1.915 +															(TAlgorithmId)iHashName[i],
   1.916 +															RSAPrivateKey,
   1.917 +															*iCertificates[i],
   1.918 +															!iNoCertSet);
   1.919 +						CleanupStack::PushL(signedData);																
   1.920 +						}
   1.921 +					else
   1.922 +						{
   1.923 +						signedData=CCmsSignedObject::NewL((TCmsContentInfoType)iContentType, iIsDetached, iDataContent->Des());
   1.924 +						CleanupStack::PushL(signedData);
   1.925 +						signedData->SignL(hashValue, (TAlgorithmId)iHashName[i], RSAPrivateKey, *iCertificates[i], !iNoCertSet);
   1.926 +						}
   1.927 +					}
   1.928 +				else
   1.929 +					{
   1.930 +					const CDSAPrivateKey& DSAPrivateKey=static_cast<CPKCS8KeyPairDSA*>(keyPair)->PrivateKey();
   1.931 +					if (!iTwoStepCreation)
   1.932 +						{
   1.933 +						signedData=CCmsSignedObject::NewL((TCmsContentInfoType)iContentType,
   1.934 +															hashValue,
   1.935 +															(TAlgorithmId)iHashName[i],
   1.936 +															DSAPrivateKey,
   1.937 +															*iCertificates[i],
   1.938 +															!iNoCertSet);
   1.939 +						CleanupStack::PushL(signedData);																	
   1.940 +						}
   1.941 +					else
   1.942 +						{
   1.943 +						signedData=CCmsSignedObject::NewL((TCmsContentInfoType)iContentType, iIsDetached, iDataContent->Des());
   1.944 +						CleanupStack::PushL(signedData);
   1.945 +						signedData->SignL(hashValue, (TAlgorithmId)iHashName[i], DSAPrivateKey, *iCertificates[i], !iNoCertSet);
   1.946 +						}
   1.947 +					iRsaAlgorithm=EFalse;			
   1.948 +					}
   1.949 +				CleanupStack::Pop(signedData);	
   1.950 +				CleanupStack::PopAndDestroy(hash);
   1.951 +				CleanupStack::PushL(signedData);
   1.952 +				}		
   1.953 +			else
   1.954 +				{
   1.955 +				signedData=CCmsSignedObject::NewL((TCmsContentInfoType)iContentType, iIsDetached, iDataContent->Des());
   1.956 +				CleanupStack::PushL(signedData);
   1.957 +				if (!iNoSigning)
   1.958 +					{
   1.959 +					if (decPKCS8Data->Algorithm()==ERSA)
   1.960 +						{
   1.961 +						const CRSAPrivateKey& RSAPrivateKey=static_cast<CPKCS8KeyPairRSA*>(keyPair)->PrivateKey();
   1.962 +						signedData->SignL(KNullDesC8, (TAlgorithmId)iHashName[i], RSAPrivateKey, *iCertificates[i], !iNoCertSet);
   1.963 +						}
   1.964 +					else
   1.965 +						{
   1.966 +						const CDSAPrivateKey& DSAPrivateKey=static_cast<CPKCS8KeyPairDSA*>(keyPair)->PrivateKey();
   1.967 +						signedData->SignL(KNullDesC8, (TAlgorithmId)iHashName[i], DSAPrivateKey, *iCertificates[i], !iNoCertSet);
   1.968 +						iRsaAlgorithm=EFalse;
   1.969 +						}												
   1.970 +					}
   1.971 +				}
   1.972 +			}
   1.973 +		else
   1.974 +			{
   1.975 +			//multiple signatures
   1.976 +			if (iIsHashAvailable)
   1.977 +				{
   1.978 +				if (decPKCS8Data->Algorithm()==ERSA)
   1.979 +					{
   1.980 +					const CRSAPrivateKey& RSAPrivateKey=static_cast<CPKCS8KeyPairRSA*>(keyPair)->PrivateKey();
   1.981 +					signedData->SignL(hashValue, (TAlgorithmId)iHashName[i], RSAPrivateKey, *iCertificates[i], !iNoCertSet);
   1.982 +					}
   1.983 +				else
   1.984 +					{
   1.985 +					const CDSAPrivateKey& DSAPrivateKey=static_cast<CPKCS8KeyPairDSA*>(keyPair)->PrivateKey();
   1.986 +					signedData->SignL(hashValue, (TAlgorithmId)iHashName[i], DSAPrivateKey, *iCertificates[i], !iNoCertSet);
   1.987 +					iRsaAlgorithm=EFalse;
   1.988 +					}
   1.989 +				CleanupStack::PopAndDestroy(hash);	
   1.990 +				}
   1.991 +			else
   1.992 +				{
   1.993 +				if (decPKCS8Data->Algorithm()==ERSA)
   1.994 +					{
   1.995 +					const CRSAPrivateKey& RSAPrivateKey=static_cast<CPKCS8KeyPairRSA*>(keyPair)->PrivateKey();
   1.996 +					signedData->SignL(KNullDesC8, (TAlgorithmId)iHashName[i], RSAPrivateKey, *iCertificates[i], !iNoCertSet);
   1.997 +					}
   1.998 +				else
   1.999 +					{
  1.1000 +					const CDSAPrivateKey& DSAPrivateKey=static_cast<CPKCS8KeyPairDSA*>(keyPair)->PrivateKey();
  1.1001 +					signedData->SignL(KNullDesC8, (TAlgorithmId)iHashName[i], DSAPrivateKey, *iCertificates[i], !iNoCertSet);
  1.1002 +					iRsaAlgorithm=EFalse;
  1.1003 +					}						
  1.1004 +				}
  1.1005 +			}	
  1.1006 +		}
  1.1007 +	
  1.1008 +	if (iAdditionalCertificate)
  1.1009 +		{
  1.1010 +		signedData->AddCertificateL(*iAdditionalCertificate);	
  1.1011 +		}
  1.1012 +
  1.1013 + 	if (iAdditionalEncodedCertificate)
  1.1014 + 		{
  1.1015 + 		signedData->AddCertificateL(*iAdditionalEncodedCertificate, (CCmsCertificateChoice::TCertificateType)iAdditionalEncodedCertificateType);
  1.1016 + 		}
  1.1017 +		
  1.1018 +	//Encoding the Signed object
  1.1019 +	CASN1EncSequence* signedObjectSeq=signedData->EncodeASN1DERLC();
  1.1020 +	HBufC8* buf=CreateDEREncodingLC(*signedObjectSeq);
  1.1021 +
  1.1022 +	//Encoding the wrapper Content Info
  1.1023 +	CCmsContentInfo* content=CCmsContentInfo::NewL(EContentTypeSignedData, *buf);
  1.1024 +	CleanupStack::PushL(content);
  1.1025 +	CASN1EncSequence* contentSeq=content->EncodeASN1DERLC();
  1.1026 +	HBufC8* signature=CreateDEREncodingLC(*contentSeq);
  1.1027 +	CleanupStack::Pop(signature);
  1.1028 +	CleanupStack::PopAndDestroy(5, signedData);	//contentSeq,content,buf,signedObjectSeq,signedData
  1.1029 +	CleanupStack::PushL(signature);
  1.1030 +
  1.1031 +
  1.1032 +	//write the result to a file, for initial debuging
  1.1033 +	//OutputResultToFileL(signature->Des());
  1.1034 +	
  1.1035 +	//Compare the result with the expected result, if the signature algorithms are RSA
  1.1036 +		
  1.1037 +	if (iRsaAlgorithm)
  1.1038 +		{
  1.1039 +		
  1.1040 +		//Check if the signature is the same as expected
  1.1041 +		TBool r=signature->Compare(*iExpectedEncoding);
  1.1042 +		if (r!=0 && !iIsOOMTest)
  1.1043 +			{
  1.1044 +			INFO_PRINTF1(_L("RSA Signature Encoding Error"));
  1.1045 +			User::Leave(KErrGeneral);
  1.1046 +			}
  1.1047 +		else
  1.1048 +			{
  1.1049 +			CheckAndValidateSignedDataL(*iExpectedEncoding);
  1.1050 +			}
  1.1051 +		}
  1.1052 +	else	
  1.1053 +		{
  1.1054 +		CheckAndValidateSignedDataL(*iExpectedEncoding);
  1.1055 +		CheckAndValidateSignedDataL(*signature);		
  1.1056 +		}	
  1.1057 +			
  1.1058 +	CleanupStack::PopAndDestroy(signature);
  1.1059 +	__UHEAP_MARKEND;
  1.1060 +	}
  1.1061 +
  1.1062 +//
  1.1063 +// Implementation of CMS Signed Data Decoding Test Step
  1.1064 +//
  1.1065 +
  1.1066 +CTCmsSignedDataDecodingStep::CTCmsSignedDataDecodingStep()
  1.1067 +	{
  1.1068 +	}
  1.1069 +
  1.1070 +CTCmsSignedDataDecodingStep::~CTCmsSignedDataDecodingStep()
  1.1071 +	{
  1.1072 +	}
  1.1073 +
  1.1074 +void CTCmsSignedDataDecodingStep::doTestL()
  1.1075 +	{
  1.1076 +	__UHEAP_MARK;
  1.1077 +	CheckAndValidateSignedDataL(*iExpectedEncoding);
  1.1078 +	__UHEAP_MARKEND;
  1.1079 +	}
  1.1080 +
  1.1081 +//
  1.1082 +// Implementation of CMS Content Info Test step
  1.1083 +//
  1.1084 +CTCmsContentInfoDecodingStep::CTCmsContentInfoDecodingStep()
  1.1085 +	{	
  1.1086 +	}
  1.1087 +
  1.1088 +CTCmsContentInfoDecodingStep::~CTCmsContentInfoDecodingStep()
  1.1089 +	{	
  1.1090 +	}
  1.1091 +	
  1.1092 +void CTCmsContentInfoDecodingStep::doTestL()
  1.1093 +	{
  1.1094 +	INFO_PRINTF1(_L("Start CMS Data Type Decoding"));
  1.1095 +	CCmsContentInfo* content=CCmsContentInfo::NewL(*iExpectedEncoding);
  1.1096 +	CleanupStack::PushL(content);
  1.1097 +	if (content->ContentType()!=iExpectedDataType)
  1.1098 +		{
  1.1099 +		INFO_PRINTF1(_L("CMS Data Type is not as expected"));
  1.1100 +		User::Leave(KErrGeneral);
  1.1101 +		}
  1.1102 +	CleanupStack::PopAndDestroy(content);		
  1.1103 +	}
  1.1104 +
  1.1105 +
  1.1106 +
  1.1107 +
  1.1108 +
  1.1109 +
  1.1110 +
  1.1111 +
  1.1112 +