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