eapol/eapol_framework/eapol_common/core/eapol_rsna_key_data_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 54 
       
    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 "eapol_rsna_key_data_header.h"
       
    30 
       
    31 
       
    32 //------------------------------------------------------
       
    33 
       
    34 // 
       
    35 eapol_rsna_key_data_header_c::~eapol_rsna_key_data_header_c()
       
    36 {
       
    37 }
       
    38 
       
    39 //------------------------------------------------------
       
    40 
       
    41 // 
       
    42 eapol_rsna_key_data_header_c::eapol_rsna_key_data_header_c(
       
    43 	abs_eap_am_tools_c * const tools,
       
    44 	const bool is_RSNA_when_true,
       
    45 	const bool is_WPXM_when_true,
       
    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 	, m_is_RSNA_when_true(is_RSNA_when_true)
       
    51 	, m_is_WPXM_when_true(is_WPXM_when_true)
       
    52 {
       
    53 }
       
    54 
       
    55 //------------------------------------------------------
       
    56 
       
    57 // 
       
    58 eapol_RSNA_key_descriptor_type_e eapol_rsna_key_data_header_c::get_descriptor_type() const
       
    59 {
       
    60 	const u8_t * const data = get_header_offset(m_offset_type, EAPOL_RSNA_KEY_DATA_TYPE_FIELD_SIZE);
       
    61 	if (data != 0)
       
    62 	{
       
    63 		return static_cast<eapol_RSNA_key_descriptor_type_e>(*data);
       
    64 	}
       
    65 	else
       
    66 	{
       
    67 		return eapol_RSNA_key_data_type_none;
       
    68 	}
       
    69 }
       
    70 
       
    71 //------------------------------------------------------
       
    72 
       
    73 // 
       
    74 u32_t eapol_rsna_key_data_header_c::get_header_and_body_length() const
       
    75 {
       
    76 	return get_length() + 2ul;
       
    77 }
       
    78 
       
    79 //------------------------------------------------------
       
    80 
       
    81 // 
       
    82 u8_t eapol_rsna_key_data_header_c::get_length() const
       
    83 {
       
    84 	const u8_t * const data = get_header_offset(m_offset_length, EAPOL_RSNA_KEY_DATA_LENGTH_FIELD_SIZE);
       
    85 	if (data != 0)
       
    86 	{
       
    87 		return *data;
       
    88 	}
       
    89 	else
       
    90 	{
       
    91 		return 0ul;
       
    92 	}
       
    93 }
       
    94 
       
    95 //------------------------------------------------------
       
    96 
       
    97 // 
       
    98 u32_t eapol_rsna_key_data_header_c::get_oui() const
       
    99 {
       
   100 	const u8_t * const data = get_header_offset(m_offset_oui, EAPOL_RSNA_KEY_DATA_OUI_FIELD_SIZE);
       
   101 	if (data != 0)
       
   102 	{
       
   103 		return eap_read_u24_t_network_order(data, EAPOL_RSNA_KEY_DATA_OUI_FIELD_SIZE);
       
   104 	}
       
   105 	else
       
   106 	{
       
   107 		return 0ul;
       
   108 	}
       
   109 }
       
   110 
       
   111 //------------------------------------------------------
       
   112 
       
   113 // 
       
   114 eapol_RSNA_key_payload_type_e eapol_rsna_key_data_header_c::get_payload_type() const
       
   115 {
       
   116 	const u8_t * const data = get_header_offset(m_offset_key_data_payload_type, EAPOL_RSNA_KEY_PAYLOAD_TYPE_FIELD_SIZE);
       
   117 	if (data != 0)
       
   118 	{
       
   119 		return static_cast<eapol_RSNA_key_payload_type_e>(*data);
       
   120 	}
       
   121 	else
       
   122 	{
       
   123 		return eapol_RSNA_key_payload_type_none;
       
   124 	}
       
   125 }
       
   126 
       
   127 //------------------------------------------------------
       
   128 
       
   129 // 
       
   130 u32_t eapol_rsna_key_data_header_c::get_key_data_payload_length() const
       
   131 {
       
   132 	const u32_t length = get_length();
       
   133 	if (length >= EAPOL_RSNA_KEY_DATA_LENGTH_FIELD_MINIMUM_SIZE)
       
   134 	{
       
   135 		return length - EAPOL_RSNA_KEY_DATA_LENGTH_FIELD_MINIMUM_SIZE;
       
   136 	}
       
   137 	else
       
   138 	{
       
   139 		return 0ul;
       
   140 	}
       
   141 }
       
   142 
       
   143 //------------------------------------------------------
       
   144 
       
   145 // 
       
   146 u8_t * eapol_rsna_key_data_header_c::get_key_data_payload_offset(const u32_t offset, const u32_t contignuous_bytes) const
       
   147 {
       
   148 	EAP_UNREFERENCED_PARAMETER(m_am_tools);
       
   149 
       
   150 	u32_t payload_length = get_key_data_payload_length();
       
   151 
       
   152 	if (payload_length >= offset+contignuous_bytes)
       
   153 	{
       
   154 		// This is pointer to the offset of the Data.
       
   155 		u8_t * const data = get_header_offset(m_offset_key_data_payload, offset+contignuous_bytes);
       
   156 		if (data != 0)
       
   157 		{
       
   158 			return &data[offset];
       
   159 		}
       
   160 		else
       
   161 		{
       
   162 			return 0;
       
   163 		}
       
   164 	}
       
   165 	else
       
   166 	{
       
   167 		EAP_ASSERT_ALWAYS(payload_length >= offset+contignuous_bytes);
       
   168 	}
       
   169 	return 0;
       
   170 }
       
   171 
       
   172 //------------------------------------------------------
       
   173 
       
   174 // 
       
   175 u8_t * eapol_rsna_key_data_header_c::get_key_data_payload(const u32_t contignuous_bytes) const
       
   176 {
       
   177 	return get_key_data_payload_offset(0u, contignuous_bytes);
       
   178 }
       
   179 
       
   180 //------------------------------------------------------
       
   181 
       
   182 // 
       
   183 u8_t * eapol_rsna_key_data_header_c::get_next_header() const
       
   184 {
       
   185 	if (get_header_buffer_length() >= get_key_data_payload_length()+get_header_length())
       
   186 	{
       
   187 		// NOTE get_key_data_payload_offset(get_key_data_payload_length(), get_header_length())
       
   188 		// cannot be used here.
       
   189 		u8_t * const data = get_header_offset(
       
   190 			m_offset_key_data_payload,
       
   191 			get_key_data_payload_length()+get_header_length());
       
   192 		if (data != 0)
       
   193 		{
       
   194 			return &data[get_key_data_payload_length()];
       
   195 		}
       
   196 		else
       
   197 		{
       
   198 			return 0;
       
   199 		}
       
   200 	}
       
   201 	else
       
   202 	{
       
   203 		return 0;
       
   204 	}
       
   205 }
       
   206 
       
   207 //------------------------------------------------------
       
   208 
       
   209 // 
       
   210 u16_t eapol_rsna_key_data_header_c::get_header_length()
       
   211 {
       
   212 	return m_offset_key_data_payload;
       
   213 }
       
   214 
       
   215 //------------------------------------------------------
       
   216 
       
   217 // 
       
   218 eap_status_e eapol_rsna_key_data_header_c::set_type(const eapol_RSNA_key_descriptor_type_e type)
       
   219 {
       
   220 	u8_t * const data = get_header_offset(m_offset_type, EAPOL_RSNA_KEY_DATA_TYPE_FIELD_SIZE);
       
   221 	if (data != 0)
       
   222 	{
       
   223 		*data = static_cast<u8_t>(type);
       
   224 		return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   225 	}
       
   226 	else
       
   227 	{
       
   228 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   229 	}
       
   230 }
       
   231 
       
   232 //------------------------------------------------------
       
   233 
       
   234 // 
       
   235 eap_status_e eapol_rsna_key_data_header_c::set_length(const u8_t length)
       
   236 {
       
   237 	u8_t * const data = get_header_offset(m_offset_length, EAPOL_RSNA_KEY_DATA_LENGTH_FIELD_SIZE);
       
   238 	if (data != 0)
       
   239 	{
       
   240 		*data = static_cast<u8_t>(length);
       
   241 		return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   242 	}
       
   243 	else
       
   244 	{
       
   245 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   246 	}
       
   247 }
       
   248 
       
   249 //------------------------------------------------------
       
   250 
       
   251 // 
       
   252 eap_status_e eapol_rsna_key_data_header_c::set_oui(const u32_t oui)
       
   253 {
       
   254 	u8_t * const data = get_header_offset(m_offset_oui, EAPOL_RSNA_KEY_DATA_OUI_FIELD_SIZE);
       
   255 	if (data != 0)
       
   256 	{
       
   257 		return eap_write_u24_t_network_order(data, EAPOL_RSNA_KEY_DATA_OUI_FIELD_SIZE, oui);
       
   258 	}
       
   259 	else
       
   260 	{
       
   261 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   262 	}
       
   263 }
       
   264 
       
   265 //------------------------------------------------------
       
   266 
       
   267 // 
       
   268 eap_status_e eapol_rsna_key_data_header_c::set_payload_type(const eapol_RSNA_key_payload_type_e type)
       
   269 {
       
   270 	u8_t * const data = get_header_offset(m_offset_key_data_payload_type, EAPOL_RSNA_KEY_PAYLOAD_TYPE_FIELD_SIZE);
       
   271 	if (data != 0)
       
   272 	{
       
   273 		*data = static_cast<u8_t>(type);
       
   274 		return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   275 	}
       
   276 	else
       
   277 	{
       
   278 		return EAP_STATUS_RETURN(m_am_tools, eap_status_allocation_error);
       
   279 	}
       
   280 }
       
   281 
       
   282 //------------------------------------------------------
       
   283 
       
   284 // 
       
   285 eap_status_e eapol_rsna_key_data_header_c::check_header() const
       
   286 {
       
   287 	if (m_is_WPXM_when_true == true)
       
   288 	{
       
   289 		// OK, WPXM could use either RSN IE or WPA IE.
       
   290 	}
       
   291 	else if (m_is_RSNA_when_true == true)
       
   292 	{
       
   293 		// RSNA
       
   294 		if (get_descriptor_type() == eapol_RSNA_key_data_type_RSN_IE)
       
   295 		{
       
   296 			// OK
       
   297 		}
       
   298 		else if (get_descriptor_type() == eapol_RSNA_key_data_type_RSN_key_data)
       
   299 		{
       
   300 			if (get_length() < EAPOL_RSNA_KEY_DATA_LENGTH_FIELD_MINIMUM_SIZE)
       
   301 			{
       
   302 				return EAP_STATUS_RETURN(m_am_tools, eap_status_wrong_eapol_oui);
       
   303 			}
       
   304 		}
       
   305 	}
       
   306 	else
       
   307 	{
       
   308 		// WPA
       
   309 		if (get_descriptor_type() != eapol_RSNA_key_data_type_WPA_IE)
       
   310 		{
       
   311 			return EAP_STATUS_RETURN(m_am_tools, eap_status_wrong_eapol_type);
       
   312 		}
       
   313 	}
       
   314 
       
   315 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   316 }
       
   317 
       
   318 //------------------------------------------------------
       
   319 
       
   320 // 
       
   321 eap_status_e eapol_rsna_key_data_header_c::reset_header()
       
   322 {
       
   323 	eap_status_e status(eap_status_process_general_error);
       
   324 
       
   325 	if (m_is_RSNA_when_true == true
       
   326 		|| m_is_WPXM_when_true == true)
       
   327 	{
       
   328 		// RSNA or WPXM
       
   329 		status = set_type(eapol_RSNA_key_data_type_RSN_key_data);
       
   330 	}
       
   331 	else
       
   332 	{
       
   333 		// WPA
       
   334 		status = set_type(eapol_RSNA_key_data_type_WPA_IE);
       
   335 	}
       
   336 
       
   337 	if (status != eap_status_ok)
       
   338 	{
       
   339 		return EAP_STATUS_RETURN(m_am_tools, status);
       
   340 	}
       
   341 
       
   342 	status = set_length(EAPOL_RSNA_KEY_DATA_LENGTH_FIELD_MINIMUM_SIZE);
       
   343 	if (status != eap_status_ok)
       
   344 	{
       
   345 		return EAP_STATUS_RETURN(m_am_tools, status);
       
   346 	}
       
   347 
       
   348 	status = set_oui(eapol_RSNA_key_data_oui_IEEE);
       
   349 	if (status != eap_status_ok)
       
   350 	{
       
   351 		return EAP_STATUS_RETURN(m_am_tools, status);
       
   352 	}
       
   353 
       
   354 	status = set_payload_type(eapol_RSNA_key_payload_type_none);
       
   355 	if (status != eap_status_ok)
       
   356 	{
       
   357 		return EAP_STATUS_RETURN(m_am_tools, status);
       
   358 	}
       
   359 
       
   360 	return EAP_STATUS_RETURN(m_am_tools, eap_status_ok);
       
   361 }
       
   362 
       
   363 //------------------------------------------------------
       
   364 
       
   365 // 
       
   366 eap_const_string eapol_rsna_key_data_header_c::get_descriptor_type_string() const
       
   367 {
       
   368 
       
   369 #if defined(USE_EAP_TRACE_STRINGS)
       
   370 	const eapol_RSNA_key_descriptor_type_e type = get_descriptor_type();
       
   371 #endif // #if defined(USE_EAP_TRACE_STRINGS)
       
   372 
       
   373 	if (m_is_RSNA_when_true == true
       
   374 		|| m_is_WPXM_when_true == true)
       
   375 	{
       
   376 #if defined(USE_EAP_TRACE_STRINGS)
       
   377 		EAP_IF_RETURN_STRING(type, eapol_RSNA_key_data_type_RSN_IE)
       
   378 		else EAP_IF_RETURN_STRING(type, eapol_RSNA_key_data_type_RSN_key_data)
       
   379 		else EAP_IF_RETURN_STRING(type, eapol_RSNA_key_data_type_none)
       
   380 		else
       
   381 #endif // #if defined(USE_EAP_TRACE_STRINGS)
       
   382 		{
       
   383 			return EAPL("Unknown EAPOL Descriptor type");
       
   384 		}
       
   385 	}
       
   386 	else
       
   387 	{
       
   388 #if defined(USE_EAP_TRACE_STRINGS)
       
   389 		EAP_IF_RETURN_STRING(type, eapol_RSNA_key_data_type_WPA_IE)
       
   390 		else EAP_IF_RETURN_STRING(type, eapol_RSNA_key_data_type_none)
       
   391 		else
       
   392 #endif // #if defined(USE_EAP_TRACE_STRINGS)
       
   393 		{
       
   394 			return EAPL("Unknown EAPOL Descriptor type");
       
   395 		}
       
   396 	}
       
   397 }
       
   398 
       
   399 //------------------------------------------------------
       
   400 
       
   401 // 
       
   402 eap_const_string eapol_rsna_key_data_header_c::get_payload_type_string() const
       
   403 {
       
   404 
       
   405 #if defined(USE_EAP_TRACE_STRINGS)
       
   406 	const eapol_RSNA_key_payload_type_e type = get_payload_type();
       
   407 
       
   408 	EAP_IF_RETURN_STRING(type, eapol_RSNA_key_payload_type_reserved)
       
   409 	else EAP_IF_RETURN_STRING(type, eapol_RSNA_key_payload_type_group_key_and_id)
       
   410 	else EAP_IF_RETURN_STRING(type, eapol_RSNA_key_payload_type_sta_key)
       
   411 	else EAP_IF_RETURN_STRING(type, eapol_RSNA_key_payload_type_pmkid)
       
   412 	else EAP_IF_RETURN_STRING(type, eapol_RSNA_key_payload_type_none)
       
   413 	else
       
   414 #endif // #if defined(USE_EAP_TRACE_STRINGS)
       
   415 	{
       
   416 		return EAPL("Unknown EAPOL Key Data type");
       
   417 	}
       
   418 }
       
   419 
       
   420 //------------------------------------------------------
       
   421 
       
   422 
       
   423 
       
   424 // End.