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