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) ||
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)) {
330 wps->state = RECV_ACK;
332 wps_success_event(wps->wps);
333 wps->state = WPS_FINISHED;
339 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
343 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
345 msg = wpabuf_alloc(1000);
349 if (wps_build_version(msg) ||
350 wps_build_msg_type(msg, WPS_WSC_ACK) ||
351 wps_build_enrollee_nonce(wps, msg) ||
352 wps_build_registrar_nonce(wps, msg)) {
361 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
365 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
367 msg = wpabuf_alloc(1000);
371 if (wps_build_version(msg) ||
372 wps_build_msg_type(msg, WPS_WSC_NACK) ||
373 wps_build_enrollee_nonce(wps, msg) ||
374 wps_build_registrar_nonce(wps, msg) ||
375 wps_build_config_error(msg, wps->config_error)) {
384 struct wpabuf * wps_enrollee_get_msg(struct wps_data *wps,
385 enum wsc_op_code *op_code)
389 switch (wps->state) {
391 msg = wps_build_m1(wps);
395 msg = wps_build_m3(wps);
399 msg = wps_build_m5(wps);
403 msg = wps_build_m7(wps);
408 msg = wps_build_wsc_nack(wps);
412 msg = wps_build_wsc_ack(wps);
415 /* Another M2/M2D may be received */
416 wps->state = RECV_M2;
420 msg = wps_build_wsc_nack(wps);
424 msg = wps_build_wsc_done(wps);
428 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
429 "a message", wps->state);
434 if (*op_code == WSC_MSG && msg) {
435 /* Save a copy of the last message for Authenticator derivation
437 wpabuf_free(wps->last_msg);
438 wps->last_msg = wpabuf_dup(msg);
445 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
447 if (r_nonce == NULL) {
448 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
452 os_memcpy(wps->nonce_r, r_nonce, WPS_NONCE_LEN);
453 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
454 wps->nonce_r, WPS_NONCE_LEN);
460 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
462 if (e_nonce == NULL) {
463 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
467 if (os_memcmp(wps->nonce_e, e_nonce, WPS_NONCE_LEN) != 0) {
468 wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce received");
476 static int wps_process_uuid_r(struct wps_data *wps, const u8 *uuid_r)
478 if (uuid_r == NULL) {
479 wpa_printf(MSG_DEBUG, "WPS: No UUID-R received");
483 os_memcpy(wps->uuid_r, uuid_r, WPS_UUID_LEN);
484 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
490 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
493 if (pk == NULL || pk_len == 0) {
494 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
498 wpabuf_free(wps->dh_pubkey_r);
499 wps->dh_pubkey_r = wpabuf_alloc_copy(pk, pk_len);
500 if (wps->dh_pubkey_r == NULL)
503 if (wps_derive_keys(wps) < 0)
506 if (wps->request_type == WPS_REQ_WLAN_MANAGER_REGISTRAR &&
507 wps_derive_mgmt_keys(wps) < 0)
514 static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1)
516 if (r_hash1 == NULL) {
517 wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received");
521 os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN);
522 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN);
528 static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2)
530 if (r_hash2 == NULL) {
531 wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received");
535 os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN);
536 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN);
542 static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
544 u8 hash[SHA256_MAC_LEN];
548 if (r_snonce1 == NULL) {
549 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
553 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1,
554 WPS_SECRET_NONCE_LEN);
556 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
558 len[0] = WPS_SECRET_NONCE_LEN;
560 len[1] = WPS_PSK_LEN;
561 addr[2] = wpabuf_head(wps->dh_pubkey_e);
562 len[2] = wpabuf_len(wps->dh_pubkey_e);
563 addr[3] = wpabuf_head(wps->dh_pubkey_r);
564 len[3] = wpabuf_len(wps->dh_pubkey_r);
565 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
567 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
568 wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does "
569 "not match with the pre-committed value");
570 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
574 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
575 "half of the device password");
581 static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
583 u8 hash[SHA256_MAC_LEN];
587 if (r_snonce2 == NULL) {
588 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
592 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2,
593 WPS_SECRET_NONCE_LEN);
595 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || 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_hash2, hash, WPS_HASH_LEN) != 0) {
607 wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does "
608 "not match with the pre-committed value");
609 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
613 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
614 "half of the device password");
620 static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
623 struct wps_parse_attr attr;
626 wpa_printf(MSG_DEBUG, "WPS: Received Credential");
627 os_memset(&wps->cred, 0, sizeof(wps->cred));
628 wpabuf_set(&msg, cred, cred_len);
629 if (wps_parse_msg(&msg, &attr) < 0 ||
630 wps_process_cred(&attr, &wps->cred))
633 if (wps->wps->cred_cb)
634 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
640 static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
641 size_t cred_len[], size_t num_cred)
649 wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
654 for (i = 0; i < num_cred; i++) {
655 if (wps_process_cred_e(wps, cred[i], cred_len[i]))
663 static int wps_process_ap_settings_e(struct wps_data *wps,
664 struct wps_parse_attr *attr)
666 struct wps_credential cred;
671 if (wps_process_ap_settings(attr, &cred) < 0)
674 wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
677 if (wps->wps->cred_cb)
678 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
684 static enum wps_process_res wps_process_m2(struct wps_data *wps,
685 const struct wpabuf *msg,
686 struct wps_parse_attr *attr)
688 wpa_printf(MSG_DEBUG, "WPS: Received M2");
690 if (wps->state != RECV_M2) {
691 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
692 "receiving M2", wps->state);
693 wps->state = SEND_WSC_NACK;
697 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
698 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
699 wps_process_uuid_r(wps, attr->uuid_r) ||
700 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
701 wps_process_authenticator(wps, attr->authenticator, msg)) {
702 wps->state = SEND_WSC_NACK;
706 if (wps->wps->ap && wps->wps->ap_setup_locked) {
707 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
708 "registration of a new Registrar");
709 wps->config_error = WPS_CFG_SETUP_LOCKED;
710 wps->state = SEND_WSC_NACK;
714 wps->state = SEND_M3;
719 static enum wps_process_res wps_process_m2d(struct wps_data *wps,
720 struct wps_parse_attr *attr)
722 wpa_printf(MSG_DEBUG, "WPS: Received M2D");
724 if (wps->state != RECV_M2) {
725 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
726 "receiving M2D", wps->state);
727 wps->state = SEND_WSC_NACK;
731 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
732 attr->manufacturer, attr->manufacturer_len);
733 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
734 attr->model_name, attr->model_name_len);
735 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
736 attr->model_number, attr->model_number_len);
737 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
738 attr->serial_number, attr->serial_number_len);
739 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
740 attr->dev_name, attr->dev_name_len);
742 if (wps->wps->event_cb) {
743 union wps_event_data data;
744 struct wps_event_m2d *m2d = &data.m2d;
745 os_memset(&data, 0, sizeof(data));
746 if (attr->config_methods)
747 m2d->config_methods =
748 WPA_GET_BE16(attr->config_methods);
749 m2d->manufacturer = attr->manufacturer;
750 m2d->manufacturer_len = attr->manufacturer_len;
751 m2d->model_name = attr->model_name;
752 m2d->model_name_len = attr->model_name_len;
753 m2d->model_number = attr->model_number;
754 m2d->model_number_len = attr->model_number_len;
755 m2d->serial_number = attr->serial_number;
756 m2d->serial_number_len = attr->serial_number_len;
757 m2d->dev_name = attr->dev_name;
758 m2d->dev_name_len = attr->dev_name_len;
759 m2d->primary_dev_type = attr->primary_dev_type;
760 if (attr->config_error)
762 WPA_GET_BE16(attr->config_error);
763 if (attr->dev_password_id)
764 m2d->dev_password_id =
765 WPA_GET_BE16(attr->dev_password_id);
766 wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_M2D, &data);
769 wps->state = RECEIVED_M2D;
774 static enum wps_process_res wps_process_m4(struct wps_data *wps,
775 const struct wpabuf *msg,
776 struct wps_parse_attr *attr)
778 struct wpabuf *decrypted;
779 struct wps_parse_attr eattr;
781 wpa_printf(MSG_DEBUG, "WPS: Received M4");
783 if (wps->state != RECV_M4) {
784 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
785 "receiving M4", wps->state);
786 wps->state = SEND_WSC_NACK;
790 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
791 wps_process_authenticator(wps, attr->authenticator, msg) ||
792 wps_process_r_hash1(wps, attr->r_hash1) ||
793 wps_process_r_hash2(wps, attr->r_hash2)) {
794 wps->state = SEND_WSC_NACK;
798 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
799 attr->encr_settings_len);
800 if (decrypted == NULL) {
801 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
802 "Settings attribute");
803 wps->state = SEND_WSC_NACK;
807 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
809 if (wps_parse_msg(decrypted, &eattr) < 0 ||
810 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
811 wps_process_r_snonce1(wps, eattr.r_snonce1)) {
812 wpabuf_free(decrypted);
813 wps->state = SEND_WSC_NACK;
816 wpabuf_free(decrypted);
818 wps->state = SEND_M5;
823 static enum wps_process_res wps_process_m6(struct wps_data *wps,
824 const struct wpabuf *msg,
825 struct wps_parse_attr *attr)
827 struct wpabuf *decrypted;
828 struct wps_parse_attr eattr;
830 wpa_printf(MSG_DEBUG, "WPS: Received M6");
832 if (wps->state != RECV_M6) {
833 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
834 "receiving M6", wps->state);
835 wps->state = SEND_WSC_NACK;
839 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
840 wps_process_authenticator(wps, attr->authenticator, msg)) {
841 wps->state = SEND_WSC_NACK;
845 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
846 attr->encr_settings_len);
847 if (decrypted == NULL) {
848 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
849 "Settings attribute");
850 wps->state = SEND_WSC_NACK;
854 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
856 if (wps_parse_msg(decrypted, &eattr) < 0 ||
857 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
858 wps_process_r_snonce2(wps, eattr.r_snonce2)) {
859 wpabuf_free(decrypted);
860 wps->state = SEND_WSC_NACK;
863 wpabuf_free(decrypted);
865 wps->state = SEND_M7;
870 static enum wps_process_res wps_process_m8(struct wps_data *wps,
871 const struct wpabuf *msg,
872 struct wps_parse_attr *attr)
874 struct wpabuf *decrypted;
875 struct wps_parse_attr eattr;
877 wpa_printf(MSG_DEBUG, "WPS: Received M8");
879 if (wps->state != RECV_M8) {
880 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
881 "receiving M8", wps->state);
882 wps->state = SEND_WSC_NACK;
886 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
887 wps_process_authenticator(wps, attr->authenticator, msg)) {
888 wps->state = SEND_WSC_NACK;
892 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
893 attr->encr_settings_len);
894 if (decrypted == NULL) {
895 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
896 "Settings attribute");
897 wps->state = SEND_WSC_NACK;
901 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
903 if (wps_parse_msg(decrypted, &eattr) < 0 ||
904 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
905 wps_process_creds(wps, eattr.cred, eattr.cred_len,
907 wps_process_ap_settings_e(wps, &eattr)) {
908 wpabuf_free(decrypted);
909 wps->state = SEND_WSC_NACK;
912 wpabuf_free(decrypted);
914 wps->state = WPS_MSG_DONE;
919 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
920 const struct wpabuf *msg)
922 struct wps_parse_attr attr;
923 enum wps_process_res ret = WPS_CONTINUE;
925 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
927 if (wps_parse_msg(msg, &attr) < 0)
930 if (attr.version == NULL || *attr.version != WPS_VERSION) {
931 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
932 attr.version ? *attr.version : 0);
936 if (attr.enrollee_nonce == NULL ||
937 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
938 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
942 if (attr.msg_type == NULL) {
943 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
947 switch (*attr.msg_type) {
949 ret = wps_process_m2(wps, msg, &attr);
952 ret = wps_process_m2d(wps, &attr);
955 ret = wps_process_m4(wps, msg, &attr);
956 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
957 wps_fail_event(wps->wps, WPS_M4);
960 ret = wps_process_m6(wps, msg, &attr);
961 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
962 wps_fail_event(wps->wps, WPS_M6);
965 ret = wps_process_m8(wps, msg, &attr);
966 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
967 wps_fail_event(wps->wps, WPS_M8);
970 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
975 if (ret == WPS_CONTINUE) {
976 /* Save a copy of the last message for Authenticator derivation
978 wpabuf_free(wps->last_msg);
979 wps->last_msg = wpabuf_dup(msg);
986 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
987 const struct wpabuf *msg)
989 struct wps_parse_attr attr;
991 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
993 if (wps_parse_msg(msg, &attr) < 0)
996 if (attr.version == NULL || *attr.version != WPS_VERSION) {
997 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
998 attr.version ? *attr.version : 0);
1002 if (attr.msg_type == NULL) {
1003 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1007 if (*attr.msg_type != WPS_WSC_ACK) {
1008 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1013 if (attr.registrar_nonce == NULL ||
1014 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1016 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1020 if (attr.enrollee_nonce == NULL ||
1021 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1022 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1026 if (wps->state == RECV_ACK && wps->wps->ap) {
1027 wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
1028 "completed successfully");
1029 wps_success_event(wps->wps);
1030 wps->state = WPS_FINISHED;
1038 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1039 const struct wpabuf *msg)
1041 struct wps_parse_attr attr;
1043 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1045 if (wps_parse_msg(msg, &attr) < 0)
1048 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1049 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1050 attr.version ? *attr.version : 0);
1054 if (attr.msg_type == NULL) {
1055 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1059 if (*attr.msg_type != WPS_WSC_NACK) {
1060 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1065 if (attr.registrar_nonce == NULL ||
1066 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1068 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1069 wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
1070 attr.registrar_nonce, WPS_NONCE_LEN);
1071 wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
1072 wps->nonce_r, WPS_NONCE_LEN);
1076 if (attr.enrollee_nonce == NULL ||
1077 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1078 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1079 wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
1080 attr.enrollee_nonce, WPS_NONCE_LEN);
1081 wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
1082 wps->nonce_e, WPS_NONCE_LEN);
1086 if (attr.config_error == NULL) {
1087 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1092 wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
1093 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1095 switch (wps->state) {
1097 wps_fail_event(wps->wps, WPS_M3);
1100 wps_fail_event(wps->wps, WPS_M5);
1103 wps_fail_event(wps->wps, WPS_M7);
1109 /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1110 * Enrollee is Authenticator */
1111 wps->state = SEND_WSC_NACK;
1117 enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps,
1118 enum wsc_op_code op_code,
1119 const struct wpabuf *msg)
1122 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1124 (unsigned long) wpabuf_len(msg), op_code);
1128 return wps_process_wsc_msg(wps, msg);
1130 return wps_process_wsc_ack(wps, msg);
1132 return wps_process_wsc_nack(wps, msg);
1134 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);