wps_attr_build.c revision 68d0e3ed07847339aedfac8e02f50db68c702e52
1/*
2 * Wi-Fi Protected Setup - attribute building
3 * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10
11#include "common.h"
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"
18#include "wps_i.h"
19
20
21int wps_build_public_key(struct wps_data *wps, struct wpabuf *msg)
22{
23	struct wpabuf *pubkey;
24
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 &&
29	    wps->wps->dh_ctx) {
30		wpa_printf(MSG_DEBUG, "WPS: Using pre-configured DH keys");
31		if (wps->wps->dh_pubkey == NULL) {
32			wpa_printf(MSG_DEBUG,
33				   "WPS: wps->wps->dh_pubkey == NULL");
34			return -1;
35		}
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);
40#ifdef CONFIG_WPS_NFC
41	} else if (wps->dev_pw_id >= 0x10 && wps->wps->ap &&
42		   wps->dev_pw_id == wps->wps->ap_nfc_dev_pw_id) {
43		wpa_printf(MSG_DEBUG, "WPS: Using NFC password token DH keys");
44		if (wps->wps->ap_nfc_dh_privkey == NULL) {
45			wpa_printf(MSG_DEBUG,
46				   "WPS: wps->wps->ap_nfc_dh_privkey == NULL");
47			return -1;
48		}
49		if (wps->wps->ap_nfc_dh_pubkey == NULL) {
50			wpa_printf(MSG_DEBUG,
51				   "WPS: wps->wps->ap_nfc_dh_pubkey == NULL");
52			return -1;
53		}
54		wps->dh_privkey = wpabuf_dup(wps->wps->ap_nfc_dh_privkey);
55		pubkey = wpabuf_dup(wps->wps->ap_nfc_dh_pubkey);
56		wps->dh_ctx = dh5_init_fixed(wps->dh_privkey, pubkey);
57#endif /* CONFIG_WPS_NFC */
58	} else {
59		wpa_printf(MSG_DEBUG, "WPS: Generate new DH keys");
60		dh5_free(wps->dh_ctx);
61		wps->dh_ctx = dh5_init(&wps->dh_privkey, &pubkey);
62		pubkey = wpabuf_zeropad(pubkey, 192);
63	}
64	if (wps->dh_ctx == NULL || wps->dh_privkey == NULL || pubkey == NULL) {
65		wpa_printf(MSG_DEBUG, "WPS: Failed to initialize "
66			   "Diffie-Hellman handshake");
67		wpabuf_free(pubkey);
68		return -1;
69	}
70	wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH Private Key", wps->dh_privkey);
71	wpa_hexdump_buf(MSG_DEBUG, "WPS: DH own Public Key", pubkey);
72
73	wpabuf_put_be16(msg, ATTR_PUBLIC_KEY);
74	wpabuf_put_be16(msg, wpabuf_len(pubkey));
75	wpabuf_put_buf(msg, pubkey);
76
77	if (wps->registrar) {
78		wpabuf_free(wps->dh_pubkey_r);
79		wps->dh_pubkey_r = pubkey;
80	} else {
81		wpabuf_free(wps->dh_pubkey_e);
82		wps->dh_pubkey_e = pubkey;
83	}
84
85	return 0;
86}
87
88
89int wps_build_req_type(struct wpabuf *msg, enum wps_request_type type)
90{
91	wpa_printf(MSG_DEBUG, "WPS:  * Request Type");
92	wpabuf_put_be16(msg, ATTR_REQUEST_TYPE);
93	wpabuf_put_be16(msg, 1);
94	wpabuf_put_u8(msg, type);
95	return 0;
96}
97
98
99int wps_build_resp_type(struct wpabuf *msg, enum wps_response_type type)
100{
101	wpa_printf(MSG_DEBUG, "WPS:  * Response Type (%d)", type);
102	wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
103	wpabuf_put_be16(msg, 1);
104	wpabuf_put_u8(msg, type);
105	return 0;
106}
107
108
109int wps_build_config_methods(struct wpabuf *msg, u16 methods)
110{
111	wpa_printf(MSG_DEBUG, "WPS:  * Config Methods (%x)", methods);
112	wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
113	wpabuf_put_be16(msg, 2);
114	wpabuf_put_be16(msg, methods);
115	return 0;
116}
117
118
119int wps_build_uuid_e(struct wpabuf *msg, const u8 *uuid)
120{
121	if (wpabuf_tailroom(msg) < 4 + WPS_UUID_LEN)
122		return -1;
123	wpa_printf(MSG_DEBUG, "WPS:  * UUID-E");
124	wpabuf_put_be16(msg, ATTR_UUID_E);
125	wpabuf_put_be16(msg, WPS_UUID_LEN);
126	wpabuf_put_data(msg, uuid, WPS_UUID_LEN);
127	return 0;
128}
129
130
131int wps_build_dev_password_id(struct wpabuf *msg, u16 id)
132{
133	wpa_printf(MSG_DEBUG, "WPS:  * Device Password ID (%d)", id);
134	wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
135	wpabuf_put_be16(msg, 2);
136	wpabuf_put_be16(msg, id);
137	return 0;
138}
139
140
141int wps_build_config_error(struct wpabuf *msg, u16 err)
142{
143	wpa_printf(MSG_DEBUG, "WPS:  * Configuration Error (%d)", err);
144	wpabuf_put_be16(msg, ATTR_CONFIG_ERROR);
145	wpabuf_put_be16(msg, 2);
146	wpabuf_put_be16(msg, err);
147	return 0;
148}
149
150
151int wps_build_authenticator(struct wps_data *wps, struct wpabuf *msg)
152{
153	u8 hash[SHA256_MAC_LEN];
154	const u8 *addr[2];
155	size_t len[2];
156
157	if (wps->last_msg == NULL) {
158		wpa_printf(MSG_DEBUG, "WPS: Last message not available for "
159			   "building authenticator");
160		return -1;
161	}
162
163	/* Authenticator = HMAC-SHA256_AuthKey(M_prev || M_curr*)
164	 * (M_curr* is M_curr without the Authenticator attribute)
165	 */
166	addr[0] = wpabuf_head(wps->last_msg);
167	len[0] = wpabuf_len(wps->last_msg);
168	addr[1] = wpabuf_head(msg);
169	len[1] = wpabuf_len(msg);
170	hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 2, addr, len, hash);
171
172	wpa_printf(MSG_DEBUG, "WPS:  * Authenticator");
173	wpabuf_put_be16(msg, ATTR_AUTHENTICATOR);
174	wpabuf_put_be16(msg, WPS_AUTHENTICATOR_LEN);
175	wpabuf_put_data(msg, hash, WPS_AUTHENTICATOR_LEN);
176
177	return 0;
178}
179
180
181int wps_build_version(struct wpabuf *msg)
182{
183	/*
184	 * Note: This attribute is deprecated and set to hardcoded 0x10 for
185	 * backwards compatibility reasons. The real version negotiation is
186	 * done with Version2.
187	 */
188	if (wpabuf_tailroom(msg) < 5)
189		return -1;
190	wpa_printf(MSG_DEBUG, "WPS:  * Version (hardcoded 0x10)");
191	wpabuf_put_be16(msg, ATTR_VERSION);
192	wpabuf_put_be16(msg, 1);
193	wpabuf_put_u8(msg, 0x10);
194	return 0;
195}
196
197
198int wps_build_wfa_ext(struct wpabuf *msg, int req_to_enroll,
199		      const u8 *auth_macs, size_t auth_macs_count)
200{
201#ifdef CONFIG_WPS2
202	u8 *len;
203
204	if (wpabuf_tailroom(msg) <
205	    7 + 3 + (req_to_enroll ? 3 : 0) +
206	    (auth_macs ? 2 + auth_macs_count * ETH_ALEN : 0))
207		return -1;
208	wpabuf_put_be16(msg, ATTR_VENDOR_EXT);
209	len = wpabuf_put(msg, 2); /* to be filled */
210	wpabuf_put_be24(msg, WPS_VENDOR_ID_WFA);
211
212	wpa_printf(MSG_DEBUG, "WPS:  * Version2 (0x%x)", WPS_VERSION);
213	wpabuf_put_u8(msg, WFA_ELEM_VERSION2);
214	wpabuf_put_u8(msg, 1);
215	wpabuf_put_u8(msg, WPS_VERSION);
216
217	if (req_to_enroll) {
218		wpa_printf(MSG_DEBUG, "WPS:  * Request to Enroll (1)");
219		wpabuf_put_u8(msg, WFA_ELEM_REQUEST_TO_ENROLL);
220		wpabuf_put_u8(msg, 1);
221		wpabuf_put_u8(msg, 1);
222	}
223
224	if (auth_macs && auth_macs_count) {
225		size_t i;
226		wpa_printf(MSG_DEBUG, "WPS:  * AuthorizedMACs (count=%d)",
227			   (int) auth_macs_count);
228		wpabuf_put_u8(msg, WFA_ELEM_AUTHORIZEDMACS);
229		wpabuf_put_u8(msg, auth_macs_count * ETH_ALEN);
230		wpabuf_put_data(msg, auth_macs, auth_macs_count * ETH_ALEN);
231		for (i = 0; i < auth_macs_count; i++)
232			wpa_printf(MSG_DEBUG, "WPS:    AuthorizedMAC: " MACSTR,
233				   MAC2STR(&auth_macs[i * ETH_ALEN]));
234	}
235
236	WPA_PUT_BE16(len, (u8 *) wpabuf_put(msg, 0) - len - 2);
237#endif /* CONFIG_WPS2 */
238
239#ifdef CONFIG_WPS_TESTING
240	if (WPS_VERSION > 0x20) {
241		if (wpabuf_tailroom(msg) < 5)
242			return -1;
243		wpa_printf(MSG_DEBUG, "WPS:  * Extensibility Testing - extra "
244			   "attribute");
245		wpabuf_put_be16(msg, ATTR_EXTENSIBILITY_TEST);
246		wpabuf_put_be16(msg, 1);
247		wpabuf_put_u8(msg, 42);
248	}
249#endif /* CONFIG_WPS_TESTING */
250	return 0;
251}
252
253
254int wps_build_msg_type(struct wpabuf *msg, enum wps_msg_type msg_type)
255{
256	wpa_printf(MSG_DEBUG, "WPS:  * Message Type (%d)", msg_type);
257	wpabuf_put_be16(msg, ATTR_MSG_TYPE);
258	wpabuf_put_be16(msg, 1);
259	wpabuf_put_u8(msg, msg_type);
260	return 0;
261}
262
263
264int wps_build_enrollee_nonce(struct wps_data *wps, struct wpabuf *msg)
265{
266	wpa_printf(MSG_DEBUG, "WPS:  * Enrollee Nonce");
267	wpabuf_put_be16(msg, ATTR_ENROLLEE_NONCE);
268	wpabuf_put_be16(msg, WPS_NONCE_LEN);
269	wpabuf_put_data(msg, wps->nonce_e, WPS_NONCE_LEN);
270	return 0;
271}
272
273
274int wps_build_registrar_nonce(struct wps_data *wps, struct wpabuf *msg)
275{
276	wpa_printf(MSG_DEBUG, "WPS:  * Registrar Nonce");
277	wpabuf_put_be16(msg, ATTR_REGISTRAR_NONCE);
278	wpabuf_put_be16(msg, WPS_NONCE_LEN);
279	wpabuf_put_data(msg, wps->nonce_r, WPS_NONCE_LEN);
280	return 0;
281}
282
283
284int wps_build_auth_type_flags(struct wps_data *wps, struct wpabuf *msg)
285{
286	u16 auth_types = WPS_AUTH_TYPES;
287#ifdef CONFIG_WPS2
288	auth_types &= ~WPS_AUTH_SHARED;
289#endif /* CONFIG_WPS2 */
290	wpa_printf(MSG_DEBUG, "WPS:  * Authentication Type Flags");
291	wpabuf_put_be16(msg, ATTR_AUTH_TYPE_FLAGS);
292	wpabuf_put_be16(msg, 2);
293	wpabuf_put_be16(msg, auth_types);
294	return 0;
295}
296
297
298int wps_build_encr_type_flags(struct wps_data *wps, struct wpabuf *msg)
299{
300	u16 encr_types = WPS_ENCR_TYPES;
301#ifdef CONFIG_WPS2
302	encr_types &= ~WPS_ENCR_WEP;
303#endif /* CONFIG_WPS2 */
304	wpa_printf(MSG_DEBUG, "WPS:  * Encryption Type Flags");
305	wpabuf_put_be16(msg, ATTR_ENCR_TYPE_FLAGS);
306	wpabuf_put_be16(msg, 2);
307	wpabuf_put_be16(msg, encr_types);
308	return 0;
309}
310
311
312int wps_build_conn_type_flags(struct wps_data *wps, struct wpabuf *msg)
313{
314	wpa_printf(MSG_DEBUG, "WPS:  * Connection Type Flags");
315	wpabuf_put_be16(msg, ATTR_CONN_TYPE_FLAGS);
316	wpabuf_put_be16(msg, 1);
317	wpabuf_put_u8(msg, WPS_CONN_ESS);
318	return 0;
319}
320
321
322int wps_build_assoc_state(struct wps_data *wps, struct wpabuf *msg)
323{
324	wpa_printf(MSG_DEBUG, "WPS:  * Association State");
325	wpabuf_put_be16(msg, ATTR_ASSOC_STATE);
326	wpabuf_put_be16(msg, 2);
327	wpabuf_put_be16(msg, WPS_ASSOC_NOT_ASSOC);
328	return 0;
329}
330
331
332int wps_build_key_wrap_auth(struct wps_data *wps, struct wpabuf *msg)
333{
334	u8 hash[SHA256_MAC_LEN];
335
336	wpa_printf(MSG_DEBUG, "WPS:  * Key Wrap Authenticator");
337	hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, wpabuf_head(msg),
338		    wpabuf_len(msg), hash);
339
340	wpabuf_put_be16(msg, ATTR_KEY_WRAP_AUTH);
341	wpabuf_put_be16(msg, WPS_KWA_LEN);
342	wpabuf_put_data(msg, hash, WPS_KWA_LEN);
343	return 0;
344}
345
346
347int wps_build_encr_settings(struct wps_data *wps, struct wpabuf *msg,
348			    struct wpabuf *plain)
349{
350	size_t pad_len;
351	const size_t block_size = 16;
352	u8 *iv, *data;
353
354	wpa_printf(MSG_DEBUG, "WPS:  * Encrypted Settings");
355
356	/* PKCS#5 v2.0 pad */
357	pad_len = block_size - wpabuf_len(plain) % block_size;
358	os_memset(wpabuf_put(plain, pad_len), pad_len, pad_len);
359
360	wpabuf_put_be16(msg, ATTR_ENCR_SETTINGS);
361	wpabuf_put_be16(msg, block_size + wpabuf_len(plain));
362
363	iv = wpabuf_put(msg, block_size);
364	if (random_get_bytes(iv, block_size) < 0)
365		return -1;
366
367	data = wpabuf_put(msg, 0);
368	wpabuf_put_buf(msg, plain);
369	if (aes_128_cbc_encrypt(wps->keywrapkey, iv, data, wpabuf_len(plain)))
370		return -1;
371
372	return 0;
373}
374
375
376#ifdef CONFIG_WPS_OOB
377int wps_build_oob_dev_pw(struct wpabuf *msg, u16 dev_pw_id,
378			 const struct wpabuf *pubkey, const u8 *dev_pw,
379			 size_t dev_pw_len)
380{
381	size_t hash_len;
382	const u8 *addr[1];
383	u8 pubkey_hash[WPS_HASH_LEN];
384
385	addr[0] = wpabuf_head(pubkey);
386	hash_len = wpabuf_len(pubkey);
387	sha256_vector(1, addr, &hash_len, pubkey_hash);
388
389	wpabuf_put_be16(msg, ATTR_OOB_DEVICE_PASSWORD);
390	wpabuf_put_be16(msg, WPS_OOB_PUBKEY_HASH_LEN + 2 + dev_pw_len);
391	wpabuf_put_data(msg, pubkey_hash, WPS_OOB_PUBKEY_HASH_LEN);
392	wpabuf_put_be16(msg, dev_pw_id);
393	wpabuf_put_data(msg, dev_pw, dev_pw_len);
394
395	return 0;
396}
397#endif /* CONFIG_WPS_OOB */
398
399
400/* Encapsulate WPS IE data with one (or more, if needed) IE headers */
401struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
402{
403	struct wpabuf *ie;
404	const u8 *pos, *end;
405
406	ie = wpabuf_alloc(wpabuf_len(data) + 100);
407	if (ie == NULL) {
408		wpabuf_free(data);
409		return NULL;
410	}
411
412	pos = wpabuf_head(data);
413	end = pos + wpabuf_len(data);
414
415	while (end > pos) {
416		size_t frag_len = end - pos;
417		if (frag_len > 251)
418			frag_len = 251;
419		wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
420		wpabuf_put_u8(ie, 4 + frag_len);
421		wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
422		wpabuf_put_data(ie, pos, frag_len);
423		pos += frag_len;
424	}
425
426	wpabuf_free(data);
427
428	return ie;
429}
430
431
432int wps_build_mac_addr(struct wpabuf *msg, const u8 *addr)
433{
434	wpa_printf(MSG_DEBUG, "WPS:  * MAC Address (" MACSTR ")",
435		   MAC2STR(addr));
436	wpabuf_put_be16(msg, ATTR_MAC_ADDR);
437	wpabuf_put_be16(msg, ETH_ALEN);
438	wpabuf_put_data(msg, addr, ETH_ALEN);
439	return 0;
440}
441