ike_eap_md5.c
Go to the documentation of this file.
1 /**
2  * @file ike_eap_md5.c
3  * @brief EAP-MD5 authentication method
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 "debug.h"
39 
40 //Check IKEv2 library configuration
41 #if (IKE_EAP_AUTH_SUPPORT == ENABLED && IKE_EAP_MD5_SUPPORT == ENABLED)
42 
43 
44 /**
45  * @brief Process EAP-MD5 request
46  * @param[in] sa Pointer to the IKE SA
47  * @param[in] packet Pointer to the EAP-MD5 request
48  * @param[in] length Length of the EAP-MD5 request, in bytes
49  * @return Error code
50  **/
51 
53  size_t length)
54 {
55  IkeContext *context;
56  Md5Context md5Context;
57 
58  //Point to the IKE context
59  context = sa->context;
60 
61  //Check current state
62  if(sa->eapState != IKE_EAP_STATE_INIT &&
63  sa->eapState != IKE_EAP_STATE_IDENTITY)
64  {
65  return ERROR_WRONG_STATE;
66  }
67 
68  //Malformed EAP-MD5 packet?
69  if(length < sizeof(IkeEapMd5Packet))
70  {
71  return ERROR_INVALID_LENGTH;
72  }
73 
74  //The Value field is one or more octets (refer to RFC 1994, section 4.1)
75  if(request->valueSize == 0)
76  {
77  return ERROR_INVALID_LENGTH;
78  }
79 
80  //Check the length of the challenge value
81  if(length < (sizeof(IkeEapMd5Packet) + request->valueSize))
82  {
83  return ERROR_INVALID_LENGTH;
84  }
85 
86  //The MD5 challenge method is analogous to the PPP CHAP protocol (with MD5
87  //as the specified algorithm)
88  md5Init(&md5Context);
89  md5Update(&md5Context, &request->identifier, sizeof(uint8_t));
90  md5Update(&md5Context, context->password, osStrlen(context->password));
91  md5Update(&md5Context, request->value, request->valueSize);
92  md5Final(&md5Context, sa->digest);
93  md5Deinit(&md5Context);
94 
95  //EAP-MD5 does not generate a master session key
96  sa->mskLen = 0;
97 
98  //Send an EAP-MD5 response
99  sa->eapState = IKE_EAP_STATE_MD5_CHALLENGE;
100 
101  //Sucessful processing
102  return NO_ERROR;
103 }
104 
105 
106 /**
107  * @brief Format EAP-MD5 response
108  * @param[in] sa Pointer to the IKE SA
109  * @param[out] p Buffer where to format the EAP-MD5 packet
110  * @param[out] written Length of the resulting EAP-MD5 packet
111  * @return Error code
112  **/
113 
114 error_t ikeFormatEapMd5Response(IkeSaEntry *sa, uint8_t *p, size_t *written)
115 {
116  size_t n;
117  IkeContext *context;
118  IkeEapMd5Packet *response;
119 
120  //Point to the IKE context
121  context = sa->context;
122 
123  //Point to the buffer where to format the EAP packet
124  response = (IkeEapMd5Packet *) p;
125 
126  //Format EAP packet
127  response->code = IKE_EAP_CODE_RESPONSE;
128  response->identifier = sa->eapRequestId;
129  response->type = IKE_EAP_METHOD_TYPE_MD5_CHALLENGE;
130  response->valueSize = MD5_DIGEST_SIZE;
131 
132  //The length of the response value depends upon the hash algorithm used
133  osMemcpy(response->value, sa->digest, MD5_DIGEST_SIZE);
134 
135  //Determine the length of the user name
136  n = osStrlen(context->username);
137 
138  //The Name field is one or more octets representing the identification of
139  //the system transmitting the packet (refer to RFC 1994, section 4.1)
140  osMemcpy(response->value + MD5_DIGEST_SIZE, context->username, n);
141 
142  //Total length of the EAP packet
143  *written = sizeof(IkeEapMd5Packet) + MD5_DIGEST_SIZE + n;
144  //Convert the length field to network byte order
145  response->length = htons(*written);
146 
147  //Sucessful processing
148  return NO_ERROR;
149 }
150 
151 #endif
#define htons(value)
Definition: cpu_endian.h:413
error_t ikeProcessEapMd5Request(IkeSaEntry *sa, const IkeEapMd5Packet *request, size_t length)
Process EAP-MD5 request.
Definition: ike_eap_md5.c:52
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
void md5Final(Md5Context *context, uint8_t *digest)
Finish the MD5 message digest.
#define osStrlen(s)
Definition: os_port.h:171
@ ERROR_WRONG_STATE
Definition: error.h:211
void md5Init(Md5Context *context)
Initialize MD5 message digest context.
#define IkeContext
Definition: ike.h:883
#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
@ ERROR_INVALID_LENGTH
Definition: error.h:111
MD5 algorithm context.
Definition: md5.h:62
uint8_t length
Definition: tcp.h:375
#define MD5_DIGEST_SIZE
Definition: md5.h:45
IKEv2 (Internet Key Exchange Protocol)
IkeEapMd5Packet
Definition: ike_eap_md5.h:62
#define IkeSaEntry
Definition: ike.h:887
uint8_t n
@ IKE_EAP_METHOD_TYPE_MD5_CHALLENGE
MD5-Challenge.
Definition: ike_eap.h:66
@ IKE_EAP_STATE_IDENTITY
Definition: ike.h:1493
void md5Deinit(Md5Context *context)
Release MD5 context.
Definition: md5.c:268
void md5Update(Md5Context *context, const void *data, size_t length)
Update the MD5 context with a portion of the message being hashed.
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
@ IKE_EAP_STATE_INIT
Definition: ike.h:1492