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) && !ssid->psk_set) {
268 wpa_printf(MSG_ERROR, "WPA-PSK accepted for key management, "
269 "but no PSK configured for network '" TSTR "'.",
274 if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
275 !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
276 !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
277 /* Group cipher cannot be stronger than the pairwise cipher. */
278 wpa_printf(MSG_DEBUG, "Removed CCMP from group cipher "
279 "list since it was not allowed for pairwise "
280 "cipher for network '" TSTR "'.", netw);
281 ssid->group_cipher &= ~WPA_CIPHER_CCMP;
285 wpa_config_free_ssid(ssid);
293 static int wpa_config_read_networks(struct wpa_config *config, HKEY hk)
296 struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
301 ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_ENUMERATE_SUB_KEYS,
303 if (ret != ERROR_SUCCESS) {
304 wpa_printf(MSG_ERROR, "Could not open wpa_supplicant networks "
314 ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL,
317 if (ret == ERROR_NO_MORE_ITEMS)
320 if (ret != ERROR_SUCCESS) {
321 wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x",
328 name[namelen] = '\0';
330 ssid = wpa_config_read_network(nhk, name, i);
332 wpa_printf(MSG_ERROR, "Failed to parse network "
333 "profile '%s'.", name);
343 if (wpa_config_add_prio_network(config, ssid)) {
344 wpa_printf(MSG_ERROR, "Failed to add network profile "
345 "'%s' to priority list.", name);
355 return errors ? -1 : 0;
359 struct wpa_config * wpa_config_read(const char *name)
363 struct wpa_config *config;
367 config = wpa_config_alloc_empty(NULL, NULL);
370 wpa_printf(MSG_DEBUG, "Reading configuration profile '%s'", name);
373 _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);
375 os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);
378 ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_QUERY_VALUE, &hk);
379 if (ret != ERROR_SUCCESS) {
380 wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
381 "configuration registry HKLM\\" TSTR, buf);
386 if (wpa_config_read_global(config, hk))
389 if (wpa_config_read_networks(config, hk))
392 if (wpa_config_read_blobs(config, hk))
395 wpa_config_debug_dump_networks(config);
400 wpa_config_free(config);
408 static int wpa_config_write_reg_dword(HKEY hk, const TCHAR *name, int val,
415 RegDeleteValue(hk, name);
419 ret = RegSetValueEx(hk, name, 0, REG_DWORD, (LPBYTE) &_val,
421 if (ret != ERROR_SUCCESS) {
422 wpa_printf(MSG_ERROR, "WINREG: Failed to set %s=%d: error %d",
423 name, val, (int) GetLastError());
431 static int wpa_config_write_reg_string(HKEY hk, const char *name,
437 _name = wpa_strdup_tchar(name);
442 RegDeleteValue(hk, _name);
447 _val = wpa_strdup_tchar(val);
452 ret = RegSetValueEx(hk, _name, 0, REG_SZ, (BYTE *) _val,
453 (os_strlen(val) + 1) * sizeof(TCHAR));
454 if (ret != ERROR_SUCCESS) {
455 wpa_printf(MSG_ERROR, "WINREG: Failed to set %s='%s': "
456 "error %d", name, val, (int) GetLastError());
468 static int wpa_config_write_global(struct wpa_config *config, HKEY hk)
470 #ifdef CONFIG_CTRL_IFACE
471 wpa_config_write_reg_string(hk, "ctrl_interface",
472 config->ctrl_interface);
473 #endif /* CONFIG_CTRL_IFACE */
475 wpa_config_write_reg_dword(hk, TEXT("eapol_version"),
476 config->eapol_version,
477 DEFAULT_EAPOL_VERSION);
478 wpa_config_write_reg_dword(hk, TEXT("ap_scan"), config->ap_scan,
480 wpa_config_write_reg_dword(hk, TEXT("fast_reauth"),
481 config->fast_reauth, DEFAULT_FAST_REAUTH);
482 wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigPMKLifetime"),
483 config->dot11RSNAConfigPMKLifetime, 0);
484 wpa_config_write_reg_dword(hk,
485 TEXT("dot11RSNAConfigPMKReauthThreshold"),
486 config->dot11RSNAConfigPMKReauthThreshold,
488 wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"),
489 config->dot11RSNAConfigSATimeout, 0);
490 wpa_config_write_reg_dword(hk, TEXT("update_config"),
491 config->update_config,
498 static int wpa_config_delete_subkeys(HKEY hk, const TCHAR *key)
504 ret = RegOpenKeyEx(hk, key, 0, KEY_ENUMERATE_SUB_KEYS | DELETE, &nhk);
505 if (ret != ERROR_SUCCESS) {
506 wpa_printf(MSG_DEBUG, "WINREG: Could not open key '" TSTR
507 "' for subkey deletion: error 0x%x (%d)", key,
508 (unsigned int) ret, (int) GetLastError());
517 ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL,
520 if (ret == ERROR_NO_MORE_ITEMS)
523 if (ret != ERROR_SUCCESS) {
524 wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x (%d)",
525 (unsigned int) ret, (int) GetLastError());
531 name[namelen] = TEXT('\0');
533 ret = RegDeleteKey(nhk, name);
534 if (ret != ERROR_SUCCESS) {
535 wpa_printf(MSG_DEBUG, "RegDeleteKey failed: 0x%x (%d)",
536 (unsigned int) ret, (int) GetLastError());
543 return errors ? -1 : 0;
547 static void write_str(HKEY hk, const char *field, struct wpa_ssid *ssid)
549 char *value = wpa_config_get(ssid, field);
552 wpa_config_write_reg_string(hk, field, value);
557 static void write_int(HKEY hk, const char *field, int value, int def)
562 os_snprintf(val, sizeof(val), "%d", value);
563 wpa_config_write_reg_string(hk, field, val);
567 static void write_bssid(HKEY hk, struct wpa_ssid *ssid)
569 char *value = wpa_config_get(ssid, "bssid");
572 wpa_config_write_reg_string(hk, "bssid", value);
577 static void write_psk(HKEY hk, struct wpa_ssid *ssid)
579 char *value = wpa_config_get(ssid, "psk");
582 wpa_config_write_reg_string(hk, "psk", value);
587 static void write_proto(HKEY hk, struct wpa_ssid *ssid)
591 if (ssid->proto == DEFAULT_PROTO)
594 value = wpa_config_get(ssid, "proto");
598 wpa_config_write_reg_string(hk, "proto", value);
603 static void write_key_mgmt(HKEY hk, struct wpa_ssid *ssid)
607 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
610 value = wpa_config_get(ssid, "key_mgmt");
614 wpa_config_write_reg_string(hk, "key_mgmt", value);
619 static void write_pairwise(HKEY hk, struct wpa_ssid *ssid)
623 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
626 value = wpa_config_get(ssid, "pairwise");
630 wpa_config_write_reg_string(hk, "pairwise", value);
635 static void write_group(HKEY hk, struct wpa_ssid *ssid)
639 if (ssid->group_cipher == DEFAULT_GROUP)
642 value = wpa_config_get(ssid, "group");
646 wpa_config_write_reg_string(hk, "group", value);
651 static void write_auth_alg(HKEY hk, struct wpa_ssid *ssid)
655 if (ssid->auth_alg == 0)
658 value = wpa_config_get(ssid, "auth_alg");
662 wpa_config_write_reg_string(hk, "auth_alg", value);
667 #ifdef IEEE8021X_EAPOL
668 static void write_eap(HKEY hk, struct wpa_ssid *ssid)
672 value = wpa_config_get(ssid, "eap");
677 wpa_config_write_reg_string(hk, "eap", value);
680 #endif /* IEEE8021X_EAPOL */
683 static void write_wep_key(HKEY hk, int idx, struct wpa_ssid *ssid)
685 char field[20], *value;
687 os_snprintf(field, sizeof(field), "wep_key%d", idx);
688 value = wpa_config_get(ssid, field);
690 wpa_config_write_reg_string(hk, field, value);
696 static int wpa_config_write_network(HKEY hk, struct wpa_ssid *ssid, int id)
703 ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_CREATE_SUB_KEY, &nhk);
704 if (ret != ERROR_SUCCESS) {
705 wpa_printf(MSG_DEBUG, "WINREG: Could not open networks key "
706 "for subkey addition: error 0x%x (%d)",
707 (unsigned int) ret, (int) GetLastError());
712 wsprintf(name, L"%04d", id);
714 os_snprintf(name, sizeof(name), "%04d", id);
716 ret = RegCreateKeyEx(nhk, name, 0, NULL, 0, KEY_WRITE, NULL, &netw,
719 if (ret != ERROR_SUCCESS) {
720 wpa_printf(MSG_DEBUG, "WINREG: Could not add network key '%s':"
722 name, (unsigned int) ret, (int) GetLastError());
726 #define STR(t) write_str(netw, #t, ssid)
727 #define INT(t) write_int(netw, #t, ssid->t, 0)
728 #define INT_DEF(t, def) write_int(netw, #t, ssid->t, def)
732 write_bssid(netw, ssid);
733 write_psk(netw, ssid);
734 write_proto(netw, ssid);
735 write_key_mgmt(netw, ssid);
736 write_pairwise(netw, ssid);
737 write_group(netw, ssid);
738 write_auth_alg(netw, ssid);
739 #ifdef IEEE8021X_EAPOL
740 write_eap(netw, ssid);
742 STR(anonymous_identity);
750 STR(private_key_passwd);
753 STR(altsubject_match);
758 STR(private_key2_passwd);
761 STR(altsubject_match2);
769 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
770 #endif /* IEEE8021X_EAPOL */
771 for (i = 0; i < 4; i++)
772 write_wep_key(netw, i, ssid);
775 #ifdef IEEE8021X_EAPOL
776 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
778 INT_DEF(fragment_size, DEFAULT_FRAGMENT_SIZE);
779 #endif /* IEEE8021X_EAPOL */
781 INT(proactive_key_caching);
784 #ifdef CONFIG_IEEE80211W
786 #endif /* CONFIG_IEEE80211W */
795 return errors ? -1 : 0;
799 static int wpa_config_write_blob(HKEY hk, struct wpa_config_blob *blob)
805 ret = RegCreateKeyEx(hk, TEXT("blobs"), 0, NULL, 0, KEY_WRITE, NULL,
807 if (ret != ERROR_SUCCESS) {
808 wpa_printf(MSG_DEBUG, "WINREG: Could not add blobs key: "
810 (unsigned int) ret, (int) GetLastError());
814 name = wpa_strdup_tchar(blob->name);
815 ret = RegSetValueEx(bhk, name, 0, REG_BINARY, blob->data,
817 if (ret != ERROR_SUCCESS) {
818 wpa_printf(MSG_ERROR, "WINREG: Failed to set blob %s': "
819 "error 0x%x (%d)", blob->name, (unsigned int) ret,
820 (int) GetLastError());
833 int wpa_config_write(const char *name, struct wpa_config *config)
839 struct wpa_ssid *ssid;
840 struct wpa_config_blob *blob;
843 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
846 _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);
848 os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);
851 ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_SET_VALUE | DELETE, &hk);
852 if (ret != ERROR_SUCCESS) {
853 wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
854 "configuration registry %s: error %d", buf,
855 (int) GetLastError());
859 if (wpa_config_write_global(config, hk)) {
860 wpa_printf(MSG_ERROR, "Failed to write global configuration "
865 wpa_config_delete_subkeys(hk, TEXT("networks"));
866 for (ssid = config->ssid, id = 0; ssid; ssid = ssid->next, id++) {
867 if (wpa_config_write_network(hk, ssid, id))
871 RegDeleteKey(hk, TEXT("blobs"));
872 for (blob = config->blobs; blob; blob = blob->next) {
873 if (wpa_config_write_blob(hk, blob))
879 wpa_printf(MSG_DEBUG, "Configuration '%s' written %ssuccessfully",
880 name, errors ? "un" : "");
881 return errors ? -1 : 0;