os/security/cryptoservices/filebasedcertificateandkeystores/source/keystore/Server/CKeyDataManager.cpp
Update contrib.
2 * Copyright (c) 2004-2010 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 "CKeyDataManager.h"
20 #include "fsdatatypes.h"
21 #include "fstokencliserv.h"
22 #include "fstokenutil.h"
23 #include "keystorepassphrase.h"
25 _LIT(KKeyStoreFilename,"keys.dat");
27 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
28 #include <e32property.h>
29 #include <authserver/aspubsubdefs.h>
31 const TInt KDefaultPassphraseTimeout = 30;
32 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
34 // *********************************************************************
35 // Key store data manager - maintains array of objects representing keys
36 // *********************************************************************
38 /*static*/ CFileKeyDataManager* CFileKeyDataManager::NewL()
40 CFileKeyDataManager* self = new (ELeave) CFileKeyDataManager();
41 CleanupStack::PushL(self);
43 CleanupStack::Pop(self);
47 CFileKeyDataManager::~CFileKeyDataManager()
55 iFile.Close(); // May already have been closed by store
58 iKeys.ResetAndDestroy();
60 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
62 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
65 CFileKeyDataManager::CFileKeyDataManager() :
66 iRootStreamId(KNullStreamId),
67 iInfoStreamId(KNullStreamId)
68 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
69 ,iPassStreamId(KNullStreamId),
70 iTimeoutStreamId(KNullStreamId)
71 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
75 void CFileKeyDataManager::ConstructL()
78 User::LeaveIfError(iFs.Connect());
81 RStoreReadStream lookupStream;
82 lookupStream.OpenLC(*iFileStore, iInfoStreamId);
84 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
85 User::LeaveIfError(iIdentityId.Attach( AuthServer::KAuthServerSecureId,
86 AuthServer::KUidAuthServerAuthChangeEvent));
88 iPassStreamId = (TStreamId) lookupStream.ReadUint32L();
89 iTimeoutStreamId = (TStreamId) lookupStream.ReadUint32L();
90 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
92 TInt count = lookupStream.ReadInt32L();
93 for (TInt index = 0; index < count; index++)
95 CFileKeyData* keyData = CFileKeyData::NewL(lookupStream);
96 CleanupStack::PushL(keyData);
98 if (keyData->Handle() > iKeyIdentifier)
99 iKeyIdentifier = keyData->Handle();
101 iKeys.AppendL(keyData);
102 CleanupStack::Pop(keyData);
105 CleanupStack::PopAndDestroy(&lookupStream);
107 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
108 ReadPassphraseTimeoutL();
109 #endif //SYMBIAN_KEYSTORE_USE_AUTH_SERVER
113 CPassphraseManager* CFileKeyDataManager::CreatePassphraseManagerLC()
115 CPassphraseManager* result = CPassphraseManager::NewL(*iFileStore);
116 CleanupStack::PushL(result);
120 void CFileKeyDataManager::OpenStoreL()
122 // Tries to locate a key store file on the default drive and then from ROM
123 // If it cannot find one, tries to create a file with permanent file store
124 // inside it In all cases, should initialise iFileStore unless it cannot
125 // create the file/store/streams
127 __ASSERT_DEBUG(!iFileStore, PanicServer(EPanicStoreInitialised));
130 FileUtils::MakePrivateFilenameL(iFs, KKeyStoreFilename, fullPath);
132 FileUtils::EnsurePathL(iFs, fullPath);
133 TRAPD(result, OpenStoreInFileL(fullPath));
135 if (result == KErrInUse)
137 // Cannot access the file now. Abort server startup rather than wiping the keystore.
141 if (result != KErrNone)
143 // Not yet opened a valid store, either no file to be found, or no valid
144 // store in it. Copy the original one stored in the ROM.
149 FileUtils::MakePrivateROMFilenameL(iFs, KKeyStoreFilename, romPath);
151 if (result != KErrNotFound)
153 // Wipe the keystore if we can't open it (it's corrupt anyway)
154 User::LeaveIfError(iFs.Delete(fullPath));
157 // Copy data from rom and open it
159 FileUtils::CopyL(iFs, romPath, fullPath);
160 OpenStoreInFileL(fullPath)
165 // We tried to copy the keystore from ROM. For some reason this
166 // failed and we still cannot open the file. Create a new one from
168 CreateStoreInFileL(fullPath);
172 __ASSERT_DEBUG(iFileStore, PanicServer(EPanicStoreInitialised));
173 __ASSERT_DEBUG((KNullStreamId!=iRootStreamId), PanicServer(EPanicRootStreamNotReady));
174 __ASSERT_DEBUG((KNullStreamId!=iInfoStreamId), PanicServer(EPanicManagerStreamNotReady));
177 void CFileKeyDataManager::CreateStoreInFileL(const TDesC& aFile)
179 TInt r = iFs.MkDirAll(aFile);
180 if ( (r!=KErrNone) && (r!=KErrAlreadyExists) )
183 iFileStore = CPermanentFileStore::ReplaceL(iFs, aFile, EFileRead | EFileWrite | EFileShareExclusive);
184 iFileStore->SetTypeL(KPermanentFileStoreLayoutUid);
186 TCleanupItem cleanupStore(RevertStore, iFileStore);
187 CleanupStack::PushL(cleanupStore);
189 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
190 // Create timeout stream with default timeout
191 RStoreWriteStream timeoutStream;
192 iTimeoutStreamId = timeoutStream.CreateLC(*iFileStore);
193 timeoutStream.WriteUint32L(KDefaultPassphraseTimeout);
194 timeoutStream.CommitL();
195 CleanupStack::PopAndDestroy(&timeoutStream);
196 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
198 // Create info stream - Currently no passphrase created, and no keys
199 RStoreWriteStream infoStream;
200 iInfoStreamId = infoStream.CreateLC(*iFileStore);
202 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
203 infoStream.WriteUint32L(KNullStreamId.Value());
204 infoStream.WriteUint32L(iTimeoutStreamId.Value());
205 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
207 infoStream.WriteUint32L(0); // Write key count of zero
208 infoStream.CommitL();
209 CleanupStack::PopAndDestroy(&infoStream);
211 // Create root stream - just contains id of info stream
212 RStoreWriteStream rootStream;
213 iRootStreamId = rootStream.CreateLC(*iFileStore);
214 iFileStore->SetRootL(iRootStreamId);
216 rootStream.WriteUint32L(iInfoStreamId.Value());
217 rootStream.CommitL();
218 CleanupStack::PopAndDestroy(&rootStream);
222 iFileStore->CommitL();
223 CleanupStack::Pop(); // cleanupStore
226 void CFileKeyDataManager::OpenStoreInFileL(const TDesC& aFile)
228 // Make sure the file isn't write protected
229 User::LeaveIfError(iFs.SetAtt(aFile, 0, KEntryAttReadOnly));
231 User::LeaveIfError(iFile.Open(iFs, aFile, EFileRead | EFileWrite | EFileShareExclusive));
233 iFileStore = CPermanentFileStore::FromL(iFile);
235 // Get the salt, root and manager TStreamIds
236 iRootStreamId = iFileStore->Root();
237 if (iRootStreamId == KNullStreamId)
239 User::Leave(KErrCorrupt);
242 RStoreReadStream rootStream;
243 rootStream.OpenLC(*iFileStore, iRootStreamId);
244 iInfoStreamId = (TStreamId)(rootStream.ReadUint32L());
245 CleanupStack::PopAndDestroy(&rootStream);
248 // Methods dealing with atomic updates to key data file ////////////////////////
250 // This is a cleanup item that reverts the store
251 void CFileKeyDataManager::RevertStore(TAny* aStore)
253 CPermanentFileStore* store = reinterpret_cast<CPermanentFileStore*>(aStore);
254 TRAP_IGNORE(store->RevertL());
255 // We're ignoring the leave code from this becuase there's no way we can
256 // handle this sensibly. This shouldn't be a problem in practice - this
257 // will leave if for example the file store is on removable which is
258 // unexpectedly remove, and this is never the case for us.
261 // Rewrites the info stream (ie the array of key data info) to the store
262 void CFileKeyDataManager::WriteKeysToStoreL()
264 RStoreWriteStream lookupStream;
265 lookupStream.ReplaceLC(*iFileStore, iInfoStreamId);
267 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
268 lookupStream.WriteUint32L(iPassStreamId.Value());
269 lookupStream.WriteUint32L(iTimeoutStreamId.Value());
270 #endif //SYMBIAN_KEYSTORE_USE_AUTH_SERVER
272 TInt keyCount = iKeys.Count();
273 lookupStream.WriteInt32L(keyCount);
275 for (TInt index = 0; index < keyCount; index++)
277 const CFileKeyData* key = iKeys[index];
278 key->ExternalizeL(lookupStream);
281 lookupStream.CommitL();
282 CleanupStack::PopAndDestroy(&lookupStream);
286 * Add a key to the store. Assumes that the key data streams (info, public key
287 * and private key) have already been written.
289 void CFileKeyDataManager::AddL(const CFileKeyData* aKeyData)
292 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
293 ASSERT(aKeyData->PassphraseStreamId() != KNullStreamId);
294 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
296 // Add the key to to the array, rewrite the infostream and
297 // ONLY THEN commit the store
298 User::LeaveIfError(iKeys.Append(aKeyData));
300 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
301 TStreamId oldDefaultPassphraseId;
303 // Set the default passphrase id if this is the first key
304 oldDefaultPassphraseId = iPassStreamId;
305 if (iKeys.Count() == 1)
307 iPassStreamId = aKeyData->PassphraseStreamId();
309 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
311 TRAPD(err,UpdateStoreL());
315 iKeys.Remove(iKeys.Count() - 1);
316 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
317 iPassStreamId = oldDefaultPassphraseId;
318 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
323 void CFileKeyDataManager::UpdateStoreL()
327 // Release ownership of key data and reset default passphrase id if store
329 TCleanupItem cleanupStore(RevertStore, iFileStore);
330 CleanupStack::PushL(cleanupStore);
332 iFileStore->CommitL();
334 CleanupStack::Pop(); // cleanupStore
338 * "Transaction safe" key removal - only removes the key in memory and file if
339 * all operations are successful.
341 void CFileKeyDataManager::RemoveL(TInt aObjectId)
344 const CFileKeyData* key = NULL;
345 for (index = 0 ; index < iKeys.Count() ; ++index)
347 if (iKeys[index]->Handle() == aObjectId)
356 User::Leave(KErrNotFound);
359 TCleanupItem cleanupStore(RevertStore, iFileStore);
360 CleanupStack::PushL(cleanupStore);
362 iFileStore->DeleteL(key->PrivateDataStreamId());
363 iFileStore->DeleteL(key->PublicDataStreamId());
364 iFileStore->DeleteL(key->InfoDataStreamId());
366 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
367 // Remove the passphrase if it's the last key
368 TStreamId oldPassphraseId = iPassStreamId;
371 iFileStore->DeleteL(iPassStreamId);
372 iPassStreamId = KNullStreamId;
374 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
379 TRAPD(res, WriteKeysToStoreL());
383 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
384 iPassStreamId = oldPassphraseId;
385 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
386 User::LeaveIfError(iKeys.Append(key)); // Put it back, shouldn't leave
391 delete key; // Cannot leave from the point it's removed to here, so no cleanup stack!
393 iFileStore->CommitL();
395 CleanupStack::Pop(); // cleanupStore
399 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
400 TBool CFileKeyDataManager::IsKeyAlreadyInStore(const TDesC& aKeyLabel, AuthServer::TIdentityId aIdentity) const
401 {// Check each key in the store to determine if aKeyLabel already exists
402 TInt keyCount = iKeys.Count();
403 TBool isInStore = EFalse;
404 for (TInt index = 0; index < keyCount; ++index)
406 const TDesC& keyLabel = iKeys[index]->Label();
407 if (keyLabel.Compare(aKeyLabel)==0 && (iKeys[index]->Identity() == aIdentity))
417 TBool CFileKeyDataManager::IsKeyAlreadyInStore(const TDesC& aKeyLabel) const
418 {// Check each key in the store to determine if aKeyLabel already exists
419 TInt keyCount = iKeys.Count();
420 TBool isInStore = EFalse;
421 for (TInt index = 0; index < keyCount; index++)
423 const TDesC& keyLabel = iKeys[index]->Label();
424 if (keyLabel.Compare(aKeyLabel)==0)
434 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
436 TInt CFileKeyDataManager::Count() const
438 return iKeys.Count();
441 const CFileKeyData* CFileKeyDataManager::operator[](TInt aIndex) const
443 return iKeys[aIndex];
446 const CFileKeyData* CFileKeyDataManager::Lookup(TInt aObjectId) const
448 TInt count = Count();
449 for (TInt i = 0; i < count; ++i)
451 if ((*this)[i]->Handle() == aObjectId)
459 // *********************************************************************
460 // Management of file and store therein
461 // *********************************************************************
463 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
465 const CFileKeyData* CFileKeyDataManager::CreateKeyDataLC(const TDesC& aLabel, AuthServer::TIdentityId aIdentityId)
467 TInt objectId = ++iKeyIdentifier;
468 TStreamId infoData = CreateWriteStreamL();
469 TStreamId publicKeyData = CreateWriteStreamL();
470 TStreamId privateKeyData = CreateWriteStreamL();
471 return CFileKeyData::NewLC(objectId, aLabel, infoData, publicKeyData, privateKeyData, aIdentityId);
475 const CFileKeyData* CFileKeyDataManager::CreateKeyDataLC(const TDesC& aLabel, TStreamId aPassStreamId)
477 ASSERT(aPassStreamId != KNullStreamId);
478 TInt objectId = ++iKeyIdentifier;
479 TStreamId infoData = CreateWriteStreamL();
480 TStreamId publicKeyData = CreateWriteStreamL();
481 TStreamId privateKeyData = CreateWriteStreamL();
482 return CFileKeyData::NewLC(objectId, aLabel, infoData, aPassStreamId, publicKeyData, privateKeyData);
485 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
487 // Creates a new write stream in the store (which it then closes)
488 // Returns the TStreamId associated with it
489 TStreamId CFileKeyDataManager::CreateWriteStreamL()
491 __ASSERT_DEBUG(iFileStore, PanicServer(EPanicStoreInitialised));
493 User::Leave(KErrNotReady);
495 RStoreWriteStream newStream;
496 TStreamId result = newStream.CreateLC(*iFileStore);
497 if (KNullStreamId == result)
498 User::Leave(KErrBadHandle);
501 CleanupStack::PopAndDestroy(&newStream);
506 CKeyInfo* CFileKeyDataManager::ReadKeyInfoLC(const CFileKeyData& aKeyData) const
508 __ASSERT_ALWAYS(iFileStore, PanicServer(EPanicStoreInitialised));
509 RStoreReadStream stream;
510 stream.OpenLC(*iFileStore, aKeyData.InfoDataStreamId());
511 CKeyInfo* info = CKeyInfo::NewL(stream);
513 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
514 ReadAuthDetailsL(stream, *info);
515 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
516 CleanupStack::PopAndDestroy(&stream);
517 info->CleanupPushL();
518 if (info->Handle() != aKeyData.Handle())
520 User::Leave(KErrCorrupt); // is this appropriate?
525 void CFileKeyDataManager::WriteKeyInfoL(const CFileKeyData& aKeyData, const CKeyInfo& aKeyInfo)
527 RStoreWriteStream infoStream;
528 OpenInfoDataStreamLC(aKeyData, infoStream);
529 infoStream << aKeyInfo;
530 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
531 WriteAuthDetailsL(infoStream, aKeyInfo);
532 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
533 infoStream.CommitL();
534 CleanupStack::PopAndDestroy(&infoStream);
537 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
538 void CFileKeyDataManager::WriteAuthDetailsL( RStoreWriteStream& aInfoStream, const CKeyInfo& aKeyInfo )
540 aInfoStream.WriteInt32L(aKeyInfo.Identity());
541 aInfoStream << aKeyInfo.AuthExpression();
542 aInfoStream.WriteInt32L(aKeyInfo.Freshness());
545 void CFileKeyDataManager::ReadAuthDetailsL( RStoreReadStream& aInfoStream, CKeyInfo& aKeyInfo ) const
547 aKeyInfo.SetIdentity(aInfoStream.ReadInt32L());
548 HBufC* expression = HBufC::NewLC(aInfoStream, KMaxTInt);
549 aKeyInfo.SetAuthExpressionL(*expression);
550 aKeyInfo.SetFreshness(aInfoStream.ReadInt32L());
551 CleanupStack::PopAndDestroy(expression);
553 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
555 void CFileKeyDataManager::SafeWriteKeyInfoL(const CFileKeyData& aKeyData, const CKeyInfo& aKeyInfo)
557 TCleanupItem cleanupStore(RevertStore, iFileStore);
558 CleanupStack::PushL(cleanupStore);
560 WriteKeyInfoL(aKeyData, aKeyInfo);
561 iFileStore->CommitL();
563 CleanupStack::Pop(); // cleanupStore
566 void CFileKeyDataManager::OpenInfoDataStreamLC(const CFileKeyData& aKeyData, RStoreWriteStream& aStream)
568 __ASSERT_ALWAYS(iFileStore, PanicServer(EPanicStoreInitialised));
569 aStream.ReplaceLC(*iFileStore, aKeyData.InfoDataStreamId());
572 void CFileKeyDataManager::OpenPublicDataStreamLC(const CFileKeyData& aKeyData, RStoreWriteStream& aStream)
574 __ASSERT_ALWAYS(iFileStore, PanicServer(EPanicStoreInitialised));
575 aStream.ReplaceLC(*iFileStore, aKeyData.PublicDataStreamId());
578 void CFileKeyDataManager::OpenPublicDataStreamLC(const CFileKeyData& aKeyData, RStoreReadStream& aStream) const
580 __ASSERT_ALWAYS(iFileStore, PanicServer(EPanicStoreInitialised));
581 aStream.OpenLC(*iFileStore, aKeyData.PublicDataStreamId());
584 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
586 void CFileKeyDataManager::OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, RStoreWriteStream& aStream)
588 __ASSERT_DEBUG(iFileStore, PanicServer(EPanicStoreInitialised));
589 aStream.ReplaceLC(*iFileStore, aKeyData.PrivateDataStreamId());
592 void CFileKeyDataManager::OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, RStoreReadStream& aStream) const
594 __ASSERT_DEBUG(iFileStore, PanicServer(EPanicStoreInitialised));
595 aStream.OpenLC(*iFileStore, aKeyData.PrivateDataStreamId());
600 void CFileKeyDataManager::OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, CPassphrase& aPassphrase,
601 RStoreReadStream& aStream)
603 __ASSERT_DEBUG(iFileStore, PanicServer(EPanicStoreInitialised));
604 aStream.OpenLC(aPassphrase.Store(), aKeyData.PrivateDataStreamId());
607 void CFileKeyDataManager::OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, CPassphrase& aPassphrase,
608 RStoreWriteStream& aStream)
610 __ASSERT_DEBUG(iFileStore, PanicServer(EPanicStoreInitialised));
611 aStream.ReplaceLC(aPassphrase.Store(), aKeyData.PrivateDataStreamId());
615 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
617 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
618 TInt CFileKeyDataManager::GetPassphraseTimeout() const
623 void CFileKeyDataManager::SetPassphraseTimeoutL(TInt aTimeout)
625 TInt oldTimeout = iTimeout;
628 TRAPD(err, WritePassphraseTimeoutL(); iFileStore->CommitL());
632 iTimeout = oldTimeout;
633 iFileStore->RevertL(); // shouldn't leave
638 void CFileKeyDataManager::ReadPassphraseTimeoutL()
640 ASSERT(iTimeout == 0); // Only called from ConstructL()
642 RStoreReadStream stream;
643 stream.OpenLC(*iFileStore, iTimeoutStreamId);
644 iTimeout = stream.ReadInt32L();
645 CleanupStack::PopAndDestroy(&stream);
648 void CFileKeyDataManager::WritePassphraseTimeoutL()
650 RStoreWriteStream stream;
651 stream.ReplaceLC(*iFileStore, iTimeoutStreamId);
652 stream.WriteUint32L(iTimeout);
654 CleanupStack::PopAndDestroy(&stream);
657 TStreamId CFileKeyDataManager::DefaultPassphraseId() const
659 ASSERT((iPassStreamId == KNullStreamId) == (Count() == 0));
660 return iPassStreamId;
663 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
666 * Attempt to compact the store - it doesn't matter if these calls leave, it
667 * will only mean that the store takes up more space than necessary.
669 void CFileKeyDataManager::CompactStore()
672 TRAP_IGNORE(iFileStore->ReclaimL(); iFileStore->CompactL());
675 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
676 TUint32 CFileKeyDataManager::CachedIdentity()
679 iIdentityId.Get(value);
682 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
684 // CFileKeyData ////////////////////////////////////////////////////////////////
686 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
687 CFileKeyData* CFileKeyData::NewLC( TInt aObjectId, const TDesC& aLabel, TStreamId aInfoData,
688 TStreamId aPublicData, TStreamId aPrivateData,
689 AuthServer::TIdentityId aIdentityId)
691 CFileKeyData* self = new (ELeave) CFileKeyData(aObjectId, aInfoData, aPublicData, aPrivateData, aIdentityId);
692 CleanupStack::PushL(self);
693 self->ConstructL(aLabel);
697 CFileKeyData* CFileKeyData::NewLC(TInt aObjectId, const TDesC& aLabel, TStreamId aInfoData,
698 TStreamId aPassphraseId, TStreamId aPublicData, TStreamId aPrivateData)
700 CFileKeyData* self = new (ELeave) CFileKeyData(aObjectId, aInfoData, aPassphraseId, aPublicData, aPrivateData);
701 CleanupStack::PushL(self);
702 self->ConstructL(aLabel);
706 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
708 CFileKeyData* CFileKeyData::NewL(RStoreReadStream& aReadStream)
710 CFileKeyData* self = new (ELeave) CFileKeyData();
711 CleanupStack::PushL(self);
712 self->InternalizeL(aReadStream);
713 CleanupStack::Pop(self);
717 CFileKeyData::~CFileKeyData()
722 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
723 CFileKeyData::CFileKeyData(TInt aObjectId, TStreamId aInfoData,
724 TStreamId aPublicData, TStreamId aPrivateData,
725 AuthServer::TIdentityId aIdentityId) :
726 iObjectId(aObjectId), iInfoData(aInfoData),
727 iPublicKeyData(aPublicData), iPrivateKeyData(aPrivateData),
728 iIdentityId(aIdentityId)
731 ASSERT(iInfoData != KNullStreamId);
732 ASSERT(iPublicKeyData != KNullStreamId);
733 ASSERT(iPrivateKeyData != KNullStreamId);
736 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
738 CFileKeyData::CFileKeyData(TInt aObjectId, TStreamId aInfoData,
739 TStreamId aPublicData, TStreamId aPrivateData,
740 AuthServer::TIdentityId aIdentityId) :
741 iObjectId(aObjectId), iInfoData(aInfoData),
742 iPublicKeyData(aPublicData), iPrivateKeyData(aPrivateData),
743 iIdentityId(aIdentityId)
746 ASSERT(iInfoData != KNullStreamId);
747 ASSERT(iPublicKeyData != KNullStreamId);
748 ASSERT(iPrivateKeyData != KNullStreamId);
753 CFileKeyData::CFileKeyData(TInt aObjectId, TStreamId aInfoData, TStreamId aPassphraseId,
754 TStreamId aPublicData, TStreamId aPrivateData) :
755 iObjectId(aObjectId), iInfoData(aInfoData), iPassphraseId(aPassphraseId),
756 iPublicKeyData(aPublicData), iPrivateKeyData(aPrivateData)
759 ASSERT(iInfoData != KNullStreamId);
760 ASSERT(iPassphraseId != KNullStreamId);
761 ASSERT(iPublicKeyData != KNullStreamId);
762 ASSERT(iPrivateKeyData != KNullStreamId);
765 CFileKeyData::CFileKeyData()
769 void CFileKeyData::ConstructL(const TDesC& aLabel)
771 TInt labelLen = aLabel.Length();
772 iLabel = HBufC::NewMaxL(labelLen);
773 TPtr theLabel(iLabel->Des());
775 theLabel.Copy(aLabel);
778 void CFileKeyData::InternalizeL(RReadStream& aReadStream)
780 iObjectId = aReadStream.ReadInt32L();
781 iInfoData.InternalizeL(aReadStream);
782 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
783 iPassphraseId.InternalizeL(aReadStream);
784 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
785 iPublicKeyData.InternalizeL(aReadStream);
786 iPrivateKeyData.InternalizeL(aReadStream);
788 TInt labelLen = aReadStream.ReadInt32L();
789 iLabel = HBufC::NewMaxL(labelLen);
790 TPtr theLabel((TUint16*)iLabel->Ptr(), labelLen, labelLen);
791 theLabel.FillZ(labelLen);
792 aReadStream.ReadL(theLabel);
793 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
794 iIdentityId = aReadStream.ReadInt32L();
795 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
798 void CFileKeyData::ExternalizeL(RWriteStream& aWriteStream) const
800 aWriteStream.WriteInt32L(iObjectId);
801 iInfoData.ExternalizeL(aWriteStream);
802 #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
803 iPassphraseId.ExternalizeL(aWriteStream);
804 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
805 iPublicKeyData.ExternalizeL(aWriteStream);
806 iPrivateKeyData.ExternalizeL(aWriteStream);
808 TInt labelLen = iLabel->Length();
809 aWriteStream.WriteInt32L(labelLen);
810 TPtr theLabel(iLabel->Des());
811 theLabel.SetLength(labelLen);
812 aWriteStream.WriteL(theLabel);
813 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
814 aWriteStream.WriteInt32L(iIdentityId);
815 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
821 CFileKeyData* CFileKeyData::CreateOldKeyL(RStoreReadStream& aReadStream)
823 CFileKeyData* self = new (ELeave) CFileKeyData();
824 CleanupStack::PushL(self);
825 self->InternalizeOldKeyL(aReadStream);
826 CleanupStack::Pop(self);
830 void CFileKeyData::InternalizeOldKeyL(RReadStream& aReadStream)
832 iObjectId = aReadStream.ReadInt32L();
833 iInfoData.InternalizeL(aReadStream);
834 iPassphraseId.InternalizeL(aReadStream);
835 iPublicKeyData.InternalizeL(aReadStream);
836 iPrivateKeyData.InternalizeL(aReadStream);
838 TInt labelLen = aReadStream.ReadInt32L();
839 iLabel = HBufC::NewMaxL(labelLen);
840 TPtr theLabel((TUint16*)iLabel->Ptr(), labelLen, labelLen);
841 theLabel.FillZ(labelLen);
842 aReadStream.ReadL(theLabel);
845 void CFileKeyData::ExternalizeWithAuthL(RWriteStream& aWriteStream)
847 aWriteStream.WriteInt32L(iObjectId);
848 iInfoData.ExternalizeL(aWriteStream);
849 iPublicKeyData.ExternalizeL(aWriteStream);
850 iPrivateKeyData.ExternalizeL(aWriteStream);
852 TInt labelLen = iLabel->Length();
853 aWriteStream.WriteInt32L(labelLen);
854 TPtr theLabel(iLabel->Des());
855 theLabel.SetLength(labelLen);
856 aWriteStream.WriteL(theLabel);
857 aWriteStream.WriteInt32L(iIdentityId);