xref: /dflybsd-src/contrib/wpa_supplicant/src/wps/wps_common.c (revision 3a84a4273475ed07d0ab1c2dfeffdfedef35d9cd)
16d49e1aeSJan Lentfer /*
26d49e1aeSJan Lentfer  * Wi-Fi Protected Setup - common functionality
33ff40c12SJohn Marino  * Copyright (c) 2008-2012, Jouni Malinen <j@w1.fi>
46d49e1aeSJan Lentfer  *
53ff40c12SJohn Marino  * This software may be distributed under the terms of the BSD license.
63ff40c12SJohn Marino  * See README for more details.
76d49e1aeSJan Lentfer  */
86d49e1aeSJan Lentfer 
96d49e1aeSJan Lentfer #include "includes.h"
106d49e1aeSJan Lentfer 
116d49e1aeSJan Lentfer #include "common.h"
123ff40c12SJohn Marino #include "common/defs.h"
133ff40c12SJohn Marino #include "common/ieee802_11_common.h"
143ff40c12SJohn Marino #include "crypto/aes_wrap.h"
153ff40c12SJohn Marino #include "crypto/crypto.h"
163ff40c12SJohn Marino #include "crypto/dh_group5.h"
173ff40c12SJohn Marino #include "crypto/sha1.h"
183ff40c12SJohn Marino #include "crypto/sha256.h"
193ff40c12SJohn Marino #include "crypto/random.h"
206d49e1aeSJan Lentfer #include "wps_i.h"
216d49e1aeSJan Lentfer #include "wps_dev_attr.h"
226d49e1aeSJan Lentfer 
236d49e1aeSJan Lentfer 
wps_kdf(const u8 * key,const u8 * label_prefix,size_t label_prefix_len,const char * label,u8 * res,size_t res_len)246d49e1aeSJan Lentfer void wps_kdf(const u8 *key, const u8 *label_prefix, size_t label_prefix_len,
256d49e1aeSJan Lentfer 	     const char *label, u8 *res, size_t res_len)
266d49e1aeSJan Lentfer {
276d49e1aeSJan Lentfer 	u8 i_buf[4], key_bits[4];
286d49e1aeSJan Lentfer 	const u8 *addr[4];
296d49e1aeSJan Lentfer 	size_t len[4];
306d49e1aeSJan Lentfer 	int i, iter;
316d49e1aeSJan Lentfer 	u8 hash[SHA256_MAC_LEN], *opos;
326d49e1aeSJan Lentfer 	size_t left;
336d49e1aeSJan Lentfer 
346d49e1aeSJan Lentfer 	WPA_PUT_BE32(key_bits, res_len * 8);
356d49e1aeSJan Lentfer 
366d49e1aeSJan Lentfer 	addr[0] = i_buf;
376d49e1aeSJan Lentfer 	len[0] = sizeof(i_buf);
386d49e1aeSJan Lentfer 	addr[1] = label_prefix;
396d49e1aeSJan Lentfer 	len[1] = label_prefix_len;
406d49e1aeSJan Lentfer 	addr[2] = (const u8 *) label;
416d49e1aeSJan Lentfer 	len[2] = os_strlen(label);
426d49e1aeSJan Lentfer 	addr[3] = key_bits;
436d49e1aeSJan Lentfer 	len[3] = sizeof(key_bits);
446d49e1aeSJan Lentfer 
456d49e1aeSJan Lentfer 	iter = (res_len + SHA256_MAC_LEN - 1) / SHA256_MAC_LEN;
466d49e1aeSJan Lentfer 	opos = res;
476d49e1aeSJan Lentfer 	left = res_len;
486d49e1aeSJan Lentfer 
496d49e1aeSJan Lentfer 	for (i = 1; i <= iter; i++) {
506d49e1aeSJan Lentfer 		WPA_PUT_BE32(i_buf, i);
516d49e1aeSJan Lentfer 		hmac_sha256_vector(key, SHA256_MAC_LEN, 4, addr, len, hash);
526d49e1aeSJan Lentfer 		if (i < iter) {
536d49e1aeSJan Lentfer 			os_memcpy(opos, hash, SHA256_MAC_LEN);
546d49e1aeSJan Lentfer 			opos += SHA256_MAC_LEN;
556d49e1aeSJan Lentfer 			left -= SHA256_MAC_LEN;
566d49e1aeSJan Lentfer 		} else
576d49e1aeSJan Lentfer 			os_memcpy(opos, hash, left);
586d49e1aeSJan Lentfer 	}
596d49e1aeSJan Lentfer }
606d49e1aeSJan Lentfer 
616d49e1aeSJan Lentfer 
wps_derive_keys(struct wps_data * wps)626d49e1aeSJan Lentfer int wps_derive_keys(struct wps_data *wps)
636d49e1aeSJan Lentfer {
646d49e1aeSJan Lentfer 	struct wpabuf *pubkey, *dh_shared;
656d49e1aeSJan Lentfer 	u8 dhkey[SHA256_MAC_LEN], kdk[SHA256_MAC_LEN];
666d49e1aeSJan Lentfer 	const u8 *addr[3];
676d49e1aeSJan Lentfer 	size_t len[3];
686d49e1aeSJan Lentfer 	u8 keys[WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN + WPS_EMSK_LEN];
696d49e1aeSJan Lentfer 
706d49e1aeSJan Lentfer 	if (wps->dh_privkey == NULL) {
716d49e1aeSJan Lentfer 		wpa_printf(MSG_DEBUG, "WPS: Own DH private key not available");
726d49e1aeSJan Lentfer 		return -1;
736d49e1aeSJan Lentfer 	}
746d49e1aeSJan Lentfer 
756d49e1aeSJan Lentfer 	pubkey = wps->registrar ? wps->dh_pubkey_e : wps->dh_pubkey_r;
766d49e1aeSJan Lentfer 	if (pubkey == NULL) {
776d49e1aeSJan Lentfer 		wpa_printf(MSG_DEBUG, "WPS: Peer DH public key not available");
786d49e1aeSJan Lentfer 		return -1;
796d49e1aeSJan Lentfer 	}
806d49e1aeSJan Lentfer 
813ff40c12SJohn Marino 	wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH Private Key", wps->dh_privkey);
823ff40c12SJohn Marino 	wpa_hexdump_buf(MSG_DEBUG, "WPS: DH peer Public Key", pubkey);
833ff40c12SJohn Marino 	dh_shared = dh5_derive_shared(wps->dh_ctx, pubkey, wps->dh_privkey);
843ff40c12SJohn Marino 	dh5_free(wps->dh_ctx);
853ff40c12SJohn Marino 	wps->dh_ctx = NULL;
866d49e1aeSJan Lentfer 	dh_shared = wpabuf_zeropad(dh_shared, 192);
876d49e1aeSJan Lentfer 	if (dh_shared == NULL) {
886d49e1aeSJan Lentfer 		wpa_printf(MSG_DEBUG, "WPS: Failed to derive DH shared key");
896d49e1aeSJan Lentfer 		return -1;
906d49e1aeSJan Lentfer 	}
916d49e1aeSJan Lentfer 
926d49e1aeSJan Lentfer 	/* Own DH private key is not needed anymore */
93*a1157835SDaniel Fojt 	wpabuf_clear_free(wps->dh_privkey);
946d49e1aeSJan Lentfer 	wps->dh_privkey = NULL;
956d49e1aeSJan Lentfer 
966d49e1aeSJan Lentfer 	wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH shared key", dh_shared);
976d49e1aeSJan Lentfer 
986d49e1aeSJan Lentfer 	/* DHKey = SHA-256(g^AB mod p) */
996d49e1aeSJan Lentfer 	addr[0] = wpabuf_head(dh_shared);
1006d49e1aeSJan Lentfer 	len[0] = wpabuf_len(dh_shared);
1016d49e1aeSJan Lentfer 	sha256_vector(1, addr, len, dhkey);
1026d49e1aeSJan Lentfer 	wpa_hexdump_key(MSG_DEBUG, "WPS: DHKey", dhkey, sizeof(dhkey));
103*a1157835SDaniel Fojt 	wpabuf_clear_free(dh_shared);
1046d49e1aeSJan Lentfer 
1056d49e1aeSJan Lentfer 	/* KDK = HMAC-SHA-256_DHKey(N1 || EnrolleeMAC || N2) */
1066d49e1aeSJan Lentfer 	addr[0] = wps->nonce_e;
1076d49e1aeSJan Lentfer 	len[0] = WPS_NONCE_LEN;
1086d49e1aeSJan Lentfer 	addr[1] = wps->mac_addr_e;
1096d49e1aeSJan Lentfer 	len[1] = ETH_ALEN;
1106d49e1aeSJan Lentfer 	addr[2] = wps->nonce_r;
1116d49e1aeSJan Lentfer 	len[2] = WPS_NONCE_LEN;
1126d49e1aeSJan Lentfer 	hmac_sha256_vector(dhkey, sizeof(dhkey), 3, addr, len, kdk);
1136d49e1aeSJan Lentfer 	wpa_hexdump_key(MSG_DEBUG, "WPS: KDK", kdk, sizeof(kdk));
1146d49e1aeSJan Lentfer 
1156d49e1aeSJan Lentfer 	wps_kdf(kdk, NULL, 0, "Wi-Fi Easy and Secure Key Derivation",
1166d49e1aeSJan Lentfer 		keys, sizeof(keys));
1176d49e1aeSJan Lentfer 	os_memcpy(wps->authkey, keys, WPS_AUTHKEY_LEN);
1186d49e1aeSJan Lentfer 	os_memcpy(wps->keywrapkey, keys + WPS_AUTHKEY_LEN, WPS_KEYWRAPKEY_LEN);
1196d49e1aeSJan Lentfer 	os_memcpy(wps->emsk, keys + WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN,
1206d49e1aeSJan Lentfer 		  WPS_EMSK_LEN);
1216d49e1aeSJan Lentfer 
1226d49e1aeSJan Lentfer 	wpa_hexdump_key(MSG_DEBUG, "WPS: AuthKey",
1236d49e1aeSJan Lentfer 			wps->authkey, WPS_AUTHKEY_LEN);
1246d49e1aeSJan Lentfer 	wpa_hexdump_key(MSG_DEBUG, "WPS: KeyWrapKey",
1256d49e1aeSJan Lentfer 			wps->keywrapkey, WPS_KEYWRAPKEY_LEN);
1266d49e1aeSJan Lentfer 	wpa_hexdump_key(MSG_DEBUG, "WPS: EMSK", wps->emsk, WPS_EMSK_LEN);
1276d49e1aeSJan Lentfer 
1286d49e1aeSJan Lentfer 	return 0;
1296d49e1aeSJan Lentfer }
1306d49e1aeSJan Lentfer 
1316d49e1aeSJan Lentfer 
wps_derive_psk(struct wps_data * wps,const u8 * dev_passwd,size_t dev_passwd_len)132*a1157835SDaniel Fojt int wps_derive_psk(struct wps_data *wps, const u8 *dev_passwd,
1336d49e1aeSJan Lentfer 		   size_t dev_passwd_len)
1346d49e1aeSJan Lentfer {
1356d49e1aeSJan Lentfer 	u8 hash[SHA256_MAC_LEN];
1366d49e1aeSJan Lentfer 
137*a1157835SDaniel Fojt 	if (hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, dev_passwd,
138*a1157835SDaniel Fojt 			(dev_passwd_len + 1) / 2, hash) < 0)
139*a1157835SDaniel Fojt 		return -1;
1406d49e1aeSJan Lentfer 	os_memcpy(wps->psk1, hash, WPS_PSK_LEN);
141*a1157835SDaniel Fojt 	if (hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN,
1426d49e1aeSJan Lentfer 			dev_passwd + (dev_passwd_len + 1) / 2,
143*a1157835SDaniel Fojt 			dev_passwd_len / 2, hash) < 0)
144*a1157835SDaniel Fojt 		return -1;
1456d49e1aeSJan Lentfer 	os_memcpy(wps->psk2, hash, WPS_PSK_LEN);
1466d49e1aeSJan Lentfer 
1476d49e1aeSJan Lentfer 	wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Device Password",
1486d49e1aeSJan Lentfer 			      dev_passwd, dev_passwd_len);
1496d49e1aeSJan Lentfer 	wpa_hexdump_key(MSG_DEBUG, "WPS: PSK1", wps->psk1, WPS_PSK_LEN);
1506d49e1aeSJan Lentfer 	wpa_hexdump_key(MSG_DEBUG, "WPS: PSK2", wps->psk2, WPS_PSK_LEN);
151*a1157835SDaniel Fojt 	return 0;
1526d49e1aeSJan Lentfer }
1536d49e1aeSJan Lentfer 
1546d49e1aeSJan Lentfer 
wps_decrypt_encr_settings(struct wps_data * wps,const u8 * encr,size_t encr_len)1556d49e1aeSJan Lentfer struct wpabuf * wps_decrypt_encr_settings(struct wps_data *wps, const u8 *encr,
1566d49e1aeSJan Lentfer 					  size_t encr_len)
1576d49e1aeSJan Lentfer {
1586d49e1aeSJan Lentfer 	struct wpabuf *decrypted;
1596d49e1aeSJan Lentfer 	const size_t block_size = 16;
1606d49e1aeSJan Lentfer 	size_t i;
1616d49e1aeSJan Lentfer 	u8 pad;
1626d49e1aeSJan Lentfer 	const u8 *pos;
1636d49e1aeSJan Lentfer 
1646d49e1aeSJan Lentfer 	/* AES-128-CBC */
1656d49e1aeSJan Lentfer 	if (encr == NULL || encr_len < 2 * block_size || encr_len % block_size)
1666d49e1aeSJan Lentfer 	{
1676d49e1aeSJan Lentfer 		wpa_printf(MSG_DEBUG, "WPS: No Encrypted Settings received");
1686d49e1aeSJan Lentfer 		return NULL;
1696d49e1aeSJan Lentfer 	}
1706d49e1aeSJan Lentfer 
1716d49e1aeSJan Lentfer 	decrypted = wpabuf_alloc(encr_len - block_size);
1726d49e1aeSJan Lentfer 	if (decrypted == NULL)
1736d49e1aeSJan Lentfer 		return NULL;
1746d49e1aeSJan Lentfer 
1756d49e1aeSJan Lentfer 	wpa_hexdump(MSG_MSGDUMP, "WPS: Encrypted Settings", encr, encr_len);
1766d49e1aeSJan Lentfer 	wpabuf_put_data(decrypted, encr + block_size, encr_len - block_size);
1776d49e1aeSJan Lentfer 	if (aes_128_cbc_decrypt(wps->keywrapkey, encr, wpabuf_mhead(decrypted),
1786d49e1aeSJan Lentfer 				wpabuf_len(decrypted))) {
179*a1157835SDaniel Fojt 		wpabuf_clear_free(decrypted);
1806d49e1aeSJan Lentfer 		return NULL;
1816d49e1aeSJan Lentfer 	}
1826d49e1aeSJan Lentfer 
1836d49e1aeSJan Lentfer 	wpa_hexdump_buf_key(MSG_MSGDUMP, "WPS: Decrypted Encrypted Settings",
1846d49e1aeSJan Lentfer 			    decrypted);
1856d49e1aeSJan Lentfer 
1866d49e1aeSJan Lentfer 	pos = wpabuf_head_u8(decrypted) + wpabuf_len(decrypted) - 1;
1876d49e1aeSJan Lentfer 	pad = *pos;
1886d49e1aeSJan Lentfer 	if (pad > wpabuf_len(decrypted)) {
1896d49e1aeSJan Lentfer 		wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad value");
190*a1157835SDaniel Fojt 		wpabuf_clear_free(decrypted);
1916d49e1aeSJan Lentfer 		return NULL;
1926d49e1aeSJan Lentfer 	}
1936d49e1aeSJan Lentfer 	for (i = 0; i < pad; i++) {
1946d49e1aeSJan Lentfer 		if (*pos-- != pad) {
1956d49e1aeSJan Lentfer 			wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad "
1966d49e1aeSJan Lentfer 				   "string");
197*a1157835SDaniel Fojt 			wpabuf_clear_free(decrypted);
1986d49e1aeSJan Lentfer 			return NULL;
1996d49e1aeSJan Lentfer 		}
2006d49e1aeSJan Lentfer 	}
2016d49e1aeSJan Lentfer 	decrypted->used -= pad;
2026d49e1aeSJan Lentfer 
2036d49e1aeSJan Lentfer 	return decrypted;
2046d49e1aeSJan Lentfer }
2056d49e1aeSJan Lentfer 
2066d49e1aeSJan Lentfer 
2076d49e1aeSJan Lentfer /**
2086d49e1aeSJan Lentfer  * wps_pin_checksum - Compute PIN checksum
2096d49e1aeSJan Lentfer  * @pin: Seven digit PIN (i.e., eight digit PIN without the checksum digit)
2106d49e1aeSJan Lentfer  * Returns: Checksum digit
2116d49e1aeSJan Lentfer  */
wps_pin_checksum(unsigned int pin)2126d49e1aeSJan Lentfer unsigned int wps_pin_checksum(unsigned int pin)
2136d49e1aeSJan Lentfer {
2146d49e1aeSJan Lentfer 	unsigned int accum = 0;
2156d49e1aeSJan Lentfer 	while (pin) {
2166d49e1aeSJan Lentfer 		accum += 3 * (pin % 10);
2176d49e1aeSJan Lentfer 		pin /= 10;
2186d49e1aeSJan Lentfer 		accum += pin % 10;
2196d49e1aeSJan Lentfer 		pin /= 10;
2206d49e1aeSJan Lentfer 	}
2216d49e1aeSJan Lentfer 
2226d49e1aeSJan Lentfer 	return (10 - accum % 10) % 10;
2236d49e1aeSJan Lentfer }
2246d49e1aeSJan Lentfer 
2256d49e1aeSJan Lentfer 
2266d49e1aeSJan Lentfer /**
2276d49e1aeSJan Lentfer  * wps_pin_valid - Check whether a PIN has a valid checksum
2286d49e1aeSJan Lentfer  * @pin: Eight digit PIN (i.e., including the checksum digit)
2296d49e1aeSJan Lentfer  * Returns: 1 if checksum digit is valid, or 0 if not
2306d49e1aeSJan Lentfer  */
wps_pin_valid(unsigned int pin)2316d49e1aeSJan Lentfer unsigned int wps_pin_valid(unsigned int pin)
2326d49e1aeSJan Lentfer {
2336d49e1aeSJan Lentfer 	return wps_pin_checksum(pin / 10) == (pin % 10);
2346d49e1aeSJan Lentfer }
2356d49e1aeSJan Lentfer 
2366d49e1aeSJan Lentfer 
2376d49e1aeSJan Lentfer /**
2386d49e1aeSJan Lentfer  * wps_generate_pin - Generate a random PIN
2396d49e1aeSJan Lentfer  * Returns: Eight digit PIN (i.e., including the checksum digit)
2406d49e1aeSJan Lentfer  */
wps_generate_pin(unsigned int * pin)241*a1157835SDaniel Fojt int wps_generate_pin(unsigned int *pin)
2426d49e1aeSJan Lentfer {
2436d49e1aeSJan Lentfer 	unsigned int val;
2446d49e1aeSJan Lentfer 
2456d49e1aeSJan Lentfer 	/* Generate seven random digits for the PIN */
246*a1157835SDaniel Fojt 	if (random_get_bytes((unsigned char *) &val, sizeof(val)) < 0)
247*a1157835SDaniel Fojt 		return -1;
2486d49e1aeSJan Lentfer 	val %= 10000000;
2496d49e1aeSJan Lentfer 
2506d49e1aeSJan Lentfer 	/* Append checksum digit */
251*a1157835SDaniel Fojt 	*pin = val * 10 + wps_pin_checksum(val);
252*a1157835SDaniel Fojt 	return 0;
2536d49e1aeSJan Lentfer }
2546d49e1aeSJan Lentfer 
2556d49e1aeSJan Lentfer 
wps_pin_str_valid(const char * pin)2563ff40c12SJohn Marino int wps_pin_str_valid(const char *pin)
2573ff40c12SJohn Marino {
2583ff40c12SJohn Marino 	const char *p;
2593ff40c12SJohn Marino 	size_t len;
2603ff40c12SJohn Marino 
2613ff40c12SJohn Marino 	p = pin;
2623ff40c12SJohn Marino 	while (*p >= '0' && *p <= '9')
2633ff40c12SJohn Marino 		p++;
2643ff40c12SJohn Marino 	if (*p != '\0')
2653ff40c12SJohn Marino 		return 0;
2663ff40c12SJohn Marino 
2673ff40c12SJohn Marino 	len = p - pin;
2683ff40c12SJohn Marino 	return len == 4 || len == 8;
2693ff40c12SJohn Marino }
2703ff40c12SJohn Marino 
2713ff40c12SJohn Marino 
wps_fail_event(struct wps_context * wps,enum wps_msg_type msg,u16 config_error,u16 error_indication,const u8 * mac_addr)2723ff40c12SJohn Marino void wps_fail_event(struct wps_context *wps, enum wps_msg_type msg,
2733ff40c12SJohn Marino 		    u16 config_error, u16 error_indication, const u8 *mac_addr)
2746d49e1aeSJan Lentfer {
2756d49e1aeSJan Lentfer 	union wps_event_data data;
2766d49e1aeSJan Lentfer 
2776d49e1aeSJan Lentfer 	if (wps->event_cb == NULL)
2786d49e1aeSJan Lentfer 		return;
2796d49e1aeSJan Lentfer 
2806d49e1aeSJan Lentfer 	os_memset(&data, 0, sizeof(data));
2816d49e1aeSJan Lentfer 	data.fail.msg = msg;
2823ff40c12SJohn Marino 	data.fail.config_error = config_error;
2833ff40c12SJohn Marino 	data.fail.error_indication = error_indication;
2843ff40c12SJohn Marino 	os_memcpy(data.fail.peer_macaddr, mac_addr, ETH_ALEN);
2856d49e1aeSJan Lentfer 	wps->event_cb(wps->cb_ctx, WPS_EV_FAIL, &data);
2866d49e1aeSJan Lentfer }
2876d49e1aeSJan Lentfer 
2886d49e1aeSJan Lentfer 
wps_success_event(struct wps_context * wps,const u8 * mac_addr)2893ff40c12SJohn Marino void wps_success_event(struct wps_context *wps, const u8 *mac_addr)
2906d49e1aeSJan Lentfer {
2913ff40c12SJohn Marino 	union wps_event_data data;
2923ff40c12SJohn Marino 
2936d49e1aeSJan Lentfer 	if (wps->event_cb == NULL)
2946d49e1aeSJan Lentfer 		return;
2956d49e1aeSJan Lentfer 
2963ff40c12SJohn Marino 	os_memset(&data, 0, sizeof(data));
2973ff40c12SJohn Marino 	os_memcpy(data.success.peer_macaddr, mac_addr, ETH_ALEN);
2983ff40c12SJohn Marino 	wps->event_cb(wps->cb_ctx, WPS_EV_SUCCESS, &data);
2996d49e1aeSJan Lentfer }
3006d49e1aeSJan Lentfer 
3016d49e1aeSJan Lentfer 
wps_pwd_auth_fail_event(struct wps_context * wps,int enrollee,int part,const u8 * mac_addr)3023ff40c12SJohn Marino void wps_pwd_auth_fail_event(struct wps_context *wps, int enrollee, int part,
3033ff40c12SJohn Marino 			     const u8 *mac_addr)
3046d49e1aeSJan Lentfer {
3056d49e1aeSJan Lentfer 	union wps_event_data data;
3066d49e1aeSJan Lentfer 
3076d49e1aeSJan Lentfer 	if (wps->event_cb == NULL)
3086d49e1aeSJan Lentfer 		return;
3096d49e1aeSJan Lentfer 
3106d49e1aeSJan Lentfer 	os_memset(&data, 0, sizeof(data));
3116d49e1aeSJan Lentfer 	data.pwd_auth_fail.enrollee = enrollee;
3126d49e1aeSJan Lentfer 	data.pwd_auth_fail.part = part;
3133ff40c12SJohn Marino 	os_memcpy(data.pwd_auth_fail.peer_macaddr, mac_addr, ETH_ALEN);
3146d49e1aeSJan Lentfer 	wps->event_cb(wps->cb_ctx, WPS_EV_PWD_AUTH_FAIL, &data);
3156d49e1aeSJan Lentfer }
3166d49e1aeSJan Lentfer 
3176d49e1aeSJan Lentfer 
wps_pbc_overlap_event(struct wps_context * wps)3186d49e1aeSJan Lentfer void wps_pbc_overlap_event(struct wps_context *wps)
3196d49e1aeSJan Lentfer {
3206d49e1aeSJan Lentfer 	if (wps->event_cb == NULL)
3216d49e1aeSJan Lentfer 		return;
3226d49e1aeSJan Lentfer 
3236d49e1aeSJan Lentfer 	wps->event_cb(wps->cb_ctx, WPS_EV_PBC_OVERLAP, NULL);
3246d49e1aeSJan Lentfer }
3256d49e1aeSJan Lentfer 
3266d49e1aeSJan Lentfer 
wps_pbc_timeout_event(struct wps_context * wps)3276d49e1aeSJan Lentfer void wps_pbc_timeout_event(struct wps_context *wps)
3286d49e1aeSJan Lentfer {
3296d49e1aeSJan Lentfer 	if (wps->event_cb == NULL)
3306d49e1aeSJan Lentfer 		return;
3316d49e1aeSJan Lentfer 
3326d49e1aeSJan Lentfer 	wps->event_cb(wps->cb_ctx, WPS_EV_PBC_TIMEOUT, NULL);
3336d49e1aeSJan Lentfer }
3343ff40c12SJohn Marino 
3353ff40c12SJohn Marino 
wps_pbc_active_event(struct wps_context * wps)3363ff40c12SJohn Marino void wps_pbc_active_event(struct wps_context *wps)
3373ff40c12SJohn Marino {
3383ff40c12SJohn Marino 	if (wps->event_cb == NULL)
3393ff40c12SJohn Marino 		return;
3403ff40c12SJohn Marino 
3413ff40c12SJohn Marino 	wps->event_cb(wps->cb_ctx, WPS_EV_PBC_ACTIVE, NULL);
3423ff40c12SJohn Marino }
3433ff40c12SJohn Marino 
3443ff40c12SJohn Marino 
wps_pbc_disable_event(struct wps_context * wps)3453ff40c12SJohn Marino void wps_pbc_disable_event(struct wps_context *wps)
3463ff40c12SJohn Marino {
3473ff40c12SJohn Marino 	if (wps->event_cb == NULL)
3483ff40c12SJohn Marino 		return;
3493ff40c12SJohn Marino 
3503ff40c12SJohn Marino 	wps->event_cb(wps->cb_ctx, WPS_EV_PBC_DISABLE, NULL);
3513ff40c12SJohn Marino }
3523ff40c12SJohn Marino 
3533ff40c12SJohn Marino 
3543ff40c12SJohn Marino #ifdef CONFIG_WPS_OOB
3553ff40c12SJohn Marino 
wps_get_oob_cred(struct wps_context * wps,int rf_band,int channel)3563ff40c12SJohn Marino struct wpabuf * wps_get_oob_cred(struct wps_context *wps, int rf_band,
3573ff40c12SJohn Marino 				 int channel)
3583ff40c12SJohn Marino {
3593ff40c12SJohn Marino 	struct wps_data data;
3603ff40c12SJohn Marino 	struct wpabuf *plain;
3613ff40c12SJohn Marino 
3623ff40c12SJohn Marino 	plain = wpabuf_alloc(500);
3633ff40c12SJohn Marino 	if (plain == NULL) {
3643ff40c12SJohn Marino 		wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
3653ff40c12SJohn Marino 			   "credential");
3663ff40c12SJohn Marino 		return NULL;
3673ff40c12SJohn Marino 	}
3683ff40c12SJohn Marino 
3693ff40c12SJohn Marino 	os_memset(&data, 0, sizeof(data));
3703ff40c12SJohn Marino 	data.wps = wps;
3713ff40c12SJohn Marino 	data.auth_type = wps->auth_types;
3723ff40c12SJohn Marino 	data.encr_type = wps->encr_types;
3733ff40c12SJohn Marino 	if (wps_build_cred(&data, plain) ||
3743ff40c12SJohn Marino 	    (rf_band && wps_build_rf_bands_attr(plain, rf_band)) ||
3753ff40c12SJohn Marino 	    (channel && wps_build_ap_channel(plain, channel)) ||
3763ff40c12SJohn Marino 	    wps_build_mac_addr(plain, wps->dev.mac_addr) ||
377*a1157835SDaniel Fojt 	    wps_build_wfa_ext(plain, 0, NULL, 0, 0)) {
3783ff40c12SJohn Marino 		os_free(data.new_psk);
379*a1157835SDaniel Fojt 		wpabuf_clear_free(plain);
3803ff40c12SJohn Marino 		return NULL;
3813ff40c12SJohn Marino 	}
3823ff40c12SJohn Marino 
3833ff40c12SJohn Marino 	if (wps->wps_state == WPS_STATE_NOT_CONFIGURED && data.new_psk &&
3843ff40c12SJohn Marino 	    wps->ap) {
3853ff40c12SJohn Marino 		struct wps_credential cred;
3863ff40c12SJohn Marino 
3873ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
3883ff40c12SJohn Marino 			   "on credential token generation");
3893ff40c12SJohn Marino 
3903ff40c12SJohn Marino 		os_memset(&cred, 0, sizeof(cred));
3913ff40c12SJohn Marino 		os_memcpy(cred.ssid, wps->ssid, wps->ssid_len);
3923ff40c12SJohn Marino 		cred.ssid_len = wps->ssid_len;
3933ff40c12SJohn Marino 		cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
3943ff40c12SJohn Marino 		cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
3953ff40c12SJohn Marino 		os_memcpy(cred.key, data.new_psk, data.new_psk_len);
3963ff40c12SJohn Marino 		cred.key_len = data.new_psk_len;
3973ff40c12SJohn Marino 
3983ff40c12SJohn Marino 		wps->wps_state = WPS_STATE_CONFIGURED;
3993ff40c12SJohn Marino 		wpa_hexdump_ascii_key(MSG_DEBUG,
4003ff40c12SJohn Marino 				      "WPS: Generated random passphrase",
4013ff40c12SJohn Marino 				      data.new_psk, data.new_psk_len);
4023ff40c12SJohn Marino 		if (wps->cred_cb)
4033ff40c12SJohn Marino 			wps->cred_cb(wps->cb_ctx, &cred);
4043ff40c12SJohn Marino 	}
4053ff40c12SJohn Marino 
4063ff40c12SJohn Marino 	os_free(data.new_psk);
4073ff40c12SJohn Marino 
4083ff40c12SJohn Marino 	return plain;
4093ff40c12SJohn Marino }
4103ff40c12SJohn Marino 
4113ff40c12SJohn Marino 
wps_build_nfc_pw_token(u16 dev_pw_id,const struct wpabuf * pubkey,const struct wpabuf * dev_pw)4123ff40c12SJohn Marino struct wpabuf * wps_build_nfc_pw_token(u16 dev_pw_id,
4133ff40c12SJohn Marino 				       const struct wpabuf *pubkey,
4143ff40c12SJohn Marino 				       const struct wpabuf *dev_pw)
4153ff40c12SJohn Marino {
4163ff40c12SJohn Marino 	struct wpabuf *data;
4173ff40c12SJohn Marino 
4183ff40c12SJohn Marino 	data = wpabuf_alloc(200);
4193ff40c12SJohn Marino 	if (data == NULL)
4203ff40c12SJohn Marino 		return NULL;
4213ff40c12SJohn Marino 
4223ff40c12SJohn Marino 	if (wps_build_oob_dev_pw(data, dev_pw_id, pubkey,
4233ff40c12SJohn Marino 				 wpabuf_head(dev_pw), wpabuf_len(dev_pw)) ||
424*a1157835SDaniel Fojt 	    wps_build_wfa_ext(data, 0, NULL, 0, 0)) {
4253ff40c12SJohn Marino 		wpa_printf(MSG_ERROR, "WPS: Failed to build NFC password "
4263ff40c12SJohn Marino 			   "token");
427*a1157835SDaniel Fojt 		wpabuf_clear_free(data);
4283ff40c12SJohn Marino 		return NULL;
4293ff40c12SJohn Marino 	}
4303ff40c12SJohn Marino 
4313ff40c12SJohn Marino 	return data;
4323ff40c12SJohn Marino }
4333ff40c12SJohn Marino 
4343ff40c12SJohn Marino 
wps_oob_use_cred(struct wps_context * wps,struct wps_parse_attr * attr)4353ff40c12SJohn Marino int wps_oob_use_cred(struct wps_context *wps, struct wps_parse_attr *attr)
4363ff40c12SJohn Marino {
4373ff40c12SJohn Marino 	struct wpabuf msg;
4383ff40c12SJohn Marino 	size_t i;
4393ff40c12SJohn Marino 
4403ff40c12SJohn Marino 	for (i = 0; i < attr->num_cred; i++) {
4413ff40c12SJohn Marino 		struct wps_credential local_cred;
4423ff40c12SJohn Marino 		struct wps_parse_attr cattr;
4433ff40c12SJohn Marino 
4443ff40c12SJohn Marino 		os_memset(&local_cred, 0, sizeof(local_cred));
4453ff40c12SJohn Marino 		wpabuf_set(&msg, attr->cred[i], attr->cred_len[i]);
4463ff40c12SJohn Marino 		if (wps_parse_msg(&msg, &cattr) < 0 ||
4473ff40c12SJohn Marino 		    wps_process_cred(&cattr, &local_cred)) {
4483ff40c12SJohn Marino 			wpa_printf(MSG_ERROR, "WPS: Failed to parse OOB "
4493ff40c12SJohn Marino 				   "credential");
4503ff40c12SJohn Marino 			return -1;
4513ff40c12SJohn Marino 		}
4523ff40c12SJohn Marino 		wps->cred_cb(wps->cb_ctx, &local_cred);
4533ff40c12SJohn Marino 	}
4543ff40c12SJohn Marino 
4553ff40c12SJohn Marino 	return 0;
4563ff40c12SJohn Marino }
4573ff40c12SJohn Marino 
4583ff40c12SJohn Marino 
4593ff40c12SJohn Marino #endif /* CONFIG_WPS_OOB */
4603ff40c12SJohn Marino 
4613ff40c12SJohn Marino 
wps_dev_type_str2bin(const char * str,u8 dev_type[WPS_DEV_TYPE_LEN])4623ff40c12SJohn Marino int wps_dev_type_str2bin(const char *str, u8 dev_type[WPS_DEV_TYPE_LEN])
4633ff40c12SJohn Marino {
4643ff40c12SJohn Marino 	const char *pos;
4653ff40c12SJohn Marino 
4663ff40c12SJohn Marino 	/* <categ>-<OUI>-<subcateg> */
4673ff40c12SJohn Marino 	WPA_PUT_BE16(dev_type, atoi(str));
4683ff40c12SJohn Marino 	pos = os_strchr(str, '-');
4693ff40c12SJohn Marino 	if (pos == NULL)
4703ff40c12SJohn Marino 		return -1;
4713ff40c12SJohn Marino 	pos++;
4723ff40c12SJohn Marino 	if (hexstr2bin(pos, &dev_type[2], 4))
4733ff40c12SJohn Marino 		return -1;
4743ff40c12SJohn Marino 	pos = os_strchr(pos, '-');
4753ff40c12SJohn Marino 	if (pos == NULL)
4763ff40c12SJohn Marino 		return -1;
4773ff40c12SJohn Marino 	pos++;
4783ff40c12SJohn Marino 	WPA_PUT_BE16(&dev_type[6], atoi(pos));
4793ff40c12SJohn Marino 
4803ff40c12SJohn Marino 
4813ff40c12SJohn Marino 	return 0;
4823ff40c12SJohn Marino }
4833ff40c12SJohn Marino 
4843ff40c12SJohn Marino 
wps_dev_type_bin2str(const u8 dev_type[WPS_DEV_TYPE_LEN],char * buf,size_t buf_len)4853ff40c12SJohn Marino char * wps_dev_type_bin2str(const u8 dev_type[WPS_DEV_TYPE_LEN], char *buf,
4863ff40c12SJohn Marino 			    size_t buf_len)
4873ff40c12SJohn Marino {
4883ff40c12SJohn Marino 	int ret;
4893ff40c12SJohn Marino 
4903ff40c12SJohn Marino 	ret = os_snprintf(buf, buf_len, "%u-%08X-%u",
4913ff40c12SJohn Marino 			  WPA_GET_BE16(dev_type), WPA_GET_BE32(&dev_type[2]),
4923ff40c12SJohn Marino 			  WPA_GET_BE16(&dev_type[6]));
493*a1157835SDaniel Fojt 	if (os_snprintf_error(buf_len, ret))
4943ff40c12SJohn Marino 		return NULL;
4953ff40c12SJohn Marino 
4963ff40c12SJohn Marino 	return buf;
4973ff40c12SJohn Marino }
4983ff40c12SJohn Marino 
4993ff40c12SJohn Marino 
uuid_gen_mac_addr(const u8 * mac_addr,u8 * uuid)5003ff40c12SJohn Marino void uuid_gen_mac_addr(const u8 *mac_addr, u8 *uuid)
5013ff40c12SJohn Marino {
5023ff40c12SJohn Marino 	const u8 *addr[2];
5033ff40c12SJohn Marino 	size_t len[2];
5043ff40c12SJohn Marino 	u8 hash[SHA1_MAC_LEN];
5053ff40c12SJohn Marino 	u8 nsid[16] = {
5063ff40c12SJohn Marino 		0x52, 0x64, 0x80, 0xf8,
5073ff40c12SJohn Marino 		0xc9, 0x9b,
5083ff40c12SJohn Marino 		0x4b, 0xe5,
5093ff40c12SJohn Marino 		0xa6, 0x55,
5103ff40c12SJohn Marino 		0x58, 0xed, 0x5f, 0x5d, 0x60, 0x84
5113ff40c12SJohn Marino 	};
5123ff40c12SJohn Marino 
5133ff40c12SJohn Marino 	addr[0] = nsid;
5143ff40c12SJohn Marino 	len[0] = sizeof(nsid);
5153ff40c12SJohn Marino 	addr[1] = mac_addr;
5163ff40c12SJohn Marino 	len[1] = 6;
5173ff40c12SJohn Marino 	sha1_vector(2, addr, len, hash);
5183ff40c12SJohn Marino 	os_memcpy(uuid, hash, 16);
5193ff40c12SJohn Marino 
5203ff40c12SJohn Marino 	/* Version: 5 = named-based version using SHA-1 */
5213ff40c12SJohn Marino 	uuid[6] = (5 << 4) | (uuid[6] & 0x0f);
5223ff40c12SJohn Marino 
5233ff40c12SJohn Marino 	/* Variant specified in RFC 4122 */
5243ff40c12SJohn Marino 	uuid[8] = 0x80 | (uuid[8] & 0x3f);
5253ff40c12SJohn Marino }
5263ff40c12SJohn Marino 
5273ff40c12SJohn Marino 
wps_config_methods_str2bin(const char * str)5283ff40c12SJohn Marino u16 wps_config_methods_str2bin(const char *str)
5293ff40c12SJohn Marino {
5303ff40c12SJohn Marino 	u16 methods = 0;
5313ff40c12SJohn Marino 
532*a1157835SDaniel Fojt 	if (str == NULL || str[0] == '\0') {
5333ff40c12SJohn Marino 		/* Default to enabling methods based on build configuration */
5343ff40c12SJohn Marino 		methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
5353ff40c12SJohn Marino 		methods |= WPS_CONFIG_VIRT_DISPLAY;
5363ff40c12SJohn Marino #ifdef CONFIG_WPS_NFC
5373ff40c12SJohn Marino 		methods |= WPS_CONFIG_NFC_INTERFACE;
5383ff40c12SJohn Marino #endif /* CONFIG_WPS_NFC */
539*a1157835SDaniel Fojt #ifdef CONFIG_P2P
540*a1157835SDaniel Fojt 		methods |= WPS_CONFIG_P2PS;
541*a1157835SDaniel Fojt #endif /* CONFIG_P2P */
5423ff40c12SJohn Marino 	} else {
5433ff40c12SJohn Marino 		if (os_strstr(str, "ethernet"))
5443ff40c12SJohn Marino 			methods |= WPS_CONFIG_ETHERNET;
5453ff40c12SJohn Marino 		if (os_strstr(str, "label"))
5463ff40c12SJohn Marino 			methods |= WPS_CONFIG_LABEL;
5473ff40c12SJohn Marino 		if (os_strstr(str, "display"))
5483ff40c12SJohn Marino 			methods |= WPS_CONFIG_DISPLAY;
5493ff40c12SJohn Marino 		if (os_strstr(str, "ext_nfc_token"))
5503ff40c12SJohn Marino 			methods |= WPS_CONFIG_EXT_NFC_TOKEN;
5513ff40c12SJohn Marino 		if (os_strstr(str, "int_nfc_token"))
5523ff40c12SJohn Marino 			methods |= WPS_CONFIG_INT_NFC_TOKEN;
5533ff40c12SJohn Marino 		if (os_strstr(str, "nfc_interface"))
5543ff40c12SJohn Marino 			methods |= WPS_CONFIG_NFC_INTERFACE;
5553ff40c12SJohn Marino 		if (os_strstr(str, "push_button"))
5563ff40c12SJohn Marino 			methods |= WPS_CONFIG_PUSHBUTTON;
5573ff40c12SJohn Marino 		if (os_strstr(str, "keypad"))
5583ff40c12SJohn Marino 			methods |= WPS_CONFIG_KEYPAD;
5593ff40c12SJohn Marino 		if (os_strstr(str, "virtual_display"))
5603ff40c12SJohn Marino 			methods |= WPS_CONFIG_VIRT_DISPLAY;
5613ff40c12SJohn Marino 		if (os_strstr(str, "physical_display"))
5623ff40c12SJohn Marino 			methods |= WPS_CONFIG_PHY_DISPLAY;
5633ff40c12SJohn Marino 		if (os_strstr(str, "virtual_push_button"))
5643ff40c12SJohn Marino 			methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
5653ff40c12SJohn Marino 		if (os_strstr(str, "physical_push_button"))
5663ff40c12SJohn Marino 			methods |= WPS_CONFIG_PHY_PUSHBUTTON;
567*a1157835SDaniel Fojt 		if (os_strstr(str, "p2ps"))
568*a1157835SDaniel Fojt 			methods |= WPS_CONFIG_P2PS;
5693ff40c12SJohn Marino 	}
5703ff40c12SJohn Marino 
5713ff40c12SJohn Marino 	return methods;
5723ff40c12SJohn Marino }
5733ff40c12SJohn Marino 
5743ff40c12SJohn Marino 
wps_build_wsc_ack(struct wps_data * wps)5753ff40c12SJohn Marino struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
5763ff40c12SJohn Marino {
5773ff40c12SJohn Marino 	struct wpabuf *msg;
5783ff40c12SJohn Marino 
5793ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
5803ff40c12SJohn Marino 
5813ff40c12SJohn Marino 	msg = wpabuf_alloc(1000);
5823ff40c12SJohn Marino 	if (msg == NULL)
5833ff40c12SJohn Marino 		return NULL;
5843ff40c12SJohn Marino 
5853ff40c12SJohn Marino 	if (wps_build_version(msg) ||
5863ff40c12SJohn Marino 	    wps_build_msg_type(msg, WPS_WSC_ACK) ||
5873ff40c12SJohn Marino 	    wps_build_enrollee_nonce(wps, msg) ||
5883ff40c12SJohn Marino 	    wps_build_registrar_nonce(wps, msg) ||
589*a1157835SDaniel Fojt 	    wps_build_wfa_ext(msg, 0, NULL, 0, 0)) {
5903ff40c12SJohn Marino 		wpabuf_free(msg);
5913ff40c12SJohn Marino 		return NULL;
5923ff40c12SJohn Marino 	}
5933ff40c12SJohn Marino 
5943ff40c12SJohn Marino 	return msg;
5953ff40c12SJohn Marino }
5963ff40c12SJohn Marino 
5973ff40c12SJohn Marino 
wps_build_wsc_nack(struct wps_data * wps)5983ff40c12SJohn Marino struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
5993ff40c12SJohn Marino {
6003ff40c12SJohn Marino 	struct wpabuf *msg;
6013ff40c12SJohn Marino 
6023ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
6033ff40c12SJohn Marino 
6043ff40c12SJohn Marino 	msg = wpabuf_alloc(1000);
6053ff40c12SJohn Marino 	if (msg == NULL)
6063ff40c12SJohn Marino 		return NULL;
6073ff40c12SJohn Marino 
6083ff40c12SJohn Marino 	if (wps_build_version(msg) ||
6093ff40c12SJohn Marino 	    wps_build_msg_type(msg, WPS_WSC_NACK) ||
6103ff40c12SJohn Marino 	    wps_build_enrollee_nonce(wps, msg) ||
6113ff40c12SJohn Marino 	    wps_build_registrar_nonce(wps, msg) ||
6123ff40c12SJohn Marino 	    wps_build_config_error(msg, wps->config_error) ||
613*a1157835SDaniel Fojt 	    wps_build_wfa_ext(msg, 0, NULL, 0, 0)) {
6143ff40c12SJohn Marino 		wpabuf_free(msg);
6153ff40c12SJohn Marino 		return NULL;
6163ff40c12SJohn Marino 	}
6173ff40c12SJohn Marino 
6183ff40c12SJohn Marino 	return msg;
6193ff40c12SJohn Marino }
6203ff40c12SJohn Marino 
6213ff40c12SJohn Marino 
6223ff40c12SJohn Marino #ifdef CONFIG_WPS_NFC
6233ff40c12SJohn Marino 
wps_nfc_token_build(int ndef,int id,struct wpabuf * pubkey,struct wpabuf * dev_pw)6243ff40c12SJohn Marino struct wpabuf * wps_nfc_token_build(int ndef, int id, struct wpabuf *pubkey,
6253ff40c12SJohn Marino 				    struct wpabuf *dev_pw)
6263ff40c12SJohn Marino {
6273ff40c12SJohn Marino 	struct wpabuf *ret;
6283ff40c12SJohn Marino 
6293ff40c12SJohn Marino 	if (pubkey == NULL || dev_pw == NULL)
6303ff40c12SJohn Marino 		return NULL;
6313ff40c12SJohn Marino 
6323ff40c12SJohn Marino 	ret = wps_build_nfc_pw_token(id, pubkey, dev_pw);
6333ff40c12SJohn Marino 	if (ndef && ret) {
6343ff40c12SJohn Marino 		struct wpabuf *tmp;
6353ff40c12SJohn Marino 		tmp = ndef_build_wifi(ret);
6363ff40c12SJohn Marino 		wpabuf_free(ret);
6373ff40c12SJohn Marino 		if (tmp == NULL)
6383ff40c12SJohn Marino 			return NULL;
6393ff40c12SJohn Marino 		ret = tmp;
6403ff40c12SJohn Marino 	}
6413ff40c12SJohn Marino 
6423ff40c12SJohn Marino 	return ret;
6433ff40c12SJohn Marino }
6443ff40c12SJohn Marino 
6453ff40c12SJohn Marino 
wps_nfc_gen_dh(struct wpabuf ** pubkey,struct wpabuf ** privkey)6463ff40c12SJohn Marino int wps_nfc_gen_dh(struct wpabuf **pubkey, struct wpabuf **privkey)
6473ff40c12SJohn Marino {
6483ff40c12SJohn Marino 	struct wpabuf *priv = NULL, *pub = NULL;
6493ff40c12SJohn Marino 	void *dh_ctx;
6503ff40c12SJohn Marino 
6513ff40c12SJohn Marino 	dh_ctx = dh5_init(&priv, &pub);
6523ff40c12SJohn Marino 	if (dh_ctx == NULL)
6533ff40c12SJohn Marino 		return -1;
6543ff40c12SJohn Marino 	pub = wpabuf_zeropad(pub, 192);
6553ff40c12SJohn Marino 	if (pub == NULL) {
6563ff40c12SJohn Marino 		wpabuf_free(priv);
657*a1157835SDaniel Fojt 		dh5_free(dh_ctx);
6583ff40c12SJohn Marino 		return -1;
6593ff40c12SJohn Marino 	}
6603ff40c12SJohn Marino 	wpa_hexdump_buf(MSG_DEBUG, "WPS: Generated new DH pubkey", pub);
6613ff40c12SJohn Marino 	dh5_free(dh_ctx);
6623ff40c12SJohn Marino 
6633ff40c12SJohn Marino 	wpabuf_free(*pubkey);
6643ff40c12SJohn Marino 	*pubkey = pub;
665*a1157835SDaniel Fojt 	wpabuf_clear_free(*privkey);
6663ff40c12SJohn Marino 	*privkey = priv;
6673ff40c12SJohn Marino 
6683ff40c12SJohn Marino 	return 0;
6693ff40c12SJohn Marino }
6703ff40c12SJohn Marino 
6713ff40c12SJohn Marino 
wps_nfc_token_gen(int ndef,int * id,struct wpabuf ** pubkey,struct wpabuf ** privkey,struct wpabuf ** dev_pw)6723ff40c12SJohn Marino struct wpabuf * wps_nfc_token_gen(int ndef, int *id, struct wpabuf **pubkey,
6733ff40c12SJohn Marino 				  struct wpabuf **privkey,
6743ff40c12SJohn Marino 				  struct wpabuf **dev_pw)
6753ff40c12SJohn Marino {
6763ff40c12SJohn Marino 	struct wpabuf *pw;
6773ff40c12SJohn Marino 	u16 val;
6783ff40c12SJohn Marino 
6793ff40c12SJohn Marino 	pw = wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN);
6803ff40c12SJohn Marino 	if (pw == NULL)
6813ff40c12SJohn Marino 		return NULL;
6823ff40c12SJohn Marino 
6833ff40c12SJohn Marino 	if (random_get_bytes(wpabuf_put(pw, WPS_OOB_DEVICE_PASSWORD_LEN),
6843ff40c12SJohn Marino 			     WPS_OOB_DEVICE_PASSWORD_LEN) ||
6853ff40c12SJohn Marino 	    random_get_bytes((u8 *) &val, sizeof(val))) {
6863ff40c12SJohn Marino 		wpabuf_free(pw);
6873ff40c12SJohn Marino 		return NULL;
6883ff40c12SJohn Marino 	}
6893ff40c12SJohn Marino 
6903ff40c12SJohn Marino 	if (wps_nfc_gen_dh(pubkey, privkey) < 0) {
6913ff40c12SJohn Marino 		wpabuf_free(pw);
6923ff40c12SJohn Marino 		return NULL;
6933ff40c12SJohn Marino 	}
6943ff40c12SJohn Marino 
6953ff40c12SJohn Marino 	*id = 0x10 + val % 0xfff0;
696*a1157835SDaniel Fojt 	wpabuf_clear_free(*dev_pw);
6973ff40c12SJohn Marino 	*dev_pw = pw;
6983ff40c12SJohn Marino 
6993ff40c12SJohn Marino 	return wps_nfc_token_build(ndef, *id, *pubkey, *dev_pw);
7003ff40c12SJohn Marino }
7013ff40c12SJohn Marino 
7023ff40c12SJohn Marino 
wps_build_nfc_handover_req(struct wps_context * ctx,struct wpabuf * nfc_dh_pubkey)7033ff40c12SJohn Marino struct wpabuf * wps_build_nfc_handover_req(struct wps_context *ctx,
7043ff40c12SJohn Marino 					   struct wpabuf *nfc_dh_pubkey)
7053ff40c12SJohn Marino {
7063ff40c12SJohn Marino 	struct wpabuf *msg;
7073ff40c12SJohn Marino 	void *len;
7083ff40c12SJohn Marino 
7093ff40c12SJohn Marino 	if (ctx == NULL)
7103ff40c12SJohn Marino 		return NULL;
7113ff40c12SJohn Marino 
7123ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "WPS: Building attributes for NFC connection "
7133ff40c12SJohn Marino 		   "handover request");
7143ff40c12SJohn Marino 
7153ff40c12SJohn Marino 	if (nfc_dh_pubkey == NULL) {
7163ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "WPS: No NFC OOB Device Password "
7173ff40c12SJohn Marino 			   "configured");
7183ff40c12SJohn Marino 		return NULL;
7193ff40c12SJohn Marino 	}
7203ff40c12SJohn Marino 
7213ff40c12SJohn Marino 	msg = wpabuf_alloc(1000);
7223ff40c12SJohn Marino 	if (msg == NULL)
7233ff40c12SJohn Marino 		return msg;
7243ff40c12SJohn Marino 	len = wpabuf_put(msg, 2);
7253ff40c12SJohn Marino 
7263ff40c12SJohn Marino 	if (wps_build_oob_dev_pw(msg, DEV_PW_NFC_CONNECTION_HANDOVER,
7273ff40c12SJohn Marino 				 nfc_dh_pubkey, NULL, 0) ||
7283ff40c12SJohn Marino 	    wps_build_uuid_e(msg, ctx->uuid) ||
729*a1157835SDaniel Fojt 	    wps_build_wfa_ext(msg, 0, NULL, 0, 0)) {
7303ff40c12SJohn Marino 		wpabuf_free(msg);
7313ff40c12SJohn Marino 		return NULL;
7323ff40c12SJohn Marino 	}
7333ff40c12SJohn Marino 
7343ff40c12SJohn Marino 	WPA_PUT_BE16(len, wpabuf_len(msg) - 2);
7353ff40c12SJohn Marino 
7363ff40c12SJohn Marino 	return msg;
7373ff40c12SJohn Marino }
7383ff40c12SJohn Marino 
7393ff40c12SJohn Marino 
wps_build_ssid(struct wpabuf * msg,struct wps_context * wps)7403ff40c12SJohn Marino static int wps_build_ssid(struct wpabuf *msg, struct wps_context *wps)
7413ff40c12SJohn Marino {
7423ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "WPS:  * SSID");
7433ff40c12SJohn Marino 	wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID in Connection Handover Select",
7443ff40c12SJohn Marino 			  wps->ssid, wps->ssid_len);
7453ff40c12SJohn Marino 	wpabuf_put_be16(msg, ATTR_SSID);
7463ff40c12SJohn Marino 	wpabuf_put_be16(msg, wps->ssid_len);
7473ff40c12SJohn Marino 	wpabuf_put_data(msg, wps->ssid, wps->ssid_len);
7483ff40c12SJohn Marino 	return 0;
7493ff40c12SJohn Marino }
7503ff40c12SJohn Marino 
7513ff40c12SJohn Marino 
wps_build_ap_freq(struct wpabuf * msg,int freq)7523ff40c12SJohn Marino static int wps_build_ap_freq(struct wpabuf *msg, int freq)
7533ff40c12SJohn Marino {
7543ff40c12SJohn Marino 	enum hostapd_hw_mode mode;
7553ff40c12SJohn Marino 	u8 channel, rf_band;
7563ff40c12SJohn Marino 	u16 ap_channel;
7573ff40c12SJohn Marino 
7583ff40c12SJohn Marino 	if (freq <= 0)
7593ff40c12SJohn Marino 		return 0;
7603ff40c12SJohn Marino 
7613ff40c12SJohn Marino 	mode = ieee80211_freq_to_chan(freq, &channel);
7623ff40c12SJohn Marino 	if (mode == NUM_HOSTAPD_MODES)
7633ff40c12SJohn Marino 		return 0; /* Unknown channel */
7643ff40c12SJohn Marino 
7653ff40c12SJohn Marino 	if (mode == HOSTAPD_MODE_IEEE80211G || mode == HOSTAPD_MODE_IEEE80211B)
7663ff40c12SJohn Marino 		rf_band = WPS_RF_24GHZ;
7673ff40c12SJohn Marino 	else if (mode == HOSTAPD_MODE_IEEE80211A)
7683ff40c12SJohn Marino 		rf_band = WPS_RF_50GHZ;
769*a1157835SDaniel Fojt 	else if (mode == HOSTAPD_MODE_IEEE80211AD)
770*a1157835SDaniel Fojt 		rf_band = WPS_RF_60GHZ;
7713ff40c12SJohn Marino 	else
7723ff40c12SJohn Marino 		return 0; /* Unknown band */
7733ff40c12SJohn Marino 	ap_channel = channel;
7743ff40c12SJohn Marino 
7753ff40c12SJohn Marino 	if (wps_build_rf_bands_attr(msg, rf_band) ||
7763ff40c12SJohn Marino 	    wps_build_ap_channel(msg, ap_channel))
7773ff40c12SJohn Marino 		return -1;
7783ff40c12SJohn Marino 
7793ff40c12SJohn Marino 	return 0;
7803ff40c12SJohn Marino }
7813ff40c12SJohn Marino 
7823ff40c12SJohn Marino 
wps_build_nfc_handover_sel(struct wps_context * ctx,struct wpabuf * nfc_dh_pubkey,const u8 * bssid,int freq)7833ff40c12SJohn Marino struct wpabuf * wps_build_nfc_handover_sel(struct wps_context *ctx,
7843ff40c12SJohn Marino 					   struct wpabuf *nfc_dh_pubkey,
7853ff40c12SJohn Marino 					   const u8 *bssid, int freq)
7863ff40c12SJohn Marino {
7873ff40c12SJohn Marino 	struct wpabuf *msg;
7883ff40c12SJohn Marino 	void *len;
7893ff40c12SJohn Marino 
7903ff40c12SJohn Marino 	if (ctx == NULL)
7913ff40c12SJohn Marino 		return NULL;
7923ff40c12SJohn Marino 
7933ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "WPS: Building attributes for NFC connection "
7943ff40c12SJohn Marino 		   "handover select");
7953ff40c12SJohn Marino 
7963ff40c12SJohn Marino 	if (nfc_dh_pubkey == NULL) {
7973ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "WPS: No NFC OOB Device Password "
7983ff40c12SJohn Marino 			   "configured");
7993ff40c12SJohn Marino 		return NULL;
8003ff40c12SJohn Marino 	}
8013ff40c12SJohn Marino 
8023ff40c12SJohn Marino 	msg = wpabuf_alloc(1000);
8033ff40c12SJohn Marino 	if (msg == NULL)
8043ff40c12SJohn Marino 		return msg;
8053ff40c12SJohn Marino 	len = wpabuf_put(msg, 2);
8063ff40c12SJohn Marino 
8073ff40c12SJohn Marino 	if (wps_build_oob_dev_pw(msg, DEV_PW_NFC_CONNECTION_HANDOVER,
8083ff40c12SJohn Marino 				 nfc_dh_pubkey, NULL, 0) ||
8093ff40c12SJohn Marino 	    wps_build_ssid(msg, ctx) ||
8103ff40c12SJohn Marino 	    wps_build_ap_freq(msg, freq) ||
8113ff40c12SJohn Marino 	    (bssid && wps_build_mac_addr(msg, bssid)) ||
812*a1157835SDaniel Fojt 	    wps_build_wfa_ext(msg, 0, NULL, 0, 0)) {
8133ff40c12SJohn Marino 		wpabuf_free(msg);
8143ff40c12SJohn Marino 		return NULL;
8153ff40c12SJohn Marino 	}
8163ff40c12SJohn Marino 
8173ff40c12SJohn Marino 	WPA_PUT_BE16(len, wpabuf_len(msg) - 2);
8183ff40c12SJohn Marino 
8193ff40c12SJohn Marino 	return msg;
8203ff40c12SJohn Marino }
8213ff40c12SJohn Marino 
8223ff40c12SJohn Marino 
wps_build_nfc_handover_req_p2p(struct wps_context * ctx,struct wpabuf * nfc_dh_pubkey)8233ff40c12SJohn Marino struct wpabuf * wps_build_nfc_handover_req_p2p(struct wps_context *ctx,
8243ff40c12SJohn Marino 					       struct wpabuf *nfc_dh_pubkey)
8253ff40c12SJohn Marino {
8263ff40c12SJohn Marino 	struct wpabuf *msg;
8273ff40c12SJohn Marino 
8283ff40c12SJohn Marino 	if (ctx == NULL)
8293ff40c12SJohn Marino 		return NULL;
8303ff40c12SJohn Marino 
8313ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "WPS: Building attributes for NFC connection "
8323ff40c12SJohn Marino 		   "handover request (P2P)");
8333ff40c12SJohn Marino 
8343ff40c12SJohn Marino 	if (nfc_dh_pubkey == NULL) {
8353ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "WPS: No NFC DH Public Key configured");
8363ff40c12SJohn Marino 		return NULL;
8373ff40c12SJohn Marino 	}
8383ff40c12SJohn Marino 
8393ff40c12SJohn Marino 	msg = wpabuf_alloc(1000);
8403ff40c12SJohn Marino 	if (msg == NULL)
8413ff40c12SJohn Marino 		return msg;
8423ff40c12SJohn Marino 
8433ff40c12SJohn Marino 	if (wps_build_manufacturer(&ctx->dev, msg) ||
8443ff40c12SJohn Marino 	    wps_build_model_name(&ctx->dev, msg) ||
8453ff40c12SJohn Marino 	    wps_build_model_number(&ctx->dev, msg) ||
8463ff40c12SJohn Marino 	    wps_build_oob_dev_pw(msg, DEV_PW_NFC_CONNECTION_HANDOVER,
8473ff40c12SJohn Marino 				 nfc_dh_pubkey, NULL, 0) ||
8483ff40c12SJohn Marino 	    wps_build_rf_bands(&ctx->dev, msg, 0) ||
8493ff40c12SJohn Marino 	    wps_build_serial_number(&ctx->dev, msg) ||
8503ff40c12SJohn Marino 	    wps_build_uuid_e(msg, ctx->uuid) ||
851*a1157835SDaniel Fojt 	    wps_build_wfa_ext(msg, 0, NULL, 0, 0)) {
8523ff40c12SJohn Marino 		wpabuf_free(msg);
8533ff40c12SJohn Marino 		return NULL;
8543ff40c12SJohn Marino 	}
8553ff40c12SJohn Marino 
8563ff40c12SJohn Marino 	return msg;
8573ff40c12SJohn Marino }
8583ff40c12SJohn Marino 
8593ff40c12SJohn Marino 
wps_build_nfc_handover_sel_p2p(struct wps_context * ctx,int nfc_dev_pw_id,struct wpabuf * nfc_dh_pubkey,struct wpabuf * nfc_dev_pw)8603ff40c12SJohn Marino struct wpabuf * wps_build_nfc_handover_sel_p2p(struct wps_context *ctx,
8613ff40c12SJohn Marino 					       int nfc_dev_pw_id,
8623ff40c12SJohn Marino 					       struct wpabuf *nfc_dh_pubkey,
8633ff40c12SJohn Marino 					       struct wpabuf *nfc_dev_pw)
8643ff40c12SJohn Marino {
8653ff40c12SJohn Marino 	struct wpabuf *msg;
8663ff40c12SJohn Marino 	const u8 *dev_pw;
8673ff40c12SJohn Marino 	size_t dev_pw_len;
8683ff40c12SJohn Marino 
8693ff40c12SJohn Marino 	if (ctx == NULL)
8703ff40c12SJohn Marino 		return NULL;
8713ff40c12SJohn Marino 
8723ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "WPS: Building attributes for NFC connection "
8733ff40c12SJohn Marino 		   "handover select (P2P)");
8743ff40c12SJohn Marino 
8753ff40c12SJohn Marino 	if (nfc_dh_pubkey == NULL ||
8763ff40c12SJohn Marino 	    (nfc_dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER &&
8773ff40c12SJohn Marino 	     nfc_dev_pw == NULL)) {
8783ff40c12SJohn Marino 		wpa_printf(MSG_DEBUG, "WPS: No NFC OOB Device Password "
8793ff40c12SJohn Marino 			   "configured");
8803ff40c12SJohn Marino 		return NULL;
8813ff40c12SJohn Marino 	}
8823ff40c12SJohn Marino 
8833ff40c12SJohn Marino 	msg = wpabuf_alloc(1000);
8843ff40c12SJohn Marino 	if (msg == NULL)
8853ff40c12SJohn Marino 		return msg;
8863ff40c12SJohn Marino 
8873ff40c12SJohn Marino 	if (nfc_dev_pw) {
8883ff40c12SJohn Marino 		dev_pw = wpabuf_head(nfc_dev_pw);
8893ff40c12SJohn Marino 		dev_pw_len = wpabuf_len(nfc_dev_pw);
8903ff40c12SJohn Marino 	} else {
8913ff40c12SJohn Marino 		dev_pw = NULL;
8923ff40c12SJohn Marino 		dev_pw_len = 0;
8933ff40c12SJohn Marino 	}
8943ff40c12SJohn Marino 
8953ff40c12SJohn Marino 	if (wps_build_manufacturer(&ctx->dev, msg) ||
8963ff40c12SJohn Marino 	    wps_build_model_name(&ctx->dev, msg) ||
8973ff40c12SJohn Marino 	    wps_build_model_number(&ctx->dev, msg) ||
8983ff40c12SJohn Marino 	    wps_build_oob_dev_pw(msg, nfc_dev_pw_id, nfc_dh_pubkey,
8993ff40c12SJohn Marino 				 dev_pw, dev_pw_len) ||
9003ff40c12SJohn Marino 	    wps_build_rf_bands(&ctx->dev, msg, 0) ||
9013ff40c12SJohn Marino 	    wps_build_serial_number(&ctx->dev, msg) ||
9023ff40c12SJohn Marino 	    wps_build_uuid_e(msg, ctx->uuid) ||
903*a1157835SDaniel Fojt 	    wps_build_wfa_ext(msg, 0, NULL, 0, 0)) {
9043ff40c12SJohn Marino 		wpabuf_free(msg);
9053ff40c12SJohn Marino 		return NULL;
9063ff40c12SJohn Marino 	}
9073ff40c12SJohn Marino 
9083ff40c12SJohn Marino 	return msg;
9093ff40c12SJohn Marino }
9103ff40c12SJohn Marino 
9113ff40c12SJohn Marino #endif /* CONFIG_WPS_NFC */
912