First public contribution.
2 * Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
15 * Implementation of reuse test cases
20 #include "tpkcs10minortests.h"
22 _LIT(KKeyLabel, "new pkcs10 test key");
25 _LIT(KPanicReuseTests, "PKCS10ReuseTests");
26 TInt EPanicInvalidTestData = 1;
28 void CPKCS10ReuseAttributeStep::doTestL()
30 // Set it to pass initially
31 SetTestStepResult(EPass);
33 //////////////////////////////////////////////////////////////////////////////
34 // Test for reuse case of CPKCS10Attribute class
35 TInt numAttr = iArrayGenAttrOID.Count();
36 __ASSERT_ALWAYS((numAttr > 0), User::Panic(KPanicReuseTests, EPanicInvalidTestData));
38 HBufC8 *string = HBufC8::NewMaxLC(iArrayGenAttrValue[0].Length());
39 string->Des().Copy(iArrayGenAttrValue[0]);
41 CASN1EncOctetString* attrString1 = CASN1EncOctetString::NewLC(*string);
43 CPKCS10Attribute *genericAttr = CPKCS10Attribute::NewLC(iArrayGenAttrOID[0], attrString1);
46 CASN1EncBase* attrEncoding1 = genericAttr->GetEncodingLC();
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();
54 // Compare the encodings
55 TBool result = IsMatchingEncodingL(attrEncoding1, attrEncoding2);
57 CleanupStack::PopAndDestroy(3, genericAttr); // attrEncoding1, attrEncoding2
58 CleanupStack::Pop(attrString1);
59 CleanupStack::PopAndDestroy(string);
63 SetTestStepResult(EFail);
67 void CPKCS10ReuseChallengePasswordAttrStep::doTestL()
69 // Set it to pass initially
70 SetTestStepResult(EPass);
72 //////////////////////////////////////////////////////////////////////////////
73 // Test for reuse case of CPKCS9ChallengePasswordAttr class
74 HBufC8 *passwordString = HBufC8::NewMaxLC(iChallengePassword.Length());
75 passwordString->Des().Copy(iChallengePassword);
77 CPKCS9ChallengePasswordAttr *chPasswordAttr = CPKCS9ChallengePasswordAttr::NewLC(*passwordString);
80 CASN1EncBase* attrEncoding1 = chPasswordAttr->GetEncodingLC();
82 // Reset and reuse the class
83 chPasswordAttr->ResetL(*passwordString);
84 CASN1EncBase* attrEncoding2 = chPasswordAttr->GetEncodingLC();
86 // Compare the encodings
87 TBool result = IsMatchingEncodingL(attrEncoding1, attrEncoding2);
89 CleanupStack::PopAndDestroy(4, passwordString); // chPasswordAttr, attrEncoding1, attrEncoding2
93 SetTestStepResult(EFail);
97 void CPKCS10ReuseExtensionAttrStep::doTestL()
99 // Set it to pass initially
100 SetTestStepResult(EPass);
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],
110 HBufC8 *extValue2 = HBufC8::NewMaxLC(iArrayV3AttrValue[1].Length());
111 extValue2->Des().Copy(iArrayV3AttrValue[1]);
112 CX509CertExtension *v3ExtAttr2 = CX509CertExtension::NewLC(iArrayV3AttrOID[1],
113 iArrayV3AttrCritical[1],
116 CPKCS9ExtensionRequestAttr *extReqAttr = CPKCS9ExtensionRequestAttr::NewLC(*v3ExtAttr1);
117 extReqAttr->AddExtensionL(*v3ExtAttr2);
120 CASN1EncBase* attrEncoding1 = extReqAttr->GetEncodingLC();
122 // Reset and reuse the class
123 extReqAttr->ResetL(*v3ExtAttr1);
124 extReqAttr->AddExtensionL(*v3ExtAttr2);
125 CASN1EncBase* attrEncoding2 = extReqAttr->GetEncodingLC();
127 // Compare the encodings
128 TBool result = IsMatchingEncodingL(attrEncoding1, attrEncoding2);
130 CleanupStack::PopAndDestroy(7, extValue1); // v3ExtAttr1, extValue2, v3ExtAttr2, extReqAttr, attrEncoding1, attrEncoding2
132 if (result == EFalse)
134 SetTestStepResult(EFail);
138 CPKCS10TesterActiveCancelRequestA::CPKCS10TesterActiveCancelRequestA( CTestExecuteLogger& aLogger ) :
139 CPKCS10TesterActive( aLogger )
143 void CPKCS10CancelRequestAStep::doTestL()
146 iSched=new(ELeave) CActiveScheduler;
147 CleanupStack::PushL(iSched);
148 CActiveScheduler::Install(iSched);
150 iActiveObjTest = new (ELeave) CPKCS10TesterActiveCancelRequestA(Logger());
151 CleanupStack::PushL(iActiveObjTest);
153 if (iActiveObjTest->doActiveCertRequestL(this) != EPass)
155 SetTestStepResult(EFail);
158 CleanupStack::PopAndDestroy(iActiveObjTest);
159 iActiveObjTest = NULL;
160 CleanupStack::PopAndDestroy(iSched);
166 TVerdict CPKCS10TesterActiveCancelRequestA::doActiveCertRequestL(CCertificateRequestStep* aStep)
170 INFO_PRINTF1(_L("Active tester for Cert Request started. "));
171 iStepPointer = aStep;
175 INFO_PRINTF1(_L("initialising keystore"));
177 // Initialise Key store.
178 iKeyStore = CUnifiedKeyStore::NewL(iFs);
179 CleanupStack::PushL(iKeyStore);
180 iKeyStore->Initialize(iStatus);
181 iState = EInitKeyStore;
183 CActiveScheduler::Start();
185 // After encoding was produced it checks correctness
186 if(iRunError!=KErrCancel)
195 CleanupStack::Pop(iKeyStore);
199 void CPKCS10TesterActiveCancelRequestA::RunL()
204 User::LeaveIfError(iStatus.Int());
210 INFO_PRINTF1(_L(" listing existing keys\n"));
211 iKeyStore->List(iKeys, iKeyFilter, iStatus);
212 iState = EDeleteAllDelete;
216 case EDeleteAllDelete:
217 if (iKeys.Count() == 0)
221 CActiveScheduler::Stop();
225 INFO_PRINTF1(_L(" deleting key\n"));
226 iKeyStore->DeleteKey(*iKeys[0], iStatus);
227 iState = EDeleteAllDelete;
235 INFO_PRINTF1(_L("Importing keys"));
237 filename = iStepPointer->iPrivateKey;
239 User::LeaveIfError(file.Open(iFs,filename,EFileRead));
240 CleanupClosePushL(file);
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
251 // Assumes only one keystore
253 ASSERT(iKeyInfo == NULL);
254 iKeyStore->ImportKey(0, *(iKeyData), EPKCS15UsageSign, KKeyLabel,0, start, end,iKeyInfo, iStatus);
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);
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"));
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"));
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;
300 iCertRequest->Cancel();
306 INFO_PRINTF1(_L("Cert Request Active tester: State corrupted."));
307 User::Leave(KErrCorrupt);