blob: 88fb761a2da7e1834893bd941d0c1f3e916cb54e [file] [log] [blame]
Patrick Williamse129be32021-04-30 20:35:19 -05001#include "config.h"
2
3#include "csr.hpp"
4
Nan Zhou014be0b2021-12-28 18:00:14 -08005#include <openssl/bio.h>
6#include <openssl/buffer.h>
7#include <openssl/ossl_typ.h>
Patrick Williamse129be32021-04-30 20:35:19 -05008#include <openssl/pem.h>
Nan Zhou014be0b2021-12-28 18:00:14 -08009#include <openssl/x509.h>
Patrick Williamse129be32021-04-30 20:35:19 -050010
Patrick Williamse129be32021-04-30 20:35:19 -050011#include <phosphor-logging/elog-errors.hpp>
12#include <phosphor-logging/elog.hpp>
Nan Zhou014be0b2021-12-28 18:00:14 -080013#include <phosphor-logging/log.hpp>
Patrick Williamse129be32021-04-30 20:35:19 -050014#include <xyz/openbmc_project/Certs/error.hpp>
15#include <xyz/openbmc_project/Common/error.hpp>
16
Patrick Williams223e4602023-05-10 07:51:11 -050017#include <cstdio>
18#include <filesystem>
19#include <memory>
20#include <utility>
21
Nan Zhoue1289ad2021-12-28 11:02:56 -080022namespace phosphor::certs
Patrick Williamse129be32021-04-30 20:35:19 -050023{
Nan Zhoucf06ccd2021-12-28 16:25:45 -080024
25using ::phosphor::logging::elog;
26using ::phosphor::logging::entry;
27using ::phosphor::logging::level;
28using ::phosphor::logging::log;
29using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Patrick Williamse129be32021-04-30 20:35:19 -050030namespace fs = std::filesystem;
31
Nan Zhoucf06ccd2021-12-28 16:25:45 -080032using X509ReqPtr = std::unique_ptr<X509_REQ, decltype(&::X509_REQ_free)>;
33using BIOPtr = std::unique_ptr<BIO, decltype(&::BIO_free_all)>;
34
Patrick Williamsb3dbfb32022-07-22 19:26:57 -050035CSR::CSR(sdbusplus::bus_t& bus, const char* path, std::string&& installPath,
Nan Zhoucf06ccd2021-12-28 16:25:45 -080036 const Status& status) :
Patrick Williamsebd21ba2022-04-05 14:58:53 -050037 internal::CSRInterface(bus, path,
38 internal::CSRInterface::action::defer_emit),
Nan Zhoucf06ccd2021-12-28 16:25:45 -080039 objectPath(path), certInstallPath(std::move(installPath)), csrStatus(status)
Patrick Williamse129be32021-04-30 20:35:19 -050040{
41 // Emit deferred signal.
42 this->emit_object_added();
43}
44
Patrick Williamse129be32021-04-30 20:35:19 -050045std::string CSR::csr()
Patrick Williamse129be32021-04-30 20:35:19 -050046{
Nan Zhoue3d47cd2022-09-16 03:41:53 +000047 if (csrStatus == Status::failure)
Patrick Williamse129be32021-04-30 20:35:19 -050048 {
49 log<level::ERR>("Failure in Generating CSR");
50 elog<InternalFailure>();
51 }
52 fs::path csrFilePath = certInstallPath;
Nan Zhou718eef32021-12-28 11:03:30 -080053 csrFilePath = csrFilePath.parent_path() / defaultCSRFileName;
Patrick Williamse129be32021-04-30 20:35:19 -050054 if (!fs::exists(csrFilePath))
55 {
56 log<level::ERR>("CSR file doesn't exists",
57 entry("FILENAME=%s", csrFilePath.c_str()));
58 elog<InternalFailure>();
59 }
60
61 FILE* fp = std::fopen(csrFilePath.c_str(), "r");
Nan Zhoucf06ccd2021-12-28 16:25:45 -080062 X509ReqPtr x509Req(PEM_read_X509_REQ(fp, nullptr, nullptr, nullptr),
63 ::X509_REQ_free);
Nan Zhoucfb58022021-12-28 11:02:26 -080064 if (x509Req == nullptr || fp == nullptr)
Patrick Williamse129be32021-04-30 20:35:19 -050065 {
Nan Zhoucfb58022021-12-28 11:02:26 -080066 if (fp != nullptr)
Patrick Williamse129be32021-04-30 20:35:19 -050067 {
68 std::fclose(fp);
69 }
Nan Zhoubf3cf752021-12-28 11:02:07 -080070 log<level::ERR>("ERROR occurred while reading CSR file",
Patrick Williamse129be32021-04-30 20:35:19 -050071 entry("FILENAME=%s", csrFilePath.c_str()));
72 elog<InternalFailure>();
73 }
74 std::fclose(fp);
75
Nan Zhoucf06ccd2021-12-28 16:25:45 -080076 BIOPtr bio(BIO_new(BIO_s_mem()), ::BIO_free_all);
Patrick Williamse129be32021-04-30 20:35:19 -050077 int ret = PEM_write_bio_X509_REQ(bio.get(), x509Req.get());
78 if (ret <= 0)
79 {
Nan Zhoubf3cf752021-12-28 11:02:07 -080080 log<level::ERR>("Error occurred while calling PEM_write_bio_X509_REQ");
Patrick Williamse129be32021-04-30 20:35:19 -050081 elog<InternalFailure>();
82 }
83
Nan Zhoucfb58022021-12-28 11:02:26 -080084 BUF_MEM* mem = nullptr;
Patrick Williamse129be32021-04-30 20:35:19 -050085 BIO_get_mem_ptr(bio.get(), &mem);
86 std::string pem(mem->data, mem->length);
87 return pem;
88}
89
Nan Zhoue1289ad2021-12-28 11:02:56 -080090} // namespace phosphor::certs