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