vpnengine/pkiservice/src/pkisupport.cpp
changeset 0 33413c0669b9
child 1 c9c2ad51f972
equal deleted inserted replaced
-1:000000000000 0:33413c0669b9
       
     1 /*
       
     2 * Copyright (c) 2003-2008 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 "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 * CPKISupport class implements the PKI interface for Symbian Certificate 
       
    16 * storage.
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 
       
    22 #include <securitydefs.h>
       
    23 #include <cctcertinfo.h>
       
    24 #include <x509cert.h>
       
    25 #include <ccertattributefilter.h>
       
    26 #include <asymmetric.h>
       
    27 #include <unifiedcertstore.h>
       
    28 #include <mctwritablecertstore.h>
       
    29 
       
    30 #include "pkiwrapper.h"
       
    31 #include "PKIMapper.h"
       
    32 #include "pkisupport.h"
       
    33 #include "pkisession.h"
       
    34 #include "log_r6.h"
       
    35 #include "pkiserviceassert.h"
       
    36 #include "pkiserviceconstants.h"
       
    37 #include <vpnlogmessages.rsg>
       
    38 
       
    39 
       
    40 CPKISupport* CPKISupport::NewLC(CPKIMapper& aMapper, CPKIWrapper& aWrapper)
       
    41     {
       
    42     CPKISupport* self = new (ELeave)CPKISupport(aMapper, aWrapper);
       
    43     CleanupStack::PushL(self);
       
    44     self->ConstructL();
       
    45     return self;
       
    46     }
       
    47 
       
    48 CPKISupport* CPKISupport::NewL(CPKIMapper& aMapper, CPKIWrapper& aWrapper)
       
    49     {
       
    50     CPKISupport* self = CPKISupport::NewLC(aMapper, aWrapper);
       
    51     CleanupStack::Pop(self);
       
    52     return self;
       
    53     }
       
    54 
       
    55 void CPKISupport::ConstructL()
       
    56 	{
       
    57 	User::LeaveIfError(iEventMediator.Connect());
       
    58 	}
       
    59 
       
    60 CPKISupport::~CPKISupport()
       
    61     {
       
    62     LOG_("CPKISupport::~CPKISupport");
       
    63     Cancel();    
       
    64 
       
    65     if(iCerts != NULL)
       
    66         {
       
    67         iCerts->Close();
       
    68         delete iCerts;
       
    69         }
       
    70     delete iCertFilter;
       
    71     delete iCertStore;
       
    72     iFSession.Close();
       
    73     iApplUids.Close();
       
    74     delete iImportCertMapping;
       
    75     delete iImportCertData;
       
    76     iEventMediator.Close();
       
    77     delete iCertInfoForLogging; 
       
    78     iCertInfoForLogging = NULL;
       
    79     }
       
    80 
       
    81 CPKISupport::CPKISupport(CPKIMapper& aMapper, CPKIWrapper& aWrapper) 
       
    82     :CActive(CActive::EPriorityStandard), 
       
    83     iMapper(aMapper),
       
    84     iWrapper(aWrapper),
       
    85     iPending(ENoPendingOperation),
       
    86     iImportCertDataPtr(NULL, 0)
       
    87     {
       
    88     LOG_("-> CPKISupport::CPKISupport()\n");   
       
    89     CActiveScheduler::Add(this);
       
    90     
       
    91     SetCertStoreType(EPkiStoreTypeAny);
       
    92     LOG_("<- CPKISupport::CPKISupport()\n");
       
    93     } 
       
    94 
       
    95 // ---------------------------------------------------------------------------
       
    96 // StartInitializeL
       
    97 // ---------------------------------------------------------------------------
       
    98 //
       
    99 void  CPKISupport::StartInitializeL(const RMessage2& aMessage)
       
   100     {
       
   101     LOG_("-> CPKISupport::StartInitialize()");
       
   102     User::LeaveIfError(iFSession.Connect());
       
   103     iCertStore = CUnifiedCertStore::NewL(iFSession, ETrue);
       
   104     PKISERVICE_ASSERT(!iCerts);
       
   105     iCerts = new (ELeave) RMPointerArray<CCTCertInfo>;
       
   106     
       
   107     iMessage = aMessage;
       
   108     iCallerStatus = NULL;
       
   109     iSupportStatus = KErrNone;
       
   110 
       
   111     iInitState = EInitInitializeCertStore;
       
   112     iCertStore->Initialize(iStatus);
       
   113     iPending = EInitializeCertStore;
       
   114     SetActive();
       
   115     
       
   116     LOG_("<- CPKISupport::StartInitialize()");
       
   117     }
       
   118 
       
   119 
       
   120 // ---------------------------------------------------------------------------
       
   121 // FindInterfacesL
       
   122 // ---------------------------------------------------------------------------
       
   123 //
       
   124 void  CPKISupport::FindInterfacesL()
       
   125     {
       
   126     TInt i(0);
       
   127     
       
   128     // Find writable cert interface
       
   129     LOG(Log::Printf(_L("Find interfaces")));
       
   130 
       
   131     TBool allStoresFound(EFalse);
       
   132 
       
   133     MCTWritableCertStore* certStore;
       
   134 
       
   135     TInt storeCount = iCertStore->WritableCertStoreCount();
       
   136     LOG_1("Found %d cert stores\n", storeCount);
       
   137 
       
   138 
       
   139     // Find both user and device cert stores
       
   140     for(i = 0; i < storeCount; i++)
       
   141         {
       
   142         certStore= &(iCertStore->WritableCertStore(i));
       
   143 
       
   144         if(certStore->Token().Label().Compare(KUserCertStore) == 0)
       
   145             {
       
   146             // found the user cert store... See if device cert store 
       
   147             // was already found as well
       
   148             iWritableCertStore = certStore;
       
   149 
       
   150             LOG(Log::Printf(_L("Found writable user certificate store\n")));
       
   151             
       
   152             if (!allStoresFound) 
       
   153                 {
       
   154                 allStoresFound = ETrue;
       
   155                 }
       
   156             else 
       
   157                 {
       
   158                 break;
       
   159                 }
       
   160             }
       
   161         else if (certStore->Token().Label().Compare(KDeviceCertStore) == 0) 
       
   162             {
       
   163 
       
   164             // found the device cert store
       
   165             iWritableDeviceCertStore = certStore;
       
   166 
       
   167             LOG(Log::Printf(_L("Found writable device certificate store\n")));
       
   168             
       
   169             // See if user cert store was already found as well
       
   170             if (!allStoresFound) 
       
   171                 {
       
   172                 allStoresFound = ETrue;
       
   173                 }
       
   174             else 
       
   175                 {
       
   176                 break;
       
   177                 }
       
   178             }
       
   179         }
       
   180     
       
   181     if (iWritableDeviceCertStore == NULL) 
       
   182         {
       
   183         LOG(Log::Printf(_L("Note: Device cert store not found\n")));
       
   184         }
       
   185     }
       
   186 
       
   187 
       
   188 // ---------------------------------------------------------------------------
       
   189 // ContinueImportCTFObjectsL
       
   190 // ---------------------------------------------------------------------------
       
   191 //
       
   192 void  CPKISupport::ListAllCertificatesL()
       
   193 {
       
   194     LOG_("-> CPKISupport::ListAllCertificatesL");
       
   195 
       
   196     PKISERVICE_ASSERT( !iCertFilter );
       
   197     iCertFilter = CCertAttributeFilter::NewL();
       
   198     
       
   199     iSupportStatus = KErrNone;
       
   200     iInitState = EInitRetrieveCertList;
       
   201     iImportCounter = 0;
       
   202 	iToggleSwitch = EFalse;
       
   203 
       
   204     // Get list of all known certificates
       
   205     GetCertificateStoreListAsync();
       
   206     LOG_("<- CPKISupport::ListAllCertificatesL");
       
   207 }
       
   208 
       
   209     
       
   210     
       
   211 // ---------------------------------------------------------------------------
       
   212 // ImportCTFCertsL
       
   213 // ---------------------------------------------------------------------------
       
   214 //
       
   215 void  CPKISupport::ReadNextCertForImportL()
       
   216 {
       
   217     LOG_("-> CPKISupport::ImportNextCertL()");
       
   218     PKISERVICE_ASSERT(iImportCounter < iCerts->Count());
       
   219     
       
   220     if(iImportCounter == 0)
       
   221         {
       
   222         LOG_1("Import %d certificates\n", iCerts->Count());
       
   223         }
       
   224         
       
   225     LOG_1("Import certificate:%d", iImportCounter);
       
   226     LOG_1("Label: %S", &((*iCerts)[iImportCounter]->Label()));
       
   227     
       
   228     PKISERVICE_ASSERT((*iCerts)[iImportCounter]->CertificateFormat() == EX509Certificate);
       
   229 
       
   230     TSecurityObjectDescriptor descriptor;    
       
   231     descriptor.SetSubjectKeyId(
       
   232         (*iCerts)[iImportCounter]->SubjectKeyId());
       
   233     descriptor.SetOwnerType(
       
   234         (TPKICertificateOwnerType)(*iCerts)[iImportCounter]->
       
   235         CertificateOwnerType());
       
   236 	descriptor.SetIsDeletable(
       
   237 	    (*iCerts)[iImportCounter]->IsDeletable());
       
   238       
       
   239     delete iImportCertMapping;
       
   240     iImportCertMapping = NULL;
       
   241     iImportCertMapping = new (ELeave) CMapDescriptor(descriptor);
       
   242             
       
   243     iImportCertMapping->SetMapObjectName((*iCerts)[iImportCounter]->Label());
       
   244     
       
   245     delete iImportCertData;
       
   246     iImportCertData = NULL;
       
   247     iImportCertData = HBufC8::NewL((*iCerts)[iImportCounter]->Size());        
       
   248     iImportCertDataPtr.Set(iImportCertData->Des());
       
   249     
       
   250     LOG_(" Values set OK, retrieving");
       
   251     iCertStore->Retrieve(
       
   252         *(*iCerts)[iImportCounter], iImportCertDataPtr, iStatus);
       
   253     iPending = ECertRetrieve;
       
   254     SetActive();
       
   255     LOG_("<- CPKISupport::ImportCTFCertsL() SetActive() and ret: EFalse");
       
   256 }
       
   257 
       
   258 // ---------------------------------------------------------------------------
       
   259 // ImportOneCTFCertL
       
   260 // ---------------------------------------------------------------------------
       
   261 //
       
   262 void CPKISupport::SaveCertInfoToCacheL()
       
   263     {
       
   264     if((iImportCertMapping != NULL) && (iImportCertData != NULL))
       
   265         {
       
   266         LOG_("Saving cert info to cache");
       
   267        
       
   268         CX509Certificate* certificate = CX509Certificate::NewLC(iImportCertDataPtr);            
       
   269         const CSubjectPublicKeyInfo& publicKeyInfo = certificate->PublicKey();            
       
   270         const TPtrC8 keyData = publicKeyInfo.KeyData();
       
   271 
       
   272         TX509KeyFactory keyFactory;
       
   273         switch(publicKeyInfo.AlgorithmId())
       
   274             {
       
   275             case ERSA:
       
   276                 {                        
       
   277                 iImportCertMapping->iKeyAlgorithm = EPKIRSA;
       
   278                 const CRSAPublicKey* keyRSA = keyFactory.RSAPublicKeyL( keyData );
       
   279                 const TInteger&  n = keyRSA->N();
       
   280                 iImportCertMapping->iKeySize = n.BitCount();
       
   281                 delete keyRSA;                    
       
   282                 }
       
   283                 break;        	
       
   284             case EDSA:
       
   285                 {                        
       
   286                 iImportCertMapping->iKeyAlgorithm = EPKIDSA;
       
   287                 TPtrC8 params = publicKeyInfo.EncodedParams();
       
   288                 const CDSAPublicKey* keyDSA = keyFactory.DSAPublicKeyL( params, keyData );
       
   289                 const TInteger& y = keyDSA->Y();
       
   290                 iImportCertMapping->iKeySize = y.BitCount();
       
   291                 delete keyDSA;
       
   292                 }
       
   293                 break;
       
   294             default:
       
   295                 iImportCertMapping->iKeyAlgorithm = EPKIInvalidAlgorithm;
       
   296                 break;                    
       
   297             }                    
       
   298         CleanupStack::PopAndDestroy(certificate);                        
       
   299 
       
   300         iWrapper.SaveIdentityL(*iImportCertMapping,
       
   301                                iImportCertDataPtr,
       
   302                                ((*iCerts)[iImportCounter])->CertificateOwnerType());
       
   303 
       
   304         TInt storeType = (*iCerts)[iImportCounter]->Token().TokenType().Type().iUid;
       
   305         if ( storeType == STORETYPE_DEVICE_CERT_ID )
       
   306             {
       
   307             iImportCertMapping->SetCertStoreType(EPkiStoreTypeDevice);
       
   308             }
       
   309         else if ( storeType == STORETYPE_USER_CERT_ID )
       
   310             {
       
   311             iImportCertMapping->SetCertStoreType(EPkiStoreTypeUser);
       
   312             }
       
   313         else
       
   314             {
       
   315             iImportCertMapping->SetCertStoreType(EPkiStoreTypeAny);
       
   316             }
       
   317             
       
   318 		for(TInt i=0;i<iApplUids.Count();i++)
       
   319 			{
       
   320 			iImportCertMapping->iApplUids.Append(iApplUids[i]);
       
   321 			}
       
   322         User::LeaveIfError( 
       
   323             iMapper.AddMapping(*iImportCertMapping) );
       
   324 
       
   325         iApplUids.Close();            
       
   326             
       
   327         iImportCertMapping = NULL;    
       
   328         iImportCounter++;
       
   329         }    
       
   330     }
       
   331 
       
   332 // ---------------------------------------------------------------------------
       
   333 // GetApplicationsOfCTFCertL
       
   334 // ---------------------------------------------------------------------------
       
   335 //
       
   336 TBool  CPKISupport::GetApplicationsOfCTFCertL()
       
   337 {
       
   338     LOG_("-> CPKISupport::GetApplicationsOfCTFCertL()");
       
   339 	if((iImportCertMapping != NULL) && (iImportCertData != NULL))
       
   340 		{
       
   341 		if(iToggleSwitch == EFalse)
       
   342 			{
       
   343 			if((*iCerts)[iImportCounter]->CertificateOwnerType() == ECACertificate)
       
   344 				{
       
   345 				PKISERVICE_ASSERT(iApplUids.Count() == 0);
       
   346 				
       
   347 				iToggleSwitch = ETrue;
       
   348 				// Get applications				
       
   349 				iCertStore->Applications(*(*iCerts)[iImportCounter], iApplUids, iStatus);
       
   350 				iPending = EApplications;
       
   351                 LOG_("<- CPKISupport::GetApplicationsOfCTFCertL() SetActive(), ret: ETrue");
       
   352 				SetActive();
       
   353 				return ETrue;
       
   354 				}
       
   355 			else
       
   356 				{
       
   357                 LOG_("<- CPKISupport::GetApplicationsOfCTFCertL() Not a CA cert, ret: EFalse");
       
   358 				return EFalse;
       
   359 				}
       
   360 			}
       
   361 		else
       
   362 			{
       
   363             LOG_("<- CPKISupport::GetApplicationsOfCTFCertL() iToggleSwitch == ETrue, ret: EFalse");
       
   364 			iToggleSwitch = EFalse;
       
   365 			return EFalse;
       
   366 			}
       
   367 		}
       
   368 	else
       
   369 		{
       
   370 		return EFalse;
       
   371 		}
       
   372 }
       
   373 
       
   374 
       
   375 // ---------------------------------------------------------------------------
       
   376 // CancelCurrentOperation
       
   377 // ---------------------------------------------------------------------------
       
   378 //
       
   379 void CPKISupport::CancelCurrentOperation()
       
   380     {
       
   381     LOG_1("CPKISupport::CancelCurrentOperation iPending: %d", iPending);    
       
   382     MCTWritableCertStore* certStore(NULL);
       
   383     switch(iPending)
       
   384         {
       
   385         case ENoPendingOperation:
       
   386             // No pending operation 
       
   387             PKISERVICE_ASSERT( iStatus.Int() != KRequestPending);        
       
   388             break;
       
   389         case EInitializeCertStore:
       
   390             // FALLTROUGH
       
   391         case EListCerts:
       
   392             // FALLTROUGH
       
   393         case EListDevCerts:
       
   394             // FALLTROUGH
       
   395         case ECertRetrieve:
       
   396             // FALLTROUGH
       
   397         case ECertRemove:
       
   398             // FALLTROUGH
       
   399         case ESetTrust:
       
   400             // FALLTROUGH
       
   401         case ETrusted:
       
   402             // FALLTROUGH
       
   403         case ESetApplicability:
       
   404             // FALLTROUGH
       
   405         case EApplications:
       
   406             // FALLTROUGH
       
   407         case EIsApplicable:
       
   408             PKISERVICE_ASSERT( iCertStore );
       
   409             iCertStore->Cancel();
       
   410             break;
       
   411         case ECertAdd:
       
   412             if (iCertStoreType == EPkiStoreTypeDevice) 
       
   413                 {
       
   414                 LOG_("CPKISupport::CancelCurrentOperation cancelling device cert store op");
       
   415                 certStore = iWritableDeviceCertStore;
       
   416                 }
       
   417             else if (iCertStoreType == EPkiStoreTypeUser)
       
   418                 {
       
   419                 LOG_("CPKISupport::CancelCurrentOperation cancelling user cert store op");
       
   420                 certStore = iWritableCertStore;
       
   421                 }
       
   422             else 
       
   423                 {
       
   424                 LOG_("CPKISupport::CancelCurrentOperation cancelling user cert store op (unknown store type was defined)");
       
   425                 certStore = iWritableCertStore;
       
   426                 }
       
   427             PKISERVICE_ASSERT( certStore );
       
   428             certStore->CancelAdd();
       
   429             break;
       
   430         default:
       
   431             LOG_("CPKISupport::CancelCurrentOperation - default");
       
   432             break;           
       
   433         }
       
   434     }       
       
   435 
       
   436 // ---------------------------------------------------------------------------
       
   437 // ConvertPKIAlgorithm
       
   438 // ---------------------------------------------------------------------------
       
   439 //
       
   440 CCTKeyInfo::EKeyAlgorithm CPKISupport::ConvertPKIAlgorithm(TPKIKeyAlgorithm aAlg)
       
   441 {
       
   442     CCTKeyInfo::EKeyAlgorithm algorithm = CCTKeyInfo::EInvalidAlgorithm;
       
   443 
       
   444     switch(aAlg)
       
   445         {
       
   446         case EPKIRSA:
       
   447             algorithm = CCTKeyInfo::ERSA;
       
   448             break;
       
   449         case EPKIDSA:
       
   450             algorithm = CCTKeyInfo::EDSA;
       
   451             break;
       
   452         case EPKIDH:
       
   453             algorithm = CCTKeyInfo::EDH;
       
   454             break;
       
   455         default:
       
   456             break;
       
   457         }
       
   458     return algorithm;
       
   459 }
       
   460 
       
   461 // ---------------------------------------------------------------------------
       
   462 // ConvertSymbianAlgorithm
       
   463 // ---------------------------------------------------------------------------
       
   464 //
       
   465 TPKIKeyAlgorithm CPKISupport::ConvertSymbianAlgorithm(CCTKeyInfo::EKeyAlgorithm aAlg)
       
   466 {
       
   467     TPKIKeyAlgorithm algorithm = EPKIInvalidAlgorithm;
       
   468 
       
   469     switch(aAlg)
       
   470         {
       
   471         case ERSA:
       
   472             algorithm = EPKIRSA;
       
   473             break;
       
   474         case EDSA:
       
   475             algorithm = EPKIDSA;
       
   476             break;
       
   477         case EDH:
       
   478             algorithm = EPKIDH;
       
   479             break;
       
   480         default:
       
   481             break;
       
   482         }
       
   483     return algorithm;
       
   484 }
       
   485 
       
   486 // ---------------------------------------------------------------------------
       
   487 // CActive methods
       
   488 // ---------------------------------------------------------------------------
       
   489 //
       
   490 void CPKISupport::RunL()
       
   491     {
       
   492     LOG_1("CPKISupport::RunL err:%d", iStatus.Int());
       
   493     
       
   494     iSupportStatus = iStatus.Int();
       
   495 	if((iSupportStatus == KErrNotSupported) && 
       
   496 	    iToggleSwitch && (iInitState == EInitCompleteImportCerts))
       
   497 		{
       
   498 		// Some implementations of MIDP2 certstore return KErrNotSupported
       
   499 		// when calling iCertStore->Applications()
       
   500 		LOG_1("CPKISupport::RunL ignore error :%d", iSupportStatus);
       
   501 		iSupportStatus = KErrNone;		
       
   502 		}
       
   503 		
       
   504     iPending = ENoPendingOperation;
       
   505     if(iSupportStatus == KErrNone)
       
   506         {
       
   507         DoRunOperationL();     
       
   508         }
       
   509     else
       
   510         {
       
   511         iSubState = ESSCompleteRequest;
       
   512         }
       
   513     
       
   514     if(iSubState == ESSCompleteRequest)
       
   515         {
       
   516         if(iInitState != EInitDone)
       
   517             {
       
   518             iMessage.Complete(iSupportStatus);
       
   519             }
       
   520         else
       
   521             {
       
   522             CompleteCallerStatus( iSupportStatus );
       
   523             }
       
   524 		}
       
   525     }
       
   526 
       
   527 // ---------------------------------------------------------------------------
       
   528 // GetRequiredBufferSize
       
   529 // ---------------------------------------------------------------------------
       
   530 //    
       
   531 TInt CPKISupport::GetRequiredBufferSize()
       
   532     {
       
   533     LOG_1("CPKISupport::GetRequiredBufferSize:%d", iRequiredBufferLength);
       
   534     return iRequiredBufferLength;
       
   535     }
       
   536 
       
   537 // ---------------------------------------------------------------------------
       
   538 // SetCallerStatusPending
       
   539 // ---------------------------------------------------------------------------
       
   540 //    
       
   541 void CPKISupport::SetCallerStatusPending(TRequestStatus& aStatus)
       
   542     {
       
   543     iCallerStatus = &aStatus;
       
   544     aStatus = KRequestPending;    
       
   545     }
       
   546 
       
   547 // ---------------------------------------------------------------------------
       
   548 // CompleteCallerStatus
       
   549 // ---------------------------------------------------------------------------
       
   550 //    
       
   551 void CPKISupport::CompleteCallerStatus(TInt aError)
       
   552     {
       
   553     LOG_("-> CPKISupport::CompleteCallerStatus");
       
   554     if(iCallerStatus)
       
   555         {
       
   556         if(iCurrentFunction == PkiService::EStoreCertificate ||
       
   557            iCurrentFunction == PkiService::EAttachCertificate)
       
   558         	LogCertStoring(aError);
       
   559         
       
   560         LOG_("Completing caller status");
       
   561         User::RequestComplete(iCallerStatus, aError);        
       
   562         }
       
   563     else
       
   564         {
       
   565         LOG_("Completing rmessage");
       
   566         iMessage.Complete(aError);
       
   567         }      
       
   568     LOG_("<- CPKISupport::CompleteCallerStatus");        
       
   569     }
       
   570 
       
   571 // ---------------------------------------------------------------------------
       
   572 // LogCertStoring
       
   573 // ---------------------------------------------------------------------------
       
   574 //    
       
   575 void CPKISupport::LogCertStoring(TInt aError)
       
   576     {
       
   577     TUid uId(KPkiServiceUid3);
       
   578     if(aError == KErrNone)
       
   579     	iEventMediator.ReportLogEvent(uId, EInfo, R_VPN_MSG_CERT_INSTALLED, 1, iCertInfoForLogging);
       
   580     else 
       
   581     	{
       
   582     	TPckgBuf<TInt> int1Des(aError);
       
   583     	iEventMediator.ReportLogEvent(uId, EError, R_VPN_MSG_CERT_INSTALL_FAILED, 2, &int1Des, iCertInfoForLogging);
       
   584     	}
       
   585     }
       
   586 
       
   587 // ---------------------------------------------------------------------------
       
   588 // DoCancel
       
   589 // ---------------------------------------------------------------------------
       
   590 //
       
   591 void CPKISupport::DoCancel()
       
   592     {
       
   593     LOG_1("CPKISupport::DoCancel:%d", iStatus.Int());
       
   594     CancelCurrentOperation();  
       
   595     CompleteCallerStatus( iStatus.Int() );
       
   596     }
       
   597 
       
   598 // ---------------------------------------------------------------------------
       
   599 // RunError
       
   600 // ---------------------------------------------------------------------------
       
   601 //
       
   602 TInt CPKISupport::RunError(TInt aError)
       
   603     {
       
   604     LOG_1("RunError status = %d", aError);
       
   605     CompleteCallerStatus( aError );
       
   606     return KErrNone;
       
   607     }
       
   608 
       
   609 // ---------------------------------------------------------------------------
       
   610 // SetCurrentFunction
       
   611 // ---------------------------------------------------------------------------
       
   612 //
       
   613 void CPKISupport::SetCurrentFunction(TInt aFunc)
       
   614     {
       
   615     iCurrentFunction = aFunc;
       
   616     iSubState = ESSComplete;
       
   617     }
       
   618 
       
   619 
       
   620 // ---------------------------------------------------------------------------
       
   621 // StoreCertificateL
       
   622 // ---------------------------------------------------------------------------
       
   623 //
       
   624 void CPKISupport::StoreCertificateL(const TDesC &aLabel, 
       
   625                                     TCertificateOwnerType aOwnerType, 
       
   626                                     const TDesC8 &aBufferPtr, 
       
   627                                     const TBool& aIsDeletable, 
       
   628                                     TRequestStatus& aStatus)
       
   629 {
       
   630     LOG_("Store certificate");
       
   631     iSupportStatus = KErrNone;
       
   632     SetCallerStatusPending( aStatus );
       
   633     iKeyId.Zero();
       
   634 
       
   635     MCTWritableCertStore* certStore(NULL);
       
   636 
       
   637     CX509Certificate* tempCert = CX509Certificate::NewLC(aBufferPtr);
       
   638     iKeyId = tempCert->KeyIdentifierL();
       
   639     CleanupStack::PopAndDestroy();
       
   640 
       
   641     if(aOwnerType == EUserCertificate)
       
   642         {
       
   643         if (iCertStoreType == EPkiStoreTypeDevice) 
       
   644             {
       
   645             LOG(Log::Printf(_L(" Using device cert store\n")));
       
   646             certStore = iWritableDeviceCertStore;
       
   647             }
       
   648         else if (iCertStoreType == EPkiStoreTypeUser)
       
   649             {
       
   650             LOG(Log::Printf(_L(" Using user cert store\n")));
       
   651             certStore = iWritableCertStore;
       
   652             }
       
   653         else if (iCertStoreType == EPkiStoreTypeAny)
       
   654             {
       
   655             LOG(Log::Printf(_L(" Any cert store type, using user cert store\n")));
       
   656             certStore = iWritableCertStore;
       
   657             }
       
   658         else 
       
   659             {
       
   660             LOG(Log::Printf(_L(" Error: Unknown cert store type, defaulting to user cert store\n")));
       
   661             certStore = iWritableCertStore;
       
   662             }
       
   663         }
       
   664     else 
       
   665         {
       
   666         certStore = iWritableCertStore;
       
   667         }
       
   668     
       
   669     certStore->Add(aLabel, EX509Certificate, 
       
   670                    aOwnerType, NULL, NULL, 
       
   671                    aBufferPtr, aIsDeletable, iStatus);
       
   672     
       
   673     ExtractCertInfoL(aLabel, aOwnerType, aBufferPtr);
       
   674         
       
   675     iPending = ECertAdd;
       
   676     SetActive();
       
   677 }
       
   678 
       
   679 // ---------------------------------------------------------------------------
       
   680 // ExtractCertInfoL
       
   681 // ---------------------------------------------------------------------------
       
   682 //
       
   683 void  CPKISupport::ExtractCertInfoL(const TDesC& aLabel,
       
   684 								    const TCertificateOwnerType& aOwnerType,
       
   685 								    const TDesC8& aBufferPtr)
       
   686 {
       
   687 	_LIT8(KFormat,"Type: %S, Label: %S, Subject: %S, Issuer: %S");
       
   688 	
       
   689 	TBuf8<4> type;
       
   690 	if(aOwnerType == EPKICACertificate)
       
   691 		type.Copy(_L8("CA"));
       
   692    	else
       
   693    		type.Copy(_L8("USER"));
       
   694 	
       
   695 	CX509Certificate* tempCert = CX509Certificate::NewLC(aBufferPtr);
       
   696 	
       
   697 	HBufC* subject = tempCert->SubjectL();
       
   698 	CleanupStack::PushL(subject);
       
   699 	
       
   700 	HBufC* issuer = tempCert->IssuerL();
       
   701 	CleanupStack::PushL(issuer);
       
   702 	
       
   703 	// Calculates size of 
       
   704 	// "Type: <type>  Label: <label>  Subject: <subject>  Issuer: <issuer>"
       
   705     TInt bufSize(((TDesC8)KFormat).Length() + type.Length() + aLabel.Length() + subject->Length() + issuer->Length());
       
   706     delete iCertInfoForLogging; iCertInfoForLogging = NULL;
       
   707 	iCertInfoForLogging = HBufC8::NewL(bufSize);
       
   708    	
       
   709 	TPtr8 ptrLogMsg = iCertInfoForLogging->Des();        	
       
   710    	ptrLogMsg.Format(KFormat, &type, &aLabel, subject, issuer);
       
   711 	
       
   712 	CleanupStack::PopAndDestroy(issuer);
       
   713 	CleanupStack::PopAndDestroy(subject);
       
   714     CleanupStack::PopAndDestroy(tempCert);
       
   715 }
       
   716 
       
   717 
       
   718 // ---------------------------------------------------------------------------
       
   719 // ?description_if_needed
       
   720 // ---------------------------------------------------------------------------
       
   721 //
       
   722 void CPKISupport::AttachCertificateL(const TDesC& aLabel, 
       
   723     const TPKIKeyIdentifier &aKeyId, const TDesC8 &aBufferPtr, 
       
   724     TRequestStatus& aStatus)
       
   725 {
       
   726     TKeyIdentifier tempKeyId;
       
   727     CX509Certificate* certificate = CX509Certificate::NewLC(aBufferPtr);
       
   728 
       
   729     LOG_("Attach certificate");
       
   730     
       
   731     iSupportStatus = KErrNone;
       
   732     iKeyId = aKeyId;
       
   733     
       
   734     ExtractCertInfoL(aLabel, EUserCertificate, aBufferPtr);
       
   735     
       
   736     // fetch the keyId from the certificate
       
   737     tempKeyId = certificate->KeyIdentifierL();
       
   738     // If given keyId is empty, use the keyId from the certificate
       
   739     if(iKeyId.Length() == 0)
       
   740         {
       
   741         iKeyId = tempKeyId;
       
   742         }
       
   743         
       
   744     SetCallerStatusPending( aStatus );
       
   745     CleanupStack::PopAndDestroy(certificate);    // certificate
       
   746     
       
   747     iSubState = ESSComplete;
       
   748 
       
   749     MCTWritableCertStore* certStore(NULL);
       
   750 
       
   751     LOG(Log::Printf(_L("-------------------"));
       
   752 
       
   753         TBuf<256> buf;
       
   754         buf.Format(_L("Attaching certificate '%S'"), &aLabel);
       
   755         Log::Printf(buf);
       
   756         Log::Printf(_L("Certificate's KEY ID:"));
       
   757         Log::HexDump(NULL, NULL, iKeyId.Ptr(), iKeyId.Length());
       
   758     );
       
   759     if (iCertStoreType == EPkiStoreTypeDevice) 
       
   760         {
       
   761         LOG(Log::Printf(_L("CPKISupport::ContinueAttachCertificate() Using device cert store\n")));
       
   762         certStore = iWritableDeviceCertStore;
       
   763         }
       
   764     else if (iCertStoreType == EPkiStoreTypeUser)
       
   765         {
       
   766         LOG(Log::Printf(_L("CPKISupport::ContinueAttachCertificate() Using user cert store\n")));
       
   767         certStore = iWritableCertStore;
       
   768         }
       
   769     else if (iCertStoreType == EPkiStoreTypeAny)
       
   770         {
       
   771         LOG(Log::Printf(_L("CPKISupport::ContinueAttachCertificate() Any cert store, using user cert store\n")));
       
   772         certStore = iWritableCertStore;
       
   773         }
       
   774     else 
       
   775         {
       
   776         LOG(Log::Printf(_L("Error: Unknown cert store type, defaulting to user cert store\n")));
       
   777         certStore = iWritableCertStore;
       
   778         }
       
   779     LOG(Log::Printf(_L("-------------------")));
       
   780 
       
   781     certStore->Add(aLabel, 
       
   782                    EX509Certificate, 
       
   783                    EUserCertificate, 
       
   784                    &iKeyId, 
       
   785                    NULL, 
       
   786                    aBufferPtr, 
       
   787                    iStatus);
       
   788     iPending = ECertAdd;
       
   789     SetActive();
       
   790 }
       
   791 
       
   792 
       
   793 // ---------------------------------------------------------------------------
       
   794 // ?description_if_needed
       
   795 // ---------------------------------------------------------------------------
       
   796 //
       
   797 void CPKISupport::RetrieveCertificateL(const TDesC &aLabel, 
       
   798     TPtr8 &aBufferPtr, const TPKICertificateOwnerType& aType, 
       
   799     TRequestStatus& aStatus)
       
   800 {
       
   801     LOG(Log::Printf(_L("Retrieve certificate\n")));
       
   802     iSupportStatus = KErrNone;
       
   803     iCerts->Close();
       
   804     iSubState = ESSContinue;
       
   805     iOutBufferPtr = &aBufferPtr;
       
   806     SetCallerStatusPending( aStatus );
       
   807     SelectCertificateL(aLabel, aType);
       
   808 }
       
   809 
       
   810 // ---------------------------------------------------------------------------
       
   811 // ?description_if_needed
       
   812 // ---------------------------------------------------------------------------
       
   813 //
       
   814 void CPKISupport::ContinueRetrieveCertificate()
       
   815 {
       
   816     iSubState = ESSComplete;
       
   817     iPending = ECertRetrieve;
       
   818     
       
   819     TUint certificateSize = (*iCerts)[0]->Size();
       
   820     iRequiredBufferLength = certificateSize;
       
   821     if (certificateSize <= iOutBufferPtr->MaxLength())
       
   822         {        
       
   823         iCertStore->Retrieve(*(*iCerts)[0], *iOutBufferPtr, iStatus);
       
   824         SetActive();
       
   825         }
       
   826     else
       
   827         {
       
   828         iStatus = KRequestPending;
       
   829         SetActive();
       
   830         
       
   831         TRequestStatus* ownStatus = &iStatus;
       
   832         User::RequestComplete(ownStatus, KPKIErrBufferTooShort);
       
   833         }
       
   834 }
       
   835 
       
   836 
       
   837 /**
       
   838     Removes all unnecessary certificates from the certificate
       
   839     list before generating the local mapping data.
       
   840     More specifically, all MIDP2 and non X509 certificates are removed.
       
   841     (MIDP2 certificates can never have a "VPN" trust setting.)
       
   842     
       
   843     iListCleaned instance variable is used to limit the frequency
       
   844     of this cleanup - it's only necessary once, during PKI startup.
       
   845 */
       
   846 void CPKISupport::CleanupCertListL() 
       
   847     {
       
   848     LOG_("Removing invalid certs (MIDP2 certs)");
       
   849     if (iCerts) 
       
   850         {
       
   851         TInt certcount = iCerts->Count();
       
   852         _LIT(KMidp2Label, "MIDP2");
       
   853         LOG_1("Total cert count, before cleanup: %d", iCerts->Count());
       
   854         RMPointerArray<CCTCertInfo> removedInfos;
       
   855         CleanupClosePushL(removedInfos);
       
   856         for (TInt i = certcount - 1; i >= 0; i--) 
       
   857             {
       
   858             CCTCertInfo* info = (*iCerts)[i];
       
   859             if (info->Label().Compare(KMidp2Label) == 0 ||
       
   860                 info->CertificateFormat() != EX509Certificate) 
       
   861                 {
       
   862                 // CCTCertInfo has private destructor
       
   863                 removedInfos.AppendL( info );
       
   864                 iCerts->Remove(i);
       
   865                 continue;
       
   866                 }
       
   867             }
       
   868         CleanupStack::PopAndDestroy(); // removedInfos
       
   869         iCerts->Compress();
       
   870         LOG_1("Total cert count, after cleanup: %d", iCerts->Count());
       
   871         }
       
   872     else 
       
   873         {
       
   874         LOG_("Certs list empty!");
       
   875         }
       
   876     }
       
   877 
       
   878 // ---------------------------------------------------------------------------
       
   879 // DoRunOperationL
       
   880 // ---------------------------------------------------------------------------
       
   881 //
       
   882 void CPKISupport::DoRunOperationL()
       
   883     {
       
   884     LOG_1("CPKISupport::DoRunOperationL iInitState:%d", iInitState);
       
   885     LOG_1("CPKISupport::DoRunOperationL iCurrentFunction:%d", 
       
   886         iCurrentFunction);
       
   887     LOG_1("CPKISupport::DoRunOperationL iSubState:%d", iSubState);
       
   888     
       
   889     switch(iInitState)
       
   890         {
       
   891         case EInitDone:
       
   892             DoRunLoggedInOperationL();
       
   893             break;
       
   894 
       
   895         case EInitInitializeCertStore:            
       
   896             if(iMapper.CacheCreated())
       
   897                 {
       
   898                 FindInterfacesL();
       
   899                 iInitState = EInitDone;
       
   900                 iMessage.Complete(KErrNone);
       
   901                 return;
       
   902                 }
       
   903             else
       
   904                 {
       
   905 				iMapper.SetCacheCreated();
       
   906                 FindInterfacesL();
       
   907                 ListAllCertificatesL();
       
   908                 }
       
   909             break;
       
   910         case EInitRetrieveCertList:
       
   911             LOG_("CPKISupport::DoRunOperationL() EInitRetrieveCertList");
       
   912             // Cert list might be new. Remove all MIDP2 certificates first,
       
   913             // if it hasn't been already done
       
   914             CleanupCertListL();
       
   915             iInitState = EInitCompleteImportCerts;
       
   916             
       
   917             //Falls through
       
   918         case EInitCompleteImportCerts:
       
   919             LOG_("CPKISupport::DoRunOperationL() EInitCompleteImportCerts");            
       
   920 			if(GetApplicationsOfCTFCertL())
       
   921 				{
       
   922                 LOG_(" Getting cert's applications returned with FALSE, breaking");
       
   923 				break;
       
   924 				}
       
   925             SaveCertInfoToCacheL();    // Handles one certificate, if found
       
   926             if(iImportCounter < iCerts->Count())
       
   927                 {
       
   928                 ReadNextCertForImportL();
       
   929                 }
       
   930             else
       
   931                 {
       
   932                 LOG_(" All certificates imported, doing clean ups");
       
   933                 // Cleanup
       
   934 
       
   935                 delete iImportCertMapping;
       
   936                 iImportCertMapping = NULL;
       
   937 
       
   938                 delete iImportCertData;
       
   939                 iImportCertData = NULL;
       
   940 
       
   941                 iCerts->Close();
       
   942 
       
   943                 delete iCertFilter;
       
   944                 iCertFilter = NULL;
       
   945                 iInitState = EInitDone;
       
   946                 iMessage.Complete(KErrNone);
       
   947                 }
       
   948             break;  // repeat            
       
   949         default:
       
   950             PKISERVICE_INVARIANT();
       
   951             iSupportStatus = KPKIErrUnexpectedState;
       
   952             iSubState = ESSCompleteRequest;
       
   953             break;
       
   954         } // End switch(iInitState)
       
   955     
       
   956     }
       
   957 
       
   958 // ---------------------------------------------------------------------------
       
   959 // DoRunLoggedInOperationL
       
   960 // ---------------------------------------------------------------------------
       
   961 //
       
   962 void CPKISupport::DoRunLoggedInOperationL()
       
   963     {
       
   964     LOG_1("CPKISupport::DoRunLoggedInOperationL iCurrentFunction:%d", 
       
   965         iCurrentFunction);
       
   966     LOG_1("CPKISupport::DoRunLoggedInOperationL iSubState:%d", iSubState);
       
   967      
       
   968     switch(iCurrentFunction)
       
   969         {
       
   970         case PkiService::EAttachCertificate:
       
   971             switch(iSubState)
       
   972                 {
       
   973                 case ESSComplete:
       
   974                     iSubState = ESSCompleteRequest;
       
   975                     break;
       
   976                 default:
       
   977                     iSupportStatus = KPKIErrUnexpectedState;
       
   978                     iSubState = ESSCompleteRequest;
       
   979                     break;
       
   980                 }
       
   981             break;
       
   982             
       
   983         case PkiService::EReadCertificate:
       
   984             switch(iSubState)
       
   985                 {
       
   986                 case ESSContinue:
       
   987                     {
       
   988                     if(iCerts->Count() > 1)
       
   989                         {
       
   990                         LOG(Log::Printf(_L("Duplicate Certificate\n")));
       
   991                         iSubState = ESSCompleteRequest;
       
   992                         iSupportStatus = KErrNotFound;
       
   993                         }
       
   994                     else if (iCerts->Count() < 1)
       
   995                         {
       
   996                         LOG(Log::Printf(_L("No matching certificates found\n")));
       
   997                         iSubState = ESSCompleteRequest;
       
   998                         iSupportStatus = KErrNotFound;
       
   999                         }
       
  1000 					else if ((((*iCerts)[0])->CertificateOwnerType() == EUserCertificate) &&
       
  1001 							(((iCertStoreType == EPkiStoreTypeDevice) && (((*iCerts)[0])->Token().Label().Compare(KDeviceCertStore)!=0)) ||
       
  1002                     	  	((iCertStoreType == EPkiStoreTypeUser) && (((*iCerts)[0])->Token().Label().Compare(KUserCertStore)!=0))))
       
  1003 						{
       
  1004 						LOG_1("User certificate's certificate info does not match certificate store type. Store type is: %d", iCertStoreType);
       
  1005 						iSubState = ESSCompleteRequest;
       
  1006 						iSupportStatus = KErrNotFound;                    	
       
  1007 						}
       
  1008                     else
       
  1009                         {
       
  1010                         ContinueRetrieveCertificate();
       
  1011                         }
       
  1012                     break;
       
  1013                     }
       
  1014                 case ESSComplete:
       
  1015                     iSubState = ESSCompleteRequest;
       
  1016                     break;
       
  1017                 default:
       
  1018                     iSupportStatus = KPKIErrUnexpectedState;
       
  1019                     iSubState = ESSCompleteRequest;
       
  1020                     break;
       
  1021                 }
       
  1022             break;
       
  1023             
       
  1024         case PkiService::ESetTrust:
       
  1025         case PkiService::ETrusted:
       
  1026             switch(iSubState)
       
  1027                 {
       
  1028                 case ESSContinue:
       
  1029                     {
       
  1030                     if(iCerts->Count() != 1)
       
  1031                         {
       
  1032                         iSubState = ESSCompleteRequest;
       
  1033                         iSupportStatus = KErrNotFound;
       
  1034                         }
       
  1035                     else
       
  1036                         {
       
  1037                         if(iCurrentFunction == PkiService::ESetTrust)
       
  1038                             {
       
  1039                             ContinueSetTrust();
       
  1040                             }
       
  1041                         else
       
  1042                             {
       
  1043                             ContinueTrusted();
       
  1044                             }
       
  1045                         }
       
  1046                     break;
       
  1047                     }
       
  1048                 case ESSComplete:
       
  1049                     if(iCurrentFunction == PkiService::ETrusted)
       
  1050                         {
       
  1051                         iWrapper.SetTrusted(iTrusted);
       
  1052                         }
       
  1053                     iSubState = ESSCompleteRequest;
       
  1054                     break;
       
  1055                 default:
       
  1056                     iSupportStatus = KPKIErrUnexpectedState;
       
  1057                     iSubState = ESSCompleteRequest;
       
  1058                     break;
       
  1059                 }
       
  1060             break;
       
  1061 
       
  1062         case PkiService::ESetApplicability:
       
  1063         case PkiService::EApplications:
       
  1064             switch(iSubState)
       
  1065                 {
       
  1066                 case ESSContinue:
       
  1067                     {
       
  1068                     if(iCerts->Count() != 1)
       
  1069                         {
       
  1070                         iSubState = ESSCompleteRequest;
       
  1071                         iSupportStatus = KErrNotFound;
       
  1072                         }
       
  1073                     else
       
  1074                         {
       
  1075                         if(iCurrentFunction == PkiService::ESetApplicability)
       
  1076                             {
       
  1077                             ContinueSetApplicability();
       
  1078                             }
       
  1079                         else if(iCurrentFunction == PkiService::EApplications)
       
  1080                             {
       
  1081                             ContinueApplications();
       
  1082                             }
       
  1083                         else
       
  1084                             {
       
  1085                             PKISERVICE_INVARIANT();
       
  1086                             }
       
  1087                         }
       
  1088                     break;
       
  1089                     }
       
  1090                 case ESSComplete:
       
  1091                     if(iCurrentFunction == PkiService::EApplications)
       
  1092                         {
       
  1093                         iWrapper.SetApplications(iApplUids);
       
  1094                         }
       
  1095                             
       
  1096                     iSubState = ESSCompleteRequest;
       
  1097                     break;
       
  1098                 default:
       
  1099                     iSupportStatus = KPKIErrUnexpectedState;
       
  1100                     iSubState = ESSCompleteRequest;
       
  1101                     break;
       
  1102                 }
       
  1103             break;
       
  1104 
       
  1105         case PkiService::ERemoveCertificate:
       
  1106             switch(iSubState)
       
  1107                 {
       
  1108                 case ESSContinue:
       
  1109                     if(iCerts->Count() != 1)
       
  1110                         {
       
  1111                         iSupportStatus = KPKIErrNotFound;
       
  1112                         iSubState = ESSCompleteRequest;
       
  1113                         }
       
  1114                     else
       
  1115                         {
       
  1116                         ContinueRemoveCertificate();
       
  1117                         }
       
  1118                     break;
       
  1119                 case ESSComplete:
       
  1120                     iSubState = ESSCompleteRequest;
       
  1121                     break;
       
  1122                 default:
       
  1123                     iSupportStatus = KPKIErrUnexpectedState;
       
  1124                     iSubState = ESSCompleteRequest;
       
  1125                     break;
       
  1126                 }
       
  1127             break;            
       
  1128         default:
       
  1129             iSubState = ESSCompleteRequest;
       
  1130             break;
       
  1131         }
       
  1132     }    
       
  1133 
       
  1134 
       
  1135 
       
  1136 
       
  1137 
       
  1138 
       
  1139 // ---------------------------------------------------------------------------
       
  1140 // ?description_if_needed
       
  1141 // ---------------------------------------------------------------------------
       
  1142 //
       
  1143 void CPKISupport::RemoveCertificateL(const TDesC &aLabel, 
       
  1144     TRequestStatus& aStatus)
       
  1145 {
       
  1146     LOG(Log::Printf(_L("Remove certificate\n")));
       
  1147     iSupportStatus = KErrNone;
       
  1148     iCerts->Close();
       
  1149     iSubState = ESSContinue;
       
  1150     SetCallerStatusPending( aStatus );
       
  1151     SelectCertificateL(aLabel);
       
  1152 }
       
  1153 
       
  1154 // ---------------------------------------------------------------------------
       
  1155 // ?description_if_needed
       
  1156 // ---------------------------------------------------------------------------
       
  1157 //
       
  1158 void CPKISupport::ContinueRemoveCertificate()
       
  1159 {
       
  1160     iCertStore->Remove(*(*iCerts)[0], iStatus);
       
  1161     iPending = ECertRemove;
       
  1162     iSubState = ESSComplete;
       
  1163     SetActive();
       
  1164 }
       
  1165 
       
  1166 
       
  1167 // ---------------------------------------------------------------------------
       
  1168 // ?description_if_needed
       
  1169 // ---------------------------------------------------------------------------
       
  1170 //
       
  1171 void CPKISupport::SetTrustL(const TDesC &aLabel, 
       
  1172     TBool aTrusted, TRequestStatus& aStatus)
       
  1173 {
       
  1174     LOG(Log::Printf(_L("SetTrust\n")));
       
  1175     iSupportStatus = KErrNone;
       
  1176     iCerts->Close();
       
  1177     iSubState = ESSContinue;
       
  1178     iTrusted = aTrusted;
       
  1179     SetCallerStatusPending( aStatus );
       
  1180     SelectCertificateL(aLabel);
       
  1181 }
       
  1182 
       
  1183 // ---------------------------------------------------------------------------
       
  1184 // ?description_if_needed
       
  1185 // ---------------------------------------------------------------------------
       
  1186 //
       
  1187 void CPKISupport::ContinueSetTrust()
       
  1188 {
       
  1189     iSubState = ESSComplete;    
       
  1190     iCertStore->SetTrust(*(*iCerts)[0], iTrusted, iStatus);
       
  1191     iPending = ESetTrust;
       
  1192     SetActive();
       
  1193 }
       
  1194 
       
  1195 // ---------------------------------------------------------------------------
       
  1196 // ?description_if_needed
       
  1197 // ---------------------------------------------------------------------------
       
  1198 //
       
  1199 void CPKISupport::TrustedL(const TDesC &aLabel, TRequestStatus& aStatus)
       
  1200 {
       
  1201     LOG(Log::Printf(_L("Trusted\n")));
       
  1202     iSupportStatus = KErrNone;
       
  1203     iCerts->Close();
       
  1204     iSubState = ESSContinue;
       
  1205     SetCallerStatusPending( aStatus );
       
  1206     SelectCertificateL(aLabel);
       
  1207 }
       
  1208 
       
  1209 // ---------------------------------------------------------------------------
       
  1210 // ?description_if_needed
       
  1211 // ---------------------------------------------------------------------------
       
  1212 //
       
  1213 void CPKISupport::ContinueTrusted()
       
  1214 {
       
  1215     iSubState = ESSComplete;
       
  1216     iCertStore->Trusted(*(*iCerts)[0], iTrusted, iStatus);
       
  1217     iPending = ETrusted;
       
  1218     SetActive();
       
  1219 }
       
  1220 
       
  1221 // ---------------------------------------------------------------------------
       
  1222 // ?description_if_needed
       
  1223 // ---------------------------------------------------------------------------
       
  1224 //
       
  1225 void CPKISupport::SetApplicabilityL(const TDesC &aLabel, const RArray<TUid>& aApplUids, TRequestStatus& aStatus)
       
  1226 {
       
  1227     LOG(Log::Printf(_L("SetApplicability\n")));
       
  1228     iSupportStatus = KErrNone;
       
  1229     iCerts->Close();
       
  1230     iSubState = ESSContinue;
       
  1231 	iApplUids.Close();
       
  1232 	for(TInt i = 0;i<aApplUids.Count();i++)
       
  1233 		{
       
  1234 		iApplUids.Append(aApplUids[i]);
       
  1235 		}
       
  1236     SetCallerStatusPending( aStatus );
       
  1237     SelectCertificateL(aLabel);
       
  1238 }
       
  1239 
       
  1240 // ---------------------------------------------------------------------------
       
  1241 // ContinueSetApplicability
       
  1242 // ---------------------------------------------------------------------------
       
  1243 //
       
  1244 void CPKISupport::ContinueSetApplicability()
       
  1245     {
       
  1246     PKISERVICE_ASSERT(iCerts && iCerts->Count());
       
  1247     iSubState = ESSComplete;
       
  1248     iCertStore->SetApplicability(*(*iCerts)[0], iApplUids, iStatus);
       
  1249     iPending = ESetApplicability;
       
  1250     SetActive();
       
  1251     }
       
  1252 
       
  1253 // ---------------------------------------------------------------------------
       
  1254 // ?description_if_needed
       
  1255 // ---------------------------------------------------------------------------
       
  1256 //
       
  1257 void CPKISupport::ApplicationsL(const TDesC &aLabel, TRequestStatus& aStatus)
       
  1258 {
       
  1259     LOG(Log::Printf(_L("Applications\n")));
       
  1260     iSupportStatus = KErrNone;
       
  1261     iCerts->Close();
       
  1262     iSubState = ESSContinue;
       
  1263     SetCallerStatusPending( aStatus );
       
  1264     SelectCertificateL(aLabel);
       
  1265 }
       
  1266 
       
  1267 // ---------------------------------------------------------------------------
       
  1268 // ContinueApplications
       
  1269 // ---------------------------------------------------------------------------
       
  1270 //
       
  1271 void CPKISupport::ContinueApplications()
       
  1272 {
       
  1273     iSubState = ESSComplete;
       
  1274 	iApplUids.Close();
       
  1275     iCertStore->Applications(*(*iCerts)[0], iApplUids, iStatus);
       
  1276     iPending = EApplications;
       
  1277     SetActive();
       
  1278 }
       
  1279 
       
  1280 
       
  1281 // ---------------------------------------------------------------------------
       
  1282 // SelectCertificateL
       
  1283 // ---------------------------------------------------------------------------
       
  1284 //
       
  1285 void CPKISupport::SelectCertificateL(const TDesC &aLabel, 
       
  1286                                      const TPKICertificateOwnerType& aType )
       
  1287 {
       
  1288     delete iCertFilter;
       
  1289     iCertFilter = NULL;
       
  1290     iCertFilter = CCertAttributeFilter::NewL();
       
  1291 
       
  1292     LOG(Log::Printf(_L(" Select by label: %S\n"), &aLabel));
       
  1293     iCertFilter->SetLabel(aLabel);
       
  1294     if (aType != 0) 
       
  1295         {
       
  1296         LOG_1(" Select by owner type: %d", aType);
       
  1297         iCertFilter->SetOwnerType(TCertificateOwnerType(aType));
       
  1298         }
       
  1299     GetCertificateStoreListAsync();
       
  1300 }
       
  1301 
       
  1302 
       
  1303 
       
  1304 // ---------------------------------------------------------------------------
       
  1305 // GetCertificateStoreListAsync()
       
  1306 // ---------------------------------------------------------------------------
       
  1307 //
       
  1308 void CPKISupport::GetCertificateStoreListAsync()
       
  1309     {
       
  1310     LOG_1("-> CPKISupport::GetCertificateStoreListAsync() iSubState:%d",
       
  1311             iSubState );
       
  1312     // preconditions
       
  1313     PKISERVICE_ASSERT( iCerts && iCertFilter && iStatus != KRequestPending );
       
  1314     LOG_1("CPKISupport::GetSertificateStoreListAsync iSubState:%d", 
       
  1315         iSubState );
       
  1316 
       
  1317     // Get list of all known certificates
       
  1318     iCertStore->List(*iCerts, *iCertFilter, iStatus);
       
  1319     iPending = EListCerts;
       
  1320     SetActive();
       
  1321     LOG_("<- CPKISupport::GetCertificateStoreListAsync()");
       
  1322     }
       
  1323 
       
  1324