Added Doxygen documentation for WPS code
[libeap.git] / src / eap_peer / eap_wsc.c
1 /*
2  * EAP-WSC peer for Wi-Fi Protected Setup
3  * Copyright (c) 2007-2008, Jouni Malinen <j@w1.fi>
4  *
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.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "uuid.h"
19 #include "eap_i.h"
20 #include "eap_common/eap_wsc_common.h"
21 #include "wps/wps.h"
22 #include "wps/wps_defs.h"
23
24
25 struct eap_wsc_data {
26         enum { WAIT_START, MESG, FRAG_ACK, WAIT_FRAG_ACK, DONE, FAIL } state;
27         int registrar;
28         struct wpabuf *in_buf;
29         struct wpabuf *out_buf;
30         enum wsc_op_code in_op_code, out_op_code;
31         size_t out_used;
32         size_t fragment_size;
33         struct wps_data *wps;
34         struct wps_context *wps_ctx;
35 };
36
37
38 static const char * eap_wsc_state_txt(int state)
39 {
40         switch (state) {
41         case WAIT_START:
42                 return "WAIT_START";
43         case MESG:
44                 return "MESG";
45         case FRAG_ACK:
46                 return "FRAG_ACK";
47         case WAIT_FRAG_ACK:
48                 return "WAIT_FRAG_ACK";
49         case DONE:
50                 return "DONE";
51         case FAIL:
52                 return "FAIL";
53         default:
54                 return "?";
55         }
56 }
57
58
59 static void eap_wsc_state(struct eap_wsc_data *data, int state)
60 {
61         wpa_printf(MSG_DEBUG, "EAP-WSC: %s -> %s",
62                    eap_wsc_state_txt(data->state),
63                    eap_wsc_state_txt(state));
64         data->state = state;
65 }
66
67
68 static int eap_wsc_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
69                               size_t psk_len)
70 {
71         wpa_printf(MSG_DEBUG, "EAP-WSC: Received new WPA/WPA2-PSK from WPS for"
72                    " STA " MACSTR, MAC2STR(mac_addr));
73         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
74
75         /* TODO */
76
77         return 0;
78 }
79
80
81 static void eap_wsc_pin_needed_cb(void *ctx, const u8 *uuid_e,
82                                   const struct wps_device_data *dev)
83 {
84         char uuid[40], txt[400];
85         int len;
86         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
87                 return;
88         wpa_printf(MSG_DEBUG, "EAP-WSC: PIN needed for E-UUID %s", uuid);
89         len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED "
90                           "%s " MACSTR " [%s|%s|%s|%s|%s|%d-%08X-%d]",
91                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
92                           dev->manufacturer, dev->model_name,
93                           dev->model_number, dev->serial_number,
94                           dev->categ, dev->oui, dev->sub_categ);
95         if (len > 0 && len < (int) sizeof(txt))
96                 wpa_printf(MSG_INFO, "%s", txt);
97 }
98
99
100 static void * eap_wsc_init(struct eap_sm *sm)
101 {
102         struct eap_wsc_data *data;
103         const u8 *identity;
104         size_t identity_len;
105         int registrar;
106         struct wps_config cfg;
107         const char *pos;
108         const char *phase1;
109         struct wps_context *wps;
110
111         wps = sm->wps;
112         if (wps == NULL) {
113                 wpa_printf(MSG_ERROR, "EAP-WSC: WPS context not available");
114                 return NULL;
115         }
116
117         identity = eap_get_config_identity(sm, &identity_len);
118
119         if (identity && identity_len == WSC_ID_REGISTRAR_LEN &&
120             os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0)
121                 registrar = 1; /* Supplicant is Registrar */
122         else if (identity && identity_len == WSC_ID_ENROLLEE_LEN &&
123             os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0)
124                 registrar = 0; /* Supplicant is Enrollee */
125         else {
126                 wpa_hexdump_ascii(MSG_INFO, "EAP-WSC: Unexpected identity",
127                                   identity, identity_len);
128                 return NULL;
129         }
130
131         data = os_zalloc(sizeof(*data));
132         if (data == NULL)
133                 return NULL;
134         data->state = registrar ? MESG : WAIT_START;
135         data->registrar = registrar;
136         data->wps_ctx = wps;
137
138         if (registrar) {
139                 struct wps_registrar_config rcfg;
140
141                 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
142                 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
143
144                 os_memset(&rcfg, 0, sizeof(rcfg));
145                 rcfg.new_psk_cb = eap_wsc_new_psk_cb;
146                 rcfg.pin_needed_cb = eap_wsc_pin_needed_cb;
147                 rcfg.cb_ctx = data;
148
149                 wps->registrar = wps_registrar_init(wps, &rcfg);
150                 if (wps->registrar == NULL) {
151                         wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to initialize "
152                                    "WPS Registrar");
153                         os_free(wps->network_key);
154                         os_free(wps);
155                         os_free(data);
156                         return NULL;
157                 }
158
159         }
160
161         os_memset(&cfg, 0, sizeof(cfg));
162         cfg.authenticator = 0;
163         cfg.wps = wps;
164         cfg.registrar = registrar ? data->wps_ctx->registrar : NULL;
165
166         phase1 = eap_get_config_phase1(sm);
167         if (phase1 == NULL) {
168                 wpa_printf(MSG_INFO, "EAP-WSC: phase1 configuration data not "
169                            "set");
170                 os_free(data);
171                 return NULL;
172         }
173
174         pos = os_strstr(phase1, "pin=");
175         if (pos) {
176                 pos += 4;
177                 cfg.pin = (const u8 *) pos;
178                 while (*pos != '\0' && *pos != ' ')
179                         pos++;
180                 cfg.pin_len = pos - (const char *) cfg.pin;
181         } else {
182                 pos = os_strstr(phase1, "pbc=1");
183                 if (pos)
184                         cfg.pbc = 1;
185         }
186
187         if (cfg.pin == NULL && !cfg.pbc) {
188                 wpa_printf(MSG_INFO, "EAP-WSC: PIN or PBC not set in phase1 "
189                            "configuration data");
190                 os_free(data);
191                 return NULL;
192         }
193
194         data->wps = wps_init(&cfg);
195         if (data->wps == NULL) {
196                 os_free(data);
197                 return NULL;
198         }
199         data->fragment_size = WSC_FRAGMENT_SIZE;
200
201         if (registrar && cfg.pin) {
202                 wps_registrar_add_pin(data->wps_ctx->registrar, NULL,
203                                       cfg.pin, cfg.pin_len);
204         }
205
206         return data;
207 }
208
209
210 static void eap_wsc_deinit(struct eap_sm *sm, void *priv)
211 {
212         struct eap_wsc_data *data = priv;
213         wpabuf_free(data->in_buf);
214         wpabuf_free(data->out_buf);
215         wps_deinit(data->wps);
216         wps_registrar_deinit(data->wps_ctx->registrar);
217         data->wps_ctx->registrar = NULL;
218         os_free(data->wps_ctx->network_key);
219         data->wps_ctx->network_key = NULL;
220         os_free(data);
221 }
222
223
224 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data,
225                                          struct eap_method_ret *ret, u8 id)
226 {
227         struct wpabuf *resp;
228         u8 flags;
229         size_t send_len, plen;
230
231         ret->ignore = FALSE;
232         wpa_printf(MSG_DEBUG, "EAP-WSC: Generating Response");
233         ret->allowNotifications = TRUE;
234
235         flags = 0;
236         send_len = wpabuf_len(data->out_buf) - data->out_used;
237         if (2 + send_len > data->fragment_size) {
238                 send_len = data->fragment_size - 2;
239                 flags |= WSC_FLAGS_MF;
240                 if (data->out_used == 0) {
241                         flags |= WSC_FLAGS_LF;
242                         send_len -= 2;
243                 }
244         }
245         plen = 2 + send_len;
246         if (flags & WSC_FLAGS_LF)
247                 plen += 2;
248         resp = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
249                              EAP_CODE_RESPONSE, id);
250         if (resp == NULL)
251                 return NULL;
252
253         wpabuf_put_u8(resp, data->out_op_code); /* Op-Code */
254         wpabuf_put_u8(resp, flags); /* Flags */
255         if (flags & WSC_FLAGS_LF)
256                 wpabuf_put_be16(resp, wpabuf_len(data->out_buf));
257
258         wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
259                         send_len);
260         data->out_used += send_len;
261
262         ret->methodState = METHOD_MAY_CONT;
263         ret->decision = DECISION_FAIL;
264
265         if (data->out_used == wpabuf_len(data->out_buf)) {
266                 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
267                            "(message sent completely)",
268                            (unsigned long) send_len);
269                 wpabuf_free(data->out_buf);
270                 data->out_buf = NULL;
271                 data->out_used = 0;
272                 if ((data->state == FAIL && data->out_op_code == WSC_ACK) ||
273                     data->out_op_code == WSC_NACK ||
274                     data->out_op_code == WSC_Done) {
275                         eap_wsc_state(data, FAIL);
276                         ret->methodState = METHOD_DONE;
277                 } else
278                         eap_wsc_state(data, MESG);
279         } else {
280                 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
281                            "(%lu more to send)", (unsigned long) send_len,
282                            (unsigned long) wpabuf_len(data->out_buf) -
283                            data->out_used);
284                 eap_wsc_state(data, WAIT_FRAG_ACK);
285         }
286
287         return resp;
288 }
289
290
291 static int eap_wsc_process_cont(struct eap_wsc_data *data,
292                                 const u8 *buf, size_t len, u8 op_code)
293 {
294         /* Process continuation of a pending message */
295         if (op_code != data->in_op_code) {
296                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
297                            "fragment (expected %d)",
298                            op_code, data->in_op_code);
299                 return -1;
300         }
301
302         if (len > wpabuf_tailroom(data->in_buf)) {
303                 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
304                 eap_wsc_state(data, FAIL);
305                 return -1;
306         }
307
308         wpabuf_put_data(data->in_buf, buf, len);
309         wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting "
310                    "for %lu bytes more", (unsigned long) len,
311                    (unsigned long) wpabuf_tailroom(data->in_buf));
312
313         return 0;
314 }
315
316
317 static struct wpabuf * eap_wsc_process_fragment(struct eap_wsc_data *data,
318                                                 struct eap_method_ret *ret,
319                                                 u8 id, u8 flags, u8 op_code,
320                                                 u16 message_length,
321                                                 const u8 *buf, size_t len)
322 {
323         /* Process a fragment that is not the last one of the message */
324         if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
325                 wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length field in a "
326                            "fragmented packet");
327                 ret->ignore = TRUE;
328                 return NULL;
329         }
330
331         if (data->in_buf == NULL) {
332                 /* First fragment of the message */
333                 data->in_buf = wpabuf_alloc(message_length);
334                 if (data->in_buf == NULL) {
335                         wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
336                                    "message");
337                         ret->ignore = TRUE;
338                         return NULL;
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 first "
343                            "fragment, waiting for %lu bytes more",
344                            (unsigned long) len,
345                            (unsigned long) wpabuf_tailroom(data->in_buf));
346         }
347
348         return eap_wsc_build_frag_ack(id, EAP_CODE_RESPONSE);
349 }
350
351
352 static struct wpabuf * eap_wsc_process(struct eap_sm *sm, void *priv,
353                                        struct eap_method_ret *ret,
354                                        const struct wpabuf *reqData)
355 {
356         struct eap_wsc_data *data = priv;
357         const u8 *start, *pos, *end;
358         size_t len;
359         u8 op_code, flags, id;
360         u16 message_length = 0;
361         enum wps_process_res res;
362         struct wpabuf tmpbuf;
363
364         pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, reqData,
365                                &len);
366         if (pos == NULL || len < 2) {
367                 ret->ignore = TRUE;
368                 return NULL;
369         }
370
371         id = eap_get_id(reqData);
372
373         start = pos;
374         end = start + len;
375
376         op_code = *pos++;
377         flags = *pos++;
378         if (flags & WSC_FLAGS_LF) {
379                 if (end - pos < 2) {
380                         wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
381                         ret->ignore = TRUE;
382                         return NULL;
383                 }
384                 message_length = WPA_GET_BE16(pos);
385                 pos += 2;
386
387                 if (message_length < end - pos) {
388                         wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
389                                    "Length");
390                         ret->ignore = TRUE;
391                         return NULL;
392                 }
393         }
394
395         wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
396                    "Flags 0x%x Message Length %d",
397                    op_code, flags, message_length);
398
399         if (data->state == WAIT_FRAG_ACK) {
400                 if (op_code != WSC_FRAG_ACK) {
401                         wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
402                                    "in WAIT_FRAG_ACK state", op_code);
403                         ret->ignore = TRUE;
404                         return NULL;
405                 }
406                 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
407                 eap_wsc_state(data, MESG);
408                 return eap_wsc_build_msg(data, ret, id);
409         }
410
411         if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
412             op_code != WSC_Done && op_code != WSC_Start) {
413                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
414                            op_code);
415                 ret->ignore = TRUE;
416                 return NULL;
417         }
418
419         if (data->state == WAIT_START) {
420                 if (op_code != WSC_Start) {
421                         wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
422                                    "in WAIT_START state", op_code);
423                         ret->ignore = TRUE;
424                         return NULL;
425                 }
426                 wpa_printf(MSG_DEBUG, "EAP-WSC: Received start");
427                 eap_wsc_state(data, MESG);
428                 /* Start message has empty payload, skip processing */
429                 goto send_msg;
430         } else if (op_code == WSC_Start) {
431                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
432                            op_code);
433                 ret->ignore = TRUE;
434                 return NULL;
435         }
436
437         if (data->in_buf &&
438             eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
439                 ret->ignore = TRUE;
440                 return NULL;
441         }
442
443         if (flags & WSC_FLAGS_MF) {
444                 return eap_wsc_process_fragment(data, ret, id, flags, op_code,
445                                                 message_length, pos,
446                                                 end - pos);
447         }
448
449         if (data->in_buf == NULL) {
450                 /* Wrap unfragmented messages as wpabuf without extra copy */
451                 wpabuf_set(&tmpbuf, pos, end - pos);
452                 data->in_buf = &tmpbuf;
453         }
454
455         res = wps_process_msg(data->wps, op_code, data->in_buf);
456         switch (res) {
457         case WPS_DONE:
458                 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
459                            "successfully - wait for EAP failure");
460                 eap_wsc_state(data, FAIL);
461                 break;
462         case WPS_CONTINUE:
463                 eap_wsc_state(data, MESG);
464                 break;
465         case WPS_FAILURE:
466                 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
467                 eap_wsc_state(data, FAIL);
468                 break;
469         case WPS_PENDING:
470                 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing pending");
471                 ret->ignore = TRUE;
472                 if (data->in_buf == &tmpbuf)
473                         data->in_buf = NULL;
474                 return NULL;
475         }
476
477         if (data->in_buf != &tmpbuf)
478                 wpabuf_free(data->in_buf);
479         data->in_buf = NULL;
480
481 send_msg:
482         if (data->out_buf == NULL) {
483                 data->out_buf = wps_get_msg(data->wps, &data->out_op_code);
484                 if (data->out_buf == NULL) {
485                         wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to receive "
486                                    "message from WPS");
487                         return NULL;
488                 }
489                 data->out_used = 0;
490         }
491
492         eap_wsc_state(data, MESG);
493         return eap_wsc_build_msg(data, ret, id);
494 }
495
496
497 int eap_peer_wsc_register(void)
498 {
499         struct eap_method *eap;
500         int ret;
501
502         eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
503                                     EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
504                                     "WSC");
505         if (eap == NULL)
506                 return -1;
507
508         eap->init = eap_wsc_init;
509         eap->deinit = eap_wsc_deinit;
510         eap->process = eap_wsc_process;
511
512         ret = eap_peer_method_register(eap);
513         if (ret)
514                 eap_peer_method_free(eap);
515         return ret;
516 }