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_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 struct wpabuf * wps_build_m7(struct wps_data *wps)
273 struct wpabuf *msg, *plain;
275 wpa_printf(MSG_DEBUG, "WPS: Building Message M7");
277 plain = wpabuf_alloc(500);
281 msg = wpabuf_alloc(1000);
287 if (wps_build_version(msg) ||
288 wps_build_msg_type(msg, WPS_M7) ||
289 wps_build_registrar_nonce(wps, msg) ||
290 wps_build_e_snonce2(wps, plain) ||
291 (wps->authenticator &&
292 (wps_build_cred_ssid(wps, plain) ||
293 wps_build_cred_mac_addr(wps, plain) ||
294 wps_build_cred_auth_type(wps, plain) ||
295 wps_build_cred_encr_type(wps, plain) ||
296 wps_build_cred_network_key(wps, plain))) ||
297 wps_build_key_wrap_auth(wps, plain) ||
298 wps_build_encr_settings(wps, msg, plain) ||
299 wps_build_authenticator(wps, msg)) {
306 wps->state = RECV_M8;
311 static struct wpabuf * wps_build_wsc_done(struct wps_data *wps)
315 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_Done");
317 msg = wpabuf_alloc(1000);
321 if (wps_build_version(msg) ||
322 wps_build_msg_type(msg, WPS_WSC_DONE) ||
323 wps_build_enrollee_nonce(wps, msg) ||
324 wps_build_registrar_nonce(wps, msg)) {
329 wps->state = wps->authenticator ? RECV_ACK : WPS_FINISHED;
334 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
338 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
340 msg = wpabuf_alloc(1000);
344 if (wps_build_version(msg) ||
345 wps_build_msg_type(msg, WPS_WSC_ACK) ||
346 wps_build_enrollee_nonce(wps, msg) ||
347 wps_build_registrar_nonce(wps, msg)) {
356 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
360 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
362 msg = wpabuf_alloc(1000);
366 if (wps_build_version(msg) ||
367 wps_build_msg_type(msg, WPS_WSC_NACK) ||
368 wps_build_enrollee_nonce(wps, msg) ||
369 wps_build_registrar_nonce(wps, msg) ||
370 wps_build_config_error(msg, wps->config_error)) {
379 struct wpabuf * wps_enrollee_get_msg(struct wps_data *wps, u8 *op_code)
383 switch (wps->state) {
385 msg = wps_build_m1(wps);
389 msg = wps_build_m3(wps);
393 msg = wps_build_m5(wps);
397 msg = wps_build_m7(wps);
401 if (wps->authenticator) {
402 msg = wps_build_wsc_nack(wps);
406 msg = wps_build_wsc_ack(wps);
409 /* Another M2/M2D may be received */
410 wps->state = RECV_M2;
414 msg = wps_build_wsc_nack(wps);
418 msg = wps_build_wsc_done(wps);
422 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
423 "a message", wps->state);
428 if (*op_code == WSC_MSG && msg) {
429 /* Save a copy of the last message for Authenticator derivation
431 wpabuf_free(wps->last_msg);
432 wps->last_msg = wpabuf_dup(msg);
439 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
441 if (r_nonce == NULL) {
442 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
446 os_memcpy(wps->nonce_r, r_nonce, WPS_NONCE_LEN);
447 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
448 wps->nonce_r, WPS_NONCE_LEN);
454 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
456 if (e_nonce == NULL) {
457 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
461 if (os_memcmp(wps->nonce_e, e_nonce, WPS_NONCE_LEN) != 0) {
462 wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce received");
470 static int wps_process_uuid_r(struct wps_data *wps, const u8 *uuid_r)
472 if (uuid_r == NULL) {
473 wpa_printf(MSG_DEBUG, "WPS: No UUID-R received");
477 os_memcpy(wps->uuid_r, uuid_r, WPS_UUID_LEN);
478 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
484 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
487 if (pk == NULL || pk_len == 0) {
488 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
492 wpabuf_free(wps->dh_pubkey_r);
493 wps->dh_pubkey_r = wpabuf_alloc_copy(pk, pk_len);
494 if (wps->dh_pubkey_r == NULL)
497 if (wps_derive_keys(wps) < 0)
500 if (wps->request_type == WPS_REQ_WLAN_MANAGER_REGISTRAR &&
501 wps_derive_mgmt_keys(wps) < 0)
508 static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1)
510 if (r_hash1 == NULL) {
511 wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received");
515 os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN);
516 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN);
522 static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2)
524 if (r_hash2 == NULL) {
525 wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received");
529 os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN);
530 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN);
536 static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
538 u8 hash[SHA256_MAC_LEN];
542 if (r_snonce1 == NULL) {
543 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
547 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1,
548 WPS_SECRET_NONCE_LEN);
550 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
552 len[0] = WPS_SECRET_NONCE_LEN;
554 len[1] = WPS_PSK_LEN;
555 addr[2] = wpabuf_head(wps->dh_pubkey_e);
556 len[2] = wpabuf_len(wps->dh_pubkey_e);
557 addr[3] = wpabuf_head(wps->dh_pubkey_r);
558 len[3] = wpabuf_len(wps->dh_pubkey_r);
559 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
561 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
562 wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does "
563 "not match with the pre-committed value");
564 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
568 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
569 "half of the device password");
575 static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
577 u8 hash[SHA256_MAC_LEN];
581 if (r_snonce2 == NULL) {
582 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
586 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2,
587 WPS_SECRET_NONCE_LEN);
589 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
591 len[0] = WPS_SECRET_NONCE_LEN;
593 len[1] = WPS_PSK_LEN;
594 addr[2] = wpabuf_head(wps->dh_pubkey_e);
595 len[2] = wpabuf_len(wps->dh_pubkey_e);
596 addr[3] = wpabuf_head(wps->dh_pubkey_r);
597 len[3] = wpabuf_len(wps->dh_pubkey_r);
598 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
600 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
601 wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does "
602 "not match with the pre-committed value");
603 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
607 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
608 "half of the device password");
614 static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
617 struct wps_parse_attr attr;
620 wpa_printf(MSG_DEBUG, "WPS: Received Credential");
621 os_memset(&wps->cred, 0, sizeof(wps->cred));
622 wpabuf_set(&msg, cred, cred_len);
623 if (wps_parse_msg(&msg, &attr) < 0 ||
624 wps_process_cred(&attr, &wps->cred))
627 if (wps->wps->cred_cb)
628 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
634 static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
635 size_t cred_len[], size_t num_cred)
639 if (wps->authenticator)
643 wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
648 for (i = 0; i < num_cred; i++) {
649 if (wps_process_cred_e(wps, cred[i], cred_len[i]))
657 static int wps_process_ap_settings_e(struct wps_data *wps,
658 struct wps_parse_attr *attr)
660 struct wps_credential cred;
662 if (!wps->authenticator)
665 if (wps_process_ap_settings(attr, &cred) < 0)
668 wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
671 if (wps->wps->cred_cb)
672 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
678 static enum wps_process_res wps_process_m2(struct wps_data *wps,
679 const struct wpabuf *msg,
680 struct wps_parse_attr *attr)
682 wpa_printf(MSG_DEBUG, "WPS: Received M2");
684 if (wps->state != RECV_M2) {
685 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
686 "receiving M2", wps->state);
687 wps->state = SEND_WSC_NACK;
691 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
692 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
693 wps_process_uuid_r(wps, attr->uuid_r) ||
694 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
695 wps_process_authenticator(wps, attr->authenticator, msg)) {
696 wps->state = SEND_WSC_NACK;
700 if (wps->authenticator && wps->wps->ap_setup_locked) {
701 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
702 "registration of a new Registrar");
703 wps->config_error = WPS_CFG_SETUP_LOCKED;
704 wps->state = SEND_WSC_NACK;
708 wps->state = SEND_M3;
713 static enum wps_process_res wps_process_m2d(struct wps_data *wps,
714 struct wps_parse_attr *attr)
716 wpa_printf(MSG_DEBUG, "WPS: Received M2D");
718 if (wps->state != RECV_M2) {
719 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
720 "receiving M2D", wps->state);
721 wps->state = SEND_WSC_NACK;
725 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
726 attr->manufacturer, attr->manufacturer_len);
727 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
728 attr->model_name, attr->model_name_len);
729 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
730 attr->model_number, attr->model_number_len);
731 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
732 attr->serial_number, attr->serial_number_len);
733 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
734 attr->dev_name, attr->dev_name_len);
737 * TODO: notify monitor programs (cli/gui/etc.) of the M2D and provide
738 * user information about the registrar properties.
741 wps->state = RECEIVED_M2D;
746 static enum wps_process_res wps_process_m4(struct wps_data *wps,
747 const struct wpabuf *msg,
748 struct wps_parse_attr *attr)
750 struct wpabuf *decrypted;
751 struct wps_parse_attr eattr;
753 wpa_printf(MSG_DEBUG, "WPS: Received M4");
755 if (wps->state != RECV_M4) {
756 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
757 "receiving M4", wps->state);
758 wps->state = SEND_WSC_NACK;
762 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
763 wps_process_authenticator(wps, attr->authenticator, msg) ||
764 wps_process_r_hash1(wps, attr->r_hash1) ||
765 wps_process_r_hash2(wps, attr->r_hash2)) {
766 wps->state = SEND_WSC_NACK;
770 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
771 attr->encr_settings_len);
772 if (decrypted == NULL) {
773 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
774 "Settings attribute");
775 wps->state = SEND_WSC_NACK;
779 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
781 if (wps_parse_msg(decrypted, &eattr) < 0 ||
782 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
783 wps_process_r_snonce1(wps, eattr.r_snonce1)) {
784 wpabuf_free(decrypted);
785 wps->state = SEND_WSC_NACK;
788 wpabuf_free(decrypted);
790 wps->state = SEND_M5;
795 static enum wps_process_res wps_process_m6(struct wps_data *wps,
796 const struct wpabuf *msg,
797 struct wps_parse_attr *attr)
799 struct wpabuf *decrypted;
800 struct wps_parse_attr eattr;
802 wpa_printf(MSG_DEBUG, "WPS: Received M6");
804 if (wps->state != RECV_M6) {
805 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
806 "receiving M6", wps->state);
807 wps->state = SEND_WSC_NACK;
811 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
812 wps_process_authenticator(wps, attr->authenticator, msg)) {
813 wps->state = SEND_WSC_NACK;
817 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
818 attr->encr_settings_len);
819 if (decrypted == NULL) {
820 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
821 "Settings attribute");
822 wps->state = SEND_WSC_NACK;
826 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
828 if (wps_parse_msg(decrypted, &eattr) < 0 ||
829 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
830 wps_process_r_snonce2(wps, eattr.r_snonce2)) {
831 wpabuf_free(decrypted);
832 wps->state = SEND_WSC_NACK;
835 wpabuf_free(decrypted);
837 wps->state = SEND_M7;
842 static enum wps_process_res wps_process_m8(struct wps_data *wps,
843 const struct wpabuf *msg,
844 struct wps_parse_attr *attr)
846 struct wpabuf *decrypted;
847 struct wps_parse_attr eattr;
849 wpa_printf(MSG_DEBUG, "WPS: Received M8");
851 if (wps->state != RECV_M8) {
852 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
853 "receiving M8", wps->state);
854 wps->state = SEND_WSC_NACK;
858 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
859 wps_process_authenticator(wps, attr->authenticator, msg)) {
860 wps->state = SEND_WSC_NACK;
864 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
865 attr->encr_settings_len);
866 if (decrypted == NULL) {
867 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
868 "Settings attribute");
869 wps->state = SEND_WSC_NACK;
873 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
875 if (wps_parse_msg(decrypted, &eattr) < 0 ||
876 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
877 wps_process_creds(wps, eattr.cred, eattr.cred_len,
879 wps_process_ap_settings_e(wps, &eattr)) {
880 wpabuf_free(decrypted);
881 wps->state = SEND_WSC_NACK;
884 wpabuf_free(decrypted);
886 wps->state = WPS_MSG_DONE;
891 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
892 const struct wpabuf *msg)
894 struct wps_parse_attr attr;
895 enum wps_process_res ret = WPS_CONTINUE;
897 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
899 if (wps_parse_msg(msg, &attr) < 0)
902 if (attr.version == NULL || *attr.version != WPS_VERSION) {
903 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
904 attr.version ? *attr.version : 0);
908 if (attr.enrollee_nonce == NULL ||
909 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
910 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
914 if (attr.msg_type == NULL) {
915 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
919 switch (*attr.msg_type) {
921 ret = wps_process_m2(wps, msg, &attr);
924 ret = wps_process_m2d(wps, &attr);
927 ret = wps_process_m4(wps, msg, &attr);
930 ret = wps_process_m6(wps, msg, &attr);
933 ret = wps_process_m8(wps, msg, &attr);
936 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
941 if (ret == WPS_CONTINUE) {
942 /* Save a copy of the last message for Authenticator derivation
944 wpabuf_free(wps->last_msg);
945 wps->last_msg = wpabuf_dup(msg);
952 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
953 const struct wpabuf *msg)
955 struct wps_parse_attr attr;
957 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
959 if (wps_parse_msg(msg, &attr) < 0)
962 if (attr.version == NULL || *attr.version != WPS_VERSION) {
963 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
964 attr.version ? *attr.version : 0);
968 if (attr.msg_type == NULL) {
969 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
973 if (*attr.msg_type != WPS_WSC_ACK) {
974 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
979 if (attr.registrar_nonce == NULL ||
980 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
982 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
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 (wps->state == RECV_ACK && wps->authenticator) {
993 wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
994 "completed successfully");
995 wps->state = WPS_FINISHED;
1003 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1004 const struct wpabuf *msg)
1006 struct wps_parse_attr attr;
1008 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1010 if (wps_parse_msg(msg, &attr) < 0)
1013 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1014 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1015 attr.version ? *attr.version : 0);
1019 if (attr.msg_type == NULL) {
1020 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1024 if (*attr.msg_type != WPS_WSC_NACK) {
1025 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1030 if (attr.registrar_nonce == NULL ||
1031 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1033 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1034 wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
1035 attr.registrar_nonce, WPS_NONCE_LEN);
1036 wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
1037 wps->nonce_r, WPS_NONCE_LEN);
1041 if (attr.enrollee_nonce == NULL ||
1042 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1043 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1044 wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
1045 attr.enrollee_nonce, WPS_NONCE_LEN);
1046 wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
1047 wps->nonce_e, WPS_NONCE_LEN);
1051 if (attr.config_error == NULL) {
1052 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1057 wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
1058 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1064 enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps, u8 op_code,
1065 const struct wpabuf *msg)
1068 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1070 (unsigned long) wpabuf_len(msg), op_code);
1074 return wps_process_wsc_msg(wps, msg);
1076 return wps_process_wsc_ack(wps, msg);
1078 return wps_process_wsc_nack(wps, msg);
1080 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);