os/security/cryptoservices/filebasedcertificateandkeystores/test/keytool/keytool_import.cpp
First public contribution.
2 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
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".
9 * Initial Contributors:
10 * Nokia Corporation - initial contribution.
19 #include <mctkeystore.h>
22 #include <unifiedkeystore.h>
23 #include <securityerr.h>
25 #include "keytool_commands.h"
26 #include "keytool_controller.h"
27 #include "controller.h"
29 #include <keytool.rsg>
31 /*static*/ CKeytoolImport* CKeytoolImport::NewLC(CController* aController)
33 CKeytoolImport* self = new (ELeave) CKeytoolImport(aController);
34 CleanupStack::PushL(self);
39 /*static*/ CKeytoolImport* CKeytoolImport::NewL(CController* aController)
41 CKeytoolImport* self = CKeytoolImport::NewLC(aController);
42 CleanupStack::Pop(self);
46 CKeytoolImport::~CKeytoolImport()
56 void CKeytoolImport::ConstructL()
58 CActiveScheduler::Add(this);
61 void CKeytoolImport::Pkcs8PreprocessorL()
64 CleanupClosePushL(fs);
65 User::LeaveIfError(fs.Connect());
68 CleanupClosePushL(file);
69 TInt r = file.Open(fs, *(iParams->iDefault), EFileRead);
72 iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_OPENFAIL);
81 iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_EMPTYFILE);
82 User::Leave(KErrCorrupt);
85 iKeyData = HBufC8::NewMaxL(fileSize);
86 TPtr8 data(iKeyData->Des());
88 User::LeaveIfError(file.Read(data, fileSize));
90 CleanupStack::PopAndDestroy(&file);
91 CleanupStack::PopAndDestroy(&fs);
95 void CKeytoolImport::DoCommandL(CUnifiedKeyStore& aKeyStore, CKeyToolParameters* aParam)
100 TRAPD(err, Pkcs8PreprocessorL());
103 iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_PKCS8);
107 // keystore index, key data, usage, label, access type, start, end, keyinfo, status
108 // must get proper keystore
109 if (aParam->iKeystoreIndex == -1)
111 aParam->iKeystoreIndex = 0;
113 if (aParam->iKeystoreIndex >= aKeyStore.KeyStoreCount())
115 iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_NOTEXIST);
116 User::Leave(KErrCorrupt);
119 if (!iParams->iLabel)
121 iParams->iLabel = (iParams->iDefault)->AllocL();
124 aKeyStore.ImportKey(iParams->iKeystoreIndex, *iKeyData, iParams->iUsage, *iParams->iLabel,
125 iParams->iAccess, TTime(0), iParams->iEndDate, iKeyInfo, iStatus);
130 void CKeytoolImport::RunL()
132 if (iStatus.Int() != KErrNone)
134 // A problem occured. Handle gracefully.
135 User::Leave(iStatus.Int());
137 iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_IMPORTSUCCESS);
138 iController->DisplayKeyL(*iKeyInfo, ETrue, iParams->iPageWise);
140 CActiveScheduler::Stop();
143 TInt CKeytoolImport::RunError(TInt aError)
145 CActiveScheduler::Stop();
148 case KErrBadPassphrase:
150 TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_PASSPHRASE, iStatus.Int()));
153 case KErrKeyNotWeakEnough:
155 TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_WEAK, iStatus.Int()));
160 TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_USAGE, iStatus.Int()));
165 TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_IMPORT, iStatus.Int()));
172 void CKeytoolImport::DoCancel()
177 CKeytoolImport::CKeytoolImport(CController* aController) : CKeyToolCommand(aController)