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