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