eapol/eapol_framework/eapol_common/common/eap_expanded_type.cpp
branchRCL_3
changeset 46 c74b3d9f6b9e
parent 45 bad0cc58d154
equal deleted inserted replaced
45:bad0cc58d154 46:c74b3d9f6b9e
    14 * Description:  EAP and WLAN authentication protocols.
    14 * Description:  EAP and WLAN authentication protocols.
    15 *
    15 *
    16 */
    16 */
    17 
    17 
    18 /*
    18 /*
    19 * %version: %
    19 * %version: 12.1.2 %
    20 */
    20 */
    21 
    21 
    22 // This is enumeration of EAPOL source code.
    22 // This is enumeration of EAPOL source code.
    23 #if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
    23 #if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
    24 	#undef EAP_FILE_NUMBER_ENUM
    24 	#undef EAP_FILE_NUMBER_ENUM
    65 	m_vendor_type = static_cast<u32_t>(type);
    65 	m_vendor_type = static_cast<u32_t>(type);
    66 }
    66 }
    67 
    67 
    68 //--------------------------------------------------
    68 //--------------------------------------------------
    69 
    69 
    70 EAP_FUNC_EXPORT bool eap_expanded_type_c::get_is_valid() const
       
    71 {
       
    72 	return true;
       
    73 }
       
    74 
       
    75 //--------------------------------------------------
       
    76 
       
    77 EAP_FUNC_EXPORT bool eap_expanded_type_c::get_is_valid_data() const
       
    78 {
       
    79 	return get_is_valid();
       
    80 }
       
    81 
       
    82 //--------------------------------------------------
       
    83 
       
    84 EAP_FUNC_EXPORT eap_expanded_type_c * eap_expanded_type_c::copy() const
       
    85 {
       
    86 	eap_expanded_type_c * const new_entry = new eap_expanded_type_c;
       
    87 
       
    88 	if (new_entry == 0
       
    89 		|| new_entry->get_is_valid() == false)
       
    90 	{
       
    91 		delete new_entry;
       
    92 		return 0;
       
    93 	}
       
    94 
       
    95 	new_entry->set_eap_type_values(m_vendor_id, m_vendor_type);
       
    96 
       
    97 	return new_entry;
       
    98 }
       
    99 
       
   100 //--------------------------------------------------
       
   101 
       
   102 EAP_FUNC_EXPORT bool eap_expanded_type_c::is_expanded_type(const eap_type_ietf_values_e eap_type)
    70 EAP_FUNC_EXPORT bool eap_expanded_type_c::is_expanded_type(const eap_type_ietf_values_e eap_type)
   103 {
    71 {
   104 	return (eap_type == eap_type_expanded_type);
    72 	return (eap_type == eap_type_expanded_type);
   105 }
    73 }
   106 
    74 
   107 //--------------------------------------------------
    75 //--------------------------------------------------
   108 
    76 
       
    77 #if defined(USE_EAP_EXPANDED_TYPES)
   109 EAP_FUNC_EXPORT bool eap_expanded_type_c::is_ietf_type(const eap_expanded_type_c eap_type)
    78 EAP_FUNC_EXPORT bool eap_expanded_type_c::is_ietf_type(const eap_expanded_type_c eap_type)
   110 {
    79 {
   111 	return (eap_type.get_vendor_id() == eap_type_vendor_id_ietf
    80 	return (eap_type.get_vendor_id() == eap_type_vendor_id_ietf
   112 			&& eap_type.get_vendor_type() < eap_type_expanded_type);
    81 			&& eap_type.get_vendor_type() < eap_type_expanded_type);
   113 }
    82 }
   114 
    83 
       
    84 #else
       
    85 EAP_FUNC_EXPORT bool eap_expanded_type_c::is_ietf_type(const eap_type_ietf_values_e eap_type)
       
    86 {
       
    87 	return (eap_type < eap_type_expanded_type);
       
    88 }
       
    89 
       
    90 #endif //#if defined(USE_EAP_EXPANDED_TYPES)
       
    91 
   115 //--------------------------------------------------
    92 //--------------------------------------------------
   116 
    93 
   117 EAP_FUNC_EXPORT eap_status_e eap_expanded_type_c::get_type_data(
    94 EAP_FUNC_EXPORT eap_status_e eap_expanded_type_c::get_type_data(
   118 	abs_eap_am_tools_c * const am_tools,
    95 	abs_eap_am_tools_c * const am_tools,
   119 	eap_type_ietf_values_e * const type) const
    96 	eap_type_ietf_values_e * const type)
   120 {
    97 {
   121 	if (type == 0)
    98 	if (type == 0)
   122 	{
    99 	{
   123 		EAP_UNREFERENCED_PARAMETER(am_tools);
   100 		EAP_UNREFERENCED_PARAMETER(am_tools);
   124 		return EAP_STATUS_RETURN(am_tools, eap_status_illegal_parameter);
   101 		return EAP_STATUS_RETURN(am_tools, eap_status_illegal_parameter);
   141 
   118 
   142 //--------------------------------------------------
   119 //--------------------------------------------------
   143 
   120 
   144 EAP_FUNC_EXPORT eap_status_e eap_expanded_type_c::get_type_data(
   121 EAP_FUNC_EXPORT eap_status_e eap_expanded_type_c::get_type_data(
   145 	abs_eap_am_tools_c * const am_tools,
   122 	abs_eap_am_tools_c * const am_tools,
   146 	eap_expanded_type_c * const type) const
   123 	eap_expanded_type_c * const type)
   147 {
   124 {
   148 	if (type == 0)
   125 	if (type == 0)
   149 	{
   126 	{
   150 		EAP_UNREFERENCED_PARAMETER(am_tools);
   127 		EAP_UNREFERENCED_PARAMETER(am_tools);
   151 		return EAP_STATUS_RETURN(am_tools, eap_status_illegal_parameter);
   128 		return EAP_STATUS_RETURN(am_tools, eap_status_illegal_parameter);
   158 
   135 
   159 //--------------------------------------------------
   136 //--------------------------------------------------
   160 
   137 
   161 EAP_FUNC_EXPORT eap_status_e eap_expanded_type_c::get_expanded_type_data(
   138 EAP_FUNC_EXPORT eap_status_e eap_expanded_type_c::get_expanded_type_data(
   162 	abs_eap_am_tools_c * const am_tools,
   139 	abs_eap_am_tools_c * const am_tools,
   163 	eap_variable_data_c * const data) const
   140 	eap_variable_data_c * const data)
   164 {
   141 {
   165 	if (data == 0
   142 	if (data == 0)
   166 		|| data->get_is_valid() == false)
       
   167 	{
   143 	{
   168 		EAP_UNREFERENCED_PARAMETER(am_tools);
   144 		EAP_UNREFERENCED_PARAMETER(am_tools);
   169 		return EAP_STATUS_RETURN(am_tools, eap_status_illegal_parameter);
   145 		return EAP_STATUS_RETURN(am_tools, eap_status_illegal_parameter);
   170 	}
   146 	}
   171 
   147 
   243 
   219 
   244 EAP_FUNC_EXPORT eap_status_e eap_expanded_type_c::set_expanded_type_data(
   220 EAP_FUNC_EXPORT eap_status_e eap_expanded_type_c::set_expanded_type_data(
   245 	abs_eap_am_tools_c * const am_tools,
   221 	abs_eap_am_tools_c * const am_tools,
   246 	const eap_variable_data_c * const data)
   222 	const eap_variable_data_c * const data)
   247 {
   223 {
   248 	if (data == 0
   224 	if (data->get_data_length() != get_eap_expanded_type_size()
   249 		|| data->get_data_length() != get_eap_expanded_type_size()
       
   250 		|| data->get_data(data->get_data_length()) == 0)
   225 		|| data->get_data(data->get_data_length()) == 0)
   251 	{
   226 	{
   252 		EAP_UNREFERENCED_PARAMETER(am_tools);
   227 		EAP_UNREFERENCED_PARAMETER(am_tools);
   253 		return EAP_STATUS_RETURN(am_tools, eap_status_illegal_parameter);
   228 		return EAP_STATUS_RETURN(am_tools, eap_status_illegal_parameter);
   254 	}
   229 	}
   280 		m_vendor_id = static_cast<eap_type_vendor_id_e>(eap_read_u24_t_network_order(vendor_id, m_vendor_id_size));
   255 		m_vendor_id = static_cast<eap_type_vendor_id_e>(eap_read_u24_t_network_order(vendor_id, m_vendor_id_size));
   281 	}
   256 	}
   282 
   257 
   283 	{
   258 	{
   284 		u8_t * const vendor_type = data->get_data_offset(offset, m_vendor_type_size);
   259 		u8_t * const vendor_type = data->get_data_offset(offset, m_vendor_type_size);
   285 		if (vendor_type == 0)
       
   286 		{
       
   287 			return EAP_STATUS_RETURN(am_tools, eap_status_illegal_parameter);
       
   288 		}
       
   289 		offset += m_vendor_type_size;
       
   290 
       
   291 		m_vendor_type = eap_read_u32_t_network_order(vendor_type, m_vendor_type_size);
       
   292 	}
       
   293 
       
   294 	return EAP_STATUS_RETURN(am_tools, eap_status_ok);
       
   295 }
       
   296 
       
   297 //--------------------------------------------------
       
   298 
       
   299 EAP_FUNC_EXPORT eap_status_e eap_expanded_type_c::set_expanded_type_data(
       
   300 	abs_eap_am_tools_c * const am_tools,
       
   301 	const void * const data,
       
   302 	const u32_t data_length)
       
   303 {
       
   304 	if (data_length != get_eap_expanded_type_size()
       
   305 		|| data == 0)
       
   306 	{
       
   307 		EAP_UNREFERENCED_PARAMETER(am_tools);
       
   308 		return EAP_STATUS_RETURN(am_tools, eap_status_illegal_parameter);
       
   309 	}
       
   310 
       
   311 	u32_t offset = 0ul;
       
   312 
       
   313 	{
       
   314 		const u8_t * const ietf_type = &(reinterpret_cast<const u8_t *>(data)[offset]);
       
   315 		if (ietf_type == 0)
       
   316 		{
       
   317 			return EAP_STATUS_RETURN(am_tools, eap_status_illegal_parameter);
       
   318 		}
       
   319 		offset += m_ietf_type_size;
       
   320 
       
   321 		if (static_cast<eap_type_ietf_values_e>(*ietf_type) != eap_type_expanded_type)
       
   322 		{
       
   323 			return EAP_STATUS_RETURN(am_tools, eap_status_illegal_parameter);
       
   324 		}
       
   325 	}
       
   326 
       
   327 	{
       
   328 		const u8_t * const vendor_id = &(reinterpret_cast<const u8_t *>(data)[offset]);
       
   329 		if (vendor_id == 0)
       
   330 		{
       
   331 			return EAP_STATUS_RETURN(am_tools, eap_status_illegal_parameter);
       
   332 		}
       
   333 		offset += m_vendor_id_size;
       
   334 
       
   335 		m_vendor_id = static_cast<eap_type_vendor_id_e>(eap_read_u24_t_network_order(vendor_id, m_vendor_id_size));
       
   336 	}
       
   337 
       
   338 	{
       
   339 		const u8_t * const vendor_type = &(reinterpret_cast<const u8_t *>(data)[offset]);
       
   340 		if (vendor_type == 0)
   260 		if (vendor_type == 0)
   341 		{
   261 		{
   342 			return EAP_STATUS_RETURN(am_tools, eap_status_illegal_parameter);
   262 			return EAP_STATUS_RETURN(am_tools, eap_status_illegal_parameter);
   343 		}
   263 		}
   344 		offset += m_vendor_type_size;
   264 		offset += m_vendor_type_size;
   469 EAP_FUNC_EXPORT eap_status_e eap_expanded_type_c::read_type(
   389 EAP_FUNC_EXPORT eap_status_e eap_expanded_type_c::read_type(
   470 	abs_eap_am_tools_c * const am_tools,
   390 	abs_eap_am_tools_c * const am_tools,
   471 	const u32_t index,
   391 	const u32_t index,
   472 	const void * const p_buffer,
   392 	const void * const p_buffer,
   473 	const u32_t buffer_length,
   393 	const u32_t buffer_length,
       
   394 #if defined(USE_EAP_EXPANDED_TYPES)
   474 	eap_expanded_type_c * const type
   395 	eap_expanded_type_c * const type
       
   396 #else
       
   397 	eap_type_ietf_values_e * const type
       
   398 #endif //#if defined(USE_EAP_EXPANDED_TYPES)
   475 	)
   399 	)
   476 {
   400 {
   477 	if (p_buffer == 0)
   401 	if (p_buffer == 0)
   478 	{
   402 	{
   479 		EAP_UNREFERENCED_PARAMETER(am_tools);
   403 		EAP_UNREFERENCED_PARAMETER(am_tools);
   573 					vendor_type_value = eap_read_u32_t_network_order(
   497 					vendor_type_value = eap_read_u32_t_network_order(
   574 						vendor_type,
   498 						vendor_type,
   575 						sizeof(u32_t));
   499 						sizeof(u32_t));
   576 				}
   500 				}
   577 
   501 
       
   502 #if defined(USE_EAP_EXPANDED_TYPES)
   578 				type->set_eap_type_values(
   503 				type->set_eap_type_values(
   579 					vendor_id_value,
   504 					vendor_id_value,
   580 					vendor_type_value);
   505 					vendor_type_value);
   581 
   506 
   582 				EAP_ASSERT_TOOLS(am_tools, (ietf_eap_type == eap_type_expanded_type));
   507 				EAP_ASSERT_TOOLS(am_tools, (ietf_eap_type == eap_type_expanded_type));
       
   508 #else
       
   509 				if (vendor_id_value == eap_type_vendor_id_ietf)
       
   510 				{
       
   511 					*type = static_cast<eap_type_value_e>(vendor_type_value); // Type field follows eap_header_c.
       
   512 				}
       
   513 				else
       
   514 				{
       
   515 					*type = ietf_eap_type; // Type field follows eap_header_c.
       
   516 				}
       
   517 #endif
   583 
   518 
   584 				return EAP_STATUS_RETURN(am_tools, eap_status_ok);
   519 				return EAP_STATUS_RETURN(am_tools, eap_status_ok);
   585 			}
   520 			}
   586 			else
   521 			else
   587 			{
   522 			{
   606 	abs_eap_am_tools_c * const am_tools,
   541 	abs_eap_am_tools_c * const am_tools,
   607 	const u32_t index,
   542 	const u32_t index,
   608 	void * const p_buffer,
   543 	void * const p_buffer,
   609 	const u32_t buffer_length,
   544 	const u32_t buffer_length,
   610 	const bool write_extented_type_when_true,
   545 	const bool write_extented_type_when_true,
       
   546 #if defined(USE_EAP_EXPANDED_TYPES)
   611 	const eap_expanded_type_c p_type ///< The EAP type to be written.
   547 	const eap_expanded_type_c p_type ///< The EAP type to be written.
       
   548 #else
       
   549 	const eap_type_ietf_values_e p_type ///< The EAP type to be written.
       
   550 #endif //#if defined(USE_EAP_EXPANDED_TYPES)
   612 	)
   551 	)
   613 {
   552 {
   614 	if (p_buffer == 0)
   553 	if (p_buffer == 0)
   615 	{
   554 	{
   616 		EAP_UNREFERENCED_PARAMETER(am_tools);
   555 		EAP_UNREFERENCED_PARAMETER(am_tools);
   617 		return EAP_STATUS_RETURN(am_tools, eap_status_illegal_parameter);
   556 		return EAP_STATUS_RETURN(am_tools, eap_status_illegal_parameter);
   618 	}
   557 	}
   619 
   558 
   620 	u8_t * const buffer = static_cast<u8_t *>(p_buffer);
   559 	u8_t * const buffer = static_cast<u8_t *>(p_buffer);
       
   560 
       
   561 #if defined(USE_EAP_EXPANDED_TYPES)
   621 
   562 
   622 	if (write_extented_type_when_true == false
   563 	if (write_extented_type_when_true == false
   623 		&& is_ietf_type(p_type) == true
   564 		&& is_ietf_type(p_type) == true
   624 		&& buffer_length >= eap_expanded_type_c::m_ietf_type_size*(index+1ul))
   565 		&& buffer_length >= eap_expanded_type_c::m_ietf_type_size*(index+1ul))
   625 	{
   566 	{
   689 	else
   630 	else
   690 	{
   631 	{
   691 		return EAP_STATUS_RETURN(am_tools, eap_status_allocation_error);
   632 		return EAP_STATUS_RETURN(am_tools, eap_status_allocation_error);
   692 	}
   633 	}
   693 
   634 
       
   635 #else
       
   636 
       
   637 	EAP_UNREFERENCED_PARAMETER(write_extented_type_when_true); // Only Expanded Type version uses this.
       
   638 
       
   639 	if (buffer_length >= eap_expanded_type_c::m_ietf_type_size*(index+1ul))
       
   640 	{
       
   641 		u8_t * const type_data =
       
   642 			&(buffer[eap_expanded_type_c::m_ietf_type_size*index]);
       
   643 		if (type_data == 0)
       
   644 		{
       
   645 			return EAP_STATUS_RETURN(am_tools, eap_status_allocation_error);
       
   646 		}
       
   647 		*type_data = static_cast<u8_t>(p_type);
       
   648 	}
       
   649 	else
       
   650 	{
       
   651 		return EAP_STATUS_RETURN(am_tools, eap_status_allocation_error);
       
   652 	}
       
   653 
       
   654 #endif //#if defined(USE_EAP_EXPANDED_TYPES)
       
   655 
   694 	return EAP_STATUS_RETURN(am_tools, eap_status_ok);
   656 	return EAP_STATUS_RETURN(am_tools, eap_status_ok);
   695 }
   657 }
   696 
   658 
   697 //--------------------------------------------------
   659 //--------------------------------------------------
       
   660 
       
   661 #if defined(USE_EAP_EXPANDED_TYPES)
   698 
   662 
   699 EAP_FUNC_EXPORT i32_t eap_expanded_type_c::compare(const eap_expanded_type_c * const data) const
   663 EAP_FUNC_EXPORT i32_t eap_expanded_type_c::compare(const eap_expanded_type_c * const data) const
   700 {
   664 {
   701 	if (*this == *data)
   665 	if (*this == *data)
   702 	{
   666 	{
   717 			return +1;
   681 			return +1;
   718 		}
   682 		}
   719 	}
   683 	}
   720 }
   684 }
   721 
   685 
       
   686 #endif //#if defined(USE_EAP_EXPANDED_TYPES)
       
   687 
   722 //--------------------------------------------------
   688 //--------------------------------------------------
   723 //--------------------------------------------------
   689 //--------------------------------------------------
   724 //--------------------------------------------------
   690 //--------------------------------------------------
   725 
   691 
   726 EAP_FUNC_EXPORT const eap_expanded_type_c &eap_static_expanded_type_c::get_type() const
   692 EAP_FUNC_EXPORT const eap_expanded_type_c &eap_static_expanded_type_c::get_type() const
   729 }
   695 }
   730 
   696 
   731 //--------------------------------------------------
   697 //--------------------------------------------------
   732 //--------------------------------------------------
   698 //--------------------------------------------------
   733 //--------------------------------------------------
   699 //--------------------------------------------------
       
   700 
       
   701 #if defined(USE_EAP_EXPANDED_TYPES)
   734 
   702 
   735 EAP_C_FUNC_EXPORT u32_t convert_eap_type_to_u32_t(eap_type_value_e type)
   703 EAP_C_FUNC_EXPORT u32_t convert_eap_type_to_u32_t(eap_type_value_e type)
   736 {
   704 {
   737 	// NOTE, this returns only 8 least significant bits of vendor type.
   705 	// NOTE, this returns only 8 least significant bits of vendor type.
   738 	return static_cast<u32_t>(type.get_vendor_id() << sizeof(u8_t)*8ul
   706 	return static_cast<u32_t>(type.get_vendor_id() << sizeof(u8_t)*8ul
   743 {
   711 {
   744 	return static_cast<u64_t>(type.get_vendor_id()) << sizeof(u32_t)*8ul
   712 	return static_cast<u64_t>(type.get_vendor_id()) << sizeof(u32_t)*8ul
   745 		| static_cast<u64_t>(type.get_vendor_type());
   713 		| static_cast<u64_t>(type.get_vendor_type());
   746 }
   714 }
   747 
   715 
       
   716 #else
       
   717 
       
   718 EAP_C_FUNC_EXPORT u32_t convert_eap_type_to_u32_t(eap_type_value_e type)
       
   719 {
       
   720 	return static_cast<u32_t>(type);
       
   721 }
       
   722 
       
   723 EAP_C_FUNC_EXPORT u64_t convert_eap_type_to_u64_t(eap_type_value_e type)
       
   724 {
       
   725 	return static_cast<u64_t>(type);
       
   726 }
       
   727 
       
   728 #endif //#if defined(USE_EAP_EXPANDED_TYPES)
       
   729 
   748 //--------------------------------------------------
   730 //--------------------------------------------------
   749 //--------------------------------------------------
   731 //--------------------------------------------------
   750 //--------------------------------------------------
   732 //--------------------------------------------------
   751 
   733 
   752 
   734