Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 1 | #pragma once |
| 2 | #ifdef BMCWEB_ENABLE_SSL |
Ed Tanous | 3ccb3ad | 2023-01-13 17:40:03 -0800 | [diff] [blame] | 3 | #include "dbus_singleton.hpp" |
| 4 | #include "dbus_utility.hpp" |
| 5 | #include "include/dbus_utility.hpp" |
| 6 | #include "logging.hpp" |
| 7 | #include "ssl_key_handler.hpp" |
| 8 | |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 9 | #include <sdbusplus/bus/match.hpp> |
| 10 | #include <sdbusplus/message/types.hpp> |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 11 | |
| 12 | namespace crow |
| 13 | { |
| 14 | namespace hostname_monitor |
| 15 | { |
Ed Tanous | cf9e417 | 2022-12-21 09:30:16 -0800 | [diff] [blame] | 16 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) |
Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 17 | static std::unique_ptr<sdbusplus::bus::match_t> hostnameSignalMonitor; |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 18 | |
| 19 | inline void installCertificate(const std::filesystem::path& certPath) |
| 20 | { |
| 21 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 5e7e2dc | 2023-02-16 10:37:01 -0800 | [diff] [blame] | 22 | [certPath](const boost::system::error_code& ec) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 23 | if (ec) |
| 24 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 25 | BMCWEB_LOG_ERROR("Replace Certificate Fail.."); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 26 | return; |
| 27 | } |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 28 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 29 | BMCWEB_LOG_INFO("Replace HTTPs Certificate Success, " |
| 30 | "remove temporary certificate file.."); |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 31 | remove(certPath.c_str()); |
Patrick Williams | 5a39f77 | 2023-10-20 11:20:21 -0500 | [diff] [blame] | 32 | }, |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 33 | "xyz.openbmc_project.Certs.Manager.Server.Https", |
| 34 | "/xyz/openbmc_project/certs/server/https/1", |
| 35 | "xyz.openbmc_project.Certs.Replace", "Replace", certPath.string()); |
| 36 | } |
| 37 | |
| 38 | inline int onPropertyUpdate(sd_bus_message* m, void* /* userdata */, |
Ed Tanous | 81ce609 | 2020-12-17 16:54:55 +0000 | [diff] [blame] | 39 | sd_bus_error* retError) |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 40 | { |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 41 | if (retError == nullptr || (sd_bus_error_is_set(retError) != 0)) |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 42 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 43 | BMCWEB_LOG_ERROR("Got sdbus error on match"); |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 44 | return 0; |
| 45 | } |
| 46 | |
Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 47 | sdbusplus::message_t message(m); |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 48 | std::string iface; |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 49 | dbus::utility::DBusPropertiesMap changedProperties; |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 50 | |
| 51 | message.read(iface, changedProperties); |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 52 | const std::string* hostname = nullptr; |
| 53 | for (const auto& propertyPair : changedProperties) |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 54 | { |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 55 | if (propertyPair.first == "HostName") |
| 56 | { |
| 57 | hostname = std::get_if<std::string>(&propertyPair.second); |
| 58 | } |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 59 | } |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 60 | if (hostname == nullptr) |
| 61 | { |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 62 | return 0; |
| 63 | } |
| 64 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 65 | BMCWEB_LOG_DEBUG("Read hostname from signal: {}", *hostname); |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 66 | const std::string certFile = "/etc/ssl/certs/https/server.pem"; |
| 67 | |
| 68 | X509* cert = ensuressl::loadCert(certFile); |
| 69 | if (cert == nullptr) |
| 70 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 71 | BMCWEB_LOG_ERROR("Failed to read cert"); |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | const int maxKeySize = 256; |
| 76 | std::array<char, maxKeySize> cnBuffer{}; |
| 77 | |
Patrick Williams | 89492a1 | 2023-05-10 07:51:34 -0500 | [diff] [blame] | 78 | int cnLength = X509_NAME_get_text_by_NID(X509_get_subject_name(cert), |
| 79 | NID_commonName, cnBuffer.data(), |
| 80 | cnBuffer.size()); |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 81 | if (cnLength == -1) |
| 82 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 83 | BMCWEB_LOG_ERROR("Failed to read NID_commonName"); |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 84 | X509_free(cert); |
| 85 | return 0; |
| 86 | } |
| 87 | std::string_view cnValue(std::begin(cnBuffer), |
| 88 | static_cast<size_t>(cnLength)); |
| 89 | |
| 90 | EVP_PKEY* pPubKey = X509_get_pubkey(cert); |
| 91 | if (pPubKey == nullptr) |
| 92 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 93 | BMCWEB_LOG_ERROR("Failed to get public key"); |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 94 | X509_free(cert); |
| 95 | return 0; |
| 96 | } |
| 97 | int isSelfSigned = X509_verify(cert, pPubKey); |
| 98 | EVP_PKEY_free(pPubKey); |
| 99 | |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 100 | BMCWEB_LOG_DEBUG( |
| 101 | "Current HTTPs Certificate Subject CN: {}, New HostName: {}, isSelfSigned: {}", |
| 102 | cnValue, *hostname, isSelfSigned); |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 103 | |
| 104 | ASN1_IA5STRING* asn1 = static_cast<ASN1_IA5STRING*>( |
| 105 | X509_get_ext_d2i(cert, NID_netscape_comment, nullptr, nullptr)); |
Ed Tanous | e662eae | 2022-01-25 10:39:19 -0800 | [diff] [blame] | 106 | if (asn1 != nullptr) |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 107 | { |
Ed Tanous | 46ff87b | 2022-01-07 09:25:51 -0800 | [diff] [blame] | 108 | // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast) |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 109 | std::string_view comment(reinterpret_cast<const char*>(asn1->data), |
| 110 | static_cast<size_t>(asn1->length)); |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 111 | BMCWEB_LOG_DEBUG("x509Comment: {}", comment); |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 112 | |
| 113 | if (ensuressl::x509Comment == comment && isSelfSigned == 1 && |
| 114 | cnValue != *hostname) |
| 115 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 116 | BMCWEB_LOG_INFO( |
| 117 | "Ready to generate new HTTPs certificate with subject cn: {}", |
| 118 | *hostname); |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 119 | |
| 120 | ensuressl::generateSslCertificate("/tmp/hostname_cert.tmp", |
| 121 | *hostname); |
| 122 | installCertificate("/tmp/hostname_cert.tmp"); |
| 123 | } |
| 124 | ASN1_STRING_free(asn1); |
| 125 | } |
| 126 | X509_free(cert); |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | inline void registerHostnameSignal() |
| 131 | { |
Ed Tanous | 62598e3 | 2023-07-17 17:06:25 -0700 | [diff] [blame] | 132 | BMCWEB_LOG_INFO("Register HostName PropertiesChanged Signal"); |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 133 | std::string propertiesMatchString = |
| 134 | ("type='signal'," |
| 135 | "interface='org.freedesktop.DBus.Properties'," |
| 136 | "path='/xyz/openbmc_project/network/config'," |
| 137 | "arg0='xyz.openbmc_project.Network.SystemConfiguration'," |
| 138 | "member='PropertiesChanged'"); |
| 139 | |
Patrick Williams | 59d494e | 2022-07-22 19:26:55 -0500 | [diff] [blame] | 140 | hostnameSignalMonitor = std::make_unique<sdbusplus::bus::match_t>( |
Alan Kuo | a822070 | 2020-11-26 11:15:29 +0800 | [diff] [blame] | 141 | *crow::connections::systemBus, propertiesMatchString, onPropertyUpdate, |
| 142 | nullptr); |
| 143 | } |
| 144 | } // namespace hostname_monitor |
| 145 | } // namespace crow |
| 146 | #endif |