Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 1 | #include "certs_manager.hpp" |
| 2 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 3 | #include <phosphor-logging/elog-errors.hpp> |
Marri Devender Rao | 13bf74e | 2019-03-26 01:52:17 -0500 | [diff] [blame^] | 4 | #include <xyz/openbmc_project/Certs/error.hpp> |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 5 | #include <xyz/openbmc_project/Common/error.hpp> |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 6 | namespace phosphor |
| 7 | { |
| 8 | namespace certs |
| 9 | { |
| 10 | |
Marri Devender Rao | 1396511 | 2019-02-27 08:47:12 -0600 | [diff] [blame] | 11 | using InternalFailure = |
| 12 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 13 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 14 | /** @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 | */ |
| 21 | Manager::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 Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 27 | { |
Marri Devender Rao | 13bf74e | 2019-03-26 01:52:17 -0500 | [diff] [blame^] | 28 | using InvalidCertificate = |
| 29 | sdbusplus::xyz::openbmc_project::Certs::Error::InvalidCertificate; |
| 30 | using Reason = xyz::openbmc_project::Certs::InvalidCertificate::REASON; |
Marri Devender Rao | bf7c588 | 2019-02-27 08:41:07 -0600 | [diff] [blame] | 31 | 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 Rao | bf7c588 | 2019-02-27 08:41:07 -0600 | [diff] [blame] | 45 | report<InternalFailure>(); |
| 46 | } |
| 47 | catch (const InvalidCertificate& e) |
| 48 | { |
Marri Devender Rao | bf7c588 | 2019-02-27 08:41:07 -0600 | [diff] [blame] | 49 | report<InvalidCertificate>( |
| 50 | Reason("Existing certificate file is corrupted")); |
| 51 | } |
| 52 | } |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 53 | } |
| 54 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 55 | void Manager::install(const std::string filePath) |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 56 | { |
Marri Devender Rao | 1396511 | 2019-02-27 08:47:12 -0600 | [diff] [blame] | 57 | 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 Othayoth | 589159f | 2018-09-28 08:32:39 -0500 | [diff] [blame] | 71 | } |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 72 | |
| 73 | void Manager::delete_() |
| 74 | { |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 75 | // 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 Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 81 | { |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 82 | certificatePtr.reset(nullptr); |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 83 | } |
| 84 | } |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 85 | } // namespace certs |
| 86 | } // namespace phosphor |