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