blob: 7d42d01cbc801019a8c0ff6ffdbb8c3cd78baa5d [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>
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -05009#include <xyz/openbmc_project/Object/Delete/server.hpp>
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050010
11namespace phosphor
12{
13namespace certs
14{
Jayanth Othayothdd74bd22018-09-28 06:13:35 -050015// RAII support for openSSL functions.
16using X509_Ptr = std::unique_ptr<X509, decltype(&::X509_free)>;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050017
18// Supported Types.
19static constexpr auto SERVER = "server";
20static constexpr auto CLIENT = "client";
21
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050022using Create = sdbusplus::xyz::openbmc_project::Certs::server::Install;
23using Delete = sdbusplus::xyz::openbmc_project::Object::server::Delete;
24using Ifaces = sdbusplus::server::object::object<Create, Delete>;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050025using InstallFunc = std::function<void()>;
26using InputType = std::string;
27
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050028class Manager : public Ifaces
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050029{
30 public:
31 /* Define all of the basic class operations:
32 * Not allowed:
33 * - Default constructor is not possible due to member
34 * reference
35 * - Move operations due to 'this' being registered as the
36 * 'context' with sdbus.
37 * Allowed:
38 * - copy
39 * - Destructor.
40 */
41 Manager() = delete;
42 Manager(const Manager&) = default;
43 Manager& operator=(const Manager&) = delete;
44 Manager(Manager&&) = delete;
45 Manager& operator=(Manager&&) = delete;
46 virtual ~Manager() = default;
47
48 /** @brief Constructor to put object onto bus at a dbus path.
49 * @param[in] bus - Bus to attach to.
50 * @param[in] path - Path to attach at.
51 * @param[in] type - Type of the certificate.
52 * @param[in] unit - Unit consumed by this certificate.
53 * @param[in] certpath - Certificate installation path.
54 */
55 Manager(sdbusplus::bus::bus& bus, const char* path, const std::string& type,
56 std::string&& unit, std::string&& certPath) :
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050057 Ifaces(bus, path),
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050058 bus(bus), path(path), type(type), unit(std::move(unit)),
59 certPath(std::move(certPath))
60 {
61 typeFuncMap[SERVER] =
62 std::bind(&phosphor::certs::Manager::serverInstall, this);
63 typeFuncMap[CLIENT] =
64 std::bind(&phosphor::certs::Manager::clientInstall, this);
65 }
66
67 /** @brief Implementation for Install
68 * Replace the existing certificate key file with another
69 * (possibly CA signed) Certificate key file.
70 *
71 * @param[in] path - Certificate key file path.
72 */
73 void install(const std::string path) override;
74
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050075 /** @brief Delete the certificate (and possibly revert
76 * to a self-signed certificate).
77 */
78 void delete_() override;
79
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050080 private:
81 /** @brief Client certificate Installation helper function **/
Marri Devender Rao947258d2018-09-25 10:52:24 -050082 virtual void clientInstall();
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050083
84 /** @brief Server certificate Installation helper function **/
Marri Devender Rao947258d2018-09-25 10:52:24 -050085 virtual void serverInstall();
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050086
Jayanth Othayothe8199a82018-09-29 00:46:10 -050087 /** @brief systemd unit reload or reset helper function
88 * Reload if the unit supports it and use a restart otherwise.
89 * @param[in] unit - service need to reload.
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050090 */
Jayanth Othayothe8199a82018-09-29 00:46:10 -050091 void reloadOrReset(const std::string& unit);
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050092
93 /** @brief helper function to copy the file.
94 * @param[in] src - Source file path to copy
95 * @param[in] dst - Destination path to copy
96 */
97 void copy(const std::string& src, const std::string& dst);
98
Jayanth Othayothdd74bd22018-09-28 06:13:35 -050099 /** @brief Certificate verification function
100 * Certificate file specific validation using openssl
101 * verify function also includes expiry date check
102 * @param[in] fileName - Certificate and key full file path.
103 * @return error code from open ssl verify function.
104 */
105 int32_t verifyCert(const std::string& filePath);
106
107 /** @brief Load Certificate file into the X509 structre.
108 * @param[in] fileName - Certificate and key full file path.
109 * @return pointer to the X509 structure.
110 */
111 X509_Ptr loadCert(const std::string& filePath);
112
Jayanth Othayoth589159f2018-09-28 08:32:39 -0500113 /** @brief Public/Private key compare function.
114 * Comparing private key against certificate public key
115 * from input .pem file.
116 * @param[in] fileName - Certificate and key full file path.
117 * @return Return true if Key compare is successful,
118 * false if not
119 */
120 bool compareKeys(const std::string& filePath);
121
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500122 /** @brief sdbusplus handler */
123 sdbusplus::bus::bus& bus;
124
125 /** @brief object path */
126 std::string path;
127
128 /** @brief Type of the certificate **/
129 InputType type;
130
131 /** @brief Unit name associated to the service **/
132 std::string unit;
133
134 /** @brief Certificate file installation path **/
135 std::string certPath;
136
137 /** @brief Type specific function pointer map **/
138 std::unordered_map<InputType, InstallFunc> typeFuncMap;
139};
140
141} // namespace certs
142} // namespace phosphor