wim/WimPlugin/src/WimKeyStore.cpp
changeset 0 164170e6151a
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2002 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:  Implementation of Wim key store interface
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include "WimKeyStore.h"
       
    22 #include "WimKeyDetails.h"
       
    23 #include "WimToken.h"
       
    24 #include "WimPin.h"
       
    25 #include "WimAuthenticationObject.h"
       
    26 #include "WimTrace.h"
       
    27 #include "WimTokenListener.h"
       
    28 #include "WimImplementationUID.hrh"
       
    29 #include "WimRSASigner.h"
       
    30 #include "WimConsts.h"
       
    31 #include <mctauthobject.h>
       
    32 #include <ct.h>
       
    33 #include <asymmetric.h>
       
    34 
       
    35 
       
    36 // ============================ MEMBER FUNCTIONS ===============================
       
    37 
       
    38 // -----------------------------------------------------------------------------
       
    39 // CWimKeyStore::CWimKeyStore()
       
    40 // Default constructor
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 CWimKeyStore::CWimKeyStore( CWimToken& aToken ) :
       
    44                             CActive( EPriorityNormal ),
       
    45                             iToken( aToken )
       
    46 
       
    47     {
       
    48     CActiveScheduler::Add( this );
       
    49     }
       
    50 
       
    51 // -----------------------------------------------------------------------------
       
    52 // CWimKeyStore::NewL()
       
    53 // Two-phased constructor
       
    54 // -----------------------------------------------------------------------------
       
    55 //
       
    56 CWimKeyStore* CWimKeyStore::NewL( CWimToken& aToken )
       
    57     {
       
    58     _WIMTRACE ( _L( "CWimKeyStore::NewL()" ) );
       
    59     CWimKeyStore* self = new( ELeave ) CWimKeyStore( aToken );
       
    60     CleanupStack::PushL( self );
       
    61     self->ConstructL();
       
    62     CleanupStack::Pop( self );
       
    63     return self;
       
    64     }
       
    65 
       
    66 // -----------------------------------------------------------------------------
       
    67 // CWimKeyStore::ConstructL()
       
    68 // Second phase constructor
       
    69 // -----------------------------------------------------------------------------
       
    70 //
       
    71 void CWimKeyStore::ConstructL()
       
    72     {
       
    73     _WIMTRACE ( _L( "CWimKeyStore::ConstructL()" ) );
       
    74     iCWimKeyDetails = CWimKeyDetails::NewL( Token() );
       
    75     iKeyNumbers = new( ELeave )CArrayFixFlat<TUint8>(1);
       
    76     iPhase = EIdle;
       
    77     iPhaseOriginal = EIdle;
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // CWimKeyStore::~CWimKeyStore()
       
    82 // Destructor
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 CWimKeyStore::~CWimKeyStore()
       
    86     {
       
    87     _WIMTRACE ( _L( "CWimKeyStore::~CWimKeyStore()" ) );
       
    88     Cancel();
       
    89     TInt count = iKeyInfos.Count();
       
    90     for ( TInt i = 0; i < count; i++ )
       
    91         {
       
    92         iKeyInfos[i]->Release();
       
    93         }
       
    94     iKeyInfos.Close();
       
    95 
       
    96     if ( iKeyNumbers )
       
    97         {
       
    98         iKeyNumbers->Reset();
       
    99         delete iKeyNumbers;
       
   100         }
       
   101 
       
   102     delete iSignature;
       
   103 
       
   104     if ( iCWimKeyDetails )
       
   105         {
       
   106         delete iCWimKeyDetails;
       
   107         iCWimKeyDetails = NULL;
       
   108         }
       
   109 
       
   110     delete iKeyId;
       
   111     }
       
   112 // -----------------------------------------------------------------------------
       
   113 // CWimKeyStore::DoRelease()
       
   114 // Deletes this interface when Release() for this instance is called.
       
   115 // -----------------------------------------------------------------------------
       
   116 //
       
   117 void CWimKeyStore::DoRelease()
       
   118     {
       
   119     _WIMTRACE ( _L( "CWimKeyStore::DoRelease()" ) );
       
   120     delete this;
       
   121     }
       
   122 
       
   123 // -----------------------------------------------------------------------------
       
   124 // CWimKeyStore::Token()
       
   125 // Returns a reference to current token of this key store interface.
       
   126 // -----------------------------------------------------------------------------
       
   127 //
       
   128 MCTToken& CWimKeyStore::Token()
       
   129     {
       
   130     _WIMTRACE ( _L( "CWimKeyStore::Token()" ) );
       
   131     return iToken;
       
   132     }
       
   133 
       
   134 // -----------------------------------------------------------------------------
       
   135 // CWimKeyStore::List()
       
   136 // Lists all the keys in the store that match the filter.
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 void CWimKeyStore::List( RMPointerArray<CCTKeyInfo>& aKeys,
       
   140                          const TCTKeyAttributeFilter& aFilter,
       
   141                          TRequestStatus& aStatus )
       
   142     {
       
   143     _WIMTRACE ( _L( "CWimKeyStore::List()" ) );
       
   144 
       
   145     if ( !EnteringAllowed( aStatus ) )
       
   146         {
       
   147         return;
       
   148         }
       
   149 
       
   150     iKeyList = &aKeys;
       
   151     iKeyFilter = aFilter;
       
   152     iPhase = EGetKeyList;
       
   153     iPhaseOriginal = EGetKeyList;
       
   154     TRequestStatus* status = &iStatus;
       
   155     User::RequestComplete( status, KErrNone );
       
   156     SetActive();
       
   157     }
       
   158 
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // CWimKeyStore::Open()
       
   162 // Open an RSA key for signing
       
   163 // -----------------------------------------------------------------------------
       
   164 //
       
   165 void CWimKeyStore::Open( const TCTTokenObjectHandle& aHandle,
       
   166                         MRSASigner*& aSigner,
       
   167                         TRequestStatus& aStatus )
       
   168     {
       
   169     _WIMTRACE ( _L( "CWimKeyStore::Open()" ) );
       
   170 
       
   171     if ( !EnteringAllowed( aStatus ) )
       
   172         {
       
   173         return;
       
   174         }
       
   175 
       
   176     //Check whether handle matches.
       
   177     if ( aHandle.iTokenHandle == Token().Handle() )
       
   178         {
       
   179         TRAPD( err, iSigner = CWimRSASigner::NewL( *this ) );
       
   180         if ( err ) //OOM
       
   181             {
       
   182             iPhase = EIdle;
       
   183             User::RequestComplete( iOriginalRequestStatus, err );
       
   184             }
       
   185         else
       
   186            {
       
   187             aSigner = iSigner;
       
   188             iOpenSigninKeyHandle = aHandle;
       
   189             iPhase = ECreateRSASignerObject;
       
   190             TRequestStatus* status = &iStatus;
       
   191             User::RequestComplete( status, KErrNone );
       
   192             SetActive();
       
   193            }
       
   194         }
       
   195 
       
   196     else
       
   197         {
       
   198         User::RequestComplete( iOriginalRequestStatus, KErrArgument );
       
   199         }
       
   200     }
       
   201 
       
   202 // -----------------------------------------------------------------------------
       
   203 // CWimKeyStore::CancelOpen()
       
   204 // Cancels an ongoing Open operation
       
   205 // -----------------------------------------------------------------------------
       
   206 //
       
   207 void CWimKeyStore::CancelOpen()
       
   208     {
       
   209     _WIMTRACE ( _L( "CWimKeyStore::CancelOpen()" ) );
       
   210     Cancel();
       
   211     }
       
   212 
       
   213 // -----------------------------------------------------------------------------
       
   214 // CWimKeyStore::ExportPublic()
       
   215 // Returns the public key in DER-encoded ASN-1.
       
   216 // -----------------------------------------------------------------------------
       
   217 //
       
   218 void CWimKeyStore::ExportPublic( const TCTTokenObjectHandle& aHandle,
       
   219                                  HBufC8*& aPublicKey,
       
   220                                  TRequestStatus& aStatus )
       
   221     {
       
   222     _WIMTRACE ( _L( "CWimKeyStore::ExportPublic()" ) );
       
   223 
       
   224     if ( !EnteringAllowed( aStatus ) )
       
   225         {
       
   226         return;
       
   227         }
       
   228 
       
   229     if ( aHandle.iTokenHandle == Token().Handle() )
       
   230         {
       
   231     
       
   232             TRAPD( err, iPublicKey = HBufC8::NewL( KPublicKeyLength ) );
       
   233 
       
   234             if ( err )
       
   235                 {
       
   236                 User::RequestComplete( iOriginalRequestStatus, err );
       
   237                 }
       
   238             else
       
   239                 {
       
   240                 iExportKeyIdIndex = aHandle.iObjectId;
       
   241                 aPublicKey = iPublicKey;
       
   242                 iPhase = EStartExportPublicKey;
       
   243                 TRequestStatus* status = &iStatus;
       
   244                 User::RequestComplete( status, KErrNone );
       
   245                 SetActive();
       
   246                 }
       
   247          
       
   248            
       
   249         }
       
   250     else
       
   251         {
       
   252         User::RequestComplete( iOriginalRequestStatus, KErrBadHandle );
       
   253         }
       
   254     }
       
   255 
       
   256 // -----------------------------------------------------------------------------
       
   257 // CWimKeyStore::CancelExportPublic()
       
   258 // Cancels an ongoing Export operation.
       
   259 // -----------------------------------------------------------------------------
       
   260 //
       
   261 void CWimKeyStore::CancelExportPublic()
       
   262     {
       
   263     _WIMTRACE ( _L( "CWimKeyStore::CancelExportPublic()" ) );
       
   264     Cancel();
       
   265     }
       
   266 
       
   267 // -----------------------------------------------------------------------------
       
   268 // CWimKeyStore::Open()
       
   269 //Open an DSA key for signing. Not Supported.
       
   270 // -----------------------------------------------------------------------------
       
   271 //
       
   272 void CWimKeyStore::Open( const TCTTokenObjectHandle& /*aHandle*/,
       
   273                          MDSASigner*& /*aSigner*/,
       
   274                          TRequestStatus& aStatus )
       
   275     {
       
   276     _WIMTRACE ( _L( "CWimKeyStore::Open()" ) );
       
   277     TRequestStatus* stat = &aStatus;
       
   278     User::RequestComplete(stat, KErrNotSupported );
       
   279     }
       
   280 
       
   281 // -----------------------------------------------------------------------------
       
   282 // CWimKeyStore::Open()
       
   283 // Open a RSA key for private decryption. Not Supported.
       
   284 // -----------------------------------------------------------------------------
       
   285 //
       
   286 void CWimKeyStore::Open( const TCTTokenObjectHandle& /*aHandle*/,
       
   287                          MCTDecryptor*& /*aDecryptor*/,
       
   288                          TRequestStatus& aStatus )
       
   289     {
       
   290     _WIMTRACE ( _L( "CWimKeyStore::Open()" ) );
       
   291     TRequestStatus* stat = &aStatus;
       
   292     User::RequestComplete( stat, KErrNotSupported );
       
   293     }
       
   294 
       
   295 // -----------------------------------------------------------------------------
       
   296 // CWimKeyStore::Open()
       
   297 // Open a DH key for key agreement. Not Supported.
       
   298 // -----------------------------------------------------------------------------
       
   299 //
       
   300 void CWimKeyStore::Open( const TCTTokenObjectHandle& /*aHandle*/,
       
   301                          MCTDH*& /*aDH*/,
       
   302                          TRequestStatus& aStatus )
       
   303     {
       
   304     _WIMTRACE ( _L( "CWimKeyStore::Open()" ) );
       
   305     TRequestStatus* stat = &aStatus;
       
   306     User::RequestComplete( stat, KErrNotSupported );
       
   307     }
       
   308 
       
   309 // -----------------------------------------------------------------------------
       
   310 // void CWimKeyStore::RunL()
       
   311 // Core operations are done here.
       
   312 // -----------------------------------------------------------------------------
       
   313 //
       
   314 void CWimKeyStore::RunL()
       
   315     {
       
   316     _WIMTRACE ( _L( "CWimKeyStore::RunL()" ) );
       
   317 
       
   318     if ( !iListDone &&
       
   319          iPhase != EGetKeyList &&
       
   320          iPhase != ECheckGetKeyList )
       
   321         {
       
   322         iPhaseOriginal = iPhase;
       
   323         iPhase = EGetKeyList;
       
   324         TRequestStatus* status = &iStatus;
       
   325         User::RequestComplete( status, KErrNone );
       
   326         SetActive();
       
   327         }
       
   328     else
       
   329         {
       
   330         switch ( iPhase )
       
   331             {
       
   332         case EGetKeyList:
       
   333             {
       
   334             TInt count = iKeyInfos.Count();
       
   335 
       
   336             for ( TInt i = 0; i < count; i++ )
       
   337                 {
       
   338                 iKeyInfos[i]->Release();
       
   339                 }
       
   340 
       
   341             iKeyInfos.Reset();
       
   342             delete iCWimKeyDetails;
       
   343             iCWimKeyDetails = NULL;
       
   344             iKeyNumbers->Reset();
       
   345             iCWimKeyDetails = CWimKeyDetails::NewL( Token() );
       
   346             iStatus = KRequestPending;
       
   347             iPhase = ECheckGetKeyList;
       
   348             iCWimKeyDetails->GetKeyList( iKeyInfos, *iKeyNumbers, iStatus );
       
   349             SetActive();
       
   350             break;
       
   351             }
       
   352         case ECheckGetKeyList:
       
   353             {
       
   354             if ( iStatus.Int() == KErrNone )
       
   355                 {
       
   356                 iListDone = ETrue;
       
   357                 iKeyIndex = 0;
       
   358                 iPhase = EFilterKeys;
       
   359                 TRequestStatus* status = &iStatus;
       
   360                 User::RequestComplete( status, KErrNone );
       
   361                 SetActive();
       
   362                 }
       
   363             else // No keys found or iWimKeyDetails->Restore call has
       
   364                  // been cancelled or failed
       
   365                 {
       
   366                 iPhase = EIdle;
       
   367                 iPhaseOriginal = EIdle;
       
   368                 User::RequestComplete( iOriginalRequestStatus, iStatus.Int() );
       
   369                 }
       
   370             break;
       
   371             }
       
   372         case EFilterKeys:
       
   373             {
       
   374             DoFilterKeysL();
       
   375             break;
       
   376             }
       
   377         case EGetKey:
       
   378             {
       
   379             TInt count = iKeyInfos.Count();
       
   380             TInt i;
       
   381 
       
   382             for ( i = 0; i < count; i++ )
       
   383                 {
       
   384                 if ( iKeyInfos[i]->Handle().iObjectId == iHandle )
       
   385                     {
       
   386                     // Make a copy of key info
       
   387                     *iInfo = CreateKeyInfoL( i );
       
   388                     i = count + 1;
       
   389                     }
       
   390                 }
       
   391             if ( i == count )
       
   392                 {
       
   393                 User::RequestComplete( iOriginalRequestStatus, KErrNotFound );
       
   394                 }
       
   395             else
       
   396                 {
       
   397                 User::RequestComplete( iOriginalRequestStatus, KErrNone );
       
   398                 }
       
   399             iPhase = EIdle;
       
   400             iPhaseOriginal = EIdle;
       
   401             break;  
       
   402             }
       
   403             
       
   404         case ECreateRSASignerObject:
       
   405             {
       
   406             TBool found = EFalse;
       
   407             TInt i;
       
   408 
       
   409             for ( i = 0; i<iKeyInfos.Count() && !found; i++ )
       
   410                 {
       
   411                 //Check if the handle matches.
       
   412                 if ( iKeyInfos[i]->Handle().iObjectId ==
       
   413                     iOpenSigninKeyHandle.iObjectId )
       
   414                     {
       
   415                     found = ETrue;
       
   416                     }
       
   417                 if ( found )
       
   418                     {
       
   419                     //Check whether found key can do RSA sign.
       
   420                     if ( iKeyInfos[i]->Algorithm() != CKeyInfoBase::ERSA )
       
   421                         {
       
   422                         //Set flag to false, the key does not do RSA sign.
       
   423                         found = EFalse;
       
   424                         }
       
   425                     else
       
   426                         {
       
   427                         //Set label for new signing key
       
   428                         iSigner->SetLabelL( iKeyInfos[i]->Label() );
       
   429                         //Set objectId
       
   430                         iSigner->SetObjectId( iKeyInfos[i]->Handle().iObjectId );
       
   431                         }
       
   432                     }
       
   433                 }
       
   434             if ( found )
       
   435                 {
       
   436                 User::RequestComplete( iOriginalRequestStatus, KErrNone );
       
   437                 }
       
   438             else
       
   439                 {
       
   440                 delete iSigner;
       
   441                 iSigner = NULL;
       
   442                 User::RequestComplete( iOriginalRequestStatus, KErrBadHandle );
       
   443                 }
       
   444             iPhase = EIdle;
       
   445             iPhaseOriginal = EIdle;
       
   446             break;
       
   447             }
       
   448         case EStartNonRepudiableRSASign:
       
   449             {
       
   450             TBool found = EFalse;
       
   451             TInt i = 0;
       
   452 
       
   453             for ( ; i < iKeyInfos.Count() && !found; i++ )
       
   454                 {
       
   455                  if ( iKeyInfos.operator[]( i )->Handle().iObjectId ==
       
   456                  iRSAKeyIdIndex )
       
   457                      {
       
   458                      found = ETrue;
       
   459                      TRAPD( err, iKeyId = iKeyInfos[i]->ID().AllocL() )
       
   460 
       
   461                      if ( err )
       
   462                          {
       
   463                          User::RequestComplete( iOriginalRequestStatus, err );
       
   464                          }
       
   465                      else
       
   466                          {
       
   467                          iCWimKeyDetails->Sign( *iDigest,
       
   468                                         *iKeyId,
       
   469                                         iSignature,
       
   470                                         iStatus );
       
   471                          iPhase = ESignCompleted;
       
   472                          SetActive();
       
   473                          }
       
   474                      }
       
   475                 }
       
   476             //Key was not found, no need for signature
       
   477            if ( !found )
       
   478               {
       
   479               iPhase = EIdle;
       
   480               iPhaseOriginal = EIdle;
       
   481               delete iSignature;
       
   482               iSignature = NULL;
       
   483               User::RequestComplete( iOriginalRequestStatus, KErrBadHandle );
       
   484               }
       
   485         	break;
       
   486             }
       
   487         case ESignCompleted:
       
   488             {
       
   489             //No need for KeyId anymore
       
   490             delete iKeyId;
       
   491             iKeyId = NULL;
       
   492 
       
   493             //Check whether signing failed
       
   494             if ( iStatus.Int() != KErrNone )
       
   495                 {
       
   496                 delete iSignature;
       
   497                 iSignature = NULL;
       
   498                 User::RequestComplete( iOriginalRequestStatus, iStatus.Int() );
       
   499                 }
       
   500             else //Sign was succesful.
       
   501                 {
       
   502                  //Create signature and complete operation
       
   503                 RInteger signature = RInteger::NewL( *iSignature );
       
   504                 CleanupStack::PushL( signature );
       
   505                 *iRSASignature = CRSASignature::NewL( signature );
       
   506                 CleanupStack::Pop();
       
   507                 delete iSignature;
       
   508                 iSignature = NULL;
       
   509                 User::RequestComplete( iOriginalRequestStatus, iStatus.Int() );
       
   510                 }
       
   511             iPhase = EIdle;
       
   512             iPhaseOriginal = EIdle;
       
   513             break;
       
   514             }
       
   515         case EStartExportPublicKey:
       
   516             {
       
   517             TInt i = 0;
       
   518             TBool found = EFalse;
       
   519             
       
   520             for( i = 0; i < iKeyInfos.Count() && !found; i++ )
       
   521                 {
       
   522             	//Check if the objectIds matches
       
   523             	if( iKeyInfos[i]->Handle().iObjectId == iExportKeyIdIndex )
       
   524             	    {
       
   525             		found = ETrue;
       
   526             	    }	
       
   527                 }
       
   528                
       
   529             if( !found )
       
   530                 {
       
   531             	iPhase = EIdle;
       
   532             	iPhaseOriginal = EIdle;
       
   533             	delete iPublicKey;
       
   534             	iPublicKey = NULL;
       
   535             	User::RequestComplete( iOriginalRequestStatus, KErrNotFound );
       
   536                 }
       
   537             else
       
   538                 {
       
   539             	iKeyId = iKeyInfos[iExportKeyIdIndex]->ID().AllocL();
       
   540                 iCWimKeyDetails->ExportPublicKeyL( *iKeyId,
       
   541                                                iPublicKey,
       
   542                                                iStatus );
       
   543 
       
   544                 iPhase = EExportPublicKeyDone;
       
   545                 SetActive();
       
   546                 }
       
   547             
       
   548             break;
       
   549             }
       
   550         case EExportPublicKeyDone:
       
   551             {
       
   552             delete iKeyId;
       
   553             iKeyId = NULL;
       
   554             iPhase = EIdle;
       
   555             iPhaseOriginal = EIdle;
       
   556             User::RequestComplete( iOriginalRequestStatus, iStatus.Int() );
       
   557             break;
       
   558             }
       
   559         default:
       
   560             {
       
   561             // Here we should not be
       
   562             iPhase = EIdle;
       
   563             iPhaseOriginal = EIdle;
       
   564             User::RequestComplete( iOriginalRequestStatus, KErrCorrupt );
       
   565             break;
       
   566             }
       
   567         }
       
   568         }
       
   569     }
       
   570 
       
   571 // -----------------------------------------------------------------------------
       
   572 // CWimKeyStore::NonRepudiableRSASign()
       
   573 // Perform signing operation
       
   574 // -----------------------------------------------------------------------------
       
   575 //
       
   576 void CWimKeyStore::NonRepudiableRSASign( const TCTTokenObjectHandle& aHandle,
       
   577                                          const TDesC8& aDigest,
       
   578                                          CRSASignature*& aSignature,
       
   579                                          TRequestStatus& aStatus )
       
   580     {
       
   581     _WIMTRACE ( _L( "CWimKeyStore::NonRepudiableRSASign()" ) );
       
   582 
       
   583     if ( !EnteringAllowed( aStatus ) )
       
   584         {
       
   585         return;
       
   586         }
       
   587 
       
   588     TRAPD( err, iSignature = HBufC8::NewL( KMaxSignatureLength ) );
       
   589 
       
   590     if ( err ) //OOM
       
   591         {
       
   592         User::RequestComplete( iOriginalRequestStatus, err );
       
   593         return;
       
   594         }
       
   595 
       
   596     iRSASignature = &aSignature;
       
   597     iRSAKeyIdIndex = aHandle.iObjectId;
       
   598     iDigest = &aDigest;
       
   599 
       
   600     iPhase = EStartNonRepudiableRSASign;
       
   601     TRequestStatus* status = &iStatus;
       
   602     User::RequestComplete( status, KErrNone );
       
   603     SetActive();
       
   604     }
       
   605 
       
   606 // -----------------------------------------------------------------------------
       
   607 // void CWimKeyStore::DoFilterKeysL()
       
   608 // Core operations are done here.
       
   609 // -----------------------------------------------------------------------------
       
   610 //
       
   611 void CWimKeyStore::DoFilterKeysL()
       
   612     {
       
   613     _WIMTRACE ( _L( "CWimKeyStore::DoFilterKeysL()" ) );
       
   614     if ( iPhaseOriginal == EGetKeyList )
       
   615         {
       
   616         if ( iKeyIndex < iKeyInfos.Count() )
       
   617             {
       
   618             // If usage is given in filter and handled key (in iKeyIndex) has
       
   619             // also same usage, take with
       
   620             //
       
   621             // OR, (if filter usage is 0 or EAllKeyUsages) AND handled key
       
   622             // usage is also 0 AND (key id length in filter is 0 OR handled
       
   623             // key id is equal to key id in filter), take with
       
   624             //
       
   625             if ( ( ( iKeyFilter.iUsage & iKeyInfos[iKeyIndex]->Usage() ) ||
       
   626                ( ( ( iKeyFilter.iUsage == 0 ) || ( iKeyFilter.iUsage ==
       
   627                EPKCS15UsageAll ) ) &&
       
   628                ( iKeyInfos[iKeyIndex]->Usage() == 0 ) ) ) &&
       
   629                ( iKeyFilter.iKeyId.Length() == 0 ||
       
   630                 !iKeyInfos[iKeyIndex]->ID().Compare( iKeyFilter.iKeyId ) ) )
       
   631                 {
       
   632                 // Make a copy of key info
       
   633                 CCTKeyInfo* copy = CreateKeyInfoL( iKeyIndex );
       
   634                 // Call Release() for copy, if leave occurs
       
   635                 CleanupReleasePushL( *copy );
       
   636                 User::LeaveIfError( iKeyList->Append ( copy ) );
       
   637                 CleanupStack::Pop( copy );
       
   638                 }
       
   639             iKeyIndex++;
       
   640             TRequestStatus* status = &iStatus;
       
   641             User::RequestComplete( status, KErrNone );
       
   642             SetActive();
       
   643             }
       
   644         else
       
   645             {
       
   646             // All keys are listed or there are not any
       
   647             iPhase = EIdle;
       
   648             iPhaseOriginal = EIdle;
       
   649             User::RequestComplete( iOriginalRequestStatus, KErrNone );
       
   650             }
       
   651         }
       
   652     else
       
   653         {
       
   654         iPhase = iPhaseOriginal;
       
   655         iPhaseOriginal = EIdle;
       
   656         TRequestStatus* status = &iStatus;
       
   657         User::RequestComplete( status, KErrNone );
       
   658         SetActive();
       
   659         }
       
   660     }
       
   661 
       
   662 // -----------------------------------------------------------------------------
       
   663 // void CWimKeyStore::CreateKeyInfoL()
       
   664 // Creates a new key info from internal array.
       
   665 // -----------------------------------------------------------------------------
       
   666 //
       
   667 CCTKeyInfo* CWimKeyStore::CreateKeyInfoL( TInt aIndex )
       
   668     {
       
   669     _WIMTRACE ( _L( "CWimKeyStore::CreateKeyInfoL()" ) );
       
   670     // Create protector
       
   671     MCTAuthenticationObject* protector = NULL;
       
   672     TUint thishandle = iKeyNumbers->At ( aIndex );
       
   673     protector = MakeAuthObjectL( thishandle );
       
   674     
       
   675     if ( protector )
       
   676         {
       
   677         CleanupStack::PushL( protector );
       
   678         }
       
   679 
       
   680     HBufC* wlabel = HBufC::NewLC( ( iKeyInfos )[aIndex]->Label().Length() );
       
   681     wlabel->Des().Copy( ( iKeyInfos )[aIndex]->Label() );
       
   682 
       
   683 #ifdef __SECURITY_PLATSEC_ARCH__
       
   684     CCTKeyInfo* copy = CCTKeyInfo::NewL( ( iKeyInfos ) [aIndex]->ID(),
       
   685                                          ( iKeyInfos ) [aIndex]->Usage(),
       
   686                                          ( iKeyInfos ) [aIndex]->Size(),
       
   687                                          protector,
       
   688                                          wlabel,
       
   689                                          ( iKeyInfos ) [aIndex]->Token(),
       
   690                                          ( iKeyInfos ) [aIndex]->Handle().iObjectId,
       
   691                                          ( iKeyInfos ) [aIndex]->UsePolicy(),
       
   692                                          ( iKeyInfos ) [aIndex]->ManagementPolicy(),
       
   693                                          ( iKeyInfos ) [aIndex]->Algorithm(),
       
   694                                          ( iKeyInfos ) [aIndex]->AccessType(),
       
   695                                          ( iKeyInfos ) [aIndex]->Native(),
       
   696                                          ( iKeyInfos ) [aIndex]->StartDate(),
       
   697                                          ( iKeyInfos ) [aIndex]->EndDate() );
       
   698 #else
       
   699    CCTKeyInfo* copy = CCTKeyInfo::NewL( ( iKeyInfos ) [aIndex]->ID(),
       
   700                                          ( iKeyInfos ) [aIndex]->Usage(),
       
   701                                          ( iKeyInfos ) [aIndex]->Size(),
       
   702                                          protector,
       
   703                                          wlabel,
       
   704                                          ( iKeyInfos ) [aIndex]->Token(),
       
   705                                          ( iKeyInfos ) [aIndex]->Handle().iObjectId,
       
   706                                          ( iKeyInfos ) [aIndex]->Owner(),
       
   707                                          ( iKeyInfos ) [aIndex]->Users(),
       
   708                                          ( iKeyInfos ) [aIndex]->Algorithm(),
       
   709                                          ( iKeyInfos ) [aIndex]->AccessType(),
       
   710                                          ( iKeyInfos ) [aIndex]->Native(),
       
   711                                          ( iKeyInfos ) [aIndex]->StartDate(),
       
   712                                          ( iKeyInfos ) [aIndex]->EndDate() );
       
   713 #endif
       
   714 
       
   715     CleanupStack::Pop( wlabel );
       
   716     if ( protector )
       
   717         {
       
   718         CleanupStack::Pop( protector );
       
   719         }
       
   720     return copy;
       
   721     }
       
   722 
       
   723 // -----------------------------------------------------------------------------
       
   724 // void CWimKeyStore::MakeAuthObjectL()
       
   725 // Lists authentication objects and matches each on given handle.
       
   726 // When match is found, one instance is created and returned.
       
   727 // -----------------------------------------------------------------------------
       
   728 //
       
   729 MCTAuthenticationObject* CWimKeyStore::MakeAuthObjectL( TInt aHandle )
       
   730     {
       
   731     
       
   732     if( !iPinNRs )
       
   733 	    {
       
   734 	    TInt ret = iToken.WimSecModule()->PinNrEntriesL( iPinNRs );
       
   735 	   
       
   736 	    if ( ret != KErrNone )
       
   737 	        {
       
   738 	        User::Leave( ret );
       
   739 	        }	
       
   740 	    }
       
   741     
       
   742         
       
   743     if ( iPinNRs ) // If PINs were found
       
   744         {
       
   745         TUid pintype = {WIM_PIN_G_UID}; // The first one is always PIN-G
       
   746         TInt count = iPinNRs->Count();
       
   747         TUint32 status = 0;
       
   748 
       
   749         for ( TInt i = 0; i < count; i++ )
       
   750             {
       
   751             if ( aHandle == (*iPinNRs)[i]->PinNumber() )
       
   752                 {
       
   753                 CWimAuthenticationObject* me =
       
   754                         CWimAuthenticationObject::NewLC(
       
   755                                             iToken,
       
   756                                          *( *iPinNRs )[i],
       
   757                                           ( *iPinNRs )[i]->Label(),
       
   758                                              pintype,
       
   759                                           ( *iPinNRs )[i]->PinNumber(),
       
   760                                              status );
       
   761                  CleanupStack::Pop( me );
       
   762                  return me;
       
   763                 }
       
   764             pintype.iUid = WIM_PIN_NR_UID; // The others are always PIN-NRs
       
   765             }
       
   766         }
       
   767     return NULL;    
       
   768     }
       
   769 // -----------------------------------------------------------------------------
       
   770 // void CWimKeyStore::RunError()
       
   771 // Unexpected error in RunL (e.g. Leave) leads us here.
       
   772 // -----------------------------------------------------------------------------
       
   773 //
       
   774 TInt CWimKeyStore::RunError( TInt aError )
       
   775     {
       
   776     _WIMTRACE ( _L( "CWimKeyStore::RunError()" ) );
       
   777 
       
   778     iPhase = EIdle;
       
   779     iPhaseOriginal = EIdle;
       
   780     User::RequestComplete( iOriginalRequestStatus, aError );
       
   781     return KErrNone;
       
   782     }
       
   783 
       
   784 // -----------------------------------------------------------------------------
       
   785 // void CWimKeyStore::DoCancel()
       
   786 // Cancels outgoing async. calls.
       
   787 // -----------------------------------------------------------------------------
       
   788 //
       
   789 void CWimKeyStore::DoCancel()
       
   790     {
       
   791     _WIMTRACE ( _L( "CWimKeyStore::DoCancel()" ) );
       
   792 
       
   793     if ( iCWimKeyDetails && iPhase == ECheckGetKeyList )
       
   794         {
       
   795         if ( iCWimKeyDetails->IsActive() )
       
   796             {
       
   797             iCWimKeyDetails->CancelList();
       
   798             }
       
   799         }
       
   800     else if ( iCWimKeyDetails && iPhase == ESignCompleted )
       
   801         {
       
   802         if ( iCWimKeyDetails->IsActive() )
       
   803             {
       
   804             iCWimKeyDetails->CancelSign();
       
   805             delete iSignature;
       
   806             iSignature = NULL;
       
   807             }
       
   808         }
       
   809     else if ( iCWimKeyDetails && iPhase == EExportPublicKeyDone )
       
   810         {
       
   811         if ( iCWimKeyDetails->IsActive() )
       
   812             {
       
   813             iCWimKeyDetails->CancelExport();
       
   814             }
       
   815         }
       
   816     iPhase = EIdle;
       
   817     iPhaseOriginal = EIdle;
       
   818     delete iKeyId;
       
   819     iKeyId = NULL;
       
   820     User::RequestComplete( iOriginalRequestStatus, KErrCancel );
       
   821     }
       
   822 
       
   823 // -----------------------------------------------------------------------------
       
   824 // CWimKeyStore::CancelList()
       
   825 // Cancels an ongoing List operation. Sets an internal flag to true.
       
   826 // -----------------------------------------------------------------------------
       
   827 //
       
   828 void CWimKeyStore::CancelList()
       
   829     {
       
   830     _WIMTRACE ( _L( "CWimKeyStore::CancelList()" ) );
       
   831 
       
   832     if ( TokenRemoved() )
       
   833         {
       
   834         return;
       
   835         }
       
   836 
       
   837     Cancel();
       
   838     }
       
   839 
       
   840 // -----------------------------------------------------------------------------
       
   841 // CWimKeyStore::GetKeyInfo()
       
   842 // Retrieves a key according to given handle.
       
   843 // -----------------------------------------------------------------------------
       
   844 //
       
   845 void CWimKeyStore::GetKeyInfo( TCTTokenObjectHandle aHandle,
       
   846                                CCTKeyInfo*& aInfo,
       
   847                                TRequestStatus& aStatus )
       
   848     {
       
   849     _WIMTRACE ( _L( "CWimKeyStore::GetKeyInfo()" ) );
       
   850 
       
   851     if ( !EnteringAllowed( aStatus ) )
       
   852         {
       
   853         return;
       
   854         }
       
   855 
       
   856     if ( aHandle.iTokenHandle == Token().Handle() )
       
   857         {
       
   858         iInfo = &aInfo;
       
   859         iHandle = aHandle.iObjectId;
       
   860         iPhase = EGetKey;
       
   861         TRequestStatus* status = &iStatus;
       
   862         User::RequestComplete( status, KErrNone );
       
   863         SetActive();
       
   864         }
       
   865     else
       
   866         {
       
   867         User::RequestComplete( iOriginalRequestStatus, KErrArgument );
       
   868         }
       
   869     }
       
   870 
       
   871 // -----------------------------------------------------------------------------
       
   872 // CWimKeyStore::CancelGetKeyInfo()
       
   873 // Cancels an ongoing GetKeyInfo operation.
       
   874 // -----------------------------------------------------------------------------
       
   875 //
       
   876 void CWimKeyStore::CancelGetKeyInfo()
       
   877     {
       
   878     _WIMTRACE ( _L( "CWimKeyStore::CancelGetKeyInfo()" ) );
       
   879 
       
   880     if ( TokenRemoved() )
       
   881         {
       
   882         return;
       
   883         }
       
   884 
       
   885     Cancel();
       
   886     }
       
   887 
       
   888 // -----------------------------------------------------------------------------
       
   889 // CWimKeyStore::EnteringAllowed()
       
   890 //
       
   891 // -----------------------------------------------------------------------------
       
   892 //
       
   893 TBool CWimKeyStore::EnteringAllowed( TRequestStatus& aStatus )
       
   894     {
       
   895     _WIMTRACE ( _L( "CWimKeyStore::EnteringAllowed()" ) );
       
   896     if ( TokenRemoved() )
       
   897         {
       
   898         TRequestStatus* status = &aStatus;
       
   899         User::RequestComplete( status, KErrHardwareNotAvailable );
       
   900         return EFalse;
       
   901         }
       
   902 
       
   903     // If this active object is in running, don't accept entering
       
   904     if ( IsActive() )
       
   905         {
       
   906         // If the caller status is the same as the status, that activated
       
   907         // this object, just return
       
   908         if ( &aStatus == iOriginalRequestStatus )
       
   909             {
       
   910             return EFalse;
       
   911             }
       
   912         else
       
   913             {
       
   914             // Otherwise complete it with error
       
   915             TRequestStatus* status = &aStatus;
       
   916             User::RequestComplete( status, KErrInUse );
       
   917             return EFalse;
       
   918             }
       
   919         }
       
   920     else
       
   921         {
       
   922         iOriginalRequestStatus = &aStatus;
       
   923         aStatus = KRequestPending;
       
   924         return ETrue;
       
   925         }
       
   926     }
       
   927 
       
   928 // -----------------------------------------------------------------------------
       
   929 // CWimKeyStore::TokenRemoved()
       
   930 // Returns true or false indicating if token is removed
       
   931 // -----------------------------------------------------------------------------
       
   932 //
       
   933 TBool CWimKeyStore::TokenRemoved()
       
   934     {
       
   935     _WIMTRACE ( _L( "CWimKeyStore::TokenRemoved()" ) );
       
   936     // If token listener is not alive, then token is removed
       
   937     if ( iToken.TokenListener()->TokenStatus() != KRequestPending )
       
   938         {
       
   939         return ETrue;
       
   940         }
       
   941     else
       
   942         {
       
   943         return EFalse;
       
   944         }
       
   945     }
       
   946