eapol/eapol_framework/eapol_symbian/am/type/gsmsim/symbian/eap_am_type_gsmsim_symbian.cpp
changeset 0 c8830336c852
child 2 1c7bc153c08e
equal deleted inserted replaced
-1:000000000000 0:c8830336c852
       
     1 /*
       
     2 * Copyright (c) 2001-2006 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:  EAP and WLAN authentication protocols.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // This is enumeration of EAPOL source code.
       
    20 #if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
       
    21 	#undef EAP_FILE_NUMBER_ENUM
       
    22 	#define EAP_FILE_NUMBER_ENUM 193 
       
    23 	#undef EAP_FILE_NUMBER_DATE 
       
    24 	#define EAP_FILE_NUMBER_DATE 1127594498 
       
    25 #endif //#if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
       
    26 
       
    27 
       
    28 // INCLUDE FILES
       
    29 #include "eap_am_memory.h"
       
    30 #include "eap_tools.h"
       
    31 #include "eap_am_type_gsmsim_symbian.h"
       
    32 #include "abs_eap_am_crypto.h"
       
    33 #include "abs_eap_am_mutex.h"
       
    34 #include "eap_am_tools_symbian.h"
       
    35 #include "eap_state_notification.h"
       
    36 #include "EapSimDbDefaults.h"
       
    37 #include "EapSimDbParameterNames.h"
       
    38 #include "EapSimDbUtils.h"
       
    39 #include "eap_am_trace_symbian.h"
       
    40 
       
    41 #include <d32dbms.h>	// For DBMS
       
    42 #include <s32strm.h> 	// For RReadStream
       
    43 
       
    44 #if defined (USE_EAP_GSMSIM_INTERFACE) && !defined(__WINS__)
       
    45 
       
    46 #include "EapSimInterface.h"	
       
    47 
       
    48 #endif // End of #if defined (USE_EAP_GSMSIM_INTERFACE) && !defined(__WINS__)
       
    49 
       
    50 const TUint SIM_IMSI_LENGTH = 15u;
       
    51 const TUint KIdentityHeaderLength = 2;
       
    52 const TUint KMaxSqlQueryLength = 512;
       
    53 const TUint KMaxDBFieldNameLength = 255;
       
    54 const TInt 	KDefaultColumnInView_One = 1; // For DB view.
       
    55 const TInt 	KMicroSecsInASecond = 1000000; // 1000000 micro seconds is 1 second.
       
    56 
       
    57 #if defined(__WINS__)
       
    58 
       
    59 const char TEST_IMSI[] = "244070100000001";
       
    60 
       
    61 #endif
       
    62 
       
    63 // ================= MEMBER FUNCTIONS =======================
       
    64 
       
    65 eap_am_type_gsmsim_symbian_c::eap_am_type_gsmsim_symbian_c(
       
    66 	abs_eap_am_tools_c * const tools,
       
    67 	abs_eap_base_type_c * const partner,
       
    68 	const TIndexType aIndexType,
       
    69 	const TInt aIndex,
       
    70 	const eap_type_value_e aTunnelingType,
       
    71 	const bool aIsClient,
       
    72 	const eap_am_network_id_c * const receive_network_id)
       
    73 : eap_am_type_gsmsim_c(tools)
       
    74 , m_am_tools(static_cast<eap_am_tools_symbian_c*>(tools)) // Tools class must be of type eap_am_tools_symbian because
       
    75 														  // this is Symbian specific code.
       
    76 , m_partner(partner)
       
    77 , m_triplet_file(tools)
       
    78 , m_nai_realm(tools)
       
    79 , m_index_type(aIndexType)
       
    80 , m_index(aIndex)
       
    81 , m_tunneling_type(aTunnelingType)
       
    82 , m_is_valid(false)
       
    83 , m_is_client(aIsClient)
       
    84 , m_stored_reauth_id(tools)
       
    85 , m_stored_pseudonym(tools)
       
    86 , m_previous_imsi(tools)
       
    87 , m_stored_required_completion(eap_type_gsmsim_complete_none)
       
    88 , m_stored_received_eap_identifier(0)
       
    89 , m_shutdown_was_called(false)
       
    90 
       
    91 #if !defined (USE_EAP_GSMSIM_INTERFACE)
       
    92 , m_sim_algorithm(tools)
       
    93 #endif // #if !defined (USE_EAP_GSMSIM_INTERFACE)
       
    94 
       
    95 , m_simulator_sim_algorithm(sim_algorithm_nokia_test_network_xor)
       
    96 , m_simulator_sim_ki(tools)
       
    97 , m_copy_of_n_rands(tools)
       
    98 , m_n_kc(tools)
       
    99 , m_n_sres(tools)
       
   100 , m_uma_automatic_realm_prefix(tools)
       
   101 , m_receive_network_id(tools)
       
   102 , m_rands_handled(0)
       
   103 , m_do_rand_uniqueness_check(true)
       
   104 , m_max_session_time(0)
       
   105 {
       
   106 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   107 
       
   108 #ifdef USE_EAP_EXPANDED_TYPES
       
   109 
       
   110 	m_tunneling_vendor_type = m_tunneling_type.get_vendor_type();
       
   111 
       
   112 #else
       
   113 
       
   114 	m_tunneling_vendor_type = static_cast<TUint>(m_tunneling_type);
       
   115 
       
   116 #endif //#ifdef USE_EAP_EXPANDED_TYPES
       
   117 
       
   118 	if (receive_network_id != 0
       
   119 		&& receive_network_id->get_is_valid_data() == true)
       
   120 	{
       
   121 		eap_status_e status = m_receive_network_id.set_copy_of_network_id(
       
   122 			receive_network_id);
       
   123 		if (status != eap_status_ok)
       
   124 		{
       
   125 			EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   126 			(void)EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   127 			return;
       
   128 		}
       
   129 	}
       
   130 
       
   131 	set_is_valid();
       
   132 
       
   133 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   134 }
       
   135 
       
   136 
       
   137 //--------------------------------------------------
       
   138 
       
   139 void eap_am_type_gsmsim_symbian_c::ConstructL()
       
   140 {
       
   141 	// Open/create database
       
   142 	EapSimDbUtils::OpenDatabaseL(m_database, m_session, m_index_type, m_index, m_tunneling_type);	
       
   143 	
       
   144 	// SIM IMSI, Kc and SRES are queried from SIM using SIM interface. 
       
   145 	// If this is not used then Nokia test algorithms and test IMSI is used.
       
   146 #if defined (USE_EAP_GSMSIM_INTERFACE)	&& !defined(__WINS__)
       
   147 	if (m_is_client == true)
       
   148 	{
       
   149 		m_isa_if = CEapSimIsaInterface::NewL(m_am_tools, this);
       
   150 	} 
       
   151 	else
       
   152 	{
       
   153 		EAP_TRACE_ERROR(m_am_tools, TRACE_FLAGS_DEFAULT, (EAPL("ERROR: EAP-SIM server does not work at the moment in plugintester.\n")));
       
   154 		User::Leave(KErrNotSupported);
       
   155 	}
       
   156 #endif //#if defined (USE_EAP_GSMSIM_INTERFACE)	&& !defined(__WINS__)
       
   157 }
       
   158 
       
   159 //--------------------------------------------------
       
   160 
       
   161 eap_am_type_gsmsim_symbian_c* eap_am_type_gsmsim_symbian_c::NewL(
       
   162 	abs_eap_am_tools_c * const aTools,
       
   163 	abs_eap_base_type_c * const aPartner,
       
   164 	const TIndexType aIndexType,
       
   165 	const TInt aIndex,
       
   166 	const eap_type_value_e aTunnelingType,
       
   167 	const bool aIsClient,
       
   168 	const eap_am_network_id_c * const receive_network_id)
       
   169 {
       
   170 	eap_am_type_gsmsim_symbian_c* self = new(ELeave) eap_am_type_gsmsim_symbian_c(
       
   171 		aTools, 
       
   172 		aPartner, 
       
   173 		aIndexType, 
       
   174 		aIndex, 
       
   175 		aTunnelingType,
       
   176 		aIsClient,
       
   177 		receive_network_id);
       
   178 
       
   179 	CleanupStack::PushL(self);
       
   180 	
       
   181 	if (self->get_is_valid() != true)
       
   182 	{
       
   183 		User::Leave(KErrGeneral);
       
   184 	}
       
   185 
       
   186 	self->ConstructL();
       
   187 
       
   188 	CleanupStack::Pop();
       
   189 	return self;
       
   190 }
       
   191 
       
   192 //--------------------------------------------------
       
   193 
       
   194 EAP_FUNC_EXPORT eap_am_type_gsmsim_symbian_c::~eap_am_type_gsmsim_symbian_c()
       
   195 {
       
   196 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   197 
       
   198 	EAP_TRACE_DEBUG(
       
   199 		m_am_tools, 
       
   200 		TRACE_FLAGS_DEFAULT, 
       
   201 		(EAPL("eap_am_type_gsmsim_symbian_c::~eap_am_type_gsmsim_symbian_c(): this = 0x%08x\n"),
       
   202 		this));
       
   203 
       
   204 	m_database.Close();
       
   205 	m_session.Close();
       
   206 
       
   207 #if defined(USE_EAP_GSMSIM_INTERFACE) && !defined(__WINS__)
       
   208 	if (m_is_client == true)
       
   209 	{
       
   210 		delete m_isa_if;
       
   211 	}
       
   212 #endif //#if defined(USE_EAP_GSMSIM_INTERFACE) && !defined(__WINS__)
       
   213 
       
   214 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   215 }
       
   216 
       
   217 //--------------------------------------------------
       
   218 
       
   219 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::store_reauth_parameters(
       
   220 	const eap_variable_data_c * const XKEY,
       
   221 	const eap_variable_data_c * const K_aut,
       
   222 	const eap_variable_data_c * const K_encr,
       
   223 	const u32_t reauth_counter)
       
   224 {	
       
   225 	// These kind of wrapper functions are needed because the Symbian L-functions must be executed in 
       
   226 	// trap harness.
       
   227 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   228 	
       
   229 	// Send error if any of the parameters are invalid.
       
   230 	if(XKEY == NULL 
       
   231 		|| K_aut == NULL
       
   232 		|| K_encr == NULL)
       
   233 	{
       
   234 		// Some of the parameters are invalid.
       
   235 		
       
   236 		EAP_TRACE_DEBUG(
       
   237 			m_am_tools,
       
   238 			TRACE_FLAGS_DEFAULT,
       
   239 			(EAPL("eap_am_type_gsmsim_symbian_c::store_reauth_parameters: ")
       
   240 			 EAPL("ERROR: Invalid parameters\n")));
       
   241 		
       
   242 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   243 		return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter);
       
   244 	}
       
   245 	
       
   246 	// Send error if any of the parameters are too long.
       
   247 	if(XKEY->get_data_length() > KMaxXKeyLengthInDB 
       
   248 		|| K_aut->get_data_length() > KMaxK_autLengthInDB 
       
   249 		|| K_encr->get_data_length() > KMaxK_encrLengthInDB )
       
   250 	{
       
   251 		// Some of the parameters are too long. Can't store them in DB.
       
   252 		
       
   253 		EAP_TRACE_DEBUG(
       
   254 			m_am_tools,
       
   255 			TRACE_FLAGS_DEFAULT,
       
   256 			(EAPL("eap_am_type_gsmsim_symbian_c::store_reauth_parameters: ")
       
   257 			 EAPL("Too long parameters. Length: XKEY=%d, K_aut=%d, K_encr=%d\n"),
       
   258 			 XKEY->get_data_length(), K_aut->get_data_length(), K_encr->get_data_length()));
       
   259 		
       
   260 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   261 		return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter);
       
   262 	}
       
   263 	
       
   264 	eap_status_e status(eap_status_ok);
       
   265 	
       
   266 	TRAPD(err, store_reauth_parametersL(XKEY, K_aut, K_encr, reauth_counter));
       
   267 	if (err != KErrNone)
       
   268 	{
       
   269 		// Convert the leave error code to EAPOL stack error code.
       
   270 		status = m_am_tools->convert_am_error_to_eapol_error(err);
       
   271 		send_error_notification(status);
       
   272 	} 	
       
   273 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   274 	return EAP_STATUS_RETURN(m_am_tools, status);
       
   275 }
       
   276 
       
   277 void eap_am_type_gsmsim_symbian_c::store_reauth_parametersL(
       
   278 	const eap_variable_data_c * const XKEY,
       
   279 	const eap_variable_data_c * const K_aut,
       
   280 	const eap_variable_data_c * const K_encr,
       
   281 	const u32_t reauth_counter)
       
   282 {	
       
   283 	// The _L functions do the actual thing.
       
   284 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   285 
       
   286 	// Form the insertion command
       
   287 	HBufC* buf = HBufC::NewLC(KMaxSqlQueryLength);
       
   288 	TPtr sqlStatement = buf->Des();
       
   289 
       
   290 	_LIT(KSQLInsert, "SELECT %S, %S, %S, %S FROM %S WHERE %S=%d AND %S=%d AND %S=%d");
       
   291 	sqlStatement.Format(KSQLInsert, &KXKey, &KK_aut, &KK_encr, &KReauthCounter, &KSimTableName, 
       
   292 		&KServiceType, m_index_type, &KServiceIndex, m_index, &KTunnelingType, m_tunneling_vendor_type);
       
   293 		
       
   294 	// Evaluate view
       
   295 	RDbView view;
       
   296 	User::LeaveIfError(view.Prepare(m_database,TDbQuery(sqlStatement), TDbWindow::EUnlimited));
       
   297 	CleanupClosePushL(view);
       
   298 	
       
   299 	User::LeaveIfError(view.EvaluateAll());
       
   300 	
       
   301 	// Create pointers to the data
       
   302 	TPtrC8 key(XKEY->get_data(XKEY->get_data_length()), XKEY->get_data_length());
       
   303 	TPtrC8 aut(K_aut->get_data(K_aut->get_data_length()), K_aut->get_data_length());
       
   304 	TPtrC8 enc(K_encr->get_data(K_encr->get_data_length()), K_encr->get_data_length());
       
   305 	
       
   306 	// Update the columns in the database
       
   307 	view.FirstL();	
       
   308 	view.UpdateL();
       
   309 	
       
   310 	// Get column set so we get the correct column numbers
       
   311 	CDbColSet* colSet = view.ColSetL();
       
   312 	CleanupStack::PushL(colSet);	
       
   313 	
       
   314 	EAP_TRACE_DATA_DEBUG(
       
   315 		m_am_tools,
       
   316 		TRACE_FLAGS_DEFAULT,
       
   317 		(EAPL("XKEY to DB"), key.Ptr(),	key.Length()));
       
   318 
       
   319 	EAP_TRACE_DATA_DEBUG(
       
   320 		m_am_tools,
       
   321 		TRACE_FLAGS_DEFAULT,
       
   322 		(EAPL("K_aut to DB"), aut.Ptr(),	aut.Length()));
       
   323 
       
   324 	EAP_TRACE_DATA_DEBUG(
       
   325 		m_am_tools,
       
   326 		TRACE_FLAGS_DEFAULT,
       
   327 		(EAPL("K_encr to DB"), enc.Ptr(),	enc.Length()));
       
   328 	
       
   329 	view.SetColL(colSet->ColNo(KXKey), key); // XKEY
       
   330 	view.SetColL(colSet->ColNo(KK_aut), aut); // K_aut
       
   331 	view.SetColL(colSet->ColNo(KK_encr), enc); // K_encr
       
   332 	view.SetColL(colSet->ColNo(KReauthCounter), reauth_counter); // ReauthCounter
       
   333 	
       
   334 	CleanupStack::PopAndDestroy(colSet); // Delete colSet.	
       
   335 
       
   336 	view.PutL();
       
   337 
       
   338 	EAP_TRACE_DEBUG(
       
   339 		m_am_tools,
       
   340 		TRACE_FLAGS_DEFAULT,
       
   341 		(EAPL("eap_am_type_gsmsim_symbian_c::store_reauth_keys_L(): %s, m_saved_reauth_counter %d.\n"),
       
   342 		 (m_is_client == true ? "client": "server"),
       
   343 		 reauth_counter));
       
   344 
       
   345 	// Close database
       
   346 	CleanupStack::PopAndDestroy(2); // view, buf
       
   347 
       
   348 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   349 }
       
   350 
       
   351 
       
   352 //--------------------------------------------------
       
   353 
       
   354 eap_status_e eap_am_type_gsmsim_symbian_c::authentication_finished(
       
   355 	const bool true_when_successful,
       
   356 	const eap_gsmsim_authentication_type_e authentication_type,
       
   357 	const eap_type_gsmsim_identity_type identity_type)
       
   358 {
       
   359 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   360 
       
   361 	eap_status_e status(eap_status_ok);
       
   362 	TInt err(KErrNone);	
       
   363 	
       
   364 	// Store the authentication time if the full authentication is successful
       
   365 	if (true_when_successful == true
       
   366 		&& authentication_type == GSMSIM_AUTHENTICATION_TYPE_FULL_AUTH)
       
   367 	{
       
   368 		TRAP(err,store_authentication_timeL());
       
   369 		if (err != KErrNone)
       
   370 		{
       
   371 			status = m_am_tools->convert_am_error_to_eapol_error(err);
       
   372 			send_error_notification(status);
       
   373 		}
       
   374 	}	
       
   375 
       
   376 	if (true_when_successful == false)
       
   377 	{
       
   378 		if( identity_type == GSMSIM_IDENTITY_TYPE_IMSI_ID 
       
   379 			|| identity_type == GSMSIM_IDENTITY_TYPE_PSEUDONYM_ID )
       
   380 		{
       
   381 			(void) store_pseudonym_id(0,0);
       
   382 		}
       
   383 		
       
   384 		if( identity_type == GSMSIM_IDENTITY_TYPE_IMSI_ID 
       
   385 			|| identity_type == GSMSIM_IDENTITY_TYPE_RE_AUTH_ID )
       
   386 		{
       
   387 			(void) store_reauthentication_id(0,0);			
       
   388 		}
       
   389 	}
       
   390 
       
   391 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   392 	return EAP_STATUS_RETURN(m_am_tools, status);
       
   393 }
       
   394 
       
   395 //--------------------------------------------------
       
   396 
       
   397 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::query_reauth_parameters(
       
   398 	eap_variable_data_c * const reauth_XKEY,
       
   399 	eap_variable_data_c * const reauth_K_aut,
       
   400 	eap_variable_data_c * const reauth_K_encr,
       
   401 	u32_t * const reauth_counter)
       
   402 {
       
   403 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);	
       
   404 	eap_status_e status(eap_status_ok);
       
   405 	TRAPD(err, query_reauth_parametersL(reauth_XKEY, reauth_K_aut, reauth_K_encr, reauth_counter));
       
   406 	if (err != KErrNone)
       
   407 	{
       
   408 		status = m_am_tools->convert_am_error_to_eapol_error(err);
       
   409 		send_error_notification(status);
       
   410 	} 
       
   411 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   412 	return EAP_STATUS_RETURN(m_am_tools, status);
       
   413 }
       
   414 
       
   415 void eap_am_type_gsmsim_symbian_c::query_reauth_parametersL(
       
   416 	eap_variable_data_c * const reauth_XKEY,
       
   417 	eap_variable_data_c * const reauth_K_aut,
       
   418 	eap_variable_data_c * const reauth_K_encr,
       
   419 	u32_t * const reauth_counter)
       
   420 {
       
   421 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   422 
       
   423 	HBufC* buf = HBufC::NewLC(KMaxSqlQueryLength);
       
   424 	TPtr sqlStatement = buf->Des();
       
   425 
       
   426 	// Form the query
       
   427 	_LIT(KSQLQuery, "SELECT %S, %S, %S, %S FROM %S WHERE %S=%d AND %S=%d AND %S=%d");
       
   428 	sqlStatement.Format(KSQLQuery, &KXKey, &KK_aut, &KK_encr, &KReauthCounter, &KSimTableName, 
       
   429 		&KServiceType, m_index_type, &KServiceIndex, m_index, &KTunnelingType, m_tunneling_vendor_type);
       
   430 		
       
   431 	RDbView view;
       
   432 	// Evaluate view
       
   433 	User::LeaveIfError(view.Prepare(m_database, TDbQuery(sqlStatement)));
       
   434 	CleanupClosePushL(view);
       
   435 	
       
   436 	User::LeaveIfError(view.EvaluateAll());	
       
   437 	
       
   438 	// Get the first (and only) row
       
   439 	view.FirstL();
       
   440 	view.GetL();
       
   441 	
       
   442 	// Get column set so we get the correct column numbers
       
   443 	CDbColSet* colSet = view.ColSetL();
       
   444 	CleanupStack::PushL(colSet);	
       
   445 
       
   446 	// Get the values from DB
       
   447 	TPtrC8 key = view.ColDes8(colSet->ColNo(KXKey));	
       
   448 	TPtrC8 aut = view.ColDes8(colSet->ColNo(KK_aut));
       
   449 	TPtrC8 enc = view.ColDes8(colSet->ColNo(KK_encr));
       
   450 
       
   451 	EAP_TRACE_DATA_DEBUG(
       
   452 		m_am_tools,
       
   453 		TRACE_FLAGS_DEFAULT,
       
   454 		(EAPL("XKEY from DB"), key.Ptr(),	key.Length()));
       
   455 
       
   456 	EAP_TRACE_DATA_DEBUG(
       
   457 		m_am_tools,
       
   458 		TRACE_FLAGS_DEFAULT,
       
   459 		(EAPL("K_aut from DB"), aut.Ptr(),	aut.Length()));
       
   460 
       
   461 	EAP_TRACE_DATA_DEBUG(
       
   462 		m_am_tools,
       
   463 		TRACE_FLAGS_DEFAULT,
       
   464 		(EAPL("K_encr from DB"), enc.Ptr(),	enc.Length()));
       
   465 					
       
   466 	eap_status_e status(eap_status_process_general_error);
       
   467 	
       
   468 	// Store the values to the supplied buffers
       
   469 	status = reauth_XKEY->set_copy_of_buffer(key.Ptr(),key.Length());
       
   470 	if (status != eap_status_ok)
       
   471 	{
       
   472 		User::Leave(
       
   473 			m_am_tools->convert_eapol_error_to_am_error(
       
   474 				EAP_STATUS_RETURN(m_am_tools, status)));
       
   475 	}
       
   476 
       
   477 	status = reauth_K_aut->set_copy_of_buffer(aut.Ptr(),aut.Length());
       
   478 	if (status != eap_status_ok)
       
   479 	{
       
   480 		User::Leave(
       
   481 			m_am_tools->convert_eapol_error_to_am_error(
       
   482 				EAP_STATUS_RETURN(m_am_tools, status)));
       
   483 	}
       
   484 
       
   485 	status = reauth_K_encr->set_copy_of_buffer(enc.Ptr(),enc.Length());
       
   486 	if (status != eap_status_ok)
       
   487 	{
       
   488 		User::Leave(
       
   489 			m_am_tools->convert_eapol_error_to_am_error(
       
   490 				EAP_STATUS_RETURN(m_am_tools, status)));
       
   491 	}
       
   492 	*reauth_counter = view.ColUint(colSet->ColNo(KReauthCounter));
       
   493 
       
   494 	CleanupStack::PopAndDestroy(colSet); // Delete colSet.
       
   495 
       
   496 	EAP_TRACE_DEBUG(
       
   497 		m_am_tools,
       
   498 		TRACE_FLAGS_DEFAULT,
       
   499 		(EAPL("eap_am_type_gsmsim_symbian_c::query_reauth_parametersL(): %s, m_saved_reauth_counter %d.\n"),
       
   500 		 (m_is_client == true ? "client": "server"),
       
   501 		 *reauth_counter));
       
   502 
       
   503 	// Close database
       
   504 	CleanupStack::PopAndDestroy(2); // view, buf
       
   505 
       
   506 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   507 }
       
   508 
       
   509 //--------------------------------------------------
       
   510 
       
   511 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::increase_reauth_counter()
       
   512 {
       
   513 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   514 	eap_status_e status(eap_status_ok);
       
   515 	TRAPD(err, increase_reauth_counterL());
       
   516 	if (err != KErrNone)
       
   517 	{
       
   518 		status = m_am_tools->convert_am_error_to_eapol_error(err);
       
   519 		send_error_notification(status);
       
   520 	} 		
       
   521 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   522 	return EAP_STATUS_RETURN(m_am_tools, status);
       
   523 }
       
   524 
       
   525 void eap_am_type_gsmsim_symbian_c::increase_reauth_counterL()
       
   526 {
       
   527 	// Form the insertion command
       
   528 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   529 		
       
   530 	HBufC* buf = HBufC::NewLC(KMaxSqlQueryLength);
       
   531 	TPtr sqlStatement = buf->Des();
       
   532 
       
   533 	_LIT(KSQLInsert, "SELECT %S FROM %S WHERE %S=%d AND %S=%d AND %S=%d");
       
   534 	sqlStatement.Format(KSQLInsert, &KReauthCounter, &KSimTableName, 
       
   535 		&KServiceType, m_index_type, &KServiceIndex, m_index, &KTunnelingType, m_tunneling_vendor_type);
       
   536 		
       
   537 	// Evaluate view
       
   538 	RDbView view;
       
   539 	User::LeaveIfError(view.Prepare(m_database,TDbQuery(sqlStatement), TDbWindow::EUnlimited));
       
   540 	CleanupClosePushL(view);
       
   541 	
       
   542 	User::LeaveIfError(view.EvaluateAll());
       
   543 	
       
   544 	view.FirstL();
       
   545 	view.GetL();
       
   546 	
       
   547 	// Get column set so we get the correct column numbers
       
   548 	CDbColSet* colSet = view.ColSetL();
       
   549 	CleanupStack::PushL(colSet);	
       
   550 	
       
   551 	// Get the previous value
       
   552 	TUint counter = view.ColUint(colSet->ColNo(KReauthCounter));
       
   553 
       
   554 	view.UpdateL();
       
   555 	
       
   556 	// Increment the value
       
   557 	view.SetColL(colSet->ColNo(KReauthCounter), counter + 1); 
       
   558 
       
   559 	counter = view.ColUint(colSet->ColNo(KReauthCounter));
       
   560 	
       
   561 	CleanupStack::PopAndDestroy(colSet); // Delete colSet.
       
   562 	
       
   563 	EAP_TRACE_DEBUG(
       
   564 		m_am_tools,
       
   565 		TRACE_FLAGS_DEFAULT,
       
   566 		(EAPL("eap_am_type_gsmsim_symbian_c::increase_reauth_counterL(): %s, m_saved_reauth_counter %d.\n"),
       
   567 		 (m_is_client == true ? "client": "server"),
       
   568 		 counter));
       
   569 
       
   570 	view.PutL();
       
   571 
       
   572 	// Close database
       
   573 	CleanupStack::PopAndDestroy(2); // view, buf
       
   574 
       
   575 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   576 }
       
   577 
       
   578 //--------------------------------------------------
       
   579 
       
   580 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::configure()
       
   581 {
       
   582 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   583 
       
   584 #if defined(_DEBUG) && defined(USE_EAP_GSMSIM_TEST_VECTORS)
       
   585 
       
   586 	{
       
   587 		// Read triplet file name
       
   588 		eap_status_e status = type_configure_read(
       
   589 			cf_str_EAP_GSMSIM_triplet_file.get_field(),
       
   590 			&m_triplet_file);
       
   591 		if (status != eap_status_ok
       
   592 			&& status != eap_status_illegal_configure_field)
       
   593 		{
       
   594 			EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   595 			return EAP_STATUS_RETURN(m_am_tools, status);
       
   596 		}
       
   597 		// This is optional value. Don't care even if the configure fails.
       
   598 	}
       
   599 
       
   600 #endif //#if defined(_DEBUG) && defined(USE_EAP_GSMSIM_TEST_VECTORS)
       
   601 
       
   602 	{
       
   603 		eap_status_e status = type_configure_read(
       
   604 			cf_str_EAP_GSMSIM_manual_realm.get_field(),
       
   605 			&m_nai_realm);
       
   606 		if (status != eap_status_ok)
       
   607 		{
       
   608 			EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   609 			return EAP_STATUS_RETURN(m_am_tools, status);
       
   610 		}
       
   611 		if (m_nai_realm.get_is_valid_data() == false)
       
   612 		{
       
   613 			// We should really generate a NAI here but it's too much work.
       
   614 			// So for testing purposes so we'll just use 3gppnetwork.org
       
   615 			
       
   616 			_LIT8(KDefaultRealm, "3gppnetwork.org");
       
   617 			TBuf8<16> tmp(KDefaultRealm);
       
   618 			status = m_nai_realm.set_copy_of_buffer(tmp.Ptr(), tmp.Length());			
       
   619 			if (status != eap_status_ok)
       
   620 			{
       
   621 				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   622 				return EAP_STATUS_RETURN(m_am_tools, status);
       
   623 			}
       
   624 		}
       
   625 	}
       
   626 
       
   627 #if !defined (USE_EAP_GSMSIM_INTERFACE)
       
   628 // Needed only for test environment.
       
   629 
       
   630 	{
       
   631 		eap_variable_data_c simulator_sim_algorithm(m_am_tools);
       
   632 
       
   633 		eap_status_e status = m_partner->read_configure(
       
   634 			cf_str_EAP_GSMSIM_simulator_sim_algorithm.get_field(),
       
   635 			&simulator_sim_algorithm);
       
   636 		if (status == eap_status_ok
       
   637 			&& simulator_sim_algorithm.get_is_valid_data() == true
       
   638 			&& simulator_sim_algorithm.get_data_length() > 0ul
       
   639 			&& simulator_sim_algorithm.get_data(
       
   640 				simulator_sim_algorithm.get_data_length()) != 0)
       
   641 		{
       
   642 			if (cf_str_EAP_GSMSIM_simulator_sim_algorithm_config_value_nokia_test_network_xor
       
   643 				.get_field()->compare(
       
   644 					m_am_tools,
       
   645 					&simulator_sim_algorithm) == true)
       
   646 			{
       
   647 				m_simulator_sim_algorithm = sim_algorithm_nokia_test_network_xor;
       
   648 			}
       
   649 			else if (cf_str_EAP_GSMSIM_simulator_sim_algorithm_config_value_tls_prf_with_shared_secret
       
   650 					 .get_field()->compare(
       
   651 						 m_am_tools,
       
   652 						 &simulator_sim_algorithm) == true)
       
   653 			{
       
   654 				m_simulator_sim_algorithm = sim_algorithm_tls_prf_with_shared_secret;
       
   655 			}
       
   656 			else
       
   657 			{
       
   658 				EAP_TRACE_DEBUG(
       
   659 					m_am_tools,
       
   660 					TRACE_FLAGS_DEFAULT,
       
   661 					(EAPL("ERROR: illegal configuration value %s\n"),
       
   662 					 cf_str_EAP_GSMSIM_simulator_sim_algorithm.get_field()->get_field()));
       
   663 				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   664 				return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_configure_field);
       
   665 			}
       
   666 		}
       
   667 	}
       
   668 	
       
   669 	{
       
   670 		eap_status_e status = m_partner->read_configure(
       
   671 			cf_str_EAP_GSMSIM_simulator_sim_ki.get_field(),
       
   672 			&m_simulator_sim_ki);
       
   673 		if (status == eap_status_ok
       
   674 			&& m_simulator_sim_ki.get_is_valid_data() == true
       
   675 			&& m_simulator_sim_ki.get_data_length() > 0ul
       
   676 			&& m_simulator_sim_ki.get_data(
       
   677 				m_simulator_sim_ki.get_data_length()) != 0)
       
   678 		{
       
   679 			// OK.
       
   680 			eap_status_e status = m_sim_algorithm.set_simulator_sim_ki(&m_simulator_sim_ki);
       
   681 			if (status != eap_status_ok)
       
   682 			{
       
   683 				return EAP_STATUS_RETURN(m_am_tools, status);
       
   684 			}
       
   685 		}		
       
   686 	}
       
   687 #endif // #if !defined (USE_EAP_GSMSIM_INTERFACE)
       
   688 
       
   689 
       
   690 #if defined(USE_EAP_SIM_RESET_RAND_DATABASES)
       
   691 	{
       
   692 		eap_variable_data_c do_rand_uniqueness_check(m_am_tools);
       
   693 
       
   694 		eap_status_e status = type_configure_read(
       
   695 			cf_str_EAP_GSMSIM_do_rand_uniqueness_check.get_field(),
       
   696 			&do_rand_uniqueness_check);
       
   697 
       
   698 		if (status == eap_status_ok
       
   699 			&& do_rand_uniqueness_check.get_is_valid_data() == true
       
   700 			&& do_rand_uniqueness_check.get_data_length() == sizeof(u32_t))
       
   701 		{
       
   702 			u32_t *flag = reinterpret_cast<u32_t *>(do_rand_uniqueness_check.get_data());
       
   703 			if (flag != 0)
       
   704 			{
       
   705 				if (*flag == 0)
       
   706 				{
       
   707 					m_do_rand_uniqueness_check = false;
       
   708 					(void) reset_rand_check();
       
   709 				}
       
   710 			}
       
   711 		}
       
   712 	}
       
   713 #endif //#if defined(USE_EAP_SIM_RESET_RAND_DATABASES)
       
   714 
       
   715 	{
       
   716 		// Read Maximum Session Validity Time from the config file
       
   717 		eap_variable_data_c sessionTimeFromFile(m_am_tools);
       
   718 		
       
   719 		eap_status_e status = m_partner->read_configure(
       
   720 			cf_str_EAP_GSMSIM_max_session_validity_time.get_field(),
       
   721 			&sessionTimeFromFile);
       
   722 		
       
   723 		if (status == eap_status_ok
       
   724 			&& sessionTimeFromFile.get_is_valid_data() == true
       
   725 			&& sessionTimeFromFile.get_data_length() == sizeof(u32_t))
       
   726 		{
       
   727 			u32_t *session = reinterpret_cast<u32_t *>(sessionTimeFromFile.get_data());
       
   728 			if (session != 0)
       
   729 			{
       
   730 				// Update the max session time (in micro seconds).
       
   731 				// configuration file saves the time in seconds. We have to convert it to micro seconds.
       
   732 				m_max_session_time = static_cast<TInt64>(*session) * static_cast<TInt64>(KMicroSecsInASecond);
       
   733 			}
       
   734 		}		
       
   735 	}
       
   736 
       
   737 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   738 	return eap_status_ok;
       
   739 }
       
   740 
       
   741 //--------------------------------------------------
       
   742 
       
   743 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::reset()
       
   744 {
       
   745 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   746 
       
   747 	m_rands_handled = 0ul;
       
   748 
       
   749 #if defined(USE_EAP_SIM_RESET_RAND_DATABASES)
       
   750 	if (m_do_rand_uniqueness_check == false)
       
   751 	{
       
   752 		(void) reset_rand_check();
       
   753 	}
       
   754 #endif //#if defined(USE_EAP_SIM_RESET_RAND_DATABASES)
       
   755 	
       
   756 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   757 	return eap_status_ok;
       
   758 }
       
   759 
       
   760 //--------------------------------------------------
       
   761 
       
   762 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::shutdown()
       
   763 {
       
   764 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   765 
       
   766 	EAP_TRACE_DEBUG(
       
   767 		m_am_tools,
       
   768 		TRACE_FLAGS_DEFAULT,
       
   769 		(EAPL("%s: eap_am_type_gsmsim_symbian_c::shutdown(), m_shutdown_was_called=%d\n"),
       
   770 		(m_is_client == true) ? "client": "server",
       
   771 		m_shutdown_was_called));
       
   772 
       
   773 	m_shutdown_was_called = true;
       
   774 
       
   775 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   776 	return eap_status_ok;
       
   777 }
       
   778 
       
   779 //--------------------------------------------------
       
   780 
       
   781 #if defined(USE_EAP_SIM_RESET_RAND_DATABASES)
       
   782 
       
   783 eap_status_e eap_am_type_gsmsim_symbian_c::reset_rand_check()
       
   784 {
       
   785 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   786 
       
   787 	RFs fs;
       
   788 
       
   789 	TInt error = fs.Connect();
       
   790 	if (error != KErrNone)
       
   791 	{
       
   792 		eap_status_e status(m_am_tools->convert_am_error_to_eapol_error(error));
       
   793 
       
   794 		send_error_notification(status);
       
   795 
       
   796 		EAP_TRACE_DEBUG(
       
   797 			m_am_tools,
       
   798 			TRACE_FLAGS_DEFAULT,
       
   799 			(EAPL("RFs::Connect() failed %d.\n"),
       
   800 			status));
       
   801 		(void) EAP_STATUS_RETURN(m_am_tools, status);
       
   802 	}
       
   803 	else
       
   804 	{
       
   805 		// Remove RAND check databases.
       
   806 		_LIT(KRandDb,		"C:\\private\\101f7989\\rand_db.cur");
       
   807 		_LIT(KRandDbPrev,	"C:\\private\\101f7989\\rand_db.prv");
       
   808 
       
   809 		TInt err = fs.Delete(KRandDb);
       
   810 		(void) EAP_STATUS_RETURN(m_am_tools, m_am_tools->convert_am_error_to_eapol_error(err));
       
   811 
       
   812 		err = fs.Delete(KRandDbPrev);
       
   813 		(void) EAP_STATUS_RETURN(m_am_tools, m_am_tools->convert_am_error_to_eapol_error(err));
       
   814 
       
   815 		fs.Close();
       
   816 	}
       
   817 
       
   818 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   819 	return eap_status_ok;
       
   820 }
       
   821 
       
   822 #endif //#if defined(USE_EAP_SIM_RESET_RAND_DATABASES)
       
   823 
       
   824 //--------------------------------------------------
       
   825 
       
   826 #if defined(__WINS__)
       
   827 
       
   828 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::query_SIM_imsi(
       
   829 	u8_t * const imsi, const u32_t max_length, u32_t * const imsi_length)
       
   830 {
       
   831 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   832 
       
   833 	if (imsi == 0 || imsi_length == 0)
       
   834 	{
       
   835 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   836 		return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter);
       
   837 	}
       
   838 
       
   839 	// This function returns Nokia test SIM IMSI.
       
   840 	// It is used only when real ISA interface is not used.	
       
   841 
       
   842 	*imsi_length = m_am_tools->strlen(TEST_IMSI);
       
   843 	if (*imsi_length > max_length)
       
   844 	{
       
   845 		// ERROR, too short buffer.
       
   846 		*imsi_length = 0u;
       
   847 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   848 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   849 	}
       
   850 	m_am_tools->memmove(imsi, TEST_IMSI, m_am_tools->strlen(TEST_IMSI));
       
   851 
       
   852 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   853 	return eap_status_ok;
       
   854 }
       
   855 
       
   856 #endif //#if defined(__WINS__)
       
   857 
       
   858 //--------------------------------------------------
       
   859 
       
   860 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::store_pseudonym_id(
       
   861 	const eap_am_network_id_c * const network_id,
       
   862 	const eap_variable_data_c * const pseudonym)
       
   863 {
       
   864 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);	
       
   865 		
       
   866 	// Send error if the pseudonym is too long.
       
   867 	if(pseudonym != NULL && pseudonym->get_data_length() > KMaxPseudonymIdLengthInDB)
       
   868 	{
       
   869 		// Pseudonym too long. Can't store this in DB.
       
   870 		
       
   871 		EAP_TRACE_DEBUG(
       
   872 			m_am_tools,
       
   873 			TRACE_FLAGS_DEFAULT,
       
   874 			(EAPL("eap_am_type_gsmsim_symbian_c::store_pseudonym_id: ")
       
   875 			 EAPL("Too long Pseudonym. Length=%d\n"),
       
   876 			 pseudonym->get_data_length()));
       
   877 		
       
   878 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   879 		return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter);
       
   880 	}
       
   881 	
       
   882 	eap_status_e status(eap_status_ok);
       
   883 	
       
   884 	TRAPD(err, store_pseudonym_idL(network_id, pseudonym));
       
   885 	if (err != KErrNone)
       
   886 	{
       
   887 		status = m_am_tools->convert_am_error_to_eapol_error(err);
       
   888 		send_error_notification(status);
       
   889 	} 
       
   890 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   891 	return EAP_STATUS_RETURN(m_am_tools, status);
       
   892 }
       
   893 
       
   894 void eap_am_type_gsmsim_symbian_c::store_pseudonym_idL(
       
   895 	const eap_am_network_id_c * const /*network_id*/,
       
   896 	const eap_variable_data_c * const pseudonym)
       
   897 {
       
   898 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   899 
       
   900 	// Form the insertion command
       
   901 	HBufC* buf = HBufC::NewLC(KMaxSqlQueryLength);
       
   902 	TPtr sqlStatement = buf->Des();
       
   903 
       
   904 	_LIT(KSQLInsert, "SELECT %S FROM %S WHERE %S=%d AND %S=%d AND %S=%d");
       
   905 	sqlStatement.Format(KSQLInsert, &KPseudonymId, &KSimTableName, 
       
   906 		&KServiceType, m_index_type, &KServiceIndex, m_index, &KTunnelingType, m_tunneling_vendor_type);
       
   907 
       
   908 	// Evaluate view
       
   909 	RDbView view;
       
   910 	User::LeaveIfError(view.Prepare(m_database,TDbQuery(sqlStatement), TDbWindow::EUnlimited));
       
   911 	CleanupClosePushL(view);
       
   912 	
       
   913 	User::LeaveIfError(view.EvaluateAll());
       
   914 	
       
   915 	// Update the columns in the database
       
   916 	view.FirstL();
       
   917 	view.UpdateL();	
       
   918 	
       
   919 	// Get column set so we get the correct column numbers
       
   920 	CDbColSet* colSet = view.ColSetL();		
       
   921 	CleanupStack::PushL(colSet);
       
   922 	
       
   923 	// Create pointer to the data
       
   924 	if ((pseudonym != NULL) 
       
   925 		&& (pseudonym->get_is_valid_data() == true))
       
   926 	{		
       
   927 		TPtrC8 pseudonymId(
       
   928 			pseudonym->get_data(pseudonym->get_data_length()),
       
   929 			pseudonym->get_data_length());
       
   930 		
       
   931 		EAP_TRACE_DATA_DEBUG(
       
   932 			m_am_tools,
       
   933 			TRACE_FLAGS_DEFAULT,
       
   934 			(EAPL("pseudonymId to DB"),	pseudonymId.Ptr(), pseudonymId.Length()));
       
   935 		
       
   936 		view.SetColL(colSet->ColNo(KPseudonymId), pseudonymId);
       
   937 	} 
       
   938 	else 
       
   939 	{
       
   940 		// If passed pseudonym was 0 that means the field must be cleared.
       
   941 		view.SetColNullL(colSet->ColNo(KPseudonymId));
       
   942 	}
       
   943 		
       
   944 	CleanupStack::PopAndDestroy(colSet); // Delete colSet.	
       
   945 		
       
   946 	view.PutL();
       
   947 	
       
   948 	// Close database
       
   949 	CleanupStack::PopAndDestroy(2); // view, buf
       
   950 
       
   951 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   952 }
       
   953 
       
   954 //--------------------------------------------------
       
   955 
       
   956 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::store_reauthentication_id(
       
   957 	const eap_am_network_id_c * const network_id,
       
   958 	const eap_variable_data_c * const reauthentication_id)
       
   959 {
       
   960 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);	
       
   961 	
       
   962 	// Send error if the pseudonym is too long.
       
   963 	if(reauthentication_id != NULL 
       
   964 		&& reauthentication_id->get_data_length() > KMaxReauthIdLengthInDB)
       
   965 	{
       
   966 		// Reauth id too long. Can't store this in DB.
       
   967 		
       
   968 		EAP_TRACE_DEBUG(
       
   969 			m_am_tools,
       
   970 			TRACE_FLAGS_DEFAULT,
       
   971 			(EAPL("eap_am_type_gsmsim_symbian_c::store_reauthentication_id: ")
       
   972 			 EAPL("Too long reauth id. Length=%d\n"),
       
   973 			 reauthentication_id->get_data_length()));
       
   974 		
       
   975 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   976 		return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter);
       
   977 	}
       
   978 	
       
   979 	eap_status_e status(eap_status_ok);
       
   980 	
       
   981 	TRAPD(err, store_reauthentication_idL(network_id, reauthentication_id));
       
   982 	if (err != KErrNone)
       
   983 	{
       
   984 		status = m_am_tools->convert_am_error_to_eapol_error(err);
       
   985 		send_error_notification(status);
       
   986 	} 
       
   987 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   988 	return EAP_STATUS_RETURN(m_am_tools, status);
       
   989 }
       
   990 
       
   991 void eap_am_type_gsmsim_symbian_c::store_reauthentication_idL(
       
   992 	const eap_am_network_id_c * const /*network_id*/,
       
   993 	const eap_variable_data_c * const reauthentication_id)
       
   994 {
       
   995 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
   996 
       
   997 	// Form the insertion command
       
   998 	HBufC* buf = HBufC::NewLC(KMaxSqlQueryLength);
       
   999 	TPtr sqlStatement = buf->Des();
       
  1000 
       
  1001 	_LIT(KSQLInsert, "SELECT %S FROM %S WHERE %S=%d AND %S=%d AND %S=%d");
       
  1002 	sqlStatement.Format(KSQLInsert, &KReauthId, &KSimTableName, 
       
  1003 		&KServiceType, m_index_type, &KServiceIndex, m_index, &KTunnelingType, m_tunneling_vendor_type);
       
  1004 		
       
  1005 	// Evaluate view
       
  1006 	RDbView view;
       
  1007 	User::LeaveIfError(view.Prepare(m_database,TDbQuery(sqlStatement), TDbWindow::EUnlimited));
       
  1008 	CleanupClosePushL(view);
       
  1009 	
       
  1010 	User::LeaveIfError(view.EvaluateAll());
       
  1011 	
       
  1012 	view.FirstL();
       
  1013 	view.UpdateL();
       
  1014 	
       
  1015 	// Get column set so we get the correct column numbers
       
  1016 	CDbColSet* colSet = view.ColSetL();
       
  1017 	CleanupStack::PushL(colSet);		
       
  1018 
       
  1019 	// Create pointer to the data
       
  1020 	if ((reauthentication_id != NULL) 
       
  1021 		&& (reauthentication_id->get_is_valid_data() == true))
       
  1022 	{
       
  1023 		TPtrC8 reauthId(
       
  1024 			reauthentication_id->get_data(reauthentication_id->get_data_length()), 
       
  1025 			reauthentication_id->get_data_length());
       
  1026 		
       
  1027 		EAP_TRACE_DATA_DEBUG(
       
  1028 			m_am_tools,
       
  1029 			TRACE_FLAGS_DEFAULT,
       
  1030 			(EAPL("reauthId to DB"), reauthId.Ptr(), reauthId.Length()));
       
  1031 		
       
  1032 		view.SetColL(colSet->ColNo(KReauthId), reauthId);		
       
  1033 	} 
       
  1034 	else 
       
  1035 	{
       
  1036 		// If passed reauthentication_id was 0 that means the field must be cleared.
       
  1037 		view.SetColNullL(colSet->ColNo(KReauthId));
       
  1038 	}
       
  1039 	
       
  1040 	CleanupStack::PopAndDestroy(colSet); // Delete colSet.	
       
  1041 	
       
  1042 	view.PutL();	
       
  1043 	
       
  1044 	// Close database
       
  1045 	CleanupStack::PopAndDestroy(2); // view, buf
       
  1046 
       
  1047 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1048 }
       
  1049 //--------------------------------------------------
       
  1050 
       
  1051 eap_status_e eap_am_type_gsmsim_symbian_c::store_imsi(
       
  1052 	const eap_variable_data_c * const imsi)
       
  1053 {
       
  1054 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);	
       
  1055 	
       
  1056 	// Send error if the IMSI is too long.
       
  1057 	if(imsi != NULL && imsi->get_data_length() > KMaxIMSILengthInDB)
       
  1058 	{
       
  1059 		// IMSI too long. Can't store this in DB.
       
  1060 		
       
  1061 		EAP_TRACE_DEBUG(
       
  1062 			m_am_tools,
       
  1063 			TRACE_FLAGS_DEFAULT,
       
  1064 			(EAPL("eap_am_type_gsmsim_symbian_c::store_imsi: ")
       
  1065 			 EAPL("Too long IMSI. Length=%d\n"),
       
  1066 			 imsi->get_data_length()));
       
  1067 		
       
  1068 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1069 		return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter);
       
  1070 	}
       
  1071 	
       
  1072 	eap_status_e status(eap_status_ok);
       
  1073 
       
  1074 	TRAPD(err, store_imsiL(imsi));
       
  1075 	if (err != KErrNone)
       
  1076 	{
       
  1077 		status = m_am_tools->convert_am_error_to_eapol_error(err);
       
  1078 		send_error_notification(status);
       
  1079 	} 
       
  1080 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1081 	return EAP_STATUS_RETURN(m_am_tools, status);
       
  1082 }
       
  1083 
       
  1084 void eap_am_type_gsmsim_symbian_c::store_imsiL(
       
  1085 	const eap_variable_data_c * const imsi)
       
  1086 {
       
  1087 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1088 	
       
  1089 	// Form the insertion command
       
  1090 	HBufC* buf = HBufC::NewLC(KMaxSqlQueryLength);
       
  1091 	TPtr sqlStatement = buf->Des();
       
  1092 
       
  1093 	_LIT(KSQLInsert, "SELECT %S FROM %S WHERE %S=%d AND %S=%d AND %S=%d");
       
  1094 	sqlStatement.Format(KSQLInsert, &KPreviousIMSI, &KSimTableName, 
       
  1095 		&KServiceType, m_index_type, &KServiceIndex, m_index, &KTunnelingType, m_tunneling_vendor_type);
       
  1096 	// Evaluate view
       
  1097 	RDbView view;
       
  1098 	
       
  1099 	User::LeaveIfError(view.Prepare(m_database,TDbQuery(sqlStatement), TDbWindow::EUnlimited));
       
  1100 	CleanupClosePushL(view);
       
  1101 	
       
  1102 	User::LeaveIfError(view.EvaluateAll());
       
  1103 	view.FirstL();
       
  1104 
       
  1105 	view.UpdateL();
       
  1106 
       
  1107 	// Get column set so we get the correct column numbers
       
  1108 	CDbColSet* colSet = view.ColSetL();
       
  1109 	CleanupStack::PushL(colSet);	
       
  1110 
       
  1111 	// Create pointer to the data
       
  1112 	if ((imsi != 0) 
       
  1113 		&& (imsi->get_is_valid_data() == true))
       
  1114 	{
       
  1115 		TPtrC8 tmp_imsi(
       
  1116 			imsi->get_data(imsi->get_data_length()), 
       
  1117 			imsi->get_data_length());	
       
  1118 	
       
  1119 		EAP_TRACE_DATA_DEBUG(
       
  1120 			m_am_tools,
       
  1121 			TRACE_FLAGS_DEFAULT,
       
  1122 			(EAPL("IMSI to DB"), tmp_imsi.Ptr(), tmp_imsi.Length()));
       
  1123 		
       
  1124 		view.SetColL(colSet->ColNo(KPreviousIMSI), tmp_imsi); 
       
  1125 	} 
       
  1126 	else 
       
  1127 	{
       
  1128 		// If passed IMSI was 0 that means the field must be cleared.
       
  1129 		view.SetColNullL(colSet->ColNo(KPreviousIMSI));
       
  1130 	}
       
  1131 	
       
  1132 	CleanupStack::PopAndDestroy(colSet); // Delete colSet.	
       
  1133 
       
  1134 	view.PutL();	
       
  1135 	
       
  1136 	// Close database
       
  1137 	CleanupStack::PopAndDestroy(2); // view, buf
       
  1138 
       
  1139 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1140 }
       
  1141 
       
  1142 //--------------------------------------------------
       
  1143 
       
  1144 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::query_SIM_IMSI_or_pseudonym_or_reauthentication_id(
       
  1145 	const bool must_be_synchronous,
       
  1146 	eap_variable_data_c * const IMSI,
       
  1147 	eap_variable_data_c * const pseudonym_identity,
       
  1148 	eap_variable_data_c * const reauthentication_identity,
       
  1149 	eap_variable_data_c * const automatic_realm, ///< If this is not used, do not add any data to this parameter.
       
  1150 	u32_t * const length_of_mnc,
       
  1151 	const gsmsim_payload_AT_type_e required_identity,
       
  1152 	const eap_type_gsmsim_complete_e required_completion,
       
  1153 	const u8_t received_eap_identifier)
       
  1154 {
       
  1155 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1156 
       
  1157 	eap_status_e status(eap_status_ok);
       
  1158 
       
  1159 	EAP_ASSERT((IMSI != 0) && (pseudonym_identity != 0) && (reauthentication_identity != 0));
       
  1160 
       
  1161 	m_stored_required_completion = required_completion;
       
  1162 	m_stored_received_eap_identifier = received_eap_identifier;
       
  1163 
       
  1164 	TRAPD(err, query_SIM_IMSI_or_pseudonym_or_reauthentication_idL(
       
  1165 		must_be_synchronous,
       
  1166 		IMSI,
       
  1167 		pseudonym_identity,
       
  1168 		reauthentication_identity,
       
  1169 		automatic_realm,
       
  1170 		length_of_mnc,
       
  1171 		required_identity,
       
  1172 		required_completion,
       
  1173 		received_eap_identifier));
       
  1174 	if (err != KErrNone)
       
  1175 	{
       
  1176 		status = m_am_tools->convert_am_error_to_eapol_error(err);
       
  1177 
       
  1178 		if (status != eap_status_pending_request)
       
  1179 		{
       
  1180 			send_error_notification(status);
       
  1181 		}
       
  1182 	} 
       
  1183 
       
  1184 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1185 	return EAP_STATUS_RETURN(m_am_tools, status);
       
  1186 }
       
  1187 
       
  1188 void eap_am_type_gsmsim_symbian_c::query_SIM_IMSI_or_pseudonym_or_reauthentication_idL(
       
  1189 	const bool /*must_be_synchronous*/,
       
  1190 	eap_variable_data_c * const IMSI,
       
  1191 	eap_variable_data_c * const pseudonym_identity,
       
  1192 	eap_variable_data_c * const reauthentication_identity,
       
  1193 	eap_variable_data_c * const automatic_realm, ///< If this is not used, do not add any data to this parameter.
       
  1194 	u32_t * const /*length_of_mnc*/,
       
  1195 	const gsmsim_payload_AT_type_e required_identity,
       
  1196 	const eap_type_gsmsim_complete_e /*required_completion*/,
       
  1197 	const u8_t /*received_eap_identifier*/)
       
  1198 {
       
  1199 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1200 
       
  1201 	eap_status_e status(eap_status_process_general_error);
       
  1202 
       
  1203 	HBufC* buf = HBufC::NewLC(KMaxSqlQueryLength);
       
  1204 	TPtr sqlStatement = buf->Des();
       
  1205 	
       
  1206 	// Reset everything
       
  1207 	IMSI->reset();
       
  1208 	pseudonym_identity->reset();
       
  1209 	reauthentication_identity->reset();
       
  1210 	automatic_realm->reset();
       
  1211 	m_stored_pseudonym.reset();
       
  1212 	m_stored_reauth_id.reset();
       
  1213 	m_previous_imsi.reset();
       
  1214 
       
  1215 	// Query all the relevant parameters
       
  1216 
       
  1217 	_LIT(KSQLQuery, "SELECT %S, %S, %S, %S FROM %S WHERE %S=%d AND %S=%d AND %S=%d");
       
  1218 	sqlStatement.Format(KSQLQuery, &KReauthId, &KReauthCounter, &KPseudonymId,
       
  1219 						&KPreviousIMSI, &KSimTableName,
       
  1220 						&KServiceType, m_index_type, 
       
  1221 						&KServiceIndex, m_index, &KTunnelingType, m_tunneling_vendor_type);
       
  1222 
       
  1223 	RDbView view;
       
  1224 	// Evaluate view
       
  1225 	User::LeaveIfError(view.Prepare(m_database, TDbQuery(sqlStatement)));
       
  1226 	CleanupClosePushL(view);
       
  1227 	
       
  1228 	User::LeaveIfError(view.EvaluateAll());
       
  1229 	
       
  1230 	// Get the first (and only) row
       
  1231 	view.FirstL();
       
  1232 	view.GetL();
       
  1233 	
       
  1234 	// Get column set so we get the correct column numbers
       
  1235 	CDbColSet* colSet = view.ColSetL();
       
  1236 	CleanupStack::PushL(colSet);
       
  1237 	
       
  1238 	// Get the values from DB
       
  1239 	TUint reauthCount = view.ColUint32(colSet->ColNo(KReauthCounter));	
       
  1240 
       
  1241 	EAP_TRACE_DEBUG(
       
  1242 		m_am_tools,
       
  1243 		TRACE_FLAGS_DEFAULT,
       
  1244 		(EAPL("query_SIM_IMSI_or_pseudonym_or_reauthentication_idL: ")
       
  1245 		 EAPL("required_identity=%d, reauthCount from DB %d\n"),
       
  1246 		 required_identity, reauthCount));
       
  1247 	
       
  1248 	// A stream is needed for LONG columns in DB.
       
  1249 	RDbColReadStream readStream;
       
  1250 
       
  1251 	// Get pseudonym ID from DB.
       
  1252 	HBufC8* pseudonymIdBuf = HBufC8::NewLC(KMaxPseudonymIdLengthInDB); // Buffer for pseudonym id.
       
  1253 	TPtr8 pseudonymId = pseudonymIdBuf->Des();
       
  1254 	
       
  1255 	readStream.OpenLC(view, colSet->ColNo(KPseudonymId));
       
  1256 	readStream.ReadL(pseudonymId, view.ColLength(colSet->ColNo(KPseudonymId)));
       
  1257 	readStream.Close();
       
  1258 	CleanupStack::Pop(&readStream); // Pop readStream.
       
  1259 	
       
  1260 	EAP_TRACE_DATA_DEBUG(
       
  1261 		m_am_tools,
       
  1262 		TRACE_FLAGS_DEFAULT,
       
  1263 		(EAPL("pseudonymId from DB"), pseudonymId.Ptr(), pseudonymId.Length()));
       
  1264 
       
  1265 	// Get reauthentication ID from DB.
       
  1266 	HBufC8* reauthIdBuf = HBufC8::NewLC(KMaxReauthIdLengthInDB); // Buffer for reauthentication id.
       
  1267 	TPtr8 reauthId = reauthIdBuf->Des();
       
  1268 	
       
  1269 	readStream.OpenLC(view, colSet->ColNo(KReauthId));
       
  1270 	readStream.ReadL(reauthId, view.ColLength(colSet->ColNo(KReauthId)));
       
  1271 	readStream.Close();
       
  1272 	CleanupStack::Pop(&readStream); // Pop readStream.
       
  1273 
       
  1274 	EAP_TRACE_DATA_DEBUG(
       
  1275 		m_am_tools,
       
  1276 		TRACE_FLAGS_DEFAULT,
       
  1277 		(EAPL("reauthId from DB"), reauthId.Ptr(), reauthId.Length()));
       
  1278 
       
  1279 #if defined(USE_EAP_GSMSIM_INTERFACE) && !defined(__WINS__)
       
  1280 
       
  1281 	// Get the previous IMSI. Used for checking if SIM has been changed.
       
  1282 	// The checking is done in complete_SIM_imsi_L
       
  1283 	
       
  1284 	TPtrC8 tmp_imsi = view.ColDes8(colSet->ColNo(KPreviousIMSI));
       
  1285 	
       
  1286 	status = m_previous_imsi.set_copy_of_buffer(tmp_imsi.Ptr(), tmp_imsi.Length());
       
  1287 	if (status != eap_status_ok)
       
  1288 	{
       
  1289 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1290 		User::Leave(
       
  1291 			m_am_tools->convert_eapol_error_to_am_error(
       
  1292 				EAP_STATUS_RETURN(m_am_tools, status)));
       
  1293 	}
       
  1294 	
       
  1295 	EAP_TRACE_DATA_DEBUG(
       
  1296 		m_am_tools,
       
  1297 		TRACE_FLAGS_DEFAULT,
       
  1298 		(EAPL("imsi from DB"), tmp_imsi.Ptr(), tmp_imsi.Size()));
       
  1299 #endif
       
  1300 
       
  1301 	if (required_identity == gsmsim_payload_AT_ANY_ID_REQ)
       
  1302 	{
       
  1303 		if (is_session_valid())
       
  1304 		{
       
  1305 			if(reauthId.Length() > 0)
       
  1306 			{
       
  1307 
       
  1308 #if defined(USE_EAP_GSMSIM_INTERFACE) && !defined(__WINS__)
       
  1309 
       
  1310 				// Store the reauth id in case we are using asynchronous processing (= ISA interface)
       
  1311 				status = m_stored_reauth_id.set_copy_of_buffer(reauthId.Ptr(), reauthId.Length());
       
  1312 				if (status != eap_status_ok)
       
  1313 				{
       
  1314 					EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1315 					User::Leave(
       
  1316 						m_am_tools->convert_eapol_error_to_am_error(
       
  1317 							EAP_STATUS_RETURN(m_am_tools, status)));
       
  1318 				}	
       
  1319 #endif
       
  1320 
       
  1321 				status = reauthentication_identity->set_copy_of_buffer(reauthId.Ptr(), reauthId.Length());
       
  1322 				if (status != eap_status_ok)
       
  1323 				{
       
  1324 					EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1325 					User::Leave(
       
  1326 						m_am_tools->convert_eapol_error_to_am_error(
       
  1327 							EAP_STATUS_RETURN(m_am_tools, status)));
       
  1328 				}
       
  1329 			}
       
  1330 		}
       
  1331 	}
       
  1332 	
       
  1333 	CleanupStack::PopAndDestroy(reauthIdBuf); // Delete reauthIdBuf
       
  1334 	
       
  1335 	if (required_identity == gsmsim_payload_AT_ANY_ID_REQ
       
  1336 			|| required_identity == gsmsim_payload_AT_FULLAUTH_ID_REQ)
       
  1337 	{
       
  1338 		if (pseudonymId.Length() > 0)
       
  1339 		{
       
  1340 
       
  1341 #if defined(USE_EAP_GSMSIM_INTERFACE) && !defined(__WINS__)
       
  1342 
       
  1343 			// Store the pseudonym in case we are using
       
  1344 			// asynchronous processing (= ISA interface)
       
  1345 			status = m_stored_pseudonym.set_copy_of_buffer(
       
  1346 				pseudonymId.Ptr(),
       
  1347 				pseudonymId.Length());
       
  1348 			if (status != eap_status_ok)
       
  1349 			{
       
  1350 				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1351 				User::Leave(
       
  1352 					m_am_tools->convert_eapol_error_to_am_error(
       
  1353 						EAP_STATUS_RETURN(m_am_tools, status)));
       
  1354 			}
       
  1355 #endif
       
  1356 
       
  1357 			status = pseudonym_identity->set_copy_of_buffer(
       
  1358 				pseudonymId.Ptr(),
       
  1359 				pseudonymId.Length());
       
  1360 			if (status != eap_status_ok)
       
  1361 			{
       
  1362 				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1363 				User::Leave(
       
  1364 					m_am_tools->convert_eapol_error_to_am_error(
       
  1365 						EAP_STATUS_RETURN(m_am_tools, status)));
       
  1366 			}
       
  1367 		}
       
  1368 	}
       
  1369 
       
  1370 	CleanupStack::PopAndDestroy(pseudonymIdBuf); // Delete pseudonymIdBuf
       
  1371 
       
  1372 	CleanupStack::PopAndDestroy(colSet); // Delete colSet.
       
  1373 
       
  1374 	CleanupStack::PopAndDestroy( &view ); // Close view.
       
  1375 
       
  1376 	CleanupStack::PopAndDestroy(buf); // Delete buf
       
  1377 
       
  1378 	if (IMSI->get_is_valid_data() == false
       
  1379 		|| IMSI->get_buffer_length() < SIM_IMSI_LENGTH)
       
  1380 	{
       
  1381 		status = IMSI->init(SIM_IMSI_LENGTH);
       
  1382 		if (status != eap_status_ok)
       
  1383 		{
       
  1384 			User::Leave(m_am_tools->convert_eapol_error_to_am_error(EAP_STATUS_RETURN(m_am_tools, status)));
       
  1385 		}
       
  1386 
       
  1387 		IMSI->set_is_valid();
       
  1388 	}
       
  1389 
       
  1390 	{
       
  1391 	
       
  1392 #if defined (USE_EAP_GSMSIM_INTERFACE) && !defined(__WINS__)
       
  1393 
       
  1394 		// For real GSM SIM interface.
       
  1395 		// Use real GSM SIM interface active object.
       
  1396 		// This gets the IMSI from the real GSM SIM, using Custom API.
       
  1397 		
       
  1398 		m_isa_if->QueryIMSIL();
       
  1399 		
       
  1400 		// KErrCompletion means that the operation is pending.
       
  1401 		User::Leave(KErrCompletion);
       
  1402 		
       
  1403 #elif defined(__WINS__) // For plugin tester.
       
  1404 
       
  1405 		// Here we get the Nokia test SIM IMSI since the SIM interface is disabled.
       
  1406 		u32_t imsi_length = 0u;
       
  1407 		
       
  1408 		status = query_SIM_imsi(
       
  1409 			IMSI->get_buffer(SIM_IMSI_LENGTH),
       
  1410 			SIM_IMSI_LENGTH,
       
  1411 			&imsi_length);
       
  1412 
       
  1413 		if (status != eap_status_ok)
       
  1414 		{
       
  1415 			EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1416 			User::Leave(
       
  1417 				m_am_tools->convert_eapol_error_to_am_error(
       
  1418 					EAP_STATUS_RETURN(m_am_tools, status)));
       
  1419 		}
       
  1420 		else if (imsi_length != SIM_IMSI_LENGTH)
       
  1421 		{
       
  1422 			EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1423 			User::Leave(KErrGeneral);
       
  1424 		}
       
  1425 		
       
  1426 		status = IMSI->set_data_length(imsi_length);
       
  1427 		
       
  1428 		if (status != eap_status_ok)
       
  1429 		{			
       
  1430 			EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1431 			User::Leave(KErrGeneral);
       
  1432 		}
       
  1433 
       
  1434 #endif // End of #if defined (USE_EAP_GSMSIM_INTERFACE) && !defined(__WINS__)
       
  1435 	
       
  1436 	}	
       
  1437 		
       
  1438 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);	
       
  1439 }
       
  1440 
       
  1441 //--------------------------------------------------
       
  1442 
       
  1443 eap_status_e eap_am_type_gsmsim_symbian_c::complete_SIM_imsi_L(
       
  1444 	const eap_variable_data_c * const IMSI,
       
  1445 	const eap_status_e completion_status)
       
  1446 {
       
  1447 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1448 	eap_status_e status(eap_status_process_general_error);
       
  1449 
       
  1450 	if (completion_status != eap_status_ok)
       
  1451 	{
       
  1452 		EAP_TRACE_ERROR(
       
  1453 			m_am_tools,
       
  1454 			TRACE_FLAGS_DEFAULT,
       
  1455 			(EAPL("complete_SIM_imsi_L: failed status=%d\n"),
       
  1456 			completion_status));
       
  1457 
       
  1458 		send_error_notification(eap_status_identity_query_failed);
       
  1459 
       
  1460 		// Signal upper layer
       
  1461 		status = get_am_partner()->complete_SIM_IMSI_or_pseudonym_or_reauthentication_id_query(
       
  1462 			IMSI, 
       
  1463 			&m_stored_pseudonym, 
       
  1464 			&m_stored_reauth_id, 
       
  1465 			0,
       
  1466 			EAP_TYPE_GSMSIM_DEFAULT_MNC_LENGTH_3_BYTES,
       
  1467 			m_stored_required_completion, 
       
  1468 			m_stored_received_eap_identifier,
       
  1469 			completion_status);	
       
  1470 
       
  1471 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1472 		return EAP_STATUS_RETURN(m_am_tools, status);		
       
  1473 	}
       
  1474 
       
  1475 	if (IMSI == 0
       
  1476 		|| IMSI->get_is_valid_data() == false)
       
  1477 	{
       
  1478 		EAP_TRACE_ERROR(
       
  1479 			m_am_tools,
       
  1480 			TRACE_FLAGS_DEFAULT,
       
  1481 			(EAPL("complete_SIM_imsi_L: failed no IMSI retrieved.\n"),
       
  1482 			completion_status));
       
  1483 
       
  1484 		send_error_notification(eap_status_identity_query_failed);
       
  1485 
       
  1486 		// IMSI query failed
       
  1487 		status = get_am_partner()->complete_SIM_IMSI_or_pseudonym_or_reauthentication_id_query(
       
  1488 			IMSI, 
       
  1489 			&m_stored_pseudonym, 
       
  1490 			&m_stored_reauth_id, 
       
  1491 			0,
       
  1492 			EAP_TYPE_GSMSIM_DEFAULT_MNC_LENGTH_3_BYTES,
       
  1493 			m_stored_required_completion, 
       
  1494 			m_stored_received_eap_identifier,
       
  1495 			eap_status_hardware_not_ready);	
       
  1496 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1497 		return status;
       
  1498 	}
       
  1499 
       
  1500 	if (m_previous_imsi.get_data_length() == 0)
       
  1501 	{
       
  1502 		EAP_TRACE_DEBUG(
       
  1503 			m_am_tools,
       
  1504 			TRACE_FLAGS_DEFAULT,
       
  1505 			(EAPL("Previous IMSI not valid. Storing new IMSI.\n")));
       
  1506 
       
  1507 		// Store new IMSI
       
  1508 		status = store_imsi(IMSI);
       
  1509 		if (status != eap_status_ok)
       
  1510 		{
       
  1511 			EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1512 			return EAP_STATUS_RETURN(m_am_tools, status);
       
  1513 		}		
       
  1514 	}
       
  1515 	else if (IMSI->compare(&m_previous_imsi) != 0)
       
  1516 	{
       
  1517 		EAP_TRACE_DEBUG(
       
  1518 			m_am_tools,
       
  1519 			TRACE_FLAGS_DEFAULT,
       
  1520 			(EAPL("SIM has been changed since last authentication. ")
       
  1521 			 EAPL("Clear pseudonym & reauth IDs.\n")));
       
  1522 
       
  1523 		// Different IMSI -> SIM has been changed.
       
  1524 		// Reset pseudonym and reauthentication ID. 
       
  1525 		status = store_reauthentication_id(0, 0);
       
  1526 		if (status != eap_status_ok)
       
  1527 		{
       
  1528 			EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1529 			return EAP_STATUS_RETURN(m_am_tools, status);
       
  1530 		}
       
  1531 		status = store_pseudonym_id(0, 0);
       
  1532 		if (status != eap_status_ok)
       
  1533 		{
       
  1534 			EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1535 			return EAP_STATUS_RETURN(m_am_tools, status);
       
  1536 		}
       
  1537 		// Store new IMSI
       
  1538 		status = store_imsi(IMSI);
       
  1539 		if (status != eap_status_ok)
       
  1540 		{
       
  1541 			EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1542 			return EAP_STATUS_RETURN(m_am_tools, status);
       
  1543 		}
       
  1544 		// Reset stored pseudonym & reauthentication ID
       
  1545 		m_stored_pseudonym.reset();
       
  1546 		m_stored_reauth_id.reset();
       
  1547 	}
       
  1548 	else
       
  1549 	{
       
  1550 		EAP_TRACE_DEBUG(
       
  1551 			m_am_tools,
       
  1552 			TRACE_FLAGS_DEFAULT,
       
  1553 			(EAPL("SIM has not been changed.\n")));
       
  1554 	}
       
  1555 
       
  1556 	status = get_am_partner()->complete_SIM_IMSI_or_pseudonym_or_reauthentication_id_query(
       
  1557 		IMSI,
       
  1558 		&m_stored_pseudonym,
       
  1559 		&m_stored_reauth_id,
       
  1560 		0,
       
  1561 		0ul,
       
  1562 		m_stored_required_completion,
       
  1563 		m_stored_received_eap_identifier,
       
  1564 		eap_status_ok);
       
  1565 
       
  1566 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1567 
       
  1568 //#endif
       
  1569 
       
  1570 	return status;
       
  1571 }
       
  1572 
       
  1573 //--------------------------------------------------
       
  1574 
       
  1575 //
       
  1576 eap_status_e eap_am_type_gsmsim_symbian_c::cancel_SIM_IMSI_or_pseudonym_or_reauthentication_id_query()
       
  1577 {
       
  1578 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1579 #if defined (USE_EAP_GSMSIM_INTERFACE) && !defined(__WINS__)
       
  1580 	if (m_is_client == true)
       
  1581 	{
       
  1582 		m_isa_if->Cancel();
       
  1583 	}
       
  1584 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1585 	return eap_status_ok;
       
  1586 #else
       
  1587 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1588 	return eap_status_not_supported;
       
  1589 	
       
  1590 #endif //#if defined (USE_EAP_GSMSIM_INTERFACE) && !defined(__WINS__)
       
  1591 	
       
  1592 }
       
  1593 
       
  1594 //--------------------------------------------------
       
  1595 
       
  1596 //
       
  1597 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::query_SIM_kc_and_sres(	
       
  1598 	const u8_t * const rand,
       
  1599 	u8_t * const kc,
       
  1600 	u8_t * const sres)
       
  1601 {
       
  1602 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1603 	eap_status_e status(eap_status_ok);
       
  1604 
       
  1605 #if !defined (USE_EAP_GSMSIM_INTERFACE)
       
  1606 	if (rand == 0
       
  1607 		|| kc == 0
       
  1608 		|| sres == 0)
       
  1609 	{
       
  1610 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1611 		return EAP_STATUS_RETURN(m_am_tools, eap_status_illegal_parameter);
       
  1612 	}
       
  1613 
       
  1614 	u8_t tmp[SIM_KC_LENGTH + SIM_SRES_LENGTH];
       
  1615 	m_am_tools->memset(tmp, 0, sizeof(tmp));
       
  1616 
       
  1617 	status = m_sim_algorithm.generate_kc_sres(
       
  1618 		m_simulator_sim_algorithm,
       
  1619 		rand,
       
  1620 		SIM_RAND_LENGTH,
       
  1621 		tmp,
       
  1622 		SIM_KC_LENGTH + SIM_SRES_LENGTH);
       
  1623 
       
  1624 	m_am_tools->memmove(sres, tmp, SIM_SRES_LENGTH);
       
  1625 	m_am_tools->memmove(kc, tmp + SIM_SRES_LENGTH, SIM_KC_LENGTH);
       
  1626 
       
  1627 #elif defined (USE_EAP_GSMSIM_INTERFACE) && !defined(__WINS__)
       
  1628 
       
  1629 	TBuf8<SIM_RAND_LENGTH> rand_buf;
       
  1630 	rand_buf.Copy(rand, SIM_RAND_LENGTH);
       
  1631 	TRAPD(err, m_isa_if->QueryKcAndSRESL(rand_buf));	
       
  1632 	if (err != KErrNone)
       
  1633 	{
       
  1634 		EAP_TRACE_ERROR(
       
  1635 			m_am_tools,
       
  1636 			TRACE_FLAGS_DEFAULT,
       
  1637 			(EAPL("m_isa_if->QueryKcAndSRESL(): %d"),
       
  1638 			err));
       
  1639 
       
  1640 		status = m_am_tools->convert_am_error_to_eapol_error(err);
       
  1641 		if (status == eap_status_process_general_error)
       
  1642 		{
       
  1643 			status = eap_status_credential_query_failed;
       
  1644 		}
       
  1645 		send_error_notification(status);
       
  1646 	}
       
  1647 	else
       
  1648 	{
       
  1649 		status = eap_status_pending_request;
       
  1650 	}
       
  1651 #else
       
  1652 
       
  1653 	EAP_UNREFERENCED_PARAMETER(rand);
       
  1654 	EAP_UNREFERENCED_PARAMETER(kc);
       
  1655 	EAP_UNREFERENCED_PARAMETER(sres);
       
  1656 	
       
  1657 #endif //#if !defined (USE_EAP_GSMSIM_INTERFACE)
       
  1658 
       
  1659 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1660 	return EAP_STATUS_RETURN(m_am_tools, status);
       
  1661 }
       
  1662 
       
  1663 //--------------------------------------------------
       
  1664 
       
  1665 eap_status_e eap_am_type_gsmsim_symbian_c::complete_SIM_kc_and_sres_L(
       
  1666 	TDesC8& aKc,
       
  1667 	TDesC8& aSRES,
       
  1668 	const eap_status_e completion_status)
       
  1669 {
       
  1670 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1671 	
       
  1672 	EAP_TRACE_DEBUG(m_am_tools, TRACE_FLAGS_DEFAULT, (EAPL("complete_SIM_kc_and_sres\n")));
       
  1673 	
       
  1674 	eap_status_e status(eap_status_ok);
       
  1675 	
       
  1676 	if (completion_status != eap_status_ok)
       
  1677 	{
       
  1678 		EAP_TRACE_ERROR(
       
  1679 			m_am_tools,
       
  1680 			TRACE_FLAGS_DEFAULT,
       
  1681 			(EAPL("complete_SIM_kc_and_sres_L: failed status=%d\n"),
       
  1682 			completion_status));
       
  1683 
       
  1684 		send_error_notification(eap_status_credential_query_failed);
       
  1685 
       
  1686 		// Signal upper layer
       
  1687 		status = get_am_partner()->complete_SIM_kc_sres(
       
  1688 			&m_copy_of_n_rands,
       
  1689 			&m_n_kc,
       
  1690 			&m_n_sres,
       
  1691 			completion_status);
       
  1692 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1693 		return EAP_STATUS_RETURN(m_am_tools, status);		
       
  1694 	}	
       
  1695 	
       
  1696 #if !defined(USE_EAP_GSMSIM_INTERFACE) || defined(__WINS__)
       
  1697 	EAP_UNREFERENCED_PARAMETER(aKc);
       
  1698 	EAP_UNREFERENCED_PARAMETER(aSRES);
       
  1699 #else
       
  1700 
       
  1701 	if (static_cast<u32_t>(aKc.Length()) != SIM_KC_LENGTH
       
  1702 		|| static_cast<u32_t>(aSRES.Length()) != SIM_SRES_LENGTH)
       
  1703 	{
       
  1704 		EAP_TRACE_ERROR(
       
  1705 			m_am_tools,
       
  1706 			TRACE_FLAGS_DEFAULT,
       
  1707 			(EAPL("complete_SIM_kc_and_sres_L: Illegal length Kc or SRES\n")));
       
  1708 
       
  1709 		send_error_notification(eap_status_credential_query_failed);
       
  1710 
       
  1711 		status = get_am_partner()->complete_SIM_kc_sres(
       
  1712 			&m_copy_of_n_rands,
       
  1713 			&m_n_kc,
       
  1714 			&m_n_sres,
       
  1715 			eap_status_hardware_not_ready);		
       
  1716 
       
  1717 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1718 		return EAP_STATUS_RETURN(m_am_tools, status);		
       
  1719 	}
       
  1720 
       
  1721 	m_rands_handled++;
       
  1722 
       
  1723 	TInt rand_count = m_copy_of_n_rands.get_data_length() / SIM_RAND_LENGTH;
       
  1724 
       
  1725 	EAP_TRACE_DEBUG(
       
  1726 		m_am_tools,
       
  1727 		TRACE_FLAGS_DEFAULT,
       
  1728 		(EAPL("complete_SIM_kc_and_sres: m_rands_handled=%d, rand_count=%d\n"),
       
  1729 		m_rands_handled,
       
  1730 		rand_count));
       
  1731 
       
  1732 	status = m_n_kc.add_data(aKc.Ptr(), aKc.Size());
       
  1733 	if (status != eap_status_ok)
       
  1734 	{		
       
  1735 		send_error_notification(eap_status_credential_query_failed);
       
  1736 
       
  1737 		// Signal upper layer
       
  1738 		status = get_am_partner()->complete_SIM_kc_sres(
       
  1739 			&m_copy_of_n_rands,
       
  1740 			&m_n_kc,
       
  1741 			&m_n_sres,
       
  1742 			status);
       
  1743 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1744 		return EAP_STATUS_RETURN(m_am_tools, status);		
       
  1745 	}
       
  1746 	
       
  1747 	status = m_n_sres.add_data(aSRES.Ptr(), aSRES.Size());
       
  1748 	if (status != eap_status_ok)
       
  1749 	{
       
  1750 		send_error_notification(eap_status_credential_query_failed);
       
  1751 
       
  1752 		// Signal upper layer
       
  1753 		status = get_am_partner()->complete_SIM_kc_sres(
       
  1754 			&m_copy_of_n_rands,
       
  1755 			&m_n_kc,
       
  1756 			&m_n_sres,
       
  1757 			status);
       
  1758 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1759 		return EAP_STATUS_RETURN(m_am_tools, status);		
       
  1760 	}
       
  1761 	
       
  1762 	if (m_rands_handled == rand_count)
       
  1763 	{
       
  1764 		// All rands handled. Complete request.
       
  1765 		status = get_am_partner()->complete_SIM_kc_sres(
       
  1766 			&m_copy_of_n_rands,
       
  1767 			&m_n_kc,
       
  1768 			&m_n_sres,
       
  1769 			eap_status_ok);		
       
  1770 	}
       
  1771 	else
       
  1772 	{
       
  1773 		// Handle next rand.		
       
  1774 		u8_t *rand = m_copy_of_n_rands.get_data_offset(
       
  1775 			m_rands_handled*SIM_RAND_LENGTH,
       
  1776 			SIM_RAND_LENGTH);
       
  1777 		if (rand == 0)
       
  1778 		{
       
  1779 			send_error_notification(eap_status_credential_query_failed);
       
  1780 
       
  1781 			// ERROR: Signal upper layer bacause of allocation error.
       
  1782 			status = get_am_partner()->complete_SIM_kc_sres(
       
  1783 				&m_copy_of_n_rands,
       
  1784 				&m_n_kc,
       
  1785 				&m_n_sres,
       
  1786 				eap_status_allocation_error);
       
  1787 			EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1788 			return EAP_STATUS_RETURN(m_am_tools, status);		
       
  1789 		}
       
  1790 
       
  1791 		EAP_TRACE_DATA_DEBUG(
       
  1792 			m_am_tools,
       
  1793 			TRACE_FLAGS_DEFAULT,
       
  1794 			(EAPL("EAP-SIM next RAND"),
       
  1795 			 rand,
       
  1796 			 SIM_RAND_LENGTH));
       
  1797 
       
  1798 		status = query_SIM_kc_and_sres(
       
  1799 			rand,
       
  1800 			0,		 // Kc and SRES are handled in the complete function and ignored here in this asynchronous case
       
  1801 			0);
       
  1802 		
       
  1803 		if (status != eap_status_ok
       
  1804 			&& status != eap_status_pending_request)
       
  1805 		{
       
  1806 			send_error_notification(eap_status_credential_query_failed);
       
  1807 
       
  1808 			// Signal upper layer
       
  1809 			status = get_am_partner()->complete_SIM_kc_sres(
       
  1810 				&m_copy_of_n_rands,
       
  1811 				&m_n_kc,
       
  1812 				&m_n_sres,
       
  1813 				status);					
       
  1814 		}
       
  1815 
       
  1816 	}
       
  1817 #endif // defined(USE_EAP_GSMSIM_INTERFACE) || defined(__WINS__)
       
  1818 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1819 	return EAP_STATUS_RETURN(m_am_tools, status);
       
  1820 }
       
  1821 
       
  1822 //--------------------------------------------------
       
  1823 
       
  1824 //
       
  1825 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::query_SIM_kc_sres(
       
  1826 	const bool must_be_synchronous,
       
  1827 	const eap_variable_data_c * const p_n_rands,
       
  1828 	eap_variable_data_c * const n_kc,
       
  1829 	eap_variable_data_c * const n_sres)
       
  1830 {
       
  1831 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1832 	EAP_UNREFERENCED_PARAMETER(must_be_synchronous);
       
  1833 
       
  1834 	eap_status_e status(eap_status_process_general_error);
       
  1835 	// NOTE if user needs to use state_selector or n_rands after return from query_SIM_kc_sres()
       
  1836 	// function those parameters MUST be copied using copy() member function of each parameter.
       
  1837 	// For example: saved_n_rands = n_rands->copy()
       
  1838 	//              saved_state_selector = state_selector->copy()
       
  1839 
       
  1840 	EAP_ASSERT(m_am_tools->get_global_mutex()->get_is_reserved() == true);
       
  1841 
       
  1842 	if (n_kc == 0
       
  1843 		|| n_sres == 0
       
  1844 		|| p_n_rands == 0)
       
  1845 	{
       
  1846 		// Something is really wrong.
       
  1847 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1848 		return EAP_STATUS_RETURN(m_am_tools, eap_status_process_general_error);
       
  1849 	}
       
  1850 
       
  1851 	m_rands_handled = 0ul;
       
  1852 
       
  1853 #if !defined(USE_EAP_GSMSIM_INTERFACE) || defined(__WINS__)
       
  1854 	
       
  1855 	eap_variable_data_c *copy_of_n_rands = p_n_rands->copy();
       
  1856 
       
  1857 	if (copy_of_n_rands == 0)
       
  1858 	{
       
  1859 		return eap_status_allocation_error;
       
  1860 	}
       
  1861 
       
  1862 	u32_t count = copy_of_n_rands->get_data_length() / SIM_RAND_LENGTH;
       
  1863 
       
  1864 	if (n_kc->get_is_valid_data() == false
       
  1865 		|| n_kc->get_buffer_length() < count*SIM_KC_LENGTH)
       
  1866 	{
       
  1867 		n_kc->init(count*SIM_KC_LENGTH);
       
  1868 		n_kc->set_is_valid();
       
  1869 	}
       
  1870 
       
  1871 	if (n_sres->get_is_valid_data() == false
       
  1872 		|| n_sres->get_buffer_length() < count*SIM_SRES_LENGTH)
       
  1873 	{
       
  1874 		n_sres->init(count*SIM_SRES_LENGTH);
       
  1875 		n_sres->set_is_valid();
       
  1876 	}
       
  1877 
       
  1878 	u8_t *rand = copy_of_n_rands->get_data(copy_of_n_rands->get_data_length());
       
  1879 	u8_t *kc = n_kc->get_data(n_kc->get_data_length());
       
  1880 	u8_t *sres = n_sres->get_data(n_sres->get_data_length());
       
  1881 
       
  1882 	for (u32_t ind = 0u; ind < count; ind++)
       
  1883 	{
       
  1884 		query_SIM_kc_and_sres(			
       
  1885 			rand+(ind*SIM_RAND_LENGTH),
       
  1886 			kc+(ind*SIM_KC_LENGTH),
       
  1887 			sres+(ind*SIM_SRES_LENGTH));
       
  1888 	}
       
  1889 
       
  1890 	n_kc->set_data_length(count*SIM_KC_LENGTH);
       
  1891 	n_sres->set_data_length(count*SIM_SRES_LENGTH);
       
  1892 
       
  1893 	status = get_am_partner()->complete_SIM_kc_sres(
       
  1894 		copy_of_n_rands,
       
  1895 		n_kc,
       
  1896 		n_sres,
       
  1897 		eap_status_ok);
       
  1898 
       
  1899 	delete copy_of_n_rands;
       
  1900 
       
  1901 	if (status == eap_status_ok)
       
  1902 	{
       
  1903 		status = eap_status_completed_request;
       
  1904 	}
       
  1905 
       
  1906 #else
       
  1907 
       
  1908 	// This is for ISA interface			
       
  1909 	status = m_copy_of_n_rands.set_copy_of_buffer(p_n_rands);
       
  1910 	if (status != eap_status_ok)
       
  1911 	{		
       
  1912 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1913 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
  1914 	}		
       
  1915 
       
  1916 	u8_t *rand = m_copy_of_n_rands.get_data(SIM_RAND_LENGTH);
       
  1917 	if (rand == 0)
       
  1918 	{
       
  1919 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1920 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
  1921 	}
       
  1922 
       
  1923 	m_n_kc.reset();
       
  1924 	m_n_sres.reset();
       
  1925 	
       
  1926 	status = query_SIM_kc_and_sres(
       
  1927 		rand,
       
  1928 		0,		 // Kc and SRES are handled in the complete function and ignored here in this asynchronous case
       
  1929 		0);
       
  1930 
       
  1931 #endif // !defined(USE_EAP_GSMSIM_INTERFACE) || defined(__WINS__)
       
  1932 
       
  1933 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1934 	return status;
       
  1935 }
       
  1936 
       
  1937 //-------------------------------------------------
       
  1938 
       
  1939 //
       
  1940 eap_status_e eap_am_type_gsmsim_symbian_c::cancel_SIM_kc_sres_query()
       
  1941 {
       
  1942 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1943 		
       
  1944 #if defined (USE_EAP_GSMSIM_INTERFACE) && !defined(__WINS__)
       
  1945 	if (m_is_client == true)
       
  1946 	{
       
  1947 		EAP_TRACE_DEBUG(m_am_tools, TRACE_FLAGS_DEFAULT, (EAPL("eap_am_type_gsmsim_symbian_c::cancel_SIM_kc_sres_query() - cancelling IF query\n")));
       
  1948 	
       
  1949 		m_isa_if->Cancel();
       
  1950 	}
       
  1951 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1952 	return eap_status_ok;
       
  1953 #else
       
  1954 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1955 	return EAP_STATUS_RETURN(m_am_tools, eap_status_not_supported);
       
  1956 	
       
  1957 #endif //#if defined (USE_EAP_GSMSIM_INTERFACE) && !defined(__WINS__)
       
  1958 		
       
  1959 }
       
  1960 
       
  1961 //--------------------------------------------------
       
  1962 
       
  1963 //
       
  1964 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::handle_gsmsim_notification(
       
  1965 	eap_gsmsim_notification_codes_e gsmsim_notification_code )
       
  1966 {
       
  1967 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1968 
       
  1969 	switch ((gsmsim_notification_code & gsmsim_notification_code_value))
       
  1970 	{
       
  1971 	case eap_gsmsim_notification_no_F_no_P_user_has_not_subscribed_to_the_requested_service:
       
  1972 		send_error_notification(eap_status_user_has_not_subscribed_to_the_requested_service);
       
  1973 		break;
       
  1974 
       
  1975 	case eap_gsmsim_notification_no_F_no_P_users_calls_are_barred:
       
  1976 		send_error_notification(eap_status_users_calls_are_barred);
       
  1977 		break;
       
  1978 
       
  1979 	default:
       
  1980 		// Do nothing
       
  1981 		break;
       
  1982 	}
       
  1983 	
       
  1984 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  1985 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
  1986 }
       
  1987 
       
  1988 //--------------------------------------------------
       
  1989 
       
  1990 EAP_FUNC_EXPORT void eap_am_type_gsmsim_symbian_c::set_is_valid()
       
  1991 {
       
  1992 	m_is_valid = true;
       
  1993 }
       
  1994 
       
  1995 //--------------------------------------------------
       
  1996 
       
  1997 EAP_FUNC_EXPORT bool eap_am_type_gsmsim_symbian_c::get_is_valid()
       
  1998 {
       
  1999 	return m_is_valid;
       
  2000 }
       
  2001 
       
  2002 //--------------------------------------------------
       
  2003 
       
  2004 //
       
  2005 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::type_configure_read(
       
  2006 	const eap_configuration_field_c * const field,
       
  2007 	eap_variable_data_c * const data)
       
  2008 {
       
  2009 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2010 	EAP_ASSERT(data != 0);
       
  2011 	EAP_ASSERT(field != 0);
       
  2012 
       
  2013 	eap_status_e status(eap_status_ok);
       
  2014 
       
  2015 	m_am_tools->trace_configuration(
       
  2016 		status,
       
  2017 		field,
       
  2018 		data);
       
  2019 
       
  2020 	// Trap must be set here because the OS independent portion of EAP SIM
       
  2021 	// that calls this function does not know anything about Symbian.	
       
  2022 	TRAPD(err, type_configure_readL(
       
  2023 		field->get_field(),
       
  2024 		field->get_field_length(),
       
  2025 		data));
       
  2026 	if (err != KErrNone)
       
  2027 	{
       
  2028 		EAP_TRACE_DEBUG(
       
  2029 			m_am_tools,
       
  2030 			TRACE_FLAGS_DEFAULT,
       
  2031 			(EAPL("eap_am_type_gsmsim_symbian_c::type_configure_read(): LEAVE from type_configure_readL, error=%d, Reads configuration from partner.\n"),
       
  2032 			err));
       
  2033 
       
  2034 		status = m_partner->read_configure(
       
  2035 			field,
       
  2036 			data);
       
  2037 	}
       
  2038 
       
  2039 	m_am_tools->trace_configuration(
       
  2040 		status,
       
  2041 		field,
       
  2042 		data);
       
  2043 
       
  2044 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2045 	return EAP_STATUS_RETURN(m_am_tools, status);
       
  2046 }
       
  2047 
       
  2048 //--------------------------------------------------
       
  2049 
       
  2050 void eap_am_type_gsmsim_symbian_c::type_configure_readL(
       
  2051 	eap_config_string field,
       
  2052 	const u32_t field_length,
       
  2053 	eap_variable_data_c * const data)
       
  2054 {
       
  2055 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2056 	EAP_UNREFERENCED_PARAMETER(field_length);
       
  2057 	
       
  2058 	// Create a buffer for the ascii strings - initialised with the argument
       
  2059 	HBufC8* asciibuf = HBufC8::NewLC(KMaxDBFieldNameLength);
       
  2060 	TPtr8 asciiString = asciibuf->Des();
       
  2061 	asciiString.Copy(reinterpret_cast<const unsigned char *>(field));
       
  2062 		
       
  2063 	// Buffer for unicode parameter
       
  2064 	HBufC* unicodebuf = HBufC::NewLC(KMaxDBFieldNameLength);
       
  2065 	TPtr unicodeString = unicodebuf->Des();
       
  2066 	
       
  2067 	// Convert to unicode 
       
  2068 	unicodeString.Copy(asciiString);
       
  2069 	
       
  2070 	// Now do the database query
       
  2071 	HBufC* buf = HBufC::NewLC(KMaxSqlQueryLength);
       
  2072 	TPtr sqlStatement = buf->Des();
       
  2073 
       
  2074 	_LIT(KSQLQueryRow, "SELECT %S FROM %S WHERE %S=%d AND %S=%d AND %S=%d");
       
  2075 	sqlStatement.Format(KSQLQueryRow, &unicodeString, &KSimTableName, 
       
  2076 		&KServiceType, m_index_type, &KServiceIndex, m_index, &KTunnelingType, m_tunneling_vendor_type);
       
  2077 	
       
  2078 	RDbView view;
       
  2079 	User::LeaveIfError(view.Prepare(m_database, TDbQuery(sqlStatement), TDbWindow::EUnlimited));
       
  2080 	CleanupClosePushL(view);
       
  2081 	User::LeaveIfError(view.EvaluateAll());	
       
  2082 	
       
  2083 	if (view.FirstL())
       
  2084 	{
       
  2085 		eap_status_e status;
       
  2086 		view.GetL();		
       
  2087 		switch (view.ColType(KDefaultColumnInView_One))
       
  2088 		{
       
  2089 		case EDbColText:
       
  2090 			{
       
  2091 				unicodeString = view.ColDes(KDefaultColumnInView_One);
       
  2092 				// Convert to 8-bit
       
  2093 				asciiString.Copy(unicodeString);
       
  2094 				if (asciiString.Size() > 0)
       
  2095 				{
       
  2096 					status = data->set_copy_of_buffer(asciiString.Ptr(), asciiString.Size());
       
  2097 					if (status != eap_status_ok)
       
  2098 					{
       
  2099 						User::Leave(
       
  2100 							m_am_tools->convert_eapol_error_to_am_error(
       
  2101 								EAP_STATUS_RETURN(m_am_tools, status)));
       
  2102 					}
       
  2103 				} 
       
  2104 				else 
       
  2105 				{
       
  2106 					status = data->init(0);
       
  2107 					if (status != eap_status_ok)
       
  2108 					{
       
  2109 						User::Leave(m_am_tools->convert_eapol_error_to_am_error(EAP_STATUS_RETURN(m_am_tools, status)));
       
  2110 					}
       
  2111 					data->set_is_valid();					
       
  2112 					break;
       
  2113 				}
       
  2114 		}
       
  2115 			break;
       
  2116 
       
  2117 		case EDbColUint32:
       
  2118 			{
       
  2119 				TUint value;
       
  2120 				value = view.ColUint32(KDefaultColumnInView_One);
       
  2121 				status = data->set_copy_of_buffer(&value, sizeof(value));
       
  2122 				if (status != eap_status_ok)
       
  2123 				{
       
  2124 					User::Leave(
       
  2125 						m_am_tools->convert_eapol_error_to_am_error(
       
  2126 							EAP_STATUS_RETURN(m_am_tools, status)));
       
  2127 				}
       
  2128 			}
       
  2129 			break;
       
  2130 
       
  2131 		default:
       
  2132 			EAP_TRACE_DEBUG(m_am_tools, TRACE_FLAGS_DEFAULT, (EAPL("type_configure_readL: Unexpected column type.\n")));
       
  2133 			User::Leave(KErrGeneral);
       
  2134 			break;
       
  2135 		}
       
  2136 	} 
       
  2137 	else 
       
  2138 	{
       
  2139 		// Could not find parameter
       
  2140 		EAP_TRACE_DEBUG(m_am_tools, TRACE_FLAGS_DEFAULT, (EAPL("type_configure_readL: Could not find configuration parameter.\n")));
       
  2141 		User::Leave(KErrArgument);
       
  2142 	}		
       
  2143 	
       
  2144 	CleanupStack::PopAndDestroy(4); // Close view, & 3 strings
       
  2145 
       
  2146 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2147 }
       
  2148 
       
  2149 //--------------------------------------------------
       
  2150 
       
  2151 void eap_am_type_gsmsim_symbian_c::send_error_notification(const eap_status_e error)
       
  2152 {
       
  2153 	// Here we swap the addresses.
       
  2154 	eap_am_network_id_c send_network_id(m_am_tools,
       
  2155 		m_receive_network_id.get_destination_id(),
       
  2156 		m_receive_network_id.get_source_id(),
       
  2157 		m_receive_network_id.get_type());
       
  2158 
       
  2159 	// Notifies the lower level of an authentication error.
       
  2160 	eap_state_notification_c notification(
       
  2161 		m_am_tools,
       
  2162 		&send_network_id,
       
  2163 		m_is_client,
       
  2164 		eap_state_notification_eap,
       
  2165 		eap_protocol_layer_general,
       
  2166 		eap_type_gsmsim,
       
  2167 		eap_state_none,
       
  2168 		eap_general_state_authentication_error,
       
  2169 		0,
       
  2170 		false);
       
  2171 
       
  2172 	notification.set_authentication_error(error);
       
  2173 
       
  2174 	m_partner->state_notification(&notification);
       
  2175 }
       
  2176 
       
  2177 
       
  2178 //--------------------------------------------------
       
  2179 //
       
  2180 // SERVER / TEST CODE BEGINS.
       
  2181 //
       
  2182 //--------------------------------------------------
       
  2183 
       
  2184 #if defined(USE_EAP_TYPE_SERVER_GSMSIM)
       
  2185 
       
  2186 #if defined(_DEBUG) && defined(USE_EAP_GSMSIM_TEST_VECTORS)
       
  2187 	#include "read_rand_and_triplets.h"
       
  2188 #endif //#if defined(_DEBUG) && defined(USE_EAP_GSMSIM_TEST_VECTORS)
       
  2189 
       
  2190 
       
  2191 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::query_SIM_triplets(
       
  2192 	const bool /*must_be_synchronous*/,
       
  2193 	const eap_variable_data_c * const /*p_username*/,
       
  2194 	eap_variable_data_c * const /* p_imsi */,
       
  2195 	eap_type_sim_triplet_array_c * const triplets,
       
  2196 	eap_type_gsmsim_identity_type * const /* type */)
       
  2197 {
       
  2198 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2199 	
       
  2200 	// Only used in EAP SIM server.
       
  2201 	
       
  2202 	// NOTE if user needs to use state_selector or imsi after return from query_SIM_triplets()
       
  2203 	// function those parameters MUST be copied using copy() member function of each parameter.
       
  2204 	// For example: saved_imsi = p_imsi_or_pseudonym->copy()
       
  2205 	//              saved_state_selector = state_selector->copy()
       
  2206 
       
  2207 	eap_status_e status = eap_status_process_general_error;
       
  2208 
       
  2209 #if !defined (USE_EAP_GSMSIM_INTERFACE)
       
  2210 
       
  2211 	if (triplets == 0)
       
  2212 	{
       
  2213 		// Something is really wrong.
       
  2214 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2215 		return eap_status_process_general_error;
       
  2216 	}
       
  2217 
       
  2218 #if defined(_DEBUG) && defined(USE_EAP_GSMSIM_TEST_VECTORS)
       
  2219 	if (m_triplet_file.get_is_valid() == true)
       
  2220 	{
       
  2221 		status = read_triplets_from_file(m_am_tools, &m_triplet_file, triplets);
       
  2222 	} else
       
  2223 #endif // defined(_DEBUG) && defined(USE_EAP_GSMSIM_TEST_VECTORS)
       
  2224 	{
       
  2225 		const u32_t rand_length = 16u;
       
  2226 		u8_t test_rand[] = "0123456789abcdef";
       
  2227 		const u32_t triplet_count = 2u;
       
  2228 		u32_t ind;
       
  2229 		
       
  2230 		triplets->set_triplet_count(triplet_count);
       
  2231 
       
  2232 		for (ind = 0u; ind < triplet_count; ind++)
       
  2233 		{
       
  2234 			eap_type_saesim_triplet_c *tripl = triplets->get_triplet(m_am_tools, ind);
       
  2235 			u8_t tmp[rand_length] = {0,};
       
  2236 	
       
  2237 			status = m_am_tools->get_crypto()->get_rand_bytes(
       
  2238 				test_rand,
       
  2239 				sizeof(test_rand)-1u);
       
  2240 			if (status != eap_status_ok)
       
  2241 			{
       
  2242 				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2243 				return status;
       
  2244 			}
       
  2245 
       
  2246 			status = tripl->get_rand()->set_copy_of_buffer(reinterpret_cast<u8_t *>(test_rand), rand_length);
       
  2247 			if (status != eap_status_ok)
       
  2248 			{
       
  2249 				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2250 				return status;
       
  2251 			}
       
  2252 	
       
  2253 			status = tripl->get_kc()->init(SIM_KC_LENGTH);
       
  2254 			if (status != eap_status_ok)
       
  2255 			{
       
  2256 				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2257 				return status;
       
  2258 			}
       
  2259 
       
  2260 			tripl->get_kc()->set_is_valid();
       
  2261 			tripl->get_kc()->set_data_length(SIM_KC_LENGTH);
       
  2262 
       
  2263 			status = tripl->get_sres()->init(SIM_SRES_LENGTH);
       
  2264 			if (status != eap_status_ok)
       
  2265 			{
       
  2266 				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2267 				return status;
       
  2268 			}
       
  2269 	
       
  2270 			tripl->get_sres()->set_is_valid();
       
  2271 			tripl->get_sres()->set_data_length(SIM_SRES_LENGTH);
       
  2272 	
       
  2273 			m_sim_algorithm.generate_kc_sres(
       
  2274 				m_simulator_sim_algorithm,
       
  2275 				test_rand,
       
  2276 				rand_length,
       
  2277 				tmp,
       
  2278 				SIM_KC_LENGTH + SIM_SRES_LENGTH);
       
  2279 	
       
  2280 			status = tripl->get_sres()->set_copy_of_buffer(tmp, SIM_SRES_LENGTH);
       
  2281 			if (status != eap_status_ok)
       
  2282 			{
       
  2283 				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2284 				return EAP_STATUS_RETURN(m_am_tools, status);
       
  2285 			}
       
  2286 			status = tripl->get_kc()->set_copy_of_buffer(tmp + SIM_SRES_LENGTH, SIM_KC_LENGTH);
       
  2287 			if (status != eap_status_ok)
       
  2288 			{
       
  2289 				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2290 				return EAP_STATUS_RETURN(m_am_tools, status);
       
  2291 			}
       
  2292 		}
       
  2293 	}
       
  2294 
       
  2295 #else
       
  2296 	EAP_UNREFERENCED_PARAMETER(triplets);
       
  2297 #endif // #if !defined (USE_EAP_GSMSIM_INTERFACE)
       
  2298 	
       
  2299 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2300 	return EAP_STATUS_RETURN(m_am_tools, status);
       
  2301 }
       
  2302 
       
  2303 #endif //#if defined(USE_EAP_TYPE_SERVER_GSMSIM)
       
  2304 
       
  2305 //--------------------------------------------------
       
  2306 
       
  2307 #if defined(USE_EAP_TYPE_SERVER_GSMSIM)
       
  2308 
       
  2309 //
       
  2310 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::cancel_SIM_triplets_query()
       
  2311 {
       
  2312 	EAP_TRACE_ERROR(m_am_tools, TRACE_FLAGS_DEFAULT, (EAPL("ERROR: cancel_SIM_triplets_query() not supported here..\n")));
       
  2313 	return EAP_STATUS_RETURN(m_am_tools, eap_status_not_supported);
       
  2314 }
       
  2315 
       
  2316 #endif //#if defined(USE_EAP_TYPE_SERVER_GSMSIM)
       
  2317 
       
  2318 
       
  2319 //--------------------------------------------------
       
  2320 
       
  2321 //
       
  2322 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::generate_encryption_IV(
       
  2323 	eap_variable_data_c * const encryption_IV,
       
  2324 		const u32_t IV_length)
       
  2325 {
       
  2326 	// EAP SIM server only.
       
  2327 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2328 
       
  2329 	eap_status_e status = encryption_IV->init(IV_length);
       
  2330 	if (status != eap_status_ok)
       
  2331 	{
       
  2332 		return EAP_STATUS_RETURN(m_am_tools, status);
       
  2333 	}
       
  2334 
       
  2335 	encryption_IV->set_is_valid();
       
  2336 	encryption_IV->set_data_length(IV_length);
       
  2337 
       
  2338 	m_am_tools->get_crypto()->add_rand_seed_hw_ticks();
       
  2339 
       
  2340 	status = m_am_tools->get_crypto()->get_rand_bytes(
       
  2341 		encryption_IV->get_data(encryption_IV->get_data_length()),
       
  2342 		encryption_IV->get_data_length());
       
  2343 
       
  2344 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2345 	return EAP_STATUS_RETURN(m_am_tools, status);
       
  2346 }
       
  2347 
       
  2348 //--------------------------------------------------
       
  2349 
       
  2350 #if defined(USE_EAP_TYPE_SERVER_GSMSIM)
       
  2351 
       
  2352 //
       
  2353 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::query_imsi_from_username(
       
  2354 	const bool must_be_synchronous,
       
  2355 	const u8_t next_eap_identifier,
       
  2356 	const eap_am_network_id_c * const network_id,
       
  2357 	const eap_variable_data_c * const username,
       
  2358 	eap_variable_data_c * const imsi,
       
  2359 	eap_type_gsmsim_identity_type * const identity_type,
       
  2360 	const eap_type_gsmsim_complete_e /* completion_action */)
       
  2361 {
       
  2362 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2363 	EAP_UNREFERENCED_PARAMETER(must_be_synchronous);
       
  2364 	EAP_UNREFERENCED_PARAMETER(next_eap_identifier);	
       
  2365 	EAP_UNREFERENCED_PARAMETER(network_id);	
       
  2366 	// Setup headers for comparison
       
  2367 	
       
  2368 	_LIT8(KReauthHeader, "RE");
       
  2369 	TBufC8<2> reauthHeader(KReauthHeader);	
       
  2370 
       
  2371 	_LIT8(KPseudonymHeader, "PS");
       
  2372 	TBufC8<2> pseudonymHeader(KPseudonymHeader);
       
  2373 
       
  2374 	if (username->get_data_length() >= 2)
       
  2375 	{
       
  2376 		TBuf8<2> head;
       
  2377 		head.Append(username->get_data(2), KIdentityHeaderLength);
       
  2378 		// Note this is syncronous call.
       
  2379 
       
  2380 		const u8_t * const username_prefix = username->get_data_offset(0u, 1u);
       
  2381 
       
  2382 		if (username->get_data_length() <= SIM_IMSI_LENGTH+1u
       
  2383 			&& username->get_data_length() > 1u
       
  2384 			&& username_prefix != 0
       
  2385 			&& *username_prefix == *GSMSIM_IMSI_PREFIX_CHARACTER)
       
  2386 		{
       
  2387 			// We have IMSI.
       
  2388 			eap_status_e status = imsi->set_copy_of_buffer(
       
  2389 				username->get_data_offset(1u, username->get_data_length()-1u),
       
  2390 				username->get_data_length()-1u);
       
  2391 			if (status != eap_status_ok)
       
  2392 			{
       
  2393 				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2394 				return EAP_STATUS_RETURN(m_am_tools, status);
       
  2395 			}
       
  2396 			
       
  2397 			*identity_type = GSMSIM_IDENTITY_TYPE_IMSI_ID;
       
  2398 
       
  2399 			EAP_TRACE_DATA_DEBUG(
       
  2400 				m_am_tools,
       
  2401 				TRACE_FLAGS_DEFAULT,
       
  2402 				(EAPL("IMSI"),
       
  2403 				 imsi->get_data(imsi->get_data_length()),
       
  2404 				 imsi->get_data_length()));
       
  2405 			
       
  2406 			EAP_TRACE_DEBUG(
       
  2407 				m_am_tools,
       
  2408 				TRACE_FLAGS_DEFAULT,
       
  2409 				(EAPL("query_imsi_from_username(): Used EAP-Identity type %d.\n"),
       
  2410 				 *identity_type));
       
  2411 			EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2412 			return EAP_STATUS_RETURN(m_am_tools, status);
       
  2413 		}
       
  2414 		else if (head == reauthHeader) 
       
  2415 		{
       
  2416 			eap_status_e status = imsi->add_data(
       
  2417 				username->get_data_offset(KIdentityHeaderLength,
       
  2418 										  SIM_IMSI_LENGTH),
       
  2419 				SIM_IMSI_LENGTH);
       
  2420 			if (status != eap_status_ok)
       
  2421 			{
       
  2422 				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2423 				return EAP_STATUS_RETURN(m_am_tools, status);
       
  2424 			}
       
  2425 			*identity_type = GSMSIM_IDENTITY_TYPE_RE_AUTH_ID;	
       
  2426 		}
       
  2427 		else if (head == pseudonymHeader) 
       
  2428 		{
       
  2429 			eap_status_e status = imsi->add_data(
       
  2430 				username->get_data_offset(
       
  2431 					KIdentityHeaderLength,
       
  2432 					SIM_IMSI_LENGTH),
       
  2433 				SIM_IMSI_LENGTH);
       
  2434 			if (status != eap_status_ok)
       
  2435 			{
       
  2436 				EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2437 				return EAP_STATUS_RETURN(m_am_tools, status);
       
  2438 			}
       
  2439 			*identity_type = GSMSIM_IDENTITY_TYPE_PSEUDONYM_ID;
       
  2440 		}
       
  2441 		else
       
  2442 		{
       
  2443 			*identity_type = GSMSIM_IDENTITY_TYPE_NONE;		
       
  2444 		}
       
  2445 	}
       
  2446 	else
       
  2447 	{
       
  2448 		*identity_type = GSMSIM_IDENTITY_TYPE_NONE;		
       
  2449 	}
       
  2450 
       
  2451 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2452 	return eap_status_ok;
       
  2453 
       
  2454 }
       
  2455 
       
  2456 #endif //#if defined(USE_EAP_TYPE_SERVER_GSMSIM)
       
  2457 
       
  2458 //--------------------------------------------------
       
  2459 
       
  2460 //
       
  2461 eap_status_e eap_am_type_gsmsim_symbian_c::cancel_imsi_from_username_query()
       
  2462 {
       
  2463 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2464 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2465 	return EAP_STATUS_RETURN(m_am_tools, eap_status_not_supported);
       
  2466 }
       
  2467 
       
  2468 //--------------------------------------------------
       
  2469 
       
  2470 //
       
  2471 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::check_is_rand_unused(
       
  2472 	const eap_variable_data_c * const /* n_rands */)
       
  2473 {
       
  2474 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2475 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2476 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
  2477 }
       
  2478 
       
  2479 //--------------------------------------------------
       
  2480 
       
  2481 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::set_rand_is_used(
       
  2482 	const eap_variable_data_c * const /* n_rands */)
       
  2483 {
       
  2484 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2485 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2486 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
  2487 }
       
  2488 
       
  2489 //--------------------------------------------------
       
  2490 
       
  2491 //
       
  2492 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::generate_reauthentication_id(
       
  2493 	const eap_am_network_id_c * const network_id,
       
  2494 	const eap_variable_data_c * const imsi,
       
  2495 	eap_variable_data_c * const reauthentication_identity,
       
  2496 	const u32_t maximum_reauthentication_identity_length)
       
  2497 {
       
  2498 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2499 	eap_status_e status(eap_status_process_general_error);
       
  2500 	_LIT8(KTmp, "RE");
       
  2501 	TBufC8<KIdentityHeaderLength> header(KTmp);
       
  2502 	reauthentication_identity->reset();
       
  2503 	status = reauthentication_identity->add_data(header.Ptr(), KIdentityHeaderLength);
       
  2504 	if (status != eap_status_ok)
       
  2505 	{
       
  2506 		reauthentication_identity->reset();
       
  2507 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2508 		return EAP_STATUS_RETURN(m_am_tools, status);
       
  2509 	}
       
  2510 	status = generate_identity(network_id, imsi, reauthentication_identity, 
       
  2511 							   maximum_reauthentication_identity_length);
       
  2512 	if (status != eap_status_ok)
       
  2513 	{
       
  2514 		reauthentication_identity->reset();
       
  2515 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2516 		return EAP_STATUS_RETURN(m_am_tools, status);
       
  2517 	}
       
  2518 
       
  2519 	status = reauthentication_identity->add_data(GSMSIM_AT_CHARACTER, GSMSIM_AT_CHARACTER_LENGTH);
       
  2520 	if (status != eap_status_ok)
       
  2521 	{
       
  2522 		reauthentication_identity->reset();
       
  2523 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2524 		return EAP_STATUS_RETURN(m_am_tools, status);
       
  2525 	}
       
  2526 
       
  2527 	status = reauthentication_identity->add_data(&m_nai_realm);
       
  2528 	if (status != eap_status_ok)
       
  2529 	{
       
  2530 		reauthentication_identity->reset();
       
  2531 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2532 		return EAP_STATUS_RETURN(m_am_tools, status);
       
  2533 	}
       
  2534 
       
  2535 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2536 	return EAP_STATUS_RETURN(m_am_tools, status);
       
  2537 }
       
  2538 
       
  2539 //--------------------------------------------------
       
  2540 
       
  2541 //
       
  2542 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::generate_pseudonym_id(
       
  2543 	const eap_am_network_id_c * const network_id,
       
  2544 	const eap_variable_data_c * const imsi,
       
  2545 	eap_variable_data_c * const pseudonym,
       
  2546 	const u32_t maximum_pseudonym_length)
       
  2547 {
       
  2548 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2549 	eap_status_e status(eap_status_process_general_error);
       
  2550 	_LIT8(KTmp, "PS");
       
  2551 	TBufC8<KIdentityHeaderLength> header(KTmp);	
       
  2552 	status = pseudonym->add_data(header.Ptr(), KIdentityHeaderLength);
       
  2553 	if (status != eap_status_ok)
       
  2554 	{
       
  2555 		pseudonym->reset();
       
  2556 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2557 		return EAP_STATUS_RETURN(m_am_tools, status);
       
  2558 	}
       
  2559 	status = generate_identity(network_id, imsi, pseudonym, maximum_pseudonym_length);
       
  2560 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2561 	return EAP_STATUS_RETURN(m_am_tools, status);
       
  2562 }
       
  2563 
       
  2564 eap_status_e eap_am_type_gsmsim_symbian_c::generate_identity(
       
  2565 	const eap_am_network_id_c * const /* network_id */,
       
  2566 	const eap_variable_data_c * const imsi,
       
  2567 	eap_variable_data_c * const identity,
       
  2568 	const u32_t /*maximum_identity_length*/)
       
  2569 {
       
  2570 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2571 	eap_status_e status = identity->add_data(imsi);
       
  2572 
       
  2573 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2574 	return EAP_STATUS_RETURN(m_am_tools, status);
       
  2575 }
       
  2576 
       
  2577 
       
  2578 //
       
  2579 EAP_FUNC_EXPORT eap_status_e eap_am_type_gsmsim_symbian_c::type_configure_write(
       
  2580 	const eap_configuration_field_c * const field,
       
  2581 	eap_variable_data_c * const data)
       
  2582 {
       
  2583 	// NOTE: At the moment this is not called anywhere.
       
  2584 	// NOTE: This is really just for simulation.
       
  2585 	// Write is routed to partner object.
       
  2586 	eap_status_e status = m_partner->write_configure(
       
  2587 			field,
       
  2588 			data);
       
  2589 	return status;
       
  2590 }
       
  2591 
       
  2592 //--------------------------------------------------
       
  2593 
       
  2594 bool eap_am_type_gsmsim_symbian_c::is_session_valid()
       
  2595 {
       
  2596 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2597 	
       
  2598 	bool sessionValidity(false);
       
  2599 	
       
  2600 	TRAPD(err, sessionValidity = is_session_validL());
       
  2601 	if (err != KErrNone) 
       
  2602 	{
       
  2603 		EAP_TRACE_ERROR(m_am_tools, 
       
  2604 			TRACE_FLAGS_DEFAULT, (
       
  2605 			EAPL("eap_am_type_gsmsim_symbian_c::is_session_valid - LEAVE - error=%d, Assuming session is invalid \n"),
       
  2606 			err));
       
  2607 			
       
  2608 		sessionValidity = false;
       
  2609 	}
       
  2610 	 		
       
  2611 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2612 	
       
  2613 	return sessionValidity;
       
  2614 }
       
  2615 
       
  2616 //--------------------------------------------------
       
  2617 
       
  2618 bool eap_am_type_gsmsim_symbian_c::is_session_validL()
       
  2619 {
       
  2620 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2621 
       
  2622 	EAP_TRACE_DEBUG(m_am_tools, TRACE_FLAGS_DEFAULT, (EAPL("eap_am_type_gsmsim_symbian_c::is_session_validL - start \n")));
       
  2623 
       
  2624 	HBufC* buf = HBufC::NewLC(KMaxSqlQueryLength);
       
  2625 	TPtr sqlStatement = buf->Des();
       
  2626 	
       
  2627 	// Query all the relevant parameters
       
  2628 	_LIT(KSQLQuery, "SELECT %S, %S FROM %S WHERE %S=%d AND %S=%d AND %S=%d");
       
  2629 	sqlStatement.Format(KSQLQuery, &cf_str_EAP_GSMSIM_max_session_validity_time_literal,
       
  2630 						&KGSMSIMLastFullAuthTime, &KSimTableName,
       
  2631 						&KServiceType, m_index_type, 
       
  2632 						&KServiceIndex, m_index, &KTunnelingType, m_tunneling_vendor_type);
       
  2633 
       
  2634 	RDbView view;
       
  2635 	// Evaluate view
       
  2636 	User::LeaveIfError(view.Prepare(m_database, TDbQuery(sqlStatement)));
       
  2637 	CleanupClosePushL(view);
       
  2638 	User::LeaveIfError(view.EvaluateAll());
       
  2639 	
       
  2640 	// Get the first (and only) row
       
  2641 	view.FirstL();
       
  2642 	view.GetL();
       
  2643 	
       
  2644 	// Get column set so we get the correct column numbers
       
  2645 	CDbColSet* colSet = view.ColSetL();
       
  2646 	CleanupStack::PushL(colSet);
       
  2647 		
       
  2648 	TInt64 maxSessionTime = view.ColInt64(colSet->ColNo(cf_str_EAP_GSMSIM_max_session_validity_time_literal));
       
  2649 	TInt64 fullAuthTime = view.ColInt64(colSet->ColNo(KGSMSIMLastFullAuthTime));
       
  2650 
       
  2651 	CleanupStack::PopAndDestroy(colSet); // Delete colSet.
       
  2652 	CleanupStack::PopAndDestroy(&view); // Close view.
       
  2653 	CleanupStack::PopAndDestroy(buf); // Delete buf.
       
  2654 	
       
  2655 	// If the max session time from DB is zero then we use the 
       
  2656 	// one read from configuration file.
       
  2657 	
       
  2658 	if( maxSessionTime == 0)
       
  2659 	{
       
  2660 		EAP_TRACE_DEBUG(m_am_tools, 
       
  2661 			TRACE_FLAGS_DEFAULT, (
       
  2662 			EAPL("Session Validity - Using max session validity time from config file\n")));
       
  2663 	
       
  2664 		maxSessionTime = m_max_session_time; // value from configuration file.
       
  2665 	}
       
  2666 		
       
  2667 	// Get the current time.
       
  2668 	TTime currentTime;
       
  2669 	currentTime.UniversalTime();
       
  2670 	
       
  2671 	TTime lastFullAuthTime(fullAuthTime);
       
  2672 	
       
  2673 #if defined(_DEBUG) || defined(DEBUG)	
       
  2674 	
       
  2675 	TDateTime currentDateTime = currentTime.DateTime();
       
  2676 	
       
  2677 	TDateTime fullAuthDateTime = lastFullAuthTime.DateTime();
       
  2678 	
       
  2679 	EAP_TRACE_DEBUG(m_am_tools, TRACE_FLAGS_DEFAULT,
       
  2680 	(EAPL("Session Validity - Current Time,        %2d-%2d-%4d : %2d-%2d-%2d-%d\n"), 
       
  2681 	currentDateTime.Day()+1, currentDateTime.Month()+1, currentDateTime.Year(), currentDateTime.Hour(),
       
  2682 	currentDateTime.Minute(), currentDateTime.Second(), currentDateTime.MicroSecond()));
       
  2683 
       
  2684 	EAP_TRACE_DEBUG(m_am_tools, TRACE_FLAGS_DEFAULT,
       
  2685 	(EAPL("Session Validity - Last Full Auth Time, %2d-%2d-%4d : %2d-%2d-%2d-%d\n"), 
       
  2686 	fullAuthDateTime.Day()+1, fullAuthDateTime.Month()+1, fullAuthDateTime.Year(), fullAuthDateTime.Hour(),
       
  2687 	fullAuthDateTime.Minute(), fullAuthDateTime.Second(), fullAuthDateTime.MicroSecond()));
       
  2688 
       
  2689 #endif
       
  2690 
       
  2691 	TTimeIntervalMicroSeconds interval = currentTime.MicroSecondsFrom(lastFullAuthTime);
       
  2692 		
       
  2693 	EAP_TRACE_DATA_DEBUG( m_am_tools, TRACE_FLAGS_DEFAULT,(EAPL("eap_am_type_gsmsim_symbian_c::is_session_validL:interval in microseconds:"),
       
  2694 			&(interval.Int64()),
       
  2695 			sizeof(interval.Int64()) ) );
       
  2696 			
       
  2697 	EAP_TRACE_DATA_DEBUG( m_am_tools, TRACE_FLAGS_DEFAULT,(EAPL("eap_am_type_gsmsim_symbian_c::is_session_validL:max session time in microseconds:"),
       
  2698 			&(maxSessionTime),
       
  2699 			sizeof(maxSessionTime) ) );
       
  2700 			
       
  2701 	
       
  2702 #if defined(_DEBUG) || defined(DEBUG)
       
  2703 
       
  2704 	TTimeIntervalMinutes intervalMins;
       
  2705 	TInt error = currentTime.MinutesFrom(lastFullAuthTime, intervalMins);
       
  2706 	
       
  2707 	if(error == KErrNone)
       
  2708 	{
       
  2709 		EAP_TRACE_DEBUG(
       
  2710 			m_am_tools,
       
  2711 			TRACE_FLAGS_DEFAULT,
       
  2712 			(EAPL("eap_am_type_gsmsim_symbian_c::is_session_validL()")
       
  2713 			 EAPL("interval in Minutes =%d\n"),
       
  2714 			 intervalMins.Int()));
       
  2715 	}
       
  2716 	
       
  2717 #endif
       
  2718 
       
  2719 
       
  2720 	if( maxSessionTime >= interval.Int64() )
       
  2721 	{
       
  2722 		EAP_TRACE_DEBUG(m_am_tools, TRACE_FLAGS_DEFAULT, (EAPL("eap_am_type_gsmsim_symbian_c::is_session_validL - Session Valid \n")));
       
  2723 
       
  2724 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);			
       
  2725 
       
  2726 		return true;	
       
  2727 	}
       
  2728 	else
       
  2729 	{
       
  2730 		EAP_TRACE_DEBUG(m_am_tools, TRACE_FLAGS_DEFAULT, (EAPL("eap_am_type_gsmsim_symbian_c::is_session_validL - Session NOT Valid \n")));
       
  2731 
       
  2732 		EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);			
       
  2733 		
       
  2734 		return false;	
       
  2735 	}
       
  2736 }
       
  2737 
       
  2738 //--------------------------------------------------
       
  2739 
       
  2740 void eap_am_type_gsmsim_symbian_c::store_authentication_timeL()
       
  2741 {
       
  2742 	EAP_TRACE_BEGIN(m_am_tools, TRACE_FLAGS_DEFAULT);
       
  2743 	
       
  2744 	EAP_TRACE_DEBUG(m_am_tools, TRACE_FLAGS_DEFAULT, (EAPL("eap_am_type_gsmsim_symbian_c::store_authentication_timeL - start \n")));	
       
  2745 
       
  2746 	HBufC* buf = HBufC::NewLC(KMaxSqlQueryLength);
       
  2747 	TPtr sqlStatement = buf->Des();
       
  2748 	
       
  2749 	// Query all the relevant parameters
       
  2750 	_LIT(KSQLQuery, "SELECT %S FROM %S WHERE %S=%d AND %S=%d AND %S=%d");
       
  2751 	sqlStatement.Format(KSQLQuery, &KGSMSIMLastFullAuthTime, &KSimTableName,
       
  2752 						&KServiceType, m_index_type, 
       
  2753 						&KServiceIndex, m_index, &KTunnelingType, m_tunneling_vendor_type);
       
  2754 
       
  2755 	RDbView view;
       
  2756 	// Evaluate view
       
  2757 	User::LeaveIfError(view.Prepare(m_database, TDbQuery(sqlStatement), TDbWindow::EUnlimited));
       
  2758 	CleanupClosePushL(view);
       
  2759 	User::LeaveIfError(view.EvaluateAll());
       
  2760 	
       
  2761 	// Get the first (and only) row for updation.
       
  2762 	view.FirstL();
       
  2763 	view.UpdateL();
       
  2764 	
       
  2765 	// Get column set so we get the correct column numbers
       
  2766 	CDbColSet* colSet = view.ColSetL();
       
  2767 	CleanupStack::PushL(colSet);
       
  2768 
       
  2769 	// Get the current universal time.
       
  2770 	TTime currentTime;
       
  2771 	currentTime.UniversalTime();
       
  2772 		
       
  2773 #if defined(_DEBUG) || defined(DEBUG)	
       
  2774 	
       
  2775 	TDateTime currentDateTime = currentTime.DateTime();
       
  2776 	
       
  2777 	EAP_TRACE_DEBUG(m_am_tools, TRACE_FLAGS_DEFAULT,
       
  2778 	(EAPL("eap_am_type_gsmsim_symbian_c::store_authentication_time, %2d-%2d-%4d : %2d-%2d-%2d-%d\n"), 
       
  2779 	currentDateTime.Day()+1, currentDateTime.Month()+1,currentDateTime.Year(), currentDateTime.Hour(),
       
  2780 	currentDateTime.Minute(), currentDateTime.Second(), currentDateTime.MicroSecond()));
       
  2781 
       
  2782 #endif
       
  2783 
       
  2784 	TInt64 fullAuthTime = currentTime.Int64();
       
  2785 	
       
  2786 	view.SetColL(colSet->ColNo(KGSMSIMLastFullAuthTime), fullAuthTime);
       
  2787 
       
  2788 	view.PutL();	
       
  2789 
       
  2790 	CleanupStack::PopAndDestroy(colSet); // Delete colSet.
       
  2791 	CleanupStack::PopAndDestroy(&view); // Close view.
       
  2792 	CleanupStack::PopAndDestroy(buf); // Delete buf.
       
  2793 
       
  2794 	EAP_TRACE_END(m_am_tools, TRACE_FLAGS_DEFAULT);			
       
  2795 }
       
  2796 
       
  2797 //--------------------------------------------------
       
  2798 
       
  2799 //  End of File