Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 1 | #pragma once |
Jayanth Othayoth | dd74bd2 | 2018-09-28 06:13:35 -0500 | [diff] [blame] | 2 | #include <openssl/x509.h> |
| 3 | |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 4 | #include <cstring> |
Jayanth Othayoth | feddcf2 | 2018-11-07 01:14:23 -0600 | [diff] [blame] | 5 | #include <phosphor-logging/elog-errors.hpp> |
| 6 | #include <phosphor-logging/elog.hpp> |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 7 | #include <sdbusplus/bus.hpp> |
| 8 | #include <sdbusplus/server/object.hpp> |
| 9 | #include <unordered_map> |
Jayanth Othayoth | feddcf2 | 2018-11-07 01:14:23 -0600 | [diff] [blame] | 10 | #include <xyz/openbmc_project/Certs/Install/error.hpp> |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Certs/Install/server.hpp> |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Object/Delete/server.hpp> |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 13 | |
| 14 | namespace phosphor |
| 15 | { |
| 16 | namespace certs |
| 17 | { |
Jayanth Othayoth | dd74bd2 | 2018-09-28 06:13:35 -0500 | [diff] [blame] | 18 | // RAII support for openSSL functions. |
| 19 | using X509_Ptr = std::unique_ptr<X509, decltype(&::X509_free)>; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 20 | |
| 21 | // Supported Types. |
| 22 | static constexpr auto SERVER = "server"; |
| 23 | static constexpr auto CLIENT = "client"; |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 24 | static constexpr auto AUTHORITY = "authority"; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 25 | |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 26 | using Create = sdbusplus::xyz::openbmc_project::Certs::server::Install; |
| 27 | using Delete = sdbusplus::xyz::openbmc_project::Object::server::Delete; |
| 28 | using Ifaces = sdbusplus::server::object::object<Create, Delete>; |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 29 | using InstallFunc = std::function<void(const std::string&)>; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 30 | using InputType = std::string; |
| 31 | |
Jayanth Othayoth | feddcf2 | 2018-11-07 01:14:23 -0600 | [diff] [blame] | 32 | using namespace phosphor::logging; |
| 33 | using InvalidCertificate = |
| 34 | sdbusplus::xyz::openbmc_project::Certs::Install::Error::InvalidCertificate; |
| 35 | using Reason = xyz::openbmc_project::Certs::Install::InvalidCertificate::REASON; |
| 36 | |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 37 | // for placeholders |
| 38 | using namespace std::placeholders; |
| 39 | |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 40 | class Manager : public Ifaces |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 41 | { |
| 42 | public: |
| 43 | /* Define all of the basic class operations: |
| 44 | * Not allowed: |
| 45 | * - Default constructor is not possible due to member |
| 46 | * reference |
| 47 | * - Move operations due to 'this' being registered as the |
| 48 | * 'context' with sdbus. |
| 49 | * Allowed: |
| 50 | * - copy |
| 51 | * - Destructor. |
| 52 | */ |
| 53 | Manager() = delete; |
| 54 | Manager(const Manager&) = default; |
| 55 | Manager& operator=(const Manager&) = delete; |
| 56 | Manager(Manager&&) = delete; |
| 57 | Manager& operator=(Manager&&) = delete; |
| 58 | virtual ~Manager() = default; |
| 59 | |
| 60 | /** @brief Constructor to put object onto bus at a dbus path. |
| 61 | * @param[in] bus - Bus to attach to. |
| 62 | * @param[in] path - Path to attach at. |
| 63 | * @param[in] type - Type of the certificate. |
| 64 | * @param[in] unit - Unit consumed by this certificate. |
| 65 | * @param[in] certpath - Certificate installation path. |
| 66 | */ |
| 67 | Manager(sdbusplus::bus::bus& bus, const char* path, const std::string& type, |
| 68 | std::string&& unit, std::string&& certPath) : |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 69 | Ifaces(bus, path), |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 70 | bus(bus), path(path), type(type), unit(std::move(unit)), |
| 71 | certPath(std::move(certPath)) |
| 72 | { |
Jayanth Othayoth | feddcf2 | 2018-11-07 01:14:23 -0600 | [diff] [blame] | 73 | auto installHelper = [this](const auto& filePath) { |
| 74 | if (!compareKeys(filePath)) |
| 75 | { |
| 76 | elog<InvalidCertificate>( |
| 77 | Reason("Private key does not match the Certificate")); |
| 78 | }; |
| 79 | }; |
| 80 | |
| 81 | typeFuncMap[SERVER] = installHelper; |
| 82 | typeFuncMap[CLIENT] = installHelper; |
| 83 | typeFuncMap[AUTHORITY] = [](auto filePath) {}; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** @brief Implementation for Install |
| 87 | * Replace the existing certificate key file with another |
| 88 | * (possibly CA signed) Certificate key file. |
| 89 | * |
| 90 | * @param[in] path - Certificate key file path. |
| 91 | */ |
| 92 | void install(const std::string path) override; |
| 93 | |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 94 | /** @brief Delete the certificate (and possibly revert |
| 95 | * to a self-signed certificate). |
| 96 | */ |
| 97 | void delete_() override; |
| 98 | |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 99 | private: |
Jayanth Othayoth | e8199a8 | 2018-09-29 00:46:10 -0500 | [diff] [blame] | 100 | /** @brief systemd unit reload or reset helper function |
| 101 | * Reload if the unit supports it and use a restart otherwise. |
| 102 | * @param[in] unit - service need to reload. |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 103 | */ |
Marri Devender Rao | 9abfae8 | 2018-10-03 08:10:35 -0500 | [diff] [blame] | 104 | virtual void reloadOrReset(const std::string& unit); |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 105 | |
| 106 | /** @brief helper function to copy the file. |
| 107 | * @param[in] src - Source file path to copy |
| 108 | * @param[in] dst - Destination path to copy |
| 109 | */ |
| 110 | void copy(const std::string& src, const std::string& dst); |
| 111 | |
Jayanth Othayoth | dd74bd2 | 2018-09-28 06:13:35 -0500 | [diff] [blame] | 112 | /** @brief Certificate verification function |
| 113 | * Certificate file specific validation using openssl |
| 114 | * verify function also includes expiry date check |
| 115 | * @param[in] fileName - Certificate and key full file path. |
| 116 | * @return error code from open ssl verify function. |
| 117 | */ |
| 118 | int32_t verifyCert(const std::string& filePath); |
| 119 | |
| 120 | /** @brief Load Certificate file into the X509 structre. |
| 121 | * @param[in] fileName - Certificate and key full file path. |
| 122 | * @return pointer to the X509 structure. |
| 123 | */ |
| 124 | X509_Ptr loadCert(const std::string& filePath); |
| 125 | |
Jayanth Othayoth | 589159f | 2018-09-28 08:32:39 -0500 | [diff] [blame] | 126 | /** @brief Public/Private key compare function. |
| 127 | * Comparing private key against certificate public key |
| 128 | * from input .pem file. |
| 129 | * @param[in] fileName - Certificate and key full file path. |
| 130 | * @return Return true if Key compare is successful, |
| 131 | * false if not |
| 132 | */ |
| 133 | bool compareKeys(const std::string& filePath); |
| 134 | |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 135 | /** @brief sdbusplus handler */ |
| 136 | sdbusplus::bus::bus& bus; |
| 137 | |
| 138 | /** @brief object path */ |
| 139 | std::string path; |
| 140 | |
| 141 | /** @brief Type of the certificate **/ |
| 142 | InputType type; |
| 143 | |
| 144 | /** @brief Unit name associated to the service **/ |
| 145 | std::string unit; |
| 146 | |
| 147 | /** @brief Certificate file installation path **/ |
| 148 | std::string certPath; |
| 149 | |
| 150 | /** @brief Type specific function pointer map **/ |
| 151 | std::unordered_map<InputType, InstallFunc> typeFuncMap; |
| 152 | }; |
| 153 | |
| 154 | } // namespace certs |
| 155 | } // namespace phosphor |