wps_enrollee.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  1. /*
  2. * Wi-Fi Protected Setup - Enrollee
  3. * Copyright (c) 2008, 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/crypto.h"
  17. #include "crypto/sha256.h"
  18. #include "wps_i.h"
  19. #include "wps_dev_attr.h"
  20. static int wps_build_mac_addr(struct wps_data *wps, struct wpabuf *msg)
  21. {
  22. wpa_printf(MSG_DEBUG, "WPS: * MAC Address");
  23. wpabuf_put_be16(msg, ATTR_MAC_ADDR);
  24. wpabuf_put_be16(msg, ETH_ALEN);
  25. wpabuf_put_data(msg, wps->mac_addr_e, ETH_ALEN);
  26. return 0;
  27. }
  28. static int wps_build_wps_state(struct wps_data *wps, struct wpabuf *msg)
  29. {
  30. u8 state;
  31. if (wps->wps->ap)
  32. state = wps->wps->wps_state;
  33. else
  34. state = WPS_STATE_NOT_CONFIGURED;
  35. wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
  36. state);
  37. wpabuf_put_be16(msg, ATTR_WPS_STATE);
  38. wpabuf_put_be16(msg, 1);
  39. wpabuf_put_u8(msg, state);
  40. return 0;
  41. }
  42. static int wps_build_e_hash(struct wps_data *wps, struct wpabuf *msg)
  43. {
  44. u8 *hash;
  45. const u8 *addr[4];
  46. size_t len[4];
  47. if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
  48. return -1;
  49. wpa_hexdump(MSG_DEBUG, "WPS: E-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
  50. wpa_hexdump(MSG_DEBUG, "WPS: E-S2",
  51. wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
  52. if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
  53. wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
  54. "E-Hash derivation");
  55. return -1;
  56. }
  57. wpa_printf(MSG_DEBUG, "WPS: * E-Hash1");
  58. wpabuf_put_be16(msg, ATTR_E_HASH1);
  59. wpabuf_put_be16(msg, SHA256_MAC_LEN);
  60. hash = wpabuf_put(msg, SHA256_MAC_LEN);
  61. /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
  62. addr[0] = wps->snonce;
  63. len[0] = WPS_SECRET_NONCE_LEN;
  64. addr[1] = wps->psk1;
  65. len[1] = WPS_PSK_LEN;
  66. addr[2] = wpabuf_head(wps->dh_pubkey_e);
  67. len[2] = wpabuf_len(wps->dh_pubkey_e);
  68. addr[3] = wpabuf_head(wps->dh_pubkey_r);
  69. len[3] = wpabuf_len(wps->dh_pubkey_r);
  70. hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
  71. wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", hash, SHA256_MAC_LEN);
  72. wpa_printf(MSG_DEBUG, "WPS: * E-Hash2");
  73. wpabuf_put_be16(msg, ATTR_E_HASH2);
  74. wpabuf_put_be16(msg, SHA256_MAC_LEN);
  75. hash = wpabuf_put(msg, SHA256_MAC_LEN);
  76. /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
  77. addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
  78. addr[1] = wps->psk2;
  79. hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
  80. wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", hash, SHA256_MAC_LEN);
  81. return 0;
  82. }
  83. static int wps_build_e_snonce1(struct wps_data *wps, struct wpabuf *msg)
  84. {
  85. wpa_printf(MSG_DEBUG, "WPS: * E-SNonce1");
  86. wpabuf_put_be16(msg, ATTR_E_SNONCE1);
  87. wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
  88. wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
  89. return 0;
  90. }
  91. static int wps_build_e_snonce2(struct wps_data *wps, struct wpabuf *msg)
  92. {
  93. wpa_printf(MSG_DEBUG, "WPS: * E-SNonce2");
  94. wpabuf_put_be16(msg, ATTR_E_SNONCE2);
  95. wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
  96. wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
  97. WPS_SECRET_NONCE_LEN);
  98. return 0;
  99. }
  100. static struct wpabuf * wps_build_m1(struct wps_data *wps)
  101. {
  102. struct wpabuf *msg;
  103. if (os_get_random(wps->nonce_e, WPS_NONCE_LEN) < 0)
  104. return NULL;
  105. wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
  106. wps->nonce_e, WPS_NONCE_LEN);
  107. wpa_printf(MSG_DEBUG, "WPS: Building Message M1");
  108. msg = wpabuf_alloc(1000);
  109. if (msg == NULL)
  110. return NULL;
  111. if (wps_build_version(msg) ||
  112. wps_build_msg_type(msg, WPS_M1) ||
  113. wps_build_uuid_e(msg, wps->uuid_e) ||
  114. wps_build_mac_addr(wps, msg) ||
  115. wps_build_enrollee_nonce(wps, msg) ||
  116. wps_build_public_key(wps, msg) ||
  117. wps_build_auth_type_flags(wps, msg) ||
  118. wps_build_encr_type_flags(wps, msg) ||
  119. wps_build_conn_type_flags(wps, msg) ||
  120. wps_build_config_methods(msg, wps->wps->config_methods) ||
  121. wps_build_wps_state(wps, msg) ||
  122. wps_build_device_attrs(&wps->wps->dev, msg) ||
  123. wps_build_rf_bands(&wps->wps->dev, msg) ||
  124. wps_build_assoc_state(wps, msg) ||
  125. wps_build_dev_password_id(msg, wps->dev_pw_id) ||
  126. wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
  127. wps_build_os_version(&wps->wps->dev, msg) ||
  128. wps_build_wfa_ext(msg, 0, NULL, 0)) {
  129. wpabuf_free(msg);
  130. return NULL;
  131. }
  132. wps->state = RECV_M2;
  133. return msg;
  134. }
  135. static struct wpabuf * wps_build_m3(struct wps_data *wps)
  136. {
  137. struct wpabuf *msg;
  138. wpa_printf(MSG_DEBUG, "WPS: Building Message M3");
  139. if (wps->dev_password == NULL) {
  140. wpa_printf(MSG_DEBUG, "WPS: No Device Password available");
  141. return NULL;
  142. }
  143. wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
  144. msg = wpabuf_alloc(1000);
  145. if (msg == NULL)
  146. return NULL;
  147. if (wps_build_version(msg) ||
  148. wps_build_msg_type(msg, WPS_M3) ||
  149. wps_build_registrar_nonce(wps, msg) ||
  150. wps_build_e_hash(wps, msg) ||
  151. wps_build_wfa_ext(msg, 0, NULL, 0) ||
  152. wps_build_authenticator(wps, msg)) {
  153. wpabuf_free(msg);
  154. return NULL;
  155. }
  156. wps->state = RECV_M4;
  157. return msg;
  158. }
  159. static struct wpabuf * wps_build_m5(struct wps_data *wps)
  160. {
  161. struct wpabuf *msg, *plain;
  162. wpa_printf(MSG_DEBUG, "WPS: Building Message M5");
  163. plain = wpabuf_alloc(200);
  164. if (plain == NULL)
  165. return NULL;
  166. msg = wpabuf_alloc(1000);
  167. if (msg == NULL) {
  168. wpabuf_free(plain);
  169. return NULL;
  170. }
  171. if (wps_build_version(msg) ||
  172. wps_build_msg_type(msg, WPS_M5) ||
  173. wps_build_registrar_nonce(wps, msg) ||
  174. wps_build_e_snonce1(wps, plain) ||
  175. wps_build_key_wrap_auth(wps, plain) ||
  176. wps_build_encr_settings(wps, msg, plain) ||
  177. wps_build_wfa_ext(msg, 0, NULL, 0) ||
  178. wps_build_authenticator(wps, msg)) {
  179. wpabuf_free(plain);
  180. wpabuf_free(msg);
  181. return NULL;
  182. }
  183. wpabuf_free(plain);
  184. wps->state = RECV_M6;
  185. return msg;
  186. }
  187. static int wps_build_cred_ssid(struct wps_data *wps, struct wpabuf *msg)
  188. {
  189. wpa_printf(MSG_DEBUG, "WPS: * SSID");
  190. wpabuf_put_be16(msg, ATTR_SSID);
  191. wpabuf_put_be16(msg, wps->wps->ssid_len);
  192. wpabuf_put_data(msg, wps->wps->ssid, wps->wps->ssid_len);
  193. return 0;
  194. }
  195. static int wps_build_cred_auth_type(struct wps_data *wps, struct wpabuf *msg)
  196. {
  197. wpa_printf(MSG_DEBUG, "WPS: * Authentication Type");
  198. wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
  199. wpabuf_put_be16(msg, 2);
  200. wpabuf_put_be16(msg, wps->wps->auth_types);
  201. return 0;
  202. }
  203. static int wps_build_cred_encr_type(struct wps_data *wps, struct wpabuf *msg)
  204. {
  205. wpa_printf(MSG_DEBUG, "WPS: * Encryption Type");
  206. wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
  207. wpabuf_put_be16(msg, 2);
  208. wpabuf_put_be16(msg, wps->wps->encr_types);
  209. return 0;
  210. }
  211. static int wps_build_cred_network_key(struct wps_data *wps, struct wpabuf *msg)
  212. {
  213. wpa_printf(MSG_DEBUG, "WPS: * Network Key");
  214. wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
  215. wpabuf_put_be16(msg, wps->wps->network_key_len);
  216. wpabuf_put_data(msg, wps->wps->network_key, wps->wps->network_key_len);
  217. return 0;
  218. }
  219. static int wps_build_cred_mac_addr(struct wps_data *wps, struct wpabuf *msg)
  220. {
  221. wpa_printf(MSG_DEBUG, "WPS: * MAC Address (AP BSSID)");
  222. wpabuf_put_be16(msg, ATTR_MAC_ADDR);
  223. wpabuf_put_be16(msg, ETH_ALEN);
  224. wpabuf_put_data(msg, wps->wps->dev.mac_addr, ETH_ALEN);
  225. return 0;
  226. }
  227. static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *plain)
  228. {
  229. if (wps->wps->ap_settings) {
  230. wpa_printf(MSG_DEBUG, "WPS: * AP Settings (pre-configured)");
  231. wpabuf_put_data(plain, wps->wps->ap_settings,
  232. wps->wps->ap_settings_len);
  233. return 0;
  234. }
  235. return wps_build_cred_ssid(wps, plain) ||
  236. wps_build_cred_mac_addr(wps, plain) ||
  237. wps_build_cred_auth_type(wps, plain) ||
  238. wps_build_cred_encr_type(wps, plain) ||
  239. wps_build_cred_network_key(wps, plain);
  240. }
  241. static struct wpabuf * wps_build_m7(struct wps_data *wps)
  242. {
  243. struct wpabuf *msg, *plain;
  244. wpa_printf(MSG_DEBUG, "WPS: Building Message M7");
  245. plain = wpabuf_alloc(500 + wps->wps->ap_settings_len);
  246. if (plain == NULL)
  247. return NULL;
  248. msg = wpabuf_alloc(1000 + wps->wps->ap_settings_len);
  249. if (msg == NULL) {
  250. wpabuf_free(plain);
  251. return NULL;
  252. }
  253. if (wps_build_version(msg) ||
  254. wps_build_msg_type(msg, WPS_M7) ||
  255. wps_build_registrar_nonce(wps, msg) ||
  256. wps_build_e_snonce2(wps, plain) ||
  257. (wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
  258. wps_build_key_wrap_auth(wps, plain) ||
  259. wps_build_encr_settings(wps, msg, plain) ||
  260. wps_build_wfa_ext(msg, 0, NULL, 0) ||
  261. wps_build_authenticator(wps, msg)) {
  262. wpabuf_free(plain);
  263. wpabuf_free(msg);
  264. return NULL;
  265. }
  266. wpabuf_free(plain);
  267. if (wps->wps->ap && wps->wps->registrar) {
  268. /*
  269. * If the Registrar is only learning our current configuration,
  270. * it may not continue protocol run to successful completion.
  271. * Store information here to make sure it remains available.
  272. */
  273. wps_device_store(wps->wps->registrar, &wps->peer_dev,
  274. wps->uuid_r);
  275. }
  276. wps->state = RECV_M8;
  277. return msg;
  278. }
  279. static struct wpabuf * wps_build_wsc_done(struct wps_data *wps)
  280. {
  281. struct wpabuf *msg;
  282. wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_Done");
  283. msg = wpabuf_alloc(1000);
  284. if (msg == NULL)
  285. return NULL;
  286. if (wps_build_version(msg) ||
  287. wps_build_msg_type(msg, WPS_WSC_DONE) ||
  288. wps_build_enrollee_nonce(wps, msg) ||
  289. wps_build_registrar_nonce(wps, msg) ||
  290. wps_build_wfa_ext(msg, 0, NULL, 0)) {
  291. wpabuf_free(msg);
  292. return NULL;
  293. }
  294. if (wps->wps->ap)
  295. wps->state = RECV_ACK;
  296. else {
  297. wps_success_event(wps->wps);
  298. wps->state = WPS_FINISHED;
  299. }
  300. return msg;
  301. }
  302. static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
  303. {
  304. struct wpabuf *msg;
  305. wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
  306. msg = wpabuf_alloc(1000);
  307. if (msg == NULL)
  308. return NULL;
  309. if (wps_build_version(msg) ||
  310. wps_build_msg_type(msg, WPS_WSC_ACK) ||
  311. wps_build_enrollee_nonce(wps, msg) ||
  312. wps_build_registrar_nonce(wps, msg) ||
  313. wps_build_wfa_ext(msg, 0, NULL, 0)) {
  314. wpabuf_free(msg);
  315. return NULL;
  316. }
  317. return msg;
  318. }
  319. static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
  320. {
  321. struct wpabuf *msg;
  322. wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
  323. msg = wpabuf_alloc(1000);
  324. if (msg == NULL)
  325. return NULL;
  326. if (wps_build_version(msg) ||
  327. wps_build_msg_type(msg, WPS_WSC_NACK) ||
  328. wps_build_enrollee_nonce(wps, msg) ||
  329. wps_build_registrar_nonce(wps, msg) ||
  330. wps_build_config_error(msg, wps->config_error) ||
  331. wps_build_wfa_ext(msg, 0, NULL, 0)) {
  332. wpabuf_free(msg);
  333. return NULL;
  334. }
  335. return msg;
  336. }
  337. struct wpabuf * wps_enrollee_get_msg(struct wps_data *wps,
  338. enum wsc_op_code *op_code)
  339. {
  340. struct wpabuf *msg;
  341. switch (wps->state) {
  342. case SEND_M1:
  343. msg = wps_build_m1(wps);
  344. *op_code = WSC_MSG;
  345. break;
  346. case SEND_M3:
  347. msg = wps_build_m3(wps);
  348. *op_code = WSC_MSG;
  349. break;
  350. case SEND_M5:
  351. msg = wps_build_m5(wps);
  352. *op_code = WSC_MSG;
  353. break;
  354. case SEND_M7:
  355. msg = wps_build_m7(wps);
  356. *op_code = WSC_MSG;
  357. break;
  358. case RECEIVED_M2D:
  359. if (wps->wps->ap) {
  360. msg = wps_build_wsc_nack(wps);
  361. *op_code = WSC_NACK;
  362. break;
  363. }
  364. msg = wps_build_wsc_ack(wps);
  365. *op_code = WSC_ACK;
  366. if (msg) {
  367. /* Another M2/M2D may be received */
  368. wps->state = RECV_M2;
  369. }
  370. break;
  371. case SEND_WSC_NACK:
  372. msg = wps_build_wsc_nack(wps);
  373. *op_code = WSC_NACK;
  374. break;
  375. case WPS_MSG_DONE:
  376. msg = wps_build_wsc_done(wps);
  377. *op_code = WSC_Done;
  378. break;
  379. default:
  380. wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
  381. "a message", wps->state);
  382. msg = NULL;
  383. break;
  384. }
  385. if (*op_code == WSC_MSG && msg) {
  386. /* Save a copy of the last message for Authenticator derivation
  387. */
  388. wpabuf_free(wps->last_msg);
  389. wps->last_msg = wpabuf_dup(msg);
  390. }
  391. return msg;
  392. }
  393. static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
  394. {
  395. if (r_nonce == NULL) {
  396. wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
  397. return -1;
  398. }
  399. os_memcpy(wps->nonce_r, r_nonce, WPS_NONCE_LEN);
  400. wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
  401. wps->nonce_r, WPS_NONCE_LEN);
  402. return 0;
  403. }
  404. static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
  405. {
  406. if (e_nonce == NULL) {
  407. wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
  408. return -1;
  409. }
  410. if (os_memcmp(wps->nonce_e, e_nonce, WPS_NONCE_LEN) != 0) {
  411. wpa_printf(MSG_DEBUG, "WPS: Invalid Enrollee Nonce received");
  412. return -1;
  413. }
  414. return 0;
  415. }
  416. static int wps_process_uuid_r(struct wps_data *wps, const u8 *uuid_r)
  417. {
  418. if (uuid_r == NULL) {
  419. wpa_printf(MSG_DEBUG, "WPS: No UUID-R received");
  420. return -1;
  421. }
  422. os_memcpy(wps->uuid_r, uuid_r, WPS_UUID_LEN);
  423. wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
  424. return 0;
  425. }
  426. static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
  427. size_t pk_len)
  428. {
  429. if (pk == NULL || pk_len == 0) {
  430. wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
  431. return -1;
  432. }
  433. #ifdef CONFIG_WPS_OOB
  434. if (wps->dev_pw_id != DEV_PW_DEFAULT &&
  435. wps->wps->oob_conf.pubkey_hash) {
  436. const u8 *addr[1];
  437. u8 hash[WPS_HASH_LEN];
  438. addr[0] = pk;
  439. sha256_vector(1, addr, &pk_len, hash);
  440. if (os_memcmp(hash,
  441. wpabuf_head(wps->wps->oob_conf.pubkey_hash),
  442. WPS_OOB_PUBKEY_HASH_LEN) != 0) {
  443. wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
  444. return -1;
  445. }
  446. }
  447. #endif /* CONFIG_WPS_OOB */
  448. wpabuf_free(wps->dh_pubkey_r);
  449. wps->dh_pubkey_r = wpabuf_alloc_copy(pk, pk_len);
  450. if (wps->dh_pubkey_r == NULL)
  451. return -1;
  452. if (wps_derive_keys(wps) < 0)
  453. return -1;
  454. return 0;
  455. }
  456. static int wps_process_r_hash1(struct wps_data *wps, const u8 *r_hash1)
  457. {
  458. if (r_hash1 == NULL) {
  459. wpa_printf(MSG_DEBUG, "WPS: No R-Hash1 received");
  460. return -1;
  461. }
  462. os_memcpy(wps->peer_hash1, r_hash1, WPS_HASH_LEN);
  463. wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", wps->peer_hash1, WPS_HASH_LEN);
  464. return 0;
  465. }
  466. static int wps_process_r_hash2(struct wps_data *wps, const u8 *r_hash2)
  467. {
  468. if (r_hash2 == NULL) {
  469. wpa_printf(MSG_DEBUG, "WPS: No R-Hash2 received");
  470. return -1;
  471. }
  472. os_memcpy(wps->peer_hash2, r_hash2, WPS_HASH_LEN);
  473. wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", wps->peer_hash2, WPS_HASH_LEN);
  474. return 0;
  475. }
  476. static int wps_process_r_snonce1(struct wps_data *wps, const u8 *r_snonce1)
  477. {
  478. u8 hash[SHA256_MAC_LEN];
  479. const u8 *addr[4];
  480. size_t len[4];
  481. if (r_snonce1 == NULL) {
  482. wpa_printf(MSG_DEBUG, "WPS: No R-SNonce1 received");
  483. return -1;
  484. }
  485. wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce1", r_snonce1,
  486. WPS_SECRET_NONCE_LEN);
  487. /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
  488. addr[0] = r_snonce1;
  489. len[0] = WPS_SECRET_NONCE_LEN;
  490. addr[1] = wps->psk1;
  491. len[1] = WPS_PSK_LEN;
  492. addr[2] = wpabuf_head(wps->dh_pubkey_e);
  493. len[2] = wpabuf_len(wps->dh_pubkey_e);
  494. addr[3] = wpabuf_head(wps->dh_pubkey_r);
  495. len[3] = wpabuf_len(wps->dh_pubkey_r);
  496. hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
  497. if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
  498. wpa_printf(MSG_DEBUG, "WPS: R-Hash1 derived from R-S1 does "
  499. "not match with the pre-committed value");
  500. wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
  501. wps_pwd_auth_fail_event(wps->wps, 1, 1);
  502. return -1;
  503. }
  504. wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the first "
  505. "half of the device password");
  506. return 0;
  507. }
  508. static int wps_process_r_snonce2(struct wps_data *wps, const u8 *r_snonce2)
  509. {
  510. u8 hash[SHA256_MAC_LEN];
  511. const u8 *addr[4];
  512. size_t len[4];
  513. if (r_snonce2 == NULL) {
  514. wpa_printf(MSG_DEBUG, "WPS: No R-SNonce2 received");
  515. return -1;
  516. }
  517. wpa_hexdump_key(MSG_DEBUG, "WPS: R-SNonce2", r_snonce2,
  518. WPS_SECRET_NONCE_LEN);
  519. /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
  520. addr[0] = r_snonce2;
  521. len[0] = WPS_SECRET_NONCE_LEN;
  522. addr[1] = wps->psk2;
  523. len[1] = WPS_PSK_LEN;
  524. addr[2] = wpabuf_head(wps->dh_pubkey_e);
  525. len[2] = wpabuf_len(wps->dh_pubkey_e);
  526. addr[3] = wpabuf_head(wps->dh_pubkey_r);
  527. len[3] = wpabuf_len(wps->dh_pubkey_r);
  528. hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
  529. if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
  530. wpa_printf(MSG_DEBUG, "WPS: R-Hash2 derived from R-S2 does "
  531. "not match with the pre-committed value");
  532. wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
  533. wps_pwd_auth_fail_event(wps->wps, 1, 2);
  534. return -1;
  535. }
  536. wpa_printf(MSG_DEBUG, "WPS: Registrar proved knowledge of the second "
  537. "half of the device password");
  538. return 0;
  539. }
  540. static int wps_process_cred_e(struct wps_data *wps, const u8 *cred,
  541. size_t cred_len, int wps2)
  542. {
  543. struct wps_parse_attr attr;
  544. struct wpabuf msg;
  545. wpa_printf(MSG_DEBUG, "WPS: Received Credential");
  546. os_memset(&wps->cred, 0, sizeof(wps->cred));
  547. wpabuf_set(&msg, cred, cred_len);
  548. if (wps_parse_msg(&msg, &attr) < 0 ||
  549. wps_process_cred(&attr, &wps->cred))
  550. return -1;
  551. if (os_memcmp(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) !=
  552. 0) {
  553. wpa_printf(MSG_DEBUG, "WPS: MAC Address in the Credential ("
  554. MACSTR ") does not match with own address (" MACSTR
  555. ")", MAC2STR(wps->cred.mac_addr),
  556. MAC2STR(wps->wps->dev.mac_addr));
  557. /*
  558. * In theory, this could be consider fatal error, but there are
  559. * number of deployed implementations using other address here
  560. * due to unclarity in the specification. For interoperability
  561. * reasons, allow this to be processed since we do not really
  562. * use the MAC Address information for anything.
  563. */
  564. #ifdef CONFIG_WPS_STRICT
  565. if (wps2) {
  566. wpa_printf(MSG_INFO, "WPS: Do not accept incorrect "
  567. "MAC Address in AP Settings");
  568. return -1;
  569. }
  570. #endif /* CONFIG_WPS_STRICT */
  571. }
  572. #ifdef CONFIG_WPS2
  573. if (!(wps->cred.encr_type &
  574. (WPS_ENCR_NONE | WPS_ENCR_TKIP | WPS_ENCR_AES))) {
  575. if (wps->cred.encr_type & WPS_ENCR_WEP) {
  576. wpa_printf(MSG_INFO, "WPS: Reject Credential "
  577. "due to WEP configuration");
  578. return -2;
  579. }
  580. wpa_printf(MSG_INFO, "WPS: Reject Credential due to "
  581. "invalid encr_type 0x%x", wps->cred.encr_type);
  582. return -1;
  583. }
  584. #endif /* CONFIG_WPS2 */
  585. if (wps->wps->cred_cb) {
  586. wps->cred.cred_attr = cred - 4;
  587. wps->cred.cred_attr_len = cred_len + 4;
  588. wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
  589. wps->cred.cred_attr = NULL;
  590. wps->cred.cred_attr_len = 0;
  591. }
  592. return 0;
  593. }
  594. static int wps_process_creds(struct wps_data *wps, const u8 *cred[],
  595. size_t cred_len[], size_t num_cred, int wps2)
  596. {
  597. size_t i;
  598. int ok = 0;
  599. if (wps->wps->ap)
  600. return 0;
  601. if (num_cred == 0) {
  602. wpa_printf(MSG_DEBUG, "WPS: No Credential attributes "
  603. "received");
  604. return -1;
  605. }
  606. for (i = 0; i < num_cred; i++) {
  607. int res;
  608. res = wps_process_cred_e(wps, cred[i], cred_len[i], wps2);
  609. if (res == 0)
  610. ok++;
  611. else if (res == -2)
  612. wpa_printf(MSG_DEBUG, "WPS: WEP credential skipped");
  613. else
  614. return -1;
  615. }
  616. if (ok == 0) {
  617. wpa_printf(MSG_DEBUG, "WPS: No valid Credential attribute "
  618. "received");
  619. return -1;
  620. }
  621. return 0;
  622. }
  623. static int wps_process_ap_settings_e(struct wps_data *wps,
  624. struct wps_parse_attr *attr,
  625. struct wpabuf *attrs, int wps2)
  626. {
  627. struct wps_credential cred;
  628. if (!wps->wps->ap)
  629. return 0;
  630. if (wps_process_ap_settings(attr, &cred) < 0)
  631. return -1;
  632. wpa_printf(MSG_INFO, "WPS: Received new AP configuration from "
  633. "Registrar");
  634. if (os_memcmp(cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN) !=
  635. 0) {
  636. wpa_printf(MSG_DEBUG, "WPS: MAC Address in the AP Settings ("
  637. MACSTR ") does not match with own address (" MACSTR
  638. ")", MAC2STR(cred.mac_addr),
  639. MAC2STR(wps->wps->dev.mac_addr));
  640. /*
  641. * In theory, this could be consider fatal error, but there are
  642. * number of deployed implementations using other address here
  643. * due to unclarity in the specification. For interoperability
  644. * reasons, allow this to be processed since we do not really
  645. * use the MAC Address information for anything.
  646. */
  647. #ifdef CONFIG_WPS_STRICT
  648. if (wps2) {
  649. wpa_printf(MSG_INFO, "WPS: Do not accept incorrect "
  650. "MAC Address in AP Settings");
  651. return -1;
  652. }
  653. #endif /* CONFIG_WPS_STRICT */
  654. }
  655. #ifdef CONFIG_WPS2
  656. if (!(cred.encr_type & (WPS_ENCR_NONE | WPS_ENCR_TKIP | WPS_ENCR_AES)))
  657. {
  658. if (cred.encr_type & WPS_ENCR_WEP) {
  659. wpa_printf(MSG_INFO, "WPS: Reject new AP settings "
  660. "due to WEP configuration");
  661. return -1;
  662. }
  663. wpa_printf(MSG_INFO, "WPS: Reject new AP settings due to "
  664. "invalid encr_type 0x%x", cred.encr_type);
  665. return -1;
  666. }
  667. #endif /* CONFIG_WPS2 */
  668. #ifdef CONFIG_WPS_STRICT
  669. if (wps2) {
  670. if ((cred.encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) ==
  671. WPS_ENCR_TKIP ||
  672. (cred.auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
  673. WPS_AUTH_WPAPSK) {
  674. wpa_printf(MSG_INFO, "WPS-STRICT: Invalid WSC 2.0 "
  675. "AP Settings: WPA-Personal/TKIP only");
  676. return -1;
  677. }
  678. }
  679. #endif /* CONFIG_WPS_STRICT */
  680. #ifdef CONFIG_WPS2
  681. if ((cred.encr_type & (WPS_ENCR_TKIP | WPS_ENCR_AES)) == WPS_ENCR_TKIP)
  682. {
  683. wpa_printf(MSG_DEBUG, "WPS: Upgrade encr_type TKIP -> "
  684. "TKIP+AES");
  685. cred.encr_type |= WPS_ENCR_AES;
  686. }
  687. if ((cred.auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) ==
  688. WPS_AUTH_WPAPSK) {
  689. wpa_printf(MSG_DEBUG, "WPS: Upgrade auth_type WPAPSK -> "
  690. "WPAPSK+WPA2PSK");
  691. cred.auth_type |= WPS_AUTH_WPA2PSK;
  692. }
  693. #endif /* CONFIG_WPS2 */
  694. if (wps->wps->cred_cb) {
  695. cred.cred_attr = wpabuf_head(attrs);
  696. cred.cred_attr_len = wpabuf_len(attrs);
  697. wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
  698. }
  699. return 0;
  700. }
  701. static enum wps_process_res wps_process_m2(struct wps_data *wps,
  702. const struct wpabuf *msg,
  703. struct wps_parse_attr *attr)
  704. {
  705. wpa_printf(MSG_DEBUG, "WPS: Received M2");
  706. if (wps->state != RECV_M2) {
  707. wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
  708. "receiving M2", wps->state);
  709. wps->state = SEND_WSC_NACK;
  710. return WPS_CONTINUE;
  711. }
  712. if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
  713. wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
  714. wps_process_uuid_r(wps, attr->uuid_r)) {
  715. wps->state = SEND_WSC_NACK;
  716. return WPS_CONTINUE;
  717. }
  718. if (wps->wps->ap &&
  719. (wps->wps->ap_setup_locked || wps->dev_password == NULL)) {
  720. wpa_printf(MSG_DEBUG, "WPS: AP Setup is locked - refuse "
  721. "registration of a new Registrar");
  722. wps->config_error = WPS_CFG_SETUP_LOCKED;
  723. wps->state = SEND_WSC_NACK;
  724. return WPS_CONTINUE;
  725. }
  726. if (wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
  727. wps_process_authenticator(wps, attr->authenticator, msg) ||
  728. wps_process_device_attrs(&wps->peer_dev, attr)) {
  729. wps->state = SEND_WSC_NACK;
  730. return WPS_CONTINUE;
  731. }
  732. wps->state = SEND_M3;
  733. return WPS_CONTINUE;
  734. }
  735. static enum wps_process_res wps_process_m2d(struct wps_data *wps,
  736. struct wps_parse_attr *attr)
  737. {
  738. wpa_printf(MSG_DEBUG, "WPS: Received M2D");
  739. if (wps->state != RECV_M2) {
  740. wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
  741. "receiving M2D", wps->state);
  742. wps->state = SEND_WSC_NACK;
  743. return WPS_CONTINUE;
  744. }
  745. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Manufacturer",
  746. attr->manufacturer, attr->manufacturer_len);
  747. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Name",
  748. attr->model_name, attr->model_name_len);
  749. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Model Number",
  750. attr->model_number, attr->model_number_len);
  751. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Serial Number",
  752. attr->serial_number, attr->serial_number_len);
  753. wpa_hexdump_ascii(MSG_DEBUG, "WPS: Device Name",
  754. attr->dev_name, attr->dev_name_len);
  755. if (wps->wps->event_cb) {
  756. union wps_event_data data;
  757. struct wps_event_m2d *m2d = &data.m2d;
  758. os_memset(&data, 0, sizeof(data));
  759. if (attr->config_methods)
  760. m2d->config_methods =
  761. WPA_GET_BE16(attr->config_methods);
  762. m2d->manufacturer = attr->manufacturer;
  763. m2d->manufacturer_len = attr->manufacturer_len;
  764. m2d->model_name = attr->model_name;
  765. m2d->model_name_len = attr->model_name_len;
  766. m2d->model_number = attr->model_number;
  767. m2d->model_number_len = attr->model_number_len;
  768. m2d->serial_number = attr->serial_number;
  769. m2d->serial_number_len = attr->serial_number_len;
  770. m2d->dev_name = attr->dev_name;
  771. m2d->dev_name_len = attr->dev_name_len;
  772. m2d->primary_dev_type = attr->primary_dev_type;
  773. if (attr->config_error)
  774. m2d->config_error =
  775. WPA_GET_BE16(attr->config_error);
  776. if (attr->dev_password_id)
  777. m2d->dev_password_id =
  778. WPA_GET_BE16(attr->dev_password_id);
  779. wps->wps->event_cb(wps->wps->cb_ctx, WPS_EV_M2D, &data);
  780. }
  781. wps->state = RECEIVED_M2D;
  782. return WPS_CONTINUE;
  783. }
  784. static enum wps_process_res wps_process_m4(struct wps_data *wps,
  785. const struct wpabuf *msg,
  786. struct wps_parse_attr *attr)
  787. {
  788. struct wpabuf *decrypted;
  789. struct wps_parse_attr eattr;
  790. wpa_printf(MSG_DEBUG, "WPS: Received M4");
  791. if (wps->state != RECV_M4) {
  792. wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
  793. "receiving M4", wps->state);
  794. wps->state = SEND_WSC_NACK;
  795. return WPS_CONTINUE;
  796. }
  797. if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
  798. wps_process_authenticator(wps, attr->authenticator, msg) ||
  799. wps_process_r_hash1(wps, attr->r_hash1) ||
  800. wps_process_r_hash2(wps, attr->r_hash2)) {
  801. wps->state = SEND_WSC_NACK;
  802. return WPS_CONTINUE;
  803. }
  804. decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
  805. attr->encr_settings_len);
  806. if (decrypted == NULL) {
  807. wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
  808. "Settings attribute");
  809. wps->state = SEND_WSC_NACK;
  810. return WPS_CONTINUE;
  811. }
  812. if (wps_validate_m4_encr(decrypted, attr->version2 != 0) < 0) {
  813. wpabuf_free(decrypted);
  814. wps->state = SEND_WSC_NACK;
  815. return WPS_CONTINUE;
  816. }
  817. wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
  818. "attribute");
  819. if (wps_parse_msg(decrypted, &eattr) < 0 ||
  820. wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
  821. wps_process_r_snonce1(wps, eattr.r_snonce1)) {
  822. wpabuf_free(decrypted);
  823. wps->state = SEND_WSC_NACK;
  824. return WPS_CONTINUE;
  825. }
  826. wpabuf_free(decrypted);
  827. wps->state = SEND_M5;
  828. return WPS_CONTINUE;
  829. }
  830. static enum wps_process_res wps_process_m6(struct wps_data *wps,
  831. const struct wpabuf *msg,
  832. struct wps_parse_attr *attr)
  833. {
  834. struct wpabuf *decrypted;
  835. struct wps_parse_attr eattr;
  836. wpa_printf(MSG_DEBUG, "WPS: Received M6");
  837. if (wps->state != RECV_M6) {
  838. wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
  839. "receiving M6", wps->state);
  840. wps->state = SEND_WSC_NACK;
  841. return WPS_CONTINUE;
  842. }
  843. if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
  844. wps_process_authenticator(wps, attr->authenticator, msg)) {
  845. wps->state = SEND_WSC_NACK;
  846. return WPS_CONTINUE;
  847. }
  848. decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
  849. attr->encr_settings_len);
  850. if (decrypted == NULL) {
  851. wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
  852. "Settings attribute");
  853. wps->state = SEND_WSC_NACK;
  854. return WPS_CONTINUE;
  855. }
  856. if (wps_validate_m6_encr(decrypted, attr->version2 != 0) < 0) {
  857. wpabuf_free(decrypted);
  858. wps->state = SEND_WSC_NACK;
  859. return WPS_CONTINUE;
  860. }
  861. wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
  862. "attribute");
  863. if (wps_parse_msg(decrypted, &eattr) < 0 ||
  864. wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
  865. wps_process_r_snonce2(wps, eattr.r_snonce2)) {
  866. wpabuf_free(decrypted);
  867. wps->state = SEND_WSC_NACK;
  868. return WPS_CONTINUE;
  869. }
  870. wpabuf_free(decrypted);
  871. wps->state = SEND_M7;
  872. return WPS_CONTINUE;
  873. }
  874. static enum wps_process_res wps_process_m8(struct wps_data *wps,
  875. const struct wpabuf *msg,
  876. struct wps_parse_attr *attr)
  877. {
  878. struct wpabuf *decrypted;
  879. struct wps_parse_attr eattr;
  880. wpa_printf(MSG_DEBUG, "WPS: Received M8");
  881. if (wps->state != RECV_M8) {
  882. wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
  883. "receiving M8", wps->state);
  884. wps->state = SEND_WSC_NACK;
  885. return WPS_CONTINUE;
  886. }
  887. if (wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
  888. wps_process_authenticator(wps, attr->authenticator, msg)) {
  889. wps->state = SEND_WSC_NACK;
  890. return WPS_CONTINUE;
  891. }
  892. decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
  893. attr->encr_settings_len);
  894. if (decrypted == NULL) {
  895. wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
  896. "Settings attribute");
  897. wps->state = SEND_WSC_NACK;
  898. return WPS_CONTINUE;
  899. }
  900. if (wps_validate_m8_encr(decrypted, wps->wps->ap, attr->version2 != 0)
  901. < 0) {
  902. wpabuf_free(decrypted);
  903. wps->state = SEND_WSC_NACK;
  904. return WPS_CONTINUE;
  905. }
  906. wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
  907. "attribute");
  908. if (wps_parse_msg(decrypted, &eattr) < 0 ||
  909. wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
  910. wps_process_creds(wps, eattr.cred, eattr.cred_len,
  911. eattr.num_cred, attr->version2 != NULL) ||
  912. wps_process_ap_settings_e(wps, &eattr, decrypted,
  913. attr->version2 != NULL)) {
  914. wpabuf_free(decrypted);
  915. wps->state = SEND_WSC_NACK;
  916. return WPS_CONTINUE;
  917. }
  918. wpabuf_free(decrypted);
  919. wps->state = WPS_MSG_DONE;
  920. return WPS_CONTINUE;
  921. }
  922. static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
  923. const struct wpabuf *msg)
  924. {
  925. struct wps_parse_attr attr;
  926. enum wps_process_res ret = WPS_CONTINUE;
  927. wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
  928. if (wps_parse_msg(msg, &attr) < 0)
  929. return WPS_FAILURE;
  930. if (attr.enrollee_nonce == NULL ||
  931. os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
  932. wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
  933. return WPS_FAILURE;
  934. }
  935. if (attr.msg_type == NULL) {
  936. wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
  937. return WPS_FAILURE;
  938. }
  939. switch (*attr.msg_type) {
  940. case WPS_M2:
  941. if (wps_validate_m2(msg) < 0)
  942. return WPS_FAILURE;
  943. ret = wps_process_m2(wps, msg, &attr);
  944. break;
  945. case WPS_M2D:
  946. if (wps_validate_m2d(msg) < 0)
  947. return WPS_FAILURE;
  948. ret = wps_process_m2d(wps, &attr);
  949. break;
  950. case WPS_M4:
  951. if (wps_validate_m4(msg) < 0)
  952. return WPS_FAILURE;
  953. ret = wps_process_m4(wps, msg, &attr);
  954. if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
  955. wps_fail_event(wps->wps, WPS_M4, wps->config_error);
  956. break;
  957. case WPS_M6:
  958. if (wps_validate_m6(msg) < 0)
  959. return WPS_FAILURE;
  960. ret = wps_process_m6(wps, msg, &attr);
  961. if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
  962. wps_fail_event(wps->wps, WPS_M6, wps->config_error);
  963. break;
  964. case WPS_M8:
  965. if (wps_validate_m8(msg) < 0)
  966. return WPS_FAILURE;
  967. ret = wps_process_m8(wps, msg, &attr);
  968. if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
  969. wps_fail_event(wps->wps, WPS_M8, wps->config_error);
  970. break;
  971. default:
  972. wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
  973. *attr.msg_type);
  974. return WPS_FAILURE;
  975. }
  976. /*
  977. * Save a copy of the last message for Authenticator derivation if we
  978. * are continuing. However, skip M2D since it is not authenticated and
  979. * neither is the ACK/NACK response frame. This allows the possibly
  980. * following M2 to be processed correctly by using the previously sent
  981. * M1 in Authenticator derivation.
  982. */
  983. if (ret == WPS_CONTINUE && *attr.msg_type != WPS_M2D) {
  984. /* Save a copy of the last message for Authenticator derivation
  985. */
  986. wpabuf_free(wps->last_msg);
  987. wps->last_msg = wpabuf_dup(msg);
  988. }
  989. return ret;
  990. }
  991. static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
  992. const struct wpabuf *msg)
  993. {
  994. struct wps_parse_attr attr;
  995. wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
  996. if (wps_parse_msg(msg, &attr) < 0)
  997. return WPS_FAILURE;
  998. if (attr.msg_type == NULL) {
  999. wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
  1000. return WPS_FAILURE;
  1001. }
  1002. if (*attr.msg_type != WPS_WSC_ACK) {
  1003. wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
  1004. *attr.msg_type);
  1005. return WPS_FAILURE;
  1006. }
  1007. if (attr.registrar_nonce == NULL ||
  1008. os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
  1009. {
  1010. wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
  1011. return WPS_FAILURE;
  1012. }
  1013. if (attr.enrollee_nonce == NULL ||
  1014. os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
  1015. wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
  1016. return WPS_FAILURE;
  1017. }
  1018. if (wps->state == RECV_ACK && wps->wps->ap) {
  1019. wpa_printf(MSG_DEBUG, "WPS: External Registrar registration "
  1020. "completed successfully");
  1021. wps_success_event(wps->wps);
  1022. wps->state = WPS_FINISHED;
  1023. return WPS_DONE;
  1024. }
  1025. return WPS_FAILURE;
  1026. }
  1027. static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
  1028. const struct wpabuf *msg)
  1029. {
  1030. struct wps_parse_attr attr;
  1031. u16 config_error;
  1032. wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
  1033. if (wps_parse_msg(msg, &attr) < 0)
  1034. return WPS_FAILURE;
  1035. if (attr.msg_type == NULL) {
  1036. wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
  1037. return WPS_FAILURE;
  1038. }
  1039. if (*attr.msg_type != WPS_WSC_NACK) {
  1040. wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
  1041. *attr.msg_type);
  1042. return WPS_FAILURE;
  1043. }
  1044. if (attr.registrar_nonce == NULL ||
  1045. os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
  1046. {
  1047. wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
  1048. wpa_hexdump(MSG_DEBUG, "WPS: Received Registrar Nonce",
  1049. attr.registrar_nonce, WPS_NONCE_LEN);
  1050. wpa_hexdump(MSG_DEBUG, "WPS: Expected Registrar Nonce",
  1051. wps->nonce_r, WPS_NONCE_LEN);
  1052. return WPS_FAILURE;
  1053. }
  1054. if (attr.enrollee_nonce == NULL ||
  1055. os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
  1056. wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
  1057. wpa_hexdump(MSG_DEBUG, "WPS: Received Enrollee Nonce",
  1058. attr.enrollee_nonce, WPS_NONCE_LEN);
  1059. wpa_hexdump(MSG_DEBUG, "WPS: Expected Enrollee Nonce",
  1060. wps->nonce_e, WPS_NONCE_LEN);
  1061. return WPS_FAILURE;
  1062. }
  1063. if (attr.config_error == NULL) {
  1064. wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
  1065. "in WSC_NACK");
  1066. return WPS_FAILURE;
  1067. }
  1068. config_error = WPA_GET_BE16(attr.config_error);
  1069. wpa_printf(MSG_DEBUG, "WPS: Registrar terminated negotiation with "
  1070. "Configuration Error %d", config_error);
  1071. switch (wps->state) {
  1072. case RECV_M4:
  1073. wps_fail_event(wps->wps, WPS_M3, config_error);
  1074. break;
  1075. case RECV_M6:
  1076. wps_fail_event(wps->wps, WPS_M5, config_error);
  1077. break;
  1078. case RECV_M8:
  1079. wps_fail_event(wps->wps, WPS_M7, config_error);
  1080. break;
  1081. default:
  1082. break;
  1083. }
  1084. /* Followed by NACK if Enrollee is Supplicant or EAP-Failure if
  1085. * Enrollee is Authenticator */
  1086. wps->state = SEND_WSC_NACK;
  1087. return WPS_FAILURE;
  1088. }
  1089. enum wps_process_res wps_enrollee_process_msg(struct wps_data *wps,
  1090. enum wsc_op_code op_code,
  1091. const struct wpabuf *msg)
  1092. {
  1093. wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
  1094. "op_code=%d)",
  1095. (unsigned long) wpabuf_len(msg), op_code);
  1096. if (op_code == WSC_UPnP) {
  1097. /* Determine the OpCode based on message type attribute */
  1098. struct wps_parse_attr attr;
  1099. if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type) {
  1100. if (*attr.msg_type == WPS_WSC_ACK)
  1101. op_code = WSC_ACK;
  1102. else if (*attr.msg_type == WPS_WSC_NACK)
  1103. op_code = WSC_NACK;
  1104. }
  1105. }
  1106. switch (op_code) {
  1107. case WSC_MSG:
  1108. case WSC_UPnP:
  1109. return wps_process_wsc_msg(wps, msg);
  1110. case WSC_ACK:
  1111. if (wps_validate_wsc_ack(msg) < 0)
  1112. return WPS_FAILURE;
  1113. return wps_process_wsc_ack(wps, msg);
  1114. case WSC_NACK:
  1115. if (wps_validate_wsc_nack(msg) < 0)
  1116. return WPS_FAILURE;
  1117. return wps_process_wsc_nack(wps, msg);
  1118. default:
  1119. wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
  1120. return WPS_FAILURE;
  1121. }
  1122. }