os/security/cryptoservices/certificateandkeymgmt/certstore/unifiedkeystore.cpp
changeset 0 bde4ae8d615e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/os/security/cryptoservices/certificateandkeymgmt/certstore/unifiedkeystore.cpp	Fri Jun 15 03:10:57 2012 +0200
     1.3 @@ -0,0 +1,1277 @@
     1.4 +/*
     1.5 +* Copyright (c) 2001-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 "unifiedkeystore.h"
    1.23 +#include <ecom/ecom.h>
    1.24 +#include <random.h>
    1.25 +#include <pbedata.h>
    1.26 +#include <asnpkcs.h>
    1.27 +#include "mctcertappinterface.h"
    1.28 +#include <mctkeystoreuids.h>
    1.29 +
    1.30 +_LIT(KUnifiedKeyStore, "UnifiedKeyStore");
    1.31 +
    1.32 +/////////////////////////////////////////////////////////////////////////////////////
    1.33 +//CUnifiedKeyStore
    1.34 +/////////////////////////////////////////////////////////////////////////////////////
    1.35 +
    1.36 +EXPORT_C CUnifiedKeyStore* CUnifiedKeyStore::NewL(RFs& aFs)
    1.37 +	{
    1.38 +	CUnifiedKeyStore* self = CUnifiedKeyStore::NewLC(aFs);
    1.39 +	CleanupStack::Pop(self);
    1.40 +	return self;
    1.41 +	}
    1.42 +
    1.43 +EXPORT_C CUnifiedKeyStore* CUnifiedKeyStore::NewLC(RFs& aFs)
    1.44 +	{
    1.45 +	CUnifiedKeyStore* self = new(ELeave) CUnifiedKeyStore(aFs);
    1.46 +	CleanupStack::PushL(self);
    1.47 +	self->ConstructL();
    1.48 +	return self;
    1.49 +	}
    1.50 +
    1.51 +EXPORT_C CUnifiedKeyStore::~CUnifiedKeyStore()
    1.52 +	{
    1.53 +	Cancel();
    1.54 +	Cleanup();
    1.55 +
    1.56 +	iKeyStoresHolder.ResetAndDestroy();
    1.57 +	iKeyStoresHolder.Close();
    1.58 +
    1.59 +	REComSession::FinalClose();
    1.60 +	}
    1.61 +
    1.62 +void CUnifiedKeyStore::DoInitializeL()
    1.63 +{//	We want the list of all token types that support a keystore interface
    1.64 +	RArray<TUid> uidArray;
    1.65 +	CleanupClosePushL(uidArray);
    1.66 +	
    1.67 +	User::LeaveIfError(uidArray.Append(TUid::Uid(KInterfaceKeyStore)));
    1.68 +
    1.69 +	TCTFindTokenTypesByInterface filter(uidArray.Array());
    1.70 +	CCTTokenTypeInfo::ListL(iTokenTypes, filter);
    1.71 +	
    1.72 +	CleanupStack::PopAndDestroy();	// uidArray
    1.73 +}
    1.74 +
    1.75 +EXPORT_C void CUnifiedKeyStore::Initialize(TRequestStatus& aStatus)
    1.76 +{// The following assertion checks that we didn't call Initialize twice
    1.77 +	__ASSERT_DEBUG((iKeyStoresHolder.Count()==0), User::Panic(KUnifiedKeyStore, EUnexpectedInitialise));
    1.78 +
    1.79 +	TRAPD(err, DoInitializeL());
    1.80 +	if (err != KErrNone)
    1.81 +		{
    1.82 +		TRequestStatus* status = &aStatus;
    1.83 +		User::RequestComplete(status, err);
    1.84 +		return;
    1.85 +		}
    1.86 +	
    1.87 +	iIndexTokenTypes = -1;
    1.88 +	StartAsyncOperation(EInitializeGetTokenList, aStatus);
    1.89 +
    1.90 +	SetActive();
    1.91 +	TRequestStatus* status = &iStatus;
    1.92 +	User::RequestComplete(status, KErrNone);
    1.93 +}
    1.94 +
    1.95 +EXPORT_C void CUnifiedKeyStore::CancelInitialize()
    1.96 +	{
    1.97 +	if (iState == EInitializeGetTokenList ||
    1.98 +		iState == EInitializeGetToken ||
    1.99 +		iState == EInitialiseGetKeyManagerInterface ||
   1.100 +		iState == EInitializeGetKeyUserInterface ||
   1.101 +		iState == EInitializeGetKeyUserInterfaceFinished ||
   1.102 +		iState == EInitializeFinished)
   1.103 +		{
   1.104 +		Cancel();
   1.105 +		}
   1.106 +	}
   1.107 +
   1.108 +EXPORT_C void CUnifiedKeyStore::CreateKey(TInt aKeyStoreIndex, TKeyUsagePKCS15 aUsage,TUint aSize, 
   1.109 +										  const TDesC& aLabel, CCTKeyInfo::EKeyAlgorithm aAlgorithm, 
   1.110 +										  TInt aAccessType, TTime aStartDate, TTime aEndDate, 
   1.111 +										  CCTKeyInfo*& aKeyInfoOut, TRequestStatus& aStatus)
   1.112 +	{
   1.113 +	StartAsyncOperation(ECreateKey, aStatus);
   1.114 +	TRAPD(err, PrepareToCreateKeyL(aKeyStoreIndex, aUsage, aSize, aLabel, aAlgorithm, aAccessType,
   1.115 +								   aStartDate, aEndDate, aStatus));
   1.116 +	if (KErrNone != err)
   1.117 +		{
   1.118 +		Complete(err);
   1.119 +		return;
   1.120 +		}
   1.121 +	
   1.122 +	iKeyInfoOut = &aKeyInfoOut;
   1.123 +	aKeyInfoOut = NULL;
   1.124 +	iKeyStoreManager->CreateKey(iKeyInfo, iStatus);
   1.125 +	SetActive();
   1.126 +	}
   1.127 +
   1.128 +EXPORT_C void CUnifiedKeyStore::CancelCreateKey()
   1.129 +	{
   1.130 +	if (iState == ECreateKey)
   1.131 +		{			
   1.132 +		Cancel();
   1.133 +		}
   1.134 +	}
   1.135 +
   1.136 +EXPORT_C void CUnifiedKeyStore::ImportKey(TInt aKeyStoreIndex, const TDesC8& aKeyData,
   1.137 +										  TKeyUsagePKCS15 aUsage, const TDesC& aLabel, 
   1.138 +										  TInt aAccessType, TTime aStartDate, TTime aEndDate, 
   1.139 +										  CCTKeyInfo*& aKeyInfoOut, TRequestStatus& aStatus)
   1.140 +	{
   1.141 +	TBool isEncrypted = TASN1DecPKCS8::IsEncryptedPKCS8Data(aKeyData);
   1.142 +	StartAsyncOperation(isEncrypted ? EImportKeyEncrypted : EImportKey, aStatus);
   1.143 +
   1.144 +	ASSERT(!iKeyData);
   1.145 +	iKeyData = aKeyData.Alloc();
   1.146 +	if (!iKeyData)	//	OOM or some other catastrophe
   1.147 +		{
   1.148 +		Complete(KErrNoMemory);
   1.149 +		return;
   1.150 +		}
   1.151 +	
   1.152 +	TRAPD(err, PrepareToCreateKeyL(aKeyStoreIndex, aUsage, 0, aLabel, CCTKeyInfo::EInvalidAlgorithm, aAccessType,
   1.153 +								   aStartDate, aEndDate, aStatus));
   1.154 +	if (KErrNone != err)
   1.155 +		{
   1.156 +		Complete(err);
   1.157 +		return;
   1.158 +		}
   1.159 +
   1.160 +	iKeyInfoOut = &aKeyInfoOut;
   1.161 +	aKeyInfoOut = NULL;
   1.162 +
   1.163 +	if (isEncrypted)
   1.164 +		{
   1.165 +		iKeyStoreManager->ImportEncryptedKey(*iKeyData, iKeyInfo, iStatus);
   1.166 +		}
   1.167 +	else
   1.168 +		{
   1.169 +		iKeyStoreManager->ImportKey(*iKeyData, iKeyInfo, iStatus);
   1.170 +		}
   1.171 +	SetActive();
   1.172 +	}
   1.173 +
   1.174 +EXPORT_C void CUnifiedKeyStore::CancelImportKey()
   1.175 +	{
   1.176 +	if (iState == EImportKey ||
   1.177 +		iState == EImportKeyEncrypted)
   1.178 +		{
   1.179 +		Cancel();
   1.180 +		}
   1.181 +	}
   1.182 +
   1.183 +void CUnifiedKeyStore::PrepareToCreateKeyL(	TInt aKeyStoreIndex,
   1.184 +											TKeyUsagePKCS15 aUsage, TUint aSize, 
   1.185 +											const TDesC& aLabel, 
   1.186 +											CCTKeyInfo::EKeyAlgorithm aAlgorithm,
   1.187 +											TInt aAccessType, 
   1.188 +											TTime aStartTime, TTime aEndTime,
   1.189 +											TRequestStatus& aStatus)
   1.190 +	{
   1.191 +	ASSERT(!iKeyStoreManager);
   1.192 +	
   1.193 +	//	These values are filled in by the server when the key is created
   1.194 +	TKeyIdentifier keyID;
   1.195 +	keyID.MaxSize();
   1.196 +	keyID.FillZ(keyID.MaxSize());
   1.197 +	TInt keyHandle = 0;
   1.198 +
   1.199 +	// Get the secure ID of the current process
   1.200 +	RProcess thisProcess;
   1.201 +	User::LeaveIfError(thisProcess.Open(thisProcess.Id()));
   1.202 +	TSecureId creatorId = thisProcess.SecureId();
   1.203 +	thisProcess.Close();
   1.204 +
   1.205 +	// Default management policy: resict to creating process
   1.206 +	TSecurityPolicy managementPolicy(creatorId);
   1.207 +
   1.208 +	// Default use policy: also resict to creating process
   1.209 +	TSecurityPolicy usePolicy(creatorId);
   1.210 +
   1.211 +	HBufC* label = aLabel.AllocLC();
   1.212 +
   1.213 +	// Panics if keystore manager index invalid
   1.214 +	MCTKeyStoreManager& keystore = KeyStoreManager(aKeyStoreIndex);
   1.215 +
   1.216 +	iKeyInfo = CCTKeyInfo::NewL(keyID, aUsage, aSize, NULL, label, keystore.Token(),
   1.217 +								keyHandle, usePolicy, managementPolicy, aAlgorithm,
   1.218 +								aAccessType, ETrue, aStartTime, aEndTime);
   1.219 +	CleanupStack::Pop(label);
   1.220 +
   1.221 +	iKeyStoreManager = &keystore;
   1.222 +	iOriginalRequestStatus = &aStatus;
   1.223 +	aStatus = KRequestPending;
   1.224 +	}
   1.225 +
   1.226 +//	************************************************************************
   1.227 +//	MKeyStore
   1.228 +//	************************************************************************
   1.229 +
   1.230 +void CUnifiedKeyStore::List(RMPointerArray<CCTKeyInfo>& aKeys, 
   1.231 +							const TCTKeyAttributeFilter& aFilter, 
   1.232 +							TRequestStatus& aStatus)
   1.233 +	{
   1.234 +	StartAsyncOperation(EList, aStatus);
   1.235 +
   1.236 +	iKeyInfos = &aKeys;
   1.237 +
   1.238 +	delete iFilter;
   1.239 +	iFilter = new TCTKeyAttributeFilter(aFilter);
   1.240 +	if (!iFilter)
   1.241 +		{
   1.242 +		Complete(KErrNoMemory);
   1.243 +		return;
   1.244 +		}
   1.245 +	
   1.246 +	iIndex = -1;
   1.247 +
   1.248 +	SetActive();
   1.249 +	TRequestStatus* status = &iStatus;
   1.250 +	User::RequestComplete(status, KErrNone);
   1.251 +}
   1.252 +
   1.253 +void CUnifiedKeyStore::CancelList()
   1.254 +	{
   1.255 +	if (iState == EList)
   1.256 +		{
   1.257 +		Cancel();
   1.258 +		}
   1.259 +	}
   1.260 +
   1.261 +void CUnifiedKeyStore::GetKeyInfo(TCTTokenObjectHandle aHandle, 
   1.262 +										   CCTKeyInfo*& aKeyInfo,
   1.263 +										   TRequestStatus& aStatus)
   1.264 +	{
   1.265 +	StartAsyncOperation(EGetKeyInfo, aStatus);
   1.266 +	
   1.267 +	ASSERT(!iKeyStore);	
   1.268 +	iKeyStore = FindKeyStore(aHandle);
   1.269 +	if (!iKeyStore)
   1.270 +		{
   1.271 +		Complete(KErrNotFound);
   1.272 +		return;
   1.273 +		}
   1.274 +
   1.275 +	iKeyStore->GetKeyInfo(aHandle, aKeyInfo, iStatus);
   1.276 +	SetActive();
   1.277 +	}
   1.278 +
   1.279 +void CUnifiedKeyStore::CancelGetKeyInfo()
   1.280 +	{
   1.281 +	if (iState == EGetKeyInfo)
   1.282 +		{
   1.283 +		Cancel();
   1.284 +		}
   1.285 +	}
   1.286 +
   1.287 +// Implementation for most of the Open() method
   1.288 +TBool CUnifiedKeyStore::DoOpen(const TCTTokenObjectHandle& aHandle, 
   1.289 +							   TRequestStatus& aStatus)
   1.290 +	{
   1.291 +	StartAsyncOperation(EOpen, aStatus);
   1.292 +	
   1.293 +	ASSERT(!iKeyStore);	
   1.294 +	iKeyStore = FindKeyStore(aHandle);
   1.295 +	if (!iKeyStore)
   1.296 +		{
   1.297 +		Complete(KErrNotFound);
   1.298 +		return EFalse;
   1.299 +		} 
   1.300 +
   1.301 +	SetActive();
   1.302 +	return ETrue;
   1.303 +	}
   1.304 +
   1.305 +void CUnifiedKeyStore::Open(const TCTTokenObjectHandle& aHandle, 
   1.306 +							MRSASigner*& aSigner,
   1.307 +							TRequestStatus& aStatus)
   1.308 +	{
   1.309 +	if (DoOpen(aHandle, aStatus))
   1.310 +		{
   1.311 +		iKeyStore->Open(aHandle, aSigner, iStatus);
   1.312 +		}
   1.313 +	}
   1.314 +
   1.315 +void CUnifiedKeyStore::Open(const TCTTokenObjectHandle& aHandle, 
   1.316 +							MDSASigner*& aSigner, 
   1.317 +							TRequestStatus& aStatus)
   1.318 +	{
   1.319 +	if (DoOpen(aHandle, aStatus))
   1.320 +		{			
   1.321 +		iKeyStore->Open(aHandle, aSigner, iStatus);
   1.322 +		}
   1.323 +	}
   1.324 +
   1.325 +void CUnifiedKeyStore::Open(const TCTTokenObjectHandle& aHandle, 
   1.326 +							MCTDecryptor*& aDecryptor,
   1.327 +							TRequestStatus& aStatus)
   1.328 +	{
   1.329 +	if (DoOpen(aHandle, aStatus))
   1.330 +		{
   1.331 +		iKeyStore->Open(aHandle, aDecryptor, iStatus);
   1.332 +		}
   1.333 +	}
   1.334 +
   1.335 +void CUnifiedKeyStore::Open(const TCTTokenObjectHandle& aHandle, 
   1.336 +							MCTDH*& aDH, TRequestStatus& aStatus)
   1.337 +	{	
   1.338 +	if (DoOpen(aHandle, aStatus))
   1.339 +		{
   1.340 +		iKeyStore->Open(aHandle, aDH, iStatus);
   1.341 +		}
   1.342 +	}
   1.343 +
   1.344 +void CUnifiedKeyStore::CancelOpen()
   1.345 +	{
   1.346 +	if (iState == EOpen)
   1.347 +		{
   1.348 +		Cancel();
   1.349 +		}
   1.350 +	}
   1.351 +
   1.352 +/** Returns the public key in DER-encoded ASN-1 */
   1.353 +void CUnifiedKeyStore::ExportPublic(const TCTTokenObjectHandle& aHandle,
   1.354 +									HBufC8*& aPublicKey,
   1.355 +									TRequestStatus& aStatus)
   1.356 +	{
   1.357 +	StartAsyncOperation(EExportPublic, aStatus);
   1.358 +
   1.359 +	iKeyStore = FindKeyStore(aHandle);
   1.360 +	if (!iKeyStore)
   1.361 +		{
   1.362 +		Complete(KErrNotFound);
   1.363 +		return;
   1.364 +		}
   1.365 +		
   1.366 +	iKeyStore->ExportPublic(aHandle, aPublicKey, iStatus);
   1.367 +	SetActive();
   1.368 +	}
   1.369 +
   1.370 +void CUnifiedKeyStore::CancelExportPublic()
   1.371 +	{
   1.372 +	if (iState == EExportPublic)
   1.373 +		{
   1.374 +		Cancel();
   1.375 +		}
   1.376 +	}
   1.377 +
   1.378 +#ifdef SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT
   1.379 +void CUnifiedKeyStore::Open(const TCTTokenObjectHandle& aHandle,
   1.380 +                            CryptoSpi::CSigner*& aSigner,
   1.381 +                            TRequestStatus& aStatus)
   1.382 +    {
   1.383 +    if (DoOpen(aHandle, aStatus))
   1.384 +        {
   1.385 +        iKeyStore->Open(aHandle, aSigner, iStatus);
   1.386 +        }
   1.387 +    }
   1.388 +
   1.389 +void CUnifiedKeyStore::Open(const TCTTokenObjectHandle& aHandle,
   1.390 +                            CryptoSpi:: CAsymmetricCipher*& asymmetricCipherObj,
   1.391 +                            TRequestStatus& aStatus)
   1.392 +    {
   1.393 +    if (DoOpen(aHandle, aStatus))
   1.394 +        {
   1.395 +        iKeyStore->Open(aHandle, asymmetricCipherObj, iStatus);
   1.396 +        }
   1.397 +    }
   1.398 +
   1.399 +void CUnifiedKeyStore::Decrypt(const TCTTokenObjectHandle& aHandle,
   1.400 +                               const TDesC8& aCiphertext,
   1.401 +                               HBufC8*& aPlaintextPtr,
   1.402 +                               TRequestStatus& aStatus)
   1.403 +    {
   1.404 +    if (DoOpen(aHandle, aStatus))
   1.405 +        {
   1.406 +        iKeyStore->Decrypt(aHandle, aCiphertext, aPlaintextPtr, iStatus);
   1.407 +        }
   1.408 +    }
   1.409 +
   1.410 +void CUnifiedKeyStore::Sign(const TCTTokenObjectHandle& aHandle,
   1.411 +                            const TDesC8& aPlaintext,
   1.412 +                            CryptoSpi::CCryptoParams*& aSignature,
   1.413 +                            TRequestStatus& aStatus)
   1.414 +    {
   1.415 +    if (DoOpen(aHandle, aStatus))
   1.416 +        {
   1.417 +        iKeyStore->Sign(aHandle, aPlaintext, aSignature, iStatus);
   1.418 +        }
   1.419 +    }
   1.420 +#endif //SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT
   1.421 +
   1.422 +//	************************************************************************
   1.423 +//	MKeyStoreManager
   1.424 +//	************************************************************************
   1.425 +	
   1.426 +EXPORT_C void CUnifiedKeyStore::ExportKey(TCTTokenObjectHandle aHandle, 
   1.427 +										  HBufC8*& aKey, TRequestStatus& aStatus)
   1.428 +	{
   1.429 +	StartAsyncOperation(EExportKey, aStatus);
   1.430 +
   1.431 +	ASSERT(!iKeyStoreManager);
   1.432 +	iKeyStoreManager = FindKeyStoreManager(aHandle);
   1.433 +	if (!iKeyStoreManager)
   1.434 +		{
   1.435 +		Complete(KErrNotFound);
   1.436 +		return;
   1.437 +		} 
   1.438 +
   1.439 +	iKeyStoreManager->ExportKey(aHandle, aKey, iStatus);
   1.440 +	SetActive();
   1.441 +	}
   1.442 +
   1.443 +EXPORT_C void CUnifiedKeyStore::CancelExportKey()
   1.444 +	{
   1.445 +	if (iState == EExportKey)
   1.446 +		{
   1.447 +		Cancel();
   1.448 +		}
   1.449 +	} 
   1.450 +
   1.451 +EXPORT_C void CUnifiedKeyStore::ExportEncryptedKey(TCTTokenObjectHandle aHandle, 
   1.452 +												   const CPBEncryptParms& aEncryptParams,
   1.453 +												   HBufC8*& aKey, TRequestStatus& aStatus)
   1.454 +	{
   1.455 +	StartAsyncOperation(EExportEncryptedKey, aStatus);
   1.456 +	
   1.457 +	ASSERT(!iKeyStoreManager);	
   1.458 +	iKeyStoreManager = FindKeyStoreManager(aHandle);
   1.459 +	if (!iKeyStoreManager)
   1.460 +		{
   1.461 +		Complete(KErrNotFound);
   1.462 +		return;
   1.463 +		} 
   1.464 +		
   1.465 +	iKeyStoreManager->ExportEncryptedKey(aHandle, aEncryptParams, aKey, iStatus);
   1.466 +	SetActive();
   1.467 +	}
   1.468 +
   1.469 +EXPORT_C void CUnifiedKeyStore::CancelExportEncryptedKey()
   1.470 +	{
   1.471 +	if (iState == EExportEncryptedKey)
   1.472 +		{
   1.473 +		Cancel();
   1.474 +		}	
   1.475 +	}
   1.476 +
   1.477 +EXPORT_C void CUnifiedKeyStore::DeleteKey(TCTTokenObjectHandle aHandle, 
   1.478 +										  TRequestStatus& aStatus)
   1.479 +	{
   1.480 +	StartAsyncOperation(EDeleteKey, aStatus);
   1.481 +
   1.482 +	iKeyStoreManager = FindKeyStoreManager(aHandle);
   1.483 +	if (!iKeyStoreManager)
   1.484 +		{
   1.485 +		Complete(KErrNotFound);
   1.486 +		return;
   1.487 +		}
   1.488 +	
   1.489 +	iKeyStoreManager->DeleteKey(aHandle, iStatus);
   1.490 +	SetActive();
   1.491 +	}
   1.492 +
   1.493 +EXPORT_C void CUnifiedKeyStore::CancelDeleteKey()
   1.494 +	{
   1.495 +	if (iState == EDeleteKey)
   1.496 +		{
   1.497 +		Cancel();
   1.498 +		}
   1.499 +	}
   1.500 +
   1.501 +EXPORT_C void CUnifiedKeyStore::SetUsePolicy(TCTTokenObjectHandle aHandle, 
   1.502 +											 const TSecurityPolicy& aPolicy,
   1.503 +											 TRequestStatus& aStatus)
   1.504 +	{
   1.505 +	StartAsyncOperation(ESetUsePolicy, aStatus);
   1.506 +
   1.507 +	iKeyStoreManager = FindKeyStoreManager(aHandle);
   1.508 +	if (!iKeyStoreManager)
   1.509 +		{
   1.510 +		Complete(KErrNotFound);
   1.511 +		return;
   1.512 +		}
   1.513 +	
   1.514 +	iKeyStoreManager->SetUsePolicy(aHandle, aPolicy, iStatus);
   1.515 +	SetActive();
   1.516 +	}
   1.517 +
   1.518 +EXPORT_C void CUnifiedKeyStore::CancelSetUsePolicy()
   1.519 +	{
   1.520 +	if (iState == ESetUsePolicy)
   1.521 +		{
   1.522 +		Cancel();
   1.523 +		}
   1.524 +	}
   1.525 +
   1.526 +EXPORT_C void CUnifiedKeyStore::SetManagementPolicy(TCTTokenObjectHandle aHandle, 
   1.527 +													const TSecurityPolicy& aPolicy,
   1.528 +													TRequestStatus& aStatus)
   1.529 +	{
   1.530 +	StartAsyncOperation(ESetManagementPolicy, aStatus);
   1.531 +
   1.532 +	iKeyStoreManager = FindKeyStoreManager(aHandle);
   1.533 +	if (!iKeyStoreManager)
   1.534 +		{
   1.535 +		Complete(KErrNotFound);
   1.536 +		return;
   1.537 +		}
   1.538 +	
   1.539 +	iKeyStoreManager->SetManagementPolicy(aHandle, aPolicy, iStatus);
   1.540 +	SetActive();
   1.541 +	}
   1.542 +
   1.543 +EXPORT_C void CUnifiedKeyStore::CancelSetManagementPolicy()
   1.544 +	{
   1.545 +	if (iState == ESetManagementPolicy)
   1.546 +		{
   1.547 +		Cancel();
   1.548 +		}
   1.549 +	}
   1.550 +
   1.551 +EXPORT_C void CUnifiedKeyStore::SetPassphraseTimeout(TInt aTimeout, 
   1.552 +													 TRequestStatus& aStatus)
   1.553 +	{
   1.554 +	StartAsyncOperation(ESetPassphraseTimeout, aStatus);
   1.555 +
   1.556 +	iIndex = -1;
   1.557 +	iNewTimeout = aTimeout;
   1.558 +	SetActive();
   1.559 +	
   1.560 +	TRequestStatus* status = &iStatus;
   1.561 +	User::RequestComplete(status, KErrNone);
   1.562 +	}
   1.563 +
   1.564 +EXPORT_C void CUnifiedKeyStore::CancelSetPassphraseTimeout()
   1.565 +	{
   1.566 +	if (iState == ESetPassphraseTimeout)
   1.567 +		{
   1.568 +		Cancel();
   1.569 +		}
   1.570 +	}
   1.571 +
   1.572 +EXPORT_C void CUnifiedKeyStore::Relock(TRequestStatus& aStatus)
   1.573 +	{
   1.574 +	StartAsyncOperation(ERelock, aStatus);
   1.575 +
   1.576 +	iIndex = -1;
   1.577 +	SetActive();
   1.578 +	
   1.579 +	TRequestStatus* status = &iStatus;
   1.580 +	User::RequestComplete(status, KErrNone);
   1.581 +	}
   1.582 +
   1.583 +EXPORT_C void CUnifiedKeyStore::CancelRelock()
   1.584 +	{
   1.585 +	if (iState == ERelock)
   1.586 +		{
   1.587 +		Cancel();
   1.588 +		}
   1.589 +	}
   1.590 +
   1.591 +//	************************************************************************
   1.592 +//	Other exports
   1.593 +//	************************************************************************
   1.594 +
   1.595 +EXPORT_C TInt CUnifiedKeyStore::KeyStoreCount() const
   1.596 +{
   1.597 +	return (iKeyStoresHolder.Count());
   1.598 +}
   1.599 +
   1.600 +EXPORT_C MCTKeyStore& CUnifiedKeyStore::KeyStore(TInt aIndex)
   1.601 +{
   1.602 +	__ASSERT_ALWAYS(aIndex >= 0 && aIndex < iKeyStoresHolder.Count(),
   1.603 +					User::Panic(KUnifiedKeyStore, EArrayAccessOutOfBounds));
   1.604 +	
   1.605 +	MCTKeyStore* keyStore = static_cast<MCTKeyStore*>(iKeyStoresHolder[aIndex]->KeyStore());
   1.606 +	return (*keyStore);
   1.607 +}
   1.608 +
   1.609 +EXPORT_C TInt CUnifiedKeyStore::KeyStoreManagerCount() const
   1.610 +	{
   1.611 +	TInt result = 0;
   1.612 +	for (TInt i = 0 ; i < iKeyStoresHolder.Count() ; ++i)
   1.613 +		{
   1.614 +		if (iKeyStoresHolder[i]->IsKeyManager())
   1.615 +			{
   1.616 +			++result;
   1.617 +			}
   1.618 +		}
   1.619 +	return result;
   1.620 +	}
   1.621 +
   1.622 +EXPORT_C MCTKeyStoreManager& CUnifiedKeyStore::KeyStoreManager(TInt aIndex)
   1.623 +	{
   1.624 +	__ASSERT_ALWAYS(aIndex >= 0, User::Panic(KUnifiedKeyStore, EArrayAccessOutOfBounds));
   1.625 +	TInt managerIndex = 0;
   1.626 +	MCTKeyStoreManager* result = NULL;
   1.627 +	for (TInt i = 0 ; i < iKeyStoresHolder.Count() ; ++i)
   1.628 +		{
   1.629 +		if (iKeyStoresHolder[i]->IsKeyManager())
   1.630 +			{
   1.631 +			if (managerIndex == aIndex)
   1.632 +				{
   1.633 +				result = static_cast<MCTKeyStoreManager*>(iKeyStoresHolder[i]->KeyStore());
   1.634 +				break;
   1.635 +				}
   1.636 +			++managerIndex;
   1.637 +			}
   1.638 +		}
   1.639 +	__ASSERT_ALWAYS(result != NULL, User::Panic(KUnifiedKeyStore, EArrayAccessOutOfBounds));
   1.640 +	return *result;
   1.641 +	}
   1.642 +
   1.643 +#ifdef SYMBIAN_AUTH_SERVER
   1.644 +	
   1.645 +EXPORT_C void CUnifiedKeyStore::CreateKey(	TInt aKeyStoreIndex, TKeyUsagePKCS15 aUsage,TUint aSize, 
   1.646 +								const TDesC& aLabel, CCTKeyInfo::EKeyAlgorithm aAlgorithm, 
   1.647 +								TInt aAccessType, TTime aStartDate, TTime aEndDate, 
   1.648 +								const TDesC& aAuthenticationString, TInt aFreshness,
   1.649 +								CCTKeyInfo*& aKeyInfoOut, TRequestStatus& aStatus)
   1.650 +		{
   1.651 +		
   1.652 +		StartAsyncOperation(ECreateKey, aStatus);
   1.653 +		TRAPD(err, PrepareToCreateKeyL(aKeyStoreIndex, aUsage, aSize, aLabel, aAlgorithm, aAccessType,
   1.654 +									   aStartDate, aEndDate, aStatus));
   1.655 +		if (KErrNone != err)
   1.656 +			{
   1.657 +			Complete(err);
   1.658 +			return;
   1.659 +			}
   1.660 +		
   1.661 +		iKeyInfoOut = &aKeyInfoOut;
   1.662 +		aKeyInfoOut = NULL;
   1.663 +		iKeyStoreManager->CreateKey(aAuthenticationString, aFreshness, iKeyInfo, iStatus);
   1.664 +		SetActive();
   1.665 +		
   1.666 +		}
   1.667 +
   1.668 +
   1.669 +EXPORT_C void CUnifiedKeyStore::ImportKey(	TInt aKeyStoreIndex, const TDesC8& aKeyData,
   1.670 +								TKeyUsagePKCS15 aUsage, const TDesC& aLabel, 
   1.671 +								TInt aAccessType, TTime aStartDate, TTime aEndDate, 
   1.672 +								const TDesC& aAuthenticationString, TInt aFreshness,
   1.673 +								CCTKeyInfo*& aKeyInfoOut, TRequestStatus& aStatus)
   1.674 +		{
   1.675 +		TBool isEncrypted = TASN1DecPKCS8::IsEncryptedPKCS8Data(aKeyData);
   1.676 +		StartAsyncOperation(isEncrypted ? EImportKeyEncrypted : EImportKey, aStatus);
   1.677 +
   1.678 +		ASSERT(!iKeyData);
   1.679 +		iKeyData = aKeyData.Alloc();
   1.680 +		if (!iKeyData)	//	OOM or some other catastrophe
   1.681 +			{
   1.682 +			Complete(KErrNoMemory);
   1.683 +			return;
   1.684 +			}
   1.685 +		
   1.686 +		TRAPD(err, PrepareToCreateKeyL(aKeyStoreIndex, aUsage, 0, aLabel, CCTKeyInfo::EInvalidAlgorithm, aAccessType,
   1.687 +									   aStartDate, aEndDate, aStatus));
   1.688 +		if (KErrNone != err)
   1.689 +			{
   1.690 +			Complete(err);
   1.691 +			return;
   1.692 +			}
   1.693 +
   1.694 +		iKeyInfoOut = &aKeyInfoOut;
   1.695 +		aKeyInfoOut = NULL;
   1.696 +
   1.697 +		if (isEncrypted)
   1.698 +			{
   1.699 +			iKeyStoreManager->ImportEncryptedKey(*iKeyData, aAuthenticationString, aFreshness, iKeyInfo, iStatus);
   1.700 +			}
   1.701 +		else
   1.702 +			{
   1.703 +			iKeyStoreManager->ImportKey(*iKeyData, aAuthenticationString, aFreshness, iKeyInfo, iStatus);
   1.704 +			}
   1.705 +		SetActive();
   1.706 +		}
   1.707 +
   1.708 +EXPORT_C void CUnifiedKeyStore::SetAuthenticationPolicy(	const TCTTokenObjectHandle aHandle, 
   1.709 +															const TDesC& aAuthenticationString,
   1.710 +															TInt aFreshness,					
   1.711 +															TRequestStatus& aStatus)
   1.712 +	{
   1.713 +	StartAsyncOperation(ESetAuthenticationPolicy, aStatus);
   1.714 +		
   1.715 +	ASSERT(!iKeyStoreManager);	
   1.716 +	iKeyStoreManager = FindKeyStoreManager(aHandle);
   1.717 +	if (!iKeyStoreManager)
   1.718 +		{
   1.719 +		Complete(KErrNotFound);
   1.720 +		return;
   1.721 +		} 
   1.722 +		
   1.723 +	iKeyStoreManager->SetAuthenticationPolicy(aHandle, aAuthenticationString, aFreshness, iStatus);
   1.724 +	SetActive();
   1.725 +		
   1.726 +	}
   1.727 +
   1.728 +EXPORT_C void CUnifiedKeyStore::GetAuthenticationPolicy(	const TCTTokenObjectHandle aHandle, 
   1.729 +															HBufC*& aAuthenticationString,
   1.730 +															TInt& aFreshness,					
   1.731 +															TRequestStatus& aStatus)
   1.732 +	{
   1.733 +	StartAsyncOperation(EGetAuthenticationPolicy, aStatus);
   1.734 +		
   1.735 +	ASSERT(!iKeyStoreManager);	
   1.736 +	iKeyStoreManager = FindKeyStoreManager(aHandle);
   1.737 +	if (!iKeyStoreManager)
   1.738 +		{
   1.739 +		Complete(KErrNotFound);
   1.740 +		return;
   1.741 +		} 
   1.742 +		
   1.743 +	iKeyStoreManager->GetAuthenticationPolicy(aHandle, aAuthenticationString, aFreshness, iStatus);
   1.744 +	SetActive();
   1.745 +		
   1.746 +	}
   1.747 +
   1.748 +#endif // SYMBIAN_AUTH_SERVER
   1.749 +
   1.750 +CUnifiedKeyStore::CUnifiedKeyStore(RFs& aFs)
   1.751 +	:	CActive(EPriorityNormal), iFs(aFs), iState(EIdle)
   1.752 +{//	Currently defaults to always try for key store manager interface
   1.753 +//	This may change (add parameter to NewL for required interface)
   1.754 +	iRequestUid.iUid = KInterfaceKeyStoreManager;
   1.755 +	CActiveScheduler::Add(this);
   1.756 +}
   1.757 +
   1.758 +void CUnifiedKeyStore::ConstructL()
   1.759 +{}
   1.760 +
   1.761 +void CUnifiedKeyStore::StartAsyncOperation(TState aState, TRequestStatus& aStatus)
   1.762 +	{
   1.763 +	ASSERT(iState == EIdle);
   1.764 +	ASSERT(iOriginalRequestStatus == NULL);
   1.765 +	iState = aState;
   1.766 +	iOriginalRequestStatus = &aStatus;
   1.767 +	aStatus = KRequestPending;
   1.768 +	}
   1.769 +
   1.770 +MCTKeyStore* CUnifiedKeyStore::FindKeyStore(const TCTTokenObjectHandle& aHandle)
   1.771 +	{
   1.772 +	for (TInt index = 0 ; index < iKeyStoresHolder.Count() ; ++index)
   1.773 +		{
   1.774 +		MCTTokenInterface* store = iKeyStoresHolder[index]->KeyStore();
   1.775 +		ASSERT(store);
   1.776 +		if (store->Token().Handle() == aHandle.iTokenHandle)
   1.777 +			{
   1.778 +			return static_cast<MCTKeyStoreManager*>(store);
   1.779 +			}
   1.780 +		}
   1.781 +	return NULL;
   1.782 +	}
   1.783 +	
   1.784 +MCTKeyStoreManager* CUnifiedKeyStore::FindKeyStoreManager(const TCTTokenObjectHandle& aHandle)
   1.785 +	{
   1.786 +	for (TInt index = 0 ; index < iKeyStoresHolder.Count() ; ++index)
   1.787 +		{
   1.788 +		MCTTokenInterface* store = iKeyStoresHolder[index]->KeyStore();
   1.789 +		ASSERT(store);
   1.790 +		if (store->Token().Handle() == aHandle.iTokenHandle && iKeyStoresHolder[index]->IsKeyManager())
   1.791 +			{
   1.792 +			return static_cast<MCTKeyStoreManager*>(store);
   1.793 +			}
   1.794 +		}
   1.795 +	return NULL;
   1.796 +	}
   1.797 +	
   1.798 +void CUnifiedKeyStore::RunL()
   1.799 +{
   1.800 +	if (EInitializeGetKeyUserInterfaceFinished != iState &&
   1.801 +		EInitializeGetKeyUserInterface != iState && 
   1.802 +		EInitializeGetToken != iState)
   1.803 +	{
   1.804 +		User::LeaveIfError(iStatus.Int());
   1.805 +	}
   1.806 +
   1.807 +	switch (iState)
   1.808 +	{
   1.809 +		case EInitializeGetTokenList:
   1.810 +		{//	Try to get a list of Tokens for each of the Token Types
   1.811 +			iIndexTokenTypes++;
   1.812 +			if (iIndexTokenTypes < iTokenTypes.Count())
   1.813 +			{
   1.814 +				__ASSERT_DEBUG(!iTokenType, User::Panic(KUnifiedKeyStore, EArrayAccessOutOfBounds));
   1.815 +				iTokenType = MCTTokenType::NewL(*iTokenTypes[iIndexTokenTypes], iFs);
   1.816 +				__ASSERT_DEBUG(iTokens.Count()==0, User::Panic(KUnifiedKeyStore, ETokensArrayAlreadyInUse));
   1.817 +				iTokenType->List(iTokens, iStatus);
   1.818 +				iIndexTokens = -1;
   1.819 +				iState = EInitializeGetToken;
   1.820 +			}
   1.821 +			else
   1.822 +			{// We don't need the list of Token Types anymore
   1.823 +				iTokenTypes.ResetAndDestroy();
   1.824 +				iTokenTypes.Close();
   1.825 +				iState = EInitializeFinished;
   1.826 +				TRequestStatus* status = &iStatus;
   1.827 +				User::RequestComplete(status, KErrNone);
   1.828 +			}
   1.829 +			SetActive();
   1.830 +			break;
   1.831 +		}
   1.832 +		case EInitializeGetToken:
   1.833 +		{
   1.834 +			if (iStatus.Int() == KErrHardwareNotAvailable)
   1.835 +				{
   1.836 +				// If the hardware corresponding to this
   1.837 +				// TokenType has been removed then just skip it
   1.838 +				// but DO NOT leave!
   1.839 +				++iIndexTokens;
   1.840 +				iState = EInitializeGetToken;
   1.841 +				TRequestStatus* status = &iStatus;
   1.842 +				User::RequestComplete(status, KErrNone);
   1.843 +				}
   1.844 +            else
   1.845 +				{
   1.846 +				User::LeaveIfError(iStatus.Int());
   1.847 +    			iIndexTokens++;		
   1.848 +		
   1.849 +				if (iIndexTokens < iTokens.Count())
   1.850 +					{
   1.851 +					iTokenType->OpenToken(*iTokens[iIndexTokens], iToken, iStatus);
   1.852 +					iRequestUid.iUid = KInterfaceKeyStoreManager;
   1.853 +					iState = EInitialiseGetKeyManagerInterface;
   1.854 +					}
   1.855 +				else
   1.856 +					{// Don't need the iTokenType anymore
   1.857 +					iTokenType->Release();
   1.858 +					iTokenType = 0;
   1.859 +
   1.860 +					iTokens.Close();	// Don't need the list of Tokens anymore
   1.861 +					iState = EInitializeGetTokenList;
   1.862 +					TRequestStatus* status = &iStatus;
   1.863 +					User::RequestComplete(status, KErrNone);
   1.864 +					}
   1.865 +				}
   1.866 +			SetActive();
   1.867 +			break;
   1.868 +		}
   1.869 +		case EInitialiseGetKeyManagerInterface:
   1.870 +		{// First try to get a manager interface to the store, if
   1.871 +		//	unsuccessful, try once to get a user interface
   1.872 +			if (iToken)
   1.873 +			{
   1.874 +				iRequestUid.iUid = KInterfaceKeyStoreManager;			
   1.875 +				iToken->GetInterface(iRequestUid, iTokenInterface, iStatus);
   1.876 +				iState = EInitializeGetKeyUserInterface;
   1.877 +				SetActive();			
   1.878 +			}
   1.879 +			else
   1.880 +			{//	No token
   1.881 +				User::Leave(KErrNotReady);
   1.882 +			}
   1.883 +			break;
   1.884 +		}
   1.885 +		case EInitializeGetKeyUserInterface:
   1.886 +		{//	Did we get a manager interface?
   1.887 +			if (iStatus==KErrNoMemory)
   1.888 +			{
   1.889 +				User::Leave(KErrNoMemory);
   1.890 +			}
   1.891 +
   1.892 +			if (iRequestUid.iUid==KInterfaceKeyStoreManager)
   1.893 +			{
   1.894 +				if (KErrNone==iStatus.Int())
   1.895 +				{//	Success! Store it and finish up
   1.896 +					CKeyStoreIF* keyStore = new (ELeave) CKeyStoreIF(iTokenInterface, ETrue);
   1.897 +					CleanupStack::PushL(keyStore);
   1.898 +					User::LeaveIfError(iKeyStoresHolder.Append(keyStore));
   1.899 +					CleanupStack::Pop(keyStore);
   1.900 +
   1.901 +					iTokenInterface = 0;
   1.902 +					iToken->Release();
   1.903 +					iToken = 0;
   1.904 +					iState = EInitializeGetToken;
   1.905 +					TRequestStatus* status = &iStatus;
   1.906 +					User::RequestComplete(status, KErrNone);
   1.907 +				}
   1.908 +				else
   1.909 +				{//	No luck getting a manager, so try getting a user
   1.910 +					iRequestUid.iUid = KInterfaceKeyStore;			
   1.911 +					iToken->GetInterface(iRequestUid, iTokenInterface, iStatus);
   1.912 +					iState = EInitializeGetKeyUserInterfaceFinished;					
   1.913 +				}
   1.914 +			}
   1.915 +			else if (iRequestUid.iUid==KInterfaceKeyStore) 
   1.916 +			{//	We were trying for user IF								
   1.917 +				if (iStatus==KErrNone)
   1.918 +				{
   1.919 +					if (iToken)
   1.920 +					{
   1.921 +						iRequestUid.iUid = KInterfaceKeyStore;			
   1.922 +						iToken->GetInterface(iRequestUid, iTokenInterface, iStatus);
   1.923 +						iState = EInitializeGetKeyUserInterfaceFinished;	
   1.924 +					}
   1.925 +					else
   1.926 +					{
   1.927 +						User::Leave(KErrNotReady);
   1.928 +					}
   1.929 +				}
   1.930 +				else
   1.931 +				{//	Couldn't even get a user IF
   1.932 +					User::Leave(iStatus.Int());
   1.933 +				}				
   1.934 +			}
   1.935 +
   1.936 +			SetActive();
   1.937 +			break;
   1.938 +		}
   1.939 +		case EInitializeGetKeyUserInterfaceFinished:
   1.940 +		{
   1.941 +			if (iStatus==KErrNone)
   1.942 +			{
   1.943 +				CKeyStoreIF* keyStore = new (ELeave) CKeyStoreIF(iTokenInterface, EFalse);
   1.944 +				CleanupStack::PushL(keyStore);
   1.945 +				User::LeaveIfError(iKeyStoresHolder.Append(keyStore));
   1.946 +				CleanupStack::Pop(keyStore);
   1.947 +
   1.948 +				iTokenInterface = 0;
   1.949 +				iToken->Release();
   1.950 +				iToken = 0;
   1.951 +				iState = EInitializeGetToken;
   1.952 +				TRequestStatus* status = &iStatus;
   1.953 +				User::RequestComplete(status, KErrNone);
   1.954 +			}
   1.955 +			else if (iStatus == KErrNoMemory)
   1.956 +			{
   1.957 +				User::Leave(KErrNoMemory);
   1.958 +			}
   1.959 +			else
   1.960 +			{
   1.961 +				iState = EInitializeGetToken;
   1.962 +				TRequestStatus* status = &iStatus;
   1.963 +				User::RequestComplete(status,iStatus.Int());
   1.964 +			}
   1.965 +			
   1.966 +			SetActive();
   1.967 +			break;
   1.968 +		}
   1.969 +		case EInitializeFinished:
   1.970 +			Complete(KErrNone);
   1.971 +			break;
   1.972 +			
   1.973 +		case EList:
   1.974 +		{//	iIndex has been initialized in List function
   1.975 +			++iIndex;
   1.976 +			if (iIndex < iKeyStoresHolder.Count())
   1.977 +				{
   1.978 +				iKeyStore = static_cast<MCTKeyStore*>(iKeyStoresHolder[iIndex]->KeyStore());
   1.979 +				ASSERT(iKeyStore);
   1.980 +				iKeyStore->List(*iKeyInfos, *iFilter, iStatus);
   1.981 +				SetActive();
   1.982 +				}
   1.983 +			else
   1.984 +				{
   1.985 +				Complete(KErrNone);
   1.986 +				}
   1.987 +			break;
   1.988 +		}
   1.989 +		
   1.990 +	    case EGetKeyInfo:
   1.991 +			Complete(KErrNone);
   1.992 +			break;
   1.993 +			
   1.994 +		case ECreateKey:
   1.995 +			*iKeyInfoOut = iKeyInfo;
   1.996 +			iKeyInfo = NULL; // Release ownership
   1.997 +			Complete(KErrNone);
   1.998 +			break;
   1.999 +			
  1.1000 +		case EImportKey:
  1.1001 +		case EImportKeyEncrypted:
  1.1002 +			*iKeyInfoOut = iKeyInfo;
  1.1003 +			iKeyInfo = NULL; // Release ownership
  1.1004 +			Complete(KErrNone);
  1.1005 +			break;
  1.1006 +			
  1.1007 +		case EExportKey:
  1.1008 +		case EExportEncryptedKey:
  1.1009 +			Complete(KErrNone);
  1.1010 +			break;		
  1.1011 +
  1.1012 +	    case ERelock:
  1.1013 +			++iIndex;
  1.1014 +			
  1.1015 +			// Find next key store manager
  1.1016 +			while (iIndex < iKeyStoresHolder.Count() && !iKeyStoresHolder[iIndex]->IsKeyManager())
  1.1017 +				++iIndex;
  1.1018 +			
  1.1019 +			if (iIndex < iKeyStoresHolder.Count())
  1.1020 +				{
  1.1021 +				iKeyStoreManager = static_cast<MCTKeyStoreManager*>(iKeyStoresHolder[iIndex]->KeyStore());
  1.1022 +				ASSERT(iKeyStoreManager);
  1.1023 +				iKeyStoreManager->Relock(iStatus);
  1.1024 +				SetActive();
  1.1025 +				}
  1.1026 +			else
  1.1027 +				{
  1.1028 +				Complete(KErrNone);
  1.1029 +				}
  1.1030 +			break;
  1.1031 +
  1.1032 +	    case ESetPassphraseTimeout:
  1.1033 +			++iIndex;
  1.1034 +			
  1.1035 +			// Find next key store manager
  1.1036 +			while (iIndex < iKeyStoresHolder.Count() && !iKeyStoresHolder[iIndex]->IsKeyManager())
  1.1037 +				++iIndex;
  1.1038 +			
  1.1039 +			if (iIndex < iKeyStoresHolder.Count())
  1.1040 +				{
  1.1041 +				iKeyStoreManager = static_cast<MCTKeyStoreManager*>(iKeyStoresHolder[iIndex]->KeyStore());
  1.1042 +				ASSERT(iKeyStoreManager);
  1.1043 +				iKeyStoreManager->SetPassphraseTimeout(iNewTimeout, iStatus);
  1.1044 +				SetActive();
  1.1045 +				}
  1.1046 +			else
  1.1047 +				{
  1.1048 +				Complete(KErrNone);
  1.1049 +				}
  1.1050 +			break;
  1.1051 +
  1.1052 +	    case EOpen:
  1.1053 +		case EExportPublic:
  1.1054 +	    case EDeleteKey:
  1.1055 +	    case ESetUsePolicy:
  1.1056 +	    case ESetManagementPolicy:
  1.1057 +	    case EGetAuthenticationPolicy:
  1.1058 +	    case ESetAuthenticationPolicy:
  1.1059 +			Complete(KErrNone);
  1.1060 +			break;
  1.1061 +		default:
  1.1062 +			User::Panic(KUnifiedKeyStore, EUnrecognisedState);
  1.1063 +			break;
  1.1064 +	}
  1.1065 +}
  1.1066 +
  1.1067 +TInt CUnifiedKeyStore::RunError(TInt aError)
  1.1068 +	{
  1.1069 +	Complete(aError); 
  1.1070 +	return KErrNone;
  1.1071 +	}
  1.1072 +
  1.1073 +void CUnifiedKeyStore::DoCancel()
  1.1074 +	{
  1.1075 +	// If the current state is the last state involved in handling a request, we
  1.1076 +	// check to see if we have already been completed - in this case we can
  1.1077 +	// simply complete the client with iStatus (this may be KErrNone).  If we
  1.1078 +	// have not we cancel the outstanding request and pass the resulting iStatus
  1.1079 +	// back to the client - this too may indicate a successful completion if the
  1.1080 +	// cancel arrived after the request was executed.
  1.1081 +	//
  1.1082 +	// For more complex cases, where there are more states to go through before
  1.1083 +	// we finish servicing the client request, we cancel any outstanding
  1.1084 +	// request, and return KErrCancel to the client.
  1.1085 +
  1.1086 +	switch (iState)
  1.1087 +		{
  1.1088 +		case EInitializeFinished:
  1.1089 +		case EGetKeyInfo:
  1.1090 +		case ECreateKey:
  1.1091 +		case EImportKey:
  1.1092 +		case EImportKeyEncrypted:
  1.1093 +		case EExportKey:
  1.1094 +		case EExportEncryptedKey:
  1.1095 +	    case EOpen:
  1.1096 +		case EExportPublic:
  1.1097 +	    case EDeleteKey:
  1.1098 +	    case ESetUsePolicy:
  1.1099 +	    case ESetManagementPolicy:
  1.1100 +			if (iStatus == KRequestPending)
  1.1101 +				{
  1.1102 +				// Attempt to cancel outstanding request and pass status back to
  1.1103 +				// client
  1.1104 +				CancelOutstandingRequest();
  1.1105 +				Complete(iStatus.Int());
  1.1106 +				}
  1.1107 +			else
  1.1108 +				{
  1.1109 +				// We've already been completed - call RunL() to process results
  1.1110 +				// and complete client
  1.1111 +				TRAPD(err, RunL());
  1.1112 +				if (err != KErrNone)
  1.1113 +					{
  1.1114 +					RunError(err);
  1.1115 +					}
  1.1116 +				}
  1.1117 +			break;
  1.1118 +
  1.1119 +		default:
  1.1120 +			CancelOutstandingRequest();
  1.1121 +			Complete(KErrCancel);
  1.1122 +			break;
  1.1123 +		}
  1.1124 +	}
  1.1125 +
  1.1126 +void CUnifiedKeyStore::CancelOutstandingRequest()
  1.1127 +	{
  1.1128 +	switch (iState)
  1.1129 +		{
  1.1130 +		case EInitializeGetTokenList:
  1.1131 +		case EInitializeGetToken:
  1.1132 +		case EInitialiseGetKeyManagerInterface:
  1.1133 +		case EInitializeGetKeyUserInterface:
  1.1134 +		case EInitializeGetKeyUserInterfaceFinished:
  1.1135 +		case EInitializeFinished:
  1.1136 +			// Don't have to cancel initialisation stuff - this happens when we
  1.1137 +			// release the objects in Cleanup().
  1.1138 +			iStatus = KErrCancel;
  1.1139 +			break;
  1.1140 +
  1.1141 +		case EList:
  1.1142 +			if (iKeyStore)
  1.1143 +				{
  1.1144 +				iKeyStore->CancelList();
  1.1145 +				}
  1.1146 +			break;
  1.1147 +
  1.1148 +		case EGetKeyInfo:
  1.1149 +			ASSERT(iKeyStore);
  1.1150 +			iKeyStore->CancelGetKeyInfo();
  1.1151 +			break;
  1.1152 +
  1.1153 +		case EOpen:
  1.1154 +			ASSERT(iKeyStore);
  1.1155 +			iKeyStore->CancelOpen();
  1.1156 +			break;
  1.1157 +
  1.1158 +		case EExportPublic:
  1.1159 +			ASSERT(iKeyStore);
  1.1160 +			iKeyStore->CancelExportPublic();
  1.1161 +			break;			
  1.1162 +
  1.1163 +		case ECreateKey:
  1.1164 +			ASSERT(iKeyStoreManager);
  1.1165 +			iKeyStoreManager->CancelCreateKey();
  1.1166 +			break;
  1.1167 +
  1.1168 +		case EImportKey:
  1.1169 +		case EImportKeyEncrypted:
  1.1170 +			ASSERT(iKeyStoreManager);
  1.1171 +			iKeyStoreManager->CancelImportKey();
  1.1172 +			break;
  1.1173 +			
  1.1174 +		case EExportKey:
  1.1175 +		case EExportEncryptedKey:
  1.1176 +			ASSERT(iKeyStoreManager);
  1.1177 +			iKeyStoreManager->CancelExportKey();
  1.1178 +			break;
  1.1179 +			
  1.1180 +		case EDeleteKey:
  1.1181 +			ASSERT(iKeyStoreManager);
  1.1182 +			iKeyStoreManager->CancelDeleteKey();
  1.1183 +			break;
  1.1184 +
  1.1185 +		case ERelock:
  1.1186 +			ASSERT(iKeyStoreManager);
  1.1187 +			iKeyStoreManager->CancelRelock();
  1.1188 +			break;
  1.1189 +
  1.1190 +		case ESetPassphraseTimeout:
  1.1191 +			ASSERT(iKeyStoreManager);
  1.1192 +			iKeyStoreManager->CancelSetPassphraseTimeout();
  1.1193 +			break;
  1.1194 +		
  1.1195 +	    case ESetUsePolicy:
  1.1196 +			ASSERT(iKeyStoreManager);
  1.1197 +			iKeyStoreManager->CancelSetUsePolicy();
  1.1198 +			break;
  1.1199 +
  1.1200 +	    case ESetManagementPolicy:
  1.1201 +			ASSERT(iKeyStoreManager);
  1.1202 +			iKeyStoreManager->CancelSetManagementPolicy();
  1.1203 +			break;
  1.1204 +
  1.1205 +		default:
  1.1206 +			User::Panic(KUnifiedKeyStore, EUnrecognisedState);
  1.1207 +			break;
  1.1208 +		}
  1.1209 +	}
  1.1210 +
  1.1211 +
  1.1212 +void CUnifiedKeyStore::Complete(TInt aError)
  1.1213 +	{
  1.1214 +	Cleanup();
  1.1215 +	if (iOriginalRequestStatus)
  1.1216 +		{
  1.1217 +		User::RequestComplete(iOriginalRequestStatus, aError);
  1.1218 +		}
  1.1219 +	}
  1.1220 +
  1.1221 +void CUnifiedKeyStore::Cleanup()
  1.1222 +	{
  1.1223 +	// If we have a key info, we want to release it
  1.1224 +	if (iKeyInfo)
  1.1225 +	{
  1.1226 +		iKeyInfo->Release();
  1.1227 +		iKeyInfo = NULL;
  1.1228 +	}
  1.1229 +
  1.1230 +	delete iKeyData;
  1.1231 +	iKeyData = NULL;
  1.1232 +
  1.1233 +	delete iFilter;
  1.1234 +	iFilter = NULL;
  1.1235 +
  1.1236 +	delete iPbeParams;
  1.1237 +	iPbeParams = NULL;
  1.1238 +
  1.1239 +	iTokenTypes.Close();
  1.1240 +
  1.1241 +	if (iTokenType)
  1.1242 +		{
  1.1243 +		iTokenType->Release();
  1.1244 +		iTokenType = 0;
  1.1245 +		}
  1.1246 +
  1.1247 +	iTokens.Close();
  1.1248 +
  1.1249 +	if (iToken)
  1.1250 +		{
  1.1251 +		iToken->Release();
  1.1252 +		iToken = 0;
  1.1253 +		}
  1.1254 +
  1.1255 +	if (iTokenInterface)
  1.1256 +		{
  1.1257 +		iTokenInterface->Release();
  1.1258 +		iTokenInterface = 0;
  1.1259 +		}
  1.1260 +	
  1.1261 +	iKeyInfoOut = NULL;
  1.1262 +	iKeyStore = NULL;
  1.1263 +	iKeyStoreManager = NULL;
  1.1264 +	
  1.1265 +	iState = EIdle;
  1.1266 +	}
  1.1267 +
  1.1268 +CUnifiedKeyStore::CKeyStoreIF::CKeyStoreIF(MCTTokenInterface* aKeyStore, TBool aIsKeyManager)
  1.1269 +:	iKeyStore(aKeyStore), iIsKeyManager(aIsKeyManager)
  1.1270 +{}
  1.1271 +
  1.1272 +CUnifiedKeyStore::CKeyStoreIF::~CKeyStoreIF()
  1.1273 +{
  1.1274 +	if (iKeyStore)
  1.1275 +	{
  1.1276 +		iKeyStore->Release();
  1.1277 +		iKeyStore = NULL;
  1.1278 +	}
  1.1279 +}
  1.1280 +