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