blob: a50d30b394b721fde5fe8dc9b35dee2787745c88 [file] [log] [blame]
// Copyright 2022 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <optional>
#include <utility>
#include <libhwsec-foundation/status/status_chain_macros.h>
#include "libhwsec/backend/backend.h"
#include "libhwsec/middleware/middleware.h"
#include "libhwsec/structures/key.h"
namespace hwsec {
ScopedKey::ScopedKey(ScopedKey&& scoped_key)
: key_(std::move(scoped_key.key_)),
middleware_derivative_(std::move(scoped_key.middleware_derivative_)) {
scoped_key.key_ = std::nullopt;
}
ScopedKey::~ScopedKey() {
Invalidate();
}
void ScopedKey::Invalidate() {
if (key_.has_value()) {
Key key = GetKey();
key_ = std::nullopt;
RETURN_IF_ERROR(Middleware(middleware_derivative_)
.CallSync<&hwsec::Backend::KeyManagerment::Flush>(key))
.With([](auto linker) {
return linker.LogError() << "Failed to flush scoped key";
})
.ReturnVoid();
}
}
ScopedKey& ScopedKey::operator=(ScopedKey&& scoped_key) {
Invalidate();
key_ = std::move(scoped_key.key_);
middleware_derivative_ = std::move(scoped_key.middleware_derivative_);
scoped_key.key_ = std::nullopt;
return *this;
}
ScopedKey::ScopedKey(Key key, MiddlewareDerivative middleware_derivative)
: key_(key), middleware_derivative_(std::move(middleware_derivative)) {}
const Key& ScopedKey::GetKey() const {
return key_.value();
}
} // namespace hwsec