Botan 3.14.1
Crypto and TLS for C&
tpm2_context.cpp
Go to the documentation of this file.
1/*
2* TPM 2 interface
3* (C) 2024 Jack Lloyd
4* (C) 2024 René Meusel, Amos Treiber - Rohde & Schwarz Cybersecurity GmbH, financed by LANCOM Systems GmbH
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#include <botan/tpm2_context.h>
10
11#include <botan/tpm2_key.h>
12#include <botan/tpm2_session.h>
13
14#include <botan/internal/buffer_stuffer.h>
15#include <botan/internal/int_utils.h>
16#include <botan/internal/loadstor.h>
17#include <botan/internal/stl_util.h>
18#include <botan/internal/tpm2_algo_mappings.h>
19#include <botan/internal/tpm2_util.h>
20#include <algorithm>
21
22#include <tss2/tss2_esys.h>
23#include <tss2/tss2_tcti.h>
24#include <tss2/tss2_tctildr.h>
25
26#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
27 #include <botan/tpm2_crypto_backend.h>
28 #include <botan/internal/tpm2_crypto_backend_impl.h>
29#endif
30
31namespace Botan::TPM2 {
32
33namespace {
34
35constexpr TPM2_HANDLE storage_root_key_handle = TPM2_HR_PERSISTENT + 1;
36
37} // namespace
38
39struct Context::Impl {
40 ESYS_CONTEXT* m_ctx{}; /// m_ctx may be owned by the library user (see m_external)
41 bool m_external{};
42
43#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
44 std::unique_ptr<CryptoCallbackState> m_crypto_callback_state;
45#endif
46};
47
49#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
51#else
52 return false;
53#endif
54}
55
56std::shared_ptr<Context> Context::create(const std::string& tcti_nameconf) {
57 TSS2_TCTI_CONTEXT* tcti_ctx = nullptr;
58 ESYS_CONTEXT* esys_ctx = nullptr;
59 check_rc("TCTI Initialization", Tss2_TctiLdr_Initialize(tcti_nameconf.c_str(), &tcti_ctx));
60 BOTAN_ASSERT_NONNULL(tcti_ctx);
61 check_rc("TPM2 Initialization", Esys_Initialize(&esys_ctx, tcti_ctx, nullptr /* ABI version */));
62 BOTAN_ASSERT_NONNULL(esys_ctx);
63
64 // We cannot std::make_shared as the constructor is private
65 return std::shared_ptr<Context>(new Context(esys_ctx, false /* context is managed by us */));
66}
67
68std::shared_ptr<Context> Context::create(std::optional<std::string> tcti, std::optional<std::string> conf) {
69 const char* const tcti_ptr = tcti.has_value() ? tcti->c_str() : nullptr;
70 const char* const conf_ptr = conf.has_value() ? conf->c_str() : nullptr;
71
72 TSS2_TCTI_CONTEXT* tcti_ctx = nullptr;
73 ESYS_CONTEXT* esys_ctx = nullptr;
74 check_rc("TCTI Initialization", Tss2_TctiLdr_Initialize_Ex(tcti_ptr, conf_ptr, &tcti_ctx));
75 BOTAN_ASSERT_NONNULL(tcti_ctx);
76 check_rc("TPM2 Initialization", Esys_Initialize(&esys_ctx, tcti_ctx, nullptr /* ABI version */));
77 BOTAN_ASSERT_NONNULL(esys_ctx);
78
79 // We cannot std::make_shared as the constructor is private
80 return std::shared_ptr<Context>(new Context(esys_ctx, false /* context is managed by us */));
81}
82
83std::shared_ptr<Context> Context::create(ESYS_CONTEXT* esys_ctx) {
84 BOTAN_ARG_CHECK(esys_ctx != nullptr, "provided esys_ctx must not be null");
85
86 // We cannot std::make_shared as the constructor is private
87 return std::shared_ptr<Context>(new Context(esys_ctx, true /* context is managed externally */));
88}
89
90Context::Context(ESYS_CONTEXT* ctx, bool external) : m_impl(std::make_unique<Impl>()) {
91 m_impl->m_ctx = ctx;
92 m_impl->m_external = external;
93 BOTAN_ASSERT_NONNULL(m_impl->m_ctx);
94}
95
96Context::Context(Context&&) noexcept = default;
97Context& Context::operator=(Context&&) noexcept = default;
98
99void Context::use_botan_crypto_backend(const std::shared_ptr<Botan::RandomNumberGenerator>& rng) {
100#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
102 m_impl->m_crypto_callback_state = Botan::TPM2::use_botan_crypto_backend(esys_context(), rng);
103#else
104 BOTAN_UNUSED(rng);
105 throw Not_Implemented("This build of botan does not provide the TPM2 crypto backend");
106#endif
107}
108
110#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
111 return m_impl->m_crypto_callback_state != nullptr;
112#else
113 return false;
114#endif
115}
116
117#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
118CryptoCallbackState& Context::crypto_callback_state() {
119 BOTAN_ASSERT_NONNULL(m_impl->m_crypto_callback_state);
120 return *m_impl->m_crypto_callback_state;
121}
122#endif
123
124ESYS_CONTEXT* Context::esys_context() noexcept {
125 return m_impl->m_ctx;
126}
127
128namespace {
129
130uint32_t get_tpm_property(ESYS_CONTEXT* ctx, TPM2_PT property) {
131 // We expect to retrieve a single piece of information, not a list.
132 constexpr uint32_t property_count = 1;
133 constexpr TPM2_CAP capability = TPM2_CAP_TPM_PROPERTIES;
134
136 check_rc("Esys_GetCapability",
137 Esys_GetCapability(ctx,
138 ESYS_TR_NONE,
139 ESYS_TR_NONE,
140 ESYS_TR_NONE,
141 capability,
142 property,
143 property_count,
144 nullptr /* more data? - we don't care here */,
145 out_ptr(capability_data)));
146 BOTAN_ASSERT_NONNULL(capability_data);
147 BOTAN_ASSERT_NOMSG(capability_data->capability == capability);
148 BOTAN_ASSERT_NOMSG(capability_data->data.tpmProperties.count == property_count);
149 BOTAN_ASSERT_NOMSG(capability_data->data.tpmProperties.tpmProperty[0].property == property);
150
151 return capability_data->data.tpmProperties.tpmProperty[0].value;
152}
153
154template <TPM2_CAP capability, typename ReturnT>
155[[nodiscard]] std::vector<ReturnT> get_tpm_property_list(ESYS_CONTEXT* ctx, TPM2_PT property, uint32_t count) {
156 const auto extract = [](const TPMU_CAPABILITIES& caps, uint32_t max_count) {
157 std::vector<ReturnT> result;
158 if constexpr(capability == TPM2_CAP_HANDLES) {
159 const auto to_read = std::min(caps.handles.count, max_count);
160 result.reserve(to_read);
161 for(size_t i = 0; i < to_read; ++i) {
162 result.push_back(caps.handles.handle[i]);
163 }
164 } else if constexpr(capability == TPM2_CAP_ALGS) {
165 const auto to_read = std::min(caps.algorithms.count, max_count);
166 result.reserve(to_read);
167 for(size_t i = 0; i < to_read; ++i) {
168 // TODO: This also contains an algProperties.algProperties bitfield
169 // that defines some characteristics of the algorithm.
170 // Currently, we don't need that information and ignore it.
171 result.push_back(caps.algorithms.algProperties[i].alg);
172 }
173 } else {
174 // TODO: support reading other capability types as needed
175 static_assert(capability != TPM2_CAP_HANDLES, "Unsupported capability");
176 }
177 return result;
178 };
179
180 TPMI_YES_NO more_data = TPM2_YES;
181 std::vector<ReturnT> properties;
182 while(more_data == TPM2_YES && count > 0) {
184 check_rc("Esys_GetCapability",
185 Esys_GetCapability(ctx,
186 ESYS_TR_NONE,
187 ESYS_TR_NONE,
188 ESYS_TR_NONE,
189 capability,
190 property,
191 count,
192 &more_data,
193 out_ptr(capability_data)));
194 BOTAN_ASSERT_NONNULL(capability_data);
195 BOTAN_ASSERT_NOMSG(capability_data->capability == capability);
196
197 const auto new_properties = extract(capability_data->data, count);
198 BOTAN_ASSERT_NOMSG(new_properties.size() <= count);
199 properties.insert(properties.end(), new_properties.begin(), new_properties.end());
200 count -= checked_cast_to<uint32_t>(new_properties.size());
201 }
202
203 return properties;
204}
205
206} // namespace
207
208std::string Context::vendor() const {
209 constexpr std::array properties = {
210 TPM2_PT_VENDOR_STRING_1, TPM2_PT_VENDOR_STRING_2, TPM2_PT_VENDOR_STRING_3, TPM2_PT_VENDOR_STRING_4};
211 std::array<uint8_t, properties.size() * 4 + 1 /* ensure zero-termination */> vendor_string{};
212
213 BufferStuffer bs(vendor_string);
214
215 // The vendor name is transported in several uint32_t fields that are
216 // loaded as big-endian bytes and concatenated to form the vendor string.
217 for(const auto prop : properties) {
218 bs.append(store_be(get_tpm_property(m_impl->m_ctx, prop)));
219 }
220
221 BOTAN_ASSERT_NOMSG(bs.remaining_capacity() == 1); // the ensured zero-termination
222 return std::string(cast_uint8_ptr_to_char(vendor_string.data()));
223}
224
225std::string Context::manufacturer() const {
226 std::array<uint8_t, 4 + 1 /* ensure zero termination */> manufacturer_data{};
227 store_be(std::span{manufacturer_data}.first<4>(), get_tpm_property(m_impl->m_ctx, TPM2_PT_MANUFACTURER));
228 return std::string(cast_uint8_ptr_to_char(manufacturer_data.data()));
229}
230
231bool Context::supports_algorithm(std::string_view algo_name) const {
232 // Go through all the string mappings we have available and check if we
233 // can find the algorithm name in any of them. If we do, we can check if
234 // the TPM supports the required algorithms.
235 const auto required_alg_ids = [&]() -> std::vector<TPM2_ALG_ID> {
236 std::vector<TPM2_ALG_ID> result;
237 if(auto algo_id = asymmetric_algorithm_botan_to_tss2(algo_name)) {
238 result.push_back(algo_id.value());
239 }
240
241 if(auto hash_id = hash_algo_botan_to_tss2(algo_name)) {
242 result.push_back(hash_id.value());
243 }
244
245 if(auto block_id = block_cipher_botan_to_tss2(algo_name)) {
246 result.push_back(block_id->first);
247 }
248
249 if(auto cipher_mode_id = cipher_mode_botan_to_tss2(algo_name)) {
250 result.push_back(cipher_mode_id.value());
251 }
252
253 if(auto cipher_spec = cipher_botan_to_tss2(algo_name)) {
254 result.push_back(cipher_spec->algorithm);
255 result.push_back(cipher_spec->mode.sym);
256 }
257
258 if(auto sig_padding = rsa_signature_padding_botan_to_tss2(algo_name)) {
259 result.push_back(sig_padding.value());
260 }
261
262 if(auto sig = rsa_signature_scheme_botan_to_tss2(algo_name)) {
263 result.push_back(sig->scheme);
264 result.push_back(sig->details.any.hashAlg);
265 }
266
267 if(auto enc_scheme = rsa_encryption_scheme_botan_to_tss2(algo_name)) {
268 result.push_back(enc_scheme->scheme);
269 if(enc_scheme->scheme == TPM2_ALG_OAEP) {
270 result.push_back(enc_scheme->details.oaep.hashAlg);
271 }
272 }
273
274 if(auto enc_id = rsa_encryption_padding_botan_to_tss2(algo_name)) {
275 result.push_back(enc_id.value());
276 }
277
278 return result;
279 }();
280
281 if(required_alg_ids.empty()) {
282 // The algorithm name is not known to us, so we cannot check for support.
283 return false;
284 }
285
286 const auto algo_caps =
287 get_tpm_property_list<TPM2_CAP_ALGS, TPM2_ALG_ID>(m_impl->m_ctx, TPM2_ALG_FIRST, TPM2_MAX_CAP_ALGS);
288
289 return std::all_of(
290 required_alg_ids.begin(), required_alg_ids.end(), [&](TPM2_ALG_ID id) { return value_exists(algo_caps, id); });
291}
292
294 return get_tpm_property(m_impl->m_ctx, TPM2_PT_MAX_DIGEST);
295}
296
297std::unique_ptr<TPM2::PrivateKey> Context::storage_root_key(std::span<const uint8_t> auth_value,
298 const SessionBundle& sessions) {
299 return TPM2::PrivateKey::load_persistent(shared_from_this(), storage_root_key_handle, auth_value, sessions);
300}
301
302std::vector<ESYS_TR> Context::transient_handles() const {
303 return get_tpm_property_list<TPM2_CAP_HANDLES, ESYS_TR>(m_impl->m_ctx, TPM2_TRANSIENT_FIRST, TPM2_MAX_CAP_HANDLES);
304}
305
306std::optional<TPM2_HANDLE> Context::find_free_persistent_handle() const {
307 const auto occupied_handles = persistent_handles();
308
309 // This is modeled after the implementation in tpm2-tools, which also takes
310 // "platform persistent" handles into account. We don't do that here, but
311 // we might need to in the future.
312 //
313 // See: https://github.com/tpm2-software/tpm2-tools/blob/bd832d3f79/lib/tpm2_capability.c#L143-L196
314
315 // all persistent handles are occupied
316 if(occupied_handles.size() >= TPM2_MAX_CAP_HANDLES) {
317 return std::nullopt;
318 }
319
320 // find the lowest handle that is not occupied
321 for(TPM2_HANDLE i = TPM2_PERSISTENT_FIRST; i < TPM2_PERSISTENT_LAST; ++i) {
322 if(!value_exists(occupied_handles, i)) {
323 return i;
324 }
325 }
326
328}
329
330std::vector<TPM2_HANDLE> Context::persistent_handles() const {
331 return get_tpm_property_list<TPM2_CAP_HANDLES, TPM2_HANDLE>(
332 m_impl->m_ctx, TPM2_PERSISTENT_FIRST, TPM2_MAX_CAP_HANDLES);
333}
334
336 const SessionBundle& sessions,
337 std::span<const uint8_t> auth_value,
338 std::optional<TPM2_HANDLE> persistent_handle) {
339 auto& handles = key.handles();
340
341 BOTAN_ARG_CHECK(!persistent_handle || !value_exists(persistent_handles(), persistent_handle.value()),
342 "Persistent handle already in use");
343 BOTAN_ARG_CHECK(!handles.has_persistent_handle(), "Key already has a persistent handle assigned");
344
345 // 1. Decide on the location to persist the key to.
346 // This uses either the handle provided by the caller or a free handle.
347 const std::optional<TPMI_DH_PERSISTENT> new_persistent_handle =
348 persistent_handle.has_value() ? persistent_handle : find_free_persistent_handle();
349
350 BOTAN_STATE_CHECK(new_persistent_handle.has_value());
351
352 // 2. Persist the transient key in the TPM's NV storage
353 // This will flush the transient key handle and replace it with a new
354 // transient handle that references the persisted key.
355 check_rc("Esys_EvictControl",
356 Esys_EvictControl(m_impl->m_ctx,
357 ESYS_TR_RH_OWNER /*TODO: hierarchy*/,
358 handles.transient_handle(),
359 sessions[0],
360 sessions[1],
361 sessions[2],
362 *new_persistent_handle,
363 out_transient_handle(handles)));
364 BOTAN_ASSERT_NOMSG(handles.has_transient_handle());
365
366 // 3. Reset the auth value of the key object
367 // This is necessary to ensure that the key object remains usable after
368 // the transient handle was recreated inside Esys_EvictControl().
369 if(!auth_value.empty()) {
370 const auto user_auth = copy_into<TPM2B_AUTH>(auth_value);
371 check_rc("Esys_TR_SetAuth", Esys_TR_SetAuth(m_impl->m_ctx, handles.transient_handle(), &user_auth));
372 }
373
374 // 4. Update the key object with the new persistent handle
375 // This double-checks that the key was persisted at the correct location,
376 // but also brings the key object into a consistent state.
377 check_rc("Esys_TR_GetTpmHandle",
378 Esys_TR_GetTpmHandle(m_impl->m_ctx, handles.transient_handle(), out_persistent_handle(handles)));
379
380 BOTAN_ASSERT_NOMSG(handles.has_persistent_handle());
381 BOTAN_ASSERT_EQUAL(*new_persistent_handle, handles.persistent_handle(), "key was persisted at the correct location");
382
383 return *new_persistent_handle;
384}
385
386void Context::evict(std::unique_ptr<TPM2::PrivateKey> key, const SessionBundle& sessions) {
388
389 auto& handles = key->handles();
390 BOTAN_ARG_CHECK(handles.has_persistent_handle(), "Key does not have a persistent handle assigned");
391
392 // 1. Evict the key from the TPM's NV storage
393 // This will free the persistent handle, but the transient handle will
394 // still be valid.
395 ESYS_TR no_new_handle = ESYS_TR_NONE;
396 check_rc("Esys_EvictControl",
397 Esys_EvictControl(m_impl->m_ctx,
398 ESYS_TR_RH_OWNER /*TODO: hierarchy*/,
399 handles.transient_handle(),
400 sessions[0],
401 sessions[1],
402 sessions[2],
403 0,
404 &no_new_handle));
405 BOTAN_ASSERT(no_new_handle == ESYS_TR_NONE, "When deleting a key, no new handle is returned");
406
407 // 2. The persistent key was deleted and the transient key was flushed by
408 // Esys_EvictControl().
409 handles._disengage();
410}
411
413 if(!m_impl) {
414 return;
415 }
416
417#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
418 // If this object manages a crypto backend state object and the ESYS context
419 // will live on, because it was externally provided, we have to de-register
420 // this state object from the crypto callbacks.
421 //
422 // This will prevent the crypto backend callbacks from using a dangling
423 // pointer and cause graceful errors if the externally provided ESYS context
424 // is used for any action that would still need the crypto backend state.
425 //
426 // We deliberately do not just disable the crypto backend silently, as that
427 // might give users the false impression that they continue to benefit from
428 // the crypto backend while in fact they're back to the TSS' default.
429 if(m_impl->m_external && uses_botan_crypto_backend()) {
430 try {
431 set_crypto_callbacks(esys_context(), nullptr /* reset callback state */);
432 } catch(...) {
433 // ignore errors in destructor
434 }
435 m_impl->m_crypto_callback_state.reset();
436 }
437#endif
438
439 // We don't finalize contexts that were provided externally. Those are
440 // expected to be handled by the library users' applications.
441 if(!m_impl->m_external) {
442 // If the TCTI context was initialized explicitly, Esys_GetTcti() will
443 // return a pointer to the TCTI context that then has to be finalized
444 // explicitly. See ESAPI Specification Section 6.3 "Esys_GetTcti".
445 TSS2_TCTI_CONTEXT* tcti_ctx = nullptr; // NOLINT(*-const-correctness) bug in clang-tidy
446 Esys_GetTcti(m_impl->m_ctx, &tcti_ctx); // ignore error in destructor
447 if(tcti_ctx != nullptr) {
448 Tss2_TctiLdr_Finalize(&tcti_ctx);
449 }
450
451 Esys_Finalize(&m_impl->m_ctx);
452 }
453}
454
455} // namespace Botan::TPM2
#define BOTAN_UNUSED
Definition assert.h:151
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition assert.h:95
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:121
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:69
#define BOTAN_ASSERT_UNREACHABLE()
Definition assert.h:173
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)
Definition tpm2_key.cpp:184
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
bool supports_botan_crypto_backend() 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)
Definition tpm2_util.h:53
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)
Definition tpm2_util.h:223
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< CryptoCallbackState > use_botan_crypto_backend(ESYS_CONTEXT *context, const std::shared_ptr< Botan::RandomNumberGenerator > &rng)
std::unique_ptr< T, esys_liberator > unique_esys_ptr
A unique pointer type for ESYS handles that automatically frees the handle.
Definition tpm2_util.h:161
std::optional< TPMI_ALG_HASH > hash_algo_botan_to_tss2(std::string_view hash_name) noexcept
constexpr auto out_transient_handle(Object &object)
Definition tpm2_util.h:217
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)
Definition tpm2_util.h:116
std::optional< TPMI_ALG_SYM_MODE > cipher_mode_botan_to_tss2(std::string_view mode_name) noexcept
void set_crypto_callbacks(ESYS_CONTEXT *ctx, void *callback_state)
constexpr auto out_ptr(T &outptr) noexcept
Definition stl_util.h:164
constexpr RT checked_cast_to(AT i)
Definition int_utils.h:104
bool value_exists(const std::vector< T > &vec, const V &val)
Definition stl_util.h:44
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition mem_ops.h:323
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745
uint32_t ESYS_TR
Forward declaration of TSS2 type for convenience.
uint32_t TPM2_HANDLE
Forward declaration of TSS2 type for convenience.