eapol/eapol_framework/eapol_common/type/tls_peap/eap/src/eap_type_tls_peap_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 120 
       
    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_tls_peap_header.h"
       
    30 #include "eap_header_string.h"
       
    31 
       
    32 /** @file */
       
    33 
       
    34 EAP_FUNC_EXPORT eap_tls_peap_header_c::~eap_tls_peap_header_c()
       
    35 {
       
    36 }
       
    37 
       
    38 // 
       
    39 EAP_FUNC_EXPORT eap_tls_peap_header_c::eap_tls_peap_header_c(
       
    40 	abs_eap_am_tools_c * const tools,
       
    41 	u8_t * const header_begin,
       
    42 	const u32_t header_buffer_length)
       
    43 	: eap_header_base_c(tools, header_begin, header_buffer_length)
       
    44 	, m_am_tools(tools)
       
    45 {
       
    46 }
       
    47 
       
    48 EAP_FUNC_EXPORT eap_code_value_e eap_tls_peap_header_c::get_eap_code() const
       
    49 {
       
    50 	return eap_header_base_c::get_code();
       
    51 }
       
    52 
       
    53 EAP_FUNC_EXPORT u8_t eap_tls_peap_header_c::get_eap_identifier() const
       
    54 {
       
    55 	return eap_header_base_c::get_identifier();
       
    56 }
       
    57 
       
    58 EAP_FUNC_EXPORT u16_t eap_tls_peap_header_c::get_eap_length() const
       
    59 {
       
    60 	return eap_header_base_c::get_length();
       
    61 }
       
    62 
       
    63 EAP_FUNC_EXPORT eap_type_value_e eap_tls_peap_header_c::get_eap_type() const
       
    64 {
       
    65 	return eap_header_base_c::get_type();
       
    66 }
       
    67 
       
    68 EAP_FUNC_EXPORT u16_t eap_tls_peap_header_c::get_data_length() const
       
    69 {
       
    70 	if (get_flag_tls_length_included() == true
       
    71 		&& get_eap_length() > static_cast<u16_t>(get_header_length()))
       
    72 	{
       
    73 		return static_cast<u16_t>(get_eap_length()-static_cast<u16_t>(get_header_length()));
       
    74 	}
       
    75 	else if (get_flag_tls_length_included() == false
       
    76 			 && get_eap_length() > static_cast<u16_t>(get_header_length()))
       
    77 	{
       
    78 		return static_cast<u16_t>(get_eap_length()-static_cast<u16_t>(get_header_length()));
       
    79 	}
       
    80 	else
       
    81 	{
       
    82 		return 0;
       
    83 	}
       
    84 }
       
    85 
       
    86 EAP_FUNC_EXPORT u32_t eap_tls_peap_header_c::get_tls_min_header_length() const
       
    87 {
       
    88 	return eap_header_base_c::get_header_length()
       
    89 		+ eap_header_base_c::get_type_field_length()
       
    90 		+ m_tls_length_delta_offset;
       
    91 }
       
    92 
       
    93 EAP_FUNC_EXPORT u32_t eap_tls_peap_header_c::get_tls_max_header_length()
       
    94 {
       
    95 	return eap_header_base_c::get_header_length()
       
    96 		+ eap_header_base_c::get_expanded_type_field_length()
       
    97 		+ m_tls_length_delta_offset
       
    98 		+ TLS_MESSAGE_LENGTH_FIELD_SIZE;
       
    99 }
       
   100 
       
   101 EAP_FUNC_EXPORT u32_t eap_tls_peap_header_c::get_header_length() const
       
   102 {
       
   103 	u32_t length = get_tls_min_header_length();
       
   104 
       
   105 	if (get_flag_tls_length_included() == true)
       
   106 	{
       
   107 		return length+TLS_MESSAGE_LENGTH_FIELD_SIZE;
       
   108 	}
       
   109 	else
       
   110 	{
       
   111 		return length;
       
   112 	}
       
   113 }
       
   114 
       
   115 EAP_FUNC_EXPORT u32_t eap_tls_peap_header_c::get_start_offset_of_data() const
       
   116 {
       
   117 	return get_header_length();
       
   118 }
       
   119 
       
   120 EAP_FUNC_EXPORT u8_t * eap_tls_peap_header_c::get_data_offset(
       
   121 	abs_eap_am_tools_c * const m_am_tools,
       
   122 	const u32_t offset,
       
   123 	const u32_t contignuous_bytes) const
       
   124 {
       
   125 	EAP_UNREFERENCED_PARAMETER(m_am_tools);
       
   126 
       
   127 	u32_t data_length = get_data_length(); // Here is removed optional TLS message length.
       
   128 
       
   129 	if (data_length >= offset+contignuous_bytes)
       
   130 	{
       
   131 		// get_header_length() handles optional TLS message length field.
       
   132 		u32_t offset_of_data = get_start_offset_of_data();
       
   133 		u8_t * const data = get_header_offset(offset_of_data, offset+contignuous_bytes);
       
   134 		if (data != 0)
       
   135 		{
       
   136 			return data+offset; // Data begins after the header.
       
   137 		}
       
   138 		else
       
   139 		{
       
   140 			return 0;
       
   141 		}
       
   142 	}
       
   143 	else
       
   144 	{
       
   145 		EAP_ASSERT_ALWAYS(get_data_length() > 0u);
       
   146 	}
       
   147 	return 0;
       
   148 }
       
   149 
       
   150 
       
   151 EAP_FUNC_EXPORT u8_t * eap_tls_peap_header_c::get_data(
       
   152 	abs_eap_am_tools_c * const m_am_tools,
       
   153 	const u32_t contignuous_bytes) const
       
   154 {
       
   155 	return get_data_offset(m_am_tools, 0u, contignuous_bytes);
       
   156 }
       
   157 
       
   158 EAP_FUNC_EXPORT u32_t eap_tls_peap_header_c::get_tls_flags_offset() const
       
   159 {
       
   160 	return eap_header_base_c::get_header_length() + eap_header_base_c::get_type_field_length();
       
   161 }
       
   162 
       
   163 EAP_FUNC_EXPORT u32_t eap_tls_peap_header_c::get_tls_length_offset() const
       
   164 {
       
   165 	return get_tls_flags_offset() + m_tls_length_delta_offset;
       
   166 }
       
   167 
       
   168 EAP_FUNC_EXPORT u8_t * eap_tls_peap_header_c::get_tls_flags() const
       
   169 {
       
   170 	u32_t flag_offset(get_tls_flags_offset()+m_flag_delta_offset);
       
   171 
       
   172 	return get_header_offset(flag_offset, sizeof(u8_t));
       
   173 }
       
   174 
       
   175 EAP_FUNC_EXPORT bool eap_tls_peap_header_c::get_tls_flag_bit(const u32_t mask) const
       
   176 {
       
   177 	const u8_t * const flag = get_tls_flags();
       
   178 
       
   179 	if (flag != 0
       
   180 		&& ((*flag) & mask))
       
   181 	{
       
   182 		return true;
       
   183 	}
       
   184 	return false;
       
   185 }
       
   186 
       
   187 EAP_FUNC_EXPORT u8_t eap_tls_peap_header_c::get_tls_flag_value(const u32_t mask, const u32_t shift) const
       
   188 {
       
   189 	const u8_t * const flag = get_tls_flags();
       
   190 
       
   191 	if (flag != 0)
       
   192 	{
       
   193 		return static_cast<u8_t>(((*flag) & mask) >> shift);
       
   194 	}
       
   195 	else
       
   196 	{
       
   197 		return 0;
       
   198 	}
       
   199 }
       
   200 
       
   201 EAP_FUNC_EXPORT bool eap_tls_peap_header_c::get_flag_tls_length_included() const
       
   202 {
       
   203 	return get_tls_flag_bit(m_flag_mask_tls_length_included);
       
   204 }
       
   205 
       
   206 EAP_FUNC_EXPORT bool eap_tls_peap_header_c::get_flag_more_fragments() const
       
   207 {
       
   208 	return get_tls_flag_bit(m_flag_mask_more_fragments);
       
   209 }
       
   210 
       
   211 EAP_FUNC_EXPORT bool eap_tls_peap_header_c::get_flag_start() const
       
   212 {
       
   213 	return get_tls_flag_bit(m_flag_mask_start);
       
   214 }
       
   215 	
       
   216 EAP_FUNC_EXPORT u8_t eap_tls_peap_header_c::get_flag_reserved() const
       
   217 {
       
   218 	return get_tls_flag_value(m_flag_mask_reserved, m_flag_shift_reserved);
       
   219 }
       
   220 
       
   221 EAP_FUNC_EXPORT u8_t eap_tls_peap_header_c::get_flag_version() const
       
   222 {
       
   223 	return get_tls_flag_value(m_flag_mask_version, m_flag_shift_version);
       
   224 }
       
   225 
       
   226 EAP_FUNC_EXPORT eap_status_e eap_tls_peap_header_c::get_tls_message_length(u32_t * const tls_length) const
       
   227 {
       
   228 	if (get_flag_tls_length_included() == false)
       
   229 	{
       
   230 		// TLS data length is NOT included.
       
   231 		*tls_length = 0u;
       
   232 		return eap_status_ok;
       
   233 	}
       
   234 	
       
   235 	u8_t * const data = get_header_offset(get_tls_length_offset(), sizeof(u32_t));
       
   236 	if (data != 0)
       
   237 	{
       
   238 		u32_t tls_message_length =
       
   239 			eap_read_u32_t_network_order(data, sizeof(u32_t));
       
   240 		
       
   241 		*tls_length = tls_message_length;
       
   242 		return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   243 	}
       
   244 	else
       
   245 	{
       
   246 		return EAP_STATUS_RETURN(m_am_tools, eap_status_header_corrupted);
       
   247 	}
       
   248 }
       
   249 
       
   250 
       
   251 EAP_FUNC_EXPORT eap_status_e eap_tls_peap_header_c::check_header(
       
   252 	abs_eap_am_tools_c * const tools,
       
   253 	const eap_type_value_e required_eap_type,
       
   254 	const bool is_client_when_true,
       
   255 	const peap_version_e peap_version,
       
   256 	const bool check_peap_version_when_true) const
       
   257 {
       
   258 	eap_status_e status = eap_status_ok;
       
   259 
       
   260 	if (get_eap_type() != required_eap_type)
       
   261 	{
       
   262 		EAP_UNREFERENCED_PARAMETER(tools);
       
   263 		status = EAP_STATUS_RETURN(tools, eap_status_header_corrupted);
       
   264 	}
       
   265 	else if (get_flag_reserved() != static_cast<u16_t>(0ul))
       
   266 	{
       
   267 		status = EAP_STATUS_RETURN(tools, eap_status_header_corrupted);
       
   268 	}
       
   269 	else if ((get_eap_type() == eap_type_tls
       
   270 			  || get_eap_type() == eap_type_ttls)
       
   271 			 && get_flag_version() != static_cast<u16_t>(0ul))
       
   272 	{
       
   273 		status = EAP_STATUS_RETURN(tools, eap_status_header_corrupted);
       
   274 	}
       
   275 	else if (get_eap_type() == eap_type_peap
       
   276 			 && check_peap_version_when_true == true
       
   277 			 && peap_version == peap_version_0_xp
       
   278 			 && get_flag_version() != static_cast<u16_t>(peap_version_0_xp))
       
   279 	{
       
   280 		// In version negotiation this check will fail.
       
   281 		// Do not add error traces here.
       
   282 		status = eap_status_no_matching_protocol_version;
       
   283 	}
       
   284 	else if (get_eap_type() == eap_type_peap
       
   285 			 && check_peap_version_when_true == true
       
   286 			 && peap_version == peap_version_1
       
   287 			 && get_flag_version() != static_cast<u16_t>(peap_version_1))
       
   288 	{
       
   289 		// In version negotiation this check will fail.
       
   290 		// Do not add error traces here.
       
   291 		status = eap_status_no_matching_protocol_version;
       
   292 	}
       
   293 	else if (get_eap_type() == eap_type_peap
       
   294 			 && check_peap_version_when_true == true
       
   295 			 && peap_version == peap_version_2
       
   296 			 && get_flag_version() != static_cast<u16_t>(peap_version_2))
       
   297 	{
       
   298 		// In version negotiation this check will fail.
       
   299 		// Do not add error traces here.
       
   300 		status = eap_status_no_matching_protocol_version;
       
   301 	}
       
   302 	
       
   303 	if (status != eap_status_ok)
       
   304 	{
       
   305 		EAP_UNREFERENCED_PARAMETER(is_client_when_true);
       
   306 		
       
   307 		// In version negotiation this check will fail.
       
   308 		// Do not add error traces here.
       
   309 		eap_status_string_c status_string;
       
   310 		EAP_UNREFERENCED_PARAMETER(status_string);
       
   311 		EAP_TRACE_DEBUG(
       
   312 			m_am_tools,
       
   313 			TRACE_FLAGS_DEFAULT|TRACE_TEST_VECTORS,
       
   314 			(EAPL("WARNING: EAP_type_TLS_PEAP: check_header(): failed, %s, required_eap_type 0x%08x, ")
       
   315 			 EAPL("peap_version %d, check_peap_version_when_true %d, get_eap_type() 0x%08x, get_flag_reserved() %d, get_flag_version() %d, status %s\n"),
       
   316 			 (is_client_when_true == true) ? "client": "server",
       
   317 			 convert_eap_type_to_u32_t(required_eap_type),
       
   318 			 peap_version,
       
   319 			 check_peap_version_when_true,
       
   320 			 convert_eap_type_to_u32_t(get_eap_type()),
       
   321 			 get_flag_reserved(),
       
   322 			 get_flag_version(),
       
   323 			 status_string.get_status_string(status)));
       
   324 	}
       
   325 	
       
   326 	if (status == eap_status_no_matching_protocol_version)
       
   327 	{
       
   328 		// In version negotiation this check will fail.
       
   329 		// Do not add error traces here.
       
   330 		return status;
       
   331 	}
       
   332 	else
       
   333 	{
       
   334 		return EAP_STATUS_RETURN(tools, status);
       
   335 	}
       
   336 }
       
   337 
       
   338 EAP_FUNC_EXPORT eap_const_string eap_tls_peap_header_c::get_code_string() const
       
   339 {
       
   340 	return eap_header_string_c::get_eap_code_string(get_eap_code());
       
   341 }
       
   342 
       
   343 EAP_FUNC_EXPORT eap_const_string eap_tls_peap_header_c::get_eap_type_string() const
       
   344 {
       
   345 	if (get_eap_length() <= eap_header_base_c::get_header_length())
       
   346 	{
       
   347 		return EAPL("No EAP-type");
       
   348 	}
       
   349 
       
   350 	return eap_header_string_c::get_eap_type_string(get_eap_type());
       
   351 }
       
   352 
       
   353 EAP_FUNC_EXPORT void eap_tls_peap_header_c::set_eap_code(const eap_code_value_e p_code)
       
   354 {
       
   355 	eap_header_base_c::set_code(p_code);
       
   356 }
       
   357 
       
   358 EAP_FUNC_EXPORT void eap_tls_peap_header_c::set_eap_identifier(const u8_t p_identifier)
       
   359 {
       
   360 	eap_header_base_c::set_identifier(p_identifier);
       
   361 }
       
   362 
       
   363 EAP_FUNC_EXPORT void eap_tls_peap_header_c::set_eap_length(
       
   364 	const u16_t p_length,
       
   365 	const bool expanded_type_when_true)
       
   366 {
       
   367 	eap_header_base_c::set_length(
       
   368 		p_length,
       
   369 		expanded_type_when_true);
       
   370 }
       
   371 
       
   372 EAP_FUNC_EXPORT void eap_tls_peap_header_c::set_eap_type(
       
   373 	const eap_type_value_e p_type,
       
   374 	const bool expanded_type_when_true)
       
   375 {
       
   376 	eap_header_base_c::set_type(p_type, expanded_type_when_true);
       
   377 }
       
   378 
       
   379 
       
   380 EAP_FUNC_EXPORT void eap_tls_peap_header_c::set_tls_flag_value(const u8_t value, const u32_t mask, const u32_t shift) const
       
   381 {
       
   382 	u8_t *flag = get_tls_flags();
       
   383 
       
   384 	if (flag != 0)
       
   385 	{
       
   386 		(*flag) = static_cast<u8_t>(((*flag) & ~mask) | ((value << shift) & mask));
       
   387 	}
       
   388 }
       
   389 
       
   390 EAP_FUNC_EXPORT void eap_tls_peap_header_c::set_tls_flag_bit(const bool flag, u32_t mask) const
       
   391 {
       
   392 	u8_t *p_flag = get_tls_flags();
       
   393 	
       
   394 	if (p_flag != 0)
       
   395 	{
       
   396 		if (flag == true)
       
   397 		{
       
   398 			(*p_flag) = static_cast<u8_t>((*p_flag) | mask);
       
   399 		}
       
   400 		else
       
   401 		{
       
   402 			(*p_flag) = static_cast<u8_t>((*p_flag) & ~mask);
       
   403 		}
       
   404 	}
       
   405 }
       
   406 
       
   407 EAP_FUNC_EXPORT void eap_tls_peap_header_c::set_flag_reserved(const u8_t reserved)
       
   408 {
       
   409 	set_tls_flag_value(reserved, m_flag_mask_reserved, m_flag_shift_reserved);
       
   410 }
       
   411 
       
   412 EAP_FUNC_EXPORT void eap_tls_peap_header_c::set_flag_version(const u8_t version)
       
   413 {
       
   414 	set_tls_flag_value(version, m_flag_mask_version, m_flag_shift_version);
       
   415 }
       
   416 
       
   417 EAP_FUNC_EXPORT void eap_tls_peap_header_c::set_flag_tls_length_included(const bool tls_length_included)
       
   418 {
       
   419 	set_tls_flag_bit(tls_length_included, m_flag_mask_tls_length_included);
       
   420 }
       
   421 
       
   422 EAP_FUNC_EXPORT void eap_tls_peap_header_c::set_flag_more_fragments(const bool more_fragments)
       
   423 {
       
   424 	set_tls_flag_bit(more_fragments, m_flag_mask_more_fragments);
       
   425 }
       
   426 
       
   427 EAP_FUNC_EXPORT void eap_tls_peap_header_c::set_flag_start(const bool start)
       
   428 {
       
   429 	set_tls_flag_bit(start, m_flag_mask_start);
       
   430 }
       
   431 
       
   432 
       
   433 EAP_FUNC_EXPORT void eap_tls_peap_header_c::set_data_length(
       
   434 	const u32_t p_data_length,
       
   435 	const bool expanded_type_when_true)
       
   436 {
       
   437 	EAP_ASSERT_ALWAYS(p_data_length+get_header_length() <= 0xffff);
       
   438 	
       
   439 	set_eap_length(
       
   440 		static_cast<u16_t>(p_data_length+get_header_length()),
       
   441 		expanded_type_when_true);
       
   442 }
       
   443 
       
   444 EAP_FUNC_EXPORT void eap_tls_peap_header_c::set_tls_message_length(const u32_t tls_length)
       
   445 {
       
   446 	EAP_ASSERT_ALWAYS(get_flag_tls_length_included() == true);
       
   447 
       
   448 	u8_t * const data = get_header_offset(get_tls_length_offset(), sizeof(u32_t));
       
   449 
       
   450 	EAP_ASSERT(data != 0);
       
   451 
       
   452 	data[0] = static_cast<u8_t>((tls_length & 0xff000000) >> 24);
       
   453 	data[1] = static_cast<u8_t>((tls_length & 0x00ff0000) >> 16);
       
   454 	data[2] = static_cast<u8_t>((tls_length & 0x0000ff00) >> 8);
       
   455 	data[3] = static_cast<u8_t>((tls_length & 0x000000ff) >> 0);
       
   456 }
       
   457 
       
   458 
       
   459 EAP_FUNC_EXPORT void eap_tls_peap_header_c::reset_header(
       
   460 	abs_eap_am_tools_c * const m_am_tools,
       
   461 	const eap_type_value_e required_eap_type,
       
   462 	const u32_t buffer_length,
       
   463 	const peap_version_e peap_version,
       
   464 	const bool expanded_type_when_true)
       
   465 {
       
   466 	EAP_UNREFERENCED_PARAMETER(m_am_tools);
       
   467 
       
   468 	eap_header_base_c::set_length(
       
   469 		static_cast<u16_t>(buffer_length),
       
   470 		expanded_type_when_true);
       
   471 
       
   472 	set_eap_code(eap_code_none);
       
   473 	set_eap_identifier(0u);
       
   474 	set_eap_type(
       
   475 		required_eap_type,
       
   476 		expanded_type_when_true);
       
   477 
       
   478 	set_flag_tls_length_included(false);
       
   479 	set_flag_more_fragments(false);
       
   480 	set_flag_start(false);
       
   481 	
       
   482 	set_flag_reserved(0u);
       
   483 		
       
   484 #if defined(USE_FAST_EAP_TYPE)
       
   485 	if (required_eap_type == eap_type_fast)
       
   486 	{
       
   487 		set_flag_version(static_cast<u8_t>(peap_version));
       
   488 	}
       
   489 	else
       
   490 #endif //#if defined(USE_FAST_EAP_TYPE)
       
   491 	if (required_eap_type == eap_type_peap)
       
   492 	{
       
   493 		if (peap_version >= peap_version_0_xp
       
   494 			&& peap_version <= peap_version_2)
       
   495 		{
       
   496 			set_flag_version(static_cast<u8_t>(peap_version));
       
   497 		}
       
   498 		else
       
   499 		{
       
   500 			EAP_ASSERT_ALWAYS((peap_version >= peap_version_0_xp) && (peap_version <= peap_version_2));
       
   501 		}
       
   502 	}
       
   503 	else
       
   504 	{
       
   505 		// All other EAP-methods.
       
   506 		set_flag_version(0u);
       
   507 	}
       
   508 }
       
   509 
       
   510 
       
   511 
       
   512 
       
   513 // End.