From 8e9a2fe05d41a460f441f46653b6014010c9b434 Mon Sep 17 00:00:00 2001 From: Andrew Zaborowski Date: Wed, 16 Sep 2020 11:17:54 +0200 Subject: [PATCH] treewide: Use l_settings_{set,get}_bytes --- src/ap.c | 14 ++++---------- src/eap-mschapv2.c | 39 ++++++++++++--------------------------- src/eap-wsc.c | 23 +++++++++-------------- src/hotspot.c | 26 ++++++++++++++------------ src/network.c | 30 +++++++++++++++--------------- src/wsc.c | 13 ++++--------- unit/test-wsc.c | 36 +++++++++++------------------------- 7 files changed, 69 insertions(+), 112 deletions(-) diff --git a/src/ap.c b/src/ap.c index dfa12444..b87f49a3 100644 --- a/src/ap.c +++ b/src/ap.c @@ -773,21 +773,15 @@ static void ap_start_eap_wsc(struct sta_state *sta) */ 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(); l_settings_set_string(sta->wsc_settings, "Security", "EAP-Method", "WSC-R"); l_settings_set_string(sta->wsc_settings, "WSC", "EnrolleeMAC", util_address_to_string(sta->addr)); - l_settings_set_string(sta->wsc_settings, "WSC", "UUID-R", - uuid_r_str); - l_settings_set_string(sta->wsc_settings, "WSC", "UUID-E", - uuid_e_str); + l_settings_set_bytes(sta->wsc_settings, "WSC", "UUID-R", + ap->wsc_uuid_r, 16); + l_settings_set_bytes(sta->wsc_settings, "WSC", "UUID-E", + sta->wsc_uuid_e, 16); l_settings_set_uint(sta->wsc_settings, "WSC", "RFBand", WSC_RF_BAND_2_4_GHZ); l_settings_set_uint(sta->wsc_settings, "WSC", "ConfigurationMethods", diff --git a/src/eap-mschapv2.c b/src/eap-mschapv2.c index 49b5335c..7eac260b 100644 --- a/src/eap-mschapv2.c +++ b/src/eap-mschapv2.c @@ -428,13 +428,14 @@ static int eap_mschapv2_check_settings(struct l_settings *settings, const char *prefix, 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 *, identity); const struct eap_secret_info *secret; char setting[64], setting2[64]; uint8_t hash[16]; int r = 0; + size_t hash_len; snprintf(setting, sizeof(setting), "%sIdentity", prefix); 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); snprintf(setting, sizeof(setting), "%sPassword-Hash", prefix); - password_hash = l_settings_get_value(settings, "Security", - setting); + password_hash = l_settings_get_bytes(settings, "Security", + setting, &hash_len); if (password && password_hash) { 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) { - unsigned char *tmp; - size_t len; - - tmp = l_util_from_hexstring(password_hash, &len); - if (tmp) - explicit_bzero(tmp, len); - - l_free(tmp); - - if (!tmp || len != 16) { + if (hash_len != 16) { l_error("Property %s is not a 16-byte hexstring", setting); return -EINVAL; @@ -534,22 +526,15 @@ static bool eap_mschapv2_load_settings(struct eap_state *eap, set_password_from_string(state, password); explicit_bzero(password, strlen(password)); } else { - unsigned char *tmp; - size_t len; - const char *hash_str; + size_t hash_len; + uint8_t *hash; snprintf(setting, sizeof(setting), "%sPassword-Hash", prefix); - hash_str = l_settings_get_value(settings, "Security", setting); - if (!hash_str) - goto error; - - tmp = l_util_from_hexstring(hash_str, &len); - if (!tmp) - goto error; - - memcpy(state->password_hash, tmp, len); - explicit_bzero(tmp, len); - l_free(tmp); + hash = l_settings_get_bytes(settings, "Security", setting, + &hash_len); + memcpy(state->password_hash, hash, 16); + explicit_bzero(hash, 16); + l_free(hash); } eap_set_data(eap, state); diff --git a/src/eap-wsc.c b/src/eap-wsc.c index 5b87924a..817cf82f 100644 --- a/src/eap-wsc.c +++ b/src/eap-wsc.c @@ -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, uint8_t *to, size_t len) { - const char *v; - size_t decoded_len; - unsigned char *decoded; + uint8_t *v; + size_t v_len; - v = l_settings_get_value(settings, "WSC", key); + v = l_settings_get_bytes(settings, "WSC", key, &v_len); if (!v) return false; - decoded = l_util_from_hexstring(v, &decoded_len); - if (!decoded) - return false; - - if (decoded_len != len) { - explicit_bzero(decoded, decoded_len); - l_free(decoded); + if (v_len != len) { + explicit_bzero(v, v_len); + l_free(v); return false; } - memcpy(to, decoded, len); - explicit_bzero(decoded, decoded_len); - l_free(decoded); + memcpy(to, v, len); + explicit_bzero(v, v_len); + l_free(v); return true; } diff --git a/src/hotspot.c b/src/hotspot.c index 0db42eb0..ea11d59a 100644 --- a/src/hotspot.c +++ b/src/hotspot.c @@ -319,7 +319,8 @@ static struct hs20_config *hs20_config_new(struct l_settings *settings, struct hs20_config *config; char *hessid_str; char **nai_realms = NULL; - const char *rc_str; + size_t rc_len; + uint8_t *rc; char *name; 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", "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", &autoconnect)) @@ -337,7 +339,7 @@ static struct hs20_config *hs20_config_new(struct l_settings *settings, 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); goto free_values; } @@ -357,22 +359,21 @@ static struct hs20_config *hs20_config_new(struct l_settings *settings, if (nai_realms) config->nai_realms = nai_realms; - if (rc_str) { - config->rc = l_util_from_hexstring(rc_str, - &config->rc_len); + if (rc) { /* * WiFi Alliance Hotspot 2.0 Spec - Section 3.1.4 * * "The Consortium OI field is 3 or 5-octet field set to a value * of a roaming consortium OI" */ - if (config->rc && config->rc_len != 3 && - config->rc_len != 5) { - l_warn("invalid RoamingConsortium length %zu", - config->rc_len); - l_free(config->rc); - config->rc = NULL; + if (rc && rc_len != 3 && rc_len != 5) { + l_warn("invalid RoamingConsortium length %zu", rc_len); + l_free(rc); + rc = NULL; } + + config->rc = rc; + config->rc_len = rc_len; } config->super.is_autoconnectable = autoconnect; @@ -392,6 +393,7 @@ free_values: l_strv_free(nai_realms); l_free(hessid_str); l_free(name); + l_free(rc); return NULL; } diff --git a/src/network.c b/src/network.c index a7d59ca0..3e856aa1 100644 --- a/src/network.c +++ b/src/network.c @@ -374,16 +374,18 @@ static int network_load_psk(struct network *network, bool need_passphrase) { const char *ssid = network_get_ssid(network); enum security security = network_get_security(network); - size_t len; - const char *psk = l_settings_get_value(network->settings, - "Security", "PreSharedKey"); + size_t psk_len; + uint8_t *psk = l_settings_get_bytes(network->settings, "Security", + "PreSharedKey", &psk_len); char *passphrase = l_settings_get_string(network->settings, "Security", "Passphrase"); int r; /* 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; + } network_reset_passphrase(network); network_reset_psk(network); @@ -392,11 +394,10 @@ static int network_load_psk(struct network *network, bool need_passphrase) if (psk) { char *path; - network->psk = l_util_from_hexstring(psk, &len); - if (network->psk && len == 32) + if (psk_len == 32) { + network->psk = psk; return 0; - - network_reset_psk(network); + } path = storage_get_network_file_path(security, ssid); 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); if (network->psk) { - char *hex = l_util_hexstring(network->psk, 32); - l_settings_set_value(network->settings, "Security", - "PreSharedKey", hex); + l_settings_set_bytes(network->settings, "Security", + "PreSharedKey", + network->psk, 32); if (fs_settings) - l_settings_set_value(fs_settings, "Security", - "PreSharedKey", hex); - - l_free(hex); + l_settings_set_bytes(fs_settings, "Security", + "PreSharedKey", + network->psk, 32); } if (network->passphrase) { diff --git a/src/wsc.c b/src/wsc.c index fa91f459..66ddb8bf 100644 --- a/src/wsc.c +++ b/src/wsc.c @@ -536,15 +536,10 @@ static void wsc_store_credentials(struct wsc_credentials_info *creds, l_debug("Storing credential for '%s(%s)'", ssid, security_to_str(security)); - if (security == SECURITY_PSK) { - char *hex = l_util_hexstring(creds[i].psk, - sizeof(creds[i].psk)); - - l_settings_set_value(settings, "Security", - "PreSharedKey", hex); - explicit_bzero(hex, strlen(hex)); - l_free(hex); - } + if (security == SECURITY_PSK) + l_settings_set_bytes(settings, "Security", + "PreSharedKey", creds[i].psk, + sizeof(creds[i].psk)); storage_network_sync(security, ssid, settings); l_settings_free(settings); diff --git a/unit/test-wsc.c b/unit/test-wsc.c index 2063244c..f2e9b864 100644 --- a/unit/test-wsc.c +++ b/unit/test-wsc.c @@ -2005,7 +2005,6 @@ static void wsc_test_pbc_handshake(const void *data) struct verify_data verify; struct handshake_state *hs; struct eapol_sm *sm; - char *hex; struct l_settings *settings; eap_init(); @@ -2037,16 +2036,11 @@ static void wsc_test_pbc_handshake(const void *data) "0-00000000-0"); l_settings_set_string(settings, "WSC", "EnrolleeMAC", util_address_to_string(sta_address)); - - hex = l_util_hexstring(m1_data_2.expected.enrollee_nonce, 16); - l_settings_set_string(settings, "WSC", "EnrolleeNonce", hex); - l_free(hex); - - hex = l_util_hexstring(m1_data_2.private_key, - m1_data_2.private_key_size); - l_settings_set_string(settings, "WSC", "PrivateKey", hex); - l_free(hex); - + l_settings_set_bytes(settings, "WSC", "EnrolleeNonce", + m1_data_2.expected.enrollee_nonce, 16); + l_settings_set_bytes(settings, "WSC", "PrivateKey", + m1_data_2.private_key, + m1_data_2.private_key_size); l_settings_set_string(settings, "WSC", "E-SNonce1", "fdbb480ee6f572f3591cc3b364f2185b"); 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 handshake_state *hs; struct eapol_sm *sm; - char *hex; struct l_settings *settings; eap_init(); @@ -2143,16 +2136,11 @@ static void wsc_test_retransmission_no_fragmentation(const void *data) "0-00000000-0"); l_settings_set_string(settings, "WSC", "EnrolleeMAC", util_address_to_string(sta_address)); - - hex = l_util_hexstring(m1_data_2.expected.enrollee_nonce, 16); - l_settings_set_string(settings, "WSC", "EnrolleeNonce", hex); - l_free(hex); - - hex = l_util_hexstring(m1_data_2.private_key, - m1_data_2.private_key_size); - l_settings_set_string(settings, "WSC", "PrivateKey", hex); - l_free(hex); - + l_settings_set_bytes(settings, "WSC", "EnrolleeNonce", + m1_data_2.expected.enrollee_nonce, 16); + l_settings_set_bytes(settings, "WSC", "PrivateKey", + m1_data_2.private_key, + m1_data_2.private_key_size); l_settings_set_string(settings, "WSC", "E-SNonce1", "fdbb480ee6f572f3591cc3b364f2185b"); 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, }; uint8_t uuid_e[16]; - L_AUTO_FREE_VAR(char *, uuid_e_str) = NULL; char ssid_str[33]; 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); 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)); l_settings_set_string(ap_8021x_settings, "WSC", "EnrolleeMAC", 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) { l_settings_set_string(ap_8021x_settings, "WSC",