blob: feffecccfbc01a1a7785996572d0586218aa6812 [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
Marri Devender Rao6ceec402019-02-01 03:15:19 -06005#include <openssl/x509.h>
6
7#include <filesystem>
8#include <phosphor-logging/elog.hpp>
Marri Devender Raoedd11312019-02-27 08:45:10 -06009#include <xyz/openbmc_project/Certs/Certificate/server.hpp>
Marri Devender Rao13bf74e2019-03-26 01:52:17 -050010#include <xyz/openbmc_project/Certs/Replace/server.hpp>
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +020011#include <xyz/openbmc_project/Object/Delete/server.hpp>
Marri Devender Rao6ceec402019-02-01 03:15:19 -060012
13namespace phosphor
14{
15namespace certs
16{
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +020017using DeleteIface = sdbusplus::xyz::openbmc_project::Object::server::Delete;
Marri Devender Raoedd11312019-02-27 08:45:10 -060018using CertificateIface = sdbusplus::server::object::object<
19 sdbusplus::xyz::openbmc_project::Certs::server::Certificate>;
Marri Devender Rao13bf74e2019-03-26 01:52:17 -050020using ReplaceIface = sdbusplus::xyz::openbmc_project::Certs::server::Replace;
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +020021using CertIfaces = sdbusplus::server::object::object<CertificateIface,
22 ReplaceIface, DeleteIface>;
Marri Devender Raoedd11312019-02-27 08:45:10 -060023
Marri Devender Rao6ceec402019-02-01 03:15:19 -060024using CertificateType = std::string;
Marri Devender Rao6ceec402019-02-01 03:15:19 -060025using CertInstallPath = std::string;
26using CertUploadPath = std::string;
27using InputType = std::string;
28using InstallFunc = std::function<void(const std::string&)>;
Marri Devender Raocd30c492019-06-12 01:40:17 -050029using AppendPrivKeyFunc = std::function<void(const std::string&)>;
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050030using CertWatchPtr = std::unique_ptr<Watch>;
Marri Devender Rao6ceec402019-02-01 03:15:19 -060031using namespace phosphor::logging;
32
33// for placeholders
34using namespace std::placeholders;
35namespace fs = std::filesystem;
36
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +020037class Manager; // Forward declaration for Certificate Manager.
38
Marri Devender Rao6ceec402019-02-01 03:15:19 -060039// Supported Types.
40static constexpr auto SERVER = "server";
41static constexpr auto CLIENT = "client";
42static constexpr auto AUTHORITY = "authority";
43
44// RAII support for openSSL functions.
45using X509_Ptr = std::unique_ptr<X509, decltype(&::X509_free)>;
Kowalski, Kamildb029c92019-07-08 17:09:39 +020046using X509_STORE_CTX_Ptr =
47 std::unique_ptr<X509_STORE_CTX, decltype(&::X509_STORE_CTX_free)>;
Marri Devender Rao6ceec402019-02-01 03:15:19 -060048
49/** @class Certificate
50 * @brief OpenBMC Certificate entry implementation.
51 * @details A concrete implementation for the
52 * xyz.openbmc_project.Certs.Certificate DBus API
53 * xyz.openbmc_project.Certs.Instal DBus API
54 */
Marri Devender Raoedd11312019-02-27 08:45:10 -060055class Certificate : public CertIfaces
Marri Devender Rao6ceec402019-02-01 03:15:19 -060056{
57 public:
58 Certificate() = delete;
59 Certificate(const Certificate&) = delete;
60 Certificate& operator=(const Certificate&) = delete;
61 Certificate(Certificate&&) = delete;
62 Certificate& operator=(Certificate&&) = delete;
63 virtual ~Certificate();
64
65 /** @brief Constructor for the Certificate Object
66 * @param[in] bus - Bus to attach to.
67 * @param[in] objPath - Object path to attach to
68 * @param[in] type - Type of the certificate
Marri Devender Rao6ceec402019-02-01 03:15:19 -060069 * @param[in] installPath - Path of the certificate to install
70 * @param[in] uploadPath - Path of the certificate file to upload
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050071 * @param[in] watchPtr - watch on self signed certificate pointer
Marri Devender Rao6ceec402019-02-01 03:15:19 -060072 */
73 Certificate(sdbusplus::bus::bus& bus, const std::string& objPath,
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +010074 const CertificateType& type, const CertInstallPath& installPath,
75 const CertUploadPath& uploadPath, const CertWatchPtr& watchPtr,
76 Manager& parent);
77
78 /** @brief Validate and Replace/Install the certificate file
79 * Install/Replace the existing certificate file with another
80 * (possibly CA signed) Certificate file.
81 * @param[in] filePath - Certificate file path.
82 */
83 void install(const std::string& filePath);
Marri Devender Rao6ceec402019-02-01 03:15:19 -060084
Marri Devender Rao13bf74e2019-03-26 01:52:17 -050085 /** @brief Validate certificate and replace the existing certificate
86 * @param[in] filePath - Certificate file path.
87 */
88 void replace(const std::string filePath) override;
89
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050090 /** @brief Populate certificate properties by parsing certificate file
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050091 */
92 void populateProperties();
93
Kowalski, Kamildb029c92019-07-08 17:09:39 +020094 /**
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +010095 * @brief Obtain certificate ID.
Kowalski, Kamildb029c92019-07-08 17:09:39 +020096 *
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +010097 * @return Certificate ID.
Kowalski, Kamildb029c92019-07-08 17:09:39 +020098 */
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +010099 std::string getCertId() const;
100
101 /**
102 * @brief Check if provied certificate is the same as the current one.
103 *
104 * @param[in] certPath - File path for certificate to check.
105 *
106 * @return Checking result. Return true if certificates are the same,
107 * false if not.
108 */
109 bool isSame(const std::string& certPath);
110
111 /**
112 * @brief Update certificate storage.
113 */
114 void storageUpdate();
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200115
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +0200116 /**
117 * @brief Delete the certificate
118 */
119 void delete_() override;
120
Marri Devender Rao13bf74e2019-03-26 01:52:17 -0500121 private:
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200122 /**
123 * @brief Populate certificate properties by parsing given certificate file
124 *
125 * @param[in] certPath Path to certificate that should be parsed
126 *
127 * @return void
128 */
129 void populateProperties(const std::string& certPath);
130
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100131 /** @brief Load Certificate file into the X509 structure.
Marri Devender Raocd30c492019-06-12 01:40:17 -0500132 * @param[in] filePath - Certificate and key full file path.
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600133 * @return pointer to the X509 structure.
134 */
135 X509_Ptr loadCert(const std::string& filePath);
136
Marri Devender Raocd30c492019-06-12 01:40:17 -0500137 /** @brief Check and append private key to the certificate file
138 * If private key is not present in the certificate file append the
139 * certificate file with private key existing in the system.
140 * @param[in] filePath - Certificate and key full file path.
141 * @return void.
142 */
143 void checkAndAppendPrivateKey(const std::string& filePath);
144
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600145 /** @brief Public/Private key compare function.
146 * Comparing private key against certificate public key
147 * from input .pem file.
Marri Devender Raocd30c492019-06-12 01:40:17 -0500148 * @param[in] filePath - Certificate and key full file path.
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600149 * @return Return true if Key compare is successful,
150 * false if not
151 */
152 bool compareKeys(const std::string& filePath);
Marri Devender Raocd30c492019-06-12 01:40:17 -0500153
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100154 /**
155 * @brief Generate certificate ID based on provided certificate file.
156 *
157 * @param[in] certPath - Certificate file path.
158 *
159 * @return Certificate ID as formatted string.
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600160 */
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100161 std::string generateCertId(const std::string& certPath);
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600162
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200163 /**
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100164 * @brief Generate file name which is unique in the provided directory.
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200165 *
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100166 * @param[in] directoryPath - Directory path.
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200167 *
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100168 * @return File path.
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200169 */
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100170 std::string generateUniqueFilePath(const std::string& directoryPath);
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200171
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100172 /**
173 * @brief Generate authority certificate file path corresponding with
174 * OpenSSL requirements.
175 *
176 * Prepare authority certificate file path for provied certificate.
177 * OpenSSL puts some restrictions on the certificate file name pattern.
178 * Certificate full file name needs to consists of basic file name which
179 * is certificate subject name hash and file name extension which is an
180 * integer. More over, certificates files names extensions must be
181 * consecutive integer numbers in case many certificates with the same
182 * subject name.
183 * https://www.boost.org/doc/libs/1_69_0/doc/html/boost_asio/reference/ssl__context/add_verify_path.html
184 * https://www.openssl.org/docs/man1.0.2/man3/SSL_CTX_load_verify_locations.html
185 *
186 * @param[in] certSrcFilePath - Certificate source file path.
187 * @param[in] certDstDirPath - Certificate destination directory path.
188 *
189 * @return Authority certificate file path.
190 */
191 std::string generateAuthCertFileX509Path(const std::string& certSrcFilePath,
192 const std::string& certDstDirPath);
193
194 /**
195 * @brief Generate authority certificate file path based on provided
196 * certificate source file path.
197 *
198 * @param[in] certSrcFilePath - Certificate source file path.
199 *
200 * @return Authority certificate file path.
201 */
202 std::string generateAuthCertFilePath(const std::string& certSrcFilePath);
203
204 /**
205 * @brief Generate certificate file path based on provided certificate
206 * source file path.
207 *
208 * @param[in] certSrcFilePath - Certificate source file path.
209 *
210 * @return Certificate file path.
211 */
212 std::string generateCertFilePath(const std::string& certSrcFilePath);
213
214 /** @brief Type specific function pointer map */
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600215 std::unordered_map<InputType, InstallFunc> typeFuncMap;
216
217 /** @brief sdbusplus handler */
218 sdbusplus::bus::bus& bus;
219
220 /** @brief object path */
221 std::string objectPath;
222
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100223 /** @brief Type of the certificate */
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600224 CertificateType certType;
225
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100226 /** @brief Stores certificate ID */
227 std::string certId;
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600228
Zbigniew Lukwinski2f3563c2020-01-08 12:35:23 +0100229 /** @brief Stores certificate file path */
230 std::string certFilePath;
231
232 /** @brief Certificate file installation path */
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600233 CertInstallPath certInstallPath;
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500234
Marri Devender Raocd30c492019-06-12 01:40:17 -0500235 /** @brief Type specific function pointer map for appending private key */
236 std::unordered_map<InputType, AppendPrivKeyFunc> appendKeyMap;
237
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500238 /** @brief Certificate file create/update watch */
239 const CertWatchPtr& certWatchPtr;
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200240
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +0200241 /** @brief Reference to Certificate Manager */
242 Manager& manager;
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600243};
244
245} // namespace certs
246} // namespace phosphor