blob: 9ba8f00425c133e2f1906ad2d637833d76a53c56 [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
Nan Zhou014be0b2021-12-28 18:00:14 -080011#include <cstdio>
Patrick Williamse129be32021-04-30 20:35:19 -050012#include <filesystem>
Nan Zhou014be0b2021-12-28 18:00:14 -080013#include <memory>
Patrick Williamse129be32021-04-30 20:35:19 -050014#include <phosphor-logging/elog-errors.hpp>
15#include <phosphor-logging/elog.hpp>
Nan Zhou014be0b2021-12-28 18:00:14 -080016#include <phosphor-logging/log.hpp>
17#include <utility>
Patrick Williamse129be32021-04-30 20:35:19 -050018#include <xyz/openbmc_project/Certs/error.hpp>
19#include <xyz/openbmc_project/Common/error.hpp>
20
Nan Zhoue1289ad2021-12-28 11:02:56 -080021namespace phosphor::certs
Patrick Williamse129be32021-04-30 20:35:19 -050022{
Nan Zhoucf06ccd2021-12-28 16:25:45 -080023
24using ::phosphor::logging::elog;
25using ::phosphor::logging::entry;
26using ::phosphor::logging::level;
27using ::phosphor::logging::log;
28using ::sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Patrick Williamse129be32021-04-30 20:35:19 -050029namespace fs = std::filesystem;
30
Nan Zhoucf06ccd2021-12-28 16:25:45 -080031using X509ReqPtr = std::unique_ptr<X509_REQ, decltype(&::X509_REQ_free)>;
32using BIOPtr = std::unique_ptr<BIO, decltype(&::BIO_free_all)>;
33
34CSR::CSR(sdbusplus::bus::bus& bus, const char* path, std::string&& installPath,
35 const Status& status) :
36 internal::CSRInterface(bus, path, true),
37 objectPath(path), certInstallPath(std::move(installPath)), csrStatus(status)
Patrick Williamse129be32021-04-30 20:35:19 -050038{
39 // Emit deferred signal.
40 this->emit_object_added();
41}
42
Patrick Williamse129be32021-04-30 20:35:19 -050043std::string CSR::csr()
Patrick Williamse129be32021-04-30 20:35:19 -050044{
45 if (csrStatus == Status::FAILURE)
46 {
47 log<level::ERR>("Failure in Generating CSR");
48 elog<InternalFailure>();
49 }
50 fs::path csrFilePath = certInstallPath;
Nan Zhou718eef32021-12-28 11:03:30 -080051 csrFilePath = csrFilePath.parent_path() / defaultCSRFileName;
Patrick Williamse129be32021-04-30 20:35:19 -050052 if (!fs::exists(csrFilePath))
53 {
54 log<level::ERR>("CSR file doesn't exists",
55 entry("FILENAME=%s", csrFilePath.c_str()));
56 elog<InternalFailure>();
57 }
58
59 FILE* fp = std::fopen(csrFilePath.c_str(), "r");
Nan Zhoucf06ccd2021-12-28 16:25:45 -080060 X509ReqPtr x509Req(PEM_read_X509_REQ(fp, nullptr, nullptr, nullptr),
61 ::X509_REQ_free);
Nan Zhoucfb58022021-12-28 11:02:26 -080062 if (x509Req == nullptr || fp == nullptr)
Patrick Williamse129be32021-04-30 20:35:19 -050063 {
Nan Zhoucfb58022021-12-28 11:02:26 -080064 if (fp != nullptr)
Patrick Williamse129be32021-04-30 20:35:19 -050065 {
66 std::fclose(fp);
67 }
Nan Zhoubf3cf752021-12-28 11:02:07 -080068 log<level::ERR>("ERROR occurred while reading CSR file",
Patrick Williamse129be32021-04-30 20:35:19 -050069 entry("FILENAME=%s", csrFilePath.c_str()));
70 elog<InternalFailure>();
71 }
72 std::fclose(fp);
73
Nan Zhoucf06ccd2021-12-28 16:25:45 -080074 BIOPtr bio(BIO_new(BIO_s_mem()), ::BIO_free_all);
Patrick Williamse129be32021-04-30 20:35:19 -050075 int ret = PEM_write_bio_X509_REQ(bio.get(), x509Req.get());
76 if (ret <= 0)
77 {
Nan Zhoubf3cf752021-12-28 11:02:07 -080078 log<level::ERR>("Error occurred while calling PEM_write_bio_X509_REQ");
Patrick Williamse129be32021-04-30 20:35:19 -050079 elog<InternalFailure>();
80 }
81
Nan Zhoucfb58022021-12-28 11:02:26 -080082 BUF_MEM* mem = nullptr;
Patrick Williamse129be32021-04-30 20:35:19 -050083 BIO_get_mem_ptr(bio.get(), &mem);
84 std::string pem(mem->data, mem->length);
85 return pem;
86}
87
Nan Zhoue1289ad2021-12-28 11:02:56 -080088} // namespace phosphor::certs