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, MESG, 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 wpa_printf(MSG_DEBUG, "EAP-WSC: Received new WPA/WPA2-PSK from WPS for"
72 " STA " MACSTR, MAC2STR(mac_addr));
73 wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
81 static void eap_wsc_pin_needed_cb(void *ctx, const u8 *uuid_e,
82 const struct wps_device_data *dev)
84 char uuid[40], txt[400];
86 if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
88 wpa_printf(MSG_DEBUG, "EAP-WSC: PIN needed for E-UUID %s", uuid);
89 len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED "
90 "%s " MACSTR " [%s|%s|%s|%s|%s|%d-%08X-%d]",
91 uuid, MAC2STR(dev->mac_addr), dev->device_name,
92 dev->manufacturer, dev->model_name,
93 dev->model_number, dev->serial_number,
94 dev->categ, dev->oui, dev->sub_categ);
95 if (len > 0 && len < (int) sizeof(txt))
96 wpa_printf(MSG_INFO, "%s", txt);
100 static void * eap_wsc_init(struct eap_sm *sm)
102 struct eap_wsc_data *data;
106 struct wps_config cfg;
109 struct wps_context *wps;
113 wpa_printf(MSG_ERROR, "EAP-WSC: WPS context not available");
117 identity = eap_get_config_identity(sm, &identity_len);
119 if (identity && identity_len == WSC_ID_REGISTRAR_LEN &&
120 os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0)
121 registrar = 1; /* Supplicant is Registrar */
122 else if (identity && identity_len == WSC_ID_ENROLLEE_LEN &&
123 os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0)
124 registrar = 0; /* Supplicant is Enrollee */
126 wpa_hexdump_ascii(MSG_INFO, "EAP-WSC: Unexpected identity",
127 identity, identity_len);
131 data = os_zalloc(sizeof(*data));
134 data->state = registrar ? MESG : WAIT_START;
135 data->registrar = registrar;
139 struct wps_registrar_config rcfg;
141 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
142 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
144 os_memset(&rcfg, 0, sizeof(rcfg));
145 rcfg.new_psk_cb = eap_wsc_new_psk_cb;
146 rcfg.pin_needed_cb = eap_wsc_pin_needed_cb;
149 wps->registrar = wps_registrar_init(wps, &rcfg);
150 if (wps->registrar == NULL) {
151 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to initialize "
153 os_free(wps->network_key);
161 os_memset(&cfg, 0, sizeof(cfg));
162 cfg.authenticator = 0;
164 cfg.registrar = registrar ? data->wps_ctx->registrar : NULL;
165 cfg.enrollee_mac_addr = registrar ? NULL : wps->dev.mac_addr;
167 phase1 = eap_get_config_phase1(sm);
168 if (phase1 == NULL) {
169 wpa_printf(MSG_INFO, "EAP-WSC: phase1 configuration data not "
175 pos = os_strstr(phase1, "pin=");
178 cfg.pin = (const u8 *) pos;
179 while (*pos != '\0' && *pos != ' ')
181 cfg.pin_len = pos - (const char *) cfg.pin;
183 pos = os_strstr(phase1, "pbc=1");
188 if (cfg.pin == NULL && !cfg.pbc) {
189 wpa_printf(MSG_INFO, "EAP-WSC: PIN or PBC not set in phase1 "
190 "configuration data");
195 cfg.uuid = registrar ? NULL : wps->uuid;
196 data->wps = wps_init(&cfg);
197 if (data->wps == NULL) {
201 data->fragment_size = WSC_FRAGMENT_SIZE;
206 wpa_printf(MSG_INFO, "EAP-WSC: Registrar functionality not "
207 "yet fully supported - using test values");
209 os_memset(uuid_e, 0, UUID_LEN);
211 wps_registrar_add_pin(data->wps_ctx->registrar, uuid_e,
212 cfg.pin, cfg.pin_len);
220 static void eap_wsc_deinit(struct eap_sm *sm, void *priv)
222 struct eap_wsc_data *data = priv;
223 wpabuf_free(data->in_buf);
224 wpabuf_free(data->out_buf);
225 wps_deinit(data->wps);
226 wps_registrar_deinit(data->wps_ctx->registrar);
227 data->wps_ctx->registrar = NULL;
228 os_free(data->wps_ctx->network_key);
229 data->wps_ctx->network_key = NULL;
234 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data,
235 struct eap_method_ret *ret, u8 id)
239 size_t send_len, plen;
242 wpa_printf(MSG_DEBUG, "EAP-WSC: Generating Response");
243 ret->allowNotifications = TRUE;
246 send_len = wpabuf_len(data->out_buf) - data->out_used;
247 if (2 + send_len > data->fragment_size) {
248 send_len = data->fragment_size - 2;
249 flags |= WSC_FLAGS_MF;
250 if (data->out_used == 0) {
251 flags |= WSC_FLAGS_LF;
256 if (flags & WSC_FLAGS_LF)
258 resp = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
259 EAP_CODE_RESPONSE, id);
263 wpabuf_put_u8(resp, data->out_op_code); /* Op-Code */
264 wpabuf_put_u8(resp, flags); /* Flags */
265 if (flags & WSC_FLAGS_LF)
266 wpabuf_put_be16(resp, wpabuf_len(data->out_buf));
268 wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
270 data->out_used += send_len;
272 ret->methodState = METHOD_MAY_CONT;
273 ret->decision = DECISION_FAIL;
275 if (data->out_used == wpabuf_len(data->out_buf)) {
276 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
277 "(message sent completely)",
278 (unsigned long) send_len);
279 wpabuf_free(data->out_buf);
280 data->out_buf = NULL;
282 if ((data->state == FAIL && data->out_op_code == WSC_ACK) ||
283 data->out_op_code == WSC_NACK ||
284 data->out_op_code == WSC_Done) {
285 eap_wsc_state(data, FAIL);
286 ret->methodState = METHOD_DONE;
288 eap_wsc_state(data, MESG);
290 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
291 "(%lu more to send)", (unsigned long) send_len,
292 (unsigned long) wpabuf_len(data->out_buf) -
294 eap_wsc_state(data, WAIT_FRAG_ACK);
301 static int eap_wsc_process_cont(struct eap_wsc_data *data,
302 const u8 *buf, size_t len, u8 op_code)
304 /* Process continuation of a pending message */
305 if (op_code != data->in_op_code) {
306 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
307 "fragment (expected %d)",
308 op_code, data->in_op_code);
312 if (len > wpabuf_tailroom(data->in_buf)) {
313 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
314 eap_wsc_state(data, FAIL);
318 wpabuf_put_data(data->in_buf, buf, len);
319 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting "
320 "for %lu bytes more", (unsigned long) len,
321 (unsigned long) wpabuf_tailroom(data->in_buf));
327 static struct wpabuf * eap_wsc_process_fragment(struct eap_wsc_data *data,
328 struct eap_method_ret *ret,
329 u8 id, u8 flags, u8 op_code,
331 const u8 *buf, size_t len)
333 /* Process a fragment that is not the last one of the message */
334 if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
335 wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length field in a "
336 "fragmented packet");
341 if (data->in_buf == NULL) {
342 /* First fragment of the message */
343 data->in_buf = wpabuf_alloc(message_length);
344 if (data->in_buf == NULL) {
345 wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
350 data->in_op_code = op_code;
351 wpabuf_put_data(data->in_buf, buf, len);
352 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in first "
353 "fragment, waiting for %lu bytes more",
355 (unsigned long) wpabuf_tailroom(data->in_buf));
358 return eap_wsc_build_frag_ack(id, EAP_CODE_RESPONSE);
362 static struct wpabuf * eap_wsc_process(struct eap_sm *sm, void *priv,
363 struct eap_method_ret *ret,
364 const struct wpabuf *reqData)
366 struct eap_wsc_data *data = priv;
367 const u8 *start, *pos, *end;
369 u8 op_code, flags, id;
370 u16 message_length = 0;
371 enum wps_process_res res;
372 struct wpabuf tmpbuf;
374 pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, reqData,
376 if (pos == NULL || len < 2) {
381 id = eap_get_id(reqData);
388 if (flags & WSC_FLAGS_LF) {
390 wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
394 message_length = WPA_GET_BE16(pos);
397 if (message_length < end - pos) {
398 wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
405 wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
406 "Flags 0x%x Message Length %d",
407 op_code, flags, message_length);
409 if (data->state == WAIT_FRAG_ACK) {
410 if (op_code != WSC_FRAG_ACK) {
411 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
412 "in WAIT_FRAG_ACK state", op_code);
416 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
417 eap_wsc_state(data, MESG);
418 return eap_wsc_build_msg(data, ret, id);
421 if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
422 op_code != WSC_Done && op_code != WSC_Start) {
423 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
429 if (data->state == WAIT_START) {
430 if (op_code != WSC_Start) {
431 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
432 "in WAIT_START state", op_code);
436 wpa_printf(MSG_DEBUG, "EAP-WSC: Received start");
437 eap_wsc_state(data, MESG);
438 /* Start message has empty payload, skip processing */
440 } else if (op_code == WSC_Start) {
441 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
448 eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
453 if (flags & WSC_FLAGS_MF) {
454 return eap_wsc_process_fragment(data, ret, id, flags, op_code,
459 if (data->in_buf == NULL) {
460 /* Wrap unfragmented messages as wpabuf without extra copy */
461 wpabuf_set(&tmpbuf, pos, end - pos);
462 data->in_buf = &tmpbuf;
465 res = wps_process_msg(data->wps, op_code, data->in_buf);
468 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
469 "successfully - wait for EAP failure");
470 eap_wsc_state(data, FAIL);
473 eap_wsc_state(data, MESG);
476 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
477 eap_wsc_state(data, FAIL);
480 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing pending");
482 if (data->in_buf == &tmpbuf)
487 if (data->in_buf != &tmpbuf)
488 wpabuf_free(data->in_buf);
492 if (data->out_buf == NULL) {
493 data->out_buf = wps_get_msg(data->wps, &data->out_op_code);
494 if (data->out_buf == NULL) {
495 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to receive "
502 eap_wsc_state(data, MESG);
503 return eap_wsc_build_msg(data, ret, id);
507 int eap_peer_wsc_register(void)
509 struct eap_method *eap;
512 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
513 EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
518 eap->init = eap_wsc_init;
519 eap->deinit = eap_wsc_deinit;
520 eap->process = eap_wsc_process;
522 ret = eap_peer_method_register(eap);
524 eap_peer_method_free(eap);