xref: /openbmc/bmcweb/features/redfish/lib/certificate_service.hpp (revision deae6a789444debc4724fb6902fc5def299afbee)
15968caeeSMarri Devender Rao #pragma once
25968caeeSMarri Devender Rao 
33ccb3adbSEd Tanous #include "app.hpp"
43ccb3adbSEd Tanous #include "async_resp.hpp"
57a1dbc48SGeorge Liu #include "dbus_utility.hpp"
61aa0c2b8SEd Tanous #include "http/parsing.hpp"
73ccb3adbSEd Tanous #include "http_response.hpp"
83ccb3adbSEd Tanous #include "query.hpp"
93ccb3adbSEd Tanous #include "registries/privilege_registry.hpp"
109b12d1f9SKrzysztof Grobelny #include "utils/dbus_utils.hpp"
113ccb3adbSEd Tanous #include "utils/json_utils.hpp"
123ccb3adbSEd Tanous #include "utils/time_utils.hpp"
139b12d1f9SKrzysztof Grobelny 
1490d2d1e8SJiaqing Zhao #include <boost/system/linux_error.hpp>
15ef4c65b7SEd Tanous #include <boost/url/format.hpp>
169b12d1f9SKrzysztof Grobelny #include <sdbusplus/asio/property.hpp>
173ccb3adbSEd Tanous #include <sdbusplus/bus/match.hpp>
189b12d1f9SKrzysztof Grobelny #include <sdbusplus/unpack_properties.hpp>
191214b7e7SGunnar Mills 
207a1dbc48SGeorge Liu #include <array>
213ccb3adbSEd Tanous #include <memory>
227a1dbc48SGeorge Liu #include <string_view>
237a1dbc48SGeorge Liu 
245968caeeSMarri Devender Rao namespace redfish
255968caeeSMarri Devender Rao {
265968caeeSMarri Devender Rao namespace certs
275968caeeSMarri Devender Rao {
2889492a15SPatrick Williams constexpr const char* certInstallIntf = "xyz.openbmc_project.Certs.Install";
2989492a15SPatrick Williams constexpr const char* certReplaceIntf = "xyz.openbmc_project.Certs.Replace";
3089492a15SPatrick Williams constexpr const char* objDeleteIntf = "xyz.openbmc_project.Object.Delete";
3189492a15SPatrick Williams constexpr const char* certPropIntf = "xyz.openbmc_project.Certs.Certificate";
3289492a15SPatrick Williams constexpr const char* dbusPropIntf = "org.freedesktop.DBus.Properties";
3389492a15SPatrick Williams constexpr const char* dbusObjManagerIntf = "org.freedesktop.DBus.ObjectManager";
3489492a15SPatrick Williams constexpr const char* httpsServiceName =
3537cce918SMarri Devender Rao     "xyz.openbmc_project.Certs.Manager.Server.Https";
3689492a15SPatrick Williams constexpr const char* ldapServiceName =
3737cce918SMarri Devender Rao     "xyz.openbmc_project.Certs.Manager.Client.Ldap";
3889492a15SPatrick Williams constexpr const char* authorityServiceName =
39b2254ccdSMichal Orzel     "xyz.openbmc_project.Certs.Manager.Authority.Truststore";
4089492a15SPatrick Williams constexpr const char* baseObjectPath = "/xyz/openbmc_project/certs";
4189492a15SPatrick Williams constexpr const char* httpsObjectPath =
42c6a8dfb1SJiaqing Zhao     "/xyz/openbmc_project/certs/server/https";
4389492a15SPatrick Williams constexpr const char* ldapObjectPath = "/xyz/openbmc_project/certs/client/ldap";
4489492a15SPatrick Williams constexpr const char* authorityObjectPath =
45b2254ccdSMichal Orzel     "/xyz/openbmc_project/certs/authority/truststore";
465968caeeSMarri Devender Rao } // namespace certs
475968caeeSMarri Devender Rao 
485968caeeSMarri Devender Rao /**
495968caeeSMarri Devender Rao  * The Certificate schema defines a Certificate Service which represents the
505968caeeSMarri Devender Rao  * actions available to manage certificates and links to where certificates
515968caeeSMarri Devender Rao  * are installed.
525968caeeSMarri Devender Rao  */
537e860f15SJohn Edward Broadbent 
548d1b46d7Szhanghch05 inline std::string getCertificateFromReqBody(
558d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
5658eb238fSKowalski, Kamil     const crow::Request& req)
5758eb238fSKowalski, Kamil {
581aa0c2b8SEd Tanous     nlohmann::json reqJson;
591aa0c2b8SEd Tanous     JsonParseResult ret = parseRequestAsJson(req, reqJson);
601aa0c2b8SEd Tanous     if (ret != JsonParseResult::Success)
6158eb238fSKowalski, Kamil     {
6258eb238fSKowalski, Kamil         // We did not receive JSON request, proceed as it is RAW data
6333c6b580SEd Tanous         return req.body();
6458eb238fSKowalski, Kamil     }
6558eb238fSKowalski, Kamil 
6658eb238fSKowalski, Kamil     std::string certificate;
6758eb238fSKowalski, Kamil     std::optional<std::string> certificateType = "PEM";
6858eb238fSKowalski, Kamil 
69afc474aeSMyung Bae     if (!json_util::readJsonPatch( //
70afc474aeSMyung Bae             req, asyncResp->res, //
71afc474aeSMyung Bae             "CertificateString", certificate, //
72afc474aeSMyung Bae             "CertificateType", certificateType //
73afc474aeSMyung Bae             ))
7458eb238fSKowalski, Kamil     {
7562598e31SEd Tanous         BMCWEB_LOG_ERROR("Required parameters are missing");
7658eb238fSKowalski, Kamil         messages::internalError(asyncResp->res);
77abb93cddSEd Tanous         return {};
7858eb238fSKowalski, Kamil     }
7958eb238fSKowalski, Kamil 
8058eb238fSKowalski, Kamil     if (*certificateType != "PEM")
8158eb238fSKowalski, Kamil     {
8258eb238fSKowalski, Kamil         messages::propertyValueNotInList(asyncResp->res, *certificateType,
8358eb238fSKowalski, Kamil                                          "CertificateType");
84abb93cddSEd Tanous         return {};
8558eb238fSKowalski, Kamil     }
8658eb238fSKowalski, Kamil 
8758eb238fSKowalski, Kamil     return certificate;
8858eb238fSKowalski, Kamil }
8958eb238fSKowalski, Kamil 
905968caeeSMarri Devender Rao /**
915968caeeSMarri Devender Rao  * Class to create a temporary certificate file for uploading to system
925968caeeSMarri Devender Rao  */
935968caeeSMarri Devender Rao class CertificateFile
945968caeeSMarri Devender Rao {
955968caeeSMarri Devender Rao   public:
965968caeeSMarri Devender Rao     CertificateFile() = delete;
975968caeeSMarri Devender Rao     CertificateFile(const CertificateFile&) = delete;
985968caeeSMarri Devender Rao     CertificateFile& operator=(const CertificateFile&) = delete;
995968caeeSMarri Devender Rao     CertificateFile(CertificateFile&&) = delete;
1005968caeeSMarri Devender Rao     CertificateFile& operator=(CertificateFile&&) = delete;
1014e23a444SEd Tanous     explicit CertificateFile(const std::string& certString)
1025968caeeSMarri Devender Rao     {
10372d52d25SEd Tanous         std::array<char, 18> dirTemplate = {'/', 't', 'm', 'p', '/', 'C',
1045207438cSEd Tanous                                             'e', 'r', 't', 's', '.', 'X',
1055207438cSEd Tanous                                             'X', 'X', 'X', 'X', 'X', '\0'};
1065207438cSEd Tanous         char* tempDirectory = mkdtemp(dirTemplate.data());
107e662eae8SEd Tanous         if (tempDirectory != nullptr)
1085968caeeSMarri Devender Rao         {
1095968caeeSMarri Devender Rao             certDirectory = tempDirectory;
1105968caeeSMarri Devender Rao             certificateFile = certDirectory / "cert.pem";
111bd79bce8SPatrick Williams             std::ofstream out(certificateFile,
112bd79bce8SPatrick Williams                               std::ofstream::out | std::ofstream::binary |
1135968caeeSMarri Devender Rao                                   std::ofstream::trunc);
1145968caeeSMarri Devender Rao             out << certString;
1155968caeeSMarri Devender Rao             out.close();
11662598e31SEd Tanous             BMCWEB_LOG_DEBUG("Creating certificate file{}",
11762598e31SEd Tanous                              certificateFile.string());
1185968caeeSMarri Devender Rao         }
1195968caeeSMarri Devender Rao     }
1205968caeeSMarri Devender Rao     ~CertificateFile()
1215968caeeSMarri Devender Rao     {
1225968caeeSMarri Devender Rao         if (std::filesystem::exists(certDirectory))
1235968caeeSMarri Devender Rao         {
12462598e31SEd Tanous             BMCWEB_LOG_DEBUG("Removing certificate file{}",
12562598e31SEd Tanous                              certificateFile.string());
12623a21a1cSEd Tanous             std::error_code ec;
12723a21a1cSEd Tanous             std::filesystem::remove_all(certDirectory, ec);
12823a21a1cSEd Tanous             if (ec)
1295968caeeSMarri Devender Rao             {
13062598e31SEd Tanous                 BMCWEB_LOG_ERROR("Failed to remove temp directory{}",
13162598e31SEd Tanous                                  certDirectory.string());
1325968caeeSMarri Devender Rao             }
1335968caeeSMarri Devender Rao         }
1345968caeeSMarri Devender Rao     }
1355968caeeSMarri Devender Rao     std::string getCertFilePath()
1365968caeeSMarri Devender Rao     {
1375968caeeSMarri Devender Rao         return certificateFile;
1385968caeeSMarri Devender Rao     }
1395968caeeSMarri Devender Rao 
1405968caeeSMarri Devender Rao   private:
1415968caeeSMarri Devender Rao     std::filesystem::path certificateFile;
1425968caeeSMarri Devender Rao     std::filesystem::path certDirectory;
1435968caeeSMarri Devender Rao };
1445968caeeSMarri Devender Rao 
1455968caeeSMarri Devender Rao /**
1464e0453b1SGunnar Mills  * @brief Parse and update Certificate Issue/Subject property
1475968caeeSMarri Devender Rao  *
1485968caeeSMarri Devender Rao  * @param[in] asyncResp Shared pointer to the response message
1495968caeeSMarri Devender Rao  * @param[in] str  Issuer/Subject value in key=value pairs
1505968caeeSMarri Devender Rao  * @param[in] type Issuer/Subject
1515968caeeSMarri Devender Rao  * @return None
1525968caeeSMarri Devender Rao  */
1534ff0f1f4SEd Tanous inline void updateCertIssuerOrSubject(nlohmann::json& out,
15426ccae32SEd Tanous                                       std::string_view value)
1555968caeeSMarri Devender Rao {
1565968caeeSMarri Devender Rao     // example: O=openbmc-project.xyz,CN=localhost
1575968caeeSMarri Devender Rao     std::string_view::iterator i = value.begin();
1585968caeeSMarri Devender Rao     while (i != value.end())
1595968caeeSMarri Devender Rao     {
1605968caeeSMarri Devender Rao         std::string_view::iterator tokenBegin = i;
1615968caeeSMarri Devender Rao         while (i != value.end() && *i != '=')
1625968caeeSMarri Devender Rao         {
1636da47babSPatrick Williams             std::advance(i, 1);
1645968caeeSMarri Devender Rao         }
1655968caeeSMarri Devender Rao         if (i == value.end())
1665968caeeSMarri Devender Rao         {
1675968caeeSMarri Devender Rao             break;
1685968caeeSMarri Devender Rao         }
16926ccae32SEd Tanous         std::string_view key(tokenBegin, static_cast<size_t>(i - tokenBegin));
1706da47babSPatrick Williams         std::advance(i, 1);
1715968caeeSMarri Devender Rao         tokenBegin = i;
1725968caeeSMarri Devender Rao         while (i != value.end() && *i != ',')
1735968caeeSMarri Devender Rao         {
1746da47babSPatrick Williams             std::advance(i, 1);
1755968caeeSMarri Devender Rao         }
17626ccae32SEd Tanous         std::string_view val(tokenBegin, static_cast<size_t>(i - tokenBegin));
1775968caeeSMarri Devender Rao         if (key == "L")
1785968caeeSMarri Devender Rao         {
1795968caeeSMarri Devender Rao             out["City"] = val;
1805968caeeSMarri Devender Rao         }
1815968caeeSMarri Devender Rao         else if (key == "CN")
1825968caeeSMarri Devender Rao         {
1835968caeeSMarri Devender Rao             out["CommonName"] = val;
1845968caeeSMarri Devender Rao         }
1855968caeeSMarri Devender Rao         else if (key == "C")
1865968caeeSMarri Devender Rao         {
1875968caeeSMarri Devender Rao             out["Country"] = val;
1885968caeeSMarri Devender Rao         }
1895968caeeSMarri Devender Rao         else if (key == "O")
1905968caeeSMarri Devender Rao         {
1915968caeeSMarri Devender Rao             out["Organization"] = val;
1925968caeeSMarri Devender Rao         }
1935968caeeSMarri Devender Rao         else if (key == "OU")
1945968caeeSMarri Devender Rao         {
1955968caeeSMarri Devender Rao             out["OrganizationalUnit"] = val;
1965968caeeSMarri Devender Rao         }
1975968caeeSMarri Devender Rao         else if (key == "ST")
1985968caeeSMarri Devender Rao         {
1995968caeeSMarri Devender Rao             out["State"] = val;
2005968caeeSMarri Devender Rao         }
2015968caeeSMarri Devender Rao         // skip comma character
2025968caeeSMarri Devender Rao         if (i != value.end())
2035968caeeSMarri Devender Rao         {
2046da47babSPatrick Williams             std::advance(i, 1);
2055968caeeSMarri Devender Rao         }
2065968caeeSMarri Devender Rao     }
2075968caeeSMarri Devender Rao }
2085968caeeSMarri Devender Rao 
2095968caeeSMarri Devender Rao /**
210d3f92ce7SJiaqing Zhao  * @brief Retrieve the installed certificate list
211d3f92ce7SJiaqing Zhao  *
212d3f92ce7SJiaqing Zhao  * @param[in] asyncResp Shared pointer to the response message
213d3f92ce7SJiaqing Zhao  * @param[in] basePath DBus object path to search
214d3f92ce7SJiaqing Zhao  * @param[in] listPtr Json pointer to the list in asyncResp
215d3f92ce7SJiaqing Zhao  * @param[in] countPtr Json pointer to the count in asyncResp
216d3f92ce7SJiaqing Zhao  * @return None
217d3f92ce7SJiaqing Zhao  */
2184ff0f1f4SEd Tanous inline void getCertificateList(
219bd79bce8SPatrick Williams     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
220bd79bce8SPatrick Williams     const std::string& basePath, const nlohmann::json::json_pointer& listPtr,
221d3f92ce7SJiaqing Zhao     const nlohmann::json::json_pointer& countPtr)
222d3f92ce7SJiaqing Zhao {
2237a1dbc48SGeorge Liu     constexpr std::array<std::string_view, 1> interfaces = {
2247a1dbc48SGeorge Liu         certs::certPropIntf};
2257a1dbc48SGeorge Liu     dbus::utility::getSubTreePaths(
2267a1dbc48SGeorge Liu         basePath, 0, interfaces,
227d3f92ce7SJiaqing Zhao         [asyncResp, listPtr, countPtr](
2287a1dbc48SGeorge Liu             const boost::system::error_code& ec,
229d3f92ce7SJiaqing Zhao             const dbus::utility::MapperGetSubTreePathsResponse& certPaths) {
230d3f92ce7SJiaqing Zhao             if (ec)
231d3f92ce7SJiaqing Zhao             {
23262598e31SEd Tanous                 BMCWEB_LOG_ERROR("Certificate collection query failed: {}", ec);
233d3f92ce7SJiaqing Zhao                 messages::internalError(asyncResp->res);
234d3f92ce7SJiaqing Zhao                 return;
235d3f92ce7SJiaqing Zhao             }
236d3f92ce7SJiaqing Zhao 
237d3f92ce7SJiaqing Zhao             nlohmann::json& links = asyncResp->res.jsonValue[listPtr];
238d3f92ce7SJiaqing Zhao             links = nlohmann::json::array();
239d3f92ce7SJiaqing Zhao             for (const auto& certPath : certPaths)
240d3f92ce7SJiaqing Zhao             {
241d3f92ce7SJiaqing Zhao                 sdbusplus::message::object_path objPath(certPath);
242d3f92ce7SJiaqing Zhao                 std::string certId = objPath.filename();
243d3f92ce7SJiaqing Zhao                 if (certId.empty())
244d3f92ce7SJiaqing Zhao                 {
245bd79bce8SPatrick Williams                     BMCWEB_LOG_ERROR("Invalid certificate objPath {}",
246bd79bce8SPatrick Williams                                      certPath);
247d3f92ce7SJiaqing Zhao                     continue;
248d3f92ce7SJiaqing Zhao                 }
249d3f92ce7SJiaqing Zhao 
250d3f92ce7SJiaqing Zhao                 boost::urls::url certURL;
251d3f92ce7SJiaqing Zhao                 if (objPath.parent_path() == certs::httpsObjectPath)
252d3f92ce7SJiaqing Zhao                 {
253ef4c65b7SEd Tanous                     certURL = boost::urls::format(
254253f11b8SEd Tanous                         "/redfish/v1/Managers/{}/NetworkProtocol/HTTPS/Certificates/{}",
255253f11b8SEd Tanous                         BMCWEB_REDFISH_MANAGER_URI_NAME, certId);
256d3f92ce7SJiaqing Zhao                 }
257d3f92ce7SJiaqing Zhao                 else if (objPath.parent_path() == certs::ldapObjectPath)
258d3f92ce7SJiaqing Zhao                 {
259ef4c65b7SEd Tanous                     certURL = boost::urls::format(
260bd79bce8SPatrick Williams                         "/redfish/v1/AccountService/LDAP/Certificates/{}",
261bd79bce8SPatrick Williams                         certId);
262d3f92ce7SJiaqing Zhao                 }
263d3f92ce7SJiaqing Zhao                 else if (objPath.parent_path() == certs::authorityObjectPath)
264d3f92ce7SJiaqing Zhao                 {
265ef4c65b7SEd Tanous                     certURL = boost::urls::format(
266253f11b8SEd Tanous                         "/redfish/v1/Managers/{}/Truststore/Certificates/{}",
267253f11b8SEd Tanous                         BMCWEB_REDFISH_MANAGER_URI_NAME, certId);
268d3f92ce7SJiaqing Zhao                 }
269d3f92ce7SJiaqing Zhao                 else
270d3f92ce7SJiaqing Zhao                 {
271d3f92ce7SJiaqing Zhao                     continue;
272d3f92ce7SJiaqing Zhao                 }
273d3f92ce7SJiaqing Zhao 
274d3f92ce7SJiaqing Zhao                 nlohmann::json::object_t link;
275d3f92ce7SJiaqing Zhao                 link["@odata.id"] = certURL;
276d3f92ce7SJiaqing Zhao                 links.emplace_back(std::move(link));
277d3f92ce7SJiaqing Zhao             }
278d3f92ce7SJiaqing Zhao 
279d3f92ce7SJiaqing Zhao             asyncResp->res.jsonValue[countPtr] = links.size();
2807a1dbc48SGeorge Liu         });
281d3f92ce7SJiaqing Zhao }
282d3f92ce7SJiaqing Zhao 
283d3f92ce7SJiaqing Zhao /**
2845968caeeSMarri Devender Rao  * @brief Retrieve the certificates properties and append to the response
2855968caeeSMarri Devender Rao  * message
2865968caeeSMarri Devender Rao  *
2875968caeeSMarri Devender Rao  * @param[in] asyncResp Shared pointer to the response message
2885968caeeSMarri Devender Rao  * @param[in] objectPath  Path of the D-Bus service object
2895968caeeSMarri Devender Rao  * @param[in] certId  Id of the certificate
2905968caeeSMarri Devender Rao  * @param[in] certURL  URL of the certificate object
2915968caeeSMarri Devender Rao  * @param[in] name  name of the certificate
2925968caeeSMarri Devender Rao  * @return None
2935968caeeSMarri Devender Rao  */
2944ff0f1f4SEd Tanous inline void getCertificateProperties(
2958d1b46d7Szhanghch05     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
296e19e97e2SJiaqing Zhao     const std::string& objectPath, const std::string& service,
2971e312598SJiaqing Zhao     const std::string& certId, const boost::urls::url& certURL,
298e19e97e2SJiaqing Zhao     const std::string& name)
2995968caeeSMarri Devender Rao {
30062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getCertificateProperties Path={} certId={} certURl={}",
30162598e31SEd Tanous                      objectPath, certId, certURL);
302*deae6a78SEd Tanous     dbus::utility::getAllProperties(
303*deae6a78SEd Tanous         service, objectPath, certs::certPropIntf,
304b9d36b47SEd Tanous         [asyncResp, certURL, certId,
3055e7e2dc5SEd Tanous          name](const boost::system::error_code& ec,
306b9d36b47SEd Tanous                const dbus::utility::DBusPropertiesMap& properties) {
3075968caeeSMarri Devender Rao             if (ec)
3085968caeeSMarri Devender Rao             {
30962598e31SEd Tanous                 BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
310bd79bce8SPatrick Williams                 messages::resourceNotFound(asyncResp->res, "Certificate",
311bd79bce8SPatrick Williams                                            certId);
3125968caeeSMarri Devender Rao                 return;
3135968caeeSMarri Devender Rao             }
3149b12d1f9SKrzysztof Grobelny 
3159b12d1f9SKrzysztof Grobelny             const std::string* certificateString = nullptr;
3169b12d1f9SKrzysztof Grobelny             const std::vector<std::string>* keyUsage = nullptr;
3179b12d1f9SKrzysztof Grobelny             const std::string* issuer = nullptr;
3189b12d1f9SKrzysztof Grobelny             const std::string* subject = nullptr;
3199b12d1f9SKrzysztof Grobelny             const uint64_t* validNotAfter = nullptr;
3209b12d1f9SKrzysztof Grobelny             const uint64_t* validNotBefore = nullptr;
3219b12d1f9SKrzysztof Grobelny 
3229b12d1f9SKrzysztof Grobelny             const bool success = sdbusplus::unpackPropertiesNoThrow(
323bd79bce8SPatrick Williams                 dbus_utils::UnpackErrorPrinter(), properties,
324bd79bce8SPatrick Williams                 "CertificateString", certificateString, "KeyUsage", keyUsage,
325bd79bce8SPatrick Williams                 "Issuer", issuer, "Subject", subject, "ValidNotAfter",
326bd79bce8SPatrick Williams                 validNotAfter, "ValidNotBefore", validNotBefore);
3279b12d1f9SKrzysztof Grobelny 
3289b12d1f9SKrzysztof Grobelny             if (!success)
3299b12d1f9SKrzysztof Grobelny             {
3309b12d1f9SKrzysztof Grobelny                 messages::internalError(asyncResp->res);
3319b12d1f9SKrzysztof Grobelny                 return;
3329b12d1f9SKrzysztof Grobelny             }
3339b12d1f9SKrzysztof Grobelny 
3341476687dSEd Tanous             asyncResp->res.jsonValue["@odata.id"] = certURL;
3351476687dSEd Tanous             asyncResp->res.jsonValue["@odata.type"] =
3361476687dSEd Tanous                 "#Certificate.v1_0_0.Certificate";
337e19e97e2SJiaqing Zhao             asyncResp->res.jsonValue["Id"] = certId;
3381476687dSEd Tanous             asyncResp->res.jsonValue["Name"] = name;
3391476687dSEd Tanous             asyncResp->res.jsonValue["Description"] = name;
3405968caeeSMarri Devender Rao             asyncResp->res.jsonValue["CertificateString"] = "";
3419b12d1f9SKrzysztof Grobelny             asyncResp->res.jsonValue["KeyUsage"] = nlohmann::json::array();
3429b12d1f9SKrzysztof Grobelny 
3439b12d1f9SKrzysztof Grobelny             if (certificateString != nullptr)
3445968caeeSMarri Devender Rao             {
345bd79bce8SPatrick Williams                 asyncResp->res.jsonValue["CertificateString"] =
346bd79bce8SPatrick Williams                     *certificateString;
3475968caeeSMarri Devender Rao             }
3489b12d1f9SKrzysztof Grobelny 
3499b12d1f9SKrzysztof Grobelny             if (keyUsage != nullptr)
3505968caeeSMarri Devender Rao             {
3519b12d1f9SKrzysztof Grobelny                 asyncResp->res.jsonValue["KeyUsage"] = *keyUsage;
3525968caeeSMarri Devender Rao             }
3539b12d1f9SKrzysztof Grobelny 
3549b12d1f9SKrzysztof Grobelny             if (issuer != nullptr)
3555968caeeSMarri Devender Rao             {
3569b12d1f9SKrzysztof Grobelny                 updateCertIssuerOrSubject(asyncResp->res.jsonValue["Issuer"],
3579b12d1f9SKrzysztof Grobelny                                           *issuer);
3585968caeeSMarri Devender Rao             }
3599b12d1f9SKrzysztof Grobelny 
3609b12d1f9SKrzysztof Grobelny             if (subject != nullptr)
3615968caeeSMarri Devender Rao             {
3629b12d1f9SKrzysztof Grobelny                 updateCertIssuerOrSubject(asyncResp->res.jsonValue["Subject"],
3639b12d1f9SKrzysztof Grobelny                                           *subject);
3645968caeeSMarri Devender Rao             }
3659b12d1f9SKrzysztof Grobelny 
3669b12d1f9SKrzysztof Grobelny             if (validNotAfter != nullptr)
3675968caeeSMarri Devender Rao             {
3685968caeeSMarri Devender Rao                 asyncResp->res.jsonValue["ValidNotAfter"] =
3699b12d1f9SKrzysztof Grobelny                     redfish::time_utils::getDateTimeUint(*validNotAfter);
3705968caeeSMarri Devender Rao             }
3719b12d1f9SKrzysztof Grobelny 
3729b12d1f9SKrzysztof Grobelny             if (validNotBefore != nullptr)
3735968caeeSMarri Devender Rao             {
3745968caeeSMarri Devender Rao                 asyncResp->res.jsonValue["ValidNotBefore"] =
3759b12d1f9SKrzysztof Grobelny                     redfish::time_utils::getDateTimeUint(*validNotBefore);
3765968caeeSMarri Devender Rao             }
3779b12d1f9SKrzysztof Grobelny 
3781e312598SJiaqing Zhao             asyncResp->res.addHeader(
379d9f6c621SEd Tanous                 boost::beast::http::field::location,
380d9f6c621SEd Tanous                 std::string_view(certURL.data(), certURL.size()));
3819b12d1f9SKrzysztof Grobelny         });
3825968caeeSMarri Devender Rao }
3835968caeeSMarri Devender Rao 
3844ff0f1f4SEd Tanous inline void
3857a3a8f7aSJiaqing Zhao     deleteCertificate(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
3867a3a8f7aSJiaqing Zhao                       const std::string& service,
3877a3a8f7aSJiaqing Zhao                       const sdbusplus::message::object_path& objectPath)
3887a3a8f7aSJiaqing Zhao {
3897a3a8f7aSJiaqing Zhao     crow::connections::systemBus->async_method_call(
3907a3a8f7aSJiaqing Zhao         [asyncResp,
3915e7e2dc5SEd Tanous          id{objectPath.filename()}](const boost::system::error_code& ec) {
3927a3a8f7aSJiaqing Zhao             if (ec)
3937a3a8f7aSJiaqing Zhao             {
3947a3a8f7aSJiaqing Zhao                 messages::resourceNotFound(asyncResp->res, "Certificate", id);
3957a3a8f7aSJiaqing Zhao                 return;
3967a3a8f7aSJiaqing Zhao             }
39762598e31SEd Tanous             BMCWEB_LOG_INFO("Certificate deleted");
3987a3a8f7aSJiaqing Zhao             asyncResp->res.result(boost::beast::http::status::no_content);
3997a3a8f7aSJiaqing Zhao         },
4007a3a8f7aSJiaqing Zhao         service, objectPath, certs::objDeleteIntf, "Delete");
4017a3a8f7aSJiaqing Zhao }
4027a3a8f7aSJiaqing Zhao 
403828252d5SJiaqing Zhao inline void handleCertificateServiceGet(
404828252d5SJiaqing Zhao     App& app, const crow::Request& req,
405828252d5SJiaqing Zhao     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
4065968caeeSMarri Devender Rao {
407828252d5SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
408828252d5SJiaqing Zhao     {
409828252d5SJiaqing Zhao         return;
410828252d5SJiaqing Zhao     }
411828252d5SJiaqing Zhao 
4123e72c202SNinad Palsule     if (req.session == nullptr)
4133e72c202SNinad Palsule     {
4143e72c202SNinad Palsule         messages::internalError(asyncResp->res);
4153e72c202SNinad Palsule         return;
4163e72c202SNinad Palsule     }
4173e72c202SNinad Palsule 
418828252d5SJiaqing Zhao     asyncResp->res.jsonValue["@odata.type"] =
419828252d5SJiaqing Zhao         "#CertificateService.v1_0_0.CertificateService";
420828252d5SJiaqing Zhao     asyncResp->res.jsonValue["@odata.id"] = "/redfish/v1/CertificateService";
421828252d5SJiaqing Zhao     asyncResp->res.jsonValue["Id"] = "CertificateService";
422828252d5SJiaqing Zhao     asyncResp->res.jsonValue["Name"] = "Certificate Service";
423828252d5SJiaqing Zhao     asyncResp->res.jsonValue["Description"] =
424828252d5SJiaqing Zhao         "Actions available to manage certificates";
425828252d5SJiaqing Zhao     // /redfish/v1/CertificateService/CertificateLocations is something
426828252d5SJiaqing Zhao     // only ConfigureManager can access then only display when the user
427828252d5SJiaqing Zhao     // has permissions ConfigureManager
428828252d5SJiaqing Zhao     Privileges effectiveUserPrivileges =
4293e72c202SNinad Palsule         redfish::getUserPrivileges(*req.session);
430828252d5SJiaqing Zhao     if (isOperationAllowedWithPrivileges({{"ConfigureManager"}},
431828252d5SJiaqing Zhao                                          effectiveUserPrivileges))
432828252d5SJiaqing Zhao     {
433828252d5SJiaqing Zhao         asyncResp->res.jsonValue["CertificateLocations"]["@odata.id"] =
434828252d5SJiaqing Zhao             "/redfish/v1/CertificateService/CertificateLocations";
435828252d5SJiaqing Zhao     }
436828252d5SJiaqing Zhao     nlohmann::json& actions = asyncResp->res.jsonValue["Actions"];
437828252d5SJiaqing Zhao     nlohmann::json& replace = actions["#CertificateService.ReplaceCertificate"];
438828252d5SJiaqing Zhao     replace["target"] =
439828252d5SJiaqing Zhao         "/redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate";
440828252d5SJiaqing Zhao     nlohmann::json::array_t allowed;
441ad539545SPatrick Williams     allowed.emplace_back("PEM");
442828252d5SJiaqing Zhao     replace["CertificateType@Redfish.AllowableValues"] = std::move(allowed);
443828252d5SJiaqing Zhao     actions["#CertificateService.GenerateCSR"]["target"] =
444828252d5SJiaqing Zhao         "/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR";
445828252d5SJiaqing Zhao }
446828252d5SJiaqing Zhao 
447828252d5SJiaqing Zhao inline void handleCertificateLocationsGet(
448828252d5SJiaqing Zhao     App& app, const crow::Request& req,
449828252d5SJiaqing Zhao     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
450828252d5SJiaqing Zhao {
451828252d5SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
452828252d5SJiaqing Zhao     {
453828252d5SJiaqing Zhao         return;
454828252d5SJiaqing Zhao     }
455828252d5SJiaqing Zhao     asyncResp->res.jsonValue["@odata.id"] =
456828252d5SJiaqing Zhao         "/redfish/v1/CertificateService/CertificateLocations";
457828252d5SJiaqing Zhao     asyncResp->res.jsonValue["@odata.type"] =
458828252d5SJiaqing Zhao         "#CertificateLocations.v1_0_0.CertificateLocations";
459828252d5SJiaqing Zhao     asyncResp->res.jsonValue["Name"] = "Certificate Locations";
460828252d5SJiaqing Zhao     asyncResp->res.jsonValue["Id"] = "CertificateLocations";
461828252d5SJiaqing Zhao     asyncResp->res.jsonValue["Description"] =
462828252d5SJiaqing Zhao         "Defines a resource that an administrator can use in order to "
463828252d5SJiaqing Zhao         "locate all certificates installed on a given service";
464828252d5SJiaqing Zhao 
465828252d5SJiaqing Zhao     getCertificateList(asyncResp, certs::baseObjectPath,
466828252d5SJiaqing Zhao                        "/Links/Certificates"_json_pointer,
467828252d5SJiaqing Zhao                        "/Links/Certificates@odata.count"_json_pointer);
468828252d5SJiaqing Zhao }
469828252d5SJiaqing Zhao 
47026d3b0fbSChandra Harkude inline void handleError(const std::string_view dbusErrorName,
47126d3b0fbSChandra Harkude                         const std::string& id, const std::string& certificate,
47226d3b0fbSChandra Harkude                         const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
47326d3b0fbSChandra Harkude {
47426d3b0fbSChandra Harkude     if (dbusErrorName == "org.freedesktop.DBus.Error.UnknownObject")
47526d3b0fbSChandra Harkude     {
47626d3b0fbSChandra Harkude         messages::resourceNotFound(asyncResp->res, "Certificate", id);
47726d3b0fbSChandra Harkude     }
47826d3b0fbSChandra Harkude     else if (dbusErrorName ==
47926d3b0fbSChandra Harkude              "xyz.openbmc_project.Certs.Error.InvalidCertificate")
48026d3b0fbSChandra Harkude     {
48126d3b0fbSChandra Harkude         messages::propertyValueIncorrect(asyncResp->res, "Certificate",
48226d3b0fbSChandra Harkude                                          certificate);
48326d3b0fbSChandra Harkude     }
48426d3b0fbSChandra Harkude     else
48526d3b0fbSChandra Harkude     {
48626d3b0fbSChandra Harkude         messages::internalError(asyncResp->res);
48726d3b0fbSChandra Harkude     }
48826d3b0fbSChandra Harkude }
48926d3b0fbSChandra Harkude 
490828252d5SJiaqing Zhao inline void handleReplaceCertificateAction(
491828252d5SJiaqing Zhao     App& app, const crow::Request& req,
492828252d5SJiaqing Zhao     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
493828252d5SJiaqing Zhao {
4943ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
49545ca1b86SEd Tanous     {
49645ca1b86SEd Tanous         return;
49745ca1b86SEd Tanous     }
4985968caeeSMarri Devender Rao     std::string certificate;
4997a31e336SEd Tanous     std::string certURI;
5005968caeeSMarri Devender Rao     std::optional<std::string> certificateType = "PEM";
5018d1b46d7Szhanghch05 
502afc474aeSMyung Bae     if (!json_util::readJsonAction( //
503afc474aeSMyung Bae             req, asyncResp->res, //
504afc474aeSMyung Bae             "CertificateString", certificate, //
505afc474aeSMyung Bae             "CertificateType", certificateType, //
506afc474aeSMyung Bae             "CertificateUri/@odata.id", certURI //
507afc474aeSMyung Bae             ))
5085968caeeSMarri Devender Rao     {
50962598e31SEd Tanous         BMCWEB_LOG_ERROR("Required parameters are missing");
5105968caeeSMarri Devender Rao         return;
5115968caeeSMarri Devender Rao     }
5125968caeeSMarri Devender Rao 
5135968caeeSMarri Devender Rao     if (!certificateType)
5145968caeeSMarri Devender Rao     {
5155968caeeSMarri Devender Rao         // should never happen, but it never hurts to be paranoid.
5165968caeeSMarri Devender Rao         return;
5175968caeeSMarri Devender Rao     }
5185968caeeSMarri Devender Rao     if (certificateType != "PEM")
5195968caeeSMarri Devender Rao     {
520828252d5SJiaqing Zhao         messages::actionParameterNotSupported(asyncResp->res, "CertificateType",
521828252d5SJiaqing Zhao                                               "ReplaceCertificate");
5225968caeeSMarri Devender Rao         return;
5235968caeeSMarri Devender Rao     }
5245968caeeSMarri Devender Rao 
52562598e31SEd Tanous     BMCWEB_LOG_INFO("Certificate URI to replace: {}", certURI);
5265968caeeSMarri Devender Rao 
5276fd29553SEd Tanous     boost::system::result<boost::urls::url> parsedUrl =
52875b63a2cSJiaqing Zhao         boost::urls::parse_relative_ref(certURI);
52975b63a2cSJiaqing Zhao     if (!parsedUrl)
5305968caeeSMarri Devender Rao     {
531828252d5SJiaqing Zhao         messages::actionParameterValueFormatError(
532828252d5SJiaqing Zhao             asyncResp->res, certURI, "CertificateUri", "ReplaceCertificate");
5335968caeeSMarri Devender Rao         return;
5345968caeeSMarri Devender Rao     }
53575b63a2cSJiaqing Zhao 
53675b63a2cSJiaqing Zhao     std::string id;
53775b63a2cSJiaqing Zhao     sdbusplus::message::object_path objectPath;
5385968caeeSMarri Devender Rao     std::string name;
53937cce918SMarri Devender Rao     std::string service;
540828252d5SJiaqing Zhao     if (crow::utility::readUrlSegments(*parsedUrl, "redfish", "v1", "Managers",
541828252d5SJiaqing Zhao                                        "bmc", "NetworkProtocol", "HTTPS",
542828252d5SJiaqing Zhao                                        "Certificates", std::ref(id)))
5435968caeeSMarri Devender Rao     {
54489492a15SPatrick Williams         objectPath = sdbusplus::message::object_path(certs::httpsObjectPath) /
54589492a15SPatrick Williams                      id;
5465968caeeSMarri Devender Rao         name = "HTTPS certificate";
54737cce918SMarri Devender Rao         service = certs::httpsServiceName;
54837cce918SMarri Devender Rao     }
54975b63a2cSJiaqing Zhao     else if (crow::utility::readUrlSegments(*parsedUrl, "redfish", "v1",
55075b63a2cSJiaqing Zhao                                             "AccountService", "LDAP",
55175b63a2cSJiaqing Zhao                                             "Certificates", std::ref(id)))
55237cce918SMarri Devender Rao     {
55389492a15SPatrick Williams         objectPath = sdbusplus::message::object_path(certs::ldapObjectPath) /
55489492a15SPatrick Williams                      id;
55537cce918SMarri Devender Rao         name = "LDAP certificate";
55637cce918SMarri Devender Rao         service = certs::ldapServiceName;
5575968caeeSMarri Devender Rao     }
55875b63a2cSJiaqing Zhao     else if (crow::utility::readUrlSegments(*parsedUrl, "redfish", "v1",
55975b63a2cSJiaqing Zhao                                             "Managers", "bmc", "Truststore",
56075b63a2cSJiaqing Zhao                                             "Certificates", std::ref(id)))
561cfcd5f6bSMarri Devender Rao     {
56275b63a2cSJiaqing Zhao         objectPath =
563828252d5SJiaqing Zhao             sdbusplus::message::object_path(certs::authorityObjectPath) / id;
564cfcd5f6bSMarri Devender Rao         name = "TrustStore certificate";
565cfcd5f6bSMarri Devender Rao         service = certs::authorityServiceName;
566cfcd5f6bSMarri Devender Rao     }
5675968caeeSMarri Devender Rao     else
5685968caeeSMarri Devender Rao     {
569828252d5SJiaqing Zhao         messages::actionParameterNotSupported(asyncResp->res, "CertificateUri",
570828252d5SJiaqing Zhao                                               "ReplaceCertificate");
5715968caeeSMarri Devender Rao         return;
5725968caeeSMarri Devender Rao     }
5735968caeeSMarri Devender Rao 
5745968caeeSMarri Devender Rao     std::shared_ptr<CertificateFile> certFile =
5755968caeeSMarri Devender Rao         std::make_shared<CertificateFile>(certificate);
5765968caeeSMarri Devender Rao     crow::connections::systemBus->async_method_call(
57726d3b0fbSChandra Harkude         [asyncResp, certFile, objectPath, service, url{*parsedUrl}, id, name,
57826d3b0fbSChandra Harkude          certificate](const boost::system::error_code& ec,
579d3e0859cSPatrick Williams                       sdbusplus::message_t& m) {
5805968caeeSMarri Devender Rao             if (ec)
5815968caeeSMarri Devender Rao             {
58262598e31SEd Tanous                 BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
58326d3b0fbSChandra Harkude                 const sd_bus_error* dbusError = m.get_error();
58426d3b0fbSChandra Harkude                 if ((dbusError != nullptr) && (dbusError->name != nullptr))
58590d2d1e8SJiaqing Zhao                 {
58626d3b0fbSChandra Harkude                     handleError(dbusError->name, id, certificate, asyncResp);
5875968caeeSMarri Devender Rao                 }
58826d3b0fbSChandra Harkude                 else
58926d3b0fbSChandra Harkude                 {
59090d2d1e8SJiaqing Zhao                     messages::internalError(asyncResp->res);
59126d3b0fbSChandra Harkude                 }
59290d2d1e8SJiaqing Zhao                 return;
59390d2d1e8SJiaqing Zhao             }
594bd79bce8SPatrick Williams             getCertificateProperties(asyncResp, objectPath, service, id, url,
595bd79bce8SPatrick Williams                                      name);
59662598e31SEd Tanous             BMCWEB_LOG_DEBUG("HTTPS certificate install file={}",
59762598e31SEd Tanous                              certFile->getCertFilePath());
5985968caeeSMarri Devender Rao         },
5995968caeeSMarri Devender Rao         service, objectPath, certs::certReplaceIntf, "Replace",
6005968caeeSMarri Devender Rao         certFile->getCertFilePath());
601828252d5SJiaqing Zhao }
6025968caeeSMarri Devender Rao 
603cf9e417dSEd Tanous // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
604828252d5SJiaqing Zhao static std::unique_ptr<sdbusplus::bus::match_t> csrMatcher;
6055968caeeSMarri Devender Rao /**
606828252d5SJiaqing Zhao  * @brief Read data from CSR D-bus object and set to response
607828252d5SJiaqing Zhao  *
608828252d5SJiaqing Zhao  * @param[in] asyncResp Shared pointer to the response message
6098ece0e45SEd Tanous  * @param[in] certURI Link to certificate collection URI
610828252d5SJiaqing Zhao  * @param[in] service D-Bus service name
611828252d5SJiaqing Zhao  * @param[in] certObjPath certificate D-Bus object path
612828252d5SJiaqing Zhao  * @param[in] csrObjPath CSR D-Bus object path
613828252d5SJiaqing Zhao  * @return None
6145968caeeSMarri Devender Rao  */
6154ff0f1f4SEd Tanous inline void getCSR(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
616828252d5SJiaqing Zhao                    const std::string& certURI, const std::string& service,
617828252d5SJiaqing Zhao                    const std::string& certObjPath,
618828252d5SJiaqing Zhao                    const std::string& csrObjPath)
6195968caeeSMarri Devender Rao {
62062598e31SEd Tanous     BMCWEB_LOG_DEBUG("getCSR CertObjectPath{} CSRObjectPath={} service={}",
62162598e31SEd Tanous                      certObjPath, csrObjPath, service);
622828252d5SJiaqing Zhao     crow::connections::systemBus->async_method_call(
623bd79bce8SPatrick Williams         [asyncResp,
624bd79bce8SPatrick Williams          certURI](const boost::system::error_code& ec, const std::string& csr) {
625828252d5SJiaqing Zhao             if (ec)
626828252d5SJiaqing Zhao             {
62762598e31SEd Tanous                 BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
628828252d5SJiaqing Zhao                 messages::internalError(asyncResp->res);
629828252d5SJiaqing Zhao                 return;
630828252d5SJiaqing Zhao             }
631828252d5SJiaqing Zhao             if (csr.empty())
632828252d5SJiaqing Zhao             {
63362598e31SEd Tanous                 BMCWEB_LOG_ERROR("CSR read is empty");
634828252d5SJiaqing Zhao                 messages::internalError(asyncResp->res);
635828252d5SJiaqing Zhao                 return;
636828252d5SJiaqing Zhao             }
637828252d5SJiaqing Zhao             asyncResp->res.jsonValue["CSRString"] = csr;
638828252d5SJiaqing Zhao             asyncResp->res.jsonValue["CertificateCollection"]["@odata.id"] =
639828252d5SJiaqing Zhao                 certURI;
640828252d5SJiaqing Zhao         },
641828252d5SJiaqing Zhao         service, csrObjPath, "xyz.openbmc_project.Certs.CSR", "CSR");
642828252d5SJiaqing Zhao }
643828252d5SJiaqing Zhao 
644828252d5SJiaqing Zhao inline void
645828252d5SJiaqing Zhao     handleGenerateCSRAction(App& app, const crow::Request& req,
646828252d5SJiaqing Zhao                             const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
647828252d5SJiaqing Zhao {
6483ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
64945ca1b86SEd Tanous     {
65045ca1b86SEd Tanous         return;
65145ca1b86SEd Tanous     }
652828252d5SJiaqing Zhao     static const int rsaKeyBitLength = 2048;
6535968caeeSMarri Devender Rao 
654828252d5SJiaqing Zhao     // Required parameters
655828252d5SJiaqing Zhao     std::string city;
656828252d5SJiaqing Zhao     std::string commonName;
657828252d5SJiaqing Zhao     std::string country;
658828252d5SJiaqing Zhao     std::string organization;
659828252d5SJiaqing Zhao     std::string organizationalUnit;
660828252d5SJiaqing Zhao     std::string state;
6617a31e336SEd Tanous     std::string certURI;
662828252d5SJiaqing Zhao 
663828252d5SJiaqing Zhao     // Optional parameters
664828252d5SJiaqing Zhao     std::optional<std::vector<std::string>> optAlternativeNames =
665828252d5SJiaqing Zhao         std::vector<std::string>();
666828252d5SJiaqing Zhao     std::optional<std::string> optContactPerson = "";
667828252d5SJiaqing Zhao     std::optional<std::string> optChallengePassword = "";
668828252d5SJiaqing Zhao     std::optional<std::string> optEmail = "";
669828252d5SJiaqing Zhao     std::optional<std::string> optGivenName = "";
670828252d5SJiaqing Zhao     std::optional<std::string> optInitials = "";
671828252d5SJiaqing Zhao     std::optional<int64_t> optKeyBitLength = rsaKeyBitLength;
672828252d5SJiaqing Zhao     std::optional<std::string> optKeyCurveId = "secp384r1";
673828252d5SJiaqing Zhao     std::optional<std::string> optKeyPairAlgorithm = "EC";
674828252d5SJiaqing Zhao     std::optional<std::vector<std::string>> optKeyUsage =
675828252d5SJiaqing Zhao         std::vector<std::string>();
676828252d5SJiaqing Zhao     std::optional<std::string> optSurname = "";
677828252d5SJiaqing Zhao     std::optional<std::string> optUnstructuredName = "";
678afc474aeSMyung Bae     if (!json_util::readJsonAction( //
679afc474aeSMyung Bae             req, asyncResp->res, //
680afc474aeSMyung Bae             "AlternativeNames", optAlternativeNames, //
681afc474aeSMyung Bae             "CertificateCollection/@odata.id", certURI, //
682afc474aeSMyung Bae             "ChallengePassword", optChallengePassword, //
683afc474aeSMyung Bae             "City", city, //
684afc474aeSMyung Bae             "CommonName", commonName, //
685afc474aeSMyung Bae             "ContactPerson", optContactPerson, //
686afc474aeSMyung Bae             "Country", country, //
687afc474aeSMyung Bae             "Email", optEmail, //
688afc474aeSMyung Bae             "GivenName", optGivenName, //
689afc474aeSMyung Bae             "Initials", optInitials, //
690afc474aeSMyung Bae             "KeyBitLength", optKeyBitLength, //
691afc474aeSMyung Bae             "KeyCurveId", optKeyCurveId, //
692afc474aeSMyung Bae             "KeyPairAlgorithm", optKeyPairAlgorithm, //
693afc474aeSMyung Bae             "KeyUsage", optKeyUsage, //
694afc474aeSMyung Bae             "Organization", organization, //
695afc474aeSMyung Bae             "OrganizationalUnit", organizationalUnit, //
696afc474aeSMyung Bae             "State", state, //
697afc474aeSMyung Bae             "Surname", optSurname, //
698afc474aeSMyung Bae             "UnstructuredName", optUnstructuredName //
699afc474aeSMyung Bae             ))
700828252d5SJiaqing Zhao     {
701828252d5SJiaqing Zhao         return;
7025968caeeSMarri Devender Rao     }
7035968caeeSMarri Devender Rao 
704828252d5SJiaqing Zhao     // bmcweb has no way to store or decode a private key challenge
705828252d5SJiaqing Zhao     // password, which will likely cause bmcweb to crash on startup
706828252d5SJiaqing Zhao     // if this is not set on a post so not allowing the user to set
707828252d5SJiaqing Zhao     // value
708828252d5SJiaqing Zhao     if (!optChallengePassword->empty())
7095968caeeSMarri Devender Rao     {
710828252d5SJiaqing Zhao         messages::actionParameterNotSupported(asyncResp->res, "GenerateCSR",
711828252d5SJiaqing Zhao                                               "ChallengePassword");
712828252d5SJiaqing Zhao         return;
713828252d5SJiaqing Zhao     }
714828252d5SJiaqing Zhao 
715828252d5SJiaqing Zhao     std::string objectPath;
716828252d5SJiaqing Zhao     std::string service;
717253f11b8SEd Tanous     if (certURI.starts_with(std::format(
718253f11b8SEd Tanous             "/redfish/v1/Managers/{}/NetworkProtocol/HTTPS/Certificates",
719253f11b8SEd Tanous             BMCWEB_REDFISH_MANAGER_URI_NAME)))
720828252d5SJiaqing Zhao     {
721828252d5SJiaqing Zhao         objectPath = certs::httpsObjectPath;
722828252d5SJiaqing Zhao         service = certs::httpsServiceName;
723828252d5SJiaqing Zhao     }
724828252d5SJiaqing Zhao     else if (certURI.starts_with(
725828252d5SJiaqing Zhao                  "/redfish/v1/AccountService/LDAP/Certificates"))
726828252d5SJiaqing Zhao     {
727828252d5SJiaqing Zhao         objectPath = certs::ldapObjectPath;
728828252d5SJiaqing Zhao         service = certs::ldapServiceName;
729828252d5SJiaqing Zhao     }
730828252d5SJiaqing Zhao     else
731828252d5SJiaqing Zhao     {
732828252d5SJiaqing Zhao         messages::actionParameterNotSupported(
733828252d5SJiaqing Zhao             asyncResp->res, "CertificateCollection", "GenerateCSR");
734828252d5SJiaqing Zhao         return;
735828252d5SJiaqing Zhao     }
736828252d5SJiaqing Zhao 
737828252d5SJiaqing Zhao     // supporting only EC and RSA algorithm
738828252d5SJiaqing Zhao     if (*optKeyPairAlgorithm != "EC" && *optKeyPairAlgorithm != "RSA")
739828252d5SJiaqing Zhao     {
740828252d5SJiaqing Zhao         messages::actionParameterNotSupported(
741828252d5SJiaqing Zhao             asyncResp->res, "KeyPairAlgorithm", "GenerateCSR");
742828252d5SJiaqing Zhao         return;
743828252d5SJiaqing Zhao     }
744828252d5SJiaqing Zhao 
745828252d5SJiaqing Zhao     // supporting only 2048 key bit length for RSA algorithm due to
746828252d5SJiaqing Zhao     // time consumed in generating private key
747828252d5SJiaqing Zhao     if (*optKeyPairAlgorithm == "RSA" && *optKeyBitLength != rsaKeyBitLength)
748828252d5SJiaqing Zhao     {
749e2616cc5SEd Tanous         messages::propertyValueNotInList(asyncResp->res, *optKeyBitLength,
750e2616cc5SEd Tanous                                          "KeyBitLength");
751828252d5SJiaqing Zhao         return;
752828252d5SJiaqing Zhao     }
753828252d5SJiaqing Zhao 
754828252d5SJiaqing Zhao     // validate KeyUsage supporting only 1 type based on URL
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         if (optKeyUsage->empty())
760828252d5SJiaqing Zhao         {
761b2ba3072SPatrick Williams             optKeyUsage->emplace_back("ServerAuthentication");
762828252d5SJiaqing Zhao         }
763828252d5SJiaqing Zhao         else if (optKeyUsage->size() == 1)
764828252d5SJiaqing Zhao         {
765828252d5SJiaqing Zhao             if ((*optKeyUsage)[0] != "ServerAuthentication")
766828252d5SJiaqing Zhao             {
767828252d5SJiaqing Zhao                 messages::propertyValueNotInList(asyncResp->res,
768828252d5SJiaqing Zhao                                                  (*optKeyUsage)[0], "KeyUsage");
769828252d5SJiaqing Zhao                 return;
770828252d5SJiaqing Zhao             }
771828252d5SJiaqing Zhao         }
772828252d5SJiaqing Zhao         else
773828252d5SJiaqing Zhao         {
774828252d5SJiaqing Zhao             messages::actionParameterNotSupported(asyncResp->res, "KeyUsage",
775828252d5SJiaqing Zhao                                                   "GenerateCSR");
776828252d5SJiaqing Zhao             return;
777828252d5SJiaqing Zhao         }
778828252d5SJiaqing Zhao     }
779828252d5SJiaqing Zhao     else if (certURI.starts_with(
780828252d5SJiaqing Zhao                  "/redfish/v1/AccountService/LDAP/Certificates"))
781828252d5SJiaqing Zhao     {
782828252d5SJiaqing Zhao         if (optKeyUsage->empty())
783828252d5SJiaqing Zhao         {
784b2ba3072SPatrick Williams             optKeyUsage->emplace_back("ClientAuthentication");
785828252d5SJiaqing Zhao         }
786828252d5SJiaqing Zhao         else if (optKeyUsage->size() == 1)
787828252d5SJiaqing Zhao         {
788828252d5SJiaqing Zhao             if ((*optKeyUsage)[0] != "ClientAuthentication")
789828252d5SJiaqing Zhao             {
790828252d5SJiaqing Zhao                 messages::propertyValueNotInList(asyncResp->res,
791828252d5SJiaqing Zhao                                                  (*optKeyUsage)[0], "KeyUsage");
792828252d5SJiaqing Zhao                 return;
793828252d5SJiaqing Zhao             }
794828252d5SJiaqing Zhao         }
795828252d5SJiaqing Zhao         else
796828252d5SJiaqing Zhao         {
797828252d5SJiaqing Zhao             messages::actionParameterNotSupported(asyncResp->res, "KeyUsage",
798828252d5SJiaqing Zhao                                                   "GenerateCSR");
799828252d5SJiaqing Zhao             return;
800828252d5SJiaqing Zhao         }
801828252d5SJiaqing Zhao     }
802828252d5SJiaqing Zhao 
803828252d5SJiaqing Zhao     // Only allow one CSR matcher at a time so setting retry
804828252d5SJiaqing Zhao     // time-out and timer expiry to 10 seconds for now.
805828252d5SJiaqing Zhao     static const int timeOut = 10;
806828252d5SJiaqing Zhao     if (csrMatcher)
807828252d5SJiaqing Zhao     {
808828252d5SJiaqing Zhao         messages::serviceTemporarilyUnavailable(asyncResp->res,
809828252d5SJiaqing Zhao                                                 std::to_string(timeOut));
810828252d5SJiaqing Zhao         return;
811828252d5SJiaqing Zhao     }
812828252d5SJiaqing Zhao 
8138e8245dbSEd Tanous     if (req.ioService == nullptr)
8148e8245dbSEd Tanous     {
8158e8245dbSEd Tanous         messages::internalError(asyncResp->res);
8168e8245dbSEd Tanous         return;
8178e8245dbSEd Tanous     }
8188e8245dbSEd Tanous 
819828252d5SJiaqing Zhao     // Make this static so it survives outside this method
820828252d5SJiaqing Zhao     static boost::asio::steady_timer timeout(*req.ioService);
821828252d5SJiaqing Zhao     timeout.expires_after(std::chrono::seconds(timeOut));
822828252d5SJiaqing Zhao     timeout.async_wait([asyncResp](const boost::system::error_code& ec) {
823828252d5SJiaqing Zhao         csrMatcher = nullptr;
824828252d5SJiaqing Zhao         if (ec)
825828252d5SJiaqing Zhao         {
826828252d5SJiaqing Zhao             // operation_aborted is expected if timer is canceled
827828252d5SJiaqing Zhao             // before completion.
828828252d5SJiaqing Zhao             if (ec != boost::asio::error::operation_aborted)
829828252d5SJiaqing Zhao             {
83062598e31SEd Tanous                 BMCWEB_LOG_ERROR("Async_wait failed {}", ec);
831828252d5SJiaqing Zhao             }
832828252d5SJiaqing Zhao             return;
833828252d5SJiaqing Zhao         }
83462598e31SEd Tanous         BMCWEB_LOG_ERROR("Timed out waiting for Generating CSR");
835828252d5SJiaqing Zhao         messages::internalError(asyncResp->res);
836828252d5SJiaqing Zhao     });
837828252d5SJiaqing Zhao 
838828252d5SJiaqing Zhao     // create a matcher to wait on CSR object
83962598e31SEd Tanous     BMCWEB_LOG_DEBUG("create matcher with path {}", objectPath);
840828252d5SJiaqing Zhao     std::string match("type='signal',"
841828252d5SJiaqing Zhao                       "interface='org.freedesktop.DBus.ObjectManager',"
842828252d5SJiaqing Zhao                       "path='" +
843828252d5SJiaqing Zhao                       objectPath +
844828252d5SJiaqing Zhao                       "',"
845828252d5SJiaqing Zhao                       "member='InterfacesAdded'");
846828252d5SJiaqing Zhao     csrMatcher = std::make_unique<sdbusplus::bus::match_t>(
847828252d5SJiaqing Zhao         *crow::connections::systemBus, match,
848828252d5SJiaqing Zhao         [asyncResp, service, objectPath, certURI](sdbusplus::message_t& m) {
849828252d5SJiaqing Zhao             timeout.cancel();
850828252d5SJiaqing Zhao             if (m.is_method_error())
851828252d5SJiaqing Zhao             {
85262598e31SEd Tanous                 BMCWEB_LOG_ERROR("Dbus method error!!!");
853828252d5SJiaqing Zhao                 messages::internalError(asyncResp->res);
854828252d5SJiaqing Zhao                 return;
855828252d5SJiaqing Zhao             }
856828252d5SJiaqing Zhao 
85780f79a40SMichael Shen             dbus::utility::DBusInterfacesMap interfacesProperties;
858828252d5SJiaqing Zhao 
859828252d5SJiaqing Zhao             sdbusplus::message::object_path csrObjectPath;
860828252d5SJiaqing Zhao             m.read(csrObjectPath, interfacesProperties);
86162598e31SEd Tanous             BMCWEB_LOG_DEBUG("CSR object added{}", csrObjectPath.str);
862828252d5SJiaqing Zhao             for (const auto& interface : interfacesProperties)
863828252d5SJiaqing Zhao             {
864828252d5SJiaqing Zhao                 if (interface.first == "xyz.openbmc_project.Certs.CSR")
865828252d5SJiaqing Zhao                 {
866828252d5SJiaqing Zhao                     getCSR(asyncResp, certURI, service, objectPath,
867828252d5SJiaqing Zhao                            csrObjectPath.str);
868828252d5SJiaqing Zhao                     break;
869828252d5SJiaqing Zhao                 }
870828252d5SJiaqing Zhao             }
871828252d5SJiaqing Zhao         });
872828252d5SJiaqing Zhao     crow::connections::systemBus->async_method_call(
8735e7e2dc5SEd Tanous         [asyncResp](const boost::system::error_code& ec, const std::string&) {
874828252d5SJiaqing Zhao             if (ec)
875828252d5SJiaqing Zhao             {
87662598e31SEd Tanous                 BMCWEB_LOG_ERROR("DBUS response error: {}", ec.message());
877828252d5SJiaqing Zhao                 messages::internalError(asyncResp->res);
878828252d5SJiaqing Zhao                 return;
879828252d5SJiaqing Zhao             }
880828252d5SJiaqing Zhao         },
881828252d5SJiaqing Zhao         service, objectPath, "xyz.openbmc_project.Certs.CSR.Create",
882828252d5SJiaqing Zhao         "GenerateCSR", *optAlternativeNames, *optChallengePassword, city,
883828252d5SJiaqing Zhao         commonName, *optContactPerson, country, *optEmail, *optGivenName,
884828252d5SJiaqing Zhao         *optInitials, *optKeyBitLength, *optKeyCurveId, *optKeyPairAlgorithm,
885828252d5SJiaqing Zhao         *optKeyUsage, organization, organizationalUnit, state, *optSurname,
886828252d5SJiaqing Zhao         *optUnstructuredName);
887828252d5SJiaqing Zhao }
888828252d5SJiaqing Zhao 
889828252d5SJiaqing Zhao inline void requestRoutesCertificateService(App& app)
890828252d5SJiaqing Zhao {
891828252d5SJiaqing Zhao     BMCWEB_ROUTE(app, "/redfish/v1/CertificateService/")
892828252d5SJiaqing Zhao         .privileges(redfish::privileges::getCertificateService)
893002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
894828252d5SJiaqing Zhao             std::bind_front(handleCertificateServiceGet, std::ref(app)));
895828252d5SJiaqing Zhao 
896828252d5SJiaqing Zhao     BMCWEB_ROUTE(app, "/redfish/v1/CertificateService/CertificateLocations/")
897828252d5SJiaqing Zhao         .privileges(redfish::privileges::getCertificateLocations)
898828252d5SJiaqing Zhao         .methods(boost::beast::http::verb::get)(
899828252d5SJiaqing Zhao             std::bind_front(handleCertificateLocationsGet, std::ref(app)));
900828252d5SJiaqing Zhao 
901828252d5SJiaqing Zhao     BMCWEB_ROUTE(
902828252d5SJiaqing Zhao         app,
903828252d5SJiaqing Zhao         "/redfish/v1/CertificateService/Actions/CertificateService.ReplaceCertificate/")
904828252d5SJiaqing Zhao         .privileges(redfish::privileges::postCertificateService)
905828252d5SJiaqing Zhao         .methods(boost::beast::http::verb::post)(
906828252d5SJiaqing Zhao             std::bind_front(handleReplaceCertificateAction, std::ref(app)));
907828252d5SJiaqing Zhao 
908828252d5SJiaqing Zhao     BMCWEB_ROUTE(
909828252d5SJiaqing Zhao         app,
910828252d5SJiaqing Zhao         "/redfish/v1/CertificateService/Actions/CertificateService.GenerateCSR/")
911828252d5SJiaqing Zhao         .privileges(redfish::privileges::postCertificateService)
912828252d5SJiaqing Zhao         .methods(boost::beast::http::verb::post)(
913828252d5SJiaqing Zhao             std::bind_front(handleGenerateCSRAction, std::ref(app)));
914828252d5SJiaqing Zhao } // requestRoutesCertificateService
915828252d5SJiaqing Zhao 
916828252d5SJiaqing Zhao inline void handleHTTPSCertificateCollectionGet(
917828252d5SJiaqing Zhao     App& app, const crow::Request& req,
918253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
919253f11b8SEd Tanous     const std::string& managerId)
920828252d5SJiaqing Zhao {
9213ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
92245ca1b86SEd Tanous     {
92345ca1b86SEd Tanous         return;
92445ca1b86SEd Tanous     }
9251476687dSEd Tanous 
926253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
927253f11b8SEd Tanous     {
928253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
929253f11b8SEd Tanous         return;
930253f11b8SEd Tanous     }
931253f11b8SEd Tanous 
932253f11b8SEd Tanous     asyncResp->res.jsonValue["@odata.id"] = boost::urls::format(
933253f11b8SEd Tanous         "/redfish/v1/Managers/{}/NetworkProtocol/HTTPS/Certificates",
934253f11b8SEd Tanous         BMCWEB_REDFISH_MANAGER_URI_NAME);
9351476687dSEd Tanous     asyncResp->res.jsonValue["@odata.type"] =
9361476687dSEd Tanous         "#CertificateCollection.CertificateCollection";
9371476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "HTTPS Certificates Collection";
9381476687dSEd Tanous     asyncResp->res.jsonValue["Description"] =
9391476687dSEd Tanous         "A Collection of HTTPS certificate instances";
9408d1b46d7Szhanghch05 
941d3f92ce7SJiaqing Zhao     getCertificateList(asyncResp, certs::httpsObjectPath,
942d3f92ce7SJiaqing Zhao                        "/Members"_json_pointer,
943d3f92ce7SJiaqing Zhao                        "/Members@odata.count"_json_pointer);
944828252d5SJiaqing Zhao }
9455968caeeSMarri Devender Rao 
946828252d5SJiaqing Zhao inline void handleHTTPSCertificateCollectionPost(
947828252d5SJiaqing Zhao     App& app, const crow::Request& req,
948253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
949253f11b8SEd Tanous     const std::string& managerId)
950828252d5SJiaqing Zhao {
9513ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
95245ca1b86SEd Tanous     {
95345ca1b86SEd Tanous         return;
95445ca1b86SEd Tanous     }
955253f11b8SEd Tanous 
956253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
957253f11b8SEd Tanous     {
958253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
959253f11b8SEd Tanous         return;
960253f11b8SEd Tanous     }
961253f11b8SEd Tanous 
96262598e31SEd Tanous     BMCWEB_LOG_DEBUG("HTTPSCertificateCollection::doPost");
9638d1b46d7Szhanghch05 
9641476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "HTTPS Certificate";
9651476687dSEd Tanous     asyncResp->res.jsonValue["Description"] = "HTTPS Certificate";
9665968caeeSMarri Devender Rao 
967b2896149SEd Tanous     std::string certHttpBody = getCertificateFromReqBody(asyncResp, req);
96858eb238fSKowalski, Kamil 
969b2896149SEd Tanous     if (certHttpBody.empty())
97058eb238fSKowalski, Kamil     {
97162598e31SEd Tanous         BMCWEB_LOG_ERROR("Cannot get certificate from request body.");
972a08752f5SZbigniew Kurzynski         messages::unrecognizedRequestBody(asyncResp->res);
97358eb238fSKowalski, Kamil         return;
97458eb238fSKowalski, Kamil     }
97558eb238fSKowalski, Kamil 
9765968caeeSMarri Devender Rao     std::shared_ptr<CertificateFile> certFile =
977b2896149SEd Tanous         std::make_shared<CertificateFile>(certHttpBody);
9785968caeeSMarri Devender Rao 
9795968caeeSMarri Devender Rao     crow::connections::systemBus->async_method_call(
9805e7e2dc5SEd Tanous         [asyncResp, certFile](const boost::system::error_code& ec,
981656ec7e3SZbigniew Kurzynski                               const std::string& objectPath) {
9825968caeeSMarri Devender Rao             if (ec)
9835968caeeSMarri Devender Rao             {
98462598e31SEd Tanous                 BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
9855968caeeSMarri Devender Rao                 messages::internalError(asyncResp->res);
9865968caeeSMarri Devender Rao                 return;
9875968caeeSMarri Devender Rao             }
988717b9802SJiaqing Zhao 
989717b9802SJiaqing Zhao             sdbusplus::message::object_path path(objectPath);
990717b9802SJiaqing Zhao             std::string certId = path.filename();
991ef4c65b7SEd Tanous             const boost::urls::url certURL = boost::urls::format(
992253f11b8SEd Tanous                 "/redfish/v1/Managers/{}/NetworkProtocol/HTTPS/Certificates/{}",
993253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME, certId);
994bd79bce8SPatrick Williams             getCertificateProperties(asyncResp, objectPath,
995bd79bce8SPatrick Williams                                      certs::httpsServiceName, certId, certURL,
996bd79bce8SPatrick Williams                                      "HTTPS Certificate");
99762598e31SEd Tanous             BMCWEB_LOG_DEBUG("HTTPS certificate install file={}",
99862598e31SEd Tanous                              certFile->getCertFilePath());
9995968caeeSMarri Devender Rao         },
1000828252d5SJiaqing Zhao         certs::httpsServiceName, certs::httpsObjectPath, certs::certInstallIntf,
1001828252d5SJiaqing Zhao         "Install", certFile->getCertFilePath());
1002828252d5SJiaqing Zhao }
10035968caeeSMarri Devender Rao 
1004828252d5SJiaqing Zhao inline void handleHTTPSCertificateGet(
1005828252d5SJiaqing Zhao     App& app, const crow::Request& req,
1006253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1007253f11b8SEd Tanous     const std::string& managerId, const std::string& certId)
10087e860f15SJohn Edward Broadbent {
10093ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
101045ca1b86SEd Tanous     {
101145ca1b86SEd Tanous         return;
101245ca1b86SEd Tanous     }
10137e860f15SJohn Edward Broadbent 
1014253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
1015253f11b8SEd Tanous     {
1016253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
1017253f11b8SEd Tanous         return;
1018253f11b8SEd Tanous     }
1019253f11b8SEd Tanous 
1020253f11b8SEd Tanous     BMCWEB_LOG_DEBUG("HTTPS Certificate ID={}", certId);
1021ef4c65b7SEd Tanous     const boost::urls::url certURL = boost::urls::format(
1022253f11b8SEd Tanous         "/redfish/v1/Managers/{}/NetworkProtocol/HTTPS/Certificates/{}",
1023253f11b8SEd Tanous         BMCWEB_REDFISH_MANAGER_URI_NAME, certId);
1024828252d5SJiaqing Zhao     std::string objPath =
1025253f11b8SEd Tanous         sdbusplus::message::object_path(certs::httpsObjectPath) / certId;
1026253f11b8SEd Tanous     getCertificateProperties(asyncResp, objPath, certs::httpsServiceName,
1027253f11b8SEd Tanous                              certId, certURL, "HTTPS Certificate");
10287e860f15SJohn Edward Broadbent }
102937cce918SMarri Devender Rao 
1030828252d5SJiaqing Zhao inline void requestRoutesHTTPSCertificate(App& app)
103137cce918SMarri Devender Rao {
1032253f11b8SEd Tanous     BMCWEB_ROUTE(
1033253f11b8SEd Tanous         app, "/redfish/v1/Managers/<str>/NetworkProtocol/HTTPS/Certificates/")
1034ed398213SEd Tanous         .privileges(redfish::privileges::getCertificateCollection)
1035828252d5SJiaqing Zhao         .methods(boost::beast::http::verb::get)(std::bind_front(
1036828252d5SJiaqing Zhao             handleHTTPSCertificateCollectionGet, std::ref(app)));
1037828252d5SJiaqing Zhao 
1038253f11b8SEd Tanous     BMCWEB_ROUTE(
1039253f11b8SEd Tanous         app, "/redfish/v1/Managers/<str>/NetworkProtocol/HTTPS/Certificates/")
1040828252d5SJiaqing Zhao         .privileges(redfish::privileges::postCertificateCollection)
1041828252d5SJiaqing Zhao         .methods(boost::beast::http::verb::post)(std::bind_front(
1042828252d5SJiaqing Zhao             handleHTTPSCertificateCollectionPost, std::ref(app)));
1043828252d5SJiaqing Zhao 
1044828252d5SJiaqing Zhao     BMCWEB_ROUTE(
1045828252d5SJiaqing Zhao         app,
1046253f11b8SEd Tanous         "/redfish/v1/Managers/<str>/NetworkProtocol/HTTPS/Certificates/<str>/")
1047828252d5SJiaqing Zhao         .privileges(redfish::privileges::getCertificate)
1048002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1049828252d5SJiaqing Zhao             std::bind_front(handleHTTPSCertificateGet, std::ref(app)));
1050828252d5SJiaqing Zhao }
1051828252d5SJiaqing Zhao 
1052828252d5SJiaqing Zhao inline void handleLDAPCertificateCollectionGet(
1053828252d5SJiaqing Zhao     App& app, const crow::Request& req,
1054828252d5SJiaqing Zhao     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1055828252d5SJiaqing Zhao {
10563ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
105745ca1b86SEd Tanous     {
105845ca1b86SEd Tanous         return;
105945ca1b86SEd Tanous     }
10601476687dSEd Tanous 
10611476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
10621476687dSEd Tanous         "/redfish/v1/AccountService/LDAP/Certificates";
10631476687dSEd Tanous     asyncResp->res.jsonValue["@odata.type"] =
10641476687dSEd Tanous         "#CertificateCollection.CertificateCollection";
10651476687dSEd Tanous     asyncResp->res.jsonValue["Name"] = "LDAP Certificates Collection";
10661476687dSEd Tanous     asyncResp->res.jsonValue["Description"] =
10671476687dSEd Tanous         "A Collection of LDAP certificate instances";
10688d1b46d7Szhanghch05 
1069d3f92ce7SJiaqing Zhao     getCertificateList(asyncResp, certs::ldapObjectPath,
1070d3f92ce7SJiaqing Zhao                        "/Members"_json_pointer,
1071d3f92ce7SJiaqing Zhao                        "/Members@odata.count"_json_pointer);
1072828252d5SJiaqing Zhao }
107337cce918SMarri Devender Rao 
1074828252d5SJiaqing Zhao inline void handleLDAPCertificateCollectionPost(
1075828252d5SJiaqing Zhao     App& app, const crow::Request& req,
1076828252d5SJiaqing Zhao     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
1077828252d5SJiaqing Zhao {
10783ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
107945ca1b86SEd Tanous     {
108045ca1b86SEd Tanous         return;
108145ca1b86SEd Tanous     }
1082b2896149SEd Tanous     std::string certHttpBody = getCertificateFromReqBody(asyncResp, req);
108358eb238fSKowalski, Kamil 
1084b2896149SEd Tanous     if (certHttpBody.empty())
108558eb238fSKowalski, Kamil     {
108662598e31SEd Tanous         BMCWEB_LOG_ERROR("Cannot get certificate from request body.");
1087a08752f5SZbigniew Kurzynski         messages::unrecognizedRequestBody(asyncResp->res);
108858eb238fSKowalski, Kamil         return;
108958eb238fSKowalski, Kamil     }
109058eb238fSKowalski, Kamil 
109158eb238fSKowalski, Kamil     std::shared_ptr<CertificateFile> certFile =
1092b2896149SEd Tanous         std::make_shared<CertificateFile>(certHttpBody);
109358eb238fSKowalski, Kamil 
109437cce918SMarri Devender Rao     crow::connections::systemBus->async_method_call(
10955e7e2dc5SEd Tanous         [asyncResp, certFile](const boost::system::error_code& ec,
1096656ec7e3SZbigniew Kurzynski                               const std::string& objectPath) {
109737cce918SMarri Devender Rao             if (ec)
109837cce918SMarri Devender Rao             {
109962598e31SEd Tanous                 BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
110037cce918SMarri Devender Rao                 messages::internalError(asyncResp->res);
110137cce918SMarri Devender Rao                 return;
110237cce918SMarri Devender Rao             }
1103717b9802SJiaqing Zhao 
1104717b9802SJiaqing Zhao             sdbusplus::message::object_path path(objectPath);
1105717b9802SJiaqing Zhao             std::string certId = path.filename();
1106ef4c65b7SEd Tanous             const boost::urls::url certURL = boost::urls::format(
1107ef4c65b7SEd Tanous                 "/redfish/v1/AccountService/LDAP/Certificates/{}", certId);
1108bd79bce8SPatrick Williams             getCertificateProperties(asyncResp, objectPath,
1109bd79bce8SPatrick Williams                                      certs::ldapServiceName, certId, certURL,
1110bd79bce8SPatrick Williams                                      "LDAP Certificate");
111162598e31SEd Tanous             BMCWEB_LOG_DEBUG("LDAP certificate install file={}",
111262598e31SEd Tanous                              certFile->getCertFilePath());
111337cce918SMarri Devender Rao         },
1114828252d5SJiaqing Zhao         certs::ldapServiceName, certs::ldapObjectPath, certs::certInstallIntf,
1115828252d5SJiaqing Zhao         "Install", certFile->getCertFilePath());
1116828252d5SJiaqing Zhao }
111737cce918SMarri Devender Rao 
1118828252d5SJiaqing Zhao inline void handleLDAPCertificateGet(
1119828252d5SJiaqing Zhao     App& app, const crow::Request& req,
1120828252d5SJiaqing Zhao     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id)
112137cce918SMarri Devender Rao {
11223ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
112345ca1b86SEd Tanous     {
112445ca1b86SEd Tanous         return;
112545ca1b86SEd Tanous     }
1126717b9802SJiaqing Zhao 
112762598e31SEd Tanous     BMCWEB_LOG_DEBUG("LDAP Certificate ID={}", id);
1128ef4c65b7SEd Tanous     const boost::urls::url certURL = boost::urls::format(
1129ef4c65b7SEd Tanous         "/redfish/v1/AccountService/LDAP/Certificates/{}", id);
1130717b9802SJiaqing Zhao     std::string objPath =
1131717b9802SJiaqing Zhao         sdbusplus::message::object_path(certs::ldapObjectPath) / id;
1132717b9802SJiaqing Zhao     getCertificateProperties(asyncResp, objPath, certs::ldapServiceName, id,
1133717b9802SJiaqing Zhao                              certURL, "LDAP Certificate");
1134828252d5SJiaqing Zhao }
1135828252d5SJiaqing Zhao 
113699612247SJiaqing Zhao inline void handleLDAPCertificateDelete(
113799612247SJiaqing Zhao     App& app, const crow::Request& req,
113899612247SJiaqing Zhao     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp, const std::string& id)
113999612247SJiaqing Zhao {
114099612247SJiaqing Zhao     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
114199612247SJiaqing Zhao     {
114299612247SJiaqing Zhao         return;
114399612247SJiaqing Zhao     }
114499612247SJiaqing Zhao 
114562598e31SEd Tanous     BMCWEB_LOG_DEBUG("Delete LDAP Certificate ID={}", id);
114699612247SJiaqing Zhao     std::string objPath =
114799612247SJiaqing Zhao         sdbusplus::message::object_path(certs::ldapObjectPath) / id;
114899612247SJiaqing Zhao 
114999612247SJiaqing Zhao     deleteCertificate(asyncResp, certs::ldapServiceName, objPath);
115099612247SJiaqing Zhao }
115199612247SJiaqing Zhao 
1152828252d5SJiaqing Zhao inline void requestRoutesLDAPCertificate(App& app)
1153cfcd5f6bSMarri Devender Rao {
1154828252d5SJiaqing Zhao     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/LDAP/Certificates/")
1155828252d5SJiaqing Zhao         .privileges(redfish::privileges::getCertificateCollection)
1156828252d5SJiaqing Zhao         .methods(boost::beast::http::verb::get)(
1157828252d5SJiaqing Zhao             std::bind_front(handleLDAPCertificateCollectionGet, std::ref(app)));
1158828252d5SJiaqing Zhao 
1159828252d5SJiaqing Zhao     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/LDAP/Certificates/")
1160828252d5SJiaqing Zhao         .privileges(redfish::privileges::postCertificateCollection)
1161828252d5SJiaqing Zhao         .methods(boost::beast::http::verb::post)(std::bind_front(
1162828252d5SJiaqing Zhao             handleLDAPCertificateCollectionPost, std::ref(app)));
1163828252d5SJiaqing Zhao 
1164828252d5SJiaqing Zhao     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/LDAP/Certificates/<str>/")
1165ed398213SEd Tanous         .privileges(redfish::privileges::getCertificate)
1166002d39b4SEd Tanous         .methods(boost::beast::http::verb::get)(
1167828252d5SJiaqing Zhao             std::bind_front(handleLDAPCertificateGet, std::ref(app)));
116899612247SJiaqing Zhao 
116999612247SJiaqing Zhao     BMCWEB_ROUTE(app, "/redfish/v1/AccountService/LDAP/Certificates/<str>/")
117099612247SJiaqing Zhao         .privileges(redfish::privileges::deleteCertificate)
117199612247SJiaqing Zhao         .methods(boost::beast::http::verb::delete_)(
117299612247SJiaqing Zhao             std::bind_front(handleLDAPCertificateDelete, std::ref(app)));
1173828252d5SJiaqing Zhao } // requestRoutesLDAPCertificate
1174828252d5SJiaqing Zhao 
1175828252d5SJiaqing Zhao inline void handleTrustStoreCertificateCollectionGet(
1176828252d5SJiaqing Zhao     App& app, const crow::Request& req,
1177253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1178253f11b8SEd Tanous     const std::string& managerId)
1179828252d5SJiaqing Zhao {
11803ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
118145ca1b86SEd Tanous     {
118245ca1b86SEd Tanous         return;
118345ca1b86SEd Tanous     }
11841476687dSEd Tanous 
1185253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
1186253f11b8SEd Tanous     {
1187253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
1188253f11b8SEd Tanous         return;
1189253f11b8SEd Tanous     }
1190253f11b8SEd Tanous 
11911476687dSEd Tanous     asyncResp->res.jsonValue["@odata.id"] =
1192253f11b8SEd Tanous         boost::urls::format("/redfish/v1/Managers/{}/Truststore/Certificates/",
1193253f11b8SEd Tanous                             BMCWEB_REDFISH_MANAGER_URI_NAME);
11941476687dSEd Tanous     asyncResp->res.jsonValue["@odata.type"] =
11951476687dSEd Tanous         "#CertificateCollection.CertificateCollection";
1196002d39b4SEd Tanous     asyncResp->res.jsonValue["Name"] = "TrustStore Certificates Collection";
11971476687dSEd Tanous     asyncResp->res.jsonValue["Description"] =
11981476687dSEd Tanous         "A Collection of TrustStore certificate instances";
11998d1b46d7Szhanghch05 
1200d3f92ce7SJiaqing Zhao     getCertificateList(asyncResp, certs::authorityObjectPath,
1201d3f92ce7SJiaqing Zhao                        "/Members"_json_pointer,
1202d3f92ce7SJiaqing Zhao                        "/Members@odata.count"_json_pointer);
1203828252d5SJiaqing Zhao }
1204cfcd5f6bSMarri Devender Rao 
1205828252d5SJiaqing Zhao inline void handleTrustStoreCertificateCollectionPost(
1206828252d5SJiaqing Zhao     App& app, const crow::Request& req,
1207253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1208253f11b8SEd Tanous     const std::string& managerId)
1209828252d5SJiaqing Zhao {
12103ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
121145ca1b86SEd Tanous     {
121245ca1b86SEd Tanous         return;
121345ca1b86SEd Tanous     }
1214253f11b8SEd Tanous 
1215253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
1216253f11b8SEd Tanous     {
1217253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
1218253f11b8SEd Tanous         return;
1219253f11b8SEd Tanous     }
1220253f11b8SEd Tanous 
1221b2896149SEd Tanous     std::string certHttpBody = getCertificateFromReqBody(asyncResp, req);
1222a08752f5SZbigniew Kurzynski 
1223b2896149SEd Tanous     if (certHttpBody.empty())
1224a08752f5SZbigniew Kurzynski     {
122562598e31SEd Tanous         BMCWEB_LOG_ERROR("Cannot get certificate from request body.");
1226a08752f5SZbigniew Kurzynski         messages::unrecognizedRequestBody(asyncResp->res);
1227a08752f5SZbigniew Kurzynski         return;
1228a08752f5SZbigniew Kurzynski     }
1229a08752f5SZbigniew Kurzynski 
1230a08752f5SZbigniew Kurzynski     std::shared_ptr<CertificateFile> certFile =
1231b2896149SEd Tanous         std::make_shared<CertificateFile>(certHttpBody);
1232cfcd5f6bSMarri Devender Rao     crow::connections::systemBus->async_method_call(
12335e7e2dc5SEd Tanous         [asyncResp, certFile](const boost::system::error_code& ec,
1234656ec7e3SZbigniew Kurzynski                               const std::string& objectPath) {
1235cfcd5f6bSMarri Devender Rao             if (ec)
1236cfcd5f6bSMarri Devender Rao             {
123762598e31SEd Tanous                 BMCWEB_LOG_ERROR("DBUS response error: {}", ec);
1238cfcd5f6bSMarri Devender Rao                 messages::internalError(asyncResp->res);
1239cfcd5f6bSMarri Devender Rao                 return;
1240cfcd5f6bSMarri Devender Rao             }
1241656ec7e3SZbigniew Kurzynski 
1242717b9802SJiaqing Zhao             sdbusplus::message::object_path path(objectPath);
1243717b9802SJiaqing Zhao             std::string certId = path.filename();
1244ef4c65b7SEd Tanous             const boost::urls::url certURL = boost::urls::format(
1245253f11b8SEd Tanous                 "/redfish/v1/Managers/{}/Truststore/Certificates/{}",
1246253f11b8SEd Tanous                 BMCWEB_REDFISH_MANAGER_URI_NAME, certId);
1247717b9802SJiaqing Zhao             getCertificateProperties(asyncResp, objectPath,
1248bd79bce8SPatrick Williams                                      certs::authorityServiceName, certId,
1249bd79bce8SPatrick Williams                                      certURL, "TrustStore Certificate");
125062598e31SEd Tanous             BMCWEB_LOG_DEBUG("TrustStore certificate install file={}",
125162598e31SEd Tanous                              certFile->getCertFilePath());
1252cfcd5f6bSMarri Devender Rao         },
1253cfcd5f6bSMarri Devender Rao         certs::authorityServiceName, certs::authorityObjectPath,
12540fda0f12SGeorge Liu         certs::certInstallIntf, "Install", certFile->getCertFilePath());
1255828252d5SJiaqing Zhao }
1256cfcd5f6bSMarri Devender Rao 
1257828252d5SJiaqing Zhao inline void handleTrustStoreCertificateGet(
1258828252d5SJiaqing Zhao     App& app, const crow::Request& req,
1259253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1260253f11b8SEd Tanous     const std::string& managerId, const std::string& certId)
1261cfcd5f6bSMarri Devender Rao {
12623ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
126345ca1b86SEd Tanous     {
126445ca1b86SEd Tanous         return;
126545ca1b86SEd Tanous     }
1266717b9802SJiaqing Zhao 
1267253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
1268253f11b8SEd Tanous     {
1269253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
1270253f11b8SEd Tanous         return;
1271253f11b8SEd Tanous     }
1272253f11b8SEd Tanous 
1273253f11b8SEd Tanous     BMCWEB_LOG_DEBUG("Truststore Certificate ID={}", certId);
1274ef4c65b7SEd Tanous     const boost::urls::url certURL = boost::urls::format(
1275253f11b8SEd Tanous         "/redfish/v1/Managers/{}/Truststore/Certificates/{}",
1276253f11b8SEd Tanous         BMCWEB_REDFISH_MANAGER_URI_NAME, certId);
1277717b9802SJiaqing Zhao     std::string objPath =
1278253f11b8SEd Tanous         sdbusplus::message::object_path(certs::authorityObjectPath) / certId;
1279828252d5SJiaqing Zhao     getCertificateProperties(asyncResp, objPath, certs::authorityServiceName,
1280253f11b8SEd Tanous                              certId, certURL, "TrustStore Certificate");
1281828252d5SJiaqing Zhao }
128207a60299SZbigniew Kurzynski 
1283828252d5SJiaqing Zhao inline void handleTrustStoreCertificateDelete(
1284828252d5SJiaqing Zhao     App& app, const crow::Request& req,
1285253f11b8SEd Tanous     const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
1286253f11b8SEd Tanous     const std::string& managerId, const std::string& certId)
1287828252d5SJiaqing Zhao {
12883ba00073SCarson Labrado     if (!redfish::setUpRedfishRoute(app, req, asyncResp))
128945ca1b86SEd Tanous     {
129045ca1b86SEd Tanous         return;
129145ca1b86SEd Tanous     }
129207a60299SZbigniew Kurzynski 
1293253f11b8SEd Tanous     if (managerId != BMCWEB_REDFISH_MANAGER_URI_NAME)
1294253f11b8SEd Tanous     {
1295253f11b8SEd Tanous         messages::resourceNotFound(asyncResp->res, "Manager", managerId);
1296253f11b8SEd Tanous         return;
1297253f11b8SEd Tanous     }
1298253f11b8SEd Tanous 
1299253f11b8SEd Tanous     BMCWEB_LOG_DEBUG("Delete TrustStore Certificate ID={}", certId);
1300717b9802SJiaqing Zhao     std::string objPath =
1301253f11b8SEd Tanous         sdbusplus::message::object_path(certs::authorityObjectPath) / certId;
130207a60299SZbigniew Kurzynski 
13037a3a8f7aSJiaqing Zhao     deleteCertificate(asyncResp, certs::authorityServiceName, objPath);
1304828252d5SJiaqing Zhao }
1305828252d5SJiaqing Zhao 
1306828252d5SJiaqing Zhao inline void requestRoutesTrustStoreCertificate(App& app)
1307828252d5SJiaqing Zhao {
1308253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/Truststore/Certificates/")
1309828252d5SJiaqing Zhao         .privileges(redfish::privileges::getCertificate)
1310828252d5SJiaqing Zhao         .methods(boost::beast::http::verb::get)(std::bind_front(
1311828252d5SJiaqing Zhao             handleTrustStoreCertificateCollectionGet, std::ref(app)));
1312828252d5SJiaqing Zhao 
1313253f11b8SEd Tanous     BMCWEB_ROUTE(app, "/redfish/v1/Managers/<str>/Truststore/Certificates/")
1314828252d5SJiaqing Zhao         .privileges(redfish::privileges::postCertificateCollection)
1315828252d5SJiaqing Zhao         .methods(boost::beast::http::verb::post)(std::bind_front(
1316828252d5SJiaqing Zhao             handleTrustStoreCertificateCollectionPost, std::ref(app)));
1317828252d5SJiaqing Zhao 
1318253f11b8SEd Tanous     BMCWEB_ROUTE(app,
1319253f11b8SEd Tanous                  "/redfish/v1/Managers/<str>/Truststore/Certificates/<str>/")
1320828252d5SJiaqing Zhao         .privileges(redfish::privileges::getCertificate)
1321828252d5SJiaqing Zhao         .methods(boost::beast::http::verb::get)(
1322828252d5SJiaqing Zhao             std::bind_front(handleTrustStoreCertificateGet, std::ref(app)));
1323828252d5SJiaqing Zhao 
1324253f11b8SEd Tanous     BMCWEB_ROUTE(app,
1325253f11b8SEd Tanous                  "/redfish/v1/Managers/<str>/Truststore/Certificates/<str>/")
1326828252d5SJiaqing Zhao         .privileges(redfish::privileges::deleteCertificate)
1327828252d5SJiaqing Zhao         .methods(boost::beast::http::verb::delete_)(
1328828252d5SJiaqing Zhao             std::bind_front(handleTrustStoreCertificateDelete, std::ref(app)));
13297e860f15SJohn Edward Broadbent } // requestRoutesTrustStoreCertificate
13305968caeeSMarri Devender Rao } // namespace redfish
1331