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 #include <string.h>
9 
10 #include "utils/includes.h"
11 #include "utils/common.h"
12 
13 #include "crypto/aes_wrap.h"
14 #include "crypto/crypto.h"
15 #include "crypto/sha1.h"
16 #include "crypto/sha256.h"
17 #include "crypto/dh_group5.h"
18 #include "crypto/random.h"
19 
20 #include "wps/wps_i.h"
21 
wps_kdf(const u8 * key,const u8 * label_prefix,size_t label_prefix_len,const char * label,u8 * res,size_t res_len)22 void wps_kdf(const u8 *key, const u8 *label_prefix, size_t label_prefix_len,
23 	     const char *label, u8 *res, size_t res_len)
24 {
25 	u8 i_buf[4], key_bits[4];
26 	const u8 *addr[4];
27 	size_t len[4];
28 	int i, iter;
29 	u8 hash[SHA256_MAC_LEN], *opos;
30 	size_t left;
31 
32 	WPA_PUT_BE32(key_bits, res_len * 8);
33 
34 	addr[0] = i_buf;
35 	len[0] = sizeof(i_buf);
36 	addr[1] = label_prefix;
37 	len[1] = label_prefix_len;
38 	addr[2] = (const u8 *) label;
39 	len[2] = os_strlen(label);
40 	addr[3] = key_bits;
41 	len[3] = sizeof(key_bits);
42 
43 	iter = (res_len + SHA256_MAC_LEN - 1) / SHA256_MAC_LEN;
44 	opos = res;
45 	left = res_len;
46 
47 	for (i = 1; i <= iter; i++) {
48 		WPA_PUT_BE32(i_buf, i);
49 		hmac_sha256_vector(key, SHA256_MAC_LEN, 4, addr, len, hash);
50 		if (i < iter) {
51 			os_memcpy(opos, hash, SHA256_MAC_LEN);
52 			opos += SHA256_MAC_LEN;
53 			left -= SHA256_MAC_LEN;
54 		} else
55 			os_memcpy(opos, hash, left);
56 	}
57 }
58 
59 
wps_derive_keys(struct wps_data * wps)60 int wps_derive_keys(struct wps_data *wps)
61 {
62 	struct wpabuf *pubkey, *dh_shared;
63 	u8 dhkey[SHA256_MAC_LEN], kdk[SHA256_MAC_LEN];
64 	const u8 *addr[3];
65 	size_t len[3];
66 	u8 keys[WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN + WPS_EMSK_LEN];
67 
68 	if (wps->dh_privkey == NULL) {
69 		wpa_printf(MSG_DEBUG,  "WPS: Own DH private key not available");
70 		return -1;
71 	}
72 
73 	pubkey = wps->registrar ? wps->dh_pubkey_e : wps->dh_pubkey_r;
74 	if (pubkey == NULL) {
75 		wpa_printf(MSG_DEBUG,  "WPS: Peer DH public key not available");
76 		return -1;
77 	}
78 
79 	wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH Private Key", wps->dh_privkey);
80 	wpa_hexdump_buf(MSG_DEBUG, "WPS: DH peer Public Key", pubkey);
81 	dh_shared = dh5_derive_shared(wps->dh_ctx, pubkey, wps->dh_privkey);
82 	dh5_free(wps->dh_ctx);
83 	wps->dh_ctx = NULL;
84 	dh_shared = wpabuf_zeropad(dh_shared, 192);
85 	if (dh_shared == NULL) {
86 		wpa_printf(MSG_DEBUG,  "WPS: Failed to derive DH shared key");
87 		return -1;
88 	}
89 
90 	/* Own DH private key is not needed anymore */
91 /*
92  * due to the public key calculated when wps start, it will not calculate anymore even when we build M1 message, also calculate the key need take a long time
93  * which would cause WPS fail, so we clean the key after WPS finished .
94  */
95 #ifndef ESP32_WORKAROUND
96 	wpabuf_free(wps->dh_privkey);
97 	wps->dh_privkey = NULL;
98 #endif //ESP32_WORKAROUND
99 
100 	wpa_hexdump_buf_key(MSG_DEBUG, "WPS: DH shared key", dh_shared);
101 
102 	/* DHKey = SHA-256(g^AB mod p) */
103 	addr[0] = wpabuf_head(dh_shared);
104 	len[0] = wpabuf_len(dh_shared);
105 
106 	sha256_vector(1, addr, len, dhkey);
107 	wpa_hexdump_key(MSG_DEBUG, "WPS: DHKey", dhkey, sizeof(dhkey));
108 	wpabuf_free(dh_shared);
109 
110 	/* KDK = HMAC-SHA-256_DHKey(N1 || EnrolleeMAC || N2) */
111 	addr[0] = wps->nonce_e;
112 	len[0] = WPS_NONCE_LEN;
113 	addr[1] = wps->mac_addr_e;
114 	len[1] = ETH_ALEN;
115 	addr[2] = wps->nonce_r;
116 	len[2] = WPS_NONCE_LEN;
117 	hmac_sha256_vector(dhkey, sizeof(dhkey), 3, addr, len, kdk);
118 	wpa_hexdump_key(MSG_DEBUG, "WPS: KDK", kdk, sizeof(kdk));
119 
120 	wps_kdf(kdk, NULL, 0, "Wi-Fi Easy and Secure Key Derivation",
121 		keys, sizeof(keys));
122 	os_memcpy(wps->authkey, keys, WPS_AUTHKEY_LEN);
123 	os_memcpy(wps->keywrapkey, keys + WPS_AUTHKEY_LEN, WPS_KEYWRAPKEY_LEN);
124 	os_memcpy(wps->emsk, keys + WPS_AUTHKEY_LEN + WPS_KEYWRAPKEY_LEN,
125 		  WPS_EMSK_LEN);
126 
127 	wpa_hexdump_key(MSG_DEBUG, "WPS: AuthKey",
128 			wps->authkey, WPS_AUTHKEY_LEN);
129 	wpa_hexdump_key(MSG_DEBUG, "WPS: KeyWrapKey",
130 			wps->keywrapkey, WPS_KEYWRAPKEY_LEN);
131 	wpa_hexdump_key(MSG_DEBUG, "WPS: EMSK", wps->emsk, WPS_EMSK_LEN);
132 
133 	return 0;
134 }
135 
136 
wps_derive_psk(struct wps_data * wps,const u8 * dev_passwd,size_t dev_passwd_len)137 void wps_derive_psk(struct wps_data *wps, const u8 *dev_passwd,
138 		    size_t dev_passwd_len)
139 {
140 	u8 hash[SHA256_MAC_LEN];
141 
142 	hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN, dev_passwd,
143 		                             (dev_passwd_len + 1) / 2, hash);
144 	os_memcpy(wps->psk1, hash, WPS_PSK_LEN);
145 	hmac_sha256(wps->authkey, WPS_AUTHKEY_LEN,
146 		            dev_passwd + (dev_passwd_len + 1) / 2,
147 		            dev_passwd_len / 2, hash);
148 	os_memcpy(wps->psk2, hash, WPS_PSK_LEN);
149 
150 	wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Device Password",
151 			      dev_passwd, dev_passwd_len);
152 	wpa_hexdump_key(MSG_DEBUG, "WPS: PSK1", wps->psk1, WPS_PSK_LEN);
153 	wpa_hexdump_key(MSG_DEBUG, "WPS: PSK2", wps->psk2, WPS_PSK_LEN);
154 }
155 
156 
wps_decrypt_encr_settings(struct wps_data * wps,const u8 * encr,size_t encr_len)157 struct wpabuf * wps_decrypt_encr_settings(struct wps_data *wps, const u8 *encr,
158 					  size_t encr_len)
159 {
160 	struct wpabuf *decrypted;
161 	const size_t block_size = 16;
162 	size_t i;
163 	u8 pad;
164 	const u8 *pos;
165 
166 	/* AES-128-CBC */
167 	if (encr == NULL || encr_len < 2 * block_size || encr_len % block_size)
168 	{
169 		wpa_printf(MSG_DEBUG,  "WPS: No Encrypted Settings received");
170 		return NULL;
171 	}
172 
173 	decrypted = wpabuf_alloc(encr_len - block_size);
174 	if (decrypted == NULL)
175 		return NULL;
176 
177 	wpa_hexdump(MSG_MSGDUMP, "WPS: Encrypted Settings", encr, encr_len);
178 	wpabuf_put_data(decrypted, encr + block_size, encr_len - block_size);
179 	wpa_printf(MSG_DEBUG,  "WPS: AES Decrypt setting");
180 	if (aes_128_cbc_decrypt(wps->keywrapkey, encr, wpabuf_mhead(decrypted),
181 				        wpabuf_len(decrypted))) {
182 		wpabuf_free(decrypted);
183 		return NULL;
184 	}
185 
186 	wpa_hexdump_buf_key(MSG_MSGDUMP, "WPS: Decrypted Encrypted Settings",
187 			    decrypted);
188 
189 	pos = wpabuf_head_u8(decrypted) + wpabuf_len(decrypted) - 1;
190 	pad = *pos;
191 	if (pad > wpabuf_len(decrypted)) {
192 		wpa_printf(MSG_DEBUG,  "WPS: Invalid PKCS#5 v2.0 pad value");
193 		wpabuf_free(decrypted);
194 		return NULL;
195 	}
196 	for (i = 0; i < pad; i++) {
197 		if (*pos-- != pad) {
198 			wpa_printf(MSG_DEBUG,  "WPS: Invalid PKCS#5 v2.0 pad "
199 				   "string");
200 			wpabuf_free(decrypted);
201 			return NULL;
202 		}
203 	}
204 	decrypted->used -= pad;
205 
206 	return decrypted;
207 }
208 
209 #ifdef CONFIG_WPS_PIN
210 /**
211  * wps_pin_checksum - Compute PIN checksum
212  * @pin: Seven digit PIN (i.e., eight digit PIN without the checksum digit)
213  * Returns: Checksum digit
214  */
wps_pin_checksum(unsigned int pin)215 unsigned int wps_pin_checksum(unsigned int pin)
216 {
217 	unsigned int accum = 0;
218 	while (pin) {
219 		accum += 3 * (pin % 10);
220 		pin /= 10;
221 		accum += pin % 10;
222 		pin /= 10;
223 	}
224 
225 	return (10 - accum % 10) % 10;
226 }
227 
228 
229 /**
230  * wps_pin_valid - Check whether a PIN has a valid checksum
231  * @pin: Eight digit PIN (i.e., including the checksum digit)
232  * Returns: 1 if checksum digit is valid, or 0 if not
233  */
wps_pin_valid(unsigned int pin)234 unsigned int wps_pin_valid(unsigned int pin)
235 {
236 	return wps_pin_checksum(pin / 10) == (pin % 10);
237 }
238 
239 
240 /**
241  * wps_generate_pin - Generate a random PIN
242  * Returns: Eight digit PIN (i.e., including the checksum digit)
243  */
wps_generate_pin(void)244 unsigned int wps_generate_pin(void)
245 {
246 	unsigned int val;
247 
248 	/* Generate seven random digits for the PIN */
249 	if (random_get_bytes((unsigned char *) &val, sizeof(val)) < 0) {
250 		return -1;
251 	}
252 	val %= 10000000;
253 
254 	/* Append checksum digit */
255 	return val * 10 + wps_pin_checksum(val);
256 }
257 
258 
wps_pin_str_valid(const char * pin)259 int wps_pin_str_valid(const char *pin)
260 {
261 	const char *p;
262 	size_t len;
263 
264 	p = pin;
265 	while (*p >= '0' && *p <= '9')
266 		p++;
267 	if (*p != '\0')
268 		return 0;
269 
270 	len = p - pin;
271 	return len == 4 || len == 8;
272 }
273 #endif
274 
wps_fail_event(struct wps_context * wps,enum wps_msg_type msg,u16 config_error,u16 error_indication)275 void wps_fail_event(struct wps_context *wps, enum wps_msg_type msg,
276 		    u16 config_error, u16 error_indication)
277 {
278 	union wps_event_data *data;
279 
280         data = (union wps_event_data *)os_zalloc(sizeof(union wps_event_data));
281 	if (data == NULL)
282 		return;
283 
284 	if (wps->event_cb == NULL) {
285 		os_free(data);
286 		return;
287 	}
288 
289 	os_memset(data, 0, sizeof(union wps_event_data));
290 	data->fail.msg = msg;
291 	data->fail.config_error = config_error;
292 	data->fail.error_indication = error_indication;
293 	wps->event_cb(wps->cb_ctx, WPS_EV_FAIL, data);
294 	os_free(data);
295 }
296 
297 
wps_success_event(struct wps_context * wps)298 void wps_success_event(struct wps_context *wps)
299 {
300 	if (wps->event_cb == NULL)
301 		return;
302 
303 	wps->event_cb(wps->cb_ctx, WPS_EV_SUCCESS, NULL);
304 }
305 
306 
wps_pwd_auth_fail_event(struct wps_context * wps,int enrollee,int part)307 void wps_pwd_auth_fail_event(struct wps_context *wps, int enrollee, int part)
308 {
309 	union wps_event_data *data;
310 
311         data = (union wps_event_data *)os_zalloc(sizeof(union wps_event_data));
312 	if (data == NULL)
313 		return;
314 
315 	if (wps->event_cb == NULL) {
316 		os_free(data);
317 		return;
318 	}
319 
320 	os_memset(data, 0, sizeof(union wps_event_data));
321 	data->pwd_auth_fail.enrollee = enrollee;
322 	data->pwd_auth_fail.part = part;
323 	wps->event_cb(wps->cb_ctx, WPS_EV_PWD_AUTH_FAIL, data);
324 	os_free(data);
325 }
326 
327 
wps_pbc_overlap_event(struct wps_context * wps)328 void wps_pbc_overlap_event(struct wps_context *wps)
329 {
330 	if (wps->event_cb == NULL)
331 		return;
332 
333 	wps->event_cb(wps->cb_ctx, WPS_EV_PBC_OVERLAP, NULL);
334 }
335 
336 
wps_pbc_timeout_event(struct wps_context * wps)337 void wps_pbc_timeout_event(struct wps_context *wps)
338 {
339 	if (wps->event_cb == NULL)
340 		return;
341 
342 	wps->event_cb(wps->cb_ctx, WPS_EV_PBC_TIMEOUT, NULL);
343 }
344 
345 
346 #ifdef CONFIG_WPS_OOB
347 
wps_get_oob_cred(struct wps_context * wps)348 struct wpabuf * wps_get_oob_cred(struct wps_context *wps)
349 {
350 	struct wps_data *data;
351 	struct wpabuf *plain;
352 
353 	data = (struct wps_data *)os_zalloc(sizeof(struct wps_data));
354 	if (data == NULL)
355 		return NULL;
356 
357 	plain = wpabuf_alloc(500);
358 	if (plain == NULL) {
359 		os_free(data);
360 		wpa_printf(MSG_ERROR,  "WPS: Failed to allocate memory for OOB "
361 			   "credential");
362 		return NULL;
363 	}
364 
365 	os_memset(data, 0, sizeof(struct wps_data));
366 	data->wps = wps;
367 	data->auth_type = wps->auth_types;
368 	data->encr_type = wps->encr_types;
369 	if (wps_build_version(plain) ||
370 	    wps_build_cred(data, plain) ||
371 	    wps_build_wfa_ext(plain, 0, NULL, 0)) {
372 		wpabuf_free(plain);
373 		os_free(data);
374 		return NULL;
375 	}
376 
377 	os_free(data);
378 	return plain;
379 }
380 
381 #ifdef CONFIG_WPS_NFC
382 
wps_build_nfc_pw_token(u16 dev_pw_id,const struct wpabuf * pubkey,const struct wpabuf * dev_pw)383 struct wpabuf * wps_build_nfc_pw_token(u16 dev_pw_id,
384 				       const struct wpabuf *pubkey,
385 				       const struct wpabuf *dev_pw)
386 {
387 	struct wpabuf *data;
388 
389 	data = wpabuf_alloc(200);
390 	if (data == NULL)
391 		return NULL;
392 
393 	if (wps_build_version(data) ||
394 	    wps_build_oob_dev_pw(data, dev_pw_id, pubkey,
395 				 wpabuf_head(dev_pw), wpabuf_len(dev_pw)) ||
396 	    wps_build_wfa_ext(data, 0, NULL, 0)) {
397 		wpa_printf(MSG_ERROR,  "WPS: Failed to build NFC password "
398 			   "token");
399 		wpabuf_free(data);
400 		return NULL;
401 	}
402 
403 	return data;
404 }
405 
406 #endif
407 
wps_oob_use_cred(struct wps_context * wps,struct wps_parse_attr * attr)408 int wps_oob_use_cred(struct wps_context *wps, struct wps_parse_attr *attr)
409 {
410 	struct wpabuf msg;
411 	size_t i;
412 
413 	for (i = 0; i < attr->num_cred; i++) {
414 		struct wps_credential local_cred;
415 		struct wps_parse_attr cattr;
416 
417 		os_memset(&local_cred, 0, sizeof(local_cred));
418 		wpabuf_set(&msg, attr->cred[i], attr->cred_len[i]);
419 		if (wps_parse_msg(&msg, &cattr) < 0 ||
420 		    wps_process_cred(&cattr, &local_cred)) {
421 			wpa_printf(MSG_ERROR,  "WPS: Failed to parse OOB "
422 				   "credential");
423 			return -1;
424 		}
425 		wps->cred_cb(wps->cb_ctx, &local_cred);
426 	}
427 
428 	return 0;
429 }
430 
431 
432 #endif /* CONFIG_WPS_OOB */
433 
434 
wps_dev_type_str2bin(const char * str,u8 dev_type[WPS_DEV_TYPE_LEN])435 int wps_dev_type_str2bin(const char *str, u8 dev_type[WPS_DEV_TYPE_LEN])
436 {
437 	const char *pos;
438 
439 	/* <categ>-<OUI>-<subcateg> */
440 	WPA_PUT_BE16(dev_type, atoi(str));
441 	pos = (char *)os_strchr(str, '-');
442 	if (pos == NULL)
443 		return -1;
444 	pos++;
445 	if (hexstr2bin(pos, &dev_type[2], 4))
446 		return -1;
447 	pos = (char *)os_strchr(pos, '-');
448 	if (pos == NULL)
449 		return -1;
450 	pos++;
451 	WPA_PUT_BE16(&dev_type[6], atoi(pos));
452 
453 
454 	return 0;
455 }
456 
457 
wps_dev_type_bin2str(const u8 dev_type[WPS_DEV_TYPE_LEN],char * buf,size_t buf_len)458 char * wps_dev_type_bin2str(const u8 dev_type[WPS_DEV_TYPE_LEN], char *buf,
459 			    size_t buf_len)
460 {
461 	int ret;
462 
463 	ret = snprintf(buf, buf_len, "%u-%08X-%u",
464 			  WPA_GET_BE16(dev_type), WPA_GET_BE32(&dev_type[2]),
465 			  WPA_GET_BE16(&dev_type[6]));
466 	if (ret < 0 || (unsigned int) ret >= buf_len)
467 		return NULL;
468 
469 	return buf;
470 }
471 
472 
uuid_gen_mac_addr(const u8 * mac_addr,u8 * uuid)473 void uuid_gen_mac_addr(const u8 *mac_addr, u8 *uuid)
474 {
475 	const u8 *addr[2];
476 	size_t len[2];
477 	u8 hash[SHA1_MAC_LEN];
478 	u8 nsid[16] = {
479 		0x52, 0x64, 0x80, 0xf8,
480 		0xc9, 0x9b,
481 		0x4b, 0xe5,
482 		0xa6, 0x55,
483 		0x58, 0xed, 0x5f, 0x5d, 0x60, 0x84
484 	};
485 
486 	addr[0] = nsid;
487 	len[0] = sizeof(nsid);
488 	addr[1] = mac_addr;
489 	len[1] = 6;
490 	sha1_vector(2, addr, len, hash);
491 	os_memcpy(uuid, hash, 16);
492 
493 	/* Version: 5 = named-based version using SHA-1 */
494 	uuid[6] = (5 << 4) | (uuid[6] & 0x0f);
495 
496 	/* Variant specified in RFC 4122 */
497 	uuid[8] = 0x80 | (uuid[8] & 0x3f);
498 }
499 
500 
wps_config_methods_str2bin(const char * str)501 u16 wps_config_methods_str2bin(const char *str)
502 {
503 	u16 methods = 0;
504 
505 	if (str == NULL) {
506 		/* Default to enabling methods based on build configuration */
507 		methods |= WPS_CONFIG_DISPLAY | WPS_CONFIG_KEYPAD;
508 #ifdef CONFIG_WPS2
509 		methods |= WPS_CONFIG_VIRT_DISPLAY;
510 #endif /* CONFIG_WPS2 */
511 #ifdef CONFIG_WPS_NFC
512 		methods |= WPS_CONFIG_NFC_INTERFACE;
513 #endif /* CONFIG_WPS_NFC */
514 	} else {
515 		if (os_strstr(str, "ethernet"))
516 			methods |= WPS_CONFIG_ETHERNET;
517 		if (os_strstr(str, "label"))
518 			methods |= WPS_CONFIG_LABEL;
519 		if (os_strstr(str, "display"))
520 			methods |= WPS_CONFIG_DISPLAY;
521 		if (os_strstr(str, "ext_nfc_token"))
522 			methods |= WPS_CONFIG_EXT_NFC_TOKEN;
523 		if (os_strstr(str, "int_nfc_token"))
524 			methods |= WPS_CONFIG_INT_NFC_TOKEN;
525 		if (os_strstr(str, "nfc_interface"))
526 			methods |= WPS_CONFIG_NFC_INTERFACE;
527 		if (os_strstr(str, "push_button"))
528 			methods |= WPS_CONFIG_PUSHBUTTON;
529 		if (os_strstr(str, "keypad"))
530 			methods |= WPS_CONFIG_KEYPAD;
531 #ifdef CONFIG_WPS2
532 		if (os_strstr(str, "virtual_display"))
533 			methods |= WPS_CONFIG_VIRT_DISPLAY;
534 		if (os_strstr(str, "physical_display"))
535 			methods |= WPS_CONFIG_PHY_DISPLAY;
536 		if (os_strstr(str, "virtual_push_button"))
537 			methods |= WPS_CONFIG_VIRT_PUSHBUTTON;
538 		if (os_strstr(str, "physical_push_button"))
539 			methods |= WPS_CONFIG_PHY_PUSHBUTTON;
540 #endif /* CONFIG_WPS2 */
541 	}
542 
543 	return methods;
544 }
545 
546 
wps_build_wsc_ack(struct wps_data * wps)547 struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
548 {
549 	struct wpabuf *msg;
550 
551 	wpa_printf(MSG_DEBUG,  "WPS: Building Message WSC_ACK");
552 
553 	msg = wpabuf_alloc(1000);
554 	if (msg == NULL)
555 		return NULL;
556 
557 	if (wps_build_version(msg) ||
558 	    wps_build_msg_type(msg, WPS_WSC_ACK) ||
559 	    wps_build_enrollee_nonce(wps, msg) ||
560 	    wps_build_registrar_nonce(wps, msg) ||
561 	    wps_build_wfa_ext(msg, 0, NULL, 0)) {
562 		wpabuf_free(msg);
563 		return NULL;
564 	}
565 
566 	return msg;
567 }
568 
569 
wps_build_wsc_nack(struct wps_data * wps)570 struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
571 {
572 	struct wpabuf *msg;
573 
574 	wpa_printf(MSG_DEBUG,  "WPS: Building Message WSC_NACK");
575 
576 	msg = wpabuf_alloc(1000);
577 	if (msg == NULL)
578 		return NULL;
579 
580 	if (wps_build_version(msg) ||
581 	    wps_build_msg_type(msg, WPS_WSC_NACK) ||
582 	    wps_build_enrollee_nonce(wps, msg) ||
583 	    wps_build_registrar_nonce(wps, msg) ||
584 	    wps_build_config_error(msg, wps->config_error) ||
585 	    wps_build_wfa_ext(msg, 0, NULL, 0)) {
586 		wpabuf_free(msg);
587 		return NULL;
588 	}
589 
590 	return msg;
591 }
592 
593 
594 #ifdef CONFIG_WPS_NFC
wps_nfc_token_gen(int ndef,int * id,struct wpabuf ** pubkey,struct wpabuf ** privkey,struct wpabuf ** dev_pw)595 struct wpabuf * wps_nfc_token_gen(int ndef, int *id, struct wpabuf **pubkey,
596 				  struct wpabuf **privkey,
597 				  struct wpabuf **dev_pw)
598 {
599 	struct wpabuf *priv = NULL, *pub = NULL, *pw, *ret;
600 	void *dh_ctx;
601 	u16 val;
602 
603 	pw = wpabuf_alloc(WPS_OOB_DEVICE_PASSWORD_LEN);
604 	if (pw == NULL)
605 		return NULL;
606 
607 	if (random_get_bytes(wpabuf_put(pw, WPS_OOB_DEVICE_PASSWORD_LEN),
608 			     WPS_OOB_DEVICE_PASSWORD_LEN) ||
609 	    random_get_bytes((u8 *) &val, sizeof(val))) {
610 		wpabuf_free(pw);
611 		return NULL;
612 	}
613 
614 	dh_ctx = dh5_init(&priv, &pub);
615 	if (dh_ctx == NULL) {
616 		wpabuf_free(pw);
617 		return NULL;
618 	}
619 	dh5_free(dh_ctx);
620 
621 	*id = 0x10 + val % 0xfff0;
622 	wpabuf_free(*pubkey);
623 	*pubkey = pub;
624 	wpabuf_free(*privkey);
625 	*privkey = priv;
626 	wpabuf_free(*dev_pw);
627 	*dev_pw = pw;
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 #endif /* CONFIG_WPS_NFC */
642