wim/WimPlugin/src/WimToken.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:  A token represents one instance of a particular kind of 
       
    15 *               cryptographic module
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 
       
    22 #include "WimToken.h"
       
    23 #include "WimCertStore.h"
       
    24 #include "WimAuthenticationObjectList.h"
       
    25 #include "WimAuthenticationObject.h"
       
    26 #include "WimKeyStore.h"
       
    27 #include "WimSecModuleMgr.h"
       
    28 #include "WimImplementationUID.hrh"
       
    29 #include "WimTokenListener.h"
       
    30 #include "WimTrace.h"
       
    31 #include <mctkeystoreuids.h>            // KInterfaceKeyStore
       
    32 
       
    33 
       
    34 // ============================ MEMBER FUNCTIONS ===============================
       
    35 
       
    36 // -----------------------------------------------------------------------------
       
    37 // CWimToken::CWimToken()
       
    38 // Default constructor.
       
    39 // -----------------------------------------------------------------------------
       
    40 //
       
    41 CWimToken::CWimToken( CWimSecModule* aWimSecModule, MCTTokenType& aTokenType )
       
    42           :iWimSecModule( aWimSecModule ),
       
    43            iTokenType( aTokenType )
       
    44     {
       
    45     }
       
    46 
       
    47 // -----------------------------------------------------------------------------
       
    48 // CWimToken::ConstructL()
       
    49 // Second phase constructor
       
    50 // -----------------------------------------------------------------------------
       
    51 //
       
    52 void CWimToken::ConstructL()
       
    53     {
       
    54     _WIMTRACE ( _L( "CWimToken::ConstructL()" ) );
       
    55     iLabel = WimSecModule()->Label().AllocL();
       
    56     iWimTokenListener = CWimTokenListener::NewL( iWimSecModule );
       
    57     iWimTokenListener->StartListening(); //Start listening
       
    58     iCount = 0;
       
    59     }
       
    60 
       
    61 // -----------------------------------------------------------------------------
       
    62 // CWimToken::NewL()
       
    63 // Two-phased constructor.
       
    64 // -----------------------------------------------------------------------------
       
    65 //
       
    66 CWimToken* CWimToken::NewL( CWimSecModule* aWimSecModule, 
       
    67                             MCTTokenType& aTokenType )
       
    68     {
       
    69     _WIMTRACE ( _L( "CWimToken::NewL()" ) );
       
    70     // First parameter should not be NULL
       
    71     __ASSERT_ALWAYS( aWimSecModule, User::Leave( KErrCorrupt ) );
       
    72     CWimToken* self = new( ELeave ) CWimToken( aWimSecModule, aTokenType );
       
    73     CleanupStack::PushL( self );
       
    74     self->ConstructL();
       
    75     CleanupStack::Pop( self );
       
    76     return self;
       
    77     }
       
    78 
       
    79 // -----------------------------------------------------------------------------
       
    80 // CWimToken::~CWimToken()
       
    81 // Destructor
       
    82 // -----------------------------------------------------------------------------
       
    83 //
       
    84 CWimToken::~CWimToken()
       
    85     {
       
    86     _WIMTRACE ( _L( "CWimToken::~CWimToken()" ) );        
       
    87     delete iLabel;
       
    88     iWimSecModule = NULL;
       
    89     delete iWimTokenListener;
       
    90     }
       
    91 
       
    92 // -----------------------------------------------------------------------------
       
    93 // CWimToken::Label()
       
    94 // Get the label from current token
       
    95 // -----------------------------------------------------------------------------
       
    96 //
       
    97 const TDesC& CWimToken::Label()
       
    98     {
       
    99     _WIMTRACE ( _L( "CWimToken::Label()" ) );
       
   100     return reinterpret_cast<TDesC&>( *iLabel );
       
   101     }
       
   102 
       
   103 // -----------------------------------------------------------------------------
       
   104 // CWimToken::Information()
       
   105 // Returns the specified information string about the token
       
   106 // -----------------------------------------------------------------------------
       
   107 //
       
   108 const TDesC& CWimToken::Information( TTokenInformation aRequiredInformation )
       
   109     {
       
   110     _WIMTRACE ( _L( "CWimToken::Information()" ) );
       
   111     if ( TokenRemoved() )
       
   112         {
       
   113         return KNullDesC;
       
   114         }
       
   115 
       
   116     if ( iWimSecModule )
       
   117         {
       
   118         switch( aRequiredInformation ) 
       
   119            {
       
   120             case EVersion: // Returns the version from current token
       
   121                 {   
       
   122                 return WimSecModule()->Version();
       
   123                 }
       
   124             case ESerialNo: // Returns the Serialnumber from current token
       
   125                 {
       
   126                 return WimSecModule()->SerialNumber();
       
   127                 }
       
   128             case EManufacturer: // Returns the Manufacturer from current token
       
   129                 {
       
   130                 return WimSecModule()->Manufacturer();
       
   131                 }
       
   132             default:    // Returns empty string 
       
   133                 break;
       
   134              }
       
   135         }
       
   136     return KNullDesC;
       
   137     }
       
   138 
       
   139 // -----------------------------------------------------------------------------
       
   140 // MCTTokenType& CWimToken::TokenType()
       
   141 // Returns the associated token type.
       
   142 // -----------------------------------------------------------------------------
       
   143 //
       
   144 MCTTokenType& CWimToken::TokenType()
       
   145     {
       
   146     _WIMTRACE ( _L( "CWimToken::TokenType()" ) );
       
   147     return iTokenType;
       
   148     }
       
   149 
       
   150 // -----------------------------------------------------------------------------
       
   151 // TCTTokenHandle CWimToken::Handle()
       
   152 // Returns the token's handle. TCTTokenHandle defines a handle to a subclass 
       
   153 // of the MCTToken class
       
   154 // WimSecModule()->TokenNumber retrieves the token actually exists. 
       
   155 // Values 0..7 are for hardware tokens. Value 255 is for SoftId-token.
       
   156 // -----------------------------------------------------------------------------
       
   157 //
       
   158 TCTTokenHandle CWimToken::Handle()
       
   159     {
       
   160     _WIMTRACE ( _L( "CWimToken::Handle()" ) );
       
   161     const TInt tokenNumber = WimSecModule()->TokenNumber();
       
   162     return TCTTokenHandle( TokenType().Type(), tokenNumber );
       
   163     }
       
   164 
       
   165 // -----------------------------------------------------------------------------
       
   166 // CWimToken::DoGetInterface()
       
   167 // Returns a valid interface and KErrNone, or interface = NULL and 
       
   168 // KErrNotSupported if it isn't supported by this token
       
   169 // -----------------------------------------------------------------------------
       
   170 // 
       
   171 void CWimToken::DoGetInterface( TUid aRequiredInterface,
       
   172                                 MCTTokenInterface*& aReturnedInterface,
       
   173                                 TRequestStatus& aStatus )
       
   174     {
       
   175     _WIMTRACE ( _L( "CWimToken::DoGetInterface()" ) );
       
   176 
       
   177     if ( TokenRemoved() )
       
   178         {
       
   179         Release();
       
   180         TRequestStatus* status = &aStatus;
       
   181         User::RequestComplete( status, KErrHardwareNotAvailable );             
       
   182         return;
       
   183         }
       
   184     
       
   185 	    TInt error = KErrNone;
       
   186 
       
   187 	    if ( TokenType().Type().iUid == WIM_IMPLEMENTATION_UID )
       
   188 	        {
       
   189 	        TRAP( error, MakeInterfaceL( aRequiredInterface, aReturnedInterface ) );
       
   190 	        }
       
   191 	    else
       
   192 	        {
       
   193 	        error = KErrArgument;
       
   194 	        }
       
   195 
       
   196 	    TRequestStatus* status = &aStatus;
       
   197 	    User::RequestComplete( status, error );
       
   198 
       
   199 	    // If something went wrong, this interface must be released, 
       
   200 	    // because framework has just before incremented references
       
   201 	    if ( error != KErrNone )
       
   202 	        {
       
   203 	        Release();
       
   204 	        }	
       
   205     }
       
   206 
       
   207 // -----------------------------------------------------------------------------
       
   208 // CWimToken::MakeInterfaceL()
       
   209 // At first checks if interface is supported. Note: support is depending on
       
   210 // Uids that are indicated in resource file.
       
   211 // Calls actual interface maker. All leavable functions are gathered into this.
       
   212 // -----------------------------------------------------------------------------
       
   213 //
       
   214 void CWimToken::MakeInterfaceL( TUid aRequiredInterface,
       
   215                                 MCTTokenInterface*& aReturnedInterface )
       
   216     {
       
   217     _WIMTRACE ( _L( "CWimToken::MakeInterfaceL()" ) );
       
   218 
       
   219     RCPointerArray <CCTTokenTypeInfo> tokenTypes;
       
   220 
       
   221     TCurrentTokenType filter;
       
   222 
       
   223     CCTTokenTypeInfo::ListL( tokenTypes, filter );
       
   224 
       
   225     if ( tokenTypes.Count() != 1 ) // There shoud be only one Wim implementation
       
   226         {
       
   227         tokenTypes.Close();
       
   228         User::Leave( KErrCorrupt );
       
   229         }
       
   230 
       
   231     // Check that given interface uid is found from resource file
       
   232     TInt count = tokenTypes[0]->Interfaces().Count();
       
   233     TInt i = 0;
       
   234 
       
   235     for ( ; i < count; i++ )
       
   236         {
       
   237         if ( tokenTypes[0]->Interfaces()[i].iUid == aRequiredInterface.iUid )
       
   238             {
       
   239             i = count + 1;
       
   240             }
       
   241         }
       
   242 
       
   243     tokenTypes.Close();
       
   244 
       
   245     if ( i == count )
       
   246         {
       
   247         User::Leave( KErrNotSupported );
       
   248         }
       
   249 
       
   250     switch ( aRequiredInterface.iUid )
       
   251         {
       
   252         case KInterfaceCertStore:   // Read Only Cert Store Interface.
       
   253             {
       
   254             iCertStoreIf = CWimCertStore::NewL( *this );
       
   255             aReturnedInterface = iCertStoreIf;
       
   256             break;
       
   257             }
       
   258         case KInterfaceWritableCertStore:
       
   259             {                       // Writable Cert Store Interface
       
   260             iCertStoreWritableIf = CWimCertStore::NewL( *this );
       
   261             aReturnedInterface = iCertStoreWritableIf;
       
   262             break;
       
   263             }
       
   264         case KInterfaceKeyStore:    // KeyStore Interface
       
   265             {
       
   266             iKeyStoreIf = CWimKeyStore::NewL( *this );
       
   267             aReturnedInterface = iKeyStoreIf;
       
   268             break;
       
   269             }
       
   270         case KCTInterfaceAuthenticationObject:
       
   271             {                       // Authentication Object Interface
       
   272             iAuthObjectListIf = CWimAuthenticationObjectList::NewL( *this );
       
   273             aReturnedInterface = iAuthObjectListIf;
       
   274             break;
       
   275             }
       
   276         default: // Should be checked in the caller function
       
   277             { 
       
   278             User::Leave( KErrCorrupt );             
       
   279             break;
       
   280             }
       
   281         }
       
   282     }
       
   283 // -----------------------------------------------------------------------------
       
   284 // CWimToken::DoCancelGetInterface()
       
   285 // Nothing to do
       
   286 // -----------------------------------------------------------------------------
       
   287 //
       
   288 TBool CWimToken::DoCancelGetInterface()
       
   289     {
       
   290     _WIMTRACE ( _L( "CWimToken::DoCancelGetInterface()" ) );
       
   291     return EFalse;
       
   292     }
       
   293 
       
   294 // -----------------------------------------------------------------------------
       
   295 // CWimToken::ReferenceCount()
       
   296 // Returns a reference to reference counter.
       
   297 // -----------------------------------------------------------------------------
       
   298 //
       
   299 TInt& CWimToken::ReferenceCount()
       
   300     {
       
   301     return iCount;
       
   302     }
       
   303 
       
   304 // -----------------------------------------------------------------------------
       
   305 // CWimToken::NotifyOnRemoval( TRequestStatus& aStatus )
       
   306 // Notifies the client when the token has been removed.
       
   307 // -----------------------------------------------------------------------------
       
   308 //
       
   309 void CWimToken::NotifyOnRemoval( TRequestStatus& aStatus )
       
   310     {
       
   311     _WIMTRACE ( _L( "CWimToken::NotifyOnRemoval()" ) );
       
   312     iWimTokenListener->SetClientStatus( aStatus );
       
   313     }
       
   314 
       
   315 // -----------------------------------------------------------------------------
       
   316 // CWimToken::CancelNotify()
       
   317 // Cancels the NotifyOnRemoval request.
       
   318 // -----------------------------------------------------------------------------
       
   319 //
       
   320 void CWimToken::CancelNotify()
       
   321     {
       
   322     _WIMTRACE ( _L( "CWimToken::CancelNotify()" ) );
       
   323     if ( TokenRemoved() )
       
   324         {
       
   325         return;
       
   326         }
       
   327     iWimTokenListener->ClearClientStatus();
       
   328     }
       
   329 
       
   330 // -----------------------------------------------------------------------------
       
   331 // CWimToken::WimSecModule()
       
   332 // Returns a pointer to current security module
       
   333 // -----------------------------------------------------------------------------
       
   334 //
       
   335 CWimSecModule* CWimToken::WimSecModule()
       
   336     {
       
   337     return iWimSecModule;
       
   338     }
       
   339 
       
   340 // -----------------------------------------------------------------------------
       
   341 // CWimToken::TokenListener()
       
   342 // Returns a pointer to token listener
       
   343 // -----------------------------------------------------------------------------
       
   344 //
       
   345 CWimTokenListener* CWimToken::TokenListener()
       
   346     {
       
   347     return iWimTokenListener;
       
   348     }
       
   349 
       
   350 // -----------------------------------------------------------------------------
       
   351 // CWimToken::TokenRemoved()
       
   352 //
       
   353 // -----------------------------------------------------------------------------
       
   354 //
       
   355 TBool CWimToken::TokenRemoved()
       
   356     {
       
   357     if ( TokenListener()->TokenStatus() == KRequestPending )
       
   358         {
       
   359         return EFalse;
       
   360         }
       
   361     else
       
   362         {
       
   363         return ETrue;
       
   364         }
       
   365     }
       
   366 // -----------------------------------------------------------------------------
       
   367 // TCurrentTokenType::Accept( const CCTTokenTypeInfo& ) const
       
   368 // Returns true if current token type is same as given token type
       
   369 // -----------------------------------------------------------------------------
       
   370 //
       
   371 TBool TCurrentTokenType::Accept( const CCTTokenTypeInfo& aInfo ) const
       
   372     {
       
   373     if ( aInfo.Type().iUid == WIM_IMPLEMENTATION_UID )
       
   374         {
       
   375         return ETrue;
       
   376         }
       
   377     else
       
   378         {
       
   379         return EFalse;
       
   380         }   
       
   381     }               
       
   382