os/security/cryptoservices/certificateandkeymgmt/tadditionalstores/Test1certstore.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) 1998-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 "test1certstore.h"
sl@0
    20
#include "tadditionalstoremappings.h"
sl@0
    21
#include <signed.h>
sl@0
    22
#include <x509cert.h>
sl@0
    23
#include <wtlscert.h>
sl@0
    24
#include <x509keys.h>
sl@0
    25
#include <ecom/implementationproxy.h>
sl@0
    26
sl@0
    27
sl@0
    28
_LIT(KName1, "Test store 1");
sl@0
    29
_LIT(KName2, "Test store 2");
sl@0
    30
sl@0
    31
sl@0
    32
//////////////////////////////////////////////////////////////////////////////////////////
sl@0
    33
//CFileCertStore
sl@0
    34
/////////////////////////////////////////////////////////////////////////////////////////
sl@0
    35
sl@0
    36
CTest1CertStore* CTest1CertStore::NewL(RFs& aFs,
sl@0
    37
									   CTest1CertStoreToken& aToken, 
sl@0
    38
									   const TDesC& aFileName,
sl@0
    39
									   TFileMode aMode)
sl@0
    40
	{
sl@0
    41
	CTest1CertStore* self = new(ELeave) CTest1CertStore(aToken, aFs);
sl@0
    42
	CleanupStack::PushL(self);
sl@0
    43
	self->ConstructL(aFileName, aMode);
sl@0
    44
	CleanupStack::Pop(self);
sl@0
    45
	return self;
sl@0
    46
	}
sl@0
    47
sl@0
    48
MCTToken& CTest1CertStore::Token()
sl@0
    49
	{
sl@0
    50
	return iToken;
sl@0
    51
	}
sl@0
    52
sl@0
    53
void CTest1CertStore::DoRelease()
sl@0
    54
	{
sl@0
    55
	if (iToken.Label() == KName1)
sl@0
    56
		{
sl@0
    57
		iToken.iRefCountInterface--;
sl@0
    58
		if (!iToken.iRefCountInterface)
sl@0
    59
			{
sl@0
    60
			delete this;
sl@0
    61
			}
sl@0
    62
		}
sl@0
    63
	else if (iToken.Label() == KName2)
sl@0
    64
		{
sl@0
    65
		iToken.iRefCountInterface2--;
sl@0
    66
		if (!iToken.iRefCountInterface2)
sl@0
    67
			{
sl@0
    68
			delete this;
sl@0
    69
			}
sl@0
    70
		}
sl@0
    71
	}
sl@0
    72
sl@0
    73
void CTest1CertStore::List(RMPointerArray<CCTCertInfo>& aCertInfos,
sl@0
    74
						  const CCertAttributeFilter& aFilter,
sl@0
    75
						  TRequestStatus& aStatus)
sl@0
    76
	{
sl@0
    77
	iOriginalRequestStatus = &aStatus;
sl@0
    78
	aStatus = KRequestPending;
sl@0
    79
	
sl@0
    80
	iCertInfos = &aCertInfos;
sl@0
    81
	iFilter = &aFilter;
sl@0
    82
sl@0
    83
	//Iinitialize the ketstore and then list the key info
sl@0
    84
	if (iFilter->iKeyUsage != EX509UsageAll)
sl@0
    85
		{
sl@0
    86
		// We have to initialize the unified key store
sl@0
    87
		TRAPD(err, iUnifiedKeyStore = CUnifiedKeyStore::NewL(iFs));
sl@0
    88
		if (err != KErrNone)
sl@0
    89
			{
sl@0
    90
			User::RequestComplete(iOriginalRequestStatus, err);
sl@0
    91
			iOriginalRequestStatus = 0;
sl@0
    92
			}
sl@0
    93
		else
sl@0
    94
			{
sl@0
    95
			iUnifiedKeyStore->Initialize(iStatus);
sl@0
    96
			iState = EGetKeyInfos;
sl@0
    97
			SetActive();
sl@0
    98
			}
sl@0
    99
		}
sl@0
   100
	else
sl@0
   101
		{
sl@0
   102
		iState = EList;
sl@0
   103
		TRequestStatus* status = &iStatus;
sl@0
   104
		User::RequestComplete(status, KErrNone);
sl@0
   105
		SetActive();
sl@0
   106
		}
sl@0
   107
	}
sl@0
   108
sl@0
   109
void CTest1CertStore::CancelList()
sl@0
   110
	{
sl@0
   111
	Cancel();
sl@0
   112
	}
sl@0
   113
sl@0
   114
void CTest1CertStore::GetCert(CCTCertInfo*& aCertInfo,
sl@0
   115
							 const TCTTokenObjectHandle& aHandle,
sl@0
   116
							 TRequestStatus& aStatus)
sl@0
   117
	{
sl@0
   118
	TInt err = KErrNotFound;
sl@0
   119
	if ((aHandle.iTokenHandle == iToken.Handle()) &&
sl@0
   120
		(aHandle.iObjectId < iCerts->Count()))
sl@0
   121
		{
sl@0
   122
		TRAP(err, aCertInfo = CCTCertInfo::NewL(iCerts->Entry(aHandle.iObjectId)));
sl@0
   123
		}
sl@0
   124
	TRequestStatus* status = &aStatus;
sl@0
   125
	User::RequestComplete(status, err);
sl@0
   126
	}
sl@0
   127
sl@0
   128
void CTest1CertStore::CancelGetCert()
sl@0
   129
	{
sl@0
   130
	// Nothing to do because GetCert is not asynchronous.
sl@0
   131
	}
sl@0
   132
sl@0
   133
void CTest1CertStore::Applications(const CCTCertInfo& aCertInfo,
sl@0
   134
								  RArray<TUid>& aApplications,
sl@0
   135
								  TRequestStatus& aStatus)
