os/security/cryptoservices/filebasedcertificateandkeystores/test/keytool/keytool_commands.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 *
    16 */
    17 
    18 
    19 #ifndef __KEYTOOL_COMMANDS_H_
    20 #define __KEYTOOL_COMMANDS_H_
    21 
    22 #include <e32base.h>
    23 #include <ct/rmpointerarray.h>
    24 #include <mctkeystore.h>
    25 #include <unifiedkeystore.h>
    26 #include <badesca.h>
    27 
    28 #ifdef KEYTOOL
    29 #include <authserver/authtypes.h>
    30 #include <authserver/identity.h>
    31 #include <authserver/authclient.h>
    32 #endif // KEYTOOL
    33 
    34 class CController;
    35 class CKeyToolController;
    36 #ifdef KEYTOOL
    37 class CKeyInfo;
    38 class CFileKeyData;
    39 class CPermanentFileStore;
    40 class CKeyStoreCenrep;
    41 class CPassphrase;
    42 #endif // KEYTOOL
    43 
    44 /** 
    45  * This class wraps up the command line parameters
    46  * which affect the keystore command to be executed.
    47  * E.g. the key label we refer to, the key store to use, etc.
    48  */
    49 class CKeyToolParameters : public CBase
    50 	{
    51 	public:
    52 		static CKeyToolParameters* NewLC();
    53 		~CKeyToolParameters();
    54 #ifdef KEYTOOL
    55 		enum TErrors
    56 			{
    57 			EMandatoryArgumentMissing=1,
    58 			};
    59 #endif // KEYTOOL
    60 	enum TSetPolicy
    61 		{
    62 		ENone =0,
    63 		ESetUserPolicy,
    64 		ESetManagerPolicy,
    65 		ERemoveUserPolicy,
    66 		ESetAllUsersPolicy,
    67 		};	
    68 	protected:
    69 		void ConstructL();
    70 
    71 	private:
    72 		CKeyToolParameters();
    73 
    74 	public:
    75 		TBool						iIsDetailed;
    76 		HBufC* 						iPrivate;
    77 		HBufC* 						iLabel;
    78 		HBufC* 						iDefault;
    79 		TInt 						iKeystoreIndex;
    80 		TInt 						iCertstoreIndex;
    81 		TKeyUsagePKCS15	 			iUsage;
    82 		CKeyInfoBase::EKeyAccess	iAccess;
    83 		TTime						iEndDate;
    84 		RArray<TUid>				iUIDs;
    85 		HBufC* 						iOwnerType;
    86 		TBool 						iPageWise;
    87 		TBool						iRemoveKey;
    88 		TSetPolicy					iPolicy;
    89 		HBufC*                      iIsDeletable;
    90 #ifdef KEYTOOL
    91 		HBufC*						iOldKeyFile;
    92 		HBufC*						iNewKeyFile;
    93 		HBufC* 						iAuthExpression;
    94 		TInt						iFreshness;
    95 		HBufC*						iPassphrase;
    96 #endif // KEYTOOL
    97 	};
    98 
    99 
   100 /**
   101  * Interface that every keytool command class must implement.
   102  */
   103 class CKeyToolCommand : public CActive
   104 	{
   105 	public:
   106 		virtual void DoCommandL(CUnifiedKeyStore& aKeyStore, CKeyToolParameters* aParam) = 0;
   107 		
   108 	protected:
   109 		CKeyToolCommand(CController* aController);
   110 
   111 	protected:
   112 		CController* iController;
   113 		CKeyToolParameters* iParams; // we do not own this!		
   114 	};
   115 	
   116 	
   117 /**
   118  * Implements the keytool list action. This is an active object as listing is asynchronous.
   119  */
   120 class CKeytoolUsage : public CKeyToolCommand
   121 	{
   122 public:
   123 	static CKeytoolUsage* NewLC(CKeyToolController* aController);
   124 	static CKeytoolUsage* NewL(CKeyToolController* aController);
   125 	~CKeytoolUsage();
   126 	void ConstructL();
   127 
   128 public: // From CKeyToolCommand
   129 	void DoCommandL(CUnifiedKeyStore& aKeyStore, CKeyToolParameters* aParam);
   130 	
   131 public: // From CActive
   132 	void RunL();
   133 	void DoCancel();
   134 	
   135 protected:
   136 	CKeytoolUsage(CKeyToolController* aController);			
   137 	};
   138 	
   139 
   140 /**
   141  * Implements the keytool list action. This is an active object as listing is asynchronous.
   142  */
   143 class CKeytoolList : public CKeyToolCommand
   144 	{
   145 public:
   146 	static CKeytoolList* NewLC(CKeyToolController* aController);
   147 	static CKeytoolList* NewL(CKeyToolController* aController);
   148 	~CKeytoolList();
   149 	void ConstructL();
   150 
   151 public: // From CKeyToolCommand
   152 	void DoCommandL(CUnifiedKeyStore& aKeyStore, CKeyToolParameters* aParam);
   153 	
   154 public: // From CActive
   155 	void RunL();
   156 	void DoCancel();
   157 	TInt RunError(TInt aError);	
   158 	
   159 protected:
   160 	CKeytoolList(CKeyToolController* aController);
   161 
   162 protected:
   163 	RMPointerArray<CCTKeyInfo> iKeys;
   164 	CUnifiedKeyStore* iKeyStore;
   165 	TCTKeyAttributeFilter iFilter;
   166 	MKeyStore* iKeyStoreImp;
   167 	
   168 	enum TState
   169 		{
   170 		EListKeys,
   171 		EFinished,
   172 		EIntermediate
   173 		} iState;			
   174 	};
   175 	
   176 
   177 
   178 
   179 /**
   180  * Implements the keytool import action.
   181  */
   182 class CKeytoolImport : public CKeyToolCommand
   183 	{
   184 public:
   185 	static CKeytoolImport* NewLC(CController* aController);
   186 	static CKeytoolImport* NewL(CController* aController);
   187 	~CKeytoolImport();
   188 	void ConstructL();
   189 
   190 public: // From CKeyToolCommand
   191 	void DoCommandL(CUnifiedKeyStore& aKeyStore, CKeyToolParameters* aParam);
   192 	
   193 public: // From CActive
   194 	void RunL();
   195 	void DoCancel();
   196 	TInt RunError(TInt aError);
   197 	
   198 protected:	
   199 	/**
   200 	 * Given some (supposed) pkcs8 data, tells you whether we are dealing with 
   201 	 * a PrivateKeyInfo or EncryptedPrivateKeyInfo ASN1 type.
   202 	 */
   203 	TBool IsPkcs8EncryptedL(TDesC8& aBinaryData);
   204 	
   205 private:
   206 	CKeytoolImport(CController* aController);
   207 	void Pkcs8PreprocessorL();
   208 	
   209 private:
   210 	HBufC8* iKeyData;
   211 	CCTKeyInfo* iKeyInfo;	
   212 	};
   213 
   214 
   215 
   216 
   217 /**
   218  * Implements the keytool remove action. 
   219  */
   220 class CKeytoolRemove : public CKeytoolList
   221 	{
   222 public:
   223 	static CKeytoolRemove* NewLC(CKeyToolController* aController);
   224 	static CKeytoolRemove* NewL(CKeyToolController* aController);
   225 	~CKeytoolRemove();
   226 	
   227 public: // From CActive
   228 	void RunL();
   229 	
   230 protected:
   231 
   232 private:
   233 	CKeytoolRemove(CKeyToolController* aController);
   234 
   235 private:
   236 	RArray<CCTKeyInfo*> iKeyList;
   237 	TInt idx;
   238 	};
   239 
   240 
   241 
   242 /**
   243  */
   244 class CKeyToolListStores : public CKeyToolCommand
   245 	{
   246 public:
   247 	static CKeyToolListStores* NewLC(CKeyToolController* aController);
   248 	static CKeyToolListStores* NewL(CKeyToolController* aController);
   249 	~CKeyToolListStores();
   250 	void ConstructL();
   251 
   252 public: // From CCertToolCommand
   253 	void DoCommandL(CUnifiedKeyStore& aKeyStore, CKeyToolParameters* aParam);
   254 	
   255 public: // From CActive
   256 	void RunL();
   257 	void DoCancel();	
   258 	
   259 protected:
   260 	CKeyToolListStores(CKeyToolController* aController);			
   261 	
   262 private:
   263 	RArray<MCTKeyStore> iCertStoreList; 
   264 	};
   265 
   266 
   267 /**
   268  * Implements the keytool set policy action. 
   269  */
   270 class CKeytoolSetPolicy : public CKeytoolList
   271 	{
   272 public:
   273 	static CKeytoolSetPolicy* NewLC(CKeyToolController* aController);
   274 	static CKeytoolSetPolicy* NewL(CKeyToolController* aController);
   275 	~CKeytoolSetPolicy();
   276 	
   277 public: // From CActive
   278 	void RunL();
   279 	TInt RunError(TInt aError);
   280 protected:
   281 
   282 private:
   283 	CKeytoolSetPolicy(CKeyToolController* aController);
   284 
   285 private:
   286 	CCTKeyInfo* iKey;
   287 	TInt idx;
   288 	};
   289 
   290 
   291 #ifdef KEYTOOL
   292 
   293 /**
   294  * Implements the migration of old keystore to the new keystore format 
   295  * which uses the authentication server. 
   296  */
   297 class CKeytoolMigrateStore : public CActive
   298 	{
   299 	
   300 private:
   301 	enum TMigrateStoreState
   302 		{
   303 		EAfterAuthentication,
   304 		
   305 		};
   306 public:
   307 	static CKeytoolMigrateStore* NewLC(CKeyToolParameters* aParams);
   308 	static CKeytoolMigrateStore* NewL(CKeyToolParameters* aParams);
   309 	~CKeytoolMigrateStore();
   310 	void ConstructL(CKeyToolParameters* aParams);
   311 
   312 public: // From CKeyToolCommand
   313 	void DoCommandL();
   314 
   315 public: // From CActive
   316 	void RunL();
   317 	void DoCancel();
   318 	TInt RunError(TInt aError);	
   319 	
   320 private:
   321 	CKeytoolMigrateStore();
   322 	void WriteKeyL(	const CKeyInfo& aKeyInfo, 
   323 					RStoreWriteStream& aWriteInfoStream );
   324 
   325 	void WriteAuthDetailsL( RStoreWriteStream& aWriteInfoStream );
   326 	void RevertStore(TAny* aStore);
   327 	void InitializeDefaultParams();
   328 	void WriteKeyInformationL();
   329 	template <class T> inline void EncryptAndStoreL(const T& aKey, RStoreWriteStream& aStream );
   330 	void StoreKeyL(const TDesC8& aKeyData, RStoreWriteStream& aStream);
   331 	template <class T> void RetrieveAndStorePublicKeyL( CFileKeyData* aKeyData, T* aPublicKey );
   332 	template <class T> void RetrieveAndStorePrivateKeyL( CFileKeyData* aKeyData, T* aPrivateKey );
   333 	static void CleanKeyInfo(TAny* aKeyInfo);
   334 	
   335 private:
   336 	AuthServer::TIdentityId iIdentityId;
   337 	AuthServer::RAuthClient iAuthClient; 
   338 	CPermanentFileStore* iWriteFileStore;
   339 	CPermanentFileStore* iReadFileStore;
   340 	TStreamId iWriteLookupStreamId;
   341 	CKeyToolParameters* iParams;
   342 	RFs iFs;
   343 	CKeyStoreCenrep* iKeyStoreCenrep;
   344 	CPassphrase* iPassphrase;
   345 	AuthServer::CIdentity* iUserIdentity;
   346 	TMigrateStoreState iState;
   347 	
   348 	RPointerArray<CFileKeyData> iKeyList;
   349 	};
   350 
   351 #endif // KEYTOOL
   352 #endif
   353