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