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