eapol/eapol_framework/eapol_common/am/include/eap_am_tools.h
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 
       
    20 
       
    21 #if !defined( _EAP_AM_TOOLS_H_ )
       
    22 #define _EAP_AM_TOOLS_H_
       
    23 
       
    24 #include "abs_eap_am_tools.h"
       
    25 #include "eap_variable_data.h"
       
    26 #include "eap_tools.h"
       
    27 #include "eap_status.h"
       
    28 #include "eap_am_export.h"
       
    29 #include "eap_status_string.h"
       
    30 #include "abs_eap_am_memory_store_data.h"
       
    31 
       
    32 
       
    33 //#if !defined(NO_EAP_AM_MEMORY_STORE)
       
    34 class eap_tlv_message_data_c;
       
    35 class eap_am_memory_store_c;
       
    36 //#endif //#if !defined(NO_EAP_AM_MEMORY_STORE)
       
    37 
       
    38 
       
    39 /// This class defines the common functions of tools.
       
    40 /// These are the platform independent functions used in tools.
       
    41 class EAP_EXPORT eap_am_tools_c
       
    42 : public abs_eap_am_tools_c
       
    43 //#if !defined(NO_EAP_AM_MEMORY_STORE)
       
    44 , public abs_eap_am_memory_store_data_c
       
    45 //#endif //#if !defined(NO_EAP_AM_MEMORY_STORE)
       
    46 {
       
    47 private:
       
    48 
       
    49 #if !defined(NO_EAP_AM_MEMORY_STORE)
       
    50 	eap_am_memory_store_c * m_memory_store;
       
    51 #endif //#if !defined(NO_EAP_AM_MEMORY_STORE)
       
    52 
       
    53 	/// This is trace mask. This could be changed dynamically with set_trace_mask() function.
       
    54 	u32_t m_trace_mask;
       
    55 
       
    56 #if defined(USE_EAP_ERROR_TESTS)
       
    57 	u32_t m_packet_index;
       
    58 #endif //#if defined(USE_EAP_ERROR_TESTS)
       
    59 
       
    60 	bool m_use_seconds_timestamp_in_traces;
       
    61 
       
    62 	bool m_thread_stopped;
       
    63 
       
    64 	bool m_use_timer_queue;
       
    65 
       
    66 	/// Function shutdown_am_tools() is called already.
       
    67 	bool m_shutdown_was_called;
       
    68 
       
    69 	/// This flag allows activation of trace when the error occurs.
       
    70 	/// Look at the set_activate_trace_on_error() and eap_status_return()
       
    71 	/// functions.
       
    72 	bool m_activate_trace_on_error;
       
    73 
       
    74 	u8_t m_tmp_buffer[256];
       
    75 	
       
    76 	u8_t m_tmp_ascii_buffer[256];
       
    77 
       
    78 
       
    79 	EAP_FUNC_IMPORT u8_t octet_to_ascii_armor(
       
    80 		const u8_t source_byte);
       
    81 
       
    82 	EAP_FUNC_IMPORT u8_t octet_from_ascii_armor(
       
    83 		const u8_t source_byte);
       
    84 
       
    85 	EAP_FUNC_IMPORT void convert_selected_bytes_to_ascii_armor(
       
    86 		const u8_t source_byte,
       
    87 		u32_t * const saved_bit_count,
       
    88 		u8_t * const saved_bits,
       
    89 		u8_t * const target,
       
    90 		u32_t * const output_ind,
       
    91 		const bool last_input_byte);
       
    92 
       
    93 	EAP_FUNC_IMPORT void restore_selected_bytes_from_ascii_armor(
       
    94 		const u8_t source_byte,
       
    95 		u32_t * const missing_bit_count,
       
    96 		u8_t * const target,
       
    97 		u32_t * const output_ind,
       
    98 		const bool last_input_byte);
       
    99 
       
   100 public:
       
   101 
       
   102 	/// These are valid values used with set_trace_mask() function.
       
   103 	enum
       
   104 	{
       
   105 		eap_trace_mask_none               = (0u),       ///< This value disables all traces.
       
   106 		eap_trace_mask_always             = (1u << 0u), ///< This values traces all permanent traces (EAP_TRACE_ALWAYS() macro).
       
   107 		eap_trace_mask_error              = (1u << 1u), ///< This values traces all errors.
       
   108 		eap_trace_mask_debug              = (1u << 2u), ///< This value traces debug information. Not used in released code.
       
   109 		eap_trace_mask_functions          = (1u << 3u), ///< This value traces function enters and returns.
       
   110 		eap_trace_mask_crypto             = (1u << 4u), ///< This value traces crypto data. Do not use on released code. All keys and intermediate values are traced.
       
   111 		eap_trace_mask_timer              = (1u << 5u), ///< This value traces the timer functions.
       
   112 		eap_trace_mask_eap_messages       = (1u << 6u), ///< This value traces only the EAP messages.
       
   113 		eap_trace_mask_test_vectors       = (1u << 7u), ///< This value traces only the test vectors of each EAP-type.
       
   114 		eap_trace_mask_crypto_sha1        = (1u << 8u), ///< This value traces only internal state of SHA1.
       
   115 		eap_trace_mask_crypto_md4         = (1u << 9u), ///< This value traces only internal state of MD4.
       
   116 		eap_trace_mask_crypto_rc4         = (1u << 10u), ///< This value traces only internal state of RC4.
       
   117 		eap_trace_mask_crypto_test_random = (1u << 11u), ///< This value traces only internal state of test random generator.
       
   118 		eap_trace_mask_message_data       = (1u << 12u), ///< This value traces only data of the messages, this will increase trace bloat.
       
   119 		eap_trace_mask_hash_map           = (1u << 13u), ///< This value traces only data of hash table. It does map data and selector.
       
   120 		eap_trace_mask_timer_queue        = (1u << 14u), ///< This value traces the timer queue.
       
   121 		eap_trace_mask_ok_returns         = (1u << 15u), ///< This value traces the OK return values, alot of them.
       
   122 	};
       
   123 
       
   124 	EAP_FUNC_IMPORT u8_t octet_to_ascii(i32_t octet);
       
   125 
       
   126 	EAP_FUNC_IMPORT u8_t ascii_to_octet(i32_t character);
       
   127 
       
   128 	EAP_FUNC_IMPORT virtual ~eap_am_tools_c();
       
   129 
       
   130 	EAP_FUNC_IMPORT eap_am_tools_c();
       
   131 
       
   132 	EAP_FUNC_IMPORT bool get_use_seconds_timestamp_in_traces();
       
   133 
       
   134 	EAP_FUNC_IMPORT bool get_thread_stopped();
       
   135 
       
   136 	EAP_FUNC_IMPORT void set_use_timer_queue();
       
   137 
       
   138 	EAP_FUNC_IMPORT bool get_use_timer_queue();
       
   139 
       
   140 	EAP_FUNC_IMPORT u32_t get_trace_mask() const;
       
   141 
       
   142 	EAP_FUNC_IMPORT void set_trace_mask(const u32_t mask);
       
   143 
       
   144 	EAP_FUNC_IMPORT void set_activate_trace_on_error();
       
   145 
       
   146 	EAP_FUNC_IMPORT void check_activate_trace_on_error();
       
   147 
       
   148 	EAP_FUNC_IMPORT void trace_data(eap_const_string prefix, const void * const data, const u32_t data_length);
       
   149 
       
   150 	EAP_FUNC_IMPORT eap_status_e timer_thread_function();
       
   151 
       
   152 	EAP_FUNC_IMPORT eap_status_e convert_ascii_to_uppercase(
       
   153 		u8_t * const source_bytes,
       
   154 		const u32_t source_bytes_length);
       
   155 
       
   156 	EAP_FUNC_IMPORT eap_status_e convert_bytes_to_hex_ascii(
       
   157 		const u8_t * const source_bytes,
       
   158 		const u32_t source_bytes_length,
       
   159 		u8_t * const target,
       
   160 		u32_t *target_length);
       
   161 
       
   162 	EAP_FUNC_IMPORT eap_status_e convert_bytes_to_hex_ascii(
       
   163 		const void * const source_bytes,
       
   164 		const u32_t source_bytes_length,
       
   165 		eap_variable_data_c * const target);
       
   166 
       
   167 	EAP_FUNC_IMPORT eap_status_e convert_hex_ascii_to_bytes(
       
   168 		const u8_t * const source_bytes,
       
   169 		const u32_t source_bytes_length,
       
   170 		u8_t * const target,
       
   171 		u32_t *target_length);
       
   172 
       
   173 	EAP_FUNC_IMPORT eap_status_e convert_hex_ascii_to_bytes(
       
   174 		const void * const source_bytes,
       
   175 		const u32_t source_bytes_length,
       
   176 		eap_variable_data_c * const target);
       
   177 
       
   178 	
       
   179 	EAP_FUNC_IMPORT eap_status_e convert_bytes_to_ascii_armor(
       
   180 		const u8_t * const source_bytes,
       
   181 		const u32_t source_bytes_length,
       
   182 		u8_t * const target,
       
   183 		u32_t *target_length);
       
   184 
       
   185 	EAP_FUNC_IMPORT eap_status_e restore_bytes_from_ascii_armor(
       
   186 		const u8_t * const source_bytes,
       
   187 		const u32_t source_bytes_length,
       
   188 		u8_t * const target,
       
   189 		u32_t *target_length);
       
   190 
       
   191 	EAP_FUNC_IMPORT eap_status_e eap_status_return(
       
   192 		const bool print_error_when_true,
       
   193 		const eap_status_e status,
       
   194 		const eap_char * const file_name,
       
   195 		const i32_t line_number);
       
   196 
       
   197 	EAP_FUNC_IMPORT eap_status_e eap_status_return_file_number(
       
   198 		const bool print_error_when_true,
       
   199 		const eap_status_e status,
       
   200 		const u32_t file_date,
       
   201 		const u32_t file_number,
       
   202 		const i32_t line_number);
       
   203 
       
   204 
       
   205 	// This is documented in abs_eap_am_tools_c::memory_store_add_data().
       
   206 	EAP_FUNC_IMPORT eap_status_e memory_store_add_data(
       
   207 		const eap_variable_data_c * const key,
       
   208 		eap_tlv_message_data_c * const data,
       
   209 		const u32_t timeout);
       
   210 
       
   211 	// This is documented in abs_eap_am_tools_c::memory_store_get_data().
       
   212 	EAP_FUNC_IMPORT eap_status_e memory_store_get_data(
       
   213 		const eap_variable_data_c * const key,
       
   214 		eap_tlv_message_data_c * const data);
       
   215 
       
   216 	// This is documented in abs_eap_am_tools_c::memory_store_remove_data().
       
   217 	EAP_FUNC_IMPORT eap_status_e memory_store_remove_data(
       
   218 		const eap_variable_data_c * const key);
       
   219 
       
   220 
       
   221 	/// This function shuts down eap_am_tools_c object.
       
   222 	EAP_FUNC_IMPORT eap_status_e shutdown_am_tools();
       
   223 
       
   224 	/// The implementing function must call eap_am_tools_c::shutdown_am_tools().
       
   225 	EAP_FUNC_IMPORT virtual eap_status_e shutdown() = 0;
       
   226 
       
   227 	/**
       
   228 	 * Function converts unicode characters into UTF8 characters.
       
   229 	 * @param dest is reference to destination utf8 variable data.
       
   230 	 * @param src is refrence to unicode variable data.
       
   231 	 * @return eap status code.
       
   232 	 */
       
   233 	EAP_FUNC_IMPORT eap_status_e generic_convert_unicode_to_utf8(
       
   234 		eap_variable_data_c & dest,
       
   235 		const eap_variable_data_c & src);
       
   236 
       
   237 	/**
       
   238 	 * Function converts UTF8 characters into unicode characters.
       
   239 	 * @param dest is reference to destination unicode variable data.
       
   240 	 * @param src is refrence to UTF8 variable data.
       
   241 	 * @return eap status code.
       
   242 	 */
       
   243 	EAP_FUNC_IMPORT eap_status_e generic_convert_utf8_to_unicode(
       
   244 		eap_variable_data_c & dest,
       
   245 		const eap_variable_data_c & src);
       
   246 
       
   247 	/**
       
   248 	 * Function parses NAI to usename and realm.
       
   249 	 * If either is missing the corresponding value will be invalid.
       
   250 	 */
       
   251 	EAP_FUNC_IMPORT eap_status_e parse_nai(
       
   252 		const eap_variable_data_c * const nai,
       
   253 		eap_variable_data_c * const username,
       
   254 		eap_variable_data_c * const realm);
       
   255 
       
   256 
       
   257 #if defined(USE_EAP_ERROR_TESTS)
       
   258 	EAP_FUNC_IMPORT eap_status_e generate_random_error(
       
   259 		eap_buf_chain_wr_c * const sent_packet,
       
   260 		const bool forse_error,
       
   261 		const u32_t packet_index,
       
   262 		const u32_t minimum_index,
       
   263 		const u32_t error_probability,
       
   264 		const u32_t minimum_packet_length);
       
   265 
       
   266 	EAP_FUNC_IMPORT u32_t get_packet_index();
       
   267 
       
   268 	EAP_FUNC_IMPORT void increase_packet_index();
       
   269 #endif //#if defined(USE_EAP_ERROR_TESTS)
       
   270 
       
   271 
       
   272 	EAP_FUNC_IMPORT eap_status_e number_string_to_u32(
       
   273 		const u8_t * const number_string,
       
   274 		const u32_t number_string_length,
       
   275 		u32_t * const integer);
       
   276 
       
   277 	EAP_FUNC_IMPORT void trace_configuration(
       
   278 		const eap_status_e configuration_read_status,
       
   279 		const eap_configuration_field_c * const field,
       
   280 		const eap_variable_data_c * const data);
       
   281 
       
   282 	EAP_FUNC_IMPORT u64_t xor_u64(const u64_t a, const u64_t b);
       
   283 
       
   284 	EAP_FUNC_IMPORT u64_t multiply_u64(const u64_t a, const u64_t b);
       
   285 
       
   286 	EAP_FUNC_IMPORT i32_t compare_u64(const u64_t a, const u64_t b);
       
   287 
       
   288 	EAP_FUNC_IMPORT eap_status_e create_uuid_v5( 
       
   289 		const void* const ns_uuid,
       
   290 		const u32_t ns_uuid_length,
       
   291 		const void* const name, 
       
   292 		const u32_t name_length,
       
   293 		eap_variable_data_c* const uuid );
       
   294 
       
   295 	EAP_FUNC_IMPORT eap_status_e create_uuid_v5_from_mac_address( 
       
   296 		const u8_t* const mac_address, 
       
   297 		const u32_t mac_address_length,
       
   298 		eap_variable_data_c* const uuid );
       
   299 
       
   300 };
       
   301 
       
   302 /** @file */
       
   303 
       
   304 //-----------------------------------------------------------------------------------------------
       
   305 
       
   306 /// This is the default trace mask.
       
   307 const u32_t TRACE_FLAGS_DEFAULT = eap_am_tools_c::eap_trace_mask_debug;
       
   308 const u32_t TRACE_FLAGS_ERROR = eap_am_tools_c::eap_trace_mask_error;
       
   309 const u32_t TRACE_FLAGS_ALWAYS = eap_am_tools_c::eap_trace_mask_always;
       
   310 
       
   311 const u32_t TRACE_TEST_VECTORS = eap_am_tools_c::eap_trace_mask_test_vectors;
       
   312 
       
   313 const u32_t EAP_TRACE_FLAGS_MESSAGE_DATA = eap_am_tools_c::eap_trace_mask_message_data;
       
   314 
       
   315 const u32_t EAP_TRACE_MASK_HASH_MAP = eap_am_tools_c::eap_trace_mask_hash_map;
       
   316 
       
   317 /// This mask activates an additional timer trace.
       
   318 const u32_t TRACE_FLAGS_TIMER = eap_am_tools_c::eap_trace_mask_timer;
       
   319 const u32_t TRACE_FLAGS_TIMER_QUEUE = eap_am_tools_c::eap_trace_mask_timer_queue;
       
   320 
       
   321 const u32_t TRACE_FLAGS_OK_RETURNS = eap_am_tools_c::eap_trace_mask_ok_returns;
       
   322 
       
   323 //-----------------------------------------------------------------------------------------------
       
   324 
       
   325 #if !defined(USE_EAP_STATUS_RETURN)
       
   326 
       
   327 	#define EAP_STATUS_RETURN(tools, status) (status)
       
   328 
       
   329 	#define EAP_STATUS_RETURN_WARNING(tools, status) (status)
       
   330 
       
   331 #else
       
   332 
       
   333 	#if defined(USE_EAP_MINIMUM_RELEASE_TRACES) && !defined(_DEBUG) && !defined(DEBUG)
       
   334 		// This is used only in release version.
       
   335 
       
   336 		#if !defined(EAP_FILE_NUMBER_ENUM)
       
   337 			#define EAP_FILE_NUMBER_ENUM 0ul
       
   338 		#endif //#if defined(EAP_FILE_NUMBER_ENUM)
       
   339 
       
   340 		#if !defined(EAP_FILE_NUMBER_DATE)
       
   341 			#define EAP_FILE_NUMBER_DATE 0ul
       
   342 		#endif //#if defined(EAP_FILE_NUMBER_DATE)
       
   343 
       
   344 		#define EAP_STATUS_RETURN(tools, status) \
       
   345 			((tools != 0 && (*(tools)).get_trace_mask()) \
       
   346 			? (*(tools)).eap_status_return_file_number( \
       
   347 				true, \
       
   348 				(status), \
       
   349 				EAP_FILE_NUMBER_DATE, \
       
   350 				EAP_FILE_NUMBER_ENUM, \
       
   351 				__LINE__) \
       
   352 			 : status)
       
   353 
       
   354 		#define EAP_STATUS_RETURN_WARNING(tools, status) \
       
   355 			((tools != 0 && (*(tools)).get_trace_mask()) \
       
   356 			? (*(tools)).eap_status_return_file_number( \
       
   357 				false, \
       
   358 				(status), \
       
   359 				EAP_FILE_NUMBER_DATE, \
       
   360 				EAP_FILE_NUMBER_ENUM, \
       
   361 				__LINE__) \
       
   362 			 : status)
       
   363 
       
   364 	#else
       
   365 
       
   366 		#define EAP_STATUS_RETURN(tools, status) \
       
   367 			((tools != 0 && (*(tools)).get_trace_mask()) \
       
   368 			? (*(tools)).eap_status_return( \
       
   369 				true, \
       
   370 				(status), \
       
   371 				__FILE__, \
       
   372 				__LINE__) \
       
   373 			: status)
       
   374 
       
   375 		#define EAP_STATUS_RETURN_WARNING(tools, status) \
       
   376 			((tools != 0 && (*(tools)).get_trace_mask()) \
       
   377 			? (*(tools)).eap_status_return( \
       
   378 				false, \
       
   379 				(status), \
       
   380 				__FILE__, \
       
   381 				__LINE__) \
       
   382 			: status)
       
   383 
       
   384 	#endif //#if defined(USE_EAP_MINIMUM_RELEASE_TRACES)
       
   385 
       
   386 #endif //#if !defined(USE_EAP_STATUS_RETURN)
       
   387 
       
   388 //-----------------------------------------------------------------------------------------------
       
   389 
       
   390 #endif //#if !defined( _EAP_AM_TOOLS_H_ )
       
   391 
       
   392 
       
   393 
       
   394 // End.