sl@0
   136
	{
sl@0
   137
	TInt err = KErrNone;
sl@0
   138
	TInt index = iCerts->Index(aCertInfo);
sl@0
   139
	if (index != KErrNotFound)
sl@0
   140
		{
sl@0
   141
		const RArray<TUid>& apps = iCerts->Mapping(index)->CertificateApps();
sl@0
   142
		TInt end = apps.Count();
sl@0
   143
		for (TInt i = 0; (i < end) && (err == KErrNone); i++)
sl@0
   144
			{
sl@0
   145
			err = aApplications.Append(apps[i]);
sl@0
   146
			}
sl@0
   147
		}
sl@0
   148
	else
sl@0
   149
		{
sl@0
   150
		err = index;
sl@0
   151
		}
sl@0
   152
	if (err != KErrNone)
sl@0
   153
		{
sl@0
   154
		aApplications.Reset();
sl@0
   155
		}
sl@0
   156
	TRequestStatus* status = &aStatus;
sl@0
   157
	User::RequestComplete(status, err);
sl@0
   158
	}
sl@0
   159
sl@0
   160
void CTest1CertStore::CancelApplications()
sl@0
   161
	{
sl@0
   162
	}
sl@0
   163
sl@0
   164
void CTest1CertStore::IsApplicable(const CCTCertInfo& aCertInfo,
sl@0
   165
								  TUid aApplication, 
sl@0
   166
								  TBool& aIsApplicable,
sl@0
   167
								  TRequestStatus& aStatus)
sl@0
   168
	{
sl@0
   169
	TInt index = iCerts->Index(aCertInfo);
sl@0
   170
	if (index != KErrNotFound)
sl@0
   171
		{
sl@0
   172
		const RArray<TUid>& apps = iCerts->Mapping(index)->CertificateApps();
sl@0
   173
		TInt end = apps.Count();
sl@0
   174
		TInt i;
sl@0
   175
		for (i = 0; i < end; i++)
sl@0
   176
			{
sl@0
   177
			if (apps[i] == aApplication)
sl@0
   178
				{
sl@0
   179
				break;
sl@0
   180
				}
sl@0
   181
			}
sl@0
   182
		if (i == end)
sl@0
   183
			{
sl@0
   184
			aIsApplicable = EFalse;
sl@0
   185
			}
sl@0
   186
		else
sl@0
   187
			{
sl@0
   188
			aIsApplicable = ETrue;
sl@0
   189
			}
sl@0
   190
		index = KErrNone;
sl@0
   191
		}
sl@0
   192
	
sl@0
   193
	TRequestStatus* status = &aStatus;
sl@0
   194
	User::RequestComplete(status, index);
sl@0
   195
	}
sl@0
   196
sl@0
   197
void CTest1CertStore::CancelIsApplicable()
sl@0
   198
	{
sl@0
   199
	// Nothing to do because IsApplicable is not asynchronous.
sl@0
   200
	}
sl@0
   201
sl@0
   202
void CTest1CertStore::Trusted(const CCTCertInfo& aCertInfo,
sl@0
   203
							 TBool& aTrusted, 
sl@0
   204
							 TRequestStatus& aStatus)
sl@0
   205
	{	
sl@0
   206
	TInt index = iCerts->Index(aCertInfo);
sl@0
   207
	if (index != KErrNotFound)
sl@0
   208
		{
sl@0
   209
		aTrusted = iCerts->Mapping(index)->Trusted();
sl@0
   210
		index = KErrNone;
sl@0
   211
		}
sl@0
   212
	
sl@0
   213
	TRequestStatus* status = &aStatus;
sl@0
   214
	User::RequestComplete(status, index);
sl@0
   215
	}
sl@0
   216
sl@0
   217
void CTest1CertStore::CancelTrusted()
sl@0
   218
	{
sl@0
   219
	// Nothing to do because Trusted is not asynchronous.
sl@0
   220
	}
sl@0
   221
sl@0
   222
void CTest1CertStore::Retrieve(const CCTCertInfo& aCertInfo,
sl@0
   223
							  TDes8& aCertificate,
sl@0
   224
							  TRequestStatus& aStatus)
sl@0
   225
	{
sl@0
   226
#ifdef CERTSTORE_SOFTWARE_ASYNCH
sl@0
   227
	// perform an asynchronous retrieval of the certificate
sl@0
   228
	iOriginalRequestStatus = &aStatus;
sl@0
   229
	aStatus = KRequestPending;
sl@0
   230
sl@0
   231
	iAsynchCertInfo = &aCertInfo;
sl@0
   232
	iAsynchCertificate = &aCertificate;
sl@0
   233
sl@0
   234
	iState = ERetrieve;
sl@0
   235
	TRequestStatus* status = &iStatus;
sl@0
   236
	User::RequestComplete(status, KErrNone);
sl@0
   237
	SetActive();
sl@0
   238
#else
sl@0
   239
	RetrieveNow(aCertInfo,aCertificate,aStatus);
sl@0
   240
#endif
sl@0
   241
	}
sl@0
   242
sl@0
   243
void CTest1CertStore::RetrieveNow(const CCTCertInfo& aCertInfo,
sl@0
   244
							  TDes8& aCertificate,
sl@0
   245
							  TRequestStatus& aStatus)
sl@0
   246
	{
sl@0
   247
	TRequestStatus* status = &aStatus;
sl@0
   248
	TInt err;
sl@0
   249
	TInt index = iCerts->Index(aCertInfo);
sl@0
   250
	if (index == KErrNotFound)
sl@0
   251
		{
sl@0
   252
		err = KErrNotFound;
sl@0
   253
		}
sl@0
   254
	else
sl@0
   255
		{
sl@0
   256
		CFileCertStoreMapping* mapping = NULL;
sl@0
   257
		mapping = iCerts->Mapping(index);
sl@0
   258
		err = index;
sl@0
   259
		if (mapping)
sl@0
   260
			{
sl@0
   261
			TRAP(err, DoLoadL(aCertificate, *mapping));
sl@0
   262
			}
sl@0
   263
		}
sl@0
   264
	User::RequestComplete(status, err);
sl@0
   265
	}
sl@0
   266
sl@0
   267
void CTest1CertStore::CancelRetrieve()
sl@0
   268
	{
sl@0
   269
	}
