blob: ed28348fb10c689d7201c9229a776f5f2ac096b9 [file] [log] [blame]
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -05001#pragma once
Marri Devender Raof4682712019-03-19 05:00:28 -05002#include "config.h"
Jayanth Othayothdd74bd22018-09-28 06:13:35 -05003
Marri Devender Raof4682712019-03-19 05:00:28 -05004#include "certificate.hpp"
5#include "csr.hpp"
Marri Devender Raoffad1ef2019-06-03 04:54:12 -05006#include "watch.hpp"
Marri Devender Raof4682712019-03-19 05:00:28 -05007
8#include <sdeventplus/source/child.hpp>
9#include <sdeventplus/source/event.hpp>
10#include <xyz/openbmc_project/Certs/CSR/Create/server.hpp>
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050011#include <xyz/openbmc_project/Certs/Install/server.hpp>
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +020012#include <xyz/openbmc_project/Collection/DeleteAll/server.hpp>
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050013
14namespace phosphor
15{
16namespace certs
17{
Marri Devender Raof4682712019-03-19 05:00:28 -050018using Install = sdbusplus::xyz::openbmc_project::Certs::server::Install;
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +020019using DeleteAll =
20 sdbusplus::xyz::openbmc_project::Collection::server::DeleteAll;
Marri Devender Raof4682712019-03-19 05:00:28 -050021using CSRCreate = sdbusplus::xyz::openbmc_project::Certs::CSR::server::Create;
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +020022using Ifaces = sdbusplus::server::object::object<Install, CSRCreate, DeleteAll>;
Marri Devender Raof4682712019-03-19 05:00:28 -050023
24using X509_REQ_Ptr = std::unique_ptr<X509_REQ, decltype(&::X509_REQ_free)>;
25using EVP_PKEY_Ptr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>;
Marri Devender Raoffad1ef2019-06-03 04:54:12 -050026using CertificatePtr = std::unique_ptr<Certificate>;
Jayanth Othayothb50789c2018-10-09 07:13:54 -050027
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050028class Manager : public Ifaces
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050029{
30 public:
31 /* Define all of the basic class operations:
32 * Not allowed:
33 * - Default constructor is not possible due to member
34 * reference
35 * - Move operations due to 'this' being registered as the
36 * 'context' with sdbus.
37 * Allowed:
38 * - copy
39 * - Destructor.
40 */
41 Manager() = delete;
42 Manager(const Manager&) = default;
43 Manager& operator=(const Manager&) = delete;
44 Manager(Manager&&) = delete;
45 Manager& operator=(Manager&&) = delete;
46 virtual ~Manager() = default;
47
48 /** @brief Constructor to put object onto bus at a dbus path.
49 * @param[in] bus - Bus to attach to.
Marri Devender Raof4682712019-03-19 05:00:28 -050050 * @param[in] event - sd event handler.
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050051 * @param[in] path - Path to attach at.
52 * @param[in] type - Type of the certificate.
53 * @param[in] unit - Unit consumed by this certificate.
Marri Devender Rao6ceec402019-02-01 03:15:19 -060054 * @param[in] installPath - Certificate installation path.
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050055 */
Marri Devender Raof4682712019-03-19 05:00:28 -050056 Manager(sdbusplus::bus::bus& bus, sdeventplus::Event& event,
57 const char* path, const CertificateType& type,
58 UnitsToRestart&& unit, CertInstallPath&& installPath);
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050059
60 /** @brief Implementation for Install
61 * Replace the existing certificate key file with another
62 * (possibly CA signed) Certificate key file.
63 *
Marri Devender Rao6ceec402019-02-01 03:15:19 -060064 * @param[in] filePath - Certificate key file path.
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050065 */
Zbigniew Kurzynski06a69d72019-09-27 10:57:38 +020066 std::string install(const std::string filePath) override;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050067
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +020068 /** @brief Implementation for DeleteAll
69 * Delete all objects in the collection.
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050070 */
Zbigniew Kurzynskia3bb38f2019-09-17 13:34:25 +020071 void deleteAll() override;
72
73 /** @brief Delete the certificate with given hash.
74 */
75 void deleteCertificate(const std::string& certHash);
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050076
Marri Devender Raof4682712019-03-19 05:00:28 -050077 /** @brief Generate Private key and CSR file
78 * Generates the Private key file and CSR file based on the input
79 * parameters. Validation of the parameters is callers responsibility.
80 * At present supports only RSA algorithm type
81 *
82 * @param[in] alternativeNames - Additional hostnames of the component that
83 * is being secured.
84 * @param[in] challengePassword - The challenge password to be applied to
85 * the certificate for revocation requests.
86 * @param[in] city - The city or locality of the organization making the
87 * request. For Example Austin
88 * @param[in] commonName - The fully qualified domain name of the component
89 * that is being secured.
90 * @param[in] contactPerson - The name of the user making the request.
91 * @param[in] country - The country of the organization making the request.
92 * @param[in] email - The email address of the contact within the
93 * organization making the request.
94 * @param[in] givenName - The given name of the user making the request.
95 * @param[in] initials - The initials of the user making the request.
96 * @param[in] keyBitLength - The length of the key in bits, if needed based
97 * on the value of the KeyPairAlgorithm parameter.
98 * @param[in] keyCurveId - The curve ID to be used with the key, if needed
99 * based on the value of the KeyPairAlgorithm parameter.
100 * @param[in] keyPairAlgorithm - The type of key pair for use with signing
101 * algorithms. Valid built-in algorithm names for private key
102 * generation are: RSA, DSA, DH and EC.
103 * @param[in] keyUsage - Key usage extensions define the purpose of the
104 * public key contained in a certificate. Valid Key usage extensions
105 * and its usage description.
106 * - ClientAuthentication: The public key is used for TLS WWW client
107 * authentication.
108 * - CodeSigning: The public key is used for the signing of executable
109 * code
110 * - CRLSigning: The public key is used for verifying signatures on
111 * certificate revocation lists (CLRs).
112 * - DataEncipherment: The public key is used for directly enciphering
113 * raw user data without the use of an intermediate symmetric
114 * cipher.
115 * - DecipherOnly: The public key could be used for deciphering data
116 * while performing key agreement.
117 * - DigitalSignature: The public key is used for verifying digital
118 * signatures, other than signatures on certificatesand CRLs.
119 * - EmailProtection: The public key is used for email protection.
120 * - EncipherOnly: Thepublic key could be used for enciphering data
121 * while performing key agreement.
122 * - KeyCertSign: The public key is used for verifying signatures on
123 * public key certificates.
124 * - KeyEncipherment: The public key is used for enciphering private or
125 * secret keys.
126 * - NonRepudiation: The public key is used to verify digital
127 * signatures, other than signatures on certificates and CRLs, and
128 * used to provide a non-repudiation service that protects against
129 * the signing entity falsely denying some action.
130 * - OCSPSigning: The public key is used for signing OCSP responses.
131 * - ServerAuthentication: The public key is used for TLS WWW server
132 * authentication.
133 * - Timestamping: The public key is used for binding the hash of an
134 * object to a time.
135 * @param[in] organization - The legal name of the organization. This
136 * should not be abbreviated and should include suffixes such as Inc,
137 * Corp, or LLC.For example, IBM Corp.
138 * @param[in] organizationalUnit - The name of the unit or division of the
139 * organization making the request.
140 * @param[in] state - The state or province where the organization is
141 * located. This should not be abbreviated. For example, Texas.
142 * @param[in] surname - The surname of the user making the request.
143 * @param[in] unstructuredName - The unstructured name of the subject.
144 *
145 * @return path[std::string] - The object path of the D-Bus object
146 * representing CSR string. Note: For new CSR request will overwrite
147 * the existing CSR in the system.
148 */
149 std::string generateCSR(
150 std::vector<std::string> alternativeNames,
151 std::string challengePassword, std::string city, std::string commonName,
152 std::string contactPerson, std::string country, std::string email,
153 std::string givenName, std::string initials, int64_t keyBitLength,
154 std::string keyCurveId, std::string keyPairAlgorithm,
155 std::vector<std::string> keyUsage, std::string organization,
156 std::string organizationalUnit, std::string state, std::string surname,
157 std::string unstructuredName) override;
158
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200159 /** @brief Get reference to certificates' collection
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500160 *
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200161 * @return Reference to certificates' collection
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500162 */
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200163 std::vector<std::unique_ptr<Certificate>>& getCertificates();
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500164
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500165 private:
Marri Devender Raof4682712019-03-19 05:00:28 -0500166 void generateCSRHelper(std::vector<std::string> alternativeNames,
167 std::string challengePassword, std::string city,
168 std::string commonName, std::string contactPerson,
169 std::string country, std::string email,
170 std::string givenName, std::string initials,
171 int64_t keyBitLength, std::string keyCurveId,
172 std::string keyPairAlgorithm,
173 std::vector<std::string> keyUsage,
174 std::string organization,
175 std::string organizationalUnit, std::string state,
176 std::string surname, std::string unstructuredName);
177
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500178 /** @brief Generate RSA Key pair and get private key from key pair
179 * @param[in] keyBitLength - KeyBit length.
180 * @return Pointer to RSA private key
181 */
182 EVP_PKEY_Ptr generateRSAKeyPair(const int64_t keyBitLength);
183
184 /** @brief Generate EC Key pair and get private key from key pair
185 * @param[in] p_KeyCurveId - Curve ID
186 * @return Pointer to EC private key
187 */
188 EVP_PKEY_Ptr generateECKeyPair(const std::string& p_KeyCurveId);
189
Marri Devender Raof4682712019-03-19 05:00:28 -0500190 /** @brief Write private key data to file
191 *
Ramesh Iyyar8a09b522019-06-07 05:23:29 -0500192 * @param[in] pKey - pointer to private key
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -0500193 * @param[in] privKeyFileName - private key filename
Marri Devender Raof4682712019-03-19 05:00:28 -0500194 */
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -0500195 void writePrivateKey(const EVP_PKEY_Ptr& pKey,
196 const std::string& privKeyFileName);
Marri Devender Raof4682712019-03-19 05:00:28 -0500197
198 /** @brief Add the specified CSR field with the data
199 * @param[in] x509Name - Structure used in setting certificate properties
200 * @param[in] field - field name
201 * @param[in] bytes - field value in bytes
202 */
203 void addEntry(X509_NAME* x509Name, const char* field,
204 const std::string& bytes);
205
Marri Devender Rao76411052019-08-07 01:25:07 -0500206 /** @brief Check if usage is extended key usage
207 * @param[in] usage - key usage value
208 * @return true if part of extended key usage
209 */
210 bool isExtendedKeyUsage(const std::string& usage);
211
Marri Devender Raof4682712019-03-19 05:00:28 -0500212 /** @brief Create CSR D-Bus object by reading the data in the CSR file
213 * @param[in] statis - SUCCESSS/FAILURE In CSR generation.
214 */
215 void createCSRObject(const Status& status);
216
217 /** @brief Write generated CSR data to file
218 *
219 * @param[in] filePath - CSR file path.
220 * @param[in] x509Req - OpenSSL Request Pointer.
221 */
222 void writeCSR(const std::string& filePath, const X509_REQ_Ptr& x509Req);
223
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500224 /** @brief Load certifiate
225 * Load certificate and create certificate object
226 */
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200227 void createCertificates();
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500228
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -0500229 /** @brief Create RSA private key file
230 * Create RSA private key file by generating rsa key if not created
231 */
232 void createRSAPrivateKeyFile();
233
234 /** @brief Getting RSA private key
235 * Gettting RSA private key from generated file
236 * @param[in] keyBitLength - Key bit length
237 * @return Pointer to RSA key
238 */
239 EVP_PKEY_Ptr getRSAKeyPair(const int64_t keyBitLength);
240
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500241 /** @brief sdbusplus handler */
242 sdbusplus::bus::bus& bus;
243
Marri Devender Raof4682712019-03-19 05:00:28 -0500244 // sdevent Event handle
245 sdeventplus::Event& event;
246
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500247 /** @brief object path */
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600248 std::string objectPath;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500249
250 /** @brief Type of the certificate **/
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600251 CertificateType certType;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500252
253 /** @brief Unit name associated to the service **/
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600254 UnitsToRestart unitToRestart;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500255
256 /** @brief Certificate file installation path **/
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600257 CertInstallPath certInstallPath;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500258
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200259 /** @brief Collection of pointers to certificate */
260 std::vector<std::unique_ptr<Certificate>> installedCerts;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500261
Marri Devender Raof4682712019-03-19 05:00:28 -0500262 /** @brief pointer to CSR */
263 std::unique_ptr<CSR> csrPtr = nullptr;
264
265 /** @brief SDEventPlus child pointer added to event loop */
Marri Devender Raoffad1ef2019-06-03 04:54:12 -0500266 std::unique_ptr<sdeventplus::source::Child> childPtr = nullptr;
267
268 /** @brief Watch on self signed certificates */
269 std::unique_ptr<Watch> certWatchPtr = nullptr;
Ramesh Iyyarc6e58c72019-07-16 08:52:47 -0500270
271 /** @brif Parent path i.e certificate directory path */
272 fs::path certParentInstallPath;
Kowalski, Kamildb029c92019-07-08 17:09:39 +0200273
274 /** @brief Certificate ID pool */
275 uint64_t certIdCounter = 1;
Marri Devender Raof4682712019-03-19 05:00:28 -0500276};
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500277} // namespace certs
278} // namespace phosphor