John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 1 | |
| 2 | #include "estoraged.hpp" |
| 3 | |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 4 | #include "cryptErase.hpp" |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 5 | #include "cryptsetupInterface.hpp" |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 6 | #include "pattern.hpp" |
John Edward Broadbent | 605085a | 2021-11-05 13:45:45 -0700 | [diff] [blame] | 7 | #include "sanitize.hpp" |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 8 | #include "verifyDriveGeometry.hpp" |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 9 | #include "zero.hpp" |
John Edward Broadbent | 4e13b0a | 2021-11-15 15:21:59 -0800 | [diff] [blame] | 10 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 11 | #include <libcryptsetup.h> |
| 12 | #include <openssl/rand.h> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 13 | |
| 14 | #include <phosphor-logging/lg2.hpp> |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 15 | #include <sdbusplus/asio/object_server.hpp> |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 16 | #include <xyz/openbmc_project/Common/error.hpp> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 17 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 18 | #include <cstdlib> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 19 | #include <filesystem> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 20 | #include <iostream> |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 21 | #include <string> |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 22 | #include <string_view> |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 23 | #include <utility> |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 24 | #include <vector> |
| 25 | |
| 26 | namespace estoraged |
| 27 | { |
| 28 | |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 29 | using Association = std::tuple<std::string, std::string, std::string>; |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 30 | using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 31 | using sdbusplus::xyz::openbmc_project::Common::Error::UnsupportedRequest; |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 32 | using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Drive; |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 33 | using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 34 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 35 | EStoraged::EStoraged(sdbusplus::asio::object_server& server, |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 36 | const std::string& configPath, const std::string& devPath, |
| 37 | const std::string& luksName, uint64_t size, |
John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 38 | uint8_t lifeTime, const std::string& partNumber, |
| 39 | const std::string& serialNumber, |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 40 | std::unique_ptr<CryptsetupInterface> cryptInterface, |
| 41 | std::unique_ptr<FilesystemInterface> fsInterface) : |
| 42 | devPath(devPath), |
| 43 | containerName(luksName), mountPoint("/mnt/" + luksName + "_fs"), |
John Edward Broadbent | 6771c69 | 2022-06-22 19:49:27 -0700 | [diff] [blame] | 44 | cryptIface(std::move(cryptInterface)), fsIface(std::move(fsInterface)), |
| 45 | objectServer(server) |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 46 | { |
| 47 | /* Get the filename of the device (without "/dev/"). */ |
| 48 | std::string deviceName = std::filesystem::path(devPath).filename().string(); |
| 49 | /* DBus object path */ |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 50 | std::string objectPath = |
| 51 | "/xyz/openbmc_project/inventory/storage/" + deviceName; |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 52 | |
| 53 | /* Add Volume interface. */ |
| 54 | volumeInterface = objectServer.add_interface( |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 55 | objectPath, "xyz.openbmc_project.Inventory.Item.Volume"); |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 56 | volumeInterface->register_method( |
| 57 | "FormatLuks", [this](const std::vector<uint8_t>& password, |
| 58 | Volume::FilesystemType type) { |
| 59 | this->formatLuks(password, type); |
| 60 | }); |
| 61 | volumeInterface->register_method( |
| 62 | "Erase", |
| 63 | [this](Volume::EraseMethod eraseType) { this->erase(eraseType); }); |
| 64 | volumeInterface->register_method("Lock", [this]() { this->lock(); }); |
| 65 | volumeInterface->register_method( |
| 66 | "Unlock", |
| 67 | [this](std::vector<uint8_t>& password) { this->unlock(password); }); |
| 68 | volumeInterface->register_method( |
| 69 | "ChangePassword", [this](const std::vector<uint8_t>& oldPassword, |
| 70 | const std::vector<uint8_t>& newPassword) { |
| 71 | this->changePassword(oldPassword, newPassword); |
| 72 | }); |
| 73 | volumeInterface->register_property_r( |
| 74 | "Locked", lockedProperty, sdbusplus::vtable::property_::emits_change, |
| 75 | [this](bool& value) { |
| 76 | value = this->isLocked(); |
| 77 | return value; |
| 78 | }); |
| 79 | |
| 80 | /* Add Drive interface. */ |
| 81 | driveInterface = objectServer.add_interface( |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 82 | objectPath, "xyz.openbmc_project.Inventory.Item.Drive"); |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 83 | driveInterface->register_property("Capacity", size); |
John Edward Broadbent | 5d799bb | 2022-03-22 16:14:24 -0700 | [diff] [blame] | 84 | driveInterface->register_property("PredictedMediaLifeLeftPercent", |
| 85 | lifeTime); |
John Edward Broadbent | 14aee77 | 2022-04-20 13:46:48 -0700 | [diff] [blame] | 86 | /* This registers the Locked property for the Drives interface. |
| 87 | * Now it is the same as the volume Locked property */ |
| 88 | driveInterface->register_property_r( |
| 89 | "Locked", lockedProperty, sdbusplus::vtable::property_::emits_change, |
| 90 | [this](bool& value) { |
| 91 | value = this->isLocked(); |
| 92 | return value; |
| 93 | }); |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 94 | |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 95 | driveInterface->register_property_r( |
| 96 | "EncryptionStatus", encryptionStatus, |
| 97 | sdbusplus::vtable::property_::emits_change, |
| 98 | [this](Drive::DriveEncryptionState& value) { |
| 99 | value = this->findEncryptionStatus(); |
| 100 | return value; |
| 101 | }); |
| 102 | |
John Edward Broadbent | 4979641 | 2022-06-22 18:31:52 -0700 | [diff] [blame] | 103 | embeddedLocationInterface = objectServer.add_interface( |
| 104 | objectPath, "xyz.openbmc_project.Inventory.Connector.Embedded"); |
John Edward Broadbent | 740e94b | 2022-06-10 19:42:30 -0700 | [diff] [blame] | 105 | |
John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 106 | /* Add Asset interface. */ |
| 107 | assetInterface = objectServer.add_interface( |
| 108 | objectPath, "xyz.openbmc_project.Inventory.Decorator.Asset"); |
| 109 | assetInterface->register_property("PartNumber", partNumber); |
| 110 | assetInterface->register_property("SerialNumber", serialNumber); |
| 111 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 112 | volumeInterface->initialize(); |
| 113 | driveInterface->initialize(); |
John Edward Broadbent | 4979641 | 2022-06-22 18:31:52 -0700 | [diff] [blame] | 114 | embeddedLocationInterface->initialize(); |
John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 115 | assetInterface->initialize(); |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 116 | |
| 117 | /* Set up the association between chassis and drive. */ |
| 118 | association = objectServer.add_interface( |
| 119 | objectPath, "xyz.openbmc_project.Association.Definitions"); |
| 120 | |
| 121 | std::vector<Association> associations; |
| 122 | associations.emplace_back("chassis", "drive", |
| 123 | std::filesystem::path(configPath).parent_path()); |
| 124 | association->register_property("Associations", associations); |
| 125 | association->initialize(); |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | EStoraged::~EStoraged() |
| 129 | { |
| 130 | objectServer.remove_interface(volumeInterface); |
| 131 | objectServer.remove_interface(driveInterface); |
John Edward Broadbent | 4979641 | 2022-06-22 18:31:52 -0700 | [diff] [blame] | 132 | objectServer.remove_interface(embeddedLocationInterface); |
John Wedig | b483830 | 2022-07-22 13:51:16 -0700 | [diff] [blame] | 133 | objectServer.remove_interface(assetInterface); |
John Wedig | 6c0d8ce | 2022-04-22 14:00:43 -0700 | [diff] [blame] | 134 | objectServer.remove_interface(association); |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | void EStoraged::formatLuks(const std::vector<uint8_t>& password, |
| 138 | Volume::FilesystemType type) |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 139 | { |
John Edward Broadbent | 4e13b0a | 2021-11-15 15:21:59 -0800 | [diff] [blame] | 140 | std::string msg = "OpenBMC.0.1.DriveFormat"; |
| 141 | lg2::info("Starting format", "REDFISH_MESSAGE_ID", msg); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 142 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 143 | if (type != Volume::FilesystemType::ext4) |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 144 | { |
| 145 | lg2::error("Only ext4 filesystems are supported currently", |
| 146 | "REDFISH_MESSAGE_ID", std::string("OpenBMC.0.1.FormatFail")); |
| 147 | throw UnsupportedRequest(); |
| 148 | } |
| 149 | |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 150 | formatLuksDev(password); |
| 151 | activateLuksDev(password); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 152 | |
| 153 | createFilesystem(); |
| 154 | mountFilesystem(); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 155 | } |
| 156 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 157 | void EStoraged::erase(Volume::EraseMethod inEraseMethod) |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 158 | { |
| 159 | std::cerr << "Erasing encrypted eMMC" << std::endl; |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 160 | lg2::info("Starting erase", "REDFISH_MESSAGE_ID", |
| 161 | std::string("OpenBMC.0.1.DriveErase")); |
| 162 | switch (inEraseMethod) |
| 163 | { |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 164 | case Volume::EraseMethod::CryptoErase: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 165 | { |
John Edward Broadbent | 59dffa6 | 2022-01-13 17:41:32 -0800 | [diff] [blame] | 166 | CryptErase myCryptErase(devPath); |
| 167 | myCryptErase.doErase(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 168 | break; |
| 169 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 170 | case Volume::EraseMethod::VerifyGeometry: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 171 | { |
| 172 | VerifyDriveGeometry myVerifyGeometry(devPath); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 173 | myVerifyGeometry.geometryOkay(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 174 | break; |
| 175 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 176 | case Volume::EraseMethod::LogicalOverWrite: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 177 | { |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 178 | Pattern myErasePattern(devPath); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 179 | myErasePattern.writePattern(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 180 | break; |
| 181 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 182 | case Volume::EraseMethod::LogicalVerify: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 183 | { |
John Edward Broadbent | 7f2ab64 | 2021-11-11 21:00:38 -0800 | [diff] [blame] | 184 | Pattern myErasePattern(devPath); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 185 | myErasePattern.verifyPattern(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 186 | break; |
| 187 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 188 | case Volume::EraseMethod::VendorSanitize: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 189 | { |
John Edward Broadbent | 605085a | 2021-11-05 13:45:45 -0700 | [diff] [blame] | 190 | Sanitize mySanitize(devPath); |
| 191 | mySanitize.doSanitize(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 192 | break; |
| 193 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 194 | case Volume::EraseMethod::ZeroOverWrite: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 195 | { |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 196 | Zero myZero(devPath); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 197 | myZero.writeZero(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 198 | break; |
| 199 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 200 | case Volume::EraseMethod::ZeroVerify: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 201 | { |
John Edward Broadbent | 4bc8a10 | 2021-12-30 16:11:49 -0800 | [diff] [blame] | 202 | Zero myZero(devPath); |
John Edward Broadbent | a6e3b99 | 2022-03-17 14:33:15 -0700 | [diff] [blame] | 203 | myZero.verifyZero(); |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 204 | break; |
| 205 | } |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 206 | case Volume::EraseMethod::SecuredLocked: |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 207 | { |
John Edward Broadbent | f59b729 | 2022-02-15 15:07:15 -0800 | [diff] [blame] | 208 | if (isLocked()) |
| 209 | { |
| 210 | lock(); |
| 211 | } |
| 212 | // TODO: implement hardware locking |
| 213 | // Until that is done, we can lock using eStoraged::lock() |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 214 | break; |
| 215 | } |
| 216 | } |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 219 | void EStoraged::lock() |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 220 | { |
John Edward Broadbent | 4e13b0a | 2021-11-15 15:21:59 -0800 | [diff] [blame] | 221 | std::string msg = "OpenBMC.0.1.DriveLock"; |
| 222 | lg2::info("Starting lock", "REDFISH_MESSAGE_ID", msg); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 223 | |
| 224 | unmountFilesystem(); |
| 225 | deactivateLuksDev(); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 228 | void EStoraged::unlock(std::vector<uint8_t> password) |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 229 | { |
John Edward Broadbent | 4e13b0a | 2021-11-15 15:21:59 -0800 | [diff] [blame] | 230 | std::string msg = "OpenBMC.0.1.DriveUnlock"; |
| 231 | lg2::info("Starting unlock", "REDFISH_MESSAGE_ID", msg); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 232 | |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 233 | activateLuksDev(std::move(password)); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 234 | mountFilesystem(); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 235 | } |
| 236 | |
John Wedig | 8d5a3a0 | 2022-09-29 15:25:58 -0700 | [diff] [blame^] | 237 | void EStoraged::changePassword(const std::vector<uint8_t>& oldPassword, |
| 238 | const std::vector<uint8_t>& newPassword) |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 239 | { |
John Edward Broadbent | e6ffe70 | 2021-10-14 14:03:11 -0700 | [diff] [blame] | 240 | lg2::info("Starting change password", "REDFISH_MESSAGE_ID", |
| 241 | std::string("OpenBMC.0.1.DrivePasswordChanged")); |
John Wedig | 8d5a3a0 | 2022-09-29 15:25:58 -0700 | [diff] [blame^] | 242 | |
| 243 | CryptHandle cryptHandle = loadLuksHeader(); |
| 244 | |
| 245 | int retval = cryptIface->cryptKeyslotChangeByPassphrase( |
| 246 | cryptHandle.get(), CRYPT_ANY_SLOT, CRYPT_ANY_SLOT, |
| 247 | reinterpret_cast<const char*>(oldPassword.data()), oldPassword.size(), |
| 248 | reinterpret_cast<const char*>(newPassword.data()), newPassword.size()); |
| 249 | if (retval < 0) |
| 250 | { |
| 251 | lg2::error("Failed to change password", "REDFISH_MESSAGE_ID", |
| 252 | std::string("OpenBMC.0.1.DrivePasswordChangeFail")); |
| 253 | throw InternalFailure(); |
| 254 | } |
| 255 | |
| 256 | lg2::info("Successfully changed password for {DEV}", "DEV", devPath, |
| 257 | "REDFISH_MESSAGE_ID", |
| 258 | std::string("OpenBMC.0.1.DrivePasswordChangeSuccess")); |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 261 | bool EStoraged::isLocked() const |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 262 | { |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 263 | return lockedProperty; |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 266 | std::string_view EStoraged::getMountPoint() const |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 267 | { |
| 268 | return mountPoint; |
| 269 | } |
| 270 | |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 271 | void EStoraged::formatLuksDev(std::vector<uint8_t> password) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 272 | { |
| 273 | lg2::info("Formatting device {DEV}", "DEV", devPath, "REDFISH_MESSAGE_ID", |
| 274 | std::string("OpenBMC.0.1.FormatLuksDev")); |
| 275 | |
| 276 | /* Generate the volume key. */ |
| 277 | const std::size_t keySize = 64; |
| 278 | std::vector<uint8_t> volumeKey(keySize); |
| 279 | if (RAND_bytes(volumeKey.data(), keySize) != 1) |
| 280 | { |
| 281 | lg2::error("Failed to create volume key", "REDFISH_MESSAGE_ID", |
| 282 | std::string("OpenBMC.0.1.FormatLuksDevFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 283 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 284 | } |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 285 | |
| 286 | /* Create the handle. */ |
| 287 | CryptHandle cryptHandle(devPath); |
| 288 | |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 289 | /* Format the LUKS encrypted device. */ |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 290 | int retval = cryptIface->cryptFormat( |
| 291 | cryptHandle.get(), CRYPT_LUKS2, "aes", "xts-plain64", nullptr, |
| 292 | reinterpret_cast<const char*>(volumeKey.data()), volumeKey.size(), |
| 293 | nullptr); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 294 | if (retval < 0) |
| 295 | { |
| 296 | lg2::error("Failed to format encrypted device: {RETVAL}", "RETVAL", |
| 297 | retval, "REDFISH_MESSAGE_ID", |
| 298 | std::string("OpenBMC.0.1.FormatLuksDevFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 299 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /* Device is now encrypted. */ |
| 303 | locked(true); |
| 304 | |
| 305 | /* Set the password. */ |
| 306 | retval = cryptIface->cryptKeyslotAddByVolumeKey( |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 307 | cryptHandle.get(), CRYPT_ANY_SLOT, nullptr, 0, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 308 | reinterpret_cast<const char*>(password.data()), password.size()); |
| 309 | |
| 310 | if (retval < 0) |
| 311 | { |
| 312 | lg2::error("Failed to set encryption password", "REDFISH_MESSAGE_ID", |
| 313 | std::string("OpenBMC.0.1.FormatLuksDevFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 314 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | lg2::info("Encrypted device {DEV} successfully formatted", "DEV", devPath, |
| 318 | "REDFISH_MESSAGE_ID", |
| 319 | std::string("OpenBMC.0.1.FormatLuksDevSuccess")); |
| 320 | } |
| 321 | |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 322 | CryptHandle EStoraged::loadLuksHeader() |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 323 | { |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 324 | |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 325 | CryptHandle cryptHandle(devPath); |
| 326 | |
| 327 | int retval = cryptIface->cryptLoad(cryptHandle.get(), CRYPT_LUKS2, nullptr); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 328 | if (retval < 0) |
| 329 | { |
| 330 | lg2::error("Failed to load LUKS header: {RETVAL}", "RETVAL", retval, |
| 331 | "REDFISH_MESSAGE_ID", |
| 332 | std::string("OpenBMC.0.1.ActivateLuksDevFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 333 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 334 | } |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 335 | return cryptHandle; |
| 336 | } |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 337 | |
John Edward Broadbent | 91c1ec1 | 2022-05-20 16:51:43 -0700 | [diff] [blame] | 338 | Drive::DriveEncryptionState EStoraged::findEncryptionStatus() |
| 339 | { |
| 340 | try |
| 341 | { |
| 342 | loadLuksHeader(); |
| 343 | return Drive::DriveEncryptionState::Encrypted; |
| 344 | } |
| 345 | catch (...) |
| 346 | { |
| 347 | return Drive::DriveEncryptionState::Unknown; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | void EStoraged::activateLuksDev(std::vector<uint8_t> password) |
| 352 | { |
| 353 | lg2::info("Activating LUKS dev {DEV}", "DEV", devPath, "REDFISH_MESSAGE_ID", |
| 354 | std::string("OpenBMC.0.1.ActivateLuksDev")); |
| 355 | |
| 356 | /* Create the handle. */ |
| 357 | CryptHandle cryptHandle = loadLuksHeader(); |
| 358 | |
| 359 | int retval = cryptIface->cryptActivateByPassphrase( |
John Edward Broadbent | b2c86be | 2022-04-15 11:45:53 -0700 | [diff] [blame] | 360 | cryptHandle.get(), containerName.c_str(), CRYPT_ANY_SLOT, |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 361 | reinterpret_cast<const char*>(password.data()), password.size(), 0); |
| 362 | |
| 363 | if (retval < 0) |
| 364 | { |
| 365 | lg2::error("Failed to activate LUKS dev: {RETVAL}", "RETVAL", retval, |
| 366 | "REDFISH_MESSAGE_ID", |
| 367 | std::string("OpenBMC.0.1.ActivateLuksDevFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 368 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /* Device is now unlocked. */ |
| 372 | locked(false); |
| 373 | |
| 374 | lg2::info("Successfully activated LUKS dev {DEV}", "DEV", devPath, |
| 375 | "REDFISH_MESSAGE_ID", |
| 376 | std::string("OpenBMC.0.1.ActivateLuksDevSuccess")); |
| 377 | } |
| 378 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 379 | void EStoraged::createFilesystem() |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 380 | { |
| 381 | /* Run the command to create the filesystem. */ |
| 382 | int retval = fsIface->runMkfs(containerName); |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 383 | if (retval != 0) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 384 | { |
| 385 | lg2::error("Failed to create filesystem: {RETVAL}", "RETVAL", retval, |
| 386 | "REDFISH_MESSAGE_ID", |
| 387 | std::string("OpenBMC.0.1.CreateFilesystemFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 388 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 389 | } |
| 390 | lg2::info("Successfully created filesystem for /dev/mapper/{CONTAINER}", |
| 391 | "CONTAINER", containerName, "REDFISH_MESSAGE_ID", |
| 392 | std::string("OpenBMC.0.1.CreateFilesystemSuccess")); |
| 393 | } |
| 394 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 395 | void EStoraged::mountFilesystem() |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 396 | { |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 397 | /* |
| 398 | * Create directory for the filesystem, if it's not already present. It |
| 399 | * might already exist if, for example, the BMC reboots after creating the |
| 400 | * directory. |
| 401 | */ |
| 402 | if (!fsIface->directoryExists(std::filesystem::path(mountPoint))) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 403 | { |
John Wedig | b17f825 | 2022-01-12 14:24:26 -0800 | [diff] [blame] | 404 | bool success = |
| 405 | fsIface->createDirectory(std::filesystem::path(mountPoint)); |
| 406 | if (!success) |
| 407 | { |
| 408 | lg2::error("Failed to create mount point: {DIR}", "DIR", mountPoint, |
| 409 | "REDFISH_MESSAGE_ID", |
| 410 | std::string("OpenBMC.0.1.MountFilesystemFail")); |
| 411 | throw InternalFailure(); |
| 412 | } |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | /* Run the command to mount the filesystem. */ |
| 416 | std::string luksContainer("/dev/mapper/" + containerName); |
| 417 | int retval = fsIface->doMount(luksContainer.c_str(), mountPoint.c_str(), |
| 418 | "ext4", 0, nullptr); |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 419 | if (retval != 0) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 420 | { |
| 421 | lg2::error("Failed to mount filesystem: {RETVAL}", "RETVAL", retval, |
| 422 | "REDFISH_MESSAGE_ID", |
| 423 | std::string("OpenBMC.0.1.MountFilesystemFail")); |
| 424 | bool removeSuccess = |
| 425 | fsIface->removeDirectory(std::filesystem::path(mountPoint)); |
| 426 | if (!removeSuccess) |
| 427 | { |
| 428 | lg2::error("Failed to remove mount point: {DIR}", "DIR", mountPoint, |
| 429 | "REDFISH_MESSAGE_ID", |
| 430 | std::string("OpenBMC.0.1.MountFilesystemFail")); |
| 431 | } |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 432 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | lg2::info("Successfully mounted filesystem at {DIR}", "DIR", mountPoint, |
| 436 | "REDFISH_MESSAGE_ID", |
| 437 | std::string("OpenBMC.0.1.MountFilesystemSuccess")); |
| 438 | } |
| 439 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 440 | void EStoraged::unmountFilesystem() |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 441 | { |
| 442 | int retval = fsIface->doUnmount(mountPoint.c_str()); |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 443 | if (retval != 0) |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 444 | { |
| 445 | lg2::error("Failed to unmount filesystem: {RETVAL}", "RETVAL", retval, |
| 446 | "REDFISH_MESSAGE_ID", |
| 447 | std::string("OpenBMC.0.1.UnmountFilesystemFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 448 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | /* Remove the mount point. */ |
| 452 | bool success = fsIface->removeDirectory(std::filesystem::path(mountPoint)); |
| 453 | if (!success) |
| 454 | { |
| 455 | lg2::error("Failed to remove mount point {DIR}", "DIR", mountPoint, |
| 456 | "REDFISH_MESSAGE_ID", |
| 457 | std::string("OpenBMC.0.1.UnmountFilesystemFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 458 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | lg2::info("Successfully unmounted filesystem at {DIR}", "DIR", mountPoint, |
| 462 | "REDFISH_MESSAGE_ID", |
| 463 | std::string("OpenBMC.0.1.MountFilesystemSuccess")); |
| 464 | } |
| 465 | |
Ed Tanous | 82897c3 | 2022-02-21 14:11:59 -0800 | [diff] [blame] | 466 | void EStoraged::deactivateLuksDev() |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 467 | { |
| 468 | lg2::info("Deactivating LUKS device {DEV}", "DEV", devPath, |
| 469 | "REDFISH_MESSAGE_ID", |
| 470 | std::string("OpenBMC.0.1.DeactivateLuksDev")); |
| 471 | |
| 472 | int retval = cryptIface->cryptDeactivate(nullptr, containerName.c_str()); |
| 473 | if (retval < 0) |
| 474 | { |
| 475 | lg2::error("Failed to deactivate crypt device: {RETVAL}", "RETVAL", |
| 476 | retval, "REDFISH_MESSAGE_ID", |
| 477 | std::string("OpenBMC.0.1.DeactivateLuksDevFail")); |
John Wedig | 972c3fa | 2021-12-29 17:30:41 -0800 | [diff] [blame] | 478 | throw InternalFailure(); |
John Wedig | b810c92 | 2021-11-17 16:38:03 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | /* Device is now locked. */ |
| 482 | locked(true); |
| 483 | |
| 484 | lg2::info("Successfully deactivated LUKS device {DEV}", "DEV", devPath, |
| 485 | "REDFISH_MESSAGE_ID", |
| 486 | std::string("OpenBMC.0.1.DeactivateLuksDevSuccess")); |
| 487 | } |
| 488 | |
John Wedig | 67a4744 | 2022-04-05 17:21:29 -0700 | [diff] [blame] | 489 | void EStoraged::locked(bool isLocked) |
| 490 | { |
| 491 | lockedProperty = isLocked; |
| 492 | } |
| 493 | |
John Wedig | 2098dab | 2021-09-14 13:56:28 -0700 | [diff] [blame] | 494 | } // namespace estoraged |