2 * Wi-Fi Protected Setup - Enrollee
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
18 #include "crypto/crypto.h"
19 #include "crypto/sha256.h"
21 #include "wps_dev_attr.h"
24 static int wps_build_mac_addr(struct wps_data *wps, struct wpabuf *msg)
26 wpa_printf(MSG_DEBUG, "WPS: * MAC Address");
27 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
28 wpabuf_put_be16(msg, ETH_ALEN);
29 wpabuf_put_data(msg, wps->mac_addr_e, ETH_ALEN);
34 static int wps_build_wps_state(struct wps_data *wps, struct wpabuf *msg)
38 state = wps->wps->wps_state;
40 state = WPS_STATE_NOT_CONFIGURED;
41 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
43 wpabuf_put_be16(msg, ATTR_WPS_STATE);
44 wpabuf_put_be16(msg, 1);
45 wpabuf_put_u8(msg, state);
50 static int wps_build_e_hash(struct wps_data *wps, struct wpabuf *msg)
56 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
58 wpa_hexdump(MSG_DEBUG, "WPS: E-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
59 wpa_hexdump(MSG_DEBUG, "WPS: E-S2",
60 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
62 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
63 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
68 wpa_printf(MSG_DEBUG, "WPS: * E-Hash1");
69 wpabuf_put_be16(msg, ATTR_E_HASH1);
70 wpabuf_put_be16(msg, SHA256_MAC_LEN);
71 hash = wpabuf_put(msg, SHA256_MAC_LEN);
72 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
73 addr[0] = wps->snonce;
74 len[0] = WPS_SECRET_NONCE_LEN;
77 addr[2] = wpabuf_head(wps->dh_pubkey_e);
78 len[2] = wpabuf_len(wps->dh_pubkey_e);
79 addr[3] = wpabuf_head(wps->dh_pubkey_r);
80 len[3] = wpabuf_len(wps->dh_pubkey_r);
81 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
82 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", hash, SHA256_MAC_LEN);
84 wpa_printf(MSG_DEBUG, "WPS: * E-Hash2");
85 wpabuf_put_be16(msg, ATTR_E_HASH2);
86 wpabuf_put_be16(msg, SHA256_MAC_LEN);
87 hash = wpabuf_put(msg, SHA256_MAC_LEN);
88 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
89 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
91 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
92 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", hash, SHA256_MAC_LEN);
98 static int wps_build_e_snonce1(struct wps_data *wps, struct wpabuf *msg)
100 wpa_printf(MSG_DEBUG, "WPS: * E-SNonce1");
101 wpabuf_put_be16(msg, ATTR_E_SNONCE1);
102 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
103 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
108 static int wps_build_e_snonce2(struct wps_data *wps, struct wpabuf *msg)
110 wpa_printf(MSG_DEBUG, "WPS: * E-SNonce2");
111 wpabuf_put_be16(msg, ATTR_E_SNONCE2);
112 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
113 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
114 WPS_SECRET_NONCE_LEN);
119 static struct wpabuf * wps_build_m1(struct wps_data *wps)
123 if (os_get_random(wps->nonce_e, WPS_NONCE_LEN) < 0)
125 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
126 wps->nonce_e, WPS_NONCE_LEN);
128 wpa_printf(MSG_DEBUG, "WPS: Building Message M1");
129 msg = wpabuf_alloc(1000);
133 if (wps_build_version(msg) ||
134 wps_build_msg_type(msg, WPS_M1) ||
135 wps_build_uuid_e(msg, wps->uuid_e) ||
136 wps_build_mac_addr(wps, msg) ||
137 wps_build_enrollee_nonce(wps, msg) ||
138 wps_build_public_key(wps, msg) ||
139 wps_build_auth_type_flags(wps, msg) ||
140 wps_build_encr_type_flags(wps, msg) ||
141 wps_build_conn_type_flags(wps, msg) ||
142 wps_build_config_methods(msg, wps->wps->config_methods) ||
143 wps_build_wps_state(wps, msg) ||
144 wps_build_device_attrs(&wps->wps->dev, msg) ||
145 wps_build_rf_bands(&wps->wps->dev, msg) ||
146 wps_build_assoc_state(wps, msg) ||
147 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
148 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
149 wps_build_os_version(&wps->wps->dev, msg) ||
150 wps_build_wfa_ext(msg, 0, NULL, 0)) {
155 wps->state = RECV_M2;
160 static struct wpabuf * wps_build_m3(struct wps_data *wps)
164 wpa_printf(MSG_DEBUG, "WPS: Building Message M3");
166 if (wps->dev_password == NULL) {
167 wpa_printf(MSG_DEBUG, "WPS: No Device Password available");
170 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
172 msg = wpabuf_alloc(1000);
176 if (wps_build_version(msg) ||
177 wps_build_msg_type(msg, WPS_M3) ||
178 wps_build_registrar_nonce(wps, msg) ||
179 wps_build_e_hash(wps, msg) ||
180 wps_build_wfa_ext(msg, 0, NULL, 0) ||
181 wps_build_authenticator(wps, msg)) {
186 wps->state = RECV_M4;
191 static struct wpabuf * wps_build_m5(struct wps_data *wps)
193 struct wpabuf *msg, *plain;
195 wpa_printf(MSG_DEBUG, "WPS: Building Message M5");
197 plain = wpabuf_alloc(200);
201 msg = wpabuf_alloc(1000);
207 if (wps_build_version(msg) ||
208 wps_build_msg_type(msg, WPS_M5) ||
209 wps_build_registrar_nonce(wps, msg) ||
210 wps_build_e_snonce1(wps, plain) ||
211 wps_build_key_wrap_auth(wps, plain) ||
212 wps_build_encr_settings(wps, msg, plain) ||
213 wps_build_wfa_ext(msg, 0, NULL, 0) ||
214 wps_build_authenticator(wps, msg)) {
221 wps->state = RECV_M6;
226 static int wps_build_cred_ssid(struct wps_data *wps, struct wpabuf *msg)
228 wpa_printf(MSG_DEBUG, "WPS: * SSID");
229 wpabuf_put_be16(msg, ATTR_SSID);
230 wpabuf_put_be16(msg, wps->wps->ssid_len);
231 wpabuf_put_data(msg, wps->wps->ssid, wps->wps->ssid_len);
236 static int wps_build_cred_auth_type(struct wps_data *wps, struct wpabuf *msg)
238 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type");
239 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
240 wpabuf_put_be16(msg, 2);
241 wpabuf_put_be16(msg, wps->wps->auth_types);
246 static int wps_build_cred_encr_type(struct wps_data *wps, struct wpabuf *msg)
248 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type");
249 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
250 wpabuf_put_be16(msg, 2);
251 wpabuf_put_be16(msg, wps->wps->encr_types);
256 static int wps_build_cred_network_key(struct wps_data *wps, struct wpabuf *msg)
258 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
259 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
260 wpabuf_put_be16(msg, wps->wps->network_key_len);
261 wpabuf_put_data(msg, wps->wps->network_key, wps->wps->network_key_len);
266 static int wps_build_cred_mac_addr(struct wps_data *wps, struct wpabuf *msg)
268 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (AP BSSID)");
269 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
270 wpabuf_put_be16(msg, ETH_ALEN);
271 wpabuf_put_data(msg, wps->wps->dev.mac_addr, ETH_ALEN);
276 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *plain)
278 if (wps->wps->ap_settings) {
279 wpa_printf(MSG_DEBUG, "WPS: * AP Settings (pre-configured)");
280 wpabuf_put_data(plain, wps->wps->ap_settings,
281 wps->wps->ap_settings_len);
285 return wps_build_cred_ssid(wps, plain) ||
286 wps_build_cred_mac_addr(wps, plain) ||
287 wps_build_cred_auth_type(wps, plain) ||
288 wps_build_cred_encr_type(wps, plain) ||
289 wps_build_cred_network_key(wps, plain);
293 static struct wpabuf * wps_build_m7(struct wps_data *wps)
295 struct wpabuf *msg, *plain;
297 wpa_printf(MSG_DEBUG, "WPS: Building Message M7");
299 plain = wpabuf_alloc(500 + wps->wps->ap_settings_len);
303 msg = wpabuf_alloc(1000 + wps->wps->ap_settings_len);
309 if (wps_build_version(msg) ||
310 wps_build_msg_type(msg, WPS_M7) ||
311 wps_build_registrar_nonce(wps, msg) ||
312 wps_build_e_snonce2(wps, plain) ||
313 (wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
314 wps_build_key_wrap_auth(wps, plain) ||
315 wps_build_encr_settings(wps, msg, plain) ||
316 wps_build_wfa_ext(msg, 0, NULL, 0) ||
317 wps_build_authenticator(wps, msg)) {
324 if (wps->wps->ap && wps->wps->registrar) {
326 * If the Registrar is only learning our current configuration,
327 * it may not continue protocol run to successful completion.
328 * Store information here to make sure it remains available.
330 wps_device_store(wps->wps->registrar, &wps->peer_dev,
334 wps->state = RECV_M8;
339 static struct wpabuf * wps_build_wsc_done(struct wps_data *wps)
343 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_Done");
345 msg = wpabuf_alloc(1000);
349 if (wps_build_version(msg) ||
350 wps_build_msg_type(msg, WPS_WSC_DONE) ||
351 wps_build_enrollee_nonce(wps, msg) ||
352 wps_build_registrar_nonce(wps, msg) ||
353 wps_build_wfa_ext(msg, 0, NULL, 0)) {
359 wps->state = RECV_ACK;
361 wps_success_event(wps->wps);
362 wps->state = WPS_FINISHED;
368 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
372 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
374 msg = wpabuf_alloc(1000);
378 if (wps_build_version(msg) ||
379 wps_build_msg_type(msg, WPS_WSC_ACK) ||
380 wps_build_enrollee_nonce(wps, msg) ||
381 wps_build_registrar_nonce(wps, msg) ||
382 wps_build_wfa_ext(msg, 0, NULL, 0)) {
391 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
395 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
397 msg = wpabuf_alloc(1000);
401 if (wps_build_version(msg) ||
402 wps_build_msg_type(msg, WPS_WSC_NACK) ||
403 wps_build_enrollee_nonce(wps, msg) ||
404 wps_build_registrar_nonce(wps, msg) ||
405 wps_build_config_error(msg, wps->config_error) ||
406 wps_build_wfa_ext(msg, 0, NULL, 0)) {
415 struct wpabuf * wps_enrollee_get_msg(struct wps_data *wps,
416 enum wsc_op_code *op_code)
420 switch (wps->state) {
422 msg = wps_build_m1(wps);
426 msg = wps_build_m3(wps);
430 msg = wps_build_m5(wps);
434 msg = wps_build_m7(wps);
439 msg = wps_build_wsc_nack(wps);
443 msg = wps_build_wsc_ack(wps);
446 /* Another M2/M2D may be received */
447 wps->state = RECV_M2;
451 msg = wps_build_wsc_nack(wps);
455 msg = wps_build_wsc_done(wps);
459 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
460 "a message", wps->state);
465 if (*op_code == WSC_MSG && msg) {
466 /* Save a copy of the last message for Authenticator derivation
468 wpabuf_free(wps->last_msg);
469 wps->last_msg = wpabuf_dup(msg);
476 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
478 if (r_nonce == NULL) {
479 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
483 os_memcpy(wps->nonce_r, r_nonce, WPS_NONCE_LEN);
484 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
485 wps->nonce_r, WPS_NONCE_LEN);
491 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
493 if (e_nonce == NULL) {
494 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
498 if (os_memcmp(wps->nonce_e, e_nonce, WPS_NONCE_LEN) != 0) {
499 wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce received");
507 static int wps_process_uuid_r(struct wps_data *wps, const u8 *uuid_r)
509 if (uuid_r == NULL) {
510 wpa_printf(MSG_DEBUG, "WPS: No UUID-R received");
514 os_memcpy(wps->uuid_r, uuid_r, WPS_UUID_LEN);
515 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
521 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
524 if (pk == NULL || pk_len == 0) {
525 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
529 #ifdef CONFIG_WPS_OOB
530 if (wps->dev_pw_id != DEV_PW_DEFAULT &&
531 wps->wps->oob_conf.pubkey_hash) {
533 u8 hash[WPS_HASH_LEN];
536 sha256_vector(1, addr, &pk_len, hash);
538 wpabuf_head(wps->wps->oob_conf.pubkey_hash),
539 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
540 wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
544 #endif /* CONFIG_WPS_OOB */
546 wpabuf_free(wps->dh_pubkey_r);
547 wps->dh_pubkey_r = wpabuf_alloc_copy(pk, pk_len);
548 if (wps->dh_pubkey_r == NULL)
551 if (wps_derive_keys(wps) < 0)
558 static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1)
560 if (r_hash1 == NULL) {
561 wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received");
565 os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN);
566 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN);
572 static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2)
574 if (r_hash2 == NULL) {
575 wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received");
579 os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN);
580 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN);
586 static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
588 u8 hash[SHA256_MAC_LEN];
592 if (r_snonce1 == NULL) {
593 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
597 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1,
598 WPS_SECRET_NONCE_LEN);
600 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
602 len[0] = WPS_SECRET_NONCE_LEN;
604 len[1] = WPS_PSK_LEN;
605 addr[2] = wpabuf_head(wps->dh_pubkey_e);
606 len[2] = wpabuf_len(wps->dh_pubkey_e);
607 addr[3] = wpabuf_head(wps->dh_pubkey_r);
608 len[3] = wpabuf_len(wps->dh_pubkey_r);
609 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
611 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
612 wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does "
613 "not match with the pre-committed value");
614 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
615 wps_pwd_auth_fail_event(wps->wps, 1, 1);
619 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
620 "half of the device password");
626 static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
628 u8 hash[SHA256_MAC_LEN];
632 if (r_snonce2 == NULL) {
633 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
637 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2,
638 WPS_SECRET_NONCE_LEN);
640 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
642 len[0] = WPS_SECRET_NONCE_LEN;
644 len[1] = WPS_PSK_LEN;
645 addr[2] = wpabuf_head(wps->dh_pubkey_e);
646 len[2] = wpabuf_len(wps->dh_pubkey_e);
647 addr[3] = wpabuf_head(wps->dh_pubkey_r);
648 len[3] = wpabuf_len(wps->dh_pubkey_r);
649 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
651 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
652 wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does "
653 "not match with the pre-committed value");
654 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
655 wps_pwd_auth_fail_event(wps->wps, 1, 2);
659 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
660 "half of the device password");
666 static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
667 size_t cred_len, int wps2)
669 struct wps_parse_attr attr;
672 wpa_printf(MSG_DEBUG, "WPS: Received Credential");
673 os_memset(&wps->cred, 0, sizeof(wps->cred));
674 wpabuf_set(&msg, cred, cred_len);
675 if (wps_parse_msg(&msg, &attr) < 0 ||
676 wps_process_cred(&attr, &wps->cred))
679 if (os_memcmp(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) !=
681 wpa_printf(MSG_DEBUG, "WPS: MAC Address in the Credential ("
682 MACSTR ") does not match with own address (" MACSTR
683 ")", MAC2STR(wps->cred.mac_addr),
684 MAC2STR(wps->wps->dev.mac_addr));
686 * In theory, this could be consider fatal error, but there are
687 * number of deployed implementations using other address here
688 * due to unclarity in the specification. For interoperability
689 * reasons, allow this to be processed since we do not really
690 * use the MAC Address information for anything.
692 #ifdef CONFIG_WPS_STRICT
694 wpa_printf(MSG_INFO, "WPS: Do not accept incorrect "
695 "MAC Address in AP Settings");
698 #endif /* CONFIG_WPS_STRICT */
702 if (!(wps->cred.encr_type &
703 (WPS_ENCR_NONE | WPS_ENCR_TKIP | WPS_ENCR_AES))) {
704 if (wps->cred.encr_type & WPS_ENCR_WEP) {
705 wpa_printf(MSG_INFO, "WPS: Reject Credential "
706 "due to WEP configuration");
710 wpa_printf(MSG_INFO, "WPS: Reject Credential due to "
711 "invalid encr_type 0x%x", wps->cred.encr_type);
714 #endif /* CONFIG_WPS2 */
716 if (wps->wps->cred_cb) {
717 wps->cred.cred_attr = cred - 4;
718 wps->cred.cred_attr_len = cred_len + 4;
719 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
720 wps->cred.cred_attr = NULL;
721 wps->cred.cred_attr_len = 0;
728 static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
729 size_t cred_len[], size_t num_cred, int wps2)
738 wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
743 for (i = 0; i < num_cred; i++) {
745 res = wps_process_cred_e(wps, cred[i], cred_len[i], wps2);
749 wpa_printf(MSG_DEBUG, "WPS: WEP credential skipped");
755 wpa_printf(MSG_DEBUG, "WPS: No valid Credential attribute "
764 static int wps_process_ap_settings_e(struct wps_data *wps,
765 struct wps_parse_attr *attr,
766 struct wpabuf *attrs, int wps2)
768 struct wps_credential cred;
773 if (wps_process_ap_settings(attr, &cred) < 0)
776 wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
779 if (os_memcmp(cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) !=
781 wpa_printf(MSG_DEBUG, "WPS: MAC Address in the AP Settings ("
782 MACSTR ") does not match with own address (" MACSTR
783 ")", MAC2STR(cred.mac_addr),
784 MAC2STR(wps->wps->dev.mac_addr));
786 * In theory, this could be consider fatal error, but there are
787 * number of deployed implementations using other address here
788 * due to unclarity in the specification. For interoperability
789 * reasons, allow this to be processed since we do not really
790 * use the MAC Address information for anything.
792 #ifdef CONFIG_WPS_STRICT
794 wpa_printf(MSG_INFO, "WPS: Do not accept incorrect "
795 "MAC Address in AP Settings");
798 #endif /* CONFIG_WPS_STRICT */
802 if (!(cred.encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP | WPS_ENCR_AES)))
804 if (cred.encr_type & WPS_ENCR_WEP) {
805 wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
806 "due to WEP configuration");
810 wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
811 "invalid encr_type 0x%x", cred.encr_type);
814 #endif /* CONFIG_WPS2 */
816 #ifdef CONFIG_WPS_STRICT
818 if ((cred.encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
820 (cred.auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
822 wpa_printf(MSG_INFO, "WPS-STRICT: Invalid WSC 2.0 "
823 "AP Settings: WPA-Personal/TKIP only");
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 (wps->wps->ap_setup_locked || wps->dev_password == NULL)) {
877 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
878 "registration of a new Registrar");
879 wps->config_error = WPS_CFG_SETUP_LOCKED;
880 wps->state = SEND_WSC_NACK;
884 if (wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
885 wps_process_authenticator(wps, attr->authenticator, msg) ||
886 wps_process_device_attrs(&wps->peer_dev, attr)) {
887 wps->state = SEND_WSC_NACK;
891 wps->state = SEND_M3;
896 static enum wps_process_res wps_process_m2d(struct wps_data *wps,
897 struct wps_parse_attr *attr)
899 wpa_printf(MSG_DEBUG, "WPS: Received M2D");
901 if (wps->state != RECV_M2) {
902 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
903 "receiving M2D", wps->state);
904 wps->state = SEND_WSC_NACK;
908 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
909 attr->manufacturer, attr->manufacturer_len);
910 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
911 attr->model_name, attr->model_name_len);
912 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
913 attr->model_number, attr->model_number_len);
914 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
915 attr->serial_number, attr->serial_number_len);
916 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
917 attr->dev_name, attr->dev_name_len);
919 if (wps->wps->event_cb) {
920 union wps_event_data data;
921 struct wps_event_m2d *m2d = &data.m2d;
922 os_memset(&data, 0, sizeof(data));
923 if (attr->config_methods)
924 m2d->config_methods =
925 WPA_GET_BE16(attr->config_methods);
926 m2d->manufacturer = attr->manufacturer;
927 m2d->manufacturer_len = attr->manufacturer_len;
928 m2d->model_name = attr->model_name;
929 m2d->model_name_len = attr->model_name_len;
930 m2d->model_number = attr->model_number;
931 m2d->model_number_len = attr->model_number_len;
932 m2d->serial_number = attr->serial_number;
933 m2d->serial_number_len = attr->serial_number_len;
934 m2d->dev_name = attr->dev_name;
935 m2d->dev_name_len = attr->dev_name_len;
936 m2d->primary_dev_type = attr->primary_dev_type;
937 if (attr->config_error)
939 WPA_GET_BE16(attr->config_error);
940 if (attr->dev_password_id)
941 m2d->dev_password_id =
942 WPA_GET_BE16(attr->dev_password_id);
943 wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_M2D, &data);
946 wps->state = RECEIVED_M2D;
951 static enum wps_process_res wps_process_m4(struct wps_data *wps,
952 const struct wpabuf *msg,
953 struct wps_parse_attr *attr)
955 struct wpabuf *decrypted;
956 struct wps_parse_attr eattr;
958 wpa_printf(MSG_DEBUG, "WPS: Received M4");
960 if (wps->state != RECV_M4) {
961 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
962 "receiving M4", wps->state);
963 wps->state = SEND_WSC_NACK;
967 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
968 wps_process_authenticator(wps, attr->authenticator, msg) ||
969 wps_process_r_hash1(wps, attr->r_hash1) ||
970 wps_process_r_hash2(wps, attr->r_hash2)) {
971 wps->state = SEND_WSC_NACK;
975 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
976 attr->encr_settings_len);
977 if (decrypted == NULL) {
978 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
979 "Settings attribute");
980 wps->state = SEND_WSC_NACK;
984 if (wps_validate_m4_encr(decrypted, attr->version2 != 0) < 0) {
985 wpabuf_free(decrypted);
986 wps->state = SEND_WSC_NACK;
990 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
992 if (wps_parse_msg(decrypted, &eattr) < 0 ||
993 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
994 wps_process_r_snonce1(wps, eattr.r_snonce1)) {
995 wpabuf_free(decrypted);
996 wps->state = SEND_WSC_NACK;
999 wpabuf_free(decrypted);
1001 wps->state = SEND_M5;
1002 return WPS_CONTINUE;
1006 static enum wps_process_res wps_process_m6(struct wps_data *wps,
1007 const struct wpabuf *msg,
1008 struct wps_parse_attr *attr)
1010 struct wpabuf *decrypted;
1011 struct wps_parse_attr eattr;
1013 wpa_printf(MSG_DEBUG, "WPS: Received M6");
1015 if (wps->state != RECV_M6) {
1016 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1017 "receiving M6", wps->state);
1018 wps->state = SEND_WSC_NACK;
1019 return WPS_CONTINUE;
1022 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1023 wps_process_authenticator(wps, attr->authenticator, msg)) {
1024 wps->state = SEND_WSC_NACK;
1025 return WPS_CONTINUE;
1028 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1029 attr->encr_settings_len);
1030 if (decrypted == NULL) {
1031 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1032 "Settings attribute");
1033 wps->state = SEND_WSC_NACK;
1034 return WPS_CONTINUE;
1037 if (wps_validate_m6_encr(decrypted, attr->version2 != 0) < 0) {
1038 wpabuf_free(decrypted);
1039 wps->state = SEND_WSC_NACK;
1040 return WPS_CONTINUE;
1043 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1045 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1046 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1047 wps_process_r_snonce2(wps, eattr.r_snonce2)) {
1048 wpabuf_free(decrypted);
1049 wps->state = SEND_WSC_NACK;
1050 return WPS_CONTINUE;
1052 wpabuf_free(decrypted);
1054 wps->state = SEND_M7;
1055 return WPS_CONTINUE;
1059 static enum wps_process_res wps_process_m8(struct wps_data *wps,
1060 const struct wpabuf *msg,
1061 struct wps_parse_attr *attr)
1063 struct wpabuf *decrypted;
1064 struct wps_parse_attr eattr;
1066 wpa_printf(MSG_DEBUG, "WPS: Received M8");
1068 if (wps->state != RECV_M8) {
1069 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1070 "receiving M8", wps->state);
1071 wps->state = SEND_WSC_NACK;
1072 return WPS_CONTINUE;
1075 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1076 wps_process_authenticator(wps, attr->authenticator, msg)) {
1077 wps->state = SEND_WSC_NACK;
1078 return WPS_CONTINUE;
1081 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1082 attr->encr_settings_len);
1083 if (decrypted == NULL) {
1084 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1085 "Settings attribute");
1086 wps->state = SEND_WSC_NACK;
1087 return WPS_CONTINUE;
1090 if (wps_validate_m8_encr(decrypted, wps->wps->ap, attr->version2 != 0)
1092 wpabuf_free(decrypted);
1093 wps->state = SEND_WSC_NACK;
1094 return WPS_CONTINUE;
1097 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1099 if (wps_parse_msg(decrypted, &eattr) < 0 ||
1100 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1101 wps_process_creds(wps, eattr.cred, eattr.cred_len,
1102 eattr.num_cred, attr->version2 != NULL) ||
1103 wps_process_ap_settings_e(wps, &eattr, decrypted,
1104 attr->version2 != NULL)) {
1105 wpabuf_free(decrypted);
1106 wps->state = SEND_WSC_NACK;
1107 return WPS_CONTINUE;
1109 wpabuf_free(decrypted);
1111 wps->state = WPS_MSG_DONE;
1112 return WPS_CONTINUE;
1116 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1117 const struct wpabuf *msg)
1119 struct wps_parse_attr attr;
1120 enum wps_process_res ret = WPS_CONTINUE;
1122 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1124 if (wps_parse_msg(msg, &attr) < 0)
1127 if (attr.enrollee_nonce == NULL ||
1128 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1129 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1133 if (attr.msg_type == NULL) {
1134 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1138 switch (*attr.msg_type) {
1140 if (wps_validate_m2(msg) < 0)
1142 ret = wps_process_m2(wps, msg, &attr);
1145 if (wps_validate_m2d(msg) < 0)
1147 ret = wps_process_m2d(wps, &attr);
1150 if (wps_validate_m4(msg) < 0)
1152 ret = wps_process_m4(wps, msg, &attr);
1153 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1154 wps_fail_event(wps->wps, WPS_M4, wps->config_error);
1157 if (wps_validate_m6(msg) < 0)
1159 ret = wps_process_m6(wps, msg, &attr);
1160 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1161 wps_fail_event(wps->wps, WPS_M6, wps->config_error);
1164 if (wps_validate_m8(msg) < 0)
1166 ret = wps_process_m8(wps, msg, &attr);
1167 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1168 wps_fail_event(wps->wps, WPS_M8, wps->config_error);
1171 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1177 * Save a copy of the last message for Authenticator derivation if we
1178 * are continuing. However, skip M2D since it is not authenticated and
1179 * neither is the ACK/NACK response frame. This allows the possibly
1180 * following M2 to be processed correctly by using the previously sent
1181 * M1 in Authenticator derivation.
1183 if (ret == WPS_CONTINUE && *attr.msg_type != WPS_M2D) {
1184 /* Save a copy of the last message for Authenticator derivation
1186 wpabuf_free(wps->last_msg);
1187 wps->last_msg = wpabuf_dup(msg);
1194 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1195 const struct wpabuf *msg)
1197 struct wps_parse_attr attr;
1199 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1201 if (wps_parse_msg(msg, &attr) < 0)
1204 if (attr.msg_type == NULL) {
1205 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1209 if (*attr.msg_type != WPS_WSC_ACK) {
1210 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1215 if (attr.registrar_nonce == NULL ||
1216 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1218 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1222 if (attr.enrollee_nonce == NULL ||
1223 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1224 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1228 if (wps->state == RECV_ACK && wps->wps->ap) {
1229 wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
1230 "completed successfully");
1231 wps_success_event(wps->wps);
1232 wps->state = WPS_FINISHED;
1240 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1241 const struct wpabuf *msg)
1243 struct wps_parse_attr attr;
1246 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1248 if (wps_parse_msg(msg, &attr) < 0)
1251 if (attr.msg_type == NULL) {
1252 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1256 if (*attr.msg_type != WPS_WSC_NACK) {
1257 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1262 if (attr.registrar_nonce == NULL ||
1263 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1265 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1266 wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
1267 attr.registrar_nonce, WPS_NONCE_LEN);
1268 wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
1269 wps->nonce_r, WPS_NONCE_LEN);
1273 if (attr.enrollee_nonce == NULL ||
1274 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1275 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1276 wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
1277 attr.enrollee_nonce, WPS_NONCE_LEN);
1278 wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
1279 wps->nonce_e, WPS_NONCE_LEN);
1283 if (attr.config_error == NULL) {
1284 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1289 config_error = WPA_GET_BE16(attr.config_error);
1290 wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
1291 "Configuration Error %d", config_error);
1293 switch (wps->state) {
1295 wps_fail_event(wps->wps, WPS_M3, config_error);
1298 wps_fail_event(wps->wps, WPS_M5, config_error);
1301 wps_fail_event(wps->wps, WPS_M7, config_error);
1307 /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1308 * Enrollee is Authenticator */
1309 wps->state = SEND_WSC_NACK;
1315 enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps,
1316 enum wsc_op_code op_code,
1317 const struct wpabuf *msg)
1320 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1322 (unsigned long) wpabuf_len(msg), op_code);
1324 if (op_code == WSC_UPnP) {
1325 /* Determine the OpCode based on message type attribute */
1326 struct wps_parse_attr attr;
1327 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type) {
1328 if (*attr.msg_type == WPS_WSC_ACK)
1330 else if (*attr.msg_type == WPS_WSC_NACK)
1338 return wps_process_wsc_msg(wps, msg);
1340 if (wps_validate_wsc_ack(msg) < 0)
1342 return wps_process_wsc_ack(wps, msg);
1344 if (wps_validate_wsc_nack(msg) < 0)
1346 return wps_process_wsc_nack(wps, msg);
1348 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);