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-2023 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.2.4
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 //CodeWarrior or Win32 compiler?
110 #if defined(__CWCC__) || defined(_WIN32)
111  #pragma pack(push, 1)
112 #endif
113 
114 
115 /**
116  * @brief Challenge packet
117  **/
118 
119 typedef __start_packed struct
120 {
121  uint8_t code; //0
122  uint8_t identifier; //1
123  uint16_t length; //2-3
124  uint8_t valueSize; //4
125  uint8_t value[]; //5
127 
128 
129 /**
130  * @brief Response packet
131  **/
132 
133 typedef __start_packed struct
134 {
135  uint8_t code; //0
136  uint8_t identifier; //1
137  uint16_t length; //2-3
138  uint8_t valueSize; //4
139  uint8_t value[]; //5
141 
142 
143 /**
144  * @brief Success packet
145  **/
146 
147 typedef __start_packed struct
148 {
149  uint8_t code; //0
150  uint8_t identifier; //1
151  uint16_t length; //2-3
152  uint8_t message[]; //4
154 
155 
156 /**
157  * @brief Failure packet
158  **/
159 
160 typedef __start_packed struct
161 {
162  uint8_t code; //0
163  uint8_t identifier; //1
164  uint16_t length; //2-3
165  uint8_t message[]; //4
167 
168 
169 //CodeWarrior or Win32 compiler?
170 #if defined(__CWCC__) || defined(_WIN32)
171  #pragma pack(pop)
172 #endif
173 
174 
175 /**
176  * @brief CHAP finite state machine
177  **/
178 
179 typedef struct
180 {
181  uint_t localState; ///<Local state
182  uint8_t localIdentifier; ///<Identifier used to match requests and replies
183  uint_t peerState; ///<Peer state
184  uint8_t peerIdentifier; ///<Identifier used to match requests and replies
185  uint_t restartCounter; ///<Restart counter
186  systime_t timestamp; ///<Timestamp to manage retransmissions
187  uint8_t challenge[16]; ///<Challenge value sent to the peer
188  const uint8_t *response; ///<Response value from the peer
189 } ChapFsm;
190 
191 
192 //CHAP related functions
195 
196 void chapTick(PppContext *context);
197 
198 void chapProcessPacket(PppContext *context,
199  const PppPacket *packet, size_t length);
200 
202  const ChapChallengePacket *challengePacket, size_t length);
203 
205  const ChapResponsePacket *responsePacket, size_t length);
206 
208  const ChapSuccessPacket *successPacket, size_t length);
209 
211  const ChapFailurePacket *failurePacket, size_t length);
212 
214 error_t chapSendResponse(PppContext *context, const uint8_t *value);
217 
218 bool_t chapCheckPassword(PppContext *context, const char_t *password);
219 
220 //C++ guard
221 #ifdef __cplusplus
222 }
223 #endif
224 
225 #endif
__start_packed struct @3 ChapFailurePacket
Failure packet.
@ CHAP_STATE_0_INITIAL
Definition: chap.h:71
@ CHAP_STATE_3_CHALLENGE_RCVD
Definition: chap.h:74
@ CHAP_ALGO_ID_CHAP_MD5
Definition: chap.h:103
int bool_t
Definition: compiler_port.h:53
#define PppPacket
Definition: ppp.h:37
@ CHAP_CODE_SUCCESS
Success.
Definition: chap.h:92
ChapAlgoId
CHAP algorithm identifiers.
Definition: chap.h:102
bool_t chapCheckPassword(PppContext *context, const char_t *password)
Password verification.
Definition: chap.c:712
uint_t restartCounter
Restart counter.
Definition: chap.h:185
@ CHAP_STATE_6_SUCCESS_SENT
Definition: chap.h:77
__start_packed struct @1 ChapResponsePacket
Response packet.
@ CHAP_STATE_1_STARTED
Definition: chap.h:72
error_t chapProcessResponse(PppContext *context, const ChapResponsePacket *responsePacket, size_t length)
Process Response packet.
Definition: chap.c:273
error_t chapAbortAuth(PppContext *context)
Abort CHAP authentication.
Definition: chap.c:91
@ CHAP_CODE_CHALLENGE
Challenge.
Definition: chap.h:90
error_t chapStartAuth(PppContext *context)
Start CHAP authentication.
Definition: chap.c:57
@ CHAP_STATE_2_CHALLENGE_SENT
Definition: chap.h:73
uint8_t value[]
Definition: chap.h:125
@ CHAP_CODE_RESPONSE
Response.
Definition: chap.h:91
void chapProcessPacket(PppContext *context, const PppPacket *packet, size_t length)
Process an incoming CHAP packet.
Definition: chap.c:149
error_t chapSendFailure(PppContext *context)
Send Failure packet.
Definition: chap.c:665
#define PppContext
Definition: ppp.h:38
error_t chapSendResponse(PppContext *context, const uint8_t *value)
Send Response packet.
Definition: chap.c:562
ChapState
CHAP states.
Definition: chap.h:70
error_t
Error codes.
Definition: error.h:43
uint16_t length
Definition: chap.h:123
@ CHAP_CODE_FAILURE
Failure.
Definition: chap.h:93
error_t chapProcessSuccess(PppContext *context, const ChapSuccessPacket *successPacket, size_t length)
Process Success packet.
Definition: chap.c:388
ChapCode
Code field values.
Definition: chap.h:89
@ CHAP_STATE_4_RESPONSE_SENT
Definition: chap.h:75
@ CHAP_ALGO_ID_MS_CHAP_V2
Definition: chap.h:105
@ CHAP_STATE_7_SUCCESS_RCVD
Definition: chap.h:78
__start_packed struct _Ipv4Header __end_packed
@ CHAP_STATE_9_FAILURE_RCVD
Definition: chap.h:80
__start_packed struct @0 ChapChallengePacket
Challenge packet.
@ CHAP_STATE_5_RESPONSE_RCVD
Definition: chap.h:76
error_t chapSendChallenge(PppContext *context)
Send Challenge packet.
Definition: chap.c:480
uint32_t systime_t
System time.
error_t chapSendSuccess(PppContext *context)
Send Success packet.
Definition: chap.c:619
char char_t
Definition: compiler_port.h:48
__start_packed struct @2 ChapSuccessPacket
Success packet.
error_t chapProcessFailure(PppContext *context, const ChapFailurePacket *failurePacket, size_t length)
Process Failure packet.
Definition: chap.c:445
uint8_t peerIdentifier
Identifier used to match requests and replies.
Definition: chap.h:184
uint8_t code
Definition: chap.h:121
uint8_t message[]
Definition: chap.h:152
uint_t localState
Local state.
Definition: chap.h:181
error_t chapProcessChallenge(PppContext *context, const ChapChallengePacket *challengePacket, size_t length)
Process Challenge packet.
Definition: chap.c:218
PPP (Point-to-Point Protocol)
uint_t peerState
Peer state.
Definition: chap.h:183
const uint8_t * response
Response value from the peer.
Definition: chap.h:188
uint8_t valueSize
Definition: chap.h:124
unsigned int uint_t
Definition: compiler_port.h:50
TCP/IP stack core.
systime_t timestamp
Timestamp to manage retransmissions.
Definition: chap.h:186
@ CHAP_STATE_8_FAILURE_SENT
Definition: chap.h:79
@ CHAP_ALGO_ID_MS_CHAP
Definition: chap.h:104
void chapTick(PppContext *context)
CHAP timer handler.
Definition: chap.c:110
uint8_t localIdentifier
Identifier used to match requests and replies.
Definition: chap.h:182
CHAP finite state machine.
Definition: chap.h:180
uint8_t identifier
Definition: chap.h:122