2 * EAP-WSC peer for Wi-Fi Protected Setup
3 * Copyright (c) 2007-2009, 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 enum wsc_op_code 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_ap_settings(struct wps_credential
*cred
,
71 const char *pos
, *end
;
74 os_memset(cred
, 0, sizeof(*cred
));
76 pos
= os_strstr(params
, "new_ssid=");
80 end
= os_strchr(pos
, ' ');
85 if ((len
& 1) || len
> 2 * sizeof(cred
->ssid
) ||
86 hexstr2bin(pos
, cred
->ssid
, len
/ 2))
88 cred
->ssid_len
= len
/ 2;
90 pos
= os_strstr(params
, "new_auth=");
93 if (os_strncmp(pos
+ 9, "OPEN", 4) == 0)
94 cred
->auth_type
= WPS_AUTH_OPEN
;
95 else if (os_strncmp(pos
+ 9, "WPAPSK", 6) == 0)
96 cred
->auth_type
= WPS_AUTH_WPAPSK
;
97 else if (os_strncmp(pos
+ 9, "WPA2PSK", 7) == 0)
98 cred
->auth_type
= WPS_AUTH_WPA2PSK
;
102 pos
= os_strstr(params
, "new_encr=");
105 if (os_strncmp(pos
+ 9, "NONE", 4) == 0)
106 cred
->encr_type
= WPS_ENCR_NONE
;
107 else if (os_strncmp(pos
+ 9, "WEP", 3) == 0)
108 cred
->encr_type
= WPS_ENCR_WEP
;
109 else if (os_strncmp(pos
+ 9, "TKIP", 4) == 0)
110 cred
->encr_type
= WPS_ENCR_TKIP
;
111 else if (os_strncmp(pos
+ 9, "CCMP", 4) == 0)
112 cred
->encr_type
= WPS_ENCR_AES
;
116 pos
= os_strstr(params
, "new_key=");
120 end
= os_strchr(pos
, ' ');
122 len
= os_strlen(pos
);
125 if ((len
& 1) || len
> 2 * sizeof(cred
->key
) ||
126 hexstr2bin(pos
, cred
->key
, len
/ 2))
128 cred
->key_len
= len
/ 2;
134 static void * eap_wsc_init(struct eap_sm
*sm
)
136 struct eap_wsc_data
*data
;
140 struct wps_config cfg
;
143 struct wps_context
*wps
;
144 struct wps_credential new_ap_settings
;
149 wpa_printf(MSG_ERROR
, "EAP-WSC: WPS context not available");
153 identity
= eap_get_config_identity(sm
, &identity_len
);
155 if (identity
&& identity_len
== WSC_ID_REGISTRAR_LEN
&&
156 os_memcmp(identity
, WSC_ID_REGISTRAR
, WSC_ID_REGISTRAR_LEN
) == 0)
157 registrar
= 1; /* Supplicant is Registrar */
158 else if (identity
&& identity_len
== WSC_ID_ENROLLEE_LEN
&&
159 os_memcmp(identity
, WSC_ID_ENROLLEE
, WSC_ID_ENROLLEE_LEN
) == 0)
160 registrar
= 0; /* Supplicant is Enrollee */
162 wpa_hexdump_ascii(MSG_INFO
, "EAP-WSC: Unexpected identity",
163 identity
, identity_len
);
167 data
= os_zalloc(sizeof(*data
));
170 data
->state
= registrar
? MESG
: WAIT_START
;
171 data
->registrar
= registrar
;
174 os_memset(&cfg
, 0, sizeof(cfg
));
176 cfg
.registrar
= registrar
;
178 phase1
= eap_get_config_phase1(sm
);
179 if (phase1
== NULL
) {
180 wpa_printf(MSG_INFO
, "EAP-WSC: phase1 configuration data not "
186 pos
= os_strstr(phase1
, "pin=");
189 cfg
.pin
= (const u8
*) pos
;
190 while (*pos
!= '\0' && *pos
!= ' ')
192 cfg
.pin_len
= pos
- (const char *) cfg
.pin
;
194 pos
= os_strstr(phase1
, "pbc=1");
199 if (cfg
.pin
== NULL
&& !cfg
.pbc
) {
200 wpa_printf(MSG_INFO
, "EAP-WSC: PIN or PBC not set in phase1 "
201 "configuration data");
206 res
= eap_wsc_new_ap_settings(&new_ap_settings
, phase1
);
212 wpa_printf(MSG_DEBUG
, "EAP-WSC: Provide new AP settings for "
214 cfg
.new_ap_settings
= &new_ap_settings
;
217 data
->wps
= wps_init(&cfg
);
218 if (data
->wps
== NULL
) {
222 data
->fragment_size
= WSC_FRAGMENT_SIZE
;
224 if (registrar
&& cfg
.pin
) {
225 wps_registrar_add_pin(data
->wps_ctx
->registrar
, NULL
,
226 cfg
.pin
, cfg
.pin_len
, 0);
229 /* Use reduced client timeout for WPS to avoid long wait */
230 if (sm
->ClientTimeout
> 30)
231 sm
->ClientTimeout
= 30;
237 static void eap_wsc_deinit(struct eap_sm
*sm
, void *priv
)
239 struct eap_wsc_data
*data
= priv
;
240 wpabuf_free(data
->in_buf
);
241 wpabuf_free(data
->out_buf
);
242 wps_deinit(data
->wps
);
243 os_free(data
->wps_ctx
->network_key
);
244 data
->wps_ctx
->network_key
= NULL
;
249 static struct wpabuf
* eap_wsc_build_msg(struct eap_wsc_data
*data
,
250 struct eap_method_ret
*ret
, u8 id
)
254 size_t send_len
, plen
;
257 wpa_printf(MSG_DEBUG
, "EAP-WSC: Generating Response");
258 ret
->allowNotifications
= TRUE
;
261 send_len
= wpabuf_len(data
->out_buf
) - data
->out_used
;
262 if (2 + send_len
> data
->fragment_size
) {
263 send_len
= data
->fragment_size
- 2;
264 flags
|= WSC_FLAGS_MF
;
265 if (data
->out_used
== 0) {
266 flags
|= WSC_FLAGS_LF
;
271 if (flags
& WSC_FLAGS_LF
)
273 resp
= eap_msg_alloc(EAP_VENDOR_WFA
, EAP_VENDOR_TYPE_WSC
, plen
,
274 EAP_CODE_RESPONSE
, id
);
278 wpabuf_put_u8(resp
, data
->out_op_code
); /* Op-Code */
279 wpabuf_put_u8(resp
, flags
); /* Flags */
280 if (flags
& WSC_FLAGS_LF
)
281 wpabuf_put_be16(resp
, wpabuf_len(data
->out_buf
));
283 wpabuf_put_data(resp
, wpabuf_head_u8(data
->out_buf
) + data
->out_used
,
285 data
->out_used
+= send_len
;
287 ret
->methodState
= METHOD_MAY_CONT
;
288 ret
->decision
= DECISION_FAIL
;
290 if (data
->out_used
== wpabuf_len(data
->out_buf
)) {
291 wpa_printf(MSG_DEBUG
, "EAP-WSC: Sending out %lu bytes "
292 "(message sent completely)",
293 (unsigned long) send_len
);
294 wpabuf_free(data
->out_buf
);
295 data
->out_buf
= NULL
;
297 if ((data
->state
== FAIL
&& data
->out_op_code
== WSC_ACK
) ||
298 data
->out_op_code
== WSC_NACK
||
299 data
->out_op_code
== WSC_Done
) {
300 eap_wsc_state(data
, FAIL
);
301 ret
->methodState
= METHOD_DONE
;
303 eap_wsc_state(data
, MESG
);
305 wpa_printf(MSG_DEBUG
, "EAP-WSC: Sending out %lu bytes "
306 "(%lu more to send)", (unsigned long) send_len
,
307 (unsigned long) wpabuf_len(data
->out_buf
) -
309 eap_wsc_state(data
, WAIT_FRAG_ACK
);
316 static int eap_wsc_process_cont(struct eap_wsc_data
*data
,
317 const u8
*buf
, size_t len
, u8 op_code
)
319 /* Process continuation of a pending message */
320 if (op_code
!= data
->in_op_code
) {
321 wpa_printf(MSG_DEBUG
, "EAP-WSC: Unexpected Op-Code %d in "
322 "fragment (expected %d)",
323 op_code
, data
->in_op_code
);
327 if (len
> wpabuf_tailroom(data
->in_buf
)) {
328 wpa_printf(MSG_DEBUG
, "EAP-WSC: Fragment overflow");
329 eap_wsc_state(data
, FAIL
);
333 wpabuf_put_data(data
->in_buf
, buf
, len
);
334 wpa_printf(MSG_DEBUG
, "EAP-WSC: Received %lu bytes, waiting "
335 "for %lu bytes more", (unsigned long) len
,
336 (unsigned long) wpabuf_tailroom(data
->in_buf
));
342 static struct wpabuf
* eap_wsc_process_fragment(struct eap_wsc_data
*data
,
343 struct eap_method_ret
*ret
,
344 u8 id
, u8 flags
, u8 op_code
,
346 const u8
*buf
, size_t len
)
348 /* Process a fragment that is not the last one of the message */
349 if (data
->in_buf
== NULL
&& !(flags
& WSC_FLAGS_LF
)) {
350 wpa_printf(MSG_DEBUG
, "EAP-WSC: No Message Length field in a "
351 "fragmented packet");
356 if (data
->in_buf
== NULL
) {
357 /* First fragment of the message */
358 data
->in_buf
= wpabuf_alloc(message_length
);
359 if (data
->in_buf
== NULL
) {
360 wpa_printf(MSG_DEBUG
, "EAP-WSC: No memory for "
365 data
->in_op_code
= op_code
;
366 wpabuf_put_data(data
->in_buf
, buf
, len
);
367 wpa_printf(MSG_DEBUG
, "EAP-WSC: Received %lu bytes in first "
368 "fragment, waiting for %lu bytes more",
370 (unsigned long) wpabuf_tailroom(data
->in_buf
));
373 return eap_wsc_build_frag_ack(id
, EAP_CODE_RESPONSE
);
377 static struct wpabuf
* eap_wsc_process(struct eap_sm
*sm
, void *priv
,
378 struct eap_method_ret
*ret
,
379 const struct wpabuf
*reqData
)
381 struct eap_wsc_data
*data
= priv
;
382 const u8
*start
, *pos
, *end
;
384 u8 op_code
, flags
, id
;
385 u16 message_length
= 0;
386 enum wps_process_res res
;
387 struct wpabuf tmpbuf
;
390 pos
= eap_hdr_validate(EAP_VENDOR_WFA
, EAP_VENDOR_TYPE_WSC
, reqData
,
392 if (pos
== NULL
|| len
< 2) {
397 id
= eap_get_id(reqData
);
404 if (flags
& WSC_FLAGS_LF
) {
406 wpa_printf(MSG_DEBUG
, "EAP-WSC: Message underflow");
410 message_length
= WPA_GET_BE16(pos
);
413 if (message_length
< end
- pos
) {
414 wpa_printf(MSG_DEBUG
, "EAP-WSC: Invalid Message "
421 wpa_printf(MSG_DEBUG
, "EAP-WSC: Received packet: Op-Code %d "
422 "Flags 0x%x Message Length %d",
423 op_code
, flags
, message_length
);
425 if (data
->state
== WAIT_FRAG_ACK
) {
426 if (op_code
!= WSC_FRAG_ACK
) {
427 wpa_printf(MSG_DEBUG
, "EAP-WSC: Unexpected Op-Code %d "
428 "in WAIT_FRAG_ACK state", op_code
);
432 wpa_printf(MSG_DEBUG
, "EAP-WSC: Fragment acknowledged");
433 eap_wsc_state(data
, MESG
);
434 return eap_wsc_build_msg(data
, ret
, id
);
437 if (op_code
!= WSC_ACK
&& op_code
!= WSC_NACK
&& op_code
!= WSC_MSG
&&
438 op_code
!= WSC_Done
&& op_code
!= WSC_Start
) {
439 wpa_printf(MSG_DEBUG
, "EAP-WSC: Unexpected Op-Code %d",
445 if (data
->state
== WAIT_START
) {
446 if (op_code
!= WSC_Start
) {
447 wpa_printf(MSG_DEBUG
, "EAP-WSC: Unexpected Op-Code %d "
448 "in WAIT_START state", op_code
);
452 wpa_printf(MSG_DEBUG
, "EAP-WSC: Received start");
453 eap_wsc_state(data
, MESG
);
454 /* Start message has empty payload, skip processing */
456 } else if (op_code
== WSC_Start
) {
457 wpa_printf(MSG_DEBUG
, "EAP-WSC: Unexpected Op-Code %d",
464 eap_wsc_process_cont(data
, pos
, end
- pos
, op_code
) < 0) {
469 if (flags
& WSC_FLAGS_MF
) {
470 return eap_wsc_process_fragment(data
, ret
, id
, flags
, op_code
,
475 if (data
->in_buf
== NULL
) {
476 /* Wrap unfragmented messages as wpabuf without extra copy */
477 wpabuf_set(&tmpbuf
, pos
, end
- pos
);
478 data
->in_buf
= &tmpbuf
;
481 res
= wps_process_msg(data
->wps
, op_code
, data
->in_buf
);
484 wpa_printf(MSG_DEBUG
, "EAP-WSC: WPS processing completed "
485 "successfully - wait for EAP failure");
486 eap_wsc_state(data
, FAIL
);
489 eap_wsc_state(data
, MESG
);
493 wpa_printf(MSG_DEBUG
, "EAP-WSC: WPS processing failed");
494 eap_wsc_state(data
, FAIL
);
498 if (data
->in_buf
!= &tmpbuf
)
499 wpabuf_free(data
->in_buf
);
503 if (data
->out_buf
== NULL
) {
504 data
->out_buf
= wps_get_msg(data
->wps
, &data
->out_op_code
);
505 if (data
->out_buf
== NULL
) {
506 wpa_printf(MSG_DEBUG
, "EAP-WSC: Failed to receive "
513 eap_wsc_state(data
, MESG
);
514 r
= eap_wsc_build_msg(data
, ret
, id
);
515 if (data
->state
== FAIL
&& ret
->methodState
== METHOD_DONE
) {
516 /* Use reduced client timeout for WPS to avoid long wait */
517 if (sm
->ClientTimeout
> 2)
518 sm
->ClientTimeout
= 2;
524 int eap_peer_wsc_register(void)
526 struct eap_method
*eap
;
529 eap
= eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION
,
530 EAP_VENDOR_WFA
, EAP_VENDOR_TYPE_WSC
,
535 eap
->init
= eap_wsc_init
;
536 eap
->deinit
= eap_wsc_deinit
;
537 eap
->process
= eap_wsc_process
;
539 ret
= eap_peer_method_register(eap
);
541 eap_peer_method_free(eap
);