Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "csr.hpp" |
| 4 | |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 5 | #include <openssl/bio.h> |
| 6 | #include <openssl/buffer.h> |
| 7 | #include <openssl/ossl_typ.h> |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 8 | #include <openssl/pem.h> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 9 | #include <openssl/x509.h> |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 10 | |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 11 | #include <cstdio> |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 12 | #include <filesystem> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 13 | #include <memory> |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 14 | #include <phosphor-logging/elog-errors.hpp> |
| 15 | #include <phosphor-logging/elog.hpp> |
Nan Zhou | 014be0b | 2021-12-28 18:00:14 -0800 | [diff] [blame] | 16 | #include <phosphor-logging/log.hpp> |
| 17 | #include <utility> |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 18 | #include <xyz/openbmc_project/Certs/error.hpp> |
| 19 | #include <xyz/openbmc_project/Common/error.hpp> |
| 20 | |
Nan Zhou | e1289ad | 2021-12-28 11:02:56 -0800 | [diff] [blame] | 21 | namespace phosphor::certs |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 22 | { |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 23 | |
| 24 | using ::phosphor::logging::elog; |
| 25 | using ::phosphor::logging::entry; |
| 26 | using ::phosphor::logging::level; |
| 27 | using ::phosphor::logging::log; |
| 28 | using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 29 | namespace fs = std::filesystem; |
| 30 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 31 | using X509ReqPtr = std::unique_ptr<X509_REQ, decltype(&::X509_REQ_free)>; |
| 32 | using BIOPtr = std::unique_ptr<BIO, decltype(&::BIO_free_all)>; |
| 33 | |
| 34 | CSR::CSR(sdbusplus::bus::bus& bus, const char* path, std::string&& installPath, |
| 35 | const Status& status) : |
Patrick Williams | ebd21ba | 2022-04-05 14:58:53 -0500 | [diff] [blame] | 36 | internal::CSRInterface(bus, path, |
| 37 | internal::CSRInterface::action::defer_emit), |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 38 | objectPath(path), certInstallPath(std::move(installPath)), csrStatus(status) |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 39 | { |
| 40 | // Emit deferred signal. |
| 41 | this->emit_object_added(); |
| 42 | } |
| 43 | |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 44 | std::string CSR::csr() |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 45 | { |
| 46 | if (csrStatus == Status::FAILURE) |
| 47 | { |
| 48 | log<level::ERR>("Failure in Generating CSR"); |
| 49 | elog<InternalFailure>(); |
| 50 | } |
| 51 | fs::path csrFilePath = certInstallPath; |
Nan Zhou | 718eef3 | 2021-12-28 11:03:30 -0800 | [diff] [blame] | 52 | csrFilePath = csrFilePath.parent_path() / defaultCSRFileName; |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 53 | if (!fs::exists(csrFilePath)) |
| 54 | { |
| 55 | log<level::ERR>("CSR file doesn't exists", |
| 56 | entry("FILENAME=%s", csrFilePath.c_str())); |
| 57 | elog<InternalFailure>(); |
| 58 | } |
| 59 | |
| 60 | FILE* fp = std::fopen(csrFilePath.c_str(), "r"); |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 61 | X509ReqPtr x509Req(PEM_read_X509_REQ(fp, nullptr, nullptr, nullptr), |
| 62 | ::X509_REQ_free); |
Nan Zhou | cfb5802 | 2021-12-28 11:02:26 -0800 | [diff] [blame] | 63 | if (x509Req == nullptr || fp == nullptr) |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 64 | { |
Nan Zhou | cfb5802 | 2021-12-28 11:02:26 -0800 | [diff] [blame] | 65 | if (fp != nullptr) |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 66 | { |
| 67 | std::fclose(fp); |
| 68 | } |
Nan Zhou | bf3cf75 | 2021-12-28 11:02:07 -0800 | [diff] [blame] | 69 | log<level::ERR>("ERROR occurred while reading CSR file", |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 70 | entry("FILENAME=%s", csrFilePath.c_str())); |
| 71 | elog<InternalFailure>(); |
| 72 | } |
| 73 | std::fclose(fp); |
| 74 | |
Nan Zhou | cf06ccd | 2021-12-28 16:25:45 -0800 | [diff] [blame] | 75 | BIOPtr bio(BIO_new(BIO_s_mem()), ::BIO_free_all); |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 76 | int ret = PEM_write_bio_X509_REQ(bio.get(), x509Req.get()); |
| 77 | if (ret <= 0) |
| 78 | { |
Nan Zhou | bf3cf75 | 2021-12-28 11:02:07 -0800 | [diff] [blame] | 79 | log<level::ERR>("Error occurred while calling PEM_write_bio_X509_REQ"); |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 80 | elog<InternalFailure>(); |
| 81 | } |
| 82 | |
Nan Zhou | cfb5802 | 2021-12-28 11:02:26 -0800 | [diff] [blame] | 83 | BUF_MEM* mem = nullptr; |
Patrick Williams | e129be3 | 2021-04-30 20:35:19 -0500 | [diff] [blame] | 84 | BIO_get_mem_ptr(bio.get(), &mem); |
| 85 | std::string pem(mem->data, mem->length); |
| 86 | return pem; |
| 87 | } |
| 88 | |
Nan Zhou | e1289ad | 2021-12-28 11:02:56 -0800 | [diff] [blame] | 89 | } // namespace phosphor::certs |