os/security/cryptoservices/filebasedcertificateandkeystores/test/ttesttools/ttesttoolengine.cpp
Update contrib.
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.
19 #include "ttesttoolengine.h"
20 #include <ccertattributefilter.h>
21 #include <mctcertstore.h>
22 #include <mctkeystore.h>
24 const TInt swinstalluid = 268452523;
25 const TInt swinstallocspuid = 268478646;
26 const TInt midletinstalluid = 270506792;
27 const TInt tlsuid = 268441661;
28 const TInt tocspuid = 268477445;
31 _LIT(KSWInstall, "SWInstall");
32 _LIT(KSWInstallOCSP, "SWInstallOCSP");
33 _LIT(KMidletInstall, "MidletInstall");
34 _LIT(KTls, "SSL/TLS");
35 _LIT(KTOcsp, "OCSP Test");
38 _LIT(KUserType, "user");
39 _LIT(KPeerType, "peer");
42 /*static*/ CTestToolEngine* CTestToolEngine::NewLC()
44 CTestToolEngine* self = new (ELeave) CTestToolEngine();
45 CleanupStack::PushL(self);
50 CTestToolEngine::CTestToolEngine() : CActive(EPriorityNormal)
56 CTestToolEngine::~CTestToolEngine()
71 void CTestToolEngine::ConstructL()
73 User::LeaveIfError(iFs.Connect());
75 iCertStore = CUnifiedCertStore::NewL(iFs, ETrue);
77 User::LeaveIfError(iFsKeyStore.Connect());
78 iKeyStore = CUnifiedKeyStore::NewL(iFsKeyStore);
80 // This filter will accept any certificates
81 iFilter = CCertAttributeFilter::NewL();
83 CActiveScheduler::Add(this);
86 void CTestToolEngine::RunL()
88 if (iStatus.Int() != KErrNone)
90 User::Leave(iStatus.Int());
95 case EInitialiseKeyStore:
97 // Construct a catch-all filter!
98 iKeyFilter.iKeyAlgorithm = CCTKeyInfo::EInvalidAlgorithm;
99 iKeyFilter.iPolicyFilter = TCTKeyAttributeFilter::EAllKeys;
100 iKeyFilter.iUsage = EPKCS15UsageAll;
103 MCTKeyStore& chosen = iKeyStore->KeyStore(iStore);
104 chosen.List(iKeys, iKeyFilter, iStatus);
108 iKeyStore->List(iKeys, iKeyFilter, iStatus);
114 else if (iKeysListCount > 0)
128 if (iLabel1.Length())
130 iNumCert1 = FilterKeys(iKeys, iLabel1, index);
132 if (iLabel2.Length())
134 iNumCert2 = FilterKeys(iKeys, iLabel2, index);
136 CActiveScheduler::Stop();
143 MCTCertStore& chosen = iCertStore->CertStore(iStore);
144 chosen.List(iCertInfos, *iFilter, iStatus);
148 iCertStore->List(iCertInfos, *iFilter, iStatus);
155 else if (iGetTrust == 1)
159 else if (iCertsCount > 0)
172 FilterKeys(iKeys, iLabel1, iIndex);
173 CActiveScheduler::Stop();
179 FilterCerts(iCertInfos, iLabel1, index);
180 iCertStore->Applications(*iCertInfos[index], iUids, iStatus);
181 CActiveScheduler::Stop();
187 FilterCerts(iCertInfos, iLabel1, index);
188 iCertStore->Trusted(*iCertInfos[index], iActualTrust, iStatus);
189 CActiveScheduler::Stop();
195 TBool filteronly = ETrue;
196 if (iLabel1.Length())
198 iNumCert1 = FilterCerts(iCertInfos, iLabel1, index, filteronly);
200 if (iLabel2.Length())
202 iNumCert2 = FilterCerts(iCertInfos, iLabel2, index, filteronly);
204 CActiveScheduler::Stop();
209 CActiveScheduler::Stop();
214 User::Panic(_L("Testtool Engine - Illegal state"), 0);
219 TInt CTestToolEngine::RunError(TInt /*aError*/)
221 CActiveScheduler::Stop();
227 void CTestToolEngine::DoCancel()
229 //CActiveScheduler::Stop();
232 TVerdict CTestToolEngine::ListCerts(const TDesC& aLabel1, const TInt aExpectedNumLabel)
234 TVerdict returnValue = EPass;
235 iLabel1.Set(aLabel1);
238 if (iNumCert1 != aExpectedNumLabel)
246 TVerdict CTestToolEngine::ListCerts(const TDesC& aLabel1, const TDesC& aLabel2, const TInt aExpectedNumLabel)
248 TVerdict returnValue = EPass;
249 iLabel1.Set(aLabel1);
250 iLabel2.Set(aLabel2);
253 if (iNumCert1 != aExpectedNumLabel || iNumCert2 != aExpectedNumLabel)
260 TVerdict CTestToolEngine::ListStore(RPointerArray<HBufC>& aCerts)
262 TVerdict returnValue = EPass;
263 iCertsCount = aCerts.Count();
267 if (!CompareWithExpectedStore(aCerts))
274 TVerdict CTestToolEngine::ListCerts(RPointerArray<HBufC>& aCerts)
276 TVerdict returnValue = EPass;
277 iCertsCount = aCerts.Count();
281 if (!CompareWithExpectedCertificates(aCerts))
288 TVerdict CTestToolEngine::ListCerts(RPointerArray<HBufC>& aCerts, const TDesC& aLabel)
290 TVerdict returnValue = EPass;
292 iCertsCount = aCerts.Count();
295 TCertificateOwnerType owner;
296 if (iLabel1.CompareF(KCaType) == KErrNone)
298 owner = ECACertificate;
300 else if (iLabel1.CompareF(KUserType) == KErrNone)
302 owner = EUserCertificate;
304 else if (iLabel1.CompareF(KPeerType) == KErrNone)
306 owner = EPeerCertificate;
308 FilterCerts(iCertInfos, owner);
309 if (!CompareWithExpectedCertificates(aCerts))
316 TVerdict CTestToolEngine::ListCerts(RPointerArray<HBufC>& aCerts, TInt aStore)
318 TVerdict returnValue = EPass;
320 iCertsCount = aCerts.Count();
323 if (!CompareWithExpectedCertificates(aCerts))
330 TVerdict CTestToolEngine::CheckApps(RPointerArray<HBufC>& aApps, const TDesC& aLabel)
332 TVerdict returnValue = EPass;
333 iAppsCount = aApps.Count();
336 if (!VerifyApps(aApps))
343 TVerdict CTestToolEngine::CheckTrust(const TDesC& aLabel,TBool aTrust)
345 TVerdict returnValue = EPass;
349 if (!VerifyTrust(aTrust))
356 TVerdict CTestToolEngine::ListStoreKey(RPointerArray<HBufC>& aKeys)
358 TVerdict returnValue = EPass;
359 iKeysListCount = aKeys.Count();
363 if (!CompareWithExpectedStoreKey(aKeys))
370 TVerdict CTestToolEngine::ListKeys(const TDesC& aLabel1, const TInt aExpectedNumLabel)
372 TVerdict returnValue = EPass;
373 iLabel1.Set(aLabel1);
376 if (iNumCert1 != aExpectedNumLabel)
384 TVerdict CTestToolEngine::ListKeys(const TDesC& aLabel1, const TDesC& aLabel2, const TInt aExpectedNumLabel)
386 TVerdict returnValue = EPass;
387 iLabel1.Set(aLabel1);
388 iLabel2.Set(aLabel2);
391 if (iNumCert1 != aExpectedNumLabel || iNumCert2 != aExpectedNumLabel)
398 TVerdict CTestToolEngine::ListKeys(RPointerArray<HBufC>& aKeys, TInt aStore)
400 TVerdict returnValue = EPass;
402 iKeysListCount = aKeys.Count();
405 if (!CompareWithExpectedKeys(aKeys))
413 TVerdict CTestToolEngine::ListKeys(RPointerArray<HBufC>& aKeys)
415 TVerdict returnValue = EPass;
416 iKeysListCount = aKeys.Count();
420 if (!CompareWithExpectedKeys(aKeys))
427 TVerdict CTestToolEngine::GetPolicy(const TDesC& aLabel1, const TDesC& aLabel2, TInt aUser)
429 TVerdict returnValue = EPass;
430 iLabel1.Set(aLabel1);
431 iLabel2.Set(aLabel2);
439 void CTestToolEngine::Initilize()
441 iState = EInitialise;
442 iCertStore->Initialize(iStatus);
444 CActiveScheduler::Start();
447 void CTestToolEngine::KeyInitilize()
449 iState = EInitialiseKeyStore;
450 iKeyStore->Initialize(iStatus);
452 CActiveScheduler::Start();
455 TBool CTestToolEngine::VerifyApps(RPointerArray<HBufC>& aApps)
457 TInt uidsCount = iUids.Count();
458 TInt appsCount = aApps.Count();
459 TBool result = ETrue;
460 if ( uidsCount != appsCount)
464 for (TInt i = 0; i < uidsCount; i++)
466 if (iUids[i] == TUid::Uid(swinstalluid))
468 result = CheckAppForUid(KSWInstall, aApps);
470 else if (iUids[i] == TUid::Uid(swinstallocspuid))
472 result = CheckAppForUid(KSWInstallOCSP, aApps);
474 else if (iUids[i] == TUid::Uid(midletinstalluid))
476 result = CheckAppForUid(KMidletInstall, aApps);
478 else if (iUids[i] == TUid::Uid(tlsuid))
480 result = CheckAppForUid(KTls, aApps);
482 else if (iUids[i] == TUid::Uid(tocspuid))
484 result = CheckAppForUid(KTOcsp, aApps);
498 TBool CTestToolEngine::CheckAppForUid(const TDesC& aAppName, RPointerArray<HBufC>& aApps)
500 TBool result = EFalse;
501 for (TInt i = 0; i < aApps.Count();i++)
503 if (aAppName.CompareF(*aApps[i]) == KErrNone)
516 TBool CTestToolEngine::VerifyPolicy(TInt aUser)
518 TBool result = EFalse;
519 _LIT(KCab, "WriteDeviceData");
523 TCompiledSecurityPolicy managementPolicy = iKeys[iIndex]->ManagementPolicy();
524 TCompiledSecurityPolicy usePolicy = iKeys[iIndex]->UsePolicy();
525 TUint32 mSecureid = managementPolicy.SecureId();
526 TUint32 uSecureid = usePolicy.SecureId();
530 if (mSecureid == 0xffffffff)
532 TCapability cab = managementPolicy.Capability(0);
533 // currently only possible for keyowner other than secure id.
534 if (cab == ECapabilityWriteDeviceData && iLabel2.CompareF(KCab) == KErrNone)
545 TUid mSecureUid = TUid::Uid(mSecureid);
546 if (mSecureUid == secid)
558 if (uSecureid == 0xffffffff)
560 TSecurityPolicy::TType utype = usePolicy.Type();
562 // currently only possible for keyuser other than secure id
563 if (utype == TSecurityPolicy::ETypePass && iLabel2.CompareF(KType) == KErrNone)
574 TUid uSecureUid = TUid::Uid(uSecureid);
575 if (uSecureUid == secid)
588 TInt CTestToolEngine::FilterCerts(RMPointerArray<CCTCertInfo>& aCertList,
589 TDesC& aLabel, TInt& aIndex, TBool aFilterOnly)
591 TInt j = 0, count = 0;
592 while (j < aCertList.Count())
594 if ( aCertList[j]->Label().MatchF(aLabel) != KErrNotFound)
599 //first certificate to be selected for retreiving
608 CCTCertInfo* tmp = aCertList[j];
621 void CTestToolEngine::FilterCerts(RMPointerArray<CCTCertInfo>& aCertList, TCertificateOwnerType& aOwnerType)
624 while (j < aCertList.Count())
626 if ( aCertList[j]->CertificateOwnerType() != aOwnerType)
628 CCTCertInfo* tmp = aCertList[j];
640 TInt CTestToolEngine::FilterKeys(RMPointerArray<CCTKeyInfo>& aKeyList,
641 TDesC& aLabel, TInt& aIndex)
643 TInt j = 0, count = 0;
644 while (j < aKeyList.Count())
646 if ( aKeyList[j]->Label().MatchF(aLabel) != KErrNotFound)
651 //first key to be selected for retreiving
664 TBool CTestToolEngine::CompareWithExpectedCertificates(RPointerArray<HBufC>& aCerts)
666 if ( iCertInfos.Count() != aCerts.Count())
670 for (TInt i = 0 ; i < iCertInfos.Count(); i++)
672 const TDesC& label = iCertInfos[i]->Label();
673 TBool found = EFalse;
674 for (TInt j = 0; j < aCerts.Count(); j++)
676 if (aCerts[j]->CompareF(label) == KErrNone)
691 TBool CTestToolEngine::CompareWithExpectedKeys(RPointerArray<HBufC>& aKeys)
693 if ( iKeys.Count() != iKeysListCount)
697 for (TInt i = 0 ; i < iKeys.Count(); i++)
699 const TDesC& label = iKeys[i]->Label();
700 TBool found = EFalse;
701 for (TInt j = 0; j < iKeysListCount; j++)
703 if (aKeys[j]->CompareF(label) == KErrNone)
717 TBool CTestToolEngine::CompareWithExpectedStore(RPointerArray<HBufC>& aCerts)
719 TBool result = EFalse;
720 TInt storeCount = iCertStore->CertStoreCount();
721 for (TInt i = 0; i < storeCount ; i++)
724 MCTToken& token = (iCertStore->CertStore(i)).Token();
725 const TDesC& tmp = token.Label();
726 for (TInt j = 0; j < aCerts.Count(); j++)
728 if (tmp.CompareF(*aCerts[j]) == KErrNone)
743 TBool CTestToolEngine::CompareWithExpectedStoreKey(RPointerArray<HBufC>& aKeys)
745 TBool result = EFalse;
746 TInt storeCount = iKeyStore->KeyStoreCount();
747 for (TInt i = 0; i < storeCount ; i++)
750 MCTToken& token = (iKeyStore->KeyStore(i)).Token();
751 const TDesC& tmp = token.Label();
752 for (TInt j = 0; j < aKeys.Count(); j++)
754 if (tmp.CompareF(*aKeys[j]) == KErrNone)
770 TBool CTestToolEngine::VerifyTrust(TBool aTrust)
772 TBool result = EFalse;
773 if (aTrust == iActualTrust)