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