cryptoservices/filebasedcertificateandkeystores/source/keystore/Server/CKeyDataManager.h
changeset 0 2c201484c85f
child 8 35751d3474b7
equal deleted inserted replaced
-1:000000000000 0:2c201484c85f
       
     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 
       
    22 /**
       
    23  @file 
       
    24  @internalTechnology
       
    25 */
       
    26  
       
    27 #ifndef __CKEYDATAMANAGER_H__
       
    28 #define __CKEYDATAMANAGER_H__
       
    29 
       
    30 #include <s32file.h>
       
    31 #include <tcttokenobjecthandle.h>
       
    32 #include <ct.h>
       
    33 
       
    34 class CKeyInfo;
       
    35 class CPassphraseManager;
       
    36 class CPassphrase;
       
    37 
       
    38 /**
       
    39  * Server side in-memory representation of a key for file key store management
       
    40  * purposes. Objects of this type are stored in an in-memory array for quick
       
    41  * keystore lookup.
       
    42  */
       
    43 class CFileKeyData : public CBase
       
    44 	{
       
    45 public:
       
    46 	/// Create a new key data object
       
    47 	static CFileKeyData* NewLC(TInt aObjectId, const TDesC& aLabel, TStreamId aInfoData,
       
    48 							   TStreamId aPassphraseId, TStreamId aPublicData, TStreamId aPrivateData);
       
    49     /// Read a key data object from a stream
       
    50 	static CFileKeyData* NewL(RStoreReadStream& aReadStream);
       
    51 public:
       
    52 	~CFileKeyData();	
       
    53 public:
       
    54 	/// Write out the key data to a stream
       
    55 	void ExternalizeL(RWriteStream&) const;
       
    56 public:
       
    57 	inline const TDesC& Label() const;
       
    58 	inline TInt32 Handle() const;
       
    59 	inline TStreamId InfoDataStreamId() const;
       
    60 	inline TStreamId PassphraseStreamId() const;
       
    61 	inline TStreamId PublicDataStreamId() const;
       
    62 	inline TStreamId PrivateDataStreamId() const;
       
    63 private:
       
    64 	CFileKeyData(TInt aObjectId, TStreamId aInfoData, TStreamId aPassphraseId,
       
    65 				 TStreamId aPublicData, TStreamId aPrivateData);
       
    66 	CFileKeyData();
       
    67 	void ConstructL(const TDesC& aLabel);
       
    68 	void InternalizeL(RReadStream&);
       
    69 private:
       
    70 	TInt iObjectId;				///< Data to identify the key
       
    71 	TStreamId iInfoData;		///< ID of stream holding publicly available data for key 
       
    72 	TStreamId iPassphraseId;    ///< ID of stream holding passphrase data (not currently used)
       
    73 	TStreamId iPublicKeyData;	///< ID of stream holding public key data
       
    74 	TStreamId iPrivateKeyData;	///< ID of stream holding private key data
       
    75 	HBufC* iLabel;				///< Key label data
       
    76 };
       
    77 
       
    78 inline const TDesC& CFileKeyData::Label() const
       
    79 	{
       
    80 	return *iLabel;
       
    81 	}
       
    82 
       
    83 inline TInt32 CFileKeyData::Handle() const
       
    84 	{
       
    85 	return iObjectId;
       
    86 	}
       
    87 
       
    88 inline TStreamId CFileKeyData::InfoDataStreamId() const
       
    89 	{
       
    90 	return iInfoData;
       
    91 	}
       
    92 
       
    93 inline TStreamId CFileKeyData::PassphraseStreamId() const
       
    94 	{
       
    95 	return iPassphraseId;
       
    96 	}
       
    97 
       
    98 inline TStreamId CFileKeyData::PublicDataStreamId() const
       
    99 	{
       
   100 	return iPublicKeyData;
       
   101 	}
       
   102 
       
   103 inline TStreamId CFileKeyData::PrivateDataStreamId() const
       
   104 	{
       
   105 	return iPrivateKeyData;
       
   106 	}
       
   107 
       
   108 /**
       
   109  * Access the server file store of all keys and key data. The only class to
       
   110  * access the store, which maintains store integrity When a new key is created,
       
   111  * it is represented by a CFileKeyData object and added to the array.  
       
   112  */
       
   113 class CFileKeyDataManager : public CBase
       
   114 {
       
   115 public:
       
   116 	static CFileKeyDataManager* NewL();
       
   117 	~CFileKeyDataManager();
       
   118 public:	
       
   119 	CPassphraseManager* CreatePassphraseManagerLC();
       
   120 	void AddL(const CFileKeyData*);
       
   121 	void RemoveL(TInt aObjectId);
       
   122 	TBool IsKeyAlreadyInStore(const TDesC& aKeyLabel) const;
       
   123 public:
       
   124 	/// Get the id of the default passphrase, or KNullStreamId if it doesn't exist yet.
       
   125 	TStreamId DefaultPassphraseId() const;
       
   126 	/// Create a new key data object for a key create/import and leave it one the cleanup stack
       
   127 	const CFileKeyData* CreateKeyDataLC(const TDesC& aLabel, TStreamId aPassphrase);
       
   128 	///	Reads the info data for a given key, returning a new CKeyInfo that's on the cleanup stack
       
   129 	CKeyInfo* ReadKeyInfoLC(const CFileKeyData& aKeyData) const;
       
   130 	/// Writes key info data for a key
       
   131 	void WriteKeyInfoL(const CFileKeyData& aKeyData, const CKeyInfo& aKeyInfo);
       
   132 	/// Writes key info data and reverts changes to the store if if leaves
       
   133 	void SafeWriteKeyInfoL(const CFileKeyData& aKeyData, const CKeyInfo& aKeyInfo);
       
   134 	// Methods for opening data streams for a key
       
   135 	void OpenPublicDataStreamLC(const CFileKeyData& aKeyData, RStoreWriteStream& aStream);
       
   136 	void OpenPublicDataStreamLC(const CFileKeyData& aKeyData, RStoreReadStream& aStream) const;
       
   137 	void OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, CPassphrase& aPassphrase, RStoreWriteStream& aStream);
       
   138 	void OpenPrivateDataStreamLC(const CFileKeyData& aKeyData, CPassphrase& aPassphrase, RStoreReadStream& aStream);
       
   139 
       
   140 	/*
       
   141 	 * not currently implemented, but might be someday
       
   142 	/// Create a new passphrase by prompting the user and pass back its id.
       
   143 	void CreatePassphrase(CPassphraseManager& aPassMan, TStreamId& aIdOut, TRequestStatus& aStatus);
       
   144 	/// Remove an existing passphrase.  Leaves if the passphrase is used by any key.
       
   145 	void RemovePassphraseL(TStreamId aId);
       
   146 	 */
       
   147 	
       
   148 public:
       
   149 	TInt Count() const;
       
   150 	const CFileKeyData* operator[](TInt aIndex) const;
       
   151 	const CFileKeyData* Lookup(TInt aObjectId) const;
       
   152 public:
       
   153 	/**
       
   154 	 * Get the passphrase timeout.  A timeout of zero indicates
       
   155 	 * that passphrases are never cached.  A timeout of -1 means cache until an
       
   156 	 * explicit close operation occurs.
       
   157 	 */
       
   158 	TInt GetPassphraseTimeout() const;
       
   159 	void SetPassphraseTimeoutL(TInt aTimeout);
       
   160 private:
       
   161 	CFileKeyDataManager();
       
   162 	void ConstructL();
       
   163 private:											//	Manages access to store
       
   164 	void OpenStoreL();
       
   165 	void OpenStoreInFileL(const TDesC& aFile);
       
   166 	void CreateStoreInFileL(const TDesC& aFile);
       
   167 private:
       
   168 	void OpenInfoDataStreamLC(const CFileKeyData& aKeyData, RStoreWriteStream&);
       
   169 	static void RevertStore(TAny* aStore);			//	Cleanupitem
       
   170 	void WriteKeysToStoreL();
       
   171 	TStreamId CreateWriteStreamL();
       
   172 	void ReadPassphraseTimeoutL();
       
   173 	void WritePassphraseTimeoutL();
       
   174 	void CompactStore();
       
   175 private:
       
   176 	RFile iFile;
       
   177 	RFs iFs;
       
   178 	CPermanentFileStore* iFileStore;
       
   179 	TStreamId iRootStreamId;	 		///< Root of the store
       
   180 	TStreamId iInfoStreamId;	 		///< Stream that contains list of key data
       
   181 	TStreamId iPassStreamId;	 		///< Stream for the default passphrase
       
   182 	TStreamId iTimeoutStreamId;  		///< Stream for timeout data
       
   183 private:
       
   184 	TInt iKeyIdentifier;
       
   185 	RPointerArray<const CFileKeyData> iKeys;	///< In memory representation of keys in the store
       
   186 	TInt iTimeout;						///< The passphrase timeout
       
   187 };
       
   188 
       
   189 #endif