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.
20 #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
, WPS_STATE_NOT_CONFIGURED
);
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
)
124 if (os_get_random(wps
->nonce_e
, WPS_NONCE_LEN
) < 0)
126 wpa_hexdump(MSG_DEBUG
, "WPS: Enrollee Nonce",
127 wps
->nonce_e
, WPS_NONCE_LEN
);
129 wpa_printf(MSG_DEBUG
, "WPS: Building Message M1");
130 msg
= wpabuf_alloc(1000);
134 methods
= WPS_CONFIG_LABEL
| WPS_CONFIG_DISPLAY
| WPS_CONFIG_KEYPAD
;
135 #ifdef CONFIG_WPS_UFD
136 methods
|= WPS_CONFIG_USBA
;
137 #endif /* CONFIG_WPS_UFD */
139 methods
|= WPS_CONFIG_PUSHBUTTON
;
141 if (wps_build_version(msg
) ||
142 wps_build_msg_type(msg
, WPS_M1
) ||
143 wps_build_uuid_e(msg
, wps
->uuid_e
) ||
144 wps_build_mac_addr(wps
, msg
) ||
145 wps_build_enrollee_nonce(wps
, msg
) ||
146 wps_build_public_key(wps
, msg
) ||
147 wps_build_auth_type_flags(wps
, msg
) ||
148 wps_build_encr_type_flags(wps
, msg
) ||
149 wps_build_conn_type_flags(wps
, msg
) ||
150 wps_build_config_methods(msg
, methods
) ||
151 wps_build_wps_state(wps
, msg
) ||
152 wps_build_device_attrs(&wps
->wps
->dev
, msg
) ||
153 wps_build_rf_bands(&wps
->wps
->dev
, msg
) ||
154 wps_build_assoc_state(wps
, msg
) ||
155 wps_build_dev_password_id(msg
, wps
->dev_pw_id
) ||
156 wps_build_config_error(msg
, WPS_CFG_NO_ERROR
) ||
157 wps_build_os_version(&wps
->wps
->dev
, msg
)) {
162 wps
->state
= RECV_M2
;
167 static struct wpabuf
* wps_build_m3(struct wps_data
*wps
)
171 wpa_printf(MSG_DEBUG
, "WPS: Building Message M3");
173 if (wps
->dev_password
== NULL
) {
174 wpa_printf(MSG_DEBUG
, "WPS: No Device Password available");
177 wps_derive_psk(wps
, wps
->dev_password
, wps
->dev_password_len
);
179 msg
= wpabuf_alloc(1000);
183 if (wps_build_version(msg
) ||
184 wps_build_msg_type(msg
, WPS_M3
) ||
185 wps_build_registrar_nonce(wps
, msg
) ||
186 wps_build_e_hash(wps
, msg
) ||
187 wps_build_authenticator(wps
, msg
)) {
192 wps
->state
= RECV_M4
;
197 static struct wpabuf
* wps_build_m5(struct wps_data
*wps
)
199 struct wpabuf
*msg
, *plain
;
201 wpa_printf(MSG_DEBUG
, "WPS: Building Message M5");
203 plain
= wpabuf_alloc(200);
207 msg
= wpabuf_alloc(1000);
213 if (wps_build_version(msg
) ||
214 wps_build_msg_type(msg
, WPS_M5
) ||
215 wps_build_registrar_nonce(wps
, msg
) ||
216 wps_build_e_snonce1(wps
, plain
) ||
217 wps_build_key_wrap_auth(wps
, plain
) ||
218 wps_build_encr_settings(wps
, msg
, plain
) ||
219 wps_build_authenticator(wps
, msg
)) {
226 wps
->state
= RECV_M6
;
231 static int wps_build_cred_ssid(struct wps_data
*wps
, struct wpabuf
*msg
)
233 wpa_printf(MSG_DEBUG
, "WPS: * SSID");
234 wpabuf_put_be16(msg
, ATTR_SSID
);
235 wpabuf_put_be16(msg
, wps
->wps
->ssid_len
);
236 wpabuf_put_data(msg
, wps
->wps
->ssid
, wps
->wps
->ssid_len
);
241 static int wps_build_cred_auth_type(struct wps_data
*wps
, struct wpabuf
*msg
)
243 wpa_printf(MSG_DEBUG
, "WPS: * Authentication Type");
244 wpabuf_put_be16(msg
, ATTR_AUTH_TYPE
);
245 wpabuf_put_be16(msg
, 2);
246 wpabuf_put_be16(msg
, wps
->wps
->auth_types
);
251 static int wps_build_cred_encr_type(struct wps_data
*wps
, struct wpabuf
*msg
)
253 wpa_printf(MSG_DEBUG
, "WPS: * Encryption Type");
254 wpabuf_put_be16(msg
, ATTR_ENCR_TYPE
);
255 wpabuf_put_be16(msg
, 2);
256 wpabuf_put_be16(msg
, wps
->wps
->encr_types
);
261 static int wps_build_cred_network_key(struct wps_data
*wps
, struct wpabuf
*msg
)
263 wpa_printf(MSG_DEBUG
, "WPS: * Network Key");
264 wpabuf_put_be16(msg
, ATTR_NETWORK_KEY
);
265 wpabuf_put_be16(msg
, wps
->wps
->network_key_len
);
266 wpabuf_put_data(msg
, wps
->wps
->network_key
, wps
->wps
->network_key_len
);
271 static int wps_build_cred_mac_addr(struct wps_data
*wps
, struct wpabuf
*msg
)
273 wpa_printf(MSG_DEBUG
, "WPS: * MAC Address (AP BSSID)");
274 wpabuf_put_be16(msg
, ATTR_MAC_ADDR
);
275 wpabuf_put_be16(msg
, ETH_ALEN
);
276 wpabuf_put_data(msg
, wps
->wps
->dev
.mac_addr
, ETH_ALEN
);
281 static int wps_build_ap_settings(struct wps_data
*wps
, struct wpabuf
*plain
)
283 if (wps
->wps
->ap_settings
) {
284 wpa_printf(MSG_DEBUG
, "WPS: * AP Settings (pre-configured)");
285 wpabuf_put_data(plain
, wps
->wps
->ap_settings
,
286 wps
->wps
->ap_settings_len
);
290 return wps_build_cred_ssid(wps
, plain
) ||
291 wps_build_cred_mac_addr(wps
, plain
) ||
292 wps_build_cred_auth_type(wps
, plain
) ||
293 wps_build_cred_encr_type(wps
, plain
) ||
294 wps_build_cred_network_key(wps
, plain
);
298 static struct wpabuf
* wps_build_m7(struct wps_data
*wps
)
300 struct wpabuf
*msg
, *plain
;
302 wpa_printf(MSG_DEBUG
, "WPS: Building Message M7");
304 plain
= wpabuf_alloc(500 + wps
->wps
->ap_settings_len
);
308 msg
= wpabuf_alloc(1000 + wps
->wps
->ap_settings_len
);
314 if (wps_build_version(msg
) ||
315 wps_build_msg_type(msg
, WPS_M7
) ||
316 wps_build_registrar_nonce(wps
, msg
) ||
317 wps_build_e_snonce2(wps
, plain
) ||
318 (wps
->wps
->ap
&& wps_build_ap_settings(wps
, plain
)) ||
319 wps_build_key_wrap_auth(wps
, plain
) ||
320 wps_build_encr_settings(wps
, msg
, plain
) ||
321 wps_build_authenticator(wps
, msg
)) {
328 wps
->state
= RECV_M8
;
333 static struct wpabuf
* wps_build_wsc_done(struct wps_data
*wps
)
337 wpa_printf(MSG_DEBUG
, "WPS: Building Message WSC_Done");
339 msg
= wpabuf_alloc(1000);
343 if (wps_build_version(msg
) ||
344 wps_build_msg_type(msg
, WPS_WSC_DONE
) ||
345 wps_build_enrollee_nonce(wps
, msg
) ||
346 wps_build_registrar_nonce(wps
, msg
)) {
352 wps
->state
= RECV_ACK
;
354 wps_success_event(wps
->wps
);
355 wps
->state
= WPS_FINISHED
;
361 static struct wpabuf
* wps_build_wsc_ack(struct wps_data
*wps
)
365 wpa_printf(MSG_DEBUG
, "WPS: Building Message WSC_ACK");
367 msg
= wpabuf_alloc(1000);
371 if (wps_build_version(msg
) ||
372 wps_build_msg_type(msg
, WPS_WSC_ACK
) ||
373 wps_build_enrollee_nonce(wps
, msg
) ||
374 wps_build_registrar_nonce(wps
, msg
)) {
383 static struct wpabuf
* wps_build_wsc_nack(struct wps_data
*wps
)
387 wpa_printf(MSG_DEBUG
, "WPS: Building Message WSC_NACK");
389 msg
= wpabuf_alloc(1000);
393 if (wps_build_version(msg
) ||
394 wps_build_msg_type(msg
, WPS_WSC_NACK
) ||
395 wps_build_enrollee_nonce(wps
, msg
) ||
396 wps_build_registrar_nonce(wps
, msg
) ||
397 wps_build_config_error(msg
, wps
->config_error
)) {
406 struct wpabuf
* wps_enrollee_get_msg(struct wps_data
*wps
,
407 enum wsc_op_code
*op_code
)
411 switch (wps
->state
) {
413 msg
= wps_build_m1(wps
);
417 msg
= wps_build_m3(wps
);
421 msg
= wps_build_m5(wps
);
425 msg
= wps_build_m7(wps
);
430 msg
= wps_build_wsc_nack(wps
);
434 msg
= wps_build_wsc_ack(wps
);
437 /* Another M2/M2D may be received */
438 wps
->state
= RECV_M2
;
442 msg
= wps_build_wsc_nack(wps
);
446 msg
= wps_build_wsc_done(wps
);
450 wpa_printf(MSG_DEBUG
, "WPS: Unsupported state %d for building "
451 "a message", wps
->state
);
456 if (*op_code
== WSC_MSG
&& msg
) {
457 /* Save a copy of the last message for Authenticator derivation
459 wpabuf_free(wps
->last_msg
);
460 wps
->last_msg
= wpabuf_dup(msg
);
467 static int wps_process_registrar_nonce(struct wps_data
*wps
, const u8
*r_nonce
)
469 if (r_nonce
== NULL
) {
470 wpa_printf(MSG_DEBUG
, "WPS: No Registrar Nonce received");
474 os_memcpy(wps
->nonce_r
, r_nonce
, WPS_NONCE_LEN
);
475 wpa_hexdump(MSG_DEBUG
, "WPS: Registrar Nonce",
476 wps
->nonce_r
, WPS_NONCE_LEN
);
482 static int wps_process_enrollee_nonce(struct wps_data
*wps
, const u8
*e_nonce
)
484 if (e_nonce
== NULL
) {
485 wpa_printf(MSG_DEBUG
, "WPS: No Enrollee Nonce received");
489 if (os_memcmp(wps
->nonce_e
, e_nonce
, WPS_NONCE_LEN
) != 0) {
490 wpa_printf(MSG_DEBUG
, "WPS: Invalid Enrollee Nonce received");
498 static int wps_process_uuid_r(struct wps_data
*wps
, const u8
*uuid_r
)
500 if (uuid_r
== NULL
) {
501 wpa_printf(MSG_DEBUG
, "WPS: No UUID-R received");
505 os_memcpy(wps
->uuid_r
, uuid_r
, WPS_UUID_LEN
);
506 wpa_hexdump(MSG_DEBUG
, "WPS: UUID-R", wps
->uuid_r
, WPS_UUID_LEN
);
512 static int wps_process_pubkey(struct wps_data
*wps
, const u8
*pk
,
515 if (pk
== NULL
|| pk_len
== 0) {
516 wpa_printf(MSG_DEBUG
, "WPS: No Public Key received");
520 if (wps
->wps
->oob_conf
.pubkey_hash
!= NULL
) {
522 u8 hash
[WPS_HASH_LEN
];
525 sha256_vector(1, addr
, &pk_len
, hash
);
527 wpabuf_head(wps
->wps
->oob_conf
.pubkey_hash
),
528 WPS_OOB_PUBKEY_HASH_LEN
) != 0) {
529 wpa_printf(MSG_ERROR
, "WPS: Public Key hash error");
534 wpabuf_free(wps
->dh_pubkey_r
);
535 wps
->dh_pubkey_r
= wpabuf_alloc_copy(pk
, pk_len
);
536 if (wps
->dh_pubkey_r
== NULL
)
539 if (wps_derive_keys(wps
) < 0)
542 if (wps
->request_type
== WPS_REQ_WLAN_MANAGER_REGISTRAR
&&
543 wps_derive_mgmt_keys(wps
) < 0)
550 static int wps_process_r_hash1(struct wps_data
*wps
, const u8
*r_hash1
)
552 if (r_hash1
== NULL
) {
553 wpa_printf(MSG_DEBUG
, "WPS: No R-Hash1 received");
557 os_memcpy(wps
->peer_hash1
, r_hash1
, WPS_HASH_LEN
);
558 wpa_hexdump(MSG_DEBUG
, "WPS: R-Hash1", wps
->peer_hash1
, WPS_HASH_LEN
);
564 static int wps_process_r_hash2(struct wps_data
*wps
, const u8
*r_hash2
)
566 if (r_hash2
== NULL
) {
567 wpa_printf(MSG_DEBUG
, "WPS: No R-Hash2 received");
571 os_memcpy(wps
->peer_hash2
, r_hash2
, WPS_HASH_LEN
);
572 wpa_hexdump(MSG_DEBUG
, "WPS: R-Hash2", wps
->peer_hash2
, WPS_HASH_LEN
);
578 static int wps_process_r_snonce1(struct wps_data
*wps
, const u8
*r_snonce1
)
580 u8 hash
[SHA256_MAC_LEN
];
584 if (r_snonce1
== NULL
) {
585 wpa_printf(MSG_DEBUG
, "WPS: No R-SNonce1 received");
589 wpa_hexdump_key(MSG_DEBUG
, "WPS: R-SNonce1", r_snonce1
,
590 WPS_SECRET_NONCE_LEN
);
592 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
594 len
[0] = WPS_SECRET_NONCE_LEN
;
596 len
[1] = WPS_PSK_LEN
;
597 addr
[2] = wpabuf_head(wps
->dh_pubkey_e
);
598 len
[2] = wpabuf_len(wps
->dh_pubkey_e
);
599 addr
[3] = wpabuf_head(wps
->dh_pubkey_r
);
600 len
[3] = wpabuf_len(wps
->dh_pubkey_r
);
601 hmac_sha256_vector(wps
->authkey
, WPS_AUTHKEY_LEN
, 4, addr
, len
, hash
);
603 if (os_memcmp(wps
->peer_hash1
, hash
, WPS_HASH_LEN
) != 0) {
604 wpa_printf(MSG_DEBUG
, "WPS: R-Hash1 derived from R-S1 does "
605 "not match with the pre-committed value");
606 wps
->config_error
= WPS_CFG_DEV_PASSWORD_AUTH_FAILURE
;
607 wps_pwd_auth_fail_event(wps
->wps
, 1, 1);
611 wpa_printf(MSG_DEBUG
, "WPS: Registrar proved knowledge of the first "
612 "half of the device password");
618 static int wps_process_r_snonce2(struct wps_data
*wps
, const u8
*r_snonce2
)
620 u8 hash
[SHA256_MAC_LEN
];
624 if (r_snonce2
== NULL
) {
625 wpa_printf(MSG_DEBUG
, "WPS: No R-SNonce2 received");
629 wpa_hexdump_key(MSG_DEBUG
, "WPS: R-SNonce2", r_snonce2
,
630 WPS_SECRET_NONCE_LEN
);
632 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
634 len
[0] = WPS_SECRET_NONCE_LEN
;
636 len
[1] = WPS_PSK_LEN
;
637 addr
[2] = wpabuf_head(wps
->dh_pubkey_e
);
638 len
[2] = wpabuf_len(wps
->dh_pubkey_e
);
639 addr
[3] = wpabuf_head(wps
->dh_pubkey_r
);
640 len
[3] = wpabuf_len(wps
->dh_pubkey_r
);
641 hmac_sha256_vector(wps
->authkey
, WPS_AUTHKEY_LEN
, 4, addr
, len
, hash
);
643 if (os_memcmp(wps
->peer_hash2
, hash
, WPS_HASH_LEN
) != 0) {
644 wpa_printf(MSG_DEBUG
, "WPS: R-Hash2 derived from R-S2 does "
645 "not match with the pre-committed value");
646 wps
->config_error
= WPS_CFG_DEV_PASSWORD_AUTH_FAILURE
;
647 wps_pwd_auth_fail_event(wps
->wps
, 1, 2);
651 wpa_printf(MSG_DEBUG
, "WPS: Registrar proved knowledge of the second "
652 "half of the device password");
658 static int wps_process_cred_e(struct wps_data
*wps
, const u8
*cred
,
661 struct wps_parse_attr attr
;
664 wpa_printf(MSG_DEBUG
, "WPS: Received Credential");
665 os_memset(&wps
->cred
, 0, sizeof(wps
->cred
));
666 wpabuf_set(&msg
, cred
, cred_len
);
667 if (wps_parse_msg(&msg
, &attr
) < 0 ||
668 wps_process_cred(&attr
, &wps
->cred
))
671 if (wps
->wps
->cred_cb
) {
672 wps
->cred
.cred_attr
= cred
- 4;
673 wps
->cred
.cred_attr_len
= cred_len
+ 4;
674 wps
->wps
->cred_cb(wps
->wps
->cb_ctx
, &wps
->cred
);
675 wps
->cred
.cred_attr
= NULL
;
676 wps
->cred
.cred_attr_len
= 0;
683 static int wps_process_creds(struct wps_data
*wps
, const u8
*cred
[],
684 size_t cred_len
[], size_t num_cred
)
692 wpa_printf(MSG_DEBUG
, "WPS: No Credential attributes "
697 for (i
= 0; i
< num_cred
; i
++) {
698 if (wps_process_cred_e(wps
, cred
[i
], cred_len
[i
]))
706 static int wps_process_ap_settings_e(struct wps_data
*wps
,
707 struct wps_parse_attr
*attr
,
708 struct wpabuf
*attrs
)
710 struct wps_credential cred
;
715 if (wps_process_ap_settings(attr
, &cred
) < 0)
718 wpa_printf(MSG_INFO
, "WPS: Received new AP configuration from "
721 if (wps
->wps
->cred_cb
) {
722 cred
.cred_attr
= wpabuf_head(attrs
);
723 cred
.cred_attr_len
= wpabuf_len(attrs
);
724 wps
->wps
->cred_cb(wps
->wps
->cb_ctx
, &cred
);
731 static enum wps_process_res
wps_process_m2(struct wps_data
*wps
,
732 const struct wpabuf
*msg
,
733 struct wps_parse_attr
*attr
)
735 wpa_printf(MSG_DEBUG
, "WPS: Received M2");
737 if (wps
->state
!= RECV_M2
) {
738 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
739 "receiving M2", wps
->state
);
740 wps
->state
= SEND_WSC_NACK
;
744 if (wps_process_registrar_nonce(wps
, attr
->registrar_nonce
) ||
745 wps_process_enrollee_nonce(wps
, attr
->enrollee_nonce
) ||
746 wps_process_uuid_r(wps
, attr
->uuid_r
) ||
747 wps_process_pubkey(wps
, attr
->public_key
, attr
->public_key_len
) ||
748 wps_process_authenticator(wps
, attr
->authenticator
, msg
)) {
749 wps
->state
= SEND_WSC_NACK
;
753 if (wps
->wps
->ap
&& wps
->wps
->ap_setup_locked
) {
754 wpa_printf(MSG_DEBUG
, "WPS: AP Setup is locked - refuse "
755 "registration of a new Registrar");
756 wps
->config_error
= WPS_CFG_SETUP_LOCKED
;
757 wps
->state
= SEND_WSC_NACK
;
761 wps
->state
= SEND_M3
;
766 static enum wps_process_res
wps_process_m2d(struct wps_data
*wps
,
767 struct wps_parse_attr
*attr
)
769 wpa_printf(MSG_DEBUG
, "WPS: Received M2D");
771 if (wps
->state
!= RECV_M2
) {
772 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
773 "receiving M2D", wps
->state
);
774 wps
->state
= SEND_WSC_NACK
;
778 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Manufacturer",
779 attr
->manufacturer
, attr
->manufacturer_len
);
780 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Model Name",
781 attr
->model_name
, attr
->model_name_len
);
782 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Model Number",
783 attr
->model_number
, attr
->model_number_len
);
784 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Serial Number",
785 attr
->serial_number
, attr
->serial_number_len
);
786 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Device Name",
787 attr
->dev_name
, attr
->dev_name_len
);
789 if (wps
->wps
->event_cb
) {
790 union wps_event_data data
;
791 struct wps_event_m2d
*m2d
= &data
.m2d
;
792 os_memset(&data
, 0, sizeof(data
));
793 if (attr
->config_methods
)
794 m2d
->config_methods
=
795 WPA_GET_BE16(attr
->config_methods
);
796 m2d
->manufacturer
= attr
->manufacturer
;
797 m2d
->manufacturer_len
= attr
->manufacturer_len
;
798 m2d
->model_name
= attr
->model_name
;
799 m2d
->model_name_len
= attr
->model_name_len
;
800 m2d
->model_number
= attr
->model_number
;
801 m2d
->model_number_len
= attr
->model_number_len
;
802 m2d
->serial_number
= attr
->serial_number
;
803 m2d
->serial_number_len
= attr
->serial_number_len
;
804 m2d
->dev_name
= attr
->dev_name
;
805 m2d
->dev_name_len
= attr
->dev_name_len
;
806 m2d
->primary_dev_type
= attr
->primary_dev_type
;
807 if (attr
->config_error
)
809 WPA_GET_BE16(attr
->config_error
);
810 if (attr
->dev_password_id
)
811 m2d
->dev_password_id
=
812 WPA_GET_BE16(attr
->dev_password_id
);
813 wps
->wps
->event_cb(wps
->wps
->cb_ctx
, WPS_EV_M2D
, &data
);
816 wps
->state
= RECEIVED_M2D
;
821 static enum wps_process_res
wps_process_m4(struct wps_data
*wps
,
822 const struct wpabuf
*msg
,
823 struct wps_parse_attr
*attr
)
825 struct wpabuf
*decrypted
;
826 struct wps_parse_attr eattr
;
828 wpa_printf(MSG_DEBUG
, "WPS: Received M4");
830 if (wps
->state
!= RECV_M4
) {
831 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
832 "receiving M4", wps
->state
);
833 wps
->state
= SEND_WSC_NACK
;
837 if (wps_process_enrollee_nonce(wps
, attr
->enrollee_nonce
) ||
838 wps_process_authenticator(wps
, attr
->authenticator
, msg
) ||
839 wps_process_r_hash1(wps
, attr
->r_hash1
) ||
840 wps_process_r_hash2(wps
, attr
->r_hash2
)) {
841 wps
->state
= SEND_WSC_NACK
;
845 decrypted
= wps_decrypt_encr_settings(wps
, attr
->encr_settings
,
846 attr
->encr_settings_len
);
847 if (decrypted
== NULL
) {
848 wpa_printf(MSG_DEBUG
, "WPS: Failed to decrypted Encrypted "
849 "Settings attribute");
850 wps
->state
= SEND_WSC_NACK
;
854 wpa_printf(MSG_DEBUG
, "WPS: Processing decrypted Encrypted Settings "
856 if (wps_parse_msg(decrypted
, &eattr
) < 0 ||
857 wps_process_key_wrap_auth(wps
, decrypted
, eattr
.key_wrap_auth
) ||
858 wps_process_r_snonce1(wps
, eattr
.r_snonce1
)) {
859 wpabuf_free(decrypted
);
860 wps
->state
= SEND_WSC_NACK
;
863 wpabuf_free(decrypted
);
865 wps
->state
= SEND_M5
;
870 static enum wps_process_res
wps_process_m6(struct wps_data
*wps
,
871 const struct wpabuf
*msg
,
872 struct wps_parse_attr
*attr
)
874 struct wpabuf
*decrypted
;
875 struct wps_parse_attr eattr
;
877 wpa_printf(MSG_DEBUG
, "WPS: Received M6");
879 if (wps
->state
!= RECV_M6
) {
880 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
881 "receiving M6", wps
->state
);
882 wps
->state
= SEND_WSC_NACK
;
886 if (wps_process_enrollee_nonce(wps
, attr
->enrollee_nonce
) ||
887 wps_process_authenticator(wps
, attr
->authenticator
, msg
)) {
888 wps
->state
= SEND_WSC_NACK
;
892 decrypted
= wps_decrypt_encr_settings(wps
, attr
->encr_settings
,
893 attr
->encr_settings_len
);
894 if (decrypted
== NULL
) {
895 wpa_printf(MSG_DEBUG
, "WPS: Failed to decrypted Encrypted "
896 "Settings attribute");
897 wps
->state
= SEND_WSC_NACK
;
901 wpa_printf(MSG_DEBUG
, "WPS: Processing decrypted Encrypted Settings "
903 if (wps_parse_msg(decrypted
, &eattr
) < 0 ||
904 wps_process_key_wrap_auth(wps
, decrypted
, eattr
.key_wrap_auth
) ||
905 wps_process_r_snonce2(wps
, eattr
.r_snonce2
)) {
906 wpabuf_free(decrypted
);
907 wps
->state
= SEND_WSC_NACK
;
910 wpabuf_free(decrypted
);
912 wps
->state
= SEND_M7
;
917 static enum wps_process_res
wps_process_m8(struct wps_data
*wps
,
918 const struct wpabuf
*msg
,
919 struct wps_parse_attr
*attr
)
921 struct wpabuf
*decrypted
;
922 struct wps_parse_attr eattr
;
924 wpa_printf(MSG_DEBUG
, "WPS: Received M8");
926 if (wps
->state
!= RECV_M8
) {
927 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
928 "receiving M8", wps
->state
);
929 wps
->state
= SEND_WSC_NACK
;
933 if (wps_process_enrollee_nonce(wps
, attr
->enrollee_nonce
) ||
934 wps_process_authenticator(wps
, attr
->authenticator
, msg
)) {
935 wps
->state
= SEND_WSC_NACK
;
939 decrypted
= wps_decrypt_encr_settings(wps
, attr
->encr_settings
,
940 attr
->encr_settings_len
);
941 if (decrypted
== NULL
) {
942 wpa_printf(MSG_DEBUG
, "WPS: Failed to decrypted Encrypted "
943 "Settings attribute");
944 wps
->state
= SEND_WSC_NACK
;
948 wpa_printf(MSG_DEBUG
, "WPS: Processing decrypted Encrypted Settings "
950 if (wps_parse_msg(decrypted
, &eattr
) < 0 ||
951 wps_process_key_wrap_auth(wps
, decrypted
, eattr
.key_wrap_auth
) ||
952 wps_process_creds(wps
, eattr
.cred
, eattr
.cred_len
,
954 wps_process_ap_settings_e(wps
, &eattr
, decrypted
)) {
955 wpabuf_free(decrypted
);
956 wps
->state
= SEND_WSC_NACK
;
959 wpabuf_free(decrypted
);
961 wps
->state
= WPS_MSG_DONE
;
966 static enum wps_process_res
wps_process_wsc_msg(struct wps_data
*wps
,
967 const struct wpabuf
*msg
)
969 struct wps_parse_attr attr
;
970 enum wps_process_res ret
= WPS_CONTINUE
;
972 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_MSG");
974 if (wps_parse_msg(msg
, &attr
) < 0)
977 if (!wps_version_supported(attr
.version
)) {
978 wpa_printf(MSG_DEBUG
, "WPS: Unsupported message version 0x%x",
979 attr
.version
? *attr
.version
: 0);
983 if (attr
.enrollee_nonce
== NULL
||
984 os_memcmp(wps
->nonce_e
, attr
.enrollee_nonce
, WPS_NONCE_LEN
!= 0)) {
985 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in enrollee nonce");
989 if (attr
.msg_type
== NULL
) {
990 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
994 switch (*attr
.msg_type
) {
996 ret
= wps_process_m2(wps
, msg
, &attr
);
999 ret
= wps_process_m2d(wps
, &attr
);
1002 ret
= wps_process_m4(wps
, msg
, &attr
);
1003 if (ret
== WPS_FAILURE
|| wps
->state
== SEND_WSC_NACK
)
1004 wps_fail_event(wps
->wps
, WPS_M4
);
1007 ret
= wps_process_m6(wps
, msg
, &attr
);
1008 if (ret
== WPS_FAILURE
|| wps
->state
== SEND_WSC_NACK
)
1009 wps_fail_event(wps
->wps
, WPS_M6
);
1012 ret
= wps_process_m8(wps
, msg
, &attr
);
1013 if (ret
== WPS_FAILURE
|| wps
->state
== SEND_WSC_NACK
)
1014 wps_fail_event(wps
->wps
, WPS_M8
);
1017 wpa_printf(MSG_DEBUG
, "WPS: Unsupported Message Type %d",
1023 * Save a copy of the last message for Authenticator derivation if we
1024 * are continuing. However, skip M2D since it is not authenticated and
1025 * neither is the ACK/NACK response frame. This allows the possibly
1026 * following M2 to be processed correctly by using the previously sent
1027 * M1 in Authenticator derivation.
1029 if (ret
== WPS_CONTINUE
&& *attr
.msg_type
!= WPS_M2D
) {
1030 /* Save a copy of the last message for Authenticator derivation
1032 wpabuf_free(wps
->last_msg
);
1033 wps
->last_msg
= wpabuf_dup(msg
);
1040 static enum wps_process_res
wps_process_wsc_ack(struct wps_data
*wps
,
1041 const struct wpabuf
*msg
)
1043 struct wps_parse_attr attr
;
1045 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_ACK");
1047 if (wps_parse_msg(msg
, &attr
) < 0)
1050 if (!wps_version_supported(attr
.version
)) {
1051 wpa_printf(MSG_DEBUG
, "WPS: Unsupported message version 0x%x",
1052 attr
.version
? *attr
.version
: 0);
1056 if (attr
.msg_type
== NULL
) {
1057 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
1061 if (*attr
.msg_type
!= WPS_WSC_ACK
) {
1062 wpa_printf(MSG_DEBUG
, "WPS: Invalid Message Type %d",
1067 if (attr
.registrar_nonce
== NULL
||
1068 os_memcmp(wps
->nonce_r
, attr
.registrar_nonce
, WPS_NONCE_LEN
!= 0))
1070 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in registrar nonce");
1074 if (attr
.enrollee_nonce
== NULL
||
1075 os_memcmp(wps
->nonce_e
, attr
.enrollee_nonce
, WPS_NONCE_LEN
!= 0)) {
1076 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in enrollee nonce");
1080 if (wps
->state
== RECV_ACK
&& wps
->wps
->ap
) {
1081 wpa_printf(MSG_DEBUG
, "WPS: External Registrar registration "
1082 "completed successfully");
1083 wps_success_event(wps
->wps
);
1084 wps
->state
= WPS_FINISHED
;
1092 static enum wps_process_res
wps_process_wsc_nack(struct wps_data
*wps
,
1093 const struct wpabuf
*msg
)
1095 struct wps_parse_attr attr
;
1097 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_NACK");
1099 if (wps_parse_msg(msg
, &attr
) < 0)
1102 if (!wps_version_supported(attr
.version
)) {
1103 wpa_printf(MSG_DEBUG
, "WPS: Unsupported message version 0x%x",
1104 attr
.version
? *attr
.version
: 0);
1108 if (attr
.msg_type
== NULL
) {
1109 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
1113 if (*attr
.msg_type
!= WPS_WSC_NACK
) {
1114 wpa_printf(MSG_DEBUG
, "WPS: Invalid Message Type %d",
1119 if (attr
.registrar_nonce
== NULL
||
1120 os_memcmp(wps
->nonce_r
, attr
.registrar_nonce
, WPS_NONCE_LEN
!= 0))
1122 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in registrar nonce");
1123 wpa_hexdump(MSG_DEBUG
, "WPS: Received Registrar Nonce",
1124 attr
.registrar_nonce
, WPS_NONCE_LEN
);
1125 wpa_hexdump(MSG_DEBUG
, "WPS: Expected Registrar Nonce",
1126 wps
->nonce_r
, WPS_NONCE_LEN
);
1130 if (attr
.enrollee_nonce
== NULL
||
1131 os_memcmp(wps
->nonce_e
, attr
.enrollee_nonce
, WPS_NONCE_LEN
!= 0)) {
1132 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in enrollee nonce");
1133 wpa_hexdump(MSG_DEBUG
, "WPS: Received Enrollee Nonce",
1134 attr
.enrollee_nonce
, WPS_NONCE_LEN
);
1135 wpa_hexdump(MSG_DEBUG
, "WPS: Expected Enrollee Nonce",
1136 wps
->nonce_e
, WPS_NONCE_LEN
);
1140 if (attr
.config_error
== NULL
) {
1141 wpa_printf(MSG_DEBUG
, "WPS: No Configuration Error attribute "
1146 wpa_printf(MSG_DEBUG
, "WPS: Registrar terminated negotiation with "
1147 "Configuration Error %d", WPA_GET_BE16(attr
.config_error
));
1149 switch (wps
->state
) {
1151 wps_fail_event(wps
->wps
, WPS_M3
);
1154 wps_fail_event(wps
->wps
, WPS_M5
);
1157 wps_fail_event(wps
->wps
, WPS_M7
);
1163 /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1164 * Enrollee is Authenticator */
1165 wps
->state
= SEND_WSC_NACK
;
1171 enum wps_process_res
wps_enrollee_process_msg(struct wps_data
*wps
,
1172 enum wsc_op_code op_code
,
1173 const struct wpabuf
*msg
)
1176 wpa_printf(MSG_DEBUG
, "WPS: Processing received message (len=%lu "
1178 (unsigned long) wpabuf_len(msg
), op_code
);
1183 return wps_process_wsc_msg(wps
, msg
);
1185 return wps_process_wsc_ack(wps
, msg
);
1187 return wps_process_wsc_nack(wps
, msg
);
1189 wpa_printf(MSG_DEBUG
, "WPS: Unsupported op_code %d", op_code
);