chap.h
Go to the documentation of this file.
1 /**
2  * @file chap.h
3  * @brief CHAP (Challenge Handshake Authentication Protocol)
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneTCP 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.0
29  **/
30 
31 #ifndef _CHAP_H
32 #define _CHAP_H
33 
34 //Dependencies
35 #include "core/net.h"
36 #include "ppp/ppp.h"
37 
38 //CHAP authentication support
39 #ifndef CHAP_SUPPORT
40  #define CHAP_SUPPORT DISABLED
41 #elif (CHAP_SUPPORT != ENABLED && CHAP_SUPPORT != DISABLED)
42  #error CHAP_SUPPORT parameter is not valid
43 #endif
44 
45 //Restart timer
46 #ifndef CHAP_RESTART_TIMER
47  #define CHAP_RESTART_TIMER 3000
48 #elif (CHAP_RESTART_TIMER < 1000)
49  #error CHAP_RESTART_TIMER parameter is not valid
50 #endif
51 
52 //Maximum number of retransmissions for Challenge packets
53 #ifndef CHAP_MAX_CHALLENGES
54  #define CHAP_MAX_CHALLENGES 5
55 #elif (CHAP_MAX_CHALLENGES < 1)
56  #error CHAP_MAX_CHALLENGES parameter is not valid
57 #endif
58 
59 //C++ guard
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63 
64 
65 /**
66  * @brief CHAP states
67  **/
68 
69 typedef enum
70 {
82 
83 
84 /**
85  * @brief Code field values
86  **/
87 
88 typedef enum
89 {
90  CHAP_CODE_CHALLENGE = 1, ///<Challenge
91  CHAP_CODE_RESPONSE = 2, ///<Response
92  CHAP_CODE_SUCCESS = 3, ///<Success
93  CHAP_CODE_FAILURE = 4 ///<Failure
95 
96 
97 /**
98  * @brief CHAP algorithm identifiers
99  **/
100 
101 typedef enum
102 {
103  CHAP_ALGO_ID_CHAP_MD5 = 5, //CHAP with MD5
104  CHAP_ALGO_ID_MS_CHAP = 128, //MS-CHAP
105  CHAP_ALGO_ID_MS_CHAP_V2 = 129 //MS-CHAP-2
107 
108 
109 //CC-RX, CodeWarrior or Win32 compiler?
110 #if defined(__CCRX__)
111  #pragma pack
112 #elif defined(__CWCC__) || defined(_WIN32)
113  #pragma pack(push, 1)
114 #endif
115 
116 
117 /**
118  * @brief Challenge packet
119  **/
120 
122 {
123  uint8_t code; //0
124  uint8_t identifier; //1
125  uint16_t length; //2-3
126  uint8_t valueSize; //4
127  uint8_t value[]; //5
129 
130 
131 /**
132  * @brief Response packet
133  **/
134 
135 typedef __packed_struct
136 {
137  uint8_t code; //0
138  uint8_t identifier; //1
139  uint16_t length; //2-3
140  uint8_t valueSize; //4
141  uint8_t value[]; //5
143 
144 
145 /**
146  * @brief Success packet
147  **/
148 
149 typedef __packed_struct
150 {
151  uint8_t code; //0
152  uint8_t identifier; //1
153  uint16_t length; //2-3
154  uint8_t message[]; //4
156 
157 
158 /**
159  * @brief Failure packet
160  **/
161 
162 typedef __packed_struct
163 {
164  uint8_t code; //0
165  uint8_t identifier; //1
166  uint16_t length; //2-3
167  uint8_t message[]; //4
169 
170 
171 //CC-RX, CodeWarrior or Win32 compiler?
172 #if defined(__CCRX__)
173  #pragma unpack
174 #elif defined(__CWCC__) || defined(_WIN32)
175  #pragma pack(pop)
176 #endif
177 
178 
179 /**
180  * @brief CHAP finite state machine
181  **/
182 
183 typedef struct
184 {
185  uint_t localState; ///<Local state
186  uint8_t localIdentifier; ///<Identifier used to match requests and replies
187  uint_t peerState; ///<Peer state
188  uint8_t peerIdentifier; ///<Identifier used to match requests and replies
189  uint_t restartCounter; ///<Restart counter
190  systime_t timestamp; ///<Timestamp to manage retransmissions
191  uint8_t challenge[16]; ///<Challenge value sent to the peer
192  const uint8_t *response; ///<Response value from the peer
193 } ChapFsm;
194 
195 
196 //CHAP related functions
199 
200 void chapTick(PppContext *context);
201 
202 void chapProcessPacket(PppContext *context,
203  const PppPacket *packet, size_t length);
204 
206  const ChapChallengePacket *challengePacket, size_t length);
207 
209  const ChapResponsePacket *responsePacket, size_t length);
210 
212  const ChapSuccessPacket *successPacket, size_t length);
213 
215  const ChapFailurePacket *failurePacket, size_t length);
216 
218 error_t chapSendResponse(PppContext *context, const uint8_t *value);
221 
222 bool_t chapCheckPassword(PppContext *context, const char_t *password);
223 
224 //C++ guard
225 #ifdef __cplusplus
226 }
227 #endif
228 
229 #endif
ChapFailurePacket
Definition: chap.h:168
error_t chapProcessSuccess(PppContext *context, const ChapSuccessPacket *successPacket, size_t length)
Process Success packet.
Definition: chap.c:388
uint16_t length
Definition: chap.h:125
error_t chapAbortAuth(PppContext *context)
Abort CHAP authentication.
Definition: chap.c:91
ChapChallengePacket
Definition: chap.h:128
ChapState
CHAP states.
Definition: chap.h:70
@ CHAP_STATE_3_CHALLENGE_RCVD
Definition: chap.h:74
@ CHAP_STATE_2_CHALLENGE_SENT
Definition: chap.h:73
@ CHAP_STATE_7_SUCCESS_RCVD
Definition: chap.h:78
@ CHAP_STATE_8_FAILURE_SENT
Definition: chap.h:79
@ CHAP_STATE_6_SUCCESS_SENT
Definition: chap.h:77
@ CHAP_STATE_9_FAILURE_RCVD
Definition: chap.h:80
@ CHAP_STATE_4_RESPONSE_SENT
Definition: chap.h:75
@ CHAP_STATE_1_STARTED
Definition: chap.h:72
@ CHAP_STATE_5_RESPONSE_RCVD
Definition: chap.h:76
@ CHAP_STATE_0_INITIAL
Definition: chap.h:71
error_t chapSendFailure(PppContext *context)
Send Failure packet.
Definition: chap.c:665
void chapProcessPacket(PppContext *context, const PppPacket *packet, size_t length)
Process an incoming CHAP packet.
Definition: chap.c:149
ChapCode
Code field values.
Definition: chap.h:89
@ CHAP_CODE_RESPONSE
Response.
Definition: chap.h:91
@ CHAP_CODE_SUCCESS
Success.
Definition: chap.h:92
@ CHAP_CODE_CHALLENGE
Challenge.
Definition: chap.h:90
@ CHAP_CODE_FAILURE
Failure.
Definition: chap.h:93
error_t chapProcessChallenge(PppContext *context, const ChapChallengePacket *challengePacket, size_t length)
Process Challenge packet.
Definition: chap.c:218
ChapAlgoId
CHAP algorithm identifiers.
Definition: chap.h:102
@ CHAP_ALGO_ID_CHAP_MD5
Definition: chap.h:103
@ CHAP_ALGO_ID_MS_CHAP
Definition: chap.h:104
@ CHAP_ALGO_ID_MS_CHAP_V2
Definition: chap.h:105
ChapResponsePacket
Definition: chap.h:142
bool_t chapCheckPassword(PppContext *context, const char_t *password)
Password verification.
Definition: chap.c:712
error_t chapSendSuccess(PppContext *context)
Send Success packet.
Definition: chap.c:619
error_t chapProcessFailure(PppContext *context, const ChapFailurePacket *failurePacket, size_t length)
Process Failure packet.
Definition: chap.c:445
error_t chapSendResponse(PppContext *context, const uint8_t *value)
Send Response packet.
Definition: chap.c:562
uint8_t valueSize
Definition: chap.h:126
error_t chapProcessResponse(PppContext *context, const ChapResponsePacket *responsePacket, size_t length)
Process Response packet.
Definition: chap.c:273
error_t chapSendChallenge(PppContext *context)
Send Challenge packet.
Definition: chap.c:480
typedef __packed_struct
Challenge packet.
Definition: chap.h:122
uint8_t identifier
Definition: chap.h:124
void chapTick(PppContext *context)
CHAP timer handler.
Definition: chap.c:110
uint8_t value[]
Definition: chap.h:127
ChapSuccessPacket
Definition: chap.h:155
uint8_t message[]
Definition: chap.h:154
error_t chapStartAuth(PppContext *context)
Start CHAP authentication.
Definition: chap.c:57
uint8_t code
Definition: coap_common.h:179
unsigned int uint_t
Definition: compiler_port.h:50
char char_t
Definition: compiler_port.h:48
int bool_t
Definition: compiler_port.h:53
error_t
Error codes.
Definition: error.h:43
TCP/IP stack core.
uint32_t systime_t
System time.
PPP (Point-to-Point Protocol)
#define PppContext
Definition: ppp.h:38
#define PppPacket
Definition: ppp.h:37
CHAP finite state machine.
Definition: chap.h:184
systime_t timestamp
Timestamp to manage retransmissions.
Definition: chap.h:190
uint_t localState
Local state.
Definition: chap.h:185
uint8_t peerIdentifier
Identifier used to match requests and replies.
Definition: chap.h:188
uint8_t localIdentifier
Identifier used to match requests and replies.
Definition: chap.h:186
uint_t peerState
Peer state.
Definition: chap.h:187
uint_t restartCounter
Restart counter.
Definition: chap.h:189
const uint8_t * response
Response value from the peer.
Definition: chap.h:192