eap_server_mschapv2.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. /*
  2. * hostapd / EAP-MSCHAPv2 (draft-kamath-pppext-eap-mschapv2-00.txt) server
  3. * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
  4. *
  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.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #include "includes.h"
  15. #include "common.h"
  16. #include "crypto/ms_funcs.h"
  17. #include "crypto/random.h"
  18. #include "eap_i.h"
  19. struct eap_mschapv2_hdr {
  20. u8 op_code; /* MSCHAPV2_OP_* */
  21. u8 mschapv2_id; /* must be changed for challenges, but not for
  22. * success/failure */
  23. u8 ms_length[2]; /* Note: misaligned; length - 5 */
  24. /* followed by data */
  25. } STRUCT_PACKED;
  26. #define MSCHAPV2_OP_CHALLENGE 1
  27. #define MSCHAPV2_OP_RESPONSE 2
  28. #define MSCHAPV2_OP_SUCCESS 3
  29. #define MSCHAPV2_OP_FAILURE 4
  30. #define MSCHAPV2_OP_CHANGE_PASSWORD 7
  31. #define MSCHAPV2_RESP_LEN 49
  32. #define ERROR_RESTRICTED_LOGON_HOURS 646
  33. #define ERROR_ACCT_DISABLED 647
  34. #define ERROR_PASSWD_EXPIRED 648
  35. #define ERROR_NO_DIALIN_PERMISSION 649
  36. #define ERROR_AUTHENTICATION_FAILURE 691
  37. #define ERROR_CHANGING_PASSWORD 709
  38. #define PASSWD_CHANGE_CHAL_LEN 16
  39. #define MSCHAPV2_KEY_LEN 16
  40. #define CHALLENGE_LEN 16
  41. struct eap_mschapv2_data {
  42. u8 auth_challenge[CHALLENGE_LEN];
  43. int auth_challenge_from_tls;
  44. u8 *peer_challenge;
  45. u8 auth_response[20];
  46. enum { CHALLENGE, SUCCESS_REQ, FAILURE_REQ, SUCCESS, FAILURE } state;
  47. u8 resp_mschapv2_id;
  48. u8 master_key[16];
  49. int master_key_valid;
  50. };
  51. static void * eap_mschapv2_init(struct eap_sm *sm)
  52. {
  53. struct eap_mschapv2_data *data;
  54. data = os_zalloc(sizeof(*data));
  55. if (data == NULL)
  56. return NULL;
  57. data->state = CHALLENGE;
  58. if (sm->auth_challenge) {
  59. os_memcpy(data->auth_challenge, sm->auth_challenge,
  60. CHALLENGE_LEN);
  61. data->auth_challenge_from_tls = 1;
  62. }
  63. if (sm->peer_challenge) {
  64. data->peer_challenge = os_malloc(CHALLENGE_LEN);
  65. if (data->peer_challenge == NULL) {
  66. os_free(data);
  67. return NULL;
  68. }
  69. os_memcpy(data->peer_challenge, sm->peer_challenge,
  70. CHALLENGE_LEN);
  71. }
  72. return data;
  73. }
  74. static void eap_mschapv2_reset(struct eap_sm *sm, void *priv)
  75. {
  76. struct eap_mschapv2_data *data = priv;
  77. if (data == NULL)
  78. return;
  79. os_free(data->peer_challenge);
  80. os_free(data);
  81. }
  82. static struct wpabuf * eap_mschapv2_build_challenge(
  83. struct eap_sm *sm, struct eap_mschapv2_data *data, u8 id)
  84. {
  85. struct wpabuf *req;
  86. struct eap_mschapv2_hdr *ms;
  87. char *name = "hostapd"; /* TODO: make this configurable */
  88. size_t ms_len;
  89. if (!data->auth_challenge_from_tls &&
  90. random_get_bytes(data->auth_challenge, CHALLENGE_LEN)) {
  91. wpa_printf(MSG_ERROR, "EAP-MSCHAPV2: Failed to get random "
  92. "data");
  93. data->state = FAILURE;
  94. return NULL;
  95. }
  96. ms_len = sizeof(*ms) + 1 + CHALLENGE_LEN + os_strlen(name);
  97. req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, ms_len,
  98. EAP_CODE_REQUEST, id);
  99. if (req == NULL) {
  100. wpa_printf(MSG_ERROR, "EAP-MSCHAPV2: Failed to allocate memory"
  101. " for request");
  102. data->state = FAILURE;
  103. return NULL;
  104. }
  105. ms = wpabuf_put(req, sizeof(*ms));
  106. ms->op_code = MSCHAPV2_OP_CHALLENGE;
  107. ms->mschapv2_id = id;
  108. WPA_PUT_BE16(ms->ms_length, ms_len);
  109. wpabuf_put_u8(req, CHALLENGE_LEN);
  110. if (!data->auth_challenge_from_tls)
  111. wpabuf_put_data(req, data->auth_challenge, CHALLENGE_LEN);
  112. else
  113. wpabuf_put(req, CHALLENGE_LEN);
  114. wpa_hexdump(MSG_MSGDUMP, "EAP-MSCHAPV2: Challenge",
  115. data->auth_challenge, CHALLENGE_LEN);
  116. wpabuf_put_data(req, name, os_strlen(name));
  117. return req;
  118. }
  119. static struct wpabuf * eap_mschapv2_build_success_req(
  120. struct eap_sm *sm, struct eap_mschapv2_data *data, u8 id)
  121. {
  122. struct wpabuf *req;
  123. struct eap_mschapv2_hdr *ms;
  124. u8 *msg;
  125. char *message = "OK";
  126. size_t ms_len;
  127. ms_len = sizeof(*ms) + 2 + 2 * sizeof(data->auth_response) + 1 + 2 +
  128. os_strlen(message);
  129. req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, ms_len,
  130. EAP_CODE_REQUEST, id);
  131. if (req == NULL) {
  132. wpa_printf(MSG_ERROR, "EAP-MSCHAPV2: Failed to allocate memory"
  133. " for request");
  134. data->state = FAILURE;
  135. return NULL;
  136. }
  137. ms = wpabuf_put(req, sizeof(*ms));
  138. ms->op_code = MSCHAPV2_OP_SUCCESS;
  139. ms->mschapv2_id = data->resp_mschapv2_id;
  140. WPA_PUT_BE16(ms->ms_length, ms_len);
  141. msg = (u8 *) (ms + 1);
  142. wpabuf_put_u8(req, 'S');
  143. wpabuf_put_u8(req, '=');
  144. wpa_snprintf_hex_uppercase(
  145. wpabuf_put(req, sizeof(data->auth_response) * 2),
  146. sizeof(data->auth_response) * 2 + 1,
  147. data->auth_response, sizeof(data->auth_response));
  148. wpabuf_put_u8(req, ' ');
  149. wpabuf_put_u8(req, 'M');
  150. wpabuf_put_u8(req, '=');
  151. wpabuf_put_data(req, message, os_strlen(message));
  152. wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-MSCHAPV2: Success Request Message",
  153. msg, ms_len - sizeof(*ms));
  154. return req;
  155. }
  156. static struct wpabuf * eap_mschapv2_build_failure_req(
  157. struct eap_sm *sm, struct eap_mschapv2_data *data, u8 id)
  158. {
  159. struct wpabuf *req;
  160. struct eap_mschapv2_hdr *ms;
  161. char *message = "E=691 R=0 C=00000000000000000000000000000000 V=3 "
  162. "M=FAILED";
  163. size_t ms_len;
  164. ms_len = sizeof(*ms) + os_strlen(message);
  165. req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, ms_len,
  166. EAP_CODE_REQUEST, id);
  167. if (req == NULL) {
  168. wpa_printf(MSG_ERROR, "EAP-MSCHAPV2: Failed to allocate memory"
  169. " for request");
  170. data->state = FAILURE;
  171. return NULL;
  172. }
  173. ms = wpabuf_put(req, sizeof(*ms));
  174. ms->op_code = MSCHAPV2_OP_FAILURE;
  175. ms->mschapv2_id = data->resp_mschapv2_id;
  176. WPA_PUT_BE16(ms->ms_length, ms_len);
  177. wpabuf_put_data(req, message, os_strlen(message));
  178. wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-MSCHAPV2: Failure Request Message",
  179. (u8 *) message, os_strlen(message));
  180. return req;
  181. }
  182. static struct wpabuf * eap_mschapv2_buildReq(struct eap_sm *sm, void *priv,
  183. u8 id)
  184. {
  185. struct eap_mschapv2_data *data = priv;
  186. switch (data->state) {
  187. case CHALLENGE:
  188. return eap_mschapv2_build_challenge(sm, data, id);
  189. case SUCCESS_REQ:
  190. return eap_mschapv2_build_success_req(sm, data, id);
  191. case FAILURE_REQ:
  192. return eap_mschapv2_build_failure_req(sm, data, id);
  193. default:
  194. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Unknown state %d in "
  195. "buildReq", data->state);
  196. break;
  197. }
  198. return NULL;
  199. }
  200. static Boolean eap_mschapv2_check(struct eap_sm *sm, void *priv,
  201. struct wpabuf *respData)
  202. {
  203. struct eap_mschapv2_data *data = priv;
  204. struct eap_mschapv2_hdr *resp;
  205. const u8 *pos;
  206. size_t len;
  207. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, respData,
  208. &len);
  209. if (pos == NULL || len < 1) {
  210. wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Invalid frame");
  211. return TRUE;
  212. }
  213. resp = (struct eap_mschapv2_hdr *) pos;
  214. if (data->state == CHALLENGE &&
  215. resp->op_code != MSCHAPV2_OP_RESPONSE) {
  216. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Expected Response - "
  217. "ignore op %d", resp->op_code);
  218. return TRUE;
  219. }
  220. if (data->state == SUCCESS_REQ &&
  221. resp->op_code != MSCHAPV2_OP_SUCCESS &&
  222. resp->op_code != MSCHAPV2_OP_FAILURE) {
  223. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Expected Success or "
  224. "Failure - ignore op %d", resp->op_code);
  225. return TRUE;
  226. }
  227. if (data->state == FAILURE_REQ &&
  228. resp->op_code != MSCHAPV2_OP_FAILURE) {
  229. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Expected Failure "
  230. "- ignore op %d", resp->op_code);
  231. return TRUE;
  232. }
  233. return FALSE;
  234. }
  235. static void eap_mschapv2_process_response(struct eap_sm *sm,
  236. struct eap_mschapv2_data *data,
  237. struct wpabuf *respData)
  238. {
  239. struct eap_mschapv2_hdr *resp;
  240. const u8 *pos, *end, *peer_challenge, *nt_response, *name;
  241. u8 flags;
  242. size_t len, name_len, i;
  243. u8 expected[24];
  244. const u8 *username, *user;
  245. size_t username_len, user_len;
  246. int res;
  247. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, respData,
  248. &len);
  249. if (pos == NULL || len < 1)
  250. return; /* Should not happen - frame already validated */
  251. end = pos + len;
  252. resp = (struct eap_mschapv2_hdr *) pos;
  253. pos = (u8 *) (resp + 1);
  254. if (len < sizeof(*resp) + 1 + 49 ||
  255. resp->op_code != MSCHAPV2_OP_RESPONSE ||
  256. pos[0] != 49) {
  257. wpa_hexdump_buf(MSG_DEBUG, "EAP-MSCHAPV2: Invalid response",
  258. respData);
  259. data->state = FAILURE;
  260. return;
  261. }
  262. data->resp_mschapv2_id = resp->mschapv2_id;
  263. pos++;
  264. peer_challenge = pos;
  265. pos += 16 + 8;
  266. nt_response = pos;
  267. pos += 24;
  268. flags = *pos++;
  269. name = pos;
  270. name_len = end - name;
  271. if (data->peer_challenge) {
  272. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Using pre-configured "
  273. "Peer-Challenge");
  274. peer_challenge = data->peer_challenge;
  275. }
  276. wpa_hexdump(MSG_MSGDUMP, "EAP-MSCHAPV2: Peer-Challenge",
  277. peer_challenge, 16);
  278. wpa_hexdump(MSG_MSGDUMP, "EAP-MSCHAPV2: NT-Response", nt_response, 24);
  279. wpa_printf(MSG_MSGDUMP, "EAP-MSCHAPV2: Flags 0x%x", flags);
  280. wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-MSCHAPV2: Name", name, name_len);
  281. /* MSCHAPv2 does not include optional domain name in the
  282. * challenge-response calculation, so remove domain prefix
  283. * (if present). */
  284. username = sm->identity;
  285. username_len = sm->identity_len;
  286. for (i = 0; i < username_len; i++) {
  287. if (username[i] == '\\') {
  288. username_len -= i + 1;
  289. username += i + 1;
  290. break;
  291. }
  292. }
  293. user = name;
  294. user_len = name_len;
  295. for (i = 0; i < user_len; i++) {
  296. if (user[i] == '\\') {
  297. user_len -= i + 1;
  298. user += i + 1;
  299. break;
  300. }
  301. }
  302. if (username_len != user_len ||
  303. os_memcmp(username, user, username_len) != 0) {
  304. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Mismatch in user names");
  305. wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Expected user "
  306. "name", username, username_len);
  307. wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Received user "
  308. "name", user, user_len);
  309. data->state = FAILURE;
  310. return;
  311. }
  312. wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-MSCHAPV2: User name",
  313. username, username_len);
  314. if (sm->user->password_hash) {
  315. res = generate_nt_response_pwhash(data->auth_challenge,
  316. peer_challenge,
  317. username, username_len,
  318. sm->user->password,
  319. expected);
  320. } else {
  321. res = generate_nt_response(data->auth_challenge,
  322. peer_challenge,
  323. username, username_len,
  324. sm->user->password,
  325. sm->user->password_len,
  326. expected);
  327. }
  328. if (res) {
  329. data->state = FAILURE;
  330. return;
  331. }
  332. if (os_memcmp(nt_response, expected, 24) == 0) {
  333. const u8 *pw_hash;
  334. u8 pw_hash_buf[16], pw_hash_hash[16];
  335. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Correct NT-Response");
  336. data->state = SUCCESS_REQ;
  337. /* Authenticator response is not really needed yet, but
  338. * calculate it here so that peer_challenge and username need
  339. * not be saved. */
  340. if (sm->user->password_hash) {
  341. pw_hash = sm->user->password;
  342. } else {
  343. nt_password_hash(sm->user->password,
  344. sm->user->password_len,
  345. pw_hash_buf);
  346. pw_hash = pw_hash_buf;
  347. }
  348. generate_authenticator_response_pwhash(
  349. pw_hash, peer_challenge, data->auth_challenge,
  350. username, username_len, nt_response,
  351. data->auth_response);
  352. hash_nt_password_hash(pw_hash, pw_hash_hash);
  353. get_master_key(pw_hash_hash, nt_response, data->master_key);
  354. data->master_key_valid = 1;
  355. wpa_hexdump_key(MSG_DEBUG, "EAP-MSCHAPV2: Derived Master Key",
  356. data->master_key, MSCHAPV2_KEY_LEN);
  357. } else {
  358. wpa_hexdump(MSG_MSGDUMP, "EAP-MSCHAPV2: Expected NT-Response",
  359. expected, 24);
  360. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Invalid NT-Response");
  361. data->state = FAILURE_REQ;
  362. }
  363. }
  364. static void eap_mschapv2_process_success_resp(struct eap_sm *sm,
  365. struct eap_mschapv2_data *data,
  366. struct wpabuf *respData)
  367. {
  368. struct eap_mschapv2_hdr *resp;
  369. const u8 *pos;
  370. size_t len;
  371. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, respData,
  372. &len);
  373. if (pos == NULL || len < 1)
  374. return; /* Should not happen - frame already validated */
  375. resp = (struct eap_mschapv2_hdr *) pos;
  376. if (resp->op_code == MSCHAPV2_OP_SUCCESS) {
  377. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Received Success Response"
  378. " - authentication completed successfully");
  379. data->state = SUCCESS;
  380. } else {
  381. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Did not receive Success "
  382. "Response - peer rejected authentication");
  383. data->state = FAILURE;
  384. }
  385. }
  386. static void eap_mschapv2_process_failure_resp(struct eap_sm *sm,
  387. struct eap_mschapv2_data *data,
  388. struct wpabuf *respData)
  389. {
  390. struct eap_mschapv2_hdr *resp;
  391. const u8 *pos;
  392. size_t len;
  393. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, respData,
  394. &len);
  395. if (pos == NULL || len < 1)
  396. return; /* Should not happen - frame already validated */
  397. resp = (struct eap_mschapv2_hdr *) pos;
  398. if (resp->op_code == MSCHAPV2_OP_FAILURE) {
  399. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Received Failure Response"
  400. " - authentication failed");
  401. } else {
  402. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Did not receive Failure "
  403. "Response - authentication failed");
  404. }
  405. data->state = FAILURE;
  406. }
  407. static void eap_mschapv2_process(struct eap_sm *sm, void *priv,
  408. struct wpabuf *respData)
  409. {
  410. struct eap_mschapv2_data *data = priv;
  411. if (sm->user == NULL || sm->user->password == NULL) {
  412. wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Password not configured");
  413. data->state = FAILURE;
  414. return;
  415. }
  416. switch (data->state) {
  417. case CHALLENGE:
  418. eap_mschapv2_process_response(sm, data, respData);
  419. break;
  420. case SUCCESS_REQ:
  421. eap_mschapv2_process_success_resp(sm, data, respData);
  422. break;
  423. case FAILURE_REQ:
  424. eap_mschapv2_process_failure_resp(sm, data, respData);
  425. break;
  426. default:
  427. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Unknown state %d in "
  428. "process", data->state);
  429. break;
  430. }
  431. }
  432. static Boolean eap_mschapv2_isDone(struct eap_sm *sm, void *priv)
  433. {
  434. struct eap_mschapv2_data *data = priv;
  435. return data->state == SUCCESS || data->state == FAILURE;
  436. }
  437. static u8 * eap_mschapv2_getKey(struct eap_sm *sm, void *priv, size_t *len)
  438. {
  439. struct eap_mschapv2_data *data = priv;
  440. u8 *key;
  441. if (data->state != SUCCESS || !data->master_key_valid)
  442. return NULL;
  443. *len = 2 * MSCHAPV2_KEY_LEN;
  444. key = os_malloc(*len);
  445. if (key == NULL)
  446. return NULL;
  447. /* MSK = server MS-MPPE-Recv-Key | MS-MPPE-Send-Key */
  448. get_asymetric_start_key(data->master_key, key, MSCHAPV2_KEY_LEN, 0, 1);
  449. get_asymetric_start_key(data->master_key, key + MSCHAPV2_KEY_LEN,
  450. MSCHAPV2_KEY_LEN, 1, 1);
  451. wpa_hexdump_key(MSG_DEBUG, "EAP-MSCHAPV2: Derived key", key, *len);
  452. return key;
  453. }
  454. static Boolean eap_mschapv2_isSuccess(struct eap_sm *sm, void *priv)
  455. {
  456. struct eap_mschapv2_data *data = priv;
  457. return data->state == SUCCESS;
  458. }
  459. int eap_server_mschapv2_register(void)
  460. {
  461. struct eap_method *eap;
  462. int ret;
  463. eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
  464. EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2,
  465. "MSCHAPV2");
  466. if (eap == NULL)
  467. return -1;
  468. eap->init = eap_mschapv2_init;
  469. eap->reset = eap_mschapv2_reset;
  470. eap->buildReq = eap_mschapv2_buildReq;
  471. eap->check = eap_mschapv2_check;
  472. eap->process = eap_mschapv2_process;
  473. eap->isDone = eap_mschapv2_isDone;
  474. eap->getKey = eap_mschapv2_getKey;
  475. eap->isSuccess = eap_mschapv2_isSuccess;
  476. ret = eap_server_method_register(eap);
  477. if (ret)
  478. eap_server_method_free(eap);
  479. return ret;
  480. }