|
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 136 |
|
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 #include "tls_record_header.h" |
|
29 |
|
30 /** @file */ |
|
31 |
|
32 |
|
33 /// Destructor does nothing. |
|
34 tls_record_header_c::~tls_record_header_c() |
|
35 { |
|
36 } |
|
37 |
|
38 /// Constructor initializes the class. |
|
39 tls_record_header_c::tls_record_header_c( |
|
40 abs_eap_am_tools_c * const tools, |
|
41 void * const header_buffer, |
|
42 const u32_t header_buffer_length) |
|
43 : eap_general_header_base_c(tools, header_buffer, header_buffer_length) |
|
44 , m_am_tools(tools) |
|
45 { |
|
46 } |
|
47 |
|
48 /// This function returns protocol of the TLS-record. |
|
49 tls_record_protocol_e tls_record_header_c::get_protocol() const |
|
50 { |
|
51 const u8_t * const protocol_data = get_header_offset(m_protocol_offset, sizeof(u8_t)); |
|
52 if (protocol_data != 0) |
|
53 { |
|
54 return static_cast<tls_record_protocol_e>(*protocol_data); |
|
55 } |
|
56 else |
|
57 { |
|
58 return tls_record_protocol_none; |
|
59 } |
|
60 } |
|
61 |
|
62 /// This function returns version of the TLS-record. |
|
63 tls_version_e tls_record_header_c::get_version() const |
|
64 { |
|
65 const u8_t * const version_data = get_header_offset(m_version_offset, sizeof(u16_t)); |
|
66 if (version_data != 0) |
|
67 { |
|
68 return static_cast<tls_version_e>(eap_read_u16_t_network_order(version_data, sizeof(u16_t))); |
|
69 } |
|
70 else |
|
71 { |
|
72 return tls_version_illegal; |
|
73 } |
|
74 } |
|
75 |
|
76 /// This function returns data length of the TLS-record. |
|
77 /// The length (in bytes) of the following TLSPlaintext.fragment (TLS-record data). The length should not exceed 2^14. |
|
78 u16_t tls_record_header_c::get_data_length() const |
|
79 { |
|
80 const u8_t * const length_data = get_header_offset(m_length_offset, sizeof(u16_t)); |
|
81 if (length_data != 0) |
|
82 { |
|
83 return eap_read_u16_t_network_order(length_data, sizeof(u16_t)); |
|
84 } |
|
85 else |
|
86 { |
|
87 return 0ul; |
|
88 } |
|
89 } |
|
90 |
|
91 /// This function returns header length of the TLS-record. This includes only protocol, version and length fiels. |
|
92 u32_t tls_record_header_c::get_header_length() |
|
93 { |
|
94 return m_data_offset; |
|
95 } |
|
96 |
|
97 /// This function returns pointer to offset of the TLS-record data. |
|
98 /// @param offset is the offset of queried data in bytes. |
|
99 /// @param contignuous_bytes is the length of queried data in bytes. |
|
100 u8_t * tls_record_header_c::get_data_offset(const u32_t offset, const u32_t contignuous_bytes) const |
|
101 { |
|
102 u32_t data_length = get_data_length(); // Here is removed optional TLS message length. |
|
103 |
|
104 if (data_length >= offset+contignuous_bytes) |
|
105 { |
|
106 u8_t * const data_begin = get_header_offset(m_data_offset, sizeof(u8_t)); |
|
107 if (data_begin == 0) |
|
108 { |
|
109 return 0; |
|
110 } |
|
111 return &(data_begin[offset]); |
|
112 } |
|
113 else |
|
114 { |
|
115 return 0; |
|
116 } |
|
117 } |
|
118 |
|
119 |
|
120 /// This function returns pointer to begin of the TLS-record data. |
|
121 /// @param contignuous_bytes is the length of queried data in bytes. |
|
122 u8_t * tls_record_header_c::get_data(const u32_t contignuous_bytes) const |
|
123 { |
|
124 return get_data_offset(0u, contignuous_bytes); |
|
125 } |
|
126 |
|
127 |
|
128 /// This function returns debug strings of the TLS-protocol values. |
|
129 eap_const_string tls_record_header_c::get_tls_protocol_string(const tls_record_protocol_e protocol) |
|
130 { |
|
131 #if defined(USE_EAP_TRACE_STRINGS) |
|
132 EAP_IF_RETURN_STRING(protocol, tls_record_protocol_none) |
|
133 else EAP_IF_RETURN_STRING(protocol, tls_record_protocol_change_cipher_spec) |
|
134 else EAP_IF_RETURN_STRING(protocol, tls_record_protocol_alert) |
|
135 else EAP_IF_RETURN_STRING(protocol, tls_record_protocol_handshake) |
|
136 else EAP_IF_RETURN_STRING(protocol, tls_record_protocol_application_data) |
|
137 else |
|
138 #else |
|
139 EAP_UNREFERENCED_PARAMETER(protocol); |
|
140 #endif // #if defined(USE_EAP_TRACE_STRINGS) |
|
141 { |
|
142 return EAPL("Unknown TLS record protocol"); |
|
143 } |
|
144 } |
|
145 |
|
146 /// This function returns debug strings of the TLS-protocol values. |
|
147 eap_const_string tls_record_header_c::get_tls_protocol_string() const |
|
148 { |
|
149 const tls_record_protocol_e protocol = get_protocol(); |
|
150 return get_tls_protocol_string(protocol); |
|
151 } |
|
152 |
|
153 |
|
154 /// This function sets the protocol of TLS-record. |
|
155 void tls_record_header_c::set_protocol(tls_record_protocol_e protocol) |
|
156 { |
|
157 u8_t * const protocol_data = get_header_offset(m_protocol_offset, sizeof(u8_t)); |
|
158 EAP_ASSERT(protocol_data != 0); |
|
159 *protocol_data = static_cast<u8_t>(protocol); |
|
160 } |
|
161 |
|
162 /// This function sets the version of the TLS-record. |
|
163 void tls_record_header_c::set_version(tls_version_e version) |
|
164 { |
|
165 u8_t * const version_data = get_header_offset(m_version_offset, sizeof(u16_t)); |
|
166 EAP_ASSERT(version_data != 0); |
|
167 version_data[0] = static_cast<u8_t>((version & 0xff00) >> 8); |
|
168 version_data[1] = static_cast<u8_t>(version & 0x00ff); |
|
169 } |
|
170 |
|
171 /// This function sets the data length of the TLS-record. |
|
172 void tls_record_header_c::set_data_length(const u16_t p_length) |
|
173 { |
|
174 u8_t * const length_data = get_header_offset(m_length_offset, sizeof(u16_t)); |
|
175 EAP_ASSERT(length_data != 0); |
|
176 length_data[0] = static_cast<u8_t>((p_length & 0xff00) >> 8); |
|
177 length_data[1] = static_cast<u8_t>(p_length & 0x00ff); |
|
178 } |
|
179 |
|
180 /// This function resets the TLS-record header. |
|
181 void tls_record_header_c::reset_header( |
|
182 const u16_t buffer_length, |
|
183 const tls_version_e version) |
|
184 { |
|
185 set_protocol(tls_record_protocol_none); |
|
186 set_version(version); |
|
187 set_data_length(buffer_length); |
|
188 } |
|
189 |
|
190 /// This function checks the header is valid. |
|
191 eap_status_e tls_record_header_c::check_header() const |
|
192 { |
|
193 if (get_protocol() > tls_record_protocol_application_data |
|
194 || get_protocol() < tls_record_protocol_change_cipher_spec) |
|
195 { |
|
196 return EAP_STATUS_RETURN(m_am_tools, eap_status_header_corrupted); |
|
197 } |
|
198 else if (get_version() != tls_version_3_1) |
|
199 { |
|
200 return EAP_STATUS_RETURN(m_am_tools, eap_status_no_matching_protocol_version); |
|
201 } |
|
202 |
|
203 return EAP_STATUS_RETURN(m_am_tools, eap_status_ok); |
|
204 } |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 // End. |