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