1/*
2 * nfc_pw_token - Tool for building NFC password tokens for WPS
3 * Copyright (c) 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 "utils/common.h"
12#include "crypto/random.h"
13#include "wpa_supplicant_i.h"
14#include "config.h"
15#include "wps_supplicant.h"
16
17
18static void print_bin(const char *title, const struct wpabuf *buf)
19{
20	size_t i, len;
21	const u8 *pos;
22
23	if (buf == NULL)
24		return;
25
26	printf("%s=", title);
27
28	pos = wpabuf_head(buf);
29	len = wpabuf_len(buf);
30	for (i = 0; i < len; i++)
31		printf("%02X", *pos++);
32
33	printf("\n");
34}
35
36
37int main(int argc, char *argv[])
38{
39	struct wpa_supplicant wpa_s;
40	int ret = -1;
41	struct wpabuf *buf = NULL, *ndef = NULL;
42	char txt[1000];
43
44	if (os_program_init())
45		return -1;
46	random_init(NULL);
47
48	os_memset(&wpa_s, 0, sizeof(wpa_s));
49	wpa_s.conf = os_zalloc(sizeof(*wpa_s.conf));
50	if (wpa_s.conf == NULL)
51		goto fail;
52
53	buf = wpas_wps_nfc_token(&wpa_s, 0);
54	if (buf == NULL)
55		goto fail;
56
57	ndef = ndef_build_wifi(buf);
58	if (ndef == NULL)
59		goto fail;
60
61	wpa_snprintf_hex_uppercase(txt, sizeof(txt), wpabuf_head(buf),
62				   wpabuf_len(buf));
63	printf("#WPS=%s\n", txt);
64
65	wpa_snprintf_hex_uppercase(txt, sizeof(txt), wpabuf_head(ndef),
66				   wpabuf_len(ndef));
67	printf("#NDEF=%s\n", txt);
68
69	printf("wps_nfc_dev_pw_id=%d\n", wpa_s.conf->wps_nfc_dev_pw_id);
70	print_bin("wps_nfc_dh_pubkey", wpa_s.conf->wps_nfc_dh_pubkey);
71	print_bin("wps_nfc_dh_privkey", wpa_s.conf->wps_nfc_dh_privkey);
72	print_bin("wps_nfc_dev_pw", wpa_s.conf->wps_nfc_dev_pw);
73
74	ret = 0;
75fail:
76	wpabuf_free(ndef);
77	wpabuf_free(buf);
78	wpa_config_free(wpa_s.conf);
79	random_deinit();
80	os_program_deinit();
81
82	return ret;
83}
84