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