sl@0: /* sl@0: * Copyright (c) 2004-2009 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: * Implements CFileKeyData and CFileKeyDataManager sl@0: * sl@0: */ sl@0: sl@0: sl@0: /** sl@0: @file sl@0: @internalTechnology sl@0: */ sl@0: sl@0: #ifndef __CKEYDATAMANAGER_H__ sl@0: #define __CKEYDATAMANAGER_H__ sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: class CKeyInfo; sl@0: class CPassphraseManager; sl@0: class CPassphrase; sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: #include sl@0: #include sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: #ifdef KEYTOOL sl@0: #include sl@0: #include sl@0: #endif // KEYTOOL sl@0: sl@0: /** sl@0: * Server side in-memory representation of a key for file key store management sl@0: * purposes. Objects of this type are stored in an in-memory array for quick sl@0: * keystore lookup. sl@0: */ sl@0: class CFileKeyData : public CBase sl@0: { sl@0: public: sl@0: /// Create a new key data object sl@0: static CFileKeyData* NewLC(TInt aObjectId, const TDesC& aLabel, TStreamId aInfoData, sl@0: TStreamId aPassphraseId, TStreamId aPublicData, TStreamId aPrivateData); sl@0: /// Read a key data object from a stream sl@0: static CFileKeyData* NewL(RStoreReadStream& aReadStream); sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: static CFileKeyData* NewLC( TInt aObjectId, const TDesC& aLabel, sl@0: TStreamId aInfoData, TStreamId aPublicData, sl@0: TStreamId aPrivateData, AuthServer::TIdentityId aIdentityId); sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: #ifdef KEYTOOL sl@0: static CFileKeyData* CreateOldKeyL(RStoreReadStream& aReadStream); sl@0: #endif // KEYTOOL sl@0: public: sl@0: ~CFileKeyData(); sl@0: public: sl@0: /// Write out the key data to a stream sl@0: void ExternalizeL(RWriteStream&) const; sl@0: #ifdef KEYTOOL sl@0: void ExternalizeWithAuthL(RWriteStream& aWriteStream) ; sl@0: #endif // KEYTOOL sl@0: public: sl@0: inline const TDesC& Label() const; sl@0: inline TInt32 Handle() const; sl@0: inline TStreamId InfoDataStreamId() const; sl@0: inline TStreamId PassphraseStreamId() const; sl@0: inline TStreamId PublicDataStreamId() const; sl@0: inline TStreamId PrivateDataStreamId() const; sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: inline AuthServer::TIdentityId Identity() const; sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: #ifdef KEYTOOL sl@0: inline void SetInfoDataStreamId( TStreamId& aInfoDataStreamId ); sl@0: inline void SetPublicDataStreamId( TStreamId& aPublicDataStreamId ); sl@0: inline void SetPrivateDataStreamId( TStreamId& aPrivateDataStreamId ); sl@0: inline AuthServer::TIdentityId Identity() const; sl@0: #endif // KEYTOOL sl@0: private: sl@0: CFileKeyData(TInt aObjectId, TStreamId aInfoData, TStreamId aPassphraseId, sl@0: TStreamId aPublicData, TStreamId aPrivateData); sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: CFileKeyData( TInt aObjectId, TStreamId aInfoData, sl@0: TStreamId aPublicData, TStreamId aPrivateData, sl@0: AuthServer::TIdentityId aIdentityId); sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: #ifdef KEYTOOL sl@0: CFileKeyData( TInt aObjectId, TStreamId aInfoData, sl@0: TStreamId aPublicData, TStreamId aPrivateData, sl@0: AuthServer::TIdentityId aIdentityId); sl@0: #endif // KEYTOOL sl@0: sl@0: CFileKeyData(); sl@0: void ConstructL(const TDesC& aLabel); sl@0: void InternalizeL(RReadStream&); sl@0: #ifdef KEYTOOL sl@0: void InternalizeOldKeyL(RReadStream& aReadStream); sl@0: #endif // KEYTOOL sl@0: sl@0: private: sl@0: TInt iObjectId; ///< Data to identify the key sl@0: TStreamId iInfoData; ///< ID of stream holding publicly available data for key sl@0: TStreamId iPassphraseId; ///< ID of stream holding passphrase data (not currently used) sl@0: TStreamId iPublicKeyData; ///< ID of stream holding public key data sl@0: TStreamId iPrivateKeyData; ///< ID of stream holding private key data sl@0: HBufC* iLabel; ///< Key label data sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: AuthServer::TIdentityId iIdentityId; sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: #ifdef KEYTOOL sl@0: AuthServer::TIdentityId iIdentityId; sl@0: #endif // KEYTOOL sl@0: }; sl@0: sl@0: inline const TDesC& CFileKeyData::Label() const sl@0: { sl@0: return *iLabel; sl@0: } sl@0: sl@0: inline TInt32 CFileKeyData::Handle() const sl@0: { sl@0: return iObjectId; sl@0: } sl@0: sl@0: inline TStreamId CFileKeyData::InfoDataStreamId() const sl@0: { sl@0: return iInfoData; sl@0: } sl@0: sl@0: inline TStreamId CFileKeyData::PassphraseStreamId() const sl@0: { sl@0: return iPassphraseId; sl@0: } sl@0: sl@0: inline TStreamId CFileKeyData::PublicDataStreamId() const sl@0: { sl@0: return iPublicKeyData; sl@0: } sl@0: sl@0: inline TStreamId CFileKeyData::PrivateDataStreamId() const sl@0: { sl@0: return iPrivateKeyData; sl@0: } sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: inline AuthServer::TIdentityId CFileKeyData::Identity() const sl@0: { sl@0: return iIdentityId; sl@0: } sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: #ifdef KEYTOOL sl@0: inline void CFileKeyData::SetInfoDataStreamId( TStreamId& aInfoDataStreamId ) sl@0: { sl@0: iInfoData = aInfoDataStreamId; sl@0: } sl@0: sl@0: inline void CFileKeyData::SetPublicDataStreamId( TStreamId& aPublicDataStreamId ) sl@0: { sl@0: iPublicKeyData = aPublicDataStreamId; sl@0: } sl@0: sl@0: inline void CFileKeyData::SetPrivateDataStreamId( TStreamId& aPrivateDataStreamId ) sl@0: { sl@0: iPrivateKeyData = aPrivateDataStreamId; sl@0: } sl@0: sl@0: #endif // KEYTOOL sl@0: /** sl@0: * Access the server file store of all keys and key data. The only class to sl@0: * access the store, which maintains store integrity When a new key is created, sl@0: * it is represented by a CFileKeyData object and added to the array. sl@0: */ sl@0: class CFileKeyDataManager : public CBase sl@0: { sl@0: public: sl@0: static CFileKeyDataManager* NewL(); sl@0: ~CFileKeyDataManager(); sl@0: public: sl@0: CPassphraseManager* CreatePassphraseManagerLC(); sl@0: void AddL(const CFileKeyData*); sl@0: void RemoveL(TInt aObjectId); sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: TBool IsKeyAlreadyInStore(const TDesC& aKeyLabel, AuthServer::TIdentityId aIdentity) const; sl@0: #else sl@0: TBool IsKeyAlreadyInStore(const TDesC& aKeyLabel) const; sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: public: sl@0: /// Get the id of the default passphrase, or KNullStreamId if it doesn't exist yet. sl@0: TStreamId DefaultPassphraseId() const; sl@0: /// Create a new key data object for a key create/import and leave it one the cleanup stack sl@0: const CFileKeyData* CreateKeyDataLC(const TDesC& aLabel, TStreamId aPassphrase); sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: const CFileKeyData* CreateKeyDataLC(const TDesC& aLabel, AuthServer::TIdentityId aIdentity); sl@0: TUint32 CachedIdentity(); sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: /// Reads the info data for a given key, returning a new CKeyInfo that's on the cleanup stack sl@0: CKeyInfo* ReadKeyInfoLC(const CFileKeyData& aKeyData) const; sl@0: sl@0: /// Writes key info data for a key sl@0: void WriteKeyInfoL(const CFileKeyData& aKeyData, const CKeyInfo& aKeyInfo); sl@0: /// Writes key info data and reverts changes to the store if if leaves sl@0: void SafeWriteKeyInfoL(const CFileKeyData& aKeyData, const CKeyInfo& aKeyInfo); sl@0: // Methods for opening data streams for a key sl@0: void OpenPublicDataStreamLC(const CFileKeyData& aKeyData, RStoreWriteStream& aStream); sl@0: void OpenPublicDataStreamLC(const CFileKeyData& aKeyData, RStoreReadStream& aStream) const; sl@0: void OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, CPassphrase& aPassphrase, RStoreReadStream& aStream); sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: void OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, RStoreWriteStream& aStream); sl@0: void OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, RStoreReadStream& aStream) const; sl@0: #else sl@0: void OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, CPassphrase& aPassphrase, RStoreWriteStream& aStream); sl@0: sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: /* sl@0: * not currently implemented, but might be someday sl@0: /// Create a new passphrase by prompting the user and pass back its id. sl@0: void CreatePassphrase(CPassphraseManager& aPassMan, TStreamId& aIdOut, TRequestStatus& aStatus); sl@0: /// Remove an existing passphrase. Leaves if the passphrase is used by any key. sl@0: void RemovePassphraseL(TStreamId aId); sl@0: */ sl@0: sl@0: public: sl@0: TInt Count() const; sl@0: const CFileKeyData* operator[](TInt aIndex) const; sl@0: const CFileKeyData* Lookup(TInt aObjectId) const; sl@0: public: sl@0: /** sl@0: * Get the passphrase timeout. A timeout of zero indicates sl@0: * that passphrases are never cached. A timeout of -1 means cache until an sl@0: * explicit close operation occurs. sl@0: */ sl@0: TInt GetPassphraseTimeout() const; sl@0: void SetPassphraseTimeoutL(TInt aTimeout); sl@0: private: sl@0: CFileKeyDataManager(); sl@0: void ConstructL(); sl@0: private: // Manages access to store sl@0: void OpenStoreL(); sl@0: void OpenStoreInFileL(const TDesC& aFile); sl@0: void CreateStoreInFileL(const TDesC& aFile); sl@0: private: sl@0: void OpenInfoDataStreamLC(const CFileKeyData& aKeyData, RStoreWriteStream&); sl@0: static void RevertStore(TAny* aStore); // Cleanupitem sl@0: void WriteKeysToStoreL(); sl@0: void UpdateStoreL(); sl@0: TStreamId CreateWriteStreamL(); sl@0: void ReadPassphraseTimeoutL(); sl@0: void WritePassphraseTimeoutL(); sl@0: void CompactStore(); sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: const HBufC8* DecryptKey(const TDesC8& aEncryptedKey); sl@0: void WriteAuthDetailsL( RStoreWriteStream& aInfoStream, const CKeyInfo& aKeyInfo ); sl@0: void ReadAuthDetailsL( RStoreReadStream& aInfoStream, CKeyInfo& aKeyInfo ) const; sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: private: sl@0: RFile iFile; sl@0: RFs iFs; sl@0: CPermanentFileStore* iFileStore; sl@0: TStreamId iRootStreamId; ///< Root of the store sl@0: TStreamId iInfoStreamId; ///< Stream that contains list of key data sl@0: sl@0: #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: RProperty iIdentityId; sl@0: #else sl@0: TStreamId iPassStreamId; ///< Stream for the default passphrase sl@0: TStreamId iTimeoutStreamId; ///< Stream for timeout data sl@0: #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER sl@0: sl@0: private: sl@0: TInt iKeyIdentifier; sl@0: RPointerArray iKeys; ///< In memory representation of keys in the store sl@0: TInt iTimeout; ///< The passphrase timeout sl@0: sl@0: }; sl@0: sl@0: #endif