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