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)
35 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State");
36 wpabuf_put_be16(msg, ATTR_WPS_STATE);
37 wpabuf_put_be16(msg, 1);
38 wpabuf_put_u8(msg, WPS_STATE_NOT_CONFIGURED);
43 static int wps_build_e_hash(struct wps_data *wps, struct wpabuf *msg)
49 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
51 wpa_hexdump(MSG_DEBUG, "WPS: E-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
52 wpa_hexdump(MSG_DEBUG, "WPS: E-S2",
53 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
55 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
56 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
61 wpa_printf(MSG_DEBUG, "WPS: * E-Hash1");
62 wpabuf_put_be16(msg, ATTR_E_HASH1);
63 wpabuf_put_be16(msg, SHA256_MAC_LEN);
64 hash = wpabuf_put(msg, SHA256_MAC_LEN);
65 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
66 addr[0] = wps->snonce;
67 len[0] = WPS_SECRET_NONCE_LEN;
70 addr[2] = wpabuf_head(wps->dh_pubkey_e);
71 len[2] = wpabuf_len(wps->dh_pubkey_e);
72 addr[3] = wpabuf_head(wps->dh_pubkey_r);
73 len[3] = wpabuf_len(wps->dh_pubkey_r);
74 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
75 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", hash, SHA256_MAC_LEN);
77 wpa_printf(MSG_DEBUG, "WPS: * E-Hash2");
78 wpabuf_put_be16(msg, ATTR_E_HASH2);
79 wpabuf_put_be16(msg, SHA256_MAC_LEN);
80 hash = wpabuf_put(msg, SHA256_MAC_LEN);
81 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
82 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
84 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
85 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", hash, SHA256_MAC_LEN);
91 static int wps_build_e_snonce1(struct wps_data *wps, struct wpabuf *msg)
93 wpa_printf(MSG_DEBUG, "WPS: * E-SNonce1");
94 wpabuf_put_be16(msg, ATTR_E_SNONCE1);
95 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
96 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
101 static int wps_build_e_snonce2(struct wps_data *wps, struct wpabuf *msg)
103 wpa_printf(MSG_DEBUG, "WPS: * E-SNonce2");
104 wpabuf_put_be16(msg, ATTR_E_SNONCE2);
105 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
106 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
107 WPS_SECRET_NONCE_LEN);
112 static struct wpabuf * wps_build_m1(struct wps_data *wps)
117 if (os_get_random(wps->nonce_e, WPS_NONCE_LEN) < 0)
119 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
120 wps->nonce_e, WPS_NONCE_LEN);
122 wpa_printf(MSG_DEBUG, "WPS: Building Message M1");
123 msg = wpabuf_alloc(1000);
127 methods = WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
129 methods |= WPS_CONFIG_PUSHBUTTON;
131 if (wps_build_version(msg) ||
132 wps_build_msg_type(msg, WPS_M1) ||
133 wps_build_uuid_e(msg, wps->uuid_e) ||
134 wps_build_mac_addr(wps, msg) ||
135 wps_build_enrollee_nonce(wps, msg) ||
136 wps_build_public_key(wps, msg) ||
137 wps_build_auth_type_flags(wps, msg) ||
138 wps_build_encr_type_flags(wps, msg) ||
139 wps_build_conn_type_flags(wps, msg) ||
140 wps_build_config_methods(msg, methods) ||
141 wps_build_wps_state(wps, msg) ||
142 wps_build_device_attrs(&wps->wps->dev, msg) ||
143 wps_build_rf_bands(&wps->wps->dev, msg) ||
144 wps_build_assoc_state(wps, msg) ||
145 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
146 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
147 wps_build_os_version(&wps->wps->dev, msg)) {
152 wps->state = RECV_M2;
157 static struct wpabuf * wps_build_m3(struct wps_data *wps)
161 wpa_printf(MSG_DEBUG, "WPS: Building Message M3");
163 if (wps->dev_password == NULL) {
164 wpa_printf(MSG_DEBUG, "WPS: No Device Password available");
167 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
169 msg = wpabuf_alloc(1000);
173 if (wps_build_version(msg) ||
174 wps_build_msg_type(msg, WPS_M3) ||
175 wps_build_registrar_nonce(wps, msg) ||
176 wps_build_e_hash(wps, msg) ||
177 wps_build_authenticator(wps, msg)) {
182 wps->state = RECV_M4;
187 static struct wpabuf * wps_build_m5(struct wps_data *wps)
189 struct wpabuf *msg, *plain;
191 wpa_printf(MSG_DEBUG, "WPS: Building Message M5");
193 plain = wpabuf_alloc(200);
197 msg = wpabuf_alloc(1000);
203 if (wps_build_version(msg) ||
204 wps_build_msg_type(msg, WPS_M5) ||
205 wps_build_registrar_nonce(wps, msg) ||
206 wps_build_e_snonce1(wps, plain) ||
207 wps_build_key_wrap_auth(wps, plain) ||
208 wps_build_encr_settings(wps, msg, plain) ||
209 wps_build_authenticator(wps, msg)) {
216 wps->state = RECV_M6;
221 static int wps_build_cred_ssid(struct wps_data *wps, struct wpabuf *msg)
223 wpa_printf(MSG_DEBUG, "WPS: * SSID");
224 wpabuf_put_be16(msg, ATTR_SSID);
225 wpabuf_put_be16(msg, wps->wps->ssid_len);
226 wpabuf_put_data(msg, wps->wps->ssid, wps->wps->ssid_len);
231 static int wps_build_cred_auth_type(struct wps_data *wps, struct wpabuf *msg)
233 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type");
234 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
235 wpabuf_put_be16(msg, 2);
236 wpabuf_put_be16(msg, wps->wps->auth_types);
241 static int wps_build_cred_encr_type(struct wps_data *wps, struct wpabuf *msg)
243 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type");
244 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
245 wpabuf_put_be16(msg, 2);
246 wpabuf_put_be16(msg, wps->wps->encr_types);
251 static int wps_build_cred_network_key(struct wps_data *wps, struct wpabuf *msg)
253 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
254 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
255 wpabuf_put_be16(msg, wps->wps->network_key_len);
256 wpabuf_put_data(msg, wps->wps->network_key, wps->wps->network_key_len);
261 static int wps_build_cred_mac_addr(struct wps_data *wps, struct wpabuf *msg)
263 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (AP BSSID)");
264 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
265 wpabuf_put_be16(msg, ETH_ALEN);
266 wpabuf_put_data(msg, wps->wps->dev.mac_addr, ETH_ALEN);
271 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *plain)
273 if (wps->wps->ap_settings) {
274 wpa_printf(MSG_DEBUG, "WPS: * AP Settings (pre-configured)");
275 wpabuf_put_data(plain, wps->wps->ap_settings,
276 wps->wps->ap_settings_len);
280 return wps_build_cred_ssid(wps, plain) ||
281 wps_build_cred_mac_addr(wps, plain) ||
282 wps_build_cred_auth_type(wps, plain) ||
283 wps_build_cred_encr_type(wps, plain) ||
284 wps_build_cred_network_key(wps, plain);
288 static struct wpabuf * wps_build_m7(struct wps_data *wps)
290 struct wpabuf *msg, *plain;
292 wpa_printf(MSG_DEBUG, "WPS: Building Message M7");
294 plain = wpabuf_alloc(500 + wps->wps->ap_settings_len);
298 msg = wpabuf_alloc(1000 + wps->wps->ap_settings_len);
304 if (wps_build_version(msg) ||
305 wps_build_msg_type(msg, WPS_M7) ||
306 wps_build_registrar_nonce(wps, msg) ||
307 wps_build_e_snonce2(wps, plain) ||
308 (wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
309 wps_build_key_wrap_auth(wps, plain) ||
310 wps_build_encr_settings(wps, msg, plain) ||
311 wps_build_authenticator(wps, msg)) {
318 wps->state = RECV_M8;
323 static struct wpabuf * wps_build_wsc_done(struct wps_data *wps)
327 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_Done");
329 msg = wpabuf_alloc(1000);
333 if (wps_build_version(msg) ||
334 wps_build_msg_type(msg, WPS_WSC_DONE) ||
335 wps_build_enrollee_nonce(wps, msg) ||
336 wps_build_registrar_nonce(wps, msg)) {
342 wps->state = RECV_ACK;
344 wps_success_event(wps->wps);
345 wps->state = WPS_FINISHED;
351 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
355 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
357 msg = wpabuf_alloc(1000);
361 if (wps_build_version(msg) ||
362 wps_build_msg_type(msg, WPS_WSC_ACK) ||
363 wps_build_enrollee_nonce(wps, msg) ||
364 wps_build_registrar_nonce(wps, msg)) {
373 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
377 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
379 msg = wpabuf_alloc(1000);
383 if (wps_build_version(msg) ||
384 wps_build_msg_type(msg, WPS_WSC_NACK) ||
385 wps_build_enrollee_nonce(wps, msg) ||
386 wps_build_registrar_nonce(wps, msg) ||
387 wps_build_config_error(msg, wps->config_error)) {
396 struct wpabuf * wps_enrollee_get_msg(struct wps_data *wps,
397 enum wsc_op_code *op_code)
401 switch (wps->state) {
403 msg = wps_build_m1(wps);
407 msg = wps_build_m3(wps);
411 msg = wps_build_m5(wps);
415 msg = wps_build_m7(wps);
420 msg = wps_build_wsc_nack(wps);
424 msg = wps_build_wsc_ack(wps);
427 /* Another M2/M2D may be received */
428 wps->state = RECV_M2;
432 msg = wps_build_wsc_nack(wps);
436 msg = wps_build_wsc_done(wps);
440 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
441 "a message", wps->state);
446 if (*op_code == WSC_MSG && msg) {
447 /* Save a copy of the last message for Authenticator derivation
449 wpabuf_free(wps->last_msg);
450 wps->last_msg = wpabuf_dup(msg);
457 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
459 if (r_nonce == NULL) {
460 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
464 os_memcpy(wps->nonce_r, r_nonce, WPS_NONCE_LEN);
465 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
466 wps->nonce_r, WPS_NONCE_LEN);
472 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
474 if (e_nonce == NULL) {
475 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
479 if (os_memcmp(wps->nonce_e, e_nonce, WPS_NONCE_LEN) != 0) {
480 wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce received");
488 static int wps_process_uuid_r(struct wps_data *wps, const u8 *uuid_r)
490 if (uuid_r == NULL) {
491 wpa_printf(MSG_DEBUG, "WPS: No UUID-R received");
495 os_memcpy(wps->uuid_r, uuid_r, WPS_UUID_LEN);
496 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
502 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
505 if (pk == NULL || pk_len == 0) {
506 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
510 wpabuf_free(wps->dh_pubkey_r);
511 wps->dh_pubkey_r = wpabuf_alloc_copy(pk, pk_len);
512 if (wps->dh_pubkey_r == NULL)
515 if (wps_derive_keys(wps) < 0)
518 if (wps->request_type == WPS_REQ_WLAN_MANAGER_REGISTRAR &&
519 wps_derive_mgmt_keys(wps) < 0)
526 static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1)
528 if (r_hash1 == NULL) {
529 wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received");
533 os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN);
534 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN);
540 static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2)
542 if (r_hash2 == NULL) {
543 wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received");
547 os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN);
548 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN);
554 static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
556 u8 hash[SHA256_MAC_LEN];
560 if (r_snonce1 == NULL) {
561 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
565 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1,
566 WPS_SECRET_NONCE_LEN);
568 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
570 len[0] = WPS_SECRET_NONCE_LEN;
572 len[1] = WPS_PSK_LEN;
573 addr[2] = wpabuf_head(wps->dh_pubkey_e);
574 len[2] = wpabuf_len(wps->dh_pubkey_e);
575 addr[3] = wpabuf_head(wps->dh_pubkey_r);
576 len[3] = wpabuf_len(wps->dh_pubkey_r);
577 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
579 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
580 wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does "
581 "not match with the pre-committed value");
582 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
583 wps_pwd_auth_fail_event(wps->wps, 1, 1);
587 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
588 "half of the device password");
594 static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
596 u8 hash[SHA256_MAC_LEN];
600 if (r_snonce2 == NULL) {
601 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
605 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2,
606 WPS_SECRET_NONCE_LEN);
608 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
610 len[0] = WPS_SECRET_NONCE_LEN;
612 len[1] = WPS_PSK_LEN;
613 addr[2] = wpabuf_head(wps->dh_pubkey_e);
614 len[2] = wpabuf_len(wps->dh_pubkey_e);
615 addr[3] = wpabuf_head(wps->dh_pubkey_r);
616 len[3] = wpabuf_len(wps->dh_pubkey_r);
617 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
619 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
620 wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does "
621 "not match with the pre-committed value");
622 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
623 wps_pwd_auth_fail_event(wps->wps, 1, 2);
627 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
628 "half of the device password");
634 static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
637 struct wps_parse_attr attr;
640 wpa_printf(MSG_DEBUG, "WPS: Received Credential");
641 os_memset(&wps->cred, 0, sizeof(wps->cred));
642 wpabuf_set(&msg, cred, cred_len);
643 if (wps_parse_msg(&msg, &attr) < 0 ||
644 wps_process_cred(&attr, &wps->cred))
647 if (wps->wps->cred_cb) {
648 wps->cred.cred_attr = cred - 4;
649 wps->cred.cred_attr_len = cred_len + 4;
650 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
651 wps->cred.cred_attr = NULL;
652 wps->cred.cred_attr_len = 0;
659 static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
660 size_t cred_len[], size_t num_cred)
668 wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
673 for (i = 0; i < num_cred; i++) {
674 if (wps_process_cred_e(wps, cred[i], cred_len[i]))
682 static int wps_process_ap_settings_e(struct wps_data *wps,
683 struct wps_parse_attr *attr,
684 struct wpabuf *attrs)
686 struct wps_credential cred;
691 if (wps_process_ap_settings(attr, &cred) < 0)
694 wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
697 if (wps->wps->cred_cb) {
698 cred.cred_attr = wpabuf_head(attrs);
699 cred.cred_attr_len = wpabuf_len(attrs);
700 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
707 static enum wps_process_res wps_process_m2(struct wps_data *wps,
708 const struct wpabuf *msg,
709 struct wps_parse_attr *attr)
711 wpa_printf(MSG_DEBUG, "WPS: Received M2");
713 if (wps->state != RECV_M2) {
714 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
715 "receiving M2", wps->state);
716 wps->state = SEND_WSC_NACK;
720 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
721 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
722 wps_process_uuid_r(wps, attr->uuid_r) ||
723 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
724 wps_process_authenticator(wps, attr->authenticator, msg)) {
725 wps->state = SEND_WSC_NACK;
729 if (wps->wps->ap && wps->wps->ap_setup_locked) {
730 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
731 "registration of a new Registrar");
732 wps->config_error = WPS_CFG_SETUP_LOCKED;
733 wps->state = SEND_WSC_NACK;
737 wps->state = SEND_M3;
742 static enum wps_process_res wps_process_m2d(struct wps_data *wps,
743 struct wps_parse_attr *attr)
745 wpa_printf(MSG_DEBUG, "WPS: Received M2D");
747 if (wps->state != RECV_M2) {
748 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
749 "receiving M2D", wps->state);
750 wps->state = SEND_WSC_NACK;
754 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
755 attr->manufacturer, attr->manufacturer_len);
756 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
757 attr->model_name, attr->model_name_len);
758 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
759 attr->model_number, attr->model_number_len);
760 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
761 attr->serial_number, attr->serial_number_len);
762 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
763 attr->dev_name, attr->dev_name_len);
765 if (wps->wps->event_cb) {
766 union wps_event_data data;
767 struct wps_event_m2d *m2d = &data.m2d;
768 os_memset(&data, 0, sizeof(data));
769 if (attr->config_methods)
770 m2d->config_methods =
771 WPA_GET_BE16(attr->config_methods);
772 m2d->manufacturer = attr->manufacturer;
773 m2d->manufacturer_len = attr->manufacturer_len;
774 m2d->model_name = attr->model_name;
775 m2d->model_name_len = attr->model_name_len;
776 m2d->model_number = attr->model_number;
777 m2d->model_number_len = attr->model_number_len;
778 m2d->serial_number = attr->serial_number;
779 m2d->serial_number_len = attr->serial_number_len;
780 m2d->dev_name = attr->dev_name;
781 m2d->dev_name_len = attr->dev_name_len;
782 m2d->primary_dev_type = attr->primary_dev_type;
783 if (attr->config_error)
785 WPA_GET_BE16(attr->config_error);
786 if (attr->dev_password_id)
787 m2d->dev_password_id =
788 WPA_GET_BE16(attr->dev_password_id);
789 wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_M2D, &data);
792 wps->state = RECEIVED_M2D;
797 static enum wps_process_res wps_process_m4(struct wps_data *wps,
798 const struct wpabuf *msg,
799 struct wps_parse_attr *attr)
801 struct wpabuf *decrypted;
802 struct wps_parse_attr eattr;
804 wpa_printf(MSG_DEBUG, "WPS: Received M4");
806 if (wps->state != RECV_M4) {
807 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
808 "receiving M4", wps->state);
809 wps->state = SEND_WSC_NACK;
813 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
814 wps_process_authenticator(wps, attr->authenticator, msg) ||
815 wps_process_r_hash1(wps, attr->r_hash1) ||
816 wps_process_r_hash2(wps, attr->r_hash2)) {
817 wps->state = SEND_WSC_NACK;
821 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
822 attr->encr_settings_len);
823 if (decrypted == NULL) {
824 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
825 "Settings attribute");
826 wps->state = SEND_WSC_NACK;
830 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
832 if (wps_parse_msg(decrypted, &eattr) < 0 ||
833 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
834 wps_process_r_snonce1(wps, eattr.r_snonce1)) {
835 wpabuf_free(decrypted);
836 wps->state = SEND_WSC_NACK;
839 wpabuf_free(decrypted);
841 wps->state = SEND_M5;
846 static enum wps_process_res wps_process_m6(struct wps_data *wps,
847 const struct wpabuf *msg,
848 struct wps_parse_attr *attr)
850 struct wpabuf *decrypted;
851 struct wps_parse_attr eattr;
853 wpa_printf(MSG_DEBUG, "WPS: Received M6");
855 if (wps->state != RECV_M6) {
856 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
857 "receiving M6", wps->state);
858 wps->state = SEND_WSC_NACK;
862 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
863 wps_process_authenticator(wps, attr->authenticator, msg)) {
864 wps->state = SEND_WSC_NACK;
868 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
869 attr->encr_settings_len);
870 if (decrypted == NULL) {
871 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
872 "Settings attribute");
873 wps->state = SEND_WSC_NACK;
877 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
879 if (wps_parse_msg(decrypted, &eattr) < 0 ||
880 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
881 wps_process_r_snonce2(wps, eattr.r_snonce2)) {
882 wpabuf_free(decrypted);
883 wps->state = SEND_WSC_NACK;
886 wpabuf_free(decrypted);
888 wps->state = SEND_M7;
893 static enum wps_process_res wps_process_m8(struct wps_data *wps,
894 const struct wpabuf *msg,
895 struct wps_parse_attr *attr)
897 struct wpabuf *decrypted;
898 struct wps_parse_attr eattr;
900 wpa_printf(MSG_DEBUG, "WPS: Received M8");
902 if (wps->state != RECV_M8) {
903 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
904 "receiving M8", wps->state);
905 wps->state = SEND_WSC_NACK;
909 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
910 wps_process_authenticator(wps, attr->authenticator, msg)) {
911 wps->state = SEND_WSC_NACK;
915 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
916 attr->encr_settings_len);
917 if (decrypted == NULL) {
918 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
919 "Settings attribute");
920 wps->state = SEND_WSC_NACK;
924 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
926 if (wps_parse_msg(decrypted, &eattr) < 0 ||
927 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
928 wps_process_creds(wps, eattr.cred, eattr.cred_len,
930 wps_process_ap_settings_e(wps, &eattr, decrypted)) {
931 wpabuf_free(decrypted);
932 wps->state = SEND_WSC_NACK;
935 wpabuf_free(decrypted);
937 wps->state = WPS_MSG_DONE;
942 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
943 const struct wpabuf *msg)
945 struct wps_parse_attr attr;
946 enum wps_process_res ret = WPS_CONTINUE;
948 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
950 if (wps_parse_msg(msg, &attr) < 0)
953 if (attr.version == NULL || *attr.version != WPS_VERSION) {
954 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
955 attr.version ? *attr.version : 0);
959 if (attr.enrollee_nonce == NULL ||
960 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
961 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
965 if (attr.msg_type == NULL) {
966 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
970 switch (*attr.msg_type) {
972 ret = wps_process_m2(wps, msg, &attr);
975 ret = wps_process_m2d(wps, &attr);
978 ret = wps_process_m4(wps, msg, &attr);
979 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
980 wps_fail_event(wps->wps, WPS_M4);
983 ret = wps_process_m6(wps, msg, &attr);
984 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
985 wps_fail_event(wps->wps, WPS_M6);
988 ret = wps_process_m8(wps, msg, &attr);
989 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
990 wps_fail_event(wps->wps, WPS_M8);
993 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
999 * Save a copy of the last message for Authenticator derivation if we
1000 * are continuing. However, skip M2D since it is not authenticated and
1001 * neither is the ACK/NACK response frame. This allows the possibly
1002 * following M2 to be processed correctly by using the previously sent
1003 * M1 in Authenticator derivation.
1005 if (ret == WPS_CONTINUE && *attr.msg_type != WPS_M2D) {
1006 /* Save a copy of the last message for Authenticator derivation
1008 wpabuf_free(wps->last_msg);
1009 wps->last_msg = wpabuf_dup(msg);
1016 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1017 const struct wpabuf *msg)
1019 struct wps_parse_attr attr;
1021 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1023 if (wps_parse_msg(msg, &attr) < 0)
1026 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1027 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1028 attr.version ? *attr.version : 0);
1032 if (attr.msg_type == NULL) {
1033 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1037 if (*attr.msg_type != WPS_WSC_ACK) {
1038 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1043 if (attr.registrar_nonce == NULL ||
1044 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1046 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1050 if (attr.enrollee_nonce == NULL ||
1051 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1052 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1056 if (wps->state == RECV_ACK && wps->wps->ap) {
1057 wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
1058 "completed successfully");
1059 wps_success_event(wps->wps);
1060 wps->state = WPS_FINISHED;
1068 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1069 const struct wpabuf *msg)
1071 struct wps_parse_attr attr;
1073 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1075 if (wps_parse_msg(msg, &attr) < 0)
1078 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1079 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1080 attr.version ? *attr.version : 0);
1084 if (attr.msg_type == NULL) {
1085 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1089 if (*attr.msg_type != WPS_WSC_NACK) {
1090 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1095 if (attr.registrar_nonce == NULL ||
1096 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1098 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1099 wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
1100 attr.registrar_nonce, WPS_NONCE_LEN);
1101 wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
1102 wps->nonce_r, WPS_NONCE_LEN);
1106 if (attr.enrollee_nonce == NULL ||
1107 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1108 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1109 wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
1110 attr.enrollee_nonce, WPS_NONCE_LEN);
1111 wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
1112 wps->nonce_e, WPS_NONCE_LEN);
1116 if (attr.config_error == NULL) {
1117 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1122 wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
1123 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1125 switch (wps->state) {
1127 wps_fail_event(wps->wps, WPS_M3);
1130 wps_fail_event(wps->wps, WPS_M5);
1133 wps_fail_event(wps->wps, WPS_M7);
1139 /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1140 * Enrollee is Authenticator */
1141 wps->state = SEND_WSC_NACK;
1147 enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps,
1148 enum wsc_op_code op_code,
1149 const struct wpabuf *msg)
1152 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1154 (unsigned long) wpabuf_len(msg), op_code);
1158 return wps_process_wsc_msg(wps, msg);
1160 return wps_process_wsc_ack(wps, msg);
1162 return wps_process_wsc_nack(wps, msg);
1164 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);