Added preliminary Wi-Fi Protected Setup (WPS) implementation
[wpasupplicant] / src / eap_server / eap_wsc.c
1 /*
2  * EAP-WSC server for Wi-Fi Protected Setup
3  * Copyright (c) 2007, 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         u8 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         data->wps = wps_init(&cfg);
113         if (data->wps == NULL) {
114                 os_free(data);
115                 return NULL;
116         }
117         data->fragment_size = WSC_FRAGMENT_SIZE;
118
119         return data;
120 }
121
122
123 static void eap_wsc_reset(struct eap_sm *sm, void *priv)
124 {
125         struct eap_wsc_data *data = priv;
126         wpabuf_free(data->in_buf);
127         wpabuf_free(data->out_buf);
128         wps_deinit(data->wps);
129         os_free(data);
130 }
131
132
133 static struct wpabuf * eap_wsc_build_start(struct eap_sm *sm,
134                                            struct eap_wsc_data *data, u8 id)
135 {
136         struct wpabuf *req;
137
138         req = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, 2,
139                             EAP_CODE_REQUEST, id);
140         if (req == NULL) {
141                 wpa_printf(MSG_ERROR, "EAP-WSC: Failed to allocate memory for "
142                            "request");
143                 return NULL;
144         }
145
146         wpa_printf(MSG_DEBUG, "EAP-WSC: Send WSC/Start");
147         wpabuf_put_u8(req, WSC_Start); /* Op-Code */
148         wpabuf_put_u8(req, 0); /* Flags */
149
150         return req;
151 }
152
153
154 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data, u8 id)
155 {
156         struct wpabuf *req;
157         u8 flags;
158         size_t send_len, plen;
159
160         flags = 0;
161         send_len = wpabuf_len(data->out_buf) - data->out_used;
162         if (2 + send_len > data->fragment_size) {
163                 send_len = data->fragment_size - 2;
164                 flags |= WSC_FLAGS_MF;
165                 if (data->out_used == 0) {
166                         flags |= WSC_FLAGS_LF;
167                         send_len -= 2;
168                 }
169         }
170         plen = 2 + send_len;
171         if (flags & WSC_FLAGS_LF)
172                 plen += 2;
173         req = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
174                             EAP_CODE_REQUEST, id);
175         if (req == NULL) {
176                 wpa_printf(MSG_ERROR, "EAP-WSC: Failed to allocate memory for "
177                            "request");
178                 return NULL;
179         }
180
181         wpabuf_put_u8(req, data->out_op_code); /* Op-Code */
182         wpabuf_put_u8(req, flags); /* Flags */
183         if (flags & WSC_FLAGS_LF)
184                 wpabuf_put_be16(req, wpabuf_len(data->out_buf));
185
186         wpabuf_put_data(req, wpabuf_head_u8(data->out_buf) + data->out_used,
187                         send_len);
188         data->out_used += send_len;
189
190         if (data->out_used == wpabuf_len(data->out_buf)) {
191                 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
192                            "(message sent completely)",
193                            (unsigned long) send_len);
194                 wpabuf_free(data->out_buf);
195                 data->out_buf = NULL;
196                 data->out_used = 0;
197                 eap_wsc_state(data, MSG);
198         } else {
199                 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
200                            "(%lu more to send)", (unsigned long) send_len,
201                            (unsigned long) wpabuf_len(data->out_buf) -
202                            data->out_used);
203                 eap_wsc_state(data, WAIT_FRAG_ACK);
204         }
205
206         return req;
207 }
208
209
210 static struct wpabuf * eap_wsc_buildReq(struct eap_sm *sm, void *priv, u8 id)
211 {
212         struct eap_wsc_data *data = priv;
213
214         switch (data->state) {
215         case START:
216                 return eap_wsc_build_start(sm, data, id);
217         case MSG:
218                 if (data->out_buf == NULL) {
219                         data->out_buf = wps_get_msg(data->wps,
220                                                     &data->out_op_code);
221                         if (data->out_buf == NULL) {
222                                 wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to "
223                                            "receive message from WPS");
224                                 return NULL;
225                         }
226                         data->out_used = 0;
227                 }
228                 /* pass through */
229         case WAIT_FRAG_ACK:
230                 return eap_wsc_build_msg(data, id);
231         case FRAG_ACK:
232                 return eap_wsc_build_frag_ack(id, EAP_CODE_REQUEST);
233         default:
234                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected state %d in "
235                            "buildReq", data->state);
236                 return NULL;
237         }
238 }
239
240
241 static Boolean eap_wsc_check(struct eap_sm *sm, void *priv,
242                              struct wpabuf *respData)
243 {
244         const u8 *pos;
245         size_t len;
246
247         pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
248                                respData, &len);
249         if (pos == NULL || len < 2) {
250                 wpa_printf(MSG_INFO, "EAP-WSC: Invalid frame");
251                 return TRUE;
252         }
253
254         return FALSE;
255 }
256
257
258 static int eap_wsc_process_cont(struct eap_wsc_data *data,
259                                 const u8 *buf, size_t len, u8 op_code)
260 {
261         /* Process continuation of a pending message */
262         if (op_code != data->in_op_code) {
263                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
264                            "fragment (expected %d)",
265                            op_code, data->in_op_code);
266                 eap_wsc_state(data, FAIL);
267                 return -1;
268         }
269
270         if (len > wpabuf_tailroom(data->in_buf)) {
271                 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
272                 eap_wsc_state(data, FAIL);
273                 return -1;
274         }
275
276         wpabuf_put_data(data->in_buf, buf, len);
277         wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting for %lu "
278                    "bytes more", (unsigned long) len,
279                    (unsigned long) wpabuf_tailroom(data->in_buf));
280
281         return 0;
282 }
283
284
285 static int eap_wsc_process_fragment(struct eap_wsc_data *data,
286                                     u8 flags, u8 op_code, u16 message_length,
287                                     const u8 *buf, size_t len)
288 {
289         /* Process a fragment that is not the last one of the message */
290         if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
291                 wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length "
292                            "field in a fragmented packet");
293                 return -1;
294         }
295
296         if (data->in_buf == NULL) {
297                 /* First fragment of the message */
298                 data->in_buf = wpabuf_alloc(message_length);
299                 if (data->in_buf == NULL) {
300                         wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
301                                    "message");
302                         return -1;
303                 }
304                 data->in_op_code = op_code;
305                 wpabuf_put_data(data->in_buf, buf, len);
306                 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in "
307                            "first fragment, waiting for %lu bytes more",
308                            (unsigned long) len,
309                            (unsigned long) wpabuf_tailroom(data->in_buf));
310         }
311
312         return 0;
313 }
314
315
316 static void eap_wsc_process(struct eap_sm *sm, void *priv,
317                             struct wpabuf *respData)
318 {
319         struct eap_wsc_data *data = priv;
320         const u8 *start, *pos, *end;
321         size_t len;
322         u8 op_code, flags;
323         u16 message_length = 0;
324         enum wps_process_res res;
325         struct wpabuf tmpbuf;
326
327         pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
328                                respData, &len);
329         if (pos == NULL || len < 2)
330                 return; /* Should not happen; message already verified */
331
332         start = pos;
333         end = start + len;
334
335         op_code = *pos++;
336         flags = *pos++;
337         if (flags & WSC_FLAGS_LF) {
338                 if (end - pos < 2) {
339                         wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
340                         return;
341                 }
342                 message_length = WPA_GET_BE16(pos);
343                 pos += 2;
344
345                 if (message_length < end - pos) {
346                         wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
347                                    "Length");
348                         return;
349                 }
350         }
351
352         wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
353                    "Flags 0x%x Message Length %d",
354                    op_code, flags, message_length);
355
356         if (data->state == WAIT_FRAG_ACK) {
357                 if (op_code != WSC_FRAG_ACK) {
358                         wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
359                                    "in WAIT_FRAG_ACK state", op_code);
360                         eap_wsc_state(data, FAIL);
361                         return;
362                 }
363                 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
364                 eap_wsc_state(data, MSG);
365                 return;
366         }
367
368         if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
369             op_code != WSC_Done) {
370                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
371                            op_code);
372                 eap_wsc_state(data, FAIL);
373                 return;
374         }
375
376         if (data->in_buf &&
377             eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
378                 eap_wsc_state(data, FAIL);
379                 return;
380         }
381
382         if (flags & WSC_FLAGS_MF) {
383                 if (eap_wsc_process_fragment(data, flags, op_code,
384                                              message_length, pos, end - pos) <
385                     0)
386                         eap_wsc_state(data, FAIL);
387                 else
388                         eap_wsc_state(data, FRAG_ACK);
389                 return;
390         }
391
392         if (data->in_buf == NULL) {
393                 /* Wrap unfragmented messages as wpabuf without extra copy */
394                 wpabuf_set(&tmpbuf, pos, end - pos);
395                 data->in_buf = &tmpbuf;
396         }
397
398         res = wps_process_msg(data->wps, op_code, data->in_buf);
399         switch (res) {
400         case WPS_DONE:
401                 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
402                            "successfully - report EAP failure");
403                 eap_wsc_state(data, FAIL);
404                 break;
405         case WPS_CONTINUE:
406                 eap_wsc_state(data, MSG);
407                 break;
408         case WPS_FAILURE:
409                 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
410                 eap_wsc_state(data, FAIL);
411                 break;
412         case WPS_PENDING:
413                 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing pending");
414                 sm->method_pending = METHOD_PENDING_WAIT;
415                 break;
416         }
417
418         if (data->in_buf != &tmpbuf)
419                 wpabuf_free(data->in_buf);
420         data->in_buf = NULL;
421 }
422
423
424 static Boolean eap_wsc_isDone(struct eap_sm *sm, void *priv)
425 {
426         struct eap_wsc_data *data = priv;
427         return data->state == FAIL;
428 }
429
430
431 static Boolean eap_wsc_isSuccess(struct eap_sm *sm, void *priv)
432 {
433         /* EAP-WSC will always result in EAP-Failure */
434         return FALSE;
435 }
436
437
438 int eap_server_wsc_register(void)
439 {
440         struct eap_method *eap;
441         int ret;
442
443         eap = eap_server_method_alloc(EAP_SERVER_METHOD_INTERFACE_VERSION,
444                                       EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
445                                       "WSC");
446         if (eap == NULL)
447                 return -1;
448
449         eap->init = eap_wsc_init;
450         eap->reset = eap_wsc_reset;
451         eap->buildReq = eap_wsc_buildReq;
452         eap->check = eap_wsc_check;
453         eap->process = eap_wsc_process;
454         eap->isDone = eap_wsc_isDone;
455         eap->isSuccess = eap_wsc_isSuccess;
456
457         ret = eap_server_method_register(eap);
458         if (ret)
459                 eap_server_method_free(eap);
460         return ret;
461 }