Home | History | Annotate | Download | only in wpa_supplicant
      1 /*
      2  * WPA Supplicant / Configuration backend: Windows registry
      3  * Copyright (c) 2003-2008, Jouni Malinen <j (at) 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  * This file implements a configuration backend for Windows registry. All the
     15  * configuration information is stored in the registry and the format for
     16  * network configuration fields is same as described in the sample
     17  * configuration file, wpa_supplicant.conf.
     18  *
     19  * Configuration data is in
     20  * \a HKEY_LOCAL_MACHINE\\SOFTWARE\\%wpa_supplicant\\configs
     21  * key. Each configuration profile has its own key under this. In terms of text
     22  * files, each profile would map to a separate text file with possibly multiple
     23  * networks. Under each profile, there is a networks key that lists all
     24  * networks as a subkey. Each network has set of values in the same way as
     25  * network block in the configuration file. In addition, blobs subkey has
     26  * possible blobs as values.
     27  *
     28  * Example network configuration block:
     29  * \verbatim
     30 HKEY_LOCAL_MACHINE\SOFTWARE\wpa_supplicant\configs\test\networks\0000
     31    ssid="example"
     32    key_mgmt=WPA-PSK
     33 \endverbatim
     34  */
     35 
     36 #include "includes.h"
     37 
     38 #include "common.h"
     39 #include "uuid.h"
     40 #include "config.h"
     41 
     42 #ifndef WPA_KEY_ROOT
     43 #define WPA_KEY_ROOT HKEY_LOCAL_MACHINE
     44 #endif
     45 #ifndef WPA_KEY_PREFIX
     46 #define WPA_KEY_PREFIX TEXT("SOFTWARE\\wpa_supplicant")
     47 #endif
     48 
     49 #ifdef UNICODE
     50 #define TSTR "%S"
     51 #else /* UNICODE */
     52 #define TSTR "%s"
     53 #endif /* UNICODE */
     54 
     55 
     56 static int wpa_config_read_blobs(struct wpa_config *config, HKEY hk)
     57 {
     58 	struct wpa_config_blob *blob;
     59 	int errors = 0;
     60 	HKEY bhk;
     61 	LONG ret;
     62 	DWORD i;
     63 
     64 	ret = RegOpenKeyEx(hk, TEXT("blobs"), 0, KEY_QUERY_VALUE, &bhk);
     65 	if (ret != ERROR_SUCCESS) {
     66 		wpa_printf(MSG_DEBUG, "Could not open wpa_supplicant config "
     67 			   "blobs key");
     68 		return 0; /* assume no blobs */
     69 	}
     70 
     71 	for (i = 0; ; i++) {
     72 #define TNAMELEN 255
     73 		TCHAR name[TNAMELEN];
     74 		char data[4096];
     75 		DWORD namelen, datalen, type;
     76 
     77 		namelen = TNAMELEN;
     78 		datalen = sizeof(data);
     79 		ret = RegEnumValue(bhk, i, name, &namelen, NULL, &type,
     80 				   (LPBYTE) data, &datalen);
     81 
     82 		if (ret == ERROR_NO_MORE_ITEMS)
     83 			break;
     84 
     85 		if (ret != ERROR_SUCCESS) {
     86 			wpa_printf(MSG_DEBUG, "RegEnumValue failed: 0x%x",
     87 				   (unsigned int) ret);
     88 			break;
     89 		}
     90 
     91 		if (namelen >= TNAMELEN)
     92 			namelen = TNAMELEN - 1;
     93 		name[namelen] = TEXT('\0');
     94 		wpa_unicode2ascii_inplace(name);
     95 
     96 		if (datalen >= sizeof(data))
     97 			datalen = sizeof(data) - 1;
     98 
     99 		wpa_printf(MSG_MSGDUMP, "blob %d: field='%s' len %d",
    100 			   (int) i, name, (int) datalen);
    101 
    102 		blob = os_zalloc(sizeof(*blob));
    103 		if (blob == NULL) {
    104 			errors++;
    105 			break;
    106 		}
    107 		blob->name = os_strdup((char *) name);
    108 		blob->data = os_malloc(datalen);
    109 		if (blob->name == NULL || blob->data == NULL) {
    110 			wpa_config_free_blob(blob);
    111 			errors++;
    112 			break;
    113 		}
    114 		os_memcpy(blob->data, data, datalen);
    115 		blob->len = datalen;
    116 
    117 		wpa_config_set_blob(config, blob);
    118 	}
    119 
    120 	RegCloseKey(bhk);
    121 
    122 	return errors ? -1 : 0;
    123 }
    124 
    125 
    126 static int wpa_config_read_reg_dword(HKEY hk, const TCHAR *name, int *_val)
    127 {
    128 	DWORD val, buflen;
    129 	LONG ret;
    130 
    131 	buflen = sizeof(val);
    132 	ret = RegQueryValueEx(hk, name, NULL, NULL, (LPBYTE) &val, &buflen);
    133 	if (ret == ERROR_SUCCESS && buflen == sizeof(val)) {
    134 		wpa_printf(MSG_DEBUG, TSTR "=%d", name, (int) val);
    135 		*_val = val;
    136 		return 0;
    137 	}
    138 
    139 	return -1;
    140 }
    141 
    142 
    143 static char * wpa_config_read_reg_string(HKEY hk, const TCHAR *name)
    144 {
    145 	DWORD buflen;
    146 	LONG ret;
    147 	TCHAR *val;
    148 
    149 	buflen = 0;
    150 	ret = RegQueryValueEx(hk, name, NULL, NULL, NULL, &buflen);
    151 	if (ret != ERROR_SUCCESS)
    152 		return NULL;
    153 	val = os_malloc(buflen);
    154 	if (val == NULL)
    155 		return NULL;
    156 
    157 	ret = RegQueryValueEx(hk, name, NULL, NULL, (LPBYTE) val, &buflen);
    158 	if (ret != ERROR_SUCCESS) {
    159 		os_free(val);
    160 		return NULL;
    161 	}
    162 
    163 	wpa_unicode2ascii_inplace(val);
    164 	wpa_printf(MSG_DEBUG, TSTR "=%s", name, (char *) val);
    165 	return (char *) val;
    166 }
    167 
    168 
    169 #ifdef CONFIG_WPS
    170 static int wpa_config_read_global_uuid(struct wpa_config *config, HKEY hk)
    171 {
    172 	char *str;
    173 	int ret = 0;
    174 
    175 	str = wpa_config_read_reg_string(hk, TEXT("uuid"));
    176 	if (str == NULL)
    177 		return 0;
    178 
    179 	if (uuid_str2bin(str, config->uuid))
    180 		ret = -1;
    181 
    182 	os_free(str);
    183 
    184 	return ret;
    185 }
    186 
    187 
    188 static int wpa_config_read_global_os_version(struct wpa_config *config,
    189 					     HKEY hk)
    190 {
    191 	char *str;
    192 	int ret = 0;
    193 
    194 	str = wpa_config_read_reg_string(hk, TEXT("os_version"));
    195 	if (str == NULL)
    196 		return 0;
    197 
    198 	if (hexstr2bin(str, config->os_version, 4))
    199 		ret = -1;
    200 
    201 	os_free(str);
    202 
    203 	return ret;
    204 }
    205 #endif /* CONFIG_WPS */
    206 
    207 
    208 static int wpa_config_read_global(struct wpa_config *config, HKEY hk)
    209 {
    210 	int errors = 0;
    211 
    212 	wpa_config_read_reg_dword(hk, TEXT("ap_scan"), &config->ap_scan);
    213 	wpa_config_read_reg_dword(hk, TEXT("fast_reauth"),
    214 				  &config->fast_reauth);
    215 	wpa_config_read_reg_dword(hk, TEXT("dot11RSNAConfigPMKLifetime"),
    216 				  (int *) &config->dot11RSNAConfigPMKLifetime);
    217 	wpa_config_read_reg_dword(hk,
    218 				  TEXT("dot11RSNAConfigPMKReauthThreshold"),
    219 				  (int *)
    220 				  &config->dot11RSNAConfigPMKReauthThreshold);
    221 	wpa_config_read_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"),
    222 				  (int *) &config->dot11RSNAConfigSATimeout);
    223 	wpa_config_read_reg_dword(hk, TEXT("update_config"),
    224 				  &config->update_config);
    225 
    226 	if (wpa_config_read_reg_dword(hk, TEXT("eapol_version"),
    227 				      &config->eapol_version) == 0) {
    228 		if (config->eapol_version < 1 ||
    229 		    config->eapol_version > 2) {
    230 			wpa_printf(MSG_ERROR, "Invalid EAPOL version (%d)",
    231 				   config->eapol_version);
    232 			errors++;
    233 		}
    234 	}
    235 
    236 	config->ctrl_interface = wpa_config_read_reg_string(
    237 		hk, TEXT("ctrl_interface"));
    238 
    239 #ifdef CONFIG_WPS
    240 	if (wpa_config_read_global_uuid(config, hk))
    241 		errors++;
    242 	config->device_name = wpa_config_read_reg_string(
    243 		hk, TEXT("device_name"));
    244 	config->manufacturer = wpa_config_read_reg_string(
    245 		hk, TEXT("manufacturer"));
    246 	config->model_name = wpa_config_read_reg_string(
    247 		hk, TEXT("model_name"));
    248 	config->serial_number = wpa_config_read_reg_string(
    249 		hk, TEXT("serial_number"));
    250 	{
    251 		char *t = wpa_config_read_reg_string(
    252 			hk, TEXT("device_type"));
    253 		if (t && wps_dev_type_str2bin(t, config->device_type))
    254 			errors++;
    255 		os_free(t);
    256 	}
    257 	config->config_methods = wpa_config_read_reg_string(
    258 		hk, TEXT("config_methods"));
    259 	if (wpa_config_read_global_os_version(config, hk))
    260 		errors++;
    261 	wpa_config_read_reg_dword(hk, TEXT("wps_cred_processing"),
    262 				  &config->wps_cred_processing);
    263 #endif /* CONFIG_WPS */
    264 #ifdef CONFIG_P2P
    265 	config->p2p_ssid_postfix = wpa_config_read_reg_string(
    266 		hk, TEXT("p2p_ssid_postfix"));
    267 	wpa_config_read_reg_dword(hk, TEXT("p2p_group_idle"),
    268 				  (int *) &config->p2p_group_idle);
    269 #endif /* CONFIG_P2P */
    270 
    271 	wpa_config_read_reg_dword(hk, TEXT("bss_max_count"),
    272 				  (int *) &config->bss_max_count);
    273 	wpa_config_read_reg_dword(hk, TEXT("filter_ssids"),
    274 				  &config->filter_ssids);
    275 	wpa_config_read_reg_dword(hk, TEXT("max_num_sta"),
    276 				  (int *) &config->max_num_sta);
    277 	wpa_config_read_reg_dword(hk, TEXT("disassoc_low_ack"),
    278 				  (int *) &config->disassoc_low_ack);
    279 
    280 	return errors ? -1 : 0;
    281 }
    282 
    283 
    284 static struct wpa_ssid * wpa_config_read_network(HKEY hk, const TCHAR *netw,
    285 						 int id)
    286 {
    287 	HKEY nhk;
    288 	LONG ret;
    289 	DWORD i;
    290 	struct wpa_ssid *ssid;
    291 	int errors = 0;
    292 
    293 	ret = RegOpenKeyEx(hk, netw, 0, KEY_QUERY_VALUE, &nhk);
    294 	if (ret != ERROR_SUCCESS) {
    295 		wpa_printf(MSG_DEBUG, "Could not open wpa_supplicant config "
    296 			   "network '" TSTR "'", netw);
    297 		return NULL;
    298 	}
    299 
    300 	wpa_printf(MSG_MSGDUMP, "Start of a new network '" TSTR "'", netw);
    301 	ssid = os_zalloc(sizeof(*ssid));
    302 	if (ssid == NULL) {
    303 		RegCloseKey(nhk);
    304 		return NULL;
    305 	}
    306 	ssid->id = id;
    307 
    308 	wpa_config_set_network_defaults(ssid);
    309 
    310 	for (i = 0; ; i++) {
    311 		TCHAR name[255], data[1024];
    312 		DWORD namelen, datalen, type;
    313 
    314 		namelen = 255;
    315 		datalen = sizeof(data);
    316 		ret = RegEnumValue(nhk, i, name, &namelen, NULL, &type,
    317 				   (LPBYTE) data, &datalen);
    318 
    319 		if (ret == ERROR_NO_MORE_ITEMS)
    320 			break;
    321 
    322 		if (ret != ERROR_SUCCESS) {
    323 			wpa_printf(MSG_ERROR, "RegEnumValue failed: 0x%x",
    324 				   (unsigned int) ret);
    325 			break;
    326 		}
    327 
    328 		if (namelen >= 255)
    329 			namelen = 255 - 1;
    330 		name[namelen] = TEXT('\0');
    331 
    332 		if (datalen >= 1024)
    333 			datalen = 1024 - 1;
    334 		data[datalen] = TEXT('\0');
    335 
    336 		wpa_unicode2ascii_inplace(name);
    337 		wpa_unicode2ascii_inplace(data);
    338 		if (wpa_config_set(ssid, (char *) name, (char *) data, 0) < 0)
    339 			errors++;
    340 	}
    341 
    342 	RegCloseKey(nhk);
    343 
    344 	if (ssid->passphrase) {
    345 		if (ssid->psk_set) {
    346 			wpa_printf(MSG_ERROR, "Both PSK and passphrase "
    347 				   "configured for network '" TSTR "'.", netw);
    348 			errors++;
    349 		}
    350 		wpa_config_update_psk(ssid);
    351 	}
    352 
    353 	if ((ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK |
    354 			       WPA_KEY_MGMT_PSK_SHA256)) &&
    355 	    !ssid->psk_set) {
    356 		wpa_printf(MSG_ERROR, "WPA-PSK accepted for key management, "
    357 			   "but no PSK configured for network '" TSTR "'.",
    358 			   netw);
    359 		errors++;
    360 	}
    361 
    362 	if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
    363 	    !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
    364 	    !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
    365 		/* Group cipher cannot be stronger than the pairwise cipher. */
    366 		wpa_printf(MSG_DEBUG, "Removed CCMP from group cipher "
    367 			   "list since it was not allowed for pairwise "
    368 			   "cipher for network '" TSTR "'.", netw);
    369 		ssid->group_cipher &= ~WPA_CIPHER_CCMP;
    370 	}
    371 
    372 	if (errors) {
    373 		wpa_config_free_ssid(ssid);
    374 		ssid = NULL;
    375 	}
    376 
    377 	return ssid;
    378 }
    379 
    380 
    381 static int wpa_config_read_networks(struct wpa_config *config, HKEY hk)
    382 {
    383 	HKEY nhk;
    384 	struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
    385 	int errors = 0;
    386 	LONG ret;
    387 	DWORD i;
    388 
    389 	ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_ENUMERATE_SUB_KEYS,
    390 			   &nhk);
    391 	if (ret != ERROR_SUCCESS) {
    392 		wpa_printf(MSG_ERROR, "Could not open wpa_supplicant networks "
    393 			   "registry key");
    394 		return -1;
    395 	}
    396 
    397 	for (i = 0; ; i++) {
    398 		TCHAR name[255];
    399 		DWORD namelen;
    400 
    401 		namelen = 255;
    402 		ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL,
    403 				   NULL);
    404 
    405 		if (ret == ERROR_NO_MORE_ITEMS)
    406 			break;
    407 
    408 		if (ret != ERROR_SUCCESS) {
    409 			wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x",
    410 				   (unsigned int) ret);
    411 			break;
    412 		}
    413 
    414 		if (namelen >= 255)
    415 			namelen = 255 - 1;
    416 		name[namelen] = '\0';
    417 
    418 		ssid = wpa_config_read_network(nhk, name, i);
    419 		if (ssid == NULL) {
    420 			wpa_printf(MSG_ERROR, "Failed to parse network "
    421 				   "profile '%s'.", name);
    422 			errors++;
    423 			continue;
    424 		}
    425 		if (head == NULL) {
    426 			head = tail = ssid;
    427 		} else {
    428 			tail->next = ssid;
    429 			tail = ssid;
    430 		}
    431 		if (wpa_config_add_prio_network(config, ssid)) {
    432 			wpa_printf(MSG_ERROR, "Failed to add network profile "
    433 				   "'%s' to priority list.", name);
    434 			errors++;
    435 			continue;
    436 		}
    437 	}
    438 
    439 	RegCloseKey(nhk);
    440 
    441 	config->ssid = head;
    442 
    443 	return errors ? -1 : 0;
    444 }
    445 
    446 
    447 struct wpa_config * wpa_config_read(const char *name)
    448 {
    449 	TCHAR buf[256];
    450 	int errors = 0;
    451 	struct wpa_config *config;
    452 	HKEY hk;
    453 	LONG ret;
    454 
    455 	config = wpa_config_alloc_empty(NULL, NULL);
    456 	if (config == NULL)
    457 		return NULL;
    458 	wpa_printf(MSG_DEBUG, "Reading configuration profile '%s'", name);
    459 
    460 #ifdef UNICODE
    461 	_snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);
    462 #else /* UNICODE */
    463 	os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);
    464 #endif /* UNICODE */
    465 
    466 	ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_QUERY_VALUE, &hk);
    467 	if (ret != ERROR_SUCCESS) {
    468 		wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
    469 			   "configuration registry HKLM\\" TSTR, buf);
    470 		os_free(config);
    471 		return NULL;
    472 	}
    473 
    474 	if (wpa_config_read_global(config, hk))
    475 		errors++;
    476 
    477 	if (wpa_config_read_networks(config, hk))
    478 		errors++;
    479 
    480 	if (wpa_config_read_blobs(config, hk))
    481 		errors++;
    482 
    483 	wpa_config_debug_dump_networks(config);
    484 
    485 	RegCloseKey(hk);
    486 
    487 	if (errors) {
    488 		wpa_config_free(config);
    489 		config = NULL;
    490 	}
    491 
    492 	return config;
    493 }
    494 
    495 
    496 static int wpa_config_write_reg_dword(HKEY hk, const TCHAR *name, int val,
    497 				      int def)
    498 {
    499 	LONG ret;
    500 	DWORD _val = val;
    501 
    502 	if (val == def) {
    503 		RegDeleteValue(hk, name);
    504 		return 0;
    505 	}
    506 
    507 	ret = RegSetValueEx(hk, name, 0, REG_DWORD, (LPBYTE) &_val,
    508 			    sizeof(_val));
    509 	if (ret != ERROR_SUCCESS) {
    510 		wpa_printf(MSG_ERROR, "WINREG: Failed to set %s=%d: error %d",
    511 			   name, val, (int) GetLastError());
    512 		return -1;
    513 	}
    514 
    515 	return 0;
    516 }
    517 
    518 
    519 static int wpa_config_write_reg_string(HKEY hk, const char *name,
    520 				       const char *val)
    521 {
    522 	LONG ret;
    523 	TCHAR *_name, *_val;
    524 
    525 	_name = wpa_strdup_tchar(name);
    526 	if (_name == NULL)
    527 		return -1;
    528 
    529 	if (val == NULL) {
    530 		RegDeleteValue(hk, _name);
    531 		os_free(_name);
    532 		return 0;
    533 	}
    534 
    535 	_val = wpa_strdup_tchar(val);
    536 	if (_val == NULL) {
    537 		os_free(_name);
    538 		return -1;
    539 	}
    540 	ret = RegSetValueEx(hk, _name, 0, REG_SZ, (BYTE *) _val,
    541 			    (os_strlen(val) + 1) * sizeof(TCHAR));
    542 	if (ret != ERROR_SUCCESS) {
    543 		wpa_printf(MSG_ERROR, "WINREG: Failed to set %s='%s': "
    544 			   "error %d", name, val, (int) GetLastError());
    545 		os_free(_name);
    546 		os_free(_val);
    547 		return -1;
    548 	}
    549 
    550 	os_free(_name);
    551 	os_free(_val);
    552 	return 0;
    553 }
    554 
    555 
    556 static int wpa_config_write_global(struct wpa_config *config, HKEY hk)
    557 {
    558 #ifdef CONFIG_CTRL_IFACE
    559 	wpa_config_write_reg_string(hk, "ctrl_interface",
    560 				    config->ctrl_interface);
    561 #endif /* CONFIG_CTRL_IFACE */
    562 
    563 	wpa_config_write_reg_dword(hk, TEXT("eapol_version"),
    564 				   config->eapol_version,
    565 				   DEFAULT_EAPOL_VERSION);
    566 	wpa_config_write_reg_dword(hk, TEXT("ap_scan"), config->ap_scan,
    567 				   DEFAULT_AP_SCAN);
    568 	wpa_config_write_reg_dword(hk, TEXT("fast_reauth"),
    569 				   config->fast_reauth, DEFAULT_FAST_REAUTH);
    570 	wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigPMKLifetime"),
    571 				   config->dot11RSNAConfigPMKLifetime, 0);
    572 	wpa_config_write_reg_dword(hk,
    573 				   TEXT("dot11RSNAConfigPMKReauthThreshold"),
    574 				   config->dot11RSNAConfigPMKReauthThreshold,
    575 				   0);
    576 	wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"),
    577 				   config->dot11RSNAConfigSATimeout, 0);
    578 	wpa_config_write_reg_dword(hk, TEXT("update_config"),
    579 				   config->update_config,
    580 				   0);
    581 #ifdef CONFIG_WPS
    582 	if (!is_nil_uuid(config->uuid)) {
    583 		char buf[40];
    584 		uuid_bin2str(config->uuid, buf, sizeof(buf));
    585 		wpa_config_write_reg_string(hk, "uuid", buf);
    586 	}
    587 	wpa_config_write_reg_string(hk, "device_name", config->device_name);
    588 	wpa_config_write_reg_string(hk, "manufacturer", config->manufacturer);
    589 	wpa_config_write_reg_string(hk, "model_name", config->model_name);
    590 	wpa_config_write_reg_string(hk, "model_number", config->model_number);
    591 	wpa_config_write_reg_string(hk, "serial_number",
    592 				    config->serial_number);
    593 	{
    594 		char _buf[WPS_DEV_TYPE_BUFSIZE], *buf;
    595 		buf = wps_dev_type_bin2str(config->device_type,
    596 					   _buf, sizeof(_buf));
    597 		wpa_config_write_reg_string(hk, "device_type", buf);
    598 	}
    599 	wpa_config_write_reg_string(hk, "config_methods",
    600 				    config->config_methods);
    601 	if (WPA_GET_BE32(config->os_version)) {
    602 		char vbuf[10];
    603 		os_snprintf(vbuf, sizeof(vbuf), "%08x",
    604 			    WPA_GET_BE32(config->os_version));
    605 		wpa_config_write_reg_string(hk, "os_version", vbuf);
    606 	}
    607 	wpa_config_write_reg_dword(hk, TEXT("wps_cred_processing"),
    608 				   config->wps_cred_processing, 0);
    609 #endif /* CONFIG_WPS */
    610 #ifdef CONFIG_P2P
    611 	wpa_config_write_reg_string(hk, "p2p_ssid_postfix",
    612 				    config->p2p_ssid_postfix);
    613 	wpa_config_write_reg_dword(hk, TEXT("p2p_group_idle"),
    614 				   config->p2p_group_idle, 0);
    615 #endif /* CONFIG_P2P */
    616 
    617 	wpa_config_write_reg_dword(hk, TEXT("bss_max_count"),
    618 				   config->bss_max_count,
    619 				   DEFAULT_BSS_MAX_COUNT);
    620 	wpa_config_write_reg_dword(hk, TEXT("filter_ssids"),
    621 				   config->filter_ssids, 0);
    622 	wpa_config_write_reg_dword(hk, TEXT("max_num_sta"),
    623 				   config->max_num_sta, DEFAULT_MAX_NUM_STA);
    624 	wpa_config_write_reg_dword(hk, TEXT("disassoc_low_ack"),
    625 				   config->disassoc_low_ack, 0);
    626 
    627 	return 0;
    628 }
    629 
    630 
    631 static int wpa_config_delete_subkeys(HKEY hk, const TCHAR *key)
    632 {
    633 	HKEY nhk;
    634 	int i, errors = 0;
    635 	LONG ret;
    636 
    637 	ret = RegOpenKeyEx(hk, key, 0, KEY_ENUMERATE_SUB_KEYS | DELETE, &nhk);
    638 	if (ret != ERROR_SUCCESS) {
    639 		wpa_printf(MSG_DEBUG, "WINREG: Could not open key '" TSTR
    640 			   "' for subkey deletion: error 0x%x (%d)", key,
    641 			   (unsigned int) ret, (int) GetLastError());
    642 		return 0;
    643 	}
    644 
    645 	for (i = 0; ; i++) {
    646 		TCHAR name[255];
    647 		DWORD namelen;
    648 
    649 		namelen = 255;
    650 		ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL,
    651 				   NULL);
    652 
    653 		if (ret == ERROR_NO_MORE_ITEMS)
    654 			break;
    655 
    656 		if (ret != ERROR_SUCCESS) {
    657 			wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x (%d)",
    658 				   (unsigned int) ret, (int) GetLastError());
    659 			break;
    660 		}
    661 
    662 		if (namelen >= 255)
    663 			namelen = 255 - 1;
    664 		name[namelen] = TEXT('\0');
    665 
    666 		ret = RegDeleteKey(nhk, name);
    667 		if (ret != ERROR_SUCCESS) {
    668 			wpa_printf(MSG_DEBUG, "RegDeleteKey failed: 0x%x (%d)",
    669 				   (unsigned int) ret, (int) GetLastError());
    670 			errors++;
    671 		}
    672 	}
    673 
    674 	RegCloseKey(nhk);
    675 
    676 	return errors ? -1 : 0;
    677 }
    678 
    679 
    680 static void write_str(HKEY hk, const char *field, struct wpa_ssid *ssid)
    681 {
    682 	char *value = wpa_config_get(ssid, field);
    683 	if (value == NULL)
    684 		return;
    685 	wpa_config_write_reg_string(hk, field, value);
    686 	os_free(value);
    687 }
    688 
    689 
    690 static void write_int(HKEY hk, const char *field, int value, int def)
    691 {
    692 	char val[20];
    693 	if (value == def)
    694 		return;
    695 	os_snprintf(val, sizeof(val), "%d", value);
    696 	wpa_config_write_reg_string(hk, field, val);
    697 }
    698 
    699 
    700 static void write_bssid(HKEY hk, struct wpa_ssid *ssid)
    701 {
    702 	char *value = wpa_config_get(ssid, "bssid");
    703 	if (value == NULL)
    704 		return;
    705 	wpa_config_write_reg_string(hk, "bssid", value);
    706 	os_free(value);
    707 }
    708 
    709 
    710 static void write_psk(HKEY hk, struct wpa_ssid *ssid)
    711 {
    712 	char *value = wpa_config_get(ssid, "psk");
    713 	if (value == NULL)
    714 		return;
    715 	wpa_config_write_reg_string(hk, "psk", value);
    716 	os_free(value);
    717 }
    718 
    719 
    720 static void write_proto(HKEY hk, struct wpa_ssid *ssid)
    721 {
    722 	char *value;
    723 
    724 	if (ssid->proto == DEFAULT_PROTO)
    725 		return;
    726 
    727 	value = wpa_config_get(ssid, "proto");
    728 	if (value == NULL)
    729 		return;
    730 	if (value[0])
    731 		wpa_config_write_reg_string(hk, "proto", value);
    732 	os_free(value);
    733 }
    734 
    735 
    736 static void write_key_mgmt(HKEY hk, struct wpa_ssid *ssid)
    737 {
    738 	char *value;
    739 
    740 	if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
    741 		return;
    742 
    743 	value = wpa_config_get(ssid, "key_mgmt");
    744 	if (value == NULL)
    745 		return;
    746 	if (value[0])
    747 		wpa_config_write_reg_string(hk, "key_mgmt", value);
    748 	os_free(value);
    749 }
    750 
    751 
    752 static void write_pairwise(HKEY hk, struct wpa_ssid *ssid)
    753 {
    754 	char *value;
    755 
    756 	if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
    757 		return;
    758 
    759 	value = wpa_config_get(ssid, "pairwise");
    760 	if (value == NULL)
    761 		return;
    762 	if (value[0])
    763 		wpa_config_write_reg_string(hk, "pairwise", value);
    764 	os_free(value);
    765 }
    766 
    767 
    768 static void write_group(HKEY hk, struct wpa_ssid *ssid)
    769 {
    770 	char *value;
    771 
    772 	if (ssid->group_cipher == DEFAULT_GROUP)
    773 		return;
    774 
    775 	value = wpa_config_get(ssid, "group");
    776 	if (value == NULL)
    777 		return;
    778 	if (value[0])
    779 		wpa_config_write_reg_string(hk, "group", value);
    780 	os_free(value);
    781 }
    782 
    783 
    784 static void write_auth_alg(HKEY hk, struct wpa_ssid *ssid)
    785 {
    786 	char *value;
    787 
    788 	if (ssid->auth_alg == 0)
    789 		return;
    790 
    791 	value = wpa_config_get(ssid, "auth_alg");
    792 	if (value == NULL)
    793 		return;
    794 	if (value[0])
    795 		wpa_config_write_reg_string(hk, "auth_alg", value);
    796 	os_free(value);
    797 }
    798 
    799 
    800 #ifdef IEEE8021X_EAPOL
    801 static void write_eap(HKEY hk, struct wpa_ssid *ssid)
    802 {
    803 	char *value;
    804 
    805 	value = wpa_config_get(ssid, "eap");
    806 	if (value == NULL)
    807 		return;
    808 
    809 	if (value[0])
    810 		wpa_config_write_reg_string(hk, "eap", value);
    811 	os_free(value);
    812 }
    813 #endif /* IEEE8021X_EAPOL */
    814 
    815 
    816 static void write_wep_key(HKEY hk, int idx, struct wpa_ssid *ssid)
    817 {
    818 	char field[20], *value;
    819 
    820 	os_snprintf(field, sizeof(field), "wep_key%d", idx);
    821 	value = wpa_config_get(ssid, field);
    822 	if (value) {
    823 		wpa_config_write_reg_string(hk, field, value);
    824 		os_free(value);
    825 	}
    826 }
    827 
    828 
    829 static int wpa_config_write_network(HKEY hk, struct wpa_ssid *ssid, int id)
    830 {
    831 	int i, errors = 0;
    832 	HKEY nhk, netw;
    833 	LONG ret;
    834 	TCHAR name[5];
    835 
    836 	ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_CREATE_SUB_KEY, &nhk);
    837 	if (ret != ERROR_SUCCESS) {
    838 		wpa_printf(MSG_DEBUG, "WINREG: Could not open networks key "
    839 			   "for subkey addition: error 0x%x (%d)",
    840 			   (unsigned int) ret, (int) GetLastError());
    841 		return 0;
    842 	}
    843 
    844 #ifdef UNICODE
    845 	wsprintf(name, L"%04d", id);
    846 #else /* UNICODE */
    847 	os_snprintf(name, sizeof(name), "%04d", id);
    848 #endif /* UNICODE */
    849 	ret = RegCreateKeyEx(nhk, name, 0, NULL, 0, KEY_WRITE, NULL, &netw,
    850 			     NULL);
    851 	RegCloseKey(nhk);
    852 	if (ret != ERROR_SUCCESS) {
    853 		wpa_printf(MSG_DEBUG, "WINREG: Could not add network key '%s':"
    854 			   " error 0x%x (%d)",
    855 			   name, (unsigned int) ret, (int) GetLastError());
    856 		return -1;
    857 	}
    858 
    859 #define STR(t) write_str(netw, #t, ssid)
    860 #define INT(t) write_int(netw, #t, ssid->t, 0)
    861 #define INTe(t) write_int(netw, #t, ssid->eap.t, 0)
    862 #define INT_DEF(t, def) write_int(netw, #t, ssid->t, def)
    863 #define INT_DEFe(t, def) write_int(netw, #t, ssid->eap.t, def)
    864 
    865 	STR(ssid);
    866 	INT(scan_ssid);
    867 	write_bssid(netw, ssid);
    868 	write_psk(netw, ssid);
    869 	write_proto(netw, ssid);
    870 	write_key_mgmt(netw, ssid);
    871 	write_pairwise(netw, ssid);
    872 	write_group(netw, ssid);
    873 	write_auth_alg(netw, ssid);
    874 #ifdef IEEE8021X_EAPOL
    875 	write_eap(netw, ssid);
    876 	STR(identity);
    877 	STR(anonymous_identity);
    878 	STR(password);
    879 	STR(ca_cert);
    880 	STR(ca_path);
    881 	STR(client_cert);
    882 	STR(private_key);
    883 	STR(private_key_passwd);
    884 	STR(dh_file);
    885 	STR(subject_match);
    886 	STR(altsubject_match);
    887 	STR(ca_cert2);
    888 	STR(ca_path2);
    889 	STR(client_cert2);
    890 	STR(private_key2);
    891 	STR(private_key2_passwd);
    892 	STR(dh_file2);
    893 	STR(subject_match2);
    894 	STR(altsubject_match2);
    895 	STR(phase1);
    896 	STR(phase2);
    897 	STR(pcsc);
    898 	STR(pin);
    899 	STR(engine_id);
    900 	STR(key_id);
    901 	STR(cert_id);
    902 	STR(ca_cert_id);
    903 	STR(key2_id);
    904 	STR(pin2);
    905 	STR(engine2_id);
    906 	STR(cert2_id);
    907 	STR(ca_cert2_id);
    908 	INTe(engine);
    909 	INTe(engine2);
    910 	INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
    911 #endif /* IEEE8021X_EAPOL */
    912 	for (i = 0; i < 4; i++)
    913 		write_wep_key(netw, i, ssid);
    914 	INT(wep_tx_keyidx);
    915 	INT(priority);
    916 #ifdef IEEE8021X_EAPOL
    917 	INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
    918 	STR(pac_file);
    919 	INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
    920 #endif /* IEEE8021X_EAPOL */
    921 	INT(mode);
    922 	INT(proactive_key_caching);
    923 	INT(disabled);
    924 	INT(peerkey);
    925 #ifdef CONFIG_IEEE80211W
    926 	INT(ieee80211w);
    927 #endif /* CONFIG_IEEE80211W */
    928 	STR(id_str);
    929 
    930 #undef STR
    931 #undef INT
    932 #undef INT_DEF
    933 
    934 	RegCloseKey(netw);
    935 
    936 	return errors ? -1 : 0;
    937 }
    938 
    939 
    940 static int wpa_config_write_blob(HKEY hk, struct wpa_config_blob *blob)
    941 {
    942 	HKEY bhk;
    943 	LONG ret;
    944 	TCHAR *name;
    945 
    946 	ret = RegCreateKeyEx(hk, TEXT("blobs"), 0, NULL, 0, KEY_WRITE, NULL,
    947 			     &bhk, NULL);
    948 	if (ret != ERROR_SUCCESS) {
    949 		wpa_printf(MSG_DEBUG, "WINREG: Could not add blobs key: "
    950 			   "error 0x%x (%d)",
    951 			   (unsigned int) ret, (int) GetLastError());
    952 		return -1;
    953 	}
    954 
    955 	name = wpa_strdup_tchar(blob->name);
    956 	ret = RegSetValueEx(bhk, name, 0, REG_BINARY, blob->data,
    957 			    blob->len);
    958 	if (ret != ERROR_SUCCESS) {
    959 		wpa_printf(MSG_ERROR, "WINREG: Failed to set blob %s': "
    960 			   "error 0x%x (%d)", blob->name, (unsigned int) ret,
    961 			   (int) GetLastError());
    962 		RegCloseKey(bhk);
    963 		os_free(name);
    964 		return -1;
    965 	}
    966 	os_free(name);
    967 
    968 	RegCloseKey(bhk);
    969 
    970 	return 0;
    971 }
    972 
    973 
    974 int wpa_config_write(const char *name, struct wpa_config *config)
    975 {
    976 	TCHAR buf[256];
    977 	HKEY hk;
    978 	LONG ret;
    979 	int errors = 0;
    980 	struct wpa_ssid *ssid;
    981 	struct wpa_config_blob *blob;
    982 	int id;
    983 
    984 	wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
    985 
    986 #ifdef UNICODE
    987 	_snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);
    988 #else /* UNICODE */
    989 	os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);
    990 #endif /* UNICODE */
    991 
    992 	ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_SET_VALUE | DELETE, &hk);
    993 	if (ret != ERROR_SUCCESS) {
    994 		wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
    995 			   "configuration registry %s: error %d", buf,
    996 			   (int) GetLastError());
    997 		return -1;
    998 	}
    999 
   1000 	if (wpa_config_write_global(config, hk)) {
   1001 		wpa_printf(MSG_ERROR, "Failed to write global configuration "
   1002 			   "data");
   1003 		errors++;
   1004 	}
   1005 
   1006 	wpa_config_delete_subkeys(hk, TEXT("networks"));
   1007 	for (ssid = config->ssid, id = 0; ssid; ssid = ssid->next, id++) {
   1008 		if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
   1009 			continue; /* do not save temporary WPS networks */
   1010 		if (wpa_config_write_network(hk, ssid, id))
   1011 			errors++;
   1012 	}
   1013 
   1014 	RegDeleteKey(hk, TEXT("blobs"));
   1015 	for (blob = config->blobs; blob; blob = blob->next) {
   1016 		if (wpa_config_write_blob(hk, blob))
   1017 			errors++;
   1018 	}
   1019 
   1020 	RegCloseKey(hk);
   1021 
   1022 	wpa_printf(MSG_DEBUG, "Configuration '%s' written %ssuccessfully",
   1023 		   name, errors ? "un" : "");
   1024 	return errors ? -1 : 0;
   1025 }
   1026