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)) {
81 wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid new_ssid");
84 cred->ssid_len = len / 2;
86 pos = os_strstr(params, "new_auth=");
88 wpa_printf(MSG_DEBUG, "EAP-WSC: Missing new_auth");
91 if (os_strncmp(pos + 9, "OPEN", 4) == 0)
92 cred->auth_type = WPS_AUTH_OPEN;
93 else if (os_strncmp(pos + 9, "WPAPSK", 6) == 0)
94 cred->auth_type = WPS_AUTH_WPAPSK;
95 else if (os_strncmp(pos + 9, "WPA2PSK", 7) == 0)
96 cred->auth_type = WPS_AUTH_WPA2PSK;
98 wpa_printf(MSG_DEBUG, "EAP-WSC: Unknown new_auth");
102 pos = os_strstr(params, "new_encr=");
104 wpa_printf(MSG_DEBUG, "EAP-WSC: Missing new_encr");
107 if (os_strncmp(pos + 9, "NONE", 4) == 0)
108 cred->encr_type = WPS_ENCR_NONE;
109 else if (os_strncmp(pos + 9, "WEP", 3) == 0)
110 cred->encr_type = WPS_ENCR_WEP;
111 else if (os_strncmp(pos + 9, "TKIP", 4) == 0)
112 cred->encr_type = WPS_ENCR_TKIP;
113 else if (os_strncmp(pos + 9, "CCMP", 4) == 0)
114 cred->encr_type = WPS_ENCR_AES;
116 wpa_printf(MSG_DEBUG, "EAP-WSC: Unknown new_encr");
120 pos = os_strstr(params, "new_key=");
124 end = os_strchr(pos, ' ');
126 len = os_strlen(pos);
129 if ((len & 1) || len > 2 * sizeof(cred->key) ||
130 hexstr2bin(pos, cred->key, len / 2)) {
131 wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid new_key");
134 cred->key_len = len / 2;
140 static void * eap_wsc_init(struct eap_sm *sm)
142 struct eap_wsc_data *data;
146 struct wps_config cfg;
147 const char *pos, *end;
149 struct wps_context *wps;
150 struct wps_credential new_ap_settings;
153 u8 pkhash[WPS_OOB_PUBKEY_HASH_LEN];
157 wpa_printf(MSG_ERROR, "EAP-WSC: WPS context not available");
161 identity = eap_get_config_identity(sm, &identity_len);
163 if (identity && identity_len == WSC_ID_REGISTRAR_LEN &&
164 os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0)
165 registrar = 1; /* Supplicant is Registrar */
166 else if (identity && identity_len == WSC_ID_ENROLLEE_LEN &&
167 os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0)
168 registrar = 0; /* Supplicant is Enrollee */
170 wpa_hexdump_ascii(MSG_INFO, "EAP-WSC: Unexpected identity",
171 identity, identity_len);
175 data = os_zalloc(sizeof(*data));
178 data->state = registrar ? MESG : WAIT_START;
179 data->registrar = registrar;
182 os_memset(&cfg, 0, sizeof(cfg));
184 cfg.registrar = registrar;
186 phase1 = eap_get_config_phase1(sm);
187 if (phase1 == NULL) {
188 wpa_printf(MSG_INFO, "EAP-WSC: phase1 configuration data not "
194 pos = os_strstr(phase1, "pin=");
197 cfg.pin = (const u8 *) pos;
198 while (*pos != '\0' && *pos != ' ')
200 cfg.pin_len = pos - (const char *) cfg.pin;
201 if (cfg.pin_len == 6 &&
202 os_strncmp((const char *) cfg.pin, "nfc-pw", 6) == 0) {
208 pos = os_strstr(phase1, "pbc=1");
213 pos = os_strstr(phase1, "dev_pw_id=");
215 u16 id = atoi(pos + 10);
216 if (id == DEV_PW_NFC_CONNECTION_HANDOVER)
218 if (cfg.pin || id == DEV_PW_NFC_CONNECTION_HANDOVER)
222 if (cfg.pin == NULL && !cfg.pbc && !nfc) {
223 wpa_printf(MSG_INFO, "EAP-WSC: PIN or PBC not set in phase1 "
224 "configuration data");
229 pos = os_strstr(phase1, " pkhash=");
233 end = os_strchr(pos, ' ');
237 len = os_strlen(pos);
238 if (len != 2 * WPS_OOB_PUBKEY_HASH_LEN ||
239 hexstr2bin(pos, pkhash, WPS_OOB_PUBKEY_HASH_LEN)) {
240 wpa_printf(MSG_INFO, "EAP-WSC: Invalid pkhash");
244 cfg.peer_pubkey_hash = pkhash;
247 res = eap_wsc_new_ap_settings(&new_ap_settings, phase1);
250 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to parse new AP "
255 wpa_printf(MSG_DEBUG, "EAP-WSC: Provide new AP settings for "
257 cfg.new_ap_settings = &new_ap_settings;
260 data->wps = wps_init(&cfg);
261 if (data->wps == NULL) {
263 wpa_printf(MSG_DEBUG, "EAP-WSC: wps_init failed");
266 res = eap_get_config_fragment_size(sm);
268 data->fragment_size = res;
270 data->fragment_size = WSC_FRAGMENT_SIZE;
271 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment size limit %u",
272 (unsigned int) data->fragment_size);
274 if (registrar && cfg.pin) {
275 wps_registrar_add_pin(data->wps_ctx->registrar, NULL, NULL,
276 cfg.pin, cfg.pin_len, 0);
279 /* Use reduced client timeout for WPS to avoid long wait */
280 if (sm->ClientTimeout > 30)
281 sm->ClientTimeout = 30;
287 static void eap_wsc_deinit(struct eap_sm *sm, void *priv)
289 struct eap_wsc_data *data = priv;
290 wpabuf_free(data->in_buf);
291 wpabuf_free(data->out_buf);
292 wps_deinit(data->wps);
293 os_free(data->wps_ctx->network_key);
294 data->wps_ctx->network_key = NULL;
299 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data,
300 struct eap_method_ret *ret, u8 id)
304 size_t send_len, plen;
307 wpa_printf(MSG_DEBUG, "EAP-WSC: Generating Response");
308 ret->allowNotifications = TRUE;
311 send_len = wpabuf_len(data->out_buf) - data->out_used;
312 if (2 + send_len > data->fragment_size) {
313 send_len = data->fragment_size - 2;
314 flags |= WSC_FLAGS_MF;
315 if (data->out_used == 0) {
316 flags |= WSC_FLAGS_LF;
321 if (flags & WSC_FLAGS_LF)
323 resp = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
324 EAP_CODE_RESPONSE, id);
328 wpabuf_put_u8(resp, data->out_op_code); /* Op-Code */
329 wpabuf_put_u8(resp, flags); /* Flags */
330 if (flags & WSC_FLAGS_LF)
331 wpabuf_put_be16(resp, wpabuf_len(data->out_buf));
333 wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
335 data->out_used += send_len;
337 ret->methodState = METHOD_MAY_CONT;
338 ret->decision = DECISION_FAIL;
340 if (data->out_used == wpabuf_len(data->out_buf)) {
341 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
342 "(message sent completely)",
343 (unsigned long) send_len);
344 wpabuf_free(data->out_buf);
345 data->out_buf = NULL;
347 if ((data->state == FAIL && data->out_op_code == WSC_ACK) ||
348 data->out_op_code == WSC_NACK ||
349 data->out_op_code == WSC_Done) {
350 eap_wsc_state(data, FAIL);
351 ret->methodState = METHOD_DONE;
353 eap_wsc_state(data, MESG);
355 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
356 "(%lu more to send)", (unsigned long) send_len,
357 (unsigned long) wpabuf_len(data->out_buf) -
359 eap_wsc_state(data, WAIT_FRAG_ACK);
366 static int eap_wsc_process_cont(struct eap_wsc_data *data,
367 const u8 *buf, size_t len, u8 op_code)
369 /* Process continuation of a pending message */
370 if (op_code != data->in_op_code) {
371 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
372 "fragment (expected %d)",
373 op_code, data->in_op_code);
377 if (len > wpabuf_tailroom(data->in_buf)) {
378 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
379 eap_wsc_state(data, FAIL);
383 wpabuf_put_data(data->in_buf, buf, len);
384 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting "
385 "for %lu bytes more", (unsigned long) len,
386 (unsigned long) wpabuf_tailroom(data->in_buf));
392 static struct wpabuf * eap_wsc_process_fragment(struct eap_wsc_data *data,
393 struct eap_method_ret *ret,
394 u8 id, u8 flags, u8 op_code,
396 const u8 *buf, size_t len)
398 /* Process a fragment that is not the last one of the message */
399 if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
400 wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length field in a "
401 "fragmented packet");
406 if (data->in_buf == NULL) {
407 /* First fragment of the message */
408 data->in_buf = wpabuf_alloc(message_length);
409 if (data->in_buf == NULL) {
410 wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
415 data->in_op_code = op_code;
416 wpabuf_put_data(data->in_buf, buf, len);
417 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in first "
418 "fragment, waiting for %lu bytes more",
420 (unsigned long) wpabuf_tailroom(data->in_buf));
423 return eap_wsc_build_frag_ack(id, EAP_CODE_RESPONSE);
427 static struct wpabuf * eap_wsc_process(struct eap_sm *sm, void *priv,
428 struct eap_method_ret *ret,
429 const struct wpabuf *reqData)
431 struct eap_wsc_data *data = priv;
432 const u8 *start, *pos, *end;
434 u8 op_code, flags, id;
435 u16 message_length = 0;
436 enum wps_process_res res;
437 struct wpabuf tmpbuf;
440 pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, reqData,
442 if (pos == NULL || len < 2) {
447 id = eap_get_id(reqData);
454 if (flags & WSC_FLAGS_LF) {
456 wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
460 message_length = WPA_GET_BE16(pos);
463 if (message_length < end - pos) {
464 wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
471 wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
472 "Flags 0x%x Message Length %d",
473 op_code, flags, message_length);
475 if (data->state == WAIT_FRAG_ACK) {
476 if (op_code != WSC_FRAG_ACK) {
477 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
478 "in WAIT_FRAG_ACK state", op_code);
482 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
483 eap_wsc_state(data, MESG);
484 return eap_wsc_build_msg(data, ret, id);
487 if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
488 op_code != WSC_Done && op_code != WSC_Start) {
489 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
495 if (data->state == WAIT_START) {
496 if (op_code != WSC_Start) {
497 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
498 "in WAIT_START state", op_code);
502 wpa_printf(MSG_DEBUG, "EAP-WSC: Received start");
503 eap_wsc_state(data, MESG);
504 /* Start message has empty payload, skip processing */
506 } else if (op_code == WSC_Start) {
507 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
514 eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
519 if (flags & WSC_FLAGS_MF) {
520 return eap_wsc_process_fragment(data, ret, id, flags, op_code,
525 if (data->in_buf == NULL) {
526 /* Wrap unfragmented messages as wpabuf without extra copy */
527 wpabuf_set(&tmpbuf, pos, end - pos);
528 data->in_buf = &tmpbuf;
531 res = wps_process_msg(data->wps, op_code, data->in_buf);
534 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
535 "successfully - wait for EAP failure");
536 eap_wsc_state(data, FAIL);
539 eap_wsc_state(data, MESG);
543 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
544 eap_wsc_state(data, FAIL);
548 if (data->in_buf != &tmpbuf)
549 wpabuf_free(data->in_buf);
553 if (data->out_buf == NULL) {
554 data->out_buf = wps_get_msg(data->wps, &data->out_op_code);
555 if (data->out_buf == NULL) {
556 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to receive "
563 eap_wsc_state(data, MESG);
564 r = eap_wsc_build_msg(data, ret, id);
565 if (data->state == FAIL && ret->methodState == METHOD_DONE) {
566 /* Use reduced client timeout for WPS to avoid long wait */
567 if (sm->ClientTimeout > 2)
568 sm->ClientTimeout = 2;
574 int eap_peer_wsc_register(void)
576 struct eap_method *eap;
579 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
580 EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
585 eap->init = eap_wsc_init;
586 eap->deinit = eap_wsc_deinit;
587 eap->process = eap_wsc_process;
589 ret = eap_peer_method_register(eap);
591 eap_peer_method_free(eap);