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