eap_md5.c
Go to the documentation of this file.
1 /**
2  * @file eap_md5.c
3  * @brief MD5-Challenge authentication method
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2022-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneEAP 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.4.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL EAP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "eap/eap.h"
36 #include "eap/eap_md5.h"
37 #include "eap/eap_debug.h"
38 #include "hash/md5.h"
39 #include "debug.h"
40 
41 //Check EAP library configuration
42 #if (EAP_MD5_SUPPORT == ENABLED)
43 
44 
45 /**
46  * @brief Check incoming MD5 challenge request
47  * @param[in] context Pointer to the 802.1X supplicant context
48  * @param[in] request Pointer to the received request
49  * @param[in] length Length of the request, in bytes
50  **/
51 
53  const EapMd5Packet *request, size_t length)
54 {
55  //Check the length of the EAP request
56  if(length < sizeof(EapMd5Packet))
57  return ERROR_INVALID_LENGTH;
58 
59  //Check the length of the challenge value
60  if(length < (sizeof(EapMd5Packet) + request->valueSize))
61  return ERROR_INVALID_LENGTH;
62 
63  //The request is valid
64  return NO_ERROR;
65 }
66 
67 
68 /**
69  * @brief Process incoming MD5 challenge request
70  * @param[in] context Pointer to the 802.1X supplicant context
71  * @param[in] request Pointer to the received request
72  * @param[in] length Length of the request, in bytes
73  **/
74 
76  const EapMd5Packet *request, size_t length)
77 {
78  Md5Context md5Context;
79 
80  //The MD5 challenge method is analogous to the PPP CHAP protocol (with MD5
81  //as the specified algorithm)
82  md5Init(&md5Context);
83  md5Update(&md5Context, &request->identifier, sizeof(uint8_t));
84  md5Update(&md5Context, context->password, osStrlen(context->password));
85  md5Update(&md5Context, request->value, request->valueSize);
86  md5Final(&md5Context, context->digest);
87 
88  //The method never continues at this point
89  context->methodState = EAP_METHOD_STATE_DONE;
90 
91  //we do not know what the server's decision is, but are willing to use the
92  //access if the server allows. In this case, set decision to COND_SUCC
93  context->decision = EAP_DECISION_COND_SUCC;
94 }
95 
96 
97 /**
98  * @brief Build MD5 challenge response
99  * @param[in] context Pointer to the 802.1X supplicant context
100  **/
101 
103 {
104  size_t n;
105  EapMd5Packet *response;
106 
107  //Point to the buffer where to format the EAP packet
108  response = (EapMd5Packet *) context->eapRespData;
109 
110  //Format EAP packet
111  response->code = EAP_CODE_RESPONSE;
112  response->identifier = context->reqId;
113  response->type = EAP_METHOD_TYPE_MD5_CHALLENGE;
114  response->valueSize = MD5_DIGEST_SIZE;
115 
116  //The length of the response value depends upon the hash algorithm used
117  osMemcpy(response->value, context->digest, MD5_DIGEST_SIZE);
118 
119  //Total length of the EAP packet
120  n = sizeof(EapMd5Packet) + MD5_DIGEST_SIZE;
121  //Convert the length field to network byte order
122  response->length = htons(n);
123 
124  //Debug message
125  TRACE_DEBUG("Sending EAP packet (%" PRIuSIZE " bytes)\r\n", n);
126  //Dump EAP header contents for debugging purpose
127  eapDumpHeader((EapPacket *) response);
128 
129  //Save the length of the EAP response
130  context->eapRespDataLen = n;
131 }
132 
133 #endif
#define htons(value)
Definition: cpu_endian.h:413
void eapDumpHeader(const EapPacket *header)
Dump EAP header for debugging purpose.
Definition: eap_debug.c:105
uint8_t digest[16]
Definition: md5.h:66
@ EAP_CODE_RESPONSE
Response.
Definition: eap.h:153
EapMd5Packet
Definition: eap_md5.h:63
#define SupplicantContext
Definition: supplicant.h:36
void md5Final(Md5Context *context, uint8_t *digest)
Finish the MD5 message digest.
#define osStrlen(s)
Definition: os_port.h:165
void md5Init(Md5Context *context)
Initialize MD5 message digest context.
#define osMemcpy(dest, src, length)
Definition: os_port.h:141
error_t
Error codes.
Definition: error.h:43
@ ERROR_INVALID_LENGTH
Definition: error.h:111
MD5 algorithm context.
Definition: md5.h:62
uint8_t length
Definition: tcp.h:368
#define MD5_DIGEST_SIZE
Definition: md5.h:45
void eapMd5ProcessRequest(SupplicantContext *context, const EapMd5Packet *request, size_t length)
Process incoming MD5 challenge request.
Definition: eap_md5.c:75
#define TRACE_DEBUG(...)
Definition: debug.h:107
Data logging functions for debugging purpose (EAP)
uint8_t n
MD5 (Message-Digest Algorithm)
EapPacket
Definition: eap.h:224
error_t eapMd5CheckRequest(SupplicantContext *context, const EapMd5Packet *request, size_t length)
Check incoming MD5 challenge request.
Definition: eap_md5.c:52
@ EAP_METHOD_STATE_DONE
Definition: eap_peer_fsm.h:80
@ EAP_DECISION_COND_SUCC
Definition: eap_peer_fsm.h:91
#define PRIuSIZE
EAP (Extensible Authentication Protocol)
MD5-Challenge authentication method.
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.
@ EAP_METHOD_TYPE_MD5_CHALLENGE
MD5-Challenge.
Definition: eap.h:169
void eapMd5BuildResponse(SupplicantContext *context)
Build MD5 challenge response.
Definition: eap_md5.c:102