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> |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Object/Delete/server.hpp> |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 12 | |
| 13 | namespace phosphor |
| 14 | { |
| 15 | namespace certs |
| 16 | { |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 17 | using DeleteIface = sdbusplus::xyz::openbmc_project::Object::server::Delete; |
Marri Devender Rao | edd1131 | 2019-02-27 08:45:10 -0600 | [diff] [blame] | 18 | using CertificateIface = sdbusplus::server::object::object< |
| 19 | sdbusplus::xyz::openbmc_project::Certs::server::Certificate>; |
Marri Devender Rao | 13bf74e | 2019-03-26 01:52:17 -0500 | [diff] [blame] | 20 | using ReplaceIface = sdbusplus::xyz::openbmc_project::Certs::server::Replace; |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 21 | using CertIfaces = sdbusplus::server::object::object<CertificateIface, |
| 22 | ReplaceIface, DeleteIface>; |
Marri Devender Rao | edd1131 | 2019-02-27 08:45:10 -0600 | [diff] [blame] | 23 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 24 | using CertificateType = std::string; |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 25 | using CertInstallPath = std::string; |
| 26 | using CertUploadPath = std::string; |
| 27 | using InputType = std::string; |
| 28 | using InstallFunc = std::function<void(const std::string&)>; |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 29 | using AppendPrivKeyFunc = std::function<void(const std::string&)>; |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 30 | using CertWatchPtr = std::unique_ptr<Watch>; |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 31 | using namespace phosphor::logging; |
| 32 | |
| 33 | // for placeholders |
| 34 | using namespace std::placeholders; |
| 35 | namespace fs = std::filesystem; |
| 36 | |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 37 | class Manager; // Forward declaration for Certificate Manager. |
| 38 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 39 | // Supported Types. |
| 40 | static constexpr auto SERVER = "server"; |
| 41 | static constexpr auto CLIENT = "client"; |
| 42 | static constexpr auto AUTHORITY = "authority"; |
| 43 | |
| 44 | // RAII support for openSSL functions. |
| 45 | using X509_Ptr = std::unique_ptr<X509, decltype(&::X509_free)>; |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 46 | using X509_STORE_CTX_Ptr = |
| 47 | std::unique_ptr<X509_STORE_CTX, decltype(&::X509_STORE_CTX_free)>; |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 48 | |
| 49 | /** @class Certificate |
| 50 | * @brief OpenBMC Certificate entry implementation. |
| 51 | * @details A concrete implementation for the |
| 52 | * xyz.openbmc_project.Certs.Certificate DBus API |
| 53 | * xyz.openbmc_project.Certs.Instal DBus API |
| 54 | */ |
Marri Devender Rao | edd1131 | 2019-02-27 08:45:10 -0600 | [diff] [blame] | 55 | class Certificate : public CertIfaces |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 56 | { |
| 57 | public: |
| 58 | Certificate() = delete; |
| 59 | Certificate(const Certificate&) = delete; |
| 60 | Certificate& operator=(const Certificate&) = delete; |
| 61 | Certificate(Certificate&&) = delete; |
| 62 | Certificate& operator=(Certificate&&) = delete; |
| 63 | virtual ~Certificate(); |
| 64 | |
| 65 | /** @brief Constructor for the Certificate Object |
| 66 | * @param[in] bus - Bus to attach to. |
| 67 | * @param[in] objPath - Object path to attach to |
| 68 | * @param[in] type - Type of the certificate |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 69 | * @param[in] installPath - Path of the certificate to install |
| 70 | * @param[in] uploadPath - Path of the certificate file to upload |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 71 | * @param[in] watchPtr - watch on self signed certificate pointer |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 72 | */ |
| 73 | Certificate(sdbusplus::bus::bus& bus, const std::string& objPath, |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 74 | const CertificateType& type, const CertInstallPath& installPath, |
| 75 | const CertUploadPath& uploadPath, const CertWatchPtr& watchPtr, |
| 76 | Manager& parent); |
| 77 | |
| 78 | /** @brief Validate and Replace/Install the certificate file |
| 79 | * Install/Replace the existing certificate file with another |
| 80 | * (possibly CA signed) Certificate file. |
| 81 | * @param[in] filePath - Certificate file path. |
| 82 | */ |
| 83 | void install(const std::string& filePath); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 84 | |
Marri Devender Rao | 13bf74e | 2019-03-26 01:52:17 -0500 | [diff] [blame] | 85 | /** @brief Validate certificate and replace the existing certificate |
| 86 | * @param[in] filePath - Certificate file path. |
| 87 | */ |
| 88 | void replace(const std::string filePath) override; |
| 89 | |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 90 | /** @brief Populate certificate properties by parsing certificate file |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 91 | */ |
| 92 | void populateProperties(); |
| 93 | |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 94 | /** |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 95 | * @brief Obtain certificate ID. |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 96 | * |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 97 | * @return Certificate ID. |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 98 | */ |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 99 | std::string getCertId() const; |
| 100 | |
| 101 | /** |
| 102 | * @brief Check if provied certificate is the same as the current one. |
| 103 | * |
| 104 | * @param[in] certPath - File path for certificate to check. |
| 105 | * |
| 106 | * @return Checking result. Return true if certificates are the same, |
| 107 | * false if not. |
| 108 | */ |
| 109 | bool isSame(const std::string& certPath); |
| 110 | |
| 111 | /** |
| 112 | * @brief Update certificate storage. |
| 113 | */ |
| 114 | void storageUpdate(); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 115 | |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 116 | /** |
| 117 | * @brief Delete the certificate |
| 118 | */ |
| 119 | void delete_() override; |
| 120 | |
Marri Devender Rao | 13bf74e | 2019-03-26 01:52:17 -0500 | [diff] [blame] | 121 | private: |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 122 | /** |
| 123 | * @brief Populate certificate properties by parsing given certificate file |
| 124 | * |
| 125 | * @param[in] certPath Path to certificate that should be parsed |
| 126 | * |
| 127 | * @return void |
| 128 | */ |
| 129 | void populateProperties(const std::string& certPath); |
| 130 | |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 131 | /** @brief Load Certificate file into the X509 structure. |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 132 | * @param[in] filePath - Certificate and key full file path. |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 133 | * @return pointer to the X509 structure. |
| 134 | */ |
| 135 | X509_Ptr loadCert(const std::string& filePath); |
| 136 | |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 137 | /** @brief Check and append private key to the certificate file |
| 138 | * If private key is not present in the certificate file append the |
| 139 | * certificate file with private key existing in the system. |
| 140 | * @param[in] filePath - Certificate and key full file path. |
| 141 | * @return void. |
| 142 | */ |
| 143 | void checkAndAppendPrivateKey(const std::string& filePath); |
| 144 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 145 | /** @brief Public/Private key compare function. |
| 146 | * Comparing private key against certificate public key |
| 147 | * from input .pem file. |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 148 | * @param[in] filePath - Certificate and key full file path. |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 149 | * @return Return true if Key compare is successful, |
| 150 | * false if not |
| 151 | */ |
| 152 | bool compareKeys(const std::string& filePath); |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 153 | |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 154 | /** |
| 155 | * @brief Generate certificate ID based on provided certificate file. |
| 156 | * |
| 157 | * @param[in] certPath - Certificate file path. |
| 158 | * |
| 159 | * @return Certificate ID as formatted string. |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 160 | */ |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 161 | std::string generateCertId(const std::string& certPath); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 162 | |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 163 | /** |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 164 | * @brief Generate file name which is unique in the provided directory. |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 165 | * |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 166 | * @param[in] directoryPath - Directory path. |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 167 | * |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 168 | * @return File path. |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 169 | */ |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 170 | std::string generateUniqueFilePath(const std::string& directoryPath); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 171 | |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 172 | /** |
| 173 | * @brief Generate authority certificate file path corresponding with |
| 174 | * OpenSSL requirements. |
| 175 | * |
| 176 | * Prepare authority certificate file path for provied certificate. |
| 177 | * OpenSSL puts some restrictions on the certificate file name pattern. |
| 178 | * Certificate full file name needs to consists of basic file name which |
| 179 | * is certificate subject name hash and file name extension which is an |
| 180 | * integer. More over, certificates files names extensions must be |
| 181 | * consecutive integer numbers in case many certificates with the same |
| 182 | * subject name. |
| 183 | * https://www.boost.org/doc/libs/1_69_0/doc/html/boost_asio/reference/ssl__context/add_verify_path.html |
| 184 | * https://www.openssl.org/docs/man1.0.2/man3/SSL_CTX_load_verify_locations.html |
| 185 | * |
| 186 | * @param[in] certSrcFilePath - Certificate source file path. |
| 187 | * @param[in] certDstDirPath - Certificate destination directory path. |
| 188 | * |
| 189 | * @return Authority certificate file path. |
| 190 | */ |
| 191 | std::string generateAuthCertFileX509Path(const std::string& certSrcFilePath, |
| 192 | const std::string& certDstDirPath); |
| 193 | |
| 194 | /** |
| 195 | * @brief Generate authority certificate file path based on provided |
| 196 | * certificate source file path. |
| 197 | * |
| 198 | * @param[in] certSrcFilePath - Certificate source file path. |
| 199 | * |
| 200 | * @return Authority certificate file path. |
| 201 | */ |
| 202 | std::string generateAuthCertFilePath(const std::string& certSrcFilePath); |
| 203 | |
| 204 | /** |
| 205 | * @brief Generate certificate file path based on provided certificate |
| 206 | * source file path. |
| 207 | * |
| 208 | * @param[in] certSrcFilePath - Certificate source file path. |
| 209 | * |
| 210 | * @return Certificate file path. |
| 211 | */ |
| 212 | std::string generateCertFilePath(const std::string& certSrcFilePath); |
| 213 | |
| 214 | /** @brief Type specific function pointer map */ |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 215 | std::unordered_map<InputType, InstallFunc> typeFuncMap; |
| 216 | |
| 217 | /** @brief sdbusplus handler */ |
| 218 | sdbusplus::bus::bus& bus; |
| 219 | |
| 220 | /** @brief object path */ |
| 221 | std::string objectPath; |
| 222 | |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 223 | /** @brief Type of the certificate */ |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 224 | CertificateType certType; |
| 225 | |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 226 | /** @brief Stores certificate ID */ |
| 227 | std::string certId; |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 228 | |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 229 | /** @brief Stores certificate file path */ |
| 230 | std::string certFilePath; |
| 231 | |
| 232 | /** @brief Certificate file installation path */ |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 233 | CertInstallPath certInstallPath; |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 234 | |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 235 | /** @brief Type specific function pointer map for appending private key */ |
| 236 | std::unordered_map<InputType, AppendPrivKeyFunc> appendKeyMap; |
| 237 | |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 238 | /** @brief Certificate file create/update watch */ |
| 239 | const CertWatchPtr& certWatchPtr; |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 240 | |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 241 | /** @brief Reference to Certificate Manager */ |
| 242 | Manager& manager; |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 243 | }; |
| 244 | |
| 245 | } // namespace certs |
| 246 | } // namespace phosphor |