xref: /openbmc/phosphor-certificate-manager/certs_manager.cpp (revision 13bf74e4486df30fb70b9dbb040277bcdf50e0ec)
1cfbc8dc8SJayanth Othayoth #include "certs_manager.hpp"
2cfbc8dc8SJayanth Othayoth 
36ceec40bSMarri Devender Rao #include <phosphor-logging/elog-errors.hpp>
4*13bf74e4SMarri Devender Rao #include <xyz/openbmc_project/Certs/error.hpp>
5cfbc8dc8SJayanth Othayoth #include <xyz/openbmc_project/Common/error.hpp>
6cfbc8dc8SJayanth Othayoth namespace phosphor
7cfbc8dc8SJayanth Othayoth {
8cfbc8dc8SJayanth Othayoth namespace certs
9cfbc8dc8SJayanth Othayoth {
10cfbc8dc8SJayanth Othayoth 
111396511dSMarri Devender Rao using InternalFailure =
121396511dSMarri Devender Rao     sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
13cfbc8dc8SJayanth Othayoth 
146ceec40bSMarri Devender Rao /** @brief Constructor to put object onto bus at a dbus path.
156ceec40bSMarri Devender Rao  *  @param[in] bus - Bus to attach to.
166ceec40bSMarri Devender Rao  *  @param[in] path - Path to attach at.
176ceec40bSMarri Devender Rao  *  @param[in] type - Type of the certificate.
186ceec40bSMarri Devender Rao  *  @param[in] unit - Unit consumed by this certificate.
196ceec40bSMarri Devender Rao  *  @param[in] installPath - Certificate installation path.
206ceec40bSMarri Devender Rao  */
216ceec40bSMarri Devender Rao Manager::Manager(sdbusplus::bus::bus& bus, const char* path,
226ceec40bSMarri Devender Rao                  const CertificateType& type, UnitsToRestart&& unit,
236ceec40bSMarri Devender Rao                  CertInstallPath&& installPath) :
246ceec40bSMarri Devender Rao     Ifaces(bus, path),
256ceec40bSMarri Devender Rao     bus(bus), objectPath(path), certType(type), unitToRestart(std::move(unit)),
266ceec40bSMarri Devender Rao     certInstallPath(std::move(installPath))
27cfbc8dc8SJayanth Othayoth {
28*13bf74e4SMarri Devender Rao     using InvalidCertificate =
29*13bf74e4SMarri Devender Rao         sdbusplus::xyz::openbmc_project::Certs::Error::InvalidCertificate;
30*13bf74e4SMarri Devender Rao     using Reason = xyz::openbmc_project::Certs::InvalidCertificate::REASON;
31bf7c588cSMarri Devender Rao     if (fs::exists(certInstallPath))
32bf7c588cSMarri Devender Rao     {
33bf7c588cSMarri Devender Rao         try
34bf7c588cSMarri Devender Rao         {
35bf7c588cSMarri Devender Rao             // TODO: Issue#3 At present supporting only one certificate to be
36bf7c588cSMarri Devender Rao             // uploaded this need to be revisited to support multiple
37bf7c588cSMarri Devender Rao             // certificates
38bf7c588cSMarri Devender Rao             auto certObjectPath = objectPath + '/' + '1';
39bf7c588cSMarri Devender Rao             certificatePtr = std::make_unique<Certificate>(
40bf7c588cSMarri Devender Rao                 bus, certObjectPath, certType, unitToRestart, certInstallPath,
41bf7c588cSMarri Devender Rao                 certInstallPath);
42bf7c588cSMarri Devender Rao         }
43bf7c588cSMarri Devender Rao         catch (const InternalFailure& e)
44bf7c588cSMarri Devender Rao         {
45bf7c588cSMarri Devender Rao             report<InternalFailure>();
46bf7c588cSMarri Devender Rao         }
47bf7c588cSMarri Devender Rao         catch (const InvalidCertificate& e)
48bf7c588cSMarri Devender Rao         {
49bf7c588cSMarri Devender Rao             report<InvalidCertificate>(
50bf7c588cSMarri Devender Rao                 Reason("Existing certificate file is corrupted"));
51bf7c588cSMarri Devender Rao         }
52bf7c588cSMarri Devender Rao     }
53dd74bd20SJayanth Othayoth }
54589159f2SJayanth Othayoth 
556ceec40bSMarri Devender Rao void Manager::install(const std::string filePath)
56cfbc8dc8SJayanth Othayoth {
571396511dSMarri Devender Rao     using NotAllowed =
581396511dSMarri Devender Rao         sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
591396511dSMarri Devender Rao     using Reason = xyz::openbmc_project::Common::NotAllowed::REASON;
601396511dSMarri Devender Rao     // TODO: Issue#3 At present supporting only one certificate to be
611396511dSMarri Devender Rao     // uploaded this need to be revisited to support multiple
621396511dSMarri Devender Rao     // certificates
631396511dSMarri Devender Rao     if (certificatePtr != nullptr)
641396511dSMarri Devender Rao     {
651396511dSMarri Devender Rao         elog<NotAllowed>(Reason("Certificate already exist"));
661396511dSMarri Devender Rao     }
671396511dSMarri Devender Rao     auto certObjectPath = objectPath + '/' + '1';
681396511dSMarri Devender Rao     certificatePtr =
691396511dSMarri Devender Rao         std::make_unique<Certificate>(bus, certObjectPath, certType,
701396511dSMarri Devender Rao                                       unitToRestart, certInstallPath, filePath);
71589159f2SJayanth Othayoth }
72ae70b3daSDeepak Kodihalli 
73ae70b3daSDeepak Kodihalli void Manager::delete_()
74ae70b3daSDeepak Kodihalli {
756ceec40bSMarri Devender Rao     // TODO: #Issue 4 when a certificate is deleted system auto generates
766ceec40bSMarri Devender Rao     // certificate file. At present we are not supporting creation of
776ceec40bSMarri Devender Rao     // certificate object for the auto-generated certificate file as
786ceec40bSMarri Devender Rao     // deletion if only applicable for REST server and Bmcweb does not allow
796ceec40bSMarri Devender Rao     // deletion of certificates
806ceec40bSMarri Devender Rao     if (certificatePtr != nullptr)
81ae70b3daSDeepak Kodihalli     {
826ceec40bSMarri Devender Rao         certificatePtr.reset(nullptr);
83ae70b3daSDeepak Kodihalli     }
84ae70b3daSDeepak Kodihalli }
85cfbc8dc8SJayanth Othayoth } // namespace certs
86cfbc8dc8SJayanth Othayoth } // namespace phosphor
87