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