sl@0
   270
sl@0
   271
sl@0
   272
void CTest1CertStore::Capabilities(const CCTCertInfo& /*aCertInfo*/, TCapabilitySet& /*aCapbilitiesOut*/,
sl@0
   273
								  TRequestStatus& aStatus)
sl@0
   274
	{
sl@0
   275
	// currently not supported
sl@0
   276
	TRequestStatus* status = &aStatus;
sl@0
   277
	User::RequestComplete(status, KErrNotSupported);
sl@0
   278
	}
sl@0
   279
sl@0
   280
void CTest1CertStore::CancelCapabilities()
sl@0
   281
	{
sl@0
   282
	// Nothing to do because Capabilities is not asynchronous.
sl@0
   283
	}
sl@0
   284
sl@0
   285
void CTest1CertStore::IsMandatory(const CCTCertInfo& /*aCertInfo*/, TBool& /*aMandatoryOut*/,
sl@0
   286
								 TRequestStatus& aStatus)
sl@0
   287
	{
sl@0
   288
	// currently not supported
sl@0
   289
	TRequestStatus* status = &aStatus;
sl@0
   290
	User::RequestComplete(status, KErrNotSupported);
sl@0
   291
	}
sl@0
   292
sl@0
   293
void CTest1CertStore::CancelIsMandatory()
sl@0
   294
	{
sl@0
   295
	// Nothing to do because IsMandatory is not asynchronous.
sl@0
   296
	}
sl@0
   297
sl@0
   298
sl@0
   299
void CTest1CertStore::Remove(const CCTCertInfo& aCertInfo, TRequestStatus& aStatus)
sl@0
   300
	{
sl@0
   301
	// This removes the certificate from the file store.
sl@0
   302
	TRAPD(err, DoRemoveL(aCertInfo));
sl@0
   303
	TRequestStatus* status = &aStatus;
sl@0
   304
	User::RequestComplete(status, err);
sl@0
   305
	}
sl@0
   306
sl@0
   307
void CTest1CertStore::CancelRemove()
sl@0
   308
	{
sl@0
   309
	}
sl@0
   310
sl@0
   311
sl@0
   312
void CTest1CertStore::SetApplicability(const CCTCertInfo& aCertInfo,
sl@0
   313
									  const RArray<TUid>& aTrusters,
sl@0
   314
									  TRequestStatus& aStatus)
sl@0
   315
	{
sl@0
   316
	TRAPD(err, DoSetApplicabilityL(aCertInfo, aTrusters));
sl@0
   317
	TRequestStatus* status = &aStatus;
sl@0
   318
	User::RequestComplete(status, err);
sl@0
   319
	}
sl@0
   320
sl@0
   321
sl@0
   322
void CTest1CertStore::CancelSetApplicability()
sl@0
   323
	{
sl@0
   324
	}
sl@0
   325
sl@0
   326
void CTest1CertStore::SetTrust(const CCTCertInfo& aCertInfo, 
sl@0
   327
							  TBool aTrusted, 
sl@0
   328
							  TRequestStatus& aStatus)
sl@0
   329
	{
sl@0
   330
	TRAPD(err, DoSetTrustL(aCertInfo, aTrusted));
sl@0
   331
	TRequestStatus* status = &aStatus;
sl@0
   332
	User::RequestComplete(status, err);
sl@0
   333
	}
sl@0
   334
sl@0
   335
void CTest1CertStore::CancelSetTrust()
sl@0
   336
	{
sl@0
   337
	// Nothing to do because SetTrust is not asynchronous.
sl@0
   338
	}
sl@0
   339
sl@0
   340
void CTest1CertStore::SetCapabilities(const CCTCertInfo& /*aCertInfo*/, const TCapabilitySet& /*aCapabilities*/, 
sl@0
   341
									 TRequestStatus& aStatus)
sl@0
   342
	{
sl@0
   343
	// currently not supported
sl@0
   344
	TRequestStatus* status = &aStatus;
sl@0
   345
	User::RequestComplete(status, KErrNotSupported);
sl@0
   346
	}
sl@0
   347
sl@0
   348
void CTest1CertStore::CancelSetCapabilities()
sl@0
   349
	{
sl@0
   350
	// Nothing to do because SetCapabilities is not asynchronous.
sl@0
   351
	}
sl@0
   352
sl@0
   353
void CTest1CertStore::SetMandatory(const CCTCertInfo& /*aCertInfo*/, TBool /*aMandatory*/,
sl@0
   354
								  TRequestStatus& aStatus)
sl@0
   355
	{
sl@0
   356
	// currently not supported
sl@0
   357
	TRequestStatus* status = &aStatus;
sl@0
   358
	User::RequestComplete(status, KErrNotSupported);
sl@0
   359
	}
sl@0
   360
sl@0
   361
void CTest1CertStore::CancelSetMandatory()
sl@0
   362
	{
sl@0
   363
	// Nothing to do because SetMandatory is not asynchronous.
sl@0
   364
	}
sl@0
   365
sl@0
   366
sl@0
   367
void CTest1CertStore::RevertStore(TAny* aStore)
sl@0
   368
	{
sl@0
   369
	//this is a CleanupItem
sl@0
   370
	CPermanentFileStore* store = REINTERPRET_CAST(CPermanentFileStore*, aStore);
sl@0
   371
	store->Revert();
sl@0
   372
	}
sl@0
   373
sl@0
   374
sl@0
   375
void CTest1CertStore::DeleteFile(TAny* aThis)
sl@0
   376
	{
sl@0
   377
	CTest1CertStore* self = REINTERPRET_CAST(CTest1CertStore*, aThis);
sl@0
   378
	TDriveUnit sysDrive (RFs::GetSystemDrive());
sl@0
   379
	TDriveName driveName(sysDrive.Name());
sl@0
   380
	TBuf<128> certStoreDefaultFullPath (driveName);
sl@0
   381
	certStoreDefaultFullPath.Append(_L("\\system\\data\\cacerts.dat"));
sl@0
   382
	self->iFile.Close();
sl@0
   383
	self->iFs.Delete(certStoreDefaultFullPath);
sl@0
   384
	}
