From df684d82ff2cf644dfd543a0399368cfb8dc0311 Mon Sep 17 00:00:00 2001 From: Dan Harkins Date: Tue, 14 Sep 2010 21:51:40 -1000 Subject: [PATCH] EAP-pwd: Add support for EAP-pwd server and peer functionality This adds an initial EAP-pwd (RFC 5931) implementation. For now, this requires OpenSSL. --- hostapd/Makefile | 6 + hostapd/config_file.c | 4 + hostapd/eap_register.c | 5 + src/ap/ap_config.c | 2 + src/ap/ap_config.h | 1 + src/ap/authsrv.c | 1 + src/ap/ieee802_1x.c | 1 + src/eap_common/eap_defs.h | 1 + src/eap_common/eap_pwd_common.c | 344 +++++++++++++ src/eap_common/eap_pwd_common.h | 79 +++ src/eap_peer/eap_methods.h | 1 + src/eap_peer/eap_pwd.c | 730 ++++++++++++++++++++++++++++ src/eap_server/eap.h | 1 + src/eap_server/eap_i.h | 1 + src/eap_server/eap_methods.h | 1 + src/eap_server/eap_server.c | 1 + src/eap_server/eap_server_pwd.c | 823 ++++++++++++++++++++++++++++++++ src/eapol_auth/eapol_auth_sm.c | 2 + src/eapol_auth/eapol_auth_sm.h | 1 + src/radius/radius_server.c | 9 + src/radius/radius_server.h | 7 + wpa_supplicant/Makefile | 8 + wpa_supplicant/eap_register.c | 9 + 23 files changed, 2038 insertions(+) create mode 100644 src/eap_common/eap_pwd_common.c create mode 100644 src/eap_common/eap_pwd_common.h create mode 100644 src/eap_peer/eap_pwd.c create mode 100644 src/eap_server/eap_server_pwd.c diff --git a/hostapd/Makefile b/hostapd/Makefile index 6102c10c3..f43f4a595 100644 --- a/hostapd/Makefile +++ b/hostapd/Makefile @@ -274,6 +274,12 @@ NEED_SHA256=y NEED_AES_OMAC1=y endif +ifdef CONFIG_EAP_PWD +CFLAGS += -DEAP_SERVER_PWD +OBJS += ../src/eap_server/eap_server_pwd.o ../src/eap_common/eap_pwd_common.o +NEED_SHA256=y +endif + ifdef CONFIG_EAP_VENDOR_TEST CFLAGS += -DEAP_SERVER_VENDOR_TEST OBJS += ../src/eap_server/eap_server_vendor_test.o diff --git a/hostapd/config_file.c b/hostapd/config_file.c index 0032504a6..5ff6add85 100644 --- a/hostapd/config_file.c +++ b/hostapd/config_file.c @@ -1369,6 +1369,10 @@ struct hostapd_config * hostapd_config_read(const char *fname) } else if (os_strcmp(buf, "tnc") == 0) { bss->tnc = atoi(pos); #endif /* EAP_SERVER_TNC */ +#ifdef EAP_SERVER_PWD + } else if (os_strcmp(buf, "pwd_group") == 0) { + bss->pwd_group = atoi(pos); +#endif /* EAP_SERVER_PWD */ #endif /* EAP_SERVER */ } else if (os_strcmp(buf, "eap_message") == 0) { char *term; diff --git a/hostapd/eap_register.c b/hostapd/eap_register.c index ae9bf9d38..bab28715e 100644 --- a/hostapd/eap_register.c +++ b/hostapd/eap_register.c @@ -130,5 +130,10 @@ int eap_server_register_methods(void) ret = eap_server_tnc_register(); #endif /* EAP_SERVER_TNC */ +#ifdef EAP_SERVER_PWD + if (ret == 0) + ret = eap_server_pwd_register(); +#endif /* EAP_SERVER_PWD */ + return ret; } diff --git a/src/ap/ap_config.c b/src/ap/ap_config.c index 59969933e..702cb765b 100644 --- a/src/ap/ap_config.c +++ b/src/ap/ap_config.c @@ -74,6 +74,8 @@ void hostapd_config_defaults_bss(struct hostapd_bss_config *bss) bss->max_listen_interval = 65535; + bss->pwd_group = 19; /* ECC: GF(p=256) */ + #ifdef CONFIG_IEEE80211W bss->assoc_sa_query_max_timeout = 1000; bss->assoc_sa_query_retry_timeout = 201; diff --git a/src/ap/ap_config.h b/src/ap/ap_config.h index 8bf71924a..fc74d8041 100644 --- a/src/ap/ap_config.h +++ b/src/ap/ap_config.h @@ -255,6 +255,7 @@ struct hostapd_bss_config { int eap_sim_aka_result_ind; int tnc; int fragment_size; + u16 pwd_group; char *radius_server_clients; int radius_server_auth_port; diff --git a/src/ap/authsrv.c b/src/ap/authsrv.c index 0ab0668ac..7c87fde41 100644 --- a/src/ap/authsrv.c +++ b/src/ap/authsrv.c @@ -119,6 +119,7 @@ static int hostapd_setup_radius_srv(struct hostapd_data *hapd) srv.get_eap_user = hostapd_radius_get_eap_user; srv.eap_req_id_text = conf->eap_req_id_text; srv.eap_req_id_text_len = conf->eap_req_id_text_len; + srv.pwd_group = conf->pwd_group; hapd->radius_srv = radius_server_init(&srv); if (hapd->radius_srv == NULL) { diff --git a/src/ap/ieee802_1x.c b/src/ap/ieee802_1x.c index c0ece64e1..a93581678 100644 --- a/src/ap/ieee802_1x.c +++ b/src/ap/ieee802_1x.c @@ -1652,6 +1652,7 @@ int ieee802_1x_init(struct hostapd_data *hapd) conf.tnc = hapd->conf->tnc; conf.wps = hapd->wps; conf.fragment_size = hapd->conf->fragment_size; + conf.pwd_group = hapd->conf->pwd_group; os_memset(&cb, 0, sizeof(cb)); cb.eapol_send = ieee802_1x_eapol_send; diff --git a/src/eap_common/eap_defs.h b/src/eap_common/eap_defs.h index 0efe7ab77..303530109 100644 --- a/src/eap_common/eap_defs.h +++ b/src/eap_common/eap_defs.h @@ -68,6 +68,7 @@ typedef enum { EAP_TYPE_IKEV2 = 49 /* RFC 5106 */, EAP_TYPE_AKA_PRIME = 50 /* draft-arkko-eap-aka-kdf-10.txt */, EAP_TYPE_GPSK = 51 /* RFC 5433 */, + EAP_TYPE_PWD = 52 /* RFC 5931 */, EAP_TYPE_EXPANDED = 254 /* RFC 3748 */ } EapType; diff --git a/src/eap_common/eap_pwd_common.c b/src/eap_common/eap_pwd_common.c new file mode 100644 index 000000000..d01ba0a04 --- /dev/null +++ b/src/eap_common/eap_pwd_common.c @@ -0,0 +1,344 @@ +/* + * EAP server/peer: EAP-pwd shared routines + * Copyright (c) 2010, Dan Harkins + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the BSD license. + * + * Alternatively, this software may be distributed under the terms of the + * GNU General Public License version 2 as published by the Free Software + * Foundation. + * + * See README and COPYING for more details. + */ + +#include "includes.h" +#include "common.h" +#include "eap_defs.h" +#include "eap_pwd_common.h" + +/* The random function H(x) = HMAC-SHA256(0^32, x) */ +void H_Init(HMAC_CTX *ctx) +{ + u8 allzero[SHA256_DIGEST_LENGTH]; + + os_memset(allzero, 0, SHA256_DIGEST_LENGTH); + HMAC_Init(ctx, allzero, SHA256_DIGEST_LENGTH, EVP_sha256()); +} + + +void H_Update(HMAC_CTX *ctx, const u8 *data, int len) +{ + HMAC_Update(ctx, data, len); +} + + +void H_Final(HMAC_CTX *ctx, u8 *digest) +{ + unsigned int mdlen = SHA256_DIGEST_LENGTH; + + HMAC_Final(ctx, digest, &mdlen); + HMAC_CTX_cleanup(ctx); +} + + +/* a counter-based KDF based on NIST SP800-108 */ +void eap_pwd_kdf(u8 *key, int keylen, u8 *label, int labellen, + u8 *result, int resultbitlen) +{ + HMAC_CTX hctx; + unsigned char digest[SHA256_DIGEST_LENGTH]; + u16 i, ctr, L; + int resultbytelen, len = 0; + unsigned int mdlen = SHA256_DIGEST_LENGTH; + unsigned char mask = 0xff; + + resultbytelen = (resultbitlen + 7)/8; + ctr = 0; + L = htons(resultbitlen); + while (len < resultbytelen) { + ctr++; i = htons(ctr); + HMAC_Init(&hctx, key, keylen, EVP_sha256()); + if (ctr > 1) + HMAC_Update(&hctx, digest, mdlen); + HMAC_Update(&hctx, (u8 *) &i, sizeof(u16)); + HMAC_Update(&hctx, label, labellen); + HMAC_Update(&hctx, (u8 *) &L, sizeof(u16)); + HMAC_Final(&hctx, digest, &mdlen); + if ((len + (int) mdlen) > resultbytelen) + os_memcpy(result + len, digest, resultbytelen - len); + else + os_memcpy(result + len, digest, mdlen); + len += mdlen; + HMAC_CTX_cleanup(&hctx); + } + + /* since we're expanding to a bit length, mask off the excess */ + if (resultbitlen % 8) { + mask >>= ((resultbytelen * 8) - resultbitlen); + result[0] &= mask; + } +} + + +/* + * compute a "random" secret point on an elliptic curve based + * on the password and identities. + */ +int compute_password_element(EAP_PWD_group *grp, u16 num, + u8 *password, int password_len, + u8 *id_server, int id_server_len, + u8 *id_peer, int id_peer_len, u8 *token) +{ + BIGNUM *x_candidate = NULL, *rnd = NULL, *cofactor = NULL; + HMAC_CTX ctx; + unsigned char pwe_digest[SHA256_DIGEST_LENGTH], *prfbuf = NULL, ctr; + int nid, is_odd, primebitlen, primebytelen, ret = 0; + + switch (num) { /* from IANA registry for IKE D-H groups */ + case 19: + nid = NID_X9_62_prime256v1; + break; + case 20: + nid = NID_secp384r1; + break; + case 21: + nid = NID_secp521r1; + break; + case 25: + nid = NID_X9_62_prime192v1; + break; + case 26: + nid = NID_secp224r1; + break; + default: + wpa_printf(MSG_INFO, "EAP-pwd: unsupported group %d", num); + return -1; + } + + grp->pwe = NULL; + grp->order = NULL; + grp->prime = NULL; + + if ((grp->group = EC_GROUP_new_by_curve_name(nid)) == NULL) { + wpa_printf(MSG_INFO, "EAP-pwd: unable to create EC_GROUP"); + goto fail; + } + + if (((rnd = BN_new()) == NULL) || + ((cofactor = BN_new()) == NULL) || + ((grp->pwe = EC_POINT_new(grp->group)) == NULL) || + ((grp->order = BN_new()) == NULL) || + ((grp->prime = BN_new()) == NULL) || + ((x_candidate = BN_new()) == NULL)) { + wpa_printf(MSG_INFO, "EAP-pwd: unable to create bignums"); + goto fail; + } + + if (!EC_GROUP_get_curve_GFp(grp->group, grp->prime, NULL, NULL, NULL)) + { + wpa_printf(MSG_INFO, "EAP-pwd: unable to get prime for GFp " + "curve"); + goto fail; + } + if (!EC_GROUP_get_order(grp->group, grp->order, NULL)) { + wpa_printf(MSG_INFO, "EAP-pwd: unable to get order for curve"); + goto fail; + } + if (!EC_GROUP_get_cofactor(grp->group, cofactor, NULL)) { + wpa_printf(MSG_INFO, "EAP-pwd: unable to get cofactor for " + "curve"); + goto fail; + } + primebitlen = BN_num_bits(grp->prime); + primebytelen = BN_num_bytes(grp->prime); + if ((prfbuf = os_malloc(primebytelen)) == NULL) { + wpa_printf(MSG_INFO, "EAP-pwd: unable to malloc space for prf " + "buffer"); + goto fail; + } + os_memset(prfbuf, 0, primebytelen); + ctr = 0; + while (1) { + if (ctr > 10) { + wpa_printf(MSG_INFO, "EAP-pwd: unable to find random " + "point on curve for group %d, something's " + "fishy", num); + goto fail; + } + ctr++; + + /* + * compute counter-mode password value and stretch to prime + * pwd-seed = H(token | peer-id | server-id | password | + * counter) + */ + H_Init(&ctx); + H_Update(&ctx, token, sizeof(u32)); + H_Update(&ctx, id_peer, id_peer_len); + H_Update(&ctx, id_server, id_server_len); + H_Update(&ctx, password, password_len); + H_Update(&ctx, &ctr, sizeof(ctr)); + H_Final(&ctx, pwe_digest); + + BN_bin2bn(pwe_digest, SHA256_DIGEST_LENGTH, rnd); + + eap_pwd_kdf(pwe_digest, SHA256_DIGEST_LENGTH, + (unsigned char *) "EAP-pwd Hunting and Pecking", + os_strlen("EAP-pwd Hunting and Pecking"), + prfbuf, primebitlen); + + BN_bin2bn(prfbuf, primebytelen, x_candidate); + if (BN_ucmp(x_candidate, grp->prime) >= 0) + continue; + + wpa_hexdump(MSG_DEBUG, "EAP-pwd: x_candidate", + prfbuf, primebytelen); + + /* + * need to unambiguously identify the solution, if there is + * one... + */ + if (BN_is_odd(rnd)) + is_odd = 1; + else + is_odd = 0; + + /* + * solve the quadratic equation, if it's not solvable then we + * don't have a point + */ + if (!EC_POINT_set_compressed_coordinates_GFp(grp->group, + grp->pwe, + x_candidate, + is_odd, NULL)) + continue; + /* + * If there's a solution to the equation then the point must be + * on the curve so why check again explicitly? OpenSSL code + * says this is required by X9.62. We're not X9.62 but it can't + * hurt just to be sure. + */ + if (!EC_POINT_is_on_curve(grp->group, grp->pwe, NULL)) { + wpa_printf(MSG_INFO, "EAP-pwd: point is not on curve"); + continue; + } + + if (BN_cmp(cofactor, BN_value_one())) { + /* make sure the point is not in a small sub-group */ + if (!EC_POINT_mul(grp->group, grp->pwe, NULL, grp->pwe, + cofactor, NULL)) { + wpa_printf(MSG_INFO, "EAP-pwd: cannot " + "multiply generator by order"); + continue; + } + if (EC_POINT_is_at_infinity(grp->group, grp->pwe)) { + wpa_printf(MSG_INFO, "EAP-pwd: point is at " + "infinity"); + continue; + } + } + /* if we got here then we have a new generator. */ + break; + } + wpa_printf(MSG_DEBUG, "EAP-pwd: found a PWE in %d tries", ctr); + grp->group_num = num; + if (0) { + fail: + EC_POINT_free(grp->pwe); + BN_free(grp->order); + BN_free(grp->prime); + free(grp); + grp = NULL; + ret = 1; + } + /* cleanliness and order.... */ + BN_free(cofactor); + BN_free(x_candidate); + BN_free(rnd); + free(prfbuf); + + return ret; +} + + +int compute_keys(EAP_PWD_group *grp, BN_CTX *bnctx, BIGNUM *k, + EC_POINT *server_element, EC_POINT *peer_element, + BIGNUM *server_scalar, BIGNUM *peer_scalar, u32 *ciphersuite, + u8 *msk, u8 *emsk) +{ + BIGNUM *scalar_sum, *x; + EC_POINT *element_sum; + HMAC_CTX ctx; + u8 mk[SHA256_DIGEST_LENGTH], *cruft; + u8 session_id[SHA256_DIGEST_LENGTH + 1]; + u8 msk_emsk[EAP_MSK_LEN + EAP_EMSK_LEN]; + int ret = -1; + + if (((cruft = os_malloc(BN_num_bytes(grp->prime))) == NULL) || + ((x = BN_new()) == NULL) || + ((scalar_sum = BN_new()) == NULL) || + ((element_sum = EC_POINT_new(grp->group)) == NULL)) + return -1; + + /* + * first compute the session-id = TypeCode | H(ciphersuite | scal_p | + * scal_s) + */ + session_id[0] = EAP_TYPE_PWD; + H_Init(&ctx); + H_Update(&ctx, (u8 *)ciphersuite, sizeof(u32)); + BN_bn2bin(peer_scalar, cruft); + H_Update(&ctx, cruft, BN_num_bytes(grp->order)); + BN_bn2bin(server_scalar, cruft); + H_Update(&ctx, cruft, BN_num_bytes(grp->order)); + H_Final(&ctx, &session_id[1]); + + /* + * then compute MK = H(k | F(elem_p + elem_s) | + * (scal_p + scal_s) mod r) + */ + H_Init(&ctx); + + /* k */ + os_memset(cruft, 0, BN_num_bytes(grp->prime)); + BN_bn2bin(k, cruft); + H_Update(&ctx, cruft, BN_num_bytes(grp->prime)); + + /* x = F(elem_p + elem_s) */ + if ((!EC_POINT_add(grp->group, element_sum, server_element, + peer_element, bnctx)) || + (!EC_POINT_get_affine_coordinates_GFp(grp->group, element_sum, x, + NULL, bnctx))) + goto fail; + + os_memset(cruft, 0, BN_num_bytes(grp->prime)); + BN_bn2bin(x, cruft); + H_Update(&ctx, cruft, BN_num_bytes(grp->prime)); + + /* (scal_p + scal_s) mod r */ + BN_add(scalar_sum, server_scalar, peer_scalar); + BN_mod(scalar_sum, scalar_sum, grp->order, bnctx); + os_memset(cruft, 0, BN_num_bytes(grp->prime)); + BN_bn2bin(scalar_sum, cruft); + H_Update(&ctx, cruft, BN_num_bytes(grp->order)); + H_Final(&ctx, mk); + + /* stretch the mk with the session-id to get MSK | EMSK */ + eap_pwd_kdf(mk, SHA256_DIGEST_LENGTH, + session_id, SHA256_DIGEST_LENGTH+1, + msk_emsk, (EAP_MSK_LEN + EAP_EMSK_LEN) * 8); + + os_memcpy(msk, msk_emsk, EAP_MSK_LEN); + os_memcpy(emsk, msk_emsk + EAP_MSK_LEN, EAP_EMSK_LEN); + + ret = 1; + +fail: + BN_free(x); + BN_free(scalar_sum); + EC_POINT_free(element_sum); + os_free(cruft); + + return ret; +} diff --git a/src/eap_common/eap_pwd_common.h b/src/eap_common/eap_pwd_common.h new file mode 100644 index 000000000..7389894a7 --- /dev/null +++ b/src/eap_common/eap_pwd_common.h @@ -0,0 +1,79 @@ +/* + * EAP server/peer: EAP-pwd shared definitions + * Copyright (c) 2009, Dan Harkins + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the BSD license. + * + * Alternatively, this software may be distributed under the terms of the + * GNU General Public License version 2 as published by the Free Software + * Foundation. + * + * See README and COPYING for more details. + */ + +#ifndef EAP_PWD_COMMON_H +#define EAP_PWD_COMMON_H + +#include +#include +#include +#include +#include + +/* + * definition of a finite cyclic group + * TODO: support one based on a prime field + */ +typedef struct group_definition_ { + u16 group_num; + EC_GROUP *group; + EC_POINT *pwe; + BIGNUM *order; + BIGNUM *prime; +} EAP_PWD_group; + +/* + * EAP-pwd header, included on all payloads + */ +struct eap_pwd_hdr { + u8 l_bit:1; + u8 m_bit:1; + u8 exch:6; + u8 total_length[0]; /* included when l_bit is set */ +} STRUCT_PACKED; + +#define EAP_PWD_OPCODE_ID_EXCH 1 +#define EAP_PWD_OPCODE_COMMIT_EXCH 2 +#define EAP_PWD_OPCODE_CONFIRM_EXCH 3 +#define EAP_PWD_GET_LENGTH_BIT(x) ((x)->lm_exch & 0x80) +#define EAP_PWD_SET_LENGTH_BIT(x) ((x)->lm_exch |= 0x80) +#define EAP_PWD_GET_MORE_BIT(x) ((x)->lm_exch & 0x40) +#define EAP_PWD_SET_MORE_BIT(x) ((x)->lm_exch |= 0x40) +#define EAP_PWD_GET_EXCHANGE(x) ((x)->lm_exch & 0x3f) +#define EAP_PWD_SET_EXCHANGE(x,y) ((x)->lm_exch |= (y)) + +/* EAP-pwd-ID payload */ +struct eap_pwd_id { + be16 group_num; + u8 random_function; +#define EAP_PWD_DEFAULT_RAND_FUNC 1 + u8 prf; +#define EAP_PWD_DEFAULT_PRF 1 + u8 token[4]; + u8 prep; +#define EAP_PWD_PREP_NONE 0 +#define EAP_PWD_PREP_MS 1 + u8 identity[0]; /* length inferred from payload */ +} STRUCT_PACKED; + +/* common routines */ +int compute_password_element(EAP_PWD_group *, u16, u8 *, int, u8 *, int, u8 *, + int, u8 *); +int compute_keys(EAP_PWD_group *, BN_CTX *, BIGNUM *, EC_POINT *, EC_POINT *, + BIGNUM *, BIGNUM *, u32 *, u8 *, u8 *); +void H_Init(HMAC_CTX *); +void H_Update(HMAC_CTX *, const u8 *, int); +void H_Final(HMAC_CTX *, u8 *); + +#endif /* EAP_PWD_COMMON_H */ diff --git a/src/eap_peer/eap_methods.h b/src/eap_peer/eap_methods.h index 384c61bb7..4330b5748 100644 --- a/src/eap_peer/eap_methods.h +++ b/src/eap_peer/eap_methods.h @@ -109,5 +109,6 @@ int eap_peer_wsc_register(void); int eap_peer_ikev2_register(void); int eap_peer_vendor_test_register(void); int eap_peer_tnc_register(void); +int eap_peer_pwd_register(void); #endif /* EAP_METHODS_H */ diff --git a/src/eap_peer/eap_pwd.c b/src/eap_peer/eap_pwd.c new file mode 100644 index 000000000..8d21b92b0 --- /dev/null +++ b/src/eap_peer/eap_pwd.c @@ -0,0 +1,730 @@ +/* + * EAP peer method: EAP-pwd (RFC 5931) + * Copyright (c) 2010, Dan Harkins + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the BSD license. + * + * Alternatively, this software may be distributed under the terms of the + * GNU General Public License version 2 as published by the Free Software + * Foundation. + * + * See README and COPYING for more details. + */ + +#include "includes.h" + +#include "common.h" +#include "eap_peer/eap_i.h" +#include "eap_common/eap_pwd_common.h" + + +struct eap_pwd_data { + enum { + PWD_ID_Req, PWD_Commit_Req, PWD_Confirm_Req, SUCCESS, FAILURE + } state; + u8 *id_peer; + size_t id_peer_len; + u8 *id_server; + size_t id_server_len; + u8 *password; + size_t password_len; + u16 group_num; + EAP_PWD_group *grp; + + BIGNUM *k; + BIGNUM *private_value; + BIGNUM *server_scalar; + BIGNUM *my_scalar; + EC_POINT *my_element; + EC_POINT *server_element; + + u8 msk[EAP_MSK_LEN]; + u8 emsk[EAP_EMSK_LEN]; +}; + +static BN_CTX *bnctx; + + +#ifndef CONFIG_NO_STDOUT_DEBUG +static const char * eap_pwd_state_txt(int state) +{ + switch (state) { + case PWD_ID_Req: + return "PWD-ID-Req"; + case PWD_Commit_Req: + return "PWD-Commit-Req"; + case PWD_Confirm_Req: + return "PWD-Confirm-Req"; + case SUCCESS: + return "SUCCESS"; + case FAILURE: + return "FAILURE"; + default: + return "PWD-UNK"; + } +} +#endif /* CONFIG_NO_STDOUT_DEBUG */ + + +static void eap_pwd_state(struct eap_pwd_data *data, int state) +{ + wpa_printf(MSG_INFO, "EAP-PWD: %s -> %s", + eap_pwd_state_txt(data->state), eap_pwd_state_txt(state)); + data->state = state; +} + + +static void * eap_pwd_init(struct eap_sm *sm) +{ + struct eap_pwd_data *data; + const u8 *identity, *password; + size_t identity_len, password_len; + + password = eap_get_config_password(sm, &password_len); + if (password == NULL) { + wpa_printf(MSG_INFO, "EAP-PWD: No password configured!"); + return NULL; + } + + identity = eap_get_config_identity(sm, &identity_len); + if (identity == NULL) { + wpa_printf(MSG_INFO, "EAP-PWD: No identity configured!"); + return NULL; + } + + if ((bnctx = BN_CTX_new()) == NULL) { + wpa_printf(MSG_INFO, "EAP-PWD: bn context allocation fail"); + return NULL; + } + + if ((data = os_zalloc(sizeof(*data))) == NULL) { + wpa_printf(MSG_INFO, "EAP-PWD: memory allocation data fail"); + return NULL; + } + + if ((data->id_peer = os_malloc(identity_len)) == NULL) { + wpa_printf(MSG_INFO, "EAP-PWD: memory allocation id fail"); + os_free(data); + return NULL; + } + + os_memcpy(data->id_peer, identity, identity_len); + data->id_peer_len = identity_len; + + if ((data->password = os_malloc(password_len)) == NULL) { + wpa_printf(MSG_INFO, "EAP-PWD: memory allocation psk fail"); + os_free(data->id_peer); + os_free(data); + return NULL; + } + os_memcpy(data->password, password, password_len); + data->password_len = password_len; + + data->state = PWD_ID_Req; + + return data; +} + + +static void eap_pwd_deinit(struct eap_sm *sm, void *priv) +{ + struct eap_pwd_data *data = priv; + + BN_free(data->private_value); + BN_free(data->server_scalar); + BN_free(data->my_scalar); + BN_free(data->k); + BN_CTX_free(bnctx); + EC_POINT_free(data->my_element); + EC_POINT_free(data->server_element); + os_free(data->id_peer); + os_free(data->password); + os_free(data->grp); + os_free(data); +} + + +static u8 * eap_pwd_getkey(struct eap_sm *sm, void *priv, size_t *len) +{ + struct eap_pwd_data *data = priv; + u8 *key; + + if (data->state != SUCCESS) + return NULL; + + key = os_malloc(EAP_MSK_LEN); + if (key == NULL) + return NULL; + + os_memcpy(key, data->msk, EAP_MSK_LEN); + *len = EAP_MSK_LEN; + + return key; +} + + +static struct wpabuf * +eap_pwd_perform_id_exchange(struct eap_sm *sm, struct eap_pwd_data *data, + struct eap_method_ret *ret, + const struct wpabuf *reqData, + const u8 *payload, size_t payload_len) +{ + struct eap_pwd_id *id; + struct wpabuf *resp; + + if (data->state != PWD_ID_Req) { + ret->ignore = TRUE; + return NULL; + } + + if (payload_len < sizeof(struct eap_pwd_id)) { + ret->ignore = TRUE; + return NULL; + } + + id = (struct eap_pwd_id *) payload; + data->group_num = be_to_host16(id->group_num); + if ((id->random_function != EAP_PWD_DEFAULT_RAND_FUNC) || + (id->prf != EAP_PWD_DEFAULT_PRF)) { + ret->ignore = TRUE; + return NULL; + } + + wpa_printf(MSG_DEBUG, "EAP-PWD (peer): server said group %d", + data->group_num); + + data->id_server = os_malloc(payload_len - sizeof(struct eap_pwd_id)); + if (data->id_server == NULL) { + wpa_printf(MSG_INFO, "EAP-PWD: memory allocation id fail"); + return NULL; + } + data->id_server_len = payload_len - sizeof(struct eap_pwd_id); + os_memcpy(data->id_server, id->identity, data->id_server_len); + wpa_hexdump_ascii(MSG_INFO, "EAP-PWD (peer): server sent id of", + data->id_server, data->id_server_len); + + if ((data->grp = (EAP_PWD_group *) os_malloc(sizeof(EAP_PWD_group))) == + NULL) { + wpa_printf(MSG_INFO, "EAP-PWD: failed to allocate memory for " + "group"); + return NULL; + } + + /* compute PWE */ + if (compute_password_element(data->grp, data->group_num, + data->password, data->password_len, + data->id_server, data->id_server_len, + data->id_peer, data->id_peer_len, + id->token)) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): unable to compute PWE"); + return NULL; + } + + wpa_printf(MSG_INFO, "EAP-PWD (peer): computed %d bit PWE...", + BN_num_bits(data->grp->prime)); + + resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD, + 1 + sizeof(struct eap_pwd_id) + data->id_peer_len, + EAP_CODE_RESPONSE, eap_get_id(reqData)); + if (resp == NULL) + return NULL; + + wpabuf_put_u8(resp, EAP_PWD_OPCODE_ID_EXCH); + wpabuf_put_be16(resp, data->group_num); + wpabuf_put_u8(resp, EAP_PWD_DEFAULT_RAND_FUNC); + wpabuf_put_u8(resp, EAP_PWD_DEFAULT_PRF); + wpabuf_put_data(resp, id->token, sizeof(id->token)); + wpabuf_put_u8(resp, EAP_PWD_PREP_NONE); + wpabuf_put_data(resp, data->id_peer, data->id_peer_len); + + eap_pwd_state(data, PWD_Commit_Req); + + return resp; +} + + +static struct wpabuf * +eap_pwd_perform_commit_exchange(struct eap_sm *sm, struct eap_pwd_data *data, + struct eap_method_ret *ret, + const struct wpabuf *reqData, + const u8 *payload, size_t payload_len) +{ + struct wpabuf *resp = NULL; + EC_POINT *K = NULL, *point = NULL; + BIGNUM *mask = NULL, *x = NULL, *y = NULL, *cofactor = NULL; + u16 offset; + u8 *ptr, *scalar = NULL, *element = NULL; + + if (((data->private_value = BN_new()) == NULL) || + ((data->my_element = EC_POINT_new(data->grp->group)) == NULL) || + ((cofactor = BN_new()) == NULL) || + ((data->my_scalar = BN_new()) == NULL) || + ((mask = BN_new()) == NULL)) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): scalar allocation fail"); + goto fin; + } + + if (!EC_GROUP_get_cofactor(data->grp->group, cofactor, NULL)) { + wpa_printf(MSG_INFO, "EAP-pwd (peer): unable to get cofactor " + "for curve"); + goto fin; + } + + BN_rand_range(data->private_value, data->grp->order); + BN_rand_range(mask, data->grp->order); + BN_add(data->my_scalar, data->private_value, mask); + BN_mod(data->my_scalar, data->my_scalar, data->grp->order, bnctx); + + if (!EC_POINT_mul(data->grp->group, data->my_element, NULL, + data->grp->pwe, mask, bnctx)) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): element allocation " + "fail"); + eap_pwd_state(data, FAILURE); + goto fin; + } + + if (!EC_POINT_invert(data->grp->group, data->my_element, bnctx)) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): element inversion fail"); + goto fin; + } + BN_free(mask); + + if (((x = BN_new()) == NULL) || + ((y = BN_new()) == NULL)) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): point allocation fail"); + goto fin; + } + + /* process the request */ + if (((data->server_scalar = BN_new()) == NULL) || + ((data->k = BN_new()) == NULL) || + ((K = EC_POINT_new(data->grp->group)) == NULL) || + ((point = EC_POINT_new(data->grp->group)) == NULL) || + ((data->server_element = EC_POINT_new(data->grp->group)) == NULL)) + { + wpa_printf(MSG_INFO, "EAP-PWD (peer): peer data allocation " + "fail"); + goto fin; + } + + /* element, x then y, followed by scalar */ + ptr = (u8 *) payload; + BN_bin2bn(ptr, BN_num_bytes(data->grp->prime), x); + ptr += BN_num_bytes(data->grp->prime); + BN_bin2bn(ptr, BN_num_bytes(data->grp->prime), y); + ptr += BN_num_bytes(data->grp->prime); + BN_bin2bn(ptr, BN_num_bytes(data->grp->order), data->server_scalar); + if (!EC_POINT_set_affine_coordinates_GFp(data->grp->group, + data->server_element, x, y, + bnctx)) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): setting peer element " + "fail"); + goto fin; + } + + /* check to ensure server's element is not in a small sub-group */ + if (BN_cmp(cofactor, BN_value_one())) { + if (!EC_POINT_mul(data->grp->group, point, NULL, + data->server_element, cofactor, NULL)) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): cannot multiply " + "server element by order!\n"); + goto fin; + } + if (EC_POINT_is_at_infinity(data->grp->group, point)) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): server element " + "is at infinity!\n"); + goto fin; + } + } + + /* compute the shared key, k */ + if ((!EC_POINT_mul(data->grp->group, K, NULL, data->grp->pwe, + data->server_scalar, bnctx)) || + (!EC_POINT_add(data->grp->group, K, K, data->server_element, + bnctx)) || + (!EC_POINT_mul(data->grp->group, K, NULL, K, data->private_value, + bnctx))) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): computing shared key " + "fail"); + goto fin; + } + + /* ensure that the shared key isn't in a small sub-group */ + if (BN_cmp(cofactor, BN_value_one())) { + if (!EC_POINT_mul(data->grp->group, K, NULL, K, cofactor, + NULL)) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): cannot multiply " + "shared key point by order"); + goto fin; + } + } + + /* + * This check is strictly speaking just for the case above where + * co-factor > 1 but it was suggested that even though this is probably + * never going to happen it is a simple and safe check "just to be + * sure" so let's be safe. + */ + if (EC_POINT_is_at_infinity(data->grp->group, K)) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): shared key point is at " + "infinity!\n"); + goto fin; + } + + if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group, K, data->k, + NULL, bnctx)) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): unable to extract " + "shared secret from point"); + goto fin; + } + + /* now do the response */ + if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group, + data->my_element, x, y, + bnctx)) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): point assignment fail"); + goto fin; + } + + if (((scalar = os_malloc(BN_num_bytes(data->grp->order))) == NULL) || + ((element = os_malloc(BN_num_bytes(data->grp->prime) * 2)) == + NULL)) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): data allocation fail"); + goto fin; + } + + /* + * bignums occupy as little memory as possible so one that is + * sufficiently smaller than the prime or order might need pre-pending + * with zeros. + */ + os_memset(scalar, 0, BN_num_bytes(data->grp->order)); + os_memset(element, 0, BN_num_bytes(data->grp->prime) * 2); + offset = BN_num_bytes(data->grp->order) - + BN_num_bytes(data->my_scalar); + BN_bn2bin(data->my_scalar, scalar + offset); + + offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(x); + BN_bn2bin(x, element + offset); + offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(y); + BN_bn2bin(y, element + BN_num_bytes(data->grp->prime) + offset); + + resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD, + sizeof(struct eap_pwd_hdr) + + BN_num_bytes(data->grp->order) + + (2 * BN_num_bytes(data->grp->prime)), + EAP_CODE_RESPONSE, eap_get_id(reqData)); + if (resp == NULL) + goto fin; + + wpabuf_put_u8(resp, EAP_PWD_OPCODE_COMMIT_EXCH); + + /* we send the element as (x,y) follwed by the scalar */ + wpabuf_put_data(resp, element, (2 * BN_num_bytes(data->grp->prime))); + wpabuf_put_data(resp, scalar, BN_num_bytes(data->grp->order)); + +fin: + os_free(scalar); + os_free(element); + BN_free(x); + BN_free(y); + BN_free(cofactor); + EC_POINT_free(K); + EC_POINT_free(point); + if (resp == NULL) + eap_pwd_state(data, FAILURE); + else + eap_pwd_state(data, PWD_Confirm_Req); + + return resp; +} + + +static struct wpabuf * +eap_pwd_perform_confirm_exchange(struct eap_sm *sm, struct eap_pwd_data *data, + struct eap_method_ret *ret, + const struct wpabuf *reqData, + const u8 *payload, size_t payload_len) +{ + struct wpabuf *resp = NULL; + BIGNUM *x = NULL, *y = NULL; + HMAC_CTX ctx; + u32 cs; + u8 conf[SHA256_DIGEST_LENGTH], *cruft = NULL, *ptr; + + /* + * first build up the ciphersuite which is group | random_function | + * prf + */ + ptr = (u8 *) &cs; + os_memcpy(ptr, &data->group_num, sizeof(u16)); + ptr += sizeof(u16); + *ptr = EAP_PWD_DEFAULT_RAND_FUNC; + ptr += sizeof(u8); + *ptr = EAP_PWD_DEFAULT_PRF; + + /* each component of the cruft will be at most as big as the prime */ + if (((cruft = os_malloc(BN_num_bytes(data->grp->prime))) == NULL) || + ((x = BN_new()) == NULL) || ((y = BN_new()) == NULL)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): debug allocation " + "fail"); + goto fin; + } + + /* + * server's commit is H(k | server_element | server_scalar | + * peer_element | peer_scalar | ciphersuite) + */ + H_Init(&ctx); + + /* + * zero the memory each time because this is mod prime math and some + * value may start with a few zeros and the previous one did not. + */ + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(data->k, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + + /* server element: x, y */ + if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group, + data->server_element, x, y, + bnctx)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point " + "assignment fail"); + goto fin; + } + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(x, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(y, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + + /* server scalar */ + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(data->server_scalar, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->order)); + + /* my element: x, y */ + if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group, + data->my_element, x, y, + bnctx)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point " + "assignment fail"); + goto fin; + } + + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(x, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(y, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + + /* my scalar */ + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(data->my_scalar, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->order)); + + /* the ciphersuite */ + H_Update(&ctx, (u8 *) &cs, sizeof(u32)); + + /* random function fin */ + H_Final(&ctx, conf); + + ptr = (u8 *) payload; + if (os_memcmp(conf, ptr, SHA256_DIGEST_LENGTH)) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): confirm did not verify"); + goto fin; + } + + wpa_printf(MSG_DEBUG, "EAP-pwd (peer): confirm verified"); + + /* + * compute confirm: + * H(k | peer_element | peer_scalar | server_element | server_scalar | + * ciphersuite) + */ + H_Init(&ctx); + + /* k */ + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(data->k, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + + /* my element */ + if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group, + data->my_element, x, y, + bnctx)) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): confirm point " + "assignment fail"); + goto fin; + } + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(x, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(y, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + + /* my scalar */ + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(data->my_scalar, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->order)); + + /* server element: x, y */ + if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group, + data->server_element, x, y, + bnctx)) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): confirm point " + "assignment fail"); + goto fin; + } + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(x, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(y, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + + /* server scalar */ + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(data->server_scalar, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->order)); + + /* the ciphersuite */ + H_Update(&ctx, (u8 *) &cs, sizeof(u32)); + + /* all done */ + H_Final(&ctx, conf); + + resp = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD, + sizeof(struct eap_pwd_hdr) + SHA256_DIGEST_LENGTH, + EAP_CODE_RESPONSE, eap_get_id(reqData)); + if (resp == NULL) + goto fin; + + wpabuf_put_u8(resp, EAP_PWD_OPCODE_CONFIRM_EXCH); + wpabuf_put_data(resp, conf, SHA256_DIGEST_LENGTH); + + if (compute_keys(data->grp, bnctx, data->k, data->server_element, + data->my_element, data->server_scalar, + data->my_scalar, &cs, data->msk, data->emsk) < 0) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): unable to compute MSK | " + "EMSK"); + goto fin; + } + +fin: + os_free(cruft); + BN_free(x); + BN_free(y); + ret->methodState = METHOD_DONE; + if (resp == NULL) { + ret->decision = DECISION_FAIL; + eap_pwd_state(data, FAILURE); + } else { + ret->decision = DECISION_UNCOND_SUCC; + eap_pwd_state(data, SUCCESS); + } + + return resp; +} + + +static struct wpabuf * +eap_pwd_process(struct eap_sm *sm, void *priv, struct eap_method_ret *ret, + const struct wpabuf *reqData) +{ + struct eap_pwd_data *data = priv; + struct wpabuf *resp = NULL; + const u8 *pos; + size_t len; + u8 exch; + + pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PWD, reqData, &len); + if ((pos == NULL) || (len < 1)) { + ret->ignore = TRUE; + return NULL; + } + + wpa_printf(MSG_INFO, "EAP-pwd: Received frame: opcode %d", *pos); + + ret->ignore = FALSE; + ret->methodState = METHOD_MAY_CONT; + ret->decision = DECISION_FAIL; + ret->allowNotifications = FALSE; + + exch = *pos & 0x3f; + switch (exch) { + case EAP_PWD_OPCODE_ID_EXCH: + resp = eap_pwd_perform_id_exchange(sm, data, ret, reqData, + pos + 1, len - 1); + break; + case EAP_PWD_OPCODE_COMMIT_EXCH: + resp = eap_pwd_perform_commit_exchange(sm, data, ret, reqData, + pos + 1, len - 1); + break; + case EAP_PWD_OPCODE_CONFIRM_EXCH: + resp = eap_pwd_perform_confirm_exchange(sm, data, ret, reqData, + pos + 1, len - 1); + break; + default: + wpa_printf(MSG_INFO, "EAP-pwd: Ignoring message with unknown " + "opcode %d", exch); + break; + } + + return resp; +} + + +static Boolean eap_pwd_key_available(struct eap_sm *sm, void *priv) +{ + struct eap_pwd_data *data = priv; + return data->state == SUCCESS; +} + + +static u8 * eap_pwd_get_emsk(struct eap_sm *sm, void *priv, size_t *len) +{ + struct eap_pwd_data *data = priv; + u8 *key; + + if (data->state != SUCCESS) + return NULL; + + if ((key = os_malloc(EAP_EMSK_LEN)) == NULL) + return NULL; + + os_memcpy(key, data->emsk, EAP_EMSK_LEN); + *len = EAP_EMSK_LEN; + + return key; +} + + +int eap_peer_pwd_register(void) +{ + struct eap_method *eap; + int ret; + + EVP_add_digest(EVP_sha256()); + eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION, + EAP_VENDOR_IETF, EAP_TYPE_PWD, "PWD"); + if (eap == NULL) + return -1; + + eap->init = eap_pwd_init; + eap->deinit = eap_pwd_deinit; + eap->process = eap_pwd_process; + eap->isKeyAvailable = eap_pwd_key_available; + eap->getKey = eap_pwd_getkey; + eap->get_emsk = eap_pwd_get_emsk; + + ret = eap_peer_method_register(eap); + if (ret) + eap_peer_method_free(eap); + return ret; +} diff --git a/src/eap_server/eap.h b/src/eap_server/eap.h index 8a346716b..0c0992350 100644 --- a/src/eap_server/eap.h +++ b/src/eap_server/eap.h @@ -95,6 +95,7 @@ struct eap_config { void *eap_sim_db_priv; Boolean backend_auth; int eap_server; + u16 pwd_group; u8 *pac_opaque_encr_key; u8 *eap_fast_a_id; size_t eap_fast_a_id_len; diff --git a/src/eap_server/eap_i.h b/src/eap_server/eap_i.h index 3b3e49c64..daac746dc 100644 --- a/src/eap_server/eap_i.h +++ b/src/eap_server/eap_i.h @@ -181,6 +181,7 @@ struct eap_sm { int pac_key_refresh_time; int eap_sim_aka_result_ind; int tnc; + u16 pwd_group; struct wps_context *wps; struct wpabuf *assoc_wps_ie; struct wpabuf *assoc_p2p_ie; diff --git a/src/eap_server/eap_methods.h b/src/eap_server/eap_methods.h index 5d4d92cbf..4a5296e58 100644 --- a/src/eap_server/eap_methods.h +++ b/src/eap_server/eap_methods.h @@ -49,5 +49,6 @@ int eap_server_fast_register(void); int eap_server_wsc_register(void); int eap_server_ikev2_register(void); int eap_server_tnc_register(void); +int eap_server_pwd_register(void); #endif /* EAP_SERVER_METHODS_H */ diff --git a/src/eap_server/eap_server.c b/src/eap_server/eap_server.c index 1d19251d0..5812b3375 100644 --- a/src/eap_server/eap_server.c +++ b/src/eap_server/eap_server.c @@ -1260,6 +1260,7 @@ struct eap_sm * eap_server_sm_init(void *eapol_ctx, if (conf->peer_addr) os_memcpy(sm->peer_addr, conf->peer_addr, ETH_ALEN); sm->fragment_size = conf->fragment_size; + sm->pwd_group = conf->pwd_group; wpa_printf(MSG_DEBUG, "EAP: Server state machine created"); diff --git a/src/eap_server/eap_server_pwd.c b/src/eap_server/eap_server_pwd.c new file mode 100644 index 000000000..d1b89ea49 --- /dev/null +++ b/src/eap_server/eap_server_pwd.c @@ -0,0 +1,823 @@ +/* + * hostapd / EAP-pwd (RFC 5931) server + * Copyright (c) 2010, Dan Harkins + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the BSD license. + * + * Alternatively, this software may be distributed under the terms of the + * GNU General Public License version 2 as published by the Free Software + * Foundation. + * + * See README and COPYING for more details. + */ + +#include "includes.h" + +#include "common.h" +#include "eap_server/eap_i.h" +#include "eap_common/eap_pwd_common.h" + + +struct eap_pwd_data { + enum { + PWD_ID_Req, PWD_Commit_Req, PWD_Confirm_Req, SUCCESS, FAILURE + } state; + u8 *id_peer; + size_t id_peer_len; + u8 *id_server; + size_t id_server_len; + u8 *password; + size_t password_len; + u32 token; + u16 group_num; + EAP_PWD_group *grp; + + BIGNUM *k; + BIGNUM *private_value; + BIGNUM *peer_scalar; + BIGNUM *my_scalar; + EC_POINT *my_element; + EC_POINT *peer_element; + + u8 msk[EAP_MSK_LEN]; + u8 emsk[EAP_EMSK_LEN]; +}; + +static BN_CTX *bnctx; + + +static const char * eap_pwd_state_txt(int state) +{ + switch (state) { + case PWD_ID_Req: + return "PWD-ID-Req"; + case PWD_Commit_Req: + return "PWD-Commit-Req"; + case PWD_Confirm_Req: + return "PWD-Confirm-Req"; + case SUCCESS: + return "SUCCESS"; + case FAILURE: + return "FAILURE"; + default: + return "PWD-Unk"; + } +} + + +static void eap_pwd_state(struct eap_pwd_data *data, int state) +{ + wpa_printf(MSG_DEBUG, "EAP-pwd: %s -> %s", + eap_pwd_state_txt(data->state), eap_pwd_state_txt(state)); + data->state = state; +} + + +static void * eap_pwd_init(struct eap_sm *sm) +{ + struct eap_pwd_data *data; + + if (sm->user == NULL || sm->user->password == NULL || + sm->user->password_len == 0) { + wpa_printf(MSG_INFO, "EAP-PWD (server): Password is not " + "configured"); + return NULL; + } + + data = os_zalloc(sizeof(*data)); + if (data == NULL) + return NULL; + + data->group_num = sm->pwd_group; + wpa_printf(MSG_DEBUG, "EAP-pwd: Selected group number %d", + data->group_num); + data->state = PWD_ID_Req; + + data->id_server = (u8 *) os_strdup("server"); + if (data->id_server) + data->id_server_len = os_strlen((char *)data->id_server); + + data->password = os_malloc(sm->user->password_len); + if (data->password == NULL) { + wpa_printf(MSG_INFO, "EAP-PWD: Mmemory allocation password " + "fail"); + return NULL; + } + data->password_len = sm->user->password_len; + os_memcpy(data->password, sm->user->password, data->password_len); + + bnctx = BN_CTX_new(); + if (bnctx == NULL) { + wpa_printf(MSG_INFO, "EAP-PWD: bn context allocation fail"); + return NULL; + } + + return data; +} + + +static void eap_pwd_reset(struct eap_sm *sm, void *priv) +{ + struct eap_pwd_data *data = priv; + + BN_free(data->private_value); + BN_free(data->peer_scalar); + BN_free(data->my_scalar); + BN_free(data->k); + BN_CTX_free(bnctx); + EC_POINT_free(data->my_element); + EC_POINT_free(data->peer_element); + os_free(data->id_peer); + os_free(data->id_server); + os_free(data->grp); + os_free(data); +} + + +static struct wpabuf * +eap_pwd_build_id_req(struct eap_sm *sm, struct eap_pwd_data *data, u8 id) +{ + struct wpabuf *req; + + wpa_printf(MSG_DEBUG, "EAP-pwd: ID/Request"); + req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD, + sizeof(struct eap_pwd_hdr) + + sizeof(struct eap_pwd_id) + data->id_server_len, + EAP_CODE_REQUEST, id); + if (req == NULL) { + eap_pwd_state(data, FAILURE); + return NULL; + } + + /* an lfsr is good enough to generate unpredictable tokens */ + data->token = os_random(); + wpabuf_put_u8(req, EAP_PWD_OPCODE_ID_EXCH); + wpabuf_put_be16(req, data->group_num); + wpabuf_put_u8(req, EAP_PWD_DEFAULT_RAND_FUNC); + wpabuf_put_u8(req, EAP_PWD_DEFAULT_PRF); + wpabuf_put_data(req, &data->token, sizeof(data->token)); + wpabuf_put_u8(req, EAP_PWD_PREP_NONE); + wpabuf_put_data(req, data->id_server, data->id_server_len); + + return req; +} + + +static struct wpabuf * +eap_pwd_build_commit_req(struct eap_sm *sm, struct eap_pwd_data *data, u8 id) +{ + struct wpabuf *req = NULL; + BIGNUM *mask = NULL, *x = NULL, *y = NULL; + u8 *scalar = NULL, *element = NULL; + u16 offset; + + wpa_printf(MSG_DEBUG, "EAP-pwd: Commit/Request"); + + if (((data->private_value = BN_new()) == NULL) || + ((data->my_element = EC_POINT_new(data->grp->group)) == NULL) || + ((data->my_scalar = BN_new()) == NULL) || + ((mask = BN_new()) == NULL)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): scalar allocation " + "fail"); + goto fin; + } + + BN_rand_range(data->private_value, data->grp->order); + BN_rand_range(mask, data->grp->order); + BN_add(data->my_scalar, data->private_value, mask); + BN_mod(data->my_scalar, data->my_scalar, data->grp->order, bnctx); + + if (!EC_POINT_mul(data->grp->group, data->my_element, NULL, + data->grp->pwe, mask, bnctx)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): element allocation " + "fail"); + eap_pwd_state(data, FAILURE); + goto fin; + } + + if (!EC_POINT_invert(data->grp->group, data->my_element, bnctx)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): element inversion " + "fail"); + goto fin; + } + BN_free(mask); + + if (((x = BN_new()) == NULL) || + ((y = BN_new()) == NULL)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): point allocation " + "fail"); + goto fin; + } + if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group, + data->my_element, x, y, + bnctx)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): point assignment " + "fail"); + goto fin; + } + + if (((scalar = os_malloc(BN_num_bytes(data->grp->order))) == NULL) || + ((element = os_malloc(BN_num_bytes(data->grp->prime) * 2)) == + NULL)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): data allocation fail"); + goto fin; + } + + /* + * bignums occupy as little memory as possible so one that is + * sufficiently smaller than the prime or order might need pre-pending + * with zeros. + */ + os_memset(scalar, 0, BN_num_bytes(data->grp->order)); + os_memset(element, 0, BN_num_bytes(data->grp->prime) * 2); + offset = BN_num_bytes(data->grp->order) - + BN_num_bytes(data->my_scalar); + BN_bn2bin(data->my_scalar, scalar + offset); + + offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(x); + BN_bn2bin(x, element + offset); + offset = BN_num_bytes(data->grp->prime) - BN_num_bytes(y); + BN_bn2bin(y, element + BN_num_bytes(data->grp->prime) + offset); + + req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD, + sizeof(struct eap_pwd_hdr) + + (2 * BN_num_bytes(data->grp->prime)) + + BN_num_bytes(data->grp->order), + EAP_CODE_REQUEST, id); + if (req == NULL) + goto fin; + wpabuf_put_u8(req, EAP_PWD_OPCODE_COMMIT_EXCH); + + /* We send the element as (x,y) followed by the scalar */ + wpabuf_put_data(req, element, (2 * BN_num_bytes(data->grp->prime))); + wpabuf_put_data(req, scalar, BN_num_bytes(data->grp->order)); + +fin: + os_free(scalar); + os_free(element); + BN_free(x); + BN_free(y); + if (req == NULL) + eap_pwd_state(data, FAILURE); + + return req; +} + + +static struct wpabuf * +eap_pwd_build_confirm_req(struct eap_sm *sm, struct eap_pwd_data *data, u8 id) +{ + struct wpabuf *req = NULL; + BIGNUM *x = NULL, *y = NULL; + HMAC_CTX ctx; + u8 conf[SHA256_DIGEST_LENGTH], *cruft = NULL, *ptr; + + wpa_printf(MSG_DEBUG, "EAP-pwd: Confirm/Request"); + + /* Each component of the cruft will be at most as big as the prime */ + if (((cruft = os_malloc(BN_num_bytes(data->grp->prime))) == NULL) || + ((x = BN_new()) == NULL) || ((y = BN_new()) == NULL)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): debug allocation " + "fail"); + goto fin; + } + + /* + * commit is H(k | server_element | server_scalar | peer_element | + * peer_scalar | ciphersuite) + */ + H_Init(&ctx); + + /* + * Zero the memory each time because this is mod prime math and some + * value may start with a few zeros and the previous one did not. + * + * First is k + */ + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(data->k, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + + /* server element: x, y */ + if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group, + data->my_element, x, y, + bnctx)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point " + "assignment fail"); + goto fin; + } + + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(x, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(y, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + + /* server scalar */ + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(data->my_scalar, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->order)); + + /* peer element: x, y */ + if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group, + data->peer_element, x, y, + bnctx)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point " + "assignment fail"); + goto fin; + } + + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(x, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(y, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + + /* peer scalar */ + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(data->peer_scalar, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->order)); + + /* ciphersuite */ + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + ptr = cruft; + os_memcpy(ptr, &data->group_num, sizeof(u16)); + ptr += sizeof(u16); + *ptr = EAP_PWD_DEFAULT_RAND_FUNC; + ptr += sizeof(u8); + *ptr = EAP_PWD_DEFAULT_PRF; + ptr += sizeof(u8); + H_Update(&ctx, cruft, ptr-cruft); + + /* all done with the random function */ + H_Final(&ctx, conf); + + req = eap_msg_alloc(EAP_VENDOR_IETF, EAP_TYPE_PWD, + sizeof(struct eap_pwd_hdr) + SHA256_DIGEST_LENGTH, + EAP_CODE_REQUEST, id); + if (req == NULL) + goto fin; + + wpabuf_put_u8(req, EAP_PWD_OPCODE_CONFIRM_EXCH); + wpabuf_put_data(req, conf, SHA256_DIGEST_LENGTH); + +fin: + os_free(cruft); + BN_free(x); + BN_free(y); + if (req == NULL) + eap_pwd_state(data, FAILURE); + + return req; +} + + +static struct wpabuf * +eap_pwd_build_req(struct eap_sm *sm, void *priv, u8 id) +{ + struct eap_pwd_data *data = priv; + + switch (data->state) { + case PWD_ID_Req: + return eap_pwd_build_id_req(sm, data, id); + case PWD_Commit_Req: + return eap_pwd_build_commit_req(sm, data, id); + case PWD_Confirm_Req: + return eap_pwd_build_confirm_req(sm, data, id); + default: + wpa_printf(MSG_INFO, "EAP-pwd: Unknown state %d in build_req", + data->state); + break; + } + + return NULL; +} + + +static Boolean eap_pwd_check(struct eap_sm *sm, void *priv, + struct wpabuf *respData) +{ + struct eap_pwd_data *data = priv; + const u8 *pos; + size_t len; + + pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PWD, respData, &len); + if (pos == NULL || len < 1) { + wpa_printf(MSG_INFO, "EAP-pwd: Invalid frame"); + return TRUE; + } + + wpa_printf(MSG_DEBUG, "EAP-pwd: Received frame: opcode=%d", *pos); + + if (data->state == PWD_ID_Req && *pos == EAP_PWD_OPCODE_ID_EXCH) + return FALSE; + + if (data->state == PWD_Commit_Req && + *pos == EAP_PWD_OPCODE_COMMIT_EXCH) + return FALSE; + + if (data->state == PWD_Confirm_Req && + *pos == EAP_PWD_OPCODE_CONFIRM_EXCH) + return FALSE; + + wpa_printf(MSG_INFO, "EAP-pwd: Unexpected opcode=%d in state=%d", + *pos, data->state); + + return TRUE; +} + + +static void eap_pwd_process_id_resp(struct eap_sm *sm, + struct eap_pwd_data *data, + const u8 *payload, size_t payload_len) +{ + struct eap_pwd_id *id; + + if (payload_len < sizeof(struct eap_pwd_id)) { + wpa_printf(MSG_INFO, "EAP-pwd: Invalid ID response"); + return; + } + + id = (struct eap_pwd_id *) payload; + if ((data->group_num != be_to_host16(id->group_num)) || + (id->random_function != EAP_PWD_DEFAULT_RAND_FUNC) || + (os_memcmp(id->token, (u8 *)&data->token, sizeof(data->token))) || + (id->prf != EAP_PWD_DEFAULT_PRF)) { + wpa_printf(MSG_INFO, "EAP-pwd: peer changed parameters"); + eap_pwd_state(data, FAILURE); + return; + } + data->id_peer = os_malloc(payload_len - sizeof(struct eap_pwd_id)); + if (data->id_peer == NULL) { + wpa_printf(MSG_INFO, "EAP-PWD: memory allocation id fail"); + return; + } + data->id_peer_len = payload_len - sizeof(struct eap_pwd_id); + os_memcpy(data->id_peer, id->identity, data->id_peer_len); + wpa_hexdump_ascii(MSG_DEBUG, "EAP-PWD (server): peer sent id of", + data->id_peer, data->id_peer_len); + + if ((data->grp = os_malloc(sizeof(EAP_PWD_group))) == NULL) { + wpa_printf(MSG_INFO, "EAP-PWD: failed to allocate memory for " + "group"); + return; + } + if (compute_password_element(data->grp, data->group_num, + data->password, data->password_len, + data->id_server, data->id_server_len, + data->id_peer, data->id_peer_len, + (u8 *) &data->token)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): unable to compute " + "PWE"); + return; + } + wpa_printf(MSG_DEBUG, "EAP-PWD (server): computed %d bit PWE...", + BN_num_bits(data->grp->prime)); + + eap_pwd_state(data, PWD_Commit_Req); +} + + +static void +eap_pwd_process_commit_resp(struct eap_sm *sm, struct eap_pwd_data *data, + const u8 *payload, size_t payload_len) +{ + u8 *ptr; + BIGNUM *x = NULL, *y = NULL, *cofactor = NULL; + EC_POINT *K = NULL, *point = NULL; + int res = 0; + + wpa_printf(MSG_DEBUG, "EAP-pwd: Received commit response"); + + if (((data->peer_scalar = BN_new()) == NULL) || + ((data->k = BN_new()) == NULL) || + ((cofactor = BN_new()) == NULL) || + ((x = BN_new()) == NULL) || + ((y = BN_new()) == NULL) || + ((point = EC_POINT_new(data->grp->group)) == NULL) || + ((K = EC_POINT_new(data->grp->group)) == NULL) || + ((data->peer_element = EC_POINT_new(data->grp->group)) == NULL)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): peer data allocation " + "fail"); + goto fin; + } + + if (!EC_GROUP_get_cofactor(data->grp->group, cofactor, NULL)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): unable to get " + "cofactor for curve"); + goto fin; + } + + /* element, x then y, followed by scalar */ + ptr = (u8 *) payload; + BN_bin2bn(ptr, BN_num_bytes(data->grp->prime), x); + ptr += BN_num_bytes(data->grp->prime); + BN_bin2bn(ptr, BN_num_bytes(data->grp->prime), y); + ptr += BN_num_bytes(data->grp->prime); + BN_bin2bn(ptr, BN_num_bytes(data->grp->order), data->peer_scalar); + if (!EC_POINT_set_affine_coordinates_GFp(data->grp->group, + data->peer_element, x, y, + bnctx)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): setting peer element " + "fail"); + goto fin; + } + + /* check to ensure peer's element is not in a small sub-group */ + if (BN_cmp(cofactor, BN_value_one())) { + if (!EC_POINT_mul(data->grp->group, point, NULL, + data->peer_element, cofactor, NULL)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): cannot " + "multiply peer element by order"); + goto fin; + } + if (EC_POINT_is_at_infinity(data->grp->group, point)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): peer element " + "is at infinity!\n"); + goto fin; + } + } + + /* compute the shared key, k */ + if ((!EC_POINT_mul(data->grp->group, K, NULL, data->grp->pwe, + data->peer_scalar, bnctx)) || + (!EC_POINT_add(data->grp->group, K, K, data->peer_element, + bnctx)) || + (!EC_POINT_mul(data->grp->group, K, NULL, K, data->private_value, + bnctx))) { + wpa_printf(MSG_INFO, "EAP-PWD (server): computing shared key " + "fail"); + goto fin; + } + + /* ensure that the shared key isn't in a small sub-group */ + if (BN_cmp(cofactor, BN_value_one())) { + if (!EC_POINT_mul(data->grp->group, K, NULL, K, cofactor, + NULL)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): cannot " + "multiply shared key point by order!\n"); + goto fin; + } + } + + /* + * This check is strictly speaking just for the case above where + * co-factor > 1 but it was suggested that even though this is probably + * never going to happen it is a simple and safe check "just to be + * sure" so let's be safe. + */ + if (EC_POINT_is_at_infinity(data->grp->group, K)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): shared key point is " + "at infinity"); + goto fin; + } + if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group, K, data->k, + NULL, bnctx)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): unable to extract " + "shared secret from secret point"); + goto fin; + } + res = 1; + +fin: + EC_POINT_free(K); + EC_POINT_free(point); + BN_free(cofactor); + BN_free(x); + BN_free(y); + + if (res) + eap_pwd_state(data, PWD_Confirm_Req); + else + eap_pwd_state(data, FAILURE); +} + + +static void +eap_pwd_process_confirm_resp(struct eap_sm *sm, struct eap_pwd_data *data, + const u8 *payload, size_t payload_len) +{ + BIGNUM *x = NULL, *y = NULL; + HMAC_CTX ctx; + u32 cs; + u8 conf[SHA256_DIGEST_LENGTH], *cruft = NULL, *ptr; + + /* build up the ciphersuite: group | random_function | prf */ + ptr = (u8 *) &cs; + os_memcpy(ptr, &data->group_num, sizeof(u16)); + ptr += sizeof(u16); + *ptr = EAP_PWD_DEFAULT_RAND_FUNC; + ptr += sizeof(u8); + *ptr = EAP_PWD_DEFAULT_PRF; + + /* each component of the cruft will be at most as big as the prime */ + if (((cruft = os_malloc(BN_num_bytes(data->grp->prime))) == NULL) || + ((x = BN_new()) == NULL) || ((y = BN_new()) == NULL)) { + wpa_printf(MSG_INFO, "EAP-PWD (peer): allocation fail"); + goto fin; + } + + /* + * commit is H(k | peer_element | peer_scalar | server_element | + * server_scalar | ciphersuite) + */ + H_Init(&ctx); + + /* k */ + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(data->k, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + + /* peer element: x, y */ + if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group, + data->peer_element, x, y, + bnctx)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point " + "assignment fail"); + goto fin; + } + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(x, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(y, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + + /* peer scalar */ + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(data->peer_scalar, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->order)); + + /* server element: x, y */ + if (!EC_POINT_get_affine_coordinates_GFp(data->grp->group, + data->my_element, x, y, + bnctx)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): confirm point " + "assignment fail"); + goto fin; + } + + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(x, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(y, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->prime)); + + /* server scalar */ + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + BN_bn2bin(data->my_scalar, cruft); + H_Update(&ctx, cruft, BN_num_bytes(data->grp->order)); + + /* ciphersuite */ + os_memset(cruft, 0, BN_num_bytes(data->grp->prime)); + H_Update(&ctx, (u8 *)&cs, sizeof(u32)); + + /* all done */ + H_Final(&ctx, conf); + + ptr = (u8 *) payload; + if (os_memcmp(conf, ptr, SHA256_DIGEST_LENGTH)) { + wpa_printf(MSG_INFO, "EAP-PWD (server): confirm did not " + "verify"); + goto fin; + } + + wpa_printf(MSG_DEBUG, "EAP-pwd (server): confirm verified"); + if (compute_keys(data->grp, bnctx, data->k, data->my_element, + data->peer_element, data->my_scalar, + data->peer_scalar, &cs, data->msk, data->emsk) < 0) + eap_pwd_state(data, FAILURE); + else + eap_pwd_state(data, SUCCESS); + +fin: + os_free(cruft); + BN_free(x); + BN_free(y); +} + + +static void eap_pwd_process(struct eap_sm *sm, void *priv, + struct wpabuf *respData) +{ + struct eap_pwd_data *data = priv; + const u8 *pos; + size_t len; + u8 exch; + + pos = eap_hdr_validate(EAP_VENDOR_IETF, EAP_TYPE_PWD, respData, &len); + if ((pos == NULL) || (len < 1)) { + wpa_printf(MSG_INFO, "Bad EAP header! pos %s and len = %d", + (pos == NULL) ? "is NULL" : "is not NULL", + (int) len); + return; + } + + exch = *pos & 0x3f; + switch (exch) { + case EAP_PWD_OPCODE_ID_EXCH: + eap_pwd_process_id_resp(sm, data, pos + 1, len - 1); + break; + case EAP_PWD_OPCODE_COMMIT_EXCH: + eap_pwd_process_commit_resp(sm, data, pos + 1, len - 1); + break; + case EAP_PWD_OPCODE_CONFIRM_EXCH: + eap_pwd_process_confirm_resp(sm, data, pos + 1, len - 1); + break; + } +} + + +static u8 * eap_pwd_getkey(struct eap_sm *sm, void *priv, size_t *len) +{ + struct eap_pwd_data *data = priv; + u8 *key; + + if (data->state != SUCCESS) + return NULL; + + key = os_malloc(EAP_MSK_LEN); + if (key == NULL) + return NULL; + + os_memcpy(key, data->msk, EAP_MSK_LEN); + *len = EAP_MSK_LEN; + + return key; +} + + +static u8 * eap_pwd_get_emsk(struct eap_sm *sm, void *priv, size_t *len) +{ + struct eap_pwd_data *data = priv; + u8 *key; + + if (data->state != SUCCESS) + return NULL; + + key = os_malloc(EAP_EMSK_LEN); + if (key == NULL) + return NULL; + + os_memcpy(key, data->emsk, EAP_EMSK_LEN); + *len = EAP_EMSK_LEN; + + return key; +} + + +static Boolean eap_pwd_is_success(struct eap_sm *sm, void *priv) +{ + struct eap_pwd_data *data = priv; + return data->state == SUCCESS; +} + + +static Boolean eap_pwd_is_done(struct eap_sm *sm, void *priv) +{ + struct eap_pwd_data *data = priv; + return (data->state == SUCCESS) || (data->state == FAILURE); +} + + +int eap_server_pwd_register(void) +{ + struct eap_method *eap; + int ret; + struct timeval tp; + struct timezone tz; + u32 sr; + + EVP_add_digest(EVP_sha256()); + + sr = 0xdeaddada; + (void) gettimeofday(&tp, &tz); + sr ^= (tp.tv_sec ^ tp.tv_usec); + srandom(sr); + + eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION, + EAP_VENDOR_IETF, EAP_TYPE_PWD, + "PWD"); + if (eap == NULL) + return -1; + + eap->init = eap_pwd_init; + eap->reset = eap_pwd_reset; + eap->buildReq = eap_pwd_build_req; + eap->check = eap_pwd_check; + eap->process = eap_pwd_process; + eap->isDone = eap_pwd_is_done; + eap->getKey = eap_pwd_getkey; + eap->get_emsk = eap_pwd_get_emsk; + eap->isSuccess = eap_pwd_is_success; + + ret = eap_server_method_register(eap); + if (ret) + eap_server_method_free(eap); + return ret; +} + diff --git a/src/eapol_auth/eapol_auth_sm.c b/src/eapol_auth/eapol_auth_sm.c index a3b1c9270..841a1c515 100644 --- a/src/eapol_auth/eapol_auth_sm.c +++ b/src/eapol_auth/eapol_auth_sm.c @@ -833,6 +833,7 @@ eapol_auth_alloc(struct eapol_authenticator *eapol, const u8 *addr, eap_conf.assoc_p2p_ie = assoc_p2p_ie; eap_conf.peer_addr = addr; eap_conf.fragment_size = eapol->conf.fragment_size; + eap_conf.pwd_group = eapol->conf.pwd_group; sm->eap = eap_server_sm_init(sm, &eapol_cb, &eap_conf); if (sm->eap == NULL) { eapol_auth_free(sm); @@ -1037,6 +1038,7 @@ static int eapol_auth_conf_clone(struct eapol_auth_config *dst, dst->msg_ctx = src->msg_ctx; dst->eap_sim_db_priv = src->eap_sim_db_priv; os_free(dst->eap_req_id_text); + dst->pwd_group = src->pwd_group; if (src->eap_req_id_text) { dst->eap_req_id_text = os_malloc(src->eap_req_id_text_len); if (dst->eap_req_id_text == NULL) diff --git a/src/eapol_auth/eapol_auth_sm.h b/src/eapol_auth/eapol_auth_sm.h index 51058c653..59a10b45b 100644 --- a/src/eapol_auth/eapol_auth_sm.h +++ b/src/eapol_auth/eapol_auth_sm.h @@ -41,6 +41,7 @@ struct eapol_auth_config { int tnc; struct wps_context *wps; int fragment_size; + u16 pwd_group; /* Opaque context pointer to owner data for callback functions */ void *ctx; diff --git a/src/radius/radius_server.c b/src/radius/radius_server.c index f8780a692..6f1c3a50b 100644 --- a/src/radius/radius_server.c +++ b/src/radius/radius_server.c @@ -221,6 +221,13 @@ struct radius_server_data { */ int tnc; + /** + * pwd_group - The D-H group assigned for EAP-pwd + * + * If EAP-pwd is not used it can be set to zero. + */ + u16 pwd_group; + /** * wps - Wi-Fi Protected Setup context * @@ -505,6 +512,7 @@ radius_server_get_new_session(struct radius_server_data *data, eap_conf.eap_sim_aka_result_ind = data->eap_sim_aka_result_ind; eap_conf.tnc = data->tnc; eap_conf.wps = data->wps; + eap_conf.pwd_group = data->pwd_group; sess->eap = eap_server_sm_init(sess, &radius_server_eapol_cb, &eap_conf); if (sess->eap == NULL) { @@ -1259,6 +1267,7 @@ radius_server_init(struct radius_server_conf *conf) data->eap_sim_aka_result_ind = conf->eap_sim_aka_result_ind; data->tnc = conf->tnc; data->wps = conf->wps; + data->pwd_group = conf->pwd_group; if (conf->eap_req_id_text) { data->eap_req_id_text = os_malloc(conf->eap_req_id_text_len); if (data->eap_req_id_text) { diff --git a/src/radius/radius_server.h b/src/radius/radius_server.h index f9c951d05..126e31446 100644 --- a/src/radius/radius_server.h +++ b/src/radius/radius_server.h @@ -142,6 +142,13 @@ struct radius_server_conf { */ int tnc; + /** + * pwd_group - EAP-pwd D-H group + * + * This is used to select which D-H group to use with EAP-pwd. + */ + u16 pwd_group; + /** * wps - Wi-Fi Protected Setup context * diff --git a/wpa_supplicant/Makefile b/wpa_supplicant/Makefile index c113dd668..a113cca24 100644 --- a/wpa_supplicant/Makefile +++ b/wpa_supplicant/Makefile @@ -474,6 +474,14 @@ NEED_SHA256=y NEED_AES_OMAC1=y endif +ifdef CONFIG_EAP_PWD +CFLAGS += -DEAP_PWD +OBJS += ../src/eap_peer/eap_pwd.o ../src/eap_common/eap_pwd_common.o +OBJS_h += ../src/eap_server/eap_pwd.o +CONFIG_IEEE8021X_EAPOL=y +NEED_SHA256=y +endif + ifdef CONFIG_WPS ifdef CONFIG_WPS2 CFLAGS += -DCONFIG_WPS2 diff --git a/wpa_supplicant/eap_register.c b/wpa_supplicant/eap_register.c index f6688740b..e5f43aa68 100644 --- a/wpa_supplicant/eap_register.c +++ b/wpa_supplicant/eap_register.c @@ -130,6 +130,10 @@ int eap_register_methods(void) ret = eap_peer_tnc_register(); #endif /* EAP_TNC */ +#ifdef EAP_PWD + if (ret == 0) + ret = eap_peer_pwd_register(); +#endif /* EAP_PWD */ #ifdef EAP_SERVER_IDENTITY if (ret == 0) @@ -231,5 +235,10 @@ int eap_register_methods(void) ret = eap_server_tnc_register(); #endif /* EAP_SERVER_TNC */ +#ifdef EAP_SERVER_PWD + if (ret == 0) + ret = eap_server_pwd_register(); +#endif /* EAP_SERVER_PWD */ + return ret; }