blob: d99f6bfd1c4731e017721d5fde452ea9d7027cb7 [file] [log] [blame]
#include "cryptErase.hpp"
#include "cryptsetupInterface.hpp"
#include "erase.hpp"
#include <libcryptsetup.h>
#include <phosphor-logging/lg2.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
#include <memory>
#include <string>
#include <string_view>
namespace estoraged
{
using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
using sdbusplus::xyz::openbmc_project::Common::Error::ResourceNotFound;
CryptErase::CryptErase(
std::string_view devPathIn,
std::unique_ptr<estoraged::CryptsetupInterface> inCryptIface) :
Erase(devPathIn),
cryptIface(std::move(inCryptIface))
{}
void CryptErase::doErase()
{
/* get cryptHandle */
CryptHandle cryptHandle(std::string(devPath).c_str());
if (cryptHandle.get() == nullptr)
{
lg2::error("Failed to initialize crypt device", "REDFISH_MESSAGE_ID",
std::string("OpenBMC.0.1.EraseFailure"));
throw ResourceNotFound();
}
/* cryptLoad */
if (cryptIface.get()->cryptLoad(cryptHandle.get(), CRYPT_LUKS2, nullptr) !=
0)
{
lg2::error("Failed to load the key slots for destruction",
"REDFISH_MESSAGE_ID",
std::string("OpenBMC.0.1.EraseFailure"));
throw ResourceNotFound();
}
/* find key slots */
int nKeySlots = cryptIface.get()->cryptKeySlotMax(CRYPT_LUKS2);
if (nKeySlots < 0)
{
lg2::error("Failed to find the max keyslots", "REDFISH_MESSAGE_ID",
std::string("OpenBMC.0.1.EraseFailure"));
throw ResourceNotFound();
}
if (nKeySlots == 0)
{
lg2::error("Max keyslots should never be zero", "REDFISH_MESSAGE_ID",
std::string("OpenBMC.0.1.EraseFailure"));
throw ResourceNotFound();
}
/* destory working keyslots */
bool keySlotIssue = false;
for (int i = 0; i < nKeySlots; i++)
{
crypt_keyslot_info ki =
cryptIface.get()->cryptKeySlotStatus(cryptHandle.get(), i);
if (ki == CRYPT_SLOT_ACTIVE || ki == CRYPT_SLOT_ACTIVE_LAST)
{
if (cryptIface.get()->cryptKeyslotDestroy(cryptHandle.get(), i) !=
0)
{
lg2::error(
"Estoraged erase failed to destroy keyslot, continuing",
"REDFISH_MESSAGE_ID",
std::string("eStorageD.1.0.EraseFailure"));
keySlotIssue = true;
}
}
}
if (keySlotIssue)
{
throw InternalFailure();
}
}
} // namespace estoraged