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