xref: /dflybsd-src/contrib/wpa_supplicant/src/wps/wps.c (revision 3a84a4273475ed07d0ab1c2dfeffdfedef35d9cd)
16d49e1aeSJan Lentfer /*
26d49e1aeSJan Lentfer  * Wi-Fi Protected Setup
33ff40c12SJohn Marino  * Copyright (c) 2007-2009, 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 "crypto/dh_group5.h"
133ff40c12SJohn Marino #include "common/ieee802_11_defs.h"
146d49e1aeSJan Lentfer #include "wps_i.h"
156d49e1aeSJan Lentfer #include "wps_dev_attr.h"
163ff40c12SJohn Marino 
173ff40c12SJohn Marino 
183ff40c12SJohn Marino #ifdef CONFIG_WPS_TESTING
193ff40c12SJohn Marino int wps_version_number = 0x20;
203ff40c12SJohn Marino int wps_testing_dummy_cred = 0;
213ff40c12SJohn Marino int wps_corrupt_pkhash = 0;
22*a1157835SDaniel Fojt int wps_force_auth_types_in_use = 0;
23*a1157835SDaniel Fojt u16 wps_force_auth_types = 0;
24*a1157835SDaniel Fojt int wps_force_encr_types_in_use = 0;
25*a1157835SDaniel Fojt u16 wps_force_encr_types = 0;
263ff40c12SJohn Marino #endif /* CONFIG_WPS_TESTING */
276d49e1aeSJan Lentfer 
286d49e1aeSJan Lentfer 
296d49e1aeSJan Lentfer /**
306d49e1aeSJan Lentfer  * wps_init - Initialize WPS Registration protocol data
316d49e1aeSJan Lentfer  * @cfg: WPS configuration
326d49e1aeSJan Lentfer  * Returns: Pointer to allocated data or %NULL on failure
336d49e1aeSJan Lentfer  *
346d49e1aeSJan Lentfer  * This function is used to initialize WPS data for a registration protocol
356d49e1aeSJan Lentfer  * instance (i.e., each run of registration protocol as a Registrar of
366d49e1aeSJan Lentfer  * Enrollee. The caller is responsible for freeing this data after the
376d49e1aeSJan Lentfer  * registration run has been completed by calling wps_deinit().
386d49e1aeSJan Lentfer  */
wps_init(const struct wps_config * cfg)396d49e1aeSJan Lentfer struct wps_data * wps_init(const struct wps_config *cfg)
406d49e1aeSJan Lentfer {
416d49e1aeSJan Lentfer 	struct wps_data *data = os_zalloc(sizeof(*data));
426d49e1aeSJan Lentfer 	if (data == NULL)
436d49e1aeSJan Lentfer 		return NULL;
446d49e1aeSJan Lentfer 	data->wps = cfg->wps;
456d49e1aeSJan Lentfer 	data->registrar = cfg->registrar;
466d49e1aeSJan Lentfer 	if (cfg->registrar) {
476d49e1aeSJan Lentfer 		os_memcpy(data->uuid_r, cfg->wps->uuid, WPS_UUID_LEN);
486d49e1aeSJan Lentfer 	} else {
496d49e1aeSJan Lentfer 		os_memcpy(data->mac_addr_e, cfg->wps->dev.mac_addr, ETH_ALEN);
506d49e1aeSJan Lentfer 		os_memcpy(data->uuid_e, cfg->wps->uuid, WPS_UUID_LEN);
516d49e1aeSJan Lentfer 	}
526d49e1aeSJan Lentfer 	if (cfg->pin) {
533ff40c12SJohn Marino 		data->dev_pw_id = cfg->dev_pw_id;
54*a1157835SDaniel Fojt 		data->dev_password = os_memdup(cfg->pin, cfg->pin_len);
556d49e1aeSJan Lentfer 		if (data->dev_password == NULL) {
566d49e1aeSJan Lentfer 			os_free(data);
576d49e1aeSJan Lentfer 			return NULL;
586d49e1aeSJan Lentfer 		}
596d49e1aeSJan Lentfer 		data->dev_password_len = cfg->pin_len;
603ff40c12SJohn Marino 		wpa_hexdump_key(MSG_DEBUG, "WPS: AP PIN dev_password",
613ff40c12SJohn Marino 				data->dev_password, data->dev_password_len);
626d49e1aeSJan Lentfer 	}
636d49e1aeSJan Lentfer 
643ff40c12SJohn Marino #ifdef CONFIG_WPS_NFC
653ff40c12SJohn Marino 	if (cfg->pin == NULL &&
663ff40c12SJohn Marino 	    cfg->dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER)
673ff40c12SJohn Marino 		data->dev_pw_id = cfg->dev_pw_id;
683ff40c12SJohn Marino 
693ff40c12SJohn Marino 	if (cfg->wps->ap && !cfg->registrar && cfg->wps->ap_nfc_dev_pw_id) {
703ff40c12SJohn Marino 		/* Keep AP PIN as alternative Device Password */
713ff40c12SJohn Marino 		data->alt_dev_pw_id = data->dev_pw_id;
723ff40c12SJohn Marino 		data->alt_dev_password = data->dev_password;
733ff40c12SJohn Marino 		data->alt_dev_password_len = data->dev_password_len;
743ff40c12SJohn Marino 
753ff40c12SJohn Marino 		data->dev_pw_id = cfg->wps->ap_nfc_dev_pw_id;
763ff40c12SJohn Marino 		data->dev_password =
77*a1157835SDaniel Fojt 			os_memdup(wpabuf_head(cfg->wps->ap_nfc_dev_pw),
78*a1157835SDaniel Fojt 				  wpabuf_len(cfg->wps->ap_nfc_dev_pw));
793ff40c12SJohn Marino 		if (data->dev_password == NULL) {
803ff40c12SJohn Marino 			os_free(data);
813ff40c12SJohn Marino 			return NULL;
823ff40c12SJohn Marino 		}
833ff40c12SJohn Marino 		data->dev_password_len = wpabuf_len(cfg->wps->ap_nfc_dev_pw);
843ff40c12SJohn Marino 		wpa_hexdump_key(MSG_DEBUG, "WPS: NFC dev_password",
853ff40c12SJohn Marino 			    data->dev_password, data->dev_password_len);
863ff40c12SJohn Marino 	}
873ff40c12SJohn Marino #endif /* CONFIG_WPS_NFC */
883ff40c12SJohn Marino 
896d49e1aeSJan Lentfer 	data->pbc = cfg->pbc;
906d49e1aeSJan Lentfer 	if (cfg->pbc) {
916d49e1aeSJan Lentfer 		/* Use special PIN '00000000' for PBC */
926d49e1aeSJan Lentfer 		data->dev_pw_id = DEV_PW_PUSHBUTTON;
93*a1157835SDaniel Fojt 		bin_clear_free(data->dev_password, data->dev_password_len);
943ff40c12SJohn Marino 		data->dev_password = (u8 *) os_strdup("00000000");
956d49e1aeSJan Lentfer 		if (data->dev_password == NULL) {
966d49e1aeSJan Lentfer 			os_free(data);
976d49e1aeSJan Lentfer 			return NULL;
986d49e1aeSJan Lentfer 		}
996d49e1aeSJan Lentfer 		data->dev_password_len = 8;
1006d49e1aeSJan Lentfer 	}
1016d49e1aeSJan Lentfer 
1026d49e1aeSJan Lentfer 	data->state = data->registrar ? RECV_M1 : SEND_M1;
1036d49e1aeSJan Lentfer 
1046d49e1aeSJan Lentfer 	if (cfg->assoc_wps_ie) {
1056d49e1aeSJan Lentfer 		struct wps_parse_attr attr;
1066d49e1aeSJan Lentfer 		wpa_hexdump_buf(MSG_DEBUG, "WPS: WPS IE from (Re)AssocReq",
1076d49e1aeSJan Lentfer 				cfg->assoc_wps_ie);
1086d49e1aeSJan Lentfer 		if (wps_parse_msg(cfg->assoc_wps_ie, &attr) < 0) {
1096d49e1aeSJan Lentfer 			wpa_printf(MSG_DEBUG, "WPS: Failed to parse WPS IE "
1106d49e1aeSJan Lentfer 				   "from (Re)AssocReq");
1116d49e1aeSJan Lentfer 		} else if (attr.request_type == NULL) {
1126d49e1aeSJan Lentfer 			wpa_printf(MSG_DEBUG, "WPS: No Request Type attribute "
1136d49e1aeSJan Lentfer 				   "in (Re)AssocReq WPS IE");
1146d49e1aeSJan Lentfer 		} else {
1156d49e1aeSJan Lentfer 			wpa_printf(MSG_DEBUG, "WPS: Request Type (from WPS IE "
1166d49e1aeSJan Lentfer 				   "in (Re)AssocReq WPS IE): %d",
1176d49e1aeSJan Lentfer 				   *attr.request_type);
1186d49e1aeSJan Lentfer 			data->request_type = *attr.request_type;
1196d49e1aeSJan Lentfer 		}
1206d49e1aeSJan Lentfer 	}
1216d49e1aeSJan Lentfer 
1223ff40c12SJohn Marino 	if (cfg->new_ap_settings) {
1233ff40c12SJohn Marino 		data->new_ap_settings =
124*a1157835SDaniel Fojt 			os_memdup(cfg->new_ap_settings,
125*a1157835SDaniel Fojt 				  sizeof(*data->new_ap_settings));
1263ff40c12SJohn Marino 		if (data->new_ap_settings == NULL) {
127*a1157835SDaniel Fojt 			bin_clear_free(data->dev_password,
128*a1157835SDaniel Fojt 				       data->dev_password_len);
1293ff40c12SJohn Marino 			os_free(data);
1303ff40c12SJohn Marino 			return NULL;
1313ff40c12SJohn Marino 		}
1323ff40c12SJohn Marino 	}
1333ff40c12SJohn Marino 
1343ff40c12SJohn Marino 	if (cfg->peer_addr)
1353ff40c12SJohn Marino 		os_memcpy(data->peer_dev.mac_addr, cfg->peer_addr, ETH_ALEN);
1363ff40c12SJohn Marino 	if (cfg->p2p_dev_addr)
1373ff40c12SJohn Marino 		os_memcpy(data->p2p_dev_addr, cfg->p2p_dev_addr, ETH_ALEN);
1383ff40c12SJohn Marino 
1393ff40c12SJohn Marino 	data->use_psk_key = cfg->use_psk_key;
1403ff40c12SJohn Marino 	data->pbc_in_m1 = cfg->pbc_in_m1;
1413ff40c12SJohn Marino 
1423ff40c12SJohn Marino 	if (cfg->peer_pubkey_hash) {
1433ff40c12SJohn Marino 		os_memcpy(data->peer_pubkey_hash, cfg->peer_pubkey_hash,
1443ff40c12SJohn Marino 			  WPS_OOB_PUBKEY_HASH_LEN);
1453ff40c12SJohn Marino 		data->peer_pubkey_hash_set = 1;
1463ff40c12SJohn Marino 	}
1473ff40c12SJohn Marino 
148*a1157835SDaniel Fojt 	data->multi_ap_backhaul_sta = cfg->multi_ap_backhaul_sta;
149*a1157835SDaniel Fojt 
1506d49e1aeSJan Lentfer 	return data;
1516d49e1aeSJan Lentfer }
1526d49e1aeSJan Lentfer 
1536d49e1aeSJan Lentfer 
1546d49e1aeSJan Lentfer /**
1556d49e1aeSJan Lentfer  * wps_deinit - Deinitialize WPS Registration protocol data
1566d49e1aeSJan Lentfer  * @data: WPS Registration protocol data from wps_init()
1576d49e1aeSJan Lentfer  */
wps_deinit(struct wps_data * data)1586d49e1aeSJan Lentfer void wps_deinit(struct wps_data *data)
1596d49e1aeSJan Lentfer {
1603ff40c12SJohn Marino #ifdef CONFIG_WPS_NFC
1613ff40c12SJohn Marino 	if (data->registrar && data->nfc_pw_token)
1623ff40c12SJohn Marino 		wps_registrar_remove_nfc_pw_token(data->wps->registrar,
1633ff40c12SJohn Marino 						  data->nfc_pw_token);
1643ff40c12SJohn Marino #endif /* CONFIG_WPS_NFC */
1653ff40c12SJohn Marino 
1666d49e1aeSJan Lentfer 	if (data->wps_pin_revealed) {
1676d49e1aeSJan Lentfer 		wpa_printf(MSG_DEBUG, "WPS: Full PIN information revealed and "
1686d49e1aeSJan Lentfer 			   "negotiation failed");
1696d49e1aeSJan Lentfer 		if (data->registrar)
1706d49e1aeSJan Lentfer 			wps_registrar_invalidate_pin(data->wps->registrar,
1716d49e1aeSJan Lentfer 						     data->uuid_e);
1726d49e1aeSJan Lentfer 	} else if (data->registrar)
1736d49e1aeSJan Lentfer 		wps_registrar_unlock_pin(data->wps->registrar, data->uuid_e);
1746d49e1aeSJan Lentfer 
175*a1157835SDaniel Fojt 	wpabuf_clear_free(data->dh_privkey);
1766d49e1aeSJan Lentfer 	wpabuf_free(data->dh_pubkey_e);
1776d49e1aeSJan Lentfer 	wpabuf_free(data->dh_pubkey_r);
1786d49e1aeSJan Lentfer 	wpabuf_free(data->last_msg);
179*a1157835SDaniel Fojt 	bin_clear_free(data->dev_password, data->dev_password_len);
180*a1157835SDaniel Fojt 	bin_clear_free(data->alt_dev_password, data->alt_dev_password_len);
181*a1157835SDaniel Fojt 	bin_clear_free(data->new_psk, data->new_psk_len);
1826d49e1aeSJan Lentfer 	wps_device_data_free(&data->peer_dev);
183*a1157835SDaniel Fojt 	bin_clear_free(data->new_ap_settings, sizeof(*data->new_ap_settings));
1843ff40c12SJohn Marino 	dh5_free(data->dh_ctx);
1856d49e1aeSJan Lentfer 	os_free(data);
1866d49e1aeSJan Lentfer }
1876d49e1aeSJan Lentfer 
1886d49e1aeSJan Lentfer 
1896d49e1aeSJan Lentfer /**
1906d49e1aeSJan Lentfer  * wps_process_msg - Process a WPS message
1916d49e1aeSJan Lentfer  * @wps: WPS Registration protocol data from wps_init()
1926d49e1aeSJan Lentfer  * @op_code: Message OP Code
1936d49e1aeSJan Lentfer  * @msg: Message data
1946d49e1aeSJan Lentfer  * Returns: Processing result
1956d49e1aeSJan Lentfer  *
1966d49e1aeSJan Lentfer  * This function is used to process WPS messages with OP Codes WSC_ACK,
1976d49e1aeSJan Lentfer  * WSC_NACK, WSC_MSG, and WSC_Done. The caller (e.g., EAP server/peer) is
1986d49e1aeSJan Lentfer  * responsible for reassembling the messages before calling this function.
1996d49e1aeSJan Lentfer  * Response to this message is built by calling wps_get_msg().
2006d49e1aeSJan Lentfer  */
wps_process_msg(struct wps_data * wps,enum wsc_op_code op_code,const struct wpabuf * msg)2016d49e1aeSJan Lentfer enum wps_process_res wps_process_msg(struct wps_data *wps,
2026d49e1aeSJan Lentfer 				     enum wsc_op_code op_code,
2036d49e1aeSJan Lentfer 				     const struct wpabuf *msg)
2046d49e1aeSJan Lentfer {
2056d49e1aeSJan Lentfer 	if (wps->registrar)
2066d49e1aeSJan Lentfer 		return wps_registrar_process_msg(wps, op_code, msg);
2076d49e1aeSJan Lentfer 	else
2086d49e1aeSJan Lentfer 		return wps_enrollee_process_msg(wps, op_code, msg);
2096d49e1aeSJan Lentfer }
2106d49e1aeSJan Lentfer 
2116d49e1aeSJan Lentfer 
2126d49e1aeSJan Lentfer /**
2136d49e1aeSJan Lentfer  * wps_get_msg - Build a WPS message
2146d49e1aeSJan Lentfer  * @wps: WPS Registration protocol data from wps_init()
2156d49e1aeSJan Lentfer  * @op_code: Buffer for returning message OP Code
2166d49e1aeSJan Lentfer  * Returns: The generated WPS message or %NULL on failure
2176d49e1aeSJan Lentfer  *
2186d49e1aeSJan Lentfer  * This function is used to build a response to a message processed by calling
2196d49e1aeSJan Lentfer  * wps_process_msg(). The caller is responsible for freeing the buffer.
2206d49e1aeSJan Lentfer  */
wps_get_msg(struct wps_data * wps,enum wsc_op_code * op_code)2216d49e1aeSJan Lentfer struct wpabuf * wps_get_msg(struct wps_data *wps, enum wsc_op_code *op_code)
2226d49e1aeSJan Lentfer {
2236d49e1aeSJan Lentfer 	if (wps->registrar)
2246d49e1aeSJan Lentfer 		return wps_registrar_get_msg(wps, op_code);
2256d49e1aeSJan Lentfer 	else
2266d49e1aeSJan Lentfer 		return wps_enrollee_get_msg(wps, op_code);
2276d49e1aeSJan Lentfer }
2286d49e1aeSJan Lentfer 
2296d49e1aeSJan Lentfer 
2306d49e1aeSJan Lentfer /**
2316d49e1aeSJan Lentfer  * wps_is_selected_pbc_registrar - Check whether WPS IE indicates active PBC
2326d49e1aeSJan Lentfer  * @msg: WPS IE contents from Beacon or Probe Response frame
2336d49e1aeSJan Lentfer  * Returns: 1 if PBC Registrar is active, 0 if not
2346d49e1aeSJan Lentfer  */
wps_is_selected_pbc_registrar(const struct wpabuf * msg)2356d49e1aeSJan Lentfer int wps_is_selected_pbc_registrar(const struct wpabuf *msg)
2366d49e1aeSJan Lentfer {
2376d49e1aeSJan Lentfer 	struct wps_parse_attr attr;
2386d49e1aeSJan Lentfer 
2396d49e1aeSJan Lentfer 	/*
2406d49e1aeSJan Lentfer 	 * In theory, this could also verify that attr.sel_reg_config_methods
2416d49e1aeSJan Lentfer 	 * includes WPS_CONFIG_PUSHBUTTON, but some deployed AP implementations
2426d49e1aeSJan Lentfer 	 * do not set Selected Registrar Config Methods attribute properly, so
2436d49e1aeSJan Lentfer 	 * it is safer to just use Device Password ID here.
2446d49e1aeSJan Lentfer 	 */
2456d49e1aeSJan Lentfer 
2466d49e1aeSJan Lentfer 	if (wps_parse_msg(msg, &attr) < 0 ||
2476d49e1aeSJan Lentfer 	    !attr.selected_registrar || *attr.selected_registrar == 0 ||
2486d49e1aeSJan Lentfer 	    !attr.dev_password_id ||
2496d49e1aeSJan Lentfer 	    WPA_GET_BE16(attr.dev_password_id) != DEV_PW_PUSHBUTTON)
2506d49e1aeSJan Lentfer 		return 0;
2516d49e1aeSJan Lentfer 
2523ff40c12SJohn Marino #ifdef CONFIG_WPS_STRICT
2533ff40c12SJohn Marino 	if (!attr.sel_reg_config_methods ||
2543ff40c12SJohn Marino 	    !(WPA_GET_BE16(attr.sel_reg_config_methods) &
2553ff40c12SJohn Marino 	      WPS_CONFIG_PUSHBUTTON))
2563ff40c12SJohn Marino 		return 0;
2573ff40c12SJohn Marino #endif /* CONFIG_WPS_STRICT */
2583ff40c12SJohn Marino 
2593ff40c12SJohn Marino 	return 1;
2603ff40c12SJohn Marino }
2613ff40c12SJohn Marino 
2623ff40c12SJohn Marino 
is_selected_pin_registrar(struct wps_parse_attr * attr)2633ff40c12SJohn Marino static int is_selected_pin_registrar(struct wps_parse_attr *attr)
2643ff40c12SJohn Marino {
2653ff40c12SJohn Marino 	/*
2663ff40c12SJohn Marino 	 * In theory, this could also verify that attr.sel_reg_config_methods
2673ff40c12SJohn Marino 	 * includes WPS_CONFIG_LABEL, WPS_CONFIG_DISPLAY, or WPS_CONFIG_KEYPAD,
2683ff40c12SJohn Marino 	 * but some deployed AP implementations do not set Selected Registrar
2693ff40c12SJohn Marino 	 * Config Methods attribute properly, so it is safer to just use
2703ff40c12SJohn Marino 	 * Device Password ID here.
2713ff40c12SJohn Marino 	 */
2723ff40c12SJohn Marino 
2733ff40c12SJohn Marino 	if (!attr->selected_registrar || *attr->selected_registrar == 0)
2743ff40c12SJohn Marino 		return 0;
2753ff40c12SJohn Marino 
2763ff40c12SJohn Marino 	if (attr->dev_password_id != NULL &&
2773ff40c12SJohn Marino 	    WPA_GET_BE16(attr->dev_password_id) == DEV_PW_PUSHBUTTON)
2783ff40c12SJohn Marino 		return 0;
2793ff40c12SJohn Marino 
2803ff40c12SJohn Marino #ifdef CONFIG_WPS_STRICT
2813ff40c12SJohn Marino 	if (!attr->sel_reg_config_methods ||
2823ff40c12SJohn Marino 	    !(WPA_GET_BE16(attr->sel_reg_config_methods) &
2833ff40c12SJohn Marino 	      (WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD)))
2843ff40c12SJohn Marino 		return 0;
2853ff40c12SJohn Marino #endif /* CONFIG_WPS_STRICT */
2863ff40c12SJohn Marino 
2876d49e1aeSJan Lentfer 	return 1;
2886d49e1aeSJan Lentfer }
2896d49e1aeSJan Lentfer 
2906d49e1aeSJan Lentfer 
2916d49e1aeSJan Lentfer /**
2926d49e1aeSJan Lentfer  * wps_is_selected_pin_registrar - Check whether WPS IE indicates active PIN
2936d49e1aeSJan Lentfer  * @msg: WPS IE contents from Beacon or Probe Response frame
2946d49e1aeSJan Lentfer  * Returns: 1 if PIN Registrar is active, 0 if not
2956d49e1aeSJan Lentfer  */
wps_is_selected_pin_registrar(const struct wpabuf * msg)2966d49e1aeSJan Lentfer int wps_is_selected_pin_registrar(const struct wpabuf *msg)
2976d49e1aeSJan Lentfer {
2986d49e1aeSJan Lentfer 	struct wps_parse_attr attr;
2996d49e1aeSJan Lentfer 
3003ff40c12SJohn Marino 	if (wps_parse_msg(msg, &attr) < 0)
3013ff40c12SJohn Marino 		return 0;
3023ff40c12SJohn Marino 
3033ff40c12SJohn Marino 	return is_selected_pin_registrar(&attr);
3043ff40c12SJohn Marino }
3053ff40c12SJohn Marino 
3063ff40c12SJohn Marino 
3073ff40c12SJohn Marino /**
3083ff40c12SJohn Marino  * wps_is_addr_authorized - Check whether WPS IE authorizes MAC address
3093ff40c12SJohn Marino  * @msg: WPS IE contents from Beacon or Probe Response frame
3103ff40c12SJohn Marino  * @addr: MAC address to search for
3113ff40c12SJohn Marino  * @ver1_compat: Whether to use version 1 compatibility mode
3123ff40c12SJohn Marino  * Returns: 2 if the specified address is explicit authorized, 1 if address is
3133ff40c12SJohn Marino  * authorized (broadcast), 0 if not
3146d49e1aeSJan Lentfer  */
wps_is_addr_authorized(const struct wpabuf * msg,const u8 * addr,int ver1_compat)3153ff40c12SJohn Marino int wps_is_addr_authorized(const struct wpabuf *msg, const u8 *addr,
3163ff40c12SJohn Marino 			   int ver1_compat)
3173ff40c12SJohn Marino {
3183ff40c12SJohn Marino 	struct wps_parse_attr attr;
3193ff40c12SJohn Marino 	unsigned int i;
3203ff40c12SJohn Marino 	const u8 *pos;
3213ff40c12SJohn Marino 	const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
3226d49e1aeSJan Lentfer 
3236d49e1aeSJan Lentfer 	if (wps_parse_msg(msg, &attr) < 0)
3246d49e1aeSJan Lentfer 		return 0;
3256d49e1aeSJan Lentfer 
3263ff40c12SJohn Marino 	if (!attr.version2 && ver1_compat) {
3273ff40c12SJohn Marino 		/*
3283ff40c12SJohn Marino 		 * Version 1.0 AP - AuthorizedMACs not used, so revert back to
3293ff40c12SJohn Marino 		 * old mechanism of using SelectedRegistrar.
3303ff40c12SJohn Marino 		 */
3313ff40c12SJohn Marino 		return is_selected_pin_registrar(&attr);
3323ff40c12SJohn Marino 	}
3333ff40c12SJohn Marino 
3343ff40c12SJohn Marino 	if (!attr.authorized_macs)
3356d49e1aeSJan Lentfer 		return 0;
3366d49e1aeSJan Lentfer 
3373ff40c12SJohn Marino 	pos = attr.authorized_macs;
3383ff40c12SJohn Marino 	for (i = 0; i < attr.authorized_macs_len / ETH_ALEN; i++) {
3393ff40c12SJohn Marino 		if (os_memcmp(pos, addr, ETH_ALEN) == 0)
3403ff40c12SJohn Marino 			return 2;
3413ff40c12SJohn Marino 		if (os_memcmp(pos, bcast, ETH_ALEN) == 0)
3426d49e1aeSJan Lentfer 			return 1;
3433ff40c12SJohn Marino 		pos += ETH_ALEN;
3443ff40c12SJohn Marino 	}
3453ff40c12SJohn Marino 
3463ff40c12SJohn Marino 	return 0;
3473ff40c12SJohn Marino }
3483ff40c12SJohn Marino 
3493ff40c12SJohn Marino 
3503ff40c12SJohn Marino /**
3513ff40c12SJohn Marino  * wps_ap_priority_compar - Prioritize WPS IE from two APs
3523ff40c12SJohn Marino  * @wps_a: WPS IE contents from Beacon or Probe Response frame
3533ff40c12SJohn Marino  * @wps_b: WPS IE contents from Beacon or Probe Response frame
3543ff40c12SJohn Marino  * Returns: 1 if wps_b is considered more likely selection for WPS
3553ff40c12SJohn Marino  * provisioning, -1 if wps_a is considered more like, or 0 if no preference
3563ff40c12SJohn Marino  */
wps_ap_priority_compar(const struct wpabuf * wps_a,const struct wpabuf * wps_b)3573ff40c12SJohn Marino int wps_ap_priority_compar(const struct wpabuf *wps_a,
3583ff40c12SJohn Marino 			   const struct wpabuf *wps_b)
3593ff40c12SJohn Marino {
360*a1157835SDaniel Fojt 	struct wps_parse_attr attr;
3613ff40c12SJohn Marino 	int sel_a, sel_b;
3623ff40c12SJohn Marino 
363*a1157835SDaniel Fojt 	if (wps_a == NULL || wps_parse_msg(wps_a, &attr) < 0)
3643ff40c12SJohn Marino 		return 1;
365*a1157835SDaniel Fojt 	sel_a = attr.selected_registrar && *attr.selected_registrar != 0;
3663ff40c12SJohn Marino 
367*a1157835SDaniel Fojt 	if (wps_b == NULL || wps_parse_msg(wps_b, &attr) < 0)
368*a1157835SDaniel Fojt 		return -1;
369*a1157835SDaniel Fojt 	sel_b = attr.selected_registrar && *attr.selected_registrar != 0;
3703ff40c12SJohn Marino 
3713ff40c12SJohn Marino 	if (sel_a && !sel_b)
3723ff40c12SJohn Marino 		return -1;
3733ff40c12SJohn Marino 	if (!sel_a && sel_b)
3743ff40c12SJohn Marino 		return 1;
3753ff40c12SJohn Marino 
3763ff40c12SJohn Marino 	return 0;
3776d49e1aeSJan Lentfer }
3786d49e1aeSJan Lentfer 
3796d49e1aeSJan Lentfer 
3806d49e1aeSJan Lentfer /**
3816d49e1aeSJan Lentfer  * wps_get_uuid_e - Get UUID-E from WPS IE
3826d49e1aeSJan Lentfer  * @msg: WPS IE contents from Beacon or Probe Response frame
3836d49e1aeSJan Lentfer  * Returns: Pointer to UUID-E or %NULL if not included
3846d49e1aeSJan Lentfer  *
3856d49e1aeSJan Lentfer  * The returned pointer is to the msg contents and it remains valid only as
3866d49e1aeSJan Lentfer  * long as the msg buffer is valid.
3876d49e1aeSJan Lentfer  */
wps_get_uuid_e(const struct wpabuf * msg)3886d49e1aeSJan Lentfer const u8 * wps_get_uuid_e(const struct wpabuf *msg)
3896d49e1aeSJan Lentfer {
3906d49e1aeSJan Lentfer 	struct wps_parse_attr attr;
3916d49e1aeSJan Lentfer 
3926d49e1aeSJan Lentfer 	if (wps_parse_msg(msg, &attr) < 0)
3936d49e1aeSJan Lentfer 		return NULL;
3946d49e1aeSJan Lentfer 	return attr.uuid_e;
3956d49e1aeSJan Lentfer }
3966d49e1aeSJan Lentfer 
3976d49e1aeSJan Lentfer 
3986d49e1aeSJan Lentfer /**
3993ff40c12SJohn Marino  * wps_is_20 - Check whether WPS attributes claim support for WPS 2.0
4003ff40c12SJohn Marino  */
wps_is_20(const struct wpabuf * msg)4013ff40c12SJohn Marino int wps_is_20(const struct wpabuf *msg)
4023ff40c12SJohn Marino {
4033ff40c12SJohn Marino 	struct wps_parse_attr attr;
4043ff40c12SJohn Marino 
4053ff40c12SJohn Marino 	if (msg == NULL || wps_parse_msg(msg, &attr) < 0)
4063ff40c12SJohn Marino 		return 0;
4073ff40c12SJohn Marino 	return attr.version2 != NULL;
4083ff40c12SJohn Marino }
4093ff40c12SJohn Marino 
4103ff40c12SJohn Marino 
4113ff40c12SJohn Marino /**
4126d49e1aeSJan Lentfer  * wps_build_assoc_req_ie - Build WPS IE for (Re)Association Request
4136d49e1aeSJan Lentfer  * @req_type: Value for Request Type attribute
4146d49e1aeSJan Lentfer  * Returns: WPS IE or %NULL on failure
4156d49e1aeSJan Lentfer  *
4166d49e1aeSJan Lentfer  * The caller is responsible for freeing the buffer.
4176d49e1aeSJan Lentfer  */
wps_build_assoc_req_ie(enum wps_request_type req_type)4186d49e1aeSJan Lentfer struct wpabuf * wps_build_assoc_req_ie(enum wps_request_type req_type)
4196d49e1aeSJan Lentfer {
4206d49e1aeSJan Lentfer 	struct wpabuf *ie;
4216d49e1aeSJan Lentfer 	u8 *len;
4226d49e1aeSJan Lentfer 
4236d49e1aeSJan Lentfer 	wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
4246d49e1aeSJan Lentfer 		   "Request");
4256d49e1aeSJan Lentfer 	ie = wpabuf_alloc(100);
4266d49e1aeSJan Lentfer 	if (ie == NULL)
4276d49e1aeSJan Lentfer 		return NULL;
4286d49e1aeSJan Lentfer 
4296d49e1aeSJan Lentfer 	wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
4306d49e1aeSJan Lentfer 	len = wpabuf_put(ie, 1);
4316d49e1aeSJan Lentfer 	wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
4326d49e1aeSJan Lentfer 
4336d49e1aeSJan Lentfer 	if (wps_build_version(ie) ||
4343ff40c12SJohn Marino 	    wps_build_req_type(ie, req_type) ||
435*a1157835SDaniel Fojt 	    wps_build_wfa_ext(ie, 0, NULL, 0, 0)) {
4363ff40c12SJohn Marino 		wpabuf_free(ie);
4373ff40c12SJohn Marino 		return NULL;
4383ff40c12SJohn Marino 	}
4393ff40c12SJohn Marino 
4403ff40c12SJohn Marino 	*len = wpabuf_len(ie) - 2;
4413ff40c12SJohn Marino 
4423ff40c12SJohn Marino 	return ie;
4433ff40c12SJohn Marino }
4443ff40c12SJohn Marino 
4453ff40c12SJohn Marino 
4463ff40c12SJohn Marino /**
4473ff40c12SJohn Marino  * wps_build_assoc_resp_ie - Build WPS IE for (Re)Association Response
4483ff40c12SJohn Marino  * Returns: WPS IE or %NULL on failure
4493ff40c12SJohn Marino  *
4503ff40c12SJohn Marino  * The caller is responsible for freeing the buffer.
4513ff40c12SJohn Marino  */
wps_build_assoc_resp_ie(void)4523ff40c12SJohn Marino struct wpabuf * wps_build_assoc_resp_ie(void)
4533ff40c12SJohn Marino {
4543ff40c12SJohn Marino 	struct wpabuf *ie;
4553ff40c12SJohn Marino 	u8 *len;
4563ff40c12SJohn Marino 
4573ff40c12SJohn Marino 	wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
4583ff40c12SJohn Marino 		   "Response");
4593ff40c12SJohn Marino 	ie = wpabuf_alloc(100);
4603ff40c12SJohn Marino 	if (ie == NULL)
4613ff40c12SJohn Marino 		return NULL;
4623ff40c12SJohn Marino 
4633ff40c12SJohn Marino 	wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
4643ff40c12SJohn Marino 	len = wpabuf_put(ie, 1);
4653ff40c12SJohn Marino 	wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
4663ff40c12SJohn Marino 
4673ff40c12SJohn Marino 	if (wps_build_version(ie) ||
4683ff40c12SJohn Marino 	    wps_build_resp_type(ie, WPS_RESP_AP) ||
469*a1157835SDaniel Fojt 	    wps_build_wfa_ext(ie, 0, NULL, 0, 0)) {
4706d49e1aeSJan Lentfer 		wpabuf_free(ie);
4716d49e1aeSJan Lentfer 		return NULL;
4726d49e1aeSJan Lentfer 	}
4736d49e1aeSJan Lentfer 
4746d49e1aeSJan Lentfer 	*len = wpabuf_len(ie) - 2;
4756d49e1aeSJan Lentfer 
4766d49e1aeSJan Lentfer 	return ie;
4776d49e1aeSJan Lentfer }
4786d49e1aeSJan Lentfer 
4796d49e1aeSJan Lentfer 
4806d49e1aeSJan Lentfer /**
4816d49e1aeSJan Lentfer  * wps_build_probe_req_ie - Build WPS IE for Probe Request
4823ff40c12SJohn Marino  * @pw_id: Password ID (DEV_PW_PUSHBUTTON for active PBC and DEV_PW_DEFAULT for
4833ff40c12SJohn Marino  * most other use cases)
4846d49e1aeSJan Lentfer  * @dev: Device attributes
4856d49e1aeSJan Lentfer  * @uuid: Own UUID
4866d49e1aeSJan Lentfer  * @req_type: Value for Request Type attribute
4873ff40c12SJohn Marino  * @num_req_dev_types: Number of requested device types
4883ff40c12SJohn Marino  * @req_dev_types: Requested device types (8 * num_req_dev_types octets) or
4893ff40c12SJohn Marino  *	%NULL if none
4906d49e1aeSJan Lentfer  * Returns: WPS IE or %NULL on failure
4916d49e1aeSJan Lentfer  *
4926d49e1aeSJan Lentfer  * The caller is responsible for freeing the buffer.
4936d49e1aeSJan Lentfer  */
wps_build_probe_req_ie(u16 pw_id,struct wps_device_data * dev,const u8 * uuid,enum wps_request_type req_type,unsigned int num_req_dev_types,const u8 * req_dev_types)4943ff40c12SJohn Marino struct wpabuf * wps_build_probe_req_ie(u16 pw_id, struct wps_device_data *dev,
4956d49e1aeSJan Lentfer 				       const u8 *uuid,
4963ff40c12SJohn Marino 				       enum wps_request_type req_type,
4973ff40c12SJohn Marino 				       unsigned int num_req_dev_types,
4983ff40c12SJohn Marino 				       const u8 *req_dev_types)
4996d49e1aeSJan Lentfer {
5006d49e1aeSJan Lentfer 	struct wpabuf *ie;
5016d49e1aeSJan Lentfer 
5026d49e1aeSJan Lentfer 	wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for Probe Request");
5036d49e1aeSJan Lentfer 
5043ff40c12SJohn Marino 	ie = wpabuf_alloc(500);
5056d49e1aeSJan Lentfer 	if (ie == NULL)
5066d49e1aeSJan Lentfer 		return NULL;
5076d49e1aeSJan Lentfer 
5086d49e1aeSJan Lentfer 	if (wps_build_version(ie) ||
5096d49e1aeSJan Lentfer 	    wps_build_req_type(ie, req_type) ||
5103ff40c12SJohn Marino 	    wps_build_config_methods(ie, dev->config_methods) ||
5116d49e1aeSJan Lentfer 	    wps_build_uuid_e(ie, uuid) ||
5126d49e1aeSJan Lentfer 	    wps_build_primary_dev_type(dev, ie) ||
5133ff40c12SJohn Marino 	    wps_build_rf_bands(dev, ie, 0) ||
5146d49e1aeSJan Lentfer 	    wps_build_assoc_state(NULL, ie) ||
5156d49e1aeSJan Lentfer 	    wps_build_config_error(ie, WPS_CFG_NO_ERROR) ||
5163ff40c12SJohn Marino 	    wps_build_dev_password_id(ie, pw_id) ||
5173ff40c12SJohn Marino 	    wps_build_manufacturer(dev, ie) ||
5183ff40c12SJohn Marino 	    wps_build_model_name(dev, ie) ||
5193ff40c12SJohn Marino 	    wps_build_model_number(dev, ie) ||
5203ff40c12SJohn Marino 	    wps_build_dev_name(dev, ie) ||
521*a1157835SDaniel Fojt 	    wps_build_wfa_ext(ie, req_type == WPS_REQ_ENROLLEE, NULL, 0, 0) ||
5223ff40c12SJohn Marino 	    wps_build_req_dev_type(dev, ie, num_req_dev_types, req_dev_types)
5233ff40c12SJohn Marino 	    ||
5243ff40c12SJohn Marino 	    wps_build_secondary_dev_type(dev, ie)
5253ff40c12SJohn Marino 		) {
5266d49e1aeSJan Lentfer 		wpabuf_free(ie);
5276d49e1aeSJan Lentfer 		return NULL;
5286d49e1aeSJan Lentfer 	}
5296d49e1aeSJan Lentfer 
5303ff40c12SJohn Marino 	return wps_ie_encapsulate(ie);
5316d49e1aeSJan Lentfer }
5326d49e1aeSJan Lentfer 
5336d49e1aeSJan Lentfer 
wps_free_pending_msgs(struct upnp_pending_message * msgs)5346d49e1aeSJan Lentfer void wps_free_pending_msgs(struct upnp_pending_message *msgs)
5356d49e1aeSJan Lentfer {
5366d49e1aeSJan Lentfer 	struct upnp_pending_message *p, *prev;
5376d49e1aeSJan Lentfer 	p = msgs;
5386d49e1aeSJan Lentfer 	while (p) {
5396d49e1aeSJan Lentfer 		prev = p;
5406d49e1aeSJan Lentfer 		p = p->next;
5416d49e1aeSJan Lentfer 		wpabuf_free(prev->msg);
5426d49e1aeSJan Lentfer 		os_free(prev);
5436d49e1aeSJan Lentfer 	}
5446d49e1aeSJan Lentfer }
5453ff40c12SJohn Marino 
5463ff40c12SJohn Marino 
wps_attr_text(struct wpabuf * data,char * buf,char * end)5473ff40c12SJohn Marino int wps_attr_text(struct wpabuf *data, char *buf, char *end)
5483ff40c12SJohn Marino {
5493ff40c12SJohn Marino 	struct wps_parse_attr attr;
5503ff40c12SJohn Marino 	char *pos = buf;
5513ff40c12SJohn Marino 	int ret;
5523ff40c12SJohn Marino 
5533ff40c12SJohn Marino 	if (wps_parse_msg(data, &attr) < 0)
5543ff40c12SJohn Marino 		return -1;
5553ff40c12SJohn Marino 
5563ff40c12SJohn Marino 	if (attr.wps_state) {
5573ff40c12SJohn Marino 		if (*attr.wps_state == WPS_STATE_NOT_CONFIGURED)
5583ff40c12SJohn Marino 			ret = os_snprintf(pos, end - pos,
5593ff40c12SJohn Marino 					  "wps_state=unconfigured\n");
5603ff40c12SJohn Marino 		else if (*attr.wps_state == WPS_STATE_CONFIGURED)
5613ff40c12SJohn Marino 			ret = os_snprintf(pos, end - pos,
5623ff40c12SJohn Marino 					  "wps_state=configured\n");
5633ff40c12SJohn Marino 		else
5643ff40c12SJohn Marino 			ret = 0;
565*a1157835SDaniel Fojt 		if (os_snprintf_error(end - pos, ret))
5663ff40c12SJohn Marino 			return pos - buf;
5673ff40c12SJohn Marino 		pos += ret;
5683ff40c12SJohn Marino 	}
5693ff40c12SJohn Marino 
5703ff40c12SJohn Marino 	if (attr.ap_setup_locked && *attr.ap_setup_locked) {
5713ff40c12SJohn Marino 		ret = os_snprintf(pos, end - pos,
5723ff40c12SJohn Marino 				  "wps_ap_setup_locked=1\n");
573*a1157835SDaniel Fojt 		if (os_snprintf_error(end - pos, ret))
5743ff40c12SJohn Marino 			return pos - buf;
5753ff40c12SJohn Marino 		pos += ret;
5763ff40c12SJohn Marino 	}
5773ff40c12SJohn Marino 
5783ff40c12SJohn Marino 	if (attr.selected_registrar && *attr.selected_registrar) {
5793ff40c12SJohn Marino 		ret = os_snprintf(pos, end - pos,
5803ff40c12SJohn Marino 				  "wps_selected_registrar=1\n");
581*a1157835SDaniel Fojt 		if (os_snprintf_error(end - pos, ret))
5823ff40c12SJohn Marino 			return pos - buf;
5833ff40c12SJohn Marino 		pos += ret;
5843ff40c12SJohn Marino 	}
5853ff40c12SJohn Marino 
5863ff40c12SJohn Marino 	if (attr.dev_password_id) {
5873ff40c12SJohn Marino 		ret = os_snprintf(pos, end - pos,
5883ff40c12SJohn Marino 				  "wps_device_password_id=%u\n",
5893ff40c12SJohn Marino 				  WPA_GET_BE16(attr.dev_password_id));
590*a1157835SDaniel Fojt 		if (os_snprintf_error(end - pos, ret))
5913ff40c12SJohn Marino 			return pos - buf;
5923ff40c12SJohn Marino 		pos += ret;
5933ff40c12SJohn Marino 	}
5943ff40c12SJohn Marino 
5953ff40c12SJohn Marino 	if (attr.sel_reg_config_methods) {
5963ff40c12SJohn Marino 		ret = os_snprintf(pos, end - pos,
5973ff40c12SJohn Marino 				  "wps_selected_registrar_config_methods="
5983ff40c12SJohn Marino 				  "0x%04x\n",
5993ff40c12SJohn Marino 				  WPA_GET_BE16(attr.sel_reg_config_methods));
600*a1157835SDaniel Fojt 		if (os_snprintf_error(end - pos, ret))
6013ff40c12SJohn Marino 			return pos - buf;
6023ff40c12SJohn Marino 		pos += ret;
6033ff40c12SJohn Marino 	}
6043ff40c12SJohn Marino 
6053ff40c12SJohn Marino 	if (attr.primary_dev_type) {
6063ff40c12SJohn Marino 		char devtype[WPS_DEV_TYPE_BUFSIZE];
6073ff40c12SJohn Marino 		ret = os_snprintf(pos, end - pos,
6083ff40c12SJohn Marino 				  "wps_primary_device_type=%s\n",
6093ff40c12SJohn Marino 				  wps_dev_type_bin2str(attr.primary_dev_type,
6103ff40c12SJohn Marino 						       devtype,
6113ff40c12SJohn Marino 						       sizeof(devtype)));
612*a1157835SDaniel Fojt 		if (os_snprintf_error(end - pos, ret))
6133ff40c12SJohn Marino 			return pos - buf;
6143ff40c12SJohn Marino 		pos += ret;
6153ff40c12SJohn Marino 	}
6163ff40c12SJohn Marino 
6173ff40c12SJohn Marino 	if (attr.dev_name) {
6183ff40c12SJohn Marino 		char *str = os_malloc(attr.dev_name_len + 1);
6193ff40c12SJohn Marino 		size_t i;
6203ff40c12SJohn Marino 		if (str == NULL)
6213ff40c12SJohn Marino 			return pos - buf;
6223ff40c12SJohn Marino 		for (i = 0; i < attr.dev_name_len; i++) {
623*a1157835SDaniel Fojt 			if (attr.dev_name[i] == 0 ||
624*a1157835SDaniel Fojt 			    is_ctrl_char(attr.dev_name[i]))
6253ff40c12SJohn Marino 				str[i] = '_';
6263ff40c12SJohn Marino 			else
6273ff40c12SJohn Marino 				str[i] = attr.dev_name[i];
6283ff40c12SJohn Marino 		}
6293ff40c12SJohn Marino 		str[i] = '\0';
6303ff40c12SJohn Marino 		ret = os_snprintf(pos, end - pos, "wps_device_name=%s\n", str);
6313ff40c12SJohn Marino 		os_free(str);
632*a1157835SDaniel Fojt 		if (os_snprintf_error(end - pos, ret))
6333ff40c12SJohn Marino 			return pos - buf;
6343ff40c12SJohn Marino 		pos += ret;
6353ff40c12SJohn Marino 	}
6363ff40c12SJohn Marino 
6373ff40c12SJohn Marino 	if (attr.config_methods) {
6383ff40c12SJohn Marino 		ret = os_snprintf(pos, end - pos,
6393ff40c12SJohn Marino 				  "wps_config_methods=0x%04x\n",
6403ff40c12SJohn Marino 				  WPA_GET_BE16(attr.config_methods));
641*a1157835SDaniel Fojt 		if (os_snprintf_error(end - pos, ret))
6423ff40c12SJohn Marino 			return pos - buf;
6433ff40c12SJohn Marino 		pos += ret;
6443ff40c12SJohn Marino 	}
6453ff40c12SJohn Marino 
6463ff40c12SJohn Marino 	return pos - buf;
6473ff40c12SJohn Marino }
6483ff40c12SJohn Marino 
6493ff40c12SJohn Marino 
wps_ei_str(enum wps_error_indication ei)6503ff40c12SJohn Marino const char * wps_ei_str(enum wps_error_indication ei)
6513ff40c12SJohn Marino {
6523ff40c12SJohn Marino 	switch (ei) {
6533ff40c12SJohn Marino 	case WPS_EI_NO_ERROR:
6543ff40c12SJohn Marino 		return "No Error";
6553ff40c12SJohn Marino 	case WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED:
6563ff40c12SJohn Marino 		return "TKIP Only Prohibited";
6573ff40c12SJohn Marino 	case WPS_EI_SECURITY_WEP_PROHIBITED:
6583ff40c12SJohn Marino 		return "WEP Prohibited";
6593ff40c12SJohn Marino 	case WPS_EI_AUTH_FAILURE:
6603ff40c12SJohn Marino 		return "Authentication Failure";
6613ff40c12SJohn Marino 	default:
6623ff40c12SJohn Marino 		return "Unknown";
6633ff40c12SJohn Marino 	}
6643ff40c12SJohn Marino }
665