cpsecplugins/cpadvancedsecplugin/src/cpcertmanuisyncwrapper.cpp
branchRCL_3
changeset 49 09b1ac925e3f
equal deleted inserted replaced
47:63339781d179 49:09b1ac925e3f
       
     1 /*
       
     2 * Copyright (c) 2010 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:   Implements a synchronous wrapper for easier use of Symbian's
       
    15 *                Security Frameworks's API's.
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include <unifiedcertstore.h>
       
    22 #include <unifiedkeystore.h>
       
    23 #include <mctwritablecertstore.h>
       
    24 #include <x509cert.h>
       
    25 #include <pkixcertchain.h>
       
    26 #include <../../inc/cpsecplugins.h>
       
    27 #include "cpcertmanuisyncwrapper.h"
       
    28 
       
    29 // CONSTANTS
       
    30 _LIT_SECURITY_POLICY_C1( KKeyStoreUsePolicy, ECapabilityReadUserData );
       
    31 // Maximum length of a certificate
       
    32 const TInt KMaxCertificateLength = 5000;
       
    33 const TInt KMaxKeyLength = 10000;
       
    34 
       
    35 // -----------------------------------------------------------------------------
       
    36 // CpCertManUISyncWrapper::ListL
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 void CpCertManUISyncWrapper::ListL(
       
    40     CUnifiedCertStore*& aStore,
       
    41     RMPointerArray<CCTCertInfo>* aArray,
       
    42     const CCertAttributeFilter& aFilter,
       
    43     const TUid aTokenUid )
       
    44     {
       
    45     RDEBUG("0", 0);
       
    46     if ( IsActive() )
       
    47         {
       
    48         // Wrapper is active. Don't go further
       
    49         User::Leave(KErrGeneral);
       
    50         }
       
    51 
       
    52     TInt count = aStore->CertStoreCount();
       
    53     for ( TInt ii = 0; ii < count; ii++ )
       
    54         {
       
    55         MCTCertStore& certstore = aStore->CertStore( ii );
       
    56         MCTToken& token = certstore.Token();
       
    57         TUid tokenuid = token.Handle().iTokenTypeUid;
       
    58         if ( tokenuid == aTokenUid )
       
    59             {
       
    60             certstore.List( *aArray, aFilter, iStatus );
       
    61             iOperation = EOperationList;
       
    62             iStore = aStore;
       
    63             SetActive();
       
    64             iWait.Start();
       
    65             User::LeaveIfError( iStatus.Int() );
       
    66             break;
       
    67             }
       
    68         }
       
    69     iOperation = EOperationNone;
       
    70     }
       
    71 
       
    72 // -----------------------------------------------------------------------------
       
    73 // CpCertManUISyncWrapper::ListL
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 void CpCertManUISyncWrapper::ListL(
       
    77     CUnifiedKeyStore*& aStore,
       
    78     RMPointerArray<CCTKeyInfo>* aArray,
       
    79     const TCTKeyAttributeFilter& aFilter )
       
    80     {
       
    81     RDEBUG("0", 0);
       
    82     if ( IsActive() )
       
    83         {
       
    84         // Wrapper is active. Don't go further
       
    85 		User::Leave(KErrGeneral);
       
    86         }
       
    87 
       
    88     aStore->List( *aArray, aFilter, iStatus );
       
    89     iOperation = EOperationKeyList;
       
    90     iKeyStore = aStore;
       
    91     SetActive();
       
    92     iWait.Start();
       
    93     User::LeaveIfError( iStatus.Int() );
       
    94     iOperation = EOperationNone;
       
    95     }
       
    96 
       
    97 // -----------------------------------------------------------------------------
       
    98 // CpCertManUISyncWrapper::GetCertificateL
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 void CpCertManUISyncWrapper::GetCertificateL( CUnifiedCertStore*& aStore,
       
   102     const CCTCertInfo& aCertInfo,
       
   103     CCertificate*& aCert,
       
   104     TUid aTokenUid )
       
   105     {
       
   106     aCert = NULL;
       
   107     RDEBUG("0", 0);
       
   108     HBufC8* buf = HBufC8::NewLC( KMaxCertificateLength );
       
   109     iCertPtr.Set( buf->Des() );
       
   110 
       
   111     TInt count = aStore->CertStoreCount();
       
   112     for (TInt i = 0; i < count; i++)
       
   113         {
       
   114         MCTCertStore& certstore = aStore->CertStore( i );
       
   115         MCTToken& token = certstore.Token();
       
   116         TUid tokenuid = token.Handle().iTokenTypeUid;
       
   117         if ( tokenuid == aTokenUid )
       
   118             {
       
   119             certstore.Retrieve( aCertInfo, iCertPtr, iStatus );
       
   120             iOperation = EGetCertificate;
       
   121             iStore = aStore;
       
   122             SetActive();
       
   123             iWait.Start();
       
   124             User::LeaveIfError( iStatus.Int() );
       
   125             break;
       
   126             }
       
   127         }
       
   128 
       
   129     
       
   130 	switch ( aCertInfo.CertificateFormat() )
       
   131 		{
       
   132 		case EX509Certificate:
       
   133 			{
       
   134 			aCert = CX509Certificate::NewL( iCertPtr );
       
   135 			break;
       
   136 			}
       
   137 		case EX509CertificateUrl:
       
   138 			{
       
   139 			break;
       
   140 			}
       
   141 		default:
       
   142 			{
       
   143 			break;
       
   144 			}
       
   145 		}
       
   146         
       
   147     CleanupStack::PopAndDestroy(buf);  
       
   148     iOperation = EOperationNone;
       
   149 
       
   150     }
       
   151 
       
   152 // -----------------------------------------------------------------------------
       
   153 // CpCertManUISyncWrapper::DeleteCertL
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 void CpCertManUISyncWrapper::DeleteCertL( CUnifiedCertStore*& aStore,
       
   157     const CCTCertInfo& aCertInfo )
       
   158     {
       
   159     RDEBUG("0", 0);
       
   160     aStore->Remove( aCertInfo, iStatus );
       
   161     iOperation = EOperationDelete;
       
   162     iStore = aStore;
       
   163     SetActive();
       
   164     iWait.Start();
       
   165     User::LeaveIfError( iStatus.Int() );
       
   166     iOperation = EOperationNone;
       
   167     }
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // CpCertManUISyncWrapper::DeleteCertL
       
   171 // -----------------------------------------------------------------------------
       
   172 //
       
   173 void CpCertManUISyncWrapper::DeleteCertL( CUnifiedCertStore*& aStore,
       
   174     const CCTCertInfo& aCertInfo,
       
   175     TUid aTokenUid )
       
   176     {
       
   177     RDEBUG("0", 0);
       
   178     TInt count = aStore->WritableCertStoreCount();
       
   179     for (TInt i = 0; i < count; i++)
       
   180         {
       
   181         MCTWritableCertStore& writablestore = aStore->WritableCertStore( i );
       
   182         MCTToken& token = writablestore.Token();
       
   183         TUid tokenuid = token.Handle().iTokenTypeUid;
       
   184         if ( tokenuid == aTokenUid )
       
   185             {
       
   186             writablestore.Remove( aCertInfo, iStatus );
       
   187             iOperation = EOperationDelete;
       
   188             iStore = aStore;
       
   189             SetActive();
       
   190             iWait.Start();
       
   191             User::LeaveIfError( iStatus.Int() );
       
   192             break;
       
   193             }
       
   194         }
       
   195     iOperation = EOperationNone;
       
   196     }
       
   197 
       
   198 // ============================ MEMBER FUNCTIONS ===============================
       
   199 
       
   200 // -----------------------------------------------------------------------------
       
   201 // CpCertManUISyncWrapper::CpCertManUISyncWrapper()
       
   202 // C++ default constructor can NOT contain any code, that
       
   203 // might leave.
       
   204 // -----------------------------------------------------------------------------
       
   205 //
       
   206 CpCertManUISyncWrapper::CpCertManUISyncWrapper() : CActive( EPriorityStandard ), iCertPtr(0,0)
       
   207     {
       
   208     CActiveScheduler::Add( this );
       
   209     }
       
   210 
       
   211 // -----------------------------------------------------------------------------
       
   212 // CpCertManUISyncWrapper::ConstructL()
       
   213 // Symbian 2nd phase constructor can leave.
       
   214 // -----------------------------------------------------------------------------
       
   215 //
       
   216 void CpCertManUISyncWrapper::ConstructL()
       
   217     {
       
   218     }
       
   219 
       
   220 
       
   221 // -----------------------------------------------------------------------------
       
   222 // CpCertManUISyncWrapper::NewLC
       
   223 // Two-phased constructor.
       
   224 // -----------------------------------------------------------------------------
       
   225 //
       
   226 CpCertManUISyncWrapper* CpCertManUISyncWrapper::NewLC()
       
   227     {
       
   228     CpCertManUISyncWrapper* wrap = new ( ELeave ) CpCertManUISyncWrapper();
       
   229     CleanupStack::PushL( wrap );
       
   230     wrap->ConstructL();
       
   231     return wrap;
       
   232     }
       
   233 
       
   234 // -----------------------------------------------------------------------------
       
   235 // CpCertManUISyncWrapper::NewL
       
   236 // Two-phased constructor.
       
   237 // -----------------------------------------------------------------------------
       
   238 //
       
   239 CpCertManUISyncWrapper* CpCertManUISyncWrapper::NewL()
       
   240     {
       
   241     CpCertManUISyncWrapper* wrap = CpCertManUISyncWrapper::NewLC();
       
   242     CleanupStack::Pop(wrap);
       
   243     return wrap;
       
   244     }
       
   245 
       
   246 // Destructor
       
   247 CpCertManUISyncWrapper::~CpCertManUISyncWrapper()
       
   248     {
       
   249     }
       
   250 
       
   251 // -----------------------------------------------------------------------------
       
   252 // CpCertManUISyncWrapper::RunL
       
   253 // If no errors happened, stop. Show an error note if needed.
       
   254 // -----------------------------------------------------------------------------
       
   255 //
       
   256 
       
   257 void CpCertManUISyncWrapper::RunL()
       
   258     {
       
   259     RDEBUG("0", 0);
       
   260     if ( iWait.IsStarted() )
       
   261         {
       
   262         iWait.AsyncStop();
       
   263         }
       
   264 
       
   265     }
       
   266 
       
   267 // -----------------------------------------------------------------------------
       
   268 // CpCertManUISyncWrapper::DoCancel
       
   269 // Cancels the ongoing operation if possible.
       
   270 // -----------------------------------------------------------------------------
       
   271 //
       
   272 void CpCertManUISyncWrapper::DoCancel()
       
   273     {
       
   274     RDEBUG("iOperation", iOperation);
       
   275     switch ( iOperation )
       
   276         {
       
   277         case EOperationInit:
       
   278             {
       
   279             iStore->CancelInitialize();
       
   280             break;
       
   281             }
       
   282         case EOperationList:
       
   283             {
       
   284             iStore->CancelList();
       
   285             break;
       
   286             }
       
   287         case EGetCertificate:
       
   288             {
       
   289             iStore->CancelRetrieve();
       
   290             break;
       
   291             }
       
   292         case EAddCertificate:
       
   293             {
       
   294             iStore->Cancel();
       
   295             break;
       
   296             }
       
   297         case ERetriveURLCertificate:
       
   298             {
       
   299             iStore->CancelRetrieve();
       
   300             break;
       
   301             }
       
   302         case EOperationDelete:
       
   303             {
       
   304             iStore->CancelRemove();
       
   305             break;
       
   306             }
       
   307         case EOperationGetApps:
       
   308             {
       
   309             iStore->CancelApplications();
       
   310             break;
       
   311             }
       
   312         case EOperationValidateX509Root:
       
   313             {
       
   314             iChain->CancelValidate();
       
   315             break;
       
   316             }
       
   317         case EShowErrorNote:
       
   318             {
       
   319             break;
       
   320             }
       
   321         case EOperationInitKeyStore:
       
   322             {
       
   323             iKeyStore->CancelInitialize();
       
   324             break;
       
   325             }
       
   326         case EOperationKeyList:
       
   327             {
       
   328             iKeyStore->CancelList();
       
   329             break;
       
   330             }
       
   331         case EOperationExportKey:
       
   332             {
       
   333             iKeyStore->CancelExportKey();
       
   334             break;
       
   335             }
       
   336         case EOperationImportKey:
       
   337             {
       
   338             iKeyStore->CancelImportKey();
       
   339             break;
       
   340             }
       
   341         case EOperationDeleteKey:
       
   342             {
       
   343             iKeyStore->CancelDeleteKey();
       
   344             break;
       
   345             }
       
   346         default:
       
   347             {
       
   348             break;
       
   349             }
       
   350         }
       
   351     }
       
   352 
       
   353 // -----------------------------------------------------------------------------
       
   354 // CpCertManUISyncWrapper::InitStoreL
       
   355 // -----------------------------------------------------------------------------
       
   356 //
       
   357 void CpCertManUISyncWrapper::InitStoreL( CUnifiedCertStore*& aStore )
       
   358     {
       
   359     aStore->Initialize( iStatus );
       
   360     iOperation = EOperationInit;
       
   361     iStore = aStore;
       
   362     SetActive();
       
   363     iWait.Start();
       
   364     User::LeaveIfError( iStatus.Int() );
       
   365     iOperation = EOperationNone;
       
   366     }
       
   367 
       
   368 // -----------------------------------------------------------------------------
       
   369 // CpCertManUISyncWrapper::InitStoreL
       
   370 // -----------------------------------------------------------------------------
       
   371 //
       
   372 void CpCertManUISyncWrapper::InitStoreL( CUnifiedKeyStore*& aStore  )
       
   373     {
       
   374     aStore->Initialize( iStatus );
       
   375     iOperation = EOperationInitKeyStore;
       
   376     iKeyStore = aStore;
       
   377     SetActive();
       
   378     iWait.Start();
       
   379     User::LeaveIfError( iStatus.Int() );
       
   380     iOperation = EOperationNone;
       
   381     }
       
   382 
       
   383 // -----------------------------------------------------------------------------
       
   384 // CpCertManUISyncWrapper::ListL
       
   385 // -----------------------------------------------------------------------------
       
   386 //
       
   387 void CpCertManUISyncWrapper::ListL(
       
   388     CUnifiedCertStore*& aStore,
       
   389     RMPointerArray<CCTCertInfo>* aArray,
       
   390     const CCertAttributeFilter& aFilter )
       
   391     {
       
   392     
       
   393     if ( IsActive() )
       
   394         {
       
   395         // Wrapper is active. Don't go further
       
   396 		User::Leave(KErrGeneral);
       
   397         }
       
   398 
       
   399     aStore->List( *aArray, aFilter, iStatus );
       
   400     iOperation = EOperationList;
       
   401     iStore = aStore;
       
   402     SetActive();
       
   403     iWait.Start();
       
   404     User::LeaveIfError( iStatus.Int() );
       
   405     iOperation = EOperationNone;
       
   406     }
       
   407 
       
   408 // -----------------------------------------------------------------------------
       
   409 // CpCertManUISyncWrapper::ListL
       
   410 // -----------------------------------------------------------------------------
       
   411 //
       
   412 void CpCertManUISyncWrapper::ListL(
       
   413     CUnifiedKeyStore*& aStore,
       
   414     RMPointerArray<CCTKeyInfo>* aArray,
       
   415     const TCTKeyAttributeFilter& aFilter,
       
   416     const TUid aTokenUid )
       
   417     {
       
   418     
       
   419     if ( IsActive() )
       
   420         {
       
   421         // Wrapper is active. Don't go further
       
   422 		User::Leave(KErrGeneral);
       
   423         }
       
   424 
       
   425     TInt count = aStore->KeyStoreManagerCount();
       
   426     for ( TInt ii = 0; ii < count; ii++ )
       
   427         {
       
   428         MCTKeyStoreManager& keystoremanager = aStore->KeyStoreManager( ii );
       
   429         MCTToken& token = keystoremanager.Token();
       
   430         TUid tokenuid = token.Handle().iTokenTypeUid;
       
   431         if ( tokenuid == aTokenUid )
       
   432             {
       
   433             keystoremanager.List( *aArray, aFilter, iStatus );
       
   434             iOperation = EOperationKeyList;
       
   435             iKeyStore = aStore;
       
   436             SetActive();
       
   437             iWait.Start();
       
   438             User::LeaveIfError( iStatus.Int() );
       
   439             break;
       
   440             }
       
   441         }
       
   442     iOperation = EOperationNone;
       
   443     }
       
   444 
       
   445 // -----------------------------------------------------------------------------
       
   446 // CpCertManUISyncWrapper::GetCertificateL
       
   447 // -----------------------------------------------------------------------------
       
   448 //
       
   449 void CpCertManUISyncWrapper::GetCertificateL(
       
   450     CUnifiedCertStore*& aStore,
       
   451     const CCTCertInfo& aCertInfo,
       
   452     CCertificate*& aCert )
       
   453     {    
       
   454     aCert = NULL;
       
   455     HBufC8* buf = HBufC8::NewLC( KMaxCertificateLength );
       
   456     iCertPtr.Set( buf->Des() );
       
   457     aStore->Retrieve( aCertInfo, iCertPtr, iStatus);
       
   458     iOperation = EGetCertificate;
       
   459     iStore = aStore;
       
   460     SetActive();
       
   461     iWait.Start();
       
   462     User::LeaveIfError( iStatus.Int() );
       
   463     
       
   464 	switch ( aCertInfo.CertificateFormat() )
       
   465 		{
       
   466 		case EX509Certificate:
       
   467 			{
       
   468 			aCert = CX509Certificate::NewL( iCertPtr );
       
   469 			break;
       
   470 			}
       
   471 		case EX509CertificateUrl:
       
   472 			{
       
   473 			break;
       
   474 			}
       
   475 		default:
       
   476 			{
       
   477 			break;
       
   478 			}
       
   479 		}
       
   480     CleanupStack::PopAndDestroy(buf); 
       
   481     iOperation = EOperationNone;
       
   482     }
       
   483 
       
   484 // -----------------------------------------------------------------------------
       
   485 // CpCertManUISyncWrapper::ValidateX509RootCertificateL
       
   486 // -----------------------------------------------------------------------------
       
   487 //
       
   488 void CpCertManUISyncWrapper::ValidateX509RootCertificateL(
       
   489     CPKIXValidationResult*& aValidationResult,
       
   490     const TTime& aValidationTime, CPKIXCertChain*& aChain )
       
   491     {
       
   492     
       
   493     aChain->ValidateL( *aValidationResult, aValidationTime, iStatus );
       
   494     iOperation = EOperationValidateX509Root;
       
   495     iChain = aChain;
       
   496     SetActive();
       
   497     iWait.Start();
       
   498     iOperation = EOperationNone;
       
   499     User::LeaveIfError(iStatus.Int());
       
   500     }
       
   501 
       
   502 // -----------------------------------------------------------------------------
       
   503 // CpCertManUISyncWrapper::GetUrlCertificateL
       
   504 // -----------------------------------------------------------------------------
       
   505 //
       
   506 void CpCertManUISyncWrapper::GetUrlCertificateL(
       
   507     CUnifiedCertStore*& aStore,
       
   508     const CCTCertInfo& aCertInfo,
       
   509     TDes8& aUrl )
       
   510     {
       
   511     
       
   512     aStore->Retrieve( aCertInfo, aUrl, iStatus);
       
   513     iOperation = ERetriveURLCertificate;
       
   514     iStore = aStore;
       
   515     SetActive();
       
   516     iWait.Start();
       
   517     User::LeaveIfError( iStatus.Int() );
       
   518     iOperation = EOperationNone;
       
   519     }
       
   520 
       
   521 // -----------------------------------------------------------------------------
       
   522 // CpCertManUISyncWrapper::GetApplicationsL
       
   523 // -----------------------------------------------------------------------------
       
   524 //
       
   525 void CpCertManUISyncWrapper::GetApplicationsL( CUnifiedCertStore*& aStore,
       
   526     const CCTCertInfo& aCertInfo, RArray<TUid>& aApps )
       
   527     {
       
   528     
       
   529     aStore->Applications( aCertInfo, aApps, iStatus );
       
   530     iOperation = EOperationGetApps;
       
   531     iStore = aStore;
       
   532     SetActive();
       
   533     iWait.Start();
       
   534     User::LeaveIfError( iStatus.Int() );
       
   535     iOperation = EOperationNone;
       
   536     }
       
   537 
       
   538 // -----------------------------------------------------------------------------
       
   539 // CpCertManUISyncWrapper::SetApplicabilityL
       
   540 // -----------------------------------------------------------------------------
       
   541 //
       
   542 void CpCertManUISyncWrapper::SetApplicabilityL( CUnifiedCertStore*& aStore,
       
   543     const CCTCertInfo& aCertInfo,
       
   544     RArray<TUid>& aApplications )
       
   545     {
       
   546     
       
   547     aStore->SetApplicability( aCertInfo, aApplications, iStatus );
       
   548     iOperation = EOperationSetApplicability;
       
   549     iStore = aStore;
       
   550     SetActive();
       
   551     iWait.Start();
       
   552     User::LeaveIfError( iStatus.Int() );    
       
   553 	aStore->SetTrust( aCertInfo, ETrue, iStatus );
       
   554 	iOperation = EOperationSetToTrusted;
       
   555 	SetActive();
       
   556 	iWait.Start();
       
   557 	User::LeaveIfError( iStatus.Int() );
       
   558     iOperation = EOperationNone;
       
   559     }
       
   560 
       
   561 // -----------------------------------------------------------------------------
       
   562 // CpCertManUISyncWrapper::MoveKeyL
       
   563 // -----------------------------------------------------------------------------
       
   564 //
       
   565 void CpCertManUISyncWrapper::MoveKeyL(
       
   566     CUnifiedKeyStore*& aStore,
       
   567     const TCTKeyAttributeFilter& aFilter,
       
   568     const TUid aSourceTokenId,
       
   569     const TUid aTargetTokenId )
       
   570     {
       
   571     
       
   572     TInt sourceIndex(-1);
       
   573     TInt targetIndex(-1);
       
   574 
       
   575     // Find the index of key stores
       
   576     TInt count = aStore->KeyStoreManagerCount();
       
   577 
       
   578     for (TInt ii = 0; ii < count; ii++)
       
   579         {
       
   580         MCTKeyStoreManager& keystoremanager = aStore->KeyStoreManager( ii );
       
   581         MCTToken& token = keystoremanager.Token();
       
   582         TUid tokenuid = token.Handle().iTokenTypeUid;
       
   583 
       
   584         if ( tokenuid == aSourceTokenId )
       
   585             {
       
   586             sourceIndex = ii;
       
   587             }
       
   588 
       
   589         if ( tokenuid == aTargetTokenId )
       
   590             {
       
   591             targetIndex = ii;
       
   592             }
       
   593         }
       
   594 
       
   595     if (( sourceIndex == -1 ) || ( targetIndex == -1 ))
       
   596         {
       
   597         // Key store(s) doesn't exist
       
   598         User::Leave( KErrNotFound );
       
   599         }
       
   600 
       
   601     RMPointerArray<CCTKeyInfo> keyEntries;
       
   602 
       
   603     MCTKeyStoreManager& sourcekeystore =
       
   604                             aStore->KeyStoreManager( sourceIndex );
       
   605 
       
   606     MCTKeyStoreManager& targetkeystore =
       
   607                             aStore->KeyStoreManager( targetIndex );
       
   608 
       
   609 
       
   610     ListL( aStore, &keyEntries, aFilter, aSourceTokenId );
       
   611 
       
   612     // Go through all matching keys and move them to
       
   613     // target store
       
   614     for ( TInt ii = 0; ii < keyEntries.Count(); ii++ )
       
   615         {
       
   616         HBufC8* keyData = HBufC8::NewLC( KMaxKeyLength );
       
   617 
       
   618         // Retrieve key from source key store
       
   619         sourcekeystore.ExportKey( (*keyEntries[ii]).Handle(), keyData, iStatus );
       
   620         iOperation = EOperationExportKey;
       
   621         SetActive();
       
   622         iWait.Start();
       
   623         User::LeaveIfError( iStatus.Int() );
       
   624 
       
   625         TCTTokenObjectHandle sourceKeyHandle = (*keyEntries[ii]).Handle();
       
   626 
       
   627         // Import key to target key store
       
   628 
       
   629         // If key info access type indicates that key is local, then importing is
       
   630         // not possible. The following is the workarround. Almost identical
       
   631         // copy of keyinfo is created without CCTKeyInfo::ELocal access type flag.
       
   632         // UsePolicy is also updated
       
   633         TInt accessType = (*keyEntries[ii]).AccessType();
       
   634         if ( accessType & CCTKeyInfo::ELocal )
       
   635             {
       
   636             // CCTKeyInfo::ELocal is set in key info
       
   637             HBufC* label = (*keyEntries[ii]).Label().AllocLC();
       
   638 
       
   639             // The following XOR operation will clear local bit if it is on.
       
   640             accessType ^= CCTKeyInfo::ELocal;
       
   641 
       
   642             CCTKeyInfo* keyInfo = CCTKeyInfo::NewL( (*keyEntries[ii]).ID(),
       
   643                                 (*keyEntries[ii]).Usage(),
       
   644                                 (*keyEntries[ii]).Size(),
       
   645                                 NULL,
       
   646                                 label,
       
   647                                 (*keyEntries[ii]).Token(),
       
   648                                 (*keyEntries[ii]).HandleID(),
       
   649                                 KKeyStoreUsePolicy,
       
   650                                 (*keyEntries[ii]).ManagementPolicy(),
       
   651                                 (*keyEntries[ii]).Algorithm(),
       
   652                                 accessType,
       
   653                                 (*keyEntries[ii]).Native(),
       
   654                                 (*keyEntries[ii]).StartDate(),
       
   655                                 (*keyEntries[ii]).EndDate() );
       
   656 
       
   657             CleanupStack::Pop(label);
       
   658             targetkeystore.ImportKey( *keyData, keyInfo, iStatus );
       
   659             }
       
   660         else
       
   661             {
       
   662             targetkeystore.ImportKey( *keyData, keyEntries[ii], iStatus );
       
   663             }
       
   664 
       
   665         iOperation = EOperationImportKey;
       
   666         SetActive();
       
   667         iWait.Start();
       
   668         User::LeaveIfError( iStatus.Int() );
       
   669 
       
   670         // Delete key from source key store
       
   671         sourcekeystore.DeleteKey( sourceKeyHandle, iStatus );
       
   672         iOperation = EOperationDeleteKey;
       
   673         SetActive();
       
   674         iWait.Start();
       
   675         User::LeaveIfError( iStatus.Int() );
       
   676 
       
   677         CleanupStack::PopAndDestroy(keyData);  
       
   678         }
       
   679 
       
   680     }
       
   681 
       
   682 // -----------------------------------------------------------------------------
       
   683 // CpCertManUISyncWrapper::MoveCertL
       
   684 // -----------------------------------------------------------------------------
       
   685 //
       
   686 TInt CpCertManUISyncWrapper::MoveCertL(
       
   687     CUnifiedCertStore*& aStore,
       
   688     const CCTCertInfo& aCertInfo,
       
   689     const TUid aSourceTokenId,
       
   690     const TUid aTargetTokenId )
       
   691     {
       
   692     
       
   693     TInt sourceIndex(-1);
       
   694     TInt targetIndex(-1);
       
   695     TInt certCount (0);
       
   696 
       
   697     // Find the index of certificate stores
       
   698     TInt count = aStore->WritableCertStoreCount();
       
   699     for (TInt ii = 0; ii < count; ii++)
       
   700         {
       
   701         MCTWritableCertStore& writablestore = aStore->WritableCertStore( ii );
       
   702         MCTToken& token = writablestore.Token();
       
   703         TUid tokenuid = token.Handle().iTokenTypeUid;
       
   704 
       
   705         if ( tokenuid == aSourceTokenId )
       
   706             {
       
   707             sourceIndex = ii;
       
   708             }
       
   709 
       
   710         if ( tokenuid == aTargetTokenId )
       
   711             {
       
   712             targetIndex = ii;
       
   713             }
       
   714         }
       
   715 
       
   716     if (( sourceIndex == -1 ) || ( targetIndex == -1 ))
       
   717         {
       
   718         // Certificate store(s) doesn't exist
       
   719         User::Leave( KErrNotFound );
       
   720         }
       
   721 
       
   722 
       
   723     MCTWritableCertStore& sourcewritablestore =
       
   724                     aStore->WritableCertStore( sourceIndex );
       
   725 
       
   726     // All of the certificates that are associated with same
       
   727     // private key will be moved to target certificate store.
       
   728     CCertAttributeFilter* filter = CCertAttributeFilter::NewL();
       
   729     filter->SetOwnerType( EUserCertificate );
       
   730     filter->SetSubjectKeyId( aCertInfo.SubjectKeyId() );
       
   731     RMPointerArray<CCTCertInfo> certEntries;
       
   732 
       
   733     // List certificates from source certificate store
       
   734     ListL( aStore, &certEntries, *filter, aSourceTokenId );
       
   735 
       
   736     delete filter;
       
   737 
       
   738     for ( TInt ii = 0; ii < certEntries.Count(); ii++ )
       
   739         {
       
   740         // Retrieve certificate from source certificate store
       
   741         HBufC8* buf = HBufC8::NewLC( KMaxCertificateLength );
       
   742         iCertPtr.Set( buf->Des() );
       
   743         sourcewritablestore.Retrieve( *certEntries[ii], iCertPtr, iStatus );
       
   744         iOperation = EGetCertificate;
       
   745         SetActive();
       
   746         iWait.Start();
       
   747         User::LeaveIfError( iStatus.Int() );
       
   748 
       
   749         // Add certificate to target certificate store
       
   750         MCTWritableCertStore& targetwritablestore =
       
   751                         aStore->WritableCertStore( targetIndex );
       
   752 
       
   753         targetwritablestore.Add( (*certEntries[ii]).Label(), EX509Certificate,
       
   754                       EUserCertificate, &((*certEntries[ii]).SubjectKeyId()),
       
   755                       &((*certEntries[ii]).IssuerKeyId()), *buf, iStatus );
       
   756 
       
   757         iOperation = EAddCertificate;
       
   758         SetActive();
       
   759         iWait.Start();
       
   760         User::LeaveIfError( iStatus.Int() );
       
   761 
       
   762         // Delete certificate from source certificate store
       
   763         sourcewritablestore.Remove( *certEntries[ii], iStatus );
       
   764         iOperation = EOperationDelete;
       
   765         iStore = aStore;
       
   766         SetActive();
       
   767         iWait.Start();
       
   768         User::LeaveIfError( iStatus.Int() );
       
   769         iOperation = EOperationNone;
       
   770         certCount++;
       
   771         CleanupStack::PopAndDestroy(buf);  
       
   772         }
       
   773 
       
   774     return certCount;
       
   775     }
       
   776 
       
   777