wps_common.c 21 KB

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