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