2 * hostapd / EAP-MSCHAPv2 (draft-kamath-pppext-eap-mschapv2-00.txt) server
3 * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
22 struct eap_mschapv2_hdr
{
23 u8 op_code
; /* MSCHAPV2_OP_* */
24 u8 mschapv2_id
; /* must be changed for challenges, but not for
26 u8 ms_length
[2]; /* Note: misaligned; length - 5 */
27 /* followed by data */
30 #define MSCHAPV2_OP_CHALLENGE 1
31 #define MSCHAPV2_OP_RESPONSE 2
32 #define MSCHAPV2_OP_SUCCESS 3
33 #define MSCHAPV2_OP_FAILURE 4
34 #define MSCHAPV2_OP_CHANGE_PASSWORD 7
36 #define MSCHAPV2_RESP_LEN 49
38 #define ERROR_RESTRICTED_LOGON_HOURS 646
39 #define ERROR_ACCT_DISABLED 647
40 #define ERROR_PASSWD_EXPIRED 648
41 #define ERROR_NO_DIALIN_PERMISSION 649
42 #define ERROR_AUTHENTICATION_FAILURE 691
43 #define ERROR_CHANGING_PASSWORD 709
45 #define PASSWD_CHANGE_CHAL_LEN 16
46 #define MSCHAPV2_KEY_LEN 16
49 #define CHALLENGE_LEN 16
51 struct eap_mschapv2_data
{
52 u8 auth_challenge
[CHALLENGE_LEN
];
53 int auth_challenge_from_tls
;
56 enum { CHALLENGE
, SUCCESS_REQ
, FAILURE_REQ
, SUCCESS
, FAILURE
} state
;
63 static void * eap_mschapv2_init(struct eap_sm
*sm
)
65 struct eap_mschapv2_data
*data
;
67 data
= os_zalloc(sizeof(*data
));
70 data
->state
= CHALLENGE
;
72 if (sm
->auth_challenge
) {
73 os_memcpy(data
->auth_challenge
, sm
->auth_challenge
,
75 data
->auth_challenge_from_tls
= 1;
78 if (sm
->peer_challenge
) {
79 data
->peer_challenge
= os_malloc(CHALLENGE_LEN
);
80 if (data
->peer_challenge
== NULL
) {
84 os_memcpy(data
->peer_challenge
, sm
->peer_challenge
,
92 static void eap_mschapv2_reset(struct eap_sm
*sm
, void *priv
)
94 struct eap_mschapv2_data
*data
= priv
;
98 os_free(data
->peer_challenge
);
103 static struct wpabuf
* eap_mschapv2_build_challenge(
104 struct eap_sm
*sm
, struct eap_mschapv2_data
*data
, u8 id
)
107 struct eap_mschapv2_hdr
*ms
;
108 char *name
= "hostapd"; /* TODO: make this configurable */
111 if (!data
->auth_challenge_from_tls
&&
112 os_get_random(data
->auth_challenge
, CHALLENGE_LEN
)) {
113 wpa_printf(MSG_ERROR
, "EAP-MSCHAPV2: Failed to get random "
115 data
->state
= FAILURE
;
119 ms_len
= sizeof(*ms
) + 1 + CHALLENGE_LEN
+ os_strlen(name
);
120 req
= eap_msg_alloc(EAP_VENDOR_IETF
, EAP_TYPE_MSCHAPV2
, ms_len
,
121 EAP_CODE_REQUEST
, id
);
123 wpa_printf(MSG_ERROR
, "EAP-MSCHAPV2: Failed to allocate memory"
125 data
->state
= FAILURE
;
129 ms
= wpabuf_put(req
, sizeof(*ms
));
130 ms
->op_code
= MSCHAPV2_OP_CHALLENGE
;
131 ms
->mschapv2_id
= id
;
132 WPA_PUT_BE16(ms
->ms_length
, ms_len
);
134 wpabuf_put_u8(req
, CHALLENGE_LEN
);
135 if (!data
->auth_challenge_from_tls
)
136 wpabuf_put_data(req
, data
->auth_challenge
, CHALLENGE_LEN
);
138 wpabuf_put(req
, CHALLENGE_LEN
);
139 wpa_hexdump(MSG_MSGDUMP
, "EAP-MSCHAPV2: Challenge",
140 data
->auth_challenge
, CHALLENGE_LEN
);
141 wpabuf_put_data(req
, name
, os_strlen(name
));
147 static struct wpabuf
* eap_mschapv2_build_success_req(
148 struct eap_sm
*sm
, struct eap_mschapv2_data
*data
, u8 id
)
151 struct eap_mschapv2_hdr
*ms
;
153 char *message
= "OK";
156 ms_len
= sizeof(*ms
) + 2 + 2 * sizeof(data
->auth_response
) + 1 + 2 +
158 req
= eap_msg_alloc(EAP_VENDOR_IETF
, EAP_TYPE_MSCHAPV2
, ms_len
,
159 EAP_CODE_REQUEST
, id
);
161 wpa_printf(MSG_ERROR
, "EAP-MSCHAPV2: Failed to allocate memory"
163 data
->state
= FAILURE
;
167 ms
= wpabuf_put(req
, sizeof(*ms
));
168 ms
->op_code
= MSCHAPV2_OP_SUCCESS
;
169 ms
->mschapv2_id
= data
->resp_mschapv2_id
;
170 WPA_PUT_BE16(ms
->ms_length
, ms_len
);
171 msg
= (u8
*) (ms
+ 1);
173 wpabuf_put_u8(req
, 'S');
174 wpabuf_put_u8(req
, '=');
175 wpa_snprintf_hex_uppercase(
176 wpabuf_put(req
, sizeof(data
->auth_response
) * 2),
177 sizeof(data
->auth_response
) * 2 + 1,
178 data
->auth_response
, sizeof(data
->auth_response
));
179 wpabuf_put_u8(req
, ' ');
180 wpabuf_put_u8(req
, 'M');
181 wpabuf_put_u8(req
, '=');
182 wpabuf_put_data(req
, message
, os_strlen(message
));
184 wpa_hexdump_ascii(MSG_MSGDUMP
, "EAP-MSCHAPV2: Success Request Message",
185 msg
, ms_len
- sizeof(*ms
));
191 static struct wpabuf
* eap_mschapv2_build_failure_req(
192 struct eap_sm
*sm
, struct eap_mschapv2_data
*data
, u8 id
)
195 struct eap_mschapv2_hdr
*ms
;
196 char *message
= "E=691 R=0 C=00000000000000000000000000000000 V=3 "
200 ms_len
= sizeof(*ms
) + os_strlen(message
);
201 req
= eap_msg_alloc(EAP_VENDOR_IETF
, EAP_TYPE_MSCHAPV2
, ms_len
,
202 EAP_CODE_REQUEST
, id
);
204 wpa_printf(MSG_ERROR
, "EAP-MSCHAPV2: Failed to allocate memory"
206 data
->state
= FAILURE
;
210 ms
= wpabuf_put(req
, sizeof(*ms
));
211 ms
->op_code
= MSCHAPV2_OP_FAILURE
;
212 ms
->mschapv2_id
= data
->resp_mschapv2_id
;
213 WPA_PUT_BE16(ms
->ms_length
, ms_len
);
215 wpabuf_put_data(req
, message
, os_strlen(message
));
217 wpa_hexdump_ascii(MSG_MSGDUMP
, "EAP-MSCHAPV2: Failure Request Message",
218 (u8
*) message
, os_strlen(message
));
224 static struct wpabuf
* eap_mschapv2_buildReq(struct eap_sm
*sm
, void *priv
,
227 struct eap_mschapv2_data
*data
= priv
;
229 switch (data
->state
) {
231 return eap_mschapv2_build_challenge(sm
, data
, id
);
233 return eap_mschapv2_build_success_req(sm
, data
, id
);
235 return eap_mschapv2_build_failure_req(sm
, data
, id
);
237 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Unknown state %d in "
238 "buildReq", data
->state
);
245 static Boolean
eap_mschapv2_check(struct eap_sm
*sm
, void *priv
,
246 struct wpabuf
*respData
)
248 struct eap_mschapv2_data
*data
= priv
;
249 struct eap_mschapv2_hdr
*resp
;
253 pos
= eap_hdr_validate(EAP_VENDOR_IETF
, EAP_TYPE_MSCHAPV2
, respData
,
255 if (pos
== NULL
|| len
< 1) {
256 wpa_printf(MSG_INFO
, "EAP-MSCHAPV2: Invalid frame");
260 resp
= (struct eap_mschapv2_hdr
*) pos
;
261 if (data
->state
== CHALLENGE
&&
262 resp
->op_code
!= MSCHAPV2_OP_RESPONSE
) {
263 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Expected Response - "
264 "ignore op %d", resp
->op_code
);
268 if (data
->state
== SUCCESS_REQ
&&
269 resp
->op_code
!= MSCHAPV2_OP_SUCCESS
&&
270 resp
->op_code
!= MSCHAPV2_OP_FAILURE
) {
271 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Expected Success or "
272 "Failure - ignore op %d", resp
->op_code
);
276 if (data
->state
== FAILURE_REQ
&&
277 resp
->op_code
!= MSCHAPV2_OP_FAILURE
) {
278 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Expected Failure "
279 "- ignore op %d", resp
->op_code
);
287 static void eap_mschapv2_process_response(struct eap_sm
*sm
,
288 struct eap_mschapv2_data
*data
,
289 struct wpabuf
*respData
)
291 struct eap_mschapv2_hdr
*resp
;
292 const u8
*pos
, *end
, *peer_challenge
, *nt_response
, *name
;
294 size_t len
, name_len
, i
;
296 const u8
*username
, *user
;
297 size_t username_len
, user_len
;
299 pos
= eap_hdr_validate(EAP_VENDOR_IETF
, EAP_TYPE_MSCHAPV2
, respData
,
301 if (pos
== NULL
|| len
< 1)
302 return; /* Should not happen - frame already validated */
305 resp
= (struct eap_mschapv2_hdr
*) pos
;
306 pos
= (u8
*) (resp
+ 1);
308 if (len
< sizeof(*resp
) + 1 + 49 ||
309 resp
->op_code
!= MSCHAPV2_OP_RESPONSE
||
311 wpa_hexdump_buf(MSG_DEBUG
, "EAP-MSCHAPV2: Invalid response",
313 data
->state
= FAILURE
;
316 data
->resp_mschapv2_id
= resp
->mschapv2_id
;
318 peer_challenge
= pos
;
324 name_len
= end
- name
;
326 if (data
->peer_challenge
) {
327 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Using pre-configured "
329 peer_challenge
= data
->peer_challenge
;
331 wpa_hexdump(MSG_MSGDUMP
, "EAP-MSCHAPV2: Peer-Challenge",
333 wpa_hexdump(MSG_MSGDUMP
, "EAP-MSCHAPV2: NT-Response", nt_response
, 24);
334 wpa_printf(MSG_MSGDUMP
, "EAP-MSCHAPV2: Flags 0x%x", flags
);
335 wpa_hexdump_ascii(MSG_MSGDUMP
, "EAP-MSCHAPV2: Name", name
, name_len
);
337 /* MSCHAPv2 does not include optional domain name in the
338 * challenge-response calculation, so remove domain prefix
340 username
= sm
->identity
;
341 username_len
= sm
->identity_len
;
342 for (i
= 0; i
< username_len
; i
++) {
343 if (username
[i
] == '\\') {
344 username_len
-= i
+ 1;
352 for (i
= 0; i
< user_len
; i
++) {
353 if (user
[i
] == '\\') {
360 if (username_len
!= user_len
||
361 os_memcmp(username
, user
, username_len
) != 0) {
362 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Mismatch in user names");
363 wpa_hexdump_ascii(MSG_DEBUG
, "EAP-MSCHAPV2: Expected user "
364 "name", username
, username_len
);
365 wpa_hexdump_ascii(MSG_DEBUG
, "EAP-MSCHAPV2: Received user "
366 "name", user
, user_len
);
367 data
->state
= FAILURE
;
371 wpa_hexdump_ascii(MSG_MSGDUMP
, "EAP-MSCHAPV2: User name",
372 username
, username_len
);
374 if (sm
->user
->password_hash
) {
375 generate_nt_response_pwhash(data
->auth_challenge
,
377 username
, username_len
,
381 generate_nt_response(data
->auth_challenge
, peer_challenge
,
382 username
, username_len
,
384 sm
->user
->password_len
,
388 if (os_memcmp(nt_response
, expected
, 24) == 0) {
390 u8 pw_hash_buf
[16], pw_hash_hash
[16];
392 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Correct NT-Response");
393 data
->state
= SUCCESS_REQ
;
395 /* Authenticator response is not really needed yet, but
396 * calculate it here so that peer_challenge and username need
398 if (sm
->user
->password_hash
) {
399 pw_hash
= sm
->user
->password
;
401 nt_password_hash(sm
->user
->password
,
402 sm
->user
->password_len
,
404 pw_hash
= pw_hash_buf
;
406 generate_authenticator_response_pwhash(
407 pw_hash
, peer_challenge
, data
->auth_challenge
,
408 username
, username_len
, nt_response
,
409 data
->auth_response
);
411 hash_nt_password_hash(pw_hash
, pw_hash_hash
);
412 get_master_key(pw_hash_hash
, nt_response
, data
->master_key
);
413 data
->master_key_valid
= 1;
414 wpa_hexdump_key(MSG_DEBUG
, "EAP-MSCHAPV2: Derived Master Key",
415 data
->master_key
, MSCHAPV2_KEY_LEN
);
417 wpa_hexdump(MSG_MSGDUMP
, "EAP-MSCHAPV2: Expected NT-Response",
419 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Invalid NT-Response");
420 data
->state
= FAILURE_REQ
;
425 static void eap_mschapv2_process_success_resp(struct eap_sm
*sm
,
426 struct eap_mschapv2_data
*data
,
427 struct wpabuf
*respData
)
429 struct eap_mschapv2_hdr
*resp
;
433 pos
= eap_hdr_validate(EAP_VENDOR_IETF
, EAP_TYPE_MSCHAPV2
, respData
,
435 if (pos
== NULL
|| len
< 1)
436 return; /* Should not happen - frame already validated */
438 resp
= (struct eap_mschapv2_hdr
*) pos
;
440 if (resp
->op_code
== MSCHAPV2_OP_SUCCESS
) {
441 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Received Success Response"
442 " - authentication completed successfully");
443 data
->state
= SUCCESS
;
445 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Did not receive Success "
446 "Response - peer rejected authentication");
447 data
->state
= FAILURE
;
452 static void eap_mschapv2_process_failure_resp(struct eap_sm
*sm
,
453 struct eap_mschapv2_data
*data
,
454 struct wpabuf
*respData
)
456 struct eap_mschapv2_hdr
*resp
;
460 pos
= eap_hdr_validate(EAP_VENDOR_IETF
, EAP_TYPE_MSCHAPV2
, respData
,
462 if (pos
== NULL
|| len
< 1)
463 return; /* Should not happen - frame already validated */
465 resp
= (struct eap_mschapv2_hdr
*) pos
;
467 if (resp
->op_code
== MSCHAPV2_OP_FAILURE
) {
468 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Received Failure Response"
469 " - authentication failed");
471 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Did not receive Failure "
472 "Response - authentication failed");
475 data
->state
= FAILURE
;
479 static void eap_mschapv2_process(struct eap_sm
*sm
, void *priv
,
480 struct wpabuf
*respData
)
482 struct eap_mschapv2_data
*data
= priv
;
484 if (sm
->user
== NULL
|| sm
->user
->password
== NULL
) {
485 wpa_printf(MSG_INFO
, "EAP-MSCHAPV2: Password not configured");
486 data
->state
= FAILURE
;
490 switch (data
->state
) {
492 eap_mschapv2_process_response(sm
, data
, respData
);
495 eap_mschapv2_process_success_resp(sm
, data
, respData
);
498 eap_mschapv2_process_failure_resp(sm
, data
, respData
);
501 wpa_printf(MSG_DEBUG
, "EAP-MSCHAPV2: Unknown state %d in "
502 "process", data
->state
);
508 static Boolean
eap_mschapv2_isDone(struct eap_sm
*sm
, void *priv
)
510 struct eap_mschapv2_data
*data
= priv
;
511 return data
->state
== SUCCESS
|| data
->state
== FAILURE
;
515 static u8
* eap_mschapv2_getKey(struct eap_sm
*sm
, void *priv
, size_t *len
)
517 struct eap_mschapv2_data
*data
= priv
;
520 if (data
->state
!= SUCCESS
|| !data
->master_key_valid
)
523 *len
= 2 * MSCHAPV2_KEY_LEN
;
524 key
= os_malloc(*len
);
527 get_asymetric_start_key(data
->master_key
, key
, MSCHAPV2_KEY_LEN
, 0, 0);
528 get_asymetric_start_key(data
->master_key
, key
+ MSCHAPV2_KEY_LEN
,
529 MSCHAPV2_KEY_LEN
, 1, 0);
530 wpa_hexdump_key(MSG_DEBUG
, "EAP-MSCHAPV2: Derived key", key
, *len
);
536 static Boolean
eap_mschapv2_isSuccess(struct eap_sm
*sm
, void *priv
)
538 struct eap_mschapv2_data
*data
= priv
;
539 return data
->state
== SUCCESS
;
543 int eap_server_mschapv2_register(void)
545 struct eap_method
*eap
;
548 eap
= eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION
,
549 EAP_VENDOR_IETF
, EAP_TYPE_MSCHAPV2
,
554 eap
->init
= eap_mschapv2_init
;
555 eap
->reset
= eap_mschapv2_reset
;
556 eap
->buildReq
= eap_mschapv2_buildReq
;
557 eap
->check
= eap_mschapv2_check
;
558 eap
->process
= eap_mschapv2_process
;
559 eap
->isDone
= eap_mschapv2_isDone
;
560 eap
->getKey
= eap_mschapv2_getKey
;
561 eap
->isSuccess
= eap_mschapv2_isSuccess
;
563 ret
= eap_server_method_register(eap
);
565 eap_server_method_free(eap
);