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