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 pos = os_strstr(phase1, "multi_ap=");
263 if (pos) {
264 u16 id = atoi(pos + 9);
265
266 if (id != 0) {
267 cfg.multi_ap_backhaul_sta = 1;
268 cfg.multi_ap_profile = id;
269 } else {
270 wpa_printf(MSG_DEBUG,
271 "EAP-WSC: Invalid multi_ap setting");
272 }
273 }
274
275 data->wps = wps_init(&cfg);
276 if (data->wps == NULL) {
277 os_free(data);
278 wpa_printf(MSG_DEBUG, "EAP-WSC: wps_init failed");
279 return NULL;
280 }
281 res = eap_get_config_fragment_size(sm);
282 if (res > 0)
283 data->fragment_size = res;
284 else
285 data->fragment_size = WSC_FRAGMENT_SIZE;
286 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment size limit %u",
287 (unsigned int) data->fragment_size);
288
289 if (registrar && cfg.pin) {
290 wps_registrar_add_pin(data->wps_ctx->registrar, NULL, NULL,
291 cfg.pin, cfg.pin_len, 0);
292 }
293
294 /* Use reduced client timeout for WPS to avoid long wait */
295 if (sm->ClientTimeout > 30)
296 sm->ClientTimeout = 30;
297
298 return data;
299 }
300
301
eap_wsc_deinit(struct eap_sm * sm,void * priv)302 static void eap_wsc_deinit(struct eap_sm *sm, void *priv)
303 {
304 struct eap_wsc_data *data = priv;
305 wpabuf_free(data->in_buf);
306 wpabuf_free(data->out_buf);
307 wps_deinit(data->wps);
308 os_free(data->wps_ctx->network_key);
309 data->wps_ctx->network_key = NULL;
310 os_free(data);
311 }
312
313
eap_wsc_build_msg(struct eap_wsc_data * data,struct eap_method_ret * ret,u8 id)314 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data,
315 struct eap_method_ret *ret, u8 id)
316 {
317 struct wpabuf *resp;
318 u8 flags;
319 size_t send_len, plen;
320
321 ret->ignore = false;
322 wpa_printf(MSG_EXCESSIVE, "EAP-WSC: Generating Response");
323 ret->allowNotifications = true;
324
325 flags = 0;
326 send_len = wpabuf_len(data->out_buf) - data->out_used;
327 if (2 + send_len > data->fragment_size) {
328 send_len = data->fragment_size - 2;
329 flags |= WSC_FLAGS_MF;
330 if (data->out_used == 0) {
331 flags |= WSC_FLAGS_LF;
332 send_len -= 2;
333 }
334 }
335 plen = 2 + send_len;
336 if (flags & WSC_FLAGS_LF)
337 plen += 2;
338 resp = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
339 EAP_CODE_RESPONSE, id);
340 if (resp == NULL)
341 return NULL;
342
343 wpabuf_put_u8(resp, data->out_op_code); /* Op-Code */
344 wpabuf_put_u8(resp, flags); /* Flags */
345 if (flags & WSC_FLAGS_LF)
346 wpabuf_put_be16(resp, wpabuf_len(data->out_buf));
347
348 wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
349 send_len);
350 data->out_used += send_len;
351
352 ret->methodState = METHOD_MAY_CONT;
353 ret->decision = DECISION_FAIL;
354
355 if (data->out_used == wpabuf_len(data->out_buf)) {
356 wpa_printf(MSG_EXCESSIVE, "EAP-WSC: Sending out %lu bytes "
357 "(message sent completely)",
358 (unsigned long) send_len);
359 wpabuf_free(data->out_buf);
360 data->out_buf = NULL;
361 data->out_used = 0;
362 if ((data->state == FAIL && data->out_op_code == WSC_ACK) ||
363 data->out_op_code == WSC_NACK ||
364 data->out_op_code == WSC_Done) {
365 eap_wsc_state(data, FAIL);
366 ret->methodState = METHOD_DONE;
367 } else
368 eap_wsc_state(data, MESG);
369 } else {
370 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
371 "(%lu more to send)", (unsigned long) send_len,
372 (unsigned long) wpabuf_len(data->out_buf) -
373 data->out_used);
374 eap_wsc_state(data, WAIT_FRAG_ACK);
375 }
376
377 return resp;
378 }
379
380
eap_wsc_process_cont(struct eap_wsc_data * data,const u8 * buf,size_t len,u8 op_code)381 static int eap_wsc_process_cont(struct eap_wsc_data *data,
382 const u8 *buf, size_t len, u8 op_code)
383 {
384 /* Process continuation of a pending message */
385 if (op_code != data->in_op_code) {
386 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
387 "fragment (expected %d)",
388 op_code, data->in_op_code);
389 return -1;
390 }
391
392 if (len > wpabuf_tailroom(data->in_buf)) {
393 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
394 eap_wsc_state(data, FAIL);
395 return -1;
396 }
397
398 wpabuf_put_data(data->in_buf, buf, len);
399 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting "
400 "for %lu bytes more", (unsigned long) len,
401 (unsigned long) wpabuf_tailroom(data->in_buf));
402
403 return 0;
404 }
405
406
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)407 static struct wpabuf * eap_wsc_process_fragment(struct eap_wsc_data *data,
408 struct eap_method_ret *ret,
409 u8 id, u8 flags, u8 op_code,
410 u16 message_length,
411 const u8 *buf, size_t len)
412 {
413 /* Process a fragment that is not the last one of the message */
414 if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
415 wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length field in a "
416 "fragmented packet");
417 ret->ignore = true;
418 return NULL;
419 }
420
421 if (data->in_buf == NULL) {
422 /* First fragment of the message */
423 data->in_buf = wpabuf_alloc(message_length);
424 if (data->in_buf == NULL) {
425 wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
426 "message");
427 ret->ignore = true;
428 return NULL;
429 }
430 data->in_op_code = op_code;
431 wpabuf_put_data(data->in_buf, buf, len);
432 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in first "
433 "fragment, waiting for %lu bytes more",
434 (unsigned long) len,
435 (unsigned long) wpabuf_tailroom(data->in_buf));
436 }
437
438 return eap_wsc_build_frag_ack(id, EAP_CODE_RESPONSE);
439 }
440
441 #ifdef HARMONY_P2P_CONNECTIVITY_PATCH
442 /* miss GO'EAP-Failure frame issue */
443 extern void wps_eap_fail_timeout(void *eloop_data, void *user_ctx);
444 extern int wps_check_msg_done(struct wps_data *wps);
445 #endif
446
eap_wsc_process(struct eap_sm * sm,void * priv,struct eap_method_ret * ret,const struct wpabuf * reqData)447 static struct wpabuf * eap_wsc_process(struct eap_sm *sm, void *priv,
448 struct eap_method_ret *ret,
449 const struct wpabuf *reqData)
450 {
451 struct eap_wsc_data *data = priv;
452 const u8 *start, *pos, *end;
453 size_t len;
454 u8 op_code, flags, id;
455 u16 message_length = 0;
456 enum wps_process_res res;
457 struct wpabuf tmpbuf;
458 struct wpabuf *r;
459
460 pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, reqData,
461 &len);
462 if (pos == NULL || len < 2) {
463 ret->ignore = true;
464 return NULL;
465 }
466
467 id = eap_get_id(reqData);
468
469 start = pos;
470 end = start + len;
471
472 op_code = *pos++;
473 flags = *pos++;
474 if (flags & WSC_FLAGS_LF) {
475 if (end - pos < 2) {
476 wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
477 ret->ignore = true;
478 return NULL;
479 }
480 message_length = WPA_GET_BE16(pos);
481 pos += 2;
482
483 if (message_length < end - pos || message_length > 50000) {
484 wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
485 "Length");
486 ret->ignore = true;
487 return NULL;
488 }
489 }
490
491 wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
492 "Flags 0x%x Message Length %d",
493 op_code, flags, message_length);
494
495 if (data->state == WAIT_FRAG_ACK) {
496 if (op_code != WSC_FRAG_ACK) {
497 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
498 "in WAIT_FRAG_ACK state", op_code);
499 ret->ignore = true;
500 return NULL;
501 }
502 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
503 eap_wsc_state(data, MESG);
504 return eap_wsc_build_msg(data, ret, id);
505 }
506
507 if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
508 op_code != WSC_Done && op_code != WSC_Start) {
509 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
510 op_code);
511 ret->ignore = true;
512 return NULL;
513 }
514
515 if (data->state == WAIT_START) {
516 if (op_code != WSC_Start) {
517 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
518 "in WAIT_START state", op_code);
519 ret->ignore = true;
520 return NULL;
521 }
522 wpa_printf(MSG_DEBUG, "EAP-WSC: Received start");
523 eap_wsc_state(data, MESG);
524 /* Start message has empty payload, skip processing */
525 goto send_msg;
526 } else if (op_code == WSC_Start) {
527 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
528 op_code);
529 ret->ignore = true;
530 return NULL;
531 }
532
533 if (data->in_buf &&
534 eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
535 ret->ignore = true;
536 return NULL;
537 }
538
539 if (flags & WSC_FLAGS_MF) {
540 return eap_wsc_process_fragment(data, ret, id, flags, op_code,
541 message_length, pos,
542 end - pos);
543 }
544
545 if (data->in_buf == NULL) {
546 /* Wrap unfragmented messages as wpabuf without extra copy */
547 wpabuf_set(&tmpbuf, pos, end - pos);
548 data->in_buf = &tmpbuf;
549 }
550
551 res = wps_process_msg(data->wps, op_code, data->in_buf);
552 switch (res) {
553 case WPS_DONE:
554 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
555 "successfully - wait for EAP failure");
556 eap_wsc_state(data, FAIL);
557 break;
558 case WPS_CONTINUE:
559 eap_wsc_state(data, MESG);
560 break;
561 case WPS_FAILURE:
562 case WPS_PENDING:
563 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
564 eap_wsc_state(data, FAIL);
565 break;
566 }
567
568 if (data->in_buf != &tmpbuf)
569 wpabuf_free(data->in_buf);
570 data->in_buf = NULL;
571
572 send_msg:
573 if (data->out_buf == NULL) {
574 data->out_buf = wps_get_msg(data->wps, &data->out_op_code);
575 if (data->out_buf == NULL) {
576 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to receive "
577 "message from WPS");
578 eap_wsc_state(data, FAIL);
579 ret->methodState = METHOD_DONE;
580 ret->decision = DECISION_FAIL;
581 return NULL;
582 }
583 data->out_used = 0;
584 }
585
586 eap_wsc_state(data, MESG);
587 r = eap_wsc_build_msg(data, ret, id);
588 if (data->state == FAIL && ret->methodState == METHOD_DONE) {
589 /* Use reduced client timeout for WPS to avoid long wait */
590 if (sm->ClientTimeout > 2)
591 sm->ClientTimeout = 2;
592 }
593 #ifdef HARMONY_P2P_CONNECTIVITY_PATCH
594 /* miss GO'EAP-Failure frame issue */
595 if ((res == WPS_CONTINUE) && (wps_check_msg_done(data->wps))) {
596 eloop_cancel_timeout(wps_eap_fail_timeout, NULL, NULL);
597 eloop_register_timeout(0, EAP_FAIL_TIMEOUT, wps_eap_fail_timeout, NULL, NULL);
598 wpa_printf(MSG_DEBUG, "EAPOL: register eap_fail_timeout sm=%p\n", sm);
599 }
600 #endif
601 return r;
602 }
603
604
eap_peer_wsc_register(void)605 int eap_peer_wsc_register(void)
606 {
607 struct eap_method *eap;
608
609 eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
610 EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
611 "WSC");
612 if (eap == NULL)
613 return -1;
614
615 eap->init = eap_wsc_init;
616 eap->deinit = eap_wsc_deinit;
617 eap->process = eap_wsc_process;
618
619 return eap_peer_method_register(eap);
620 }
621