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