e3b1f2c62bd5ec87ac60509ce7b91868b23a4cb1
[mech_eap.git] / src / wps / wps_registrar.c
1 /*
2  * Wi-Fi Protected Setup - Registrar
3  * Copyright (c) 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 "sha256.h"
19 #include "base64.h"
20 #include "ieee802_11_defs.h"
21 #include "eloop.h"
22 #include "wps_i.h"
23 #include "wps_dev_attr.h"
24
25
26 struct wps_uuid_pin {
27         struct wps_uuid_pin *next;
28         u8 uuid[WPS_UUID_LEN];
29         int wildcard_uuid;
30         u8 *pin;
31         size_t pin_len;
32         int locked;
33 };
34
35
36 static void wps_free_pin(struct wps_uuid_pin *pin)
37 {
38         os_free(pin->pin);
39         os_free(pin);
40 }
41
42
43 static void wps_free_pins(struct wps_uuid_pin *pins)
44 {
45         struct wps_uuid_pin *pin, *prev;
46
47         pin = pins;
48         while (pin) {
49                 prev = pin;
50                 pin = pin->next;
51                 wps_free_pin(prev);
52         }
53 }
54
55
56 struct wps_pbc_session {
57         struct wps_pbc_session *next;
58         u8 addr[ETH_ALEN];
59         u8 uuid_e[WPS_UUID_LEN];
60         struct os_time timestamp;
61 };
62
63
64 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
65 {
66         struct wps_pbc_session *prev;
67
68         while (pbc) {
69                 prev = pbc;
70                 pbc = pbc->next;
71                 os_free(prev);
72         }
73 }
74
75
76 struct wps_registrar {
77         struct wps_context *wps;
78
79         int pbc;
80         int selected_registrar;
81
82         int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
83                           size_t psk_len);
84         int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
85                          const u8 *probe_resp_ie, size_t probe_resp_ie_len);
86         void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
87                               const struct wps_device_data *dev);
88         void *cb_ctx;
89
90         struct wps_uuid_pin *pins;
91         struct wps_pbc_session *pbc_sessions;
92 };
93
94
95 static int wps_set_ie(struct wps_registrar *reg);
96 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
97
98
99 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
100                                           const u8 *addr, const u8 *uuid_e)
101 {
102         struct wps_pbc_session *pbc, *prev = NULL;
103         struct os_time now;
104
105         os_get_time(&now);
106
107         pbc = reg->pbc_sessions;
108         while (pbc) {
109                 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
110                     os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
111                         if (prev)
112                                 prev->next = pbc->next;
113                         else
114                                 reg->pbc_sessions = pbc->next;
115                         break;
116                 }
117                 prev = pbc;
118                 pbc = pbc->next;
119         }
120
121         if (!pbc) {
122                 pbc = os_zalloc(sizeof(*pbc));
123                 if (pbc == NULL)
124                         return;
125                 os_memcpy(pbc->addr, addr, ETH_ALEN);
126                 if (uuid_e)
127                         os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
128         }
129
130         pbc->next = reg->pbc_sessions;
131         reg->pbc_sessions = pbc;
132         pbc->timestamp = now;
133
134         /* remove entries that have timed out */
135         prev = pbc;
136         pbc = pbc->next;
137
138         while (pbc) {
139                 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
140                         prev->next = NULL;
141                         wps_free_pbc_sessions(pbc);
142                         break;
143                 }
144                 prev = pbc;
145                 pbc = pbc->next;
146         }
147 }
148
149
150 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
151                                              const u8 *addr, const u8 *uuid_e)
152 {
153         struct wps_pbc_session *pbc, *prev = NULL;
154
155         pbc = reg->pbc_sessions;
156         while (pbc) {
157                 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
158                     os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
159                         if (prev)
160                                 prev->next = pbc->next;
161                         else
162                                 reg->pbc_sessions = pbc->next;
163                         os_free(pbc);
164                         break;
165                 }
166                 prev = pbc;
167                 pbc = pbc->next;
168         }
169 }
170
171
172 int wps_registrar_pbc_overlap(struct wps_registrar *reg,
173                               const u8 *addr, const u8 *uuid_e)
174 {
175         int count = 0;
176         struct wps_pbc_session *pbc;
177         struct os_time now;
178
179         os_get_time(&now);
180
181         for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
182                 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
183                         break;
184                 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
185                     uuid_e == NULL ||
186                     os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
187                         count++;
188         }
189
190         if (addr || uuid_e)
191                 count++;
192
193         return count > 1 ? 1 : 0;
194 }
195
196
197 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
198 {
199         wpa_printf(MSG_DEBUG, "WPS:  * Wi-Fi Protected Setup State (%d)",
200                    wps->wps_state);
201         wpabuf_put_be16(msg, ATTR_WPS_STATE);
202         wpabuf_put_be16(msg, 1);
203         wpabuf_put_u8(msg, wps->wps_state);
204         return 0;
205 }
206
207
208 static int wps_build_ap_setup_locked(struct wps_context *wps,
209                                      struct wpabuf *msg)
210 {
211         if (wps->ap_setup_locked) {
212                 wpa_printf(MSG_DEBUG, "WPS:  * AP Setup Locked");
213                 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
214                 wpabuf_put_be16(msg, 1);
215                 wpabuf_put_u8(msg, 1);
216         }
217         return 0;
218 }
219
220
221 static int wps_build_selected_registrar(struct wps_registrar *reg,
222                                         struct wpabuf *msg)
223 {
224         if (!reg->selected_registrar)
225                 return 0;
226         wpa_printf(MSG_DEBUG, "WPS:  * Selected Registrar");
227         wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
228         wpabuf_put_be16(msg, 1);
229         wpabuf_put_u8(msg, 1);
230         return 0;
231 }
232
233
234 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
235                                              struct wpabuf *msg)
236 {
237         u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
238         if (!reg->selected_registrar)
239                 return 0;
240         wpa_printf(MSG_DEBUG, "WPS:  * Device Password ID (%d)", id);
241         wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
242         wpabuf_put_be16(msg, 2);
243         wpabuf_put_be16(msg, id);
244         return 0;
245 }
246
247
248 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
249                                             struct wpabuf *msg)
250 {
251         u16 methods;
252         if (!reg->selected_registrar)
253                 return 0;
254         methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
255         if (reg->pbc)
256                 methods |= WPS_CONFIG_PUSHBUTTON;
257         wpa_printf(MSG_DEBUG, "WPS:  * Selected Registrar Config Methods (%x)",
258                    methods);
259         wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
260         wpabuf_put_be16(msg, 2);
261         wpabuf_put_be16(msg, methods);
262         return 0;
263 }
264
265
266 static int wps_build_probe_config_methods(struct wps_registrar *reg,
267                                           struct wpabuf *msg)
268 {
269         u16 methods;
270         methods = 0;
271         wpa_printf(MSG_DEBUG, "WPS:  * Config Methods (%x)", methods);
272         wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
273         wpabuf_put_be16(msg, 2);
274         wpabuf_put_be16(msg, methods);
275         return 0;
276 }
277
278
279 static int wps_build_config_methods_r(struct wps_registrar *reg,
280                                       struct wpabuf *msg)
281 {
282         u16 methods;
283         methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
284         if (reg->pbc)
285                 methods |= WPS_CONFIG_PUSHBUTTON;
286         return wps_build_config_methods(msg, methods);
287 }
288
289
290 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
291 {
292         u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
293         wpa_printf(MSG_DEBUG, "WPS:  * Response Type (%d)", resp);
294         wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
295         wpabuf_put_be16(msg, 1);
296         wpabuf_put_u8(msg, resp);
297         return 0;
298 }
299
300
301 /**
302  * wps_registrar_init - Initialize WPS Registrar data
303  * @wps: Pointer to longterm WPS context
304  * @cfg: Registrar configuration
305  * Returns: Pointer to allocated Registrar data or %NULL on failure
306  *
307  * This function is used to initialize WPS Registrar functionality. It can be
308  * used for a single Registrar run (e.g., when run in a supplicant) or multiple
309  * runs (e.g., when run as an internal Registrar in an AP). Caller is
310  * responsible for freeing the returned data with wps_registrar_deinit() when
311  * Registrar functionality is not needed anymore.
312  */
313 struct wps_registrar *
314 wps_registrar_init(struct wps_context *wps,
315                    const struct wps_registrar_config *cfg)
316 {
317         struct wps_registrar *reg = os_zalloc(sizeof(*reg));
318         if (reg == NULL)
319                 return NULL;
320
321         reg->wps = wps;
322         reg->new_psk_cb = cfg->new_psk_cb;
323         reg->set_ie_cb = cfg->set_ie_cb;
324         reg->pin_needed_cb = cfg->pin_needed_cb;
325         reg->cb_ctx = cfg->cb_ctx;
326
327         if (wps_set_ie(reg)) {
328                 wps_registrar_deinit(reg);
329                 return NULL;
330         }
331
332         return reg;
333 }
334
335
336 /**
337  * wps_registrar_deinit - Deinitialize WPS Registrar data
338  * @reg: Registrar data from wps_registrar_deinit()
339  */
340 void wps_registrar_deinit(struct wps_registrar *reg)
341 {
342         if (reg == NULL)
343                 return;
344         eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
345         wps_free_pins(reg->pins);
346         wps_free_pbc_sessions(reg->pbc_sessions);
347         os_free(reg);
348 }
349
350
351 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
352                           const u8 *pin, size_t pin_len)
353 {
354         struct wps_uuid_pin *p;
355
356         p = os_zalloc(sizeof(*p));
357         if (p == NULL)
358                 return -1;
359         if (uuid == NULL)
360                 p->wildcard_uuid = 1;
361         else
362                 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
363         p->pin = os_malloc(pin_len);
364         if (p->pin == NULL) {
365                 os_free(p);
366                 return -1;
367         }
368         os_memcpy(p->pin, pin, pin_len);
369         p->pin_len = pin_len;
370
371         p->next = reg->pins;
372         reg->pins = p;
373
374         wpa_printf(MSG_DEBUG, "WPS: A new PIN configured");
375         wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
376         wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
377         reg->selected_registrar = 1;
378         reg->pbc = 0;
379         wps_set_ie(reg);
380
381         return 0;
382 }
383
384
385 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
386 {
387         struct wps_uuid_pin *pin, *prev;
388
389         prev = NULL;
390         pin = reg->pins;
391         while (pin) {
392                 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
393                         if (prev == NULL)
394                                 reg->pins = pin->next;
395                         else
396                                 prev->next = pin->next;
397                         wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
398                                     pin->uuid, WPS_UUID_LEN);
399                         wps_free_pin(pin);
400                         return 0;
401                 }
402                 prev = pin;
403                 pin = pin->next;
404         }
405
406         return -1;
407 }
408
409
410 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
411                                         const u8 *uuid, size_t *pin_len)
412 {
413         struct wps_uuid_pin *pin;
414
415         pin = reg->pins;
416         while (pin) {
417                 if (!pin->wildcard_uuid &&
418                     os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0)
419                         break;
420                 pin = pin->next;
421         }
422
423         if (!pin) {
424                 /* Check for wildcard UUIDs since none of the UUID-specific
425                  * PINs matched */
426                 pin = reg->pins;
427                 while (pin) {
428                         if (pin->wildcard_uuid == 1) {
429                                 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
430                                            "PIN. Assigned it for this UUID-E");
431                                 pin->wildcard_uuid = 2;
432                                 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
433                                 break;
434                         }
435                         pin = pin->next;
436                 }
437         }
438
439         if (!pin)
440                 return NULL;
441
442         /*
443          * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
444          * that could otherwise avoid PIN invalidations.
445          */
446         if (pin->locked) {
447                 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
448                            "allow concurrent re-use");
449                 return NULL;
450         }
451         *pin_len = pin->pin_len;
452         pin->locked = 1;
453         return pin->pin;
454 }
455
456
457 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
458 {
459         struct wps_uuid_pin *pin;
460
461         pin = reg->pins;
462         while (pin) {
463                 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
464                         if (pin->wildcard_uuid == 2) {
465                                 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
466                                            "wildcard PIN");
467                                 return wps_registrar_invalidate_pin(reg, uuid);
468                         }
469                         pin->locked = 0;
470                         return 0;
471                 }
472                 pin = pin->next;
473         }
474
475         return -1;
476 }
477
478
479 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
480 {
481         struct wps_registrar *reg = eloop_ctx;
482
483         wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
484         reg->selected_registrar = 0;
485         reg->pbc = 0;
486         wps_set_ie(reg);
487 }
488
489
490 int wps_registrar_button_pushed(struct wps_registrar *reg)
491 {
492         if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
493                 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
494                            "mode");
495                 return -1;
496         }
497         wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
498         reg->selected_registrar = 1;
499         reg->pbc = 1;
500         wps_set_ie(reg);
501
502         eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
503         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
504                                reg, NULL);
505         return 0;
506 }
507
508
509 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
510 {
511         wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
512         eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
513         reg->selected_registrar = 0;
514         reg->pbc = 0;
515         wps_set_ie(reg);
516 }
517
518
519 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
520                                 const struct wpabuf *wps_data)
521 {
522         struct wps_parse_attr attr;
523         u16 methods;
524
525         wpa_hexdump_buf(MSG_MSGDUMP,
526                         "WPS: Probe Request with WPS data received",
527                         wps_data);
528
529         if (wps_parse_msg(wps_data, &attr) < 0 ||
530             attr.version == NULL || *attr.version != WPS_VERSION) {
531                 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
532                            "version 0x%x", attr.version ? *attr.version : 0);
533                 return;
534         }
535
536         if (attr.config_methods == NULL) {
537                 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
538                            "Probe Request");
539                 return;
540         }
541
542         methods = WPA_GET_BE16(attr.config_methods);
543         if (!(methods & WPS_CONFIG_PUSHBUTTON))
544                 return; /* Not PBC */
545
546         wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
547                    MACSTR, MAC2STR(addr));
548
549         wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
550 }
551
552
553 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
554                           const u8 *psk, size_t psk_len)
555 {
556         if (reg->new_psk_cb == NULL)
557                 return 0;
558
559         return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
560 }
561
562
563 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
564                               const struct wps_device_data *dev)
565 {
566         if (reg->pin_needed_cb == NULL)
567                 return;
568
569         reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
570 }
571
572
573 static int wps_cb_set_ie(struct wps_registrar *reg,
574                          const struct wpabuf *beacon_ie,
575                          const struct wpabuf *probe_resp_ie)
576 {
577         if (reg->set_ie_cb == NULL)
578                 return 0;
579
580         return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
581                               wpabuf_len(beacon_ie),
582                               wpabuf_head(probe_resp_ie),
583                               wpabuf_len(probe_resp_ie));
584 }
585
586
587 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
588 static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
589 {
590         struct wpabuf *ie;
591         const u8 *pos, *end;
592
593         ie = wpabuf_alloc(wpabuf_len(data) + 100);
594         if (ie == NULL) {
595                 wpabuf_free(data);
596                 return NULL;
597         }
598
599         pos = wpabuf_head(data);
600         end = pos + wpabuf_len(data);
601
602         while (end > pos) {
603                 size_t frag_len = end - pos;
604                 if (frag_len > 251)
605                         frag_len = 251;
606                 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
607                 wpabuf_put_u8(ie, 4 + frag_len);
608                 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
609                 wpabuf_put_data(ie, pos, frag_len);
610                 pos += frag_len;
611         }
612
613         wpabuf_free(data);
614
615         return ie;
616 }
617
618
619 static int wps_set_ie(struct wps_registrar *reg)
620 {
621         struct wpabuf *beacon;
622         struct wpabuf *probe;
623         int ret;
624
625         wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
626
627         beacon = wpabuf_alloc(300);
628         if (beacon == NULL)
629                 return -1;
630         probe = wpabuf_alloc(400);
631         if (probe == NULL) {
632                 wpabuf_free(beacon);
633                 return -1;
634         }
635
636         if (wps_build_version(beacon) ||
637             wps_build_wps_state(reg->wps, beacon) ||
638             wps_build_ap_setup_locked(reg->wps, beacon) ||
639             wps_build_selected_registrar(reg, beacon) ||
640             wps_build_sel_reg_dev_password_id(reg, beacon) ||
641             wps_build_sel_reg_config_methods(reg, beacon) ||
642             wps_build_version(probe) ||
643             wps_build_wps_state(reg->wps, probe) ||
644             wps_build_ap_setup_locked(reg->wps, probe) ||
645             wps_build_selected_registrar(reg, probe) ||
646             wps_build_sel_reg_dev_password_id(reg, probe) ||
647             wps_build_sel_reg_config_methods(reg, probe) ||
648             wps_build_resp_type(reg, probe) ||
649             wps_build_uuid_e(probe, reg->wps->uuid) ||
650             wps_build_device_attrs(&reg->wps->dev, probe) ||
651             wps_build_probe_config_methods(reg, probe) ||
652             wps_build_rf_bands(&reg->wps->dev, probe)) {
653                 wpabuf_free(beacon);
654                 wpabuf_free(probe);
655                 return -1;
656         }
657
658         beacon = wps_ie_encapsulate(beacon);
659         probe = wps_ie_encapsulate(probe);
660
661         if (!beacon || !probe) {
662                 wpabuf_free(beacon);
663                 wpabuf_free(probe);
664                 return -1;
665         }
666
667         ret = wps_cb_set_ie(reg, beacon, probe);
668         wpabuf_free(beacon);
669         wpabuf_free(probe);
670
671         return ret;
672 }
673
674
675 static int wps_get_dev_password(struct wps_data *wps)
676 {
677         const u8 *pin;
678         size_t pin_len = 0;
679
680         os_free(wps->dev_password);
681         wps->dev_password = NULL;
682
683         if (wps->pbc) {
684                 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
685                 pin = (const u8 *) "00000000";
686                 pin_len = 8;
687         } else {
688                 pin = wps_registrar_get_pin(wps->registrar, wps->uuid_e,
689                                             &pin_len);
690         }
691         if (pin == NULL) {
692                 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
693                            "the Enrollee");
694                 wps_cb_pin_needed(wps->registrar, wps->uuid_e, &wps->peer_dev);
695                 return -1;
696         }
697
698         wps->dev_password = os_malloc(pin_len);
699         if (wps->dev_password == NULL)
700                 return -1;
701         os_memcpy(wps->dev_password, pin, pin_len);
702         wps->dev_password_len = pin_len;
703
704         return 0;
705 }
706
707
708 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
709 {
710         wpa_printf(MSG_DEBUG, "WPS:  * UUID-R");
711         wpabuf_put_be16(msg, ATTR_UUID_R);
712         wpabuf_put_be16(msg, WPS_UUID_LEN);
713         wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
714         return 0;
715 }
716
717
718 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
719 {
720         u8 *hash;
721         const u8 *addr[4];
722         size_t len[4];
723
724         if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
725                 return -1;
726         wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
727         wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
728                     wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
729
730         if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
731                 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
732                            "R-Hash derivation");
733                 return -1;
734         }
735
736         wpa_printf(MSG_DEBUG, "WPS:  * R-Hash1");
737         wpabuf_put_be16(msg, ATTR_R_HASH1);
738         wpabuf_put_be16(msg, SHA256_MAC_LEN);
739         hash = wpabuf_put(msg, SHA256_MAC_LEN);
740         /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
741         addr[0] = wps->snonce;
742         len[0] = WPS_SECRET_NONCE_LEN;
743         addr[1] = wps->psk1;
744         len[1] = WPS_PSK_LEN;
745         addr[2] = wpabuf_head(wps->dh_pubkey_e);
746         len[2] = wpabuf_len(wps->dh_pubkey_e);
747         addr[3] = wpabuf_head(wps->dh_pubkey_r);
748         len[3] = wpabuf_len(wps->dh_pubkey_r);
749         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
750         wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
751
752         wpa_printf(MSG_DEBUG, "WPS:  * R-Hash2");
753         wpabuf_put_be16(msg, ATTR_R_HASH2);
754         wpabuf_put_be16(msg, SHA256_MAC_LEN);
755         hash = wpabuf_put(msg, SHA256_MAC_LEN);
756         /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
757         addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
758         addr[1] = wps->psk2;
759         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
760         wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
761
762         return 0;
763 }
764
765
766 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
767 {
768         wpa_printf(MSG_DEBUG, "WPS:  * R-SNonce1");
769         wpabuf_put_be16(msg, ATTR_R_SNONCE1);
770         wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
771         wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
772         return 0;
773 }
774
775
776 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
777 {
778         wpa_printf(MSG_DEBUG, "WPS:  * R-SNonce2");
779         wpabuf_put_be16(msg, ATTR_R_SNONCE2);
780         wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
781         wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
782                         WPS_SECRET_NONCE_LEN);
783         return 0;
784 }
785
786
787 static int wps_build_cred_network_idx(struct wpabuf *msg,
788                                       struct wps_credential *cred)
789 {
790         wpa_printf(MSG_DEBUG, "WPS:  * Network Index");
791         wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
792         wpabuf_put_be16(msg, 1);
793         wpabuf_put_u8(msg, 0);
794         return 0;
795 }
796
797
798 static int wps_build_cred_ssid(struct wpabuf *msg,
799                                struct wps_credential *cred)
800 {
801         wpa_printf(MSG_DEBUG, "WPS:  * SSID");
802         wpabuf_put_be16(msg, ATTR_SSID);
803         wpabuf_put_be16(msg, cred->ssid_len);
804         wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
805         return 0;
806 }
807
808
809 static int wps_build_cred_auth_type(struct wpabuf *msg,
810                                     struct wps_credential *cred)
811 {
812         wpa_printf(MSG_DEBUG, "WPS:  * Authentication Type (0x%x)",
813                    cred->auth_type);
814         wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
815         wpabuf_put_be16(msg, 2);
816         wpabuf_put_be16(msg, cred->auth_type);
817         return 0;
818 }
819
820
821 static int wps_build_cred_encr_type(struct wpabuf *msg,
822                                     struct wps_credential *cred)
823 {
824         wpa_printf(MSG_DEBUG, "WPS:  * Encryption Type (0x%x)",
825                    cred->encr_type);
826         wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
827         wpabuf_put_be16(msg, 2);
828         wpabuf_put_be16(msg, cred->encr_type);
829         return 0;
830 }
831
832
833 static int wps_build_cred_network_key(struct wpabuf *msg,
834                                       struct wps_credential *cred)
835 {
836         wpa_printf(MSG_DEBUG, "WPS:  * Network Key");
837         wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
838         wpabuf_put_be16(msg, cred->key_len);
839         wpabuf_put_data(msg, cred->key, cred->key_len);
840         return 0;
841 }
842
843
844 static int wps_build_cred_mac_addr(struct wpabuf *msg,
845                                    struct wps_credential *cred)
846 {
847         wpa_printf(MSG_DEBUG, "WPS:  * MAC Address");
848         wpabuf_put_be16(msg, ATTR_MAC_ADDR);
849         wpabuf_put_be16(msg, ETH_ALEN);
850         wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
851         return 0;
852 }
853
854
855 static int wps_build_credential(struct wpabuf *msg,
856                                 struct wps_credential *cred)
857 {
858         if (wps_build_cred_network_idx(msg, cred) ||
859             wps_build_cred_ssid(msg, cred) ||
860             wps_build_cred_auth_type(msg, cred) ||
861             wps_build_cred_encr_type(msg, cred) ||
862             wps_build_cred_network_key(msg, cred) ||
863             wps_build_cred_mac_addr(msg, cred))
864                 return -1;
865         return 0;
866 }
867
868
869 static int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
870 {
871         struct wpabuf *cred;
872
873         wpa_printf(MSG_DEBUG, "WPS:  * Credential");
874         os_memset(&wps->cred, 0, sizeof(wps->cred));
875
876         os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
877         wps->cred.ssid_len = wps->wps->ssid_len;
878
879         /* Select the best authentication and encryption type */
880         if (wps->auth_type & WPS_AUTH_WPA2PSK)
881                 wps->auth_type = WPS_AUTH_WPA2PSK;
882         else if (wps->auth_type & WPS_AUTH_WPAPSK)
883                 wps->auth_type = WPS_AUTH_WPAPSK;
884         else if (wps->auth_type & WPS_AUTH_OPEN)
885                 wps->auth_type = WPS_AUTH_OPEN;
886         else if (wps->auth_type & WPS_AUTH_SHARED)
887                 wps->auth_type = WPS_AUTH_SHARED;
888         else {
889                 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
890                            wps->auth_type);
891                 return -1;
892         }
893         wps->cred.auth_type = wps->auth_type;
894
895         if (wps->auth_type == WPS_AUTH_WPA2PSK ||
896             wps->auth_type == WPS_AUTH_WPAPSK) {
897                 if (wps->encr_type & WPS_ENCR_AES)
898                         wps->encr_type = WPS_ENCR_AES;
899                 else if (wps->encr_type & WPS_ENCR_TKIP)
900                         wps->encr_type = WPS_ENCR_TKIP;
901                 else {
902                         wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
903                                    "type for WPA/WPA2");
904                         return -1;
905                 }
906         } else {
907                 if (wps->encr_type & WPS_ENCR_WEP)
908                         wps->encr_type = WPS_ENCR_WEP;
909                 else if (wps->encr_type & WPS_ENCR_NONE)
910                         wps->encr_type = WPS_ENCR_NONE;
911                 else {
912                         wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
913                                    "type for non-WPA/WPA2 mode");
914                         return -1;
915                 }
916         }
917         wps->cred.encr_type = wps->encr_type;
918         os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
919
920         if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap) {
921                 u8 r[16];
922                 /* Generate a random passphrase */
923                 if (os_get_random(r, sizeof(r)) < 0)
924                         return -1;
925                 os_free(wps->new_psk);
926                 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
927                 if (wps->new_psk == NULL)
928                         return -1;
929                 wps->new_psk_len--; /* remove newline */
930                 while (wps->new_psk_len &&
931                        wps->new_psk[wps->new_psk_len - 1] == '=')
932                         wps->new_psk_len--;
933                 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
934                                       wps->new_psk, wps->new_psk_len);
935                 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
936                 wps->cred.key_len = wps->new_psk_len;
937         } else if (wps->wps->network_key) {
938                 os_memcpy(wps->cred.key, wps->wps->network_key,
939                           wps->wps->network_key_len);
940                 wps->cred.key_len = wps->wps->network_key_len;
941         } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
942                 char hex[65];
943                 /* Generate a random per-device PSK */
944                 os_free(wps->new_psk);
945                 wps->new_psk_len = 32;
946                 wps->new_psk = os_malloc(wps->new_psk_len);
947                 if (wps->new_psk == NULL)
948                         return -1;
949                 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
950                         os_free(wps->new_psk);
951                         wps->new_psk = NULL;
952                         return -1;
953                 }
954                 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
955                                 wps->new_psk, wps->new_psk_len);
956                 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
957                                  wps->new_psk_len);
958                 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
959                 wps->cred.key_len = wps->new_psk_len * 2;
960         }
961
962         cred = wpabuf_alloc(200);
963         if (cred == NULL)
964                 return -1;
965
966         if (wps_build_credential(cred, &wps->cred)) {
967                 wpabuf_free(cred);
968                 return -1;
969         }
970
971         wpabuf_put_be16(msg, ATTR_CRED);
972         wpabuf_put_be16(msg, wpabuf_len(cred));
973         wpabuf_put_buf(msg, cred);
974         wpabuf_free(cred);
975
976         return 0;
977 }
978
979
980 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
981 {
982         wpa_printf(MSG_DEBUG, "WPS:  * AP Settings");
983
984         if (wps_build_credential(msg, &wps->cred))
985                 return -1;
986
987         return 0;
988 }
989
990
991 static struct wpabuf * wps_build_m2(struct wps_data *wps)
992 {
993         struct wpabuf *msg;
994
995         if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
996                 return NULL;
997         wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
998                     wps->nonce_r, WPS_NONCE_LEN);
999         wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1000
1001         wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1002         msg = wpabuf_alloc(1000);
1003         if (msg == NULL)
1004                 return NULL;
1005
1006         if (wps_build_version(msg) ||
1007             wps_build_msg_type(msg, WPS_M2) ||
1008             wps_build_enrollee_nonce(wps, msg) ||
1009             wps_build_registrar_nonce(wps, msg) ||
1010             wps_build_uuid_r(wps, msg) ||
1011             wps_build_public_key(wps, msg) ||
1012             wps_derive_keys(wps) ||
1013             wps_build_auth_type_flags(wps, msg) ||
1014             wps_build_encr_type_flags(wps, msg) ||
1015             wps_build_conn_type_flags(wps, msg) ||
1016             wps_build_config_methods_r(wps->registrar, msg) ||
1017             wps_build_device_attrs(&wps->wps->dev, msg) ||
1018             wps_build_rf_bands(&wps->wps->dev, msg) ||
1019             wps_build_assoc_state(wps, msg) ||
1020             wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1021             wps_build_dev_password_id(msg, DEV_PW_DEFAULT) ||
1022             wps_build_os_version(&wps->wps->dev, msg) ||
1023             wps_build_authenticator(wps, msg)) {
1024                 wpabuf_free(msg);
1025                 return NULL;
1026         }
1027
1028         wps->state = RECV_M3;
1029         return msg;
1030 }
1031
1032
1033 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1034 {
1035         struct wpabuf *msg;
1036         u16 err = WPS_CFG_NO_ERROR;
1037
1038         wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1039         msg = wpabuf_alloc(1000);
1040         if (msg == NULL)
1041                 return NULL;
1042
1043         if (wps->authenticator && wps->wps->ap_setup_locked)
1044                 err = WPS_CFG_SETUP_LOCKED;
1045
1046         if (wps_build_version(msg) ||
1047             wps_build_msg_type(msg, WPS_M2D) ||
1048             wps_build_enrollee_nonce(wps, msg) ||
1049             wps_build_registrar_nonce(wps, msg) ||
1050             wps_build_uuid_r(wps, msg) ||
1051             wps_build_auth_type_flags(wps, msg) ||
1052             wps_build_encr_type_flags(wps, msg) ||
1053             wps_build_conn_type_flags(wps, msg) ||
1054             wps_build_config_methods_r(wps->registrar, msg) ||
1055             wps_build_device_attrs(&wps->wps->dev, msg) ||
1056             wps_build_rf_bands(&wps->wps->dev, msg) ||
1057             wps_build_assoc_state(wps, msg) ||
1058             wps_build_config_error(msg, err) ||
1059             wps_build_os_version(&wps->wps->dev, msg)) {
1060                 wpabuf_free(msg);
1061                 return NULL;
1062         }
1063
1064         wps->state = RECV_M2D_ACK;
1065         return msg;
1066 }
1067
1068
1069 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1070 {
1071         struct wpabuf *msg, *plain;
1072
1073         wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1074
1075         wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1076
1077         plain = wpabuf_alloc(200);
1078         if (plain == NULL)
1079                 return NULL;
1080
1081         msg = wpabuf_alloc(1000);
1082         if (msg == NULL) {
1083                 wpabuf_free(plain);
1084                 return NULL;
1085         }
1086
1087         if (wps_build_version(msg) ||
1088             wps_build_msg_type(msg, WPS_M4) ||
1089             wps_build_enrollee_nonce(wps, msg) ||
1090             wps_build_r_hash(wps, msg) ||
1091             wps_build_r_snonce1(wps, plain) ||
1092             wps_build_key_wrap_auth(wps, plain) ||
1093             wps_build_encr_settings(wps, msg, plain) ||
1094             wps_build_authenticator(wps, msg)) {
1095                 wpabuf_free(plain);
1096                 wpabuf_free(msg);
1097                 return NULL;
1098         }
1099         wpabuf_free(plain);
1100
1101         wps->state = RECV_M5;
1102         return msg;
1103 }
1104
1105
1106 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1107 {
1108         struct wpabuf *msg, *plain;
1109
1110         wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1111
1112         plain = wpabuf_alloc(200);
1113         if (plain == NULL)
1114                 return NULL;
1115
1116         msg = wpabuf_alloc(1000);
1117         if (msg == NULL) {
1118                 wpabuf_free(plain);
1119                 return NULL;
1120         }
1121
1122         if (wps_build_version(msg) ||
1123             wps_build_msg_type(msg, WPS_M6) ||
1124             wps_build_enrollee_nonce(wps, msg) ||
1125             wps_build_r_snonce2(wps, plain) ||
1126             wps_build_key_wrap_auth(wps, plain) ||
1127             wps_build_encr_settings(wps, msg, plain) ||
1128             wps_build_authenticator(wps, msg)) {
1129                 wpabuf_free(plain);
1130                 wpabuf_free(msg);
1131                 return NULL;
1132         }
1133         wpabuf_free(plain);
1134
1135         wps->wps_pin_revealed = 1;
1136         wps->state = RECV_M7;
1137         return msg;
1138 }
1139
1140
1141 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1142 {
1143         struct wpabuf *msg, *plain;
1144
1145         wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1146
1147         plain = wpabuf_alloc(500);
1148         if (plain == NULL)
1149                 return NULL;
1150
1151         msg = wpabuf_alloc(1000);
1152         if (msg == NULL) {
1153                 wpabuf_free(plain);
1154                 return NULL;
1155         }
1156
1157         if (wps_build_version(msg) ||
1158             wps_build_msg_type(msg, WPS_M8) ||
1159             wps_build_enrollee_nonce(wps, msg) ||
1160             (wps->wps->ap && wps_build_cred(wps, plain)) ||
1161             (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
1162             wps_build_key_wrap_auth(wps, plain) ||
1163             wps_build_encr_settings(wps, msg, plain) ||
1164             wps_build_authenticator(wps, msg)) {
1165                 wpabuf_free(plain);
1166                 wpabuf_free(msg);
1167                 return NULL;
1168         }
1169         wpabuf_free(plain);
1170
1171         wps->state = RECV_DONE;
1172         return msg;
1173 }
1174
1175
1176 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1177 {
1178         struct wpabuf *msg;
1179
1180         wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1181
1182         msg = wpabuf_alloc(1000);
1183         if (msg == NULL)
1184                 return NULL;
1185
1186         if (wps_build_version(msg) ||
1187             wps_build_msg_type(msg, WPS_WSC_ACK) ||
1188             wps_build_enrollee_nonce(wps, msg) ||
1189             wps_build_registrar_nonce(wps, msg)) {
1190                 wpabuf_free(msg);
1191                 return NULL;
1192         }
1193
1194         return msg;
1195 }
1196
1197
1198 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1199 {
1200         struct wpabuf *msg;
1201
1202         wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1203
1204         msg = wpabuf_alloc(1000);
1205         if (msg == NULL)
1206                 return NULL;
1207
1208         if (wps_build_version(msg) ||
1209             wps_build_msg_type(msg, WPS_WSC_NACK) ||
1210             wps_build_enrollee_nonce(wps, msg) ||
1211             wps_build_registrar_nonce(wps, msg) ||
1212             wps_build_config_error(msg, wps->config_error)) {
1213                 wpabuf_free(msg);
1214                 return NULL;
1215         }
1216
1217         return msg;
1218 }
1219
1220
1221 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1222                                       enum wsc_op_code *op_code)
1223 {
1224         struct wpabuf *msg;
1225
1226         switch (wps->state) {
1227         case SEND_M2:
1228                 if (wps_get_dev_password(wps) < 0)
1229                         msg = wps_build_m2d(wps);
1230                 else
1231                         msg = wps_build_m2(wps);
1232                 *op_code = WSC_MSG;
1233                 break;
1234         case SEND_M2D:
1235                 msg = wps_build_m2d(wps);
1236                 *op_code = WSC_MSG;
1237                 break;
1238         case SEND_M4:
1239                 msg = wps_build_m4(wps);
1240                 *op_code = WSC_MSG;
1241                 break;
1242         case SEND_M6:
1243                 msg = wps_build_m6(wps);
1244                 *op_code = WSC_MSG;
1245                 break;
1246         case SEND_M8:
1247                 msg = wps_build_m8(wps);
1248                 *op_code = WSC_MSG;
1249                 break;
1250         case RECV_DONE:
1251                 msg = wps_build_wsc_ack(wps);
1252                 *op_code = WSC_ACK;
1253                 break;
1254         case SEND_WSC_NACK:
1255                 msg = wps_build_wsc_nack(wps);
1256                 *op_code = WSC_NACK;
1257                 break;
1258         default:
1259                 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1260                            "a message", wps->state);
1261                 msg = NULL;
1262                 break;
1263         }
1264
1265         if (*op_code == WSC_MSG && msg) {
1266                 /* Save a copy of the last message for Authenticator derivation
1267                  */
1268                 wpabuf_free(wps->last_msg);
1269                 wps->last_msg = wpabuf_dup(msg);
1270         }
1271
1272         return msg;
1273 }
1274
1275
1276 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1277 {
1278         if (e_nonce == NULL) {
1279                 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1280                 return -1;
1281         }
1282
1283         os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1284         wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1285                     wps->nonce_e, WPS_NONCE_LEN);
1286
1287         return 0;
1288 }
1289
1290
1291 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1292 {
1293         if (r_nonce == NULL) {
1294                 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1295                 return -1;
1296         }
1297
1298         if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1299                 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1300                 return -1;
1301         }
1302
1303         return 0;
1304 }
1305
1306
1307 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1308 {
1309         if (uuid_e == NULL) {
1310                 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1311                 return -1;
1312         }
1313
1314         os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1315         wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1316
1317         return 0;
1318 }
1319
1320
1321 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1322 {
1323         if (pw_id == NULL) {
1324                 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1325                 return -1;
1326         }
1327
1328         wps->dev_pw_id = WPA_GET_BE16(pw_id);
1329         wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1330
1331         return 0;
1332 }
1333
1334
1335 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1336 {
1337         if (e_hash1 == NULL) {
1338                 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1339                 return -1;
1340         }
1341
1342         os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1343         wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1344
1345         return 0;
1346 }
1347
1348
1349 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1350 {
1351         if (e_hash2 == NULL) {
1352                 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1353                 return -1;
1354         }
1355
1356         os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1357         wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1358
1359         return 0;
1360 }
1361
1362
1363 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1364 {
1365         u8 hash[SHA256_MAC_LEN];
1366         const u8 *addr[4];
1367         size_t len[4];
1368
1369         if (e_snonce1 == NULL) {
1370                 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1371                 return -1;
1372         }
1373
1374         wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1375                         WPS_SECRET_NONCE_LEN);
1376
1377         /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1378         addr[0] = e_snonce1;
1379         len[0] = WPS_SECRET_NONCE_LEN;
1380         addr[1] = wps->psk1;
1381         len[1] = WPS_PSK_LEN;
1382         addr[2] = wpabuf_head(wps->dh_pubkey_e);
1383         len[2] = wpabuf_len(wps->dh_pubkey_e);
1384         addr[3] = wpabuf_head(wps->dh_pubkey_r);
1385         len[3] = wpabuf_len(wps->dh_pubkey_r);
1386         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1387
1388         if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1389                 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1390                            "not match with the pre-committed value");
1391                 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1392                 return -1;
1393         }
1394
1395         wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1396                    "half of the device password");
1397
1398         return 0;
1399 }
1400
1401
1402 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1403 {
1404         u8 hash[SHA256_MAC_LEN];
1405         const u8 *addr[4];
1406         size_t len[4];
1407
1408         if (e_snonce2 == NULL) {
1409                 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1410                 return -1;
1411         }
1412
1413         wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1414                         WPS_SECRET_NONCE_LEN);
1415
1416         /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1417         addr[0] = e_snonce2;
1418         len[0] = WPS_SECRET_NONCE_LEN;
1419         addr[1] = wps->psk2;
1420         len[1] = WPS_PSK_LEN;
1421         addr[2] = wpabuf_head(wps->dh_pubkey_e);
1422         len[2] = wpabuf_len(wps->dh_pubkey_e);
1423         addr[3] = wpabuf_head(wps->dh_pubkey_r);
1424         len[3] = wpabuf_len(wps->dh_pubkey_r);
1425         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1426
1427         if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1428                 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1429                            "not match with the pre-committed value");
1430                 wps_registrar_invalidate_pin(wps->registrar, wps->uuid_e);
1431                 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1432                 return -1;
1433         }
1434
1435         wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1436                    "half of the device password");
1437         wps->wps_pin_revealed = 0;
1438         wps_registrar_unlock_pin(wps->registrar, wps->uuid_e);
1439
1440         return 0;
1441 }
1442
1443
1444 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1445 {
1446         if (mac_addr == NULL) {
1447                 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1448                 return -1;
1449         }
1450
1451         wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1452                    MAC2STR(mac_addr));
1453         os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1454         os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1455
1456         return 0;
1457 }
1458
1459
1460 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1461                               size_t pk_len)
1462 {
1463         if (pk == NULL || pk_len == 0) {
1464                 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1465                 return -1;
1466         }
1467
1468         wpabuf_free(wps->dh_pubkey_e);
1469         wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1470         if (wps->dh_pubkey_e == NULL)
1471                 return -1;
1472
1473         return 0;
1474 }
1475
1476
1477 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1478 {
1479         u16 auth_types;
1480
1481         if (auth == NULL) {
1482                 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1483                            "received");
1484                 return -1;
1485         }
1486
1487         auth_types = WPA_GET_BE16(auth);
1488
1489         wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1490                    auth_types);
1491         wps->auth_type = wps->wps->auth_types & auth_types;
1492         if (wps->auth_type == 0) {
1493                 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1494                            "authentication types (own 0x%x Enrollee 0x%x)",
1495                            wps->wps->auth_types, auth_types);
1496                 return -1;
1497         }
1498
1499         return 0;
1500 }
1501
1502
1503 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1504 {
1505         u16 encr_types;
1506
1507         if (encr == NULL) {
1508                 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1509                            "received");
1510                 return -1;
1511         }
1512
1513         encr_types = WPA_GET_BE16(encr);
1514
1515         wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1516                    encr_types);
1517         wps->encr_type = wps->wps->encr_types & encr_types;
1518         if (wps->encr_type == 0) {
1519                 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1520                            "encryption types");
1521                 return -1;
1522         }
1523
1524         return 0;
1525 }
1526
1527
1528 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1529 {
1530         if (conn == NULL) {
1531                 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1532                            "received");
1533                 return -1;
1534         }
1535
1536         wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1537                    *conn);
1538
1539         return 0;
1540 }
1541
1542
1543 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1544 {
1545         u16 m;
1546
1547         if (methods == NULL) {
1548                 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1549                 return -1;
1550         }
1551
1552         m = WPA_GET_BE16(methods);
1553
1554         wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1555
1556         return 0;
1557 }
1558
1559
1560 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1561 {
1562         if (state == NULL) {
1563                 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1564                            "received");
1565                 return -1;
1566         }
1567
1568         wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1569                    *state);
1570
1571         return 0;
1572 }
1573
1574
1575 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1576 {
1577         u16 a;
1578
1579         if (assoc == NULL) {
1580                 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1581                 return -1;
1582         }
1583
1584         a = WPA_GET_BE16(assoc);
1585         wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
1586
1587         return 0;
1588 }
1589
1590
1591 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
1592 {
1593         u16 e;
1594
1595         if (err == NULL) {
1596                 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
1597                 return -1;
1598         }
1599
1600         e = WPA_GET_BE16(err);
1601         wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
1602
1603         return 0;
1604 }
1605
1606
1607 static enum wps_process_res wps_process_m1(struct wps_data *wps,
1608                                            struct wps_parse_attr *attr)
1609 {
1610         wpa_printf(MSG_DEBUG, "WPS: Received M1");
1611
1612         if (wps->state != RECV_M1) {
1613                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1614                            "receiving M1", wps->state);
1615                 return WPS_FAILURE;
1616         }
1617
1618         if (wps_process_uuid_e(wps, attr->uuid_e) ||
1619             wps_process_mac_addr(wps, attr->mac_addr) ||
1620             wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1621             wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
1622             wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
1623             wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
1624             wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
1625             wps_process_config_methods(wps, attr->config_methods) ||
1626             wps_process_wps_state(wps, attr->wps_state) ||
1627             wps_process_device_attrs(&wps->peer_dev, attr) ||
1628             wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
1629             wps_process_assoc_state(wps, attr->assoc_state) ||
1630             wps_process_dev_password_id(wps, attr->dev_password_id) ||
1631             wps_process_config_error(wps, attr->config_error) ||
1632             wps_process_os_version(&wps->peer_dev, attr->os_version))
1633                 return WPS_FAILURE;
1634
1635         if (wps->dev_pw_id != DEV_PW_DEFAULT &&
1636             wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
1637             wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
1638             wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
1639             (wps->dev_pw_id != DEV_PW_PUSHBUTTON || !wps->registrar->pbc)) {
1640                 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
1641                            wps->dev_pw_id);
1642                 wps->state = SEND_M2D;
1643                 return WPS_CONTINUE;
1644         }
1645
1646         if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
1647                 if (wps_registrar_pbc_overlap(wps->registrar, wps->mac_addr_e,
1648                                               wps->uuid_e)) {
1649                         wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
1650                                    "negotiation");
1651                         wps->state = SEND_M2D;
1652                         return WPS_CONTINUE;
1653                 }
1654                 wps_registrar_add_pbc_session(wps->registrar, wps->mac_addr_e,
1655                                               wps->uuid_e);
1656                 wps->pbc = 1;
1657         }
1658
1659         wps->state = SEND_M2;
1660         return WPS_CONTINUE;
1661 }
1662
1663
1664 static enum wps_process_res wps_process_m3(struct wps_data *wps,
1665                                            const struct wpabuf *msg,
1666                                            struct wps_parse_attr *attr)
1667 {
1668         wpa_printf(MSG_DEBUG, "WPS: Received M3");
1669
1670         if (wps->state != RECV_M3) {
1671                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1672                            "receiving M3", wps->state);
1673                 wps->state = SEND_WSC_NACK;
1674                 return WPS_CONTINUE;
1675         }
1676
1677         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1678             wps_process_authenticator(wps, attr->authenticator, msg) ||
1679             wps_process_e_hash1(wps, attr->e_hash1) ||
1680             wps_process_e_hash2(wps, attr->e_hash2)) {
1681                 wps->state = SEND_WSC_NACK;
1682                 return WPS_CONTINUE;
1683         }
1684
1685         wps->state = SEND_M4;
1686         return WPS_CONTINUE;
1687 }
1688
1689
1690 static enum wps_process_res wps_process_m5(struct wps_data *wps,
1691                                            const struct wpabuf *msg,
1692                                            struct wps_parse_attr *attr)
1693 {
1694         struct wpabuf *decrypted;
1695         struct wps_parse_attr eattr;
1696
1697         wpa_printf(MSG_DEBUG, "WPS: Received M5");
1698
1699         if (wps->state != RECV_M5) {
1700                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1701                            "receiving M5", wps->state);
1702                 wps->state = SEND_WSC_NACK;
1703                 return WPS_CONTINUE;
1704         }
1705
1706         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1707             wps_process_authenticator(wps, attr->authenticator, msg)) {
1708                 wps->state = SEND_WSC_NACK;
1709                 return WPS_CONTINUE;
1710         }
1711
1712         decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1713                                               attr->encr_settings_len);
1714         if (decrypted == NULL) {
1715                 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1716                            "Settings attribute");
1717                 wps->state = SEND_WSC_NACK;
1718                 return WPS_CONTINUE;
1719         }
1720
1721         wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1722                    "attribute");
1723         if (wps_parse_msg(decrypted, &eattr) < 0 ||
1724             wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1725             wps_process_e_snonce1(wps, eattr.e_snonce1)) {
1726                 wpabuf_free(decrypted);
1727                 wps->state = SEND_WSC_NACK;
1728                 return WPS_CONTINUE;
1729         }
1730         wpabuf_free(decrypted);
1731
1732         wps->state = SEND_M6;
1733         return WPS_CONTINUE;
1734 }
1735
1736
1737 static int wps_process_ap_settings_r(struct wps_data *wps,
1738                                      struct wps_parse_attr *attr)
1739 {
1740         if (wps->wps->ap)
1741                 return 0;
1742
1743         /* AP Settings Attributes in M7 when Enrollee is an AP */
1744         if (wps_process_ap_settings(attr, &wps->cred) < 0)
1745                 return -1;
1746
1747         wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
1748
1749         /*
1750          * TODO: Provide access to AP settings and allow changes before sending
1751          * out M8. For now, just copy the settings unchanged into M8.
1752          */
1753
1754         return 0;
1755 }
1756
1757
1758 static enum wps_process_res wps_process_m7(struct wps_data *wps,
1759                                            const struct wpabuf *msg,
1760                                            struct wps_parse_attr *attr)
1761 {
1762         struct wpabuf *decrypted;
1763         struct wps_parse_attr eattr;
1764
1765         wpa_printf(MSG_DEBUG, "WPS: Received M7");
1766
1767         if (wps->state != RECV_M7) {
1768                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1769                            "receiving M7", wps->state);
1770                 wps->state = SEND_WSC_NACK;
1771                 return WPS_CONTINUE;
1772         }
1773
1774         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1775             wps_process_authenticator(wps, attr->authenticator, msg)) {
1776                 wps->state = SEND_WSC_NACK;
1777                 return WPS_CONTINUE;
1778         }
1779
1780         decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1781                                               attr->encr_settings_len);
1782         if (decrypted == NULL) {
1783                 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1784                            "Settings attribute");
1785                 wps->state = SEND_WSC_NACK;
1786                 return WPS_CONTINUE;
1787         }
1788
1789         wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1790                    "attribute");
1791         if (wps_parse_msg(decrypted, &eattr) < 0 ||
1792             wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1793             wps_process_e_snonce2(wps, eattr.e_snonce2) ||
1794             wps_process_ap_settings_r(wps, &eattr)) {
1795                 wpabuf_free(decrypted);
1796                 wps->state = SEND_WSC_NACK;
1797                 return WPS_CONTINUE;
1798         }
1799
1800         wpabuf_free(decrypted);
1801
1802         wps->state = SEND_M8;
1803         return WPS_CONTINUE;
1804 }
1805
1806
1807 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1808                                                 const struct wpabuf *msg)
1809 {
1810         struct wps_parse_attr attr;
1811         enum wps_process_res ret = WPS_CONTINUE;
1812
1813         wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1814
1815         if (wps_parse_msg(msg, &attr) < 0)
1816                 return WPS_FAILURE;
1817
1818         if (attr.version == NULL || *attr.version != WPS_VERSION) {
1819                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1820                            attr.version ? *attr.version : 0);
1821                 return WPS_FAILURE;
1822         }
1823
1824         if (attr.msg_type == NULL) {
1825                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1826                 return WPS_FAILURE;
1827         }
1828
1829         if (*attr.msg_type != WPS_M1 &&
1830             (attr.registrar_nonce == NULL ||
1831              os_memcmp(wps->nonce_r, attr.registrar_nonce,
1832                        WPS_NONCE_LEN != 0))) {
1833                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1834                 return WPS_FAILURE;
1835         }
1836
1837         switch (*attr.msg_type) {
1838         case WPS_M1:
1839                 ret = wps_process_m1(wps, &attr);
1840                 break;
1841         case WPS_M3:
1842                 ret = wps_process_m3(wps, msg, &attr);
1843                 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1844                         wps_fail_event(wps->wps, WPS_M3);
1845                 break;
1846         case WPS_M5:
1847                 ret = wps_process_m5(wps, msg, &attr);
1848                 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1849                         wps_fail_event(wps->wps, WPS_M5);
1850                 break;
1851         case WPS_M7:
1852                 ret = wps_process_m7(wps, msg, &attr);
1853                 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1854                         wps_fail_event(wps->wps, WPS_M7);
1855                 break;
1856         default:
1857                 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1858                            *attr.msg_type);
1859                 return WPS_FAILURE;
1860         }
1861
1862         if (ret == WPS_CONTINUE) {
1863                 /* Save a copy of the last message for Authenticator derivation
1864                  */
1865                 wpabuf_free(wps->last_msg);
1866                 wps->last_msg = wpabuf_dup(msg);
1867         }
1868
1869         return ret;
1870 }
1871
1872
1873 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1874                                                 const struct wpabuf *msg)
1875 {
1876         struct wps_parse_attr attr;
1877
1878         wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1879
1880         if (wps_parse_msg(msg, &attr) < 0)
1881                 return WPS_FAILURE;
1882
1883         if (attr.version == NULL || *attr.version != WPS_VERSION) {
1884                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1885                            attr.version ? *attr.version : 0);
1886                 return WPS_FAILURE;
1887         }
1888
1889         if (attr.msg_type == NULL) {
1890                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1891                 return WPS_FAILURE;
1892         }
1893
1894         if (*attr.msg_type != WPS_WSC_ACK) {
1895                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1896                            *attr.msg_type);
1897                 return WPS_FAILURE;
1898         }
1899
1900         if (attr.registrar_nonce == NULL ||
1901             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1902         {
1903                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1904                 return WPS_FAILURE;
1905         }
1906
1907         if (attr.enrollee_nonce == NULL ||
1908             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1909                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1910                 return WPS_FAILURE;
1911         }
1912
1913         if (wps->state == RECV_M2D_ACK) {
1914                 /* TODO: support for multiple registrars and sending of
1915                  * multiple M2/M2D messages */
1916
1917                 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
1918                            "terminate negotiation");
1919         }
1920
1921         return WPS_FAILURE;
1922 }
1923
1924
1925 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1926                                                  const struct wpabuf *msg)
1927 {
1928         struct wps_parse_attr attr;
1929         int old_state;
1930
1931         wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1932
1933         old_state = wps->state;
1934         wps->state = SEND_WSC_NACK;
1935
1936         if (wps_parse_msg(msg, &attr) < 0)
1937                 return WPS_FAILURE;
1938
1939         if (attr.version == NULL || *attr.version != WPS_VERSION) {
1940                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1941                            attr.version ? *attr.version : 0);
1942                 return WPS_FAILURE;
1943         }
1944
1945         if (attr.msg_type == NULL) {
1946                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1947                 return WPS_FAILURE;
1948         }
1949
1950         if (*attr.msg_type != WPS_WSC_NACK) {
1951                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1952                            *attr.msg_type);
1953                 return WPS_FAILURE;
1954         }
1955
1956         if (attr.registrar_nonce == NULL ||
1957             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1958         {
1959                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1960                 return WPS_FAILURE;
1961         }
1962
1963         if (attr.enrollee_nonce == NULL ||
1964             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1965                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1966                 return WPS_FAILURE;
1967         }
1968
1969         if (attr.config_error == NULL) {
1970                 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1971                            "in WSC_NACK");
1972                 return WPS_FAILURE;
1973         }
1974
1975         wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
1976                    "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1977
1978         switch (old_state) {
1979         case RECV_M3:
1980                 wps_fail_event(wps->wps, WPS_M2);
1981                 break;
1982         case RECV_M5:
1983                 wps_fail_event(wps->wps, WPS_M4);
1984                 break;
1985         case RECV_M7:
1986                 wps_fail_event(wps->wps, WPS_M6);
1987                 break;
1988         case RECV_DONE:
1989                 wps_fail_event(wps->wps, WPS_M8);
1990                 break;
1991         default:
1992                 break;
1993         }
1994
1995         return WPS_FAILURE;
1996 }
1997
1998
1999 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2000                                                  const struct wpabuf *msg)
2001 {
2002         struct wps_parse_attr attr;
2003
2004         wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2005
2006         if (wps->state != RECV_DONE) {
2007                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2008                            "receiving WSC_Done", wps->state);
2009                 return WPS_FAILURE;
2010         }
2011
2012         if (wps_parse_msg(msg, &attr) < 0)
2013                 return WPS_FAILURE;
2014
2015         if (attr.version == NULL || *attr.version != WPS_VERSION) {
2016                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2017                            attr.version ? *attr.version : 0);
2018                 return WPS_FAILURE;
2019         }
2020
2021         if (attr.msg_type == NULL) {
2022                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2023                 return WPS_FAILURE;
2024         }
2025
2026         if (*attr.msg_type != WPS_WSC_DONE) {
2027                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2028                            *attr.msg_type);
2029                 return WPS_FAILURE;
2030         }
2031
2032         if (attr.registrar_nonce == NULL ||
2033             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2034         {
2035                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2036                 return WPS_FAILURE;
2037         }
2038
2039         if (attr.enrollee_nonce == NULL ||
2040             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2041                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2042                 return WPS_FAILURE;
2043         }
2044
2045         wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2046
2047         if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2048             wps->wps->ap) {
2049                 struct wps_credential cred;
2050
2051                 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2052                            "on first Enrollee connection");
2053
2054                 os_memset(&cred, 0, sizeof(cred));
2055                 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2056                 cred.ssid_len = wps->wps->ssid_len;
2057                 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2058                 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2059                 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2060                 cred.key_len = wps->new_psk_len;
2061
2062                 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2063                 wpa_hexdump_ascii_key(MSG_DEBUG,
2064                                       "WPS: Generated random passphrase",
2065                                       wps->new_psk, wps->new_psk_len);
2066                 if (wps->wps->cred_cb)
2067                         wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2068
2069                 os_free(wps->new_psk);
2070                 wps->new_psk = NULL;
2071         }
2072
2073         if (!wps->wps->ap) {
2074                 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based "
2075                            "on the modified AP configuration");
2076                 if (wps->wps->cred_cb)
2077                         wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2078         }
2079
2080         if (wps->new_psk) {
2081                 if (wps_cb_new_psk(wps->registrar, wps->mac_addr_e,
2082                                    wps->new_psk, wps->new_psk_len)) {
2083                         wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2084                                    "new PSK");
2085                 }
2086                 os_free(wps->new_psk);
2087                 wps->new_psk = NULL;
2088         }
2089
2090         if (wps->pbc) {
2091                 wps_registrar_remove_pbc_session(wps->registrar,
2092                                                  wps->mac_addr_e, wps->uuid_e);
2093                 wps_registrar_pbc_completed(wps->registrar);
2094         }
2095
2096         wps_success_event(wps->wps);
2097
2098         return WPS_DONE;
2099 }
2100
2101
2102 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2103                                                enum wsc_op_code op_code,
2104                                                const struct wpabuf *msg)
2105 {
2106         enum wps_process_res ret;
2107
2108         wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2109                    "op_code=%d)",
2110                    (unsigned long) wpabuf_len(msg), op_code);
2111
2112         switch (op_code) {
2113         case WSC_MSG:
2114                 return wps_process_wsc_msg(wps, msg);
2115         case WSC_ACK:
2116                 return wps_process_wsc_ack(wps, msg);
2117         case WSC_NACK:
2118                 return wps_process_wsc_nack(wps, msg);
2119         case WSC_Done:
2120                 ret = wps_process_wsc_done(wps, msg);
2121                 if (ret == WPS_FAILURE) {
2122                         wps->state = SEND_WSC_NACK;
2123                         wps_fail_event(wps->wps, WPS_WSC_DONE);
2124                 }
2125                 return ret;
2126         default:
2127                 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
2128                 return WPS_FAILURE;
2129         }
2130 }