nl80211: Fix a typo
[hostap-gosc2009.git] / src / wps / wps_enrollee.c
blobfbc41e5d276421a8a84ebfdc5339e219ca307144
1 /*
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
10 * license.
12 * See README and COPYING for more details.
15 #include "includes.h"
17 #include "common.h"
18 #include "crypto/crypto.h"
19 #include "crypto/sha256.h"
20 #include "wps_i.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);
30 return 0;
34 static int wps_build_wps_state(struct wps_data *wps, struct wpabuf *msg)
36 u8 state;
37 if (wps->wps->ap)
38 state = wps->wps->wps_state;
39 else
40 state = WPS_STATE_NOT_CONFIGURED;
41 wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
42 state);
43 wpabuf_put_be16(msg, ATTR_WPS_STATE);
44 wpabuf_put_be16(msg, 1);
45 wpabuf_put_u8(msg, state);
46 return 0;
50 static int wps_build_e_hash(struct wps_data *wps, struct wpabuf *msg)
52 u8 *hash;
53 const u8 *addr[4];
54 size_t len[4];
56 if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
57 return -1;
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 "
64 "E-Hash derivation");
65 return -1;
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;
75 addr[1] = wps->psk1;
76 len[1] = WPS_PSK_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;
90 addr[1] = wps->psk2;
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);
94 return 0;
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);
104 return 0;
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);
115 return 0;
119 static struct wpabuf * wps_build_m1(struct wps_data *wps)
121 struct wpabuf *msg;
123 if (os_get_random(wps->nonce_e, WPS_NONCE_LEN) < 0)
124 return NULL;
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);
130 if (msg == NULL)
131 return NULL;
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)) {
150 wpabuf_free(msg);
151 return NULL;
154 wps->state = RECV_M2;
155 return msg;
159 static struct wpabuf * wps_build_m3(struct wps_data *wps)
161 struct wpabuf *msg;
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");
167 return NULL;
169 wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
171 msg = wpabuf_alloc(1000);
172 if (msg == NULL)
173 return NULL;
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)) {
180 wpabuf_free(msg);
181 return NULL;
184 wps->state = RECV_M4;
185 return msg;
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);
196 if (plain == NULL)
197 return NULL;
199 msg = wpabuf_alloc(1000);
200 if (msg == NULL) {
201 wpabuf_free(plain);
202 return NULL;
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)) {
212 wpabuf_free(plain);
213 wpabuf_free(msg);
214 return NULL;
216 wpabuf_free(plain);
218 wps->state = RECV_M6;
219 return msg;
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);
229 return 0;
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);
239 return 0;
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);
249 return 0;
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);
259 return 0;
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);
269 return 0;
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);
279 return 0;
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);
297 if (plain == NULL)
298 return NULL;
300 msg = wpabuf_alloc(1000 + wps->wps->ap_settings_len);
301 if (msg == NULL) {
302 wpabuf_free(plain);
303 return NULL;
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)) {
314 wpabuf_free(plain);
315 wpabuf_free(msg);
316 return NULL;
318 wpabuf_free(plain);
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,
327 wps->uuid_r);
330 wps->state = RECV_M8;
331 return msg;
335 static struct wpabuf * wps_build_wsc_done(struct wps_data *wps)
337 struct wpabuf *msg;
339 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_Done");
341 msg = wpabuf_alloc(1000);
342 if (msg == NULL)
343 return NULL;
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)) {
349 wpabuf_free(msg);
350 return NULL;
353 if (wps->wps->ap)
354 wps->state = RECV_ACK;
355 else {
356 wps_success_event(wps->wps);
357 wps->state = WPS_FINISHED;
359 return msg;
363 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
365 struct wpabuf *msg;
367 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
369 msg = wpabuf_alloc(1000);
370 if (msg == NULL)
371 return NULL;
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)) {
377 wpabuf_free(msg);
378 return NULL;
381 return msg;
385 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
387 struct wpabuf *msg;
389 wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
391 msg = wpabuf_alloc(1000);
392 if (msg == NULL)
393 return NULL;
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)) {
400 wpabuf_free(msg);
401 return NULL;
404 return msg;
408 struct wpabuf * wps_enrollee_get_msg(struct wps_data *wps,
409 enum wsc_op_code *op_code)
411 struct wpabuf *msg;
413 switch (wps->state) {
414 case SEND_M1:
415 msg = wps_build_m1(wps);
416 *op_code = WSC_MSG;
417 break;
418 case SEND_M3:
419 msg = wps_build_m3(wps);
420 *op_code = WSC_MSG;
421 break;
422 case SEND_M5:
423 msg = wps_build_m5(wps);
424 *op_code = WSC_MSG;
425 break;
426 case SEND_M7:
427 msg = wps_build_m7(wps);
428 *op_code = WSC_MSG;
429 break;
430 case RECEIVED_M2D:
431 if (wps->wps->ap) {
432 msg = wps_build_wsc_nack(wps);
433 *op_code = WSC_NACK;
434 break;
436 msg = wps_build_wsc_ack(wps);
437 *op_code = WSC_ACK;
438 if (msg) {
439 /* Another M2/M2D may be received */
440 wps->state = RECV_M2;
442 break;
443 case SEND_WSC_NACK:
444 msg = wps_build_wsc_nack(wps);
445 *op_code = WSC_NACK;
446 break;
447 case WPS_MSG_DONE:
448 msg = wps_build_wsc_done(wps);
449 *op_code = WSC_Done;
450 break;
451 default:
452 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
453 "a message", wps->state);
454 msg = NULL;
455 break;
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);
465 return 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");
473 return -1;
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);
480 return 0;
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");
488 return -1;
491 if (os_memcmp(wps->nonce_e, e_nonce, WPS_NONCE_LEN) != 0) {
492 wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce received");
493 return -1;
496 return 0;
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");
504 return -1;
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);
510 return 0;
514 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
515 size_t pk_len)
517 if (pk == NULL || pk_len == 0) {
518 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
519 return -1;
522 #ifdef CONFIG_WPS_OOB
523 if (wps->dev_pw_id != DEV_PW_DEFAULT &&
524 wps->wps->oob_conf.pubkey_hash) {
525 const u8 *addr[1];
526 u8 hash[WPS_HASH_LEN];
528 addr[0] = pk;
529 sha256_vector(1, addr, &pk_len, hash);
530 if (os_memcmp(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");
534 return -1;
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)
542 return -1;
544 if (wps_derive_keys(wps) < 0)
545 return -1;
547 return 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");
555 return -1;
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);
561 return 0;
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");
569 return -1;
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);
575 return 0;
579 static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
581 u8 hash[SHA256_MAC_LEN];
582 const u8 *addr[4];
583 size_t len[4];
585 if (r_snonce1 == NULL) {
586 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
587 return -1;
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) */
594 addr[0] = r_snonce1;
595 len[0] = WPS_SECRET_NONCE_LEN;
596 addr[1] = wps->psk1;
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);
609 return -1;
612 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
613 "half of the device password");
615 return 0;
619 static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
621 u8 hash[SHA256_MAC_LEN];
622 const u8 *addr[4];
623 size_t len[4];
625 if (r_snonce2 == NULL) {
626 wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
627 return -1;
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) */
634 addr[0] = r_snonce2;
635 len[0] = WPS_SECRET_NONCE_LEN;
636 addr[1] = wps->psk2;
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);
649 return -1;
652 wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
653 "half of the device password");
655 return 0;
659 static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
660 size_t cred_len)
662 struct wps_parse_attr attr;
663 struct wpabuf msg;
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))
670 return -1;
672 if (os_memcmp(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) !=
673 0) {
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;
695 return 0;
699 static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
700 size_t cred_len[], size_t num_cred)
702 size_t i;
704 if (wps->wps->ap)
705 return 0;
707 if (num_cred == 0) {
708 wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
709 "received");
710 return -1;
713 for (i = 0; i < num_cred; i++) {
714 if (wps_process_cred_e(wps, cred[i], cred_len[i]))
715 return -1;
718 return 0;
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;
728 if (!wps->wps->ap)
729 return 0;
731 if (wps_process_ap_settings(attr, &cred) < 0)
732 return -1;
734 wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
735 "Registrar");
737 if (os_memcmp(cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) !=
738 0) {
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);
758 return 0;
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;
772 return WPS_CONTINUE;
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;
782 return WPS_CONTINUE;
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;
790 return WPS_CONTINUE;
793 wps->state = SEND_M3;
794 return WPS_CONTINUE;
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;
807 return WPS_CONTINUE;
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)
840 m2d->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;
849 return WPS_CONTINUE;
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;
866 return WPS_CONTINUE;
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;
874 return WPS_CONTINUE;
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;
883 return WPS_CONTINUE;
886 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
887 "attribute");
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;
893 return WPS_CONTINUE;
895 wpabuf_free(decrypted);
897 wps->state = SEND_M5;
898 return WPS_CONTINUE;
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;
915 return WPS_CONTINUE;
918 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
919 wps_process_authenticator(wps, attr->authenticator, msg)) {
920 wps->state = SEND_WSC_NACK;
921 return WPS_CONTINUE;
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;
930 return WPS_CONTINUE;
933 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
934 "attribute");
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;
940 return WPS_CONTINUE;
942 wpabuf_free(decrypted);
944 wps->state = SEND_M7;
945 return WPS_CONTINUE;
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;
962 return WPS_CONTINUE;
965 if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
966 wps_process_authenticator(wps, attr->authenticator, msg)) {
967 wps->state = SEND_WSC_NACK;
968 return WPS_CONTINUE;
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;
977 return WPS_CONTINUE;
980 wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
981 "attribute");
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,
985 eattr.num_cred) ||
986 wps_process_ap_settings_e(wps, &eattr, decrypted)) {
987 wpabuf_free(decrypted);
988 wps->state = SEND_WSC_NACK;
989 return WPS_CONTINUE;
991 wpabuf_free(decrypted);
993 wps->state = WPS_MSG_DONE;
994 return WPS_CONTINUE;
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)
1007 return WPS_FAILURE;
1009 if (!wps_version_supported(attr.version)) {
1010 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1011 attr.version ? *attr.version : 0);
1012 return WPS_FAILURE;
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");
1018 return WPS_FAILURE;
1021 if (attr.msg_type == NULL) {
1022 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1023 return WPS_FAILURE;
1026 switch (*attr.msg_type) {
1027 case WPS_M2:
1028 ret = wps_process_m2(wps, msg, &attr);
1029 break;
1030 case WPS_M2D:
1031 ret = wps_process_m2d(wps, &attr);
1032 break;
1033 case WPS_M4:
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);
1037 break;
1038 case WPS_M6:
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);
1042 break;
1043 case WPS_M8:
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);
1047 break;
1048 default:
1049 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1050 *attr.msg_type);
1051 return WPS_FAILURE;
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);
1068 return ret;
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)
1080 return WPS_FAILURE;
1082 if (!wps_version_supported(attr.version)) {
1083 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1084 attr.version ? *attr.version : 0);
1085 return WPS_FAILURE;
1088 if (attr.msg_type == NULL) {
1089 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1090 return WPS_FAILURE;
1093 if (*attr.msg_type != WPS_WSC_ACK) {
1094 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1095 *attr.msg_type);
1096 return WPS_FAILURE;
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");
1103 return WPS_FAILURE;
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");
1109 return WPS_FAILURE;
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;
1117 return WPS_DONE;
1120 return WPS_FAILURE;
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)
1132 return WPS_FAILURE;
1134 if (!wps_version_supported(attr.version)) {
1135 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1136 attr.version ? *attr.version : 0);
1137 return WPS_FAILURE;
1140 if (attr.msg_type == NULL) {
1141 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1142 return WPS_FAILURE;
1145 if (*attr.msg_type != WPS_WSC_NACK) {
1146 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1147 *attr.msg_type);
1148 return WPS_FAILURE;
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);
1159 return WPS_FAILURE;
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);
1169 return WPS_FAILURE;
1172 if (attr.config_error == NULL) {
1173 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1174 "in WSC_NACK");
1175 return WPS_FAILURE;
1178 wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
1179 "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1181 switch (wps->state) {
1182 case RECV_M4:
1183 wps_fail_event(wps->wps, WPS_M3);
1184 break;
1185 case RECV_M6:
1186 wps_fail_event(wps->wps, WPS_M5);
1187 break;
1188 case RECV_M8:
1189 wps_fail_event(wps->wps, WPS_M7);
1190 break;
1191 default:
1192 break;
1195 /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
1196 * Enrollee is Authenticator */
1197 wps->state = SEND_WSC_NACK;
1199 return WPS_FAILURE;
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 "
1209 "op_code=%d)",
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)
1217 op_code = WSC_ACK;
1218 else if (*attr.msg_type == WPS_WSC_NACK)
1219 op_code = WSC_NACK;
1223 switch (op_code) {
1224 case WSC_MSG:
1225 case WSC_UPnP:
1226 return wps_process_wsc_msg(wps, msg);
1227 case WSC_ACK:
1228 return wps_process_wsc_ack(wps, msg);
1229 case WSC_NACK:
1230 return wps_process_wsc_nack(wps, msg);
1231 default:
1232 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
1233 return WPS_FAILURE;