3
0
mirror of https://git.kernel.org/pub/scm/network/wireless/iwd.git synced 2024-11-26 02:19:26 +01:00

treewide: Use l_settings_{set,get}_bytes

This commit is contained in:
Andrew Zaborowski 2020-09-16 11:17:54 +02:00 committed by Denis Kenzior
parent 6b99b33974
commit 8e9a2fe05d
7 changed files with 69 additions and 112 deletions

View File

@ -773,21 +773,15 @@ static void ap_start_eap_wsc(struct sta_state *sta)
*/ */
bool wait_for_eapol_start = !sta->wsc_v2; bool wait_for_eapol_start = !sta->wsc_v2;
L_AUTO_FREE_VAR(char *, uuid_r_str) = NULL;
L_AUTO_FREE_VAR(char *, uuid_e_str) = NULL;
uuid_r_str = l_util_hexstring(ap->wsc_uuid_r, 16);
uuid_e_str = l_util_hexstring(sta->wsc_uuid_e, 16);
sta->wsc_settings = l_settings_new(); sta->wsc_settings = l_settings_new();
l_settings_set_string(sta->wsc_settings, "Security", "EAP-Method", l_settings_set_string(sta->wsc_settings, "Security", "EAP-Method",
"WSC-R"); "WSC-R");
l_settings_set_string(sta->wsc_settings, "WSC", "EnrolleeMAC", l_settings_set_string(sta->wsc_settings, "WSC", "EnrolleeMAC",
util_address_to_string(sta->addr)); util_address_to_string(sta->addr));
l_settings_set_string(sta->wsc_settings, "WSC", "UUID-R", l_settings_set_bytes(sta->wsc_settings, "WSC", "UUID-R",
uuid_r_str); ap->wsc_uuid_r, 16);
l_settings_set_string(sta->wsc_settings, "WSC", "UUID-E", l_settings_set_bytes(sta->wsc_settings, "WSC", "UUID-E",
uuid_e_str); sta->wsc_uuid_e, 16);
l_settings_set_uint(sta->wsc_settings, "WSC", "RFBand", l_settings_set_uint(sta->wsc_settings, "WSC", "RFBand",
WSC_RF_BAND_2_4_GHZ); WSC_RF_BAND_2_4_GHZ);
l_settings_set_uint(sta->wsc_settings, "WSC", "ConfigurationMethods", l_settings_set_uint(sta->wsc_settings, "WSC", "ConfigurationMethods",

View File

@ -428,13 +428,14 @@ static int eap_mschapv2_check_settings(struct l_settings *settings,
const char *prefix, const char *prefix,
struct l_queue **out_missing) struct l_queue **out_missing)
{ {
const char *password_hash; L_AUTO_FREE_VAR(uint8_t *, password_hash) = NULL;
L_AUTO_FREE_VAR(char *, password) = NULL; L_AUTO_FREE_VAR(char *, password) = NULL;
L_AUTO_FREE_VAR(char *, identity); L_AUTO_FREE_VAR(char *, identity);
const struct eap_secret_info *secret; const struct eap_secret_info *secret;
char setting[64], setting2[64]; char setting[64], setting2[64];
uint8_t hash[16]; uint8_t hash[16];
int r = 0; int r = 0;
size_t hash_len;
snprintf(setting, sizeof(setting), "%sIdentity", prefix); snprintf(setting, sizeof(setting), "%sIdentity", prefix);
identity = l_settings_get_string(settings, "Security", setting); identity = l_settings_get_string(settings, "Security", setting);
@ -460,8 +461,8 @@ static int eap_mschapv2_check_settings(struct l_settings *settings,
password = l_settings_get_string(settings, "Security", setting2); password = l_settings_get_string(settings, "Security", setting2);
snprintf(setting, sizeof(setting), "%sPassword-Hash", prefix); snprintf(setting, sizeof(setting), "%sPassword-Hash", prefix);
password_hash = l_settings_get_value(settings, "Security", password_hash = l_settings_get_bytes(settings, "Security",
setting); setting, &hash_len);
if (password && password_hash) { if (password && password_hash) {
l_error("Exactly one of (%s, %s) must be present", l_error("Exactly one of (%s, %s) must be present",
@ -471,16 +472,7 @@ static int eap_mschapv2_check_settings(struct l_settings *settings,
} }
if (password_hash) { if (password_hash) {
unsigned char *tmp; if (hash_len != 16) {
size_t len;
tmp = l_util_from_hexstring(password_hash, &len);
if (tmp)
explicit_bzero(tmp, len);
l_free(tmp);
if (!tmp || len != 16) {
l_error("Property %s is not a 16-byte hexstring", l_error("Property %s is not a 16-byte hexstring",
setting); setting);
return -EINVAL; return -EINVAL;
@ -534,22 +526,15 @@ static bool eap_mschapv2_load_settings(struct eap_state *eap,
set_password_from_string(state, password); set_password_from_string(state, password);
explicit_bzero(password, strlen(password)); explicit_bzero(password, strlen(password));
} else { } else {
unsigned char *tmp; size_t hash_len;
size_t len; uint8_t *hash;
const char *hash_str;
snprintf(setting, sizeof(setting), "%sPassword-Hash", prefix); snprintf(setting, sizeof(setting), "%sPassword-Hash", prefix);
hash_str = l_settings_get_value(settings, "Security", setting); hash = l_settings_get_bytes(settings, "Security", setting,
if (!hash_str) &hash_len);
goto error; memcpy(state->password_hash, hash, 16);
explicit_bzero(hash, 16);
tmp = l_util_from_hexstring(hash_str, &len); l_free(hash);
if (!tmp)
goto error;
memcpy(state->password_hash, tmp, len);
explicit_bzero(tmp, len);
l_free(tmp);
} }
eap_set_data(eap, state); eap_set_data(eap, state);

View File

@ -1718,27 +1718,22 @@ static void eap_wsc_handle_retransmit(struct eap_state *eap,
static bool load_hexencoded(struct l_settings *settings, const char *key, static bool load_hexencoded(struct l_settings *settings, const char *key,
uint8_t *to, size_t len) uint8_t *to, size_t len)
{ {
const char *v; uint8_t *v;
size_t decoded_len; size_t v_len;
unsigned char *decoded;
v = l_settings_get_value(settings, "WSC", key); v = l_settings_get_bytes(settings, "WSC", key, &v_len);
if (!v) if (!v)
return false; return false;
decoded = l_util_from_hexstring(v, &decoded_len); if (v_len != len) {
if (!decoded) explicit_bzero(v, v_len);
return false; l_free(v);
if (decoded_len != len) {
explicit_bzero(decoded, decoded_len);
l_free(decoded);
return false; return false;
} }
memcpy(to, decoded, len); memcpy(to, v, len);
explicit_bzero(decoded, decoded_len); explicit_bzero(v, v_len);
l_free(decoded); l_free(v);
return true; return true;
} }

View File

@ -319,7 +319,8 @@ static struct hs20_config *hs20_config_new(struct l_settings *settings,
struct hs20_config *config; struct hs20_config *config;
char *hessid_str; char *hessid_str;
char **nai_realms = NULL; char **nai_realms = NULL;
const char *rc_str; size_t rc_len;
uint8_t *rc;
char *name; char *name;
bool autoconnect; bool autoconnect;
@ -329,7 +330,8 @@ static struct hs20_config *hs20_config_new(struct l_settings *settings,
nai_realms = l_settings_get_string_list(settings, "Hotspot", nai_realms = l_settings_get_string_list(settings, "Hotspot",
"NAIRealmNames", ','); "NAIRealmNames", ',');
rc_str = l_settings_get_value(settings, "Hotspot", "RoamingConsortium"); rc = l_settings_get_bytes(settings, "Hotspot", "RoamingConsortium",
&rc_len);
if (!l_settings_get_bool(settings, "Settings", "AutoConnect", if (!l_settings_get_bool(settings, "Settings", "AutoConnect",
&autoconnect)) &autoconnect))
@ -337,7 +339,7 @@ static struct hs20_config *hs20_config_new(struct l_settings *settings,
name = l_settings_get_string(settings, "Hotspot", "Name"); name = l_settings_get_string(settings, "Hotspot", "Name");
if ((!hessid_str && !nai_realms && !rc_str) || !name) { if ((!hessid_str && !nai_realms && !rc) || !name) {
l_error("Could not parse hotspot config %s", filename); l_error("Could not parse hotspot config %s", filename);
goto free_values; goto free_values;
} }
@ -357,22 +359,21 @@ static struct hs20_config *hs20_config_new(struct l_settings *settings,
if (nai_realms) if (nai_realms)
config->nai_realms = nai_realms; config->nai_realms = nai_realms;
if (rc_str) { if (rc) {
config->rc = l_util_from_hexstring(rc_str,
&config->rc_len);
/* /*
* WiFi Alliance Hotspot 2.0 Spec - Section 3.1.4 * WiFi Alliance Hotspot 2.0 Spec - Section 3.1.4
* *
* "The Consortium OI field is 3 or 5-octet field set to a value * "The Consortium OI field is 3 or 5-octet field set to a value
* of a roaming consortium OI" * of a roaming consortium OI"
*/ */
if (config->rc && config->rc_len != 3 && if (rc && rc_len != 3 && rc_len != 5) {
config->rc_len != 5) { l_warn("invalid RoamingConsortium length %zu", rc_len);
l_warn("invalid RoamingConsortium length %zu", l_free(rc);
config->rc_len); rc = NULL;
l_free(config->rc);
config->rc = NULL;
} }
config->rc = rc;
config->rc_len = rc_len;
} }
config->super.is_autoconnectable = autoconnect; config->super.is_autoconnectable = autoconnect;
@ -392,6 +393,7 @@ free_values:
l_strv_free(nai_realms); l_strv_free(nai_realms);
l_free(hessid_str); l_free(hessid_str);
l_free(name); l_free(name);
l_free(rc);
return NULL; return NULL;
} }

View File

@ -374,16 +374,18 @@ static int network_load_psk(struct network *network, bool need_passphrase)
{ {
const char *ssid = network_get_ssid(network); const char *ssid = network_get_ssid(network);
enum security security = network_get_security(network); enum security security = network_get_security(network);
size_t len; size_t psk_len;
const char *psk = l_settings_get_value(network->settings, uint8_t *psk = l_settings_get_bytes(network->settings, "Security",
"Security", "PreSharedKey"); "PreSharedKey", &psk_len);
char *passphrase = l_settings_get_string(network->settings, char *passphrase = l_settings_get_string(network->settings,
"Security", "Passphrase"); "Security", "Passphrase");
int r; int r;
/* PSK can be generated from the passphrase but not the other way */ /* PSK can be generated from the passphrase but not the other way */
if ((!psk || need_passphrase) && !passphrase) if ((!psk || need_passphrase) && !passphrase) {
l_free(psk);
return -ENOKEY; return -ENOKEY;
}
network_reset_passphrase(network); network_reset_passphrase(network);
network_reset_psk(network); network_reset_psk(network);
@ -392,11 +394,10 @@ static int network_load_psk(struct network *network, bool need_passphrase)
if (psk) { if (psk) {
char *path; char *path;
network->psk = l_util_from_hexstring(psk, &len); if (psk_len == 32) {
if (network->psk && len == 32) network->psk = psk;
return 0; return 0;
}
network_reset_psk(network);
path = storage_get_network_file_path(security, ssid); path = storage_get_network_file_path(security, ssid);
l_error("%s: invalid PreSharedKey format", path); l_error("%s: invalid PreSharedKey format", path);
@ -439,15 +440,14 @@ void network_sync_psk(struct network *network)
fs_settings = storage_network_open(SECURITY_PSK, ssid); fs_settings = storage_network_open(SECURITY_PSK, ssid);
if (network->psk) { if (network->psk) {
char *hex = l_util_hexstring(network->psk, 32); l_settings_set_bytes(network->settings, "Security",
l_settings_set_value(network->settings, "Security", "PreSharedKey",
"PreSharedKey", hex); network->psk, 32);
if (fs_settings) if (fs_settings)
l_settings_set_value(fs_settings, "Security", l_settings_set_bytes(fs_settings, "Security",
"PreSharedKey", hex); "PreSharedKey",
network->psk, 32);
l_free(hex);
} }
if (network->passphrase) { if (network->passphrase) {

View File

@ -536,16 +536,11 @@ static void wsc_store_credentials(struct wsc_credentials_info *creds,
l_debug("Storing credential for '%s(%s)'", ssid, l_debug("Storing credential for '%s(%s)'", ssid,
security_to_str(security)); security_to_str(security));
if (security == SECURITY_PSK) { if (security == SECURITY_PSK)
char *hex = l_util_hexstring(creds[i].psk, l_settings_set_bytes(settings, "Security",
"PreSharedKey", creds[i].psk,
sizeof(creds[i].psk)); sizeof(creds[i].psk));
l_settings_set_value(settings, "Security",
"PreSharedKey", hex);
explicit_bzero(hex, strlen(hex));
l_free(hex);
}
storage_network_sync(security, ssid, settings); storage_network_sync(security, ssid, settings);
l_settings_free(settings); l_settings_free(settings);
} }

View File

@ -2005,7 +2005,6 @@ static void wsc_test_pbc_handshake(const void *data)
struct verify_data verify; struct verify_data verify;
struct handshake_state *hs; struct handshake_state *hs;
struct eapol_sm *sm; struct eapol_sm *sm;
char *hex;
struct l_settings *settings; struct l_settings *settings;
eap_init(); eap_init();
@ -2037,16 +2036,11 @@ static void wsc_test_pbc_handshake(const void *data)
"0-00000000-0"); "0-00000000-0");
l_settings_set_string(settings, "WSC", "EnrolleeMAC", l_settings_set_string(settings, "WSC", "EnrolleeMAC",
util_address_to_string(sta_address)); util_address_to_string(sta_address));
l_settings_set_bytes(settings, "WSC", "EnrolleeNonce",
hex = l_util_hexstring(m1_data_2.expected.enrollee_nonce, 16); m1_data_2.expected.enrollee_nonce, 16);
l_settings_set_string(settings, "WSC", "EnrolleeNonce", hex); l_settings_set_bytes(settings, "WSC", "PrivateKey",
l_free(hex); m1_data_2.private_key,
hex = l_util_hexstring(m1_data_2.private_key,
m1_data_2.private_key_size); m1_data_2.private_key_size);
l_settings_set_string(settings, "WSC", "PrivateKey", hex);
l_free(hex);
l_settings_set_string(settings, "WSC", "E-SNonce1", l_settings_set_string(settings, "WSC", "E-SNonce1",
"fdbb480ee6f572f3591cc3b364f2185b"); "fdbb480ee6f572f3591cc3b364f2185b");
l_settings_set_string(settings, "WSC", "E-SNonce2", l_settings_set_string(settings, "WSC", "E-SNonce2",
@ -2111,7 +2105,6 @@ static void wsc_test_retransmission_no_fragmentation(const void *data)
struct verify_data verify; struct verify_data verify;
struct handshake_state *hs; struct handshake_state *hs;
struct eapol_sm *sm; struct eapol_sm *sm;
char *hex;
struct l_settings *settings; struct l_settings *settings;
eap_init(); eap_init();
@ -2143,16 +2136,11 @@ static void wsc_test_retransmission_no_fragmentation(const void *data)
"0-00000000-0"); "0-00000000-0");
l_settings_set_string(settings, "WSC", "EnrolleeMAC", l_settings_set_string(settings, "WSC", "EnrolleeMAC",
util_address_to_string(sta_address)); util_address_to_string(sta_address));
l_settings_set_bytes(settings, "WSC", "EnrolleeNonce",
hex = l_util_hexstring(m1_data_2.expected.enrollee_nonce, 16); m1_data_2.expected.enrollee_nonce, 16);
l_settings_set_string(settings, "WSC", "EnrolleeNonce", hex); l_settings_set_bytes(settings, "WSC", "PrivateKey",
l_free(hex); m1_data_2.private_key,
hex = l_util_hexstring(m1_data_2.private_key,
m1_data_2.private_key_size); m1_data_2.private_key_size);
l_settings_set_string(settings, "WSC", "PrivateKey", hex);
l_free(hex);
l_settings_set_string(settings, "WSC", "E-SNonce1", l_settings_set_string(settings, "WSC", "E-SNonce1",
"fdbb480ee6f572f3591cc3b364f2185b"); "fdbb480ee6f572f3591cc3b364f2185b");
l_settings_set_string(settings, "WSC", "E-SNonce2", l_settings_set_string(settings, "WSC", "E-SNonce2",
@ -2479,11 +2467,9 @@ static void wsc_r_test_pbc_handshake(const void *data)
.expected_creds = *expected_creds, .expected_creds = *expected_creds,
}; };
uint8_t uuid_e[16]; uint8_t uuid_e[16];
L_AUTO_FREE_VAR(char *, uuid_e_str) = NULL;
char ssid_str[33]; char ssid_str[33];
wsc_uuid_from_addr(s.sta_address, uuid_e); wsc_uuid_from_addr(s.sta_address, uuid_e);
uuid_e_str = l_util_hexstring(uuid_e, 16);
memcpy(wsc_data.expected_creds.addr, s.sta_address, 6); memcpy(wsc_data.expected_creds.addr, s.sta_address, 6);
memset(wsc_data.expected_creds.ssid + expected_creds->ssid_len, 0, memset(wsc_data.expected_creds.ssid + expected_creds->ssid_len, 0,
@ -2500,7 +2486,7 @@ static void wsc_r_test_pbc_handshake(const void *data)
strlen(ap_8021x_str)); strlen(ap_8021x_str));
l_settings_set_string(ap_8021x_settings, "WSC", "EnrolleeMAC", l_settings_set_string(ap_8021x_settings, "WSC", "EnrolleeMAC",
util_address_to_string(s.sta_address)); util_address_to_string(s.sta_address));
l_settings_set_string(ap_8021x_settings, "WSC", "UUID-E", uuid_e_str); l_settings_set_bytes(ap_8021x_settings, "WSC", "UUID-E", uuid_e, 16);
if (expected_creds->auth_type == WSC_AUTHENTICATION_TYPE_WPA2_PERSONAL) { if (expected_creds->auth_type == WSC_AUTHENTICATION_TYPE_WPA2_PERSONAL) {
l_settings_set_string(ap_8021x_settings, "WSC", l_settings_set_string(ap_8021x_settings, "WSC",