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