Added Doxygen documentation for WPS code
[libeap.git] / src / eap_server / eap_wsc.c
1 /*
2  * EAP-WSC server 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 "eap_i.h"
19 #include "eap_common/eap_wsc_common.h"
20 #include "wps/wps.h"
21
22
23 struct eap_wsc_data {
24         enum { START, MSG, FRAG_ACK, WAIT_FRAG_ACK, DONE, 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 };
33
34
35 static const char * eap_wsc_state_txt(int state)
36 {
37         switch (state) {
38         case START:
39                 return "START";
40         case MSG:
41                 return "MSG";
42         case FRAG_ACK:
43                 return "FRAG_ACK";
44         case WAIT_FRAG_ACK:
45                 return "WAIT_FRAG_ACK";
46         case DONE:
47                 return "DONE";
48         case FAIL:
49                 return "FAIL";
50         default:
51                 return "?";
52         }
53 }
54
55
56 static void eap_wsc_state(struct eap_wsc_data *data, int state)
57 {
58         wpa_printf(MSG_DEBUG, "EAP-WSC: %s -> %s",
59                    eap_wsc_state_txt(data->state),
60                    eap_wsc_state_txt(state));
61         data->state = state;
62 }
63
64
65 static void * eap_wsc_init(struct eap_sm *sm)
66 {
67         struct eap_wsc_data *data;
68         int registrar;
69         struct wps_config cfg;
70
71         if (sm->identity && sm->identity_len == WSC_ID_REGISTRAR_LEN &&
72             os_memcmp(sm->identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) ==
73             0)
74                 registrar = 0; /* Supplicant is Registrar */
75         else if (sm->identity && sm->identity_len == WSC_ID_ENROLLEE_LEN &&
76                  os_memcmp(sm->identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN)
77                  == 0)
78                 registrar = 1; /* Supplicant is Enrollee */
79         else {
80                 wpa_hexdump_ascii(MSG_INFO, "EAP-WSC: Unexpected identity",
81                                   sm->identity, sm->identity_len);
82                 return NULL;
83         }
84
85         data = os_zalloc(sizeof(*data));
86         if (data == NULL)
87                 return NULL;
88         data->state = registrar ? START : MSG;
89         data->registrar = registrar;
90
91         os_memset(&cfg, 0, sizeof(cfg));
92         cfg.authenticator = 1;
93         cfg.wps = sm->wps;
94         if (registrar) {
95                 if (sm->wps == NULL || sm->wps->registrar == NULL) {
96                         wpa_printf(MSG_INFO, "EAP-WSC: WPS Registrar not "
97                                    "initialized");
98                         os_free(data);
99                         return NULL;
100                 }
101                 cfg.registrar = sm->wps->registrar;
102         } else {
103                 if (sm->user == NULL || sm->user->password == NULL) {
104                         wpa_printf(MSG_INFO, "EAP-WSC: No AP PIN (password) "
105                                    "configured for Enrollee functionality");
106                         os_free(data);
107                         return NULL;
108                 }
109                 cfg.pin = sm->user->password;
110                 cfg.pin_len = sm->user->password_len;
111         }
112         cfg.assoc_wps_ie = sm->assoc_wps_ie;
113         data->wps = wps_init(&cfg);
114         if (data->wps == NULL) {
115                 os_free(data);
116                 return NULL;
117         }
118         data->fragment_size = WSC_FRAGMENT_SIZE;
119
120         return data;
121 }
122
123
124 static void eap_wsc_reset(struct eap_sm *sm, void *priv)
125 {
126         struct eap_wsc_data *data = priv;
127         wpabuf_free(data->in_buf);
128         wpabuf_free(data->out_buf);
129         wps_deinit(data->wps);
130         os_free(data);
131 }
132
133
134 static struct wpabuf * eap_wsc_build_start(struct eap_sm *sm,
135                                            struct eap_wsc_data *data, u8 id)
136 {
137         struct wpabuf *req;
138
139         req = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, 2,
140                             EAP_CODE_REQUEST, id);
141         if (req == NULL) {
142                 wpa_printf(MSG_ERROR, "EAP-WSC: Failed to allocate memory for "
143                            "request");
144                 return NULL;
145         }
146
147         wpa_printf(MSG_DEBUG, "EAP-WSC: Send WSC/Start");
148         wpabuf_put_u8(req, WSC_Start); /* Op-Code */
149         wpabuf_put_u8(req, 0); /* Flags */
150
151         return req;
152 }
153
154
155 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data, u8 id)
156 {
157         struct wpabuf *req;
158         u8 flags;
159         size_t send_len, plen;
160
161         flags = 0;
162         send_len = wpabuf_len(data->out_buf) - data->out_used;
163         if (2 + send_len > data->fragment_size) {
164                 send_len = data->fragment_size - 2;
165                 flags |= WSC_FLAGS_MF;
166                 if (data->out_used == 0) {
167                         flags |= WSC_FLAGS_LF;
168                         send_len -= 2;
169                 }
170         }
171         plen = 2 + send_len;
172         if (flags & WSC_FLAGS_LF)
173                 plen += 2;
174         req = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
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         wpabuf_put_u8(req, data->out_op_code); /* Op-Code */
183         wpabuf_put_u8(req, flags); /* Flags */
184         if (flags & WSC_FLAGS_LF)
185                 wpabuf_put_be16(req, wpabuf_len(data->out_buf));
186
187         wpabuf_put_data(req, wpabuf_head_u8(data->out_buf) + data->out_used,
188                         send_len);
189         data->out_used += send_len;
190
191         if (data->out_used == wpabuf_len(data->out_buf)) {
192                 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
193                            "(message sent completely)",
194                            (unsigned long) send_len);
195                 wpabuf_free(data->out_buf);
196                 data->out_buf = NULL;
197                 data->out_used = 0;
198                 eap_wsc_state(data, MSG);
199         } else {
200                 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
201                            "(%lu more to send)", (unsigned long) send_len,
202                            (unsigned long) wpabuf_len(data->out_buf) -
203                            data->out_used);
204                 eap_wsc_state(data, WAIT_FRAG_ACK);
205         }
206
207         return req;
208 }
209
210
211 static struct wpabuf * eap_wsc_buildReq(struct eap_sm *sm, void *priv, u8 id)
212 {
213         struct eap_wsc_data *data = priv;
214
215         switch (data->state) {
216         case START:
217                 return eap_wsc_build_start(sm, data, id);
218         case MSG:
219                 if (data->out_buf == NULL) {
220                         data->out_buf = wps_get_msg(data->wps,
221                                                     &data->out_op_code);
222                         if (data->out_buf == NULL) {
223                                 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to "
224                                            "receive message from WPS");
225                                 return NULL;
226                         }
227                         data->out_used = 0;
228                 }
229                 /* pass through */
230         case WAIT_FRAG_ACK:
231                 return eap_wsc_build_msg(data, id);
232         case FRAG_ACK:
233                 return eap_wsc_build_frag_ack(id, EAP_CODE_REQUEST);
234         default:
235                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected state %d in "
236                            "buildReq", data->state);
237                 return NULL;
238         }
239 }
240
241
242 static Boolean eap_wsc_check(struct eap_sm *sm, void *priv,
243                              struct wpabuf *respData)
244 {
245         const u8 *pos;
246         size_t len;
247
248         pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
249                                respData, &len);
250         if (pos == NULL || len < 2) {
251                 wpa_printf(MSG_INFO, "EAP-WSC: Invalid frame");
252                 return TRUE;
253         }
254
255         return FALSE;
256 }
257
258
259 static int eap_wsc_process_cont(struct eap_wsc_data *data,
260                                 const u8 *buf, size_t len, u8 op_code)
261 {
262         /* Process continuation of a pending message */
263         if (op_code != data->in_op_code) {
264                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
265                            "fragment (expected %d)",
266                            op_code, data->in_op_code);
267                 eap_wsc_state(data, FAIL);
268                 return -1;
269         }
270
271         if (len > wpabuf_tailroom(data->in_buf)) {
272                 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
273                 eap_wsc_state(data, FAIL);
274                 return -1;
275         }
276
277         wpabuf_put_data(data->in_buf, buf, len);
278         wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting for %lu "
279                    "bytes more", (unsigned long) len,
280                    (unsigned long) wpabuf_tailroom(data->in_buf));
281
282         return 0;
283 }
284
285
286 static int eap_wsc_process_fragment(struct eap_wsc_data *data,
287                                     u8 flags, u8 op_code, u16 message_length,
288                                     const u8 *buf, size_t len)
289 {
290         /* Process a fragment that is not the last one of the message */
291         if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
292                 wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length "
293                            "field in a fragmented packet");
294                 return -1;
295         }
296
297         if (data->in_buf == NULL) {
298                 /* First fragment of the message */
299                 data->in_buf = wpabuf_alloc(message_length);
300                 if (data->in_buf == NULL) {
301                         wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
302                                    "message");
303                         return -1;
304                 }
305                 data->in_op_code = op_code;
306                 wpabuf_put_data(data->in_buf, buf, len);
307                 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in "
308                            "first fragment, waiting for %lu bytes more",
309                            (unsigned long) len,
310                            (unsigned long) wpabuf_tailroom(data->in_buf));
311         }
312
313         return 0;
314 }
315
316
317 static void eap_wsc_process(struct eap_sm *sm, void *priv,
318                             struct wpabuf *respData)
319 {
320         struct eap_wsc_data *data = priv;
321         const u8 *start, *pos, *end;
322         size_t len;
323         u8 op_code, flags;
324         u16 message_length = 0;
325         enum wps_process_res res;
326         struct wpabuf tmpbuf;
327
328         pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
329                                respData, &len);
330         if (pos == NULL || len < 2)
331                 return; /* Should not happen; message already verified */
332
333         start = pos;
334         end = start + len;
335
336         op_code = *pos++;
337         flags = *pos++;
338         if (flags & WSC_FLAGS_LF) {
339                 if (end - pos < 2) {
340                         wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
341                         return;
342                 }
343                 message_length = WPA_GET_BE16(pos);
344                 pos += 2;
345
346                 if (message_length < end - pos) {
347                         wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
348                                    "Length");
349                         return;
350                 }
351         }
352
353         wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
354                    "Flags 0x%x Message Length %d",
355                    op_code, flags, message_length);
356
357         if (data->state == WAIT_FRAG_ACK) {
358                 if (op_code != WSC_FRAG_ACK) {
359                         wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
360                                    "in WAIT_FRAG_ACK state", op_code);
361                         eap_wsc_state(data, FAIL);
362                         return;
363                 }
364                 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
365                 eap_wsc_state(data, MSG);
366                 return;
367         }
368
369         if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
370             op_code != WSC_Done) {
371                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
372                            op_code);
373                 eap_wsc_state(data, FAIL);
374                 return;
375         }
376
377         if (data->in_buf &&
378             eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
379                 eap_wsc_state(data, FAIL);
380                 return;
381         }
382
383         if (flags & WSC_FLAGS_MF) {
384                 if (eap_wsc_process_fragment(data, flags, op_code,
385                                              message_length, pos, end - pos) <
386                     0)
387                         eap_wsc_state(data, FAIL);
388                 else
389                         eap_wsc_state(data, FRAG_ACK);
390                 return;
391         }
392
393         if (data->in_buf == NULL) {
394                 /* Wrap unfragmented messages as wpabuf without extra copy */
395                 wpabuf_set(&tmpbuf, pos, end - pos);
396                 data->in_buf = &tmpbuf;
397         }
398
399         res = wps_process_msg(data->wps, op_code, data->in_buf);
400         switch (res) {
401         case WPS_DONE:
402                 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
403                            "successfully - report EAP failure");
404                 eap_wsc_state(data, FAIL);
405                 break;
406         case WPS_CONTINUE:
407                 eap_wsc_state(data, MSG);
408                 break;
409         case WPS_FAILURE:
410                 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
411                 eap_wsc_state(data, FAIL);
412                 break;
413         case WPS_PENDING:
414                 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing pending");
415                 sm->method_pending = METHOD_PENDING_WAIT;
416                 break;
417         }
418
419         if (data->in_buf != &tmpbuf)
420                 wpabuf_free(data->in_buf);
421         data->in_buf = NULL;
422 }
423
424
425 static Boolean eap_wsc_isDone(struct eap_sm *sm, void *priv)
426 {
427         struct eap_wsc_data *data = priv;
428         return data->state == FAIL;
429 }
430
431
432 static Boolean eap_wsc_isSuccess(struct eap_sm *sm, void *priv)
433 {
434         /* EAP-WSC will always result in EAP-Failure */
435         return FALSE;
436 }
437
438
439 static int eap_wsc_getTimeout(struct eap_sm *sm, void *priv)
440 {
441         /* Recommended retransmit times: retransmit timeout 5 seconds,
442          * per-message timeout 15 seconds, i.e., 3 tries. */
443         sm->MaxRetrans = 2; /* total 3 attempts */
444         return 5;
445 }
446
447
448 int eap_server_wsc_register(void)
449 {
450         struct eap_method *eap;
451         int ret;
452
453         eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
454                                       EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
455                                       "WSC");
456         if (eap == NULL)
457                 return -1;
458
459         eap->init = eap_wsc_init;
460         eap->reset = eap_wsc_reset;
461         eap->buildReq = eap_wsc_buildReq;
462         eap->check = eap_wsc_check;
463         eap->process = eap_wsc_process;
464         eap->isDone = eap_wsc_isDone;
465         eap->isSuccess = eap_wsc_isSuccess;
466         eap->getTimeout = eap_wsc_getTimeout;
467
468         ret = eap_server_method_register(eap);
469         if (ret)
470                 eap_server_method_free(eap);
471         return ret;
472 }