b6c9bc9413b39d99d05698a4cdfb10c51c8921f1
[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         if (cred->auth_type != WPS_AUTH_OPEN &&
214             cred->auth_type != WPS_AUTH_SHARED &&
215             cred->auth_type != WPS_AUTH_WPAPSK &&
216             cred->auth_type != WPS_AUTH_WPA2PSK) {
217                 wpa_printf(MSG_DEBUG, "WPS: Ignored credentials for "
218                            "unsupported authentication type %d",
219                            cred->auth_type);
220                 return 0;
221         }
222
223         if (ssid && (ssid->key_mgmt & WPA_KEY_MGMT_WPS)) {
224                 wpa_printf(MSG_DEBUG, "WPS: Replace WPS network block based "
225                            "on the received credential");
226                 os_free(ssid->eap.identity);
227                 ssid->eap.identity = NULL;
228                 ssid->eap.identity_len = 0;
229                 os_free(ssid->eap.phase1);
230                 ssid->eap.phase1 = NULL;
231                 os_free(ssid->eap.eap_methods);
232                 ssid->eap.eap_methods = NULL;
233         } else {
234                 wpa_printf(MSG_DEBUG, "WPS: Create a new network based on the "
235                            "received credential");
236                 ssid = wpa_config_add_network(wpa_s->conf);
237                 if (ssid == NULL)
238                         return -1;
239         }
240
241         wpa_config_set_network_defaults(ssid);
242
243         os_free(ssid->ssid);
244         ssid->ssid = os_malloc(cred->ssid_len);
245         if (ssid->ssid) {
246                 os_memcpy(ssid->ssid, cred->ssid, cred->ssid_len);
247                 ssid->ssid_len = cred->ssid_len;
248         }
249
250         switch (cred->encr_type) {
251         case WPS_ENCR_NONE:
252                 break;
253         case WPS_ENCR_WEP:
254                 if (cred->key_len <= 0)
255                         break;
256                 if (cred->key_len != 5 && cred->key_len != 13 &&
257                     cred->key_len != 10 && cred->key_len != 26) {
258                         wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key length "
259                                    "%lu", (unsigned long) cred->key_len);
260                         return -1;
261                 }
262                 if (cred->key_idx > NUM_WEP_KEYS) {
263                         wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key index %d",
264                                    cred->key_idx);
265                         return -1;
266                 }
267                 if (cred->key_idx)
268                         key_idx = cred->key_idx - 1;
269                 if (cred->key_len == 10 || cred->key_len == 26) {
270                         if (hexstr2bin((char *) cred->key,
271                                        ssid->wep_key[key_idx],
272                                        cred->key_len / 2) < 0) {
273                                 wpa_printf(MSG_ERROR, "WPS: Invalid WEP Key "
274                                            "%d", key_idx);
275                                 return -1;
276                         }
277                         ssid->wep_key_len[key_idx] = cred->key_len / 2;
278                 } else {
279                         os_memcpy(ssid->wep_key[key_idx], cred->key,
280                                   cred->key_len);
281                         ssid->wep_key_len[key_idx] = cred->key_len;
282                 }
283                 ssid->wep_tx_keyidx = key_idx;
284                 break;
285         case WPS_ENCR_TKIP:
286                 ssid->pairwise_cipher = WPA_CIPHER_TKIP;
287                 break;
288         case WPS_ENCR_AES:
289                 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
290                 break;
291         }
292
293         switch (cred->auth_type) {
294         case WPS_AUTH_OPEN:
295                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
296                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
297                 ssid->proto = 0;
298                 break;
299         case WPS_AUTH_SHARED:
300                 ssid->auth_alg = WPA_AUTH_ALG_SHARED;
301                 ssid->key_mgmt = WPA_KEY_MGMT_NONE;
302                 ssid->proto = 0;
303                 break;
304         case WPS_AUTH_WPAPSK:
305                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
306                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
307                 ssid->proto = WPA_PROTO_WPA;
308                 break;
309         case WPS_AUTH_WPA:
310                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
311                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
312                 ssid->proto = WPA_PROTO_WPA;
313                 break;
314         case WPS_AUTH_WPA2:
315                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
316                 ssid->key_mgmt = WPA_KEY_MGMT_IEEE8021X;
317                 ssid->proto = WPA_PROTO_RSN;
318                 break;
319         case WPS_AUTH_WPA2PSK:
320                 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
321                 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
322                 ssid->proto = WPA_PROTO_RSN;
323                 break;
324         }
325
326         if (ssid->key_mgmt == WPA_KEY_MGMT_PSK) {
327                 if (cred->key_len == 2 * PMK_LEN) {
328                         if (hexstr2bin((const char *) cred->key, ssid->psk,
329                                        PMK_LEN)) {
330                                 wpa_printf(MSG_ERROR, "WPS: Invalid Network "
331                                            "Key");
332                                 return -1;
333                         }
334                         ssid->psk_set = 1;
335                 } else if (cred->key_len >= 8 && cred->key_len < 2 * PMK_LEN) {
336                         os_free(ssid->passphrase);
337                         ssid->passphrase = os_malloc(cred->key_len + 1);
338                         if (ssid->passphrase == NULL)
339                                 return -1;
340                         os_memcpy(ssid->passphrase, cred->key, cred->key_len);
341                         ssid->passphrase[cred->key_len] = '\0';
342                         wpa_config_update_psk(ssid);
343                 } else {
344                         wpa_printf(MSG_ERROR, "WPS: Invalid Network Key "
345                                    "length %lu",
346                                    (unsigned long) cred->key_len);
347                         return -1;
348                 }
349         }
350
351         wpas_wps_security_workaround(wpa_s, ssid, cred);
352
353 #ifndef CONFIG_NO_CONFIG_WRITE
354         if (wpa_s->conf->update_config &&
355             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
356                 wpa_printf(MSG_DEBUG, "WPS: Failed to update configuration");
357                 return -1;
358         }
359 #endif /* CONFIG_NO_CONFIG_WRITE */
360
361         return 0;
362 }
363
364
365 static void wpa_supplicant_wps_event_m2d(struct wpa_supplicant *wpa_s,
366                                          struct wps_event_m2d *m2d)
367 {
368         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_M2D
369                 "dev_password_id=%d config_error=%d",
370                 m2d->dev_password_id, m2d->config_error);
371 }
372
373
374 static void wpa_supplicant_wps_event_fail(struct wpa_supplicant *wpa_s,
375                                           struct wps_event_fail *fail)
376 {
377         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_FAIL "msg=%d", fail->msg);
378         wpas_clear_wps(wpa_s);
379 }
380
381
382 static void wpa_supplicant_wps_event_success(struct wpa_supplicant *wpa_s)
383 {
384         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_SUCCESS);
385         wpa_s->wps_success = 1;
386 }
387
388
389 static void wpa_supplicant_wps_event(void *ctx, enum wps_event event,
390                                      union wps_event_data *data)
391 {
392         struct wpa_supplicant *wpa_s = ctx;
393         switch (event) {
394         case WPS_EV_M2D:
395                 wpa_supplicant_wps_event_m2d(wpa_s, &data->m2d);
396                 break;
397         case WPS_EV_FAIL:
398                 wpa_supplicant_wps_event_fail(wpa_s, &data->fail);
399                 break;
400         case WPS_EV_SUCCESS:
401                 wpa_supplicant_wps_event_success(wpa_s);
402                 break;
403         case WPS_EV_PWD_AUTH_FAIL:
404                 break;
405         }
406 }
407
408
409 enum wps_request_type wpas_wps_get_req_type(struct wpa_ssid *ssid)
410 {
411         if (eap_is_wps_pbc_enrollee(&ssid->eap) ||
412             eap_is_wps_pin_enrollee(&ssid->eap))
413                 return WPS_REQ_ENROLLEE;
414         else
415                 return WPS_REQ_REGISTRAR;
416 }
417
418
419 static void wpas_clear_wps(struct wpa_supplicant *wpa_s)
420 {
421         int id;
422         struct wpa_ssid *ssid;
423
424         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
425
426         /* Remove any existing WPS network from configuration */
427         ssid = wpa_s->conf->ssid;
428         while (ssid) {
429                 if (ssid->key_mgmt & WPA_KEY_MGMT_WPS) {
430                         if (ssid == wpa_s->current_ssid)
431                                 wpa_s->current_ssid = NULL;
432                         id = ssid->id;
433                 } else
434                         id = -1;
435                 ssid = ssid->next;
436                 if (id >= 0)
437                         wpa_config_remove_network(wpa_s->conf, id);
438         }
439 }
440
441
442 static void wpas_wps_timeout(void *eloop_ctx, void *timeout_ctx)
443 {
444         struct wpa_supplicant *wpa_s = eloop_ctx;
445         wpa_printf(MSG_INFO, WPS_EVENT_TIMEOUT "Requested operation timed "
446                    "out");
447         wpas_clear_wps(wpa_s);
448 }
449
450
451 static struct wpa_ssid * wpas_wps_add_network(struct wpa_supplicant *wpa_s,
452                                               int registrar, const u8 *bssid)
453 {
454         struct wpa_ssid *ssid;
455
456         ssid = wpa_config_add_network(wpa_s->conf);
457         if (ssid == NULL)
458                 return NULL;
459         wpa_config_set_network_defaults(ssid);
460         if (wpa_config_set(ssid, "key_mgmt", "WPS", 0) < 0 ||
461             wpa_config_set(ssid, "eap", "WSC", 0) < 0 ||
462             wpa_config_set(ssid, "identity", registrar ?
463                            "\"" WSC_ID_REGISTRAR "\"" :
464                            "\"" WSC_ID_ENROLLEE "\"", 0) < 0) {
465                 wpa_config_remove_network(wpa_s->conf, ssid->id);
466                 return NULL;
467         }
468
469         if (bssid) {
470                 size_t i;
471                 struct wpa_scan_res *res;
472
473                 os_memcpy(ssid->bssid, bssid, ETH_ALEN);
474                 ssid->bssid_set = 1;
475
476                 /* Try to get SSID from scan results */
477                 if (wpa_s->scan_res == NULL &&
478                     wpa_supplicant_get_scan_results(wpa_s) < 0)
479                         return ssid; /* Could not find any scan results */
480
481                 for (i = 0; i < wpa_s->scan_res->num; i++) {
482                         const u8 *ie;
483
484                         res = wpa_s->scan_res->res[i];
485                         if (os_memcmp(bssid, res->bssid, ETH_ALEN) != 0)
486                                 continue;
487
488                         ie = wpa_scan_get_ie(res, WLAN_EID_SSID);
489                         if (ie == NULL)
490                                 break;
491                         os_free(ssid->ssid);
492                         ssid->ssid = os_malloc(ie[1]);
493                         if (ssid->ssid == NULL)
494                                 break;
495                         os_memcpy(ssid->ssid, ie + 2, ie[1]);
496                         ssid->ssid_len = ie[1];
497                         break;
498                 }
499         }
500
501         return ssid;
502 }
503
504
505 static void wpas_wps_reassoc(struct wpa_supplicant *wpa_s,
506                              struct wpa_ssid *selected)
507 {
508         struct wpa_ssid *ssid;
509
510         /* Mark all other networks disabled and trigger reassociation */
511         ssid = wpa_s->conf->ssid;
512         while (ssid) {
513                 ssid->disabled = ssid != selected;
514                 ssid = ssid->next;
515         }
516         wpa_s->disconnected = 0;
517         wpa_s->reassociate = 1;
518         wpa_s->scan_runs = 0;
519         wpa_s->wps_success = 0;
520         wpa_s->blacklist_cleared = 0;
521         wpa_supplicant_req_scan(wpa_s, 0, 0);
522 }
523
524
525 int wpas_wps_start_pbc(struct wpa_supplicant *wpa_s, const u8 *bssid)
526 {
527         struct wpa_ssid *ssid;
528         wpas_clear_wps(wpa_s);
529         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
530         if (ssid == NULL)
531                 return -1;
532         wpa_config_set(ssid, "phase1", "\"pbc=1\"", 0);
533         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
534                                wpa_s, NULL);
535         wpas_wps_reassoc(wpa_s, ssid);
536         return 0;
537 }
538
539
540 int wpas_wps_start_pin(struct wpa_supplicant *wpa_s, const u8 *bssid,
541                        const char *pin)
542 {
543         struct wpa_ssid *ssid;
544         char val[128];
545         unsigned int rpin = 0;
546
547         wpas_clear_wps(wpa_s);
548         ssid = wpas_wps_add_network(wpa_s, 0, bssid);
549         if (ssid == NULL)
550                 return -1;
551         if (pin)
552                 os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
553         else {
554                 rpin = wps_generate_pin();
555                 os_snprintf(val, sizeof(val), "\"pin=%08d\"", rpin);
556         }
557         wpa_config_set(ssid, "phase1", val, 0);
558         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
559                                wpa_s, NULL);
560         wpas_wps_reassoc(wpa_s, ssid);
561         return rpin;
562 }
563
564
565 #ifdef CONFIG_WPS_OOB
566 int wpas_wps_start_oob(struct wpa_supplicant *wpa_s, char *device_type,
567                        char *path, char *method, char *name)
568 {
569         struct wps_context *wps = wpa_s->wps;
570         struct oob_device_data *oob_dev;
571
572         oob_dev = wps_get_oob_device(device_type);
573         if (oob_dev == NULL)
574                 return -1;
575         oob_dev->device_path = path;
576         oob_dev->device_name = name;
577         wps->oob_conf.oob_method = wps_get_oob_method(method);
578
579         if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E) {
580                 /*
581                  * Use pre-configured DH keys in order to be able to write the
582                  * key hash into the OOB file.
583                  */
584                 wpabuf_free(wps->dh_pubkey);
585                 wpabuf_free(wps->dh_privkey);
586                 wps->dh_privkey = NULL;
587                 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
588                                          &wps->dh_privkey);
589                 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
590                 if (wps->dh_pubkey == NULL) {
591                         wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
592                                    "Diffie-Hellman handshake");
593                         return -1;
594                 }
595         }
596
597         if (wps->oob_conf.oob_method == OOB_METHOD_CRED)
598                 wpas_clear_wps(wpa_s);
599
600         if (wps_process_oob(wps, oob_dev, 0) < 0)
601                 return -1;
602
603         if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
604              wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
605             wpas_wps_start_pin(wpa_s, NULL,
606                                wpabuf_head(wps->oob_conf.dev_password)) < 0)
607                         return -1;
608
609         return 0;
610 }
611 #endif /* CONFIG_WPS_OOB */
612
613
614 int wpas_wps_start_reg(struct wpa_supplicant *wpa_s, const u8 *bssid,
615                        const char *pin)
616 {
617         struct wpa_ssid *ssid;
618         char val[30];
619
620         if (!pin)
621                 return -1;
622         wpas_clear_wps(wpa_s);
623         ssid = wpas_wps_add_network(wpa_s, 1, bssid);
624         if (ssid == NULL)
625                 return -1;
626         os_snprintf(val, sizeof(val), "\"pin=%s\"", pin);
627         wpa_config_set(ssid, "phase1", val, 0);
628         eloop_register_timeout(WPS_PBC_WALK_TIME, 0, wpas_wps_timeout,
629                                wpa_s, NULL);
630         wpas_wps_reassoc(wpa_s, ssid);
631         return 0;
632 }
633
634
635 static int wpas_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
636                                size_t psk_len)
637 {
638         wpa_printf(MSG_DEBUG, "WPS: Received new WPA/WPA2-PSK from WPS for "
639                    "STA " MACSTR, MAC2STR(mac_addr));
640         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
641
642         /* TODO */
643
644         return 0;
645 }
646
647
648 static void wpas_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
649                                    const struct wps_device_data *dev)
650 {
651         char uuid[40], txt[400];
652         int len;
653         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
654                 return;
655         wpa_printf(MSG_DEBUG, "WPS: PIN needed for UUID-E %s", uuid);
656         len = os_snprintf(txt, sizeof(txt), "WPS-EVENT-PIN-NEEDED %s " MACSTR
657                           " [%s|%s|%s|%s|%s|%d-%08X-%d]",
658                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
659                           dev->manufacturer, dev->model_name,
660                           dev->model_number, dev->serial_number,
661                           dev->categ, dev->oui, dev->sub_categ);
662         if (len > 0 && len < (int) sizeof(txt))
663                 wpa_printf(MSG_INFO, "%s", txt);
664 }
665
666
667 int wpas_wps_init(struct wpa_supplicant *wpa_s)
668 {
669         struct wps_context *wps;
670         struct wps_registrar_config rcfg;
671
672         wps = os_zalloc(sizeof(*wps));
673         if (wps == NULL)
674                 return -1;
675
676         wps->cred_cb = wpa_supplicant_wps_cred;
677         wps->event_cb = wpa_supplicant_wps_event;
678         wps->cb_ctx = wpa_s;
679
680         wps->dev.device_name = wpa_s->conf->device_name;
681         wps->dev.manufacturer = wpa_s->conf->manufacturer;
682         wps->dev.model_name = wpa_s->conf->model_name;
683         wps->dev.model_number = wpa_s->conf->model_number;
684         wps->dev.serial_number = wpa_s->conf->serial_number;
685         if (wpa_s->conf->device_type) {
686                 char *pos;
687                 u8 oui[4];
688                 /* <categ>-<OUI>-<subcateg> */
689                 wps->dev.categ = atoi(wpa_s->conf->device_type);
690                 pos = os_strchr(wpa_s->conf->device_type, '-');
691                 if (pos == NULL) {
692                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
693                         os_free(wps);
694                         return -1;
695                 }
696                 pos++;
697                 if (hexstr2bin(pos, oui, 4)) {
698                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
699                         os_free(wps);
700                         return -1;
701                 }
702                 wps->dev.oui = WPA_GET_BE32(oui);
703                 pos = os_strchr(pos, '-');
704                 if (pos == NULL) {
705                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
706                         os_free(wps);
707                         return -1;
708                 }
709                 pos++;
710                 wps->dev.sub_categ = atoi(pos);
711         }
712         wps->dev.os_version = WPA_GET_BE32(wpa_s->conf->os_version);
713         wps->dev.rf_bands = WPS_RF_24GHZ | WPS_RF_50GHZ; /* TODO: config */
714         os_memcpy(wps->dev.mac_addr, wpa_s->own_addr, ETH_ALEN);
715         if (is_nil_uuid(wpa_s->conf->uuid)) {
716                 uuid_gen_mac_addr(wpa_s->own_addr, wps->uuid);
717                 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
718                             wps->uuid, WPS_UUID_LEN);
719         } else
720                 os_memcpy(wps->uuid, wpa_s->conf->uuid, WPS_UUID_LEN);
721
722         wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
723         wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
724
725         os_memset(&rcfg, 0, sizeof(rcfg));
726         rcfg.new_psk_cb = wpas_wps_new_psk_cb;
727         rcfg.pin_needed_cb = wpas_wps_pin_needed_cb;
728         rcfg.cb_ctx = wpa_s;
729
730         wps->registrar = wps_registrar_init(wps, &rcfg);
731         if (wps->registrar == NULL) {
732                 wpa_printf(MSG_DEBUG, "Failed to initialize WPS Registrar");
733                 os_free(wps);
734                 return -1;
735         }
736
737         wpa_s->wps = wps;
738
739         return 0;
740 }
741
742
743 void wpas_wps_deinit(struct wpa_supplicant *wpa_s)
744 {
745         eloop_cancel_timeout(wpas_wps_timeout, wpa_s, NULL);
746
747         if (wpa_s->wps == NULL)
748                 return;
749
750         wps_registrar_deinit(wpa_s->wps->registrar);
751         wpabuf_free(wpa_s->wps->dh_pubkey);
752         wpabuf_free(wpa_s->wps->dh_privkey);
753         wpabuf_free(wpa_s->wps->oob_conf.pubkey_hash);
754         wpabuf_free(wpa_s->wps->oob_conf.dev_password);
755         os_free(wpa_s->wps->network_key);
756         os_free(wpa_s->wps);
757         wpa_s->wps = NULL;
758 }
759
760
761 int wpas_wps_ssid_bss_match(struct wpa_supplicant *wpa_s,
762                             struct wpa_ssid *ssid, struct wpa_scan_res *bss)
763 {
764         struct wpabuf *wps_ie;
765
766         if (!(ssid->key_mgmt & WPA_KEY_MGMT_WPS))
767                 return -1;
768
769         wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
770         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
771                 if (!wps_ie) {
772                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
773                         return 0;
774                 }
775
776                 if (!wps_is_selected_pbc_registrar(wps_ie)) {
777                         wpa_printf(MSG_DEBUG, "   skip - WPS AP "
778                                    "without active PBC Registrar");
779                         wpabuf_free(wps_ie);
780                         return 0;
781                 }
782
783                 /* TODO: overlap detection */
784                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
785                            "(Active PBC)");
786                 wpabuf_free(wps_ie);
787                 return 1;
788         }
789
790         if (eap_is_wps_pin_enrollee(&ssid->eap)) {
791                 if (!wps_ie) {
792                         wpa_printf(MSG_DEBUG, "   skip - non-WPS AP");
793                         return 0;
794                 }
795
796                 /*
797                  * Start with WPS APs that advertise active PIN Registrar and
798                  * allow any WPS AP after third scan since some APs do not set
799                  * Selected Registrar attribute properly when using external
800                  * Registrar.
801                  */
802                 if (!wps_is_selected_pin_registrar(wps_ie)) {
803                         if (wpa_s->scan_runs < WPS_PIN_SCAN_IGNORE_SEL_REG) {
804                                 wpa_printf(MSG_DEBUG, "   skip - WPS AP "
805                                            "without active PIN Registrar");
806                                 wpabuf_free(wps_ie);
807                                 return 0;
808                         }
809                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
810                 } else {
811                         wpa_printf(MSG_DEBUG, "   selected based on WPS IE "
812                                    "(Active PIN)");
813                 }
814                 wpabuf_free(wps_ie);
815                 return 1;
816         }
817
818         if (wps_ie) {
819                 wpa_printf(MSG_DEBUG, "   selected based on WPS IE");
820                 wpabuf_free(wps_ie);
821                 return 1;
822         }
823
824         return -1;
825 }
826
827
828 int wpas_wps_ssid_wildcard_ok(struct wpa_supplicant *wpa_s,
829                               struct wpa_ssid *ssid,
830                               struct wpa_scan_res *bss)
831 {
832         struct wpabuf *wps_ie = NULL;
833         int ret = 0;
834
835         if (eap_is_wps_pbc_enrollee(&ssid->eap)) {
836                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
837                 if (wps_ie && wps_is_selected_pbc_registrar(wps_ie)) {
838                         /* allow wildcard SSID for WPS PBC */
839                         ret = 1;
840                 }
841         } else if (eap_is_wps_pin_enrollee(&ssid->eap)) {
842                 wps_ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
843                 if (wps_ie &&
844                     (wps_is_selected_pin_registrar(wps_ie) ||
845                      wpa_s->scan_runs >= WPS_PIN_SCAN_IGNORE_SEL_REG)) {
846                         /* allow wildcard SSID for WPS PIN */
847                         ret = 1;
848                 }
849         }
850
851         if (!ret && ssid->bssid_set &&
852             os_memcmp(ssid->bssid, bss->bssid, ETH_ALEN) == 0) {
853                 /* allow wildcard SSID due to hardcoded BSSID match */
854                 ret = 1;
855         }
856
857         wpabuf_free(wps_ie);
858
859         return ret;
860 }
861
862
863 int wpas_wps_scan_pbc_overlap(struct wpa_supplicant *wpa_s,
864                               struct wpa_scan_res *selected,
865                               struct wpa_ssid *ssid)
866 {
867         const u8 *sel_uuid, *uuid;
868         size_t i;
869         struct wpabuf *wps_ie;
870         int ret = 0;
871
872         if (!eap_is_wps_pbc_enrollee(&ssid->eap))
873                 return 0;
874
875         /* Make sure that only one AP is in active PBC mode */
876         wps_ie = wpa_scan_get_vendor_ie_multi(selected, WPS_IE_VENDOR_TYPE);
877         if (wps_ie)
878                 sel_uuid = wps_get_uuid_e(wps_ie);
879         else
880                 sel_uuid = NULL;
881
882         for (i = 0; i < wpa_s->scan_res->num; i++) {
883                 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
884                 struct wpabuf *ie;
885                 if (bss == selected)
886                         continue;
887                 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
888                 if (!ie)
889                         continue;
890                 if (!wps_is_selected_pbc_registrar(ie)) {
891                         wpabuf_free(ie);
892                         continue;
893                 }
894                 uuid = wps_get_uuid_e(ie);
895                 if (sel_uuid == NULL || uuid == NULL ||
896                     os_memcmp(sel_uuid, uuid, 16) != 0) {
897                         ret = 1; /* PBC overlap */
898                         wpabuf_free(ie);
899                         break;
900                 }
901
902                 /* TODO: verify that this is reasonable dual-band situation */
903
904                 wpabuf_free(ie);
905         }
906
907         wpabuf_free(wps_ie);
908
909         return ret;
910 }
911
912
913 void wpas_wps_notify_scan_results(struct wpa_supplicant *wpa_s)
914 {
915         size_t i;
916
917         if (wpa_s->disconnected || wpa_s->wpa_state >= WPA_ASSOCIATED)
918                 return;
919
920         for (i = 0; i < wpa_s->scan_res->num; i++) {
921                 struct wpa_scan_res *bss = wpa_s->scan_res->res[i];
922                 struct wpabuf *ie;
923                 ie = wpa_scan_get_vendor_ie_multi(bss, WPS_IE_VENDOR_TYPE);
924                 if (!ie)
925                         continue;
926                 if (wps_is_selected_pbc_registrar(ie))
927                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PBC);
928                 else if (wps_is_selected_pin_registrar(ie))
929                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE_PIN);
930                 else
931                         wpa_msg(wpa_s, MSG_INFO, WPS_EVENT_AP_AVAILABLE);
932                 wpabuf_free(ie);
933                 break;
934         }
935 }
936
937
938 int wpas_wps_searching(struct wpa_supplicant *wpa_s)
939 {
940         struct wpa_ssid *ssid;
941
942         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
943                 if ((ssid->key_mgmt & WPA_KEY_MGMT_WPS) && !ssid->disabled)
944                         return 1;
945         }
946
947         return 0;
948 }