os/security/cryptoservices/filebasedcertificateandkeystores/source/generic/server/fsserver.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     1 /*
     2 * Copyright (c) 2004-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 the License "Eclipse Public License v1.0"
     6 * which accompanies this distribution, and is available
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
     8 *
     9 * Initial Contributors:
    10 * Nokia Corporation - initial contribution.
    11 *
    12 * Contributors:
    13 *
    14 * Description: 
    15 * fstokenserver.cpp
    16 *
    17 */
    18 
    19 
    20 #include "fsserver.h"
    21 #include "fstokenutil.h"
    22 #include "cfskeystoreserver.h"
    23 #include "CKeyStoreSession.h"
    24 #include "filecertstore.h"
    25 #include "CCertStoreSession.h"
    26 #include "CFSCertAppsServer.h"
    27 #include "CCertAppsSession.h"
    28 #include "FSResources.h"
    29 #include "FSDialog.h"
    30 #include "tokenserverdebug.h"
    31 #include "fstokenservername.h"
    32 
    33 // CTokenServerSession /////////////////////////////////////////////////////////
    34 
    35 CTokenServerSession::CTokenServerSession()
    36 	{
    37 	}
    38 
    39 inline CTokenServer& CTokenServerSession::Server()
    40 	{
    41 	return static_cast<CTokenServer&>(const_cast<CServer2&>(*CSession2::Server()));
    42 	}
    43 
    44 void CTokenServerSession::CreateL()
    45 	{
    46 	Server().AddSession();
    47 	}
    48 
    49 CTokenServerSession::~CTokenServerSession()
    50 	{
    51 	Server().DropSession();
    52 	}
    53 
    54 /**
    55  * Handle a client request.  Leaving is handled by
    56  * CTokenServerSession::ServiceError() which reports the error code to the
    57  * client.
    58  */
    59 void CTokenServerSession::ServiceL(const RMessage2& aMessage)
    60 	{
    61 #ifdef _DEBUG
    62 	// OOM testing
    63 	switch (aMessage.Function())
    64 		{
    65 		case EStartOOMTest:
    66 			TokenServerDebug::StartOOMTest();
    67 			aMessage.Complete(KErrNone);
    68 			return;
    69 			
    70 		case EIncHeapFailPoint:
    71 			TokenServerDebug::IncHeapFailPoint();
    72 			aMessage.Complete(KErrNone);
    73 			return;
    74 			
    75 		case EResetHeapFail:
    76 			TokenServerDebug::ResetHeapFail();
    77 			aMessage.Complete(KErrNone);
    78 			return;
    79 
    80 		case EAllocCount:
    81 			aMessage.Complete(User::CountAllocCells());
    82 			return;
    83 		}	
    84 #endif
    85 	
    86 	DoServiceL(aMessage);
    87 	}
    88 
    89 /**
    90  * Handle an error from ServiceL() A bad descriptor error implies a badly
    91  * programmed client, so panic it - otherwise report the error to the client.
    92  */
    93 void CTokenServerSession::ServiceError(const RMessage2& aMessage, TInt aError)
    94 	{
    95 	if (aError==KErrBadDescriptor)
    96 		{
    97 		PanicClient(aMessage, EPanicBadDescriptor);
    98 		}
    99 	
   100 	CSession2::ServiceError(aMessage, aError);
   101 	}
   102 
   103 // CTokenServer ////////////////////////////////////////////////////////////////
   104 
   105 inline CTokenServer::CTokenServer() :
   106 	CServer2(0, ESharableSessions)
   107 	{
   108 	}
   109 
   110 CServer2* CTokenServer::NewLC()
   111 	{
   112 	CTokenServer* self=new(ELeave) CTokenServer;
   113 	CleanupStack::PushL(self);
   114 	self->ConstructL();
   115 	return self;
   116 	}
   117 
   118 CTokenServer::~CTokenServer()
   119 	{
   120 	FSResources::Cleanup();
   121 	FSDialog::Cleanup();
   122 
   123 	delete iKeyStoreServer;
   124 	delete iCertStoreServer;
   125 	delete iCertAppsServer;
   126 	}
   127 
   128 /**	2nd phase construction - ensure the timer and server objects are running. */
   129 void CTokenServer::ConstructL()
   130 	{
   131 	FSResources::InitialiseL();
   132 	FSDialog::InitialiseL();
   133 	
   134 	TPtrC serverName(KFSTokenServerName());
   135 		// Naming the server thread after the server helps to debug panics
   136 #ifdef __WINS__
   137 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   138 	serverName.Set(KFSNewTokenServerName());
   139 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   140 #endif // __WINS__
   141 		
   142 	StartL(serverName);
   143 	
   144 	// Ensure that the server still exits even if the 1st client fails to connect
   145 	iShutdown.ConstructL();
   146 	iShutdown.Start();
   147 	}
   148 
   149 /** A new session is being created - cancel the shutdown timer if it was running. */
   150 void CTokenServer::AddSession()
   151 	{
   152 	++iSessionCount;
   153 	iShutdown.Cancel();
   154 	}
   155 
   156 /** A session is being destroyed - start the shutdown timer if it is the last session. */
   157 void CTokenServer::DropSession()
   158 	{
   159 	if (--iSessionCount==0)
   160 		{
   161 		iShutdown.Start();
   162 		}
   163 	}
   164 
   165 /** Lazily create key store server object. */
   166 CFSKeyStoreServer& CTokenServer::KeyStoreServerL() const
   167 	{
   168 	if (!iKeyStoreServer)
   169 		{
   170 		iKeyStoreServer = CFSKeyStoreServer::NewL();
   171 		}
   172 	
   173 	return *iKeyStoreServer;
   174 	}
   175 
   176 /** Lazily create cert store server object. */
   177 CFSCertStoreServer& CTokenServer::CertStoreServerL() const
   178 	{
   179 	if (!iCertStoreServer)
   180 		{
   181 		iCertStoreServer = CFSCertStoreServer::NewL();
   182 		}
   183 	
   184 	return *iCertStoreServer;
   185 	}
   186 
   187 /** Lazily create cert apps server object. */
   188 CFSCertAppsServer& CTokenServer::CertAppsServerL() const
   189 	{
   190 	if (!iCertAppsServer)
   191 		{
   192 		iCertAppsServer = CFSCertAppsServer::NewL();
   193 		}
   194 	
   195 	return *iCertAppsServer;
   196 	}
   197 
   198 /** Create a new client session. */
   199 CSession2* CTokenServer::NewSessionL(const TVersion& aVersion, const RMessage2& /*aMessage*/) const
   200 	{
   201 	// The token the client wants to talk to is encoded in the major version number
   202 	ETokenEnum token = static_cast<ETokenEnum>(aVersion.iMajor);	
   203 
   204 	// The minor version number represents the version of the protocol
   205 	if (aVersion.iMinor != KFSProtolVersion)
   206 		{
   207 		User::Leave(KErrNotSupported);
   208 		}
   209 
   210 	CTokenServerSession* result = NULL;
   211 	
   212 	switch (token)
   213 		{
   214 		case EFileKeyStore:
   215 			result = KeyStoreServerL().CreateSessionL();
   216 			break;
   217 
   218 		case EFileCertStore:
   219 			result = CertStoreServerL().CreateSessionL();
   220 			break;
   221 			
   222 		case EFileCertApps:
   223 			result = CertAppsServerL().CreateSessionL();
   224 			break;
   225 			
   226 		default:
   227 			User::Leave(KErrNotSupported);
   228 			break;
   229 		}
   230 
   231 	return result;
   232 	}
   233 
   234 // CShutdown ///////////////////////////////////////////////////////////////////
   235 
   236 inline CShutdown::CShutdown() :
   237 	CTimer(-1)
   238 	{
   239 	CActiveScheduler::Add(this);
   240 	}
   241 
   242 inline void CShutdown::ConstructL()
   243 	{
   244 	CTimer::ConstructL();
   245 	}
   246 
   247 inline void CShutdown::Start()
   248 	{
   249 	After(KServerShutdownDelay);
   250 	}
   251 
   252 /** Initiate server exit when the timer expires. */
   253 void CShutdown::RunL()
   254 	{
   255 	CActiveScheduler::Stop();
   256 	}
   257 
   258 // Server startup //////////////////////////////////////////////////////////////
   259 
   260 /**
   261  * Perform all server initialisation, in particular creation of the scheduler
   262  * and server and then run the scheduler.
   263  */
   264 static void RunServerL()
   265 	{
   266 	TPtrC serverName(KFSTokenServerName());
   267 	// Naming the server thread after the server helps to debug panics
   268 #ifdef __WINS__
   269 #ifdef SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   270 	serverName.Set(KFSNewTokenServerName());
   271 #endif // SYMBIAN_KEYSTORE_USE_AUTH_SERVER
   272 #endif // __WINS__
   273 	
   274 	User::LeaveIfError(User::RenameThread(serverName));
   275 	
   276 	// Create and install the active scheduler we need
   277 	CActiveScheduler* s=new(ELeave) CActiveScheduler;
   278 	CleanupStack::PushL(s);
   279 	CActiveScheduler::Install(s);
   280 	
   281 	// Create the server and leave it on the cleanup stack
   282 	CTokenServer::NewLC();
   283 	
   284 	// Before starting the server, notify client that initialisation is
   285 	// complete.
   286 	// (note that WINS on EKA1 uses threads since it lacks process emulation)
   287 	RProcess::Rendezvous(KErrNone);
   288 
   289 	// Ready to run
   290 	CActiveScheduler::Start();
   291 	
   292 	// Cleanup the server and scheduler
   293 	CleanupStack::PopAndDestroy(2);
   294 	}
   295 
   296 /** Server process entry point. */
   297 TInt E32Main()
   298 	{
   299 #ifdef _DEBUG
   300 	TokenServerDebug::HeapCheckStart();
   301 #endif
   302 
   303 	CTrapCleanup* cleanup=CTrapCleanup::New();
   304 	TInt r=KErrNoMemory;
   305 	if (cleanup)
   306 		{
   307 		TRAP(r,RunServerL());
   308 		delete cleanup;
   309 		}
   310 
   311 #ifdef _DEBUG
   312 	TokenServerDebug::HeapCheckEnd();
   313 #endif
   314 	return r;
   315 	}
   316 
   317 // Only for wins on EKA1 - WINS loads a DLL and starts a new thread
   318 // by calling WinsMain which does the "server" startup