os/security/cryptoservices/filebasedcertificateandkeystores/source/generic/client/clientsession.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 *
    16 */
    17 
    18 
    19 #include "clientsession.h"
    20 #include <e32std.h>
    21 #include <e32uid.h>
    22 #include "clientutils.h"
    23 #include "fstokenservername.h"
    24 
    25 #ifdef SYMBIAN_AUTH_SERVER
    26 
    27 #ifdef __WINS__
    28 #include <u32hal.h>
    29 #endif //__WINS__
    30 
    31 #include <e32svr.h>
    32 #endif // SYMBIAN_AUTH_SERVER
    33 
    34 //	\\	//	\\	//	\\	//	\\	//	\\	//	\\	//	\\	//	\\	//	\\	//	\\	
    35 //	Tokentype session class for file based certificate store
    36 //	Connects and passes messages to the file store tokentype server
    37 //	Coded specifically for file store token type
    38 //	\\	//	\\	//	\\	//	\\	//	\\	//	\\	//	\\	//	\\	//	\\	//	\\	
    39 
    40 _LIT(KFSTokenServerImg,"fstokenserver");
    41 #ifdef SYMBIAN_AUTH_SERVER
    42 #ifdef __WINS__
    43 _LIT(KFSNewTokenServerImg,"fstokenserver_useauth");
    44 #endif // __WINS__
    45 #endif // SYMBIAN_AUTH_SERVER
    46 
    47 RFileStoreClientSession::RFileStoreClientSession()
    48 {}
    49 
    50 TInt RFileStoreClientSession::SendRequest(TFSTokenMessages aRequest, const TIpcArgs& aArgs) const
    51 {
    52 	return SendReceive(aRequest, aArgs);
    53 }
    54 
    55 void RFileStoreClientSession::SendAsyncRequest(TFSTokenMessages aRequest, const TIpcArgs& aArgs, TRequestStatus* aStatus) const
    56 {
    57 	__ASSERT_ALWAYS(aStatus, FSTokenPanic(EBadArgument));
    58 
    59 	if (aStatus)
    60 	{
    61 		*aStatus = KRequestPending;
    62 		SendReceive(aRequest, aArgs, *aStatus);
    63 	}
    64 }
    65 
    66 
    67 //	\\	//	\\	//	\\	//	\\	//	\\	//	\\	//	\\	//	\\	//	\\	//	\\	
    68 //	Client-server startup code
    69 //	\\	//	\\	//	\\	//	\\	//	\\	//	\\	//	\\	//	\\	//	\\	//	\\	
    70 
    71 static TInt StartServer();	//	Forward declaration
    72 //
    73 // Connect to the server, attempting to start it if necessary
    74 //
    75 
    76 #ifdef SYMBIAN_AUTH_SERVER
    77 #ifdef __WINS__
    78 
    79 static bool UseAuthServer(void);
    80 
    81 #endif // SYMBIAN_AUTH_SERVER
    82 #endif // __WINS__
    83 
    84 TInt RFileStoreClientSession::Connect(ETokenEnum aToken)
    85 	{
    86 	// The version is made up of three pieces of information:
    87 	// 1. iMajor - The token we want to talk to
    88 	// 2. iMinor - The protocol version number
    89 	// 3. iBuild - unused
    90 	TVersion version(aToken, KFSProtolVersion, 0);
    91 	
    92 	TInt retry=2;
    93 	for (;;)
    94 		{
    95 		TInt err = KErrNone;
    96 #ifdef SYMBIAN_AUTH_SERVER
    97 #ifdef __WINS__
    98 		if(UseAuthServer())
    99 			{
   100 			err = CreateSession(KFSNewTokenServerName, version, 1);
   101 			}
   102 		else
   103 #endif // __WINS__
   104 #endif // SYMBIAN_AUTH_SERVER
   105 			{
   106 			err = CreateSession(KFSTokenServerName, version, 1);
   107 			}
   108 		
   109 		if (err!=KErrNotFound && err!=KErrServerTerminated)
   110 			return err;
   111 		if (--retry==0)
   112 			return err;
   113 		err=StartServer();
   114 		if (err!=KErrNone && err!=KErrAlreadyExists)
   115 			return err;
   116 		}
   117 	}
   118 
   119 #ifdef SYMBIAN_AUTH_SERVER
   120 #ifdef __WINS__
   121 
   122 /*static*/ bool UseAuthServer(void)
   123 	{
   124 	bool useAuthServer = false;
   125 	
   126 	TUint32 useAuth = 0;
   127 	// For the emulator allow the constant to be patched via epoc.ini
   128 	UserSvr::HalFunction(EHalGroupEmulator, EEmulatorHalIntProperty,
   129 	(TAny*)"KKeyStoreUseAuthServer", &useAuth); // read emulator property (if present)
   130 	if(useAuth)
   131 		{
   132 		useAuthServer = true;
   133 		}
   134 	return useAuthServer; 
   135 	}
   136 #endif // __WINS__
   137 #endif // SYMBIAN_AUTH_SERVER
   138 
   139 TInt StartServer()
   140 	{
   141 	// Server startup is different for WINS in EKA1 mode ONLY (lack of process
   142 	// emulation - we load the library in this instance
   143 	const TUidType serverUid(KNullUid, KNullUid, KUidFSTokenServer);
   144 
   145 	RProcess server;
   146 	TInt error = KErrNone;
   147 	
   148 #ifdef SYMBIAN_AUTH_SERVER
   149 #ifdef __WINS__
   150 	if(UseAuthServer())
   151 		{
   152 		error = server.Create(KFSNewTokenServerImg, KNullDesC, serverUid);
   153 		}
   154 	else
   155 #endif // __WINS__
   156 #endif // SYMBIAN_AUTH_SERVER
   157 		{
   158 		error = server.Create(KFSTokenServerImg, KNullDesC, serverUid);
   159 		}
   160 	
   161 	if (error != KErrNone)
   162 		{
   163 		return error;
   164 		}
   165 
   166 	// Synchronise with the process to make sure it hasn't died straight away
   167 	TRequestStatus stat;
   168 	server.Rendezvous(stat);
   169 	if (stat != KRequestPending)
   170 		{
   171 		// logon failed - server is not yet running, so cannot have terminated
   172 		server.Kill(0);				// Abort startup
   173 		}
   174 	else
   175 		{
   176 		// logon OK - start the server
   177 		server.Resume();
   178 		}
   179 
   180 	// Wait to synchronise with server - if it dies in the meantime, it
   181 	// also gets completed
   182 	User::WaitForRequest(stat);	
   183 
   184 	// We can't use the 'exit reason' if the server panicked as this
   185 	// is the panic 'reason' and may be '0' which cannot be distinguished
   186 	// from KErrNone
   187 	error = (server.ExitType()==EExitPanic) ? KErrGeneral : stat.Int();
   188 	server.Close();
   189 	return (error);
   190 	}
   191