eapol/eapol_framework/eapol_common/type/gsmsim/core/eap_type_gsmsim_header.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 78 
       
    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 
       
    29 #include "eap_type_gsmsim_header.h"
       
    30 #include "eap_header_string.h"
       
    31 
       
    32 
       
    33 /** @file */
       
    34 
       
    35 
       
    36 // 
       
    37 EAP_FUNC_EXPORT gsmsim_payload_AT_header_c::~gsmsim_payload_AT_header_c()
       
    38 {
       
    39 }
       
    40 
       
    41 // 
       
    42 EAP_FUNC_EXPORT gsmsim_payload_AT_header_c::gsmsim_payload_AT_header_c(
       
    43 	abs_eap_am_tools_c * const tools,
       
    44 	void * const header_buffer,
       
    45 	const u32_t header_buffer_length)
       
    46 	: eap_general_header_base_c(tools, header_buffer, header_buffer_length)
       
    47 	  , m_am_tools(tools)
       
    48 {
       
    49 }
       
    50 
       
    51 EAP_FUNC_EXPORT gsmsim_payload_AT_type_e gsmsim_payload_AT_header_c::get_current_payload() const
       
    52 {
       
    53 	const u8_t * const payload_data = get_header_offset(m_type_offset, sizeof(u8_t));
       
    54 	if (payload_data != 0)
       
    55 	{
       
    56 		return static_cast<gsmsim_payload_AT_type_e>(*payload_data);
       
    57 	}
       
    58 	else
       
    59 	{
       
    60 		return gsmsim_payload_NONE;
       
    61 	}
       
    62 }
       
    63 
       
    64 EAP_FUNC_EXPORT u16_t gsmsim_payload_AT_header_c::get_payload_length() const
       
    65 {
       
    66 	const u8_t * const length_data = get_header_offset(m_length_offset, sizeof(u8_t));
       
    67 	if (length_data != 0)
       
    68 	{
       
    69 		return static_cast<u16_t>(*length_data * 4u);
       
    70 	}
       
    71 	else
       
    72 	{
       
    73 		return 0ul;
       
    74 	}
       
    75 }
       
    76 
       
    77 EAP_FUNC_EXPORT u16_t gsmsim_payload_AT_header_c::get_reserved() const
       
    78 {
       
    79 	const u8_t * const reserved = get_header_offset(m_reserved_offset, sizeof(u16_t));
       
    80 	if (reserved != 0)
       
    81 	{
       
    82 		return eap_read_u16_t_network_order(reserved, sizeof(u16_t));
       
    83 	}
       
    84 	else
       
    85 	{
       
    86 		// this is illegal reserved field value.
       
    87 		return 0xffff;
       
    88 	}
       
    89 }
       
    90 
       
    91 EAP_FUNC_EXPORT u32_t gsmsim_payload_AT_header_c::get_data_length() const
       
    92 {
       
    93 	if (get_payload_length() > get_header_length())
       
    94 		return static_cast<u32_t>(get_payload_length()-get_header_length());
       
    95 	else
       
    96 		return 0;
       
    97 }
       
    98 
       
    99 EAP_FUNC_EXPORT u16_t gsmsim_payload_AT_header_c::get_header_length()
       
   100 {
       
   101 	return m_data_offset;
       
   102 }
       
   103 
       
   104 EAP_FUNC_EXPORT u16_t gsmsim_payload_AT_header_c::get_max_payload_data_length()
       
   105 {
       
   106 	return static_cast<u16_t>((0xff*4u-get_header_length()));
       
   107 }
       
   108 
       
   109 EAP_FUNC_EXPORT u8_t * gsmsim_payload_AT_header_c::get_data(const u32_t contignuous_bytes) const
       
   110 {
       
   111 	if (get_data_length() >= contignuous_bytes
       
   112 		&& contignuous_bytes > 0)
       
   113 	{
       
   114 		return get_header_offset(m_data_offset, contignuous_bytes); // Data begins after the header.
       
   115 	}
       
   116 	else
       
   117 	{
       
   118 		EAP_ASSERT(
       
   119 			get_data_length() >= contignuous_bytes
       
   120 			&& contignuous_bytes > 0);
       
   121 	}
       
   122 	return 0;
       
   123 }
       
   124 
       
   125 EAP_FUNC_EXPORT u8_t * gsmsim_payload_AT_header_c::get_data_offset(const u32_t offset, const u32_t contignuous_bytes) const
       
   126 {
       
   127 	if (get_header_buffer_length()-get_header_length() >= offset+contignuous_bytes
       
   128 		&& contignuous_bytes > 0)
       
   129 	{
       
   130 		u8_t * const data = get_header_offset(m_data_offset, contignuous_bytes);
       
   131 		if (data != 0)
       
   132 		{
       
   133 			return data+offset; // Data begins after the header.
       
   134 		}
       
   135 		else
       
   136 		{
       
   137 			return 0;
       
   138 		}
       
   139 	}
       
   140 	else
       
   141 	{
       
   142 		EAP_ASSERT(
       
   143 			get_data_length() >= offset+contignuous_bytes
       
   144 			&& contignuous_bytes > 0);
       
   145 	}
       
   146 	return 0;
       
   147 }
       
   148 
       
   149 EAP_FUNC_EXPORT u8_t * gsmsim_payload_AT_header_c::get_next_header() const
       
   150 {
       
   151 	if (get_header_buffer_length() >= get_data_length()+2ul*get_header_length())
       
   152 	{
       
   153 		return get_data_offset(get_data_length(), get_header_length());
       
   154 	}
       
   155 	else
       
   156 	{
       
   157 		return 0;
       
   158 	}
       
   159 }
       
   160 
       
   161 // Mostly this is zero.
       
   162 // With some attributes this is used for special purposes.
       
   163 EAP_FUNC_EXPORT void gsmsim_payload_AT_header_c::set_reserved(
       
   164 	const u16_t reserved)
       
   165 {
       
   166 	u8_t *reserved_data = get_header_offset(m_reserved_offset, sizeof(u16_t));
       
   167 	
       
   168 	EAP_ASSERT(reserved_data != 0);
       
   169 	
       
   170 	reserved_data[0] = static_cast<u8_t>(((reserved & 0xff00) >> 8));
       
   171 	reserved_data[1] = static_cast<u8_t>((reserved & 0x00ff));
       
   172 }
       
   173 
       
   174 EAP_FUNC_EXPORT void gsmsim_payload_AT_header_c::set_current_payload(
       
   175 	const gsmsim_payload_AT_type_e p_current_payload)
       
   176 {
       
   177 	EAP_ASSERT_ALWAYS(p_current_payload == static_cast<gsmsim_payload_AT_type_e>(static_cast<u8_t>(p_current_payload)));
       
   178 
       
   179 	u8_t * const payload_type = get_header_offset(m_type_offset, sizeof(u8_t));
       
   180 
       
   181 	EAP_ASSERT(payload_type != 0);
       
   182 	
       
   183 	*payload_type = static_cast<u8_t>(p_current_payload);
       
   184 }
       
   185 
       
   186 EAP_FUNC_EXPORT void gsmsim_payload_AT_header_c::set_data_length(const u16_t p_data_length)
       
   187 {
       
   188 	u32_t total_length = p_data_length+gsmsim_payload_AT_header_c::get_header_length();
       
   189 	u32_t remaider = total_length % 4u;
       
   190 
       
   191 	EAP_ASSERT(remaider == 0);
       
   192 	EAP_ASSERT((total_length / 4u) <= 0xff);
       
   193 	
       
   194 	EAP_UNREFERENCED_PARAMETER(remaider);
       
   195 	
       
   196 	u8_t * const length_data = get_header_offset(m_length_offset, sizeof(u8_t));
       
   197 	
       
   198 	EAP_ASSERT(length_data != 0);
       
   199 	
       
   200 	*length_data = static_cast<u8_t>(total_length/4u);
       
   201 }
       
   202 
       
   203 EAP_FUNC_EXPORT void gsmsim_payload_AT_header_c::reset_header(const u16_t data_length)
       
   204 {
       
   205 	set_reserved(0u);
       
   206 	set_current_payload(gsmsim_payload_NONE);
       
   207 	set_data_length(data_length);
       
   208 }
       
   209 
       
   210 EAP_FUNC_EXPORT eap_const_string gsmsim_payload_AT_header_c::get_payload_AT_string(
       
   211 	const gsmsim_payload_AT_type_e payload_type)
       
   212 {
       
   213 
       
   214 #if defined(USE_EAP_TRACE_STRINGS)
       
   215 	EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_NONE)
       
   216 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_RAND)
       
   217 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_PADDING)
       
   218 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_NONCE_MT)
       
   219 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_PERMANENT_ID_REQ)
       
   220 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_MAC)
       
   221 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_NOTIFICATION)
       
   222 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_ANY_ID_REQ)
       
   223 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_IDENTITY)
       
   224 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_VERSION_LIST)
       
   225 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_SELECTED_VERSION)
       
   226 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_FULLAUTH_ID_REQ)
       
   227 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_COUNTER)
       
   228 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_COUNTER_TOO_SMALL)
       
   229 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_NONCE_S)
       
   230 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_CLIENT_ERROR_CODE)
       
   231 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_IV)
       
   232 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_ENCR_DATA)
       
   233 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_NEXT_PSEUDONYM)
       
   234 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_NEXT_REAUTH_ID)
       
   235 	else EAP_IF_RETURN_STRING(payload_type, gsmsim_payload_AT_RESULT_IND)
       
   236 	else
       
   237 #endif // #if defined(USE_EAP_TRACE_STRINGS)
       
   238 	{
       
   239 		EAP_UNREFERENCED_PARAMETER(payload_type);
       
   240 		return EAPL("Unknown GSMSIM payload AT");
       
   241 	}
       
   242 }
       
   243 
       
   244 EAP_FUNC_EXPORT eap_const_string gsmsim_payload_AT_header_c::get_payload_AT_string() const
       
   245 {
       
   246 	return get_payload_AT_string(get_current_payload());
       
   247 }
       
   248 
       
   249 EAP_FUNC_EXPORT eap_status_e gsmsim_payload_AT_header_c::check_header() const
       
   250 {
       
   251 	if (get_reserved() != 0)
       
   252 	{
       
   253 		return EAP_STATUS_RETURN(m_am_tools, eap_status_header_corrupted);
       
   254 	}
       
   255 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   256 }
       
   257 
       
   258 
       
   259 //----------------------------------------------------------------------------
       
   260 
       
   261 
       
   262 // 
       
   263 EAP_FUNC_EXPORT gsmsim_header_c::~gsmsim_header_c()
       
   264 {
       
   265 }
       
   266 
       
   267 // 
       
   268 EAP_FUNC_EXPORT gsmsim_header_c::gsmsim_header_c(
       
   269 	abs_eap_am_tools_c * const tools,
       
   270 	u8_t * const header_buffer,
       
   271 	const u32_t header_buffer_length)
       
   272 	: eap_header_base_c(tools, header_buffer, header_buffer_length)
       
   273 	, m_am_tools(tools)
       
   274 {
       
   275 }
       
   276 
       
   277 EAP_FUNC_EXPORT gsmsim_subtype_e gsmsim_header_c::get_subtype() const
       
   278 {
       
   279 	u32_t subtype_offset(get_sub_type_offset());
       
   280 
       
   281 	const u8_t * const subtype_data = get_header_offset(subtype_offset, sizeof(u8_t));
       
   282 	if (subtype_data != 0)
       
   283 	{
       
   284 		return static_cast<gsmsim_subtype_e>(*subtype_data);
       
   285 	}
       
   286 	else
       
   287 	{
       
   288 		return gsmsim_subtype_NONE;
       
   289 	}
       
   290 }
       
   291 
       
   292 EAP_FUNC_EXPORT u16_t gsmsim_header_c::get_data_length() const
       
   293 {
       
   294 	if (get_length() > static_cast<u16_t>(get_header_length()))
       
   295 	{
       
   296 		return static_cast<u16_t>(get_length()-static_cast<u16_t>(get_header_length()));
       
   297 	}
       
   298 	else
       
   299 	{
       
   300 		return 0;
       
   301 	}
       
   302 }
       
   303 
       
   304 u32_t gsmsim_header_c::get_sub_type_offset() const
       
   305 {
       
   306 	return eap_header_base_c::get_header_length() + eap_header_base_c::get_type_field_length() + m_subtype_delta_offset;
       
   307 }
       
   308 
       
   309 EAP_FUNC_EXPORT u32_t gsmsim_header_c::get_header_length() const
       
   310 {
       
   311 	return get_sub_type_offset() + m_data_delta_offset;
       
   312 }
       
   313 
       
   314 EAP_FUNC_EXPORT u8_t * gsmsim_header_c::get_data_offset(const u32_t offset, const u32_t contignuous_bytes) const
       
   315 {
       
   316 	u32_t data_length = get_data_length();
       
   317 
       
   318 	if (data_length >= offset+contignuous_bytes
       
   319 		&& contignuous_bytes > 0u)
       
   320 	{
       
   321 		return (get_header_offset(get_header_length(), contignuous_bytes)+offset); // Data begins after the header.
       
   322 	}
       
   323 	else
       
   324 	{
       
   325 		EAP_ASSERT(get_data_length() > 0u);
       
   326 	}
       
   327 	return 0;
       
   328 }
       
   329 
       
   330 
       
   331 EAP_FUNC_EXPORT u8_t * gsmsim_header_c::get_data(const u32_t contignuous_bytes) const
       
   332 {
       
   333 	return get_data_offset(0u, contignuous_bytes);
       
   334 }
       
   335 
       
   336 
       
   337 EAP_FUNC_EXPORT u16_t gsmsim_header_c::get_reserved() const
       
   338 {
       
   339 	u32_t reserved_offset(get_sub_type_offset()+m_reserved_delta_offset);
       
   340 
       
   341 	u8_t * const reserved_data = get_header_offset(reserved_offset, sizeof(u16_t));
       
   342 	if (reserved_data != 0)
       
   343 	{
       
   344 		return eap_read_u16_t_network_order(reserved_data, sizeof(u16_t));
       
   345 	}
       
   346 	else
       
   347 	{
       
   348 		// This is illegal reserved data value.
       
   349 		return 0xffff;
       
   350 	}
       
   351 }
       
   352 
       
   353 
       
   354 EAP_FUNC_EXPORT eap_status_e gsmsim_header_c::check_header() const
       
   355 {
       
   356 	if (get_type() != eap_type_gsmsim)
       
   357 	{
       
   358 		return EAP_STATUS_RETURN(m_am_tools, eap_status_header_corrupted);
       
   359 	}
       
   360 	else if (get_reserved() != static_cast<u16_t>(0ul))
       
   361 	{
       
   362 		return EAP_STATUS_RETURN(m_am_tools, eap_status_header_corrupted);
       
   363 	}
       
   364 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   365 }
       
   366 
       
   367 EAP_FUNC_EXPORT eap_const_string gsmsim_header_c::get_subtype_string() const
       
   368 {
       
   369 
       
   370 #if defined(USE_EAP_TRACE_STRINGS)
       
   371 	const gsmsim_subtype_e packet_type = get_subtype();
       
   372 
       
   373 	EAP_IF_RETURN_STRING(packet_type, gsmsim_subtype_NONE)
       
   374 	else EAP_IF_RETURN_STRING(packet_type, gsmsim_subtype_Start)
       
   375 	else EAP_IF_RETURN_STRING(packet_type, gsmsim_subtype_Challenge)
       
   376 	else EAP_IF_RETURN_STRING(packet_type, gsmsim_subtype_Notification)
       
   377 	else EAP_IF_RETURN_STRING(packet_type, gsmsim_subtype_Re_authentication)
       
   378 	else EAP_IF_RETURN_STRING(packet_type, gsmsim_subtype_Client_Error)
       
   379 	else
       
   380 #endif // #if defined(USE_EAP_TRACE_STRINGS)
       
   381 	{
       
   382 		return EAPL("Unknown GSMSIM-type");
       
   383 	}
       
   384 }
       
   385 
       
   386 EAP_FUNC_EXPORT eap_const_string gsmsim_header_c::get_code_string() const
       
   387 {
       
   388 	return eap_header_string_c::get_eap_code_string(get_code());
       
   389 }
       
   390 
       
   391 EAP_FUNC_EXPORT eap_const_string gsmsim_header_c::get_eap_type_string() const
       
   392 {
       
   393 	if (get_length() <= eap_header_base_c::get_header_length())
       
   394 	{
       
   395 		return EAPL("No EAP-type");
       
   396 	}
       
   397 
       
   398 	return eap_header_string_c::get_eap_type_string(get_type());
       
   399 }
       
   400 
       
   401 EAP_FUNC_EXPORT void gsmsim_header_c::set_reserved(const u16_t reserved)
       
   402 {
       
   403 	u32_t reserved_offset(get_sub_type_offset()+m_reserved_delta_offset);
       
   404 	u8_t *reserved_data = get_header_offset(reserved_offset, sizeof(u16_t));
       
   405 
       
   406 	EAP_ASSERT(reserved_data != 0);
       
   407 
       
   408 	reserved_data[0] = static_cast<u8_t>((reserved & 0xff00) >> 8);
       
   409 	reserved_data[1] = static_cast<u8_t>(reserved & 0x00ff);
       
   410 }
       
   411 
       
   412 EAP_FUNC_EXPORT void gsmsim_header_c::set_subtype(const gsmsim_subtype_e p_subtype)
       
   413 {
       
   414 	u32_t subtype_offset(get_sub_type_offset()+m_subtype_delta_offset);
       
   415 	u8_t * const subtype_data = get_header_offset(subtype_offset, sizeof(u8_t));
       
   416 
       
   417 	EAP_ASSERT(subtype_data != 0);
       
   418 	
       
   419 	*subtype_data = static_cast<u8_t>(p_subtype);
       
   420 }
       
   421 
       
   422 EAP_FUNC_EXPORT void gsmsim_header_c::set_data_length(
       
   423 	const u32_t p_data_length,
       
   424 	const bool expanded_type_when_true)
       
   425 {
       
   426 	EAP_ASSERT_ALWAYS(p_data_length+get_header_length() <= 0xffff);
       
   427 	
       
   428 	set_length(
       
   429 		static_cast<u16_t>(p_data_length+get_header_length()),
       
   430 		expanded_type_when_true);
       
   431 }
       
   432 
       
   433 EAP_FUNC_EXPORT void gsmsim_header_c::reset_header(
       
   434 	const u32_t buffer_length,
       
   435 	const bool expanded_type_when_true)
       
   436 {
       
   437 	eap_header_base_c::set_length(
       
   438 		static_cast<u16_t>(buffer_length),
       
   439 		expanded_type_when_true);
       
   440 
       
   441 	set_code(eap_code_none);
       
   442 	set_identifier(0u);
       
   443 
       
   444 	set_type(
       
   445 		eap_type_gsmsim,
       
   446 		expanded_type_when_true); // SIM = 18
       
   447 	
       
   448 	set_subtype(gsmsim_subtype_NONE);
       
   449 	set_reserved(0u);
       
   450 }
       
   451 
       
   452 
       
   453 
       
   454 
       
   455 // End.