2 * EAP-WSC peer for Wi-Fi Protected Setup
3 * Copyright (c) 2007-2008, Jouni Malinen <j@w1.fi>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
20 #include "eap_common/eap_wsc_common.h"
22 #include "wps/wps_defs.h"
26 enum { WAIT_START, MSG, FRAG_ACK, WAIT_FRAG_ACK, DONE, FAIL } state;
28 struct wpabuf *in_buf;
29 struct wpabuf *out_buf;
30 u8 in_op_code, out_op_code;
34 struct wps_context *wps_ctx;
38 static const char * eap_wsc_state_txt(int state)
48 return "WAIT_FRAG_ACK";
59 static void eap_wsc_state(struct eap_wsc_data *data, int state)
61 wpa_printf(MSG_DEBUG, "EAP-WSC: %s -> %s",
62 eap_wsc_state_txt(data->state),
63 eap_wsc_state_txt(state));
68 static int eap_wsc_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
71 /* struct eap_wsc_data *data = ctx; */
73 wpa_printf(MSG_DEBUG, "EAP-SC: Received new WPA/WPA2-PSK from WPS for "
74 "STA " MACSTR, MAC2STR(mac_addr));
75 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
83 static void eap_wsc_pin_needed_cb(void *ctx, const u8 *uuid_e,
84 const struct wps_device_data *dev)
86 /* struct eap_wsc_data *data = ctx; */
87 char uuid[40], txt[400];
89 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
91 wpa_printf(MSG_DEBUG, "EAP-WSC: PIN needed for E-UUID %s", uuid);
92 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED "
93 "%s " MACSTR " [%s|%s|%s|%s|%s|%d-%08X-%d]",
94 uuid, MAC2STR(dev->mac_addr), dev->device_name,
95 dev->manufacturer, dev->model_name,
96 dev->model_number, dev->serial_number,
97 dev->categ, dev->oui, dev->sub_categ);
98 if (len > 0 && len < (int) sizeof(txt))
99 wpa_printf(MSG_INFO, "%s", txt);
103 static void * eap_wsc_init(struct eap_sm *sm)
105 struct eap_wsc_data *data;
109 struct wps_config cfg;
113 struct wps_context *wps = NULL;
115 identity = eap_get_config_identity(sm, &identity_len);
117 if (identity && identity_len == WSC_ID_REGISTRAR_LEN &&
118 os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0)
119 registrar = 1; /* Supplicant is Registrar */
120 else if (identity && identity_len == WSC_ID_ENROLLEE_LEN &&
121 os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0)
122 registrar = 0; /* Supplicant is Enrollee */
124 wpa_hexdump_ascii(MSG_INFO, "EAP-WSC: Unexpected identity",
125 identity, identity_len);
129 data = os_zalloc(sizeof(*data));
132 data->state = registrar ? MSG : WAIT_START;
133 data->registrar = registrar;
136 struct wps_registrar_config rcfg;
138 wps = os_zalloc(sizeof(*wps));
146 /* TODO: configure.. */
147 wps->auth_types = WPS_AUTH_WPA2PSK;
148 wps->encr_types = WPS_ENCR_AES;
149 os_memcpy(wps->ssid, "test", 4);
152 os_memset(&rcfg, 0, sizeof(rcfg));
153 rcfg.new_psk_cb = eap_wsc_new_psk_cb;
154 rcfg.pin_needed_cb = eap_wsc_pin_needed_cb;
157 wps->registrar = wps_registrar_init(wps, &rcfg);
158 if (wps->registrar == NULL) {
159 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to initialize "
161 os_free(wps->network_key);
170 os_memset(&cfg, 0, sizeof(cfg));
171 cfg.authenticator = 0;
173 cfg.registrar = data->wps_ctx ? data->wps_ctx->registrar : NULL;
174 cfg.enrollee_mac_addr = sm->mac_addr;
176 phase1 = eap_get_config_phase1(sm);
177 if (phase1 == NULL) {
178 wpa_printf(MSG_INFO, "EAP-WSC: phase1 configuration data not "
184 pos = os_strstr(phase1, "pin=");
187 cfg.pin = (const u8 *) pos;
188 while (*pos != '\0' && *pos != ' ')
190 cfg.pin_len = pos - (const char *) cfg.pin;
192 pos = os_strstr(phase1, "pbc=1");
197 if (cfg.pin == NULL && !cfg.pbc) {
198 wpa_printf(MSG_INFO, "EAP-WSC: PIN or PBC not set in phase1 "
199 "configuration data");
204 pos = os_strstr(phase1, "uuid=");
206 wpa_printf(MSG_INFO, "EAP-WSC: UUID not set in phase1 "
207 "configuration data");
211 if (uuid_str2bin(pos + 5, uuid)) {
212 wpa_printf(MSG_INFO, "EAP-WSC: Invalid UUID in phase1 "
213 "configuration data");
217 if (registrar && wps)
218 os_memcpy(wps->uuid, uuid, UUID_LEN);
221 cfg.wps_cred_cb = sm->eapol_cb->wps_cred;
222 cfg.cb_ctx = sm->eapol_ctx;
223 data->wps = wps_init(&cfg);
224 if (data->wps == NULL) {
228 data->fragment_size = WSC_FRAGMENT_SIZE;
233 wpa_printf(MSG_INFO, "EAP-WSC: Registrar functionality not "
234 "yet fully supported - using test values");
236 os_memset(uuid_e, 0, UUID_LEN);
237 wps_registrar_add_pin(data->wps_ctx->registrar, uuid_e,
238 (const u8 *) "12345670", 8);
245 static void eap_wsc_deinit(struct eap_sm *sm, void *priv)
247 struct eap_wsc_data *data = priv;
248 wpabuf_free(data->in_buf);
249 wpabuf_free(data->out_buf);
250 wps_deinit(data->wps);
252 wps_registrar_deinit(data->wps_ctx->registrar);
253 os_free(data->wps_ctx->network_key);
254 os_free(data->wps_ctx);
260 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data,
261 struct eap_method_ret *ret, u8 id)
265 size_t send_len, plen;
268 wpa_printf(MSG_DEBUG, "EAP-WSC: Generating Response");
269 ret->allowNotifications = TRUE;
272 send_len = wpabuf_len(data->out_buf) - data->out_used;
273 if (2 + send_len > data->fragment_size) {
274 send_len = data->fragment_size - 2;
275 flags |= WSC_FLAGS_MF;
276 if (data->out_used == 0) {
277 flags |= WSC_FLAGS_LF;
282 if (flags & WSC_FLAGS_LF)
284 resp = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
285 EAP_CODE_RESPONSE, id);
289 wpabuf_put_u8(resp, data->out_op_code); /* Op-Code */
290 wpabuf_put_u8(resp, flags); /* Flags */
291 if (flags & WSC_FLAGS_LF)
292 wpabuf_put_be16(resp, wpabuf_len(data->out_buf));
294 wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
296 data->out_used += send_len;
298 ret->methodState = METHOD_MAY_CONT;
299 ret->decision = DECISION_FAIL;
301 if (data->out_used == wpabuf_len(data->out_buf)) {
302 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
303 "(message sent completely)",
304 (unsigned long) send_len);
305 wpabuf_free(data->out_buf);
306 data->out_buf = NULL;
308 if ((data->state == FAIL && data->out_op_code == WSC_ACK) ||
309 data->out_op_code == WSC_NACK ||
310 data->out_op_code == WSC_Done) {
311 eap_wsc_state(data, FAIL);
312 ret->methodState = METHOD_DONE;
314 eap_wsc_state(data, MSG);
316 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
317 "(%lu more to send)", (unsigned long) send_len,
318 (unsigned long) wpabuf_len(data->out_buf) -
320 eap_wsc_state(data, WAIT_FRAG_ACK);
327 static int eap_wsc_process_cont(struct eap_wsc_data *data,
328 const u8 *buf, size_t len, u8 op_code)
330 /* Process continuation of a pending message */
331 if (op_code != data->in_op_code) {
332 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
333 "fragment (expected %d)",
334 op_code, data->in_op_code);
338 if (len > wpabuf_tailroom(data->in_buf)) {
339 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
340 eap_wsc_state(data, FAIL);
344 wpabuf_put_data(data->in_buf, buf, len);
345 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting "
346 "for %lu bytes more", (unsigned long) len,
347 (unsigned long) wpabuf_tailroom(data->in_buf));
353 static struct wpabuf * eap_wsc_process_fragment(struct eap_wsc_data *data,
354 struct eap_method_ret *ret,
355 u8 id, u8 flags, u8 op_code,
357 const u8 *buf, size_t len)
359 /* Process a fragment that is not the last one of the message */
360 if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
361 wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length field in a "
362 "fragmented packet");
367 if (data->in_buf == NULL) {
368 /* First fragment of the message */
369 data->in_buf = wpabuf_alloc(message_length);
370 if (data->in_buf == NULL) {
371 wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
376 data->in_op_code = op_code;
377 wpabuf_put_data(data->in_buf, buf, len);
378 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in first "
379 "fragment, waiting for %lu bytes more",
381 (unsigned long) wpabuf_tailroom(data->in_buf));
384 return eap_wsc_build_frag_ack(id, EAP_CODE_RESPONSE);
388 static struct wpabuf * eap_wsc_process(struct eap_sm *sm, void *priv,
389 struct eap_method_ret *ret,
390 const struct wpabuf *reqData)
392 struct eap_wsc_data *data = priv;
393 const u8 *start, *pos, *end;
395 u8 op_code, flags, id;
396 u16 message_length = 0;
397 enum wps_process_res res;
398 struct wpabuf tmpbuf;
400 pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, reqData,
402 if (pos == NULL || len < 2) {
407 id = eap_get_id(reqData);
414 if (flags & WSC_FLAGS_LF) {
416 wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
420 message_length = WPA_GET_BE16(pos);
423 if (message_length < end - pos) {
424 wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
431 wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
432 "Flags 0x%x Message Length %d",
433 op_code, flags, message_length);
435 if (data->state == WAIT_FRAG_ACK) {
436 if (op_code != WSC_FRAG_ACK) {
437 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
438 "in WAIT_FRAG_ACK state", op_code);
442 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
443 eap_wsc_state(data, MSG);
444 return eap_wsc_build_msg(data, ret, id);
447 if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
448 op_code != WSC_Done && op_code != WSC_Start) {
449 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
455 if (data->state == WAIT_START) {
456 if (op_code != WSC_Start) {
457 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
458 "in WAIT_START state", op_code);
462 wpa_printf(MSG_DEBUG, "EAP-WSC: Received start");
463 eap_wsc_state(data, MSG);
464 /* Start message has empty payload, skip processing */
466 } else if (op_code == WSC_Start) {
467 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
474 eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
479 if (flags & WSC_FLAGS_MF) {
480 return eap_wsc_process_fragment(data, ret, id, flags, op_code,
485 if (data->in_buf == NULL) {
486 /* Wrap unfragmented messages as wpabuf without extra copy */
487 wpabuf_set(&tmpbuf, pos, end - pos);
488 data->in_buf = &tmpbuf;
491 res = wps_process_msg(data->wps, op_code, data->in_buf);
494 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
495 "successfully - wait for EAP failure");
496 eap_wsc_state(data, FAIL);
499 eap_wsc_state(data, MSG);
502 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
503 eap_wsc_state(data, FAIL);
506 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing pending");
508 if (data->in_buf == &tmpbuf)
513 if (data->in_buf != &tmpbuf)
514 wpabuf_free(data->in_buf);
518 if (data->out_buf == NULL) {
519 data->out_buf = wps_get_msg(data->wps, &data->out_op_code);
520 if (data->out_buf == NULL) {
521 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to receive "
528 eap_wsc_state(data, MSG);
529 return eap_wsc_build_msg(data, ret, id);
533 int eap_peer_wsc_register(void)
535 struct eap_method *eap;
538 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
539 EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
544 eap->init = eap_wsc_init;
545 eap->deinit = eap_wsc_deinit;
546 eap->process = eap_wsc_process;
548 ret = eap_peer_method_register(eap);
550 eap_peer_method_free(eap);