WPS UFD: Build OOB functionality only if UFD is enabled
[wpasupplicant] / hostapd / wps_hostapd.c
1 /*
2  * hostapd / 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 "hostapd.h"
18 #include "driver_i.h"
19 #include "eloop.h"
20 #include "uuid.h"
21 #include "wpa_ctrl.h"
22 #include "ieee802_11_defs.h"
23 #include "sta_info.h"
24 #include "eapol_sm.h"
25 #include "wps/wps.h"
26 #include "wps/wps_defs.h"
27 #include "wps/wps_dev_attr.h"
28 #include "wps_hostapd.h"
29 #include "dh_groups.h"
30
31
32 #ifdef CONFIG_WPS_UPNP
33 #include "wps/wps_upnp.h"
34 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
35                                  struct wps_context *wps);
36 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd);
37 #endif /* CONFIG_WPS_UPNP */
38
39
40 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
41                                   size_t psk_len)
42 {
43         struct hostapd_data *hapd = ctx;
44         struct hostapd_wpa_psk *p;
45         struct hostapd_ssid *ssid = &hapd->conf->ssid;
46
47         wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
48                    MACSTR, MAC2STR(mac_addr));
49         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
50
51         if (psk_len != PMK_LEN) {
52                 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
53                            (unsigned long) psk_len);
54                 return -1;
55         }
56
57         /* Add the new PSK to runtime PSK list */
58         p = os_zalloc(sizeof(*p));
59         if (p == NULL)
60                 return -1;
61         os_memcpy(p->addr, mac_addr, ETH_ALEN);
62         os_memcpy(p->psk, psk, PMK_LEN);
63
64         p->next = ssid->wpa_psk;
65         ssid->wpa_psk = p;
66
67         if (ssid->wpa_psk_file) {
68                 FILE *f;
69                 char hex[PMK_LEN * 2 + 1];
70                 /* Add the new PSK to PSK list file */
71                 f = fopen(ssid->wpa_psk_file, "a");
72                 if (f == NULL) {
73                         wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
74                                    "'%s'", ssid->wpa_psk_file);
75                         return -1;
76                 }
77
78                 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
79                 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
80                 fclose(f);
81         }
82
83         return 0;
84 }
85
86
87 static int hostapd_wps_set_ie_cb(void *ctx, const u8 *beacon_ie,
88                                  size_t beacon_ie_len, const u8 *probe_resp_ie,
89                                  size_t probe_resp_ie_len)
90 {
91         struct hostapd_data *hapd = ctx;
92
93         os_free(hapd->wps_beacon_ie);
94         if (beacon_ie_len == 0) {
95                 hapd->wps_beacon_ie = NULL;
96                 hapd->wps_beacon_ie_len = 0;
97         } else {
98                 hapd->wps_beacon_ie = os_malloc(beacon_ie_len);
99                 if (hapd->wps_beacon_ie == NULL) {
100                         hapd->wps_beacon_ie_len = 0;
101                         return -1;
102                 }
103                 os_memcpy(hapd->wps_beacon_ie, beacon_ie, beacon_ie_len);
104                 hapd->wps_beacon_ie_len = beacon_ie_len;
105         }
106         hostapd_set_wps_beacon_ie(hapd, hapd->wps_beacon_ie,
107                                   hapd->wps_beacon_ie_len);
108
109         os_free(hapd->wps_probe_resp_ie);
110         if (probe_resp_ie_len == 0) {
111                 hapd->wps_probe_resp_ie = NULL;
112                 hapd->wps_probe_resp_ie_len = 0;
113         } else {
114                 hapd->wps_probe_resp_ie = os_malloc(probe_resp_ie_len);
115                 if (hapd->wps_probe_resp_ie == NULL) {
116                         hapd->wps_probe_resp_ie_len = 0;
117                         return -1;
118                 }
119                 os_memcpy(hapd->wps_probe_resp_ie, probe_resp_ie,
120                           probe_resp_ie_len);
121                 hapd->wps_probe_resp_ie_len = probe_resp_ie_len;
122         }
123         hostapd_set_wps_probe_resp_ie(hapd, hapd->wps_probe_resp_ie,
124                                       hapd->wps_probe_resp_ie_len);
125
126         return 0;
127 }
128
129
130 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
131                                       const struct wps_device_data *dev)
132 {
133         struct hostapd_data *hapd = ctx;
134         char uuid[40], txt[400];
135         int len;
136         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
137                 return;
138         wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
139         len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
140                           "%s " MACSTR " [%s|%s|%s|%s|%s|%d-%08X-%d]",
141                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
142                           dev->manufacturer, dev->model_name,
143                           dev->model_number, dev->serial_number,
144                           dev->categ, dev->oui, dev->sub_categ);
145         if (len > 0 && len < (int) sizeof(txt))
146                 wpa_msg(hapd, MSG_INFO, "%s", txt);
147
148         if (hapd->conf->wps_pin_requests) {
149                 FILE *f;
150                 struct os_time t;
151                 f = fopen(hapd->conf->wps_pin_requests, "a");
152                 if (f == NULL)
153                         return;
154                 os_get_time(&t);
155                 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
156                         "\t%d-%08X-%d\n",
157                         t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
158                         dev->manufacturer, dev->model_name, dev->model_number,
159                         dev->serial_number,
160                         dev->categ, dev->oui, dev->sub_categ);
161                 fclose(f);
162         }
163 }
164
165
166 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
167                                        const u8 *uuid_e)
168 {
169         struct hostapd_data *hapd = ctx;
170         char uuid[40];
171         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
172                 return;
173         wpa_msg(hapd, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
174                 MAC2STR(mac_addr), uuid);
175 }
176
177
178 static int str_starts(const char *str, const char *start)
179 {
180         return os_strncmp(str, start, os_strlen(start)) == 0;
181 }
182
183
184 static void wps_reload_config(void *eloop_data, void *user_ctx)
185 {
186         struct hostapd_iface *iface = eloop_data;
187
188         wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
189         if (hostapd_reload_config(iface) < 0) {
190                 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
191                            "configuration");
192         }
193 }
194
195
196 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
197 {
198         struct hostapd_data *hapd = ctx;
199         FILE *oconf, *nconf;
200         size_t len, i;
201         char *tmp_fname;
202         char buf[1024];
203         int multi_bss;
204         int wpa;
205
206         wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
207                         cred->cred_attr, cred->cred_attr_len);
208
209         wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
210         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
211         wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
212                    cred->auth_type);
213         wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
214         wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
215         wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
216                         cred->key, cred->key_len);
217         wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
218                    MAC2STR(cred->mac_addr));
219
220         if ((hapd->conf->wps_cred_processing == 1 ||
221              hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
222                 size_t blen = cred->cred_attr_len * 2 + 1;
223                 char *buf = os_malloc(blen);
224                 if (buf) {
225                         wpa_snprintf_hex(buf, blen,
226                                          cred->cred_attr, cred->cred_attr_len);
227                         wpa_msg(hapd, MSG_INFO, "%s%s",
228                                 WPS_EVENT_NEW_AP_SETTINGS, buf);
229                         os_free(buf);
230                 }
231         } else
232                 wpa_msg(hapd, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
233
234         if (hapd->conf->wps_cred_processing == 1)
235                 return 0;
236
237         len = os_strlen(hapd->iface->config_fname) + 5;
238         tmp_fname = os_malloc(len);
239         if (tmp_fname == NULL)
240                 return -1;
241         os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
242
243         oconf = fopen(hapd->iface->config_fname, "r");
244         if (oconf == NULL) {
245                 wpa_printf(MSG_WARNING, "WPS: Could not open current "
246                            "configuration file");
247                 os_free(tmp_fname);
248                 return -1;
249         }
250
251         nconf = fopen(tmp_fname, "w");
252         if (nconf == NULL) {
253                 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
254                            "configuration file");
255                 os_free(tmp_fname);
256                 fclose(oconf);
257                 return -1;
258         }
259
260         fprintf(nconf, "# WPS configuration - START\n");
261
262         fprintf(nconf, "wps_state=2\n");
263
264         fprintf(nconf, "ssid=");
265         for (i = 0; i < cred->ssid_len; i++)
266                 fputc(cred->ssid[i], nconf);
267         fprintf(nconf, "\n");
268
269         if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
270             (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
271                 wpa = 3;
272         else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
273                 wpa = 2;
274         else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
275                 wpa = 1;
276         else
277                 wpa = 0;
278
279         if (wpa) {
280                 char *prefix;
281                 fprintf(nconf, "wpa=%d\n", wpa);
282
283                 fprintf(nconf, "wpa_key_mgmt=");
284                 prefix = "";
285                 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
286                         fprintf(nconf, "WPA-EAP");
287                         prefix = " ";
288                 }
289                 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
290                         fprintf(nconf, "%sWPA-PSK", prefix);
291                 fprintf(nconf, "\n");
292
293                 fprintf(nconf, "wpa_pairwise=");
294                 prefix = "";
295                 if (cred->encr_type & WPS_ENCR_AES) {
296                         fprintf(nconf, "CCMP");
297                         prefix = " ";
298                 }
299                 if (cred->encr_type & WPS_ENCR_TKIP) {
300                         fprintf(nconf, "%sTKIP", prefix);
301                 }
302                 fprintf(nconf, "\n");
303
304                 if (cred->key_len >= 8 && cred->key_len < 64) {
305                         fprintf(nconf, "wpa_passphrase=");
306                         for (i = 0; i < cred->key_len; i++)
307                                 fputc(cred->key[i], nconf);
308                         fprintf(nconf, "\n");
309                 } else if (cred->key_len == 64) {
310                         fprintf(nconf, "wpa_psk=");
311                         for (i = 0; i < cred->key_len; i++)
312                                 fputc(cred->key[i], nconf);
313                         fprintf(nconf, "\n");
314                 } else {
315                         wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
316                                    "for WPA/WPA2",
317                                    (unsigned long) cred->key_len);
318                 }
319
320                 fprintf(nconf, "auth_algs=1\n");
321         } else {
322                 if ((cred->auth_type & WPS_AUTH_OPEN) &&
323                     (cred->auth_type & WPS_AUTH_SHARED))
324                         fprintf(nconf, "auth_algs=3\n");
325                 else if (cred->auth_type & WPS_AUTH_SHARED)
326                         fprintf(nconf, "auth_algs=2\n");
327                 else
328                         fprintf(nconf, "auth_algs=1\n");
329
330                 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx <= 4) {
331                         int key_idx = cred->key_idx;
332                         if (key_idx)
333                                 key_idx--;
334                         fprintf(nconf, "wep_default_key=%d\n", key_idx);
335                         fprintf(nconf, "wep_key%d=", key_idx);
336                         if (cred->key_len != 10 && cred->key_len != 26)
337                                 fputc('"', nconf);
338                         for (i = 0; i < cred->key_len; i++)
339                                 fputc(cred->key[i], nconf);
340                         if (cred->key_len != 10 && cred->key_len != 26)
341                                 fputc('"', nconf);
342                         fprintf(nconf, "\n");
343                 }
344         }
345
346         fprintf(nconf, "# WPS configuration - END\n");
347
348         multi_bss = 0;
349         while (fgets(buf, sizeof(buf), oconf)) {
350                 if (os_strncmp(buf, "bss=", 4) == 0)
351                         multi_bss = 1;
352                 if (!multi_bss &&
353                     (str_starts(buf, "ssid=") ||
354                      str_starts(buf, "auth_algs=") ||
355                      str_starts(buf, "wps_state=") ||
356                      str_starts(buf, "wpa=") ||
357                      str_starts(buf, "wpa_psk=") ||
358                      str_starts(buf, "wpa_pairwise=") ||
359                      str_starts(buf, "rsn_pairwise=") ||
360                      str_starts(buf, "wpa_key_mgmt=") ||
361                      str_starts(buf, "wpa_passphrase="))) {
362                         fprintf(nconf, "#WPS# %s", buf);
363                 } else
364                         fprintf(nconf, "%s", buf);
365         }
366
367         fclose(nconf);
368         fclose(oconf);
369
370         if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
371                 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
372                            "configuration file: %s", strerror(errno));
373                 os_free(tmp_fname);
374                 return -1;
375         }
376
377         os_free(tmp_fname);
378
379         /* Schedule configuration reload after short period of time to allow
380          * EAP-WSC to be finished.
381          */
382         eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
383                                NULL);
384
385         /* TODO: dualband AP may need to update multiple configuration files */
386
387         wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
388
389         return 0;
390 }
391
392
393 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
394                                   struct wps_event_pwd_auth_fail *data)
395 {
396         FILE *f;
397
398         if (!data->enrollee)
399                 return;
400
401         /*
402          * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
403          * if this happens multiple times.
404          */
405         hapd->ap_pin_failures++;
406         if (hapd->ap_pin_failures < 4)
407                 return;
408
409         wpa_msg(hapd, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
410         hapd->wps->ap_setup_locked = 1;
411
412         wps_registrar_update_ie(hapd->wps->registrar);
413
414         if (hapd->conf->wps_cred_processing == 1)
415                 return;
416
417         f = fopen(hapd->iface->config_fname, "a");
418         if (f == NULL) {
419                 wpa_printf(MSG_WARNING, "WPS: Could not append to the current "
420                            "configuration file");
421                 return;
422         }
423
424         fprintf(f, "# WPS AP Setup Locked based on possible attack\n");
425         fprintf(f, "ap_setup_locked=1\n");
426         fclose(f);
427
428         /* TODO: dualband AP may need to update multiple configuration files */
429
430         wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
431 }
432
433
434 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
435                                  union wps_event_data *data)
436 {
437         struct hostapd_data *hapd = ctx;
438
439         if (event == WPS_EV_PWD_AUTH_FAIL)
440                 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
441 }
442
443
444 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
445 {
446         os_free(hapd->wps_beacon_ie);
447         hapd->wps_beacon_ie = NULL;
448         hapd->wps_beacon_ie_len = 0;
449         hostapd_set_wps_beacon_ie(hapd, NULL, 0);
450
451         os_free(hapd->wps_probe_resp_ie);
452         hapd->wps_probe_resp_ie = NULL;
453         hapd->wps_probe_resp_ie_len = 0;
454         hostapd_set_wps_probe_resp_ie(hapd, NULL, 0);
455 }
456
457
458 int hostapd_init_wps(struct hostapd_data *hapd,
459                      struct hostapd_bss_config *conf)
460 {
461         struct wps_context *wps;
462         struct wps_registrar_config cfg;
463
464         if (conf->wps_state == 0) {
465                 hostapd_wps_clear_ies(hapd);
466                 return 0;
467         }
468
469         wps = os_zalloc(sizeof(*wps));
470         if (wps == NULL)
471                 return -1;
472
473         wps->cred_cb = hostapd_wps_cred_cb;
474         wps->event_cb = hostapd_wps_event_cb;
475         wps->cb_ctx = hapd;
476
477         os_memset(&cfg, 0, sizeof(cfg));
478         wps->wps_state = hapd->conf->wps_state;
479         wps->ap_setup_locked = hapd->conf->ap_setup_locked;
480         if (is_nil_uuid(hapd->conf->uuid)) {
481                 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
482                 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
483                             wps->uuid, UUID_LEN);
484         } else
485                 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
486         wps->ssid_len = hapd->conf->ssid.ssid_len;
487         os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
488         wps->ap = 1;
489         os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
490         wps->dev.device_name = hapd->conf->device_name ?
491                 os_strdup(hapd->conf->device_name) : NULL;
492         wps->dev.manufacturer = hapd->conf->manufacturer ?
493                 os_strdup(hapd->conf->manufacturer) : NULL;
494         wps->dev.model_name = hapd->conf->model_name ?
495                 os_strdup(hapd->conf->model_name) : NULL;
496         wps->dev.model_number = hapd->conf->model_number ?
497                 os_strdup(hapd->conf->model_number) : NULL;
498         wps->dev.serial_number = hapd->conf->serial_number ?
499                 os_strdup(hapd->conf->serial_number) : NULL;
500         if (hapd->conf->config_methods) {
501                 char *m = hapd->conf->config_methods;
502                 if (os_strstr(m, "label"))
503                         wps->config_methods |= WPS_CONFIG_LABEL;
504                 if (os_strstr(m, "display"))
505                         wps->config_methods |= WPS_CONFIG_DISPLAY;
506                 if (os_strstr(m, "push_button"))
507                         wps->config_methods |= WPS_CONFIG_PUSHBUTTON;
508                 if (os_strstr(m, "keypad"))
509                         wps->config_methods |= WPS_CONFIG_KEYPAD;
510         }
511         if (hapd->conf->device_type) {
512                 char *pos;
513                 u8 oui[4];
514                 /* <categ>-<OUI>-<subcateg> */
515                 wps->dev.categ = atoi(hapd->conf->device_type);
516                 pos = os_strchr(hapd->conf->device_type, '-');
517                 if (pos == NULL) {
518                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
519                         os_free(wps);
520                         return -1;
521                 }
522                 pos++;
523                 if (hexstr2bin(pos, oui, 4)) {
524                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
525                         os_free(wps);
526                         return -1;
527                 }
528                 wps->dev.oui = WPA_GET_BE32(oui);
529                 pos = os_strchr(pos, '-');
530                 if (pos == NULL) {
531                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
532                         os_free(wps);
533                         return -1;
534                 }
535                 pos++;
536                 wps->dev.sub_categ = atoi(pos);
537         }
538         wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
539         wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
540                 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
541
542         if (conf->wpa & WPA_PROTO_RSN) {
543                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
544                         wps->auth_types |= WPS_AUTH_WPA2PSK;
545                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
546                         wps->auth_types |= WPS_AUTH_WPA2;
547
548                 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
549                         wps->encr_types |= WPS_ENCR_AES;
550                 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
551                         wps->encr_types |= WPS_ENCR_TKIP;
552         }
553
554         if (conf->wpa & WPA_PROTO_WPA) {
555                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
556                         wps->auth_types |= WPS_AUTH_WPAPSK;
557                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
558                         wps->auth_types |= WPS_AUTH_WPA;
559
560                 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
561                         wps->encr_types |= WPS_ENCR_AES;
562                 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
563                         wps->encr_types |= WPS_ENCR_TKIP;
564         }
565
566         if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
567                 wps->encr_types |= WPS_ENCR_NONE;
568                 wps->auth_types |= WPS_AUTH_OPEN;
569         } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
570                 wps->encr_types |= WPS_ENCR_WEP;
571                 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
572                         wps->auth_types |= WPS_AUTH_OPEN;
573                 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
574                         wps->auth_types |= WPS_AUTH_SHARED;
575         } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
576                 wps->auth_types |= WPS_AUTH_OPEN;
577                 if (conf->default_wep_key_len)
578                         wps->encr_types |= WPS_ENCR_WEP;
579                 else
580                         wps->encr_types |= WPS_ENCR_NONE;
581         }
582
583         if (conf->ssid.wpa_psk_file) {
584                 /* Use per-device PSKs */
585         } else if (conf->ssid.wpa_passphrase) {
586                 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
587                 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
588         } else if (conf->ssid.wpa_psk) {
589                 wps->network_key = os_malloc(2 * PMK_LEN + 1);
590                 if (wps->network_key == NULL) {
591                         os_free(wps);
592                         return -1;
593                 }
594                 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
595                                  conf->ssid.wpa_psk->psk, PMK_LEN);
596                 wps->network_key_len = 2 * PMK_LEN;
597         } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
598                 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
599                 if (wps->network_key == NULL) {
600                         os_free(wps);
601                         return -1;
602                 }
603                 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
604                           conf->ssid.wep.len[0]);
605                 wps->network_key_len = conf->ssid.wep.len[0];
606         }
607
608         if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
609                 /* Override parameters to enable security by default */
610                 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
611                 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
612         }
613
614         wps->ap_settings = conf->ap_settings;
615         wps->ap_settings_len = conf->ap_settings_len;
616
617         cfg.new_psk_cb = hostapd_wps_new_psk_cb;
618         cfg.set_ie_cb = hostapd_wps_set_ie_cb;
619         cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
620         cfg.reg_success_cb = hostapd_wps_reg_success_cb;
621         cfg.cb_ctx = hapd;
622         cfg.skip_cred_build = conf->skip_cred_build;
623         cfg.extra_cred = conf->extra_cred;
624         cfg.extra_cred_len = conf->extra_cred_len;
625         cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
626                 conf->skip_cred_build;
627
628         wps->registrar = wps_registrar_init(wps, &cfg);
629         if (wps->registrar == NULL) {
630                 printf("Failed to initialize WPS Registrar\n");
631                 os_free(wps->network_key);
632                 os_free(wps);
633                 return -1;
634         }
635
636 #ifdef CONFIG_WPS_UPNP
637         wps->friendly_name = hapd->conf->friendly_name;
638         wps->manufacturer_url = hapd->conf->manufacturer_url;
639         wps->model_description = hapd->conf->model_description;
640         wps->model_url = hapd->conf->model_url;
641         wps->upc = hapd->conf->upc;
642
643         if (hostapd_wps_upnp_init(hapd, wps) < 0) {
644                 wpa_printf(MSG_ERROR, "Failed to initialize WPS UPnP");
645                 wps_registrar_deinit(wps->registrar);
646                 os_free(wps->network_key);
647                 os_free(wps);
648                 return -1;
649         }
650 #endif /* CONFIG_WPS_UPNP */
651
652         hapd->wps = wps;
653
654         return 0;
655 }
656
657
658 void hostapd_deinit_wps(struct hostapd_data *hapd)
659 {
660         if (hapd->wps == NULL)
661                 return;
662 #ifdef CONFIG_WPS_UPNP
663         hostapd_wps_upnp_deinit(hapd);
664 #endif /* CONFIG_WPS_UPNP */
665         wps_registrar_deinit(hapd->wps->registrar);
666         os_free(hapd->wps->network_key);
667         wps_device_data_free(&hapd->wps->dev);
668         wpabuf_free(hapd->wps->dh_pubkey);
669         wpabuf_free(hapd->wps->dh_privkey);
670         wpabuf_free(hapd->wps->oob_conf.pubkey_hash);
671         wpabuf_free(hapd->wps->oob_conf.dev_password);
672         wps_free_pending_msgs(hapd->wps->upnp_msgs);
673         os_free(hapd->wps);
674         hapd->wps = NULL;
675         hostapd_wps_clear_ies(hapd);
676 }
677
678
679 int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
680                         const char *pin)
681 {
682         u8 u[UUID_LEN];
683         int any = 0;
684
685         if (hapd->wps == NULL)
686                 return -1;
687         if (os_strcmp(uuid, "any") == 0)
688                 any = 1;
689         else if (uuid_str2bin(uuid, u))
690                 return -1;
691         return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
692                                      (const u8 *) pin, os_strlen(pin));
693 }
694
695
696 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
697 {
698         if (hapd->wps == NULL)
699                 return -1;
700         return wps_registrar_button_pushed(hapd->wps->registrar);
701 }
702
703
704 #ifdef CONFIG_WPS_OOB
705 int hostapd_wps_start_oob(struct hostapd_data *hapd, char *device_type,
706                           char *path, char *method)
707 {
708         struct wps_context *wps = hapd->wps;
709         struct oob_device_data *oob_dev;
710
711         oob_dev = wps_get_oob_device(device_type);
712         if (oob_dev == NULL)
713                 return -1;
714         oob_dev->device_path = path;
715         wps->oob_conf.oob_method = wps_get_oob_method(method);
716
717         if (wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) {
718                 /*
719                  * Use pre-configured DH keys in order to be able to write the
720                  * key hash into the OOB file.
721                  */
722                 wpabuf_free(wps->dh_pubkey);
723                 wpabuf_free(wps->dh_privkey);
724                 wps->dh_privkey = NULL;
725                 wps->dh_pubkey = dh_init(dh_groups_get(WPS_DH_GROUP),
726                                          &wps->dh_privkey);
727                 wps->dh_pubkey = wpabuf_zeropad(wps->dh_pubkey, 192);
728                 if (wps->dh_pubkey == NULL) {
729                         wpa_printf(MSG_ERROR, "WPS: Failed to initialize "
730                                    "Diffie-Hellman handshake");
731                         return -1;
732                 }
733         }
734
735         if (wps_process_oob(wps, oob_dev, 1) < 0)
736                 goto error;
737
738         if ((wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_E ||
739              wps->oob_conf.oob_method == OOB_METHOD_DEV_PWD_R) &&
740             hostapd_wps_add_pin(hapd, "any",
741                                 wpabuf_head(wps->oob_conf.dev_password)) < 0)
742                 goto error;
743
744         return 0;
745
746 error:
747         wpabuf_free(wps->dh_pubkey);
748         wps->dh_pubkey = NULL;
749         wpabuf_free(wps->dh_privkey);
750         wps->dh_privkey = NULL;
751         return -1;
752 }
753 #endif /* CONFIG_WPS_OOB */
754
755
756 void hostapd_wps_probe_req_rx(struct hostapd_data *hapd, const u8 *addr,
757                               const u8 *ie, size_t ie_len)
758 {
759         struct wpabuf *wps_ie;
760         const u8 *end, *pos, *wps;
761
762         if (hapd->wps == NULL)
763                 return;
764
765         pos = ie;
766         end = ie + ie_len;
767         wps = NULL;
768
769         while (pos + 1 < end) {
770                 if (pos + 2 + pos[1] > end)
771                         return;
772                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
773                     WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA) {
774                         wps = pos;
775                         break;
776                 }
777                 pos += 2 + pos[1];
778         }
779
780         if (wps == NULL)
781                 return; /* No WPS IE in Probe Request */
782
783         wps_ie = wpabuf_alloc(ie_len);
784         if (wps_ie == NULL)
785                 return;
786
787         /* There may be multiple WPS IEs in the message, so need to concatenate
788          * their WPS Data fields */
789         while (pos + 1 < end) {
790                 if (pos + 2 + pos[1] > end)
791                         break;
792                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
793                     WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA)
794                         wpabuf_put_data(wps_ie, pos + 6, pos[1] - 4);
795                 pos += 2 + pos[1];
796         }
797
798         if (wpabuf_len(wps_ie) > 0) {
799                 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);
800 #ifdef CONFIG_WPS_UPNP
801                 /* FIX: what exactly should be included in the WLANEvent?
802                  * WPS attributes? Full ProbeReq frame? */
803                 upnp_wps_device_send_wlan_event(hapd->wps_upnp, addr,
804                                                 UPNP_WPS_WLANEVENT_TYPE_PROBE,
805                                                 wps_ie);
806 #endif /* CONFIG_WPS_UPNP */
807         }
808
809         wpabuf_free(wps_ie);
810 }
811
812
813 #ifdef CONFIG_WPS_UPNP
814
815 static struct wpabuf *
816 hostapd_rx_req_get_device_info(void *priv, struct upnp_wps_peer *peer)
817 {
818         struct hostapd_data *hapd = priv;
819         struct wps_config cfg;
820         struct wps_data *wps;
821         enum wsc_op_code op_code;
822         struct wpabuf *m1;
823
824         /*
825          * Request for DeviceInfo, i.e., M1 TLVs. This is a start of WPS
826          * registration over UPnP with the AP acting as an Enrollee. It should
827          * be noted that this is frequently used just to get the device data,
828          * i.e., there may not be any intent to actually complete the
829          * registration.
830          */
831
832         if (peer->wps)
833                 wps_deinit(peer->wps);
834
835         os_memset(&cfg, 0, sizeof(cfg));
836         cfg.wps = hapd->wps;
837         cfg.pin = (u8 *) hapd->conf->ap_pin;
838         cfg.pin_len = os_strlen(hapd->conf->ap_pin);
839         wps = wps_init(&cfg);
840         if (wps == NULL)
841                 return NULL;
842
843         m1 = wps_get_msg(wps, &op_code);
844         if (m1 == NULL) {
845                 wps_deinit(wps);
846                 return NULL;
847         }
848
849         peer->wps = wps;
850
851         return m1;
852 }
853
854
855 static struct wpabuf *
856 hostapd_rx_req_put_message(void *priv, struct upnp_wps_peer *peer,
857                            const struct wpabuf *msg)
858 {
859         enum wps_process_res res;
860         enum wsc_op_code op_code;
861
862         /* PutMessage: msg = InMessage, return OutMessage */
863         res = wps_process_msg(peer->wps, WSC_UPnP, msg);
864         if (res == WPS_FAILURE)
865                 return NULL;
866         return wps_get_msg(peer->wps, &op_code);
867 }
868
869
870 static struct wpabuf *
871 hostapd_rx_req_get_ap_settings(void *priv, const struct wpabuf *msg)
872 {
873         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
874         return NULL;
875 }
876
877
878 static int hostapd_rx_req_set_ap_settings(void *priv, const struct wpabuf *msg)
879 {
880         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
881         return -1;
882 }
883
884
885 static int hostapd_rx_req_del_ap_settings(void *priv, const struct wpabuf *msg)
886 {
887         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
888         return -1;
889 }
890
891
892 static struct wpabuf *
893 hostapd_rx_req_get_sta_settings(void *priv, const struct wpabuf *msg)
894 {
895         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
896         return NULL;
897 }
898
899
900 static int hostapd_rx_req_set_sta_settings(void *priv,
901                                            const struct wpabuf *msg)
902 {
903         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
904         return -1;
905 }
906
907
908 static int hostapd_rx_req_del_sta_settings(void *priv,
909                                            const struct wpabuf *msg)
910 {
911         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
912         return -1;
913 }
914
915
916 static int hostapd_rx_req_put_wlan_response(
917         void *priv, enum upnp_wps_wlanevent_type ev_type,
918         const u8 *mac_addr, const struct wpabuf *msg,
919         enum wps_msg_type msg_type)
920 {
921         struct hostapd_data *hapd = priv;
922         struct sta_info *sta;
923         struct upnp_pending_message *p;
924
925         wpa_printf(MSG_DEBUG, "WPS UPnP: PutWLANResponse ev_type=%d mac_addr="
926                    MACSTR, ev_type, MAC2STR(mac_addr));
927         wpa_hexdump_ascii(MSG_MSGDUMP, "WPS UPnP: PutWLANResponse NewMessage",
928                           wpabuf_head(msg), wpabuf_len(msg));
929         if (ev_type != UPNP_WPS_WLANEVENT_TYPE_EAP) {
930                 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored unexpected "
931                            "PutWLANResponse WLANEventType %d", ev_type);
932                 return -1;
933         }
934
935         /*
936          * EAP response to ongoing to WPS Registration. Send it to EAP-WSC
937          * server implementation for delivery to the peer.
938          */
939
940         sta = ap_get_sta(hapd, mac_addr);
941         if (!sta) {
942                 /*
943                  * Workaround - Intel wsccmd uses bogus NewWLANEventMAC:
944                  * Pick STA that is in an ongoing WPS registration without
945                  * checking the MAC address.
946                  */
947                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found based "
948                            "on NewWLANEventMAC; try wildcard match");
949                 for (sta = hapd->sta_list; sta; sta = sta->next) {
950                         if (sta->eapol_sm && (sta->flags & WLAN_STA_WPS))
951                                 break;
952                 }
953         }
954
955         if (!sta) {
956                 wpa_printf(MSG_DEBUG, "WPS UPnP: No matching STA found");
957                 return 0;
958         }
959
960         p = os_zalloc(sizeof(*p));
961         if (p == NULL)
962                 return -1;
963         os_memcpy(p->addr, sta->addr, ETH_ALEN);
964         p->msg = wpabuf_dup(msg);
965         p->type = msg_type;
966         p->next = hapd->wps->upnp_msgs;
967         hapd->wps->upnp_msgs = p;
968
969         return eapol_auth_eap_pending_cb(sta->eapol_sm, sta->eapol_sm->eap);
970 }
971
972
973 static int hostapd_rx_req_set_selected_registrar(void *priv,
974                                                  const struct wpabuf *msg)
975 {
976         struct hostapd_data *hapd = priv;
977         return wps_registrar_set_selected_registrar(hapd->wps->registrar, msg);
978 }
979
980
981 static int hostapd_rx_req_reboot_ap(void *priv, const struct wpabuf *msg)
982 {
983         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
984         return -1;
985 }
986
987
988 static int hostapd_rx_req_reset_ap(void *priv, const struct wpabuf *msg)
989 {
990         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
991         return -1;
992 }
993
994
995 static int hostapd_rx_req_reboot_sta(void *priv, const struct wpabuf *msg)
996 {
997         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
998         return -1;
999 }
1000
1001
1002 static int hostapd_rx_req_reset_sta(void *priv, const struct wpabuf *msg)
1003 {
1004         wpa_printf(MSG_DEBUG, "WPS UPnP: TODO %s", __func__);
1005         return -1;
1006 }
1007
1008
1009 static int hostapd_wps_upnp_init(struct hostapd_data *hapd,
1010                                  struct wps_context *wps)
1011 {
1012         struct upnp_wps_device_ctx *ctx;
1013
1014         if (!hapd->conf->upnp_iface)
1015                 return 0;
1016         ctx = os_zalloc(sizeof(*ctx));
1017         if (ctx == NULL)
1018                 return -1;
1019
1020         ctx->rx_req_get_device_info = hostapd_rx_req_get_device_info;
1021         ctx->rx_req_put_message = hostapd_rx_req_put_message;
1022         ctx->rx_req_get_ap_settings = hostapd_rx_req_get_ap_settings;
1023         ctx->rx_req_set_ap_settings = hostapd_rx_req_set_ap_settings;
1024         ctx->rx_req_del_ap_settings = hostapd_rx_req_del_ap_settings;
1025         ctx->rx_req_get_sta_settings = hostapd_rx_req_get_sta_settings;
1026         ctx->rx_req_set_sta_settings = hostapd_rx_req_set_sta_settings;
1027         ctx->rx_req_del_sta_settings = hostapd_rx_req_del_sta_settings;
1028         ctx->rx_req_put_wlan_response = hostapd_rx_req_put_wlan_response;
1029         ctx->rx_req_set_selected_registrar =
1030                 hostapd_rx_req_set_selected_registrar;
1031         ctx->rx_req_reboot_ap = hostapd_rx_req_reboot_ap;
1032         ctx->rx_req_reset_ap = hostapd_rx_req_reset_ap;
1033         ctx->rx_req_reboot_sta = hostapd_rx_req_reboot_sta;
1034         ctx->rx_req_reset_sta = hostapd_rx_req_reset_sta;
1035
1036         hapd->wps_upnp = upnp_wps_device_init(ctx, wps, hapd);
1037         if (hapd->wps_upnp == NULL) {
1038                 os_free(ctx);
1039                 return -1;
1040         }
1041         wps->wps_upnp = hapd->wps_upnp;
1042
1043         if (upnp_wps_device_start(hapd->wps_upnp, hapd->conf->upnp_iface)) {
1044                 upnp_wps_device_deinit(hapd->wps_upnp);
1045                 hapd->wps_upnp = NULL;
1046                 return -1;
1047         }
1048
1049         return 0;
1050 }
1051
1052
1053 static void hostapd_wps_upnp_deinit(struct hostapd_data *hapd)
1054 {
1055         upnp_wps_device_deinit(hapd->wps_upnp);
1056 }
1057
1058 #endif /* CONFIG_WPS_UPNP */