2 * EAP-WSC peer for Wi-Fi Protected Setup
3 * Copyright (c) 2007-2009, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
14 #include "eap_common/eap_wsc_common.h"
16 #include "wps/wps_defs.h"
20 enum { WAIT_START, MESG, FRAG_ACK, WAIT_FRAG_ACK, DONE, FAIL } state;
22 struct wpabuf *in_buf;
23 struct wpabuf *out_buf;
24 enum wsc_op_code in_op_code, out_op_code;
28 struct wps_context *wps_ctx;
32 static const char * eap_wsc_state_txt(int state)
42 return "WAIT_FRAG_ACK";
53 static void eap_wsc_state(struct eap_wsc_data *data, int state)
55 wpa_printf(MSG_DEBUG, "EAP-WSC: %s -> %s",
56 eap_wsc_state_txt(data->state),
57 eap_wsc_state_txt(state));
62 static int eap_wsc_new_ap_settings(struct wps_credential *cred,
65 const char *pos, *end;
68 os_memset(cred, 0, sizeof(*cred));
70 pos = os_strstr(params, "new_ssid=");
74 end = os_strchr(pos, ' ');
79 if ((len & 1) || len > 2 * sizeof(cred->ssid) ||
80 hexstr2bin(pos, cred->ssid, len / 2))
82 cred->ssid_len = len / 2;
84 pos = os_strstr(params, "new_auth=");
87 if (os_strncmp(pos + 9, "OPEN", 4) == 0)
88 cred->auth_type = WPS_AUTH_OPEN;
89 else if (os_strncmp(pos + 9, "WPAPSK", 6) == 0)
90 cred->auth_type = WPS_AUTH_WPAPSK;
91 else if (os_strncmp(pos + 9, "WPA2PSK", 7) == 0)
92 cred->auth_type = WPS_AUTH_WPA2PSK;
96 pos = os_strstr(params, "new_encr=");
99 if (os_strncmp(pos + 9, "NONE", 4) == 0)
100 cred->encr_type = WPS_ENCR_NONE;
101 else if (os_strncmp(pos + 9, "WEP", 3) == 0)
102 cred->encr_type = WPS_ENCR_WEP;
103 else if (os_strncmp(pos + 9, "TKIP", 4) == 0)
104 cred->encr_type = WPS_ENCR_TKIP;
105 else if (os_strncmp(pos + 9, "CCMP", 4) == 0)
106 cred->encr_type = WPS_ENCR_AES;
110 pos = os_strstr(params, "new_key=");
114 end = os_strchr(pos, ' ');
116 len = os_strlen(pos);
119 if ((len & 1) || len > 2 * sizeof(cred->key) ||
120 hexstr2bin(pos, cred->key, len / 2))
122 cred->key_len = len / 2;
128 static void * eap_wsc_init(struct eap_sm *sm)
130 struct eap_wsc_data *data;
134 struct wps_config cfg;
137 struct wps_context *wps;
138 struct wps_credential new_ap_settings;
143 wpa_printf(MSG_ERROR, "EAP-WSC: WPS context not available");
147 identity = eap_get_config_identity(sm, &identity_len);
149 if (identity && identity_len == WSC_ID_REGISTRAR_LEN &&
150 os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0)
151 registrar = 1; /* Supplicant is Registrar */
152 else if (identity && identity_len == WSC_ID_ENROLLEE_LEN &&
153 os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0)
154 registrar = 0; /* Supplicant is Enrollee */
156 wpa_hexdump_ascii(MSG_INFO, "EAP-WSC: Unexpected identity",
157 identity, identity_len);
161 data = os_zalloc(sizeof(*data));
164 data->state = registrar ? MESG : WAIT_START;
165 data->registrar = registrar;
168 os_memset(&cfg, 0, sizeof(cfg));
170 cfg.registrar = registrar;
172 phase1 = eap_get_config_phase1(sm);
173 if (phase1 == NULL) {
174 wpa_printf(MSG_INFO, "EAP-WSC: phase1 configuration data not "
180 pos = os_strstr(phase1, "pin=");
183 cfg.pin = (const u8 *) pos;
184 while (*pos != '\0' && *pos != ' ')
186 cfg.pin_len = pos - (const char *) cfg.pin;
188 pos = os_strstr(phase1, "pbc=1");
193 if (cfg.pin == NULL && !cfg.pbc) {
194 wpa_printf(MSG_INFO, "EAP-WSC: PIN or PBC not set in phase1 "
195 "configuration data");
200 pos = os_strstr(phase1, "dev_pw_id=");
202 cfg.dev_pw_id = atoi(pos + 10);
204 res = eap_wsc_new_ap_settings(&new_ap_settings, phase1);
210 wpa_printf(MSG_DEBUG, "EAP-WSC: Provide new AP settings for "
212 cfg.new_ap_settings = &new_ap_settings;
215 data->wps = wps_init(&cfg);
216 if (data->wps == NULL) {
220 res = eap_get_config_fragment_size(sm);
222 data->fragment_size = res;
224 data->fragment_size = WSC_FRAGMENT_SIZE;
225 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment size limit %u",
226 (unsigned int) data->fragment_size);
228 if (registrar && cfg.pin) {
229 wps_registrar_add_pin(data->wps_ctx->registrar, NULL, NULL,
230 cfg.pin, cfg.pin_len, 0);
233 /* Use reduced client timeout for WPS to avoid long wait */
234 if (sm->ClientTimeout > 30)
235 sm->ClientTimeout = 30;
241 static void eap_wsc_deinit(struct eap_sm *sm, void *priv)
243 struct eap_wsc_data *data = priv;
244 wpabuf_free(data->in_buf);
245 wpabuf_free(data->out_buf);
246 wps_deinit(data->wps);
247 os_free(data->wps_ctx->network_key);
248 data->wps_ctx->network_key = NULL;
253 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data,
254 struct eap_method_ret *ret, u8 id)
258 size_t send_len, plen;
261 wpa_printf(MSG_DEBUG, "EAP-WSC: Generating Response");
262 ret->allowNotifications = TRUE;
265 send_len = wpabuf_len(data->out_buf) - data->out_used;
266 if (2 + send_len > data->fragment_size) {
267 send_len = data->fragment_size - 2;
268 flags |= WSC_FLAGS_MF;
269 if (data->out_used == 0) {
270 flags |= WSC_FLAGS_LF;
275 if (flags & WSC_FLAGS_LF)
277 resp = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
278 EAP_CODE_RESPONSE, id);
282 wpabuf_put_u8(resp, data->out_op_code); /* Op-Code */
283 wpabuf_put_u8(resp, flags); /* Flags */
284 if (flags & WSC_FLAGS_LF)
285 wpabuf_put_be16(resp, wpabuf_len(data->out_buf));
287 wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
289 data->out_used += send_len;
291 ret->methodState = METHOD_MAY_CONT;
292 ret->decision = DECISION_FAIL;
294 if (data->out_used == wpabuf_len(data->out_buf)) {
295 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
296 "(message sent completely)",
297 (unsigned long) send_len);
298 wpabuf_free(data->out_buf);
299 data->out_buf = NULL;
301 if ((data->state == FAIL && data->out_op_code == WSC_ACK) ||
302 data->out_op_code == WSC_NACK ||
303 data->out_op_code == WSC_Done) {
304 eap_wsc_state(data, FAIL);
305 ret->methodState = METHOD_DONE;
307 eap_wsc_state(data, MESG);
309 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
310 "(%lu more to send)", (unsigned long) send_len,
311 (unsigned long) wpabuf_len(data->out_buf) -
313 eap_wsc_state(data, WAIT_FRAG_ACK);
320 static int eap_wsc_process_cont(struct eap_wsc_data *data,
321 const u8 *buf, size_t len, u8 op_code)
323 /* Process continuation of a pending message */
324 if (op_code != data->in_op_code) {
325 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
326 "fragment (expected %d)",
327 op_code, data->in_op_code);
331 if (len > wpabuf_tailroom(data->in_buf)) {
332 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
333 eap_wsc_state(data, FAIL);
337 wpabuf_put_data(data->in_buf, buf, len);
338 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting "
339 "for %lu bytes more", (unsigned long) len,
340 (unsigned long) wpabuf_tailroom(data->in_buf));
346 static struct wpabuf * eap_wsc_process_fragment(struct eap_wsc_data *data,
347 struct eap_method_ret *ret,
348 u8 id, u8 flags, u8 op_code,
350 const u8 *buf, size_t len)
352 /* Process a fragment that is not the last one of the message */
353 if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
354 wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length field in a "
355 "fragmented packet");
360 if (data->in_buf == NULL) {
361 /* First fragment of the message */
362 data->in_buf = wpabuf_alloc(message_length);
363 if (data->in_buf == NULL) {
364 wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
369 data->in_op_code = op_code;
370 wpabuf_put_data(data->in_buf, buf, len);
371 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in first "
372 "fragment, waiting for %lu bytes more",
374 (unsigned long) wpabuf_tailroom(data->in_buf));
377 return eap_wsc_build_frag_ack(id, EAP_CODE_RESPONSE);
381 static struct wpabuf * eap_wsc_process(struct eap_sm *sm, void *priv,
382 struct eap_method_ret *ret,
383 const struct wpabuf *reqData)
385 struct eap_wsc_data *data = priv;
386 const u8 *start, *pos, *end;
388 u8 op_code, flags, id;
389 u16 message_length = 0;
390 enum wps_process_res res;
391 struct wpabuf tmpbuf;
394 pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, reqData,
396 if (pos == NULL || len < 2) {
401 id = eap_get_id(reqData);
408 if (flags & WSC_FLAGS_LF) {
410 wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
414 message_length = WPA_GET_BE16(pos);
417 if (message_length < end - pos) {
418 wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
425 wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
426 "Flags 0x%x Message Length %d",
427 op_code, flags, message_length);
429 if (data->state == WAIT_FRAG_ACK) {
430 if (op_code != WSC_FRAG_ACK) {
431 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
432 "in WAIT_FRAG_ACK state", op_code);
436 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
437 eap_wsc_state(data, MESG);
438 return eap_wsc_build_msg(data, ret, id);
441 if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
442 op_code != WSC_Done && op_code != WSC_Start) {
443 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
449 if (data->state == WAIT_START) {
450 if (op_code != WSC_Start) {
451 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
452 "in WAIT_START state", op_code);
456 wpa_printf(MSG_DEBUG, "EAP-WSC: Received start");
457 eap_wsc_state(data, MESG);
458 /* Start message has empty payload, skip processing */
460 } else if (op_code == WSC_Start) {
461 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
468 eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
473 if (flags & WSC_FLAGS_MF) {
474 return eap_wsc_process_fragment(data, ret, id, flags, op_code,
479 if (data->in_buf == NULL) {
480 /* Wrap unfragmented messages as wpabuf without extra copy */
481 wpabuf_set(&tmpbuf, pos, end - pos);
482 data->in_buf = &tmpbuf;
485 res = wps_process_msg(data->wps, op_code, data->in_buf);
488 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
489 "successfully - wait for EAP failure");
490 eap_wsc_state(data, FAIL);
493 eap_wsc_state(data, MESG);
497 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
498 eap_wsc_state(data, FAIL);
502 if (data->in_buf != &tmpbuf)
503 wpabuf_free(data->in_buf);
507 if (data->out_buf == NULL) {
508 data->out_buf = wps_get_msg(data->wps, &data->out_op_code);
509 if (data->out_buf == NULL) {
510 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to receive "
517 eap_wsc_state(data, MESG);
518 r = eap_wsc_build_msg(data, ret, id);
519 if (data->state == FAIL && ret->methodState == METHOD_DONE) {
520 /* Use reduced client timeout for WPS to avoid long wait */
521 if (sm->ClientTimeout > 2)
522 sm->ClientTimeout = 2;
528 int eap_peer_wsc_register(void)
530 struct eap_method *eap;
533 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
534 EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
539 eap->init = eap_wsc_init;
540 eap->deinit = eap_wsc_deinit;
541 eap->process = eap_wsc_process;
543 ret = eap_peer_method_register(eap);
545 eap_peer_method_free(eap);