eap_server_mschapv2.c 15 KB

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