blob: 4eb31f115595c0eadff720643b7a97cb7d45881e [file] [log] [blame]
Alan Kuoa8220702020-11-26 11:15:29 +08001#pragma once
Ed Tanous3ccb3ad2023-01-13 17:40:03 -08002#include "dbus_singleton.hpp"
3#include "dbus_utility.hpp"
4#include "include/dbus_utility.hpp"
5#include "logging.hpp"
6#include "ssl_key_handler.hpp"
7
Alan Kuoa8220702020-11-26 11:15:29 +08008#include <sdbusplus/bus/match.hpp>
9#include <sdbusplus/message/types.hpp>
Alan Kuoa8220702020-11-26 11:15:29 +080010
11namespace crow
12{
13namespace hostname_monitor
14{
Ed Tanouscf9e4172022-12-21 09:30:16 -080015// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
Patrick Williams59d494e2022-07-22 19:26:55 -050016static std::unique_ptr<sdbusplus::bus::match_t> hostnameSignalMonitor;
Alan Kuoa8220702020-11-26 11:15:29 +080017
18inline void installCertificate(const std::filesystem::path& certPath)
19{
20 crow::connections::systemBus->async_method_call(
Ed Tanous5e7e2dc2023-02-16 10:37:01 -080021 [certPath](const boost::system::error_code& ec) {
Patrick Williamsbd79bce2024-08-16 15:22:20 -040022 if (ec)
23 {
24 BMCWEB_LOG_ERROR("Replace Certificate Fail..");
25 return;
26 }
Alan Kuoa8220702020-11-26 11:15:29 +080027
Patrick Williamsbd79bce2024-08-16 15:22:20 -040028 BMCWEB_LOG_INFO("Replace HTTPs Certificate Success, "
29 "remove temporary certificate file..");
30 std::error_code ec2;
31 std::filesystem::remove(certPath.c_str(), ec2);
32 if (ec2)
33 {
34 BMCWEB_LOG_ERROR("Failed to remove certificate");
35 }
36 },
Alan Kuoa8220702020-11-26 11:15:29 +080037 "xyz.openbmc_project.Certs.Manager.Server.Https",
38 "/xyz/openbmc_project/certs/server/https/1",
39 "xyz.openbmc_project.Certs.Replace", "Replace", certPath.string());
40}
41
42inline int onPropertyUpdate(sd_bus_message* m, void* /* userdata */,
Ed Tanous81ce6092020-12-17 16:54:55 +000043 sd_bus_error* retError)
Alan Kuoa8220702020-11-26 11:15:29 +080044{
Ed Tanouse662eae2022-01-25 10:39:19 -080045 if (retError == nullptr || (sd_bus_error_is_set(retError) != 0))
Alan Kuoa8220702020-11-26 11:15:29 +080046 {
Ed Tanous62598e32023-07-17 17:06:25 -070047 BMCWEB_LOG_ERROR("Got sdbus error on match");
Alan Kuoa8220702020-11-26 11:15:29 +080048 return 0;
49 }
50
Patrick Williams59d494e2022-07-22 19:26:55 -050051 sdbusplus::message_t message(m);
Alan Kuoa8220702020-11-26 11:15:29 +080052 std::string iface;
Ed Tanousb9d36b42022-02-26 21:42:46 -080053 dbus::utility::DBusPropertiesMap changedProperties;
Alan Kuoa8220702020-11-26 11:15:29 +080054
55 message.read(iface, changedProperties);
Ed Tanousb9d36b42022-02-26 21:42:46 -080056 const std::string* hostname = nullptr;
57 for (const auto& propertyPair : changedProperties)
Alan Kuoa8220702020-11-26 11:15:29 +080058 {
Ed Tanousb9d36b42022-02-26 21:42:46 -080059 if (propertyPair.first == "HostName")
60 {
61 hostname = std::get_if<std::string>(&propertyPair.second);
62 }
Alan Kuoa8220702020-11-26 11:15:29 +080063 }
Alan Kuoa8220702020-11-26 11:15:29 +080064 if (hostname == nullptr)
65 {
Alan Kuoa8220702020-11-26 11:15:29 +080066 return 0;
67 }
68
Ed Tanous62598e32023-07-17 17:06:25 -070069 BMCWEB_LOG_DEBUG("Read hostname from signal: {}", *hostname);
Alan Kuoa8220702020-11-26 11:15:29 +080070 const std::string certFile = "/etc/ssl/certs/https/server.pem";
71
72 X509* cert = ensuressl::loadCert(certFile);
73 if (cert == nullptr)
74 {
Ed Tanous62598e32023-07-17 17:06:25 -070075 BMCWEB_LOG_ERROR("Failed to read cert");
Alan Kuoa8220702020-11-26 11:15:29 +080076 return 0;
77 }
78
79 const int maxKeySize = 256;
80 std::array<char, maxKeySize> cnBuffer{};
81
Patrick Williamsbd79bce2024-08-16 15:22:20 -040082 int cnLength =
83 X509_NAME_get_text_by_NID(X509_get_subject_name(cert), NID_commonName,
84 cnBuffer.data(), cnBuffer.size());
Alan Kuoa8220702020-11-26 11:15:29 +080085 if (cnLength == -1)
86 {
Ed Tanous62598e32023-07-17 17:06:25 -070087 BMCWEB_LOG_ERROR("Failed to read NID_commonName");
Alan Kuoa8220702020-11-26 11:15:29 +080088 X509_free(cert);
89 return 0;
90 }
91 std::string_view cnValue(std::begin(cnBuffer),
92 static_cast<size_t>(cnLength));
93
94 EVP_PKEY* pPubKey = X509_get_pubkey(cert);
95 if (pPubKey == nullptr)
96 {
Ed Tanous62598e32023-07-17 17:06:25 -070097 BMCWEB_LOG_ERROR("Failed to get public key");
Alan Kuoa8220702020-11-26 11:15:29 +080098 X509_free(cert);
99 return 0;
100 }
101 int isSelfSigned = X509_verify(cert, pPubKey);
102 EVP_PKEY_free(pPubKey);
103
Ed Tanous62598e32023-07-17 17:06:25 -0700104 BMCWEB_LOG_DEBUG(
105 "Current HTTPs Certificate Subject CN: {}, New HostName: {}, isSelfSigned: {}",
106 cnValue, *hostname, isSelfSigned);
Alan Kuoa8220702020-11-26 11:15:29 +0800107
108 ASN1_IA5STRING* asn1 = static_cast<ASN1_IA5STRING*>(
109 X509_get_ext_d2i(cert, NID_netscape_comment, nullptr, nullptr));
Ed Tanouse662eae2022-01-25 10:39:19 -0800110 if (asn1 != nullptr)
Alan Kuoa8220702020-11-26 11:15:29 +0800111 {
Ed Tanous46ff87b2022-01-07 09:25:51 -0800112 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
Alan Kuoa8220702020-11-26 11:15:29 +0800113 std::string_view comment(reinterpret_cast<const char*>(asn1->data),
114 static_cast<size_t>(asn1->length));
Ed Tanous62598e32023-07-17 17:06:25 -0700115 BMCWEB_LOG_DEBUG("x509Comment: {}", comment);
Alan Kuoa8220702020-11-26 11:15:29 +0800116
117 if (ensuressl::x509Comment == comment && isSelfSigned == 1 &&
118 cnValue != *hostname)
119 {
Ed Tanous62598e32023-07-17 17:06:25 -0700120 BMCWEB_LOG_INFO(
121 "Ready to generate new HTTPs certificate with subject cn: {}",
122 *hostname);
Alan Kuoa8220702020-11-26 11:15:29 +0800123
Ed Tanous099225c2024-03-27 22:03:05 -0700124 std::string certData = ensuressl::generateSslCertificate(*hostname);
125 if (certData.empty())
126 {
127 BMCWEB_LOG_ERROR("Failed to generate cert");
128 return 0;
129 }
130 ensuressl::writeCertificateToFile("/tmp/hostname_cert.tmp",
131 certData);
132
Alan Kuoa8220702020-11-26 11:15:29 +0800133 installCertificate("/tmp/hostname_cert.tmp");
134 }
135 ASN1_STRING_free(asn1);
136 }
137 X509_free(cert);
138 return 0;
139}
140
141inline void registerHostnameSignal()
142{
Ed Tanous62598e32023-07-17 17:06:25 -0700143 BMCWEB_LOG_INFO("Register HostName PropertiesChanged Signal");
Alan Kuoa8220702020-11-26 11:15:29 +0800144 std::string propertiesMatchString =
145 ("type='signal',"
146 "interface='org.freedesktop.DBus.Properties',"
147 "path='/xyz/openbmc_project/network/config',"
148 "arg0='xyz.openbmc_project.Network.SystemConfiguration',"
149 "member='PropertiesChanged'");
150
Patrick Williams59d494e2022-07-22 19:26:55 -0500151 hostnameSignalMonitor = std::make_unique<sdbusplus::bus::match_t>(
Alan Kuoa8220702020-11-26 11:15:29 +0800152 *crow::connections::systemBus, propertiesMatchString, onPropertyUpdate,
153 nullptr);
154}
155} // namespace hostname_monitor
156} // namespace crow