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"
24 static int wps_build_mac_addr(struct wps_data *wps, struct wpabuf *msg)
26 wpa_printf(MSG_DEBUG, "WPS: * MAC Address");
27 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
28 wpabuf_put_be16(msg, ETH_ALEN);
29 wpabuf_put_data(msg, wps->mac_addr_e, ETH_ALEN);
34 static int wps_build_wps_state(struct wps_data *wps, struct wpabuf *msg)
38 state = wps->wps->wps_state;
40 state = WPS_STATE_NOT_CONFIGURED;
41 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
43 wpabuf_put_be16(msg, ATTR_WPS_STATE);
44 wpabuf_put_be16(msg, 1);
45 wpabuf_put_u8(msg, state);
50 static int wps_build_e_hash(struct wps_data *wps, struct wpabuf *msg)
56 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
58 wpa_hexdump(MSG_DEBUG, "WPS: E-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
59 wpa_hexdump(MSG_DEBUG, "WPS: E-S2",
60 wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
62 if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
63 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
68 wpa_printf(MSG_DEBUG, "WPS: * E-Hash1");
69 wpabuf_put_be16(msg, ATTR_E_HASH1);
70 wpabuf_put_be16(msg, SHA256_MAC_LEN);
71 hash = wpabuf_put(msg, SHA256_MAC_LEN);
72 /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
73 addr[0] = wps->snonce;
74 len[0] = WPS_SECRET_NONCE_LEN;
77 addr[2] = wpabuf_head(wps->dh_pubkey_e);
78 len[2] = wpabuf_len(wps->dh_pubkey_e);
79 addr[3] = wpabuf_head(wps->dh_pubkey_r);
80 len[3] = wpabuf_len(wps->dh_pubkey_r);
81 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
82 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", hash, SHA256_MAC_LEN);
84 wpa_printf(MSG_DEBUG, "WPS: * E-Hash2");
85 wpabuf_put_be16(msg, ATTR_E_HASH2);
86 wpabuf_put_be16(msg, SHA256_MAC_LEN);
87 hash = wpabuf_put(msg, SHA256_MAC_LEN);
88 /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
89 addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
91 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
92 wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", hash, SHA256_MAC_LEN);
98 static int wps_build_e_snonce1(struct wps_data *wps, struct wpabuf *msg)
100 wpa_printf(MSG_DEBUG, "WPS: * E-SNonce1");
101 wpabuf_put_be16(msg, ATTR_E_SNONCE1);
102 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
103 wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
108 static int wps_build_e_snonce2(struct wps_data *wps, struct wpabuf *msg)
110 wpa_printf(MSG_DEBUG, "WPS: * E-SNonce2");
111 wpabuf_put_be16(msg, ATTR_E_SNONCE2);
112 wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
113 wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
114 WPS_SECRET_NONCE_LEN);
119 static struct wpabuf * wps_build_m1(struct wps_data *wps)
124 if (os_get_random(wps->nonce_e, WPS_NONCE_LEN) < 0)
126 wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
127 wps->nonce_e, WPS_NONCE_LEN);
129 wpa_printf(MSG_DEBUG, "WPS: Building Message M1");
130 msg = wpabuf_alloc(1000);
134 methods = WPS_CONFIG_LABEL | WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
135 #ifdef CONFIG_WPS_UFD
136 methods |= WPS_CONFIG_USBA;
137 #endif /* CONFIG_WPS_UFD */
138 #ifdef CONFIG_WPS_NFC
139 methods |= WPS_CONFIG_NFC_INTERFACE;
140 #endif /* CONFIG_WPS_NFC */
142 methods |= WPS_CONFIG_PUSHBUTTON;
144 if (wps_build_version(msg) ||
145 wps_build_msg_type(msg, WPS_M1) ||
146 wps_build_uuid_e(msg, wps->uuid_e) ||
147 wps_build_mac_addr(wps, msg) ||
148 wps_build_enrollee_nonce(wps, msg) ||
149 wps_build_public_key(wps, msg) ||
150 wps_build_auth_type_flags(wps, msg) ||
151 wps_build_encr_type_flags(wps, msg) ||
152 wps_build_conn_type_flags(wps, msg) ||
153 wps_build_config_methods(msg, methods) ||
154 wps_build_wps_state(wps, msg) ||
155 wps_build_device_attrs(&wps->wps->dev, msg) ||
156 wps_build_rf_bands(&wps->wps->dev, msg) ||
157 wps_build_assoc_state(wps, msg) ||
158 wps_build_dev_password_id(msg, wps->dev_pw_id) ||
159 wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
160 wps_build_os_version(&wps->wps->dev, msg)) {
165 wps->state = RECV_M2;
170 static struct wpabuf * wps_build_m3(struct wps_data *wps)
174 wpa_printf(MSG_DEBUG, "WPS: Building Message M3");
176 if (wps->dev_password == NULL) {
177 wpa_printf(MSG_DEBUG, "WPS: No Device Password available");
180 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
182 msg = wpabuf_alloc(1000);
186 if (wps_build_version(msg) ||
187 wps_build_msg_type(msg, WPS_M3) ||
188 wps_build_registrar_nonce(wps, msg) ||
189 wps_build_e_hash(wps, msg) ||
190 wps_build_authenticator(wps, msg)) {
195 wps->state = RECV_M4;
200 static struct wpabuf * wps_build_m5(struct wps_data *wps)
202 struct wpabuf *msg, *plain;
204 wpa_printf(MSG_DEBUG, "WPS: Building Message M5");
206 plain = wpabuf_alloc(200);
210 msg = wpabuf_alloc(1000);
216 if (wps_build_version(msg) ||
217 wps_build_msg_type(msg, WPS_M5) ||
218 wps_build_registrar_nonce(wps, msg) ||
219 wps_build_e_snonce1(wps, plain) ||
220 wps_build_key_wrap_auth(wps, plain) ||
221 wps_build_encr_settings(wps, msg, plain) ||
222 wps_build_authenticator(wps, msg)) {
229 wps->state = RECV_M6;
234 static int wps_build_cred_ssid(struct wps_data *wps, struct wpabuf *msg)
236 wpa_printf(MSG_DEBUG, "WPS: * SSID");
237 wpabuf_put_be16(msg, ATTR_SSID);
238 wpabuf_put_be16(msg, wps->wps->ssid_len);
239 wpabuf_put_data(msg, wps->wps->ssid, wps->wps->ssid_len);
244 static int wps_build_cred_auth_type(struct wps_data *wps, struct wpabuf *msg)
246 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type");
247 wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
248 wpabuf_put_be16(msg, 2);
249 wpabuf_put_be16(msg, wps->wps->auth_types);
254 static int wps_build_cred_encr_type(struct wps_data *wps, struct wpabuf *msg)
256 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type");
257 wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
258 wpabuf_put_be16(msg, 2);
259 wpabuf_put_be16(msg, wps->wps->encr_types);
264 static int wps_build_cred_network_key(struct wps_data *wps, struct wpabuf *msg)
266 wpa_printf(MSG_DEBUG, "WPS: * Network Key");
267 wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
268 wpabuf_put_be16(msg, wps->wps->network_key_len);
269 wpabuf_put_data(msg, wps->wps->network_key, wps->wps->network_key_len);
274 static int wps_build_cred_mac_addr(struct wps_data *wps, struct wpabuf *msg)
276 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (AP BSSID)");
277 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
278 wpabuf_put_be16(msg, ETH_ALEN);
279 wpabuf_put_data(msg, wps->wps->dev.mac_addr, ETH_ALEN);
284 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *plain)
286 if (wps->wps->ap_settings) {
287 wpa_printf(MSG_DEBUG, "WPS: * AP Settings (pre-configured)");
288 wpabuf_put_data(plain, wps->wps->ap_settings,
289 wps->wps->ap_settings_len);
293 return wps_build_cred_ssid(wps, plain) ||
294 wps_build_cred_mac_addr(wps, plain) ||
295 wps_build_cred_auth_type(wps, plain) ||
296 wps_build_cred_encr_type(wps, plain) ||
297 wps_build_cred_network_key(wps, plain);
301 static struct wpabuf * wps_build_m7(struct wps_data *wps)
303 struct wpabuf *msg, *plain;
305 wpa_printf(MSG_DEBUG, "WPS: Building Message M7");
307 plain = wpabuf_alloc(500 + wps->wps->ap_settings_len);
311 msg = wpabuf_alloc(1000 + wps->wps->ap_settings_len);
317 if (wps_build_version(msg) ||
318 wps_build_msg_type(msg, WPS_M7) ||
319 wps_build_registrar_nonce(wps, msg) ||
320 wps_build_e_snonce2(wps, plain) ||
321 (wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
322 wps_build_key_wrap_auth(wps, plain) ||
323 wps_build_encr_settings(wps, msg, plain) ||
324 wps_build_authenticator(wps, msg)) {
331 if (wps->wps->ap && wps->wps->registrar) {
333 * If the Registrar is only learning our current configuration,
334 * it may not continue protocol run to successful completion.
335 * Store information here to make sure it remains available.
337 wps_device_store(wps->wps->registrar, &wps->peer_dev,
341 wps->state = RECV_M8;
346 static struct wpabuf * wps_build_wsc_done(struct wps_data *wps)
350 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_Done");
352 msg = wpabuf_alloc(1000);
356 if (wps_build_version(msg) ||
357 wps_build_msg_type(msg, WPS_WSC_DONE) ||
358 wps_build_enrollee_nonce(wps, msg) ||
359 wps_build_registrar_nonce(wps, msg)) {
365 wps->state = RECV_ACK;
367 wps_success_event(wps->wps);
368 wps->state = WPS_FINISHED;
374 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
378 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
380 msg = wpabuf_alloc(1000);
384 if (wps_build_version(msg) ||
385 wps_build_msg_type(msg, WPS_WSC_ACK) ||
386 wps_build_enrollee_nonce(wps, msg) ||
387 wps_build_registrar_nonce(wps, msg)) {
396 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
400 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
402 msg = wpabuf_alloc(1000);
406 if (wps_build_version(msg) ||
407 wps_build_msg_type(msg, WPS_WSC_NACK) ||
408 wps_build_enrollee_nonce(wps, msg) ||
409 wps_build_registrar_nonce(wps, msg) ||
410 wps_build_config_error(msg, wps->config_error)) {
419 struct wpabuf * wps_enrollee_get_msg(struct wps_data *wps,
420 enum wsc_op_code *op_code)
424 switch (wps->state) {
426 msg = wps_build_m1(wps);
430 msg = wps_build_m3(wps);
434 msg = wps_build_m5(wps);
438 msg = wps_build_m7(wps);
443 msg = wps_build_wsc_nack(wps);
447 msg = wps_build_wsc_ack(wps);
450 /* Another M2/M2D may be received */
451 wps->state = RECV_M2;
455 msg = wps_build_wsc_nack(wps);
459 msg = wps_build_wsc_done(wps);
463 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
464 "a message", wps->state);
469 if (*op_code == WSC_MSG && msg) {
470 /* Save a copy of the last message for Authenticator derivation
472 wpabuf_free(wps->last_msg);
473 wps->last_msg = wpabuf_dup(msg);
480 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
482 if (r_nonce == NULL) {
483 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
487 os_memcpy(wps->nonce_r, r_nonce, WPS_NONCE_LEN);
488 wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
489 wps->nonce_r, WPS_NONCE_LEN);
495 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
497 if (e_nonce == NULL) {
498 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
502 if (os_memcmp(wps->nonce_e, e_nonce, WPS_NONCE_LEN) != 0) {
503 wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce received");
511 static int wps_process_uuid_r(struct wps_data *wps, const u8 *uuid_r)
513 if (uuid_r == NULL) {
514 wpa_printf(MSG_DEBUG, "WPS: No UUID-R received");
518 os_memcpy(wps->uuid_r, uuid_r, WPS_UUID_LEN);
519 wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
525 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
528 if (pk == NULL || pk_len == 0) {
529 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
533 #ifdef CONFIG_WPS_OOB
534 if (wps->dev_pw_id != DEV_PW_DEFAULT &&
535 wps->wps->oob_conf.pubkey_hash) {
537 u8 hash[WPS_HASH_LEN];
540 sha256_vector(1, addr, &pk_len, hash);
542 wpabuf_head(wps->wps->oob_conf.pubkey_hash),
543 WPS_OOB_PUBKEY_HASH_LEN) != 0) {
544 wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
548 #endif /* CONFIG_WPS_OOB */
550 wpabuf_free(wps->dh_pubkey_r);
551 wps->dh_pubkey_r = wpabuf_alloc_copy(pk, pk_len);
552 if (wps->dh_pubkey_r == NULL)
555 if (wps_derive_keys(wps) < 0)
562 static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1)
564 if (r_hash1 == NULL) {
565 wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received");
569 os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN);
570 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN);
576 static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2)
578 if (r_hash2 == NULL) {
579 wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received");
583 os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN);
584 wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN);
590 static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
592 u8 hash[SHA256_MAC_LEN];
596 if (r_snonce1 == NULL) {
597 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
601 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1,
602 WPS_SECRET_NONCE_LEN);
604 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
606 len[0] = WPS_SECRET_NONCE_LEN;
608 len[1] = WPS_PSK_LEN;
609 addr[2] = wpabuf_head(wps->dh_pubkey_e);
610 len[2] = wpabuf_len(wps->dh_pubkey_e);
611 addr[3] = wpabuf_head(wps->dh_pubkey_r);
612 len[3] = wpabuf_len(wps->dh_pubkey_r);
613 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
615 if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
616 wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does "
617 "not match with the pre-committed value");
618 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
619 wps_pwd_auth_fail_event(wps->wps, 1, 1);
623 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
624 "half of the device password");
630 static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
632 u8 hash[SHA256_MAC_LEN];
636 if (r_snonce2 == NULL) {
637 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
641 wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2,
642 WPS_SECRET_NONCE_LEN);
644 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
646 len[0] = WPS_SECRET_NONCE_LEN;
648 len[1] = WPS_PSK_LEN;
649 addr[2] = wpabuf_head(wps->dh_pubkey_e);
650 len[2] = wpabuf_len(wps->dh_pubkey_e);
651 addr[3] = wpabuf_head(wps->dh_pubkey_r);
652 len[3] = wpabuf_len(wps->dh_pubkey_r);
653 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
655 if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
656 wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does "
657 "not match with the pre-committed value");
658 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
659 wps_pwd_auth_fail_event(wps->wps, 1, 2);
663 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
664 "half of the device password");
670 static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
673 struct wps_parse_attr attr;
676 wpa_printf(MSG_DEBUG, "WPS: Received Credential");
677 os_memset(&wps->cred, 0, sizeof(wps->cred));
678 wpabuf_set(&msg, cred, cred_len);
679 if (wps_parse_msg(&msg, &attr) < 0 ||
680 wps_process_cred(&attr, &wps->cred))
683 if (wps->wps->cred_cb) {
684 wps->cred.cred_attr = cred - 4;
685 wps->cred.cred_attr_len = cred_len + 4;
686 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
687 wps->cred.cred_attr = NULL;
688 wps->cred.cred_attr_len = 0;
695 static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
696 size_t cred_len[], size_t num_cred)
704 wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
709 for (i = 0; i < num_cred; i++) {
710 if (wps_process_cred_e(wps, cred[i], cred_len[i]))
718 static int wps_process_ap_settings_e(struct wps_data *wps,
719 struct wps_parse_attr *attr,
720 struct wpabuf *attrs)
722 struct wps_credential cred;
727 if (wps_process_ap_settings(attr, &cred) < 0)
730 wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
733 if (wps->wps->cred_cb) {
734 cred.cred_attr = wpabuf_head(attrs);
735 cred.cred_attr_len = wpabuf_len(attrs);
736 wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
743 static enum wps_process_res wps_process_m2(struct wps_data *wps,
744 const struct wpabuf *msg,
745 struct wps_parse_attr *attr)
747 wpa_printf(MSG_DEBUG, "WPS: Received M2");
749 if (wps->state != RECV_M2) {
750 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
751 "receiving M2", wps->state);
752 wps->state = SEND_WSC_NACK;
756 if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
757 wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
758 wps_process_uuid_r(wps, attr->uuid_r) ||
759 wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
760 wps_process_authenticator(wps, attr->authenticator, msg) ||
761 wps_process_device_attrs(&wps->peer_dev, attr)) {
762 wps->state = SEND_WSC_NACK;
766 if (wps->wps->ap && wps->wps->ap_setup_locked) {
767 wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
768 "registration of a new Registrar");
769 wps->config_error = WPS_CFG_SETUP_LOCKED;
770 wps->state = SEND_WSC_NACK;
774 wps->state = SEND_M3;
779 static enum wps_process_res wps_process_m2d(struct wps_data *wps,
780 struct wps_parse_attr *attr)
782 wpa_printf(MSG_DEBUG, "WPS: Received M2D");
784 if (wps->state != RECV_M2) {
785 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
786 "receiving M2D", wps->state);
787 wps->state = SEND_WSC_NACK;
791 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
792 attr->manufacturer, attr->manufacturer_len);
793 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
794 attr->model_name, attr->model_name_len);
795 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
796 attr->model_number, attr->model_number_len);
797 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
798 attr->serial_number, attr->serial_number_len);
799 wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
800 attr->dev_name, attr->dev_name_len);
802 if (wps->wps->event_cb) {
803 union wps_event_data data;
804 struct wps_event_m2d *m2d = &data.m2d;
805 os_memset(&data, 0, sizeof(data));
806 if (attr->config_methods)
807 m2d->config_methods =
808 WPA_GET_BE16(attr->config_methods);
809 m2d->manufacturer = attr->manufacturer;
810 m2d->manufacturer_len = attr->manufacturer_len;
811 m2d->model_name = attr->model_name;
812 m2d->model_name_len = attr->model_name_len;
813 m2d->model_number = attr->model_number;
814 m2d->model_number_len = attr->model_number_len;
815 m2d->serial_number = attr->serial_number;
816 m2d->serial_number_len = attr->serial_number_len;
817 m2d->dev_name = attr->dev_name;
818 m2d->dev_name_len = attr->dev_name_len;
819 m2d->primary_dev_type = attr->primary_dev_type;
820 if (attr->config_error)
822 WPA_GET_BE16(attr->config_error);
823 if (attr->dev_password_id)
824 m2d->dev_password_id =
825 WPA_GET_BE16(attr->dev_password_id);
826 wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_M2D, &data);
829 wps->state = RECEIVED_M2D;
834 static enum wps_process_res wps_process_m4(struct wps_data *wps,
835 const struct wpabuf *msg,
836 struct wps_parse_attr *attr)
838 struct wpabuf *decrypted;
839 struct wps_parse_attr eattr;
841 wpa_printf(MSG_DEBUG, "WPS: Received M4");
843 if (wps->state != RECV_M4) {
844 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
845 "receiving M4", wps->state);
846 wps->state = SEND_WSC_NACK;
850 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
851 wps_process_authenticator(wps, attr->authenticator, msg) ||
852 wps_process_r_hash1(wps, attr->r_hash1) ||
853 wps_process_r_hash2(wps, attr->r_hash2)) {
854 wps->state = SEND_WSC_NACK;
858 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
859 attr->encr_settings_len);
860 if (decrypted == NULL) {
861 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
862 "Settings attribute");
863 wps->state = SEND_WSC_NACK;
867 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
869 if (wps_parse_msg(decrypted, &eattr) < 0 ||
870 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
871 wps_process_r_snonce1(wps, eattr.r_snonce1)) {
872 wpabuf_free(decrypted);
873 wps->state = SEND_WSC_NACK;
876 wpabuf_free(decrypted);
878 wps->state = SEND_M5;
883 static enum wps_process_res wps_process_m6(struct wps_data *wps,
884 const struct wpabuf *msg,
885 struct wps_parse_attr *attr)
887 struct wpabuf *decrypted;
888 struct wps_parse_attr eattr;
890 wpa_printf(MSG_DEBUG, "WPS: Received M6");
892 if (wps->state != RECV_M6) {
893 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
894 "receiving M6", wps->state);
895 wps->state = SEND_WSC_NACK;
899 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
900 wps_process_authenticator(wps, attr->authenticator, msg)) {
901 wps->state = SEND_WSC_NACK;
905 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
906 attr->encr_settings_len);
907 if (decrypted == NULL) {
908 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
909 "Settings attribute");
910 wps->state = SEND_WSC_NACK;
914 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
916 if (wps_parse_msg(decrypted, &eattr) < 0 ||
917 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
918 wps_process_r_snonce2(wps, eattr.r_snonce2)) {
919 wpabuf_free(decrypted);
920 wps->state = SEND_WSC_NACK;
923 wpabuf_free(decrypted);
925 wps->state = SEND_M7;
930 static enum wps_process_res wps_process_m8(struct wps_data *wps,
931 const struct wpabuf *msg,
932 struct wps_parse_attr *attr)
934 struct wpabuf *decrypted;
935 struct wps_parse_attr eattr;
937 wpa_printf(MSG_DEBUG, "WPS: Received M8");
939 if (wps->state != RECV_M8) {
940 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
941 "receiving M8", wps->state);
942 wps->state = SEND_WSC_NACK;
946 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
947 wps_process_authenticator(wps, attr->authenticator, msg)) {
948 wps->state = SEND_WSC_NACK;
952 decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
953 attr->encr_settings_len);
954 if (decrypted == NULL) {
955 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
956 "Settings attribute");
957 wps->state = SEND_WSC_NACK;
961 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
963 if (wps_parse_msg(decrypted, &eattr) < 0 ||
964 wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
965 wps_process_creds(wps, eattr.cred, eattr.cred_len,
967 wps_process_ap_settings_e(wps, &eattr, decrypted)) {
968 wpabuf_free(decrypted);
969 wps->state = SEND_WSC_NACK;
972 wpabuf_free(decrypted);
974 wps->state = WPS_MSG_DONE;
979 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
980 const struct wpabuf *msg)
982 struct wps_parse_attr attr;
983 enum wps_process_res ret = WPS_CONTINUE;
985 wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
987 if (wps_parse_msg(msg, &attr) < 0)
990 if (!wps_version_supported(attr.version)) {
991 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
992 attr.version ? *attr.version : 0);
996 if (attr.enrollee_nonce == NULL ||
997 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
998 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1002 if (attr.msg_type == NULL) {
1003 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1007 switch (*attr.msg_type) {
1009 ret = wps_process_m2(wps, msg, &attr);
1012 ret = wps_process_m2d(wps, &attr);
1015 ret = wps_process_m4(wps, msg, &attr);
1016 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1017 wps_fail_event(wps->wps, WPS_M4);
1020 ret = wps_process_m6(wps, msg, &attr);
1021 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1022 wps_fail_event(wps->wps, WPS_M6);
1025 ret = wps_process_m8(wps, msg, &attr);
1026 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1027 wps_fail_event(wps->wps, WPS_M8);
1030 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1036 * Save a copy of the last message for Authenticator derivation if we
1037 * are continuing. However, skip M2D since it is not authenticated and
1038 * neither is the ACK/NACK response frame. This allows the possibly
1039 * following M2 to be processed correctly by using the previously sent
1040 * M1 in Authenticator derivation.
1042 if (ret == WPS_CONTINUE && *attr.msg_type != WPS_M2D) {
1043 /* Save a copy of the last message for Authenticator derivation
1045 wpabuf_free(wps->last_msg);
1046 wps->last_msg = wpabuf_dup(msg);
1053 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1054 const struct wpabuf *msg)
1056 struct wps_parse_attr attr;
1058 wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1060 if (wps_parse_msg(msg, &attr) < 0)
1063 if (!wps_version_supported(attr.version)) {
1064 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1065 attr.version ? *attr.version : 0);
1069 if (attr.msg_type == NULL) {
1070 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1074 if (*attr.msg_type != WPS_WSC_ACK) {
1075 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1080 if (attr.registrar_nonce == NULL ||
1081 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1083 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1087 if (attr.enrollee_nonce == NULL ||
1088 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1089 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1093 if (wps->state == RECV_ACK && wps->wps->ap) {
1094 wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
1095 "completed successfully");
1096 wps_success_event(wps->wps);
1097 wps->state = WPS_FINISHED;
1105 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1106 const struct wpabuf *msg)
1108 struct wps_parse_attr attr;
1110 wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1112 if (wps_parse_msg(msg, &attr) < 0)
1115 if (!wps_version_supported(attr.version)) {
1116 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1117 attr.version ? *attr.version : 0);
1121 if (attr.msg_type == NULL) {
1122 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1126 if (*attr.msg_type != WPS_WSC_NACK) {
1127 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1132 if (attr.registrar_nonce == NULL ||
1133 os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1135 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1136 wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
1137 attr.registrar_nonce, WPS_NONCE_LEN);
1138 wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
1139 wps->nonce_r, WPS_NONCE_LEN);
1143 if (attr.enrollee_nonce == NULL ||
1144 os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1145 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1146 wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
1147 attr.enrollee_nonce, WPS_NONCE_LEN);
1148 wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
1149 wps->nonce_e, WPS_NONCE_LEN);
1153 if (attr.config_error == NULL) {
1154 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1159 wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
1160 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1162 switch (wps->state) {
1164 wps_fail_event(wps->wps, WPS_M3);
1167 wps_fail_event(wps->wps, WPS_M5);
1170 wps_fail_event(wps->wps, WPS_M7);
1176 /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1177 * Enrollee is Authenticator */
1178 wps->state = SEND_WSC_NACK;
1184 enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps,
1185 enum wsc_op_code op_code,
1186 const struct wpabuf *msg)
1189 wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1191 (unsigned long) wpabuf_len(msg), op_code);
1193 if (op_code == WSC_UPnP) {
1194 /* Determine the OpCode based on message type attribute */
1195 struct wps_parse_attr attr;
1196 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type) {
1197 if (*attr.msg_type == WPS_WSC_ACK)
1199 else if (*attr.msg_type == WPS_WSC_NACK)
1207 return wps_process_wsc_msg(wps, msg);
1209 return wps_process_wsc_ack(wps, msg);
1211 return wps_process_wsc_nack(wps, msg);
1213 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);