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