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.
19 #include "ieee802_11_defs.h"
21 #include "wps_dev_attr.h"
24 static int wps_build_req_type(struct wpabuf *msg, enum wps_request_type type)
26 wpa_printf(MSG_DEBUG, "WPS: * Request Type");
27 wpabuf_put_be16(msg, ATTR_REQUEST_TYPE);
28 wpabuf_put_be16(msg, 1);
29 wpabuf_put_u8(msg, type);
34 static int wps_build_uuid_e(struct wpabuf *msg, const u8 *uuid)
36 wpa_printf(MSG_DEBUG, "WPS: * UUID-E");
37 wpabuf_put_be16(msg, ATTR_UUID_E);
38 wpabuf_put_be16(msg, WPS_UUID_LEN);
39 wpabuf_put_data(msg, uuid, WPS_UUID_LEN);
44 static int wps_build_mac_addr(struct wps_data *wps, struct wpabuf *msg)
46 wpa_printf(MSG_DEBUG, "WPS: * MAC Address");
47 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
48 wpabuf_put_be16(msg, ETH_ALEN);
49 wpabuf_put_data(msg, wps->mac_addr_e, ETH_ALEN);
54 static int wps_build_config_methods(struct wpabuf *msg, u16 methods)
56 wpa_printf(MSG_DEBUG, "WPS: * Config Methods");
57 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
58 wpabuf_put_be16(msg, 2);
59 wpabuf_put_be16(msg, methods);
64 static int wps_build_wps_state(struct wps_data *wps, struct wpabuf *msg)
66 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State");
67 wpabuf_put_be16(msg, ATTR_WPS_STATE);
68 wpabuf_put_be16(msg, 1);
69 wpabuf_put_u8(msg, WPS_STATE_CONFIGURED);
74 static int wps_build_rf_bands(struct wps_data *wps, struct wpabuf *msg)
76 wpa_printf(MSG_DEBUG, "WPS: * RF Bands");
77 wpabuf_put_be16(msg, ATTR_RF_BANDS);
78 wpabuf_put_be16(msg, 1);
79 wpabuf_put_u8(msg, WPS_RF_24GHZ | WPS_RF_50GHZ);
84 static int wps_build_dev_password_id(struct wpabuf *msg, u16 id)
86 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID");
87 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
88 wpabuf_put_be16(msg, 2);
89 wpabuf_put_be16(msg, id);
94 static int wps_build_config_error(struct wps_data *wps, struct wpabuf *msg)
96 u16 err = WPS_CFG_NO_ERROR;
97 wpabuf_put_be16(msg, ATTR_CONFIG_ERROR);
98 wpabuf_put_be16(msg, 2);
99 if (wps && wps->authenticator && wps->wps->ap_setup_locked)
100 err = WPS_CFG_SETUP_LOCKED;
101 wpa_printf(MSG_DEBUG, "WPS: * Configuration Error (%d)", err);
102 wpabuf_put_be16(msg, err);
107 static int wps_build_e_hash(struct wps_data *wps, struct wpabuf *msg)
113 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
115 wpa_hexdump(MSG_DEBUG, "WPS: E-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
116 wpa_hexdump(MSG_DEBUG, "WPS: E-S2",
117 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
119 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
120 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
121 "E-Hash derivation");
125 wpa_printf(MSG_DEBUG, "WPS: * E-Hash1");
126 wpabuf_put_be16(msg, ATTR_E_HASH1);
127 wpabuf_put_be16(msg, SHA256_MAC_LEN);
128 hash = wpabuf_put(msg, SHA256_MAC_LEN);
129 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
130 addr[0] = wps->snonce;
131 len[0] = WPS_SECRET_NONCE_LEN;
133 len[1] = WPS_PSK_LEN;
134 addr[2] = wpabuf_head(wps->dh_pubkey_e);
135 len[2] = wpabuf_len(wps->dh_pubkey_e);
136 addr[3] = wpabuf_head(wps->dh_pubkey_r);
137 len[3] = wpabuf_len(wps->dh_pubkey_r);
138 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
139 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", hash, SHA256_MAC_LEN);
141 wpa_printf(MSG_DEBUG, "WPS: * E-Hash2");
142 wpabuf_put_be16(msg, ATTR_E_HASH2);
143 wpabuf_put_be16(msg, SHA256_MAC_LEN);
144 hash = wpabuf_put(msg, SHA256_MAC_LEN);
145 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
146 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
148 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
149 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", hash, SHA256_MAC_LEN);
155 static int wps_build_e_snonce1(struct wps_data *wps, struct wpabuf *msg)
157 wpa_printf(MSG_DEBUG, "WPS: * E-SNonce1");
158 wpabuf_put_be16(msg, ATTR_E_SNONCE1);
159 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
160 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
165 static int wps_build_e_snonce2(struct wps_data *wps, struct wpabuf *msg)
167 wpa_printf(MSG_DEBUG, "WPS: * E-SNonce2");
168 wpabuf_put_be16(msg, ATTR_E_SNONCE2);
169 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
170 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
171 WPS_SECRET_NONCE_LEN);
176 static struct wpabuf * wps_build_m1(struct wps_data *wps)
181 if (os_get_random(wps->nonce_e, WPS_NONCE_LEN) < 0)
183 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
184 wps->nonce_e, WPS_NONCE_LEN);
186 wpa_printf(MSG_DEBUG, "WPS: Building Message M1");
187 msg = wpabuf_alloc(1000);
191 methods = WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
193 methods |= WPS_CONFIG_PUSHBUTTON;
195 if (wps_build_version(msg) ||
196 wps_build_msg_type(msg, WPS_M1) ||
197 wps_build_uuid_e(msg, wps->uuid_e) ||
198 wps_build_mac_addr(wps, msg) ||
199 wps_build_enrollee_nonce(wps, msg) ||
200 wps_build_public_key(wps, msg) ||
201 wps_build_auth_type_flags(wps, msg) ||
202 wps_build_encr_type_flags(wps, msg) ||
203 wps_build_conn_type_flags(wps, msg) ||
204 wps_build_config_methods(msg, methods) ||
205 wps_build_wps_state(wps, msg) ||
206 wps_build_device_attrs(&wps->wps->dev, msg) ||
207 wps_build_rf_bands(wps, msg) ||
208 wps_build_assoc_state(wps, msg) ||
209 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
210 wps_build_config_error(wps, msg) ||
211 wps_build_os_version(&wps->wps->dev, msg)) {
216 wps->state = RECV_M2;
221 static struct wpabuf * wps_build_m3(struct wps_data *wps)
225 wpa_printf(MSG_DEBUG, "WPS: Building Message M3");
227 if (wps->dev_password == NULL) {
228 wpa_printf(MSG_DEBUG, "WPS: No Device Password available");
231 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
233 msg = wpabuf_alloc(1000);
237 if (wps_build_version(msg) ||
238 wps_build_msg_type(msg, WPS_M3) ||
239 wps_build_registrar_nonce(wps, msg) ||
240 wps_build_e_hash(wps, msg) ||
241 wps_build_authenticator(wps, msg)) {
246 wps->state = RECV_M4;
251 static struct wpabuf * wps_build_m5(struct wps_data *wps)
253 struct wpabuf *msg, *plain;
255 wpa_printf(MSG_DEBUG, "WPS: Building Message M5");
257 plain = wpabuf_alloc(200);
261 msg = wpabuf_alloc(1000);
267 if (wps_build_version(msg) ||
268 wps_build_msg_type(msg, WPS_M5) ||
269 wps_build_registrar_nonce(wps, msg) ||
270 wps_build_e_snonce1(wps, plain) ||
271 wps_build_key_wrap_auth(wps, plain) ||
272 wps_build_encr_settings(wps, msg, plain) ||
273 wps_build_authenticator(wps, msg)) {
280 wps->state = RECV_M6;
285 static int wps_build_cred_ssid(struct wps_data *wps, struct wpabuf *msg)
287 wpa_printf(MSG_DEBUG, "WPS: * SSID");
288 wpabuf_put_be16(msg, ATTR_SSID);
289 wpabuf_put_be16(msg, wps->wps->ssid_len);
290 wpabuf_put_data(msg, wps->wps->ssid, wps->wps->ssid_len);
295 static int wps_build_cred_auth_type(struct wps_data *wps, struct wpabuf *msg)
297 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type");
298 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
299 wpabuf_put_be16(msg, 2);
300 wpabuf_put_be16(msg, wps->wps->auth_types);
305 static int wps_build_cred_encr_type(struct wps_data *wps, struct wpabuf *msg)
307 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type");
308 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
309 wpabuf_put_be16(msg, 2);
310 wpabuf_put_be16(msg, wps->wps->encr_types);
315 static int wps_build_cred_network_key(struct wps_data *wps, struct wpabuf *msg)
317 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
318 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
319 wpabuf_put_be16(msg, wps->wps->network_key_len);
320 wpabuf_put_data(msg, wps->wps->network_key, wps->wps->network_key_len);
325 static int wps_build_cred_mac_addr(struct wps_data *wps, struct wpabuf *msg)
327 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (AP BSSID)");
328 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
329 wpabuf_put_be16(msg, ETH_ALEN);
330 wpabuf_put_data(msg, wps->wps->dev.mac_addr, ETH_ALEN);
335 static struct wpabuf * wps_build_m7(struct wps_data *wps)
337 struct wpabuf *msg, *plain;
339 wpa_printf(MSG_DEBUG, "WPS: Building Message M7");
341 plain = wpabuf_alloc(500);
345 msg = wpabuf_alloc(1000);
351 if (wps_build_version(msg) ||
352 wps_build_msg_type(msg, WPS_M7) ||
353 wps_build_registrar_nonce(wps, msg) ||
354 wps_build_e_snonce2(wps, plain) ||
355 (wps->authenticator &&
356 (wps_build_cred_ssid(wps, plain) ||
357 wps_build_cred_mac_addr(wps, plain) ||
358 wps_build_cred_auth_type(wps, plain) ||
359 wps_build_cred_encr_type(wps, plain) ||
360 wps_build_cred_network_key(wps, plain))) ||
361 wps_build_key_wrap_auth(wps, plain) ||
362 wps_build_encr_settings(wps, msg, plain) ||
363 wps_build_authenticator(wps, msg)) {
370 wps->state = RECV_M8;
375 static struct wpabuf * wps_build_wsc_done(struct wps_data *wps)
379 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_Done");
381 msg = wpabuf_alloc(1000);
385 if (wps_build_version(msg) ||
386 wps_build_msg_type(msg, WPS_WSC_DONE) ||
387 wps_build_enrollee_nonce(wps, msg) ||
388 wps_build_registrar_nonce(wps, msg)) {
393 wps->state = wps->authenticator ? RECV_ACK : WPS_FINISHED;
398 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
402 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
404 msg = wpabuf_alloc(1000);
408 if (wps_build_version(msg) ||
409 wps_build_msg_type(msg, WPS_WSC_ACK) ||
410 wps_build_enrollee_nonce(wps, msg) ||
411 wps_build_registrar_nonce(wps, msg)) {
420 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
424 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
426 msg = wpabuf_alloc(1000);
430 if (wps_build_version(msg) ||
431 wps_build_msg_type(msg, WPS_WSC_NACK) ||
432 wps_build_enrollee_nonce(wps, msg) ||
433 wps_build_registrar_nonce(wps, msg) ||
434 wps_build_config_error(wps, msg)) {
443 struct wpabuf * wps_enrollee_get_msg(struct wps_data *wps, u8 *op_code)
447 switch (wps->state) {
449 msg = wps_build_m1(wps);
453 msg = wps_build_m3(wps);
457 msg = wps_build_m5(wps);
461 msg = wps_build_m7(wps);
465 msg = wps_build_wsc_ack(wps);
468 /* Another M2/M2D may be received */
469 wps->state = RECV_M2;
473 msg = wps_build_wsc_nack(wps);
477 msg = wps_build_wsc_done(wps);
481 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
482 "a message", wps->state);
487 if (*op_code == WSC_MSG && msg) {
488 /* Save a copy of the last message for Authenticator derivation
490 wpabuf_free(wps->last_msg);
491 wps->last_msg = wpabuf_dup(msg);
498 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
500 if (r_nonce == NULL) {
501 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
505 os_memcpy(wps->nonce_r, r_nonce, WPS_NONCE_LEN);
506 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
507 wps->nonce_r, WPS_NONCE_LEN);
513 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
515 if (e_nonce == NULL) {
516 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
520 if (os_memcmp(wps->nonce_e, e_nonce, WPS_NONCE_LEN) != 0) {
521 wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce received");
529 static int wps_process_uuid_r(struct wps_data *wps, const u8 *uuid_r)
531 if (uuid_r == NULL) {
532 wpa_printf(MSG_DEBUG, "WPS: No UUID-R received");
536 os_memcpy(wps->uuid_r, uuid_r, WPS_UUID_LEN);
537 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
543 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
546 if (pk == NULL || pk_len == 0) {
547 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
551 wpabuf_free(wps->dh_pubkey_r);
552 wps->dh_pubkey_r = wpabuf_alloc_copy(pk, pk_len);
553 if (wps->dh_pubkey_r == NULL)
556 if (wps_derive_keys(wps) < 0)
559 if (wps->request_type == WPS_REQ_WLAN_MANAGER_REGISTRAR &&
560 wps_derive_mgmt_keys(wps) < 0)
567 static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1)
569 if (r_hash1 == NULL) {
570 wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received");
574 os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN);
575 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN);
581 static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2)
583 if (r_hash2 == NULL) {
584 wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received");
588 os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN);
589 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN);
595 static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
597 u8 hash[SHA256_MAC_LEN];
601 if (r_snonce1 == NULL) {
602 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
606 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1,
607 WPS_SECRET_NONCE_LEN);
609 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
611 len[0] = WPS_SECRET_NONCE_LEN;
613 len[1] = WPS_PSK_LEN;
614 addr[2] = wpabuf_head(wps->dh_pubkey_e);
615 len[2] = wpabuf_len(wps->dh_pubkey_e);
616 addr[3] = wpabuf_head(wps->dh_pubkey_r);
617 len[3] = wpabuf_len(wps->dh_pubkey_r);
618 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
620 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
621 wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does "
622 "not match with the pre-committed value");
626 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
627 "half of the device password");
633 static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
635 u8 hash[SHA256_MAC_LEN];
639 if (r_snonce2 == NULL) {
640 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
644 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2,
645 WPS_SECRET_NONCE_LEN);
647 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
649 len[0] = WPS_SECRET_NONCE_LEN;
651 len[1] = WPS_PSK_LEN;
652 addr[2] = wpabuf_head(wps->dh_pubkey_e);
653 len[2] = wpabuf_len(wps->dh_pubkey_e);
654 addr[3] = wpabuf_head(wps->dh_pubkey_r);
655 len[3] = wpabuf_len(wps->dh_pubkey_r);
656 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
658 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
659 wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does "
660 "not match with the pre-committed value");
664 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
665 "half of the device password");
671 static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
674 struct wps_parse_attr attr;
677 wpa_printf(MSG_DEBUG, "WPS: Received Credential");
678 os_memset(&wps->cred, 0, sizeof(wps->cred));
679 wpabuf_set(&msg, cred, cred_len);
680 if (wps_parse_msg(&msg, &attr) < 0 ||
681 wps_process_cred(&attr, &wps->cred))
684 if (wps->wps->cred_cb)
685 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
691 static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
692 size_t cred_len[], size_t num_cred)
696 if (wps->authenticator)
700 wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
705 for (i = 0; i < num_cred; i++) {
706 if (wps_process_cred_e(wps, cred[i], cred_len[i]))
714 static int wps_process_ap_settings_e(struct wps_data *wps,
715 struct wps_parse_attr *attr)
717 struct wps_credential cred;
719 if (!wps->authenticator)
722 if (wps_process_ap_settings(attr, &cred) < 0)
725 wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
728 if (wps->wps->cred_cb)
729 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
735 static enum wps_process_res wps_process_m2(struct wps_data *wps,
736 const struct wpabuf *msg,
737 struct wps_parse_attr *attr)
739 wpa_printf(MSG_DEBUG, "WPS: Received M2");
741 if (wps->state != RECV_M2) {
742 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
743 "receiving M2", wps->state);
747 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
748 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
749 wps_process_uuid_r(wps, attr->uuid_r) ||
750 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
751 wps_process_authenticator(wps, attr->authenticator, msg))
754 if (wps->authenticator && wps->wps->ap_setup_locked) {
755 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
756 "registration of a new Registrar");
757 wps->state = SEND_WSC_NACK;
761 wps->state = SEND_M3;
766 static enum wps_process_res wps_process_m2d(struct wps_data *wps,
767 struct wps_parse_attr *attr)
769 wpa_printf(MSG_DEBUG, "WPS: Received M2D");
771 if (wps->state != RECV_M2) {
772 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
773 "receiving M2D", wps->state);
777 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
778 attr->manufacturer, attr->manufacturer_len);
779 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
780 attr->model_name, attr->model_name_len);
781 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
782 attr->model_number, attr->model_number_len);
783 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
784 attr->serial_number, attr->serial_number_len);
785 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
786 attr->dev_name, attr->dev_name_len);
789 * TODO: notify monitor programs (cli/gui/etc.) of the M2D and provide
790 * user information about the registrar properties.
793 wps->state = RECEIVED_M2D;
798 static enum wps_process_res wps_process_m4(struct wps_data *wps,
799 const struct wpabuf *msg,
800 struct wps_parse_attr *attr)
802 struct wpabuf *decrypted;
803 struct wps_parse_attr eattr;
805 wpa_printf(MSG_DEBUG, "WPS: Received M4");
807 if (wps->state != RECV_M4) {
808 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
809 "receiving M4", wps->state);
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))
819 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
820 attr->encr_settings_len);
821 if (decrypted == NULL) {
822 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
823 "Settings attribute");
827 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
829 if (wps_parse_msg(decrypted, &eattr) < 0 ||
830 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
831 wps_process_r_snonce1(wps, eattr.r_snonce1)) {
832 wpabuf_free(decrypted);
835 wpabuf_free(decrypted);
837 wps->state = SEND_M5;
842 static enum wps_process_res wps_process_m6(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 M6");
851 if (wps->state != RECV_M6) {
852 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
853 "receiving M6", wps->state);
857 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
858 wps_process_authenticator(wps, attr->authenticator, msg))
861 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
862 attr->encr_settings_len);
863 if (decrypted == NULL) {
864 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
865 "Settings attribute");
869 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
871 if (wps_parse_msg(decrypted, &eattr) < 0 ||
872 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
873 wps_process_r_snonce2(wps, eattr.r_snonce2)) {
874 wpabuf_free(decrypted);
877 wpabuf_free(decrypted);
879 wps->state = SEND_M7;
884 static enum wps_process_res wps_process_m8(struct wps_data *wps,
885 const struct wpabuf *msg,
886 struct wps_parse_attr *attr)
888 struct wpabuf *decrypted;
889 struct wps_parse_attr eattr;
891 wpa_printf(MSG_DEBUG, "WPS: Received M8");
893 if (wps->state != RECV_M8) {
894 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
895 "receiving M8", wps->state);
899 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
900 wps_process_authenticator(wps, attr->authenticator, msg))
903 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
904 attr->encr_settings_len);
905 if (decrypted == NULL) {
906 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
907 "Settings attribute");
911 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
913 if (wps_parse_msg(decrypted, &eattr) < 0 ||
914 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
915 wps_process_creds(wps, eattr.cred, eattr.cred_len,
917 wps_process_ap_settings_e(wps, &eattr)) {
918 wpabuf_free(decrypted);
921 wpabuf_free(decrypted);
923 wps->state = WPS_MSG_DONE;
928 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
929 const struct wpabuf *msg)
931 struct wps_parse_attr attr;
932 enum wps_process_res ret = WPS_CONTINUE;
934 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
936 if (wps_parse_msg(msg, &attr) < 0)
939 if (attr.version == NULL || *attr.version != WPS_VERSION) {
940 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
941 attr.version ? *attr.version : 0);
945 if (attr.enrollee_nonce == NULL ||
946 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
947 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
951 if (attr.msg_type == NULL) {
952 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
956 switch (*attr.msg_type) {
958 ret = wps_process_m2(wps, msg, &attr);
961 ret = wps_process_m2d(wps, &attr);
964 ret = wps_process_m4(wps, msg, &attr);
967 ret = wps_process_m6(wps, msg, &attr);
970 ret = wps_process_m8(wps, msg, &attr);
973 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
978 if (ret == WPS_CONTINUE) {
979 /* Save a copy of the last message for Authenticator derivation
981 wpabuf_free(wps->last_msg);
982 wps->last_msg = wpabuf_dup(msg);
989 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
990 const struct wpabuf *msg)
992 struct wps_parse_attr attr;
994 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
996 if (wps_parse_msg(msg, &attr) < 0)
999 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1000 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1001 attr.version ? *attr.version : 0);
1005 if (attr.msg_type == NULL) {
1006 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1010 if (*attr.msg_type != WPS_WSC_ACK) {
1011 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1016 if (attr.registrar_nonce == NULL ||
1017 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1019 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1023 if (attr.enrollee_nonce == NULL ||
1024 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1025 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1029 if (wps->state == RECV_ACK && wps->authenticator) {
1030 wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
1031 "completed successfully");
1032 wps->state = WPS_FINISHED;
1040 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1041 const struct wpabuf *msg)
1043 struct wps_parse_attr attr;
1045 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1047 if (wps_parse_msg(msg, &attr) < 0)
1050 if (attr.version == NULL || *attr.version != WPS_VERSION) {
1051 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1052 attr.version ? *attr.version : 0);
1056 if (attr.msg_type == NULL) {
1057 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1061 if (*attr.msg_type != WPS_WSC_NACK) {
1062 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1067 if (attr.registrar_nonce == NULL ||
1068 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1070 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1071 wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
1072 attr.registrar_nonce, WPS_NONCE_LEN);
1073 wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
1074 wps->nonce_r, WPS_NONCE_LEN);
1078 if (attr.enrollee_nonce == NULL ||
1079 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1080 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1081 wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
1082 attr.enrollee_nonce, WPS_NONCE_LEN);
1083 wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
1084 wps->nonce_e, WPS_NONCE_LEN);
1088 if (attr.config_error == NULL) {
1089 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1094 wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
1095 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1101 enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps, u8 op_code,
1102 const struct wpabuf *msg)
1105 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1107 (unsigned long) wpabuf_len(msg), op_code);
1111 return wps_process_wsc_msg(wps, msg);
1113 return wps_process_wsc_ack(wps, msg);
1115 return wps_process_wsc_nack(wps, msg);
1117 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
1123 struct wpabuf * wps_enrollee_build_assoc_req_ie(void)
1128 wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for (Re)Association "
1130 ie = wpabuf_alloc(100);
1134 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
1135 len = wpabuf_put(ie, 1);
1136 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
1138 if (wps_build_version(ie) ||
1139 wps_build_req_type(ie, WPS_REQ_ENROLLEE)) {
1144 *len = wpabuf_len(ie) - 2;
1150 struct wpabuf * wps_enrollee_build_probe_req_ie(int pbc, const u8 *uuid)
1155 struct wps_device_data dev;
1157 wpa_printf(MSG_DEBUG, "WPS: Building WPS IE for Probe Request");
1159 /* TODO: get device data from caller */
1160 os_memset(&dev, 0, sizeof(dev));
1161 dev.categ = WPS_DEV_COMPUTER;
1162 dev.oui = WPS_DEV_OUI_WFA;
1163 dev.sub_categ = WPS_DEV_COMPUTER_PC;
1165 ie = wpabuf_alloc(200);
1169 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
1170 len = wpabuf_put(ie, 1);
1171 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
1174 methods = WPS_CONFIG_PUSHBUTTON;
1176 methods = WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY |
1179 if (wps_build_version(ie) ||
1180 wps_build_req_type(ie, WPS_REQ_ENROLLEE) ||
1181 wps_build_config_methods(ie, methods) ||
1182 wps_build_uuid_e(ie, uuid) ||
1183 wps_build_primary_dev_type(&dev, ie) ||
1184 wps_build_rf_bands(NULL, ie) ||
1185 wps_build_assoc_state(NULL, ie) ||
1186 wps_build_config_error(NULL, ie) ||
1187 wps_build_dev_password_id(ie, pbc ? DEV_PW_PUSHBUTTON :
1193 *len = wpabuf_len(ie) - 2;