cryptoservices/certificateandkeymgmt/tpkcs10/tpkcs10minortests.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 * Implementation of reuse test cases
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "tpkcs10minortests.h"
       
    21 
       
    22 _LIT(KKeyLabel, "new pkcs10 test key"); 
       
    23 
       
    24 // Panics
       
    25 _LIT(KPanicReuseTests, "PKCS10ReuseTests");
       
    26 TInt EPanicInvalidTestData = 1;
       
    27 
       
    28 void CPKCS10ReuseAttributeStep::doTestL()
       
    29 	{
       
    30 	// Set it to pass initially
       
    31 	SetTestStepResult(EPass);
       
    32 
       
    33 	//////////////////////////////////////////////////////////////////////////////
       
    34 	// Test for reuse case of CPKCS10Attribute class
       
    35  	TInt numAttr = iArrayGenAttrOID.Count();
       
    36  	__ASSERT_ALWAYS((numAttr > 0), User::Panic(KPanicReuseTests, EPanicInvalidTestData));
       
    37 
       
    38 	HBufC8 *string = HBufC8::NewMaxLC(iArrayGenAttrValue[0].Length());
       
    39 	string->Des().Copy(iArrayGenAttrValue[0]);
       
    40 
       
    41 	CASN1EncOctetString* attrString1 = CASN1EncOctetString::NewLC(*string);
       
    42 
       
    43 	CPKCS10Attribute *genericAttr = CPKCS10Attribute::NewLC(iArrayGenAttrOID[0], attrString1);
       
    44 
       
    45 	// Get the encoding
       
    46 	CASN1EncBase* attrEncoding1 = genericAttr->GetEncodingLC();
       
    47 
       
    48 	// Reset and reuse the class
       
    49 	CASN1EncOctetString* attrString2 = CASN1EncOctetString::NewLC(*string);
       
    50 	genericAttr->ResetL(iArrayGenAttrOID[0], attrString2);
       
    51 	CleanupStack::Pop(attrString2);
       
    52 	CASN1EncBase* attrEncoding2 = genericAttr->GetEncodingLC();
       
    53 
       
    54 	// Compare the encodings
       
    55 	TBool result = IsMatchingEncodingL(attrEncoding1, attrEncoding2);
       
    56 
       
    57 	CleanupStack::PopAndDestroy(3, genericAttr);	// attrEncoding1, attrEncoding2
       
    58 	CleanupStack::Pop(attrString1);
       
    59 	CleanupStack::PopAndDestroy(string);
       
    60 
       
    61 	if (result == EFalse)
       
    62 		{
       
    63 		SetTestStepResult(EFail);
       
    64 		}
       
    65 	}
       
    66 
       
    67 void CPKCS10ReuseChallengePasswordAttrStep::doTestL()
       
    68 	{
       
    69 	// Set it to pass initially
       
    70 	SetTestStepResult(EPass);
       
    71 
       
    72 	//////////////////////////////////////////////////////////////////////////////
       
    73 	// Test for reuse case of CPKCS9ChallengePasswordAttr class
       
    74 	HBufC8 *passwordString = HBufC8::NewMaxLC(iChallengePassword.Length());
       
    75 	passwordString->Des().Copy(iChallengePassword);
       
    76 
       
    77 	CPKCS9ChallengePasswordAttr *chPasswordAttr = CPKCS9ChallengePasswordAttr::NewLC(*passwordString);
       
    78 
       
    79 	// Get the encoding
       
    80 	CASN1EncBase* attrEncoding1 = chPasswordAttr->GetEncodingLC();
       
    81 
       
    82 	// Reset and reuse the class
       
    83 	chPasswordAttr->ResetL(*passwordString);
       
    84 	CASN1EncBase* attrEncoding2 = chPasswordAttr->GetEncodingLC();
       
    85 
       
    86 	// Compare the encodings
       
    87 	TBool result = IsMatchingEncodingL(attrEncoding1, attrEncoding2);
       
    88 
       
    89 	CleanupStack::PopAndDestroy(4, passwordString);	// chPasswordAttr, attrEncoding1, attrEncoding2
       
    90 
       
    91 	if (result == EFalse)
       
    92 		{
       
    93 		SetTestStepResult(EFail);
       
    94 		}
       
    95 	}
       
    96 
       
    97 void CPKCS10ReuseExtensionAttrStep::doTestL()
       
    98 	{
       
    99 	// Set it to pass initially
       
   100 	SetTestStepResult(EPass);
       
   101 
       
   102 	//////////////////////////////////////////////////////////////////////////////
       
   103 	// Test for reuse case of CPKCS10Attribute class
       
   104 	HBufC8 *extValue1 = HBufC8::NewMaxLC(iArrayV3AttrValue[0].Length());
       
   105 	extValue1->Des().Copy(iArrayV3AttrValue[0]);
       
   106 	CX509CertExtension *v3ExtAttr1 = CX509CertExtension::NewLC(iArrayV3AttrOID[0], 
       
   107 												 iArrayV3AttrCritical[0],
       
   108 												 *extValue1);
       
   109 
       
   110 	HBufC8 *extValue2 = HBufC8::NewMaxLC(iArrayV3AttrValue[1].Length());
       
   111 	extValue2->Des().Copy(iArrayV3AttrValue[1]);
       
   112 	CX509CertExtension *v3ExtAttr2 = CX509CertExtension::NewLC(iArrayV3AttrOID[1], 
       
   113 												 iArrayV3AttrCritical[1],
       
   114 												 *extValue2);
       
   115 
       
   116 	CPKCS9ExtensionRequestAttr *extReqAttr = CPKCS9ExtensionRequestAttr::NewLC(*v3ExtAttr1);
       
   117 	extReqAttr->AddExtensionL(*v3ExtAttr2);
       
   118 
       
   119 	// Get the encoding
       
   120 	CASN1EncBase* attrEncoding1 = extReqAttr->GetEncodingLC();
       
   121 
       
   122 	// Reset and reuse the class
       
   123 	extReqAttr->ResetL(*v3ExtAttr1);
       
   124 	extReqAttr->AddExtensionL(*v3ExtAttr2);
       
   125 	CASN1EncBase* attrEncoding2 = extReqAttr->GetEncodingLC();
       
   126 
       
   127 	// Compare the encodings
       
   128 	TBool result = IsMatchingEncodingL(attrEncoding1, attrEncoding2);
       
   129 
       
   130 	CleanupStack::PopAndDestroy(7, extValue1);	// v3ExtAttr1, extValue2, v3ExtAttr2, extReqAttr, attrEncoding1, attrEncoding2
       
   131 
       
   132 	if (result == EFalse)
       
   133 		{
       
   134 		SetTestStepResult(EFail);
       
   135 		}
       
   136 	}
       
   137 	
       
   138 CPKCS10TesterActiveCancelRequestA::CPKCS10TesterActiveCancelRequestA( CTestExecuteLogger& aLogger ) : 
       
   139    CPKCS10TesterActive( aLogger )
       
   140 	{
       
   141 	}
       
   142 	
       
   143 void CPKCS10CancelRequestAStep::doTestL()
       
   144 {
       
   145     
       
   146     iSched=new(ELeave) CActiveScheduler; 
       
   147     CleanupStack::PushL(iSched);  
       
   148 	CActiveScheduler::Install(iSched);
       
   149 	
       
   150 	iActiveObjTest = new (ELeave) CPKCS10TesterActiveCancelRequestA(Logger());
       
   151 	CleanupStack::PushL(iActiveObjTest);
       
   152 
       
   153 	if (iActiveObjTest->doActiveCertRequestL(this) != EPass)
       
   154 	 {
       
   155 	 	SetTestStepResult(EFail);
       
   156 	 }
       
   157 	 
       
   158 	 CleanupStack::PopAndDestroy(iActiveObjTest);
       
   159 	 iActiveObjTest = NULL;
       
   160 	 CleanupStack::PopAndDestroy(iSched);
       
   161 	 iSched=NULL;
       
   162 	 
       
   163  }
       
   164 
       
   165 	
       
   166 TVerdict CPKCS10TesterActiveCancelRequestA::doActiveCertRequestL(CCertificateRequestStep* aStep)
       
   167 	{
       
   168 	
       
   169 	iTestSuccess= EFail;
       
   170 	INFO_PRINTF1(_L("Active tester for Cert Request started. "));
       
   171 	iStepPointer = aStep;
       
   172 		
       
   173     DeleteAllKeysL();
       
   174 	
       
   175     INFO_PRINTF1(_L("initialising keystore"));
       
   176     
       
   177 	// Initialise Key store.
       
   178   	iKeyStore = CUnifiedKeyStore::NewL(iFs);
       
   179   	CleanupStack::PushL(iKeyStore); 
       
   180   	iKeyStore->Initialize(iStatus);  
       
   181 	iState = EInitKeyStore; 
       
   182 	SetActive();
       
   183    	CActiveScheduler::Start();
       
   184    	
       
   185    	// After encoding was produced it checks correctness
       
   186     if(iRunError!=KErrCancel)
       
   187      {
       
   188 	     iTestSuccess= EFail;
       
   189 	 }
       
   190 	 else
       
   191 	 {
       
   192 	 	iTestSuccess= EPass;
       
   193 	 }
       
   194   	
       
   195    	CleanupStack::Pop(iKeyStore);  
       
   196     return iTestSuccess;
       
   197 }
       
   198 
       
   199 void CPKCS10TesterActiveCancelRequestA::RunL()
       
   200 {
       
   201 	
       
   202 	iRunError =KErrNone;
       
   203 	
       
   204  	User::LeaveIfError(iStatus.Int());
       
   205    
       
   206 	switch(iState)
       
   207 		{
       
   208 		
       
   209 		case EDeleteAllInit:
       
   210 			INFO_PRINTF1(_L("  listing existing keys\n"));
       
   211 			iKeyStore->List(iKeys, iKeyFilter, iStatus);
       
   212 			iState = EDeleteAllDelete;
       
   213 			SetActive();
       
   214 			break;
       
   215 			
       
   216 		case EDeleteAllDelete:
       
   217 			if (iKeys.Count() == 0)
       
   218 				{
       
   219 				// key log is empty
       
   220 				iKeys.Close();
       
   221 				CActiveScheduler::Stop();
       
   222 				break;
       
   223 				}
       
   224 			
       
   225 			INFO_PRINTF1(_L("  deleting key\n"));
       
   226 			iKeyStore->DeleteKey(*iKeys[0], iStatus);
       
   227 			iState = EDeleteAllDelete;
       
   228 			SetActive();
       
   229 			iKeys[0]->Release();
       
   230 			iKeys.Remove(0);
       
   231 			break;
       
   232 		
       
   233 		case EInitKeyStore:
       
   234 			{
       
   235 	  		INFO_PRINTF1(_L("Importing keys"));
       
   236  			TFileName filename;
       
   237  			filename = iStepPointer->iPrivateKey;
       
   238 			RFile file;
       
   239 			User::LeaveIfError(file.Open(iFs,filename,EFileRead));
       
   240 			CleanupClosePushL(file);
       
   241 			TInt size;
       
   242 			User::LeaveIfError(file.Size(size));
       
   243 			iKeyData = HBufC8::NewMaxL(size);
       
   244 			TPtr8 keyPtr = iKeyData->Des();
       
   245 			User::LeaveIfError(file.Read(keyPtr));			
       
   246 			CleanupStack::PopAndDestroy(); // file
       
   247 
       
   248 			TTime start(0.0); 
       
   249 			TTime end(0.0); 
       
   250 			
       
   251 			// Assumes only one keystore
       
   252 			// Check parameters!
       
   253 		 	ASSERT(iKeyInfo == NULL);      
       
   254 			iKeyStore->ImportKey(0, *(iKeyData), EPKCS15UsageSign, KKeyLabel,0, start, end,iKeyInfo, iStatus);
       
   255 	  		iState = EImportKey;
       
   256 	  		SetActive();
       
   257 			break;
       
   258 			}
       
   259 	  	case EImportKey:
       
   260 			{
       
   261 			INFO_PRINTF1(_L("Setting security policy for new stored key"));
       
   262 			TSecureId secureId(0x101f7784); // Application secure ID 
       
   263 			TSecurityPolicy securePolicy(secureId,ECapabilityReadUserData);
       
   264 			iKeyStore->SetUsePolicy(iKeyInfo->Handle(),securePolicy,iStatus);
       
   265 			iState = EKeyPolicy;
       
   266  			SetActive();
       
   267  			break;
       
   268 		   	}
       
   269 		case EKeyPolicy:
       
   270 			{
       
   271 			iAttrCollection=CPKCS10Attributes::NewL();
       
   272  			CleanupStack::PushL(iAttrCollection); 
       
   273 			INFO_PRINTF1(_L("Adding generic attributes"));		
       
   274  			AddGenericAttributesL();
       
   275  			INFO_PRINTF1(_L("Adding Challenge password"));
       
   276  			AddChallengePasswordL();
       
   277  			INFO_PRINTF1(_L("Adding V3 extensions"));
       
   278  			AddV3ExtensionsL();
       
   279  			INFO_PRINTF1(_L("Generating distinguished name"));
       
   280  			iDN=MakeDistinguishedNameL();
       
   281  			CleanupStack::PushL(iDN);
       
   282    	 		INFO_PRINTF1(_L("Generating cert request"));
       
   283    	 		iCertRequest=CPKCS10Request::NewL(*iDN,*iKeyInfo,iAttrCollection);
       
   284      		CleanupStack::PushL(iCertRequest);
       
   285      		INFO_PRINTF1(_L("Setting digest algorithm"));
       
   286 			TAlgorithmId digestAlgo=iStepPointer->ConvertNameToDigestId(iStepPointer->iDigestAlg);
       
   287 			iCertRequest->SetDigestAlgL(digestAlgo);
       
   288  			INFO_PRINTF1(_L("Requesting cert request encoding"));
       
   289  	  		
       
   290  	  		// Clean up
       
   291  	  		CleanupStack::Pop(iCertRequest);
       
   292  			CleanupStack::Pop(iDN);
       
   293     	 	CleanupStack::Pop(iAttrCollection);
       
   294     	  	iAttrCollection=NULL;  
       
   295 			iOutputASN1Encoding=NULL;
       
   296 			iCertRequest->CreateEncoding(iOutputASN1Encoding,iStatus);
       
   297 			iState=EGenerateCertRequest; 
       
   298 			SetActive();
       
   299 			
       
   300 			iCertRequest->Cancel();
       
   301 		
       
   302 	  		break;	
       
   303 			}
       
   304 		default:
       
   305 			{
       
   306 		  	INFO_PRINTF1(_L("Cert Request Active tester: State corrupted."));
       
   307 			User::Leave(KErrCorrupt);
       
   308 			}
       
   309  		} 
       
   310  		
       
   311    	return; 
       
   312 }