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