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  * Copyright (C) 2010-2018 Oryx Embedded SARL. All rights reserved.
8  *
9  * This file is part of CycloneTCP Open.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * @author Oryx Embedded SARL (www.oryx-embedded.com)
26  * @version 1.9.0
27  **/
28 
29 #ifndef _CHAP_H
30 #define _CHAP_H
31 
32 //Dependencies
33 #include "core/net.h"
34 #include "ppp/ppp.h"
35 
36 //CHAP authentication support
37 #ifndef CHAP_SUPPORT
38  #define CHAP_SUPPORT DISABLED
39 #elif (CHAP_SUPPORT != ENABLED && CHAP_SUPPORT != DISABLED)
40  #error CHAP_SUPPORT parameter is not valid
41 #endif
42 
43 //Restart timer
44 #ifndef CHAP_RESTART_TIMER
45  #define CHAP_RESTART_TIMER 3000
46 #elif (CHAP_RESTART_TIMER < 1000)
47  #error CHAP_RESTART_TIMER parameter is not valid
48 #endif
49 
50 //Maximum number of retransmissions for Challenge packets
51 #ifndef CHAP_MAX_CHALLENGES
52  #define CHAP_MAX_CHALLENGES 5
53 #elif (CHAP_MAX_CHALLENGES < 1)
54  #error CHAP_MAX_CHALLENGES parameter is not valid
55 #endif
56 
57 //C++ guard
58 #ifdef __cplusplus
59  extern "C" {
60 #endif
61 
62 
63 /**
64  * @brief CHAP states
65  **/
66 
67 typedef enum
68 {
79 } ChapState;
80 
81 
82 /**
83  * @brief Code field values
84  **/
85 
86 typedef enum
87 {
88  CHAP_CODE_CHALLENGE = 1, ///<Challenge
89  CHAP_CODE_RESPONSE = 2, ///<Response
90  CHAP_CODE_SUCCESS = 3, ///<Success
91  CHAP_CODE_FAILURE = 4 ///<Failure
92 } ChapCode;
93 
94 
95 /**
96  * @brief CHAP algorithm identifiers
97  **/
98 
99 typedef enum
100 {
101  CHAP_ALGO_ID_CHAP_MD5 = 5, //CHAP with MD5
102  CHAP_ALGO_ID_MS_CHAP = 128, //MS-CHAP
103  CHAP_ALGO_ID_MS_CHAP_V2 = 129 //MS-CHAP-2
104 } ChapAlgoId;
105 
106 
107 //CodeWarrior or Win32 compiler?
108 #if defined(__CWCC__) || defined(_WIN32)
109  #pragma pack(push, 1)
110 #endif
111 
112 
113 /**
114  * @brief Challenge packet
115  **/
116 
117 typedef __start_packed struct
118 {
119  uint8_t code; //0
120  uint8_t identifier; //1
121  uint16_t length; //2-3
122  uint8_t valueSize; //4
123  uint8_t value[]; //5
125 
126 
127 /**
128  * @brief Response packet
129  **/
130 
131 typedef __start_packed struct
132 {
133  uint8_t code; //0
134  uint8_t identifier; //1
135  uint16_t length; //2-3
136  uint8_t valueSize; //4
137  uint8_t value[]; //5
139 
140 
141 /**
142  * @brief Success packet
143  **/
144 
145 typedef __start_packed struct
146 {
147  uint8_t code; //0
148  uint8_t identifier; //1
149  uint16_t length; //2-3
150  uint8_t message[]; //4
152 
153 
154 /**
155  * @brief Failure packet
156  **/
157 
158 typedef __start_packed struct
159 {
160  uint8_t code; //0
161  uint8_t identifier; //1
162  uint16_t length; //2-3
163  uint8_t message[]; //4
165 
166 
167 //CodeWarrior or Win32 compiler?
168 #if defined(__CWCC__) || defined(_WIN32)
169  #pragma pack(pop)
170 #endif
171 
172 
173 /**
174  * @brief CHAP finite state machine
175  **/
176 
177 typedef struct
178 {
179  uint_t localState; ///<Local state
180  uint8_t localIdentifier; ///<Identifier used to match requests and replies
181  uint_t peerState; ///<Peer state
182  uint8_t peerIdentifier; ///<Identifier used to match requests and replies
183  uint_t restartCounter; ///<Restart counter
184  systime_t timestamp; ///<Timestamp to manage retransmissions
185  uint8_t challenge[16]; ///<Challenge value sent to the peer
186  const uint8_t *response; ///<Response value from the peer
187 } ChapFsm;
188 
189 
190 //CHAP related functions
193 
194 void chapTick(PppContext *context);
195 
196 void chapProcessPacket(PppContext *context,
197  const PppPacket *packet, size_t length);
198 
200  const ChapChallengePacket *challengePacket, size_t length);
201 
203  const ChapResponsePacket *responsePacket, size_t length);
204 
206  const ChapSuccessPacket *successPacket, size_t length);
207 
209  const ChapFailurePacket *failurePacket, size_t length);
210 
212 error_t chapSendResponse(PppContext *context, const uint8_t *value);
215 
216 bool_t chapCheckPassword(PppContext *context, const char_t *password);
217 
218 //C++ guard
219 #ifdef __cplusplus
220  }
221 #endif
222 
223 #endif
PPP (Point-to-Point Protocol)
uint32_t systime_t
Definition: compiler_port.h:44
char char_t
Definition: compiler_port.h:41
ChapAlgoId
CHAP algorithm identifiers.
Definition: chap.h:99
error_t chapSendFailure(PppContext *context)
Send Failure packet.
Definition: chap.c:663
TCP/IP stack core.
error_t chapSendResponse(PppContext *context, const uint8_t *value)
Send Response packet.
Definition: chap.c:560
uint8_t code
Definition: chap.h:119
error_t chapStartAuth(PppContext *context)
Start CHAP authentication.
Definition: chap.c:55
uint8_t message[]
Definition: chap.h:150
systime_t timestamp
Timestamp to manage retransmissions.
Definition: chap.h:184
Failure.
Definition: chap.h:91
uint8_t identifier
Definition: chap.h:120
#define PppPacket
Definition: ppp.h:35
void chapTick(PppContext *context)
CHAP timer handler.
Definition: chap.c:108
error_t chapProcessSuccess(PppContext *context, const ChapSuccessPacket *successPacket, size_t length)
Process Success packet.
Definition: chap.c:386
uint8_t valueSize
Definition: chap.h:122
Response.
Definition: chap.h:89
void chapProcessPacket(PppContext *context, const PppPacket *packet, size_t length)
Process an incoming CHAP packet.
Definition: chap.c:147
error_t chapProcessResponse(PppContext *context, const ChapResponsePacket *responsePacket, size_t length)
Process Response packet.
Definition: chap.c:271
uint8_t localIdentifier
Identifier used to match requests and replies.
Definition: chap.h:180
uint8_t peerIdentifier
Identifier used to match requests and replies.
Definition: chap.h:182
Challenge.
Definition: chap.h:88
uint_t restartCounter
Restart counter.
Definition: chap.h:183
__start_packed struct @267 ChapSuccessPacket
Success packet.
#define PppContext
Definition: ppp.h:36
__start_packed struct @265 ChapChallengePacket
Challenge packet.
uint_t peerState
Peer state.
Definition: chap.h:181
__start_packed struct _Ipv4Header __end_packed
ChapState
CHAP states.
Definition: chap.h:67
error_t
Error codes.
Definition: error.h:40
unsigned int uint_t
Definition: compiler_port.h:43
error_t chapProcessChallenge(PppContext *context, const ChapChallengePacket *challengePacket, size_t length)
Process Challenge packet.
Definition: chap.c:216
uint8_t value[]
Definition: chap.h:123
__start_packed struct @266 ChapResponsePacket
Response packet.
error_t chapProcessFailure(PppContext *context, const ChapFailurePacket *failurePacket, size_t length)
Process Failure packet.
Definition: chap.c:443
const uint8_t * response
Response value from the peer.
Definition: chap.h:186
bool_t chapCheckPassword(PppContext *context, const char_t *password)
Password verification.
Definition: chap.c:710
error_t chapSendChallenge(PppContext *context)
Send Challenge packet.
Definition: chap.c:478
ChapCode
Code field values.
Definition: chap.h:86
uint16_t length
Definition: chap.h:121
error_t chapSendSuccess(PppContext *context)
Send Success packet.
Definition: chap.c:617
uint_t localState
Local state.
Definition: chap.h:179
int bool_t
Definition: compiler_port.h:47
Success.
Definition: chap.h:90
__start_packed struct @268 ChapFailurePacket
Failure packet.
CHAP finite state machine.
Definition: chap.h:177
error_t chapAbortAuth(PppContext *context)
Abort CHAP authentication.
Definition: chap.c:89