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