eiwd

no dbus iwd with iwctl functionality
git clone https://git.daat.foo/repos/eiwd.git
Log | Files | Refs | Submodules | README | LICENSE

eap-pwd.c (21089B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2018-2019  Intel Corporation. All rights reserved.
      6  *
      7  *  This library is free software; you can redistribute it and/or
      8  *  modify it under the terms of the GNU Lesser General Public
      9  *  License as published by the Free Software Foundation; either
     10  *  version 2.1 of the License, or (at your option) any later version.
     11  *
     12  *  This library is distributed in the hope that it will be useful,
     13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     15  *  Lesser General Public License for more details.
     16  *
     17  *  You should have received a copy of the GNU Lesser General Public
     18  *  License along with this library; if not, write to the Free Software
     19  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     20  *
     21  */
     22 
     23 #ifdef HAVE_CONFIG_H
     24 #include <config.h>
     25 #endif
     26 
     27 #define _GNU_SOURCE
     28 #include <stdio.h>
     29 #include <string.h>
     30 
     31 #include <ell/ell.h>
     32 
     33 #include "ell/useful.h"
     34 #include "src/missing.h"
     35 #include "src/eap.h"
     36 #include "src/eap-private.h"
     37 #include "src/crypto.h"
     38 #include "src/util.h"
     39 
     40 #define EAP_PWD_GROUP_DESC	19
     41 #define EAP_PWD_RAND_FN		0x01
     42 #define EAP_PWD_PRF		0x01
     43 
     44 /* EAP header + PWD-Exch */
     45 #define EAP_PWD_HDR_LEN		6
     46 #define EAP_PWD_L_BIT		(1 << 7)
     47 #define EAP_PWD_M_BIT		(1 << 6)
     48 
     49 enum eap_pwd_prep {
     50 	EAP_PWD_PREP_NONE =	0x00,
     51 	EAP_PWD_PREP_MS =	0x01,
     52 	EAP_PWD_PREP_SASL =	0x02
     53 };
     54 
     55 enum eap_pwd_exch {
     56 	EAP_PWD_EXCH_RESERVED = 0,
     57 	EAP_PWD_EXCH_ID,
     58 	EAP_PWD_EXCH_COMMIT,
     59 	EAP_PWD_EXCH_CONFIRM
     60 };
     61 
     62 enum eap_pwd_state {
     63 	EAP_PWD_STATE_INIT = 0,
     64 	EAP_PWD_STATE_ID,
     65 	EAP_PWD_STATE_COMMIT,
     66 	EAP_PWD_STATE_CONFIRM
     67 };
     68 
     69 struct eap_pwd_handle {
     70 	enum eap_pwd_state state;
     71 	enum eap_pwd_prep prep;
     72 	char *identity;
     73 	char *password;
     74 	const struct l_ecc_curve *curve;
     75 	struct l_ecc_point *pwe;
     76 	struct l_ecc_point *element_s;
     77 	struct l_ecc_point *element_p;
     78 	uint32_t ciphersuite;
     79 	struct l_ecc_scalar *scalar_s;
     80 	struct l_ecc_scalar *scalar_p;
     81 	struct l_ecc_scalar *p_rand;
     82 	uint8_t *rx_frag_buf;
     83 	uint16_t rx_frag_total;
     84 	uint16_t rx_frag_count;
     85 	uint8_t *tx_frag_buf;
     86 	uint8_t *tx_frag_pos;
     87 	uint16_t tx_frag_remaining;
     88 };
     89 
     90 /* RFC 5931, Section 2.5 - Key Derivation Function */
     91 static bool kdf(uint8_t *key, size_t key_len, const char *label,
     92 		size_t label_len, void *out, size_t olen)
     93 {
     94 	struct l_checksum *hmac;
     95 	struct iovec iov[4];
     96 	uint16_t ibuf, i = 1;
     97 	uint16_t L = L_CPU_TO_BE16(olen * 8);
     98 	size_t len = 0;
     99 
    100 	while (len < olen) {
    101 		int iov_pos = 0;
    102 
    103 		hmac = l_checksum_new_hmac(L_CHECKSUM_SHA256, key, key_len);
    104 		if (!hmac)
    105 			return false;
    106 
    107 		/* PRF(key, K(i - 1) | i | label | L) */
    108 		if (i > 1) {
    109 			iov[iov_pos].iov_base = out + len - 32;
    110 			iov[iov_pos++].iov_len = 32;
    111 		}
    112 
    113 		ibuf = L_CPU_TO_BE16(i);
    114 		iov[iov_pos].iov_base = (void *) &ibuf;
    115 		iov[iov_pos++].iov_len = 2;
    116 		iov[iov_pos].iov_base = (void *)label;
    117 		iov[iov_pos++].iov_len = label_len;
    118 		iov[iov_pos].iov_base = &L;
    119 		iov[iov_pos++].iov_len = 2;
    120 
    121 		if (!l_checksum_updatev(hmac, iov, iov_pos)) {
    122 			l_checksum_free(hmac);
    123 			return false;
    124 		}
    125 
    126 		l_checksum_get_digest(hmac, out + len, minsize(olen - len, 32));
    127 		l_checksum_free(hmac);
    128 
    129 		len += 32;
    130 		i++;
    131 	}
    132 
    133 	return true;
    134 }
    135 
    136 static bool eap_pwd_reset_state(struct eap_state *eap)
    137 {
    138 	struct eap_pwd_handle *pwd = eap_get_data(eap);
    139 
    140 	pwd->state = EAP_PWD_STATE_INIT;
    141 
    142 	l_free(pwd->tx_frag_buf);
    143 	pwd->tx_frag_buf = NULL;
    144 	pwd->tx_frag_pos = NULL;
    145 	pwd->tx_frag_remaining = 0;
    146 
    147 	l_free(pwd->rx_frag_buf);
    148 	pwd->rx_frag_buf = NULL;
    149 	pwd->rx_frag_count = 0;
    150 	pwd->rx_frag_total = 0;
    151 
    152 	pwd->prep = EAP_PWD_PREP_NONE;
    153 	pwd->ciphersuite = 0;
    154 
    155 	l_ecc_point_free(pwd->pwe);
    156 	pwd->pwe = NULL;
    157 	l_ecc_point_free(pwd->element_p);
    158 	pwd->element_p = NULL;
    159 	l_ecc_point_free(pwd->element_s);
    160 	pwd->element_s = NULL;
    161 	l_ecc_scalar_free(pwd->scalar_p);
    162 	pwd->scalar_p = NULL;
    163 	l_ecc_scalar_free(pwd->scalar_s);
    164 	pwd->scalar_s = NULL;
    165 	l_ecc_scalar_free(pwd->p_rand);
    166 	pwd->p_rand = NULL;
    167 
    168 	return true;
    169 }
    170 
    171 static void eap_pwd_free(struct eap_state *eap)
    172 {
    173 	struct eap_pwd_handle *pwd = eap_get_data(eap);
    174 
    175 	eap_pwd_reset_state(eap);
    176 	l_free(pwd->identity);
    177 
    178 	if (pwd->password) {
    179 		explicit_bzero(pwd->password, strlen(pwd->password));
    180 		l_free(pwd->password);
    181 	}
    182 
    183 	l_free(pwd);
    184 
    185 	eap_set_data(eap, NULL);
    186 }
    187 
    188 static void eap_pwd_send_response(struct eap_state *eap,
    189 					uint8_t *pkt, size_t len)
    190 {
    191 	struct eap_pwd_handle *pwd = eap_get_data(eap);
    192 	size_t mtu = eap_get_mtu(eap);
    193 	uint8_t frag[mtu];
    194 	uint8_t *pos = frag;
    195 	/* first fragment data bytes (mtu - header - Total-Length) */
    196 	uint16_t send_bytes = mtu - EAP_PWD_HDR_LEN - 2;
    197 
    198 	/* packet will fit within mtu */
    199 	if (len <= mtu) {
    200 		eap_method_respond(eap, pkt, len);
    201 		return;
    202 	}
    203 
    204 	if (pwd->tx_frag_buf) {
    205 		l_error("already processing fragment, cannot send response");
    206 		return;
    207 	}
    208 
    209 	/* header */
    210 	memcpy(pos, pkt, 5);
    211 	pos += 5;
    212 	/* PWD-Exch, first frag, so L and M are both set */
    213 	*pos++ = pwd->state | EAP_PWD_L_BIT | EAP_PWD_M_BIT;
    214 	/* Total-Length */
    215 	l_put_be16((uint16_t)len, pos);
    216 	pos += 2;
    217 	/* copy packet data bytes */
    218 	memcpy(pos, pkt + EAP_PWD_HDR_LEN, send_bytes);
    219 
    220 	pwd->tx_frag_remaining = len - EAP_PWD_HDR_LEN - send_bytes;
    221 
    222 	l_info("sending initial fragment, %zu bytes", mtu);
    223 
    224 	eap_method_respond(eap, frag, mtu);
    225 
    226 	/* alloc/copy remainder of packet to frag buf */
    227 	pwd->tx_frag_buf = l_malloc(pwd->tx_frag_remaining);
    228 
    229 	memcpy(pwd->tx_frag_buf, pkt + EAP_PWD_HDR_LEN + send_bytes,
    230 			pwd->tx_frag_remaining);
    231 
    232 	pwd->tx_frag_pos = pwd->tx_frag_buf;
    233 }
    234 
    235 static void eap_pwd_handle_id(struct eap_state *eap,
    236 				const uint8_t *pkt, size_t len)
    237 {
    238 	struct eap_pwd_handle *pwd = eap_get_data(eap);
    239 	uint16_t group;
    240 	uint8_t rand_fn;
    241 	uint8_t prf;
    242 	uint32_t token;
    243 	uint8_t counter = 0;
    244 	uint8_t resp[15 + strlen(pwd->identity)];
    245 	uint8_t *pos;
    246 	uint8_t pwd_seed[32];
    247 	uint8_t pwd_value[L_ECC_SCALAR_MAX_BYTES];	/* used as X value */
    248 	size_t nbytes;
    249 	bool found = false;
    250 
    251 	/*
    252 	 * Group desc (2) + Random func (1) + prf (1) + token (4) + prep (1) +
    253 	 * Identity (at least 1 byte)
    254 	 */
    255 	if (len < 9) {
    256 		l_error("bad packet length");
    257 		goto error;
    258 	}
    259 
    260 	if (pwd->state != EAP_PWD_STATE_INIT) {
    261 		l_error("received ID request in invalid state");
    262 		goto error;
    263 	}
    264 
    265 	pwd->state = EAP_PWD_STATE_ID;
    266 
    267 	group = l_get_be16(pkt);
    268 
    269 	pwd->curve = l_ecc_curve_from_ike_group(group);
    270 	if (!pwd->curve) {
    271 		l_error("group %d not supported", group);
    272 		goto error;
    273 	}
    274 
    275 	rand_fn = pkt[2];
    276 	if (rand_fn != EAP_PWD_RAND_FN) {
    277 		l_error("rand_fn %d not supported", rand_fn);
    278 		goto error;
    279 	}
    280 
    281 	prf = pkt[3];
    282 	if (prf != EAP_PWD_PRF) {
    283 		l_error("PRF function %d not supported", prf);
    284 		goto error;
    285 	}
    286 
    287 	/*
    288 	 * RFC 5931 Section 3.2.1
    289 	 * The Group Description, Random Function, and PRF together, and in that
    290 	 * order, comprise the Ciphersuite...
    291 	 */
    292 	pwd->ciphersuite = l_get_u32(pkt);
    293 	token = l_get_u32(pkt + 4);
    294 	pwd->prep = pkt[8];
    295 
    296 	if (pwd->prep != EAP_PWD_PREP_NONE) {
    297 		/*
    298 		 * TODO: Support other PW prep types
    299 		 */
    300 		l_error("prep type %d not currently supported", pwd->prep);
    301 		goto error;
    302 	}
    303 
    304 	nbytes = l_ecc_curve_get_scalar_bytes(pwd->curve);
    305 
    306 	while (counter < 20) {
    307 		struct l_ecc_point *pwe = NULL;
    308 
    309 		counter++;
    310 
    311 		/* pwd-seed = H(token|peer-ID|server-ID|password|counter) */
    312 		hkdf_extract(L_CHECKSUM_SHA256, NULL, 0, 5, pwd_seed, &token, 4,
    313 				pwd->identity, strlen(pwd->identity), pkt + 9,
    314 				len - 9, pwd->password, strlen(pwd->password),
    315 				&counter, (size_t) 1);
    316 
    317 		/*
    318 		 * pwd-value = KDF(pwd-seed, "EAP-pwd Hunting And Pecking",
    319 		 *                 len(p))
    320 		 */
    321 		kdf(pwd_seed, 32, "EAP-pwd Hunting And Pecking",
    322 				strlen("EAP-pwd Hunting And Pecking"),
    323 				pwd_value, nbytes);
    324 
    325 		/*
    326 		 * The RFC requires the point be solved unambiguously (since
    327 		 * solving for Y results in two solutions). The correct Y value
    328 		 * is chosen based on the LSB of the pwd-seed:
    329 		 *
    330 		 *     if (LSB(y) == LSB(pwd-seed))
    331 		 *     then
    332 		 *         PWE = (x, y)
    333 		 *     else
    334 		 *         PWE = (x, p-y)
    335 		 *
    336 		 * The ELL API (somewhat hidden from view here) automatically
    337 		 * performs a subtraction (P - Y) when:
    338 		 *     - Y is even and BIT1
    339 		 *     - Y is odd and BIT0
    340 		 *
    341 		 * So we choose the point type which matches the parity of
    342 		 * pwd-seed. This means a subtraction will be performed (P - Y)
    343 		 * if the parity of pwd-seed and the computed Y do not match.
    344 		 */
    345 		if (pwd_seed[31] & 1)
    346 			pwe = l_ecc_point_from_data(pwd->curve,
    347 					L_ECC_POINT_TYPE_COMPRESSED_BIT1,
    348 					pwd_value, nbytes);
    349 		else
    350 			pwe = l_ecc_point_from_data(pwd->curve,
    351 					L_ECC_POINT_TYPE_COMPRESSED_BIT0,
    352 					pwd_value, nbytes);
    353 
    354 		if (!pwe)
    355 			continue;
    356 
    357 		if (!found) {
    358 			found = true;
    359 			pwd->pwe = pwe;
    360 		} else
    361 			l_ecc_point_free(pwe);
    362 	}
    363 
    364 	explicit_bzero(pwd_seed, sizeof(pwd_seed));
    365 	explicit_bzero(pwd_value, sizeof(pwd_value));
    366 
    367 	pos = resp + 5; /* header */
    368 	*pos++ = EAP_PWD_EXCH_ID;
    369 	l_put_be16(group, pos);
    370 	pos += 2;
    371 	*pos++ = rand_fn;
    372 	*pos++ = prf;
    373 	l_put_u32(token, pos);
    374 	pos += 4;
    375 	*pos++ = pwd->prep;
    376 	memcpy(pos, pwd->identity, strlen(pwd->identity));
    377 	pos += strlen(pwd->identity);
    378 
    379 	eap_pwd_send_response(eap, resp, pos - resp);
    380 
    381 	return;
    382 
    383 error:
    384 	eap_method_error(eap);
    385 }
    386 
    387 static void eap_pwd_handle_commit(struct eap_state *eap,
    388 					const uint8_t *pkt, size_t len)
    389 {
    390 	struct eap_pwd_handle *pwd = eap_get_data(eap);
    391 	uint8_t resp[L_ECC_POINT_MAX_BYTES + L_ECC_SCALAR_MAX_BYTES + 6];
    392 	uint8_t *pos;
    393 	struct l_ecc_scalar *p_mask;
    394 	struct l_ecc_scalar *order;
    395 	size_t nbytes = l_ecc_curve_get_scalar_bytes(pwd->curve);
    396 
    397 	/* [Element (nbytes * 2)][Scalar (nbytes)] */
    398 	if (len != nbytes + nbytes * 2) {
    399 		l_error("bad packet length, expected %zu, got %zu",
    400 				nbytes + nbytes * 2, len);
    401 		goto error;
    402 	}
    403 
    404 	if (pwd->state != EAP_PWD_STATE_ID) {
    405 		l_error("received commit request in invalid state");
    406 		goto error;
    407 	}
    408 
    409 	pwd->state = EAP_PWD_STATE_COMMIT;
    410 
    411 	/*
    412 	 * Commit contains Element_S (nbytes * 2) then Scalar_s (nbytes)
    413 	 */
    414 	pwd->element_s = l_ecc_point_from_data(pwd->curve,
    415 						L_ECC_POINT_TYPE_FULL,
    416 						pkt, nbytes * 2);
    417 	if (!pwd->element_s) {
    418 		l_error("Server sent invalid Element_S during commit");
    419 		goto error;
    420 	}
    421 
    422 	pwd->scalar_s = l_ecc_scalar_new(pwd->curve, pkt + nbytes * 2, nbytes);
    423 	if (!pwd->scalar_s) {
    424 		l_error("Server sent invalid Scalar_S during commit");
    425 		goto error;
    426 	}
    427 
    428 	pwd->p_rand = l_ecc_scalar_new_random(pwd->curve);
    429 	p_mask = l_ecc_scalar_new_random(pwd->curve);
    430 	pwd->scalar_p = l_ecc_scalar_new(pwd->curve, NULL, 0);
    431 
    432 	order = l_ecc_curve_get_order(pwd->curve);
    433 
    434 	l_ecc_scalar_add(pwd->scalar_p, pwd->p_rand, p_mask, order);
    435 
    436 	l_ecc_scalar_free(order);
    437 
    438 	pwd->element_p = l_ecc_point_new(pwd->curve);
    439 	/* p_mask * PWE */
    440 	l_ecc_point_multiply(pwd->element_p, p_mask, pwd->pwe);
    441 
    442 	l_ecc_scalar_free(p_mask);
    443 
    444 	/* inv(p_mask * PWE) */
    445 	l_ecc_point_inverse(pwd->element_p);
    446 
    447 	/* send element_p and scalar_p */
    448 	pos = resp + 5; /* header */
    449 	*pos++ = EAP_PWD_EXCH_COMMIT;
    450 	pos += l_ecc_point_get_data(pwd->element_p, pos, nbytes * 2);
    451 	pos += l_ecc_scalar_get_data(pwd->scalar_p, pos, nbytes);
    452 
    453 	eap_pwd_send_response(eap, resp, pos - resp);
    454 
    455 	return;
    456 
    457 error:
    458 	eap_method_error(eap);
    459 }
    460 
    461 static void eap_pwd_handle_confirm(struct eap_state *eap,
    462 					const uint8_t *pkt, size_t len)
    463 {
    464 	struct eap_pwd_handle *pwd = eap_get_data(eap);
    465 	struct l_ecc_point *kp;
    466 	uint8_t resp[38];
    467 	uint8_t *pos;
    468 	uint8_t confirm_s[32];
    469 	uint8_t confirm_p[32];
    470 	uint8_t expected_confirm_s[32];
    471 	uint8_t mk[32];
    472 	uint8_t msk_emsk[128], session_id[33];
    473 	/* buffers used for the final hash */
    474 	uint8_t kpx[L_ECC_SCALAR_MAX_BYTES];
    475 	uint8_t scalar_s[L_ECC_SCALAR_MAX_BYTES];
    476 	uint8_t scalar_p[L_ECC_SCALAR_MAX_BYTES];
    477 	uint8_t element_s[L_ECC_POINT_MAX_BYTES];
    478 	uint8_t element_p[L_ECC_POINT_MAX_BYTES];
    479 	ssize_t plen, clen;
    480 
    481 	if (len != 32) {
    482 		l_error("bad packet length");
    483 		goto error;
    484 	}
    485 
    486 	if (pwd->state != EAP_PWD_STATE_COMMIT) {
    487 		l_error("received confirm request in invalid state");
    488 		goto error;
    489 	}
    490 
    491 	pwd->state = EAP_PWD_STATE_CONFIRM;
    492 
    493 	memcpy(confirm_s, pkt, 32);
    494 
    495 	kp = l_ecc_point_new(pwd->curve);
    496 
    497 	/* compute KP = (p_rand * (Scalar_S * PWE + Element_S)) */
    498 	l_ecc_point_multiply(kp, pwd->scalar_s, pwd->pwe);
    499 
    500 	l_ecc_point_add(kp, kp, pwd->element_s);
    501 
    502 	l_ecc_point_multiply(kp, pwd->p_rand, kp);
    503 
    504 	/*
    505 	 * We just need to store clen/plen once. Since all these buffers are
    506 	 * created with enough bytes in mind we know these won't fail. Also, all
    507 	 * scalar/point objects were created with the same curve, so it can be
    508 	 * safe to assume the return values will not change from what clen/plen
    509 	 * already are.
    510 	 */
    511 	clen = l_ecc_point_get_x(kp, kpx, sizeof(kpx));
    512 	if (clen < 0)
    513 		goto invalid_point;
    514 
    515 	plen = l_ecc_point_get_data(pwd->element_s, element_s,
    516 					sizeof(element_s));
    517 	if (plen < 0)
    518 		goto invalid_point;
    519 
    520 	if (l_ecc_point_get_data(pwd->element_p, element_p,
    521 					sizeof(element_p)) < 0)
    522 		goto invalid_point;
    523 
    524 	if (l_ecc_scalar_get_data(pwd->scalar_s, scalar_s,
    525 					sizeof(scalar_s)) < 0)
    526 		goto invalid_point;
    527 
    528 	if (l_ecc_scalar_get_data(pwd->scalar_p, scalar_p,
    529 					sizeof(scalar_p)) < 0)
    530 		goto invalid_point;
    531 
    532 	l_ecc_point_free(kp);
    533 
    534 	/*
    535 	 * compute Confirm_P = H(kp | Element_P | Scalar_P |
    536 	 *                       Element_S | Scalar_S | Ciphersuite)
    537 	 */
    538 	hkdf_extract(L_CHECKSUM_SHA256, NULL, 0, 6, confirm_p, kpx, clen,
    539 				element_p, plen, scalar_p, clen, element_s,
    540 				plen, scalar_s, clen, &pwd->ciphersuite,
    541 				(size_t) 4);
    542 
    543 	hkdf_extract(L_CHECKSUM_SHA256, NULL, 0, 6, expected_confirm_s, kpx,
    544 				clen, element_s, plen, scalar_s, clen,
    545 				element_p, plen, scalar_p, clen,
    546 				&pwd->ciphersuite, (size_t) 4);
    547 
    548 	if (memcmp(confirm_s, expected_confirm_s, 32)) {
    549 		l_error("Confirm_S did not verify");
    550 		goto error;
    551 	}
    552 
    553 	pos = resp + 5; /* header */
    554 	*pos++ = EAP_PWD_EXCH_CONFIRM;
    555 	memcpy(pos, confirm_p, 32);
    556 	pos += 32;
    557 
    558 	/* derive MK = H(kp | Confirm_P | Confirm_S ) */
    559 	hkdf_extract(L_CHECKSUM_SHA256, NULL, 0, 3, mk, kpx, clen, confirm_p,
    560 			(size_t) 32, confirm_s, (size_t) 32);
    561 
    562 	eap_pwd_send_response(eap, resp, pos - resp);
    563 
    564 	eap_method_success(eap);
    565 
    566 	session_id[0] = 52;
    567 	hkdf_extract(L_CHECKSUM_SHA256, NULL, 0, 3, session_id + 1,
    568 			&pwd->ciphersuite, (size_t) 4, scalar_p, clen,
    569 			scalar_s, clen);
    570 
    571 	kdf(mk, 32, (const char *) session_id, 33, msk_emsk, 128);
    572 	eap_set_key_material(eap, msk_emsk, 64, msk_emsk + 64, 64, NULL, 0,
    573 				session_id, sizeof(session_id));
    574 
    575 	explicit_bzero(mk, sizeof(mk));
    576 	explicit_bzero(msk_emsk, sizeof(msk_emsk));
    577 	explicit_bzero(kpx, sizeof(kpx));
    578 
    579 	return;
    580 
    581 invalid_point:
    582 	l_ecc_point_free(kp);
    583 
    584 	l_error("invalid point during confirm exchange");
    585 error:
    586 	explicit_bzero(kpx, sizeof(kpx));
    587 
    588 	eap_method_error(eap);
    589 }
    590 
    591 static void eap_pwd_process(struct eap_state *eap,
    592 				const uint8_t *pkt, size_t len)
    593 {
    594 	uint8_t pwd_exch = bit_field(pkt[0], 0, 6);
    595 
    596 	if (len < 1)
    597 		return;
    598 
    599 	switch (pwd_exch) {
    600 	case EAP_PWD_EXCH_ID:
    601 		eap_pwd_handle_id(eap, pkt + 1, len - 1);
    602 		break;
    603 	case EAP_PWD_EXCH_COMMIT:
    604 		eap_pwd_handle_commit(eap, pkt + 1, len - 1);
    605 		break;
    606 	case EAP_PWD_EXCH_CONFIRM:
    607 		eap_pwd_handle_confirm(eap, pkt + 1, len - 1);
    608 		break;
    609 	}
    610 }
    611 
    612 static void eap_pwd_send_ack(struct eap_state *eap)
    613 {
    614 	struct eap_pwd_handle *pwd = eap_get_data(eap);
    615 	uint8_t buf[6];
    616 
    617 	buf[5] = pwd->state + 1;
    618 
    619 	eap_method_respond(eap, buf, 6);
    620 }
    621 
    622 #define FRAG_BYTES(mtu, remaining) \
    623 	(((mtu - EAP_PWD_HDR_LEN) < remaining) ? (mtu - EAP_PWD_HDR_LEN) : \
    624 			remaining)
    625 
    626 static void eap_pwd_handle_request(struct eap_state *eap,
    627 					const uint8_t *pkt, size_t len)
    628 {
    629 	struct eap_pwd_handle *pwd = eap_get_data(eap);
    630 	uint8_t len_bit = false;
    631 	uint8_t more_bit = false;
    632 
    633 	/* ACK from tx fragment, send next fragment */
    634 	if (len == 1 && pwd->tx_frag_buf) {
    635 		size_t mtu = eap_get_mtu(eap);
    636 		uint8_t frag[mtu];
    637 		uint8_t *pos = frag;
    638 		uint16_t frag_bytes = FRAG_BYTES(mtu, pwd->tx_frag_remaining);
    639 
    640 		pos += 5; /* header */
    641 		*pos = pwd->state;
    642 
    643 		/* more fragments coming, set M bit */
    644 		if (frag_bytes < pwd->tx_frag_remaining)
    645 			*pos |= EAP_PWD_M_BIT;
    646 
    647 		pos++;
    648 
    649 		memcpy(pos, pwd->tx_frag_pos, frag_bytes);
    650 
    651 		pwd->tx_frag_pos += frag_bytes;
    652 		pwd->tx_frag_remaining -= frag_bytes;
    653 
    654 		l_info("sending fragment, %d bytes",
    655 				frag_bytes + EAP_PWD_HDR_LEN);
    656 
    657 		eap_method_respond(eap, frag, frag_bytes + EAP_PWD_HDR_LEN);
    658 
    659 		if (!pwd->tx_frag_remaining) {
    660 			/* done sending fragments, free */
    661 			l_free(pwd->tx_frag_buf);
    662 			pwd->tx_frag_buf = NULL;
    663 			pwd->tx_frag_pos = NULL;
    664 			pwd->tx_frag_remaining = 0;
    665 		}
    666 
    667 		return;
    668 	}
    669 
    670 	if (pwd->tx_frag_buf) {
    671 		l_error("received packet while waiting for ACK!");
    672 		return;
    673 	}
    674 
    675 	if (len < 1) {
    676 		l_error("packet is too small");
    677 		return;
    678 	}
    679 
    680 	/* set if Total-Length parameter is include (i.e. first fragment) */
    681 	len_bit = test_bit(pkt, 7);
    682 	/* set on all but the last fragment */
    683 	more_bit = test_bit(pkt, 6);
    684 
    685 	/* first rx fragment */
    686 	if (len_bit) {
    687 		if (len < 3) {
    688 			l_error("malformed packet");
    689 			return;
    690 		}
    691 
    692 		/* remove length of Total-Length parameter (2) */
    693 		pwd->rx_frag_total = l_get_be16(pkt + 1) - 2;
    694 
    695 		if (pwd->rx_frag_total < len - 2) {
    696 			l_error("Total-Length too small for remaining length");
    697 			pwd->rx_frag_total = 0;
    698 
    699 			return;
    700 		}
    701 
    702 		pwd->rx_frag_buf = l_malloc(pwd->rx_frag_total);
    703 
    704 		/* skip copying Total-Length for easier processing later */
    705 		pwd->rx_frag_buf[0] = pkt[0];
    706 		memcpy(pwd->rx_frag_buf + 1, pkt + 3, len - 2);
    707 
    708 		pwd->rx_frag_count = len - 2;
    709 
    710 		l_info("received first fragment, %d total bytes",
    711 				pwd->rx_frag_total);
    712 
    713 		eap_pwd_send_ack(eap);
    714 
    715 		return;
    716 	}
    717 
    718 	/* more rx fragments */
    719 	if (pwd->rx_frag_buf) {
    720 		if (pwd->rx_frag_total - pwd->rx_frag_count <
    721 						(uint16_t) len - 1) {
    722 			l_error("Not enough room for fragment (%zu)", len - 1);
    723 			return;
    724 
    725 		}
    726 		/* continue building packet (not including PWD-Exch byte) */
    727 		memcpy(pwd->rx_frag_buf + pwd->rx_frag_count, pkt + 1, len - 1);
    728 		pwd->rx_frag_count += (len - 1);
    729 
    730 		l_info("received another fragment, %zu bytes", len);
    731 
    732 		/* more fragments coming */
    733 		if (more_bit) {
    734 			eap_pwd_send_ack(eap);
    735 			return;
    736 		}
    737 
    738 		if (pwd->rx_frag_count != pwd->rx_frag_total) {
    739 			l_error("fragment length mismatch");
    740 			return;
    741 		}
    742 
    743 		/* this was the last fragment, process */
    744 		eap_pwd_process(eap, pwd->rx_frag_buf, pwd->rx_frag_total);
    745 
    746 		l_free(pwd->rx_frag_buf);
    747 		pwd->rx_frag_buf = NULL;
    748 		pwd->rx_frag_count = 0;
    749 		pwd->rx_frag_total = 0;
    750 
    751 		return;
    752 	}
    753 
    754 	/* no fragmentation, process normally */
    755 	eap_pwd_process(eap, pkt, len);
    756 }
    757 
    758 static int eap_pwd_check_settings(struct l_settings *settings,
    759 					struct l_queue *secrets,
    760 					const char *prefix,
    761 					struct l_queue **out_missing)
    762 {
    763 	const struct eap_secret_info *secret;
    764 	char identity_key[72];
    765 	char password_key[72];
    766 
    767 	L_AUTO_FREE_VAR(char *, identity);
    768 	L_AUTO_FREE_VAR(char *, password) = NULL;
    769 
    770 	snprintf(identity_key, sizeof(identity_key), "%sIdentity", prefix);
    771 	snprintf(password_key, sizeof(password_key), "%sPassword", prefix);
    772 
    773 	identity = l_settings_get_string(settings, "Security", identity_key);
    774 
    775 	if (!identity) {
    776 		secret = l_queue_find(secrets, eap_secret_info_match,
    777 								identity_key);
    778 		if (secret)
    779 			return 0;
    780 
    781 		eap_append_secret(out_missing, EAP_SECRET_REMOTE_USER_PASSWORD,
    782 					identity_key, password_key, NULL,
    783 					EAP_CACHE_TEMPORARY);
    784 
    785 		return 0;
    786 	}
    787 
    788 	password = l_settings_get_string(settings, "Security", password_key);
    789 
    790 	if (!password) {
    791 		secret = l_queue_find(secrets, eap_secret_info_match,
    792 								password_key);
    793 		if (secret)
    794 			return 0;
    795 
    796 		eap_append_secret(out_missing, EAP_SECRET_REMOTE_PASSWORD,
    797 					password_key, NULL, identity,
    798 					EAP_CACHE_TEMPORARY);
    799 	} else
    800 		explicit_bzero(password, strlen(password));
    801 
    802 	return 0;
    803 }
    804 
    805 static bool eap_pwd_load_settings(struct eap_state *eap,
    806 					struct l_settings *settings,
    807 					const char *prefix)
    808 {
    809 	struct eap_pwd_handle *pwd;
    810 	char setting_key[72];
    811 
    812 	pwd = l_new(struct eap_pwd_handle, 1);
    813 
    814 	pwd->state = EAP_PWD_STATE_INIT;
    815 
    816 	snprintf(setting_key, sizeof(setting_key), "%sIdentity", prefix);
    817 	pwd->identity = l_settings_get_string(settings, "Security",
    818 								setting_key);
    819 
    820 	if (!pwd->identity) {
    821 		l_error("'%s' setting is missing", setting_key);
    822 		goto error;
    823 	}
    824 
    825 	snprintf(setting_key, sizeof(setting_key), "%sPassword", prefix);
    826 	pwd->password = l_settings_get_string(settings, "Security",
    827 								setting_key);
    828 
    829 	if (!pwd->password) {
    830 		snprintf(setting_key, sizeof(setting_key), "%sPassword",
    831 								prefix);
    832 		l_error("'%s' setting is missing", setting_key);
    833 		goto error;
    834 	}
    835 
    836 	eap_set_data(eap, pwd);
    837 
    838 	return true;
    839 
    840 error:
    841 	if (pwd->password) {
    842 		explicit_bzero(pwd->password, strlen(pwd->password));
    843 		l_free(pwd->password);
    844 	}
    845 
    846 	l_free(pwd->identity);
    847 	l_free(pwd);
    848 
    849 	return false;
    850 }
    851 
    852 static struct eap_method eap_pwd = {
    853 	.request_type = EAP_TYPE_PWD,
    854 	.exports_msk = true,
    855 	.name = "PWD",
    856 	.free = eap_pwd_free,
    857 	.handle_request = eap_pwd_handle_request,
    858 	.check_settings = eap_pwd_check_settings,
    859 	.load_settings = eap_pwd_load_settings,
    860 	.reset_state = eap_pwd_reset_state,
    861 };
    862 
    863 static int eap_pwd_init(void)
    864 {
    865 	l_debug("");
    866 	return eap_register_method(&eap_pwd);
    867 }
    868 
    869 static void eap_pwd_exit(void)
    870 {
    871 	l_debug("");
    872 	eap_unregister_method(&eap_pwd);
    873 }
    874 
    875 EAP_METHOD_BUILTIN(eap_pwd, eap_pwd_init, eap_pwd_exit)