csr.cpp (223e460421eebb1c598d9285b0cb01f1150fa50d) csr.cpp (f2646271e5fc66e4c5f3f8bfd6eeb68b6be3f103)
1#include "config.h"
2
3#include "csr.hpp"
4
5#include <openssl/bio.h>
6#include <openssl/buffer.h>
7#include <openssl/ossl_typ.h>
8#include <openssl/pem.h>
9#include <openssl/x509.h>
10
11#include <phosphor-logging/elog-errors.hpp>
12#include <phosphor-logging/elog.hpp>
1#include "config.h"
2
3#include "csr.hpp"
4
5#include <openssl/bio.h>
6#include <openssl/buffer.h>
7#include <openssl/ossl_typ.h>
8#include <openssl/pem.h>
9#include <openssl/x509.h>
10
11#include <phosphor-logging/elog-errors.hpp>
12#include <phosphor-logging/elog.hpp>
13#include <phosphor-logging/log.hpp>
13#include <phosphor-logging/lg2.hpp>
14#include <xyz/openbmc_project/Certs/error.hpp>
15#include <xyz/openbmc_project/Common/error.hpp>
16
17#include <cstdio>
18#include <filesystem>
19#include <memory>
20#include <utility>
21
22namespace phosphor::certs
23{
24
25using ::phosphor::logging::elog;
14#include <xyz/openbmc_project/Certs/error.hpp>
15#include <xyz/openbmc_project/Common/error.hpp>
16
17#include <cstdio>
18#include <filesystem>
19#include <memory>
20#include <utility>
21
22namespace phosphor::certs
23{
24
25using ::phosphor::logging::elog;
26using ::phosphor::logging::entry;
27using ::phosphor::logging::level;
28using ::phosphor::logging::log;
29using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
30namespace fs = std::filesystem;
31
32using X509ReqPtr = std::unique_ptr<X509_REQ, decltype(&::X509_REQ_free)>;
33using BIOPtr = std::unique_ptr<BIO, decltype(&::BIO_free_all)>;
34
35CSR::CSR(sdbusplus::bus_t& bus, const char* path, std::string&& installPath,
36 const Status& status) :

--- 4 unchanged lines hidden (view full) ---

41 // Emit deferred signal.
42 this->emit_object_added();
43}
44
45std::string CSR::csr()
46{
47 if (csrStatus == Status::failure)
48 {
26using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
27namespace fs = std::filesystem;
28
29using X509ReqPtr = std::unique_ptr<X509_REQ, decltype(&::X509_REQ_free)>;
30using BIOPtr = std::unique_ptr<BIO, decltype(&::BIO_free_all)>;
31
32CSR::CSR(sdbusplus::bus_t& bus, const char* path, std::string&& installPath,
33 const Status& status) :

--- 4 unchanged lines hidden (view full) ---

38 // Emit deferred signal.
39 this->emit_object_added();
40}
41
42std::string CSR::csr()
43{
44 if (csrStatus == Status::failure)
45 {
49 log<level::ERR>("Failure in Generating CSR");
46 lg2::error("Failure in Generating CSR");
50 elog<InternalFailure>();
51 }
52 fs::path csrFilePath = certInstallPath;
53 csrFilePath = csrFilePath.parent_path() / defaultCSRFileName;
54 if (!fs::exists(csrFilePath))
55 {
47 elog<InternalFailure>();
48 }
49 fs::path csrFilePath = certInstallPath;
50 csrFilePath = csrFilePath.parent_path() / defaultCSRFileName;
51 if (!fs::exists(csrFilePath))
52 {
56 log<level::ERR>("CSR file doesn't exists",
57 entry("FILENAME=%s", csrFilePath.c_str()));
53 lg2::error("CSR file doesn't exists, FILENAME:{FILENAME}", "FILENAME",
54 csrFilePath);
58 elog<InternalFailure>();
59 }
60
61 FILE* fp = std::fopen(csrFilePath.c_str(), "r");
62 X509ReqPtr x509Req(PEM_read_X509_REQ(fp, nullptr, nullptr, nullptr),
63 ::X509_REQ_free);
64 if (x509Req == nullptr || fp == nullptr)
65 {
66 if (fp != nullptr)
67 {
68 std::fclose(fp);
69 }
55 elog<InternalFailure>();
56 }
57
58 FILE* fp = std::fopen(csrFilePath.c_str(), "r");
59 X509ReqPtr x509Req(PEM_read_X509_REQ(fp, nullptr, nullptr, nullptr),
60 ::X509_REQ_free);
61 if (x509Req == nullptr || fp == nullptr)
62 {
63 if (fp != nullptr)
64 {
65 std::fclose(fp);
66 }
70 log<level::ERR>("ERROR occurred while reading CSR file",
71 entry("FILENAME=%s", csrFilePath.c_str()));
67 lg2::error("ERROR occurred while reading CSR file, FILENAME:{FILENAME}",
68 "FILENAME", csrFilePath);
72 elog<InternalFailure>();
73 }
74 std::fclose(fp);
75
76 BIOPtr bio(BIO_new(BIO_s_mem()), ::BIO_free_all);
77 int ret = PEM_write_bio_X509_REQ(bio.get(), x509Req.get());
78 if (ret <= 0)
79 {
69 elog<InternalFailure>();
70 }
71 std::fclose(fp);
72
73 BIOPtr bio(BIO_new(BIO_s_mem()), ::BIO_free_all);
74 int ret = PEM_write_bio_X509_REQ(bio.get(), x509Req.get());
75 if (ret <= 0)
76 {
80 log<level::ERR>("Error occurred while calling PEM_write_bio_X509_REQ");
77 lg2::error("Error occurred while calling PEM_write_bio_X509_REQ");
81 elog<InternalFailure>();
82 }
83
84 BUF_MEM* mem = nullptr;
85 BIO_get_mem_ptr(bio.get(), &mem);
86 std::string pem(mem->data, mem->length);
87 return pem;
88}
89
90} // namespace phosphor::certs
78 elog<InternalFailure>();
79 }
80
81 BUF_MEM* mem = nullptr;
82 BIO_get_mem_ptr(bio.get(), &mem);
83 std::string pem(mem->data, mem->length);
84 return pem;
85}
86
87} // namespace phosphor::certs