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"
23 static int wps_build_mac_addr(struct wps_data
*wps
, struct wpabuf
*msg
)
25 wpa_printf(MSG_DEBUG
, "WPS: * MAC Address");
26 wpabuf_put_be16(msg
, ATTR_MAC_ADDR
);
27 wpabuf_put_be16(msg
, ETH_ALEN
);
28 wpabuf_put_data(msg
, wps
->mac_addr_e
, ETH_ALEN
);
33 static int wps_build_wps_state(struct wps_data
*wps
, struct wpabuf
*msg
)
37 state
= wps
->wps
->wps_state
;
39 state
= WPS_STATE_NOT_CONFIGURED
;
40 wpa_printf(MSG_DEBUG
, "WPS: * Wi-Fi Protected Setup State (%d)",
42 wpabuf_put_be16(msg
, ATTR_WPS_STATE
);
43 wpabuf_put_be16(msg
, 1);
44 wpabuf_put_u8(msg
, state
);
49 static int wps_build_e_hash(struct wps_data
*wps
, struct wpabuf
*msg
)
55 if (os_get_random(wps
->snonce
, 2 * WPS_SECRET_NONCE_LEN
) < 0)
57 wpa_hexdump(MSG_DEBUG
, "WPS: E-S1", wps
->snonce
, WPS_SECRET_NONCE_LEN
);
58 wpa_hexdump(MSG_DEBUG
, "WPS: E-S2",
59 wps
->snonce
+ WPS_SECRET_NONCE_LEN
, WPS_SECRET_NONCE_LEN
);
61 if (wps
->dh_pubkey_e
== NULL
|| wps
->dh_pubkey_r
== NULL
) {
62 wpa_printf(MSG_DEBUG
, "WPS: DH public keys not available for "
67 wpa_printf(MSG_DEBUG
, "WPS: * E-Hash1");
68 wpabuf_put_be16(msg
, ATTR_E_HASH1
);
69 wpabuf_put_be16(msg
, SHA256_MAC_LEN
);
70 hash
= wpabuf_put(msg
, SHA256_MAC_LEN
);
71 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
72 addr
[0] = wps
->snonce
;
73 len
[0] = WPS_SECRET_NONCE_LEN
;
76 addr
[2] = wpabuf_head(wps
->dh_pubkey_e
);
77 len
[2] = wpabuf_len(wps
->dh_pubkey_e
);
78 addr
[3] = wpabuf_head(wps
->dh_pubkey_r
);
79 len
[3] = wpabuf_len(wps
->dh_pubkey_r
);
80 hmac_sha256_vector(wps
->authkey
, WPS_AUTHKEY_LEN
, 4, addr
, len
, hash
);
81 wpa_hexdump(MSG_DEBUG
, "WPS: E-Hash1", hash
, SHA256_MAC_LEN
);
83 wpa_printf(MSG_DEBUG
, "WPS: * E-Hash2");
84 wpabuf_put_be16(msg
, ATTR_E_HASH2
);
85 wpabuf_put_be16(msg
, SHA256_MAC_LEN
);
86 hash
= wpabuf_put(msg
, SHA256_MAC_LEN
);
87 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
88 addr
[0] = wps
->snonce
+ WPS_SECRET_NONCE_LEN
;
90 hmac_sha256_vector(wps
->authkey
, WPS_AUTHKEY_LEN
, 4, addr
, len
, hash
);
91 wpa_hexdump(MSG_DEBUG
, "WPS: E-Hash2", hash
, SHA256_MAC_LEN
);
97 static int wps_build_e_snonce1(struct wps_data
*wps
, struct wpabuf
*msg
)
99 wpa_printf(MSG_DEBUG
, "WPS: * E-SNonce1");
100 wpabuf_put_be16(msg
, ATTR_E_SNONCE1
);
101 wpabuf_put_be16(msg
, WPS_SECRET_NONCE_LEN
);
102 wpabuf_put_data(msg
, wps
->snonce
, WPS_SECRET_NONCE_LEN
);
107 static int wps_build_e_snonce2(struct wps_data
*wps
, struct wpabuf
*msg
)
109 wpa_printf(MSG_DEBUG
, "WPS: * E-SNonce2");
110 wpabuf_put_be16(msg
, ATTR_E_SNONCE2
);
111 wpabuf_put_be16(msg
, WPS_SECRET_NONCE_LEN
);
112 wpabuf_put_data(msg
, wps
->snonce
+ WPS_SECRET_NONCE_LEN
,
113 WPS_SECRET_NONCE_LEN
);
118 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 methods
= WPS_CONFIG_LABEL
| WPS_CONFIG_DISPLAY
| WPS_CONFIG_KEYPAD
;
135 methods
|= WPS_CONFIG_PUSHBUTTON
;
137 if (wps_build_version(msg
) ||
138 wps_build_msg_type(msg
, WPS_M1
) ||
139 wps_build_uuid_e(msg
, wps
->uuid_e
) ||
140 wps_build_mac_addr(wps
, msg
) ||
141 wps_build_enrollee_nonce(wps
, msg
) ||
142 wps_build_public_key(wps
, msg
) ||
143 wps_build_auth_type_flags(wps
, msg
) ||
144 wps_build_encr_type_flags(wps
, msg
) ||
145 wps_build_conn_type_flags(wps
, msg
) ||
146 wps_build_config_methods(msg
, methods
) ||
147 wps_build_wps_state(wps
, msg
) ||
148 wps_build_device_attrs(&wps
->wps
->dev
, msg
) ||
149 wps_build_rf_bands(&wps
->wps
->dev
, msg
) ||
150 wps_build_assoc_state(wps
, msg
) ||
151 wps_build_dev_password_id(msg
, wps
->dev_pw_id
) ||
152 wps_build_config_error(msg
, WPS_CFG_NO_ERROR
) ||
153 wps_build_os_version(&wps
->wps
->dev
, msg
)) {
158 wps
->state
= RECV_M2
;
163 static struct wpabuf
* wps_build_m3(struct wps_data
*wps
)
167 wpa_printf(MSG_DEBUG
, "WPS: Building Message M3");
169 if (wps
->dev_password
== NULL
) {
170 wpa_printf(MSG_DEBUG
, "WPS: No Device Password available");
173 wps_derive_psk(wps
, wps
->dev_password
, wps
->dev_password_len
);
175 msg
= wpabuf_alloc(1000);
179 if (wps_build_version(msg
) ||
180 wps_build_msg_type(msg
, WPS_M3
) ||
181 wps_build_registrar_nonce(wps
, msg
) ||
182 wps_build_e_hash(wps
, msg
) ||
183 wps_build_authenticator(wps
, msg
)) {
188 wps
->state
= RECV_M4
;
193 static struct wpabuf
* wps_build_m5(struct wps_data
*wps
)
195 struct wpabuf
*msg
, *plain
;
197 wpa_printf(MSG_DEBUG
, "WPS: Building Message M5");
199 plain
= wpabuf_alloc(200);
203 msg
= wpabuf_alloc(1000);
209 if (wps_build_version(msg
) ||
210 wps_build_msg_type(msg
, WPS_M5
) ||
211 wps_build_registrar_nonce(wps
, msg
) ||
212 wps_build_e_snonce1(wps
, plain
) ||
213 wps_build_key_wrap_auth(wps
, plain
) ||
214 wps_build_encr_settings(wps
, msg
, plain
) ||
215 wps_build_authenticator(wps
, msg
)) {
222 wps
->state
= RECV_M6
;
227 static int wps_build_cred_ssid(struct wps_data
*wps
, struct wpabuf
*msg
)
229 wpa_printf(MSG_DEBUG
, "WPS: * SSID");
230 wpabuf_put_be16(msg
, ATTR_SSID
);
231 wpabuf_put_be16(msg
, wps
->wps
->ssid_len
);
232 wpabuf_put_data(msg
, wps
->wps
->ssid
, wps
->wps
->ssid_len
);
237 static int wps_build_cred_auth_type(struct wps_data
*wps
, struct wpabuf
*msg
)
239 wpa_printf(MSG_DEBUG
, "WPS: * Authentication Type");
240 wpabuf_put_be16(msg
, ATTR_AUTH_TYPE
);
241 wpabuf_put_be16(msg
, 2);
242 wpabuf_put_be16(msg
, wps
->wps
->auth_types
);
247 static int wps_build_cred_encr_type(struct wps_data
*wps
, struct wpabuf
*msg
)
249 wpa_printf(MSG_DEBUG
, "WPS: * Encryption Type");
250 wpabuf_put_be16(msg
, ATTR_ENCR_TYPE
);
251 wpabuf_put_be16(msg
, 2);
252 wpabuf_put_be16(msg
, wps
->wps
->encr_types
);
257 static int wps_build_cred_network_key(struct wps_data
*wps
, struct wpabuf
*msg
)
259 wpa_printf(MSG_DEBUG
, "WPS: * Network Key");
260 wpabuf_put_be16(msg
, ATTR_NETWORK_KEY
);
261 wpabuf_put_be16(msg
, wps
->wps
->network_key_len
);
262 wpabuf_put_data(msg
, wps
->wps
->network_key
, wps
->wps
->network_key_len
);
267 static int wps_build_cred_mac_addr(struct wps_data
*wps
, struct wpabuf
*msg
)
269 wpa_printf(MSG_DEBUG
, "WPS: * MAC Address (AP BSSID)");
270 wpabuf_put_be16(msg
, ATTR_MAC_ADDR
);
271 wpabuf_put_be16(msg
, ETH_ALEN
);
272 wpabuf_put_data(msg
, wps
->wps
->dev
.mac_addr
, ETH_ALEN
);
277 static int wps_build_ap_settings(struct wps_data
*wps
, struct wpabuf
*plain
)
279 if (wps
->wps
->ap_settings
) {
280 wpa_printf(MSG_DEBUG
, "WPS: * AP Settings (pre-configured)");
281 wpabuf_put_data(plain
, wps
->wps
->ap_settings
,
282 wps
->wps
->ap_settings_len
);
286 return wps_build_cred_ssid(wps
, plain
) ||
287 wps_build_cred_mac_addr(wps
, plain
) ||
288 wps_build_cred_auth_type(wps
, plain
) ||
289 wps_build_cred_encr_type(wps
, plain
) ||
290 wps_build_cred_network_key(wps
, plain
);
294 static struct wpabuf
* wps_build_m7(struct wps_data
*wps
)
296 struct wpabuf
*msg
, *plain
;
298 wpa_printf(MSG_DEBUG
, "WPS: Building Message M7");
300 plain
= wpabuf_alloc(500 + wps
->wps
->ap_settings_len
);
304 msg
= wpabuf_alloc(1000 + wps
->wps
->ap_settings_len
);
310 if (wps_build_version(msg
) ||
311 wps_build_msg_type(msg
, WPS_M7
) ||
312 wps_build_registrar_nonce(wps
, msg
) ||
313 wps_build_e_snonce2(wps
, plain
) ||
314 (wps
->wps
->ap
&& wps_build_ap_settings(wps
, plain
)) ||
315 wps_build_key_wrap_auth(wps
, plain
) ||
316 wps_build_encr_settings(wps
, msg
, plain
) ||
317 wps_build_authenticator(wps
, msg
)) {
324 wps
->state
= RECV_M8
;
329 static struct wpabuf
* wps_build_wsc_done(struct wps_data
*wps
)
333 wpa_printf(MSG_DEBUG
, "WPS: Building Message WSC_Done");
335 msg
= wpabuf_alloc(1000);
339 if (wps_build_version(msg
) ||
340 wps_build_msg_type(msg
, WPS_WSC_DONE
) ||
341 wps_build_enrollee_nonce(wps
, msg
) ||
342 wps_build_registrar_nonce(wps
, msg
)) {
348 wps
->state
= RECV_ACK
;
350 wps_success_event(wps
->wps
);
351 wps
->state
= WPS_FINISHED
;
357 static struct wpabuf
* wps_build_wsc_ack(struct wps_data
*wps
)
361 wpa_printf(MSG_DEBUG
, "WPS: Building Message WSC_ACK");
363 msg
= wpabuf_alloc(1000);
367 if (wps_build_version(msg
) ||
368 wps_build_msg_type(msg
, WPS_WSC_ACK
) ||
369 wps_build_enrollee_nonce(wps
, msg
) ||
370 wps_build_registrar_nonce(wps
, msg
)) {
379 static struct wpabuf
* wps_build_wsc_nack(struct wps_data
*wps
)
383 wpa_printf(MSG_DEBUG
, "WPS: Building Message WSC_NACK");
385 msg
= wpabuf_alloc(1000);
389 if (wps_build_version(msg
) ||
390 wps_build_msg_type(msg
, WPS_WSC_NACK
) ||
391 wps_build_enrollee_nonce(wps
, msg
) ||
392 wps_build_registrar_nonce(wps
, msg
) ||
393 wps_build_config_error(msg
, wps
->config_error
)) {
402 struct wpabuf
* wps_enrollee_get_msg(struct wps_data
*wps
,
403 enum wsc_op_code
*op_code
)
407 switch (wps
->state
) {
409 msg
= wps_build_m1(wps
);
413 msg
= wps_build_m3(wps
);
417 msg
= wps_build_m5(wps
);
421 msg
= wps_build_m7(wps
);
426 msg
= wps_build_wsc_nack(wps
);
430 msg
= wps_build_wsc_ack(wps
);
433 /* Another M2/M2D may be received */
434 wps
->state
= RECV_M2
;
438 msg
= wps_build_wsc_nack(wps
);
442 msg
= wps_build_wsc_done(wps
);
446 wpa_printf(MSG_DEBUG
, "WPS: Unsupported state %d for building "
447 "a message", wps
->state
);
452 if (*op_code
== WSC_MSG
&& msg
) {
453 /* Save a copy of the last message for Authenticator derivation
455 wpabuf_free(wps
->last_msg
);
456 wps
->last_msg
= wpabuf_dup(msg
);
463 static int wps_process_registrar_nonce(struct wps_data
*wps
, const u8
*r_nonce
)
465 if (r_nonce
== NULL
) {
466 wpa_printf(MSG_DEBUG
, "WPS: No Registrar Nonce received");
470 os_memcpy(wps
->nonce_r
, r_nonce
, WPS_NONCE_LEN
);
471 wpa_hexdump(MSG_DEBUG
, "WPS: Registrar Nonce",
472 wps
->nonce_r
, WPS_NONCE_LEN
);
478 static int wps_process_enrollee_nonce(struct wps_data
*wps
, const u8
*e_nonce
)
480 if (e_nonce
== NULL
) {
481 wpa_printf(MSG_DEBUG
, "WPS: No Enrollee Nonce received");
485 if (os_memcmp(wps
->nonce_e
, e_nonce
, WPS_NONCE_LEN
) != 0) {
486 wpa_printf(MSG_DEBUG
, "WPS: Invalid Enrollee Nonce received");
494 static int wps_process_uuid_r(struct wps_data
*wps
, const u8
*uuid_r
)
496 if (uuid_r
== NULL
) {
497 wpa_printf(MSG_DEBUG
, "WPS: No UUID-R received");
501 os_memcpy(wps
->uuid_r
, uuid_r
, WPS_UUID_LEN
);
502 wpa_hexdump(MSG_DEBUG
, "WPS: UUID-R", wps
->uuid_r
, WPS_UUID_LEN
);
508 static int wps_process_pubkey(struct wps_data
*wps
, const u8
*pk
,
511 if (pk
== NULL
|| pk_len
== 0) {
512 wpa_printf(MSG_DEBUG
, "WPS: No Public Key received");
516 wpabuf_free(wps
->dh_pubkey_r
);
517 wps
->dh_pubkey_r
= wpabuf_alloc_copy(pk
, pk_len
);
518 if (wps
->dh_pubkey_r
== NULL
)
521 if (wps_derive_keys(wps
) < 0)
528 static int wps_process_r_hash1(struct wps_data
*wps
, const u8
*r_hash1
)
530 if (r_hash1
== NULL
) {
531 wpa_printf(MSG_DEBUG
, "WPS: No R-Hash1 received");
535 os_memcpy(wps
->peer_hash1
, r_hash1
, WPS_HASH_LEN
);
536 wpa_hexdump(MSG_DEBUG
, "WPS: R-Hash1", wps
->peer_hash1
, WPS_HASH_LEN
);
542 static int wps_process_r_hash2(struct wps_data
*wps
, const u8
*r_hash2
)
544 if (r_hash2
== NULL
) {
545 wpa_printf(MSG_DEBUG
, "WPS: No R-Hash2 received");
549 os_memcpy(wps
->peer_hash2
, r_hash2
, WPS_HASH_LEN
);
550 wpa_hexdump(MSG_DEBUG
, "WPS: R-Hash2", wps
->peer_hash2
, WPS_HASH_LEN
);
556 static int wps_process_r_snonce1(struct wps_data
*wps
, const u8
*r_snonce1
)
558 u8 hash
[SHA256_MAC_LEN
];
562 if (r_snonce1
== NULL
) {
563 wpa_printf(MSG_DEBUG
, "WPS: No R-SNonce1 received");
567 wpa_hexdump_key(MSG_DEBUG
, "WPS: R-SNonce1", r_snonce1
,
568 WPS_SECRET_NONCE_LEN
);
570 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
572 len
[0] = WPS_SECRET_NONCE_LEN
;
574 len
[1] = WPS_PSK_LEN
;
575 addr
[2] = wpabuf_head(wps
->dh_pubkey_e
);
576 len
[2] = wpabuf_len(wps
->dh_pubkey_e
);
577 addr
[3] = wpabuf_head(wps
->dh_pubkey_r
);
578 len
[3] = wpabuf_len(wps
->dh_pubkey_r
);
579 hmac_sha256_vector(wps
->authkey
, WPS_AUTHKEY_LEN
, 4, addr
, len
, hash
);
581 if (os_memcmp(wps
->peer_hash1
, hash
, WPS_HASH_LEN
) != 0) {
582 wpa_printf(MSG_DEBUG
, "WPS: R-Hash1 derived from R-S1 does "
583 "not match with the pre-committed value");
584 wps
->config_error
= WPS_CFG_DEV_PASSWORD_AUTH_FAILURE
;
585 wps_pwd_auth_fail_event(wps
->wps
, 1, 1);
589 wpa_printf(MSG_DEBUG
, "WPS: Registrar proved knowledge of the first "
590 "half of the device password");
596 static int wps_process_r_snonce2(struct wps_data
*wps
, const u8
*r_snonce2
)
598 u8 hash
[SHA256_MAC_LEN
];
602 if (r_snonce2
== NULL
) {
603 wpa_printf(MSG_DEBUG
, "WPS: No R-SNonce2 received");
607 wpa_hexdump_key(MSG_DEBUG
, "WPS: R-SNonce2", r_snonce2
,
608 WPS_SECRET_NONCE_LEN
);
610 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
612 len
[0] = WPS_SECRET_NONCE_LEN
;
614 len
[1] = WPS_PSK_LEN
;
615 addr
[2] = wpabuf_head(wps
->dh_pubkey_e
);
616 len
[2] = wpabuf_len(wps
->dh_pubkey_e
);
617 addr
[3] = wpabuf_head(wps
->dh_pubkey_r
);
618 len
[3] = wpabuf_len(wps
->dh_pubkey_r
);
619 hmac_sha256_vector(wps
->authkey
, WPS_AUTHKEY_LEN
, 4, addr
, len
, hash
);
621 if (os_memcmp(wps
->peer_hash2
, hash
, WPS_HASH_LEN
) != 0) {
622 wpa_printf(MSG_DEBUG
, "WPS: R-Hash2 derived from R-S2 does "
623 "not match with the pre-committed value");
624 wps
->config_error
= WPS_CFG_DEV_PASSWORD_AUTH_FAILURE
;
625 wps_pwd_auth_fail_event(wps
->wps
, 1, 2);
629 wpa_printf(MSG_DEBUG
, "WPS: Registrar proved knowledge of the second "
630 "half of the device password");
636 static int wps_process_cred_e(struct wps_data
*wps
, const u8
*cred
,
639 struct wps_parse_attr attr
;
642 wpa_printf(MSG_DEBUG
, "WPS: Received Credential");
643 os_memset(&wps
->cred
, 0, sizeof(wps
->cred
));
644 wpabuf_set(&msg
, cred
, cred_len
);
645 if (wps_parse_msg(&msg
, &attr
) < 0 ||
646 wps_process_cred(&attr
, &wps
->cred
))
649 if (os_memcmp(wps
->cred
.mac_addr
, wps
->wps
->dev
.mac_addr
, ETH_ALEN
) !=
651 wpa_printf(MSG_DEBUG
, "WPS: MAC Address in the Credential ("
652 MACSTR
") does not match with own address (" MACSTR
653 ")", MAC2STR(wps
->cred
.mac_addr
),
654 MAC2STR(wps
->wps
->dev
.mac_addr
));
656 * In theory, this could be consider fatal error, but there are
657 * number of deployed implementations using other address here
658 * due to unclarity in the specification. For interoperability
659 * reasons, allow this to be processed since we do not really
660 * use the MAC Address information for anything.
664 if (wps
->wps
->cred_cb
) {
665 wps
->cred
.cred_attr
= cred
- 4;
666 wps
->cred
.cred_attr_len
= cred_len
+ 4;
667 wps
->wps
->cred_cb(wps
->wps
->cb_ctx
, &wps
->cred
);
668 wps
->cred
.cred_attr
= NULL
;
669 wps
->cred
.cred_attr_len
= 0;
676 static int wps_process_creds(struct wps_data
*wps
, const u8
*cred
[],
677 size_t cred_len
[], size_t num_cred
)
685 wpa_printf(MSG_DEBUG
, "WPS: No Credential attributes "
690 for (i
= 0; i
< num_cred
; i
++) {
691 if (wps_process_cred_e(wps
, cred
[i
], cred_len
[i
]))
699 static int wps_process_ap_settings_e(struct wps_data
*wps
,
700 struct wps_parse_attr
*attr
,
701 struct wpabuf
*attrs
)
703 struct wps_credential cred
;
708 if (wps_process_ap_settings(attr
, &cred
) < 0)
711 wpa_printf(MSG_INFO
, "WPS: Received new AP configuration from "
714 if (os_memcmp(cred
.mac_addr
, wps
->wps
->dev
.mac_addr
, ETH_ALEN
) !=
716 wpa_printf(MSG_DEBUG
, "WPS: MAC Address in the AP Settings ("
717 MACSTR
") does not match with own address (" MACSTR
718 ")", MAC2STR(cred
.mac_addr
),
719 MAC2STR(wps
->wps
->dev
.mac_addr
));
721 * In theory, this could be consider fatal error, but there are
722 * number of deployed implementations using other address here
723 * due to unclarity in the specification. For interoperability
724 * reasons, allow this to be processed since we do not really
725 * use the MAC Address information for anything.
729 if (wps
->wps
->cred_cb
) {
730 cred
.cred_attr
= wpabuf_head(attrs
);
731 cred
.cred_attr_len
= wpabuf_len(attrs
);
732 wps
->wps
->cred_cb(wps
->wps
->cb_ctx
, &cred
);
739 static enum wps_process_res
wps_process_m2(struct wps_data
*wps
,
740 const struct wpabuf
*msg
,
741 struct wps_parse_attr
*attr
)
743 wpa_printf(MSG_DEBUG
, "WPS: Received M2");
745 if (wps
->state
!= RECV_M2
) {
746 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
747 "receiving M2", wps
->state
);
748 wps
->state
= SEND_WSC_NACK
;
752 if (wps_process_registrar_nonce(wps
, attr
->registrar_nonce
) ||
753 wps_process_enrollee_nonce(wps
, attr
->enrollee_nonce
) ||
754 wps_process_uuid_r(wps
, attr
->uuid_r
) ||
755 wps_process_pubkey(wps
, attr
->public_key
, attr
->public_key_len
) ||
756 wps_process_authenticator(wps
, attr
->authenticator
, msg
)) {
757 wps
->state
= SEND_WSC_NACK
;
761 if (wps
->wps
->ap
&& wps
->wps
->ap_setup_locked
) {
762 wpa_printf(MSG_DEBUG
, "WPS: AP Setup is locked - refuse "
763 "registration of a new Registrar");
764 wps
->config_error
= WPS_CFG_SETUP_LOCKED
;
765 wps
->state
= SEND_WSC_NACK
;
769 wps
->state
= SEND_M3
;
774 static enum wps_process_res
wps_process_m2d(struct wps_data
*wps
,
775 struct wps_parse_attr
*attr
)
777 wpa_printf(MSG_DEBUG
, "WPS: Received M2D");
779 if (wps
->state
!= RECV_M2
) {
780 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
781 "receiving M2D", wps
->state
);
782 wps
->state
= SEND_WSC_NACK
;
786 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Manufacturer",
787 attr
->manufacturer
, attr
->manufacturer_len
);
788 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Model Name",
789 attr
->model_name
, attr
->model_name_len
);
790 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Model Number",
791 attr
->model_number
, attr
->model_number_len
);
792 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Serial Number",
793 attr
->serial_number
, attr
->serial_number_len
);
794 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Device Name",
795 attr
->dev_name
, attr
->dev_name_len
);
797 if (wps
->wps
->event_cb
) {
798 union wps_event_data data
;
799 struct wps_event_m2d
*m2d
= &data
.m2d
;
800 os_memset(&data
, 0, sizeof(data
));
801 if (attr
->config_methods
)
802 m2d
->config_methods
=
803 WPA_GET_BE16(attr
->config_methods
);
804 m2d
->manufacturer
= attr
->manufacturer
;
805 m2d
->manufacturer_len
= attr
->manufacturer_len
;
806 m2d
->model_name
= attr
->model_name
;
807 m2d
->model_name_len
= attr
->model_name_len
;
808 m2d
->model_number
= attr
->model_number
;
809 m2d
->model_number_len
= attr
->model_number_len
;
810 m2d
->serial_number
= attr
->serial_number
;
811 m2d
->serial_number_len
= attr
->serial_number_len
;
812 m2d
->dev_name
= attr
->dev_name
;
813 m2d
->dev_name_len
= attr
->dev_name_len
;
814 m2d
->primary_dev_type
= attr
->primary_dev_type
;
815 if (attr
->config_error
)
817 WPA_GET_BE16(attr
->config_error
);
818 if (attr
->dev_password_id
)
819 m2d
->dev_password_id
=
820 WPA_GET_BE16(attr
->dev_password_id
);
821 wps
->wps
->event_cb(wps
->wps
->cb_ctx
, WPS_EV_M2D
, &data
);
824 wps
->state
= RECEIVED_M2D
;
829 static enum wps_process_res
wps_process_m4(struct wps_data
*wps
,
830 const struct wpabuf
*msg
,
831 struct wps_parse_attr
*attr
)
833 struct wpabuf
*decrypted
;
834 struct wps_parse_attr eattr
;
836 wpa_printf(MSG_DEBUG
, "WPS: Received M4");
838 if (wps
->state
!= RECV_M4
) {
839 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
840 "receiving M4", wps
->state
);
841 wps
->state
= SEND_WSC_NACK
;
845 if (wps_process_enrollee_nonce(wps
, attr
->enrollee_nonce
) ||
846 wps_process_authenticator(wps
, attr
->authenticator
, msg
) ||
847 wps_process_r_hash1(wps
, attr
->r_hash1
) ||
848 wps_process_r_hash2(wps
, attr
->r_hash2
)) {
849 wps
->state
= SEND_WSC_NACK
;
853 decrypted
= wps_decrypt_encr_settings(wps
, attr
->encr_settings
,
854 attr
->encr_settings_len
);
855 if (decrypted
== NULL
) {
856 wpa_printf(MSG_DEBUG
, "WPS: Failed to decrypted Encrypted "
857 "Settings attribute");
858 wps
->state
= SEND_WSC_NACK
;
862 wpa_printf(MSG_DEBUG
, "WPS: Processing decrypted Encrypted Settings "
864 if (wps_parse_msg(decrypted
, &eattr
) < 0 ||
865 wps_process_key_wrap_auth(wps
, decrypted
, eattr
.key_wrap_auth
) ||
866 wps_process_r_snonce1(wps
, eattr
.r_snonce1
)) {
867 wpabuf_free(decrypted
);
868 wps
->state
= SEND_WSC_NACK
;
871 wpabuf_free(decrypted
);
873 wps
->state
= SEND_M5
;
878 static enum wps_process_res
wps_process_m6(struct wps_data
*wps
,
879 const struct wpabuf
*msg
,
880 struct wps_parse_attr
*attr
)
882 struct wpabuf
*decrypted
;
883 struct wps_parse_attr eattr
;
885 wpa_printf(MSG_DEBUG
, "WPS: Received M6");
887 if (wps
->state
!= RECV_M6
) {
888 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
889 "receiving M6", wps
->state
);
890 wps
->state
= SEND_WSC_NACK
;
894 if (wps_process_enrollee_nonce(wps
, attr
->enrollee_nonce
) ||
895 wps_process_authenticator(wps
, attr
->authenticator
, msg
)) {
896 wps
->state
= SEND_WSC_NACK
;
900 decrypted
= wps_decrypt_encr_settings(wps
, attr
->encr_settings
,
901 attr
->encr_settings_len
);
902 if (decrypted
== NULL
) {
903 wpa_printf(MSG_DEBUG
, "WPS: Failed to decrypted Encrypted "
904 "Settings attribute");
905 wps
->state
= SEND_WSC_NACK
;
909 wpa_printf(MSG_DEBUG
, "WPS: Processing decrypted Encrypted Settings "
911 if (wps_parse_msg(decrypted
, &eattr
) < 0 ||
912 wps_process_key_wrap_auth(wps
, decrypted
, eattr
.key_wrap_auth
) ||
913 wps_process_r_snonce2(wps
, eattr
.r_snonce2
)) {
914 wpabuf_free(decrypted
);
915 wps
->state
= SEND_WSC_NACK
;
918 wpabuf_free(decrypted
);
920 wps
->state
= SEND_M7
;
925 static enum wps_process_res
wps_process_m8(struct wps_data
*wps
,
926 const struct wpabuf
*msg
,
927 struct wps_parse_attr
*attr
)
929 struct wpabuf
*decrypted
;
930 struct wps_parse_attr eattr
;
932 wpa_printf(MSG_DEBUG
, "WPS: Received M8");
934 if (wps
->state
!= RECV_M8
) {
935 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
936 "receiving M8", wps
->state
);
937 wps
->state
= SEND_WSC_NACK
;
941 if (wps_process_enrollee_nonce(wps
, attr
->enrollee_nonce
) ||
942 wps_process_authenticator(wps
, attr
->authenticator
, msg
)) {
943 wps
->state
= SEND_WSC_NACK
;
947 decrypted
= wps_decrypt_encr_settings(wps
, attr
->encr_settings
,
948 attr
->encr_settings_len
);
949 if (decrypted
== NULL
) {
950 wpa_printf(MSG_DEBUG
, "WPS: Failed to decrypted Encrypted "
951 "Settings attribute");
952 wps
->state
= SEND_WSC_NACK
;
956 wpa_printf(MSG_DEBUG
, "WPS: Processing decrypted Encrypted Settings "
958 if (wps_parse_msg(decrypted
, &eattr
) < 0 ||
959 wps_process_key_wrap_auth(wps
, decrypted
, eattr
.key_wrap_auth
) ||
960 wps_process_creds(wps
, eattr
.cred
, eattr
.cred_len
,
962 wps_process_ap_settings_e(wps
, &eattr
, decrypted
)) {
963 wpabuf_free(decrypted
);
964 wps
->state
= SEND_WSC_NACK
;
967 wpabuf_free(decrypted
);
969 wps
->state
= WPS_MSG_DONE
;
974 static enum wps_process_res
wps_process_wsc_msg(struct wps_data
*wps
,
975 const struct wpabuf
*msg
)
977 struct wps_parse_attr attr
;
978 enum wps_process_res ret
= WPS_CONTINUE
;
980 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_MSG");
982 if (wps_parse_msg(msg
, &attr
) < 0)
985 if (!wps_version_supported(attr
.version
)) {
986 wpa_printf(MSG_DEBUG
, "WPS: Unsupported message version 0x%x",
987 attr
.version
? *attr
.version
: 0);
991 if (attr
.enrollee_nonce
== NULL
||
992 os_memcmp(wps
->nonce_e
, attr
.enrollee_nonce
, WPS_NONCE_LEN
!= 0)) {
993 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in enrollee nonce");
997 if (attr
.msg_type
== NULL
) {
998 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
1002 switch (*attr
.msg_type
) {
1004 ret
= wps_process_m2(wps
, msg
, &attr
);
1007 ret
= wps_process_m2d(wps
, &attr
);
1010 ret
= wps_process_m4(wps
, msg
, &attr
);
1011 if (ret
== WPS_FAILURE
|| wps
->state
== SEND_WSC_NACK
)
1012 wps_fail_event(wps
->wps
, WPS_M4
);
1015 ret
= wps_process_m6(wps
, msg
, &attr
);
1016 if (ret
== WPS_FAILURE
|| wps
->state
== SEND_WSC_NACK
)
1017 wps_fail_event(wps
->wps
, WPS_M6
);
1020 ret
= wps_process_m8(wps
, msg
, &attr
);
1021 if (ret
== WPS_FAILURE
|| wps
->state
== SEND_WSC_NACK
)
1022 wps_fail_event(wps
->wps
, WPS_M8
);
1025 wpa_printf(MSG_DEBUG
, "WPS: Unsupported Message Type %d",
1031 * Save a copy of the last message for Authenticator derivation if we
1032 * are continuing. However, skip M2D since it is not authenticated and
1033 * neither is the ACK/NACK response frame. This allows the possibly
1034 * following M2 to be processed correctly by using the previously sent
1035 * M1 in Authenticator derivation.
1037 if (ret
== WPS_CONTINUE
&& *attr
.msg_type
!= WPS_M2D
) {
1038 /* Save a copy of the last message for Authenticator derivation
1040 wpabuf_free(wps
->last_msg
);
1041 wps
->last_msg
= wpabuf_dup(msg
);
1048 static enum wps_process_res
wps_process_wsc_ack(struct wps_data
*wps
,
1049 const struct wpabuf
*msg
)
1051 struct wps_parse_attr attr
;
1053 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_ACK");
1055 if (wps_parse_msg(msg
, &attr
) < 0)
1058 if (!wps_version_supported(attr
.version
)) {
1059 wpa_printf(MSG_DEBUG
, "WPS: Unsupported message version 0x%x",
1060 attr
.version
? *attr
.version
: 0);
1064 if (attr
.msg_type
== NULL
) {
1065 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
1069 if (*attr
.msg_type
!= WPS_WSC_ACK
) {
1070 wpa_printf(MSG_DEBUG
, "WPS: Invalid Message Type %d",
1075 if (attr
.registrar_nonce
== NULL
||
1076 os_memcmp(wps
->nonce_r
, attr
.registrar_nonce
, WPS_NONCE_LEN
!= 0))
1078 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in registrar nonce");
1082 if (attr
.enrollee_nonce
== NULL
||
1083 os_memcmp(wps
->nonce_e
, attr
.enrollee_nonce
, WPS_NONCE_LEN
!= 0)) {
1084 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in enrollee nonce");
1088 if (wps
->state
== RECV_ACK
&& wps
->wps
->ap
) {
1089 wpa_printf(MSG_DEBUG
, "WPS: External Registrar registration "
1090 "completed successfully");
1091 wps_success_event(wps
->wps
);
1092 wps
->state
= WPS_FINISHED
;
1100 static enum wps_process_res
wps_process_wsc_nack(struct wps_data
*wps
,
1101 const struct wpabuf
*msg
)
1103 struct wps_parse_attr attr
;
1105 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_NACK");
1107 if (wps_parse_msg(msg
, &attr
) < 0)
1110 if (!wps_version_supported(attr
.version
)) {
1111 wpa_printf(MSG_DEBUG
, "WPS: Unsupported message version 0x%x",
1112 attr
.version
? *attr
.version
: 0);
1116 if (attr
.msg_type
== NULL
) {
1117 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
1121 if (*attr
.msg_type
!= WPS_WSC_NACK
) {
1122 wpa_printf(MSG_DEBUG
, "WPS: Invalid Message Type %d",
1127 if (attr
.registrar_nonce
== NULL
||
1128 os_memcmp(wps
->nonce_r
, attr
.registrar_nonce
, WPS_NONCE_LEN
!= 0))
1130 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in registrar nonce");
1131 wpa_hexdump(MSG_DEBUG
, "WPS: Received Registrar Nonce",
1132 attr
.registrar_nonce
, WPS_NONCE_LEN
);
1133 wpa_hexdump(MSG_DEBUG
, "WPS: Expected Registrar Nonce",
1134 wps
->nonce_r
, WPS_NONCE_LEN
);
1138 if (attr
.enrollee_nonce
== NULL
||
1139 os_memcmp(wps
->nonce_e
, attr
.enrollee_nonce
, WPS_NONCE_LEN
!= 0)) {
1140 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in enrollee nonce");
1141 wpa_hexdump(MSG_DEBUG
, "WPS: Received Enrollee Nonce",
1142 attr
.enrollee_nonce
, WPS_NONCE_LEN
);
1143 wpa_hexdump(MSG_DEBUG
, "WPS: Expected Enrollee Nonce",
1144 wps
->nonce_e
, WPS_NONCE_LEN
);
1148 if (attr
.config_error
== NULL
) {
1149 wpa_printf(MSG_DEBUG
, "WPS: No Configuration Error attribute "
1154 wpa_printf(MSG_DEBUG
, "WPS: Registrar terminated negotiation with "
1155 "Configuration Error %d", WPA_GET_BE16(attr
.config_error
));
1157 switch (wps
->state
) {
1159 wps_fail_event(wps
->wps
, WPS_M3
);
1162 wps_fail_event(wps
->wps
, WPS_M5
);
1165 wps_fail_event(wps
->wps
, WPS_M7
);
1171 /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1172 * Enrollee is Authenticator */
1173 wps
->state
= SEND_WSC_NACK
;
1179 enum wps_process_res
wps_enrollee_process_msg(struct wps_data
*wps
,
1180 enum wsc_op_code op_code
,
1181 const struct wpabuf
*msg
)
1184 wpa_printf(MSG_DEBUG
, "WPS: Processing received message (len=%lu "
1186 (unsigned long) wpabuf_len(msg
), op_code
);
1188 if (op_code
== WSC_UPnP
) {
1189 /* Determine the OpCode based on message type attribute */
1190 struct wps_parse_attr attr
;
1191 if (wps_parse_msg(msg
, &attr
) == 0 && attr
.msg_type
) {
1192 if (*attr
.msg_type
== WPS_WSC_ACK
)
1194 else if (*attr
.msg_type
== WPS_WSC_NACK
)
1202 return wps_process_wsc_msg(wps
, msg
);
1204 return wps_process_wsc_ack(wps
, msg
);
1206 return wps_process_wsc_nack(wps
, msg
);
1208 wpa_printf(MSG_DEBUG
, "WPS: Unsupported op_code %d", op_code
);