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