|
1 /* |
|
2 * Copyright (c) 2001-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
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". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "unifiedkeystore.h" |
|
20 #include <ecom.h> |
|
21 #include <random.h> |
|
22 #include <pbedata.h> |
|
23 #include <asnpkcs.h> |
|
24 |
|
25 _LIT(KUnifiedKeyStore, "UnifiedKeyStore"); |
|
26 |
|
27 ///////////////////////////////////////////////////////////////////////////////////// |
|
28 //CUnifiedKeyStore |
|
29 ///////////////////////////////////////////////////////////////////////////////////// |
|
30 |
|
31 EXPORT_C CUnifiedKeyStore* CUnifiedKeyStore::NewL(RFs& aFs) |
|
32 { |
|
33 CUnifiedKeyStore* self = CUnifiedKeyStore::NewLC(aFs); |
|
34 CleanupStack::Pop(self); |
|
35 return self; |
|
36 } |
|
37 |
|
38 EXPORT_C CUnifiedKeyStore* CUnifiedKeyStore::NewLC(RFs& aFs) |
|
39 { |
|
40 CUnifiedKeyStore* self = new(ELeave) CUnifiedKeyStore(aFs); |
|
41 CleanupStack::PushL(self); |
|
42 self->ConstructL(); |
|
43 return self; |
|
44 } |
|
45 |
|
46 EXPORT_C CUnifiedKeyStore::~CUnifiedKeyStore() |
|
47 { |
|
48 Cancel(); |
|
49 Cleanup(); |
|
50 |
|
51 iKeyStoresHolder.ResetAndDestroy(); |
|
52 iKeyStoresHolder.Close(); |
|
53 |
|
54 REComSession::FinalClose(); |
|
55 } |
|
56 |
|
57 void CUnifiedKeyStore::DoInitializeL() |
|
58 {// We want the list of all token types that support a keystore interface |
|
59 RArray<TUid> uidArray; |
|
60 CleanupClosePushL(uidArray); |
|
61 |
|
62 User::LeaveIfError(uidArray.Append(TUid::Uid(KInterfaceKeyStore))); |
|
63 |
|
64 TCTFindTokenTypesByInterface filter(uidArray.Array()); |
|
65 CCTTokenTypeInfo::ListL(iTokenTypes, filter); |
|
66 |
|
67 CleanupStack::PopAndDestroy(); // uidArray |
|
68 } |
|
69 |
|
70 EXPORT_C void CUnifiedKeyStore::Initialize(TRequestStatus& aStatus) |
|
71 {// The following assertion checks that we didn't call Initialize twice |
|
72 __ASSERT_DEBUG((iKeyStoresHolder.Count()==0), User::Panic(KUnifiedKeyStore, EUnexpectedInitialise)); |
|
73 |
|
74 TRAPD(err, DoInitializeL()); |
|
75 if (err != KErrNone) |
|
76 { |
|
77 TRequestStatus* status = &aStatus; |
|
78 User::RequestComplete(status, err); |
|
79 return; |
|
80 } |
|
81 |
|
82 iIndexTokenTypes = -1; |
|
83 StartAsyncOperation(EInitializeGetTokenList, aStatus); |
|
84 |
|
85 SetActive(); |
|
86 TRequestStatus* status = &iStatus; |
|
87 User::RequestComplete(status, KErrNone); |
|
88 } |
|
89 |
|
90 EXPORT_C void CUnifiedKeyStore::CancelInitialize() |
|
91 { |
|
92 if (iState == EInitializeGetTokenList || |
|
93 iState == EInitializeGetToken || |
|
94 iState == EInitialiseGetKeyManagerInterface || |
|
95 iState == EInitializeGetKeyUserInterface || |
|
96 iState == EInitializeGetKeyUserInterfaceFinished || |
|
97 iState == EInitializeFinished) |
|
98 { |
|
99 Cancel(); |
|
100 } |
|
101 } |
|
102 |
|
103 EXPORT_C void CUnifiedKeyStore::CreateKey(TInt aKeyStoreIndex, TKeyUsagePKCS15 aUsage,TUint aSize, |
|
104 const TDesC& aLabel, CCTKeyInfo::EKeyAlgorithm aAlgorithm, |
|
105 TInt aAccessType, TTime aStartDate, TTime aEndDate, |
|
106 CCTKeyInfo*& aKeyInfoOut, TRequestStatus& aStatus) |
|
107 { |
|
108 StartAsyncOperation(ECreateKey, aStatus); |
|
109 TRAPD(err, PrepareToCreateKeyL(aKeyStoreIndex, aUsage, aSize, aLabel, aAlgorithm, aAccessType, |
|
110 aStartDate, aEndDate, aStatus)); |
|
111 if (KErrNone != err) |
|
112 { |
|
113 Complete(err); |
|
114 return; |
|
115 } |
|
116 |
|
117 iKeyInfoOut = &aKeyInfoOut; |
|
118 aKeyInfoOut = NULL; |
|
119 iKeyStoreManager->CreateKey(iKeyInfo, iStatus); |
|
120 SetActive(); |
|
121 } |
|
122 |
|
123 EXPORT_C void CUnifiedKeyStore::CancelCreateKey() |
|
124 { |
|
125 if (iState == ECreateKey) |
|
126 { |
|
127 Cancel(); |
|
128 } |
|
129 } |
|
130 |
|
131 EXPORT_C void CUnifiedKeyStore::ImportKey(TInt aKeyStoreIndex, const TDesC8& aKeyData, |
|
132 TKeyUsagePKCS15 aUsage, const TDesC& aLabel, |
|
133 TInt aAccessType, TTime aStartDate, TTime aEndDate, |
|
134 CCTKeyInfo*& aKeyInfoOut, TRequestStatus& aStatus) |
|
135 { |
|
136 TBool isEncrypted = TASN1DecPKCS8::IsEncryptedPKCS8Data(aKeyData); |
|
137 StartAsyncOperation(isEncrypted ? EImportKeyEncrypted : EImportKey, aStatus); |
|
138 |
|
139 ASSERT(!iKeyData); |
|
140 iKeyData = aKeyData.Alloc(); |
|
141 if (!iKeyData) // OOM or some other catastrophe |
|
142 { |
|
143 Complete(KErrNoMemory); |
|
144 return; |
|
145 } |
|
146 |
|
147 TRAPD(err, PrepareToCreateKeyL(aKeyStoreIndex, aUsage, 0, aLabel, CCTKeyInfo::EInvalidAlgorithm, aAccessType, |
|
148 aStartDate, aEndDate, aStatus)); |
|
149 if (KErrNone != err) |
|
150 { |
|
151 Complete(err); |
|
152 return; |
|
153 } |
|
154 |
|
155 iKeyInfoOut = &aKeyInfoOut; |
|
156 aKeyInfoOut = NULL; |
|
157 |
|
158 if (isEncrypted) |
|
159 { |
|
160 iKeyStoreManager->ImportEncryptedKey(*iKeyData, iKeyInfo, iStatus); |
|
161 } |
|
162 else |
|
163 { |
|
164 iKeyStoreManager->ImportKey(*iKeyData, iKeyInfo, iStatus); |
|
165 } |
|
166 SetActive(); |
|
167 } |
|
168 |
|
169 EXPORT_C void CUnifiedKeyStore::CancelImportKey() |
|
170 { |
|
171 if (iState == EImportKey || |
|
172 iState == EImportKeyEncrypted) |
|
173 { |
|
174 Cancel(); |
|
175 } |
|
176 } |
|
177 |
|
178 void CUnifiedKeyStore::PrepareToCreateKeyL( TInt aKeyStoreIndex, |
|
179 TKeyUsagePKCS15 aUsage, TUint aSize, |
|
180 const TDesC& aLabel, |
|
181 CCTKeyInfo::EKeyAlgorithm aAlgorithm, |
|
182 TInt aAccessType, |
|
183 TTime aStartTime, TTime aEndTime, |
|
184 TRequestStatus& aStatus) |
|
185 { |
|
186 ASSERT(!iKeyStoreManager); |
|
187 |
|
188 // These values are filled in by the server when the key is created |
|
189 TKeyIdentifier keyID; |
|
190 keyID.MaxSize(); |
|
191 keyID.FillZ(keyID.MaxSize()); |
|
192 TInt keyHandle = 0; |
|
193 |
|
194 // Get the secure ID of the current process |
|
195 RProcess thisProcess; |
|
196 User::LeaveIfError(thisProcess.Open(thisProcess.Id())); |
|
197 TSecureId creatorId = thisProcess.SecureId(); |
|
198 thisProcess.Close(); |
|
199 |
|
200 // Default management policy: resict to creating process |
|
201 TSecurityPolicy managementPolicy(creatorId); |
|
202 |
|
203 // Default use policy: also resict to creating process |
|
204 TSecurityPolicy usePolicy(creatorId); |
|
205 |
|
206 HBufC* label = aLabel.AllocLC(); |
|
207 |
|
208 // Panics if keystore manager index invalid |
|
209 MCTKeyStoreManager& keystore = KeyStoreManager(aKeyStoreIndex); |
|
210 |
|
211 iKeyInfo = CCTKeyInfo::NewL(keyID, aUsage, aSize, NULL, label, keystore.Token(), |
|
212 keyHandle, usePolicy, managementPolicy, aAlgorithm, |
|
213 aAccessType, ETrue, aStartTime, aEndTime); |
|
214 CleanupStack::Pop(label); |
|
215 |
|
216 iKeyStoreManager = &keystore; |
|
217 iOriginalRequestStatus = &aStatus; |
|
218 aStatus = KRequestPending; |
|
219 } |
|
220 |
|
221 // ************************************************************************ |
|
222 // MKeyStore |
|
223 // ************************************************************************ |
|
224 |
|
225 void CUnifiedKeyStore::List(RMPointerArray<CCTKeyInfo>& aKeys, |
|
226 const TCTKeyAttributeFilter& aFilter, |
|
227 TRequestStatus& aStatus) |
|
228 { |
|
229 StartAsyncOperation(EList, aStatus); |
|
230 |
|
231 iKeyInfos = &aKeys; |
|
232 |
|
233 delete iFilter; |
|
234 iFilter = new TCTKeyAttributeFilter(aFilter); |
|
235 if (!iFilter) |
|
236 { |
|
237 Complete(KErrNoMemory); |
|
238 return; |
|
239 } |
|
240 |
|
241 iIndex = -1; |
|
242 |
|
243 SetActive(); |
|
244 TRequestStatus* status = &iStatus; |
|
245 User::RequestComplete(status, KErrNone); |
|
246 } |
|
247 |
|
248 void CUnifiedKeyStore::CancelList() |
|
249 { |
|
250 if (iState == EList) |
|
251 { |
|
252 Cancel(); |
|
253 } |
|
254 } |
|
255 |
|
256 void CUnifiedKeyStore::GetKeyInfo(TCTTokenObjectHandle aHandle, |
|
257 CCTKeyInfo*& aKeyInfo, |
|
258 TRequestStatus& aStatus) |
|
259 { |
|
260 StartAsyncOperation(EGetKeyInfo, aStatus); |
|
261 |
|
262 ASSERT(!iKeyStore); |
|
263 iKeyStore = FindKeyStore(aHandle); |
|
264 if (!iKeyStore) |
|
265 { |
|
266 Complete(KErrNotFound); |
|
267 return; |
|
268 } |
|
269 |
|
270 iKeyStore->GetKeyInfo(aHandle, aKeyInfo, iStatus); |
|
271 SetActive(); |
|
272 } |
|
273 |
|
274 void CUnifiedKeyStore::CancelGetKeyInfo() |
|
275 { |
|
276 if (iState == EGetKeyInfo) |
|
277 { |
|
278 Cancel(); |
|
279 } |
|
280 } |
|
281 |
|
282 // Implementation for most of the Open() method |
|
283 TBool CUnifiedKeyStore::DoOpen(const TCTTokenObjectHandle& aHandle, |
|
284 TRequestStatus& aStatus) |
|
285 { |
|
286 StartAsyncOperation(EOpen, aStatus); |
|
287 |
|
288 ASSERT(!iKeyStore); |
|
289 iKeyStore = FindKeyStore(aHandle); |
|
290 if (!iKeyStore) |
|
291 { |
|
292 Complete(KErrNotFound); |
|
293 return EFalse; |
|
294 } |
|
295 |
|
296 SetActive(); |
|
297 return ETrue; |
|
298 } |
|
299 |
|
300 void CUnifiedKeyStore::Open(const TCTTokenObjectHandle& aHandle, |
|
301 MRSASigner*& aSigner, |
|
302 TRequestStatus& aStatus) |
|
303 { |
|
304 if (DoOpen(aHandle, aStatus)) |
|
305 { |
|
306 iKeyStore->Open(aHandle, aSigner, iStatus); |
|
307 } |
|
308 } |
|
309 |
|
310 void CUnifiedKeyStore::Open(const TCTTokenObjectHandle& aHandle, |
|
311 MDSASigner*& aSigner, |
|
312 TRequestStatus& aStatus) |
|
313 { |
|
314 if (DoOpen(aHandle, aStatus)) |
|
315 { |
|
316 iKeyStore->Open(aHandle, aSigner, iStatus); |
|
317 } |
|
318 } |
|
319 |
|
320 void CUnifiedKeyStore::Open(const TCTTokenObjectHandle& aHandle, |
|
321 MCTDecryptor*& aDecryptor, |
|
322 TRequestStatus& aStatus) |
|
323 { |
|
324 if (DoOpen(aHandle, aStatus)) |
|
325 { |
|
326 iKeyStore->Open(aHandle, aDecryptor, iStatus); |
|
327 } |
|
328 } |
|
329 |
|
330 void CUnifiedKeyStore::Open(const TCTTokenObjectHandle& aHandle, |
|
331 MCTDH*& aDH, TRequestStatus& aStatus) |
|
332 { |
|
333 if (DoOpen(aHandle, aStatus)) |
|
334 { |
|
335 iKeyStore->Open(aHandle, aDH, iStatus); |
|
336 } |
|
337 } |
|
338 |
|
339 void CUnifiedKeyStore::CancelOpen() |
|
340 { |
|
341 if (iState == EOpen) |
|
342 { |
|
343 Cancel(); |
|
344 } |
|
345 } |
|
346 |
|
347 /** Returns the public key in DER-encoded ASN-1 */ |
|
348 void CUnifiedKeyStore::ExportPublic(const TCTTokenObjectHandle& aHandle, |
|
349 HBufC8*& aPublicKey, |
|
350 TRequestStatus& aStatus) |
|
351 { |
|
352 StartAsyncOperation(EExportPublic, aStatus); |
|
353 |
|
354 iKeyStore = FindKeyStore(aHandle); |
|
355 if (!iKeyStore) |
|
356 { |
|
357 Complete(KErrNotFound); |
|
358 return; |
|
359 } |
|
360 |
|
361 iKeyStore->ExportPublic(aHandle, aPublicKey, iStatus); |
|
362 SetActive(); |
|
363 } |
|
364 |
|
365 void CUnifiedKeyStore::CancelExportPublic() |
|
366 { |
|
367 if (iState == EExportPublic) |
|
368 { |
|
369 Cancel(); |
|
370 } |
|
371 } |
|
372 |
|
373 // ************************************************************************ |
|
374 // MKeyStoreManager |
|
375 // ************************************************************************ |
|
376 |
|
377 EXPORT_C void CUnifiedKeyStore::ExportKey(TCTTokenObjectHandle aHandle, |
|
378 HBufC8*& aKey, TRequestStatus& aStatus) |
|
379 { |
|
380 StartAsyncOperation(EExportKey, aStatus); |
|
381 |
|
382 ASSERT(!iKeyStoreManager); |
|
383 iKeyStoreManager = FindKeyStoreManager(aHandle); |
|
384 if (!iKeyStoreManager) |
|
385 { |
|
386 Complete(KErrNotFound); |
|
387 return; |
|
388 } |
|
389 |
|
390 iKeyStoreManager->ExportKey(aHandle, aKey, iStatus); |
|
391 SetActive(); |
|
392 } |
|
393 |
|
394 EXPORT_C void CUnifiedKeyStore::CancelExportKey() |
|
395 { |
|
396 if (iState == EExportKey) |
|
397 { |
|
398 Cancel(); |
|
399 } |
|
400 } |
|
401 |
|
402 EXPORT_C void CUnifiedKeyStore::ExportEncryptedKey(TCTTokenObjectHandle aHandle, |
|
403 const CPBEncryptParms& aEncryptParams, |
|
404 HBufC8*& aKey, TRequestStatus& aStatus) |
|
405 { |
|
406 StartAsyncOperation(EExportEncryptedKey, aStatus); |
|
407 |
|
408 ASSERT(!iKeyStoreManager); |
|
409 iKeyStoreManager = FindKeyStoreManager(aHandle); |
|
410 if (!iKeyStoreManager) |
|
411 { |
|
412 Complete(KErrNotFound); |
|
413 return; |
|
414 } |
|
415 |
|
416 iKeyStoreManager->ExportEncryptedKey(aHandle, aEncryptParams, aKey, iStatus); |
|
417 SetActive(); |
|
418 } |
|
419 |
|
420 EXPORT_C void CUnifiedKeyStore::CancelExportEncryptedKey() |
|
421 { |
|
422 if (iState == EExportEncryptedKey) |
|
423 { |
|
424 Cancel(); |
|
425 } |
|
426 } |
|
427 |
|
428 EXPORT_C void CUnifiedKeyStore::DeleteKey(TCTTokenObjectHandle aHandle, |
|
429 TRequestStatus& aStatus) |
|
430 { |
|
431 StartAsyncOperation(EDeleteKey, aStatus); |
|
432 |
|
433 iKeyStoreManager = FindKeyStoreManager(aHandle); |
|
434 if (!iKeyStoreManager) |
|
435 { |
|
436 Complete(KErrNotFound); |
|
437 return; |
|
438 } |
|
439 |
|
440 iKeyStoreManager->DeleteKey(aHandle, iStatus); |
|
441 SetActive(); |
|
442 } |
|
443 |
|
444 EXPORT_C void CUnifiedKeyStore::CancelDeleteKey() |
|
445 { |
|
446 if (iState == EDeleteKey) |
|
447 { |
|
448 Cancel(); |
|
449 } |
|
450 } |
|
451 |
|
452 EXPORT_C void CUnifiedKeyStore::SetUsePolicy(TCTTokenObjectHandle aHandle, |
|
453 const TSecurityPolicy& aPolicy, |
|
454 TRequestStatus& aStatus) |
|
455 { |
|
456 StartAsyncOperation(ESetUsePolicy, aStatus); |
|
457 |
|
458 iKeyStoreManager = FindKeyStoreManager(aHandle); |
|
459 if (!iKeyStoreManager) |
|
460 { |
|
461 Complete(KErrNotFound); |
|
462 return; |
|
463 } |
|
464 |
|
465 iKeyStoreManager->SetUsePolicy(aHandle, aPolicy, iStatus); |
|
466 SetActive(); |
|
467 } |
|
468 |
|
469 EXPORT_C void CUnifiedKeyStore::CancelSetUsePolicy() |
|
470 { |
|
471 if (iState == ESetUsePolicy) |
|
472 { |
|
473 Cancel(); |
|
474 } |
|
475 } |
|
476 |
|
477 EXPORT_C void CUnifiedKeyStore::SetManagementPolicy(TCTTokenObjectHandle aHandle, |
|
478 const TSecurityPolicy& aPolicy, |
|
479 TRequestStatus& aStatus) |
|
480 { |
|
481 StartAsyncOperation(ESetManagementPolicy, aStatus); |
|
482 |
|
483 iKeyStoreManager = FindKeyStoreManager(aHandle); |
|
484 if (!iKeyStoreManager) |
|
485 { |
|
486 Complete(KErrNotFound); |
|
487 return; |
|
488 } |
|
489 |
|
490 iKeyStoreManager->SetManagementPolicy(aHandle, aPolicy, iStatus); |
|
491 SetActive(); |
|
492 } |
|
493 |
|
494 EXPORT_C void CUnifiedKeyStore::CancelSetManagementPolicy() |
|
495 { |
|
496 if (iState == ESetManagementPolicy) |
|
497 { |
|
498 Cancel(); |
|
499 } |
|
500 } |
|
501 |
|
502 EXPORT_C void CUnifiedKeyStore::SetPassphraseTimeout(TInt aTimeout, |
|
503 TRequestStatus& aStatus) |
|
504 { |
|
505 StartAsyncOperation(ESetPassphraseTimeout, aStatus); |
|
506 |
|
507 iIndex = -1; |
|
508 iNewTimeout = aTimeout; |
|
509 SetActive(); |
|
510 |
|
511 TRequestStatus* status = &iStatus; |
|
512 User::RequestComplete(status, KErrNone); |
|
513 } |
|
514 |
|
515 EXPORT_C void CUnifiedKeyStore::CancelSetPassphraseTimeout() |
|
516 { |
|
517 if (iState == ESetPassphraseTimeout) |
|
518 { |
|
519 Cancel(); |
|
520 } |
|
521 } |
|
522 |
|
523 EXPORT_C void CUnifiedKeyStore::Relock(TRequestStatus& aStatus) |
|
524 { |
|
525 StartAsyncOperation(ERelock, aStatus); |
|
526 |
|
527 iIndex = -1; |
|
528 SetActive(); |
|
529 |
|
530 TRequestStatus* status = &iStatus; |
|
531 User::RequestComplete(status, KErrNone); |
|
532 } |
|
533 |
|
534 EXPORT_C void CUnifiedKeyStore::CancelRelock() |
|
535 { |
|
536 if (iState == ERelock) |
|
537 { |
|
538 Cancel(); |
|
539 } |
|
540 } |
|
541 |
|
542 // ************************************************************************ |
|
543 // Other exports |
|
544 // ************************************************************************ |
|
545 |
|
546 EXPORT_C TInt CUnifiedKeyStore::KeyStoreCount() const |
|
547 { |
|
548 return (iKeyStoresHolder.Count()); |
|
549 } |
|
550 |
|
551 EXPORT_C MCTKeyStore& CUnifiedKeyStore::KeyStore(TInt aIndex) |
|
552 { |
|
553 __ASSERT_ALWAYS(aIndex >= 0 && aIndex < iKeyStoresHolder.Count(), |
|
554 User::Panic(KUnifiedKeyStore, EArrayAccessOutOfBounds)); |
|
555 |
|
556 MCTKeyStore* keyStore = static_cast<MCTKeyStore*>(iKeyStoresHolder[aIndex]->KeyStore()); |
|
557 return (*keyStore); |
|
558 } |
|
559 |
|
560 EXPORT_C TInt CUnifiedKeyStore::KeyStoreManagerCount() const |
|
561 { |
|
562 TInt result = 0; |
|
563 for (TInt i = 0 ; i < iKeyStoresHolder.Count() ; ++i) |
|
564 { |
|
565 if (iKeyStoresHolder[i]->IsKeyManager()) |
|
566 { |
|
567 ++result; |
|
568 } |
|
569 } |
|
570 return result; |
|
571 } |
|
572 |
|
573 EXPORT_C MCTKeyStoreManager& CUnifiedKeyStore::KeyStoreManager(TInt aIndex) |
|
574 { |
|
575 __ASSERT_ALWAYS(aIndex >= 0, User::Panic(KUnifiedKeyStore, EArrayAccessOutOfBounds)); |
|
576 TInt managerIndex = 0; |
|
577 MCTKeyStoreManager* result = NULL; |
|
578 for (TInt i = 0 ; i < iKeyStoresHolder.Count() ; ++i) |
|
579 { |
|
580 if (iKeyStoresHolder[i]->IsKeyManager()) |
|
581 { |
|
582 if (managerIndex == aIndex) |
|
583 { |
|
584 result = static_cast<MCTKeyStoreManager*>(iKeyStoresHolder[i]->KeyStore()); |
|
585 break; |
|
586 } |
|
587 ++managerIndex; |
|
588 } |
|
589 } |
|
590 __ASSERT_ALWAYS(result != NULL, User::Panic(KUnifiedKeyStore, EArrayAccessOutOfBounds)); |
|
591 return *result; |
|
592 } |
|
593 |
|
594 CUnifiedKeyStore::CUnifiedKeyStore(RFs& aFs) |
|
595 : CActive(EPriorityNormal), iFs(aFs), iState(EIdle) |
|
596 {// Currently defaults to always try for key store manager interface |
|
597 // This may change (add parameter to NewL for required interface) |
|
598 iRequestUid.iUid = KInterfaceKeyStoreManager; |
|
599 CActiveScheduler::Add(this); |
|
600 } |
|
601 |
|
602 void CUnifiedKeyStore::ConstructL() |
|
603 {} |
|
604 |
|
605 void CUnifiedKeyStore::StartAsyncOperation(TState aState, TRequestStatus& aStatus) |
|
606 { |
|
607 ASSERT(iState == EIdle); |
|
608 ASSERT(iOriginalRequestStatus == NULL); |
|
609 iState = aState; |
|
610 iOriginalRequestStatus = &aStatus; |
|
611 aStatus = KRequestPending; |
|
612 } |
|
613 |
|
614 MCTKeyStore* CUnifiedKeyStore::FindKeyStore(const TCTTokenObjectHandle& aHandle) |
|
615 { |
|
616 for (TInt index = 0 ; index < iKeyStoresHolder.Count() ; ++index) |
|
617 { |
|
618 MCTTokenInterface* store = iKeyStoresHolder[index]->KeyStore(); |
|
619 ASSERT(store); |
|
620 if (store->Token().Handle() == aHandle.iTokenHandle) |
|
621 { |
|
622 return static_cast<MCTKeyStoreManager*>(store); |
|
623 } |
|
624 } |
|
625 return NULL; |
|
626 } |
|
627 |
|
628 MCTKeyStoreManager* CUnifiedKeyStore::FindKeyStoreManager(const TCTTokenObjectHandle& aHandle) |
|
629 { |
|
630 for (TInt index = 0 ; index < iKeyStoresHolder.Count() ; ++index) |
|
631 { |
|
632 MCTTokenInterface* store = iKeyStoresHolder[index]->KeyStore(); |
|
633 ASSERT(store); |
|
634 if (store->Token().Handle() == aHandle.iTokenHandle && iKeyStoresHolder[index]->IsKeyManager()) |
|
635 { |
|
636 return static_cast<MCTKeyStoreManager*>(store); |
|
637 } |
|
638 } |
|
639 return NULL; |
|
640 } |
|
641 |
|
642 void CUnifiedKeyStore::RunL() |
|
643 { |
|
644 if (EInitializeGetKeyUserInterfaceFinished != iState && |
|
645 EInitializeGetKeyUserInterface != iState && |
|
646 EInitializeGetToken != iState) |
|
647 { |
|
648 User::LeaveIfError(iStatus.Int()); |
|
649 } |
|
650 |
|
651 switch (iState) |
|
652 { |
|
653 case EInitializeGetTokenList: |
|
654 {// Try to get a list of Tokens for each of the Token Types |
|
655 iIndexTokenTypes++; |
|
656 if (iIndexTokenTypes < iTokenTypes.Count()) |
|
657 { |
|
658 __ASSERT_DEBUG(!iTokenType, User::Panic(KUnifiedKeyStore, EArrayAccessOutOfBounds)); |
|
659 iTokenType = MCTTokenType::NewL(*iTokenTypes[iIndexTokenTypes], iFs); |
|
660 __ASSERT_DEBUG(iTokens.Count()==0, User::Panic(KUnifiedKeyStore, ETokensArrayAlreadyInUse)); |
|
661 iTokenType->List(iTokens, iStatus); |
|
662 iIndexTokens = -1; |
|
663 iState = EInitializeGetToken; |
|
664 } |
|
665 else |
|
666 {// We don't need the list of Token Types anymore |
|
667 iTokenTypes.ResetAndDestroy(); |
|
668 iTokenTypes.Close(); |
|
669 iState = EInitializeFinished; |
|
670 TRequestStatus* status = &iStatus; |
|
671 User::RequestComplete(status, KErrNone); |
|
672 } |
|
673 SetActive(); |
|
674 break; |
|
675 } |
|
676 case EInitializeGetToken: |
|
677 { |
|
678 if (iStatus.Int() == KErrHardwareNotAvailable) |
|
679 { |
|
680 // If the hardware corresponding to this |
|
681 // TokenType has been removed then just skip it |
|
682 // but DO NOT leave! |
|
683 ++iIndexTokens; |
|
684 iState = EInitializeGetToken; |
|
685 TRequestStatus* status = &iStatus; |
|
686 User::RequestComplete(status, KErrNone); |
|
687 } |
|
688 else |
|
689 { |
|
690 User::LeaveIfError(iStatus.Int()); |
|
691 iIndexTokens++; |
|
692 |
|
693 if (iIndexTokens < iTokens.Count()) |
|
694 { |
|
695 iTokenType->OpenToken(*iTokens[iIndexTokens], iToken, iStatus); |
|
696 iRequestUid.iUid = KInterfaceKeyStoreManager; |
|
697 iState = EInitialiseGetKeyManagerInterface; |
|
698 } |
|
699 else |
|
700 {// Don't need the iTokenType anymore |
|
701 iTokenType->Release(); |
|
702 iTokenType = 0; |
|
703 |
|
704 iTokens.Close(); // Don't need the list of Tokens anymore |
|
705 iState = EInitializeGetTokenList; |
|
706 TRequestStatus* status = &iStatus; |
|
707 User::RequestComplete(status, KErrNone); |
|
708 } |
|
709 } |
|
710 SetActive(); |
|
711 break; |
|
712 } |
|
713 case EInitialiseGetKeyManagerInterface: |
|
714 {// First try to get a manager interface to the store, if |
|
715 // unsuccessful, try once to get a user interface |
|
716 if (iToken) |
|
717 { |
|
718 iRequestUid.iUid = KInterfaceKeyStoreManager; |
|
719 iToken->GetInterface(iRequestUid, iTokenInterface, iStatus); |
|
720 iState = EInitializeGetKeyUserInterface; |
|
721 SetActive(); |
|
722 } |
|
723 else |
|
724 {// No token |
|
725 User::Leave(KErrNotReady); |
|
726 } |
|
727 break; |
|
728 } |
|
729 case EInitializeGetKeyUserInterface: |
|
730 {// Did we get a manager interface? |
|
731 if (iStatus==KErrNoMemory) |
|
732 { |
|
733 User::Leave(KErrNoMemory); |
|
734 } |
|
735 |
|
736 if (iRequestUid.iUid==KInterfaceKeyStoreManager) |
|
737 { |
|
738 if (KErrNone==iStatus.Int()) |
|
739 {// Success! Store it and finish up |
|
740 CKeyStoreIF* keyStore = new (ELeave) CKeyStoreIF(iTokenInterface, ETrue); |
|
741 CleanupStack::PushL(keyStore); |
|
742 User::LeaveIfError(iKeyStoresHolder.Append(keyStore)); |
|
743 CleanupStack::Pop(keyStore); |
|
744 |
|
745 iTokenInterface = 0; |
|
746 iToken->Release(); |
|
747 iToken = 0; |
|
748 iState = EInitializeGetToken; |
|
749 TRequestStatus* status = &iStatus; |
|
750 User::RequestComplete(status, KErrNone); |
|
751 } |
|
752 else |
|
753 {// No luck getting a manager, so try getting a user |
|
754 iRequestUid.iUid = KInterfaceKeyStore; |
|
755 iToken->GetInterface(iRequestUid, iTokenInterface, iStatus); |
|
756 iState = EInitializeGetKeyUserInterfaceFinished; |
|
757 } |
|
758 } |
|
759 else if (iRequestUid.iUid==KInterfaceKeyStore) |
|
760 {// We were trying for user IF |
|
761 if (iStatus==KErrNone) |
|
762 { |
|
763 if (iToken) |
|
764 { |
|
765 iRequestUid.iUid = KInterfaceKeyStore; |
|
766 iToken->GetInterface(iRequestUid, iTokenInterface, iStatus); |
|
767 iState = EInitializeGetKeyUserInterfaceFinished; |
|
768 } |
|
769 else |
|
770 { |
|
771 User::Leave(KErrNotReady); |
|
772 } |
|
773 } |
|
774 else |
|
775 {// Couldn't even get a user IF |
|
776 User::Leave(iStatus.Int()); |
|
777 } |
|
778 } |
|
779 |
|
780 SetActive(); |
|
781 break; |
|
782 } |
|
783 case EInitializeGetKeyUserInterfaceFinished: |
|
784 { |
|
785 if (iStatus==KErrNone) |
|
786 { |
|
787 CKeyStoreIF* keyStore = new (ELeave) CKeyStoreIF(iTokenInterface, EFalse); |
|
788 CleanupStack::PushL(keyStore); |
|
789 User::LeaveIfError(iKeyStoresHolder.Append(keyStore)); |
|
790 CleanupStack::Pop(keyStore); |
|
791 |
|
792 iTokenInterface = 0; |
|
793 iToken->Release(); |
|
794 iToken = 0; |
|
795 iState = EInitializeGetToken; |
|
796 TRequestStatus* status = &iStatus; |
|
797 User::RequestComplete(status, KErrNone); |
|
798 } |
|
799 else if (iStatus == KErrNoMemory) |
|
800 { |
|
801 User::Leave(KErrNoMemory); |
|
802 } |
|
803 else |
|
804 { |
|
805 iState = EInitializeGetToken; |
|
806 TRequestStatus* status = &iStatus; |
|
807 User::RequestComplete(status,iStatus.Int()); |
|
808 } |
|
809 |
|
810 SetActive(); |
|
811 break; |
|
812 } |
|
813 case EInitializeFinished: |
|
814 Complete(KErrNone); |
|
815 break; |
|
816 |
|
817 case EList: |
|
818 {// iIndex has been initialized in List function |
|
819 ++iIndex; |
|
820 if (iIndex < iKeyStoresHolder.Count()) |
|
821 { |
|
822 iKeyStore = static_cast<MCTKeyStore*>(iKeyStoresHolder[iIndex]->KeyStore()); |
|
823 ASSERT(iKeyStore); |
|
824 iKeyStore->List(*iKeyInfos, *iFilter, iStatus); |
|
825 SetActive(); |
|
826 } |
|
827 else |
|
828 { |
|
829 Complete(KErrNone); |
|
830 } |
|
831 break; |
|
832 } |
|
833 |
|
834 case EGetKeyInfo: |
|
835 Complete(KErrNone); |
|
836 break; |
|
837 |
|
838 case ECreateKey: |
|
839 *iKeyInfoOut = iKeyInfo; |
|
840 iKeyInfo = NULL; // Release ownership |
|
841 Complete(KErrNone); |
|
842 break; |
|
843 |
|
844 case EImportKey: |
|
845 case EImportKeyEncrypted: |
|
846 *iKeyInfoOut = iKeyInfo; |
|
847 iKeyInfo = NULL; // Release ownership |
|
848 Complete(KErrNone); |
|
849 break; |
|
850 |
|
851 case EExportKey: |
|
852 case EExportEncryptedKey: |
|
853 Complete(KErrNone); |
|
854 break; |
|
855 |
|
856 case ERelock: |
|
857 ++iIndex; |
|
858 |
|
859 // Find next key store manager |
|
860 while (iIndex < iKeyStoresHolder.Count() && !iKeyStoresHolder[iIndex]->IsKeyManager()) |
|
861 ++iIndex; |
|
862 |
|
863 if (iIndex < iKeyStoresHolder.Count()) |
|
864 { |
|
865 iKeyStoreManager = static_cast<MCTKeyStoreManager*>(iKeyStoresHolder[iIndex]->KeyStore()); |
|
866 ASSERT(iKeyStoreManager); |
|
867 iKeyStoreManager->Relock(iStatus); |
|
868 SetActive(); |
|
869 } |
|
870 else |
|
871 { |
|
872 Complete(KErrNone); |
|
873 } |
|
874 break; |
|
875 |
|
876 case ESetPassphraseTimeout: |
|
877 ++iIndex; |
|
878 |
|
879 // Find next key store manager |
|
880 while (iIndex < iKeyStoresHolder.Count() && !iKeyStoresHolder[iIndex]->IsKeyManager()) |
|
881 ++iIndex; |
|
882 |
|
883 if (iIndex < iKeyStoresHolder.Count()) |
|
884 { |
|
885 iKeyStoreManager = static_cast<MCTKeyStoreManager*>(iKeyStoresHolder[iIndex]->KeyStore()); |
|
886 ASSERT(iKeyStoreManager); |
|
887 iKeyStoreManager->SetPassphraseTimeout(iNewTimeout, iStatus); |
|
888 SetActive(); |
|
889 } |
|
890 else |
|
891 { |
|
892 Complete(KErrNone); |
|
893 } |
|
894 break; |
|
895 |
|
896 case EOpen: |
|
897 case EExportPublic: |
|
898 case EDeleteKey: |
|
899 case ESetUsePolicy: |
|
900 case ESetManagementPolicy: |
|
901 Complete(KErrNone); |
|
902 break; |
|
903 default: |
|
904 User::Panic(KUnifiedKeyStore, EUnrecognisedState); |
|
905 break; |
|
906 } |
|
907 } |
|
908 |
|
909 TInt CUnifiedKeyStore::RunError(TInt aError) |
|
910 { |
|
911 Complete(aError); |
|
912 return KErrNone; |
|
913 } |
|
914 |
|
915 void CUnifiedKeyStore::DoCancel() |
|
916 { |
|
917 // If the current state is the last state involved in handling a request, we |
|
918 // check to see if we have already been completed - in this case we can |
|
919 // simply complete the client with iStatus (this may be KErrNone). If we |
|
920 // have not we cancel the outstanding request and pass the resulting iStatus |
|
921 // back to the client - this too may indicate a successful completion if the |
|
922 // cancel arrived after the request was executed. |
|
923 // |
|
924 // For more complex cases, where there are more states to go through before |
|
925 // we finish servicing the client request, we cancel any outstanding |
|
926 // request, and return KErrCancel to the client. |
|
927 |
|
928 switch (iState) |
|
929 { |
|
930 case EInitializeFinished: |
|
931 case EGetKeyInfo: |
|
932 case ECreateKey: |
|
933 case EImportKey: |
|
934 case EImportKeyEncrypted: |
|
935 case EExportKey: |
|
936 case EExportEncryptedKey: |
|
937 case EOpen: |
|
938 case EExportPublic: |
|
939 case EDeleteKey: |
|
940 case ESetUsePolicy: |
|
941 case ESetManagementPolicy: |
|
942 if (iStatus == KRequestPending) |
|
943 { |
|
944 // Attempt to cancel outstanding request and pass status back to |
|
945 // client |
|
946 CancelOutstandingRequest(); |
|
947 Complete(iStatus.Int()); |
|
948 } |
|
949 else |
|
950 { |
|
951 // We've already been completed - call RunL() to process results |
|
952 // and complete client |
|
953 TRAPD(err, RunL()); |
|
954 if (err != KErrNone) |
|
955 { |
|
956 RunError(err); |
|
957 } |
|
958 } |
|
959 break; |
|
960 |
|
961 default: |
|
962 CancelOutstandingRequest(); |
|
963 Complete(KErrCancel); |
|
964 break; |
|
965 } |
|
966 } |
|
967 |
|
968 void CUnifiedKeyStore::CancelOutstandingRequest() |
|
969 { |
|
970 switch (iState) |
|
971 { |
|
972 case EInitializeGetTokenList: |
|
973 case EInitializeGetToken: |
|
974 case EInitialiseGetKeyManagerInterface: |
|
975 case EInitializeGetKeyUserInterface: |
|
976 case EInitializeGetKeyUserInterfaceFinished: |
|
977 case EInitializeFinished: |
|
978 // Don't have to cancel initialisation stuff - this happens when we |
|
979 // release the objects in Cleanup(). |
|
980 iStatus = KErrCancel; |
|
981 break; |
|
982 |
|
983 case EList: |
|
984 if (iKeyStore) |
|
985 { |
|
986 iKeyStore->CancelList(); |
|
987 } |
|
988 break; |
|
989 |
|
990 case EGetKeyInfo: |
|
991 ASSERT(iKeyStore); |
|
992 iKeyStore->CancelGetKeyInfo(); |
|
993 break; |
|
994 |
|
995 case EOpen: |
|
996 ASSERT(iKeyStore); |
|
997 iKeyStore->CancelOpen(); |
|
998 break; |
|
999 |
|
1000 case EExportPublic: |
|
1001 ASSERT(iKeyStore); |
|
1002 iKeyStore->CancelExportPublic(); |
|
1003 break; |
|
1004 |
|
1005 case ECreateKey: |
|
1006 ASSERT(iKeyStoreManager); |
|
1007 iKeyStoreManager->CancelCreateKey(); |
|
1008 break; |
|
1009 |
|
1010 case EImportKey: |
|
1011 case EImportKeyEncrypted: |
|
1012 ASSERT(iKeyStoreManager); |
|
1013 iKeyStoreManager->CancelImportKey(); |
|
1014 break; |
|
1015 |
|
1016 case EExportKey: |
|
1017 case EExportEncryptedKey: |
|
1018 ASSERT(iKeyStoreManager); |
|
1019 iKeyStoreManager->CancelExportKey(); |
|
1020 break; |
|
1021 |
|
1022 case EDeleteKey: |
|
1023 ASSERT(iKeyStoreManager); |
|
1024 iKeyStoreManager->CancelDeleteKey(); |
|
1025 break; |
|
1026 |
|
1027 case ERelock: |
|
1028 ASSERT(iKeyStoreManager); |
|
1029 iKeyStoreManager->CancelRelock(); |
|
1030 break; |
|
1031 |
|
1032 case ESetPassphraseTimeout: |
|
1033 ASSERT(iKeyStoreManager); |
|
1034 iKeyStoreManager->CancelSetPassphraseTimeout(); |
|
1035 break; |
|
1036 |
|
1037 case ESetUsePolicy: |
|
1038 ASSERT(iKeyStoreManager); |
|
1039 iKeyStoreManager->CancelSetUsePolicy(); |
|
1040 break; |
|
1041 |
|
1042 case ESetManagementPolicy: |
|
1043 ASSERT(iKeyStoreManager); |
|
1044 iKeyStoreManager->CancelSetManagementPolicy(); |
|
1045 break; |
|
1046 |
|
1047 default: |
|
1048 User::Panic(KUnifiedKeyStore, EUnrecognisedState); |
|
1049 break; |
|
1050 } |
|
1051 } |
|
1052 |
|
1053 |
|
1054 void CUnifiedKeyStore::Complete(TInt aError) |
|
1055 { |
|
1056 Cleanup(); |
|
1057 if (iOriginalRequestStatus) |
|
1058 { |
|
1059 User::RequestComplete(iOriginalRequestStatus, aError); |
|
1060 } |
|
1061 } |
|
1062 |
|
1063 void CUnifiedKeyStore::Cleanup() |
|
1064 { |
|
1065 // If we have a key info, we want to release it |
|
1066 if (iKeyInfo) |
|
1067 { |
|
1068 iKeyInfo->Release(); |
|
1069 iKeyInfo = NULL; |
|
1070 } |
|
1071 |
|
1072 delete iKeyData; |
|
1073 iKeyData = NULL; |
|
1074 |
|
1075 delete iFilter; |
|
1076 iFilter = NULL; |
|
1077 |
|
1078 delete iPbeParams; |
|
1079 iPbeParams = NULL; |
|
1080 |
|
1081 iTokenTypes.Close(); |
|
1082 |
|
1083 if (iTokenType) |
|
1084 { |
|
1085 iTokenType->Release(); |
|
1086 iTokenType = 0; |
|
1087 } |
|
1088 |
|
1089 iTokens.Close(); |
|
1090 |
|
1091 if (iToken) |
|
1092 { |
|
1093 iToken->Release(); |
|
1094 iToken = 0; |
|
1095 } |
|
1096 |
|
1097 if (iTokenInterface) |
|
1098 { |
|
1099 iTokenInterface->Release(); |
|
1100 iTokenInterface = 0; |
|
1101 } |
|
1102 |
|
1103 iKeyInfoOut = NULL; |
|
1104 iKeyStore = NULL; |
|
1105 iKeyStoreManager = NULL; |
|
1106 |
|
1107 iState = EIdle; |
|
1108 } |
|
1109 |
|
1110 CUnifiedKeyStore::CKeyStoreIF::CKeyStoreIF(MCTTokenInterface* aKeyStore, TBool aIsKeyManager) |
|
1111 : iKeyStore(aKeyStore), iIsKeyManager(aIsKeyManager) |
|
1112 {} |
|
1113 |
|
1114 CUnifiedKeyStore::CKeyStoreIF::~CKeyStoreIF() |
|
1115 { |
|
1116 if (iKeyStore) |
|
1117 { |
|
1118 iKeyStore->Release(); |
|
1119 iKeyStore = NULL; |
|
1120 } |
|
1121 } |