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