1 #pragma once 2 3 #include "cryptsetupInterface.hpp" 4 #include "filesystemInterface.hpp" 5 #include "util.hpp" 6 7 #include <libcryptsetup.h> 8 9 #include <sdbusplus/asio/object_server.hpp> 10 #include <sdbusplus/bus.hpp> 11 #include <sdbusplus/exception.hpp> 12 #include <sdbusplus/server/object.hpp> 13 #include <util.hpp> 14 #include <xyz/openbmc_project/Inventory/Item/Drive/server.hpp> 15 #include <xyz/openbmc_project/Inventory/Item/Volume/server.hpp> 16 17 #include <filesystem> 18 #include <memory> 19 #include <string> 20 #include <string_view> 21 #include <vector> 22 23 namespace estoraged 24 { 25 using estoraged::Cryptsetup; 26 using estoraged::Filesystem; 27 using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Drive; 28 using sdbusplus::xyz::openbmc_project::Inventory::Item::server::Volume; 29 30 /** @class eStoraged 31 * @brief eStoraged object to manage a LUKS encrypted storage device. 32 */ 33 class EStoraged 34 { 35 public: 36 /** @brief Constructor for eStoraged 37 * 38 * @param[in] server - sdbusplus asio object server 39 * @param[in] configPath - path of the config object from Entity Manager 40 * @param[in] devPath - path to device file, e.g. /dev/mmcblk0 41 * @param[in] luksName - name for the LUKS container 42 * @param[in] size - size of the drive in bytes 43 * @param[in] lifeTime - percent of lifetime remaining for a drive 44 * @param[in] partNumber - part number for the storage device 45 * @param[in] serialNumber - serial number for the storage device 46 * @param[in] locationCode - location code for the storage device 47 * @param[in] cryptInterface - (optional) pointer to CryptsetupInterface 48 * object 49 * @param[in] fsInterface - (optional) pointer to FilesystemInterface 50 * object 51 */ 52 EStoraged(sdbusplus::asio::object_server& server, 53 const std::string& configPath, const std::string& devPath, 54 const std::string& luksName, uint64_t size, uint8_t lifeTime, 55 const std::string& partNumber, const std::string& serialNumber, 56 const std::string& locationCode, 57 std::unique_ptr<CryptsetupInterface> cryptInterface = 58 std::make_unique<Cryptsetup>(), 59 std::unique_ptr<FilesystemInterface> fsInterface = 60 std::make_unique<Filesystem>()); 61 62 /** @brief Destructor for eStoraged. */ 63 ~EStoraged(); 64 65 EStoraged& operator=(const EStoraged&) = delete; 66 EStoraged(const EStoraged&) = delete; 67 EStoraged(EStoraged&&) = default; 68 EStoraged& operator=(EStoraged&&) = delete; 69 70 /** @brief Format the LUKS encrypted device and create empty filesystem. 71 * 72 * @param[in] password - password to set for the LUKS device. 73 * @param[in] type - filesystem type, e.g. ext4 74 */ 75 void formatLuks(const std::vector<uint8_t>& password, 76 Volume::FilesystemType type); 77 78 /** @brief Erase the contents of the storage device. 79 * 80 * @param[in] eraseType - type of erase operation. 81 */ 82 void erase(Volume::EraseMethod eraseType); 83 84 /** @brief Unmount filesystem and lock the LUKS device. 85 */ 86 void lock(); 87 88 /** @brief Unlock device and mount the filesystem. 89 * 90 * @param[in] password - password for the LUKS device. 91 */ 92 void unlock(std::vector<uint8_t> password); 93 94 /** @brief Change the password for the LUKS device. 95 * 96 * @param[in] oldPassword - old password for the LUKS device. 97 * @param[in] newPassword - new password for the LUKS device. 98 */ 99 void changePassword(const std::vector<uint8_t>& oldPassword, 100 const std::vector<uint8_t>& newPassword); 101 102 /** @brief Check if the LUKS device is currently locked. */ 103 bool isLocked() const; 104 105 /** @brief Get the mount point for the filesystem on the LUKS device. */ 106 std::string_view getMountPoint() const; 107 108 /** @brief Get the path to the mapped crypt device. */ 109 std::string_view getCryptDevicePath() const; 110 111 private: 112 /** @brief Full path of the device file, e.g. /dev/mmcblk0. */ 113 std::string devPath; 114 115 /** @brief Name of the LUKS container. */ 116 std::string containerName; 117 118 /** @brief Mount point for the filesystem. */ 119 std::string mountPoint; 120 121 /** @brief Indicates whether the LUKS device is currently locked. */ 122 bool lockedProperty{false}; 123 124 /** @brief Pointer to cryptsetup interface object. 125 * @details This is used to mock out the cryptsetup functions. 126 */ 127 std::unique_ptr<CryptsetupInterface> cryptIface; 128 129 /** @brief Pointer to filesystem interface object. 130 * @details This is used to mock out filesystem operations. 131 */ 132 std::unique_ptr<FilesystemInterface> fsIface; 133 134 /** @brief Path where the mapped crypt device gets created. */ 135 const std::string cryptDevicePath; 136 137 /** @brief D-Bus object server. */ 138 sdbusplus::asio::object_server& objectServer; 139 140 /** @brief D-Bus interface for the logical volume. */ 141 std::shared_ptr<sdbusplus::asio::dbus_interface> volumeInterface; 142 143 /** @brief D-Bus interface for the physical drive. */ 144 std::shared_ptr<sdbusplus::asio::dbus_interface> driveInterface; 145 146 /** @brief D-Bus interface for the location type of the drive. */ 147 std::shared_ptr<sdbusplus::asio::dbus_interface> embeddedLocationInterface; 148 149 /** @brief D-Bus interface for the location code of the drive. */ 150 std::shared_ptr<sdbusplus::asio::dbus_interface> locationCodeInterface; 151 152 /** @brief D-Bus interface for the asset information. */ 153 std::shared_ptr<sdbusplus::asio::dbus_interface> assetInterface; 154 155 /** @brief Association between chassis and drive. */ 156 std::shared_ptr<sdbusplus::asio::dbus_interface> association; 157 158 /** @brief Indicates whether the LUKS header is on the disk. */ 159 Drive::DriveEncryptionState encryptionStatus{ 160 Drive::DriveEncryptionState::Unknown}; 161 162 /** @brief Format LUKS encrypted device. 163 * 164 * @param[in] password - password to set for the LUKS device. 165 */ 166 void formatLuksDev(std::vector<uint8_t> password); 167 168 /** @brief check the LUKS header, for devPath 169 * 170 * @returns a CryptHandle to the LUKS drive 171 */ 172 CryptHandle loadLuksHeader(); 173 174 /** @brief Unlock the device. 175 * 176 * @param[in] password - password to activate the LUKS device. 177 */ 178 179 Drive::DriveEncryptionState findEncryptionStatus(); 180 181 void activateLuksDev(std::vector<uint8_t> password); 182 183 /** @brief Create the filesystem on the LUKS device. 184 * @details The LUKS device should already be activated, i.e. unlocked. 185 */ 186 void createFilesystem(); 187 188 /** @brief Deactivate the LUKS device. 189 * @details The filesystem is assumed to be unmounted already. 190 */ 191 void deactivateLuksDev(); 192 193 /** @brief Mount the filesystem. 194 * @details The filesystem should already exist and the LUKS device should 195 * be unlocked already. 196 */ 197 void mountFilesystem(); 198 199 /** @brief Unmount the filesystem. */ 200 void unmountFilesystem(); 201 }; 202 203 } // namespace estoraged 204