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