eiwd

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

eap-wsc.c (56618B)


      1 /*
      2  *
      3  *  Wireless daemon for Linux
      4  *
      5  *  Copyright (C) 2016-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 <string.h>
     29 #include <stdio.h>
     30 #include <errno.h>
     31 #include <ell/ell.h>
     32 
     33 #include "src/missing.h"
     34 #include "src/crypto.h"
     35 #include "src/eap.h"
     36 #include "src/eap-private.h"
     37 #include "src/wscutil.h"
     38 #include "src/util.h"
     39 #include "src/eap-wsc.h"
     40 
     41 #define EAP_WSC_HEADER_LEN	14
     42 #define EAP_WSC_PDU_MAX_LEN	4096
     43 
     44 /* WSC v2.0.5, Section 7.7.1 */
     45 enum wsc_op {
     46 	WSC_OP_START	= 0x01,
     47 	WSC_OP_ACK	= 0x02,
     48 	WSC_OP_NACK	= 0x03,
     49 	WSC_OP_MSG	= 0x04,
     50 	WSC_OP_DONE	= 0x05,
     51 	WSC_OP_FRAG_ACK = 0x06,
     52 };
     53 
     54 /* WSC v2.0.5, Section 7.7.1 */
     55 enum wsc_flag {
     56 	WSC_FLAG_MF	= 0x01,
     57 	WSC_FLAG_LF	= 0x02,
     58 };
     59 
     60 enum state {
     61 	/* Enrollee states */
     62 	STATE_EXPECT_START = 0,
     63 	STATE_EXPECT_M2,
     64 	STATE_EXPECT_M4,
     65 	STATE_EXPECT_M6,
     66 	STATE_EXPECT_M8,
     67 	STATE_FINISHED,
     68 	/* Registrar states */
     69 	STATE_EXPECT_IDENTITY,
     70 	STATE_EXPECT_M1,
     71 	STATE_EXPECT_M3,
     72 	STATE_EXPECT_M5,
     73 	STATE_EXPECT_M7,
     74 	STATE_EXPECT_DONE,
     75 };
     76 
     77 static struct l_key *dh5_generator;
     78 static struct l_key *dh5_prime;
     79 
     80 struct eap_wsc_state {
     81 	bool registrar;
     82 	struct wsc_m1 *m1;
     83 	struct wsc_m2 *m2;
     84 	struct wsc_credential wpa2_cred;
     85 	struct wsc_credential open_cred;
     86 	uint8_t *sent_pdu;
     87 	size_t sent_len;
     88 	struct l_key *private;
     89 	char *device_password;
     90 	uint8_t local_snonce1[16];
     91 	uint8_t local_snonce2[16];
     92 	uint8_t iv1[16];
     93 	uint8_t iv2[16];
     94 	uint8_t iv3[16];
     95 	uint8_t psk1[16];
     96 	uint8_t psk2[16];
     97 	uint8_t e_hash1[32];
     98 	uint8_t e_hash2[32];
     99 	uint8_t r_hash2[32];
    100 	enum state state;
    101 	struct l_checksum *hmac_auth_key;
    102 	struct l_cipher *aes_cbc_128;
    103 	uint8_t *rx_pdu_buf;
    104 	size_t rx_pdu_buf_len;
    105 	size_t rx_pdu_buf_offset;
    106 	size_t tx_frag_offset;
    107 	size_t tx_last_frag_len;
    108 };
    109 
    110 static inline void eap_wsc_state_set_sent_pdu(struct eap_wsc_state *wsc,
    111 						uint8_t *pdu, size_t len)
    112 {
    113 	l_free(wsc->sent_pdu);
    114 	wsc->sent_pdu = pdu;
    115 	wsc->sent_len = len;
    116 }
    117 
    118 static inline bool authenticator_check(struct eap_wsc_state *wsc,
    119 					const uint8_t *pdu, size_t len)
    120 {
    121 	uint8_t authenticator[8];
    122 	struct iovec iov[2];
    123 
    124 	iov[0].iov_base = wsc->sent_pdu;
    125 	iov[0].iov_len = wsc->sent_len;
    126 	iov[1].iov_base = (void *) pdu;
    127 	iov[1].iov_len = len - 12;
    128 	l_checksum_updatev(wsc->hmac_auth_key, iov, 2);
    129 	l_checksum_get_digest(wsc->hmac_auth_key, authenticator, 8);
    130 
    131 	/* Authenticator is the last 8 bytes of the message */
    132 	if (memcmp(authenticator, pdu + len - 8, 8))
    133 		return false;
    134 
    135 	return true;
    136 }
    137 
    138 static inline void authenticator_put(struct eap_wsc_state *wsc,
    139 					const uint8_t *prev_msg,
    140 					size_t prev_msg_len,
    141 					uint8_t *cur_msg, size_t cur_msg_len)
    142 {
    143 	struct iovec iov[2];
    144 
    145 	iov[0].iov_base = (void *) prev_msg;
    146 	iov[0].iov_len = prev_msg_len;
    147 	iov[1].iov_base = cur_msg;
    148 	iov[1].iov_len = cur_msg_len - 12;
    149 
    150 	l_checksum_updatev(wsc->hmac_auth_key, iov, 2);
    151 	l_checksum_get_digest(wsc->hmac_auth_key, cur_msg + cur_msg_len - 8, 8);
    152 }
    153 
    154 static inline bool keywrap_authenticator_check(struct eap_wsc_state *wsc,
    155 						const uint8_t *pdu, size_t len)
    156 {
    157 	uint8_t authenticator[8];
    158 
    159 	/* We omit the included KeyWrapAuthenticator element from the hash */
    160 	l_checksum_update(wsc->hmac_auth_key, pdu, len - 12);
    161 	l_checksum_get_digest(wsc->hmac_auth_key, authenticator, 8);
    162 
    163 	/* KeyWrapAuthenticator is the last 8 bytes of the message */
    164 	if (memcmp(authenticator, pdu + len - 8, 8))
    165 		return false;
    166 
    167 	return true;
    168 }
    169 
    170 static inline void keywrap_authenticator_put(struct eap_wsc_state *wsc,
    171 						uint8_t *pdu, size_t len)
    172 {
    173 	l_checksum_update(wsc->hmac_auth_key, pdu, len - 12);
    174 	l_checksum_get_digest(wsc->hmac_auth_key, pdu + len - 8, 8);
    175 }
    176 
    177 static inline bool x_hash_check(struct eap_wsc_state *wsc,
    178 				uint8_t *x_snonce,
    179 				uint8_t *psk,
    180 				uint8_t *x_hash_expected)
    181 {
    182 	struct iovec iov[4];
    183 	uint8_t hash[32];
    184 
    185 	/*
    186 	 * WSC 2.0.5, Section 7.4:
    187 	 * The Enrollee creates two 128-bit secret nonces, E-S1, E-S2 and
    188 	 * then computes
    189 	 * E-Hash1 = HMACAuthKey(E-S1 || PSK1 || PKE || PKR)
    190 	 * E-Hash2 = HMACAuthKey(E-S2 || PSK2 || PKE || PKR)
    191 	 * The Registrar creates two 128-bit secret nonces, R-S1, R-S2 and
    192 	 * then computes
    193 	 * R-Hash1 = HMACAuthKey(R-S1 || PSK1 || PKE || PKR)
    194 	 * R-Hash2 = HMACAuthKey(R-S2 || PSK2 || PKE || PKR)
    195 	 */
    196 	iov[0].iov_base = x_snonce;
    197 	iov[0].iov_len = 16;
    198 	iov[1].iov_base = psk;
    199 	iov[1].iov_len = 16;
    200 	iov[2].iov_base = wsc->m1->public_key;
    201 	iov[2].iov_len = sizeof(wsc->m1->public_key);
    202 	iov[3].iov_base = wsc->m2->public_key;
    203 	iov[3].iov_len = sizeof(wsc->m2->public_key);
    204 	l_checksum_updatev(wsc->hmac_auth_key, iov, 4);
    205 	l_checksum_get_digest(wsc->hmac_auth_key, hash, sizeof(hash));
    206 
    207 	return !memcmp(hash, x_hash_expected, sizeof(hash));
    208 }
    209 
    210 static uint8_t *encrypted_settings_decrypt(struct eap_wsc_state *wsc,
    211 						const uint8_t *pdu,
    212 						size_t len,
    213 						size_t *out_len)
    214 {
    215 	size_t encrypted_len;
    216 	uint8_t *decrypted;
    217 	unsigned int i;
    218 	uint8_t pad;
    219 
    220 	/* WSC 2.0.5, Section 12, Encrypted Settings:
    221 	 * "The Data field of the Encrypted Settings attribute includes an
    222 	 * initialization vector (IV) followed by a set of encrypted Wi-Fi
    223 	 * Simple Configuration TLV attributes."
    224 	 *
    225 	 * Account for the IV being in the beginning 16 bytes
    226 	 */
    227 	if (len < 16)
    228 		return NULL;
    229 
    230 	encrypted_len = len - 16;
    231 	if (encrypted_len < 16 || encrypted_len % 16)
    232 		return NULL;
    233 
    234 	decrypted = l_malloc(encrypted_len);
    235 
    236 	l_cipher_set_iv(wsc->aes_cbc_128, pdu, 16);
    237 
    238 	if (!l_cipher_decrypt(wsc->aes_cbc_128, pdu + 16,
    239 						decrypted, encrypted_len))
    240 		goto fail;
    241 
    242 	/* Check that the pad value is sane */
    243 	pad = decrypted[encrypted_len - 1];
    244 	if (pad > encrypted_len)
    245 		goto fail;
    246 
    247 	for (i = 0; i < pad; i++) {
    248 		if (decrypted[encrypted_len - pad + i] == pad)
    249 			continue;
    250 
    251 		goto fail;
    252 	}
    253 
    254 	*out_len = encrypted_len - pad;
    255 
    256 	return decrypted;
    257 
    258 fail:
    259 	explicit_bzero(decrypted, encrypted_len);
    260 	l_free(decrypted);
    261 	return NULL;
    262 }
    263 
    264 static bool encrypted_settings_encrypt(struct eap_wsc_state *wsc,
    265 						const uint8_t *iv,
    266 						const uint8_t *in,
    267 						size_t in_len,
    268 						uint8_t *out,
    269 						size_t *out_len)
    270 {
    271 	size_t len = 0;
    272 	unsigned int i;
    273 	uint8_t pad;
    274 
    275 	l_cipher_set_iv(wsc->aes_cbc_128, iv, 16);
    276 	memcpy(out, iv, 16);
    277 	len += 16;
    278 
    279 	memcpy(out + len, in, in_len);
    280 	len += in_len;
    281 
    282 	pad = 16 - in_len % 16;
    283 
    284 	for (i = 0; i < pad; i++)
    285 		out[len++] = pad;
    286 
    287 	if (!l_cipher_encrypt(wsc->aes_cbc_128, out + 16, out + 16, len - 16)) {
    288 		explicit_bzero(out + 16, len - 16);
    289 		return false;
    290 	}
    291 
    292 	*out_len = len;
    293 	return true;
    294 }
    295 
    296 static void eap_wsc_state_free(struct eap_wsc_state *wsc)
    297 {
    298 	if (wsc->device_password) {
    299 		explicit_bzero(wsc->device_password,
    300 				strlen(wsc->device_password));
    301 		l_free(wsc->device_password);
    302 	}
    303 
    304 	l_key_free(wsc->private);
    305 
    306 	l_free(wsc->sent_pdu);
    307 	wsc->sent_pdu = NULL;
    308 	wsc->sent_len = 0;
    309 
    310 	if (wsc->rx_pdu_buf) {
    311 		l_free(wsc->rx_pdu_buf);
    312 		wsc->rx_pdu_buf = NULL;
    313 		wsc->rx_pdu_buf_len = 0;
    314 		wsc->rx_pdu_buf_offset = 0;
    315 	}
    316 
    317 	l_checksum_free(wsc->hmac_auth_key);
    318 	l_cipher_free(wsc->aes_cbc_128);
    319 
    320 	l_free(wsc->m1);
    321 	l_free(wsc->m2);
    322 
    323 	explicit_bzero(wsc->local_snonce1, 16);
    324 	explicit_bzero(wsc->local_snonce2, 16);
    325 	explicit_bzero(wsc->psk1, 16);
    326 	explicit_bzero(wsc->psk2, 16);
    327 	explicit_bzero(&wsc->wpa2_cred, sizeof(struct wsc_credential));
    328 	explicit_bzero(&wsc->open_cred, sizeof(struct wsc_credential));
    329 
    330 	l_free(wsc);
    331 }
    332 
    333 static void eap_wsc_free(struct eap_state *eap)
    334 {
    335 	struct eap_wsc_state *wsc = eap_get_data(eap);
    336 
    337 	eap_set_data(eap, NULL);
    338 	eap_wsc_state_free(wsc);
    339 }
    340 
    341 static void eap_wsc_r_send_start(struct eap_state *eap)
    342 {
    343 	uint8_t buf[EAP_WSC_HEADER_LEN];
    344 
    345 	buf[12] = WSC_OP_START;
    346 	buf[13] = 0;
    347 
    348 	eap_method_new_request(eap, buf, EAP_WSC_HEADER_LEN);
    349 }
    350 
    351 static void eap_wsc_send_fragment(struct eap_state *eap)
    352 {
    353 	struct eap_wsc_state *wsc = eap_get_data(eap);
    354 	size_t mtu = eap_get_mtu(eap);
    355 	uint8_t buf[mtu];
    356 	size_t len = wsc->sent_len - wsc->tx_frag_offset;
    357 	size_t header_len = EAP_WSC_HEADER_LEN;
    358 
    359 	buf[12] = WSC_OP_MSG;
    360 
    361 	if (len > mtu - EAP_WSC_HEADER_LEN) {
    362 		len = mtu - EAP_WSC_HEADER_LEN;
    363 		buf[13] = WSC_FLAG_MF;
    364 	} else {
    365 		buf[13] = 0;
    366 	}
    367 
    368 	if (!wsc->tx_frag_offset) {
    369 		buf[13] |= WSC_FLAG_LF;
    370 
    371 		l_put_be16(wsc->sent_len, &buf[14]);
    372 		len -= 2;
    373 		header_len += 2;
    374 	}
    375 
    376 	memcpy(buf + header_len, wsc->sent_pdu + wsc->tx_frag_offset, len);
    377 
    378 	if (wsc->registrar)
    379 		eap_method_new_request(eap, buf, header_len + len);
    380 	else
    381 		eap_method_respond(eap, buf, header_len + len);
    382 
    383 	wsc->tx_last_frag_len = len;
    384 }
    385 
    386 static void eap_wsc_send_message(struct eap_state *eap,
    387 						uint8_t *pdu, size_t pdu_len)
    388 {
    389 	struct eap_wsc_state *wsc = eap_get_data(eap);
    390 	size_t msg_len = pdu_len + EAP_WSC_HEADER_LEN;
    391 
    392 	eap_wsc_state_set_sent_pdu(wsc, pdu, pdu_len);
    393 
    394 	if (msg_len <= eap_get_mtu(eap)) {
    395 		uint8_t *buf = l_malloc(msg_len);
    396 
    397 		buf[12] = WSC_OP_MSG;
    398 		buf[13] = 0;
    399 		memcpy(buf + EAP_WSC_HEADER_LEN, pdu, pdu_len);
    400 
    401 		if (wsc->registrar)
    402 			eap_method_new_request(eap, buf, msg_len);
    403 		else
    404 			eap_method_respond(eap, buf, msg_len);
    405 
    406 		l_free(buf);
    407 		return;
    408 	}
    409 
    410 	wsc->tx_frag_offset = 0;
    411 	eap_wsc_send_fragment(eap);
    412 }
    413 
    414 static void eap_wsc_send_nack(struct eap_state *eap,
    415 					enum wsc_configuration_error error)
    416 {
    417 	struct eap_wsc_state *wsc = eap_get_data(eap);
    418 	struct wsc_nack nack;
    419 	uint8_t *pdu;
    420 	size_t pdu_len;
    421 	uint8_t buf[256];
    422 
    423 	/*
    424 	 * WSC 2.0.5, Table 34, Configuration Error 0 states:
    425 	 * "- not valid for WSC_NACK except when a station acts as an External
    426 	 * Registrar (to learn the current AP settings after M7 with
    427 	 * configuration error = 0)"
    428 	 *
    429 	 * However, section 7.7.3 states:
    430 	 * "Once M5 is sent, for example, if anything but M6 is received,
    431 	 * the Enrollee will respond with a NACK message."
    432 	 *
    433 	 * Section 7.1 states:
    434 	 * "If a message is received with either an invalid nonce or an invalid
    435 	 * Authenticator attribute, the recipient shall silently ignore this
    436 	 * message."
    437 	 *
    438 	 * So it is entirely unclear what to do in the situation of an
    439 	 * out-of-order message being sent.  To centralize decision making,
    440 	 * callers will call this function with error 0.
    441 	 */
    442 	if (error == WSC_CONFIGURATION_ERROR_NO_ERROR)
    443 		return;
    444 
    445 	nack.version2 = true;
    446 	if (wsc->state != STATE_EXPECT_M1)
    447 		memcpy(nack.enrollee_nonce, wsc->m1->enrollee_nonce,
    448 						sizeof(nack.enrollee_nonce));
    449 	else
    450 		memset(nack.enrollee_nonce, 0, sizeof(nack.enrollee_nonce));
    451 
    452 	if (wsc->m2)
    453 		memcpy(nack.registrar_nonce, wsc->m2->registrar_nonce,
    454 						sizeof(nack.registrar_nonce));
    455 	else
    456 		memset(nack.registrar_nonce, 0, sizeof(nack.registrar_nonce));
    457 
    458 	nack.configuration_error = error;
    459 
    460 	pdu = wsc_build_wsc_nack(&nack, &pdu_len);
    461 	if (!pdu)
    462 		return;
    463 
    464 	buf[12] = WSC_OP_NACK;
    465 	buf[13] = 0;
    466 	memcpy(buf + EAP_WSC_HEADER_LEN, pdu, pdu_len);
    467 
    468 	if (wsc->registrar)
    469 		eap_method_new_request(eap, buf, pdu_len + EAP_WSC_HEADER_LEN);
    470 	else
    471 		eap_method_respond(eap, buf, pdu_len + EAP_WSC_HEADER_LEN);
    472 
    473 	l_free(pdu);
    474 }
    475 
    476 static void eap_wsc_send_done(struct eap_state *eap)
    477 {
    478 	struct eap_wsc_state *wsc = eap_get_data(eap);
    479 	struct wsc_done done;
    480 	uint8_t *pdu;
    481 	size_t pdu_len;
    482 	uint8_t buf[256];
    483 
    484 	done.version2 = true;
    485 	memcpy(done.enrollee_nonce, wsc->m1->enrollee_nonce,
    486 						sizeof(done.enrollee_nonce));
    487 	memcpy(done.registrar_nonce, wsc->m2->registrar_nonce,
    488 						sizeof(done.registrar_nonce));
    489 
    490 	pdu = wsc_build_wsc_done(&done, &pdu_len);
    491 	if (!pdu)
    492 		return;
    493 
    494 	buf[12] = WSC_OP_DONE;
    495 	buf[13] = 0;
    496 	memcpy(buf + EAP_WSC_HEADER_LEN, pdu, pdu_len);
    497 
    498 	eap_method_respond(eap, buf, pdu_len + EAP_WSC_HEADER_LEN);
    499 	l_free(pdu);
    500 }
    501 
    502 static void eap_wsc_send_frag_ack(struct eap_state *eap)
    503 {
    504 	struct eap_wsc_state *wsc = eap_get_data(eap);
    505 	uint8_t buf[EAP_WSC_HEADER_LEN];
    506 
    507 	buf[12] = WSC_OP_FRAG_ACK;
    508 	buf[13] = 0;
    509 
    510 	if (wsc->registrar)
    511 		eap_method_new_request(eap, buf, EAP_WSC_HEADER_LEN);
    512 	else
    513 		eap_method_respond(eap, buf, EAP_WSC_HEADER_LEN);
    514 }
    515 
    516 static void eap_wsc_handle_m8(struct eap_state *eap,
    517 					const uint8_t *pdu, size_t len)
    518 {
    519 	struct eap_wsc_state *wsc = eap_get_data(eap);
    520 	struct wsc_m8 m8;
    521 	struct iovec encrypted;
    522 	uint8_t *decrypted;
    523 	size_t decrypted_len;
    524 	struct wsc_m8_encrypted_settings m8es;
    525 	struct iovec creds[3];
    526 	size_t n_creds;
    527 	size_t i;
    528 	bool nack = true;
    529 
    530 	/* Spec unclear what to do here, see comments in eap_wsc_send_nack */
    531 	if (wsc_parse_m8(pdu, len, &m8, &encrypted) != 0) {
    532 		eap_wsc_send_nack(eap, WSC_CONFIGURATION_ERROR_NO_ERROR);
    533 		return;
    534 	}
    535 
    536 	if (memcmp(m8.enrollee_nonce, wsc->m1->enrollee_nonce,
    537 						sizeof(m8.enrollee_nonce)))
    538 		return;
    539 
    540 	if (!authenticator_check(wsc, pdu, len))
    541 		return;
    542 
    543 	decrypted = encrypted_settings_decrypt(wsc, encrypted.iov_base,
    544 							encrypted.iov_len,
    545 							&decrypted_len);
    546 	if (!decrypted)
    547 		goto send_nack;
    548 
    549 	n_creds = L_ARRAY_SIZE(creds);
    550 
    551 	if (wsc_parse_m8_encrypted_settings(decrypted, decrypted_len,
    552 						&m8es, creds, &n_creds))
    553 		goto cleanup;
    554 
    555 	if (!keywrap_authenticator_check(wsc, decrypted, decrypted_len))
    556 		goto cleanup;
    557 
    558 	for (i = 0; i < n_creds; i++) {
    559 		struct wsc_credential cred;
    560 
    561 		if (wsc_parse_credential(creds[i].iov_base, creds[i].iov_len,
    562 						&cred) == 0)
    563 			eap_method_event(eap, EAP_WSC_EVENT_CREDENTIAL_OBTAINED,
    564 						&cred);
    565 
    566 		explicit_bzero(&cred, sizeof(cred));
    567 	}
    568 
    569 	eap_wsc_send_done(eap);
    570 	wsc->state = STATE_FINISHED;
    571 	nack = false;
    572 
    573 cleanup:
    574 	explicit_bzero(&m8es, sizeof(m8es));
    575 	explicit_bzero(decrypted, decrypted_len);
    576 	l_free(decrypted);
    577 
    578 	if (!nack)
    579 		return;
    580 
    581 send_nack:
    582 	eap_wsc_send_nack(eap, WSC_CONFIGURATION_ERROR_DECRYPTION_CRC_FAILURE);
    583 }
    584 
    585 static void eap_wsc_send_m7(struct eap_state *eap,
    586 				const uint8_t *m6_pdu, size_t m6_len)
    587 {
    588 	struct eap_wsc_state *wsc = eap_get_data(eap);
    589 	struct wsc_m7_encrypted_settings m7es;
    590 	struct wsc_m7 m7;
    591 	uint8_t *pdu;
    592 	size_t pdu_len;
    593 	/* 20 for SNonce, 12 for Authenticator, 16 for IV + up to 16 pad */
    594 	uint8_t encrypted[64];
    595 	size_t encrypted_len;
    596 	bool r;
    597 
    598 	memcpy(m7es.e_snonce2, wsc->local_snonce2, sizeof(wsc->local_snonce2));
    599 	pdu = wsc_build_m7_encrypted_settings(&m7es, &pdu_len);
    600 	explicit_bzero(m7es.e_snonce2, sizeof(wsc->local_snonce2));
    601 	if (!pdu)
    602 		return;
    603 
    604 	keywrap_authenticator_put(wsc, pdu, pdu_len);
    605 	r = encrypted_settings_encrypt(wsc, wsc->iv2, pdu, pdu_len,
    606 						encrypted, &encrypted_len);
    607 	explicit_bzero(pdu, pdu_len);
    608 	l_free(pdu);
    609 
    610 	if (!r)
    611 		return;
    612 
    613 	m7.version2 = true;
    614 	memcpy(m7.registrar_nonce, wsc->m2->registrar_nonce,
    615 						sizeof(m7.registrar_nonce));
    616 
    617 	pdu = wsc_build_m7(&m7, encrypted, encrypted_len, &pdu_len);
    618 	if (!pdu)
    619 		return;
    620 
    621 	authenticator_put(wsc, m6_pdu, m6_len, pdu, pdu_len);
    622 	eap_wsc_send_message(eap, pdu, pdu_len);
    623 	wsc->state = STATE_EXPECT_M8;
    624 }
    625 
    626 static void eap_wsc_handle_m6(struct eap_state *eap,
    627 					const uint8_t *pdu, size_t len)
    628 {
    629 	struct eap_wsc_state *wsc = eap_get_data(eap);
    630 	struct wsc_m6 m6;
    631 	struct iovec encrypted;
    632 	uint8_t *decrypted;
    633 	size_t decrypted_len;
    634 	struct wsc_m6_encrypted_settings m6es;
    635 	enum wsc_configuration_error error = WSC_CONFIGURATION_ERROR_NO_ERROR;
    636 
    637 	/* Spec unclear what to do here, see comments in eap_wsc_send_nack */
    638 	if (wsc_parse_m6(pdu, len, &m6, &encrypted) != 0)
    639 		goto send_nack;
    640 
    641 	if (memcmp(m6.enrollee_nonce, wsc->m1->enrollee_nonce,
    642 						sizeof(m6.enrollee_nonce)))
    643 		return;
    644 
    645 	if (!authenticator_check(wsc, pdu, len))
    646 		return;
    647 
    648 	decrypted = encrypted_settings_decrypt(wsc, encrypted.iov_base,
    649 							encrypted.iov_len,
    650 							&decrypted_len);
    651 	if (!decrypted) {
    652 		error = WSC_CONFIGURATION_ERROR_DECRYPTION_CRC_FAILURE;
    653 		goto send_nack;
    654 	}
    655 
    656 	if (wsc_parse_m6_encrypted_settings(decrypted, decrypted_len, &m6es)) {
    657 		error = WSC_CONFIGURATION_ERROR_DECRYPTION_CRC_FAILURE;
    658 		goto cleanup;
    659 	}
    660 
    661 	if (!keywrap_authenticator_check(wsc, decrypted, decrypted_len)) {
    662 		error = WSC_CONFIGURATION_ERROR_DECRYPTION_CRC_FAILURE;
    663 		goto cleanup;
    664 	}
    665 
    666 	/* We now have R-SNonce2, verify R-Hash2 stored in eap_wsc_handle_m4 */
    667 	if (!x_hash_check(wsc, m6es.r_snonce2, wsc->psk2, wsc->r_hash2)) {
    668 		error = WSC_CONFIGURATION_ERROR_DEVICE_PASSWORD_AUTH_FAILURE;
    669 		goto cleanup;
    670 	}
    671 
    672 	eap_wsc_send_m7(eap, pdu, len);
    673 
    674 cleanup:
    675 	explicit_bzero(&m6es, sizeof(m6es));
    676 	explicit_bzero(decrypted, decrypted_len);
    677 	l_free(decrypted);
    678 
    679 	if (!error)
    680 		return;
    681 
    682 send_nack:
    683 	eap_wsc_send_nack(eap, error);
    684 }
    685 
    686 static void eap_wsc_send_m5(struct eap_state *eap,
    687 				const uint8_t *m4_pdu, size_t m4_len)
    688 {
    689 	struct eap_wsc_state *wsc = eap_get_data(eap);
    690 	struct wsc_m5_encrypted_settings m5es;
    691 	struct wsc_m5 m5;
    692 	uint8_t *pdu;
    693 	size_t pdu_len;
    694 	/* 20 for SNonce, 12 for Authenticator, 16 for IV + up to 16 pad */
    695 	uint8_t encrypted[64];
    696 	size_t encrypted_len;
    697 	bool r;
    698 
    699 	memset(m5.authenticator, 0, sizeof(m5.authenticator));
    700 
    701 	memcpy(m5es.e_snonce1, wsc->local_snonce1, sizeof(wsc->local_snonce1));
    702 	pdu = wsc_build_m5_encrypted_settings(&m5es, &pdu_len);
    703 	explicit_bzero(m5es.e_snonce1, sizeof(wsc->local_snonce1));
    704 	if (!pdu)
    705 		return;
    706 
    707 	keywrap_authenticator_put(wsc, pdu, pdu_len);
    708 	r = encrypted_settings_encrypt(wsc, wsc->iv1, pdu, pdu_len,
    709 						encrypted, &encrypted_len);
    710 	explicit_bzero(pdu, pdu_len);
    711 	l_free(pdu);
    712 
    713 	if (!r)
    714 		return;
    715 
    716 	m5.version2 = true;
    717 	memcpy(m5.registrar_nonce, wsc->m2->registrar_nonce,
    718 						sizeof(m5.registrar_nonce));
    719 
    720 	pdu = wsc_build_m5(&m5, encrypted, encrypted_len, &pdu_len);
    721 	if (!pdu)
    722 		return;
    723 
    724 	authenticator_put(wsc, m4_pdu, m4_len, pdu, pdu_len);
    725 	eap_wsc_send_message(eap, pdu, pdu_len);
    726 	wsc->state = STATE_EXPECT_M6;
    727 }
    728 
    729 static void eap_wsc_handle_m4(struct eap_state *eap,
    730 					const uint8_t *pdu, size_t len)
    731 {
    732 	struct eap_wsc_state *wsc = eap_get_data(eap);
    733 	struct wsc_m4 m4;
    734 	struct iovec encrypted;
    735 	uint8_t *decrypted;
    736 	size_t decrypted_len;
    737 	struct wsc_m4_encrypted_settings m4es;
    738 	enum wsc_configuration_error error = WSC_CONFIGURATION_ERROR_NO_ERROR;
    739 
    740 	/* Spec unclear what to do here, see comments in eap_wsc_send_nack */
    741 	if (wsc_parse_m4(pdu, len, &m4, &encrypted) != 0)
    742 		goto send_nack;
    743 
    744 	if (memcmp(m4.enrollee_nonce, wsc->m1->enrollee_nonce,
    745 						sizeof(m4.enrollee_nonce)))
    746 		return;
    747 
    748 	if (!authenticator_check(wsc, pdu, len))
    749 		return;
    750 
    751 	decrypted = encrypted_settings_decrypt(wsc, encrypted.iov_base,
    752 							encrypted.iov_len,
    753 							&decrypted_len);
    754 	if (!decrypted) {
    755 		error = WSC_CONFIGURATION_ERROR_DECRYPTION_CRC_FAILURE;
    756 		goto send_nack;
    757 	}
    758 
    759 	if (wsc_parse_m4_encrypted_settings(decrypted, decrypted_len, &m4es)) {
    760 		error = WSC_CONFIGURATION_ERROR_DECRYPTION_CRC_FAILURE;
    761 		goto cleanup;
    762 	}
    763 
    764 	if (!keywrap_authenticator_check(wsc, decrypted, decrypted_len)) {
    765 		error = WSC_CONFIGURATION_ERROR_DECRYPTION_CRC_FAILURE;
    766 		goto cleanup;
    767 	}
    768 
    769 	/* Since we have obtained R-SNonce1, we can now verify R-Hash1. */
    770 	if (!x_hash_check(wsc, m4es.r_snonce1, wsc->psk1, m4.r_hash1)) {
    771 		error = WSC_CONFIGURATION_ERROR_DEVICE_PASSWORD_AUTH_FAILURE;
    772 		goto cleanup;
    773 	}
    774 
    775 	/* Now store R_Hash2 so we can verify it when we receive M6 */
    776 	memcpy(wsc->r_hash2, m4.r_hash2, sizeof(m4.r_hash2));
    777 	eap_wsc_send_m5(eap, pdu, len);
    778 
    779 cleanup:
    780 	explicit_bzero(&m4es, sizeof(m4es));
    781 	explicit_bzero(decrypted, decrypted_len);
    782 	l_free(decrypted);
    783 
    784 	if (!error)
    785 		return;
    786 
    787 send_nack:
    788 	eap_wsc_send_nack(eap, error);
    789 }
    790 
    791 static void eap_wsc_send_m3(struct eap_state *eap,
    792 				const uint8_t *m2_pdu, size_t m2_len)
    793 {
    794 	struct eap_wsc_state *wsc = eap_get_data(eap);
    795 	struct wsc_m2 *m2 = wsc->m2;
    796 	size_t len;
    797 	size_t len_half1;
    798 	struct wsc_m3 m3;
    799 	struct iovec iov[4];
    800 	uint8_t *pdu;
    801 	size_t pdu_len;
    802 
    803 	memset(m3.authenticator, 0, sizeof(m3.authenticator));
    804 
    805 	len = strlen(wsc->device_password);
    806 
    807 	/* WSC 2.0.5, Section 7.4:
    808 	 * In case the UTF8 representation of the DevicePassword length is an
    809 	 * odd number (N), the first half of DevicePassword will have length
    810 	 * of N/2+1 and the second half of the DevicePassword will have length
    811 	 * of N/2.
    812 	 */
    813 	len_half1 = len / 2;
    814 	if ((len % 2) == 1)
    815 		len_half1 += 1;
    816 
    817 	l_checksum_update(wsc->hmac_auth_key, wsc->device_password, len_half1);
    818 	l_checksum_get_digest(wsc->hmac_auth_key, wsc->psk1, sizeof(wsc->psk1));
    819 
    820 	l_checksum_update(wsc->hmac_auth_key, wsc->device_password + len_half1,
    821 					len / 2);
    822 	l_checksum_get_digest(wsc->hmac_auth_key, wsc->psk2, sizeof(wsc->psk2));
    823 
    824 	m3.version2 = true;
    825 	memcpy(m3.registrar_nonce, m2->registrar_nonce,
    826 						sizeof(m3.registrar_nonce));
    827 
    828 	/* WSC 2.0.5, Section 7.4:
    829 	 * The Enrollee creates two 128-bit secret nonces, E-S1, E-S2 and then
    830 	 * computes:
    831 	 * E-Hash1 = HMACAuthKey(E-S1 || PSK1 || PKE || PKR)
    832 	 * E-Hash2 = HMACAuthKey(E-S2 || PSK2 || PKE || PKR)
    833 	 */
    834 	iov[0].iov_base = wsc->local_snonce1;
    835 	iov[0].iov_len = sizeof(wsc->local_snonce1);
    836 	iov[1].iov_base = wsc->psk1;
    837 	iov[1].iov_len = sizeof(wsc->psk1);
    838 	iov[2].iov_base = wsc->m1->public_key;
    839 	iov[2].iov_len = sizeof(wsc->m1->public_key);
    840 	iov[3].iov_base = m2->public_key;
    841 	iov[3].iov_len = sizeof(m2->public_key);
    842 	l_checksum_updatev(wsc->hmac_auth_key, iov, 4);
    843 	l_checksum_get_digest(wsc->hmac_auth_key,
    844 					m3.e_hash1, sizeof(m3.e_hash1));
    845 
    846 	iov[0].iov_base = wsc->local_snonce2;
    847 	iov[0].iov_len = sizeof(wsc->local_snonce2);
    848 	iov[1].iov_base = wsc->psk2;
    849 	iov[1].iov_len = sizeof(wsc->psk2);
    850 	l_checksum_updatev(wsc->hmac_auth_key, iov, 4);
    851 	l_checksum_get_digest(wsc->hmac_auth_key,
    852 					m3.e_hash2, sizeof(m3.e_hash2));
    853 
    854 	pdu = wsc_build_m3(&m3, &pdu_len);
    855 	if (!pdu)
    856 		return;
    857 
    858 	authenticator_put(wsc, m2_pdu, m2_len, pdu, pdu_len);
    859 	eap_wsc_send_message(eap, pdu, pdu_len);
    860 	wsc->state = STATE_EXPECT_M4;
    861 }
    862 
    863 static void eap_wsc_handle_m2(struct eap_state *eap,
    864 					const uint8_t *pdu, size_t len)
    865 {
    866 	struct eap_wsc_state *wsc = eap_get_data(eap);
    867 	struct l_key *remote_public;
    868 	uint8_t shared_secret[192] = { 0 };
    869 	size_t shared_secret_len = sizeof(shared_secret);
    870 	struct l_checksum *sha256;
    871 	uint8_t dhkey[32];
    872 	struct l_checksum *hmac_sha256;
    873 	struct iovec iov[3];
    874 	uint8_t kdk[32];
    875 	struct wsc_session_key keys;
    876 	bool r;
    877 
    878 	/* TODO: Check to see if message is M2D first */
    879 	if (!wsc->m2)
    880 		wsc->m2 = l_new(struct wsc_m2, 1);
    881 
    882 	/* Spec unclear what to do here, see comments in eap_wsc_send_nack */
    883 	if (wsc_parse_m2(pdu, len, wsc->m2) != 0) {
    884 		eap_wsc_send_nack(eap, WSC_CONFIGURATION_ERROR_NO_ERROR);
    885 		return;
    886 	}
    887 
    888 	if (memcmp(wsc->m2->enrollee_nonce, wsc->m1->enrollee_nonce,
    889 					sizeof(wsc->m2->enrollee_nonce)))
    890 		return;
    891 
    892 	if (!l_key_validate_dh_payload(wsc->m2->public_key,
    893 					sizeof(wsc->m2->public_key),
    894 					crypto_dh5_prime,
    895 					crypto_dh5_prime_size))
    896 		return;
    897 
    898 	remote_public = l_key_new(L_KEY_RAW, wsc->m2->public_key,
    899 						sizeof(wsc->m2->public_key));
    900 	if (!remote_public)
    901 		return;
    902 
    903 	r = l_key_compute_dh_secret(remote_public, wsc->private, dh5_prime,
    904 					shared_secret, &shared_secret_len);
    905 	l_key_free(remote_public);
    906 
    907 	if (!r)
    908 		return;
    909 
    910 	sha256 = l_checksum_new(L_CHECKSUM_SHA256);
    911 	if (!sha256)
    912 		return;
    913 
    914 	l_checksum_update(sha256, shared_secret, shared_secret_len);
    915 	l_checksum_get_digest(sha256, dhkey, sizeof(dhkey));
    916 	l_checksum_free(sha256);
    917 
    918 	explicit_bzero(shared_secret, shared_secret_len);
    919 
    920 	hmac_sha256 = l_checksum_new_hmac(L_CHECKSUM_SHA256,
    921 							dhkey, sizeof(dhkey));
    922 	explicit_bzero(dhkey, sizeof(dhkey));
    923 
    924 	if (!hmac_sha256)
    925 		return;
    926 
    927 	iov[0].iov_base = wsc->m1->enrollee_nonce;
    928 	iov[0].iov_len = 16;
    929 	iov[1].iov_base = wsc->m1->addr;
    930 	iov[1].iov_len = 6;
    931 	iov[2].iov_base = wsc->m2->registrar_nonce;
    932 	iov[2].iov_len = 16;
    933 
    934 	l_checksum_updatev(hmac_sha256, iov, 3);
    935 	l_checksum_get_digest(hmac_sha256, kdk, sizeof(kdk));
    936 	l_checksum_free(hmac_sha256);
    937 
    938 	r = wsc_kdf(kdk, &keys, sizeof(keys));
    939 	explicit_bzero(kdk, sizeof(kdk));
    940 	if (!r)
    941 		return;
    942 
    943 	wsc->hmac_auth_key = l_checksum_new_hmac(L_CHECKSUM_SHA256,
    944 						keys.auth_key,
    945 						sizeof(keys.auth_key));
    946 	if (!authenticator_check(wsc, pdu, len)) {
    947 		l_checksum_free(wsc->hmac_auth_key);
    948 		wsc->hmac_auth_key = NULL;
    949 		goto clear_keys;
    950 	}
    951 
    952 	/* Everything checks out, lets build M3 */
    953 	eap_wsc_send_m3(eap, pdu, len);
    954 
    955 	/*
    956 	 * AuthKey is uploaded into the kernel, once we upload KeyWrapKey,
    957 	 * the keys variable is no longer useful.  Make sure to wipe it
    958 	 */
    959 	wsc->aes_cbc_128 = l_cipher_new(L_CIPHER_AES_CBC, keys.keywrap_key,
    960 						sizeof(keys.keywrap_key));
    961 
    962 clear_keys:
    963 	explicit_bzero(&keys, sizeof(keys));
    964 }
    965 
    966 static void eap_wsc_r_send_m8(struct eap_state *eap,
    967 				const uint8_t *m7_pdu, size_t m7_len)
    968 {
    969 	struct eap_wsc_state *wsc = eap_get_data(eap);
    970 	struct wsc_m8_encrypted_settings m8es = {};
    971 	struct wsc_m8 m8;
    972 	uint8_t *pdu;
    973 	size_t pdu_len;
    974 	/* At least: 2 * credential, 12 for Authenticator, 16 for IV + up to 16 pad */
    975 	uint8_t encrypted[1024];
    976 	size_t encrypted_len;
    977 	bool r;
    978 	struct wsc_credential creds[2];
    979 	unsigned int creds_cnt = 0;
    980 	unsigned int auth_types =
    981 		wsc->m1->auth_type_flags & wsc->m2->auth_type_flags;
    982 
    983 	memset(m8.authenticator, 0, sizeof(m8.authenticator));
    984 
    985 	if (auth_types & WSC_AUTHENTICATION_TYPE_OPEN)
    986 		memcpy(&creds[creds_cnt++], &wsc->open_cred,
    987 			sizeof(struct wsc_credential));
    988 
    989 	if (auth_types & WSC_AUTHENTICATION_TYPE_WPA2_PERSONAL)
    990 		memcpy(&creds[creds_cnt++], &wsc->wpa2_cred,
    991 			sizeof(struct wsc_credential));
    992 
    993 	pdu = wsc_build_m8_encrypted_settings(&m8es, creds, creds_cnt,
    994 						&pdu_len);
    995 	explicit_bzero(creds, creds_cnt * sizeof(struct wsc_credential));
    996 	if (!pdu)
    997 		return;
    998 
    999 	keywrap_authenticator_put(wsc, pdu, pdu_len);
   1000 	r = encrypted_settings_encrypt(wsc, wsc->iv3, pdu, pdu_len,
   1001 						encrypted, &encrypted_len);
   1002 	explicit_bzero(pdu, pdu_len);
   1003 	l_free(pdu);
   1004 
   1005 	if (!r)
   1006 		return;
   1007 
   1008 	m8.version2 = true;
   1009 	memcpy(m8.enrollee_nonce, wsc->m1->enrollee_nonce,
   1010 						sizeof(m8.enrollee_nonce));
   1011 
   1012 	pdu = wsc_build_m8(&m8, encrypted, encrypted_len, &pdu_len);
   1013 	if (!pdu)
   1014 		return;
   1015 
   1016 	authenticator_put(wsc, m7_pdu, m7_len, pdu, pdu_len);
   1017 	eap_wsc_send_message(eap, pdu, pdu_len);
   1018 	wsc->state = STATE_EXPECT_DONE;
   1019 }
   1020 
   1021 static void eap_wsc_r_handle_m7(struct eap_state *eap,
   1022 					const uint8_t *pdu, size_t len)
   1023 {
   1024 	struct eap_wsc_state *wsc = eap_get_data(eap);
   1025 	struct wsc_m7 m7;
   1026 	struct iovec encrypted;
   1027 	uint8_t *decrypted;
   1028 	size_t decrypted_len;
   1029 	struct wsc_m7_encrypted_settings m7es;
   1030 	enum wsc_configuration_error error = WSC_CONFIGURATION_ERROR_NO_ERROR;
   1031 
   1032 	memset(m7es.authenticator, 0, sizeof(m7es.authenticator));
   1033 	memset(m7.authenticator, 0, sizeof(m7.authenticator));
   1034 
   1035 	/* Spec unclear what to do here, see comments in eap_wsc_send_nack */
   1036 	if (wsc_parse_m7(pdu, len, &m7, &encrypted) != 0)
   1037 		goto send_nack;
   1038 
   1039 	if (memcmp(m7.registrar_nonce, wsc->m2->registrar_nonce,
   1040 						sizeof(m7.registrar_nonce)))
   1041 		goto send_nack;
   1042 
   1043 	if (!authenticator_check(wsc, pdu, len))
   1044 		return;
   1045 
   1046 	decrypted = encrypted_settings_decrypt(wsc, encrypted.iov_base,
   1047 							encrypted.iov_len,
   1048 							&decrypted_len);
   1049 	if (!decrypted) {
   1050 		error = WSC_CONFIGURATION_ERROR_DECRYPTION_CRC_FAILURE;
   1051 		goto send_nack;
   1052 	}
   1053 
   1054 	if (wsc_parse_m7_encrypted_settings(decrypted, decrypted_len, &m7es)) {
   1055 		error = WSC_CONFIGURATION_ERROR_DECRYPTION_CRC_FAILURE;
   1056 		goto cleanup;
   1057 	}
   1058 
   1059 	if (!keywrap_authenticator_check(wsc, decrypted, decrypted_len)) {
   1060 		error = WSC_CONFIGURATION_ERROR_DECRYPTION_CRC_FAILURE;
   1061 		goto cleanup;
   1062 	}
   1063 
   1064 	/* We have E-SNonce2, verify E-Hash2 stored in eap_wsc_r_handle_m3 */
   1065 	if (!x_hash_check(wsc, m7es.e_snonce2, wsc->psk2, wsc->e_hash2)) {
   1066 		error = WSC_CONFIGURATION_ERROR_DEVICE_PASSWORD_AUTH_FAILURE;
   1067 		goto cleanup;
   1068 	}
   1069 
   1070 	eap_wsc_r_send_m8(eap, pdu, len);
   1071 
   1072 cleanup:
   1073 	explicit_bzero(&m7es, sizeof(m7es));
   1074 	explicit_bzero(decrypted, decrypted_len);
   1075 	l_free(decrypted);
   1076 
   1077 	if (!error)
   1078 		return;
   1079 
   1080 send_nack:
   1081 	eap_wsc_send_nack(eap, error);
   1082 }
   1083 
   1084 static void eap_wsc_r_send_m6(struct eap_state *eap,
   1085 				const uint8_t *m5_pdu, size_t m5_len)
   1086 {
   1087 	struct eap_wsc_state *wsc = eap_get_data(eap);
   1088 	struct wsc_m6_encrypted_settings m6es;
   1089 	struct wsc_m6 m6;
   1090 	uint8_t *pdu;
   1091 	size_t pdu_len;
   1092 	/* 20 for SNonce, 12 for Authenticator, 16 for IV + up to 16 pad */
   1093 	uint8_t encrypted[64];
   1094 	size_t encrypted_len;
   1095 	bool r;
   1096 
   1097 	memset(m6es.authenticator, 0, sizeof(m6es.authenticator));
   1098 	memset(m6.authenticator, 0, sizeof(m6.authenticator));
   1099 
   1100 	memcpy(m6es.r_snonce2, wsc->local_snonce2, sizeof(wsc->local_snonce2));
   1101 	pdu = wsc_build_m6_encrypted_settings(&m6es, &pdu_len);
   1102 	explicit_bzero(m6es.r_snonce2, sizeof(wsc->local_snonce2));
   1103 	if (!pdu)
   1104 		return;
   1105 
   1106 	keywrap_authenticator_put(wsc, pdu, pdu_len);
   1107 	r = encrypted_settings_encrypt(wsc, wsc->iv2, pdu, pdu_len,
   1108 						encrypted, &encrypted_len);
   1109 	explicit_bzero(pdu, pdu_len);
   1110 	l_free(pdu);
   1111 
   1112 	if (!r)
   1113 		return;
   1114 
   1115 	m6.version2 = true;
   1116 	memcpy(m6.enrollee_nonce, wsc->m1->enrollee_nonce,
   1117 						sizeof(m6.enrollee_nonce));
   1118 
   1119 	pdu = wsc_build_m6(&m6, encrypted, encrypted_len, &pdu_len);
   1120 	if (!pdu)
   1121 		return;
   1122 
   1123 	authenticator_put(wsc, m5_pdu, m5_len, pdu, pdu_len);
   1124 	eap_wsc_send_message(eap, pdu, pdu_len);
   1125 	wsc->state = STATE_EXPECT_M7;
   1126 }
   1127 
   1128 static void eap_wsc_r_handle_m5(struct eap_state *eap,
   1129 					const uint8_t *pdu, size_t len)
   1130 {
   1131 	struct eap_wsc_state *wsc = eap_get_data(eap);
   1132 	struct wsc_m5 m5;
   1133 	struct iovec encrypted;
   1134 	uint8_t *decrypted;
   1135 	size_t decrypted_len;
   1136 	struct wsc_m5_encrypted_settings m5es;
   1137 	enum wsc_configuration_error error = WSC_CONFIGURATION_ERROR_NO_ERROR;
   1138 
   1139 	memset(m5es.authenticator, 0, sizeof(m5es.authenticator));
   1140 
   1141 	/* Spec unclear what to do here, see comments in eap_wsc_send_nack */
   1142 	if (wsc_parse_m5(pdu, len, &m5, &encrypted) != 0)
   1143 		goto send_nack;
   1144 
   1145 	if (memcmp(m5.registrar_nonce, wsc->m2->registrar_nonce,
   1146 						sizeof(m5.registrar_nonce)))
   1147 		goto send_nack;
   1148 
   1149 	if (!authenticator_check(wsc, pdu, len))
   1150 		return;
   1151 
   1152 	decrypted = encrypted_settings_decrypt(wsc, encrypted.iov_base,
   1153 							encrypted.iov_len,
   1154 							&decrypted_len);
   1155 	if (!decrypted) {
   1156 		error = WSC_CONFIGURATION_ERROR_DECRYPTION_CRC_FAILURE;
   1157 		goto send_nack;
   1158 	}
   1159 
   1160 	if (wsc_parse_m5_encrypted_settings(decrypted, decrypted_len, &m5es)) {
   1161 		error = WSC_CONFIGURATION_ERROR_DECRYPTION_CRC_FAILURE;
   1162 		goto cleanup;
   1163 	}
   1164 
   1165 	if (!keywrap_authenticator_check(wsc, decrypted, decrypted_len)) {
   1166 		error = WSC_CONFIGURATION_ERROR_DECRYPTION_CRC_FAILURE;
   1167 		goto cleanup;
   1168 	}
   1169 
   1170 	/* We have E-SNonce1, verify E-Hash1 stored in eap_wsc_r_handle_m3 */
   1171 	if (!x_hash_check(wsc, m5es.e_snonce1, wsc->psk1, wsc->e_hash1)) {
   1172 		error = WSC_CONFIGURATION_ERROR_DEVICE_PASSWORD_AUTH_FAILURE;
   1173 		goto cleanup;
   1174 	}
   1175 
   1176 	eap_wsc_r_send_m6(eap, pdu, len);
   1177 
   1178 cleanup:
   1179 	explicit_bzero(&m5es, sizeof(m5es));
   1180 	explicit_bzero(decrypted, decrypted_len);
   1181 	l_free(decrypted);
   1182 
   1183 	if (!error)
   1184 		return;
   1185 
   1186 send_nack:
   1187 	eap_wsc_send_nack(eap, error);
   1188 }
   1189 
   1190 static void eap_wsc_r_send_m4(struct eap_state *eap,
   1191 				const uint8_t *m3_pdu, size_t m3_len)
   1192 {
   1193 	struct eap_wsc_state *wsc = eap_get_data(eap);
   1194 	struct wsc_m4_encrypted_settings m4es;
   1195 	struct wsc_m4 m4;
   1196 	uint8_t *pdu;
   1197 	size_t pdu_len;
   1198 	/* 20 for SNonce, 12 for Authenticator, 16 for IV + up to 16 pad */
   1199 	uint8_t encrypted[64];
   1200 	size_t encrypted_len;
   1201 	bool r;
   1202 	size_t len;
   1203 	size_t len_half1;
   1204 	struct iovec iov[4];
   1205 
   1206 	memset(m4es.authenticator, 0, sizeof(m4es.authenticator));
   1207 	memset(m4.authenticator, 0, sizeof(m4.authenticator));
   1208 
   1209 	memcpy(m4es.r_snonce1, wsc->local_snonce1, sizeof(wsc->local_snonce1));
   1210 	pdu = wsc_build_m4_encrypted_settings(&m4es, &pdu_len);
   1211 	explicit_bzero(m4es.r_snonce1, sizeof(wsc->local_snonce1));
   1212 	if (!pdu)
   1213 		return;
   1214 
   1215 	keywrap_authenticator_put(wsc, pdu, pdu_len);
   1216 	r = encrypted_settings_encrypt(wsc, wsc->iv1, pdu, pdu_len,
   1217 						encrypted, &encrypted_len);
   1218 	explicit_bzero(pdu, pdu_len);
   1219 	l_free(pdu);
   1220 
   1221 	if (!r)
   1222 		return;
   1223 
   1224 	len = strlen(wsc->device_password);
   1225 
   1226 	/* WSC 2.0.5, Section 7.4:
   1227 	 * In case the UTF8 representation of the DevicePassword length is an
   1228 	 * odd number (N), the first half of DevicePassword will have length
   1229 	 * of N/2+1 and the second half of the DevicePassword will have length
   1230 	 * of N/2.
   1231 	 */
   1232 	len_half1 = (len + 1) / 2;
   1233 
   1234 	l_checksum_update(wsc->hmac_auth_key, wsc->device_password, len_half1);
   1235 	l_checksum_get_digest(wsc->hmac_auth_key, wsc->psk1, sizeof(wsc->psk1));
   1236 
   1237 	l_checksum_update(wsc->hmac_auth_key, wsc->device_password + len_half1,
   1238 					len / 2);
   1239 	l_checksum_get_digest(wsc->hmac_auth_key, wsc->psk2, sizeof(wsc->psk2));
   1240 
   1241 	m4.version2 = true;
   1242 	memcpy(m4.enrollee_nonce, wsc->m1->enrollee_nonce,
   1243 						sizeof(m4.enrollee_nonce));
   1244 
   1245 	/* WSC 2.0.5, Section 7.4:
   1246 	 * The Registrar creates two 128-bit secret nonces, R-S1, R-S2 and then
   1247 	 * computes:
   1248 	 * R-Hash1 = HMACAuthKey(R-S1 || PSK1 || PKE || PKR)
   1249 	 * R-Hash2 = HMACAuthKey(R-S2 || PSK2 || PKE || PKR)
   1250 	 */
   1251 	iov[0].iov_base = wsc->local_snonce1;
   1252 	iov[0].iov_len = sizeof(wsc->local_snonce1);
   1253 	iov[1].iov_base = wsc->psk1;
   1254 	iov[1].iov_len = sizeof(wsc->psk1);
   1255 	iov[2].iov_base = wsc->m1->public_key;
   1256 	iov[2].iov_len = sizeof(wsc->m1->public_key);
   1257 	iov[3].iov_base = wsc->m2->public_key;
   1258 	iov[3].iov_len = sizeof(wsc->m2->public_key);
   1259 	l_checksum_updatev(wsc->hmac_auth_key, iov, 4);
   1260 	l_checksum_get_digest(wsc->hmac_auth_key,
   1261 					m4.r_hash1, sizeof(m4.r_hash1));
   1262 
   1263 	iov[0].iov_base = wsc->local_snonce2;
   1264 	iov[0].iov_len = sizeof(wsc->local_snonce2);
   1265 	iov[1].iov_base = wsc->psk2;
   1266 	iov[1].iov_len = sizeof(wsc->psk2);
   1267 	l_checksum_updatev(wsc->hmac_auth_key, iov, 4);
   1268 	l_checksum_get_digest(wsc->hmac_auth_key,
   1269 					m4.r_hash2, sizeof(m4.r_hash2));
   1270 
   1271 	pdu = wsc_build_m4(&m4, encrypted, encrypted_len, &pdu_len);
   1272 	if (!pdu)
   1273 		return;
   1274 
   1275 	authenticator_put(wsc, m3_pdu, m3_len, pdu, pdu_len);
   1276 	eap_wsc_send_message(eap, pdu, pdu_len);
   1277 	wsc->state = STATE_EXPECT_M5;
   1278 }
   1279 
   1280 static void eap_wsc_r_handle_m3(struct eap_state *eap,
   1281 					const uint8_t *pdu, size_t len)
   1282 {
   1283 	struct eap_wsc_state *wsc = eap_get_data(eap);
   1284 	struct wsc_m3 m3;
   1285 
   1286 	if (wsc_parse_m3(pdu, len, &m3) != 0)
   1287 		goto send_nack;
   1288 
   1289 	if (memcmp(m3.registrar_nonce, wsc->m2->registrar_nonce,
   1290 						sizeof(m3.registrar_nonce)))
   1291 		goto send_nack;
   1292 
   1293 	if (!authenticator_check(wsc, pdu, len))
   1294 		return;
   1295 
   1296 	/* We can't verify these until we receive the E-S1/E-S2 in M5/M7 */
   1297 	memcpy(wsc->e_hash1, m3.e_hash1, sizeof(m3.e_hash1));
   1298 	memcpy(wsc->e_hash2, m3.e_hash2, sizeof(m3.e_hash2));
   1299 
   1300 	eap_wsc_r_send_m4(eap, pdu, len);
   1301 	return;
   1302 
   1303 send_nack:
   1304 	/* Spec unclear what to do here, see comments in eap_wsc_send_nack */
   1305 	eap_wsc_send_nack(eap, WSC_CONFIGURATION_ERROR_NO_ERROR);
   1306 }
   1307 
   1308 static void eap_wsc_r_send_m2(struct eap_state *eap,
   1309 				const uint8_t *m1_pdu, size_t m1_len)
   1310 {
   1311 	struct eap_wsc_state *wsc = eap_get_data(eap);
   1312 	uint8_t *pdu;
   1313 	size_t pdu_len;
   1314 
   1315 	wsc->m2->version2 = true;
   1316 	memcpy(wsc->m2->enrollee_nonce, wsc->m1->enrollee_nonce, 16);
   1317 	wsc->m2->association_state = WSC_ASSOCIATION_STATE_NOT_ASSOCIATED;
   1318 	wsc->m2->configuration_error = WSC_CONFIGURATION_ERROR_NO_ERROR;
   1319 
   1320 	pdu = wsc_build_m2(wsc->m2, &pdu_len);
   1321 	if (!pdu)
   1322 		return;
   1323 
   1324 	authenticator_put(wsc, m1_pdu, m1_len, pdu, pdu_len);
   1325 	eap_wsc_send_message(eap, pdu, pdu_len);
   1326 	wsc->state = STATE_EXPECT_M3;
   1327 }
   1328 
   1329 static void eap_wsc_r_handle_m1(struct eap_state *eap,
   1330 					const uint8_t *pdu, size_t len)
   1331 {
   1332 	struct eap_wsc_state *wsc = eap_get_data(eap);
   1333 	struct l_key *remote_public;
   1334 	uint8_t shared_secret[192] = { 0 };
   1335 	size_t shared_secret_len = sizeof(shared_secret);
   1336 	struct l_checksum *sha256;
   1337 	uint8_t dhkey[32];
   1338 	struct l_checksum *hmac_sha256;
   1339 	struct iovec iov[3];
   1340 	uint8_t kdk[32];
   1341 	struct wsc_session_key keys;
   1342 	bool r;
   1343 	uint8_t expected_enrollee_mac[6];
   1344 	uint8_t expected_uuid_e[16];
   1345 
   1346 	memcpy(expected_enrollee_mac, wsc->m1->addr, 6);
   1347 	memcpy(expected_uuid_e, wsc->m1->uuid_e, 16);
   1348 
   1349 	/* Spec unclear what to do here, see comments in eap_wsc_send_nack */
   1350 	if (wsc_parse_m1(pdu, len, wsc->m1) != 0) {
   1351 		eap_wsc_send_nack(eap, WSC_CONFIGURATION_ERROR_NO_ERROR);
   1352 		return;
   1353 	}
   1354 
   1355 	if (memcmp(expected_enrollee_mac, wsc->m1->addr, 6) ||
   1356 			memcmp(expected_uuid_e, wsc->m1->uuid_e, 16)) {
   1357 		eap_wsc_send_nack(eap,
   1358 			WSC_CONFIGURATION_ERROR_MULTIPLE_PBC_SESSIONS_DETECTED);
   1359 		eap_method_error(eap);
   1360 		return;
   1361 	}
   1362 
   1363 	if (wsc->m1->state != WSC_STATE_NOT_CONFIGURED ||
   1364 			!(wsc->m1->auth_type_flags &
   1365 				wsc->m2->auth_type_flags) ||
   1366 			!(wsc->m1->encryption_type_flags &
   1367 				wsc->m2->encryption_type_flags) ||
   1368 			!(wsc->m1->connection_type_flags &
   1369 				wsc->m2->connection_type_flags) ||
   1370 			!(wsc->m1->config_methods &
   1371 				wsc->m2->config_methods) ||
   1372 			!(wsc->m1->rf_bands & wsc->m2->rf_bands)) {
   1373 		eap_wsc_send_nack(eap, WSC_CONFIGURATION_ERROR_NO_ERROR);
   1374 		return;
   1375 	}
   1376 
   1377 	if (wsc->m1->configuration_error != WSC_CONFIGURATION_ERROR_NO_ERROR ||
   1378 			wsc->m1->device_password_id !=
   1379 			wsc->m2->device_password_id) {
   1380 		eap_wsc_send_nack(eap, WSC_CONFIGURATION_ERROR_NO_ERROR);
   1381 		return;
   1382 	}
   1383 
   1384 	/* m1->request_to_enroll not checked */
   1385 
   1386 	if (!l_key_validate_dh_payload(wsc->m1->public_key,
   1387 					sizeof(wsc->m1->public_key),
   1388 					crypto_dh5_prime,
   1389 					crypto_dh5_prime_size))
   1390 		return;
   1391 
   1392 	/*
   1393 	 * Done validating the M1, we can now derive the keys for most of
   1394 	 * the rest of the registration protocol.
   1395 	 */
   1396 
   1397 	remote_public = l_key_new(L_KEY_RAW, wsc->m1->public_key,
   1398 						sizeof(wsc->m1->public_key));
   1399 	if (!remote_public)
   1400 		return;
   1401 
   1402 	r = l_key_compute_dh_secret(remote_public, wsc->private, dh5_prime,
   1403 					shared_secret, &shared_secret_len);
   1404 	l_key_free(remote_public);
   1405 
   1406 	if (!r)
   1407 		return;
   1408 
   1409 	sha256 = l_checksum_new(L_CHECKSUM_SHA256);
   1410 	if (!sha256)
   1411 		return;
   1412 
   1413 	l_checksum_update(sha256, shared_secret, shared_secret_len);
   1414 	explicit_bzero(shared_secret, shared_secret_len);
   1415 	l_checksum_get_digest(sha256, dhkey, sizeof(dhkey));
   1416 	l_checksum_free(sha256);
   1417 
   1418 	hmac_sha256 = l_checksum_new_hmac(L_CHECKSUM_SHA256,
   1419 							dhkey, sizeof(dhkey));
   1420 	explicit_bzero(dhkey, sizeof(dhkey));
   1421 
   1422 	if (!hmac_sha256)
   1423 		return;
   1424 
   1425 	iov[0].iov_base = wsc->m1->enrollee_nonce;
   1426 	iov[0].iov_len = 16;
   1427 	iov[1].iov_base = wsc->m1->addr;
   1428 	iov[1].iov_len = 6;
   1429 	iov[2].iov_base = wsc->m2->registrar_nonce;
   1430 	iov[2].iov_len = 16;
   1431 
   1432 	l_checksum_updatev(hmac_sha256, iov, 3);
   1433 	l_checksum_get_digest(hmac_sha256, kdk, sizeof(kdk));
   1434 	l_checksum_free(hmac_sha256);
   1435 
   1436 	r = wsc_kdf(kdk, &keys, sizeof(keys));
   1437 	explicit_bzero(kdk, sizeof(kdk));
   1438 	if (!r)
   1439 		return;
   1440 
   1441 	wsc->hmac_auth_key = l_checksum_new_hmac(L_CHECKSUM_SHA256,
   1442 						keys.auth_key,
   1443 						sizeof(keys.auth_key));
   1444 
   1445 	/*
   1446 	 * AuthKey is uploaded into the kernel, once we upload KeyWrapKey,
   1447 	 * the keys variable is no longer useful.  Make sure to wipe it
   1448 	 */
   1449 	wsc->aes_cbc_128 = l_cipher_new(L_CIPHER_AES_CBC, keys.keywrap_key,
   1450 						sizeof(keys.keywrap_key));
   1451 	explicit_bzero(&keys, sizeof(keys));
   1452 
   1453 	eap_wsc_r_send_m2(eap, pdu, len);
   1454 }
   1455 
   1456 static void eap_wsc_handle_nack(struct eap_state *eap,
   1457 					const uint8_t *pdu, size_t len)
   1458 {
   1459 	struct eap_wsc_state *wsc = eap_get_data(eap);
   1460 	struct wsc_nack nack;
   1461 
   1462 	if (wsc_parse_wsc_nack(pdu, len, &nack) != 0)
   1463 		return;
   1464 
   1465 	if (wsc->state != STATE_EXPECT_M1 && memcmp(nack.enrollee_nonce,
   1466 						wsc->m1->enrollee_nonce,
   1467 						sizeof(nack.enrollee_nonce)))
   1468 		return;
   1469 
   1470 	if (!wsc->m2)
   1471 		return;
   1472 
   1473 	if (memcmp(nack.registrar_nonce, wsc->m2->registrar_nonce,
   1474 						sizeof(nack.registrar_nonce)))
   1475 		return;
   1476 
   1477 	/*
   1478 	 * The spec is completely unclear what the NACK error should be set
   1479 	 * to.  Our choice is to reflect back what the request error code is
   1480 	 * in the response.
   1481 	 */
   1482 	if (!wsc->registrar)
   1483 		eap_wsc_send_nack(eap, nack.configuration_error);
   1484 
   1485 	eap_method_error(eap);
   1486 }
   1487 
   1488 static void eap_wsc_handle_message(struct eap_state *eap,
   1489 					const uint8_t *pkt, size_t len)
   1490 {
   1491 	struct eap_wsc_state *wsc = eap_get_data(eap);
   1492 	uint8_t op;
   1493 	uint8_t flags;
   1494 	uint8_t *pdu;
   1495 	size_t pdu_len;
   1496 	size_t rx_header_offset = 0;
   1497 
   1498 	/*
   1499 	 * pkt[0:len] is an EAP-Request packet contents if !wsc->registrar
   1500 	 * and an EAP-Response otherwise.  Most of the parsing is going to
   1501 	 * be identical though.
   1502 	 *
   1503 	 * Since we have disjoint sets of enum state values between
   1504 	 * Enrollee and Registrar, most of the time we don't need to look
   1505 	 * at wsc->registrar.
   1506 	 */
   1507 
   1508 	if (len < 2)
   1509 		return;
   1510 
   1511 	op = pkt[0];
   1512 	flags = pkt[1];
   1513 
   1514 	pkt += 2;
   1515 	len -= 2;
   1516 
   1517 	switch (op) {
   1518 	case WSC_OP_START:
   1519 		if (len)
   1520 			return;
   1521 
   1522 		if (wsc->state != STATE_EXPECT_START)
   1523 			return;
   1524 
   1525 		pdu = wsc_build_m1(wsc->m1, &pdu_len);
   1526 		if (!pdu)
   1527 			return;
   1528 
   1529 		eap_wsc_send_message(eap, pdu, pdu_len);
   1530 		wsc->state = STATE_EXPECT_M2;
   1531 		return;
   1532 	case WSC_OP_NACK:
   1533 		if (!len)
   1534 			return;
   1535 
   1536 		eap_wsc_handle_nack(eap, pkt, len);
   1537 		return;
   1538 	case WSC_OP_ACK:
   1539 		/* Not used in the in-band Enrollee Registration scenario */
   1540 		return;
   1541 	case WSC_OP_DONE:
   1542 		if (wsc->state != STATE_EXPECT_DONE)
   1543 			return;
   1544 
   1545 		/*
   1546 		 * Notify higher layers that the registration protocol has
   1547 		 * finished successfully as the EAP result is going to
   1548 		 * always be the same.  May not be strictly needed as the
   1549 		 * higher layers get the real confirmation when the enrollee
   1550 		 * uses the credentials on their next connection.
   1551 		 */
   1552 		eap_method_event(eap, EAP_WSC_EVENT_CREDENTIAL_SENT, NULL);
   1553 		eap_method_error(eap);
   1554 		return;
   1555 	case WSC_OP_FRAG_ACK:
   1556 		if (wsc->tx_last_frag_len &&
   1557 				(wsc->tx_frag_offset + wsc->tx_last_frag_len) <
   1558 								wsc->sent_len) {
   1559 			wsc->tx_frag_offset += wsc->tx_last_frag_len;
   1560 			wsc->tx_last_frag_len = 0;
   1561 
   1562 			eap_wsc_send_fragment(eap);
   1563 		}
   1564 
   1565 		return;
   1566 	case WSC_OP_MSG:
   1567 		if (flags & WSC_FLAG_LF) {
   1568 			if (wsc->rx_pdu_buf ||
   1569 					!(flags & WSC_FLAG_MF) || len < 2)
   1570 				goto invalid_frag;
   1571 
   1572 			wsc->rx_pdu_buf_len = l_get_be16(pkt);
   1573 
   1574 			if (!wsc->rx_pdu_buf_len ||
   1575 					wsc->rx_pdu_buf_len >
   1576 							EAP_WSC_PDU_MAX_LEN) {
   1577 				l_warn("Fragmented pkt size is outside of "
   1578 					"allowed boundaries [1, %u]",
   1579 					EAP_WSC_PDU_MAX_LEN);
   1580 				return;
   1581 			}
   1582 
   1583 			if (wsc->rx_pdu_buf_len < len) {
   1584 				l_warn("Fragmented pkt size is smaller than "
   1585 					"the received packet");
   1586 				return;
   1587 			}
   1588 
   1589 			wsc->rx_pdu_buf = l_malloc(wsc->rx_pdu_buf_len);
   1590 			wsc->rx_pdu_buf_offset = 0;
   1591 
   1592 			rx_header_offset = 2;
   1593 		}
   1594 
   1595 		if (wsc->rx_pdu_buf) {
   1596 			pdu_len = len - rx_header_offset;
   1597 
   1598 			if (wsc->rx_pdu_buf_len <
   1599 					(wsc->rx_pdu_buf_offset + pdu_len)) {
   1600 				l_error("Request fragment pkt size mismatch");
   1601 				goto invalid_frag;
   1602 			}
   1603 
   1604 			memcpy(wsc->rx_pdu_buf + wsc->rx_pdu_buf_offset,
   1605 					pkt + rx_header_offset, pdu_len);
   1606 			wsc->rx_pdu_buf_offset += pdu_len;
   1607 		}
   1608 
   1609 		if (flags & WSC_FLAG_MF) {
   1610 			if (!wsc->rx_pdu_buf)
   1611 				goto invalid_frag;
   1612 
   1613 			eap_wsc_send_frag_ack(eap);
   1614 			return;
   1615 		} else if (wsc->rx_pdu_buf) {
   1616 			if (wsc->rx_pdu_buf_len != wsc->rx_pdu_buf_offset) {
   1617 				l_error("Message fragment pkt size mismatch");
   1618 				goto invalid_frag;
   1619 			}
   1620 
   1621 			pkt = wsc->rx_pdu_buf;
   1622 			len = wsc->rx_pdu_buf_len;
   1623 		}
   1624 
   1625 		break;
   1626 	}
   1627 
   1628 	if (!len)
   1629 		return;
   1630 
   1631 	switch (wsc->state) {
   1632 	/* Enrollee state machine */
   1633 	case STATE_EXPECT_START:
   1634 		return;
   1635 	case STATE_EXPECT_M2:
   1636 		eap_wsc_handle_m2(eap, pkt, len);
   1637 		break;
   1638 	case STATE_EXPECT_M4:
   1639 		eap_wsc_handle_m4(eap, pkt, len);
   1640 		break;
   1641 	case STATE_EXPECT_M6:
   1642 		eap_wsc_handle_m6(eap, pkt, len);
   1643 		break;
   1644 	case STATE_EXPECT_M8:
   1645 		eap_wsc_handle_m8(eap, pkt, len);
   1646 		break;
   1647 	case STATE_FINISHED:
   1648 		eap_wsc_send_nack(eap, WSC_CONFIGURATION_ERROR_NO_ERROR);
   1649 		return;
   1650 
   1651 	/* Registrar state machine */
   1652 	case STATE_EXPECT_M1:
   1653 		eap_wsc_r_handle_m1(eap, pkt, len);
   1654 		break;
   1655 	case STATE_EXPECT_M3:
   1656 		eap_wsc_r_handle_m3(eap, pkt, len);
   1657 		break;
   1658 	case STATE_EXPECT_M5:
   1659 		eap_wsc_r_handle_m5(eap, pkt, len);
   1660 		break;
   1661 	case STATE_EXPECT_M7:
   1662 		eap_wsc_r_handle_m7(eap, pkt, len);
   1663 		break;
   1664 	case STATE_EXPECT_IDENTITY:
   1665 	case STATE_EXPECT_DONE:
   1666 		eap_wsc_send_nack(eap, WSC_CONFIGURATION_ERROR_NO_ERROR);
   1667 		return;
   1668 	}
   1669 
   1670 	if (wsc->rx_pdu_buf) {
   1671 		l_free(wsc->rx_pdu_buf);
   1672 		wsc->rx_pdu_buf = NULL;
   1673 		wsc->rx_pdu_buf_len = 0;
   1674 		wsc->rx_pdu_buf_offset = 0;
   1675 	}
   1676 
   1677 	return;
   1678 
   1679 invalid_frag:
   1680 	eap_method_error(eap);
   1681 }
   1682 
   1683 static void eap_wsc_handle_retransmit(struct eap_state *eap,
   1684 						const uint8_t *pkt, size_t len)
   1685 {
   1686 	struct eap_wsc_state *wsc = eap_get_data(eap);
   1687 	uint8_t op;
   1688 	uint8_t flags;
   1689 
   1690 	if (len < 2)
   1691 		return;
   1692 
   1693 	op = pkt[0];
   1694 
   1695 	switch (op) {
   1696 	case WSC_OP_NACK:
   1697 		eap_wsc_handle_nack(eap, pkt + 2, len - 2);
   1698 		return;
   1699 	case WSC_OP_ACK:
   1700 	case WSC_OP_DONE:
   1701 		/* Should never receive these as Enrollee */
   1702 		return;
   1703 	case WSC_OP_MSG:
   1704 		flags = pkt[1];
   1705 
   1706 		if (flags & WSC_FLAG_MF) {
   1707 			if (!wsc->rx_pdu_buf) {
   1708 				eap_method_error(eap);
   1709 				return;
   1710 			}
   1711 
   1712 			eap_wsc_send_frag_ack(eap);
   1713 			return;
   1714 		}
   1715 	}
   1716 
   1717 	if (!wsc->sent_pdu || !wsc->sent_len) {
   1718 		eap_method_error(eap);
   1719 		return;
   1720 	}
   1721 
   1722 	if (wsc->sent_len + EAP_WSC_HEADER_LEN > eap_get_mtu(eap)) {
   1723 		eap_wsc_send_fragment(eap);
   1724 	} else {
   1725 		size_t msg_len = wsc->sent_len + EAP_WSC_HEADER_LEN;
   1726 		uint8_t buf[msg_len];
   1727 
   1728 		buf[12] = WSC_OP_MSG;
   1729 		buf[13] = 0;
   1730 		memcpy(buf + EAP_WSC_HEADER_LEN, wsc->sent_pdu, wsc->sent_len);
   1731 
   1732 		eap_method_respond(eap, buf, msg_len);
   1733 	}
   1734 }
   1735 
   1736 static bool load_hexencoded(struct l_settings *settings, const char *key,
   1737 						uint8_t *to, size_t len)
   1738 {
   1739 	uint8_t *v;
   1740 	size_t v_len;
   1741 
   1742 	v = l_settings_get_bytes(settings, "WSC", key, &v_len);
   1743 	if (!v)
   1744 		return false;
   1745 
   1746 	if (v_len != len) {
   1747 		explicit_bzero(v, v_len);
   1748 		l_free(v);
   1749 		return false;
   1750 	}
   1751 
   1752 	memcpy(to, v, len);
   1753 	explicit_bzero(v, v_len);
   1754 	l_free(v);
   1755 
   1756 	return true;
   1757 }
   1758 
   1759 static bool load_primary_device_type(struct l_settings *settings,
   1760 					struct wsc_primary_device_type *pdt)
   1761 {
   1762 	const char *v;
   1763 
   1764 	v = l_settings_get_value(settings, "WSC", "PrimaryDeviceType");
   1765 	if (!v)
   1766 		return false;
   1767 
   1768 	return wsc_device_type_from_setting_str(v, pdt);
   1769 }
   1770 
   1771 static bool load_constrained_string(struct l_settings *settings,
   1772 						const char *key,
   1773 						char *out, size_t max)
   1774 {
   1775 	char *v;
   1776 	size_t tocopy;
   1777 
   1778 	v = l_settings_get_string(settings, "WSC", key);
   1779 	if (!v)
   1780 		return false;
   1781 
   1782 	tocopy = strlen(v);
   1783 	if (tocopy >= max)
   1784 		tocopy = max - 1;
   1785 
   1786 	memcpy(out, v, tocopy);
   1787 	out[max - 1] = '\0';
   1788 
   1789 	l_free(v);
   1790 
   1791 	return true;
   1792 }
   1793 
   1794 static bool load_device_data(struct l_settings *settings,
   1795 				char *manufacturer,
   1796 				char *model_name,
   1797 				char *model_number,
   1798 				char *serial_number,
   1799 				struct wsc_primary_device_type *dev_type,
   1800 				char *device_name,
   1801 				uint8_t *rf_bands,
   1802 				uint32_t *os_version)
   1803 {
   1804 	struct wsc_m1 *m1;
   1805 	unsigned int u32;
   1806 
   1807 	if (!load_constrained_string(settings, "Manufacturer",
   1808 				manufacturer, sizeof(m1->manufacturer)))
   1809 		strcpy(manufacturer, " ");
   1810 
   1811 	if (!load_constrained_string(settings, "ModelName",
   1812 				model_name, sizeof(m1->model_name)))
   1813 		strcpy(model_name, " ");
   1814 
   1815 	if (!load_constrained_string(settings, "ModelNumber",
   1816 				model_number, sizeof(m1->model_number)))
   1817 		strcpy(model_number, " ");
   1818 
   1819 	if (!load_constrained_string(settings, "SerialNumber",
   1820 				serial_number, sizeof(m1->serial_number)))
   1821 		strcpy(serial_number, " ");
   1822 
   1823 	if (!load_primary_device_type(settings, dev_type)) {
   1824 		/* Make ourselves a WFA standard PC by default */
   1825 		dev_type->category = 1;
   1826 		memcpy(dev_type->oui, wsc_wfa_oui, 3);
   1827 		dev_type->oui_type = 0x04;
   1828 		dev_type->subcategory = 1;
   1829 	}
   1830 
   1831 	if (!load_constrained_string(settings, "DeviceName",
   1832 				device_name, sizeof(m1->device_name)))
   1833 		strcpy(device_name, " ");
   1834 
   1835 	if (!l_settings_get_uint(settings, "WSC", "RFBand", &u32))
   1836 		return false;
   1837 
   1838 	switch (u32) {
   1839 	case WSC_RF_BAND_2_4_GHZ:
   1840 	case WSC_RF_BAND_5_0_GHZ:
   1841 	case WSC_RF_BAND_60_GHZ:
   1842 		*rf_bands = u32;
   1843 		break;
   1844 	default:
   1845 		return false;
   1846 	}
   1847 
   1848 	if (!l_settings_get_uint(settings, "WSC", "OSVersion", &u32))
   1849 		u32 = 0;
   1850 
   1851 	*os_version = u32 & 0x7fffffff;
   1852 
   1853 	return true;
   1854 }
   1855 
   1856 static struct eap_wsc_state *eap_wsc_new_common(struct l_settings *settings,
   1857 						bool registrar)
   1858 {
   1859 	struct eap_wsc_state *wsc;
   1860 	const char *v;
   1861 	uint8_t private_key[192];
   1862 	size_t len;
   1863 	unsigned int u32;
   1864 
   1865 	wsc = l_new(struct eap_wsc_state, 1);
   1866 	wsc->registrar = registrar;
   1867 
   1868 	wsc->m1 = l_new(struct wsc_m1, 1);
   1869 
   1870 	if (registrar)
   1871 		wsc->m2 = l_new(struct wsc_m2, 1);
   1872 
   1873 	v = l_settings_get_value(settings, "WSC", "EnrolleeMAC");
   1874 	if (!v)
   1875 		goto err;
   1876 
   1877 	if (!util_string_to_address(v, wsc->m1->addr))
   1878 		goto err;
   1879 
   1880 	if (!l_settings_get_uint(settings, "WSC", "ConfigurationMethods", &u32))
   1881 		u32 = WSC_CONFIGURATION_METHOD_VIRTUAL_DISPLAY_PIN;
   1882 
   1883 	if (!registrar)
   1884 		wsc->m1->config_methods = u32;
   1885 	else
   1886 		wsc->m2->config_methods = u32;
   1887 
   1888 	if (!l_settings_get_uint(settings, "WSC", "DevicePasswordId", &u32))
   1889 		u32 = WSC_DEVICE_PASSWORD_ID_PUSH_BUTTON;
   1890 
   1891 	if (!registrar)
   1892 		wsc->m1->device_password_id = u32;
   1893 	else
   1894 		wsc->m2->device_password_id = u32;
   1895 
   1896 	wsc->device_password = l_settings_get_string(settings, "WSC",
   1897 							"DevicePassword");
   1898 	if (wsc->device_password) {
   1899 		int i;
   1900 
   1901 		for (i = 0; wsc->device_password[i]; i++) {
   1902 			if (!l_ascii_isxdigit(wsc->device_password[i]))
   1903 				goto err;
   1904 		}
   1905 
   1906 		/*
   1907 		 * WSC 2.0.5: Section 7.4:
   1908 		 * If an out-of-band mechanism is used as the configuration
   1909 		 * method, the device password is expressed in hexadecimal
   1910 		 * using ASCII character (two characters per octet, uppercase
   1911 		 * letters only).
   1912 		 */
   1913 		for (i = 0; wsc->device_password[i]; i++) {
   1914 			if (wsc->device_password[i] >= 'a' &&
   1915 					wsc->device_password[i] <= 'f')
   1916 				wsc->device_password[i] =
   1917 					'A' + wsc->device_password[i] - 'a';
   1918 		}
   1919 	} else
   1920 		wsc->device_password = l_strdup("00000000");
   1921 
   1922 	/*
   1923 	 * The settings we read below probably shouldn't be used except to
   1924 	 * eliminate randomness in debugging/tests.
   1925 	 */
   1926 	if (load_hexencoded(settings, "PrivateKey", private_key, 192)) {
   1927 		if (!l_key_validate_dh_payload(private_key, 192,
   1928 						crypto_dh5_prime,
   1929 						crypto_dh5_prime_size)) {
   1930 			explicit_bzero(private_key, 192);
   1931 			goto err;
   1932 		}
   1933 
   1934 		wsc->private = l_key_new(L_KEY_RAW, private_key, 192);
   1935 		explicit_bzero(private_key, 192);
   1936 	} else
   1937 		wsc->private = l_key_generate_dh_private(crypto_dh5_prime,
   1938 							crypto_dh5_prime_size);
   1939 
   1940 	if (!wsc->private)
   1941 		goto err;
   1942 
   1943 	len = sizeof(wsc->m1->public_key);
   1944 	if (!l_key_compute_dh_public(dh5_generator, wsc->private, dh5_prime,
   1945 					registrar ? wsc->m2->public_key :
   1946 					wsc->m1->public_key, &len))
   1947 		goto err;
   1948 
   1949 	if (len != sizeof(wsc->m1->public_key))
   1950 		goto err;
   1951 
   1952 	if (!load_hexencoded(settings, registrar ? "R-SNonce1" : "E-SNonce1",
   1953 							wsc->local_snonce1, 16))
   1954 		l_getrandom(wsc->local_snonce1, 16);
   1955 
   1956 	if (!load_hexencoded(settings, registrar ? "R-SNonce2" : "E-SNonce2",
   1957 							wsc->local_snonce2, 16))
   1958 		l_getrandom(wsc->local_snonce2, 16);
   1959 
   1960 	if (!load_hexencoded(settings, "IV1", wsc->iv1, 16))
   1961 		l_getrandom(wsc->iv1, 16);
   1962 
   1963 	if (!load_hexencoded(settings, "IV2", wsc->iv2, 16))
   1964 		l_getrandom(wsc->iv2, 16);
   1965 
   1966 	return wsc;
   1967 
   1968 err:
   1969 	eap_wsc_state_free(wsc);
   1970 	return NULL;
   1971 }
   1972 
   1973 static bool eap_wsc_load_settings(struct eap_state *eap,
   1974 					struct l_settings *settings,
   1975 					const char *prefix)
   1976 {
   1977 	struct eap_wsc_state *wsc = eap_wsc_new_common(settings, false);
   1978 
   1979 	if (!wsc)
   1980 		return false;
   1981 
   1982 	wsc->m1->version2 = true;
   1983 	wsc->m1->state = WSC_STATE_NOT_CONFIGURED;
   1984 	wsc->m1->association_state = WSC_ASSOCIATION_STATE_NOT_ASSOCIATED;
   1985 	wsc->m1->configuration_error = WSC_CONFIGURATION_ERROR_NO_ERROR;
   1986 
   1987 	wsc->m1->auth_type_flags = WSC_AUTHENTICATION_TYPE_WPA2_PERSONAL |
   1988 					WSC_AUTHENTICATION_TYPE_WPA_PERSONAL |
   1989 					WSC_AUTHENTICATION_TYPE_OPEN;
   1990 	wsc->m1->encryption_type_flags = WSC_ENCRYPTION_TYPE_NONE |
   1991 						WSC_ENCRYPTION_TYPE_AES_TKIP;
   1992 	wsc->m1->connection_type_flags = WSC_CONNECTION_TYPE_ESS;
   1993 
   1994 	if (!wsc_uuid_from_addr(wsc->m1->addr, wsc->m1->uuid_e))
   1995 		goto err;
   1996 
   1997 	if (!load_device_data(settings,
   1998 				wsc->m1->manufacturer,
   1999 				wsc->m1->model_name,
   2000 				wsc->m1->model_number,
   2001 				wsc->m1->serial_number,
   2002 				&wsc->m1->primary_device_type,
   2003 				wsc->m1->device_name,
   2004 				&wsc->m1->rf_bands,
   2005 				&wsc->m1->os_version))
   2006 		goto err;
   2007 
   2008 	/* Debug settings */
   2009 	if (!load_hexencoded(settings, "EnrolleeNonce",
   2010 						wsc->m1->enrollee_nonce, 16))
   2011 		l_getrandom(wsc->m1->enrollee_nonce, 16);
   2012 
   2013 	wsc->state = STATE_EXPECT_START;
   2014 	eap_set_data(eap, wsc);
   2015 
   2016 	return true;
   2017 
   2018 err:
   2019 	eap_wsc_state_free(wsc);
   2020 	return false;
   2021 }
   2022 
   2023 static struct eap_method eap_wsc = {
   2024 	.vendor_id = { 0x00, 0x37, 0x2a },
   2025 	.vendor_type = 0x00000001,
   2026 	.request_type = EAP_TYPE_EXPANDED,
   2027 	.exports_msk = true,
   2028 	.name = "WSC",
   2029 	.free = eap_wsc_free,
   2030 	.handle_request = eap_wsc_handle_message,
   2031 	.handle_retransmit = eap_wsc_handle_retransmit,
   2032 	.load_settings = eap_wsc_load_settings,
   2033 };
   2034 
   2035 static bool eap_wsc_r_load_settings(struct eap_state *eap,
   2036 					struct l_settings *settings,
   2037 					const char *prefix)
   2038 {
   2039 	/*
   2040 	 * On the Registrar we store some information in wsc->m1 for the
   2041 	 * actual M1's validation, and we fill in the elements of M2
   2042 	 * that don't depend on the data received in M1.
   2043 	 */
   2044 	struct eap_wsc_state *wsc = eap_wsc_new_common(settings, true);
   2045 	char *str;
   2046 
   2047 	if (!wsc)
   2048 		return false;
   2049 
   2050 	if (!load_hexencoded(settings, "UUID-E", wsc->m1->uuid_e, 16))
   2051 		goto err;
   2052 
   2053 	if (!load_hexencoded(settings, "UUID-R", wsc->m2->uuid_r, 16))
   2054 		goto err;
   2055 
   2056 	if (!load_device_data(settings,
   2057 				wsc->m2->manufacturer,
   2058 				wsc->m2->model_name,
   2059 				wsc->m2->model_number,
   2060 				wsc->m2->serial_number,
   2061 				&wsc->m2->primary_device_type,
   2062 				wsc->m2->device_name,
   2063 				&wsc->m2->rf_bands,
   2064 				&wsc->m2->os_version))
   2065 		goto err;
   2066 
   2067 	wsc->m2->auth_type_flags = 0;
   2068 
   2069 	str = l_settings_get_string(settings, "WSC", "WPA2-SSID");
   2070 	if (str) {
   2071 		if (strlen(str) > SSID_MAX_SIZE)
   2072 			goto bad_string;
   2073 
   2074 		wsc->m2->auth_type_flags |=
   2075 			WSC_AUTHENTICATION_TYPE_WPA2_PERSONAL;
   2076 		wsc->m2->encryption_type_flags |= WSC_ENCRYPTION_TYPE_AES_TKIP;
   2077 		wsc->wpa2_cred.auth_type =
   2078 			WSC_AUTHENTICATION_TYPE_WPA2_PERSONAL;
   2079 		wsc->wpa2_cred.encryption_type = WSC_ENCRYPTION_TYPE_AES_TKIP;
   2080 
   2081 		wsc->wpa2_cred.ssid_len = strlen(str);
   2082 		memcpy(wsc->wpa2_cred.ssid, str, wsc->wpa2_cred.ssid_len);
   2083 		l_free(str);
   2084 
   2085 		str = l_settings_get_string(settings, "WSC", "WPA2-Passphrase");
   2086 		if (str) {
   2087 			size_t len = strlen(str);
   2088 
   2089 			if (len < 8 || len > 63) {
   2090 				explicit_bzero(str, len);
   2091 				goto bad_string;
   2092 			}
   2093 
   2094 			memcpy(wsc->wpa2_cred.network_key, str, len);
   2095 			wsc->wpa2_cred.network_key_len = len;
   2096 			explicit_bzero(str, len);
   2097 			l_free(str);
   2098 		} else {
   2099 			uint8_t buf[32];
   2100 
   2101 			if (!load_hexencoded(settings, "WPA2-PSK", buf, 32))
   2102 				goto err;
   2103 
   2104 			str = l_util_hexstring(buf, 32);
   2105 			explicit_bzero(buf, 32);
   2106 			memcpy(wsc->wpa2_cred.network_key, str, 64);
   2107 			explicit_bzero(str, 64);
   2108 			free(str);
   2109 			wsc->wpa2_cred.network_key_len = 64;
   2110 		}
   2111 
   2112 		memcpy(wsc->wpa2_cred.addr, wsc->m1->addr, 6);
   2113 	}
   2114 
   2115 	str = l_settings_get_string(settings, "WSC", "Open-SSID");
   2116 	if (str) {
   2117 		if (strlen(str) > SSID_MAX_SIZE)
   2118 			goto bad_string;
   2119 
   2120 		wsc->m2->auth_type_flags |= WSC_AUTHENTICATION_TYPE_OPEN;
   2121 		wsc->m2->encryption_type_flags |= WSC_ENCRYPTION_TYPE_NONE;
   2122 		wsc->open_cred.auth_type = WSC_AUTHENTICATION_TYPE_OPEN;
   2123 		wsc->open_cred.encryption_type = WSC_ENCRYPTION_TYPE_NONE;
   2124 
   2125 		wsc->open_cred.ssid_len = strlen(str);
   2126 		memcpy(wsc->open_cred.ssid, str, wsc->open_cred.ssid_len);
   2127 		l_free(str);
   2128 
   2129 		wsc->open_cred.network_key_len = 0;
   2130 
   2131 		memcpy(wsc->open_cred.addr, wsc->m1->addr, 6);
   2132 	}
   2133 
   2134 	if (!wsc->m2->auth_type_flags)
   2135 		goto err;
   2136 
   2137 	wsc->m2->connection_type_flags = WSC_CONNECTION_TYPE_ESS;
   2138 
   2139 	/* Debug settings */
   2140 	if (!load_hexencoded(settings, "RegistrarNonce",
   2141 						wsc->m2->registrar_nonce, 16))
   2142 		l_getrandom(wsc->m2->registrar_nonce, 16);
   2143 
   2144 	if (!load_hexencoded(settings, "IV3", wsc->iv3, 16))
   2145 		l_getrandom(wsc->iv3, 16);
   2146 
   2147 	wsc->state = STATE_EXPECT_IDENTITY;
   2148 	eap_set_data(eap, wsc);
   2149 
   2150 	return true;
   2151 
   2152 bad_string:
   2153 	l_free(str);
   2154 err:
   2155 	eap_wsc_state_free(wsc);
   2156 	return false;
   2157 }
   2158 
   2159 static bool eap_wsc_r_validate_identity(struct eap_state *eap,
   2160 					const char *identity)
   2161 {
   2162 	struct eap_wsc_state *wsc = eap_get_data(eap);
   2163 
   2164 	if (strcmp(identity, "WFA-SimpleConfig-Enrollee-1-0"))
   2165 		return false;
   2166 
   2167 	/* Identity Request/Response done, directly send the WSC_Start */
   2168 	eap_wsc_r_send_start(eap);
   2169 	wsc->state = STATE_EXPECT_M1;
   2170 	return true;
   2171 }
   2172 
   2173 static struct eap_method eap_wsc_r = {
   2174 	.name = "WSC-R",
   2175 	.request_type = EAP_TYPE_EXPANDED,
   2176 	.vendor_id = { 0x00, 0x37, 0x2a },
   2177 	.vendor_type = 0x00000001,
   2178 	.exports_msk = true,
   2179 	.free = eap_wsc_free,
   2180 	.load_settings = eap_wsc_r_load_settings,
   2181 	.validate_identity = eap_wsc_r_validate_identity,
   2182 	.handle_response = eap_wsc_handle_message,
   2183 };
   2184 
   2185 static int eap_wsc_init(void)
   2186 {
   2187 	int r = -ENOTSUP;
   2188 
   2189 	l_debug("");
   2190 
   2191 	dh5_generator = l_key_new(L_KEY_RAW, crypto_dh5_generator,
   2192 						crypto_dh5_generator_size);
   2193 	if (!dh5_generator)
   2194 		goto fail_generator;
   2195 
   2196 	dh5_prime = l_key_new(L_KEY_RAW, crypto_dh5_prime,
   2197 						crypto_dh5_prime_size);
   2198 	if (!dh5_prime)
   2199 		goto fail_prime;
   2200 
   2201 	r = eap_register_method(&eap_wsc);
   2202 	if (r)
   2203 		goto fail_register;
   2204 
   2205 	r = eap_register_method(&eap_wsc_r);
   2206 	if (!r)
   2207 		return 0;
   2208 
   2209 	eap_unregister_method(&eap_wsc);
   2210 
   2211 fail_register:
   2212 	l_key_free(dh5_prime);
   2213 	dh5_prime = NULL;
   2214 fail_prime:
   2215 	l_key_free(dh5_generator);
   2216 	dh5_generator = NULL;
   2217 fail_generator:
   2218 	return r;
   2219 }
   2220 
   2221 static void eap_wsc_exit(void)
   2222 {
   2223 	l_debug("");
   2224 
   2225 	eap_unregister_method(&eap_wsc);
   2226 	eap_unregister_method(&eap_wsc_r);
   2227 
   2228 	l_key_free(dh5_prime);
   2229 	l_key_free(dh5_generator);
   2230 }
   2231 
   2232 EAP_METHOD_BUILTIN(eap_wsc, eap_wsc_init, eap_wsc_exit)