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" |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 6 | #include "watch.hpp" |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 7 | |
| 8 | #include <sdeventplus/source/child.hpp> |
| 9 | #include <sdeventplus/source/event.hpp> |
| 10 | #include <xyz/openbmc_project/Certs/CSR/Create/server.hpp> |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 11 | #include <xyz/openbmc_project/Certs/Install/server.hpp> |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Object/Delete/server.hpp> |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 13 | |
| 14 | namespace phosphor |
| 15 | { |
| 16 | namespace certs |
| 17 | { |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 18 | using Install = sdbusplus::xyz::openbmc_project::Certs::server::Install; |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 19 | using Delete = sdbusplus::xyz::openbmc_project::Object::server::Delete; |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 20 | using CSRCreate = sdbusplus::xyz::openbmc_project::Certs::CSR::server::Create; |
| 21 | using Ifaces = sdbusplus::server::object::object<Install, CSRCreate, Delete>; |
| 22 | |
| 23 | using X509_REQ_Ptr = std::unique_ptr<X509_REQ, decltype(&::X509_REQ_free)>; |
| 24 | using EVP_PKEY_Ptr = std::unique_ptr<EVP_PKEY, decltype(&::EVP_PKEY_free)>; |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 25 | using CertificatePtr = std::unique_ptr<Certificate>; |
Jayanth Othayoth | b50789c | 2018-10-09 07:13:54 -0500 | [diff] [blame] | 26 | |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 27 | class Manager : public Ifaces |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 28 | { |
| 29 | public: |
| 30 | /* Define all of the basic class operations: |
| 31 | * Not allowed: |
| 32 | * - Default constructor is not possible due to member |
| 33 | * reference |
| 34 | * - Move operations due to 'this' being registered as the |
| 35 | * 'context' with sdbus. |
| 36 | * Allowed: |
| 37 | * - copy |
| 38 | * - Destructor. |
| 39 | */ |
| 40 | Manager() = delete; |
| 41 | Manager(const Manager&) = default; |
| 42 | Manager& operator=(const Manager&) = delete; |
| 43 | Manager(Manager&&) = delete; |
| 44 | Manager& operator=(Manager&&) = delete; |
| 45 | virtual ~Manager() = default; |
| 46 | |
| 47 | /** @brief Constructor to put object onto bus at a dbus path. |
| 48 | * @param[in] bus - Bus to attach to. |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 49 | * @param[in] event - sd event handler. |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 50 | * @param[in] path - Path to attach at. |
| 51 | * @param[in] type - Type of the certificate. |
| 52 | * @param[in] unit - Unit consumed by this certificate. |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 53 | * @param[in] installPath - Certificate installation path. |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 54 | */ |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 55 | Manager(sdbusplus::bus::bus& bus, sdeventplus::Event& event, |
| 56 | const char* path, const CertificateType& type, |
| 57 | UnitsToRestart&& unit, CertInstallPath&& installPath); |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 58 | |
| 59 | /** @brief Implementation for Install |
| 60 | * Replace the existing certificate key file with another |
| 61 | * (possibly CA signed) Certificate key file. |
| 62 | * |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 63 | * @param[in] filePath - Certificate key file path. |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 64 | */ |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 65 | void install(const std::string filePath) override; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 66 | |
Deepak Kodihalli | ae70b3d | 2018-09-30 05:42:00 -0500 | [diff] [blame] | 67 | /** @brief Delete the certificate (and possibly revert |
| 68 | * to a self-signed certificate). |
| 69 | */ |
| 70 | void delete_() override; |
| 71 | |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 72 | /** @brief Generate Private key and CSR file |
| 73 | * Generates the Private key file and CSR file based on the input |
| 74 | * parameters. Validation of the parameters is callers responsibility. |
| 75 | * At present supports only RSA algorithm type |
| 76 | * |
| 77 | * @param[in] alternativeNames - Additional hostnames of the component that |
| 78 | * is being secured. |
| 79 | * @param[in] challengePassword - The challenge password to be applied to |
| 80 | * the certificate for revocation requests. |
| 81 | * @param[in] city - The city or locality of the organization making the |
| 82 | * request. For Example Austin |
| 83 | * @param[in] commonName - The fully qualified domain name of the component |
| 84 | * that is being secured. |
| 85 | * @param[in] contactPerson - The name of the user making the request. |
| 86 | * @param[in] country - The country of the organization making the request. |
| 87 | * @param[in] email - The email address of the contact within the |
| 88 | * organization making the request. |
| 89 | * @param[in] givenName - The given name of the user making the request. |
| 90 | * @param[in] initials - The initials of the user making the request. |
| 91 | * @param[in] keyBitLength - The length of the key in bits, if needed based |
| 92 | * on the value of the KeyPairAlgorithm parameter. |
| 93 | * @param[in] keyCurveId - The curve ID to be used with the key, if needed |
| 94 | * based on the value of the KeyPairAlgorithm parameter. |
| 95 | * @param[in] keyPairAlgorithm - The type of key pair for use with signing |
| 96 | * algorithms. Valid built-in algorithm names for private key |
| 97 | * generation are: RSA, DSA, DH and EC. |
| 98 | * @param[in] keyUsage - Key usage extensions define the purpose of the |
| 99 | * public key contained in a certificate. Valid Key usage extensions |
| 100 | * and its usage description. |
| 101 | * - ClientAuthentication: The public key is used for TLS WWW client |
| 102 | * authentication. |
| 103 | * - CodeSigning: The public key is used for the signing of executable |
| 104 | * code |
| 105 | * - CRLSigning: The public key is used for verifying signatures on |
| 106 | * certificate revocation lists (CLRs). |
| 107 | * - DataEncipherment: The public key is used for directly enciphering |
| 108 | * raw user data without the use of an intermediate symmetric |
| 109 | * cipher. |
| 110 | * - DecipherOnly: The public key could be used for deciphering data |
| 111 | * while performing key agreement. |
| 112 | * - DigitalSignature: The public key is used for verifying digital |
| 113 | * signatures, other than signatures on certificatesand CRLs. |
| 114 | * - EmailProtection: The public key is used for email protection. |
| 115 | * - EncipherOnly: Thepublic key could be used for enciphering data |
| 116 | * while performing key agreement. |
| 117 | * - KeyCertSign: The public key is used for verifying signatures on |
| 118 | * public key certificates. |
| 119 | * - KeyEncipherment: The public key is used for enciphering private or |
| 120 | * secret keys. |
| 121 | * - NonRepudiation: The public key is used to verify digital |
| 122 | * signatures, other than signatures on certificates and CRLs, and |
| 123 | * used to provide a non-repudiation service that protects against |
| 124 | * the signing entity falsely denying some action. |
| 125 | * - OCSPSigning: The public key is used for signing OCSP responses. |
| 126 | * - ServerAuthentication: The public key is used for TLS WWW server |
| 127 | * authentication. |
| 128 | * - Timestamping: The public key is used for binding the hash of an |
| 129 | * object to a time. |
| 130 | * @param[in] organization - The legal name of the organization. This |
| 131 | * should not be abbreviated and should include suffixes such as Inc, |
| 132 | * Corp, or LLC.For example, IBM Corp. |
| 133 | * @param[in] organizationalUnit - The name of the unit or division of the |
| 134 | * organization making the request. |
| 135 | * @param[in] state - The state or province where the organization is |
| 136 | * located. This should not be abbreviated. For example, Texas. |
| 137 | * @param[in] surname - The surname of the user making the request. |
| 138 | * @param[in] unstructuredName - The unstructured name of the subject. |
| 139 | * |
| 140 | * @return path[std::string] - The object path of the D-Bus object |
| 141 | * representing CSR string. Note: For new CSR request will overwrite |
| 142 | * the existing CSR in the system. |
| 143 | */ |
| 144 | std::string generateCSR( |
| 145 | std::vector<std::string> alternativeNames, |
| 146 | std::string challengePassword, std::string city, std::string commonName, |
| 147 | std::string contactPerson, std::string country, std::string email, |
| 148 | std::string givenName, std::string initials, int64_t keyBitLength, |
| 149 | std::string keyCurveId, std::string keyPairAlgorithm, |
| 150 | std::vector<std::string> keyUsage, std::string organization, |
| 151 | std::string organizationalUnit, std::string state, std::string surname, |
| 152 | std::string unstructuredName) override; |
| 153 | |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 154 | /** @brief Get reference to certificate |
| 155 | * |
| 156 | * @return Reference to certificate |
| 157 | */ |
| 158 | CertificatePtr& getCertificate(); |
| 159 | |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 160 | private: |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 161 | void generateCSRHelper(std::vector<std::string> alternativeNames, |
| 162 | std::string challengePassword, std::string city, |
| 163 | std::string commonName, std::string contactPerson, |
| 164 | std::string country, std::string email, |
| 165 | std::string givenName, std::string initials, |
| 166 | int64_t keyBitLength, std::string keyCurveId, |
| 167 | std::string keyPairAlgorithm, |
| 168 | std::vector<std::string> keyUsage, |
| 169 | std::string organization, |
| 170 | std::string organizationalUnit, std::string state, |
| 171 | std::string surname, std::string unstructuredName); |
| 172 | |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 173 | /** @brief Generate RSA Key pair and get private key from key pair |
| 174 | * @param[in] keyBitLength - KeyBit length. |
| 175 | * @return Pointer to RSA private key |
| 176 | */ |
| 177 | EVP_PKEY_Ptr generateRSAKeyPair(const int64_t keyBitLength); |
| 178 | |
| 179 | /** @brief Generate EC Key pair and get private key from key pair |
| 180 | * @param[in] p_KeyCurveId - Curve ID |
| 181 | * @return Pointer to EC private key |
| 182 | */ |
| 183 | EVP_PKEY_Ptr generateECKeyPair(const std::string& p_KeyCurveId); |
| 184 | |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 185 | /** @brief Write private key data to file |
| 186 | * |
Ramesh Iyyar | 8a09b52 | 2019-06-07 05:23:29 -0500 | [diff] [blame] | 187 | * @param[in] pKey - pointer to private key |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 188 | * @param[in] privKeyFileName - private key filename |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 189 | */ |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 190 | void writePrivateKey(const EVP_PKEY_Ptr& pKey, |
| 191 | const std::string& privKeyFileName); |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 192 | |
| 193 | /** @brief Add the specified CSR field with the data |
| 194 | * @param[in] x509Name - Structure used in setting certificate properties |
| 195 | * @param[in] field - field name |
| 196 | * @param[in] bytes - field value in bytes |
| 197 | */ |
| 198 | void addEntry(X509_NAME* x509Name, const char* field, |
| 199 | const std::string& bytes); |
| 200 | |
Marri Devender Rao | 7641105 | 2019-08-07 01:25:07 -0500 | [diff] [blame^] | 201 | /** @brief Check if usage is extended key usage |
| 202 | * @param[in] usage - key usage value |
| 203 | * @return true if part of extended key usage |
| 204 | */ |
| 205 | bool isExtendedKeyUsage(const std::string& usage); |
| 206 | |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 207 | /** @brief Create CSR D-Bus object by reading the data in the CSR file |
| 208 | * @param[in] statis - SUCCESSS/FAILURE In CSR generation. |
| 209 | */ |
| 210 | void createCSRObject(const Status& status); |
| 211 | |
| 212 | /** @brief Write generated CSR data to file |
| 213 | * |
| 214 | * @param[in] filePath - CSR file path. |
| 215 | * @param[in] x509Req - OpenSSL Request Pointer. |
| 216 | */ |
| 217 | void writeCSR(const std::string& filePath, const X509_REQ_Ptr& x509Req); |
| 218 | |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 219 | /** @brief Load certifiate |
| 220 | * Load certificate and create certificate object |
| 221 | */ |
| 222 | void createCertificate(); |
| 223 | |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 224 | /** @brief Create RSA private key file |
| 225 | * Create RSA private key file by generating rsa key if not created |
| 226 | */ |
| 227 | void createRSAPrivateKeyFile(); |
| 228 | |
| 229 | /** @brief Getting RSA private key |
| 230 | * Gettting RSA private key from generated file |
| 231 | * @param[in] keyBitLength - Key bit length |
| 232 | * @return Pointer to RSA key |
| 233 | */ |
| 234 | EVP_PKEY_Ptr getRSAKeyPair(const int64_t keyBitLength); |
| 235 | |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 236 | /** @brief sdbusplus handler */ |
| 237 | sdbusplus::bus::bus& bus; |
| 238 | |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 239 | // sdevent Event handle |
| 240 | sdeventplus::Event& event; |
| 241 | |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 242 | /** @brief object path */ |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 243 | std::string objectPath; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 244 | |
| 245 | /** @brief Type of the certificate **/ |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 246 | CertificateType certType; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 247 | |
| 248 | /** @brief Unit name associated to the service **/ |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 249 | UnitsToRestart unitToRestart; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 250 | |
| 251 | /** @brief Certificate file installation path **/ |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 252 | CertInstallPath certInstallPath; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 253 | |
Marri Devender Rao | 6ceec40 | 2019-02-01 03:15:19 -0600 | [diff] [blame] | 254 | /** @brief pointer to certificate */ |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 255 | CertificatePtr certificatePtr = nullptr; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 256 | |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 257 | /** @brief pointer to CSR */ |
| 258 | std::unique_ptr<CSR> csrPtr = nullptr; |
| 259 | |
| 260 | /** @brief SDEventPlus child pointer added to event loop */ |
Marri Devender Rao | ffad1ef | 2019-06-03 04:54:12 -0500 | [diff] [blame] | 261 | std::unique_ptr<sdeventplus::source::Child> childPtr = nullptr; |
| 262 | |
| 263 | /** @brief Watch on self signed certificates */ |
| 264 | std::unique_ptr<Watch> certWatchPtr = nullptr; |
Ramesh Iyyar | c6e58c7 | 2019-07-16 08:52:47 -0500 | [diff] [blame] | 265 | |
| 266 | /** @brif Parent path i.e certificate directory path */ |
| 267 | fs::path certParentInstallPath; |
Marri Devender Rao | f468271 | 2019-03-19 05:00:28 -0500 | [diff] [blame] | 268 | }; |
Jayanth Othayoth | cfbc8dc | 2018-09-03 07:22:27 -0500 | [diff] [blame] | 269 | } // namespace certs |
| 270 | } // namespace phosphor |