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