Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 1 | #pragma once |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame^] | 2 | #include "config.h" |
Jayanth Othayoth | dd74bd2 | 2018-09-28 06:13:35 -0500 | [diff] [blame] | 3 | |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame^] | 4 | #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 Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 10 | #include <xyz/openbmc_project/Certs/Install/server.hpp> |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Object/Delete/server.hpp> |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 12 | |
| 13 | namespace phosphor |
| 14 | { |
| 15 | namespace certs |
| 16 | { |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame^] | 17 | using Install = sdbusplus::xyz::openbmc_project::Certs::server::Install; |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 18 | using Delete = sdbusplus::xyz::openbmc_project::Object::server::Delete; |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame^] | 19 | using CSRCreate = sdbusplus::xyz::openbmc_project::Certs::CSR::server::Create; |
| 20 | using Ifaces = sdbusplus::server::object::object<Install, CSRCreate, Delete>; |
| 21 | |
| 22 | using X509_REQ_Ptr = std::unique_ptr<X509_REQ, decltype(&::X509_REQ_free)>; |
| 23 | using EVP_PKEY_Ptr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>; |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 24 | |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 25 | class Manager : public Ifaces |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 26 | { |
| 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 Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame^] | 47 | * @param[in] event - sd event handler. |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 48 | * @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 Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 51 | * @param[in] installPath - Certificate installation path. |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 52 | */ |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame^] | 53 | Manager(sdbusplus::bus::bus& bus, sdeventplus::Event& event, |
| 54 | const char* path, const CertificateType& type, |
| 55 | UnitsToRestart&& unit, CertInstallPath&& installPath); |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 56 | |
| 57 | /** @brief Implementation for Install |
| 58 | * Replace the existing certificate key file with another |
| 59 | * (possibly CA signed) Certificate key file. |
| 60 | * |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 61 | * @param[in] filePath - Certificate key file path. |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 62 | */ |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 63 | void install(const std::string filePath) override; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 64 | |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 65 | /** @brief Delete the certificate (and possibly revert |
| 66 | * to a self-signed certificate). |
| 67 | */ |
| 68 | void delete_() override; |
| 69 | |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame^] | 70 | /** @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 Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 152 | private: |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame^] | 153 | 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 Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 193 | /** @brief sdbusplus handler */ |
| 194 | sdbusplus::bus::bus& bus; |
| 195 | |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame^] | 196 | // sdevent Event handle |
| 197 | sdeventplus::Event& event; |
| 198 | |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 199 | /** @brief object path */ |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 200 | std::string objectPath; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 201 | |
| 202 | /** @brief Type of the certificate **/ |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 203 | CertificateType certType; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 204 | |
| 205 | /** @brief Unit name associated to the service **/ |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 206 | UnitsToRestart unitToRestart; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 207 | |
| 208 | /** @brief Certificate file installation path **/ |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 209 | CertInstallPath certInstallPath; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 210 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 211 | /** @brief pointer to certificate */ |
| 212 | std::unique_ptr<Certificate> certificatePtr = nullptr; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 213 | |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame^] | 214 | /** @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 Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 220 | } // namespace certs |
| 221 | } // namespace phosphor |