WPS: Use Config Error 12 to indicate PBC overlap in M2D
[libeap.git] / src / wps / wps_registrar.c
1 /*
2  * Wi-Fi Protected Setup - Registrar
3  * Copyright (c) 2008-2009, 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 #include "wps_upnp.h"
25 #include "crypto.h"
26 #include "uuid.h"
27
28 #define WPS_WORKAROUNDS
29
30 struct wps_uuid_pin {
31         struct wps_uuid_pin *next;
32         u8 uuid[WPS_UUID_LEN];
33         int wildcard_uuid;
34         u8 *pin;
35         size_t pin_len;
36 #define PIN_LOCKED BIT(0)
37 #define PIN_EXPIRES BIT(1)
38         int flags;
39         struct os_time expiration;
40 };
41
42
43 static void wps_free_pin(struct wps_uuid_pin *pin)
44 {
45         os_free(pin->pin);
46         os_free(pin);
47 }
48
49
50 static void wps_free_pins(struct wps_uuid_pin *pins)
51 {
52         struct wps_uuid_pin *pin, *prev;
53
54         pin = pins;
55         while (pin) {
56                 prev = pin;
57                 pin = pin->next;
58                 wps_free_pin(prev);
59         }
60 }
61
62
63 struct wps_pbc_session {
64         struct wps_pbc_session *next;
65         u8 addr[ETH_ALEN];
66         u8 uuid_e[WPS_UUID_LEN];
67         struct os_time timestamp;
68 };
69
70
71 static void wps_free_pbc_sessions(struct wps_pbc_session *pbc)
72 {
73         struct wps_pbc_session *prev;
74
75         while (pbc) {
76                 prev = pbc;
77                 pbc = pbc->next;
78                 os_free(prev);
79         }
80 }
81
82
83 struct wps_registrar_device {
84         struct wps_registrar_device *next;
85         struct wps_device_data dev;
86         u8 uuid[WPS_UUID_LEN];
87 };
88
89
90 struct wps_registrar {
91         struct wps_context *wps;
92
93         int pbc;
94         int selected_registrar;
95
96         int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
97                           size_t psk_len);
98         int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
99                          const u8 *probe_resp_ie, size_t probe_resp_ie_len);
100         void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
101                               const struct wps_device_data *dev);
102         void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
103                                const u8 *uuid_e);
104         void *cb_ctx;
105
106         struct wps_uuid_pin *pins;
107         struct wps_pbc_session *pbc_sessions;
108
109         int skip_cred_build;
110         struct wpabuf *extra_cred;
111         int disable_auto_conf;
112         int sel_reg_dev_password_id_override;
113         int sel_reg_config_methods_override;
114         int static_wep_only;
115
116         struct wps_registrar_device *devices;
117 };
118
119
120 static int wps_set_ie(struct wps_registrar *reg);
121 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx);
122 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
123                                                void *timeout_ctx);
124
125
126 static void wps_free_devices(struct wps_registrar_device *dev)
127 {
128         struct wps_registrar_device *prev;
129
130         while (dev) {
131                 prev = dev;
132                 dev = dev->next;
133                 wps_device_data_free(&prev->dev);
134                 os_free(prev);
135         }
136 }
137
138
139 static struct wps_registrar_device * wps_device_get(struct wps_registrar *reg,
140                                                     const u8 *addr)
141 {
142         struct wps_registrar_device *dev;
143
144         for (dev = reg->devices; dev; dev = dev->next) {
145                 if (os_memcmp(dev->dev.mac_addr, addr, ETH_ALEN) == 0)
146                         return dev;
147         }
148         return NULL;
149 }
150
151
152 static void wps_device_clone_data(struct wps_device_data *dst,
153                                   struct wps_device_data *src)
154 {
155         os_memcpy(dst->mac_addr, src->mac_addr, ETH_ALEN);
156         dst->categ = src->categ;
157         dst->oui = src->oui;
158         dst->sub_categ = src->sub_categ;
159
160 #define WPS_STRDUP(n) \
161         os_free(dst->n); \
162         dst->n = src->n ? os_strdup(src->n) : NULL
163
164         WPS_STRDUP(device_name);
165         WPS_STRDUP(manufacturer);
166         WPS_STRDUP(model_name);
167         WPS_STRDUP(model_number);
168         WPS_STRDUP(serial_number);
169 #undef WPS_STRDUP
170 }
171
172
173 int wps_device_store(struct wps_registrar *reg,
174                      struct wps_device_data *dev, const u8 *uuid)
175 {
176         struct wps_registrar_device *d;
177
178         d = wps_device_get(reg, dev->mac_addr);
179         if (d == NULL) {
180                 d = os_zalloc(sizeof(*d));
181                 if (d == NULL)
182                         return -1;
183                 d->next = reg->devices;
184                 reg->devices = d;
185         }
186
187         wps_device_clone_data(&d->dev, dev);
188         os_memcpy(d->uuid, uuid, WPS_UUID_LEN);
189
190         return 0;
191 }
192
193
194 static void wps_registrar_add_pbc_session(struct wps_registrar *reg,
195                                           const u8 *addr, const u8 *uuid_e)
196 {
197         struct wps_pbc_session *pbc, *prev = NULL;
198         struct os_time now;
199
200         os_get_time(&now);
201
202         pbc = reg->pbc_sessions;
203         while (pbc) {
204                 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
205                     os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
206                         if (prev)
207                                 prev->next = pbc->next;
208                         else
209                                 reg->pbc_sessions = pbc->next;
210                         break;
211                 }
212                 prev = pbc;
213                 pbc = pbc->next;
214         }
215
216         if (!pbc) {
217                 pbc = os_zalloc(sizeof(*pbc));
218                 if (pbc == NULL)
219                         return;
220                 os_memcpy(pbc->addr, addr, ETH_ALEN);
221                 if (uuid_e)
222                         os_memcpy(pbc->uuid_e, uuid_e, WPS_UUID_LEN);
223         }
224
225         pbc->next = reg->pbc_sessions;
226         reg->pbc_sessions = pbc;
227         pbc->timestamp = now;
228
229         /* remove entries that have timed out */
230         prev = pbc;
231         pbc = pbc->next;
232
233         while (pbc) {
234                 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME) {
235                         prev->next = NULL;
236                         wps_free_pbc_sessions(pbc);
237                         break;
238                 }
239                 prev = pbc;
240                 pbc = pbc->next;
241         }
242 }
243
244
245 static void wps_registrar_remove_pbc_session(struct wps_registrar *reg,
246                                              const u8 *addr, const u8 *uuid_e)
247 {
248         struct wps_pbc_session *pbc, *prev = NULL;
249
250         pbc = reg->pbc_sessions;
251         while (pbc) {
252                 if (os_memcmp(pbc->addr, addr, ETH_ALEN) == 0 &&
253                     os_memcmp(pbc->uuid_e, uuid_e, WPS_UUID_LEN) == 0) {
254                         if (prev)
255                                 prev->next = pbc->next;
256                         else
257                                 reg->pbc_sessions = pbc->next;
258                         os_free(pbc);
259                         break;
260                 }
261                 prev = pbc;
262                 pbc = pbc->next;
263         }
264 }
265
266
267 static int wps_registrar_pbc_overlap(struct wps_registrar *reg,
268                                      const u8 *addr, const u8 *uuid_e)
269 {
270         int count = 0;
271         struct wps_pbc_session *pbc;
272         struct os_time now;
273
274         os_get_time(&now);
275
276         for (pbc = reg->pbc_sessions; pbc; pbc = pbc->next) {
277                 if (now.sec > pbc->timestamp.sec + WPS_PBC_WALK_TIME)
278                         break;
279                 if (addr == NULL || os_memcmp(addr, pbc->addr, ETH_ALEN) ||
280                     uuid_e == NULL ||
281                     os_memcmp(uuid_e, pbc->uuid_e, WPS_UUID_LEN))
282                         count++;
283         }
284
285         if (addr || uuid_e)
286                 count++;
287
288         return count > 1 ? 1 : 0;
289 }
290
291
292 static int wps_build_wps_state(struct wps_context *wps, struct wpabuf *msg)
293 {
294         wpa_printf(MSG_DEBUG, "WPS:  * Wi-Fi Protected Setup State (%d)",
295                    wps->wps_state);
296         wpabuf_put_be16(msg, ATTR_WPS_STATE);
297         wpabuf_put_be16(msg, 1);
298         wpabuf_put_u8(msg, wps->wps_state);
299         return 0;
300 }
301
302
303 #ifdef CONFIG_WPS_UPNP
304 static void wps_registrar_free_pending_m2(struct wps_context *wps)
305 {
306         struct upnp_pending_message *p, *p2, *prev = NULL;
307         p = wps->upnp_msgs;
308         while (p) {
309                 if (p->type == WPS_M2 || p->type == WPS_M2D) {
310                         if (prev == NULL)
311                                 wps->upnp_msgs = p->next;
312                         else
313                                 prev->next = p->next;
314                         wpa_printf(MSG_DEBUG, "WPS UPnP: Drop pending M2/M2D");
315                         p2 = p;
316                         p = p->next;
317                         wpabuf_free(p2->msg);
318                         os_free(p2);
319                         continue;
320                 }
321                 prev = p;
322                 p = p->next;
323         }
324 }
325 #endif /* CONFIG_WPS_UPNP */
326
327
328 static int wps_build_ap_setup_locked(struct wps_context *wps,
329                                      struct wpabuf *msg)
330 {
331         if (wps->ap_setup_locked) {
332                 wpa_printf(MSG_DEBUG, "WPS:  * AP Setup Locked");
333                 wpabuf_put_be16(msg, ATTR_AP_SETUP_LOCKED);
334                 wpabuf_put_be16(msg, 1);
335                 wpabuf_put_u8(msg, 1);
336         }
337         return 0;
338 }
339
340
341 static int wps_build_selected_registrar(struct wps_registrar *reg,
342                                         struct wpabuf *msg)
343 {
344         if (!reg->selected_registrar)
345                 return 0;
346         wpa_printf(MSG_DEBUG, "WPS:  * Selected Registrar");
347         wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR);
348         wpabuf_put_be16(msg, 1);
349         wpabuf_put_u8(msg, 1);
350         return 0;
351 }
352
353
354 static int wps_build_sel_reg_dev_password_id(struct wps_registrar *reg,
355                                              struct wpabuf *msg)
356 {
357         u16 id = reg->pbc ? DEV_PW_PUSHBUTTON : DEV_PW_DEFAULT;
358         if (!reg->selected_registrar)
359                 return 0;
360         if (reg->sel_reg_dev_password_id_override >= 0)
361                 id = reg->sel_reg_dev_password_id_override;
362         wpa_printf(MSG_DEBUG, "WPS:  * Device Password ID (%d)", id);
363         wpabuf_put_be16(msg, ATTR_DEV_PASSWORD_ID);
364         wpabuf_put_be16(msg, 2);
365         wpabuf_put_be16(msg, id);
366         return 0;
367 }
368
369
370 static int wps_build_sel_reg_config_methods(struct wps_registrar *reg,
371                                             struct wpabuf *msg)
372 {
373         u16 methods;
374         if (!reg->selected_registrar)
375                 return 0;
376         methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
377         if (reg->pbc)
378                 methods |= WPS_CONFIG_PUSHBUTTON;
379         if (reg->sel_reg_config_methods_override >= 0)
380                 methods = reg->sel_reg_config_methods_override;
381         wpa_printf(MSG_DEBUG, "WPS:  * Selected Registrar Config Methods (%x)",
382                    methods);
383         wpabuf_put_be16(msg, ATTR_SELECTED_REGISTRAR_CONFIG_METHODS);
384         wpabuf_put_be16(msg, 2);
385         wpabuf_put_be16(msg, methods);
386         return 0;
387 }
388
389
390 static int wps_build_probe_config_methods(struct wps_registrar *reg,
391                                           struct wpabuf *msg)
392 {
393         u16 methods;
394         methods = 0;
395         wpa_printf(MSG_DEBUG, "WPS:  * Config Methods (%x)", methods);
396         wpabuf_put_be16(msg, ATTR_CONFIG_METHODS);
397         wpabuf_put_be16(msg, 2);
398         wpabuf_put_be16(msg, methods);
399         return 0;
400 }
401
402
403 static int wps_build_config_methods_r(struct wps_registrar *reg,
404                                       struct wpabuf *msg)
405 {
406         u16 methods;
407         methods = reg->wps->config_methods & ~WPS_CONFIG_PUSHBUTTON;
408         if (reg->pbc)
409                 methods |= WPS_CONFIG_PUSHBUTTON;
410         return wps_build_config_methods(msg, methods);
411 }
412
413
414 static int wps_build_resp_type(struct wps_registrar *reg, struct wpabuf *msg)
415 {
416         u8 resp = reg->wps->ap ? WPS_RESP_AP : WPS_RESP_REGISTRAR;
417         wpa_printf(MSG_DEBUG, "WPS:  * Response Type (%d)", resp);
418         wpabuf_put_be16(msg, ATTR_RESPONSE_TYPE);
419         wpabuf_put_be16(msg, 1);
420         wpabuf_put_u8(msg, resp);
421         return 0;
422 }
423
424
425 /**
426  * wps_registrar_init - Initialize WPS Registrar data
427  * @wps: Pointer to longterm WPS context
428  * @cfg: Registrar configuration
429  * Returns: Pointer to allocated Registrar data or %NULL on failure
430  *
431  * This function is used to initialize WPS Registrar functionality. It can be
432  * used for a single Registrar run (e.g., when run in a supplicant) or multiple
433  * runs (e.g., when run as an internal Registrar in an AP). Caller is
434  * responsible for freeing the returned data with wps_registrar_deinit() when
435  * Registrar functionality is not needed anymore.
436  */
437 struct wps_registrar *
438 wps_registrar_init(struct wps_context *wps,
439                    const struct wps_registrar_config *cfg)
440 {
441         struct wps_registrar *reg = os_zalloc(sizeof(*reg));
442         if (reg == NULL)
443                 return NULL;
444
445         reg->wps = wps;
446         reg->new_psk_cb = cfg->new_psk_cb;
447         reg->set_ie_cb = cfg->set_ie_cb;
448         reg->pin_needed_cb = cfg->pin_needed_cb;
449         reg->reg_success_cb = cfg->reg_success_cb;
450         reg->cb_ctx = cfg->cb_ctx;
451         reg->skip_cred_build = cfg->skip_cred_build;
452         if (cfg->extra_cred) {
453                 reg->extra_cred = wpabuf_alloc_copy(cfg->extra_cred,
454                                                     cfg->extra_cred_len);
455                 if (reg->extra_cred == NULL) {
456                         os_free(reg);
457                         return NULL;
458                 }
459         }
460         reg->disable_auto_conf = cfg->disable_auto_conf;
461         reg->sel_reg_dev_password_id_override = -1;
462         reg->sel_reg_config_methods_override = -1;
463         reg->static_wep_only = cfg->static_wep_only;
464
465         if (wps_set_ie(reg)) {
466                 wps_registrar_deinit(reg);
467                 return NULL;
468         }
469
470         return reg;
471 }
472
473
474 /**
475  * wps_registrar_deinit - Deinitialize WPS Registrar data
476  * @reg: Registrar data from wps_registrar_init()
477  */
478 void wps_registrar_deinit(struct wps_registrar *reg)
479 {
480         if (reg == NULL)
481                 return;
482         eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
483         eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
484         wps_free_pins(reg->pins);
485         wps_free_pbc_sessions(reg->pbc_sessions);
486         wpabuf_free(reg->extra_cred);
487         wps_free_devices(reg->devices);
488         os_free(reg);
489 }
490
491
492 /**
493  * wps_registrar_add_pin - Configure a new PIN for Registrar
494  * @reg: Registrar data from wps_registrar_init()
495  * @uuid: UUID-E or %NULL for wildcard (any UUID)
496  * @pin: PIN (Device Password)
497  * @pin_len: Length of pin in octets
498  * @timeout: Time (in seconds) when the PIN will be invalidated; 0 = no timeout
499  * Returns: 0 on success, -1 on failure
500  */
501 int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
502                           const u8 *pin, size_t pin_len, int timeout)
503 {
504         struct wps_uuid_pin *p;
505
506         p = os_zalloc(sizeof(*p));
507         if (p == NULL)
508                 return -1;
509         if (uuid == NULL)
510                 p->wildcard_uuid = 1;
511         else
512                 os_memcpy(p->uuid, uuid, WPS_UUID_LEN);
513         p->pin = os_malloc(pin_len);
514         if (p->pin == NULL) {
515                 os_free(p);
516                 return -1;
517         }
518         os_memcpy(p->pin, pin, pin_len);
519         p->pin_len = pin_len;
520
521         if (timeout) {
522                 p->flags |= PIN_EXPIRES;
523                 os_get_time(&p->expiration);
524                 p->expiration.sec += timeout;
525         }
526
527         p->next = reg->pins;
528         reg->pins = p;
529
530         wpa_printf(MSG_DEBUG, "WPS: A new PIN configured (timeout=%d)",
531                    timeout);
532         wpa_hexdump(MSG_DEBUG, "WPS: UUID", uuid, WPS_UUID_LEN);
533         wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: PIN", pin, pin_len);
534         reg->selected_registrar = 1;
535         reg->pbc = 0;
536         wps_set_ie(reg);
537
538         return 0;
539 }
540
541
542 static void wps_registrar_expire_pins(struct wps_registrar *reg)
543 {
544         struct wps_uuid_pin *pin, *prev, *del;
545         struct os_time now;
546
547         os_get_time(&now);
548         prev = NULL;
549         pin = reg->pins;
550         while (pin) {
551                 if ((pin->flags & PIN_EXPIRES) &&
552                     os_time_before(&pin->expiration, &now)) {
553                         if (prev == NULL)
554                                 reg->pins = pin->next;
555                         else
556                                 prev->next = pin->next;
557                         del = pin;
558                         pin = pin->next;
559                         wpa_hexdump(MSG_DEBUG, "WPS: Expired PIN for UUID",
560                                     del->uuid, WPS_UUID_LEN);
561                         wps_free_pin(del);
562                         continue;
563                 }
564                 prev = pin;
565                 pin = pin->next;
566         }
567 }
568
569
570 /**
571  * wps_registrar_invalidate_pin - Invalidate a PIN for a specific UUID-E
572  * @reg: Registrar data from wps_registrar_init()
573  * @uuid: UUID-E
574  * Returns: 0 on success, -1 on failure (e.g., PIN not found)
575  */
576 int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid)
577 {
578         struct wps_uuid_pin *pin, *prev;
579
580         prev = NULL;
581         pin = reg->pins;
582         while (pin) {
583                 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
584                         if (prev == NULL)
585                                 reg->pins = pin->next;
586                         else
587                                 prev->next = pin->next;
588                         wpa_hexdump(MSG_DEBUG, "WPS: Invalidated PIN for UUID",
589                                     pin->uuid, WPS_UUID_LEN);
590                         wps_free_pin(pin);
591                         return 0;
592                 }
593                 prev = pin;
594                 pin = pin->next;
595         }
596
597         return -1;
598 }
599
600
601 static const u8 * wps_registrar_get_pin(struct wps_registrar *reg,
602                                         const u8 *uuid, size_t *pin_len)
603 {
604         struct wps_uuid_pin *pin;
605
606         wps_registrar_expire_pins(reg);
607
608         pin = reg->pins;
609         while (pin) {
610                 if (!pin->wildcard_uuid &&
611                     os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0)
612                         break;
613                 pin = pin->next;
614         }
615
616         if (!pin) {
617                 /* Check for wildcard UUIDs since none of the UUID-specific
618                  * PINs matched */
619                 pin = reg->pins;
620                 while (pin) {
621                         if (pin->wildcard_uuid == 1) {
622                                 wpa_printf(MSG_DEBUG, "WPS: Found a wildcard "
623                                            "PIN. Assigned it for this UUID-E");
624                                 pin->wildcard_uuid = 2;
625                                 os_memcpy(pin->uuid, uuid, WPS_UUID_LEN);
626                                 break;
627                         }
628                         pin = pin->next;
629                 }
630         }
631
632         if (!pin)
633                 return NULL;
634
635         /*
636          * Lock the PIN to avoid attacks based on concurrent re-use of the PIN
637          * that could otherwise avoid PIN invalidations.
638          */
639         if (pin->flags & PIN_LOCKED) {
640                 wpa_printf(MSG_DEBUG, "WPS: Selected PIN locked - do not "
641                            "allow concurrent re-use");
642                 return NULL;
643         }
644         *pin_len = pin->pin_len;
645         pin->flags |= PIN_LOCKED;
646         return pin->pin;
647 }
648
649
650 /**
651  * wps_registrar_unlock_pin - Unlock a PIN for a specific UUID-E
652  * @reg: Registrar data from wps_registrar_init()
653  * @uuid: UUID-E
654  * Returns: 0 on success, -1 on failure
655  *
656  * PINs are locked to enforce only one concurrent use. This function unlocks a
657  * PIN to allow it to be used again. If the specified PIN was configured using
658  * a wildcard UUID, it will be removed instead of allowing multiple uses.
659  */
660 int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid)
661 {
662         struct wps_uuid_pin *pin;
663
664         pin = reg->pins;
665         while (pin) {
666                 if (os_memcmp(pin->uuid, uuid, WPS_UUID_LEN) == 0) {
667                         if (pin->wildcard_uuid == 2) {
668                                 wpa_printf(MSG_DEBUG, "WPS: Invalidating used "
669                                            "wildcard PIN");
670                                 return wps_registrar_invalidate_pin(reg, uuid);
671                         }
672                         pin->flags &= ~PIN_LOCKED;
673                         return 0;
674                 }
675                 pin = pin->next;
676         }
677
678         return -1;
679 }
680
681
682 static void wps_registrar_stop_pbc(struct wps_registrar *reg)
683 {
684         reg->selected_registrar = 0;
685         reg->pbc = 0;
686         wps_set_ie(reg);
687 }
688
689
690 static void wps_registrar_pbc_timeout(void *eloop_ctx, void *timeout_ctx)
691 {
692         struct wps_registrar *reg = eloop_ctx;
693
694         wpa_printf(MSG_DEBUG, "WPS: PBC timed out - disable PBC mode");
695         wps_registrar_stop_pbc(reg);
696 }
697
698
699 /**
700  * wps_registrar_button_pushed - Notify Registrar that AP button was pushed
701  * @reg: Registrar data from wps_registrar_init()
702  * Returns: 0 on success, -1 on failure
703  *
704  * This function is called on an AP when a push button is pushed to activate
705  * PBC mode. The PBC mode will be stopped after walk time (2 minutes) timeout
706  * or when a PBC registration is completed.
707  */
708 int wps_registrar_button_pushed(struct wps_registrar *reg)
709 {
710         if (wps_registrar_pbc_overlap(reg, NULL, NULL)) {
711                 wpa_printf(MSG_DEBUG, "WPS: PBC overlap - do not start PBC "
712                            "mode");
713                 return -1;
714         }
715         wpa_printf(MSG_DEBUG, "WPS: Button pushed - PBC mode started");
716         reg->selected_registrar = 1;
717         reg->pbc = 1;
718         wps_set_ie(reg);
719
720         eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
721         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wps_registrar_pbc_timeout,
722                                reg, NULL);
723         return 0;
724 }
725
726
727 static void wps_registrar_pbc_completed(struct wps_registrar *reg)
728 {
729         wpa_printf(MSG_DEBUG, "WPS: PBC completed - stopping PBC mode");
730         eloop_cancel_timeout(wps_registrar_pbc_timeout, reg, NULL);
731         wps_registrar_stop_pbc(reg);
732 }
733
734
735 /**
736  * wps_registrar_probe_req_rx - Notify Registrar of Probe Request
737  * @reg: Registrar data from wps_registrar_init()
738  * @addr: MAC address of the Probe Request sender
739  * @wps_data: WPS IE contents
740  *
741  * This function is called on an AP when a Probe Request with WPS IE is
742  * received. This is used to track PBC mode use and to detect possible overlap
743  * situation with other WPS APs.
744  */
745 void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
746                                 const struct wpabuf *wps_data)
747 {
748         struct wps_parse_attr attr;
749         u16 methods;
750
751         wpa_hexdump_buf(MSG_MSGDUMP,
752                         "WPS: Probe Request with WPS data received",
753                         wps_data);
754
755         if (wps_parse_msg(wps_data, &attr) < 0)
756                 return;
757         if (!wps_version_supported(attr.version)) {
758                 wpa_printf(MSG_DEBUG, "WPS: Unsupported ProbeReq WPS IE "
759                            "version 0x%x", attr.version ? *attr.version : 0);
760                 return;
761         }
762
763         if (attr.config_methods == NULL) {
764                 wpa_printf(MSG_DEBUG, "WPS: No Config Methods attribute in "
765                            "Probe Request");
766                 return;
767         }
768
769         methods = WPA_GET_BE16(attr.config_methods);
770         if (!(methods & WPS_CONFIG_PUSHBUTTON))
771                 return; /* Not PBC */
772
773         wpa_printf(MSG_DEBUG, "WPS: Probe Request for PBC received from "
774                    MACSTR, MAC2STR(addr));
775
776         wps_registrar_add_pbc_session(reg, addr, attr.uuid_e);
777 }
778
779
780 static int wps_cb_new_psk(struct wps_registrar *reg, const u8 *mac_addr,
781                           const u8 *psk, size_t psk_len)
782 {
783         if (reg->new_psk_cb == NULL)
784                 return 0;
785
786         return reg->new_psk_cb(reg->cb_ctx, mac_addr, psk, psk_len);
787 }
788
789
790 static void wps_cb_pin_needed(struct wps_registrar *reg, const u8 *uuid_e,
791                               const struct wps_device_data *dev)
792 {
793         if (reg->pin_needed_cb == NULL)
794                 return;
795
796         reg->pin_needed_cb(reg->cb_ctx, uuid_e, dev);
797 }
798
799
800 static void wps_cb_reg_success(struct wps_registrar *reg, const u8 *mac_addr,
801                                const u8 *uuid_e)
802 {
803         if (reg->reg_success_cb == NULL)
804                 return;
805
806         reg->reg_success_cb(reg->cb_ctx, mac_addr, uuid_e);
807 }
808
809
810 static int wps_cb_set_ie(struct wps_registrar *reg,
811                          const struct wpabuf *beacon_ie,
812                          const struct wpabuf *probe_resp_ie)
813 {
814         if (reg->set_ie_cb == NULL)
815                 return 0;
816
817         return reg->set_ie_cb(reg->cb_ctx, wpabuf_head(beacon_ie),
818                               wpabuf_len(beacon_ie),
819                               wpabuf_head(probe_resp_ie),
820                               wpabuf_len(probe_resp_ie));
821 }
822
823
824 /* Encapsulate WPS IE data with one (or more, if needed) IE headers */
825 static struct wpabuf * wps_ie_encapsulate(struct wpabuf *data)
826 {
827         struct wpabuf *ie;
828         const u8 *pos, *end;
829
830         ie = wpabuf_alloc(wpabuf_len(data) + 100);
831         if (ie == NULL) {
832                 wpabuf_free(data);
833                 return NULL;
834         }
835
836         pos = wpabuf_head(data);
837         end = pos + wpabuf_len(data);
838
839         while (end > pos) {
840                 size_t frag_len = end - pos;
841                 if (frag_len > 251)
842                         frag_len = 251;
843                 wpabuf_put_u8(ie, WLAN_EID_VENDOR_SPECIFIC);
844                 wpabuf_put_u8(ie, 4 + frag_len);
845                 wpabuf_put_be32(ie, WPS_DEV_OUI_WFA);
846                 wpabuf_put_data(ie, pos, frag_len);
847                 pos += frag_len;
848         }
849
850         wpabuf_free(data);
851
852         return ie;
853 }
854
855
856 static int wps_set_ie(struct wps_registrar *reg)
857 {
858         struct wpabuf *beacon;
859         struct wpabuf *probe;
860         int ret;
861
862         wpa_printf(MSG_DEBUG, "WPS: Build Beacon and Probe Response IEs");
863
864         beacon = wpabuf_alloc(300);
865         if (beacon == NULL)
866                 return -1;
867         probe = wpabuf_alloc(400);
868         if (probe == NULL) {
869                 wpabuf_free(beacon);
870                 return -1;
871         }
872
873         if (wps_build_version(beacon) ||
874             wps_build_wps_state(reg->wps, beacon) ||
875             wps_build_ap_setup_locked(reg->wps, beacon) ||
876             wps_build_selected_registrar(reg, beacon) ||
877             wps_build_sel_reg_dev_password_id(reg, beacon) ||
878             wps_build_sel_reg_config_methods(reg, beacon) ||
879             wps_build_version(probe) ||
880             wps_build_wps_state(reg->wps, probe) ||
881             wps_build_ap_setup_locked(reg->wps, probe) ||
882             wps_build_selected_registrar(reg, probe) ||
883             wps_build_sel_reg_dev_password_id(reg, probe) ||
884             wps_build_sel_reg_config_methods(reg, probe) ||
885             wps_build_resp_type(reg, probe) ||
886             wps_build_uuid_e(probe, reg->wps->uuid) ||
887             wps_build_device_attrs(&reg->wps->dev, probe) ||
888             wps_build_probe_config_methods(reg, probe) ||
889             wps_build_rf_bands(&reg->wps->dev, probe)) {
890                 wpabuf_free(beacon);
891                 wpabuf_free(probe);
892                 return -1;
893         }
894
895         beacon = wps_ie_encapsulate(beacon);
896         probe = wps_ie_encapsulate(probe);
897
898         if (!beacon || !probe) {
899                 wpabuf_free(beacon);
900                 wpabuf_free(probe);
901                 return -1;
902         }
903
904         if (reg->static_wep_only) {
905                 /*
906                  * Windows XP and Vista clients can get confused about
907                  * EAP-Identity/Request when they probe the network with
908                  * EAPOL-Start. In such a case, they may assume the network is
909                  * using IEEE 802.1X and prompt user for a certificate while
910                  * the correct (non-WPS) behavior would be to ask for the
911                  * static WEP key. As a workaround, use Microsoft Provisioning
912                  * IE to advertise that legacy 802.1X is not supported.
913                  */
914                 const u8 ms_wps[7] = {
915                         WLAN_EID_VENDOR_SPECIFIC, 5,
916                         /* Microsoft Provisioning IE (00:50:f2:5) */
917                         0x00, 0x50, 0xf2, 5,
918                         0x00 /* no legacy 802.1X or MS WPS */
919                 };
920                 wpa_printf(MSG_DEBUG, "WPS: Add Microsoft Provisioning IE "
921                            "into Beacon/Probe Response frames");
922                 wpabuf_put_data(beacon, ms_wps, sizeof(ms_wps));
923                 wpabuf_put_data(probe, ms_wps, sizeof(ms_wps));
924         }
925
926         ret = wps_cb_set_ie(reg, beacon, probe);
927         wpabuf_free(beacon);
928         wpabuf_free(probe);
929
930         return ret;
931 }
932
933
934 static int wps_get_dev_password(struct wps_data *wps)
935 {
936         const u8 *pin;
937         size_t pin_len = 0;
938
939         os_free(wps->dev_password);
940         wps->dev_password = NULL;
941
942         if (wps->pbc) {
943                 wpa_printf(MSG_DEBUG, "WPS: Use default PIN for PBC");
944                 pin = (const u8 *) "00000000";
945                 pin_len = 8;
946         } else {
947                 pin = wps_registrar_get_pin(wps->wps->registrar, wps->uuid_e,
948                                             &pin_len);
949         }
950         if (pin == NULL) {
951                 wpa_printf(MSG_DEBUG, "WPS: No Device Password available for "
952                            "the Enrollee");
953                 wps_cb_pin_needed(wps->wps->registrar, wps->uuid_e,
954                                   &wps->peer_dev);
955                 return -1;
956         }
957
958         wps->dev_password = os_malloc(pin_len);
959         if (wps->dev_password == NULL)
960                 return -1;
961         os_memcpy(wps->dev_password, pin, pin_len);
962         wps->dev_password_len = pin_len;
963
964         return 0;
965 }
966
967
968 static int wps_build_uuid_r(struct wps_data *wps, struct wpabuf *msg)
969 {
970         wpa_printf(MSG_DEBUG, "WPS:  * UUID-R");
971         wpabuf_put_be16(msg, ATTR_UUID_R);
972         wpabuf_put_be16(msg, WPS_UUID_LEN);
973         wpabuf_put_data(msg, wps->uuid_r, WPS_UUID_LEN);
974         return 0;
975 }
976
977
978 static int wps_build_r_hash(struct wps_data *wps, struct wpabuf *msg)
979 {
980         u8 *hash;
981         const u8 *addr[4];
982         size_t len[4];
983
984         if (os_get_random(wps->snonce, 2 * WPS_SECRET_NONCE_LEN) < 0)
985                 return -1;
986         wpa_hexdump(MSG_DEBUG, "WPS: R-S1", wps->snonce, WPS_SECRET_NONCE_LEN);
987         wpa_hexdump(MSG_DEBUG, "WPS: R-S2",
988                     wps->snonce + WPS_SECRET_NONCE_LEN, WPS_SECRET_NONCE_LEN);
989
990         if (wps->dh_pubkey_e == NULL || wps->dh_pubkey_r == NULL) {
991                 wpa_printf(MSG_DEBUG, "WPS: DH public keys not available for "
992                            "R-Hash derivation");
993                 return -1;
994         }
995
996         wpa_printf(MSG_DEBUG, "WPS:  * R-Hash1");
997         wpabuf_put_be16(msg, ATTR_R_HASH1);
998         wpabuf_put_be16(msg, SHA256_MAC_LEN);
999         hash = wpabuf_put(msg, SHA256_MAC_LEN);
1000         /* R-Hash1 = HMAC_AuthKey(R-S1 || PSK1 || PK_E || PK_R) */
1001         addr[0] = wps->snonce;
1002         len[0] = WPS_SECRET_NONCE_LEN;
1003         addr[1] = wps->psk1;
1004         len[1] = WPS_PSK_LEN;
1005         addr[2] = wpabuf_head(wps->dh_pubkey_e);
1006         len[2] = wpabuf_len(wps->dh_pubkey_e);
1007         addr[3] = wpabuf_head(wps->dh_pubkey_r);
1008         len[3] = wpabuf_len(wps->dh_pubkey_r);
1009         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1010         wpa_hexdump(MSG_DEBUG, "WPS: R-Hash1", hash, SHA256_MAC_LEN);
1011
1012         wpa_printf(MSG_DEBUG, "WPS:  * R-Hash2");
1013         wpabuf_put_be16(msg, ATTR_R_HASH2);
1014         wpabuf_put_be16(msg, SHA256_MAC_LEN);
1015         hash = wpabuf_put(msg, SHA256_MAC_LEN);
1016         /* R-Hash2 = HMAC_AuthKey(R-S2 || PSK2 || PK_E || PK_R) */
1017         addr[0] = wps->snonce + WPS_SECRET_NONCE_LEN;
1018         addr[1] = wps->psk2;
1019         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1020         wpa_hexdump(MSG_DEBUG, "WPS: R-Hash2", hash, SHA256_MAC_LEN);
1021
1022         return 0;
1023 }
1024
1025
1026 static int wps_build_r_snonce1(struct wps_data *wps, struct wpabuf *msg)
1027 {
1028         wpa_printf(MSG_DEBUG, "WPS:  * R-SNonce1");
1029         wpabuf_put_be16(msg, ATTR_R_SNONCE1);
1030         wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1031         wpabuf_put_data(msg, wps->snonce, WPS_SECRET_NONCE_LEN);
1032         return 0;
1033 }
1034
1035
1036 static int wps_build_r_snonce2(struct wps_data *wps, struct wpabuf *msg)
1037 {
1038         wpa_printf(MSG_DEBUG, "WPS:  * R-SNonce2");
1039         wpabuf_put_be16(msg, ATTR_R_SNONCE2);
1040         wpabuf_put_be16(msg, WPS_SECRET_NONCE_LEN);
1041         wpabuf_put_data(msg, wps->snonce + WPS_SECRET_NONCE_LEN,
1042                         WPS_SECRET_NONCE_LEN);
1043         return 0;
1044 }
1045
1046
1047 static int wps_build_cred_network_idx(struct wpabuf *msg,
1048                                       struct wps_credential *cred)
1049 {
1050         wpa_printf(MSG_DEBUG, "WPS:  * Network Index");
1051         wpabuf_put_be16(msg, ATTR_NETWORK_INDEX);
1052         wpabuf_put_be16(msg, 1);
1053         wpabuf_put_u8(msg, 1);
1054         return 0;
1055 }
1056
1057
1058 static int wps_build_cred_ssid(struct wpabuf *msg,
1059                                struct wps_credential *cred)
1060 {
1061         wpa_printf(MSG_DEBUG, "WPS:  * SSID");
1062         wpabuf_put_be16(msg, ATTR_SSID);
1063         wpabuf_put_be16(msg, cred->ssid_len);
1064         wpabuf_put_data(msg, cred->ssid, cred->ssid_len);
1065         return 0;
1066 }
1067
1068
1069 static int wps_build_cred_auth_type(struct wpabuf *msg,
1070                                     struct wps_credential *cred)
1071 {
1072         wpa_printf(MSG_DEBUG, "WPS:  * Authentication Type (0x%x)",
1073                    cred->auth_type);
1074         wpabuf_put_be16(msg, ATTR_AUTH_TYPE);
1075         wpabuf_put_be16(msg, 2);
1076         wpabuf_put_be16(msg, cred->auth_type);
1077         return 0;
1078 }
1079
1080
1081 static int wps_build_cred_encr_type(struct wpabuf *msg,
1082                                     struct wps_credential *cred)
1083 {
1084         wpa_printf(MSG_DEBUG, "WPS:  * Encryption Type (0x%x)",
1085                    cred->encr_type);
1086         wpabuf_put_be16(msg, ATTR_ENCR_TYPE);
1087         wpabuf_put_be16(msg, 2);
1088         wpabuf_put_be16(msg, cred->encr_type);
1089         return 0;
1090 }
1091
1092
1093 static int wps_build_cred_network_key(struct wpabuf *msg,
1094                                       struct wps_credential *cred)
1095 {
1096         wpa_printf(MSG_DEBUG, "WPS:  * Network Key");
1097         wpabuf_put_be16(msg, ATTR_NETWORK_KEY);
1098         wpabuf_put_be16(msg, cred->key_len);
1099         wpabuf_put_data(msg, cred->key, cred->key_len);
1100         return 0;
1101 }
1102
1103
1104 static int wps_build_cred_mac_addr(struct wpabuf *msg,
1105                                    struct wps_credential *cred)
1106 {
1107         wpa_printf(MSG_DEBUG, "WPS:  * MAC Address (" MACSTR ")",
1108                    MAC2STR(cred->mac_addr));
1109         wpabuf_put_be16(msg, ATTR_MAC_ADDR);
1110         wpabuf_put_be16(msg, ETH_ALEN);
1111         wpabuf_put_data(msg, cred->mac_addr, ETH_ALEN);
1112         return 0;
1113 }
1114
1115
1116 static int wps_build_credential(struct wpabuf *msg,
1117                                 struct wps_credential *cred)
1118 {
1119         if (wps_build_cred_network_idx(msg, cred) ||
1120             wps_build_cred_ssid(msg, cred) ||
1121             wps_build_cred_auth_type(msg, cred) ||
1122             wps_build_cred_encr_type(msg, cred) ||
1123             wps_build_cred_network_key(msg, cred) ||
1124             wps_build_cred_mac_addr(msg, cred))
1125                 return -1;
1126         return 0;
1127 }
1128
1129
1130 int wps_build_cred(struct wps_data *wps, struct wpabuf *msg)
1131 {
1132         struct wpabuf *cred;
1133
1134         if (wps->wps->registrar->skip_cred_build)
1135                 goto skip_cred_build;
1136
1137         wpa_printf(MSG_DEBUG, "WPS:  * Credential");
1138         os_memset(&wps->cred, 0, sizeof(wps->cred));
1139
1140         os_memcpy(wps->cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
1141         wps->cred.ssid_len = wps->wps->ssid_len;
1142
1143         /* Select the best authentication and encryption type */
1144         if (wps->auth_type & WPS_AUTH_WPA2PSK)
1145                 wps->auth_type = WPS_AUTH_WPA2PSK;
1146         else if (wps->auth_type & WPS_AUTH_WPAPSK)
1147                 wps->auth_type = WPS_AUTH_WPAPSK;
1148         else if (wps->auth_type & WPS_AUTH_OPEN)
1149                 wps->auth_type = WPS_AUTH_OPEN;
1150         else if (wps->auth_type & WPS_AUTH_SHARED)
1151                 wps->auth_type = WPS_AUTH_SHARED;
1152         else {
1153                 wpa_printf(MSG_DEBUG, "WPS: Unsupported auth_type 0x%x",
1154                            wps->auth_type);
1155                 return -1;
1156         }
1157         wps->cred.auth_type = wps->auth_type;
1158
1159         if (wps->auth_type == WPS_AUTH_WPA2PSK ||
1160             wps->auth_type == WPS_AUTH_WPAPSK) {
1161                 if (wps->encr_type & WPS_ENCR_AES)
1162                         wps->encr_type = WPS_ENCR_AES;
1163                 else if (wps->encr_type & WPS_ENCR_TKIP)
1164                         wps->encr_type = WPS_ENCR_TKIP;
1165                 else {
1166                         wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1167                                    "type for WPA/WPA2");
1168                         return -1;
1169                 }
1170         } else {
1171                 if (wps->encr_type & WPS_ENCR_WEP)
1172                         wps->encr_type = WPS_ENCR_WEP;
1173                 else if (wps->encr_type & WPS_ENCR_NONE)
1174                         wps->encr_type = WPS_ENCR_NONE;
1175                 else {
1176                         wpa_printf(MSG_DEBUG, "WPS: No suitable encryption "
1177                                    "type for non-WPA/WPA2 mode");
1178                         return -1;
1179                 }
1180         }
1181         wps->cred.encr_type = wps->encr_type;
1182         /* Set MAC address in the Credential to be the AP's address (BSSID) */
1183         os_memcpy(wps->cred.mac_addr, wps->wps->dev.mac_addr, ETH_ALEN);
1184
1185         if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->wps->ap &&
1186             !wps->wps->registrar->disable_auto_conf) {
1187                 u8 r[16];
1188                 /* Generate a random passphrase */
1189                 if (os_get_random(r, sizeof(r)) < 0)
1190                         return -1;
1191                 os_free(wps->new_psk);
1192                 wps->new_psk = base64_encode(r, sizeof(r), &wps->new_psk_len);
1193                 if (wps->new_psk == NULL)
1194                         return -1;
1195                 wps->new_psk_len--; /* remove newline */
1196                 while (wps->new_psk_len &&
1197                        wps->new_psk[wps->new_psk_len - 1] == '=')
1198                         wps->new_psk_len--;
1199                 wpa_hexdump_ascii_key(MSG_DEBUG, "WPS: Generated passphrase",
1200                                       wps->new_psk, wps->new_psk_len);
1201                 os_memcpy(wps->cred.key, wps->new_psk, wps->new_psk_len);
1202                 wps->cred.key_len = wps->new_psk_len;
1203         } else if (wps->wps->network_key) {
1204                 os_memcpy(wps->cred.key, wps->wps->network_key,
1205                           wps->wps->network_key_len);
1206                 wps->cred.key_len = wps->wps->network_key_len;
1207         } else if (wps->auth_type & (WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK)) {
1208                 char hex[65];
1209                 /* Generate a random per-device PSK */
1210                 os_free(wps->new_psk);
1211                 wps->new_psk_len = 32;
1212                 wps->new_psk = os_malloc(wps->new_psk_len);
1213                 if (wps->new_psk == NULL)
1214                         return -1;
1215                 if (os_get_random(wps->new_psk, wps->new_psk_len) < 0) {
1216                         os_free(wps->new_psk);
1217                         wps->new_psk = NULL;
1218                         return -1;
1219                 }
1220                 wpa_hexdump_key(MSG_DEBUG, "WPS: Generated per-device PSK",
1221                                 wps->new_psk, wps->new_psk_len);
1222                 wpa_snprintf_hex(hex, sizeof(hex), wps->new_psk,
1223                                  wps->new_psk_len);
1224                 os_memcpy(wps->cred.key, hex, wps->new_psk_len * 2);
1225                 wps->cred.key_len = wps->new_psk_len * 2;
1226         }
1227
1228         cred = wpabuf_alloc(200);
1229         if (cred == NULL)
1230                 return -1;
1231
1232         if (wps_build_credential(cred, &wps->cred)) {
1233                 wpabuf_free(cred);
1234                 return -1;
1235         }
1236
1237         wpabuf_put_be16(msg, ATTR_CRED);
1238         wpabuf_put_be16(msg, wpabuf_len(cred));
1239         wpabuf_put_buf(msg, cred);
1240         wpabuf_free(cred);
1241
1242 skip_cred_build:
1243         if (wps->wps->registrar->extra_cred) {
1244                 wpa_printf(MSG_DEBUG, "WPS:  * Credential (pre-configured)");
1245                 wpabuf_put_buf(msg, wps->wps->registrar->extra_cred);
1246         }
1247
1248         return 0;
1249 }
1250
1251
1252 static int wps_build_ap_settings(struct wps_data *wps, struct wpabuf *msg)
1253 {
1254         wpa_printf(MSG_DEBUG, "WPS:  * AP Settings");
1255
1256         if (wps_build_credential(msg, &wps->cred))
1257                 return -1;
1258
1259         return 0;
1260 }
1261
1262
1263 static struct wpabuf * wps_build_m2(struct wps_data *wps)
1264 {
1265         struct wpabuf *msg;
1266
1267         if (os_get_random(wps->nonce_r, WPS_NONCE_LEN) < 0)
1268                 return NULL;
1269         wpa_hexdump(MSG_DEBUG, "WPS: Registrar Nonce",
1270                     wps->nonce_r, WPS_NONCE_LEN);
1271         wpa_hexdump(MSG_DEBUG, "WPS: UUID-R", wps->uuid_r, WPS_UUID_LEN);
1272
1273         wpa_printf(MSG_DEBUG, "WPS: Building Message M2");
1274         msg = wpabuf_alloc(1000);
1275         if (msg == NULL)
1276                 return NULL;
1277
1278         if (wps_build_version(msg) ||
1279             wps_build_msg_type(msg, WPS_M2) ||
1280             wps_build_enrollee_nonce(wps, msg) ||
1281             wps_build_registrar_nonce(wps, msg) ||
1282             wps_build_uuid_r(wps, msg) ||
1283             wps_build_public_key(wps, msg) ||
1284             wps_derive_keys(wps) ||
1285             wps_build_auth_type_flags(wps, msg) ||
1286             wps_build_encr_type_flags(wps, msg) ||
1287             wps_build_conn_type_flags(wps, msg) ||
1288             wps_build_config_methods_r(wps->wps->registrar, msg) ||
1289             wps_build_device_attrs(&wps->wps->dev, msg) ||
1290             wps_build_rf_bands(&wps->wps->dev, msg) ||
1291             wps_build_assoc_state(wps, msg) ||
1292             wps_build_config_error(msg, WPS_CFG_NO_ERROR) ||
1293             wps_build_dev_password_id(msg, wps->dev_pw_id) ||
1294             wps_build_os_version(&wps->wps->dev, msg) ||
1295             wps_build_authenticator(wps, msg)) {
1296                 wpabuf_free(msg);
1297                 return NULL;
1298         }
1299
1300         wps->state = RECV_M3;
1301         return msg;
1302 }
1303
1304
1305 static struct wpabuf * wps_build_m2d(struct wps_data *wps)
1306 {
1307         struct wpabuf *msg;
1308         u16 err = wps->config_error;
1309
1310         wpa_printf(MSG_DEBUG, "WPS: Building Message M2D");
1311         msg = wpabuf_alloc(1000);
1312         if (msg == NULL)
1313                 return NULL;
1314
1315         if (wps->wps->ap && wps->wps->ap_setup_locked &&
1316             err == WPS_CFG_NO_ERROR)
1317                 err = WPS_CFG_SETUP_LOCKED;
1318
1319         if (wps_build_version(msg) ||
1320             wps_build_msg_type(msg, WPS_M2D) ||
1321             wps_build_enrollee_nonce(wps, msg) ||
1322             wps_build_registrar_nonce(wps, msg) ||
1323             wps_build_uuid_r(wps, msg) ||
1324             wps_build_auth_type_flags(wps, msg) ||
1325             wps_build_encr_type_flags(wps, msg) ||
1326             wps_build_conn_type_flags(wps, msg) ||
1327             wps_build_config_methods_r(wps->wps->registrar, msg) ||
1328             wps_build_device_attrs(&wps->wps->dev, msg) ||
1329             wps_build_rf_bands(&wps->wps->dev, msg) ||
1330             wps_build_assoc_state(wps, msg) ||
1331             wps_build_config_error(msg, err) ||
1332             wps_build_os_version(&wps->wps->dev, msg)) {
1333                 wpabuf_free(msg);
1334                 return NULL;
1335         }
1336
1337         wps->state = RECV_M2D_ACK;
1338         return msg;
1339 }
1340
1341
1342 static struct wpabuf * wps_build_m4(struct wps_data *wps)
1343 {
1344         struct wpabuf *msg, *plain;
1345
1346         wpa_printf(MSG_DEBUG, "WPS: Building Message M4");
1347
1348         wps_derive_psk(wps, wps->dev_password, wps->dev_password_len);
1349
1350         plain = wpabuf_alloc(200);
1351         if (plain == NULL)
1352                 return NULL;
1353
1354         msg = wpabuf_alloc(1000);
1355         if (msg == NULL) {
1356                 wpabuf_free(plain);
1357                 return NULL;
1358         }
1359
1360         if (wps_build_version(msg) ||
1361             wps_build_msg_type(msg, WPS_M4) ||
1362             wps_build_enrollee_nonce(wps, msg) ||
1363             wps_build_r_hash(wps, msg) ||
1364             wps_build_r_snonce1(wps, plain) ||
1365             wps_build_key_wrap_auth(wps, plain) ||
1366             wps_build_encr_settings(wps, msg, plain) ||
1367             wps_build_authenticator(wps, msg)) {
1368                 wpabuf_free(plain);
1369                 wpabuf_free(msg);
1370                 return NULL;
1371         }
1372         wpabuf_free(plain);
1373
1374         wps->state = RECV_M5;
1375         return msg;
1376 }
1377
1378
1379 static struct wpabuf * wps_build_m6(struct wps_data *wps)
1380 {
1381         struct wpabuf *msg, *plain;
1382
1383         wpa_printf(MSG_DEBUG, "WPS: Building Message M6");
1384
1385         plain = wpabuf_alloc(200);
1386         if (plain == NULL)
1387                 return NULL;
1388
1389         msg = wpabuf_alloc(1000);
1390         if (msg == NULL) {
1391                 wpabuf_free(plain);
1392                 return NULL;
1393         }
1394
1395         if (wps_build_version(msg) ||
1396             wps_build_msg_type(msg, WPS_M6) ||
1397             wps_build_enrollee_nonce(wps, msg) ||
1398             wps_build_r_snonce2(wps, plain) ||
1399             wps_build_key_wrap_auth(wps, plain) ||
1400             wps_build_encr_settings(wps, msg, plain) ||
1401             wps_build_authenticator(wps, msg)) {
1402                 wpabuf_free(plain);
1403                 wpabuf_free(msg);
1404                 return NULL;
1405         }
1406         wpabuf_free(plain);
1407
1408         wps->wps_pin_revealed = 1;
1409         wps->state = RECV_M7;
1410         return msg;
1411 }
1412
1413
1414 static struct wpabuf * wps_build_m8(struct wps_data *wps)
1415 {
1416         struct wpabuf *msg, *plain;
1417
1418         wpa_printf(MSG_DEBUG, "WPS: Building Message M8");
1419
1420         plain = wpabuf_alloc(500);
1421         if (plain == NULL)
1422                 return NULL;
1423
1424         msg = wpabuf_alloc(1000);
1425         if (msg == NULL) {
1426                 wpabuf_free(plain);
1427                 return NULL;
1428         }
1429
1430         if (wps_build_version(msg) ||
1431             wps_build_msg_type(msg, WPS_M8) ||
1432             wps_build_enrollee_nonce(wps, msg) ||
1433             (wps->wps->ap && wps_build_cred(wps, plain)) ||
1434             (!wps->wps->ap && wps_build_ap_settings(wps, plain)) ||
1435             wps_build_key_wrap_auth(wps, plain) ||
1436             wps_build_encr_settings(wps, msg, plain) ||
1437             wps_build_authenticator(wps, msg)) {
1438                 wpabuf_free(plain);
1439                 wpabuf_free(msg);
1440                 return NULL;
1441         }
1442         wpabuf_free(plain);
1443
1444         wps->state = RECV_DONE;
1445         return msg;
1446 }
1447
1448
1449 static struct wpabuf * wps_build_wsc_ack(struct wps_data *wps)
1450 {
1451         struct wpabuf *msg;
1452
1453         wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_ACK");
1454
1455         msg = wpabuf_alloc(1000);
1456         if (msg == NULL)
1457                 return NULL;
1458
1459         if (wps_build_version(msg) ||
1460             wps_build_msg_type(msg, WPS_WSC_ACK) ||
1461             wps_build_enrollee_nonce(wps, msg) ||
1462             wps_build_registrar_nonce(wps, msg)) {
1463                 wpabuf_free(msg);
1464                 return NULL;
1465         }
1466
1467         return msg;
1468 }
1469
1470
1471 static struct wpabuf * wps_build_wsc_nack(struct wps_data *wps)
1472 {
1473         struct wpabuf *msg;
1474
1475         wpa_printf(MSG_DEBUG, "WPS: Building Message WSC_NACK");
1476
1477         msg = wpabuf_alloc(1000);
1478         if (msg == NULL)
1479                 return NULL;
1480
1481         if (wps_build_version(msg) ||
1482             wps_build_msg_type(msg, WPS_WSC_NACK) ||
1483             wps_build_enrollee_nonce(wps, msg) ||
1484             wps_build_registrar_nonce(wps, msg) ||
1485             wps_build_config_error(msg, wps->config_error)) {
1486                 wpabuf_free(msg);
1487                 return NULL;
1488         }
1489
1490         return msg;
1491 }
1492
1493
1494 struct wpabuf * wps_registrar_get_msg(struct wps_data *wps,
1495                                       enum wsc_op_code *op_code)
1496 {
1497         struct wpabuf *msg;
1498
1499 #ifdef CONFIG_WPS_UPNP
1500         if (wps->wps->wps_upnp) {
1501                 struct upnp_pending_message *p, *prev = NULL;
1502                 if (wps->ext_reg > 1)
1503                         wps_registrar_free_pending_m2(wps->wps);
1504                 p = wps->wps->upnp_msgs;
1505                 /* TODO: check pending message MAC address */
1506                 while (p && p->next) {
1507                         prev = p;
1508                         p = p->next;
1509                 }
1510                 if (p) {
1511                         wpa_printf(MSG_DEBUG, "WPS: Use pending message from "
1512                                    "UPnP");
1513                         if (prev)
1514                                 prev->next = NULL;
1515                         else
1516                                 wps->wps->upnp_msgs = NULL;
1517                         msg = p->msg;
1518                         os_free(p);
1519                         *op_code = WSC_MSG;
1520                         if (wps->ext_reg == 0)
1521                                 wps->ext_reg = 1;
1522                         return msg;
1523                 }
1524         }
1525         if (wps->ext_reg) {
1526                 wpa_printf(MSG_DEBUG, "WPS: Using external Registrar, but no "
1527                            "pending message available");
1528                 return NULL;
1529         }
1530 #endif /* CONFIG_WPS_UPNP */
1531
1532         switch (wps->state) {
1533         case SEND_M2:
1534                 if (wps_get_dev_password(wps) < 0)
1535                         msg = wps_build_m2d(wps);
1536                 else
1537                         msg = wps_build_m2(wps);
1538                 *op_code = WSC_MSG;
1539                 break;
1540         case SEND_M2D:
1541                 msg = wps_build_m2d(wps);
1542                 *op_code = WSC_MSG;
1543                 break;
1544         case SEND_M4:
1545                 msg = wps_build_m4(wps);
1546                 *op_code = WSC_MSG;
1547                 break;
1548         case SEND_M6:
1549                 msg = wps_build_m6(wps);
1550                 *op_code = WSC_MSG;
1551                 break;
1552         case SEND_M8:
1553                 msg = wps_build_m8(wps);
1554                 *op_code = WSC_MSG;
1555                 break;
1556         case RECV_DONE:
1557                 msg = wps_build_wsc_ack(wps);
1558                 *op_code = WSC_ACK;
1559                 break;
1560         case SEND_WSC_NACK:
1561                 msg = wps_build_wsc_nack(wps);
1562                 *op_code = WSC_NACK;
1563                 break;
1564         default:
1565                 wpa_printf(MSG_DEBUG, "WPS: Unsupported state %d for building "
1566                            "a message", wps->state);
1567                 msg = NULL;
1568                 break;
1569         }
1570
1571         if (*op_code == WSC_MSG && msg) {
1572                 /* Save a copy of the last message for Authenticator derivation
1573                  */
1574                 wpabuf_free(wps->last_msg);
1575                 wps->last_msg = wpabuf_dup(msg);
1576         }
1577
1578         return msg;
1579 }
1580
1581
1582 static int wps_process_enrollee_nonce(struct wps_data *wps, const u8 *e_nonce)
1583 {
1584         if (e_nonce == NULL) {
1585                 wpa_printf(MSG_DEBUG, "WPS: No Enrollee Nonce received");
1586                 return -1;
1587         }
1588
1589         os_memcpy(wps->nonce_e, e_nonce, WPS_NONCE_LEN);
1590         wpa_hexdump(MSG_DEBUG, "WPS: Enrollee Nonce",
1591                     wps->nonce_e, WPS_NONCE_LEN);
1592
1593         return 0;
1594 }
1595
1596
1597 static int wps_process_registrar_nonce(struct wps_data *wps, const u8 *r_nonce)
1598 {
1599         if (r_nonce == NULL) {
1600                 wpa_printf(MSG_DEBUG, "WPS: No Registrar Nonce received");
1601                 return -1;
1602         }
1603
1604         if (os_memcmp(wps->nonce_r, r_nonce, WPS_NONCE_LEN) != 0) {
1605                 wpa_printf(MSG_DEBUG, "WPS: Invalid Registrar Nonce received");
1606                 return -1;
1607         }
1608
1609         return 0;
1610 }
1611
1612
1613 static int wps_process_uuid_e(struct wps_data *wps, const u8 *uuid_e)
1614 {
1615         if (uuid_e == NULL) {
1616                 wpa_printf(MSG_DEBUG, "WPS: No UUID-E received");
1617                 return -1;
1618         }
1619
1620         os_memcpy(wps->uuid_e, uuid_e, WPS_UUID_LEN);
1621         wpa_hexdump(MSG_DEBUG, "WPS: UUID-E", wps->uuid_e, WPS_UUID_LEN);
1622
1623         return 0;
1624 }
1625
1626
1627 static int wps_process_dev_password_id(struct wps_data *wps, const u8 *pw_id)
1628 {
1629         if (pw_id == NULL) {
1630                 wpa_printf(MSG_DEBUG, "WPS: No Device Password ID received");
1631                 return -1;
1632         }
1633
1634         wps->dev_pw_id = WPA_GET_BE16(pw_id);
1635         wpa_printf(MSG_DEBUG, "WPS: Device Password ID %d", wps->dev_pw_id);
1636
1637         return 0;
1638 }
1639
1640
1641 static int wps_process_e_hash1(struct wps_data *wps, const u8 *e_hash1)
1642 {
1643         if (e_hash1 == NULL) {
1644                 wpa_printf(MSG_DEBUG, "WPS: No E-Hash1 received");
1645                 return -1;
1646         }
1647
1648         os_memcpy(wps->peer_hash1, e_hash1, WPS_HASH_LEN);
1649         wpa_hexdump(MSG_DEBUG, "WPS: E-Hash1", wps->peer_hash1, WPS_HASH_LEN);
1650
1651         return 0;
1652 }
1653
1654
1655 static int wps_process_e_hash2(struct wps_data *wps, const u8 *e_hash2)
1656 {
1657         if (e_hash2 == NULL) {
1658                 wpa_printf(MSG_DEBUG, "WPS: No E-Hash2 received");
1659                 return -1;
1660         }
1661
1662         os_memcpy(wps->peer_hash2, e_hash2, WPS_HASH_LEN);
1663         wpa_hexdump(MSG_DEBUG, "WPS: E-Hash2", wps->peer_hash2, WPS_HASH_LEN);
1664
1665         return 0;
1666 }
1667
1668
1669 static int wps_process_e_snonce1(struct wps_data *wps, const u8 *e_snonce1)
1670 {
1671         u8 hash[SHA256_MAC_LEN];
1672         const u8 *addr[4];
1673         size_t len[4];
1674
1675         if (e_snonce1 == NULL) {
1676                 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce1 received");
1677                 return -1;
1678         }
1679
1680         wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce1", e_snonce1,
1681                         WPS_SECRET_NONCE_LEN);
1682
1683         /* E-Hash1 = HMAC_AuthKey(E-S1 || PSK1 || PK_E || PK_R) */
1684         addr[0] = e_snonce1;
1685         len[0] = WPS_SECRET_NONCE_LEN;
1686         addr[1] = wps->psk1;
1687         len[1] = WPS_PSK_LEN;
1688         addr[2] = wpabuf_head(wps->dh_pubkey_e);
1689         len[2] = wpabuf_len(wps->dh_pubkey_e);
1690         addr[3] = wpabuf_head(wps->dh_pubkey_r);
1691         len[3] = wpabuf_len(wps->dh_pubkey_r);
1692         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1693
1694         if (os_memcmp(wps->peer_hash1, hash, WPS_HASH_LEN) != 0) {
1695                 wpa_printf(MSG_DEBUG, "WPS: E-Hash1 derived from E-S1 does "
1696                            "not match with the pre-committed value");
1697                 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1698                 wps_pwd_auth_fail_event(wps->wps, 0, 1);
1699                 return -1;
1700         }
1701
1702         wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the first "
1703                    "half of the device password");
1704
1705         return 0;
1706 }
1707
1708
1709 static int wps_process_e_snonce2(struct wps_data *wps, const u8 *e_snonce2)
1710 {
1711         u8 hash[SHA256_MAC_LEN];
1712         const u8 *addr[4];
1713         size_t len[4];
1714
1715         if (e_snonce2 == NULL) {
1716                 wpa_printf(MSG_DEBUG, "WPS: No E-SNonce2 received");
1717                 return -1;
1718         }
1719
1720         wpa_hexdump_key(MSG_DEBUG, "WPS: E-SNonce2", e_snonce2,
1721                         WPS_SECRET_NONCE_LEN);
1722
1723         /* E-Hash2 = HMAC_AuthKey(E-S2 || PSK2 || PK_E || PK_R) */
1724         addr[0] = e_snonce2;
1725         len[0] = WPS_SECRET_NONCE_LEN;
1726         addr[1] = wps->psk2;
1727         len[1] = WPS_PSK_LEN;
1728         addr[2] = wpabuf_head(wps->dh_pubkey_e);
1729         len[2] = wpabuf_len(wps->dh_pubkey_e);
1730         addr[3] = wpabuf_head(wps->dh_pubkey_r);
1731         len[3] = wpabuf_len(wps->dh_pubkey_r);
1732         hmac_sha256_vector(wps->authkey, WPS_AUTHKEY_LEN, 4, addr, len, hash);
1733
1734         if (os_memcmp(wps->peer_hash2, hash, WPS_HASH_LEN) != 0) {
1735                 wpa_printf(MSG_DEBUG, "WPS: E-Hash2 derived from E-S2 does "
1736                            "not match with the pre-committed value");
1737                 wps_registrar_invalidate_pin(wps->wps->registrar, wps->uuid_e);
1738                 wps->config_error = WPS_CFG_DEV_PASSWORD_AUTH_FAILURE;
1739                 wps_pwd_auth_fail_event(wps->wps, 0, 2);
1740                 return -1;
1741         }
1742
1743         wpa_printf(MSG_DEBUG, "WPS: Enrollee proved knowledge of the second "
1744                    "half of the device password");
1745         wps->wps_pin_revealed = 0;
1746         wps_registrar_unlock_pin(wps->wps->registrar, wps->uuid_e);
1747
1748         return 0;
1749 }
1750
1751
1752 static int wps_process_mac_addr(struct wps_data *wps, const u8 *mac_addr)
1753 {
1754         if (mac_addr == NULL) {
1755                 wpa_printf(MSG_DEBUG, "WPS: No MAC Address received");
1756                 return -1;
1757         }
1758
1759         wpa_printf(MSG_DEBUG, "WPS: Enrollee MAC Address " MACSTR,
1760                    MAC2STR(mac_addr));
1761         os_memcpy(wps->mac_addr_e, mac_addr, ETH_ALEN);
1762         os_memcpy(wps->peer_dev.mac_addr, mac_addr, ETH_ALEN);
1763
1764         return 0;
1765 }
1766
1767
1768 static int wps_process_pubkey(struct wps_data *wps, const u8 *pk,
1769                               size_t pk_len)
1770 {
1771         if (pk == NULL || pk_len == 0) {
1772                 wpa_printf(MSG_DEBUG, "WPS: No Public Key received");
1773                 return -1;
1774         }
1775
1776 #ifdef CONFIG_WPS_OOB
1777         if (wps->wps->oob_conf.pubkey_hash != NULL) {
1778                 const u8 *addr[1];
1779                 u8 hash[WPS_HASH_LEN];
1780
1781                 addr[0] = pk;
1782                 sha256_vector(1, addr, &pk_len, hash);
1783                 if (os_memcmp(hash,
1784                               wpabuf_head(wps->wps->oob_conf.pubkey_hash),
1785                               WPS_OOB_PUBKEY_HASH_LEN) != 0) {
1786                         wpa_printf(MSG_ERROR, "WPS: Public Key hash error");
1787                         return -1;
1788                 }
1789         }
1790 #endif /* CONFIG_WPS_OOB */
1791
1792         wpabuf_free(wps->dh_pubkey_e);
1793         wps->dh_pubkey_e = wpabuf_alloc_copy(pk, pk_len);
1794         if (wps->dh_pubkey_e == NULL)
1795                 return -1;
1796
1797         return 0;
1798 }
1799
1800
1801 static int wps_process_auth_type_flags(struct wps_data *wps, const u8 *auth)
1802 {
1803         u16 auth_types;
1804
1805         if (auth == NULL) {
1806                 wpa_printf(MSG_DEBUG, "WPS: No Authentication Type flags "
1807                            "received");
1808                 return -1;
1809         }
1810
1811         auth_types = WPA_GET_BE16(auth);
1812
1813         wpa_printf(MSG_DEBUG, "WPS: Enrollee Authentication Type flags 0x%x",
1814                    auth_types);
1815         wps->auth_type = wps->wps->auth_types & auth_types;
1816         if (wps->auth_type == 0) {
1817                 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1818                            "authentication types (own 0x%x Enrollee 0x%x)",
1819                            wps->wps->auth_types, auth_types);
1820 #ifdef WPS_WORKAROUNDS
1821                 /*
1822                  * Some deployed implementations seem to advertise incorrect
1823                  * information in this attribute. For example, Linksys WRT350N
1824                  * seems to have a byteorder bug that breaks this negotiation.
1825                  * In order to interoperate with existing implementations,
1826                  * assume that the Enrollee supports everything we do.
1827                  */
1828                 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1829                            "does not advertise supported authentication types "
1830                            "correctly");
1831                 wps->auth_type = wps->wps->auth_types;
1832 #else /* WPS_WORKAROUNDS */
1833                 return -1;
1834 #endif /* WPS_WORKAROUNDS */
1835         }
1836
1837         return 0;
1838 }
1839
1840
1841 static int wps_process_encr_type_flags(struct wps_data *wps, const u8 *encr)
1842 {
1843         u16 encr_types;
1844
1845         if (encr == NULL) {
1846                 wpa_printf(MSG_DEBUG, "WPS: No Encryption Type flags "
1847                            "received");
1848                 return -1;
1849         }
1850
1851         encr_types = WPA_GET_BE16(encr);
1852
1853         wpa_printf(MSG_DEBUG, "WPS: Enrollee Encryption Type flags 0x%x",
1854                    encr_types);
1855         wps->encr_type = wps->wps->encr_types & encr_types;
1856         if (wps->encr_type == 0) {
1857                 wpa_printf(MSG_DEBUG, "WPS: No match in supported "
1858                            "encryption types (own 0x%x Enrollee 0x%x)",
1859                            wps->wps->encr_types, encr_types);
1860 #ifdef WPS_WORKAROUNDS
1861                 /*
1862                  * Some deployed implementations seem to advertise incorrect
1863                  * information in this attribute. For example, Linksys WRT350N
1864                  * seems to have a byteorder bug that breaks this negotiation.
1865                  * In order to interoperate with existing implementations,
1866                  * assume that the Enrollee supports everything we do.
1867                  */
1868                 wpa_printf(MSG_DEBUG, "WPS: Workaround - assume Enrollee "
1869                            "does not advertise supported encryption types "
1870                            "correctly");
1871                 wps->encr_type = wps->wps->encr_types;
1872 #else /* WPS_WORKAROUNDS */
1873                 return -1;
1874 #endif /* WPS_WORKAROUNDS */
1875         }
1876
1877         return 0;
1878 }
1879
1880
1881 static int wps_process_conn_type_flags(struct wps_data *wps, const u8 *conn)
1882 {
1883         if (conn == NULL) {
1884                 wpa_printf(MSG_DEBUG, "WPS: No Connection Type flags "
1885                            "received");
1886                 return -1;
1887         }
1888
1889         wpa_printf(MSG_DEBUG, "WPS: Enrollee Connection Type flags 0x%x",
1890                    *conn);
1891
1892         return 0;
1893 }
1894
1895
1896 static int wps_process_config_methods(struct wps_data *wps, const u8 *methods)
1897 {
1898         u16 m;
1899
1900         if (methods == NULL) {
1901                 wpa_printf(MSG_DEBUG, "WPS: No Config Methods received");
1902                 return -1;
1903         }
1904
1905         m = WPA_GET_BE16(methods);
1906
1907         wpa_printf(MSG_DEBUG, "WPS: Enrollee Config Methods 0x%x", m);
1908
1909         return 0;
1910 }
1911
1912
1913 static int wps_process_wps_state(struct wps_data *wps, const u8 *state)
1914 {
1915         if (state == NULL) {
1916                 wpa_printf(MSG_DEBUG, "WPS: No Wi-Fi Protected Setup State "
1917                            "received");
1918                 return -1;
1919         }
1920
1921         wpa_printf(MSG_DEBUG, "WPS: Enrollee Wi-Fi Protected Setup State %d",
1922                    *state);
1923
1924         return 0;
1925 }
1926
1927
1928 static int wps_process_assoc_state(struct wps_data *wps, const u8 *assoc)
1929 {
1930         u16 a;
1931
1932         if (assoc == NULL) {
1933                 wpa_printf(MSG_DEBUG, "WPS: No Association State received");
1934                 return -1;
1935         }
1936
1937         a = WPA_GET_BE16(assoc);
1938         wpa_printf(MSG_DEBUG, "WPS: Enrollee Association State %d", a);
1939
1940         return 0;
1941 }
1942
1943
1944 static int wps_process_config_error(struct wps_data *wps, const u8 *err)
1945 {
1946         u16 e;
1947
1948         if (err == NULL) {
1949                 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error received");
1950                 return -1;
1951         }
1952
1953         e = WPA_GET_BE16(err);
1954         wpa_printf(MSG_DEBUG, "WPS: Enrollee Configuration Error %d", e);
1955
1956         return 0;
1957 }
1958
1959
1960 static enum wps_process_res wps_process_m1(struct wps_data *wps,
1961                                            struct wps_parse_attr *attr)
1962 {
1963         wpa_printf(MSG_DEBUG, "WPS: Received M1");
1964
1965         if (wps->state != RECV_M1) {
1966                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
1967                            "receiving M1", wps->state);
1968                 return WPS_FAILURE;
1969         }
1970
1971         if (wps_process_uuid_e(wps, attr->uuid_e) ||
1972             wps_process_mac_addr(wps, attr->mac_addr) ||
1973             wps_process_enrollee_nonce(wps, attr->enrollee_nonce) ||
1974             wps_process_pubkey(wps, attr->public_key, attr->public_key_len) ||
1975             wps_process_auth_type_flags(wps, attr->auth_type_flags) ||
1976             wps_process_encr_type_flags(wps, attr->encr_type_flags) ||
1977             wps_process_conn_type_flags(wps, attr->conn_type_flags) ||
1978             wps_process_config_methods(wps, attr->config_methods) ||
1979             wps_process_wps_state(wps, attr->wps_state) ||
1980             wps_process_device_attrs(&wps->peer_dev, attr) ||
1981             wps_process_rf_bands(&wps->peer_dev, attr->rf_bands) ||
1982             wps_process_assoc_state(wps, attr->assoc_state) ||
1983             wps_process_dev_password_id(wps, attr->dev_password_id) ||
1984             wps_process_config_error(wps, attr->config_error) ||
1985             wps_process_os_version(&wps->peer_dev, attr->os_version))
1986                 return WPS_FAILURE;
1987
1988         if (wps->dev_pw_id < 0x10 &&
1989             wps->dev_pw_id != DEV_PW_DEFAULT &&
1990             wps->dev_pw_id != DEV_PW_USER_SPECIFIED &&
1991             wps->dev_pw_id != DEV_PW_MACHINE_SPECIFIED &&
1992             wps->dev_pw_id != DEV_PW_REGISTRAR_SPECIFIED &&
1993             (wps->dev_pw_id != DEV_PW_PUSHBUTTON ||
1994              !wps->wps->registrar->pbc)) {
1995                 wpa_printf(MSG_DEBUG, "WPS: Unsupported Device Password ID %d",
1996                            wps->dev_pw_id);
1997                 wps->state = SEND_M2D;
1998                 return WPS_CONTINUE;
1999         }
2000
2001 #ifdef CONFIG_WPS_OOB
2002         if (wps->dev_pw_id >= 0x10 &&
2003             wps->dev_pw_id != wps->wps->oob_dev_pw_id) {
2004                 wpa_printf(MSG_DEBUG, "WPS: OOB Device Password ID "
2005                            "%d mismatch", wps->dev_pw_id);
2006                 wps->state = SEND_M2D;
2007                 return WPS_CONTINUE;
2008         }
2009 #endif /* CONFIG_WPS_OOB */
2010
2011         if (wps->dev_pw_id == DEV_PW_PUSHBUTTON) {
2012                 if (wps_registrar_pbc_overlap(wps->wps->registrar,
2013                                               wps->mac_addr_e, wps->uuid_e)) {
2014                         wpa_printf(MSG_DEBUG, "WPS: PBC overlap - deny PBC "
2015                                    "negotiation");
2016                         wps->state = SEND_M2D;
2017                         wps->config_error = WPS_CFG_MULTIPLE_PBC_DETECTED;
2018                         return WPS_CONTINUE;
2019                 }
2020                 wps_registrar_add_pbc_session(wps->wps->registrar,
2021                                               wps->mac_addr_e, wps->uuid_e);
2022                 wps->pbc = 1;
2023         }
2024
2025         wps->state = SEND_M2;
2026         return WPS_CONTINUE;
2027 }
2028
2029
2030 static enum wps_process_res wps_process_m3(struct wps_data *wps,
2031                                            const struct wpabuf *msg,
2032                                            struct wps_parse_attr *attr)
2033 {
2034         wpa_printf(MSG_DEBUG, "WPS: Received M3");
2035
2036         if (wps->state != RECV_M3) {
2037                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2038                            "receiving M3", wps->state);
2039                 wps->state = SEND_WSC_NACK;
2040                 return WPS_CONTINUE;
2041         }
2042
2043         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2044             wps_process_authenticator(wps, attr->authenticator, msg) ||
2045             wps_process_e_hash1(wps, attr->e_hash1) ||
2046             wps_process_e_hash2(wps, attr->e_hash2)) {
2047                 wps->state = SEND_WSC_NACK;
2048                 return WPS_CONTINUE;
2049         }
2050
2051         wps->state = SEND_M4;
2052         return WPS_CONTINUE;
2053 }
2054
2055
2056 static enum wps_process_res wps_process_m5(struct wps_data *wps,
2057                                            const struct wpabuf *msg,
2058                                            struct wps_parse_attr *attr)
2059 {
2060         struct wpabuf *decrypted;
2061         struct wps_parse_attr eattr;
2062
2063         wpa_printf(MSG_DEBUG, "WPS: Received M5");
2064
2065         if (wps->state != RECV_M5) {
2066                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2067                            "receiving M5", wps->state);
2068                 wps->state = SEND_WSC_NACK;
2069                 return WPS_CONTINUE;
2070         }
2071
2072         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2073             wps_process_authenticator(wps, attr->authenticator, msg)) {
2074                 wps->state = SEND_WSC_NACK;
2075                 return WPS_CONTINUE;
2076         }
2077
2078         decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2079                                               attr->encr_settings_len);
2080         if (decrypted == NULL) {
2081                 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2082                            "Settings attribute");
2083                 wps->state = SEND_WSC_NACK;
2084                 return WPS_CONTINUE;
2085         }
2086
2087         wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2088                    "attribute");
2089         if (wps_parse_msg(decrypted, &eattr) < 0 ||
2090             wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2091             wps_process_e_snonce1(wps, eattr.e_snonce1)) {
2092                 wpabuf_free(decrypted);
2093                 wps->state = SEND_WSC_NACK;
2094                 return WPS_CONTINUE;
2095         }
2096         wpabuf_free(decrypted);
2097
2098         wps->state = SEND_M6;
2099         return WPS_CONTINUE;
2100 }
2101
2102
2103 static void wps_sta_cred_cb(struct wps_data *wps)
2104 {
2105         /*
2106          * Update credential to only include a single authentication and
2107          * encryption type in case the AP configuration includes more than one
2108          * option.
2109          */
2110         if (wps->cred.auth_type & WPS_AUTH_WPA2PSK)
2111                 wps->cred.auth_type = WPS_AUTH_WPA2PSK;
2112         else if (wps->cred.auth_type & WPS_AUTH_WPAPSK)
2113                 wps->cred.auth_type = WPS_AUTH_WPAPSK;
2114         if (wps->cred.encr_type & WPS_ENCR_AES)
2115                 wps->cred.encr_type = WPS_ENCR_AES;
2116         else if (wps->cred.encr_type & WPS_ENCR_TKIP)
2117                 wps->cred.encr_type = WPS_ENCR_TKIP;
2118         wpa_printf(MSG_DEBUG, "WPS: Update local configuration based on the "
2119                    "AP configuration");
2120         if (wps->wps->cred_cb)
2121                 wps->wps->cred_cb(wps->wps->cb_ctx, &wps->cred);
2122 }
2123
2124
2125 static void wps_cred_update(struct wps_credential *dst,
2126                             struct wps_credential *src)
2127 {
2128         os_memcpy(dst->ssid, src->ssid, sizeof(dst->ssid));
2129         dst->ssid_len = src->ssid_len;
2130         dst->auth_type = src->auth_type;
2131         dst->encr_type = src->encr_type;
2132         dst->key_idx = src->key_idx;
2133         os_memcpy(dst->key, src->key, sizeof(dst->key));
2134         dst->key_len = src->key_len;
2135 }
2136
2137
2138 static int wps_process_ap_settings_r(struct wps_data *wps,
2139                                      struct wps_parse_attr *attr)
2140 {
2141         if (wps->wps->ap)
2142                 return 0;
2143
2144         /* AP Settings Attributes in M7 when Enrollee is an AP */
2145         if (wps_process_ap_settings(attr, &wps->cred) < 0)
2146                 return -1;
2147
2148         wpa_printf(MSG_INFO, "WPS: Received old AP configuration from AP");
2149
2150         if (wps->new_ap_settings) {
2151                 wpa_printf(MSG_INFO, "WPS: Update AP configuration based on "
2152                            "new settings");
2153                 wps_cred_update(&wps->cred, wps->new_ap_settings);
2154                 return 0;
2155         } else {
2156                 /*
2157                  * Use the AP PIN only to receive the current AP settings, not
2158                  * to reconfigure the AP.
2159                  */
2160                 wps_sta_cred_cb(wps);
2161                 return 1;
2162         }
2163 }
2164
2165
2166 static enum wps_process_res wps_process_m7(struct wps_data *wps,
2167                                            const struct wpabuf *msg,
2168                                            struct wps_parse_attr *attr)
2169 {
2170         struct wpabuf *decrypted;
2171         struct wps_parse_attr eattr;
2172
2173         wpa_printf(MSG_DEBUG, "WPS: Received M7");
2174
2175         if (wps->state != RECV_M7) {
2176                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2177                            "receiving M7", wps->state);
2178                 wps->state = SEND_WSC_NACK;
2179                 return WPS_CONTINUE;
2180         }
2181
2182         if (wps_process_registrar_nonce(wps, attr->registrar_nonce) ||
2183             wps_process_authenticator(wps, attr->authenticator, msg)) {
2184                 wps->state = SEND_WSC_NACK;
2185                 return WPS_CONTINUE;
2186         }
2187
2188         decrypted = wps_decrypt_encr_settings(wps, attr->encr_settings,
2189                                               attr->encr_settings_len);
2190         if (decrypted == NULL) {
2191                 wpa_printf(MSG_DEBUG, "WPS: Failed to decrypted Encrypted "
2192                            "Settings attribute");
2193                 wps->state = SEND_WSC_NACK;
2194                 return WPS_CONTINUE;
2195         }
2196
2197         wpa_printf(MSG_DEBUG, "WPS: Processing decrypted Encrypted Settings "
2198                    "attribute");
2199         if (wps_parse_msg(decrypted, &eattr) < 0 ||
2200             wps_process_key_wrap_auth(wps, decrypted, eattr.key_wrap_auth) ||
2201             wps_process_e_snonce2(wps, eattr.e_snonce2) ||
2202             wps_process_ap_settings_r(wps, &eattr)) {
2203                 wpabuf_free(decrypted);
2204                 wps->state = SEND_WSC_NACK;
2205                 return WPS_CONTINUE;
2206         }
2207
2208         wpabuf_free(decrypted);
2209
2210         wps->state = SEND_M8;
2211         return WPS_CONTINUE;
2212 }
2213
2214
2215 static enum wps_process_res wps_process_wsc_msg(struct wps_data *wps,
2216                                                 const struct wpabuf *msg)
2217 {
2218         struct wps_parse_attr attr;
2219         enum wps_process_res ret = WPS_CONTINUE;
2220
2221         wpa_printf(MSG_DEBUG, "WPS: Received WSC_MSG");
2222
2223         if (wps_parse_msg(msg, &attr) < 0)
2224                 return WPS_FAILURE;
2225
2226         if (!wps_version_supported(attr.version)) {
2227                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2228                            attr.version ? *attr.version : 0);
2229                 return WPS_FAILURE;
2230         }
2231
2232         if (attr.msg_type == NULL) {
2233                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2234                 return WPS_FAILURE;
2235         }
2236
2237         if (*attr.msg_type != WPS_M1 &&
2238             (attr.registrar_nonce == NULL ||
2239              os_memcmp(wps->nonce_r, attr.registrar_nonce,
2240                        WPS_NONCE_LEN != 0))) {
2241                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2242                 return WPS_FAILURE;
2243         }
2244
2245         switch (*attr.msg_type) {
2246         case WPS_M1:
2247 #ifdef CONFIG_WPS_UPNP
2248                 if (wps->wps->wps_upnp && attr.mac_addr) {
2249                         /* Remove old pending messages when starting new run */
2250                         wps_free_pending_msgs(wps->wps->upnp_msgs);
2251                         wps->wps->upnp_msgs = NULL;
2252
2253                         upnp_wps_device_send_wlan_event(
2254                                 wps->wps->wps_upnp, attr.mac_addr,
2255                                 UPNP_WPS_WLANEVENT_TYPE_EAP, msg);
2256                 }
2257 #endif /* CONFIG_WPS_UPNP */
2258                 ret = wps_process_m1(wps, &attr);
2259                 break;
2260         case WPS_M3:
2261                 ret = wps_process_m3(wps, msg, &attr);
2262                 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2263                         wps_fail_event(wps->wps, WPS_M3);
2264                 break;
2265         case WPS_M5:
2266                 ret = wps_process_m5(wps, msg, &attr);
2267                 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2268                         wps_fail_event(wps->wps, WPS_M5);
2269                 break;
2270         case WPS_M7:
2271                 ret = wps_process_m7(wps, msg, &attr);
2272                 if (ret == WPS_FAILURE || wps->state == SEND_WSC_NACK)
2273                         wps_fail_event(wps->wps, WPS_M7);
2274                 break;
2275         default:
2276                 wpa_printf(MSG_DEBUG, "WPS: Unsupported Message Type %d",
2277                            *attr.msg_type);
2278                 return WPS_FAILURE;
2279         }
2280
2281         if (ret == WPS_CONTINUE) {
2282                 /* Save a copy of the last message for Authenticator derivation
2283                  */
2284                 wpabuf_free(wps->last_msg);
2285                 wps->last_msg = wpabuf_dup(msg);
2286         }
2287
2288         return ret;
2289 }
2290
2291
2292 static enum wps_process_res wps_process_wsc_ack(struct wps_data *wps,
2293                                                 const struct wpabuf *msg)
2294 {
2295         struct wps_parse_attr attr;
2296
2297         wpa_printf(MSG_DEBUG, "WPS: Received WSC_ACK");
2298
2299         if (wps_parse_msg(msg, &attr) < 0)
2300                 return WPS_FAILURE;
2301
2302         if (!wps_version_supported(attr.version)) {
2303                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2304                            attr.version ? *attr.version : 0);
2305                 return WPS_FAILURE;
2306         }
2307
2308         if (attr.msg_type == NULL) {
2309                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2310                 return WPS_FAILURE;
2311         }
2312
2313         if (*attr.msg_type != WPS_WSC_ACK) {
2314                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2315                            *attr.msg_type);
2316                 return WPS_FAILURE;
2317         }
2318
2319 #ifdef CONFIG_WPS_UPNP
2320         if (wps->wps->wps_upnp && wps->ext_reg && wps->state == RECV_M2D_ACK &&
2321             upnp_wps_subscribers(wps->wps->wps_upnp)) {
2322                 if (wps->wps->upnp_msgs)
2323                         return WPS_CONTINUE;
2324                 wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2325                            "external Registrar");
2326                 return WPS_PENDING;
2327         }
2328 #endif /* CONFIG_WPS_UPNP */
2329
2330         if (attr.registrar_nonce == NULL ||
2331             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2332         {
2333                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2334                 return WPS_FAILURE;
2335         }
2336
2337         if (attr.enrollee_nonce == NULL ||
2338             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2339                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2340                 return WPS_FAILURE;
2341         }
2342
2343         if (wps->state == RECV_M2D_ACK) {
2344 #ifdef CONFIG_WPS_UPNP
2345                 if (wps->wps->wps_upnp &&
2346                     upnp_wps_subscribers(wps->wps->wps_upnp)) {
2347                         if (wps->wps->upnp_msgs)
2348                                 return WPS_CONTINUE;
2349                         if (wps->ext_reg == 0)
2350                                 wps->ext_reg = 1;
2351                         wpa_printf(MSG_DEBUG, "WPS: Wait for response from an "
2352                                    "external Registrar");
2353                         return WPS_PENDING;
2354                 }
2355 #endif /* CONFIG_WPS_UPNP */
2356
2357                 wpa_printf(MSG_DEBUG, "WPS: No more registrars available - "
2358                            "terminate negotiation");
2359         }
2360
2361         return WPS_FAILURE;
2362 }
2363
2364
2365 static enum wps_process_res wps_process_wsc_nack(struct wps_data *wps,
2366                                                  const struct wpabuf *msg)
2367 {
2368         struct wps_parse_attr attr;
2369         int old_state;
2370
2371         wpa_printf(MSG_DEBUG, "WPS: Received WSC_NACK");
2372
2373         old_state = wps->state;
2374         wps->state = SEND_WSC_NACK;
2375
2376         if (wps_parse_msg(msg, &attr) < 0)
2377                 return WPS_FAILURE;
2378
2379         if (!wps_version_supported(attr.version)) {
2380                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2381                            attr.version ? *attr.version : 0);
2382                 return WPS_FAILURE;
2383         }
2384
2385         if (attr.msg_type == NULL) {
2386                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2387                 return WPS_FAILURE;
2388         }
2389
2390         if (*attr.msg_type != WPS_WSC_NACK) {
2391                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2392                            *attr.msg_type);
2393                 return WPS_FAILURE;
2394         }
2395
2396 #ifdef CONFIG_WPS_UPNP
2397         if (wps->wps->wps_upnp && wps->ext_reg) {
2398                 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2399                            "Registrar terminated by the Enrollee");
2400                 return WPS_FAILURE;
2401         }
2402 #endif /* CONFIG_WPS_UPNP */
2403
2404         if (attr.registrar_nonce == NULL ||
2405             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2406         {
2407                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2408                 return WPS_FAILURE;
2409         }
2410
2411         if (attr.enrollee_nonce == NULL ||
2412             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2413                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2414                 return WPS_FAILURE;
2415         }
2416
2417         if (attr.config_error == NULL) {
2418                 wpa_printf(MSG_DEBUG, "WPS: No Configuration Error attribute "
2419                            "in WSC_NACK");
2420                 return WPS_FAILURE;
2421         }
2422
2423         wpa_printf(MSG_DEBUG, "WPS: Enrollee terminated negotiation with "
2424                    "Configuration Error %d", WPA_GET_BE16(attr.config_error));
2425
2426         switch (old_state) {
2427         case RECV_M3:
2428                 wps_fail_event(wps->wps, WPS_M2);
2429                 break;
2430         case RECV_M5:
2431                 wps_fail_event(wps->wps, WPS_M4);
2432                 break;
2433         case RECV_M7:
2434                 wps_fail_event(wps->wps, WPS_M6);
2435                 break;
2436         case RECV_DONE:
2437                 wps_fail_event(wps->wps, WPS_M8);
2438                 break;
2439         default:
2440                 break;
2441         }
2442
2443         return WPS_FAILURE;
2444 }
2445
2446
2447 static enum wps_process_res wps_process_wsc_done(struct wps_data *wps,
2448                                                  const struct wpabuf *msg)
2449 {
2450         struct wps_parse_attr attr;
2451
2452         wpa_printf(MSG_DEBUG, "WPS: Received WSC_Done");
2453
2454         if (wps->state != RECV_DONE &&
2455             (!wps->wps->wps_upnp || !wps->ext_reg)) {
2456                 wpa_printf(MSG_DEBUG, "WPS: Unexpected state (%d) for "
2457                            "receiving WSC_Done", wps->state);
2458                 return WPS_FAILURE;
2459         }
2460
2461         if (wps_parse_msg(msg, &attr) < 0)
2462                 return WPS_FAILURE;
2463
2464         if (!wps_version_supported(attr.version)) {
2465                 wpa_printf(MSG_DEBUG, "WPS: Unsupported message version 0x%x",
2466                            attr.version ? *attr.version : 0);
2467                 return WPS_FAILURE;
2468         }
2469
2470         if (attr.msg_type == NULL) {
2471                 wpa_printf(MSG_DEBUG, "WPS: No Message Type attribute");
2472                 return WPS_FAILURE;
2473         }
2474
2475         if (*attr.msg_type != WPS_WSC_DONE) {
2476                 wpa_printf(MSG_DEBUG, "WPS: Invalid Message Type %d",
2477                            *attr.msg_type);
2478                 return WPS_FAILURE;
2479         }
2480
2481 #ifdef CONFIG_WPS_UPNP
2482         if (wps->wps->wps_upnp && wps->ext_reg) {
2483                 wpa_printf(MSG_DEBUG, "WPS: Negotiation using external "
2484                            "Registrar completed successfully");
2485                 wps_device_store(wps->wps->registrar, &wps->peer_dev,
2486                                  wps->uuid_e);
2487                 return WPS_DONE;
2488         }
2489 #endif /* CONFIG_WPS_UPNP */
2490
2491         if (attr.registrar_nonce == NULL ||
2492             os_memcmp(wps->nonce_r, attr.registrar_nonce, WPS_NONCE_LEN != 0))
2493         {
2494                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in registrar nonce");
2495                 return WPS_FAILURE;
2496         }
2497
2498         if (attr.enrollee_nonce == NULL ||
2499             os_memcmp(wps->nonce_e, attr.enrollee_nonce, WPS_NONCE_LEN != 0)) {
2500                 wpa_printf(MSG_DEBUG, "WPS: Mismatch in enrollee nonce");
2501                 return WPS_FAILURE;
2502         }
2503
2504         wpa_printf(MSG_DEBUG, "WPS: Negotiation completed successfully");
2505         wps_device_store(wps->wps->registrar, &wps->peer_dev,
2506                          wps->uuid_e);
2507
2508         if (wps->wps->wps_state == WPS_STATE_NOT_CONFIGURED && wps->new_psk &&
2509             wps->wps->ap && !wps->wps->registrar->disable_auto_conf) {
2510                 struct wps_credential cred;
2511
2512                 wpa_printf(MSG_DEBUG, "WPS: Moving to Configured state based "
2513                            "on first Enrollee connection");
2514
2515                 os_memset(&cred, 0, sizeof(cred));
2516                 os_memcpy(cred.ssid, wps->wps->ssid, wps->wps->ssid_len);
2517                 cred.ssid_len = wps->wps->ssid_len;
2518                 cred.auth_type = WPS_AUTH_WPAPSK | WPS_AUTH_WPA2PSK;
2519                 cred.encr_type = WPS_ENCR_TKIP | WPS_ENCR_AES;
2520                 os_memcpy(cred.key, wps->new_psk, wps->new_psk_len);
2521                 cred.key_len = wps->new_psk_len;
2522
2523                 wps->wps->wps_state = WPS_STATE_CONFIGURED;
2524                 wpa_hexdump_ascii_key(MSG_DEBUG,
2525                                       "WPS: Generated random passphrase",
2526                                       wps->new_psk, wps->new_psk_len);
2527                 if (wps->wps->cred_cb)
2528                         wps->wps->cred_cb(wps->wps->cb_ctx, &cred);
2529
2530                 os_free(wps->new_psk);
2531                 wps->new_psk = NULL;
2532         }
2533
2534         if (!wps->wps->ap)
2535                 wps_sta_cred_cb(wps);
2536
2537         if (wps->new_psk) {
2538                 if (wps_cb_new_psk(wps->wps->registrar, wps->mac_addr_e,
2539                                    wps->new_psk, wps->new_psk_len)) {
2540                         wpa_printf(MSG_DEBUG, "WPS: Failed to configure the "
2541                                    "new PSK");
2542                 }
2543                 os_free(wps->new_psk);
2544                 wps->new_psk = NULL;
2545         }
2546
2547         wps_cb_reg_success(wps->wps->registrar, wps->mac_addr_e, wps->uuid_e);
2548
2549         if (wps->pbc) {
2550                 wps_registrar_remove_pbc_session(wps->wps->registrar,
2551                                                  wps->mac_addr_e, wps->uuid_e);
2552                 wps_registrar_pbc_completed(wps->wps->registrar);
2553         }
2554
2555         wps_success_event(wps->wps);
2556
2557         return WPS_DONE;
2558 }
2559
2560
2561 enum wps_process_res wps_registrar_process_msg(struct wps_data *wps,
2562                                                enum wsc_op_code op_code,
2563                                                const struct wpabuf *msg)
2564 {
2565         enum wps_process_res ret;
2566
2567         wpa_printf(MSG_DEBUG, "WPS: Processing received message (len=%lu "
2568                    "op_code=%d)",
2569                    (unsigned long) wpabuf_len(msg), op_code);
2570
2571 #ifdef CONFIG_WPS_UPNP
2572         if (wps->wps->wps_upnp && op_code == WSC_MSG && wps->ext_reg == 1) {
2573                 struct wps_parse_attr attr;
2574                 if (wps_parse_msg(msg, &attr) == 0 && attr.msg_type &&
2575                     *attr.msg_type == WPS_M3)
2576                         wps->ext_reg = 2; /* past M2/M2D phase */
2577         }
2578         if (wps->ext_reg > 1)
2579                 wps_registrar_free_pending_m2(wps->wps);
2580         if (wps->wps->wps_upnp && wps->ext_reg &&
2581             wps->wps->upnp_msgs == NULL &&
2582             (op_code == WSC_MSG || op_code == WSC_Done)) {
2583                 struct wps_parse_attr attr;
2584                 int type;
2585                 if (wps_parse_msg(msg, &attr) < 0 || attr.msg_type == NULL)
2586                         type = -1;
2587                 else
2588                         type = *attr.msg_type;
2589                 wpa_printf(MSG_DEBUG, "WPS: Sending received message (type %d)"
2590                            " to external Registrar for processing", type);
2591                 upnp_wps_device_send_wlan_event(wps->wps->wps_upnp,
2592                                                 wps->mac_addr_e,
2593                                                 UPNP_WPS_WLANEVENT_TYPE_EAP,
2594                                                 msg);
2595                 if (op_code == WSC_MSG)
2596                         return WPS_PENDING;
2597         } else if (wps->wps->wps_upnp && wps->ext_reg && op_code == WSC_MSG) {
2598                 wpa_printf(MSG_DEBUG, "WPS: Skip internal processing - using "
2599                            "external Registrar");
2600                 return WPS_CONTINUE;
2601         }
2602 #endif /* CONFIG_WPS_UPNP */
2603
2604         switch (op_code) {
2605         case WSC_MSG:
2606                 return wps_process_wsc_msg(wps, msg);
2607         case WSC_ACK:
2608                 return wps_process_wsc_ack(wps, msg);
2609         case WSC_NACK:
2610                 return wps_process_wsc_nack(wps, msg);
2611         case WSC_Done:
2612                 ret = wps_process_wsc_done(wps, msg);
2613                 if (ret == WPS_FAILURE) {
2614                         wps->state = SEND_WSC_NACK;
2615                         wps_fail_event(wps->wps, WPS_WSC_DONE);
2616                 }
2617                 return ret;
2618         default:
2619                 wpa_printf(MSG_DEBUG, "WPS: Unsupported op_code %d", op_code);
2620                 return WPS_FAILURE;
2621         }
2622 }
2623
2624
2625 int wps_registrar_update_ie(struct wps_registrar *reg)
2626 {
2627         return wps_set_ie(reg);
2628 }
2629
2630
2631 static void wps_registrar_set_selected_timeout(void *eloop_ctx,
2632                                                void *timeout_ctx)
2633 {
2634         struct wps_registrar *reg = eloop_ctx;
2635
2636         wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar timed out - "
2637                    "unselect Registrar");
2638         reg->selected_registrar = 0;
2639         reg->pbc = 0;
2640         reg->sel_reg_dev_password_id_override = -1;
2641         reg->sel_reg_config_methods_override = -1;
2642         wps_set_ie(reg);
2643 }
2644
2645
2646 /**
2647  * wps_registrar_set_selected_registrar - Notification of SetSelectedRegistrar
2648  * @reg: Registrar data from wps_registrar_init()
2649  * @msg: Received message from SetSelectedRegistrar
2650  * Returns: 0 on success, -1 on failure
2651  *
2652  * This function is called when an AP receives a SetSelectedRegistrar UPnP
2653  * message.
2654  */
2655 int wps_registrar_set_selected_registrar(struct wps_registrar *reg,
2656                                          const struct wpabuf *msg)
2657 {
2658         struct wps_parse_attr attr;
2659
2660         wpa_hexdump_buf(MSG_MSGDUMP, "WPS: SetSelectedRegistrar attributes",
2661                         msg);
2662
2663         if (wps_parse_msg(msg, &attr) < 0)
2664                 return -1;
2665         if (!wps_version_supported(attr.version)) {
2666                 wpa_printf(MSG_DEBUG, "WPS: Unsupported SetSelectedRegistrar "
2667                            "version 0x%x", attr.version ? *attr.version : 0);
2668                 return -1;
2669         }
2670
2671         if (attr.selected_registrar == NULL ||
2672             *attr.selected_registrar == 0) {
2673                 wpa_printf(MSG_DEBUG, "WPS: SetSelectedRegistrar: Disable "
2674                            "Selected Registrar");
2675                 eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg,
2676                                      NULL);
2677                 wps_registrar_set_selected_timeout(reg, NULL);
2678                 return 0;
2679         }
2680
2681         reg->selected_registrar = 1;
2682         reg->sel_reg_dev_password_id_override = attr.dev_password_id ?
2683                 WPA_GET_BE16(attr.dev_password_id) : DEV_PW_DEFAULT;
2684         reg->sel_reg_config_methods_override = attr.sel_reg_config_methods ?
2685                 WPA_GET_BE16(attr.sel_reg_config_methods) : -1;
2686         wps_set_ie(reg);
2687
2688         eloop_cancel_timeout(wps_registrar_set_selected_timeout, reg, NULL);
2689         eloop_register_timeout(WPS_PBC_WALK_TIME, 0,
2690                                wps_registrar_set_selected_timeout,
2691                                reg, NULL);
2692         return 0;
2693 }
2694
2695
2696 int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
2697                            char *buf, size_t buflen)
2698 {
2699         struct wps_registrar_device *d;
2700         int len = 0, ret;
2701         char uuid[40];
2702
2703         d = wps_device_get(reg, addr);
2704         if (d == NULL)
2705                 return 0;
2706         if (uuid_bin2str(d->uuid, uuid, sizeof(uuid)))
2707                 return 0;
2708
2709         ret = os_snprintf(buf + len, buflen - len,
2710                           "wpsUuid=%s\n"
2711                           "wpsPrimaryDeviceType=%u-%08X-%u\n"
2712                           "wpsDeviceName=%s\n"
2713                           "wpsManufacturer=%s\n"
2714                           "wpsModelName=%s\n"
2715                           "wpsModelNumber=%s\n"
2716                           "wpsSerialNumber=%s\n",
2717                           uuid,
2718                           d->dev.categ, d->dev.oui, d->dev.sub_categ,
2719                           d->dev.device_name ? d->dev.device_name : "",
2720                           d->dev.manufacturer ? d->dev.manufacturer : "",
2721                           d->dev.model_name ? d->dev.model_name : "",
2722                           d->dev.model_number ? d->dev.model_number : "",
2723                           d->dev.serial_number ? d->dev.serial_number : "");
2724         if (ret < 0 || (size_t) ret >= buflen - len)
2725                 return len;
2726         len += ret;
2727
2728         return len;
2729 }