nfc_pw_token.c 1.7 KB

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