blob: 70ee5222b44be5f84274a5f55a1a294cba2f7650 [file] [log] [blame]
Marri Devender Rao6ceec402019-02-01 03:15:19 -06001#pragma once
2
Marri Devender Raoffad1ef2019-06-03 04:54:12 -05003#include "watch.hpp"
4
Marri Devender Rao6ceec402019-02-01 03:15:19 -06005#include <openssl/x509.h>
6
7#include <filesystem>
8#include <phosphor-logging/elog.hpp>
Marri Devender Raoedd11312019-02-27 08:45:10 -06009#include <xyz/openbmc_project/Certs/Certificate/server.hpp>
Marri Devender Rao13bf74e2019-03-26 01:52:17 -050010#include <xyz/openbmc_project/Certs/Replace/server.hpp>
Marri Devender Rao6ceec402019-02-01 03:15:19 -060011
12namespace phosphor
13{
14namespace certs
15{
Marri Devender Raoedd11312019-02-27 08:45:10 -060016using CertificateIface = sdbusplus::server::object::object<
17 sdbusplus::xyz::openbmc_project::Certs::server::Certificate>;
Marri Devender Rao13bf74e2019-03-26 01:52:17 -050018using ReplaceIface = sdbusplus::xyz::openbmc_project::Certs::server::Replace;
Marri Devender Raoedd11312019-02-27 08:45:10 -060019using CertIfaces =
Marri Devender Rao13bf74e2019-03-26 01:52:17 -050020 sdbusplus::server::object::object<CertificateIface, ReplaceIface>;
Marri Devender Raoedd11312019-02-27 08:45:10 -060021
Marri Devender Rao6ceec402019-02-01 03:15:19 -060022using CertificateType = std::string;
23using UnitsToRestart = std::string;
24using CertInstallPath = std::string;
25using CertUploadPath = std::string;
26using InputType = std::string;
27using InstallFunc = std::function<void(const std::string&)>;
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050028using CertWatchPtr = std::unique_ptr<Watch>;
Marri Devender Rao6ceec402019-02-01 03:15:19 -060029using namespace phosphor::logging;
30
31// for placeholders
32using namespace std::placeholders;
33namespace fs = std::filesystem;
34
35// Supported Types.
36static constexpr auto SERVER = "server";
37static constexpr auto CLIENT = "client";
38static constexpr auto AUTHORITY = "authority";
39
40// RAII support for openSSL functions.
41using X509_Ptr = std::unique_ptr<X509, decltype(&::X509_free)>;
42
43/** @class Certificate
44 * @brief OpenBMC Certificate entry implementation.
45 * @details A concrete implementation for the
46 * xyz.openbmc_project.Certs.Certificate DBus API
47 * xyz.openbmc_project.Certs.Instal DBus API
48 */
Marri Devender Raoedd11312019-02-27 08:45:10 -060049class Certificate : public CertIfaces
Marri Devender Rao6ceec402019-02-01 03:15:19 -060050{
51 public:
52 Certificate() = delete;
53 Certificate(const Certificate&) = delete;
54 Certificate& operator=(const Certificate&) = delete;
55 Certificate(Certificate&&) = delete;
56 Certificate& operator=(Certificate&&) = delete;
57 virtual ~Certificate();
58
59 /** @brief Constructor for the Certificate Object
60 * @param[in] bus - Bus to attach to.
61 * @param[in] objPath - Object path to attach to
62 * @param[in] type - Type of the certificate
63 * @param[in] unit - Units to restart after a certificate is installed
64 * @param[in] installPath - Path of the certificate to install
65 * @param[in] uploadPath - Path of the certificate file to upload
Marri Devender Rao8f80c352019-05-13 00:53:01 -050066 * @param[in] isSkipUnitReload - If true do not restart units
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050067 * @param[in] watchPtr - watch on self signed certificate pointer
Marri Devender Rao6ceec402019-02-01 03:15:19 -060068 */
69 Certificate(sdbusplus::bus::bus& bus, const std::string& objPath,
70 const CertificateType& type, const UnitsToRestart& unit,
71 const CertInstallPath& installPath,
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050072 const CertUploadPath& uploadPath, bool isSkipUnitReload,
73 const CertWatchPtr& watchPtr);
Marri Devender Rao6ceec402019-02-01 03:15:19 -060074
Marri Devender Rao13bf74e2019-03-26 01:52:17 -050075 /** @brief Validate certificate and replace the existing certificate
76 * @param[in] filePath - Certificate file path.
77 */
78 void replace(const std::string filePath) override;
79
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050080 /** @brief Populate certificate properties by parsing certificate file
81 * @return void
82 */
83 void populateProperties();
84
Marri Devender Rao13bf74e2019-03-26 01:52:17 -050085 private:
86 /** @brief Validate and Replace/Install the certificate file
87 * Install/Replace the existing certificate file with another
Marri Devender Rao6ceec402019-02-01 03:15:19 -060088 * (possibly CA signed) Certificate file.
89 * @param[in] filePath - Certificate file path.
Marri Devender Rao8f80c352019-05-13 00:53:01 -050090 * @param[in] isSkipUnitReload - If true do not restart units
Marri Devender Rao6ceec402019-02-01 03:15:19 -060091 */
Marri Devender Rao8f80c352019-05-13 00:53:01 -050092 void install(const std::string& filePath, bool isSkipUnitReload);
Marri Devender Rao6ceec402019-02-01 03:15:19 -060093
Marri Devender Rao6ceec402019-02-01 03:15:19 -060094 /** @brief Load Certificate file into the X509 structre.
95 * @param[in] fileName - Certificate and key full file path.
96 * @return pointer to the X509 structure.
97 */
98 X509_Ptr loadCert(const std::string& filePath);
99
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600100 /** @brief Public/Private key compare function.
101 * Comparing private key against certificate public key
102 * from input .pem file.
103 * @param[in] fileName - Certificate and key full file path.
104 * @return Return true if Key compare is successful,
105 * false if not
106 */
107 bool compareKeys(const std::string& filePath);
108 /** @brief systemd unit reload or reset helper function
109 * Reload if the unit supports it and use a restart otherwise.
110 * @param[in] unit - service need to reload.
111 */
112 void reloadOrReset(const UnitsToRestart& unit);
113
114 /** @brief Type specific function pointer map **/
115 std::unordered_map<InputType, InstallFunc> typeFuncMap;
116
117 /** @brief sdbusplus handler */
118 sdbusplus::bus::bus& bus;
119
120 /** @brief object path */
121 std::string objectPath;
122
123 /** @brief Type of the certificate **/
124 CertificateType certType;
125
126 /** @brief Unit name associated to the service **/
127 UnitsToRestart unitToRestart;
128
129 /** @brief Certificate file installation path **/
130 CertInstallPath certInstallPath;
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500131
132 /** @brief Certificate file create/update watch */
133 const CertWatchPtr& certWatchPtr;
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600134};
135
136} // namespace certs
137} // namespace phosphor