blob: f08c1e7a814de54b3b23b7abe8598dc730b0f1eb [file] [log] [blame]
Ravi Tejaa49895e2020-06-16 03:57:58 -05001#include "config.h"
2
3#include "ca_certs_manager.hpp"
4
5#include <filesystem>
6#include <fstream>
7#include <phosphor-logging/elog-errors.hpp>
8#include <phosphor-logging/elog.hpp>
9#include <phosphor-logging/log.hpp>
10#include <xyz/openbmc_project/Common/error.hpp>
11
Nan Zhoue1289ad2021-12-28 11:02:56 -080012namespace ca::cert
Ravi Tejaa49895e2020-06-16 03:57:58 -050013{
Ravi Tejaa49895e2020-06-16 03:57:58 -050014namespace fs = std::filesystem;
Nan Zhoucf06ccd2021-12-28 16:25:45 -080015using ::phosphor::logging::elog;
16using ::phosphor::logging::entry;
17using ::phosphor::logging::level;
18using ::phosphor::logging::log;
19
20using ::sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
21using Argument =
22 ::phosphor::logging::xyz::openbmc_project::Common::InvalidArgument;
23
24static constexpr size_t maxCertSize = 4096;
Ravi Tejaa49895e2020-06-16 03:57:58 -050025
26sdbusplus::message::object_path CACertMgr::signCSR(std::string csr)
27{
28 std::string objPath;
29 try
30 {
31 if (csr.size() > maxCertSize)
32 {
33 log<level::ERR>("Invalid CSR size");
34 elog<InvalidArgument>(Argument::ARGUMENT_NAME("CSR"),
35 Argument::ARGUMENT_VALUE(csr.c_str()));
36 }
37 auto id = lastEntryId + 1;
Nan Zhou718eef32021-12-28 11:03:30 -080038 objPath =
39 fs::path(objectNamePrefix) / "ca" / "entry" / std::to_string(id);
Ravi Tejaa49895e2020-06-16 03:57:58 -050040 std::string cert;
41 // Creating the dbus object here with the empty certificate string
42 // actual signing is being done by the hypervisor, once it signs then
43 // the certificate string would be updated with actual certificate.
44 entries.insert(std::make_pair(
45 id, std::make_unique<Entry>(bus, objPath, id, csr, cert, *this)));
46 lastEntryId++;
47 }
48 catch (const std::invalid_argument& e)
49 {
50 log<level::ERR>(e.what());
51 elog<InvalidArgument>(Argument::ARGUMENT_NAME("csr"),
52 Argument::ARGUMENT_VALUE(csr.c_str()));
53 }
54 return objPath;
55}
56
57void CACertMgr::erase(uint32_t entryId)
58{
59 entries.erase(entryId);
60}
61
62void CACertMgr::deleteAll()
63{
64 auto iter = entries.begin();
65 while (iter != entries.end())
66 {
67 auto& entry = iter->second;
68 ++iter;
69 entry->delete_();
70 }
71}
72
Nan Zhoue1289ad2021-12-28 11:02:56 -080073} // namespace ca::cert