blob: 0c8f4e624a4ad4a72f5de2fa7bb1557ec35d1751 [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"
6
7#include <sdeventplus/source/child.hpp>
8#include <sdeventplus/source/event.hpp>
9#include <xyz/openbmc_project/Certs/CSR/Create/server.hpp>
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050010#include <xyz/openbmc_project/Certs/Install/server.hpp>
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050011#include <xyz/openbmc_project/Object/Delete/server.hpp>
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050012
13namespace phosphor
14{
15namespace certs
16{
Marri Devender Raof4682712019-03-19 05:00:28 -050017using Install = sdbusplus::xyz::openbmc_project::Certs::server::Install;
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050018using Delete = sdbusplus::xyz::openbmc_project::Object::server::Delete;
Marri Devender Raof4682712019-03-19 05:00:28 -050019using CSRCreate = sdbusplus::xyz::openbmc_project::Certs::CSR::server::Create;
20using Ifaces = sdbusplus::server::object::object<Install, CSRCreate, Delete>;
21
22using X509_REQ_Ptr = std::unique_ptr<X509_REQ, decltype(&::X509_REQ_free)>;
23using EVP_PKEY_Ptr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>;
Jayanth Othayothb50789c2018-10-09 07:13:54 -050024
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050025class Manager : public Ifaces
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050026{
27 public:
28 /* Define all of the basic class operations:
29 * Not allowed:
30 * - Default constructor is not possible due to member
31 * reference
32 * - Move operations due to 'this' being registered as the
33 * 'context' with sdbus.
34 * Allowed:
35 * - copy
36 * - Destructor.
37 */
38 Manager() = delete;
39 Manager(const Manager&) = default;
40 Manager& operator=(const Manager&) = delete;
41 Manager(Manager&&) = delete;
42 Manager& operator=(Manager&&) = delete;
43 virtual ~Manager() = default;
44
45 /** @brief Constructor to put object onto bus at a dbus path.
46 * @param[in] bus - Bus to attach to.
Marri Devender Raof4682712019-03-19 05:00:28 -050047 * @param[in] event - sd event handler.
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050048 * @param[in] path - Path to attach at.
49 * @param[in] type - Type of the certificate.
50 * @param[in] unit - Unit consumed by this certificate.
Marri Devender Rao6ceec402019-02-01 03:15:19 -060051 * @param[in] installPath - Certificate installation path.
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050052 */
Marri Devender Raof4682712019-03-19 05:00:28 -050053 Manager(sdbusplus::bus::bus& bus, sdeventplus::Event& event,
54 const char* path, const CertificateType& type,
55 UnitsToRestart&& unit, CertInstallPath&& installPath);
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050056
57 /** @brief Implementation for Install
58 * Replace the existing certificate key file with another
59 * (possibly CA signed) Certificate key file.
60 *
Marri Devender Rao6ceec402019-02-01 03:15:19 -060061 * @param[in] filePath - Certificate key file path.
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050062 */
Marri Devender Rao6ceec402019-02-01 03:15:19 -060063 void install(const std::string filePath) override;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -050064
Deepak Kodihalliae70b3d2018-09-30 05:42:00 -050065 /** @brief Delete the certificate (and possibly revert
66 * to a self-signed certificate).
67 */
68 void delete_() override;
69
Marri Devender Raof4682712019-03-19 05:00:28 -050070 /** @brief Generate Private key and CSR file
71 * Generates the Private key file and CSR file based on the input
72 * parameters. Validation of the parameters is callers responsibility.
73 * At present supports only RSA algorithm type
74 *
75 * @param[in] alternativeNames - Additional hostnames of the component that
76 * is being secured.
77 * @param[in] challengePassword - The challenge password to be applied to
78 * the certificate for revocation requests.
79 * @param[in] city - The city or locality of the organization making the
80 * request. For Example Austin
81 * @param[in] commonName - The fully qualified domain name of the component
82 * that is being secured.
83 * @param[in] contactPerson - The name of the user making the request.
84 * @param[in] country - The country of the organization making the request.
85 * @param[in] email - The email address of the contact within the
86 * organization making the request.
87 * @param[in] givenName - The given name of the user making the request.
88 * @param[in] initials - The initials of the user making the request.
89 * @param[in] keyBitLength - The length of the key in bits, if needed based
90 * on the value of the KeyPairAlgorithm parameter.
91 * @param[in] keyCurveId - The curve ID to be used with the key, if needed
92 * based on the value of the KeyPairAlgorithm parameter.
93 * @param[in] keyPairAlgorithm - The type of key pair for use with signing
94 * algorithms. Valid built-in algorithm names for private key
95 * generation are: RSA, DSA, DH and EC.
96 * @param[in] keyUsage - Key usage extensions define the purpose of the
97 * public key contained in a certificate. Valid Key usage extensions
98 * and its usage description.
99 * - ClientAuthentication: The public key is used for TLS WWW client
100 * authentication.
101 * - CodeSigning: The public key is used for the signing of executable
102 * code
103 * - CRLSigning: The public key is used for verifying signatures on
104 * certificate revocation lists (CLRs).
105 * - DataEncipherment: The public key is used for directly enciphering
106 * raw user data without the use of an intermediate symmetric
107 * cipher.
108 * - DecipherOnly: The public key could be used for deciphering data
109 * while performing key agreement.
110 * - DigitalSignature: The public key is used for verifying digital
111 * signatures, other than signatures on certificatesand CRLs.
112 * - EmailProtection: The public key is used for email protection.
113 * - EncipherOnly: Thepublic key could be used for enciphering data
114 * while performing key agreement.
115 * - KeyCertSign: The public key is used for verifying signatures on
116 * public key certificates.
117 * - KeyEncipherment: The public key is used for enciphering private or
118 * secret keys.
119 * - NonRepudiation: The public key is used to verify digital
120 * signatures, other than signatures on certificates and CRLs, and
121 * used to provide a non-repudiation service that protects against
122 * the signing entity falsely denying some action.
123 * - OCSPSigning: The public key is used for signing OCSP responses.
124 * - ServerAuthentication: The public key is used for TLS WWW server
125 * authentication.
126 * - Timestamping: The public key is used for binding the hash of an
127 * object to a time.
128 * @param[in] organization - The legal name of the organization. This
129 * should not be abbreviated and should include suffixes such as Inc,
130 * Corp, or LLC.For example, IBM Corp.
131 * @param[in] organizationalUnit - The name of the unit or division of the
132 * organization making the request.
133 * @param[in] state - The state or province where the organization is
134 * located. This should not be abbreviated. For example, Texas.
135 * @param[in] surname - The surname of the user making the request.
136 * @param[in] unstructuredName - The unstructured name of the subject.
137 *
138 * @return path[std::string] - The object path of the D-Bus object
139 * representing CSR string. Note: For new CSR request will overwrite
140 * the existing CSR in the system.
141 */
142 std::string generateCSR(
143 std::vector<std::string> alternativeNames,
144 std::string challengePassword, std::string city, std::string commonName,
145 std::string contactPerson, std::string country, std::string email,
146 std::string givenName, std::string initials, int64_t keyBitLength,
147 std::string keyCurveId, std::string keyPairAlgorithm,
148 std::vector<std::string> keyUsage, std::string organization,
149 std::string organizationalUnit, std::string state, std::string surname,
150 std::string unstructuredName) override;
151
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500152 private:
Marri Devender Raof4682712019-03-19 05:00:28 -0500153 void generateCSRHelper(std::vector<std::string> alternativeNames,
154 std::string challengePassword, std::string city,
155 std::string commonName, std::string contactPerson,
156 std::string country, std::string email,
157 std::string givenName, std::string initials,
158 int64_t keyBitLength, std::string keyCurveId,
159 std::string keyPairAlgorithm,
160 std::vector<std::string> keyUsage,
161 std::string organization,
162 std::string organizationalUnit, std::string state,
163 std::string surname, std::string unstructuredName);
164
165 /** @brief Write private key data to file
166 *
167 * @param[in] keyBitLength - KeyBit length.
168 * @param[in] x509Req - pointer to X509 request.
169 * @return pointer to private key
170 */
171 EVP_PKEY_Ptr writePrivateKey(int64_t keyBitLength, X509_REQ_Ptr& x509Req);
172
173 /** @brief Add the specified CSR field with the data
174 * @param[in] x509Name - Structure used in setting certificate properties
175 * @param[in] field - field name
176 * @param[in] bytes - field value in bytes
177 */
178 void addEntry(X509_NAME* x509Name, const char* field,
179 const std::string& bytes);
180
181 /** @brief Create CSR D-Bus object by reading the data in the CSR file
182 * @param[in] statis - SUCCESSS/FAILURE In CSR generation.
183 */
184 void createCSRObject(const Status& status);
185
186 /** @brief Write generated CSR data to file
187 *
188 * @param[in] filePath - CSR file path.
189 * @param[in] x509Req - OpenSSL Request Pointer.
190 */
191 void writeCSR(const std::string& filePath, const X509_REQ_Ptr& x509Req);
192
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500193 /** @brief sdbusplus handler */
194 sdbusplus::bus::bus& bus;
195
Marri Devender Raof4682712019-03-19 05:00:28 -0500196 // sdevent Event handle
197 sdeventplus::Event& event;
198
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500199 /** @brief object path */
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600200 std::string objectPath;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500201
202 /** @brief Type of the certificate **/
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600203 CertificateType certType;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500204
205 /** @brief Unit name associated to the service **/
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600206 UnitsToRestart unitToRestart;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500207
208 /** @brief Certificate file installation path **/
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600209 CertInstallPath certInstallPath;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500210
Marri Devender Rao6ceec402019-02-01 03:15:19 -0600211 /** @brief pointer to certificate */
212 std::unique_ptr<Certificate> certificatePtr = nullptr;
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500213
Marri Devender Raof4682712019-03-19 05:00:28 -0500214 /** @brief pointer to CSR */
215 std::unique_ptr<CSR> csrPtr = nullptr;
216
217 /** @brief SDEventPlus child pointer added to event loop */
218 std::unique_ptr<sdeventplus::source::Child> childPtr;
219};
Jayanth Othayothcfbc8dc2018-09-03 07:22:27 -0500220} // namespace certs
221} // namespace phosphor