2 * Wi-Fi Protected Setup - Enrollee
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/crypto.h"
13 #include "crypto/sha256.h"
14 #include "crypto/random.h"
16 #include "wps_dev_attr.h"
19 static int wps_build_mac_addr(struct wps_data *wps, struct wpabuf *msg)
21 wpa_printf(MSG_DEBUG, "WPS: * MAC Address");
22 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
23 wpabuf_put_be16(msg, ETH_ALEN);
24 wpabuf_put_data(msg, wps->mac_addr_e, ETH_ALEN);
29 static int wps_build_wps_state(struct wps_data *wps, struct wpabuf *msg)
33 state = wps->wps->wps_state;
35 state = WPS_STATE_NOT_CONFIGURED;
36 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
38 wpabuf_put_be16(msg, ATTR_WPS_STATE);
39 wpabuf_put_be16(msg, 1);
40 wpabuf_put_u8(msg, state);
45 static int wps_build_e_hash(struct wps_data *wps, struct wpabuf *msg)
51 if (random_get_bytes(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
53 wpa_hexdump(MSG_DEBUG, "WPS: E-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
54 wpa_hexdump(MSG_DEBUG, "WPS: E-S2",
55 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
57 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
58 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
63 wpa_printf(MSG_DEBUG, "WPS: * E-Hash1");
64 wpabuf_put_be16(msg, ATTR_E_HASH1);
65 wpabuf_put_be16(msg, SHA256_MAC_LEN);
66 hash = wpabuf_put(msg, SHA256_MAC_LEN);
67 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
68 addr[0] = wps->snonce;
69 len[0] = WPS_SECRET_NONCE_LEN;
72 addr[2] = wpabuf_head(wps->dh_pubkey_e);
73 len[2] = wpabuf_len(wps->dh_pubkey_e);
74 addr[3] = wpabuf_head(wps->dh_pubkey_r);
75 len[3] = wpabuf_len(wps->dh_pubkey_r);
76 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
77 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", hash, SHA256_MAC_LEN);
79 wpa_printf(MSG_DEBUG, "WPS: * E-Hash2");
80 wpabuf_put_be16(msg, ATTR_E_HASH2);
81 wpabuf_put_be16(msg, SHA256_MAC_LEN);
82 hash = wpabuf_put(msg, SHA256_MAC_LEN);
83 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
84 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
86 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
87 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", hash, SHA256_MAC_LEN);
93 static int wps_build_e_snonce1(struct wps_data *wps, struct wpabuf *msg)
95 wpa_printf(MSG_DEBUG, "WPS: * E-SNonce1");
96 wpabuf_put_be16(msg, ATTR_E_SNONCE1);
97 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
98 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
103 static int wps_build_e_snonce2(struct wps_data *wps, struct wpabuf *msg)
105 wpa_printf(MSG_DEBUG, "WPS: * E-SNonce2");
106 wpabuf_put_be16(msg, ATTR_E_SNONCE2);
107 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
108 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
109 WPS_SECRET_NONCE_LEN);
114 static struct wpabuf * wps_build_m1(struct wps_data *wps)
119 if (random_get_bytes(wps->nonce_e, WPS_NONCE_LEN) < 0)
121 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
122 wps->nonce_e, WPS_NONCE_LEN);
124 wpa_printf(MSG_DEBUG, "WPS: Building Message M1");
125 msg = wpabuf_alloc(1000);
129 config_methods = wps->wps->config_methods;
130 if (wps->wps->ap && !wps->pbc_in_m1 &&
131 (wps->dev_password_len != 0 ||
132 (config_methods & WPS_CONFIG_DISPLAY))) {
134 * These are the methods that the AP supports as an Enrollee
135 * for adding external Registrars, so remove PushButton.
137 * As a workaround for Windows 7 mechanism for probing WPS
138 * capabilities from M1, leave PushButton option if no PIN
139 * method is available or if WPS configuration enables PBC
142 config_methods &= ~WPS_CONFIG_PUSHBUTTON;
144 config_methods &= ~(WPS_CONFIG_VIRT_PUSHBUTTON |
145 WPS_CONFIG_PHY_PUSHBUTTON);
146 #endif /* CONFIG_WPS2 */
149 if (wps_build_version(msg) ||
150 wps_build_msg_type(msg, WPS_M1) ||
151 wps_build_uuid_e(msg, wps->uuid_e) ||
152 wps_build_mac_addr(wps, msg) ||
153 wps_build_enrollee_nonce(wps, msg) ||
154 wps_build_public_key(wps, msg) ||
155 wps_build_auth_type_flags(wps, msg) ||
156 wps_build_encr_type_flags(wps, msg) ||
157 wps_build_conn_type_flags(wps, msg) ||
158 wps_build_config_methods(msg, config_methods) ||
159 wps_build_wps_state(wps, msg) ||
160 wps_build_device_attrs(&wps->wps->dev, msg) ||
161 wps_build_rf_bands(&wps->wps->dev, msg) ||
162 wps_build_assoc_state(wps, msg) ||
163 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
164 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
165 wps_build_os_version(&wps->wps->dev, msg) ||
166 wps_build_wfa_ext(msg, 0, NULL, 0) ||
167 wps_build_vendor_ext_m1(&wps->wps->dev, msg)) {
172 wps->state = RECV_M2;
177 static struct wpabuf * wps_build_m3(struct wps_data *wps)
181 wpa_printf(MSG_DEBUG, "WPS: Building Message M3");
183 if (wps->dev_password == NULL) {
184 wpa_printf(MSG_DEBUG, "WPS: No Device Password available");
187 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
189 msg = wpabuf_alloc(1000);
193 if (wps_build_version(msg) ||
194 wps_build_msg_type(msg, WPS_M3) ||
195 wps_build_registrar_nonce(wps, msg) ||
196 wps_build_e_hash(wps, msg) ||
197 wps_build_wfa_ext(msg, 0, NULL, 0) ||
198 wps_build_authenticator(wps, msg)) {
203 wps->state = RECV_M4;
208 static struct wpabuf * wps_build_m5(struct wps_data *wps)
210 struct wpabuf *msg, *plain;
212 wpa_printf(MSG_DEBUG, "WPS: Building Message M5");
214 plain = wpabuf_alloc(200);
218 msg = wpabuf_alloc(1000);
224 if (wps_build_version(msg) ||
225 wps_build_msg_type(msg, WPS_M5) ||
226 wps_build_registrar_nonce(wps, msg) ||
227 wps_build_e_snonce1(wps, plain) ||
228 wps_build_key_wrap_auth(wps, plain) ||
229 wps_build_encr_settings(wps, msg, plain) ||
230 wps_build_wfa_ext(msg, 0, NULL, 0) ||
231 wps_build_authenticator(wps, msg)) {
238 wps->state = RECV_M6;
243 static int wps_build_cred_ssid(struct wps_data *wps, struct wpabuf *msg)
245 wpa_printf(MSG_DEBUG, "WPS: * SSID");
246 wpabuf_put_be16(msg, ATTR_SSID);
247 wpabuf_put_be16(msg, wps->wps->ssid_len);
248 wpabuf_put_data(msg, wps->wps->ssid, wps->wps->ssid_len);
253 static int wps_build_cred_auth_type(struct wps_data *wps, struct wpabuf *msg)
255 u16 auth_type = wps->wps->auth_types;
257 /* Select the best authentication type */
258 if (auth_type & WPS_AUTH_WPA2PSK)
259 auth_type = WPS_AUTH_WPA2PSK;
260 else if (auth_type & WPS_AUTH_WPAPSK)
261 auth_type = WPS_AUTH_WPAPSK;
262 else if (auth_type & WPS_AUTH_OPEN)
263 auth_type = WPS_AUTH_OPEN;
264 else if (auth_type & WPS_AUTH_SHARED)
265 auth_type = WPS_AUTH_SHARED;
267 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)", auth_type);
268 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
269 wpabuf_put_be16(msg, 2);
270 wpabuf_put_be16(msg, auth_type);
275 static int wps_build_cred_encr_type(struct wps_data *wps, struct wpabuf *msg)
277 u16 encr_type = wps->wps->encr_types;
279 /* Select the best encryption type */
280 if (wps->wps->auth_types & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) {
281 if (encr_type & WPS_ENCR_AES)
282 encr_type = WPS_ENCR_AES;
283 else if (encr_type & WPS_ENCR_TKIP)
284 encr_type = WPS_ENCR_TKIP;
286 if (encr_type & WPS_ENCR_WEP)
287 encr_type = WPS_ENCR_WEP;
288 else if (encr_type & WPS_ENCR_NONE)
289 encr_type = WPS_ENCR_NONE;
292 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)", encr_type);
293 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
294 wpabuf_put_be16(msg, 2);
295 wpabuf_put_be16(msg, encr_type);
300 static int wps_build_cred_network_key(struct wps_data *wps, struct wpabuf *msg)
302 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
303 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
304 wpabuf_put_be16(msg, wps->wps->network_key_len);
305 wpabuf_put_data(msg, wps->wps->network_key, wps->wps->network_key_len);
310 static int wps_build_cred_mac_addr(struct wps_data *wps, struct wpabuf *msg)
312 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (AP BSSID)");
313 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
314 wpabuf_put_be16(msg, ETH_ALEN);
315 wpabuf_put_data(msg, wps->wps->dev.mac_addr, ETH_ALEN);
320 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *plain)
322 if (wps->wps->ap_settings) {
323 wpa_printf(MSG_DEBUG, "WPS: * AP Settings (pre-configured)");
324 wpabuf_put_data(plain, wps->wps->ap_settings,
325 wps->wps->ap_settings_len);
329 return wps_build_cred_ssid(wps, plain) ||
330 wps_build_cred_mac_addr(wps, plain) ||
331 wps_build_cred_auth_type(wps, plain) ||
332 wps_build_cred_encr_type(wps, plain) ||
333 wps_build_cred_network_key(wps, plain);
337 static struct wpabuf * wps_build_m7(struct wps_data *wps)
339 struct wpabuf *msg, *plain;
341 wpa_printf(MSG_DEBUG, "WPS: Building Message M7");
343 plain = wpabuf_alloc(500 + wps->wps->ap_settings_len);
347 msg = wpabuf_alloc(1000 + wps->wps->ap_settings_len);
353 if (wps_build_version(msg) ||
354 wps_build_msg_type(msg, WPS_M7) ||
355 wps_build_registrar_nonce(wps, msg) ||
356 wps_build_e_snonce2(wps, plain) ||
357 (wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
358 wps_build_key_wrap_auth(wps, plain) ||
359 wps_build_encr_settings(wps, msg, plain) ||
360 wps_build_wfa_ext(msg, 0, NULL, 0) ||
361 wps_build_authenticator(wps, msg)) {
368 if (wps->wps->ap && wps->wps->registrar) {
370 * If the Registrar is only learning our current configuration,
371 * it may not continue protocol run to successful completion.
372 * Store information here to make sure it remains available.
374 wps_device_store(wps->wps->registrar, &wps->peer_dev,
378 wps->state = RECV_M8;
383 static struct wpabuf * wps_build_wsc_done(struct wps_data *wps)
387 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_Done");
389 msg = wpabuf_alloc(1000);
393 if (wps_build_version(msg) ||
394 wps_build_msg_type(msg, WPS_WSC_DONE) ||
395 wps_build_enrollee_nonce(wps, msg) ||
396 wps_build_registrar_nonce(wps, msg) ||
397 wps_build_wfa_ext(msg, 0, NULL, 0)) {
403 wps->state = RECV_ACK;
405 wps_success_event(wps->wps);
406 wps->state = WPS_FINISHED;
412 struct wpabuf * wps_enrollee_get_msg(struct wps_data *wps,
413 enum wsc_op_code *op_code)
417 switch (wps->state) {
419 msg = wps_build_m1(wps);
423 msg = wps_build_m3(wps);
427 msg = wps_build_m5(wps);
431 msg = wps_build_m7(wps);
436 msg = wps_build_wsc_nack(wps);
440 msg = wps_build_wsc_ack(wps);
443 /* Another M2/M2D may be received */
444 wps->state = RECV_M2;
448 msg = wps_build_wsc_nack(wps);
452 msg = wps_build_wsc_done(wps);
456 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
457 "a message", wps->state);
462 if (*op_code == WSC_MSG && msg) {
463 /* Save a copy of the last message for Authenticator derivation
465 wpabuf_free(wps->last_msg);
466 wps->last_msg = wpabuf_dup(msg);
473 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
475 if (r_nonce == NULL) {
476 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
480 os_memcpy(wps->nonce_r, r_nonce, WPS_NONCE_LEN);
481 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
482 wps->nonce_r, WPS_NONCE_LEN);
488 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
490 if (e_nonce == NULL) {
491 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
495 if (os_memcmp(wps->nonce_e, e_nonce, WPS_NONCE_LEN) != 0) {
496 wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce received");
504 static int wps_process_uuid_r(struct wps_data *wps, const u8 *uuid_r)
506 if (uuid_r == NULL) {
507 wpa_printf(MSG_DEBUG, "WPS: No UUID-R received");
511 os_memcpy(wps->uuid_r, uuid_r, WPS_UUID_LEN);
512 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
518 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
521 if (pk == NULL || pk_len == 0) {
522 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
526 wpabuf_free(wps->dh_pubkey_r);
527 wps->dh_pubkey_r = wpabuf_alloc_copy(pk, pk_len);
528 if (wps->dh_pubkey_r == NULL)
531 if (wps_derive_keys(wps) < 0)
538 static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1)
540 if (r_hash1 == NULL) {
541 wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received");
545 os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN);
546 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN);
552 static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2)
554 if (r_hash2 == NULL) {
555 wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received");
559 os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN);
560 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN);
566 static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
568 u8 hash[SHA256_MAC_LEN];
572 if (r_snonce1 == NULL) {
573 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
577 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1,
578 WPS_SECRET_NONCE_LEN);
580 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
582 len[0] = WPS_SECRET_NONCE_LEN;
584 len[1] = WPS_PSK_LEN;
585 addr[2] = wpabuf_head(wps->dh_pubkey_e);
586 len[2] = wpabuf_len(wps->dh_pubkey_e);
587 addr[3] = wpabuf_head(wps->dh_pubkey_r);
588 len[3] = wpabuf_len(wps->dh_pubkey_r);
589 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
591 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
592 wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does "
593 "not match with the pre-committed value");
594 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
595 wps_pwd_auth_fail_event(wps->wps, 1, 1);
599 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
600 "half of the device password");
606 static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
608 u8 hash[SHA256_MAC_LEN];
612 if (r_snonce2 == NULL) {
613 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
617 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2,
618 WPS_SECRET_NONCE_LEN);
620 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
622 len[0] = WPS_SECRET_NONCE_LEN;
624 len[1] = WPS_PSK_LEN;
625 addr[2] = wpabuf_head(wps->dh_pubkey_e);
626 len[2] = wpabuf_len(wps->dh_pubkey_e);
627 addr[3] = wpabuf_head(wps->dh_pubkey_r);
628 len[3] = wpabuf_len(wps->dh_pubkey_r);
629 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
631 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
632 wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does "
633 "not match with the pre-committed value");
634 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
635 wps_pwd_auth_fail_event(wps->wps, 1, 2);
639 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
640 "half of the device password");
646 static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
647 size_t cred_len, int wps2)
649 struct wps_parse_attr attr;
653 wpa_printf(MSG_DEBUG, "WPS: Received Credential");
654 os_memset(&wps->cred, 0, sizeof(wps->cred));
655 wpabuf_set(&msg, cred, cred_len);
656 if (wps_parse_msg(&msg, &attr) < 0 ||
657 wps_process_cred(&attr, &wps->cred))
660 if (os_memcmp(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) !=
662 wpa_printf(MSG_DEBUG, "WPS: MAC Address in the Credential ("
663 MACSTR ") does not match with own address (" MACSTR
664 ")", MAC2STR(wps->cred.mac_addr),
665 MAC2STR(wps->wps->dev.mac_addr));
667 * In theory, this could be consider fatal error, but there are
668 * number of deployed implementations using other address here
669 * due to unclarity in the specification. For interoperability
670 * reasons, allow this to be processed since we do not really
671 * use the MAC Address information for anything.
673 #ifdef CONFIG_WPS_STRICT
675 wpa_printf(MSG_INFO, "WPS: Do not accept incorrect "
676 "MAC Address in AP Settings");
679 #endif /* CONFIG_WPS_STRICT */
683 if (!(wps->cred.encr_type &
684 (WPS_ENCR_NONE | WPS_ENCR_TKIP | WPS_ENCR_AES))) {
685 if (wps->cred.encr_type & WPS_ENCR_WEP) {
686 wpa_printf(MSG_INFO, "WPS: Reject Credential "
687 "due to WEP configuration");
688 wps->error_indication = WPS_EI_SECURITY_WEP_PROHIBITED;
692 wpa_printf(MSG_INFO, "WPS: Reject Credential due to "
693 "invalid encr_type 0x%x", wps->cred.encr_type);
696 #endif /* CONFIG_WPS2 */
698 if (wps->wps->cred_cb) {
699 wps->cred.cred_attr = cred - 4;
700 wps->cred.cred_attr_len = cred_len + 4;
701 ret = wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
702 wps->cred.cred_attr = NULL;
703 wps->cred.cred_attr_len = 0;
710 static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
711 size_t cred_len[], size_t num_cred, int wps2)
720 wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
725 for (i = 0; i < num_cred; i++) {
727 res = wps_process_cred_e(wps, cred[i], cred_len[i], wps2);
731 wpa_printf(MSG_DEBUG, "WPS: WEP credential skipped");
737 wpa_printf(MSG_DEBUG, "WPS: No valid Credential attribute "
746 static int wps_process_ap_settings_e(struct wps_data *wps,
747 struct wps_parse_attr *attr,
748 struct wpabuf *attrs, int wps2)
750 struct wps_credential cred;
755 if (wps_process_ap_settings(attr, &cred) < 0)
758 wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
761 if (os_memcmp(cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) !=
763 wpa_printf(MSG_DEBUG, "WPS: MAC Address in the AP Settings ("
764 MACSTR ") does not match with own address (" MACSTR
765 ")", MAC2STR(cred.mac_addr),
766 MAC2STR(wps->wps->dev.mac_addr));
768 * In theory, this could be consider fatal error, but there are
769 * number of deployed implementations using other address here
770 * due to unclarity in the specification. For interoperability
771 * reasons, allow this to be processed since we do not really
772 * use the MAC Address information for anything.
774 #ifdef CONFIG_WPS_STRICT
776 wpa_printf(MSG_INFO, "WPS: Do not accept incorrect "
777 "MAC Address in AP Settings");
780 #endif /* CONFIG_WPS_STRICT */
784 if (!(cred.encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP | WPS_ENCR_AES)))
786 if (cred.encr_type & WPS_ENCR_WEP) {
787 wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
788 "due to WEP configuration");
789 wps->error_indication = WPS_EI_SECURITY_WEP_PROHIBITED;
793 wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
794 "invalid encr_type 0x%x", cred.encr_type);
797 #endif /* CONFIG_WPS2 */
799 #ifdef CONFIG_WPS_STRICT
801 if ((cred.encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
803 (cred.auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
805 wpa_printf(MSG_INFO, "WPS-STRICT: Invalid WSC 2.0 "
806 "AP Settings: WPA-Personal/TKIP only");
807 wps->error_indication =
808 WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED;
812 #endif /* CONFIG_WPS_STRICT */
815 if ((cred.encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) == WPS_ENCR_TKIP)
817 wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
819 cred.encr_type |= WPS_ENCR_AES;
822 if ((cred.auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
824 wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
826 cred.auth_type |= WPS_AUTH_WPA2PSK;
828 #endif /* CONFIG_WPS2 */
830 if (wps->wps->cred_cb) {
831 cred.cred_attr = wpabuf_head(attrs);
832 cred.cred_attr_len = wpabuf_len(attrs);
833 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
840 static enum wps_process_res wps_process_m2(struct wps_data *wps,
841 const struct wpabuf *msg,
842 struct wps_parse_attr *attr)
844 wpa_printf(MSG_DEBUG, "WPS: Received M2");
846 if (wps->state != RECV_M2) {
847 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
848 "receiving M2", wps->state);
849 wps->state = SEND_WSC_NACK;
853 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
854 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
855 wps_process_uuid_r(wps, attr->uuid_r)) {
856 wps->state = SEND_WSC_NACK;
861 * Stop here on an AP as an Enrollee if AP Setup is locked unless the
862 * special locked mode is used to allow protocol run up to M7 in order
863 * to support external Registrars that only learn the current AP
864 * configuration without changing it.
867 ((wps->wps->ap_setup_locked && wps->wps->ap_setup_locked != 2) ||
868 wps->dev_password == NULL)) {
869 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
870 "registration of a new Registrar");
871 wps->config_error = WPS_CFG_SETUP_LOCKED;
872 wps->state = SEND_WSC_NACK;
876 if (wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
877 wps_process_authenticator(wps, attr->authenticator, msg) ||
878 wps_process_device_attrs(&wps->peer_dev, attr)) {
879 wps->state = SEND_WSC_NACK;
883 wps->state = SEND_M3;
888 static enum wps_process_res wps_process_m2d(struct wps_data *wps,
889 struct wps_parse_attr *attr)
891 wpa_printf(MSG_DEBUG, "WPS: Received M2D");
893 if (wps->state != RECV_M2) {
894 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
895 "receiving M2D", wps->state);
896 wps->state = SEND_WSC_NACK;
900 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
901 attr->manufacturer, attr->manufacturer_len);
902 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
903 attr->model_name, attr->model_name_len);
904 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
905 attr->model_number, attr->model_number_len);
906 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
907 attr->serial_number, attr->serial_number_len);
908 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
909 attr->dev_name, attr->dev_name_len);
911 if (wps->wps->event_cb) {
912 union wps_event_data data;
913 struct wps_event_m2d *m2d = &data.m2d;
914 os_memset(&data, 0, sizeof(data));
915 if (attr->config_methods)
916 m2d->config_methods =
917 WPA_GET_BE16(attr->config_methods);
918 m2d->manufacturer = attr->manufacturer;
919 m2d->manufacturer_len = attr->manufacturer_len;
920 m2d->model_name = attr->model_name;
921 m2d->model_name_len = attr->model_name_len;
922 m2d->model_number = attr->model_number;
923 m2d->model_number_len = attr->model_number_len;
924 m2d->serial_number = attr->serial_number;
925 m2d->serial_number_len = attr->serial_number_len;
926 m2d->dev_name = attr->dev_name;
927 m2d->dev_name_len = attr->dev_name_len;
928 m2d->primary_dev_type = attr->primary_dev_type;
929 if (attr->config_error)
931 WPA_GET_BE16(attr->config_error);
932 if (attr->dev_password_id)
933 m2d->dev_password_id =
934 WPA_GET_BE16(attr->dev_password_id);
935 wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_M2D, &data);
938 wps->state = RECEIVED_M2D;
943 static enum wps_process_res wps_process_m4(struct wps_data *wps,
944 const struct wpabuf *msg,
945 struct wps_parse_attr *attr)
947 struct wpabuf *decrypted;
948 struct wps_parse_attr eattr;
950 wpa_printf(MSG_DEBUG, "WPS: Received M4");
952 if (wps->state != RECV_M4) {
953 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
954 "receiving M4", wps->state);
955 wps->state = SEND_WSC_NACK;
959 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
960 wps_process_authenticator(wps, attr->authenticator, msg) ||
961 wps_process_r_hash1(wps, attr->r_hash1) ||
962 wps_process_r_hash2(wps, attr->r_hash2)) {
963 wps->state = SEND_WSC_NACK;
967 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
968 attr->encr_settings_len);
969 if (decrypted == NULL) {
970 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
971 "Settings attribute");
972 wps->state = SEND_WSC_NACK;
976 if (wps_validate_m4_encr(decrypted, attr->version2 != NULL) < 0) {
977 wpabuf_free(decrypted);
978 wps->state = SEND_WSC_NACK;
982 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
984 if (wps_parse_msg(decrypted, &eattr) < 0 ||
985 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
986 wps_process_r_snonce1(wps, eattr.r_snonce1)) {
987 wpabuf_free(decrypted);
988 wps->state = SEND_WSC_NACK;
991 wpabuf_free(decrypted);
993 wps->state = SEND_M5;
998 static enum wps_process_res wps_process_m6(struct wps_data *wps,
999 const struct wpabuf *msg,
1000 struct wps_parse_attr *attr)
1002 struct wpabuf *decrypted;
1003 struct wps_parse_attr eattr;
1005 wpa_printf(MSG_DEBUG, "WPS: Received M6");
1007 if (wps->state != RECV_M6) {
1008 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1009 "receiving M6", wps->state);
1010 wps->state = SEND_WSC_NACK;
1011 return WPS_CONTINUE;
1014 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1015 wps_process_authenticator(wps, attr->authenticator, msg)) {
1016 wps->state = SEND_WSC_NACK;
1017 return WPS_CONTINUE;
1020 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1021 attr->encr_settings_len);
1022 if (decrypted == NULL) {
1023 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1024 "Settings attribute");
1025 wps->state = SEND_WSC_NACK;
1026 return WPS_CONTINUE;
1029 if (wps_validate_m6_encr(decrypted, attr->version2 != NULL) < 0) {
1030 wpabuf_free(decrypted);
1031 wps->state = SEND_WSC_NACK;
1032 return WPS_CONTINUE;
1035 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1037 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1038 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1039 wps_process_r_snonce2(wps, eattr.r_snonce2)) {
1040 wpabuf_free(decrypted);
1041 wps->state = SEND_WSC_NACK;
1042 return WPS_CONTINUE;
1044 wpabuf_free(decrypted);
1047 wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_AP_PIN_SUCCESS,
1050 wps->state = SEND_M7;
1051 return WPS_CONTINUE;
1055 static enum wps_process_res wps_process_m8(struct wps_data *wps,
1056 const struct wpabuf *msg,
1057 struct wps_parse_attr *attr)
1059 struct wpabuf *decrypted;
1060 struct wps_parse_attr eattr;
1062 wpa_printf(MSG_DEBUG, "WPS: Received M8");
1064 if (wps->state != RECV_M8) {
1065 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1066 "receiving M8", wps->state);
1067 wps->state = SEND_WSC_NACK;
1068 return WPS_CONTINUE;
1071 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1072 wps_process_authenticator(wps, attr->authenticator, msg)) {
1073 wps->state = SEND_WSC_NACK;
1074 return WPS_CONTINUE;
1077 if (wps->wps->ap && wps->wps->ap_setup_locked) {
1079 * Stop here if special ap_setup_locked == 2 mode allowed the
1080 * protocol to continue beyond M2. This allows ER to learn the
1081 * current AP settings without changing them.
1083 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
1084 "registration of a new Registrar");
1085 wps->config_error = WPS_CFG_SETUP_LOCKED;
1086 wps->state = SEND_WSC_NACK;
1087 return WPS_CONTINUE;
1090 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1091 attr->encr_settings_len);
1092 if (decrypted == NULL) {
1093 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1094 "Settings attribute");
1095 wps->state = SEND_WSC_NACK;
1096 return WPS_CONTINUE;
1099 if (wps_validate_m8_encr(decrypted, wps->wps->ap,
1100 attr->version2 != NULL) < 0) {
1101 wpabuf_free(decrypted);
1102 wps->state = SEND_WSC_NACK;
1103 return WPS_CONTINUE;
1106 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1108 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1109 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1110 wps_process_creds(wps, eattr.cred, eattr.cred_len,
1111 eattr.num_cred, attr->version2 != NULL) ||
1112 wps_process_ap_settings_e(wps, &eattr, decrypted,
1113 attr->version2 != NULL)) {
1114 wpabuf_free(decrypted);
1115 wps->state = SEND_WSC_NACK;
1116 return WPS_CONTINUE;
1118 wpabuf_free(decrypted);
1120 wps->state = WPS_MSG_DONE;
1121 return WPS_CONTINUE;
1125 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1126 const struct wpabuf *msg)
1128 struct wps_parse_attr attr;
1129 enum wps_process_res ret = WPS_CONTINUE;
1131 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1133 if (wps_parse_msg(msg, &attr) < 0)
1136 if (attr.enrollee_nonce == NULL ||
1137 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
1138 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1142 if (attr.msg_type == NULL) {
1143 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1144 wps->state = SEND_WSC_NACK;
1145 return WPS_CONTINUE;
1148 switch (*attr.msg_type) {
1150 if (wps_validate_m2(msg) < 0)
1152 ret = wps_process_m2(wps, msg, &attr);
1155 if (wps_validate_m2d(msg) < 0)
1157 ret = wps_process_m2d(wps, &attr);
1160 if (wps_validate_m4(msg) < 0)
1162 ret = wps_process_m4(wps, msg, &attr);
1163 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1164 wps_fail_event(wps->wps, WPS_M4, wps->config_error,
1165 wps->error_indication);
1168 if (wps_validate_m6(msg) < 0)
1170 ret = wps_process_m6(wps, msg, &attr);
1171 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1172 wps_fail_event(wps->wps, WPS_M6, wps->config_error,
1173 wps->error_indication);
1176 if (wps_validate_m8(msg) < 0)
1178 ret = wps_process_m8(wps, msg, &attr);
1179 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1180 wps_fail_event(wps->wps, WPS_M8, wps->config_error,
1181 wps->error_indication);
1184 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1190 * Save a copy of the last message for Authenticator derivation if we
1191 * are continuing. However, skip M2D since it is not authenticated and
1192 * neither is the ACK/NACK response frame. This allows the possibly
1193 * following M2 to be processed correctly by using the previously sent
1194 * M1 in Authenticator derivation.
1196 if (ret == WPS_CONTINUE && *attr.msg_type != WPS_M2D) {
1197 /* Save a copy of the last message for Authenticator derivation
1199 wpabuf_free(wps->last_msg);
1200 wps->last_msg = wpabuf_dup(msg);
1207 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1208 const struct wpabuf *msg)
1210 struct wps_parse_attr attr;
1212 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1214 if (wps_parse_msg(msg, &attr) < 0)
1217 if (attr.msg_type == NULL) {
1218 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1222 if (*attr.msg_type != WPS_WSC_ACK) {
1223 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1228 if (attr.registrar_nonce == NULL ||
1229 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
1231 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1235 if (attr.enrollee_nonce == NULL ||
1236 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
1237 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1241 if (wps->state == RECV_ACK && wps->wps->ap) {
1242 wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
1243 "completed successfully");
1244 wps_success_event(wps->wps);
1245 wps->state = WPS_FINISHED;
1253 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1254 const struct wpabuf *msg)
1256 struct wps_parse_attr attr;
1259 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1261 if (wps_parse_msg(msg, &attr) < 0)
1264 if (attr.msg_type == NULL) {
1265 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1269 if (*attr.msg_type != WPS_WSC_NACK) {
1270 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1275 if (attr.registrar_nonce == NULL ||
1276 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN) != 0)
1278 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1279 wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
1280 attr.registrar_nonce, WPS_NONCE_LEN);
1281 wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
1282 wps->nonce_r, WPS_NONCE_LEN);
1286 if (attr.enrollee_nonce == NULL ||
1287 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN) != 0) {
1288 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1289 wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
1290 attr.enrollee_nonce, WPS_NONCE_LEN);
1291 wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
1292 wps->nonce_e, WPS_NONCE_LEN);
1296 if (attr.config_error == NULL) {
1297 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1302 config_error = WPA_GET_BE16(attr.config_error);
1303 wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
1304 "Configuration Error %d", config_error);
1306 switch (wps->state) {
1308 wps_fail_event(wps->wps, WPS_M3, config_error,
1309 wps->error_indication);
1312 wps_fail_event(wps->wps, WPS_M5, config_error,
1313 wps->error_indication);
1316 wps_fail_event(wps->wps, WPS_M7, config_error,
1317 wps->error_indication);
1323 /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1324 * Enrollee is Authenticator */
1325 wps->state = SEND_WSC_NACK;
1331 enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps,
1332 enum wsc_op_code op_code,
1333 const struct wpabuf *msg)
1336 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1338 (unsigned long) wpabuf_len(msg), op_code);
1340 if (op_code == WSC_UPnP) {
1341 /* Determine the OpCode based on message type attribute */
1342 struct wps_parse_attr attr;
1343 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type) {
1344 if (*attr.msg_type == WPS_WSC_ACK)
1346 else if (*attr.msg_type == WPS_WSC_NACK)
1354 return wps_process_wsc_msg(wps, msg);
1356 if (wps_validate_wsc_ack(msg) < 0)
1358 return wps_process_wsc_ack(wps, msg);
1360 if (wps_validate_wsc_nack(msg) < 0)
1362 return wps_process_wsc_nack(wps, msg);
1364 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);