cryptoservices/filebasedcertificateandkeystores/test/keytool/keytool_import.cpp
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 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <mctkeystore.h>
       
    20 #include <asn1dec.h>
       
    21 #include <asnpkcs.h>
       
    22 #include <unifiedkeystore.h>
       
    23 #include <securityerr.h>
       
    24 
       
    25 #include "keytool_commands.h"
       
    26 #include "keytool_controller.h"
       
    27 #include "controller.h"
       
    28 
       
    29 #include <keytool.rsg>
       
    30 
       
    31 /*static*/ CKeytoolImport* CKeytoolImport::NewLC(CController* aController)
       
    32 	{
       
    33 	CKeytoolImport* self = new (ELeave) CKeytoolImport(aController);
       
    34 	CleanupStack::PushL(self);
       
    35 	self->ConstructL();
       
    36 	return self;	
       
    37 	}
       
    38 
       
    39 /*static*/ CKeytoolImport* CKeytoolImport::NewL(CController* aController)
       
    40 	{
       
    41 	CKeytoolImport* self = CKeytoolImport::NewLC(aController);
       
    42 	CleanupStack::Pop(self);
       
    43 	return self;	
       
    44 	}
       
    45 
       
    46 CKeytoolImport::~CKeytoolImport()
       
    47 	{
       
    48 	Cancel();	
       
    49 	delete iKeyData;
       
    50 	if (iKeyInfo != NULL)
       
    51 		{
       
    52 		iKeyInfo->Release();
       
    53 		}	
       
    54 	}
       
    55 
       
    56 void CKeytoolImport::ConstructL()
       
    57 	{
       
    58 	CActiveScheduler::Add(this);		
       
    59 	}
       
    60 
       
    61 void CKeytoolImport::Pkcs8PreprocessorL()
       
    62 	{
       
    63 	RFs fs;
       
    64 	CleanupClosePushL(fs);
       
    65 	User::LeaveIfError(fs.Connect());	
       
    66 
       
    67 	RFile file;
       
    68 	CleanupClosePushL(file);	
       
    69 	TInt r = file.Open(fs, *(iParams->iDefault), EFileRead);
       
    70 	if (r != KErrNone)
       
    71 		{
       
    72 		iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_OPENFAIL);				
       
    73 		User::Leave(r);
       
    74 		}
       
    75         
       
    76 	TInt fileSize = 0;
       
    77 	file.Size(fileSize);
       
    78 
       
    79 	if (fileSize <= 0)
       
    80 	{
       
    81 		iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_EMPTYFILE);	
       
    82 		User::Leave(KErrCorrupt);
       
    83 	}
       
    84 
       
    85 	iKeyData = HBufC8::NewMaxL(fileSize);
       
    86 	TPtr8 data(iKeyData->Des());
       
    87 	data.FillZ();
       
    88 	User::LeaveIfError(file.Read(data, fileSize));
       
    89 	
       
    90 	CleanupStack::PopAndDestroy(&file); 
       
    91 	CleanupStack::PopAndDestroy(&fs); 
       
    92 	}
       
    93 
       
    94 
       
    95 void CKeytoolImport::DoCommandL(CUnifiedKeyStore& aKeyStore, CKeyToolParameters* aParam)
       
    96 	{
       
    97 	Cancel();	
       
    98 	iParams = aParam;
       
    99 	
       
   100 	TRAPD(err, Pkcs8PreprocessorL());
       
   101 	if (err != KErrNone)
       
   102 		{
       
   103 		iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_PKCS8);		
       
   104 		User::Leave(err);
       
   105 		}
       
   106 			
       
   107 	// keystore index, key data, usage, label, access type, start, end, keyinfo, status
       
   108 	// must get proper keystore
       
   109 	if (aParam->iKeystoreIndex == -1)
       
   110 		{
       
   111 		aParam->iKeystoreIndex = 0;
       
   112 		}
       
   113 	if (aParam->iKeystoreIndex >= aKeyStore.KeyStoreCount())
       
   114 		{
       
   115 		iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_NOTEXIST);	
       
   116 		User::Leave(KErrCorrupt);
       
   117 		}
       
   118 
       
   119 	if (!iParams->iLabel)
       
   120 		{
       
   121 		iParams->iLabel	= (iParams->iDefault)->AllocL();
       
   122 		}
       
   123 	
       
   124 	aKeyStore.ImportKey(iParams->iKeystoreIndex, *iKeyData, iParams->iUsage, *iParams->iLabel, 
       
   125 						iParams->iAccess, TTime(0), iParams->iEndDate, iKeyInfo, iStatus);
       
   126 
       
   127 	SetActive();
       
   128 	}
       
   129 
       
   130 void CKeytoolImport::RunL()
       
   131 	{	
       
   132 	if (iStatus.Int() != KErrNone)
       
   133 		{
       
   134 		// A problem occured. Handle gracefully.
       
   135 		User::Leave(iStatus.Int());
       
   136 		}
       
   137 	iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_IMPORTSUCCESS);
       
   138 	iController->DisplayKeyL(*iKeyInfo, ETrue, iParams->iPageWise);
       
   139 
       
   140 	CActiveScheduler::Stop();		
       
   141 	}
       
   142 
       
   143 TInt CKeytoolImport::RunError(TInt aError)
       
   144 	{
       
   145 	CActiveScheduler::Stop();
       
   146 	switch (aError)
       
   147 		{
       
   148 		case KErrBadPassphrase:
       
   149 			{
       
   150 			TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_PASSPHRASE, iStatus.Int()));
       
   151 			}
       
   152 			break;
       
   153 		case KErrKeyNotWeakEnough:
       
   154 			{
       
   155 			TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_WEAK, iStatus.Int()));			
       
   156 			}
       
   157 			break;			
       
   158 		case KErrKeyUsage:
       
   159 			{
       
   160 			TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_USAGE, iStatus.Int()));						
       
   161 			}
       
   162 			break;
       
   163 		default:
       
   164 			{
       
   165 			TRAP_IGNORE(iController->DisplayLocalisedMsgL(R_KEYTOOL_ERR_IMPORT, iStatus.Int()));			
       
   166 			}
       
   167 		}	
       
   168 	return KErrNone;
       
   169 	}
       
   170 
       
   171 
       
   172 void CKeytoolImport::DoCancel()
       
   173 	{
       
   174 	
       
   175 	}
       
   176 	
       
   177 CKeytoolImport::CKeytoolImport(CController* aController) : CKeyToolCommand(aController)
       
   178 	{	
       
   179 	}