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