• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * EAP-WSC server for Wi-Fi Protected Setup
3  * Copyright (c) 2007-2008, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #include "includes.h"
10 
11 #include "common.h"
12 #include "eloop.h"
13 #include "eap_i.h"
14 #include "eap_common/eap_wsc_common.h"
15 #include "p2p/p2p.h"
16 #include "wps/wps.h"
17 
18 
19 struct eap_wsc_data {
20 	enum { START, MESG, FRAG_ACK, WAIT_FRAG_ACK, DONE, FAIL } state;
21 	int registrar;
22 	struct wpabuf *in_buf;
23 	struct wpabuf *out_buf;
24 	enum wsc_op_code in_op_code, out_op_code;
25 	size_t out_used;
26 	size_t fragment_size;
27 	struct wps_data *wps;
28 	int ext_reg_timeout;
29 };
30 
31 
32 #ifndef CONFIG_NO_STDOUT_DEBUG
eap_wsc_state_txt(int state)33 static const char * eap_wsc_state_txt(int state)
34 {
35 	switch (state) {
36 	case START:
37 		return "START";
38 	case MESG:
39 		return "MESG";
40 	case FRAG_ACK:
41 		return "FRAG_ACK";
42 	case WAIT_FRAG_ACK:
43 		return "WAIT_FRAG_ACK";
44 	case DONE:
45 		return "DONE";
46 	case FAIL:
47 		return "FAIL";
48 	default:
49 		return "?";
50 	}
51 }
52 #endif /* CONFIG_NO_STDOUT_DEBUG */
53 
54 
eap_wsc_state(struct eap_wsc_data * data,int state)55 static void eap_wsc_state(struct eap_wsc_data *data, int state)
56 {
57 #ifndef CONFIG_PRINT_NOUSE
58 	wpa_printf(MSG_DEBUG, "EAP-WSC: %s -> %s",
59 		   eap_wsc_state_txt(data->state),
60 		   eap_wsc_state_txt(state));
61 #endif /* CONFIG_PRINT_NOUSE */
62 	data->state = state;
63 }
64 
65 
eap_wsc_ext_reg_timeout(void * eloop_ctx,void * timeout_ctx)66 static void eap_wsc_ext_reg_timeout(void *eloop_ctx, void *timeout_ctx)
67 {
68 	struct eap_sm *sm = eloop_ctx;
69 	struct eap_wsc_data *data = timeout_ctx;
70 
71 	if (sm->method_pending != METHOD_PENDING_WAIT)
72 		return;
73 
74 	wpa_printf(MSG_DEBUG, "EAP-WSC: Timeout while waiting for an External "
75 		   "Registrar");
76 	data->ext_reg_timeout = 1;
77 	eap_sm_pending_cb(sm);
78 }
79 
80 
eap_wsc_init(struct eap_sm * sm)81 static void * eap_wsc_init(struct eap_sm *sm)
82 {
83 	struct eap_wsc_data *data;
84 	int registrar;
85 	struct wps_config cfg;
86 
87 	if (sm->identity && sm->identity_len == WSC_ID_REGISTRAR_LEN &&
88 	    os_memcmp(sm->identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) ==
89 	    0)
90 		registrar = 0; /* Supplicant is Registrar */
91 	else if (sm->identity && sm->identity_len == WSC_ID_ENROLLEE_LEN &&
92 		 os_memcmp(sm->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN)
93 		 == 0)
94 		registrar = 1; /* Supplicant is Enrollee */
95 	else {
96 		wpa_hexdump_ascii(MSG_INFO, "EAP-WSC: Unexpected identity",
97 				  sm->identity, sm->identity_len);
98 		return NULL;
99 	}
100 
101 	data = os_zalloc(sizeof(*data));
102 	if (data == NULL)
103 		return NULL;
104 	data->state = registrar ? START : MESG;
105 	data->registrar = registrar;
106 
107 	os_memset(&cfg, 0, sizeof(cfg));
108 	cfg.wps = sm->cfg->wps;
109 	cfg.registrar = registrar;
110 	if (registrar) {
111 		if (!sm->cfg->wps || !sm->cfg->wps->registrar) {
112 			wpa_printf(MSG_INFO, "EAP-WSC: WPS Registrar not "
113 				   "initialized");
114 			os_free(data);
115 			return NULL;
116 		}
117 	} else {
118 		if (sm->user == NULL || sm->user->password == NULL) {
119 			/*
120 			 * In theory, this should not really be needed, but
121 			 * Windows 7 uses Registrar mode to probe AP's WPS
122 			 * capabilities before trying to use Enrollee and fails
123 			 * if the AP does not allow that probing to happen..
124 			 */
125 			wpa_printf(MSG_DEBUG, "EAP-WSC: No AP PIN (password) "
126 				   "configured for Enrollee functionality - "
127 				   "allow for probing capabilities (M1)");
128 		} else {
129 			cfg.pin = sm->user->password;
130 			cfg.pin_len = sm->user->password_len;
131 		}
132 	}
133 	cfg.assoc_wps_ie = sm->assoc_wps_ie;
134 	cfg.peer_addr = sm->peer_addr;
135 #ifdef CONFIG_P2P
136 	if (sm->assoc_p2p_ie) {
137 		if (!sm->cfg->wps->use_passphrase) {
138 			wpa_printf(MSG_DEBUG,
139 				   "EAP-WSC: Prefer PSK format for non-6 GHz P2P client");
140 			cfg.use_psk_key = 1;
141 		}
142 		cfg.p2p_dev_addr = p2p_get_go_dev_addr(sm->assoc_p2p_ie);
143 	}
144 #endif /* CONFIG_P2P */
145 	cfg.pbc_in_m1 = sm->cfg->pbc_in_m1;
146 	data->wps = wps_init(&cfg);
147 	if (data->wps == NULL) {
148 		os_free(data);
149 		return NULL;
150 	}
151 	data->fragment_size = sm->cfg->fragment_size > 0 ?
152 		sm->cfg->fragment_size : WSC_FRAGMENT_SIZE;
153 
154 	return data;
155 }
156 
157 
eap_wsc_reset(struct eap_sm * sm,void * priv)158 static void eap_wsc_reset(struct eap_sm *sm, void *priv)
159 {
160 	struct eap_wsc_data *data = priv;
161 	eloop_cancel_timeout(eap_wsc_ext_reg_timeout, sm, data);
162 	wpabuf_free(data->in_buf);
163 	wpabuf_free(data->out_buf);
164 	wps_deinit(data->wps);
165 	os_free(data);
166 }
167 
168 
eap_wsc_build_start(struct eap_sm * sm,struct eap_wsc_data * data,u8 id)169 static struct wpabuf * eap_wsc_build_start(struct eap_sm *sm,
170 					   struct eap_wsc_data *data, u8 id)
171 {
172 	struct wpabuf *req;
173 
174 	req = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, 2,
175 			    EAP_CODE_REQUEST, id);
176 	if (req == NULL) {
177 		wpa_printf(MSG_ERROR, "EAP-WSC: Failed to allocate memory for "
178 			   "request");
179 		return NULL;
180 	}
181 
182 	wpa_printf(MSG_DEBUG, "EAP-WSC: Send WSC/Start");
183 	wpabuf_put_u8(req, WSC_Start); /* Op-Code */
184 	wpabuf_put_u8(req, 0); /* Flags */
185 
186 	return req;
187 }
188 
189 
eap_wsc_build_msg(struct eap_wsc_data * data,u8 id)190 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data, u8 id)
191 {
192 	struct wpabuf *req;
193 	u8 flags;
194 	size_t send_len, plen;
195 
196 	flags = 0;
197 	send_len = wpabuf_len(data->out_buf) - data->out_used;
198 	if (2 + send_len > data->fragment_size) {
199 		send_len = data->fragment_size - 2;
200 		flags |= WSC_FLAGS_MF;
201 		if (data->out_used == 0) {
202 			flags |= WSC_FLAGS_LF;
203 			send_len -= 2;
204 		}
205 	}
206 	plen = 2 + send_len;
207 	if (flags & WSC_FLAGS_LF)
208 		plen += 2;
209 	req = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
210 			    EAP_CODE_REQUEST, id);
211 	if (req == NULL) {
212 		wpa_printf(MSG_ERROR, "EAP-WSC: Failed to allocate memory for "
213 			   "request");
214 		return NULL;
215 	}
216 
217 	wpabuf_put_u8(req, data->out_op_code); /* Op-Code */
218 	wpabuf_put_u8(req, flags); /* Flags */
219 	if (flags & WSC_FLAGS_LF)
220 		wpabuf_put_be16(req, wpabuf_len(data->out_buf));
221 
222 	wpabuf_put_data(req, wpabuf_head_u8(data->out_buf) + data->out_used,
223 			send_len);
224 	data->out_used += send_len;
225 
226 	if (data->out_used == wpabuf_len(data->out_buf)) {
227 		wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
228 			   "(message sent completely)",
229 			   (unsigned long) send_len);
230 		wpabuf_free(data->out_buf);
231 		data->out_buf = NULL;
232 		data->out_used = 0;
233 		eap_wsc_state(data, MESG);
234 	} else {
235 		wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
236 			   "(%lu more to send)", (unsigned long) send_len,
237 			   (unsigned long) wpabuf_len(data->out_buf) -
238 			   data->out_used);
239 		eap_wsc_state(data, WAIT_FRAG_ACK);
240 	}
241 
242 	return req;
243 }
244 
245 
eap_wsc_buildReq(struct eap_sm * sm,void * priv,u8 id)246 static struct wpabuf * eap_wsc_buildReq(struct eap_sm *sm, void *priv, u8 id)
247 {
248 	struct eap_wsc_data *data = priv;
249 
250 	switch (data->state) {
251 	case START:
252 		return eap_wsc_build_start(sm, data, id);
253 	case MESG:
254 		if (data->out_buf == NULL) {
255 			data->out_buf = wps_get_msg(data->wps,
256 						    &data->out_op_code);
257 			if (data->out_buf == NULL) {
258 				wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to "
259 					   "receive message from WPS");
260 				return NULL;
261 			}
262 			data->out_used = 0;
263 		}
264 		/* fall through */
265 	case WAIT_FRAG_ACK:
266 		return eap_wsc_build_msg(data, id);
267 	case FRAG_ACK:
268 		return eap_wsc_build_frag_ack(id, EAP_CODE_REQUEST);
269 	default:
270 		wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected state %d in "
271 			   "buildReq", data->state);
272 		return NULL;
273 	}
274 }
275 
276 
eap_wsc_check(struct eap_sm * sm,void * priv,struct wpabuf * respData)277 static bool eap_wsc_check(struct eap_sm *sm, void *priv,
278 			  struct wpabuf *respData)
279 {
280 	const u8 *pos;
281 	size_t len;
282 
283 	pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
284 			       respData, &len);
285 	if (pos == NULL || len < 2) {
286 		wpa_printf(MSG_INFO, "EAP-WSC: Invalid frame");
287 		return true;
288 	}
289 
290 	return false;
291 }
292 
293 
eap_wsc_process_cont(struct eap_wsc_data * data,const u8 * buf,size_t len,u8 op_code)294 static int eap_wsc_process_cont(struct eap_wsc_data *data,
295 				const u8 *buf, size_t len, u8 op_code)
296 {
297 	/* Process continuation of a pending message */
298 	if (op_code != data->in_op_code) {
299 		wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
300 			   "fragment (expected %d)",
301 			   op_code, data->in_op_code);
302 		eap_wsc_state(data, FAIL);
303 		return -1;
304 	}
305 
306 	if (len > wpabuf_tailroom(data->in_buf)) {
307 		wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
308 		eap_wsc_state(data, FAIL);
309 		return -1;
310 	}
311 
312 	wpabuf_put_data(data->in_buf, buf, len);
313 	wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting for %lu "
314 		   "bytes more", (unsigned long) len,
315 		   (unsigned long) wpabuf_tailroom(data->in_buf));
316 
317 	return 0;
318 }
319 
320 
eap_wsc_process_fragment(struct eap_wsc_data * data,u8 flags,u8 op_code,u16 message_length,const u8 * buf,size_t len)321 static int eap_wsc_process_fragment(struct eap_wsc_data *data,
322 				    u8 flags, u8 op_code, u16 message_length,
323 				    const u8 *buf, size_t len)
324 {
325 	/* Process a fragment that is not the last one of the message */
326 	if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
327 		wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length "
328 			   "field in a fragmented packet");
329 		return -1;
330 	}
331 
332 	if (data->in_buf == NULL) {
333 		/* First fragment of the message */
334 		data->in_buf = wpabuf_alloc(message_length);
335 		if (data->in_buf == NULL) {
336 			wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
337 				   "message");
338 			return -1;
339 		}
340 		data->in_op_code = op_code;
341 		wpabuf_put_data(data->in_buf, buf, len);
342 		wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in "
343 			   "first fragment, waiting for %lu bytes more",
344 			   (unsigned long) len,
345 			   (unsigned long) wpabuf_tailroom(data->in_buf));
346 	}
347 
348 	return 0;
349 }
350 
351 
eap_wsc_process(struct eap_sm * sm,void * priv,struct wpabuf * respData)352 static void eap_wsc_process(struct eap_sm *sm, void *priv,
353 			    struct wpabuf *respData)
354 {
355 	struct eap_wsc_data *data = priv;
356 	const u8 *start, *pos, *end;
357 	size_t len;
358 	u8 op_code, flags;
359 	u16 message_length = 0;
360 	enum wps_process_res res;
361 	struct wpabuf tmpbuf;
362 
363 	eloop_cancel_timeout(eap_wsc_ext_reg_timeout, sm, data);
364 	if (data->ext_reg_timeout) {
365 		eap_wsc_state(data, FAIL);
366 		return;
367 	}
368 
369 	pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
370 			       respData, &len);
371 	if (pos == NULL || len < 2)
372 		return; /* Should not happen; message already verified */
373 
374 	start = pos;
375 	end = start + len;
376 
377 	op_code = *pos++;
378 	flags = *pos++;
379 	if (flags & WSC_FLAGS_LF) {
380 		if (end - pos < 2) {
381 			wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
382 			return;
383 		}
384 		message_length = WPA_GET_BE16(pos);
385 		pos += 2;
386 
387 		if (message_length < end - pos || message_length > 50000) {
388 			wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
389 				   "Length");
390 			return;
391 		}
392 	}
393 
394 	wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
395 		   "Flags 0x%x Message Length %d",
396 		   op_code, flags, message_length);
397 
398 	if (data->state == WAIT_FRAG_ACK) {
399 		if (op_code != WSC_FRAG_ACK) {
400 			wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
401 				   "in WAIT_FRAG_ACK state", op_code);
402 			eap_wsc_state(data, FAIL);
403 			return;
404 		}
405 		wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
406 		eap_wsc_state(data, MESG);
407 		return;
408 	}
409 
410 	if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
411 	    op_code != WSC_Done) {
412 		wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
413 			   op_code);
414 		eap_wsc_state(data, FAIL);
415 		return;
416 	}
417 
418 	if (data->in_buf &&
419 	    eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
420 		eap_wsc_state(data, FAIL);
421 		return;
422 	}
423 
424 	if (flags & WSC_FLAGS_MF) {
425 		if (eap_wsc_process_fragment(data, flags, op_code,
426 					     message_length, pos, end - pos) <
427 		    0)
428 			eap_wsc_state(data, FAIL);
429 		else
430 			eap_wsc_state(data, FRAG_ACK);
431 		return;
432 	}
433 
434 	if (data->in_buf == NULL) {
435 		/* Wrap unfragmented messages as wpabuf without extra copy */
436 		wpabuf_set(&tmpbuf, pos, end - pos);
437 		data->in_buf = &tmpbuf;
438 	}
439 
440 	res = wps_process_msg(data->wps, op_code, data->in_buf);
441 	switch (res) {
442 	case WPS_DONE:
443 		wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
444 			   "successfully - report EAP failure");
445 		eap_wsc_state(data, FAIL);
446 		break;
447 	case WPS_CONTINUE:
448 		eap_wsc_state(data, MESG);
449 		break;
450 	case WPS_FAILURE:
451 		wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
452 		eap_wsc_state(data, FAIL);
453 		break;
454 	case WPS_PENDING:
455 		eap_wsc_state(data, MESG);
456 		sm->method_pending = METHOD_PENDING_WAIT;
457 		eloop_cancel_timeout(eap_wsc_ext_reg_timeout, sm, data);
458 		eloop_register_timeout(5, 0, eap_wsc_ext_reg_timeout,
459 				       sm, data);
460 		break;
461 	}
462 
463 	if (data->in_buf != &tmpbuf)
464 		wpabuf_free(data->in_buf);
465 	data->in_buf = NULL;
466 }
467 
468 
eap_wsc_isDone(struct eap_sm * sm,void * priv)469 static bool eap_wsc_isDone(struct eap_sm *sm, void *priv)
470 {
471 	struct eap_wsc_data *data = priv;
472 	return data->state == FAIL;
473 }
474 
475 
eap_wsc_isSuccess(struct eap_sm * sm,void * priv)476 static bool eap_wsc_isSuccess(struct eap_sm *sm, void *priv)
477 {
478 	/* EAP-WSC will always result in EAP-Failure */
479 	return false;
480 }
481 
482 
eap_wsc_getTimeout(struct eap_sm * sm,void * priv)483 static int eap_wsc_getTimeout(struct eap_sm *sm, void *priv)
484 {
485 	/* Recommended retransmit times: retransmit timeout 5 seconds,
486 	 * per-message timeout 15 seconds, i.e., 3 tries. */
487 	sm->MaxRetrans = 2; /* total 3 attempts */
488 	return 5;
489 }
490 
491 
eap_server_wsc_register(void)492 int eap_server_wsc_register(void)
493 {
494 	struct eap_method *eap;
495 
496 	eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
497 				      EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
498 				      "WSC");
499 	if (eap == NULL)
500 		return -1;
501 
502 	eap->init = eap_wsc_init;
503 	eap->reset = eap_wsc_reset;
504 	eap->buildReq = eap_wsc_buildReq;
505 	eap->check = eap_wsc_check;
506 	eap->process = eap_wsc_process;
507 	eap->isDone = eap_wsc_isDone;
508 	eap->isSuccess = eap_wsc_isSuccess;
509 	eap->getTimeout = eap_wsc_getTimeout;
510 
511 	return eap_server_method_register(eap);
512 }
513