• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * RADIUS message processing
3  * Copyright (c) 2002-2009, 2012, 2014-2022, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #ifndef RADIUS_H
10 #define RADIUS_H
11 
12 /* RFC 2865 - RADIUS */
13 
14 #ifdef _MSC_VER
15 #pragma pack(push, 1)
16 #endif /* _MSC_VER */
17 
18 struct radius_hdr {
19 	u8 code;
20 	u8 identifier;
21 	be16 length; /* including this header */
22 	u8 authenticator[16];
23 	/* followed by length-20 octets of attributes */
24 } STRUCT_PACKED;
25 
26 enum { RADIUS_CODE_ACCESS_REQUEST = 1,
27        RADIUS_CODE_ACCESS_ACCEPT = 2,
28        RADIUS_CODE_ACCESS_REJECT = 3,
29        RADIUS_CODE_ACCOUNTING_REQUEST = 4,
30        RADIUS_CODE_ACCOUNTING_RESPONSE = 5,
31        RADIUS_CODE_ACCESS_CHALLENGE = 11,
32        RADIUS_CODE_STATUS_SERVER = 12,
33        RADIUS_CODE_STATUS_CLIENT = 13,
34        RADIUS_CODE_DISCONNECT_REQUEST = 40,
35        RADIUS_CODE_DISCONNECT_ACK = 41,
36        RADIUS_CODE_DISCONNECT_NAK = 42,
37        RADIUS_CODE_COA_REQUEST = 43,
38        RADIUS_CODE_COA_ACK = 44,
39        RADIUS_CODE_COA_NAK = 45,
40        RADIUS_CODE_RESERVED = 255
41 };
42 
43 struct radius_attr_hdr {
44 	u8 type;
45 	u8 length; /* including this header */
46 	/* followed by length-2 octets of attribute value */
47 } STRUCT_PACKED;
48 
49 struct radius_attr_hdr_ext {
50 	u8 type;
51 	u8 length; /* including this header */
52 	u8 ext_type;
53 	/* followed by length-3 octets of attribute value */
54 } STRUCT_PACKED;
55 
56 #define RADIUS_MAX_ATTR_LEN (255 - sizeof(struct radius_attr_hdr))
57 #define RADIUS_MAX_EXT_ATTR_LEN (255 - sizeof(struct radius_attr_hdr_ext))
58 
59 enum { RADIUS_ATTR_USER_NAME = 1,
60        RADIUS_ATTR_USER_PASSWORD = 2,
61        RADIUS_ATTR_NAS_IP_ADDRESS = 4,
62        RADIUS_ATTR_NAS_PORT = 5,
63        RADIUS_ATTR_SERVICE_TYPE = 6,
64        RADIUS_ATTR_FRAMED_IP_ADDRESS = 8,
65        RADIUS_ATTR_FILTER_ID = 11,
66        RADIUS_ATTR_FRAMED_MTU = 12,
67        RADIUS_ATTR_REPLY_MESSAGE = 18,
68        RADIUS_ATTR_STATE = 24,
69        RADIUS_ATTR_CLASS = 25,
70        RADIUS_ATTR_VENDOR_SPECIFIC = 26,
71        RADIUS_ATTR_SESSION_TIMEOUT = 27,
72        RADIUS_ATTR_IDLE_TIMEOUT = 28,
73        RADIUS_ATTR_TERMINATION_ACTION = 29,
74        RADIUS_ATTR_CALLED_STATION_ID = 30,
75        RADIUS_ATTR_CALLING_STATION_ID = 31,
76        RADIUS_ATTR_NAS_IDENTIFIER = 32,
77        RADIUS_ATTR_PROXY_STATE = 33,
78        RADIUS_ATTR_ACCT_STATUS_TYPE = 40,
79        RADIUS_ATTR_ACCT_DELAY_TIME = 41,
80        RADIUS_ATTR_ACCT_INPUT_OCTETS = 42,
81        RADIUS_ATTR_ACCT_OUTPUT_OCTETS = 43,
82        RADIUS_ATTR_ACCT_SESSION_ID = 44,
83        RADIUS_ATTR_ACCT_AUTHENTIC = 45,
84        RADIUS_ATTR_ACCT_SESSION_TIME = 46,
85        RADIUS_ATTR_ACCT_INPUT_PACKETS = 47,
86        RADIUS_ATTR_ACCT_OUTPUT_PACKETS = 48,
87        RADIUS_ATTR_ACCT_TERMINATE_CAUSE = 49,
88        RADIUS_ATTR_ACCT_MULTI_SESSION_ID = 50,
89        RADIUS_ATTR_ACCT_LINK_COUNT = 51,
90        RADIUS_ATTR_ACCT_INPUT_GIGAWORDS = 52,
91        RADIUS_ATTR_ACCT_OUTPUT_GIGAWORDS = 53,
92        RADIUS_ATTR_EVENT_TIMESTAMP = 55,
93        RADIUS_ATTR_EGRESS_VLANID = 56,
94        RADIUS_ATTR_NAS_PORT_TYPE = 61,
95        RADIUS_ATTR_TUNNEL_TYPE = 64,
96        RADIUS_ATTR_TUNNEL_MEDIUM_TYPE = 65,
97        RADIUS_ATTR_TUNNEL_PASSWORD = 69,
98        RADIUS_ATTR_CONNECT_INFO = 77,
99        RADIUS_ATTR_EAP_MESSAGE = 79,
100        RADIUS_ATTR_MESSAGE_AUTHENTICATOR = 80,
101        RADIUS_ATTR_TUNNEL_PRIVATE_GROUP_ID = 81,
102        RADIUS_ATTR_ACCT_INTERIM_INTERVAL = 85,
103        RADIUS_ATTR_CHARGEABLE_USER_IDENTITY = 89,
104        RADIUS_ATTR_NAS_IPV6_ADDRESS = 95,
105        RADIUS_ATTR_ERROR_CAUSE = 101,
106        RADIUS_ATTR_EAP_KEY_NAME = 102,
107        RADIUS_ATTR_OPERATOR_NAME = 126,
108        RADIUS_ATTR_LOCATION_INFO = 127,
109        RADIUS_ATTR_LOCATION_DATA = 128,
110        RADIUS_ATTR_BASIC_LOCATION_POLICY_RULES = 129,
111        RADIUS_ATTR_EXTENDED_LOCATION_POLICY_RULES = 130,
112        RADIUS_ATTR_LOCATION_CAPABLE = 131,
113        RADIUS_ATTR_REQUESTED_LOCATION_INFO = 132,
114        RADIUS_ATTR_GSS_ACCEPTOR_SERVICE_NAME = 164,
115        RADIUS_ATTR_GSS_ACCEPTOR_HOST_NAME = 165,
116        RADIUS_ATTR_GSS_ACCEPTOR_SERVICE_SPECIFICS = 166,
117        RADIUS_ATTR_GSS_ACCEPTOR_REALM_NAME = 167,
118        RADIUS_ATTR_MOBILITY_DOMAIN_ID = 177,
119        RADIUS_ATTR_WLAN_HESSID = 181,
120        RADIUS_ATTR_WLAN_REASON_CODE = 185,
121        RADIUS_ATTR_WLAN_PAIRWISE_CIPHER = 186,
122        RADIUS_ATTR_WLAN_GROUP_CIPHER = 187,
123        RADIUS_ATTR_WLAN_AKM_SUITE = 188,
124        RADIUS_ATTR_WLAN_GROUP_MGMT_CIPHER = 189,
125        RADIUS_ATTR_EXT_TYPE_1 = 241,
126        RADIUS_ATTR_EXT_TYPE_2 = 242,
127        RADIUS_ATTR_EXT_TYPE_3 = 243,
128        RADIUS_ATTR_EXT_TYPE_4 = 244,
129        RADIUS_ATTR_LONG_EXT_TYPE_1 = 245,
130        RADIUS_ATTR_LONG_EXT_TYPE_2 = 246,
131        RADIUS_ATTR_EXT_VENDOR_SPECIFIC_1 = (241 << 8) | 26,
132        RADIUS_ATTR_EXT_VENDOR_SPECIFIC_2 = (242 << 8) | 26,
133        RADIUS_ATTR_EXT_VENDOR_SPECIFIC_3 = (243 << 8) | 26,
134        RADIUS_ATTR_EXT_VENDOR_SPECIFIC_4 = (244 << 8) | 26,
135        RADIUS_ATTR_EXT_VENDOR_SPECIFIC_5 = (245 << 8) | 26,
136        RADIUS_ATTR_EXT_VENDOR_SPECIFIC_6 = (246 << 8) | 26,
137 };
138 
139 
140 /* Service-Type values (RFC 2865, 5.6) */
141 #define RADIUS_SERVICE_TYPE_FRAMED 2
142 
143 /* Termination-Action */
144 #define RADIUS_TERMINATION_ACTION_DEFAULT 0
145 #define RADIUS_TERMINATION_ACTION_RADIUS_REQUEST 1
146 
147 /* NAS-Port-Type */
148 #define RADIUS_NAS_PORT_TYPE_IEEE_802_11 19
149 
150 /* Acct-Status-Type */
151 #define RADIUS_ACCT_STATUS_TYPE_START 1
152 #define RADIUS_ACCT_STATUS_TYPE_STOP 2
153 #define RADIUS_ACCT_STATUS_TYPE_INTERIM_UPDATE 3
154 #define RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_ON 7
155 #define RADIUS_ACCT_STATUS_TYPE_ACCOUNTING_OFF 8
156 
157 /* Acct-Authentic */
158 #define RADIUS_ACCT_AUTHENTIC_RADIUS 1
159 #define RADIUS_ACCT_AUTHENTIC_LOCAL 2
160 #define RADIUS_ACCT_AUTHENTIC_REMOTE 3
161 
162 /* Acct-Terminate-Cause */
163 #define RADIUS_ACCT_TERMINATE_CAUSE_USER_REQUEST 1
164 #define RADIUS_ACCT_TERMINATE_CAUSE_LOST_CARRIER 2
165 #define RADIUS_ACCT_TERMINATE_CAUSE_LOST_SERVICE 3
166 #define RADIUS_ACCT_TERMINATE_CAUSE_IDLE_TIMEOUT 4
167 #define RADIUS_ACCT_TERMINATE_CAUSE_SESSION_TIMEOUT 5
168 #define RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_RESET 6
169 #define RADIUS_ACCT_TERMINATE_CAUSE_ADMIN_REBOOT 7
170 #define RADIUS_ACCT_TERMINATE_CAUSE_PORT_ERROR 8
171 #define RADIUS_ACCT_TERMINATE_CAUSE_NAS_ERROR 9
172 #define RADIUS_ACCT_TERMINATE_CAUSE_NAS_REQUEST 10
173 #define RADIUS_ACCT_TERMINATE_CAUSE_NAS_REBOOT 11
174 #define RADIUS_ACCT_TERMINATE_CAUSE_PORT_UNNEEDED 12
175 #define RADIUS_ACCT_TERMINATE_CAUSE_PORT_PREEMPTED 13
176 #define RADIUS_ACCT_TERMINATE_CAUSE_PORT_SUSPENDED 14
177 #define RADIUS_ACCT_TERMINATE_CAUSE_SERVICE_UNAVAILABLE 15
178 #define RADIUS_ACCT_TERMINATE_CAUSE_CALLBACK 16
179 #define RADIUS_ACCT_TERMINATE_CAUSE_USER_ERROR 17
180 #define RADIUS_ACCT_TERMINATE_CAUSE_HOST_REQUEST 18
181 
182 #define RADIUS_TUNNEL_TAGS 32
183 
184 /* Tunnel-Type */
185 #define RADIUS_TUNNEL_TYPE_PPTP 1
186 #define RADIUS_TUNNEL_TYPE_L2TP 3
187 #define RADIUS_TUNNEL_TYPE_IPIP 7
188 #define RADIUS_TUNNEL_TYPE_GRE 10
189 #define RADIUS_TUNNEL_TYPE_VLAN 13
190 
191 /* Tunnel-Medium-Type */
192 #define RADIUS_TUNNEL_MEDIUM_TYPE_IPV4 1
193 #define RADIUS_TUNNEL_MEDIUM_TYPE_IPV6 2
194 #define RADIUS_TUNNEL_MEDIUM_TYPE_802 6
195 
196 
197 struct radius_attr_vendor {
198 	u8 vendor_type;
199 	u8 vendor_length;
200 } STRUCT_PACKED;
201 
202 #define RADIUS_VENDOR_ID_CISCO 9
203 #define RADIUS_CISCO_AV_PAIR 1
204 
205 /* RFC 2548 - Microsoft Vendor-specific RADIUS Attributes */
206 #define RADIUS_VENDOR_ID_MICROSOFT 311
207 
208 enum { RADIUS_VENDOR_ATTR_MS_MPPE_SEND_KEY = 16,
209        RADIUS_VENDOR_ATTR_MS_MPPE_RECV_KEY = 17
210 };
211 
212 /* FreeRADIUS vendor-specific attributes */
213 #define RADIUS_VENDOR_ID_FREERADIUS 11344
214 /* Extended-Vendor-Specific-5 (245.26; long extended header) */
215 enum {
216 	RADIUS_VENDOR_ATTR_FREERADIUS_802_1X_ANONCE = 1,
217 	RADIUS_VENDOR_ATTR_FREERADIUS_802_1X_EAPOL_KEY_MSG = 2,
218 };
219 
220 /* Hotspot 2.0 - WFA Vendor-specific RADIUS Attributes */
221 #define RADIUS_VENDOR_ID_WFA 40808
222 
223 enum {
224 	RADIUS_VENDOR_ATTR_WFA_HS20_SUBSCR_REMEDIATION = 1,
225 	RADIUS_VENDOR_ATTR_WFA_HS20_AP_VERSION = 2,
226 	RADIUS_VENDOR_ATTR_WFA_HS20_STA_VERSION = 3,
227 	RADIUS_VENDOR_ATTR_WFA_HS20_DEAUTH_REQ = 4,
228 	RADIUS_VENDOR_ATTR_WFA_HS20_SESSION_INFO_URL = 5,
229 	RADIUS_VENDOR_ATTR_WFA_HS20_ROAMING_CONSORTIUM = 6,
230 	RADIUS_VENDOR_ATTR_WFA_HS20_T_C_FILENAME = 7,
231 	RADIUS_VENDOR_ATTR_WFA_HS20_TIMESTAMP = 8,
232 	RADIUS_VENDOR_ATTR_WFA_HS20_T_C_FILTERING = 9,
233 	RADIUS_VENDOR_ATTR_WFA_HS20_T_C_URL = 10,
234 };
235 
236 #ifdef _MSC_VER
237 #pragma pack(pop)
238 #endif /* _MSC_VER */
239 
240 struct radius_ms_mppe_keys {
241 	u8 *send;
242 	size_t send_len;
243 	u8 *recv;
244 	size_t recv_len;
245 };
246 
247 
248 struct radius_msg;
249 
250 /* Default size to be allocated for new RADIUS messages */
251 #define RADIUS_DEFAULT_MSG_SIZE 1024
252 
253 /* Default size to be allocated for attribute array */
254 #define RADIUS_DEFAULT_ATTR_COUNT 16
255 
256 /* Maximum message length for incoming RADIUS messages, as stated in RFC 2865
257  * Section 3 ("Packet Format").*/
258 #define RADIUS_MAX_MSG_LEN 4096
259 
260 /* MAC address ASCII format for IEEE 802.1X use
261  * (draft-congdon-radius-8021x-20.txt) */
262 #define RADIUS_802_1X_ADDR_FORMAT "%02X-%02X-%02X-%02X-%02X-%02X"
263 /* MAC address ASCII format for non-802.1X use */
264 #define RADIUS_ADDR_FORMAT "%02x%02x%02x%02x%02x%02x"
265 
266 struct radius_hdr * radius_msg_get_hdr(struct radius_msg *msg);
267 struct wpabuf * radius_msg_get_buf(struct radius_msg *msg);
268 struct radius_msg * radius_msg_new(u8 code, u8 identifier);
269 void radius_msg_free(struct radius_msg *msg);
270 void radius_msg_dump(struct radius_msg *msg);
271 int radius_msg_finish(struct radius_msg *msg, const u8 *secret,
272 		      size_t secret_len);
273 int radius_msg_finish_srv(struct radius_msg *msg, const u8 *secret,
274 			  size_t secret_len, const u8 *req_authenticator);
275 int radius_msg_finish_das_resp(struct radius_msg *msg, const u8 *secret,
276 			       size_t secret_len,
277 			       const struct radius_hdr *req_hdr);
278 void radius_msg_finish_acct(struct radius_msg *msg, const u8 *secret,
279 			    size_t secret_len);
280 void radius_msg_finish_acct_resp(struct radius_msg *msg, const u8 *secret,
281 				 size_t secret_len,
282 				 const u8 *req_authenticator);
283 int radius_msg_verify_acct_req(struct radius_msg *msg, const u8 *secret,
284 			       size_t secret_len);
285 int radius_msg_verify_das_req(struct radius_msg *msg, const u8 *secret,
286 			      size_t secret_len,
287 			      int require_message_authenticator);
288 struct radius_attr_hdr * radius_msg_add_attr(struct radius_msg *msg, u16 type,
289 					     const u8 *data, size_t data_len);
290 struct radius_msg * radius_msg_parse(const u8 *data, size_t len);
291 int radius_msg_add_eap(struct radius_msg *msg, const u8 *data,
292 		       size_t data_len);
293 struct wpabuf * radius_msg_get_eap(struct radius_msg *msg);
294 int radius_msg_verify(struct radius_msg *msg, const u8 *secret,
295 		      size_t secret_len, struct radius_msg *sent_msg,
296 		      int auth);
297 int radius_msg_verify_msg_auth(struct radius_msg *msg, const u8 *secret,
298 			       size_t secret_len, const u8 *req_auth);
299 int radius_msg_copy_attr(struct radius_msg *dst, struct radius_msg *src,
300 			 u8 type);
301 int radius_msg_make_authenticator(struct radius_msg *msg);
302 struct radius_ms_mppe_keys *
303 radius_msg_get_ms_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
304 		       const u8 *secret, size_t secret_len);
305 struct radius_ms_mppe_keys *
306 radius_msg_get_cisco_keys(struct radius_msg *msg, struct radius_msg *sent_msg,
307 			  const u8 *secret, size_t secret_len);
308 int radius_msg_add_mppe_keys(struct radius_msg *msg,
309 			     const u8 *req_authenticator,
310 			     const u8 *secret, size_t secret_len,
311 			     const u8 *send_key, size_t send_key_len,
312 			     const u8 *recv_key, size_t recv_key_len);
313 int radius_msg_add_wfa(struct radius_msg *msg, u8 subtype, const u8 *data,
314 		       size_t len);
315 int radius_msg_add_ext_vs(struct radius_msg *msg, u16 type, u32 vendor_id,
316 			  u8 vendor_type, const u8 *data, size_t len);
317 int radius_user_password_hide(struct radius_msg *msg,
318 			      const u8 *data, size_t data_len,
319 			      const u8 *secret, size_t secret_len,
320 			      u8 *buf, size_t buf_len);
321 struct radius_attr_hdr *
322 radius_msg_add_attr_user_password(struct radius_msg *msg,
323 				  const u8 *data, size_t data_len,
324 				  const u8 *secret, size_t secret_len);
325 int radius_msg_get_attr(struct radius_msg *msg, u8 type, u8 *buf, size_t len);
326 int radius_msg_get_vlanid(struct radius_msg *msg, int *untagged, int numtagged,
327 			  int *tagged);
328 char * radius_msg_get_tunnel_password(struct radius_msg *msg, int *keylen,
329 				      const u8 *secret, size_t secret_len,
330 				      struct radius_msg *sent_msg, size_t n);
331 
radius_msg_add_attr_int32(struct radius_msg * msg,u8 type,u32 value)332 static inline int radius_msg_add_attr_int32(struct radius_msg *msg, u8 type,
333 					    u32 value)
334 {
335 	u32 val = htonl(value);
336 	return radius_msg_add_attr(msg, type, (u8 *) &val, 4) != NULL;
337 }
338 
radius_msg_get_attr_int32(struct radius_msg * msg,u8 type,u32 * value)339 static inline int radius_msg_get_attr_int32(struct radius_msg *msg, u8 type,
340 					    u32 *value)
341 {
342 	u32 val;
343 	int res;
344 	res = radius_msg_get_attr(msg, type, (u8 *) &val, 4);
345 	if (res != 4)
346 		return -1;
347 
348 	*value = ntohl(val);
349 	return 0;
350 }
351 int radius_msg_get_attr_ptr(struct radius_msg *msg, u8 type, u8 **buf,
352 			    size_t *len, const u8 *start);
353 int radius_msg_count_attr(struct radius_msg *msg, u8 type, int min_len);
354 
355 
356 struct radius_attr_data {
357 	u8 *data;
358 	size_t len;
359 };
360 
361 struct radius_class_data {
362 	struct radius_attr_data *attr;
363 	size_t count;
364 };
365 
366 void radius_free_class(struct radius_class_data *c);
367 int radius_copy_class(struct radius_class_data *dst,
368 		      const struct radius_class_data *src);
369 
370 u8 radius_msg_find_unlisted_attr(struct radius_msg *msg, u8 *attrs);
371 
372 int radius_gen_session_id(u8 *id, size_t len);
373 
374 #endif /* RADIUS_H */
375