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