sl@0
   385
sl@0
   386
sl@0
   387
sl@0
   388
CTest1CertStore::~CTest1CertStore()
sl@0
   389
	{
sl@0
   390
	Cancel();
sl@0
   391
sl@0
   392
	if (iStore != NULL)
sl@0
   393
		{
sl@0
   394
#ifdef _DEBUG
sl@0
   395
#else		
sl@0
   396
		TInt err;
sl@0
   397
		TRAP(err, iStore->ReclaimL());
sl@0
   398
		TRAP(err, iStore->CompactL());
sl@0
   399
		if (err == KErrNone)
sl@0
   400
			{
sl@0
   401
			TRAP(err, iStore->CommitL());
sl@0
   402
			}
sl@0
   403
#endif
sl@0
   404
		delete iStore;
sl@0
   405
		}
sl@0
   406
sl@0
   407
	iFile.Close();
sl@0
   408
	delete iCerts;
sl@0
   409
sl@0
   410
	iKeyInfos.Close();
sl@0
   411
sl@0
   412
	delete iUnifiedKeyStore;
sl@0
   413
	}
sl@0
   414
sl@0
   415
sl@0
   416
void CTest1CertStore::DoSetApplicabilityL(const CCTCertInfo& aCertInfo,
sl@0
   417
										 const RArray<TUid>& aTrusters)
sl@0
   418
	{
sl@0
   419
	TInt index = iCerts->Index(aCertInfo);
sl@0
   420
	if (index == KErrNotFound)
sl@0
   421
		{
sl@0
   422
		User::Leave(index);
sl@0
   423
		}
sl@0
   424
	
sl@0
   425
	CFileCertStoreMapping* mapping = iCerts->Mapping(index);
sl@0
   426
	const RArray<TUid>& trusters = mapping->CertificateApps(); //oldEntry->Trusters();
sl@0
   427
	RArray<TUid>* oldTrusters = new(ELeave) RArray<TUid>();
sl@0
   428
	CleanupStack::PushL(oldTrusters);
sl@0
   429
	CleanupClosePushL(*oldTrusters);
sl@0
   430
	TInt iEnd = trusters.Count();
sl@0
   431
	TInt i;
sl@0
   432
	for (i = 0; i < iEnd; i++)
sl@0
   433
		{
sl@0
   434
		User::LeaveIfError(oldTrusters->Append(trusters[i]));
sl@0
   435
		}
sl@0
   436
sl@0
   437
	RArray<TUid>* newTrusters = new (ELeave) RArray<TUid>;
sl@0
   438
	CleanupStack::PushL(newTrusters);
sl@0
   439
	CleanupClosePushL(*newTrusters);
sl@0
   440
	for (i = 0 ; i < aTrusters.Count() ; ++i)
sl@0
   441
		{
sl@0
   442
		User::LeaveIfError(newTrusters->Append(aTrusters[i]));		
sl@0
   443
		}
sl@0
   444
sl@0
   445
	mapping->SetCertificateApps(newTrusters);
sl@0
   446
	CleanupStack::Pop(2, newTrusters);
sl@0
   447
sl@0
   448
	TRAPD(err, UpdateStoreL());
sl@0
   449
	CleanupStack::Pop(2);	// *oldTrusters, oldTrusters
sl@0
   450
	if (err != KErrNone)
sl@0
   451
		{
sl@0
   452
		// If there is an error, we undo the change in oldEntry
sl@0
   453
		mapping->SetCertificateApps(oldTrusters);
sl@0
   454
		}
sl@0
   455
	else
sl@0
   456
		{
sl@0
   457
		oldTrusters->Close();
sl@0
   458
		delete oldTrusters;
sl@0
   459
		}
sl@0
   460
	}
sl@0
   461
sl@0
   462
sl@0
   463
void CTest1CertStore::DoSetTrustL(const CCTCertInfo& aCertInfo,
sl@0
   464
								 TBool aTrusted)
sl@0
   465
	{
sl@0
   466
	TInt index = iCerts->Index(aCertInfo);
sl@0
   467
	if (index == KErrNotFound)
sl@0
   468
		{
sl@0
   469
		User::Leave(index);
sl@0
   470
		}
sl@0
   471
	CFileCertStoreMapping* mapping = iCerts->Mapping(index);
sl@0
   472
	TBool oldValue = mapping->Trusted();
sl@0
   473
	mapping->SetTrusted(aTrusted);
sl@0
   474
	TRAPD(err, UpdateStoreL());
sl@0
   475
	if (err != KErrNone)
sl@0
   476
		{
sl@0
   477
		// If there is an error, we undo the change in oldEntry
sl@0
   478
		mapping->SetTrusted(oldValue);
sl@0
   479
		}
sl@0
   480
	}
sl@0
   481
sl@0
   482
void CTest1CertStore::Add(const TDesC& aLabel,
sl@0
   483
						 TCertificateFormat aFormat,
sl@0
   484
						 TCertificateOwnerType aCertificateOwnerType,
sl@0
   485
						 const TKeyIdentifier* aSubjectKeyId,
sl@0
   486
						 const TKeyIdentifier* aIssuerKeyId,
sl@0
   487
						 const TDesC8& aCert, 
sl@0
   488
						 TRequestStatus& aStatus)
sl@0
   489
	{
sl@0
   490
	TRAPD(err, DoAddL(aLabel, aFormat, aCertificateOwnerType, aSubjectKeyId,
sl@0
   491
		aIssuerKeyId, aCert));
sl@0
   492
	TRequestStatus* status = &aStatus;
sl@0
   493
	User::RequestComplete(status, err);
sl@0
   494
	}
sl@0
   495
sl@0
   496
void CTest1CertStore::CancelAdd()
sl@0
   497
	{
sl@0
   498
	Cancel();
sl@0
   499
	}
sl@0
   500
sl@0
   501
