PEAPv0: Added support for IPMK/CMK derivation in session resumption case
[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, 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         /* struct eap_wsc_data *data = ctx; */
72
73         wpa_printf(MSG_DEBUG, "EAP-SC: Received new WPA/WPA2-PSK from WPS for "
74                    "STA " MACSTR, MAC2STR(mac_addr));
75         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
76
77         /* TODO */
78
79         return 0;
80 }
81
82
83 static void eap_wsc_pin_needed_cb(void *ctx, const u8 *uuid_e,
84                                   const struct wps_device_data *dev)
85 {
86         /* struct eap_wsc_data *data = ctx; */
87         char uuid[40], txt[400];
88         int len;
89         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
90                 return;
91         wpa_printf(MSG_DEBUG, "EAP-WSC: PIN needed for E-UUID %s", uuid);
92         len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED "
93                           "%s " MACSTR " [%s|%s|%s|%s|%s|%d-%08X-%d]",
94                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
95                           dev->manufacturer, dev->model_name,
96                           dev->model_number, dev->serial_number,
97                           dev->categ, dev->oui, dev->sub_categ);
98         if (len > 0 && len < (int) sizeof(txt))
99                 wpa_printf(MSG_INFO, "%s", txt);
100 }
101
102
103 static void * eap_wsc_init(struct eap_sm *sm)
104 {
105         struct eap_wsc_data *data;
106         const u8 *identity;
107         size_t identity_len;
108         int registrar;
109         struct wps_config cfg;
110         u8 uuid[UUID_LEN];
111         const char *pos;
112         const char *phase1;
113         struct wps_context *wps = NULL;
114
115         identity = eap_get_config_identity(sm, &identity_len);
116
117         if (identity && identity_len == WSC_ID_REGISTRAR_LEN &&
118             os_memcmp(identity, WSC_ID_REGISTRAR, WSC_ID_REGISTRAR_LEN) == 0)
119                 registrar = 1; /* Supplicant is Registrar */
120         else if (identity && identity_len == WSC_ID_ENROLLEE_LEN &&
121             os_memcmp(identity, WSC_ID_ENROLLEE, WSC_ID_ENROLLEE_LEN) == 0)
122                 registrar = 0; /* Supplicant is Enrollee */
123         else {
124                 wpa_hexdump_ascii(MSG_INFO, "EAP-WSC: Unexpected identity",
125                                   identity, identity_len);
126                 return NULL;
127         }
128
129         data = os_zalloc(sizeof(*data));
130         if (data == NULL)
131                 return NULL;
132         data->state = registrar ? MSG : WAIT_START;
133         data->registrar = registrar;
134
135         if (registrar) {
136                 struct wps_registrar_config rcfg;
137
138                 wps = os_zalloc(sizeof(*wps));
139                 if (wps == NULL) {
140                         os_free(data);
141                         return NULL;
142                 }
143
144                 wps->cb_ctx = data;
145
146                 /* TODO: configure.. */
147                 wps->auth_types = WPS_AUTH_WPA2PSK;
148                 wps->encr_types = WPS_ENCR_AES;
149                 os_memcpy(wps->ssid, "test", 4);
150                 wps->ssid_len = 4;
151
152                 os_memset(&rcfg, 0, sizeof(rcfg));
153                 rcfg.new_psk_cb = eap_wsc_new_psk_cb;
154                 rcfg.pin_needed_cb = eap_wsc_pin_needed_cb;
155                 rcfg.cb_ctx = data;
156
157                 wps->registrar = wps_registrar_init(wps, &rcfg);
158                 if (wps->registrar == NULL) {
159                         wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to initialize "
160                                    "WPS Registrar");
161                         os_free(wps->network_key);
162                         os_free(wps);
163                         os_free(data);
164                         return NULL;
165                 }
166
167                 data->wps_ctx = wps;
168         }
169
170         os_memset(&cfg, 0, sizeof(cfg));
171         cfg.authenticator = 0;
172         cfg.wps = wps;
173         cfg.registrar = data->wps_ctx ? data->wps_ctx->registrar : NULL;
174         cfg.enrollee_mac_addr = sm->mac_addr;
175
176         phase1 = eap_get_config_phase1(sm);
177         if (phase1 == NULL) {
178                 wpa_printf(MSG_INFO, "EAP-WSC: phase1 configuration data not "
179                            "set");
180                 os_free(data);
181                 return NULL;
182         }
183
184         pos = os_strstr(phase1, "pin=");
185         if (pos) {
186                 pos += 4;
187                 cfg.pin = (const u8 *) pos;
188                 while (*pos != '\0' && *pos != ' ')
189                         pos++;
190                 cfg.pin_len = pos - (const char *) cfg.pin;
191         } else {
192                 pos = os_strstr(phase1, "pbc=1");
193                 if (pos)
194                         cfg.pbc = 1;
195         }
196
197         if (cfg.pin == NULL && !cfg.pbc) {
198                 wpa_printf(MSG_INFO, "EAP-WSC: PIN or PBC not set in phase1 "
199                            "configuration data");
200                 os_free(data);
201                 return NULL;
202         }
203
204         pos = os_strstr(phase1, "uuid=");
205         if (pos == NULL) {
206                 wpa_printf(MSG_INFO, "EAP-WSC: UUID not set in phase1 "
207                            "configuration data");
208                 os_free(data);
209                 return NULL;
210         }
211         if (uuid_str2bin(pos + 5, uuid)) {
212                 wpa_printf(MSG_INFO, "EAP-WSC: Invalid UUID in phase1 "
213                            "configuration data");
214                 os_free(data);
215                 return NULL;
216         }
217         if (registrar && wps)
218                 os_memcpy(wps->uuid, uuid, UUID_LEN);
219         else
220                 cfg.uuid = uuid;
221         cfg.wps_cred_cb = sm->eapol_cb->wps_cred;
222         cfg.cb_ctx = sm->eapol_ctx;
223         data->wps = wps_init(&cfg);
224         if (data->wps == NULL) {
225                 os_free(data);
226                 return NULL;
227         }
228         data->fragment_size = WSC_FRAGMENT_SIZE;
229
230
231         if (registrar) {
232                 /* Testing */
233                 wpa_printf(MSG_INFO, "EAP-WSC: Registrar functionality not "
234                            "yet fully supported - using test values");
235                 u8 uuid_e[UUID_LEN];
236                 os_memset(uuid_e, 0, UUID_LEN);
237                 wps_registrar_add_pin(data->wps_ctx->registrar, uuid_e,
238                                       (const u8 *) "12345670", 8);
239         }
240
241         return data;
242 }
243
244
245 static void eap_wsc_deinit(struct eap_sm *sm, void *priv)
246 {
247         struct eap_wsc_data *data = priv;
248         wpabuf_free(data->in_buf);
249         wpabuf_free(data->out_buf);
250         wps_deinit(data->wps);
251         if (data->wps_ctx) {
252                 wps_registrar_deinit(data->wps_ctx->registrar);
253                 os_free(data->wps_ctx->network_key);
254                 os_free(data->wps_ctx);
255         }
256         os_free(data);
257 }
258
259
260 static struct wpabuf * eap_wsc_build_msg(struct eap_wsc_data *data,
261                                          struct eap_method_ret *ret, u8 id)
262 {
263         struct wpabuf *resp;
264         u8 flags;
265         size_t send_len, plen;
266
267         ret->ignore = FALSE;
268         wpa_printf(MSG_DEBUG, "EAP-WSC: Generating Response");
269         ret->allowNotifications = TRUE;
270
271         flags = 0;
272         send_len = wpabuf_len(data->out_buf) - data->out_used;
273         if (2 + send_len > data->fragment_size) {
274                 send_len = data->fragment_size - 2;
275                 flags |= WSC_FLAGS_MF;
276                 if (data->out_used == 0) {
277                         flags |= WSC_FLAGS_LF;
278                         send_len -= 2;
279                 }
280         }
281         plen = 2 + send_len;
282         if (flags & WSC_FLAGS_LF)
283                 plen += 2;
284         resp = eap_msg_alloc(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, plen,
285                              EAP_CODE_RESPONSE, id);
286         if (resp == NULL)
287                 return NULL;
288
289         wpabuf_put_u8(resp, data->out_op_code); /* Op-Code */
290         wpabuf_put_u8(resp, flags); /* Flags */
291         if (flags & WSC_FLAGS_LF)
292                 wpabuf_put_be16(resp, wpabuf_len(data->out_buf));
293
294         wpabuf_put_data(resp, wpabuf_head_u8(data->out_buf) + data->out_used,
295                         send_len);
296         data->out_used += send_len;
297
298         ret->methodState = METHOD_MAY_CONT;
299         ret->decision = DECISION_FAIL;
300
301         if (data->out_used == wpabuf_len(data->out_buf)) {
302                 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
303                            "(message sent completely)",
304                            (unsigned long) send_len);
305                 wpabuf_free(data->out_buf);
306                 data->out_buf = NULL;
307                 data->out_used = 0;
308                 if ((data->state == FAIL && data->out_op_code == WSC_ACK) ||
309                     data->out_op_code == WSC_NACK ||
310                     data->out_op_code == WSC_Done) {
311                         eap_wsc_state(data, FAIL);
312                         ret->methodState = METHOD_DONE;
313                 } else
314                         eap_wsc_state(data, MSG);
315         } else {
316                 wpa_printf(MSG_DEBUG, "EAP-WSC: Sending out %lu bytes "
317                            "(%lu more to send)", (unsigned long) send_len,
318                            (unsigned long) wpabuf_len(data->out_buf) -
319                            data->out_used);
320                 eap_wsc_state(data, WAIT_FRAG_ACK);
321         }
322
323         return resp;
324 }
325
326
327 static int eap_wsc_process_cont(struct eap_wsc_data *data,
328                                 const u8 *buf, size_t len, u8 op_code)
329 {
330         /* Process continuation of a pending message */
331         if (op_code != data->in_op_code) {
332                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d in "
333                            "fragment (expected %d)",
334                            op_code, data->in_op_code);
335                 return -1;
336         }
337
338         if (len > wpabuf_tailroom(data->in_buf)) {
339                 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment overflow");
340                 eap_wsc_state(data, FAIL);
341                 return -1;
342         }
343
344         wpabuf_put_data(data->in_buf, buf, len);
345         wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes, waiting "
346                    "for %lu bytes more", (unsigned long) len,
347                    (unsigned long) wpabuf_tailroom(data->in_buf));
348
349         return 0;
350 }
351
352
353 static struct wpabuf * eap_wsc_process_fragment(struct eap_wsc_data *data,
354                                                 struct eap_method_ret *ret,
355                                                 u8 id, u8 flags, u8 op_code,
356                                                 u16 message_length,
357                                                 const u8 *buf, size_t len)
358 {
359         /* Process a fragment that is not the last one of the message */
360         if (data->in_buf == NULL && !(flags & WSC_FLAGS_LF)) {
361                 wpa_printf(MSG_DEBUG, "EAP-WSC: No Message Length field in a "
362                            "fragmented packet");
363                 ret->ignore = TRUE;
364                 return NULL;
365         }
366
367         if (data->in_buf == NULL) {
368                 /* First fragment of the message */
369                 data->in_buf = wpabuf_alloc(message_length);
370                 if (data->in_buf == NULL) {
371                         wpa_printf(MSG_DEBUG, "EAP-WSC: No memory for "
372                                    "message");
373                         ret->ignore = TRUE;
374                         return NULL;
375                 }
376                 data->in_op_code = op_code;
377                 wpabuf_put_data(data->in_buf, buf, len);
378                 wpa_printf(MSG_DEBUG, "EAP-WSC: Received %lu bytes in first "
379                            "fragment, waiting for %lu bytes more",
380                            (unsigned long) len,
381                            (unsigned long) wpabuf_tailroom(data->in_buf));
382         }
383
384         return eap_wsc_build_frag_ack(id, EAP_CODE_RESPONSE);
385 }
386
387
388 static struct wpabuf * eap_wsc_process(struct eap_sm *sm, void *priv,
389                                        struct eap_method_ret *ret,
390                                        const struct wpabuf *reqData)
391 {
392         struct eap_wsc_data *data = priv;
393         const u8 *start, *pos, *end;
394         size_t len;
395         u8 op_code, flags, id;
396         u16 message_length = 0;
397         enum wps_process_res res;
398         struct wpabuf tmpbuf;
399
400         pos = eap_hdr_validate(EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC, reqData,
401                                &len);
402         if (pos == NULL || len < 2) {
403                 ret->ignore = TRUE;
404                 return NULL;
405         }
406
407         id = eap_get_id(reqData);
408
409         start = pos;
410         end = start + len;
411
412         op_code = *pos++;
413         flags = *pos++;
414         if (flags & WSC_FLAGS_LF) {
415                 if (end - pos < 2) {
416                         wpa_printf(MSG_DEBUG, "EAP-WSC: Message underflow");
417                         ret->ignore = TRUE;
418                         return NULL;
419                 }
420                 message_length = WPA_GET_BE16(pos);
421                 pos += 2;
422
423                 if (message_length < end - pos) {
424                         wpa_printf(MSG_DEBUG, "EAP-WSC: Invalid Message "
425                                    "Length");
426                         ret->ignore = TRUE;
427                         return NULL;
428                 }
429         }
430
431         wpa_printf(MSG_DEBUG, "EAP-WSC: Received packet: Op-Code %d "
432                    "Flags 0x%x Message Length %d",
433                    op_code, flags, message_length);
434
435         if (data->state == WAIT_FRAG_ACK) {
436                 if (op_code != WSC_FRAG_ACK) {
437                         wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
438                                    "in WAIT_FRAG_ACK state", op_code);
439                         ret->ignore = TRUE;
440                         return NULL;
441                 }
442                 wpa_printf(MSG_DEBUG, "EAP-WSC: Fragment acknowledged");
443                 eap_wsc_state(data, MSG);
444                 return eap_wsc_build_msg(data, ret, id);
445         }
446
447         if (op_code != WSC_ACK && op_code != WSC_NACK && op_code != WSC_MSG &&
448             op_code != WSC_Done && op_code != WSC_Start) {
449                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
450                            op_code);
451                 ret->ignore = TRUE;
452                 return NULL;
453         }
454
455         if (data->state == WAIT_START) {
456                 if (op_code != WSC_Start) {
457                         wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d "
458                                    "in WAIT_START state", op_code);
459                         ret->ignore = TRUE;
460                         return NULL;
461                 }
462                 wpa_printf(MSG_DEBUG, "EAP-WSC: Received start");
463                 eap_wsc_state(data, MSG);
464                 /* Start message has empty payload, skip processing */
465                 goto send_msg;
466         } else if (op_code == WSC_Start) {
467                 wpa_printf(MSG_DEBUG, "EAP-WSC: Unexpected Op-Code %d",
468                            op_code);
469                 ret->ignore = TRUE;
470                 return NULL;
471         }
472
473         if (data->in_buf &&
474             eap_wsc_process_cont(data, pos, end - pos, op_code) < 0) {
475                 ret->ignore = TRUE;
476                 return NULL;
477         }
478
479         if (flags & WSC_FLAGS_MF) {
480                 return eap_wsc_process_fragment(data, ret, id, flags, op_code,
481                                                 message_length, pos,
482                                                 end - pos);
483         }
484
485         if (data->in_buf == NULL) {
486                 /* Wrap unfragmented messages as wpabuf without extra copy */
487                 wpabuf_set(&tmpbuf, pos, end - pos);
488                 data->in_buf = &tmpbuf;
489         }
490
491         res = wps_process_msg(data->wps, op_code, data->in_buf);
492         switch (res) {
493         case WPS_DONE:
494                 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing completed "
495                            "successfully - wait for EAP failure");
496                 eap_wsc_state(data, FAIL);
497                 break;
498         case WPS_CONTINUE:
499                 eap_wsc_state(data, MSG);
500                 break;
501         case WPS_FAILURE:
502                 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing failed");
503                 eap_wsc_state(data, FAIL);
504                 break;
505         case WPS_PENDING:
506                 wpa_printf(MSG_DEBUG, "EAP-WSC: WPS processing pending");
507                 ret->ignore = TRUE;
508                 if (data->in_buf == &tmpbuf)
509                         data->in_buf = NULL;
510                 return NULL;
511         }
512
513         if (data->in_buf != &tmpbuf)
514                 wpabuf_free(data->in_buf);
515         data->in_buf = NULL;
516
517 send_msg:
518         if (data->out_buf == NULL) {
519                 data->out_buf = wps_get_msg(data->wps, &data->out_op_code);
520                 if (data->out_buf == NULL) {
521                         wpa_printf(MSG_DEBUG, "EAP-WSC: Failed to receive "
522                                    "message from WPS");
523                         return NULL;
524                 }
525                 data->out_used = 0;
526         }
527
528         eap_wsc_state(data, MSG);
529         return eap_wsc_build_msg(data, ret, id);
530 }
531
532
533 int eap_peer_wsc_register(void)
534 {
535         struct eap_method *eap;
536         int ret;
537
538         eap = eap_peer_method_alloc(EAP_PEER_METHOD_INTERFACE_VERSION,
539                                     EAP_VENDOR_WFA, EAP_VENDOR_TYPE_WSC,
540                                     "WSC");
541         if (eap == NULL)
542                 return -1;
543
544         eap->init = eap_wsc_init;
545         eap->deinit = eap_wsc_deinit;
546         eap->process = eap_wsc_process;
547
548         ret = eap_peer_method_register(eap);
549         if (ret)
550                 eap_peer_method_free(eap);
551         return ret;
552 }