blob: e271b1f3469ba7309c2defe1e4c2882a51ff351f [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>
Ravi Tejaf2646272023-09-30 13:00:55 -050013#include <phosphor-logging/lg2.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;
Nan Zhoucf06ccd2021-12-28 16:25:45 -080026using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Patrick Williamse129be32021-04-30 20:35:19 -050027namespace fs = std::filesystem;
28
Nan Zhoucf06ccd2021-12-28 16:25:45 -080029using X509ReqPtr = std::unique_ptr<X509_REQ, decltype(&::X509_REQ_free)>;
30using BIOPtr = std::unique_ptr<BIO, decltype(&::BIO_free_all)>;
31
Patrick Williamsb3dbfb32022-07-22 19:26:57 -050032CSR::CSR(sdbusplus::bus_t& bus, const char* path, std::string&& installPath,
Nan Zhoucf06ccd2021-12-28 16:25:45 -080033 const Status& status) :
Patrick Williamsebd21ba2022-04-05 14:58:53 -050034 internal::CSRInterface(bus, path,
35 internal::CSRInterface::action::defer_emit),
Nan Zhoucf06ccd2021-12-28 16:25:45 -080036 objectPath(path), certInstallPath(std::move(installPath)), csrStatus(status)
Patrick Williamse129be32021-04-30 20:35:19 -050037{
38 // Emit deferred signal.
39 this->emit_object_added();
40}
41
Patrick Williamse129be32021-04-30 20:35:19 -050042std::string CSR::csr()
Patrick Williamse129be32021-04-30 20:35:19 -050043{
Nan Zhoue3d47cd2022-09-16 03:41:53 +000044 if (csrStatus == Status::failure)
Patrick Williamse129be32021-04-30 20:35:19 -050045 {
Ravi Tejaf2646272023-09-30 13:00:55 -050046 lg2::error("Failure in Generating CSR");
Patrick Williamse129be32021-04-30 20:35:19 -050047 elog<InternalFailure>();
48 }
49 fs::path csrFilePath = certInstallPath;
Nan Zhou718eef32021-12-28 11:03:30 -080050 csrFilePath = csrFilePath.parent_path() / defaultCSRFileName;
Patrick Williamse129be32021-04-30 20:35:19 -050051 if (!fs::exists(csrFilePath))
52 {
Ravi Tejaf2646272023-09-30 13:00:55 -050053 lg2::error("CSR file doesn't exists, FILENAME:{FILENAME}", "FILENAME",
54 csrFilePath);
Patrick Williamse129be32021-04-30 20:35:19 -050055 elog<InternalFailure>();
56 }
57
58 FILE* fp = std::fopen(csrFilePath.c_str(), "r");
Nan Zhoucf06ccd2021-12-28 16:25:45 -080059 X509ReqPtr x509Req(PEM_read_X509_REQ(fp, nullptr, nullptr, nullptr),
60 ::X509_REQ_free);
Nan Zhoucfb58022021-12-28 11:02:26 -080061 if (x509Req == nullptr || fp == nullptr)
Patrick Williamse129be32021-04-30 20:35:19 -050062 {
Nan Zhoucfb58022021-12-28 11:02:26 -080063 if (fp != nullptr)
Patrick Williamse129be32021-04-30 20:35:19 -050064 {
65 std::fclose(fp);
66 }
Ravi Tejaf2646272023-09-30 13:00:55 -050067 lg2::error("ERROR occurred while reading CSR file, FILENAME:{FILENAME}",
68 "FILENAME", csrFilePath);
Patrick Williamse129be32021-04-30 20:35:19 -050069 elog<InternalFailure>();
70 }
71 std::fclose(fp);
72
Nan Zhoucf06ccd2021-12-28 16:25:45 -080073 BIOPtr bio(BIO_new(BIO_s_mem()), ::BIO_free_all);
Patrick Williamse129be32021-04-30 20:35:19 -050074 int ret = PEM_write_bio_X509_REQ(bio.get(), x509Req.get());
75 if (ret <= 0)
76 {
Ravi Tejaf2646272023-09-30 13:00:55 -050077 lg2::error("Error occurred while calling PEM_write_bio_X509_REQ");
Patrick Williamse129be32021-04-30 20:35:19 -050078 elog<InternalFailure>();
79 }
80
Nan Zhoucfb58022021-12-28 11:02:26 -080081 BUF_MEM* mem = nullptr;
Patrick Williamse129be32021-04-30 20:35:19 -050082 BIO_get_mem_ptr(bio.get(), &mem);
83 std::string pem(mem->data, mem->length);
84 return pem;
85}
86
Nan Zhoue1289ad2021-12-28 11:02:56 -080087} // namespace phosphor::certs