vpnengine/pkiservice/src/pkisupport.cpp
changeset 1 c9c2ad51f972
parent 0 33413c0669b9
child 4 29b591713d44
equal deleted inserted replaced
0:33413c0669b9 1:c9c2ad51f972
    27 #include <unifiedcertstore.h>
    27 #include <unifiedcertstore.h>
    28 #include <mctwritablecertstore.h>
    28 #include <mctwritablecertstore.h>
    29 
    29 
    30 #include "pkiwrapper.h"
    30 #include "pkiwrapper.h"
    31 #include "PKIMapper.h"
    31 #include "PKIMapper.h"
       
    32 #include "mapdescriptor.h"
    32 #include "pkisupport.h"
    33 #include "pkisupport.h"
    33 #include "pkisession.h"
    34 #include "pkisession.h"
    34 #include "log_r6.h"
    35 #include "log_r6.h"
    35 #include "pkiserviceassert.h"
    36 #include "pkiserviceassert.h"
    36 #include "pkiserviceconstants.h"
    37 #include "pkiserviceconstants.h"
    60 CPKISupport::~CPKISupport()
    61 CPKISupport::~CPKISupport()
    61     {
    62     {
    62     LOG_("CPKISupport::~CPKISupport");
    63     LOG_("CPKISupport::~CPKISupport");
    63     Cancel();    
    64     Cancel();    
    64 
    65 
    65     if(iCerts != NULL)
    66     iCerts.Close();
    66         {
       
    67         iCerts->Close();
       
    68         delete iCerts;
       
    69         }
       
    70     delete iCertFilter;
    67     delete iCertFilter;
    71     delete iCertStore;
    68     delete iCertStore;
    72     iFSession.Close();
    69     iFSession.Close();
    73     iApplUids.Close();
    70     iApplUids.Close();
    74     delete iImportCertMapping;
       
    75     delete iImportCertData;
    71     delete iImportCertData;
    76     iEventMediator.Close();
    72     iEventMediator.Close();
    77     delete iCertInfoForLogging; 
    73     delete iCertInfoForLogging; 
    78     iCertInfoForLogging = NULL;
    74     iCertInfoForLogging = NULL;
    79     }
    75     }
    98 //
    94 //
    99 void  CPKISupport::StartInitializeL(const RMessage2& aMessage)
    95 void  CPKISupport::StartInitializeL(const RMessage2& aMessage)
   100     {
    96     {
   101     LOG_("-> CPKISupport::StartInitialize()");
    97     LOG_("-> CPKISupport::StartInitialize()");
   102     User::LeaveIfError(iFSession.Connect());
    98     User::LeaveIfError(iFSession.Connect());
   103     iCertStore = CUnifiedCertStore::NewL(iFSession, ETrue);
    99     iCertStore = CUnifiedCertStore::NewL(iFSession, ETrue);    
   104     PKISERVICE_ASSERT(!iCerts);
       
   105     iCerts = new (ELeave) RMPointerArray<CCTCertInfo>;
       
   106     
   100     
   107     iMessage = aMessage;
   101     iMessage = aMessage;
   108     iCallerStatus = NULL;
   102     iCallerStatus = NULL;
   109     iSupportStatus = KErrNone;
   103     iSupportStatus = KErrNone;
   110 
   104 
   213 // ---------------------------------------------------------------------------
   207 // ---------------------------------------------------------------------------
   214 //
   208 //
   215 void  CPKISupport::ReadNextCertForImportL()
   209 void  CPKISupport::ReadNextCertForImportL()
   216 {
   210 {
   217     LOG_("-> CPKISupport::ImportNextCertL()");
   211     LOG_("-> CPKISupport::ImportNextCertL()");
   218     PKISERVICE_ASSERT(iImportCounter < iCerts->Count());
   212     PKISERVICE_ASSERT(iImportCounter < iCerts.Count());
   219     
   213     
   220     if(iImportCounter == 0)
   214     if(iImportCounter == 0)
   221         {
   215         {
   222         LOG_1("Import %d certificates\n", iCerts->Count());
   216         LOG_1("Import %d certificates\n", iCerts.Count());
   223         }
   217         }
   224         
   218         
   225     LOG_1("Import certificate:%d", iImportCounter);
   219     LOG_1("Import certificate:%d", iImportCounter);
   226     LOG_1("Label: %S", &((*iCerts)[iImportCounter]->Label()));
   220     LOG_1("Label: %S", &(iCerts[iImportCounter]->Label()));
   227     
   221         
   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;
   222     delete iImportCertData;
   246     iImportCertData = NULL;
   223     iImportCertData = NULL;
   247     iImportCertData = HBufC8::NewL((*iCerts)[iImportCounter]->Size());        
   224     iImportCertData = HBufC8::NewL(iCerts[iImportCounter]->Size());        
   248     iImportCertDataPtr.Set(iImportCertData->Des());
   225     iImportCertDataPtr.Set(iImportCertData->Des());
   249     
   226         
   250     LOG_(" Values set OK, retrieving");
       
   251     iCertStore->Retrieve(
   227     iCertStore->Retrieve(
   252         *(*iCerts)[iImportCounter], iImportCertDataPtr, iStatus);
   228         *(iCerts[iImportCounter]), iImportCertDataPtr, iStatus);
   253     iPending = ECertRetrieve;
   229     iPending = ECertRetrieve;
   254     SetActive();
   230     SetActive();
   255     LOG_("<- CPKISupport::ImportCTFCertsL() SetActive() and ret: EFalse");
   231     LOG_("<- CPKISupport::ImportCTFCertsL() SetActive() and ret: EFalse");
   256 }
   232 }
   257 
   233 
   259 // ImportOneCTFCertL
   235 // ImportOneCTFCertL
   260 // ---------------------------------------------------------------------------
   236 // ---------------------------------------------------------------------------
   261 //
   237 //
   262 void CPKISupport::SaveCertInfoToCacheL()
   238 void CPKISupport::SaveCertInfoToCacheL()
   263     {
   239     {
   264     if((iImportCertMapping != NULL) && (iImportCertData != NULL))
   240     LOG_("Saving cert info to cache");
   265         {
   241     PKISERVICE_ASSERT(iImportCertData != NULL);
   266         LOG_("Saving cert info to cache");
   242     
   267        
   243     CX509Certificate* certificate = CX509Certificate::NewLC(iImportCertDataPtr);       
   268         CX509Certificate* certificate = CX509Certificate::NewLC(iImportCertDataPtr);            
   244     const CCTCertInfo* currentCertInfo = iCerts[iImportCounter]; 
   269         const CSubjectPublicKeyInfo& publicKeyInfo = certificate->PublicKey();            
   245     TPkiServiceStoreType storeType = EPkiStoreTypeAny;
   270         const TPtrC8 keyData = publicKeyInfo.KeyData();
   246     switch(currentCertInfo->Token().TokenType().Type().iUid)
   271 
   247         {
   272         TX509KeyFactory keyFactory;
   248         case STORETYPE_DEVICE_CERT_ID:
   273         switch(publicKeyInfo.AlgorithmId())
   249             storeType = EPkiStoreTypeDevice;
       
   250             break;
       
   251         case STORETYPE_USER_CERT_ID:
       
   252             storeType = EPkiStoreTypeUser;
       
   253             break;
       
   254         default:
       
   255             storeType = EPkiStoreTypeAny;
       
   256             break;
       
   257         }
       
   258     
       
   259     CMapDescriptor* newMapping = CMapDescriptor::NewL(currentCertInfo->Label(),
       
   260                                                       *certificate,
       
   261                                                       (TPKICertificateOwnerType)currentCertInfo->CertificateOwnerType(),
       
   262                                                       storeType);  
       
   263     CleanupStack::PushL(newMapping);
       
   264     
       
   265     newMapping->SetMapDeletable(currentCertInfo->IsDeletable());
       
   266     newMapping->SetMapApplications(iApplUids);
       
   267     iApplUids.Reset();
       
   268     User::LeaveIfError(iMapper.AddMapping(newMapping));
       
   269             
       
   270     CleanupStack::Pop(newMapping);
       
   271     CleanupStack::PopAndDestroy(certificate);
       
   272     iImportCounter++;    
       
   273     }
       
   274 
       
   275 // ---------------------------------------------------------------------------
       
   276 // GetApplicationsOfCTFCertL
       
   277 // ---------------------------------------------------------------------------
       
   278 //
       
   279 TBool CPKISupport::GetApplicationsOfCTFCertL()
       
   280 {
       
   281     LOG_("-> CPKISupport::GetApplicationsOfCTFCertL()");
       
   282     PKISERVICE_ASSERT(iImportCertData != NULL);
       
   283     
       
   284     if(iToggleSwitch == EFalse)
       
   285         {
       
   286         if(iCerts[iImportCounter]->CertificateOwnerType() == ECACertificate)
   274             {
   287             {
   275             case ERSA:
   288             PKISERVICE_ASSERT(iApplUids.Count() == 0);
   276                 {                        
   289             
   277                 iImportCertMapping->iKeyAlgorithm = EPKIRSA;
   290             iToggleSwitch = ETrue;
   278                 const CRSAPublicKey* keyRSA = keyFactory.RSAPublicKeyL( keyData );
   291             // Get applications				
   279                 const TInteger&  n = keyRSA->N();
   292             iCertStore->Applications(*(iCerts[iImportCounter]), iApplUids, iStatus);
   280                 iImportCertMapping->iKeySize = n.BitCount();
   293             iPending = EApplications;
   281                 delete keyRSA;                    
   294             LOG_("<- CPKISupport::GetApplicationsOfCTFCertL() SetActive(), ret: ETrue");
   282                 }
   295             SetActive();
   283                 break;        	
   296             return ETrue;
   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             }
   297             }
   313         else
   298         else
   314             {
   299             {
   315             iImportCertMapping->SetCertStoreType(EPkiStoreTypeAny);
   300             LOG_("<- CPKISupport::GetApplicationsOfCTFCertL() Not a CA cert, ret: EFalse");
       
   301             return EFalse;
   316             }
   302             }
   317             
   303         }
   318 		for(TInt i=0;i<iApplUids.Count();i++)
   304     else
   319 			{
   305         {
   320 			iImportCertMapping->iApplUids.Append(iApplUids[i]);
   306         LOG_("<- CPKISupport::GetApplicationsOfCTFCertL() iToggleSwitch == ETrue, ret: EFalse");
   321 			}
   307         iToggleSwitch = EFalse;
   322         User::LeaveIfError( 
   308         return EFalse;
   323             iMapper.AddMapping(*iImportCertMapping) );
   309         }
   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 }
   310 }
   373 
   311 
   374 
   312 
   375 // ---------------------------------------------------------------------------
   313 // ---------------------------------------------------------------------------
   376 // CancelCurrentOperation
   314 // CancelCurrentOperation
   633     iKeyId.Zero();
   571     iKeyId.Zero();
   634 
   572 
   635     MCTWritableCertStore* certStore(NULL);
   573     MCTWritableCertStore* certStore(NULL);
   636 
   574 
   637     CX509Certificate* tempCert = CX509Certificate::NewLC(aBufferPtr);
   575     CX509Certificate* tempCert = CX509Certificate::NewLC(aBufferPtr);
   638     iKeyId = tempCert->KeyIdentifierL();
   576     iKeyId = tempCert->SubjectKeyIdentifierL();
   639     CleanupStack::PopAndDestroy();
   577     CleanupStack::PopAndDestroy();
   640 
   578 
   641     if(aOwnerType == EUserCertificate)
   579     if(aOwnerType == EUserCertificate)
   642         {
   580         {
   643         if (iCertStoreType == EPkiStoreTypeDevice) 
   581         if (iCertStoreType == EPkiStoreTypeDevice) 
   718 // ---------------------------------------------------------------------------
   656 // ---------------------------------------------------------------------------
   719 // ?description_if_needed
   657 // ?description_if_needed
   720 // ---------------------------------------------------------------------------
   658 // ---------------------------------------------------------------------------
   721 //
   659 //
   722 void CPKISupport::AttachCertificateL(const TDesC& aLabel, 
   660 void CPKISupport::AttachCertificateL(const TDesC& aLabel, 
   723     const TPKIKeyIdentifier &aKeyId, const TDesC8 &aBufferPtr, 
   661     const TDesC8 &aBufferPtr, 
   724     TRequestStatus& aStatus)
   662     TRequestStatus& aStatus)
   725 {
   663 {
   726     TKeyIdentifier tempKeyId;
   664     TKeyIdentifier tempKeyId;
   727     CX509Certificate* certificate = CX509Certificate::NewLC(aBufferPtr);
   665     CX509Certificate* certificate = CX509Certificate::NewLC(aBufferPtr);
   728 
   666 
   729     LOG_("Attach certificate");
   667     LOG_("Attach certificate");
   730     
   668     
   731     iSupportStatus = KErrNone;
   669     iSupportStatus = KErrNone;   
   732     iKeyId = aKeyId;
   670     
   733     
   671     ExtractCertInfoL(aLabel, EUserCertificate, aBufferPtr);    
   734     ExtractCertInfoL(aLabel, EUserCertificate, aBufferPtr);
   672     iKeyId = certificate->SubjectKeyIdentifierL();
   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         
   673         
   744     SetCallerStatusPending( aStatus );
   674     SetCallerStatusPending( aStatus );
   745     CleanupStack::PopAndDestroy(certificate);    // certificate
   675     CleanupStack::PopAndDestroy(certificate);    // certificate
   746     
   676     
   747     iSubState = ESSComplete;
   677     iSubState = ESSComplete;
   779     LOG(Log::Printf(_L("-------------------")));
   709     LOG(Log::Printf(_L("-------------------")));
   780 
   710 
   781     certStore->Add(aLabel, 
   711     certStore->Add(aLabel, 
   782                    EX509Certificate, 
   712                    EX509Certificate, 
   783                    EUserCertificate, 
   713                    EUserCertificate, 
   784                    &iKeyId, 
   714                    NULL, 
   785                    NULL, 
   715                    NULL, 
   786                    aBufferPtr, 
   716                    aBufferPtr, 
   787                    iStatus);
   717                    iStatus);
   788     iPending = ECertAdd;
   718     iPending = ECertAdd;
   789     SetActive();
   719     SetActive();
   793 // ---------------------------------------------------------------------------
   723 // ---------------------------------------------------------------------------
   794 // ?description_if_needed
   724 // ?description_if_needed
   795 // ---------------------------------------------------------------------------
   725 // ---------------------------------------------------------------------------
   796 //
   726 //
   797 void CPKISupport::RetrieveCertificateL(const TDesC &aLabel, 
   727 void CPKISupport::RetrieveCertificateL(const TDesC &aLabel, 
       
   728     const TPKIKeyIdentifier& aCertificateKeyId,
   798     TPtr8 &aBufferPtr, const TPKICertificateOwnerType& aType, 
   729     TPtr8 &aBufferPtr, const TPKICertificateOwnerType& aType, 
   799     TRequestStatus& aStatus)
   730     TRequestStatus& aStatus)
   800 {
   731 {
   801     LOG(Log::Printf(_L("Retrieve certificate\n")));
   732     LOG(Log::Printf(_L("Retrieve certificate\n")));
   802     iSupportStatus = KErrNone;
   733     iSupportStatus = KErrNone;
   803     iCerts->Close();
   734     iCerts.Close();
   804     iSubState = ESSContinue;
   735     iSubState = ESSContinue;
   805     iOutBufferPtr = &aBufferPtr;
   736     iOutBufferPtr = &aBufferPtr;
   806     SetCallerStatusPending( aStatus );
   737     SetCallerStatusPending( aStatus );
   807     SelectCertificateL(aLabel, aType);
   738     SelectCertificateL(aLabel, aCertificateKeyId, aType);
   808 }
   739 }
   809 
   740 
   810 // ---------------------------------------------------------------------------
   741 // ---------------------------------------------------------------------------
   811 // ?description_if_needed
   742 // ?description_if_needed
   812 // ---------------------------------------------------------------------------
   743 // ---------------------------------------------------------------------------
   814 void CPKISupport::ContinueRetrieveCertificate()
   745 void CPKISupport::ContinueRetrieveCertificate()
   815 {
   746 {
   816     iSubState = ESSComplete;
   747     iSubState = ESSComplete;
   817     iPending = ECertRetrieve;
   748     iPending = ECertRetrieve;
   818     
   749     
   819     TUint certificateSize = (*iCerts)[0]->Size();
   750     TUint certificateSize = iCerts[0]->Size();
   820     iRequiredBufferLength = certificateSize;
   751     iRequiredBufferLength = certificateSize;
   821     if (certificateSize <= iOutBufferPtr->MaxLength())
   752     if (certificateSize <= iOutBufferPtr->MaxLength())
   822         {        
   753         {        
   823         iCertStore->Retrieve(*(*iCerts)[0], *iOutBufferPtr, iStatus);
   754         iCertStore->Retrieve(*(iCerts[0]), *iOutBufferPtr, iStatus);
   824         SetActive();
   755         SetActive();
   825         }
   756         }
   826     else
   757     else
   827         {
   758         {
   828         iStatus = KRequestPending;
   759         iStatus = KRequestPending;
   844     of this cleanup - it's only necessary once, during PKI startup.
   775     of this cleanup - it's only necessary once, during PKI startup.
   845 */
   776 */
   846 void CPKISupport::CleanupCertListL() 
   777 void CPKISupport::CleanupCertListL() 
   847     {
   778     {
   848     LOG_("Removing invalid certs (MIDP2 certs)");
   779     LOG_("Removing invalid certs (MIDP2 certs)");
   849     if (iCerts) 
   780 
   850         {
   781     TInt certcount = iCerts.Count();
   851         TInt certcount = iCerts->Count();
   782     _LIT(KMidp2Label, "MIDP2");
   852         _LIT(KMidp2Label, "MIDP2");
   783     LOG_1("Total cert count, before cleanup: %d", iCerts.Count());
   853         LOG_1("Total cert count, before cleanup: %d", iCerts->Count());
   784     RMPointerArray<CCTCertInfo> removedInfos;
   854         RMPointerArray<CCTCertInfo> removedInfos;
   785     CleanupClosePushL(removedInfos);
   855         CleanupClosePushL(removedInfos);
   786     for (TInt i = certcount - 1; i >= 0; i--) 
   856         for (TInt i = certcount - 1; i >= 0; i--) 
   787         {
       
   788         CCTCertInfo* info = iCerts[i];
       
   789         if (info->Label().Compare(KMidp2Label) == 0 ||
       
   790             info->CertificateFormat() != EX509Certificate) 
   857             {
   791             {
   858             CCTCertInfo* info = (*iCerts)[i];
   792             // CCTCertInfo has private destructor
   859             if (info->Label().Compare(KMidp2Label) == 0 ||
   793             removedInfos.AppendL( info );
   860                 info->CertificateFormat() != EX509Certificate) 
   794             iCerts.Remove(i);
   861                 {
   795             continue;
   862                 // CCTCertInfo has private destructor
       
   863                 removedInfos.AppendL( info );
       
   864                 iCerts->Remove(i);
       
   865                 continue;
       
   866                 }
       
   867             }
   796             }
   868         CleanupStack::PopAndDestroy(); // removedInfos
   797         }
   869         iCerts->Compress();
   798     CleanupStack::PopAndDestroy(); // removedInfos
   870         LOG_1("Total cert count, after cleanup: %d", iCerts->Count());
   799     iCerts.Compress();
   871         }
   800     LOG_1("Total cert count, after cleanup: %d", iCerts.Count());
   872     else 
       
   873         {
       
   874         LOG_("Certs list empty!");
       
   875         }
       
   876     }
   801     }
   877 
   802 
   878 // ---------------------------------------------------------------------------
   803 // ---------------------------------------------------------------------------
   879 // DoRunOperationL
   804 // DoRunOperationL
   880 // ---------------------------------------------------------------------------
   805 // ---------------------------------------------------------------------------
   911             LOG_("CPKISupport::DoRunOperationL() EInitRetrieveCertList");
   836             LOG_("CPKISupport::DoRunOperationL() EInitRetrieveCertList");
   912             // Cert list might be new. Remove all MIDP2 certificates first,
   837             // Cert list might be new. Remove all MIDP2 certificates first,
   913             // if it hasn't been already done
   838             // if it hasn't been already done
   914             CleanupCertListL();
   839             CleanupCertListL();
   915             iInitState = EInitCompleteImportCerts;
   840             iInitState = EInitCompleteImportCerts;
   916             
   841             if(iImportCounter < iCerts.Count())
   917             //Falls through
   842                 {
       
   843                 ReadNextCertForImportL();
       
   844                 }
       
   845             else
       
   846                 {
       
   847                 LOG_(" Cert store is empty");
       
   848                 iInitState = EInitDone;
       
   849                 iMessage.Complete(KErrNone);
       
   850                 }
       
   851             break;
   918         case EInitCompleteImportCerts:
   852         case EInitCompleteImportCerts:
   919             LOG_("CPKISupport::DoRunOperationL() EInitCompleteImportCerts");            
   853             LOG_("CPKISupport::DoRunOperationL() EInitCompleteImportCerts");            
   920 			if(GetApplicationsOfCTFCertL())
   854 			if(GetApplicationsOfCTFCertL())
   921 				{
   855 				{
   922                 LOG_(" Getting cert's applications returned with FALSE, breaking");
   856                 LOG_(" Getting cert's applications returned with FALSE, breaking");
   923 				break;
   857 				break;
   924 				}
   858 				}
   925             SaveCertInfoToCacheL();    // Handles one certificate, if found
   859             SaveCertInfoToCacheL();    // Handles one certificate, if found
   926             if(iImportCounter < iCerts->Count())
   860             if(iImportCounter < iCerts.Count())
   927                 {
   861                 {
   928                 ReadNextCertForImportL();
   862                 ReadNextCertForImportL();
   929                 }
   863                 }
   930             else
   864             else
   931                 {
   865                 {
   932                 LOG_(" All certificates imported, doing clean ups");
   866                 LOG_(" All certificates imported, doing clean ups");
   933                 // Cleanup
   867                 // Cleanup
   934 
       
   935                 delete iImportCertMapping;
       
   936                 iImportCertMapping = NULL;
       
   937 
       
   938                 delete iImportCertData;
   868                 delete iImportCertData;
   939                 iImportCertData = NULL;
   869                 iImportCertData = NULL;
   940 
   870 
   941                 iCerts->Close();
   871                 iCerts.Close();
   942 
   872 
   943                 delete iCertFilter;
   873                 delete iCertFilter;
   944                 iCertFilter = NULL;
   874                 iCertFilter = NULL;
   945                 iInitState = EInitDone;
   875                 iInitState = EInitDone;
   946                 iMessage.Complete(KErrNone);
   876                 iMessage.Complete(KErrNone);
   983         case PkiService::EReadCertificate:
   913         case PkiService::EReadCertificate:
   984             switch(iSubState)
   914             switch(iSubState)
   985                 {
   915                 {
   986                 case ESSContinue:
   916                 case ESSContinue:
   987                     {
   917                     {
   988                     if(iCerts->Count() > 1)
   918                     if(iCerts.Count() > 1)
   989                         {
   919                         {
   990                         LOG(Log::Printf(_L("Duplicate Certificate\n")));
   920                         LOG(Log::Printf(_L("Duplicate Certificate\n")));
   991                         iSubState = ESSCompleteRequest;
   921                         iSubState = ESSCompleteRequest;
   992                         iSupportStatus = KErrNotFound;
   922                         iSupportStatus = KErrNotFound;
   993                         }
   923                         }
   994                     else if (iCerts->Count() < 1)
   924                     else if (iCerts.Count() < 1)
   995                         {
   925                         {
   996                         LOG(Log::Printf(_L("No matching certificates found\n")));
   926                         LOG(Log::Printf(_L("No matching certificates found\n")));
   997                         iSubState = ESSCompleteRequest;
   927                         iSubState = ESSCompleteRequest;
   998                         iSupportStatus = KErrNotFound;
   928                         iSupportStatus = KErrNotFound;
   999                         }
   929                         }
  1000 					else if ((((*iCerts)[0])->CertificateOwnerType() == EUserCertificate) &&
   930 					else if ((iCerts[0]->CertificateOwnerType() == EUserCertificate) &&
  1001 							(((iCertStoreType == EPkiStoreTypeDevice) && (((*iCerts)[0])->Token().Label().Compare(KDeviceCertStore)!=0)) ||
   931 							(((iCertStoreType == EPkiStoreTypeDevice) && (iCerts[0]->Token().Label().Compare(KDeviceCertStore)!=0)) ||
  1002                     	  	((iCertStoreType == EPkiStoreTypeUser) && (((*iCerts)[0])->Token().Label().Compare(KUserCertStore)!=0))))
   932                     	  	((iCertStoreType == EPkiStoreTypeUser) && (iCerts[0]->Token().Label().Compare(KUserCertStore)!=0))))
  1003 						{
   933 						{
  1004 						LOG_1("User certificate's certificate info does not match certificate store type. Store type is: %d", iCertStoreType);
   934 						LOG_1("User certificate's certificate info does not match certificate store type. Store type is: %d", iCertStoreType);
  1005 						iSubState = ESSCompleteRequest;
   935 						iSubState = ESSCompleteRequest;
  1006 						iSupportStatus = KErrNotFound;                    	
   936 						iSupportStatus = KErrNotFound;                    	
  1007 						}
   937 						}
  1025         case PkiService::ETrusted:
   955         case PkiService::ETrusted:
  1026             switch(iSubState)
   956             switch(iSubState)
  1027                 {
   957                 {
  1028                 case ESSContinue:
   958                 case ESSContinue:
  1029                     {
   959                     {
  1030                     if(iCerts->Count() != 1)
   960                     if(iCerts.Count() != 1)
  1031                         {
   961                         {
  1032                         iSubState = ESSCompleteRequest;
   962                         iSubState = ESSCompleteRequest;
  1033                         iSupportStatus = KErrNotFound;
   963                         iSupportStatus = KErrNotFound;
  1034                         }
   964                         }
  1035                     else
   965                     else
  1063         case PkiService::EApplications:
   993         case PkiService::EApplications:
  1064             switch(iSubState)
   994             switch(iSubState)
  1065                 {
   995                 {
  1066                 case ESSContinue:
   996                 case ESSContinue:
  1067                     {
   997                     {
  1068                     if(iCerts->Count() != 1)
   998                     if(iCerts.Count() != 1)
  1069                         {
   999                         {
  1070                         iSubState = ESSCompleteRequest;
  1000                         iSubState = ESSCompleteRequest;
  1071                         iSupportStatus = KErrNotFound;
  1001                         iSupportStatus = KErrNotFound;
  1072                         }
  1002                         }
  1073                     else
  1003                     else
  1104 
  1034 
  1105         case PkiService::ERemoveCertificate:
  1035         case PkiService::ERemoveCertificate:
  1106             switch(iSubState)
  1036             switch(iSubState)
  1107                 {
  1037                 {
  1108                 case ESSContinue:
  1038                 case ESSContinue:
  1109                     if(iCerts->Count() != 1)
  1039                     if(iCerts.Count() != 1)
  1110                         {
  1040                         {
  1111                         iSupportStatus = KPKIErrNotFound;
  1041                         iSupportStatus = KPKIErrNotFound;
  1112                         iSubState = ESSCompleteRequest;
  1042                         iSubState = ESSCompleteRequest;
  1113                         }
  1043                         }
  1114                     else
  1044                     else
  1139 // ---------------------------------------------------------------------------
  1069 // ---------------------------------------------------------------------------
  1140 // ?description_if_needed
  1070 // ?description_if_needed
  1141 // ---------------------------------------------------------------------------
  1071 // ---------------------------------------------------------------------------
  1142 //
  1072 //
  1143 void CPKISupport::RemoveCertificateL(const TDesC &aLabel, 
  1073 void CPKISupport::RemoveCertificateL(const TDesC &aLabel, 
       
  1074     const TPKIKeyIdentifier& aCertificateKeyId,
  1144     TRequestStatus& aStatus)
  1075     TRequestStatus& aStatus)
  1145 {
  1076 {
  1146     LOG(Log::Printf(_L("Remove certificate\n")));
  1077     LOG(Log::Printf(_L("Remove certificate\n")));
  1147     iSupportStatus = KErrNone;
  1078     iSupportStatus = KErrNone;
  1148     iCerts->Close();
  1079     iCerts.Close();
  1149     iSubState = ESSContinue;
  1080     iSubState = ESSContinue;
  1150     SetCallerStatusPending( aStatus );
  1081     SetCallerStatusPending( aStatus );
  1151     SelectCertificateL(aLabel);
  1082     SelectCertificateL(aLabel, aCertificateKeyId);
  1152 }
  1083 }
  1153 
  1084 
  1154 // ---------------------------------------------------------------------------
  1085 // ---------------------------------------------------------------------------
  1155 // ?description_if_needed
  1086 // ?description_if_needed
  1156 // ---------------------------------------------------------------------------
  1087 // ---------------------------------------------------------------------------
  1157 //
  1088 //
  1158 void CPKISupport::ContinueRemoveCertificate()
  1089 void CPKISupport::ContinueRemoveCertificate()
  1159 {
  1090 {
  1160     iCertStore->Remove(*(*iCerts)[0], iStatus);
  1091     iCertStore->Remove(*(iCerts[0]), iStatus);
  1161     iPending = ECertRemove;
  1092     iPending = ECertRemove;
  1162     iSubState = ESSComplete;
  1093     iSubState = ESSComplete;
  1163     SetActive();
  1094     SetActive();
  1164 }
  1095 }
  1165 
  1096 
  1167 // ---------------------------------------------------------------------------
  1098 // ---------------------------------------------------------------------------
  1168 // ?description_if_needed
  1099 // ?description_if_needed
  1169 // ---------------------------------------------------------------------------
  1100 // ---------------------------------------------------------------------------
  1170 //
  1101 //
  1171 void CPKISupport::SetTrustL(const TDesC &aLabel, 
  1102 void CPKISupport::SetTrustL(const TDesC &aLabel, 
       
  1103     const TPKIKeyIdentifier& aCertificateKeyId,
  1172     TBool aTrusted, TRequestStatus& aStatus)
  1104     TBool aTrusted, TRequestStatus& aStatus)
  1173 {
  1105 {
  1174     LOG(Log::Printf(_L("SetTrust\n")));
  1106     LOG(Log::Printf(_L("SetTrust\n")));
  1175     iSupportStatus = KErrNone;
  1107     iSupportStatus = KErrNone;
  1176     iCerts->Close();
  1108     iCerts.Close();
  1177     iSubState = ESSContinue;
  1109     iSubState = ESSContinue;
  1178     iTrusted = aTrusted;
  1110     iTrusted = aTrusted;
  1179     SetCallerStatusPending( aStatus );
  1111     SetCallerStatusPending( aStatus );
  1180     SelectCertificateL(aLabel);
  1112     SelectCertificateL(aLabel, aCertificateKeyId);
  1181 }
  1113 }
  1182 
  1114 
  1183 // ---------------------------------------------------------------------------
  1115 // ---------------------------------------------------------------------------
  1184 // ?description_if_needed
  1116 // ?description_if_needed
  1185 // ---------------------------------------------------------------------------
  1117 // ---------------------------------------------------------------------------
  1186 //
  1118 //
  1187 void CPKISupport::ContinueSetTrust()
  1119 void CPKISupport::ContinueSetTrust()
  1188 {
  1120 {
  1189     iSubState = ESSComplete;    
  1121     iSubState = ESSComplete;    
  1190     iCertStore->SetTrust(*(*iCerts)[0], iTrusted, iStatus);
  1122     iCertStore->SetTrust(*(iCerts[0]), iTrusted, iStatus);
  1191     iPending = ESetTrust;
  1123     iPending = ESetTrust;
  1192     SetActive();
  1124     SetActive();
  1193 }
  1125 }
  1194 
  1126 
  1195 // ---------------------------------------------------------------------------
  1127 // ---------------------------------------------------------------------------
  1196 // ?description_if_needed
  1128 // ?description_if_needed
  1197 // ---------------------------------------------------------------------------
  1129 // ---------------------------------------------------------------------------
  1198 //
  1130 //
  1199 void CPKISupport::TrustedL(const TDesC &aLabel, TRequestStatus& aStatus)
  1131 void CPKISupport::TrustedL(const TDesC &aLabel, 
       
  1132                            const TPKIKeyIdentifier& aCertificateKeyId, 
       
  1133                            TRequestStatus& aStatus)
  1200 {
  1134 {
  1201     LOG(Log::Printf(_L("Trusted\n")));
  1135     LOG(Log::Printf(_L("Trusted\n")));
  1202     iSupportStatus = KErrNone;
  1136     iSupportStatus = KErrNone;
  1203     iCerts->Close();
  1137     iCerts.Close();
  1204     iSubState = ESSContinue;
  1138     iSubState = ESSContinue;
  1205     SetCallerStatusPending( aStatus );
  1139     SetCallerStatusPending( aStatus );
  1206     SelectCertificateL(aLabel);
  1140     SelectCertificateL(aLabel, aCertificateKeyId);
  1207 }
  1141 }
  1208 
  1142 
  1209 // ---------------------------------------------------------------------------
  1143 // ---------------------------------------------------------------------------
  1210 // ?description_if_needed
  1144 // ?description_if_needed
  1211 // ---------------------------------------------------------------------------
  1145 // ---------------------------------------------------------------------------
  1212 //
  1146 //
  1213 void CPKISupport::ContinueTrusted()
  1147 void CPKISupport::ContinueTrusted()
  1214 {
  1148 {
  1215     iSubState = ESSComplete;
  1149     iSubState = ESSComplete;
  1216     iCertStore->Trusted(*(*iCerts)[0], iTrusted, iStatus);
  1150     iCertStore->Trusted(*(iCerts[0]), iTrusted, iStatus);
  1217     iPending = ETrusted;
  1151     iPending = ETrusted;
  1218     SetActive();
  1152     SetActive();
  1219 }
  1153 }
  1220 
  1154 
  1221 // ---------------------------------------------------------------------------
  1155 // ---------------------------------------------------------------------------
  1222 // ?description_if_needed
  1156 // ?description_if_needed
  1223 // ---------------------------------------------------------------------------
  1157 // ---------------------------------------------------------------------------
  1224 //
  1158 //
  1225 void CPKISupport::SetApplicabilityL(const TDesC &aLabel, const RArray<TUid>& aApplUids, TRequestStatus& aStatus)
  1159 void CPKISupport::SetApplicabilityL(const TDesC &aLabel, 
       
  1160                                     const TPKIKeyIdentifier& aCertificateKeyId,
       
  1161                                     const RArray<TUid>& aApplUids, TRequestStatus& aStatus)
  1226 {
  1162 {
  1227     LOG(Log::Printf(_L("SetApplicability\n")));
  1163     LOG(Log::Printf(_L("SetApplicability\n")));
  1228     iSupportStatus = KErrNone;
  1164     iSupportStatus = KErrNone;
  1229     iCerts->Close();
  1165     iCerts.Close();
  1230     iSubState = ESSContinue;
  1166     iSubState = ESSContinue;
  1231 	iApplUids.Close();
  1167 	iApplUids.Close();
  1232 	for(TInt i = 0;i<aApplUids.Count();i++)
  1168 	for(TInt i = 0;i<aApplUids.Count();i++)
  1233 		{
  1169 		{
  1234 		iApplUids.Append(aApplUids[i]);
  1170 		iApplUids.Append(aApplUids[i]);
  1235 		}
  1171 		}
  1236     SetCallerStatusPending( aStatus );
  1172     SetCallerStatusPending( aStatus );
  1237     SelectCertificateL(aLabel);
  1173     SelectCertificateL(aLabel, aCertificateKeyId);
  1238 }
  1174 }
  1239 
  1175 
  1240 // ---------------------------------------------------------------------------
  1176 // ---------------------------------------------------------------------------
  1241 // ContinueSetApplicability
  1177 // ContinueSetApplicability
  1242 // ---------------------------------------------------------------------------
  1178 // ---------------------------------------------------------------------------
  1243 //
  1179 //
  1244 void CPKISupport::ContinueSetApplicability()
  1180 void CPKISupport::ContinueSetApplicability()
  1245     {
  1181     {
  1246     PKISERVICE_ASSERT(iCerts && iCerts->Count());
  1182     PKISERVICE_ASSERT(iCerts.Count());
  1247     iSubState = ESSComplete;
  1183     iSubState = ESSComplete;
  1248     iCertStore->SetApplicability(*(*iCerts)[0], iApplUids, iStatus);
  1184     iCertStore->SetApplicability(*(iCerts[0]), iApplUids, iStatus);
  1249     iPending = ESetApplicability;
  1185     iPending = ESetApplicability;
  1250     SetActive();
  1186     SetActive();
  1251     }
  1187     }
  1252 
  1188 
  1253 // ---------------------------------------------------------------------------
  1189 // ---------------------------------------------------------------------------
  1254 // ?description_if_needed
  1190 // ?description_if_needed
  1255 // ---------------------------------------------------------------------------
  1191 // ---------------------------------------------------------------------------
  1256 //
  1192 //
  1257 void CPKISupport::ApplicationsL(const TDesC &aLabel, TRequestStatus& aStatus)
  1193 void CPKISupport::ApplicationsL(const TDesC &aLabel, const TPKIKeyIdentifier& aCertificateKeyId,
       
  1194                                 TRequestStatus& aStatus)
  1258 {
  1195 {
  1259     LOG(Log::Printf(_L("Applications\n")));
  1196     LOG(Log::Printf(_L("Applications\n")));
  1260     iSupportStatus = KErrNone;
  1197     iSupportStatus = KErrNone;
  1261     iCerts->Close();
  1198     iCerts.Close();
  1262     iSubState = ESSContinue;
  1199     iSubState = ESSContinue;
  1263     SetCallerStatusPending( aStatus );
  1200     SetCallerStatusPending( aStatus );
  1264     SelectCertificateL(aLabel);
  1201     SelectCertificateL(aLabel, aCertificateKeyId);
  1265 }
  1202 }
  1266 
  1203 
  1267 // ---------------------------------------------------------------------------
  1204 // ---------------------------------------------------------------------------
  1268 // ContinueApplications
  1205 // ContinueApplications
  1269 // ---------------------------------------------------------------------------
  1206 // ---------------------------------------------------------------------------
  1270 //
  1207 //
  1271 void CPKISupport::ContinueApplications()
  1208 void CPKISupport::ContinueApplications()
  1272 {
  1209 {
  1273     iSubState = ESSComplete;
  1210     iSubState = ESSComplete;
  1274 	iApplUids.Close();
  1211 	iApplUids.Close();
  1275     iCertStore->Applications(*(*iCerts)[0], iApplUids, iStatus);
  1212     iCertStore->Applications(*(iCerts[0]), iApplUids, iStatus);
  1276     iPending = EApplications;
  1213     iPending = EApplications;
  1277     SetActive();
  1214     SetActive();
  1278 }
  1215 }
  1279 
  1216 
  1280 
  1217 
  1281 // ---------------------------------------------------------------------------
  1218 // ---------------------------------------------------------------------------
  1282 // SelectCertificateL
  1219 // SelectCertificateL
  1283 // ---------------------------------------------------------------------------
  1220 // ---------------------------------------------------------------------------
  1284 //
  1221 //
  1285 void CPKISupport::SelectCertificateL(const TDesC &aLabel, 
  1222 void CPKISupport::SelectCertificateL(const TDesC &aLabel, 
       
  1223                                      const TPKIKeyIdentifier& aCertificateKeyId,
  1286                                      const TPKICertificateOwnerType& aType )
  1224                                      const TPKICertificateOwnerType& aType )
  1287 {
  1225 {
  1288     delete iCertFilter;
  1226     delete iCertFilter;
  1289     iCertFilter = NULL;
  1227     iCertFilter = NULL;
  1290     iCertFilter = CCertAttributeFilter::NewL();
  1228     iCertFilter = CCertAttributeFilter::NewL();
  1291 
  1229 
  1292     LOG(Log::Printf(_L(" Select by label: %S\n"), &aLabel));
  1230     LOG(Log::Printf(_L(" Select by label: %S\n"), &aLabel));
  1293     iCertFilter->SetLabel(aLabel);
  1231     iCertFilter->SetLabel(aLabel);
       
  1232     iCertFilter->SetSubjectKeyId(aCertificateKeyId);
  1294     if (aType != 0) 
  1233     if (aType != 0) 
  1295         {
  1234         {
  1296         LOG_1(" Select by owner type: %d", aType);
  1235         LOG_1(" Select by owner type: %d", aType);
  1297         iCertFilter->SetOwnerType(TCertificateOwnerType(aType));
  1236         iCertFilter->SetOwnerType(TCertificateOwnerType(aType));
  1298         }
  1237         }
  1308 void CPKISupport::GetCertificateStoreListAsync()
  1247 void CPKISupport::GetCertificateStoreListAsync()
  1309     {
  1248     {
  1310     LOG_1("-> CPKISupport::GetCertificateStoreListAsync() iSubState:%d",
  1249     LOG_1("-> CPKISupport::GetCertificateStoreListAsync() iSubState:%d",
  1311             iSubState );
  1250             iSubState );
  1312     // preconditions
  1251     // preconditions
  1313     PKISERVICE_ASSERT( iCerts && iCertFilter && iStatus != KRequestPending );
  1252     PKISERVICE_ASSERT( iCertFilter && iStatus != KRequestPending );
  1314     LOG_1("CPKISupport::GetSertificateStoreListAsync iSubState:%d", 
  1253     LOG_1("CPKISupport::GetSertificateStoreListAsync iSubState:%d", 
  1315         iSubState );
  1254         iSubState );
  1316 
  1255 
  1317     // Get list of all known certificates
  1256     // Get list of all known certificates
  1318     iCertStore->List(*iCerts, *iCertFilter, iStatus);
  1257     iCertStore->List(iCerts, *iCertFilter, iStatus);
  1319     iPending = EListCerts;
  1258     iPending = EListCerts;
  1320     SetActive();
  1259     SetActive();
  1321     LOG_("<- CPKISupport::GetCertificateStoreListAsync()");
  1260     LOG_("<- CPKISupport::GetCertificateStoreListAsync()");
  1322     }
  1261     }
  1323 
  1262