void CTest1CertStore::DoAddL(const TDesC& aLabel,
sl@0
   502
							TCertificateFormat aFormat,
sl@0
   503
							TCertificateOwnerType aCertificateOwnerType,
sl@0
   504
							const TKeyIdentifier* aSubjectKeyId,
sl@0
   505
							const TKeyIdentifier* aIssuerKeyId,
sl@0
   506
							const TDesC8& aCert)
sl@0
   507
	{
sl@0
   508
	TKeyIdentifier subjectKeyId;
sl@0
   509
	switch (aFormat)
sl@0
   510
		{
sl@0
   511
		case EX509Certificate:
sl@0
   512
			if (!aSubjectKeyId)
sl@0
   513
				{
sl@0
   514
				CCertificate* cert = CX509Certificate::NewLC(aCert);
sl@0
   515
				subjectKeyId = cert->KeyIdentifierL();
sl@0
   516
				aSubjectKeyId = &subjectKeyId;
sl@0
   517
				CleanupStack::PopAndDestroy(cert);
sl@0
   518
				}
sl@0
   519
			break;
sl@0
   520
	
sl@0
   521
		case EWTLSCertificate:
sl@0
   522
			if (!aSubjectKeyId)
sl@0
   523
				{
sl@0
   524
				CCertificate* cert = CWTLSCertificate::NewLC(aCert);
sl@0
   525
				subjectKeyId = cert->KeyIdentifierL();
sl@0
   526
				aSubjectKeyId = &subjectKeyId;
sl@0
   527
				CleanupStack::PopAndDestroy(cert);
sl@0
   528
				}
sl@0
   529
			break;
sl@0
   530
sl@0
   531
		case EX509CertificateUrl:
sl@0
   532
			if (!aSubjectKeyId)
sl@0
   533
				{
sl@0
   534
				User::Leave(KErrArgument);
sl@0
   535
				}
sl@0
   536
			break;
sl@0
   537
sl@0
   538
		default:
sl@0
   539
			User::Leave(KErrNotSupported);	
sl@0
   540
			break;
sl@0
   541
		}
sl@0
   542
sl@0
   543
sl@0
   544
	TInt iend = iCerts->Count();
sl@0
   545
	for (TInt i = 0; i < iend; i++)
sl@0
   546
		{
sl@0
   547
		if (iCerts->Entry(i).Label() == aLabel)
sl@0
   548
			{
sl@0
   549
			User::Leave(KErrBadName);
sl@0
   550
			}
sl@0
   551
		}
sl@0
   552
sl@0
   553
	CCTCertInfo* entry = CCTCertInfo::NewL(aLabel, aFormat, 
sl@0
   554
		aCertificateOwnerType, aCert.Length(), aSubjectKeyId, aIssuerKeyId, iToken, iCerts->Count());
sl@0
   555
	// AddCertL takes ownership of entry no matter what happens.
sl@0
   556
	AddCertL(entry, aCert, *iCerts);
sl@0
   557
	}
sl@0
   558
sl@0
   559
/*void CFileCertStore::AddUserCertL(const CCertificate& aCert, 
sl@0
   560
								  const TDesC& aLabel,
sl@0
   561
								  TCertificateFormat aFormat, 
sl@0
   562
								  const TKeyIdentifier& aIssuerKeyHash, 
sl@0
   563
								  const TKeyIdentifier& aSubjectKeyHash)
sl@0
   564
	{
sl@0
   565
	if (aFormat != EX509Certificate)
sl@0
   566
		{
sl@0
   567
		User::Leave(KErrNotSupported);
sl@0
   568
		}
sl@0
   569
sl@0
   570
	TInt iend = iUserCerts->Count();
sl@0
   571
	for (TInt i = 0; i < iend; i++)
sl@0
   572
		{
sl@0
   573
		if (iUserCerts->Entry(i).Label() == aLabel)
sl@0
   574
			{
sl@0
   575
			User::Leave(KErrBadName);
sl@0
   576
			}
sl@0
   577
		}
sl@0
   578
sl@0
   579
	// We compute the subject key hash using the information contained in the certificate
sl@0
   580
	const CSubjectPublicKeyInfo& key = aCert.PublicKey();
sl@0
   581
	CX509RSAPublicKey* rsaKey = CX509RSAPublicKey::NewLC(key.KeyData());
sl@0
   582
#ifdef SYMBIAN_CRYPTO	
sl@0
   583
	const TInteger& modulus = rsaKey->Modulus();
sl@0
   584
#else
sl@0
   585
	const CInteger& modulus = rsaKey->Modulus();
sl@0
   586
#endif
sl@0
   587
	HBufC8* modulusBuffer = modulus.BufferLC();
sl@0
   588
sl@0
   589
	CSHA1* sha1 = CSHA1::NewL();
sl@0
   590
	CleanupStack::PushL(sha1);
sl@0
   591
	
sl@0
   592
	TPtrC8 hash = sha1->Hash(*modulusBuffer);
sl@0
   593
sl@0
   594
sl@0
   595
	TKeyIdentifier keyId;
sl@0
   596
	keyId = hash;
sl@0
   597
sl@0
   598
	CleanupStack::PopAndDestroy(3);	// rsaKey, modulusBuffer, sha1
sl@0
   599
sl@0
   600
	// If the caller supplied a aSubjectKeyHash, we must compare it with the computed
sl@0
   601
	// value and ensure they are the same
sl@0
   602
	if ((aSubjectKeyHash != KNullDesC8) && (aSubjectKeyHash != keyId))
sl@0
   603
		{
sl@0
   604
		User::Leave(KErrArgument);
sl@0
   605
		}
sl@0
   606
	
sl@0
   607
	CCTCertInfo* entry = CCTCertInfo::NewL(aLabel, 
sl@0
   608
		*this, EX509Certificate, aCert.Encoding().Length(), keyId, aIssuerKeyHash);
sl@0
   609
	AddCertL(entry, aCert, *iUserCerts);
sl@0
   610
	}*/
sl@0
   611
sl@0
   612
