blob: 0f4e908bf8e9b3f81e9a67b75c0d3093a27f2e90 [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 -050014static constexpr auto maxCertSize = 4096;
15namespace fs = std::filesystem;
16using namespace phosphor::logging;
17using InvalidArgument =
18 sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
19using Argument = xyz::openbmc_project::Common::InvalidArgument;
20
21sdbusplus::message::object_path CACertMgr::signCSR(std::string csr)
22{
23 std::string objPath;
24 try
25 {
26 if (csr.size() > maxCertSize)
27 {
28 log<level::ERR>("Invalid CSR size");
29 elog<InvalidArgument>(Argument::ARGUMENT_NAME("CSR"),
30 Argument::ARGUMENT_VALUE(csr.c_str()));
31 }
32 auto id = lastEntryId + 1;
Ravi Teja11d09062021-04-20 11:52:42 -050033 objPath = fs::path(OBJPATH) / "ca" / "entry" / std::to_string(id);
Ravi Tejaa49895e2020-06-16 03:57:58 -050034 std::string cert;
35 // Creating the dbus object here with the empty certificate string
36 // actual signing is being done by the hypervisor, once it signs then
37 // the certificate string would be updated with actual certificate.
38 entries.insert(std::make_pair(
39 id, std::make_unique<Entry>(bus, objPath, id, csr, cert, *this)));
40 lastEntryId++;
41 }
42 catch (const std::invalid_argument& e)
43 {
44 log<level::ERR>(e.what());
45 elog<InvalidArgument>(Argument::ARGUMENT_NAME("csr"),
46 Argument::ARGUMENT_VALUE(csr.c_str()));
47 }
48 return objPath;
49}
50
51void CACertMgr::erase(uint32_t entryId)
52{
53 entries.erase(entryId);
54}
55
56void CACertMgr::deleteAll()
57{
58 auto iter = entries.begin();
59 while (iter != entries.end())
60 {
61 auto& entry = iter->second;
62 ++iter;
63 entry->delete_();
64 }
65}
66
Nan Zhoue1289ad2021-12-28 11:02:56 -080067} // namespace ca::cert