wps_common.c 17 KB

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