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