blob: 47cb12d212fb97859bf8ad191b8caa91fbddceb0 [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>
Ravi Tejaf2646272023-09-30 13:00:55 -05007#include <phosphor-logging/lg2.hpp>
Ravi Tejaa49895e2020-06-16 03:57:58 -05008#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;
Nan Zhoucf06ccd2021-12-28 16:25:45 -080017
18using ::sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
19using Argument =
20 ::phosphor::logging::xyz::openbmc_project::Common::InvalidArgument;
21
22static constexpr size_t maxCertSize = 4096;
Ravi Tejaa49895e2020-06-16 03:57:58 -050023
24sdbusplus::message::object_path CACertMgr::signCSR(std::string csr)
25{
26 std::string objPath;
27 try
28 {
29 if (csr.size() > maxCertSize)
30 {
Ravi Tejaf2646272023-09-30 13:00:55 -050031 lg2::error("Invalid CSR size");
Ravi Tejaa49895e2020-06-16 03:57:58 -050032 elog<InvalidArgument>(Argument::ARGUMENT_NAME("CSR"),
33 Argument::ARGUMENT_VALUE(csr.c_str()));
34 }
35 auto id = lastEntryId + 1;
Patrick Williams223e4602023-05-10 07:51:11 -050036 objPath = fs::path(objectNamePrefix) / "ca" / "entry" /
37 std::to_string(id);
Ravi Tejaa49895e2020-06-16 03:57:58 -050038 std::string cert;
39 // Creating the dbus object here with the empty certificate string
40 // actual signing is being done by the hypervisor, once it signs then
41 // the certificate string would be updated with actual certificate.
42 entries.insert(std::make_pair(
43 id, std::make_unique<Entry>(bus, objPath, id, csr, cert, *this)));
44 lastEntryId++;
45 }
46 catch (const std::invalid_argument& e)
47 {
Ravi Tejaf2646272023-09-30 13:00:55 -050048 lg2::error(e.what());
Ravi Tejaa49895e2020-06-16 03:57:58 -050049 elog<InvalidArgument>(Argument::ARGUMENT_NAME("csr"),
50 Argument::ARGUMENT_VALUE(csr.c_str()));
51 }
52 return objPath;
53}
54
55void CACertMgr::erase(uint32_t entryId)
56{
57 entries.erase(entryId);
58}
59
60void CACertMgr::deleteAll()
61{
62 auto iter = entries.begin();
63 while (iter != entries.end())
64 {
65 auto& entry = iter->second;
66 ++iter;
67 entry->delete_();
68 }
69}
70
Nan Zhoue1289ad2021-12-28 11:02:56 -080071} // namespace ca::cert