eapol/eapol_framework/eapol_common/include/isakmp_header.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(_ISAKMP_HEADER_H_)
       
    22 #define _ISAKMP_HEADER_H_
       
    23 
       
    24 #include "eap_am_memory.h"
       
    25 #include "sae_cookie.h"
       
    26 
       
    27 
       
    28 enum isakmp_version_value_e
       
    29 {
       
    30 	isakmp_version_none = 0,
       
    31 	isakmp_version_current = ((1 << 4) | (0)),
       
    32 };
       
    33 
       
    34 enum isakmp_exchange_type_value_e
       
    35 {
       
    36 	isakmp_exchange_type = 240, // This is in private area.
       
    37 };
       
    38 
       
    39 enum isakmp_payload_type_e
       
    40 {
       
    41 	isakmp_payload_NONE                =   0,
       
    42 	isakmp_payload_sa                  =   1,
       
    43 	isakmp_payload_proposal            =   2,
       
    44 	isakmp_payload_transform           =   3,
       
    45 	isakmp_payload_key_exchange        =   4,
       
    46 	isakmp_payload_identification      =   5,
       
    47 	isakmp_payload_certificate         =   6,
       
    48 	isakmp_payload_certificate_request =   7,
       
    49 	isakmp_payload_mac                 =   8,
       
    50 	isakmp_payload_signature           =   9,
       
    51 	isakmp_payload_nonce               =  10,
       
    52 	isakmp_payload_notification        =  11,
       
    53 	isakmp_payload_delete              =  12,
       
    54 	isakmp_payload_vendor_id           =  13,
       
    55 	isakmp_payload_EAP                 = 103, // Found from draft-ietf-ipsra-pic-01.txt.
       
    56 	isakmp_payload_legacy_mac          = 128, // NOTE this is in private area.
       
    57 };
       
    58 
       
    59 
       
    60 enum isakmp_attribute_type_e
       
    61 {
       
    62 	isakmp_attribute_Encryption_Algorithm               =  1, // B
       
    63 	isakmp_attribute_Hash_Algorithm                     =  2, // B
       
    64 	isakmp_attribute_Authentication_Method              =  3, // B
       
    65 	isakmp_attribute_Group_Description                  =  4, // B
       
    66 	isakmp_attribute_Group_Type                         =  5, // B
       
    67 	isakmp_attribute_Group_Prime_Irreducible_Polynomial =  6, // V
       
    68 	isakmp_attribute_Group_Generator_One                =  7, // V
       
    69 	isakmp_attribute_Group_Generator_Two                =  8, // V
       
    70 	isakmp_attribute_Group_Curve_A                      =  9, // V
       
    71 	isakmp_attribute_Group_Curve_B                      = 10, // V
       
    72 	isakmp_attribute_Life_Type                          = 11, // B
       
    73 	isakmp_attribute_Life_Duration                      = 12, // V
       
    74 	isakmp_attribute_PRF                                = 13, // B
       
    75 	isakmp_attribute_Key_Length                         = 14, // B
       
    76 	isakmp_attribute_Field_Size                         = 15, // B
       
    77 	isakmp_attribute_Group_Order                        = 16, // V
       
    78 };
       
    79 
       
    80 const u32_t ISAKMP_COOKIE_LENGTH = 8u;
       
    81 
       
    82 
       
    83 // 
       
    84 class isakmp_generic_payload_header_c
       
    85 {
       
    86 private:
       
    87 	//--------------------------------------------------
       
    88 
       
    89 	u8_t m_next_payload;
       
    90 	u8_t m_reserved;
       
    91 	u16_t m_length;
       
    92 	// m_length-sizeof(isakmp_generic_payload_header_c) data octets follows isakmp_generic_payload_header_c.
       
    93 
       
    94 	//--------------------------------------------------
       
    95 protected:
       
    96 	//--------------------------------------------------
       
    97 
       
    98 	//--------------------------------------------------
       
    99 public:
       
   100 	//--------------------------------------------------
       
   101 
       
   102 	// 
       
   103 	~isakmp_generic_payload_header_c()
       
   104 	{
       
   105 	}
       
   106 
       
   107 	// 
       
   108 	isakmp_generic_payload_header_c()
       
   109 	{
       
   110 	}
       
   111 
       
   112 	const isakmp_payload_type_e get_next_payload() const
       
   113 	{
       
   114 		return (isakmp_payload_type_e)m_next_payload;
       
   115 	}
       
   116 
       
   117 	const u8_t get_reserved() const
       
   118 	{
       
   119 		return m_reserved;
       
   120 	}
       
   121 
       
   122 	u16_t get_data_length() const
       
   123 	{
       
   124 		if (eap_ntohs(m_length) > static_cast<u16_t>(sizeof(isakmp_generic_payload_header_c))
       
   125 			return eap_ntohs(m_length)-(u16_t)sizeof(isakmp_generic_payload_header_c);
       
   126 		else
       
   127 			return 0;
       
   128 	}
       
   129 
       
   130 	u16_t get_header_length() const
       
   131 	{
       
   132 		return sizeof(isakmp_generic_payload_header_c);
       
   133 	}
       
   134 
       
   135 	u8_t * get_data(abs_eap_am_tools_c * const m_am_tools, const u32_t contignuous_bytes) const
       
   136 	{
       
   137 		if (get_data_length() >= contignuous_bytes
       
   138 			&& contignuous_bytes > 0)
       
   139 		{
       
   140 			return reinterpret_cast<u8_t *>(this+1); // Data begins after the header.
       
   141 		}
       
   142 		else
       
   143 		{
       
   144 			EAP_ASSERT_ALWAYS(get_data_length() > 0u);
       
   145 		}
       
   146 		return 0;
       
   147 	}
       
   148 
       
   149 	isakmp_generic_payload_header_c * const get_next_header() const
       
   150 	{
       
   151 		return reinterpret_cast<isakmp_generic_payload_header_c *>((reinterpret_cast<u8_t *>(this+1))+get_data_length());
       
   152 	}
       
   153 
       
   154 	void set_next_payload(abs_eap_am_tools_c * const m_am_tools, const isakmp_payload_type_e p_next_payload)
       
   155 	{
       
   156 		EAP_ASSERT_ALWAYS(p_next_payload == (isakmp_payload_type_e)((u8_t)p_next_payload));
       
   157 		m_next_payload = static_cast<u8_t>(p_next_payload;
       
   158 	}
       
   159 
       
   160 	void set_reserved(const u8_t p_reserved)
       
   161 	{
       
   162 		m_reserved = p_reserved;
       
   163 	}
       
   164 
       
   165 	void set_data_length(const u16_t p_data_length)
       
   166 	{
       
   167 		m_length = eap_htons(p_data_length+sizeof(isakmp_generic_payload_header_c));
       
   168 	}
       
   169 
       
   170 	void reset_header(abs_eap_am_tools_c * const m_am_tools, const u16_t buffer_length)
       
   171 	{
       
   172 		set_next_payload(m_am_tools, isakmp_payload_NONE);
       
   173 		set_reserved(0u);
       
   174 		set_data_length(buffer_length);
       
   175 	}
       
   176 
       
   177 	// 
       
   178 	//--------------------------------------------------
       
   179 }; // class isakmp_generic_payload_header_c
       
   180 
       
   181 
       
   182 
       
   183 // 
       
   184 class isakmp_attribute_header_c
       
   185 {
       
   186 private:
       
   187 	//--------------------------------------------------
       
   188 
       
   189 	u16_t m_type;
       
   190 	u16_t m_value_or_length;
       
   191 
       
   192 	//--------------------------------------------------
       
   193 protected:
       
   194 	//--------------------------------------------------
       
   195 
       
   196 	//--------------------------------------------------
       
   197 public:
       
   198 	//--------------------------------------------------
       
   199 
       
   200 	// 
       
   201 	~isakmp_attribute_header_c()
       
   202 	{
       
   203 	}
       
   204 
       
   205 	// 
       
   206 	isakmp_attribute_header_c()
       
   207 	{
       
   208 	}
       
   209 
       
   210 	const isakmp_attribute_type_e get_type() const
       
   211 	{
       
   212 		return (isakmp_attribute_type_e)(m_type && 0x7FFF);
       
   213 	}
       
   214 
       
   215 	const u16_t get_value() const
       
   216 	{
       
   217 		return m_value_or_length;
       
   218 	}
       
   219 
       
   220 
       
   221 	bool is_type_fixed() const
       
   222 	{
       
   223 		if ((m_type & 0x8000) != 0)
       
   224 		{
       
   225 			return true;
       
   226 		}
       
   227 		return false;
       
   228 	}
       
   229 
       
   230 	void set_type_fixed(const isakmp_attribute_type_e p_type)
       
   231 	{
       
   232 		m_type = p_type | 0x8000;
       
   233 	}
       
   234 
       
   235 	void set_type_variable(const isakmp_attribute_type_e p_type)
       
   236 	{
       
   237 		m_type = p_type | 0x0000;
       
   238 	}
       
   239 
       
   240 	void set_value_or_length(const u16_t p_value)
       
   241 	{
       
   242 		m_value_or_length = p_value;
       
   243 	}
       
   244 
       
   245 	// 
       
   246 	//--------------------------------------------------
       
   247 }; // class isakmp_attribute_header_c
       
   248 
       
   249 
       
   250 const u8_t isakmp_flag_server = (1 << 3);
       
   251 const u8_t isakmp_mask_server = (~(1 << 3));
       
   252 
       
   253 
       
   254 // 
       
   255 class isakmp_header_c
       
   256 {
       
   257 private:
       
   258 	//--------------------------------------------------
       
   259 
       
   260 	sae_cookie_c m_source_cookie; // m_initiator.
       
   261 	sae_cookie_c m_destination_cookie; // m_responder.
       
   262 	u8_t m_next_payload;
       
   263 	u8_t m_version;
       
   264 	u8_t m_exchange_type;
       
   265 	u8_t m_flags;
       
   266 	u32_t m_sequence_number; //m_message_id.
       
   267 	u32_t m_length;
       
   268 	// m_length-sizeof(isakmp_header_c) data octets follows isakmp_header_c.
       
   269 
       
   270 	//--------------------------------------------------
       
   271 protected:
       
   272 	//--------------------------------------------------
       
   273 
       
   274 	//--------------------------------------------------
       
   275 public:
       
   276 	//--------------------------------------------------
       
   277 
       
   278 	// 
       
   279 	~isakmp_header_c()
       
   280 	{
       
   281 	}
       
   282 
       
   283 	// 
       
   284 	isakmp_header_c()
       
   285 	{
       
   286 	}
       
   287 
       
   288 	const isakmp_payload_type_e get_next_payload() const
       
   289 	{
       
   290 		return (isakmp_payload_type_e)m_next_payload;
       
   291 	}
       
   292 
       
   293 	const isakmp_version_value_e get_version() const
       
   294 	{
       
   295 		return (const isakmp_version_value_e)m_version;
       
   296 	}
       
   297 
       
   298 	const u8_t get_exchange_type() const
       
   299 	{
       
   300 		return m_exchange_type;
       
   301 	}
       
   302 
       
   303 	const u8_t get_flags() const
       
   304 	{
       
   305 		return m_flags;
       
   306 	}
       
   307 
       
   308 	const u32_t get_sequence_number()
       
   309 	{
       
   310 		return eap_ntohl(m_sequence_number);
       
   311 	}
       
   312 
       
   313 	u32_t get_data_length() const
       
   314 	{
       
   315 		if (eap_ntohl(m_length) > (u32_t)sizeof(isakmp_header_c))
       
   316 			return eap_ntohl(m_length)-(u32_t)sizeof(isakmp_header_c);
       
   317 		else
       
   318 			return 0;
       
   319 	}
       
   320 
       
   321 	u32_t get_header_length() const
       
   322 	{
       
   323 		return sizeof(isakmp_header_c);
       
   324 	}
       
   325 
       
   326 	sae_cookie_c *get_source_cookie()
       
   327 	{
       
   328 		return &m_source_cookie;
       
   329 	}
       
   330 
       
   331 	sae_cookie_c *get_destination_cookie()
       
   332 	{
       
   333 		return &m_destination_cookie;
       
   334 	}
       
   335 
       
   336 	u8_t * get_data_offset(abs_eap_am_tools_c * const m_am_tools, const u32_t offset, const u32_t contignuous_bytes) const
       
   337 	{
       
   338 		u32_t data_length = get_data_length();
       
   339 		if (data_length >= offset+contignuous_bytes
       
   340 			&& contignuous_bytes > 0u)
       
   341 		{
       
   342 			return ((reinterpret_cast<u8_t *>(this+1))+offset); // Data begins after the header.
       
   343 		}
       
   344 		else
       
   345 		{
       
   346 			EAP_ASSERT_ALWAYS(get_data_length() > 0u);
       
   347 		}
       
   348 		return 0;
       
   349 	}
       
   350 
       
   351 
       
   352 	u8_t * get_data(abs_eap_am_tools_c * const m_am_tools, const u32_t contignuous_bytes) const
       
   353 	{
       
   354 		return get_data_offset(m_am_tools, 0u, contignuous_bytes);
       
   355 	}
       
   356 
       
   357 
       
   358 	void set_next_payload(const isakmp_payload_type_e p_next_payload)
       
   359 	{
       
   360 		m_next_payload = static_cast<u8_t>(p_next_payload;
       
   361 	}
       
   362 
       
   363 	void set_version(const isakmp_version_value_e p_version)
       
   364 	{
       
   365 		m_version = static_cast<u8_t>(p_version;
       
   366 	}
       
   367 
       
   368 	void set_exchange_type(const u8_t p_exchange_type)
       
   369 	{
       
   370 		m_exchange_type = p_exchange_type;
       
   371 	}
       
   372 
       
   373 	void set_flags(const u8_t p_flags)
       
   374 	{
       
   375 		m_flags = p_flags;
       
   376 	}
       
   377 
       
   378 	void set_sequence_number(const u32_t sequence_number)
       
   379 	{
       
   380 		m_sequence_number = eap_htonl(sequence_number);
       
   381 	}
       
   382 
       
   383 	void set_data_length(const u32_t p_data_length)
       
   384 	{
       
   385 		m_length = eap_htonl(p_data_length+sizeof(isakmp_header_c));
       
   386 	}
       
   387 
       
   388 	void reset_header(const u32_t buffer_length,
       
   389 		const bool is_client_when_true)
       
   390 	{
       
   391 		set_next_payload(isakmp_payload_NONE);
       
   392 		set_version(isakmp_version_current);
       
   393 		set_exchange_type(isakmp_exchange_type);
       
   394 		if (is_client_when_true == true)
       
   395 		{
       
   396 			set_flags(0u);
       
   397 		}
       
   398 		else
       
   399 		{
       
   400 			set_flags(isakmp_flag_server);
       
   401 		}
       
   402 		set_sequence_number(0u);
       
   403 		set_data_length(buffer_length-sizeof(isakmp_header_c));
       
   404 		get_source_cookie()->reset_cookie();
       
   405 		get_destination_cookie()->reset_cookie();
       
   406 	}
       
   407 
       
   408 	eap_status_e check_header(const bool is_client_when_true)
       
   409 	{
       
   410 		if (get_version() != isakmp_version_current)
       
   411 		{
       
   412 			return eap_status_wrong_isakmp_header_version;
       
   413 		}
       
   414 		else if (get_exchange_type() != isakmp_exchange_type)
       
   415 		{
       
   416 			return eap_status_wrong_isakmp_exchange_type;
       
   417 		}
       
   418 		else if (is_client_when_true == false
       
   419 			&& get_flags() != 0u)
       
   420 		{
       
   421 			return eap_status_wrong_isakmp_flags;
       
   422 		}
       
   423 		else if (is_client_when_true == true
       
   424 			&& get_flags() != isakmp_flag_server)
       
   425 		{
       
   426 			return eap_status_wrong_isakmp_flags;
       
   427 		}
       
   428 		return eap_status_ok;
       
   429 	}
       
   430 
       
   431 	// 
       
   432 	//--------------------------------------------------
       
   433 }; // class isakmp_header_c
       
   434 
       
   435 
       
   436 #endif //#if !defined(_ISAKMP_HEADER_H_)
       
   437 
       
   438 //--------------------------------------------------
       
   439 
       
   440 
       
   441 
       
   442 // End.