cd171f0934a0749149bf3e53705c0e47b8502d50
[libeap.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 static 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->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->wps->registrar, wps->uuid_e,
695                                   &wps->peer_dev);
696                 return -1;
697         }
698
699         wps->dev_password = os_malloc(pin_len);
700         if (wps->dev_password == NULL)
701                 return -1;
702         os_memcpy(wps->dev_password, pin, pin_len);
703         wps->dev_password_len = pin_len;
704
705         return 0;
706 }
707
708
709 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
710 {
711         wpa_printf(MSG_DEBUG, "WPS:  * UUID-R");
712         wpabuf_put_be16(msg, ATTR_UUID_R);
713         wpabuf_put_be16(msg, WPS_UUID_LEN);
714         wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
715         return 0;
716 }
717
718
719 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
720 {
721         u8 *hash;
722         const u8 *addr[4];
723         size_t len[4];
724
725         if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
726                 return -1;
727         wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
728         wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
729                     wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
730
731         if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
732                 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
733                            "R-Hash derivation");
734                 return -1;
735         }
736
737         wpa_printf(MSG_DEBUG, "WPS:  * R-Hash1");
738         wpabuf_put_be16(msg, ATTR_R_HASH1);
739         wpabuf_put_be16(msg, SHA256_MAC_LEN);
740         hash = wpabuf_put(msg, SHA256_MAC_LEN);
741         /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
742         addr[0] = wps->snonce;
743         len[0] = WPS_SECRET_NONCE_LEN;
744         addr[1] = wps->psk1;
745         len[1] = WPS_PSK_LEN;
746         addr[2] = wpabuf_head(wps->dh_pubkey_e);
747         len[2] = wpabuf_len(wps->dh_pubkey_e);
748         addr[3] = wpabuf_head(wps->dh_pubkey_r);
749         len[3] = wpabuf_len(wps->dh_pubkey_r);
750         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
751         wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
752
753         wpa_printf(MSG_DEBUG, "WPS:  * R-Hash2");
754         wpabuf_put_be16(msg, ATTR_R_HASH2);
755         wpabuf_put_be16(msg, SHA256_MAC_LEN);
756         hash = wpabuf_put(msg, SHA256_MAC_LEN);
757         /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
758         addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
759         addr[1] = wps->psk2;
760         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
761         wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
762
763         return 0;
764 }
765
766
767 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
768 {
769         wpa_printf(MSG_DEBUG, "WPS:  * R-SNonce1");
770         wpabuf_put_be16(msg, ATTR_R_SNONCE1);
771         wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
772         wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
773         return 0;
774 }
775
776
777 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
778 {
779         wpa_printf(MSG_DEBUG, "WPS:  * R-SNonce2");
780         wpabuf_put_be16(msg, ATTR_R_SNONCE2);
781         wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
782         wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
783                         WPS_SECRET_NONCE_LEN);
784         return 0;
785 }
786
787
788 static int wps_build_cred_network_idx(struct wpabuf *msg,
789                                       struct wps_credential *cred)
790 {
791         wpa_printf(MSG_DEBUG, "WPS:  * Network Index");
792         wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
793         wpabuf_put_be16(msg, 1);
794         wpabuf_put_u8(msg, 0);
795         return 0;
796 }
797
798
799 static int wps_build_cred_ssid(struct wpabuf *msg,
800                                struct wps_credential *cred)
801 {
802         wpa_printf(MSG_DEBUG, "WPS:  * SSID");
803         wpabuf_put_be16(msg, ATTR_SSID);
804         wpabuf_put_be16(msg, cred->ssid_len);
805         wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
806         return 0;
807 }
808
809
810 static int wps_build_cred_auth_type(struct wpabuf *msg,
811                                     struct wps_credential *cred)
812 {
813         wpa_printf(MSG_DEBUG, "WPS:  * Authentication Type (0x%x)",
814                    cred->auth_type);
815         wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
816         wpabuf_put_be16(msg, 2);
817         wpabuf_put_be16(msg, cred->auth_type);
818         return 0;
819 }
820
821
822 static int wps_build_cred_encr_type(struct wpabuf *msg,
823                                     struct wps_credential *cred)
824 {
825         wpa_printf(MSG_DEBUG, "WPS:  * Encryption Type (0x%x)",
826                    cred->encr_type);
827         wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
828         wpabuf_put_be16(msg, 2);
829         wpabuf_put_be16(msg, cred->encr_type);
830         return 0;
831 }
832
833
834 static int wps_build_cred_network_key(struct wpabuf *msg,
835                                       struct wps_credential *cred)
836 {
837         wpa_printf(MSG_DEBUG, "WPS:  * Network Key");
838         wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
839         wpabuf_put_be16(msg, cred->key_len);
840         wpabuf_put_data(msg, cred->key, cred->key_len);
841         return 0;
842 }
843
844
845 static int wps_build_cred_mac_addr(struct wpabuf *msg,
846                                    struct wps_credential *cred)
847 {
848         wpa_printf(MSG_DEBUG, "WPS:  * MAC Address");
849         wpabuf_put_be16(msg, ATTR_MAC_ADDR);
850         wpabuf_put_be16(msg, ETH_ALEN);
851         wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
852         return 0;
853 }
854
855
856 static int wps_build_credential(struct wpabuf *msg,
857                                 struct wps_credential *cred)
858 {
859         if (wps_build_cred_network_idx(msg, cred) ||
860             wps_build_cred_ssid(msg, cred) ||
861             wps_build_cred_auth_type(msg, cred) ||
862             wps_build_cred_encr_type(msg, cred) ||
863             wps_build_cred_network_key(msg, cred) ||
864             wps_build_cred_mac_addr(msg, cred))
865                 return -1;
866         return 0;
867 }
868
869
870 static int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
871 {
872         struct wpabuf *cred;
873
874         wpa_printf(MSG_DEBUG, "WPS:  * Credential");
875         os_memset(&wps->cred, 0, sizeof(wps->cred));
876
877         os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
878         wps->cred.ssid_len = wps->wps->ssid_len;
879
880         /* Select the best authentication and encryption type */
881         if (wps->auth_type & WPS_AUTH_WPA2PSK)
882                 wps->auth_type = WPS_AUTH_WPA2PSK;
883         else if (wps->auth_type & WPS_AUTH_WPAPSK)
884                 wps->auth_type = WPS_AUTH_WPAPSK;
885         else if (wps->auth_type & WPS_AUTH_OPEN)
886                 wps->auth_type = WPS_AUTH_OPEN;
887         else if (wps->auth_type & WPS_AUTH_SHARED)
888                 wps->auth_type = WPS_AUTH_SHARED;
889         else {
890                 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
891                            wps->auth_type);
892                 return -1;
893         }
894         wps->cred.auth_type = wps->auth_type;
895
896         if (wps->auth_type == WPS_AUTH_WPA2PSK ||
897             wps->auth_type == WPS_AUTH_WPAPSK) {
898                 if (wps->encr_type & WPS_ENCR_AES)
899                         wps->encr_type = WPS_ENCR_AES;
900                 else if (wps->encr_type & WPS_ENCR_TKIP)
901                         wps->encr_type = WPS_ENCR_TKIP;
902                 else {
903                         wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
904                                    "type for WPA/WPA2");
905                         return -1;
906                 }
907         } else {
908                 if (wps->encr_type & WPS_ENCR_WEP)
909                         wps->encr_type = WPS_ENCR_WEP;
910                 else if (wps->encr_type & WPS_ENCR_NONE)
911                         wps->encr_type = WPS_ENCR_NONE;
912                 else {
913                         wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
914                                    "type for non-WPA/WPA2 mode");
915                         return -1;
916                 }
917         }
918         wps->cred.encr_type = wps->encr_type;
919         os_memcpy(wps->cred.mac_addr, wps->mac_addr_e, ETH_ALEN);
920
921         if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap) {
922                 u8 r[16];
923                 /* Generate a random passphrase */
924                 if (os_get_random(r, sizeof(r)) < 0)
925                         return -1;
926                 os_free(wps->new_psk);
927                 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
928                 if (wps->new_psk == NULL)
929                         return -1;
930                 wps->new_psk_len--; /* remove newline */
931                 while (wps->new_psk_len &&
932                        wps->new_psk[wps->new_psk_len - 1] == '=')
933                         wps->new_psk_len--;
934                 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
935                                       wps->new_psk, wps->new_psk_len);
936                 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
937                 wps->cred.key_len = wps->new_psk_len;
938         } else if (wps->wps->network_key) {
939                 os_memcpy(wps->cred.key, wps->wps->network_key,
940                           wps->wps->network_key_len);
941                 wps->cred.key_len = wps->wps->network_key_len;
942         } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
943                 char hex[65];
944                 /* Generate a random per-device PSK */
945                 os_free(wps->new_psk);
946                 wps->new_psk_len = 32;
947                 wps->new_psk = os_malloc(wps->new_psk_len);
948                 if (wps->new_psk == NULL)
949                         return -1;
950                 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
951                         os_free(wps->new_psk);
952                         wps->new_psk = NULL;
953                         return -1;
954                 }
955                 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
956                                 wps->new_psk, wps->new_psk_len);
957                 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
958                                  wps->new_psk_len);
959                 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
960                 wps->cred.key_len = wps->new_psk_len * 2;
961         }
962
963         cred = wpabuf_alloc(200);
964         if (cred == NULL)
965                 return -1;
966
967         if (wps_build_credential(cred, &wps->cred)) {
968                 wpabuf_free(cred);
969                 return -1;
970         }
971
972         wpabuf_put_be16(msg, ATTR_CRED);
973         wpabuf_put_be16(msg, wpabuf_len(cred));
974         wpabuf_put_buf(msg, cred);
975         wpabuf_free(cred);
976
977         return 0;
978 }
979
980
981 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
982 {
983         wpa_printf(MSG_DEBUG, "WPS:  * AP Settings");
984
985         if (wps_build_credential(msg, &wps->cred))
986                 return -1;
987
988         return 0;
989 }
990
991
992 static struct wpabuf * wps_build_m2(struct wps_data *wps)
993 {
994         struct wpabuf *msg;
995
996         if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
997                 return NULL;
998         wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
999                     wps->nonce_r, WPS_NONCE_LEN);
1000         wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1001
1002         wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1003         msg = wpabuf_alloc(1000);
1004         if (msg == NULL)
1005                 return NULL;
1006
1007         if (wps_build_version(msg) ||
1008             wps_build_msg_type(msg, WPS_M2) ||
1009             wps_build_enrollee_nonce(wps, msg) ||
1010             wps_build_registrar_nonce(wps, msg) ||
1011             wps_build_uuid_r(wps, msg) ||
1012             wps_build_public_key(wps, msg) ||
1013             wps_derive_keys(wps) ||
1014             wps_build_auth_type_flags(wps, msg) ||
1015             wps_build_encr_type_flags(wps, msg) ||
1016             wps_build_conn_type_flags(wps, msg) ||
1017             wps_build_config_methods_r(wps->wps->registrar, msg) ||
1018             wps_build_device_attrs(&wps->wps->dev, msg) ||
1019             wps_build_rf_bands(&wps->wps->dev, msg) ||
1020             wps_build_assoc_state(wps, msg) ||
1021             wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1022             wps_build_dev_password_id(msg, DEV_PW_DEFAULT) ||
1023             wps_build_os_version(&wps->wps->dev, msg) ||
1024             wps_build_authenticator(wps, msg)) {
1025                 wpabuf_free(msg);
1026                 return NULL;
1027         }
1028
1029         wps->state = RECV_M3;
1030         return msg;
1031 }
1032
1033
1034 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1035 {
1036         struct wpabuf *msg;
1037         u16 err = WPS_CFG_NO_ERROR;
1038
1039         wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1040         msg = wpabuf_alloc(1000);
1041         if (msg == NULL)
1042                 return NULL;
1043
1044         if (wps->wps->ap && wps->wps->ap_setup_locked)
1045                 err = WPS_CFG_SETUP_LOCKED;
1046
1047         if (wps_build_version(msg) ||
1048             wps_build_msg_type(msg, WPS_M2D) ||
1049             wps_build_enrollee_nonce(wps, msg) ||
1050             wps_build_registrar_nonce(wps, msg) ||
1051             wps_build_uuid_r(wps, msg) ||
1052             wps_build_auth_type_flags(wps, msg) ||
1053             wps_build_encr_type_flags(wps, msg) ||
1054             wps_build_conn_type_flags(wps, msg) ||
1055             wps_build_config_methods_r(wps->wps->registrar, msg) ||
1056             wps_build_device_attrs(&wps->wps->dev, msg) ||
1057             wps_build_rf_bands(&wps->wps->dev, msg) ||
1058             wps_build_assoc_state(wps, msg) ||
1059             wps_build_config_error(msg, err) ||
1060             wps_build_os_version(&wps->wps->dev, msg)) {
1061                 wpabuf_free(msg);
1062                 return NULL;
1063         }
1064
1065         wps->state = RECV_M2D_ACK;
1066         return msg;
1067 }
1068
1069
1070 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1071 {
1072         struct wpabuf *msg, *plain;
1073
1074         wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1075
1076         wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1077
1078         plain = wpabuf_alloc(200);
1079         if (plain == NULL)
1080                 return NULL;
1081
1082         msg = wpabuf_alloc(1000);
1083         if (msg == NULL) {
1084                 wpabuf_free(plain);
1085                 return NULL;
1086         }
1087
1088         if (wps_build_version(msg) ||
1089             wps_build_msg_type(msg, WPS_M4) ||
1090             wps_build_enrollee_nonce(wps, msg) ||
1091             wps_build_r_hash(wps, msg) ||
1092             wps_build_r_snonce1(wps, plain) ||
1093             wps_build_key_wrap_auth(wps, plain) ||
1094             wps_build_encr_settings(wps, msg, plain) ||
1095             wps_build_authenticator(wps, msg)) {
1096                 wpabuf_free(plain);
1097                 wpabuf_free(msg);
1098                 return NULL;
1099         }
1100         wpabuf_free(plain);
1101
1102         wps->state = RECV_M5;
1103         return msg;
1104 }
1105
1106
1107 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1108 {
1109         struct wpabuf *msg, *plain;
1110
1111         wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1112
1113         plain = wpabuf_alloc(200);
1114         if (plain == NULL)
1115                 return NULL;
1116
1117         msg = wpabuf_alloc(1000);
1118         if (msg == NULL) {
1119                 wpabuf_free(plain);
1120                 return NULL;
1121         }
1122
1123         if (wps_build_version(msg) ||
1124             wps_build_msg_type(msg, WPS_M6) ||
1125             wps_build_enrollee_nonce(wps, msg) ||
1126             wps_build_r_snonce2(wps, plain) ||
1127             wps_build_key_wrap_auth(wps, plain) ||
1128             wps_build_encr_settings(wps, msg, plain) ||
1129             wps_build_authenticator(wps, msg)) {
1130                 wpabuf_free(plain);
1131                 wpabuf_free(msg);
1132                 return NULL;
1133         }
1134         wpabuf_free(plain);
1135
1136         wps->wps_pin_revealed = 1;
1137         wps->state = RECV_M7;
1138         return msg;
1139 }
1140
1141
1142 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1143 {
1144         struct wpabuf *msg, *plain;
1145
1146         wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1147
1148         plain = wpabuf_alloc(500);
1149         if (plain == NULL)
1150                 return NULL;
1151
1152         msg = wpabuf_alloc(1000);
1153         if (msg == NULL) {
1154                 wpabuf_free(plain);
1155                 return NULL;
1156         }
1157
1158         if (wps_build_version(msg) ||
1159             wps_build_msg_type(msg, WPS_M8) ||
1160             wps_build_enrollee_nonce(wps, msg) ||
1161             (wps->wps->ap && wps_build_cred(wps, plain)) ||
1162             (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
1163             wps_build_key_wrap_auth(wps, plain) ||
1164             wps_build_encr_settings(wps, msg, plain) ||
1165             wps_build_authenticator(wps, msg)) {
1166                 wpabuf_free(plain);
1167                 wpabuf_free(msg);
1168                 return NULL;
1169         }
1170         wpabuf_free(plain);
1171
1172         wps->state = RECV_DONE;
1173         return msg;
1174 }
1175
1176
1177 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1178 {
1179         struct wpabuf *msg;
1180
1181         wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1182
1183         msg = wpabuf_alloc(1000);
1184         if (msg == NULL)
1185                 return NULL;
1186
1187         if (wps_build_version(msg) ||
1188             wps_build_msg_type(msg, WPS_WSC_ACK) ||
1189             wps_build_enrollee_nonce(wps, msg) ||
1190             wps_build_registrar_nonce(wps, msg)) {
1191                 wpabuf_free(msg);
1192                 return NULL;
1193         }
1194
1195         return msg;
1196 }
1197
1198
1199 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1200 {
1201         struct wpabuf *msg;
1202
1203         wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1204
1205         msg = wpabuf_alloc(1000);
1206         if (msg == NULL)
1207                 return NULL;
1208
1209         if (wps_build_version(msg) ||
1210             wps_build_msg_type(msg, WPS_WSC_NACK) ||
1211             wps_build_enrollee_nonce(wps, msg) ||
1212             wps_build_registrar_nonce(wps, msg) ||
1213             wps_build_config_error(msg, wps->config_error)) {
1214                 wpabuf_free(msg);
1215                 return NULL;
1216         }
1217
1218         return msg;
1219 }
1220
1221
1222 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1223                                       enum wsc_op_code *op_code)
1224 {
1225         struct wpabuf *msg;
1226
1227         switch (wps->state) {
1228         case SEND_M2:
1229                 if (wps_get_dev_password(wps) < 0)
1230                         msg = wps_build_m2d(wps);
1231                 else
1232                         msg = wps_build_m2(wps);
1233                 *op_code = WSC_MSG;
1234                 break;
1235         case SEND_M2D:
1236                 msg = wps_build_m2d(wps);
1237                 *op_code = WSC_MSG;
1238                 break;
1239         case SEND_M4:
1240                 msg = wps_build_m4(wps);
1241                 *op_code = WSC_MSG;
1242                 break;
1243         case SEND_M6:
1244                 msg = wps_build_m6(wps);
1245                 *op_code = WSC_MSG;
1246                 break;
1247         case SEND_M8:
1248                 msg = wps_build_m8(wps);
1249                 *op_code = WSC_MSG;
1250                 break;
1251         case RECV_DONE:
1252                 msg = wps_build_wsc_ack(wps);
1253                 *op_code = WSC_ACK;
1254                 break;
1255         case SEND_WSC_NACK:
1256                 msg = wps_build_wsc_nack(wps);
1257                 *op_code = WSC_NACK;
1258                 break;
1259         default:
1260                 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1261                            "a message", wps->state);
1262                 msg = NULL;
1263                 break;
1264         }
1265
1266         if (*op_code == WSC_MSG && msg) {
1267                 /* Save a copy of the last message for Authenticator derivation
1268                  */
1269                 wpabuf_free(wps->last_msg);
1270                 wps->last_msg = wpabuf_dup(msg);
1271         }
1272
1273         return msg;
1274 }
1275
1276
1277 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1278 {
1279         if (e_nonce == NULL) {
1280                 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1281                 return -1;
1282         }
1283
1284         os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1285         wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1286                     wps->nonce_e, WPS_NONCE_LEN);
1287
1288         return 0;
1289 }
1290
1291
1292 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1293 {
1294         if (r_nonce == NULL) {
1295                 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1296                 return -1;
1297         }
1298
1299         if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1300                 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1301                 return -1;
1302         }
1303
1304         return 0;
1305 }
1306
1307
1308 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1309 {
1310         if (uuid_e == NULL) {
1311                 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1312                 return -1;
1313         }
1314
1315         os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1316         wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1317
1318         return 0;
1319 }
1320
1321
1322 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1323 {
1324         if (pw_id == NULL) {
1325                 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1326                 return -1;
1327         }
1328
1329         wps->dev_pw_id = WPA_GET_BE16(pw_id);
1330         wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1331
1332         return 0;
1333 }
1334
1335
1336 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1337 {
1338         if (e_hash1 == NULL) {
1339                 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1340                 return -1;
1341         }
1342
1343         os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1344         wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1345
1346         return 0;
1347 }
1348
1349
1350 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1351 {
1352         if (e_hash2 == NULL) {
1353                 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1354                 return -1;
1355         }
1356
1357         os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1358         wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1359
1360         return 0;
1361 }
1362
1363
1364 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1365 {
1366         u8 hash[SHA256_MAC_LEN];
1367         const u8 *addr[4];
1368         size_t len[4];
1369
1370         if (e_snonce1 == NULL) {
1371                 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1372                 return -1;
1373         }
1374
1375         wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1376                         WPS_SECRET_NONCE_LEN);
1377
1378         /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1379         addr[0] = e_snonce1;
1380         len[0] = WPS_SECRET_NONCE_LEN;
1381         addr[1] = wps->psk1;
1382         len[1] = WPS_PSK_LEN;
1383         addr[2] = wpabuf_head(wps->dh_pubkey_e);
1384         len[2] = wpabuf_len(wps->dh_pubkey_e);
1385         addr[3] = wpabuf_head(wps->dh_pubkey_r);
1386         len[3] = wpabuf_len(wps->dh_pubkey_r);
1387         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1388
1389         if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1390                 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1391                            "not match with the pre-committed value");
1392                 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1393                 return -1;
1394         }
1395
1396         wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1397                    "half of the device password");
1398
1399         return 0;
1400 }
1401
1402
1403 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1404 {
1405         u8 hash[SHA256_MAC_LEN];
1406         const u8 *addr[4];
1407         size_t len[4];
1408
1409         if (e_snonce2 == NULL) {
1410                 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1411                 return -1;
1412         }
1413
1414         wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1415                         WPS_SECRET_NONCE_LEN);
1416
1417         /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1418         addr[0] = e_snonce2;
1419         len[0] = WPS_SECRET_NONCE_LEN;
1420         addr[1] = wps->psk2;
1421         len[1] = WPS_PSK_LEN;
1422         addr[2] = wpabuf_head(wps->dh_pubkey_e);
1423         len[2] = wpabuf_len(wps->dh_pubkey_e);
1424         addr[3] = wpabuf_head(wps->dh_pubkey_r);
1425         len[3] = wpabuf_len(wps->dh_pubkey_r);
1426         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1427
1428         if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1429                 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1430                            "not match with the pre-committed value");
1431                 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
1432                 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1433                 return -1;
1434         }
1435
1436         wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1437                    "half of the device password");
1438         wps->wps_pin_revealed = 0;
1439         wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
1440
1441         return 0;
1442 }
1443
1444
1445 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1446 {
1447         if (mac_addr == NULL) {
1448                 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1449                 return -1;
1450         }
1451
1452         wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1453                    MAC2STR(mac_addr));
1454         os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1455         os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1456
1457         return 0;
1458 }
1459
1460
1461 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1462                               size_t pk_len)
1463 {
1464         if (pk == NULL || pk_len == 0) {
1465                 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1466                 return -1;
1467         }
1468
1469         wpabuf_free(wps->dh_pubkey_e);
1470         wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1471         if (wps->dh_pubkey_e == NULL)
1472                 return -1;
1473
1474         return 0;
1475 }
1476
1477
1478 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1479 {
1480         u16 auth_types;
1481
1482         if (auth == NULL) {
1483                 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1484                            "received");
1485                 return -1;
1486         }
1487
1488         auth_types = WPA_GET_BE16(auth);
1489
1490         wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1491                    auth_types);
1492         wps->auth_type = wps->wps->auth_types & auth_types;
1493         if (wps->auth_type == 0) {
1494                 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1495                            "authentication types (own 0x%x Enrollee 0x%x)",
1496                            wps->wps->auth_types, auth_types);
1497                 return -1;
1498         }
1499
1500         return 0;
1501 }
1502
1503
1504 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1505 {
1506         u16 encr_types;
1507
1508         if (encr == NULL) {
1509                 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1510                            "received");
1511                 return -1;
1512         }
1513
1514         encr_types = WPA_GET_BE16(encr);
1515
1516         wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1517                    encr_types);
1518         wps->encr_type = wps->wps->encr_types & encr_types;
1519         if (wps->encr_type == 0) {
1520                 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1521                            "encryption types");
1522                 return -1;
1523         }
1524
1525         return 0;
1526 }
1527
1528
1529 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1530 {
1531         if (conn == NULL) {
1532                 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1533                            "received");
1534                 return -1;
1535         }
1536
1537         wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1538                    *conn);
1539
1540         return 0;
1541 }
1542
1543
1544 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1545 {
1546         u16 m;
1547
1548         if (methods == NULL) {
1549                 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1550                 return -1;
1551         }
1552
1553         m = WPA_GET_BE16(methods);
1554
1555         wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1556
1557         return 0;
1558 }
1559
1560
1561 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1562 {
1563         if (state == NULL) {
1564                 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1565                            "received");
1566                 return -1;
1567         }
1568
1569         wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1570                    *state);
1571
1572         return 0;
1573 }
1574
1575
1576 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1577 {
1578         u16 a;
1579
1580         if (assoc == NULL) {
1581                 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1582                 return -1;
1583         }
1584
1585         a = WPA_GET_BE16(assoc);
1586         wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
1587
1588         return 0;
1589 }
1590
1591
1592 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
1593 {
1594         u16 e;
1595
1596         if (err == NULL) {
1597                 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
1598                 return -1;
1599         }
1600
1601         e = WPA_GET_BE16(err);
1602         wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
1603
1604         return 0;
1605 }
1606
1607
1608 static enum wps_process_res wps_process_m1(struct wps_data *wps,
1609                                            struct wps_parse_attr *attr)
1610 {
1611         wpa_printf(MSG_DEBUG, "WPS: Received M1");
1612
1613         if (wps->state != RECV_M1) {
1614                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1615                            "receiving M1", wps->state);
1616                 return WPS_FAILURE;
1617         }
1618
1619         if (wps_process_uuid_e(wps, attr->uuid_e) ||
1620             wps_process_mac_addr(wps, attr->mac_addr) ||
1621             wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1622             wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
1623             wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
1624             wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
1625             wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
1626             wps_process_config_methods(wps, attr->config_methods) ||
1627             wps_process_wps_state(wps, attr->wps_state) ||
1628             wps_process_device_attrs(&wps->peer_dev, attr) ||
1629             wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
1630             wps_process_assoc_state(wps, attr->assoc_state) ||
1631             wps_process_dev_password_id(wps, attr->dev_password_id) ||
1632             wps_process_config_error(wps, attr->config_error) ||
1633             wps_process_os_version(&wps->peer_dev, attr->os_version))
1634                 return WPS_FAILURE;
1635
1636         if (wps->dev_pw_id != DEV_PW_DEFAULT &&
1637             wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
1638             wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
1639             wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
1640             (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
1641              !wps->wps->registrar->pbc)) {
1642                 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
1643                            wps->dev_pw_id);
1644                 wps->state = SEND_M2D;
1645                 return WPS_CONTINUE;
1646         }
1647
1648         if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
1649                 if (wps_registrar_pbc_overlap(wps->wps->registrar,
1650                                               wps->mac_addr_e, wps->uuid_e)) {
1651                         wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
1652                                    "negotiation");
1653                         wps->state = SEND_M2D;
1654                         return WPS_CONTINUE;
1655                 }
1656                 wps_registrar_add_pbc_session(wps->wps->registrar,
1657                                               wps->mac_addr_e, wps->uuid_e);
1658                 wps->pbc = 1;
1659         }
1660
1661         wps->state = SEND_M2;
1662         return WPS_CONTINUE;
1663 }
1664
1665
1666 static enum wps_process_res wps_process_m3(struct wps_data *wps,
1667                                            const struct wpabuf *msg,
1668                                            struct wps_parse_attr *attr)
1669 {
1670         wpa_printf(MSG_DEBUG, "WPS: Received M3");
1671
1672         if (wps->state != RECV_M3) {
1673                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1674                            "receiving M3", wps->state);
1675                 wps->state = SEND_WSC_NACK;
1676                 return WPS_CONTINUE;
1677         }
1678
1679         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1680             wps_process_authenticator(wps, attr->authenticator, msg) ||
1681             wps_process_e_hash1(wps, attr->e_hash1) ||
1682             wps_process_e_hash2(wps, attr->e_hash2)) {
1683                 wps->state = SEND_WSC_NACK;
1684                 return WPS_CONTINUE;
1685         }
1686
1687         wps->state = SEND_M4;
1688         return WPS_CONTINUE;
1689 }
1690
1691
1692 static enum wps_process_res wps_process_m5(struct wps_data *wps,
1693                                            const struct wpabuf *msg,
1694                                            struct wps_parse_attr *attr)
1695 {
1696         struct wpabuf *decrypted;
1697         struct wps_parse_attr eattr;
1698
1699         wpa_printf(MSG_DEBUG, "WPS: Received M5");
1700
1701         if (wps->state != RECV_M5) {
1702                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1703                            "receiving M5", wps->state);
1704                 wps->state = SEND_WSC_NACK;
1705                 return WPS_CONTINUE;
1706         }
1707
1708         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1709             wps_process_authenticator(wps, attr->authenticator, msg)) {
1710                 wps->state = SEND_WSC_NACK;
1711                 return WPS_CONTINUE;
1712         }
1713
1714         decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1715                                               attr->encr_settings_len);
1716         if (decrypted == NULL) {
1717                 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1718                            "Settings attribute");
1719                 wps->state = SEND_WSC_NACK;
1720                 return WPS_CONTINUE;
1721         }
1722
1723         wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1724                    "attribute");
1725         if (wps_parse_msg(decrypted, &eattr) < 0 ||
1726             wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1727             wps_process_e_snonce1(wps, eattr.e_snonce1)) {
1728                 wpabuf_free(decrypted);
1729                 wps->state = SEND_WSC_NACK;
1730                 return WPS_CONTINUE;
1731         }
1732         wpabuf_free(decrypted);
1733
1734         wps->state = SEND_M6;
1735         return WPS_CONTINUE;
1736 }
1737
1738
1739 static int wps_process_ap_settings_r(struct wps_data *wps,
1740                                      struct wps_parse_attr *attr)
1741 {
1742         if (wps->wps->ap)
1743                 return 0;
1744
1745         /* AP Settings Attributes in M7 when Enrollee is an AP */
1746         if (wps_process_ap_settings(attr, &wps->cred) < 0)
1747                 return -1;
1748
1749         wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
1750
1751         /*
1752          * TODO: Provide access to AP settings and allow changes before sending
1753          * out M8. For now, just copy the settings unchanged into M8.
1754          */
1755
1756         return 0;
1757 }
1758
1759
1760 static enum wps_process_res wps_process_m7(struct wps_data *wps,
1761                                            const struct wpabuf *msg,
1762                                            struct wps_parse_attr *attr)
1763 {
1764         struct wpabuf *decrypted;
1765         struct wps_parse_attr eattr;
1766
1767         wpa_printf(MSG_DEBUG, "WPS: Received M7");
1768
1769         if (wps->state != RECV_M7) {
1770                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1771                            "receiving M7", wps->state);
1772                 wps->state = SEND_WSC_NACK;
1773                 return WPS_CONTINUE;
1774         }
1775
1776         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1777             wps_process_authenticator(wps, attr->authenticator, msg)) {
1778                 wps->state = SEND_WSC_NACK;
1779                 return WPS_CONTINUE;
1780         }
1781
1782         decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1783                                               attr->encr_settings_len);
1784         if (decrypted == NULL) {
1785                 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1786                            "Settings attribute");
1787                 wps->state = SEND_WSC_NACK;
1788                 return WPS_CONTINUE;
1789         }
1790
1791         wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1792                    "attribute");
1793         if (wps_parse_msg(decrypted, &eattr) < 0 ||
1794             wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1795             wps_process_e_snonce2(wps, eattr.e_snonce2) ||
1796             wps_process_ap_settings_r(wps, &eattr)) {
1797                 wpabuf_free(decrypted);
1798                 wps->state = SEND_WSC_NACK;
1799                 return WPS_CONTINUE;
1800         }
1801
1802         wpabuf_free(decrypted);
1803
1804         wps->state = SEND_M8;
1805         return WPS_CONTINUE;
1806 }
1807
1808
1809 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1810                                                 const struct wpabuf *msg)
1811 {
1812         struct wps_parse_attr attr;
1813         enum wps_process_res ret = WPS_CONTINUE;
1814
1815         wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1816
1817         if (wps_parse_msg(msg, &attr) < 0)
1818                 return WPS_FAILURE;
1819
1820         if (attr.version == NULL || *attr.version != WPS_VERSION) {
1821                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1822                            attr.version ? *attr.version : 0);
1823                 return WPS_FAILURE;
1824         }
1825
1826         if (attr.msg_type == NULL) {
1827                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1828                 return WPS_FAILURE;
1829         }
1830
1831         if (*attr.msg_type != WPS_M1 &&
1832             (attr.registrar_nonce == NULL ||
1833              os_memcmp(wps->nonce_r, attr.registrar_nonce,
1834                        WPS_NONCE_LEN != 0))) {
1835                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1836                 return WPS_FAILURE;
1837         }
1838
1839         switch (*attr.msg_type) {
1840         case WPS_M1:
1841                 ret = wps_process_m1(wps, &attr);
1842                 break;
1843         case WPS_M3:
1844                 ret = wps_process_m3(wps, msg, &attr);
1845                 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1846                         wps_fail_event(wps->wps, WPS_M3);
1847                 break;
1848         case WPS_M5:
1849                 ret = wps_process_m5(wps, msg, &attr);
1850                 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1851                         wps_fail_event(wps->wps, WPS_M5);
1852                 break;
1853         case WPS_M7:
1854                 ret = wps_process_m7(wps, msg, &attr);
1855                 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
1856                         wps_fail_event(wps->wps, WPS_M7);
1857                 break;
1858         default:
1859                 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1860                            *attr.msg_type);
1861                 return WPS_FAILURE;
1862         }
1863
1864         if (ret == WPS_CONTINUE) {
1865                 /* Save a copy of the last message for Authenticator derivation
1866                  */
1867                 wpabuf_free(wps->last_msg);
1868                 wps->last_msg = wpabuf_dup(msg);
1869         }
1870
1871         return ret;
1872 }
1873
1874
1875 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1876                                                 const struct wpabuf *msg)
1877 {
1878         struct wps_parse_attr attr;
1879
1880         wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1881
1882         if (wps_parse_msg(msg, &attr) < 0)
1883                 return WPS_FAILURE;
1884
1885         if (attr.version == NULL || *attr.version != WPS_VERSION) {
1886                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1887                            attr.version ? *attr.version : 0);
1888                 return WPS_FAILURE;
1889         }
1890
1891         if (attr.msg_type == NULL) {
1892                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1893                 return WPS_FAILURE;
1894         }
1895
1896         if (*attr.msg_type != WPS_WSC_ACK) {
1897                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1898                            *attr.msg_type);
1899                 return WPS_FAILURE;
1900         }
1901
1902         if (attr.registrar_nonce == NULL ||
1903             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1904         {
1905                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1906                 return WPS_FAILURE;
1907         }
1908
1909         if (attr.enrollee_nonce == NULL ||
1910             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1911                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1912                 return WPS_FAILURE;
1913         }
1914
1915         if (wps->state == RECV_M2D_ACK) {
1916                 /* TODO: support for multiple registrars and sending of
1917                  * multiple M2/M2D messages */
1918
1919                 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
1920                            "terminate negotiation");
1921         }
1922
1923         return WPS_FAILURE;
1924 }
1925
1926
1927 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1928                                                  const struct wpabuf *msg)
1929 {
1930         struct wps_parse_attr attr;
1931         int old_state;
1932
1933         wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1934
1935         old_state = wps->state;
1936         wps->state = SEND_WSC_NACK;
1937
1938         if (wps_parse_msg(msg, &attr) < 0)
1939                 return WPS_FAILURE;
1940
1941         if (attr.version == NULL || *attr.version != WPS_VERSION) {
1942                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1943                            attr.version ? *attr.version : 0);
1944                 return WPS_FAILURE;
1945         }
1946
1947         if (attr.msg_type == NULL) {
1948                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1949                 return WPS_FAILURE;
1950         }
1951
1952         if (*attr.msg_type != WPS_WSC_NACK) {
1953                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1954                            *attr.msg_type);
1955                 return WPS_FAILURE;
1956         }
1957
1958         if (attr.registrar_nonce == NULL ||
1959             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1960         {
1961                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1962                 return WPS_FAILURE;
1963         }
1964
1965         if (attr.enrollee_nonce == NULL ||
1966             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1967                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1968                 return WPS_FAILURE;
1969         }
1970
1971         if (attr.config_error == NULL) {
1972                 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1973                            "in WSC_NACK");
1974                 return WPS_FAILURE;
1975         }
1976
1977         wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
1978                    "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1979
1980         switch (old_state) {
1981         case RECV_M3:
1982                 wps_fail_event(wps->wps, WPS_M2);
1983                 break;
1984         case RECV_M5:
1985                 wps_fail_event(wps->wps, WPS_M4);
1986                 break;
1987         case RECV_M7:
1988                 wps_fail_event(wps->wps, WPS_M6);
1989                 break;
1990         case RECV_DONE:
1991                 wps_fail_event(wps->wps, WPS_M8);
1992                 break;
1993         default:
1994                 break;
1995         }
1996
1997         return WPS_FAILURE;
1998 }
1999
2000
2001 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2002                                                  const struct wpabuf *msg)
2003 {
2004         struct wps_parse_attr attr;
2005
2006         wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2007
2008         if (wps->state != RECV_DONE) {
2009                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2010                            "receiving WSC_Done", wps->state);
2011                 return WPS_FAILURE;
2012         }
2013
2014         if (wps_parse_msg(msg, &attr) < 0)
2015                 return WPS_FAILURE;
2016
2017         if (attr.version == NULL || *attr.version != WPS_VERSION) {
2018                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2019                            attr.version ? *attr.version : 0);
2020                 return WPS_FAILURE;
2021         }
2022
2023         if (attr.msg_type == NULL) {
2024                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2025                 return WPS_FAILURE;
2026         }
2027
2028         if (*attr.msg_type != WPS_WSC_DONE) {
2029                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2030                            *attr.msg_type);
2031                 return WPS_FAILURE;
2032         }
2033
2034         if (attr.registrar_nonce == NULL ||
2035             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2036         {
2037                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2038                 return WPS_FAILURE;
2039         }
2040
2041         if (attr.enrollee_nonce == NULL ||
2042             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2043                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2044                 return WPS_FAILURE;
2045         }
2046
2047         wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2048
2049         if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2050             wps->wps->ap) {
2051                 struct wps_credential cred;
2052
2053                 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2054                            "on first Enrollee connection");
2055
2056                 os_memset(&cred, 0, sizeof(cred));
2057                 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2058                 cred.ssid_len = wps->wps->ssid_len;
2059                 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2060                 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2061                 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2062                 cred.key_len = wps->new_psk_len;
2063
2064                 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2065                 wpa_hexdump_ascii_key(MSG_DEBUG,
2066                                       "WPS: Generated random passphrase",
2067                                       wps->new_psk, wps->new_psk_len);
2068                 if (wps->wps->cred_cb)
2069                         wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2070
2071                 os_free(wps->new_psk);
2072                 wps->new_psk = NULL;
2073         }
2074
2075         if (!wps->wps->ap) {
2076                 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based "
2077                            "on the modified AP configuration");
2078                 if (wps->wps->cred_cb)
2079                         wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2080         }
2081
2082         if (wps->new_psk) {
2083                 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2084                                    wps->new_psk, wps->new_psk_len)) {
2085                         wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2086                                    "new PSK");
2087                 }
2088                 os_free(wps->new_psk);
2089                 wps->new_psk = NULL;
2090         }
2091
2092         if (wps->pbc) {
2093                 wps_registrar_remove_pbc_session(wps->wps->registrar,
2094                                                  wps->mac_addr_e, wps->uuid_e);
2095                 wps_registrar_pbc_completed(wps->wps->registrar);
2096         }
2097
2098         wps_success_event(wps->wps);
2099
2100         return WPS_DONE;
2101 }
2102
2103
2104 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2105                                                enum wsc_op_code op_code,
2106                                                const struct wpabuf *msg)
2107 {
2108         enum wps_process_res ret;
2109
2110         wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2111                    "op_code=%d)",
2112                    (unsigned long) wpabuf_len(msg), op_code);
2113
2114         switch (op_code) {
2115         case WSC_MSG:
2116                 return wps_process_wsc_msg(wps, msg);
2117         case WSC_ACK:
2118                 return wps_process_wsc_ack(wps, msg);
2119         case WSC_NACK:
2120                 return wps_process_wsc_nack(wps, msg);
2121         case WSC_Done:
2122                 ret = wps_process_wsc_done(wps, msg);
2123                 if (ret == WPS_FAILURE) {
2124                         wps->state = SEND_WSC_NACK;
2125                         wps_fail_event(wps->wps, WPS_WSC_DONE);
2126                 }
2127                 return ret;
2128         default:
2129                 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
2130                 return WPS_FAILURE;
2131         }
2132 }