os/security/cryptoservices/filebasedcertificateandkeystores/source/keystore/Server/CKeyDataManager.h
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 /*
     2 * Copyright (c) 2004-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 * Implements CFileKeyData and CFileKeyDataManager 
    16 *
    17 */
    18 
    19 
    20 /**
    21  @file 
    22  @internalTechnology
    23 */
    24  
    25 #ifndef __CKEYDATAMANAGER_H__
    26 #define __CKEYDATAMANAGER_H__
    27 
    28 #include <s32file.h>
    29 #include <ct/tcttokenobjecthandle.h>
    30 #include <ct.h>
    31 
    32 class CKeyInfo;
    33 class CPassphraseManager;
    34 class CPassphrase;
    35 
    36 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    37 #include <authserver/authtypes.h>
    38 #include <e32property.h>
    39 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    40 
    41 #ifdef KEYTOOL
    42 #include <authserver/authtypes.h>
    43 #include <e32property.h>
    44 #endif // KEYTOOL
    45 
    46 /**
    47  * Server side in-memory representation of a key for file key store management
    48  * purposes. Objects of this type are stored in an in-memory array for quick
    49  * keystore lookup.
    50  */
    51 class CFileKeyData : public CBase
    52 	{
    53 public:
    54 	/// Create a new key data object
    55 	static CFileKeyData* NewLC(TInt aObjectId, const TDesC& aLabel, TStreamId aInfoData,
    56 							   TStreamId aPassphraseId, TStreamId aPublicData, TStreamId aPrivateData);
    57     /// Read a key data object from a stream
    58 	static CFileKeyData* NewL(RStoreReadStream& aReadStream);
    59 	
    60 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    61 	static CFileKeyData* NewLC(	TInt aObjectId, const TDesC& aLabel, 
    62 								TStreamId aInfoData, TStreamId aPublicData, 
    63 								TStreamId aPrivateData, AuthServer::TIdentityId aIdentityId);
    64 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    65 	
    66 #ifdef KEYTOOL
    67 	static CFileKeyData* CreateOldKeyL(RStoreReadStream& aReadStream);
    68 #endif // KEYTOOL
    69 public:
    70 	~CFileKeyData();	
    71 public:
    72 	/// Write out the key data to a stream
    73 	void ExternalizeL(RWriteStream&) const;
    74 #ifdef KEYTOOL
    75 	void ExternalizeWithAuthL(RWriteStream& aWriteStream) ;
    76 #endif // KEYTOOL
    77 public:
    78 	inline const TDesC& Label() const;
    79 	inline TInt32 Handle() const;
    80 	inline TStreamId InfoDataStreamId() const;
    81 	inline TStreamId PassphraseStreamId() const;
    82 	inline TStreamId PublicDataStreamId() const;
    83 	inline TStreamId PrivateDataStreamId() const;
    84 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    85 	inline AuthServer::TIdentityId Identity() const;
    86 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    87 #ifdef KEYTOOL
    88 	inline void SetInfoDataStreamId( TStreamId& aInfoDataStreamId );
    89 	inline void SetPublicDataStreamId( TStreamId& aPublicDataStreamId );
    90 	inline void SetPrivateDataStreamId( TStreamId& aPrivateDataStreamId );
    91 	inline AuthServer::TIdentityId Identity() const;
    92 #endif // KEYTOOL
    93 private:
    94 	CFileKeyData(TInt aObjectId, TStreamId aInfoData, TStreamId aPassphraseId,
    95 				 TStreamId aPublicData, TStreamId aPrivateData);
    96 
    97 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
    98 	CFileKeyData(	TInt aObjectId, TStreamId aInfoData,  
    99 					TStreamId aPublicData, TStreamId aPrivateData, 
   100 					AuthServer::TIdentityId aIdentityId);
   101 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   102 
   103 #ifdef KEYTOOL
   104 	CFileKeyData(	TInt aObjectId, TStreamId aInfoData,  
   105 					TStreamId aPublicData, TStreamId aPrivateData, 
   106 					AuthServer::TIdentityId aIdentityId);
   107 #endif // KEYTOOL
   108 
   109 	CFileKeyData();
   110 	void ConstructL(const TDesC& aLabel);
   111 	void InternalizeL(RReadStream&);
   112 #ifdef KEYTOOL
   113 	void InternalizeOldKeyL(RReadStream& aReadStream);
   114 #endif // KEYTOOL
   115 	
   116 private:
   117 	TInt iObjectId;				///< Data to identify the key
   118 	TStreamId iInfoData;		///< ID of stream holding publicly available data for key 
   119 	TStreamId iPassphraseId;    ///< ID of stream holding passphrase data (not currently used)
   120 	TStreamId iPublicKeyData;	///< ID of stream holding public key data
   121 	TStreamId iPrivateKeyData;	///< ID of stream holding private key data
   122 	HBufC* iLabel;				///< Key label data
   123 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   124 	AuthServer::TIdentityId iIdentityId;
   125 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   126 #ifdef KEYTOOL
   127 	AuthServer::TIdentityId iIdentityId;
   128 #endif // KEYTOOL
   129 };
   130 
   131 inline const TDesC& CFileKeyData::Label() const
   132 	{
   133 	return *iLabel;
   134 	}
   135 
   136 inline TInt32 CFileKeyData::Handle() const
   137 	{
   138 	return iObjectId;
   139 	}
   140 
   141 inline TStreamId CFileKeyData::InfoDataStreamId() const
   142 	{
   143 	return iInfoData;
   144 	}
   145 
   146 inline TStreamId CFileKeyData::PassphraseStreamId() const
   147 	{
   148 	return iPassphraseId;
   149 	}
   150 
   151 inline TStreamId CFileKeyData::PublicDataStreamId() const
   152 	{
   153 	return iPublicKeyData;
   154 	}
   155 
   156 inline TStreamId CFileKeyData::PrivateDataStreamId() const
   157 	{
   158 	return iPrivateKeyData;
   159 	}
   160 
   161 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   162 inline AuthServer::TIdentityId CFileKeyData::Identity() const
   163 	{
   164 	return iIdentityId;
   165 	}
   166 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   167 
   168 #ifdef KEYTOOL
   169 	inline void CFileKeyData::SetInfoDataStreamId( TStreamId& aInfoDataStreamId )
   170 		{
   171 		iInfoData = aInfoDataStreamId;
   172 		}
   173 	
   174 	inline void CFileKeyData::SetPublicDataStreamId( TStreamId& aPublicDataStreamId )
   175 		{
   176 		iPublicKeyData = aPublicDataStreamId;
   177 		}
   178 	
   179 	inline void CFileKeyData::SetPrivateDataStreamId( TStreamId& aPrivateDataStreamId )
   180 		{
   181 		iPrivateKeyData = aPrivateDataStreamId;
   182 		}
   183 	
   184 #endif // KEYTOOL
   185 /**
   186  * Access the server file store of all keys and key data. The only class to
   187  * access the store, which maintains store integrity When a new key is created,
   188  * it is represented by a CFileKeyData object and added to the array.  
   189  */
   190 class CFileKeyDataManager : public CBase
   191 {
   192 public:
   193 	static CFileKeyDataManager* NewL();
   194 	~CFileKeyDataManager();
   195 public:	
   196 	CPassphraseManager* CreatePassphraseManagerLC();
   197 	void AddL(const CFileKeyData*);
   198 	void RemoveL(TInt aObjectId);
   199 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   200 	TBool IsKeyAlreadyInStore(const TDesC& aKeyLabel, AuthServer::TIdentityId aIdentity) const;
   201 #else
   202 	TBool IsKeyAlreadyInStore(const TDesC& aKeyLabel) const;
   203 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   204 public:
   205 	/// Get the id of the default passphrase, or KNullStreamId if it doesn't exist yet.
   206 	TStreamId DefaultPassphraseId() const;
   207 	/// Create a new key data object for a key create/import and leave it one the cleanup stack
   208 	const CFileKeyData* CreateKeyDataLC(const TDesC& aLabel, TStreamId aPassphrase);
   209 	
   210 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   211 	const CFileKeyData* CreateKeyDataLC(const TDesC& aLabel, AuthServer::TIdentityId aIdentity);
   212 	TUint32 CachedIdentity();
   213 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   214 	
   215 	///	Reads the info data for a given key, returning a new CKeyInfo that's on the cleanup stack
   216 	CKeyInfo* ReadKeyInfoLC(const CFileKeyData& aKeyData) const;
   217 	
   218 	/// Writes key info data for a key
   219 	void WriteKeyInfoL(const CFileKeyData& aKeyData, const CKeyInfo& aKeyInfo);
   220 	/// Writes key info data and reverts changes to the store if if leaves
   221 	void SafeWriteKeyInfoL(const CFileKeyData& aKeyData, const CKeyInfo& aKeyInfo);
   222 	// Methods for opening data streams for a key
   223 	void OpenPublicDataStreamLC(const CFileKeyData& aKeyData, RStoreWriteStream& aStream);
   224 	void OpenPublicDataStreamLC(const CFileKeyData& aKeyData, RStoreReadStream& aStream) const;
   225 	void OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, CPassphrase& aPassphrase, RStoreReadStream& aStream);	
   226 	
   227 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   228 	void OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, RStoreWriteStream& aStream);
   229 	void OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, RStoreReadStream& aStream) const;
   230 #else
   231 	void OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, CPassphrase& aPassphrase, RStoreWriteStream& aStream);
   232 
   233 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   234 	
   235 	/*
   236 	 * not currently implemented, but might be someday
   237 	/// Create a new passphrase by prompting the user and pass back its id.
   238 	void CreatePassphrase(CPassphraseManager& aPassMan, TStreamId& aIdOut, TRequestStatus& aStatus);
   239 	/// Remove an existing passphrase.  Leaves if the passphrase is used by any key.
   240 	void RemovePassphraseL(TStreamId aId);
   241 	 */
   242 	
   243 public:
   244 	TInt Count() const;
   245 	const CFileKeyData* operator[](TInt aIndex) const;
   246 	const CFileKeyData* Lookup(TInt aObjectId) const;
   247 public:
   248 	/**
   249 	 * Get the passphrase timeout.  A timeout of zero indicates
   250 	 * that passphrases are never cached.  A timeout of -1 means cache until an
   251 	 * explicit close operation occurs.
   252 	 */
   253 	TInt GetPassphraseTimeout() const;
   254 	void SetPassphraseTimeoutL(TInt aTimeout);
   255 private:
   256 	CFileKeyDataManager();
   257 	void ConstructL();
   258 private:											//	Manages access to store
   259 	void OpenStoreL();
   260 	void OpenStoreInFileL(const TDesC& aFile);
   261 	void CreateStoreInFileL(const TDesC& aFile);
   262 private:
   263 	void OpenInfoDataStreamLC(const CFileKeyData& aKeyData, RStoreWriteStream&);
   264 	static void RevertStore(TAny* aStore);			//	Cleanupitem
   265 	void WriteKeysToStoreL();
   266 	void UpdateStoreL();
   267 	TStreamId CreateWriteStreamL();
   268 	void ReadPassphraseTimeoutL();
   269 	void WritePassphraseTimeoutL();
   270 	void CompactStore();
   271 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   272 	const HBufC8* DecryptKey(const TDesC8& aEncryptedKey);
   273 	void WriteAuthDetailsL( RStoreWriteStream& aInfoStream, const CKeyInfo& aKeyInfo );
   274 	void ReadAuthDetailsL( RStoreReadStream& aInfoStream, CKeyInfo& aKeyInfo ) const;
   275 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   276 	
   277 private:
   278 	RFile iFile;
   279 	RFs iFs;
   280 	CPermanentFileStore* iFileStore;
   281 	TStreamId iRootStreamId;	 		///< Root of the store
   282 	TStreamId iInfoStreamId;	 		///< Stream that contains list of key data
   283 	
   284 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   285 	RProperty iIdentityId;
   286 #else
   287 	TStreamId iPassStreamId;	 		///< Stream for the default passphrase
   288 	TStreamId iTimeoutStreamId;  		///< Stream for timeout data
   289 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   290 	
   291 private:
   292 	TInt iKeyIdentifier;
   293 	RPointerArray<const CFileKeyData> iKeys;	///< In memory representation of keys in the store
   294 	TInt iTimeout;						///< The passphrase timeout
   295 	
   296 };
   297 
   298 #endif