Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 3 | #include "certificate.hpp" |
| 4 | |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 5 | #include "certs_manager.hpp" |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 6 | #include "x509_utils.hpp" |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 7 | |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 8 | #include <openssl/asn1.h> |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 9 | #include <openssl/bio.h> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 10 | #include <openssl/buffer.h> |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 11 | #include <openssl/err.h> |
| 12 | #include <openssl/evp.h> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 13 | #include <openssl/obj_mac.h> |
| 14 | #include <openssl/objects.h> |
| 15 | #include <openssl/opensslv.h> |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 16 | #include <openssl/pem.h> |
| 17 | #include <openssl/x509v3.h> |
| 18 | |
Patrick Williams | 223e460 | 2023-05-10 07:51:11 -0500 | [diff] [blame] | 19 | #include <phosphor-logging/elog-errors.hpp> |
| 20 | #include <phosphor-logging/elog.hpp> |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 21 | #include <phosphor-logging/lg2.hpp> |
Patrick Williams | 223e460 | 2023-05-10 07:51:11 -0500 | [diff] [blame] | 22 | #include <watch.hpp> |
| 23 | #include <xyz/openbmc_project/Certs/error.hpp> |
| 24 | #include <xyz/openbmc_project/Common/error.hpp> |
| 25 | |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 26 | #include <cstdint> |
| 27 | #include <cstdio> |
| 28 | #include <cstdlib> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 29 | #include <exception> |
| 30 | #include <filesystem> |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 31 | #include <fstream> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 32 | #include <map> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 33 | #include <utility> |
| 34 | #include <vector> |
Marri Devender Rao | 13bf74e | 2019-03-26 01:52:17 -0500 | [diff] [blame] | 35 | |
Nan Zhou | e1289ad | 2021-12-28 11:02:56 -0800 | [diff] [blame] | 36 | namespace phosphor::certs |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 37 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 38 | |
| 39 | namespace |
| 40 | { |
| 41 | namespace fs = std::filesystem; |
| 42 | using ::phosphor::logging::elog; |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 43 | using InvalidCertificateError = |
| 44 | ::sdbusplus::xyz::openbmc_project::Certs::Error::InvalidCertificate; |
| 45 | using ::phosphor::logging::xyz::openbmc_project::Certs::InvalidCertificate; |
| 46 | using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 47 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 48 | // RAII support for openSSL functions. |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 49 | using BIOMemPtr = std::unique_ptr<BIO, decltype(&::BIO_free)>; |
| 50 | using X509StorePtr = std::unique_ptr<X509_STORE, decltype(&::X509_STORE_free)>; |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 51 | using ASN1TimePtr = std::unique_ptr<ASN1_TIME, decltype(&ASN1_STRING_free)>; |
| 52 | using EVPPkeyPtr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>; |
| 53 | using BufMemPtr = std::unique_ptr<BUF_MEM, decltype(&::BUF_MEM_free)>; |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 54 | |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 55 | // Refer to schema 2018.3 |
| 56 | // http://redfish.dmtf.org/schemas/v1/Certificate.json#/definitions/KeyUsage for |
| 57 | // supported KeyUsage types in redfish |
| 58 | // Refer to |
| 59 | // https://github.com/openssl/openssl/blob/master/include/openssl/x509v3.h for |
| 60 | // key usage bit fields |
| 61 | std::map<uint8_t, std::string> keyUsageToRfStr = { |
| 62 | {KU_DIGITAL_SIGNATURE, "DigitalSignature"}, |
| 63 | {KU_NON_REPUDIATION, "NonRepudiation"}, |
| 64 | {KU_KEY_ENCIPHERMENT, "KeyEncipherment"}, |
| 65 | {KU_DATA_ENCIPHERMENT, "DataEncipherment"}, |
| 66 | {KU_KEY_AGREEMENT, "KeyAgreement"}, |
| 67 | {KU_KEY_CERT_SIGN, "KeyCertSign"}, |
| 68 | {KU_CRL_SIGN, "CRLSigning"}, |
| 69 | {KU_ENCIPHER_ONLY, "EncipherOnly"}, |
| 70 | {KU_DECIPHER_ONLY, "DecipherOnly"}}; |
| 71 | |
| 72 | // Refer to schema 2018.3 |
| 73 | // http://redfish.dmtf.org/schemas/v1/Certificate.json#/definitions/KeyUsage for |
| 74 | // supported Extended KeyUsage types in redfish |
| 75 | std::map<uint8_t, std::string> extendedKeyUsageToRfStr = { |
| 76 | {NID_server_auth, "ServerAuthentication"}, |
| 77 | {NID_client_auth, "ClientAuthentication"}, |
| 78 | {NID_email_protect, "EmailProtection"}, |
| 79 | {NID_OCSP_sign, "OCSPSigning"}, |
| 80 | {NID_ad_timeStamping, "Timestamping"}, |
| 81 | {NID_code_sign, "CodeSigning"}}; |
| 82 | |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 83 | /** |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 84 | * @brief Dumps the PEM encoded certificate to installFilePath |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 85 | * |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 86 | * @param[in] pem - PEM encoded X509 certificate buffer. |
| 87 | * @param[in] certFilePath - Path to the destination file. |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 88 | * |
| 89 | * @return void |
| 90 | */ |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 91 | |
| 92 | void dumpCertificate(const std::string& pem, const std::string& certFilePath) |
| 93 | { |
| 94 | std::ofstream outputCertFileStream; |
| 95 | |
| 96 | outputCertFileStream.exceptions( |
| 97 | std::ofstream::failbit | std::ofstream::badbit | std::ofstream::eofbit); |
| 98 | |
| 99 | try |
| 100 | { |
| 101 | outputCertFileStream.open(certFilePath, std::ios::out); |
| 102 | outputCertFileStream << pem << "\n" << std::flush; |
| 103 | outputCertFileStream.close(); |
| 104 | } |
| 105 | catch (const std::exception& e) |
| 106 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 107 | lg2::error( |
| 108 | "Failed to dump certificate, ERR:{ERR}, SRC_PEM:{SRC_PEM}, DST:{DST}", |
| 109 | "ERR", e, "SRC_PEM", pem, "DST", certFilePath); |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 110 | elog<InternalFailure>(); |
| 111 | } |
| 112 | } |
| 113 | } // namespace |
| 114 | |
| 115 | void Certificate::copyCertificate(const std::string& certSrcFilePath, |
| 116 | const std::string& certFilePath) |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 117 | { |
Jayanth Othayoth | f44a39c | 2024-11-25 01:46:47 -0600 | [diff] [blame] | 118 | try |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 119 | { |
Jayanth Othayoth | f44a39c | 2024-11-25 01:46:47 -0600 | [diff] [blame] | 120 | // Copy the certificate to the installation path |
| 121 | // During bootup will be parsing existing file so no need to |
| 122 | // copy it. |
| 123 | if (certSrcFilePath != certFilePath) |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 124 | { |
Jayanth Othayoth | f44a39c | 2024-11-25 01:46:47 -0600 | [diff] [blame] | 125 | fs::copy(certSrcFilePath, certFilePath, |
| 126 | fs::copy_options::overwrite_existing); |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 127 | } |
| 128 | } |
Jayanth Othayoth | f44a39c | 2024-11-25 01:46:47 -0600 | [diff] [blame] | 129 | catch (const fs::filesystem_error& e) |
| 130 | { |
| 131 | lg2::error( |
| 132 | "Failed to copy certificate, ERR:{ERR}, SRC:{SRC}, DST:{DST}", |
| 133 | "ERR", e.what(), "SRC", certSrcFilePath, "DST", certFilePath); |
| 134 | elog<InternalFailure>(); |
| 135 | } |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | std::string |
| 139 | Certificate::generateUniqueFilePath(const std::string& directoryPath) |
| 140 | { |
Milton D. Miller II | b627429 | 2024-11-29 11:26:30 +0000 | [diff] [blame^] | 141 | char* filePath = tempnam(directoryPath.c_str(), nullptr); |
| 142 | if (filePath == nullptr) |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 143 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 144 | lg2::error( |
| 145 | "Error occurred while creating random certificate file path, DIR:{DIR}", |
| 146 | "DIR", directoryPath); |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 147 | elog<InternalFailure>(); |
| 148 | } |
Milton D. Miller II | b627429 | 2024-11-29 11:26:30 +0000 | [diff] [blame^] | 149 | std::string filePathStr(filePath); |
| 150 | free(filePath); |
| 151 | return filePathStr; |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | std::string Certificate::generateAuthCertFileX509Path( |
| 155 | const std::string& certSrcFilePath, const std::string& certDstDirPath) |
| 156 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 157 | const internal::X509Ptr cert = loadCert(certSrcFilePath); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 158 | unsigned long hash = X509_subject_name_hash(cert.get()); |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 159 | static constexpr auto certHashLength = 9; |
| 160 | char hashBuf[certHashLength]; |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 161 | |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 162 | snprintf(hashBuf, certHashLength, "%08lx", hash); |
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 | const std::string certHash(hashBuf); |
Nan Zhou | 718eef3 | 2021-12-28 11:03:30 -0800 | [diff] [blame] | 165 | for (size_t i = 0; i < maxNumAuthorityCertificates; ++i) |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 166 | { |
Zbigniew Lukwinski | 73d1fbf | 2020-01-15 15:31:12 +0100 | [diff] [blame] | 167 | const std::string certDstFileX509Path = |
| 168 | certDstDirPath + "/" + certHash + "." + std::to_string(i); |
| 169 | if (!fs::exists(certDstFileX509Path)) |
| 170 | { |
| 171 | return certDstFileX509Path; |
| 172 | } |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 173 | } |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 174 | |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 175 | lg2::error("Authority certificate x509 file path already used, DIR:{DIR}", |
| 176 | "DIR", certDstDirPath); |
Zbigniew Lukwinski | 73d1fbf | 2020-01-15 15:31:12 +0100 | [diff] [blame] | 177 | elog<InternalFailure>(); |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | std::string |
| 181 | Certificate::generateAuthCertFilePath(const std::string& certSrcFilePath) |
| 182 | { |
| 183 | // If there is a certificate file path (which means certificate replacement |
| 184 | // is doing) use it (do not create new one) |
| 185 | if (!certFilePath.empty()) |
| 186 | { |
| 187 | return certFilePath; |
| 188 | } |
| 189 | // If source certificate file is located in the certificates directory use |
| 190 | // it (do not create new one) |
| 191 | else if (fs::path(certSrcFilePath).parent_path().string() == |
| 192 | certInstallPath) |
| 193 | { |
| 194 | return certSrcFilePath; |
| 195 | } |
| 196 | // Otherwise generate new file name/path |
| 197 | else |
| 198 | { |
| 199 | return generateUniqueFilePath(certInstallPath); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | std::string |
| 204 | Certificate::generateCertFilePath(const std::string& certSrcFilePath) |
| 205 | { |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 206 | if (certType == CertificateType::authority) |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 207 | { |
| 208 | return generateAuthCertFilePath(certSrcFilePath); |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | return certInstallPath; |
| 213 | } |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Patrick Williams | b3dbfb3 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 216 | Certificate::Certificate(sdbusplus::bus_t& bus, const std::string& objPath, |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 217 | CertificateType type, const std::string& installPath, |
| 218 | const std::string& uploadPath, Watch* watch, |
Willy Tu | 698a574 | 2022-09-23 21:33:01 +0000 | [diff] [blame] | 219 | Manager& parent, bool restore) : |
Patrick Williams | ebd21ba | 2022-04-05 14:58:53 -0500 | [diff] [blame] | 220 | internal::CertificateInterface( |
| 221 | bus, objPath.c_str(), |
| 222 | internal::CertificateInterface::action::defer_emit), |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 223 | objectPath(objPath), certType(type), certInstallPath(installPath), |
| 224 | certWatch(watch), manager(parent) |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 225 | { |
| 226 | auto installHelper = [this](const auto& filePath) { |
| 227 | if (!compareKeys(filePath)) |
| 228 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 229 | elog<InvalidCertificateError>(InvalidCertificate::REASON( |
| 230 | "Private key does not match the Certificate")); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 231 | }; |
| 232 | }; |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 233 | typeFuncMap[CertificateType::server] = installHelper; |
| 234 | typeFuncMap[CertificateType::client] = installHelper; |
| 235 | typeFuncMap[CertificateType::authority] = [](const std::string&) {}; |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 236 | |
| 237 | auto appendPrivateKey = [this](const std::string& filePath) { |
| 238 | checkAndAppendPrivateKey(filePath); |
| 239 | }; |
| 240 | |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 241 | appendKeyMap[CertificateType::server] = appendPrivateKey; |
| 242 | appendKeyMap[CertificateType::client] = appendPrivateKey; |
| 243 | appendKeyMap[CertificateType::authority] = [](const std::string&) {}; |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 244 | |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 245 | // Generate certificate file path |
| 246 | certFilePath = generateCertFilePath(uploadPath); |
| 247 | |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 248 | // install the certificate |
Willy Tu | 698a574 | 2022-09-23 21:33:01 +0000 | [diff] [blame] | 249 | install(uploadPath, restore); |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 250 | |
Marri Devender Rao | edd1131 | 2019-02-27 08:45:10 -0600 | [diff] [blame] | 251 | this->emit_object_added(); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 252 | } |
| 253 | |
Patrick Williams | b3dbfb3 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 254 | Certificate::Certificate(sdbusplus::bus_t& bus, const std::string& objPath, |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 255 | const CertificateType& type, |
| 256 | const std::string& installPath, X509_STORE& x509Store, |
| 257 | const std::string& pem, Watch* watchPtr, |
Willy Tu | 698a574 | 2022-09-23 21:33:01 +0000 | [diff] [blame] | 258 | Manager& parent, bool restore) : |
Patrick Williams | ebd21ba | 2022-04-05 14:58:53 -0500 | [diff] [blame] | 259 | internal::CertificateInterface( |
| 260 | bus, objPath.c_str(), |
| 261 | internal::CertificateInterface::action::defer_emit), |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 262 | objectPath(objPath), certType(type), certInstallPath(installPath), |
| 263 | certWatch(watchPtr), manager(parent) |
| 264 | { |
| 265 | // Generate certificate file path |
| 266 | certFilePath = generateUniqueFilePath(installPath); |
| 267 | |
| 268 | // install the certificate |
Willy Tu | 698a574 | 2022-09-23 21:33:01 +0000 | [diff] [blame] | 269 | install(x509Store, pem, restore); |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 270 | |
| 271 | this->emit_object_added(); |
| 272 | } |
| 273 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 274 | Certificate::~Certificate() |
| 275 | { |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 276 | if (!fs::remove(certFilePath)) |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 277 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 278 | lg2::info("Certificate file not found! PATH:{PATH}", "PATH", |
| 279 | certFilePath); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
Marri Devender Rao | 13bf74e | 2019-03-26 01:52:17 -0500 | [diff] [blame] | 283 | void Certificate::replace(const std::string filePath) |
| 284 | { |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 285 | manager.replaceCertificate(this, filePath); |
Marri Devender Rao | 13bf74e | 2019-03-26 01:52:17 -0500 | [diff] [blame] | 286 | } |
| 287 | |
Willy Tu | 698a574 | 2022-09-23 21:33:01 +0000 | [diff] [blame] | 288 | void Certificate::install(const std::string& certSrcFilePath, bool restore) |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 289 | { |
Willy Tu | 698a574 | 2022-09-23 21:33:01 +0000 | [diff] [blame] | 290 | if (restore) |
| 291 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 292 | lg2::debug("Certificate install, FILEPATH:{FILEPATH}", "FILEPATH", |
| 293 | certSrcFilePath); |
Willy Tu | 698a574 | 2022-09-23 21:33:01 +0000 | [diff] [blame] | 294 | } |
| 295 | else |
| 296 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 297 | lg2::info("Certificate install, FILEPATH:{FILEPATH}", "FILEPATH", |
| 298 | certSrcFilePath); |
Willy Tu | 698a574 | 2022-09-23 21:33:01 +0000 | [diff] [blame] | 299 | } |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 300 | |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 301 | // stop watch for user initiated certificate install |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 302 | if (certWatch != nullptr) |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 303 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 304 | certWatch->stopWatch(); |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 305 | } |
| 306 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 307 | // Verify the certificate file |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 308 | fs::path file(certSrcFilePath); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 309 | if (!fs::exists(file)) |
| 310 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 311 | lg2::error("File is Missing, FILE:{FILE}", "FILE", certSrcFilePath); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 312 | elog<InternalFailure>(); |
| 313 | } |
| 314 | |
| 315 | try |
| 316 | { |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 317 | if (fs::file_size(certSrcFilePath) == 0) |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 318 | { |
| 319 | // file is empty |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 320 | lg2::error("File is empty, FILE:{FILE}", "FILE", certSrcFilePath); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 321 | elog<InvalidCertificateError>( |
| 322 | InvalidCertificate::REASON("File is empty")); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | catch (const fs::filesystem_error& e) |
| 326 | { |
| 327 | // Log Error message |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 328 | lg2::error("File is empty, FILE:{FILE}, ERR:{ERR}", "FILE", |
| 329 | certSrcFilePath, "ERR", e); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 330 | elog<InternalFailure>(); |
| 331 | } |
| 332 | |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 333 | X509StorePtr x509Store = getX509Store(certSrcFilePath); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 334 | |
Nan Zhou | bf3cf75 | 2021-12-28 11:02:07 -0800 | [diff] [blame] | 335 | // Load Certificate file into the X509 structure. |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 336 | internal::X509Ptr cert = loadCert(certSrcFilePath); |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 337 | |
| 338 | // Perform validation |
| 339 | validateCertificateAgainstStore(*x509Store, *cert); |
| 340 | validateCertificateStartDate(*cert); |
| 341 | validateCertificateInSSLContext(*cert); |
| 342 | |
| 343 | // Invoke type specific append private key function. |
| 344 | if (auto it = appendKeyMap.find(certType); it == appendKeyMap.end()) |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 345 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 346 | lg2::error("Unsupported Type, TYPE:{TYPE}", "TYPE", |
| 347 | certificateTypeToString(certType)); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 348 | elog<InternalFailure>(); |
| 349 | } |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 350 | else |
| 351 | { |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 352 | it->second(certSrcFilePath); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 353 | } |
| 354 | |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 355 | // Invoke type specific compare keys function. |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 356 | if (auto it = typeFuncMap.find(certType); it == typeFuncMap.end()) |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 357 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 358 | lg2::error("Unsupported Type, TYPE:{TYPE}", "TYPE", |
| 359 | certificateTypeToString(certType)); |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 360 | elog<InternalFailure>(); |
| 361 | } |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 362 | else |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 363 | { |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 364 | it->second(certSrcFilePath); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 365 | } |
Marri Devender Rao | 8f80c35 | 2019-05-13 00:53:01 -0500 | [diff] [blame] | 366 | |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 367 | copyCertificate(certSrcFilePath, certFilePath); |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 368 | storageUpdate(); |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 369 | |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 370 | // Keep certificate ID |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 371 | certId = generateCertId(*cert); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 372 | |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 373 | // Parse the certificate file and populate properties |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 374 | populateProperties(*cert); |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 375 | |
| 376 | // restart watch |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 377 | if (certWatch != nullptr) |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 378 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 379 | certWatch->startWatch(); |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 380 | } |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 381 | } |
| 382 | |
Willy Tu | 698a574 | 2022-09-23 21:33:01 +0000 | [diff] [blame] | 383 | void Certificate::install(X509_STORE& x509Store, const std::string& pem, |
| 384 | bool restore) |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 385 | { |
Willy Tu | 698a574 | 2022-09-23 21:33:01 +0000 | [diff] [blame] | 386 | if (restore) |
| 387 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 388 | lg2::debug("Certificate install, PEM_STR:{PEM_STR}", "PEM_STR", pem); |
Willy Tu | 698a574 | 2022-09-23 21:33:01 +0000 | [diff] [blame] | 389 | } |
| 390 | else |
| 391 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 392 | lg2::info("Certificate install, PEM_STR:{PEM_STR} ", "PEM_STR", pem); |
Willy Tu | 698a574 | 2022-09-23 21:33:01 +0000 | [diff] [blame] | 393 | } |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 394 | |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 395 | if (certType != CertificateType::authority) |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 396 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 397 | lg2::error("Bulk install error: Unsupported Type; only authority " |
| 398 | "supports bulk install, TYPE:{TYPE}", |
| 399 | "TYPE", certificateTypeToString(certType)); |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 400 | elog<InternalFailure>(); |
| 401 | } |
| 402 | |
| 403 | // stop watch for user initiated certificate install |
| 404 | if (certWatch) |
| 405 | { |
| 406 | certWatch->stopWatch(); |
| 407 | } |
| 408 | |
| 409 | // Load Certificate file into the X509 structure. |
| 410 | internal::X509Ptr cert = parseCert(pem); |
| 411 | // Perform validation; no type specific compare keys function |
| 412 | validateCertificateAgainstStore(x509Store, *cert); |
| 413 | validateCertificateStartDate(*cert); |
| 414 | validateCertificateInSSLContext(*cert); |
| 415 | |
| 416 | // Copy the PEM to the installation path |
| 417 | dumpCertificate(pem, certFilePath); |
| 418 | storageUpdate(); |
| 419 | // Keep certificate ID |
| 420 | certId = generateCertId(*cert); |
| 421 | // Parse the certificate file and populate properties |
| 422 | populateProperties(*cert); |
| 423 | // restart watch |
| 424 | if (certWatch) |
| 425 | { |
| 426 | certWatch->startWatch(); |
| 427 | } |
| 428 | } |
| 429 | |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 430 | void Certificate::populateProperties() |
| 431 | { |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 432 | internal::X509Ptr cert = loadCert(certInstallPath); |
| 433 | populateProperties(*cert); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 434 | } |
| 435 | |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 436 | std::string Certificate::getCertId() const |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 437 | { |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 438 | return certId; |
| 439 | } |
| 440 | |
| 441 | bool Certificate::isSame(const std::string& certPath) |
| 442 | { |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 443 | internal::X509Ptr cert = loadCert(certPath); |
| 444 | return getCertId() == generateCertId(*cert); |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | void Certificate::storageUpdate() |
| 448 | { |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 449 | if (certType == CertificateType::authority) |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 450 | { |
| 451 | // Create symbolic link in the certificate directory |
| 452 | std::string certFileX509Path; |
| 453 | try |
| 454 | { |
| 455 | if (!certFilePath.empty() && |
| 456 | fs::is_regular_file(fs::path(certFilePath))) |
| 457 | { |
| 458 | certFileX509Path = |
| 459 | generateAuthCertFileX509Path(certFilePath, certInstallPath); |
| 460 | fs::create_symlink(fs::path(certFilePath), |
| 461 | fs::path(certFileX509Path)); |
| 462 | } |
| 463 | } |
| 464 | catch (const std::exception& e) |
| 465 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 466 | lg2::error("Failed to create symlink for certificate, ERR:{ERR}," |
| 467 | "FILE:{FILE}, SYMLINK:{SYMLINK}", |
| 468 | "ERR", e, "FILE", certFilePath, "SYMLINK", |
| 469 | certFileX509Path); |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 470 | elog<InternalFailure>(); |
| 471 | } |
| 472 | } |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 473 | } |
| 474 | |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 475 | void Certificate::populateProperties(X509& cert) |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 476 | { |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 477 | // Update properties if no error thrown |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 478 | BIOMemPtr certBio(BIO_new(BIO_s_mem()), BIO_free); |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 479 | PEM_write_bio_X509(certBio.get(), &cert); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 480 | BufMemPtr certBuf(BUF_MEM_new(), BUF_MEM_free); |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 481 | BUF_MEM* buf = certBuf.get(); |
| 482 | BIO_get_mem_ptr(certBio.get(), &buf); |
| 483 | std::string certStr(buf->data, buf->length); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 484 | certificateString(certStr); |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 485 | |
| 486 | static const int maxKeySize = 4096; |
| 487 | char subBuffer[maxKeySize] = {0}; |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 488 | BIOMemPtr subBio(BIO_new(BIO_s_mem()), BIO_free); |
Manojkiran Eda | 5d4f793 | 2024-06-17 11:49:21 +0530 | [diff] [blame] | 489 | // This pointer cannot be freed independently. |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 490 | X509_NAME* sub = X509_get_subject_name(&cert); |
Marri Devender Rao | dec5877 | 2019-06-11 03:10:00 -0500 | [diff] [blame] | 491 | X509_NAME_print_ex(subBio.get(), sub, 0, XN_FLAG_SEP_COMMA_PLUS); |
| 492 | BIO_read(subBio.get(), subBuffer, maxKeySize); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 493 | subject(subBuffer); |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 494 | |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 495 | char issuerBuffer[maxKeySize] = {0}; |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 496 | BIOMemPtr issuerBio(BIO_new(BIO_s_mem()), BIO_free); |
Manojkiran Eda | 5d4f793 | 2024-06-17 11:49:21 +0530 | [diff] [blame] | 497 | // This pointer cannot be freed independently. |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 498 | X509_NAME* issuerName = X509_get_issuer_name(&cert); |
| 499 | X509_NAME_print_ex(issuerBio.get(), issuerName, 0, XN_FLAG_SEP_COMMA_PLUS); |
Marri Devender Rao | dec5877 | 2019-06-11 03:10:00 -0500 | [diff] [blame] | 500 | BIO_read(issuerBio.get(), issuerBuffer, maxKeySize); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 501 | issuer(issuerBuffer); |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 502 | |
| 503 | std::vector<std::string> keyUsageList; |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 504 | |
| 505 | // Go through each usage in the bit string and convert to |
| 506 | // corresponding string value |
Jayanth Othayoth | cb1ee9d | 2024-11-24 22:23:33 -0600 | [diff] [blame] | 507 | ASN1_BIT_STRING* usage = static_cast<ASN1_BIT_STRING*>( |
| 508 | X509_get_ext_d2i(&cert, NID_key_usage, nullptr, nullptr)); |
| 509 | if (usage != nullptr) |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 510 | { |
| 511 | for (auto i = 0; i < usage->length; ++i) |
| 512 | { |
| 513 | for (auto& x : keyUsageToRfStr) |
| 514 | { |
| 515 | if (x.first & usage->data[i]) |
| 516 | { |
| 517 | keyUsageList.push_back(x.second); |
| 518 | break; |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | } |
| 523 | |
Jayanth Othayoth | cb1ee9d | 2024-11-24 22:23:33 -0600 | [diff] [blame] | 524 | EXTENDED_KEY_USAGE* extUsage = static_cast<EXTENDED_KEY_USAGE*>( |
| 525 | X509_get_ext_d2i(&cert, NID_ext_key_usage, nullptr, nullptr)); |
| 526 | if (extUsage == nullptr) |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 527 | { |
| 528 | for (int i = 0; i < sk_ASN1_OBJECT_num(extUsage); i++) |
| 529 | { |
| 530 | keyUsageList.push_back(extendedKeyUsageToRfStr[OBJ_obj2nid( |
| 531 | sk_ASN1_OBJECT_value(extUsage, i))]); |
| 532 | } |
| 533 | } |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 534 | keyUsage(keyUsageList); |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 535 | |
| 536 | int days = 0; |
| 537 | int secs = 0; |
| 538 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 539 | ASN1TimePtr epoch(ASN1_TIME_new(), ASN1_STRING_free); |
Nan Zhou | cf811c4 | 2021-12-02 14:56:17 -0800 | [diff] [blame] | 540 | // Set time to 00:00am GMT, Jan 1 1970; format: YYYYMMDDHHMMSSZ |
| 541 | ASN1_TIME_set_string(epoch.get(), "19700101000000Z"); |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 542 | |
Jayanth Othayoth | 8a59ea2 | 2024-11-24 23:14:10 -0600 | [diff] [blame] | 543 | constexpr uint64_t dayToSeconds = 86400; // 24 * 60 * 60 |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 544 | ASN1_TIME* notAfter = X509_get_notAfter(&cert); |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 545 | ASN1_TIME_diff(&days, &secs, epoch.get(), notAfter); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 546 | validNotAfter((days * dayToSeconds) + secs); |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 547 | |
Nan Zhou | e869bb6 | 2021-12-30 11:34:42 -0800 | [diff] [blame] | 548 | ASN1_TIME* notBefore = X509_get_notBefore(&cert); |
Dhruvaraj Subhashchandran | 36f2514 | 2019-02-14 05:06:26 -0600 | [diff] [blame] | 549 | ASN1_TIME_diff(&days, &secs, epoch.get(), notBefore); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 550 | validNotBefore((days * dayToSeconds) + secs); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 551 | } |
| 552 | |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 553 | void Certificate::checkAndAppendPrivateKey(const std::string& filePath) |
| 554 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 555 | BIOMemPtr keyBio(BIO_new(BIO_s_file()), ::BIO_free); |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 556 | if (!keyBio) |
| 557 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 558 | lg2::error("Error occurred during BIO_s_file call, FILE:{FILE}", "FILE", |
| 559 | filePath); |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 560 | elog<InternalFailure>(); |
| 561 | } |
| 562 | BIO_read_filename(keyBio.get(), filePath.c_str()); |
| 563 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 564 | EVPPkeyPtr priKey( |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 565 | PEM_read_bio_PrivateKey(keyBio.get(), nullptr, nullptr, nullptr), |
| 566 | ::EVP_PKEY_free); |
| 567 | if (!priKey) |
| 568 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 569 | lg2::info("Private key not present in file, FILE:{FILE}", "FILE", |
| 570 | filePath); |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 571 | fs::path privateKeyFile = fs::path(certInstallPath).parent_path(); |
Nan Zhou | 718eef3 | 2021-12-28 11:03:30 -0800 | [diff] [blame] | 572 | privateKeyFile = privateKeyFile / defaultPrivateKeyFileName; |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 573 | if (!fs::exists(privateKeyFile)) |
| 574 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 575 | lg2::error("Private key file is not found, FILE:{FILE}", "FILE", |
| 576 | privateKeyFile); |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 577 | elog<InternalFailure>(); |
| 578 | } |
| 579 | |
| 580 | std::ifstream privKeyFileStream; |
| 581 | std::ofstream certFileStream; |
Patrick Williams | a2f68d8 | 2024-08-16 15:21:36 -0400 | [diff] [blame] | 582 | privKeyFileStream.exceptions( |
| 583 | std::ifstream::failbit | std::ifstream::badbit | |
| 584 | std::ifstream::eofbit); |
| 585 | certFileStream.exceptions( |
| 586 | std::ofstream::failbit | std::ofstream::badbit | |
| 587 | std::ofstream::eofbit); |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 588 | try |
| 589 | { |
| 590 | privKeyFileStream.open(privateKeyFile); |
| 591 | certFileStream.open(filePath, std::ios::app); |
Marri Devender Rao | 18e51c9 | 2019-07-15 04:59:01 -0500 | [diff] [blame] | 592 | certFileStream << std::endl; // insert line break |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 593 | certFileStream << privKeyFileStream.rdbuf() << std::flush; |
| 594 | privKeyFileStream.close(); |
| 595 | certFileStream.close(); |
| 596 | } |
| 597 | catch (const std::exception& e) |
| 598 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 599 | lg2::error( |
| 600 | "Failed to append private key, ERR:{ERR}, SRC:{SRC}, DST:{DST}", |
| 601 | "ERR", e, "SRC", privateKeyFile, "DST", filePath); |
Marri Devender Rao | cd30c49 | 2019-06-12 01:40:17 -0500 | [diff] [blame] | 602 | elog<InternalFailure>(); |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 607 | bool Certificate::compareKeys(const std::string& filePath) |
| 608 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 609 | lg2::info("Certificate compareKeys, FILEPATH:{FILEPATH}", "FILEPATH", |
| 610 | filePath); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 611 | internal::X509Ptr cert(X509_new(), ::X509_free); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 612 | if (!cert) |
| 613 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 614 | lg2::error( |
| 615 | "Error occurred during X509_new call, FILE:{FILE}, ERRCODE:{ERRCODE}", |
| 616 | "FILE", filePath, "ERRCODE", ERR_get_error()); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 617 | elog<InternalFailure>(); |
| 618 | } |
| 619 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 620 | BIOMemPtr bioCert(BIO_new_file(filePath.c_str(), "rb"), ::BIO_free); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 621 | if (!bioCert) |
| 622 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 623 | lg2::error("Error occurred during BIO_new_file call, FILE:{FILE}", |
| 624 | "FILE", filePath); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 625 | elog<InternalFailure>(); |
| 626 | } |
| 627 | |
| 628 | X509* x509 = cert.get(); |
| 629 | PEM_read_bio_X509(bioCert.get(), &x509, nullptr, nullptr); |
| 630 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 631 | EVPPkeyPtr pubKey(X509_get_pubkey(cert.get()), ::EVP_PKEY_free); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 632 | if (!pubKey) |
| 633 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 634 | lg2::error( |
| 635 | "Error occurred during X509_get_pubkey, FILE:{FILE}, ERRCODE:{ERRCODE}", |
| 636 | "FILE", filePath, "ERRCODE", ERR_get_error()); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 637 | elog<InvalidCertificateError>( |
| 638 | InvalidCertificate::REASON("Failed to get public key info")); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 639 | } |
| 640 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 641 | BIOMemPtr keyBio(BIO_new(BIO_s_file()), ::BIO_free); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 642 | if (!keyBio) |
| 643 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 644 | lg2::error("Error occurred during BIO_s_file call, FILE:{FILE}", "FILE", |
| 645 | filePath); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 646 | elog<InternalFailure>(); |
| 647 | } |
| 648 | BIO_read_filename(keyBio.get(), filePath.c_str()); |
| 649 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 650 | EVPPkeyPtr priKey( |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 651 | PEM_read_bio_PrivateKey(keyBio.get(), nullptr, nullptr, nullptr), |
| 652 | ::EVP_PKEY_free); |
| 653 | if (!priKey) |
| 654 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 655 | lg2::error( |
| 656 | "Error occurred during PEM_read_bio_PrivateKey, FILE:{FILE}, ERRCODE:{ERRCODE}", |
| 657 | "FILE", filePath, "ERRCODE", ERR_get_error()); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 658 | elog<InvalidCertificateError>( |
| 659 | InvalidCertificate::REASON("Failed to get private key info")); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 660 | } |
| 661 | |
Patrick Williams | 55ceaa2 | 2021-12-14 06:52:26 -0600 | [diff] [blame] | 662 | #if (OPENSSL_VERSION_NUMBER < 0x30000000L) |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 663 | int32_t rc = EVP_PKEY_cmp(priKey.get(), pubKey.get()); |
Patrick Williams | 55ceaa2 | 2021-12-14 06:52:26 -0600 | [diff] [blame] | 664 | #else |
| 665 | int32_t rc = EVP_PKEY_eq(priKey.get(), pubKey.get()); |
| 666 | #endif |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 667 | if (rc != 1) |
| 668 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 669 | lg2::error( |
| 670 | "Private key is not matching with Certificate, FILE:{FILE}, ERRCODE:{ERRCODE}", |
| 671 | "FILE", filePath, "ERRCODE", rc); |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 672 | return false; |
| 673 | } |
| 674 | return true; |
| 675 | } |
| 676 | |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 677 | void Certificate::delete_() |
| 678 | { |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 679 | manager.deleteCertificate(this); |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 680 | } |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 681 | |
| 682 | std::string Certificate::getObjectPath() |
| 683 | { |
| 684 | return objectPath; |
| 685 | } |
| 686 | |
| 687 | std::string Certificate::getCertFilePath() |
| 688 | { |
| 689 | return certFilePath; |
| 690 | } |
| 691 | |
| 692 | void Certificate::setCertFilePath(const std::string& path) |
| 693 | { |
| 694 | certFilePath = path; |
| 695 | } |
| 696 | |
| 697 | void Certificate::setCertInstallPath(const std::string& path) |
| 698 | { |
| 699 | certInstallPath = path; |
| 700 | } |
| 701 | |
Nan Zhou | e1289ad | 2021-12-28 11:02:56 -0800 | [diff] [blame] | 702 | } // namespace phosphor::certs |