cryptoservices/certificateandkeymgmt/certstore/unifiedcertstore.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 "unifiedcertstore.h"
       
    20 #include "unifiedcertstoreworkingvars.h"
       
    21 #include "CCheckedCertStore.h"
       
    22 #include <certificateapps.h>
       
    23 #include <x509cert.h>
       
    24 #include <wtlscert.h>
       
    25 #include <hash.h>
       
    26 #include <ecom.h>
       
    27 
       
    28 _LIT(KUCSPanic, "CUnifiedCertStore"); 
       
    29 #define assert(x) __ASSERT_ALWAYS((x), User::Panic(KUCSPanic, 1));
       
    30 
       
    31 /////////////////////////////////////////////////////////////////////////////////////
       
    32 //CUnifiedCertStore
       
    33 /////////////////////////////////////////////////////////////////////////////////////
       
    34 
       
    35 EXPORT_C CUnifiedCertStore* CUnifiedCertStore::NewL(RFs& aFs, TBool aOpenForWrite)
       
    36 	{
       
    37 	CUnifiedCertStore* self = CUnifiedCertStore::NewLC(aFs, aOpenForWrite);
       
    38 	CleanupStack::Pop(self);
       
    39 	return self;
       
    40 	}
       
    41 
       
    42 EXPORT_C CUnifiedCertStore* CUnifiedCertStore::NewLC(RFs& aFs, TBool aOpenForWrite)
       
    43 	{
       
    44 	CUnifiedCertStore* self = new(ELeave) CUnifiedCertStore(aFs, aOpenForWrite);
       
    45 	CleanupStack::PushL(self);
       
    46 	RArray<TInt> orderingFilter;
       
    47 	self->ConstructL(orderingFilter);
       
    48 	return self;
       
    49 	}
       
    50 
       
    51 EXPORT_C CUnifiedCertStore::~CUnifiedCertStore()
       
    52 	{
       
    53 	Cancel();
       
    54 
       
    55 	assert(!iWorkingVars);
       
    56 
       
    57 	TInt end = iReadOnlyCertStores.Count();
       
    58 	TInt i;
       
    59 	for (i = 0; i < end; i++)
       
    60 		{
       
    61 		iReadOnlyCertStores[i]->Release();
       
    62 		}
       
    63 	iReadOnlyCertStores.Close();
       
    64 
       
    65 	end = iWritableCertStores.Count();
       
    66 	for (i = 0; i < end; i++)
       
    67 		{
       
    68 		iWritableCertStores[i]->Release();
       
    69 		}
       
    70 	iWritableCertStores.Close();
       
    71 
       
    72 	// The elements are already released by the two loops above
       
    73 	iCertStores.Close();
       
    74 	
       
    75 	// release resources allocated to order attributes list
       
    76 	iOrderAttributes.Close();
       
    77     
       
    78     DestroyTemporaryMembers();
       
    79 	iHardwareTypeUids.Close();
       
    80 	REComSession::FinalClose();
       
    81 	}
       
    82 
       
    83 
       
    84 EXPORT_C void CUnifiedCertStore::Initialize(TRequestStatus& aStatus)
       
    85 	{
       
    86 	BeginAsyncOp(aStatus, EInitializeGetTokenList);
       
    87 	TRAPD(err, InitializeL());
       
    88 	if (err != KErrNone)
       
    89 		{
       
    90 		Complete(err);
       
    91 		}
       
    92 	}
       
    93 
       
    94 void CUnifiedCertStore::InitializeL()
       
    95 	{
       
    96 	AllocWorkingVarsL();
       
    97 	
       
    98 	// We want the list of all token types that support a readable or writable 
       
    99 	// certstore interface
       
   100 	RArray<TUid> uidArray;
       
   101 	CleanupClosePushL(uidArray);
       
   102 		
       
   103 	User::LeaveIfError(uidArray.Append(TUid::Uid(KInterfaceWritableCertStore)));
       
   104 	
       
   105 	TCTFindTokenTypesByInterface filter(uidArray.Array());
       
   106 	CCTTokenTypeInfo::ListL(iWorkingVars->iWritableTokenTypes, filter);
       
   107 
       
   108 	uidArray.Reset();
       
   109 
       
   110 	User::LeaveIfError(uidArray.Append(TUid::Uid(KInterfaceCertStore)));
       
   111 
       
   112 	RCPointerArray<CCTTokenTypeInfo> tokenTypes;
       
   113 	CleanupClosePushL(tokenTypes);
       
   114 
       
   115 	TCTFindTokenTypesByInterface filter2(uidArray.Array());
       
   116 	CCTTokenTypeInfo::ListL(tokenTypes, filter2);
       
   117 	
       
   118   	// Check whether client specified order list has attributes in it
       
   119   	if(iOrderAttributes.Count() > 0)
       
   120   	    {
       
   121   	    ApplyOrderingL(tokenTypes);
       
   122   	    }
       
   123 
       
   124 	// Make a note of all hardware token types
       
   125 	TInt i = 0;
       
   126 	TInt end = tokenTypes.Count();
       
   127 	for (; i < end; i++)
       
   128 		{
       
   129 		TCTTokenTypeAttribute software;
       
   130 		software.iUID = KCTSoftware;
       
   131 		TInt find = tokenTypes[i]->Attributes().Find(software);
       
   132 		// In the case (TInt)ETrue == KThirdPartyCertStore == 1  
       
   133 		if (find != KErrNotFound && tokenTypes[i]->Attributes()[find].iVal != 
       
   134 			(TInt)ETrue && tokenTypes[i]->Attributes()[find].iVal != KManufactureCertStore)
       
   135 			{
       
   136 			// This is a hardware type. Add its UID to the list.
       
   137 			User::LeaveIfError(iHardwareTypeUids.Append(tokenTypes[i]->Type()));
       
   138 			}
       
   139 		}
       
   140 
       
   141 	i = 0;
       
   142 	while (i < end)
       
   143 		{
       
   144 		TInt j = 0;
       
   145 		TInt jEnd = iWorkingVars->iWritableTokenTypes.Count();
       
   146 		while (j < jEnd)
       
   147 			{
       
   148 			if (iWorkingVars->iWritableTokenTypes[j]->Type() == tokenTypes[i]->Type())
       
   149 				{
       
   150 				break;
       
   151 				}
       
   152 			j++;
       
   153 			}
       
   154 		if (j == jEnd)
       
   155 			{
       
   156 			User::LeaveIfError(iWorkingVars->iReadOnlyTokenTypes.Append(tokenTypes[i]));
       
   157 			tokenTypes.Remove(i);
       
   158 			end--;
       
   159 			}
       
   160 		else
       
   161 			{
       
   162 			i++;
       
   163 			}
       
   164 		}
       
   165 
       
   166 	CleanupStack::PopAndDestroy(2);	// uidArray, tokenTypes
       
   167 	
       
   168 	iWorkingVars->iIndex = -1;
       
   169 
       
   170 	TRequestStatus* status = &iStatus;
       
   171 	User::RequestComplete(status, KErrNone);
       
   172 	SetActive();
       
   173 	}
       
   174 
       
   175 EXPORT_C void CUnifiedCertStore::CancelInitialize()
       
   176 	{
       
   177 	if (iState == EInitializeGetTokenList ||
       
   178 		iState == EInitializeGetToken ||
       
   179 		iState == EInitializeGetWritableInterface ||
       
   180 		iState == EInitializeGetReadableInterface ||
       
   181 		iState == EInitializeGetReadableInterfaceFinished ||
       
   182 		iState == EInitializeFinished)
       
   183 		{
       
   184 		Cancel();
       
   185 		}	
       
   186 	}
       
   187 
       
   188 void CUnifiedCertStore::List(RMPointerArray<CCTCertInfo>& aCertInfos,
       
   189 							 const CCertAttributeFilter& aFilter,
       
   190 							 TRequestStatus& aStatus)
       
   191 	{
       
   192 	BeginAsyncOp(aStatus, EList);	
       
   193 	TRAPD(err, ListL(aCertInfos, aFilter));
       
   194 	if (err != KErrNone)
       
   195 		{
       
   196 		Complete(err);
       
   197 		}
       
   198 	}
       
   199 
       
   200 void CUnifiedCertStore::ListL(RMPointerArray<CCTCertInfo>& aCertInfos,
       
   201 							  const CCertAttributeFilter& aFilter)
       
   202 	{
       
   203 	if (!iIsInitialized)
       
   204 		{
       
   205 		User::Leave(KErrNotReady);
       
   206 		}
       
   207 
       
   208 	AllocWorkingVarsL();
       
   209 	iWorkingVars->iCertInfos = &aCertInfos;
       
   210 	iWorkingVars->iFilter = &aFilter;
       
   211 	iWorkingVars->iCertIndex = aCertInfos.Count();
       
   212 	iIndex = -1;
       
   213 	
       
   214 	SetActive();
       
   215 	TRequestStatus* status = &iStatus;
       
   216 	User::RequestComplete(status, KErrNone);
       
   217 	}
       
   218 
       
   219 void CUnifiedCertStore::CancelList()
       
   220 	{
       
   221 	if (iState == EList ||
       
   222 		iState == ERetrieveForList)
       
   223 		{
       
   224 		Cancel();
       
   225 		}
       
   226 	}
       
   227 
       
   228 EXPORT_C void CUnifiedCertStore::List(RMPointerArray<CCTCertInfo>& aCertInfos,
       
   229 									  const CCertAttributeFilter& aFilter, 
       
   230 									  const TDesC8& aIssuer, 
       
   231 									  TRequestStatus& aStatus)
       
   232 	{
       
   233 	RPointerArray<const TDesC8> array;
       
   234 	if (array.Append(&aIssuer) != KErrNone)
       
   235 		{
       
   236 		TRequestStatus* status = &aStatus;
       
   237 		User::RequestComplete(status, KErrNoMemory);
       
   238 		}
       
   239 	else
       
   240 		{
       
   241 		List(aCertInfos, aFilter, array, aStatus);
       
   242 		array.Close();
       
   243 		}
       
   244 	}
       
   245 
       
   246 EXPORT_C void CUnifiedCertStore::List(RMPointerArray<CCTCertInfo>& aCertInfos,
       
   247 									  const CCertAttributeFilter& aFilter, 
       
   248 									  RPointerArray<const TDesC8> aIssuers, 
       
   249 									  TRequestStatus& aStatus)
       
   250 	{
       
   251 	BeginAsyncOp(aStatus, EList);	
       
   252 	TRAPD(err, ListL(aCertInfos, aFilter, aIssuers));
       
   253 	if (err != KErrNone)
       
   254 		{
       
   255 		Complete(err);
       
   256 		}
       
   257 	}
       
   258 
       
   259 void CUnifiedCertStore::ListL(RMPointerArray<CCTCertInfo>& aCertInfos,
       
   260 							  const CCertAttributeFilter& aFilter, 
       
   261 							  RPointerArray<const TDesC8> aIssuers)
       
   262 	{
       
   263 	// Obscure special case: If aIssuers has no elements, we should
       
   264 	// return nothing.
       
   265 	if (aIssuers.Count() == 0)
       
   266 		{
       
   267 		Complete(KErrNone);
       
   268 		return;
       
   269 		}
       
   270 
       
   271 	AllocWorkingVarsL();
       
   272 	iWorkingVars->iCertInfos = &aCertInfos;
       
   273 	iWorkingVars->iFilter = &aFilter;
       
   274 	iWorkingVars->iCertIndex = aCertInfos.Count();
       
   275 	
       
   276 	TInt count = aIssuers.Count();
       
   277 	for (TInt i = 0 ; i < count ; ++i)
       
   278 		{
       
   279 		User::LeaveIfError(iWorkingVars->iIssuerNames.Append(aIssuers[i]));
       
   280 		}
       
   281 	
       
   282 	iIndex = -1;
       
   283 	SetActive();
       
   284 	TRequestStatus* status = &iStatus;
       
   285 	User::RequestComplete(status, KErrNone);	
       
   286 	}
       
   287 
       
   288 EXPORT_C void CUnifiedCertStore::Retrieve(const CCTCertInfo& aCertInfo, 
       
   289 								 CCertificate*& aCert,
       
   290 								 TRequestStatus& aStatus)
       
   291 	{
       
   292 	BeginAsyncOp(aStatus, ERetrieve);	
       
   293 	TRAPD(err, RetrieveL(aCertInfo, aCert));
       
   294 	if (err != KErrNone)
       
   295 		{
       
   296 		Complete(err);
       
   297 		}
       
   298 	}
       
   299 
       
   300 void CUnifiedCertStore::RetrieveL(const CCTCertInfo& aCertInfo, 
       
   301 								  CCertificate*& aCert)
       
   302 	{
       
   303 	FindCertStoreL(aCertInfo.Handle());
       
   304 
       
   305     if (aCertInfo.CertificateFormat() != EX509Certificate &&
       
   306         aCertInfo.CertificateFormat() != EWTLSCertificate)
       
   307         {
       
   308         User::Leave(KErrNotSupported);
       
   309         }
       
   310 
       
   311 	AllocWorkingVarsL();
       
   312 	iWorkingVars->iCertDesC = HBufC8::NewMaxL(aCertInfo.Size());
       
   313 	iWorkingVars->iReturnedCert = &aCert;
       
   314 	iWorkingVars->iCertType = aCertInfo.CertificateFormat();
       
   315 	iWorkingVars->iCertDes.Set(iWorkingVars->iCertDesC->Des());
       
   316 	iCurrentCertStore->Retrieve(aCertInfo, iWorkingVars->iCertDes, iStatus);
       
   317 	SetActive();
       
   318     }
       
   319 
       
   320 void CUnifiedCertStore::GetCert(CCTCertInfo*& aCertInfo, 
       
   321 								const TCTTokenObjectHandle& aHandle, 
       
   322 								TRequestStatus& aStatus)
       
   323 	{
       
   324 	BeginAsyncOp(aStatus, EGetCert);
       
   325 	TRAPD(err, GetCertL(aCertInfo, aHandle));
       
   326 	if (err != KErrNone)
       
   327 		{
       
   328 		Complete(err);
       
   329 		}
       
   330 	}
       
   331 
       
   332 void CUnifiedCertStore::GetCertL(CCTCertInfo*& aCertInfo,
       
   333 								 const TCTTokenObjectHandle& aHandle)
       
   334 	{
       
   335 	FindCertStoreL(aHandle);
       
   336 	iCurrentCertStore->GetCert(aCertInfo, aHandle, iStatus);
       
   337 	SetActive();
       
   338 	}
       
   339 
       
   340 void CUnifiedCertStore::CancelGetCert()
       
   341 	{
       
   342 	if (iState == EGetCert)
       
   343 		{
       
   344 		Cancel();
       
   345 		}
       
   346 	}
       
   347 
       
   348 void CUnifiedCertStore::Applications(const CCTCertInfo& aCertInfo,
       
   349 									 RArray<TUid>& aApplications,
       
   350 									 TRequestStatus& aStatus)
       
   351 	{
       
   352 	BeginAsyncOp(aStatus, EApplications);
       
   353 	TRAPD(err, ApplicationsL(aCertInfo, aApplications));
       
   354 	if (err != KErrNone)
       
   355 		{
       
   356 		Complete(err);
       
   357 		}
       
   358 	}
       
   359 
       
   360 void CUnifiedCertStore::ApplicationsL(const CCTCertInfo& aCertInfo,
       
   361 									 RArray<TUid>& aApplications)
       
   362 	{
       
   363 	FindCertStoreL(aCertInfo.Handle());
       
   364 	iCurrentCertStore->Applications(aCertInfo, aApplications, iStatus);
       
   365 	SetActive();
       
   366 	}
       
   367 
       
   368 void CUnifiedCertStore::CancelApplications()
       
   369 	{
       
   370 	if (iState == EApplications)
       
   371 		{
       
   372 		Cancel();
       
   373 		}
       
   374 	}
       
   375 
       
   376 void CUnifiedCertStore::IsApplicable(const CCTCertInfo& aCertInfo,
       
   377 									 TUid aApplication, 
       
   378 									 TBool& aIsApplicable, 
       
   379 									 TRequestStatus& aStatus)
       
   380 	{
       
   381 	BeginAsyncOp(aStatus, EIsApplicable);
       
   382 	TRAPD(err, IsApplicableL(aCertInfo, aApplication, aIsApplicable));
       
   383 	if (err != KErrNone)
       
   384 		{
       
   385 		Complete(err);
       
   386 		}
       
   387 	}
       
   388 
       
   389 void CUnifiedCertStore::IsApplicableL(const CCTCertInfo& aCertInfo,
       
   390 									  TUid aApplication, 
       
   391 									  TBool& aIsApplicable)
       
   392 	{
       
   393 	FindCertStoreL(aCertInfo.Handle());
       
   394 	iCurrentCertStore->IsApplicable(aCertInfo, aApplication, aIsApplicable, iStatus);
       
   395 	SetActive();
       
   396 	}
       
   397 
       
   398 void CUnifiedCertStore::CancelIsApplicable()
       
   399 	{
       
   400 	if (iState == EIsApplicable)
       
   401 		{
       
   402 		Cancel();
       
   403 		}
       
   404 	}
       
   405 
       
   406 void CUnifiedCertStore::Trusted(const CCTCertInfo& aCertInfo, 
       
   407 								TBool& aTrusted, 
       
   408 								TRequestStatus& aStatus)
       
   409 	{
       
   410 	BeginAsyncOp(aStatus, ETrusted);
       
   411 	TRAPD(err, TrustedL(aCertInfo, aTrusted));
       
   412 	if (err != KErrNone)
       
   413 		{
       
   414 		Complete(err);
       
   415 		}
       
   416 	}
       
   417 
       
   418 void CUnifiedCertStore::TrustedL(const CCTCertInfo& aCertInfo, 
       
   419 								 TBool& aTrusted)
       
   420 	{
       
   421 	FindCertStoreL(aCertInfo.Handle());
       
   422 	iCurrentCertStore->Trusted(aCertInfo, aTrusted, iStatus);
       
   423 	SetActive();
       
   424 	}
       
   425 
       
   426 void CUnifiedCertStore::CancelTrusted()
       
   427 	{
       
   428 	if (iState == ETrusted)
       
   429 		{
       
   430 		Cancel();
       
   431 		}
       
   432 	}
       
   433 
       
   434 void CUnifiedCertStore::Retrieve(const CCTCertInfo& aCertInfo,
       
   435 								 TDes8& aEncodedCert,
       
   436 								 TRequestStatus& aStatus)
       
   437 	{
       
   438 	BeginAsyncOp(aStatus, ERetrieveData);
       
   439 	TRAPD(err, RetrieveDataL(aCertInfo, aEncodedCert));
       
   440 	if (err != KErrNone)
       
   441 		{
       
   442 		Complete(err);
       
   443 		}
       
   444 	}
       
   445 
       
   446 void CUnifiedCertStore::RetrieveDataL(const CCTCertInfo& aCertInfo,
       
   447 									  TDes8& aEncodedCert)
       
   448 	{
       
   449 	FindCertStoreL(aCertInfo.Handle());
       
   450 	iCurrentCertStore->Retrieve(aCertInfo, aEncodedCert, iStatus);	
       
   451 	SetActive();
       
   452 	}
       
   453 
       
   454 void CUnifiedCertStore::CancelRetrieve()
       
   455 	{
       
   456 	if (iState == ERetrieveData)
       
   457 		{
       
   458 		Cancel();
       
   459 		}
       
   460 	}
       
   461 
       
   462 EXPORT_C void CUnifiedCertStore::Remove(const CCTCertInfo& aCertInfo,
       
   463 										TRequestStatus& aStatus)
       
   464 	{
       
   465 	BeginAsyncOp(aStatus, ERemove);
       
   466 	TRAPD(err, RemoveL(aCertInfo));
       
   467 	if (err != KErrNone)
       
   468 		{
       
   469 		Complete(err);
       
   470 		}
       
   471 	}
       
   472 
       
   473 void CUnifiedCertStore::RemoveL(const CCTCertInfo& aCertInfo)
       
   474 	{
       
   475 	FindWritableCertStoreL(aCertInfo.Handle());
       
   476 	iCurrentWritableCertStore->Remove(aCertInfo, iStatus);	
       
   477 	SetActive();
       
   478 	}
       
   479 
       
   480 EXPORT_C void CUnifiedCertStore::CancelRemove()
       
   481 	{
       
   482 	if (iState == ERemove)
       
   483 		{
       
   484 		Cancel();
       
   485 		}
       
   486 	}
       
   487 
       
   488 EXPORT_C void CUnifiedCertStore::SetApplicability(const CCTCertInfo& aCertInfo, 
       
   489 												  const RArray<TUid>& aApplications,
       
   490 												  TRequestStatus& aStatus)
       
   491 	{
       
   492 	BeginAsyncOp(aStatus, ESetApplicability);	
       
   493 	TRAPD(err, SetApplicabilityL(aCertInfo, aApplications));
       
   494 	if (err != KErrNone)
       
   495 		{
       
   496 		Complete(err);
       
   497 		}
       
   498 	}
       
   499 
       
   500 void CUnifiedCertStore::SetApplicabilityL(const CCTCertInfo& aCertInfo, 
       
   501 										  const RArray<TUid>& aApplications)
       
   502 	{
       
   503 	FindWritableCertStoreL(aCertInfo.Handle());
       
   504 	
       
   505 	// Search for duplicates in the array of application
       
   506 	// complete with KErrArgument if there are any duplicates
       
   507 	if(aApplications.Count() > 1)
       
   508 		{
       
   509 		TInt i=0, j=1;
       
   510 
       
   511 		for(i=0; i<aApplications.Count()-1; i++ )
       
   512 			{
       
   513 			for(j=i+1; j<aApplications.Count(); j++)
       
   514 				{
       
   515 				if(aApplications[i] == aApplications[j])
       
   516 					{
       
   517 					User::Leave(KErrArgument);
       
   518 					}
       
   519 				}		  			
       
   520 			}
       
   521 		}
       
   522 
       
   523 	// Check requested applications actaully exist
       
   524 	CCertificateAppInfoManager* appInfoManager = CCertificateAppInfoManager::NewLC(iFs, EFalse);
       
   525 	const RArray<TCertificateAppInfo>& applications = appInfoManager->Applications();
       
   526 
       
   527 	for (TInt i = 0 ; i < aApplications.Count() ; ++i)
       
   528 		{
       
   529 		TInt j = 0;
       
   530 		for ( ; j < applications.Count() ; ++j)
       
   531 			{
       
   532 			if (aApplications[i] == applications[j].Id())
       
   533 				{
       
   534 				break;
       
   535 				}
       
   536 			}
       
   537 		if (j == applications.Count())
       
   538 			{
       
   539 			User::Leave(KErrArgument);
       
   540 			}
       
   541 		}
       
   542 	CleanupStack::PopAndDestroy(appInfoManager);
       
   543 
       
   544 	iCurrentWritableCertStore->SetApplicability(aCertInfo, aApplications, iStatus);
       
   545 	SetActive();
       
   546 	}
       
   547 
       
   548 EXPORT_C void CUnifiedCertStore::CancelSetApplicability()
       
   549 	{
       
   550 	if (iState == ESetApplicability)
       
   551 		{
       
   552 		Cancel();
       
   553 		}
       
   554 	}
       
   555 
       
   556 EXPORT_C void CUnifiedCertStore::SetTrust(const CCTCertInfo& aCertInfo, 
       
   557 										  TBool aTrusted,
       
   558 										  TRequestStatus& aStatus)
       
   559 	{
       
   560 	BeginAsyncOp(aStatus, ESetTrust);	
       
   561 	TRAPD(err, SetTrustL(aCertInfo, aTrusted));
       
   562 	if (err != KErrNone)
       
   563 		{
       
   564 		Complete(err);
       
   565 		}
       
   566 	}
       
   567 
       
   568 void CUnifiedCertStore::SetTrustL(const CCTCertInfo& aCertInfo, TBool aTrusted)
       
   569 	{
       
   570 	FindWritableCertStoreL(aCertInfo.Handle());
       
   571 	iCurrentWritableCertStore->SetTrust(aCertInfo, aTrusted, iStatus);
       
   572 	SetActive();
       
   573 	}
       
   574 
       
   575 EXPORT_C void CUnifiedCertStore::CancelSetTrust()
       
   576 	{
       
   577 	if (iState == ESetTrust)
       
   578 		{
       
   579 		Cancel();
       
   580 		}
       
   581 	}
       
   582 
       
   583 /**
       
   584  * Get the certstore containing a given certificate.
       
   585  * 
       
   586  * Returns the certstore containing the cert referenced in certinfo or NULL if
       
   587  * not found.
       
   588  */
       
   589 MCTCertStore* CUnifiedCertStore::GetCertStore(const TCTTokenObjectHandle& aHandle)
       
   590 	{
       
   591 	TInt count = iCertStores.Count();
       
   592 	for (TInt i = 0; i < count; i++)
       
   593 		{
       
   594 		MCTCertStore* certstore = iCertStores[i];
       
   595 		MCTToken& token = certstore->Token();
       
   596 		if (token.Handle() == aHandle.iTokenHandle)
       
   597 			{
       
   598 			return certstore;
       
   599 			}
       
   600 		}
       
   601 	return NULL;
       
   602 	}
       
   603 
       
   604 /**
       
   605  * Set iCurrentCertStore to the certstore containing a given certificate, or
       
   606  * leave if it could not be found.  The handle is the handle of the *certinfo*,
       
   607  * *NOT* the token.
       
   608  */
       
   609 void CUnifiedCertStore::FindCertStoreL(const TCTTokenObjectHandle& aHandle)
       
   610 	{
       
   611 	assert(!iCurrentCertStore);
       
   612 	assert(!iCurrentWritableCertStore);
       
   613 		
       
   614 	if (!iIsInitialized)
       
   615 		{
       
   616 		User::Leave(KErrNotReady);
       
   617 		}
       
   618 
       
   619 	iCurrentCertStore = GetCertStore(aHandle);
       
   620 	
       
   621 	if (!iCurrentCertStore)
       
   622 		{
       
   623 		User::Leave(KErrNotFound);
       
   624 		}
       
   625 	}
       
   626 
       
   627 /**
       
   628  * Set iCurrentWritableCertStore to the writable certstore containing a given
       
   629  * certificate, or leave if it could not be found.  The handle is the handle of
       
   630  * the *certinfo*, *NOT* the token.
       
   631  */
       
   632 void CUnifiedCertStore::FindWritableCertStoreL(const TCTTokenObjectHandle& aHandle)
       
   633 	{
       
   634 	assert(!iCurrentCertStore);
       
   635 	assert(!iCurrentWritableCertStore);
       
   636 		
       
   637 	if (!iIsInitialized)
       
   638 		{
       
   639 		User::Leave(KErrNotReady);
       
   640 		}
       
   641 
       
   642 	if (!iOpenedForWrite)
       
   643 		{
       
   644 		User::Leave(KErrAccessDenied);
       
   645 		}
       
   646 
       
   647 	iCurrentWritableCertStore = NULL;
       
   648 	TInt count = iWritableCertStores.Count();
       
   649 	for (TInt i = 0; i < count; i++)
       
   650 		{
       
   651 		MCTWritableCertStore* certstore = iWritableCertStores[i];
       
   652 		MCTToken& token = certstore->Token();
       
   653 		if (token.Handle() == aHandle.iTokenHandle)
       
   654 			{
       
   655 			iCurrentWritableCertStore = certstore;
       
   656 			break;
       
   657 			}
       
   658 		}
       
   659 
       
   660 	if (!iCurrentWritableCertStore)
       
   661 		{
       
   662 		User::Leave(KErrNotFound);
       
   663 		}
       
   664 	}
       
   665 
       
   666 EXPORT_C TInt CUnifiedCertStore::CertStoreCount() const
       
   667 	{
       
   668 	return iCertStores.Count();
       
   669 	}
       
   670 
       
   671 EXPORT_C MCTCertStore& CUnifiedCertStore::CertStore(TInt aIndex)
       
   672 	{
       
   673 	assert(aIndex < iCertStores.Count());
       
   674 	return *iCertStores[aIndex];
       
   675 	}
       
   676 
       
   677 EXPORT_C TInt CUnifiedCertStore::WritableCertStoreCount() const
       
   678 	{
       
   679 	return iWritableCertStores.Count();
       
   680 	}
       
   681 
       
   682 EXPORT_C MCTWritableCertStore& CUnifiedCertStore::WritableCertStore(TInt aIndex)
       
   683 	{
       
   684 	assert(aIndex < iWritableCertStores.Count());
       
   685 	return *iWritableCertStores[aIndex];
       
   686 	}
       
   687 
       
   688 EXPORT_C TInt CUnifiedCertStore::ReadOnlyCertStoreCount() const
       
   689 	{
       
   690 	return iReadOnlyCertStores.Count();
       
   691 	}
       
   692 
       
   693 EXPORT_C MCTCertStore& CUnifiedCertStore::ReadOnlyCertStore(TInt aIndex)
       
   694 	{
       
   695 	assert(aIndex < iReadOnlyCertStores.Count());
       
   696 	return *iReadOnlyCertStores[aIndex];
       
   697 	}
       
   698 
       
   699 CUnifiedCertStore::CUnifiedCertStore(RFs& aFs, TBool aOpenForWrite)
       
   700 	: CActive(EPriorityNormal), iFs(aFs), iOpenedForWrite(aOpenForWrite), iOrderAttributes()
       
   701 	{
       
   702 	CActiveScheduler::Add(this);
       
   703 	assert(IsAdded());
       
   704 	}
       
   705 
       
   706 void CUnifiedCertStore::ConstructL(RArray<TInt>& aOrderFilter)
       
   707 	{
       
   708 
       
   709  	for (TInt i=0;i<aOrderFilter.Count();i++)
       
   710  		{
       
   711  		User::LeaveIfError(iOrderAttributes.Append(aOrderFilter[i]));	
       
   712  		}
       
   713 	}
       
   714 
       
   715 void CUnifiedCertStore::RunL()
       
   716 	{
       
   717 	if ((iState != EInitializeGetReadableInterface) &&
       
   718 		(iState != EInitializeGetReadableInterfaceFinished) &&
       
   719 		(iState != EInitializeGetToken)) // We don't want to leave if we're in this state
       
   720 										 // since we want to enumerate all tokens, see below
       
   721 		{
       
   722 		User::LeaveIfError(iStatus.Int());
       
   723 		}
       
   724 
       
   725 	switch (iState)
       
   726 		{
       
   727 		case EInitializeGetTokenList:
       
   728 			// We need to try to get a list of Tokens for each of the Token Types
       
   729 			iWorkingVars->iIndex++;
       
   730 			TInt end;
       
   731 			if (!iCurrentlyDoingReadOnly)
       
   732 				{
       
   733 				end = iWorkingVars->iWritableTokenTypes.Count();
       
   734 				}
       
   735 			else
       
   736 				{
       
   737 				end = iWorkingVars->iReadOnlyTokenTypes.Count();
       
   738 				}
       
   739 			if (iWorkingVars->iIndex < end)
       
   740 				{
       
   741 				assert(!iTokenType);
       
   742 				TInt createRes = KErrNone;
       
   743 				if (iCurrentlyDoingReadOnly)
       
   744 					{
       
   745 					TRAP(createRes, iTokenType = MCTTokenType::NewL(*iWorkingVars->iReadOnlyTokenTypes[iWorkingVars->iIndex], iFs));
       
   746 					}
       
   747 				else
       
   748 					{
       
   749 					TRAP(createRes, iTokenType = MCTTokenType::NewL(*iWorkingVars->iWritableTokenTypes[iWorkingVars->iIndex], iFs));
       
   750 					}
       
   751 
       
   752 				if (KErrNoMemory==createRes)
       
   753 					{
       
   754 					// Leave if there's no memory, so OOM tests work
       
   755 					User::Leave(createRes);
       
   756 					}
       
   757 				else if (KErrNone!=createRes)
       
   758 					{
       
   759 					//	ECOM couldn't load that token type, don't give up, try the next...
       
   760 					TRequestStatus* stat = &iStatus;
       
   761 					User::RequestComplete(stat, KErrNone);
       
   762 					}
       
   763 				else
       
   764 					{
       
   765 					assert(iTokens.Count() == 0);
       
   766 					iTokenType->List(iTokens, iStatus);
       
   767 					iIndexTokens = -1;
       
   768 					iState = EInitializeGetToken;
       
   769 					}				   
       
   770 				}
       
   771 			else if (!iCurrentlyDoingReadOnly)
       
   772 				{
       
   773 				iCurrentlyDoingReadOnly = ETrue;
       
   774 				iWorkingVars->iIndex = -1;
       
   775 				TRequestStatus* status = &iStatus;
       
   776 				User::RequestComplete(status, KErrNone);
       
   777 				}
       
   778 			else
       
   779 				{
       
   780 				iState = EInitializeFinished;
       
   781 				TRequestStatus* status = &iStatus;
       
   782 				User::RequestComplete(status, KErrNone);
       
   783 				}
       
   784 			SetActive();
       
   785 			break;
       
   786 
       
   787 		case EInitializeGetToken:
       
   788 			if (iStatus.Int() == KErrHardwareNotAvailable) 
       
   789 				{
       
   790 				// If the hardware corresponding to this
       
   791 				// TokenType has been removed then just skip it
       
   792 				// but DO NOT leave!
       
   793 				++iIndexTokens;
       
   794 				iState = EInitializeGetToken;
       
   795 				TRequestStatus* status = &iStatus;
       
   796 				User::RequestComplete(status,KErrNone);
       
   797 				}
       
   798             else 
       
   799 				{
       
   800 				User::LeaveIfError(iStatus.Int());
       
   801 
       
   802 				// iIndexTokens is initialized at EInitializeGetTokenList
       
   803 				++iIndexTokens;
       
   804 
       
   805 				//	We need to try to get a certstore interface (readable or
       
   806 				//	writable) for each of the Tokens in iTokens		
       
   807 				if (iIndexTokens < iTokens.Count())
       
   808 					{
       
   809                     assert(!iToken);
       
   810 					iTokenType->OpenToken(*iTokens[iIndexTokens], iToken, iStatus);
       
   811 					if ((iOpenedForWrite) && !iCurrentlyDoingReadOnly)
       
   812 						{
       
   813 						iState = EInitializeGetWritableInterface;
       
   814 						}
       
   815 					else
       
   816 						{
       
   817 						iState = EInitializeGetReadableInterface;
       
   818 						}
       
   819 					}
       
   820 				else
       
   821 					{
       
   822 					// We don't need the iTokenType anymore
       
   823 					iTokenType->Release();
       
   824 					iTokenType = 0;
       
   825 					// We don't need the list of Tokens anymore
       
   826 					iTokens.Close();
       
   827 					iState = EInitializeGetTokenList;
       
   828 					TRequestStatus* status = &iStatus;
       
   829 					User::RequestComplete(status, KErrNone);
       
   830 					}
       
   831 				}
       
   832 			SetActive();
       
   833 			break;
       
   834 
       
   835 		case EInitializeGetWritableInterface:
       
   836 			{
       
   837 			User::LeaveIfError(iStatus.Int());
       
   838 			// First we try to get a writable interface to the store, if
       
   839 			// that doesn't work we will try to get a readable interface
       
   840             assert(iToken);
       
   841             assert(!iTokenInterface);
       
   842 			TUid uid = { KInterfaceWritableCertStore };
       
   843 			iToken->GetInterface(uid, iTokenInterface, iStatus);
       
   844 			iState = EInitializeGetReadableInterface;
       
   845 			SetActive();
       
   846 			}
       
   847 			break;
       
   848 
       
   849 		case EInitializeGetReadableInterface:
       
   850 			// We check if we managed to get a writable interface
       
   851 			if (iStatus == KErrNoMemory)
       
   852 				{
       
   853 				User::Leave(KErrNoMemory);
       
   854 				}
       
   855             
       
   856 			if (!iCurrentlyDoingReadOnly && iOpenedForWrite && (iStatus == KErrNone))
       
   857 				{
       
   858 				assert(iTokenInterface);
       
   859 
       
   860 				//	Drop the interface into a "writable checking" object
       
   861 				CCheckedCertStore* interf = 
       
   862 					CCheckedCertStore::NewCheckedWritableCertStoreL(iTokenInterface, iPSCertstoreChangeProperty);
       
   863 
       
   864                 CleanupReleasePushL(*interf);
       
   865 				iTokenInterface = 0;
       
   866 				
       
   867 				User::LeaveIfError(iWritableCertStores.Append(interf));
       
   868 				CleanupStack::Pop();
       
   869 				
       
   870 				User::LeaveIfError(iCertStores.Append(interf));	
       
   871 				
       
   872 				// We don't need the Token anymore
       
   873 				iToken->Release();
       
   874 				iToken = 0;
       
   875 				iState = EInitializeGetToken;
       
   876 				TRequestStatus* status = &iStatus;
       
   877 				User::RequestComplete(status, KErrNone);
       
   878 				}
       
   879 			else 
       
   880 				{
       
   881 				// We do the check only if we were not trying to get a Writeable Interface
       
   882 				// before, if we trying to get a writeable interface before, we know that we
       
   883 				// have a valid iToken.
       
   884 				if ((iCurrentlyDoingReadOnly || !iOpenedForWrite) && (iStatus != KErrNone))
       
   885 					{
       
   886 					User::Leave(iStatus.Int());
       
   887 					}
       
   888 				else
       
   889 					{
       
   890 					assert(iToken);
       
   891                     assert(!iTokenInterface);
       
   892 					TUid uid = { KInterfaceCertStore };
       
   893 					iToken->GetInterface(uid, iTokenInterface, iStatus);
       
   894 					iState = EInitializeGetReadableInterfaceFinished;
       
   895 					}
       
   896 				}
       
   897 			SetActive();
       
   898 			break;
       
   899 
       
   900 		case EInitializeGetReadableInterfaceFinished:
       
   901 			{
       
   902 			if (iStatus == KErrNoMemory)
       
   903 				{
       
   904 				User::Leave(KErrNoMemory);
       
   905 				}
       
   906             
       
   907 			if (iStatus == KErrNone)
       
   908 				{
       
   909 				assert(iTokenInterface);
       
   910 
       
   911 				//	Drop the interface into a "read only checking" object
       
   912                 CCheckedCertStore* interf = 
       
   913 						CCheckedCertStore::NewCheckedCertStoreL(iTokenInterface, iPSCertstoreChangeProperty);
       
   914 				
       
   915 				CleanupReleasePushL(*interf);
       
   916 				iTokenInterface = 0;
       
   917 
       
   918 				User::LeaveIfError(iReadOnlyCertStores.Append(interf));
       
   919 				CleanupStack::Pop(interf);
       
   920                 
       
   921 				User::LeaveIfError(iCertStores.Append(interf));
       
   922 				}			
       
   923 				
       
   924 			// We don't need the Token anymore
       
   925 			iToken->Release();
       
   926 			iToken = 0;
       
   927 			
       
   928 			iStatus = KErrNone;
       
   929 			iState = EInitializeGetToken;
       
   930 			TRequestStatus* status = &iStatus;
       
   931 			User::RequestComplete(status, iStatus.Int());
       
   932 			SetActive();
       
   933             }
       
   934 			break;
       
   935 
       
   936 		case EInitializeFinished:
       
   937             assert(!iTokenType);
       
   938             assert(!iToken);
       
   939             assert(!iTokenInterface);
       
   940 			iIsInitialized = ETrue;
       
   941 			Complete(iStatus.Int());
       
   942 			break;
       
   943 
       
   944 		case EList:
       
   945 			// iIndex has been initialized in List
       
   946 			iIndex++;
       
   947 			iCurrentCertStore = NULL;
       
   948 			if (iIndex < iCertStores.Count())
       
   949 				{
       
   950 				iCurrentCertStore = iCertStores[iIndex];
       
   951 				iCurrentCertStore->List(*iWorkingVars->iCertInfos, *iWorkingVars->iFilter, iStatus);
       
   952 				iWorkingVars->iCertIndex = 0;
       
   953 				SetActive();
       
   954 				}
       
   955 			else if (iWorkingVars->iIssuerNames.Count() > 0)
       
   956 				{
       
   957 				// We have an issuer name. We now remove all certs
       
   958 				// that don't match that issuer.
       
   959 
       
   960 				// If this is the first time in here, we need to parse
       
   961 				// and hash all the issuer names.
       
   962 				if (iWorkingVars->iParsedIssuerNames.Count() == 0)
       
   963 					{
       
   964 					CSHA1* sha1 = CSHA1::NewL();
       
   965 					CleanupStack::PushL(sha1);
       
   966 					TInt count = iWorkingVars->iIssuerNames.Count();
       
   967 					for (TInt i = 0; i < count; i++)
       
   968 						{
       
   969 						CX500DistinguishedName* dn = 
       
   970 							CX500DistinguishedName::NewLC(*iWorkingVars->
       
   971 														  iIssuerNames[i]);
       
   972 						User::LeaveIfError(
       
   973 							iWorkingVars->iParsedIssuerNames.Append(dn));
       
   974 						CleanupStack::Pop(dn);
       
   975 						TPtrC8 hash=sha1->Hash(*iWorkingVars->iIssuerNames[i]);
       
   976 						User::LeaveIfError(
       
   977 							iWorkingVars->iHashedIssuerNames.Append(
       
   978 								hash.AllocLC()));
       
   979 						CleanupStack::Pop();
       
   980 						}
       
   981 					CleanupStack::PopAndDestroy();
       
   982 					}
       
   983 
       
   984 				while (iWorkingVars->iCertIndex <
       
   985 					   iWorkingVars->iCertInfos->Count())
       
   986 					{
       
   987 					CCTCertInfo* info = 
       
   988 						(*iWorkingVars->iCertInfos)[iWorkingVars->iCertIndex];
       
   989 					TCompareResults res = CompareCertInfoDN(info);
       
   990 					if (res == EYes)
       
   991 						{
       
   992 						// It matches. leave it for the next one.
       
   993 						iWorkingVars->iCertIndex++;
       
   994 						}
       
   995 					else if (res == ENo)
       
   996 						{
       
   997 						// It doesn't match. Remove it and try the next one.
       
   998 						info->Release();
       
   999 						iWorkingVars->iCertInfos->
       
  1000 							Remove(iWorkingVars->iCertIndex);
       
  1001 						}
       
  1002 					else // res == EMaybe
       
  1003 						{
       
  1004 						// Need to load the cert and properly compare the DNs.
       
  1005 						iCurrentCertStore = GetCertStore(info->Handle());
       
  1006 						assert(iCurrentCertStore);
       
  1007 						
       
  1008 						iWorkingVars->iCertDesC=HBufC8::NewMaxL(info->Size());
       
  1009 						iWorkingVars->iCertType = info->CertificateFormat();
       
  1010 						iState = ERetrieveForList;
       
  1011 						iWorkingVars->iCertDes.Set(iWorkingVars->iCertDesC->Des());
       
  1012 						iCurrentCertStore->Retrieve(*info, iWorkingVars->iCertDes, iStatus);
       
  1013 						SetActive();
       
  1014 						return;
       
  1015 						}
       
  1016 					}
       
  1017 				Complete(KErrNone);
       
  1018 				}
       
  1019 			else
       
  1020 				{
       
  1021 				Complete(KErrNone);
       
  1022 				}
       
  1023 			break;
       
  1024 
       
  1025 	case ERetrieve:
       
  1026 		{
       
  1027 		switch (iWorkingVars->iCertType)
       
  1028 			{
       
  1029             case EX509Certificate:
       
  1030                 {
       
  1031                 TPtr8 theCert(iWorkingVars->iCertDesC->Des());
       
  1032                 *(iWorkingVars->iReturnedCert) = CX509Certificate::NewL(theCert);
       
  1033                 }
       
  1034                 break;
       
  1035             case EWTLSCertificate:
       
  1036                 {
       
  1037                 TPtr8 theCert(iWorkingVars->iCertDesC->Des());
       
  1038                 *(iWorkingVars->iReturnedCert) = CWTLSCertificate::NewL(theCert);			
       
  1039                 }
       
  1040                 break;
       
  1041             default:
       
  1042                 assert(EFalse);
       
  1043                 break;
       
  1044 			}
       
  1045 		Complete(KErrNone);
       
  1046 		}
       
  1047 		break;
       
  1048 
       
  1049 	case ERetrieveForList:
       
  1050 		{
       
  1051 		TPtr8 theCert(iWorkingVars->iCertDesC->Des());
       
  1052 		CX509Certificate* cert=CX509Certificate::NewLC(theCert);
       
  1053 		if (MatchL(cert->IssuerName()))
       
  1054 			{
       
  1055 			// It matches. leave it for the next one.
       
  1056 			iWorkingVars->iCertIndex++;
       
  1057 			}
       
  1058 		else
       
  1059 			{
       
  1060 			// It doesn't match. Remove it and try the next one.
       
  1061 			(*iWorkingVars->iCertInfos)[iWorkingVars->iCertIndex]->Release();
       
  1062 			iWorkingVars->iCertInfos->Remove(iWorkingVars->iCertIndex);
       
  1063 			}
       
  1064 		CleanupStack::PopAndDestroy(cert);
       
  1065 		delete iWorkingVars->iCertDesC;
       
  1066 		iWorkingVars->iCertDesC = 0;
       
  1067 		iState = EList;
       
  1068 		SetActive();
       
  1069 		TRequestStatus* status = & iStatus;
       
  1070 		User::RequestComplete(status, KErrNone);
       
  1071 		break;
       
  1072 		}
       
  1073 		
       
  1074  	case ERemove:
       
  1075  	case ESetApplicability:
       
  1076  	case ESetTrust:
       
  1077 	case EGetCert:
       
  1078 	case EApplications:
       
  1079 	case EIsApplicable:
       
  1080 	case ETrusted:
       
  1081 	case ERetrieveData:
       
  1082 		Complete(KErrNone);
       
  1083 		break;
       
  1084 
       
  1085 	default:
       
  1086         User::Panic(KUCSPanic, 1);
       
  1087 		break;
       
  1088 		}
       
  1089 	}
       
  1090 
       
  1091 TInt CUnifiedCertStore::RunError(TInt aError)
       
  1092 	{
       
  1093 	Complete(aError);
       
  1094 	return KErrNone;
       
  1095 	}
       
  1096 
       
  1097 void CUnifiedCertStore::DoCancel()
       
  1098 	{
       
  1099 	// If the current state is the last state involved in handling a request, we
       
  1100 	// check to see if we have already been completed - in this case we can
       
  1101 	// simply complete the client with iStatus (this may be KErrNone).  If we
       
  1102 	// have not we cancel the outstanding request and pass the resulting iStatus
       
  1103 	// back to the client - this too may indicate a successful completion if the
       
  1104 	// cancel arrived after the request was executed.
       
  1105 	//
       
  1106 	// For more complex cases, where there are more states to go through before
       
  1107 	// we finish servicing the client request, we cancel any outstanding
       
  1108 	// request, and return KErrCancel to the client.
       
  1109 
       
  1110 	switch (iState)
       
  1111 		{
       
  1112 		case EInitializeFinished:
       
  1113 		case ERetrieve:
       
  1114 		case EGetCert:
       
  1115 		case EApplications:
       
  1116 		case EIsApplicable:
       
  1117 		case ETrusted:
       
  1118 		case ERetrieveData:
       
  1119 		case ERemove:
       
  1120 		case ESetApplicability:
       
  1121 		case ESetTrust:		
       
  1122 			if (iStatus == KRequestPending)
       
  1123 				{
       
  1124 				// Attempt to cancel outstanding request and pass status back to
       
  1125 				// client
       
  1126 				CancelOutstandingRequest();
       
  1127 				Complete(iStatus.Int());
       
  1128 				}
       
  1129 			else
       
  1130 				{
       
  1131 				// We've already been completed - call RunL() to process results
       
  1132 				// and complete client
       
  1133 				TRAPD(err, RunL());
       
  1134 				if (err != KErrNone)
       
  1135 					{
       
  1136 					RunError(err);
       
  1137 					}
       
  1138 				}
       
  1139 			break;
       
  1140 			
       
  1141 		default:
       
  1142 			CancelOutstandingRequest();
       
  1143 			Complete(KErrCancel);
       
  1144 			break;
       
  1145 		}
       
  1146 	}
       
  1147 
       
  1148 void CUnifiedCertStore::CancelOutstandingRequest()
       
  1149 	{
       
  1150 	switch (iState)
       
  1151 		{
       
  1152 		case EInitializeGetTokenList:
       
  1153 		case EInitializeGetToken:
       
  1154 		case EInitializeGetWritableInterface:
       
  1155 		case EInitializeGetReadableInterface:
       
  1156 		case EInitializeGetReadableInterfaceFinished:
       
  1157 		case EInitializeFinished:
       
  1158 			// Don't have to cancel initialisation stuff - this happens when we
       
  1159 			// release the objects in DestroyTemporaryMembers().
       
  1160 			iStatus = KErrCancel;
       
  1161 			break;
       
  1162 
       
  1163 		case EList:
       
  1164 			if (iCurrentCertStore)
       
  1165 				{
       
  1166 				iCurrentCertStore->CancelList();
       
  1167 				}
       
  1168 			break;
       
  1169 			
       
  1170 		case ERetrieve:
       
  1171 		case ERetrieveForList:
       
  1172 		case ERetrieveData:
       
  1173 			assert(iCurrentCertStore);
       
  1174 			iCurrentCertStore->CancelRetrieve();
       
  1175 			break;
       
  1176 			
       
  1177 		case EGetCert:
       
  1178 			assert(iCurrentCertStore);
       
  1179 			iCurrentCertStore->CancelGetCert();
       
  1180 			break;
       
  1181 			
       
  1182 		case EApplications:
       
  1183 			assert(iCurrentCertStore);
       
  1184 			iCurrentCertStore->CancelApplications();
       
  1185 			break;
       
  1186 			
       
  1187 		case EIsApplicable:
       
  1188 			assert(iCurrentCertStore);
       
  1189 			iCurrentCertStore->CancelIsApplicable();
       
  1190 			break;
       
  1191 			
       
  1192 		case ETrusted:
       
  1193 			assert(iCurrentCertStore);
       
  1194 			iCurrentCertStore->CancelTrusted();
       
  1195 			break;
       
  1196 			
       
  1197 		case ERemove:
       
  1198 			assert(iCurrentWritableCertStore);
       
  1199 			iCurrentWritableCertStore->CancelRemove();
       
  1200 			break;
       
  1201 			
       
  1202 		case ESetApplicability:
       
  1203 			assert(iCurrentWritableCertStore);
       
  1204 			iCurrentWritableCertStore->CancelSetApplicability();
       
  1205 			break;
       
  1206 			
       
  1207 		case ESetTrust:
       
  1208 			assert(iCurrentWritableCertStore);
       
  1209 			iCurrentWritableCertStore->CancelSetTrust();
       
  1210 			break;
       
  1211 			
       
  1212 		default:
       
  1213 			User::Panic(KUCSPanic, 1);
       
  1214 			break;
       
  1215 		}
       
  1216 	}
       
  1217 
       
  1218 TBool CUnifiedCertStore::MatchL(const CX500DistinguishedName& aName) const
       
  1219 	{
       
  1220 	// Return true if the supplied DN is the same as any of the supplied DNs.
       
  1221 	TInt count = iWorkingVars->iIssuerNames.Count();
       
  1222 	for (TInt i = 0; i < count; i++)
       
  1223 		{
       
  1224 		if (aName.ExactMatchL(*iWorkingVars->iParsedIssuerNames[i]))
       
  1225 			return ETrue;
       
  1226 		}
       
  1227 	return EFalse;
       
  1228 	}
       
  1229 
       
  1230 void CUnifiedCertStore::AllocWorkingVarsL()
       
  1231 	{
       
  1232 	assert(!iWorkingVars);
       
  1233 	iWorkingVars = new (ELeave) CUnifiedCertStoreWorkingVars;
       
  1234 	}
       
  1235 
       
  1236 void CUnifiedCertStore::BeginAsyncOp(TRequestStatus& aStatus, TState aState)
       
  1237 	{
       
  1238 	assert(iState == EIdle);
       
  1239 	assert(!iClientStatus);
       
  1240 	
       
  1241 	iClientStatus = &aStatus;
       
  1242 	*iClientStatus = KRequestPending;
       
  1243 	iState = aState;
       
  1244 	}
       
  1245 
       
  1246 void CUnifiedCertStore::Complete(TInt aError)
       
  1247 	{
       
  1248 	assert(iClientStatus);
       
  1249 	User::RequestComplete(iClientStatus, aError);	
       
  1250 	DestroyTemporaryMembers();
       
  1251 	iState = EIdle;
       
  1252 	}
       
  1253 
       
  1254 void CUnifiedCertStore::DestroyTemporaryMembers()
       
  1255 	{
       
  1256 	if (!iIsInitialized)
       
  1257 		{
       
  1258 		TInt end = iReadOnlyCertStores.Count();
       
  1259 		TInt i;
       
  1260 		for (i = 0; i < end; i++)
       
  1261 			{
       
  1262 			iReadOnlyCertStores[i]->Release();
       
  1263 			}
       
  1264 		iReadOnlyCertStores.Close();
       
  1265 
       
  1266 		end = iWritableCertStores.Count();
       
  1267 		for (i = 0; i < end; i++)
       
  1268 			{
       
  1269 			iWritableCertStores[i]->Release();
       
  1270 			}
       
  1271 		iWritableCertStores.Close();
       
  1272 
       
  1273 		// The elements are already released by the two loops above
       
  1274 		iCertStores.Close();
       
  1275 		}
       
  1276 
       
  1277     if (iTokenType)
       
  1278 		{
       
  1279 		iTokenType->Release();
       
  1280 		iTokenType = 0;
       
  1281 		}
       
  1282 
       
  1283 	if (iToken)
       
  1284 		{
       
  1285 		iToken->Release();
       
  1286 		iToken = 0;
       
  1287 		}
       
  1288 
       
  1289 	if (iTokenInterface)
       
  1290 		{
       
  1291 		iTokenInterface->Release();
       
  1292 		iTokenInterface = 0;
       
  1293 		}
       
  1294 
       
  1295 	iTokens.Close();
       
  1296 
       
  1297 	delete iWorkingVars;
       
  1298 	iWorkingVars = 0;
       
  1299 
       
  1300 	iCurrentCertStore = NULL;
       
  1301 	iCurrentWritableCertStore = NULL;
       
  1302 	}
       
  1303 
       
  1304 CUnifiedCertStore::TCompareResults 
       
  1305 CUnifiedCertStore::CompareCertInfoDN(const CCTCertInfo* aCertInfo)
       
  1306 	{
       
  1307 	if (aCertInfo->IssuerHash() && 
       
  1308 		(aCertInfo->CertificateFormat() == EX509CertificateUrl ||
       
  1309 			iHardwareTypeUids.Find(aCertInfo->Token().TokenType().Type()) != 
       
  1310 		 KErrNotFound))
       
  1311 		{
       
  1312 		TInt count = iWorkingVars->iHashedIssuerNames.Count();
       
  1313 		for (TInt i = 0; i < count; i++)
       
  1314 			{
       
  1315 			if (*aCertInfo->IssuerHash()==*iWorkingVars->iHashedIssuerNames[i])
       
  1316 				return EYes;
       
  1317 			}
       
  1318 		return ENo;
       
  1319 		}
       
  1320 	if (aCertInfo->CertificateFormat() != EX509Certificate)
       
  1321 		return ENo;
       
  1322 	return EMaybe;
       
  1323 	}
       
  1324 
       
  1325 EXPORT_C CUnifiedCertStore* CUnifiedCertStore::NewL(RFs& aFs, 
       
  1326                                                      TBool aOpenForWrite,
       
  1327                                                      RArray<TInt>& aOrderFilter)
       
  1328  	{
       
  1329  	CUnifiedCertStore* self = CUnifiedCertStore::NewLC(aFs, 
       
  1330  	                                                   aOpenForWrite, 
       
  1331  	                                                   aOrderFilter);
       
  1332  	CleanupStack::Pop(self);
       
  1333  	return self;
       
  1334  	}
       
  1335 
       
  1336 EXPORT_C CUnifiedCertStore* CUnifiedCertStore::NewLC(RFs& aFs, 
       
  1337                                                       TBool aOpenForWrite,
       
  1338                                                       RArray<TInt>& aOrderFilter)
       
  1339  	{
       
  1340  	CUnifiedCertStore* self = new(ELeave) CUnifiedCertStore(aFs, 
       
  1341  	                                                        aOpenForWrite);
       
  1342  	CleanupStack::PushL(self);
       
  1343  	self->ConstructL(aOrderFilter);
       
  1344  	return self;
       
  1345  	}
       
  1346 
       
  1347 void CUnifiedCertStore::FilterTokenTypesL(RCPointerArray<CCTTokenTypeInfo>& aSearchTokenTypes,
       
  1348                                          RCPointerArray<CCTTokenTypeInfo>& aTempTokenTypes,
       
  1349 										 TInt aOrderAttribute)
       
  1350 	{
       
  1351 	//We allow aOrderAttribute=KUnknownHardwareCertStore here to keep DC.
       
  1352 	//assert(aOrderAttribute);
       
  1353 	
       
  1354 	// Get number of token types
       
  1355 	TInt tokenTypesCount = aSearchTokenTypes.Count(); 
       
  1356 	
       
  1357 	// loop through token types
       
  1358 	for(TInt tokenTypesLoop = tokenTypesCount-1; tokenTypesLoop >= 0; tokenTypesLoop--)
       
  1359 		{
       
  1360 		// get the list of attributes supported by this token type.
       
  1361 		// Note: The attribute list consists of values such as
       
  1362 		// KCTSoftware defined in TCTTokenTypeAttribute.h
       
  1363 		const RArray<TCTTokenTypeAttribute>& attributesList = 
       
  1364 			aSearchTokenTypes[tokenTypesLoop]->Attributes();
       
  1365 		
       
  1366 		// Get the number of attributes in the attribute list.
       
  1367 		// The number of attributes will match the ECOM resource
       
  1368 		// file definition. E.g. see 101f5015.rss for the software
       
  1369 		// implementation of certstore. 
       
  1370 		TInt attributeCount = attributesList.Count();
       
  1371 		
       
  1372 		// Check each attribute in the attribute list
       
  1373 		for(TInt attribLoop = 0; attribLoop < attributeCount; attribLoop++)
       
  1374 			{
       
  1375 			// Check whether attribute in the list matches an order attribute
       
  1376 			// E.g. KCTSoftware
       
  1377 			if(attributesList[attribLoop].iUID == KCTSoftware
       
  1378 				&& attributesList[attribLoop].iVal == aOrderAttribute)
       
  1379 				{
       
  1380 				// Found the attribute of interest. Add token type to the temp container. 
       
  1381 				User::LeaveIfError(aTempTokenTypes.Append(aSearchTokenTypes[tokenTypesLoop]));
       
  1382 				// Remove from the Searchlist.
       
  1383 				aSearchTokenTypes.Remove(tokenTypesLoop);
       
  1384 				// No need to examine the other attributes, so break loop
       
  1385 				break;
       
  1386 				}
       
  1387 			}
       
  1388 		}                      
       
  1389 	}
       
  1390  	
       
  1391 void CUnifiedCertStore::ApplyOrderingL(RCPointerArray<CCTTokenTypeInfo>& aTokenTypes)
       
  1392 	{
       
  1393 	// Number of attributes in ordering filter
       
  1394 	TInt numOrderAttributes=iOrderAttributes.Count();
       
  1395 	assert(numOrderAttributes>0);
       
  1396 	
       
  1397 	// Contains writable tokens types
       
  1398 	RCPointerArray<CCTTokenTypeInfo> tempWritableTokenTypes;
       
  1399 	CleanupClosePushL(tempWritableTokenTypes);
       
  1400 	
       
  1401 	// Contains read-only tokens types
       
  1402 	RCPointerArray<CCTTokenTypeInfo> tempReadOnlyTokenTypes;
       
  1403 	CleanupClosePushL(tempReadOnlyTokenTypes);
       
  1404 		
       
  1405 	// For each order attribute, order the token types
       
  1406 	for(TInt attributeLoop = 0; attributeLoop < numOrderAttributes; attributeLoop++)
       
  1407 		{
       
  1408 		// Get ordering attribute Uid from Order filter
       
  1409 		TInt orderAttribute = iOrderAttributes[attributeLoop];
       
  1410 		
       
  1411 		// Order for writable token types
       
  1412 		FilterTokenTypesL(iWorkingVars->iWritableTokenTypes,
       
  1413 			tempWritableTokenTypes,
       
  1414 			orderAttribute);
       
  1415 		
       
  1416 		// Order for read-only token types
       
  1417 		FilterTokenTypesL(aTokenTypes,
       
  1418 			tempReadOnlyTokenTypes,
       
  1419 			orderAttribute);
       
  1420 		}
       
  1421 		
       
  1422 	// release and close the resources so can refill container, and get rid of 
       
  1423 	// the TokenType which have been filtered out.
       
  1424 	TInt tokenTypesCount = iWorkingVars->iWritableTokenTypes.Count();
       
  1425 	TInt i;	
       
  1426 	for(i = tokenTypesCount-1; i >= 0 ;i--)
       
  1427 		{
       
  1428 		if (iWorkingVars->iWritableTokenTypes[i])
       
  1429 			{
       
  1430 			CCTTokenTypeInfo* ptr=iWorkingVars->iWritableTokenTypes[i];
       
  1431 			iWorkingVars->iWritableTokenTypes.Remove(i);
       
  1432 			delete ptr;			
       
  1433 			}
       
  1434 		}
       
  1435 	iWorkingVars->iWritableTokenTypes.Reset();
       
  1436 	
       
  1437 	// release and close the resources so can refill container, and get rid of 
       
  1438 	// the TokenType which have been filtered out.
       
  1439 	tokenTypesCount = aTokenTypes.Count();
       
  1440 	for(i = tokenTypesCount-1; i >= 0 ;i--)
       
  1441 		{
       
  1442 		if (aTokenTypes[i])
       
  1443 			{
       
  1444 			CCTTokenTypeInfo* ptr=aTokenTypes[i];
       
  1445 			aTokenTypes.Remove(i);
       
  1446 			delete ptr;			
       
  1447 			}
       
  1448 		}
       
  1449 	aTokenTypes.Reset();
       
  1450 	
       
  1451 	// Assign contents of temp token types to containers. 
       
  1452 	// Note: temp tokens types are ordered according to user specification
       
  1453 	tokenTypesCount = tempWritableTokenTypes.Count();
       
  1454 	for(i = 0; i < tokenTypesCount; i++)
       
  1455 		{
       
  1456 		User::LeaveIfError(iWorkingVars->iWritableTokenTypes.Append(tempWritableTokenTypes[i]));
       
  1457 		tempWritableTokenTypes[i] = NULL; 
       
  1458 		}
       
  1459 		
       
  1460 	tokenTypesCount = tempReadOnlyTokenTypes.Count();
       
  1461 	for(i = 0; i < tokenTypesCount; i++)
       
  1462 		{
       
  1463 		User::LeaveIfError(aTokenTypes.Append(tempReadOnlyTokenTypes[i]));
       
  1464 		tempReadOnlyTokenTypes[i] = NULL;
       
  1465 		}
       
  1466 	
       
  1467 	CleanupStack::PopAndDestroy(2);	// tempReadOnlyTokenTypes, tempWritableTokenTypes, 
       
  1468 	}