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