blob: cf61d4f739a7584aeaf9b834b797788c77a1cee0 [file] [log] [blame]
Nan Zhou014be0b2021-12-28 18:00:14 -08001#include "config.h"
2
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -05003#include "certs_manager.hpp"
4
Nan Zhou6ec13c82021-12-30 11:34:50 -08005#include "x509_utils.hpp"
6
Nan Zhou014be0b2021-12-28 18:00:14 -08007#include <openssl/asn1.h>
8#include <openssl/bn.h>
9#include <openssl/ec.h>
Andrew Geissler8dbcc722023-12-08 10:15:42 -060010#include <openssl/err.h>
Patrick Williams26fb83e2021-12-14 14:08:28 -060011#include <openssl/evp.h>
Nan Zhou014be0b2021-12-28 18:00:14 -080012#include <openssl/obj_mac.h>
13#include <openssl/objects.h>
14#include <openssl/opensslv.h>
Marri Devender Raof4682712019-03-19 05:00:28 -050015#include <openssl/pem.h>
Nan Zhou014be0b2021-12-28 18:00:14 -080016#include <openssl/rsa.h>
Marri Devender Raof4682712019-03-19 05:00:28 -050017#include <unistd.h>
18
Patrick Williams223e4602023-05-10 07:51:11 -050019#include <phosphor-logging/elog-errors.hpp>
20#include <phosphor-logging/elog.hpp>
Ravi Tejaf2646272023-09-30 13:00:55 -050021#include <phosphor-logging/lg2.hpp>
Patrick Williams223e4602023-05-10 07:51:11 -050022#include <sdbusplus/bus.hpp>
23#include <sdbusplus/exception.hpp>
24#include <sdbusplus/message.hpp>
25#include <sdeventplus/source/base.hpp>
26#include <sdeventplus/source/child.hpp>
27#include <xyz/openbmc_project/Certs/error.hpp>
28#include <xyz/openbmc_project/Common/error.hpp>
29
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +020030#include <algorithm>
Nan Zhou014be0b2021-12-28 18:00:14 -080031#include <array>
32#include <cerrno>
33#include <chrono>
34#include <csignal>
35#include <cstdio>
36#include <cstdlib>
37#include <cstring>
38#include <exception>
Nan Zhou6ec13c82021-12-30 11:34:50 -080039#include <fstream>
Nan Zhou014be0b2021-12-28 18:00:14 -080040#include <utility>
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +010041
Nan Zhoue1289ad2021-12-28 11:02:56 -080042namespace phosphor::certs
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050043{
Nan Zhoucf06ccd2021-12-28 16:25:45 -080044namespace
45{
46namespace fs = std::filesystem;
47using ::phosphor::logging::commit;
48using ::phosphor::logging::elog;
Nan Zhoucf06ccd2021-12-28 16:25:45 -080049using ::phosphor::logging::report;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050050
Nan Zhoucf06ccd2021-12-28 16:25:45 -080051using ::sdbusplus::xyz::openbmc_project::Certs::Error::InvalidCertificate;
52using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
53using ::sdbusplus::xyz::openbmc_project::Common::Error::NotAllowed;
54using NotAllowedReason =
55 ::phosphor::logging::xyz::openbmc_project::Common::NotAllowed::REASON;
56using InvalidCertificateReason = ::phosphor::logging::xyz::openbmc_project::
57 Certs::InvalidCertificate::REASON;
58using ::sdbusplus::xyz::openbmc_project::Common::Error::InvalidArgument;
59using Argument =
60 ::phosphor::logging::xyz::openbmc_project::Common::InvalidArgument;
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -050061
Nan Zhoucf06ccd2021-12-28 16:25:45 -080062// RAII support for openSSL functions.
63using X509ReqPtr = std::unique_ptr<X509_REQ, decltype(&::X509_REQ_free)>;
64using EVPPkeyPtr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>;
65using BignumPtr = std::unique_ptr<BIGNUM, decltype(&::BN_free)>;
Nan Zhou6ec13c82021-12-30 11:34:50 -080066using X509StorePtr = std::unique_ptr<X509_STORE, decltype(&::X509_STORE_free)>;
Nan Zhoucf06ccd2021-12-28 16:25:45 -080067
68constexpr int supportedKeyBitLength = 2048;
69constexpr int defaultKeyBitLength = 2048;
70// secp224r1 is equal to RSA 2048 KeyBitLength. Refer RFC 5349
71constexpr auto defaultKeyCurveID = "secp224r1";
Nan Zhou6ec13c82021-12-30 11:34:50 -080072// PEM certificate block markers, defined in go/rfc/7468.
73constexpr std::string_view beginCertificate = "-----BEGIN CERTIFICATE-----";
74constexpr std::string_view endCertificate = "-----END CERTIFICATE-----";
75
76/**
77 * @brief Splits the given authorities list file and returns an array of
78 * individual PEM encoded x509 certificate.
79 *
80 * @param[in] sourceFilePath - Path to the authorities list file.
81 *
82 * @return An array of individual PEM encoded x509 certificate
83 */
84std::vector<std::string> splitCertificates(const std::string& sourceFilePath)
85{
86 std::ifstream inputCertFileStream;
87 inputCertFileStream.exceptions(
88 std::ifstream::failbit | std::ifstream::badbit | std::ifstream::eofbit);
89
90 std::stringstream pemStream;
91 std::vector<std::string> certificatesList;
92 try
93 {
94 inputCertFileStream.open(sourceFilePath);
95 pemStream << inputCertFileStream.rdbuf();
96 inputCertFileStream.close();
97 }
98 catch (const std::exception& e)
99 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500100 lg2::error("Failed to read certificates list, ERR:{ERR}, SRC:{SRC}",
101 "ERR", e, "SRC", sourceFilePath);
Nan Zhou6ec13c82021-12-30 11:34:50 -0800102 elog<InternalFailure>();
103 }
104 std::string pem = pemStream.str();
105 size_t begin = 0;
106 // |begin| points to the current start position for searching the next
107 // |beginCertificate| block. When we find the beginning of the certificate,
108 // we extract the content between the beginning and the end of the current
109 // certificate. And finally we move |begin| to the end of the current
110 // certificate to start searching the next potential certificate.
111 for (begin = pem.find(beginCertificate, begin); begin != std::string::npos;
112 begin = pem.find(beginCertificate, begin))
113 {
114 size_t end = pem.find(endCertificate, begin);
115 if (end == std::string::npos)
116 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500117 lg2::error(
Nan Zhou6ec13c82021-12-30 11:34:50 -0800118 "invalid PEM contains a BEGIN identifier without an END");
119 elog<InvalidCertificate>(InvalidCertificateReason(
120 "invalid PEM contains a BEGIN identifier without an END"));
121 }
122 end += endCertificate.size();
123 certificatesList.emplace_back(pem.substr(begin, end - begin));
124 begin = end;
125 }
126 return certificatesList;
127}
128
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800129} // namespace
Marri Devender Raof4682712019-03-19 05:00:28 -0500130
Patrick Williamsb3dbfb32022-07-22 19:26:57 -0500131Manager::Manager(sdbusplus::bus_t& bus, sdeventplus::Event& event,
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800132 const char* path, CertificateType type,
133 const std::string& unit, const std::string& installPath) :
Patrick Williamsa2f68d82024-08-16 15:21:36 -0400134 internal::ManagerInterface(bus, path), bus(bus), event(event),
135 objectPath(path), certType(type), unitToRestart(std::move(unit)),
136 certInstallPath(std::move(installPath)),
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -0500137 certParentInstallPath(fs::path(certInstallPath).parent_path())
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500138{
Marri Devender Raob57d75e2019-07-25 04:56:21 -0500139 try
140 {
Marri Devender Raodb5c6fc2020-03-10 13:27:49 -0500141 // Create certificate directory if not existing.
Nan Zhoubf3cf752021-12-28 11:02:07 -0800142 // Set correct certificate directory permissions.
Marri Devender Raodb5c6fc2020-03-10 13:27:49 -0500143 fs::path certDirectory;
144 try
Marri Devender Raob57d75e2019-07-25 04:56:21 -0500145 {
Nan Zhoue3d47cd2022-09-16 03:41:53 +0000146 if (certType == CertificateType::authority)
Marri Devender Raodb5c6fc2020-03-10 13:27:49 -0500147 {
148 certDirectory = certInstallPath;
149 }
150 else
151 {
152 certDirectory = certParentInstallPath;
153 }
154
155 if (!fs::exists(certDirectory))
156 {
157 fs::create_directories(certDirectory);
158 }
159
160 auto permission = fs::perms::owner_read | fs::perms::owner_write |
161 fs::perms::owner_exec;
162 fs::permissions(certDirectory, permission,
163 fs::perm_options::replace);
164 storageUpdate();
165 }
Patrick Williams71957992021-10-06 14:42:52 -0500166 catch (const fs::filesystem_error& e)
Marri Devender Raodb5c6fc2020-03-10 13:27:49 -0500167 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500168 lg2::error(
169 "Failed to create directory, ERR:{ERR}, DIRECTORY:{DIRECTORY}",
170 "ERR", e, "DIRECTORY", certParentInstallPath);
Marri Devender Raodb5c6fc2020-03-10 13:27:49 -0500171 report<InternalFailure>();
172 }
173
174 // Generating RSA private key file if certificate type is server/client
Nan Zhoue3d47cd2022-09-16 03:41:53 +0000175 if (certType != CertificateType::authority)
Marri Devender Raodb5c6fc2020-03-10 13:27:49 -0500176 {
177 createRSAPrivateKeyFile();
178 }
179
180 // restore any existing certificates
181 createCertificates();
182
183 // watch is not required for authority certificates
Nan Zhoue3d47cd2022-09-16 03:41:53 +0000184 if (certType != CertificateType::authority)
Marri Devender Raodb5c6fc2020-03-10 13:27:49 -0500185 {
186 // watch for certificate file create/replace
Patrick Williamsa2f68d82024-08-16 15:21:36 -0400187 certWatchPtr = std::make_unique<
188 Watch>(event, certInstallPath, [this]() {
Marri Devender Raodb5c6fc2020-03-10 13:27:49 -0500189 try
190 {
191 // if certificate file existing update it
192 if (!installedCerts.empty())
193 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500194 lg2::info("Inotify callback to update "
195 "certificate properties");
Marri Devender Raodb5c6fc2020-03-10 13:27:49 -0500196 installedCerts[0]->populateProperties();
197 }
198 else
199 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500200 lg2::info(
Marri Devender Raodb5c6fc2020-03-10 13:27:49 -0500201 "Inotify callback to create certificate object");
202 createCertificates();
203 }
204 }
205 catch (const InternalFailure& e)
206 {
207 commit<InternalFailure>();
208 }
209 catch (const InvalidCertificate& e)
210 {
211 commit<InvalidCertificate>();
212 }
213 });
Marri Devender Raob57d75e2019-07-25 04:56:21 -0500214 }
Zbigniew Lukwinskife590c42019-12-10 12:33:50 +0100215 else
216 {
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500217 try
218 {
Marri Devender Raodb5c6fc2020-03-10 13:27:49 -0500219 const std::string singleCertPath = "/etc/ssl/certs/Root-CA.pem";
220 if (fs::exists(singleCertPath) && !fs::is_empty(singleCertPath))
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500221 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500222 lg2::notice(
223 "Legacy certificate detected, will be installed from,"
224 "SINGLE_CERTPATH:{SINGLE_CERTPATH}",
225 "SINGLE_CERTPATH", singleCertPath);
Marri Devender Raodb5c6fc2020-03-10 13:27:49 -0500226 install(singleCertPath);
227 if (!fs::remove(singleCertPath))
228 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500229 lg2::error("Unable to remove old certificate from,"
230 "SINGLE_CERTPATH:{SINGLE_CERTPATH}",
231 "SINGLE_CERTPATH", singleCertPath);
Marri Devender Raodb5c6fc2020-03-10 13:27:49 -0500232 elog<InternalFailure>();
233 }
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500234 }
235 }
Marri Devender Raodb5c6fc2020-03-10 13:27:49 -0500236 catch (const std::exception& ex)
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500237 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500238 lg2::error(
239 "Error in restoring legacy certificate, ERROR_STR:{ERROR_STR}",
240 "ERROR_STR", ex);
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200241 }
242 }
243 }
Patrick Williams71957992021-10-06 14:42:52 -0500244 catch (const std::exception& ex)
Marri Devender Raodb5c6fc2020-03-10 13:27:49 -0500245 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500246 lg2::error(
247 "Error in certificate manager constructor, ERROR_STR:{ERROR_STR}",
248 "ERROR_STR", ex);
Marri Devender Raodb5c6fc2020-03-10 13:27:49 -0500249 }
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500250}
251
Zbigniew Kurzynski06a69d72019-09-27 10:57:38 +0200252std::string Manager::install(const std::string filePath)
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500253{
Nan Zhoue3d47cd2022-09-16 03:41:53 +0000254 if (certType != CertificateType::authority && !installedCerts.empty())
Marri Devender Rao13965112019-02-27 08:47:12 -0600255 {
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800256 elog<NotAllowed>(NotAllowedReason("Certificate already exist"));
Marri Devender Rao13965112019-02-27 08:47:12 -0600257 }
Nan Zhoue3d47cd2022-09-16 03:41:53 +0000258 else if (certType == CertificateType::authority &&
Nan Zhou718eef32021-12-28 11:03:30 -0800259 installedCerts.size() >= maxNumAuthorityCertificates)
Zbigniew Lukwinski3b07b772019-10-09 11:43:34 +0200260 {
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800261 elog<NotAllowed>(NotAllowedReason("Certificates limit reached"));
Zbigniew Lukwinski3b07b772019-10-09 11:43:34 +0200262 }
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500263
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100264 std::string certObjectPath;
265 if (isCertificateUnique(filePath))
266 {
267 certObjectPath = objectPath + '/' + std::to_string(certIdCounter);
268 installedCerts.emplace_back(std::make_unique<Certificate>(
269 bus, certObjectPath, certType, certInstallPath, filePath,
Willy Tu698a5742022-09-23 21:33:01 +0000270 certWatchPtr.get(), *this, /*restore=*/false));
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100271 reloadOrReset(unitToRestart);
272 certIdCounter++;
273 }
274 else
275 {
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800276 elog<NotAllowed>(NotAllowedReason("Certificate already exist"));
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100277 }
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200278
Zbigniew Kurzynski06a69d72019-09-27 10:57:38 +0200279 return certObjectPath;
Jayanth Othayoth589159f2018-09-28 08:32:39 -0500280}
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -0500281
Nan Zhou6ec13c82021-12-30 11:34:50 -0800282std::vector<sdbusplus::message::object_path>
283 Manager::installAll(const std::string filePath)
284{
Nan Zhoue3d47cd2022-09-16 03:41:53 +0000285 if (certType != CertificateType::authority)
Nan Zhou6ec13c82021-12-30 11:34:50 -0800286 {
Patrick Williamsa2f68d82024-08-16 15:21:36 -0400287 elog<NotAllowed>(NotAllowedReason(
288 "The InstallAll interface is only allowed for "
289 "Authority certificates"));
Nan Zhou6ec13c82021-12-30 11:34:50 -0800290 }
291
292 if (!installedCerts.empty())
293 {
294 elog<NotAllowed>(NotAllowedReason(
295 "There are already root certificates; Call DeleteAll then "
296 "InstallAll, or use ReplaceAll"));
297 }
298
299 fs::path sourceFile(filePath);
300 if (!fs::exists(sourceFile))
301 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500302 lg2::error("File is Missing, FILE:{FILE}", "FILE", filePath);
Nan Zhou6ec13c82021-12-30 11:34:50 -0800303 elog<InternalFailure>();
304 }
305 std::vector<std::string> authorities = splitCertificates(sourceFile);
306 if (authorities.size() > maxNumAuthorityCertificates)
307 {
308 elog<NotAllowed>(NotAllowedReason("Certificates limit reached"));
309 }
310
Ravi Tejaf2646272023-09-30 13:00:55 -0500311 lg2::info("Starts authority list install");
Nan Zhou78357b02022-06-09 22:33:35 +0000312
Nan Zhou6ec13c82021-12-30 11:34:50 -0800313 fs::path authorityStore(certInstallPath);
Patrick Williamsa2f68d82024-08-16 15:21:36 -0400314 fs::path authoritiesListFile =
315 authorityStore / defaultAuthoritiesListFileName;
Nan Zhou6ec13c82021-12-30 11:34:50 -0800316
317 // Atomically install all the certificates
318 fs::path tempPath = Certificate::generateUniqueFilePath(authorityStore);
319 fs::create_directory(tempPath);
320 // Copies the authorities list
321 Certificate::copyCertificate(sourceFile,
322 tempPath / defaultAuthoritiesListFileName);
323 std::vector<std::unique_ptr<Certificate>> tempCertificates;
324 uint64_t tempCertIdCounter = certIdCounter;
325 X509StorePtr x509Store = getX509Store(sourceFile);
326 for (const auto& authority : authorities)
327 {
Patrick Williamsa2f68d82024-08-16 15:21:36 -0400328 std::string certObjectPath =
329 objectPath + '/' + std::to_string(tempCertIdCounter);
Nan Zhou6ec13c82021-12-30 11:34:50 -0800330 tempCertificates.emplace_back(std::make_unique<Certificate>(
331 bus, certObjectPath, certType, tempPath, *x509Store, authority,
Willy Tu698a5742022-09-23 21:33:01 +0000332 certWatchPtr.get(), *this, /*restore=*/false));
Nan Zhou6ec13c82021-12-30 11:34:50 -0800333 tempCertIdCounter++;
334 }
335
336 // We are good now, issue swap
337 installedCerts = std::move(tempCertificates);
338 certIdCounter = tempCertIdCounter;
339 // Rename all the certificates including the authorities list
340 for (const fs::path& f : fs::directory_iterator(tempPath))
341 {
342 if (fs::is_symlink(f))
343 {
344 continue;
345 }
346 fs::rename(/*from=*/f, /*to=*/certInstallPath / f.filename());
347 }
348 // Update file locations and create symbol links
349 for (const auto& cert : installedCerts)
350 {
351 cert->setCertInstallPath(certInstallPath);
Patrick Williamsa2f68d82024-08-16 15:21:36 -0400352 cert->setCertFilePath(
353 certInstallPath / fs::path(cert->getCertFilePath()).filename());
Nan Zhou6ec13c82021-12-30 11:34:50 -0800354 cert->storageUpdate();
355 }
356 // Remove the temporary folder
357 fs::remove_all(tempPath);
358
359 std::vector<sdbusplus::message::object_path> objects;
360 for (const auto& certificate : installedCerts)
361 {
362 objects.emplace_back(certificate->getObjectPath());
363 }
364
Ravi Tejaf2646272023-09-30 13:00:55 -0500365 lg2::info("Finishes authority list install; reload units starts");
Nan Zhou6ec13c82021-12-30 11:34:50 -0800366 reloadOrReset(unitToRestart);
367 return objects;
368}
369
370std::vector<sdbusplus::message::object_path>
371 Manager::replaceAll(std::string filePath)
372{
373 installedCerts.clear();
374 certIdCounter = 1;
375 storageUpdate();
376 return installAll(std::move(filePath));
377}
378
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +0200379void Manager::deleteAll()
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -0500380{
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600381 // TODO: #Issue 4 when a certificate is deleted system auto generates
382 // certificate file. At present we are not supporting creation of
383 // certificate object for the auto-generated certificate file as
384 // deletion if only applicable for REST server and Bmcweb does not allow
385 // deletion of certificates
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200386 installedCerts.clear();
Nan Zhou6ec13c82021-12-30 11:34:50 -0800387 // If the authorities list exists, delete it as well
Nan Zhoue3d47cd2022-09-16 03:41:53 +0000388 if (certType == CertificateType::authority)
Nan Zhou6ec13c82021-12-30 11:34:50 -0800389 {
Patrick Williamsa2f68d82024-08-16 15:21:36 -0400390 if (fs::path authoritiesList =
391 fs::path(certInstallPath) / defaultAuthoritiesListFileName;
Nan Zhou6ec13c82021-12-30 11:34:50 -0800392 fs::exists(authoritiesList))
393 {
394 fs::remove(authoritiesList);
395 }
396 }
397 certIdCounter = 1;
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100398 storageUpdate();
399 reloadOrReset(unitToRestart);
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -0500400}
Marri Devender Raof4682712019-03-19 05:00:28 -0500401
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100402void Manager::deleteCertificate(const Certificate* const certificate)
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +0200403{
Patrick Williams223e4602023-05-10 07:51:11 -0500404 const std::vector<std::unique_ptr<Certificate>>::iterator& certIt =
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +0200405 std::find_if(installedCerts.begin(), installedCerts.end(),
Patrick Williams223e4602023-05-10 07:51:11 -0500406 [certificate](const std::unique_ptr<Certificate>& cert) {
Patrick Williamsa2f68d82024-08-16 15:21:36 -0400407 return (cert.get() == certificate);
408 });
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +0200409 if (certIt != installedCerts.end())
410 {
411 installedCerts.erase(certIt);
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100412 storageUpdate();
413 reloadOrReset(unitToRestart);
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +0200414 }
415 else
416 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500417 lg2::error("Certificate does not exist, ID:{ID}", "ID",
418 certificate->getCertId());
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100419 elog<InternalFailure>();
420 }
421}
422
423void Manager::replaceCertificate(Certificate* const certificate,
424 const std::string& filePath)
425{
426 if (isCertificateUnique(filePath, certificate))
427 {
Willy Tu698a5742022-09-23 21:33:01 +0000428 certificate->install(filePath, false);
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100429 storageUpdate();
430 reloadOrReset(unitToRestart);
431 }
432 else
433 {
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800434 elog<NotAllowed>(NotAllowedReason("Certificate already exist"));
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +0200435 }
436}
437
Marri Devender Raof4682712019-03-19 05:00:28 -0500438std::string Manager::generateCSR(
439 std::vector<std::string> alternativeNames, std::string challengePassword,
440 std::string city, std::string commonName, std::string contactPerson,
441 std::string country, std::string email, std::string givenName,
442 std::string initials, int64_t keyBitLength, std::string keyCurveId,
443 std::string keyPairAlgorithm, std::vector<std::string> keyUsage,
444 std::string organization, std::string organizationalUnit, std::string state,
445 std::string surname, std::string unstructuredName)
446{
447 // We support only one CSR.
448 csrPtr.reset(nullptr);
449 auto pid = fork();
450 if (pid == -1)
451 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500452 lg2::error("Error occurred during forking process");
Marri Devender Raof4682712019-03-19 05:00:28 -0500453 report<InternalFailure>();
454 }
455 else if (pid == 0)
456 {
457 try
458 {
Patrick Williamsa2f68d82024-08-16 15:21:36 -0400459 generateCSRHelper(
460 alternativeNames, challengePassword, city, commonName,
461 contactPerson, country, email, givenName, initials,
462 keyBitLength, keyCurveId, keyPairAlgorithm, keyUsage,
463 organization, organizationalUnit, state, surname,
464 unstructuredName);
Marri Devender Raof4682712019-03-19 05:00:28 -0500465 exit(EXIT_SUCCESS);
466 }
467 catch (const InternalFailure& e)
468 {
469 // commit the error reported in child process and exit
470 // Callback method from SDEvent Loop looks for exit status
471 exit(EXIT_FAILURE);
472 commit<InternalFailure>();
473 }
Ramesh Iyyard2393f22020-10-29 09:46:51 -0500474 catch (const InvalidArgument& e)
475 {
476 // commit the error reported in child process and exit
477 // Callback method from SDEvent Loop looks for exit status
478 exit(EXIT_FAILURE);
479 commit<InvalidArgument>();
480 }
Marri Devender Raof4682712019-03-19 05:00:28 -0500481 }
482 else
483 {
484 using namespace sdeventplus::source;
Patrick Williamsa2f68d82024-08-16 15:21:36 -0400485 Child::Callback callback =
486 [this](Child& eventSource, const siginfo_t* si) {
487 eventSource.set_enabled(Enabled::On);
488 if (si->si_status != 0)
489 {
490 this->createCSRObject(Status::failure);
491 }
492 else
493 {
494 this->createCSRObject(Status::success);
495 }
496 };
Marri Devender Raof4682712019-03-19 05:00:28 -0500497 try
498 {
499 sigset_t ss;
500 if (sigemptyset(&ss) < 0)
501 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500502 lg2::error("Unable to initialize signal set");
Marri Devender Raof4682712019-03-19 05:00:28 -0500503 elog<InternalFailure>();
504 }
505 if (sigaddset(&ss, SIGCHLD) < 0)
506 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500507 lg2::error("Unable to add signal to signal set");
Marri Devender Raof4682712019-03-19 05:00:28 -0500508 elog<InternalFailure>();
509 }
510
511 // Block SIGCHLD first, so that the event loop can handle it
Nan Zhoucfb58022021-12-28 11:02:26 -0800512 if (sigprocmask(SIG_BLOCK, &ss, nullptr) < 0)
Marri Devender Raof4682712019-03-19 05:00:28 -0500513 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500514 lg2::error("Unable to block signal");
Marri Devender Raof4682712019-03-19 05:00:28 -0500515 elog<InternalFailure>();
516 }
517 if (childPtr)
518 {
519 childPtr.reset();
520 }
521 childPtr = std::make_unique<Child>(event, pid, WEXITED | WSTOPPED,
522 std::move(callback));
523 }
524 catch (const InternalFailure& e)
525 {
526 commit<InternalFailure>();
527 }
528 }
529 auto csrObjectPath = objectPath + '/' + "csr";
530 return csrObjectPath;
531}
532
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200533std::vector<std::unique_ptr<Certificate>>& Manager::getCertificates()
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500534{
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200535 return installedCerts;
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500536}
537
Marri Devender Raof4682712019-03-19 05:00:28 -0500538void Manager::generateCSRHelper(
539 std::vector<std::string> alternativeNames, std::string challengePassword,
540 std::string city, std::string commonName, std::string contactPerson,
541 std::string country, std::string email, std::string givenName,
542 std::string initials, int64_t keyBitLength, std::string keyCurveId,
543 std::string keyPairAlgorithm, std::vector<std::string> keyUsage,
544 std::string organization, std::string organizationalUnit, std::string state,
545 std::string surname, std::string unstructuredName)
546{
547 int ret = 0;
548
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800549 X509ReqPtr x509Req(X509_REQ_new(), ::X509_REQ_free);
Marri Devender Raof4682712019-03-19 05:00:28 -0500550
551 // set subject of x509 req
552 X509_NAME* x509Name = X509_REQ_get_subject_name(x509Req.get());
553
554 if (!alternativeNames.empty())
555 {
556 for (auto& name : alternativeNames)
557 {
558 addEntry(x509Name, "subjectAltName", name);
559 }
560 }
561 addEntry(x509Name, "challengePassword", challengePassword);
562 addEntry(x509Name, "L", city);
563 addEntry(x509Name, "CN", commonName);
564 addEntry(x509Name, "name", contactPerson);
565 addEntry(x509Name, "C", country);
566 addEntry(x509Name, "emailAddress", email);
567 addEntry(x509Name, "GN", givenName);
568 addEntry(x509Name, "initials", initials);
569 addEntry(x509Name, "algorithm", keyPairAlgorithm);
570 if (!keyUsage.empty())
571 {
572 for (auto& usage : keyUsage)
573 {
Marri Devender Rao76411052019-08-07 01:25:07 -0500574 if (isExtendedKeyUsage(usage))
575 {
576 addEntry(x509Name, "extendedKeyUsage", usage);
577 }
578 else
579 {
580 addEntry(x509Name, "keyUsage", usage);
581 }
Marri Devender Raof4682712019-03-19 05:00:28 -0500582 }
583 }
584 addEntry(x509Name, "O", organization);
Jayanth Othayothdc91fb62021-05-04 23:17:47 -0500585 addEntry(x509Name, "OU", organizationalUnit);
Marri Devender Raof4682712019-03-19 05:00:28 -0500586 addEntry(x509Name, "ST", state);
587 addEntry(x509Name, "SN", surname);
588 addEntry(x509Name, "unstructuredName", unstructuredName);
589
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800590 EVPPkeyPtr pKey(nullptr, ::EVP_PKEY_free);
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500591
Ravi Tejaf2646272023-09-30 13:00:55 -0500592 lg2::info("Given Key pair algorithm, KEYPAIRALGORITHM:{KEYPAIRALGORITHM}",
593 "KEYPAIRALGORITHM", keyPairAlgorithm);
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500594
595 // Used EC algorithm as default if user did not give algorithm type.
596 if (keyPairAlgorithm == "RSA")
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -0500597 pKey = getRSAKeyPair(keyBitLength);
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500598 else if ((keyPairAlgorithm == "EC") || (keyPairAlgorithm.empty()))
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -0500599 pKey = generateECKeyPair(keyCurveId);
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500600 else
601 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500602 lg2::error("Given Key pair algorithm is not supported. Supporting "
603 "RSA and EC only");
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500604 elog<InvalidArgument>(
605 Argument::ARGUMENT_NAME("KEYPAIRALGORITHM"),
606 Argument::ARGUMENT_VALUE(keyPairAlgorithm.c_str()));
607 }
608
609 ret = X509_REQ_set_pubkey(x509Req.get(), pKey.get());
610 if (ret == 0)
611 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500612 lg2::error("Error occurred while setting Public key");
Andrew Geissler8dbcc722023-12-08 10:15:42 -0600613 ERR_print_errors_fp(stderr);
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500614 elog<InternalFailure>();
615 }
616
617 // Write private key to file
Nan Zhou718eef32021-12-28 11:03:30 -0800618 writePrivateKey(pKey, defaultPrivateKeyFileName);
Marri Devender Raof4682712019-03-19 05:00:28 -0500619
620 // set sign key of x509 req
621 ret = X509_REQ_sign(x509Req.get(), pKey.get(), EVP_sha256());
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500622 if (ret == 0)
Marri Devender Raof4682712019-03-19 05:00:28 -0500623 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500624 lg2::error("Error occurred while signing key of x509");
Andrew Geissler8dbcc722023-12-08 10:15:42 -0600625 ERR_print_errors_fp(stderr);
Marri Devender Raof4682712019-03-19 05:00:28 -0500626 elog<InternalFailure>();
627 }
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500628
Ravi Tejaf2646272023-09-30 13:00:55 -0500629 lg2::info("Writing CSR to file");
Nan Zhou718eef32021-12-28 11:03:30 -0800630 fs::path csrFilePath = certParentInstallPath / defaultCSRFileName;
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -0500631 writeCSR(csrFilePath.string(), x509Req);
Marri Devender Raof4682712019-03-19 05:00:28 -0500632}
633
Marri Devender Rao76411052019-08-07 01:25:07 -0500634bool Manager::isExtendedKeyUsage(const std::string& usage)
635{
636 const static std::array<const char*, 6> usageList = {
637 "ServerAuthentication", "ClientAuthentication", "OCSPSigning",
638 "Timestamping", "CodeSigning", "EmailProtection"};
Patrick Williamsd96b81c2023-10-20 11:19:39 -0500639 auto it = std::find_if(
640 usageList.begin(), usageList.end(),
641 [&usage](const char* s) { return (strcmp(s, usage.c_str()) == 0); });
Marri Devender Rao76411052019-08-07 01:25:07 -0500642 return it != usageList.end();
643}
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800644EVPPkeyPtr Manager::generateRSAKeyPair(const int64_t keyBitLength)
Marri Devender Raof4682712019-03-19 05:00:28 -0500645{
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500646 int64_t keyBitLen = keyBitLength;
Marri Devender Raof4682712019-03-19 05:00:28 -0500647 // set keybit length to default value if not set
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500648 if (keyBitLen <= 0)
Marri Devender Raof4682712019-03-19 05:00:28 -0500649 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500650 lg2::info("KeyBitLength is not given.Hence, using default KeyBitLength:"
651 "{DEFAULTKEYBITLENGTH}",
652 "DEFAULTKEYBITLENGTH", defaultKeyBitLength);
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800653 keyBitLen = defaultKeyBitLength;
Marri Devender Raof4682712019-03-19 05:00:28 -0500654 }
Patrick Williams26fb83e2021-12-14 14:08:28 -0600655
656#if (OPENSSL_VERSION_NUMBER < 0x30000000L)
657
658 // generate rsa key
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800659 BignumPtr bne(BN_new(), ::BN_free);
Patrick Williams26fb83e2021-12-14 14:08:28 -0600660 auto ret = BN_set_word(bne.get(), RSA_F4);
661 if (ret == 0)
662 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500663 lg2::error("Error occurred during BN_set_word call");
Andrew Geissler8dbcc722023-12-08 10:15:42 -0600664 ERR_print_errors_fp(stderr);
Patrick Williams26fb83e2021-12-14 14:08:28 -0600665 elog<InternalFailure>();
666 }
Nan Zhou762da742022-01-14 17:21:44 -0800667 using RSAPtr = std::unique_ptr<RSA, decltype(&::RSA_free)>;
668 RSAPtr rsa(RSA_new(), ::RSA_free);
669 ret = RSA_generate_key_ex(rsa.get(), keyBitLen, bne.get(), nullptr);
Marri Devender Raof4682712019-03-19 05:00:28 -0500670 if (ret != 1)
671 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500672 lg2::error(
673 "Error occurred during RSA_generate_key_ex call: {KEYBITLENGTH}",
674 "KEYBITLENGTH", keyBitLen);
Andrew Geissler8dbcc722023-12-08 10:15:42 -0600675 ERR_print_errors_fp(stderr);
Marri Devender Raof4682712019-03-19 05:00:28 -0500676 elog<InternalFailure>();
677 }
678
679 // set public key of x509 req
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800680 EVPPkeyPtr pKey(EVP_PKEY_new(), ::EVP_PKEY_free);
Nan Zhou762da742022-01-14 17:21:44 -0800681 ret = EVP_PKEY_assign_RSA(pKey.get(), rsa.get());
Marri Devender Raof4682712019-03-19 05:00:28 -0500682 if (ret == 0)
683 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500684 lg2::error("Error occurred during assign rsa key into EVP");
Andrew Geissler8dbcc722023-12-08 10:15:42 -0600685 ERR_print_errors_fp(stderr);
Marri Devender Raof4682712019-03-19 05:00:28 -0500686 elog<InternalFailure>();
687 }
Nan Zhou762da742022-01-14 17:21:44 -0800688 // Now |rsa| is managed by |pKey|
689 rsa.release();
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500690 return pKey;
Patrick Williams26fb83e2021-12-14 14:08:28 -0600691
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 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500697 lg2::error("Error occurred creating EVP_PKEY_CTX from algorithm");
Andrew Geissler8dbcc722023-12-08 10:15:42 -0600698 ERR_print_errors_fp(stderr);
Patrick Williams26fb83e2021-12-14 14:08:28 -0600699 elog<InternalFailure>();
700 }
701
702 if ((EVP_PKEY_keygen_init(ctx.get()) <= 0) ||
Jayanth Othayoth1c1497a2024-11-24 22:35:15 -0600703 (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx.get(),
704 static_cast<int>(keyBitLen)) <= 0))
Patrick Williams26fb83e2021-12-14 14:08:28 -0600705
706 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500707 lg2::error("Error occurred initializing keygen context");
Andrew Geissler8dbcc722023-12-08 10:15:42 -0600708 ERR_print_errors_fp(stderr);
Patrick Williams26fb83e2021-12-14 14:08:28 -0600709 elog<InternalFailure>();
710 }
711
712 EVP_PKEY* pKey = nullptr;
713 if (EVP_PKEY_keygen(ctx.get(), &pKey) <= 0)
714 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500715 lg2::error("Error occurred during generate EC key");
Andrew Geissler8dbcc722023-12-08 10:15:42 -0600716 ERR_print_errors_fp(stderr);
Patrick Williams26fb83e2021-12-14 14:08:28 -0600717 elog<InternalFailure>();
718 }
719
720 return {pKey, &::EVP_PKEY_free};
721#endif
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500722}
723
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800724EVPPkeyPtr Manager::generateECKeyPair(const std::string& curveId)
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500725{
726 std::string curId(curveId);
727
728 if (curId.empty())
729 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500730 lg2::info("KeyCurveId is not given. Hence using default curve id,"
731 "DEFAULTKEYCURVEID:{DEFAULTKEYCURVEID}",
732 "DEFAULTKEYCURVEID", defaultKeyCurveID);
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800733 curId = defaultKeyCurveID;
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500734 }
735
736 int ecGrp = OBJ_txt2nid(curId.c_str());
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500737 if (ecGrp == NID_undef)
738 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500739 lg2::error(
740 "Error occurred during convert the curve id string format into NID,"
741 "KEYCURVEID:{KEYCURVEID}",
742 "KEYCURVEID", curId);
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500743 elog<InternalFailure>();
744 }
745
Patrick Williams26fb83e2021-12-14 14:08:28 -0600746#if (OPENSSL_VERSION_NUMBER < 0x30000000L)
747
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500748 EC_KEY* ecKey = EC_KEY_new_by_curve_name(ecGrp);
749
Nan Zhoucfb58022021-12-28 11:02:26 -0800750 if (ecKey == nullptr)
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500751 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500752 lg2::error(
753 "Error occurred during create the EC_Key object from NID, ECGROUP:{ECGROUP}",
754 "ECGROUP", ecGrp);
Andrew Geissler8dbcc722023-12-08 10:15:42 -0600755 ERR_print_errors_fp(stderr);
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500756 elog<InternalFailure>();
757 }
758
759 // If you want to save a key and later load it with
760 // SSL_CTX_use_PrivateKey_file, then you must set the OPENSSL_EC_NAMED_CURVE
761 // flag on the key.
762 EC_KEY_set_asn1_flag(ecKey, OPENSSL_EC_NAMED_CURVE);
763
764 int ret = EC_KEY_generate_key(ecKey);
765
766 if (ret == 0)
767 {
768 EC_KEY_free(ecKey);
Ravi Tejaf2646272023-09-30 13:00:55 -0500769 lg2::error("Error occurred during generate EC key");
Andrew Geissler8dbcc722023-12-08 10:15:42 -0600770 ERR_print_errors_fp(stderr);
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500771 elog<InternalFailure>();
772 }
773
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800774 EVPPkeyPtr pKey(EVP_PKEY_new(), ::EVP_PKEY_free);
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500775 ret = EVP_PKEY_assign_EC_KEY(pKey.get(), ecKey);
776 if (ret == 0)
777 {
778 EC_KEY_free(ecKey);
Ravi Tejaf2646272023-09-30 13:00:55 -0500779 lg2::error("Error occurred during assign EC Key into EVP");
Andrew Geissler8dbcc722023-12-08 10:15:42 -0600780 ERR_print_errors_fp(stderr);
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500781 elog<InternalFailure>();
782 }
783
784 return pKey;
Patrick Williams26fb83e2021-12-14 14:08:28 -0600785
786#else
Nan Zhoue3d47cd2022-09-16 03:41:53 +0000787 auto holderOfKey = [](EVP_PKEY* key) {
Patrick Williams26fb83e2021-12-14 14:08:28 -0600788 return std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>{
789 key, &::EVP_PKEY_free};
790 };
791
792 // Create context to set up curve parameters.
793 auto ctx = std::unique_ptr<EVP_PKEY_CTX, decltype(&::EVP_PKEY_CTX_free)>(
794 EVP_PKEY_CTX_new_id(EVP_PKEY_EC, nullptr), &::EVP_PKEY_CTX_free);
795 if (!ctx)
796 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500797 lg2::error("Error occurred creating EVP_PKEY_CTX for params");
Andrew Geissler8dbcc722023-12-08 10:15:42 -0600798 ERR_print_errors_fp(stderr);
Patrick Williams26fb83e2021-12-14 14:08:28 -0600799 elog<InternalFailure>();
800 }
801
802 // Set up curve parameters.
803 EVP_PKEY* params = nullptr;
804
805 if ((EVP_PKEY_paramgen_init(ctx.get()) <= 0) ||
806 (EVP_PKEY_CTX_set_ec_param_enc(ctx.get(), OPENSSL_EC_NAMED_CURVE) <=
807 0) ||
808 (EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx.get(), ecGrp) <= 0) ||
809 (EVP_PKEY_paramgen(ctx.get(), &params) <= 0))
810 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500811 lg2::error("Error occurred setting curve parameters");
Andrew Geissler8dbcc722023-12-08 10:15:42 -0600812 ERR_print_errors_fp(stderr);
Patrick Williams26fb83e2021-12-14 14:08:28 -0600813 elog<InternalFailure>();
814 }
815
816 // Move parameters to RAII holder.
Nan Zhoue3d47cd2022-09-16 03:41:53 +0000817 auto pparms = holderOfKey(params);
Patrick Williams26fb83e2021-12-14 14:08:28 -0600818
819 // Create new context for key.
820 ctx.reset(EVP_PKEY_CTX_new_from_pkey(nullptr, params, nullptr));
821
822 if (!ctx || (EVP_PKEY_keygen_init(ctx.get()) <= 0))
823 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500824 lg2::error("Error occurred initializing keygen context");
Andrew Geissler8dbcc722023-12-08 10:15:42 -0600825 ERR_print_errors_fp(stderr);
Patrick Williams26fb83e2021-12-14 14:08:28 -0600826 elog<InternalFailure>();
827 }
828
829 EVP_PKEY* pKey = nullptr;
830 if (EVP_PKEY_keygen(ctx.get(), &pKey) <= 0)
831 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500832 lg2::error("Error occurred during generate EC key");
Andrew Geissler8dbcc722023-12-08 10:15:42 -0600833 ERR_print_errors_fp(stderr);
Patrick Williams26fb83e2021-12-14 14:08:28 -0600834 elog<InternalFailure>();
835 }
836
Nan Zhoue3d47cd2022-09-16 03:41:53 +0000837 return holderOfKey(pKey);
Patrick Williams26fb83e2021-12-14 14:08:28 -0600838#endif
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500839}
840
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800841void Manager::writePrivateKey(const EVPPkeyPtr& pKey,
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -0500842 const std::string& privKeyFileName)
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500843{
Ravi Tejaf2646272023-09-30 13:00:55 -0500844 lg2::info("Writing private key to file");
Marri Devender Raof4682712019-03-19 05:00:28 -0500845 // write private key to file
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -0500846 fs::path privKeyPath = certParentInstallPath / privKeyFileName;
Marri Devender Raof4682712019-03-19 05:00:28 -0500847
848 FILE* fp = std::fopen(privKeyPath.c_str(), "w");
Nan Zhoucfb58022021-12-28 11:02:26 -0800849 if (fp == nullptr)
Marri Devender Raof4682712019-03-19 05:00:28 -0500850 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500851 lg2::error("Error occurred creating private key file");
Marri Devender Raof4682712019-03-19 05:00:28 -0500852 elog<InternalFailure>();
853 }
Patrick Williamsa2f68d82024-08-16 15:21:36 -0400854 int ret =
855 PEM_write_PrivateKey(fp, pKey.get(), nullptr, nullptr, 0, 0, nullptr);
Marri Devender Raof4682712019-03-19 05:00:28 -0500856 std::fclose(fp);
857 if (ret == 0)
858 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500859 lg2::error("Error occurred while writing private key to file");
Marri Devender Raof4682712019-03-19 05:00:28 -0500860 elog<InternalFailure>();
861 }
Marri Devender Raof4682712019-03-19 05:00:28 -0500862}
863
864void Manager::addEntry(X509_NAME* x509Name, const char* field,
865 const std::string& bytes)
866{
867 if (bytes.empty())
868 {
869 return;
870 }
871 int ret = X509_NAME_add_entry_by_txt(
872 x509Name, field, MBSTRING_ASC,
873 reinterpret_cast<const unsigned char*>(bytes.c_str()), -1, -1, 0);
874 if (ret != 1)
875 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500876 lg2::error("Unable to set entry, FIELD:{FIELD}, VALUE:{VALUE}", "FIELD",
877 field, "VALUE", bytes);
Andrew Geissler8dbcc722023-12-08 10:15:42 -0600878 ERR_print_errors_fp(stderr);
Marri Devender Raof4682712019-03-19 05:00:28 -0500879 elog<InternalFailure>();
880 }
881}
882
883void Manager::createCSRObject(const Status& status)
884{
885 if (csrPtr)
886 {
887 csrPtr.reset(nullptr);
888 }
889 auto csrObjectPath = objectPath + '/' + "csr";
890 csrPtr = std::make_unique<CSR>(bus, csrObjectPath.c_str(),
891 certInstallPath.c_str(), status);
892}
893
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800894void Manager::writeCSR(const std::string& filePath, const X509ReqPtr& x509Req)
Marri Devender Raof4682712019-03-19 05:00:28 -0500895{
896 if (fs::exists(filePath))
897 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500898 lg2::info("Removing the existing file, FILENAME:{FILENAME}", "FILENAME",
899 filePath);
Marri Devender Raof4682712019-03-19 05:00:28 -0500900 if (!fs::remove(filePath.c_str()))
901 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500902 lg2::error("Unable to remove the file, FILENAME:{FILENAME}",
903 "FILENAME", filePath);
Marri Devender Raof4682712019-03-19 05:00:28 -0500904 elog<InternalFailure>();
905 }
906 }
907
Jayanth Othayothcb1ee9d2024-11-24 22:23:33 -0600908 FILE* fp = std::fopen(filePath.c_str(), "w");
Marri Devender Raof4682712019-03-19 05:00:28 -0500909
Jayanth Othayothcb1ee9d2024-11-24 22:23:33 -0600910 if (fp == nullptr)
Marri Devender Raof4682712019-03-19 05:00:28 -0500911 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500912 lg2::error(
913 "Error opening the file to write the CSR, FILENAME:{FILENAME}",
914 "FILENAME", filePath);
Marri Devender Raof4682712019-03-19 05:00:28 -0500915 elog<InternalFailure>();
916 }
917
918 int rc = PEM_write_X509_REQ(fp, x509Req.get());
919 if (!rc)
920 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500921 lg2::error("PEM write routine failed, FILENAME:{FILENAME}", "FILENAME",
922 filePath);
Marri Devender Raof4682712019-03-19 05:00:28 -0500923 std::fclose(fp);
924 elog<InternalFailure>();
925 }
926 std::fclose(fp);
927}
928
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200929void Manager::createCertificates()
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500930{
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200931 auto certObjectPath = objectPath + '/';
932
Nan Zhoue3d47cd2022-09-16 03:41:53 +0000933 if (certType == CertificateType::authority)
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500934 {
Zbigniew Lukwinskife590c42019-12-10 12:33:50 +0100935 // Check whether install path is a directory.
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200936 if (!fs::is_directory(certInstallPath))
937 {
Ravi Tejaf2646272023-09-30 13:00:55 -0500938 lg2::error("Certificate installation path exists and it is "
939 "not a directory");
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200940 elog<InternalFailure>();
Nan Zhou6ec13c82021-12-30 11:34:50 -0800941 }
942
943 // If the authorities list exists, recover from it and return
Patrick Williamsa2f68d82024-08-16 15:21:36 -0400944 if (fs::path authoritiesListFilePath =
945 fs::path(certInstallPath) / defaultAuthoritiesListFileName;
Nan Zhou6ec13c82021-12-30 11:34:50 -0800946 fs::exists(authoritiesListFilePath))
947 {
948 // remove all other files and directories
949 for (auto& path : fs::directory_iterator(certInstallPath))
950 {
951 if (path.path() != authoritiesListFilePath)
952 {
953 fs::remove_all(path);
954 }
955 }
956 installAll(authoritiesListFilePath);
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200957 return;
958 }
959
960 for (auto& path : fs::directory_iterator(certInstallPath))
961 {
962 try
963 {
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100964 // Assume here any regular file located in certificate directory
965 // contains certificates body. Do not want to use soft links
966 // would add value.
967 if (fs::is_regular_file(path))
968 {
969 installedCerts.emplace_back(std::make_unique<Certificate>(
970 bus, certObjectPath + std::to_string(certIdCounter++),
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800971 certType, certInstallPath, path.path(),
Willy Tu698a5742022-09-23 21:33:01 +0000972 certWatchPtr.get(), *this, /*restore=*/true));
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100973 }
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200974 }
975 catch (const InternalFailure& e)
976 {
977 report<InternalFailure>();
978 }
979 catch (const InvalidCertificate& e)
980 {
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800981 report<InvalidCertificate>(InvalidCertificateReason(
982 "Existing certificate file is corrupted"));
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200983 }
984 }
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500985 }
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200986 else if (fs::exists(certInstallPath))
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500987 {
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200988 try
989 {
990 installedCerts.emplace_back(std::make_unique<Certificate>(
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100991 bus, certObjectPath + '1', certType, certInstallPath,
Willy Tu698a5742022-09-23 21:33:01 +0000992 certInstallPath, certWatchPtr.get(), *this, /*restore=*/false));
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200993 }
994 catch (const InternalFailure& e)
995 {
996 report<InternalFailure>();
997 }
998 catch (const InvalidCertificate& e)
999 {
Nan Zhoucf06ccd2021-12-28 16:25:45 -08001000 report<InvalidCertificate>(InvalidCertificateReason(
1001 "Existing certificate file is corrupted"));
Kowalski, Kamildb029c92019-07-08 17:09:39 +02001002 }
Marri Devender Raoffad1ef2019-06-03 04:54:12 -05001003 }
1004}
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -05001005
1006void Manager::createRSAPrivateKeyFile()
1007{
Patrick Williamsa2f68d82024-08-16 15:21:36 -04001008 fs::path rsaPrivateKeyFileName =
1009 certParentInstallPath / defaultRSAPrivateKeyFileName;
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -05001010
1011 try
1012 {
1013 if (!fs::exists(rsaPrivateKeyFileName))
1014 {
Nan Zhoucf06ccd2021-12-28 16:25:45 -08001015 writePrivateKey(generateRSAKeyPair(supportedKeyBitLength),
Nan Zhou718eef32021-12-28 11:03:30 -08001016 defaultRSAPrivateKeyFileName);
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -05001017 }
1018 }
1019 catch (const InternalFailure& e)
1020 {
1021 report<InternalFailure>();
1022 }
1023}
1024
Nan Zhoucf06ccd2021-12-28 16:25:45 -08001025EVPPkeyPtr Manager::getRSAKeyPair(const int64_t keyBitLength)
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -05001026{
Nan Zhoucf06ccd2021-12-28 16:25:45 -08001027 if (keyBitLength != supportedKeyBitLength)
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -05001028 {
Ravi Tejaf2646272023-09-30 13:00:55 -05001029 lg2::error(
1030 "Given Key bit length is not supported, GIVENKEYBITLENGTH:"
1031 "{GIVENKEYBITLENGTH}, SUPPORTEDKEYBITLENGTH:{SUPPORTEDKEYBITLENGTH}",
1032 "GIVENKEYBITLENGTH", keyBitLength, "SUPPORTEDKEYBITLENGTH",
1033 supportedKeyBitLength);
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -05001034 elog<InvalidArgument>(
1035 Argument::ARGUMENT_NAME("KEYBITLENGTH"),
1036 Argument::ARGUMENT_VALUE(std::to_string(keyBitLength).c_str()));
1037 }
Patrick Williamsa2f68d82024-08-16 15:21:36 -04001038 fs::path rsaPrivateKeyFileName =
1039 certParentInstallPath / defaultRSAPrivateKeyFileName;
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -05001040
1041 FILE* privateKeyFile = std::fopen(rsaPrivateKeyFileName.c_str(), "r");
1042 if (!privateKeyFile)
1043 {
Ravi Tejaf2646272023-09-30 13:00:55 -05001044 lg2::error(
1045 "Unable to open RSA private key file to read, RSAKEYFILE:{RSAKEYFILE},"
1046 "ERRORREASON:{ERRORREASON}",
1047 "RSAKEYFILE", rsaPrivateKeyFileName, "ERRORREASON",
1048 strerror(errno));
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -05001049 elog<InternalFailure>();
1050 }
1051
Nan Zhoucf06ccd2021-12-28 16:25:45 -08001052 EVPPkeyPtr privateKey(
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -05001053 PEM_read_PrivateKey(privateKeyFile, nullptr, nullptr, nullptr),
1054 ::EVP_PKEY_free);
1055 std::fclose(privateKeyFile);
1056
1057 if (!privateKey)
1058 {
Ravi Tejaf2646272023-09-30 13:00:55 -05001059 lg2::error("Error occurred during PEM_read_PrivateKey call");
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -05001060 elog<InternalFailure>();
1061 }
1062 return privateKey;
1063}
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +01001064
1065void Manager::storageUpdate()
1066{
Nan Zhoue3d47cd2022-09-16 03:41:53 +00001067 if (certType == CertificateType::authority)
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +01001068 {
1069 // Remove symbolic links in the certificate directory
1070 for (auto& certPath : fs::directory_iterator(certInstallPath))
1071 {
1072 try
1073 {
1074 if (fs::is_symlink(certPath))
1075 {
1076 fs::remove(certPath);
1077 }
1078 }
1079 catch (const std::exception& e)
1080 {
Ravi Tejaf2646272023-09-30 13:00:55 -05001081 lg2::error(
1082 "Failed to remove symlink for certificate, ERR:{ERR} SYMLINK:{SYMLINK}",
1083 "ERR", e, "SYMLINK", certPath.path().string());
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +01001084 elog<InternalFailure>();
1085 }
1086 }
1087 }
1088
1089 for (const auto& cert : installedCerts)
1090 {
1091 cert->storageUpdate();
1092 }
1093}
1094
Nan Zhoucf06ccd2021-12-28 16:25:45 -08001095void Manager::reloadOrReset(const std::string& unit)
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +01001096{
1097 if (!unit.empty())
1098 {
1099 try
1100 {
Nan Zhoucf06ccd2021-12-28 16:25:45 -08001101 constexpr auto defaultSystemdService = "org.freedesktop.systemd1";
1102 constexpr auto defaultSystemdObjectPath =
1103 "/org/freedesktop/systemd1";
1104 constexpr auto defaultSystemdInterface =
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +01001105 "org.freedesktop.systemd1.Manager";
Nan Zhoucf06ccd2021-12-28 16:25:45 -08001106 auto method = bus.new_method_call(
1107 defaultSystemdService, defaultSystemdObjectPath,
1108 defaultSystemdInterface, "ReloadOrRestartUnit");
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +01001109 method.append(unit, "replace");
1110 bus.call_noreply(method);
1111 }
Patrick Williamsb3dbfb32022-07-22 19:26:57 -05001112 catch (const sdbusplus::exception_t& e)
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +01001113 {
Ravi Tejaf2646272023-09-30 13:00:55 -05001114 lg2::error(
1115 "Failed to reload or restart service, ERR:{ERR}, UNIT:{UNIT}",
1116 "ERR", e, "UNIT", unit);
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +01001117 elog<InternalFailure>();
1118 }
1119 }
1120}
1121
1122bool Manager::isCertificateUnique(const std::string& filePath,
1123 const Certificate* const certToDrop)
1124{
1125 if (std::any_of(
1126 installedCerts.begin(), installedCerts.end(),
Patrick Williams223e4602023-05-10 07:51:11 -05001127 [&filePath, certToDrop](const std::unique_ptr<Certificate>& cert) {
Patrick Williamsa2f68d82024-08-16 15:21:36 -04001128 return cert.get() != certToDrop && cert->isSame(filePath);
1129 }))
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +01001130 {
1131 return false;
1132 }
1133 else
1134 {
1135 return true;
1136 }
1137}
1138
Nan Zhoue1289ad2021-12-28 11:02:56 -08001139} // namespace phosphor::certs