Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <openssl/x509.h> |
| 4 | |
| 5 | #include <filesystem> |
| 6 | #include <phosphor-logging/elog.hpp> |
Marri Devender Rao | edd1131 | 2019-02-27 08:45:10 -0600 | [diff] [blame] | 7 | #include <xyz/openbmc_project/Certs/Certificate/server.hpp> |
Marri Devender Rao | 13bf74e | 2019-03-26 01:52:17 -0500 | [diff] [blame] | 8 | #include <xyz/openbmc_project/Certs/Replace/server.hpp> |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 9 | |
| 10 | namespace phosphor |
| 11 | { |
| 12 | namespace certs |
| 13 | { |
Marri Devender Rao | edd1131 | 2019-02-27 08:45:10 -0600 | [diff] [blame] | 14 | using CertificateIface = sdbusplus::server::object::object< |
| 15 | sdbusplus::xyz::openbmc_project::Certs::server::Certificate>; |
Marri Devender Rao | 13bf74e | 2019-03-26 01:52:17 -0500 | [diff] [blame] | 16 | using ReplaceIface = sdbusplus::xyz::openbmc_project::Certs::server::Replace; |
Marri Devender Rao | edd1131 | 2019-02-27 08:45:10 -0600 | [diff] [blame] | 17 | using CertIfaces = |
Marri Devender Rao | 13bf74e | 2019-03-26 01:52:17 -0500 | [diff] [blame] | 18 | sdbusplus::server::object::object<CertificateIface, ReplaceIface>; |
Marri Devender Rao | edd1131 | 2019-02-27 08:45:10 -0600 | [diff] [blame] | 19 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 20 | using CertificateType = std::string; |
| 21 | using UnitsToRestart = std::string; |
| 22 | using CertInstallPath = std::string; |
| 23 | using CertUploadPath = std::string; |
| 24 | using InputType = std::string; |
| 25 | using InstallFunc = std::function<void(const std::string&)>; |
| 26 | |
| 27 | using namespace phosphor::logging; |
| 28 | |
| 29 | // for placeholders |
| 30 | using namespace std::placeholders; |
| 31 | namespace fs = std::filesystem; |
| 32 | |
| 33 | // Supported Types. |
| 34 | static constexpr auto SERVER = "server"; |
| 35 | static constexpr auto CLIENT = "client"; |
| 36 | static constexpr auto AUTHORITY = "authority"; |
| 37 | |
| 38 | // RAII support for openSSL functions. |
| 39 | using X509_Ptr = std::unique_ptr<X509, decltype(&::X509_free)>; |
| 40 | |
| 41 | /** @class Certificate |
| 42 | * @brief OpenBMC Certificate entry implementation. |
| 43 | * @details A concrete implementation for the |
| 44 | * xyz.openbmc_project.Certs.Certificate DBus API |
| 45 | * xyz.openbmc_project.Certs.Instal DBus API |
| 46 | */ |
Marri Devender Rao | edd1131 | 2019-02-27 08:45:10 -0600 | [diff] [blame] | 47 | class Certificate : public CertIfaces |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 48 | { |
| 49 | public: |
| 50 | Certificate() = delete; |
| 51 | Certificate(const Certificate&) = delete; |
| 52 | Certificate& operator=(const Certificate&) = delete; |
| 53 | Certificate(Certificate&&) = delete; |
| 54 | Certificate& operator=(Certificate&&) = delete; |
| 55 | virtual ~Certificate(); |
| 56 | |
| 57 | /** @brief Constructor for the Certificate Object |
| 58 | * @param[in] bus - Bus to attach to. |
| 59 | * @param[in] objPath - Object path to attach to |
| 60 | * @param[in] type - Type of the certificate |
| 61 | * @param[in] unit - Units to restart after a certificate is installed |
| 62 | * @param[in] installPath - Path of the certificate to install |
| 63 | * @param[in] uploadPath - Path of the certificate file to upload |
Marri Devender Rao | 8f80c35 | 2019-05-13 00:53:01 -0500 | [diff] [blame] | 64 | * @param[in] isSkipUnitReload - If true do not restart units |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 65 | */ |
| 66 | Certificate(sdbusplus::bus::bus& bus, const std::string& objPath, |
| 67 | const CertificateType& type, const UnitsToRestart& unit, |
| 68 | const CertInstallPath& installPath, |
Marri Devender Rao | 8f80c35 | 2019-05-13 00:53:01 -0500 | [diff] [blame] | 69 | const CertUploadPath& uploadPath, bool isSkipUnitReload); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 70 | |
Marri Devender Rao | 13bf74e | 2019-03-26 01:52:17 -0500 | [diff] [blame] | 71 | /** @brief Validate certificate and replace the existing certificate |
| 72 | * @param[in] filePath - Certificate file path. |
| 73 | */ |
| 74 | void replace(const std::string filePath) override; |
| 75 | |
| 76 | private: |
| 77 | /** @brief Validate and Replace/Install the certificate file |
| 78 | * Install/Replace the existing certificate file with another |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 79 | * (possibly CA signed) Certificate file. |
| 80 | * @param[in] filePath - Certificate file path. |
Marri Devender Rao | 8f80c35 | 2019-05-13 00:53:01 -0500 | [diff] [blame] | 81 | * @param[in] isSkipUnitReload - If true do not restart units |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 82 | */ |
Marri Devender Rao | 8f80c35 | 2019-05-13 00:53:01 -0500 | [diff] [blame] | 83 | void install(const std::string& filePath, bool isSkipUnitReload); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 84 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 85 | /** @brief Load Certificate file into the X509 structre. |
| 86 | * @param[in] fileName - Certificate and key full file path. |
| 87 | * @return pointer to the X509 structure. |
| 88 | */ |
| 89 | X509_Ptr loadCert(const std::string& filePath); |
| 90 | |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 91 | /** @brief Populate certificate properties by parsing certificate file |
| 92 | * @return void |
| 93 | */ |
| 94 | void populateProperties(); |
| 95 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 96 | /** @brief Public/Private key compare function. |
| 97 | * Comparing private key against certificate public key |
| 98 | * from input .pem file. |
| 99 | * @param[in] fileName - Certificate and key full file path. |
| 100 | * @return Return true if Key compare is successful, |
| 101 | * false if not |
| 102 | */ |
| 103 | bool compareKeys(const std::string& filePath); |
| 104 | /** @brief systemd unit reload or reset helper function |
| 105 | * Reload if the unit supports it and use a restart otherwise. |
| 106 | * @param[in] unit - service need to reload. |
| 107 | */ |
| 108 | void reloadOrReset(const UnitsToRestart& unit); |
| 109 | |
| 110 | /** @brief Type specific function pointer map **/ |
| 111 | std::unordered_map<InputType, InstallFunc> typeFuncMap; |
| 112 | |
| 113 | /** @brief sdbusplus handler */ |
| 114 | sdbusplus::bus::bus& bus; |
| 115 | |
| 116 | /** @brief object path */ |
| 117 | std::string objectPath; |
| 118 | |
| 119 | /** @brief Type of the certificate **/ |
| 120 | CertificateType certType; |
| 121 | |
| 122 | /** @brief Unit name associated to the service **/ |
| 123 | UnitsToRestart unitToRestart; |
| 124 | |
| 125 | /** @brief Certificate file installation path **/ |
| 126 | CertInstallPath certInstallPath; |
| 127 | }; |
| 128 | |
| 129 | } // namespace certs |
| 130 | } // namespace phosphor |