From 2c6ffdb08b2207ff7c31041f77cc3755508d45c4 Mon Sep 17 00:00:00 2001 From: Ed Tanous Date: Wed, 28 Jun 2023 11:28:38 -0700 Subject: Use openssl random number generator We already have a generator class. We should use it. Wrap this into a function that can be unit tested, and add unit tests. Note, some files also needed to change name, because random.hpp conflicts with the built in random, and causes circular build problems. This commit changes it to ossl_random. Tested: Unit tests pass. Now has coverage. Redfish service validator passes. Change-Id: I5f8eee1af5f4843a352c6fd0e26d67fd3320ef53 Signed-off-by: Ed Tanous --- include/image_upload.hpp | 8 ++----- include/ossl_random.hpp | 52 +++++++++++++++++++++++++++++++++++++++++++++ include/persistent_data.hpp | 7 ++---- include/random.hpp | 49 ------------------------------------------ include/sessions.hpp | 2 +- include/ssl_key_handler.hpp | 2 +- 6 files changed, 58 insertions(+), 62 deletions(-) create mode 100644 include/ossl_random.hpp delete mode 100644 include/random.hpp (limited to 'include') diff --git a/include/image_upload.hpp b/include/image_upload.hpp index 18a0c09b78..cdd7dd47d2 100644 --- a/include/image_upload.hpp +++ b/include/image_upload.hpp @@ -3,10 +3,8 @@ #include "app.hpp" #include "dbus_singleton.hpp" #include "dbus_utility.hpp" +#include "ossl_random.hpp" -#include -#include -#include #include #include @@ -93,9 +91,7 @@ inline void "member='InterfacesAdded',path='/xyz/openbmc_project/software'", callback); - std::string filepath( - "/tmp/images/" + - boost::uuids::to_string(boost::uuids::random_generator()())); + std::string filepath("/tmp/images/" + bmcweb::getRandomUUID()); BMCWEB_LOG_DEBUG << "Writing file to " << filepath; std::ofstream out(filepath, std::ofstream::out | std::ofstream::binary | std::ofstream::trunc); diff --git a/include/ossl_random.hpp b/include/ossl_random.hpp new file mode 100644 index 0000000000..2cbec849a9 --- /dev/null +++ b/include/ossl_random.hpp @@ -0,0 +1,52 @@ +#pragma once + +#include + +#include +#include +#include + +namespace bmcweb +{ + +struct OpenSSLGenerator +{ + uint8_t operator()() + { + uint8_t index = 0; + int rc = RAND_bytes(&index, sizeof(index)); + if (rc != opensslSuccess) + { + std::cerr << "Cannot get random number\n"; + err = true; + } + + return index; + } + + static constexpr uint8_t max() + { + return std::numeric_limits::max(); + } + static constexpr uint8_t min() + { + return std::numeric_limits::min(); + } + + bool error() const + { + return err; + } + + // all generators require this variable + using result_type = uint8_t; + + private: + // RAND_bytes() returns 1 on success, 0 otherwise. -1 if bad function + static constexpr int opensslSuccess = 1; + bool err = false; +}; + +std::string getRandomUUID(); + +} // namespace bmcweb diff --git a/include/persistent_data.hpp b/include/persistent_data.hpp index 4344074a14..a08ca7e086 100644 --- a/include/persistent_data.hpp +++ b/include/persistent_data.hpp @@ -3,12 +3,10 @@ #include "event_service_store.hpp" #include "http_request.hpp" #include "http_response.hpp" +#include "ossl_random.hpp" #include "sessions.hpp" #include -#include -#include -#include #include #include @@ -178,8 +176,7 @@ class ConfigFile if (systemUuid.empty()) { - systemUuid = - boost::uuids::to_string(boost::uuids::random_generator()()); + systemUuid = bmcweb::getRandomUUID(); needWrite = true; } if (fileRevision < jsonRevision) diff --git a/include/random.hpp b/include/random.hpp deleted file mode 100644 index f5f094689e..0000000000 --- a/include/random.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#pragma once - -#include - -#include -#include - -namespace bmcweb -{ - -struct OpenSSLGenerator -{ - uint8_t operator()() - { - uint8_t index = 0; - int rc = RAND_bytes(&index, sizeof(index)); - if (rc != opensslSuccess) - { - std::cerr << "Cannot get random number\n"; - err = true; - } - - return index; - } - - static constexpr uint8_t max() - { - return std::numeric_limits::max(); - } - static constexpr uint8_t min() - { - return std::numeric_limits::min(); - } - - bool error() const - { - return err; - } - - // all generators require this variable - using result_type = uint8_t; - - private: - // RAND_bytes() returns 1 on success, 0 otherwise. -1 if bad function - static constexpr int opensslSuccess = 1; - bool err = false; -}; - -} // namespace bmcweb diff --git a/include/sessions.hpp b/include/sessions.hpp index 98912e827a..9179723e5f 100644 --- a/include/sessions.hpp +++ b/include/sessions.hpp @@ -1,7 +1,7 @@ #pragma once #include "logging.hpp" -#include "random.hpp" +#include "ossl_random.hpp" #include "utility.hpp" #include "utils/ip_utils.hpp" diff --git a/include/ssl_key_handler.hpp b/include/ssl_key_handler.hpp index 0794fdcfac..abc9b50d61 100644 --- a/include/ssl_key_handler.hpp +++ b/include/ssl_key_handler.hpp @@ -1,7 +1,7 @@ #pragma once #include "logging.hpp" -#include "random.hpp" +#include "ossl_random.hpp" extern "C" { -- cgit v1.2.3