eap_server_mschapv2.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. os_free(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. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, respData,
  241. &len);
  242. if (pos == NULL || len < 1)
  243. return; /* Should not happen - frame already validated */
  244. end = pos + len;
  245. resp = (struct eap_mschapv2_hdr *) pos;
  246. pos = (u8 *) (resp + 1);
  247. if (len < sizeof(*resp) + 1 + 49 ||
  248. resp->op_code != MSCHAPV2_OP_RESPONSE ||
  249. pos[0] != 49) {
  250. wpa_hexdump_buf(MSG_DEBUG, "EAP-MSCHAPV2: Invalid response",
  251. respData);
  252. data->state = FAILURE;
  253. return;
  254. }
  255. data->resp_mschapv2_id = resp->mschapv2_id;
  256. pos++;
  257. peer_challenge = pos;
  258. pos += 16 + 8;
  259. nt_response = pos;
  260. pos += 24;
  261. flags = *pos++;
  262. name = pos;
  263. name_len = end - name;
  264. if (data->peer_challenge) {
  265. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Using pre-configured "
  266. "Peer-Challenge");
  267. peer_challenge = data->peer_challenge;
  268. }
  269. wpa_hexdump(MSG_MSGDUMP, "EAP-MSCHAPV2: Peer-Challenge",
  270. peer_challenge, 16);
  271. wpa_hexdump(MSG_MSGDUMP, "EAP-MSCHAPV2: NT-Response", nt_response, 24);
  272. wpa_printf(MSG_MSGDUMP, "EAP-MSCHAPV2: Flags 0x%x", flags);
  273. wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-MSCHAPV2: Name", name, name_len);
  274. /* MSCHAPv2 does not include optional domain name in the
  275. * challenge-response calculation, so remove domain prefix
  276. * (if present). */
  277. username = sm->identity;
  278. username_len = sm->identity_len;
  279. for (i = 0; i < username_len; i++) {
  280. if (username[i] == '\\') {
  281. username_len -= i + 1;
  282. username += i + 1;
  283. break;
  284. }
  285. }
  286. user = name;
  287. user_len = name_len;
  288. for (i = 0; i < user_len; i++) {
  289. if (user[i] == '\\') {
  290. user_len -= i + 1;
  291. user += i + 1;
  292. break;
  293. }
  294. }
  295. if (username_len != user_len ||
  296. os_memcmp(username, user, username_len) != 0) {
  297. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Mismatch in user names");
  298. wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Expected user "
  299. "name", username, username_len);
  300. wpa_hexdump_ascii(MSG_DEBUG, "EAP-MSCHAPV2: Received user "
  301. "name", user, user_len);
  302. data->state = FAILURE;
  303. return;
  304. }
  305. wpa_hexdump_ascii(MSG_MSGDUMP, "EAP-MSCHAPV2: User name",
  306. username, username_len);
  307. if (sm->user->password_hash) {
  308. res = generate_nt_response_pwhash(data->auth_challenge,
  309. peer_challenge,
  310. username, username_len,
  311. sm->user->password,
  312. expected);
  313. } else {
  314. res = generate_nt_response(data->auth_challenge,
  315. peer_challenge,
  316. username, username_len,
  317. sm->user->password,
  318. sm->user->password_len,
  319. expected);
  320. }
  321. if (res) {
  322. data->state = FAILURE;
  323. return;
  324. }
  325. if (os_memcmp(nt_response, expected, 24) == 0) {
  326. const u8 *pw_hash;
  327. u8 pw_hash_buf[16], pw_hash_hash[16];
  328. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Correct NT-Response");
  329. data->state = SUCCESS_REQ;
  330. /* Authenticator response is not really needed yet, but
  331. * calculate it here so that peer_challenge and username need
  332. * not be saved. */
  333. if (sm->user->password_hash) {
  334. pw_hash = sm->user->password;
  335. } else {
  336. if (nt_password_hash(sm->user->password,
  337. sm->user->password_len,
  338. pw_hash_buf) < 0) {
  339. data->state = FAILURE;
  340. return;
  341. }
  342. pw_hash = pw_hash_buf;
  343. }
  344. generate_authenticator_response_pwhash(
  345. pw_hash, peer_challenge, data->auth_challenge,
  346. username, username_len, nt_response,
  347. data->auth_response);
  348. hash_nt_password_hash(pw_hash, pw_hash_hash);
  349. get_master_key(pw_hash_hash, nt_response, data->master_key);
  350. data->master_key_valid = 1;
  351. wpa_hexdump_key(MSG_DEBUG, "EAP-MSCHAPV2: Derived Master Key",
  352. data->master_key, MSCHAPV2_KEY_LEN);
  353. } else {
  354. wpa_hexdump(MSG_MSGDUMP, "EAP-MSCHAPV2: Expected NT-Response",
  355. expected, 24);
  356. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Invalid NT-Response");
  357. data->state = FAILURE_REQ;
  358. }
  359. }
  360. static void eap_mschapv2_process_success_resp(struct eap_sm *sm,
  361. struct eap_mschapv2_data *data,
  362. struct wpabuf *respData)
  363. {
  364. struct eap_mschapv2_hdr *resp;
  365. const u8 *pos;
  366. size_t len;
  367. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, respData,
  368. &len);
  369. if (pos == NULL || len < 1)
  370. return; /* Should not happen - frame already validated */
  371. resp = (struct eap_mschapv2_hdr *) pos;
  372. if (resp->op_code == MSCHAPV2_OP_SUCCESS) {
  373. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Received Success Response"
  374. " - authentication completed successfully");
  375. data->state = SUCCESS;
  376. } else {
  377. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Did not receive Success "
  378. "Response - peer rejected authentication");
  379. data->state = FAILURE;
  380. }
  381. }
  382. static void eap_mschapv2_process_failure_resp(struct eap_sm *sm,
  383. struct eap_mschapv2_data *data,
  384. struct wpabuf *respData)
  385. {
  386. struct eap_mschapv2_hdr *resp;
  387. const u8 *pos;
  388. size_t len;
  389. pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2, respData,
  390. &len);
  391. if (pos == NULL || len < 1)
  392. return; /* Should not happen - frame already validated */
  393. resp = (struct eap_mschapv2_hdr *) pos;
  394. if (resp->op_code == MSCHAPV2_OP_FAILURE) {
  395. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Received Failure Response"
  396. " - authentication failed");
  397. } else {
  398. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Did not receive Failure "
  399. "Response - authentication failed");
  400. }
  401. data->state = FAILURE;
  402. }
  403. static void eap_mschapv2_process(struct eap_sm *sm, void *priv,
  404. struct wpabuf *respData)
  405. {
  406. struct eap_mschapv2_data *data = priv;
  407. if (sm->user == NULL || sm->user->password == NULL) {
  408. wpa_printf(MSG_INFO, "EAP-MSCHAPV2: Password not configured");
  409. data->state = FAILURE;
  410. return;
  411. }
  412. switch (data->state) {
  413. case CHALLENGE:
  414. eap_mschapv2_process_response(sm, data, respData);
  415. break;
  416. case SUCCESS_REQ:
  417. eap_mschapv2_process_success_resp(sm, data, respData);
  418. break;
  419. case FAILURE_REQ:
  420. eap_mschapv2_process_failure_resp(sm, data, respData);
  421. break;
  422. default:
  423. wpa_printf(MSG_DEBUG, "EAP-MSCHAPV2: Unknown state %d in "
  424. "process", data->state);
  425. break;
  426. }
  427. }
  428. static Boolean eap_mschapv2_isDone(struct eap_sm *sm, void *priv)
  429. {
  430. struct eap_mschapv2_data *data = priv;
  431. return data->state == SUCCESS || data->state == FAILURE;
  432. }
  433. static u8 * eap_mschapv2_getKey(struct eap_sm *sm, void *priv, size_t *len)
  434. {
  435. struct eap_mschapv2_data *data = priv;
  436. u8 *key;
  437. if (data->state != SUCCESS || !data->master_key_valid)
  438. return NULL;
  439. *len = 2 * MSCHAPV2_KEY_LEN;
  440. key = os_malloc(*len);
  441. if (key == NULL)
  442. return NULL;
  443. /* MSK = server MS-MPPE-Recv-Key | MS-MPPE-Send-Key */
  444. get_asymetric_start_key(data->master_key, key, MSCHAPV2_KEY_LEN, 0, 1);
  445. get_asymetric_start_key(data->master_key, key + MSCHAPV2_KEY_LEN,
  446. MSCHAPV2_KEY_LEN, 1, 1);
  447. wpa_hexdump_key(MSG_DEBUG, "EAP-MSCHAPV2: Derived key", key, *len);
  448. return key;
  449. }
  450. static Boolean eap_mschapv2_isSuccess(struct eap_sm *sm, void *priv)
  451. {
  452. struct eap_mschapv2_data *data = priv;
  453. return data->state == SUCCESS;
  454. }
  455. int eap_server_mschapv2_register(void)
  456. {
  457. struct eap_method *eap;
  458. int ret;
  459. eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
  460. EAP_VENDOR_IETF, EAP_TYPE_MSCHAPV2,
  461. "MSCHAPV2");
  462. if (eap == NULL)
  463. return -1;
  464. eap->init = eap_mschapv2_init;
  465. eap->reset = eap_mschapv2_reset;
  466. eap->buildReq = eap_mschapv2_buildReq;
  467. eap->check = eap_mschapv2_check;
  468. eap->process = eap_mschapv2_process;
  469. eap->isDone = eap_mschapv2_isDone;
  470. eap->getKey = eap_mschapv2_getKey;
  471. eap->isSuccess = eap_mschapv2_isSuccess;
  472. ret = eap_server_method_register(eap);
  473. if (ret)
  474. eap_server_method_free(eap);
  475. return ret;
  476. }