Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 3 | #include "certs_manager.hpp" |
| 4 | |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 5 | #include "x509_utils.hpp" |
| 6 | |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 7 | #include <openssl/asn1.h> |
| 8 | #include <openssl/bn.h> |
| 9 | #include <openssl/ec.h> |
Andrew Geissler | 8dbcc72 | 2023-12-08 10:15:42 -0600 | [diff] [blame^] | 10 | #include <openssl/err.h> |
Patrick Williams | 26fb83e | 2021-12-14 14:08:28 -0600 | [diff] [blame] | 11 | #include <openssl/evp.h> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 12 | #include <openssl/obj_mac.h> |
| 13 | #include <openssl/objects.h> |
| 14 | #include <openssl/opensslv.h> |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 15 | #include <openssl/pem.h> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 16 | #include <openssl/rsa.h> |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 17 | #include <unistd.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 <sdbusplus/bus.hpp> |
| 23 | #include <sdbusplus/exception.hpp> |
| 24 | #include <sdbusplus/message.hpp> |
| 25 | #include <sdeventplus/source/base.hpp> |
| 26 | #include <sdeventplus/source/child.hpp> |
| 27 | #include <xyz/openbmc_project/Certs/error.hpp> |
| 28 | #include <xyz/openbmc_project/Common/error.hpp> |
| 29 | |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 30 | #include <algorithm> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 31 | #include <array> |
| 32 | #include <cerrno> |
| 33 | #include <chrono> |
| 34 | #include <csignal> |
| 35 | #include <cstdio> |
| 36 | #include <cstdlib> |
| 37 | #include <cstring> |
| 38 | #include <exception> |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 39 | #include <fstream> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 40 | #include <utility> |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 41 | |
Nan Zhou | e1289ad | 2021-12-28 11:02:56 -0800 | [diff] [blame] | 42 | namespace phosphor::certs |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 43 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 44 | namespace |
| 45 | { |
| 46 | namespace fs = std::filesystem; |
| 47 | using ::phosphor::logging::commit; |
| 48 | using ::phosphor::logging::elog; |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 49 | using ::phosphor::logging::report; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 50 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 51 | using ::sdbusplus::xyz::openbmc_project::Certs::Error::InvalidCertificate; |
| 52 | using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
| 53 | using ::sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed; |
| 54 | using NotAllowedReason = |
| 55 | ::phosphor::logging::xyz::openbmc_project::Common::NotAllowed::REASON; |
| 56 | using InvalidCertificateReason = ::phosphor::logging::xyz::openbmc_project:: |
| 57 | Certs::InvalidCertificate::REASON; |
| 58 | using ::sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument; |
| 59 | using Argument = |
| 60 | ::phosphor::logging::xyz::openbmc_project::Common::InvalidArgument; |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 61 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 62 | // RAII support for openSSL functions. |
| 63 | using X509ReqPtr = std::unique_ptr<X509_REQ, decltype(&::X509_REQ_free)>; |
| 64 | using EVPPkeyPtr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>; |
| 65 | using BignumPtr = std::unique_ptr<BIGNUM, decltype(&::BN_free)>; |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 66 | using X509StorePtr = std::unique_ptr<X509_STORE, decltype(&::X509_STORE_free)>; |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 67 | |
| 68 | constexpr int supportedKeyBitLength = 2048; |
| 69 | constexpr int defaultKeyBitLength = 2048; |
| 70 | // secp224r1 is equal to RSA 2048 KeyBitLength. Refer RFC 5349 |
| 71 | constexpr auto defaultKeyCurveID = "secp224r1"; |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 72 | // PEM certificate block markers, defined in go/rfc/7468. |
| 73 | constexpr std::string_view beginCertificate = "-----BEGIN CERTIFICATE-----"; |
| 74 | constexpr std::string_view endCertificate = "-----END CERTIFICATE-----"; |
| 75 | |
| 76 | /** |
| 77 | * @brief Splits the given authorities list file and returns an array of |
| 78 | * individual PEM encoded x509 certificate. |
| 79 | * |
| 80 | * @param[in] sourceFilePath - Path to the authorities list file. |
| 81 | * |
| 82 | * @return An array of individual PEM encoded x509 certificate |
| 83 | */ |
| 84 | std::vector<std::string> splitCertificates(const std::string& sourceFilePath) |
| 85 | { |
| 86 | std::ifstream inputCertFileStream; |
| 87 | inputCertFileStream.exceptions( |
| 88 | std::ifstream::failbit | std::ifstream::badbit | std::ifstream::eofbit); |
| 89 | |
| 90 | std::stringstream pemStream; |
| 91 | std::vector<std::string> certificatesList; |
| 92 | try |
| 93 | { |
| 94 | inputCertFileStream.open(sourceFilePath); |
| 95 | pemStream << inputCertFileStream.rdbuf(); |
| 96 | inputCertFileStream.close(); |
| 97 | } |
| 98 | catch (const std::exception& e) |
| 99 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 100 | lg2::error("Failed to read certificates list, ERR:{ERR}, SRC:{SRC}", |
| 101 | "ERR", e, "SRC", sourceFilePath); |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 102 | elog<InternalFailure>(); |
| 103 | } |
| 104 | std::string pem = pemStream.str(); |
| 105 | size_t begin = 0; |
| 106 | // |begin| points to the current start position for searching the next |
| 107 | // |beginCertificate| block. When we find the beginning of the certificate, |
| 108 | // we extract the content between the beginning and the end of the current |
| 109 | // certificate. And finally we move |begin| to the end of the current |
| 110 | // certificate to start searching the next potential certificate. |
| 111 | for (begin = pem.find(beginCertificate, begin); begin != std::string::npos; |
| 112 | begin = pem.find(beginCertificate, begin)) |
| 113 | { |
| 114 | size_t end = pem.find(endCertificate, begin); |
| 115 | if (end == std::string::npos) |
| 116 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 117 | lg2::error( |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 118 | "invalid PEM contains a BEGIN identifier without an END"); |
| 119 | elog<InvalidCertificate>(InvalidCertificateReason( |
| 120 | "invalid PEM contains a BEGIN identifier without an END")); |
| 121 | } |
| 122 | end += endCertificate.size(); |
| 123 | certificatesList.emplace_back(pem.substr(begin, end - begin)); |
| 124 | begin = end; |
| 125 | } |
| 126 | return certificatesList; |
| 127 | } |
| 128 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 129 | } // namespace |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 130 | |
Patrick Williams | b3dbfb3 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 131 | Manager::Manager(sdbusplus::bus_t& bus, sdeventplus::Event& event, |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 132 | const char* path, CertificateType type, |
| 133 | const std::string& unit, const std::string& installPath) : |
| 134 | internal::ManagerInterface(bus, path), |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 135 | bus(bus), event(event), objectPath(path), certType(type), |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 136 | unitToRestart(std::move(unit)), certInstallPath(std::move(installPath)), |
| 137 | certParentInstallPath(fs::path(certInstallPath).parent_path()) |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 138 | { |
Marri Devender Rao | b57d75e | 2019-07-25 04:56:21 -0500 | [diff] [blame] | 139 | try |
| 140 | { |
Marri Devender Rao | db5c6fc | 2020-03-10 13:27:49 -0500 | [diff] [blame] | 141 | // Create certificate directory if not existing. |
Nan Zhou | bf3cf75 | 2021-12-28 11:02:07 -0800 | [diff] [blame] | 142 | // Set correct certificate directory permissions. |
Marri Devender Rao | db5c6fc | 2020-03-10 13:27:49 -0500 | [diff] [blame] | 143 | fs::path certDirectory; |
| 144 | try |
Marri Devender Rao | b57d75e | 2019-07-25 04:56:21 -0500 | [diff] [blame] | 145 | { |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 146 | if (certType == CertificateType::authority) |
Marri Devender Rao | db5c6fc | 2020-03-10 13:27:49 -0500 | [diff] [blame] | 147 | { |
| 148 | certDirectory = certInstallPath; |
| 149 | } |
| 150 | else |
| 151 | { |
| 152 | certDirectory = certParentInstallPath; |
| 153 | } |
| 154 | |
| 155 | if (!fs::exists(certDirectory)) |
| 156 | { |
| 157 | fs::create_directories(certDirectory); |
| 158 | } |
| 159 | |
| 160 | auto permission = fs::perms::owner_read | fs::perms::owner_write | |
| 161 | fs::perms::owner_exec; |
| 162 | fs::permissions(certDirectory, permission, |
| 163 | fs::perm_options::replace); |
| 164 | storageUpdate(); |
| 165 | } |
Patrick Williams | 7195799 | 2021-10-06 14:42:52 -0500 | [diff] [blame] | 166 | catch (const fs::filesystem_error& e) |
Marri Devender Rao | db5c6fc | 2020-03-10 13:27:49 -0500 | [diff] [blame] | 167 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 168 | lg2::error( |
| 169 | "Failed to create directory, ERR:{ERR}, DIRECTORY:{DIRECTORY}", |
| 170 | "ERR", e, "DIRECTORY", certParentInstallPath); |
Marri Devender Rao | db5c6fc | 2020-03-10 13:27:49 -0500 | [diff] [blame] | 171 | report<InternalFailure>(); |
| 172 | } |
| 173 | |
| 174 | // Generating RSA private key file if certificate type is server/client |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 175 | if (certType != CertificateType::authority) |
Marri Devender Rao | db5c6fc | 2020-03-10 13:27:49 -0500 | [diff] [blame] | 176 | { |
| 177 | createRSAPrivateKeyFile(); |
| 178 | } |
| 179 | |
| 180 | // restore any existing certificates |
| 181 | createCertificates(); |
| 182 | |
| 183 | // watch is not required for authority certificates |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 184 | if (certType != CertificateType::authority) |
Marri Devender Rao | db5c6fc | 2020-03-10 13:27:49 -0500 | [diff] [blame] | 185 | { |
| 186 | // watch for certificate file create/replace |
Patrick Williams | 223e460 | 2023-05-10 07:51:11 -0500 | [diff] [blame] | 187 | certWatchPtr = std::make_unique<Watch>(event, certInstallPath, |
| 188 | [this]() { |
Marri Devender Rao | db5c6fc | 2020-03-10 13:27:49 -0500 | [diff] [blame] | 189 | try |
| 190 | { |
| 191 | // if certificate file existing update it |
| 192 | if (!installedCerts.empty()) |
| 193 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 194 | lg2::info("Inotify callback to update " |
| 195 | "certificate properties"); |
Marri Devender Rao | db5c6fc | 2020-03-10 13:27:49 -0500 | [diff] [blame] | 196 | installedCerts[0]->populateProperties(); |
| 197 | } |
| 198 | else |
| 199 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 200 | lg2::info( |
Marri Devender Rao | db5c6fc | 2020-03-10 13:27:49 -0500 | [diff] [blame] | 201 | "Inotify callback to create certificate object"); |
| 202 | createCertificates(); |
| 203 | } |
| 204 | } |
| 205 | catch (const InternalFailure& e) |
| 206 | { |
| 207 | commit<InternalFailure>(); |
| 208 | } |
| 209 | catch (const InvalidCertificate& e) |
| 210 | { |
| 211 | commit<InvalidCertificate>(); |
| 212 | } |
| 213 | }); |
Marri Devender Rao | b57d75e | 2019-07-25 04:56:21 -0500 | [diff] [blame] | 214 | } |
Zbigniew Lukwinski | fe590c4 | 2019-12-10 12:33:50 +0100 | [diff] [blame] | 215 | else |
| 216 | { |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 217 | try |
| 218 | { |
Marri Devender Rao | db5c6fc | 2020-03-10 13:27:49 -0500 | [diff] [blame] | 219 | const std::string singleCertPath = "/etc/ssl/certs/Root-CA.pem"; |
| 220 | if (fs::exists(singleCertPath) && !fs::is_empty(singleCertPath)) |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 221 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 222 | lg2::notice( |
| 223 | "Legacy certificate detected, will be installed from," |
| 224 | "SINGLE_CERTPATH:{SINGLE_CERTPATH}", |
| 225 | "SINGLE_CERTPATH", singleCertPath); |
Marri Devender Rao | db5c6fc | 2020-03-10 13:27:49 -0500 | [diff] [blame] | 226 | install(singleCertPath); |
| 227 | if (!fs::remove(singleCertPath)) |
| 228 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 229 | lg2::error("Unable to remove old certificate from," |
| 230 | "SINGLE_CERTPATH:{SINGLE_CERTPATH}", |
| 231 | "SINGLE_CERTPATH", singleCertPath); |
Marri Devender Rao | db5c6fc | 2020-03-10 13:27:49 -0500 | [diff] [blame] | 232 | elog<InternalFailure>(); |
| 233 | } |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 234 | } |
| 235 | } |
Marri Devender Rao | db5c6fc | 2020-03-10 13:27:49 -0500 | [diff] [blame] | 236 | catch (const std::exception& ex) |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 237 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 238 | lg2::error( |
| 239 | "Error in restoring legacy certificate, ERROR_STR:{ERROR_STR}", |
| 240 | "ERROR_STR", ex); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | } |
Patrick Williams | 7195799 | 2021-10-06 14:42:52 -0500 | [diff] [blame] | 244 | catch (const std::exception& ex) |
Marri Devender Rao | db5c6fc | 2020-03-10 13:27:49 -0500 | [diff] [blame] | 245 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 246 | lg2::error( |
| 247 | "Error in certificate manager constructor, ERROR_STR:{ERROR_STR}", |
| 248 | "ERROR_STR", ex); |
Marri Devender Rao | db5c6fc | 2020-03-10 13:27:49 -0500 | [diff] [blame] | 249 | } |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 250 | } |
| 251 | |
Zbigniew Kurzynski | 06a69d7 | 2019-09-27 10:57:38 +0200 | [diff] [blame] | 252 | std::string Manager::install(const std::string filePath) |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 253 | { |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 254 | if (certType != CertificateType::authority && !installedCerts.empty()) |
Marri Devender Rao | 1396511 | 2019-02-27 08:47:12 -0600 | [diff] [blame] | 255 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 256 | elog<NotAllowed>(NotAllowedReason("Certificate already exist")); |
Marri Devender Rao | 1396511 | 2019-02-27 08:47:12 -0600 | [diff] [blame] | 257 | } |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 258 | else if (certType == CertificateType::authority && |
Nan Zhou | 718eef3 | 2021-12-28 11:03:30 -0800 | [diff] [blame] | 259 | installedCerts.size() >= maxNumAuthorityCertificates) |
Zbigniew Lukwinski | 3b07b77 | 2019-10-09 11:43:34 +0200 | [diff] [blame] | 260 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 261 | elog<NotAllowed>(NotAllowedReason("Certificates limit reached")); |
Zbigniew Lukwinski | 3b07b77 | 2019-10-09 11:43:34 +0200 | [diff] [blame] | 262 | } |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 263 | |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 264 | std::string certObjectPath; |
| 265 | if (isCertificateUnique(filePath)) |
| 266 | { |
| 267 | certObjectPath = objectPath + '/' + std::to_string(certIdCounter); |
| 268 | installedCerts.emplace_back(std::make_unique<Certificate>( |
| 269 | bus, certObjectPath, certType, certInstallPath, filePath, |
Willy Tu | 698a574 | 2022-09-23 21:33:01 +0000 | [diff] [blame] | 270 | certWatchPtr.get(), *this, /*restore=*/false)); |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 271 | reloadOrReset(unitToRestart); |
| 272 | certIdCounter++; |
| 273 | } |
| 274 | else |
| 275 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 276 | elog<NotAllowed>(NotAllowedReason("Certificate already exist")); |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 277 | } |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 278 | |
Zbigniew Kurzynski | 06a69d7 | 2019-09-27 10:57:38 +0200 | [diff] [blame] | 279 | return certObjectPath; |
Jayanth Othayoth | 589159f | 2018-09-28 08:32:39 -0500 | [diff] [blame] | 280 | } |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 281 | |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 282 | std::vector<sdbusplus::message::object_path> |
| 283 | Manager::installAll(const std::string filePath) |
| 284 | { |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 285 | if (certType != CertificateType::authority) |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 286 | { |
| 287 | elog<NotAllowed>( |
| 288 | NotAllowedReason("The InstallAll interface is only allowed for " |
| 289 | "Authority certificates")); |
| 290 | } |
| 291 | |
| 292 | if (!installedCerts.empty()) |
| 293 | { |
| 294 | elog<NotAllowed>(NotAllowedReason( |
| 295 | "There are already root certificates; Call DeleteAll then " |
| 296 | "InstallAll, or use ReplaceAll")); |
| 297 | } |
| 298 | |
| 299 | fs::path sourceFile(filePath); |
| 300 | if (!fs::exists(sourceFile)) |
| 301 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 302 | lg2::error("File is Missing, FILE:{FILE}", "FILE", filePath); |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 303 | elog<InternalFailure>(); |
| 304 | } |
| 305 | std::vector<std::string> authorities = splitCertificates(sourceFile); |
| 306 | if (authorities.size() > maxNumAuthorityCertificates) |
| 307 | { |
| 308 | elog<NotAllowed>(NotAllowedReason("Certificates limit reached")); |
| 309 | } |
| 310 | |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 311 | lg2::info("Starts authority list install"); |
Nan Zhou | 78357b0 | 2022-06-09 22:33:35 +0000 | [diff] [blame] | 312 | |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 313 | fs::path authorityStore(certInstallPath); |
Patrick Williams | 223e460 | 2023-05-10 07:51:11 -0500 | [diff] [blame] | 314 | fs::path authoritiesListFile = authorityStore / |
| 315 | defaultAuthoritiesListFileName; |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 316 | |
| 317 | // Atomically install all the certificates |
| 318 | fs::path tempPath = Certificate::generateUniqueFilePath(authorityStore); |
| 319 | fs::create_directory(tempPath); |
| 320 | // Copies the authorities list |
| 321 | Certificate::copyCertificate(sourceFile, |
| 322 | tempPath / defaultAuthoritiesListFileName); |
| 323 | std::vector<std::unique_ptr<Certificate>> tempCertificates; |
| 324 | uint64_t tempCertIdCounter = certIdCounter; |
| 325 | X509StorePtr x509Store = getX509Store(sourceFile); |
| 326 | for (const auto& authority : authorities) |
| 327 | { |
Patrick Williams | 223e460 | 2023-05-10 07:51:11 -0500 | [diff] [blame] | 328 | std::string certObjectPath = objectPath + '/' + |
| 329 | std::to_string(tempCertIdCounter); |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 330 | tempCertificates.emplace_back(std::make_unique<Certificate>( |
| 331 | bus, certObjectPath, certType, tempPath, *x509Store, authority, |
Willy Tu | 698a574 | 2022-09-23 21:33:01 +0000 | [diff] [blame] | 332 | certWatchPtr.get(), *this, /*restore=*/false)); |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 333 | tempCertIdCounter++; |
| 334 | } |
| 335 | |
| 336 | // We are good now, issue swap |
| 337 | installedCerts = std::move(tempCertificates); |
| 338 | certIdCounter = tempCertIdCounter; |
| 339 | // Rename all the certificates including the authorities list |
| 340 | for (const fs::path& f : fs::directory_iterator(tempPath)) |
| 341 | { |
| 342 | if (fs::is_symlink(f)) |
| 343 | { |
| 344 | continue; |
| 345 | } |
| 346 | fs::rename(/*from=*/f, /*to=*/certInstallPath / f.filename()); |
| 347 | } |
| 348 | // Update file locations and create symbol links |
| 349 | for (const auto& cert : installedCerts) |
| 350 | { |
| 351 | cert->setCertInstallPath(certInstallPath); |
| 352 | cert->setCertFilePath(certInstallPath / |
| 353 | fs::path(cert->getCertFilePath()).filename()); |
| 354 | cert->storageUpdate(); |
| 355 | } |
| 356 | // Remove the temporary folder |
| 357 | fs::remove_all(tempPath); |
| 358 | |
| 359 | std::vector<sdbusplus::message::object_path> objects; |
| 360 | for (const auto& certificate : installedCerts) |
| 361 | { |
| 362 | objects.emplace_back(certificate->getObjectPath()); |
| 363 | } |
| 364 | |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 365 | lg2::info("Finishes authority list install; reload units starts"); |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 366 | reloadOrReset(unitToRestart); |
| 367 | return objects; |
| 368 | } |
| 369 | |
| 370 | std::vector<sdbusplus::message::object_path> |
| 371 | Manager::replaceAll(std::string filePath) |
| 372 | { |
| 373 | installedCerts.clear(); |
| 374 | certIdCounter = 1; |
| 375 | storageUpdate(); |
| 376 | return installAll(std::move(filePath)); |
| 377 | } |
| 378 | |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 379 | void Manager::deleteAll() |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 380 | { |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 381 | // TODO: #Issue 4 when a certificate is deleted system auto generates |
| 382 | // certificate file. At present we are not supporting creation of |
| 383 | // certificate object for the auto-generated certificate file as |
| 384 | // deletion if only applicable for REST server and Bmcweb does not allow |
| 385 | // deletion of certificates |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 386 | installedCerts.clear(); |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 387 | // If the authorities list exists, delete it as well |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 388 | if (certType == CertificateType::authority) |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 389 | { |
Patrick Williams | 223e460 | 2023-05-10 07:51:11 -0500 | [diff] [blame] | 390 | if (fs::path authoritiesList = fs::path(certInstallPath) / |
| 391 | defaultAuthoritiesListFileName; |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 392 | fs::exists(authoritiesList)) |
| 393 | { |
| 394 | fs::remove(authoritiesList); |
| 395 | } |
| 396 | } |
| 397 | certIdCounter = 1; |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 398 | storageUpdate(); |
| 399 | reloadOrReset(unitToRestart); |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 400 | } |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 401 | |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 402 | void Manager::deleteCertificate(const Certificate* const certificate) |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 403 | { |
Patrick Williams | 223e460 | 2023-05-10 07:51:11 -0500 | [diff] [blame] | 404 | const std::vector<std::unique_ptr<Certificate>>::iterator& certIt = |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 405 | std::find_if(installedCerts.begin(), installedCerts.end(), |
Patrick Williams | 223e460 | 2023-05-10 07:51:11 -0500 | [diff] [blame] | 406 | [certificate](const std::unique_ptr<Certificate>& cert) { |
| 407 | return (cert.get() == certificate); |
Patrick Williams | d96b81c | 2023-10-20 11:19:39 -0500 | [diff] [blame] | 408 | }); |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 409 | if (certIt != installedCerts.end()) |
| 410 | { |
| 411 | installedCerts.erase(certIt); |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 412 | storageUpdate(); |
| 413 | reloadOrReset(unitToRestart); |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 414 | } |
| 415 | else |
| 416 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 417 | lg2::error("Certificate does not exist, ID:{ID}", "ID", |
| 418 | certificate->getCertId()); |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 419 | elog<InternalFailure>(); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | void Manager::replaceCertificate(Certificate* const certificate, |
| 424 | const std::string& filePath) |
| 425 | { |
| 426 | if (isCertificateUnique(filePath, certificate)) |
| 427 | { |
Willy Tu | 698a574 | 2022-09-23 21:33:01 +0000 | [diff] [blame] | 428 | certificate->install(filePath, false); |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 429 | storageUpdate(); |
| 430 | reloadOrReset(unitToRestart); |
| 431 | } |
| 432 | else |
| 433 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 434 | elog<NotAllowed>(NotAllowedReason("Certificate already exist")); |
Zbigniew Kurzynski | a3bb38f | 2019-09-17 13:34:25 +0200 | [diff] [blame] | 435 | } |
| 436 | } |
| 437 | |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 438 | std::string Manager::generateCSR( |
| 439 | std::vector<std::string> alternativeNames, std::string challengePassword, |
| 440 | std::string city, std::string commonName, std::string contactPerson, |
| 441 | std::string country, std::string email, std::string givenName, |
| 442 | std::string initials, int64_t keyBitLength, std::string keyCurveId, |
| 443 | std::string keyPairAlgorithm, std::vector<std::string> keyUsage, |
| 444 | std::string organization, std::string organizationalUnit, std::string state, |
| 445 | std::string surname, std::string unstructuredName) |
| 446 | { |
| 447 | // We support only one CSR. |
| 448 | csrPtr.reset(nullptr); |
| 449 | auto pid = fork(); |
| 450 | if (pid == -1) |
| 451 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 452 | lg2::error("Error occurred during forking process"); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 453 | report<InternalFailure>(); |
| 454 | } |
| 455 | else if (pid == 0) |
| 456 | { |
| 457 | try |
| 458 | { |
| 459 | generateCSRHelper(alternativeNames, challengePassword, city, |
| 460 | commonName, contactPerson, country, email, |
| 461 | givenName, initials, keyBitLength, keyCurveId, |
| 462 | keyPairAlgorithm, keyUsage, organization, |
| 463 | organizationalUnit, state, surname, |
| 464 | unstructuredName); |
| 465 | exit(EXIT_SUCCESS); |
| 466 | } |
| 467 | catch (const InternalFailure& e) |
| 468 | { |
| 469 | // commit the error reported in child process and exit |
| 470 | // Callback method from SDEvent Loop looks for exit status |
| 471 | exit(EXIT_FAILURE); |
| 472 | commit<InternalFailure>(); |
| 473 | } |
Ramesh Iyyar | d2393f2 | 2020-10-29 09:46:51 -0500 | [diff] [blame] | 474 | catch (const InvalidArgument& e) |
| 475 | { |
| 476 | // commit the error reported in child process and exit |
| 477 | // Callback method from SDEvent Loop looks for exit status |
| 478 | exit(EXIT_FAILURE); |
| 479 | commit<InvalidArgument>(); |
| 480 | } |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 481 | } |
| 482 | else |
| 483 | { |
| 484 | using namespace sdeventplus::source; |
Patrick Williams | d96b81c | 2023-10-20 11:19:39 -0500 | [diff] [blame] | 485 | Child::Callback callback = [this](Child& eventSource, |
| 486 | const siginfo_t* si) { |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 487 | eventSource.set_enabled(Enabled::On); |
| 488 | if (si->si_status != 0) |
| 489 | { |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 490 | this->createCSRObject(Status::failure); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 491 | } |
| 492 | else |
| 493 | { |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 494 | this->createCSRObject(Status::success); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 495 | } |
| 496 | }; |
| 497 | try |
| 498 | { |
| 499 | sigset_t ss; |
| 500 | if (sigemptyset(&ss) < 0) |
| 501 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 502 | lg2::error("Unable to initialize signal set"); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 503 | elog<InternalFailure>(); |
| 504 | } |
| 505 | if (sigaddset(&ss, SIGCHLD) < 0) |
| 506 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 507 | lg2::error("Unable to add signal to signal set"); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 508 | elog<InternalFailure>(); |
| 509 | } |
| 510 | |
| 511 | // Block SIGCHLD first, so that the event loop can handle it |
Nan Zhou | cfb5802 | 2021-12-28 11:02:26 -0800 | [diff] [blame] | 512 | if (sigprocmask(SIG_BLOCK, &ss, nullptr) < 0) |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 513 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 514 | lg2::error("Unable to block signal"); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 515 | elog<InternalFailure>(); |
| 516 | } |
| 517 | if (childPtr) |
| 518 | { |
| 519 | childPtr.reset(); |
| 520 | } |
| 521 | childPtr = std::make_unique<Child>(event, pid, WEXITED | WSTOPPED, |
| 522 | std::move(callback)); |
| 523 | } |
| 524 | catch (const InternalFailure& e) |
| 525 | { |
| 526 | commit<InternalFailure>(); |
| 527 | } |
| 528 | } |
| 529 | auto csrObjectPath = objectPath + '/' + "csr"; |
| 530 | return csrObjectPath; |
| 531 | } |
| 532 | |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 533 | std::vector<std::unique_ptr<Certificate>>& Manager::getCertificates() |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 534 | { |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 535 | return installedCerts; |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 536 | } |
| 537 | |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 538 | void Manager::generateCSRHelper( |
| 539 | std::vector<std::string> alternativeNames, std::string challengePassword, |
| 540 | std::string city, std::string commonName, std::string contactPerson, |
| 541 | std::string country, std::string email, std::string givenName, |
| 542 | std::string initials, int64_t keyBitLength, std::string keyCurveId, |
| 543 | std::string keyPairAlgorithm, std::vector<std::string> keyUsage, |
| 544 | std::string organization, std::string organizationalUnit, std::string state, |
| 545 | std::string surname, std::string unstructuredName) |
| 546 | { |
| 547 | int ret = 0; |
| 548 | |
| 549 | // set version of x509 req |
| 550 | int nVersion = 1; |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 551 | X509ReqPtr x509Req(X509_REQ_new(), ::X509_REQ_free); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 552 | ret = X509_REQ_set_version(x509Req.get(), nVersion); |
| 553 | if (ret == 0) |
| 554 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 555 | lg2::error("Error occurred during X509_REQ_set_version call"); |
Andrew Geissler | 8dbcc72 | 2023-12-08 10:15:42 -0600 | [diff] [blame^] | 556 | ERR_print_errors_fp(stderr); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 557 | elog<InternalFailure>(); |
| 558 | } |
| 559 | |
| 560 | // set subject of x509 req |
| 561 | X509_NAME* x509Name = X509_REQ_get_subject_name(x509Req.get()); |
| 562 | |
| 563 | if (!alternativeNames.empty()) |
| 564 | { |
| 565 | for (auto& name : alternativeNames) |
| 566 | { |
| 567 | addEntry(x509Name, "subjectAltName", name); |
| 568 | } |
| 569 | } |
| 570 | addEntry(x509Name, "challengePassword", challengePassword); |
| 571 | addEntry(x509Name, "L", city); |
| 572 | addEntry(x509Name, "CN", commonName); |
| 573 | addEntry(x509Name, "name", contactPerson); |
| 574 | addEntry(x509Name, "C", country); |
| 575 | addEntry(x509Name, "emailAddress", email); |
| 576 | addEntry(x509Name, "GN", givenName); |
| 577 | addEntry(x509Name, "initials", initials); |
| 578 | addEntry(x509Name, "algorithm", keyPairAlgorithm); |
| 579 | if (!keyUsage.empty()) |
| 580 | { |
| 581 | for (auto& usage : keyUsage) |
| 582 | { |
Marri Devender Rao | 7641105 | 2019-08-07 01:25:07 -0500 | [diff] [blame] | 583 | if (isExtendedKeyUsage(usage)) |
| 584 | { |
| 585 | addEntry(x509Name, "extendedKeyUsage", usage); |
| 586 | } |
| 587 | else |
| 588 | { |
| 589 | addEntry(x509Name, "keyUsage", usage); |
| 590 | } |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 591 | } |
| 592 | } |
| 593 | addEntry(x509Name, "O", organization); |
Jayanth Othayoth | dc91fb6 | 2021-05-04 23:17:47 -0500 | [diff] [blame] | 594 | addEntry(x509Name, "OU", organizationalUnit); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 595 | addEntry(x509Name, "ST", state); |
| 596 | addEntry(x509Name, "SN", surname); |
| 597 | addEntry(x509Name, "unstructuredName", unstructuredName); |
| 598 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 599 | EVPPkeyPtr pKey(nullptr, ::EVP_PKEY_free); |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 600 | |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 601 | lg2::info("Given Key pair algorithm, KEYPAIRALGORITHM:{KEYPAIRALGORITHM}", |
| 602 | "KEYPAIRALGORITHM", keyPairAlgorithm); |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 603 | |
| 604 | // Used EC algorithm as default if user did not give algorithm type. |
| 605 | if (keyPairAlgorithm == "RSA") |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 606 | pKey = getRSAKeyPair(keyBitLength); |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 607 | else if ((keyPairAlgorithm == "EC") || (keyPairAlgorithm.empty())) |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 608 | pKey = generateECKeyPair(keyCurveId); |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 609 | else |
| 610 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 611 | lg2::error("Given Key pair algorithm is not supported. Supporting " |
| 612 | "RSA and EC only"); |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 613 | elog<InvalidArgument>( |
| 614 | Argument::ARGUMENT_NAME("KEYPAIRALGORITHM"), |
| 615 | Argument::ARGUMENT_VALUE(keyPairAlgorithm.c_str())); |
| 616 | } |
| 617 | |
| 618 | ret = X509_REQ_set_pubkey(x509Req.get(), pKey.get()); |
| 619 | if (ret == 0) |
| 620 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 621 | lg2::error("Error occurred while setting Public key"); |
Andrew Geissler | 8dbcc72 | 2023-12-08 10:15:42 -0600 | [diff] [blame^] | 622 | ERR_print_errors_fp(stderr); |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 623 | elog<InternalFailure>(); |
| 624 | } |
| 625 | |
| 626 | // Write private key to file |
Nan Zhou | 718eef3 | 2021-12-28 11:03:30 -0800 | [diff] [blame] | 627 | writePrivateKey(pKey, defaultPrivateKeyFileName); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 628 | |
| 629 | // set sign key of x509 req |
| 630 | ret = X509_REQ_sign(x509Req.get(), pKey.get(), EVP_sha256()); |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 631 | if (ret == 0) |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 632 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 633 | lg2::error("Error occurred while signing key of x509"); |
Andrew Geissler | 8dbcc72 | 2023-12-08 10:15:42 -0600 | [diff] [blame^] | 634 | ERR_print_errors_fp(stderr); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 635 | elog<InternalFailure>(); |
| 636 | } |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 637 | |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 638 | lg2::info("Writing CSR to file"); |
Nan Zhou | 718eef3 | 2021-12-28 11:03:30 -0800 | [diff] [blame] | 639 | fs::path csrFilePath = certParentInstallPath / defaultCSRFileName; |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 640 | writeCSR(csrFilePath.string(), x509Req); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 641 | } |
| 642 | |
Marri Devender Rao | 7641105 | 2019-08-07 01:25:07 -0500 | [diff] [blame] | 643 | bool Manager::isExtendedKeyUsage(const std::string& usage) |
| 644 | { |
| 645 | const static std::array<const char*, 6> usageList = { |
| 646 | "ServerAuthentication", "ClientAuthentication", "OCSPSigning", |
| 647 | "Timestamping", "CodeSigning", "EmailProtection"}; |
Patrick Williams | d96b81c | 2023-10-20 11:19:39 -0500 | [diff] [blame] | 648 | auto it = std::find_if( |
| 649 | usageList.begin(), usageList.end(), |
| 650 | [&usage](const char* s) { return (strcmp(s, usage.c_str()) == 0); }); |
Marri Devender Rao | 7641105 | 2019-08-07 01:25:07 -0500 | [diff] [blame] | 651 | return it != usageList.end(); |
| 652 | } |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 653 | EVPPkeyPtr Manager::generateRSAKeyPair(const int64_t keyBitLength) |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 654 | { |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 655 | int64_t keyBitLen = keyBitLength; |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 656 | // set keybit length to default value if not set |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 657 | if (keyBitLen <= 0) |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 658 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 659 | lg2::info("KeyBitLength is not given.Hence, using default KeyBitLength:" |
| 660 | "{DEFAULTKEYBITLENGTH}", |
| 661 | "DEFAULTKEYBITLENGTH", defaultKeyBitLength); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 662 | keyBitLen = defaultKeyBitLength; |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 663 | } |
Patrick Williams | 26fb83e | 2021-12-14 14:08:28 -0600 | [diff] [blame] | 664 | |
| 665 | #if (OPENSSL_VERSION_NUMBER < 0x30000000L) |
| 666 | |
| 667 | // generate rsa key |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 668 | BignumPtr bne(BN_new(), ::BN_free); |
Patrick Williams | 26fb83e | 2021-12-14 14:08:28 -0600 | [diff] [blame] | 669 | auto ret = BN_set_word(bne.get(), RSA_F4); |
| 670 | if (ret == 0) |
| 671 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 672 | lg2::error("Error occurred during BN_set_word call"); |
Andrew Geissler | 8dbcc72 | 2023-12-08 10:15:42 -0600 | [diff] [blame^] | 673 | ERR_print_errors_fp(stderr); |
Patrick Williams | 26fb83e | 2021-12-14 14:08:28 -0600 | [diff] [blame] | 674 | elog<InternalFailure>(); |
| 675 | } |
Nan Zhou | 762da74 | 2022-01-14 17:21:44 -0800 | [diff] [blame] | 676 | using RSAPtr = std::unique_ptr<RSA, decltype(&::RSA_free)>; |
| 677 | RSAPtr rsa(RSA_new(), ::RSA_free); |
| 678 | ret = RSA_generate_key_ex(rsa.get(), keyBitLen, bne.get(), nullptr); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 679 | if (ret != 1) |
| 680 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 681 | lg2::error( |
| 682 | "Error occurred during RSA_generate_key_ex call: {KEYBITLENGTH}", |
| 683 | "KEYBITLENGTH", keyBitLen); |
Andrew Geissler | 8dbcc72 | 2023-12-08 10:15:42 -0600 | [diff] [blame^] | 684 | ERR_print_errors_fp(stderr); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 685 | elog<InternalFailure>(); |
| 686 | } |
| 687 | |
| 688 | // set public key of x509 req |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 689 | EVPPkeyPtr pKey(EVP_PKEY_new(), ::EVP_PKEY_free); |
Nan Zhou | 762da74 | 2022-01-14 17:21:44 -0800 | [diff] [blame] | 690 | ret = EVP_PKEY_assign_RSA(pKey.get(), rsa.get()); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 691 | if (ret == 0) |
| 692 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 693 | lg2::error("Error occurred during assign rsa key into EVP"); |
Andrew Geissler | 8dbcc72 | 2023-12-08 10:15:42 -0600 | [diff] [blame^] | 694 | ERR_print_errors_fp(stderr); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 695 | elog<InternalFailure>(); |
| 696 | } |
Nan Zhou | 762da74 | 2022-01-14 17:21:44 -0800 | [diff] [blame] | 697 | // Now |rsa| is managed by |pKey| |
| 698 | rsa.release(); |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 699 | return pKey; |
Patrick Williams | 26fb83e | 2021-12-14 14:08:28 -0600 | [diff] [blame] | 700 | |
| 701 | #else |
| 702 | auto ctx = std::unique_ptr<EVP_PKEY_CTX, decltype(&::EVP_PKEY_CTX_free)>( |
| 703 | EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, nullptr), &::EVP_PKEY_CTX_free); |
| 704 | if (!ctx) |
| 705 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 706 | lg2::error("Error occurred creating EVP_PKEY_CTX from algorithm"); |
Andrew Geissler | 8dbcc72 | 2023-12-08 10:15:42 -0600 | [diff] [blame^] | 707 | ERR_print_errors_fp(stderr); |
Patrick Williams | 26fb83e | 2021-12-14 14:08:28 -0600 | [diff] [blame] | 708 | elog<InternalFailure>(); |
| 709 | } |
| 710 | |
| 711 | if ((EVP_PKEY_keygen_init(ctx.get()) <= 0) || |
| 712 | (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx.get(), keyBitLen) <= 0)) |
| 713 | |
| 714 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 715 | lg2::error("Error occurred initializing keygen context"); |
Andrew Geissler | 8dbcc72 | 2023-12-08 10:15:42 -0600 | [diff] [blame^] | 716 | ERR_print_errors_fp(stderr); |
Patrick Williams | 26fb83e | 2021-12-14 14:08:28 -0600 | [diff] [blame] | 717 | elog<InternalFailure>(); |
| 718 | } |
| 719 | |
| 720 | EVP_PKEY* pKey = nullptr; |
| 721 | if (EVP_PKEY_keygen(ctx.get(), &pKey) <= 0) |
| 722 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 723 | lg2::error("Error occurred during generate EC key"); |
Andrew Geissler | 8dbcc72 | 2023-12-08 10:15:42 -0600 | [diff] [blame^] | 724 | ERR_print_errors_fp(stderr); |
Patrick Williams | 26fb83e | 2021-12-14 14:08:28 -0600 | [diff] [blame] | 725 | elog<InternalFailure>(); |
| 726 | } |
| 727 | |
| 728 | return {pKey, &::EVP_PKEY_free}; |
| 729 | #endif |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 730 | } |
| 731 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 732 | EVPPkeyPtr Manager::generateECKeyPair(const std::string& curveId) |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 733 | { |
| 734 | std::string curId(curveId); |
| 735 | |
| 736 | if (curId.empty()) |
| 737 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 738 | lg2::info("KeyCurveId is not given. Hence using default curve id," |
| 739 | "DEFAULTKEYCURVEID:{DEFAULTKEYCURVEID}", |
| 740 | "DEFAULTKEYCURVEID", defaultKeyCurveID); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 741 | curId = defaultKeyCurveID; |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | int ecGrp = OBJ_txt2nid(curId.c_str()); |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 745 | if (ecGrp == NID_undef) |
| 746 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 747 | lg2::error( |
| 748 | "Error occurred during convert the curve id string format into NID," |
| 749 | "KEYCURVEID:{KEYCURVEID}", |
| 750 | "KEYCURVEID", curId); |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 751 | elog<InternalFailure>(); |
| 752 | } |
| 753 | |
Patrick Williams | 26fb83e | 2021-12-14 14:08:28 -0600 | [diff] [blame] | 754 | #if (OPENSSL_VERSION_NUMBER < 0x30000000L) |
| 755 | |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 756 | EC_KEY* ecKey = EC_KEY_new_by_curve_name(ecGrp); |
| 757 | |
Nan Zhou | cfb5802 | 2021-12-28 11:02:26 -0800 | [diff] [blame] | 758 | if (ecKey == nullptr) |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 759 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 760 | lg2::error( |
| 761 | "Error occurred during create the EC_Key object from NID, ECGROUP:{ECGROUP}", |
| 762 | "ECGROUP", ecGrp); |
Andrew Geissler | 8dbcc72 | 2023-12-08 10:15:42 -0600 | [diff] [blame^] | 763 | ERR_print_errors_fp(stderr); |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 764 | elog<InternalFailure>(); |
| 765 | } |
| 766 | |
| 767 | // If you want to save a key and later load it with |
| 768 | // SSL_CTX_use_PrivateKey_file, then you must set the OPENSSL_EC_NAMED_CURVE |
| 769 | // flag on the key. |
| 770 | EC_KEY_set_asn1_flag(ecKey, OPENSSL_EC_NAMED_CURVE); |
| 771 | |
| 772 | int ret = EC_KEY_generate_key(ecKey); |
| 773 | |
| 774 | if (ret == 0) |
| 775 | { |
| 776 | EC_KEY_free(ecKey); |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 777 | lg2::error("Error occurred during generate EC key"); |
Andrew Geissler | 8dbcc72 | 2023-12-08 10:15:42 -0600 | [diff] [blame^] | 778 | ERR_print_errors_fp(stderr); |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 779 | elog<InternalFailure>(); |
| 780 | } |
| 781 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 782 | EVPPkeyPtr pKey(EVP_PKEY_new(), ::EVP_PKEY_free); |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 783 | ret = EVP_PKEY_assign_EC_KEY(pKey.get(), ecKey); |
| 784 | if (ret == 0) |
| 785 | { |
| 786 | EC_KEY_free(ecKey); |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 787 | lg2::error("Error occurred during assign EC Key into EVP"); |
Andrew Geissler | 8dbcc72 | 2023-12-08 10:15:42 -0600 | [diff] [blame^] | 788 | ERR_print_errors_fp(stderr); |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 789 | elog<InternalFailure>(); |
| 790 | } |
| 791 | |
| 792 | return pKey; |
Patrick Williams | 26fb83e | 2021-12-14 14:08:28 -0600 | [diff] [blame] | 793 | |
| 794 | #else |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 795 | auto holderOfKey = [](EVP_PKEY* key) { |
Patrick Williams | 26fb83e | 2021-12-14 14:08:28 -0600 | [diff] [blame] | 796 | return std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>{ |
| 797 | key, &::EVP_PKEY_free}; |
| 798 | }; |
| 799 | |
| 800 | // Create context to set up curve parameters. |
| 801 | auto ctx = std::unique_ptr<EVP_PKEY_CTX, decltype(&::EVP_PKEY_CTX_free)>( |
| 802 | EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr), &::EVP_PKEY_CTX_free); |
| 803 | if (!ctx) |
| 804 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 805 | lg2::error("Error occurred creating EVP_PKEY_CTX for params"); |
Andrew Geissler | 8dbcc72 | 2023-12-08 10:15:42 -0600 | [diff] [blame^] | 806 | ERR_print_errors_fp(stderr); |
Patrick Williams | 26fb83e | 2021-12-14 14:08:28 -0600 | [diff] [blame] | 807 | elog<InternalFailure>(); |
| 808 | } |
| 809 | |
| 810 | // Set up curve parameters. |
| 811 | EVP_PKEY* params = nullptr; |
| 812 | |
| 813 | if ((EVP_PKEY_paramgen_init(ctx.get()) <= 0) || |
| 814 | (EVP_PKEY_CTX_set_ec_param_enc(ctx.get(), OPENSSL_EC_NAMED_CURVE) <= |
| 815 | 0) || |
| 816 | (EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx.get(), ecGrp) <= 0) || |
| 817 | (EVP_PKEY_paramgen(ctx.get(), ¶ms) <= 0)) |
| 818 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 819 | lg2::error("Error occurred setting curve parameters"); |
Andrew Geissler | 8dbcc72 | 2023-12-08 10:15:42 -0600 | [diff] [blame^] | 820 | ERR_print_errors_fp(stderr); |
Patrick Williams | 26fb83e | 2021-12-14 14:08:28 -0600 | [diff] [blame] | 821 | elog<InternalFailure>(); |
| 822 | } |
| 823 | |
| 824 | // Move parameters to RAII holder. |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 825 | auto pparms = holderOfKey(params); |
Patrick Williams | 26fb83e | 2021-12-14 14:08:28 -0600 | [diff] [blame] | 826 | |
| 827 | // Create new context for key. |
| 828 | ctx.reset(EVP_PKEY_CTX_new_from_pkey(nullptr, params, nullptr)); |
| 829 | |
| 830 | if (!ctx || (EVP_PKEY_keygen_init(ctx.get()) <= 0)) |
| 831 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 832 | lg2::error("Error occurred initializing keygen context"); |
Andrew Geissler | 8dbcc72 | 2023-12-08 10:15:42 -0600 | [diff] [blame^] | 833 | ERR_print_errors_fp(stderr); |
Patrick Williams | 26fb83e | 2021-12-14 14:08:28 -0600 | [diff] [blame] | 834 | elog<InternalFailure>(); |
| 835 | } |
| 836 | |
| 837 | EVP_PKEY* pKey = nullptr; |
| 838 | if (EVP_PKEY_keygen(ctx.get(), &pKey) <= 0) |
| 839 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 840 | lg2::error("Error occurred during generate EC key"); |
Andrew Geissler | 8dbcc72 | 2023-12-08 10:15:42 -0600 | [diff] [blame^] | 841 | ERR_print_errors_fp(stderr); |
Patrick Williams | 26fb83e | 2021-12-14 14:08:28 -0600 | [diff] [blame] | 842 | elog<InternalFailure>(); |
| 843 | } |
| 844 | |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 845 | return holderOfKey(pKey); |
Patrick Williams | 26fb83e | 2021-12-14 14:08:28 -0600 | [diff] [blame] | 846 | #endif |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 847 | } |
| 848 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 849 | void Manager::writePrivateKey(const EVPPkeyPtr& pKey, |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 850 | const std::string& privKeyFileName) |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 851 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 852 | lg2::info("Writing private key to file"); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 853 | // write private key to file |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 854 | fs::path privKeyPath = certParentInstallPath / privKeyFileName; |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 855 | |
| 856 | FILE* fp = std::fopen(privKeyPath.c_str(), "w"); |
Nan Zhou | cfb5802 | 2021-12-28 11:02:26 -0800 | [diff] [blame] | 857 | if (fp == nullptr) |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 858 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 859 | lg2::error("Error occurred creating private key file"); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 860 | elog<InternalFailure>(); |
| 861 | } |
Patrick Williams | 223e460 | 2023-05-10 07:51:11 -0500 | [diff] [blame] | 862 | int ret = PEM_write_PrivateKey(fp, pKey.get(), nullptr, nullptr, 0, 0, |
| 863 | nullptr); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 864 | std::fclose(fp); |
| 865 | if (ret == 0) |
| 866 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 867 | lg2::error("Error occurred while writing private key to file"); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 868 | elog<InternalFailure>(); |
| 869 | } |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | void Manager::addEntry(X509_NAME* x509Name, const char* field, |
| 873 | const std::string& bytes) |
| 874 | { |
| 875 | if (bytes.empty()) |
| 876 | { |
| 877 | return; |
| 878 | } |
| 879 | int ret = X509_NAME_add_entry_by_txt( |
| 880 | x509Name, field, MBSTRING_ASC, |
| 881 | reinterpret_cast<const unsigned char*>(bytes.c_str()), -1, -1, 0); |
| 882 | if (ret != 1) |
| 883 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 884 | lg2::error("Unable to set entry, FIELD:{FIELD}, VALUE:{VALUE}", "FIELD", |
| 885 | field, "VALUE", bytes); |
Andrew Geissler | 8dbcc72 | 2023-12-08 10:15:42 -0600 | [diff] [blame^] | 886 | ERR_print_errors_fp(stderr); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 887 | elog<InternalFailure>(); |
| 888 | } |
| 889 | } |
| 890 | |
| 891 | void Manager::createCSRObject(const Status& status) |
| 892 | { |
| 893 | if (csrPtr) |
| 894 | { |
| 895 | csrPtr.reset(nullptr); |
| 896 | } |
| 897 | auto csrObjectPath = objectPath + '/' + "csr"; |
| 898 | csrPtr = std::make_unique<CSR>(bus, csrObjectPath.c_str(), |
| 899 | certInstallPath.c_str(), status); |
| 900 | } |
| 901 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 902 | void Manager::writeCSR(const std::string& filePath, const X509ReqPtr& x509Req) |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 903 | { |
| 904 | if (fs::exists(filePath)) |
| 905 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 906 | lg2::info("Removing the existing file, FILENAME:{FILENAME}", "FILENAME", |
| 907 | filePath); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 908 | if (!fs::remove(filePath.c_str())) |
| 909 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 910 | lg2::error("Unable to remove the file, FILENAME:{FILENAME}", |
| 911 | "FILENAME", filePath); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 912 | elog<InternalFailure>(); |
| 913 | } |
| 914 | } |
| 915 | |
Nan Zhou | cfb5802 | 2021-12-28 11:02:26 -0800 | [diff] [blame] | 916 | FILE* fp = nullptr; |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 917 | |
Nan Zhou | cfb5802 | 2021-12-28 11:02:26 -0800 | [diff] [blame] | 918 | if ((fp = std::fopen(filePath.c_str(), "w")) == nullptr) |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 919 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 920 | lg2::error( |
| 921 | "Error opening the file to write the CSR, FILENAME:{FILENAME}", |
| 922 | "FILENAME", filePath); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 923 | elog<InternalFailure>(); |
| 924 | } |
| 925 | |
| 926 | int rc = PEM_write_X509_REQ(fp, x509Req.get()); |
| 927 | if (!rc) |
| 928 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 929 | lg2::error("PEM write routine failed, FILENAME:{FILENAME}", "FILENAME", |
| 930 | filePath); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 931 | std::fclose(fp); |
| 932 | elog<InternalFailure>(); |
| 933 | } |
| 934 | std::fclose(fp); |
| 935 | } |
| 936 | |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 937 | void Manager::createCertificates() |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 938 | { |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 939 | auto certObjectPath = objectPath + '/'; |
| 940 | |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 941 | if (certType == CertificateType::authority) |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 942 | { |
Zbigniew Lukwinski | fe590c4 | 2019-12-10 12:33:50 +0100 | [diff] [blame] | 943 | // Check whether install path is a directory. |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 944 | if (!fs::is_directory(certInstallPath)) |
| 945 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 946 | lg2::error("Certificate installation path exists and it is " |
| 947 | "not a directory"); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 948 | elog<InternalFailure>(); |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | // If the authorities list exists, recover from it and return |
Patrick Williams | 223e460 | 2023-05-10 07:51:11 -0500 | [diff] [blame] | 952 | if (fs::path authoritiesListFilePath = fs::path(certInstallPath) / |
| 953 | defaultAuthoritiesListFileName; |
Nan Zhou | 6ec13c8 | 2021-12-30 11:34:50 -0800 | [diff] [blame] | 954 | fs::exists(authoritiesListFilePath)) |
| 955 | { |
| 956 | // remove all other files and directories |
| 957 | for (auto& path : fs::directory_iterator(certInstallPath)) |
| 958 | { |
| 959 | if (path.path() != authoritiesListFilePath) |
| 960 | { |
| 961 | fs::remove_all(path); |
| 962 | } |
| 963 | } |
| 964 | installAll(authoritiesListFilePath); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 965 | return; |
| 966 | } |
| 967 | |
| 968 | for (auto& path : fs::directory_iterator(certInstallPath)) |
| 969 | { |
| 970 | try |
| 971 | { |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 972 | // Assume here any regular file located in certificate directory |
| 973 | // contains certificates body. Do not want to use soft links |
| 974 | // would add value. |
| 975 | if (fs::is_regular_file(path)) |
| 976 | { |
| 977 | installedCerts.emplace_back(std::make_unique<Certificate>( |
| 978 | bus, certObjectPath + std::to_string(certIdCounter++), |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 979 | certType, certInstallPath, path.path(), |
Willy Tu | 698a574 | 2022-09-23 21:33:01 +0000 | [diff] [blame] | 980 | certWatchPtr.get(), *this, /*restore=*/true)); |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 981 | } |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 982 | } |
| 983 | catch (const InternalFailure& e) |
| 984 | { |
| 985 | report<InternalFailure>(); |
| 986 | } |
| 987 | catch (const InvalidCertificate& e) |
| 988 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 989 | report<InvalidCertificate>(InvalidCertificateReason( |
| 990 | "Existing certificate file is corrupted")); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 991 | } |
| 992 | } |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 993 | } |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 994 | else if (fs::exists(certInstallPath)) |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 995 | { |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 996 | try |
| 997 | { |
| 998 | installedCerts.emplace_back(std::make_unique<Certificate>( |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 999 | bus, certObjectPath + '1', certType, certInstallPath, |
Willy Tu | 698a574 | 2022-09-23 21:33:01 +0000 | [diff] [blame] | 1000 | certInstallPath, certWatchPtr.get(), *this, /*restore=*/false)); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 1001 | } |
| 1002 | catch (const InternalFailure& e) |
| 1003 | { |
| 1004 | report<InternalFailure>(); |
| 1005 | } |
| 1006 | catch (const InvalidCertificate& e) |
| 1007 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1008 | report<InvalidCertificate>(InvalidCertificateReason( |
| 1009 | "Existing certificate file is corrupted")); |
Kowalski, Kamil | db029c9 | 2019-07-08 17:09:39 +0200 | [diff] [blame] | 1010 | } |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 1011 | } |
| 1012 | } |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1013 | |
| 1014 | void Manager::createRSAPrivateKeyFile() |
| 1015 | { |
Patrick Williams | 223e460 | 2023-05-10 07:51:11 -0500 | [diff] [blame] | 1016 | fs::path rsaPrivateKeyFileName = certParentInstallPath / |
| 1017 | defaultRSAPrivateKeyFileName; |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1018 | |
| 1019 | try |
| 1020 | { |
| 1021 | if (!fs::exists(rsaPrivateKeyFileName)) |
| 1022 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1023 | writePrivateKey(generateRSAKeyPair(supportedKeyBitLength), |
Nan Zhou | 718eef3 | 2021-12-28 11:03:30 -0800 | [diff] [blame] | 1024 | defaultRSAPrivateKeyFileName); |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1025 | } |
| 1026 | } |
| 1027 | catch (const InternalFailure& e) |
| 1028 | { |
| 1029 | report<InternalFailure>(); |
| 1030 | } |
| 1031 | } |
| 1032 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1033 | EVPPkeyPtr Manager::getRSAKeyPair(const int64_t keyBitLength) |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1034 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1035 | if (keyBitLength != supportedKeyBitLength) |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1036 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 1037 | lg2::error( |
| 1038 | "Given Key bit length is not supported, GIVENKEYBITLENGTH:" |
| 1039 | "{GIVENKEYBITLENGTH}, SUPPORTEDKEYBITLENGTH:{SUPPORTEDKEYBITLENGTH}", |
| 1040 | "GIVENKEYBITLENGTH", keyBitLength, "SUPPORTEDKEYBITLENGTH", |
| 1041 | supportedKeyBitLength); |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1042 | elog<InvalidArgument>( |
| 1043 | Argument::ARGUMENT_NAME("KEYBITLENGTH"), |
| 1044 | Argument::ARGUMENT_VALUE(std::to_string(keyBitLength).c_str())); |
| 1045 | } |
Patrick Williams | 223e460 | 2023-05-10 07:51:11 -0500 | [diff] [blame] | 1046 | fs::path rsaPrivateKeyFileName = certParentInstallPath / |
| 1047 | defaultRSAPrivateKeyFileName; |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1048 | |
| 1049 | FILE* privateKeyFile = std::fopen(rsaPrivateKeyFileName.c_str(), "r"); |
| 1050 | if (!privateKeyFile) |
| 1051 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 1052 | lg2::error( |
| 1053 | "Unable to open RSA private key file to read, RSAKEYFILE:{RSAKEYFILE}," |
| 1054 | "ERRORREASON:{ERRORREASON}", |
| 1055 | "RSAKEYFILE", rsaPrivateKeyFileName, "ERRORREASON", |
| 1056 | strerror(errno)); |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1057 | elog<InternalFailure>(); |
| 1058 | } |
| 1059 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1060 | EVPPkeyPtr privateKey( |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1061 | PEM_read_PrivateKey(privateKeyFile, nullptr, nullptr, nullptr), |
| 1062 | ::EVP_PKEY_free); |
| 1063 | std::fclose(privateKeyFile); |
| 1064 | |
| 1065 | if (!privateKey) |
| 1066 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 1067 | lg2::error("Error occurred during PEM_read_PrivateKey call"); |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 1068 | elog<InternalFailure>(); |
| 1069 | } |
| 1070 | return privateKey; |
| 1071 | } |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 1072 | |
| 1073 | void Manager::storageUpdate() |
| 1074 | { |
Nan Zhou | e3d47cd | 2022-09-16 03:41:53 +0000 | [diff] [blame] | 1075 | if (certType == CertificateType::authority) |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 1076 | { |
| 1077 | // Remove symbolic links in the certificate directory |
| 1078 | for (auto& certPath : fs::directory_iterator(certInstallPath)) |
| 1079 | { |
| 1080 | try |
| 1081 | { |
| 1082 | if (fs::is_symlink(certPath)) |
| 1083 | { |
| 1084 | fs::remove(certPath); |
| 1085 | } |
| 1086 | } |
| 1087 | catch (const std::exception& e) |
| 1088 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 1089 | lg2::error( |
| 1090 | "Failed to remove symlink for certificate, ERR:{ERR} SYMLINK:{SYMLINK}", |
| 1091 | "ERR", e, "SYMLINK", certPath.path().string()); |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 1092 | elog<InternalFailure>(); |
| 1093 | } |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | for (const auto& cert : installedCerts) |
| 1098 | { |
| 1099 | cert->storageUpdate(); |
| 1100 | } |
| 1101 | } |
| 1102 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1103 | void Manager::reloadOrReset(const std::string& unit) |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 1104 | { |
| 1105 | if (!unit.empty()) |
| 1106 | { |
| 1107 | try |
| 1108 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1109 | constexpr auto defaultSystemdService = "org.freedesktop.systemd1"; |
| 1110 | constexpr auto defaultSystemdObjectPath = |
| 1111 | "/org/freedesktop/systemd1"; |
| 1112 | constexpr auto defaultSystemdInterface = |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 1113 | "org.freedesktop.systemd1.Manager"; |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 1114 | auto method = bus.new_method_call( |
| 1115 | defaultSystemdService, defaultSystemdObjectPath, |
| 1116 | defaultSystemdInterface, "ReloadOrRestartUnit"); |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 1117 | method.append(unit, "replace"); |
| 1118 | bus.call_noreply(method); |
| 1119 | } |
Patrick Williams | b3dbfb3 | 2022-07-22 19:26:57 -0500 | [diff] [blame] | 1120 | catch (const sdbusplus::exception_t& e) |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 1121 | { |
Ravi Teja | f264627 | 2023-09-30 13:00:55 -0500 | [diff] [blame] | 1122 | lg2::error( |
| 1123 | "Failed to reload or restart service, ERR:{ERR}, UNIT:{UNIT}", |
| 1124 | "ERR", e, "UNIT", unit); |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 1125 | elog<InternalFailure>(); |
| 1126 | } |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | bool Manager::isCertificateUnique(const std::string& filePath, |
| 1131 | const Certificate* const certToDrop) |
| 1132 | { |
| 1133 | if (std::any_of( |
| 1134 | installedCerts.begin(), installedCerts.end(), |
Patrick Williams | 223e460 | 2023-05-10 07:51:11 -0500 | [diff] [blame] | 1135 | [&filePath, certToDrop](const std::unique_ptr<Certificate>& cert) { |
| 1136 | return cert.get() != certToDrop && cert->isSame(filePath); |
Patrick Williams | d96b81c | 2023-10-20 11:19:39 -0500 | [diff] [blame] | 1137 | })) |
Zbigniew Lukwinski | 2f3563c | 2020-01-08 12:35:23 +0100 | [diff] [blame] | 1138 | { |
| 1139 | return false; |
| 1140 | } |
| 1141 | else |
| 1142 | { |
| 1143 | return true; |
| 1144 | } |
| 1145 | } |
| 1146 | |
Nan Zhou | e1289ad | 2021-12-28 11:02:56 -0800 | [diff] [blame] | 1147 | } // namespace phosphor::certs |