2 * WPA Supplicant / Configuration backend: Windows registry
3 * Copyright (c) 2003-2008, 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
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.
28 * Example network configuration block:
30 HKEY_LOCAL_MACHINE\SOFTWARE\wpa_supplicant\configs\test\networks\0000
43 #define WPA_KEY_ROOT HKEY_LOCAL_MACHINE
45 #ifndef WPA_KEY_PREFIX
46 #define WPA_KEY_PREFIX TEXT("SOFTWARE\\wpa_supplicant")
56 static int wpa_config_read_blobs(struct wpa_config *config, HKEY hk)
58 struct wpa_config_blob *blob;
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 "
68 return 0; /* assume no blobs */
75 DWORD namelen, datalen, type;
78 datalen = sizeof(data);
79 ret = RegEnumValue(bhk, i, name, &namelen, NULL, &type,
80 (LPBYTE) data, &datalen);
82 if (ret == ERROR_NO_MORE_ITEMS)
85 if (ret != ERROR_SUCCESS) {
86 wpa_printf(MSG_DEBUG, "RegEnumValue failed: 0x%x",
91 if (namelen >= TNAMELEN)
92 namelen = TNAMELEN - 1;
93 name[namelen] = TEXT('\0');
94 wpa_unicode2ascii_inplace(name);
96 if (datalen >= sizeof(data))
97 datalen = sizeof(data) - 1;
99 wpa_printf(MSG_MSGDUMP, "blob %d: field='%s' len %d",
100 (int) i, name, (int) datalen);
102 blob = os_zalloc(sizeof(*blob));
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);
114 os_memcpy(blob->data, data, datalen);
117 wpa_config_set_blob(config, blob);
122 return errors ? -1 : 0;
126 static int wpa_config_read_reg_dword(HKEY hk, const TCHAR *name, int *_val)
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);
143 static char * wpa_config_read_reg_string(HKEY hk, const TCHAR *name)
150 ret = RegQueryValueEx(hk, name, NULL, NULL, NULL, &buflen);
151 if (ret != ERROR_SUCCESS)
153 val = os_malloc(buflen);
157 ret = RegQueryValueEx(hk, name, NULL, NULL, (LPBYTE) val, &buflen);
158 if (ret != ERROR_SUCCESS) {
163 wpa_unicode2ascii_inplace(val);
164 wpa_printf(MSG_DEBUG, TSTR "=%s", name, (char *) val);
170 static int wpa_config_read_global_uuid(struct wpa_config *config, HKEY hk)
175 str = wpa_config_read_reg_string(hk, TEXT("uuid"));
179 if (uuid_str2bin(str, config->uuid))
188 static int wpa_config_read_global_os_version(struct wpa_config *config,
194 str = wpa_config_read_reg_string(hk, TEXT("os_version"));
198 if (hexstr2bin(str, config->os_version, 4))
205 #endif /* CONFIG_WPS */
208 static int wpa_config_read_global(struct wpa_config *config, HKEY hk)
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"),
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);
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);
236 config->ctrl_interface = wpa_config_read_reg_string(
237 hk, TEXT("ctrl_interface"));
240 if (wpa_config_read_global_uuid(config, hk))
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 config->device_type = wpa_config_read_reg_string(
251 hk, TEXT("device_type"));
252 config->config_methods = wpa_config_read_reg_string(
253 hk, TEXT("config_methods"));
254 if (wpa_config_read_global_os_version(config, hk))
256 wpa_config_read_reg_dword(hk, TEXT("wps_cred_processing"),
257 &config->wps_cred_processing);
258 #endif /* CONFIG_WPS */
260 config->p2p_ssid_postfix = wpa_config_read_reg_string(
261 hk, TEXT("p2p_ssid_postfix"));
262 #endif /* CONFIG_P2P */
264 wpa_config_read_reg_dword(hk, TEXT("bss_max_count"),
265 (int *) &config->bss_max_count);
266 wpa_config_read_reg_dword(hk, TEXT("filter_ssids"),
267 &config->filter_ssids);
268 wpa_config_read_reg_dword(hk, TEXT("max_num_sta"),
269 (int *) &config->max_num_sta);
271 return errors ? -1 : 0;
275 static struct wpa_ssid * wpa_config_read_network(HKEY hk, const TCHAR *netw,
281 struct wpa_ssid *ssid;
284 ret = RegOpenKeyEx(hk, netw, 0, KEY_QUERY_VALUE, &nhk);
285 if (ret != ERROR_SUCCESS) {
286 wpa_printf(MSG_DEBUG, "Could not open wpa_supplicant config "
287 "network '" TSTR "'", netw);
291 wpa_printf(MSG_MSGDUMP, "Start of a new network '" TSTR "'", netw);
292 ssid = os_zalloc(sizeof(*ssid));
299 wpa_config_set_network_defaults(ssid);
302 TCHAR name[255], data[1024];
303 DWORD namelen, datalen, type;
306 datalen = sizeof(data);
307 ret = RegEnumValue(nhk, i, name, &namelen, NULL, &type,
308 (LPBYTE) data, &datalen);
310 if (ret == ERROR_NO_MORE_ITEMS)
313 if (ret != ERROR_SUCCESS) {
314 wpa_printf(MSG_ERROR, "RegEnumValue failed: 0x%x",
321 name[namelen] = TEXT('\0');
325 data[datalen] = TEXT('\0');
327 wpa_unicode2ascii_inplace(name);
328 wpa_unicode2ascii_inplace(data);
329 if (wpa_config_set(ssid, (char *) name, (char *) data, 0) < 0)
335 if (ssid->passphrase) {
337 wpa_printf(MSG_ERROR, "Both PSK and passphrase "
338 "configured for network '" TSTR "'.", netw);
341 wpa_config_update_psk(ssid);
344 if ((ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK |
345 WPA_KEY_MGMT_PSK_SHA256)) &&
347 wpa_printf(MSG_ERROR, "WPA-PSK accepted for key management, "
348 "but no PSK configured for network '" TSTR "'.",
353 if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
354 !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
355 !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
356 /* Group cipher cannot be stronger than the pairwise cipher. */
357 wpa_printf(MSG_DEBUG, "Removed CCMP from group cipher "
358 "list since it was not allowed for pairwise "
359 "cipher for network '" TSTR "'.", netw);
360 ssid->group_cipher &= ~WPA_CIPHER_CCMP;
364 wpa_config_free_ssid(ssid);
372 static int wpa_config_read_networks(struct wpa_config *config, HKEY hk)
375 struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
380 ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_ENUMERATE_SUB_KEYS,
382 if (ret != ERROR_SUCCESS) {
383 wpa_printf(MSG_ERROR, "Could not open wpa_supplicant networks "
393 ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL,
396 if (ret == ERROR_NO_MORE_ITEMS)
399 if (ret != ERROR_SUCCESS) {
400 wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x",
407 name[namelen] = '\0';
409 ssid = wpa_config_read_network(nhk, name, i);
411 wpa_printf(MSG_ERROR, "Failed to parse network "
412 "profile '%s'.", name);
422 if (wpa_config_add_prio_network(config, ssid)) {
423 wpa_printf(MSG_ERROR, "Failed to add network profile "
424 "'%s' to priority list.", name);
434 return errors ? -1 : 0;
438 struct wpa_config * wpa_config_read(const char *name)
442 struct wpa_config *config;
446 config = wpa_config_alloc_empty(NULL, NULL);
449 wpa_printf(MSG_DEBUG, "Reading configuration profile '%s'", name);
452 _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);
454 os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);
457 ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_QUERY_VALUE, &hk);
458 if (ret != ERROR_SUCCESS) {
459 wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
460 "configuration registry HKLM\\" TSTR, buf);
465 if (wpa_config_read_global(config, hk))
468 if (wpa_config_read_networks(config, hk))
471 if (wpa_config_read_blobs(config, hk))
474 wpa_config_debug_dump_networks(config);
479 wpa_config_free(config);
487 static int wpa_config_write_reg_dword(HKEY hk, const TCHAR *name, int val,
494 RegDeleteValue(hk, name);
498 ret = RegSetValueEx(hk, name, 0, REG_DWORD, (LPBYTE) &_val,
500 if (ret != ERROR_SUCCESS) {
501 wpa_printf(MSG_ERROR, "WINREG: Failed to set %s=%d: error %d",
502 name, val, (int) GetLastError());
510 static int wpa_config_write_reg_string(HKEY hk, const char *name,
516 _name = wpa_strdup_tchar(name);
521 RegDeleteValue(hk, _name);
526 _val = wpa_strdup_tchar(val);
531 ret = RegSetValueEx(hk, _name, 0, REG_SZ, (BYTE *) _val,
532 (os_strlen(val) + 1) * sizeof(TCHAR));
533 if (ret != ERROR_SUCCESS) {
534 wpa_printf(MSG_ERROR, "WINREG: Failed to set %s='%s': "
535 "error %d", name, val, (int) GetLastError());
547 static int wpa_config_write_global(struct wpa_config *config, HKEY hk)
549 #ifdef CONFIG_CTRL_IFACE
550 wpa_config_write_reg_string(hk, "ctrl_interface",
551 config->ctrl_interface);
552 #endif /* CONFIG_CTRL_IFACE */
554 wpa_config_write_reg_dword(hk, TEXT("eapol_version"),
555 config->eapol_version,
556 DEFAULT_EAPOL_VERSION);
557 wpa_config_write_reg_dword(hk, TEXT("ap_scan"), config->ap_scan,
559 wpa_config_write_reg_dword(hk, TEXT("fast_reauth"),
560 config->fast_reauth, DEFAULT_FAST_REAUTH);
561 wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigPMKLifetime"),
562 config->dot11RSNAConfigPMKLifetime, 0);
563 wpa_config_write_reg_dword(hk,
564 TEXT("dot11RSNAConfigPMKReauthThreshold"),
565 config->dot11RSNAConfigPMKReauthThreshold,
567 wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"),
568 config->dot11RSNAConfigSATimeout, 0);
569 wpa_config_write_reg_dword(hk, TEXT("update_config"),
570 config->update_config,
573 if (!is_nil_uuid(config->uuid)) {
575 uuid_bin2str(config->uuid, buf, sizeof(buf));
576 wpa_config_write_reg_string(hk, "uuid", buf);
578 wpa_config_write_reg_string(hk, "device_name", config->device_name);
579 wpa_config_write_reg_string(hk, "manufacturer", config->manufacturer);
580 wpa_config_write_reg_string(hk, "model_name", config->model_name);
581 wpa_config_write_reg_string(hk, "model_number", config->model_number);
582 wpa_config_write_reg_string(hk, "serial_number",
583 config->serial_number);
584 wpa_config_write_reg_string(hk, "device_type", config->device_type);
585 wpa_config_write_reg_string(hk, "config_methods",
586 config->config_methods);
587 if (WPA_GET_BE32(config->os_version)) {
589 os_snprintf(vbuf, sizeof(vbuf), "%08x",
590 WPA_GET_BE32(config->os_version));
591 wpa_config_write_reg_string(hk, "os_version", vbuf);
593 wpa_config_write_reg_dword(hk, TEXT("wps_cred_processing"),
594 config->wps_cred_processing, 0);
595 #endif /* CONFIG_WPS */
597 wpa_config_write_reg_string(hk, "p2p_ssid_postfix",
598 config->p2p_ssid_postfix);
599 #endif /* CONFIG_P2P */
601 wpa_config_write_reg_dword(hk, TEXT("bss_max_count"),
602 config->bss_max_count,
603 DEFAULT_BSS_MAX_COUNT);
604 wpa_config_write_reg_dword(hk, TEXT("filter_ssids"),
605 config->filter_ssids, 0);
606 wpa_config_write_reg_dword(hk, TEXT("max_num_sta"),
607 config->max_num_sta, DEFAULT_MAX_NUM_STA);
613 static int wpa_config_delete_subkeys(HKEY hk, const TCHAR *key)
619 ret = RegOpenKeyEx(hk, key, 0, KEY_ENUMERATE_SUB_KEYS | DELETE, &nhk);
620 if (ret != ERROR_SUCCESS) {
621 wpa_printf(MSG_DEBUG, "WINREG: Could not open key '" TSTR
622 "' for subkey deletion: error 0x%x (%d)", key,
623 (unsigned int) ret, (int) GetLastError());
632 ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL,
635 if (ret == ERROR_NO_MORE_ITEMS)
638 if (ret != ERROR_SUCCESS) {
639 wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x (%d)",
640 (unsigned int) ret, (int) GetLastError());
646 name[namelen] = TEXT('\0');
648 ret = RegDeleteKey(nhk, name);
649 if (ret != ERROR_SUCCESS) {
650 wpa_printf(MSG_DEBUG, "RegDeleteKey failed: 0x%x (%d)",
651 (unsigned int) ret, (int) GetLastError());
658 return errors ? -1 : 0;
662 static void write_str(HKEY hk, const char *field, struct wpa_ssid *ssid)
664 char *value = wpa_config_get(ssid, field);
667 wpa_config_write_reg_string(hk, field, value);
672 static void write_int(HKEY hk, const char *field, int value, int def)
677 os_snprintf(val, sizeof(val), "%d", value);
678 wpa_config_write_reg_string(hk, field, val);
682 static void write_bssid(HKEY hk, struct wpa_ssid *ssid)
684 char *value = wpa_config_get(ssid, "bssid");
687 wpa_config_write_reg_string(hk, "bssid", value);
692 static void write_psk(HKEY hk, struct wpa_ssid *ssid)
694 char *value = wpa_config_get(ssid, "psk");
697 wpa_config_write_reg_string(hk, "psk", value);
702 static void write_proto(HKEY hk, struct wpa_ssid *ssid)
706 if (ssid->proto == DEFAULT_PROTO)
709 value = wpa_config_get(ssid, "proto");
713 wpa_config_write_reg_string(hk, "proto", value);
718 static void write_key_mgmt(HKEY hk, struct wpa_ssid *ssid)
722 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
725 value = wpa_config_get(ssid, "key_mgmt");
729 wpa_config_write_reg_string(hk, "key_mgmt", value);
734 static void write_pairwise(HKEY hk, struct wpa_ssid *ssid)
738 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
741 value = wpa_config_get(ssid, "pairwise");
745 wpa_config_write_reg_string(hk, "pairwise", value);
750 static void write_group(HKEY hk, struct wpa_ssid *ssid)
754 if (ssid->group_cipher == DEFAULT_GROUP)
757 value = wpa_config_get(ssid, "group");
761 wpa_config_write_reg_string(hk, "group", value);
766 static void write_auth_alg(HKEY hk, struct wpa_ssid *ssid)
770 if (ssid->auth_alg == 0)
773 value = wpa_config_get(ssid, "auth_alg");
777 wpa_config_write_reg_string(hk, "auth_alg", value);
782 #ifdef IEEE8021X_EAPOL
783 static void write_eap(HKEY hk, struct wpa_ssid *ssid)
787 value = wpa_config_get(ssid, "eap");
792 wpa_config_write_reg_string(hk, "eap", value);
795 #endif /* IEEE8021X_EAPOL */
798 static void write_wep_key(HKEY hk, int idx, struct wpa_ssid *ssid)
800 char field[20], *value;
802 os_snprintf(field, sizeof(field), "wep_key%d", idx);
803 value = wpa_config_get(ssid, field);
805 wpa_config_write_reg_string(hk, field, value);
811 static int wpa_config_write_network(HKEY hk, struct wpa_ssid *ssid, int id)
818 ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_CREATE_SUB_KEY, &nhk);
819 if (ret != ERROR_SUCCESS) {
820 wpa_printf(MSG_DEBUG, "WINREG: Could not open networks key "
821 "for subkey addition: error 0x%x (%d)",
822 (unsigned int) ret, (int) GetLastError());
827 wsprintf(name, L"%04d", id);
829 os_snprintf(name, sizeof(name), "%04d", id);
831 ret = RegCreateKeyEx(nhk, name, 0, NULL, 0, KEY_WRITE, NULL, &netw,
834 if (ret != ERROR_SUCCESS) {
835 wpa_printf(MSG_DEBUG, "WINREG: Could not add network key '%s':"
837 name, (unsigned int) ret, (int) GetLastError());
841 #define STR(t) write_str(netw, #t, ssid)
842 #define INT(t) write_int(netw, #t, ssid->t, 0)
843 #define INTe(t) write_int(netw, #t, ssid->eap.t, 0)
844 #define INT_DEF(t, def) write_int(netw, #t, ssid->t, def)
845 #define INT_DEFe(t, def) write_int(netw, #t, ssid->eap.t, def)
849 write_bssid(netw, ssid);
850 write_psk(netw, ssid);
851 write_proto(netw, ssid);
852 write_key_mgmt(netw, ssid);
853 write_pairwise(netw, ssid);
854 write_group(netw, ssid);
855 write_auth_alg(netw, ssid);
856 #ifdef IEEE8021X_EAPOL
857 write_eap(netw, ssid);
859 STR(anonymous_identity);
865 STR(private_key_passwd);
868 STR(altsubject_match);
873 STR(private_key2_passwd);
876 STR(altsubject_match2);
892 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
893 #endif /* IEEE8021X_EAPOL */
894 for (i = 0; i < 4; i++)
895 write_wep_key(netw, i, ssid);
898 #ifdef IEEE8021X_EAPOL
899 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
901 INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
902 #endif /* IEEE8021X_EAPOL */
904 INT(proactive_key_caching);
907 #ifdef CONFIG_IEEE80211W
909 #endif /* CONFIG_IEEE80211W */
918 return errors ? -1 : 0;
922 static int wpa_config_write_blob(HKEY hk, struct wpa_config_blob *blob)
928 ret = RegCreateKeyEx(hk, TEXT("blobs"), 0, NULL, 0, KEY_WRITE, NULL,
930 if (ret != ERROR_SUCCESS) {
931 wpa_printf(MSG_DEBUG, "WINREG: Could not add blobs key: "
933 (unsigned int) ret, (int) GetLastError());
937 name = wpa_strdup_tchar(blob->name);
938 ret = RegSetValueEx(bhk, name, 0, REG_BINARY, blob->data,
940 if (ret != ERROR_SUCCESS) {
941 wpa_printf(MSG_ERROR, "WINREG: Failed to set blob %s': "
942 "error 0x%x (%d)", blob->name, (unsigned int) ret,
943 (int) GetLastError());
956 int wpa_config_write(const char *name, struct wpa_config *config)
962 struct wpa_ssid *ssid;
963 struct wpa_config_blob *blob;
966 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
969 _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);
971 os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);
974 ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_SET_VALUE | DELETE, &hk);
975 if (ret != ERROR_SUCCESS) {
976 wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
977 "configuration registry %s: error %d", buf,
978 (int) GetLastError());
982 if (wpa_config_write_global(config, hk)) {
983 wpa_printf(MSG_ERROR, "Failed to write global configuration "
988 wpa_config_delete_subkeys(hk, TEXT("networks"));
989 for (ssid = config->ssid, id = 0; ssid; ssid = ssid->next, id++) {
990 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
991 continue; /* do not save temporary WPS networks */
992 if (wpa_config_write_network(hk, ssid, id))
996 RegDeleteKey(hk, TEXT("blobs"));
997 for (blob = config->blobs; blob; blob = blob->next) {
998 if (wpa_config_write_blob(hk, blob))
1004 wpa_printf(MSG_DEBUG, "Configuration '%s' written %ssuccessfully",
1005 name, errors ? "un" : "");
1006 return errors ? -1 : 0;