From e754f3bee4a2efdef36bf8e1bc9f3ec9e4b627e4 Mon Sep 17 00:00:00 2001 From: Vibhav Pant Date: Wed, 13 Sep 2023 18:53:36 +0530 Subject: [PATCH] Unify all code for generating random data into functions in utils --- .../platform/implementation/linux/utils.cc | 51 ++++++++++++++ .../platform/implementation/linux/utils.h | 4 ++ .../implementation/linux/utils_test.cc | 21 +++++- .../implementation/linux/wifi_hotspot.cc | 66 ++++++------------- .../implementation/linux/wifi_medium.cc | 31 +++------ 5 files changed, 106 insertions(+), 67 deletions(-) diff --git a/internal/platform/implementation/linux/utils.cc b/internal/platform/implementation/linux/utils.cc index c3c196cc..9de6ebd2 100644 --- a/internal/platform/implementation/linux/utils.cc +++ b/internal/platform/implementation/linux/utils.cc @@ -12,9 +12,13 @@ // See the License for the specific language governing permissions and // limitations under the License. +#include + #include +#include "absl/strings/str_cat.h" #include "internal/platform/implementation/linux/utils.h" +#include "internal/platform/logging.h" namespace nearby { namespace linux { @@ -47,5 +51,52 @@ std::optional UuidFromString(const std::string &uuid_str) { return Uuid(uuid.qwords[0], uuid.qwords[1]); } + +std::optional NewUuidStr() { + sd_id128_t id; + char id_cstr[SD_ID128_UUID_STRING_MAX]; + if (auto ret = sd_id128_randomize(&id); ret < 0) { + NEARBY_LOGS(ERROR) << __func__ << ": could not generate a random UUID: " + << std::strerror(ret); + return std::nullopt; + } + + return std::string(sd_id128_to_uuid_string(id, id_cstr)); +} + +std::string RandString(std::string allowed_chars, size_t length) { + thread_local static std::random_device device{}; + + std::mt19937 gen{device()}; + std::uniform_int_distribution dist(0, allowed_chars.length() - 1); + + std::string s; + s.reserve(length); + + for (auto i = 0; i < length; i++) { + s += allowed_chars[dist(gen)]; + } + + return s; +} + +std::string RandSSID() { + std::string allowed_chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789"; + + return absl::StrCat("DIRECT-", RandString(allowed_chars, 25)); +} + +std::string RandWPAPassphrase() { + std::string allowed_chars = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz" + "0123456789" + "!\"#$%&'()*+,-./[\\]^_`~{|}"; + + return RandString(allowed_chars, 63); +} } // namespace linux } // namespace nearby diff --git a/internal/platform/implementation/linux/utils.h b/internal/platform/implementation/linux/utils.h index bf9b1b7e..3f251f4a 100644 --- a/internal/platform/implementation/linux/utils.h +++ b/internal/platform/implementation/linux/utils.h @@ -27,6 +27,10 @@ namespace nearby { namespace linux { std::optional UuidFromString(const std::string &uuid_str); +std::optional NewUuidStr(); + +std::string RandSSID(); +std::string RandWPAPassphrase(); } // namespace linux } // namespace nearby diff --git a/internal/platform/implementation/linux/utils_test.cc b/internal/platform/implementation/linux/utils_test.cc index a298b843..9e8e6529 100644 --- a/internal/platform/implementation/linux/utils_test.cc +++ b/internal/platform/implementation/linux/utils_test.cc @@ -28,9 +28,28 @@ TEST(UtilsTests, UuidFromStringRoundTrip) { auto nearby_uuid = UuidFromString(input); EXPECT_TRUE(nearby_uuid.has_value()); - EXPECT_EQ(absl::AsciiStrToLower(std::string{*nearby_uuid}), "b5209043-f493-4b38-8c34-810aa3cd1407"); } + +TEST(UtilsTests, GenNewUuid) { + auto uuid_str = NewUuidStr(); + EXPECT_TRUE(uuid_str.has_value()); + auto uuid = UuidFromString(*uuid_str); + EXPECT_TRUE(uuid.has_value()); + EXPECT_EQ(absl::AsciiStrToLower(std::string{*uuid}), + *uuid_str); +} + +TEST(UtilsTests, GenRandSSID) { + std::string ssid = RandSSID(); + EXPECT_EQ(ssid.length(), 32); + EXPECT_EQ(ssid.find("DIRECT-"), 0); +} + +TEST(UtilsTests, GenRandRandWPAPassphrase) { + std::string password = RandWPAPassphrase(); + EXPECT_EQ(password.length(), 63); +} } // namespace linux } // namespace nearby diff --git a/internal/platform/implementation/linux/wifi_hotspot.cc b/internal/platform/implementation/linux/wifi_hotspot.cc index fa1a3962..1ce8059d 100644 --- a/internal/platform/implementation/linux/wifi_hotspot.cc +++ b/internal/platform/implementation/linux/wifi_hotspot.cc @@ -19,10 +19,9 @@ #include #include -#include - #include "internal/platform/implementation/linux/dbus.h" #include "internal/platform/implementation/linux/network_manager.h" +#include "internal/platform/implementation/linux/utils.h" #include "internal/platform/implementation/linux/wifi_hotspot.h" #include "internal/platform/implementation/linux/wifi_hotspot_server_socket.h" #include "internal/platform/implementation/linux/wifi_hotspot_socket.h" @@ -135,64 +134,41 @@ bool NetworkManagerWifiHotspotMedium::StartWifiHotspot( return false; } - sd_id128_t id; - if (auto ret = sd_id128_randomize(&id); ret < 0) { - NEARBY_LOGS(ERROR) << __func__ << ": error generating a 128-bit ID: " - << std::strerror(ret); - return false; - } - - char id_cstr[SD_ID128_UUID_STRING_MAX]; - sd_id128_to_string(id, id_cstr); - - std::string ssid = absl::StrCat("DIRECT-", id_cstr); - ssid.resize(32); + std::string ssid = RandSSID(); hotspot_credentials->SetSSID(ssid); - if (auto ret = sd_id128_randomize(&id); ret < 0) { - NEARBY_LOGS(ERROR) << __func__ << ": error generating a 128-bit ID: " - << std::strerror(ret); - return false; - } - - sd_id128_to_string(id, id_cstr); - std::string password = std::string(id_cstr, 15); + std::string password = RandWPAPassphrase(); hotspot_credentials->SetPassword(password); - if (auto ret = sd_id128_randomize(&id); ret < 0) { - NEARBY_LOGS(ERROR) << __func__ << ": error generating a 128-bit ID: " - << std::strerror(ret); + auto connection_id = NewUuidStr(); + if (!connection_id.has_value()) { + NEARBY_LOGS(ERROR) << __func__ << ": could not generate a connection UUID"; return false; } - sd_id128_to_uuid_string(id, id_cstr); - std::vector ssid_bytes(ssid.begin(), ssid.end()); std::map> connection_settings{ { "connection", - std::map{ - {"uuid", std::string(id_cstr)}, - {"id", "Google Nearby Hotspot"}, - {"type", "802-11-wireless"}, - {"zone", "Public"}}, + {{"uuid", *connection_id}, + {"id", "Google Nearby Hotspot"}, + {"type", "802-11-wireless"}, + {"zone", "Public"}}, }, {"802-11-wireless", - std::map{ - {"assigned-mac-address", "random"}, - {"ap-isolation", networkmanager::constants::kNMTernaryFalse}, - {"mode", "ap"}, - {"ssid", ssid_bytes}, - {"security", "802-11-wireless-security"}}}, + {{"assigned-mac-address", "random"}, + {"ap-isolation", networkmanager::constants::kNMTernaryFalse}, + {"mode", "ap"}, + {"ssid", std::vector(ssid.begin(), ssid.end())}, + {"security", "802-11-wireless-security"}}}, {"802-11-wireless-security", - std::map{ - {"pmf", networkmanager::constants::setting:: - kWirelessSecurityPMFDisable}, - {"key-mgmt", "wpa-psk"}, - {"psk", password}}}, - {"ipv4", std::map{{"method", "shared"}}}, + {{"pmf", + networkmanager::constants::setting::kWirelessSecurityPMFDisable}, + {"key-mgmt", "wpa-psk"}, + {"psk", password}}}, + {"ipv4", {{"method", "shared"}}}, {"ipv6", - std::map{ + { {"addr-gen-mode", networkmanager::constants::setting:: kIP6ConfigAddrGenModeStablePrivacy}, {"method", "shared"}, diff --git a/internal/platform/implementation/linux/wifi_medium.cc b/internal/platform/implementation/linux/wifi_medium.cc index ed555559..e9c33d2d 100644 --- a/internal/platform/implementation/linux/wifi_medium.cc +++ b/internal/platform/implementation/linux/wifi_medium.cc @@ -28,6 +28,7 @@ #include "internal/platform/implementation/linux/dbus.h" #include "internal/platform/implementation/linux/generated/dbus/networkmanager/device_wireless_client.h" #include "internal/platform/implementation/linux/network_manager_active_connection.h" +#include "internal/platform/implementation/linux/utils.h" #include "internal/platform/implementation/linux/wifi_medium.h" #include "internal/platform/implementation/wifi.h" @@ -229,21 +230,10 @@ api::WifiConnectionStatus NetworkManagerWifiMedium::ConnectToNetwork( return api::WifiConnectionStatus::kConnectionFailure; } - std::vector ssid_bytes(ssid.begin(), ssid.end()); - std::string connection_id; - - { - sd_id128_t id; - char id_cstr[SD_ID128_UUID_STRING_MAX]; - - if (auto ret = sd_id128_randomize(&id); ret < 0) { - NEARBY_LOGS(ERROR) << __func__ - << ": could not generate a connection UUID"; - return api::WifiConnectionStatus::kUnknown; - } - - sd_id128_to_uuid_string(id, id_cstr); - connection_id = std::string(id_cstr); + auto connection_id = NewUuidStr(); + if (!connection_id.has_value()) { + NEARBY_LOGS(ERROR) << __func__ << ": could not generate a connection UUID"; + return api::WifiConnectionStatus::kUnknown; } auto [auth_alg, key_mgmt] = AuthAlgAndKeyMgmt(auth_type); @@ -251,22 +241,21 @@ api::WifiConnectionStatus NetworkManagerWifiMedium::ConnectToNetwork( std::map> connection_settings{ {"connection", - std::map{ - {"uuid", connection_id}, + { + {"uuid", *connection_id}, {"autoconnect", true}, {"id", std::string(ssid)}, {"type", "802-11-wireless"}, {"zone", "Public"}, }}, {"802-11-wireless", - std::map{ - {"ssid", ssid_bytes}, + { + {"ssid", std::vector(ssid.begin(), ssid.end())}, {"mode", "infrastructure"}, {"security", "802-11-wireless-security"}, {"assigned-mac-address", "random"}, }}, - {"802-11-wireless-security", - std::map{{"key-mgmt", key_mgmt}}}}; + {"802-11-wireless-security", {{"key-mgmt", key_mgmt}}}}; if (!password.empty()) { connection_settings["802-11-wireless-security"]["psk"] = std::string(password);