9#include <botan/tpm2_context.h>
11#include <botan/tpm2_key.h>
12#include <botan/tpm2_session.h>
14#include <botan/internal/fmt.h>
15#include <botan/internal/loadstor.h>
16#include <botan/internal/stl_util.h>
17#include <botan/internal/tpm2_algo_mappings.h>
18#include <botan/internal/tpm2_util.h>
20#include <tss2/tss2_esys.h>
21#include <tss2/tss2_tcti.h>
22#include <tss2/tss2_tctildr.h>
24#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
25 #include <botan/internal/tpm2_crypto_backend.h>
32constexpr TPM2_HANDLE storage_root_key_handle = TPM2_HR_PERSISTENT + 1;
37 TSS2_TCTI_CONTEXT* m_tcti_ctx;
40#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
41 std::unique_ptr<CryptoCallbackState> m_crypto_callback_state;
46#if defined(BOTAN_TSS2_SUPPORTS_CRYPTO_CALLBACKS) and defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
55 return std::shared_ptr<Context>(
new Context(tcti_nameconf.c_str()));
58std::shared_ptr<Context>
Context::create(std::optional<std::string> tcti, std::optional<std::string> conf) {
59 const auto tcti_ptr = tcti.has_value() ? tcti->c_str() :
nullptr;
60 const auto conf_ptr = conf.has_value() ? conf->c_str() :
nullptr;
63 return std::shared_ptr<Context>(
new Context(tcti_ptr, conf_ptr));
66Context::Context(
const char* tcti_nameconf) : m_impl(std::make_unique<Impl>()) {
67 check_rc(
"TCTI Initialization", Tss2_TctiLdr_Initialize(tcti_nameconf, &m_impl->m_tcti_ctx));
69 check_rc(
"TPM2 Initialization", Esys_Initialize(&m_impl->m_ctx, m_impl->m_tcti_ctx,
nullptr ));
73Context::Context(
const char* tcti_name,
const char* tcti_conf) : m_impl(std::make_unique<Impl>()) {
74 check_rc(
"TCTI Initialization", Tss2_TctiLdr_Initialize_Ex(tcti_name, tcti_conf, &m_impl->m_tcti_ctx));
76 check_rc(
"TPM2 Initialization", Esys_Initialize(&m_impl->m_ctx, m_impl->m_tcti_ctx,
nullptr ));
81#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
83 m_impl->m_crypto_callback_state = std::make_unique<CryptoCallbackState>(rng);
87 throw Not_Implemented(
"This build of botan does not provide the TPM2 crypto backend");
92#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
93 return m_impl->m_crypto_callback_state !=
nullptr;
99#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
102 return *m_impl->m_crypto_callback_state;
107 return m_impl->m_ctx;
112uint32_t get_tpm_property(ESYS_CONTEXT* ctx, TPM2_PT property) {
114 constexpr uint32_t property_count = 1;
115 constexpr TPM2_CAP capability = TPM2_CAP_TPM_PROPERTIES;
119 Esys_GetCapability(ctx,
131 BOTAN_ASSERT_NOMSG(capability_data->data.tpmProperties.tpmProperty[0].property == property);
133 return capability_data->data.tpmProperties.tpmProperty[0].value;
136template <TPM2_CAP capability,
typename ReturnT>
137[[nodiscard]] std::vector<ReturnT> get_tpm_property_list(ESYS_CONTEXT* ctx, TPM2_PT property, uint32_t count) {
138 auto extract = [](
const TPMU_CAPABILITIES& caps, uint32_t max_count) {
139 std::vector<ReturnT> result;
140 if constexpr(capability == TPM2_CAP_HANDLES) {
141 const auto to_read = std::min(caps.handles.count, max_count);
142 result.reserve(to_read);
143 for(
size_t i = 0; i < to_read; ++i) {
144 result.push_back(caps.handles.handle[i]);
146 }
else if constexpr(capability == TPM2_CAP_ALGS) {
147 const auto to_read = std::min(caps.algorithms.count, max_count);
148 result.reserve(to_read);
149 for(
size_t i = 0; i < to_read; ++i) {
153 result.push_back(caps.algorithms.algProperties[i].alg);
157 static_assert(capability != TPM2_CAP_HANDLES,
"Unsupported capability");
162 TPMI_YES_NO more_data = TPM2_YES;
163 std::vector<ReturnT> properties;
164 while(more_data == TPM2_YES && count > 0) {
167 Esys_GetCapability(ctx,
179 const auto new_properties = extract(capability_data->data, count);
181 properties.insert(properties.end(), new_properties.begin(), new_properties.end());
182 count -= new_properties.size();
191 constexpr std::array properties = {
192 TPM2_PT_VENDOR_STRING_1, TPM2_PT_VENDOR_STRING_2, TPM2_PT_VENDOR_STRING_3, TPM2_PT_VENDOR_STRING_4};
193 std::array<uint8_t, properties.size() * 4 + 1 > vendor_string{};
199 for(
auto prop : properties) {
208 std::array<uint8_t, 4 + 1 > manufacturer_data{};
209 store_be(std::span{manufacturer_data}.first<4>(), get_tpm_property(m_impl->m_ctx, TPM2_PT_MANUFACTURER));
217 const auto required_alg_ids = [&]() -> std::vector<TPM2_ALG_ID> {
218 std::vector<TPM2_ALG_ID> result;
220 result.push_back(algo_id.value());
224 result.push_back(hash_id.value());
228 result.push_back(block_id->first);
232 result.push_back(cipher_mode_id.value());
236 result.push_back(cipher_spec->algorithm);
237 result.push_back(cipher_spec->mode.sym);
241 result.push_back(sig_padding.value());
245 result.push_back(sig->scheme);
246 result.push_back(sig->details.any.hashAlg);
250 result.push_back(enc_scheme->scheme);
251 if(enc_scheme->scheme == TPM2_ALG_OAEP) {
252 result.push_back(enc_scheme->details.oaep.hashAlg);
257 result.push_back(enc_id.value());
263 if(required_alg_ids.empty()) {
268 const auto algo_caps =
269 get_tpm_property_list<TPM2_CAP_ALGS, TPM2_ALG_ID>(m_impl->m_ctx, TPM2_ALG_FIRST, TPM2_MAX_CAP_ALGS);
272 required_alg_ids.begin(), required_alg_ids.end(), [&](TPM2_ALG_ID
id) { return value_exists(algo_caps, id); });
276 return get_tpm_property(m_impl->m_ctx, TPM2_PT_MAX_DIGEST);
285 return get_tpm_property_list<TPM2_CAP_HANDLES, ESYS_TR>(m_impl->m_ctx, TPM2_TRANSIENT_FIRST, TPM2_MAX_CAP_HANDLES);
298 if(occupied_handles.size() >= TPM2_MAX_CAP_HANDLES) {
303 for(
TPM2_HANDLE i = TPM2_PERSISTENT_FIRST; i < TPM2_PERSISTENT_LAST; ++i) {
313 return get_tpm_property_list<TPM2_CAP_HANDLES, TPM2_HANDLE>(
314 m_impl->m_ctx, TPM2_PERSISTENT_FIRST, TPM2_MAX_CAP_HANDLES);
319 std::span<const uint8_t> auth_value,
320 std::optional<TPM2_HANDLE> persistent_handle) {
324 "Persistent handle already in use");
325 BOTAN_ARG_CHECK(!handles.has_persistent_handle(),
"Key already has a persistent handle assigned");
329 const TPMI_DH_PERSISTENT new_persistent_handle = [&] {
330 if(persistent_handle.has_value()) {
331 return persistent_handle.value();
335 return free_persistent_handle.value();
343 Esys_EvictControl(m_impl->m_ctx,
345 handles.transient_handle(),
349 new_persistent_handle,
356 if(!auth_value.empty()) {
358 check_rc(
"Esys_TR_SetAuth", Esys_TR_SetAuth(m_impl->m_ctx, handles.transient_handle(), &user_auth));
368 BOTAN_ASSERT_EQUAL(new_persistent_handle, handles.persistent_handle(),
"key was persisted at the correct location");
370 return new_persistent_handle;
376 auto& handles = key->handles();
377 BOTAN_ARG_CHECK(handles.has_persistent_handle(),
"Key does not have a persistent handle assigned");
382 ESYS_TR no_new_handle = ESYS_TR_NONE;
384 Esys_EvictControl(m_impl->m_ctx,
386 handles.transient_handle(),
392 BOTAN_ASSERT(no_new_handle == ESYS_TR_NONE,
"When deleting a key, no new handle is returned");
396 handles._disengage();
401 Esys_Finalize(&m_impl->m_ctx);
402 Tss2_TctiLdr_Finalize(&m_impl->m_tcti_ctx);
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_STATE_CHECK(expr)
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
#define BOTAN_ASSERT_NONNULL(ptr)
#define BOTAN_ARG_CHECK(expr, msg)
#define BOTAN_ASSERT(expr, assertion_made)
#define BOTAN_ASSERT_UNREACHABLE()
Helper class to ease in-place marshalling of concatenated fixed-length values.
constexpr void append(std::span< const uint8_t > buffer)
constexpr size_t remaining_capacity() const
std::optional< TPM2_HANDLE > find_free_persistent_handle() const
bool supports_algorithm(std::string_view algo_name) const
void use_botan_crypto_backend(const std::shared_ptr< Botan::RandomNumberGenerator > &rng)
static bool supports_botan_crypto_backend() noexcept
std::vector< TPM2_HANDLE > persistent_handles() const
size_t max_random_bytes_per_request() const
std::vector< ESYS_TR > transient_handles() const
TPM2_HANDLE persist(TPM2::PrivateKey &key, const SessionBundle &sessions, std::span< const uint8_t > auth_value={}, std::optional< TPM2_HANDLE > persistent_handle=std::nullopt)
Makes key persistent at location persistent_handle or any free.
Context(const Context &)=delete
static std::shared_ptr< Context > create(const std::string &tcti_nameconf)
std::string manufacturer() const
std::string vendor() const
ESYS_CONTEXT * esys_context() noexcept
std::unique_ptr< TPM2::PrivateKey > storage_root_key(std::span< const uint8_t > auth_value, const SessionBundle &sessions)
void evict(std::unique_ptr< TPM2::PrivateKey > key, const SessionBundle &sessions)
Evicts a persistent key from the TPM. The key cannot be used after.
bool uses_botan_crypto_backend() const noexcept
static std::unique_ptr< PrivateKey > load_persistent(const std::shared_ptr< Context > &ctx, TPM2_HANDLE persistent_object_handle, std::span< const uint8_t > auth_value, const SessionBundle &sessions)
void enable_crypto_callbacks(const std::shared_ptr< Context > &ctx)
std::optional< TPMT_SIG_SCHEME > rsa_signature_scheme_botan_to_tss2(std::string_view name)
std::optional< TPMI_ALG_SIG_SCHEME > rsa_signature_padding_botan_to_tss2(std::string_view padding_name) noexcept
std::optional< TPMI_ALG_ASYM_SCHEME > rsa_encryption_padding_botan_to_tss2(std::string_view name) noexcept
constexpr void check_rc(std::string_view location, TSS2_RC rc)
std::optional< std::pair< TPMI_ALG_SYM, TPM2_KEY_BITS > > block_cipher_botan_to_tss2(std::string_view cipher_name) noexcept
constexpr auto out_persistent_handle(Object &object)
std::optional< TPMT_SYM_DEF > cipher_botan_to_tss2(std::string_view algo_name)
std::optional< TPMT_RSA_DECRYPT > rsa_encryption_scheme_botan_to_tss2(std::string_view padding)
std::unique_ptr< T, esys_liberator > unique_esys_ptr
A unique pointer type for ESYS handles that automatically frees the handle.
std::optional< TPMI_ALG_HASH > hash_algo_botan_to_tss2(std::string_view hash_name) noexcept
constexpr auto out_transient_handle(Object &object)
std::optional< TPM2_ALG_ID > asymmetric_algorithm_botan_to_tss2(std::string_view algo_name) noexcept
constexpr void copy_into(T &dest, std::span< const uint8_t > data)
std::optional< TPMI_ALG_SYM_MODE > cipher_mode_botan_to_tss2(std::string_view mode_name) noexcept
constexpr auto out_ptr(T &outptr) noexcept
const char * cast_uint8_ptr_to_char(const uint8_t *b)
bool value_exists(const std::vector< T > &vec, const OT &val)
constexpr auto store_be(ParamTs &&... params)
uint32_t ESYS_TR
Forward declaration of TSS2 type for convenience.
uint32_t TPM2_HANDLE
Forward declaration of TSS2 type for convenience.