First public contribution.
2 * Copyright (c) 1998-2010 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.
19 #include "taction_build.h"
20 #include "t_inputextra.h"
22 _LIT(KRoot, "build\\CA3_root.crt");
23 _LIT(KCA2, "build\\CA2.crt");
24 _LIT(KCA1, "build\\CA1.crt");
25 _LIT(KEE, "build\\EE.crt");
27 _LIT(KRootStart, "<root>");
28 _LIT(KRootEnd, "</root>");
29 _LIT(KCertificateStart, "<certificate>");
30 _LIT(KCertificateEnd, "</certificate>");
31 _LIT(KLabelStart, "<label>");
32 _LIT(KLabelEnd, "</label>");
33 _LIT(KExtraStart, "<extra>");
34 _LIT(KExtraEnd, "</extra>");
35 _LIT(KInitStart, "<init>");
36 _LIT(KInitEnd, "</init>");
37 _LIT(KAddCallStart, "<addcall>");
38 _LIT(KAddCallEnd, "</addcall>");
40 _LIT(KAddCertStart, "<addcert>");
41 _LIT(KAddCertEnd, "</addcert>");
43 _LIT(KCertPath, "\\pkixtestdata\\");
45 CAddCall* CAddCall::NewL(const TDesC& aBuf)
47 CAddCall* self = CAddCall::NewLC(aBuf);
52 CAddCall* CAddCall::NewLC(const TDesC& aBuf)
54 CAddCall* self = new(ELeave) CAddCall;
55 CleanupStack::PushL(self);
56 self->ConstructL(aBuf);
69 void CAddCall::ConstructL(const TDesC& aBuf)
71 iCertsToAdd = new(ELeave) CDesCArrayFlat (1);
73 while(AddCert(aBuf, KAddCertStart, KAddCertEnd, pos, *iCertsToAdd))
78 TBool CAddCall::AddCert(const TDesC& aBuf, const TDesC& aStart, const TDesC& aEnd, TInt& aPos, CDesCArray& aCerts)
80 TPtrC certBuf = Input::ParseElement(aBuf, aStart, aEnd, aPos);
81 if (certBuf != KNullDesC)
83 aCerts.AppendL(certBuf);
90 CTestAction* CTestActionBuild::NewL(RFs& aFs,
91 CConsoleBase& aConsole,
93 const TTestActionSpec& aTestActionSpec)
95 CTestAction* self = CTestActionBuild::NewLC(aFs, aConsole,
96 aOut, aTestActionSpec);
97 CleanupStack::Pop(self);
101 CTestAction* CTestActionBuild::NewLC(RFs& aFs,
102 CConsoleBase& aConsole,
104 const TTestActionSpec& aTestActionSpec)
106 CTestActionBuild* self = new(ELeave) CTestActionBuild(aFs, aConsole,
108 CleanupStack::PushL(self);
109 self->ConstructL(aTestActionSpec);
113 CTestActionBuild::~CTestActionBuild()
115 delete iUnifiedCertStore;
118 delete iExtraCertsFileName;
119 delete iExtraCertsLabel;
120 delete iInitCertsFileName;
121 delete iInitCertsLabel;
122 iAddCalls.ResetAndDestroy();
124 delete iValidationResult;
127 CTestActionBuild::CTestActionBuild(RFs& aFs,
128 CConsoleBase& aConsole,
130 : CTestAction(aConsole, aOut), iFs(aFs)
134 void CTestActionBuild::ConstructL(const TTestActionSpec& aTestActionSpec)
136 CTestAction::ConstructL(aTestActionSpec);
140 HBufC* aBody = HBufC::NewLC(aTestActionSpec.iActionBody.Length());
141 aBody->Des().Copy(aTestActionSpec.iActionBody);
142 TPtrC rootCert(Input::ParseElement(*aBody,
143 KRootStart, KRootEnd, pos, err));
145 iRootCertFileName = Input::ParseElement(rootCert, KCertificateStart,
146 KCertificateEnd, dummyPos);
147 iRootCertLabel = Input::ParseElement(rootCert, KLabelStart,
148 KLabelEnd, dummyPos);
150 iExtraCertsFileName = new(ELeave) CDesCArrayFlat(1);
151 iExtraCertsLabel = new(ELeave) CDesCArrayFlat(1);
152 while(AddCert(*aBody, KExtraStart, KExtraEnd, pos, *iExtraCertsFileName,
157 iInitCertsFileName = new(ELeave) CDesCArrayFlat(1);
158 iInitCertsLabel = new(ELeave) CDesCArrayFlat(1);
159 while(AddCert(*aBody, KInitStart, KInitEnd, pos, *iInitCertsFileName,
164 while(AddAddCallsL(*aBody, pos))
168 iValidationResult = CPKIXValidationResult::NewL();
170 TDriveUnit sysDrive (RFs::GetSystemDrive());
171 TDriveName driveName(sysDrive.Name());
172 iCertPath.Copy(driveName);
173 iCertPath.Append(KCertPath);
175 CleanupStack::PopAndDestroy(aBody);
178 void CTestActionBuild::DoPerformPrerequisite(TRequestStatus& aStatus)
180 iActionState = EAction;
181 TRequestStatus* status = &aStatus;
182 User::RequestComplete(status, KErrNone);
185 void CTestActionBuild::DoPerformPostrequisite(TRequestStatus& aStatus)
187 TRequestStatus* status = &aStatus;
188 User::RequestComplete(status, KErrNone);
191 void CTestActionBuild::PerformAction(TRequestStatus& aStatus)
195 case EInitCertStoreManager1:
196 __ASSERT_DEBUG(!iUnifiedCertStore, User::Panic(_L("CPKIXCertTest"), 1));
197 iUnifiedCertStore = CUnifiedCertStore::NewL(iFs, ETrue); // We open the store for writing
198 iUnifiedCertStore->Initialize(aStatus);
199 iState = ERemoveCertsBeforeTest;
202 case ERemoveCertsBeforeTest:
203 // A new iCertUtils is created and destroyed for each test because
204 // we need to close before validating as the chain won't be able to
205 // open the store manager if it is already open for write elsewhere
206 __ASSERT_DEBUG(!iCertUtils, User::Panic(_L("CPKIXCertTest"), 1));
207 iCertUtils = CCertUtils::NewL(iFs);
208 iCertUtils->RemoveCertsL(*iUnifiedCertStore, aStatus);
215 TRAPD(err, iCertUtils->RemoveApplicationL(uid));
216 iCertUtils->AddApplicationL(_L("testpkix"), uid);
218 TRAP(err, iCertUtils->RemoveApplicationL(uid2));
219 iCertUtils->AddApplicationL(_L("testpkix"), uid2);
220 iCertUtils->AddCert(iRootCertLabel, EX509Certificate, ECACertificate,
221 1, iCertPath, iRootCertFileName, *iUnifiedCertStore, aStatus);//1 is trusted for our use
222 iState = EAddIntermediateCerts;
226 case EAddIntermediateCerts:
227 iCertUtils->AddCACertsL(*iExtraCertsFileName,
228 *iExtraCertsLabel, EX509Certificate,
229 2, iCertPath, *iUnifiedCertStore, aStatus); // 2 not trusted for our use
230 iState = EDoBuildTestStart;
233 case EDoBuildTestStart:
235 // We have to close the store manager because it is open for write
236 // and CPKIXCertChain won't be able to open it if we don't close it
237 // iCertUtils muts also be deleted since it uses this store manager
240 delete iUnifiedCertStore;
241 iUnifiedCertStore = 0;
243 __ASSERT_DEBUG(!iChain, User::Panic(_L("CPKIXCertTest"), 1));
246 InputExtra::ReadFilesLC(*iInitCertsFileName, iCertPath, iFs);
247 TUid testUid = TUid::Uid(1);
248 iChain = CPKIXCertChain::NewL(iFs, *initCerts, testUid);
249 CleanupStack::PopAndDestroy(initCerts); // initCerts
251 TInt addCount = iAddCalls.Count();
252 for (TInt i = 0; i < addCount; i++)
254 const CAddCall* addCall = iAddCalls[i];
255 HBufC8* addCerts = InputExtra::ReadFilesLC(*(addCall->iCertsToAdd),
257 // the root of the chain is set when the certificate chain is getting created through the CPKIXCertChainAO
258 // via CPKIXChainBuilder. If the chain has the root certificate then the iChainHasRoot is set which is returned
259 // through this method. If the corresponding certificate does not have the issuer certificate in the chain then
260 // the extra certificates provided in the test script would be added for completing the chain.
261 if (!(iChain->ChainHasRoot()))
263 iChain->AddCertL(*addCerts);
265 CleanupStack::PopAndDestroy(); // addCerts
270 iChain->ValidateL(*iValidationResult, iTime, aStatus);
272 iState = EDoBuildTestFinished;
276 case EDoBuildTestFinished:
278 iResult = CompareChainsL(*iChain);
281 iState = EInitCertStoreManager2;
282 TRequestStatus* status = &aStatus;
283 User::RequestComplete(status, KErrNone);
287 case EInitCertStoreManager2:
288 __ASSERT_DEBUG(!iChain, User::Panic(_L("CPKIXCertTest"), 1));
289 //TRAP(err, HandleEDoBuildTests_SingleTest_InitCertStoreManager2L());
290 iUnifiedCertStore = CUnifiedCertStore::NewL(iFs, ETrue); // We open the store for writing
291 iUnifiedCertStore->Initialize(aStatus);
292 iState = ERemoveCertsAfterTest;
295 case ERemoveCertsAfterTest:
296 __ASSERT_DEBUG(!iCertUtils, User::Panic(_L("CPKIXCertTest"), 1));
297 iCertUtils = CCertUtils::NewL(iFs);
298 iCertUtils->RemoveCertsL(*iUnifiedCertStore, aStatus);
306 delete iUnifiedCertStore;
307 iUnifiedCertStore = 0;
308 TRequestStatus* status = &aStatus;
310 User::RequestComplete(status, KErrNone);
316 TBool CTestActionBuild::TestResult(TInt /*aError*/)
321 void CTestActionBuild::PerformCancel()
325 void CTestActionBuild::Reset()
329 void CTestActionBuild::DoReportAction()
331 iConsole.Printf(_L("u"));
334 void CTestActionBuild::DoCheckResult(TInt /*aError*/)
338 TBool CTestActionBuild::AddCert(const TDesC& aBuf,
340 const TDesC& aEnd, TInt& aPos,
341 CDesCArray& aCertsFileName,
342 CDesCArray& aCertsLabel)
344 TPtrC certBuf = Input::ParseElement(aBuf, aStart, aEnd, aPos);
346 if (certBuf != KNullDesC)
348 aCertsFileName.AppendL(Input::ParseElement(certBuf, KCertificateStart,
349 KCertificateEnd, dummyPos));
350 aCertsLabel.AppendL(Input::ParseElement(certBuf, KLabelStart,
351 KLabelEnd, dummyPos));
357 TBool CTestActionBuild::AddAddCallsL(const TDesC& aBuf, TInt& aPos)
359 TPtrC addCallsBuf = Input::ParseElement(aBuf, KAddCallStart, KAddCallEnd, aPos);
360 if (addCallsBuf != KNullDesC)
362 CAddCall* addCall = CAddCall::NewLC(addCallsBuf);
363 iAddCalls.AppendL(addCall);
364 CleanupStack::Pop(addCall);
370 TBool CTestActionBuild::CompareChainsL(const CPKIXCertChain& aChain)
372 if (aChain.Count() != 4)
376 if ( (!CompareCertL(aChain.Cert(0), KEE)) ||
377 (!CompareCertL(aChain.Cert(1), KCA1)) ||
378 (!CompareCertL(aChain.Cert(2), KCA2)) ||
379 (!CompareCertL(aChain.Cert(3), KRoot)) )
389 TBool CTestActionBuild::CompareCertL(const CX509Certificate& aCert, const TDesC& aFilename)
391 HBufC8* correct = Input::ReadFileLC(aFilename, iCertPath, iFs);
392 TBool res = (correct->Des() == aCert.Encoding());
393 CleanupStack::PopAndDestroy();