blob: bbb802533730214659f1fd554a089d18c7e647a0 [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;
Nan Zhou718eef32021-12-28 11:03:30 -080033 objPath =
34 fs::path(objectNamePrefix) / "ca" / "entry" / std::to_string(id);
Ravi Tejaa49895e2020-06-16 03:57:58 -050035 std::string cert;
36 // Creating the dbus object here with the empty certificate string
37 // actual signing is being done by the hypervisor, once it signs then
38 // the certificate string would be updated with actual certificate.
39 entries.insert(std::make_pair(
40 id, std::make_unique<Entry>(bus, objPath, id, csr, cert, *this)));
41 lastEntryId++;
42 }
43 catch (const std::invalid_argument& e)
44 {
45 log<level::ERR>(e.what());
46 elog<InvalidArgument>(Argument::ARGUMENT_NAME("csr"),
47 Argument::ARGUMENT_VALUE(csr.c_str()));
48 }
49 return objPath;
50}
51
52void CACertMgr::erase(uint32_t entryId)
53{
54 entries.erase(entryId);
55}
56
57void CACertMgr::deleteAll()
58{
59 auto iter = entries.begin();
60 while (iter != entries.end())
61 {
62 auto& entry = iter->second;
63 ++iter;
64 entry->delete_();
65 }
66}
67
Nan Zhoue1289ad2021-12-28 11:02:56 -080068} // namespace ca::cert