ike_eap.c
Go to the documentation of this file.
1 /**
2  * @file ike_eap.c
3  * @brief EAP authentication
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2022-2026 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneIPSEC Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.6.8
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL IKE_TRACE_LEVEL
33 
34 //Dependencies
35 #include "ike/ike.h"
36 #include "ike/ike_eap.h"
37 #include "ike/ike_eap_md5.h"
38 #include "ike/ike_eap_mschapv2.h"
39 #include "debug.h"
40 
41 //Check IKEv2 library configuration
42 #if (IKE_EAP_AUTH_SUPPORT == ENABLED)
43 
44 
45 /**
46  * @brief Process EAP packet
47  * @param[in] sa Pointer to the IKE SA
48  * @param[in] packet Pointer to the EAP packet
49  * @param[in] length Length of the EAP packet, in bytes
50  * @return Error code
51  **/
52 
54  size_t length)
55 {
56  error_t error;
57 
58  //Malformed EAP packet?
59  if(length < sizeof(IkeEapPacket))
60  {
61  return ERROR_INVALID_LENGTH;
62  }
63 
64  //The Length field is two octets and indicates the length, in octets, of the
65  //EAP packet including the Code, Identifier, Length, and Data fields
66  if(ntohs(packet->length) < sizeof(IkeEapPacket))
67  {
68  return ERROR_INVALID_LENGTH;
69  }
70 
71  //A message with the Length field set to a value larger than the number of
72  //received octets must be silently discarded (refer to RFC 3748, section 4.0)
73  if(ntohs(packet->length) > length)
74  {
75  return ERROR_INVALID_LENGTH;
76  }
77 
78  //Octets outside the range of the Length field should be treated as data
79  //link layer padding and must be ignored upon reception
80  length = ntohs(packet->length);
81 
82  //Based on the Code field, the EAP layer demultiplexes incoming EAP packets
83  //to the EAP peer and authenticator layers
84  if(packet->code == IKE_EAP_CODE_REQUEST)
85  {
86  //Process EAP request
87  error = ikeProcessEapRequest(sa, (IkeEapRequest *) packet, length);
88  }
89  else if(packet->code == IKE_EAP_CODE_SUCCESS)
90  {
91  //Process EAP success packet
92  error = ikeProcessEapSuccess(sa, packet, length);
93  }
94  else if(packet->code == IKE_EAP_CODE_FAILURE)
95  {
96  //Process EAP failure packet
97  error = ikeProcessEapFailure(sa, packet, length);
98  }
99  else
100  {
101  //Unknown EAP packet
102  error = ERROR_UNKNOWN_TYPE;
103  }
104 
105  //Return status code
106  return error;
107 }
108 
109 
110 /**
111  * @brief Process EAP request
112  * @param[in] sa Pointer to the IKE SA
113  * @param[in] packet Pointer to the EAP request
114  * @param[in] length Length of the EAP request, in bytes
115  * @return Error code
116  **/
117 
119  size_t length)
120 {
121  error_t error;
122 
123  //Malformed EAP request?
124  if(length < sizeof(IkeEapRequest))
125  {
126  return ERROR_INVALID_LENGTH;
127  }
128 
129  //Initialize status code
130  error = NO_ERROR;
131 
132  //Since in IKE, EAP runs over a reliable protocol, the Identifier serves no
133  //function here. In a response message, this octet must be set to match the
134  //identifier in the corresponding request (refer to RFC 7296, section 3.16)
135  sa->eapRequestId = request->identifier;
136 
137  //Check method type
138  if(request->type == IKE_EAP_METHOD_TYPE_IDENTITY)
139  {
140  //Process Identity request
141  error = ikeProcessEapIdentityRequest(sa, request, length);
142  }
143 #if (IKE_EAP_MD5_SUPPORT == ENABLED)
144  else if(request->type == IKE_EAP_METHOD_TYPE_MD5_CHALLENGE)
145  {
146  //Process EAP-MD5 request
147  error = ikeProcessEapMd5Request(sa, (IkeEapMd5Packet *) request, length);
148  }
149 #endif
150 #if (IKE_EAP_MSCHAPV2_SUPPORT == ENABLED)
151  else if(request->type == IKE_EAP_METHOD_TYPE_MSCHAPV2)
152  {
153  //Process EAP MS-CHAP-V2 request
154  error = ikeProcessEapMschapv2Request(sa, (IkeEapMschapv2Packet *) request,
155  length);
156  }
157 #endif
158  else
159  {
160  //The method type is unacceptable
161  sa->eapState = IKE_EAP_STATE_NAK;
162  }
163 
164  //Return status code
165  return error;
166 }
167 
168 
169 /**
170  * @brief Process EAP Identity request
171  * @param[in] sa Pointer to the IKE SA
172  * @param[in] packet Pointer to the EAP Identity request
173  * @param[in] length Length of the EAP Identity request, in bytes
174  * @return Error code
175  **/
176 
178  const IkeEapRequest *request, size_t length)
179 {
180  //Check current state
181  if(sa->eapState != IKE_EAP_STATE_INIT)
182  {
183  return ERROR_WRONG_STATE;
184  }
185 
186  //Since IKE passes an indication of initiator identity in the first message
187  //in the IKE_AUTH exchange, the responder should not send EAP Identity
188  //requests. The initiator may, however, respond to such requests if it
189  //receives them (refer to RFC 7296, section 3.16)
190  sa->eapState = IKE_EAP_STATE_IDENTITY;
191 
192  //Sucessful processing
193  return NO_ERROR;
194 }
195 
196 
197 /**
198  * @brief Process EAP success packet
199  * @param[in] sa Pointer to the IKE SA
200  * @param[in] packet Pointer to the EAP success packet
201  * @param[in] length Length of the EAP success packet, in bytes
202  * @return Error code
203  **/
204 
206  size_t length)
207 {
208  //Check current state
209  if(sa->eapState != IKE_EAP_STATE_MD5_CHALLENGE &&
210  sa->eapState != IKE_EAP_STATE_MSCHAPV2_SUCCESS)
211  {
212  return ERROR_WRONG_STATE;
213  }
214 
215  //The Success packet is sent by the authenticator to the peer after
216  //completion of an EAP authentication method to indicate that the peer has
217  //authenticated successfully to the authenticator (refer to RFC 3748,
218  //section 4.2)
219  sa->eapState = IKE_EAP_STATE_SUCCESS;
220 
221  //Sucessful processing
222  return NO_ERROR;
223 }
224 
225 
226 /**
227  * @brief Process EAP failure packet
228  * @param[in] sa Pointer to the IKE SA
229  * @param[in] packet Pointer to the EAP failure packet
230  * @param[in] length Length of the EAP failure packet, in bytes
231  * @return Error code
232  **/
233 
235  size_t length)
236 {
237  //The authenticator cannot authenticate the peer
238  sa->eapState = IKE_EAP_STATE_FAILURE;
239 
240  //The responder may at any time terminate the IKE exchange by sending an EAP
241  //payload containing the Failure message (refer to RFC 7296, section 2.16)
243 }
244 
245 
246 /**
247  * @brief Format EAP packet
248  * @param[in] sa Pointer to the IKE SA
249  * @param[out] p Buffer where to format the EAP packet
250  * @param[out] written Length of the resulting EAP packet
251  * @return Error code
252  **/
253 
254 error_t ikeFormatEapPacket(IkeSaEntry *sa, uint8_t *p, size_t *written)
255 {
256  error_t error;
257 
258  //Check current state
259  if(sa->eapState == IKE_EAP_STATE_IDENTITY)
260  {
261  //Format Identity response
262  error = ikeFormatEapIdentityResponse(sa, p, written);
263  }
264  else if(sa->eapState == IKE_EAP_STATE_NAK)
265  {
266  //Format Nak response
267  error = ikeFormatEapNakResponse(sa, p, written);
268  }
269 #if (IKE_EAP_MD5_SUPPORT == ENABLED)
270  else if(sa->eapState == IKE_EAP_STATE_MD5_CHALLENGE)
271  {
272  //Format EAP-MD5 response
273  error = ikeFormatEapMd5Response(sa, p, written);
274  }
275 #endif
276 #if (IKE_EAP_MSCHAPV2_SUPPORT == ENABLED)
277  else if(sa->eapState == IKE_EAP_STATE_MSCHAPV2_CHALLENGE)
278  {
279  //Format EAP MS-CHAP-V2 Response packet
280  error = ikeFormatEapMschapv2Response(sa, p, written);
281  }
282  else if(sa->eapState == IKE_EAP_STATE_MSCHAPV2_SUCCESS)
283  {
284  //Format EAP MS-CHAP-V2 Success Response packet
285  error = ikeFormatEapMschapv2SuccessResponse(sa, p, written);
286  }
287  else if(sa->eapState == IKE_EAP_STATE_MSCHAPV2_FAILURE)
288  {
289  //Format EAP MS-CHAP-V2 Failure Response packet
290  error = ikeFormatEapMschapv2FailureResponse(sa, p, written);
291  }
292 #endif
293  else
294  {
295  //Report an error
296  error = ERROR_WRONG_STATE;
297  }
298 
299  //Return status code
300  return error;
301 }
302 
303 
304 /**
305  * @brief Format EAP Identity response
306  * @param[in] sa Pointer to the IKE SA
307  * @param[out] p Buffer where to format the EAP Identity response
308  * @param[out] written Length of the resulting EAP Identity response
309  * @return Error code
310  **/
311 
313  size_t *written)
314 {
315  size_t n;
316  IkeContext *context;
317  IkeEapResponse *response;
318 
319  //Point to the IKE context
320  context = sa->context;
321 
322  //Point to the buffer where to format the EAP packet
323  response = (IkeEapResponse *) p;
324 
325  //A response of type 1 (Identity) should be sent in response to a request
326  //with a type of 1 (Identity)
327  response->code = IKE_EAP_CODE_RESPONSE;
328  response->identifier = sa->eapRequestId;
329  response->type = IKE_EAP_METHOD_TYPE_IDENTITY;
330 
331  //Determine the length of the user name
332  n = osStrlen(context->username);
333  //Copy user name
334  osMemcpy(response->data, context->username, n);
335 
336  //Total length of the EAP packet
337  *written = sizeof(IkeEapResponse) + n;
338  //Convert the length field to network byte order
339  response->length = htons(*written);
340 
341  //Sucessful processing
342  return NO_ERROR;
343 }
344 
345 
346 /**
347  * @brief Format EAP Nak response
348  * @param[in] sa Pointer to the IKE SA
349  * @param[out] p Buffer where to format the EAP Nak response
350  * @param[out] written Length of the resulting EAP Nak response
351  * @return Error code
352  **/
353 
354 error_t ikeFormatEapNakResponse(IkeSaEntry *sa, uint8_t *p, size_t *written)
355 {
356  size_t n;
357  IkeEapResponse *response;
358 
359  //Point to the buffer where to format the EAP packet
360  response = (IkeEapResponse *) p;
361 
362  //The legacy Nak Type is valid only in response messages. It is sent in
363  //reply to a request where the desired authentication type is unacceptable
364  //(refer to RFC 3748, section 5.3.1)
365  response->code = IKE_EAP_CODE_RESPONSE;
366  response->identifier = sa->eapRequestId;
367  response->type = IKE_EAP_METHOD_TYPE_NAK;
368 
369  //The response contains one or more authentication types desired by the peer
370  n = 0;
371 
372 #if (IKE_EAP_MD5_SUPPORT == ENABLED)
373  //MD5-Challenge authentication method
374  response->data[n++] = IKE_EAP_METHOD_TYPE_MD5_CHALLENGE;
375 #endif
376 
377 #if (IKE_EAP_MSCHAPV2_SUPPORT == ENABLED)
378  //MS-CHAP-V2 authentication method
379  response->data[n++] = IKE_EAP_METHOD_TYPE_MSCHAPV2;
380 #endif
381 
382  //Type zero is used to indicate that the sender has no viable alternatives,
383  //and therefore the authenticator should not send another request after
384  //receiving a Nak Response containing a zero value
385  if(n == 0)
386  {
387  response->data[n++] = IKE_EAP_METHOD_TYPE_NONE;
388  }
389 
390  //Total length of the EAP packet
391  *written = sizeof(IkeEapResponse) + n;
392  //Convert the length field to network byte order
393  response->length = htons(*written);
394 
395  //Sucessful processing
396  return NO_ERROR;
397 }
398 
399 #endif
#define htons(value)
Definition: cpu_endian.h:413
error_t ikeProcessEapMschapv2Request(IkeSaEntry *sa, const IkeEapMschapv2Packet *request, size_t length)
Process EAP MS-CHAP-V2 request.
IkeEapPacket
Definition: ike_eap.h:95
error_t ikeProcessEapMd5Request(IkeSaEntry *sa, const IkeEapMd5Packet *request, size_t length)
Process EAP-MD5 request.
Definition: ike_eap_md5.c:52
@ IKE_EAP_STATE_MSCHAPV2_CHALLENGE
Definition: ike.h:1496
uint8_t p
Definition: ndp.h:300
error_t ikeFormatEapMd5Response(IkeSaEntry *sa, uint8_t *p, size_t *written)
Format EAP-MD5 response.
Definition: ike_eap_md5.c:114
@ IKE_EAP_CODE_RESPONSE
Response.
Definition: ike_eap.h:50
@ IKE_EAP_CODE_FAILURE
Failure.
Definition: ike_eap.h:52
error_t ikeFormatEapIdentityResponse(IkeSaEntry *sa, uint8_t *p, size_t *written)
Format EAP Identity response.
Definition: ike_eap.c:312
error_t ikeFormatEapNakResponse(IkeSaEntry *sa, uint8_t *p, size_t *written)
Format EAP Nak response.
Definition: ike_eap.c:354
EAP MS-CHAP-V2 authentication method.
error_t ikeProcessEapPacket(IkeSaEntry *sa, const IkeEapPacket *packet, size_t length)
Process EAP packet.
Definition: ike_eap.c:53
#define osStrlen(s)
Definition: os_port.h:171
@ ERROR_WRONG_STATE
Definition: error.h:211
@ IKE_EAP_STATE_MSCHAPV2_FAILURE
Definition: ike.h:1498
#define IkeContext
Definition: ike.h:883
IkeEapRequest
Definition: ike_eap.h:109
error_t ikeProcessEapRequest(IkeSaEntry *sa, const IkeEapRequest *request, size_t length)
Process EAP request.
Definition: ike_eap.c:118
#define osMemcpy(dest, src, length)
Definition: os_port.h:147
EAP authentication.
error_t
Error codes.
Definition: error.h:43
EAP-MD5 authentication method.
@ IKE_EAP_STATE_MD5_CHALLENGE
Definition: ike.h:1495
@ IKE_EAP_METHOD_TYPE_NONE
None.
Definition: ike_eap.h:62
error_t ikeFormatEapMschapv2FailureResponse(IkeSaEntry *sa, uint8_t *p, size_t *written)
Format EAP MS-CHAP-V2 Failure Response packet.
@ ERROR_INVALID_LENGTH
Definition: error.h:111
@ ERROR_UNKNOWN_TYPE
Definition: error.h:297
uint8_t length
Definition: tcp.h:375
@ IKE_EAP_STATE_FAILURE
Definition: ike.h:1500
@ IKE_EAP_CODE_REQUEST
Request.
Definition: ike_eap.h:49
error_t ikeProcessEapSuccess(IkeSaEntry *sa, const IkeEapPacket *packet, size_t length)
Process EAP success packet.
Definition: ike_eap.c:205
@ IKE_EAP_METHOD_TYPE_MSCHAPV2
MS-CHAP-V2.
Definition: ike_eap.h:72
IKEv2 (Internet Key Exchange Protocol)
IkeEapMd5Packet
Definition: ike_eap_md5.h:62
error_t ikeProcessEapIdentityRequest(IkeSaEntry *sa, const IkeEapRequest *request, size_t length)
Process EAP Identity request.
Definition: ike_eap.c:177
#define ntohs(value)
Definition: cpu_endian.h:421
@ IKE_EAP_STATE_NAK
Definition: ike.h:1494
#define IkeSaEntry
Definition: ike.h:887
uint8_t n
@ ERROR_AUTHENTICATION_FAILED
Definition: error.h:69
@ IKE_EAP_METHOD_TYPE_MD5_CHALLENGE
MD5-Challenge.
Definition: ike_eap.h:66
IkeEapMschapv2Packet
error_t ikeFormatEapMschapv2Response(IkeSaEntry *sa, uint8_t *p, size_t *written)
Format EAP MS-CHAP-V2 Response packet.
@ IKE_EAP_STATE_SUCCESS
Definition: ike.h:1499
error_t ikeFormatEapMschapv2SuccessResponse(IkeSaEntry *sa, uint8_t *p, size_t *written)
Format EAP MS-CHAP-V2 Success Response packet.
@ IKE_EAP_METHOD_TYPE_NAK
Legacy Nak.
Definition: ike_eap.h:65
@ IKE_EAP_CODE_SUCCESS
Success.
Definition: ike_eap.h:51
error_t ikeFormatEapPacket(IkeSaEntry *sa, uint8_t *p, size_t *written)
Format EAP packet.
Definition: ike_eap.c:254
@ IKE_EAP_STATE_IDENTITY
Definition: ike.h:1493
IkeEapResponse
Definition: ike_eap.h:123
@ IKE_EAP_METHOD_TYPE_IDENTITY
Identity.
Definition: ike_eap.h:63
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
error_t ikeProcessEapFailure(IkeSaEntry *sa, const IkeEapPacket *packet, size_t length)
Process EAP failure packet.
Definition: ike_eap.c:234
@ IKE_EAP_STATE_MSCHAPV2_SUCCESS
Definition: ike.h:1497
@ IKE_EAP_STATE_INIT
Definition: ike.h:1492