tools: add dhtnet-certmgr

Certificate manager: generate and load certificate/identity

Change-Id: I920834133b5f78985833ee4043b5aa4562211197
diff --git a/tools/dhtnet_crtmgr/dhtnet_crtmgr.cpp b/tools/dhtnet_crtmgr/dhtnet_crtmgr.cpp
new file mode 100644
index 0000000..3d3d94a
--- /dev/null
+++ b/tools/dhtnet_crtmgr/dhtnet_crtmgr.cpp
@@ -0,0 +1,51 @@
+/*
+ *  Copyright (C) 2023 Savoir-faire Linux Inc.
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#include "dhtnet_crtmgr.h"
+#include "fileutils.h"
+
+#include <opendht/crypto.h>
+
+
+namespace dhtnet {
+
+dht::crypto::Identity
+loadIdentity(const std::filesystem::path& privatekey, const std::filesystem::path& cert)
+{
+    // check files exists
+    if (!std::filesystem::exists(privatekey) or !std::filesystem::exists(cert))
+    {
+        fmt::print(stderr, "Error: missing identity files\n");
+        return {};
+    }
+
+    // Load identity
+    auto privateKey = std::make_unique<dht::crypto::PrivateKey>(fileutils::loadFile(privatekey));
+    auto certificate = std::make_unique<dht::crypto::Certificate>(fileutils::loadFile(cert));
+    return dht::crypto::Identity(std::move(privateKey), std::move(certificate));
+}
+
+// generate a new identity
+dht::crypto::Identity generateIdentity(const std::filesystem::path& path_id, const std::string& name, const dht::crypto::Identity& ca)
+{
+    auto identity = dht::crypto::generateIdentity(name, ca);
+    if (!std::filesystem::exists(path_id))
+        std::filesystem::create_directories(path_id);
+    dht::crypto::saveIdentity(identity, path_id / name);
+    return identity;
+}
+} // namespace dhtnet