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