blob: 878ae710ad883b3ec24cc3dc76444e880ad1334c [file] [log] [blame]
Marri Devender Rao6ceec402019-02-01 03:15:19 -06001#pragma once
2
3#include <openssl/x509.h>
4
5#include <filesystem>
6#include <phosphor-logging/elog.hpp>
Marri Devender Raoedd11312019-02-27 08:45:10 -06007#include <xyz/openbmc_project/Certs/Certificate/server.hpp>
8#include <xyz/openbmc_project/Certs/Install/server.hpp>
Marri Devender Rao6ceec402019-02-01 03:15:19 -06009
10namespace phosphor
11{
12namespace certs
13{
Marri Devender Raoedd11312019-02-27 08:45:10 -060014using CertificateIface = sdbusplus::server::object::object<
15 sdbusplus::xyz::openbmc_project::Certs::server::Certificate>;
16using InstallIface = sdbusplus::xyz::openbmc_project::Certs::server::Install;
17using CertIfaces =
18 sdbusplus::server::object::object<CertificateIface, InstallIface>;
19
Marri Devender Rao6ceec402019-02-01 03:15:19 -060020using CertificateType = std::string;
21using UnitsToRestart = std::string;
22using CertInstallPath = std::string;
23using CertUploadPath = std::string;
24using InputType = std::string;
25using InstallFunc = std::function<void(const std::string&)>;
26
27using namespace phosphor::logging;
28
29// for placeholders
30using namespace std::placeholders;
31namespace fs = std::filesystem;
32
33// Supported Types.
34static constexpr auto SERVER = "server";
35static constexpr auto CLIENT = "client";
36static constexpr auto AUTHORITY = "authority";
37
38// RAII support for openSSL functions.
39using X509_Ptr = std::unique_ptr<X509, decltype(&::X509_free)>;
40
41/** @class Certificate
42 * @brief OpenBMC Certificate entry implementation.
43 * @details A concrete implementation for the
44 * xyz.openbmc_project.Certs.Certificate DBus API
45 * xyz.openbmc_project.Certs.Instal DBus API
46 */
Marri Devender Raoedd11312019-02-27 08:45:10 -060047class Certificate : public CertIfaces
Marri Devender Rao6ceec402019-02-01 03:15:19 -060048{
49 public:
50 Certificate() = delete;
51 Certificate(const Certificate&) = delete;
52 Certificate& operator=(const Certificate&) = delete;
53 Certificate(Certificate&&) = delete;
54 Certificate& operator=(Certificate&&) = delete;
55 virtual ~Certificate();
56
57 /** @brief Constructor for the Certificate Object
58 * @param[in] bus - Bus to attach to.
59 * @param[in] objPath - Object path to attach to
60 * @param[in] type - Type of the certificate
61 * @param[in] unit - Units to restart after a certificate is installed
62 * @param[in] installPath - Path of the certificate to install
63 * @param[in] uploadPath - Path of the certificate file to upload
64 */
65 Certificate(sdbusplus::bus::bus& bus, const std::string& objPath,
66 const CertificateType& type, const UnitsToRestart& unit,
67 const CertInstallPath& installPath,
68 const CertUploadPath& uploadPath);
69
70 /** @brief Implementation for Install
71 * Replace the existing certificate file with another
72 * (possibly CA signed) Certificate file.
73 * @param[in] filePath - Certificate file path.
74 */
Marri Devender Raoedd11312019-02-27 08:45:10 -060075 void install(const std::string filePath) override;
Marri Devender Rao6ceec402019-02-01 03:15:19 -060076
77 private:
78 /** @brief Load Certificate file into the X509 structre.
79 * @param[in] fileName - Certificate and key full file path.
80 * @return pointer to the X509 structure.
81 */
82 X509_Ptr loadCert(const std::string& filePath);
83
Dhruvaraj Subhashchandran36f25142019-02-14 05:06:26 -060084 /** @brief Populate certificate properties by parsing certificate file
85 * @return void
86 */
87 void populateProperties();
88
Marri Devender Rao6ceec402019-02-01 03:15:19 -060089 /** @brief Public/Private key compare function.
90 * Comparing private key against certificate public key
91 * from input .pem file.
92 * @param[in] fileName - Certificate and key full file path.
93 * @return Return true if Key compare is successful,
94 * false if not
95 */
96 bool compareKeys(const std::string& filePath);
97 /** @brief systemd unit reload or reset helper function
98 * Reload if the unit supports it and use a restart otherwise.
99 * @param[in] unit - service need to reload.
100 */
101 void reloadOrReset(const UnitsToRestart& unit);
102
103 /** @brief Type specific function pointer map **/
104 std::unordered_map<InputType, InstallFunc> typeFuncMap;
105
106 /** @brief sdbusplus handler */
107 sdbusplus::bus::bus& bus;
108
109 /** @brief object path */
110 std::string objectPath;
111
112 /** @brief Type of the certificate **/
113 CertificateType certType;
114
115 /** @brief Unit name associated to the service **/
116 UnitsToRestart unitToRestart;
117
118 /** @brief Certificate file installation path **/
119 CertInstallPath certInstallPath;
120};
121
122} // namespace certs
123} // namespace phosphor