2 * Wi-Fi Protected Setup - common functionality
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 "dh_groups.h"
25 static int wps_set_attr(struct wps_parse_attr *attr, u16 type,
26 const u8 *pos, u16 len)
31 wpa_printf(MSG_DEBUG, "WPS: Invalid Version length %u",
39 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type "
45 case ATTR_ENROLLEE_NONCE:
46 if (len != WPS_NONCE_LEN) {
47 wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce "
51 attr->enrollee_nonce = pos;
53 case ATTR_REGISTRAR_NONCE:
54 if (len != WPS_NONCE_LEN) {
55 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce "
59 attr->registrar_nonce = pos;
62 if (len != WPS_UUID_LEN) {
63 wpa_printf(MSG_DEBUG, "WPS: Invalid UUID-E length %u",
70 if (len != WPS_UUID_LEN) {
71 wpa_printf(MSG_DEBUG, "WPS: Invalid UUID-R length %u",
77 case ATTR_AUTH_TYPE_FLAGS:
79 wpa_printf(MSG_DEBUG, "WPS: Invalid Authentication "
80 "Type Flags length %u", len);
83 attr->auth_type_flags = pos;
85 case ATTR_ENCR_TYPE_FLAGS:
87 wpa_printf(MSG_DEBUG, "WPS: Invalid Encryption Type "
88 "Flags length %u", len);
91 attr->encr_type_flags = pos;
93 case ATTR_CONN_TYPE_FLAGS:
95 wpa_printf(MSG_DEBUG, "WPS: Invalid Connection Type "
96 "Flags length %u", len);
99 attr->conn_type_flags = pos;
101 case ATTR_CONFIG_METHODS:
103 wpa_printf(MSG_DEBUG, "WPS: Invalid Config Methods "
107 attr->config_methods = pos;
109 case ATTR_SELECTED_REGISTRAR_CONFIG_METHODS:
111 wpa_printf(MSG_DEBUG, "WPS: Invalid Selected "
112 "Registrar Config Methods length %u", len);
115 attr->sel_reg_config_methods = pos;
117 case ATTR_PRIMARY_DEV_TYPE:
118 if (len != sizeof(struct wps_dev_type)) {
119 wpa_printf(MSG_DEBUG, "WPS: Invalid Primary Device "
120 "Type length %u", len);
123 attr->primary_dev_type = pos;
127 wpa_printf(MSG_DEBUG, "WPS: Invalid RF Bands length "
131 attr->rf_bands = pos;
133 case ATTR_ASSOC_STATE:
135 wpa_printf(MSG_DEBUG, "WPS: Invalid Association State "
139 attr->assoc_state = pos;
141 case ATTR_CONFIG_ERROR:
143 wpa_printf(MSG_DEBUG, "WPS: Invalid Configuration "
144 "Error length %u", len);
147 attr->config_error = pos;
149 case ATTR_DEV_PASSWORD_ID:
151 wpa_printf(MSG_DEBUG, "WPS: Invalid Device Password "
152 "ID length %u", len);
155 attr->dev_password_id = pos;
157 case ATTR_OS_VERSION:
159 wpa_printf(MSG_DEBUG, "WPS: Invalid OS Version length "
163 attr->os_version = pos;
167 wpa_printf(MSG_DEBUG, "WPS: Invalid Wi-Fi Protected "
168 "Setup State length %u", len);
171 attr->wps_state = pos;
173 case ATTR_AUTHENTICATOR:
174 if (len != WPS_AUTHENTICATOR_LEN) {
175 wpa_printf(MSG_DEBUG, "WPS: Invalid Authenticator "
179 attr->authenticator = pos;
182 if (len != WPS_HASH_LEN) {
183 wpa_printf(MSG_DEBUG, "WPS: Invalid R-Hash1 length %u",
190 if (len != WPS_HASH_LEN) {
191 wpa_printf(MSG_DEBUG, "WPS: Invalid R-Hash2 length %u",
198 if (len != WPS_HASH_LEN) {
199 wpa_printf(MSG_DEBUG, "WPS: Invalid E-Hash1 length %u",
206 if (len != WPS_HASH_LEN) {
207 wpa_printf(MSG_DEBUG, "WPS: Invalid E-Hash2 length %u",
214 if (len != WPS_SECRET_NONCE_LEN) {
215 wpa_printf(MSG_DEBUG, "WPS: Invalid R-SNonce1 length "
219 attr->r_snonce1 = pos;
222 if (len != WPS_SECRET_NONCE_LEN) {
223 wpa_printf(MSG_DEBUG, "WPS: Invalid R-SNonce2 length "
227 attr->r_snonce2 = pos;
230 if (len != WPS_SECRET_NONCE_LEN) {
231 wpa_printf(MSG_DEBUG, "WPS: Invalid E-SNonce1 length "
235 attr->e_snonce1 = pos;
238 if (len != WPS_SECRET_NONCE_LEN) {
239 wpa_printf(MSG_DEBUG, "WPS: Invalid E-SNonce2 length "
243 attr->e_snonce2 = pos;
245 case ATTR_KEY_WRAP_AUTH:
246 if (len != WPS_KWA_LEN) {
247 wpa_printf(MSG_DEBUG, "WPS: Invalid Key Wrap "
248 "Authenticator length %u", len);
251 attr->key_wrap_auth = pos;
255 wpa_printf(MSG_DEBUG, "WPS: Invalid Authentication "
256 "Type length %u", len);
259 attr->auth_type = pos;
263 wpa_printf(MSG_DEBUG, "WPS: Invalid Encryption "
264 "Type length %u", len);
267 attr->encr_type = pos;
269 case ATTR_NETWORK_INDEX:
271 wpa_printf(MSG_DEBUG, "WPS: Invalid Network Index "
275 attr->network_idx = pos;
277 case ATTR_NETWORK_KEY_INDEX:
279 wpa_printf(MSG_DEBUG, "WPS: Invalid Network Key Index "
283 attr->network_key_idx = pos;
286 if (len != ETH_ALEN) {
287 wpa_printf(MSG_DEBUG, "WPS: Invalid MAC Address "
291 attr->mac_addr = pos;
293 case ATTR_KEY_PROVIDED_AUTO:
295 wpa_printf(MSG_DEBUG, "WPS: Invalid Key Provided "
296 "Automatically length %u", len);
299 attr->key_prov_auto = pos;
301 case ATTR_802_1X_ENABLED:
303 wpa_printf(MSG_DEBUG, "WPS: Invalid 802.1X Enabled "
307 attr->dot1x_enabled = pos;
309 case ATTR_SELECTED_REGISTRAR:
311 wpa_printf(MSG_DEBUG, "WPS: Invalid Selected Registrar"
315 attr->selected_registrar = pos;
317 case ATTR_REQUEST_TYPE:
319 wpa_printf(MSG_DEBUG, "WPS: Invalid Request Type "
323 attr->request_type = pos;
325 case ATTR_RESPONSE_TYPE:
327 wpa_printf(MSG_DEBUG, "WPS: Invalid Response Type "
331 attr->request_type = pos;
333 case ATTR_MANUFACTURER:
334 attr->manufacturer = pos;
335 attr->manufacturer_len = len;
337 case ATTR_MODEL_NAME:
338 attr->model_name = pos;
339 attr->model_name_len = len;
341 case ATTR_MODEL_NUMBER:
342 attr->model_number = pos;
343 attr->model_number_len = len;
345 case ATTR_SERIAL_NUMBER:
346 attr->serial_number = pos;
347 attr->serial_number_len = len;
350 attr->dev_name = pos;
351 attr->dev_name_len = len;
353 case ATTR_PUBLIC_KEY:
354 attr->public_key = pos;
355 attr->public_key_len = len;
357 case ATTR_ENCR_SETTINGS:
358 attr->encr_settings = pos;
359 attr->encr_settings_len = len;
362 if (attr->num_cred >= MAX_CRED_COUNT) {
363 wpa_printf(MSG_DEBUG, "WPS: Skipped Credential "
364 "attribute (max %d credentials)",
368 attr->cred[attr->num_cred] = pos;
369 attr->cred_len[attr->num_cred] = len;
374 attr->ssid_len = len;
376 case ATTR_NETWORK_KEY:
377 attr->network_key = pos;
378 attr->network_key_len = len;
381 attr->eap_type = pos;
382 attr->eap_type_len = len;
384 case ATTR_EAP_IDENTITY:
385 attr->eap_identity = pos;
386 attr->eap_identity_len = len;
389 wpa_printf(MSG_DEBUG, "WPS: Unsupported attribute type 0x%x "
390 "len=%u", type, len);
398 int wps_parse_msg(const struct wpabuf *msg, struct wps_parse_attr *attr)
403 os_memset(attr, 0, sizeof(*attr));
404 pos = wpabuf_head(msg);
405 end = pos + wpabuf_len(msg);
409 wpa_printf(MSG_DEBUG, "WPS: Invalid message - "
410 "%lu bytes remaining",
411 (unsigned long) (end - pos));
415 type = WPA_GET_BE16(pos);
417 len = WPA_GET_BE16(pos);
419 wpa_printf(MSG_MSGDUMP, "WPS: attr type=0x%x len=%u",
421 if (len > end - pos) {
422 wpa_printf(MSG_DEBUG, "WPS: Attribute overflow");
426 if (wps_set_attr(attr, type, pos, len) < 0)
436 void wps_kdf(const u8 *key, const char *label, u8 *res, size_t res_len)
438 u8 i_buf[4], key_bits[4];
442 u8 hash[SHA256_MAC_LEN], *opos;
445 WPA_PUT_BE32(key_bits, res_len * 8);
448 len[0] = sizeof(i_buf);
449 addr[1] = (const u8 *) label;
450 len[1] = os_strlen(label);
452 len[2] = sizeof(key_bits);
454 iter = (res_len + SHA256_MAC_LEN - 1) / SHA256_MAC_LEN;
458 for (i = 1; i <= iter; i++) {
459 WPA_PUT_BE32(i_buf, i);
460 hmac_sha256_vector(key, SHA256_MAC_LEN, 3, addr, len, hash);
462 os_memcpy(opos, hash, SHA256_MAC_LEN);
463 opos += SHA256_MAC_LEN;
464 left -= SHA256_MAC_LEN;
466 os_memcpy(opos, hash, left);
471 int wps_build_public_key(struct wps_data *wps, struct wpabuf *msg)
473 struct wpabuf *pubkey;
475 wpa_printf(MSG_DEBUG, "WPS: * Public Key");
476 pubkey = dh_init(dh_groups_get(WPS_DH_GROUP), &wps->dh_privkey);
477 if (pubkey == NULL) {
478 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize "
479 "Diffie-Hellman handshake");
483 wpabuf_put_be16(msg, ATTR_PUBLIC_KEY);
484 wpabuf_put_be16(msg, wpabuf_len(pubkey));
485 wpabuf_put_buf(msg, pubkey);
487 if (wps->registrar) {
488 wpabuf_free(wps->dh_pubkey_r);
489 wps->dh_pubkey_r = pubkey;
491 wpabuf_free(wps->dh_pubkey_e);
492 wps->dh_pubkey_e = pubkey;
499 int wps_derive_keys(struct wps_data *wps)
501 struct wpabuf *pubkey, *dh_shared;
502 u8 dhkey[SHA256_MAC_LEN], kdk[SHA256_MAC_LEN];
505 u8 keys[WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN + WPS_EMSK_LEN];
507 if (wps->dh_privkey == NULL) {
508 wpa_printf(MSG_DEBUG, "WPS: Own DH private key not available");
512 pubkey = wps->registrar ? wps->dh_pubkey_e : wps->dh_pubkey_r;
513 if (pubkey == NULL) {
514 wpa_printf(MSG_DEBUG, "WPS: Peer DH public key not available");
518 dh_shared = dh_derive_shared(pubkey, wps->dh_privkey,
519 dh_groups_get(WPS_DH_GROUP));
520 if (dh_shared == NULL) {
521 wpa_printf(MSG_DEBUG, "WPS: Failed to derive DH shared key");
525 /* Own DH private key is not needed anymore */
526 wpabuf_free(wps->dh_privkey);
527 wps->dh_privkey = NULL;
529 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH shared key", dh_shared);
531 /* DHKey = SHA-256(g^AB mod p) */
532 addr[0] = wpabuf_head(dh_shared);
533 len[0] = wpabuf_len(dh_shared);
534 sha256_vector(1, addr, len, dhkey);
535 wpa_hexdump_key(MSG_DEBUG, "WPS: DHKey", dhkey, sizeof(dhkey));
536 wpabuf_free(dh_shared);
538 /* KDK = HMAC-SHA-256_DHKey(N1 || EnrolleeMAC || N2) */
539 addr[0] = wps->nonce_e;
540 len[0] = WPS_NONCE_LEN;
541 addr[1] = wps->mac_addr_e;
543 addr[2] = wps->nonce_r;
544 len[2] = WPS_NONCE_LEN;
545 hmac_sha256_vector(dhkey, sizeof(dhkey), 3, addr, len, kdk);
546 wpa_hexdump_key(MSG_DEBUG, "WPS: KDK", kdk, sizeof(kdk));
548 wps_kdf(kdk, "Wi-Fi Easy and Secure Key Derivation",
550 os_memcpy(wps->authkey, keys, WPS_AUTHKEY_LEN);
551 os_memcpy(wps->keywrapkey, keys + WPS_AUTHKEY_LEN, WPS_KEYWRAPKEY_LEN);
552 os_memcpy(wps->emsk, keys + WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN,
555 wpa_hexdump_key(MSG_DEBUG, "WPS: AuthKey",
556 wps->authkey, WPS_AUTHKEY_LEN);
557 wpa_hexdump_key(MSG_DEBUG, "WPS: KeyWrapKey",
558 wps->keywrapkey, WPS_KEYWRAPKEY_LEN);
559 wpa_hexdump_key(MSG_DEBUG, "WPS: EMSK", wps->emsk, WPS_EMSK_LEN);
565 int wps_build_authenticator(struct wps_data *wps, struct wpabuf *msg)
567 u8 hash[SHA256_MAC_LEN];
571 if (wps->last_msg == NULL) {
572 wpa_printf(MSG_DEBUG, "WPS: Last message not available for "
573 "building authenticator");
577 /* Authenticator = HMAC-SHA256_AuthKey(M_prev || M_curr*)
578 * (M_curr* is M_curr without the Authenticator attribute)
580 addr[0] = wpabuf_head(wps->last_msg);
581 len[0] = wpabuf_len(wps->last_msg);
582 addr[1] = wpabuf_head(msg);
583 len[1] = wpabuf_len(msg);
584 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 2, addr, len, hash);
586 wpa_printf(MSG_DEBUG, "WPS: * Authenticator");
587 wpabuf_put_be16(msg, ATTR_AUTHENTICATOR);
588 wpabuf_put_be16(msg, WPS_AUTHENTICATOR_LEN);
589 wpabuf_put_data(msg, hash, WPS_AUTHENTICATOR_LEN);
595 int wps_process_authenticator(struct wps_data *wps, const u8 *authenticator,
596 const struct wpabuf *msg)
598 u8 hash[SHA256_MAC_LEN];
602 if (authenticator == NULL) {
603 wpa_printf(MSG_DEBUG, "WPS: No Authenticator attribute "
608 if (wps->last_msg == NULL) {
609 wpa_printf(MSG_DEBUG, "WPS: Last message not available for "
610 "validating authenticator");
614 /* Authenticator = HMAC-SHA256_AuthKey(M_prev || M_curr*)
615 * (M_curr* is M_curr without the Authenticator attribute)
617 addr[0] = wpabuf_head(wps->last_msg);
618 len[0] = wpabuf_len(wps->last_msg);
619 addr[1] = wpabuf_head(msg);
620 len[1] = wpabuf_len(msg) - 4 - WPS_AUTHENTICATOR_LEN;
621 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 2, addr, len, hash);
623 if (os_memcmp(hash, authenticator, WPS_AUTHENTICATOR_LEN) != 0) {
624 wpa_printf(MSG_DEBUG, "WPS: Incorrect Authenticator");
632 void wps_derive_psk(struct wps_data *wps, const u8 *dev_passwd,
633 size_t dev_passwd_len)
635 u8 hash[SHA256_MAC_LEN];
637 hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, dev_passwd,
638 (dev_passwd_len + 1) / 2, hash);
639 os_memcpy(wps->psk1, hash, WPS_PSK_LEN);
640 hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN,
641 dev_passwd + (dev_passwd_len + 1) / 2,
642 dev_passwd_len / 2, hash);
643 os_memcpy(wps->psk2, hash, WPS_PSK_LEN);
645 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Device Password",
646 dev_passwd, dev_passwd_len);
647 wpa_hexdump_key(MSG_DEBUG, "WPS: PSK1", wps->psk1, WPS_PSK_LEN);
648 wpa_hexdump_key(MSG_DEBUG, "WPS: PSK2", wps->psk2, WPS_PSK_LEN);
652 struct wpabuf * wps_decrypt_encr_settings(struct wps_data *wps, const u8 *encr,
655 struct wpabuf *decrypted;
656 const size_t block_size = 16;
662 if (encr == NULL || encr_len < 2 * block_size || encr_len % block_size)
664 wpa_printf(MSG_DEBUG, "WPS: No Encrypted Settings received");
668 decrypted = wpabuf_alloc(encr_len - block_size);
669 if (decrypted == NULL)
672 wpa_hexdump(MSG_MSGDUMP, "WPS: Encrypted Settings", encr, encr_len);
673 wpabuf_put_data(decrypted, encr + block_size, encr_len - block_size);
674 if (aes_128_cbc_decrypt(wps->keywrapkey, encr, wpabuf_mhead(decrypted),
675 wpabuf_len(decrypted))) {
676 wpabuf_free(decrypted);
680 wpa_hexdump_buf_key(MSG_MSGDUMP, "WPS: Decrypted Encrypted Settings",
683 pos = wpabuf_head_u8(decrypted) + wpabuf_len(decrypted) - 1;
685 if (pad > wpabuf_len(decrypted)) {
686 wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad value");
687 wpabuf_free(decrypted);
690 for (i = 0; i < pad; i++) {
692 wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad "
694 wpabuf_free(decrypted);
698 decrypted->used -= pad;
704 int wps_process_key_wrap_auth(struct wps_data *wps, struct wpabuf *msg,
705 const u8 *key_wrap_auth)
707 u8 hash[SHA256_MAC_LEN];
711 if (key_wrap_auth == NULL) {
712 wpa_printf(MSG_DEBUG, "WPS: No KWA in decrypted attribute");
716 head = wpabuf_head(msg);
717 len = wpabuf_len(msg) - 4 - WPS_KWA_LEN;
718 if (head + len != key_wrap_auth - 4) {
719 wpa_printf(MSG_DEBUG, "WPS: KWA not in the end of the "
720 "decrypted attribute");
724 hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, head, len, hash);
725 if (os_memcmp(hash, key_wrap_auth, WPS_KWA_LEN) != 0) {
726 wpa_printf(MSG_DEBUG, "WPS: Invalid KWA");
734 int wps_build_version(struct wpabuf *msg)
736 wpa_printf(MSG_DEBUG, "WPS: * Version");
737 wpabuf_put_be16(msg, ATTR_VERSION);
738 wpabuf_put_be16(msg, 1);
739 wpabuf_put_u8(msg, WPS_VERSION);
744 int wps_build_msg_type(struct wpabuf *msg, enum wps_msg_type msg_type)
746 wpa_printf(MSG_DEBUG, "WPS: * Message Type (%d)", msg_type);
747 wpabuf_put_be16(msg, ATTR_MSG_TYPE);
748 wpabuf_put_be16(msg, 1);
749 wpabuf_put_u8(msg, msg_type);
754 int wps_build_enrollee_nonce(struct wps_data *wps, struct wpabuf *msg)
756 wpa_printf(MSG_DEBUG, "WPS: * Enrollee Nonce");
757 wpabuf_put_be16(msg, ATTR_ENROLLEE_NONCE);
758 wpabuf_put_be16(msg, WPS_NONCE_LEN);
759 wpabuf_put_data(msg, wps->nonce_e, WPS_NONCE_LEN);
764 int wps_build_registrar_nonce(struct wps_data *wps, struct wpabuf *msg)
766 wpa_printf(MSG_DEBUG, "WPS: * Registrar Nonce");
767 wpabuf_put_be16(msg, ATTR_REGISTRAR_NONCE);
768 wpabuf_put_be16(msg, WPS_NONCE_LEN);
769 wpabuf_put_data(msg, wps->nonce_r, WPS_NONCE_LEN);
774 int wps_build_auth_type_flags(struct wps_data *wps, struct wpabuf *msg)
776 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type Flags");
777 wpabuf_put_be16(msg, ATTR_AUTH_TYPE_FLAGS);
778 wpabuf_put_be16(msg, 2);
779 wpabuf_put_be16(msg, WPS_AUTH_TYPES);
784 int wps_build_encr_type_flags(struct wps_data *wps, struct wpabuf *msg)
786 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type Flags");
787 wpabuf_put_be16(msg, ATTR_ENCR_TYPE_FLAGS);
788 wpabuf_put_be16(msg, 2);
789 wpabuf_put_be16(msg, WPS_ENCR_TYPES);
794 int wps_build_conn_type_flags(struct wps_data *wps, struct wpabuf *msg)
796 wpa_printf(MSG_DEBUG, "WPS: * Connection Type Flags");
797 wpabuf_put_be16(msg, ATTR_CONN_TYPE_FLAGS);
798 wpabuf_put_be16(msg, 1);
799 wpabuf_put_u8(msg, WPS_CONN_ESS);
804 int wps_build_assoc_state(struct wps_data *wps, struct wpabuf *msg)
806 wpa_printf(MSG_DEBUG, "WPS: * Association State");
807 wpabuf_put_be16(msg, ATTR_ASSOC_STATE);
808 wpabuf_put_be16(msg, 2);
809 wpabuf_put_be16(msg, WPS_ASSOC_NOT_ASSOC);
814 int wps_build_key_wrap_auth(struct wps_data *wps, struct wpabuf *msg)
816 u8 hash[SHA256_MAC_LEN];
818 wpa_printf(MSG_DEBUG, "WPS: * Key Wrap Authenticator");
819 hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, wpabuf_head(msg),
820 wpabuf_len(msg), hash);
822 wpabuf_put_be16(msg, ATTR_KEY_WRAP_AUTH);
823 wpabuf_put_be16(msg, WPS_KWA_LEN);
824 wpabuf_put_data(msg, hash, WPS_KWA_LEN);
829 int wps_build_encr_settings(struct wps_data *wps, struct wpabuf *msg,
830 struct wpabuf *plain)
833 const size_t block_size = 16;
836 wpa_printf(MSG_DEBUG, "WPS: * Encrypted Settings");
838 /* PKCS#5 v2.0 pad */
839 pad_len = block_size - wpabuf_len(plain) % block_size;
840 os_memset(wpabuf_put(plain, pad_len), pad_len, pad_len);
842 wpabuf_put_be16(msg, ATTR_ENCR_SETTINGS);
843 wpabuf_put_be16(msg, block_size + wpabuf_len(plain));
845 iv = wpabuf_put(msg, block_size);
846 if (os_get_random(iv, block_size) < 0)
849 data = wpabuf_put(msg, 0);
850 wpabuf_put_buf(msg, plain);
851 if (aes_128_cbc_encrypt(wps->keywrapkey, iv, data, wpabuf_len(plain)))
858 static int wps_process_cred_network_idx(struct wps_credential *cred,
862 wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
867 wpa_printf(MSG_DEBUG, "WPS: Network Index: %d", *idx);
873 static int wps_process_cred_ssid(struct wps_credential *cred, const u8 *ssid,
877 wpa_printf(MSG_DEBUG, "WPS: Credential did not include SSID");
881 /* Remove zero-padding since some Registrar implementations seem to use
882 * hardcoded 32-octet length for this attribute */
883 while (ssid_len > 0 && ssid[ssid_len - 1] == 0)
886 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", ssid, ssid_len);
887 if (ssid_len <= sizeof(cred->ssid)) {
888 os_memcpy(cred->ssid, ssid, ssid_len);
889 cred->ssid_len = ssid_len;
896 static int wps_process_cred_auth_type(struct wps_credential *cred,
899 if (auth_type == NULL) {
900 wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
901 "Authentication Type");
905 cred->auth_type = WPA_GET_BE16(auth_type);
906 wpa_printf(MSG_DEBUG, "WPS: Authentication Type: 0x%x",
913 static int wps_process_cred_encr_type(struct wps_credential *cred,
916 if (encr_type == NULL) {
917 wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
922 cred->encr_type = WPA_GET_BE16(encr_type);
923 wpa_printf(MSG_DEBUG, "WPS: Encryption Type: 0x%x",
930 static int wps_process_cred_network_key_idx(struct wps_credential *cred,
934 return 0; /* optional attribute */
936 wpa_printf(MSG_DEBUG, "WPS: Network Key Index: %d", *key_idx);
937 cred->key_idx = *key_idx;
943 static int wps_process_cred_network_key(struct wps_credential *cred,
944 const u8 *key, size_t key_len)
947 wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
952 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key", key, key_len);
953 if (key_len <= sizeof(cred->key)) {
954 os_memcpy(cred->key, key, key_len);
955 cred->key_len = key_len;
962 static int wps_process_cred_mac_addr(struct wps_credential *cred,
965 if (mac_addr == NULL) {
966 wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
971 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR, MAC2STR(mac_addr));
972 os_memcpy(cred->mac_addr, mac_addr, ETH_ALEN);
978 static int wps_process_cred_eap_type(struct wps_credential *cred,
979 const u8 *eap_type, size_t eap_type_len)
981 if (eap_type == NULL)
982 return 0; /* optional attribute */
984 wpa_hexdump(MSG_DEBUG, "WPS: EAP Type", eap_type, eap_type_len);
990 static int wps_process_cred_eap_identity(struct wps_credential *cred,
994 if (identity == NULL)
995 return 0; /* optional attribute */
997 wpa_hexdump_ascii(MSG_DEBUG, "WPS: EAP Identity",
998 identity, identity_len);
1004 static int wps_process_cred_key_prov_auto(struct wps_credential *cred,
1005 const u8 *key_prov_auto)
1007 if (key_prov_auto == NULL)
1008 return 0; /* optional attribute */
1010 wpa_printf(MSG_DEBUG, "WPS: Key Provided Automatically: %d",
1017 static int wps_process_cred_802_1x_enabled(struct wps_credential *cred,
1018 const u8 *dot1x_enabled)
1020 if (dot1x_enabled == NULL)
1021 return 0; /* optional attribute */
1023 wpa_printf(MSG_DEBUG, "WPS: 802.1X Enabled: %d", *dot1x_enabled);
1029 int wps_process_cred(struct wps_parse_attr *attr,
1030 struct wps_credential *cred)
1032 wpa_printf(MSG_DEBUG, "WPS: Process Credential");
1034 /* TODO: support multiple Network Keys */
1035 if (wps_process_cred_network_idx(cred, attr->network_idx) ||
1036 wps_process_cred_ssid(cred, attr->ssid, attr->ssid_len) ||
1037 wps_process_cred_auth_type(cred, attr->auth_type) ||
1038 wps_process_cred_encr_type(cred, attr->encr_type) ||
1039 wps_process_cred_network_key_idx(cred, attr->network_key_idx) ||
1040 wps_process_cred_network_key(cred, attr->network_key,
1041 attr->network_key_len) ||
1042 wps_process_cred_mac_addr(cred, attr->mac_addr) ||
1043 wps_process_cred_eap_type(cred, attr->eap_type,
1044 attr->eap_type_len) ||
1045 wps_process_cred_eap_identity(cred, attr->eap_identity,
1046 attr->eap_identity_len) ||
1047 wps_process_cred_key_prov_auto(cred, attr->key_prov_auto) ||
1048 wps_process_cred_802_1x_enabled(cred, attr->dot1x_enabled))
1055 int wps_process_ap_settings(struct wps_parse_attr *attr,
1056 struct wps_credential *cred)
1058 wpa_printf(MSG_DEBUG, "WPS: Processing AP Settings");
1059 os_memset(cred, 0, sizeof(*cred));
1060 /* TODO: optional attributes New Password and Device Password ID */
1061 if (wps_process_cred_ssid(cred, attr->ssid, attr->ssid_len) ||
1062 wps_process_cred_auth_type(cred, attr->auth_type) ||
1063 wps_process_cred_encr_type(cred, attr->encr_type) ||
1064 wps_process_cred_network_key_idx(cred, attr->network_key_idx) ||
1065 wps_process_cred_network_key(cred, attr->network_key,
1066 attr->network_key_len) ||
1067 wps_process_cred_mac_addr(cred, attr->mac_addr))