12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114 |
- /*
- * Wi-Fi Protected Setup - Registrar
- * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License version 2 as
- * published by the Free Software Foundation.
- *
- * Alternatively, this software may be distributed under the terms of BSD
- * license.
- *
- * See README and COPYING for more details.
- */
- #include "includes.h"
- #include "common.h"
- #include "sha256.h"
- #include "base64.h"
- #include "ieee802_11_defs.h"
- #include "eloop.h"
- #include "wps_i.h"
- #include "wps_dev_attr.h"
- struct wps_uuid_pin {
- struct wps_uuid_pin *next;
- u8 uuid[WPS_UUID_LEN];
- int wildcard_uuid;
- u8 *pin;
- size_t pin_len;
- int locked;
- };
- static void wps_free_pin(struct wps_uuid_pin *pin)
- {
- os_free(pin->pin);
- os_free(pin);
- }
- static void wps_free_pins(struct wps_uuid_pin *pins)
- {
- struct wps_uuid_pin *pin, *prev;
- pin = pins;
- while (pin) {
- prev = pin;
- pin = pin->next;
- wps_free_pin(prev);
- }
- }
- struct wps_pbc_session {
- struct wps_pbc_session *next;
- u8 addr[ETH_ALEN];
- u8 uuid_e[WPS_UUID_LEN];
- struct os_time timestamp;
- };
- static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
- {
- struct wps_pbc_session *prev;
- while (pbc) {
- prev = pbc;
- pbc = pbc->next;
- os_free(prev);
- }
- }
- struct wps_registrar {
- struct wps_context *wps;
- int pbc;
- int selected_registrar;
- int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
- size_t psk_len);
- int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
- const u8 *probe_resp_ie, size_t probe_resp_ie_len);
- void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
- const struct wps_device_data *dev);
- void *cb_ctx;
- struct wps_uuid_pin *pins;
- struct wps_pbc_session *pbc_sessions;
- };
- static int wps_set_ie(struct wps_registrar *reg);
- static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
- static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
- const u8 *addr, const u8 *uuid_e)
- {
- struct wps_pbc_session *pbc, *prev = NULL;
- struct os_time now;
- os_get_time(&now);
- pbc = reg->pbc_sessions;
- while (pbc) {
- if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
- os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
- if (prev)
- prev->next = pbc->next;
- else
- reg->pbc_sessions = pbc->next;
- break;
- }
- prev = pbc;
- pbc = pbc->next;
- }
- if (!pbc) {
- pbc = os_zalloc(sizeof(*pbc));
- if (pbc == NULL)
- return;
- os_memcpy(pbc->addr, addr, ETH_ALEN);
- if (uuid_e)
- os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
- }
- pbc->next = reg->pbc_sessions;
- reg->pbc_sessions = pbc;
- pbc->timestamp = now;
- /* remove entries that have timed out */
- prev = pbc;
- pbc = pbc->next;
- while (pbc) {
- if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
- prev->next = NULL;
- wps_free_pbc_sessions(pbc);
- break;
- }
- prev = pbc;
- pbc = pbc->next;
- }
- }
- static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
- const u8 *addr, const u8 *uuid_e)
- {
- struct wps_pbc_session *pbc, *prev = NULL;
- pbc = reg->pbc_sessions;
- while (pbc) {
- if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
- os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
- if (prev)
- prev->next = pbc->next;
- else
- reg->pbc_sessions = pbc->next;
- os_free(pbc);
- break;
- }
- prev = pbc;
- pbc = pbc->next;
- }
- }
- int wps_registrar_pbc_overlap(struct wps_registrar *reg,
- const u8 *addr, const u8 *uuid_e)
- {
- int count = 0;
- struct wps_pbc_session *pbc;
- struct os_time now;
- os_get_time(&now);
- for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
- if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
- break;
- if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
- uuid_e == NULL ||
- os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
- count++;
- }
- if (addr || uuid_e)
- count++;
- return count > 1 ? 1 : 0;
- }
- static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
- {
- wpa_printf(MSG_DEBUG, "WPS: * Wi-Fi Protected Setup State (%d)",
- wps->wps_state);
- wpabuf_put_be16(msg, ATTR_WPS_STATE);
- wpabuf_put_be16(msg, 1);
- wpabuf_put_u8(msg, wps->wps_state);
- return 0;
- }
- static int wps_build_ap_setup_locked(struct wps_context *wps,
- struct wpabuf *msg)
- {
- if (wps->ap_setup_locked) {
- wpa_printf(MSG_DEBUG, "WPS: * AP Setup Locked");
- wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
- wpabuf_put_be16(msg, 1);
- wpabuf_put_u8(msg, 1);
- }
- return 0;
- }
- static int wps_build_selected_registrar(struct wps_registrar *reg,
- struct wpabuf *msg)
- {
- if (!reg->selected_registrar)
- return 0;
- wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar");
- wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
- wpabuf_put_be16(msg, 1);
- wpabuf_put_u8(msg, 1);
- return 0;
- }
- static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
- struct wpabuf *msg)
- {
- u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
- if (!reg->selected_registrar)
- return 0;
- wpa_printf(MSG_DEBUG, "WPS: * Device Password ID (%d)", id);
- wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
- wpabuf_put_be16(msg, 2);
- wpabuf_put_be16(msg, id);
- return 0;
- }
- static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
- struct wpabuf *msg)
- {
- u16 methods;
- if (!reg->selected_registrar)
- return 0;
- methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
- if (reg->pbc)
- methods |= WPS_CONFIG_PUSHBUTTON;
- wpa_printf(MSG_DEBUG, "WPS: * Selected Registrar Config Methods (%x)",
- methods);
- wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
- wpabuf_put_be16(msg, 2);
- wpabuf_put_be16(msg, methods);
- return 0;
- }
- static int wps_build_probe_config_methods(struct wps_registrar *reg,
- struct wpabuf *msg)
- {
- u16 methods;
- methods = 0;
- wpa_printf(MSG_DEBUG, "WPS: * Config Methods (%x)", methods);
- wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
- wpabuf_put_be16(msg, 2);
- wpabuf_put_be16(msg, methods);
- return 0;
- }
- static int wps_build_config_methods_r(struct wps_registrar *reg,
- struct wpabuf *msg)
- {
- u16 methods;
- methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
- if (reg->pbc)
- methods |= WPS_CONFIG_PUSHBUTTON;
- return wps_build_config_methods(msg, methods);
- }
- static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
- {
- u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
- wpa_printf(MSG_DEBUG, "WPS: * Response Type (%d)", resp);
- wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
- wpabuf_put_be16(msg, 1);
- wpabuf_put_u8(msg, resp);
- return 0;
- }
- struct wps_registrar *
- wps_registrar_init(struct wps_context *wps,
- const struct wps_registrar_config *cfg)
- {
- struct wps_registrar *reg = os_zalloc(sizeof(*reg));
- if (reg == NULL)
- return NULL;
- reg->wps = wps;
- reg->new_psk_cb = cfg->new_psk_cb;
- reg->set_ie_cb = cfg->set_ie_cb;
- reg->pin_needed_cb = cfg->pin_needed_cb;
- reg->cb_ctx = cfg->cb_ctx;
- if (wps_set_ie(reg)) {
- wps_registrar_deinit(reg);
- return NULL;
- }
- return reg;
- }
- void wps_registrar_deinit(struct wps_registrar *reg)
- {
- if (reg == NULL)
- return;
- eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
- wps_free_pins(reg->pins);
- wps_free_pbc_sessions(reg->pbc_sessions);
- os_free(reg);
- }
- int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
- const u8 *pin, size_t pin_len)
- {
- struct wps_uuid_pin *p;
- p = os_zalloc(sizeof(*p));
- if (p == NULL)
- return -1;
- if (uuid == NULL)
- p->wildcard_uuid = 1;
- else
- os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
- p->pin = os_malloc(pin_len);
- if (p->pin == NULL) {
- os_free(p);
- return -1;
- }
- os_memcpy(p->pin, pin, pin_len);
- p->pin_len = pin_len;
- p->next = reg->pins;
- reg->pins = p;
- wpa_printf(MSG_DEBUG, "WPS: A new PIN configured");
- wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
- wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
- reg->selected_registrar = 1;
- reg->pbc = 0;
- wps_set_ie(reg);
- return 0;
- }
- int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
- {
- struct wps_uuid_pin *pin, *prev;
- prev = NULL;
- pin = reg->pins;
- while (pin) {
- if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
- if (prev == NULL)
- reg->pins = pin->next;
- else
- prev->next = pin->next;
- wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
- pin->uuid, WPS_UUID_LEN);
- wps_free_pin(pin);
- return 0;
- }
- prev = pin;
- pin = pin->next;
- }
- return -1;
- }
- static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
- const u8 *uuid, size_t *pin_len)
- {
- struct wps_uuid_pin *pin;
- pin = reg->pins;
- while (pin) {
- if (!pin->wildcard_uuid &&
- os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0)
- break;
- pin = pin->next;
- }
- if (!pin) {
- /* Check for wildcard UUIDs since none of the UUID-specific
- * PINs matched */
- pin = reg->pins;
- while (pin) {
- if (pin->wildcard_uuid == 1) {
- wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
- "PIN. Assigned it for this UUID-E");
- pin->wildcard_uuid = 2;
- os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
- break;
- }
- pin = pin->next;
- }
- }
- if (!pin)
- return NULL;
- /*
- * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
- * that could otherwise avoid PIN invalidations.
- */
- if (pin->locked) {
- wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
- "allow concurrent re-use");
- return NULL;
- }
- *pin_len = pin->pin_len;
- pin->locked = 1;
- return pin->pin;
- }
- int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
- {
- struct wps_uuid_pin *pin;
- pin = reg->pins;
- while (pin) {
- if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
- if (pin->wildcard_uuid == 2) {
- wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
- "wildcard PIN");
- return wps_registrar_invalidate_pin(reg, uuid);
- }
- pin->locked = 0;
- return 0;
- }
- pin = pin->next;
- }
- return -1;
- }
- static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
- {
- struct wps_registrar *reg = eloop_ctx;
- wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
- reg->selected_registrar = 0;
- reg->pbc = 0;
- wps_set_ie(reg);
- }
- int wps_registrar_button_pushed(struct wps_registrar *reg)
- {
- if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
- wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
- "mode");
- return -1;
- }
- wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
- reg->selected_registrar = 1;
- reg->pbc = 1;
- wps_set_ie(reg);
- eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
- eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
- reg, NULL);
- return 0;
- }
- static void wps_registrar_pbc_completed(struct wps_registrar *reg)
- {
- wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
- eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
- reg->selected_registrar = 0;
- reg->pbc = 0;
- wps_set_ie(reg);
- }
- void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
- const struct wpabuf *wps_data)
- {
- struct wps_parse_attr attr;
- u16 methods;
- wpa_hexdump_buf(MSG_MSGDUMP,
- "WPS: Probe Request with WPS data received",
- wps_data);
- if (wps_parse_msg(wps_data, &attr) < 0 ||
- attr.version == NULL || *attr.version != WPS_VERSION) {
- wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
- "version 0x%x", attr.version ? *attr.version : 0);
- return;
- }
- if (attr.config_methods == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
- "Probe Request");
- return;
- }
- methods = WPA_GET_BE16(attr.config_methods);
- if (!(methods & WPS_CONFIG_PUSHBUTTON))
- return; /* Not PBC */
- wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
- MACSTR, MAC2STR(addr));
- wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
- }
- static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
- const u8 *psk, size_t psk_len)
- {
- if (reg->new_psk_cb == NULL)
- return 0;
- return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
- }
- static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
- const struct wps_device_data *dev)
- {
- if (reg->pin_needed_cb == NULL)
- return;
- reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
- }
- static int wps_cb_set_ie(struct wps_registrar *reg,
- const struct wpabuf *beacon_ie,
- const struct wpabuf *probe_resp_ie)
- {
- if (reg->set_ie_cb == NULL)
- return 0;
- return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
- wpabuf_len(beacon_ie),
- wpabuf_head(probe_resp_ie),
- wpabuf_len(probe_resp_ie));
- }
- /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
- static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
- {
- struct wpabuf *ie;
- const u8 *pos, *end;
- ie = wpabuf_alloc(wpabuf_len(data) + 100);
- if (ie == NULL) {
- wpabuf_free(data);
- return NULL;
- }
- pos = wpabuf_head(data);
- end = pos + wpabuf_len(data);
- while (end > pos) {
- size_t frag_len = end - pos;
- if (frag_len > 251)
- frag_len = 251;
- wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
- wpabuf_put_u8(ie, 4 + frag_len);
- wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
- wpabuf_put_data(ie, pos, frag_len);
- pos += frag_len;
- }
- wpabuf_free(data);
- return ie;
- }
- static int wps_set_ie(struct wps_registrar *reg)
- {
- struct wpabuf *beacon;
- struct wpabuf *probe;
- int ret;
- wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
- beacon = wpabuf_alloc(300);
- if (beacon == NULL)
- return -1;
- probe = wpabuf_alloc(400);
- if (probe == NULL) {
- wpabuf_free(beacon);
- return -1;
- }
- if (wps_build_version(beacon) ||
- wps_build_wps_state(reg->wps, beacon) ||
- wps_build_ap_setup_locked(reg->wps, beacon) ||
- wps_build_selected_registrar(reg, beacon) ||
- wps_build_sel_reg_dev_password_id(reg, beacon) ||
- wps_build_sel_reg_config_methods(reg, beacon) ||
- wps_build_version(probe) ||
- wps_build_wps_state(reg->wps, probe) ||
- wps_build_ap_setup_locked(reg->wps, probe) ||
- wps_build_selected_registrar(reg, probe) ||
- wps_build_sel_reg_dev_password_id(reg, probe) ||
- wps_build_sel_reg_config_methods(reg, probe) ||
- wps_build_resp_type(reg, probe) ||
- wps_build_uuid_e(probe, reg->wps->uuid) ||
- wps_build_device_attrs(®->wps->dev, probe) ||
- wps_build_probe_config_methods(reg, probe) ||
- wps_build_rf_bands(®->wps->dev, probe)) {
- wpabuf_free(beacon);
- wpabuf_free(probe);
- return -1;
- }
- beacon = wps_ie_encapsulate(beacon);
- probe = wps_ie_encapsulate(probe);
- if (!beacon || !probe) {
- wpabuf_free(beacon);
- wpabuf_free(probe);
- return -1;
- }
- ret = wps_cb_set_ie(reg, beacon, probe);
- wpabuf_free(beacon);
- wpabuf_free(probe);
- return ret;
- }
- static int wps_get_dev_password(struct wps_data *wps)
- {
- const u8 *pin;
- size_t pin_len = 0;
- os_free(wps->dev_password);
- wps->dev_password = NULL;
- if (wps->pbc) {
- wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
- pin = (const u8 *) "00000000";
- pin_len = 8;
- } else {
- pin = wps_registrar_get_pin(wps->registrar, wps->uuid_e,
- &pin_len);
- }
- if (pin == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
- "the Enrollee");
- wps_cb_pin_needed(wps->registrar, wps->uuid_e, &wps->peer_dev);
- return -1;
- }
- wps->dev_password = os_malloc(pin_len);
- if (wps->dev_password == NULL)
- return -1;
- os_memcpy(wps->dev_password, pin, pin_len);
- wps->dev_password_len = pin_len;
- return 0;
- }
- static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
- {
- wpa_printf(MSG_DEBUG, "WPS: * UUID-R");
- wpabuf_put_be16(msg, ATTR_UUID_R);
- wpabuf_put_be16(msg, WPS_UUID_LEN);
- wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
- return 0;
- }
- static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
- {
- u8 *hash;
- const u8 *addr[4];
- size_t len[4];
- if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
- return -1;
- wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
- wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
- wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
- if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
- "R-Hash derivation");
- return -1;
- }
- wpa_printf(MSG_DEBUG, "WPS: * R-Hash1");
- wpabuf_put_be16(msg, ATTR_R_HASH1);
- wpabuf_put_be16(msg, SHA256_MAC_LEN);
- hash = wpabuf_put(msg, SHA256_MAC_LEN);
- /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
- addr[0] = wps->snonce;
- len[0] = WPS_SECRET_NONCE_LEN;
- addr[1] = wps->psk1;
- len[1] = WPS_PSK_LEN;
- addr[2] = wpabuf_head(wps->dh_pubkey_e);
- len[2] = wpabuf_len(wps->dh_pubkey_e);
- addr[3] = wpabuf_head(wps->dh_pubkey_r);
- len[3] = wpabuf_len(wps->dh_pubkey_r);
- hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
- wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
- wpa_printf(MSG_DEBUG, "WPS: * R-Hash2");
- wpabuf_put_be16(msg, ATTR_R_HASH2);
- wpabuf_put_be16(msg, SHA256_MAC_LEN);
- hash = wpabuf_put(msg, SHA256_MAC_LEN);
- /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
- addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
- addr[1] = wps->psk2;
- hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
- wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
- return 0;
- }
- static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
- {
- wpa_printf(MSG_DEBUG, "WPS: * R-SNonce1");
- wpabuf_put_be16(msg, ATTR_R_SNONCE1);
- wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
- wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
- return 0;
- }
- static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
- {
- wpa_printf(MSG_DEBUG, "WPS: * R-SNonce2");
- wpabuf_put_be16(msg, ATTR_R_SNONCE2);
- wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
- wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
- WPS_SECRET_NONCE_LEN);
- return 0;
- }
- static int wps_build_cred_network_idx(struct wpabuf *msg,
- struct wps_credential *cred)
- {
- wpa_printf(MSG_DEBUG, "WPS: * Network Index");
- wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
- wpabuf_put_be16(msg, 1);
- wpabuf_put_u8(msg, 0);
- return 0;
- }
- static int wps_build_cred_ssid(struct wpabuf *msg,
- struct wps_credential *cred)
- {
- wpa_printf(MSG_DEBUG, "WPS: * SSID");
- wpabuf_put_be16(msg, ATTR_SSID);
- wpabuf_put_be16(msg, cred->ssid_len);
- wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
- return 0;
- }
- static int wps_build_cred_auth_type(struct wpabuf *msg,
- struct wps_credential *cred)
- {
- wpa_printf(MSG_DEBUG, "WPS: * Authentication Type (0x%x)",
- cred->auth_type);
- wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
- wpabuf_put_be16(msg, 2);
- wpabuf_put_be16(msg, cred->auth_type);
- return 0;
- }
- static int wps_build_cred_encr_type(struct wpabuf *msg,
- struct wps_credential *cred)
- {
- wpa_printf(MSG_DEBUG, "WPS: * Encryption Type (0x%x)",
- cred->encr_type);
- wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
- wpabuf_put_be16(msg, 2);
- wpabuf_put_be16(msg, cred->encr_type);
- return 0;
- }
- static int wps_build_cred_network_key(struct wpabuf *msg,
- struct wps_credential *cred)
- {
- wpa_printf(MSG_DEBUG, "WPS: * Network Key");
- wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
- wpabuf_put_be16(msg, cred->key_len);
- wpabuf_put_data(msg, cred->key, cred->key_len);
- return 0;
- }
- static int wps_build_cred_mac_addr(struct wpabuf *msg,
- struct wps_credential *cred)
- {
- wpa_printf(MSG_DEBUG, "WPS: * MAC Address");
- wpabuf_put_be16(msg, ATTR_MAC_ADDR);
- wpabuf_put_be16(msg, ETH_ALEN);
- wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
- return 0;
- }
- static int wps_build_credential(struct wpabuf *msg,
- struct wps_credential *cred)
- {
- if (wps_build_cred_network_idx(msg, cred) ||
- wps_build_cred_ssid(msg, cred) ||
- wps_build_cred_auth_type(msg, cred) ||
- wps_build_cred_encr_type(msg, cred) ||
- wps_build_cred_network_key(msg, cred) ||
- wps_build_cred_mac_addr(msg, cred))
- return -1;
- return 0;
- }
- static int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
- {
- struct wpabuf *cred;
- wpa_printf(MSG_DEBUG, "WPS: * Credential");
- os_memset(&wps->cred, 0, sizeof(wps->cred));
- os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
- wps->cred.ssid_len = wps->wps->ssid_len;
- /* Select the best authentication and encryption type */
- if (wps->auth_type & WPS_AUTH_WPA2PSK)
- wps->auth_type = WPS_AUTH_WPA2PSK;
- else if (wps->auth_type & WPS_AUTH_WPAPSK)
- wps->auth_type = WPS_AUTH_WPAPSK;
- else if (wps->auth_type & WPS_AUTH_OPEN)
- wps->auth_type = WPS_AUTH_OPEN;
- else if (wps->auth_type & WPS_AUTH_SHARED)
- wps->auth_type = WPS_AUTH_SHARED;
- else {
- wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
- wps->auth_type);
- return -1;
- }
- wps->cred.auth_type = wps->auth_type;
- if (wps->auth_type == WPS_AUTH_WPA2PSK ||
- wps->auth_type == WPS_AUTH_WPAPSK) {
- if (wps->encr_type & WPS_ENCR_AES)
- wps->encr_type = WPS_ENCR_AES;
- else if (wps->encr_type & WPS_ENCR_TKIP)
- wps->encr_type = WPS_ENCR_TKIP;
- else {
- wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
- "type for WPA/WPA2");
- return -1;
- }
- } else {
- if (wps->encr_type & WPS_ENCR_WEP)
- wps->encr_type = WPS_ENCR_WEP;
- else if (wps->encr_type & WPS_ENCR_NONE)
- wps->encr_type = WPS_ENCR_NONE;
- else {
- wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
- "type for non-WPA/WPA2 mode");
- return -1;
- }
- }
- wps->cred.encr_type = wps->encr_type;
- os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
- if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap) {
- u8 r[16];
- /* Generate a random passphrase */
- if (os_get_random(r, sizeof(r)) < 0)
- return -1;
- os_free(wps->new_psk);
- wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
- if (wps->new_psk == NULL)
- return -1;
- wps->new_psk_len--; /* remove newline */
- while (wps->new_psk_len &&
- wps->new_psk[wps->new_psk_len - 1] == '=')
- wps->new_psk_len--;
- wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
- wps->new_psk, wps->new_psk_len);
- os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
- wps->cred.key_len = wps->new_psk_len;
- } else if (wps->wps->network_key) {
- os_memcpy(wps->cred.key, wps->wps->network_key,
- wps->wps->network_key_len);
- wps->cred.key_len = wps->wps->network_key_len;
- } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
- char hex[65];
- /* Generate a random per-device PSK */
- os_free(wps->new_psk);
- wps->new_psk_len = 32;
- wps->new_psk = os_malloc(wps->new_psk_len);
- if (wps->new_psk == NULL)
- return -1;
- if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
- os_free(wps->new_psk);
- wps->new_psk = NULL;
- return -1;
- }
- wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
- wps->new_psk, wps->new_psk_len);
- wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
- wps->new_psk_len);
- os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
- wps->cred.key_len = wps->new_psk_len * 2;
- }
- cred = wpabuf_alloc(200);
- if (cred == NULL)
- return -1;
- if (wps_build_credential(cred, &wps->cred)) {
- wpabuf_free(cred);
- return -1;
- }
- wpabuf_put_be16(msg, ATTR_CRED);
- wpabuf_put_be16(msg, wpabuf_len(cred));
- wpabuf_put_buf(msg, cred);
- wpabuf_free(cred);
- return 0;
- }
- static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
- {
- wpa_printf(MSG_DEBUG, "WPS: * AP Settings");
- if (wps_build_credential(msg, &wps->cred))
- return -1;
- return 0;
- }
- static struct wpabuf * wps_build_m2(struct wps_data *wps)
- {
- struct wpabuf *msg;
- if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
- return NULL;
- wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
- wps->nonce_r, WPS_NONCE_LEN);
- os_memcpy(wps->uuid_r, wps->wps->uuid, WPS_UUID_LEN);
- wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
- wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
- msg = wpabuf_alloc(1000);
- if (msg == NULL)
- return NULL;
- if (wps_build_version(msg) ||
- wps_build_msg_type(msg, WPS_M2) ||
- wps_build_enrollee_nonce(wps, msg) ||
- wps_build_registrar_nonce(wps, msg) ||
- wps_build_uuid_r(wps, msg) ||
- wps_build_public_key(wps, msg) ||
- wps_derive_keys(wps) ||
- wps_build_auth_type_flags(wps, msg) ||
- wps_build_encr_type_flags(wps, msg) ||
- wps_build_conn_type_flags(wps, msg) ||
- wps_build_config_methods_r(wps->registrar, msg) ||
- wps_build_device_attrs(&wps->wps->dev, msg) ||
- wps_build_rf_bands(&wps->wps->dev, msg) ||
- wps_build_assoc_state(wps, msg) ||
- wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
- wps_build_dev_password_id(msg, DEV_PW_DEFAULT) ||
- wps_build_os_version(&wps->wps->dev, msg) ||
- wps_build_authenticator(wps, msg)) {
- wpabuf_free(msg);
- return NULL;
- }
- wps->state = RECV_M3;
- return msg;
- }
- static struct wpabuf * wps_build_m2d(struct wps_data *wps)
- {
- struct wpabuf *msg;
- u16 err = WPS_CFG_NO_ERROR;
- wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
- msg = wpabuf_alloc(1000);
- if (msg == NULL)
- return NULL;
- if (wps->authenticator && wps->wps->ap_setup_locked)
- err = WPS_CFG_SETUP_LOCKED;
- if (wps_build_version(msg) ||
- wps_build_msg_type(msg, WPS_M2D) ||
- wps_build_enrollee_nonce(wps, msg) ||
- wps_build_registrar_nonce(wps, msg) ||
- wps_build_uuid_r(wps, msg) ||
- wps_build_auth_type_flags(wps, msg) ||
- wps_build_encr_type_flags(wps, msg) ||
- wps_build_conn_type_flags(wps, msg) ||
- wps_build_config_methods_r(wps->registrar, msg) ||
- wps_build_device_attrs(&wps->wps->dev, msg) ||
- wps_build_rf_bands(&wps->wps->dev, msg) ||
- wps_build_assoc_state(wps, msg) ||
- wps_build_config_error(msg, err) ||
- wps_build_os_version(&wps->wps->dev, msg)) {
- wpabuf_free(msg);
- return NULL;
- }
- wps->state = RECV_M2D_ACK;
- return msg;
- }
- static struct wpabuf * wps_build_m4(struct wps_data *wps)
- {
- struct wpabuf *msg, *plain;
- wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
- wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
- plain = wpabuf_alloc(200);
- if (plain == NULL)
- return NULL;
- msg = wpabuf_alloc(1000);
- if (msg == NULL) {
- wpabuf_free(plain);
- return NULL;
- }
- if (wps_build_version(msg) ||
- wps_build_msg_type(msg, WPS_M4) ||
- wps_build_enrollee_nonce(wps, msg) ||
- wps_build_r_hash(wps, msg) ||
- wps_build_r_snonce1(wps, plain) ||
- wps_build_key_wrap_auth(wps, plain) ||
- wps_build_encr_settings(wps, msg, plain) ||
- wps_build_authenticator(wps, msg)) {
- wpabuf_free(plain);
- wpabuf_free(msg);
- return NULL;
- }
- wpabuf_free(plain);
- wps->state = RECV_M5;
- return msg;
- }
- static struct wpabuf * wps_build_m6(struct wps_data *wps)
- {
- struct wpabuf *msg, *plain;
- wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
- plain = wpabuf_alloc(200);
- if (plain == NULL)
- return NULL;
- msg = wpabuf_alloc(1000);
- if (msg == NULL) {
- wpabuf_free(plain);
- return NULL;
- }
- if (wps_build_version(msg) ||
- wps_build_msg_type(msg, WPS_M6) ||
- wps_build_enrollee_nonce(wps, msg) ||
- wps_build_r_snonce2(wps, plain) ||
- wps_build_key_wrap_auth(wps, plain) ||
- wps_build_encr_settings(wps, msg, plain) ||
- wps_build_authenticator(wps, msg)) {
- wpabuf_free(plain);
- wpabuf_free(msg);
- return NULL;
- }
- wpabuf_free(plain);
- wps->wps_pin_revealed = 1;
- wps->state = RECV_M7;
- return msg;
- }
- static struct wpabuf * wps_build_m8(struct wps_data *wps)
- {
- struct wpabuf *msg, *plain;
- wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
- plain = wpabuf_alloc(500);
- if (plain == NULL)
- return NULL;
- msg = wpabuf_alloc(1000);
- if (msg == NULL) {
- wpabuf_free(plain);
- return NULL;
- }
- if (wps_build_version(msg) ||
- wps_build_msg_type(msg, WPS_M8) ||
- wps_build_enrollee_nonce(wps, msg) ||
- (wps->wps->ap && wps_build_cred(wps, plain)) ||
- (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
- wps_build_key_wrap_auth(wps, plain) ||
- wps_build_encr_settings(wps, msg, plain) ||
- wps_build_authenticator(wps, msg)) {
- wpabuf_free(plain);
- wpabuf_free(msg);
- return NULL;
- }
- wpabuf_free(plain);
- wps->state = RECV_DONE;
- return msg;
- }
- static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
- {
- struct wpabuf *msg;
- wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
- msg = wpabuf_alloc(1000);
- if (msg == NULL)
- return NULL;
- if (wps_build_version(msg) ||
- wps_build_msg_type(msg, WPS_WSC_ACK) ||
- wps_build_enrollee_nonce(wps, msg) ||
- wps_build_registrar_nonce(wps, msg)) {
- wpabuf_free(msg);
- return NULL;
- }
- return msg;
- }
- static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
- {
- struct wpabuf *msg;
- wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
- msg = wpabuf_alloc(1000);
- if (msg == NULL)
- return NULL;
- if (wps_build_version(msg) ||
- wps_build_msg_type(msg, WPS_WSC_NACK) ||
- wps_build_enrollee_nonce(wps, msg) ||
- wps_build_registrar_nonce(wps, msg) ||
- wps_build_config_error(msg, wps->config_error)) {
- wpabuf_free(msg);
- return NULL;
- }
- return msg;
- }
- struct wpabuf * wps_registrar_get_msg(struct wps_data *wps, u8 *op_code)
- {
- struct wpabuf *msg;
- switch (wps->state) {
- case SEND_M2:
- if (wps_get_dev_password(wps) < 0)
- msg = wps_build_m2d(wps);
- else
- msg = wps_build_m2(wps);
- *op_code = WSC_MSG;
- break;
- case SEND_M2D:
- msg = wps_build_m2d(wps);
- *op_code = WSC_MSG;
- break;
- case SEND_M4:
- msg = wps_build_m4(wps);
- *op_code = WSC_MSG;
- break;
- case SEND_M6:
- msg = wps_build_m6(wps);
- *op_code = WSC_MSG;
- break;
- case SEND_M8:
- msg = wps_build_m8(wps);
- *op_code = WSC_MSG;
- break;
- case RECV_DONE:
- msg = wps_build_wsc_ack(wps);
- *op_code = WSC_ACK;
- break;
- case SEND_WSC_NACK:
- msg = wps_build_wsc_nack(wps);
- *op_code = WSC_NACK;
- break;
- default:
- wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
- "a message", wps->state);
- msg = NULL;
- break;
- }
- if (*op_code == WSC_MSG && msg) {
- /* Save a copy of the last message for Authenticator derivation
- */
- wpabuf_free(wps->last_msg);
- wps->last_msg = wpabuf_dup(msg);
- }
- return msg;
- }
- static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
- {
- if (e_nonce == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
- return -1;
- }
- os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
- wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
- wps->nonce_e, WPS_NONCE_LEN);
- return 0;
- }
- static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
- {
- if (r_nonce == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
- return -1;
- }
- if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
- wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
- return -1;
- }
- return 0;
- }
- static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
- {
- if (uuid_e == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
- return -1;
- }
- os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
- wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
- return 0;
- }
- static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
- {
- if (pw_id == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
- return -1;
- }
- wps->dev_pw_id = WPA_GET_BE16(pw_id);
- wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
- return 0;
- }
- static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
- {
- if (e_hash1 == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
- return -1;
- }
- os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
- wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
- return 0;
- }
- static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
- {
- if (e_hash2 == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
- return -1;
- }
- os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
- wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
- return 0;
- }
- static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
- {
- u8 hash[SHA256_MAC_LEN];
- const u8 *addr[4];
- size_t len[4];
- if (e_snonce1 == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
- return -1;
- }
- wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
- WPS_SECRET_NONCE_LEN);
- /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
- addr[0] = e_snonce1;
- len[0] = WPS_SECRET_NONCE_LEN;
- addr[1] = wps->psk1;
- len[1] = WPS_PSK_LEN;
- addr[2] = wpabuf_head(wps->dh_pubkey_e);
- len[2] = wpabuf_len(wps->dh_pubkey_e);
- addr[3] = wpabuf_head(wps->dh_pubkey_r);
- len[3] = wpabuf_len(wps->dh_pubkey_r);
- hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
- if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
- wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
- "not match with the pre-committed value");
- wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
- return -1;
- }
- wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
- "half of the device password");
- return 0;
- }
- static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
- {
- u8 hash[SHA256_MAC_LEN];
- const u8 *addr[4];
- size_t len[4];
- if (e_snonce2 == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
- return -1;
- }
- wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
- WPS_SECRET_NONCE_LEN);
- /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
- addr[0] = e_snonce2;
- len[0] = WPS_SECRET_NONCE_LEN;
- addr[1] = wps->psk2;
- len[1] = WPS_PSK_LEN;
- addr[2] = wpabuf_head(wps->dh_pubkey_e);
- len[2] = wpabuf_len(wps->dh_pubkey_e);
- addr[3] = wpabuf_head(wps->dh_pubkey_r);
- len[3] = wpabuf_len(wps->dh_pubkey_r);
- hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
- if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
- wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
- "not match with the pre-committed value");
- wps_registrar_invalidate_pin(wps->registrar, wps->uuid_e);
- wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
- return -1;
- }
- wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
- "half of the device password");
- wps->wps_pin_revealed = 0;
- wps_registrar_unlock_pin(wps->registrar, wps->uuid_e);
- return 0;
- }
- static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
- {
- if (mac_addr == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
- return -1;
- }
- wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
- MAC2STR(mac_addr));
- os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
- os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
- return 0;
- }
- static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
- size_t pk_len)
- {
- if (pk == NULL || pk_len == 0) {
- wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
- return -1;
- }
- wpabuf_free(wps->dh_pubkey_e);
- wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
- if (wps->dh_pubkey_e == NULL)
- return -1;
- return 0;
- }
- static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
- {
- u16 auth_types;
- if (auth == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
- "received");
- return -1;
- }
- auth_types = WPA_GET_BE16(auth);
- wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
- auth_types);
- wps->auth_type = wps->wps->auth_types & auth_types;
- if (wps->auth_type == 0) {
- wpa_printf(MSG_DEBUG, "WPS: No match in supported "
- "authentication types (own 0x%x Enrollee 0x%x)",
- wps->wps->auth_types, auth_types);
- return -1;
- }
- return 0;
- }
- static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
- {
- u16 encr_types;
- if (encr == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
- "received");
- return -1;
- }
- encr_types = WPA_GET_BE16(encr);
- wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
- encr_types);
- wps->encr_type = wps->wps->encr_types & encr_types;
- if (wps->encr_type == 0) {
- wpa_printf(MSG_DEBUG, "WPS: No match in supported "
- "encryption types");
- return -1;
- }
- return 0;
- }
- static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
- {
- if (conn == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
- "received");
- return -1;
- }
- wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
- *conn);
- return 0;
- }
- static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
- {
- u16 m;
- if (methods == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
- return -1;
- }
- m = WPA_GET_BE16(methods);
- wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
- return 0;
- }
- static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
- {
- if (state == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
- "received");
- return -1;
- }
- wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
- *state);
- return 0;
- }
- static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
- {
- u16 a;
- if (assoc == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No Association State received");
- return -1;
- }
- a = WPA_GET_BE16(assoc);
- wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
- return 0;
- }
- static int wps_process_config_error(struct wps_data *wps, const u8 *err)
- {
- u16 e;
- if (err == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
- return -1;
- }
- e = WPA_GET_BE16(err);
- wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
- return 0;
- }
- static enum wps_process_res wps_process_m1(struct wps_data *wps,
- struct wps_parse_attr *attr)
- {
- wpa_printf(MSG_DEBUG, "WPS: Received M1");
- if (wps->state != RECV_M1) {
- wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
- "receiving M1", wps->state);
- return WPS_FAILURE;
- }
- if (wps_process_uuid_e(wps, attr->uuid_e) ||
- wps_process_mac_addr(wps, attr->mac_addr) ||
- wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
- wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
- wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
- wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
- wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
- wps_process_config_methods(wps, attr->config_methods) ||
- wps_process_wps_state(wps, attr->wps_state) ||
- wps_process_device_attrs(&wps->peer_dev, attr) ||
- wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
- wps_process_assoc_state(wps, attr->assoc_state) ||
- wps_process_dev_password_id(wps, attr->dev_password_id) ||
- wps_process_config_error(wps, attr->config_error) ||
- wps_process_os_version(&wps->peer_dev, attr->os_version))
- return WPS_FAILURE;
- if (wps->dev_pw_id != DEV_PW_DEFAULT &&
- wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
- wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
- wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
- (wps->dev_pw_id != DEV_PW_PUSHBUTTON || !wps->registrar->pbc)) {
- wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
- wps->dev_pw_id);
- wps->state = SEND_M2D;
- return WPS_CONTINUE;
- }
- if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
- if (wps_registrar_pbc_overlap(wps->registrar, wps->mac_addr_e,
- wps->uuid_e)) {
- wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
- "negotiation");
- wps->state = SEND_M2D;
- return WPS_CONTINUE;
- }
- wps_registrar_add_pbc_session(wps->registrar, wps->mac_addr_e,
- wps->uuid_e);
- wps->pbc = 1;
- }
- wps->state = SEND_M2;
- return WPS_CONTINUE;
- }
- static enum wps_process_res wps_process_m3(struct wps_data *wps,
- const struct wpabuf *msg,
- struct wps_parse_attr *attr)
- {
- wpa_printf(MSG_DEBUG, "WPS: Received M3");
- if (wps->state != RECV_M3) {
- wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
- "receiving M3", wps->state);
- wps->state = SEND_WSC_NACK;
- return WPS_CONTINUE;
- }
- if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
- wps_process_authenticator(wps, attr->authenticator, msg) ||
- wps_process_e_hash1(wps, attr->e_hash1) ||
- wps_process_e_hash2(wps, attr->e_hash2)) {
- wps->state = SEND_WSC_NACK;
- return WPS_CONTINUE;
- }
- wps->state = SEND_M4;
- return WPS_CONTINUE;
- }
- static enum wps_process_res wps_process_m5(struct wps_data *wps,
- const struct wpabuf *msg,
- struct wps_parse_attr *attr)
- {
- struct wpabuf *decrypted;
- struct wps_parse_attr eattr;
- wpa_printf(MSG_DEBUG, "WPS: Received M5");
- if (wps->state != RECV_M5) {
- wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
- "receiving M5", wps->state);
- wps->state = SEND_WSC_NACK;
- return WPS_CONTINUE;
- }
- if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
- wps_process_authenticator(wps, attr->authenticator, msg)) {
- wps->state = SEND_WSC_NACK;
- return WPS_CONTINUE;
- }
- decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
- attr->encr_settings_len);
- if (decrypted == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
- "Settings attribute");
- wps->state = SEND_WSC_NACK;
- return WPS_CONTINUE;
- }
- wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
- "attribute");
- if (wps_parse_msg(decrypted, &eattr) < 0 ||
- wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
- wps_process_e_snonce1(wps, eattr.e_snonce1)) {
- wpabuf_free(decrypted);
- wps->state = SEND_WSC_NACK;
- return WPS_CONTINUE;
- }
- wpabuf_free(decrypted);
- wps->state = SEND_M6;
- return WPS_CONTINUE;
- }
- static int wps_process_ap_settings_r(struct wps_data *wps,
- struct wps_parse_attr *attr)
- {
- if (wps->wps->ap)
- return 0;
- /* AP Settings Attributes in M7 when Enrollee is an AP */
- if (wps_process_ap_settings(attr, &wps->cred) < 0)
- return -1;
- wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
- /*
- * TODO: Provide access to AP settings and allow changes before sending
- * out M8. For now, just copy the settings unchanged into M8.
- */
- return 0;
- }
- static enum wps_process_res wps_process_m7(struct wps_data *wps,
- const struct wpabuf *msg,
- struct wps_parse_attr *attr)
- {
- struct wpabuf *decrypted;
- struct wps_parse_attr eattr;
- wpa_printf(MSG_DEBUG, "WPS: Received M7");
- if (wps->state != RECV_M7) {
- wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
- "receiving M7", wps->state);
- wps->state = SEND_WSC_NACK;
- return WPS_CONTINUE;
- }
- if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
- wps_process_authenticator(wps, attr->authenticator, msg)) {
- wps->state = SEND_WSC_NACK;
- return WPS_CONTINUE;
- }
- decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
- attr->encr_settings_len);
- if (decrypted == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
- "Settings attribute");
- wps->state = SEND_WSC_NACK;
- return WPS_CONTINUE;
- }
- wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
- "attribute");
- if (wps_parse_msg(decrypted, &eattr) < 0 ||
- wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
- wps_process_e_snonce2(wps, eattr.e_snonce2) ||
- wps_process_ap_settings_r(wps, &eattr)) {
- wpabuf_free(decrypted);
- wps->state = SEND_WSC_NACK;
- return WPS_CONTINUE;
- }
- wpabuf_free(decrypted);
- wps->state = SEND_M8;
- return WPS_CONTINUE;
- }
- static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
- const struct wpabuf *msg)
- {
- struct wps_parse_attr attr;
- enum wps_process_res ret = WPS_CONTINUE;
- wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
- if (wps_parse_msg(msg, &attr) < 0)
- return WPS_FAILURE;
- if (attr.version == NULL || *attr.version != WPS_VERSION) {
- wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
- attr.version ? *attr.version : 0);
- return WPS_FAILURE;
- }
- if (attr.msg_type == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
- return WPS_FAILURE;
- }
- if (*attr.msg_type != WPS_M1 &&
- (attr.registrar_nonce == NULL ||
- os_memcmp(wps->nonce_r, attr.registrar_nonce,
- WPS_NONCE_LEN != 0))) {
- wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
- return WPS_FAILURE;
- }
- switch (*attr.msg_type) {
- case WPS_M1:
- ret = wps_process_m1(wps, &attr);
- break;
- case WPS_M3:
- ret = wps_process_m3(wps, msg, &attr);
- if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
- wps_fail_event(wps->wps, WPS_M3);
- break;
- case WPS_M5:
- ret = wps_process_m5(wps, msg, &attr);
- if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
- wps_fail_event(wps->wps, WPS_M5);
- break;
- case WPS_M7:
- ret = wps_process_m7(wps, msg, &attr);
- if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
- wps_fail_event(wps->wps, WPS_M7);
- break;
- default:
- wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
- *attr.msg_type);
- return WPS_FAILURE;
- }
- if (ret == WPS_CONTINUE) {
- /* Save a copy of the last message for Authenticator derivation
- */
- wpabuf_free(wps->last_msg);
- wps->last_msg = wpabuf_dup(msg);
- }
- return ret;
- }
- static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
- const struct wpabuf *msg)
- {
- struct wps_parse_attr attr;
- wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
- if (wps_parse_msg(msg, &attr) < 0)
- return WPS_FAILURE;
- if (attr.version == NULL || *attr.version != WPS_VERSION) {
- wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
- attr.version ? *attr.version : 0);
- return WPS_FAILURE;
- }
- if (attr.msg_type == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
- return WPS_FAILURE;
- }
- if (*attr.msg_type != WPS_WSC_ACK) {
- wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
- *attr.msg_type);
- return WPS_FAILURE;
- }
- if (attr.registrar_nonce == NULL ||
- os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
- {
- wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
- return WPS_FAILURE;
- }
- if (attr.enrollee_nonce == NULL ||
- os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
- wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
- return WPS_FAILURE;
- }
- if (wps->state == RECV_M2D_ACK) {
- /* TODO: support for multiple registrars and sending of
- * multiple M2/M2D messages */
- wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
- "terminate negotiation");
- }
- return WPS_FAILURE;
- }
- static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
- const struct wpabuf *msg)
- {
- struct wps_parse_attr attr;
- int old_state;
- wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
- old_state = wps->state;
- wps->state = SEND_WSC_NACK;
- if (wps_parse_msg(msg, &attr) < 0)
- return WPS_FAILURE;
- if (attr.version == NULL || *attr.version != WPS_VERSION) {
- wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
- attr.version ? *attr.version : 0);
- return WPS_FAILURE;
- }
- if (attr.msg_type == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
- return WPS_FAILURE;
- }
- if (*attr.msg_type != WPS_WSC_NACK) {
- wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
- *attr.msg_type);
- return WPS_FAILURE;
- }
- if (attr.registrar_nonce == NULL ||
- os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
- {
- wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
- return WPS_FAILURE;
- }
- if (attr.enrollee_nonce == NULL ||
- os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
- wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
- return WPS_FAILURE;
- }
- if (attr.config_error == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
- "in WSC_NACK");
- return WPS_FAILURE;
- }
- wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
- "Configuration Error %d", WPA_GET_BE16(attr.config_error));
- switch (old_state) {
- case RECV_M3:
- wps_fail_event(wps->wps, WPS_M2);
- break;
- case RECV_M5:
- wps_fail_event(wps->wps, WPS_M4);
- break;
- case RECV_M7:
- wps_fail_event(wps->wps, WPS_M6);
- break;
- case RECV_DONE:
- wps_fail_event(wps->wps, WPS_M8);
- break;
- default:
- break;
- }
- return WPS_FAILURE;
- }
- static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
- const struct wpabuf *msg)
- {
- struct wps_parse_attr attr;
- wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
- if (wps->state != RECV_DONE) {
- wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
- "receiving WSC_Done", wps->state);
- return WPS_FAILURE;
- }
- if (wps_parse_msg(msg, &attr) < 0)
- return WPS_FAILURE;
- if (attr.version == NULL || *attr.version != WPS_VERSION) {
- wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
- attr.version ? *attr.version : 0);
- return WPS_FAILURE;
- }
- if (attr.msg_type == NULL) {
- wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
- return WPS_FAILURE;
- }
- if (*attr.msg_type != WPS_WSC_DONE) {
- wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
- *attr.msg_type);
- return WPS_FAILURE;
- }
- if (attr.registrar_nonce == NULL ||
- os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
- {
- wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
- return WPS_FAILURE;
- }
- if (attr.enrollee_nonce == NULL ||
- os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
- wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
- return WPS_FAILURE;
- }
- wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
- if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
- wps->wps->ap) {
- struct wps_credential cred;
- wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
- "on first Enrollee connection");
- os_memset(&cred, 0, sizeof(cred));
- os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
- cred.ssid_len = wps->wps->ssid_len;
- cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
- cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
- os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
- cred.key_len = wps->new_psk_len;
- wps->wps->wps_state = WPS_STATE_CONFIGURED;
- wpa_hexdump_ascii_key(MSG_DEBUG,
- "WPS: Generated random passphrase",
- wps->new_psk, wps->new_psk_len);
- if (wps->wps->cred_cb)
- wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
- os_free(wps->new_psk);
- wps->new_psk = NULL;
- }
- if (!wps->wps->ap) {
- wpa_printf(MSG_DEBUG, "WPS: Update local configuration based "
- "on the modified AP configuration");
- if (wps->wps->cred_cb)
- wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
- }
- if (wps->new_psk) {
- if (wps_cb_new_psk(wps->registrar, wps->mac_addr_e,
- wps->new_psk, wps->new_psk_len)) {
- wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
- "new PSK");
- }
- os_free(wps->new_psk);
- wps->new_psk = NULL;
- }
- if (wps->pbc) {
- wps_registrar_remove_pbc_session(wps->registrar,
- wps->mac_addr_e, wps->uuid_e);
- wps_registrar_pbc_completed(wps->registrar);
- }
- wps_success_event(wps->wps);
- return WPS_DONE;
- }
- enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
- u8 op_code,
- const struct wpabuf *msg)
- {
- enum wps_process_res ret;
- wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
- "op_code=%d)",
- (unsigned long) wpabuf_len(msg), op_code);
- switch (op_code) {
- case WSC_MSG:
- return wps_process_wsc_msg(wps, msg);
- case WSC_ACK:
- return wps_process_wsc_ack(wps, msg);
- case WSC_NACK:
- return wps_process_wsc_nack(wps, msg);
- case WSC_Done:
- ret = wps_process_wsc_done(wps, msg);
- if (ret == WPS_FAILURE) {
- wps->state = SEND_WSC_NACK;
- wps_fail_event(wps->wps, WPS_WSC_DONE);
- }
- return ret;
- default:
- wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
- return WPS_FAILURE;
- }
- }
|