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