Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 1 | #pragma once |
| 2 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 3 | #include <app.hpp> |
Iwona Klimaszewska | e6604b1 | 2019-10-23 00:52:55 +0200 | [diff] [blame] | 4 | #include <boost/convert.hpp> |
| 5 | #include <boost/convert/strtol.hpp> |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 6 | #include <dbus_utility.hpp> |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 7 | #include <registries/privilege_registry.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 8 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 9 | namespace redfish |
| 10 | { |
| 11 | namespace certs |
| 12 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 13 | constexpr char const* httpsObjectPath = |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 14 | "/xyz/openbmc_project/certs/server/https"; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 15 | constexpr char const* certInstallIntf = "xyz.openbmc_project.Certs.Install"; |
| 16 | constexpr char const* certReplaceIntf = "xyz.openbmc_project.Certs.Replace"; |
| 17 | constexpr char const* objDeleteIntf = "xyz.openbmc_project.Object.Delete"; |
| 18 | constexpr char const* certPropIntf = "xyz.openbmc_project.Certs.Certificate"; |
| 19 | constexpr char const* dbusPropIntf = "org.freedesktop.DBus.Properties"; |
| 20 | constexpr char const* dbusObjManagerIntf = "org.freedesktop.DBus.ObjectManager"; |
| 21 | constexpr char const* ldapObjectPath = "/xyz/openbmc_project/certs/client/ldap"; |
| 22 | constexpr char const* httpsServiceName = |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 23 | "xyz.openbmc_project.Certs.Manager.Server.Https"; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 24 | constexpr char const* ldapServiceName = |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 25 | "xyz.openbmc_project.Certs.Manager.Client.Ldap"; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 26 | constexpr char const* authorityServiceName = |
Marri Devender Rao | cfcd5f6 | 2019-05-17 08:34:37 -0500 | [diff] [blame] | 27 | "xyz.openbmc_project.Certs.Manager.Authority.Ldap"; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 28 | constexpr char const* authorityObjectPath = |
Marri Devender Rao | cfcd5f6 | 2019-05-17 08:34:37 -0500 | [diff] [blame] | 29 | "/xyz/openbmc_project/certs/authority/ldap"; |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 30 | } // namespace certs |
| 31 | |
| 32 | /** |
| 33 | * The Certificate schema defines a Certificate Service which represents the |
| 34 | * actions available to manage certificates and links to where certificates |
| 35 | * are installed. |
| 36 | */ |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 37 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 38 | // TODO: Issue#61 No entries are available for Certificate |
| 39 | // service at https://www.dmtf.org/standards/redfish |
| 40 | // "redfish standard registries". Need to modify after DMTF |
| 41 | // publish Privilege details for certificate service |
| 42 | |
| 43 | inline void requestRoutesCertificateService(App& app) |
| 44 | { |
| 45 | BMCWEB_ROUTE(app, "/redfish/v1/CertificateService/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 46 | .privileges(redfish::privileges::getCertificateService) |
Abhishek Patel | 7204878 | 2021-06-02 09:53:24 -0500 | [diff] [blame] | 47 | .methods( |
| 48 | boost::beast::http::verb:: |
| 49 | get)([](const crow::Request& req, |
| 50 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 51 | asyncResp->res.jsonValue = { |
| 52 | {"@odata.type", |
| 53 | "#CertificateService.v1_0_0.CertificateService"}, |
| 54 | {"@odata.id", "/redfish/v1/CertificateService"}, |
| 55 | {"Id", "CertificateService"}, |
| 56 | {"Name", "Certificate Service"}, |
| 57 | {"Description", "Actions available to manage certificates"}}; |
| 58 | // /redfish/v1/CertificateService/CertificateLocations is something |
| 59 | // only ConfigureManager can access then only display when the user |
| 60 | // has permissions ConfigureManager |
| 61 | Privileges effectiveUserPrivileges = |
| 62 | redfish::getUserPrivileges(req.userRole); |
| 63 | if (isOperationAllowedWithPrivileges({{"ConfigureManager"}}, |
| 64 | effectiveUserPrivileges)) |
| 65 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 66 | asyncResp->res.jsonValue["CertificateLocations"] = { |
| 67 | {"@odata.id", |
| 68 | "/redfish/v1/CertificateService/CertificateLocations"}}; |
Abhishek Patel | 7204878 | 2021-06-02 09:53:24 -0500 | [diff] [blame] | 69 | } |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 70 | asyncResp->res |
| 71 | .jsonValue["Actions"] |
| 72 | ["#CertificateService.ReplaceCertificate"] = { |
| 73 | {"target", |
| 74 | "/redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate"}, |
| 75 | {"CertificateType@Redfish.AllowableValues", {"PEM"}}}; |
Abhishek Patel | 7204878 | 2021-06-02 09:53:24 -0500 | [diff] [blame] | 76 | asyncResp->res |
| 77 | .jsonValue["Actions"]["#CertificateService.GenerateCSR"] = { |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 78 | {"target", |
| 79 | "/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR"}}; |
Abhishek Patel | 7204878 | 2021-06-02 09:53:24 -0500 | [diff] [blame] | 80 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 81 | } // requestRoutesCertificateService |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 82 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 83 | /** |
| 84 | * @brief Find the ID specified in the URL |
| 85 | * Finds the numbers specified after the last "/" in the URL and returns. |
| 86 | * @param[in] path URL |
| 87 | * @return -1 on failure and number on success |
| 88 | */ |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 89 | inline long getIDFromURL(const std::string_view url) |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 90 | { |
Ed Tanous | f23b729 | 2020-10-15 09:41:17 -0700 | [diff] [blame] | 91 | std::size_t found = url.rfind('/'); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 92 | if (found == std::string::npos) |
| 93 | { |
| 94 | return -1; |
| 95 | } |
Iwona Klimaszewska | e6604b1 | 2019-10-23 00:52:55 +0200 | [diff] [blame] | 96 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 97 | if ((found + 1) < url.length()) |
| 98 | { |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 99 | std::string_view str = url.substr(found + 1); |
Iwona Klimaszewska | e6604b1 | 2019-10-23 00:52:55 +0200 | [diff] [blame] | 100 | |
| 101 | return boost::convert<long>(str, boost::cnv::strtol()).value_or(-1); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 102 | } |
Iwona Klimaszewska | e6604b1 | 2019-10-23 00:52:55 +0200 | [diff] [blame] | 103 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 104 | return -1; |
| 105 | } |
| 106 | |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 107 | inline std::string getCertificateFromReqBody( |
| 108 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 109 | const crow::Request& req) |
Kowalski, Kamil | 58eb238 | 2019-08-12 11:54:31 +0200 | [diff] [blame] | 110 | { |
| 111 | nlohmann::json reqJson = nlohmann::json::parse(req.body, nullptr, false); |
| 112 | |
| 113 | if (reqJson.is_discarded()) |
| 114 | { |
| 115 | // We did not receive JSON request, proceed as it is RAW data |
| 116 | return req.body; |
| 117 | } |
| 118 | |
| 119 | std::string certificate; |
| 120 | std::optional<std::string> certificateType = "PEM"; |
| 121 | |
| 122 | if (!json_util::readJson(reqJson, asyncResp->res, "CertificateString", |
| 123 | certificate, "CertificateType", certificateType)) |
| 124 | { |
| 125 | BMCWEB_LOG_ERROR << "Required parameters are missing"; |
| 126 | messages::internalError(asyncResp->res); |
Ed Tanous | abb93cd | 2021-09-02 14:34:57 -0700 | [diff] [blame] | 127 | return {}; |
Kowalski, Kamil | 58eb238 | 2019-08-12 11:54:31 +0200 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | if (*certificateType != "PEM") |
| 131 | { |
| 132 | messages::propertyValueNotInList(asyncResp->res, *certificateType, |
| 133 | "CertificateType"); |
Ed Tanous | abb93cd | 2021-09-02 14:34:57 -0700 | [diff] [blame] | 134 | return {}; |
Kowalski, Kamil | 58eb238 | 2019-08-12 11:54:31 +0200 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | return certificate; |
| 138 | } |
| 139 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 140 | /** |
| 141 | * Class to create a temporary certificate file for uploading to system |
| 142 | */ |
| 143 | class CertificateFile |
| 144 | { |
| 145 | public: |
| 146 | CertificateFile() = delete; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 147 | CertificateFile(const CertificateFile&) = delete; |
| 148 | CertificateFile& operator=(const CertificateFile&) = delete; |
| 149 | CertificateFile(CertificateFile&&) = delete; |
| 150 | CertificateFile& operator=(CertificateFile&&) = delete; |
| 151 | CertificateFile(const std::string& certString) |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 152 | { |
Ed Tanous | 72d52d2 | 2020-10-12 07:46:27 -0700 | [diff] [blame] | 153 | std::array<char, 18> dirTemplate = {'/', 't', 'm', 'p', '/', 'C', |
Ed Tanous | 5207438 | 2020-09-28 19:16:18 -0700 | [diff] [blame] | 154 | 'e', 'r', 't', 's', '.', 'X', |
| 155 | 'X', 'X', 'X', 'X', 'X', '\0'}; |
| 156 | char* tempDirectory = mkdtemp(dirTemplate.data()); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 157 | if (tempDirectory != nullptr) |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 158 | { |
| 159 | certDirectory = tempDirectory; |
| 160 | certificateFile = certDirectory / "cert.pem"; |
| 161 | std::ofstream out(certificateFile, std::ofstream::out | |
| 162 | std::ofstream::binary | |
| 163 | std::ofstream::trunc); |
| 164 | out << certString; |
| 165 | out.close(); |
| 166 | BMCWEB_LOG_DEBUG << "Creating certificate file" << certificateFile; |
| 167 | } |
| 168 | } |
| 169 | ~CertificateFile() |
| 170 | { |
| 171 | if (std::filesystem::exists(certDirectory)) |
| 172 | { |
| 173 | BMCWEB_LOG_DEBUG << "Removing certificate file" << certificateFile; |
Ed Tanous | 23a21a1 | 2020-07-25 04:45:05 +0000 | [diff] [blame] | 174 | std::error_code ec; |
| 175 | std::filesystem::remove_all(certDirectory, ec); |
| 176 | if (ec) |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 177 | { |
| 178 | BMCWEB_LOG_ERROR << "Failed to remove temp directory" |
| 179 | << certDirectory; |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | std::string getCertFilePath() |
| 184 | { |
| 185 | return certificateFile; |
| 186 | } |
| 187 | |
| 188 | private: |
| 189 | std::filesystem::path certificateFile; |
| 190 | std::filesystem::path certDirectory; |
| 191 | }; |
| 192 | |
Marri Devender Rao | 3021581 | 2019-03-18 08:59:21 -0500 | [diff] [blame] | 193 | static std::unique_ptr<sdbusplus::bus::match::match> csrMatcher; |
| 194 | /** |
| 195 | * @brief Read data from CSR D-bus object and set to response |
| 196 | * |
| 197 | * @param[in] asyncResp Shared pointer to the response message |
| 198 | * @param[in] certURI Link to certifiate collection URI |
| 199 | * @param[in] service D-Bus service name |
| 200 | * @param[in] certObjPath certificate D-Bus object path |
| 201 | * @param[in] csrObjPath CSR D-Bus object path |
| 202 | * @return None |
| 203 | */ |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 204 | static void getCSR(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 205 | const std::string& certURI, const std::string& service, |
| 206 | const std::string& certObjPath, |
| 207 | const std::string& csrObjPath) |
Marri Devender Rao | 3021581 | 2019-03-18 08:59:21 -0500 | [diff] [blame] | 208 | { |
| 209 | BMCWEB_LOG_DEBUG << "getCSR CertObjectPath" << certObjPath |
| 210 | << " CSRObjectPath=" << csrObjPath |
| 211 | << " service=" << service; |
| 212 | crow::connections::systemBus->async_method_call( |
| 213 | [asyncResp, certURI](const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 214 | const std::string& csr) { |
Marri Devender Rao | 3021581 | 2019-03-18 08:59:21 -0500 | [diff] [blame] | 215 | if (ec) |
| 216 | { |
| 217 | BMCWEB_LOG_ERROR << "DBUS response error: " << ec; |
| 218 | messages::internalError(asyncResp->res); |
| 219 | return; |
| 220 | } |
| 221 | if (csr.empty()) |
| 222 | { |
| 223 | BMCWEB_LOG_ERROR << "CSR read is empty"; |
| 224 | messages::internalError(asyncResp->res); |
| 225 | return; |
| 226 | } |
| 227 | asyncResp->res.jsonValue["CSRString"] = csr; |
| 228 | asyncResp->res.jsonValue["CertificateCollection"] = { |
| 229 | {"@odata.id", certURI}}; |
| 230 | }, |
| 231 | service, csrObjPath, "xyz.openbmc_project.Certs.CSR", "CSR"); |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Action to Generate CSR |
| 236 | */ |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 237 | inline void requestRoutesCertificateActionGenerateCSR(App& app) |
Marri Devender Rao | 3021581 | 2019-03-18 08:59:21 -0500 | [diff] [blame] | 238 | { |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 239 | BMCWEB_ROUTE( |
| 240 | app, |
| 241 | "/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 242 | // Incorrect Privilege; Should be ConfigureManager |
| 243 | //.privileges(redfish::privileges::postCertificateService) |
Ed Tanous | 432a890 | 2021-06-14 15:28:56 -0700 | [diff] [blame] | 244 | .privileges({{"ConfigureComponents"}}) |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 245 | .methods( |
| 246 | boost::beast::http::verb:: |
| 247 | post)([](const crow::Request& req, |
| 248 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 249 | static const int rsaKeyBitLength = 2048; |
Marri Devender Rao | 3021581 | 2019-03-18 08:59:21 -0500 | [diff] [blame] | 250 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 251 | // Required parameters |
| 252 | std::string city; |
| 253 | std::string commonName; |
| 254 | std::string country; |
| 255 | std::string organization; |
| 256 | std::string organizationalUnit; |
| 257 | std::string state; |
| 258 | nlohmann::json certificateCollection; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 259 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 260 | // Optional parameters |
| 261 | std::optional<std::vector<std::string>> optAlternativeNames = |
| 262 | std::vector<std::string>(); |
| 263 | std::optional<std::string> optContactPerson = ""; |
| 264 | std::optional<std::string> optChallengePassword = ""; |
| 265 | std::optional<std::string> optEmail = ""; |
| 266 | std::optional<std::string> optGivenName = ""; |
| 267 | std::optional<std::string> optInitials = ""; |
| 268 | std::optional<int64_t> optKeyBitLength = rsaKeyBitLength; |
| 269 | std::optional<std::string> optKeyCurveId = "secp384r1"; |
| 270 | std::optional<std::string> optKeyPairAlgorithm = "EC"; |
| 271 | std::optional<std::vector<std::string>> optKeyUsage = |
| 272 | std::vector<std::string>(); |
| 273 | std::optional<std::string> optSurname = ""; |
| 274 | std::optional<std::string> optUnstructuredName = ""; |
| 275 | if (!json_util::readJson( |
| 276 | req, asyncResp->res, "City", city, "CommonName", commonName, |
| 277 | "ContactPerson", optContactPerson, "Country", country, |
| 278 | "Organization", organization, "OrganizationalUnit", |
| 279 | organizationalUnit, "State", state, "CertificateCollection", |
| 280 | certificateCollection, "AlternativeNames", |
| 281 | optAlternativeNames, "ChallengePassword", |
| 282 | optChallengePassword, "Email", optEmail, "GivenName", |
| 283 | optGivenName, "Initials", optInitials, "KeyBitLength", |
| 284 | optKeyBitLength, "KeyCurveId", optKeyCurveId, |
| 285 | "KeyPairAlgorithm", optKeyPairAlgorithm, "KeyUsage", |
| 286 | optKeyUsage, "Surname", optSurname, "UnstructuredName", |
| 287 | optUnstructuredName)) |
| 288 | { |
| 289 | return; |
| 290 | } |
| 291 | |
| 292 | // bmcweb has no way to store or decode a private key challenge |
| 293 | // password, which will likely cause bmcweb to crash on startup |
| 294 | // if this is not set on a post so not allowing the user to set |
| 295 | // value |
Ed Tanous | 26f6976 | 2022-01-25 09:49:11 -0800 | [diff] [blame] | 296 | if (!optChallengePassword->empty()) |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 297 | { |
| 298 | messages::actionParameterNotSupported( |
| 299 | asyncResp->res, "GenerateCSR", "ChallengePassword"); |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | std::string certURI; |
| 304 | if (!redfish::json_util::readJson(certificateCollection, |
| 305 | asyncResp->res, "@odata.id", |
| 306 | certURI)) |
| 307 | { |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | std::string objectPath; |
| 312 | std::string service; |
| 313 | if (boost::starts_with( |
| 314 | certURI, |
| 315 | "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates")) |
| 316 | { |
| 317 | objectPath = certs::httpsObjectPath; |
| 318 | service = certs::httpsServiceName; |
| 319 | } |
| 320 | else if (boost::starts_with( |
| 321 | certURI, |
| 322 | "/redfish/v1/AccountService/LDAP/Certificates")) |
| 323 | { |
| 324 | objectPath = certs::ldapObjectPath; |
| 325 | service = certs::ldapServiceName; |
| 326 | } |
| 327 | else |
| 328 | { |
| 329 | messages::actionParameterNotSupported( |
| 330 | asyncResp->res, "CertificateCollection", "GenerateCSR"); |
| 331 | return; |
| 332 | } |
| 333 | |
| 334 | // supporting only EC and RSA algorithm |
| 335 | if (*optKeyPairAlgorithm != "EC" && *optKeyPairAlgorithm != "RSA") |
| 336 | { |
| 337 | messages::actionParameterNotSupported( |
| 338 | asyncResp->res, "KeyPairAlgorithm", "GenerateCSR"); |
| 339 | return; |
| 340 | } |
| 341 | |
| 342 | // supporting only 2048 key bit length for RSA algorithm due to |
| 343 | // time consumed in generating private key |
| 344 | if (*optKeyPairAlgorithm == "RSA" && |
| 345 | *optKeyBitLength != rsaKeyBitLength) |
| 346 | { |
| 347 | messages::propertyValueNotInList( |
| 348 | asyncResp->res, std::to_string(*optKeyBitLength), |
| 349 | "KeyBitLength"); |
| 350 | return; |
| 351 | } |
| 352 | |
| 353 | // validate KeyUsage supporting only 1 type based on URL |
| 354 | if (boost::starts_with( |
| 355 | certURI, |
| 356 | "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates")) |
| 357 | { |
Ed Tanous | 26f6976 | 2022-01-25 09:49:11 -0800 | [diff] [blame] | 358 | if (optKeyUsage->empty()) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 359 | { |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 360 | optKeyUsage->push_back("ServerAuthentication"); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 361 | } |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 362 | else if (optKeyUsage->size() == 1) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 363 | { |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 364 | if ((*optKeyUsage)[0] != "ServerAuthentication") |
| 365 | { |
| 366 | messages::propertyValueNotInList( |
| 367 | asyncResp->res, (*optKeyUsage)[0], "KeyUsage"); |
| 368 | return; |
| 369 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 370 | } |
| 371 | else |
| 372 | { |
| 373 | messages::actionParameterNotSupported( |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 374 | asyncResp->res, "KeyUsage", "GenerateCSR"); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 375 | return; |
| 376 | } |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 377 | } |
| 378 | else if (boost::starts_with( |
| 379 | certURI, |
| 380 | "/redfish/v1/AccountService/LDAP/Certificates")) |
| 381 | { |
Ed Tanous | 26f6976 | 2022-01-25 09:49:11 -0800 | [diff] [blame] | 382 | if (optKeyUsage->empty()) |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 383 | { |
| 384 | optKeyUsage->push_back("ClientAuthentication"); |
| 385 | } |
| 386 | else if (optKeyUsage->size() == 1) |
| 387 | { |
| 388 | if ((*optKeyUsage)[0] != "ClientAuthentication") |
| 389 | { |
| 390 | messages::propertyValueNotInList( |
| 391 | asyncResp->res, (*optKeyUsage)[0], "KeyUsage"); |
| 392 | return; |
| 393 | } |
| 394 | } |
| 395 | else |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 396 | { |
| 397 | messages::actionParameterNotSupported( |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 398 | asyncResp->res, "KeyUsage", "GenerateCSR"); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 399 | return; |
| 400 | } |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 401 | } |
Marri Devender Rao | 3021581 | 2019-03-18 08:59:21 -0500 | [diff] [blame] | 402 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 403 | // Only allow one CSR matcher at a time so setting retry |
| 404 | // time-out and timer expiry to 10 seconds for now. |
| 405 | static const int timeOut = 10; |
| 406 | if (csrMatcher) |
| 407 | { |
| 408 | messages::serviceTemporarilyUnavailable( |
| 409 | asyncResp->res, std::to_string(timeOut)); |
| 410 | return; |
| 411 | } |
Marri Devender Rao | 3021581 | 2019-03-18 08:59:21 -0500 | [diff] [blame] | 412 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 413 | // Make this static so it survives outside this method |
| 414 | static boost::asio::steady_timer timeout(*req.ioService); |
| 415 | timeout.expires_after(std::chrono::seconds(timeOut)); |
| 416 | timeout.async_wait( |
| 417 | [asyncResp](const boost::system::error_code& ec) { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 418 | csrMatcher = nullptr; |
| 419 | if (ec) |
| 420 | { |
| 421 | // operation_aborted is expected if timer is canceled |
| 422 | // before completion. |
| 423 | if (ec != boost::asio::error::operation_aborted) |
| 424 | { |
| 425 | BMCWEB_LOG_ERROR << "Async_wait failed " << ec; |
| 426 | } |
| 427 | return; |
| 428 | } |
| 429 | BMCWEB_LOG_ERROR << "Timed out waiting for Generating CSR"; |
| 430 | messages::internalError(asyncResp->res); |
| 431 | }); |
| 432 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 433 | // create a matcher to wait on CSR object |
| 434 | BMCWEB_LOG_DEBUG << "create matcher with path " << objectPath; |
| 435 | std::string match("type='signal'," |
| 436 | "interface='org.freedesktop.DBus.ObjectManager'," |
| 437 | "path='" + |
| 438 | objectPath + |
| 439 | "'," |
| 440 | "member='InterfacesAdded'"); |
| 441 | csrMatcher = std::make_unique<sdbusplus::bus::match::match>( |
| 442 | *crow::connections::systemBus, match, |
| 443 | [asyncResp, service, objectPath, |
| 444 | certURI](sdbusplus::message::message& m) { |
| 445 | timeout.cancel(); |
| 446 | if (m.is_method_error()) |
| 447 | { |
| 448 | BMCWEB_LOG_ERROR << "Dbus method error!!!"; |
| 449 | messages::internalError(asyncResp->res); |
| 450 | return; |
| 451 | } |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 452 | std::vector<std::pair< |
| 453 | std::string, |
| 454 | std::vector<std::pair<std::string, |
| 455 | dbus::utility::DbusVariantType>>>> |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 456 | interfacesProperties; |
| 457 | sdbusplus::message::object_path csrObjectPath; |
| 458 | m.read(csrObjectPath, interfacesProperties); |
| 459 | BMCWEB_LOG_DEBUG << "CSR object added" << csrObjectPath.str; |
| 460 | for (auto& interface : interfacesProperties) |
| 461 | { |
| 462 | if (interface.first == "xyz.openbmc_project.Certs.CSR") |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 463 | { |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 464 | getCSR(asyncResp, certURI, service, objectPath, |
| 465 | csrObjectPath.str); |
| 466 | break; |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 467 | } |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 468 | } |
| 469 | }); |
| 470 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 914e2d5 | 2022-01-07 11:38:34 -0800 | [diff] [blame] | 471 | [asyncResp](const boost::system::error_code ec, |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 472 | const std::string&) { |
| 473 | if (ec) |
| 474 | { |
| 475 | BMCWEB_LOG_ERROR << "DBUS response error: " |
| 476 | << ec.message(); |
| 477 | messages::internalError(asyncResp->res); |
| 478 | return; |
| 479 | } |
| 480 | }, |
| 481 | service, objectPath, "xyz.openbmc_project.Certs.CSR.Create", |
| 482 | "GenerateCSR", *optAlternativeNames, *optChallengePassword, |
| 483 | city, commonName, *optContactPerson, country, *optEmail, |
| 484 | *optGivenName, *optInitials, *optKeyBitLength, *optKeyCurveId, |
| 485 | *optKeyPairAlgorithm, *optKeyUsage, organization, |
| 486 | organizationalUnit, state, *optSurname, *optUnstructuredName); |
| 487 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 488 | } // requestRoutesCertificateActionGenerateCSR |
Marri Devender Rao | 3021581 | 2019-03-18 08:59:21 -0500 | [diff] [blame] | 489 | |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 490 | /** |
Gunnar Mills | 4e0453b | 2020-07-08 14:00:30 -0500 | [diff] [blame] | 491 | * @brief Parse and update Certificate Issue/Subject property |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 492 | * |
| 493 | * @param[in] asyncResp Shared pointer to the response message |
| 494 | * @param[in] str Issuer/Subject value in key=value pairs |
| 495 | * @param[in] type Issuer/Subject |
| 496 | * @return None |
| 497 | */ |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 498 | static void updateCertIssuerOrSubject(nlohmann::json& out, |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 499 | const std::string_view value) |
| 500 | { |
| 501 | // example: O=openbmc-project.xyz,CN=localhost |
| 502 | std::string_view::iterator i = value.begin(); |
| 503 | while (i != value.end()) |
| 504 | { |
| 505 | std::string_view::iterator tokenBegin = i; |
| 506 | while (i != value.end() && *i != '=') |
| 507 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 508 | ++i; |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 509 | } |
| 510 | if (i == value.end()) |
| 511 | { |
| 512 | break; |
| 513 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 514 | const std::string_view key(tokenBegin, |
| 515 | static_cast<size_t>(i - tokenBegin)); |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 516 | ++i; |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 517 | tokenBegin = i; |
| 518 | while (i != value.end() && *i != ',') |
| 519 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 520 | ++i; |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 521 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 522 | const std::string_view val(tokenBegin, |
| 523 | static_cast<size_t>(i - tokenBegin)); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 524 | if (key == "L") |
| 525 | { |
| 526 | out["City"] = val; |
| 527 | } |
| 528 | else if (key == "CN") |
| 529 | { |
| 530 | out["CommonName"] = val; |
| 531 | } |
| 532 | else if (key == "C") |
| 533 | { |
| 534 | out["Country"] = val; |
| 535 | } |
| 536 | else if (key == "O") |
| 537 | { |
| 538 | out["Organization"] = val; |
| 539 | } |
| 540 | else if (key == "OU") |
| 541 | { |
| 542 | out["OrganizationalUnit"] = val; |
| 543 | } |
| 544 | else if (key == "ST") |
| 545 | { |
| 546 | out["State"] = val; |
| 547 | } |
| 548 | // skip comma character |
| 549 | if (i != value.end()) |
| 550 | { |
Manojkiran Eda | 17a897d | 2020-09-12 15:31:58 +0530 | [diff] [blame] | 551 | ++i; |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | /** |
| 557 | * @brief Retrieve the certificates properties and append to the response |
| 558 | * message |
| 559 | * |
| 560 | * @param[in] asyncResp Shared pointer to the response message |
| 561 | * @param[in] objectPath Path of the D-Bus service object |
| 562 | * @param[in] certId Id of the certificate |
| 563 | * @param[in] certURL URL of the certificate object |
| 564 | * @param[in] name name of the certificate |
| 565 | * @return None |
| 566 | */ |
| 567 | static void getCertificateProperties( |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 568 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 569 | const std::string& objectPath, const std::string& service, long certId, |
| 570 | const std::string& certURL, const std::string& name) |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 571 | { |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 572 | using PropertiesMap = |
| 573 | boost::container::flat_map<std::string, dbus::utility::DbusVariantType>; |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 574 | BMCWEB_LOG_DEBUG << "getCertificateProperties Path=" << objectPath |
| 575 | << " certId=" << certId << " certURl=" << certURL; |
| 576 | crow::connections::systemBus->async_method_call( |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 577 | [asyncResp, certURL, certId, name](const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 578 | const PropertiesMap& properties) { |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 579 | if (ec) |
| 580 | { |
| 581 | BMCWEB_LOG_ERROR << "DBUS response error: " << ec; |
Marri Devender Rao | 8aae75a | 2019-07-16 03:26:50 -0500 | [diff] [blame] | 582 | messages::resourceNotFound(asyncResp->res, name, |
| 583 | std::to_string(certId)); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 584 | return; |
| 585 | } |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 586 | asyncResp->res.jsonValue = { |
| 587 | {"@odata.id", certURL}, |
| 588 | {"@odata.type", "#Certificate.v1_0_0.Certificate"}, |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 589 | {"Id", std::to_string(certId)}, |
| 590 | {"Name", name}, |
| 591 | {"Description", name}}; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 592 | for (const auto& property : properties) |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 593 | { |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 594 | if (property.first == "CertificateString") |
| 595 | { |
| 596 | asyncResp->res.jsonValue["CertificateString"] = ""; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 597 | const std::string* value = |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 598 | std::get_if<std::string>(&property.second); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 599 | if (value != nullptr) |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 600 | { |
| 601 | asyncResp->res.jsonValue["CertificateString"] = *value; |
| 602 | } |
| 603 | } |
| 604 | else if (property.first == "KeyUsage") |
| 605 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 606 | nlohmann::json& keyUsage = |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 607 | asyncResp->res.jsonValue["KeyUsage"]; |
| 608 | keyUsage = nlohmann::json::array(); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 609 | const std::vector<std::string>* value = |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 610 | std::get_if<std::vector<std::string>>(&property.second); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 611 | if (value != nullptr) |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 612 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 613 | for (const std::string& usage : *value) |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 614 | { |
| 615 | keyUsage.push_back(usage); |
| 616 | } |
| 617 | } |
| 618 | } |
| 619 | else if (property.first == "Issuer") |
| 620 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 621 | const std::string* value = |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 622 | std::get_if<std::string>(&property.second); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 623 | if (value != nullptr) |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 624 | { |
| 625 | updateCertIssuerOrSubject( |
| 626 | asyncResp->res.jsonValue["Issuer"], *value); |
| 627 | } |
| 628 | } |
| 629 | else if (property.first == "Subject") |
| 630 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 631 | const std::string* value = |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 632 | std::get_if<std::string>(&property.second); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 633 | if (value != nullptr) |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 634 | { |
| 635 | updateCertIssuerOrSubject( |
| 636 | asyncResp->res.jsonValue["Subject"], *value); |
| 637 | } |
| 638 | } |
| 639 | else if (property.first == "ValidNotAfter") |
| 640 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 641 | const uint64_t* value = |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 642 | std::get_if<uint64_t>(&property.second); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 643 | if (value != nullptr) |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 644 | { |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 645 | asyncResp->res.jsonValue["ValidNotAfter"] = |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 646 | crow::utility::getDateTimeUint(*value); |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | else if (property.first == "ValidNotBefore") |
| 650 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 651 | const uint64_t* value = |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 652 | std::get_if<uint64_t>(&property.second); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 653 | if (value != nullptr) |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 654 | { |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 655 | asyncResp->res.jsonValue["ValidNotBefore"] = |
Nan Zhou | 1d8782e | 2021-11-29 22:23:18 -0800 | [diff] [blame] | 656 | crow::utility::getDateTimeUint(*value); |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 657 | } |
| 658 | } |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 659 | } |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 660 | asyncResp->res.addHeader("Location", certURL); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 661 | }, |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 662 | service, objectPath, certs::dbusPropIntf, "GetAll", |
| 663 | certs::certPropIntf); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 664 | } |
| 665 | |
| 666 | using GetObjectType = |
| 667 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 668 | |
| 669 | /** |
| 670 | * Action to replace an existing certificate |
| 671 | */ |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 672 | inline void requestRoutesCertificateActionsReplaceCertificate(App& app) |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 673 | { |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 674 | BMCWEB_ROUTE( |
| 675 | app, |
| 676 | "/redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 677 | .privileges(redfish::privileges::postCertificateService) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 678 | .methods( |
| 679 | boost::beast::http::verb:: |
| 680 | post)([](const crow::Request& req, |
| 681 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 682 | std::string certificate; |
| 683 | nlohmann::json certificateUri; |
| 684 | std::optional<std::string> certificateType = "PEM"; |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 685 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 686 | if (!json_util::readJson(req, asyncResp->res, "CertificateString", |
| 687 | certificate, "CertificateUri", |
| 688 | certificateUri, "CertificateType", |
| 689 | certificateType)) |
| 690 | { |
| 691 | BMCWEB_LOG_ERROR << "Required parameters are missing"; |
| 692 | messages::internalError(asyncResp->res); |
| 693 | return; |
| 694 | } |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 695 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 696 | if (!certificateType) |
| 697 | { |
| 698 | // should never happen, but it never hurts to be paranoid. |
| 699 | return; |
| 700 | } |
| 701 | if (certificateType != "PEM") |
| 702 | { |
| 703 | messages::actionParameterNotSupported( |
| 704 | asyncResp->res, "CertificateType", "ReplaceCertificate"); |
| 705 | return; |
| 706 | } |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 707 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 708 | std::string certURI; |
| 709 | if (!redfish::json_util::readJson(certificateUri, asyncResp->res, |
| 710 | "@odata.id", certURI)) |
| 711 | { |
| 712 | messages::actionParameterMissing( |
| 713 | asyncResp->res, "ReplaceCertificate", "CertificateUri"); |
| 714 | return; |
| 715 | } |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 716 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 717 | BMCWEB_LOG_INFO << "Certificate URI to replace" << certURI; |
| 718 | long id = getIDFromURL(certURI); |
| 719 | if (id < 0) |
| 720 | { |
| 721 | messages::actionParameterValueFormatError( |
| 722 | asyncResp->res, certURI, "CertificateUri", |
| 723 | "ReplaceCertificate"); |
| 724 | return; |
| 725 | } |
| 726 | std::string objectPath; |
| 727 | std::string name; |
| 728 | std::string service; |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 729 | if (boost::starts_with( |
| 730 | certURI, |
| 731 | "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/")) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 732 | { |
| 733 | objectPath = std::string(certs::httpsObjectPath) + "/" + |
| 734 | std::to_string(id); |
| 735 | name = "HTTPS certificate"; |
| 736 | service = certs::httpsServiceName; |
| 737 | } |
| 738 | else if (boost::starts_with( |
| 739 | certURI, |
| 740 | "/redfish/v1/AccountService/LDAP/Certificates/")) |
| 741 | { |
| 742 | objectPath = std::string(certs::ldapObjectPath) + "/" + |
| 743 | std::to_string(id); |
| 744 | name = "LDAP certificate"; |
| 745 | service = certs::ldapServiceName; |
| 746 | } |
| 747 | else if (boost::starts_with( |
| 748 | certURI, |
| 749 | "/redfish/v1/Managers/bmc/Truststore/Certificates/")) |
| 750 | { |
| 751 | objectPath = std::string(certs::authorityObjectPath) + "/" + |
| 752 | std::to_string(id); |
| 753 | name = "TrustStore certificate"; |
| 754 | service = certs::authorityServiceName; |
| 755 | } |
| 756 | else |
| 757 | { |
| 758 | messages::actionParameterNotSupported( |
| 759 | asyncResp->res, "CertificateUri", "ReplaceCertificate"); |
| 760 | return; |
| 761 | } |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 762 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 763 | std::shared_ptr<CertificateFile> certFile = |
| 764 | std::make_shared<CertificateFile>(certificate); |
| 765 | crow::connections::systemBus->async_method_call( |
| 766 | [asyncResp, certFile, objectPath, service, certURI, id, |
| 767 | name](const boost::system::error_code ec) { |
| 768 | if (ec) |
| 769 | { |
| 770 | BMCWEB_LOG_ERROR << "DBUS response error: " << ec; |
| 771 | messages::resourceNotFound(asyncResp->res, name, |
| 772 | std::to_string(id)); |
| 773 | return; |
| 774 | } |
| 775 | getCertificateProperties(asyncResp, objectPath, service, id, |
| 776 | certURI, name); |
| 777 | BMCWEB_LOG_DEBUG << "HTTPS certificate install file=" |
| 778 | << certFile->getCertFilePath(); |
| 779 | }, |
| 780 | service, objectPath, certs::certReplaceIntf, "Replace", |
| 781 | certFile->getCertFilePath()); |
| 782 | }); |
| 783 | } // requestRoutesCertificateActionsReplaceCertificate |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 784 | |
| 785 | /** |
| 786 | * Certificate resource describes a certificate used to prove the identity |
| 787 | * of a component, account or service. |
| 788 | */ |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 789 | |
| 790 | inline void requestRoutesHTTPSCertificate(App& app) |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 791 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 792 | BMCWEB_ROUTE( |
| 793 | app, |
| 794 | "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 795 | .privileges(redfish::privileges::getCertificate) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 796 | .methods( |
| 797 | boost::beast::http::verb:: |
| 798 | get)([](const crow::Request& req, |
| 799 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 800 | const std::string& param) -> void { |
| 801 | if (param.empty()) |
| 802 | { |
| 803 | messages::internalError(asyncResp->res); |
| 804 | return; |
| 805 | } |
| 806 | long id = getIDFromURL(req.url); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 807 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 808 | BMCWEB_LOG_DEBUG << "HTTPSCertificate::doGet ID=" |
| 809 | << std::to_string(id); |
| 810 | std::string certURL = |
| 811 | "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/" + |
| 812 | std::to_string(id); |
| 813 | std::string objectPath = certs::httpsObjectPath; |
| 814 | objectPath += "/"; |
| 815 | objectPath += std::to_string(id); |
| 816 | getCertificateProperties(asyncResp, objectPath, |
| 817 | certs::httpsServiceName, id, certURL, |
| 818 | "HTTPS Certificate"); |
| 819 | }); |
| 820 | } |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 821 | |
| 822 | /** |
| 823 | * Collection of HTTPS certificates |
| 824 | */ |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 825 | inline void requestRoutesHTTPSCertificateCollection(App& app) |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 826 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 827 | BMCWEB_ROUTE(app, |
| 828 | "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 829 | .privileges(redfish::privileges::getCertificateCollection) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 830 | .methods( |
| 831 | boost::beast::http::verb:: |
| 832 | get)([](const crow::Request&, |
| 833 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 834 | asyncResp->res.jsonValue = { |
| 835 | {"@odata.id", |
| 836 | "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates"}, |
| 837 | {"@odata.type", "#CertificateCollection.CertificateCollection"}, |
| 838 | {"Name", "HTTPS Certificates Collection"}, |
| 839 | {"Description", "A Collection of HTTPS certificate instances"}}; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 840 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 841 | crow::connections::systemBus->async_method_call( |
| 842 | [asyncResp](const boost::system::error_code ec, |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 843 | const dbus::utility::ManagedObjectType& certs) { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 844 | if (ec) |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 845 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 846 | BMCWEB_LOG_ERROR << "DBUS response error: " << ec; |
| 847 | messages::internalError(asyncResp->res); |
| 848 | return; |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 849 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 850 | nlohmann::json& members = |
| 851 | asyncResp->res.jsonValue["Members"]; |
| 852 | members = nlohmann::json::array(); |
| 853 | for (const auto& cert : certs) |
| 854 | { |
| 855 | long id = getIDFromURL(cert.first.str); |
| 856 | if (id >= 0) |
| 857 | { |
| 858 | members.push_back( |
| 859 | {{"@odata.id", |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 860 | "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/" + |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 861 | std::to_string(id)}}); |
| 862 | } |
| 863 | } |
| 864 | asyncResp->res.jsonValue["Members@odata.count"] = |
| 865 | members.size(); |
| 866 | }, |
| 867 | certs::httpsServiceName, certs::httpsObjectPath, |
| 868 | certs::dbusObjManagerIntf, "GetManagedObjects"); |
| 869 | }); |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 870 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 871 | BMCWEB_ROUTE(app, |
| 872 | "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 873 | .privileges(redfish::privileges::postCertificateCollection) |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 874 | .methods( |
| 875 | boost::beast::http::verb:: |
| 876 | post)([](const crow::Request& req, |
| 877 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 878 | BMCWEB_LOG_DEBUG << "HTTPSCertificateCollection::doPost"; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 879 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 880 | asyncResp->res.jsonValue = {{"Name", "HTTPS Certificate"}, |
| 881 | {"Description", "HTTPS Certificate"}}; |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 882 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 883 | std::string certFileBody = |
| 884 | getCertificateFromReqBody(asyncResp, req); |
Kowalski, Kamil | 58eb238 | 2019-08-12 11:54:31 +0200 | [diff] [blame] | 885 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 886 | if (certFileBody.empty()) |
| 887 | { |
| 888 | BMCWEB_LOG_ERROR << "Cannot get certificate from request body."; |
| 889 | messages::unrecognizedRequestBody(asyncResp->res); |
| 890 | return; |
| 891 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 892 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 893 | std::shared_ptr<CertificateFile> certFile = |
| 894 | std::make_shared<CertificateFile>(certFileBody); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 895 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 896 | crow::connections::systemBus->async_method_call( |
| 897 | [asyncResp, certFile](const boost::system::error_code ec, |
| 898 | const std::string& objectPath) { |
| 899 | if (ec) |
| 900 | { |
| 901 | BMCWEB_LOG_ERROR << "DBUS response error: " << ec; |
| 902 | messages::internalError(asyncResp->res); |
| 903 | return; |
| 904 | } |
| 905 | long certId = getIDFromURL(objectPath); |
| 906 | if (certId < 0) |
| 907 | { |
| 908 | BMCWEB_LOG_ERROR << "Invalid objectPath value" |
| 909 | << objectPath; |
| 910 | messages::internalError(asyncResp->res); |
| 911 | return; |
| 912 | } |
| 913 | std::string certURL = |
| 914 | "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/" + |
| 915 | std::to_string(certId); |
| 916 | getCertificateProperties(asyncResp, objectPath, |
| 917 | certs::httpsServiceName, certId, |
| 918 | certURL, "HTTPS Certificate"); |
| 919 | BMCWEB_LOG_DEBUG << "HTTPS certificate install file=" |
| 920 | << certFile->getCertFilePath(); |
| 921 | }, |
| 922 | certs::httpsServiceName, certs::httpsObjectPath, |
| 923 | certs::certInstallIntf, "Install", certFile->getCertFilePath()); |
| 924 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 925 | } // requestRoutesHTTPSCertificateCollection |
| 926 | |
| 927 | /** |
| 928 | * @brief Retrieve the certificates installed list and append to the |
| 929 | * response |
| 930 | * |
| 931 | * @param[in] asyncResp Shared pointer to the response message |
| 932 | * @param[in] certURL Path of the certificate object |
| 933 | * @param[in] path Path of the D-Bus service object |
| 934 | * @return None |
| 935 | */ |
Ed Tanous | 4f48d5f | 2021-06-21 08:27:45 -0700 | [diff] [blame] | 936 | inline void |
| 937 | getCertificateLocations(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 938 | const std::string& certURL, const std::string& path, |
| 939 | const std::string& service) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 940 | { |
| 941 | BMCWEB_LOG_DEBUG << "getCertificateLocations URI=" << certURL |
| 942 | << " Path=" << path << " service= " << service; |
| 943 | crow::connections::systemBus->async_method_call( |
| 944 | [asyncResp, certURL](const boost::system::error_code ec, |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 945 | const dbus::utility::ManagedObjectType& certs) { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 946 | if (ec) |
| 947 | { |
| 948 | BMCWEB_LOG_WARNING |
| 949 | << "Certificate collection query failed: " << ec |
| 950 | << ", skipping " << certURL; |
| 951 | return; |
| 952 | } |
| 953 | nlohmann::json& links = |
| 954 | asyncResp->res.jsonValue["Links"]["Certificates"]; |
Ed Tanous | 9eb808c | 2022-01-25 10:19:23 -0800 | [diff] [blame] | 955 | for (const auto& cert : certs) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 956 | { |
| 957 | long id = getIDFromURL(cert.first.str); |
| 958 | if (id >= 0) |
Zbigniew Kurzynski | 656ec7e | 2019-09-30 18:43:28 +0200 | [diff] [blame] | 959 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 960 | links.push_back( |
| 961 | {{"@odata.id", certURL + std::to_string(id)}}); |
Zbigniew Kurzynski | 656ec7e | 2019-09-30 18:43:28 +0200 | [diff] [blame] | 962 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 963 | } |
| 964 | asyncResp->res.jsonValue["Links"]["Certificates@odata.count"] = |
| 965 | links.size(); |
| 966 | }, |
| 967 | service, path, certs::dbusObjManagerIntf, "GetManagedObjects"); |
| 968 | } |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 969 | |
| 970 | /** |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 971 | * The certificate location schema defines a resource that an administrator |
| 972 | * can use in order to locate all certificates installed on a given service. |
| 973 | */ |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 974 | inline void requestRoutesCertificateLocations(App& app) |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 975 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 976 | BMCWEB_ROUTE(app, "/redfish/v1/CertificateService/CertificateLocations/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 977 | .privileges(redfish::privileges::getCertificateLocations) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 978 | .methods( |
| 979 | boost::beast::http::verb:: |
| 980 | get)([](const crow::Request&, |
| 981 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 982 | asyncResp->res.jsonValue = { |
| 983 | {"@odata.id", |
| 984 | "/redfish/v1/CertificateService/CertificateLocations"}, |
| 985 | {"@odata.type", |
| 986 | "#CertificateLocations.v1_0_0.CertificateLocations"}, |
| 987 | {"Name", "Certificate Locations"}, |
| 988 | {"Id", "CertificateLocations"}, |
| 989 | {"Description", |
| 990 | "Defines a resource that an administrator can use in order to " |
| 991 | "locate all certificates installed on a given service"}}; |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 992 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 993 | nlohmann::json& links = |
| 994 | asyncResp->res.jsonValue["Links"]["Certificates"]; |
| 995 | links = nlohmann::json::array(); |
| 996 | getCertificateLocations( |
| 997 | asyncResp, |
| 998 | "/redfish/v1/Managers/bmc/NetworkProtocol/HTTPS/Certificates/", |
| 999 | certs::httpsObjectPath, certs::httpsServiceName); |
| 1000 | getCertificateLocations( |
| 1001 | asyncResp, "/redfish/v1/AccountService/LDAP/Certificates/", |
| 1002 | certs::ldapObjectPath, certs::ldapServiceName); |
| 1003 | getCertificateLocations( |
| 1004 | asyncResp, "/redfish/v1/Managers/bmc/Truststore/Certificates/", |
| 1005 | certs::authorityObjectPath, certs::authorityServiceName); |
| 1006 | }); |
| 1007 | } |
| 1008 | // requestRoutesCertificateLocations |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 1009 | |
| 1010 | /** |
| 1011 | * Collection of LDAP certificates |
| 1012 | */ |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1013 | inline void requestRoutesLDAPCertificateCollection(App& app) |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 1014 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1015 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/LDAP/Certificates/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1016 | .privileges(redfish::privileges::getCertificateCollection) |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1017 | .methods( |
| 1018 | boost::beast::http::verb:: |
| 1019 | get)([](const crow::Request&, |
| 1020 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 1021 | asyncResp->res.jsonValue = { |
| 1022 | {"@odata.id", "/redfish/v1/AccountService/LDAP/Certificates"}, |
| 1023 | {"@odata.type", "#CertificateCollection.CertificateCollection"}, |
| 1024 | {"Name", "LDAP Certificates Collection"}, |
| 1025 | {"Description", "A Collection of LDAP certificate instances"}}; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1026 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1027 | crow::connections::systemBus->async_method_call( |
| 1028 | [asyncResp](const boost::system::error_code ec, |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 1029 | const dbus::utility::ManagedObjectType& certs) { |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1030 | nlohmann::json& members = |
| 1031 | asyncResp->res.jsonValue["Members"]; |
| 1032 | nlohmann::json& count = |
| 1033 | asyncResp->res.jsonValue["Members@odata.count"]; |
| 1034 | members = nlohmann::json::array(); |
| 1035 | count = 0; |
| 1036 | if (ec) |
| 1037 | { |
| 1038 | BMCWEB_LOG_WARNING << "LDAP certificate query failed: " |
| 1039 | << ec; |
| 1040 | return; |
| 1041 | } |
| 1042 | for (const auto& cert : certs) |
| 1043 | { |
| 1044 | long id = getIDFromURL(cert.first.str); |
| 1045 | if (id >= 0) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1046 | { |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1047 | members.push_back( |
| 1048 | {{"@odata.id", |
| 1049 | "/redfish/v1/AccountService/LDAP/Certificates/" + |
| 1050 | std::to_string(id)}}); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1051 | } |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1052 | } |
| 1053 | count = members.size(); |
| 1054 | }, |
| 1055 | certs::ldapServiceName, certs::ldapObjectPath, |
| 1056 | certs::dbusObjManagerIntf, "GetManagedObjects"); |
| 1057 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1058 | |
| 1059 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/LDAP/Certificates/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1060 | .privileges(redfish::privileges::postCertificateCollection) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1061 | .methods(boost::beast::http::verb::post)( |
| 1062 | [](const crow::Request& req, |
| 1063 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 1064 | std::string certFileBody = |
| 1065 | getCertificateFromReqBody(asyncResp, req); |
| 1066 | |
| 1067 | if (certFileBody.empty()) |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 1068 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1069 | BMCWEB_LOG_ERROR |
| 1070 | << "Cannot get certificate from request body."; |
| 1071 | messages::unrecognizedRequestBody(asyncResp->res); |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 1072 | return; |
| 1073 | } |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 1074 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1075 | std::shared_ptr<CertificateFile> certFile = |
| 1076 | std::make_shared<CertificateFile>(certFileBody); |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1077 | |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1078 | crow::connections::systemBus->async_method_call( |
| 1079 | [asyncResp, certFile](const boost::system::error_code ec, |
| 1080 | const std::string& objectPath) { |
| 1081 | if (ec) |
| 1082 | { |
| 1083 | BMCWEB_LOG_ERROR << "DBUS response error: " << ec; |
| 1084 | messages::internalError(asyncResp->res); |
| 1085 | return; |
| 1086 | } |
| 1087 | long certId = getIDFromURL(objectPath); |
| 1088 | if (certId < 0) |
| 1089 | { |
| 1090 | BMCWEB_LOG_ERROR << "Invalid objectPath value" |
| 1091 | << objectPath; |
| 1092 | messages::internalError(asyncResp->res); |
| 1093 | return; |
| 1094 | } |
| 1095 | std::string certURL = |
| 1096 | "/redfish/v1/AccountService/LDAP/Certificates/" + |
| 1097 | std::to_string(certId); |
| 1098 | getCertificateProperties(asyncResp, objectPath, |
| 1099 | certs::ldapServiceName, certId, |
| 1100 | certURL, "LDAP Certificate"); |
| 1101 | BMCWEB_LOG_DEBUG << "LDAP certificate install file=" |
| 1102 | << certFile->getCertFilePath(); |
| 1103 | }, |
| 1104 | certs::ldapServiceName, certs::ldapObjectPath, |
| 1105 | certs::certInstallIntf, "Install", |
| 1106 | certFile->getCertFilePath()); |
| 1107 | }); |
| 1108 | } // requestRoutesLDAPCertificateCollection |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 1109 | |
| 1110 | /** |
| 1111 | * Certificate resource describes a certificate used to prove the identity |
| 1112 | * of a component, account or service. |
| 1113 | */ |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1114 | inline void requestRoutesLDAPCertificate(App& app) |
Marri Devender Rao | 37cce91 | 2019-02-20 01:05:22 -0600 | [diff] [blame] | 1115 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1116 | BMCWEB_ROUTE(app, "/redfish/v1/AccountService/LDAP/Certificates/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1117 | .privileges(redfish::privileges::getCertificate) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1118 | .methods(boost::beast::http::verb::get)( |
| 1119 | [](const crow::Request& req, |
| 1120 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1121 | const std::string&) { |
| 1122 | long id = getIDFromURL(req.url); |
| 1123 | if (id < 0) |
| 1124 | { |
| 1125 | BMCWEB_LOG_ERROR << "Invalid url value" << req.url; |
| 1126 | messages::internalError(asyncResp->res); |
| 1127 | return; |
| 1128 | } |
| 1129 | BMCWEB_LOG_DEBUG << "LDAP Certificate ID=" |
| 1130 | << std::to_string(id); |
| 1131 | std::string certURL = |
| 1132 | "/redfish/v1/AccountService/LDAP/Certificates/" + |
| 1133 | std::to_string(id); |
| 1134 | std::string objectPath = certs::ldapObjectPath; |
| 1135 | objectPath += "/"; |
| 1136 | objectPath += std::to_string(id); |
| 1137 | getCertificateProperties(asyncResp, objectPath, |
| 1138 | certs::ldapServiceName, id, certURL, |
| 1139 | "LDAP Certificate"); |
| 1140 | }); |
| 1141 | } // requestRoutesLDAPCertificate |
Marri Devender Rao | cfcd5f6 | 2019-05-17 08:34:37 -0500 | [diff] [blame] | 1142 | /** |
| 1143 | * Collection of TrustStoreCertificate certificates |
| 1144 | */ |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1145 | inline void requestRoutesTrustStoreCertificateCollection(App& app) |
Marri Devender Rao | cfcd5f6 | 2019-05-17 08:34:37 -0500 | [diff] [blame] | 1146 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1147 | BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Truststore/Certificates/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1148 | .privileges(redfish::privileges::getCertificate) |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1149 | .methods( |
| 1150 | boost::beast::http::verb:: |
| 1151 | get)([](const crow::Request&, |
| 1152 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 1153 | asyncResp->res.jsonValue = { |
| 1154 | {"@odata.id", |
| 1155 | "/redfish/v1/Managers/bmc/Truststore/Certificates/"}, |
| 1156 | {"@odata.type", "#CertificateCollection.CertificateCollection"}, |
| 1157 | {"Name", "TrustStore Certificates Collection"}, |
| 1158 | {"Description", |
| 1159 | "A Collection of TrustStore certificate instances"}}; |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1160 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1161 | crow::connections::systemBus->async_method_call( |
| 1162 | [asyncResp](const boost::system::error_code ec, |
Ed Tanous | 711ac7a | 2021-12-20 09:34:41 -0800 | [diff] [blame] | 1163 | const dbus::utility::ManagedObjectType& certs) { |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1164 | if (ec) |
| 1165 | { |
| 1166 | BMCWEB_LOG_ERROR << "DBUS response error: " << ec; |
| 1167 | messages::internalError(asyncResp->res); |
| 1168 | return; |
| 1169 | } |
| 1170 | nlohmann::json& members = |
| 1171 | asyncResp->res.jsonValue["Members"]; |
| 1172 | members = nlohmann::json::array(); |
| 1173 | for (const auto& cert : certs) |
| 1174 | { |
| 1175 | long id = getIDFromURL(cert.first.str); |
| 1176 | if (id >= 0) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1177 | { |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1178 | members.push_back( |
| 1179 | {{"@odata.id", |
| 1180 | "/redfish/v1/Managers/bmc/Truststore/Certificates/" + |
| 1181 | std::to_string(id)}}); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1182 | } |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1183 | } |
| 1184 | asyncResp->res.jsonValue["Members@odata.count"] = |
| 1185 | members.size(); |
| 1186 | }, |
| 1187 | certs::authorityServiceName, certs::authorityObjectPath, |
| 1188 | certs::dbusObjManagerIntf, "GetManagedObjects"); |
| 1189 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1190 | |
| 1191 | BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Truststore/Certificates/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1192 | .privileges(redfish::privileges::postCertificateCollection) |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1193 | .methods( |
| 1194 | boost::beast::http::verb:: |
| 1195 | post)([](const crow::Request& req, |
| 1196 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) { |
| 1197 | std::string certFileBody = |
| 1198 | getCertificateFromReqBody(asyncResp, req); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1199 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1200 | if (certFileBody.empty()) |
| 1201 | { |
| 1202 | BMCWEB_LOG_ERROR << "Cannot get certificate from request body."; |
| 1203 | messages::unrecognizedRequestBody(asyncResp->res); |
| 1204 | return; |
| 1205 | } |
Marri Devender Rao | cfcd5f6 | 2019-05-17 08:34:37 -0500 | [diff] [blame] | 1206 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1207 | std::shared_ptr<CertificateFile> certFile = |
| 1208 | std::make_shared<CertificateFile>(certFileBody); |
| 1209 | crow::connections::systemBus->async_method_call( |
| 1210 | [asyncResp, certFile](const boost::system::error_code ec, |
| 1211 | const std::string& objectPath) { |
| 1212 | if (ec) |
| 1213 | { |
| 1214 | BMCWEB_LOG_ERROR << "DBUS response error: " << ec; |
| 1215 | messages::internalError(asyncResp->res); |
| 1216 | return; |
| 1217 | } |
| 1218 | long certId = getIDFromURL(objectPath); |
| 1219 | if (certId < 0) |
| 1220 | { |
| 1221 | BMCWEB_LOG_ERROR << "Invalid objectPath value" |
| 1222 | << objectPath; |
| 1223 | messages::internalError(asyncResp->res); |
| 1224 | return; |
| 1225 | } |
| 1226 | std::string certURL = |
| 1227 | "/redfish/v1/Managers/bmc/Truststore/Certificates/" + |
| 1228 | std::to_string(certId); |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 1229 | |
George Liu | 0fda0f1 | 2021-11-16 10:06:17 +0800 | [diff] [blame] | 1230 | getCertificateProperties( |
| 1231 | asyncResp, objectPath, certs::authorityServiceName, |
| 1232 | certId, certURL, "TrustStore Certificate"); |
| 1233 | BMCWEB_LOG_DEBUG << "TrustStore certificate install file=" |
| 1234 | << certFile->getCertFilePath(); |
| 1235 | }, |
| 1236 | certs::authorityServiceName, certs::authorityObjectPath, |
| 1237 | certs::certInstallIntf, "Install", certFile->getCertFilePath()); |
| 1238 | }); |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1239 | } // requestRoutesTrustStoreCertificateCollection |
Marri Devender Rao | cfcd5f6 | 2019-05-17 08:34:37 -0500 | [diff] [blame] | 1240 | |
| 1241 | /** |
| 1242 | * Certificate resource describes a certificate used to prove the identity |
| 1243 | * of a component, account or service. |
| 1244 | */ |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1245 | inline void requestRoutesTrustStoreCertificate(App& app) |
Marri Devender Rao | cfcd5f6 | 2019-05-17 08:34:37 -0500 | [diff] [blame] | 1246 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1247 | BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Truststore/Certificates/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1248 | .privileges(redfish::privileges::getCertificate) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1249 | .methods(boost::beast::http::verb::get)( |
| 1250 | [](const crow::Request& req, |
| 1251 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1252 | const std::string&) { |
| 1253 | long id = getIDFromURL(req.url); |
| 1254 | if (id < 0) |
Zbigniew Kurzynski | 07a6029 | 2019-09-17 15:56:16 +0200 | [diff] [blame] | 1255 | { |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1256 | BMCWEB_LOG_ERROR << "Invalid url value" << req.url; |
| 1257 | messages::internalError(asyncResp->res); |
Zbigniew Kurzynski | 07a6029 | 2019-09-17 15:56:16 +0200 | [diff] [blame] | 1258 | return; |
| 1259 | } |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1260 | BMCWEB_LOG_DEBUG << "TrustStoreCertificate::doGet ID=" |
| 1261 | << std::to_string(id); |
| 1262 | std::string certURL = |
| 1263 | "/redfish/v1/Managers/bmc/Truststore/Certificates/" + |
| 1264 | std::to_string(id); |
| 1265 | std::string objectPath = certs::authorityObjectPath; |
| 1266 | objectPath += "/"; |
| 1267 | objectPath += std::to_string(id); |
| 1268 | getCertificateProperties(asyncResp, objectPath, |
| 1269 | certs::authorityServiceName, id, |
| 1270 | certURL, "TrustStore Certificate"); |
| 1271 | }); |
| 1272 | |
| 1273 | BMCWEB_ROUTE(app, "/redfish/v1/Managers/bmc/Truststore/Certificates/<str>/") |
Ed Tanous | ed39821 | 2021-06-09 17:05:54 -0700 | [diff] [blame] | 1274 | .privileges(redfish::privileges::deleteCertificate) |
John Edward Broadbent | 7e860f1 | 2021-04-08 15:57:16 -0700 | [diff] [blame] | 1275 | .methods(boost::beast::http::verb::delete_)( |
| 1276 | [](const crow::Request& req, |
| 1277 | const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, |
| 1278 | const std::string& param) { |
| 1279 | if (param.empty()) |
| 1280 | { |
| 1281 | messages::internalError(asyncResp->res); |
| 1282 | return; |
| 1283 | } |
| 1284 | |
| 1285 | long id = getIDFromURL(req.url); |
| 1286 | if (id < 0) |
| 1287 | { |
| 1288 | BMCWEB_LOG_ERROR << "Invalid url value: " << req.url; |
| 1289 | messages::resourceNotFound(asyncResp->res, |
| 1290 | "TrustStore Certificate", |
| 1291 | std::string(req.url)); |
| 1292 | return; |
| 1293 | } |
| 1294 | BMCWEB_LOG_DEBUG << "TrustStoreCertificate::doDelete ID=" |
| 1295 | << std::to_string(id); |
| 1296 | std::string certPath = certs::authorityObjectPath; |
| 1297 | certPath += "/"; |
| 1298 | certPath += std::to_string(id); |
| 1299 | |
| 1300 | crow::connections::systemBus->async_method_call( |
| 1301 | [asyncResp, id](const boost::system::error_code ec) { |
| 1302 | if (ec) |
| 1303 | { |
| 1304 | messages::resourceNotFound(asyncResp->res, |
| 1305 | "TrustStore Certificate", |
| 1306 | std::to_string(id)); |
| 1307 | return; |
| 1308 | } |
| 1309 | BMCWEB_LOG_INFO << "Certificate deleted"; |
| 1310 | asyncResp->res.result( |
| 1311 | boost::beast::http::status::no_content); |
| 1312 | }, |
| 1313 | certs::authorityServiceName, certPath, certs::objDeleteIntf, |
| 1314 | "Delete"); |
| 1315 | }); |
| 1316 | } // requestRoutesTrustStoreCertificate |
Marri Devender Rao | 5968cae | 2019-01-21 10:27:12 -0600 | [diff] [blame] | 1317 | } // namespace redfish |