cryptoservices/certificateandkeymgmt/tpkcs10/tcertrequeststep.cpp
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     1 /*
       
     2 * Copyright (c) 2007-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 
       
    20 
       
    21 /**
       
    22  @file  
       
    23  @internalTechnology
       
    24 */
       
    25  
       
    26 #include "tcertrequeststep.h"
       
    27 #include <f32file.h> 
       
    28 #include <e32std.h>
       
    29 
       
    30 _LIT(KKeyLabel, "new pkcs10 test key"); 
       
    31 
       
    32 // CertRequest tester active.
       
    33 CPKCS10TesterActive::CPKCS10TesterActive( CTestExecuteLogger& aLogger ) : 
       
    34    CActive( EPriorityStandard ),
       
    35    iLogger( aLogger )
       
    36 	{
       
    37 	CActiveScheduler::Add( this );
       
    38 	User::LeaveIfError(iFs.Connect());
       
    39 	}
       
    40 
       
    41 CPKCS10TesterActive::~CPKCS10TesterActive()
       
    42 	{
       
    43    	delete iDN;
       
    44 	iDN=NULL;	 
       
    45 	delete iCertRequest;
       
    46 	iCertRequest=NULL;			
       
    47  	delete iOutputASN1Encoding;	
       
    48  	iOutputASN1Encoding=NULL; 			
       
    49  	delete iSecondOutputASN1Encoding;
       
    50  	iSecondOutputASN1Encoding=NULL;
       
    51  	if (iKeyInfo)
       
    52  	{
       
    53 		iKeyInfo->Release();
       
    54 		iKeyInfo=NULL;	 			
       
    55  	}
       
    56 	delete iKeyData;
       
    57 	iKeyData=NULL;
       
    58 	if(iKeyStore) // debug.
       
    59 	{
       
    60 	    iKeyStore->Cancel();
       
    61 		delete iKeyStore;
       
    62 		iKeyStore=NULL;	
       
    63 	}
       
    64 	iFs.Close ();
       
    65 	}
       
    66 	
       
    67 void CPKCS10TesterActive::DeleteAllKeysL()
       
    68 	{
       
    69 	// Delete Key store.
       
    70 	INFO_PRINTF1(_L("deleting store keys"));
       
    71   	iKeyStore = CUnifiedKeyStore::NewL(iFs);
       
    72   	CleanupStack::PushL(iKeyStore); 
       
    73   	iKeyStore->Initialize(iStatus);  
       
    74 	
       
    75 	iState = EDeleteAllInit;
       
    76 	SetActive();
       
    77    	CActiveScheduler::Start();
       
    78    	   	
       
    79    	iKeys.Close();
       
    80    	CleanupStack::Pop(iKeyStore);
       
    81    	delete iKeyStore;
       
    82 	iKeyStore = NULL;	
       
    83 	
       
    84 	}
       
    85 
       
    86 TVerdict CPKCS10TesterActive::doActiveCertRequestL(CCertificateRequestStep* aStep)
       
    87 	{
       
    88 	
       
    89 	iTestSuccess= EFail;
       
    90 	INFO_PRINTF1(_L("Active tester for Cert Request started. "));
       
    91 	iStepPointer = aStep;
       
    92 		
       
    93     DeleteAllKeysL();
       
    94 	
       
    95     INFO_PRINTF1(_L("initialising keystore"));
       
    96     
       
    97 	// Initialise Key store.
       
    98   	iKeyStore = CUnifiedKeyStore::NewL(iFs);
       
    99   	CleanupStack::PushL(iKeyStore); 
       
   100   	iKeyStore->Initialize(iStatus);  
       
   101 	iState = EInitKeyStore; 
       
   102 	SetActive();
       
   103    	CActiveScheduler::Start();
       
   104    	
       
   105    	// After encoding was produced it checks correctness
       
   106     if(iRunError==KErrNone)
       
   107      {
       
   108 	     if( !(iStepPointer->iOOMCondition) &&verifyCertReqEncodingL()!=EPass  )
       
   109 		 {
       
   110 			  iTestSuccess= EFail;
       
   111 		 }
       
   112 		 else    
       
   113 		 {
       
   114 		 	  iTestSuccess= EPass;
       
   115 		 }
       
   116 
       
   117 		 
       
   118 		 if( iTestSuccess && iStepPointer->iGenerateSecondRequest) // if cert was reused.
       
   119 		 {
       
   120 			// compare encoding of first and second request.
       
   121 			if((iOutputASN1Encoding->Compare(iSecondOutputASN1Encoding->Des())) == 0)
       
   122 			{
       
   123 		 		INFO_PRINTF1(_L("Reuse verified"));
       
   124 		 	}
       
   125 			else
       
   126 		 	{
       
   127 		 		iTestSuccess= EFail;
       
   128 		 		//iStepPointerSetTestStepResult(EFail);
       
   129 		 		INFO_PRINTF1(_L("New output encoding is not what is expected"));		
       
   130 		 	} 
       
   131 		 }
       
   132      }
       
   133   	
       
   134    	CleanupStack::Pop(iKeyStore);  
       
   135     return iTestSuccess;
       
   136 }
       
   137 	
       
   138 TInt CPKCS10TesterActive::RunError(TInt aError)
       
   139 	{
       
   140 	iRunError =aError;
       
   141 	iKeyStore->Cancel();
       
   142 	if(iCertRequest)
       
   143 	{
       
   144 		iCertRequest->Cancel();	
       
   145 	}
       
   146 	CActiveScheduler::Stop();
       
   147 	return KErrNone;
       
   148 	
       
   149 	}
       
   150 	
       
   151 void CPKCS10TesterActive::RunL()
       
   152 	{
       
   153 	iRunError =KErrNone;
       
   154 	
       
   155  	User::LeaveIfError(iStatus.Int());
       
   156    
       
   157 	switch(iState)
       
   158 		{
       
   159 		
       
   160 		case EDeleteAllInit:
       
   161 			INFO_PRINTF1(_L("  listing existing keys\n"));
       
   162 			iKeyStore->List(iKeys, iKeyFilter, iStatus);
       
   163 			iState = EDeleteAllDelete;
       
   164 			SetActive();
       
   165 			break;
       
   166 			
       
   167 		case EDeleteAllDelete:
       
   168 			if (iKeys.Count() == 0)
       
   169 				{
       
   170 				// key log is empty
       
   171 				iKeys.Close();
       
   172 				CActiveScheduler::Stop();
       
   173 				break;
       
   174 				}
       
   175 			
       
   176 			INFO_PRINTF1(_L("  deleting key\n"));
       
   177 			iKeyStore->DeleteKey(*iKeys[0], iStatus);
       
   178 			iState = EDeleteAllDelete;
       
   179 			SetActive();
       
   180 			iKeys[0]->Release();
       
   181 			iKeys.Remove(0);
       
   182 			break;
       
   183 		
       
   184 		case EInitKeyStore:
       
   185 			{
       
   186 	  		INFO_PRINTF1(_L("Importing keys"));
       
   187  			TFileName filename;
       
   188  			filename = iStepPointer->iPrivateKey;
       
   189 			RFile file;
       
   190 			User::LeaveIfError(file.Open(iFs,filename,EFileRead));
       
   191 			CleanupClosePushL(file);
       
   192 			TInt size;
       
   193 			User::LeaveIfError(file.Size(size));
       
   194 			iKeyData = HBufC8::NewMaxL(size);
       
   195 			TPtr8 keyPtr = iKeyData->Des();
       
   196 			User::LeaveIfError(file.Read(keyPtr));			
       
   197 			CleanupStack::PopAndDestroy(); // file
       
   198 
       
   199 			TTime start(0.0); 
       
   200 			TTime end(0.0); 
       
   201 			
       
   202 			// Assumes only one keystore
       
   203 			// Check parameters!
       
   204 		 	ASSERT(iKeyInfo == NULL);      
       
   205 			iKeyStore->ImportKey(0, *(iKeyData), EPKCS15UsageSign, KKeyLabel,0, start, end,iKeyInfo, iStatus);
       
   206 	  		iState = EImportKey;
       
   207 	  		SetActive();
       
   208 			break;
       
   209 			}
       
   210 	  	case EImportKey:
       
   211 			{
       
   212 			INFO_PRINTF1(_L("Setting security policy for new stored key"));
       
   213 			TSecureId secureId(0x101f7784); // Application secure ID 
       
   214 			TSecurityPolicy securePolicy(secureId,ECapabilityReadUserData);
       
   215 			iKeyStore->SetUsePolicy(iKeyInfo->Handle(),securePolicy,iStatus);
       
   216 			iState = EKeyPolicy;
       
   217  			SetActive();
       
   218  			break;
       
   219 		   	}
       
   220 		case EKeyPolicy:
       
   221 			{
       
   222 			iAttrCollection=CPKCS10Attributes::NewL();
       
   223  			CleanupStack::PushL(iAttrCollection); 
       
   224 			INFO_PRINTF1(_L("Adding generic attributes"));		
       
   225  			AddGenericAttributesL();
       
   226  			INFO_PRINTF1(_L("Adding Challenge password"));
       
   227  			AddChallengePasswordL();
       
   228  			INFO_PRINTF1(_L("Adding V3 extensions"));
       
   229  			AddV3ExtensionsL();
       
   230  			INFO_PRINTF1(_L("Generating distinguished name"));
       
   231  			iDN=MakeDistinguishedNameL();
       
   232  			CleanupStack::PushL(iDN);
       
   233    	 		INFO_PRINTF1(_L("Generating cert request"));
       
   234    	 		iCertRequest=CPKCS10Request::NewL(*iDN,*iKeyInfo,iAttrCollection);
       
   235      		CleanupStack::PushL(iCertRequest);
       
   236      		INFO_PRINTF1(_L("Setting digest algorithm"));
       
   237 			TAlgorithmId digestAlgo=iStepPointer->ConvertNameToDigestId(iStepPointer->iDigestAlg);
       
   238 			iCertRequest->SetDigestAlgL(digestAlgo);
       
   239  			INFO_PRINTF1(_L("Requesting cert request encoding"));
       
   240  	  		
       
   241  	  		// Clean up
       
   242  	  		CleanupStack::Pop(iCertRequest);
       
   243  			CleanupStack::Pop(iDN);
       
   244     	 	CleanupStack::Pop(iAttrCollection);
       
   245     	  	iAttrCollection=NULL;  
       
   246 			iOutputASN1Encoding=NULL;
       
   247 			iCertRequest->CreateEncoding(iOutputASN1Encoding,iStatus);
       
   248 			iState=EGenerateCertRequest; 
       
   249 	 		SetActive();     
       
   250 	  		break;	
       
   251 			}
       
   252 		case EGenerateCertRequest:
       
   253 			{ 
       
   254 			
       
   255 			// Use to debug encoding 			
       
   256   			// iStepPointer->OutputEncodingToFileL(iOutputASN1Encoding->Des()); //debug
       
   257 			// Used for cert request reuse cases  
       
   258 			if(iStepPointer->iGenerateSecondRequest)
       
   259 				{       
       
   260 						INFO_PRINTF1(_L("Reusing instance of CPKCS10Request"));
       
   261 				        if(iStepPointer->iRepopulateDataRequest)
       
   262 				        {
       
   263 				         	iAttrCollection=CPKCS10Attributes::NewL();
       
   264 				            CleanupStack::PushL(iAttrCollection); 
       
   265 				        	AddGenericAttributesL();
       
   266 				 			AddChallengePasswordL();
       
   267 				 			AddV3ExtensionsL();
       
   268 				 			// deletes previous value of iDN.
       
   269 				 			delete iDN;
       
   270 				 			iDN=MakeDistinguishedNameL();
       
   271 				 			CleanupStack::PushL(iDN);
       
   272 				   	 		TAlgorithmId digestAlgo2=iStepPointer->ConvertNameToDigestId(iStepPointer->iDigestAlg);
       
   273 							// Repopulates data.
       
   274 							CleanupStack::PushL(iCertRequest);
       
   275 							iCertRequest->SetDigestAlgL(digestAlgo2);
       
   276 							iCertRequest->SetDistinguishedNameL(*iDN);
       
   277 							iCertRequest->SetAttributes(iAttrCollection);
       
   278 							iCertRequest->SetKeyInfoL(*iKeyInfo);
       
   279 							// Clean up
       
   280 							CleanupStack::Pop(iCertRequest);
       
   281 							CleanupStack::Pop(iDN);
       
   282     						CleanupStack::Pop(iAttrCollection);
       
   283     						iAttrCollection=NULL;		
       
   284 				        }
       
   285 				        INFO_PRINTF1(_L("Launches second cert request"));
       
   286 				        iSecondOutputASN1Encoding=NULL;
       
   287 				        iCertRequest->CreateEncoding(iSecondOutputASN1Encoding,iStatus);
       
   288 				        iState=EGenerateSecondCertRequest;
       
   289 				}
       
   290 				else
       
   291 				{
       
   292 					 // if no reuse case delete keys and prepare for final state
       
   293 						INFO_PRINTF1(_L("Deleting key"));
       
   294  		 				iKeyStore->DeleteKey(*iKeyInfo, iStatus);
       
   295  		 				iState=EDeleteKey;
       
   296 				}
       
   297 			
       
   298  		  	SetActive();  
       
   299  		 	break;	
       
   300 			}
       
   301 		case EGenerateSecondCertRequest:
       
   302 			{
       
   303 			INFO_PRINTF1(_L("Deleting key"));
       
   304  		 	iKeyStore->DeleteKey(*iKeyInfo,iStatus);
       
   305  		 	iState=EDeleteKey;
       
   306  		 	SetActive();
       
   307  		 	break;
       
   308 			}
       
   309 		case EDeleteKey:
       
   310 			{
       
   311 		    iKeyInfo->Release();  
       
   312 			iKeyInfo = NULL;
       
   313 		 	CActiveScheduler::Stop();
       
   314  		    break;	
       
   315 			}
       
   316  		default:
       
   317 			{
       
   318 		  	INFO_PRINTF1(_L("Cert Request Active tester: State corrupted."));
       
   319 			User::Leave(KErrCorrupt);
       
   320 			}
       
   321  		} 
       
   322  		
       
   323    	return; 
       
   324 }
       
   325 
       
   326 	
       
   327 CCertificateRequestStep::~CCertificateRequestStep()
       
   328 /**
       
   329  * Destructor
       
   330  */
       
   331 	{   
       
   332  		delete iActiveObjTest;
       
   333  		delete iSched;
       
   334 	}
       
   335 
       
   336 CCertificateRequestStep::CCertificateRequestStep()
       
   337 {
       
   338 	SetTestStepName(KCertificateRequestStep);
       
   339 }
       
   340 
       
   341 TVerdict CCertificateRequestStep::doTestStepPreambleL()
       
   342 {
       
   343 	__UHEAP_MARK;	
       
   344 	User::LeaveIfError (iFs.Connect());
       
   345 	
       
   346 	// initializes data.
       
   347            
       
   348 	// Read values form config file 
       
   349 	GetIntFromConfig(ConfigSection(), _L("Expected_error"), iExpectedError);	
       
   350     GetStringFromConfig(ConfigSection(), _L("DN_country"), iDN_country);
       
   351     GetStringFromConfig(ConfigSection(), _L("DN_state"), iDN_state);
       
   352     GetStringFromConfig(ConfigSection(), _L("DN_locality"), iDN_locality);
       
   353     GetStringFromConfig(ConfigSection(), _L("DN_organization"), iDN_organization);
       
   354     GetStringFromConfig(ConfigSection(), _L("DN_unit"), iDN_unit);
       
   355     GetStringFromConfig(ConfigSection(), _L("DN_common"), iDN_common);
       
   356     GetStringFromConfig(ConfigSection(), _L("DN_email"), iDN_email);
       
   357     GetStringFromConfig(ConfigSection(), _L("PrivateKey"),iPrivateKey);
       
   358     GetStringFromConfig(ConfigSection(), _L("OPENSSL_certreq"),iOPENSSLCertReq);
       
   359     GetStringFromConfig(ConfigSection(), _L("KeyAlg"),iKeyAlg);
       
   360     GetStringFromConfig(ConfigSection(), _L("ChallengePassword"),iChallengePassword);
       
   361     GetStringFromConfig(ConfigSection(), _L("DigestAlg"),iDigestAlg);
       
   362     GetBoolFromConfig(ConfigSection(), _L("OOMCondition"),iOOMCondition);
       
   363     GetBoolFromConfig(ConfigSection(), _L("GenerateSecondRequest"),iGenerateSecondRequest);
       
   364     GetBoolFromConfig(ConfigSection(), _L("RepopulateDataRequest"),iRepopulateDataRequest);
       
   365     GetIntFromConfig(ConfigSection(), _L("ElemCertReqCount"),iElemCertReqCount);
       
   366     GetIntFromConfig(ConfigSection(), _L("ElemCertInfoCount"),iElemCertInfoCount);
       
   367     GetIntFromConfig(ConfigSection(), _L("CertReqVer"),iCertReqVer);
       
   368     GetIntFromConfig(ConfigSection(), _L("ElemSubPubKeytInfoCount"),iElemSubPubKeytInfoCount);
       
   369     GetIntFromConfig(ConfigSection(), _L("ElemKeyAlgIdenCount"),iElemKeyAlgIdenCount);
       
   370     GetIntFromConfig(ConfigSection(), _L("ElemSigAlgIdenCount"),iElemSigAlgIdenCount);
       
   371     GetIntFromConfig(ConfigSection(), _L("Attribute_count"),iAttribute_count );
       
   372     
       
   373     // Read generic Attributes (ARRAY).
       
   374     TInt index(0);
       
   375 	TName fGenericAttrOID;
       
   376 	fGenericAttrOID.Format(_L("Attribute_OID_%d"), index);
       
   377 	TName fGenericAttrValue;
       
   378 	fGenericAttrValue.Format(_L("Attribute_value_%d"), index); 
       
   379 	
       
   380 	TPtrC genericAttrOIDName;
       
   381 	TPtrC genericAttrValueName;
       
   382     
       
   383    	while (GetStringFromConfig(ConfigSection(), fGenericAttrOID, genericAttrOIDName)
       
   384 			&& GetStringFromConfig(ConfigSection(), fGenericAttrValue, genericAttrValueName))
       
   385 	{
       
   386 	   
       
   387 	    iArrayGenAttrOID.AppendL(genericAttrOIDName);
       
   388 	    iArrayGenAttrValue.AppendL(genericAttrValueName);
       
   389 		index++;
       
   390 		fGenericAttrOID.Format(_L("Attribute_OID_%d"), index);
       
   391 		fGenericAttrValue.Format(_L("Attribute_value_%d"), index);
       
   392 		
       
   393 	}	
       
   394 		
       
   395 	// Read the v3 extension attributes (Array)
       
   396 	index=0;
       
   397     TName fV3AttrOID;
       
   398 	fV3AttrOID.Format(_L("V3_Extension_OID_%d"), index);
       
   399 	TName fV3AttrCritical;
       
   400 	fV3AttrCritical.Format(_L("V3_Extension_Critical_%d"), index); 
       
   401 	TName fV3AttrValue;
       
   402 	fV3AttrValue.Format(_L("V3_Extension_Value_%d"), index); 
       
   403 
       
   404 	TPtrC v3AttrOID;
       
   405 	TBool v3AttrCritical;
       
   406 	TPtrC v3AttrValue;
       
   407 	
       
   408    	while (GetStringFromConfig(ConfigSection(), fV3AttrOID, v3AttrOID)
       
   409 			&& GetBoolFromConfig(ConfigSection(), fV3AttrCritical, v3AttrCritical)
       
   410 			&& GetStringFromConfig(ConfigSection(), fV3AttrValue, v3AttrValue))
       
   411 		{
       
   412 	    iArrayV3AttrOID.AppendL(v3AttrOID);
       
   413 	    iArrayV3AttrCritical.AppendL(v3AttrCritical);
       
   414 	    iArrayV3AttrValue.AppendL(v3AttrValue);
       
   415 		index++;
       
   416 		fV3AttrOID.Format(_L("V3_Extension_OID_%d"), index);
       
   417 		fV3AttrCritical.Format(_L("V3_Extension_Critical_%d"), index); 
       
   418 		fV3AttrValue.Format(_L("V3_Extension_Value_%d"), index); 
       
   419 		}	
       
   420 		
       
   421 	SetTestStepResult(EPass);
       
   422 	return TestStepResult();
       
   423 }
       
   424 
       
   425 
       
   426 TVerdict CCertificateRequestStep::doTestStepL()
       
   427 {
       
   428 	if (!iOOMCondition)
       
   429 		{
       
   430 		 doTestL(); 
       
   431 		}
       
   432 	else
       
   433 		{
       
   434  		return doOOMTestL();
       
   435 	    }	
       
   436 
       
   437    	 return TestStepResult();
       
   438 }
       
   439 
       
   440 TVerdict CCertificateRequestStep::doOOMTestL()
       
   441 {
       
   442 	TVerdict verdict = EFail;
       
   443  	TInt countAfter = 0;
       
   444 	TInt countBefore = 0;
       
   445  	for (TInt oomCount = 0; ; oomCount++)
       
   446  		{
       
   447  		INFO_PRINTF2(_L("\n ==== Number of memory allocations %d ===="), oomCount);
       
   448  		verdict = EFail;
       
   449  		__UHEAP_RESET;
       
   450  		__UHEAP_SETFAIL(RHeap::EDeterministic, oomCount);
       
   451  		countBefore = User::CountAllocCells();
       
   452  		TRAPD(error, doTestL());// ----> This is the actual test that runs under OOM conditions.
       
   453  		countAfter = User::CountAllocCells();
       
   454  		__UHEAP_RESET;
       
   455  		if (error != KErrNoMemory)  
       
   456  			{
       
   457  			verdict = EPass;
       
   458  			INFO_PRINTF2(_L("OOM Status %d"),error);
       
   459 			INFO_PRINTF1(_L("Test outcome : Passed"));
       
   460  			break;
       
   461  			}
       
   462  		else
       
   463  			{
       
   464  			if (countBefore != countAfter)
       
   465  				{
       
   466  				INFO_PRINTF2(_L("OOM Status %d"),error);
       
   467  				INFO_PRINTF2(_L("OOM Failed at %d"), oomCount);
       
   468  				SetTestStepResult(verdict);  
       
   469  				break;
       
   470  				}
       
   471  			}
       
   472  		INFO_PRINTF2(_L("OOM Failed Point status %d"), error);
       
   473 		}
       
   474 	INFO_PRINTF3(_L("Heap alloc count ok: %d final vs %d initial"), countAfter,countBefore);
       
   475  	SetTestStepResult(verdict);
       
   476  	if (verdict==EFail)
       
   477 	 	{
       
   478  		User::Leave(KErrGeneral);	 		
       
   479 	 	}	 	
       
   480  	return verdict;
       
   481 	}	
       
   482 	
       
   483 
       
   484 
       
   485 	
       
   486 void CCertificateRequestStep::doTestL()
       
   487 {
       
   488     
       
   489     iSched=new(ELeave) CActiveScheduler; 
       
   490     CleanupStack::PushL(iSched);  
       
   491 	CActiveScheduler::Install(iSched);
       
   492 	
       
   493 	
       
   494 	iActiveObjTest = new (ELeave) CPKCS10TesterActive(Logger());
       
   495 	CleanupStack::PushL(iActiveObjTest);
       
   496 
       
   497 	if (iActiveObjTest->doActiveCertRequestL(this) != EPass)
       
   498 	 {
       
   499 	 	SetTestStepResult(EFail);
       
   500 	 	INFO_PRINTF1(_L("Verification FAIL."));
       
   501 	 	// To keep happy out of memory test.
       
   502 	 	User::Leave(KErrNoMemory);
       
   503 
       
   504 	 }
       
   505 	 else
       
   506 	 {
       
   507 	 	INFO_PRINTF1(_L("Verification PASS."));	
       
   508 	 }
       
   509 
       
   510 	 CleanupStack::PopAndDestroy(iActiveObjTest);
       
   511 	 iActiveObjTest = NULL;
       
   512 	 CleanupStack::PopAndDestroy(iSched);
       
   513 	 iSched=NULL;
       
   514 	 
       
   515  }
       
   516 
       
   517 TVerdict CPKCS10TesterActive::verifyCertReqEncodingL()
       
   518 {   
       
   519     TVerdict certReqCheck= EPass;
       
   520 	INFO_PRINTF1(_L("Verifiying cert request encoding"));
       
   521 	TInt pos(0);
       
   522 	 
       
   523 	CArrayPtrFlat<TASN1DecGeneric>* certReq= TASN1DecSequence().DecodeDERLC(*iOutputASN1Encoding,pos);
       
   524 	// Verifies Number of elements in cert request.
       
   525 	if(certReq->Count() != iStepPointer->iElemCertReqCount)
       
   526 	{
       
   527 		INFO_PRINTF1(_L("VERIFICATION FAILED: Incorrect number of elements in cert request"));
       
   528 		certReqCheck= EFail;
       
   529 	}
       
   530 	
       
   531 	CArrayPtrFlat<TASN1DecGeneric>* certReqInfo=TASN1DecSequence().DecodeDERLC(*certReq->At(0));  
       
   532 	// Verifies Number of elements in cert request info.
       
   533 	if(certReqInfo->Count() != iStepPointer->iElemCertInfoCount)
       
   534 	{
       
   535 		INFO_PRINTF1(_L("VERIFICATION FAILED: Incorrect number of elements in cert request info"));
       
   536 		certReqCheck= EFail;
       
   537 	}
       
   538 		  
       
   539 	TASN1DecInteger decInt;
       
   540 	TInt version = decInt.DecodeDERShortL(*certReqInfo->At(0));
       
   541 	// Verifies expected version in cert request info.
       
   542 	if(version != iStepPointer->iCertReqVer)
       
   543 	{
       
   544 		INFO_PRINTF1(_L("VERIFICATION FAILED: Incorrect version in cert request info"));
       
   545 		certReqCheck= EFail;
       
   546 	}
       
   547 
       
   548 	pos = 0;
       
   549 	CX500DistinguishedName* dn = CX500DistinguishedName::NewLC(certReqInfo->At(1)->Encoding());
       
   550 	// Verifies distinguished name.
       
   551 	if(!(iDN->ExactMatchL(*dn)))
       
   552 		{
       
   553 		INFO_PRINTF1(_L("VERIFICATION FAILED: Incorrect distinguished name encoding"));
       
   554 		certReqCheck= EFail;
       
   555 	}
       
   556 	
       
   557 	CArrayPtrFlat<TASN1DecGeneric>* subjPubKeyInfo = TASN1DecSequence().DecodeDERLC(*certReqInfo->At(2));
       
   558 	// Verifies number of elements in public key info.
       
   559 	if( iStepPointer->iElemSubPubKeytInfoCount != subjPubKeyInfo->Count())
       
   560 	{
       
   561 		INFO_PRINTF1(_L("VERIFICATION FAILED: Incorrect number of elements in public key info."));
       
   562 		certReqCheck= EFail;
       
   563 	}
       
   564 	
       
   565 	CArrayPtrFlat<TASN1DecGeneric>* keyAlg = TASN1DecSequence().DecodeDERLC(*subjPubKeyInfo->At(0));
       
   566 	HBufC* keyAlgOid = TASN1DecObjectIdentifier().DecodeDERL(*keyAlg->At(0));
       
   567 	CleanupStack::PushL(keyAlgOid);
       
   568 	// Verifies number of elements in public key algorithm identifier.
       
   569 	if( keyAlg->Count() != iStepPointer->iElemKeyAlgIdenCount)
       
   570 	{
       
   571 		INFO_PRINTF1(_L("VERIFICATION FAILED: Incorrect number of elements in public key algorithm identifier."));
       
   572 		certReqCheck= EFail;
       
   573 	}
       
   574 
       
   575     HBufC8* pubKeyData = TASN1DecBitString().ExtractOctetStringL(*subjPubKeyInfo->At(1));
       
   576 	CleanupStack::PushL(pubKeyData);
       
   577     // Verifies number of elements in signature algorithm identifier.
       
   578 	CArrayPtrFlat<TASN1DecGeneric>* sigAlg = TASN1DecSequence().DecodeDERLC(*certReq->At(1));
       
   579 	
       
   580 	if( sigAlg->Count() != iStepPointer->iElemSigAlgIdenCount)
       
   581 	{
       
   582 		INFO_PRINTF1(_L("VERIFICATION FAILED: Incorrect number of elements in signature algorithm identifier."));
       
   583 		certReqCheck= EFail;
       
   584 	}
       
   585 	
       
   586 	
       
   587 	HBufC* sigAlgOid = TASN1DecObjectIdentifier().DecodeDERL(*sigAlg->At(0));
       
   588 	CleanupStack::PushL(sigAlgOid);
       
   589 	
       
   590 	HBufC8* signature = TASN1DecBitString().ExtractOctetStringL(*certReq->At(2));
       
   591 	CleanupStack::PushL(signature);
       
   592 	
       
   593 	CMessageDigest* digest = NULL;
       
   594 	
       
   595 	switch (iStepPointer->ConvertNameToDigestId(iStepPointer->iDigestAlg))
       
   596 		{
       
   597 		case ESHA1:
       
   598 			digest = CSHA1::NewL();
       
   599 			break;
       
   600 
       
   601 		case EMD2:
       
   602 			digest = CMD2::NewL();
       
   603 			break;
       
   604 
       
   605 		case EMD5:
       
   606 			digest = CMD5::NewL();
       
   607 			break;
       
   608 
       
   609 		default:
       
   610 				User::Leave(KErrCorrupt);
       
   611 		}
       
   612 	CleanupStack::PushL(digest);
       
   613 	 
       
   614 	if (iStepPointer->ConvertNameToEKeyAlgorithm(iStepPointer->iKeyAlg) == CCTKeyInfo::ERSA)
       
   615 	{	
       
   616 	    // Verifies key.
       
   617 		if(*keyAlgOid != KRSA)
       
   618 		{
       
   619 			INFO_PRINTF1(_L("VERIFICATION FAILED: Expecting RSA key."));
       
   620 			certReqCheck= EFail;
       
   621 		}
       
   622 				
       
   623 		// Verifies digest.
       
   624 		switch  (iStepPointer->ConvertNameToDigestId(iStepPointer->iDigestAlg))
       
   625 		{
       
   626 			case ESHA1:
       
   627 			{
       
   628 				if(*sigAlgOid != KSHA1WithRSA)
       
   629 				{
       
   630 					INFO_PRINTF1(_L("VERIFICATION FAILED: Expecting RSA with SHA1 signature."));
       
   631 					certReqCheck= EFail;
       
   632 				}
       
   633 			}
       
   634 			break;
       
   635 			case EMD2:
       
   636 			{
       
   637 				if(*sigAlgOid != KMD2WithRSA)
       
   638 				{
       
   639 					INFO_PRINTF1(_L("VERIFICATION FAILED: Expecting RSA with MD2 signature."));
       
   640 					certReqCheck= EFail;
       
   641 				}
       
   642 			}
       
   643 			break;
       
   644 			case EMD5:
       
   645 			{
       
   646 				if(*sigAlgOid != KMD5WithRSA)
       
   647 				{
       
   648 					INFO_PRINTF1(_L("VERIFICATION FAILED: Expecting RSA with MD5 signature."));
       
   649 					certReqCheck= EFail;
       
   650 				}
       
   651 			}
       
   652 			break;
       
   653 			default:
       
   654 			{
       
   655 				INFO_PRINTF1(_L("VERIFICATION FAILED: Unrecognised signature algorithm."));
       
   656 				User::Leave(KErrCorrupt);
       
   657 			}
       
   658 			break;
       
   659 		}
       
   660 		        
       
   661         // Checks RSA signature.
       
   662         // There are doubts about the validity of the method below
       
   663  /*		CRSAPublicKey* pubKey = TX509KeyFactory().RSAPublicKeyL(*pubKeyData);
       
   664 		CleanupStack::PushL(pubKey);
       
   665 		
       
   666 		RInteger sigInt = RInteger::NewL(*signature);
       
   667 		CleanupStack::PushL(sigInt);
       
   668 		CRSASignature* sig = CRSASignature::NewL(sigInt);
       
   669 		CleanupStack::Pop(); // sigInt
       
   670 		CleanupStack::PushL(sig);
       
   671 		CRSAPKCS1v15Verifier* verifier = CRSAPKCS1v15Verifier::NewLC(*pubKey);
       
   672 		digest->Update(certReq->At(0)->Encoding());
       
   673 		
       
   674 		if(!(verifier->VerifyL(digest->Final(),*sig)))
       
   675 		{
       
   676 			INFO_PRINTF1(_L("VERIFICATION FAILED: RSA Signature verification failed."));
       
   677 			User::Leave(KErrGeneral);
       
   678 		}   
       
   679 		CleanupStack::PopAndDestroy(verifier);	
       
   680 		CleanupStack::PopAndDestroy(sig);
       
   681 		CleanupStack::PopAndDestroy(pubKey);   */
       
   682 	}
       
   683 	else if (iStepPointer->ConvertNameToEKeyAlgorithm(iStepPointer->iKeyAlg)== CCTKeyInfo::EDSA)
       
   684 	{
       
   685 				// Verifies key 
       
   686 		if(*keyAlgOid != KDSA)
       
   687 		{
       
   688 				certReqCheck= EFail;
       
   689 		}
       
   690 		INFO_PRINTF1(_L("DSA key algorithm OID CORRECT"));
       
   691 		// Verifies digest
       
   692 	    if(*sigAlgOid != KDSAWithSHA1)
       
   693 		{
       
   694 				certReqCheck= EFail;
       
   695 		}
       
   696 		INFO_PRINTF1(_L("Signature algorithm OID CORRECT"));
       
   697 		
       
   698 		CDSAParameters* params = TX509KeyFactory().DSAParametersL(keyAlg->At(1)->Encoding());
       
   699 		CleanupStack::PushL(params);
       
   700 		CDSAPublicKey* pubKey = TX509KeyFactory().DSAPublicKeyL(*params, *pubKeyData);
       
   701 		CleanupStack::PushL(pubKey);
       
   702 
       
   703 		// Test sig
       
   704 		CDSASignature* sig = TX509KeyFactory().DSASignatureL(*signature);
       
   705 		CleanupStack::PushL(sig);
       
   706 
       
   707 		CDSAVerifier* verifier = CDSAVerifier::NewLC(*pubKey);
       
   708 		digest->Update(certReq->At(0)->Encoding());
       
   709 		// Verifies signature.
       
   710 		if(!(verifier->VerifyL(digest->Final(),*sig)))
       
   711 		{
       
   712 				certReqCheck= EFail;
       
   713 		}
       
   714 			
       
   715 		CleanupStack::PopAndDestroy(verifier);
       
   716 		CleanupStack::PopAndDestroy(sig);
       
   717 		CleanupStack::PopAndDestroy(pubKey);
       
   718 		CleanupStack::PopAndDestroy(params);
       
   719 	}
       
   720 	else
       
   721 	{
       
   722 		INFO_PRINTF1(_L("VERIFICATION FAILED: Invalid key algorithm."));
       
   723 		certReqCheck= EFail;
       
   724 	}
       
   725     
       
   726     // Verifies number of attributes.
       
   727     CArrayPtrFlat<TASN1DecGeneric>* attrSet = TASN1DecSet().DecodeDERLC(*certReqInfo->At(3));
       
   728     
       
   729     if(attrSet->Count() != iStepPointer->iAttribute_count)
       
   730 	{
       
   731 			INFO_PRINTF1(_L("VERIFICATION FAILED: Number of attributes incorrect"));
       
   732 			certReqCheck= EFail;
       
   733 	}
       
   734 		
       
   735 	// makes binary compare if key is not DSA.
       
   736  	if (iStepPointer->ConvertNameToEKeyAlgorithm(iStepPointer->iKeyAlg) != CCTKeyInfo::EDSA)  // Do not compare if we have DSA signatures, these are not deterministic!
       
   737 	{
       
   738 		if(!(CompareRequestToOPENSSLReqL()))
       
   739 		{
       
   740 				INFO_PRINTF1(_L("VERIFICATION FAILED: Binary compare with OPENSSL cert request does not match"));
       
   741 				certReqCheck= EFail;
       
   742 		}
       
   743 	}
       
   744 	else
       
   745 	{
       
   746 		INFO_PRINTF1(_L("No binary compare becuase is a DSA cert req."));	
       
   747 	}   
       
   748      
       
   749     INFO_PRINTF1(_L("Verification completed."));
       
   750    
       
   751     // pop and destroy: attrSet, digest, signature, sigAlgOid, sigAlg, pubKeyData
       
   752     // keyAlgOid, keyAlg, elmSubjPubKeyInfo, dnChecker, certReqInfo, certReqASN1
       
   753     CleanupStack::PopAndDestroy(12,certReq);
       
   754 	return certReqCheck;	
       
   755 }
       
   756 
       
   757 TVerdict CCertificateRequestStep::doTestStepPostambleL()
       
   758 {
       
   759  	iArrayGenAttrOID.Close();
       
   760  	iArrayGenAttrValue.Close();
       
   761 	iArrayV3AttrOID.Close();
       
   762 	iArrayV3AttrCritical.Close();
       
   763 	iArrayV3AttrValue.Close();
       
   764 	__UHEAP_MARKEND;
       
   765 	
       
   766 	return TestStepResult();
       
   767 }
       
   768 
       
   769 void cleanuparray(TAny* aArray)
       
   770 {
       
   771 	CArrayPtrFlat<CX520AttributeTypeAndValue>* array=(CArrayPtrFlat<CX520AttributeTypeAndValue>*)aArray;
       
   772 	array->ResetAndDestroy();
       
   773 	delete array;
       
   774 }
       
   775 	
       
   776 CX500DistinguishedName* CPKCS10TesterActive::MakeDistinguishedNameL()
       
   777 {
       
   778     CArrayPtrFlat<CX520AttributeTypeAndValue>* array = new(ELeave) CArrayPtrFlat<CX520AttributeTypeAndValue>(7);
       
   779 	TCleanupItem cleanup(cleanuparray, array);	
       
   780 	CleanupStack::PushL(cleanup);
       
   781 	array->SetReserveL(7);
       
   782 	
       
   783 	HBufC8 *converter = HBufC8::NewMaxLC(iStepPointer->iDN_common.Length());
       
   784     converter->Des().Copy(iStepPointer->iDN_common);	
       
   785 	CX520AttributeTypeAndValue* commonName = CX520AttributeTypeAndValue::NewLC(ECommonName,*converter);
       
   786 	array->AppendL(commonName);
       
   787 	CleanupStack::Pop(commonName);
       
   788 	CleanupStack::PopAndDestroy(converter);
       
   789 	
       
   790 	converter = HBufC8::NewMaxLC(iStepPointer->iDN_country.Length());
       
   791     converter->Des().Copy(iStepPointer->iDN_country);
       
   792 	CX520AttributeTypeAndValue* country = CX520AttributeTypeAndValue::NewLC(ECountryName,*converter);
       
   793 	array->AppendL(country);
       
   794 	CleanupStack::Pop(country);
       
   795 	CleanupStack::PopAndDestroy(converter);
       
   796 	
       
   797 	converter = HBufC8::NewMaxLC(iStepPointer->iDN_locality.Length());
       
   798     converter->Des().Copy(iStepPointer->iDN_locality);
       
   799 	CX520AttributeTypeAndValue* locality = CX520AttributeTypeAndValue::NewLC(ELocalityName,*converter);
       
   800 	array->AppendL(locality);
       
   801 	CleanupStack::Pop(locality);
       
   802 	CleanupStack::PopAndDestroy(converter);
       
   803 	
       
   804 	converter = HBufC8::NewMaxLC(iStepPointer->iDN_state.Length());
       
   805     converter->Des().Copy(iStepPointer->iDN_state);
       
   806 	CX520AttributeTypeAndValue* province = CX520AttributeTypeAndValue::NewLC(EStateOrProvinceName,*converter);
       
   807 	array->AppendL(province);
       
   808 	CleanupStack::Pop(province);
       
   809 	CleanupStack::PopAndDestroy(converter);
       
   810 		
       
   811 	converter = HBufC8::NewMaxLC(iStepPointer->iDN_organization.Length());
       
   812     converter->Des().Copy(iStepPointer->iDN_organization);
       
   813 	CX520AttributeTypeAndValue* org = CX520AttributeTypeAndValue::NewLC(EOrganizationName,*converter);
       
   814 	array->AppendL(org);
       
   815 	CleanupStack::Pop(org);
       
   816 	CleanupStack::PopAndDestroy(converter);
       
   817 	
       
   818 	converter = HBufC8::NewMaxLC(iStepPointer->iDN_unit.Length());
       
   819     converter->Des().Copy(iStepPointer->iDN_unit);
       
   820 	CX520AttributeTypeAndValue* unit = CX520AttributeTypeAndValue::NewLC(EOrganizationalUnitName,*converter);
       
   821 	array->AppendL(unit);
       
   822 	CleanupStack::Pop(unit);
       
   823 	CleanupStack::PopAndDestroy(converter);
       
   824 	//delete converter;
       
   825 	converter = HBufC8::NewMaxLC(iStepPointer->iDN_email.Length());
       
   826     converter->Des().Copy(iStepPointer->iDN_email);	
       
   827 	CX520AttributeTypeAndValue* email = CX520AttributeTypeAndValue::NewLC(EPKCS9EmailAddress,*converter);
       
   828 	array->AppendL(email);
       
   829 	CleanupStack::Pop(email);
       
   830 	CleanupStack::PopAndDestroy(converter);
       
   831     
       
   832 	CX500DistinguishedName* dn = CX500DistinguishedName::NewL(*array);
       
   833 	
       
   834 	CleanupStack::PopAndDestroy(); //array
       
   835 
       
   836 	return dn;
       
   837 }
       
   838 
       
   839 void CPKCS10TesterActive::AddGenericAttributesL()
       
   840 {
       
   841 	TInt index;
       
   842  	TInt numberGenAttr;
       
   843  	CPKCS10Attribute* genericAttr;
       
   844  	CASN1EncPrintableString* attrString;
       
   845  			
       
   846  	// Add generic attributes.
       
   847  	numberGenAttr= iStepPointer->iArrayGenAttrOID.Count() ;
       
   848  			
       
   849  	index=0;
       
   850  	HBufC8 *converter;
       
   851  	while(numberGenAttr>index)
       
   852  	{   
       
   853  	  	converter = HBufC8::NewMaxLC(iStepPointer->iArrayGenAttrValue[index].Length());
       
   854         converter->Des().Copy(iStepPointer->iArrayGenAttrValue[index]);
       
   855  	    attrString=CASN1EncPrintableString::NewLC(*converter);
       
   856  	    genericAttr= CPKCS10Attribute::NewL(iStepPointer->iArrayGenAttrOID[index],attrString);
       
   857  	    CleanupStack::Pop(attrString);  
       
   858  	    CleanupStack::PushL(genericAttr);
       
   859  	    iAttrCollection->AddPKCSAttributeL(genericAttr);
       
   860  	    CleanupStack::Pop(genericAttr);
       
   861  		CleanupStack::PopAndDestroy(converter);
       
   862  		index++;  
       
   863  	}
       
   864  	
       
   865  	if(numberGenAttr>0)
       
   866  	{
       
   867  		INFO_PRINTF1(_L("Generic attributes not found nor added"));	
       
   868  	}		
       
   869 }
       
   870 
       
   871 void CPKCS10TesterActive::AddChallengePasswordL()
       
   872 {
       
   873      if(iStepPointer->iChallengePassword.Length()>0)
       
   874    {
       
   875       HBufC8 *passwordString = HBufC8::NewMaxLC(iStepPointer->iChallengePassword.Length());
       
   876 	  passwordString->Des().Copy(iStepPointer->iChallengePassword);
       
   877    	  CPKCS9ChallengePasswordAttr* challengePassword = CPKCS9ChallengePasswordAttr::NewL(*passwordString);
       
   878    	  CleanupStack::PopAndDestroy(passwordString);
       
   879    	  CleanupStack::PushL(challengePassword);
       
   880    	  iAttrCollection->AddPKCSAttributeL(challengePassword);
       
   881    	  CleanupStack::Pop(challengePassword);
       
   882    }
       
   883    else
       
   884    {
       
   885    	   	INFO_PRINTF1(_L("Challenge Password not found or added"));
       
   886    }
       
   887 }
       
   888 
       
   889 void CPKCS10TesterActive::AddV3ExtensionsL()
       
   890 {
       
   891 	TInt index;
       
   892  	TInt numV3ExtensionAttr;
       
   893  	CX509CertExtension* v3ExtensionAttr;
       
   894  	HBufC8* rawExtensionValue;
       
   895  			
       
   896  	// Add generic attributes.
       
   897  	numV3ExtensionAttr= iStepPointer->iArrayV3AttrOID.Count() ;
       
   898  			
       
   899  	index=0;
       
   900    	
       
   901  	while(numV3ExtensionAttr>index)
       
   902 	{
       
   903 	
       
   904  		TFileName filename;
       
   905 		filename = iStepPointer->iArrayV3AttrValue[index];
       
   906 		RFile file;
       
   907 		User::LeaveIfError(file.Open(iFs,filename,EFileRead|EFileStream));
       
   908 		CleanupClosePushL(file);
       
   909 		TInt size;
       
   910 		User::LeaveIfError(file.Size(size));
       
   911 		rawExtensionValue = HBufC8::NewMaxL(size);
       
   912 		CleanupStack::PushL(rawExtensionValue);
       
   913 		TPtr8 extValuePtr = rawExtensionValue->Des();
       
   914 		
       
   915 		User::LeaveIfError(file.Read(extValuePtr));
       
   916 				
       
   917 		v3ExtensionAttr= CX509CertExtension::NewLC(iStepPointer->iArrayV3AttrOID[index], 
       
   918 											 iStepPointer->iArrayV3AttrCritical[index],
       
   919 											 extValuePtr);  
       
   920 	
       
   921 		if(index==0) // creates a new  CPKCS9ExtensionRequestAttr object.
       
   922 		{
       
   923 			iV3ExtensionsCollection = CPKCS9ExtensionRequestAttr::NewL(*v3ExtensionAttr);	
       
   924 		}
       
   925 		else // adds extension to existing CPKCS9ExtensionRequestAttr.
       
   926 		{
       
   927 			iV3ExtensionsCollection->AddExtensionL(*v3ExtensionAttr);	
       
   928 		}
       
   929 		CleanupStack::PopAndDestroy(v3ExtensionAttr);
       
   930 		CleanupStack::PopAndDestroy(); // rawExtensionValue			
       
   931 		CleanupStack::PopAndDestroy(); // file
       
   932 		index++;
       
   933 	}
       
   934 	
       
   935 	if(numV3ExtensionAttr>0)
       
   936 	{
       
   937 		// Add extension attributes to collection of attributes.
       
   938 		iAttrCollection->AddPKCSAttributeL(iV3ExtensionsCollection);	
       
   939 	}
       
   940 		else
       
   941 	{
       
   942 		INFO_PRINTF1(_L("Extension requests not found nor added"));
       
   943 	}
       
   944 
       
   945 }
       
   946 
       
   947 TAlgorithmId CCertificateRequestStep::ConvertNameToDigestId(const TDesC& aName)
       
   948 {
       
   949 	if (aName.Compare(_L("SHA1"))==0)
       
   950 		{
       
   951 		return ESHA1;	
       
   952 		}
       
   953 	else if (aName.Compare(_L("MD2"))==0)
       
   954 			{
       
   955 			return EMD2;
       
   956 			}
       
   957 		 else if (aName.Compare(_L("MD5"))==0)
       
   958 			{
       
   959 			return EMD5;
       
   960 			}
       
   961 			else
       
   962 			 { //invalid algorithm
       
   963 			 return TAlgorithmId(7);	
       
   964 			 }
       
   965 }
       
   966 TInt  CCertificateRequestStep::ConvertNameToEKeyAlgorithm(const TDesC& aName)
       
   967 {
       
   968 	if (aName.Compare(_L("RSA"))==0)
       
   969 			{
       
   970 			return CCTKeyInfo::ERSA;
       
   971 			}
       
   972 			else if (aName.Compare(_L("DSA"))==0)
       
   973 			{
       
   974 				return CCTKeyInfo::EDSA;
       
   975 			}
       
   976 				else
       
   977 				 { //invalid algorithm
       
   978 					 return 7;	
       
   979 				 }
       
   980 
       
   981 }
       
   982 	
       
   983 void CCertificateRequestStep::OutputEncodingToFileL(const TDesC8& aEncoding)
       
   984 {
       
   985 	INFO_PRINTF1(_L("Writting encoding to file"));
       
   986 	
       
   987 	_LIT(KPath, "c:\\tpkcs10\\myresults\\");
       
   988 	TInt err=iFs.MkDir(KPath);
       
   989 	if (err!=KErrNone && err!=KErrAlreadyExists)
       
   990 	{
       
   991 		User::Leave(err);	 
       
   992 	}
       
   993 
       
   994 	_LIT(KExtension, ".der");
       
   995 	TFileName rName;
       
   996 	rName.Append(KPath);
       
   997 	rName.Append(ConfigSection());
       
   998 	rName.Append(KExtension);
       
   999 	rName.LowerCase();
       
  1000 		
       
  1001 	RFile file;
       
  1002 	CleanupClosePushL(file);
       
  1003 	User::LeaveIfError(file.Replace(iFs, rName, EFileWrite | EFileStream));
       
  1004 	User::LeaveIfError(file.Write(aEncoding));
       
  1005 	CleanupStack::PopAndDestroy(&file);
       
  1006 }
       
  1007 	
       
  1008 	
       
  1009 TBool CPKCS10TesterActive::CompareRequestToOPENSSLReqL()
       
  1010 {
       
  1011 	RFile file;
       
  1012 	TFileName fileName;
       
  1013 	fileName = iStepPointer->iOPENSSLCertReq;
       
  1014 	User::LeaveIfError(file.Open(iFs, fileName, EFileRead));
       
  1015 	CleanupClosePushL(file);
       
  1016 	TInt size;
       
  1017 	User::LeaveIfError(file.Size(size));
       
  1018 	HBufC8* buf = HBufC8::NewMaxLC(size);
       
  1019 	TPtr8 ptr = buf->Des();
       
  1020 	User::LeaveIfError(file.Read(ptr));
       
  1021 	TBool result = *iOutputASN1Encoding == *buf;
       
  1022 	CleanupStack::PopAndDestroy(2); // buf, file
       
  1023 	return result;
       
  1024 }
       
  1025 
       
  1026 TBool CCertificateRequestStep::IsMatchingEncodingL(CASN1EncBase* aASN1Enc1, CASN1EncBase* aASN1Enc2)
       
  1027 	{
       
  1028 	TBool result = EFalse;
       
  1029 	
       
  1030 	// Check the length first
       
  1031 	TInt lenEnc1 = aASN1Enc1->LengthDER();
       
  1032 	TInt lenEnc2 = aASN1Enc2->LengthDER();
       
  1033 	if (lenEnc1 == lenEnc2)
       
  1034 		{
       
  1035 		// Get the encoding and compare them
       
  1036 		HBufC8* enc1Buf = HBufC8::NewMaxLC(lenEnc1);
       
  1037 		HBufC8* enc2Buf = HBufC8::NewMaxLC(lenEnc2);
       
  1038 		TPtr8 enc1Ptr(enc1Buf->Des());
       
  1039 		TPtr8 enc2Ptr(enc2Buf->Des());
       
  1040 		TUint pos1 = 0, pos2 = 0;
       
  1041 
       
  1042 		aASN1Enc1->WriteDERL(enc1Ptr, pos1);
       
  1043 		aASN1Enc2->WriteDERL(enc2Ptr, pos2);
       
  1044 
       
  1045 		result = (*enc1Buf == *enc2Buf);
       
  1046 		CleanupStack::PopAndDestroy(2, enc1Buf);
       
  1047 		}
       
  1048 	else
       
  1049 		{
       
  1050 		result = EFalse;
       
  1051 		}
       
  1052 
       
  1053 	return result;
       
  1054 	}