Removed duplicated authenticator yes/no from wps_config and wps_data
[mech_eap.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.wps = wps;
163         cfg.registrar = registrar ? data->wps_ctx->registrar : NULL;
164
165         phase1 = eap_get_config_phase1(sm);
166         if (phase1 == NULL) {
167                 wpa_printf(MSG_INFO, "EAP-WSC: phase1 configuration data not "
168                            "set");
169                 os_free(data);
170                 return NULL;
171         }
172
173         pos = os_strstr(phase1, "pin=");
174         if (pos) {
175                 pos += 4;
176                 cfg.pin = (const u8 *) pos;
177                 while (*pos != '\0' && *pos != ' ')
178                         pos++;
179                 cfg.pin_len = pos - (const char *) cfg.pin;
180         } else {
181                 pos = os_strstr(phase1, "pbc=1");
182                 if (pos)
183                         cfg.pbc = 1;
184         }
185
186         if (cfg.pin == NULL && !cfg.pbc) {
187                 wpa_printf(MSG_INFO, "EAP-WSC: PIN or PBC not set in phase1 "
188                            "configuration data");
189                 os_free(data);
190                 return NULL;
191         }
192
193         data->wps = wps_init(&cfg);
194         if (data->wps == NULL) {
195                 os_free(data);
196                 return NULL;
197         }
198         data->fragment_size = WSC_FRAGMENT_SIZE;
199
200         if (registrar && cfg.pin) {
201                 wps_registrar_add_pin(data->wps_ctx->registrar, NULL,
202                                       cfg.pin, cfg.pin_len);
203         }
204
205         return data;
206 }
207
208
209 static void eap_wsc_deinit(struct eap_sm *sm, void *priv)
210 {
211         struct eap_wsc_data *data = priv;
212         wpabuf_free(data->in_buf);
213         wpabuf_free(data->out_buf);
214         wps_deinit(data->wps);
215         wps_registrar_deinit(data->wps_ctx->registrar);
216         data->wps_ctx->registrar = NULL;
217         os_free(data->wps_ctx->network_key);
218         data->wps_ctx->network_key = NULL;
219         os_free(data);
220 }
221
222
223 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data,
224                                          struct eap_method_ret *ret, u8 id)
225 {
226         struct wpabuf *resp;
227         u8 flags;
228         size_t send_len, plen;
229
230         ret->ignore = FALSE;
231         wpa_printf(MSG_DEBUG, "EAP-WSC: Generating Response");
232         ret->allowNotifications = TRUE;
233
234         flags = 0;
235         send_len = wpabuf_len(data->out_buf) - data->out_used;
236         if (2 + send_len > data->fragment_size) {
237                 send_len = data->fragment_size - 2;
238                 flags |= WSC_FLAGS_MF;
239                 if (data->out_used == 0) {
240                         flags |= WSC_FLAGS_LF;
241                         send_len -= 2;
242                 }
243         }
244         plen = 2 + send_len;
245         if (flags & WSC_FLAGS_LF)
246                 plen += 2;
247         resp = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
248                              EAP_CODE_RESPONSE, id);
249         if (resp == NULL)
250                 return NULL;
251
252         wpabuf_put_u8(resp, data->out_op_code); /* Op-Code */
253         wpabuf_put_u8(resp, flags); /* Flags */
254         if (flags & WSC_FLAGS_LF)
255                 wpabuf_put_be16(resp, wpabuf_len(data->out_buf));
256
257         wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
258                         send_len);
259         data->out_used += send_len;
260
261         ret->methodState = METHOD_MAY_CONT;
262         ret->decision = DECISION_FAIL;
263
264         if (data->out_used == wpabuf_len(data->out_buf)) {
265                 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
266                            "(message sent completely)",
267                            (unsigned long) send_len);
268                 wpabuf_free(data->out_buf);
269                 data->out_buf = NULL;
270                 data->out_used = 0;
271                 if ((data->state == FAIL && data->out_op_code == WSC_ACK) ||
272                     data->out_op_code == WSC_NACK ||
273                     data->out_op_code == WSC_Done) {
274                         eap_wsc_state(data, FAIL);
275                         ret->methodState = METHOD_DONE;
276                 } else
277                         eap_wsc_state(data, MESG);
278         } else {
279                 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
280                            "(%lu more to send)", (unsigned long) send_len,
281                            (unsigned long) wpabuf_len(data->out_buf) -
282                            data->out_used);
283                 eap_wsc_state(data, WAIT_FRAG_ACK);
284         }
285
286         return resp;
287 }
288
289
290 static int eap_wsc_process_cont(struct eap_wsc_data *data,
291                                 const u8 *buf, size_t len, u8 op_code)
292 {
293         /* Process continuation of a pending message */
294         if (op_code != data->in_op_code) {
295                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
296                            "fragment (expected %d)",
297                            op_code, data->in_op_code);
298                 return -1;
299         }
300
301         if (len > wpabuf_tailroom(data->in_buf)) {
302                 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
303                 eap_wsc_state(data, FAIL);
304                 return -1;
305         }
306
307         wpabuf_put_data(data->in_buf, buf, len);
308         wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting "
309                    "for %lu bytes more", (unsigned long) len,
310                    (unsigned long) wpabuf_tailroom(data->in_buf));
311
312         return 0;
313 }
314
315
316 static struct wpabuf * eap_wsc_process_fragment(struct eap_wsc_data *data,
317                                                 struct eap_method_ret *ret,
318                                                 u8 id, u8 flags, u8 op_code,
319                                                 u16 message_length,
320                                                 const u8 *buf, size_t len)
321 {
322         /* Process a fragment that is not the last one of the message */
323         if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
324                 wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length field in a "
325                            "fragmented packet");
326                 ret->ignore = TRUE;
327                 return NULL;
328         }
329
330         if (data->in_buf == NULL) {
331                 /* First fragment of the message */
332                 data->in_buf = wpabuf_alloc(message_length);
333                 if (data->in_buf == NULL) {
334                         wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
335                                    "message");
336                         ret->ignore = TRUE;
337                         return NULL;
338                 }
339                 data->in_op_code = op_code;
340                 wpabuf_put_data(data->in_buf, buf, len);
341                 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in first "
342                            "fragment, waiting for %lu bytes more",
343                            (unsigned long) len,
344                            (unsigned long) wpabuf_tailroom(data->in_buf));
345         }
346
347         return eap_wsc_build_frag_ack(id, EAP_CODE_RESPONSE);
348 }
349
350
351 static struct wpabuf * eap_wsc_process(struct eap_sm *sm, void *priv,
352                                        struct eap_method_ret *ret,
353                                        const struct wpabuf *reqData)
354 {
355         struct eap_wsc_data *data = priv;
356         const u8 *start, *pos, *end;
357         size_t len;
358         u8 op_code, flags, id;
359         u16 message_length = 0;
360         enum wps_process_res res;
361         struct wpabuf tmpbuf;
362
363         pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, reqData,
364                                &len);
365         if (pos == NULL || len < 2) {
366                 ret->ignore = TRUE;
367                 return NULL;
368         }
369
370         id = eap_get_id(reqData);
371
372         start = pos;
373         end = start + len;
374
375         op_code = *pos++;
376         flags = *pos++;
377         if (flags & WSC_FLAGS_LF) {
378                 if (end - pos < 2) {
379                         wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
380                         ret->ignore = TRUE;
381                         return NULL;
382                 }
383                 message_length = WPA_GET_BE16(pos);
384                 pos += 2;
385
386                 if (message_length < end - pos) {
387                         wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
388                                    "Length");
389                         ret->ignore = TRUE;
390                         return NULL;
391                 }
392         }
393
394         wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
395                    "Flags 0x%x Message Length %d",
396                    op_code, flags, message_length);
397
398         if (data->state == WAIT_FRAG_ACK) {
399                 if (op_code != WSC_FRAG_ACK) {
400                         wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
401                                    "in WAIT_FRAG_ACK state", op_code);
402                         ret->ignore = TRUE;
403                         return NULL;
404                 }
405                 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
406                 eap_wsc_state(data, MESG);
407                 return eap_wsc_build_msg(data, ret, id);
408         }
409
410         if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
411             op_code != WSC_Done && op_code != WSC_Start) {
412                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
413                            op_code);
414                 ret->ignore = TRUE;
415                 return NULL;
416         }
417
418         if (data->state == WAIT_START) {
419                 if (op_code != WSC_Start) {
420                         wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
421                                    "in WAIT_START state", op_code);
422                         ret->ignore = TRUE;
423                         return NULL;
424                 }
425                 wpa_printf(MSG_DEBUG, "EAP-WSC: Received start");
426                 eap_wsc_state(data, MESG);
427                 /* Start message has empty payload, skip processing */
428                 goto send_msg;
429         } else if (op_code == WSC_Start) {
430                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
431                            op_code);
432                 ret->ignore = TRUE;
433                 return NULL;
434         }
435
436         if (data->in_buf &&
437             eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
438                 ret->ignore = TRUE;
439                 return NULL;
440         }
441
442         if (flags & WSC_FLAGS_MF) {
443                 return eap_wsc_process_fragment(data, ret, id, flags, op_code,
444                                                 message_length, pos,
445                                                 end - pos);
446         }
447
448         if (data->in_buf == NULL) {
449                 /* Wrap unfragmented messages as wpabuf without extra copy */
450                 wpabuf_set(&tmpbuf, pos, end - pos);
451                 data->in_buf = &tmpbuf;
452         }
453
454         res = wps_process_msg(data->wps, op_code, data->in_buf);
455         switch (res) {
456         case WPS_DONE:
457                 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
458                            "successfully - wait for EAP failure");
459                 eap_wsc_state(data, FAIL);
460                 break;
461         case WPS_CONTINUE:
462                 eap_wsc_state(data, MESG);
463                 break;
464         case WPS_FAILURE:
465                 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
466                 eap_wsc_state(data, FAIL);
467                 break;
468         }
469
470         if (data->in_buf != &tmpbuf)
471                 wpabuf_free(data->in_buf);
472         data->in_buf = NULL;
473
474 send_msg:
475         if (data->out_buf == NULL) {
476                 data->out_buf = wps_get_msg(data->wps, &data->out_op_code);
477                 if (data->out_buf == NULL) {
478                         wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to receive "
479                                    "message from WPS");
480                         return NULL;
481                 }
482                 data->out_used = 0;
483         }
484
485         eap_wsc_state(data, MESG);
486         return eap_wsc_build_msg(data, ret, id);
487 }
488
489
490 int eap_peer_wsc_register(void)
491 {
492         struct eap_method *eap;
493         int ret;
494
495         eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
496                                     EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
497                                     "WSC");
498         if (eap == NULL)
499                 return -1;
500
501         eap->init = eap_wsc_init;
502         eap->deinit = eap_wsc_deinit;
503         eap->process = eap_wsc_process;
504
505         ret = eap_peer_method_register(eap);
506         if (ret)
507                 eap_peer_method_free(eap);
508         return ret;
509 }