blob: 76f64b49f89ed41279d5e99382e84120ab146ec0 [file] [log] [blame]
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -05001#pragma once
Jayanth Othayothdd74bd22018-09-28 06:13:35 -05002#include <openssl/x509.h>
3
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -05004#include <cstring>
5#include <sdbusplus/bus.hpp>
6#include <sdbusplus/server/object.hpp>
7#include <unordered_map>
8#include <xyz/openbmc_project/Certs/Install/server.hpp>
9
10namespace phosphor
11{
12namespace certs
13{
Jayanth Othayothdd74bd22018-09-28 06:13:35 -050014// RAII support for openSSL functions.
15using X509_Ptr = std::unique_ptr<X509, decltype(&::X509_free)>;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050016
17// Supported Types.
18static constexpr auto SERVER = "server";
19static constexpr auto CLIENT = "client";
20
21using CreateIface = sdbusplus::server::object::object<
22 sdbusplus::xyz::openbmc_project::Certs::server::Install>;
23using InstallFunc = std::function<void()>;
24using InputType = std::string;
25
26class Manager : public CreateIface
27{
28 public:
29 /* Define all of the basic class operations:
30 * Not allowed:
31 * - Default constructor is not possible due to member
32 * reference
33 * - Move operations due to 'this' being registered as the
34 * 'context' with sdbus.
35 * Allowed:
36 * - copy
37 * - Destructor.
38 */
39 Manager() = delete;
40 Manager(const Manager&) = default;
41 Manager& operator=(const Manager&) = delete;
42 Manager(Manager&&) = delete;
43 Manager& operator=(Manager&&) = delete;
44 virtual ~Manager() = default;
45
46 /** @brief Constructor to put object onto bus at a dbus path.
47 * @param[in] bus - Bus to attach to.
48 * @param[in] path - Path to attach at.
49 * @param[in] type - Type of the certificate.
50 * @param[in] unit - Unit consumed by this certificate.
51 * @param[in] certpath - Certificate installation path.
52 */
53 Manager(sdbusplus::bus::bus& bus, const char* path, const std::string& type,
54 std::string&& unit, std::string&& certPath) :
55 CreateIface(bus, path),
56 bus(bus), path(path), type(type), unit(std::move(unit)),
57 certPath(std::move(certPath))
58 {
59 typeFuncMap[SERVER] =
60 std::bind(&phosphor::certs::Manager::serverInstall, this);
61 typeFuncMap[CLIENT] =
62 std::bind(&phosphor::certs::Manager::clientInstall, this);
63 }
64
65 /** @brief Implementation for Install
66 * Replace the existing certificate key file with another
67 * (possibly CA signed) Certificate key file.
68 *
69 * @param[in] path - Certificate key file path.
70 */
71 void install(const std::string path) override;
72
73 private:
74 /** @brief Client certificate Installation helper function **/
Marri Devender Rao947258d2018-09-25 10:52:24 -050075 virtual void clientInstall();
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050076
77 /** @brief Server certificate Installation helper function **/
Marri Devender Rao947258d2018-09-25 10:52:24 -050078 virtual void serverInstall();
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050079
80 /** @brief systemd unit reload helper function
81 * @param[in] unit - service need to reload.
82 */
83 void reload(const std::string& unit);
84
85 /** @brief helper function to copy the file.
86 * @param[in] src - Source file path to copy
87 * @param[in] dst - Destination path to copy
88 */
89 void copy(const std::string& src, const std::string& dst);
90
Jayanth Othayothdd74bd22018-09-28 06:13:35 -050091 /** @brief Certificate verification function
92 * Certificate file specific validation using openssl
93 * verify function also includes expiry date check
94 * @param[in] fileName - Certificate and key full file path.
95 * @return error code from open ssl verify function.
96 */
97 int32_t verifyCert(const std::string& filePath);
98
99 /** @brief Load Certificate file into the X509 structre.
100 * @param[in] fileName - Certificate and key full file path.
101 * @return pointer to the X509 structure.
102 */
103 X509_Ptr loadCert(const std::string& filePath);
104
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500105 /** @brief sdbusplus handler */
106 sdbusplus::bus::bus& bus;
107
108 /** @brief object path */
109 std::string path;
110
111 /** @brief Type of the certificate **/
112 InputType type;
113
114 /** @brief Unit name associated to the service **/
115 std::string unit;
116
117 /** @brief Certificate file installation path **/
118 std::string certPath;
119
120 /** @brief Type specific function pointer map **/
121 std::unordered_map<InputType, InstallFunc> typeFuncMap;
122};
123
124} // namespace certs
125} // namespace phosphor