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