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, WPS_STATE_NOT_CONFIGURED);
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)
524 if (wps->request_type == WPS_REQ_WLAN_MANAGER_REGISTRAR &&
525 wps_derive_mgmt_keys(wps) < 0)
532 static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1)
534 if (r_hash1 == NULL) {
535 wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received");
539 os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN);
540 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN);
546 static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2)
548 if (r_hash2 == NULL) {
549 wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received");
553 os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN);
554 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN);
560 static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
562 u8 hash[SHA256_MAC_LEN];
566 if (r_snonce1 == NULL) {
567 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
571 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1,
572 WPS_SECRET_NONCE_LEN);
574 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
576 len[0] = WPS_SECRET_NONCE_LEN;
578 len[1] = WPS_PSK_LEN;
579 addr[2] = wpabuf_head(wps->dh_pubkey_e);
580 len[2] = wpabuf_len(wps->dh_pubkey_e);
581 addr[3] = wpabuf_head(wps->dh_pubkey_r);
582 len[3] = wpabuf_len(wps->dh_pubkey_r);
583 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
585 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
586 wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does "
587 "not match with the pre-committed value");
588 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
589 wps_pwd_auth_fail_event(wps->wps, 1, 1);
593 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
594 "half of the device password");
600 static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
602 u8 hash[SHA256_MAC_LEN];
606 if (r_snonce2 == NULL) {
607 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
611 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2,
612 WPS_SECRET_NONCE_LEN);
614 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
616 len[0] = WPS_SECRET_NONCE_LEN;
618 len[1] = WPS_PSK_LEN;
619 addr[2] = wpabuf_head(wps->dh_pubkey_e);
620 len[2] = wpabuf_len(wps->dh_pubkey_e);
621 addr[3] = wpabuf_head(wps->dh_pubkey_r);
622 len[3] = wpabuf_len(wps->dh_pubkey_r);
623 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
625 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
626 wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does "
627 "not match with the pre-committed value");
628 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
629 wps_pwd_auth_fail_event(wps->wps, 1, 2);
633 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
634 "half of the device password");
640 static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
643 struct wps_parse_attr attr;
646 wpa_printf(MSG_DEBUG, "WPS: Received Credential");
647 os_memset(&wps->cred, 0, sizeof(wps->cred));
648 wpabuf_set(&msg, cred, cred_len);
649 if (wps_parse_msg(&msg, &attr) < 0 ||
650 wps_process_cred(&attr, &wps->cred))
653 if (wps->wps->cred_cb) {
654 wps->cred.cred_attr = cred - 4;
655 wps->cred.cred_attr_len = cred_len + 4;
656 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
657 wps->cred.cred_attr = NULL;
658 wps->cred.cred_attr_len = 0;
665 static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
666 size_t cred_len[], size_t num_cred)
674 wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
679 for (i = 0; i < num_cred; i++) {
680 if (wps_process_cred_e(wps, cred[i], cred_len[i]))
688 static int wps_process_ap_settings_e(struct wps_data *wps,
689 struct wps_parse_attr *attr,
690 struct wpabuf *attrs)
692 struct wps_credential cred;
697 if (wps_process_ap_settings(attr, &cred) < 0)
700 wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
703 if (wps->wps->cred_cb) {
704 cred.cred_attr = wpabuf_head(attrs);
705 cred.cred_attr_len = wpabuf_len(attrs);
706 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
713 static enum wps_process_res wps_process_m2(struct wps_data *wps,
714 const struct wpabuf *msg,
715 struct wps_parse_attr *attr)
717 wpa_printf(MSG_DEBUG, "WPS: Received M2");
719 if (wps->state != RECV_M2) {
720 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
721 "receiving M2", wps->state);
722 wps->state = SEND_WSC_NACK;
726 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
727 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
728 wps_process_uuid_r(wps, attr->uuid_r) ||
729 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
730 wps_process_authenticator(wps, attr->authenticator, msg)) {
731 wps->state = SEND_WSC_NACK;
735 if (wps->wps->ap && wps->wps->ap_setup_locked) {
736 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
737 "registration of a new Registrar");
738 wps->config_error = WPS_CFG_SETUP_LOCKED;
739 wps->state = SEND_WSC_NACK;
743 wps->state = SEND_M3;
748 static enum wps_process_res wps_process_m2d(struct wps_data *wps,
749 struct wps_parse_attr *attr)
751 wpa_printf(MSG_DEBUG, "WPS: Received M2D");
753 if (wps->state != RECV_M2) {
754 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
755 "receiving M2D", wps->state);
756 wps->state = SEND_WSC_NACK;
760 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
761 attr->manufacturer, attr->manufacturer_len);
762 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
763 attr->model_name, attr->model_name_len);
764 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
765 attr->model_number, attr->model_number_len);
766 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
767 attr->serial_number, attr->serial_number_len);
768 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
769 attr->dev_name, attr->dev_name_len);
771 if (wps->wps->event_cb) {
772 union wps_event_data data;
773 struct wps_event_m2d *m2d = &data.m2d;
774 os_memset(&data, 0, sizeof(data));
775 if (attr->config_methods)
776 m2d->config_methods =
777 WPA_GET_BE16(attr->config_methods);
778 m2d->manufacturer = attr->manufacturer;
779 m2d->manufacturer_len = attr->manufacturer_len;
780 m2d->model_name = attr->model_name;
781 m2d->model_name_len = attr->model_name_len;
782 m2d->model_number = attr->model_number;
783 m2d->model_number_len = attr->model_number_len;
784 m2d->serial_number = attr->serial_number;
785 m2d->serial_number_len = attr->serial_number_len;
786 m2d->dev_name = attr->dev_name;
787 m2d->dev_name_len = attr->dev_name_len;
788 m2d->primary_dev_type = attr->primary_dev_type;
789 if (attr->config_error)
791 WPA_GET_BE16(attr->config_error);
792 if (attr->dev_password_id)
793 m2d->dev_password_id =
794 WPA_GET_BE16(attr->dev_password_id);
795 wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_M2D, &data);
798 wps->state = RECEIVED_M2D;
803 static enum wps_process_res wps_process_m4(struct wps_data *wps,
804 const struct wpabuf *msg,
805 struct wps_parse_attr *attr)
807 struct wpabuf *decrypted;
808 struct wps_parse_attr eattr;
810 wpa_printf(MSG_DEBUG, "WPS: Received M4");
812 if (wps->state != RECV_M4) {
813 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
814 "receiving M4", wps->state);
815 wps->state = SEND_WSC_NACK;
819 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
820 wps_process_authenticator(wps, attr->authenticator, msg) ||
821 wps_process_r_hash1(wps, attr->r_hash1) ||
822 wps_process_r_hash2(wps, attr->r_hash2)) {
823 wps->state = SEND_WSC_NACK;
827 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
828 attr->encr_settings_len);
829 if (decrypted == NULL) {
830 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
831 "Settings attribute");
832 wps->state = SEND_WSC_NACK;
836 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
838 if (wps_parse_msg(decrypted, &eattr) < 0 ||
839 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
840 wps_process_r_snonce1(wps, eattr.r_snonce1)) {
841 wpabuf_free(decrypted);
842 wps->state = SEND_WSC_NACK;
845 wpabuf_free(decrypted);
847 wps->state = SEND_M5;
852 static enum wps_process_res wps_process_m6(struct wps_data *wps,
853 const struct wpabuf *msg,
854 struct wps_parse_attr *attr)
856 struct wpabuf *decrypted;
857 struct wps_parse_attr eattr;
859 wpa_printf(MSG_DEBUG, "WPS: Received M6");
861 if (wps->state != RECV_M6) {
862 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
863 "receiving M6", wps->state);
864 wps->state = SEND_WSC_NACK;
868 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
869 wps_process_authenticator(wps, attr->authenticator, msg)) {
870 wps->state = SEND_WSC_NACK;
874 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
875 attr->encr_settings_len);
876 if (decrypted == NULL) {
877 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
878 "Settings attribute");
879 wps->state = SEND_WSC_NACK;
883 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
885 if (wps_parse_msg(decrypted, &eattr) < 0 ||
886 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
887 wps_process_r_snonce2(wps, eattr.r_snonce2)) {
888 wpabuf_free(decrypted);
889 wps->state = SEND_WSC_NACK;
892 wpabuf_free(decrypted);
894 wps->state = SEND_M7;
899 static enum wps_process_res wps_process_m8(struct wps_data *wps,
900 const struct wpabuf *msg,
901 struct wps_parse_attr *attr)
903 struct wpabuf *decrypted;
904 struct wps_parse_attr eattr;
906 wpa_printf(MSG_DEBUG, "WPS: Received M8");
908 if (wps->state != RECV_M8) {
909 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
910 "receiving M8", wps->state);
911 wps->state = SEND_WSC_NACK;
915 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
916 wps_process_authenticator(wps, attr->authenticator, msg)) {
917 wps->state = SEND_WSC_NACK;
921 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
922 attr->encr_settings_len);
923 if (decrypted == NULL) {
924 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
925 "Settings attribute");
926 wps->state = SEND_WSC_NACK;
930 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
932 if (wps_parse_msg(decrypted, &eattr) < 0 ||
933 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
934 wps_process_creds(wps, eattr.cred, eattr.cred_len,
936 wps_process_ap_settings_e(wps, &eattr, decrypted)) {
937 wpabuf_free(decrypted);
938 wps->state = SEND_WSC_NACK;
941 wpabuf_free(decrypted);
943 wps->state = WPS_MSG_DONE;
948 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
949 const struct wpabuf *msg)
951 struct wps_parse_attr attr;
952 enum wps_process_res ret = WPS_CONTINUE;
954 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
956 if (wps_parse_msg(msg, &attr) < 0)
959 if (!wps_version_supported(attr.version)) {
960 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
961 attr.version ? *attr.version : 0);
965 if (attr.enrollee_nonce == NULL ||
966 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
967 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
971 if (attr.msg_type == NULL) {
972 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
976 switch (*attr.msg_type) {
978 ret = wps_process_m2(wps, msg, &attr);
981 ret = wps_process_m2d(wps, &attr);
984 ret = wps_process_m4(wps, msg, &attr);
985 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
986 wps_fail_event(wps->wps, WPS_M4);
989 ret = wps_process_m6(wps, msg, &attr);
990 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
991 wps_fail_event(wps->wps, WPS_M6);
994 ret = wps_process_m8(wps, msg, &attr);
995 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
996 wps_fail_event(wps->wps, WPS_M8);
999 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1005 * Save a copy of the last message for Authenticator derivation if we
1006 * are continuing. However, skip M2D since it is not authenticated and
1007 * neither is the ACK/NACK response frame. This allows the possibly
1008 * following M2 to be processed correctly by using the previously sent
1009 * M1 in Authenticator derivation.
1011 if (ret == WPS_CONTINUE && *attr.msg_type != WPS_M2D) {
1012 /* Save a copy of the last message for Authenticator derivation
1014 wpabuf_free(wps->last_msg);
1015 wps->last_msg = wpabuf_dup(msg);
1022 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1023 const struct wpabuf *msg)
1025 struct wps_parse_attr attr;
1027 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1029 if (wps_parse_msg(msg, &attr) < 0)
1032 if (!wps_version_supported(attr.version)) {
1033 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1034 attr.version ? *attr.version : 0);
1038 if (attr.msg_type == NULL) {
1039 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1043 if (*attr.msg_type != WPS_WSC_ACK) {
1044 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1049 if (attr.registrar_nonce == NULL ||
1050 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1052 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1056 if (attr.enrollee_nonce == NULL ||
1057 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1058 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1062 if (wps->state == RECV_ACK && wps->wps->ap) {
1063 wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
1064 "completed successfully");
1065 wps_success_event(wps->wps);
1066 wps->state = WPS_FINISHED;
1074 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1075 const struct wpabuf *msg)
1077 struct wps_parse_attr attr;
1079 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1081 if (wps_parse_msg(msg, &attr) < 0)
1084 if (!wps_version_supported(attr.version)) {
1085 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1086 attr.version ? *attr.version : 0);
1090 if (attr.msg_type == NULL) {
1091 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1095 if (*attr.msg_type != WPS_WSC_NACK) {
1096 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1101 if (attr.registrar_nonce == NULL ||
1102 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1104 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1105 wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
1106 attr.registrar_nonce, WPS_NONCE_LEN);
1107 wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
1108 wps->nonce_r, WPS_NONCE_LEN);
1112 if (attr.enrollee_nonce == NULL ||
1113 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1114 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1115 wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
1116 attr.enrollee_nonce, WPS_NONCE_LEN);
1117 wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
1118 wps->nonce_e, WPS_NONCE_LEN);
1122 if (attr.config_error == NULL) {
1123 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1128 wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
1129 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1131 switch (wps->state) {
1133 wps_fail_event(wps->wps, WPS_M3);
1136 wps_fail_event(wps->wps, WPS_M5);
1139 wps_fail_event(wps->wps, WPS_M7);
1145 /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1146 * Enrollee is Authenticator */
1147 wps->state = SEND_WSC_NACK;
1153 enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps,
1154 enum wsc_op_code op_code,
1155 const struct wpabuf *msg)
1158 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1160 (unsigned long) wpabuf_len(msg), op_code);
1165 return wps_process_wsc_msg(wps, msg);
1167 return wps_process_wsc_ack(wps, msg);
1169 return wps_process_wsc_nack(wps, msg);
1171 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);