wps_enrollee.c 40 KB

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