eap_debug.c
Go to the documentation of this file.
1 /**
2  * @file eap_debug.c
3  * @brief Data logging functions for debugging purpose (EAP)
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_debug.h"
37 #include "debug.h"
38 
39 //Check EAP library configuration
40 #if (EAP_SUPPORT == ENABLED)
41 
42 #if(EAP_TRACE_LEVEL >= TRACE_LEVEL_DEBUG)
43 
44 //EAPOL packet types
45 static const EapParamName eapolPacketTypeList[] =
46 {
47  {EAPOL_TYPE_EAP, "EAPOL-EAP"},
48  {EAPOL_TYPE_START, "EAPOL-Start"},
49  {EAPOL_TYPE_LOGOFF, "EAPOL-Logoff"},
50  {EAPOL_TYPE_KEY, "EAPOL-Key"},
51  {EAPOL_TYPE_ENCAPSULATED_ASF_ALERT, "EAPOL-Encapsulated-ASF-Alert"}
52 };
53 
54 //EAP codes
55 static const EapParamName eapCodeList[] =
56 {
57  {EAP_CODE_REQUEST, "Request"},
58  {EAP_CODE_RESPONSE, "Response"},
59  {EAP_CODE_SUCCESS, "Success"},
60  {EAP_CODE_FAILURE, "Failure"}
61 };
62 
63 //EAP method types
64 static const EapParamName methodTypeList[] =
65 {
66  {EAP_METHOD_TYPE_IDENTITY, "Identity"},
67  {EAP_METHOD_TYPE_NOTIFICATION, "Notification"},
68  {EAP_METHOD_TYPE_NAK, "Nak"},
69  {EAP_METHOD_TYPE_MD5_CHALLENGE, "MD5-Challenge"},
70  {EAP_METHOD_TYPE_OTP, "One-Time Password"},
71  {EAP_METHOD_TYPE_GTC, "Generic Token Card"},
72  {EAP_METHOD_TYPE_TLS, "EAP-TLS"},
73  {EAP_METHOD_TYPE_TTLS, "EAP-TTLS"},
74  {EAP_METHOD_TYPE_PEAP, "PEAP"},
75  {EAP_METHOD_TYPE_MSCHAP_V2, "EAP-MSCHAP-V2"},
76  {EAP_METHOD_TYPE_EXPANDED_NAK, "Expanded NAK"}
77 };
78 
79 
80 /**
81  * @brief Dump EAPOL header for debugging purpose
82  * @param[in] header Pointer to the EAPOL header
83  **/
84 
85 void eapolDumpHeader(const EapolPdu *header)
86 {
87  const char_t *packetTypeName;
88 
89  //Convert the Packet Type field to string representation
90  packetTypeName = eapGetParamName(header->packetType, eapolPacketTypeList,
91  arraysize(eapolPacketTypeList));
92 
93  //Dump EAPOL header contents
94  TRACE_DEBUG(" Protocol Version = %" PRIu8 "\r\n", header->protocolVersion);
95  TRACE_DEBUG(" Packet Type = %" PRIu8 " (%s)\r\n", header->packetType, packetTypeName);
96  TRACE_DEBUG(" Packet Body Length = %" PRIu16 "\r\n", ntohs(header->packetBodyLen));
97 }
98 
99 
100 /**
101  * @brief Dump EAP header for debugging purpose
102  * @param[in] header Pointer to the EAP header
103  **/
104 
105 void eapDumpHeader(const EapPacket *header)
106 {
107  const char_t *codeName;
108  const char_t *methodTypeName;
109 
110  //Convert the Code field to string representation
111  codeName = eapGetParamName(header->code, eapCodeList,
112  arraysize(eapCodeList));
113 
114  //Dump EAP header contents
115  TRACE_DEBUG(" Code = %" PRIu8 " (%s)\r\n", header->code, codeName);
116  TRACE_DEBUG(" Identifier = %" PRIu8 "\r\n", header->identifier);
117  TRACE_DEBUG(" Length = %" PRIu16 "\r\n", ntohs(header->length));
118 
119  //Check Code field
120  if(header->code == EAP_CODE_REQUEST ||
121  header->code == EAP_CODE_RESPONSE)
122  {
123  //Convert the Method Type field to string representation
124  methodTypeName = eapGetParamName(header->data[0], methodTypeList,
125  arraysize(methodTypeList));
126 
127  //Dump Method Type field
128  TRACE_DEBUG(" Method Type = %" PRIu8 " (%s)\r\n", header->data[0],
129  methodTypeName);
130 
131  //EAP-TLS method?
132  if(header->data[0] == EAP_METHOD_TYPE_TLS)
133  {
134  //Dump Flags field
135  eapDumpTlsFlags(header->data[1]);
136  }
137  }
138 }
139 
140 
141 /**
142  * @brief Dump EAP-TLS flags
143  * @param[in] flags EAP-TLS specific options
144  **/
145 
146 void eapDumpTlsFlags(uint8_t flags)
147 {
148  uint8_t l;
149  uint8_t m;
150  uint8_t s;
151 
152  //The L flag (length included) is set to indicate the presence of the
153  //four-octet TLS Message Length field
154  l = (flags & EAP_TLS_FLAGS_L) ? 1 : 0;
155 
156  //The M flag (more fragments) is set on all but the last fragment
157  m = (flags & EAP_TLS_FLAGS_M) ? 1 : 0;
158 
159  //The S flag (EAP-TLS start) is set only within the EAP-TLS start message
160  s = (flags & EAP_TLS_FLAGS_S) ? 1 : 0;
161 
162  //Check whether any flag is set
163  if(l != 0 || m != 0 || s != 0)
164  {
165  //Dump the value of the Flags field
166  TRACE_DEBUG(" Flags = 0x%02" PRIX8 " (", flags);
167 
168  //Dump flags
169  while(1)
170  {
171  if(l != 0)
172  {
173  TRACE_DEBUG("Length");
174  l = FALSE;
175  }
176  else if(m != 0)
177  {
178  TRACE_DEBUG("More");
179  m = FALSE;
180  }
181  else if(s != 0)
182  {
183  TRACE_DEBUG("Start");
184  s = FALSE;
185  }
186  else
187  {
188  }
189 
190  if(l != 0 || m != 0 || s != 0)
191  {
192  TRACE_DEBUG(", ");
193  }
194  else
195  {
196  TRACE_DEBUG(")\r\n");
197  break;
198  }
199  }
200  }
201  else
202  {
203  //Dump the value of the Flags field
204  TRACE_DEBUG(" Flags = 0x%02" PRIX8 "\r\n", flags);
205  }
206 }
207 
208 #endif
209 
210 
211 /**
212  * @brief Convert a parameter to string representation
213  * @param[in] value Parameter value
214  * @param[in] paramList List of acceptable parameters
215  * @param[in] paramListLen Number of entries in the list
216  * @return NULL-terminated string describing the parameter
217  **/
218 
219 const char_t *eapGetParamName(uint_t value, const EapParamName *paramList,
220  size_t paramListLen)
221 {
222  uint_t i;
223 
224  //Default name for unknown values
225  static const char_t defaultName[] = "Unknown";
226 
227  //Loop through the list of acceptable parameters
228  for(i = 0; i < paramListLen; i++)
229  {
230  if(paramList[i].value == value)
231  return paramList[i].name;
232  }
233 
234  //Unknown value
235  return defaultName;
236 }
237 
238 #endif
@ EAP_METHOD_TYPE_TLS
EAP-TLS.
Definition: eap.h:172
@ EAP_TLS_FLAGS_L
Length included.
Definition: eap.h:186
void eapDumpHeader(const EapPacket *header)
Dump EAP header for debugging purpose.
Definition: eap_debug.c:105
@ EAP_METHOD_TYPE_OTP
One-Time Password (OTP)
Definition: eap.h:170
@ EAP_CODE_RESPONSE
Response.
Definition: eap.h:153
const char_t * eapGetParamName(uint_t value, const EapParamName *paramList, size_t paramListLen)
Convert a parameter to string representation.
Definition: eap_debug.c:219
EapolPdu
Definition: eap.h:211
@ EAP_CODE_FAILURE
Failure.
Definition: eap.h:155
@ EAP_METHOD_TYPE_NAK
Legacy Nak.
Definition: eap.h:168
#define FALSE
Definition: os_port.h:46
void eapolDumpHeader(const EapolPdu *header)
Dump EAPOL header for debugging purpose.
Definition: eap_debug.c:85
@ EAP_METHOD_TYPE_IDENTITY
Identity.
Definition: eap.h:166
@ EAPOL_TYPE_EAP
EAPOL-EAP.
Definition: eap.h:134
@ EAP_CODE_SUCCESS
Success.
Definition: eap.h:154
@ EAPOL_TYPE_ENCAPSULATED_ASF_ALERT
EAPOL-Encapsulated-ASF-Alert.
Definition: eap.h:138
void eapDumpTlsFlags(uint8_t flags)
Dump EAP-TLS flags.
Definition: eap_debug.c:146
@ EAP_METHOD_TYPE_MSCHAP_V2
EAP-MSCHAP-V2.
Definition: eap.h:175
@ EAP_TLS_FLAGS_S
EAP-TLS start.
Definition: eap.h:188
@ EAPOL_TYPE_KEY
EAPOL-Key.
Definition: eap.h:137
@ EAP_CODE_REQUEST
Request.
Definition: eap.h:152
@ EAP_METHOD_TYPE_TTLS
EAP-TTLS.
Definition: eap.h:173
uint8_t l
Definition: ndp.h:412
#define ntohs(value)
Definition: cpu_endian.h:421
uint8_t flags
Definition: tcp.h:351
#define TRACE_DEBUG(...)
Definition: debug.h:107
@ EAP_METHOD_TYPE_EXPANDED_NAK
Expanded NAK.
Definition: eap.h:176
char char_t
Definition: compiler_port.h:48
@ EAP_METHOD_TYPE_NOTIFICATION
Notification.
Definition: eap.h:167
Data logging functions for debugging purpose (EAP)
@ EAP_METHOD_TYPE_PEAP
PEAP.
Definition: eap.h:174
uint8_t m
Definition: ndp.h:304
@ EAP_TLS_FLAGS_M
More fragments.
Definition: eap.h:187
EapPacket
Definition: eap.h:224
const char_t * name
Definition: eap_debug.h:52
@ EAP_METHOD_TYPE_GTC
Generic Token Card (GTC)
Definition: eap.h:171
uint8_t value[]
Definition: tcp.h:369
uint8_t s
Definition: igmp_common.h:234
@ EAPOL_TYPE_LOGOFF
EAPOL-Logoff.
Definition: eap.h:136
@ EAPOL_TYPE_START
EAPOL-Start.
Definition: eap.h:135
unsigned int uint_t
Definition: compiler_port.h:50
EAP (Extensible Authentication Protocol)
Parameter value/name binding.
Definition: eap_debug.h:50
Debugging facilities.
@ EAP_METHOD_TYPE_MD5_CHALLENGE
MD5-Challenge.
Definition: eap.h:169
#define arraysize(a)
Definition: os_port.h:71