sl@0: /* sl@0: * Copyright (c) 2004-2010 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of the License "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * sl@0: */ sl@0: sl@0: sl@0: #include "CKeyDataManager.h" sl@0: #include "fsdatatypes.h" sl@0: #include "fstokencliserv.h" sl@0: #include "fstokenutil.h" sl@0: #include "keystorepassphrase.h" sl@0: sl@0: _LIT(KKeyStoreFilename,"keys.dat"); sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: #include sl@0: #include sl@0: #else sl@0: const TInt KDefaultPassphraseTimeout = 30; sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: // ********************************************************************* sl@0: // Key store data manager - maintains array of objects representing keys sl@0: // ********************************************************************* sl@0: sl@0: /*static*/ CFileKeyDataManager* CFileKeyDataManager::NewL() sl@0: { sl@0: CFileKeyDataManager* self = new (ELeave) CFileKeyDataManager(); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(); sl@0: CleanupStack::Pop(self); sl@0: return self; sl@0: } sl@0: sl@0: CFileKeyDataManager::~CFileKeyDataManager() sl@0: { sl@0: if (iFileStore) sl@0: { sl@0: CompactStore(); sl@0: delete iFileStore; sl@0: } sl@0: sl@0: iFile.Close(); // May already have been closed by store sl@0: iFs.Close(); sl@0: sl@0: iKeys.ResetAndDestroy(); sl@0: iKeys.Close(); sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: iIdentityId.Close(); sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: } sl@0: sl@0: CFileKeyDataManager::CFileKeyDataManager() : sl@0: iRootStreamId(KNullStreamId), sl@0: iInfoStreamId(KNullStreamId) sl@0: #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: ,iPassStreamId(KNullStreamId), sl@0: iTimeoutStreamId(KNullStreamId) sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: { sl@0: } sl@0: sl@0: void CFileKeyDataManager::ConstructL() sl@0: { sl@0: sl@0: User::LeaveIfError(iFs.Connect()); sl@0: OpenStoreL(); sl@0: sl@0: RStoreReadStream lookupStream; sl@0: lookupStream.OpenLC(*iFileStore, iInfoStreamId); sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: User::LeaveIfError(iIdentityId.Attach( AuthServer::KAuthServerSecureId, sl@0: AuthServer::KUidAuthServerAuthChangeEvent)); sl@0: #else sl@0: iPassStreamId = (TStreamId) lookupStream.ReadUint32L(); sl@0: iTimeoutStreamId = (TStreamId) lookupStream.ReadUint32L(); sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: TInt count = lookupStream.ReadInt32L(); sl@0: for (TInt index = 0; index < count; index++) sl@0: { sl@0: CFileKeyData* keyData = CFileKeyData::NewL(lookupStream); sl@0: CleanupStack::PushL(keyData); sl@0: sl@0: if (keyData->Handle() > iKeyIdentifier) sl@0: iKeyIdentifier = keyData->Handle(); sl@0: sl@0: iKeys.AppendL(keyData); sl@0: CleanupStack::Pop(keyData); sl@0: } sl@0: sl@0: CleanupStack::PopAndDestroy(&lookupStream); sl@0: sl@0: #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: ReadPassphraseTimeoutL(); sl@0: #endif //SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: } sl@0: sl@0: CPassphraseManager* CFileKeyDataManager::CreatePassphraseManagerLC() sl@0: { sl@0: CPassphraseManager* result = CPassphraseManager::NewL(*iFileStore); sl@0: CleanupStack::PushL(result); sl@0: return result; sl@0: } sl@0: sl@0: void CFileKeyDataManager::OpenStoreL() sl@0: { sl@0: // Tries to locate a key store file on the default drive and then from ROM sl@0: // If it cannot find one, tries to create a file with permanent file store sl@0: // inside it In all cases, should initialise iFileStore unless it cannot sl@0: // create the file/store/streams sl@0: sl@0: __ASSERT_DEBUG(!iFileStore, PanicServer(EPanicStoreInitialised)); sl@0: sl@0: TFileName fullPath; sl@0: FileUtils::MakePrivateFilenameL(iFs, KKeyStoreFilename, fullPath); sl@0: sl@0: FileUtils::EnsurePathL(iFs, fullPath); sl@0: TRAPD(result, OpenStoreInFileL(fullPath)); sl@0: sl@0: if (result == KErrInUse) sl@0: { sl@0: // Cannot access the file now. Abort server startup rather than wiping the keystore. sl@0: User::Leave(result); sl@0: } sl@0: sl@0: if (result != KErrNone) sl@0: { sl@0: // Not yet opened a valid store, either no file to be found, or no valid sl@0: // store in it. Copy the original one stored in the ROM. sl@0: delete iFileStore; sl@0: iFileStore = NULL; sl@0: sl@0: TFileName romPath; sl@0: FileUtils::MakePrivateROMFilenameL(iFs, KKeyStoreFilename, romPath); sl@0: sl@0: if (result != KErrNotFound) sl@0: { sl@0: // Wipe the keystore if we can't open it (it's corrupt anyway) sl@0: User::LeaveIfError(iFs.Delete(fullPath)); sl@0: } sl@0: sl@0: // Copy data from rom and open it sl@0: TRAPD(err, sl@0: FileUtils::CopyL(iFs, romPath, fullPath); sl@0: OpenStoreInFileL(fullPath) sl@0: ); sl@0: sl@0: if (KErrNone != err) sl@0: { sl@0: // We tried to copy the keystore from ROM. For some reason this sl@0: // failed and we still cannot open the file. Create a new one from sl@0: // scratch. sl@0: CreateStoreInFileL(fullPath); sl@0: } sl@0: } sl@0: sl@0: __ASSERT_DEBUG(iFileStore, PanicServer(EPanicStoreInitialised)); sl@0: __ASSERT_DEBUG((KNullStreamId!=iRootStreamId), PanicServer(EPanicRootStreamNotReady)); sl@0: __ASSERT_DEBUG((KNullStreamId!=iInfoStreamId), PanicServer(EPanicManagerStreamNotReady)); sl@0: } sl@0: sl@0: void CFileKeyDataManager::CreateStoreInFileL(const TDesC& aFile) sl@0: { sl@0: TInt r = iFs.MkDirAll(aFile); sl@0: if ( (r!=KErrNone) && (r!=KErrAlreadyExists) ) sl@0: User::Leave(r); sl@0: sl@0: iFileStore = CPermanentFileStore::ReplaceL(iFs, aFile, EFileRead | EFileWrite | EFileShareExclusive); sl@0: iFileStore->SetTypeL(KPermanentFileStoreLayoutUid); sl@0: sl@0: TCleanupItem cleanupStore(RevertStore, iFileStore); sl@0: CleanupStack::PushL(cleanupStore); sl@0: sl@0: #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: // Create timeout stream with default timeout sl@0: RStoreWriteStream timeoutStream; sl@0: iTimeoutStreamId = timeoutStream.CreateLC(*iFileStore); sl@0: timeoutStream.WriteUint32L(KDefaultPassphraseTimeout); sl@0: timeoutStream.CommitL(); sl@0: CleanupStack::PopAndDestroy(&timeoutStream); sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: // Create info stream - Currently no passphrase created, and no keys sl@0: RStoreWriteStream infoStream; sl@0: iInfoStreamId = infoStream.CreateLC(*iFileStore); sl@0: sl@0: #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: infoStream.WriteUint32L(KNullStreamId.Value()); sl@0: infoStream.WriteUint32L(iTimeoutStreamId.Value()); sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: infoStream.WriteUint32L(0); // Write key count of zero sl@0: infoStream.CommitL(); sl@0: CleanupStack::PopAndDestroy(&infoStream); sl@0: sl@0: // Create root stream - just contains id of info stream sl@0: RStoreWriteStream rootStream; sl@0: iRootStreamId = rootStream.CreateLC(*iFileStore); sl@0: iFileStore->SetRootL(iRootStreamId); sl@0: sl@0: rootStream.WriteUint32L(iInfoStreamId.Value()); sl@0: rootStream.CommitL(); sl@0: CleanupStack::PopAndDestroy(&rootStream); sl@0: sl@0: WriteKeysToStoreL(); sl@0: sl@0: iFileStore->CommitL(); sl@0: CleanupStack::Pop(); // cleanupStore sl@0: } sl@0: sl@0: void CFileKeyDataManager::OpenStoreInFileL(const TDesC& aFile) sl@0: { sl@0: // Make sure the file isn't write protected sl@0: User::LeaveIfError(iFs.SetAtt(aFile, 0, KEntryAttReadOnly)); sl@0: sl@0: User::LeaveIfError(iFile.Open(iFs, aFile, EFileRead | EFileWrite | EFileShareExclusive)); sl@0: sl@0: iFileStore = CPermanentFileStore::FromL(iFile); sl@0: sl@0: // Get the salt, root and manager TStreamIds sl@0: iRootStreamId = iFileStore->Root(); sl@0: if (iRootStreamId == KNullStreamId) sl@0: { sl@0: User::Leave(KErrCorrupt); sl@0: } sl@0: sl@0: RStoreReadStream rootStream; sl@0: rootStream.OpenLC(*iFileStore, iRootStreamId); sl@0: iInfoStreamId = (TStreamId)(rootStream.ReadUint32L()); sl@0: CleanupStack::PopAndDestroy(&rootStream); sl@0: } sl@0: sl@0: // Methods dealing with atomic updates to key data file //////////////////////// sl@0: sl@0: // This is a cleanup item that reverts the store sl@0: void CFileKeyDataManager::RevertStore(TAny* aStore) sl@0: { sl@0: CPermanentFileStore* store = reinterpret_cast(aStore); sl@0: TRAP_IGNORE(store->RevertL()); sl@0: // We're ignoring the leave code from this becuase there's no way we can sl@0: // handle this sensibly. This shouldn't be a problem in practice - this sl@0: // will leave if for example the file store is on removable which is sl@0: // unexpectedly remove, and this is never the case for us. sl@0: } sl@0: sl@0: // Rewrites the info stream (ie the array of key data info) to the store sl@0: void CFileKeyDataManager::WriteKeysToStoreL() sl@0: { sl@0: RStoreWriteStream lookupStream; sl@0: lookupStream.ReplaceLC(*iFileStore, iInfoStreamId); sl@0: sl@0: #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: lookupStream.WriteUint32L(iPassStreamId.Value()); sl@0: lookupStream.WriteUint32L(iTimeoutStreamId.Value()); sl@0: #endif //SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: TInt keyCount = iKeys.Count(); sl@0: lookupStream.WriteInt32L(keyCount); sl@0: sl@0: for (TInt index = 0; index < keyCount; index++) sl@0: { sl@0: const CFileKeyData* key = iKeys[index]; sl@0: key->ExternalizeL(lookupStream); sl@0: } sl@0: sl@0: lookupStream.CommitL(); sl@0: CleanupStack::PopAndDestroy(&lookupStream); sl@0: } sl@0: sl@0: /** sl@0: * Add a key to the store. Assumes that the key data streams (info, public key sl@0: * and private key) have already been written. sl@0: */ sl@0: void CFileKeyDataManager::AddL(const CFileKeyData* aKeyData) sl@0: { sl@0: ASSERT(aKeyData); sl@0: #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: ASSERT(aKeyData->PassphraseStreamId() != KNullStreamId); sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: // Add the key to to the array, rewrite the infostream and sl@0: // ONLY THEN commit the store sl@0: User::LeaveIfError(iKeys.Append(aKeyData)); sl@0: sl@0: #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: TStreamId oldDefaultPassphraseId; sl@0: sl@0: // Set the default passphrase id if this is the first key sl@0: oldDefaultPassphraseId = iPassStreamId; sl@0: if (iKeys.Count() == 1) sl@0: { sl@0: iPassStreamId = aKeyData->PassphraseStreamId(); sl@0: } sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: TRAPD(err,UpdateStoreL()); sl@0: sl@0: if (err != KErrNone) sl@0: { sl@0: iKeys.Remove(iKeys.Count() - 1); sl@0: #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: iPassStreamId = oldDefaultPassphraseId; sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: User::Leave(err); sl@0: } sl@0: } sl@0: sl@0: void CFileKeyDataManager::UpdateStoreL() sl@0: { sl@0: WriteKeysToStoreL(); sl@0: sl@0: // Release ownership of key data and reset default passphrase id if store sl@0: // can't be written sl@0: TCleanupItem cleanupStore(RevertStore, iFileStore); sl@0: CleanupStack::PushL(cleanupStore); sl@0: sl@0: iFileStore->CommitL(); sl@0: sl@0: CleanupStack::Pop(); // cleanupStore sl@0: } sl@0: sl@0: /** sl@0: * "Transaction safe" key removal - only removes the key in memory and file if sl@0: * all operations are successful. sl@0: */ sl@0: void CFileKeyDataManager::RemoveL(TInt aObjectId) sl@0: { sl@0: TInt index; sl@0: const CFileKeyData* key = NULL; sl@0: for (index = 0 ; index < iKeys.Count() ; ++index) sl@0: { sl@0: if (iKeys[index]->Handle() == aObjectId) sl@0: { sl@0: key = iKeys[index]; sl@0: break; sl@0: } sl@0: } sl@0: sl@0: if (!key) sl@0: { sl@0: User::Leave(KErrNotFound); sl@0: } sl@0: sl@0: TCleanupItem cleanupStore(RevertStore, iFileStore); sl@0: CleanupStack::PushL(cleanupStore); sl@0: sl@0: iFileStore->DeleteL(key->PrivateDataStreamId()); sl@0: iFileStore->DeleteL(key->PublicDataStreamId()); sl@0: iFileStore->DeleteL(key->InfoDataStreamId()); sl@0: sl@0: #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: // Remove the passphrase if it's the last key sl@0: TStreamId oldPassphraseId = iPassStreamId; sl@0: if (Count() == 1) sl@0: { sl@0: iFileStore->DeleteL(iPassStreamId); sl@0: iPassStreamId = KNullStreamId; sl@0: } sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: // Remove the key sl@0: iKeys.Remove(index); sl@0: sl@0: TRAPD(res, WriteKeysToStoreL()); sl@0: sl@0: if (res != KErrNone) sl@0: { sl@0: #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: iPassStreamId = oldPassphraseId; sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: User::LeaveIfError(iKeys.Append(key)); // Put it back, shouldn't leave sl@0: User::Leave(res); sl@0: } sl@0: else sl@0: { sl@0: delete key; // Cannot leave from the point it's removed to here, so no cleanup stack! sl@0: } sl@0: iFileStore->CommitL(); sl@0: sl@0: CleanupStack::Pop(); // cleanupStore sl@0: CompactStore(); sl@0: } sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: TBool CFileKeyDataManager::IsKeyAlreadyInStore(const TDesC& aKeyLabel, AuthServer::TIdentityId aIdentity) const sl@0: {// Check each key in the store to determine if aKeyLabel already exists sl@0: TInt keyCount = iKeys.Count(); sl@0: TBool isInStore = EFalse; sl@0: for (TInt index = 0; index < keyCount; ++index) sl@0: { sl@0: const TDesC& keyLabel = iKeys[index]->Label(); sl@0: if (keyLabel.Compare(aKeyLabel)==0 && (iKeys[index]->Identity() == aIdentity)) sl@0: { sl@0: isInStore = ETrue; sl@0: break; sl@0: } sl@0: } sl@0: return (isInStore); sl@0: } sl@0: sl@0: #else sl@0: TBool CFileKeyDataManager::IsKeyAlreadyInStore(const TDesC& aKeyLabel) const sl@0: {// Check each key in the store to determine if aKeyLabel already exists sl@0: TInt keyCount = iKeys.Count(); sl@0: TBool isInStore = EFalse; sl@0: for (TInt index = 0; index < keyCount; index++) sl@0: { sl@0: const TDesC& keyLabel = iKeys[index]->Label(); sl@0: if (keyLabel.Compare(aKeyLabel)==0) sl@0: { sl@0: isInStore = ETrue; sl@0: break; sl@0: } sl@0: } sl@0: sl@0: return (isInStore); sl@0: } sl@0: sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: TInt CFileKeyDataManager::Count() const sl@0: { sl@0: return iKeys.Count(); sl@0: } sl@0: sl@0: const CFileKeyData* CFileKeyDataManager::operator[](TInt aIndex) const sl@0: { sl@0: return iKeys[aIndex]; sl@0: } sl@0: sl@0: const CFileKeyData* CFileKeyDataManager::Lookup(TInt aObjectId) const sl@0: { sl@0: TInt count = Count(); sl@0: for (TInt i = 0; i < count; ++i) sl@0: { sl@0: if ((*this)[i]->Handle() == aObjectId) sl@0: { sl@0: return (*this)[i]; sl@0: } sl@0: } sl@0: return NULL; sl@0: } sl@0: sl@0: // ********************************************************************* sl@0: // Management of file and store therein sl@0: // ********************************************************************* sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: const CFileKeyData* CFileKeyDataManager::CreateKeyDataLC(const TDesC& aLabel, AuthServer::TIdentityId aIdentityId) sl@0: { sl@0: TInt objectId = ++iKeyIdentifier; sl@0: TStreamId infoData = CreateWriteStreamL(); sl@0: TStreamId publicKeyData = CreateWriteStreamL(); sl@0: TStreamId privateKeyData = CreateWriteStreamL(); sl@0: return CFileKeyData::NewLC(objectId, aLabel, infoData, publicKeyData, privateKeyData, aIdentityId); sl@0: } sl@0: sl@0: #else sl@0: const CFileKeyData* CFileKeyDataManager::CreateKeyDataLC(const TDesC& aLabel, TStreamId aPassStreamId) sl@0: { sl@0: ASSERT(aPassStreamId != KNullStreamId); sl@0: TInt objectId = ++iKeyIdentifier; sl@0: TStreamId infoData = CreateWriteStreamL(); sl@0: TStreamId publicKeyData = CreateWriteStreamL(); sl@0: TStreamId privateKeyData = CreateWriteStreamL(); sl@0: return CFileKeyData::NewLC(objectId, aLabel, infoData, aPassStreamId, publicKeyData, privateKeyData); sl@0: } sl@0: sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: // Creates a new write stream in the store (which it then closes) sl@0: // Returns the TStreamId associated with it sl@0: TStreamId CFileKeyDataManager::CreateWriteStreamL() sl@0: { sl@0: __ASSERT_DEBUG(iFileStore, PanicServer(EPanicStoreInitialised)); sl@0: if (!iFileStore) sl@0: User::Leave(KErrNotReady); sl@0: sl@0: RStoreWriteStream newStream; sl@0: TStreamId result = newStream.CreateLC(*iFileStore); sl@0: if (KNullStreamId == result) sl@0: User::Leave(KErrBadHandle); sl@0: sl@0: newStream.CommitL(); sl@0: CleanupStack::PopAndDestroy(&newStream); sl@0: sl@0: return result; sl@0: } sl@0: sl@0: CKeyInfo* CFileKeyDataManager::ReadKeyInfoLC(const CFileKeyData& aKeyData) const sl@0: { sl@0: __ASSERT_ALWAYS(iFileStore, PanicServer(EPanicStoreInitialised)); sl@0: RStoreReadStream stream; sl@0: stream.OpenLC(*iFileStore, aKeyData.InfoDataStreamId()); sl@0: CKeyInfo* info = CKeyInfo::NewL(stream); sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: ReadAuthDetailsL(stream, *info); sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: CleanupStack::PopAndDestroy(&stream); sl@0: info->CleanupPushL(); sl@0: if (info->Handle() != aKeyData.Handle()) sl@0: { sl@0: User::Leave(KErrCorrupt); // is this appropriate? sl@0: } sl@0: return info; sl@0: } sl@0: sl@0: void CFileKeyDataManager::WriteKeyInfoL(const CFileKeyData& aKeyData, const CKeyInfo& aKeyInfo) sl@0: { sl@0: RStoreWriteStream infoStream; sl@0: OpenInfoDataStreamLC(aKeyData, infoStream); sl@0: infoStream << aKeyInfo; sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: WriteAuthDetailsL(infoStream, aKeyInfo); sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: infoStream.CommitL(); sl@0: CleanupStack::PopAndDestroy(&infoStream); sl@0: } sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: void CFileKeyDataManager::WriteAuthDetailsL( RStoreWriteStream& aInfoStream, const CKeyInfo& aKeyInfo ) sl@0: { sl@0: aInfoStream.WriteInt32L(aKeyInfo.Identity()); sl@0: aInfoStream << aKeyInfo.AuthExpression(); sl@0: aInfoStream.WriteInt32L(aKeyInfo.Freshness()); sl@0: } sl@0: sl@0: void CFileKeyDataManager::ReadAuthDetailsL( RStoreReadStream& aInfoStream, CKeyInfo& aKeyInfo ) const sl@0: { sl@0: aKeyInfo.SetIdentity(aInfoStream.ReadInt32L()); sl@0: HBufC* expression = HBufC::NewLC(aInfoStream, KMaxTInt); sl@0: aKeyInfo.SetAuthExpressionL(*expression); sl@0: aKeyInfo.SetFreshness(aInfoStream.ReadInt32L()); sl@0: CleanupStack::PopAndDestroy(expression); sl@0: } sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: void CFileKeyDataManager::SafeWriteKeyInfoL(const CFileKeyData& aKeyData, const CKeyInfo& aKeyInfo) sl@0: { sl@0: TCleanupItem cleanupStore(RevertStore, iFileStore); sl@0: CleanupStack::PushL(cleanupStore); sl@0: sl@0: WriteKeyInfoL(aKeyData, aKeyInfo); sl@0: iFileStore->CommitL(); sl@0: sl@0: CleanupStack::Pop(); // cleanupStore sl@0: } sl@0: sl@0: void CFileKeyDataManager::OpenInfoDataStreamLC(const CFileKeyData& aKeyData, RStoreWriteStream& aStream) sl@0: { sl@0: __ASSERT_ALWAYS(iFileStore, PanicServer(EPanicStoreInitialised)); sl@0: aStream.ReplaceLC(*iFileStore, aKeyData.InfoDataStreamId()); sl@0: } sl@0: sl@0: void CFileKeyDataManager::OpenPublicDataStreamLC(const CFileKeyData& aKeyData, RStoreWriteStream& aStream) sl@0: { sl@0: __ASSERT_ALWAYS(iFileStore, PanicServer(EPanicStoreInitialised)); sl@0: aStream.ReplaceLC(*iFileStore, aKeyData.PublicDataStreamId()); sl@0: } sl@0: sl@0: void CFileKeyDataManager::OpenPublicDataStreamLC(const CFileKeyData& aKeyData, RStoreReadStream& aStream) const sl@0: { sl@0: __ASSERT_ALWAYS(iFileStore, PanicServer(EPanicStoreInitialised)); sl@0: aStream.OpenLC(*iFileStore, aKeyData.PublicDataStreamId()); sl@0: } sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: void CFileKeyDataManager::OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, RStoreWriteStream& aStream) sl@0: { sl@0: __ASSERT_DEBUG(iFileStore, PanicServer(EPanicStoreInitialised)); sl@0: aStream.ReplaceLC(*iFileStore, aKeyData.PrivateDataStreamId()); sl@0: } sl@0: sl@0: void CFileKeyDataManager::OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, RStoreReadStream& aStream) const sl@0: { sl@0: __ASSERT_DEBUG(iFileStore, PanicServer(EPanicStoreInitialised)); sl@0: aStream.OpenLC(*iFileStore, aKeyData.PrivateDataStreamId()); sl@0: } sl@0: sl@0: #else sl@0: sl@0: void CFileKeyDataManager::OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, CPassphrase& aPassphrase, sl@0: RStoreReadStream& aStream) sl@0: { sl@0: __ASSERT_DEBUG(iFileStore, PanicServer(EPanicStoreInitialised)); sl@0: aStream.OpenLC(aPassphrase.Store(), aKeyData.PrivateDataStreamId()); sl@0: } sl@0: sl@0: void CFileKeyDataManager::OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, CPassphrase& aPassphrase, sl@0: RStoreWriteStream& aStream) sl@0: { sl@0: __ASSERT_DEBUG(iFileStore, PanicServer(EPanicStoreInitialised)); sl@0: aStream.ReplaceLC(aPassphrase.Store(), aKeyData.PrivateDataStreamId()); sl@0: } sl@0: sl@0: sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: TInt CFileKeyDataManager::GetPassphraseTimeout() const sl@0: { sl@0: return iTimeout; sl@0: } sl@0: sl@0: void CFileKeyDataManager::SetPassphraseTimeoutL(TInt aTimeout) sl@0: { sl@0: TInt oldTimeout = iTimeout; sl@0: sl@0: iTimeout = aTimeout; sl@0: TRAPD(err, WritePassphraseTimeoutL(); iFileStore->CommitL()); sl@0: sl@0: if (err != KErrNone) sl@0: { sl@0: iTimeout = oldTimeout; sl@0: iFileStore->RevertL(); // shouldn't leave sl@0: User::Leave(err); sl@0: } sl@0: } sl@0: sl@0: void CFileKeyDataManager::ReadPassphraseTimeoutL() sl@0: { sl@0: ASSERT(iTimeout == 0); // Only called from ConstructL() sl@0: sl@0: RStoreReadStream stream; sl@0: stream.OpenLC(*iFileStore, iTimeoutStreamId); sl@0: iTimeout = stream.ReadInt32L(); sl@0: CleanupStack::PopAndDestroy(&stream); sl@0: } sl@0: sl@0: void CFileKeyDataManager::WritePassphraseTimeoutL() sl@0: { sl@0: RStoreWriteStream stream; sl@0: stream.ReplaceLC(*iFileStore, iTimeoutStreamId); sl@0: stream.WriteUint32L(iTimeout); sl@0: stream.CommitL(); sl@0: CleanupStack::PopAndDestroy(&stream); sl@0: } sl@0: sl@0: TStreamId CFileKeyDataManager::DefaultPassphraseId() const sl@0: { sl@0: ASSERT((iPassStreamId == KNullStreamId) == (Count() == 0)); sl@0: return iPassStreamId; sl@0: } sl@0: sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: /** sl@0: * Attempt to compact the store - it doesn't matter if these calls leave, it sl@0: * will only mean that the store takes up more space than necessary. sl@0: */ sl@0: void CFileKeyDataManager::CompactStore() sl@0: { sl@0: ASSERT(iFileStore); sl@0: TRAP_IGNORE(iFileStore->ReclaimL(); iFileStore->CompactL()); sl@0: } sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: TUint32 CFileKeyDataManager::CachedIdentity() sl@0: { sl@0: TInt value = 0; sl@0: iIdentityId.Get(value); sl@0: return value; sl@0: } sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: // CFileKeyData //////////////////////////////////////////////////////////////// sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: CFileKeyData* CFileKeyData::NewLC( TInt aObjectId, const TDesC& aLabel, TStreamId aInfoData, sl@0: TStreamId aPublicData, TStreamId aPrivateData, sl@0: AuthServer::TIdentityId aIdentityId) sl@0: { sl@0: CFileKeyData* self = new (ELeave) CFileKeyData(aObjectId, aInfoData, aPublicData, aPrivateData, aIdentityId); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aLabel); sl@0: return self; sl@0: } sl@0: #else sl@0: CFileKeyData* CFileKeyData::NewLC(TInt aObjectId, const TDesC& aLabel, TStreamId aInfoData, sl@0: TStreamId aPassphraseId, TStreamId aPublicData, TStreamId aPrivateData) sl@0: { sl@0: CFileKeyData* self = new (ELeave) CFileKeyData(aObjectId, aInfoData, aPassphraseId, aPublicData, aPrivateData); sl@0: CleanupStack::PushL(self); sl@0: self->ConstructL(aLabel); sl@0: return self; sl@0: } sl@0: sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: CFileKeyData* CFileKeyData::NewL(RStoreReadStream& aReadStream) sl@0: { sl@0: CFileKeyData* self = new (ELeave) CFileKeyData(); sl@0: CleanupStack::PushL(self); sl@0: self->InternalizeL(aReadStream); sl@0: CleanupStack::Pop(self); sl@0: return (self); sl@0: } sl@0: sl@0: CFileKeyData::~CFileKeyData() sl@0: { sl@0: delete iLabel; sl@0: } sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: CFileKeyData::CFileKeyData(TInt aObjectId, TStreamId aInfoData, sl@0: TStreamId aPublicData, TStreamId aPrivateData, sl@0: AuthServer::TIdentityId aIdentityId) : sl@0: iObjectId(aObjectId), iInfoData(aInfoData), sl@0: iPublicKeyData(aPublicData), iPrivateKeyData(aPrivateData), sl@0: iIdentityId(aIdentityId) sl@0: { sl@0: ASSERT(iObjectId); sl@0: ASSERT(iInfoData != KNullStreamId); sl@0: ASSERT(iPublicKeyData != KNullStreamId); sl@0: ASSERT(iPrivateKeyData != KNullStreamId); sl@0: ASSERT(iIdentityId); sl@0: } sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: #ifdef KEYTOOL sl@0: CFileKeyData::CFileKeyData(TInt aObjectId, TStreamId aInfoData, sl@0: TStreamId aPublicData, TStreamId aPrivateData, sl@0: AuthServer::TIdentityId aIdentityId) : sl@0: iObjectId(aObjectId), iInfoData(aInfoData), sl@0: iPublicKeyData(aPublicData), iPrivateKeyData(aPrivateData), sl@0: iIdentityId(aIdentityId) sl@0: { sl@0: ASSERT(iObjectId); sl@0: ASSERT(iInfoData != KNullStreamId); sl@0: ASSERT(iPublicKeyData != KNullStreamId); sl@0: ASSERT(iPrivateKeyData != KNullStreamId); sl@0: ASSERT(iIdentityId); sl@0: } sl@0: #endif // KEYTOOL sl@0: sl@0: CFileKeyData::CFileKeyData(TInt aObjectId, TStreamId aInfoData, TStreamId aPassphraseId, sl@0: TStreamId aPublicData, TStreamId aPrivateData) : sl@0: iObjectId(aObjectId), iInfoData(aInfoData), iPassphraseId(aPassphraseId), sl@0: iPublicKeyData(aPublicData), iPrivateKeyData(aPrivateData) sl@0: { sl@0: ASSERT(iObjectId); sl@0: ASSERT(iInfoData != KNullStreamId); sl@0: ASSERT(iPassphraseId != KNullStreamId); sl@0: ASSERT(iPublicKeyData != KNullStreamId); sl@0: ASSERT(iPrivateKeyData != KNullStreamId); sl@0: } sl@0: sl@0: CFileKeyData::CFileKeyData() sl@0: { sl@0: } sl@0: sl@0: void CFileKeyData::ConstructL(const TDesC& aLabel) sl@0: { sl@0: TInt labelLen = aLabel.Length(); sl@0: iLabel = HBufC::NewMaxL(labelLen); sl@0: TPtr theLabel(iLabel->Des()); sl@0: theLabel.FillZ(); sl@0: theLabel.Copy(aLabel); sl@0: } sl@0: sl@0: void CFileKeyData::InternalizeL(RReadStream& aReadStream) sl@0: { sl@0: iObjectId = aReadStream.ReadInt32L(); sl@0: iInfoData.InternalizeL(aReadStream); sl@0: #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: iPassphraseId.InternalizeL(aReadStream); sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: iPublicKeyData.InternalizeL(aReadStream); sl@0: iPrivateKeyData.InternalizeL(aReadStream); sl@0: sl@0: TInt labelLen = aReadStream.ReadInt32L(); sl@0: iLabel = HBufC::NewMaxL(labelLen); sl@0: TPtr theLabel((TUint16*)iLabel->Ptr(), labelLen, labelLen); sl@0: theLabel.FillZ(labelLen); sl@0: aReadStream.ReadL(theLabel); sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: iIdentityId = aReadStream.ReadInt32L(); sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: } sl@0: sl@0: void CFileKeyData::ExternalizeL(RWriteStream& aWriteStream) const sl@0: { sl@0: aWriteStream.WriteInt32L(iObjectId); sl@0: iInfoData.ExternalizeL(aWriteStream); sl@0: #ifndef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: iPassphraseId.ExternalizeL(aWriteStream); sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: iPublicKeyData.ExternalizeL(aWriteStream); sl@0: iPrivateKeyData.ExternalizeL(aWriteStream); sl@0: sl@0: TInt labelLen = iLabel->Length(); sl@0: aWriteStream.WriteInt32L(labelLen); sl@0: TPtr theLabel(iLabel->Des()); sl@0: theLabel.SetLength(labelLen); sl@0: aWriteStream.WriteL(theLabel); sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: aWriteStream.WriteInt32L(iIdentityId); sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: } sl@0: sl@0: #ifdef KEYTOOL sl@0: sl@0: CFileKeyData* CFileKeyData::CreateOldKeyL(RStoreReadStream& aReadStream) sl@0: { sl@0: CFileKeyData* self = new (ELeave) CFileKeyData(); sl@0: CleanupStack::PushL(self); sl@0: self->InternalizeOldKeyL(aReadStream); sl@0: CleanupStack::Pop(self); sl@0: return (self); sl@0: } sl@0: sl@0: void CFileKeyData::InternalizeOldKeyL(RReadStream& aReadStream) sl@0: { sl@0: iObjectId = aReadStream.ReadInt32L(); sl@0: iInfoData.InternalizeL(aReadStream); sl@0: iPassphraseId.InternalizeL(aReadStream); sl@0: iPublicKeyData.InternalizeL(aReadStream); sl@0: iPrivateKeyData.InternalizeL(aReadStream); sl@0: sl@0: TInt labelLen = aReadStream.ReadInt32L(); sl@0: iLabel = HBufC::NewMaxL(labelLen); sl@0: TPtr theLabel((TUint16*)iLabel->Ptr(), labelLen, labelLen); sl@0: theLabel.FillZ(labelLen); sl@0: aReadStream.ReadL(theLabel); sl@0: } sl@0: sl@0: void CFileKeyData::ExternalizeWithAuthL(RWriteStream& aWriteStream) sl@0: { sl@0: aWriteStream.WriteInt32L(iObjectId); sl@0: iInfoData.ExternalizeL(aWriteStream); sl@0: iPublicKeyData.ExternalizeL(aWriteStream); sl@0: iPrivateKeyData.ExternalizeL(aWriteStream); sl@0: sl@0: TInt labelLen = iLabel->Length(); sl@0: aWriteStream.WriteInt32L(labelLen); sl@0: TPtr theLabel(iLabel->Des()); sl@0: theLabel.SetLength(labelLen); sl@0: aWriteStream.WriteL(theLabel); sl@0: aWriteStream.WriteInt32L(iIdentityId); sl@0: } sl@0: sl@0: #endif // KEYTOOL