blob: 81c6f9832b316525ed2e8298c7f2c73f2bad5821 [file] [log] [blame]
Patrick Williamse129be32021-04-30 20:35:19 -05001#include "config.h"
2
3#include "csr.hpp"
4
5#include <openssl/pem.h>
6
7#include <filesystem>
8#include <phosphor-logging/elog-errors.hpp>
9#include <phosphor-logging/elog.hpp>
10#include <xyz/openbmc_project/Certs/error.hpp>
11#include <xyz/openbmc_project/Common/error.hpp>
12
Nan Zhoue1289ad2021-12-28 11:02:56 -080013namespace phosphor::certs
Patrick Williamse129be32021-04-30 20:35:19 -050014{
Nan Zhoucf06ccd2021-12-28 16:25:45 -080015
16using ::phosphor::logging::elog;
17using ::phosphor::logging::entry;
18using ::phosphor::logging::level;
19using ::phosphor::logging::log;
20using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Patrick Williamse129be32021-04-30 20:35:19 -050021namespace fs = std::filesystem;
22
Nan Zhoucf06ccd2021-12-28 16:25:45 -080023using X509ReqPtr = std::unique_ptr<X509_REQ, decltype(&::X509_REQ_free)>;
24using BIOPtr = std::unique_ptr<BIO, decltype(&::BIO_free_all)>;
25
26CSR::CSR(sdbusplus::bus::bus& bus, const char* path, std::string&& installPath,
27 const Status& status) :
28 internal::CSRInterface(bus, path, true),
29 objectPath(path), certInstallPath(std::move(installPath)), csrStatus(status)
Patrick Williamse129be32021-04-30 20:35:19 -050030{
31 // Emit deferred signal.
32 this->emit_object_added();
33}
34
Patrick Williamse129be32021-04-30 20:35:19 -050035std::string CSR::csr()
Patrick Williamse129be32021-04-30 20:35:19 -050036{
37 if (csrStatus == Status::FAILURE)
38 {
39 log<level::ERR>("Failure in Generating CSR");
40 elog<InternalFailure>();
41 }
42 fs::path csrFilePath = certInstallPath;
Nan Zhou718eef32021-12-28 11:03:30 -080043 csrFilePath = csrFilePath.parent_path() / defaultCSRFileName;
Patrick Williamse129be32021-04-30 20:35:19 -050044 if (!fs::exists(csrFilePath))
45 {
46 log<level::ERR>("CSR file doesn't exists",
47 entry("FILENAME=%s", csrFilePath.c_str()));
48 elog<InternalFailure>();
49 }
50
51 FILE* fp = std::fopen(csrFilePath.c_str(), "r");
Nan Zhoucf06ccd2021-12-28 16:25:45 -080052 X509ReqPtr x509Req(PEM_read_X509_REQ(fp, nullptr, nullptr, nullptr),
53 ::X509_REQ_free);
Nan Zhoucfb58022021-12-28 11:02:26 -080054 if (x509Req == nullptr || fp == nullptr)
Patrick Williamse129be32021-04-30 20:35:19 -050055 {
Nan Zhoucfb58022021-12-28 11:02:26 -080056 if (fp != nullptr)
Patrick Williamse129be32021-04-30 20:35:19 -050057 {
58 std::fclose(fp);
59 }
Nan Zhoubf3cf752021-12-28 11:02:07 -080060 log<level::ERR>("ERROR occurred while reading CSR file",
Patrick Williamse129be32021-04-30 20:35:19 -050061 entry("FILENAME=%s", csrFilePath.c_str()));
62 elog<InternalFailure>();
63 }
64 std::fclose(fp);
65
Nan Zhoucf06ccd2021-12-28 16:25:45 -080066 BIOPtr bio(BIO_new(BIO_s_mem()), ::BIO_free_all);
Patrick Williamse129be32021-04-30 20:35:19 -050067 int ret = PEM_write_bio_X509_REQ(bio.get(), x509Req.get());
68 if (ret <= 0)
69 {
Nan Zhoubf3cf752021-12-28 11:02:07 -080070 log<level::ERR>("Error occurred while calling PEM_write_bio_X509_REQ");
Patrick Williamse129be32021-04-30 20:35:19 -050071 elog<InternalFailure>();
72 }
73
Nan Zhoucfb58022021-12-28 11:02:26 -080074 BUF_MEM* mem = nullptr;
Patrick Williamse129be32021-04-30 20:35:19 -050075 BIO_get_mem_ptr(bio.get(), &mem);
76 std::string pem(mem->data, mem->length);
77 return pem;
78}
79
Nan Zhoue1289ad2021-12-28 11:02:56 -080080} // namespace phosphor::certs