blob: 1ea3991aed50dc13deca2e954358ff8aaaffbb2b [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{
Patrick Williamse129be32021-04-30 20:35:19 -050015using X509_REQ_Ptr = std::unique_ptr<X509_REQ, decltype(&::X509_REQ_free)>;
16using BIO_Ptr = std::unique_ptr<BIO, decltype(&::BIO_free_all)>;
17using InternalFailure =
18 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
19using namespace phosphor::logging;
20namespace fs = std::filesystem;
21
22CSR::CSR(sdbusplus::bus::bus& bus, const char* path,
23 CertInstallPath&& installPath, const Status& status) :
24 CSRIface(bus, path, true),
25 bus(bus), objectPath(path), certInstallPath(std::move(installPath)),
26 csrStatus(status)
27{
28 // Emit deferred signal.
29 this->emit_object_added();
30}
31
Patrick Williamse129be32021-04-30 20:35:19 -050032std::string CSR::csr()
Patrick Williamse129be32021-04-30 20:35:19 -050033{
34 if (csrStatus == Status::FAILURE)
35 {
36 log<level::ERR>("Failure in Generating CSR");
37 elog<InternalFailure>();
38 }
39 fs::path csrFilePath = certInstallPath;
40 csrFilePath = csrFilePath.parent_path() / CSR_FILE_NAME;
41 if (!fs::exists(csrFilePath))
42 {
43 log<level::ERR>("CSR file doesn't exists",
44 entry("FILENAME=%s", csrFilePath.c_str()));
45 elog<InternalFailure>();
46 }
47
48 FILE* fp = std::fopen(csrFilePath.c_str(), "r");
Nan Zhoucfb58022021-12-28 11:02:26 -080049 X509_REQ_Ptr x509Req(PEM_read_X509_REQ(fp, nullptr, nullptr, nullptr),
Patrick Williamse129be32021-04-30 20:35:19 -050050 ::X509_REQ_free);
Nan Zhoucfb58022021-12-28 11:02:26 -080051 if (x509Req == nullptr || fp == nullptr)
Patrick Williamse129be32021-04-30 20:35:19 -050052 {
Nan Zhoucfb58022021-12-28 11:02:26 -080053 if (fp != nullptr)
Patrick Williamse129be32021-04-30 20:35:19 -050054 {
55 std::fclose(fp);
56 }
Nan Zhoubf3cf752021-12-28 11:02:07 -080057 log<level::ERR>("ERROR occurred while reading CSR file",
Patrick Williamse129be32021-04-30 20:35:19 -050058 entry("FILENAME=%s", csrFilePath.c_str()));
59 elog<InternalFailure>();
60 }
61 std::fclose(fp);
62
63 BIO_Ptr bio(BIO_new(BIO_s_mem()), ::BIO_free_all);
64 int ret = PEM_write_bio_X509_REQ(bio.get(), x509Req.get());
65 if (ret <= 0)
66 {
Nan Zhoubf3cf752021-12-28 11:02:07 -080067 log<level::ERR>("Error occurred while calling PEM_write_bio_X509_REQ");
Patrick Williamse129be32021-04-30 20:35:19 -050068 elog<InternalFailure>();
69 }
70
Nan Zhoucfb58022021-12-28 11:02:26 -080071 BUF_MEM* mem = nullptr;
Patrick Williamse129be32021-04-30 20:35:19 -050072 BIO_get_mem_ptr(bio.get(), &mem);
73 std::string pem(mem->data, mem->length);
74 return pem;
75}
76
Nan Zhoue1289ad2021-12-28 11:02:56 -080077} // namespace phosphor::certs