First public contribution.
2 * Copyright (c) 2001-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 "unifiedkeystore.h"
20 #include <ecom/ecom.h>
24 #include "mctcertappinterface.h"
25 #include <mctkeystoreuids.h>
27 _LIT(KUnifiedKeyStore, "UnifiedKeyStore");
29 /////////////////////////////////////////////////////////////////////////////////////
31 /////////////////////////////////////////////////////////////////////////////////////
33 EXPORT_C CUnifiedKeyStore* CUnifiedKeyStore::NewL(RFs& aFs)
35 CUnifiedKeyStore* self = CUnifiedKeyStore::NewLC(aFs);
36 CleanupStack::Pop(self);
40 EXPORT_C CUnifiedKeyStore* CUnifiedKeyStore::NewLC(RFs& aFs)
42 CUnifiedKeyStore* self = new(ELeave) CUnifiedKeyStore(aFs);
43 CleanupStack::PushL(self);
48 EXPORT_C CUnifiedKeyStore::~CUnifiedKeyStore()
53 iKeyStoresHolder.ResetAndDestroy();
54 iKeyStoresHolder.Close();
56 REComSession::FinalClose();
59 void CUnifiedKeyStore::DoInitializeL()
60 {// We want the list of all token types that support a keystore interface
61 RArray<TUid> uidArray;
62 CleanupClosePushL(uidArray);
64 User::LeaveIfError(uidArray.Append(TUid::Uid(KInterfaceKeyStore)));
66 TCTFindTokenTypesByInterface filter(uidArray.Array());
67 CCTTokenTypeInfo::ListL(iTokenTypes, filter);
69 CleanupStack::PopAndDestroy(); // uidArray
72 EXPORT_C void CUnifiedKeyStore::Initialize(TRequestStatus& aStatus)
73 {// The following assertion checks that we didn't call Initialize twice
74 __ASSERT_DEBUG((iKeyStoresHolder.Count()==0), User::Panic(KUnifiedKeyStore, EUnexpectedInitialise));
76 TRAPD(err, DoInitializeL());
79 TRequestStatus* status = &aStatus;
80 User::RequestComplete(status, err);
84 iIndexTokenTypes = -1;
85 StartAsyncOperation(EInitializeGetTokenList, aStatus);
88 TRequestStatus* status = &iStatus;
89 User::RequestComplete(status, KErrNone);
92 EXPORT_C void CUnifiedKeyStore::CancelInitialize()
94 if (iState == EInitializeGetTokenList ||
95 iState == EInitializeGetToken ||
96 iState == EInitialiseGetKeyManagerInterface ||
97 iState == EInitializeGetKeyUserInterface ||
98 iState == EInitializeGetKeyUserInterfaceFinished ||
99 iState == EInitializeFinished)
105 EXPORT_C void CUnifiedKeyStore::CreateKey(TInt aKeyStoreIndex, TKeyUsagePKCS15 aUsage,TUint aSize,
106 const TDesC& aLabel, CCTKeyInfo::EKeyAlgorithm aAlgorithm,
107 TInt aAccessType, TTime aStartDate, TTime aEndDate,
108 CCTKeyInfo*& aKeyInfoOut, TRequestStatus& aStatus)
110 StartAsyncOperation(ECreateKey, aStatus);
111 TRAPD(err, PrepareToCreateKeyL(aKeyStoreIndex, aUsage, aSize, aLabel, aAlgorithm, aAccessType,
112 aStartDate, aEndDate, aStatus));
119 iKeyInfoOut = &aKeyInfoOut;
121 iKeyStoreManager->CreateKey(iKeyInfo, iStatus);
125 EXPORT_C void CUnifiedKeyStore::CancelCreateKey()
127 if (iState == ECreateKey)
133 EXPORT_C void CUnifiedKeyStore::ImportKey(TInt aKeyStoreIndex, const TDesC8& aKeyData,
134 TKeyUsagePKCS15 aUsage, const TDesC& aLabel,
135 TInt aAccessType, TTime aStartDate, TTime aEndDate,
136 CCTKeyInfo*& aKeyInfoOut, TRequestStatus& aStatus)
138 TBool isEncrypted = TASN1DecPKCS8::IsEncryptedPKCS8Data(aKeyData);
139 StartAsyncOperation(isEncrypted ? EImportKeyEncrypted : EImportKey, aStatus);
142 iKeyData = aKeyData.Alloc();
143 if (!iKeyData) // OOM or some other catastrophe
145 Complete(KErrNoMemory);
149 TRAPD(err, PrepareToCreateKeyL(aKeyStoreIndex, aUsage, 0, aLabel, CCTKeyInfo::EInvalidAlgorithm, aAccessType,
150 aStartDate, aEndDate, aStatus));
157 iKeyInfoOut = &aKeyInfoOut;
162 iKeyStoreManager->ImportEncryptedKey(*iKeyData, iKeyInfo, iStatus);
166 iKeyStoreManager->ImportKey(*iKeyData, iKeyInfo, iStatus);
171 EXPORT_C void CUnifiedKeyStore::CancelImportKey()
173 if (iState == EImportKey ||
174 iState == EImportKeyEncrypted)
180 void CUnifiedKeyStore::PrepareToCreateKeyL( TInt aKeyStoreIndex,
181 TKeyUsagePKCS15 aUsage, TUint aSize,
183 CCTKeyInfo::EKeyAlgorithm aAlgorithm,
185 TTime aStartTime, TTime aEndTime,
186 TRequestStatus& aStatus)
188 ASSERT(!iKeyStoreManager);
190 // These values are filled in by the server when the key is created
191 TKeyIdentifier keyID;
193 keyID.FillZ(keyID.MaxSize());
196 // Get the secure ID of the current process
197 RProcess thisProcess;
198 User::LeaveIfError(thisProcess.Open(thisProcess.Id()));
199 TSecureId creatorId = thisProcess.SecureId();
202 // Default management policy: resict to creating process
203 TSecurityPolicy managementPolicy(creatorId);
205 // Default use policy: also resict to creating process
206 TSecurityPolicy usePolicy(creatorId);
208 HBufC* label = aLabel.AllocLC();
210 // Panics if keystore manager index invalid
211 MCTKeyStoreManager& keystore = KeyStoreManager(aKeyStoreIndex);
213 iKeyInfo = CCTKeyInfo::NewL(keyID, aUsage, aSize, NULL, label, keystore.Token(),
214 keyHandle, usePolicy, managementPolicy, aAlgorithm,
215 aAccessType, ETrue, aStartTime, aEndTime);
216 CleanupStack::Pop(label);
218 iKeyStoreManager = &keystore;
219 iOriginalRequestStatus = &aStatus;
220 aStatus = KRequestPending;
223 // ************************************************************************
225 // ************************************************************************
227 void CUnifiedKeyStore::List(RMPointerArray<CCTKeyInfo>& aKeys,
228 const TCTKeyAttributeFilter& aFilter,
229 TRequestStatus& aStatus)
231 StartAsyncOperation(EList, aStatus);
236 iFilter = new TCTKeyAttributeFilter(aFilter);
239 Complete(KErrNoMemory);
246 TRequestStatus* status = &iStatus;
247 User::RequestComplete(status, KErrNone);
250 void CUnifiedKeyStore::CancelList()
258 void CUnifiedKeyStore::GetKeyInfo(TCTTokenObjectHandle aHandle,
259 CCTKeyInfo*& aKeyInfo,
260 TRequestStatus& aStatus)
262 StartAsyncOperation(EGetKeyInfo, aStatus);
265 iKeyStore = FindKeyStore(aHandle);
268 Complete(KErrNotFound);
272 iKeyStore->GetKeyInfo(aHandle, aKeyInfo, iStatus);
276 void CUnifiedKeyStore::CancelGetKeyInfo()
278 if (iState == EGetKeyInfo)
284 // Implementation for most of the Open() method
285 TBool CUnifiedKeyStore::DoOpen(const TCTTokenObjectHandle& aHandle,
286 TRequestStatus& aStatus)
288 StartAsyncOperation(EOpen, aStatus);
291 iKeyStore = FindKeyStore(aHandle);
294 Complete(KErrNotFound);
302 void CUnifiedKeyStore::Open(const TCTTokenObjectHandle& aHandle,
303 MRSASigner*& aSigner,
304 TRequestStatus& aStatus)
306 if (DoOpen(aHandle, aStatus))
308 iKeyStore->Open(aHandle, aSigner, iStatus);
312 void CUnifiedKeyStore::Open(const TCTTokenObjectHandle& aHandle,
313 MDSASigner*& aSigner,
314 TRequestStatus& aStatus)
316 if (DoOpen(aHandle, aStatus))
318 iKeyStore->Open(aHandle, aSigner, iStatus);
322 void CUnifiedKeyStore::Open(const TCTTokenObjectHandle& aHandle,
323 MCTDecryptor*& aDecryptor,
324 TRequestStatus& aStatus)
326 if (DoOpen(aHandle, aStatus))
328 iKeyStore->Open(aHandle, aDecryptor, iStatus);
332 void CUnifiedKeyStore::Open(const TCTTokenObjectHandle& aHandle,
333 MCTDH*& aDH, TRequestStatus& aStatus)
335 if (DoOpen(aHandle, aStatus))
337 iKeyStore->Open(aHandle, aDH, iStatus);
341 void CUnifiedKeyStore::CancelOpen()
349 /** Returns the public key in DER-encoded ASN-1 */
350 void CUnifiedKeyStore::ExportPublic(const TCTTokenObjectHandle& aHandle,
352 TRequestStatus& aStatus)
354 StartAsyncOperation(EExportPublic, aStatus);
356 iKeyStore = FindKeyStore(aHandle);
359 Complete(KErrNotFound);
363 iKeyStore->ExportPublic(aHandle, aPublicKey, iStatus);
367 void CUnifiedKeyStore::CancelExportPublic()
369 if (iState == EExportPublic)
375 #ifdef SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT
376 void CUnifiedKeyStore::Open(const TCTTokenObjectHandle& aHandle,
377 CryptoSpi::CSigner*& aSigner,
378 TRequestStatus& aStatus)
380 if (DoOpen(aHandle, aStatus))
382 iKeyStore->Open(aHandle, aSigner, iStatus);
386 void CUnifiedKeyStore::Open(const TCTTokenObjectHandle& aHandle,
387 CryptoSpi:: CAsymmetricCipher*& asymmetricCipherObj,
388 TRequestStatus& aStatus)
390 if (DoOpen(aHandle, aStatus))
392 iKeyStore->Open(aHandle, asymmetricCipherObj, iStatus);
396 void CUnifiedKeyStore::Decrypt(const TCTTokenObjectHandle& aHandle,
397 const TDesC8& aCiphertext,
398 HBufC8*& aPlaintextPtr,
399 TRequestStatus& aStatus)
401 if (DoOpen(aHandle, aStatus))
403 iKeyStore->Decrypt(aHandle, aCiphertext, aPlaintextPtr, iStatus);
407 void CUnifiedKeyStore::Sign(const TCTTokenObjectHandle& aHandle,
408 const TDesC8& aPlaintext,
409 CryptoSpi::CCryptoParams*& aSignature,
410 TRequestStatus& aStatus)
412 if (DoOpen(aHandle, aStatus))
414 iKeyStore->Sign(aHandle, aPlaintext, aSignature, iStatus);
417 #endif //SYMBIAN_ENABLE_SDP_WMDRM_SUPPORT
419 // ************************************************************************
421 // ************************************************************************
423 EXPORT_C void CUnifiedKeyStore::ExportKey(TCTTokenObjectHandle aHandle,
424 HBufC8*& aKey, TRequestStatus& aStatus)
426 StartAsyncOperation(EExportKey, aStatus);
428 ASSERT(!iKeyStoreManager);
429 iKeyStoreManager = FindKeyStoreManager(aHandle);
430 if (!iKeyStoreManager)
432 Complete(KErrNotFound);
436 iKeyStoreManager->ExportKey(aHandle, aKey, iStatus);
440 EXPORT_C void CUnifiedKeyStore::CancelExportKey()
442 if (iState == EExportKey)
448 EXPORT_C void CUnifiedKeyStore::ExportEncryptedKey(TCTTokenObjectHandle aHandle,
449 const CPBEncryptParms& aEncryptParams,
450 HBufC8*& aKey, TRequestStatus& aStatus)
452 StartAsyncOperation(EExportEncryptedKey, aStatus);
454 ASSERT(!iKeyStoreManager);
455 iKeyStoreManager = FindKeyStoreManager(aHandle);
456 if (!iKeyStoreManager)
458 Complete(KErrNotFound);
462 iKeyStoreManager->ExportEncryptedKey(aHandle, aEncryptParams, aKey, iStatus);
466 EXPORT_C void CUnifiedKeyStore::CancelExportEncryptedKey()
468 if (iState == EExportEncryptedKey)
474 EXPORT_C void CUnifiedKeyStore::DeleteKey(TCTTokenObjectHandle aHandle,
475 TRequestStatus& aStatus)
477 StartAsyncOperation(EDeleteKey, aStatus);
479 iKeyStoreManager = FindKeyStoreManager(aHandle);
480 if (!iKeyStoreManager)
482 Complete(KErrNotFound);
486 iKeyStoreManager->DeleteKey(aHandle, iStatus);
490 EXPORT_C void CUnifiedKeyStore::CancelDeleteKey()
492 if (iState == EDeleteKey)
498 EXPORT_C void CUnifiedKeyStore::SetUsePolicy(TCTTokenObjectHandle aHandle,
499 const TSecurityPolicy& aPolicy,
500 TRequestStatus& aStatus)
502 StartAsyncOperation(ESetUsePolicy, aStatus);
504 iKeyStoreManager = FindKeyStoreManager(aHandle);
505 if (!iKeyStoreManager)
507 Complete(KErrNotFound);
511 iKeyStoreManager->SetUsePolicy(aHandle, aPolicy, iStatus);
515 EXPORT_C void CUnifiedKeyStore::CancelSetUsePolicy()
517 if (iState == ESetUsePolicy)
523 EXPORT_C void CUnifiedKeyStore::SetManagementPolicy(TCTTokenObjectHandle aHandle,
524 const TSecurityPolicy& aPolicy,
525 TRequestStatus& aStatus)
527 StartAsyncOperation(ESetManagementPolicy, aStatus);
529 iKeyStoreManager = FindKeyStoreManager(aHandle);
530 if (!iKeyStoreManager)
532 Complete(KErrNotFound);
536 iKeyStoreManager->SetManagementPolicy(aHandle, aPolicy, iStatus);
540 EXPORT_C void CUnifiedKeyStore::CancelSetManagementPolicy()
542 if (iState == ESetManagementPolicy)
548 EXPORT_C void CUnifiedKeyStore::SetPassphraseTimeout(TInt aTimeout,
549 TRequestStatus& aStatus)
551 StartAsyncOperation(ESetPassphraseTimeout, aStatus);
554 iNewTimeout = aTimeout;
557 TRequestStatus* status = &iStatus;
558 User::RequestComplete(status, KErrNone);
561 EXPORT_C void CUnifiedKeyStore::CancelSetPassphraseTimeout()
563 if (iState == ESetPassphraseTimeout)
569 EXPORT_C void CUnifiedKeyStore::Relock(TRequestStatus& aStatus)
571 StartAsyncOperation(ERelock, aStatus);
576 TRequestStatus* status = &iStatus;
577 User::RequestComplete(status, KErrNone);
580 EXPORT_C void CUnifiedKeyStore::CancelRelock()
582 if (iState == ERelock)
588 // ************************************************************************
590 // ************************************************************************
592 EXPORT_C TInt CUnifiedKeyStore::KeyStoreCount() const
594 return (iKeyStoresHolder.Count());
597 EXPORT_C MCTKeyStore& CUnifiedKeyStore::KeyStore(TInt aIndex)
599 __ASSERT_ALWAYS(aIndex >= 0 && aIndex < iKeyStoresHolder.Count(),
600 User::Panic(KUnifiedKeyStore, EArrayAccessOutOfBounds));
602 MCTKeyStore* keyStore = static_cast<MCTKeyStore*>(iKeyStoresHolder[aIndex]->KeyStore());
606 EXPORT_C TInt CUnifiedKeyStore::KeyStoreManagerCount() const
609 for (TInt i = 0 ; i < iKeyStoresHolder.Count() ; ++i)
611 if (iKeyStoresHolder[i]->IsKeyManager())
619 EXPORT_C MCTKeyStoreManager& CUnifiedKeyStore::KeyStoreManager(TInt aIndex)
621 __ASSERT_ALWAYS(aIndex >= 0, User::Panic(KUnifiedKeyStore, EArrayAccessOutOfBounds));
622 TInt managerIndex = 0;
623 MCTKeyStoreManager* result = NULL;
624 for (TInt i = 0 ; i < iKeyStoresHolder.Count() ; ++i)
626 if (iKeyStoresHolder[i]->IsKeyManager())
628 if (managerIndex == aIndex)
630 result = static_cast<MCTKeyStoreManager*>(iKeyStoresHolder[i]->KeyStore());
636 __ASSERT_ALWAYS(result != NULL, User::Panic(KUnifiedKeyStore, EArrayAccessOutOfBounds));
640 #ifdef SYMBIAN_AUTH_SERVER
642 EXPORT_C void CUnifiedKeyStore::CreateKey( TInt aKeyStoreIndex, TKeyUsagePKCS15 aUsage,TUint aSize,
643 const TDesC& aLabel, CCTKeyInfo::EKeyAlgorithm aAlgorithm,
644 TInt aAccessType, TTime aStartDate, TTime aEndDate,
645 const TDesC& aAuthenticationString, TInt aFreshness,
646 CCTKeyInfo*& aKeyInfoOut, TRequestStatus& aStatus)
649 StartAsyncOperation(ECreateKey, aStatus);
650 TRAPD(err, PrepareToCreateKeyL(aKeyStoreIndex, aUsage, aSize, aLabel, aAlgorithm, aAccessType,
651 aStartDate, aEndDate, aStatus));
658 iKeyInfoOut = &aKeyInfoOut;
660 iKeyStoreManager->CreateKey(aAuthenticationString, aFreshness, iKeyInfo, iStatus);
666 EXPORT_C void CUnifiedKeyStore::ImportKey( TInt aKeyStoreIndex, const TDesC8& aKeyData,
667 TKeyUsagePKCS15 aUsage, const TDesC& aLabel,
668 TInt aAccessType, TTime aStartDate, TTime aEndDate,
669 const TDesC& aAuthenticationString, TInt aFreshness,
670 CCTKeyInfo*& aKeyInfoOut, TRequestStatus& aStatus)
672 TBool isEncrypted = TASN1DecPKCS8::IsEncryptedPKCS8Data(aKeyData);
673 StartAsyncOperation(isEncrypted ? EImportKeyEncrypted : EImportKey, aStatus);
676 iKeyData = aKeyData.Alloc();
677 if (!iKeyData) // OOM or some other catastrophe
679 Complete(KErrNoMemory);
683 TRAPD(err, PrepareToCreateKeyL(aKeyStoreIndex, aUsage, 0, aLabel, CCTKeyInfo::EInvalidAlgorithm, aAccessType,
684 aStartDate, aEndDate, aStatus));
691 iKeyInfoOut = &aKeyInfoOut;
696 iKeyStoreManager->ImportEncryptedKey(*iKeyData, aAuthenticationString, aFreshness, iKeyInfo, iStatus);
700 iKeyStoreManager->ImportKey(*iKeyData, aAuthenticationString, aFreshness, iKeyInfo, iStatus);
705 EXPORT_C void CUnifiedKeyStore::SetAuthenticationPolicy( const TCTTokenObjectHandle aHandle,
706 const TDesC& aAuthenticationString,
708 TRequestStatus& aStatus)
710 StartAsyncOperation(ESetAuthenticationPolicy, aStatus);
712 ASSERT(!iKeyStoreManager);
713 iKeyStoreManager = FindKeyStoreManager(aHandle);
714 if (!iKeyStoreManager)
716 Complete(KErrNotFound);
720 iKeyStoreManager->SetAuthenticationPolicy(aHandle, aAuthenticationString, aFreshness, iStatus);
725 EXPORT_C void CUnifiedKeyStore::GetAuthenticationPolicy( const TCTTokenObjectHandle aHandle,
726 HBufC*& aAuthenticationString,
728 TRequestStatus& aStatus)
730 StartAsyncOperation(EGetAuthenticationPolicy, aStatus);
732 ASSERT(!iKeyStoreManager);
733 iKeyStoreManager = FindKeyStoreManager(aHandle);
734 if (!iKeyStoreManager)
736 Complete(KErrNotFound);
740 iKeyStoreManager->GetAuthenticationPolicy(aHandle, aAuthenticationString, aFreshness, iStatus);
745 #endif // SYMBIAN_AUTH_SERVER
747 CUnifiedKeyStore::CUnifiedKeyStore(RFs& aFs)
748 : CActive(EPriorityNormal), iFs(aFs), iState(EIdle)
749 {// Currently defaults to always try for key store manager interface
750 // This may change (add parameter to NewL for required interface)
751 iRequestUid.iUid = KInterfaceKeyStoreManager;
752 CActiveScheduler::Add(this);
755 void CUnifiedKeyStore::ConstructL()
758 void CUnifiedKeyStore::StartAsyncOperation(TState aState, TRequestStatus& aStatus)
760 ASSERT(iState == EIdle);
761 ASSERT(iOriginalRequestStatus == NULL);
763 iOriginalRequestStatus = &aStatus;
764 aStatus = KRequestPending;
767 MCTKeyStore* CUnifiedKeyStore::FindKeyStore(const TCTTokenObjectHandle& aHandle)
769 for (TInt index = 0 ; index < iKeyStoresHolder.Count() ; ++index)
771 MCTTokenInterface* store = iKeyStoresHolder[index]->KeyStore();
773 if (store->Token().Handle() == aHandle.iTokenHandle)
775 return static_cast<MCTKeyStoreManager*>(store);
781 MCTKeyStoreManager* CUnifiedKeyStore::FindKeyStoreManager(const TCTTokenObjectHandle& aHandle)
783 for (TInt index = 0 ; index < iKeyStoresHolder.Count() ; ++index)
785 MCTTokenInterface* store = iKeyStoresHolder[index]->KeyStore();
787 if (store->Token().Handle() == aHandle.iTokenHandle && iKeyStoresHolder[index]->IsKeyManager())
789 return static_cast<MCTKeyStoreManager*>(store);
795 void CUnifiedKeyStore::RunL()
797 if (EInitializeGetKeyUserInterfaceFinished != iState &&
798 EInitializeGetKeyUserInterface != iState &&
799 EInitializeGetToken != iState)
801 User::LeaveIfError(iStatus.Int());
806 case EInitializeGetTokenList:
807 {// Try to get a list of Tokens for each of the Token Types
809 if (iIndexTokenTypes < iTokenTypes.Count())
811 __ASSERT_DEBUG(!iTokenType, User::Panic(KUnifiedKeyStore, EArrayAccessOutOfBounds));
812 iTokenType = MCTTokenType::NewL(*iTokenTypes[iIndexTokenTypes], iFs);
813 __ASSERT_DEBUG(iTokens.Count()==0, User::Panic(KUnifiedKeyStore, ETokensArrayAlreadyInUse));
814 iTokenType->List(iTokens, iStatus);
816 iState = EInitializeGetToken;
819 {// We don't need the list of Token Types anymore
820 iTokenTypes.ResetAndDestroy();
822 iState = EInitializeFinished;
823 TRequestStatus* status = &iStatus;
824 User::RequestComplete(status, KErrNone);
829 case EInitializeGetToken:
831 if (iStatus.Int() == KErrHardwareNotAvailable)
833 // If the hardware corresponding to this
834 // TokenType has been removed then just skip it
837 iState = EInitializeGetToken;
838 TRequestStatus* status = &iStatus;
839 User::RequestComplete(status, KErrNone);
843 User::LeaveIfError(iStatus.Int());
846 if (iIndexTokens < iTokens.Count())
848 iTokenType->OpenToken(*iTokens[iIndexTokens], iToken, iStatus);
849 iRequestUid.iUid = KInterfaceKeyStoreManager;
850 iState = EInitialiseGetKeyManagerInterface;
853 {// Don't need the iTokenType anymore
854 iTokenType->Release();
857 iTokens.Close(); // Don't need the list of Tokens anymore
858 iState = EInitializeGetTokenList;
859 TRequestStatus* status = &iStatus;
860 User::RequestComplete(status, KErrNone);
866 case EInitialiseGetKeyManagerInterface:
867 {// First try to get a manager interface to the store, if
868 // unsuccessful, try once to get a user interface
871 iRequestUid.iUid = KInterfaceKeyStoreManager;
872 iToken->GetInterface(iRequestUid, iTokenInterface, iStatus);
873 iState = EInitializeGetKeyUserInterface;
878 User::Leave(KErrNotReady);
882 case EInitializeGetKeyUserInterface:
883 {// Did we get a manager interface?
884 if (iStatus==KErrNoMemory)
886 User::Leave(KErrNoMemory);
889 if (iRequestUid.iUid==KInterfaceKeyStoreManager)
891 if (KErrNone==iStatus.Int())
892 {// Success! Store it and finish up
893 CKeyStoreIF* keyStore = new (ELeave) CKeyStoreIF(iTokenInterface, ETrue);
894 CleanupStack::PushL(keyStore);
895 User::LeaveIfError(iKeyStoresHolder.Append(keyStore));
896 CleanupStack::Pop(keyStore);
901 iState = EInitializeGetToken;
902 TRequestStatus* status = &iStatus;
903 User::RequestComplete(status, KErrNone);
906 {// No luck getting a manager, so try getting a user
907 iRequestUid.iUid = KInterfaceKeyStore;
908 iToken->GetInterface(iRequestUid, iTokenInterface, iStatus);
909 iState = EInitializeGetKeyUserInterfaceFinished;
912 else if (iRequestUid.iUid==KInterfaceKeyStore)
913 {// We were trying for user IF
914 if (iStatus==KErrNone)
918 iRequestUid.iUid = KInterfaceKeyStore;
919 iToken->GetInterface(iRequestUid, iTokenInterface, iStatus);
920 iState = EInitializeGetKeyUserInterfaceFinished;
924 User::Leave(KErrNotReady);
928 {// Couldn't even get a user IF
929 User::Leave(iStatus.Int());
936 case EInitializeGetKeyUserInterfaceFinished:
938 if (iStatus==KErrNone)
940 CKeyStoreIF* keyStore = new (ELeave) CKeyStoreIF(iTokenInterface, EFalse);
941 CleanupStack::PushL(keyStore);
942 User::LeaveIfError(iKeyStoresHolder.Append(keyStore));
943 CleanupStack::Pop(keyStore);
948 iState = EInitializeGetToken;
949 TRequestStatus* status = &iStatus;
950 User::RequestComplete(status, KErrNone);
952 else if (iStatus == KErrNoMemory)
954 User::Leave(KErrNoMemory);
958 iState = EInitializeGetToken;
959 TRequestStatus* status = &iStatus;
960 User::RequestComplete(status,iStatus.Int());
966 case EInitializeFinished:
971 {// iIndex has been initialized in List function
973 if (iIndex < iKeyStoresHolder.Count())
975 iKeyStore = static_cast<MCTKeyStore*>(iKeyStoresHolder[iIndex]->KeyStore());
977 iKeyStore->List(*iKeyInfos, *iFilter, iStatus);
992 *iKeyInfoOut = iKeyInfo;
993 iKeyInfo = NULL; // Release ownership
998 case EImportKeyEncrypted:
999 *iKeyInfoOut = iKeyInfo;
1000 iKeyInfo = NULL; // Release ownership
1005 case EExportEncryptedKey:
1012 // Find next key store manager
1013 while (iIndex < iKeyStoresHolder.Count() && !iKeyStoresHolder[iIndex]->IsKeyManager())
1016 if (iIndex < iKeyStoresHolder.Count())
1018 iKeyStoreManager = static_cast<MCTKeyStoreManager*>(iKeyStoresHolder[iIndex]->KeyStore());
1019 ASSERT(iKeyStoreManager);
1020 iKeyStoreManager->Relock(iStatus);
1029 case ESetPassphraseTimeout:
1032 // Find next key store manager
1033 while (iIndex < iKeyStoresHolder.Count() && !iKeyStoresHolder[iIndex]->IsKeyManager())
1036 if (iIndex < iKeyStoresHolder.Count())
1038 iKeyStoreManager = static_cast<MCTKeyStoreManager*>(iKeyStoresHolder[iIndex]->KeyStore());
1039 ASSERT(iKeyStoreManager);
1040 iKeyStoreManager->SetPassphraseTimeout(iNewTimeout, iStatus);
1053 case ESetManagementPolicy:
1054 case EGetAuthenticationPolicy:
1055 case ESetAuthenticationPolicy:
1059 User::Panic(KUnifiedKeyStore, EUnrecognisedState);
1064 TInt CUnifiedKeyStore::RunError(TInt aError)
1070 void CUnifiedKeyStore::DoCancel()
1072 // If the current state is the last state involved in handling a request, we
1073 // check to see if we have already been completed - in this case we can
1074 // simply complete the client with iStatus (this may be KErrNone). If we
1075 // have not we cancel the outstanding request and pass the resulting iStatus
1076 // back to the client - this too may indicate a successful completion if the
1077 // cancel arrived after the request was executed.
1079 // For more complex cases, where there are more states to go through before
1080 // we finish servicing the client request, we cancel any outstanding
1081 // request, and return KErrCancel to the client.
1085 case EInitializeFinished:
1089 case EImportKeyEncrypted:
1091 case EExportEncryptedKey:
1096 case ESetManagementPolicy:
1097 if (iStatus == KRequestPending)
1099 // Attempt to cancel outstanding request and pass status back to
1101 CancelOutstandingRequest();
1102 Complete(iStatus.Int());
1106 // We've already been completed - call RunL() to process results
1107 // and complete client
1109 if (err != KErrNone)
1117 CancelOutstandingRequest();
1118 Complete(KErrCancel);
1123 void CUnifiedKeyStore::CancelOutstandingRequest()
1127 case EInitializeGetTokenList:
1128 case EInitializeGetToken:
1129 case EInitialiseGetKeyManagerInterface:
1130 case EInitializeGetKeyUserInterface:
1131 case EInitializeGetKeyUserInterfaceFinished:
1132 case EInitializeFinished:
1133 // Don't have to cancel initialisation stuff - this happens when we
1134 // release the objects in Cleanup().
1135 iStatus = KErrCancel;
1141 iKeyStore->CancelList();
1147 iKeyStore->CancelGetKeyInfo();
1152 iKeyStore->CancelOpen();
1157 iKeyStore->CancelExportPublic();
1161 ASSERT(iKeyStoreManager);
1162 iKeyStoreManager->CancelCreateKey();
1166 case EImportKeyEncrypted:
1167 ASSERT(iKeyStoreManager);
1168 iKeyStoreManager->CancelImportKey();
1172 case EExportEncryptedKey:
1173 ASSERT(iKeyStoreManager);
1174 iKeyStoreManager->CancelExportKey();
1178 ASSERT(iKeyStoreManager);
1179 iKeyStoreManager->CancelDeleteKey();
1183 ASSERT(iKeyStoreManager);
1184 iKeyStoreManager->CancelRelock();
1187 case ESetPassphraseTimeout:
1188 ASSERT(iKeyStoreManager);
1189 iKeyStoreManager->CancelSetPassphraseTimeout();
1193 ASSERT(iKeyStoreManager);
1194 iKeyStoreManager->CancelSetUsePolicy();
1197 case ESetManagementPolicy:
1198 ASSERT(iKeyStoreManager);
1199 iKeyStoreManager->CancelSetManagementPolicy();
1203 User::Panic(KUnifiedKeyStore, EUnrecognisedState);
1209 void CUnifiedKeyStore::Complete(TInt aError)
1212 if (iOriginalRequestStatus)
1214 User::RequestComplete(iOriginalRequestStatus, aError);
1218 void CUnifiedKeyStore::Cleanup()
1220 // If we have a key info, we want to release it
1223 iKeyInfo->Release();
1236 iTokenTypes.Close();
1240 iTokenType->Release();
1252 if (iTokenInterface)
1254 iTokenInterface->Release();
1255 iTokenInterface = 0;
1260 iKeyStoreManager = NULL;
1265 CUnifiedKeyStore::CKeyStoreIF::CKeyStoreIF(MCTTokenInterface* aKeyStore, TBool aIsKeyManager)
1266 : iKeyStore(aKeyStore), iIsKeyManager(aIsKeyManager)
1269 CUnifiedKeyStore::CKeyStoreIF::~CKeyStoreIF()
1273 iKeyStore->Release();