os/security/cryptoservices/filebasedcertificateandkeystores/test/keytool/keytool_engine.cpp
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 #include "keytool_engine.h"
    20 #include "keytool_controller.h"
    21 #include "keytool_commands.h"
    22 
    23 #include <keytool.rsg>
    24 
    25 
    26 /*static*/ CKeyToolEngine* CKeyToolEngine::NewLC(CKeyToolController* aController)
    27 	{
    28 	CKeyToolEngine* self = new (ELeave) CKeyToolEngine(aController);
    29 	CleanupStack::PushL(self);
    30 	self->ConstructL();
    31 	return self;
    32 	}
    33 	
    34 /* static */ CKeyToolEngine* CKeyToolEngine::NewL(CKeyToolController* aController)
    35 	{
    36 	CKeyToolEngine* self = CKeyToolEngine::NewLC(aController);
    37 	CleanupStack::Pop(self);
    38 	return self;
    39 	}
    40 	
    41 CKeyToolEngine::CKeyToolEngine(CKeyToolController* aController) : CActive(EPriorityNormal)
    42 	{
    43 	iCurrentAction = EIdle;
    44 	iController = aController;
    45 	iInitialized = EFalse;
    46 	}
    47 	
    48 CKeyToolEngine::~CKeyToolEngine()
    49 	{
    50 	Cancel();	
    51 	delete iHandler;	
    52 	delete iKeyStore;		
    53 	iFs.Close();
    54 	if (iActiveStarted)
    55 		{
    56 		delete iScheduler;
    57 		}	
    58 	iInitialized = EFalse;
    59 #ifdef KEYTOOL
    60 	delete iMigrateStoreHandler;
    61 #endif // KEYTOOL
    62 	}
    63 	
    64 void CKeyToolEngine::ConstructL()
    65 	{
    66 	iActiveStarted = EFalse;
    67 	iScheduler = CActiveScheduler::Current();
    68 	if (!iScheduler)
    69 		{
    70 		iActiveStarted =  ETrue;
    71 		iScheduler = new(ELeave) CActiveScheduler;
    72 		CActiveScheduler::Install(iScheduler);	
    73 		}
    74 	
    75 	User::LeaveIfError(iFs.Connect());
    76 	
    77 	iKeyStore = CUnifiedKeyStore::NewL(iFs);
    78 	
    79 	CActiveScheduler::Add(this);	
    80 	}
    81 
    82 void CKeyToolEngine::RunL()
    83 	{
    84 	if (iStatus.Int() != KErrNone)
    85 		{
    86 		User::Leave(iStatus.Int());
    87 		}
    88 		
    89 	switch (iState)
    90 		{
    91 		case EInitialise:
    92 			{
    93 			iInitialized = ETrue;
    94 			iHandler->DoCommandL(*iKeyStore, iParam);		
    95 			iState = EDone;
    96 			}
    97 			break;
    98 		case EDone:
    99 			{
   100 			}
   101 			break;
   102 		default:
   103 			{
   104 			User::Panic(_L("Keytool Engine - Illegal state"), 0);
   105 			}
   106 		}
   107 	}
   108 	
   109 TInt CKeyToolEngine::RunError(TInt aError)
   110 	{	
   111 	CActiveScheduler::Stop();	
   112 	
   113 	switch (iCurrentAction)
   114 		{
   115 		case EList:
   116 			{
   117 			TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_LIST, aError));				
   118 			}
   119 			break;
   120 		case EImport:
   121 			{
   122 			TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_IMPORT, aError));				
   123 			}
   124 			break;
   125 		default:
   126 			{
   127 			TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_UNKNOWN, aError));				
   128 			}
   129 		}	
   130 	return KErrNone;
   131 	}
   132 
   133 	
   134 void CKeyToolEngine::DoCancel()
   135 	{
   136 	CActiveScheduler::Stop();
   137 	}
   138 
   139 
   140 //\\//\\//\\//\\////\\//\\//\\//\\////\\//\\//\\//\\//
   141 //\\//\\//\\//\\// Business methods //\\//\\//\\//\\//
   142 //\\//\\//\\//\\////\\//\\//\\//\\////\\//\\//\\//\\//
   143 	
   144 void CKeyToolEngine::InitializeL()
   145 	{
   146 	if (!iInitialized)
   147 		{
   148 		iState = EInitialise;
   149 		iKeyStore->Initialize(iStatus);	
   150 		SetActive();		
   151 		}
   152 	else 
   153 		{
   154 		iHandler->DoCommandL(*iKeyStore, iParam);		
   155 		iState = EDone;		
   156 		}
   157 	}
   158 
   159 	
   160 void CKeyToolEngine::ListL(CKeyToolParameters* aParam)
   161 	{ 
   162 	Cancel();
   163 	iParam = aParam;
   164 	iCurrentAction = EList;
   165 	delete iHandler;
   166 	iHandler = NULL;
   167 	iHandler = CKeytoolList::NewL(iController);	
   168 	InitializeL();
   169 	}
   170 	
   171 void CKeyToolEngine::ImportL(CKeyToolParameters* aParam)
   172 	{ 
   173 	Cancel();
   174 	
   175 	if (!aParam->iDefault)
   176 		{
   177 		iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_KEYFILE);				
   178 		User::Leave(KErrArgument);
   179 		}	
   180 
   181 	iParam = aParam;
   182 	iCurrentAction = EImport;
   183 	delete iHandler; // Reentrant call
   184 	iHandler = NULL;
   185 	iHandler = CKeytoolImport::NewL(iController);	
   186 	InitializeL();
   187 	}
   188 
   189 void CKeyToolEngine::RemoveL(CKeyToolParameters* aParam)
   190 	{ 
   191 	Cancel();
   192 	
   193 	if (!aParam->iDefault)
   194 		{
   195 		iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_REMOVE);				
   196 		User::Leave(KErrArgument);
   197 		}
   198 
   199 	iParam = aParam;
   200 	iCurrentAction = ERemove;
   201 	iHandler = CKeytoolRemove::NewL(iController);	
   202 	InitializeL();
   203 	}
   204 
   205 	
   206 void CKeyToolEngine::DisplayUsageL(CKeyToolParameters* aParam)
   207 	{
   208 	iHandler = CKeytoolUsage::NewL(iController);	
   209 	iHandler->DoCommandL(*iKeyStore, aParam);
   210 	}
   211 
   212 void CKeyToolEngine::ListStoresL(CKeyToolParameters* aParam)
   213 	{ 
   214 	Cancel();
   215 	iParam = aParam;
   216 	iCurrentAction = EList;
   217 	iHandler = CKeyToolListStores::NewL(iController);	
   218 	InitializeL();
   219 	}
   220 
   221 void CKeyToolEngine::SetPolicyL(CKeyToolParameters* aParam)
   222 	{ 
   223 	Cancel();
   224 	
   225 	if (!aParam->iDefault)
   226 		{
   227 		iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_SUSERFAIL);				
   228 		User::Leave(KErrArgument);
   229 		}
   230 
   231 	iParam = aParam;
   232 	iCurrentAction = ESetPolicy;
   233 	iHandler = CKeytoolSetPolicy::NewL(iController);	
   234 	InitializeL();
   235 	}
   236 
   237 #ifdef KEYTOOL
   238 
   239 void CKeyToolEngine::MigrateStoreL(CKeyToolParameters* aParams)
   240 	{
   241 	iParam = aParams;
   242 	iMigrateStoreHandler = CKeytoolMigrateStore::NewL(aParams);	
   243 	iMigrateStoreHandler->DoCommandL();
   244 	}
   245 
   246 #endif // KEYTOOL