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.
18 #include "crypto/crypto.h"
19 #include "crypto/sha256.h"
21 #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
)
123 if (os_get_random(wps
->nonce_e
, WPS_NONCE_LEN
) < 0)
125 wpa_hexdump(MSG_DEBUG
, "WPS: Enrollee Nonce",
126 wps
->nonce_e
, WPS_NONCE_LEN
);
128 wpa_printf(MSG_DEBUG
, "WPS: Building Message M1");
129 msg
= wpabuf_alloc(1000);
133 if (wps_build_version(msg
) ||
134 wps_build_msg_type(msg
, WPS_M1
) ||
135 wps_build_uuid_e(msg
, wps
->uuid_e
) ||
136 wps_build_mac_addr(wps
, msg
) ||
137 wps_build_enrollee_nonce(wps
, msg
) ||
138 wps_build_public_key(wps
, msg
) ||
139 wps_build_auth_type_flags(wps
, msg
) ||
140 wps_build_encr_type_flags(wps
, msg
) ||
141 wps_build_conn_type_flags(wps
, msg
) ||
142 wps_build_config_methods(msg
, wps
->wps
->config_methods
) ||
143 wps_build_wps_state(wps
, msg
) ||
144 wps_build_device_attrs(&wps
->wps
->dev
, msg
) ||
145 wps_build_rf_bands(&wps
->wps
->dev
, msg
) ||
146 wps_build_assoc_state(wps
, msg
) ||
147 wps_build_dev_password_id(msg
, wps
->dev_pw_id
) ||
148 wps_build_config_error(msg
, WPS_CFG_NO_ERROR
) ||
149 wps_build_os_version(&wps
->wps
->dev
, msg
)) {
154 wps
->state
= RECV_M2
;
159 static struct wpabuf
* wps_build_m3(struct wps_data
*wps
)
163 wpa_printf(MSG_DEBUG
, "WPS: Building Message M3");
165 if (wps
->dev_password
== NULL
) {
166 wpa_printf(MSG_DEBUG
, "WPS: No Device Password available");
169 wps_derive_psk(wps
, wps
->dev_password
, wps
->dev_password_len
);
171 msg
= wpabuf_alloc(1000);
175 if (wps_build_version(msg
) ||
176 wps_build_msg_type(msg
, WPS_M3
) ||
177 wps_build_registrar_nonce(wps
, msg
) ||
178 wps_build_e_hash(wps
, msg
) ||
179 wps_build_authenticator(wps
, msg
)) {
184 wps
->state
= RECV_M4
;
189 static struct wpabuf
* wps_build_m5(struct wps_data
*wps
)
191 struct wpabuf
*msg
, *plain
;
193 wpa_printf(MSG_DEBUG
, "WPS: Building Message M5");
195 plain
= wpabuf_alloc(200);
199 msg
= wpabuf_alloc(1000);
205 if (wps_build_version(msg
) ||
206 wps_build_msg_type(msg
, WPS_M5
) ||
207 wps_build_registrar_nonce(wps
, msg
) ||
208 wps_build_e_snonce1(wps
, plain
) ||
209 wps_build_key_wrap_auth(wps
, plain
) ||
210 wps_build_encr_settings(wps
, msg
, plain
) ||
211 wps_build_authenticator(wps
, msg
)) {
218 wps
->state
= RECV_M6
;
223 static int wps_build_cred_ssid(struct wps_data
*wps
, struct wpabuf
*msg
)
225 wpa_printf(MSG_DEBUG
, "WPS: * SSID");
226 wpabuf_put_be16(msg
, ATTR_SSID
);
227 wpabuf_put_be16(msg
, wps
->wps
->ssid_len
);
228 wpabuf_put_data(msg
, wps
->wps
->ssid
, wps
->wps
->ssid_len
);
233 static int wps_build_cred_auth_type(struct wps_data
*wps
, struct wpabuf
*msg
)
235 wpa_printf(MSG_DEBUG
, "WPS: * Authentication Type");
236 wpabuf_put_be16(msg
, ATTR_AUTH_TYPE
);
237 wpabuf_put_be16(msg
, 2);
238 wpabuf_put_be16(msg
, wps
->wps
->auth_types
);
243 static int wps_build_cred_encr_type(struct wps_data
*wps
, struct wpabuf
*msg
)
245 wpa_printf(MSG_DEBUG
, "WPS: * Encryption Type");
246 wpabuf_put_be16(msg
, ATTR_ENCR_TYPE
);
247 wpabuf_put_be16(msg
, 2);
248 wpabuf_put_be16(msg
, wps
->wps
->encr_types
);
253 static int wps_build_cred_network_key(struct wps_data
*wps
, struct wpabuf
*msg
)
255 wpa_printf(MSG_DEBUG
, "WPS: * Network Key");
256 wpabuf_put_be16(msg
, ATTR_NETWORK_KEY
);
257 wpabuf_put_be16(msg
, wps
->wps
->network_key_len
);
258 wpabuf_put_data(msg
, wps
->wps
->network_key
, wps
->wps
->network_key_len
);
263 static int wps_build_cred_mac_addr(struct wps_data
*wps
, struct wpabuf
*msg
)
265 wpa_printf(MSG_DEBUG
, "WPS: * MAC Address (AP BSSID)");
266 wpabuf_put_be16(msg
, ATTR_MAC_ADDR
);
267 wpabuf_put_be16(msg
, ETH_ALEN
);
268 wpabuf_put_data(msg
, wps
->wps
->dev
.mac_addr
, ETH_ALEN
);
273 static int wps_build_ap_settings(struct wps_data
*wps
, struct wpabuf
*plain
)
275 if (wps
->wps
->ap_settings
) {
276 wpa_printf(MSG_DEBUG
, "WPS: * AP Settings (pre-configured)");
277 wpabuf_put_data(plain
, wps
->wps
->ap_settings
,
278 wps
->wps
->ap_settings_len
);
282 return wps_build_cred_ssid(wps
, plain
) ||
283 wps_build_cred_mac_addr(wps
, plain
) ||
284 wps_build_cred_auth_type(wps
, plain
) ||
285 wps_build_cred_encr_type(wps
, plain
) ||
286 wps_build_cred_network_key(wps
, plain
);
290 static struct wpabuf
* wps_build_m7(struct wps_data
*wps
)
292 struct wpabuf
*msg
, *plain
;
294 wpa_printf(MSG_DEBUG
, "WPS: Building Message M7");
296 plain
= wpabuf_alloc(500 + wps
->wps
->ap_settings_len
);
300 msg
= wpabuf_alloc(1000 + wps
->wps
->ap_settings_len
);
306 if (wps_build_version(msg
) ||
307 wps_build_msg_type(msg
, WPS_M7
) ||
308 wps_build_registrar_nonce(wps
, msg
) ||
309 wps_build_e_snonce2(wps
, plain
) ||
310 (wps
->wps
->ap
&& wps_build_ap_settings(wps
, plain
)) ||
311 wps_build_key_wrap_auth(wps
, plain
) ||
312 wps_build_encr_settings(wps
, msg
, plain
) ||
313 wps_build_authenticator(wps
, msg
)) {
320 if (wps
->wps
->ap
&& wps
->wps
->registrar
) {
322 * If the Registrar is only learning our current configuration,
323 * it may not continue protocol run to successful completion.
324 * Store information here to make sure it remains available.
326 wps_device_store(wps
->wps
->registrar
, &wps
->peer_dev
,
330 wps
->state
= RECV_M8
;
335 static struct wpabuf
* wps_build_wsc_done(struct wps_data
*wps
)
339 wpa_printf(MSG_DEBUG
, "WPS: Building Message WSC_Done");
341 msg
= wpabuf_alloc(1000);
345 if (wps_build_version(msg
) ||
346 wps_build_msg_type(msg
, WPS_WSC_DONE
) ||
347 wps_build_enrollee_nonce(wps
, msg
) ||
348 wps_build_registrar_nonce(wps
, msg
)) {
354 wps
->state
= RECV_ACK
;
356 wps_success_event(wps
->wps
);
357 wps
->state
= WPS_FINISHED
;
363 static struct wpabuf
* wps_build_wsc_ack(struct wps_data
*wps
)
367 wpa_printf(MSG_DEBUG
, "WPS: Building Message WSC_ACK");
369 msg
= wpabuf_alloc(1000);
373 if (wps_build_version(msg
) ||
374 wps_build_msg_type(msg
, WPS_WSC_ACK
) ||
375 wps_build_enrollee_nonce(wps
, msg
) ||
376 wps_build_registrar_nonce(wps
, msg
)) {
385 static struct wpabuf
* wps_build_wsc_nack(struct wps_data
*wps
)
389 wpa_printf(MSG_DEBUG
, "WPS: Building Message WSC_NACK");
391 msg
= wpabuf_alloc(1000);
395 if (wps_build_version(msg
) ||
396 wps_build_msg_type(msg
, WPS_WSC_NACK
) ||
397 wps_build_enrollee_nonce(wps
, msg
) ||
398 wps_build_registrar_nonce(wps
, msg
) ||
399 wps_build_config_error(msg
, wps
->config_error
)) {
408 struct wpabuf
* wps_enrollee_get_msg(struct wps_data
*wps
,
409 enum wsc_op_code
*op_code
)
413 switch (wps
->state
) {
415 msg
= wps_build_m1(wps
);
419 msg
= wps_build_m3(wps
);
423 msg
= wps_build_m5(wps
);
427 msg
= wps_build_m7(wps
);
432 msg
= wps_build_wsc_nack(wps
);
436 msg
= wps_build_wsc_ack(wps
);
439 /* Another M2/M2D may be received */
440 wps
->state
= RECV_M2
;
444 msg
= wps_build_wsc_nack(wps
);
448 msg
= wps_build_wsc_done(wps
);
452 wpa_printf(MSG_DEBUG
, "WPS: Unsupported state %d for building "
453 "a message", wps
->state
);
458 if (*op_code
== WSC_MSG
&& msg
) {
459 /* Save a copy of the last message for Authenticator derivation
461 wpabuf_free(wps
->last_msg
);
462 wps
->last_msg
= wpabuf_dup(msg
);
469 static int wps_process_registrar_nonce(struct wps_data
*wps
, const u8
*r_nonce
)
471 if (r_nonce
== NULL
) {
472 wpa_printf(MSG_DEBUG
, "WPS: No Registrar Nonce received");
476 os_memcpy(wps
->nonce_r
, r_nonce
, WPS_NONCE_LEN
);
477 wpa_hexdump(MSG_DEBUG
, "WPS: Registrar Nonce",
478 wps
->nonce_r
, WPS_NONCE_LEN
);
484 static int wps_process_enrollee_nonce(struct wps_data
*wps
, const u8
*e_nonce
)
486 if (e_nonce
== NULL
) {
487 wpa_printf(MSG_DEBUG
, "WPS: No Enrollee Nonce received");
491 if (os_memcmp(wps
->nonce_e
, e_nonce
, WPS_NONCE_LEN
) != 0) {
492 wpa_printf(MSG_DEBUG
, "WPS: Invalid Enrollee Nonce received");
500 static int wps_process_uuid_r(struct wps_data
*wps
, const u8
*uuid_r
)
502 if (uuid_r
== NULL
) {
503 wpa_printf(MSG_DEBUG
, "WPS: No UUID-R received");
507 os_memcpy(wps
->uuid_r
, uuid_r
, WPS_UUID_LEN
);
508 wpa_hexdump(MSG_DEBUG
, "WPS: UUID-R", wps
->uuid_r
, WPS_UUID_LEN
);
514 static int wps_process_pubkey(struct wps_data
*wps
, const u8
*pk
,
517 if (pk
== NULL
|| pk_len
== 0) {
518 wpa_printf(MSG_DEBUG
, "WPS: No Public Key received");
522 #ifdef CONFIG_WPS_OOB
523 if (wps
->dev_pw_id
!= DEV_PW_DEFAULT
&&
524 wps
->wps
->oob_conf
.pubkey_hash
) {
526 u8 hash
[WPS_HASH_LEN
];
529 sha256_vector(1, addr
, &pk_len
, hash
);
531 wpabuf_head(wps
->wps
->oob_conf
.pubkey_hash
),
532 WPS_OOB_PUBKEY_HASH_LEN
) != 0) {
533 wpa_printf(MSG_ERROR
, "WPS: Public Key hash error");
537 #endif /* CONFIG_WPS_OOB */
539 wpabuf_free(wps
->dh_pubkey_r
);
540 wps
->dh_pubkey_r
= wpabuf_alloc_copy(pk
, pk_len
);
541 if (wps
->dh_pubkey_r
== NULL
)
544 if (wps_derive_keys(wps
) < 0)
551 static int wps_process_r_hash1(struct wps_data
*wps
, const u8
*r_hash1
)
553 if (r_hash1
== NULL
) {
554 wpa_printf(MSG_DEBUG
, "WPS: No R-Hash1 received");
558 os_memcpy(wps
->peer_hash1
, r_hash1
, WPS_HASH_LEN
);
559 wpa_hexdump(MSG_DEBUG
, "WPS: R-Hash1", wps
->peer_hash1
, WPS_HASH_LEN
);
565 static int wps_process_r_hash2(struct wps_data
*wps
, const u8
*r_hash2
)
567 if (r_hash2
== NULL
) {
568 wpa_printf(MSG_DEBUG
, "WPS: No R-Hash2 received");
572 os_memcpy(wps
->peer_hash2
, r_hash2
, WPS_HASH_LEN
);
573 wpa_hexdump(MSG_DEBUG
, "WPS: R-Hash2", wps
->peer_hash2
, WPS_HASH_LEN
);
579 static int wps_process_r_snonce1(struct wps_data
*wps
, const u8
*r_snonce1
)
581 u8 hash
[SHA256_MAC_LEN
];
585 if (r_snonce1
== NULL
) {
586 wpa_printf(MSG_DEBUG
, "WPS: No R-SNonce1 received");
590 wpa_hexdump_key(MSG_DEBUG
, "WPS: R-SNonce1", r_snonce1
,
591 WPS_SECRET_NONCE_LEN
);
593 /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
595 len
[0] = WPS_SECRET_NONCE_LEN
;
597 len
[1] = WPS_PSK_LEN
;
598 addr
[2] = wpabuf_head(wps
->dh_pubkey_e
);
599 len
[2] = wpabuf_len(wps
->dh_pubkey_e
);
600 addr
[3] = wpabuf_head(wps
->dh_pubkey_r
);
601 len
[3] = wpabuf_len(wps
->dh_pubkey_r
);
602 hmac_sha256_vector(wps
->authkey
, WPS_AUTHKEY_LEN
, 4, addr
, len
, hash
);
604 if (os_memcmp(wps
->peer_hash1
, hash
, WPS_HASH_LEN
) != 0) {
605 wpa_printf(MSG_DEBUG
, "WPS: R-Hash1 derived from R-S1 does "
606 "not match with the pre-committed value");
607 wps
->config_error
= WPS_CFG_DEV_PASSWORD_AUTH_FAILURE
;
608 wps_pwd_auth_fail_event(wps
->wps
, 1, 1);
612 wpa_printf(MSG_DEBUG
, "WPS: Registrar proved knowledge of the first "
613 "half of the device password");
619 static int wps_process_r_snonce2(struct wps_data
*wps
, const u8
*r_snonce2
)
621 u8 hash
[SHA256_MAC_LEN
];
625 if (r_snonce2
== NULL
) {
626 wpa_printf(MSG_DEBUG
, "WPS: No R-SNonce2 received");
630 wpa_hexdump_key(MSG_DEBUG
, "WPS: R-SNonce2", r_snonce2
,
631 WPS_SECRET_NONCE_LEN
);
633 /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
635 len
[0] = WPS_SECRET_NONCE_LEN
;
637 len
[1] = WPS_PSK_LEN
;
638 addr
[2] = wpabuf_head(wps
->dh_pubkey_e
);
639 len
[2] = wpabuf_len(wps
->dh_pubkey_e
);
640 addr
[3] = wpabuf_head(wps
->dh_pubkey_r
);
641 len
[3] = wpabuf_len(wps
->dh_pubkey_r
);
642 hmac_sha256_vector(wps
->authkey
, WPS_AUTHKEY_LEN
, 4, addr
, len
, hash
);
644 if (os_memcmp(wps
->peer_hash2
, hash
, WPS_HASH_LEN
) != 0) {
645 wpa_printf(MSG_DEBUG
, "WPS: R-Hash2 derived from R-S2 does "
646 "not match with the pre-committed value");
647 wps
->config_error
= WPS_CFG_DEV_PASSWORD_AUTH_FAILURE
;
648 wps_pwd_auth_fail_event(wps
->wps
, 1, 2);
652 wpa_printf(MSG_DEBUG
, "WPS: Registrar proved knowledge of the second "
653 "half of the device password");
659 static int wps_process_cred_e(struct wps_data
*wps
, const u8
*cred
,
662 struct wps_parse_attr attr
;
665 wpa_printf(MSG_DEBUG
, "WPS: Received Credential");
666 os_memset(&wps
->cred
, 0, sizeof(wps
->cred
));
667 wpabuf_set(&msg
, cred
, cred_len
);
668 if (wps_parse_msg(&msg
, &attr
) < 0 ||
669 wps_process_cred(&attr
, &wps
->cred
))
672 if (os_memcmp(wps
->cred
.mac_addr
, wps
->wps
->dev
.mac_addr
, ETH_ALEN
) !=
674 wpa_printf(MSG_DEBUG
, "WPS: MAC Address in the Credential ("
675 MACSTR
") does not match with own address (" MACSTR
676 ")", MAC2STR(wps
->cred
.mac_addr
),
677 MAC2STR(wps
->wps
->dev
.mac_addr
));
679 * In theory, this could be consider fatal error, but there are
680 * number of deployed implementations using other address here
681 * due to unclarity in the specification. For interoperability
682 * reasons, allow this to be processed since we do not really
683 * use the MAC Address information for anything.
687 if (wps
->wps
->cred_cb
) {
688 wps
->cred
.cred_attr
= cred
- 4;
689 wps
->cred
.cred_attr_len
= cred_len
+ 4;
690 wps
->wps
->cred_cb(wps
->wps
->cb_ctx
, &wps
->cred
);
691 wps
->cred
.cred_attr
= NULL
;
692 wps
->cred
.cred_attr_len
= 0;
699 static int wps_process_creds(struct wps_data
*wps
, const u8
*cred
[],
700 size_t cred_len
[], size_t num_cred
)
708 wpa_printf(MSG_DEBUG
, "WPS: No Credential attributes "
713 for (i
= 0; i
< num_cred
; i
++) {
714 if (wps_process_cred_e(wps
, cred
[i
], cred_len
[i
]))
722 static int wps_process_ap_settings_e(struct wps_data
*wps
,
723 struct wps_parse_attr
*attr
,
724 struct wpabuf
*attrs
)
726 struct wps_credential cred
;
731 if (wps_process_ap_settings(attr
, &cred
) < 0)
734 wpa_printf(MSG_INFO
, "WPS: Received new AP configuration from "
737 if (os_memcmp(cred
.mac_addr
, wps
->wps
->dev
.mac_addr
, ETH_ALEN
) !=
739 wpa_printf(MSG_DEBUG
, "WPS: MAC Address in the AP Settings ("
740 MACSTR
") does not match with own address (" MACSTR
741 ")", MAC2STR(cred
.mac_addr
),
742 MAC2STR(wps
->wps
->dev
.mac_addr
));
744 * In theory, this could be consider fatal error, but there are
745 * number of deployed implementations using other address here
746 * due to unclarity in the specification. For interoperability
747 * reasons, allow this to be processed since we do not really
748 * use the MAC Address information for anything.
752 if (wps
->wps
->cred_cb
) {
753 cred
.cred_attr
= wpabuf_head(attrs
);
754 cred
.cred_attr_len
= wpabuf_len(attrs
);
755 wps
->wps
->cred_cb(wps
->wps
->cb_ctx
, &cred
);
762 static enum wps_process_res
wps_process_m2(struct wps_data
*wps
,
763 const struct wpabuf
*msg
,
764 struct wps_parse_attr
*attr
)
766 wpa_printf(MSG_DEBUG
, "WPS: Received M2");
768 if (wps
->state
!= RECV_M2
) {
769 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
770 "receiving M2", wps
->state
);
771 wps
->state
= SEND_WSC_NACK
;
775 if (wps_process_registrar_nonce(wps
, attr
->registrar_nonce
) ||
776 wps_process_enrollee_nonce(wps
, attr
->enrollee_nonce
) ||
777 wps_process_uuid_r(wps
, attr
->uuid_r
)) {
778 wps
->state
= SEND_WSC_NACK
;
783 (wps
->wps
->ap_setup_locked
|| wps
->dev_password
== NULL
)) {
784 wpa_printf(MSG_DEBUG
, "WPS: AP Setup is locked - refuse "
785 "registration of a new Registrar");
786 wps
->config_error
= WPS_CFG_SETUP_LOCKED
;
787 wps
->state
= SEND_WSC_NACK
;
791 if (wps_process_pubkey(wps
, attr
->public_key
, attr
->public_key_len
) ||
792 wps_process_authenticator(wps
, attr
->authenticator
, msg
) ||
793 wps_process_device_attrs(&wps
->peer_dev
, attr
)) {
794 wps
->state
= SEND_WSC_NACK
;
798 wps
->state
= SEND_M3
;
803 static enum wps_process_res
wps_process_m2d(struct wps_data
*wps
,
804 struct wps_parse_attr
*attr
)
806 wpa_printf(MSG_DEBUG
, "WPS: Received M2D");
808 if (wps
->state
!= RECV_M2
) {
809 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
810 "receiving M2D", wps
->state
);
811 wps
->state
= SEND_WSC_NACK
;
815 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Manufacturer",
816 attr
->manufacturer
, attr
->manufacturer_len
);
817 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Model Name",
818 attr
->model_name
, attr
->model_name_len
);
819 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Model Number",
820 attr
->model_number
, attr
->model_number_len
);
821 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Serial Number",
822 attr
->serial_number
, attr
->serial_number_len
);
823 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Device Name",
824 attr
->dev_name
, attr
->dev_name_len
);
826 if (wps
->wps
->event_cb
) {
827 union wps_event_data data
;
828 struct wps_event_m2d
*m2d
= &data
.m2d
;
829 os_memset(&data
, 0, sizeof(data
));
830 if (attr
->config_methods
)
831 m2d
->config_methods
=
832 WPA_GET_BE16(attr
->config_methods
);
833 m2d
->manufacturer
= attr
->manufacturer
;
834 m2d
->manufacturer_len
= attr
->manufacturer_len
;
835 m2d
->model_name
= attr
->model_name
;
836 m2d
->model_name_len
= attr
->model_name_len
;
837 m2d
->model_number
= attr
->model_number
;
838 m2d
->model_number_len
= attr
->model_number_len
;
839 m2d
->serial_number
= attr
->serial_number
;
840 m2d
->serial_number_len
= attr
->serial_number_len
;
841 m2d
->dev_name
= attr
->dev_name
;
842 m2d
->dev_name_len
= attr
->dev_name_len
;
843 m2d
->primary_dev_type
= attr
->primary_dev_type
;
844 if (attr
->config_error
)
846 WPA_GET_BE16(attr
->config_error
);
847 if (attr
->dev_password_id
)
848 m2d
->dev_password_id
=
849 WPA_GET_BE16(attr
->dev_password_id
);
850 wps
->wps
->event_cb(wps
->wps
->cb_ctx
, WPS_EV_M2D
, &data
);
853 wps
->state
= RECEIVED_M2D
;
858 static enum wps_process_res
wps_process_m4(struct wps_data
*wps
,
859 const struct wpabuf
*msg
,
860 struct wps_parse_attr
*attr
)
862 struct wpabuf
*decrypted
;
863 struct wps_parse_attr eattr
;
865 wpa_printf(MSG_DEBUG
, "WPS: Received M4");
867 if (wps
->state
!= RECV_M4
) {
868 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
869 "receiving M4", wps
->state
);
870 wps
->state
= SEND_WSC_NACK
;
874 if (wps_process_enrollee_nonce(wps
, attr
->enrollee_nonce
) ||
875 wps_process_authenticator(wps
, attr
->authenticator
, msg
) ||
876 wps_process_r_hash1(wps
, attr
->r_hash1
) ||
877 wps_process_r_hash2(wps
, attr
->r_hash2
)) {
878 wps
->state
= SEND_WSC_NACK
;
882 decrypted
= wps_decrypt_encr_settings(wps
, attr
->encr_settings
,
883 attr
->encr_settings_len
);
884 if (decrypted
== NULL
) {
885 wpa_printf(MSG_DEBUG
, "WPS: Failed to decrypted Encrypted "
886 "Settings attribute");
887 wps
->state
= SEND_WSC_NACK
;
891 wpa_printf(MSG_DEBUG
, "WPS: Processing decrypted Encrypted Settings "
893 if (wps_parse_msg(decrypted
, &eattr
) < 0 ||
894 wps_process_key_wrap_auth(wps
, decrypted
, eattr
.key_wrap_auth
) ||
895 wps_process_r_snonce1(wps
, eattr
.r_snonce1
)) {
896 wpabuf_free(decrypted
);
897 wps
->state
= SEND_WSC_NACK
;
900 wpabuf_free(decrypted
);
902 wps
->state
= SEND_M5
;
907 static enum wps_process_res
wps_process_m6(struct wps_data
*wps
,
908 const struct wpabuf
*msg
,
909 struct wps_parse_attr
*attr
)
911 struct wpabuf
*decrypted
;
912 struct wps_parse_attr eattr
;
914 wpa_printf(MSG_DEBUG
, "WPS: Received M6");
916 if (wps
->state
!= RECV_M6
) {
917 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
918 "receiving M6", wps
->state
);
919 wps
->state
= SEND_WSC_NACK
;
923 if (wps_process_enrollee_nonce(wps
, attr
->enrollee_nonce
) ||
924 wps_process_authenticator(wps
, attr
->authenticator
, msg
)) {
925 wps
->state
= SEND_WSC_NACK
;
929 decrypted
= wps_decrypt_encr_settings(wps
, attr
->encr_settings
,
930 attr
->encr_settings_len
);
931 if (decrypted
== NULL
) {
932 wpa_printf(MSG_DEBUG
, "WPS: Failed to decrypted Encrypted "
933 "Settings attribute");
934 wps
->state
= SEND_WSC_NACK
;
938 wpa_printf(MSG_DEBUG
, "WPS: Processing decrypted Encrypted Settings "
940 if (wps_parse_msg(decrypted
, &eattr
) < 0 ||
941 wps_process_key_wrap_auth(wps
, decrypted
, eattr
.key_wrap_auth
) ||
942 wps_process_r_snonce2(wps
, eattr
.r_snonce2
)) {
943 wpabuf_free(decrypted
);
944 wps
->state
= SEND_WSC_NACK
;
947 wpabuf_free(decrypted
);
949 wps
->state
= SEND_M7
;
954 static enum wps_process_res
wps_process_m8(struct wps_data
*wps
,
955 const struct wpabuf
*msg
,
956 struct wps_parse_attr
*attr
)
958 struct wpabuf
*decrypted
;
959 struct wps_parse_attr eattr
;
961 wpa_printf(MSG_DEBUG
, "WPS: Received M8");
963 if (wps
->state
!= RECV_M8
) {
964 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
965 "receiving M8", wps
->state
);
966 wps
->state
= SEND_WSC_NACK
;
970 if (wps_process_enrollee_nonce(wps
, attr
->enrollee_nonce
) ||
971 wps_process_authenticator(wps
, attr
->authenticator
, msg
)) {
972 wps
->state
= SEND_WSC_NACK
;
976 decrypted
= wps_decrypt_encr_settings(wps
, attr
->encr_settings
,
977 attr
->encr_settings_len
);
978 if (decrypted
== NULL
) {
979 wpa_printf(MSG_DEBUG
, "WPS: Failed to decrypted Encrypted "
980 "Settings attribute");
981 wps
->state
= SEND_WSC_NACK
;
985 wpa_printf(MSG_DEBUG
, "WPS: Processing decrypted Encrypted Settings "
987 if (wps_parse_msg(decrypted
, &eattr
) < 0 ||
988 wps_process_key_wrap_auth(wps
, decrypted
, eattr
.key_wrap_auth
) ||
989 wps_process_creds(wps
, eattr
.cred
, eattr
.cred_len
,
991 wps_process_ap_settings_e(wps
, &eattr
, decrypted
)) {
992 wpabuf_free(decrypted
);
993 wps
->state
= SEND_WSC_NACK
;
996 wpabuf_free(decrypted
);
998 wps
->state
= WPS_MSG_DONE
;
1003 static enum wps_process_res
wps_process_wsc_msg(struct wps_data
*wps
,
1004 const struct wpabuf
*msg
)
1006 struct wps_parse_attr attr
;
1007 enum wps_process_res ret
= WPS_CONTINUE
;
1009 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_MSG");
1011 if (wps_parse_msg(msg
, &attr
) < 0)
1014 if (!wps_version_supported(attr
.version
)) {
1015 wpa_printf(MSG_DEBUG
, "WPS: Unsupported message version 0x%x",
1016 attr
.version
? *attr
.version
: 0);
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 (attr
.msg_type
== NULL
) {
1027 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
1031 switch (*attr
.msg_type
) {
1033 ret
= wps_process_m2(wps
, msg
, &attr
);
1036 ret
= wps_process_m2d(wps
, &attr
);
1039 ret
= wps_process_m4(wps
, msg
, &attr
);
1040 if (ret
== WPS_FAILURE
|| wps
->state
== SEND_WSC_NACK
)
1041 wps_fail_event(wps
->wps
, WPS_M4
);
1044 ret
= wps_process_m6(wps
, msg
, &attr
);
1045 if (ret
== WPS_FAILURE
|| wps
->state
== SEND_WSC_NACK
)
1046 wps_fail_event(wps
->wps
, WPS_M6
);
1049 ret
= wps_process_m8(wps
, msg
, &attr
);
1050 if (ret
== WPS_FAILURE
|| wps
->state
== SEND_WSC_NACK
)
1051 wps_fail_event(wps
->wps
, WPS_M8
);
1054 wpa_printf(MSG_DEBUG
, "WPS: Unsupported Message Type %d",
1060 * Save a copy of the last message for Authenticator derivation if we
1061 * are continuing. However, skip M2D since it is not authenticated and
1062 * neither is the ACK/NACK response frame. This allows the possibly
1063 * following M2 to be processed correctly by using the previously sent
1064 * M1 in Authenticator derivation.
1066 if (ret
== WPS_CONTINUE
&& *attr
.msg_type
!= WPS_M2D
) {
1067 /* Save a copy of the last message for Authenticator derivation
1069 wpabuf_free(wps
->last_msg
);
1070 wps
->last_msg
= wpabuf_dup(msg
);
1077 static enum wps_process_res
wps_process_wsc_ack(struct wps_data
*wps
,
1078 const struct wpabuf
*msg
)
1080 struct wps_parse_attr attr
;
1082 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_ACK");
1084 if (wps_parse_msg(msg
, &attr
) < 0)
1087 if (!wps_version_supported(attr
.version
)) {
1088 wpa_printf(MSG_DEBUG
, "WPS: Unsupported message version 0x%x",
1089 attr
.version
? *attr
.version
: 0);
1093 if (attr
.msg_type
== NULL
) {
1094 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
1098 if (*attr
.msg_type
!= WPS_WSC_ACK
) {
1099 wpa_printf(MSG_DEBUG
, "WPS: Invalid Message Type %d",
1104 if (attr
.registrar_nonce
== NULL
||
1105 os_memcmp(wps
->nonce_r
, attr
.registrar_nonce
, WPS_NONCE_LEN
!= 0))
1107 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in registrar nonce");
1111 if (attr
.enrollee_nonce
== NULL
||
1112 os_memcmp(wps
->nonce_e
, attr
.enrollee_nonce
, WPS_NONCE_LEN
!= 0)) {
1113 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in enrollee nonce");
1117 if (wps
->state
== RECV_ACK
&& wps
->wps
->ap
) {
1118 wpa_printf(MSG_DEBUG
, "WPS: External Registrar registration "
1119 "completed successfully");
1120 wps_success_event(wps
->wps
);
1121 wps
->state
= WPS_FINISHED
;
1129 static enum wps_process_res
wps_process_wsc_nack(struct wps_data
*wps
,
1130 const struct wpabuf
*msg
)
1132 struct wps_parse_attr attr
;
1134 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_NACK");
1136 if (wps_parse_msg(msg
, &attr
) < 0)
1139 if (!wps_version_supported(attr
.version
)) {
1140 wpa_printf(MSG_DEBUG
, "WPS: Unsupported message version 0x%x",
1141 attr
.version
? *attr
.version
: 0);
1145 if (attr
.msg_type
== NULL
) {
1146 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
1150 if (*attr
.msg_type
!= WPS_WSC_NACK
) {
1151 wpa_printf(MSG_DEBUG
, "WPS: Invalid Message Type %d",
1156 if (attr
.registrar_nonce
== NULL
||
1157 os_memcmp(wps
->nonce_r
, attr
.registrar_nonce
, WPS_NONCE_LEN
!= 0))
1159 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in registrar nonce");
1160 wpa_hexdump(MSG_DEBUG
, "WPS: Received Registrar Nonce",
1161 attr
.registrar_nonce
, WPS_NONCE_LEN
);
1162 wpa_hexdump(MSG_DEBUG
, "WPS: Expected Registrar Nonce",
1163 wps
->nonce_r
, WPS_NONCE_LEN
);
1167 if (attr
.enrollee_nonce
== NULL
||
1168 os_memcmp(wps
->nonce_e
, attr
.enrollee_nonce
, WPS_NONCE_LEN
!= 0)) {
1169 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in enrollee nonce");
1170 wpa_hexdump(MSG_DEBUG
, "WPS: Received Enrollee Nonce",
1171 attr
.enrollee_nonce
, WPS_NONCE_LEN
);
1172 wpa_hexdump(MSG_DEBUG
, "WPS: Expected Enrollee Nonce",
1173 wps
->nonce_e
, WPS_NONCE_LEN
);
1177 if (attr
.config_error
== NULL
) {
1178 wpa_printf(MSG_DEBUG
, "WPS: No Configuration Error attribute "
1183 wpa_printf(MSG_DEBUG
, "WPS: Registrar terminated negotiation with "
1184 "Configuration Error %d", WPA_GET_BE16(attr
.config_error
));
1186 switch (wps
->state
) {
1188 wps_fail_event(wps
->wps
, WPS_M3
);
1191 wps_fail_event(wps
->wps
, WPS_M5
);
1194 wps_fail_event(wps
->wps
, WPS_M7
);
1200 /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1201 * Enrollee is Authenticator */
1202 wps
->state
= SEND_WSC_NACK
;
1208 enum wps_process_res
wps_enrollee_process_msg(struct wps_data
*wps
,
1209 enum wsc_op_code op_code
,
1210 const struct wpabuf
*msg
)
1213 wpa_printf(MSG_DEBUG
, "WPS: Processing received message (len=%lu "
1215 (unsigned long) wpabuf_len(msg
), op_code
);
1217 if (op_code
== WSC_UPnP
) {
1218 /* Determine the OpCode based on message type attribute */
1219 struct wps_parse_attr attr
;
1220 if (wps_parse_msg(msg
, &attr
) == 0 && attr
.msg_type
) {
1221 if (*attr
.msg_type
== WPS_WSC_ACK
)
1223 else if (*attr
.msg_type
== WPS_WSC_NACK
)
1231 return wps_process_wsc_msg(wps
, msg
);
1233 return wps_process_wsc_ack(wps
, msg
);
1235 return wps_process_wsc_nack(wps
, msg
);
1237 wpa_printf(MSG_DEBUG
, "WPS: Unsupported op_code %d", op_code
);