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 <edtanous@google.com>
diff --git a/include/image_upload.hpp b/include/image_upload.hpp
index 18a0c09..cdd7dd4 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 <boost/uuid/uuid.hpp>
-#include <boost/uuid/uuid_generators.hpp>
-#include <boost/uuid/uuid_io.hpp>
 #include <sdbusplus/bus/match.hpp>
 
 #include <cstdio>
@@ -93,9 +91,7 @@
         "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/random.hpp b/include/ossl_random.hpp
similarity index 94%
rename from include/random.hpp
rename to include/ossl_random.hpp
index f5f0946..2cbec84 100644
--- a/include/random.hpp
+++ b/include/ossl_random.hpp
@@ -4,6 +4,7 @@
 
 #include <iostream>
 #include <limits>
+#include <string>
 
 namespace bmcweb
 {
@@ -46,4 +47,6 @@
     bool err = false;
 };
 
+std::string getRandomUUID();
+
 } // namespace bmcweb
diff --git a/include/persistent_data.hpp b/include/persistent_data.hpp
index 4344074..a08ca7e 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 <boost/beast/http/fields.hpp>
-#include <boost/uuid/uuid.hpp>
-#include <boost/uuid/uuid_generators.hpp>
-#include <boost/uuid/uuid_io.hpp>
 #include <nlohmann/json.hpp>
 
 #include <filesystem>
@@ -178,8 +176,7 @@
 
         if (systemUuid.empty())
         {
-            systemUuid =
-                boost::uuids::to_string(boost::uuids::random_generator()());
+            systemUuid = bmcweb::getRandomUUID();
             needWrite = true;
         }
         if (fileRevision < jsonRevision)
diff --git a/include/sessions.hpp b/include/sessions.hpp
index 98912e8..9179723 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 0794fdc..abc9b50 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"
 {
diff --git a/meson.build b/meson.build
index dd8eb81..e9716be 100644
--- a/meson.build
+++ b/meson.build
@@ -355,6 +355,7 @@
   'src/boost_url.cpp',
   'src/dbus_singleton.cpp',
   'src/json_html_serializer.cpp',
+  'src/ossl_random.cpp',
 )
 
 bmcweblib = static_library(
@@ -391,6 +392,7 @@
   'test/include/multipart_test.cpp',
   'test/include/openbmc_dbus_rest_test.cpp',
   'test/include/str_utility_test.cpp',
+  'test/include/ossl_random.cpp',
   'test/redfish-core/include/privileges_test.cpp',
   'test/redfish-core/include/redfish_aggregator_test.cpp',
   'test/redfish-core/include/registries_test.cpp',
diff --git a/redfish-core/include/event_service_manager.hpp b/redfish-core/include/event_service_manager.hpp
index 3a20a79..a1c4fb9 100644
--- a/redfish-core/include/event_service_manager.hpp
+++ b/redfish-core/include/event_service_manager.hpp
@@ -19,8 +19,8 @@
 #include "event_service_store.hpp"
 #include "http_client.hpp"
 #include "metric_report.hpp"
+#include "ossl_random.hpp"
 #include "persistent_data.hpp"
-#include "random.hpp"
 #include "registries.hpp"
 #include "registries_selector.hpp"
 #include "str_utility.hpp"
diff --git a/redfish-core/lib/update_service.hpp b/redfish-core/lib/update_service.hpp
index 28621d5..357c047 100644
--- a/redfish-core/lib/update_service.hpp
+++ b/redfish-core/lib/update_service.hpp
@@ -20,6 +20,7 @@
 #include "app.hpp"
 #include "dbus_utility.hpp"
 #include "multipart_parser.hpp"
+#include "ossl_random.hpp"
 #include "query.hpp"
 #include "registries/privilege_registry.hpp"
 #include "task.hpp"
@@ -531,9 +532,8 @@
 
 inline void uploadImageFile(crow::Response& res, std::string_view body)
 {
-    std::filesystem::path filepath(
-        "/tmp/images/" +
-        boost::uuids::to_string(boost::uuids::random_generator()()));
+    std::filesystem::path 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/src/ossl_random.cpp b/src/ossl_random.cpp
new file mode 100644
index 0000000..f196a8b
--- /dev/null
+++ b/src/ossl_random.cpp
@@ -0,0 +1,12 @@
+#include "ossl_random.hpp"
+
+#include <boost/uuid/uuid_generators.hpp>
+#include <boost/uuid/uuid_io.hpp>
+
+std::string bmcweb::getRandomUUID()
+{
+    using bmcweb::OpenSSLGenerator;
+    OpenSSLGenerator ossl;
+    return boost::uuids::to_string(
+        boost::uuids::basic_random_generator<OpenSSLGenerator>(ossl)());
+}
diff --git a/test/include/ossl_random.cpp b/test/include/ossl_random.cpp
new file mode 100644
index 0000000..22935e7
--- /dev/null
+++ b/test/include/ossl_random.cpp
@@ -0,0 +1,23 @@
+#include "ossl_random.hpp"
+
+#include <string>
+
+#include <gmock/gmock.h> // IWYU pragma: keep
+#include <gtest/gtest.h> // IWYU pragma: keep
+
+namespace
+{
+
+using testing::MatchesRegex;
+
+TEST(Bmcweb, GetRandomUUID)
+{
+    using bmcweb::getRandomUUID;
+    // 78e96a4b-62fe-48d8-ac09-7f75a94671e0
+    EXPECT_THAT(
+        getRandomUUID(),
+        MatchesRegex(
+            "^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$"));
+}
+
+} // namespace