1 /*
2 * EAP-WSC peer for Wi-Fi Protected Setup
3 * Copyright (c) 2007-2009, 2012, 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 "uuid.h"
13 #include "eap_i.h"
14 #include "eap_common/eap_wsc_common.h"
15 #include "wps/wps.h"
16 #include "wps/wps_defs.h"
17 #ifdef HARMONY_P2P_CONNECTIVITY_PATCH
18 #include "eloop.h"
19 #endif
20
21 #define EAP_FAIL_TIMEOUT 30000
22
23 struct eap_wsc_data {
24 enum { WAIT_START, MESG, WAIT_FRAG_ACK, FAIL } state;
25 int registrar;
26 struct wpabuf *in_buf;
27 struct wpabuf *out_buf;
28 enum wsc_op_code in_op_code, out_op_code;
29 size_t out_used;
30 size_t fragment_size;
31 struct wps_data *wps;
32 struct wps_context *wps_ctx;
33 };
34
35
eap_wsc_state_txt(int state)36 static const char * eap_wsc_state_txt(int state)
37 {
38 switch (state) {
39 case WAIT_START:
40 return "WAIT_START";
41 case MESG:
42 return "MESG";
43 case WAIT_FRAG_ACK:
44 return "WAIT_FRAG_ACK";
45 case FAIL:
46 return "FAIL";
47 default:
48 return "?";
49 }
50 }
51
52
eap_wsc_state(struct eap_wsc_data * data,int state)53 static void eap_wsc_state(struct eap_wsc_data *data, int state)
54 {
55 wpa_printf(MSG_EXCESSIVE, "EAP-WSC: %s -> %s",
56 eap_wsc_state_txt(data->state),
57 eap_wsc_state_txt(state));
58 data->state = state;
59 }
60
61
eap_wsc_new_ap_settings(struct wps_credential * cred,const char * params)62 static int eap_wsc_new_ap_settings(struct wps_credential *cred,
63 const char *params)
64 {
65 const char *pos, *end;
66 size_t len;
67
68 os_memset(cred, 0, sizeof(*cred));
69
70 pos = os_strstr(params, "new_ssid=");
71 if (pos == NULL)
72 return 0;
73 pos += 9;
74 end = os_strchr(pos, ' ');
75 if (end == NULL)
76 len = os_strlen(pos);
77 else
78 len = end - 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");
82 return -1;
83 }
84 cred->ssid_len = len / 2;
85
86 pos = os_strstr(params, "new_auth=");
87 if (pos == NULL) {
88 wpa_printf(MSG_DEBUG, "EAP-WSC: Missing new_auth");
89 return -1;
90 }
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;
97 else {
98 wpa_printf(MSG_DEBUG, "EAP-WSC: Unknown new_auth");
99 return -1;
100 }
101
102 pos = os_strstr(params, "new_encr=");
103 if (pos == NULL) {
104 wpa_printf(MSG_DEBUG, "EAP-WSC: Missing new_encr");
105 return -1;
106 }
107 if (os_strncmp(pos + 9, "NONE", 4) == 0)
108 cred->encr_type = WPS_ENCR_NONE;
109 #ifdef CONFIG_TESTING_OPTIONS
110 else if (os_strncmp(pos + 9, "WEP", 3) == 0)
111 cred->encr_type = WPS_ENCR_WEP;
112 #endif /* CONFIG_TESTING_OPTIONS */
113 else if (os_strncmp(pos + 9, "TKIP", 4) == 0)
114 cred->encr_type = WPS_ENCR_TKIP;
115 else if (os_strncmp(pos + 9, "CCMP", 4) == 0)
116 cred->encr_type = WPS_ENCR_AES;
117 else {
118 wpa_printf(MSG_DEBUG, "EAP-WSC: Unknown new_encr");
119 return -1;
120 }
121
122 pos = os_strstr(params, "new_key=");
123 if (pos == NULL)
124 return 0;
125 pos += 8;
126 end = os_strchr(pos, ' ');
127 if (end == NULL)
128 len = os_strlen(pos);
129 else
130 len = end - pos;
131 if ((len & 1) || len > 2 * sizeof(cred->key) ||
132 hexstr2bin(pos, cred->key, len / 2)) {
133 wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid new_key");
134 return -1;
135 }
136 cred->key_len = len / 2;
137
138 return 1;
139 }
140
141
eap_wsc_init(struct eap_sm * sm)142 static void * eap_wsc_init(struct eap_sm *sm)
143 {
144 struct eap_wsc_data *data;
145 const u8 *identity;
146 size_t identity_len;
147 int registrar;
148 struct wps_config cfg;
149 const char *pos, *end;
150 const char *phase1;
151 struct wps_context *wps;
152 struct wps_credential new_ap_settings;
153 int res;
154 int nfc = 0;
155 u8 pkhash[WPS_OOB_PUBKEY_HASH_LEN];
156
157 wps = sm->wps;
158 if (wps == NULL) {
159 wpa_printf(MSG_ERROR, "EAP-WSC: WPS context not available");
160 return NULL;
161 }
162
163 identity = eap_get_config_identity(sm, &identity_len);
164
165 if (identity && identity_len == WSC_ID_REGISTRAR_LEN &&
166 os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0)
167 registrar = 1; /* Supplicant is Registrar */
168 else if (identity && identity_len == WSC_ID_ENROLLEE_LEN &&
169 os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0)
170 registrar = 0; /* Supplicant is Enrollee */
171 else {
172 wpa_hexdump_ascii(MSG_INFO, "EAP-WSC: Unexpected identity",
173 identity, identity_len);
174 return NULL;
175 }
176
177 data = os_zalloc(sizeof(*data));
178 if (data == NULL)
179 return NULL;
180 data->state = registrar ? MESG : WAIT_START;
181 data->registrar = registrar;
182 data->wps_ctx = wps;
183
184 os_memset(&cfg, 0, sizeof(cfg));
185 cfg.wps = wps;
186 cfg.registrar = registrar;
187
188 phase1 = eap_get_config_phase1(sm);
189 if (phase1 == NULL) {
190 wpa_printf(MSG_INFO, "EAP-WSC: phase1 configuration data not "
191 "set");
192 os_free(data);
193 return NULL;
194 }
195
196 pos = os_strstr(phase1, "pin=");
197 if (pos) {
198 pos += 4;
199 cfg.pin = (const u8 *) pos;
200 while (*pos != '\0' && *pos != ' ')
201 pos++;
202 cfg.pin_len = pos - (const char *) cfg.pin;
203 if (cfg.pin_len == 6 &&
204 os_strncmp((const char *) cfg.pin, "nfc-pw", 6) == 0) {
205 cfg.pin = NULL;
206 cfg.pin_len = 0;
207 nfc = 1;
208 }
209 } else {
210 pos = os_strstr(phase1, "pbc=1");
211 if (pos)
212 cfg.pbc = 1;
213 }
214
215 pos = os_strstr(phase1, "dev_pw_id=");
216 if (pos) {
217 u16 id = atoi(pos + 10);
218 if (id == DEV_PW_NFC_CONNECTION_HANDOVER)
219 nfc = 1;
220 if (cfg.pin || id == DEV_PW_NFC_CONNECTION_HANDOVER)
221 cfg.dev_pw_id = id;
222 }
223
224 if (cfg.pin == NULL && !cfg.pbc && !nfc) {
225 wpa_printf(MSG_INFO, "EAP-WSC: PIN or PBC not set in phase1 "
226 "configuration data");
227 os_free(data);
228 return NULL;
229 }
230
231 pos = os_strstr(phase1, " pkhash=");
232 if (pos) {
233 size_t len;
234 pos += 8;
235 end = os_strchr(pos, ' ');
236 if (end)
237 len = end - pos;
238 else
239 len = os_strlen(pos);
240 if (len != 2 * WPS_OOB_PUBKEY_HASH_LEN ||
241 hexstr2bin(pos, pkhash, WPS_OOB_PUBKEY_HASH_LEN)) {
242 wpa_printf(MSG_INFO, "EAP-WSC: Invalid pkhash");
243 os_free(data);
244 return NULL;
245 }
246 cfg.peer_pubkey_hash = pkhash;
247 }
248
249 res = eap_wsc_new_ap_settings(&new_ap_settings, phase1);
250 if (res < 0) {
251 os_free(data);
252 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to parse new AP "
253 "settings");
254 return NULL;
255 }
256 if (res == 1) {
257 wpa_printf(MSG_DEBUG, "EAP-WSC: Provide new AP settings for "
258 "WPS");
259 cfg.new_ap_settings = &new_ap_settings;
260 }
261
262 if (os_strstr(phase1, "multi_ap=1"))
263 cfg.multi_ap_backhaul_sta = 1;
264
265 data->wps = wps_init(&cfg);
266 if (data->wps == NULL) {
267 os_free(data);
268 wpa_printf(MSG_DEBUG, "EAP-WSC: wps_init failed");
269 return NULL;
270 }
271 res = eap_get_config_fragment_size(sm);
272 if (res > 0)
273 data->fragment_size = res;
274 else
275 data->fragment_size = WSC_FRAGMENT_SIZE;
276 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment size limit %u",
277 (unsigned int) data->fragment_size);
278
279 if (registrar && cfg.pin) {
280 wps_registrar_add_pin(data->wps_ctx->registrar, NULL, NULL,
281 cfg.pin, cfg.pin_len, 0);
282 }
283
284 /* Use reduced client timeout for WPS to avoid long wait */
285 if (sm->ClientTimeout > 30)
286 sm->ClientTimeout = 30;
287
288 return data;
289 }
290
291
eap_wsc_deinit(struct eap_sm * sm,void * priv)292 static void eap_wsc_deinit(struct eap_sm *sm, void *priv)
293 {
294 struct eap_wsc_data *data = priv;
295 wpabuf_free(data->in_buf);
296 wpabuf_free(data->out_buf);
297 wps_deinit(data->wps);
298 os_free(data->wps_ctx->network_key);
299 data->wps_ctx->network_key = NULL;
300 os_free(data);
301 }
302
303
eap_wsc_build_msg(struct eap_wsc_data * data,struct eap_method_ret * ret,u8 id)304 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data,
305 struct eap_method_ret *ret, u8 id)
306 {
307 struct wpabuf *resp;
308 u8 flags;
309 size_t send_len, plen;
310
311 ret->ignore = false;
312 wpa_printf(MSG_EXCESSIVE, "EAP-WSC: Generating Response");
313 ret->allowNotifications = true;
314
315 flags = 0;
316 send_len = wpabuf_len(data->out_buf) - data->out_used;
317 if (2 + send_len > data->fragment_size) {
318 send_len = data->fragment_size - 2;
319 flags |= WSC_FLAGS_MF;
320 if (data->out_used == 0) {
321 flags |= WSC_FLAGS_LF;
322 send_len -= 2;
323 }
324 }
325 plen = 2 + send_len;
326 if (flags & WSC_FLAGS_LF)
327 plen += 2;
328 resp = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
329 EAP_CODE_RESPONSE, id);
330 if (resp == NULL)
331 return NULL;
332
333 wpabuf_put_u8(resp, data->out_op_code); /* Op-Code */
334 wpabuf_put_u8(resp, flags); /* Flags */
335 if (flags & WSC_FLAGS_LF)
336 wpabuf_put_be16(resp, wpabuf_len(data->out_buf));
337
338 wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
339 send_len);
340 data->out_used += send_len;
341
342 ret->methodState = METHOD_MAY_CONT;
343 ret->decision = DECISION_FAIL;
344
345 if (data->out_used == wpabuf_len(data->out_buf)) {
346 wpa_printf(MSG_EXCESSIVE, "EAP-WSC: Sending out %lu bytes "
347 "(message sent completely)",
348 (unsigned long) send_len);
349 wpabuf_free(data->out_buf);
350 data->out_buf = NULL;
351 data->out_used = 0;
352 if ((data->state == FAIL && data->out_op_code == WSC_ACK) ||
353 data->out_op_code == WSC_NACK ||
354 data->out_op_code == WSC_Done) {
355 eap_wsc_state(data, FAIL);
356 ret->methodState = METHOD_DONE;
357 } else
358 eap_wsc_state(data, MESG);
359 } else {
360 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
361 "(%lu more to send)", (unsigned long) send_len,
362 (unsigned long) wpabuf_len(data->out_buf) -
363 data->out_used);
364 eap_wsc_state(data, WAIT_FRAG_ACK);
365 }
366
367 return resp;
368 }
369
eap_wsc_process_cont(struct eap_wsc_data * data,const u8 * buf,size_t len,u8 op_code)370 static int eap_wsc_process_cont(struct eap_wsc_data *data,
371 const u8 *buf, size_t len, u8 op_code)
372 {
373 /* Process continuation of a pending message */
374 if (op_code != data->in_op_code) {
375 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
376 "fragment (expected %d)",
377 op_code, data->in_op_code);
378 return -1;
379 }
380
381 if (len > wpabuf_tailroom(data->in_buf)) {
382 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
383 eap_wsc_state(data, FAIL);
384 return -1;
385 }
386
387 wpabuf_put_data(data->in_buf, buf, len);
388 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting "
389 "for %lu bytes more", (unsigned long) len,
390 (unsigned long) wpabuf_tailroom(data->in_buf));
391
392 return 0;
393 }
394
395
eap_wsc_process_fragment(struct eap_wsc_data * data,struct eap_method_ret * ret,u8 id,u8 flags,u8 op_code,u16 message_length,const u8 * buf,size_t len)396 static struct wpabuf * eap_wsc_process_fragment(struct eap_wsc_data *data,
397 struct eap_method_ret *ret,
398 u8 id, u8 flags, u8 op_code,
399 u16 message_length,
400 const u8 *buf, size_t len)
401 {
402 /* Process a fragment that is not the last one of the message */
403 if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
404 wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length field in a "
405 "fragmented packet");
406 ret->ignore = true;
407 return NULL;
408 }
409
410 if (data->in_buf == NULL) {
411 /* First fragment of the message */
412 data->in_buf = wpabuf_alloc(message_length);
413 if (data->in_buf == NULL) {
414 wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
415 "message");
416 ret->ignore = true;
417 return NULL;
418 }
419 data->in_op_code = op_code;
420 wpabuf_put_data(data->in_buf, buf, len);
421 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in first "
422 "fragment, waiting for %lu bytes more",
423 (unsigned long) len,
424 (unsigned long) wpabuf_tailroom(data->in_buf));
425 }
426
427 return eap_wsc_build_frag_ack(id, EAP_CODE_RESPONSE);
428 }
429
430 #ifdef HARMONY_P2P_CONNECTIVITY_PATCH
431 /* miss GO'EAP-Failure frame issue */
432 extern void wps_eap_fail_timeout(void *eloop_data, void *user_ctx);
433 extern int wps_check_msg_done(struct wps_data *wps);
434 #endif
435
eap_wsc_process(struct eap_sm * sm,void * priv,struct eap_method_ret * ret,const struct wpabuf * reqData)436 static struct wpabuf * eap_wsc_process(struct eap_sm *sm, void *priv,
437 struct eap_method_ret *ret,
438 const struct wpabuf *reqData)
439 {
440 struct eap_wsc_data *data = priv;
441 const u8 *start, *pos, *end;
442 size_t len;
443 u8 op_code, flags, id;
444 u16 message_length = 0;
445 enum wps_process_res res;
446 struct wpabuf tmpbuf;
447 struct wpabuf *r;
448
449 pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, reqData,
450 &len);
451 if (pos == NULL || len < 2) {
452 ret->ignore = true;
453 return NULL;
454 }
455
456 id = eap_get_id(reqData);
457
458 start = pos;
459 end = start + len;
460
461 op_code = *pos++;
462 flags = *pos++;
463 if (flags & WSC_FLAGS_LF) {
464 if (end - pos < 2) {
465 wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
466 ret->ignore = true;
467 return NULL;
468 }
469 message_length = WPA_GET_BE16(pos);
470 pos += 2;
471
472 if (message_length < end - pos || message_length > 50000) {
473 wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
474 "Length");
475 ret->ignore = true;
476 return NULL;
477 }
478 }
479
480 wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
481 "Flags 0x%x Message Length %d",
482 op_code, flags, message_length);
483
484 if (data->state == WAIT_FRAG_ACK) {
485 if (op_code != WSC_FRAG_ACK) {
486 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
487 "in WAIT_FRAG_ACK state", op_code);
488 ret->ignore = true;
489 return NULL;
490 }
491 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
492 eap_wsc_state(data, MESG);
493 return eap_wsc_build_msg(data, ret, id);
494 }
495
496 if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
497 op_code != WSC_Done && op_code != WSC_Start) {
498 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
499 op_code);
500 ret->ignore = true;
501 return NULL;
502 }
503
504 if (data->state == WAIT_START) {
505 if (op_code != WSC_Start) {
506 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
507 "in WAIT_START state", op_code);
508 ret->ignore = true;
509 return NULL;
510 }
511 wpa_printf(MSG_DEBUG, "EAP-WSC: Received start");
512 eap_wsc_state(data, MESG);
513 /* Start message has empty payload, skip processing */
514 goto send_msg;
515 } else if (op_code == WSC_Start) {
516 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
517 op_code);
518 ret->ignore = true;
519 return NULL;
520 }
521
522 if (data->in_buf &&
523 eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
524 ret->ignore = true;
525 return NULL;
526 }
527
528 if (flags & WSC_FLAGS_MF) {
529 return eap_wsc_process_fragment(data, ret, id, flags, op_code,
530 message_length, pos,
531 end - pos);
532 }
533
534 if (data->in_buf == NULL) {
535 /* Wrap unfragmented messages as wpabuf without extra copy */
536 wpabuf_set(&tmpbuf, pos, end - pos);
537 data->in_buf = &tmpbuf;
538 }
539
540 res = wps_process_msg(data->wps, op_code, data->in_buf);
541 switch (res) {
542 case WPS_DONE:
543 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
544 "successfully - wait for EAP failure");
545 eap_wsc_state(data, FAIL);
546 break;
547 case WPS_CONTINUE:
548 eap_wsc_state(data, MESG);
549 break;
550 case WPS_FAILURE:
551 case WPS_PENDING:
552 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
553 eap_wsc_state(data, FAIL);
554 break;
555 }
556
557 if (data->in_buf != &tmpbuf)
558 wpabuf_free(data->in_buf);
559 data->in_buf = NULL;
560
561 send_msg:
562 if (data->out_buf == NULL) {
563 data->out_buf = wps_get_msg(data->wps, &data->out_op_code);
564 if (data->out_buf == NULL) {
565 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to receive "
566 "message from WPS");
567 eap_wsc_state(data, FAIL);
568 ret->methodState = METHOD_DONE;
569 ret->decision = DECISION_FAIL;
570 return NULL;
571 }
572 data->out_used = 0;
573 }
574
575 eap_wsc_state(data, MESG);
576 r = eap_wsc_build_msg(data, ret, id);
577 if (data->state == FAIL && ret->methodState == METHOD_DONE) {
578 /* Use reduced client timeout for WPS to avoid long wait */
579 if (sm->ClientTimeout > 2)
580 sm->ClientTimeout = 2;
581 }
582 #ifdef HARMONY_P2P_CONNECTIVITY_PATCH
583 /* miss GO'EAP-Failure frame issue */
584 if ((res == WPS_CONTINUE) && (wps_check_msg_done(data->wps))) {
585 eloop_cancel_timeout(wps_eap_fail_timeout, NULL, NULL);
586 eloop_register_timeout(0, EAP_FAIL_TIMEOUT, wps_eap_fail_timeout, NULL, NULL);
587 wpa_printf(MSG_DEBUG, "EAPOL: register eap_fail_timeout sm=%p\n", sm);
588 }
589 #endif
590 return r;
591 }
592
593
eap_peer_wsc_register(void)594 int eap_peer_wsc_register(void)
595 {
596 struct eap_method *eap;
597
598 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
599 EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
600 "WSC");
601 if (eap == NULL)
602 return -1;
603
604 eap->init = eap_wsc_init;
605 eap->deinit = eap_wsc_deinit;
606 eap->process = eap_wsc_process;
607
608 return eap_peer_method_register(eap);
609 }
610