/*void CFileCertStore::AddUserCertURLL(const TDesC8& aCert, 
sl@0
   613
									 const TDesC& aLabel,
sl@0
   614
									 const TKeyIdentifier& aIssuerKeyHash, 
sl@0
   615
									 const TKeyIdentifier& aSubjectKeyHash)
sl@0
   616
	{
sl@0
   617
	TInt iEnd = iUserCerts->Count();
sl@0
   618
	for (TInt i = 0; i < iEnd; i++)
sl@0
   619
		{
sl@0
   620
		if (iUserCerts->Entry(i).Label() == aLabel)
sl@0
   621
			{
sl@0
   622
			User::Leave(KErrBadName);
sl@0
   623
			}
sl@0
   624
		}
sl@0
   625
sl@0
   626
	CCTCertInfo* entry = CCTCertInfo::NewL(aLabel, 
sl@0
   627
		*this, EX509CertificateUrl, aCert.Length(), aSubjectKeyHash, aIssuerKeyHash);
sl@0
   628
	AddCertL(entry, aCert, *iUserCerts);
sl@0
   629
	}*/
sl@0
   630
sl@0
   631
void CTest1CertStore::AddCertL(CCTCertInfo* aCertInfo,
sl@0
   632
							  const TDesC8& aCert,
sl@0
   633
							  CFileCertStoreMappings& aMappings)
sl@0
   634
	{
sl@0
   635
	CleanupReleasePushL(*aCertInfo);
sl@0
   636
//create the mapping object		
sl@0
   637
	CFileCertStoreMapping* mapping = CFileCertStoreMapping::NewL();
sl@0
   638
	mapping->SetEntry(aCertInfo);
sl@0
   639
	CleanupStack::Pop();//aEntry, mapping has taken ownership
sl@0
   640
	CleanupStack::PushL(mapping);
sl@0
   641
sl@0
   642
	TCleanupItem cleanupStore(CTest1CertStore::RevertStore, iStore);//store will revert() if a leave occurs
sl@0
   643
	CleanupStack::PushL(cleanupStore);
sl@0
   644
sl@0
   645
//store cert
sl@0
   646
	RStoreWriteStream stream;
sl@0
   647
	TStreamId certId = stream.CreateLC(*iStore);//stream for cert
sl@0
   648
	stream.WriteL(aCert);
sl@0
   649
	stream.CommitL();
sl@0
   650
	CleanupStack::PopAndDestroy();//stream
sl@0
   651
	mapping->SetId(certId);
sl@0
   652
sl@0
   653
//add mapping to mappings, & store mappings
sl@0
   654
	aMappings.AddL(mapping); //takes ownership
sl@0
   655
	CleanupStack::Pop();//mapping; mappings has taken ownership
sl@0
   656
sl@0
   657
//Update the mapping. if error, remove the entry.
sl@0
   658
	TRAPD(err, aMappings.ReplaceL());
sl@0
   659
	if (err == KErrNone)
sl@0
   660
		{
sl@0
   661
		TRAP(err, iStore->CommitL());
sl@0
   662
		if (err != KErrNone)
sl@0
   663
			{
sl@0
   664
			aMappings.Remove(*mapping->Entry());
sl@0
   665
			User::Leave(err);
sl@0
   666
			}
sl@0
   667
		}
sl@0
   668
	else
sl@0
   669
		{
sl@0
   670
		//oom tests pass currently
sl@0
   671
		aMappings.Remove(*mapping->Entry());
sl@0
   672
		User::Leave(err);
sl@0
   673
		}
sl@0
   674
	CleanupStack::Pop();//revert store
sl@0
   675
	}
sl@0
   676
sl@0
   677
void CTest1CertStore::UpdateStoreL()
sl@0
   678
	{
sl@0
   679
//tries to write out the new cacerts to the file
sl@0
   680
	TCleanupItem cleanupStore(RevertStore, iStore);//store will revert() if a leave occurs
sl@0
   681
	CleanupStack::PushL(cleanupStore);
sl@0
   682
	iCerts->ReplaceL();
sl@0
   683
	iStore->CommitL();
sl@0
   684
	CleanupStack::Pop();//revert store
sl@0
   685
	}
sl@0
   686
sl@0
   687
sl@0
   688
sl@0
   689
sl@0
   690
sl@0
   691
void CTest1CertStore::DoLoadL(TDes8& aCertificate, CFileCertStoreMapping& aMapping) const
sl@0
   692
	{
sl@0
   693
	RStoreReadStream stream;
sl@0
   694
	stream.OpenLC(*iStore, aMapping.Id());
sl@0
   695
	CCTCertInfo* entry = aMapping.Entry();
sl@0
   696
	stream.ReadL(aCertificate, entry->Size());
sl@0
   697
	CleanupStack::PopAndDestroy();//stream
sl@0
   698
	}
sl@0
   699
sl@0
   700
void CTest1CertStore::DoRemoveL(const CCTCertInfo& aCertInfo)
sl@0
   701
	{
sl@0
   702
	switch(aCertInfo.CertificateFormat())
sl@0
   703
		{
sl@0
   704
		case EWTLSCertificate://must be a CA cert
sl@0
   705
		case EX509CertificateUrl:
sl@0
   706
		case EX509Certificate:
sl@0
   707
			User::LeaveIfError(iCerts->SetTempRemove(aCertInfo, ETrue));
sl@0
   708
			break;
sl@0
   709
			
sl@0
   710
		default:
sl@0
   711
			{
sl@0
   712
			User::Leave(KErrNotSupported);
sl@0
   713
			}
sl@0
   714
		}
sl@0
   715
	TRAPD(err, UpdateStoreL());
sl@0
   716
	if (err != KErrNone)
sl@0
   717
		{
sl@0
   718
		User::LeaveIfError(iCerts->SetTempRemove(aCertInfo, EFalse));
sl@0
   719
		}
sl@0
   720
	else
sl@0
   721
		{
sl@0
   722
		User::LeaveIfError(iCerts->Remove(aCertInfo));	
sl@0
   723
		}
sl@0
   724
	}
sl@0
   725
sl@0
   726
