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, 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)
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 #ifdef CONFIG_WPS_OOB
521 if (wps->dev_pw_id != DEV_PW_DEFAULT &&
522 wps->wps->oob_conf.pubkey_hash) {
524 u8 hash[WPS_HASH_LEN];
527 sha256_vector(1, addr, &pk_len, hash);
529 wpabuf_head(wps->wps->oob_conf.pubkey_hash),
530 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
531 wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
535 #endif /* CONFIG_WPS_OOB */
537 wpabuf_free(wps->dh_pubkey_r);
538 wps->dh_pubkey_r = wpabuf_alloc_copy(pk, pk_len);
539 if (wps->dh_pubkey_r == NULL)
542 if (wps_derive_keys(wps) < 0)
545 if (wps->request_type == WPS_REQ_WLAN_MANAGER_REGISTRAR &&
546 wps_derive_mgmt_keys(wps) < 0)
553 static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1)
555 if (r_hash1 == NULL) {
556 wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received");
560 os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN);
561 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN);
567 static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2)
569 if (r_hash2 == NULL) {
570 wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received");
574 os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN);
575 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN);
581 static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
583 u8 hash[SHA256_MAC_LEN];
587 if (r_snonce1 == NULL) {
588 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
592 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1,
593 WPS_SECRET_NONCE_LEN);
595 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
597 len[0] = WPS_SECRET_NONCE_LEN;
599 len[1] = WPS_PSK_LEN;
600 addr[2] = wpabuf_head(wps->dh_pubkey_e);
601 len[2] = wpabuf_len(wps->dh_pubkey_e);
602 addr[3] = wpabuf_head(wps->dh_pubkey_r);
603 len[3] = wpabuf_len(wps->dh_pubkey_r);
604 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
606 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
607 wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does "
608 "not match with the pre-committed value");
609 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
610 wps_pwd_auth_fail_event(wps->wps, 1, 1);
614 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
615 "half of the device password");
621 static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
623 u8 hash[SHA256_MAC_LEN];
627 if (r_snonce2 == NULL) {
628 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
632 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2,
633 WPS_SECRET_NONCE_LEN);
635 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
637 len[0] = WPS_SECRET_NONCE_LEN;
639 len[1] = WPS_PSK_LEN;
640 addr[2] = wpabuf_head(wps->dh_pubkey_e);
641 len[2] = wpabuf_len(wps->dh_pubkey_e);
642 addr[3] = wpabuf_head(wps->dh_pubkey_r);
643 len[3] = wpabuf_len(wps->dh_pubkey_r);
644 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
646 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
647 wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does "
648 "not match with the pre-committed value");
649 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
650 wps_pwd_auth_fail_event(wps->wps, 1, 2);
654 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
655 "half of the device password");
661 static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
664 struct wps_parse_attr attr;
667 wpa_printf(MSG_DEBUG, "WPS: Received Credential");
668 os_memset(&wps->cred, 0, sizeof(wps->cred));
669 wpabuf_set(&msg, cred, cred_len);
670 if (wps_parse_msg(&msg, &attr) < 0 ||
671 wps_process_cred(&attr, &wps->cred))
674 if (wps->wps->cred_cb) {
675 wps->cred.cred_attr = cred - 4;
676 wps->cred.cred_attr_len = cred_len + 4;
677 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
678 wps->cred.cred_attr = NULL;
679 wps->cred.cred_attr_len = 0;
686 static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
687 size_t cred_len[], size_t num_cred)
695 wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
700 for (i = 0; i < num_cred; i++) {
701 if (wps_process_cred_e(wps, cred[i], cred_len[i]))
709 static int wps_process_ap_settings_e(struct wps_data *wps,
710 struct wps_parse_attr *attr,
711 struct wpabuf *attrs)
713 struct wps_credential cred;
718 if (wps_process_ap_settings(attr, &cred) < 0)
721 wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
724 if (wps->wps->cred_cb) {
725 cred.cred_attr = wpabuf_head(attrs);
726 cred.cred_attr_len = wpabuf_len(attrs);
727 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
734 static enum wps_process_res wps_process_m2(struct wps_data *wps,
735 const struct wpabuf *msg,
736 struct wps_parse_attr *attr)
738 wpa_printf(MSG_DEBUG, "WPS: Received M2");
740 if (wps->state != RECV_M2) {
741 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
742 "receiving M2", wps->state);
743 wps->state = SEND_WSC_NACK;
747 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
748 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
749 wps_process_uuid_r(wps, attr->uuid_r) ||
750 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
751 wps_process_authenticator(wps, attr->authenticator, msg)) {
752 wps->state = SEND_WSC_NACK;
756 if (wps->wps->ap && wps->wps->ap_setup_locked) {
757 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
758 "registration of a new Registrar");
759 wps->config_error = WPS_CFG_SETUP_LOCKED;
760 wps->state = SEND_WSC_NACK;
764 wps->state = SEND_M3;
769 static enum wps_process_res wps_process_m2d(struct wps_data *wps,
770 struct wps_parse_attr *attr)
772 wpa_printf(MSG_DEBUG, "WPS: Received M2D");
774 if (wps->state != RECV_M2) {
775 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
776 "receiving M2D", wps->state);
777 wps->state = SEND_WSC_NACK;
781 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
782 attr->manufacturer, attr->manufacturer_len);
783 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
784 attr->model_name, attr->model_name_len);
785 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
786 attr->model_number, attr->model_number_len);
787 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
788 attr->serial_number, attr->serial_number_len);
789 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
790 attr->dev_name, attr->dev_name_len);
792 if (wps->wps->event_cb) {
793 union wps_event_data data;
794 struct wps_event_m2d *m2d = &data.m2d;
795 os_memset(&data, 0, sizeof(data));
796 if (attr->config_methods)
797 m2d->config_methods =
798 WPA_GET_BE16(attr->config_methods);
799 m2d->manufacturer = attr->manufacturer;
800 m2d->manufacturer_len = attr->manufacturer_len;
801 m2d->model_name = attr->model_name;
802 m2d->model_name_len = attr->model_name_len;
803 m2d->model_number = attr->model_number;
804 m2d->model_number_len = attr->model_number_len;
805 m2d->serial_number = attr->serial_number;
806 m2d->serial_number_len = attr->serial_number_len;
807 m2d->dev_name = attr->dev_name;
808 m2d->dev_name_len = attr->dev_name_len;
809 m2d->primary_dev_type = attr->primary_dev_type;
810 if (attr->config_error)
812 WPA_GET_BE16(attr->config_error);
813 if (attr->dev_password_id)
814 m2d->dev_password_id =
815 WPA_GET_BE16(attr->dev_password_id);
816 wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_M2D, &data);
819 wps->state = RECEIVED_M2D;
824 static enum wps_process_res wps_process_m4(struct wps_data *wps,
825 const struct wpabuf *msg,
826 struct wps_parse_attr *attr)
828 struct wpabuf *decrypted;
829 struct wps_parse_attr eattr;
831 wpa_printf(MSG_DEBUG, "WPS: Received M4");
833 if (wps->state != RECV_M4) {
834 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
835 "receiving M4", wps->state);
836 wps->state = SEND_WSC_NACK;
840 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
841 wps_process_authenticator(wps, attr->authenticator, msg) ||
842 wps_process_r_hash1(wps, attr->r_hash1) ||
843 wps_process_r_hash2(wps, attr->r_hash2)) {
844 wps->state = SEND_WSC_NACK;
848 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
849 attr->encr_settings_len);
850 if (decrypted == NULL) {
851 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
852 "Settings attribute");
853 wps->state = SEND_WSC_NACK;
857 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
859 if (wps_parse_msg(decrypted, &eattr) < 0 ||
860 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
861 wps_process_r_snonce1(wps, eattr.r_snonce1)) {
862 wpabuf_free(decrypted);
863 wps->state = SEND_WSC_NACK;
866 wpabuf_free(decrypted);
868 wps->state = SEND_M5;
873 static enum wps_process_res wps_process_m6(struct wps_data *wps,
874 const struct wpabuf *msg,
875 struct wps_parse_attr *attr)
877 struct wpabuf *decrypted;
878 struct wps_parse_attr eattr;
880 wpa_printf(MSG_DEBUG, "WPS: Received M6");
882 if (wps->state != RECV_M6) {
883 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
884 "receiving M6", wps->state);
885 wps->state = SEND_WSC_NACK;
889 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
890 wps_process_authenticator(wps, attr->authenticator, msg)) {
891 wps->state = SEND_WSC_NACK;
895 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
896 attr->encr_settings_len);
897 if (decrypted == NULL) {
898 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
899 "Settings attribute");
900 wps->state = SEND_WSC_NACK;
904 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
906 if (wps_parse_msg(decrypted, &eattr) < 0 ||
907 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
908 wps_process_r_snonce2(wps, eattr.r_snonce2)) {
909 wpabuf_free(decrypted);
910 wps->state = SEND_WSC_NACK;
913 wpabuf_free(decrypted);
915 wps->state = SEND_M7;
920 static enum wps_process_res wps_process_m8(struct wps_data *wps,
921 const struct wpabuf *msg,
922 struct wps_parse_attr *attr)
924 struct wpabuf *decrypted;
925 struct wps_parse_attr eattr;
927 wpa_printf(MSG_DEBUG, "WPS: Received M8");
929 if (wps->state != RECV_M8) {
930 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
931 "receiving M8", wps->state);
932 wps->state = SEND_WSC_NACK;
936 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
937 wps_process_authenticator(wps, attr->authenticator, msg)) {
938 wps->state = SEND_WSC_NACK;
942 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
943 attr->encr_settings_len);
944 if (decrypted == NULL) {
945 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
946 "Settings attribute");
947 wps->state = SEND_WSC_NACK;
951 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
953 if (wps_parse_msg(decrypted, &eattr) < 0 ||
954 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
955 wps_process_creds(wps, eattr.cred, eattr.cred_len,
957 wps_process_ap_settings_e(wps, &eattr, decrypted)) {
958 wpabuf_free(decrypted);
959 wps->state = SEND_WSC_NACK;
962 wpabuf_free(decrypted);
964 wps->state = WPS_MSG_DONE;
969 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
970 const struct wpabuf *msg)
972 struct wps_parse_attr attr;
973 enum wps_process_res ret = WPS_CONTINUE;
975 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
977 if (wps_parse_msg(msg, &attr) < 0)
980 if (!wps_version_supported(attr.version)) {
981 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
982 attr.version ? *attr.version : 0);
986 if (attr.enrollee_nonce == NULL ||
987 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
988 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
992 if (attr.msg_type == NULL) {
993 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
997 switch (*attr.msg_type) {
999 ret = wps_process_m2(wps, msg, &attr);
1002 ret = wps_process_m2d(wps, &attr);
1005 ret = wps_process_m4(wps, msg, &attr);
1006 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1007 wps_fail_event(wps->wps, WPS_M4);
1010 ret = wps_process_m6(wps, msg, &attr);
1011 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1012 wps_fail_event(wps->wps, WPS_M6);
1015 ret = wps_process_m8(wps, msg, &attr);
1016 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1017 wps_fail_event(wps->wps, WPS_M8);
1020 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1026 * Save a copy of the last message for Authenticator derivation if we
1027 * are continuing. However, skip M2D since it is not authenticated and
1028 * neither is the ACK/NACK response frame. This allows the possibly
1029 * following M2 to be processed correctly by using the previously sent
1030 * M1 in Authenticator derivation.
1032 if (ret == WPS_CONTINUE && *attr.msg_type != WPS_M2D) {
1033 /* Save a copy of the last message for Authenticator derivation
1035 wpabuf_free(wps->last_msg);
1036 wps->last_msg = wpabuf_dup(msg);
1043 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1044 const struct wpabuf *msg)
1046 struct wps_parse_attr attr;
1048 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1050 if (wps_parse_msg(msg, &attr) < 0)
1053 if (!wps_version_supported(attr.version)) {
1054 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1055 attr.version ? *attr.version : 0);
1059 if (attr.msg_type == NULL) {
1060 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1064 if (*attr.msg_type != WPS_WSC_ACK) {
1065 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1070 if (attr.registrar_nonce == NULL ||
1071 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1073 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1077 if (attr.enrollee_nonce == NULL ||
1078 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1079 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1083 if (wps->state == RECV_ACK && wps->wps->ap) {
1084 wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
1085 "completed successfully");
1086 wps_success_event(wps->wps);
1087 wps->state = WPS_FINISHED;
1095 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1096 const struct wpabuf *msg)
1098 struct wps_parse_attr attr;
1100 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1102 if (wps_parse_msg(msg, &attr) < 0)
1105 if (!wps_version_supported(attr.version)) {
1106 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1107 attr.version ? *attr.version : 0);
1111 if (attr.msg_type == NULL) {
1112 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1116 if (*attr.msg_type != WPS_WSC_NACK) {
1117 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1122 if (attr.registrar_nonce == NULL ||
1123 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1125 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1126 wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
1127 attr.registrar_nonce, WPS_NONCE_LEN);
1128 wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
1129 wps->nonce_r, WPS_NONCE_LEN);
1133 if (attr.enrollee_nonce == NULL ||
1134 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1135 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1136 wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
1137 attr.enrollee_nonce, WPS_NONCE_LEN);
1138 wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
1139 wps->nonce_e, WPS_NONCE_LEN);
1143 if (attr.config_error == NULL) {
1144 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1149 wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
1150 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1152 switch (wps->state) {
1154 wps_fail_event(wps->wps, WPS_M3);
1157 wps_fail_event(wps->wps, WPS_M5);
1160 wps_fail_event(wps->wps, WPS_M7);
1166 /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1167 * Enrollee is Authenticator */
1168 wps->state = SEND_WSC_NACK;
1174 enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps,
1175 enum wsc_op_code op_code,
1176 const struct wpabuf *msg)
1179 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1181 (unsigned long) wpabuf_len(msg), op_code);
1186 return wps_process_wsc_msg(wps, msg);
1188 return wps_process_wsc_ack(wps, msg);
1190 return wps_process_wsc_nack(wps, msg);
1192 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);