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