2 * Wi-Fi Protected Setup - attribute building
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/aes_wrap.h"
13 #include "crypto/crypto.h"
14 #include "crypto/dh_group5.h"
15 #include "crypto/sha256.h"
16 #include "crypto/random.h"
17 #include "common/ieee802_11_defs.h"
21 int wps_build_public_key(struct wps_data *wps, struct wpabuf *msg)
23 struct wpabuf *pubkey;
25 wpa_printf(MSG_DEBUG, "WPS: * Public Key");
26 wpabuf_free(wps->dh_privkey);
27 wps->dh_privkey = NULL;
28 if (wps->dev_pw_id != DEV_PW_DEFAULT && wps->wps->dh_privkey &&
30 wpa_printf(MSG_DEBUG, "WPS: Using pre-configured DH keys");
31 if (wps->wps->dh_pubkey == NULL) {
33 "WPS: wps->wps->dh_pubkey == NULL");
36 wps->dh_privkey = wpabuf_dup(wps->wps->dh_privkey);
37 wps->dh_ctx = wps->wps->dh_ctx;
38 wps->wps->dh_ctx = NULL;
39 pubkey = wpabuf_dup(wps->wps->dh_pubkey);
41 } else if ((wps->dev_pw_id >= 0x10 ||
42 wps->dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER) &&
44 (wps->wps->ap_nfc_dh_pubkey &&
45 wps->wps->ap_nfc_dev_pw_id ==
46 DEV_PW_NFC_CONNECTION_HANDOVER &&
47 wps->dev_pw_id == DEV_PW_NFC_CONNECTION_HANDOVER)) &&
48 (wps->dev_pw_id == wps->wps->ap_nfc_dev_pw_id ||
49 wps->wps->ap_nfc_dh_pubkey)) {
50 wpa_printf(MSG_DEBUG, "WPS: Using NFC password token DH keys");
51 if (wps->wps->ap_nfc_dh_privkey == NULL) {
53 "WPS: wps->wps->ap_nfc_dh_privkey == NULL");
56 if (wps->wps->ap_nfc_dh_pubkey == NULL) {
58 "WPS: wps->wps->ap_nfc_dh_pubkey == NULL");
61 wps->dh_privkey = wpabuf_dup(wps->wps->ap_nfc_dh_privkey);
62 pubkey = wpabuf_dup(wps->wps->ap_nfc_dh_pubkey);
63 wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, pubkey);
64 #endif /* CONFIG_WPS_NFC */
66 wpa_printf(MSG_DEBUG, "WPS: Generate new DH keys");
67 dh5_free(wps->dh_ctx);
68 wps->dh_ctx = dh5_init(&wps->dh_privkey, &pubkey);
69 pubkey = wpabuf_zeropad(pubkey, 192);
71 if (wps->dh_ctx == NULL || wps->dh_privkey == NULL || pubkey == NULL) {
72 wpa_printf(MSG_DEBUG, "WPS: Failed to initialize "
73 "Diffie-Hellman handshake");
77 wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH Private Key", wps->dh_privkey);
78 wpa_hexdump_buf(MSG_DEBUG, "WPS: DH own Public Key", pubkey);
80 wpabuf_put_be16(msg, ATTR_PUBLIC_KEY);
81 wpabuf_put_be16(msg, wpabuf_len(pubkey));
82 wpabuf_put_buf(msg, pubkey);
85 wpabuf_free(wps->dh_pubkey_r);
86 wps->dh_pubkey_r = pubkey;
88 wpabuf_free(wps->dh_pubkey_e);
89 wps->dh_pubkey_e = pubkey;
96 int wps_build_req_type(struct wpabuf *msg, enum wps_request_type type)
98 wpa_printf(MSG_DEBUG, "WPS: * Request Type");
99 wpabuf_put_be16(msg, ATTR_REQUEST_TYPE);
100 wpabuf_put_be16(msg, 1);
101 wpabuf_put_u8(msg, type);
106 int wps_build_resp_type(struct wpabuf *msg, enum wps_response_type type)
108 wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", type);
109 wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
110 wpabuf_put_be16(msg, 1);
111 wpabuf_put_u8(msg, type);
116 int wps_build_config_methods(struct wpabuf *msg, u16 methods)
118 wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
119 wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
120 wpabuf_put_be16(msg, 2);
121 wpabuf_put_be16(msg, methods);
126 int wps_build_uuid_e(struct wpabuf *msg, const u8 *uuid)
128 if (wpabuf_tailroom(msg) < 4 + WPS_UUID_LEN)
130 wpa_printf(MSG_DEBUG, "WPS: * UUID-E");
131 wpabuf_put_be16(msg, ATTR_UUID_E);
132 wpabuf_put_be16(msg, WPS_UUID_LEN);
133 wpabuf_put_data(msg, uuid, WPS_UUID_LEN);
138 int wps_build_dev_password_id(struct wpabuf *msg, u16 id)
140 wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
141 wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
142 wpabuf_put_be16(msg, 2);
143 wpabuf_put_be16(msg, id);
148 int wps_build_config_error(struct wpabuf *msg, u16 err)
150 wpa_printf(MSG_DEBUG, "WPS: * Configuration Error (%d)", err);
151 wpabuf_put_be16(msg, ATTR_CONFIG_ERROR);
152 wpabuf_put_be16(msg, 2);
153 wpabuf_put_be16(msg, err);
158 int wps_build_authenticator(struct wps_data *wps, struct wpabuf *msg)
160 u8 hash[SHA256_MAC_LEN];
164 if (wps->last_msg == NULL) {
165 wpa_printf(MSG_DEBUG, "WPS: Last message not available for "
166 "building authenticator");
170 /* Authenticator = HMAC-SHA256_AuthKey(M_prev || M_curr*)
171 * (M_curr* is M_curr without the Authenticator attribute)
173 addr[0] = wpabuf_head(wps->last_msg);
174 len[0] = wpabuf_len(wps->last_msg);
175 addr[1] = wpabuf_head(msg);
176 len[1] = wpabuf_len(msg);
177 hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 2, addr, len, hash);
179 wpa_printf(MSG_DEBUG, "WPS: * Authenticator");
180 wpabuf_put_be16(msg, ATTR_AUTHENTICATOR);
181 wpabuf_put_be16(msg, WPS_AUTHENTICATOR_LEN);
182 wpabuf_put_data(msg, hash, WPS_AUTHENTICATOR_LEN);
188 int wps_build_version(struct wpabuf *msg)
191 * Note: This attribute is deprecated and set to hardcoded 0x10 for
192 * backwards compatibility reasons. The real version negotiation is
193 * done with Version2.
195 if (wpabuf_tailroom(msg) < 5)
197 wpa_printf(MSG_DEBUG, "WPS: * Version (hardcoded 0x10)");
198 wpabuf_put_be16(msg, ATTR_VERSION);
199 wpabuf_put_be16(msg, 1);
200 wpabuf_put_u8(msg, 0x10);
205 int wps_build_wfa_ext(struct wpabuf *msg, int req_to_enroll,
206 const u8 *auth_macs, size_t auth_macs_count)
211 #ifdef CONFIG_WPS_TESTING
212 if (WPS_VERSION == 0x10)
214 #endif /* CONFIG_WPS_TESTING */
216 if (wpabuf_tailroom(msg) <
217 7 + 3 + (req_to_enroll ? 3 : 0) +
218 (auth_macs ? 2 + auth_macs_count * ETH_ALEN : 0))
220 wpabuf_put_be16(msg, ATTR_VENDOR_EXT);
221 len = wpabuf_put(msg, 2); /* to be filled */
222 wpabuf_put_be24(msg, WPS_VENDOR_ID_WFA);
224 wpa_printf(MSG_DEBUG, "WPS: * Version2 (0x%x)", WPS_VERSION);
225 wpabuf_put_u8(msg, WFA_ELEM_VERSION2);
226 wpabuf_put_u8(msg, 1);
227 wpabuf_put_u8(msg, WPS_VERSION);
230 wpa_printf(MSG_DEBUG, "WPS: * Request to Enroll (1)");
231 wpabuf_put_u8(msg, WFA_ELEM_REQUEST_TO_ENROLL);
232 wpabuf_put_u8(msg, 1);
233 wpabuf_put_u8(msg, 1);
236 if (auth_macs && auth_macs_count) {
238 wpa_printf(MSG_DEBUG, "WPS: * AuthorizedMACs (count=%d)",
239 (int) auth_macs_count);
240 wpabuf_put_u8(msg, WFA_ELEM_AUTHORIZEDMACS);
241 wpabuf_put_u8(msg, auth_macs_count * ETH_ALEN);
242 wpabuf_put_data(msg, auth_macs, auth_macs_count * ETH_ALEN);
243 for (i = 0; i < auth_macs_count; i++)
244 wpa_printf(MSG_DEBUG, "WPS: AuthorizedMAC: " MACSTR,
245 MAC2STR(&auth_macs[i * ETH_ALEN]));
248 WPA_PUT_BE16(len, (u8 *) wpabuf_put(msg, 0) - len - 2);
249 #endif /* CONFIG_WPS2 */
251 #ifdef CONFIG_WPS_TESTING
252 if (WPS_VERSION > 0x20) {
253 if (wpabuf_tailroom(msg) < 5)
255 wpa_printf(MSG_DEBUG, "WPS: * Extensibility Testing - extra "
257 wpabuf_put_be16(msg, ATTR_EXTENSIBILITY_TEST);
258 wpabuf_put_be16(msg, 1);
259 wpabuf_put_u8(msg, 42);
261 #endif /* CONFIG_WPS_TESTING */
266 int wps_build_msg_type(struct wpabuf *msg, enum wps_msg_type msg_type)
268 wpa_printf(MSG_DEBUG, "WPS: * Message Type (%d)", msg_type);
269 wpabuf_put_be16(msg, ATTR_MSG_TYPE);
270 wpabuf_put_be16(msg, 1);
271 wpabuf_put_u8(msg, msg_type);
276 int wps_build_enrollee_nonce(struct wps_data *wps, struct wpabuf *msg)
278 wpa_printf(MSG_DEBUG, "WPS: * Enrollee Nonce");
279 wpabuf_put_be16(msg, ATTR_ENROLLEE_NONCE);
280 wpabuf_put_be16(msg, WPS_NONCE_LEN);
281 wpabuf_put_data(msg, wps->nonce_e, WPS_NONCE_LEN);
286 int wps_build_registrar_nonce(struct wps_data *wps, struct wpabuf *msg)
288 wpa_printf(MSG_DEBUG, "WPS: * Registrar Nonce");
289 wpabuf_put_be16(msg, ATTR_REGISTRAR_NONCE);
290 wpabuf_put_be16(msg, WPS_NONCE_LEN);
291 wpabuf_put_data(msg, wps->nonce_r, WPS_NONCE_LEN);
296 int wps_build_auth_type_flags(struct wps_data *wps, struct wpabuf *msg)
298 u16 auth_types = WPS_AUTH_TYPES;
300 auth_types &= ~WPS_AUTH_SHARED;
301 #endif /* CONFIG_WPS2 */
302 wpa_printf(MSG_DEBUG, "WPS: * Authentication Type Flags");
303 wpabuf_put_be16(msg, ATTR_AUTH_TYPE_FLAGS);
304 wpabuf_put_be16(msg, 2);
305 wpabuf_put_be16(msg, auth_types);
310 int wps_build_encr_type_flags(struct wps_data *wps, struct wpabuf *msg)
312 u16 encr_types = WPS_ENCR_TYPES;
314 encr_types &= ~WPS_ENCR_WEP;
315 #endif /* CONFIG_WPS2 */
316 wpa_printf(MSG_DEBUG, "WPS: * Encryption Type Flags");
317 wpabuf_put_be16(msg, ATTR_ENCR_TYPE_FLAGS);
318 wpabuf_put_be16(msg, 2);
319 wpabuf_put_be16(msg, encr_types);
324 int wps_build_conn_type_flags(struct wps_data *wps, struct wpabuf *msg)
326 wpa_printf(MSG_DEBUG, "WPS: * Connection Type Flags");
327 wpabuf_put_be16(msg, ATTR_CONN_TYPE_FLAGS);
328 wpabuf_put_be16(msg, 1);
329 wpabuf_put_u8(msg, WPS_CONN_ESS);
334 int wps_build_assoc_state(struct wps_data *wps, struct wpabuf *msg)
336 wpa_printf(MSG_DEBUG, "WPS: * Association State");
337 wpabuf_put_be16(msg, ATTR_ASSOC_STATE);
338 wpabuf_put_be16(msg, 2);
339 wpabuf_put_be16(msg, WPS_ASSOC_NOT_ASSOC);
344 int wps_build_key_wrap_auth(struct wps_data *wps, struct wpabuf *msg)
346 u8 hash[SHA256_MAC_LEN];
348 wpa_printf(MSG_DEBUG, "WPS: * Key Wrap Authenticator");
349 hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, wpabuf_head(msg),
350 wpabuf_len(msg), hash);
352 wpabuf_put_be16(msg, ATTR_KEY_WRAP_AUTH);
353 wpabuf_put_be16(msg, WPS_KWA_LEN);
354 wpabuf_put_data(msg, hash, WPS_KWA_LEN);
359 int wps_build_encr_settings(struct wps_data *wps, struct wpabuf *msg,
360 struct wpabuf *plain)
363 const size_t block_size = 16;
366 wpa_printf(MSG_DEBUG, "WPS: * Encrypted Settings");
368 /* PKCS#5 v2.0 pad */
369 pad_len = block_size - wpabuf_len(plain) % block_size;
370 os_memset(wpabuf_put(plain, pad_len), pad_len, pad_len);
372 wpabuf_put_be16(msg, ATTR_ENCR_SETTINGS);
373 wpabuf_put_be16(msg, block_size + wpabuf_len(plain));
375 iv = wpabuf_put(msg, block_size);
376 if (random_get_bytes(iv, block_size) < 0)
379 data = wpabuf_put(msg, 0);
380 wpabuf_put_buf(msg, plain);
381 if (aes_128_cbc_encrypt(wps->keywrapkey, iv, data, wpabuf_len(plain)))
388 #ifdef CONFIG_WPS_OOB
389 int wps_build_oob_dev_pw(struct wpabuf *msg, u16 dev_pw_id,
390 const struct wpabuf *pubkey, const u8 *dev_pw,
395 u8 pubkey_hash[WPS_HASH_LEN];
397 wpa_printf(MSG_DEBUG, "WPS: * OOB Device Password (dev_pw_id=%u)",
399 addr[0] = wpabuf_head(pubkey);
400 hash_len = wpabuf_len(pubkey);
401 sha256_vector(1, addr, &hash_len, pubkey_hash);
402 #ifdef CONFIG_WPS_TESTING
403 if (wps_corrupt_pkhash) {
404 wpa_hexdump(MSG_DEBUG, "WPS: Real Public Key Hash",
405 pubkey_hash, WPS_OOB_PUBKEY_HASH_LEN);
406 wpa_printf(MSG_INFO, "WPS: Testing - corrupt public key hash");
407 pubkey_hash[WPS_OOB_PUBKEY_HASH_LEN - 2]++;
409 #endif /* CONFIG_WPS_TESTING */
411 wpabuf_put_be16(msg, ATTR_OOB_DEVICE_PASSWORD);
412 wpabuf_put_be16(msg, WPS_OOB_PUBKEY_HASH_LEN + 2 + dev_pw_len);
413 wpa_hexdump(MSG_DEBUG, "WPS: Public Key Hash",
414 pubkey_hash, WPS_OOB_PUBKEY_HASH_LEN);
415 wpabuf_put_data(msg, pubkey_hash, WPS_OOB_PUBKEY_HASH_LEN);
416 wpabuf_put_be16(msg, dev_pw_id);
418 wpa_hexdump_key(MSG_DEBUG, "WPS: OOB Device Password",
420 wpabuf_put_data(msg, dev_pw, dev_pw_len);
425 #endif /* CONFIG_WPS_OOB */
428 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
429 struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
434 ie = wpabuf_alloc(wpabuf_len(data) + 100);
440 pos = wpabuf_head(data);
441 end = pos + wpabuf_len(data);
444 size_t frag_len = end - pos;
447 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
448 wpabuf_put_u8(ie, 4 + frag_len);
449 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
450 wpabuf_put_data(ie, pos, frag_len);
460 int wps_build_mac_addr(struct wpabuf *msg, const u8 *addr)
462 wpa_printf(MSG_DEBUG, "WPS: * MAC Address (" MACSTR ")",
464 wpabuf_put_be16(msg, ATTR_MAC_ADDR);
465 wpabuf_put_be16(msg, ETH_ALEN);
466 wpabuf_put_data(msg, addr, ETH_ALEN);
471 int wps_build_rf_bands_attr(struct wpabuf *msg, u8 rf_bands)
473 wpa_printf(MSG_DEBUG, "WPS: * RF Bands (%x)", rf_bands);
474 wpabuf_put_be16(msg, ATTR_RF_BANDS);
475 wpabuf_put_be16(msg, 1);
476 wpabuf_put_u8(msg, rf_bands);
481 int wps_build_ap_channel(struct wpabuf *msg, u16 ap_channel)
483 wpa_printf(MSG_DEBUG, "WPS: * AP Channel (%u)", ap_channel);
484 wpabuf_put_be16(msg, ATTR_AP_CHANNEL);
485 wpabuf_put_be16(msg, 2);
486 wpabuf_put_be16(msg, ap_channel);