blob: fa3191a357ca1eb9282bc8728d7052e728988b31 [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
Nan Zhou014be0b2021-12-28 18:00:14 -08005#include <openssl/ossl_typ.h>
Marri Devender Rao6ceec402019-02-01 03:15:19 -06006#include <openssl/x509.h>
7
Nan Zhoucf06ccd2021-12-28 16:25:45 -08008#include <functional>
9#include <memory>
Nan Zhou014be0b2021-12-28 18:00:14 -080010#include <sdbusplus/server/object.hpp>
11#include <string>
12#include <string_view>
13#include <unordered_map>
Marri Devender Raoedd11312019-02-27 08:45:10 -060014#include <xyz/openbmc_project/Certs/Certificate/server.hpp>
Marri Devender Rao13bf74e2019-03-26 01:52:17 -050015#include <xyz/openbmc_project/Certs/Replace/server.hpp>
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +020016#include <xyz/openbmc_project/Object/Delete/server.hpp>
Marri Devender Rao6ceec402019-02-01 03:15:19 -060017
Nan Zhoue1289ad2021-12-28 11:02:56 -080018namespace phosphor::certs
Marri Devender Rao6ceec402019-02-01 03:15:19 -060019{
Marri Devender Raoedd11312019-02-27 08:45:10 -060020
Nan Zhoucf06ccd2021-12-28 16:25:45 -080021// Certificate types
22enum class CertificateType
23{
24 Authority,
25 Server,
26 Client,
27 Unsupported,
28};
29
30inline constexpr const char* certificateTypeToString(CertificateType type)
31{
32 switch (type)
33 {
34 case CertificateType::Authority:
35 return "authority";
36 case CertificateType::Server:
37 return "server";
38 case CertificateType::Client:
39 return "client";
40 default:
41 return "unsupported";
42 }
43}
44
45inline constexpr CertificateType stringToCertificateType(std::string_view type)
46{
47 if (type == "authority")
48 {
49 return CertificateType::Authority;
50 }
51 if (type == "server")
52 {
53 return CertificateType::Server;
54 }
55 if (type == "client")
56 {
57 return CertificateType::Client;
58 }
59 return CertificateType::Unsupported;
60}
61
62namespace internal
63{
64using CertificateInterface = sdbusplus::server::object_t<
65 sdbusplus::xyz::openbmc_project::Certs::server::Certificate,
66 sdbusplus::xyz::openbmc_project::Certs::server::Replace,
67 sdbusplus::xyz::openbmc_project::Object::server::Delete>;
Marri Devender Rao6ceec402019-02-01 03:15:19 -060068using InstallFunc = std::function<void(const std::string&)>;
Marri Devender Raocd30c492019-06-12 01:40:17 -050069using AppendPrivKeyFunc = std::function<void(const std::string&)>;
Nan Zhoucf06ccd2021-12-28 16:25:45 -080070using X509Ptr = std::unique_ptr<X509, decltype(&::X509_free)>;
71} // namespace internal
Marri Devender Rao6ceec402019-02-01 03:15:19 -060072
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +020073class Manager; // Forward declaration for Certificate Manager.
74
Marri Devender Rao6ceec402019-02-01 03:15:19 -060075/** @class Certificate
76 * @brief OpenBMC Certificate entry implementation.
77 * @details A concrete implementation for the
78 * xyz.openbmc_project.Certs.Certificate DBus API
Nan Zhoubf3cf752021-12-28 11:02:07 -080079 * xyz.openbmc_project.Certs.Install DBus API
Marri Devender Rao6ceec402019-02-01 03:15:19 -060080 */
Nan Zhoucf06ccd2021-12-28 16:25:45 -080081class Certificate : public internal::CertificateInterface
Marri Devender Rao6ceec402019-02-01 03:15:19 -060082{
83 public:
84 Certificate() = delete;
85 Certificate(const Certificate&) = delete;
86 Certificate& operator=(const Certificate&) = delete;
87 Certificate(Certificate&&) = delete;
88 Certificate& operator=(Certificate&&) = delete;
89 virtual ~Certificate();
90
91 /** @brief Constructor for the Certificate Object
92 * @param[in] bus - Bus to attach to.
93 * @param[in] objPath - Object path to attach to
94 * @param[in] type - Type of the certificate
Marri Devender Rao6ceec402019-02-01 03:15:19 -060095 * @param[in] installPath - Path of the certificate to install
96 * @param[in] uploadPath - Path of the certificate file to upload
Nan Zhoucf06ccd2021-12-28 16:25:45 -080097 * @param[in] watchPtr - watch on self signed certificate
98 * @param[in] parent - the manager that owns the certificate
Marri Devender Rao6ceec402019-02-01 03:15:19 -060099 */
100 Certificate(sdbusplus::bus::bus& bus, const std::string& objPath,
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800101 CertificateType type, const std::string& installPath,
102 const std::string& uploadPath, Watch* watch, Manager& parent);
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100103
104 /** @brief Validate and Replace/Install the certificate file
105 * Install/Replace the existing certificate file with another
106 * (possibly CA signed) Certificate file.
107 * @param[in] filePath - Certificate file path.
108 */
109 void install(const std::string& filePath);
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600110
Marri Devender Rao13bf74e2019-03-26 01:52:17 -0500111 /** @brief Validate certificate and replace the existing certificate
112 * @param[in] filePath - Certificate file path.
113 */
114 void replace(const std::string filePath) override;
115
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500116 /** @brief Populate certificate properties by parsing certificate file
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500117 */
118 void populateProperties();
119
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200120 /**
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100121 * @brief Obtain certificate ID.
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200122 *
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100123 * @return Certificate ID.
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200124 */
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100125 std::string getCertId() const;
126
127 /**
Nan Zhoubf3cf752021-12-28 11:02:07 -0800128 * @brief Check if provided certificate is the same as the current one.
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100129 *
130 * @param[in] certPath - File path for certificate to check.
131 *
132 * @return Checking result. Return true if certificates are the same,
133 * false if not.
134 */
135 bool isSame(const std::string& certPath);
136
137 /**
138 * @brief Update certificate storage.
139 */
140 void storageUpdate();
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200141
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +0200142 /**
143 * @brief Delete the certificate
144 */
145 void delete_() override;
146
Marri Devender Rao13bf74e2019-03-26 01:52:17 -0500147 private:
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200148 /**
Nan Zhoue869bb62021-12-30 11:34:42 -0800149 * @brief Populate certificate properties by parsing given certificate
150 * object
Marri Devender Raoc4522d22020-03-12 06:50:17 -0500151 *
Nan Zhoue869bb62021-12-30 11:34:42 -0800152 * @param[in] cert The given certificate object
Marri Devender Raoc4522d22020-03-12 06:50:17 -0500153 *
154 * @return void
155 */
Nan Zhoue869bb62021-12-30 11:34:42 -0800156 void populateProperties(X509& cert);
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600157
Marri Devender Raocd30c492019-06-12 01:40:17 -0500158 /** @brief Check and append private key to the certificate file
159 * If private key is not present in the certificate file append the
160 * certificate file with private key existing in the system.
161 * @param[in] filePath - Certificate and key full file path.
162 * @return void.
163 */
164 void checkAndAppendPrivateKey(const std::string& filePath);
165
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600166 /** @brief Public/Private key compare function.
167 * Comparing private key against certificate public key
168 * from input .pem file.
Marri Devender Raocd30c492019-06-12 01:40:17 -0500169 * @param[in] filePath - Certificate and key full file path.
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600170 * @return Return true if Key compare is successful,
171 * false if not
172 */
173 bool compareKeys(const std::string& filePath);
Marri Devender Raocd30c492019-06-12 01:40:17 -0500174
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100175 /**
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100176 * @brief Generate file name which is unique in the provided directory.
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200177 *
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100178 * @param[in] directoryPath - Directory path.
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200179 *
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100180 * @return File path.
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200181 */
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100182 std::string generateUniqueFilePath(const std::string& directoryPath);
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200183
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100184 /**
185 * @brief Generate authority certificate file path corresponding with
186 * OpenSSL requirements.
187 *
Nan Zhoubf3cf752021-12-28 11:02:07 -0800188 * Prepare authority certificate file path for provided certificate.
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100189 * OpenSSL puts some restrictions on the certificate file name pattern.
190 * Certificate full file name needs to consists of basic file name which
191 * is certificate subject name hash and file name extension which is an
192 * integer. More over, certificates files names extensions must be
193 * consecutive integer numbers in case many certificates with the same
194 * subject name.
195 * https://www.boost.org/doc/libs/1_69_0/doc/html/boost_asio/reference/ssl__context/add_verify_path.html
196 * https://www.openssl.org/docs/man1.0.2/man3/SSL_CTX_load_verify_locations.html
197 *
198 * @param[in] certSrcFilePath - Certificate source file path.
199 * @param[in] certDstDirPath - Certificate destination directory path.
200 *
201 * @return Authority certificate file path.
202 */
203 std::string generateAuthCertFileX509Path(const std::string& certSrcFilePath,
204 const std::string& certDstDirPath);
205
206 /**
207 * @brief Generate authority certificate file path based on provided
208 * certificate source file path.
209 *
210 * @param[in] certSrcFilePath - Certificate source file path.
211 *
212 * @return Authority certificate file path.
213 */
214 std::string generateAuthCertFilePath(const std::string& certSrcFilePath);
215
216 /**
217 * @brief Generate certificate file path based on provided certificate
218 * source file path.
219 *
220 * @param[in] certSrcFilePath - Certificate source file path.
221 *
222 * @return Certificate file path.
223 */
224 std::string generateCertFilePath(const std::string& certSrcFilePath);
225
226 /** @brief Type specific function pointer map */
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800227 std::unordered_map<CertificateType, internal::InstallFunc> typeFuncMap;
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600228
229 /** @brief object path */
230 std::string objectPath;
231
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100232 /** @brief Type of the certificate */
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600233 CertificateType certType;
234
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100235 /** @brief Stores certificate ID */
236 std::string certId;
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600237
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100238 /** @brief Stores certificate file path */
239 std::string certFilePath;
240
241 /** @brief Certificate file installation path */
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800242 std::string certInstallPath;
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500243
Marri Devender Raocd30c492019-06-12 01:40:17 -0500244 /** @brief Type specific function pointer map for appending private key */
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800245 std::unordered_map<CertificateType, internal::AppendPrivKeyFunc>
246 appendKeyMap;
Marri Devender Raocd30c492019-06-12 01:40:17 -0500247
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800248 /** @brief Certificate file create/update watch
249 * Note that Certificate object doesn't own the pointer
250 */
251 Watch* certWatch;
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200252
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +0200253 /** @brief Reference to Certificate Manager */
254 Manager& manager;
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600255};
256
Nan Zhoue1289ad2021-12-28 11:02:56 -0800257} // namespace phosphor::certs