blob: 4e90ea174fee08b1f6c0adcd86c788f2a91c2164 [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
Nan Zhou6ec13c82021-12-30 11:34:50 -0800104 /** @brief Constructor for the Certificate Object; a variant for authorities
105 * list install
106 * @param[in] bus - Bus to attach to.
107 * @param[in] objPath - Object path to attach to
108 * @param[in] type - Type of the certificate
109 * @param[in] installPath - Path of the certificate to install
110 * @param[in] x509Store - an initialized X509 store used for certificate
111 * validation; Certificate object doesn't own it
112 * @param[in] pem - Content of the certificate file to upload; it shall be
113 * a single PEM encoded x509 certificate
114 * @param[in] watchPtr - watch on self signed certificate
115 * @param[in] parent - Pointer to the manager which owns the constructed
116 * Certificate object
117 */
118 Certificate(sdbusplus::bus::bus& bus, const std::string& objPath,
119 const CertificateType& type, const std::string& installPath,
120 X509_STORE& x509Store, const std::string& pem, Watch* watchPtr,
121 Manager& parent);
122
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100123 /** @brief Validate and Replace/Install the certificate file
124 * Install/Replace the existing certificate file with another
125 * (possibly CA signed) Certificate file.
126 * @param[in] filePath - Certificate file path.
127 */
128 void install(const std::string& filePath);
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600129
Nan Zhou6ec13c82021-12-30 11:34:50 -0800130 /** @brief Validate and Replace/Install the certificate file
131 * Install/Replace the existing certificate file with another
132 * (possibly CA signed) Certificate file.
133 * @param[in] x509Store - an initialized X509 store used for certificate
134 * validation; Certificate object doesn't own it
135 * @param[in] pem - a string buffer which stores a PEM encoded certificate.
136 */
137 void install(X509_STORE& x509Store, const std::string& pem);
138
Marri Devender Rao13bf74e2019-03-26 01:52:17 -0500139 /** @brief Validate certificate and replace the existing certificate
140 * @param[in] filePath - Certificate file path.
141 */
142 void replace(const std::string filePath) override;
143
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500144 /** @brief Populate certificate properties by parsing certificate file
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500145 */
146 void populateProperties();
147
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200148 /**
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100149 * @brief Obtain certificate ID.
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200150 *
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100151 * @return Certificate ID.
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200152 */
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100153 std::string getCertId() const;
154
155 /**
Nan Zhoubf3cf752021-12-28 11:02:07 -0800156 * @brief Check if provided certificate is the same as the current one.
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100157 *
158 * @param[in] certPath - File path for certificate to check.
159 *
160 * @return Checking result. Return true if certificates are the same,
161 * false if not.
162 */
163 bool isSame(const std::string& certPath);
164
165 /**
166 * @brief Update certificate storage.
167 */
168 void storageUpdate();
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200169
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +0200170 /**
171 * @brief Delete the certificate
172 */
173 void delete_() override;
174
Nan Zhou6ec13c82021-12-30 11:34:50 -0800175 /**
176 * @brief Generate file name which is unique in the provided directory.
177 *
178 * @param[in] directoryPath - Directory path.
179 *
180 * @return File path.
181 */
182 static std::string generateUniqueFilePath(const std::string& directoryPath);
183
184 /**
185 * @brief Copies the certificate from sourceFilePath to installFilePath
186 *
187 * @param[in] sourceFilePath - Path to the source file.
188 * @param[in] certFilePath - Path to the destination file.
189 *
190 * @return void
191 */
192 static void copyCertificate(const std::string& certSrcFilePath,
193 const std::string& certFilePath);
194
195 /**
196 * @brief Returns the associated dbus object path.
197 */
198 std::string getObjectPath();
199
200 /**
201 * @brief Returns the associated cert file path.
202 */
203 std::string getCertFilePath();
204
205 /** @brief: Set the data member |certFilePath| to |path|
206 */
207 void setCertFilePath(const std::string& path);
208
209 /** @brief: Set the data member |certInstallPath| to |path|
210 */
211 void setCertInstallPath(const std::string& path);
212
Marri Devender Rao13bf74e2019-03-26 01:52:17 -0500213 private:
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200214 /**
Nan Zhoue869bb62021-12-30 11:34:42 -0800215 * @brief Populate certificate properties by parsing given certificate
216 * object
Marri Devender Raoc4522d22020-03-12 06:50:17 -0500217 *
Nan Zhoue869bb62021-12-30 11:34:42 -0800218 * @param[in] cert The given certificate object
Marri Devender Raoc4522d22020-03-12 06:50:17 -0500219 *
220 * @return void
221 */
Nan Zhoue869bb62021-12-30 11:34:42 -0800222 void populateProperties(X509& cert);
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600223
Marri Devender Raocd30c492019-06-12 01:40:17 -0500224 /** @brief Check and append private key to the certificate file
225 * If private key is not present in the certificate file append the
226 * certificate file with private key existing in the system.
227 * @param[in] filePath - Certificate and key full file path.
228 * @return void.
229 */
230 void checkAndAppendPrivateKey(const std::string& filePath);
231
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600232 /** @brief Public/Private key compare function.
233 * Comparing private key against certificate public key
234 * from input .pem file.
Marri Devender Raocd30c492019-06-12 01:40:17 -0500235 * @param[in] filePath - Certificate and key full file path.
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600236 * @return Return true if Key compare is successful,
237 * false if not
238 */
239 bool compareKeys(const std::string& filePath);
Marri Devender Raocd30c492019-06-12 01:40:17 -0500240
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100241 /**
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100242 * @brief Generate authority certificate file path corresponding with
243 * OpenSSL requirements.
244 *
Nan Zhoubf3cf752021-12-28 11:02:07 -0800245 * Prepare authority certificate file path for provided certificate.
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100246 * OpenSSL puts some restrictions on the certificate file name pattern.
247 * Certificate full file name needs to consists of basic file name which
248 * is certificate subject name hash and file name extension which is an
249 * integer. More over, certificates files names extensions must be
250 * consecutive integer numbers in case many certificates with the same
251 * subject name.
252 * https://www.boost.org/doc/libs/1_69_0/doc/html/boost_asio/reference/ssl__context/add_verify_path.html
253 * https://www.openssl.org/docs/man1.0.2/man3/SSL_CTX_load_verify_locations.html
254 *
255 * @param[in] certSrcFilePath - Certificate source file path.
256 * @param[in] certDstDirPath - Certificate destination directory path.
257 *
258 * @return Authority certificate file path.
259 */
260 std::string generateAuthCertFileX509Path(const std::string& certSrcFilePath,
261 const std::string& certDstDirPath);
262
263 /**
264 * @brief Generate authority certificate file path based on provided
265 * certificate source file path.
266 *
267 * @param[in] certSrcFilePath - Certificate source file path.
268 *
269 * @return Authority certificate file path.
270 */
271 std::string generateAuthCertFilePath(const std::string& certSrcFilePath);
272
273 /**
274 * @brief Generate certificate file path based on provided certificate
275 * source file path.
276 *
277 * @param[in] certSrcFilePath - Certificate source file path.
278 *
279 * @return Certificate file path.
280 */
281 std::string generateCertFilePath(const std::string& certSrcFilePath);
282
283 /** @brief Type specific function pointer map */
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800284 std::unordered_map<CertificateType, internal::InstallFunc> typeFuncMap;
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600285
286 /** @brief object path */
287 std::string objectPath;
288
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100289 /** @brief Type of the certificate */
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600290 CertificateType certType;
291
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100292 /** @brief Stores certificate ID */
293 std::string certId;
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600294
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100295 /** @brief Stores certificate file path */
296 std::string certFilePath;
297
298 /** @brief Certificate file installation path */
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800299 std::string certInstallPath;
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500300
Marri Devender Raocd30c492019-06-12 01:40:17 -0500301 /** @brief Type specific function pointer map for appending private key */
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800302 std::unordered_map<CertificateType, internal::AppendPrivKeyFunc>
303 appendKeyMap;
Marri Devender Raocd30c492019-06-12 01:40:17 -0500304
Nan Zhoucf06ccd2021-12-28 16:25:45 -0800305 /** @brief Certificate file create/update watch
306 * Note that Certificate object doesn't own the pointer
307 */
308 Watch* certWatch;
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200309
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +0200310 /** @brief Reference to Certificate Manager */
311 Manager& manager;
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600312};
313
Nan Zhoue1289ad2021-12-28 11:02:56 -0800314} // namespace phosphor::certs