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