//private functions
sl@0
   727
CTest1CertStore::CTest1CertStore(CTest1CertStoreToken& aToken, RFs& aFs)
sl@0
   728
	: CActive(EPriorityNormal), iToken(aToken), iFs(aFs)
sl@0
   729
	{
sl@0
   730
	CActiveScheduler::Add(this);
sl@0
   731
	}
sl@0
   732
sl@0
   733
void CTest1CertStore::ConstructL(const TDesC& aFileName, TFileMode aMode)
sl@0
   734
	{
sl@0
   735
	iStore = OpenStoreL(aFileName, aMode);
sl@0
   736
	RestoreL();
sl@0
   737
	}
sl@0
   738
sl@0
   739
void CTest1CertStore::RestoreL()
sl@0
   740
	{
sl@0
   741
	TStreamId caCertEntryStreamId;
sl@0
   742
	RStoreReadStream stream;
sl@0
   743
	stream.OpenLC(*iStore, iStore->Root());
sl@0
   744
sl@0
   745
	stream >> caCertEntryStreamId;
sl@0
   746
	CleanupStack::PopAndDestroy();//stream
sl@0
   747
sl@0
   748
	iCerts = CFileCertStoreMappings::NewL(caCertEntryStreamId, *iStore);
sl@0
   749
	RStoreReadStream caCertEntryStream;
sl@0
   750
	caCertEntryStream.OpenLC(*iStore, caCertEntryStreamId);
sl@0
   751
sl@0
   752
	TInt caCount = caCertEntryStream.ReadInt32L();
sl@0
   753
	for (TInt i = 0; i < caCount; i++)
sl@0
   754
		{
sl@0
   755
		CFileCertStoreMapping* caCertMapping = CFileCertStoreMapping::NewL();
sl@0
   756
		CleanupStack::PushL(caCertMapping);
sl@0
   757
		CCTCertInfo* caCertEntry = CCTCertInfo::NewLC(caCertEntryStream, iToken);
sl@0
   758
		caCertMapping->SetEntry(caCertEntry);
sl@0
   759
		CleanupStack::Pop();//caCertEntry
sl@0
   760
		// Read the CertificateApps uids
sl@0
   761
		RArray<TUid>* certificateApps = new(ELeave) RArray<TUid>();
sl@0
   762
		CleanupStack::PushL(certificateApps);
sl@0
   763
		CleanupClosePushL(*certificateApps);
sl@0
   764
		TInt count = caCertEntryStream.ReadInt32L();
sl@0
   765
		for (TInt j = 0; j < count; j++)
sl@0
   766
			{
sl@0
   767
			TUid id;
sl@0
   768
			caCertEntryStream >> id;
sl@0
   769
			User::LeaveIfError(certificateApps->Append(id));
sl@0
   770
			}
sl@0
   771
		CleanupStack::Pop(2);	// *certificateApps, certificateApps
sl@0
   772
		caCertMapping->SetCertificateApps(certificateApps);
sl@0
   773
		TBool trusted = caCertEntryStream.ReadUint8L();
sl@0
   774
		caCertMapping->SetTrusted(trusted);
sl@0
   775
		TStreamId caCertStreamId;
sl@0
   776
		caCertEntryStream >> caCertStreamId;
sl@0
   777
		caCertMapping->SetId(caCertStreamId);
sl@0
   778
		iCerts->AddL(caCertMapping);
sl@0
   779
		CleanupStack::Pop();//caCertMapping
sl@0
   780
		}
sl@0
   781
	CleanupStack::PopAndDestroy();//caCertStream
sl@0
   782
	}
sl@0
   783
sl@0
   784
sl@0
   785
sl@0
   786
sl@0
   787
sl@0
   788
CPermanentFileStore* CTest1CertStore::OpenStoreLC(const TDesC& aFileName, TFileMode aMode)
sl@0
   789
	{
sl@0
   790
//this function creates, opens and returns a permanent file store in KCertStorePath,
sl@0
   791
//on the drive letter passed in, leaving it on the cleanup stack. 
sl@0
   792
//if the store isn't found it returns NULL
sl@0
   793
	TInt err = iFile.Open(iFs, aFileName, aMode); 	
sl@0
   794
    if (err == KErrNone)
sl@0
   795
		{
sl@0
   796
		CPermanentFileStore* store = CPermanentFileStore::FromLC(iFile);
sl@0
   797
		return store;
sl@0
   798
		}
sl@0
   799
	else
sl@0
   800
		{
sl@0
   801
		User::Leave(err);
sl@0
   802
		}
sl@0
   803
	return NULL;
sl@0
   804
	}
sl@0
   805
sl@0
   806
CPermanentFileStore* CTest1CertStore::OpenStoreL(const TDesC& aFileName, TFileMode aMode)
sl@0
   807
	{
sl@0
   808
	CPermanentFileStore* store = NULL;
sl@0
   809
	store = OpenStoreLC(aFileName, aMode);	
sl@0
   810
	CleanupStack::Pop(store);
sl@0
   811
	return store;
sl@0
   812
	}
sl@0
   813
sl@0
   814
