2 * EAP-WSC peer for Wi-Fi Protected Setup
3 * Copyright (c) 2007-2009, 2012, 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;
140 u8 dev_pw[WPS_OOB_DEVICE_PASSWORD_LEN];
145 wpa_printf(MSG_ERROR, "EAP-WSC: WPS context not available");
149 identity = eap_get_config_identity(sm, &identity_len);
151 if (identity && identity_len == WSC_ID_REGISTRAR_LEN &&
152 os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0)
153 registrar = 1; /* Supplicant is Registrar */
154 else if (identity && identity_len == WSC_ID_ENROLLEE_LEN &&
155 os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0)
156 registrar = 0; /* Supplicant is Enrollee */
158 wpa_hexdump_ascii(MSG_INFO, "EAP-WSC: Unexpected identity",
159 identity, identity_len);
163 data = os_zalloc(sizeof(*data));
166 data->state = registrar ? MESG : WAIT_START;
167 data->registrar = registrar;
170 os_memset(&cfg, 0, sizeof(cfg));
172 cfg.registrar = registrar;
174 phase1 = eap_get_config_phase1(sm);
175 if (phase1 == NULL) {
176 wpa_printf(MSG_INFO, "EAP-WSC: phase1 configuration data not "
182 pos = os_strstr(phase1, "pin=");
185 cfg.pin = (const u8 *) pos;
186 while (*pos != '\0' && *pos != ' ')
188 cfg.pin_len = pos - (const char *) cfg.pin;
189 if (cfg.pin_len >= WPS_OOB_DEVICE_PASSWORD_MIN_LEN * 2 &&
190 cfg.pin_len <= WPS_OOB_DEVICE_PASSWORD_LEN * 2 &&
191 hexstr2bin((const char *) cfg.pin, dev_pw,
192 cfg.pin_len / 2) == 0) {
193 /* Convert OOB Device Password to binary */
197 if (cfg.pin_len == 6 && os_strncmp(pos, "nfc-pw", 6) == 0) {
203 pos = os_strstr(phase1, "pbc=1");
208 if (cfg.pin == NULL && !cfg.pbc && !nfc) {
209 wpa_printf(MSG_INFO, "EAP-WSC: PIN or PBC not set in phase1 "
210 "configuration data");
215 pos = os_strstr(phase1, "dev_pw_id=");
217 cfg.dev_pw_id = atoi(pos + 10);
219 res = eap_wsc_new_ap_settings(&new_ap_settings, phase1);
225 wpa_printf(MSG_DEBUG, "EAP-WSC: Provide new AP settings for "
227 cfg.new_ap_settings = &new_ap_settings;
230 data->wps = wps_init(&cfg);
231 if (data->wps == NULL) {
235 res = eap_get_config_fragment_size(sm);
237 data->fragment_size = res;
239 data->fragment_size = WSC_FRAGMENT_SIZE;
240 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment size limit %u",
241 (unsigned int) data->fragment_size);
243 if (registrar && cfg.pin) {
244 wps_registrar_add_pin(data->wps_ctx->registrar, NULL, NULL,
245 cfg.pin, cfg.pin_len, 0);
248 /* Use reduced client timeout for WPS to avoid long wait */
249 if (sm->ClientTimeout > 30)
250 sm->ClientTimeout = 30;
256 static void eap_wsc_deinit(struct eap_sm *sm, void *priv)
258 struct eap_wsc_data *data = priv;
259 wpabuf_free(data->in_buf);
260 wpabuf_free(data->out_buf);
261 wps_deinit(data->wps);
262 os_free(data->wps_ctx->network_key);
263 data->wps_ctx->network_key = NULL;
268 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data,
269 struct eap_method_ret *ret, u8 id)
273 size_t send_len, plen;
276 wpa_printf(MSG_DEBUG, "EAP-WSC: Generating Response");
277 ret->allowNotifications = TRUE;
280 send_len = wpabuf_len(data->out_buf) - data->out_used;
281 if (2 + send_len > data->fragment_size) {
282 send_len = data->fragment_size - 2;
283 flags |= WSC_FLAGS_MF;
284 if (data->out_used == 0) {
285 flags |= WSC_FLAGS_LF;
290 if (flags & WSC_FLAGS_LF)
292 resp = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
293 EAP_CODE_RESPONSE, id);
297 wpabuf_put_u8(resp, data->out_op_code); /* Op-Code */
298 wpabuf_put_u8(resp, flags); /* Flags */
299 if (flags & WSC_FLAGS_LF)
300 wpabuf_put_be16(resp, wpabuf_len(data->out_buf));
302 wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
304 data->out_used += send_len;
306 ret->methodState = METHOD_MAY_CONT;
307 ret->decision = DECISION_FAIL;
309 if (data->out_used == wpabuf_len(data->out_buf)) {
310 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
311 "(message sent completely)",
312 (unsigned long) send_len);
313 wpabuf_free(data->out_buf);
314 data->out_buf = NULL;
316 if ((data->state == FAIL && data->out_op_code == WSC_ACK) ||
317 data->out_op_code == WSC_NACK ||
318 data->out_op_code == WSC_Done) {
319 eap_wsc_state(data, FAIL);
320 ret->methodState = METHOD_DONE;
322 eap_wsc_state(data, MESG);
324 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
325 "(%lu more to send)", (unsigned long) send_len,
326 (unsigned long) wpabuf_len(data->out_buf) -
328 eap_wsc_state(data, WAIT_FRAG_ACK);
335 static int eap_wsc_process_cont(struct eap_wsc_data *data,
336 const u8 *buf, size_t len, u8 op_code)
338 /* Process continuation of a pending message */
339 if (op_code != data->in_op_code) {
340 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
341 "fragment (expected %d)",
342 op_code, data->in_op_code);
346 if (len > wpabuf_tailroom(data->in_buf)) {
347 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
348 eap_wsc_state(data, FAIL);
352 wpabuf_put_data(data->in_buf, buf, len);
353 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting "
354 "for %lu bytes more", (unsigned long) len,
355 (unsigned long) wpabuf_tailroom(data->in_buf));
361 static struct wpabuf * eap_wsc_process_fragment(struct eap_wsc_data *data,
362 struct eap_method_ret *ret,
363 u8 id, u8 flags, u8 op_code,
365 const u8 *buf, size_t len)
367 /* Process a fragment that is not the last one of the message */
368 if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
369 wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length field in a "
370 "fragmented packet");
375 if (data->in_buf == NULL) {
376 /* First fragment of the message */
377 data->in_buf = wpabuf_alloc(message_length);
378 if (data->in_buf == NULL) {
379 wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
384 data->in_op_code = op_code;
385 wpabuf_put_data(data->in_buf, buf, len);
386 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in first "
387 "fragment, waiting for %lu bytes more",
389 (unsigned long) wpabuf_tailroom(data->in_buf));
392 return eap_wsc_build_frag_ack(id, EAP_CODE_RESPONSE);
396 static struct wpabuf * eap_wsc_process(struct eap_sm *sm, void *priv,
397 struct eap_method_ret *ret,
398 const struct wpabuf *reqData)
400 struct eap_wsc_data *data = priv;
401 const u8 *start, *pos, *end;
403 u8 op_code, flags, id;
404 u16 message_length = 0;
405 enum wps_process_res res;
406 struct wpabuf tmpbuf;
409 pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, reqData,
411 if (pos == NULL || len < 2) {
416 id = eap_get_id(reqData);
423 if (flags & WSC_FLAGS_LF) {
425 wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
429 message_length = WPA_GET_BE16(pos);
432 if (message_length < end - pos) {
433 wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
440 wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
441 "Flags 0x%x Message Length %d",
442 op_code, flags, message_length);
444 if (data->state == WAIT_FRAG_ACK) {
445 if (op_code != WSC_FRAG_ACK) {
446 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
447 "in WAIT_FRAG_ACK state", op_code);
451 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
452 eap_wsc_state(data, MESG);
453 return eap_wsc_build_msg(data, ret, id);
456 if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
457 op_code != WSC_Done && op_code != WSC_Start) {
458 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
464 if (data->state == WAIT_START) {
465 if (op_code != WSC_Start) {
466 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
467 "in WAIT_START state", op_code);
471 wpa_printf(MSG_DEBUG, "EAP-WSC: Received start");
472 eap_wsc_state(data, MESG);
473 /* Start message has empty payload, skip processing */
475 } else if (op_code == WSC_Start) {
476 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
483 eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
488 if (flags & WSC_FLAGS_MF) {
489 return eap_wsc_process_fragment(data, ret, id, flags, op_code,
494 if (data->in_buf == NULL) {
495 /* Wrap unfragmented messages as wpabuf without extra copy */
496 wpabuf_set(&tmpbuf, pos, end - pos);
497 data->in_buf = &tmpbuf;
500 res = wps_process_msg(data->wps, op_code, data->in_buf);
503 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
504 "successfully - wait for EAP failure");
505 eap_wsc_state(data, FAIL);
508 eap_wsc_state(data, MESG);
512 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
513 eap_wsc_state(data, FAIL);
517 if (data->in_buf != &tmpbuf)
518 wpabuf_free(data->in_buf);
522 if (data->out_buf == NULL) {
523 data->out_buf = wps_get_msg(data->wps, &data->out_op_code);
524 if (data->out_buf == NULL) {
525 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to receive "
532 eap_wsc_state(data, MESG);
533 r = eap_wsc_build_msg(data, ret, id);
534 if (data->state == FAIL && ret->methodState == METHOD_DONE) {
535 /* Use reduced client timeout for WPS to avoid long wait */
536 if (sm->ClientTimeout > 2)
537 sm->ClientTimeout = 2;
543 int eap_peer_wsc_register(void)
545 struct eap_method *eap;
548 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
549 EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
554 eap->init = eap_wsc_init;
555 eap->deinit = eap_wsc_deinit;
556 eap->process = eap_wsc_process;
558 ret = eap_peer_method_register(eap);
560 eap_peer_method_free(eap);