wps_attr_build.c revision fb79edc9df1f20461e90e478363d207348213d35
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#ifdef CONFIG_WPS_TESTING
205	if (WPS_VERSION == 0x10)
206		return 0;
207#endif /* CONFIG_WPS_TESTING */
208
209	if (wpabuf_tailroom(msg) <
210	    7 + 3 + (req_to_enroll ? 3 : 0) +
211	    (auth_macs ? 2 + auth_macs_count * ETH_ALEN : 0))
212		return -1;
213	wpabuf_put_be16(msg, ATTR_VENDOR_EXT);
214	len = wpabuf_put(msg, 2); /* to be filled */
215	wpabuf_put_be24(msg, WPS_VENDOR_ID_WFA);
216
217	wpa_printf(MSG_DEBUG, "WPS:  * Version2 (0x%x)", WPS_VERSION);
218	wpabuf_put_u8(msg, WFA_ELEM_VERSION2);
219	wpabuf_put_u8(msg, 1);
220	wpabuf_put_u8(msg, WPS_VERSION);
221
222	if (req_to_enroll) {
223		wpa_printf(MSG_DEBUG, "WPS:  * Request to Enroll (1)");
224		wpabuf_put_u8(msg, WFA_ELEM_REQUEST_TO_ENROLL);
225		wpabuf_put_u8(msg, 1);
226		wpabuf_put_u8(msg, 1);
227	}
228
229	if (auth_macs && auth_macs_count) {
230		size_t i;
231		wpa_printf(MSG_DEBUG, "WPS:  * AuthorizedMACs (count=%d)",
232			   (int) auth_macs_count);
233		wpabuf_put_u8(msg, WFA_ELEM_AUTHORIZEDMACS);
234		wpabuf_put_u8(msg, auth_macs_count * ETH_ALEN);
235		wpabuf_put_data(msg, auth_macs, auth_macs_count * ETH_ALEN);
236		for (i = 0; i < auth_macs_count; i++)
237			wpa_printf(MSG_DEBUG, "WPS:    AuthorizedMAC: " MACSTR,
238				   MAC2STR(&auth_macs[i * ETH_ALEN]));
239	}
240
241	WPA_PUT_BE16(len, (u8 *) wpabuf_put(msg, 0) - len - 2);
242#endif /* CONFIG_WPS2 */
243
244#ifdef CONFIG_WPS_TESTING
245	if (WPS_VERSION > 0x20) {
246		if (wpabuf_tailroom(msg) < 5)
247			return -1;
248		wpa_printf(MSG_DEBUG, "WPS:  * Extensibility Testing - extra "
249			   "attribute");
250		wpabuf_put_be16(msg, ATTR_EXTENSIBILITY_TEST);
251		wpabuf_put_be16(msg, 1);
252		wpabuf_put_u8(msg, 42);
253	}
254#endif /* CONFIG_WPS_TESTING */
255	return 0;
256}
257
258
259int wps_build_msg_type(struct wpabuf *msg, enum wps_msg_type msg_type)
260{
261	wpa_printf(MSG_DEBUG, "WPS:  * Message Type (%d)", msg_type);
262	wpabuf_put_be16(msg, ATTR_MSG_TYPE);
263	wpabuf_put_be16(msg, 1);
264	wpabuf_put_u8(msg, msg_type);
265	return 0;
266}
267
268
269int wps_build_enrollee_nonce(struct wps_data *wps, struct wpabuf *msg)
270{
271	wpa_printf(MSG_DEBUG, "WPS:  * Enrollee Nonce");
272	wpabuf_put_be16(msg, ATTR_ENROLLEE_NONCE);
273	wpabuf_put_be16(msg, WPS_NONCE_LEN);
274	wpabuf_put_data(msg, wps->nonce_e, WPS_NONCE_LEN);
275	return 0;
276}
277
278
279int wps_build_registrar_nonce(struct wps_data *wps, struct wpabuf *msg)
280{
281	wpa_printf(MSG_DEBUG, "WPS:  * Registrar Nonce");
282	wpabuf_put_be16(msg, ATTR_REGISTRAR_NONCE);
283	wpabuf_put_be16(msg, WPS_NONCE_LEN);
284	wpabuf_put_data(msg, wps->nonce_r, WPS_NONCE_LEN);
285	return 0;
286}
287
288
289int wps_build_auth_type_flags(struct wps_data *wps, struct wpabuf *msg)
290{
291	u16 auth_types = WPS_AUTH_TYPES;
292#ifdef CONFIG_WPS2
293	auth_types &= ~WPS_AUTH_SHARED;
294#endif /* CONFIG_WPS2 */
295	wpa_printf(MSG_DEBUG, "WPS:  * Authentication Type Flags");
296	wpabuf_put_be16(msg, ATTR_AUTH_TYPE_FLAGS);
297	wpabuf_put_be16(msg, 2);
298	wpabuf_put_be16(msg, auth_types);
299	return 0;
300}
301
302
303int wps_build_encr_type_flags(struct wps_data *wps, struct wpabuf *msg)
304{
305	u16 encr_types = WPS_ENCR_TYPES;
306#ifdef CONFIG_WPS2
307	encr_types &= ~WPS_ENCR_WEP;
308#endif /* CONFIG_WPS2 */
309	wpa_printf(MSG_DEBUG, "WPS:  * Encryption Type Flags");
310	wpabuf_put_be16(msg, ATTR_ENCR_TYPE_FLAGS);
311	wpabuf_put_be16(msg, 2);
312	wpabuf_put_be16(msg, encr_types);
313	return 0;
314}
315
316
317int wps_build_conn_type_flags(struct wps_data *wps, struct wpabuf *msg)
318{
319	wpa_printf(MSG_DEBUG, "WPS:  * Connection Type Flags");
320	wpabuf_put_be16(msg, ATTR_CONN_TYPE_FLAGS);
321	wpabuf_put_be16(msg, 1);
322	wpabuf_put_u8(msg, WPS_CONN_ESS);
323	return 0;
324}
325
326
327int wps_build_assoc_state(struct wps_data *wps, struct wpabuf *msg)
328{
329	wpa_printf(MSG_DEBUG, "WPS:  * Association State");
330	wpabuf_put_be16(msg, ATTR_ASSOC_STATE);
331	wpabuf_put_be16(msg, 2);
332	wpabuf_put_be16(msg, WPS_ASSOC_NOT_ASSOC);
333	return 0;
334}
335
336
337int wps_build_key_wrap_auth(struct wps_data *wps, struct wpabuf *msg)
338{
339	u8 hash[SHA256_MAC_LEN];
340
341	wpa_printf(MSG_DEBUG, "WPS:  * Key Wrap Authenticator");
342	hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, wpabuf_head(msg),
343		    wpabuf_len(msg), hash);
344
345	wpabuf_put_be16(msg, ATTR_KEY_WRAP_AUTH);
346	wpabuf_put_be16(msg, WPS_KWA_LEN);
347	wpabuf_put_data(msg, hash, WPS_KWA_LEN);
348	return 0;
349}
350
351
352int wps_build_encr_settings(struct wps_data *wps, struct wpabuf *msg,
353			    struct wpabuf *plain)
354{
355	size_t pad_len;
356	const size_t block_size = 16;
357	u8 *iv, *data;
358
359	wpa_printf(MSG_DEBUG, "WPS:  * Encrypted Settings");
360
361	/* PKCS#5 v2.0 pad */
362	pad_len = block_size - wpabuf_len(plain) % block_size;
363	os_memset(wpabuf_put(plain, pad_len), pad_len, pad_len);
364
365	wpabuf_put_be16(msg, ATTR_ENCR_SETTINGS);
366	wpabuf_put_be16(msg, block_size + wpabuf_len(plain));
367
368	iv = wpabuf_put(msg, block_size);
369	if (random_get_bytes(iv, block_size) < 0)
370		return -1;
371
372	data = wpabuf_put(msg, 0);
373	wpabuf_put_buf(msg, plain);
374	if (aes_128_cbc_encrypt(wps->keywrapkey, iv, data, wpabuf_len(plain)))
375		return -1;
376
377	return 0;
378}
379
380
381#ifdef CONFIG_WPS_OOB
382int wps_build_oob_dev_pw(struct wpabuf *msg, u16 dev_pw_id,
383			 const struct wpabuf *pubkey, const u8 *dev_pw,
384			 size_t dev_pw_len)
385{
386	size_t hash_len;
387	const u8 *addr[1];
388	u8 pubkey_hash[WPS_HASH_LEN];
389
390	wpa_printf(MSG_DEBUG, "WPS:  * OOB Device Password (dev_pw_id=%u)",
391		   dev_pw_id);
392	addr[0] = wpabuf_head(pubkey);
393	hash_len = wpabuf_len(pubkey);
394	sha256_vector(1, addr, &hash_len, pubkey_hash);
395
396	wpabuf_put_be16(msg, ATTR_OOB_DEVICE_PASSWORD);
397	wpabuf_put_be16(msg, WPS_OOB_PUBKEY_HASH_LEN + 2 + dev_pw_len);
398	wpa_hexdump(MSG_DEBUG, "WPS: Public Key Hash",
399		    pubkey_hash, WPS_OOB_PUBKEY_HASH_LEN);
400	wpabuf_put_data(msg, pubkey_hash, WPS_OOB_PUBKEY_HASH_LEN);
401	wpabuf_put_be16(msg, dev_pw_id);
402	wpa_hexdump_key(MSG_DEBUG, "WPS: OOB Device Password",
403			dev_pw, dev_pw_len);
404	wpabuf_put_data(msg, dev_pw, dev_pw_len);
405
406	return 0;
407}
408#endif /* CONFIG_WPS_OOB */
409
410
411/* Encapsulate WPS IE data with one (or more, if needed) IE headers */
412struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
413{
414	struct wpabuf *ie;
415	const u8 *pos, *end;
416
417	ie = wpabuf_alloc(wpabuf_len(data) + 100);
418	if (ie == NULL) {
419		wpabuf_free(data);
420		return NULL;
421	}
422
423	pos = wpabuf_head(data);
424	end = pos + wpabuf_len(data);
425
426	while (end > pos) {
427		size_t frag_len = end - pos;
428		if (frag_len > 251)
429			frag_len = 251;
430		wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
431		wpabuf_put_u8(ie, 4 + frag_len);
432		wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
433		wpabuf_put_data(ie, pos, frag_len);
434		pos += frag_len;
435	}
436
437	wpabuf_free(data);
438
439	return ie;
440}
441
442
443int wps_build_mac_addr(struct wpabuf *msg, const u8 *addr)
444{
445	wpa_printf(MSG_DEBUG, "WPS:  * MAC Address (" MACSTR ")",
446		   MAC2STR(addr));
447	wpabuf_put_be16(msg, ATTR_MAC_ADDR);
448	wpabuf_put_be16(msg, ETH_ALEN);
449	wpabuf_put_data(msg, addr, ETH_ALEN);
450	return 0;
451}
452