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 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
39 #define WPA_KEY_ROOT HKEY_LOCAL_MACHINE
41 #ifndef WPA_KEY_PREFIX
42 #define WPA_KEY_PREFIX TEXT("SOFTWARE\\wpa_supplicant")
52 static int wpa_config_read_blobs(struct wpa_config *config, HKEY hk)
54 struct wpa_config_blob *blob;
60 ret = RegOpenKeyEx(hk, TEXT("blobs"), 0, KEY_QUERY_VALUE, &bhk);
61 if (ret != ERROR_SUCCESS) {
62 wpa_printf(MSG_DEBUG, "Could not open wpa_supplicant config "
64 return 0; /* assume no blobs */
71 DWORD namelen, datalen, type;
74 datalen = sizeof(data);
75 ret = RegEnumValue(bhk, i, name, &namelen, NULL, &type,
76 (LPBYTE) data, &datalen);
78 if (ret == ERROR_NO_MORE_ITEMS)
81 if (ret != ERROR_SUCCESS) {
82 wpa_printf(MSG_DEBUG, "RegEnumValue failed: 0x%x",
87 if (namelen >= TNAMELEN)
88 namelen = TNAMELEN - 1;
89 name[namelen] = TEXT('\0');
90 wpa_unicode2ascii_inplace(name);
92 if (datalen >= sizeof(data))
93 datalen = sizeof(data) - 1;
95 wpa_printf(MSG_MSGDUMP, "blob %d: field='%s' len %d",
96 (int) i, name, (int) datalen);
98 blob = os_zalloc(sizeof(*blob));
103 blob->name = os_strdup((char *) name);
104 blob->data = os_malloc(datalen);
105 if (blob->name == NULL || blob->data == NULL) {
106 wpa_config_free_blob(blob);
110 os_memcpy(blob->data, data, datalen);
113 wpa_config_set_blob(config, blob);
118 return errors ? -1 : 0;
122 static int wpa_config_read_reg_dword(HKEY hk, const TCHAR *name, int *_val)
127 buflen = sizeof(val);
128 ret = RegQueryValueEx(hk, name, NULL, NULL, (LPBYTE) &val, &buflen);
129 if (ret == ERROR_SUCCESS && buflen == sizeof(val)) {
130 wpa_printf(MSG_DEBUG, TSTR "=%d", name, (int) val);
139 static char * wpa_config_read_reg_string(HKEY hk, const TCHAR *name)
146 ret = RegQueryValueEx(hk, name, NULL, NULL, NULL, &buflen);
147 if (ret != ERROR_SUCCESS)
149 val = os_malloc(buflen);
153 ret = RegQueryValueEx(hk, name, NULL, NULL, (LPBYTE) val, &buflen);
154 if (ret != ERROR_SUCCESS) {
159 wpa_unicode2ascii_inplace(val);
160 wpa_printf(MSG_DEBUG, TSTR "=%s", name, (char *) val);
166 static int wpa_config_read_global_uuid(struct wpa_config *config, HKEY hk)
171 str = wpa_config_read_reg_string(hk, TEXT("uuid"));
175 if (uuid_str2bin(str, config->uuid))
184 static int wpa_config_read_global_os_version(struct wpa_config *config,
190 str = wpa_config_read_reg_string(hk, TEXT("os_version"));
194 if (hexstr2bin(str, config->os_version, 4))
201 #endif /* CONFIG_WPS */
204 static int wpa_config_read_global(struct wpa_config *config, HKEY hk)
208 wpa_config_read_reg_dword(hk, TEXT("ap_scan"), &config->ap_scan);
209 wpa_config_read_reg_dword(hk, TEXT("fast_reauth"),
210 &config->fast_reauth);
211 wpa_config_read_reg_dword(hk, TEXT("dot11RSNAConfigPMKLifetime"),
212 (int *) &config->dot11RSNAConfigPMKLifetime);
213 wpa_config_read_reg_dword(hk,
214 TEXT("dot11RSNAConfigPMKReauthThreshold"),
216 &config->dot11RSNAConfigPMKReauthThreshold);
217 wpa_config_read_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"),
218 (int *) &config->dot11RSNAConfigSATimeout);
219 wpa_config_read_reg_dword(hk, TEXT("update_config"),
220 &config->update_config);
222 if (wpa_config_read_reg_dword(hk, TEXT("eapol_version"),
223 &config->eapol_version) == 0) {
224 if (config->eapol_version < 1 ||
225 config->eapol_version > 2) {
226 wpa_printf(MSG_ERROR, "Invalid EAPOL version (%d)",
227 config->eapol_version);
232 config->ctrl_interface = wpa_config_read_reg_string(
233 hk, TEXT("ctrl_interface"));
236 if (wpa_config_read_global_uuid(config, hk))
238 config->device_name = wpa_config_read_reg_string(
239 hk, TEXT("device_name"));
240 config->manufacturer = wpa_config_read_reg_string(
241 hk, TEXT("manufacturer"));
242 config->model_name = wpa_config_read_reg_string(
243 hk, TEXT("model_name"));
244 config->serial_number = wpa_config_read_reg_string(
245 hk, TEXT("serial_number"));
246 config->device_type = wpa_config_read_reg_string(
247 hk, TEXT("device_type"));
248 if (wpa_config_read_global_os_version(config, hk))
250 #endif /* CONFIG_WPS */
252 return errors ? -1 : 0;
256 static struct wpa_ssid * wpa_config_read_network(HKEY hk, const TCHAR *netw,
262 struct wpa_ssid *ssid;
265 ret = RegOpenKeyEx(hk, netw, 0, KEY_QUERY_VALUE, &nhk);
266 if (ret != ERROR_SUCCESS) {
267 wpa_printf(MSG_DEBUG, "Could not open wpa_supplicant config "
268 "network '" TSTR "'", netw);
272 wpa_printf(MSG_MSGDUMP, "Start of a new network '" TSTR "'", netw);
273 ssid = os_zalloc(sizeof(*ssid));
280 wpa_config_set_network_defaults(ssid);
283 TCHAR name[255], data[1024];
284 DWORD namelen, datalen, type;
287 datalen = sizeof(data);
288 ret = RegEnumValue(nhk, i, name, &namelen, NULL, &type,
289 (LPBYTE) data, &datalen);
291 if (ret == ERROR_NO_MORE_ITEMS)
294 if (ret != ERROR_SUCCESS) {
295 wpa_printf(MSG_ERROR, "RegEnumValue failed: 0x%x",
302 name[namelen] = TEXT('\0');
306 data[datalen] = TEXT('\0');
308 wpa_unicode2ascii_inplace(name);
309 wpa_unicode2ascii_inplace(data);
310 if (wpa_config_set(ssid, (char *) name, (char *) data, 0) < 0)
316 if (ssid->passphrase) {
318 wpa_printf(MSG_ERROR, "Both PSK and passphrase "
319 "configured for network '" TSTR "'.", netw);
322 wpa_config_update_psk(ssid);
325 if ((ssid->key_mgmt & (WPA_KEY_MGMT_PSK | WPA_KEY_MGMT_FT_PSK |
326 WPA_KEY_MGMT_PSK_SHA256)) &&
328 wpa_printf(MSG_ERROR, "WPA-PSK accepted for key management, "
329 "but no PSK configured for network '" TSTR "'.",
334 if ((ssid->group_cipher & WPA_CIPHER_CCMP) &&
335 !(ssid->pairwise_cipher & WPA_CIPHER_CCMP) &&
336 !(ssid->pairwise_cipher & WPA_CIPHER_NONE)) {
337 /* Group cipher cannot be stronger than the pairwise cipher. */
338 wpa_printf(MSG_DEBUG, "Removed CCMP from group cipher "
339 "list since it was not allowed for pairwise "
340 "cipher for network '" TSTR "'.", netw);
341 ssid->group_cipher &= ~WPA_CIPHER_CCMP;
345 wpa_config_free_ssid(ssid);
353 static int wpa_config_read_networks(struct wpa_config *config, HKEY hk)
356 struct wpa_ssid *ssid, *tail = NULL, *head = NULL;
361 ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_ENUMERATE_SUB_KEYS,
363 if (ret != ERROR_SUCCESS) {
364 wpa_printf(MSG_ERROR, "Could not open wpa_supplicant networks "
374 ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL,
377 if (ret == ERROR_NO_MORE_ITEMS)
380 if (ret != ERROR_SUCCESS) {
381 wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x",
388 name[namelen] = '\0';
390 ssid = wpa_config_read_network(nhk, name, i);
392 wpa_printf(MSG_ERROR, "Failed to parse network "
393 "profile '%s'.", name);
403 if (wpa_config_add_prio_network(config, ssid)) {
404 wpa_printf(MSG_ERROR, "Failed to add network profile "
405 "'%s' to priority list.", name);
415 return errors ? -1 : 0;
419 struct wpa_config * wpa_config_read(const char *name)
423 struct wpa_config *config;
427 config = wpa_config_alloc_empty(NULL, NULL);
430 wpa_printf(MSG_DEBUG, "Reading configuration profile '%s'", name);
433 _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);
435 os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);
438 ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_QUERY_VALUE, &hk);
439 if (ret != ERROR_SUCCESS) {
440 wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
441 "configuration registry HKLM\\" TSTR, buf);
446 if (wpa_config_read_global(config, hk))
449 if (wpa_config_read_networks(config, hk))
452 if (wpa_config_read_blobs(config, hk))
455 wpa_config_debug_dump_networks(config);
460 wpa_config_free(config);
468 static int wpa_config_write_reg_dword(HKEY hk, const TCHAR *name, int val,
475 RegDeleteValue(hk, name);
479 ret = RegSetValueEx(hk, name, 0, REG_DWORD, (LPBYTE) &_val,
481 if (ret != ERROR_SUCCESS) {
482 wpa_printf(MSG_ERROR, "WINREG: Failed to set %s=%d: error %d",
483 name, val, (int) GetLastError());
491 static int wpa_config_write_reg_string(HKEY hk, const char *name,
497 _name = wpa_strdup_tchar(name);
502 RegDeleteValue(hk, _name);
507 _val = wpa_strdup_tchar(val);
512 ret = RegSetValueEx(hk, _name, 0, REG_SZ, (BYTE *) _val,
513 (os_strlen(val) + 1) * sizeof(TCHAR));
514 if (ret != ERROR_SUCCESS) {
515 wpa_printf(MSG_ERROR, "WINREG: Failed to set %s='%s': "
516 "error %d", name, val, (int) GetLastError());
528 static int wpa_config_write_global(struct wpa_config *config, HKEY hk)
530 #ifdef CONFIG_CTRL_IFACE
531 wpa_config_write_reg_string(hk, "ctrl_interface",
532 config->ctrl_interface);
533 #endif /* CONFIG_CTRL_IFACE */
535 wpa_config_write_reg_dword(hk, TEXT("eapol_version"),
536 config->eapol_version,
537 DEFAULT_EAPOL_VERSION);
538 wpa_config_write_reg_dword(hk, TEXT("ap_scan"), config->ap_scan,
540 wpa_config_write_reg_dword(hk, TEXT("fast_reauth"),
541 config->fast_reauth, DEFAULT_FAST_REAUTH);
542 wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigPMKLifetime"),
543 config->dot11RSNAConfigPMKLifetime, 0);
544 wpa_config_write_reg_dword(hk,
545 TEXT("dot11RSNAConfigPMKReauthThreshold"),
546 config->dot11RSNAConfigPMKReauthThreshold,
548 wpa_config_write_reg_dword(hk, TEXT("dot11RSNAConfigSATimeout"),
549 config->dot11RSNAConfigSATimeout, 0);
550 wpa_config_write_reg_dword(hk, TEXT("update_config"),
551 config->update_config,
554 if (!is_nil_uuid(config->uuid)) {
556 uuid_bin2str(config->uuid, buf, sizeof(buf));
557 wpa_config_write_reg_string(hk, "uuid", buf);
559 wpa_config_write_reg_string(hk, "device_name", config->device_name);
560 wpa_config_write_reg_string(hk, "manufacturer", config->manufacturer);
561 wpa_config_write_reg_string(hk, "model_name", config->model_name);
562 wpa_config_write_reg_string(hk, "model_number", config->model_number);
563 wpa_config_write_reg_string(hk, "serial_number",
564 config->serial_number);
565 wpa_config_write_reg_string(hk, "device_type", config->device_type);
566 if (WPA_GET_BE32(config->os_version)) {
568 os_snprintf(vbuf, sizeof(vbuf), "%08x",
569 WPA_GET_BE32(config->os_version));
570 wpa_config_write_reg_string(hk, "os_version", vbuf);
572 #endif /* CONFIG_WPS */
578 static int wpa_config_delete_subkeys(HKEY hk, const TCHAR *key)
584 ret = RegOpenKeyEx(hk, key, 0, KEY_ENUMERATE_SUB_KEYS | DELETE, &nhk);
585 if (ret != ERROR_SUCCESS) {
586 wpa_printf(MSG_DEBUG, "WINREG: Could not open key '" TSTR
587 "' for subkey deletion: error 0x%x (%d)", key,
588 (unsigned int) ret, (int) GetLastError());
597 ret = RegEnumKeyEx(nhk, i, name, &namelen, NULL, NULL, NULL,
600 if (ret == ERROR_NO_MORE_ITEMS)
603 if (ret != ERROR_SUCCESS) {
604 wpa_printf(MSG_DEBUG, "RegEnumKeyEx failed: 0x%x (%d)",
605 (unsigned int) ret, (int) GetLastError());
611 name[namelen] = TEXT('\0');
613 ret = RegDeleteKey(nhk, name);
614 if (ret != ERROR_SUCCESS) {
615 wpa_printf(MSG_DEBUG, "RegDeleteKey failed: 0x%x (%d)",
616 (unsigned int) ret, (int) GetLastError());
623 return errors ? -1 : 0;
627 static void write_str(HKEY hk, const char *field, struct wpa_ssid *ssid)
629 char *value = wpa_config_get(ssid, field);
632 wpa_config_write_reg_string(hk, field, value);
637 static void write_int(HKEY hk, const char *field, int value, int def)
642 os_snprintf(val, sizeof(val), "%d", value);
643 wpa_config_write_reg_string(hk, field, val);
647 static void write_bssid(HKEY hk, struct wpa_ssid *ssid)
649 char *value = wpa_config_get(ssid, "bssid");
652 wpa_config_write_reg_string(hk, "bssid", value);
657 static void write_psk(HKEY hk, struct wpa_ssid *ssid)
659 char *value = wpa_config_get(ssid, "psk");
662 wpa_config_write_reg_string(hk, "psk", value);
667 static void write_proto(HKEY hk, struct wpa_ssid *ssid)
671 if (ssid->proto == DEFAULT_PROTO)
674 value = wpa_config_get(ssid, "proto");
678 wpa_config_write_reg_string(hk, "proto", value);
683 static void write_key_mgmt(HKEY hk, struct wpa_ssid *ssid)
687 if (ssid->key_mgmt == DEFAULT_KEY_MGMT)
690 value = wpa_config_get(ssid, "key_mgmt");
694 wpa_config_write_reg_string(hk, "key_mgmt", value);
699 static void write_pairwise(HKEY hk, struct wpa_ssid *ssid)
703 if (ssid->pairwise_cipher == DEFAULT_PAIRWISE)
706 value = wpa_config_get(ssid, "pairwise");
710 wpa_config_write_reg_string(hk, "pairwise", value);
715 static void write_group(HKEY hk, struct wpa_ssid *ssid)
719 if (ssid->group_cipher == DEFAULT_GROUP)
722 value = wpa_config_get(ssid, "group");
726 wpa_config_write_reg_string(hk, "group", value);
731 static void write_auth_alg(HKEY hk, struct wpa_ssid *ssid)
735 if (ssid->auth_alg == 0)
738 value = wpa_config_get(ssid, "auth_alg");
742 wpa_config_write_reg_string(hk, "auth_alg", value);
747 #ifdef IEEE8021X_EAPOL
748 static void write_eap(HKEY hk, struct wpa_ssid *ssid)
752 value = wpa_config_get(ssid, "eap");
757 wpa_config_write_reg_string(hk, "eap", value);
760 #endif /* IEEE8021X_EAPOL */
763 static void write_wep_key(HKEY hk, int idx, struct wpa_ssid *ssid)
765 char field[20], *value;
767 os_snprintf(field, sizeof(field), "wep_key%d", idx);
768 value = wpa_config_get(ssid, field);
770 wpa_config_write_reg_string(hk, field, value);
776 static int wpa_config_write_network(HKEY hk, struct wpa_ssid *ssid, int id)
783 ret = RegOpenKeyEx(hk, TEXT("networks"), 0, KEY_CREATE_SUB_KEY, &nhk);
784 if (ret != ERROR_SUCCESS) {
785 wpa_printf(MSG_DEBUG, "WINREG: Could not open networks key "
786 "for subkey addition: error 0x%x (%d)",
787 (unsigned int) ret, (int) GetLastError());
792 wsprintf(name, L"%04d", id);
794 os_snprintf(name, sizeof(name), "%04d", id);
796 ret = RegCreateKeyEx(nhk, name, 0, NULL, 0, KEY_WRITE, NULL, &netw,
799 if (ret != ERROR_SUCCESS) {
800 wpa_printf(MSG_DEBUG, "WINREG: Could not add network key '%s':"
802 name, (unsigned int) ret, (int) GetLastError());
806 #define STR(t) write_str(netw, #t, ssid)
807 #define INT(t) write_int(netw, #t, ssid->t, 0)
808 #define INTe(t) write_int(netw, #t, ssid->eap.t, 0)
809 #define INT_DEF(t, def) write_int(netw, #t, ssid->t, def)
810 #define INT_DEFe(t, def) write_int(netw, #t, ssid->eap.t, def)
814 write_bssid(netw, ssid);
815 write_psk(netw, ssid);
816 write_proto(netw, ssid);
817 write_key_mgmt(netw, ssid);
818 write_pairwise(netw, ssid);
819 write_group(netw, ssid);
820 write_auth_alg(netw, ssid);
821 #ifdef IEEE8021X_EAPOL
822 write_eap(netw, ssid);
824 STR(anonymous_identity);
830 STR(private_key_passwd);
833 STR(altsubject_match);
838 STR(private_key2_passwd);
841 STR(altsubject_match2);
857 INT_DEF(eapol_flags, DEFAULT_EAPOL_FLAGS);
858 #endif /* IEEE8021X_EAPOL */
859 for (i = 0; i < 4; i++)
860 write_wep_key(netw, i, ssid);
863 #ifdef IEEE8021X_EAPOL
864 INT_DEF(eap_workaround, DEFAULT_EAP_WORKAROUND);
866 INT_DEFe(fragment_size, DEFAULT_FRAGMENT_SIZE);
867 #endif /* IEEE8021X_EAPOL */
869 INT(proactive_key_caching);
872 #ifdef CONFIG_IEEE80211W
874 #endif /* CONFIG_IEEE80211W */
883 return errors ? -1 : 0;
887 static int wpa_config_write_blob(HKEY hk, struct wpa_config_blob *blob)
893 ret = RegCreateKeyEx(hk, TEXT("blobs"), 0, NULL, 0, KEY_WRITE, NULL,
895 if (ret != ERROR_SUCCESS) {
896 wpa_printf(MSG_DEBUG, "WINREG: Could not add blobs key: "
898 (unsigned int) ret, (int) GetLastError());
902 name = wpa_strdup_tchar(blob->name);
903 ret = RegSetValueEx(bhk, name, 0, REG_BINARY, blob->data,
905 if (ret != ERROR_SUCCESS) {
906 wpa_printf(MSG_ERROR, "WINREG: Failed to set blob %s': "
907 "error 0x%x (%d)", blob->name, (unsigned int) ret,
908 (int) GetLastError());
921 int wpa_config_write(const char *name, struct wpa_config *config)
927 struct wpa_ssid *ssid;
928 struct wpa_config_blob *blob;
931 wpa_printf(MSG_DEBUG, "Writing configuration file '%s'", name);
934 _snwprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%S"), name);
936 os_snprintf(buf, 256, WPA_KEY_PREFIX TEXT("\\configs\\%s"), name);
939 ret = RegOpenKeyEx(WPA_KEY_ROOT, buf, 0, KEY_SET_VALUE | DELETE, &hk);
940 if (ret != ERROR_SUCCESS) {
941 wpa_printf(MSG_ERROR, "Could not open wpa_supplicant "
942 "configuration registry %s: error %d", buf,
943 (int) GetLastError());
947 if (wpa_config_write_global(config, hk)) {
948 wpa_printf(MSG_ERROR, "Failed to write global configuration "
953 wpa_config_delete_subkeys(hk, TEXT("networks"));
954 for (ssid = config->ssid, id = 0; ssid; ssid = ssid->next, id++) {
955 if (ssid->key_mgmt == WPA_KEY_MGMT_WPS)
956 continue; /* do not save temporary WPS networks */
957 if (wpa_config_write_network(hk, ssid, id))
961 RegDeleteKey(hk, TEXT("blobs"));
962 for (blob = config->blobs; blob; blob = blob->next) {
963 if (wpa_config_write_blob(hk, blob))
969 wpa_printf(MSG_DEBUG, "Configuration '%s' written %ssuccessfully",
970 name, errors ? "un" : "");
971 return errors ? -1 : 0;