vpnengine/pkiservice/src/pkiwrapper.cpp
changeset 0 33413c0669b9
child 1 c9c2ad51f972
equal deleted inserted replaced
-1:000000000000 0:33413c0669b9
       
     1 /*
       
     2 * Copyright (c) 2006 - 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 * A class wrapping symbian certificate storage for use by PKI service clients.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #include <x509cert.h>
       
    22 
       
    23 #include "pkiwrapper.h"
       
    24 #include "PKIMapper.h"
       
    25 #include "pkisession.h"
       
    26 #include "pkisupport.h"
       
    27 #include "log_r6.h"
       
    28 
       
    29 
       
    30 
       
    31 CPKIWrapper* CPKIWrapper::NewL(CPKIMapper& aMapper)
       
    32     {
       
    33     CPKIWrapper* self = new (ELeave) CPKIWrapper(aMapper);
       
    34     CleanupStack::PushL(self);
       
    35     self->ConstructL();
       
    36     CleanupStack::Pop();
       
    37     return self;
       
    38     }
       
    39 
       
    40 CPKIWrapper::~CPKIWrapper()
       
    41     {
       
    42     LOG_("-> CPKIWrapper::~CPKIWrapper()");
       
    43     Cancel();
       
    44     delete iCurrentMapping;
       
    45     delete iObjectName;
       
    46     delete iCertBuffer;
       
    47     iUidArray.Close();
       
    48     
       
    49     delete iPKISupport;    
       
    50     LOG_("<- CPKIWrapper::~CPKIWrapper()");
       
    51     }
       
    52 
       
    53 CPKIWrapper::CPKIWrapper(CPKIMapper& aMapper)
       
    54     :CActive(CActive::EPriorityStandard), 
       
    55     iMapper(aMapper),
       
    56     iPtrCertBuffer(0,0)
       
    57     {
       
    58     CActiveScheduler::Add(this);
       
    59     }
       
    60 
       
    61 void CPKIWrapper::ConstructL()
       
    62     {
       
    63     LOG_("-> CPKIWrapper::ConstructL()");   
       
    64     iObjectName = new (ELeave) TBuf<MAX_FILENAME_LENGTH>;
       
    65     iPKISupport = CPKISupport::NewL(iMapper, *this);
       
    66 
       
    67     SetCertStoreType(EPkiStoreTypeAny);
       
    68     SetInformational(EFalse);
       
    69 
       
    70     LOG_("<- CPKIWrapper::ConstructL()");
       
    71     }
       
    72 
       
    73 //====================================================================================================================================    
       
    74 /**
       
    75 *   Initialize
       
    76 */
       
    77 //====================================================================================================================================    
       
    78 void CPKIWrapper::InitializeL(const RMessage2& aMessage)
       
    79 {
       
    80     LOG_("CPKIWrapper::InitializeL()");    
       
    81     iPKISupport->StartInitializeL(aMessage);    
       
    82 }
       
    83     
       
    84 
       
    85 //====================================================================================================================================    
       
    86 /**
       
    87 *   Initialization routine
       
    88 */
       
    89 //====================================================================================================================================    
       
    90 TInt CPKIWrapper::InitOperation(const RMessage2& aMessage)
       
    91 {
       
    92     if (IsActive())
       
    93         {
       
    94         LOG(Log::Printf(_L("Pkiservice busy. Function %d\n"), aMessage.Function()));
       
    95         return KPKIErrServiceBusy;
       
    96         }
       
    97     
       
    98     LOG(Log::Printf(_L("InitOperation function %d\n"), aMessage.Function()));
       
    99     iCurrentStatus = KErrNone;              // Clear status
       
   100     iCurrentState = EExecute;               // Set state
       
   101     iMessage = aMessage;                    // Save message
       
   102     iPKISupport->SetCurrentFunction(iMessage.Function());
       
   103     iIndex = KErrNotFound;
       
   104     
       
   105     // Trigger function
       
   106     iStatus = KRequestPending;
       
   107     SetActive();
       
   108     TRequestStatus *status = &iStatus;
       
   109     User::RequestComplete(status, KErrNone);
       
   110     
       
   111     return KErrNone;
       
   112 }
       
   113     
       
   114 /**---------------------------------------------------------
       
   115  *
       
   116  * CancelPendingOperation
       
   117  *
       
   118  *----------------------------------------------------------*/
       
   119 void CPKIWrapper::CancelPendingOperation()
       
   120 {
       
   121     Cancel();
       
   122     
       
   123     delete iCertBuffer;
       
   124     iCertBuffer = NULL;
       
   125 }
       
   126 
       
   127 /**---------------------------------------------------------
       
   128  *
       
   129  * GetRequiredBufferSize
       
   130  *
       
   131  *----------------------------------------------------------*/
       
   132 TInt CPKIWrapper::GetRequiredBufferSizeL(const RMessage2& aMessage)
       
   133 {
       
   134     iCurrentStatus = KErrNone;                      // Clear status
       
   135     LOG(Log::Printf(_L("Start Function %d\n"), iMessage.Function()));
       
   136     iMessage = aMessage;                            // Save message
       
   137     CompleteRequestAndCleanupL();
       
   138     return iCurrentStatus;
       
   139 }
       
   140 
       
   141 //====================================================================================================================================    
       
   142 /**
       
   143 *   Starter routines
       
   144 */
       
   145 //====================================================================================================================================    
       
   146 
       
   147 void CPKIWrapper::SetTrusted(TBool aValue)
       
   148 {
       
   149     iTrusted = aValue;
       
   150 }
       
   151 
       
   152 void CPKIWrapper::SetApplications(const RArray<TUid>& aApplUids)
       
   153 {
       
   154     iUidArray.Close();
       
   155 	for(TInt i=0;i<aApplUids.Count();i++)
       
   156 		{
       
   157 		iUidArray.Append(aApplUids[i]);
       
   158 		}
       
   159 }
       
   160 
       
   161 
       
   162 /**---------------------------------------------------------
       
   163  *
       
   164  * StartReadCertificateL
       
   165  *
       
   166  *----------------------------------------------------------*/
       
   167 void CPKIWrapper::StartReadCertificateL()
       
   168     {
       
   169     LOG(Log::Printf(_L("StartReadCertificate\n")));
       
   170     iMessage.ReadL(0, iCurrentDescriptor);
       
   171     TInt outDataSize = iMessage.GetDesMaxLength(1);
       
   172     iCertBuffer = HBufC8::NewL(outDataSize);
       
   173     iPtrCertBuffer.Set(iCertBuffer->Des()); // A pointer for changing the contents of buf
       
   174     }
       
   175 
       
   176 
       
   177 
       
   178 /**---------------------------------------------------------
       
   179  *
       
   180  * StartStoreCertificateL
       
   181  *
       
   182  *----------------------------------------------------------*/
       
   183 void CPKIWrapper::StartStoreCertificateL()
       
   184     {
       
   185     iMessage.ReadL(0, iCurrentDescriptor);
       
   186     iObjectName->Copy(iCurrentDescriptor().iObjectName);
       
   187     TInt inDataSize = iMessage.GetDesLength(1);
       
   188     iCertBuffer = HBufC8::NewL(inDataSize);
       
   189     iPtrCertBuffer.Set(iCertBuffer->Des()); // A pointer for changing the contents of buf
       
   190     iMessage.ReadL(1, iPtrCertBuffer); 
       
   191     }
       
   192 
       
   193 /**---------------------------------------------------------
       
   194  *
       
   195  * StartAttachCertificateL
       
   196  *
       
   197  *----------------------------------------------------------*/
       
   198 void CPKIWrapper::StartAttachCertificateL()
       
   199     {
       
   200     iMessage.ReadL(0, iCurrentDescriptor);
       
   201     iObjectName->Copy(iCurrentDescriptor().iObjectName);
       
   202 
       
   203     TInt inDataSize = iMessage.GetDesLength(1);
       
   204     iCertBuffer = HBufC8::NewL(inDataSize);
       
   205     iPtrCertBuffer.Set(iCertBuffer->Des()); // A pointer for changing the contents of buf
       
   206     iMessage.ReadL(1, iPtrCertBuffer); 
       
   207     }
       
   208 
       
   209 
       
   210 /**---------------------------------------------------------
       
   211  *
       
   212  * StartRemoveCertificateL
       
   213  *
       
   214  *----------------------------------------------------------*/
       
   215 void CPKIWrapper::StartRemoveCertificateL()
       
   216     {
       
   217     iMessage.ReadL(0, iCurrentDescriptor);
       
   218     }
       
   219 
       
   220 /**---------------------------------------------------------
       
   221  *
       
   222  * StartSetTrustL
       
   223  *
       
   224  *----------------------------------------------------------*/
       
   225 void CPKIWrapper::StartSetTrustL()
       
   226     {
       
   227     iMessage.ReadL(0, iCurrentDescriptor);
       
   228     TPckg<TBool> pckgTrusted(iTrusted);
       
   229     iMessage.ReadL(1, pckgTrusted, 0);
       
   230     }
       
   231 
       
   232 /**---------------------------------------------------------
       
   233  *
       
   234  * StartTrustedL
       
   235  *
       
   236  *----------------------------------------------------------*/
       
   237 void CPKIWrapper::StartTrustedL()
       
   238     {
       
   239     iMessage.ReadL(0, iCurrentDescriptor);
       
   240     }
       
   241 
       
   242 
       
   243 /**---------------------------------------------------------
       
   244  *
       
   245  * StartSetApplicabilityL
       
   246  *
       
   247  *----------------------------------------------------------*/
       
   248 void CPKIWrapper::StartSetApplicabilityL()
       
   249     {
       
   250     iMessage.ReadL(0, iCurrentDescriptor);
       
   251     TPckg<TUint> pckgCount(iCount);
       
   252     iMessage.ReadL(1, pckgCount);
       
   253     // Allocate list for applications
       
   254     CBufFlat* list = CBufFlat::NewL(sizeof(TUid));
       
   255     CleanupStack::PushL(list);
       
   256     list->ResizeL(iCount * sizeof(TUid));
       
   257     TPtr8 ptrList = list->Ptr(0);
       
   258     iMessage.ReadL(2, ptrList);
       
   259     iUidArray.Close();
       
   260     if(iCount > 0)
       
   261         {
       
   262         TUid tempUid;
       
   263         for (TUint i = 0; i < iCount; i++)
       
   264             {
       
   265             list->Read(i * sizeof(TUid), (TAny*)&tempUid, sizeof(TUid));
       
   266             iUidArray.Append(tempUid);
       
   267             }
       
   268         }
       
   269     CleanupStack::PopAndDestroy(1);     // list
       
   270     }
       
   271 
       
   272 /**---------------------------------------------------------
       
   273  *
       
   274  * StartApplicationsL
       
   275  *
       
   276  *----------------------------------------------------------*/
       
   277 void CPKIWrapper::StartApplicationsL()
       
   278     {
       
   279     iMessage.ReadL(0, iCurrentDescriptor);
       
   280     TPckg<TUint> pckgCount(iMaxCount);
       
   281     iMessage.ReadL(1, pckgCount);
       
   282     }
       
   283 
       
   284     
       
   285 //====================================================================================================================================    
       
   286 /**
       
   287 *   Continuation routines
       
   288 *   Activate active object if necessary
       
   289 */
       
   290 //====================================================================================================================================        
       
   291     
       
   292 
       
   293 /**---------------------------------------------------------
       
   294  *
       
   295  * ExecuteReadCertificateL
       
   296  *
       
   297  *----------------------------------------------------------*/
       
   298 void CPKIWrapper::ExecuteReadCertificateL()
       
   299     {
       
   300     TInt index;
       
   301 
       
   302     LOG(Log::Printf(_L("CPKIWrapper::ExecuteReadCertificateL()\n")));
       
   303     iCurrentStatus = iMapper.ResolveCertMappingL(iCurrentDescriptor(), *iObjectName, 
       
   304                                                            index, iInfoOnly, 
       
   305                                                            iPKISupport->CertStoreType());
       
   306     if(iCurrentStatus == KErrNone)
       
   307         {
       
   308         TSecurityObjectDescriptor sdesc = iCurrentDescriptor();
       
   309         iCurrentState = EComplete;
       
   310         iPKISupport->RetrieveCertificateL(*iObjectName, iPtrCertBuffer, sdesc.iOwnerType, iStatus);
       
   311         SetActive();
       
   312         }
       
   313     LOG_1("CPKIWrapper::ExecuteReadCertificateL() exit:%d", iCurrentStatus);
       
   314     }
       
   315 
       
   316 
       
   317 /**---------------------------------------------------------
       
   318  *
       
   319  * ExecuteStoreCertificateL
       
   320  *
       
   321  *----------------------------------------------------------*/
       
   322 void CPKIWrapper::ExecuteStoreCertificateL()
       
   323     {
       
   324     LOG(Log::Printf(_L("CPKIWrapper::ExecuteStoreCertificateL()\n")));
       
   325     
       
   326     if (iPtrCertBuffer.Length() > 0) 
       
   327         {
       
   328         // See whether the certificate already exists in the store
       
   329         TBool certIsNew = iMapper.CertificateIsUniqueL(
       
   330             iPtrCertBuffer);
       
   331 
       
   332         if (certIsNew)
       
   333             {
       
   334             LOG(Log::Printf(_L("Creating new certificate entry\n")));
       
   335             delete iCurrentMapping;
       
   336             iCurrentMapping = NULL;
       
   337             iCurrentMapping = new (ELeave) CMapDescriptor(iCurrentDescriptor());
       
   338             iMapper.GenerateUniqueNameL(iPtrCertBuffer, *iObjectName);
       
   339             iCurrentMapping->SetMapObjectName(*iObjectName);
       
   340             SaveIdentityL(*iCurrentMapping, iPtrCertBuffer, (TCertificateOwnerType)iCurrentDescriptor().iOwnerType);
       
   341             
       
   342             TPkiServiceStoreType storeType = iPKISupport->CertStoreType();
       
   343             if (storeType == EPkiStoreTypeAny)
       
   344                 {
       
   345                 storeType = EPkiStoreTypeUser;
       
   346                 }
       
   347             iCurrentMapping->SetCertStoreType(storeType);
       
   348 
       
   349             if(iCurrentStatus == KErrNone)
       
   350                 {
       
   351                 LOG(Log::Printf(_L("Status OK, identity saved\n")));
       
   352                 iCurrentStatus = KErrNone;
       
   353                 iCurrentState = EComplete;
       
   354                 LOG(Log::Printf(_L("Storing CERT with LABEL:")));
       
   355                 LOG(Log::Printf(iCurrentMapping->iObjectName));
       
   356                 iPKISupport->StoreCertificateL(iCurrentMapping->iObjectName, 
       
   357                     (TCertificateOwnerType)iCurrentDescriptor().iOwnerType, 
       
   358                     iPtrCertBuffer, iCurrentDescriptor().iIsDeletable, iStatus);
       
   359                 LOG(Log::Printf(_L("CERT stored\n")));
       
   360                 SetActive();
       
   361                 }
       
   362             }
       
   363         else 
       
   364             {
       
   365             // Identical certificate already existed, no need to add this one.
       
   366             // Set current status to KErrBadName so that the operations can
       
   367             // continue (any other error would halt the process)
       
   368             LOG(Log::Printf(_L("Certificate already exists, not adding")));
       
   369             iCurrentStatus = KErrBadName;
       
   370             }
       
   371         }
       
   372     else 
       
   373         {
       
   374         LOG(Log::Printf(_L("Certificate buffer invalid")));
       
   375         iCurrentStatus = KErrGeneral;
       
   376         }
       
   377     }
       
   378 
       
   379 
       
   380 /**---------------------------------------------------------
       
   381  *
       
   382  * ExecuteAttachCertificateL
       
   383  *
       
   384  *----------------------------------------------------------*/
       
   385 void CPKIWrapper::ExecuteAttachCertificateL()
       
   386     {
       
   387     LOG(Log::Printf(_L("ExecuteAttachCertificateL")));
       
   388 
       
   389     iMapper.GenerateUniqueNameL(iPtrCertBuffer, *iObjectName, EUserCertificate);
       
   390 
       
   391     delete iCurrentMapping;
       
   392     iCurrentMapping = NULL;
       
   393     iCurrentMapping = new (ELeave) CMapDescriptor(iCurrentDescriptor());
       
   394     iCurrentMapping->SetMapObjectName(*iObjectName);
       
   395     SaveIdentityL(*iCurrentMapping, iPtrCertBuffer, (TCertificateOwnerType)iCurrentDescriptor().iOwnerType);
       
   396 
       
   397     TPkiServiceStoreType storeType = iPKISupport->CertStoreType();
       
   398     if (storeType == EPkiStoreTypeAny)
       
   399         {
       
   400         storeType = EPkiStoreTypeUser;
       
   401         }
       
   402     iCurrentMapping->SetCertStoreType(storeType);
       
   403     if(iCurrentStatus == KErrNone)
       
   404         {
       
   405         iCurrentStatus = KErrNone;
       
   406         iCurrentState = EComplete;
       
   407         LOG(Log::Printf(_L("Attaching certificate")));
       
   408         LOG(Log::Printf(iCurrentMapping->iObjectName));
       
   409         iPKISupport->AttachCertificateL(iCurrentMapping->iObjectName, iCurrentDescriptor().iSubjectKeyId, iPtrCertBuffer, iStatus);
       
   410         LOG(Log::Printf(_L("Certificate attached")));
       
   411         SetActive();
       
   412         }
       
   413     }
       
   414 
       
   415 
       
   416 /**---------------------------------------------------------
       
   417  *
       
   418  * ExecuteRemoveCertificateL
       
   419  *
       
   420  *----------------------------------------------------------*/
       
   421 void CPKIWrapper::ExecuteRemoveCertificateL()
       
   422     {
       
   423     TInt index;
       
   424 
       
   425     iCurrentStatus = iMapper.ResolveCertMappingL(iCurrentDescriptor(), 
       
   426                                         *iObjectName, index, iInfoOnly,
       
   427                                         iPKISupport->CertStoreType());
       
   428     if(iCurrentStatus == KErrNone)
       
   429         {
       
   430         delete iCurrentMapping;
       
   431         iCurrentMapping = NULL;
       
   432         iCurrentMapping = new (ELeave) CMapDescriptor(iCurrentDescriptor());
       
   433         *iCurrentMapping = iMapper.GetMapDescriptorAtIndex(index);
       
   434         iCurrentState = EComplete;
       
   435         iPKISupport->RemoveCertificateL(*iObjectName, iStatus);
       
   436         SetActive();
       
   437         }
       
   438     }
       
   439 
       
   440 /**---------------------------------------------------------
       
   441  *
       
   442  * ExecuteSetTrustL
       
   443  *
       
   444  *----------------------------------------------------------*/
       
   445 void CPKIWrapper::ExecuteSetTrustL()
       
   446     {
       
   447     TInt index;
       
   448 
       
   449     iCurrentStatus = iMapper.ResolveCertMappingL(iCurrentDescriptor(), *iObjectName, 
       
   450                                                            index, iInfoOnly, 
       
   451                                                            iPKISupport->CertStoreType());
       
   452     if(iCurrentStatus == KErrNone)
       
   453         {
       
   454         const CMapDescriptor& mapping = iMapper.GetMapDescriptorAtIndex(index);
       
   455         if (mapping.iOwnerType == EPKICACertificate)
       
   456             {            
       
   457             iCurrentState = EComplete;
       
   458             iPKISupport->SetTrustL(*iObjectName, iTrusted, iStatus);
       
   459             SetActive();
       
   460             }
       
   461         else
       
   462             {
       
   463             iCurrentStatus = KErrArgument;
       
   464             }
       
   465         }
       
   466     }
       
   467 
       
   468 /**---------------------------------------------------------
       
   469  *
       
   470  * ExecuteTrustedL
       
   471  *
       
   472  *----------------------------------------------------------*/
       
   473 void CPKIWrapper::ExecuteTrustedL()
       
   474     {
       
   475     TInt index;
       
   476 
       
   477     iCurrentStatus = iMapper.ResolveCertMappingL(iCurrentDescriptor(), *iObjectName, 
       
   478                                                             index, iInfoOnly,
       
   479                                                             iPKISupport->CertStoreType());
       
   480     if(iCurrentStatus == KErrNone)
       
   481         {
       
   482         iCurrentState = EComplete;
       
   483         iPKISupport->TrustedL(*iObjectName, iStatus);
       
   484         SetActive();
       
   485         }
       
   486     }
       
   487 
       
   488 /**---------------------------------------------------------
       
   489  *
       
   490  * ExecuteSetApplicabilityL
       
   491  *
       
   492  *----------------------------------------------------------*/
       
   493 void CPKIWrapper::ExecuteSetApplicabilityL()
       
   494     {
       
   495     TInt index(KErrNotFound);
       
   496     iCurrentStatus = iMapper.ResolveCertMappingL(
       
   497         iCurrentDescriptor(), *iObjectName, 
       
   498         index, iInfoOnly,
       
   499         iPKISupport->CertStoreType());
       
   500     
       
   501 	// Save index
       
   502 	iIndex = index;
       
   503     if(iCurrentStatus == KErrNone)
       
   504         {
       
   505         LOG_1("ExecuteSetApplicabilityL:%d", iIndex);
       
   506         iCurrentState = EComplete;
       
   507         iPKISupport->SetApplicabilityL(*iObjectName, iUidArray, iStatus);
       
   508         SetActive();
       
   509         }
       
   510     }
       
   511 
       
   512 /**---------------------------------------------------------
       
   513  *
       
   514  * ExecuteApplicationsL
       
   515  *
       
   516  *----------------------------------------------------------*/
       
   517 void CPKIWrapper::ExecuteApplicationsL()
       
   518 {
       
   519     TInt index;
       
   520 
       
   521     iCurrentStatus = iMapper.ResolveCertMappingL(iCurrentDescriptor(), *iObjectName, 
       
   522                                                            index, iInfoOnly,
       
   523                                                            iPKISupport->CertStoreType());
       
   524     iUidArray.Close();
       
   525     if(iCurrentStatus == KErrNone)
       
   526         {
       
   527         iCurrentState = EComplete;
       
   528         iPKISupport->ApplicationsL(*iObjectName, iStatus);
       
   529         SetActive();
       
   530         }
       
   531 }
       
   532 
       
   533     
       
   534 //====================================================================================================================================    
       
   535 /*
       
   536 *   Completion functions
       
   537 */    
       
   538 //====================================================================================================================================    
       
   539 void CPKIWrapper::CompleteRequestAndCleanupL()
       
   540     {
       
   541     switch (iMessage.Function())
       
   542         {
       
   543         case PkiService::EGetRequiredBufferSize:
       
   544             if (iCurrentStatus == KErrNone)
       
   545                 {
       
   546                 TPckg<TInt> pckgSize(iRequiredBufferLength);
       
   547                 iMessage.WriteL(0, pckgSize);
       
   548                 }
       
   549             break;
       
   550             
       
   551         case PkiService::EReadCertificate:
       
   552             if (iCurrentStatus == KErrNone)
       
   553                 {
       
   554                 iMessage.WriteL(1, iPtrCertBuffer);
       
   555                 }
       
   556             break;
       
   557                         
       
   558         case PkiService::EStoreCertificate:
       
   559             // FALLTROUGH
       
   560         case PkiService::EAttachCertificate:
       
   561             if (iCurrentStatus == KErrNone)
       
   562                 {
       
   563                 User::LeaveIfError( 
       
   564                     iMapper.AddMapping(*iCurrentMapping) );
       
   565                 iCurrentMapping = NULL;    
       
   566                 }
       
   567             if (iCurrentStatus == KErrBadName)
       
   568                 {
       
   569                 // Already exists
       
   570                 iCurrentStatus = KErrNone;
       
   571                 }
       
   572             break;
       
   573                         
       
   574         case PkiService::ERemoveCertificate:
       
   575             if (iCurrentStatus == KErrNone)
       
   576                 {
       
   577                 iMapper.DeleteMapping(*iCurrentMapping);
       
   578                 }
       
   579             break;
       
   580 
       
   581 		case PkiService::ESetApplicability:
       
   582 			if (iCurrentStatus == KErrNone)
       
   583 				{
       
   584 				iMapper.GetMapDescriptorAtIndex(iIndex).iApplUids.Close();
       
   585 				TUint i;
       
   586 				for(i=0;i<iCount;i++)
       
   587 					{
       
   588 					iMapper.GetMapDescriptorAtIndex(iIndex).iApplUids.Append(iUidArray[i]);
       
   589 					}
       
   590 				}
       
   591 			break;
       
   592 
       
   593         case PkiService::ETrusted:
       
   594             if (iCurrentStatus == KErrNone)
       
   595                 {
       
   596                 TPckgC<TBool> pckgTrusted(iTrusted);
       
   597                 iMessage.WriteL(1, pckgTrusted);
       
   598                 }
       
   599             break;
       
   600             
       
   601         case PkiService::EApplications:
       
   602             if (iCurrentStatus == KErrNone)
       
   603                 {
       
   604                 TUint pos = 0;
       
   605                 CBufFlat* list = CBufFlat::NewL(sizeof(TUid));
       
   606                 CleanupStack::PushL(list);
       
   607 				iCount = iUidArray.Count();
       
   608 				if(iCount > iMaxCount)
       
   609 					{
       
   610 					// Prevent overrun
       
   611 					iCount = iMaxCount;
       
   612 					}
       
   613                 list->ResizeL(iCount * sizeof(TUid));
       
   614 
       
   615                 for(TUint i = 0; i < iCount; i++)
       
   616                     {
       
   617                     list->Write(pos * sizeof(TUid),
       
   618                                 (TAny*)&iUidArray[i].iUid,
       
   619                                 sizeof(TUid));
       
   620                     pos++;
       
   621                     }
       
   622                 
       
   623                 TPckgC<TUint> pckgCount(iCount);
       
   624                 iMessage.WriteL(1, pckgCount);
       
   625                 TPtr8 ptrList = list->Ptr(0);
       
   626                 iMessage.WriteL(2, ptrList);
       
   627 
       
   628                 CleanupStack::PopAndDestroy(1); // list
       
   629                 }
       
   630             break;
       
   631         }
       
   632     
       
   633     LOG(Log::Printf(_L("Complete function %d, status %d\n"), 
       
   634         iMessage.Function(), iCurrentStatus));
       
   635 
       
   636     delete iCertBuffer;
       
   637     delete iCurrentMapping;
       
   638     iCurrentMapping = NULL;
       
   639     iCertBuffer = NULL;
       
   640     iMessage.Complete(iCurrentStatus);
       
   641     }
       
   642 
       
   643 
       
   644     
       
   645 //====================================================================================================================================    
       
   646 /*
       
   647 *   Active object functions
       
   648 */    
       
   649 //====================================================================================================================================    
       
   650 void CPKIWrapper::RunL()
       
   651      {
       
   652      TRequestStatus *status = &iStatus;
       
   653      iCurrentStatus = iStatus.Int(); // Status from interface active object     
       
   654      switch (iCurrentState)
       
   655          {
       
   656          case EExecute:
       
   657              // Begin case EExecute
       
   658              if(iCurrentStatus == KErrNone)
       
   659                  {
       
   660                  switch ( iMessage.Function() )
       
   661                      {                             
       
   662                      case PkiService::ESetTrust:
       
   663                          StartSetTrustL();
       
   664                          ExecuteSetTrustL();
       
   665                          break;
       
   666 
       
   667                      case PkiService::ETrusted:
       
   668                          StartTrustedL();
       
   669                          ExecuteTrustedL();
       
   670                          break;
       
   671                          
       
   672                      case PkiService::ESetApplicability:
       
   673                          StartSetApplicabilityL();
       
   674                          ExecuteSetApplicabilityL();
       
   675                          break;
       
   676 
       
   677                      case PkiService::EApplications:
       
   678                          StartApplicationsL();
       
   679                          ExecuteApplicationsL();
       
   680                          break;
       
   681 
       
   682                      case PkiService::EReadCertificate:
       
   683                          StartReadCertificateL();
       
   684                          ExecuteReadCertificateL();
       
   685                          break;
       
   686 
       
   687                      case PkiService::EStoreCertificate:
       
   688                          StartStoreCertificateL();
       
   689                          ExecuteStoreCertificateL();
       
   690                          break;
       
   691 
       
   692                      case PkiService::EAttachCertificate:
       
   693                          StartAttachCertificateL();
       
   694                          ExecuteAttachCertificateL();
       
   695                          break;
       
   696 
       
   697                      case PkiService::ERemoveCertificate:
       
   698                          StartRemoveCertificateL();
       
   699                          ExecuteRemoveCertificateL();
       
   700                          break;
       
   701                      default:
       
   702                          iCurrentStatus = KPKIErrNotSupported;
       
   703                          iCurrentState = EComplete;
       
   704                          break;                           
       
   705                      }                   
       
   706                  }
       
   707              if(iCurrentStatus != KErrNone)
       
   708                  {
       
   709                  // Trigger completion
       
   710                  iCurrentState = EComplete;
       
   711                  iStatus = KRequestPending;
       
   712                  SetActive();
       
   713                  User::RequestComplete(status, iCurrentStatus);
       
   714                  }
       
   715              break;
       
   716              // End case EExecute
       
   717 
       
   718          case EComplete:
       
   719              // Begin case EComplete
       
   720              if(iCurrentStatus == KPKIErrBufferTooShort)
       
   721                  {
       
   722                  iRequiredBufferLength = iPKISupport->GetRequiredBufferSize();
       
   723                  }
       
   724              if (iMessage.Function() == PkiService::ELogon)
       
   725                  {
       
   726                  iCurrentStatus = iStatus.Int();
       
   727                 }
       
   728              CompleteRequestAndCleanupL();
       
   729              break;
       
   730              // End case EComplete
       
   731          default:
       
   732             LOG_1("CPKIWrapper::RunL unknown State:%d", iCurrentState);
       
   733             break;    
       
   734          }
       
   735      }
       
   736 
       
   737 TInt CPKIWrapper::RunError(TInt aError)
       
   738     {
       
   739     LOG(Log::Printf(_L("CPKIWrapper::RunError, Complete function %d, status %d\n"), iMessage.Function(), aError));
       
   740     delete iCertBuffer;
       
   741     delete iCurrentMapping;
       
   742     iCurrentMapping = NULL; 
       
   743     iCertBuffer = NULL;
       
   744     
       
   745     iMessage.Complete(aError);
       
   746     return KErrNone;
       
   747     }
       
   748 
       
   749 void CPKIWrapper::DoCancel()
       
   750     {
       
   751     LOG_1("Cancel function %d", iMessage.Function());
       
   752     if ( iPKISupport )
       
   753         {
       
   754         iPKISupport->Cancel();
       
   755         }
       
   756     iMessage.Complete(KErrCancel);
       
   757     }
       
   758       
       
   759 
       
   760 
       
   761 void CPKIWrapper::SaveIdentityL(CMapDescriptor &aCertDesc, 
       
   762                                 const TDesC8& aCertDataIn,
       
   763                                 TCertificateOwnerType aOwner)
       
   764 {
       
   765     LOG(Log::Printf(_L("CPKIWrapper::SaveIdentityL()\n")));
       
   766 
       
   767     CX509Certificate* certificate = CX509Certificate::NewLC(aCertDataIn);            
       
   768 
       
   769     // Validity period
       
   770     aCertDesc.SetMapStartTime(certificate->ValidityPeriod().Start());
       
   771     aCertDesc.SetMapEndTime(certificate->ValidityPeriod().Finish());    
       
   772     
       
   773     
       
   774     // Copy issuer
       
   775     const TPtrC8* issuer = certificate->DataElementEncoding(CX509Certificate::EIssuerName);
       
   776     aCertDesc.SetMapTrustedAuthorityL(*issuer);
       
   777 
       
   778     // Copy subject name
       
   779     const TPtrC8* subject = certificate->DataElementEncoding(CX509Certificate::ESubjectName);    
       
   780     aCertDesc.SetMapIdentitySubjectNameL(*subject);
       
   781 
       
   782     // Copy rfc822 name from subjectAlt name    
       
   783     const CX509CertExtension* subjAltName = certificate->Extension(KSubjectAltName);
       
   784     if(subjAltName != NULL)
       
   785         {
       
   786         CX509AltNameExt* subjectAlt = CX509AltNameExt::NewLC(subjAltName->Data());
       
   787         if(subjectAlt != NULL)
       
   788             {
       
   789             const CArrayPtrFlat<CX509GeneralName> *nameArray; 
       
   790             nameArray = &subjectAlt->AltName();
       
   791             // Search rfc822
       
   792             for(TInt i = 0; i < nameArray->Count(); i++)
       
   793                 {
       
   794                 if(nameArray->At(i)->Tag() == EX509RFC822Name)
       
   795                     {
       
   796                     TPtrC8 data = nameArray->At(i)->Data();
       
   797                     aCertDesc.SetMapIdentityRfc822NameL(data.Right(data.Length() - 2));
       
   798                     break;
       
   799                     }
       
   800                 }                            
       
   801             }
       
   802         CleanupStack::PopAndDestroy(subjectAlt);
       
   803         }
       
   804 
       
   805     // Key usage
       
   806     const CX509CertExtension* keyUsage = certificate->Extension(KKeyUsage);
       
   807     if((keyUsage != NULL) && keyUsage->Critical())
       
   808         {
       
   809         aCertDesc.iKeyUsageDer.Copy(keyUsage->Data());        
       
   810         }
       
   811 
       
   812     // Serial number
       
   813 	const TPtrC8* serial = certificate->DataElementEncoding(CX509Certificate::ESerialNumber);
       
   814 	if(serial != NULL)
       
   815 		{
       
   816 		aCertDesc.SetMapSerialNumberL(*serial);               
       
   817 		}
       
   818     
       
   819 	// Set Subject Key Identifier if we are handling CA
       
   820 	if(aOwner == ECACertificate)
       
   821 	{
       
   822 	    TPKIKeyIdentifier keyId = certificate->SubjectKeyIdentifierL();
       
   823 	    aCertDesc.SetMapSubjectKeyId(keyId);
       
   824 	}
       
   825 	
       
   826     CleanupStack::PopAndDestroy(certificate); 
       
   827     
       
   828 
       
   829     if(CPKIMapper::CertValidity(aCertDesc.iStartTime, aCertDesc.iEndTime) == EExpired)
       
   830     {
       
   831         LOG(Log::Printf(_L("Certificate expired\n")));
       
   832     }
       
   833 }
       
   834 
       
   835 
       
   836 void CPKIWrapper::SetCertStoreType(TPkiServiceStoreType aStoreType)
       
   837     {
       
   838 	LOG(Log::Printf(_L("CPKIWrapper: SETTING CERT STORE TYPE: %d\n"), aStoreType));
       
   839     iPKISupport->SetCertStoreType(aStoreType);
       
   840     }
       
   841 
       
   842     
       
   843     
       
   844 TPkiServiceStoreType CPKIWrapper::CertStoreType() const
       
   845     {
       
   846     return iPKISupport->CertStoreType();
       
   847     }
       
   848         
       
   849 void CPKIWrapper::SetInformational(const TBool aInfoOnly) 
       
   850     {
       
   851     iInfoOnly = aInfoOnly;
       
   852     }
       
   853