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_process_pubkey(wps
, attr
->public_key
, attr
->public_key_len
) ||
779 wps_process_authenticator(wps
, attr
->authenticator
, msg
) ||
780 wps_process_device_attrs(&wps
->peer_dev
, attr
)) {
781 wps
->state
= SEND_WSC_NACK
;
785 if (wps
->wps
->ap
&& wps
->wps
->ap_setup_locked
) {
786 wpa_printf(MSG_DEBUG
, "WPS: AP Setup is locked - refuse "
787 "registration of a new Registrar");
788 wps
->config_error
= WPS_CFG_SETUP_LOCKED
;
789 wps
->state
= SEND_WSC_NACK
;
793 wps
->state
= SEND_M3
;
798 static enum wps_process_res
wps_process_m2d(struct wps_data
*wps
,
799 struct wps_parse_attr
*attr
)
801 wpa_printf(MSG_DEBUG
, "WPS: Received M2D");
803 if (wps
->state
!= RECV_M2
) {
804 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
805 "receiving M2D", wps
->state
);
806 wps
->state
= SEND_WSC_NACK
;
810 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Manufacturer",
811 attr
->manufacturer
, attr
->manufacturer_len
);
812 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Model Name",
813 attr
->model_name
, attr
->model_name_len
);
814 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Model Number",
815 attr
->model_number
, attr
->model_number_len
);
816 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Serial Number",
817 attr
->serial_number
, attr
->serial_number_len
);
818 wpa_hexdump_ascii(MSG_DEBUG
, "WPS: Device Name",
819 attr
->dev_name
, attr
->dev_name_len
);
821 if (wps
->wps
->event_cb
) {
822 union wps_event_data data
;
823 struct wps_event_m2d
*m2d
= &data
.m2d
;
824 os_memset(&data
, 0, sizeof(data
));
825 if (attr
->config_methods
)
826 m2d
->config_methods
=
827 WPA_GET_BE16(attr
->config_methods
);
828 m2d
->manufacturer
= attr
->manufacturer
;
829 m2d
->manufacturer_len
= attr
->manufacturer_len
;
830 m2d
->model_name
= attr
->model_name
;
831 m2d
->model_name_len
= attr
->model_name_len
;
832 m2d
->model_number
= attr
->model_number
;
833 m2d
->model_number_len
= attr
->model_number_len
;
834 m2d
->serial_number
= attr
->serial_number
;
835 m2d
->serial_number_len
= attr
->serial_number_len
;
836 m2d
->dev_name
= attr
->dev_name
;
837 m2d
->dev_name_len
= attr
->dev_name_len
;
838 m2d
->primary_dev_type
= attr
->primary_dev_type
;
839 if (attr
->config_error
)
841 WPA_GET_BE16(attr
->config_error
);
842 if (attr
->dev_password_id
)
843 m2d
->dev_password_id
=
844 WPA_GET_BE16(attr
->dev_password_id
);
845 wps
->wps
->event_cb(wps
->wps
->cb_ctx
, WPS_EV_M2D
, &data
);
848 wps
->state
= RECEIVED_M2D
;
853 static enum wps_process_res
wps_process_m4(struct wps_data
*wps
,
854 const struct wpabuf
*msg
,
855 struct wps_parse_attr
*attr
)
857 struct wpabuf
*decrypted
;
858 struct wps_parse_attr eattr
;
860 wpa_printf(MSG_DEBUG
, "WPS: Received M4");
862 if (wps
->state
!= RECV_M4
) {
863 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
864 "receiving M4", wps
->state
);
865 wps
->state
= SEND_WSC_NACK
;
869 if (wps_process_enrollee_nonce(wps
, attr
->enrollee_nonce
) ||
870 wps_process_authenticator(wps
, attr
->authenticator
, msg
) ||
871 wps_process_r_hash1(wps
, attr
->r_hash1
) ||
872 wps_process_r_hash2(wps
, attr
->r_hash2
)) {
873 wps
->state
= SEND_WSC_NACK
;
877 decrypted
= wps_decrypt_encr_settings(wps
, attr
->encr_settings
,
878 attr
->encr_settings_len
);
879 if (decrypted
== NULL
) {
880 wpa_printf(MSG_DEBUG
, "WPS: Failed to decrypted Encrypted "
881 "Settings attribute");
882 wps
->state
= SEND_WSC_NACK
;
886 wpa_printf(MSG_DEBUG
, "WPS: Processing decrypted Encrypted Settings "
888 if (wps_parse_msg(decrypted
, &eattr
) < 0 ||
889 wps_process_key_wrap_auth(wps
, decrypted
, eattr
.key_wrap_auth
) ||
890 wps_process_r_snonce1(wps
, eattr
.r_snonce1
)) {
891 wpabuf_free(decrypted
);
892 wps
->state
= SEND_WSC_NACK
;
895 wpabuf_free(decrypted
);
897 wps
->state
= SEND_M5
;
902 static enum wps_process_res
wps_process_m6(struct wps_data
*wps
,
903 const struct wpabuf
*msg
,
904 struct wps_parse_attr
*attr
)
906 struct wpabuf
*decrypted
;
907 struct wps_parse_attr eattr
;
909 wpa_printf(MSG_DEBUG
, "WPS: Received M6");
911 if (wps
->state
!= RECV_M6
) {
912 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
913 "receiving M6", wps
->state
);
914 wps
->state
= SEND_WSC_NACK
;
918 if (wps_process_enrollee_nonce(wps
, attr
->enrollee_nonce
) ||
919 wps_process_authenticator(wps
, attr
->authenticator
, msg
)) {
920 wps
->state
= SEND_WSC_NACK
;
924 decrypted
= wps_decrypt_encr_settings(wps
, attr
->encr_settings
,
925 attr
->encr_settings_len
);
926 if (decrypted
== NULL
) {
927 wpa_printf(MSG_DEBUG
, "WPS: Failed to decrypted Encrypted "
928 "Settings attribute");
929 wps
->state
= SEND_WSC_NACK
;
933 wpa_printf(MSG_DEBUG
, "WPS: Processing decrypted Encrypted Settings "
935 if (wps_parse_msg(decrypted
, &eattr
) < 0 ||
936 wps_process_key_wrap_auth(wps
, decrypted
, eattr
.key_wrap_auth
) ||
937 wps_process_r_snonce2(wps
, eattr
.r_snonce2
)) {
938 wpabuf_free(decrypted
);
939 wps
->state
= SEND_WSC_NACK
;
942 wpabuf_free(decrypted
);
944 wps
->state
= SEND_M7
;
949 static enum wps_process_res
wps_process_m8(struct wps_data
*wps
,
950 const struct wpabuf
*msg
,
951 struct wps_parse_attr
*attr
)
953 struct wpabuf
*decrypted
;
954 struct wps_parse_attr eattr
;
956 wpa_printf(MSG_DEBUG
, "WPS: Received M8");
958 if (wps
->state
!= RECV_M8
) {
959 wpa_printf(MSG_DEBUG
, "WPS: Unexpected state (%d) for "
960 "receiving M8", wps
->state
);
961 wps
->state
= SEND_WSC_NACK
;
965 if (wps_process_enrollee_nonce(wps
, attr
->enrollee_nonce
) ||
966 wps_process_authenticator(wps
, attr
->authenticator
, msg
)) {
967 wps
->state
= SEND_WSC_NACK
;
971 decrypted
= wps_decrypt_encr_settings(wps
, attr
->encr_settings
,
972 attr
->encr_settings_len
);
973 if (decrypted
== NULL
) {
974 wpa_printf(MSG_DEBUG
, "WPS: Failed to decrypted Encrypted "
975 "Settings attribute");
976 wps
->state
= SEND_WSC_NACK
;
980 wpa_printf(MSG_DEBUG
, "WPS: Processing decrypted Encrypted Settings "
982 if (wps_parse_msg(decrypted
, &eattr
) < 0 ||
983 wps_process_key_wrap_auth(wps
, decrypted
, eattr
.key_wrap_auth
) ||
984 wps_process_creds(wps
, eattr
.cred
, eattr
.cred_len
,
986 wps_process_ap_settings_e(wps
, &eattr
, decrypted
)) {
987 wpabuf_free(decrypted
);
988 wps
->state
= SEND_WSC_NACK
;
991 wpabuf_free(decrypted
);
993 wps
->state
= WPS_MSG_DONE
;
998 static enum wps_process_res
wps_process_wsc_msg(struct wps_data
*wps
,
999 const struct wpabuf
*msg
)
1001 struct wps_parse_attr attr
;
1002 enum wps_process_res ret
= WPS_CONTINUE
;
1004 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_MSG");
1006 if (wps_parse_msg(msg
, &attr
) < 0)
1009 if (!wps_version_supported(attr
.version
)) {
1010 wpa_printf(MSG_DEBUG
, "WPS: Unsupported message version 0x%x",
1011 attr
.version
? *attr
.version
: 0);
1015 if (attr
.enrollee_nonce
== NULL
||
1016 os_memcmp(wps
->nonce_e
, attr
.enrollee_nonce
, WPS_NONCE_LEN
!= 0)) {
1017 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in enrollee nonce");
1021 if (attr
.msg_type
== NULL
) {
1022 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
1026 switch (*attr
.msg_type
) {
1028 ret
= wps_process_m2(wps
, msg
, &attr
);
1031 ret
= wps_process_m2d(wps
, &attr
);
1034 ret
= wps_process_m4(wps
, msg
, &attr
);
1035 if (ret
== WPS_FAILURE
|| wps
->state
== SEND_WSC_NACK
)
1036 wps_fail_event(wps
->wps
, WPS_M4
);
1039 ret
= wps_process_m6(wps
, msg
, &attr
);
1040 if (ret
== WPS_FAILURE
|| wps
->state
== SEND_WSC_NACK
)
1041 wps_fail_event(wps
->wps
, WPS_M6
);
1044 ret
= wps_process_m8(wps
, msg
, &attr
);
1045 if (ret
== WPS_FAILURE
|| wps
->state
== SEND_WSC_NACK
)
1046 wps_fail_event(wps
->wps
, WPS_M8
);
1049 wpa_printf(MSG_DEBUG
, "WPS: Unsupported Message Type %d",
1055 * Save a copy of the last message for Authenticator derivation if we
1056 * are continuing. However, skip M2D since it is not authenticated and
1057 * neither is the ACK/NACK response frame. This allows the possibly
1058 * following M2 to be processed correctly by using the previously sent
1059 * M1 in Authenticator derivation.
1061 if (ret
== WPS_CONTINUE
&& *attr
.msg_type
!= WPS_M2D
) {
1062 /* Save a copy of the last message for Authenticator derivation
1064 wpabuf_free(wps
->last_msg
);
1065 wps
->last_msg
= wpabuf_dup(msg
);
1072 static enum wps_process_res
wps_process_wsc_ack(struct wps_data
*wps
,
1073 const struct wpabuf
*msg
)
1075 struct wps_parse_attr attr
;
1077 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_ACK");
1079 if (wps_parse_msg(msg
, &attr
) < 0)
1082 if (!wps_version_supported(attr
.version
)) {
1083 wpa_printf(MSG_DEBUG
, "WPS: Unsupported message version 0x%x",
1084 attr
.version
? *attr
.version
: 0);
1088 if (attr
.msg_type
== NULL
) {
1089 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
1093 if (*attr
.msg_type
!= WPS_WSC_ACK
) {
1094 wpa_printf(MSG_DEBUG
, "WPS: Invalid Message Type %d",
1099 if (attr
.registrar_nonce
== NULL
||
1100 os_memcmp(wps
->nonce_r
, attr
.registrar_nonce
, WPS_NONCE_LEN
!= 0))
1102 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in registrar nonce");
1106 if (attr
.enrollee_nonce
== NULL
||
1107 os_memcmp(wps
->nonce_e
, attr
.enrollee_nonce
, WPS_NONCE_LEN
!= 0)) {
1108 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in enrollee nonce");
1112 if (wps
->state
== RECV_ACK
&& wps
->wps
->ap
) {
1113 wpa_printf(MSG_DEBUG
, "WPS: External Registrar registration "
1114 "completed successfully");
1115 wps_success_event(wps
->wps
);
1116 wps
->state
= WPS_FINISHED
;
1124 static enum wps_process_res
wps_process_wsc_nack(struct wps_data
*wps
,
1125 const struct wpabuf
*msg
)
1127 struct wps_parse_attr attr
;
1129 wpa_printf(MSG_DEBUG
, "WPS: Received WSC_NACK");
1131 if (wps_parse_msg(msg
, &attr
) < 0)
1134 if (!wps_version_supported(attr
.version
)) {
1135 wpa_printf(MSG_DEBUG
, "WPS: Unsupported message version 0x%x",
1136 attr
.version
? *attr
.version
: 0);
1140 if (attr
.msg_type
== NULL
) {
1141 wpa_printf(MSG_DEBUG
, "WPS: No Message Type attribute");
1145 if (*attr
.msg_type
!= WPS_WSC_NACK
) {
1146 wpa_printf(MSG_DEBUG
, "WPS: Invalid Message Type %d",
1151 if (attr
.registrar_nonce
== NULL
||
1152 os_memcmp(wps
->nonce_r
, attr
.registrar_nonce
, WPS_NONCE_LEN
!= 0))
1154 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in registrar nonce");
1155 wpa_hexdump(MSG_DEBUG
, "WPS: Received Registrar Nonce",
1156 attr
.registrar_nonce
, WPS_NONCE_LEN
);
1157 wpa_hexdump(MSG_DEBUG
, "WPS: Expected Registrar Nonce",
1158 wps
->nonce_r
, WPS_NONCE_LEN
);
1162 if (attr
.enrollee_nonce
== NULL
||
1163 os_memcmp(wps
->nonce_e
, attr
.enrollee_nonce
, WPS_NONCE_LEN
!= 0)) {
1164 wpa_printf(MSG_DEBUG
, "WPS: Mismatch in enrollee nonce");
1165 wpa_hexdump(MSG_DEBUG
, "WPS: Received Enrollee Nonce",
1166 attr
.enrollee_nonce
, WPS_NONCE_LEN
);
1167 wpa_hexdump(MSG_DEBUG
, "WPS: Expected Enrollee Nonce",
1168 wps
->nonce_e
, WPS_NONCE_LEN
);
1172 if (attr
.config_error
== NULL
) {
1173 wpa_printf(MSG_DEBUG
, "WPS: No Configuration Error attribute "
1178 wpa_printf(MSG_DEBUG
, "WPS: Registrar terminated negotiation with "
1179 "Configuration Error %d", WPA_GET_BE16(attr
.config_error
));
1181 switch (wps
->state
) {
1183 wps_fail_event(wps
->wps
, WPS_M3
);
1186 wps_fail_event(wps
->wps
, WPS_M5
);
1189 wps_fail_event(wps
->wps
, WPS_M7
);
1195 /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1196 * Enrollee is Authenticator */
1197 wps
->state
= SEND_WSC_NACK
;
1203 enum wps_process_res
wps_enrollee_process_msg(struct wps_data
*wps
,
1204 enum wsc_op_code op_code
,
1205 const struct wpabuf
*msg
)
1208 wpa_printf(MSG_DEBUG
, "WPS: Processing received message (len=%lu "
1210 (unsigned long) wpabuf_len(msg
), op_code
);
1212 if (op_code
== WSC_UPnP
) {
1213 /* Determine the OpCode based on message type attribute */
1214 struct wps_parse_attr attr
;
1215 if (wps_parse_msg(msg
, &attr
) == 0 && attr
.msg_type
) {
1216 if (*attr
.msg_type
== WPS_WSC_ACK
)
1218 else if (*attr
.msg_type
== WPS_WSC_NACK
)
1226 return wps_process_wsc_msg(wps
, msg
);
1228 return wps_process_wsc_ack(wps
, msg
);
1230 return wps_process_wsc_nack(wps
, msg
);
1232 wpa_printf(MSG_DEBUG
, "WPS: Unsupported op_code %d", op_code
);