blob: 252864f604f35537feed39772c915ca1f3149f27 [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>
Jayanth Othayothfeddcf22018-11-07 01:14:23 -06005#include <phosphor-logging/elog-errors.hpp>
6#include <phosphor-logging/elog.hpp>
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -05007#include <sdbusplus/bus.hpp>
8#include <sdbusplus/server/object.hpp>
9#include <unordered_map>
Jayanth Othayothfeddcf22018-11-07 01:14:23 -060010#include <xyz/openbmc_project/Certs/Install/error.hpp>
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050011#include <xyz/openbmc_project/Certs/Install/server.hpp>
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050012#include <xyz/openbmc_project/Object/Delete/server.hpp>
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050013
14namespace phosphor
15{
16namespace certs
17{
Jayanth Othayothdd74bd22018-09-28 06:13:35 -050018// RAII support for openSSL functions.
19using X509_Ptr = std::unique_ptr<X509, decltype(&::X509_free)>;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050020
21// Supported Types.
22static constexpr auto SERVER = "server";
23static constexpr auto CLIENT = "client";
Jayanth Othayothb50789c2018-10-09 07:13:54 -050024static constexpr auto AUTHORITY = "authority";
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050025
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050026using Create = sdbusplus::xyz::openbmc_project::Certs::server::Install;
27using Delete = sdbusplus::xyz::openbmc_project::Object::server::Delete;
28using Ifaces = sdbusplus::server::object::object<Create, Delete>;
Jayanth Othayothb50789c2018-10-09 07:13:54 -050029using InstallFunc = std::function<void(const std::string&)>;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050030using InputType = std::string;
31
Jayanth Othayothfeddcf22018-11-07 01:14:23 -060032using namespace phosphor::logging;
33using InvalidCertificate =
34 sdbusplus::xyz::openbmc_project::Certs::Install::Error::InvalidCertificate;
35using Reason = xyz::openbmc_project::Certs::Install::InvalidCertificate::REASON;
36
Jayanth Othayothb50789c2018-10-09 07:13:54 -050037// for placeholders
38using namespace std::placeholders;
39
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050040class Manager : public Ifaces
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050041{
42 public:
43 /* Define all of the basic class operations:
44 * Not allowed:
45 * - Default constructor is not possible due to member
46 * reference
47 * - Move operations due to 'this' being registered as the
48 * 'context' with sdbus.
49 * Allowed:
50 * - copy
51 * - Destructor.
52 */
53 Manager() = delete;
54 Manager(const Manager&) = default;
55 Manager& operator=(const Manager&) = delete;
56 Manager(Manager&&) = delete;
57 Manager& operator=(Manager&&) = delete;
58 virtual ~Manager() = default;
59
60 /** @brief Constructor to put object onto bus at a dbus path.
61 * @param[in] bus - Bus to attach to.
62 * @param[in] path - Path to attach at.
63 * @param[in] type - Type of the certificate.
64 * @param[in] unit - Unit consumed by this certificate.
65 * @param[in] certpath - Certificate installation path.
66 */
67 Manager(sdbusplus::bus::bus& bus, const char* path, const std::string& type,
68 std::string&& unit, std::string&& certPath) :
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050069 Ifaces(bus, path),
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050070 bus(bus), path(path), type(type), unit(std::move(unit)),
71 certPath(std::move(certPath))
72 {
Jayanth Othayothfeddcf22018-11-07 01:14:23 -060073 auto installHelper = [this](const auto& filePath) {
74 if (!compareKeys(filePath))
75 {
76 elog<InvalidCertificate>(
77 Reason("Private key does not match the Certificate"));
78 };
79 };
80
81 typeFuncMap[SERVER] = installHelper;
82 typeFuncMap[CLIENT] = installHelper;
83 typeFuncMap[AUTHORITY] = [](auto filePath) {};
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050084 }
85
86 /** @brief Implementation for Install
87 * Replace the existing certificate key file with another
88 * (possibly CA signed) Certificate key file.
89 *
90 * @param[in] path - Certificate key file path.
91 */
92 void install(const std::string path) override;
93
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050094 /** @brief Delete the certificate (and possibly revert
95 * to a self-signed certificate).
96 */
97 void delete_() override;
98
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050099 private:
Jayanth Othayothe8199a82018-09-29 00:46:10 -0500100 /** @brief systemd unit reload or reset helper function
101 * Reload if the unit supports it and use a restart otherwise.
102 * @param[in] unit - service need to reload.
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500103 */
Marri Devender Rao9abfae82018-10-03 08:10:35 -0500104 virtual void reloadOrReset(const std::string& unit);
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500105
106 /** @brief helper function to copy the file.
107 * @param[in] src - Source file path to copy
108 * @param[in] dst - Destination path to copy
109 */
110 void copy(const std::string& src, const std::string& dst);
111
Jayanth Othayothdd74bd22018-09-28 06:13:35 -0500112 /** @brief Certificate verification function
113 * Certificate file specific validation using openssl
114 * verify function also includes expiry date check
115 * @param[in] fileName - Certificate and key full file path.
116 * @return error code from open ssl verify function.
117 */
118 int32_t verifyCert(const std::string& filePath);
119
120 /** @brief Load Certificate file into the X509 structre.
121 * @param[in] fileName - Certificate and key full file path.
122 * @return pointer to the X509 structure.
123 */
124 X509_Ptr loadCert(const std::string& filePath);
125
Jayanth Othayoth589159f2018-09-28 08:32:39 -0500126 /** @brief Public/Private key compare function.
127 * Comparing private key against certificate public key
128 * from input .pem file.
129 * @param[in] fileName - Certificate and key full file path.
130 * @return Return true if Key compare is successful,
131 * false if not
132 */
133 bool compareKeys(const std::string& filePath);
134
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500135 /** @brief sdbusplus handler */
136 sdbusplus::bus::bus& bus;
137
138 /** @brief object path */
139 std::string path;
140
141 /** @brief Type of the certificate **/
142 InputType type;
143
144 /** @brief Unit name associated to the service **/
145 std::string unit;
146
147 /** @brief Certificate file installation path **/
148 std::string certPath;
149
150 /** @brief Type specific function pointer map **/
151 std::unordered_map<InputType, InstallFunc> typeFuncMap;
152};
153
154} // namespace certs
155} // namespace phosphor