WPS UFD: Use pre-configured DH keys only with OOB
[libeap.git] / wpa_supplicant / wps_supplicant.c
1 /*
2  * wpa_supplicant / WPS integration
3  * Copyright (c) 2008, Jouni Malinen <j@w1.fi>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * Alternatively, this software may be distributed under the terms of BSD
10  * license.
11  *
12  * See README and COPYING for more details.
13  */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "ieee802_11_defs.h"
19 #include "wpa_common.h"
20 #include "config.h"
21 #include "eap_peer/eap.h"
22 #include "wpa_supplicant_i.h"
23 #include "eloop.h"
24 #include "uuid.h"
25 #include "wpa_ctrl.h"
26 #include "ctrl_iface_dbus.h"
27 #include "eap_common/eap_wsc_common.h"
28 #include "blacklist.h"
29 #include "wps_supplicant.h"
30 #include "dh_groups.h"
31
32 #define WPS_PIN_SCAN_IGNORE_SEL_REG 3
33
34 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx);
35 static void wpas_clear_wps(struct wpa_supplicant *wpa_s);
36
37
38 int wpas_wps_eapol_cb(struct wpa_supplicant *wpa_s)
39 {
40         if (!wpa_s->wps_success &&
41             wpa_s->current_ssid &&
42             eap_is_wps_pin_enrollee(&wpa_s->current_ssid->eap)) {
43                 const u8 *bssid = wpa_s->bssid;
44                 if (is_zero_ether_addr(bssid))
45                         bssid = wpa_s->pending_bssid;
46
47                 wpa_printf(MSG_DEBUG, "WPS: PIN registration with " MACSTR
48                            " did not succeed - continue trying to find "
49                            "suitable AP", MAC2STR(bssid));
50                 wpa_blacklist_add(wpa_s, bssid);
51
52                 wpa_supplicant_deauthenticate(wpa_s,
53                                               WLAN_REASON_DEAUTH_LEAVING);
54                 wpa_s->reassociate = 1;
55                 wpa_supplicant_req_scan(wpa_s,
56                                         wpa_s->blacklist_cleared ? 5 : 0, 0);
57                 wpa_s->blacklist_cleared = 0;
58                 return 1;
59         }
60
61         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
62
63         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid &&
64             !(wpa_s->current_ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
65                 wpa_printf(MSG_DEBUG, "WPS: Network configuration replaced - "
66                            "try to associate with the received credential");
67                 wpa_supplicant_deauthenticate(wpa_s,
68                                               WLAN_REASON_DEAUTH_LEAVING);
69                 wpa_s->reassociate = 1;
70                 wpa_supplicant_req_scan(wpa_s, 0, 0);
71                 return 1;
72         }
73
74         if (wpa_s->key_mgmt == WPA_KEY_MGMT_WPS && wpa_s->current_ssid) {
75                 wpa_printf(MSG_DEBUG, "WPS: Registration completed - waiting "
76                            "for external credential processing");
77                 wpas_clear_wps(wpa_s);
78                 wpa_supplicant_deauthenticate(wpa_s,
79                                               WLAN_REASON_DEAUTH_LEAVING);
80                 return 1;
81         }
82
83         return 0;
84 }
85
86
87 static int wpa_supplicant_wps_cred(void *ctx,
88                                    const struct wps_credential *cred)
89 {
90         struct wpa_supplicant *wpa_s = ctx;
91         struct wpa_ssid *ssid = wpa_s->current_ssid;
92         u8 key_idx = 0;
93
94         if ((wpa_s->conf->wps_cred_processing == 1 ||
95              wpa_s->conf->wps_cred_processing == 2) && cred->cred_attr) {
96                 size_t blen = cred->cred_attr_len * 2 + 1;
97                 char *buf = os_malloc(blen);
98                 if (buf) {
99                         wpa_snprintf_hex(buf, blen,
100                                          cred->cred_attr, cred->cred_attr_len);
101                         wpa_msg(wpa_s, MSG_INFO, "%s%s",
102                                 WPS_EVENT_CRED_RECEIVED, buf);
103                         os_free(buf);
104                 }
105                 wpa_supplicant_dbus_notify_wps_cred(wpa_s, cred);
106         } else
107                 wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_CRED_RECEIVED);
108
109         wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
110                         cred->cred_attr, cred->cred_attr_len);
111
112         if (wpa_s->conf->wps_cred_processing == 1)
113                 return 0;
114
115         if (cred->auth_type != WPS_AUTH_OPEN &&
116             cred->auth_type != WPS_AUTH_SHARED &&
117             cred->auth_type != WPS_AUTH_WPAPSK &&
118             cred->auth_type != WPS_AUTH_WPA2PSK) {
119                 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
120                            "unsupported authentication type %d",
121                            cred->auth_type);
122                 return 0;
123         }
124
125         if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
126                 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
127                            "on the received credential");
128                 os_free(ssid->eap.identity);
129                 ssid->eap.identity = NULL;
130                 ssid->eap.identity_len = 0;
131                 os_free(ssid->eap.phase1);
132                 ssid->eap.phase1 = NULL;
133                 os_free(ssid->eap.eap_methods);
134                 ssid->eap.eap_methods = NULL;
135         } else {
136                 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
137                            "received credential");
138                 ssid = wpa_config_add_network(wpa_s->conf);
139                 if (ssid == NULL)
140                         return -1;
141         }
142
143         wpa_config_set_network_defaults(ssid);
144
145         os_free(ssid->ssid);
146         ssid->ssid = os_malloc(cred->ssid_len);
147         if (ssid->ssid) {
148                 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
149                 ssid->ssid_len = cred->ssid_len;
150         }
151
152         switch (cred->encr_type) {
153         case WPS_ENCR_NONE:
154                 break;
155         case WPS_ENCR_WEP:
156                 if (cred->key_len <= 0)
157                         break;
158                 if (cred->key_len != 5 && cred->key_len != 13 &&
159                     cred->key_len != 10 && cred->key_len != 26) {
160                         wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
161                                    "%lu", (unsigned long) cred->key_len);
162                         return -1;
163                 }
164                 if (cred->key_idx > NUM_WEP_KEYS) {
165                         wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
166                                    cred->key_idx);
167                         return -1;
168                 }
169                 if (cred->key_idx)
170                         key_idx = cred->key_idx - 1;
171                 if (cred->key_len == 10 || cred->key_len == 26) {
172                         if (hexstr2bin((char *) cred->key,
173                                        ssid->wep_key[key_idx],
174                                        cred->key_len / 2) < 0) {
175                                 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
176                                            "%d", key_idx);
177                                 return -1;
178                         }
179                         ssid->wep_key_len[key_idx] = cred->key_len / 2;
180                 } else {
181                         os_memcpy(ssid->wep_key[key_idx], cred->key,
182                                   cred->key_len);
183                         ssid->wep_key_len[key_idx] = cred->key_len;
184                 }
185                 ssid->wep_tx_keyidx = key_idx;
186                 break;
187         case WPS_ENCR_TKIP:
188                 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
189                 break;
190         case WPS_ENCR_AES:
191                 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
192                 break;
193         }
194
195         switch (cred->auth_type) {
196         case WPS_AUTH_OPEN:
197                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
198                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
199                 ssid->proto = 0;
200                 break;
201         case WPS_AUTH_SHARED:
202                 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
203                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
204                 ssid->proto = 0;
205                 break;
206         case WPS_AUTH_WPAPSK:
207                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
208                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
209                 ssid->proto = WPA_PROTO_WPA;
210                 break;
211         case WPS_AUTH_WPA:
212                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
213                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
214                 ssid->proto = WPA_PROTO_WPA;
215                 break;
216         case WPS_AUTH_WPA2:
217                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
218                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
219                 ssid->proto = WPA_PROTO_RSN;
220                 break;
221         case WPS_AUTH_WPA2PSK:
222                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
223                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
224                 ssid->proto = WPA_PROTO_RSN;
225                 break;
226         }
227
228         if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
229                 if (cred->key_len == 2 * PMK_LEN) {
230                         if (hexstr2bin((const char *) cred->key, ssid->psk,
231                                        PMK_LEN)) {
232                                 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
233                                            "Key");
234                                 return -1;
235                         }
236                         ssid->psk_set = 1;
237                 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
238                         os_free(ssid->passphrase);
239                         ssid->passphrase = os_malloc(cred->key_len + 1);
240                         if (ssid->passphrase == NULL)
241                                 return -1;
242                         os_memcpy(ssid->passphrase, cred->key, cred->key_len);
243                         ssid->passphrase[cred->key_len] = '\0';
244                         wpa_config_update_psk(ssid);
245                 } else {
246                         wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
247                                    "length %lu",
248                                    (unsigned long) cred->key_len);
249                         return -1;
250                 }
251         }
252
253 #ifndef CONFIG_NO_CONFIG_WRITE
254         if (wpa_s->conf->update_config &&
255             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
256                 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
257                 return -1;
258         }
259 #endif /* CONFIG_NO_CONFIG_WRITE */
260
261         return 0;
262 }
263
264
265 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
266                                          struct wps_event_m2d *m2d)
267 {
268         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
269                 "dev_password_id=%d config_error=%d",
270                 m2d->dev_password_id, m2d->config_error);
271 }
272
273
274 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
275                                           struct wps_event_fail *fail)
276 {
277         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
278         wpas_clear_wps(wpa_s);
279 }
280
281
282 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
283 {
284         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
285         wpa_s->wps_success = 1;
286 }
287
288
289 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
290                                      union wps_event_data *data)
291 {
292         struct wpa_supplicant *wpa_s = ctx;
293         switch (event) {
294         case WPS_EV_M2D:
295                 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
296                 break;
297         case WPS_EV_FAIL:
298                 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
299                 break;
300         case WPS_EV_SUCCESS:
301                 wpa_supplicant_wps_event_success(wpa_s);
302                 break;
303         case WPS_EV_PWD_AUTH_FAIL:
304                 break;
305         }
306 }
307
308
309 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
310 {
311         if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
312             eap_is_wps_pin_enrollee(&ssid->eap))
313                 return WPS_REQ_ENROLLEE;
314         else
315                 return WPS_REQ_REGISTRAR;
316 }
317
318
319 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
320 {
321         int id;
322         struct wpa_ssid *ssid;
323
324         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
325
326         /* Remove any existing WPS network from configuration */
327         ssid = wpa_s->conf->ssid;
328         while (ssid) {
329                 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
330                         if (ssid == wpa_s->current_ssid)
331                                 wpa_s->current_ssid = NULL;
332                         id = ssid->id;
333                 } else
334                         id = -1;
335                 ssid = ssid->next;
336                 if (id >= 0)
337                         wpa_config_remove_network(wpa_s->conf, id);
338         }
339 }
340
341
342 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
343 {
344         struct wpa_supplicant *wpa_s = eloop_ctx;
345         wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
346                    "out");
347         wpas_clear_wps(wpa_s);
348 }
349
350
351 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
352                                               int registrar, const u8 *bssid)
353 {
354         struct wpa_ssid *ssid;
355
356         ssid = wpa_config_add_network(wpa_s->conf);
357         if (ssid == NULL)
358                 return NULL;
359         wpa_config_set_network_defaults(ssid);
360         if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
361             wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
362             wpa_config_set(ssid, "identity", registrar ?
363                            "\"" WSC_ID_REGISTRAR "\"" :
364                            "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
365                 wpa_config_remove_network(wpa_s->conf, ssid->id);
366                 return NULL;
367         }
368
369         if (bssid) {
370                 size_t i;
371                 struct wpa_scan_res *res;
372
373                 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
374                 ssid->bssid_set = 1;
375
376                 /* Try to get SSID from scan results */
377                 if (wpa_s->scan_res == NULL &&
378                     wpa_supplicant_get_scan_results(wpa_s) < 0)
379                         return ssid; /* Could not find any scan results */
380
381                 for (i = 0; i < wpa_s->scan_res->num; i++) {
382                         const u8 *ie;
383
384                         res = wpa_s->scan_res->res[i];
385                         if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
386                                 continue;
387
388                         ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
389                         if (ie == NULL)
390                                 break;
391                         os_free(ssid->ssid);
392                         ssid->ssid = os_malloc(ie[1]);
393                         if (ssid->ssid == NULL)
394                                 break;
395                         os_memcpy(ssid->ssid, ie + 2, ie[1]);
396                         ssid->ssid_len = ie[1];
397                         break;
398                 }
399         }
400
401         return ssid;
402 }
403
404
405 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
406                              struct wpa_ssid *selected)
407 {
408         struct wpa_ssid *ssid;
409
410         /* Mark all other networks disabled and trigger reassociation */
411         ssid = wpa_s->conf->ssid;
412         while (ssid) {
413                 ssid->disabled = ssid != selected;
414                 ssid = ssid->next;
415         }
416         wpa_s->disconnected = 0;
417         wpa_s->reassociate = 1;
418         wpa_s->scan_runs = 0;
419         wpa_s->wps_success = 0;
420         wpa_s->blacklist_cleared = 0;
421         wpa_supplicant_req_scan(wpa_s, 0, 0);
422 }
423
424
425 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
426 {
427         struct wpa_ssid *ssid;
428         wpas_clear_wps(wpa_s);
429         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
430         if (ssid == NULL)
431                 return -1;
432         wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
433         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
434                                wpa_s, NULL);
435         wpas_wps_reassoc(wpa_s, ssid);
436         return 0;
437 }
438
439
440 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
441                        const char *pin)
442 {
443         struct wpa_ssid *ssid;
444         char val[128];
445         unsigned int rpin = 0;
446
447         wpas_clear_wps(wpa_s);
448         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
449         if (ssid == NULL)
450                 return -1;
451         if (pin)
452                 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
453         else {
454                 rpin = wps_generate_pin();
455                 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
456         }
457         wpa_config_set(ssid, "phase1", val, 0);
458         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
459                                wpa_s, NULL);
460         wpas_wps_reassoc(wpa_s, ssid);
461         return rpin;
462 }
463
464
465 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
466                        char *path, char *method)
467 {
468         struct wps_context *wps = wpa_s->wps;
469         struct oob_device_data *oob_dev;
470
471         oob_dev = wps_get_oob_device(device_type);
472         if (oob_dev == NULL)
473                 return -1;
474         oob_dev->device_path = path;
475         wps->oob_conf.oob_method = wps_get_oob_method(method);
476
477         if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
478                 /*
479                  * Use pre-configured DH keys in order to be able to write the
480                  * key hash into the OOB file.
481                  */
482                 wpabuf_free(wps->dh_pubkey);
483                 wpabuf_free(wps->dh_privkey);
484                 wps->dh_privkey = NULL;
485                 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
486                                          &wps->dh_privkey);
487                 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
488                 if (wps->dh_pubkey == NULL) {
489                         wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
490                                    "Diffie-Hellman handshake");
491                         return -1;
492                 }
493         }
494
495         if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
496                 wpas_clear_wps(wpa_s);
497
498         if (wps_process_oob(wps, oob_dev, 0) < 0)
499                 return -1;
500
501         if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
502              wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
503             wpas_wps_start_pin(wpa_s, NULL,
504                                wpabuf_head(wps->oob_conf.dev_password)) < 0)
505                         return -1;
506
507         return 0;
508 }
509
510
511 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
512                        const char *pin)
513 {
514         struct wpa_ssid *ssid;
515         char val[30];
516
517         if (!pin)
518                 return -1;
519         wpas_clear_wps(wpa_s);
520         ssid = wpas_wps_add_network(wpa_s, 1, bssid);
521         if (ssid == NULL)
522                 return -1;
523         os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
524         wpa_config_set(ssid, "phase1", val, 0);
525         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
526                                wpa_s, NULL);
527         wpas_wps_reassoc(wpa_s, ssid);
528         return 0;
529 }
530
531
532 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
533                                size_t psk_len)
534 {
535         wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
536                    "STA " MACSTR, MAC2STR(mac_addr));
537         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
538
539         /* TODO */
540
541         return 0;
542 }
543
544
545 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
546                                    const struct wps_device_data *dev)
547 {
548         char uuid[40], txt[400];
549         int len;
550         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
551                 return;
552         wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
553         len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
554                           " [%s|%s|%s|%s|%s|%d-%08X-%d]",
555                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
556                           dev->manufacturer, dev->model_name,
557                           dev->model_number, dev->serial_number,
558                           dev->categ, dev->oui, dev->sub_categ);
559         if (len > 0 && len < (int) sizeof(txt))
560                 wpa_printf(MSG_INFO, "%s", txt);
561 }
562
563
564 int wpas_wps_init(struct wpa_supplicant *wpa_s)
565 {
566         struct wps_context *wps;
567         struct wps_registrar_config rcfg;
568
569         wps = os_zalloc(sizeof(*wps));
570         if (wps == NULL)
571                 return -1;
572
573         wps->cred_cb = wpa_supplicant_wps_cred;
574         wps->event_cb = wpa_supplicant_wps_event;
575         wps->cb_ctx = wpa_s;
576
577         wps->dev.device_name = wpa_s->conf->device_name;
578         wps->dev.manufacturer = wpa_s->conf->manufacturer;
579         wps->dev.model_name = wpa_s->conf->model_name;
580         wps->dev.model_number = wpa_s->conf->model_number;
581         wps->dev.serial_number = wpa_s->conf->serial_number;
582         if (wpa_s->conf->device_type) {
583                 char *pos;
584                 u8 oui[4];
585                 /* <categ>-<OUI>-<subcateg> */
586                 wps->dev.categ = atoi(wpa_s->conf->device_type);
587                 pos = os_strchr(wpa_s->conf->device_type, '-');
588                 if (pos == NULL) {
589                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
590                         os_free(wps);
591                         return -1;
592                 }
593                 pos++;
594                 if (hexstr2bin(pos, oui, 4)) {
595                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
596                         os_free(wps);
597                         return -1;
598                 }
599                 wps->dev.oui = WPA_GET_BE32(oui);
600                 pos = os_strchr(pos, '-');
601                 if (pos == NULL) {
602                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
603                         os_free(wps);
604                         return -1;
605                 }
606                 pos++;
607                 wps->dev.sub_categ = atoi(pos);
608         }
609         wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
610         wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
611         os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
612         if (is_nil_uuid(wpa_s->conf->uuid)) {
613                 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
614                 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
615                             wps->uuid, WPS_UUID_LEN);
616         } else
617                 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
618
619         wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
620         wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
621
622         os_memset(&rcfg, 0, sizeof(rcfg));
623         rcfg.new_psk_cb = wpas_wps_new_psk_cb;
624         rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
625         rcfg.cb_ctx = wpa_s;
626
627         wps->registrar = wps_registrar_init(wps, &rcfg);
628         if (wps->registrar == NULL) {
629                 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
630                 os_free(wps);
631                 return -1;
632         }
633
634         wpa_s->wps = wps;
635
636         return 0;
637 }
638
639
640 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
641 {
642         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
643
644         if (wpa_s->wps == NULL)
645                 return;
646
647         wps_registrar_deinit(wpa_s->wps->registrar);
648         wpabuf_free(wpa_s->wps->dh_pubkey);
649         wpabuf_free(wpa_s->wps->dh_privkey);
650         wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
651         wpabuf_free(wpa_s->wps->oob_conf.dev_password);
652         os_free(wpa_s->wps->network_key);
653         os_free(wpa_s->wps);
654         wpa_s->wps = NULL;
655 }
656
657
658 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
659                             struct wpa_ssid *ssid, struct wpa_scan_res *bss)
660 {
661         struct wpabuf *wps_ie;
662
663         if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
664                 return -1;
665
666         wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
667         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
668                 if (!wps_ie) {
669                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
670                         return 0;
671                 }
672
673                 if (!wps_is_selected_pbc_registrar(wps_ie)) {
674                         wpa_printf(MSG_DEBUG, "   skip - WPS AP "
675                                    "without active PBC Registrar");
676                         wpabuf_free(wps_ie);
677                         return 0;
678                 }
679
680                 /* TODO: overlap detection */
681                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
682                            "(Active PBC)");
683                 wpabuf_free(wps_ie);
684                 return 1;
685         }
686
687         if (eap_is_wps_pin_enrollee(&ssid->eap)) {
688                 if (!wps_ie) {
689                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
690                         return 0;
691                 }
692
693                 /*
694                  * Start with WPS APs that advertise active PIN Registrar and
695                  * allow any WPS AP after third scan since some APs do not set
696                  * Selected Registrar attribute properly when using external
697                  * Registrar.
698                  */
699                 if (!wps_is_selected_pin_registrar(wps_ie)) {
700                         if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
701                                 wpa_printf(MSG_DEBUG, "   skip - WPS AP "
702                                            "without active PIN Registrar");
703                                 wpabuf_free(wps_ie);
704                                 return 0;
705                         }
706                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
707                 } else {
708                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
709                                    "(Active PIN)");
710                 }
711                 wpabuf_free(wps_ie);
712                 return 1;
713         }
714
715         if (wps_ie) {
716                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
717                 wpabuf_free(wps_ie);
718                 return 1;
719         }
720
721         return -1;
722 }
723
724
725 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
726                               struct wpa_ssid *ssid,
727                               struct wpa_scan_res *bss)
728 {
729         struct wpabuf *wps_ie = NULL;
730         int ret = 0;
731
732         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
733                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
734                 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
735                         /* allow wildcard SSID for WPS PBC */
736                         ret = 1;
737                 }
738         } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
739                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
740                 if (wps_ie &&
741                     (wps_is_selected_pin_registrar(wps_ie) ||
742                      wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
743                         /* allow wildcard SSID for WPS PIN */
744                         ret = 1;
745                 }
746         }
747
748         if (!ret && ssid->bssid_set &&
749             os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
750                 /* allow wildcard SSID due to hardcoded BSSID match */
751                 ret = 1;
752         }
753
754         wpabuf_free(wps_ie);
755
756         return ret;
757 }
758
759
760 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
761                               struct wpa_scan_res *selected,
762                               struct wpa_ssid *ssid)
763 {
764         const u8 *sel_uuid, *uuid;
765         size_t i;
766         struct wpabuf *wps_ie;
767         int ret = 0;
768
769         if (!eap_is_wps_pbc_enrollee(&ssid->eap))
770                 return 0;
771
772         /* Make sure that only one AP is in active PBC mode */
773         wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
774         if (wps_ie)
775                 sel_uuid = wps_get_uuid_e(wps_ie);
776         else
777                 sel_uuid = NULL;
778
779         for (i = 0; i < wpa_s->scan_res->num; i++) {
780                 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
781                 struct wpabuf *ie;
782                 if (bss == selected)
783                         continue;
784                 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
785                 if (!ie)
786                         continue;
787                 if (!wps_is_selected_pbc_registrar(ie)) {
788                         wpabuf_free(ie);
789                         continue;
790                 }
791                 uuid = wps_get_uuid_e(ie);
792                 if (sel_uuid == NULL || uuid == NULL ||
793                     os_memcmp(sel_uuid, uuid, 16) != 0) {
794                         ret = 1; /* PBC overlap */
795                         wpabuf_free(ie);
796                         break;
797                 }
798
799                 /* TODO: verify that this is reasonable dual-band situation */
800
801                 wpabuf_free(ie);
802         }
803
804         wpabuf_free(wps_ie);
805
806         return ret;
807 }
808
809
810 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
811 {
812         size_t i;
813
814         if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
815                 return;
816
817         for (i = 0; i < wpa_s->scan_res->num; i++) {
818                 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
819                 struct wpabuf *ie;
820                 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
821                 if (!ie)
822                         continue;
823                 if (wps_is_selected_pbc_registrar(ie))
824                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
825                 else if (wps_is_selected_pin_registrar(ie))
826                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
827                 else
828                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
829                 wpabuf_free(ie);
830                 break;
831         }
832 }
833
834
835 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
836 {
837         struct wpa_ssid *ssid;
838
839         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
840                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
841                         return 1;
842         }
843
844         return 0;
845 }