wim/WimServer/src/WimServer.cpp
changeset 0 164170e6151a
child 5 3b17fc5c9564
equal deleted inserted replaced
-1:000000000000 0:164170e6151a
       
     1 /*
       
     2 * Copyright (c) 2003-2009 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:  Server startup and initialization
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include    "WimServer.h"
       
    22 #include    "WimSession.h"
       
    23 #include    "WimSig.h"
       
    24 #include    "WimSessionRegistry.h"
       
    25 #include    "WimTrustSettingsStore.h"
       
    26 #include    "WimDefs.h"
       
    27 #include    "WimTrace.h"
       
    28 #include    "WimUtilityFuncs.h"
       
    29 #include    "WimCallbackImpl.h"
       
    30 #include    <s32file.h>         // UserSvr
       
    31 
       
    32 #include    "WimCertInfo.h"
       
    33 #include    "Wimi.h"            // WIMI definitions
       
    34 #include    "WimSatRefreshObserver.h"
       
    35 #include    "WimBTSapObserver.h"
       
    36 
       
    37 #include    <e32property.h>     // RProperty
       
    38 #include    <PSVariables.h>     // Property values
       
    39 
       
    40 #ifdef WIMSERVER_SHUTDOWN
       
    41 const TInt KServerShutdownDelay = 0x200000; 
       
    42 #endif
       
    43 // Initialize static variables. These variables are static because static
       
    44 // callback functions in CWimCallBack.
       
    45 CWimServer* CWimServer::iWimServer = NULL;
       
    46 TInt CWimServer::iWimStatus = KErrNone;
       
    47 TBool CWimServer::iWimInitialized = EFalse;
       
    48 
       
    49 
       
    50 // ============================= LOCAL FUNCTIONS ===============================
       
    51 
       
    52 // -----------------------------------------------------------------------------
       
    53 // StartServer
       
    54 // Server startup method. Creates active scheduler for the server.
       
    55 // Returns: TInt: error code
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 static void StartServerL()
       
    59     {
       
    60     _WIMTRACE(_L("WIM | WIMServer | StartServer | Enter"));
       
    61     User::LeaveIfError( User::RenameThread( KWIMServerName ) );
       
    62     _WIMTRACE(_L("WIM | WIMServer | StartServer | Create AS"));
       
    63  
       
    64     CActiveScheduler* s = new(ELeave) CActiveScheduler;
       
    65     CleanupStack::PushL( s );    
       
    66 
       
    67     CActiveScheduler::Install( s );
       
    68     
       
    69     _WIMTRACE(_L("WIM | WIMServer | StartServer | Create server"));
       
    70         
       
    71     CWimServer::NewL();
       
    72       
       
    73     _WIMTRACE(_L("WIM | WIMServer | StartServer | Notify client"));
       
    74     // Notify Client that server has starter
       
    75     RProcess::Rendezvous( KErrNone );       
       
    76 
       
    77     _WIMTRACE(_L("WIM | WIMServer | StartServer | Start ActiveScheduler"));
       
    78     CActiveScheduler::Start();
       
    79        
       
    80     CleanupStack::PopAndDestroy( s );
       
    81     }
       
    82 
       
    83 
       
    84 // -----------------------------------------------------------------------------
       
    85 // E32Main
       
    86 // Server entry point
       
    87 // Returns: TInt: error code
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 GLDEF_C TInt E32Main()
       
    91     {
       
    92     __UHEAP_MARK;
       
    93     _WIMTRACE(_L("WIM | WIMServer | E32Main | Begin"));
       
    94   
       
    95     CTrapCleanup* cleanup=CTrapCleanup::New();
       
    96     TInt r=KErrNoMemory;
       
    97     
       
    98     if ( cleanup )
       
    99         {
       
   100         TRAP( r, StartServerL() );
       
   101         delete cleanup;
       
   102         }
       
   103     _WIMTRACE(_L("WIM | WIMServer | E32Main Memory leak checking line"));    
       
   104     __UHEAP_MARKEND;    
       
   105     _WIMTRACE(_L("WIM | WIMServer | E32Main Memory checking passed"));
       
   106     return r;
       
   107     }
       
   108 
       
   109 
       
   110 // ============================ MEMBER FUNCTIONS ===============================
       
   111 
       
   112 // -----------------------------------------------------------------------------
       
   113 // CWimServer::CWimServer
       
   114 // C++ default constructor can NOT contain any code, that
       
   115 // might leave.
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 CWimServer::CWimServer( TInt aPriority )
       
   119     : CPolicyServer( aPriority, wimPolicy, ESharableSessions )
       
   120     {
       
   121     _WIMTRACE(_L("WIM | WIMServer | CWimServer::CWimServer | Begin"));
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CWimServer::ConstructL
       
   126 // Symbian 2nd phase constructor can leave.
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 void CWimServer::ConstructL()
       
   130     {
       
   131     _WIMTRACE(_L("WIM | WIMServer | CWimServer::ConstructL | Begin"));
       
   132     StartL( KWIMServerName );
       
   133     _WIMTRACE(_L("WIM | WIMServer | CWimServer::ConstructL | StartL"));
       
   134 
       
   135     iWimServer = this;
       
   136     InitializeCallbackFunctions();  // Initialize WIMI callback functions
       
   137     iWimSessionRegistry = CWimSessionRegistry::NewL();
       
   138     iWimTrustSettingsStore = CWimTrustSettingsStore::NewL();
       
   139     iWimTimer = CWimTimer::NewL( this );
       
   140 #ifdef WIMSERVER_SHUTDOWN    
       
   141     iShutdown.ConstructL();
       
   142     iShutdown.Start();
       
   143 #endif    
       
   144      _WIMTRACE(_L("WIM | WIMServer | CWimServer::ConstructL | End"));
       
   145     }
       
   146 
       
   147 // -----------------------------------------------------------------------------
       
   148 // CWimServer::NewL
       
   149 // Two-phased constructor.
       
   150 // -----------------------------------------------------------------------------
       
   151 //
       
   152 CWimServer* CWimServer::NewL()
       
   153     {
       
   154     _WIMTRACE(_L("WIM | WIMServer | CWimServer::NewL | Begin"));
       
   155     CWimServer* self = new( ELeave ) CWimServer( EPriorityHigh );
       
   156     CleanupStack::PushL( self );
       
   157     self->ConstructL() ;
       
   158     CleanupStack::Pop( self );
       
   159     return self;
       
   160     }
       
   161 
       
   162 // Destructor
       
   163 CWimServer::~CWimServer()
       
   164     {
       
   165     _WIMTRACE(_L("WIM | WIMServer | CWimServer::~CWimServer | Begin"));
       
   166     if ( iRefreshObserver )
       
   167         {
       
   168         delete iRefreshObserver;
       
   169         iRefreshObserver = NULL;
       
   170         }
       
   171     if ( iBTSapObserver )
       
   172         {
       
   173         delete iBTSapObserver;
       
   174         iBTSapObserver = NULL;
       
   175         }
       
   176     // Call close down request only if WIM is initialized
       
   177     if ( iWimInitialized )
       
   178         {
       
   179         WIMI_CloseDownReq();
       
   180         _WIMTRACE(_L("WIM | WIMServer | CWimServer::~CWimServer | WIMI_CloseDownReq"));
       
   181         }
       
   182     //iWimTrustSettingsStore->CloseD();
       
   183     delete iWimTrustSettingsStore;
       
   184     delete iWimSessionRegistry;
       
   185     delete iWimTimer;
       
   186     CWimCallBack::DeletegApdu();
       
   187     _WIMTRACE(_L("WIM | WIMServer | CWimServer::~CWimServer | delete iWimSessionRegistry"));
       
   188     }
       
   189 
       
   190 // -----------------------------------------------------------------------------
       
   191 // CWimServer::WimInitialize
       
   192 // Initialize WIMI API, fetches WIM data to WIMI.
       
   193 // -----------------------------------------------------------------------------
       
   194 //
       
   195 void CWimServer::WimInitialize( const RMessage2& aMessage )
       
   196     {
       
   197     _WIMTRACE(_L("WIM | WIMServer | CWimServer::WimInitialize | Begin"));
       
   198 
       
   199     // Create observer for bluetooth sap event.
       
   200     // This is checked every time we enter here.
       
   201     if ( !iBTSapObserver )
       
   202         {
       
   203         TRAPD( err, iBTSapObserver = CWimBTSapObserver::NewL());
       
   204         if ( KErrNone != err )
       
   205             {
       
   206             // creation failed
       
   207             iBTSapObserver = NULL; // to be sure
       
   208             _WIMTRACE(_L("WIM|CWimServer::WimInitialize | BT Sap observer NOT created."));
       
   209             }
       
   210         }
       
   211     // If BT SAP observer exists, start it
       
   212     if ( iBTSapObserver )
       
   213         {
       
   214         iBTSapObserver->Start(); //If already started, this doesn't start again
       
   215         }
       
   216 
       
   217     // Get state of smart card
       
   218     TInt simState = CWimUtilityFuncs::SimState();
       
   219 
       
   220     // Check whether state of Smart Card is OK and whether BT SAP is connected
       
   221     if ( simState != KErrNone ) // Card is not OK
       
   222         {
       
   223         SetWimInitialized( EFalse, simState );
       
   224         iWimStatus = simState;
       
   225         aMessage.Complete( iWimStatus );
       
   226         }
       
   227     else // Smart Card is ready
       
   228         {
       
   229         // Initialize WIMI if not yet done
       
   230         // iWimInitialized is set in callback function InitOKResp()
       
   231         if ( !iWimInitialized )
       
   232             {
       
   233             TInt initStatus = CWimUtilityFuncs::MapWIMError(
       
   234                                       WIMI_InitializeReq( &iWimCallBack ) );
       
   235 
       
   236             _WIMTRACE2(_L("WIM|WIMServer|CWimServer::WimInitialize|initStatus=%d"), initStatus );
       
   237 
       
   238             // In case WIMI_InitializeReq returs error
       
   239             // iWimStatus may not be set
       
   240             if ( initStatus != KErrNone )
       
   241                 {
       
   242                 SetWimInitialized( EFalse, initStatus  );
       
   243                 iWimStatus = KErrNotReady;
       
   244                 }
       
   245             }
       
   246         else // WIMI already initialized
       
   247             {
       
   248             _WIMTRACE(_L("WIM|WIMServer|CWimServer::WimInitialize|Wim already initialized"));
       
   249             iWimStatus = KErrNone;
       
   250             }
       
   251 
       
   252         // Create observer for SAT refresh event.
       
   253         // This is checked every time we enter here. It may have failed before
       
   254         // because SAT Server wasn't started.
       
   255         if ( !iRefreshObserver )
       
   256             {
       
   257             TRAPD( err, iRefreshObserver = CWimSatRefreshObserver::NewL());
       
   258             if ( KErrNone != err )
       
   259                 {
       
   260                 // creation failed
       
   261                 iRefreshObserver = NULL; // to be sure
       
   262                 _WIMTRACE(_L("WIM|CWimServer::WimInitialize | SAT Refresh observer NOT created."));
       
   263                 }
       
   264             }
       
   265 
       
   266         aMessage.Complete( iWimStatus );
       
   267         }
       
   268     }
       
   269 
       
   270 // -----------------------------------------------------------------------------
       
   271 // CWimServer::SetWimInitialized
       
   272 // Set iWimInitialized flag
       
   273 // -----------------------------------------------------------------------------
       
   274 //
       
   275 void CWimServer::SetWimInitialized( TBool aInitialized, TInt aStatus )
       
   276     {
       
   277     _WIMTRACE2(_L("WIM|WIMServer|CWimServer::SetWimInitialized|Value=%d"), aInitialized);
       
   278     iWimInitialized = aInitialized;
       
   279 
       
   280     if ( !aInitialized )
       
   281         {
       
   282         iWimStatus = aStatus;
       
   283         }
       
   284     }
       
   285 
       
   286 // -----------------------------------------------------------------------------
       
   287 // CWimServer::NewSessionL
       
   288 // Creates a new sharable session.
       
   289 // -----------------------------------------------------------------------------
       
   290 //
       
   291 CSession2* CWimServer::NewSessionL(
       
   292     const TVersion& aVersion,
       
   293     const RMessage2& /*aMessage*/ ) const
       
   294     {
       
   295     _WIMTRACE(_L("WIM | WIMServer | CWimServer::NewSessionL | Begin"));
       
   296     TVersion v( KWIMServMajorVersionNumber, KWIMServMinorVersionNumber,
       
   297         KWIMServBuildVersionNumber );
       
   298 
       
   299     if ( !User::QueryVersionSupported( v, aVersion ) )
       
   300         {
       
   301         User::Leave( KErrNotSupported );
       
   302         }
       
   303 
       
   304     CWimSession* wimSession;
       
   305     wimSession = CWimSession::NewL( ( CWimServer* )this );
       
   306     iWimSessionRegistry->AddSessionL( wimSession );
       
   307 
       
   308     return wimSession;
       
   309     }
       
   310 
       
   311 // -----------------------------------------------------------------------------
       
   312 // CWimServer::InitializeCallbackFunctions
       
   313 // WIMI API callback initialization.
       
   314 // -----------------------------------------------------------------------------
       
   315 //
       
   316 void CWimServer::InitializeCallbackFunctions()
       
   317     {
       
   318     _WIMTRACE(_L("WIM | WIMServer | CWimSession::InitializeCallbackFunctions | Begin"));
       
   319     _WIMTRACE2(_L("WIM | WIMServer | CWimSession::InitializeCallbackFunctions | iWimCallBack = %d"), iWimCallBack );
       
   320 
       
   321     iWimCallBack.InitOkResp = CWimCallBack::InitOkResp;
       
   322     iWimCallBack.CloseDownResp = CWimCallBack::CloseDownResp;
       
   323     iWimCallBack.SignResp = CWimCallBack::SignResp;
       
   324     iWimCallBack.CertificateResp = CWimCallBack::CertificateResp;
       
   325     iWimCallBack.CertificateDeleteResp = CWimCallBack::CertificateDeleteResp;
       
   326     iWimCallBack.CertificateStoreResp = CWimCallBack::CertificateStoreResp;
       
   327     iWimCallBack.VerifyPINResp = CWimCallBack::VerifyPINResp;
       
   328     iWimCallBack.ChangePINResp = CWimCallBack::ChangePINResp;
       
   329     iWimCallBack.UnblockPINResp = CWimCallBack::UnblockPINResp;
       
   330     iWimCallBack.EnablePINResp = CWimCallBack::EnablePINResp;
       
   331     iWimCallBack.CardInsertedResp = CWimCallBack::CardInsertedResp;
       
   332     iWimCallBack.Open = CWimCallBack::Open;
       
   333     iWimCallBack.Close = CWimCallBack::Close;
       
   334     iWimCallBack.APDUReq = CWimCallBack::APDUReq;
       
   335     iWimCallBack.GetATRReq = CWimCallBack::GetATRReq;
       
   336     iWimCallBack.GetReaderListReq = CWimCallBack::GetReaderListReq;
       
   337     iWimCallBack.ReadSimFileReq = CWimCallBack::ReadSimFileReq;
       
   338     iWimCallBack.ProvSCFileResp = CWimCallBack::OmaProvisioningResp;
       
   339 
       
   340      _WIMTRACE(_L("WIM | WIMServer | CWimSession::InitializeCallbackFunctions | END"));
       
   341     }
       
   342 
       
   343 // -----------------------------------------------------------------------------
       
   344 // CWimServer::PanicServer
       
   345 // Panics the server.
       
   346 // -----------------------------------------------------------------------------
       
   347 //
       
   348 void CWimServer::PanicServer( TWimServerPanic aPanic )
       
   349     {
       
   350     _WIMTRACE(_L("WIM | WIMServer | CWimServer::PanicServer | Begin"));
       
   351     // Set server state to EWimServerNotRunning
       
   352     TInt retVal = RProperty::Set( KUidSystemCategory,
       
   353                                   KWimServerUid.iUid,
       
   354                                   EWimServerNotRunning );
       
   355     retVal = retVal; // To prevent warning
       
   356 
       
   357     User::Panic( KWIMServerName, aPanic );
       
   358     }
       
   359 
       
   360 // -----------------------------------------------------------------------------
       
   361 // CWimServer::Server
       
   362 // Return static iServer
       
   363 // -----------------------------------------------------------------------------
       
   364 //
       
   365 CWimServer* CWimServer::Server()
       
   366     {
       
   367     _WIMTRACE(_L("WIM | WIMServer | CWimServer::Server | Begin"));
       
   368     return CWimServer::iWimServer;
       
   369     }
       
   370 
       
   371 // -----------------------------------------------------------------------------
       
   372 // CWimServer::WimSessionRegistry
       
   373 // Get pointer to WimSessionRegistry
       
   374 // -----------------------------------------------------------------------------
       
   375 //
       
   376 CWimSessionRegistry* CWimServer::WimSessionRegistry()
       
   377     {
       
   378     _WIMTRACE(_L("WIM | WIMServer | CWimServer::WimSessionRegistry | Begin"));
       
   379     return iWimSessionRegistry;
       
   380     }
       
   381 
       
   382 // -----------------------------------------------------------------------------
       
   383 // CWimServer::GetSessions
       
   384 // Return all sessions of server
       
   385 // -----------------------------------------------------------------------------
       
   386 //
       
   387 void CWimServer::GetSessionsL( RArray<CWimSession*>& aSessions ) const
       
   388     {
       
   389     _WIMTRACE(_L("WIM | WIMServer | CWimServer::GetSessionsL | Begin"));
       
   390     iWimSessionRegistry->GetSessionsL( aSessions );
       
   391     }
       
   392 
       
   393 // -----------------------------------------------------------------------------
       
   394 // CWimServer::WimTrustSettingsStore
       
   395 // Return TrustSettingsStore pointer
       
   396 // -----------------------------------------------------------------------------
       
   397 //
       
   398 CWimTrustSettingsStore* CWimServer::WimTrustSettingsStore()
       
   399     {
       
   400     _WIMTRACE(_L("WIM | WIMServer | CWimServer::WimTrustSettingsStore | Begin"));
       
   401     return iWimTrustSettingsStore;
       
   402     }
       
   403 
       
   404 // -----------------------------------------------------------------------------
       
   405 // CWimServer::CustomSecurityCheckL
       
   406 // Custom policy check for requests of certain WimServer services.Called by
       
   407 // framework.
       
   408 // -----------------------------------------------------------------------------
       
   409 //
       
   410     CPolicyServer::TCustomResult
       
   411 CWimServer::CustomSecurityCheckL( const RMessage2& aMsg,
       
   412                                   TInt& /*aAction*/,
       
   413                                   TSecurityInfo& /*aMissing*/ )
       
   414     {
       
   415     _WIMTRACE2(_L("CWimServer::CustomSecurityCheckL | Begin, heap=%d"),
       
   416                User::CountAllocCells());
       
   417 
       
   418     WIMI_STAT callStatus;
       
   419     TUint8 usage( 255 ); // init stg unused
       
   420     // Set initial return value to failed
       
   421     CPolicyServer::TCustomResult test( EFail );
       
   422     TInt function = aMsg.Function();
       
   423 
       
   424     _WIMTRACE4(_L("CWimServer::CustomSecurityCheckL: SID=%d,VID=%d, Function=%d"),
       
   425                aMsg.SecureId().iId, aMsg.VendorId().iId, function);
       
   426 
       
   427     switch ( function )
       
   428         {
       
   429         case EGetWIMCertDetails:
       
   430             {
       
   431             //Leave the capability checking to Certificate Handler object
       
   432             test = EPass;
       
   433             break;
       
   434             }
       
   435 
       
   436         case EGetCertExtras:
       
   437             {
       
   438             // Int2 contains usage of required cert
       
   439             TWimEntryType certEntryType = ( TWimEntryType ) aMsg.Int2();
       
   440 
       
   441             switch ( certEntryType )
       
   442                 {
       
   443                 case EWimEntryTypeCA:
       
   444                     {
       
   445                     // CA cert reading does not require caps
       
   446                     _WIMTRACE(_L("CWimServer::CustomSecurityCheckL: CA cert read req, OK"));
       
   447                     test = EPass;
       
   448                     break;
       
   449                     }
       
   450                 case EWimEntryTypePersonal: //lint -fallthrough
       
   451                 case EWimEntryTypeAll:
       
   452                     {
       
   453                     // User cert reading requires caps
       
   454                     test = CheckReadCapsForUsage( aMsg, WIMI_CU_Client );
       
   455                     break;
       
   456                     }
       
   457                 default:
       
   458                     {
       
   459                     test = EFail;
       
   460                     break;
       
   461                     }
       
   462                 }
       
   463             break;
       
   464             }
       
   465 
       
   466         case EStoreCertificate:
       
   467             {
       
   468             ResolveStoreCertReqL( aMsg, usage );
       
   469             test = CheckWriteCapsForUsage( aMsg, usage );
       
   470             break;
       
   471             }
       
   472 
       
   473         case ERemoveCertificate:
       
   474             {
       
   475             callStatus = ResolveRemoveCertReqL( aMsg, usage );
       
   476             if ( WIMI_Ok == callStatus )
       
   477                 {
       
   478                 test = CheckWriteCapsForUsage( aMsg, usage );
       
   479                 }
       
   480             break;
       
   481             }
       
   482 
       
   483         case ESignTextReq:
       
   484             {
       
   485             test = CheckReadCapsForUsage( aMsg, WIMI_CU_Client );
       
   486             break;
       
   487             }
       
   488 
       
   489         case ESetApplicability:         //lint -fallthrough
       
   490         case ESetTrust:                 //lint -fallthrough
       
   491         case ESetDefaultTrustSettings:  //lint -fallthrough
       
   492         case ERemoveTrustSettings:
       
   493             {
       
   494             if ( ResolveTrustModficationReqL( aMsg, usage ))
       
   495                 {
       
   496                 test = CheckWriteCapsForUsage( aMsg, usage );
       
   497                 }
       
   498             break;
       
   499             }
       
   500 
       
   501         case EExportPublicKey:
       
   502             {
       
   503             ResolveExportPublicReqL( aMsg, usage );
       
   504             test = CheckReadCapsForUsage( aMsg, usage );
       
   505             break;
       
   506             }
       
   507 
       
   508         default:
       
   509             {
       
   510             _WIMTRACE2(_L("CWimServer::CustomSecurityCheckL: UNKNOWN FUNCTION %d"),
       
   511                         function);
       
   512             // Inconsistent policy, should not be here. Panic.
       
   513             PanicServer( EWimIncorrectPolicy );
       
   514             break;
       
   515             }
       
   516         } // switch ( function )
       
   517 
       
   518     _WIMTRACE2(_L("CWimServer::CustomSecurityCheckL: Result=%d"), test);
       
   519     _WIMTRACE2(_L("CWimServer::CustomSecurityCheckL | End, heap=%d"),
       
   520                User::CountAllocCells());
       
   521 
       
   522     return test;
       
   523     }
       
   524 
       
   525 
       
   526 // -----------------------------------------------------------------------------
       
   527 // CWimServer::ResolveCertUsage
       
   528 // Resolves usage (CA/User) for a certificate.
       
   529 // -----------------------------------------------------------------------------
       
   530 //
       
   531 WIMI_STAT CWimServer::ResolveCertUsage( const RMessage2& aMsg,
       
   532                                         TUint8& aUsage )
       
   533     {
       
   534     _WIMTRACE(_L("CWimServer::ResolveCertUsage | Begin"));
       
   535 
       
   536     // aMsg.Ptr0 contains reference to certificate
       
   537     WIMI_Ref_pt pCertRef = const_cast< WIMI_Ref_pt >( aMsg.Ptr0() );
       
   538 
       
   539     WIMI_STAT callStatus = GetCertificateInfo( pCertRef, aUsage );
       
   540 
       
   541     _WIMTRACE(_L("CWimServer::ResolveCertUsage | End"));
       
   542     return callStatus;
       
   543     }
       
   544 
       
   545 
       
   546 // -----------------------------------------------------------------------------
       
   547 // CWimServer::GetCertificateInfo
       
   548 // Fetches certificate info. Wrapper for WIMI call.
       
   549 // -----------------------------------------------------------------------------
       
   550 //
       
   551 WIMI_STAT CWimServer::GetCertificateInfo( WIMI_Ref_pt aCertRef,
       
   552                                           TUint8& aUsage )
       
   553     {
       
   554     _WIMTRACE(_L("CWimServer::GetCertificateInfo | Begin"));
       
   555 
       
   556     WIMI_Ref_pt     pWimRef = NULL;
       
   557     WIMI_BinData_t  ptLabel;
       
   558     WIMI_BinData_t  ptKeyID;
       
   559     WIMI_BinData_t  ptCAID;
       
   560     WIMI_BinData_t  ptIssuerHash;
       
   561     WIMI_BinData_t  ptTrustedUsage;
       
   562     TUint8          uiCDFRefs;
       
   563     TUint8          type;
       
   564     TUint16         certLen;
       
   565     TUint8          modifiable = 0;
       
   566 
       
   567     WIMI_STAT callStatus = WIMI_GetCertificateInfo(
       
   568                                 aCertRef,
       
   569                                 &pWimRef,
       
   570                                 &ptLabel,
       
   571                                 &ptKeyID, // Key Id (hash)
       
   572                                 &ptCAID,
       
   573                                 &ptIssuerHash,
       
   574                                 &ptTrustedUsage,
       
   575                                 &uiCDFRefs,
       
   576                                 &aUsage,  // 0 = client, 1 = CA
       
   577                                 &type,    // WTLSCert(1),
       
   578                                           // X509Cert(2),
       
   579                                           // X968Cert(3),
       
   580                                           // CertURL(4)
       
   581                                 &certLen, // cert. content or URL length
       
   582                                 &modifiable );
       
   583 
       
   584     if ( callStatus == WIMI_Ok )
       
   585         {
       
   586         free_WIMI_Ref_t( pWimRef );
       
   587         WSL_OS_Free( ptLabel.pb_buf );
       
   588         WSL_OS_Free( ptKeyID.pb_buf );
       
   589         WSL_OS_Free( ptCAID.pb_buf );
       
   590         WSL_OS_Free( ptIssuerHash.pb_buf );
       
   591         WSL_OS_Free( ptTrustedUsage.pb_buf );
       
   592         }
       
   593 
       
   594     _WIMTRACE(_L("CWimServer::GetCertificateInfo | End"));
       
   595     return callStatus;
       
   596     }
       
   597 
       
   598 
       
   599 // -----------------------------------------------------------------------------
       
   600 // CWimServer::ResolveStoreCertReqL
       
   601 // Resolves which kind of certificate (CA/User) client is accessing.
       
   602 // -----------------------------------------------------------------------------
       
   603 //
       
   604 void CWimServer::ResolveStoreCertReqL( const RMessage2& aMsg,
       
   605                                        TUint8& aUsage )
       
   606     {
       
   607     _WIMTRACE(_L("CWimServer::ResolveStoreCertReqL | Begin"));
       
   608     TPckgBuf<TWimCertAddParameters> certInfoPckg;
       
   609     aMsg.ReadL( 1, certInfoPckg );
       
   610 
       
   611     // Convert from TWimCertType to TUint8
       
   612     if ( certInfoPckg().iUsage == EWimCertTypeCA )
       
   613         {
       
   614         aUsage = WIMI_CU_CA;     // 1
       
   615         _WIMTRACE(_L("CWimServer::ResolveStoreCertReqL | Usage=CA"));
       
   616         }
       
   617     else
       
   618         {
       
   619         aUsage = WIMI_CU_Client; // 0
       
   620         _WIMTRACE(_L("CWimServer::ResolveStoreCertReqL | Usage=Client"));
       
   621         }
       
   622     _WIMTRACE(_L("CWimServer::ResolveStoreCertReqL | End"));
       
   623     }
       
   624 
       
   625 
       
   626 // -----------------------------------------------------------------------------
       
   627 // CWimServer::ResolveRemoveCertReqL
       
   628 // Resolves which kind of certificate client is accessing.
       
   629 // -----------------------------------------------------------------------------
       
   630 //
       
   631 WIMI_STAT CWimServer::ResolveRemoveCertReqL( const RMessage2& aMsg,
       
   632                                              TUint8& aUsage )
       
   633     {
       
   634     _WIMTRACE2(_L("CWimServer::ResolveRemoveCertReqL | Begin, heap=%d"),
       
   635                User::CountAllocCells());
       
   636 
       
   637     WIMI_STAT callStatus = WIMI_Ok;
       
   638     TWimCertRemoveAddr dataStruct = {0};
       
   639 
       
   640     TPckgBuf<TWimCertRemoveAddr> wimCertRemoveAddrPckg( dataStruct );
       
   641     aMsg.ReadL( 0, wimCertRemoveAddrPckg );
       
   642     WIMI_Ref_pt pCertRef = ( void* )wimCertRemoveAddrPckg().iCertAddr;
       
   643 
       
   644     callStatus = GetCertificateInfo( pCertRef, aUsage );
       
   645 
       
   646     _WIMTRACE2(_L("CWimServer::ResolveRemoveCertReqL | End, heap=%d"),
       
   647                User::CountAllocCells());
       
   648 
       
   649     return callStatus;
       
   650     }
       
   651 
       
   652 
       
   653 // -----------------------------------------------------------------------------
       
   654 // CWimServer::ResolveTrustModficationReqL
       
   655 // Resolves which kind of certificate client is accessing.
       
   656 // -----------------------------------------------------------------------------
       
   657 //
       
   658 TBool CWimServer::ResolveTrustModficationReqL( const RMessage2& aMsg,
       
   659                                                TUint8& aUsage )
       
   660     {
       
   661     _WIMTRACE(_L("CWimServer::ResolveTrustModficationReqL | Begin"));
       
   662     TBool rc( EFalse );
       
   663     TPckgBuf<TWimCertInfoPckg> pckg;
       
   664     aMsg.ReadL( 0, pckg );
       
   665 
       
   666     TInt owner = pckg().iCertificateOwnerType;
       
   667 
       
   668     switch ( owner )
       
   669         {
       
   670         case ECACertificate:
       
   671             {
       
   672             aUsage = WIMI_CU_CA;
       
   673             rc = ETrue;
       
   674             _WIMTRACE(_L("CWimServer::ResolveTrustModficationReqL | Usage=CA"));
       
   675             break;
       
   676             }
       
   677         case EUserCertificate:     //lint -fallthrough
       
   678         case EPeerCertificate:
       
   679             {
       
   680             aUsage = WIMI_CU_Client;
       
   681             rc = ETrue;
       
   682             _WIMTRACE(_L("CWimServer::ResolveTrustModficationReqL | Usage=Client"));
       
   683             break;
       
   684             }
       
   685         default:
       
   686             {
       
   687             rc = EFalse;
       
   688             _WIMTRACE(_L("CWimServer::ResolveTrustModficationReqL | FAIL"));
       
   689             break;
       
   690             }
       
   691         }
       
   692     return rc;
       
   693     }
       
   694 
       
   695 
       
   696 // -----------------------------------------------------------------------------
       
   697 // CWimServer::ResolveExportPublicReqL
       
   698 // Resolves which kind of key (CA/User) client requests.
       
   699 // -----------------------------------------------------------------------------
       
   700 //
       
   701 WIMI_STAT CWimServer::ResolveExportPublicReqL( const RMessage2& aMsg,
       
   702                                                TUint8& aUsage )
       
   703     {
       
   704     _WIMTRACE2(_L("CWimServer::ResolveExportPublicReqL | Begin, heap=%d"),
       
   705                User::CountAllocCells());
       
   706 
       
   707     WIMI_STAT callStatus = WIMI_Ok;
       
   708 
       
   709     TPckgBuf<TExportPublicKey> keyExportPckg;
       
   710     aMsg.ReadL( 0, keyExportPckg );
       
   711 
       
   712     TBuf8<KKeyIdLen> keyIdBuf = keyExportPckg().iKeyId;
       
   713     TUint8 clicertCount = 0;
       
   714     WIMI_RefList_t clicertRefList = NULL;
       
   715     const TUint16 KInvalidUsage = 10;
       
   716     TUint16 usage = KInvalidUsage;
       
   717 
       
   718     callStatus = WIMI_GetCertificateListByKeyHash( ( TUint8* ) keyIdBuf.Ptr(),
       
   719                                             WIMI_CU_Client,
       
   720                                             &clicertCount,
       
   721                                             &clicertRefList );
       
   722     if( callStatus != WIMI_Ok )
       
   723         {
       
   724     	return callStatus;
       
   725         }
       
   726     free_WIMI_RefList_t( clicertRefList );
       
   727     
       
   728     if ( clicertCount > 0 )
       
   729         {
       
   730     	usage = WIMI_CU_Client;
       
   731         }    
       
   732     else
       
   733         {
       
   734     	TUint8 cacertCount = 0;
       
   735         WIMI_RefList_t cacertRefList = NULL;
       
   736     
       
   737         callStatus = WIMI_GetCertificateListByKeyHash( ( TUint8* ) keyIdBuf.Ptr(),
       
   738                                             WIMI_CU_CA,
       
   739                                             &cacertCount,
       
   740                                             &cacertRefList );
       
   741         if ( callStatus != WIMI_Ok )
       
   742             {
       
   743             return callStatus;
       
   744             }
       
   745         free_WIMI_RefList_t( cacertRefList );
       
   746 
       
   747         if ( cacertCount > 0 )
       
   748             {
       
   749     	    usage = WIMI_CU_CA;
       
   750             }
       
   751         else
       
   752             {
       
   753         	aMsg.Complete( KErrBadHandle );
       
   754             }
       
   755         }
       
   756     
       
   757     aUsage = static_cast< TUint8 >( usage );
       
   758 
       
   759     _WIMTRACE2(_L("CWimServer::ResolveExportPublicReqL | End, heap=%d"),
       
   760                User::CountAllocCells());
       
   761 
       
   762     return callStatus;
       
   763     }
       
   764 
       
   765 
       
   766 // -----------------------------------------------------------------------------
       
   767 // CWimServer::CheckReadCapsForUsage
       
   768 // Check if client has sufficient read capability for service in question.
       
   769 // -----------------------------------------------------------------------------
       
   770 //
       
   771     CPolicyServer::TCustomResult
       
   772 CWimServer::CheckReadCapsForUsage( const RMessage2& aMsg,
       
   773                                    TUint8 aUsage )
       
   774     {
       
   775     _WIMTRACE2(_L("CWimServer::CheckReadCapsForUsage | Begin, usage=%d"), aUsage );
       
   776 
       
   777     CPolicyServer::TCustomResult rc( EFail );
       
   778 
       
   779     switch ( aUsage )
       
   780         {
       
   781         case WIMI_CU_CA:
       
   782             {
       
   783             // CA certificate reading doesn't require any capability.
       
   784             _WIMTRACE(_L("CWimServer::CheckReadCapsForUsage: CA cert read req, OK."));
       
   785             rc = EPass;
       
   786             break;
       
   787             }
       
   788         case WIMI_CU_Client:
       
   789             {
       
   790             // User certificate reading requires ReadUserData capability.
       
   791             if ( aMsg.HasCapability( ECapabilityReadUserData ))
       
   792                 {
       
   793                 rc = EPass;
       
   794                 _WIMTRACE(_L("CWimServer::CheckReadCapsForUsage: User cert read capa PASS"));
       
   795                 }
       
   796             else
       
   797                 {
       
   798                 rc = EFail;
       
   799                 _WIMTRACE(_L("CWimServer::CheckReadCapsForUsage: User cert read capa FAIL"));
       
   800                 }
       
   801             break;
       
   802             }
       
   803         default:
       
   804             {
       
   805             _WIMTRACE(_L("CWimServer::CheckReadCapsAccordingToUsage: FAIL:Unknown usage."));
       
   806             rc = EFail;
       
   807             break;
       
   808             }
       
   809         }
       
   810     _WIMTRACE(_L("CWimServer::CheckReadCapsForUsage | End"));
       
   811     return rc;
       
   812     }
       
   813 
       
   814 
       
   815 // -----------------------------------------------------------------------------
       
   816 // CWimServer::CheckWriteCapsForUsage
       
   817 // Check if client has sufficient write capability for service in question.
       
   818 // -----------------------------------------------------------------------------
       
   819 //
       
   820     CPolicyServer::TCustomResult
       
   821 CWimServer::CheckWriteCapsForUsage( const RMessage2& aMsg,
       
   822                                     TUint8 aUsage )
       
   823     {
       
   824     _WIMTRACE2(_L("CWimServer::CheckWriteCapsForUsage | Begin, usage=%d"), aUsage);
       
   825 
       
   826     CPolicyServer::TCustomResult rc( EFail );
       
   827 
       
   828     switch ( aUsage )
       
   829         {
       
   830         case WIMI_CU_CA:
       
   831             {
       
   832             // CA certificate writing/modifying requires WriteDeviceData
       
   833             // capability.
       
   834             if ( aMsg.HasCapability( ECapabilityWriteDeviceData ))
       
   835                 {
       
   836                 rc = EPass;
       
   837                 _WIMTRACE(_L("CWimServer::CheckWriteCapsForUsage: CA cert write capa PASS"));
       
   838                 }
       
   839             else
       
   840                 {
       
   841                 rc = EFail;
       
   842                 _WIMTRACE(_L("CWimServer::CheckWriteCapsForUsage: CA cert write capa FAIL"));
       
   843                 }
       
   844             break;
       
   845             }
       
   846         case WIMI_CU_Client:
       
   847             {
       
   848             // User certificate writing/modifying requires WriteUserData
       
   849             // capability.
       
   850             if ( aMsg.HasCapability( ECapabilityWriteUserData ))
       
   851                 {
       
   852                 rc = EPass;
       
   853                 _WIMTRACE(_L("CWimServer::CheckWriteCapsForUsage: User cert write capa PASS"));
       
   854                 }
       
   855             else
       
   856                 {
       
   857                 rc = EFail;
       
   858                 _WIMTRACE(_L("CWimServer::CheckWriteCapsForUsage: User cert write capa FAIL"));
       
   859                 }
       
   860             break;
       
   861             }
       
   862         default:
       
   863             {
       
   864             _WIMTRACE(_L("CWimServer::CheckWriteCapsForUsage: FAIL:Unknown usage."));
       
   865             rc = EFail; // Unknown cert usage -> fail to be sure
       
   866             break;
       
   867             }
       
   868         }
       
   869     _WIMTRACE(_L("CWimServer::CheckWriteCapsForUsage | End"));
       
   870     return rc;
       
   871     }
       
   872 
       
   873 
       
   874 // -----------------------------------------------------------------------------
       
   875 // CWimServer::IsAccessingToken
       
   876 // Tells if currently requested service is accessing physical token.
       
   877 // -----------------------------------------------------------------------------
       
   878 //
       
   879 TBool CWimServer::IsAccessingToken()
       
   880     {
       
   881     return iIsAccessingToken;
       
   882     }
       
   883 
       
   884 // -----------------------------------------------------------------------------
       
   885 // CWimServer::SetIsAccessingToken
       
   886 // Tell that currently requested service is accessing physical token.
       
   887 // -----------------------------------------------------------------------------
       
   888 //
       
   889 void CWimServer::SetIsAccessingToken( TBool aValue )
       
   890     {
       
   891     iIsAccessingToken = aValue;
       
   892     }
       
   893 
       
   894 // -----------------------------------------------------------------------------
       
   895 // CWimServer::RefreshNotificationReceived
       
   896 // Informs server about completed SIM Refresh operation. Returns true if SIM
       
   897 // Refresh notification is received in the middle of service request that
       
   898 // accesses physical token. See IsAccessingToken().
       
   899 // Server shall re-initialize itself at earliest convenience.
       
   900 // -----------------------------------------------------------------------------
       
   901 //
       
   902 TBool CWimServer::RefreshNotificationReceived()
       
   903     {
       
   904     return iRefreshNotificationReceived;
       
   905     }
       
   906 
       
   907 // -----------------------------------------------------------------------------
       
   908 // CWimServer::SetRefreshNotificationReceived
       
   909 // Tell server about completed SIM Refresh operation. Is set if SIM
       
   910 // Refresh notification is received in the middle of service request
       
   911 // that accesses physical token. See IsAccessingToken().
       
   912 // Server shall re-initialize itself at earliest convenience.
       
   913 // -----------------------------------------------------------------------------
       
   914 //
       
   915 void CWimServer::SetRefreshNotificationReceived( TBool aValue )
       
   916     {
       
   917     iRefreshNotificationReceived = aValue;
       
   918     }
       
   919 
       
   920 // -----------------------------------------------------------------------------
       
   921 // CWimServer::TimerExpired()
       
   922 // Interface derived from 
       
   923 // -----------------------------------------------------------------------------
       
   924 //
       
   925 void CWimServer::TimerExpired()
       
   926     {
       
   927     _WIMTRACE(_L("WIM | WIMServer | CWimServer::TimerExpired | Begin"));
       
   928     WIMI_Ref_pt pWimRefTemp = NULL;
       
   929 
       
   930     pWimRefTemp = WIMI_GetWIMRef( 0 );
       
   931  
       
   932     if ( pWimRefTemp )  // Close the WIM
       
   933         {
       
   934         WIMI_CloseWIM( pWimRefTemp );
       
   935         free_WIMI_Ref_t( pWimRefTemp );
       
   936         }
       
   937     //SetWimInitialized( EFalse );
       
   938     }
       
   939 
       
   940 // -----------------------------------------------------------------------------
       
   941 // CWimServer::WimTimer()
       
   942 // Return the pointer of Timer 
       
   943 // -----------------------------------------------------------------------------
       
   944 //
       
   945 CWimTimer* CWimServer::WimTimer()
       
   946     {
       
   947     _WIMTRACE(_L("WIM | WIMServer | CWimServer::WimTimer | Begin"));
       
   948     return iWimTimer;
       
   949     }
       
   950 
       
   951 // -----------------------------------------------------------------------------
       
   952 // CWimServer::CancelWimInitialize()
       
   953 // Cancel Wim Initialize 
       
   954 // -----------------------------------------------------------------------------
       
   955 //
       
   956 void CWimServer::CancelWimInitialize( const RMessage2& aMessage )
       
   957     {
       
   958 	 if ( !iWimInitialized )
       
   959 		 {
       
   960 		 CWimCallBack::CancelWimInitialize();	
       
   961 		 }
       
   962 	aMessage.Complete( KErrNone );	 
       
   963     }
       
   964 
       
   965 
       
   966 #ifdef WIMSERVER_SHUTDOWN
       
   967 
       
   968 // CShutdown
       
   969 
       
   970 // ---------------------------------------------------------------------------
       
   971 // CShutdown::CShutdown()
       
   972 // ---------------------------------------------------------------------------
       
   973 //
       
   974 inline CShutdown::CShutdown() :
       
   975   CTimer(-1)
       
   976     {
       
   977     CActiveScheduler::Add(this);
       
   978     }
       
   979 
       
   980 
       
   981 // ---------------------------------------------------------------------------
       
   982 // CShutdown::ConstructL()
       
   983 // ---------------------------------------------------------------------------
       
   984 //
       
   985 inline void CShutdown::ConstructL()
       
   986     {
       
   987     CTimer::ConstructL();
       
   988     }
       
   989 
       
   990 
       
   991 // ---------------------------------------------------------------------------
       
   992 // CShutdown::Start()
       
   993 // ---------------------------------------------------------------------------
       
   994 //
       
   995 inline void CShutdown::Start()
       
   996     {
       
   997     After(KServerShutdownDelay);
       
   998     }
       
   999 
       
  1000 
       
  1001 // ---------------------------------------------------------------------------
       
  1002 // CShutdown::RunL()
       
  1003 // Initiate server exit when the timer expires.
       
  1004 // ---------------------------------------------------------------------------
       
  1005 //
       
  1006 void CShutdown::RunL()
       
  1007     {
       
  1008      _WIMTRACE(_L("WIM | WIMServer |  CShutdown::RunL"));
       
  1009     CActiveScheduler::Stop();
       
  1010     _WIMTRACE(_L("WIM | WIMServer |  CShutdown::End"));
       
  1011     }
       
  1012 
       
  1013 // ---------------------------------------------------------------------------
       
  1014 // CWimServer::DropSession()
       
  1015 // ---------------------------------------------------------------------------
       
  1016 //
       
  1017 void CWimServer::AddSession()
       
  1018     {
       
  1019     ++iSessionCount;
       
  1020     iShutdown.Cancel();
       
  1021     }
       
  1022 
       
  1023 
       
  1024 // ---------------------------------------------------------------------------
       
  1025 // CWimServer::DropSession()
       
  1026 // ---------------------------------------------------------------------------
       
  1027 //
       
  1028 void CWimServer::DropSession()
       
  1029     {
       
  1030     if (--iSessionCount==0)
       
  1031         {
       
  1032         iShutdown.Start();
       
  1033         }
       
  1034     }
       
  1035 
       
  1036 #endif //WIMSERVER_SHUTDOWN