void CTest1CertStore::RunL()
sl@0
   815
	{
sl@0
   816
	switch (iState)
sl@0
   817
		{
sl@0
   818
	case EGetKeyInfos:
sl@0
   819
		iKeyFilter.iUsage = KeyUsageX509ToPKCS15Private(iFilter->iKeyUsage);
sl@0
   820
		iUnifiedKeyStore->List(iKeyInfos, iKeyFilter, iStatus);
sl@0
   821
		iState = EList;
sl@0
   822
		SetActive();
sl@0
   823
		break;
sl@0
   824
sl@0
   825
	case ERetrieve:
sl@0
   826
		// Asynch Retrieve 
sl@0
   827
		RetrieveNow(*iAsynchCertInfo,*iAsynchCertificate,*iOriginalRequestStatus);
sl@0
   828
		break;
sl@0
   829
sl@0
   830
	case EList:
sl@0
   831
		{	
sl@0
   832
		TInt count = iCerts->Count();
sl@0
   833
		for (TInt index = 0; index < count; index++)
sl@0
   834
			{
sl@0
   835
			const CCTCertInfo& certInfo = iCerts->Entry(index);
sl@0
   836
			
sl@0
   837
			TBool accept = ETrue;
sl@0
   838
			if (iFilter->iUidIsSet)
sl@0
   839
				{
sl@0
   840
				accept = iCerts->Mapping(index)->IsApplicable(iFilter->iUid);
sl@0
   841
				}
sl@0
   842
			if (iFilter->iFormatIsSet && accept)
sl@0
   843
				{
sl@0
   844
				accept = (iFilter->iFormat == certInfo.CertificateFormat());
sl@0
   845
				}
sl@0
   846
			if (iFilter->iOwnerTypeIsSet && accept)
sl@0
   847
				{
sl@0
   848
				accept = (iFilter->iOwnerType == certInfo.CertificateOwnerType());
sl@0
   849
				}
sl@0
   850
			if ((iFilter->iSubjectKeyId != KNullDesC8) && accept)
sl@0
   851
				{
sl@0
   852
				accept = (iFilter->iSubjectKeyId == certInfo.SubjectKeyId());
sl@0
   853
				}
sl@0
   854
			
sl@0
   855
			if (accept)
sl@0
   856
				{
sl@0
   857
				// Fill in the cert hash.
sl@0
   858
				// (This returns an incorrect hard-coded value, which allows 
sl@0
   859
				// the test code to check that the store is being treated
sl@0
   860
				// correctly as a hardware store, as it'll behave differently
sl@0
   861
				// to if it returned the correct hashes.)
sl@0
   862
				_LIT8(KHash, "\x70\xe4\xf4\x54\x5f\x8e\xe6\xf2\xbd\x4e\x76\x2b\x8d\xa1\x83\xd8\xe0\x5d\x4a\x7d");
sl@0
   863
				CCTCertInfo* copy = CCTCertInfo::NewLC(
sl@0
   864
					certInfo.Label(), certInfo.CertificateFormat(),
sl@0
   865
					certInfo.CertificateOwnerType(), certInfo.Size(),
sl@0
   866
					&certInfo.SubjectKeyId(), &certInfo.IssuerKeyId(),
sl@0
   867
					certInfo.Token(), certInfo.Handle().iObjectId,
sl@0
   868
					ETrue, &KHash);
sl@0
   869
				User::LeaveIfError(iCertInfos->Append(copy));
sl@0
   870
				CleanupStack::Pop();
sl@0
   871
				}
sl@0
   872
			}
sl@0
   873
		iKeyInfos.Close();
sl@0
   874
		delete iUnifiedKeyStore;
sl@0
   875
		iUnifiedKeyStore = 0;
sl@0
   876
		User::RequestComplete(iOriginalRequestStatus, KErrNone);
sl@0
   877
		}
sl@0
   878
		break;
sl@0
   879
sl@0
   880
	default:
sl@0
   881
		// ignore the undefined operations
sl@0
   882
		break;
sl@0
   883
		}
sl@0
   884
	}
sl@0
   885
sl@0
   886
void CTest1CertStore::DoCancel()
sl@0
   887
	{
sl@0
   888
	if (iUnifiedKeyStore)
sl@0
   889
		{
sl@0
   890
		if (iState == EGetKeyInfos)
sl@0
   891
			{
sl@0
   892
			iUnifiedKeyStore->CancelInitialize();
sl@0
   893
			}
sl@0
   894
		else if (iState == EList)
sl@0
   895
			{
sl@0
   896
			iUnifiedKeyStore->CancelList();
sl@0
   897
			}
sl@0
   898
		iState = EList;
sl@0
   899
		iKeyInfos.Close();
sl@0
   900
		delete iUnifiedKeyStore;
sl@0
   901
		iUnifiedKeyStore = 0;
sl@0
   902
		}
sl@0
   903
	User::RequestComplete(iOriginalRequestStatus, KErrCancel);
sl@0
   904
	}
sl@0
   905
sl@0
   906
const TImplementationProxy ImplementationTable[] =
sl@0
   907
	{
sl@0
   908
#ifdef CERTSTORE_SOFTWARE_ASYNCH
sl@0
   909
		IMPLEMENTATION_PROXY_ENTRY(0x101FF738, CTest1CertStoreTokenType::NewL)
sl@0
   910
#else
sl@0
   911
	#ifdef CERTSTORE_HARDWARE_SIM
sl@0
   912
		IMPLEMENTATION_PROXY_ENTRY(0x10206846, CTest1CertStoreTokenType::NewL)
sl@0
   913
	#else
sl@0
   914
		#ifdef CERTSTORE_HARDWARE_WIM
sl@0
   915
			IMPLEMENTATION_PROXY_ENTRY(0x10206847, CTest1CertStoreTokenType::NewL)
sl@0
   916
		#else
sl@0
   917
			#ifdef CERTSTORE_HARDWARE_UICC
sl@0
   918
				IMPLEMENTATION_PROXY_ENTRY(0x10206848, CTest1CertStoreTokenType::NewL)
sl@0
   919
 			#else
sl@0
   920
				#ifdef CERTSTORE_DEVICE_IMMUTABLE
sl@0
   921
					IMPLEMENTATION_PROXY_ENTRY(0x102077C3, CTest1CertStoreTokenType::NewL)
sl@0
   922
				#else
sl@0
   923
					IMPLEMENTATION_PROXY_ENTRY(0x101F5279, CTest1CertStoreTokenType::NewL)
sl@0
   924
				#endif
sl@0
   925
 			#endif
sl@0
   926
		#endif
sl@0
   927
	#endif
sl@0
   928
#endif
sl@0
   929
	};
sl@0
   930
sl@0
   931
EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
sl@0
   932
	{
sl@0
   933
	aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
sl@0
   934
sl@0
   935
	return ImplementationTable;
sl@0
   936
	}
sl@0
   937
sl@0
   938
sl@0
   939
sl@0
   940
sl@0
   941