os/security/cryptoservices/certificateandkeymgmt/tadditionalstores/Test1certstore.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/tadditionalstores/Test1certstore.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,941 @@
     1.4 +/*
     1.5 +* Copyright (c) 1998-2009 Nokia Corporation and/or its subsidiary(-ies).
     1.6 +* All rights reserved.
     1.7 +* This component and the accompanying materials are made available
     1.8 +* under the terms of the License "Eclipse Public License v1.0"
     1.9 +* which accompanies this distribution, and is available
    1.10 +* at the URL "http://www.eclipse.org/legal/epl-v10.html".
    1.11 +*
    1.12 +* Initial Contributors:
    1.13 +* Nokia Corporation - initial contribution.
    1.14 +*
    1.15 +* Contributors:
    1.16 +*
    1.17 +* Description: 
    1.18 +*
    1.19 +*/
    1.20 +
    1.21 +
    1.22 +#include "test1certstore.h"
    1.23 +#include "tadditionalstoremappings.h"
    1.24 +#include <signed.h>
    1.25 +#include <x509cert.h>
    1.26 +#include <wtlscert.h>
    1.27 +#include <x509keys.h>
    1.28 +#include <ecom/implementationproxy.h>
    1.29 +
    1.30 +
    1.31 +_LIT(KName1, "Test store 1");
    1.32 +_LIT(KName2, "Test store 2");
    1.33 +
    1.34 +
    1.35 +//////////////////////////////////////////////////////////////////////////////////////////
    1.36 +//CFileCertStore
    1.37 +/////////////////////////////////////////////////////////////////////////////////////////
    1.38 +
    1.39 +CTest1CertStore* CTest1CertStore::NewL(RFs& aFs,
    1.40 +									   CTest1CertStoreToken& aToken, 
    1.41 +									   const TDesC& aFileName,
    1.42 +									   TFileMode aMode)
    1.43 +	{
    1.44 +	CTest1CertStore* self = new(ELeave) CTest1CertStore(aToken, aFs);
    1.45 +	CleanupStack::PushL(self);
    1.46 +	self->ConstructL(aFileName, aMode);
    1.47 +	CleanupStack::Pop(self);
    1.48 +	return self;
    1.49 +	}
    1.50 +
    1.51 +MCTToken& CTest1CertStore::Token()
    1.52 +	{
    1.53 +	return iToken;
    1.54 +	}
    1.55 +
    1.56 +void CTest1CertStore::DoRelease()
    1.57 +	{
    1.58 +	if (iToken.Label() == KName1)
    1.59 +		{
    1.60 +		iToken.iRefCountInterface--;
    1.61 +		if (!iToken.iRefCountInterface)
    1.62 +			{
    1.63 +			delete this;
    1.64 +			}
    1.65 +		}
    1.66 +	else if (iToken.Label() == KName2)
    1.67 +		{
    1.68 +		iToken.iRefCountInterface2--;
    1.69 +		if (!iToken.iRefCountInterface2)
    1.70 +			{
    1.71 +			delete this;
    1.72 +			}
    1.73 +		}
    1.74 +	}
    1.75 +
    1.76 +void CTest1CertStore::List(RMPointerArray<CCTCertInfo>& aCertInfos,
    1.77 +						  const CCertAttributeFilter& aFilter,
    1.78 +						  TRequestStatus& aStatus)
    1.79 +	{
    1.80 +	iOriginalRequestStatus = &aStatus;
    1.81 +	aStatus = KRequestPending;
    1.82 +	
    1.83 +	iCertInfos = &aCertInfos;
    1.84 +	iFilter = &aFilter;
    1.85 +
    1.86 +	//Iinitialize the ketstore and then list the key info
    1.87 +	if (iFilter->iKeyUsage != EX509UsageAll)
    1.88 +		{
    1.89 +		// We have to initialize the unified key store
    1.90 +		TRAPD(err, iUnifiedKeyStore = CUnifiedKeyStore::NewL(iFs));
    1.91 +		if (err != KErrNone)
    1.92 +			{
    1.93 +			User::RequestComplete(iOriginalRequestStatus, err);
    1.94 +			iOriginalRequestStatus = 0;
    1.95 +			}
    1.96 +		else
    1.97 +			{
    1.98 +			iUnifiedKeyStore->Initialize(iStatus);
    1.99 +			iState = EGetKeyInfos;
   1.100 +			SetActive();
   1.101 +			}
   1.102 +		}
   1.103 +	else
   1.104 +		{
   1.105 +		iState = EList;
   1.106 +		TRequestStatus* status = &iStatus;
   1.107 +		User::RequestComplete(status, KErrNone);
   1.108 +		SetActive();
   1.109 +		}
   1.110 +	}
   1.111 +
   1.112 +void CTest1CertStore::CancelList()
   1.113 +	{
   1.114 +	Cancel();
   1.115 +	}
   1.116 +
   1.117 +void CTest1CertStore::GetCert(CCTCertInfo*& aCertInfo,
   1.118 +							 const TCTTokenObjectHandle& aHandle,
   1.119 +							 TRequestStatus& aStatus)
   1.120 +	{
   1.121 +	TInt err = KErrNotFound;
   1.122 +	if ((aHandle.iTokenHandle == iToken.Handle()) &&
   1.123 +		(aHandle.iObjectId < iCerts->Count()))
   1.124 +		{
   1.125 +		TRAP(err, aCertInfo = CCTCertInfo::NewL(iCerts->Entry(aHandle.iObjectId)));
   1.126 +		}
   1.127 +	TRequestStatus* status = &aStatus;
   1.128 +	User::RequestComplete(status, err);
   1.129 +	}
   1.130 +
   1.131 +void CTest1CertStore::CancelGetCert()
   1.132 +	{
   1.133 +	// Nothing to do because GetCert is not asynchronous.
   1.134 +	}
   1.135 +
   1.136 +void CTest1CertStore::Applications(const CCTCertInfo& aCertInfo,
   1.137 +								  RArray<TUid>& aApplications,
   1.138 +								  TRequestStatus& aStatus)
   1.139 +	{
   1.140 +	TInt err = KErrNone;
   1.141 +	TInt index = iCerts->Index(aCertInfo);
   1.142 +	if (index != KErrNotFound)
   1.143 +		{
   1.144 +		const RArray<TUid>& apps = iCerts->Mapping(index)->CertificateApps();
   1.145 +		TInt end = apps.Count();
   1.146 +		for (TInt i = 0; (i < end) && (err == KErrNone); i++)
   1.147 +			{
   1.148 +			err = aApplications.Append(apps[i]);
   1.149 +			}
   1.150 +		}
   1.151 +	else
   1.152 +		{
   1.153 +		err = index;
   1.154 +		}
   1.155 +	if (err != KErrNone)
   1.156 +		{
   1.157 +		aApplications.Reset();
   1.158 +		}
   1.159 +	TRequestStatus* status = &aStatus;
   1.160 +	User::RequestComplete(status, err);
   1.161 +	}
   1.162 +
   1.163 +void CTest1CertStore::CancelApplications()
   1.164 +	{
   1.165 +	}
   1.166 +
   1.167 +void CTest1CertStore::IsApplicable(const CCTCertInfo& aCertInfo,
   1.168 +								  TUid aApplication, 
   1.169 +								  TBool& aIsApplicable,
   1.170 +								  TRequestStatus& aStatus)
   1.171 +	{
   1.172 +	TInt index = iCerts->Index(aCertInfo);
   1.173 +	if (index != KErrNotFound)
   1.174 +		{
   1.175 +		const RArray<TUid>& apps = iCerts->Mapping(index)->CertificateApps();
   1.176 +		TInt end = apps.Count();
   1.177 +		TInt i;
   1.178 +		for (i = 0; i < end; i++)
   1.179 +			{
   1.180 +			if (apps[i] == aApplication)
   1.181 +				{
   1.182 +				break;
   1.183 +				}
   1.184 +			}
   1.185 +		if (i == end)
   1.186 +			{
   1.187 +			aIsApplicable = EFalse;
   1.188 +			}
   1.189 +		else
   1.190 +			{
   1.191 +			aIsApplicable = ETrue;
   1.192 +			}
   1.193 +		index = KErrNone;
   1.194 +		}
   1.195 +	
   1.196 +	TRequestStatus* status = &aStatus;
   1.197 +	User::RequestComplete(status, index);
   1.198 +	}
   1.199 +
   1.200 +void CTest1CertStore::CancelIsApplicable()
   1.201 +	{
   1.202 +	// Nothing to do because IsApplicable is not asynchronous.
   1.203 +	}
   1.204 +
   1.205 +void CTest1CertStore::Trusted(const CCTCertInfo& aCertInfo,
   1.206 +							 TBool& aTrusted, 
   1.207 +							 TRequestStatus& aStatus)
   1.208 +	{	
   1.209 +	TInt index = iCerts->Index(aCertInfo);
   1.210 +	if (index != KErrNotFound)
   1.211 +		{
   1.212 +		aTrusted = iCerts->Mapping(index)->Trusted();
   1.213 +		index = KErrNone;
   1.214 +		}
   1.215 +	
   1.216 +	TRequestStatus* status = &aStatus;
   1.217 +	User::RequestComplete(status, index);
   1.218 +	}
   1.219 +
   1.220 +void CTest1CertStore::CancelTrusted()
   1.221 +	{
   1.222 +	// Nothing to do because Trusted is not asynchronous.
   1.223 +	}
   1.224 +
   1.225 +void CTest1CertStore::Retrieve(const CCTCertInfo& aCertInfo,
   1.226 +							  TDes8& aCertificate,
   1.227 +							  TRequestStatus& aStatus)
   1.228 +	{
   1.229 +#ifdef CERTSTORE_SOFTWARE_ASYNCH
   1.230 +	// perform an asynchronous retrieval of the certificate
   1.231 +	iOriginalRequestStatus = &aStatus;
   1.232 +	aStatus = KRequestPending;
   1.233 +
   1.234 +	iAsynchCertInfo = &aCertInfo;
   1.235 +	iAsynchCertificate = &aCertificate;
   1.236 +
   1.237 +	iState = ERetrieve;
   1.238 +	TRequestStatus* status = &iStatus;
   1.239 +	User::RequestComplete(status, KErrNone);
   1.240 +	SetActive();
   1.241 +#else
   1.242 +	RetrieveNow(aCertInfo,aCertificate,aStatus);
   1.243 +#endif
   1.244 +	}
   1.245 +
   1.246 +void CTest1CertStore::RetrieveNow(const CCTCertInfo& aCertInfo,
   1.247 +							  TDes8& aCertificate,
   1.248 +							  TRequestStatus& aStatus)
   1.249 +	{
   1.250 +	TRequestStatus* status = &aStatus;
   1.251 +	TInt err;
   1.252 +	TInt index = iCerts->Index(aCertInfo);
   1.253 +	if (index == KErrNotFound)
   1.254 +		{
   1.255 +		err = KErrNotFound;
   1.256 +		}
   1.257 +	else
   1.258 +		{
   1.259 +		CFileCertStoreMapping* mapping = NULL;
   1.260 +		mapping = iCerts->Mapping(index);
   1.261 +		err = index;
   1.262 +		if (mapping)
   1.263 +			{
   1.264 +			TRAP(err, DoLoadL(aCertificate, *mapping));
   1.265 +			}
   1.266 +		}
   1.267 +	User::RequestComplete(status, err);
   1.268 +	}
   1.269 +
   1.270 +void CTest1CertStore::CancelRetrieve()
   1.271 +	{
   1.272 +	}
   1.273 +
   1.274 +
   1.275 +void CTest1CertStore::Capabilities(const CCTCertInfo& /*aCertInfo*/, TCapabilitySet& /*aCapbilitiesOut*/,
   1.276 +								  TRequestStatus& aStatus)
   1.277 +	{
   1.278 +	// currently not supported
   1.279 +	TRequestStatus* status = &aStatus;
   1.280 +	User::RequestComplete(status, KErrNotSupported);
   1.281 +	}
   1.282 +
   1.283 +void CTest1CertStore::CancelCapabilities()
   1.284 +	{
   1.285 +	// Nothing to do because Capabilities is not asynchronous.
   1.286 +	}
   1.287 +
   1.288 +void CTest1CertStore::IsMandatory(const CCTCertInfo& /*aCertInfo*/, TBool& /*aMandatoryOut*/,
   1.289 +								 TRequestStatus& aStatus)
   1.290 +	{
   1.291 +	// currently not supported
   1.292 +	TRequestStatus* status = &aStatus;
   1.293 +	User::RequestComplete(status, KErrNotSupported);
   1.294 +	}
   1.295 +
   1.296 +void CTest1CertStore::CancelIsMandatory()
   1.297 +	{
   1.298 +	// Nothing to do because IsMandatory is not asynchronous.
   1.299 +	}
   1.300 +
   1.301 +
   1.302 +void CTest1CertStore::Remove(const CCTCertInfo& aCertInfo, TRequestStatus& aStatus)
   1.303 +	{
   1.304 +	// This removes the certificate from the file store.
   1.305 +	TRAPD(err, DoRemoveL(aCertInfo));
   1.306 +	TRequestStatus* status = &aStatus;
   1.307 +	User::RequestComplete(status, err);
   1.308 +	}
   1.309 +
   1.310 +void CTest1CertStore::CancelRemove()
   1.311 +	{
   1.312 +	}
   1.313 +
   1.314 +
   1.315 +void CTest1CertStore::SetApplicability(const CCTCertInfo& aCertInfo,
   1.316 +									  const RArray<TUid>& aTrusters,
   1.317 +									  TRequestStatus& aStatus)
   1.318 +	{
   1.319 +	TRAPD(err, DoSetApplicabilityL(aCertInfo, aTrusters));
   1.320 +	TRequestStatus* status = &aStatus;
   1.321 +	User::RequestComplete(status, err);
   1.322 +	}
   1.323 +
   1.324 +
   1.325 +void CTest1CertStore::CancelSetApplicability()
   1.326 +	{
   1.327 +	}
   1.328 +
   1.329 +void CTest1CertStore::SetTrust(const CCTCertInfo& aCertInfo, 
   1.330 +							  TBool aTrusted, 
   1.331 +							  TRequestStatus& aStatus)
   1.332 +	{
   1.333 +	TRAPD(err, DoSetTrustL(aCertInfo, aTrusted));
   1.334 +	TRequestStatus* status = &aStatus;
   1.335 +	User::RequestComplete(status, err);
   1.336 +	}
   1.337 +
   1.338 +void CTest1CertStore::CancelSetTrust()
   1.339 +	{
   1.340 +	// Nothing to do because SetTrust is not asynchronous.
   1.341 +	}
   1.342 +
   1.343 +void CTest1CertStore::SetCapabilities(const CCTCertInfo& /*aCertInfo*/, const TCapabilitySet& /*aCapabilities*/, 
   1.344 +									 TRequestStatus& aStatus)
   1.345 +	{
   1.346 +	// currently not supported
   1.347 +	TRequestStatus* status = &aStatus;
   1.348 +	User::RequestComplete(status, KErrNotSupported);
   1.349 +	}
   1.350 +
   1.351 +void CTest1CertStore::CancelSetCapabilities()
   1.352 +	{
   1.353 +	// Nothing to do because SetCapabilities is not asynchronous.
   1.354 +	}
   1.355 +
   1.356 +void CTest1CertStore::SetMandatory(const CCTCertInfo& /*aCertInfo*/, TBool /*aMandatory*/,
   1.357 +								  TRequestStatus& aStatus)
   1.358 +	{
   1.359 +	// currently not supported
   1.360 +	TRequestStatus* status = &aStatus;
   1.361 +	User::RequestComplete(status, KErrNotSupported);
   1.362 +	}
   1.363 +
   1.364 +void CTest1CertStore::CancelSetMandatory()
   1.365 +	{
   1.366 +	// Nothing to do because SetMandatory is not asynchronous.
   1.367 +	}
   1.368 +
   1.369 +
   1.370 +void CTest1CertStore::RevertStore(TAny* aStore)
   1.371 +	{
   1.372 +	//this is a CleanupItem
   1.373 +	CPermanentFileStore* store = REINTERPRET_CAST(CPermanentFileStore*, aStore);
   1.374 +	store->Revert();
   1.375 +	}
   1.376 +
   1.377 +
   1.378 +void CTest1CertStore::DeleteFile(TAny* aThis)
   1.379 +	{
   1.380 +	CTest1CertStore* self = REINTERPRET_CAST(CTest1CertStore*, aThis);
   1.381 +	TDriveUnit sysDrive (RFs::GetSystemDrive());
   1.382 +	TDriveName driveName(sysDrive.Name());
   1.383 +	TBuf<128> certStoreDefaultFullPath (driveName);
   1.384 +	certStoreDefaultFullPath.Append(_L("\\system\\data\\cacerts.dat"));
   1.385 +	self->iFile.Close();
   1.386 +	self->iFs.Delete(certStoreDefaultFullPath);
   1.387 +	}
   1.388 +
   1.389 +
   1.390 +
   1.391 +CTest1CertStore::~CTest1CertStore()
   1.392 +	{
   1.393 +	Cancel();
   1.394 +
   1.395 +	if (iStore != NULL)
   1.396 +		{
   1.397 +#ifdef _DEBUG
   1.398 +#else		
   1.399 +		TInt err;
   1.400 +		TRAP(err, iStore->ReclaimL());
   1.401 +		TRAP(err, iStore->CompactL());
   1.402 +		if (err == KErrNone)
   1.403 +			{
   1.404 +			TRAP(err, iStore->CommitL());
   1.405 +			}
   1.406 +#endif
   1.407 +		delete iStore;
   1.408 +		}
   1.409 +
   1.410 +	iFile.Close();
   1.411 +	delete iCerts;
   1.412 +
   1.413 +	iKeyInfos.Close();
   1.414 +
   1.415 +	delete iUnifiedKeyStore;
   1.416 +	}
   1.417 +
   1.418 +
   1.419 +void CTest1CertStore::DoSetApplicabilityL(const CCTCertInfo& aCertInfo,
   1.420 +										 const RArray<TUid>& aTrusters)
   1.421 +	{
   1.422 +	TInt index = iCerts->Index(aCertInfo);
   1.423 +	if (index == KErrNotFound)
   1.424 +		{
   1.425 +		User::Leave(index);
   1.426 +		}
   1.427 +	
   1.428 +	CFileCertStoreMapping* mapping = iCerts->Mapping(index);
   1.429 +	const RArray<TUid>& trusters = mapping->CertificateApps(); //oldEntry->Trusters();
   1.430 +	RArray<TUid>* oldTrusters = new(ELeave) RArray<TUid>();
   1.431 +	CleanupStack::PushL(oldTrusters);
   1.432 +	CleanupClosePushL(*oldTrusters);
   1.433 +	TInt iEnd = trusters.Count();
   1.434 +	TInt i;
   1.435 +	for (i = 0; i < iEnd; i++)
   1.436 +		{
   1.437 +		User::LeaveIfError(oldTrusters->Append(trusters[i]));
   1.438 +		}
   1.439 +
   1.440 +	RArray<TUid>* newTrusters = new (ELeave) RArray<TUid>;
   1.441 +	CleanupStack::PushL(newTrusters);
   1.442 +	CleanupClosePushL(*newTrusters);
   1.443 +	for (i = 0 ; i < aTrusters.Count() ; ++i)
   1.444 +		{
   1.445 +		User::LeaveIfError(newTrusters->Append(aTrusters[i]));		
   1.446 +		}
   1.447 +
   1.448 +	mapping->SetCertificateApps(newTrusters);
   1.449 +	CleanupStack::Pop(2, newTrusters);
   1.450 +
   1.451 +	TRAPD(err, UpdateStoreL());
   1.452 +	CleanupStack::Pop(2);	// *oldTrusters, oldTrusters
   1.453 +	if (err != KErrNone)
   1.454 +		{
   1.455 +		// If there is an error, we undo the change in oldEntry
   1.456 +		mapping->SetCertificateApps(oldTrusters);
   1.457 +		}
   1.458 +	else
   1.459 +		{
   1.460 +		oldTrusters->Close();
   1.461 +		delete oldTrusters;
   1.462 +		}
   1.463 +	}
   1.464 +
   1.465 +
   1.466 +void CTest1CertStore::DoSetTrustL(const CCTCertInfo& aCertInfo,
   1.467 +								 TBool aTrusted)
   1.468 +	{
   1.469 +	TInt index = iCerts->Index(aCertInfo);
   1.470 +	if (index == KErrNotFound)
   1.471 +		{
   1.472 +		User::Leave(index);
   1.473 +		}
   1.474 +	CFileCertStoreMapping* mapping = iCerts->Mapping(index);
   1.475 +	TBool oldValue = mapping->Trusted();
   1.476 +	mapping->SetTrusted(aTrusted);
   1.477 +	TRAPD(err, UpdateStoreL());
   1.478 +	if (err != KErrNone)
   1.479 +		{
   1.480 +		// If there is an error, we undo the change in oldEntry
   1.481 +		mapping->SetTrusted(oldValue);
   1.482 +		}
   1.483 +	}
   1.484 +
   1.485 +void CTest1CertStore::Add(const TDesC& aLabel,
   1.486 +						 TCertificateFormat aFormat,
   1.487 +						 TCertificateOwnerType aCertificateOwnerType,
   1.488 +						 const TKeyIdentifier* aSubjectKeyId,
   1.489 +						 const TKeyIdentifier* aIssuerKeyId,
   1.490 +						 const TDesC8& aCert, 
   1.491 +						 TRequestStatus& aStatus)
   1.492 +	{
   1.493 +	TRAPD(err, DoAddL(aLabel, aFormat, aCertificateOwnerType, aSubjectKeyId,
   1.494 +		aIssuerKeyId, aCert));
   1.495 +	TRequestStatus* status = &aStatus;
   1.496 +	User::RequestComplete(status, err);
   1.497 +	}
   1.498 +
   1.499 +void CTest1CertStore::CancelAdd()
   1.500 +	{
   1.501 +	Cancel();
   1.502 +	}
   1.503 +
   1.504 +void CTest1CertStore::DoAddL(const TDesC& aLabel,
   1.505 +							TCertificateFormat aFormat,
   1.506 +							TCertificateOwnerType aCertificateOwnerType,
   1.507 +							const TKeyIdentifier* aSubjectKeyId,
   1.508 +							const TKeyIdentifier* aIssuerKeyId,
   1.509 +							const TDesC8& aCert)
   1.510 +	{
   1.511 +	TKeyIdentifier subjectKeyId;
   1.512 +	switch (aFormat)
   1.513 +		{
   1.514 +		case EX509Certificate:
   1.515 +			if (!aSubjectKeyId)
   1.516 +				{
   1.517 +				CCertificate* cert = CX509Certificate::NewLC(aCert);
   1.518 +				subjectKeyId = cert->KeyIdentifierL();
   1.519 +				aSubjectKeyId = &subjectKeyId;
   1.520 +				CleanupStack::PopAndDestroy(cert);
   1.521 +				}
   1.522 +			break;
   1.523 +	
   1.524 +		case EWTLSCertificate:
   1.525 +			if (!aSubjectKeyId)
   1.526 +				{
   1.527 +				CCertificate* cert = CWTLSCertificate::NewLC(aCert);
   1.528 +				subjectKeyId = cert->KeyIdentifierL();
   1.529 +				aSubjectKeyId = &subjectKeyId;
   1.530 +				CleanupStack::PopAndDestroy(cert);
   1.531 +				}
   1.532 +			break;
   1.533 +
   1.534 +		case EX509CertificateUrl:
   1.535 +			if (!aSubjectKeyId)
   1.536 +				{
   1.537 +				User::Leave(KErrArgument);
   1.538 +				}
   1.539 +			break;
   1.540 +
   1.541 +		default:
   1.542 +			User::Leave(KErrNotSupported);	
   1.543 +			break;
   1.544 +		}
   1.545 +
   1.546 +
   1.547 +	TInt iend = iCerts->Count();
   1.548 +	for (TInt i = 0; i < iend; i++)
   1.549 +		{
   1.550 +		if (iCerts->Entry(i).Label() == aLabel)
   1.551 +			{
   1.552 +			User::Leave(KErrBadName);
   1.553 +			}
   1.554 +		}
   1.555 +
   1.556 +	CCTCertInfo* entry = CCTCertInfo::NewL(aLabel, aFormat, 
   1.557 +		aCertificateOwnerType, aCert.Length(), aSubjectKeyId, aIssuerKeyId, iToken, iCerts->Count());
   1.558 +	// AddCertL takes ownership of entry no matter what happens.
   1.559 +	AddCertL(entry, aCert, *iCerts);
   1.560 +	}
   1.561 +
   1.562 +/*void CFileCertStore::AddUserCertL(const CCertificate& aCert, 
   1.563 +								  const TDesC& aLabel,
   1.564 +								  TCertificateFormat aFormat, 
   1.565 +								  const TKeyIdentifier& aIssuerKeyHash, 
   1.566 +								  const TKeyIdentifier& aSubjectKeyHash)
   1.567 +	{
   1.568 +	if (aFormat != EX509Certificate)
   1.569 +		{
   1.570 +		User::Leave(KErrNotSupported);
   1.571 +		}
   1.572 +
   1.573 +	TInt iend = iUserCerts->Count();
   1.574 +	for (TInt i = 0; i < iend; i++)
   1.575 +		{
   1.576 +		if (iUserCerts->Entry(i).Label() == aLabel)
   1.577 +			{
   1.578 +			User::Leave(KErrBadName);
   1.579 +			}
   1.580 +		}
   1.581 +
   1.582 +	// We compute the subject key hash using the information contained in the certificate
   1.583 +	const CSubjectPublicKeyInfo& key = aCert.PublicKey();
   1.584 +	CX509RSAPublicKey* rsaKey = CX509RSAPublicKey::NewLC(key.KeyData());
   1.585 +#ifdef SYMBIAN_CRYPTO	
   1.586 +	const TInteger& modulus = rsaKey->Modulus();
   1.587 +#else
   1.588 +	const CInteger& modulus = rsaKey->Modulus();
   1.589 +#endif
   1.590 +	HBufC8* modulusBuffer = modulus.BufferLC();
   1.591 +
   1.592 +	CSHA1* sha1 = CSHA1::NewL();
   1.593 +	CleanupStack::PushL(sha1);
   1.594 +	
   1.595 +	TPtrC8 hash = sha1->Hash(*modulusBuffer);
   1.596 +
   1.597 +
   1.598 +	TKeyIdentifier keyId;
   1.599 +	keyId = hash;
   1.600 +
   1.601 +	CleanupStack::PopAndDestroy(3);	// rsaKey, modulusBuffer, sha1
   1.602 +
   1.603 +	// If the caller supplied a aSubjectKeyHash, we must compare it with the computed
   1.604 +	// value and ensure they are the same
   1.605 +	if ((aSubjectKeyHash != KNullDesC8) && (aSubjectKeyHash != keyId))
   1.606 +		{
   1.607 +		User::Leave(KErrArgument);
   1.608 +		}
   1.609 +	
   1.610 +	CCTCertInfo* entry = CCTCertInfo::NewL(aLabel, 
   1.611 +		*this, EX509Certificate, aCert.Encoding().Length(), keyId, aIssuerKeyHash);
   1.612 +	AddCertL(entry, aCert, *iUserCerts);
   1.613 +	}*/
   1.614 +
   1.615 +/*void CFileCertStore::AddUserCertURLL(const TDesC8& aCert, 
   1.616 +									 const TDesC& aLabel,
   1.617 +									 const TKeyIdentifier& aIssuerKeyHash, 
   1.618 +									 const TKeyIdentifier& aSubjectKeyHash)
   1.619 +	{
   1.620 +	TInt iEnd = iUserCerts->Count();
   1.621 +	for (TInt i = 0; i < iEnd; i++)
   1.622 +		{
   1.623 +		if (iUserCerts->Entry(i).Label() == aLabel)
   1.624 +			{
   1.625 +			User::Leave(KErrBadName);
   1.626 +			}
   1.627 +		}
   1.628 +
   1.629 +	CCTCertInfo* entry = CCTCertInfo::NewL(aLabel, 
   1.630 +		*this, EX509CertificateUrl, aCert.Length(), aSubjectKeyHash, aIssuerKeyHash);
   1.631 +	AddCertL(entry, aCert, *iUserCerts);
   1.632 +	}*/
   1.633 +
   1.634 +void CTest1CertStore::AddCertL(CCTCertInfo* aCertInfo,
   1.635 +							  const TDesC8& aCert,
   1.636 +							  CFileCertStoreMappings& aMappings)
   1.637 +	{
   1.638 +	CleanupReleasePushL(*aCertInfo);
   1.639 +//create the mapping object		
   1.640 +	CFileCertStoreMapping* mapping = CFileCertStoreMapping::NewL();
   1.641 +	mapping->SetEntry(aCertInfo);
   1.642 +	CleanupStack::Pop();//aEntry, mapping has taken ownership
   1.643 +	CleanupStack::PushL(mapping);
   1.644 +
   1.645 +	TCleanupItem cleanupStore(CTest1CertStore::RevertStore, iStore);//store will revert() if a leave occurs
   1.646 +	CleanupStack::PushL(cleanupStore);
   1.647 +
   1.648 +//store cert
   1.649 +	RStoreWriteStream stream;
   1.650 +	TStreamId certId = stream.CreateLC(*iStore);//stream for cert
   1.651 +	stream.WriteL(aCert);
   1.652 +	stream.CommitL();
   1.653 +	CleanupStack::PopAndDestroy();//stream
   1.654 +	mapping->SetId(certId);
   1.655 +
   1.656 +//add mapping to mappings, & store mappings
   1.657 +	aMappings.AddL(mapping); //takes ownership
   1.658 +	CleanupStack::Pop();//mapping; mappings has taken ownership
   1.659 +
   1.660 +//Update the mapping. if error, remove the entry.
   1.661 +	TRAPD(err, aMappings.ReplaceL());
   1.662 +	if (err == KErrNone)
   1.663 +		{
   1.664 +		TRAP(err, iStore->CommitL());
   1.665 +		if (err != KErrNone)
   1.666 +			{
   1.667 +			aMappings.Remove(*mapping->Entry());
   1.668 +			User::Leave(err);
   1.669 +			}
   1.670 +		}
   1.671 +	else
   1.672 +		{
   1.673 +		//oom tests pass currently
   1.674 +		aMappings.Remove(*mapping->Entry());
   1.675 +		User::Leave(err);
   1.676 +		}
   1.677 +	CleanupStack::Pop();//revert store
   1.678 +	}
   1.679 +
   1.680 +void CTest1CertStore::UpdateStoreL()
   1.681 +	{
   1.682 +//tries to write out the new cacerts to the file
   1.683 +	TCleanupItem cleanupStore(RevertStore, iStore);//store will revert() if a leave occurs
   1.684 +	CleanupStack::PushL(cleanupStore);
   1.685 +	iCerts->ReplaceL();
   1.686 +	iStore->CommitL();
   1.687 +	CleanupStack::Pop();//revert store
   1.688 +	}
   1.689 +
   1.690 +
   1.691 +
   1.692 +
   1.693 +
   1.694 +void CTest1CertStore::DoLoadL(TDes8& aCertificate, CFileCertStoreMapping& aMapping) const
   1.695 +	{
   1.696 +	RStoreReadStream stream;
   1.697 +	stream.OpenLC(*iStore, aMapping.Id());
   1.698 +	CCTCertInfo* entry = aMapping.Entry();
   1.699 +	stream.ReadL(aCertificate, entry->Size());
   1.700 +	CleanupStack::PopAndDestroy();//stream
   1.701 +	}
   1.702 +
   1.703 +void CTest1CertStore::DoRemoveL(const CCTCertInfo& aCertInfo)
   1.704 +	{
   1.705 +	switch(aCertInfo.CertificateFormat())
   1.706 +		{
   1.707 +		case EWTLSCertificate://must be a CA cert
   1.708 +		case EX509CertificateUrl:
   1.709 +		case EX509Certificate:
   1.710 +			User::LeaveIfError(iCerts->SetTempRemove(aCertInfo, ETrue));
   1.711 +			break;
   1.712 +			
   1.713 +		default:
   1.714 +			{
   1.715 +			User::Leave(KErrNotSupported);
   1.716 +			}
   1.717 +		}
   1.718 +	TRAPD(err, UpdateStoreL());
   1.719 +	if (err != KErrNone)
   1.720 +		{
   1.721 +		User::LeaveIfError(iCerts->SetTempRemove(aCertInfo, EFalse));
   1.722 +		}
   1.723 +	else
   1.724 +		{
   1.725 +		User::LeaveIfError(iCerts->Remove(aCertInfo));	
   1.726 +		}
   1.727 +	}
   1.728 +
   1.729 +//private functions
   1.730 +CTest1CertStore::CTest1CertStore(CTest1CertStoreToken& aToken, RFs& aFs)
   1.731 +	: CActive(EPriorityNormal), iToken(aToken), iFs(aFs)
   1.732 +	{
   1.733 +	CActiveScheduler::Add(this);
   1.734 +	}
   1.735 +
   1.736 +void CTest1CertStore::ConstructL(const TDesC& aFileName, TFileMode aMode)
   1.737 +	{
   1.738 +	iStore = OpenStoreL(aFileName, aMode);
   1.739 +	RestoreL();
   1.740 +	}
   1.741 +
   1.742 +void CTest1CertStore::RestoreL()
   1.743 +	{
   1.744 +	TStreamId caCertEntryStreamId;
   1.745 +	RStoreReadStream stream;
   1.746 +	stream.OpenLC(*iStore, iStore->Root());
   1.747 +
   1.748 +	stream >> caCertEntryStreamId;
   1.749 +	CleanupStack::PopAndDestroy();//stream
   1.750 +
   1.751 +	iCerts = CFileCertStoreMappings::NewL(caCertEntryStreamId, *iStore);
   1.752 +	RStoreReadStream caCertEntryStream;
   1.753 +	caCertEntryStream.OpenLC(*iStore, caCertEntryStreamId);
   1.754 +
   1.755 +	TInt caCount = caCertEntryStream.ReadInt32L();
   1.756 +	for (TInt i = 0; i < caCount; i++)
   1.757 +		{
   1.758 +		CFileCertStoreMapping* caCertMapping = CFileCertStoreMapping::NewL();
   1.759 +		CleanupStack::PushL(caCertMapping);
   1.760 +		CCTCertInfo* caCertEntry = CCTCertInfo::NewLC(caCertEntryStream, iToken);
   1.761 +		caCertMapping->SetEntry(caCertEntry);
   1.762 +		CleanupStack::Pop();//caCertEntry
   1.763 +		// Read the CertificateApps uids
   1.764 +		RArray<TUid>* certificateApps = new(ELeave) RArray<TUid>();
   1.765 +		CleanupStack::PushL(certificateApps);
   1.766 +		CleanupClosePushL(*certificateApps);
   1.767 +		TInt count = caCertEntryStream.ReadInt32L();
   1.768 +		for (TInt j = 0; j < count; j++)
   1.769 +			{
   1.770 +			TUid id;
   1.771 +			caCertEntryStream >> id;
   1.772 +			User::LeaveIfError(certificateApps->Append(id));
   1.773 +			}
   1.774 +		CleanupStack::Pop(2);	// *certificateApps, certificateApps
   1.775 +		caCertMapping->SetCertificateApps(certificateApps);
   1.776 +		TBool trusted = caCertEntryStream.ReadUint8L();
   1.777 +		caCertMapping->SetTrusted(trusted);
   1.778 +		TStreamId caCertStreamId;
   1.779 +		caCertEntryStream >> caCertStreamId;
   1.780 +		caCertMapping->SetId(caCertStreamId);
   1.781 +		iCerts->AddL(caCertMapping);
   1.782 +		CleanupStack::Pop();//caCertMapping
   1.783 +		}
   1.784 +	CleanupStack::PopAndDestroy();//caCertStream
   1.785 +	}
   1.786 +
   1.787 +
   1.788 +
   1.789 +
   1.790 +
   1.791 +CPermanentFileStore* CTest1CertStore::OpenStoreLC(const TDesC& aFileName, TFileMode aMode)
   1.792 +	{
   1.793 +//this function creates, opens and returns a permanent file store in KCertStorePath,
   1.794 +//on the drive letter passed in, leaving it on the cleanup stack. 
   1.795 +//if the store isn't found it returns NULL
   1.796 +	TInt err = iFile.Open(iFs, aFileName, aMode); 	
   1.797 +    if (err == KErrNone)
   1.798 +		{
   1.799 +		CPermanentFileStore* store = CPermanentFileStore::FromLC(iFile);
   1.800 +		return store;
   1.801 +		}
   1.802 +	else
   1.803 +		{
   1.804 +		User::Leave(err);
   1.805 +		}
   1.806 +	return NULL;
   1.807 +	}
   1.808 +
   1.809 +CPermanentFileStore* CTest1CertStore::OpenStoreL(const TDesC& aFileName, TFileMode aMode)
   1.810 +	{
   1.811 +	CPermanentFileStore* store = NULL;
   1.812 +	store = OpenStoreLC(aFileName, aMode);	
   1.813 +	CleanupStack::Pop(store);
   1.814 +	return store;
   1.815 +	}
   1.816 +
   1.817 +void CTest1CertStore::RunL()
   1.818 +	{
   1.819 +	switch (iState)
   1.820 +		{
   1.821 +	case EGetKeyInfos:
   1.822 +		iKeyFilter.iUsage = KeyUsageX509ToPKCS15Private(iFilter->iKeyUsage);
   1.823 +		iUnifiedKeyStore->List(iKeyInfos, iKeyFilter, iStatus);
   1.824 +		iState = EList;
   1.825 +		SetActive();
   1.826 +		break;
   1.827 +
   1.828 +	case ERetrieve:
   1.829 +		// Asynch Retrieve 
   1.830 +		RetrieveNow(*iAsynchCertInfo,*iAsynchCertificate,*iOriginalRequestStatus);
   1.831 +		break;
   1.832 +
   1.833 +	case EList:
   1.834 +		{	
   1.835 +		TInt count = iCerts->Count();
   1.836 +		for (TInt index = 0; index < count; index++)
   1.837 +			{
   1.838 +			const CCTCertInfo& certInfo = iCerts->Entry(index);
   1.839 +			
   1.840 +			TBool accept = ETrue;
   1.841 +			if (iFilter->iUidIsSet)
   1.842 +				{
   1.843 +				accept = iCerts->Mapping(index)->IsApplicable(iFilter->iUid);
   1.844 +				}
   1.845 +			if (iFilter->iFormatIsSet && accept)
   1.846 +				{
   1.847 +				accept = (iFilter->iFormat == certInfo.CertificateFormat());
   1.848 +				}
   1.849 +			if (iFilter->iOwnerTypeIsSet && accept)
   1.850 +				{
   1.851 +				accept = (iFilter->iOwnerType == certInfo.CertificateOwnerType());
   1.852 +				}
   1.853 +			if ((iFilter->iSubjectKeyId != KNullDesC8) && accept)
   1.854 +				{
   1.855 +				accept = (iFilter->iSubjectKeyId == certInfo.SubjectKeyId());
   1.856 +				}
   1.857 +			
   1.858 +			if (accept)
   1.859 +				{
   1.860 +				// Fill in the cert hash.
   1.861 +				// (This returns an incorrect hard-coded value, which allows 
   1.862 +				// the test code to check that the store is being treated
   1.863 +				// correctly as a hardware store, as it'll behave differently
   1.864 +				// to if it returned the correct hashes.)
   1.865 +				_LIT8(KHash, "\x70\xe4\xf4\x54\x5f\x8e\xe6\xf2\xbd\x4e\x76\x2b\x8d\xa1\x83\xd8\xe0\x5d\x4a\x7d");
   1.866 +				CCTCertInfo* copy = CCTCertInfo::NewLC(
   1.867 +					certInfo.Label(), certInfo.CertificateFormat(),
   1.868 +					certInfo.CertificateOwnerType(), certInfo.Size(),
   1.869 +					&certInfo.SubjectKeyId(), &certInfo.IssuerKeyId(),
   1.870 +					certInfo.Token(), certInfo.Handle().iObjectId,
   1.871 +					ETrue, &KHash);
   1.872 +				User::LeaveIfError(iCertInfos->Append(copy));
   1.873 +				CleanupStack::Pop();
   1.874 +				}
   1.875 +			}
   1.876 +		iKeyInfos.Close();
   1.877 +		delete iUnifiedKeyStore;
   1.878 +		iUnifiedKeyStore = 0;
   1.879 +		User::RequestComplete(iOriginalRequestStatus, KErrNone);
   1.880 +		}
   1.881 +		break;
   1.882 +
   1.883 +	default:
   1.884 +		// ignore the undefined operations
   1.885 +		break;
   1.886 +		}
   1.887 +	}
   1.888 +
   1.889 +void CTest1CertStore::DoCancel()
   1.890 +	{
   1.891 +	if (iUnifiedKeyStore)
   1.892 +		{
   1.893 +		if (iState == EGetKeyInfos)
   1.894 +			{
   1.895 +			iUnifiedKeyStore->CancelInitialize();
   1.896 +			}
   1.897 +		else if (iState == EList)
   1.898 +			{
   1.899 +			iUnifiedKeyStore->CancelList();
   1.900 +			}
   1.901 +		iState = EList;
   1.902 +		iKeyInfos.Close();
   1.903 +		delete iUnifiedKeyStore;
   1.904 +		iUnifiedKeyStore = 0;
   1.905 +		}
   1.906 +	User::RequestComplete(iOriginalRequestStatus, KErrCancel);
   1.907 +	}
   1.908 +
   1.909 +const TImplementationProxy ImplementationTable[] =
   1.910 +	{
   1.911 +#ifdef CERTSTORE_SOFTWARE_ASYNCH
   1.912 +		IMPLEMENTATION_PROXY_ENTRY(0x101FF738, CTest1CertStoreTokenType::NewL)
   1.913 +#else
   1.914 +	#ifdef CERTSTORE_HARDWARE_SIM
   1.915 +		IMPLEMENTATION_PROXY_ENTRY(0x10206846, CTest1CertStoreTokenType::NewL)
   1.916 +	#else
   1.917 +		#ifdef CERTSTORE_HARDWARE_WIM
   1.918 +			IMPLEMENTATION_PROXY_ENTRY(0x10206847, CTest1CertStoreTokenType::NewL)
   1.919 +		#else
   1.920 +			#ifdef CERTSTORE_HARDWARE_UICC
   1.921 +				IMPLEMENTATION_PROXY_ENTRY(0x10206848, CTest1CertStoreTokenType::NewL)
   1.922 + 			#else
   1.923 +				#ifdef CERTSTORE_DEVICE_IMMUTABLE
   1.924 +					IMPLEMENTATION_PROXY_ENTRY(0x102077C3, CTest1CertStoreTokenType::NewL)
   1.925 +				#else
   1.926 +					IMPLEMENTATION_PROXY_ENTRY(0x101F5279, CTest1CertStoreTokenType::NewL)
   1.927 +				#endif
   1.928 + 			#endif
   1.929 +		#endif
   1.930 +	#endif
   1.931 +#endif
   1.932 +	};
   1.933 +
   1.934 +EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
   1.935 +	{
   1.936 +	aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
   1.937 +
   1.938 +	return ImplementationTable;
   1.939 +	}
   1.940 +
   1.941 +
   1.942 +
   1.943 +
   1.944 +