140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0 240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors 35968caeeSMarri Devender Rao #pragma once 45968caeeSMarri Devender Rao 5d7857201SEd Tanous #include "bmcweb_config.h" 6d7857201SEd Tanous 73ccb3adbSEd Tanous #include "app.hpp" 83ccb3adbSEd Tanous #include "async_resp.hpp" 9d7857201SEd Tanous #include "dbus_singleton.hpp" 107a1dbc48SGeorge Liu #include "dbus_utility.hpp" 11d7857201SEd Tanous #include "error_messages.hpp" 121aa0c2b8SEd Tanous #include "http/parsing.hpp" 13d7857201SEd Tanous #include "http_request.hpp" 143ccb3adbSEd Tanous #include "http_response.hpp" 15d98a2f93SEd Tanous #include "io_context_singleton.hpp" 16d7857201SEd Tanous #include "logging.hpp" 17d7857201SEd Tanous #include "privileges.hpp" 183ccb3adbSEd Tanous #include "query.hpp" 193ccb3adbSEd Tanous #include "registries/privilege_registry.hpp" 20d7857201SEd Tanous #include "utility.hpp" 219b12d1f9SKrzysztof Grobelny #include "utils/dbus_utils.hpp" 223ccb3adbSEd Tanous #include "utils/json_utils.hpp" 233ccb3adbSEd Tanous #include "utils/time_utils.hpp" 249b12d1f9SKrzysztof Grobelny 25d7857201SEd Tanous #include <systemd/sd-bus.h> 26d7857201SEd Tanous 27d7857201SEd Tanous #include <boost/asio/error.hpp> 28d7857201SEd Tanous #include <boost/asio/steady_timer.hpp> 29d7857201SEd Tanous #include <boost/beast/http/field.hpp> 30d7857201SEd Tanous #include <boost/beast/http/status.hpp> 31d7857201SEd Tanous #include <boost/beast/http/verb.hpp> 32d7857201SEd Tanous #include <boost/system/result.hpp> 33ef4c65b7SEd Tanous #include <boost/url/format.hpp> 34d7857201SEd Tanous #include <boost/url/parse.hpp> 35d7857201SEd Tanous #include <boost/url/url.hpp> 36d7857201SEd Tanous #include <nlohmann/json.hpp> 373ccb3adbSEd Tanous #include <sdbusplus/bus/match.hpp> 38d7857201SEd Tanous #include <sdbusplus/message.hpp> 39d7857201SEd Tanous #include <sdbusplus/message/native_types.hpp> 409b12d1f9SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp> 411214b7e7SGunnar Mills 427a1dbc48SGeorge Liu #include <array> 43d7857201SEd Tanous #include <chrono> 44d7857201SEd Tanous #include <cstddef> 45d7857201SEd Tanous #include <cstdint> 46d7857201SEd Tanous #include <cstdlib> 47d7857201SEd Tanous #include <filesystem> 48d7857201SEd Tanous #include <format> 49d7857201SEd Tanous #include <fstream> 50d7857201SEd Tanous #include <functional> 51d7857201SEd Tanous #include <iterator> 523ccb3adbSEd Tanous #include <memory> 53d7857201SEd Tanous #include <optional> 54d7857201SEd Tanous #include <string> 557a1dbc48SGeorge Liu #include <string_view> 56d7857201SEd Tanous #include <system_error> 57d7857201SEd Tanous #include <utility> 58d7857201SEd Tanous #include <vector> 597a1dbc48SGeorge Liu 605968caeeSMarri Devender Rao namespace redfish 615968caeeSMarri Devender Rao { 625968caeeSMarri Devender Rao namespace certs 635968caeeSMarri Devender Rao { 6489492a15SPatrick Williams constexpr const char* certInstallIntf = "xyz.openbmc_project.Certs.Install"; 6589492a15SPatrick Williams constexpr const char* certReplaceIntf = "xyz.openbmc_project.Certs.Replace"; 6689492a15SPatrick Williams constexpr const char* objDeleteIntf = "xyz.openbmc_project.Object.Delete"; 6789492a15SPatrick Williams constexpr const char* certPropIntf = "xyz.openbmc_project.Certs.Certificate"; 6889492a15SPatrick Williams constexpr const char* httpsServiceName = 6937cce918SMarri Devender Rao "xyz.openbmc_project.Certs.Manager.Server.Https"; 7089492a15SPatrick Williams constexpr const char* ldapServiceName = 7137cce918SMarri Devender Rao "xyz.openbmc_project.Certs.Manager.Client.Ldap"; 7289492a15SPatrick Williams constexpr const char* authorityServiceName = 73b2254ccdSMichal Orzel "xyz.openbmc_project.Certs.Manager.Authority.Truststore"; 7489492a15SPatrick Williams constexpr const char* baseObjectPath = "/xyz/openbmc_project/certs"; 7589492a15SPatrick Williams constexpr const char* httpsObjectPath = 76c6a8dfb1SJiaqing Zhao "/xyz/openbmc_project/certs/server/https"; 7789492a15SPatrick Williams constexpr const char* ldapObjectPath = "/xyz/openbmc_project/certs/client/ldap"; 7889492a15SPatrick Williams constexpr const char* authorityObjectPath = 79b2254ccdSMichal Orzel "/xyz/openbmc_project/certs/authority/truststore"; 805968caeeSMarri Devender Rao } // namespace certs 815968caeeSMarri Devender Rao 825968caeeSMarri Devender Rao /** 835968caeeSMarri Devender Rao * The Certificate schema defines a Certificate Service which represents the 845968caeeSMarri Devender Rao * actions available to manage certificates and links to where certificates 855968caeeSMarri Devender Rao * are installed. 865968caeeSMarri Devender Rao */ 877e860f15SJohn Edward Broadbent 888d1b46d7Szhanghch05 inline std::string getCertificateFromReqBody( 898d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 9058eb238fSKowalski, Kamil const crow::Request& req) 9158eb238fSKowalski, Kamil { 921aa0c2b8SEd Tanous nlohmann::json reqJson; 931aa0c2b8SEd Tanous JsonParseResult ret = parseRequestAsJson(req, reqJson); 941aa0c2b8SEd Tanous if (ret != JsonParseResult::Success) 9558eb238fSKowalski, Kamil { 9658eb238fSKowalski, Kamil // We did not receive JSON request, proceed as it is RAW data 9733c6b580SEd Tanous return req.body(); 9858eb238fSKowalski, Kamil } 9958eb238fSKowalski, Kamil 10058eb238fSKowalski, Kamil std::string certificate; 10158eb238fSKowalski, Kamil std::optional<std::string> certificateType = "PEM"; 10258eb238fSKowalski, Kamil 103afc474aeSMyung Bae if (!json_util::readJsonPatch( // 104afc474aeSMyung Bae req, asyncResp->res, // 105afc474aeSMyung Bae "CertificateString", certificate, // 106afc474aeSMyung Bae "CertificateType", certificateType // 107afc474aeSMyung Bae )) 10858eb238fSKowalski, Kamil { 10962598e31SEd Tanous BMCWEB_LOG_ERROR("Required parameters are missing"); 11058eb238fSKowalski, Kamil messages::internalError(asyncResp->res); 111abb93cddSEd Tanous return {}; 11258eb238fSKowalski, Kamil } 11358eb238fSKowalski, Kamil 11458eb238fSKowalski, Kamil if (*certificateType != "PEM") 11558eb238fSKowalski, Kamil { 11658eb238fSKowalski, Kamil messages::propertyValueNotInList(asyncResp->res, *certificateType, 11758eb238fSKowalski, Kamil "CertificateType"); 118abb93cddSEd Tanous return {}; 11958eb238fSKowalski, Kamil } 12058eb238fSKowalski, Kamil 12158eb238fSKowalski, Kamil return certificate; 12258eb238fSKowalski, Kamil } 12358eb238fSKowalski, Kamil 1245968caeeSMarri Devender Rao /** 1255968caeeSMarri Devender Rao * Class to create a temporary certificate file for uploading to system 1265968caeeSMarri Devender Rao */ 1275968caeeSMarri Devender Rao class CertificateFile 1285968caeeSMarri Devender Rao { 1295968caeeSMarri Devender Rao public: 1305968caeeSMarri Devender Rao CertificateFile() = delete; 1315968caeeSMarri Devender Rao CertificateFile(const CertificateFile&) = delete; 1325968caeeSMarri Devender Rao CertificateFile& operator=(const CertificateFile&) = delete; 1335968caeeSMarri Devender Rao CertificateFile(CertificateFile&&) = delete; 1345968caeeSMarri Devender Rao CertificateFile& operator=(CertificateFile&&) = delete; 1354e23a444SEd Tanous explicit CertificateFile(const std::string& certString) 1365968caeeSMarri Devender Rao { 13772d52d25SEd Tanous std::array<char, 18> dirTemplate = {'/', 't', 'm', 'p', '/', 'C', 1385207438cSEd Tanous 'e', 'r', 't', 's', '.', 'X', 1395207438cSEd Tanous 'X', 'X', 'X', 'X', 'X', '\0'}; 14092e11bf8SMyung Bae // NOLINTNEXTLINE(misc-include-cleaner) 1415207438cSEd Tanous char* tempDirectory = mkdtemp(dirTemplate.data()); 142e662eae8SEd Tanous if (tempDirectory != nullptr) 1435968caeeSMarri Devender Rao { 1445968caeeSMarri Devender Rao certDirectory = tempDirectory; 1455968caeeSMarri Devender Rao certificateFile = certDirectory / "cert.pem"; 146bd79bce8SPatrick Williams std::ofstream out(certificateFile, 147bd79bce8SPatrick Williams std::ofstream::out | std::ofstream::binary | 1485968caeeSMarri Devender Rao std::ofstream::trunc); 1495968caeeSMarri Devender Rao out << certString; 1505968caeeSMarri Devender Rao out.close(); 15162598e31SEd Tanous BMCWEB_LOG_DEBUG("Creating certificate file{}", 15262598e31SEd Tanous certificateFile.string()); 1535968caeeSMarri Devender Rao } 1545968caeeSMarri Devender Rao } 1555968caeeSMarri Devender Rao ~CertificateFile() 1565968caeeSMarri Devender Rao { 1575968caeeSMarri Devender Rao if (std::filesystem::exists(certDirectory)) 1585968caeeSMarri Devender Rao { 15962598e31SEd Tanous BMCWEB_LOG_DEBUG("Removing certificate file{}", 16062598e31SEd Tanous certificateFile.string()); 16123a21a1cSEd Tanous std::error_code ec; 16223a21a1cSEd Tanous std::filesystem::remove_all(certDirectory, ec); 16323a21a1cSEd Tanous if (ec) 1645968caeeSMarri Devender Rao { 16562598e31SEd Tanous BMCWEB_LOG_ERROR("Failed to remove temp directory{}", 16662598e31SEd Tanous certDirectory.string()); 1675968caeeSMarri Devender Rao } 1685968caeeSMarri Devender Rao } 1695968caeeSMarri Devender Rao } 1705968caeeSMarri Devender Rao std::string getCertFilePath() 1715968caeeSMarri Devender Rao { 1725968caeeSMarri Devender Rao return certificateFile; 1735968caeeSMarri Devender Rao } 1745968caeeSMarri Devender Rao 1755968caeeSMarri Devender Rao private: 1765968caeeSMarri Devender Rao std::filesystem::path certificateFile; 1775968caeeSMarri Devender Rao std::filesystem::path certDirectory; 1785968caeeSMarri Devender Rao }; 1795968caeeSMarri Devender Rao 1805968caeeSMarri Devender Rao /** 1814e0453b1SGunnar Mills * @brief Parse and update Certificate Issue/Subject property 1825968caeeSMarri Devender Rao * 1835968caeeSMarri Devender Rao * @param[in] asyncResp Shared pointer to the response message 1845968caeeSMarri Devender Rao * @param[in] str Issuer/Subject value in key=value pairs 1855968caeeSMarri Devender Rao * @param[in] type Issuer/Subject 1865968caeeSMarri Devender Rao * @return None 1875968caeeSMarri Devender Rao */ 1884ff0f1f4SEd Tanous inline void updateCertIssuerOrSubject(nlohmann::json& out, 18926ccae32SEd Tanous std::string_view value) 1905968caeeSMarri Devender Rao { 1915968caeeSMarri Devender Rao // example: O=openbmc-project.xyz,CN=localhost 1925968caeeSMarri Devender Rao std::string_view::iterator i = value.begin(); 1935968caeeSMarri Devender Rao while (i != value.end()) 1945968caeeSMarri Devender Rao { 1955968caeeSMarri Devender Rao std::string_view::iterator tokenBegin = i; 1965968caeeSMarri Devender Rao while (i != value.end() && *i != '=') 1975968caeeSMarri Devender Rao { 1986da47babSPatrick Williams std::advance(i, 1); 1995968caeeSMarri Devender Rao } 2005968caeeSMarri Devender Rao if (i == value.end()) 2015968caeeSMarri Devender Rao { 2025968caeeSMarri Devender Rao break; 2035968caeeSMarri Devender Rao } 20426ccae32SEd Tanous std::string_view key(tokenBegin, static_cast<size_t>(i - tokenBegin)); 2056da47babSPatrick Williams std::advance(i, 1); 2065968caeeSMarri Devender Rao tokenBegin = i; 2075968caeeSMarri Devender Rao while (i != value.end() && *i != ',') 2085968caeeSMarri Devender Rao { 2096da47babSPatrick Williams std::advance(i, 1); 2105968caeeSMarri Devender Rao } 21126ccae32SEd Tanous std::string_view val(tokenBegin, static_cast<size_t>(i - tokenBegin)); 2125968caeeSMarri Devender Rao if (key == "L") 2135968caeeSMarri Devender Rao { 2145968caeeSMarri Devender Rao out["City"] = val; 2155968caeeSMarri Devender Rao } 2165968caeeSMarri Devender Rao else if (key == "CN") 2175968caeeSMarri Devender Rao { 2185968caeeSMarri Devender Rao out["CommonName"] = val; 2195968caeeSMarri Devender Rao } 2205968caeeSMarri Devender Rao else if (key == "C") 2215968caeeSMarri Devender Rao { 2225968caeeSMarri Devender Rao out["Country"] = val; 2235968caeeSMarri Devender Rao } 2245968caeeSMarri Devender Rao else if (key == "O") 2255968caeeSMarri Devender Rao { 2265968caeeSMarri Devender Rao out["Organization"] = val; 2275968caeeSMarri Devender Rao } 2285968caeeSMarri Devender Rao else if (key == "OU") 2295968caeeSMarri Devender Rao { 2305968caeeSMarri Devender Rao out["OrganizationalUnit"] = val; 2315968caeeSMarri Devender Rao } 2325968caeeSMarri Devender Rao else if (key == "ST") 2335968caeeSMarri Devender Rao { 2345968caeeSMarri Devender Rao out["State"] = val; 2355968caeeSMarri Devender Rao } 2365968caeeSMarri Devender Rao // skip comma character 2375968caeeSMarri Devender Rao if (i != value.end()) 2385968caeeSMarri Devender Rao { 2396da47babSPatrick Williams std::advance(i, 1); 2405968caeeSMarri Devender Rao } 2415968caeeSMarri Devender Rao } 2425968caeeSMarri Devender Rao } 2435968caeeSMarri Devender Rao 2445968caeeSMarri Devender Rao /** 245d3f92ce7SJiaqing Zhao * @brief Retrieve the installed certificate list 246d3f92ce7SJiaqing Zhao * 247d3f92ce7SJiaqing Zhao * @param[in] asyncResp Shared pointer to the response message 248d3f92ce7SJiaqing Zhao * @param[in] basePath DBus object path to search 249d3f92ce7SJiaqing Zhao * @param[in] listPtr Json pointer to the list in asyncResp 250d3f92ce7SJiaqing Zhao * @param[in] countPtr Json pointer to the count in asyncResp 251d3f92ce7SJiaqing Zhao * @return None 252d3f92ce7SJiaqing Zhao */ 2534ff0f1f4SEd Tanous inline void getCertificateList( 254bd79bce8SPatrick Williams const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 255bd79bce8SPatrick Williams const std::string& basePath, const nlohmann::json::json_pointer& listPtr, 256d3f92ce7SJiaqing Zhao const nlohmann::json::json_pointer& countPtr) 257d3f92ce7SJiaqing Zhao { 2587a1dbc48SGeorge Liu constexpr std::array<std::string_view, 1> interfaces = { 2597a1dbc48SGeorge Liu certs::certPropIntf}; 2607a1dbc48SGeorge Liu dbus::utility::getSubTreePaths( 2617a1dbc48SGeorge Liu basePath, 0, interfaces, 262d3f92ce7SJiaqing Zhao [asyncResp, listPtr, countPtr]( 2637a1dbc48SGeorge Liu const boost::system::error_code& ec, 264d3f92ce7SJiaqing Zhao const dbus::utility::MapperGetSubTreePathsResponse& certPaths) { 265d3f92ce7SJiaqing Zhao if (ec) 266d3f92ce7SJiaqing Zhao { 26762598e31SEd Tanous BMCWEB_LOG_ERROR("Certificate collection query failed: {}", ec); 268d3f92ce7SJiaqing Zhao messages::internalError(asyncResp->res); 269d3f92ce7SJiaqing Zhao return; 270d3f92ce7SJiaqing Zhao } 271d3f92ce7SJiaqing Zhao 272d3f92ce7SJiaqing Zhao nlohmann::json& links = asyncResp->res.jsonValue[listPtr]; 273d3f92ce7SJiaqing Zhao links = nlohmann::json::array(); 274d3f92ce7SJiaqing Zhao for (const auto& certPath : certPaths) 275d3f92ce7SJiaqing Zhao { 276d3f92ce7SJiaqing Zhao sdbusplus::message::object_path objPath(certPath); 277d3f92ce7SJiaqing Zhao std::string certId = objPath.filename(); 278d3f92ce7SJiaqing Zhao if (certId.empty()) 279d3f92ce7SJiaqing Zhao { 280bd79bce8SPatrick Williams BMCWEB_LOG_ERROR("Invalid certificate objPath {}", 281bd79bce8SPatrick Williams certPath); 282d3f92ce7SJiaqing Zhao continue; 283d3f92ce7SJiaqing Zhao } 284d3f92ce7SJiaqing Zhao 285d3f92ce7SJiaqing Zhao boost::urls::url certURL; 286d3f92ce7SJiaqing Zhao if (objPath.parent_path() == certs::httpsObjectPath) 287d3f92ce7SJiaqing Zhao { 288ef4c65b7SEd Tanous certURL = boost::urls::format( 289253f11b8SEd Tanous "/redfish/v1/Managers/{}/NetworkProtocol/HTTPS/Certificates/{}", 290253f11b8SEd Tanous BMCWEB_REDFISH_MANAGER_URI_NAME, certId); 291d3f92ce7SJiaqing Zhao } 292d3f92ce7SJiaqing Zhao else if (objPath.parent_path() == certs::ldapObjectPath) 293d3f92ce7SJiaqing Zhao { 294ef4c65b7SEd Tanous certURL = boost::urls::format( 295bd79bce8SPatrick Williams "/redfish/v1/AccountService/LDAP/Certificates/{}", 296bd79bce8SPatrick Williams certId); 297d3f92ce7SJiaqing Zhao } 298d3f92ce7SJiaqing Zhao else if (objPath.parent_path() == certs::authorityObjectPath) 299d3f92ce7SJiaqing Zhao { 300ef4c65b7SEd Tanous certURL = boost::urls::format( 301253f11b8SEd Tanous "/redfish/v1/Managers/{}/Truststore/Certificates/{}", 302253f11b8SEd Tanous BMCWEB_REDFISH_MANAGER_URI_NAME, certId); 303d3f92ce7SJiaqing Zhao } 304d3f92ce7SJiaqing Zhao else 305d3f92ce7SJiaqing Zhao { 306d3f92ce7SJiaqing Zhao continue; 307d3f92ce7SJiaqing Zhao } 308d3f92ce7SJiaqing Zhao 309d3f92ce7SJiaqing Zhao nlohmann::json::object_t link; 310d3f92ce7SJiaqing Zhao link["@odata.id"] = certURL; 311d3f92ce7SJiaqing Zhao links.emplace_back(std::move(link)); 312d3f92ce7SJiaqing Zhao } 313d3f92ce7SJiaqing Zhao 314d3f92ce7SJiaqing Zhao asyncResp->res.jsonValue[countPtr] = links.size(); 3157a1dbc48SGeorge Liu }); 316d3f92ce7SJiaqing Zhao } 317d3f92ce7SJiaqing Zhao 318d3f92ce7SJiaqing Zhao /** 3195968caeeSMarri Devender Rao * @brief Retrieve the certificates properties and append to the response 3205968caeeSMarri Devender Rao * message 3215968caeeSMarri Devender Rao * 3225968caeeSMarri Devender Rao * @param[in] asyncResp Shared pointer to the response message 3235968caeeSMarri Devender Rao * @param[in] objectPath Path of the D-Bus service object 3245968caeeSMarri Devender Rao * @param[in] certId Id of the certificate 3255968caeeSMarri Devender Rao * @param[in] certURL URL of the certificate object 3265968caeeSMarri Devender Rao * @param[in] name name of the certificate 3275968caeeSMarri Devender Rao * @return None 3285968caeeSMarri Devender Rao */ 3294ff0f1f4SEd Tanous inline void getCertificateProperties( 3308d1b46d7Szhanghch05 const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 331e19e97e2SJiaqing Zhao const std::string& objectPath, const std::string& service, 3321e312598SJiaqing Zhao const std::string& certId, const boost::urls::url& certURL, 333e19e97e2SJiaqing Zhao const std::string& name) 3345968caeeSMarri Devender Rao { 33562598e31SEd Tanous BMCWEB_LOG_DEBUG("getCertificateProperties Path={} certId={} certURl={}", 33662598e31SEd Tanous objectPath, certId, certURL); 337deae6a78SEd Tanous dbus::utility::getAllProperties( 338deae6a78SEd Tanous service, objectPath, certs::certPropIntf, 339b9d36b47SEd Tanous [asyncResp, certURL, certId, 3405e7e2dc5SEd Tanous name](const boost::system::error_code& ec, 341b9d36b47SEd Tanous const dbus::utility::DBusPropertiesMap& properties) { 3425968caeeSMarri Devender Rao if (ec) 3435968caeeSMarri Devender Rao { 34462598e31SEd Tanous BMCWEB_LOG_ERROR("DBUS response error: {}", ec); 345bd79bce8SPatrick Williams messages::resourceNotFound(asyncResp->res, "Certificate", 346bd79bce8SPatrick Williams certId); 3475968caeeSMarri Devender Rao return; 3485968caeeSMarri Devender Rao } 3499b12d1f9SKrzysztof Grobelny 3509b12d1f9SKrzysztof Grobelny const std::string* certificateString = nullptr; 3519b12d1f9SKrzysztof Grobelny const std::vector<std::string>* keyUsage = nullptr; 3529b12d1f9SKrzysztof Grobelny const std::string* issuer = nullptr; 3539b12d1f9SKrzysztof Grobelny const std::string* subject = nullptr; 3549b12d1f9SKrzysztof Grobelny const uint64_t* validNotAfter = nullptr; 3559b12d1f9SKrzysztof Grobelny const uint64_t* validNotBefore = nullptr; 3569b12d1f9SKrzysztof Grobelny 3579b12d1f9SKrzysztof Grobelny const bool success = sdbusplus::unpackPropertiesNoThrow( 358bd79bce8SPatrick Williams dbus_utils::UnpackErrorPrinter(), properties, 359bd79bce8SPatrick Williams "CertificateString", certificateString, "KeyUsage", keyUsage, 360bd79bce8SPatrick Williams "Issuer", issuer, "Subject", subject, "ValidNotAfter", 361bd79bce8SPatrick Williams validNotAfter, "ValidNotBefore", validNotBefore); 3629b12d1f9SKrzysztof Grobelny 3639b12d1f9SKrzysztof Grobelny if (!success) 3649b12d1f9SKrzysztof Grobelny { 3659b12d1f9SKrzysztof Grobelny messages::internalError(asyncResp->res); 3669b12d1f9SKrzysztof Grobelny return; 3679b12d1f9SKrzysztof Grobelny } 3689b12d1f9SKrzysztof Grobelny 3691476687dSEd Tanous asyncResp->res.jsonValue["@odata.id"] = certURL; 3701476687dSEd Tanous asyncResp->res.jsonValue["@odata.type"] = 3711476687dSEd Tanous "#Certificate.v1_0_0.Certificate"; 372e19e97e2SJiaqing Zhao asyncResp->res.jsonValue["Id"] = certId; 3731476687dSEd Tanous asyncResp->res.jsonValue["Name"] = name; 3741476687dSEd Tanous asyncResp->res.jsonValue["Description"] = name; 3755968caeeSMarri Devender Rao asyncResp->res.jsonValue["CertificateString"] = ""; 3769b12d1f9SKrzysztof Grobelny asyncResp->res.jsonValue["KeyUsage"] = nlohmann::json::array(); 3779b12d1f9SKrzysztof Grobelny 3789b12d1f9SKrzysztof Grobelny if (certificateString != nullptr) 3795968caeeSMarri Devender Rao { 380bd79bce8SPatrick Williams asyncResp->res.jsonValue["CertificateString"] = 381bd79bce8SPatrick Williams *certificateString; 3825968caeeSMarri Devender Rao } 3839b12d1f9SKrzysztof Grobelny 3849b12d1f9SKrzysztof Grobelny if (keyUsage != nullptr) 3855968caeeSMarri Devender Rao { 3869b12d1f9SKrzysztof Grobelny asyncResp->res.jsonValue["KeyUsage"] = *keyUsage; 3875968caeeSMarri Devender Rao } 3889b12d1f9SKrzysztof Grobelny 3899b12d1f9SKrzysztof Grobelny if (issuer != nullptr) 3905968caeeSMarri Devender Rao { 3919b12d1f9SKrzysztof Grobelny updateCertIssuerOrSubject(asyncResp->res.jsonValue["Issuer"], 3929b12d1f9SKrzysztof Grobelny *issuer); 3935968caeeSMarri Devender Rao } 3949b12d1f9SKrzysztof Grobelny 3959b12d1f9SKrzysztof Grobelny if (subject != nullptr) 3965968caeeSMarri Devender Rao { 3979b12d1f9SKrzysztof Grobelny updateCertIssuerOrSubject(asyncResp->res.jsonValue["Subject"], 3989b12d1f9SKrzysztof Grobelny *subject); 3995968caeeSMarri Devender Rao } 4009b12d1f9SKrzysztof Grobelny 4019b12d1f9SKrzysztof Grobelny if (validNotAfter != nullptr) 4025968caeeSMarri Devender Rao { 4035968caeeSMarri Devender Rao asyncResp->res.jsonValue["ValidNotAfter"] = 4049b12d1f9SKrzysztof Grobelny redfish::time_utils::getDateTimeUint(*validNotAfter); 4055968caeeSMarri Devender Rao } 4069b12d1f9SKrzysztof Grobelny 4079b12d1f9SKrzysztof Grobelny if (validNotBefore != nullptr) 4085968caeeSMarri Devender Rao { 4095968caeeSMarri Devender Rao asyncResp->res.jsonValue["ValidNotBefore"] = 4109b12d1f9SKrzysztof Grobelny redfish::time_utils::getDateTimeUint(*validNotBefore); 4115968caeeSMarri Devender Rao } 4129b12d1f9SKrzysztof Grobelny 4131e312598SJiaqing Zhao asyncResp->res.addHeader( 414d9f6c621SEd Tanous boost::beast::http::field::location, 415d9f6c621SEd Tanous std::string_view(certURL.data(), certURL.size())); 4169b12d1f9SKrzysztof Grobelny }); 4175968caeeSMarri Devender Rao } 4185968caeeSMarri Devender Rao 419504af5a0SPatrick Williams inline void deleteCertificate( 420504af5a0SPatrick Williams const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 4217a3a8f7aSJiaqing Zhao const std::string& service, 4227a3a8f7aSJiaqing Zhao const sdbusplus::message::object_path& objectPath) 4237a3a8f7aSJiaqing Zhao { 424*177612aaSEd Tanous dbus::utility::async_method_call( 425*177612aaSEd Tanous asyncResp, 4267a3a8f7aSJiaqing Zhao [asyncResp, 4275e7e2dc5SEd Tanous id{objectPath.filename()}](const boost::system::error_code& ec) { 4287a3a8f7aSJiaqing Zhao if (ec) 4297a3a8f7aSJiaqing Zhao { 4307a3a8f7aSJiaqing Zhao messages::resourceNotFound(asyncResp->res, "Certificate", id); 4317a3a8f7aSJiaqing Zhao return; 4327a3a8f7aSJiaqing Zhao } 43362598e31SEd Tanous BMCWEB_LOG_INFO("Certificate deleted"); 4347a3a8f7aSJiaqing Zhao asyncResp->res.result(boost::beast::http::status::no_content); 4357a3a8f7aSJiaqing Zhao }, 4367a3a8f7aSJiaqing Zhao service, objectPath, certs::objDeleteIntf, "Delete"); 4377a3a8f7aSJiaqing Zhao } 4387a3a8f7aSJiaqing Zhao 439828252d5SJiaqing Zhao inline void handleCertificateServiceGet( 440828252d5SJiaqing Zhao App& app, const crow::Request& req, 441828252d5SJiaqing Zhao const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 4425968caeeSMarri Devender Rao { 443828252d5SJiaqing Zhao if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 444828252d5SJiaqing Zhao { 445828252d5SJiaqing Zhao return; 446828252d5SJiaqing Zhao } 447828252d5SJiaqing Zhao 4483e72c202SNinad Palsule if (req.session == nullptr) 4493e72c202SNinad Palsule { 4503e72c202SNinad Palsule messages::internalError(asyncResp->res); 4513e72c202SNinad Palsule return; 4523e72c202SNinad Palsule } 4533e72c202SNinad Palsule 454828252d5SJiaqing Zhao asyncResp->res.jsonValue["@odata.type"] = 455828252d5SJiaqing Zhao "#CertificateService.v1_0_0.CertificateService"; 456828252d5SJiaqing Zhao asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/CertificateService"; 457828252d5SJiaqing Zhao asyncResp->res.jsonValue["Id"] = "CertificateService"; 458828252d5SJiaqing Zhao asyncResp->res.jsonValue["Name"] = "Certificate Service"; 459828252d5SJiaqing Zhao asyncResp->res.jsonValue["Description"] = 460828252d5SJiaqing Zhao "Actions available to manage certificates"; 461828252d5SJiaqing Zhao // /redfish/v1/CertificateService/CertificateLocations is something 462828252d5SJiaqing Zhao // only ConfigureManager can access then only display when the user 463828252d5SJiaqing Zhao // has permissions ConfigureManager 464828252d5SJiaqing Zhao Privileges effectiveUserPrivileges = 4653e72c202SNinad Palsule redfish::getUserPrivileges(*req.session); 466828252d5SJiaqing Zhao if (isOperationAllowedWithPrivileges({{"ConfigureManager"}}, 467828252d5SJiaqing Zhao effectiveUserPrivileges)) 468828252d5SJiaqing Zhao { 469828252d5SJiaqing Zhao asyncResp->res.jsonValue["CertificateLocations"]["@odata.id"] = 470828252d5SJiaqing Zhao "/redfish/v1/CertificateService/CertificateLocations"; 471828252d5SJiaqing Zhao } 472828252d5SJiaqing Zhao nlohmann::json& actions = asyncResp->res.jsonValue["Actions"]; 473828252d5SJiaqing Zhao nlohmann::json& replace = actions["#CertificateService.ReplaceCertificate"]; 474828252d5SJiaqing Zhao replace["target"] = 475828252d5SJiaqing Zhao "/redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate"; 476828252d5SJiaqing Zhao nlohmann::json::array_t allowed; 477ad539545SPatrick Williams allowed.emplace_back("PEM"); 478828252d5SJiaqing Zhao replace["CertificateType@Redfish.AllowableValues"] = std::move(allowed); 479828252d5SJiaqing Zhao actions["#CertificateService.GenerateCSR"]["target"] = 480828252d5SJiaqing Zhao "/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR"; 481828252d5SJiaqing Zhao } 482828252d5SJiaqing Zhao 483828252d5SJiaqing Zhao inline void handleCertificateLocationsGet( 484828252d5SJiaqing Zhao App& app, const crow::Request& req, 485828252d5SJiaqing Zhao const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 486828252d5SJiaqing Zhao { 487828252d5SJiaqing Zhao if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 488828252d5SJiaqing Zhao { 489828252d5SJiaqing Zhao return; 490828252d5SJiaqing Zhao } 491828252d5SJiaqing Zhao asyncResp->res.jsonValue["@odata.id"] = 492828252d5SJiaqing Zhao "/redfish/v1/CertificateService/CertificateLocations"; 493828252d5SJiaqing Zhao asyncResp->res.jsonValue["@odata.type"] = 494828252d5SJiaqing Zhao "#CertificateLocations.v1_0_0.CertificateLocations"; 495828252d5SJiaqing Zhao asyncResp->res.jsonValue["Name"] = "Certificate Locations"; 496828252d5SJiaqing Zhao asyncResp->res.jsonValue["Id"] = "CertificateLocations"; 497828252d5SJiaqing Zhao asyncResp->res.jsonValue["Description"] = 498828252d5SJiaqing Zhao "Defines a resource that an administrator can use in order to " 499828252d5SJiaqing Zhao "locate all certificates installed on a given service"; 500828252d5SJiaqing Zhao 501828252d5SJiaqing Zhao getCertificateList(asyncResp, certs::baseObjectPath, 502828252d5SJiaqing Zhao "/Links/Certificates"_json_pointer, 503828252d5SJiaqing Zhao "/Links/Certificates@odata.count"_json_pointer); 504828252d5SJiaqing Zhao } 505828252d5SJiaqing Zhao 50626d3b0fbSChandra Harkude inline void handleError(const std::string_view dbusErrorName, 50726d3b0fbSChandra Harkude const std::string& id, const std::string& certificate, 50826d3b0fbSChandra Harkude const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 50926d3b0fbSChandra Harkude { 51026d3b0fbSChandra Harkude if (dbusErrorName == "org.freedesktop.DBus.Error.UnknownObject") 51126d3b0fbSChandra Harkude { 51226d3b0fbSChandra Harkude messages::resourceNotFound(asyncResp->res, "Certificate", id); 51326d3b0fbSChandra Harkude } 51426d3b0fbSChandra Harkude else if (dbusErrorName == 51526d3b0fbSChandra Harkude "xyz.openbmc_project.Certs.Error.InvalidCertificate") 51626d3b0fbSChandra Harkude { 51726d3b0fbSChandra Harkude messages::propertyValueIncorrect(asyncResp->res, "Certificate", 51826d3b0fbSChandra Harkude certificate); 51926d3b0fbSChandra Harkude } 52026d3b0fbSChandra Harkude else 52126d3b0fbSChandra Harkude { 52226d3b0fbSChandra Harkude messages::internalError(asyncResp->res); 52326d3b0fbSChandra Harkude } 52426d3b0fbSChandra Harkude } 52526d3b0fbSChandra Harkude 526828252d5SJiaqing Zhao inline void handleReplaceCertificateAction( 527828252d5SJiaqing Zhao App& app, const crow::Request& req, 528828252d5SJiaqing Zhao const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 529828252d5SJiaqing Zhao { 5303ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 53145ca1b86SEd Tanous { 53245ca1b86SEd Tanous return; 53345ca1b86SEd Tanous } 5345968caeeSMarri Devender Rao std::string certificate; 5357a31e336SEd Tanous std::string certURI; 5365968caeeSMarri Devender Rao std::optional<std::string> certificateType = "PEM"; 5378d1b46d7Szhanghch05 538afc474aeSMyung Bae if (!json_util::readJsonAction( // 539afc474aeSMyung Bae req, asyncResp->res, // 540afc474aeSMyung Bae "CertificateString", certificate, // 541afc474aeSMyung Bae "CertificateType", certificateType, // 542afc474aeSMyung Bae "CertificateUri/@odata.id", certURI // 543afc474aeSMyung Bae )) 5445968caeeSMarri Devender Rao { 54562598e31SEd Tanous BMCWEB_LOG_ERROR("Required parameters are missing"); 5465968caeeSMarri Devender Rao return; 5475968caeeSMarri Devender Rao } 5485968caeeSMarri Devender Rao 5495968caeeSMarri Devender Rao if (!certificateType) 5505968caeeSMarri Devender Rao { 5515968caeeSMarri Devender Rao // should never happen, but it never hurts to be paranoid. 5525968caeeSMarri Devender Rao return; 5535968caeeSMarri Devender Rao } 5545968caeeSMarri Devender Rao if (certificateType != "PEM") 5555968caeeSMarri Devender Rao { 556828252d5SJiaqing Zhao messages::actionParameterNotSupported(asyncResp->res, "CertificateType", 557828252d5SJiaqing Zhao "ReplaceCertificate"); 5585968caeeSMarri Devender Rao return; 5595968caeeSMarri Devender Rao } 5605968caeeSMarri Devender Rao 56162598e31SEd Tanous BMCWEB_LOG_INFO("Certificate URI to replace: {}", certURI); 5625968caeeSMarri Devender Rao 5636fd29553SEd Tanous boost::system::result<boost::urls::url> parsedUrl = 56475b63a2cSJiaqing Zhao boost::urls::parse_relative_ref(certURI); 56575b63a2cSJiaqing Zhao if (!parsedUrl) 5665968caeeSMarri Devender Rao { 567828252d5SJiaqing Zhao messages::actionParameterValueFormatError( 568828252d5SJiaqing Zhao asyncResp->res, certURI, "CertificateUri", "ReplaceCertificate"); 5695968caeeSMarri Devender Rao return; 5705968caeeSMarri Devender Rao } 57175b63a2cSJiaqing Zhao 57275b63a2cSJiaqing Zhao std::string id; 57375b63a2cSJiaqing Zhao sdbusplus::message::object_path objectPath; 5745968caeeSMarri Devender Rao std::string name; 57537cce918SMarri Devender Rao std::string service; 576828252d5SJiaqing Zhao if (crow::utility::readUrlSegments(*parsedUrl, "redfish", "v1", "Managers", 577828252d5SJiaqing Zhao "bmc", "NetworkProtocol", "HTTPS", 578828252d5SJiaqing Zhao "Certificates", std::ref(id))) 5795968caeeSMarri Devender Rao { 58089492a15SPatrick Williams objectPath = sdbusplus::message::object_path(certs::httpsObjectPath) / 58189492a15SPatrick Williams id; 5825968caeeSMarri Devender Rao name = "HTTPS certificate"; 58337cce918SMarri Devender Rao service = certs::httpsServiceName; 58437cce918SMarri Devender Rao } 58575b63a2cSJiaqing Zhao else if (crow::utility::readUrlSegments(*parsedUrl, "redfish", "v1", 58675b63a2cSJiaqing Zhao "AccountService", "LDAP", 58775b63a2cSJiaqing Zhao "Certificates", std::ref(id))) 58837cce918SMarri Devender Rao { 58989492a15SPatrick Williams objectPath = sdbusplus::message::object_path(certs::ldapObjectPath) / 59089492a15SPatrick Williams id; 59137cce918SMarri Devender Rao name = "LDAP certificate"; 59237cce918SMarri Devender Rao service = certs::ldapServiceName; 5935968caeeSMarri Devender Rao } 59475b63a2cSJiaqing Zhao else if (crow::utility::readUrlSegments(*parsedUrl, "redfish", "v1", 59575b63a2cSJiaqing Zhao "Managers", "bmc", "Truststore", 59675b63a2cSJiaqing Zhao "Certificates", std::ref(id))) 597cfcd5f6bSMarri Devender Rao { 59875b63a2cSJiaqing Zhao objectPath = 599828252d5SJiaqing Zhao sdbusplus::message::object_path(certs::authorityObjectPath) / id; 600cfcd5f6bSMarri Devender Rao name = "TrustStore certificate"; 601cfcd5f6bSMarri Devender Rao service = certs::authorityServiceName; 602cfcd5f6bSMarri Devender Rao } 6035968caeeSMarri Devender Rao else 6045968caeeSMarri Devender Rao { 605828252d5SJiaqing Zhao messages::actionParameterNotSupported(asyncResp->res, "CertificateUri", 606828252d5SJiaqing Zhao "ReplaceCertificate"); 6075968caeeSMarri Devender Rao return; 6085968caeeSMarri Devender Rao } 6095968caeeSMarri Devender Rao 6105968caeeSMarri Devender Rao std::shared_ptr<CertificateFile> certFile = 6115968caeeSMarri Devender Rao std::make_shared<CertificateFile>(certificate); 612*177612aaSEd Tanous dbus::utility::async_method_call( 613*177612aaSEd Tanous asyncResp, 61426d3b0fbSChandra Harkude [asyncResp, certFile, objectPath, service, url{*parsedUrl}, id, name, 61526d3b0fbSChandra Harkude certificate](const boost::system::error_code& ec, 616d3e0859cSPatrick Williams sdbusplus::message_t& m) { 6175968caeeSMarri Devender Rao if (ec) 6185968caeeSMarri Devender Rao { 61962598e31SEd Tanous BMCWEB_LOG_ERROR("DBUS response error: {}", ec); 62026d3b0fbSChandra Harkude const sd_bus_error* dbusError = m.get_error(); 62126d3b0fbSChandra Harkude if ((dbusError != nullptr) && (dbusError->name != nullptr)) 62290d2d1e8SJiaqing Zhao { 62326d3b0fbSChandra Harkude handleError(dbusError->name, id, certificate, asyncResp); 6245968caeeSMarri Devender Rao } 62526d3b0fbSChandra Harkude else 62626d3b0fbSChandra Harkude { 62790d2d1e8SJiaqing Zhao messages::internalError(asyncResp->res); 62826d3b0fbSChandra Harkude } 62990d2d1e8SJiaqing Zhao return; 63090d2d1e8SJiaqing Zhao } 631bd79bce8SPatrick Williams getCertificateProperties(asyncResp, objectPath, service, id, url, 632bd79bce8SPatrick Williams name); 63362598e31SEd Tanous BMCWEB_LOG_DEBUG("HTTPS certificate install file={}", 63462598e31SEd Tanous certFile->getCertFilePath()); 6355968caeeSMarri Devender Rao }, 6365968caeeSMarri Devender Rao service, objectPath, certs::certReplaceIntf, "Replace", 6375968caeeSMarri Devender Rao certFile->getCertFilePath()); 638828252d5SJiaqing Zhao } 6395968caeeSMarri Devender Rao 640cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) 641828252d5SJiaqing Zhao static std::unique_ptr<sdbusplus::bus::match_t> csrMatcher; 6425968caeeSMarri Devender Rao /** 643828252d5SJiaqing Zhao * @brief Read data from CSR D-bus object and set to response 644828252d5SJiaqing Zhao * 645828252d5SJiaqing Zhao * @param[in] asyncResp Shared pointer to the response message 6468ece0e45SEd Tanous * @param[in] certURI Link to certificate collection URI 647828252d5SJiaqing Zhao * @param[in] service D-Bus service name 648828252d5SJiaqing Zhao * @param[in] certObjPath certificate D-Bus object path 649828252d5SJiaqing Zhao * @param[in] csrObjPath CSR D-Bus object path 650828252d5SJiaqing Zhao * @return None 6515968caeeSMarri Devender Rao */ 6524ff0f1f4SEd Tanous inline void getCSR(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 653828252d5SJiaqing Zhao const std::string& certURI, const std::string& service, 654828252d5SJiaqing Zhao const std::string& certObjPath, 655828252d5SJiaqing Zhao const std::string& csrObjPath) 6565968caeeSMarri Devender Rao { 65762598e31SEd Tanous BMCWEB_LOG_DEBUG("getCSR CertObjectPath{} CSRObjectPath={} service={}", 65862598e31SEd Tanous certObjPath, csrObjPath, service); 659*177612aaSEd Tanous dbus::utility::async_method_call( 660*177612aaSEd Tanous asyncResp, 661bd79bce8SPatrick Williams [asyncResp, 662bd79bce8SPatrick Williams certURI](const boost::system::error_code& ec, const std::string& csr) { 663828252d5SJiaqing Zhao if (ec) 664828252d5SJiaqing Zhao { 66562598e31SEd Tanous BMCWEB_LOG_ERROR("DBUS response error: {}", ec); 666828252d5SJiaqing Zhao messages::internalError(asyncResp->res); 667828252d5SJiaqing Zhao return; 668828252d5SJiaqing Zhao } 669828252d5SJiaqing Zhao if (csr.empty()) 670828252d5SJiaqing Zhao { 67162598e31SEd Tanous BMCWEB_LOG_ERROR("CSR read is empty"); 672828252d5SJiaqing Zhao messages::internalError(asyncResp->res); 673828252d5SJiaqing Zhao return; 674828252d5SJiaqing Zhao } 675828252d5SJiaqing Zhao asyncResp->res.jsonValue["CSRString"] = csr; 676828252d5SJiaqing Zhao asyncResp->res.jsonValue["CertificateCollection"]["@odata.id"] = 677828252d5SJiaqing Zhao certURI; 678828252d5SJiaqing Zhao }, 679828252d5SJiaqing Zhao service, csrObjPath, "xyz.openbmc_project.Certs.CSR", "CSR"); 680828252d5SJiaqing Zhao } 681828252d5SJiaqing Zhao 682504af5a0SPatrick Williams inline void handleGenerateCSRAction( 683504af5a0SPatrick Williams App& app, const crow::Request& req, 684828252d5SJiaqing Zhao const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 685828252d5SJiaqing Zhao { 6863ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 68745ca1b86SEd Tanous { 68845ca1b86SEd Tanous return; 68945ca1b86SEd Tanous } 690828252d5SJiaqing Zhao static const int rsaKeyBitLength = 2048; 6915968caeeSMarri Devender Rao 692828252d5SJiaqing Zhao // Required parameters 693828252d5SJiaqing Zhao std::string city; 694828252d5SJiaqing Zhao std::string commonName; 695828252d5SJiaqing Zhao std::string country; 696828252d5SJiaqing Zhao std::string organization; 697828252d5SJiaqing Zhao std::string organizationalUnit; 698828252d5SJiaqing Zhao std::string state; 6997a31e336SEd Tanous std::string certURI; 700828252d5SJiaqing Zhao 701828252d5SJiaqing Zhao // Optional parameters 702828252d5SJiaqing Zhao std::optional<std::vector<std::string>> optAlternativeNames = 703828252d5SJiaqing Zhao std::vector<std::string>(); 704828252d5SJiaqing Zhao std::optional<std::string> optContactPerson = ""; 705828252d5SJiaqing Zhao std::optional<std::string> optChallengePassword = ""; 706828252d5SJiaqing Zhao std::optional<std::string> optEmail = ""; 707828252d5SJiaqing Zhao std::optional<std::string> optGivenName = ""; 708828252d5SJiaqing Zhao std::optional<std::string> optInitials = ""; 709828252d5SJiaqing Zhao std::optional<int64_t> optKeyBitLength = rsaKeyBitLength; 710828252d5SJiaqing Zhao std::optional<std::string> optKeyCurveId = "secp384r1"; 711828252d5SJiaqing Zhao std::optional<std::string> optKeyPairAlgorithm = "EC"; 712828252d5SJiaqing Zhao std::optional<std::vector<std::string>> optKeyUsage = 713828252d5SJiaqing Zhao std::vector<std::string>(); 714828252d5SJiaqing Zhao std::optional<std::string> optSurname = ""; 715828252d5SJiaqing Zhao std::optional<std::string> optUnstructuredName = ""; 716afc474aeSMyung Bae if (!json_util::readJsonAction( // 717afc474aeSMyung Bae req, asyncResp->res, // 718afc474aeSMyung Bae "AlternativeNames", optAlternativeNames, // 719afc474aeSMyung Bae "CertificateCollection/@odata.id", certURI, // 720afc474aeSMyung Bae "ChallengePassword", optChallengePassword, // 721afc474aeSMyung Bae "City", city, // 722afc474aeSMyung Bae "CommonName", commonName, // 723afc474aeSMyung Bae "ContactPerson", optContactPerson, // 724afc474aeSMyung Bae "Country", country, // 725afc474aeSMyung Bae "Email", optEmail, // 726afc474aeSMyung Bae "GivenName", optGivenName, // 727afc474aeSMyung Bae "Initials", optInitials, // 728afc474aeSMyung Bae "KeyBitLength", optKeyBitLength, // 729afc474aeSMyung Bae "KeyCurveId", optKeyCurveId, // 730afc474aeSMyung Bae "KeyPairAlgorithm", optKeyPairAlgorithm, // 731afc474aeSMyung Bae "KeyUsage", optKeyUsage, // 732afc474aeSMyung Bae "Organization", organization, // 733afc474aeSMyung Bae "OrganizationalUnit", organizationalUnit, // 734afc474aeSMyung Bae "State", state, // 735afc474aeSMyung Bae "Surname", optSurname, // 736afc474aeSMyung Bae "UnstructuredName", optUnstructuredName // 737afc474aeSMyung Bae )) 738828252d5SJiaqing Zhao { 739828252d5SJiaqing Zhao return; 7405968caeeSMarri Devender Rao } 7415968caeeSMarri Devender Rao 742828252d5SJiaqing Zhao // bmcweb has no way to store or decode a private key challenge 743828252d5SJiaqing Zhao // password, which will likely cause bmcweb to crash on startup 744828252d5SJiaqing Zhao // if this is not set on a post so not allowing the user to set 745828252d5SJiaqing Zhao // value 746828252d5SJiaqing Zhao if (!optChallengePassword->empty()) 7475968caeeSMarri Devender Rao { 748828252d5SJiaqing Zhao messages::actionParameterNotSupported(asyncResp->res, "GenerateCSR", 749828252d5SJiaqing Zhao "ChallengePassword"); 750828252d5SJiaqing Zhao return; 751828252d5SJiaqing Zhao } 752828252d5SJiaqing Zhao 753828252d5SJiaqing Zhao std::string objectPath; 754828252d5SJiaqing Zhao std::string service; 755253f11b8SEd Tanous if (certURI.starts_with(std::format( 756253f11b8SEd Tanous "/redfish/v1/Managers/{}/NetworkProtocol/HTTPS/Certificates", 757253f11b8SEd Tanous BMCWEB_REDFISH_MANAGER_URI_NAME))) 758828252d5SJiaqing Zhao { 759828252d5SJiaqing Zhao objectPath = certs::httpsObjectPath; 760828252d5SJiaqing Zhao service = certs::httpsServiceName; 761828252d5SJiaqing Zhao } 762828252d5SJiaqing Zhao else if (certURI.starts_with( 763828252d5SJiaqing Zhao "/redfish/v1/AccountService/LDAP/Certificates")) 764828252d5SJiaqing Zhao { 765828252d5SJiaqing Zhao objectPath = certs::ldapObjectPath; 766828252d5SJiaqing Zhao service = certs::ldapServiceName; 767828252d5SJiaqing Zhao } 768828252d5SJiaqing Zhao else 769828252d5SJiaqing Zhao { 770828252d5SJiaqing Zhao messages::actionParameterNotSupported( 771828252d5SJiaqing Zhao asyncResp->res, "CertificateCollection", "GenerateCSR"); 772828252d5SJiaqing Zhao return; 773828252d5SJiaqing Zhao } 774828252d5SJiaqing Zhao 775828252d5SJiaqing Zhao // supporting only EC and RSA algorithm 776828252d5SJiaqing Zhao if (*optKeyPairAlgorithm != "EC" && *optKeyPairAlgorithm != "RSA") 777828252d5SJiaqing Zhao { 778828252d5SJiaqing Zhao messages::actionParameterNotSupported( 779828252d5SJiaqing Zhao asyncResp->res, "KeyPairAlgorithm", "GenerateCSR"); 780828252d5SJiaqing Zhao return; 781828252d5SJiaqing Zhao } 782828252d5SJiaqing Zhao 783828252d5SJiaqing Zhao // supporting only 2048 key bit length for RSA algorithm due to 784828252d5SJiaqing Zhao // time consumed in generating private key 785828252d5SJiaqing Zhao if (*optKeyPairAlgorithm == "RSA" && *optKeyBitLength != rsaKeyBitLength) 786828252d5SJiaqing Zhao { 787e2616cc5SEd Tanous messages::propertyValueNotInList(asyncResp->res, *optKeyBitLength, 788e2616cc5SEd Tanous "KeyBitLength"); 789828252d5SJiaqing Zhao return; 790828252d5SJiaqing Zhao } 791828252d5SJiaqing Zhao 792828252d5SJiaqing Zhao // validate KeyUsage supporting only 1 type based on URL 793253f11b8SEd Tanous if (certURI.starts_with(std::format( 794253f11b8SEd Tanous "/redfish/v1/Managers/{}/NetworkProtocol/HTTPS/Certificates", 795253f11b8SEd Tanous BMCWEB_REDFISH_MANAGER_URI_NAME))) 796828252d5SJiaqing Zhao { 797828252d5SJiaqing Zhao if (optKeyUsage->empty()) 798828252d5SJiaqing Zhao { 799b2ba3072SPatrick Williams optKeyUsage->emplace_back("ServerAuthentication"); 800828252d5SJiaqing Zhao } 801828252d5SJiaqing Zhao else if (optKeyUsage->size() == 1) 802828252d5SJiaqing Zhao { 803828252d5SJiaqing Zhao if ((*optKeyUsage)[0] != "ServerAuthentication") 804828252d5SJiaqing Zhao { 805828252d5SJiaqing Zhao messages::propertyValueNotInList(asyncResp->res, 806828252d5SJiaqing Zhao (*optKeyUsage)[0], "KeyUsage"); 807828252d5SJiaqing Zhao return; 808828252d5SJiaqing Zhao } 809828252d5SJiaqing Zhao } 810828252d5SJiaqing Zhao else 811828252d5SJiaqing Zhao { 812828252d5SJiaqing Zhao messages::actionParameterNotSupported(asyncResp->res, "KeyUsage", 813828252d5SJiaqing Zhao "GenerateCSR"); 814828252d5SJiaqing Zhao return; 815828252d5SJiaqing Zhao } 816828252d5SJiaqing Zhao } 817828252d5SJiaqing Zhao else if (certURI.starts_with( 818828252d5SJiaqing Zhao "/redfish/v1/AccountService/LDAP/Certificates")) 819828252d5SJiaqing Zhao { 820828252d5SJiaqing Zhao if (optKeyUsage->empty()) 821828252d5SJiaqing Zhao { 822b2ba3072SPatrick Williams optKeyUsage->emplace_back("ClientAuthentication"); 823828252d5SJiaqing Zhao } 824828252d5SJiaqing Zhao else if (optKeyUsage->size() == 1) 825828252d5SJiaqing Zhao { 826828252d5SJiaqing Zhao if ((*optKeyUsage)[0] != "ClientAuthentication") 827828252d5SJiaqing Zhao { 828828252d5SJiaqing Zhao messages::propertyValueNotInList(asyncResp->res, 829828252d5SJiaqing Zhao (*optKeyUsage)[0], "KeyUsage"); 830828252d5SJiaqing Zhao return; 831828252d5SJiaqing Zhao } 832828252d5SJiaqing Zhao } 833828252d5SJiaqing Zhao else 834828252d5SJiaqing Zhao { 835828252d5SJiaqing Zhao messages::actionParameterNotSupported(asyncResp->res, "KeyUsage", 836828252d5SJiaqing Zhao "GenerateCSR"); 837828252d5SJiaqing Zhao return; 838828252d5SJiaqing Zhao } 839828252d5SJiaqing Zhao } 840828252d5SJiaqing Zhao 841828252d5SJiaqing Zhao // Only allow one CSR matcher at a time so setting retry 842828252d5SJiaqing Zhao // time-out and timer expiry to 10 seconds for now. 843828252d5SJiaqing Zhao static const int timeOut = 10; 844828252d5SJiaqing Zhao if (csrMatcher) 845828252d5SJiaqing Zhao { 846828252d5SJiaqing Zhao messages::serviceTemporarilyUnavailable(asyncResp->res, 847828252d5SJiaqing Zhao std::to_string(timeOut)); 848828252d5SJiaqing Zhao return; 849828252d5SJiaqing Zhao } 850828252d5SJiaqing Zhao 851828252d5SJiaqing Zhao // Make this static so it survives outside this method 852d98a2f93SEd Tanous static boost::asio::steady_timer timeout(getIoContext()); 853828252d5SJiaqing Zhao timeout.expires_after(std::chrono::seconds(timeOut)); 854828252d5SJiaqing Zhao timeout.async_wait([asyncResp](const boost::system::error_code& ec) { 855828252d5SJiaqing Zhao csrMatcher = nullptr; 856828252d5SJiaqing Zhao if (ec) 857828252d5SJiaqing Zhao { 858828252d5SJiaqing Zhao // operation_aborted is expected if timer is canceled 859828252d5SJiaqing Zhao // before completion. 860828252d5SJiaqing Zhao if (ec != boost::asio::error::operation_aborted) 861828252d5SJiaqing Zhao { 86262598e31SEd Tanous BMCWEB_LOG_ERROR("Async_wait failed {}", ec); 863828252d5SJiaqing Zhao } 864828252d5SJiaqing Zhao return; 865828252d5SJiaqing Zhao } 86662598e31SEd Tanous BMCWEB_LOG_ERROR("Timed out waiting for Generating CSR"); 867828252d5SJiaqing Zhao messages::internalError(asyncResp->res); 868828252d5SJiaqing Zhao }); 869828252d5SJiaqing Zhao 870828252d5SJiaqing Zhao // create a matcher to wait on CSR object 87162598e31SEd Tanous BMCWEB_LOG_DEBUG("create matcher with path {}", objectPath); 872828252d5SJiaqing Zhao std::string match("type='signal'," 873828252d5SJiaqing Zhao "interface='org.freedesktop.DBus.ObjectManager'," 874828252d5SJiaqing Zhao "path='" + 875828252d5SJiaqing Zhao objectPath + 876828252d5SJiaqing Zhao "'," 877828252d5SJiaqing Zhao "member='InterfacesAdded'"); 878828252d5SJiaqing Zhao csrMatcher = std::make_unique<sdbusplus::bus::match_t>( 879828252d5SJiaqing Zhao *crow::connections::systemBus, match, 880828252d5SJiaqing Zhao [asyncResp, service, objectPath, certURI](sdbusplus::message_t& m) { 881828252d5SJiaqing Zhao timeout.cancel(); 882828252d5SJiaqing Zhao if (m.is_method_error()) 883828252d5SJiaqing Zhao { 88462598e31SEd Tanous BMCWEB_LOG_ERROR("Dbus method error!!!"); 885828252d5SJiaqing Zhao messages::internalError(asyncResp->res); 886828252d5SJiaqing Zhao return; 887828252d5SJiaqing Zhao } 888828252d5SJiaqing Zhao 88980f79a40SMichael Shen dbus::utility::DBusInterfacesMap interfacesProperties; 890828252d5SJiaqing Zhao 891828252d5SJiaqing Zhao sdbusplus::message::object_path csrObjectPath; 892828252d5SJiaqing Zhao m.read(csrObjectPath, interfacesProperties); 89362598e31SEd Tanous BMCWEB_LOG_DEBUG("CSR object added{}", csrObjectPath.str); 894828252d5SJiaqing Zhao for (const auto& interface : interfacesProperties) 895828252d5SJiaqing Zhao { 896828252d5SJiaqing Zhao if (interface.first == "xyz.openbmc_project.Certs.CSR") 897828252d5SJiaqing Zhao { 898828252d5SJiaqing Zhao getCSR(asyncResp, certURI, service, objectPath, 899828252d5SJiaqing Zhao csrObjectPath.str); 900828252d5SJiaqing Zhao break; 901828252d5SJiaqing Zhao } 902828252d5SJiaqing Zhao } 903828252d5SJiaqing Zhao }); 904*177612aaSEd Tanous dbus::utility::async_method_call( 905*177612aaSEd Tanous asyncResp, 9065e7e2dc5SEd Tanous [asyncResp](const boost::system::error_code& ec, const std::string&) { 907828252d5SJiaqing Zhao if (ec) 908828252d5SJiaqing Zhao { 90962598e31SEd Tanous BMCWEB_LOG_ERROR("DBUS response error: {}", ec.message()); 910828252d5SJiaqing Zhao messages::internalError(asyncResp->res); 911828252d5SJiaqing Zhao return; 912828252d5SJiaqing Zhao } 913828252d5SJiaqing Zhao }, 914828252d5SJiaqing Zhao service, objectPath, "xyz.openbmc_project.Certs.CSR.Create", 915828252d5SJiaqing Zhao "GenerateCSR", *optAlternativeNames, *optChallengePassword, city, 916828252d5SJiaqing Zhao commonName, *optContactPerson, country, *optEmail, *optGivenName, 917828252d5SJiaqing Zhao *optInitials, *optKeyBitLength, *optKeyCurveId, *optKeyPairAlgorithm, 918828252d5SJiaqing Zhao *optKeyUsage, organization, organizationalUnit, state, *optSurname, 919828252d5SJiaqing Zhao *optUnstructuredName); 920828252d5SJiaqing Zhao } 921828252d5SJiaqing Zhao 922828252d5SJiaqing Zhao inline void requestRoutesCertificateService(App& app) 923828252d5SJiaqing Zhao { 924828252d5SJiaqing Zhao BMCWEB_ROUTE(app, "/redfish/v1/CertificateService/") 925828252d5SJiaqing Zhao .privileges(redfish::privileges::getCertificateService) 926002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 927828252d5SJiaqing Zhao std::bind_front(handleCertificateServiceGet, std::ref(app))); 928828252d5SJiaqing Zhao 929828252d5SJiaqing Zhao BMCWEB_ROUTE(app, "/redfish/v1/CertificateService/CertificateLocations/") 930828252d5SJiaqing Zhao .privileges(redfish::privileges::getCertificateLocations) 931828252d5SJiaqing Zhao .methods(boost::beast::http::verb::get)( 932828252d5SJiaqing Zhao std::bind_front(handleCertificateLocationsGet, std::ref(app))); 933828252d5SJiaqing Zhao 934828252d5SJiaqing Zhao BMCWEB_ROUTE( 935828252d5SJiaqing Zhao app, 936828252d5SJiaqing Zhao "/redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate/") 937828252d5SJiaqing Zhao .privileges(redfish::privileges::postCertificateService) 938828252d5SJiaqing Zhao .methods(boost::beast::http::verb::post)( 939828252d5SJiaqing Zhao std::bind_front(handleReplaceCertificateAction, std::ref(app))); 940828252d5SJiaqing Zhao 941828252d5SJiaqing Zhao BMCWEB_ROUTE( 942828252d5SJiaqing Zhao app, 943828252d5SJiaqing Zhao "/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR/") 944828252d5SJiaqing Zhao .privileges(redfish::privileges::postCertificateService) 945828252d5SJiaqing Zhao .methods(boost::beast::http::verb::post)( 946828252d5SJiaqing Zhao std::bind_front(handleGenerateCSRAction, std::ref(app))); 947828252d5SJiaqing Zhao } // requestRoutesCertificateService 948828252d5SJiaqing Zhao 949828252d5SJiaqing Zhao inline void handleHTTPSCertificateCollectionGet( 950828252d5SJiaqing Zhao App& app, const crow::Request& req, 951253f11b8SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 952253f11b8SEd Tanous const std::string& managerId) 953828252d5SJiaqing Zhao { 9543ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 95545ca1b86SEd Tanous { 95645ca1b86SEd Tanous return; 95745ca1b86SEd Tanous } 9581476687dSEd Tanous 959253f11b8SEd Tanous if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME) 960253f11b8SEd Tanous { 961253f11b8SEd Tanous messages::resourceNotFound(asyncResp->res, "Manager", managerId); 962253f11b8SEd Tanous return; 963253f11b8SEd Tanous } 964253f11b8SEd Tanous 965253f11b8SEd Tanous asyncResp->res.jsonValue["@odata.id"] = boost::urls::format( 966253f11b8SEd Tanous "/redfish/v1/Managers/{}/NetworkProtocol/HTTPS/Certificates", 967253f11b8SEd Tanous BMCWEB_REDFISH_MANAGER_URI_NAME); 9681476687dSEd Tanous asyncResp->res.jsonValue["@odata.type"] = 9691476687dSEd Tanous "#CertificateCollection.CertificateCollection"; 9701476687dSEd Tanous asyncResp->res.jsonValue["Name"] = "HTTPS Certificates Collection"; 9711476687dSEd Tanous asyncResp->res.jsonValue["Description"] = 9721476687dSEd Tanous "A Collection of HTTPS certificate instances"; 9738d1b46d7Szhanghch05 974d3f92ce7SJiaqing Zhao getCertificateList(asyncResp, certs::httpsObjectPath, 975d3f92ce7SJiaqing Zhao "/Members"_json_pointer, 976d3f92ce7SJiaqing Zhao "/Members@odata.count"_json_pointer); 977828252d5SJiaqing Zhao } 9785968caeeSMarri Devender Rao 979828252d5SJiaqing Zhao inline void handleHTTPSCertificateCollectionPost( 980828252d5SJiaqing Zhao App& app, const crow::Request& req, 981253f11b8SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 982253f11b8SEd Tanous const std::string& managerId) 983828252d5SJiaqing Zhao { 9843ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 98545ca1b86SEd Tanous { 98645ca1b86SEd Tanous return; 98745ca1b86SEd Tanous } 988253f11b8SEd Tanous 989253f11b8SEd Tanous if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME) 990253f11b8SEd Tanous { 991253f11b8SEd Tanous messages::resourceNotFound(asyncResp->res, "Manager", managerId); 992253f11b8SEd Tanous return; 993253f11b8SEd Tanous } 994253f11b8SEd Tanous 99562598e31SEd Tanous BMCWEB_LOG_DEBUG("HTTPSCertificateCollection::doPost"); 9968d1b46d7Szhanghch05 9971476687dSEd Tanous asyncResp->res.jsonValue["Name"] = "HTTPS Certificate"; 9981476687dSEd Tanous asyncResp->res.jsonValue["Description"] = "HTTPS Certificate"; 9995968caeeSMarri Devender Rao 1000b2896149SEd Tanous std::string certHttpBody = getCertificateFromReqBody(asyncResp, req); 100158eb238fSKowalski, Kamil 1002b2896149SEd Tanous if (certHttpBody.empty()) 100358eb238fSKowalski, Kamil { 100462598e31SEd Tanous BMCWEB_LOG_ERROR("Cannot get certificate from request body."); 1005a08752f5SZbigniew Kurzynski messages::unrecognizedRequestBody(asyncResp->res); 100658eb238fSKowalski, Kamil return; 100758eb238fSKowalski, Kamil } 100858eb238fSKowalski, Kamil 10095968caeeSMarri Devender Rao std::shared_ptr<CertificateFile> certFile = 1010b2896149SEd Tanous std::make_shared<CertificateFile>(certHttpBody); 10115968caeeSMarri Devender Rao 1012*177612aaSEd Tanous dbus::utility::async_method_call( 1013*177612aaSEd Tanous asyncResp, 10145e7e2dc5SEd Tanous [asyncResp, certFile](const boost::system::error_code& ec, 1015656ec7e3SZbigniew Kurzynski const std::string& objectPath) { 10165968caeeSMarri Devender Rao if (ec) 10175968caeeSMarri Devender Rao { 101862598e31SEd Tanous BMCWEB_LOG_ERROR("DBUS response error: {}", ec); 10195968caeeSMarri Devender Rao messages::internalError(asyncResp->res); 10205968caeeSMarri Devender Rao return; 10215968caeeSMarri Devender Rao } 1022717b9802SJiaqing Zhao 1023717b9802SJiaqing Zhao sdbusplus::message::object_path path(objectPath); 1024717b9802SJiaqing Zhao std::string certId = path.filename(); 1025ef4c65b7SEd Tanous const boost::urls::url certURL = boost::urls::format( 1026253f11b8SEd Tanous "/redfish/v1/Managers/{}/NetworkProtocol/HTTPS/Certificates/{}", 1027253f11b8SEd Tanous BMCWEB_REDFISH_MANAGER_URI_NAME, certId); 1028bd79bce8SPatrick Williams getCertificateProperties(asyncResp, objectPath, 1029bd79bce8SPatrick Williams certs::httpsServiceName, certId, certURL, 1030bd79bce8SPatrick Williams "HTTPS Certificate"); 103162598e31SEd Tanous BMCWEB_LOG_DEBUG("HTTPS certificate install file={}", 103262598e31SEd Tanous certFile->getCertFilePath()); 10335968caeeSMarri Devender Rao }, 1034828252d5SJiaqing Zhao certs::httpsServiceName, certs::httpsObjectPath, certs::certInstallIntf, 1035828252d5SJiaqing Zhao "Install", certFile->getCertFilePath()); 1036828252d5SJiaqing Zhao } 10375968caeeSMarri Devender Rao 1038828252d5SJiaqing Zhao inline void handleHTTPSCertificateGet( 1039828252d5SJiaqing Zhao App& app, const crow::Request& req, 1040253f11b8SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1041253f11b8SEd Tanous const std::string& managerId, const std::string& certId) 10427e860f15SJohn Edward Broadbent { 10433ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 104445ca1b86SEd Tanous { 104545ca1b86SEd Tanous return; 104645ca1b86SEd Tanous } 10477e860f15SJohn Edward Broadbent 1048253f11b8SEd Tanous if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME) 1049253f11b8SEd Tanous { 1050253f11b8SEd Tanous messages::resourceNotFound(asyncResp->res, "Manager", managerId); 1051253f11b8SEd Tanous return; 1052253f11b8SEd Tanous } 1053253f11b8SEd Tanous 1054253f11b8SEd Tanous BMCWEB_LOG_DEBUG("HTTPS Certificate ID={}", certId); 1055ef4c65b7SEd Tanous const boost::urls::url certURL = boost::urls::format( 1056253f11b8SEd Tanous "/redfish/v1/Managers/{}/NetworkProtocol/HTTPS/Certificates/{}", 1057253f11b8SEd Tanous BMCWEB_REDFISH_MANAGER_URI_NAME, certId); 1058828252d5SJiaqing Zhao std::string objPath = 1059253f11b8SEd Tanous sdbusplus::message::object_path(certs::httpsObjectPath) / certId; 1060253f11b8SEd Tanous getCertificateProperties(asyncResp, objPath, certs::httpsServiceName, 1061253f11b8SEd Tanous certId, certURL, "HTTPS Certificate"); 10627e860f15SJohn Edward Broadbent } 106337cce918SMarri Devender Rao 1064828252d5SJiaqing Zhao inline void requestRoutesHTTPSCertificate(App& app) 106537cce918SMarri Devender Rao { 1066253f11b8SEd Tanous BMCWEB_ROUTE( 1067253f11b8SEd Tanous app, "/redfish/v1/Managers/<str>/NetworkProtocol/HTTPS/Certificates/") 1068ed398213SEd Tanous .privileges(redfish::privileges::getCertificateCollection) 1069828252d5SJiaqing Zhao .methods(boost::beast::http::verb::get)(std::bind_front( 1070828252d5SJiaqing Zhao handleHTTPSCertificateCollectionGet, std::ref(app))); 1071828252d5SJiaqing Zhao 1072253f11b8SEd Tanous BMCWEB_ROUTE( 1073253f11b8SEd Tanous app, "/redfish/v1/Managers/<str>/NetworkProtocol/HTTPS/Certificates/") 1074828252d5SJiaqing Zhao .privileges(redfish::privileges::postCertificateCollection) 1075828252d5SJiaqing Zhao .methods(boost::beast::http::verb::post)(std::bind_front( 1076828252d5SJiaqing Zhao handleHTTPSCertificateCollectionPost, std::ref(app))); 1077828252d5SJiaqing Zhao 1078828252d5SJiaqing Zhao BMCWEB_ROUTE( 1079828252d5SJiaqing Zhao app, 1080253f11b8SEd Tanous "/redfish/v1/Managers/<str>/NetworkProtocol/HTTPS/Certificates/<str>/") 1081828252d5SJiaqing Zhao .privileges(redfish::privileges::getCertificate) 1082002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 1083828252d5SJiaqing Zhao std::bind_front(handleHTTPSCertificateGet, std::ref(app))); 1084828252d5SJiaqing Zhao } 1085828252d5SJiaqing Zhao 1086828252d5SJiaqing Zhao inline void handleLDAPCertificateCollectionGet( 1087828252d5SJiaqing Zhao App& app, const crow::Request& req, 1088828252d5SJiaqing Zhao const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1089828252d5SJiaqing Zhao { 10903ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 109145ca1b86SEd Tanous { 109245ca1b86SEd Tanous return; 109345ca1b86SEd Tanous } 10941476687dSEd Tanous 10951476687dSEd Tanous asyncResp->res.jsonValue["@odata.id"] = 10961476687dSEd Tanous "/redfish/v1/AccountService/LDAP/Certificates"; 10971476687dSEd Tanous asyncResp->res.jsonValue["@odata.type"] = 10981476687dSEd Tanous "#CertificateCollection.CertificateCollection"; 10991476687dSEd Tanous asyncResp->res.jsonValue["Name"] = "LDAP Certificates Collection"; 11001476687dSEd Tanous asyncResp->res.jsonValue["Description"] = 11011476687dSEd Tanous "A Collection of LDAP certificate instances"; 11028d1b46d7Szhanghch05 1103d3f92ce7SJiaqing Zhao getCertificateList(asyncResp, certs::ldapObjectPath, 1104d3f92ce7SJiaqing Zhao "/Members"_json_pointer, 1105d3f92ce7SJiaqing Zhao "/Members@odata.count"_json_pointer); 1106828252d5SJiaqing Zhao } 110737cce918SMarri Devender Rao 1108828252d5SJiaqing Zhao inline void handleLDAPCertificateCollectionPost( 1109828252d5SJiaqing Zhao App& app, const crow::Request& req, 1110828252d5SJiaqing Zhao const std::shared_ptr<bmcweb::AsyncResp>& asyncResp) 1111828252d5SJiaqing Zhao { 11123ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 111345ca1b86SEd Tanous { 111445ca1b86SEd Tanous return; 111545ca1b86SEd Tanous } 1116b2896149SEd Tanous std::string certHttpBody = getCertificateFromReqBody(asyncResp, req); 111758eb238fSKowalski, Kamil 1118b2896149SEd Tanous if (certHttpBody.empty()) 111958eb238fSKowalski, Kamil { 112062598e31SEd Tanous BMCWEB_LOG_ERROR("Cannot get certificate from request body."); 1121a08752f5SZbigniew Kurzynski messages::unrecognizedRequestBody(asyncResp->res); 112258eb238fSKowalski, Kamil return; 112358eb238fSKowalski, Kamil } 112458eb238fSKowalski, Kamil 112558eb238fSKowalski, Kamil std::shared_ptr<CertificateFile> certFile = 1126b2896149SEd Tanous std::make_shared<CertificateFile>(certHttpBody); 112758eb238fSKowalski, Kamil 1128*177612aaSEd Tanous dbus::utility::async_method_call( 1129*177612aaSEd Tanous asyncResp, 11305e7e2dc5SEd Tanous [asyncResp, certFile](const boost::system::error_code& ec, 1131656ec7e3SZbigniew Kurzynski const std::string& objectPath) { 113237cce918SMarri Devender Rao if (ec) 113337cce918SMarri Devender Rao { 113462598e31SEd Tanous BMCWEB_LOG_ERROR("DBUS response error: {}", ec); 113537cce918SMarri Devender Rao messages::internalError(asyncResp->res); 113637cce918SMarri Devender Rao return; 113737cce918SMarri Devender Rao } 1138717b9802SJiaqing Zhao 1139717b9802SJiaqing Zhao sdbusplus::message::object_path path(objectPath); 1140717b9802SJiaqing Zhao std::string certId = path.filename(); 1141ef4c65b7SEd Tanous const boost::urls::url certURL = boost::urls::format( 1142ef4c65b7SEd Tanous "/redfish/v1/AccountService/LDAP/Certificates/{}", certId); 1143bd79bce8SPatrick Williams getCertificateProperties(asyncResp, objectPath, 1144bd79bce8SPatrick Williams certs::ldapServiceName, certId, certURL, 1145bd79bce8SPatrick Williams "LDAP Certificate"); 114662598e31SEd Tanous BMCWEB_LOG_DEBUG("LDAP certificate install file={}", 114762598e31SEd Tanous certFile->getCertFilePath()); 114837cce918SMarri Devender Rao }, 1149828252d5SJiaqing Zhao certs::ldapServiceName, certs::ldapObjectPath, certs::certInstallIntf, 1150828252d5SJiaqing Zhao "Install", certFile->getCertFilePath()); 1151828252d5SJiaqing Zhao } 115237cce918SMarri Devender Rao 1153828252d5SJiaqing Zhao inline void handleLDAPCertificateGet( 1154828252d5SJiaqing Zhao App& app, const crow::Request& req, 1155828252d5SJiaqing Zhao const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id) 115637cce918SMarri Devender Rao { 11573ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 115845ca1b86SEd Tanous { 115945ca1b86SEd Tanous return; 116045ca1b86SEd Tanous } 1161717b9802SJiaqing Zhao 116262598e31SEd Tanous BMCWEB_LOG_DEBUG("LDAP Certificate ID={}", id); 1163ef4c65b7SEd Tanous const boost::urls::url certURL = boost::urls::format( 1164ef4c65b7SEd Tanous "/redfish/v1/AccountService/LDAP/Certificates/{}", id); 1165717b9802SJiaqing Zhao std::string objPath = 1166717b9802SJiaqing Zhao sdbusplus::message::object_path(certs::ldapObjectPath) / id; 1167717b9802SJiaqing Zhao getCertificateProperties(asyncResp, objPath, certs::ldapServiceName, id, 1168717b9802SJiaqing Zhao certURL, "LDAP Certificate"); 1169828252d5SJiaqing Zhao } 1170828252d5SJiaqing Zhao 117199612247SJiaqing Zhao inline void handleLDAPCertificateDelete( 117299612247SJiaqing Zhao App& app, const crow::Request& req, 117399612247SJiaqing Zhao const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id) 117499612247SJiaqing Zhao { 117599612247SJiaqing Zhao if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 117699612247SJiaqing Zhao { 117799612247SJiaqing Zhao return; 117899612247SJiaqing Zhao } 117999612247SJiaqing Zhao 118062598e31SEd Tanous BMCWEB_LOG_DEBUG("Delete LDAP Certificate ID={}", id); 118199612247SJiaqing Zhao std::string objPath = 118299612247SJiaqing Zhao sdbusplus::message::object_path(certs::ldapObjectPath) / id; 118399612247SJiaqing Zhao 118499612247SJiaqing Zhao deleteCertificate(asyncResp, certs::ldapServiceName, objPath); 118599612247SJiaqing Zhao } 118699612247SJiaqing Zhao 1187828252d5SJiaqing Zhao inline void requestRoutesLDAPCertificate(App& app) 1188cfcd5f6bSMarri Devender Rao { 1189828252d5SJiaqing Zhao BMCWEB_ROUTE(app, "/redfish/v1/AccountService/LDAP/Certificates/") 1190828252d5SJiaqing Zhao .privileges(redfish::privileges::getCertificateCollection) 1191828252d5SJiaqing Zhao .methods(boost::beast::http::verb::get)( 1192828252d5SJiaqing Zhao std::bind_front(handleLDAPCertificateCollectionGet, std::ref(app))); 1193828252d5SJiaqing Zhao 1194828252d5SJiaqing Zhao BMCWEB_ROUTE(app, "/redfish/v1/AccountService/LDAP/Certificates/") 1195828252d5SJiaqing Zhao .privileges(redfish::privileges::postCertificateCollection) 1196828252d5SJiaqing Zhao .methods(boost::beast::http::verb::post)(std::bind_front( 1197828252d5SJiaqing Zhao handleLDAPCertificateCollectionPost, std::ref(app))); 1198828252d5SJiaqing Zhao 1199828252d5SJiaqing Zhao BMCWEB_ROUTE(app, "/redfish/v1/AccountService/LDAP/Certificates/<str>/") 1200ed398213SEd Tanous .privileges(redfish::privileges::getCertificate) 1201002d39b4SEd Tanous .methods(boost::beast::http::verb::get)( 1202828252d5SJiaqing Zhao std::bind_front(handleLDAPCertificateGet, std::ref(app))); 120399612247SJiaqing Zhao 120499612247SJiaqing Zhao BMCWEB_ROUTE(app, "/redfish/v1/AccountService/LDAP/Certificates/<str>/") 120599612247SJiaqing Zhao .privileges(redfish::privileges::deleteCertificate) 120699612247SJiaqing Zhao .methods(boost::beast::http::verb::delete_)( 120799612247SJiaqing Zhao std::bind_front(handleLDAPCertificateDelete, std::ref(app))); 1208828252d5SJiaqing Zhao } // requestRoutesLDAPCertificate 1209828252d5SJiaqing Zhao 1210828252d5SJiaqing Zhao inline void handleTrustStoreCertificateCollectionGet( 1211828252d5SJiaqing Zhao App& app, const crow::Request& req, 1212253f11b8SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1213253f11b8SEd Tanous const std::string& managerId) 1214828252d5SJiaqing Zhao { 12153ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 121645ca1b86SEd Tanous { 121745ca1b86SEd Tanous return; 121845ca1b86SEd Tanous } 12191476687dSEd Tanous 1220253f11b8SEd Tanous if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME) 1221253f11b8SEd Tanous { 1222253f11b8SEd Tanous messages::resourceNotFound(asyncResp->res, "Manager", managerId); 1223253f11b8SEd Tanous return; 1224253f11b8SEd Tanous } 1225253f11b8SEd Tanous 12261476687dSEd Tanous asyncResp->res.jsonValue["@odata.id"] = 1227253f11b8SEd Tanous boost::urls::format("/redfish/v1/Managers/{}/Truststore/Certificates/", 1228253f11b8SEd Tanous BMCWEB_REDFISH_MANAGER_URI_NAME); 12291476687dSEd Tanous asyncResp->res.jsonValue["@odata.type"] = 12301476687dSEd Tanous "#CertificateCollection.CertificateCollection"; 1231002d39b4SEd Tanous asyncResp->res.jsonValue["Name"] = "TrustStore Certificates Collection"; 12321476687dSEd Tanous asyncResp->res.jsonValue["Description"] = 12331476687dSEd Tanous "A Collection of TrustStore certificate instances"; 12348d1b46d7Szhanghch05 1235d3f92ce7SJiaqing Zhao getCertificateList(asyncResp, certs::authorityObjectPath, 1236d3f92ce7SJiaqing Zhao "/Members"_json_pointer, 1237d3f92ce7SJiaqing Zhao "/Members@odata.count"_json_pointer); 1238828252d5SJiaqing Zhao } 1239cfcd5f6bSMarri Devender Rao 1240828252d5SJiaqing Zhao inline void handleTrustStoreCertificateCollectionPost( 1241828252d5SJiaqing Zhao App& app, const crow::Request& req, 1242253f11b8SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1243253f11b8SEd Tanous const std::string& managerId) 1244828252d5SJiaqing Zhao { 12453ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 124645ca1b86SEd Tanous { 124745ca1b86SEd Tanous return; 124845ca1b86SEd Tanous } 1249253f11b8SEd Tanous 1250253f11b8SEd Tanous if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME) 1251253f11b8SEd Tanous { 1252253f11b8SEd Tanous messages::resourceNotFound(asyncResp->res, "Manager", managerId); 1253253f11b8SEd Tanous return; 1254253f11b8SEd Tanous } 1255253f11b8SEd Tanous 1256b2896149SEd Tanous std::string certHttpBody = getCertificateFromReqBody(asyncResp, req); 1257a08752f5SZbigniew Kurzynski 1258b2896149SEd Tanous if (certHttpBody.empty()) 1259a08752f5SZbigniew Kurzynski { 126062598e31SEd Tanous BMCWEB_LOG_ERROR("Cannot get certificate from request body."); 1261a08752f5SZbigniew Kurzynski messages::unrecognizedRequestBody(asyncResp->res); 1262a08752f5SZbigniew Kurzynski return; 1263a08752f5SZbigniew Kurzynski } 1264a08752f5SZbigniew Kurzynski 1265a08752f5SZbigniew Kurzynski std::shared_ptr<CertificateFile> certFile = 1266b2896149SEd Tanous std::make_shared<CertificateFile>(certHttpBody); 1267*177612aaSEd Tanous dbus::utility::async_method_call( 1268*177612aaSEd Tanous asyncResp, 12695e7e2dc5SEd Tanous [asyncResp, certFile](const boost::system::error_code& ec, 1270656ec7e3SZbigniew Kurzynski const std::string& objectPath) { 1271cfcd5f6bSMarri Devender Rao if (ec) 1272cfcd5f6bSMarri Devender Rao { 127362598e31SEd Tanous BMCWEB_LOG_ERROR("DBUS response error: {}", ec); 1274cfcd5f6bSMarri Devender Rao messages::internalError(asyncResp->res); 1275cfcd5f6bSMarri Devender Rao return; 1276cfcd5f6bSMarri Devender Rao } 1277656ec7e3SZbigniew Kurzynski 1278717b9802SJiaqing Zhao sdbusplus::message::object_path path(objectPath); 1279717b9802SJiaqing Zhao std::string certId = path.filename(); 1280ef4c65b7SEd Tanous const boost::urls::url certURL = boost::urls::format( 1281253f11b8SEd Tanous "/redfish/v1/Managers/{}/Truststore/Certificates/{}", 1282253f11b8SEd Tanous BMCWEB_REDFISH_MANAGER_URI_NAME, certId); 1283717b9802SJiaqing Zhao getCertificateProperties(asyncResp, objectPath, 1284bd79bce8SPatrick Williams certs::authorityServiceName, certId, 1285bd79bce8SPatrick Williams certURL, "TrustStore Certificate"); 128662598e31SEd Tanous BMCWEB_LOG_DEBUG("TrustStore certificate install file={}", 128762598e31SEd Tanous certFile->getCertFilePath()); 1288cfcd5f6bSMarri Devender Rao }, 1289cfcd5f6bSMarri Devender Rao certs::authorityServiceName, certs::authorityObjectPath, 12900fda0f12SGeorge Liu certs::certInstallIntf, "Install", certFile->getCertFilePath()); 1291828252d5SJiaqing Zhao } 1292cfcd5f6bSMarri Devender Rao 1293828252d5SJiaqing Zhao inline void handleTrustStoreCertificateGet( 1294828252d5SJiaqing Zhao App& app, const crow::Request& req, 1295253f11b8SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1296253f11b8SEd Tanous const std::string& managerId, const std::string& certId) 1297cfcd5f6bSMarri Devender Rao { 12983ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 129945ca1b86SEd Tanous { 130045ca1b86SEd Tanous return; 130145ca1b86SEd Tanous } 1302717b9802SJiaqing Zhao 1303253f11b8SEd Tanous if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME) 1304253f11b8SEd Tanous { 1305253f11b8SEd Tanous messages::resourceNotFound(asyncResp->res, "Manager", managerId); 1306253f11b8SEd Tanous return; 1307253f11b8SEd Tanous } 1308253f11b8SEd Tanous 1309253f11b8SEd Tanous BMCWEB_LOG_DEBUG("Truststore Certificate ID={}", certId); 1310ef4c65b7SEd Tanous const boost::urls::url certURL = boost::urls::format( 1311253f11b8SEd Tanous "/redfish/v1/Managers/{}/Truststore/Certificates/{}", 1312253f11b8SEd Tanous BMCWEB_REDFISH_MANAGER_URI_NAME, certId); 1313717b9802SJiaqing Zhao std::string objPath = 1314253f11b8SEd Tanous sdbusplus::message::object_path(certs::authorityObjectPath) / certId; 1315828252d5SJiaqing Zhao getCertificateProperties(asyncResp, objPath, certs::authorityServiceName, 1316253f11b8SEd Tanous certId, certURL, "TrustStore Certificate"); 1317828252d5SJiaqing Zhao } 131807a60299SZbigniew Kurzynski 1319828252d5SJiaqing Zhao inline void handleTrustStoreCertificateDelete( 1320828252d5SJiaqing Zhao App& app, const crow::Request& req, 1321253f11b8SEd Tanous const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, 1322253f11b8SEd Tanous const std::string& managerId, const std::string& certId) 1323828252d5SJiaqing Zhao { 13243ba00073SCarson Labrado if (!redfish::setUpRedfishRoute(app, req, asyncResp)) 132545ca1b86SEd Tanous { 132645ca1b86SEd Tanous return; 132745ca1b86SEd Tanous } 132807a60299SZbigniew Kurzynski 1329253f11b8SEd Tanous if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME) 1330253f11b8SEd Tanous { 1331253f11b8SEd Tanous messages::resourceNotFound(asyncResp->res, "Manager", managerId); 1332253f11b8SEd Tanous return; 1333253f11b8SEd Tanous } 1334253f11b8SEd Tanous 1335253f11b8SEd Tanous BMCWEB_LOG_DEBUG("Delete TrustStore Certificate ID={}", certId); 1336717b9802SJiaqing Zhao std::string objPath = 1337253f11b8SEd Tanous sdbusplus::message::object_path(certs::authorityObjectPath) / certId; 133807a60299SZbigniew Kurzynski 13397a3a8f7aSJiaqing Zhao deleteCertificate(asyncResp, certs::authorityServiceName, objPath); 1340828252d5SJiaqing Zhao } 1341828252d5SJiaqing Zhao 1342828252d5SJiaqing Zhao inline void requestRoutesTrustStoreCertificate(App& app) 1343828252d5SJiaqing Zhao { 1344253f11b8SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/Truststore/Certificates/") 1345828252d5SJiaqing Zhao .privileges(redfish::privileges::getCertificate) 1346828252d5SJiaqing Zhao .methods(boost::beast::http::verb::get)(std::bind_front( 1347828252d5SJiaqing Zhao handleTrustStoreCertificateCollectionGet, std::ref(app))); 1348828252d5SJiaqing Zhao 1349253f11b8SEd Tanous BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/Truststore/Certificates/") 1350828252d5SJiaqing Zhao .privileges(redfish::privileges::postCertificateCollection) 1351828252d5SJiaqing Zhao .methods(boost::beast::http::verb::post)(std::bind_front( 1352828252d5SJiaqing Zhao handleTrustStoreCertificateCollectionPost, std::ref(app))); 1353828252d5SJiaqing Zhao 1354253f11b8SEd Tanous BMCWEB_ROUTE(app, 1355253f11b8SEd Tanous "/redfish/v1/Managers/<str>/Truststore/Certificates/<str>/") 1356828252d5SJiaqing Zhao .privileges(redfish::privileges::getCertificate) 1357828252d5SJiaqing Zhao .methods(boost::beast::http::verb::get)( 1358828252d5SJiaqing Zhao std::bind_front(handleTrustStoreCertificateGet, std::ref(app))); 1359828252d5SJiaqing Zhao 1360253f11b8SEd Tanous BMCWEB_ROUTE(app, 1361253f11b8SEd Tanous "/redfish/v1/Managers/<str>/Truststore/Certificates/<str>/") 1362828252d5SJiaqing Zhao .privileges(redfish::privileges::deleteCertificate) 1363828252d5SJiaqing Zhao .methods(boost::beast::http::verb::delete_)( 1364828252d5SJiaqing Zhao std::bind_front(handleTrustStoreCertificateDelete, std::ref(app))); 13657e860f15SJohn Edward Broadbent } // requestRoutesTrustStoreCertificate 13665968caeeSMarri Devender Rao } // namespace redfish 1367