blob: 3d3d94a77399e191862cdac9b2cdba67e3d14ca4 [file] [log] [blame]
Amna423b25b2024-02-06 13:21:19 -05001/*
2 * Copyright (C) 2023 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#include "dhtnet_crtmgr.h"
19#include "fileutils.h"
20
21#include <opendht/crypto.h>
22
23
24namespace dhtnet {
25
26dht::crypto::Identity
27loadIdentity(const std::filesystem::path& privatekey, const std::filesystem::path& cert)
28{
29 // check files exists
30 if (!std::filesystem::exists(privatekey) or !std::filesystem::exists(cert))
31 {
32 fmt::print(stderr, "Error: missing identity files\n");
33 return {};
34 }
35
36 // Load identity
37 auto privateKey = std::make_unique<dht::crypto::PrivateKey>(fileutils::loadFile(privatekey));
38 auto certificate = std::make_unique<dht::crypto::Certificate>(fileutils::loadFile(cert));
39 return dht::crypto::Identity(std::move(privateKey), std::move(certificate));
40}
41
42// generate a new identity
43dht::crypto::Identity generateIdentity(const std::filesystem::path& path_id, const std::string& name, const dht::crypto::Identity& ca)
44{
45 auto identity = dht::crypto::generateIdentity(name, ca);
46 if (!std::filesystem::exists(path_id))
47 std::filesystem::create_directories(path_id);
48 dht::crypto::saveIdentity(identity, path_id / name);
49 return identity;
50}
51} // namespace dhtnet