eapol/eapol_framework/eapol_common/type/tls_peap/tls/src/tls_peap_tlv_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 132 
       
    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 "tls_peap_tlv_header.h"
       
    30 
       
    31 /** @file */
       
    32 
       
    33 
       
    34 /**
       
    35  * The destructor of the tls_peap_tlv_header_c class does nothing.
       
    36  */
       
    37 tls_peap_tlv_header_c::~tls_peap_tlv_header_c()
       
    38 {
       
    39 }
       
    40 
       
    41 /**
       
    42  * The constructor of the tls_peap_tlv_header_c class simply initializes the attributes.
       
    43  */
       
    44 tls_peap_tlv_header_c::tls_peap_tlv_header_c(
       
    45 	abs_eap_am_tools_c * const tools,
       
    46 	void * const header_begin,
       
    47 	const u32_t header_buffer_length)
       
    48 	: eap_general_header_base_c(tools, header_begin, header_buffer_length)
       
    49 	, m_am_tools(tools)
       
    50 {
       
    51 }
       
    52 
       
    53 /**
       
    54  * This function returns the TLV type flag.
       
    55  */
       
    56 tls_peap_tlv_type_e tls_peap_tlv_header_c::get_flag_tlv_type() const
       
    57 {
       
    58 	const u8_t * const flag_tlv_type_data = get_header_offset(m_flags_and_tlv_type_offset, sizeof(u16_t));
       
    59 	if (flag_tlv_type_data != 0)
       
    60 	{
       
    61 		return static_cast<tls_peap_tlv_type_e>(
       
    62 			(static_cast<u16_t>(
       
    63 				flag_tlv_type_data[0] & ~(m_flag_mask_mandatory_tlv|m_flag_mask_reserved)) << 8)
       
    64 			| (static_cast<u16_t>(flag_tlv_type_data[1])));
       
    65 	}
       
    66 	else
       
    67 	{
       
    68 		return tls_peap_tlv_type_none;
       
    69 	}
       
    70 }
       
    71 
       
    72 /**
       
    73  * This function returns the TLV mandatory flag.
       
    74  */
       
    75 bool tls_peap_tlv_header_c::get_flag_mandatory_tlv() const
       
    76 {
       
    77 	const u8_t * const flag_tlv_type_data = get_header_offset(m_flags_and_tlv_type_offset, sizeof(u8_t));
       
    78 	if (flag_tlv_type_data != 0)
       
    79 	{
       
    80 		u8_t flag = static_cast<u8_t>(flag_tlv_type_data[0] & m_flag_mask_mandatory_tlv);
       
    81 		if (flag != 0)
       
    82 		{
       
    83 			return true;
       
    84 		}
       
    85 	}
       
    86 	return false;
       
    87 }
       
    88 
       
    89 /**
       
    90  * This function returns the TLV reserved flag.
       
    91  */
       
    92 bool tls_peap_tlv_header_c::get_flag_reserved() const
       
    93 {
       
    94 	const u8_t * const flag_tlv_type_data = get_header_offset(m_flags_and_tlv_type_offset, sizeof(u8_t));
       
    95 	if (flag_tlv_type_data != 0)
       
    96 	{
       
    97 		u8_t flag = static_cast<u8_t>(flag_tlv_type_data[0] & m_flag_mask_reserved);
       
    98 		if (flag != 0)
       
    99 		{
       
   100 			return true;
       
   101 		}
       
   102 	}
       
   103 	return false;
       
   104 }
       
   105 
       
   106 /**
       
   107  * This function returns the data length of TLV.
       
   108  */
       
   109 u16_t tls_peap_tlv_header_c::get_data_length() const
       
   110 {
       
   111 	const u8_t * const length_data = get_header_offset(m_length_offset, sizeof(u16_t));
       
   112 	if (length_data != 0)
       
   113 	{
       
   114 		return eap_read_u16_t_network_order(length_data, sizeof(u16_t));
       
   115 	}
       
   116 	else
       
   117 	{
       
   118 		return 0ul;
       
   119 	}
       
   120 }
       
   121 
       
   122 /**
       
   123  * This function returns the header length of TLV.
       
   124  */
       
   125 u32_t tls_peap_tlv_header_c::get_header_length()
       
   126 {
       
   127 	return m_data_offset;
       
   128 }
       
   129 
       
   130 /**
       
   131  * This function returns pointer to the offset of data of TLV.
       
   132  * @param offset is the offset of queried data in bytes.
       
   133  * @param contignuous_bytes is the length of queried data in bytes.
       
   134  */
       
   135 u8_t * tls_peap_tlv_header_c::get_data_offset(const u32_t offset, const u32_t contignuous_bytes) const
       
   136 {
       
   137 	EAP_UNREFERENCED_PARAMETER(m_am_tools);
       
   138 
       
   139 	u32_t data_length = get_data_length(); // Here is removed optional TLS message length.
       
   140 
       
   141 	if (data_length >= offset+contignuous_bytes)
       
   142 	{
       
   143 		u8_t * const data = get_header_offset(m_data_offset, offset+contignuous_bytes);
       
   144 		if (data != 0)
       
   145 		{
       
   146 			return &data[offset];
       
   147 		}
       
   148 		else
       
   149 		{
       
   150 			return 0;
       
   151 		}
       
   152 	}
       
   153 	else
       
   154 	{
       
   155 		EAP_ASSERT_ALWAYS(data_length >= offset+contignuous_bytes);
       
   156 	}
       
   157 	return 0;
       
   158 }
       
   159 
       
   160 
       
   161 /**
       
   162  * This function returns pointer to the offset of data of TLV.
       
   163  * @param contignuous_bytes is the length of queried data in bytes.
       
   164  */
       
   165 u8_t * tls_peap_tlv_header_c::get_data(const u32_t contignuous_bytes) const
       
   166 {
       
   167 	return get_data_offset(0u, contignuous_bytes);
       
   168 }
       
   169 
       
   170 
       
   171 /**
       
   172  * This function return pointer to the next TLV header in the same buffer.
       
   173  */
       
   174 u8_t * tls_peap_tlv_header_c::get_next_header() const
       
   175 {
       
   176 	if (get_header_buffer_length() >= 2ul*get_header_length()+get_data_length())
       
   177 	{
       
   178 		return get_data_offset(get_data_length(), get_header_length());
       
   179 	}
       
   180 	else
       
   181 	{
       
   182 		return 0;
       
   183 	}
       
   184 }
       
   185 
       
   186 
       
   187 /**
       
   188  * This function checks the header is valid.
       
   189  */
       
   190 eap_status_e tls_peap_tlv_header_c::check_header() const
       
   191 {
       
   192 	if (get_flag_tlv_type() != tls_peap_tlv_type_result
       
   193 		&& get_flag_tlv_type() != tls_peap_tlv_type_nak
       
   194 		&& get_flag_tlv_type() != tls_peap_tlv_type_crypto_binding
       
   195 		&& get_flag_tlv_type() != tls_peap_tlv_eap_payload
       
   196 		&& get_flag_tlv_type() != tls_peap_tlv_type_intermediate_result)
       
   197 	{
       
   198 		return EAP_STATUS_RETURN(m_am_tools, eap_status_header_corrupted);
       
   199 	}
       
   200 	else if (get_flag_reserved() != 0)
       
   201 	{
       
   202 		return EAP_STATUS_RETURN(m_am_tools, eap_status_header_corrupted);
       
   203 	}
       
   204 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   205 }
       
   206 
       
   207 /**
       
   208  * This function returns debug strings of the TLV type.
       
   209  */
       
   210 eap_const_string tls_peap_tlv_header_c::get_tlv_type_string() const
       
   211 {
       
   212 
       
   213 #if defined(USE_EAP_TRACE_STRINGS)
       
   214 	const tls_peap_tlv_type_e protocol = get_flag_tlv_type();
       
   215 
       
   216 	EAP_IF_RETURN_STRING(protocol, tls_peap_tlv_type_none)
       
   217 	else EAP_IF_RETURN_STRING(protocol, tls_peap_tlv_type_result)
       
   218 	else EAP_IF_RETURN_STRING(protocol, tls_peap_tlv_type_nak)
       
   219 	else EAP_IF_RETURN_STRING(protocol, tls_peap_tlv_type_crypto_binding)
       
   220 	else EAP_IF_RETURN_STRING(protocol, tls_peap_tlv_eap_payload)
       
   221 	else EAP_IF_RETURN_STRING(protocol, tls_peap_tlv_type_intermediate_result)
       
   222 	else
       
   223 #endif // #if defined(USE_EAP_TRACE_STRINGS)
       
   224 	{
       
   225 		return EAPL("Unknown TLV type");
       
   226 	}
       
   227 }
       
   228 
       
   229 /**
       
   230  * This function sets the TLV type flag.
       
   231  */
       
   232 void tls_peap_tlv_header_c::set_flag_tlv_type(tls_peap_tlv_type_e type)
       
   233 {
       
   234 	u8_t * const flag_tlv_type_data = get_header_offset(m_flags_and_tlv_type_offset, sizeof(u16_t));
       
   235 	
       
   236 	EAP_ASSERT(flag_tlv_type_data != 0);
       
   237 	
       
   238 	flag_tlv_type_data[0] = static_cast<u8_t>(
       
   239 		flag_tlv_type_data[0] & (m_flag_mask_mandatory_tlv|m_flag_mask_reserved)
       
   240 		| ((type & 0xff00) >> 8) & ~(m_flag_mask_mandatory_tlv|m_flag_mask_reserved));
       
   241 	flag_tlv_type_data[1] = static_cast<u8_t>(type & 0x00ff);
       
   242 }
       
   243 
       
   244 /**
       
   245  * This function sets the TLV reserved flag.
       
   246  */
       
   247 void tls_peap_tlv_header_c::set_flag_reserved(bool reserved)
       
   248 {
       
   249 	u8_t * const flag_tlv_type_data = get_header_offset(m_flags_and_tlv_type_offset, sizeof(u16_t));
       
   250 	EAP_ASSERT(flag_tlv_type_data != 0);
       
   251 	
       
   252 	if (reserved == true)
       
   253 	{
       
   254 		flag_tlv_type_data[0] |= m_flag_mask_reserved;
       
   255 	}
       
   256 	else
       
   257 	{
       
   258 		flag_tlv_type_data[0] &= ~m_flag_mask_reserved;
       
   259 	}
       
   260 }
       
   261 
       
   262 /**
       
   263  * This function sets the TLV manadtory flag.
       
   264  */
       
   265 void tls_peap_tlv_header_c::set_flag_mandatory_tlv(const bool mandatory_when_true)
       
   266 {
       
   267 	u8_t * const flag_tlv_type_data = get_header_offset(m_flags_and_tlv_type_offset, sizeof(u16_t));
       
   268 	EAP_ASSERT(flag_tlv_type_data != 0);
       
   269 	
       
   270 	if (mandatory_when_true == true)
       
   271 	{
       
   272 		flag_tlv_type_data[0] |= m_flag_mask_mandatory_tlv;
       
   273 	}
       
   274 	else
       
   275 	{
       
   276 		flag_tlv_type_data[0] &= ~m_flag_mask_mandatory_tlv;
       
   277 	}
       
   278 }
       
   279 
       
   280 /**
       
   281  * This function sets the TLV data length.
       
   282  */
       
   283 void tls_peap_tlv_header_c::set_data_length(const u16_t p_length)
       
   284 {
       
   285 	u8_t * const length_data = get_header_offset(m_length_offset, sizeof(u16_t));
       
   286 	
       
   287 	EAP_ASSERT(length_data != 0);
       
   288 	
       
   289 	length_data[0] = static_cast<u8_t>((p_length & 0xff00) >> 8);
       
   290 	length_data[1] = static_cast<u8_t>((p_length & 0x00ff));
       
   291 }
       
   292 
       
   293 /**
       
   294  * This function resets the TLV header.
       
   295  */
       
   296 void tls_peap_tlv_header_c::reset_header(const u16_t buffer_length)
       
   297 {
       
   298 	set_flag_tlv_type(tls_peap_tlv_type_none);
       
   299 	set_flag_reserved(0);
       
   300 	set_flag_mandatory_tlv(true);
       
   301 	set_data_length(buffer_length);
       
   302 }
       
   303 
       
   304 
       
   305 
       
   306 
       
   307 // End.