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