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)) {
171 wps->state = RECV_M2;
176 static struct wpabuf * wps_build_m3(struct wps_data *wps)
180 wpa_printf(MSG_DEBUG, "WPS: Building Message M3");
182 if (wps->dev_password == NULL) {
183 wpa_printf(MSG_DEBUG, "WPS: No Device Password available");
186 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
188 msg = wpabuf_alloc(1000);
192 if (wps_build_version(msg) ||
193 wps_build_msg_type(msg, WPS_M3) ||
194 wps_build_registrar_nonce(wps, msg) ||
195 wps_build_e_hash(wps, msg) ||
196 wps_build_wfa_ext(msg, 0, NULL, 0) ||
197 wps_build_authenticator(wps, msg)) {
202 wps->state = RECV_M4;
207 static struct wpabuf * wps_build_m5(struct wps_data *wps)
209 struct wpabuf *msg, *plain;
211 wpa_printf(MSG_DEBUG, "WPS: Building Message M5");
213 plain = wpabuf_alloc(200);
217 msg = wpabuf_alloc(1000);
223 if (wps_build_version(msg) ||
224 wps_build_msg_type(msg, WPS_M5) ||
225 wps_build_registrar_nonce(wps, msg) ||
226 wps_build_e_snonce1(wps, plain) ||
227 wps_build_key_wrap_auth(wps, plain) ||
228 wps_build_encr_settings(wps, msg, plain) ||
229 wps_build_wfa_ext(msg, 0, NULL, 0) ||
230 wps_build_authenticator(wps, msg)) {
237 wps->state = RECV_M6;
242 static int wps_build_cred_ssid(struct wps_data *wps, struct wpabuf *msg)
244 wpa_printf(MSG_DEBUG, "WPS: * SSID");
245 wpabuf_put_be16(msg, ATTR_SSID);
246 wpabuf_put_be16(msg, wps->wps->ssid_len);
247 wpabuf_put_data(msg, wps->wps->ssid, wps->wps->ssid_len);
252 static int wps_build_cred_auth_type(struct wps_data *wps, struct wpabuf *msg)
254 u16 auth_type = wps->wps->auth_types;
256 /* Select the best authentication type */
257 if (auth_type & WPS_AUTH_WPA2PSK)
258 auth_type = WPS_AUTH_WPA2PSK;
259 else if (auth_type & WPS_AUTH_WPAPSK)
260 auth_type = WPS_AUTH_WPAPSK;
261 else if (auth_type & WPS_AUTH_OPEN)
262 auth_type = WPS_AUTH_OPEN;
263 else if (auth_type & WPS_AUTH_SHARED)
264 auth_type = WPS_AUTH_SHARED;
266 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)", auth_type);
267 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
268 wpabuf_put_be16(msg, 2);
269 wpabuf_put_be16(msg, auth_type);
274 static int wps_build_cred_encr_type(struct wps_data *wps, struct wpabuf *msg)
276 u16 encr_type = wps->wps->encr_types;
278 /* Select the best encryption type */
279 if (wps->wps->auth_types & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK)) {
280 if (encr_type & WPS_ENCR_AES)
281 encr_type = WPS_ENCR_AES;
282 else if (encr_type & WPS_ENCR_TKIP)
283 encr_type = WPS_ENCR_TKIP;
285 if (encr_type & WPS_ENCR_WEP)
286 encr_type = WPS_ENCR_WEP;
287 else if (encr_type & WPS_ENCR_NONE)
288 encr_type = WPS_ENCR_NONE;
291 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)", encr_type);
292 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
293 wpabuf_put_be16(msg, 2);
294 wpabuf_put_be16(msg, encr_type);
299 static int wps_build_cred_network_key(struct wps_data *wps, struct wpabuf *msg)
301 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
302 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
303 wpabuf_put_be16(msg, wps->wps->network_key_len);
304 wpabuf_put_data(msg, wps->wps->network_key, wps->wps->network_key_len);
309 static int wps_build_cred_mac_addr(struct wps_data *wps, struct wpabuf *msg)
311 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (AP BSSID)");
312 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
313 wpabuf_put_be16(msg, ETH_ALEN);
314 wpabuf_put_data(msg, wps->wps->dev.mac_addr, ETH_ALEN);
319 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *plain)
321 if (wps->wps->ap_settings) {
322 wpa_printf(MSG_DEBUG, "WPS: * AP Settings (pre-configured)");
323 wpabuf_put_data(plain, wps->wps->ap_settings,
324 wps->wps->ap_settings_len);
328 return wps_build_cred_ssid(wps, plain) ||
329 wps_build_cred_mac_addr(wps, plain) ||
330 wps_build_cred_auth_type(wps, plain) ||
331 wps_build_cred_encr_type(wps, plain) ||
332 wps_build_cred_network_key(wps, plain);
336 static struct wpabuf * wps_build_m7(struct wps_data *wps)
338 struct wpabuf *msg, *plain;
340 wpa_printf(MSG_DEBUG, "WPS: Building Message M7");
342 plain = wpabuf_alloc(500 + wps->wps->ap_settings_len);
346 msg = wpabuf_alloc(1000 + wps->wps->ap_settings_len);
352 if (wps_build_version(msg) ||
353 wps_build_msg_type(msg, WPS_M7) ||
354 wps_build_registrar_nonce(wps, msg) ||
355 wps_build_e_snonce2(wps, plain) ||
356 (wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
357 wps_build_key_wrap_auth(wps, plain) ||
358 wps_build_encr_settings(wps, msg, plain) ||
359 wps_build_wfa_ext(msg, 0, NULL, 0) ||
360 wps_build_authenticator(wps, msg)) {
367 if (wps->wps->ap && wps->wps->registrar) {
369 * If the Registrar is only learning our current configuration,
370 * it may not continue protocol run to successful completion.
371 * Store information here to make sure it remains available.
373 wps_device_store(wps->wps->registrar, &wps->peer_dev,
377 wps->state = RECV_M8;
382 static struct wpabuf * wps_build_wsc_done(struct wps_data *wps)
386 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_Done");
388 msg = wpabuf_alloc(1000);
392 if (wps_build_version(msg) ||
393 wps_build_msg_type(msg, WPS_WSC_DONE) ||
394 wps_build_enrollee_nonce(wps, msg) ||
395 wps_build_registrar_nonce(wps, msg) ||
396 wps_build_wfa_ext(msg, 0, NULL, 0)) {
402 wps->state = RECV_ACK;
404 wps_success_event(wps->wps);
405 wps->state = WPS_FINISHED;
411 struct wpabuf * wps_enrollee_get_msg(struct wps_data *wps,
412 enum wsc_op_code *op_code)
416 switch (wps->state) {
418 msg = wps_build_m1(wps);
422 msg = wps_build_m3(wps);
426 msg = wps_build_m5(wps);
430 msg = wps_build_m7(wps);
435 msg = wps_build_wsc_nack(wps);
439 msg = wps_build_wsc_ack(wps);
442 /* Another M2/M2D may be received */
443 wps->state = RECV_M2;
447 msg = wps_build_wsc_nack(wps);
451 msg = wps_build_wsc_done(wps);
455 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
456 "a message", wps->state);
461 if (*op_code == WSC_MSG && msg) {
462 /* Save a copy of the last message for Authenticator derivation
464 wpabuf_free(wps->last_msg);
465 wps->last_msg = wpabuf_dup(msg);
472 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
474 if (r_nonce == NULL) {
475 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
479 os_memcpy(wps->nonce_r, r_nonce, WPS_NONCE_LEN);
480 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
481 wps->nonce_r, WPS_NONCE_LEN);
487 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
489 if (e_nonce == NULL) {
490 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
494 if (os_memcmp(wps->nonce_e, e_nonce, WPS_NONCE_LEN) != 0) {
495 wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce received");
503 static int wps_process_uuid_r(struct wps_data *wps, const u8 *uuid_r)
505 if (uuid_r == NULL) {
506 wpa_printf(MSG_DEBUG, "WPS: No UUID-R received");
510 os_memcpy(wps->uuid_r, uuid_r, WPS_UUID_LEN);
511 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
517 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
520 if (pk == NULL || pk_len == 0) {
521 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
525 #ifdef CONFIG_WPS_OOB
526 if (wps->dev_pw_id != DEV_PW_DEFAULT &&
527 wps->wps->oob_conf.pubkey_hash) {
529 u8 hash[WPS_HASH_LEN];
532 sha256_vector(1, addr, &pk_len, hash);
534 wpabuf_head(wps->wps->oob_conf.pubkey_hash),
535 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
536 wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
540 #endif /* CONFIG_WPS_OOB */
542 wpabuf_free(wps->dh_pubkey_r);
543 wps->dh_pubkey_r = wpabuf_alloc_copy(pk, pk_len);
544 if (wps->dh_pubkey_r == NULL)
547 if (wps_derive_keys(wps) < 0)
554 static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1)
556 if (r_hash1 == NULL) {
557 wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received");
561 os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN);
562 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN);
568 static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2)
570 if (r_hash2 == NULL) {
571 wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received");
575 os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN);
576 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN);
582 static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
584 u8 hash[SHA256_MAC_LEN];
588 if (r_snonce1 == NULL) {
589 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
593 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1,
594 WPS_SECRET_NONCE_LEN);
596 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
598 len[0] = WPS_SECRET_NONCE_LEN;
600 len[1] = WPS_PSK_LEN;
601 addr[2] = wpabuf_head(wps->dh_pubkey_e);
602 len[2] = wpabuf_len(wps->dh_pubkey_e);
603 addr[3] = wpabuf_head(wps->dh_pubkey_r);
604 len[3] = wpabuf_len(wps->dh_pubkey_r);
605 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
607 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
608 wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does "
609 "not match with the pre-committed value");
610 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
611 wps_pwd_auth_fail_event(wps->wps, 1, 1);
615 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
616 "half of the device password");
622 static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
624 u8 hash[SHA256_MAC_LEN];
628 if (r_snonce2 == NULL) {
629 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
633 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2,
634 WPS_SECRET_NONCE_LEN);
636 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
638 len[0] = WPS_SECRET_NONCE_LEN;
640 len[1] = WPS_PSK_LEN;
641 addr[2] = wpabuf_head(wps->dh_pubkey_e);
642 len[2] = wpabuf_len(wps->dh_pubkey_e);
643 addr[3] = wpabuf_head(wps->dh_pubkey_r);
644 len[3] = wpabuf_len(wps->dh_pubkey_r);
645 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
647 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
648 wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does "
649 "not match with the pre-committed value");
650 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
651 wps_pwd_auth_fail_event(wps->wps, 1, 2);
655 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
656 "half of the device password");
662 static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
663 size_t cred_len, int wps2)
665 struct wps_parse_attr attr;
668 wpa_printf(MSG_DEBUG, "WPS: Received Credential");
669 os_memset(&wps->cred, 0, sizeof(wps->cred));
670 wpabuf_set(&msg, cred, cred_len);
671 if (wps_parse_msg(&msg, &attr) < 0 ||
672 wps_process_cred(&attr, &wps->cred))
675 if (os_memcmp(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) !=
677 wpa_printf(MSG_DEBUG, "WPS: MAC Address in the Credential ("
678 MACSTR ") does not match with own address (" MACSTR
679 ")", MAC2STR(wps->cred.mac_addr),
680 MAC2STR(wps->wps->dev.mac_addr));
682 * In theory, this could be consider fatal error, but there are
683 * number of deployed implementations using other address here
684 * due to unclarity in the specification. For interoperability
685 * reasons, allow this to be processed since we do not really
686 * use the MAC Address information for anything.
688 #ifdef CONFIG_WPS_STRICT
690 wpa_printf(MSG_INFO, "WPS: Do not accept incorrect "
691 "MAC Address in AP Settings");
694 #endif /* CONFIG_WPS_STRICT */
698 if (!(wps->cred.encr_type &
699 (WPS_ENCR_NONE | WPS_ENCR_TKIP | WPS_ENCR_AES))) {
700 if (wps->cred.encr_type & WPS_ENCR_WEP) {
701 wpa_printf(MSG_INFO, "WPS: Reject Credential "
702 "due to WEP configuration");
703 wps->error_indication = WPS_EI_SECURITY_WEP_PROHIBITED;
707 wpa_printf(MSG_INFO, "WPS: Reject Credential due to "
708 "invalid encr_type 0x%x", wps->cred.encr_type);
711 #endif /* CONFIG_WPS2 */
713 if (wps->wps->cred_cb) {
714 wps->cred.cred_attr = cred - 4;
715 wps->cred.cred_attr_len = cred_len + 4;
716 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
717 wps->cred.cred_attr = NULL;
718 wps->cred.cred_attr_len = 0;
725 static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
726 size_t cred_len[], size_t num_cred, int wps2)
735 wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
740 for (i = 0; i < num_cred; i++) {
742 res = wps_process_cred_e(wps, cred[i], cred_len[i], wps2);
746 wpa_printf(MSG_DEBUG, "WPS: WEP credential skipped");
752 wpa_printf(MSG_DEBUG, "WPS: No valid Credential attribute "
761 static int wps_process_ap_settings_e(struct wps_data *wps,
762 struct wps_parse_attr *attr,
763 struct wpabuf *attrs, int wps2)
765 struct wps_credential cred;
770 if (wps_process_ap_settings(attr, &cred) < 0)
773 wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
776 if (os_memcmp(cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) !=
778 wpa_printf(MSG_DEBUG, "WPS: MAC Address in the AP Settings ("
779 MACSTR ") does not match with own address (" MACSTR
780 ")", MAC2STR(cred.mac_addr),
781 MAC2STR(wps->wps->dev.mac_addr));
783 * In theory, this could be consider fatal error, but there are
784 * number of deployed implementations using other address here
785 * due to unclarity in the specification. For interoperability
786 * reasons, allow this to be processed since we do not really
787 * use the MAC Address information for anything.
789 #ifdef CONFIG_WPS_STRICT
791 wpa_printf(MSG_INFO, "WPS: Do not accept incorrect "
792 "MAC Address in AP Settings");
795 #endif /* CONFIG_WPS_STRICT */
799 if (!(cred.encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP | WPS_ENCR_AES)))
801 if (cred.encr_type & WPS_ENCR_WEP) {
802 wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
803 "due to WEP configuration");
804 wps->error_indication = WPS_EI_SECURITY_WEP_PROHIBITED;
808 wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
809 "invalid encr_type 0x%x", cred.encr_type);
812 #endif /* CONFIG_WPS2 */
814 #ifdef CONFIG_WPS_STRICT
816 if ((cred.encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
818 (cred.auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
820 wpa_printf(MSG_INFO, "WPS-STRICT: Invalid WSC 2.0 "
821 "AP Settings: WPA-Personal/TKIP only");
822 wps->error_indication =
823 WPS_EI_SECURITY_TKIP_ONLY_PROHIBITED;
827 #endif /* CONFIG_WPS_STRICT */
830 if ((cred.encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) == WPS_ENCR_TKIP)
832 wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
834 cred.encr_type |= WPS_ENCR_AES;
837 if ((cred.auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
839 wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
841 cred.auth_type |= WPS_AUTH_WPA2PSK;
843 #endif /* CONFIG_WPS2 */
845 if (wps->wps->cred_cb) {
846 cred.cred_attr = wpabuf_head(attrs);
847 cred.cred_attr_len = wpabuf_len(attrs);
848 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
855 static enum wps_process_res wps_process_m2(struct wps_data *wps,
856 const struct wpabuf *msg,
857 struct wps_parse_attr *attr)
859 wpa_printf(MSG_DEBUG, "WPS: Received M2");
861 if (wps->state != RECV_M2) {
862 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
863 "receiving M2", wps->state);
864 wps->state = SEND_WSC_NACK;
868 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
869 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
870 wps_process_uuid_r(wps, attr->uuid_r)) {
871 wps->state = SEND_WSC_NACK;
876 * Stop here on an AP as an Enrollee if AP Setup is locked unless the
877 * special locked mode is used to allow protocol run up to M7 in order
878 * to support external Registrars that only learn the current AP
879 * configuration without changing it.
882 ((wps->wps->ap_setup_locked && wps->wps->ap_setup_locked != 2) ||
883 wps->dev_password == NULL)) {
884 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
885 "registration of a new Registrar");
886 wps->config_error = WPS_CFG_SETUP_LOCKED;
887 wps->state = SEND_WSC_NACK;
891 if (wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
892 wps_process_authenticator(wps, attr->authenticator, msg) ||
893 wps_process_device_attrs(&wps->peer_dev, attr)) {
894 wps->state = SEND_WSC_NACK;
898 wps->state = SEND_M3;
903 static enum wps_process_res wps_process_m2d(struct wps_data *wps,
904 struct wps_parse_attr *attr)
906 wpa_printf(MSG_DEBUG, "WPS: Received M2D");
908 if (wps->state != RECV_M2) {
909 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
910 "receiving M2D", wps->state);
911 wps->state = SEND_WSC_NACK;
915 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
916 attr->manufacturer, attr->manufacturer_len);
917 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
918 attr->model_name, attr->model_name_len);
919 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
920 attr->model_number, attr->model_number_len);
921 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
922 attr->serial_number, attr->serial_number_len);
923 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
924 attr->dev_name, attr->dev_name_len);
926 if (wps->wps->event_cb) {
927 union wps_event_data data;
928 struct wps_event_m2d *m2d = &data.m2d;
929 os_memset(&data, 0, sizeof(data));
930 if (attr->config_methods)
931 m2d->config_methods =
932 WPA_GET_BE16(attr->config_methods);
933 m2d->manufacturer = attr->manufacturer;
934 m2d->manufacturer_len = attr->manufacturer_len;
935 m2d->model_name = attr->model_name;
936 m2d->model_name_len = attr->model_name_len;
937 m2d->model_number = attr->model_number;
938 m2d->model_number_len = attr->model_number_len;
939 m2d->serial_number = attr->serial_number;
940 m2d->serial_number_len = attr->serial_number_len;
941 m2d->dev_name = attr->dev_name;
942 m2d->dev_name_len = attr->dev_name_len;
943 m2d->primary_dev_type = attr->primary_dev_type;
944 if (attr->config_error)
946 WPA_GET_BE16(attr->config_error);
947 if (attr->dev_password_id)
948 m2d->dev_password_id =
949 WPA_GET_BE16(attr->dev_password_id);
950 wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_M2D, &data);
953 wps->state = RECEIVED_M2D;
958 static enum wps_process_res wps_process_m4(struct wps_data *wps,
959 const struct wpabuf *msg,
960 struct wps_parse_attr *attr)
962 struct wpabuf *decrypted;
963 struct wps_parse_attr eattr;
965 wpa_printf(MSG_DEBUG, "WPS: Received M4");
967 if (wps->state != RECV_M4) {
968 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
969 "receiving M4", wps->state);
970 wps->state = SEND_WSC_NACK;
974 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
975 wps_process_authenticator(wps, attr->authenticator, msg) ||
976 wps_process_r_hash1(wps, attr->r_hash1) ||
977 wps_process_r_hash2(wps, attr->r_hash2)) {
978 wps->state = SEND_WSC_NACK;
982 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
983 attr->encr_settings_len);
984 if (decrypted == NULL) {
985 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
986 "Settings attribute");
987 wps->state = SEND_WSC_NACK;
991 if (wps_validate_m4_encr(decrypted, attr->version2 != NULL) < 0) {
992 wpabuf_free(decrypted);
993 wps->state = SEND_WSC_NACK;
997 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
999 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1000 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1001 wps_process_r_snonce1(wps, eattr.r_snonce1)) {
1002 wpabuf_free(decrypted);
1003 wps->state = SEND_WSC_NACK;
1004 return WPS_CONTINUE;
1006 wpabuf_free(decrypted);
1008 wps->state = SEND_M5;
1009 return WPS_CONTINUE;
1013 static enum wps_process_res wps_process_m6(struct wps_data *wps,
1014 const struct wpabuf *msg,
1015 struct wps_parse_attr *attr)
1017 struct wpabuf *decrypted;
1018 struct wps_parse_attr eattr;
1020 wpa_printf(MSG_DEBUG, "WPS: Received M6");
1022 if (wps->state != RECV_M6) {
1023 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1024 "receiving M6", wps->state);
1025 wps->state = SEND_WSC_NACK;
1026 return WPS_CONTINUE;
1029 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1030 wps_process_authenticator(wps, attr->authenticator, msg)) {
1031 wps->state = SEND_WSC_NACK;
1032 return WPS_CONTINUE;
1035 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1036 attr->encr_settings_len);
1037 if (decrypted == NULL) {
1038 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1039 "Settings attribute");
1040 wps->state = SEND_WSC_NACK;
1041 return WPS_CONTINUE;
1044 if (wps_validate_m6_encr(decrypted, attr->version2 != NULL) < 0) {
1045 wpabuf_free(decrypted);
1046 wps->state = SEND_WSC_NACK;
1047 return WPS_CONTINUE;
1050 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1052 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1053 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1054 wps_process_r_snonce2(wps, eattr.r_snonce2)) {
1055 wpabuf_free(decrypted);
1056 wps->state = SEND_WSC_NACK;
1057 return WPS_CONTINUE;
1059 wpabuf_free(decrypted);
1062 wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_AP_PIN_SUCCESS,
1065 wps->state = SEND_M7;
1066 return WPS_CONTINUE;
1070 static enum wps_process_res wps_process_m8(struct wps_data *wps,
1071 const struct wpabuf *msg,
1072 struct wps_parse_attr *attr)
1074 struct wpabuf *decrypted;
1075 struct wps_parse_attr eattr;
1077 wpa_printf(MSG_DEBUG, "WPS: Received M8");
1079 if (wps->state != RECV_M8) {
1080 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1081 "receiving M8", wps->state);
1082 wps->state = SEND_WSC_NACK;
1083 return WPS_CONTINUE;
1086 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1087 wps_process_authenticator(wps, attr->authenticator, msg)) {
1088 wps->state = SEND_WSC_NACK;
1089 return WPS_CONTINUE;
1092 if (wps->wps->ap && wps->wps->ap_setup_locked) {
1094 * Stop here if special ap_setup_locked == 2 mode allowed the
1095 * protocol to continue beyond M2. This allows ER to learn the
1096 * current AP settings without changing them.
1098 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
1099 "registration of a new Registrar");
1100 wps->config_error = WPS_CFG_SETUP_LOCKED;
1101 wps->state = SEND_WSC_NACK;
1102 return WPS_CONTINUE;
1105 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1106 attr->encr_settings_len);
1107 if (decrypted == NULL) {
1108 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1109 "Settings attribute");
1110 wps->state = SEND_WSC_NACK;
1111 return WPS_CONTINUE;
1114 if (wps_validate_m8_encr(decrypted, wps->wps->ap,
1115 attr->version2 != NULL) < 0) {
1116 wpabuf_free(decrypted);
1117 wps->state = SEND_WSC_NACK;
1118 return WPS_CONTINUE;
1121 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1123 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1124 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1125 wps_process_creds(wps, eattr.cred, eattr.cred_len,
1126 eattr.num_cred, attr->version2 != NULL) ||
1127 wps_process_ap_settings_e(wps, &eattr, decrypted,
1128 attr->version2 != NULL)) {
1129 wpabuf_free(decrypted);
1130 wps->state = SEND_WSC_NACK;
1131 return WPS_CONTINUE;
1133 wpabuf_free(decrypted);
1135 wps->state = WPS_MSG_DONE;
1136 return WPS_CONTINUE;
1140 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1141 const struct wpabuf *msg)
1143 struct wps_parse_attr attr;
1144 enum wps_process_res ret = WPS_CONTINUE;
1146 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1148 if (wps_parse_msg(msg, &attr) < 0)
1151 if (attr.enrollee_nonce == NULL ||
1152 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1153 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1157 if (attr.msg_type == NULL) {
1158 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1159 wps->state = SEND_WSC_NACK;
1160 return WPS_CONTINUE;
1163 switch (*attr.msg_type) {
1165 if (wps_validate_m2(msg) < 0)
1167 ret = wps_process_m2(wps, msg, &attr);
1170 if (wps_validate_m2d(msg) < 0)
1172 ret = wps_process_m2d(wps, &attr);
1175 if (wps_validate_m4(msg) < 0)
1177 ret = wps_process_m4(wps, msg, &attr);
1178 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1179 wps_fail_event(wps->wps, WPS_M4, wps->config_error,
1180 wps->error_indication);
1183 if (wps_validate_m6(msg) < 0)
1185 ret = wps_process_m6(wps, msg, &attr);
1186 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1187 wps_fail_event(wps->wps, WPS_M6, wps->config_error,
1188 wps->error_indication);
1191 if (wps_validate_m8(msg) < 0)
1193 ret = wps_process_m8(wps, msg, &attr);
1194 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1195 wps_fail_event(wps->wps, WPS_M8, wps->config_error,
1196 wps->error_indication);
1199 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1205 * Save a copy of the last message for Authenticator derivation if we
1206 * are continuing. However, skip M2D since it is not authenticated and
1207 * neither is the ACK/NACK response frame. This allows the possibly
1208 * following M2 to be processed correctly by using the previously sent
1209 * M1 in Authenticator derivation.
1211 if (ret == WPS_CONTINUE && *attr.msg_type != WPS_M2D) {
1212 /* Save a copy of the last message for Authenticator derivation
1214 wpabuf_free(wps->last_msg);
1215 wps->last_msg = wpabuf_dup(msg);
1222 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1223 const struct wpabuf *msg)
1225 struct wps_parse_attr attr;
1227 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1229 if (wps_parse_msg(msg, &attr) < 0)
1232 if (attr.msg_type == NULL) {
1233 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1237 if (*attr.msg_type != WPS_WSC_ACK) {
1238 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1243 if (attr.registrar_nonce == NULL ||
1244 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1246 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1250 if (attr.enrollee_nonce == NULL ||
1251 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1252 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1256 if (wps->state == RECV_ACK && wps->wps->ap) {
1257 wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
1258 "completed successfully");
1259 wps_success_event(wps->wps);
1260 wps->state = WPS_FINISHED;
1268 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1269 const struct wpabuf *msg)
1271 struct wps_parse_attr attr;
1274 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1276 if (wps_parse_msg(msg, &attr) < 0)
1279 if (attr.msg_type == NULL) {
1280 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1284 if (*attr.msg_type != WPS_WSC_NACK) {
1285 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1290 if (attr.registrar_nonce == NULL ||
1291 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1293 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1294 wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
1295 attr.registrar_nonce, WPS_NONCE_LEN);
1296 wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
1297 wps->nonce_r, WPS_NONCE_LEN);
1301 if (attr.enrollee_nonce == NULL ||
1302 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1303 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1304 wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
1305 attr.enrollee_nonce, WPS_NONCE_LEN);
1306 wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
1307 wps->nonce_e, WPS_NONCE_LEN);
1311 if (attr.config_error == NULL) {
1312 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1317 config_error = WPA_GET_BE16(attr.config_error);
1318 wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
1319 "Configuration Error %d", config_error);
1321 switch (wps->state) {
1323 wps_fail_event(wps->wps, WPS_M3, config_error,
1324 wps->error_indication);
1327 wps_fail_event(wps->wps, WPS_M5, config_error,
1328 wps->error_indication);
1331 wps_fail_event(wps->wps, WPS_M7, config_error,
1332 wps->error_indication);
1338 /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1339 * Enrollee is Authenticator */
1340 wps->state = SEND_WSC_NACK;
1346 enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps,
1347 enum wsc_op_code op_code,
1348 const struct wpabuf *msg)
1351 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1353 (unsigned long) wpabuf_len(msg), op_code);
1355 if (op_code == WSC_UPnP) {
1356 /* Determine the OpCode based on message type attribute */
1357 struct wps_parse_attr attr;
1358 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type) {
1359 if (*attr.msg_type == WPS_WSC_ACK)
1361 else if (*attr.msg_type == WPS_WSC_NACK)
1369 return wps_process_wsc_msg(wps, msg);
1371 if (wps_validate_wsc_ack(msg) < 0)
1373 return wps_process_wsc_ack(wps, msg);
1375 if (wps_validate_wsc_nack(msg) < 0)
1377 return wps_process_wsc_nack(wps, msg);
1379 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);