• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Wi-Fi Protected Setup - common functionality
3  * Copyright (c) 2008-2012, 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 "common/defs.h"
13 #include "common/ieee802_11_common.h"
14 #include "crypto/aes_wrap.h"
15 #include "crypto/crypto.h"
16 #include "crypto/dh_group5.h"
17 #include "crypto/sha1.h"
18 #include "crypto/sha256.h"
19 #include "crypto/random.h"
20 #include "wps_i.h"
21 #include "wps_dev_attr.h"
22 
23 
wps_kdf(const u8 * key,const u8 * label_prefix,size_t label_prefix_len,const char * label,u8 * res,size_t res_len)24 void wps_kdf(const u8 *key, const u8 *label_prefix, size_t label_prefix_len,
25 	     const char *label, u8 *res, size_t res_len)
26 {
27 	u8 i_buf[4], key_bits[4];
28 	const u8 *addr[4];
29 	size_t len[4];
30 	int i, iter;
31 	u8 hash[SHA256_MAC_LEN], *opos;
32 	size_t left;
33 
34 	WPA_PUT_BE32(key_bits, res_len * 8);
35 
36 	addr[0] = i_buf;
37 	len[0] = sizeof(i_buf);
38 	addr[1] = label_prefix;
39 	len[1] = label_prefix_len;
40 	addr[2] = (const u8 *) label;
41 	len[2] = os_strlen(label);
42 	addr[3] = key_bits;
43 	len[3] = sizeof(key_bits);
44 
45 	iter = (res_len + SHA256_MAC_LEN - 1) / SHA256_MAC_LEN;
46 	opos = res;
47 	left = res_len;
48 
49 	for (i = 1; i <= iter; i++) {
50 		WPA_PUT_BE32(i_buf, i);
51 		hmac_sha256_vector(key, SHA256_MAC_LEN, 4, addr, len, hash);
52 		if (i < iter) {
53 			os_memcpy(opos, hash, SHA256_MAC_LEN);
54 			opos += SHA256_MAC_LEN;
55 			left -= SHA256_MAC_LEN;
56 		} else
57 			os_memcpy(opos, hash, left);
58 	}
59 }
60 
61 
wps_derive_keys(struct wps_data * wps)62 int wps_derive_keys(struct wps_data *wps)
63 {
64 	struct wpabuf *pubkey, *dh_shared;
65 	u8 dhkey[SHA256_MAC_LEN], kdk[SHA256_MAC_LEN];
66 	const u8 *addr[3];
67 	size_t len[3];
68 	u8 keys[WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN + WPS_EMSK_LEN];
69 
70 	if (wps->dh_privkey == NULL) {
71 		wpa_printf(MSG_DEBUG, "WPS: Own DH private key not available");
72 		return -1;
73 	}
74 
75 	pubkey = wps->registrar ? wps->dh_pubkey_e : wps->dh_pubkey_r;
76 	if (pubkey == NULL) {
77 		wpa_printf(MSG_DEBUG, "WPS: Peer DH public key not available");
78 		return -1;
79 	}
80 
81 	wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH Private Key", wps->dh_privkey);
82 	wpa_hexdump_buf(MSG_DEBUG, "WPS: DH peer Public Key", pubkey);
83 	dh_shared = dh5_derive_shared(wps->dh_ctx, pubkey, wps->dh_privkey);
84 	dh5_free(wps->dh_ctx);
85 	wps->dh_ctx = NULL;
86 	dh_shared = wpabuf_zeropad(dh_shared, 192);
87 	if (dh_shared == NULL) {
88 		wpa_printf(MSG_DEBUG, "WPS: Failed to derive DH shared key");
89 		return -1;
90 	}
91 
92 	/* Own DH private key is not needed anymore */
93 	wpabuf_free(wps->dh_privkey);
94 	wps->dh_privkey = NULL;
95 
96 	wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH shared key", dh_shared);
97 
98 	/* DHKey = SHA-256(g^AB mod p) */
99 	addr[0] = wpabuf_head(dh_shared);
100 	len[0] = wpabuf_len(dh_shared);
101 	sha256_vector(1, addr, len, dhkey);
102 	wpa_hexdump_key(MSG_DEBUG, "WPS: DHKey", dhkey, sizeof(dhkey));
103 	wpabuf_free(dh_shared);
104 
105 	/* KDK = HMAC-SHA-256_DHKey(N1 || EnrolleeMAC || N2) */
106 	addr[0] = wps->nonce_e;
107 	len[0] = WPS_NONCE_LEN;
108 	addr[1] = wps->mac_addr_e;
109 	len[1] = ETH_ALEN;
110 	addr[2] = wps->nonce_r;
111 	len[2] = WPS_NONCE_LEN;
112 	hmac_sha256_vector(dhkey, sizeof(dhkey), 3, addr, len, kdk);
113 	wpa_hexdump_key(MSG_DEBUG, "WPS: KDK", kdk, sizeof(kdk));
114 
115 	wps_kdf(kdk, NULL, 0, "Wi-Fi Easy and Secure Key Derivation",
116 		keys, sizeof(keys));
117 	os_memcpy(wps->authkey, keys, WPS_AUTHKEY_LEN);
118 	os_memcpy(wps->keywrapkey, keys + WPS_AUTHKEY_LEN, WPS_KEYWRAPKEY_LEN);
119 	os_memcpy(wps->emsk, keys + WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN,
120 		  WPS_EMSK_LEN);
121 
122 	wpa_hexdump_key(MSG_DEBUG, "WPS: AuthKey",
123 			wps->authkey, WPS_AUTHKEY_LEN);
124 	wpa_hexdump_key(MSG_DEBUG, "WPS: KeyWrapKey",
125 			wps->keywrapkey, WPS_KEYWRAPKEY_LEN);
126 	wpa_hexdump_key(MSG_DEBUG, "WPS: EMSK", wps->emsk, WPS_EMSK_LEN);
127 
128 	return 0;
129 }
130 
131 
wps_derive_psk(struct wps_data * wps,const u8 * dev_passwd,size_t dev_passwd_len)132 void wps_derive_psk(struct wps_data *wps, const u8 *dev_passwd,
133 		    size_t dev_passwd_len)
134 {
135 	u8 hash[SHA256_MAC_LEN];
136 
137 	hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, dev_passwd,
138 		    (dev_passwd_len + 1) / 2, hash);
139 	os_memcpy(wps->psk1, hash, WPS_PSK_LEN);
140 	hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN,
141 		    dev_passwd + (dev_passwd_len + 1) / 2,
142 		    dev_passwd_len / 2, hash);
143 	os_memcpy(wps->psk2, hash, WPS_PSK_LEN);
144 
145 	wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Device Password",
146 			      dev_passwd, dev_passwd_len);
147 	wpa_hexdump_key(MSG_DEBUG, "WPS: PSK1", wps->psk1, WPS_PSK_LEN);
148 	wpa_hexdump_key(MSG_DEBUG, "WPS: PSK2", wps->psk2, WPS_PSK_LEN);
149 }
150 
151 
wps_decrypt_encr_settings(struct wps_data * wps,const u8 * encr,size_t encr_len)152 struct wpabuf * wps_decrypt_encr_settings(struct wps_data *wps, const u8 *encr,
153 					  size_t encr_len)
154 {
155 	struct wpabuf *decrypted;
156 	const size_t block_size = 16;
157 	size_t i;
158 	u8 pad;
159 	const u8 *pos;
160 
161 	/* AES-128-CBC */
162 	if (encr == NULL || encr_len < 2 * block_size || encr_len % block_size)
163 	{
164 		wpa_printf(MSG_DEBUG, "WPS: No Encrypted Settings received");
165 		return NULL;
166 	}
167 
168 	decrypted = wpabuf_alloc(encr_len - block_size);
169 	if (decrypted == NULL)
170 		return NULL;
171 
172 	wpa_hexdump(MSG_MSGDUMP, "WPS: Encrypted Settings", encr, encr_len);
173 	wpabuf_put_data(decrypted, encr + block_size, encr_len - block_size);
174 	if (aes_128_cbc_decrypt(wps->keywrapkey, encr, wpabuf_mhead(decrypted),
175 				wpabuf_len(decrypted))) {
176 		wpabuf_free(decrypted);
177 		return NULL;
178 	}
179 
180 	wpa_hexdump_buf_key(MSG_MSGDUMP, "WPS: Decrypted Encrypted Settings",
181 			    decrypted);
182 
183 	pos = wpabuf_head_u8(decrypted) + wpabuf_len(decrypted) - 1;
184 	pad = *pos;
185 	if (pad > wpabuf_len(decrypted)) {
186 		wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad value");
187 		wpabuf_free(decrypted);
188 		return NULL;
189 	}
190 	for (i = 0; i < pad; i++) {
191 		if (*pos-- != pad) {
192 			wpa_printf(MSG_DEBUG, "WPS: Invalid PKCS#5 v2.0 pad "
193 				   "string");
194 			wpabuf_free(decrypted);
195 			return NULL;
196 		}
197 	}
198 	decrypted->used -= pad;
199 
200 	return decrypted;
201 }
202 
203 
204 /**
205  * wps_pin_checksum - Compute PIN checksum
206  * @pin: Seven digit PIN (i.e., eight digit PIN without the checksum digit)
207  * Returns: Checksum digit
208  */
wps_pin_checksum(unsigned int pin)209 unsigned int wps_pin_checksum(unsigned int pin)
210 {
211 	unsigned int accum = 0;
212 	while (pin) {
213 		accum += 3 * (pin % 10);
214 		pin /= 10;
215 		accum += pin % 10;
216 		pin /= 10;
217 	}
218 
219 	return (10 - accum % 10) % 10;
220 }
221 
222 
223 /**
224  * wps_pin_valid - Check whether a PIN has a valid checksum
225  * @pin: Eight digit PIN (i.e., including the checksum digit)
226  * Returns: 1 if checksum digit is valid, or 0 if not
227  */
wps_pin_valid(unsigned int pin)228 unsigned int wps_pin_valid(unsigned int pin)
229 {
230 	return wps_pin_checksum(pin / 10) == (pin % 10);
231 }
232 
233 
234 /**
235  * wps_generate_pin - Generate a random PIN
236  * Returns: Eight digit PIN (i.e., including the checksum digit)
237  */
wps_generate_pin(unsigned int * pin)238 int wps_generate_pin(unsigned int *pin)
239 {
240 	unsigned int val;
241 
242 	/* Generate seven random digits for the PIN */
243 	if (random_get_bytes((unsigned char *) &val, sizeof(val)) < 0)
244 		return -1;
245 	val %= 10000000;
246 
247 	/* Append checksum digit */
248 	*pin = val * 10 + wps_pin_checksum(val);
249 	return 0;
250 }
251 
252 
wps_pin_str_valid(const char * pin)253 int wps_pin_str_valid(const char *pin)
254 {
255 	const char *p;
256 	size_t len;
257 
258 	p = pin;
259 	while (*p >= '0' && *p <= '9')
260 		p++;
261 	if (*p != '\0')
262 		return 0;
263 
264 	len = p - pin;
265 	return len == 4 || len == 8;
266 }
267 
268 
wps_fail_event(struct wps_context * wps,enum wps_msg_type msg,u16 config_error,u16 error_indication,const u8 * mac_addr)269 void wps_fail_event(struct wps_context *wps, enum wps_msg_type msg,
270 		    u16 config_error, u16 error_indication, const u8 *mac_addr)
271 {
272 	union wps_event_data data;
273 
274 	if (wps->event_cb == NULL)
275 		return;
276 
277 	os_memset(&data, 0, sizeof(data));
278 	data.fail.msg = msg;
279 	data.fail.config_error = config_error;
280 	data.fail.error_indication = error_indication;
281 	os_memcpy(data.fail.peer_macaddr, mac_addr, ETH_ALEN);
282 	wps->event_cb(wps->cb_ctx, WPS_EV_FAIL, &data);
283 }
284 
285 
wps_success_event(struct wps_context * wps,const u8 * mac_addr)286 void wps_success_event(struct wps_context *wps, const u8 *mac_addr)
287 {
288 	union wps_event_data data;
289 
290 	if (wps->event_cb == NULL)
291 		return;
292 
293 	os_memset(&data, 0, sizeof(data));
294 	os_memcpy(data.success.peer_macaddr, mac_addr, ETH_ALEN);
295 	wps->event_cb(wps->cb_ctx, WPS_EV_SUCCESS, &data);
296 }
297 
298 
wps_pwd_auth_fail_event(struct wps_context * wps,int enrollee,int part,const u8 * mac_addr)299 void wps_pwd_auth_fail_event(struct wps_context *wps, int enrollee, int part,
300 			     const u8 *mac_addr)
301 {
302 	union wps_event_data data;
303 
304 	if (wps->event_cb == NULL)
305 		return;
306 
307 	os_memset(&data, 0, sizeof(data));
308 	data.pwd_auth_fail.enrollee = enrollee;
309 	data.pwd_auth_fail.part = part;
310 	os_memcpy(data.pwd_auth_fail.peer_macaddr, mac_addr, ETH_ALEN);
311 	wps->event_cb(wps->cb_ctx, WPS_EV_PWD_AUTH_FAIL, &data);
312 }
313 
314 
wps_pbc_overlap_event(struct wps_context * wps)315 void wps_pbc_overlap_event(struct wps_context *wps)
316 {
317 	if (wps->event_cb == NULL)
318 		return;
319 
320 	wps->event_cb(wps->cb_ctx, WPS_EV_PBC_OVERLAP, NULL);
321 }
322 
323 
wps_pbc_timeout_event(struct wps_context * wps)324 void wps_pbc_timeout_event(struct wps_context *wps)
325 {
326 	if (wps->event_cb == NULL)
327 		return;
328 
329 	wps->event_cb(wps->cb_ctx, WPS_EV_PBC_TIMEOUT, NULL);
330 }
331 
332 
wps_pbc_active_event(struct wps_context * wps)333 void wps_pbc_active_event(struct wps_context *wps)
334 {
335 	if (wps->event_cb == NULL)
336 		return;
337 
338 	wps->event_cb(wps->cb_ctx, WPS_EV_PBC_ACTIVE, NULL);
339 }
340 
341 
wps_pbc_disable_event(struct wps_context * wps)342 void wps_pbc_disable_event(struct wps_context *wps)
343 {
344 	if (wps->event_cb == NULL)
345 		return;
346 
347 	wps->event_cb(wps->cb_ctx, WPS_EV_PBC_DISABLE, NULL);
348 }
349 
350 
351 #ifdef CONFIG_WPS_OOB
352 
wps_get_oob_cred(struct wps_context * wps,int rf_band,int channel)353 struct wpabuf * wps_get_oob_cred(struct wps_context *wps, int rf_band,
354 				 int channel)
355 {
356 	struct wps_data data;
357 	struct wpabuf *plain;
358 
359 	plain = wpabuf_alloc(500);
360 	if (plain == NULL) {
361 		wpa_printf(MSG_ERROR, "WPS: Failed to allocate memory for OOB "
362 			   "credential");
363 		return NULL;
364 	}
365 
366 	os_memset(&data, 0, sizeof(data));
367 	data.wps = wps;
368 	data.auth_type = wps->auth_types;
369 	data.encr_type = wps->encr_types;
370 	if (wps_build_cred(&data, plain) ||
371 	    (rf_band && wps_build_rf_bands_attr(plain, rf_band)) ||
372 	    (channel && wps_build_ap_channel(plain, channel)) ||
373 	    wps_build_mac_addr(plain, wps->dev.mac_addr) ||
374 	    wps_build_wfa_ext(plain, 0, NULL, 0)) {
375 		os_free(data.new_psk);
376 		wpabuf_free(plain);
377 		return NULL;
378 	}
379 
380 	if (wps->wps_state == WPS_STATE_NOT_CONFIGURED && data.new_psk &&
381 	    wps->ap) {
382 		struct wps_credential cred;
383 
384 		wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
385 			   "on credential token generation");
386 
387 		os_memset(&cred, 0, sizeof(cred));
388 		os_memcpy(cred.ssid, wps->ssid, wps->ssid_len);
389 		cred.ssid_len = wps->ssid_len;
390 		cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
391 		cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
392 		os_memcpy(cred.key, data.new_psk, data.new_psk_len);
393 		cred.key_len = data.new_psk_len;
394 
395 		wps->wps_state = WPS_STATE_CONFIGURED;
396 		wpa_hexdump_ascii_key(MSG_DEBUG,
397 				      "WPS: Generated random passphrase",
398 				      data.new_psk, data.new_psk_len);
399 		if (wps->cred_cb)
400 			wps->cred_cb(wps->cb_ctx, &cred);
401 	}
402 
403 	os_free(data.new_psk);
404 
405 	return plain;
406 }
407 
408 
wps_build_nfc_pw_token(u16 dev_pw_id,const struct wpabuf * pubkey,const struct wpabuf * dev_pw)409 struct wpabuf * wps_build_nfc_pw_token(u16 dev_pw_id,
410 				       const struct wpabuf *pubkey,
411 				       const struct wpabuf *dev_pw)
412 {
413 	struct wpabuf *data;
414 
415 	data = wpabuf_alloc(200);
416 	if (data == NULL)
417 		return NULL;
418 
419 	if (wps_build_oob_dev_pw(data, dev_pw_id, pubkey,
420 				 wpabuf_head(dev_pw), wpabuf_len(dev_pw)) ||
421 	    wps_build_wfa_ext(data, 0, NULL, 0)) {
422 		wpa_printf(MSG_ERROR, "WPS: Failed to build NFC password "
423 			   "token");
424 		wpabuf_free(data);
425 		return NULL;
426 	}
427 
428 	return data;
429 }
430 
431 
wps_oob_use_cred(struct wps_context * wps,struct wps_parse_attr * attr)432 int wps_oob_use_cred(struct wps_context *wps, struct wps_parse_attr *attr)
433 {
434 	struct wpabuf msg;
435 	size_t i;
436 
437 	for (i = 0; i < attr->num_cred; i++) {
438 		struct wps_credential local_cred;
439 		struct wps_parse_attr cattr;
440 
441 		os_memset(&local_cred, 0, sizeof(local_cred));
442 		wpabuf_set(&msg, attr->cred[i], attr->cred_len[i]);
443 		if (wps_parse_msg(&msg, &cattr) < 0 ||
444 		    wps_process_cred(&cattr, &local_cred)) {
445 			wpa_printf(MSG_ERROR, "WPS: Failed to parse OOB "
446 				   "credential");
447 			return -1;
448 		}
449 		wps->cred_cb(wps->cb_ctx, &local_cred);
450 	}
451 
452 	return 0;
453 }
454 
455 
456 #endif /* CONFIG_WPS_OOB */
457 
458 
wps_dev_type_str2bin(const char * str,u8 dev_type[WPS_DEV_TYPE_LEN])459 int wps_dev_type_str2bin(const char *str, u8 dev_type[WPS_DEV_TYPE_LEN])
460 {
461 	const char *pos;
462 
463 	/* <categ>-<OUI>-<subcateg> */
464 	WPA_PUT_BE16(dev_type, atoi(str));
465 	pos = os_strchr(str, '-');
466 	if (pos == NULL)
467 		return -1;
468 	pos++;
469 	if (hexstr2bin(pos, &dev_type[2], 4))
470 		return -1;
471 	pos = os_strchr(pos, '-');
472 	if (pos == NULL)
473 		return -1;
474 	pos++;
475 	WPA_PUT_BE16(&dev_type[6], atoi(pos));
476 
477 
478 	return 0;
479 }
480 
481 
wps_dev_type_bin2str(const u8 dev_type[WPS_DEV_TYPE_LEN],char * buf,size_t buf_len)482 char * wps_dev_type_bin2str(const u8 dev_type[WPS_DEV_TYPE_LEN], char *buf,
483 			    size_t buf_len)
484 {
485 	int ret;
486 
487 	ret = os_snprintf(buf, buf_len, "%u-%08X-%u",
488 			  WPA_GET_BE16(dev_type), WPA_GET_BE32(&dev_type[2]),
489 			  WPA_GET_BE16(&dev_type[6]));
490 	if (os_snprintf_error(buf_len, ret))
491 		return NULL;
492 
493 	return buf;
494 }
495 
496 
uuid_gen_mac_addr(const u8 * mac_addr,u8 * uuid)497 void uuid_gen_mac_addr(const u8 *mac_addr, u8 *uuid)
498 {
499 	const u8 *addr[2];
500 	size_t len[2];
501 	u8 hash[SHA1_MAC_LEN];
502 	u8 nsid[16] = {
503 		0x52, 0x64, 0x80, 0xf8,
504 		0xc9, 0x9b,
505 		0x4b, 0xe5,
506 		0xa6, 0x55,
507 		0x58, 0xed, 0x5f, 0x5d, 0x60, 0x84
508 	};
509 
510 	addr[0] = nsid;
511 	len[0] = sizeof(nsid);
512 	addr[1] = mac_addr;
513 	len[1] = 6;
514 	sha1_vector(2, addr, len, hash);
515 	os_memcpy(uuid, hash, 16);
516 
517 	/* Version: 5 = named-based version using SHA-1 */
518 	uuid[6] = (5 << 4) | (uuid[6] & 0x0f);
519 
520 	/* Variant specified in RFC 4122 */
521 	uuid[8] = 0x80 | (uuid[8] & 0x3f);
522 }
523 
524 
wps_config_methods_str2bin(const char * str)525 u16 wps_config_methods_str2bin(const char *str)
526 {
527 	u16 methods = 0;
528 
529 	if (str == NULL || str[0] == '\0') {
530 		/* Default to enabling methods based on build configuration */
531 		methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
532 		methods |= WPS_CONFIG_VIRT_DISPLAY;
533 #ifdef CONFIG_WPS_NFC
534 		methods |= WPS_CONFIG_NFC_INTERFACE;
535 #endif /* CONFIG_WPS_NFC */
536 #ifdef CONFIG_P2P
537 		methods |= WPS_CONFIG_P2PS;
538 #endif /* CONFIG_P2P */
539 	} else {
540 		if (os_strstr(str, "ethernet"))
541 			methods |= WPS_CONFIG_ETHERNET;
542 		if (os_strstr(str, "label"))
543 			methods |= WPS_CONFIG_LABEL;
544 		if (os_strstr(str, "display"))
545 			methods |= WPS_CONFIG_DISPLAY;
546 		if (os_strstr(str, "ext_nfc_token"))
547 			methods |= WPS_CONFIG_EXT_NFC_TOKEN;
548 		if (os_strstr(str, "int_nfc_token"))
549 			methods |= WPS_CONFIG_INT_NFC_TOKEN;
550 		if (os_strstr(str, "nfc_interface"))
551 			methods |= WPS_CONFIG_NFC_INTERFACE;
552 		if (os_strstr(str, "push_button"))
553 			methods |= WPS_CONFIG_PUSHBUTTON;
554 		if (os_strstr(str, "keypad"))
555 			methods |= WPS_CONFIG_KEYPAD;
556 		if (os_strstr(str, "virtual_display"))
557 			methods |= WPS_CONFIG_VIRT_DISPLAY;
558 		if (os_strstr(str, "physical_display"))
559 			methods |= WPS_CONFIG_PHY_DISPLAY;
560 		if (os_strstr(str, "virtual_push_button"))
561 			methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
562 		if (os_strstr(str, "physical_push_button"))
563 			methods |= WPS_CONFIG_PHY_PUSHBUTTON;
564 		if (os_strstr(str, "p2ps"))
565 			methods |= WPS_CONFIG_P2PS;
566 	}
567 
568 	return methods;
569 }
570 
571 
wps_build_wsc_ack(struct wps_data * wps)572 struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
573 {
574 	struct wpabuf *msg;
575 
576 	wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
577 
578 	msg = wpabuf_alloc(1000);
579 	if (msg == NULL)
580 		return NULL;
581 
582 	if (wps_build_version(msg) ||
583 	    wps_build_msg_type(msg, WPS_WSC_ACK) ||
584 	    wps_build_enrollee_nonce(wps, msg) ||
585 	    wps_build_registrar_nonce(wps, msg) ||
586 	    wps_build_wfa_ext(msg, 0, NULL, 0)) {
587 		wpabuf_free(msg);
588 		return NULL;
589 	}
590 
591 	return msg;
592 }
593 
594 
wps_build_wsc_nack(struct wps_data * wps)595 struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
596 {
597 	struct wpabuf *msg;
598 
599 	wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
600 
601 	msg = wpabuf_alloc(1000);
602 	if (msg == NULL)
603 		return NULL;
604 
605 	if (wps_build_version(msg) ||
606 	    wps_build_msg_type(msg, WPS_WSC_NACK) ||
607 	    wps_build_enrollee_nonce(wps, msg) ||
608 	    wps_build_registrar_nonce(wps, msg) ||
609 	    wps_build_config_error(msg, wps->config_error) ||
610 	    wps_build_wfa_ext(msg, 0, NULL, 0)) {
611 		wpabuf_free(msg);
612 		return NULL;
613 	}
614 
615 	return msg;
616 }
617 
618 
619 #ifdef CONFIG_WPS_NFC
620 
wps_nfc_token_build(int ndef,int id,struct wpabuf * pubkey,struct wpabuf * dev_pw)621 struct wpabuf * wps_nfc_token_build(int ndef, int id, struct wpabuf *pubkey,
622 				    struct wpabuf *dev_pw)
623 {
624 	struct wpabuf *ret;
625 
626 	if (pubkey == NULL || dev_pw == NULL)
627 		return NULL;
628 
629 	ret = wps_build_nfc_pw_token(id, pubkey, dev_pw);
630 	if (ndef && ret) {
631 		struct wpabuf *tmp;
632 		tmp = ndef_build_wifi(ret);
633 		wpabuf_free(ret);
634 		if (tmp == NULL)
635 			return NULL;
636 		ret = tmp;
637 	}
638 
639 	return ret;
640 }
641 
642 
wps_nfc_gen_dh(struct wpabuf ** pubkey,struct wpabuf ** privkey)643 int wps_nfc_gen_dh(struct wpabuf **pubkey, struct wpabuf **privkey)
644 {
645 	struct wpabuf *priv = NULL, *pub = NULL;
646 	void *dh_ctx;
647 
648 	dh_ctx = dh5_init(&priv, &pub);
649 	if (dh_ctx == NULL)
650 		return -1;
651 	pub = wpabuf_zeropad(pub, 192);
652 	if (pub == NULL) {
653 		wpabuf_free(priv);
654 		return -1;
655 	}
656 	wpa_hexdump_buf(MSG_DEBUG, "WPS: Generated new DH pubkey", pub);
657 	dh5_free(dh_ctx);
658 
659 	wpabuf_free(*pubkey);
660 	*pubkey = pub;
661 	wpabuf_free(*privkey);
662 	*privkey = priv;
663 
664 	return 0;
665 }
666 
667 
wps_nfc_token_gen(int ndef,int * id,struct wpabuf ** pubkey,struct wpabuf ** privkey,struct wpabuf ** dev_pw)668 struct wpabuf * wps_nfc_token_gen(int ndef, int *id, struct wpabuf **pubkey,
669 				  struct wpabuf **privkey,
670 				  struct wpabuf **dev_pw)
671 {
672 	struct wpabuf *pw;
673 	u16 val;
674 
675 	pw = wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN);
676 	if (pw == NULL)
677 		return NULL;
678 
679 	if (random_get_bytes(wpabuf_put(pw, WPS_OOB_DEVICE_PASSWORD_LEN),
680 			     WPS_OOB_DEVICE_PASSWORD_LEN) ||
681 	    random_get_bytes((u8 *) &val, sizeof(val))) {
682 		wpabuf_free(pw);
683 		return NULL;
684 	}
685 
686 	if (wps_nfc_gen_dh(pubkey, privkey) < 0) {
687 		wpabuf_free(pw);
688 		return NULL;
689 	}
690 
691 	*id = 0x10 + val % 0xfff0;
692 	wpabuf_free(*dev_pw);
693 	*dev_pw = pw;
694 
695 	return wps_nfc_token_build(ndef, *id, *pubkey, *dev_pw);
696 }
697 
698 
wps_build_nfc_handover_req(struct wps_context * ctx,struct wpabuf * nfc_dh_pubkey)699 struct wpabuf * wps_build_nfc_handover_req(struct wps_context *ctx,
700 					   struct wpabuf *nfc_dh_pubkey)
701 {
702 	struct wpabuf *msg;
703 	void *len;
704 
705 	if (ctx == NULL)
706 		return NULL;
707 
708 	wpa_printf(MSG_DEBUG, "WPS: Building attributes for NFC connection "
709 		   "handover request");
710 
711 	if (nfc_dh_pubkey == NULL) {
712 		wpa_printf(MSG_DEBUG, "WPS: No NFC OOB Device Password "
713 			   "configured");
714 		return NULL;
715 	}
716 
717 	msg = wpabuf_alloc(1000);
718 	if (msg == NULL)
719 		return msg;
720 	len = wpabuf_put(msg, 2);
721 
722 	if (wps_build_oob_dev_pw(msg, DEV_PW_NFC_CONNECTION_HANDOVER,
723 				 nfc_dh_pubkey, NULL, 0) ||
724 	    wps_build_uuid_e(msg, ctx->uuid) ||
725 	    wps_build_wfa_ext(msg, 0, NULL, 0)) {
726 		wpabuf_free(msg);
727 		return NULL;
728 	}
729 
730 	WPA_PUT_BE16(len, wpabuf_len(msg) - 2);
731 
732 	return msg;
733 }
734 
735 
wps_build_ssid(struct wpabuf * msg,struct wps_context * wps)736 static int wps_build_ssid(struct wpabuf *msg, struct wps_context *wps)
737 {
738 	wpa_printf(MSG_DEBUG, "WPS:  * SSID");
739 	wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID in Connection Handover Select",
740 			  wps->ssid, wps->ssid_len);
741 	wpabuf_put_be16(msg, ATTR_SSID);
742 	wpabuf_put_be16(msg, wps->ssid_len);
743 	wpabuf_put_data(msg, wps->ssid, wps->ssid_len);
744 	return 0;
745 }
746 
747 
wps_build_ap_freq(struct wpabuf * msg,int freq)748 static int wps_build_ap_freq(struct wpabuf *msg, int freq)
749 {
750 	enum hostapd_hw_mode mode;
751 	u8 channel, rf_band;
752 	u16 ap_channel;
753 
754 	if (freq <= 0)
755 		return 0;
756 
757 	mode = ieee80211_freq_to_chan(freq, &channel);
758 	if (mode == NUM_HOSTAPD_MODES)
759 		return 0; /* Unknown channel */
760 
761 	if (mode == HOSTAPD_MODE_IEEE80211G || mode == HOSTAPD_MODE_IEEE80211B)
762 		rf_band = WPS_RF_24GHZ;
763 	else if (mode == HOSTAPD_MODE_IEEE80211A)
764 		rf_band = WPS_RF_50GHZ;
765 	else if (mode == HOSTAPD_MODE_IEEE80211AD)
766 		rf_band = WPS_RF_60GHZ;
767 	else
768 		return 0; /* Unknown band */
769 	ap_channel = channel;
770 
771 	if (wps_build_rf_bands_attr(msg, rf_band) ||
772 	    wps_build_ap_channel(msg, ap_channel))
773 		return -1;
774 
775 	return 0;
776 }
777 
778 
wps_build_nfc_handover_sel(struct wps_context * ctx,struct wpabuf * nfc_dh_pubkey,const u8 * bssid,int freq)779 struct wpabuf * wps_build_nfc_handover_sel(struct wps_context *ctx,
780 					   struct wpabuf *nfc_dh_pubkey,
781 					   const u8 *bssid, int freq)
782 {
783 	struct wpabuf *msg;
784 	void *len;
785 
786 	if (ctx == NULL)
787 		return NULL;
788 
789 	wpa_printf(MSG_DEBUG, "WPS: Building attributes for NFC connection "
790 		   "handover select");
791 
792 	if (nfc_dh_pubkey == NULL) {
793 		wpa_printf(MSG_DEBUG, "WPS: No NFC OOB Device Password "
794 			   "configured");
795 		return NULL;
796 	}
797 
798 	msg = wpabuf_alloc(1000);
799 	if (msg == NULL)
800 		return msg;
801 	len = wpabuf_put(msg, 2);
802 
803 	if (wps_build_oob_dev_pw(msg, DEV_PW_NFC_CONNECTION_HANDOVER,
804 				 nfc_dh_pubkey, NULL, 0) ||
805 	    wps_build_ssid(msg, ctx) ||
806 	    wps_build_ap_freq(msg, freq) ||
807 	    (bssid && wps_build_mac_addr(msg, bssid)) ||
808 	    wps_build_wfa_ext(msg, 0, NULL, 0)) {
809 		wpabuf_free(msg);
810 		return NULL;
811 	}
812 
813 	WPA_PUT_BE16(len, wpabuf_len(msg) - 2);
814 
815 	return msg;
816 }
817 
818 
wps_build_nfc_handover_req_p2p(struct wps_context * ctx,struct wpabuf * nfc_dh_pubkey)819 struct wpabuf * wps_build_nfc_handover_req_p2p(struct wps_context *ctx,
820 					       struct wpabuf *nfc_dh_pubkey)
821 {
822 	struct wpabuf *msg;
823 
824 	if (ctx == NULL)
825 		return NULL;
826 
827 	wpa_printf(MSG_DEBUG, "WPS: Building attributes for NFC connection "
828 		   "handover request (P2P)");
829 
830 	if (nfc_dh_pubkey == NULL) {
831 		wpa_printf(MSG_DEBUG, "WPS: No NFC DH Public Key configured");
832 		return NULL;
833 	}
834 
835 	msg = wpabuf_alloc(1000);
836 	if (msg == NULL)
837 		return msg;
838 
839 	if (wps_build_manufacturer(&ctx->dev, msg) ||
840 	    wps_build_model_name(&ctx->dev, msg) ||
841 	    wps_build_model_number(&ctx->dev, msg) ||
842 	    wps_build_oob_dev_pw(msg, DEV_PW_NFC_CONNECTION_HANDOVER,
843 				 nfc_dh_pubkey, NULL, 0) ||
844 	    wps_build_rf_bands(&ctx->dev, msg, 0) ||
845 	    wps_build_serial_number(&ctx->dev, msg) ||
846 	    wps_build_uuid_e(msg, ctx->uuid) ||
847 	    wps_build_wfa_ext(msg, 0, NULL, 0)) {
848 		wpabuf_free(msg);
849 		return NULL;
850 	}
851 
852 	return msg;
853 }
854 
855 
wps_build_nfc_handover_sel_p2p(struct wps_context * ctx,int nfc_dev_pw_id,struct wpabuf * nfc_dh_pubkey,struct wpabuf * nfc_dev_pw)856 struct wpabuf * wps_build_nfc_handover_sel_p2p(struct wps_context *ctx,
857 					       int nfc_dev_pw_id,
858 					       struct wpabuf *nfc_dh_pubkey,
859 					       struct wpabuf *nfc_dev_pw)
860 {
861 	struct wpabuf *msg;
862 	const u8 *dev_pw;
863 	size_t dev_pw_len;
864 
865 	if (ctx == NULL)
866 		return NULL;
867 
868 	wpa_printf(MSG_DEBUG, "WPS: Building attributes for NFC connection "
869 		   "handover select (P2P)");
870 
871 	if (nfc_dh_pubkey == NULL ||
872 	    (nfc_dev_pw_id != DEV_PW_NFC_CONNECTION_HANDOVER &&
873 	     nfc_dev_pw == NULL)) {
874 		wpa_printf(MSG_DEBUG, "WPS: No NFC OOB Device Password "
875 			   "configured");
876 		return NULL;
877 	}
878 
879 	msg = wpabuf_alloc(1000);
880 	if (msg == NULL)
881 		return msg;
882 
883 	if (nfc_dev_pw) {
884 		dev_pw = wpabuf_head(nfc_dev_pw);
885 		dev_pw_len = wpabuf_len(nfc_dev_pw);
886 	} else {
887 		dev_pw = NULL;
888 		dev_pw_len = 0;
889 	}
890 
891 	if (wps_build_manufacturer(&ctx->dev, msg) ||
892 	    wps_build_model_name(&ctx->dev, msg) ||
893 	    wps_build_model_number(&ctx->dev, msg) ||
894 	    wps_build_oob_dev_pw(msg, nfc_dev_pw_id, nfc_dh_pubkey,
895 				 dev_pw, dev_pw_len) ||
896 	    wps_build_rf_bands(&ctx->dev, msg, 0) ||
897 	    wps_build_serial_number(&ctx->dev, msg) ||
898 	    wps_build_uuid_e(msg, ctx->uuid) ||
899 	    wps_build_wfa_ext(msg, 0, NULL, 0)) {
900 		wpabuf_free(msg);
901 		return NULL;
902 	}
903 
904 	return msg;
905 }
906 
907 #endif /* CONFIG_WPS_NFC */
908