os/security/cryptoservices/certificateandkeymgmt/tcertstore/T_unifiedcertstoreadd.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 "t_unifiedcertstoreadd.h"
    20 #include "t_certstoredefs.h"
    21 #include "t_input.h"
    22 #include "t_certstoreout.h"
    23 #include <mctwritablecertstore.h>
    24 
    25 CTestAction* CAddCertificate::NewL(RFs& aFs,
    26 								   CConsoleBase& aConsole, 
    27 								   Output& aOut, 
    28 								   const TTestActionSpec& aTestActionSpec)
    29 	{
    30 	CAddCertificate* self = new(ELeave) CAddCertificate(aFs, aConsole, aOut);
    31 	CleanupStack::PushL(self);
    32 	self->ConstructL(aTestActionSpec);
    33 	CleanupStack::Pop(self);
    34 	return self;
    35 	}
    36 
    37 CAddCertificate::~CAddCertificate()
    38 	{
    39 	delete iCertificate;
    40 	delete iCertificateURL;
    41 	delete iCertificateContent;
    42 	delete iCertificateLabel;
    43 	}
    44 
    45 void CAddCertificate::PerformAction(TRequestStatus& aStatus)
    46 	{
    47 	switch (iState)
    48 		{
    49 		case EAdding:
    50 			{
    51  			if (iNotificationSubscribed)
    52  				{
    53  				if (!iNotifier)
    54  					{
    55  					iNotifier = CCertStoreChangeNotifier::NewL(iNotifierFlag);
    56  					iNotifier->StartNotification();
    57  					}
    58  				iState = ECheckNotification;
    59  				}
    60  			else
    61  				{
    62  				iState = EFinished;
    63  				}
    64 			MCTWritableCertStore& store = UnifiedCertStore().WritableCertStore(iStoreIndex);
    65 			
    66 			TKeyIdentifier* issuerKeyId = &iIssuerKeyId;
    67 			TKeyIdentifier* subjectKeyId = &iSubjectKeyId;
    68 			
    69 			ASSERT(iCertificateLabel);
    70 			
    71 			// Use the Add() with Deletable param if Deletable flag present in test data
    72 			if (iDeletableFlagPresent)
    73 				{
    74 				store.Add(*iCertificateLabel, iCertificateFormat, iOwnerType,
    75 						  subjectKeyId, issuerKeyId, *iCertificateContent, 
    76 						  iDeletable, aStatus);
    77 				}
    78 			// otherwise, use the original Add()
    79 			else 
    80 				{
    81 				store.Add(*iCertificateLabel, iCertificateFormat, iOwnerType,
    82 						  subjectKeyId, issuerKeyId, *iCertificateContent, aStatus);
    83 				}
    84 			}
    85 			break;
    86  		case ECheckNotification:
    87  			{
    88   			iState = EFinished;
    89  			if (iNotifierFlag)
    90 	 			{
    91  				TRequestStatus* status = &aStatus;
    92  				User::RequestComplete(status, KErrNone);
    93 	 			}
    94 			else
    95 				{
    96 				iNotifier->SetCompleteStatus(&aStatus);
    97 				} 			
    98  			break;
    99  			}
   100 		case EFinished:
   101 			{
   102 			if (aStatus == iExpectedResult)
   103 				{
   104 				iResult = ETrue;
   105 				}
   106 			else
   107 				{
   108 				iResult = EFalse;
   109 				}
   110 
   111             if (aStatus != KErrNoMemory)
   112                 {
   113                 iFinished = ETrue;
   114                 }
   115             
   116 			TRequestStatus* status = &aStatus;
   117 			User::RequestComplete(status, aStatus.Int());
   118 			}
   119 			break;
   120 
   121 		default:
   122 			User::Invariant();
   123 			break;
   124 		}
   125 	}
   126 
   127 void CAddCertificate::PerformCancel()
   128 	{
   129 	switch (iState)
   130 		{
   131 	case ECheckNotification:
   132 	case EFinished:	
   133 		{
   134 		MCTWritableCertStore& store = UnifiedCertStore().WritableCertStore(iStoreIndex);
   135 		store.CancelAdd();
   136 		break;
   137 		}
   138 	default:
   139 		break;
   140 		}
   141 	}
   142 
   143 void CAddCertificate::AfterOOMFailure()
   144 	{
   145 	}
   146 
   147 void CAddCertificate::Reset()
   148 	{
   149 	iState = EAdding;
   150 	}
   151 
   152 void CAddCertificate::DoReportAction()
   153 	{
   154 	iOut.writeString(_L("Adding certificate..."));
   155 	iOut.writeNewLine();
   156 	iOut.writeString(_L("\tLabel = "));
   157 	iOut.writeString(*iCertificateLabel);
   158 	iOut.writeNewLine();
   159 	iOut.writeString(_L("\tOwner type = "));
   160 	WriteOwnerType();
   161 	WriteFormat();
   162 	iOut.writeString(_L("\tSubjectKeyId:  "));
   163 	iOut.writeOctetString(iSubjectKeyId);
   164 	iOut.writeNewLine();
   165 	iOut.writeString(_L("\tDeletable = "));
   166 	iDeletable ? iOut.writeString(KTrue) : iOut.writeString(KFalse);
   167 	iOut.writeNewLine();
   168 	iOut.writeNewLine();
   169 	}
   170 
   171 void CAddCertificate::WriteFormat()
   172 	{
   173 	iOut.writeString(_L("\tFormat = "));
   174 	switch (iCertificateFormat)
   175 		{
   176 		case EX509Certificate:
   177 			iOut.writeString(_L("X.509\n"));
   178 			break;
   179 			
   180 		case EWTLSCertificate:
   181 			iOut.writeString(_L("WTLS\n"));
   182 			break;
   183 			
   184 		case EX509CertificateUrl:
   185 			iOut.writeString(_L("X.509 URL\n"));
   186 			break;
   187 			
   188 		case EWTLSCertificateUrl:
   189 			iOut.writeString(_L("WTLS URL\n"));
   190 			break;
   191 			
   192 		default:
   193 			iOut.writeString(_L("Unknown format\n"));
   194 			break;
   195 		}
   196 	}
   197 
   198 void CAddCertificate::WriteOwnerType()
   199 	{
   200 	switch (iOwnerType)
   201 		{
   202 		case ECACertificate:
   203 			iOut.writeString(_L("CA\n"));
   204 			break;
   205 			
   206 		case EUserCertificate:
   207 			iOut.writeString(_L("User"));
   208 			break;
   209 			
   210 		case EPeerCertificate:
   211 			iOut.writeString(_L("Peer"));
   212 			break;
   213 
   214 		default:
   215 			iOut.writeString(_L("Unknown"));
   216 			break;
   217 		}
   218 	}
   219 	
   220 CAddCertificate::CAddCertificate(RFs& aFs, CConsoleBase& aConsole,
   221 								 Output& aOut)
   222 : CSubscriberAction(aFs, aConsole, aOut), iState(EAdding), 
   223   iDeletable(ETrue), iDeletableFlagPresent(EFalse)
   224 	{
   225 	}
   226 
   227 void CAddCertificate::ConstructL(const TTestActionSpec& aTestActionSpec)
   228 	{
   229 	CSubscriberAction::ConstructL(aTestActionSpec);
   230 
   231 	SetCertFormatL(Input::ParseElement(aTestActionSpec.iActionBody, KCertFormatStart));
   232 	SetCertOwnerTypeL(Input::ParseElement(aTestActionSpec.iActionBody, KCertOwnerTypeStart));
   233 	SetCertLabelL(Input::ParseElement(aTestActionSpec.iActionBody, KCertLabelStart));
   234 	SetKeyId(iIssuerKeyId, Input::ParseElement(aTestActionSpec.iActionBody, KIssuerKeyStart));
   235 	SetKeyId(iSubjectKeyId, Input::ParseElement(aTestActionSpec.iActionBody, KSubjectKeyStart));
   236 	SetStoreToUse(Input::ParseElement(aTestActionSpec.iActionBody, KStoreToUseStart));
   237 
   238 	TPtrC8 certFileOrURL = Input::ParseElement(aTestActionSpec.iActionBody, KCertFileStart);
   239 
   240 	SetCertificateContentL(certFileOrURL);
   241 
   242 	if (iCertificateFormat == EX509CertificateUrl ||
   243 		iCertificateFormat == EWTLSCertificateUrl)
   244 		{
   245 		iCertificateURL = certFileOrURL.AllocL();
   246 		}
   247 	else
   248 		{
   249 		ConstructCertL(certFileOrURL);
   250 		}
   251 		
   252 	// check for a possible deletable flag value for the certificate
   253 	TInt err = KErrNone;
   254 	TInt pos = 0;
   255 	const TDesC8& deletableStr = Input::ParseElement(aTestActionSpec.iActionBody, 
   256 														KDeletableStart,
   257 														KDeletableEnd,
   258 														pos,
   259 														err);
   260 
   261 	// set the deletable attribute if a value was found for the certificate
   262 	if (err == KErrNone)
   263 		{
   264 		SetDeletable(deletableStr);
   265 		}
   266 
   267 	// Setting the expected result
   268 	HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length());
   269 	TPtr(result->Des()).Copy(aTestActionSpec.iActionResult);
   270 	Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult);
   271 	CleanupStack::PopAndDestroy(result);
   272 	}
   273 
   274 void CAddCertificate::SetKeyId(TKeyIdentifier& aKeyIdentifier, const TDesC8& aKeyInfo)
   275 	{
   276 	TInt size = aKeyInfo.Length();
   277 	for (TInt i = 0; i < size; i += 2)
   278 		{
   279 		TInt a = (aKeyInfo[i+1] >= 'a') ? (aKeyInfo[i+1] - 'a' + 10) : (aKeyInfo[i+1] - '0');
   280 		TInt b = (aKeyInfo[i] >= 'a') ? (aKeyInfo[i] - 'a' + 10) : (aKeyInfo[i] - '0');
   281 		aKeyIdentifier.Append(a  + b * 16);
   282 		}
   283 	}
   284 
   285 void CAddCertificate::SetCertFormatL(const TDesC8& aFormat)
   286 	{
   287 	if (aFormat == KX509)
   288 		{
   289 		iCertificateFormat = EX509Certificate;
   290 		}
   291 	else if (aFormat == KWTLS)
   292 		{
   293 		iCertificateFormat = EWTLSCertificate;
   294 		}
   295 	else if (aFormat == KX509URL)
   296 		{
   297 		iCertificateFormat = EX509CertificateUrl;
   298 		}
   299 	else if (aFormat == KWTLSURL)
   300 		{
   301 		iCertificateFormat = EWTLSCertificateUrl;
   302 		}
   303 	else if (aFormat == KUnknown)
   304 		{
   305 		iCertificateFormat = EUnknownCertificate;
   306 		}
   307 	else
   308 		{
   309 		iOut.write(_L("Unknown cert format: "));
   310 		iOut.writeString(aFormat);
   311 		iOut.writeNewLine();		   
   312 		User::Leave(KErrArgument);
   313 		}
   314 	}
   315 
   316 void CAddCertificate::SetCertOwnerTypeL(const TDesC8& aOwnerType)
   317 	{
   318 	if (aOwnerType == KCACert)
   319 		{
   320 		iOwnerType = ECACertificate;
   321 		}
   322 	else if (aOwnerType == KUserCert)
   323 		{
   324 		iOwnerType = EUserCertificate;
   325 		}
   326 	else if (aOwnerType == KPeerCert)
   327 		{
   328 		iOwnerType = EPeerCertificate;
   329 		}
   330 	else if (aOwnerType == KUnknown)
   331 		{
   332 		// set dummy bogus owner type
   333 		iOwnerType = static_cast<TCertificateOwnerType>(EPeerCertificate + 1);
   334 		}
   335 	else
   336 		{
   337 		iOut.write(_L("Unknown cert owner type: "));
   338 		iOut.writeString(aOwnerType);
   339 		iOut.writeNewLine();		   
   340 		User::Leave(KErrArgument);
   341 		}
   342 	}
   343 
   344 void CAddCertificate::SetCertLabelL(const TDesC8& aLabel)
   345 	{
   346 	delete iCertificateLabel;
   347 	iCertificateLabel = NULL;
   348 	iCertificateLabel = HBufC::NewL(aLabel.Length());
   349 	TPtr ptr = iCertificateLabel->Des();
   350 	ptr.Copy(aLabel);
   351 	}
   352 
   353 void CAddCertificate::SetStoreToUse(const TDesC8& aStoreToUse)
   354 	{
   355 	TLex8 lex(aStoreToUse);
   356 	lex.Val(iStoreIndex);
   357 	}
   358 
   359 void CAddCertificate::SetCertificateContentL(const TDesC8& aFileName)
   360 	{
   361 	TFileName fileName;
   362 	fileName.Copy(aFileName);
   363 	RFs fs; 
   364 	User::LeaveIfError(fs.Connect());
   365 	CleanupClosePushL(fs);
   366 	__ASSERT_DEBUG(!iCertificateContent, User::Panic(_L("CAddCertificate"), 1));
   367 	TRAPD(err, iCertificateContent = Input::ReadFileL(fileName, fs));
   368 	if (err != KErrNone)
   369 		{
   370 		iConsole.Printf(_L("Error reading file : "));
   371 		iConsole.Printf(fileName);
   372 		iConsole.Printf(_L("\n"));
   373 		User::Leave(err);
   374 		}
   375 	CleanupStack::PopAndDestroy();	// fs
   376 	}
   377 
   378 void CAddCertificate::SetDeletable(const TDesC8& aDeletable)
   379 	{
   380 	iDeletableFlagPresent = ETrue;
   381 	if (aDeletable.Compare(KTrue)==0)
   382 		{
   383 		iDeletable = ETrue;
   384 		}
   385 	else
   386 		{
   387 		iDeletable = EFalse;
   388 		}
   389 	}
   390 
   391 void CAddCertificate::ConstructCertL(const TDesC8& aCert)
   392 	{
   393 	TFileName filename;
   394 	filename.Copy(aCert);
   395 	RFs fs; 
   396 	User::LeaveIfError(fs.Connect());
   397 	CleanupClosePushL(fs);
   398 	HBufC8* certBuf = 0;
   399 	TRAPD(err, certBuf = Input::ReadFileL(filename, fs));
   400 	if (err != KErrNone)
   401 		{
   402 		iConsole.Printf(_L("Error reading file : "));
   403 		iConsole.Printf(filename);
   404 		iConsole.Printf(_L("\n"));
   405 		User::Leave(err);
   406 		}
   407 	CleanupStack::PushL(certBuf);
   408 	switch (iCertificateFormat)
   409 		{
   410 		case EX509Certificate:
   411 			iCertificate = CX509Certificate::NewL(*certBuf);
   412 			break;
   413 			
   414 		case EWTLSCertificate:
   415 			iCertificate = CWTLSCertificate::NewL(*certBuf);
   416 			break;
   417 			
   418 		default:
   419 			// Unknown format - do nothing
   420 			break;
   421 		}
   422 	CleanupStack::PopAndDestroy(2);
   423 	}
   424 
   425 void CAddCertificate::DoCheckResult(TInt aError)
   426 	{
   427 	
   428 	if (iFinished)
   429 		{
   430 		if (iResult )
   431 			{
   432 			if (iExpectedResult == KErrNone )
   433 				{
   434 				iConsole.Write(_L("\tcertificate added successfully\n"));
   435 				iOut.writeString(_L("\tcertificate added successfully"));
   436 				}
   437 			else	
   438 				{
   439 				iConsole.Write(_L("\tcertificate not added.\n"));
   440 				iOut.writeString(_L("\tcertificate not added."));
   441 				}
   442 			iOut.writeNewLine();
   443 			iOut.writeNewLine();
   444 			}
   445 		else if( !iResult)
   446 			{
   447 			if(iExpectedResult == KErrNone )
   448 				{
   449 				iConsole.Write(_L("\tcertificate not added\n"));
   450 				iOut.writeString(_L("\tcertificate not added"));	
   451 				}
   452 			else
   453 				{
   454 				iConsole.Write(_L("\tcertificate should not be added\n"));
   455 				iOut.writeString(_L("\tcertificate should not be added"));
   456 				}
   457 			iOut.writeNewLine();
   458 			iOut.writeString(_L("\t"));
   459 			iOut.writeError(aError);
   460 			if (aError == KErrBadName)
   461 				{
   462 				iOut.writeString(_L(" Check that the label is unique"));
   463 				}
   464 			iOut.writeNewLine();
   465 			iOut.writeNewLine();
   466 			}
   467 		}	
   468 	}
   469 //////////////////////////////////////////////////////////
   470 //	Key import, from keystore for adding user certificates
   471 //////////////////////////////////////////////////////////
   472 
   473 CTestAction* CImportKey::NewL(RFs& aFs, 
   474 							CConsoleBase& aConsole, 
   475 							Output& aOut,
   476 							const TTestActionSpec& aTestActionSpec)
   477 {
   478 	CTestAction* self = CImportKey::NewLC(aFs, aConsole, aOut, aTestActionSpec);
   479 	CleanupStack::Pop(self);
   480 	return self;
   481 }
   482 
   483 CTestAction* CImportKey::NewLC(RFs& aFs,
   484 							CConsoleBase& aConsole, 
   485 							Output& aOut,
   486 							const TTestActionSpec& aTestActionSpec)
   487 {
   488 	CImportKey* self = new (ELeave) CImportKey(aFs, aConsole, aOut);
   489 	CleanupStack::PushL(self);
   490 	self->ConstructL(aTestActionSpec);
   491 	return self;
   492 }
   493 
   494 CImportKey::~CImportKey()
   495 {
   496 	delete iLabel;
   497 	delete iKeyData;
   498 	if (iKeyInfo)
   499 		{
   500 		iKeyInfo->Release();
   501 		}
   502 	delete iUnifiedKeyStore;
   503 	iFs.Close();
   504 }
   505 
   506 CImportKey::CImportKey(RFs& aFs, CConsoleBase& aConsole, Output& aOut)
   507 :	CCertStoreTestAction(aFs, aConsole, aOut),
   508 	iState(EInitialise)
   509 {}
   510 
   511 
   512 void CImportKey::ConstructL(const TTestActionSpec& aTestActionSpec)
   513 {
   514 	User::LeaveIfError(iFs.Connect());
   515 
   516 	CCertStoreTestAction::ConstructL(aTestActionSpec);
   517 
   518 	TInt err = KErrNone;
   519 	TInt pos = 0;
   520 	SetKeyDataFileL(Input::ParseElement(aTestActionSpec.iActionBody, KImportDataFile, KImportDataFileEnd, pos, err));
   521 	for (;SetKeyUsage(Input::ParseElement(aTestActionSpec.iActionBody, KKeyUsageStart, KKeyUsageEnd, pos, err));)
   522 		;
   523 	
   524 	SetKeyLabel(Input::ParseElement(aTestActionSpec.iActionBody, KKeyLabelStart, KKeyLabelEnd, pos, err));
   525 	for (;SetKeyAccessType(Input::ParseElement(aTestActionSpec.iActionBody, KKeyAccessTypeStart, KKeyAccessTypeEnd, pos, err));)
   526 		;
   527 	
   528 	SetKeyPassphrase(Input::ParseElement(aTestActionSpec.iActionBody, KKeyPassphraseStart, KKeyPassphraseEnd, pos, err));
   529 		
   530 	pos = 0;
   531 	
   532 	HBufC* result = HBufC::NewLC(aTestActionSpec.iActionResult.Length());
   533 	TPtr(result->Des()).Copy(aTestActionSpec.iActionResult);
   534 	Input::GetExpectedResultL(Input::ParseElement(*result, KReturnStart, KReturnEnd), iExpectedResult);
   535 	CleanupStack::PopAndDestroy(result);
   536 }
   537 
   538 TBool CImportKey::SetKeyUsage(const TDesC8& aKeyUsage)
   539 	{
   540 	TBool ret = ETrue;
   541 	if (aKeyUsage.Compare(KAllKeyUsages)==0)
   542 		iUsage = EPKCS15UsageAll;
   543 	else if (aKeyUsage.Compare(KAllKeyUsagesButNR)==0)
   544 		iUsage |= (TKeyUsagePKCS15)(EPKCS15UsageSign) |
   545 			(TKeyUsagePKCS15)(EPKCS15UsageSignRecover) |
   546 			(TKeyUsagePKCS15)(EPKCS15UsageDecrypt);
   547 	else if (aKeyUsage.Compare(KDSAUsage)==0)
   548 		iUsage |= (TKeyUsagePKCS15)(EPKCS15UsageSign) |
   549 			(TKeyUsagePKCS15)(EPKCS15UsageSignRecover);
   550 	else if (aKeyUsage.Compare(KDerive)==0)
   551 		iUsage |= EPKCS15UsageDerive;
   552 	else if (aKeyUsage.Compare(KSign)==0)
   553 		iUsage |= EPKCS15UsageSign;
   554 	else if (aKeyUsage.Compare(KSignRecover)==0)
   555 		iUsage |= EPKCS15UsageSignRecover;
   556 	else if (aKeyUsage.Compare(KDecrypt)==0)
   557 		iUsage |= EPKCS15UsageDecrypt;
   558 	else if (aKeyUsage.Compare(KNR)==0)
   559 		iUsage |= EPKCS15UsageNonRepudiation;
   560 	else if (aKeyUsage.Compare(KEncipherAndSign)==0)
   561 		iUsage |= (TKeyUsagePKCS15)(EPKCS15UsageSign) |
   562 			(TKeyUsagePKCS15)(EPKCS15UsageSignRecover) |
   563 			(TKeyUsagePKCS15)(EPKCS15UsageUnwrap);
   564 	else
   565 		ret = EFalse;
   566 	return ret;
   567 	}
   568 
   569 void CImportKey::SetKeyLabel(const TDesC8& aKeyLabel)
   570 {
   571 	iLabel = HBufC::NewMax(aKeyLabel.Size());
   572 	if (iLabel)
   573 	{
   574 		TPtr theLabel(iLabel->Des());
   575 		theLabel.FillZ();
   576 		theLabel.Copy(aKeyLabel);
   577 	}
   578 }
   579 
   580 
   581 void CImportKey::SetKeyPassphrase(const TDesC8& aPassphrase)
   582 	{
   583 	// If the passphrase is empty, then use "clanger" by default.
   584 	_LIT8(KDefaultPassphrase, "clanger");
   585 	TPtrC8 phrase(KDefaultPassphrase());
   586 	if (aPassphrase.Length())
   587 		{
   588 		phrase.Set(aPassphrase);
   589 		}
   590 
   591 
   592 	RFs fs;
   593 	RFile file;
   594 	fs.Connect();
   595 	
   596 	// Write the passphrase straight to the file.
   597 	TDriveUnit sysDrive (fs.GetSystemDrive());
   598 	TBuf<24> fileName (sysDrive.Name());
   599 	fileName.Append(_L("\\password.txt"));
   600 	
   601 	file.Replace(fs, fileName, EFileWrite);
   602 	file.Write(phrase);
   603 	file.Close();
   604 	fs.Close();
   605 	}
   606 
   607 void CImportKey::SetKeyDataFileL(const TDesC8& aDes)
   608 {	
   609 //	Now the filename itself
   610 	TFileName fileName;
   611 	fileName.FillZ();
   612 	fileName.Copy(aDes);
   613 	
   614 	TDriveUnit sysDrive (RFs::GetSystemDrive());
   615 	TBuf<64> buf(sysDrive.Name());
   616 	buf.Append(_L("\\tcertstore\\data\\"));
   617 	buf.Append(fileName);
   618 		
   619 	RFile file;
   620 	TInt r = file.Open(iFs, buf, EFileRead);
   621 	if ( (r==KErrNotFound) || (r==KErrPathNotFound) )
   622 	{//	Not on c:, try z:
   623 		buf[0] = 'z';
   624 		r = file.Open(iFs, buf, EFileRead);
   625 	}
   626 
   627 	User::LeaveIfError(r);
   628 
   629 	CleanupClosePushL(file);
   630 
   631 	TInt fileSize = 0;
   632 	User::LeaveIfError(file.Size(fileSize));
   633 
   634 	if (fileSize > 0)
   635 	{
   636 		iKeyData = HBufC8::NewMaxL(fileSize);	
   637 		TPtr8 data(iKeyData->Des());
   638 		data.FillZ();
   639 		User::LeaveIfError(file.Read(data, fileSize));
   640 		CleanupStack::Pop(1);
   641 	}
   642 
   643 	file.Close();
   644 }
   645 
   646 TBool CImportKey::SetKeyAccessType(const TDesC8& aKeyAccessType)
   647 	{
   648 	TBool ret = ETrue;
   649 	if (aKeyAccessType.Compare(KExtractable)==0)
   650 		{
   651 		iAccessType |= CCTKeyInfo::EExtractable;
   652 		}
   653 	else if (aKeyAccessType.Compare(KSensitive)==0)
   654 		{
   655 		iAccessType |= CCTKeyInfo::ESensitive;
   656 		}
   657 	else if (aKeyAccessType.Compare(KAlwaysSensitive)==0)
   658 		{
   659 		iAccessType |= CCTKeyInfo::EAlwaysSensitive;
   660 		}
   661 	else if (aKeyAccessType.Compare(KNeverExtractable)==0)
   662 		{
   663 		iAccessType |= CCTKeyInfo::ENeverExtractable;
   664 		}
   665 	else if (aKeyAccessType.Compare(KLocal)==0)
   666 		{
   667 		iAccessType |= CCTKeyInfo::ELocal;
   668 		}
   669 	else
   670 		ret = EFalse;
   671 	return ret;
   672 	}
   673 
   674 void CImportKey::PerformAction(TRequestStatus& aStatus)
   675 {
   676 	TDriveUnit sysDrive (RFs::GetSystemDrive());
   677 	switch (iState)
   678 	{
   679 	case EInitialise:
   680 		{
   681 		if (iKeyInfo != NULL)
   682 			{
   683 			iKeyInfo->Release();
   684 			iKeyInfo = NULL;
   685 			}
   686 
   687 		// Delete t_secdlg files - this will then always answer "clanger" for the passphrase
   688 		    
   689 		TInt result;
   690 		TBuf<24> datFile(sysDrive.Name());
   691 		datFile.Append(_L("\\t_secdlg_in.dat"));
   692 		result = iFs.Delete(datFile);
   693 		
   694 		if (result != KErrNone && result != KErrNotFound)
   695 			{
   696 			TRequestStatus* status = &aStatus;
   697 			User::RequestComplete(status, result);
   698 			return;
   699 			}
   700 			
   701 		datFile.Copy(sysDrive.Name());
   702 		datFile.Append(_L("\\t_secdlg_out.dat"));
   703 		result = iFs.Delete(datFile);
   704 		
   705 		if (result != KErrNone && result != KErrNotFound)
   706 			{
   707 			TRequestStatus* status = &aStatus;
   708 			User::RequestComplete(status, result);
   709 			return;
   710 			}
   711 		
   712 		TRAP(result, iUnifiedKeyStore = CUnifiedKeyStore::NewL(iFs));
   713 		if ( (result==KErrNone) && (iUnifiedKeyStore) )
   714 			{
   715 				iUnifiedKeyStore->Initialize(aStatus);
   716 				iState = EImportKey;
   717 			}
   718 		else
   719 			{
   720 				aStatus = result;
   721 				iState = EFinished;
   722 			}
   723 		}
   724 		break;
   725 	case EImportKey:
   726 		{
   727 			if (KErrNone==aStatus.Int())
   728 			{
   729 			//	Currently uses the first store, change to check the script for a specific store
   730 				iUnifiedKeyStore->ImportKey(0, iKeyData->Des(), iUsage, *iLabel, iAccessType,
   731 											TTime(0), TTime(0), iKeyInfo, aStatus);			
   732 			}
   733 			else
   734 				{
   735 				// Errors get passed to next state
   736 				TRequestStatus* status = &aStatus;
   737 				User::RequestComplete(status, aStatus.Int());
   738 				}
   739 
   740 			iState = EFinished;
   741 		}
   742 		
   743 		break;
   744 		
   745 		case EFinished:
   746 		{
   747 			TRequestStatus* status = &aStatus;
   748 			User::RequestComplete(status, aStatus.Int());
   749 			if ( (aStatus == iExpectedResult) || (aStatus==KErrAlreadyExists) )
   750 			{
   751 				iResult = ETrue;
   752 			}
   753 			else
   754 			{
   755 				iResult = EFalse;
   756 			}
   757 			
   758 			iFinished = ETrue;
   759 		}
   760 		break;
   761 	}
   762 }
   763 
   764 void CImportKey::PerformCancel()
   765 {//	To do when test harness cancel comes back.  Currently cancel testing
   766 //	is performed in RunL with a set of flags and a separate active object
   767 }
   768 
   769 void CImportKey::Reset()
   770 {}
   771 
   772 void CImportKey::DoReportAction()
   773 {
   774 	_LIT(KImporting, "Importing key from keystore...");
   775 	iOut.writeString(KImporting);
   776 	TPtr theLabel(iLabel->Des());
   777 	iOut.writeString(theLabel);
   778 	iOut.writeNewLine();
   779 }
   780 
   781 
   782 void CImportKey::DoCheckResult(TInt aError)
   783 {
   784 	
   785 	if (iFinished)
   786 	{
   787 		if (aError == KErrNone)
   788 		{
   789 			_LIT(KSuccessful, "Key imported successfully\n");
   790 			iConsole.Write(KSuccessful);
   791 			iOut.writeString(KSuccessful);
   792 			iOut.writeNewLine();
   793 			iOut.writeNewLine();
   794 		}
   795 		else
   796 		{
   797 			if ( (aError!=iExpectedResult) && (aError!=KErrAlreadyExists) )
   798 			{
   799 				_LIT(KFailed, "!!!Key import failure!!!\n");
   800 				iConsole.Write(KFailed);
   801 				iOut.writeString(KFailed);
   802 			}
   803 			
   804 			iOut.writeNewLine();
   805 			iOut.writeNewLine();
   806 		}
   807 	}
   808 }
   809