2
* WPA Supplicant / Configuration backend: Windows registry
3
* Copyright (c) 2003-2006, Jouni Malinen <j@w1.fi>
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.
9
* Alternatively, this software may be distributed under the terms of BSD
12
* See README and COPYING for more details.
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.
19
* Configuration data is in HKEY_LOCAL_MACHINE\SOFTWARE\wpa_supplicant\configs
20
* key. Each configuration profile has its own key under this. In terms of text
21
* files, each profile would map to a separate text file with possibly multiple
22
* networks. Under each profile, there is a networks key that lists all
23
* networks as a subkey. Each network has set of values in the same way as
24
* network block in the configuration file. In addition, blobs subkey has
25
* possible blobs as values.
27
* HKEY_LOCAL_MACHINE\SOFTWARE\wpa_supplicant\configs\test\networks\0000
38
#define WPA_KEY_ROOT HKEY_LOCAL_MACHINE
40
#ifndef WPA_KEY_PREFIX
41
#define WPA_KEY_PREFIX TEXT("SOFTWARE\\wpa_supplicant")
51
static int wpa_config_read_blobs(struct wpa_config *config, HKEY hk)
53
struct wpa_config_blob *blob;
59
ret = RegOpenKeyEx(hk, TEXT("blobs"), 0, KEY_QUERY_VALUE, &bhk);
60
if (ret != ERROR_SUCCESS) {
61
wpa_printf(MSG_DEBUG, "Could not open wpa_supplicant config "
63
return 0; /* assume no blobs */
70
DWORD namelen, datalen, type;
73
datalen = sizeof(data);
74
ret = RegEnumValue(bhk, i, name, &namelen, NULL, &type,
75
(LPBYTE) data, &datalen);
77
if (ret == ERROR_NO_MORE_ITEMS)
80
if (ret != ERROR_SUCCESS) {
81
wpa_printf(MSG_DEBUG, "RegEnumValue failed: 0x%x",
86
if (namelen >= TNAMELEN)
87
namelen = TNAMELEN - 1;
88
name[namelen] = TEXT('\0');
89
wpa_unicode2ascii_inplace(name);
91
if (datalen >= sizeof(data))
92
datalen = sizeof(data) - 1;
94
wpa_printf(MSG_MSGDUMP, "blob %d: field='%s' len %d",
95
(int) i, name, (int) datalen);
97
blob = os_zalloc(sizeof(*blob));
102
blob->name = os_strdup((char *) name);
103
blob->data = os_malloc(datalen);
104
if (blob->name == NULL || blob->data == NULL) {
105
wpa_config_free_blob(blob);
109
os_memcpy(blob->data, data, datalen);
112
wpa_config_set_blob(config, blob);
117
return errors ? -1 : 0;
121
static int wpa_config_read_reg_dword(HKEY hk, const TCHAR *name, int *_val)
126
buflen = sizeof(val);
127
ret = RegQueryValueEx(hk, name, NULL, NULL, (LPBYTE) &val, &buflen);
128
if (ret == ERROR_SUCCESS && buflen == sizeof(val)) {
129
wpa_printf(MSG_DEBUG, TSTR "=%d", name, (int) val);
138
static char * wpa_config_read_reg_string(HKEY hk, const TCHAR *name)
145
ret = RegQueryValueEx(hk, name, NULL, NULL, NULL, &buflen);
146
if (ret != ERROR_SUCCESS)
148
val = os_malloc(buflen);
152
ret = RegQueryValueEx(hk, name, NULL, NULL, (LPBYTE) val, &buflen);
153
if (ret != ERROR_SUCCESS) {
158
wpa_unicode2ascii_inplace(val);
159
wpa_printf(MSG_DEBUG, TSTR "=%s", name, (char *) val);
164
static int wpa_config_read_global(struct wpa_config *config, HKEY hk)
168
wpa_config_read_reg_dword(hk, TEXT("ap_scan"), &config->ap_scan);
169
wpa_config_read_reg_dword(hk, TEXT("fast_reauth"),
170
&config->fast_reauth);
171
wpa_config_read_reg_dword(hk, TEXT("dot11RSNAConfigPMKLifetime"),
172
&config->dot11RSNAConfigPMKLifetime);
173
wpa_config_read_reg_dword(hk,
174
TEXT("dot11RSNAConfigPMKReauthThreshold"),
175
&config->dot11RSNAConfigPMKReauthThreshold);
176
wpa_config_read_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"),
177
&config->dot11RSNAConfigSATimeout);
178
wpa_config_read_reg_dword(hk, TEXT("update_config"),
179
&config->update_config);
181
if (wpa_config_read_reg_dword(hk, TEXT("eapol_version"),
182
&config->eapol_version) == 0) {
183
if (config->eapol_version < 1 ||
184
config->eapol_version > 2) {
185
wpa_printf(MSG_ERROR, "Invalid EAPOL version (%d)",
186
config->eapol_version);
191
config->ctrl_interface = wpa_config_read_reg_string(
192
hk, TEXT("ctrl_interface"));
194
return errors ? -1 : 0;
198
static struct wpa_ssid * wpa_config_read_network(HKEY hk, const TCHAR *netw,
204
struct wpa_ssid *ssid;
207
ret = RegOpenKeyEx(hk, netw, 0, KEY_QUERY_VALUE, &nhk);
208
if (ret != ERROR_SUCCESS) {
209
wpa_printf(MSG_DEBUG, "Could not open wpa_supplicant config "
210
"network '" TSTR "'", netw);
214
wpa_printf(MSG_MSGDUMP, "Start of a new network '" TSTR "'", netw);
215
ssid = os_zalloc(sizeof(*ssid));
222
wpa_config_set_network_defaults(ssid);
225
TCHAR name[255], data[1024];
226
DWORD namelen, datalen, type;
229
datalen = sizeof(data);
230
ret = RegEnumValue(nhk, i, name, &namelen, NULL, &type,
231
(LPBYTE) data, &datalen);
233
if (ret == ERROR_NO_MORE_ITEMS)
236
if (ret != ERROR_SUCCESS) {
237
wpa_printf(MSG_ERROR, "RegEnumValue failed: 0x%x",
244
name[namelen] = TEXT('\0');
248
data[datalen] = TEXT('\0');
250
wpa_unicode2ascii_inplace(name);
251
wpa_unicode2ascii_inplace(data);
252
if (wpa_config_set(ssid, (char *) name, (char *) data, 0) < 0)
258
if (ssid->passphrase) {
260
wpa_printf(MSG_ERROR, "Both PSK and passphrase "
261
"configured for network '" TSTR "'.", netw);
264
wpa_config_update_psk(ssid);
267
if ((ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK)) &&
269
wpa_printf(MSG_ERROR, "WPA-PSK accepted for key management, "
270
"but no PSK configured for network '" TSTR "'.",
275
if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
276
!(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
277
!(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
278
/* Group cipher cannot be stronger than the pairwise cipher. */
279
wpa_printf(MSG_DEBUG, "Removed CCMP from group cipher "
280
"list since it was not allowed for pairwise "
281
"cipher for network '" TSTR "'.", netw);
282
ssid->group_cipher &= ~WPA_CIPHER_CCMP;
286
wpa_config_free_ssid(ssid);
294
static int wpa_config_read_networks(struct wpa_config *config, HKEY hk)
297
struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
302
ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_ENUMERATE_SUB_KEYS,
304
if (ret != ERROR_SUCCESS) {
305
wpa_printf(MSG_ERROR, "Could not open wpa_supplicant networks "
315
ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL,
318
if (ret == ERROR_NO_MORE_ITEMS)
321
if (ret != ERROR_SUCCESS) {
322
wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x",
329
name[namelen] = '\0';
331
ssid = wpa_config_read_network(nhk, name, i);
333
wpa_printf(MSG_ERROR, "Failed to parse network "
334
"profile '%s'.", name);
344
if (wpa_config_add_prio_network(config, ssid)) {
345
wpa_printf(MSG_ERROR, "Failed to add network profile "
346
"'%s' to priority list.", name);
356
return errors ? -1 : 0;
360
struct wpa_config * wpa_config_read(const char *name)
364
struct wpa_config *config;
368
config = wpa_config_alloc_empty(NULL, NULL);
371
wpa_printf(MSG_DEBUG, "Reading configuration profile '%s'", name);
374
_snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);
376
os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);
379
ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_QUERY_VALUE, &hk);
380
if (ret != ERROR_SUCCESS) {
381
wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
382
"configuration registry HKLM\\" TSTR, buf);
387
if (wpa_config_read_global(config, hk))
390
if (wpa_config_read_networks(config, hk))
393
if (wpa_config_read_blobs(config, hk))
396
wpa_config_debug_dump_networks(config);
401
wpa_config_free(config);
409
static int wpa_config_write_reg_dword(HKEY hk, const TCHAR *name, int val,
416
RegDeleteValue(hk, name);
420
ret = RegSetValueEx(hk, name, 0, REG_DWORD, (LPBYTE) &_val,
422
if (ret != ERROR_SUCCESS) {
423
wpa_printf(MSG_ERROR, "WINREG: Failed to set %s=%d: error %d",
424
name, val, (int) GetLastError());
432
static int wpa_config_write_reg_string(HKEY hk, const char *name,
438
_name = wpa_strdup_tchar(name);
443
RegDeleteValue(hk, _name);
448
_val = wpa_strdup_tchar(val);
453
ret = RegSetValueEx(hk, _name, 0, REG_SZ, (BYTE *) _val,
454
(os_strlen(val) + 1) * sizeof(TCHAR));
455
if (ret != ERROR_SUCCESS) {
456
wpa_printf(MSG_ERROR, "WINREG: Failed to set %s='%s': "
457
"error %d", name, val, (int) GetLastError());
469
static int wpa_config_write_global(struct wpa_config *config, HKEY hk)
471
#ifdef CONFIG_CTRL_IFACE
472
wpa_config_write_reg_string(hk, "ctrl_interface",
473
config->ctrl_interface);
474
#endif /* CONFIG_CTRL_IFACE */
476
wpa_config_write_reg_dword(hk, TEXT("eapol_version"),
477
config->eapol_version,
478
DEFAULT_EAPOL_VERSION);
479
wpa_config_write_reg_dword(hk, TEXT("ap_scan"), config->ap_scan,
481
wpa_config_write_reg_dword(hk, TEXT("fast_reauth"),
482
config->fast_reauth, DEFAULT_FAST_REAUTH);
483
wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigPMKLifetime"),
484
config->dot11RSNAConfigPMKLifetime, 0);
485
wpa_config_write_reg_dword(hk,
486
TEXT("dot11RSNAConfigPMKReauthThreshold"),
487
config->dot11RSNAConfigPMKReauthThreshold,
489
wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"),
490
config->dot11RSNAConfigSATimeout, 0);
491
wpa_config_write_reg_dword(hk, TEXT("update_config"),
492
config->update_config,
499
static int wpa_config_delete_subkeys(HKEY hk, const TCHAR *key)
505
ret = RegOpenKeyEx(hk, key, 0, KEY_ENUMERATE_SUB_KEYS | DELETE, &nhk);
506
if (ret != ERROR_SUCCESS) {
507
wpa_printf(MSG_DEBUG, "WINREG: Could not open key '" TSTR
508
"' for subkey deletion: error 0x%x (%d)", key,
509
(unsigned int) ret, (int) GetLastError());
518
ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL,
521
if (ret == ERROR_NO_MORE_ITEMS)
524
if (ret != ERROR_SUCCESS) {
525
wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x (%d)",
526
(unsigned int) ret, (int) GetLastError());
532
name[namelen] = TEXT('\0');
534
ret = RegDeleteKey(nhk, name);
535
if (ret != ERROR_SUCCESS) {
536
wpa_printf(MSG_DEBUG, "RegDeleteKey failed: 0x%x (%d)",
537
(unsigned int) ret, (int) GetLastError());
544
return errors ? -1 : 0;
548
static void write_str(HKEY hk, const char *field, struct wpa_ssid *ssid)
550
char *value = wpa_config_get(ssid, field);
553
wpa_config_write_reg_string(hk, field, value);
558
static void write_int(HKEY hk, const char *field, int value, int def)
563
os_snprintf(val, sizeof(val), "%d", value);
564
wpa_config_write_reg_string(hk, field, val);
568
static void write_bssid(HKEY hk, struct wpa_ssid *ssid)
570
char *value = wpa_config_get(ssid, "bssid");
573
wpa_config_write_reg_string(hk, "bssid", value);
578
static void write_psk(HKEY hk, struct wpa_ssid *ssid)
580
char *value = wpa_config_get(ssid, "psk");
583
wpa_config_write_reg_string(hk, "psk", value);
588
static void write_proto(HKEY hk, struct wpa_ssid *ssid)
592
if (ssid->proto == DEFAULT_PROTO)
595
value = wpa_config_get(ssid, "proto");
599
wpa_config_write_reg_string(hk, "proto", value);
604
static void write_key_mgmt(HKEY hk, struct wpa_ssid *ssid)
608
if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
611
value = wpa_config_get(ssid, "key_mgmt");
615
wpa_config_write_reg_string(hk, "key_mgmt", value);
620
static void write_pairwise(HKEY hk, struct wpa_ssid *ssid)
624
if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
627
value = wpa_config_get(ssid, "pairwise");
631
wpa_config_write_reg_string(hk, "pairwise", value);
636
static void write_group(HKEY hk, struct wpa_ssid *ssid)
640
if (ssid->group_cipher == DEFAULT_GROUP)
643
value = wpa_config_get(ssid, "group");
647
wpa_config_write_reg_string(hk, "group", value);
652
static void write_auth_alg(HKEY hk, struct wpa_ssid *ssid)
656
if (ssid->auth_alg == 0)
659
value = wpa_config_get(ssid, "auth_alg");
663
wpa_config_write_reg_string(hk, "auth_alg", value);
668
#ifdef IEEE8021X_EAPOL
669
static void write_eap(HKEY hk, struct wpa_ssid *ssid)
673
value = wpa_config_get(ssid, "eap");
678
wpa_config_write_reg_string(hk, "eap", value);
681
#endif /* IEEE8021X_EAPOL */
684
static void write_wep_key(HKEY hk, int idx, struct wpa_ssid *ssid)
686
char field[20], *value;
688
os_snprintf(field, sizeof(field), "wep_key%d", idx);
689
value = wpa_config_get(ssid, field);
691
wpa_config_write_reg_string(hk, field, value);
697
static int wpa_config_write_network(HKEY hk, struct wpa_ssid *ssid, int id)
704
ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_CREATE_SUB_KEY, &nhk);
705
if (ret != ERROR_SUCCESS) {
706
wpa_printf(MSG_DEBUG, "WINREG: Could not open networks key "
707
"for subkey addition: error 0x%x (%d)",
708
(unsigned int) ret, (int) GetLastError());
713
wsprintf(name, L"%04d", id);
715
os_snprintf(name, sizeof(name), "%04d", id);
717
ret = RegCreateKeyEx(nhk, name, 0, NULL, 0, KEY_WRITE, NULL, &netw,
720
if (ret != ERROR_SUCCESS) {
721
wpa_printf(MSG_DEBUG, "WINREG: Could not add network key '%s':"
723
name, (unsigned int) ret, (int) GetLastError());
727
#define STR(t) write_str(netw, #t, ssid)
728
#define INT(t) write_int(netw, #t, ssid->t, 0)
729
#define INTe(t) write_int(netw, #t, ssid->eap.t, 0)
730
#define INT_DEF(t, def) write_int(netw, #t, ssid->t, def)
731
#define INT_DEFe(t, def) write_int(netw, #t, ssid->eap.t, def)
735
write_bssid(netw, ssid);
736
write_psk(netw, ssid);
737
write_proto(netw, ssid);
738
write_key_mgmt(netw, ssid);
739
write_pairwise(netw, ssid);
740
write_group(netw, ssid);
741
write_auth_alg(netw, ssid);
742
#ifdef IEEE8021X_EAPOL
743
write_eap(netw, ssid);
745
STR(anonymous_identity);
751
STR(private_key_passwd);
754
STR(altsubject_match);
759
STR(private_key2_passwd);
762
STR(altsubject_match2);
770
INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
771
#endif /* IEEE8021X_EAPOL */
772
for (i = 0; i < 4; i++)
773
write_wep_key(netw, i, ssid);
776
#ifdef IEEE8021X_EAPOL
777
INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
779
INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
780
#endif /* IEEE8021X_EAPOL */
782
INT(proactive_key_caching);
785
#ifdef CONFIG_IEEE80211W
787
#endif /* CONFIG_IEEE80211W */
796
return errors ? -1 : 0;
800
static int wpa_config_write_blob(HKEY hk, struct wpa_config_blob *blob)
806
ret = RegCreateKeyEx(hk, TEXT("blobs"), 0, NULL, 0, KEY_WRITE, NULL,
808
if (ret != ERROR_SUCCESS) {
809
wpa_printf(MSG_DEBUG, "WINREG: Could not add blobs key: "
811
(unsigned int) ret, (int) GetLastError());
815
name = wpa_strdup_tchar(blob->name);
816
ret = RegSetValueEx(bhk, name, 0, REG_BINARY, blob->data,
818
if (ret != ERROR_SUCCESS) {
819
wpa_printf(MSG_ERROR, "WINREG: Failed to set blob %s': "
820
"error 0x%x (%d)", blob->name, (unsigned int) ret,
821
(int) GetLastError());
834
int wpa_config_write(const char *name, struct wpa_config *config)
840
struct wpa_ssid *ssid;
841
struct wpa_config_blob *blob;
844
wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
847
_snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);
849
os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);
852
ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_SET_VALUE | DELETE, &hk);
853
if (ret != ERROR_SUCCESS) {
854
wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
855
"configuration registry %s: error %d", buf,
856
(int) GetLastError());
860
if (wpa_config_write_global(config, hk)) {
861
wpa_printf(MSG_ERROR, "Failed to write global configuration "
866
wpa_config_delete_subkeys(hk, TEXT("networks"));
867
for (ssid = config->ssid, id = 0; ssid; ssid = ssid->next, id++) {
868
if (wpa_config_write_network(hk, ssid, id))
872
RegDeleteKey(hk, TEXT("blobs"));
873
for (blob = config->blobs; blob; blob = blob->next) {
874
if (wpa_config_write_blob(hk, blob))
880
wpa_printf(MSG_DEBUG, "Configuration '%s' written %ssuccessfully",
881
name, errors ? "un" : "");
882
return errors ? -1 : 0;