WPS: Lock AP Setup on multiple AP PIN validation failures
[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 "wps/wps.h"
24 #include "wps/wps_defs.h"
25 #include "wps/wps_dev_attr.h"
26 #include "wps_hostapd.h"
27
28
29 static int hostapd_wps_new_psk_cb(void *ctx, const u8 *mac_addr, const u8 *psk,
30                                   size_t psk_len)
31 {
32         struct hostapd_data *hapd = ctx;
33         struct hostapd_wpa_psk *p;
34         struct hostapd_ssid *ssid = &hapd->conf->ssid;
35
36         wpa_printf(MSG_DEBUG, "Received new WPA/WPA2-PSK from WPS for STA "
37                    MACSTR, MAC2STR(mac_addr));
38         wpa_hexdump_key(MSG_DEBUG, "Per-device PSK", psk, psk_len);
39
40         if (psk_len != PMK_LEN) {
41                 wpa_printf(MSG_DEBUG, "Unexpected PSK length %lu",
42                            (unsigned long) psk_len);
43                 return -1;
44         }
45
46         /* Add the new PSK to runtime PSK list */
47         p = os_zalloc(sizeof(*p));
48         if (p == NULL)
49                 return -1;
50         os_memcpy(p->addr, mac_addr, ETH_ALEN);
51         os_memcpy(p->psk, psk, PMK_LEN);
52
53         p->next = ssid->wpa_psk;
54         ssid->wpa_psk = p;
55
56         if (ssid->wpa_psk_file) {
57                 FILE *f;
58                 char hex[PMK_LEN * 2 + 1];
59                 /* Add the new PSK to PSK list file */
60                 f = fopen(ssid->wpa_psk_file, "a");
61                 if (f == NULL) {
62                         wpa_printf(MSG_DEBUG, "Failed to add the PSK to "
63                                    "'%s'", ssid->wpa_psk_file);
64                         return -1;
65                 }
66
67                 wpa_snprintf_hex(hex, sizeof(hex), psk, psk_len);
68                 fprintf(f, MACSTR " %s\n", MAC2STR(mac_addr), hex);
69                 fclose(f);
70         }
71
72         return 0;
73 }
74
75
76 static int hostapd_wps_set_ie_cb(void *ctx, const u8 *beacon_ie,
77                                  size_t beacon_ie_len, const u8 *probe_resp_ie,
78                                  size_t probe_resp_ie_len)
79 {
80         struct hostapd_data *hapd = ctx;
81
82         os_free(hapd->wps_beacon_ie);
83         if (beacon_ie_len == 0) {
84                 hapd->wps_beacon_ie = NULL;
85                 hapd->wps_beacon_ie_len = 0;
86         } else {
87                 hapd->wps_beacon_ie = os_malloc(beacon_ie_len);
88                 if (hapd->wps_beacon_ie == NULL) {
89                         hapd->wps_beacon_ie_len = 0;
90                         return -1;
91                 }
92                 os_memcpy(hapd->wps_beacon_ie, beacon_ie, beacon_ie_len);
93                 hapd->wps_beacon_ie_len = beacon_ie_len;
94         }
95         hostapd_set_wps_beacon_ie(hapd, hapd->wps_beacon_ie,
96                                   hapd->wps_beacon_ie_len);
97
98         os_free(hapd->wps_probe_resp_ie);
99         if (probe_resp_ie_len == 0) {
100                 hapd->wps_probe_resp_ie = NULL;
101                 hapd->wps_probe_resp_ie_len = 0;
102         } else {
103                 hapd->wps_probe_resp_ie = os_malloc(probe_resp_ie_len);
104                 if (hapd->wps_probe_resp_ie == NULL) {
105                         hapd->wps_probe_resp_ie_len = 0;
106                         return -1;
107                 }
108                 os_memcpy(hapd->wps_probe_resp_ie, probe_resp_ie,
109                           probe_resp_ie_len);
110                 hapd->wps_probe_resp_ie_len = probe_resp_ie_len;
111         }
112         hostapd_set_wps_probe_resp_ie(hapd, hapd->wps_probe_resp_ie,
113                                       hapd->wps_probe_resp_ie_len);
114
115         return 0;
116 }
117
118
119 static void hostapd_wps_pin_needed_cb(void *ctx, const u8 *uuid_e,
120                                       const struct wps_device_data *dev)
121 {
122         struct hostapd_data *hapd = ctx;
123         char uuid[40], txt[400];
124         int len;
125         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
126                 return;
127         wpa_printf(MSG_DEBUG, "WPS: PIN needed for E-UUID %s", uuid);
128         len = os_snprintf(txt, sizeof(txt), WPS_EVENT_PIN_NEEDED
129                           "%s " MACSTR " [%s|%s|%s|%s|%s|%d-%08X-%d]",
130                           uuid, MAC2STR(dev->mac_addr), dev->device_name,
131                           dev->manufacturer, dev->model_name,
132                           dev->model_number, dev->serial_number,
133                           dev->categ, dev->oui, dev->sub_categ);
134         if (len > 0 && len < (int) sizeof(txt))
135                 wpa_msg(hapd, MSG_INFO, "%s", txt);
136
137         if (hapd->conf->wps_pin_requests) {
138                 FILE *f;
139                 struct os_time t;
140                 f = fopen(hapd->conf->wps_pin_requests, "a");
141                 if (f == NULL)
142                         return;
143                 os_get_time(&t);
144                 fprintf(f, "%ld\t%s\t" MACSTR "\t%s\t%s\t%s\t%s\t%s"
145                         "\t%d-%08X-%d\n",
146                         t.sec, uuid, MAC2STR(dev->mac_addr), dev->device_name,
147                         dev->manufacturer, dev->model_name, dev->model_number,
148                         dev->serial_number,
149                         dev->categ, dev->oui, dev->sub_categ);
150                 fclose(f);
151         }
152 }
153
154
155 static void hostapd_wps_reg_success_cb(void *ctx, const u8 *mac_addr,
156                                        const u8 *uuid_e)
157 {
158         struct hostapd_data *hapd = ctx;
159         char uuid[40];
160         if (uuid_bin2str(uuid_e, uuid, sizeof(uuid)))
161                 return;
162         wpa_msg(hapd, MSG_INFO, WPS_EVENT_REG_SUCCESS MACSTR " %s",
163                 MAC2STR(mac_addr), uuid);
164 }
165
166
167 static int str_starts(const char *str, const char *start)
168 {
169         return os_strncmp(str, start, os_strlen(start)) == 0;
170 }
171
172
173 static void wps_reload_config(void *eloop_data, void *user_ctx)
174 {
175         struct hostapd_iface *iface = eloop_data;
176
177         wpa_printf(MSG_DEBUG, "WPS: Reload configuration data");
178         if (hostapd_reload_config(iface) < 0) {
179                 wpa_printf(MSG_WARNING, "WPS: Failed to reload the updated "
180                            "configuration");
181         }
182 }
183
184
185 static int hostapd_wps_cred_cb(void *ctx, const struct wps_credential *cred)
186 {
187         struct hostapd_data *hapd = ctx;
188         FILE *oconf, *nconf;
189         size_t len, i;
190         char *tmp_fname;
191         char buf[1024];
192         int multi_bss;
193         int wpa;
194
195         wpa_hexdump_key(MSG_DEBUG, "WPS: Received Credential attribute",
196                         cred->cred_attr, cred->cred_attr_len);
197
198         wpa_printf(MSG_DEBUG, "WPS: Received new AP Settings");
199         wpa_hexdump_ascii(MSG_DEBUG, "WPS: SSID", cred->ssid, cred->ssid_len);
200         wpa_printf(MSG_DEBUG, "WPS: Authentication Type 0x%x",
201                    cred->auth_type);
202         wpa_printf(MSG_DEBUG, "WPS: Encryption Type 0x%x", cred->encr_type);
203         wpa_printf(MSG_DEBUG, "WPS: Network Key Index %d", cred->key_idx);
204         wpa_hexdump_key(MSG_DEBUG, "WPS: Network Key",
205                         cred->key, cred->key_len);
206         wpa_printf(MSG_DEBUG, "WPS: MAC Address " MACSTR,
207                    MAC2STR(cred->mac_addr));
208
209         if ((hapd->conf->wps_cred_processing == 1 ||
210              hapd->conf->wps_cred_processing == 2) && cred->cred_attr) {
211                 size_t blen = cred->cred_attr_len * 2 + 1;
212                 char *buf = os_malloc(blen);
213                 if (buf) {
214                         wpa_snprintf_hex(buf, blen,
215                                          cred->cred_attr, cred->cred_attr_len);
216                         wpa_msg(hapd, MSG_INFO, "%s%s",
217                                 WPS_EVENT_NEW_AP_SETTINGS, buf);
218                         os_free(buf);
219                 }
220         } else
221                 wpa_msg(hapd, MSG_INFO, WPS_EVENT_NEW_AP_SETTINGS);
222
223         if (hapd->conf->wps_cred_processing == 1)
224                 return 0;
225
226         len = os_strlen(hapd->iface->config_fname) + 5;
227         tmp_fname = os_malloc(len);
228         if (tmp_fname == NULL)
229                 return -1;
230         os_snprintf(tmp_fname, len, "%s-new", hapd->iface->config_fname);
231
232         oconf = fopen(hapd->iface->config_fname, "r");
233         if (oconf == NULL) {
234                 wpa_printf(MSG_WARNING, "WPS: Could not open current "
235                            "configuration file");
236                 os_free(tmp_fname);
237                 return -1;
238         }
239
240         nconf = fopen(tmp_fname, "w");
241         if (nconf == NULL) {
242                 wpa_printf(MSG_WARNING, "WPS: Could not write updated "
243                            "configuration file");
244                 os_free(tmp_fname);
245                 fclose(oconf);
246                 return -1;
247         }
248
249         fprintf(nconf, "# WPS configuration - START\n");
250
251         fprintf(nconf, "wps_state=2\n");
252
253         fprintf(nconf, "ssid=");
254         for (i = 0; i < cred->ssid_len; i++)
255                 fputc(cred->ssid[i], nconf);
256         fprintf(nconf, "\n");
257
258         if ((cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK)) &&
259             (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK)))
260                 wpa = 3;
261         else if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA2PSK))
262                 wpa = 2;
263         else if (cred->auth_type & (WPS_AUTH_WPA | WPS_AUTH_WPAPSK))
264                 wpa = 1;
265         else
266                 wpa = 0;
267
268         if (wpa) {
269                 char *prefix;
270                 fprintf(nconf, "wpa=%d\n", wpa);
271
272                 fprintf(nconf, "wpa_key_mgmt=");
273                 prefix = "";
274                 if (cred->auth_type & (WPS_AUTH_WPA2 | WPS_AUTH_WPA)) {
275                         fprintf(nconf, "WPA-EAP");
276                         prefix = " ";
277                 }
278                 if (cred->auth_type & (WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK))
279                         fprintf(nconf, "%sWPA-PSK", prefix);
280                 fprintf(nconf, "\n");
281
282                 fprintf(nconf, "wpa_pairwise=");
283                 prefix = "";
284                 if (cred->encr_type & WPS_ENCR_AES) {
285                         fprintf(nconf, "CCMP");
286                         prefix = " ";
287                 }
288                 if (cred->encr_type & WPS_ENCR_TKIP) {
289                         fprintf(nconf, "%sTKIP", prefix);
290                 }
291                 fprintf(nconf, "\n");
292
293                 if (cred->key_len >= 8 && cred->key_len < 64) {
294                         fprintf(nconf, "wpa_passphrase=");
295                         for (i = 0; i < cred->key_len; i++)
296                                 fputc(cred->key[i], nconf);
297                         fprintf(nconf, "\n");
298                 } else if (cred->key_len == 64) {
299                         fprintf(nconf, "wpa_psk=");
300                         for (i = 0; i < cred->key_len; i++)
301                                 fputc(cred->key[i], nconf);
302                         fprintf(nconf, "\n");
303                 } else {
304                         wpa_printf(MSG_WARNING, "WPS: Invalid key length %lu "
305                                    "for WPA/WPA2",
306                                    (unsigned long) cred->key_len);
307                 }
308
309                 fprintf(nconf, "auth_algs=1\n");
310         } else {
311                 if ((cred->auth_type & WPS_AUTH_OPEN) &&
312                     (cred->auth_type & WPS_AUTH_SHARED))
313                         fprintf(nconf, "auth_algs=3\n");
314                 else if (cred->auth_type & WPS_AUTH_SHARED)
315                         fprintf(nconf, "auth_algs=2\n");
316                 else
317                         fprintf(nconf, "auth_algs=1\n");
318
319                 if (cred->encr_type & WPS_ENCR_WEP && cred->key_idx < 4) {
320                         fprintf(nconf, "wep_default_key=%d\n", cred->key_idx);
321                         fprintf(nconf, "wep_key%d=", cred->key_idx);
322                         if (cred->key_len != 10 && cred->key_len != 26)
323                                 fputc('"', nconf);
324                         for (i = 0; i < cred->key_len; i++)
325                                 fputc(cred->key[i], nconf);
326                         if (cred->key_len != 10 && cred->key_len != 26)
327                                 fputc('"', nconf);
328                         fprintf(nconf, "\n");
329                 }
330         }
331
332         fprintf(nconf, "# WPS configuration - END\n");
333
334         multi_bss = 0;
335         while (fgets(buf, sizeof(buf), oconf)) {
336                 if (os_strncmp(buf, "bss=", 4) == 0)
337                         multi_bss = 1;
338                 if (!multi_bss &&
339                     (str_starts(buf, "ssid=") ||
340                      str_starts(buf, "auth_algs=") ||
341                      str_starts(buf, "wps_state=") ||
342                      str_starts(buf, "wpa=") ||
343                      str_starts(buf, "wpa_psk=") ||
344                      str_starts(buf, "wpa_pairwise=") ||
345                      str_starts(buf, "rsn_pairwise=") ||
346                      str_starts(buf, "wpa_key_mgmt=") ||
347                      str_starts(buf, "wpa_passphrase="))) {
348                         fprintf(nconf, "#WPS# %s", buf);
349                 } else
350                         fprintf(nconf, "%s", buf);
351         }
352
353         fclose(nconf);
354         fclose(oconf);
355
356         if (rename(tmp_fname, hapd->iface->config_fname) < 0) {
357                 wpa_printf(MSG_WARNING, "WPS: Failed to rename the updated "
358                            "configuration file: %s", strerror(errno));
359                 os_free(tmp_fname);
360                 return -1;
361         }
362
363         os_free(tmp_fname);
364
365         /* Schedule configuration reload after short period of time to allow
366          * EAP-WSC to be finished.
367          */
368         eloop_register_timeout(0, 100000, wps_reload_config, hapd->iface,
369                                NULL);
370
371         /* TODO: dualband AP may need to update multiple configuration files */
372
373         wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
374
375         return 0;
376 }
377
378
379 static void hostapd_pwd_auth_fail(struct hostapd_data *hapd,
380                                   struct wps_event_pwd_auth_fail *data)
381 {
382         FILE *f;
383
384         if (!data->enrollee)
385                 return;
386
387         /*
388          * Registrar failed to prove its knowledge of the AP PIN. Lock AP setup
389          * if this happens multiple times.
390          */
391         hapd->ap_pin_failures++;
392         if (hapd->ap_pin_failures < 4)
393                 return;
394
395         wpa_msg(hapd, MSG_INFO, WPS_EVENT_AP_SETUP_LOCKED);
396         hapd->wps->ap_setup_locked = 1;
397
398         wps_registrar_update_ie(hapd->wps->registrar);
399
400         if (hapd->conf->wps_cred_processing == 1)
401                 return;
402
403         f = fopen(hapd->iface->config_fname, "a");
404         if (f == NULL) {
405                 wpa_printf(MSG_WARNING, "WPS: Could not append to the current "
406                            "configuration file");
407                 return;
408         }
409
410         fprintf(f, "# WPS AP Setup Locked based on possible attack\n");
411         fprintf(f, "ap_setup_locked=1\n");
412         fclose(f);
413
414         /* TODO: dualband AP may need to update multiple configuration files */
415
416         wpa_printf(MSG_DEBUG, "WPS: AP configuration updated");
417 }
418
419
420 static void hostapd_wps_event_cb(void *ctx, enum wps_event event,
421                                  union wps_event_data *data)
422 {
423         struct hostapd_data *hapd = ctx;
424
425         if (event == WPS_EV_PWD_AUTH_FAIL)
426                 hostapd_pwd_auth_fail(hapd, &data->pwd_auth_fail);
427 }
428
429
430 static void hostapd_wps_clear_ies(struct hostapd_data *hapd)
431 {
432         os_free(hapd->wps_beacon_ie);
433         hapd->wps_beacon_ie = NULL;
434         hapd->wps_beacon_ie_len = 0;
435         hostapd_set_wps_beacon_ie(hapd, NULL, 0);
436
437         os_free(hapd->wps_probe_resp_ie);
438         hapd->wps_probe_resp_ie = NULL;
439         hapd->wps_probe_resp_ie_len = 0;
440         hostapd_set_wps_probe_resp_ie(hapd, NULL, 0);
441 }
442
443
444 int hostapd_init_wps(struct hostapd_data *hapd,
445                      struct hostapd_bss_config *conf)
446 {
447         struct wps_context *wps;
448         struct wps_registrar_config cfg;
449
450         if (conf->wps_state == 0) {
451                 hostapd_wps_clear_ies(hapd);
452                 return 0;
453         }
454
455         wps = os_zalloc(sizeof(*wps));
456         if (wps == NULL)
457                 return -1;
458
459         wps->cred_cb = hostapd_wps_cred_cb;
460         wps->event_cb = hostapd_wps_event_cb;
461         wps->cb_ctx = hapd;
462
463         os_memset(&cfg, 0, sizeof(cfg));
464         wps->wps_state = hapd->conf->wps_state;
465         wps->ap_setup_locked = hapd->conf->ap_setup_locked;
466         if (is_nil_uuid(hapd->conf->uuid)) {
467                 uuid_gen_mac_addr(hapd->own_addr, wps->uuid);
468                 wpa_hexdump(MSG_DEBUG, "WPS: UUID based on MAC address",
469                             wps->uuid, UUID_LEN);
470         } else
471                 os_memcpy(wps->uuid, hapd->conf->uuid, UUID_LEN);
472         wps->ssid_len = hapd->conf->ssid.ssid_len;
473         os_memcpy(wps->ssid, hapd->conf->ssid.ssid, wps->ssid_len);
474         wps->ap = 1;
475         os_memcpy(wps->dev.mac_addr, hapd->own_addr, ETH_ALEN);
476         wps->dev.device_name = hapd->conf->device_name ?
477                 os_strdup(hapd->conf->device_name) : NULL;
478         wps->dev.manufacturer = hapd->conf->manufacturer ?
479                 os_strdup(hapd->conf->manufacturer) : NULL;
480         wps->dev.model_name = hapd->conf->model_name ?
481                 os_strdup(hapd->conf->model_name) : NULL;
482         wps->dev.model_number = hapd->conf->model_number ?
483                 os_strdup(hapd->conf->model_number) : NULL;
484         wps->dev.serial_number = hapd->conf->serial_number ?
485                 os_strdup(hapd->conf->serial_number) : NULL;
486         if (hapd->conf->config_methods) {
487                 char *m = hapd->conf->config_methods;
488                 if (os_strstr(m, "label"))
489                         wps->config_methods |= WPS_CONFIG_LABEL;
490                 if (os_strstr(m, "display"))
491                         wps->config_methods |= WPS_CONFIG_DISPLAY;
492                 if (os_strstr(m, "push_button"))
493                         wps->config_methods |= WPS_CONFIG_PUSHBUTTON;
494                 if (os_strstr(m, "keypad"))
495                         wps->config_methods |= WPS_CONFIG_KEYPAD;
496         }
497         if (hapd->conf->device_type) {
498                 char *pos;
499                 u8 oui[4];
500                 /* <categ>-<OUI>-<subcateg> */
501                 wps->dev.categ = atoi(hapd->conf->device_type);
502                 pos = os_strchr(hapd->conf->device_type, '-');
503                 if (pos == NULL) {
504                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
505                         os_free(wps);
506                         return -1;
507                 }
508                 pos++;
509                 if (hexstr2bin(pos, oui, 4)) {
510                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type OUI");
511                         os_free(wps);
512                         return -1;
513                 }
514                 wps->dev.oui = WPA_GET_BE32(oui);
515                 pos = os_strchr(pos, '-');
516                 if (pos == NULL) {
517                         wpa_printf(MSG_ERROR, "WPS: Invalid device_type");
518                         os_free(wps);
519                         return -1;
520                 }
521                 pos++;
522                 wps->dev.sub_categ = atoi(pos);
523         }
524         wps->dev.os_version = WPA_GET_BE32(hapd->conf->os_version);
525         wps->dev.rf_bands = hapd->iconf->hw_mode == HOSTAPD_MODE_IEEE80211A ?
526                 WPS_RF_50GHZ : WPS_RF_24GHZ; /* FIX: dualband AP */
527
528         if (conf->wpa & WPA_PROTO_RSN) {
529                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
530                         wps->auth_types |= WPS_AUTH_WPA2PSK;
531                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
532                         wps->auth_types |= WPS_AUTH_WPA2;
533
534                 if (conf->rsn_pairwise & WPA_CIPHER_CCMP)
535                         wps->encr_types |= WPS_ENCR_AES;
536                 if (conf->rsn_pairwise & WPA_CIPHER_TKIP)
537                         wps->encr_types |= WPS_ENCR_TKIP;
538         }
539
540         if (conf->wpa & WPA_PROTO_WPA) {
541                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_PSK)
542                         wps->auth_types |= WPS_AUTH_WPAPSK;
543                 if (conf->wpa_key_mgmt & WPA_KEY_MGMT_IEEE8021X)
544                         wps->auth_types |= WPS_AUTH_WPA;
545
546                 if (conf->wpa_pairwise & WPA_CIPHER_CCMP)
547                         wps->encr_types |= WPS_ENCR_AES;
548                 if (conf->wpa_pairwise & WPA_CIPHER_TKIP)
549                         wps->encr_types |= WPS_ENCR_TKIP;
550         }
551
552         if (conf->ssid.security_policy == SECURITY_PLAINTEXT) {
553                 wps->encr_types |= WPS_ENCR_NONE;
554                 wps->auth_types |= WPS_AUTH_OPEN;
555         } else if (conf->ssid.security_policy == SECURITY_STATIC_WEP) {
556                 wps->encr_types |= WPS_ENCR_WEP;
557                 if (conf->auth_algs & WPA_AUTH_ALG_OPEN)
558                         wps->auth_types |= WPS_AUTH_OPEN;
559                 if (conf->auth_algs & WPA_AUTH_ALG_SHARED)
560                         wps->auth_types |= WPS_AUTH_SHARED;
561         } else if (conf->ssid.security_policy == SECURITY_IEEE_802_1X) {
562                 wps->auth_types |= WPS_AUTH_OPEN;
563                 if (conf->default_wep_key_len)
564                         wps->encr_types |= WPS_ENCR_WEP;
565                 else
566                         wps->encr_types |= WPS_ENCR_NONE;
567         }
568
569         if (conf->ssid.wpa_psk_file) {
570                 /* Use per-device PSKs */
571         } else if (conf->ssid.wpa_passphrase) {
572                 wps->network_key = (u8 *) os_strdup(conf->ssid.wpa_passphrase);
573                 wps->network_key_len = os_strlen(conf->ssid.wpa_passphrase);
574         } else if (conf->ssid.wpa_psk) {
575                 wps->network_key = os_malloc(2 * PMK_LEN + 1);
576                 if (wps->network_key == NULL) {
577                         os_free(wps);
578                         return -1;
579                 }
580                 wpa_snprintf_hex((char *) wps->network_key, 2 * PMK_LEN + 1,
581                                  conf->ssid.wpa_psk->psk, PMK_LEN);
582                 wps->network_key_len = 2 * PMK_LEN;
583         } else if (conf->ssid.wep.keys_set && conf->ssid.wep.key[0]) {
584                 wps->network_key = os_malloc(conf->ssid.wep.len[0]);
585                 if (wps->network_key == NULL) {
586                         os_free(wps);
587                         return -1;
588                 }
589                 os_memcpy(wps->network_key, conf->ssid.wep.key[0],
590                           conf->ssid.wep.len[0]);
591                 wps->network_key_len = conf->ssid.wep.len[0];
592         }
593
594         if (conf->wps_state == WPS_STATE_NOT_CONFIGURED) {
595                 /* Override parameters to enable security by default */
596                 wps->auth_types = WPS_AUTH_WPA2PSK | WPS_AUTH_WPAPSK;
597                 wps->encr_types = WPS_ENCR_AES | WPS_ENCR_TKIP;
598         }
599
600         wps->ap_settings = conf->ap_settings;
601         wps->ap_settings_len = conf->ap_settings_len;
602
603         cfg.new_psk_cb = hostapd_wps_new_psk_cb;
604         cfg.set_ie_cb = hostapd_wps_set_ie_cb;
605         cfg.pin_needed_cb = hostapd_wps_pin_needed_cb;
606         cfg.reg_success_cb = hostapd_wps_reg_success_cb;
607         cfg.cb_ctx = hapd;
608         cfg.skip_cred_build = conf->skip_cred_build;
609         cfg.extra_cred = conf->extra_cred;
610         cfg.extra_cred_len = conf->extra_cred_len;
611         cfg.disable_auto_conf = (hapd->conf->wps_cred_processing == 1) &&
612                 conf->skip_cred_build;
613
614         wps->registrar = wps_registrar_init(wps, &cfg);
615         if (wps->registrar == NULL) {
616                 printf("Failed to initialize WPS Registrar\n");
617                 os_free(wps->network_key);
618                 os_free(wps);
619                 return -1;
620         }
621
622         hapd->wps = wps;
623
624         return 0;
625 }
626
627
628 void hostapd_deinit_wps(struct hostapd_data *hapd)
629 {
630         if (hapd->wps == NULL)
631                 return;
632         wps_registrar_deinit(hapd->wps->registrar);
633         os_free(hapd->wps->network_key);
634         wps_device_data_free(&hapd->wps->dev);
635         os_free(hapd->wps);
636         hapd->wps = NULL;
637         hostapd_wps_clear_ies(hapd);
638 }
639
640
641 int hostapd_wps_add_pin(struct hostapd_data *hapd, const char *uuid,
642                         const char *pin)
643 {
644         u8 u[UUID_LEN];
645         int any = 0;
646
647         if (hapd->wps == NULL)
648                 return -1;
649         if (os_strcmp(uuid, "any") == 0)
650                 any = 1;
651         else if (uuid_str2bin(uuid, u))
652                 return -1;
653         return wps_registrar_add_pin(hapd->wps->registrar, any ? NULL : u,
654                                      (const u8 *) pin, os_strlen(pin));
655 }
656
657
658 int hostapd_wps_button_pushed(struct hostapd_data *hapd)
659 {
660         if (hapd->wps == NULL)
661                 return -1;
662         return wps_registrar_button_pushed(hapd->wps->registrar);
663 }
664
665
666 void hostapd_wps_probe_req_rx(struct hostapd_data *hapd, const u8 *addr,
667                               const u8 *ie, size_t ie_len)
668 {
669         struct wpabuf *wps_ie;
670         const u8 *end, *pos, *wps;
671
672         if (hapd->wps == NULL)
673                 return;
674
675         pos = ie;
676         end = ie + ie_len;
677         wps = NULL;
678
679         while (pos + 1 < end) {
680                 if (pos + 2 + pos[1] > end)
681                         return;
682                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
683                     WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA) {
684                         wps = pos;
685                         break;
686                 }
687                 pos += 2 + pos[1];
688         }
689
690         if (wps == NULL)
691                 return; /* No WPS IE in Probe Request */
692
693         wps_ie = wpabuf_alloc(ie_len);
694         if (wps_ie == NULL)
695                 return;
696
697         /* There may be multiple WPS IEs in the message, so need to concatenate
698          * their WPS Data fields */
699         while (pos + 1 < end) {
700                 if (pos + 2 + pos[1] > end)
701                         break;
702                 if (pos[0] == WLAN_EID_VENDOR_SPECIFIC && pos[1] >= 4 &&
703                     WPA_GET_BE32(&pos[2]) == WPS_DEV_OUI_WFA)
704                         wpabuf_put_data(wps_ie, pos + 6, pos[1] - 4);
705                 pos += 2 + pos[1];
706         }
707
708         if (wpabuf_len(wps_ie) > 0)
709                 wps_registrar_probe_req_rx(hapd->wps->registrar, addr, wps_ie);
710
711         wpabuf_free(wps_ie);
712 }