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