2 * Wi-Fi Protected Setup - attribute processing
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
12 #include "crypto/sha256.h"
16 int wps_process_authenticator(struct wps_data *wps, const u8 *authenticator,
17 const struct wpabuf *msg)
19 u8 hash[SHA256_MAC_LEN];
23 if (authenticator == NULL) {
24 wpa_printf(MSG_DEBUG, "WPS: No Authenticator attribute "
29 if (wps->last_msg == NULL) {
30 wpa_printf(MSG_DEBUG, "WPS: Last message not available for "
31 "validating authenticator");
35 /* Authenticator = HMAC-SHA256_AuthKey(M_prev || M_curr*)
36 * (M_curr* is M_curr without the Authenticator attribute)
38 addr[0] = wpabuf_head(wps->last_msg);
39 len[0] = wpabuf_len(wps->last_msg);
40 addr[1] = wpabuf_head(msg);
41 len[1] = wpabuf_len(msg) - 4 - WPS_AUTHENTICATOR_LEN;
42 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 2, addr, len, hash);
44 if (os_memcmp_const(hash, authenticator, WPS_AUTHENTICATOR_LEN) != 0) {
45 wpa_printf(MSG_DEBUG, "WPS: Incorrect Authenticator");
53 int wps_process_key_wrap_auth(struct wps_data *wps, struct wpabuf *msg,
54 const u8 *key_wrap_auth)
56 u8 hash[SHA256_MAC_LEN];
60 if (key_wrap_auth == NULL) {
61 wpa_printf(MSG_DEBUG, "WPS: No KWA in decrypted attribute");
65 head = wpabuf_head(msg);
66 len = wpabuf_len(msg) - 4 - WPS_KWA_LEN;
67 if (head + len != key_wrap_auth - 4) {
68 wpa_printf(MSG_DEBUG, "WPS: KWA not in the end of the "
69 "decrypted attribute");
73 hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, head, len, hash);
74 if (os_memcmp_const(hash, key_wrap_auth, WPS_KWA_LEN) != 0) {
75 wpa_printf(MSG_DEBUG, "WPS: Invalid KWA");
83 static int wps_process_cred_network_idx(struct wps_credential *cred,
87 wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
92 wpa_printf(MSG_DEBUG, "WPS: Network Index: %d", *idx);
98 static int wps_process_cred_ssid(struct wps_credential *cred, const u8 *ssid,
102 wpa_printf(MSG_DEBUG, "WPS: Credential did not include SSID");
106 /* Remove zero-padding since some Registrar implementations seem to use
107 * hardcoded 32-octet length for this attribute */
108 while (ssid_len > 0 && ssid[ssid_len - 1] == 0)
111 wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", ssid, ssid_len);
112 if (ssid_len <= sizeof(cred->ssid)) {
113 os_memcpy(cred->ssid, ssid, ssid_len);
114 cred->ssid_len = ssid_len;
121 static int wps_process_cred_auth_type(struct wps_credential *cred,
124 if (auth_type == NULL) {
125 wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
126 "Authentication Type");
130 cred->auth_type = WPA_GET_BE16(auth_type);
131 wpa_printf(MSG_DEBUG, "WPS: Authentication Type: 0x%x",
138 static int wps_process_cred_encr_type(struct wps_credential *cred,
141 if (encr_type == NULL) {
142 wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
147 cred->encr_type = WPA_GET_BE16(encr_type);
148 wpa_printf(MSG_DEBUG, "WPS: Encryption Type: 0x%x",
155 static int wps_process_cred_network_key_idx(struct wps_credential *cred,
159 return 0; /* optional attribute */
161 wpa_printf(MSG_DEBUG, "WPS: Network Key Index: %d", *key_idx);
162 cred->key_idx = *key_idx;
168 static int wps_process_cred_network_key(struct wps_credential *cred,
169 const u8 *key, size_t key_len)
172 wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
174 if (cred->auth_type == WPS_AUTH_OPEN &&
175 cred->encr_type == WPS_ENCR_NONE) {
176 wpa_printf(MSG_DEBUG, "WPS: Workaround - Allow "
177 "missing mandatory Network Key attribute "
184 wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key", key, key_len);
185 if (key_len <= sizeof(cred->key)) {
186 os_memcpy(cred->key, key, key_len);
187 cred->key_len = key_len;
194 static int wps_process_cred_mac_addr(struct wps_credential *cred,
197 if (mac_addr == NULL) {
198 wpa_printf(MSG_DEBUG, "WPS: Credential did not include "
203 wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR, MAC2STR(mac_addr));
204 os_memcpy(cred->mac_addr, mac_addr, ETH_ALEN);
210 static int wps_workaround_cred_key(struct wps_credential *cred)
212 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK) &&
213 cred->key_len > 8 && cred->key_len < 64 &&
214 cred->key[cred->key_len - 1] == 0) {
215 #ifdef CONFIG_WPS_STRICT
216 wpa_printf(MSG_INFO, "WPS: WPA/WPA2-Personal passphrase uses "
217 "forbidden NULL termination");
218 wpa_hexdump_ascii_key(MSG_INFO, "WPS: Network Key",
219 cred->key, cred->key_len);
221 #else /* CONFIG_WPS_STRICT */
223 * A deployed external registrar is known to encode ASCII
224 * passphrases incorrectly. Remove the extra NULL termination
225 * to fix the encoding.
227 wpa_printf(MSG_DEBUG, "WPS: Workaround - remove NULL "
228 "termination from ASCII passphrase");
230 #endif /* CONFIG_WPS_STRICT */
234 if (cred->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK) &&
235 (cred->key_len < 8 || has_ctrl_char(cred->key, cred->key_len))) {
236 wpa_printf(MSG_INFO, "WPS: Reject credential with invalid WPA/WPA2-Personal passphrase");
237 wpa_hexdump_ascii_key(MSG_INFO, "WPS: Network Key",
238 cred->key, cred->key_len);
246 int wps_process_cred(struct wps_parse_attr *attr,
247 struct wps_credential *cred)
249 wpa_printf(MSG_DEBUG, "WPS: Process Credential");
251 /* TODO: support multiple Network Keys */
252 if (wps_process_cred_network_idx(cred, attr->network_idx) ||
253 wps_process_cred_ssid(cred, attr->ssid, attr->ssid_len) ||
254 wps_process_cred_auth_type(cred, attr->auth_type) ||
255 wps_process_cred_encr_type(cred, attr->encr_type) ||
256 wps_process_cred_network_key_idx(cred, attr->network_key_idx) ||
257 wps_process_cred_network_key(cred, attr->network_key,
258 attr->network_key_len) ||
259 wps_process_cred_mac_addr(cred, attr->mac_addr))
262 return wps_workaround_cred_key(cred);
266 int wps_process_ap_settings(struct wps_parse_attr *attr,
267 struct wps_credential *cred)
269 wpa_printf(MSG_DEBUG, "WPS: Processing AP Settings");
270 os_memset(cred, 0, sizeof(*cred));
271 /* TODO: optional attributes New Password and Device Password ID */
272 if (wps_process_cred_ssid(cred, attr->ssid, attr->ssid_len) ||
273 wps_process_cred_auth_type(cred, attr->auth_type) ||
274 wps_process_cred_encr_type(cred, attr->encr_type) ||
275 wps_process_cred_network_key_idx(cred, attr->network_key_idx) ||
276 wps_process_cred_network_key(cred, attr->network_key,
277 attr->network_key_len) ||
278 wps_process_cred_mac_addr(cred, attr->mac_addr))
281 return wps_workaround_cred_key(cred);