blob: 1ca4f1abbf6a0d1ff55378911d397ee9f10705fa [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";
Jayanth Othayothb50789c2018-10-09 07:13:54 -050021static constexpr auto AUTHORITY = "authority";
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050022
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050023using Create = sdbusplus::xyz::openbmc_project::Certs::server::Install;
24using Delete = sdbusplus::xyz::openbmc_project::Object::server::Delete;
25using Ifaces = sdbusplus::server::object::object<Create, Delete>;
Jayanth Othayothb50789c2018-10-09 07:13:54 -050026using InstallFunc = std::function<void(const std::string&)>;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050027using InputType = std::string;
28
Jayanth Othayothb50789c2018-10-09 07:13:54 -050029// for placeholders
30using namespace std::placeholders;
31
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050032class Manager : public Ifaces
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050033{
34 public:
35 /* Define all of the basic class operations:
36 * Not allowed:
37 * - Default constructor is not possible due to member
38 * reference
39 * - Move operations due to 'this' being registered as the
40 * 'context' with sdbus.
41 * Allowed:
42 * - copy
43 * - Destructor.
44 */
45 Manager() = delete;
46 Manager(const Manager&) = default;
47 Manager& operator=(const Manager&) = delete;
48 Manager(Manager&&) = delete;
49 Manager& operator=(Manager&&) = delete;
50 virtual ~Manager() = default;
51
52 /** @brief Constructor to put object onto bus at a dbus path.
53 * @param[in] bus - Bus to attach to.
54 * @param[in] path - Path to attach at.
55 * @param[in] type - Type of the certificate.
56 * @param[in] unit - Unit consumed by this certificate.
57 * @param[in] certpath - Certificate installation path.
58 */
59 Manager(sdbusplus::bus::bus& bus, const char* path, const std::string& type,
60 std::string&& unit, std::string&& certPath) :
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050061 Ifaces(bus, path),
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050062 bus(bus), path(path), type(type), unit(std::move(unit)),
63 certPath(std::move(certPath))
64 {
65 typeFuncMap[SERVER] =
Jayanth Othayothb50789c2018-10-09 07:13:54 -050066 std::bind(&phosphor::certs::Manager::serverInstallHelper, this, _1);
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050067 typeFuncMap[CLIENT] =
Jayanth Othayothb50789c2018-10-09 07:13:54 -050068 std::bind(&phosphor::certs::Manager::clientInstallHelper, this, _1);
69 typeFuncMap[AUTHORITY] = std::bind(
70 &phosphor::certs::Manager::authorityInstallHelper, this, _1);
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050071 }
72
73 /** @brief Implementation for Install
74 * Replace the existing certificate key file with another
75 * (possibly CA signed) Certificate key file.
76 *
77 * @param[in] path - Certificate key file path.
78 */
79 void install(const std::string path) override;
80
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050081 /** @brief Delete the certificate (and possibly revert
82 * to a self-signed certificate).
83 */
84 void delete_() override;
85
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050086 private:
Jayanth Othayothb50789c2018-10-09 07:13:54 -050087 /** @brief Client certificate Installation helper function
88 * @param[in] path - Certificate key file path.
89 */
90 virtual void clientInstallHelper(const std::string& filePath);
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050091
Jayanth Othayothb50789c2018-10-09 07:13:54 -050092 /** @brief Server certificate Installation helper function
93 * @param[in] path - Certificate key file path.
94 */
95 virtual void serverInstallHelper(const std::string& filePath);
96
97 /** @brief Authority certificate Installation helper function
98 * @param[in] path - Certificate key file path.
99 */
100 virtual void authorityInstallHelper(const std::string& filePath);
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500101
Jayanth Othayothe8199a82018-09-29 00:46:10 -0500102 /** @brief systemd unit reload or reset helper function
103 * Reload if the unit supports it and use a restart otherwise.
104 * @param[in] unit - service need to reload.
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500105 */
Marri Devender Rao9abfae82018-10-03 08:10:35 -0500106 virtual void reloadOrReset(const std::string& unit);
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500107
108 /** @brief helper function to copy the file.
109 * @param[in] src - Source file path to copy
110 * @param[in] dst - Destination path to copy
111 */
112 void copy(const std::string& src, const std::string& dst);
113
Jayanth Othayothdd74bd22018-09-28 06:13:35 -0500114 /** @brief Certificate verification function
115 * Certificate file specific validation using openssl
116 * verify function also includes expiry date check
117 * @param[in] fileName - Certificate and key full file path.
118 * @return error code from open ssl verify function.
119 */
120 int32_t verifyCert(const std::string& filePath);
121
122 /** @brief Load Certificate file into the X509 structre.
123 * @param[in] fileName - Certificate and key full file path.
124 * @return pointer to the X509 structure.
125 */
126 X509_Ptr loadCert(const std::string& filePath);
127
Jayanth Othayoth589159f2018-09-28 08:32:39 -0500128 /** @brief Public/Private key compare function.
129 * Comparing private key against certificate public key
130 * from input .pem file.
131 * @param[in] fileName - Certificate and key full file path.
132 * @return Return true if Key compare is successful,
133 * false if not
134 */
135 bool compareKeys(const std::string& filePath);
136
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500137 /** @brief sdbusplus handler */
138 sdbusplus::bus::bus& bus;
139
140 /** @brief object path */
141 std::string path;
142
143 /** @brief Type of the certificate **/
144 InputType type;
145
146 /** @brief Unit name associated to the service **/
147 std::string unit;
148
149 /** @brief Certificate file installation path **/
150 std::string certPath;
151
152 /** @brief Type specific function pointer map **/
153 std::unordered_map<InputType, InstallFunc> typeFuncMap;
154};
155
156} // namespace certs
157} // namespace phosphor