blob: ea19fcd74c411adb92c54bb555b26259be08759d [file] [log] [blame]
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -05001#include "certs_manager.hpp"
2
Marri Devender Rao6ceec402019-02-01 03:15:19 -06003#include <phosphor-logging/elog-errors.hpp>
Marri Devender Rao13bf74e2019-03-26 01:52:17 -05004#include <xyz/openbmc_project/Certs/error.hpp>
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -05005#include <xyz/openbmc_project/Common/error.hpp>
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -05006namespace phosphor
7{
8namespace certs
9{
10
Marri Devender Rao13965112019-02-27 08:47:12 -060011using InternalFailure =
12 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050013
Marri Devender Rao6ceec402019-02-01 03:15:19 -060014/** @brief Constructor to put object onto bus at a dbus path.
15 * @param[in] bus - Bus to attach to.
16 * @param[in] path - Path to attach at.
17 * @param[in] type - Type of the certificate.
18 * @param[in] unit - Unit consumed by this certificate.
19 * @param[in] installPath - Certificate installation path.
20 */
21Manager::Manager(sdbusplus::bus::bus& bus, const char* path,
22 const CertificateType& type, UnitsToRestart&& unit,
23 CertInstallPath&& installPath) :
24 Ifaces(bus, path),
25 bus(bus), objectPath(path), certType(type), unitToRestart(std::move(unit)),
26 certInstallPath(std::move(installPath))
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050027{
Marri Devender Rao13bf74e2019-03-26 01:52:17 -050028 using InvalidCertificate =
29 sdbusplus::xyz::openbmc_project::Certs::Error::InvalidCertificate;
30 using Reason = xyz::openbmc_project::Certs::InvalidCertificate::REASON;
Marri Devender Raobf7c5882019-02-27 08:41:07 -060031 if (fs::exists(certInstallPath))
32 {
33 try
34 {
35 // TODO: Issue#3 At present supporting only one certificate to be
36 // uploaded this need to be revisited to support multiple
37 // certificates
38 auto certObjectPath = objectPath + '/' + '1';
39 certificatePtr = std::make_unique<Certificate>(
40 bus, certObjectPath, certType, unitToRestart, certInstallPath,
41 certInstallPath);
42 }
43 catch (const InternalFailure& e)
44 {
Marri Devender Raobf7c5882019-02-27 08:41:07 -060045 report<InternalFailure>();
46 }
47 catch (const InvalidCertificate& e)
48 {
Marri Devender Raobf7c5882019-02-27 08:41:07 -060049 report<InvalidCertificate>(
50 Reason("Existing certificate file is corrupted"));
51 }
52 }
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050053}
54
Marri Devender Rao6ceec402019-02-01 03:15:19 -060055void Manager::install(const std::string filePath)
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050056{
Marri Devender Rao13965112019-02-27 08:47:12 -060057 using NotAllowed =
58 sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
59 using Reason = xyz::openbmc_project::Common::NotAllowed::REASON;
60 // TODO: Issue#3 At present supporting only one certificate to be
61 // uploaded this need to be revisited to support multiple
62 // certificates
63 if (certificatePtr != nullptr)
64 {
65 elog<NotAllowed>(Reason("Certificate already exist"));
66 }
67 auto certObjectPath = objectPath + '/' + '1';
68 certificatePtr =
69 std::make_unique<Certificate>(bus, certObjectPath, certType,
70 unitToRestart, certInstallPath, filePath);
Jayanth Othayoth589159f2018-09-28 08:32:39 -050071}
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050072
73void Manager::delete_()
74{
Marri Devender Rao6ceec402019-02-01 03:15:19 -060075 // TODO: #Issue 4 when a certificate is deleted system auto generates
76 // certificate file. At present we are not supporting creation of
77 // certificate object for the auto-generated certificate file as
78 // deletion if only applicable for REST server and Bmcweb does not allow
79 // deletion of certificates
80 if (certificatePtr != nullptr)
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050081 {
Marri Devender Rao6ceec402019-02-01 03:15:19 -060082 certificatePtr.reset(nullptr);
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050083 }
84}
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050085} // namespace certs
86} // namespace phosphor