WPS: Added more verbose debug info on authentication type mismatch
[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 (own 0x%x Enrollee 0x%x)",
1424                            wps->wps->auth_types, auth_types);
1425                 return -1;
1426         }
1427
1428         return 0;
1429 }
1430
1431
1432 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1433 {
1434         u16 encr_types;
1435
1436         if (encr == NULL) {
1437                 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1438                            "received");
1439                 return -1;
1440         }
1441
1442         encr_types = WPA_GET_BE16(encr);
1443
1444         wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1445                    encr_types);
1446         wps->encr_type = wps->wps->encr_types & encr_types;
1447         if (wps->encr_type == 0) {
1448                 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1449                            "encryption types");
1450                 return -1;
1451         }
1452
1453         return 0;
1454 }
1455
1456
1457 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1458 {
1459         if (conn == NULL) {
1460                 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1461                            "received");
1462                 return -1;
1463         }
1464
1465         wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1466                    *conn);
1467
1468         return 0;
1469 }
1470
1471
1472 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1473 {
1474         u16 m;
1475
1476         if (methods == NULL) {
1477                 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1478                 return -1;
1479         }
1480
1481         m = WPA_GET_BE16(methods);
1482
1483         wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1484
1485         return 0;
1486 }
1487
1488
1489 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1490 {
1491         if (state == NULL) {
1492                 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1493                            "received");
1494                 return -1;
1495         }
1496
1497         wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1498                    *state);
1499
1500         return 0;
1501 }
1502
1503
1504 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1505 {
1506         u16 a;
1507
1508         if (assoc == NULL) {
1509                 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1510                 return -1;
1511         }
1512
1513         a = WPA_GET_BE16(assoc);
1514         wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
1515
1516         return 0;
1517 }
1518
1519
1520 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
1521 {
1522         u16 e;
1523
1524         if (err == NULL) {
1525                 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
1526                 return -1;
1527         }
1528
1529         e = WPA_GET_BE16(err);
1530         wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
1531
1532         return 0;
1533 }
1534
1535
1536 static enum wps_process_res wps_process_m1(struct wps_data *wps,
1537                                            struct wps_parse_attr *attr)
1538 {
1539         wpa_printf(MSG_DEBUG, "WPS: Received M1");
1540
1541         if (wps->state != RECV_M1) {
1542                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1543                            "receiving M1", wps->state);
1544                 return WPS_FAILURE;
1545         }
1546
1547         if (wps_process_uuid_e(wps, attr->uuid_e) ||
1548             wps_process_mac_addr(wps, attr->mac_addr) ||
1549             wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1550             wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
1551             wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
1552             wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
1553             wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
1554             wps_process_config_methods(wps, attr->config_methods) ||
1555             wps_process_wps_state(wps, attr->wps_state) ||
1556             wps_process_device_attrs(&wps->peer_dev, attr) ||
1557             wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
1558             wps_process_assoc_state(wps, attr->assoc_state) ||
1559             wps_process_dev_password_id(wps, attr->dev_password_id) ||
1560             wps_process_config_error(wps, attr->config_error) ||
1561             wps_process_os_version(&wps->peer_dev, attr->os_version))
1562                 return WPS_FAILURE;
1563
1564         if (wps->dev_pw_id != DEV_PW_DEFAULT &&
1565             wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
1566             wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
1567             wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
1568             (wps->dev_pw_id != DEV_PW_PUSHBUTTON || !wps->registrar->pbc)) {
1569                 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
1570                            wps->dev_pw_id);
1571                 wps->state = SEND_M2D;
1572                 return WPS_CONTINUE;
1573         }
1574
1575         if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
1576                 if (wps_registrar_pbc_overlap(wps->registrar, wps->mac_addr_e,
1577                                               wps->uuid_e)) {
1578                         wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
1579                                    "negotiation");
1580                         wps->state = SEND_M2D;
1581                         return WPS_CONTINUE;
1582                 }
1583                 wps_registrar_add_pbc_session(wps->registrar, wps->mac_addr_e,
1584                                               wps->uuid_e);
1585                 wps->pbc = 1;
1586         }
1587
1588         wps->state = SEND_M2;
1589         return WPS_CONTINUE;
1590 }
1591
1592
1593 static enum wps_process_res wps_process_m3(struct wps_data *wps,
1594                                            const struct wpabuf *msg,
1595                                            struct wps_parse_attr *attr)
1596 {
1597         wpa_printf(MSG_DEBUG, "WPS: Received M3");
1598
1599         if (wps->state != RECV_M3) {
1600                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1601                            "receiving M3", wps->state);
1602                 return WPS_FAILURE;
1603         }
1604
1605         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1606             wps_process_authenticator(wps, attr->authenticator, msg) ||
1607             wps_process_e_hash1(wps, attr->e_hash1) ||
1608             wps_process_e_hash2(wps, attr->e_hash2))
1609                 return WPS_FAILURE;
1610
1611         wps->state = SEND_M4;
1612         return WPS_CONTINUE;
1613 }
1614
1615
1616 static enum wps_process_res wps_process_m5(struct wps_data *wps,
1617                                            const struct wpabuf *msg,
1618                                            struct wps_parse_attr *attr)
1619 {
1620         struct wpabuf *decrypted;
1621         struct wps_parse_attr eattr;
1622
1623         wpa_printf(MSG_DEBUG, "WPS: Received M5");
1624
1625         if (wps->state != RECV_M5) {
1626                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1627                            "receiving M5", wps->state);
1628                 return WPS_FAILURE;
1629         }
1630
1631         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1632             wps_process_authenticator(wps, attr->authenticator, msg))
1633                 return WPS_FAILURE;
1634
1635         decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1636                                               attr->encr_settings_len);
1637         if (decrypted == NULL) {
1638                 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1639                            "Settings attribute");
1640                 return WPS_FAILURE;
1641         }
1642
1643         wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1644                    "attribute");
1645         if (wps_parse_msg(decrypted, &eattr) < 0 ||
1646             wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1647             wps_process_e_snonce1(wps, eattr.e_snonce1)) {
1648                 wpabuf_free(decrypted);
1649                 return WPS_FAILURE;
1650         }
1651         wpabuf_free(decrypted);
1652
1653         wps->state = SEND_M6;
1654         return WPS_CONTINUE;
1655 }
1656
1657
1658 static int wps_process_ap_settings_r(struct wps_data *wps,
1659                                      struct wps_parse_attr *attr)
1660 {
1661         if (wps->wps->ap)
1662                 return 0;
1663
1664         /* AP Settings Attributes in M7 when Enrollee is an AP */
1665         if (wps_process_ap_settings(attr, &wps->cred) < 0)
1666                 return -1;
1667
1668         wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
1669
1670         /*
1671          * TODO: Provide access to AP settings and allow changes before sending
1672          * out M8. For now, just copy the settings unchanged into M8.
1673          */
1674
1675         return 0;
1676 }
1677
1678
1679 static enum wps_process_res wps_process_m7(struct wps_data *wps,
1680                                            const struct wpabuf *msg,
1681                                            struct wps_parse_attr *attr)
1682 {
1683         struct wpabuf *decrypted;
1684         struct wps_parse_attr eattr;
1685
1686         wpa_printf(MSG_DEBUG, "WPS: Received M7");
1687
1688         if (wps->state != RECV_M7) {
1689                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1690                            "receiving M7", wps->state);
1691                 return WPS_FAILURE;
1692         }
1693
1694         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
1695             wps_process_authenticator(wps, attr->authenticator, msg))
1696                 return WPS_FAILURE;
1697
1698         decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
1699                                               attr->encr_settings_len);
1700         if (decrypted == NULL) {
1701                 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
1702                            "Settings attribute");
1703                 return WPS_FAILURE;
1704         }
1705
1706         wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
1707                    "attribute");
1708         if (wps_parse_msg(decrypted, &eattr) < 0 ||
1709             wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
1710             wps_process_e_snonce2(wps, eattr.e_snonce2) ||
1711             wps_process_ap_settings_r(wps, &eattr)) {
1712                 wpabuf_free(decrypted);
1713                 return WPS_FAILURE;
1714         }
1715
1716         wpabuf_free(decrypted);
1717
1718         wps->state = SEND_M8;
1719         return WPS_CONTINUE;
1720 }
1721
1722
1723 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
1724                                                 const struct wpabuf *msg)
1725 {
1726         struct wps_parse_attr attr;
1727         enum wps_process_res ret = WPS_CONTINUE;
1728
1729         wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
1730
1731         if (wps_parse_msg(msg, &attr) < 0)
1732                 return WPS_FAILURE;
1733
1734         if (attr.version == NULL || *attr.version != WPS_VERSION) {
1735                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1736                            attr.version ? *attr.version : 0);
1737                 return WPS_FAILURE;
1738         }
1739
1740         if (attr.msg_type == NULL) {
1741                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1742                 return WPS_FAILURE;
1743         }
1744
1745         if (*attr.msg_type != WPS_M1 &&
1746             (attr.registrar_nonce == NULL ||
1747              os_memcmp(wps->nonce_r, attr.registrar_nonce,
1748                        WPS_NONCE_LEN != 0))) {
1749                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1750                 return WPS_FAILURE;
1751         }
1752
1753         switch (*attr.msg_type) {
1754         case WPS_M1:
1755                 ret = wps_process_m1(wps, &attr);
1756                 break;
1757         case WPS_M3:
1758                 ret = wps_process_m3(wps, msg, &attr);
1759                 break;
1760         case WPS_M5:
1761                 ret = wps_process_m5(wps, msg, &attr);
1762                 break;
1763         case WPS_M7:
1764                 ret = wps_process_m7(wps, msg, &attr);
1765                 break;
1766         default:
1767                 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
1768                            *attr.msg_type);
1769                 return WPS_FAILURE;
1770         }
1771
1772         if (ret == WPS_CONTINUE) {
1773                 /* Save a copy of the last message for Authenticator derivation
1774                  */
1775                 wpabuf_free(wps->last_msg);
1776                 wps->last_msg = wpabuf_dup(msg);
1777         }
1778
1779         return ret;
1780 }
1781
1782
1783 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
1784                                                 const struct wpabuf *msg)
1785 {
1786         struct wps_parse_attr attr;
1787
1788         wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
1789
1790         if (wps_parse_msg(msg, &attr) < 0)
1791                 return WPS_FAILURE;
1792
1793         if (attr.version == NULL || *attr.version != WPS_VERSION) {
1794                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1795                            attr.version ? *attr.version : 0);
1796                 return WPS_FAILURE;
1797         }
1798
1799         if (attr.msg_type == NULL) {
1800                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1801                 return WPS_FAILURE;
1802         }
1803
1804         if (*attr.msg_type != WPS_WSC_ACK) {
1805                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1806                            *attr.msg_type);
1807                 return WPS_FAILURE;
1808         }
1809
1810         if (attr.registrar_nonce == NULL ||
1811             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1812         {
1813                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1814                 return WPS_FAILURE;
1815         }
1816
1817         if (attr.enrollee_nonce == NULL ||
1818             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1819                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1820                 return WPS_FAILURE;
1821         }
1822
1823         if (wps->state == RECV_M2D_ACK) {
1824                 /* TODO: support for multiple registrars and sending of
1825                  * multiple M2/M2D messages */
1826
1827                 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
1828                            "terminate negotiation");
1829         }
1830
1831         return WPS_FAILURE;
1832 }
1833
1834
1835 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
1836                                                  const struct wpabuf *msg)
1837 {
1838         struct wps_parse_attr attr;
1839
1840         wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
1841
1842         if (wps_parse_msg(msg, &attr) < 0)
1843                 return WPS_FAILURE;
1844
1845         if (attr.version == NULL || *attr.version != WPS_VERSION) {
1846                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1847                            attr.version ? *attr.version : 0);
1848                 return WPS_FAILURE;
1849         }
1850
1851         if (attr.msg_type == NULL) {
1852                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1853                 return WPS_FAILURE;
1854         }
1855
1856         if (*attr.msg_type != WPS_WSC_NACK) {
1857                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1858                            *attr.msg_type);
1859                 return WPS_FAILURE;
1860         }
1861
1862         if (attr.registrar_nonce == NULL ||
1863             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1864         {
1865                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1866                 return WPS_FAILURE;
1867         }
1868
1869         if (attr.enrollee_nonce == NULL ||
1870             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1871                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1872                 return WPS_FAILURE;
1873         }
1874
1875         if (attr.config_error == NULL) {
1876                 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
1877                            "in WSC_NACK");
1878                 return WPS_FAILURE;
1879         }
1880
1881         wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
1882                    "Configuration Error %d", WPA_GET_BE16(attr.config_error));
1883
1884         return WPS_FAILURE;
1885 }
1886
1887
1888 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
1889                                                  const struct wpabuf *msg)
1890 {
1891         struct wps_parse_attr attr;
1892
1893         wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
1894
1895         if (wps->state != RECV_DONE) {
1896                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1897                            "receiving WSC_Done", wps->state);
1898                 return WPS_FAILURE;
1899         }
1900
1901         if (wps_parse_msg(msg, &attr) < 0)
1902                 return WPS_FAILURE;
1903
1904         if (attr.version == NULL || *attr.version != WPS_VERSION) {
1905                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
1906                            attr.version ? *attr.version : 0);
1907                 return WPS_FAILURE;
1908         }
1909
1910         if (attr.msg_type == NULL) {
1911                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
1912                 return WPS_FAILURE;
1913         }
1914
1915         if (*attr.msg_type != WPS_WSC_DONE) {
1916                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
1917                            *attr.msg_type);
1918                 return WPS_FAILURE;
1919         }
1920
1921         if (attr.registrar_nonce == NULL ||
1922             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
1923         {
1924                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
1925                 return WPS_FAILURE;
1926         }
1927
1928         if (attr.enrollee_nonce == NULL ||
1929             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
1930                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
1931                 return WPS_FAILURE;
1932         }
1933
1934         wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
1935
1936         if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
1937             wps->wps->ap) {
1938                 struct wps_credential cred;
1939
1940                 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
1941                            "on first Enrollee connection");
1942
1943                 os_memset(&cred, 0, sizeof(cred));
1944                 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1945                 cred.ssid_len = wps->wps->ssid_len;
1946                 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
1947                 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
1948                 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
1949                 cred.key_len = wps->new_psk_len;
1950
1951                 wps->wps->wps_state = WPS_STATE_CONFIGURED;
1952                 wpa_hexdump_ascii_key(MSG_DEBUG,
1953                                       "WPS: Generated random passphrase",
1954                                       wps->new_psk, wps->new_psk_len);
1955                 if (wps->wps->cred_cb)
1956                         wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
1957
1958                 os_free(wps->new_psk);
1959                 wps->new_psk = NULL;
1960         }
1961
1962         if (!wps->wps->ap) {
1963                 wpa_printf(MSG_DEBUG, "WPS: Update local configuration based "
1964                            "on the modified AP configuration");
1965                 if (wps->wps->cred_cb)
1966                         wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
1967         }
1968
1969         if (wps->new_psk) {
1970                 if (wps_cb_new_psk(wps->registrar, wps->mac_addr_e,
1971                                    wps->new_psk, wps->new_psk_len)) {
1972                         wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
1973                                    "new PSK");
1974                 }
1975                 os_free(wps->new_psk);
1976                 wps->new_psk = NULL;
1977         }
1978
1979         if (wps->pbc) {
1980                 wps_registrar_remove_pbc_session(wps->registrar,
1981                                                  wps->mac_addr_e, wps->uuid_e);
1982                 wps_registrar_pbc_completed(wps->registrar);
1983         }
1984
1985         return WPS_DONE;
1986 }
1987
1988
1989 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
1990                                                u8 op_code,
1991                                                const struct wpabuf *msg)
1992 {
1993
1994         wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
1995                    "op_code=%d)",
1996                    (unsigned long) wpabuf_len(msg), op_code);
1997
1998         switch (op_code) {
1999         case WSC_MSG:
2000                 return wps_process_wsc_msg(wps, msg);
2001         case WSC_ACK:
2002                 return wps_process_wsc_ack(wps, msg);
2003         case WSC_NACK:
2004                 return wps_process_wsc_nack(wps, msg);
2005         case WSC_Done:
2006                 return wps_process_wsc_done(wps, msg);
2007         default:
2008                 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
2009                 return WPS_FAILURE;
2010         }
2011 }