eap_server_pwd.c revision 351611
11556Srgrimes/* 21556Srgrimes * hostapd / EAP-pwd (RFC 5931) server 31556Srgrimes * Copyright (c) 2010, Dan Harkins <dharkins@lounge.org> 41556Srgrimes * 51556Srgrimes * This software may be distributed under the terms of the BSD license. 61556Srgrimes * See README for more details. 71556Srgrimes */ 81556Srgrimes 91556Srgrimes#include "includes.h" 101556Srgrimes 111556Srgrimes#include "common.h" 121556Srgrimes#include "crypto/sha256.h" 131556Srgrimes#include "crypto/ms_funcs.h" 141556Srgrimes#include "crypto/crypto.h" 151556Srgrimes#include "eap_server/eap_i.h" 161556Srgrimes#include "eap_common/eap_pwd_common.h" 171556Srgrimes 181556Srgrimes 191556Srgrimesstruct eap_pwd_data { 201556Srgrimes enum { 211556Srgrimes PWD_ID_Req, PWD_Commit_Req, PWD_Confirm_Req, SUCCESS, FAILURE 221556Srgrimes } state; 231556Srgrimes u8 *id_peer; 241556Srgrimes size_t id_peer_len; 251556Srgrimes u8 *id_server; 261556Srgrimes size_t id_server_len; 271556Srgrimes u8 *password; 281556Srgrimes size_t password_len; 291556Srgrimes int password_hash; 301556Srgrimes u8 *salt; 311556Srgrimes size_t salt_len; 321556Srgrimes u32 token; 331556Srgrimes u16 group_num; 341556Srgrimes u8 password_prep; 351556Srgrimes EAP_PWD_group *grp; 361556Srgrimes 371556Srgrimes struct wpabuf *inbuf; 3836150Scharnier size_t in_frag_pos; 3936150Scharnier struct wpabuf *outbuf; 4036150Scharnier size_t out_frag_pos; 4136150Scharnier size_t mtu; 4239049Scracauer 431556Srgrimes struct crypto_bignum *k; 441556Srgrimes struct crypto_bignum *private_value; 4517987Speter struct crypto_bignum *peer_scalar; 4617987Speter struct crypto_bignum *my_scalar; 4717987Speter struct crypto_ec_point *my_element; 4817987Speter struct crypto_ec_point *peer_element; 491556Srgrimes 501556Srgrimes u8 my_confirm[SHA256_MAC_LEN]; 511556Srgrimes 521556Srgrimes u8 msk[EAP_MSK_LEN]; 531556Srgrimes u8 emsk[EAP_EMSK_LEN]; 5417987Speter u8 session_id[1 + SHA256_MAC_LEN]; 551556Srgrimes}; 561556Srgrimes 571556Srgrimes 581556Srgrimesstatic const char * eap_pwd_state_txt(int state) 591556Srgrimes{ 601556Srgrimes switch (state) { 611556Srgrimes case PWD_ID_Req: 621556Srgrimes return "PWD-ID-Req"; 631556Srgrimes case PWD_Commit_Req: 641556Srgrimes return "PWD-Commit-Req"; 651556Srgrimes case PWD_Confirm_Req: 661556Srgrimes return "PWD-Confirm-Req"; 671556Srgrimes case SUCCESS: 681556Srgrimes return "SUCCESS"; 691556Srgrimes case FAILURE: 701556Srgrimes return "FAILURE"; 711556Srgrimes default: 721556Srgrimes return "PWD-Unk"; 731556Srgrimes } 741556Srgrimes} 751556Srgrimes 761556Srgrimes 7717987Speterstatic void eap_pwd_state(struct eap_pwd_data *data, int state) 7838521Scracauer{ 7938536Scracauer wpa_printf(MSG_DEBUG, "EAP-pwd: %s -> %s", 8038950Scracauer eap_pwd_state_txt(data->state), eap_pwd_state_txt(state)); 8138521Scracauer data->state = state; 8238521Scracauer} 8320902Ssteve 841556Srgrimes 8517987Speterstatic void * eap_pwd_init(struct eap_sm *sm) 8617987Speter{ 8720902Ssteve struct eap_pwd_data *data; 881556Srgrimes 8920902Ssteve if (sm->user == NULL || sm->user->password == NULL || 9020902Ssteve sm->user->password_len == 0) { 9120902Ssteve wpa_printf(MSG_INFO, "EAP-PWD (server): Password is not " 9220902Ssteve "configured"); 9320902Ssteve return NULL; 9420902Ssteve } 9520902Ssteve 9620902Ssteve data = os_zalloc(sizeof(*data)); 9720902Ssteve if (data == NULL) 9820902Ssteve return NULL; 9920902Ssteve 10020902Ssteve data->group_num = sm->pwd_group; 10120902Ssteve wpa_printf(MSG_DEBUG, "EAP-pwd: Selected group number %d", 10220902Ssteve data->group_num); 10320902Ssteve data->state = PWD_ID_Req; 10420902Ssteve 10520902Ssteve data->id_server = (u8 *) os_strdup("server"); 10620902Ssteve if (data->id_server) 10720902Ssteve data->id_server_len = os_strlen((char *) data->id_server); 10820902Ssteve 10920902Ssteve data->password = os_malloc(sm->user->password_len); 11020902Ssteve if (data->password == NULL) { 11120902Ssteve wpa_printf(MSG_INFO, "EAP-PWD: Memory allocation password " 11220902Ssteve "fail"); 11320902Ssteve bin_clear_free(data->id_server, data->id_server_len); 11420902Ssteve os_free(data); 11520902Ssteve return NULL; 11620902Ssteve } 11720902Ssteve data->password_len = sm->user->password_len; 11820902Ssteve os_memcpy(data->password, sm->user->password, data->password_len); 11920902Ssteve data->password_hash = sm->user->password_hash; 12020902Ssteve 12120902Ssteve data->salt_len = sm->user->salt_len; 12220902Ssteve if (data->salt_len) { 12320902Ssteve data->salt = os_memdup(sm->user->salt, sm->user->salt_len); 12420902Ssteve if (!data->salt) { 12520902Ssteve wpa_printf(MSG_INFO, 12620902Ssteve "EAP-pwd: Memory allocation of salt failed"); 12720902Ssteve bin_clear_free(data->id_server, data->id_server_len); 12820902Ssteve bin_clear_free(data->password, data->password_len); 12920902Ssteve os_free(data); 13020902Ssteve return NULL; 13120902Ssteve } 13220902Ssteve } 13320902Ssteve 13420902Ssteve data->in_frag_pos = data->out_frag_pos = 0; 1351556Srgrimes data->inbuf = data->outbuf = NULL; 1361556Srgrimes /* use default MTU from RFC 5931 if not configured otherwise */ 13717987Speter data->mtu = sm->fragment_size > 0 ? sm->fragment_size : 1020; 13817987Speter 13917987Speter return data; 14020425Ssteve} 14117987Speter 1421556Srgrimes 1431556Srgrimesstatic void eap_pwd_reset(struct eap_sm *sm, void *priv) 1441556Srgrimes{ 1451556Srgrimes struct eap_pwd_data *data = priv; 14620902Ssteve 1471556Srgrimes crypto_bignum_deinit(data->private_value, 1); 14820902Ssteve crypto_bignum_deinit(data->peer_scalar, 1); 14920902Ssteve crypto_bignum_deinit(data->my_scalar, 1); 1501556Srgrimes crypto_bignum_deinit(data->k, 1); 1511556Srgrimes crypto_ec_point_deinit(data->my_element, 1); 1521556Srgrimes crypto_ec_point_deinit(data->peer_element, 1); 15320902Ssteve bin_clear_free(data->id_peer, data->id_peer_len); 15420902Ssteve bin_clear_free(data->id_server, data->id_server_len); 15520902Ssteve bin_clear_free(data->password, data->password_len); 15620902Ssteve bin_clear_free(data->salt, data->salt_len); 15720902Ssteve if (data->grp) { 15820902Ssteve crypto_ec_deinit(data->grp->group); 15920902Ssteve crypto_ec_point_deinit(data->grp->pwe, 1); 16020902Ssteve os_free(data->grp); 16120902Ssteve } 16220902Ssteve wpabuf_free(data->inbuf); 16320902Ssteve wpabuf_free(data->outbuf); 16420902Ssteve bin_clear_free(data, sizeof(*data)); 16520902Ssteve} 16620902Ssteve 16720902Ssteve 16820902Sstevestatic void eap_pwd_build_id_req(struct eap_sm *sm, struct eap_pwd_data *data, 16920902Ssteve u8 id) 17020902Ssteve{ 17120902Ssteve wpa_printf(MSG_DEBUG, "EAP-pwd: ID/Request"); 1721556Srgrimes /* 1731556Srgrimes * if we're fragmenting then we already have an id request, just return 1741556Srgrimes */ 1751556Srgrimes if (data->out_frag_pos) 1761556Srgrimes return; 1771556Srgrimes 1781556Srgrimes data->outbuf = wpabuf_alloc(sizeof(struct eap_pwd_id) + 1791556Srgrimes data->id_server_len); 1801556Srgrimes if (data->outbuf == NULL) { 18120902Ssteve eap_pwd_state(data, FAILURE); 1821556Srgrimes return; 1831556Srgrimes } 1841556Srgrimes 1851556Srgrimes if (os_get_random((u8 *) &data->token, sizeof(data->token)) < 0) { 1861556Srgrimes wpabuf_free(data->outbuf); 1871556Srgrimes data->outbuf = NULL; 1881556Srgrimes eap_pwd_state(data, FAILURE); 1891556Srgrimes return; 1901556Srgrimes } 19120902Ssteve 19220902Ssteve wpa_hexdump_key(MSG_DEBUG, "EAP-pwd (server): password", 19338950Scracauer data->password, data->password_len); 1941556Srgrimes if (data->salt_len) 19520902Ssteve wpa_hexdump(MSG_DEBUG, "EAP-pwd (server): salt", 1961556Srgrimes data->salt, data->salt_len); 1971556Srgrimes 1981556Srgrimes /* 1991556Srgrimes * If this is a salted password then figure out how it was hashed 2001556Srgrimes * based on the length. 2011556Srgrimes */ 2021556Srgrimes if (data->salt_len) { 2031556Srgrimes switch (data->password_len) { 2041556Srgrimes case 20: 2051556Srgrimes data->password_prep = EAP_PWD_PREP_SSHA1; 2061556Srgrimes break; 2071556Srgrimes case 32: 2081556Srgrimes data->password_prep = EAP_PWD_PREP_SSHA256; 2091556Srgrimes break; 2101556Srgrimes case 64: 2111556Srgrimes data->password_prep = EAP_PWD_PREP_SSHA512; 21231098Sbde break; 21320425Ssteve default: 21417987Speter wpa_printf(MSG_INFO, 21517987Speter "EAP-pwd (server): bad size %d for salted password", 2161556Srgrimes (int) data->password_len); 21731098Sbde eap_pwd_state(data, FAILURE); 2181556Srgrimes return; 2191556Srgrimes } 2201556Srgrimes } else { 2211556Srgrimes /* Otherwise, figure out whether it's MS hashed or plain */ 2221556Srgrimes data->password_prep = data->password_hash ? EAP_PWD_PREP_MS : 2231556Srgrimes EAP_PWD_PREP_NONE; 2241556Srgrimes } 2251556Srgrimes 22638521Scracauer wpabuf_put_be16(data->outbuf, data->group_num); 2271556Srgrimes wpabuf_put_u8(data->outbuf, EAP_PWD_DEFAULT_RAND_FUNC); 2281556Srgrimes wpabuf_put_u8(data->outbuf, EAP_PWD_DEFAULT_PRF); 22938521Scracauer wpabuf_put_data(data->outbuf, &data->token, sizeof(data->token)); 2301556Srgrimes wpabuf_put_u8(data->outbuf, data->password_prep); 2311556Srgrimes wpabuf_put_data(data->outbuf, data->id_server, data->id_server_len); 2321556Srgrimes} 2331556Srgrimes 2341556Srgrimes 2351556Srgrimesstatic void eap_pwd_build_commit_req(struct eap_sm *sm, 2361556Srgrimes struct eap_pwd_data *data, u8 id) 2371556Srgrimes{ 2381556Srgrimes struct crypto_bignum *mask = NULL; 2391556Srgrimes u8 *scalar, *element; 24038535Scracauer size_t prime_len, order_len; 24138521Scracauer 2421556Srgrimes wpa_printf(MSG_DEBUG, "EAP-pwd: Commit/Request"); 24338521Scracauer /* 2441556Srgrimes * if we're fragmenting then we already have an commit request, just 2451556Srgrimes * return 2461556Srgrimes */ 2471556Srgrimes if (data->out_frag_pos) 2481556Srgrimes return; 24938521Scracauer 2501556Srgrimes prime_len = crypto_ec_prime_len(data->grp->group); 2511556Srgrimes order_len = crypto_ec_order_len(data->grp->group); 2521556Srgrimes 2531556Srgrimes data->private_value = crypto_bignum_init(); 2541556Srgrimes data->my_element = crypto_ec_point_init(data->grp->group); 25517987Speter data->my_scalar = crypto_bignum_init(); 25620902Ssteve mask = crypto_bignum_init(); 2578855Srgrimes if (!data->private_value || !data->my_element || !data->my_scalar || 2588855Srgrimes !mask) { 2598855Srgrimes wpa_printf(MSG_INFO, "EAP-PWD (server): scalar allocation " 2601556Srgrimes "fail"); 26117987Speter goto fin; 26217987Speter } 26317987Speter 26417987Speter if (eap_pwd_get_rand_mask(data->grp, data->private_value, mask, 26517987Speter data->my_scalar) < 0) 26617987Speter goto fin; 26731098Sbde 26817987Speter if (crypto_ec_point_mul(data->grp->group, data->grp->pwe, mask, 2691556Srgrimes data->my_element) < 0) { 2708855Srgrimes wpa_printf(MSG_INFO, "EAP-PWD (server): element allocation " 2711556Srgrimes "fail"); 2721556Srgrimes eap_pwd_state(data, FAILURE); 2731556Srgrimes goto fin; 2741556Srgrimes } 2751556Srgrimes 2761556Srgrimes if (crypto_ec_point_invert(data->grp->group, data->my_element) < 0) { 2771556Srgrimes wpa_printf(MSG_INFO, "EAP-PWD (server): element inversion " 2781556Srgrimes "fail"); 2791556Srgrimes goto fin; 28031098Sbde } 2811556Srgrimes 2821556Srgrimes data->outbuf = wpabuf_alloc(2 * prime_len + order_len + 2831556Srgrimes (data->salt ? 1 + data->salt_len : 0)); 2841556Srgrimes if (data->outbuf == NULL) 2851556Srgrimes goto fin; 2861556Srgrimes 28731098Sbde /* If we're doing salted password prep, add the salt */ 28830969Sache if (data->salt_len) { 28931098Sbde wpabuf_put_u8(data->outbuf, data->salt_len); 29031098Sbde wpabuf_put_data(data->outbuf, data->salt, data->salt_len); 29130969Sache } 2921556Srgrimes 2931556Srgrimes /* We send the element as (x,y) followed by the scalar */ 29420902Ssteve element = wpabuf_put(data->outbuf, 2 * prime_len); 2951556Srgrimes scalar = wpabuf_put(data->outbuf, order_len); 2961556Srgrimes crypto_bignum_to_bin(data->my_scalar, scalar, order_len, order_len); 2971556Srgrimes if (crypto_ec_point_to_bin(data->grp->group, data->my_element, element, 29817987Speter element + prime_len) < 0) { 29920425Ssteve wpa_printf(MSG_INFO, "EAP-PWD (server): point assignment " 30017987Speter "fail"); 30117987Speter goto fin; 30217987Speter } 3031556Srgrimes 3041556Srgrimesfin: 3051556Srgrimes crypto_bignum_deinit(mask, 1); 30617987Speter if (data->outbuf == NULL) 30717987Speter eap_pwd_state(data, FAILURE); 30817987Speter} 3091556Srgrimes 3101556Srgrimes 31120902Sstevestatic void eap_pwd_build_confirm_req(struct eap_sm *sm, 3121556Srgrimes struct eap_pwd_data *data, u8 id) 3131556Srgrimes{ 3141556Srgrimes struct crypto_hash *hash = NULL; 3151556Srgrimes u8 conf[SHA256_MAC_LEN], *cruft = NULL, *ptr; 31620425Ssteve u16 grp; 31717987Speter size_t prime_len, order_len; 31817987Speter 31920902Ssteve wpa_printf(MSG_DEBUG, "EAP-pwd: Confirm/Request"); 32020902Ssteve /* 3211556Srgrimes * if we're fragmenting then we already have an confirm request, just 3221556Srgrimes * return 32320902Ssteve */ 3241556Srgrimes if (data->out_frag_pos) 3251556Srgrimes return; 3261556Srgrimes 3271556Srgrimes prime_len = crypto_ec_prime_len(data->grp->group); 32817987Speter order_len = crypto_ec_order_len(data->grp->group); 3291556Srgrimes 3301556Srgrimes /* Each component of the cruft will be at most as big as the prime */ 3311556Srgrimes cruft = os_malloc(prime_len * 2); 3321556Srgrimes if (!cruft) { 3331556Srgrimes wpa_printf(MSG_INFO, "EAP-PWD (server): debug allocation " 3341556Srgrimes "fail"); 33517987Speter goto fin; 3361556Srgrimes } 3371556Srgrimes 3381556Srgrimes /* 3391556Srgrimes * commit is H(k | server_element | server_scalar | peer_element | 3401556Srgrimes * peer_scalar | ciphersuite) 3411556Srgrimes */ 3421556Srgrimes hash = eap_pwd_h_init(); 3431556Srgrimes if (hash == NULL) 3441556Srgrimes goto fin; 3451556Srgrimes 3461556Srgrimes /* 34720425Ssteve * Zero the memory each time because this is mod prime math and some 34817987Speter * value may start with a few zeros and the previous one did not. 34917987Speter * 35031098Sbde * First is k 35130969Sache */ 3521556Srgrimes crypto_bignum_to_bin(data->k, cruft, prime_len, prime_len); 35330969Sache eap_pwd_h_update(hash, cruft, prime_len); 3541556Srgrimes 3551556Srgrimes /* server element: x, y */ 3561556Srgrimes if (crypto_ec_point_to_bin(data->grp->group, data->my_element, cruft, 3571556Srgrimes cruft + prime_len) < 0) { 35838950Scracauer wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point " 35920902Ssteve "assignment fail"); 36020902Ssteve goto fin; 3611556Srgrimes } 36238950Scracauer eap_pwd_h_update(hash, cruft, prime_len * 2); 36338950Scracauer 36438950Scracauer /* server scalar */ 36538521Scracauer crypto_bignum_to_bin(data->my_scalar, cruft, order_len, order_len); 36638950Scracauer eap_pwd_h_update(hash, cruft, order_len); 36738950Scracauer 36838950Scracauer /* peer element: x, y */ 36938950Scracauer if (crypto_ec_point_to_bin(data->grp->group, data->peer_element, cruft, 37039049Scracauer cruft + prime_len) < 0) { 37139049Scracauer wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point " 37239049Scracauer "assignment fail"); 3731556Srgrimes goto fin; 3741556Srgrimes } 3751556Srgrimes eap_pwd_h_update(hash, cruft, prime_len * 2); 3761556Srgrimes 3771556Srgrimes /* peer scalar */ 3781556Srgrimes crypto_bignum_to_bin(data->peer_scalar, cruft, order_len, order_len); 3791556Srgrimes eap_pwd_h_update(hash, cruft, order_len); 3801556Srgrimes 38120902Ssteve /* ciphersuite */ 38220902Ssteve grp = htons(data->group_num); 3831556Srgrimes os_memset(cruft, 0, prime_len); 3841556Srgrimes ptr = cruft; 3851556Srgrimes os_memcpy(ptr, &grp, sizeof(u16)); 38638521Scracauer ptr += sizeof(u16); 3871556Srgrimes *ptr = EAP_PWD_DEFAULT_RAND_FUNC; 38820902Ssteve ptr += sizeof(u8); 38920902Ssteve *ptr = EAP_PWD_DEFAULT_PRF; 39020902Ssteve ptr += sizeof(u8); 39120902Ssteve eap_pwd_h_update(hash, cruft, ptr - cruft); 39220902Ssteve 39320902Ssteve /* all done with the random function */ 39420902Ssteve eap_pwd_h_final(hash, conf); 39520902Ssteve hash = NULL; 39620902Ssteve os_memcpy(data->my_confirm, conf, SHA256_MAC_LEN); 39720902Ssteve 39820902Ssteve data->outbuf = wpabuf_alloc(SHA256_MAC_LEN); 39920902Ssteve if (data->outbuf == NULL) 40020902Ssteve goto fin; 40120902Ssteve 40220902Ssteve wpabuf_put_data(data->outbuf, conf, SHA256_MAC_LEN); 40320902Ssteve 4041556Srgrimesfin: 40520902Ssteve bin_clear_free(cruft, prime_len * 2); 4061556Srgrimes if (data->outbuf == NULL) 40720902Ssteve eap_pwd_state(data, FAILURE); 40820902Ssteve eap_pwd_h_final(hash, NULL); 4091556Srgrimes} 41038521Scracauer 4111556Srgrimes 4121556Srgrimesstatic struct wpabuf * 4131556Srgrimeseap_pwd_build_req(struct eap_sm *sm, void *priv, u8 id) 4141556Srgrimes{ 4151556Srgrimes struct eap_pwd_data *data = priv; 4161556Srgrimes struct wpabuf *req; 4171556Srgrimes u8 lm_exch; 4181556Srgrimes const u8 *buf; 41917987Speter u16 totlen = 0; 42017987Speter size_t len; 42117987Speter 42238521Scracauer /* 4231556Srgrimes * if we're buffering response fragments then just ACK 4241556Srgrimes */ 4251556Srgrimes if (data->in_frag_pos) { 4261556Srgrimes wpa_printf(MSG_DEBUG, "EAP-pwd: ACKing a fragment!!"); 4271556Srgrimes req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD, 4281556Srgrimes EAP_PWD_HDR_SIZE, EAP_CODE_REQUEST, id); 4291556Srgrimes if (req == NULL) { 4301556Srgrimes eap_pwd_state(data, FAILURE); 4311556Srgrimes return NULL; 4321556Srgrimes } 4331556Srgrimes switch (data->state) { 4341556Srgrimes case PWD_ID_Req: 4351556Srgrimes wpabuf_put_u8(req, EAP_PWD_OPCODE_ID_EXCH); 4361556Srgrimes break; 43720425Ssteve case PWD_Commit_Req: 43817987Speter wpabuf_put_u8(req, EAP_PWD_OPCODE_COMMIT_EXCH); 43917987Speter break; 4401556Srgrimes case PWD_Confirm_Req: 4411556Srgrimes wpabuf_put_u8(req, EAP_PWD_OPCODE_CONFIRM_EXCH); 4421556Srgrimes break; 4431556Srgrimes default: 4441556Srgrimes eap_pwd_state(data, FAILURE); /* just to be sure */ 4451556Srgrimes wpabuf_free(req); 4461556Srgrimes return NULL; 4471556Srgrimes } 4481556Srgrimes return req; 4491556Srgrimes } 4501556Srgrimes 4511556Srgrimes /* 4521556Srgrimes * build the data portion of a request 4531556Srgrimes */ 4541556Srgrimes switch (data->state) { 4551556Srgrimes case PWD_ID_Req: 4561556Srgrimes eap_pwd_build_id_req(sm, data, id); 4571556Srgrimes lm_exch = EAP_PWD_OPCODE_ID_EXCH; 4581556Srgrimes break; 4591556Srgrimes case PWD_Commit_Req: 4601556Srgrimes eap_pwd_build_commit_req(sm, data, id); 4611556Srgrimes lm_exch = EAP_PWD_OPCODE_COMMIT_EXCH; 462 break; 463 case PWD_Confirm_Req: 464 eap_pwd_build_confirm_req(sm, data, id); 465 lm_exch = EAP_PWD_OPCODE_CONFIRM_EXCH; 466 break; 467 default: 468 wpa_printf(MSG_INFO, "EAP-pwd: Unknown state %d in build_req", 469 data->state); 470 eap_pwd_state(data, FAILURE); 471 lm_exch = 0; /* hush now, sweet compiler */ 472 break; 473 } 474 475 if (data->state == FAILURE) 476 return NULL; 477 478 /* 479 * determine whether that data needs to be fragmented 480 */ 481 len = wpabuf_len(data->outbuf) - data->out_frag_pos; 482 if ((len + EAP_PWD_HDR_SIZE) > data->mtu) { 483 len = data->mtu - EAP_PWD_HDR_SIZE; 484 EAP_PWD_SET_MORE_BIT(lm_exch); 485 /* 486 * if this is the first fragment, need to set the M bit 487 * and add the total length to the eap_pwd_hdr 488 */ 489 if (data->out_frag_pos == 0) { 490 EAP_PWD_SET_LENGTH_BIT(lm_exch); 491 totlen = wpabuf_len(data->outbuf) + 492 EAP_PWD_HDR_SIZE + sizeof(u16); 493 len -= sizeof(u16); 494 wpa_printf(MSG_DEBUG, "EAP-pwd: Fragmenting output, " 495 "total length = %d", totlen); 496 } 497 wpa_printf(MSG_DEBUG, "EAP-pwd: Send a %d byte fragment", 498 (int) len); 499 } 500 501 /* 502 * alloc an eap request and populate it with the data 503 */ 504 req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD, 505 EAP_PWD_HDR_SIZE + len + 506 (totlen ? sizeof(u16) : 0), 507 EAP_CODE_REQUEST, id); 508 if (req == NULL) { 509 eap_pwd_state(data, FAILURE); 510 return NULL; 511 } 512 513 wpabuf_put_u8(req, lm_exch); 514 if (EAP_PWD_GET_LENGTH_BIT(lm_exch)) 515 wpabuf_put_be16(req, totlen); 516 517 buf = wpabuf_head_u8(data->outbuf); 518 wpabuf_put_data(req, buf + data->out_frag_pos, len); 519 data->out_frag_pos += len; 520 /* 521 * either not fragged or last fragment, either way free up the data 522 */ 523 if (data->out_frag_pos >= wpabuf_len(data->outbuf)) { 524 wpabuf_free(data->outbuf); 525 data->outbuf = NULL; 526 data->out_frag_pos = 0; 527 } 528 529 return req; 530} 531 532 533static Boolean eap_pwd_check(struct eap_sm *sm, void *priv, 534 struct wpabuf *respData) 535{ 536 struct eap_pwd_data *data = priv; 537 const u8 *pos; 538 size_t len; 539 540 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PWD, respData, &len); 541 if (pos == NULL || len < 1) { 542 wpa_printf(MSG_INFO, "EAP-pwd: Invalid frame"); 543 return TRUE; 544 } 545 546 wpa_printf(MSG_DEBUG, "EAP-pwd: Received frame: exch = %d, len = %d", 547 EAP_PWD_GET_EXCHANGE(*pos), (int) len); 548 549 if (data->state == PWD_ID_Req && 550 ((EAP_PWD_GET_EXCHANGE(*pos)) == EAP_PWD_OPCODE_ID_EXCH)) 551 return FALSE; 552 553 if (data->state == PWD_Commit_Req && 554 ((EAP_PWD_GET_EXCHANGE(*pos)) == EAP_PWD_OPCODE_COMMIT_EXCH)) 555 return FALSE; 556 557 if (data->state == PWD_Confirm_Req && 558 ((EAP_PWD_GET_EXCHANGE(*pos)) == EAP_PWD_OPCODE_CONFIRM_EXCH)) 559 return FALSE; 560 561 wpa_printf(MSG_INFO, "EAP-pwd: Unexpected opcode=%d in state=%d", 562 *pos, data->state); 563 564 return TRUE; 565} 566 567 568static void eap_pwd_process_id_resp(struct eap_sm *sm, 569 struct eap_pwd_data *data, 570 const u8 *payload, size_t payload_len) 571{ 572 struct eap_pwd_id *id; 573 const u8 *password; 574 size_t password_len; 575 u8 pwhashhash[16]; 576 int res; 577 578 if (payload_len < sizeof(struct eap_pwd_id)) { 579 wpa_printf(MSG_INFO, "EAP-pwd: Invalid ID response"); 580 return; 581 } 582 583 id = (struct eap_pwd_id *) payload; 584 if ((data->group_num != be_to_host16(id->group_num)) || 585 (id->random_function != EAP_PWD_DEFAULT_RAND_FUNC) || 586 (os_memcmp(id->token, (u8 *)&data->token, sizeof(data->token))) || 587 (id->prf != EAP_PWD_DEFAULT_PRF) || 588 (id->prep != data->password_prep)) { 589 wpa_printf(MSG_INFO, "EAP-pwd: peer changed parameters"); 590 eap_pwd_state(data, FAILURE); 591 return; 592 } 593 if (data->id_peer || data->grp) { 594 wpa_printf(MSG_INFO, "EAP-pwd: data was already allocated"); 595 return; 596 } 597 data->id_peer = os_malloc(payload_len - sizeof(struct eap_pwd_id)); 598 if (data->id_peer == NULL) { 599 wpa_printf(MSG_INFO, "EAP-PWD: memory allocation id fail"); 600 return; 601 } 602 data->id_peer_len = payload_len - sizeof(struct eap_pwd_id); 603 os_memcpy(data->id_peer, id->identity, data->id_peer_len); 604 wpa_hexdump_ascii(MSG_DEBUG, "EAP-PWD (server): peer sent id of", 605 data->id_peer, data->id_peer_len); 606 607 data->grp = get_eap_pwd_group(data->group_num); 608 if (data->grp == NULL) { 609 wpa_printf(MSG_INFO, "EAP-PWD: failed to allocate memory for " 610 "group"); 611 return; 612 } 613 614 /* 615 * If it's PREP_MS then hash the password again, otherwise regardless 616 * of the prep the client is doing, the password we have is the one to 617 * use to generate the password element. 618 */ 619 if (data->password_prep == EAP_PWD_PREP_MS) { 620 res = hash_nt_password_hash(data->password, pwhashhash); 621 if (res) 622 return; 623 password = pwhashhash; 624 password_len = sizeof(pwhashhash); 625 } else { 626 password = data->password; 627 password_len = data->password_len; 628 } 629 630 res = compute_password_element(data->grp, data->group_num, 631 password, password_len, 632 data->id_server, data->id_server_len, 633 data->id_peer, data->id_peer_len, 634 (u8 *) &data->token); 635 forced_memzero(pwhashhash, sizeof(pwhashhash)); 636 if (res) { 637 wpa_printf(MSG_INFO, "EAP-PWD (server): unable to compute " 638 "PWE"); 639 return; 640 } 641 wpa_printf(MSG_DEBUG, "EAP-PWD (server): computed %d bit PWE...", 642 (int) crypto_ec_prime_len_bits(data->grp->group)); 643 644 eap_pwd_state(data, PWD_Commit_Req); 645} 646 647 648static void 649eap_pwd_process_commit_resp(struct eap_sm *sm, struct eap_pwd_data *data, 650 const u8 *payload, size_t payload_len) 651{ 652 const u8 *ptr; 653 struct crypto_ec_point *K = NULL; 654 int res = 0; 655 size_t prime_len, order_len; 656 657 wpa_printf(MSG_DEBUG, "EAP-pwd: Received commit response"); 658 659 prime_len = crypto_ec_prime_len(data->grp->group); 660 order_len = crypto_ec_order_len(data->grp->group); 661 662 if (payload_len != 2 * prime_len + order_len) { 663 wpa_printf(MSG_INFO, 664 "EAP-pwd: Unexpected Commit payload length %u (expected %u)", 665 (unsigned int) payload_len, 666 (unsigned int) (2 * prime_len + order_len)); 667 goto fin; 668 } 669 670 data->k = crypto_bignum_init(); 671 K = crypto_ec_point_init(data->grp->group); 672 if (!data->k || !K) { 673 wpa_printf(MSG_INFO, "EAP-PWD (server): peer data allocation " 674 "fail"); 675 goto fin; 676 } 677 678 /* element, x then y, followed by scalar */ 679 ptr = payload; 680 data->peer_element = eap_pwd_get_element(data->grp, ptr); 681 if (!data->peer_element) { 682 wpa_printf(MSG_INFO, "EAP-PWD (server): setting peer element " 683 "fail"); 684 goto fin; 685 } 686 ptr += prime_len * 2; 687 data->peer_scalar = eap_pwd_get_scalar(data->grp, ptr); 688 if (!data->peer_scalar) { 689 wpa_printf(MSG_INFO, "EAP-PWD (server): peer data allocation " 690 "fail"); 691 goto fin; 692 } 693 694 /* detect reflection attacks */ 695 if (crypto_bignum_cmp(data->my_scalar, data->peer_scalar) == 0 || 696 crypto_ec_point_cmp(data->grp->group, data->my_element, 697 data->peer_element) == 0) { 698 wpa_printf(MSG_INFO, 699 "EAP-PWD (server): detected reflection attack!"); 700 goto fin; 701 } 702 703 /* compute the shared key, k */ 704 if ((crypto_ec_point_mul(data->grp->group, data->grp->pwe, 705 data->peer_scalar, K) < 0) || 706 (crypto_ec_point_add(data->grp->group, K, data->peer_element, 707 K) < 0) || 708 (crypto_ec_point_mul(data->grp->group, K, data->private_value, 709 K) < 0)) { 710 wpa_printf(MSG_INFO, "EAP-PWD (server): computing shared key " 711 "fail"); 712 goto fin; 713 } 714 715 /* 716 * This check is strictly speaking just for the case where 717 * co-factor > 1 but it was suggested that even though this is probably 718 * never going to happen it is a simple and safe check "just to be 719 * sure" so let's be safe. 720 */ 721 if (crypto_ec_point_is_at_infinity(data->grp->group, K)) { 722 wpa_printf(MSG_INFO, "EAP-PWD (server): shared key point is " 723 "at infinity"); 724 goto fin; 725 } 726 if (crypto_ec_point_x(data->grp->group, K, data->k)) { 727 wpa_printf(MSG_INFO, "EAP-PWD (server): unable to extract " 728 "shared secret from secret point"); 729 goto fin; 730 } 731 res = 1; 732 733fin: 734 crypto_ec_point_deinit(K, 1); 735 736 if (res) 737 eap_pwd_state(data, PWD_Confirm_Req); 738 else 739 eap_pwd_state(data, FAILURE); 740} 741 742 743static void 744eap_pwd_process_confirm_resp(struct eap_sm *sm, struct eap_pwd_data *data, 745 const u8 *payload, size_t payload_len) 746{ 747 struct crypto_hash *hash = NULL; 748 u32 cs; 749 u16 grp; 750 u8 conf[SHA256_MAC_LEN], *cruft = NULL, *ptr; 751 size_t prime_len, order_len; 752 753 prime_len = crypto_ec_prime_len(data->grp->group); 754 order_len = crypto_ec_order_len(data->grp->group); 755 756 if (payload_len != SHA256_MAC_LEN) { 757 wpa_printf(MSG_INFO, 758 "EAP-pwd: Unexpected Confirm payload length %u (expected %u)", 759 (unsigned int) payload_len, SHA256_MAC_LEN); 760 goto fin; 761 } 762 763 /* build up the ciphersuite: group | random_function | prf */ 764 grp = htons(data->group_num); 765 ptr = (u8 *) &cs; 766 os_memcpy(ptr, &grp, sizeof(u16)); 767 ptr += sizeof(u16); 768 *ptr = EAP_PWD_DEFAULT_RAND_FUNC; 769 ptr += sizeof(u8); 770 *ptr = EAP_PWD_DEFAULT_PRF; 771 772 /* each component of the cruft will be at most as big as the prime */ 773 cruft = os_malloc(prime_len * 2); 774 if (!cruft) { 775 wpa_printf(MSG_INFO, "EAP-PWD (peer): allocation fail"); 776 goto fin; 777 } 778 779 /* 780 * commit is H(k | peer_element | peer_scalar | server_element | 781 * server_scalar | ciphersuite) 782 */ 783 hash = eap_pwd_h_init(); 784 if (hash == NULL) 785 goto fin; 786 787 /* k */ 788 crypto_bignum_to_bin(data->k, cruft, prime_len, prime_len); 789 eap_pwd_h_update(hash, cruft, prime_len); 790 791 /* peer element: x, y */ 792 if (crypto_ec_point_to_bin(data->grp->group, data->peer_element, cruft, 793 cruft + prime_len) < 0) { 794 wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point " 795 "assignment fail"); 796 goto fin; 797 } 798 eap_pwd_h_update(hash, cruft, prime_len * 2); 799 800 /* peer scalar */ 801 crypto_bignum_to_bin(data->peer_scalar, cruft, order_len, order_len); 802 eap_pwd_h_update(hash, cruft, order_len); 803 804 /* server element: x, y */ 805 if (crypto_ec_point_to_bin(data->grp->group, data->my_element, cruft, 806 cruft + prime_len) < 0) { 807 wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point " 808 "assignment fail"); 809 goto fin; 810 } 811 eap_pwd_h_update(hash, cruft, prime_len * 2); 812 813 /* server scalar */ 814 crypto_bignum_to_bin(data->my_scalar, cruft, order_len, order_len); 815 eap_pwd_h_update(hash, cruft, order_len); 816 817 /* ciphersuite */ 818 eap_pwd_h_update(hash, (u8 *) &cs, sizeof(u32)); 819 820 /* all done */ 821 eap_pwd_h_final(hash, conf); 822 hash = NULL; 823 824 ptr = (u8 *) payload; 825 if (os_memcmp_const(conf, ptr, SHA256_MAC_LEN)) { 826 wpa_printf(MSG_INFO, "EAP-PWD (server): confirm did not " 827 "verify"); 828 goto fin; 829 } 830 831 wpa_printf(MSG_DEBUG, "EAP-pwd (server): confirm verified"); 832 if (compute_keys(data->grp, data->k, 833 data->peer_scalar, data->my_scalar, conf, 834 data->my_confirm, &cs, data->msk, data->emsk, 835 data->session_id) < 0) 836 eap_pwd_state(data, FAILURE); 837 else 838 eap_pwd_state(data, SUCCESS); 839 840fin: 841 bin_clear_free(cruft, prime_len * 2); 842 eap_pwd_h_final(hash, NULL); 843} 844 845 846static void eap_pwd_process(struct eap_sm *sm, void *priv, 847 struct wpabuf *respData) 848{ 849 struct eap_pwd_data *data = priv; 850 const u8 *pos; 851 size_t len; 852 u8 lm_exch; 853 u16 tot_len; 854 855 pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PWD, respData, &len); 856 if ((pos == NULL) || (len < 1)) { 857 wpa_printf(MSG_INFO, "Bad EAP header! pos %s and len = %d", 858 (pos == NULL) ? "is NULL" : "is not NULL", 859 (int) len); 860 return; 861 } 862 863 lm_exch = *pos; 864 pos++; /* skip over the bits and the exch */ 865 len--; 866 867 /* 868 * if we're fragmenting then this should be an ACK with no data, 869 * just return and continue fragmenting in the "build" section above 870 */ 871 if (data->out_frag_pos) { 872 if (len > 1) 873 wpa_printf(MSG_INFO, "EAP-pwd: Bad response! " 874 "Fragmenting but not an ACK"); 875 else 876 wpa_printf(MSG_DEBUG, "EAP-pwd: received ACK from " 877 "peer"); 878 return; 879 } 880 /* 881 * if we're receiving fragmented packets then we need to buffer... 882 * 883 * the first fragment has a total length 884 */ 885 if (EAP_PWD_GET_LENGTH_BIT(lm_exch)) { 886 if (len < 2) { 887 wpa_printf(MSG_DEBUG, 888 "EAP-pwd: Frame too short to contain Total-Length field"); 889 return; 890 } 891 tot_len = WPA_GET_BE16(pos); 892 wpa_printf(MSG_DEBUG, "EAP-pwd: Incoming fragments, total " 893 "length = %d", tot_len); 894 if (tot_len > 15000) 895 return; 896 if (data->inbuf) { 897 wpa_printf(MSG_DEBUG, 898 "EAP-pwd: Unexpected new fragment start when previous fragment is still in use"); 899 return; 900 } 901 data->inbuf = wpabuf_alloc(tot_len); 902 if (data->inbuf == NULL) { 903 wpa_printf(MSG_INFO, "EAP-pwd: Out of memory to " 904 "buffer fragments!"); 905 return; 906 } 907 data->in_frag_pos = 0; 908 pos += sizeof(u16); 909 len -= sizeof(u16); 910 } 911 /* 912 * the first and all intermediate fragments have the M bit set 913 */ 914 if (EAP_PWD_GET_MORE_BIT(lm_exch) || data->in_frag_pos) { 915 if (!data->inbuf) { 916 wpa_printf(MSG_DEBUG, 917 "EAP-pwd: No buffer for reassembly"); 918 eap_pwd_state(data, FAILURE); 919 return; 920 } 921 if ((data->in_frag_pos + len) > wpabuf_size(data->inbuf)) { 922 wpa_printf(MSG_DEBUG, "EAP-pwd: Buffer overflow " 923 "attack detected! (%d+%d > %d)", 924 (int) data->in_frag_pos, (int) len, 925 (int) wpabuf_size(data->inbuf)); 926 eap_pwd_state(data, FAILURE); 927 return; 928 } 929 wpabuf_put_data(data->inbuf, pos, len); 930 data->in_frag_pos += len; 931 } 932 if (EAP_PWD_GET_MORE_BIT(lm_exch)) { 933 wpa_printf(MSG_DEBUG, "EAP-pwd: Got a %d byte fragment", 934 (int) len); 935 return; 936 } 937 /* 938 * last fragment won't have the M bit set (but we're obviously 939 * buffering fragments so that's how we know it's the last) 940 */ 941 if (data->in_frag_pos && data->inbuf) { 942 pos = wpabuf_head_u8(data->inbuf); 943 len = data->in_frag_pos; 944 wpa_printf(MSG_DEBUG, "EAP-pwd: Last fragment, %d bytes", 945 (int) len); 946 } 947 switch (EAP_PWD_GET_EXCHANGE(lm_exch)) { 948 case EAP_PWD_OPCODE_ID_EXCH: 949 eap_pwd_process_id_resp(sm, data, pos, len); 950 break; 951 case EAP_PWD_OPCODE_COMMIT_EXCH: 952 eap_pwd_process_commit_resp(sm, data, pos, len); 953 break; 954 case EAP_PWD_OPCODE_CONFIRM_EXCH: 955 eap_pwd_process_confirm_resp(sm, data, pos, len); 956 break; 957 } 958 /* 959 * if we had been buffering fragments, here's a great place 960 * to clean up 961 */ 962 if (data->in_frag_pos) { 963 wpabuf_free(data->inbuf); 964 data->inbuf = NULL; 965 data->in_frag_pos = 0; 966 } 967} 968 969 970static u8 * eap_pwd_getkey(struct eap_sm *sm, void *priv, size_t *len) 971{ 972 struct eap_pwd_data *data = priv; 973 u8 *key; 974 975 if (data->state != SUCCESS) 976 return NULL; 977 978 key = os_memdup(data->msk, EAP_MSK_LEN); 979 if (key == NULL) 980 return NULL; 981 982 *len = EAP_MSK_LEN; 983 984 return key; 985} 986 987 988static u8 * eap_pwd_get_emsk(struct eap_sm *sm, void *priv, size_t *len) 989{ 990 struct eap_pwd_data *data = priv; 991 u8 *key; 992 993 if (data->state != SUCCESS) 994 return NULL; 995 996 key = os_memdup(data->emsk, EAP_EMSK_LEN); 997 if (key == NULL) 998 return NULL; 999 1000 *len = EAP_EMSK_LEN; 1001 1002 return key; 1003} 1004 1005 1006static Boolean eap_pwd_is_success(struct eap_sm *sm, void *priv) 1007{ 1008 struct eap_pwd_data *data = priv; 1009 return data->state == SUCCESS; 1010} 1011 1012 1013static Boolean eap_pwd_is_done(struct eap_sm *sm, void *priv) 1014{ 1015 struct eap_pwd_data *data = priv; 1016 return (data->state == SUCCESS) || (data->state == FAILURE); 1017} 1018 1019 1020static u8 * eap_pwd_get_session_id(struct eap_sm *sm, void *priv, size_t *len) 1021{ 1022 struct eap_pwd_data *data = priv; 1023 u8 *id; 1024 1025 if (data->state != SUCCESS) 1026 return NULL; 1027 1028 id = os_memdup(data->session_id, 1 + SHA256_MAC_LEN); 1029 if (id == NULL) 1030 return NULL; 1031 1032 *len = 1 + SHA256_MAC_LEN; 1033 1034 return id; 1035} 1036 1037 1038int eap_server_pwd_register(void) 1039{ 1040 struct eap_method *eap; 1041 1042 eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION, 1043 EAP_VENDOR_IETF, EAP_TYPE_PWD, 1044 "PWD"); 1045 if (eap == NULL) 1046 return -1; 1047 1048 eap->init = eap_pwd_init; 1049 eap->reset = eap_pwd_reset; 1050 eap->buildReq = eap_pwd_build_req; 1051 eap->check = eap_pwd_check; 1052 eap->process = eap_pwd_process; 1053 eap->isDone = eap_pwd_is_done; 1054 eap->getKey = eap_pwd_getkey; 1055 eap->get_emsk = eap_pwd_get_emsk; 1056 eap->isSuccess = eap_pwd_is_success; 1057 eap->getSessionId = eap_pwd_get_session_id; 1058 1059 return eap_server_method_register(eap); 1060} 1061