Botan 3.14.1
Crypto and TLS for C&
ffi_tpm2.cpp
Go to the documentation of this file.
1/*
2* (C) 2024 Jack Lloyd
3* (C) 2024 René Meusel, Amos Treiber - Rohde & Schwarz Cybersecurity GmbH
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/ffi.h>
9
10#include <botan/assert.h>
11#include <botan/internal/ffi_rng.h>
12#include <botan/internal/ffi_util.h>
13
14#if defined(BOTAN_HAS_TPM2)
15 #include <botan/tpm2_context.h>
16 #include <botan/tpm2_rng.h>
17 #include <botan/tpm2_session.h>
18
19 #if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
20 #include <botan/tpm2_crypto_backend.h>
21 #endif
22#endif
23
24extern "C" {
25
26using namespace Botan_FFI;
27
28#if defined(BOTAN_HAS_TPM2)
29
30BOTAN_FFI_DECLARE_SHARED_STRUCT(botan_tpm2_ctx_struct, Botan::TPM2::Context, 0xD2B95E15);
31BOTAN_FFI_DECLARE_SHARED_STRUCT(botan_tpm2_session_struct, Botan::TPM2::Session, 0x9ACCAB52);
32
33 #if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
34BOTAN_FFI_DECLARE_UNIQUE_STRUCT(botan_tpm2_crypto_backend_state_struct, Botan::TPM2::CryptoCallbackState, 0x1AC84DE5);
35 #endif
36
37} // extern "C"
38
39namespace {
40
42 return Botan::TPM2::SessionBundle((s1 != nullptr) ? safe_get_sp(s1) : nullptr,
43 (s2 != nullptr) ? safe_get_sp(s2) : nullptr,
44 (s3 != nullptr) ? safe_get_sp(s3) : nullptr);
45}
46
47} // namespace
48
49extern "C" {
50
51#endif
52
54#if defined(BOTAN_HAS_TPM2)
56#else
57 return 0;
58#endif
59}
60
61int botan_tpm2_ctx_init(botan_tpm2_ctx_t* ctx_out, const char* tcti_nameconf) {
62#if defined(BOTAN_HAS_TPM2)
63 return ffi_guard_thunk(__func__, [=]() -> int {
64 if(ctx_out == nullptr) {
66 }
67
68 auto ctx = (tcti_nameconf == nullptr) ? Botan::TPM2::Context::create()
69 : Botan::TPM2::Context::create(std::string(tcti_nameconf));
70 return ffi_new_object(ctx_out, std::move(ctx));
71 });
72#else
73 BOTAN_UNUSED(ctx_out, tcti_nameconf);
75#endif
76}
77
78int botan_tpm2_ctx_init_ex(botan_tpm2_ctx_t* ctx_out, const char* tcti_name, const char* tcti_conf) {
79#if defined(BOTAN_HAS_TPM2)
80 return ffi_guard_thunk(__func__, [=]() -> int {
81 if(ctx_out == nullptr) {
83 }
84
85 auto tcti_name_str = [=]() -> std::optional<std::string> {
86 if(tcti_name == nullptr) {
87 return {};
88 } else {
89 return std::string(tcti_name);
90 }
91 }();
92
93 auto tcti_conf_str = [=]() -> std::optional<std::string> {
94 if(tcti_conf == nullptr) {
95 return {};
96 } else {
97 return std::string(tcti_conf);
98 }
99 }();
100
101 auto ctx = Botan::TPM2::Context::create(std::move(tcti_name_str), std::move(tcti_conf_str));
102 return ffi_new_object(ctx_out, std::move(ctx));
103 });
104#else
105 BOTAN_UNUSED(ctx_out, tcti_name, tcti_conf);
107#endif
108}
109
110int botan_tpm2_ctx_from_esys(botan_tpm2_ctx_t* ctx_out, ESYS_CONTEXT* esys_ctx) {
111#if defined(BOTAN_HAS_TPM2)
112 return ffi_guard_thunk(__func__, [=]() -> int {
113 if(any_null_pointers(ctx_out, esys_ctx)) {
115 }
116
117 auto ctx = Botan::TPM2::Context::create(esys_ctx);
118 return ffi_new_object(ctx_out, std::move(ctx));
119 });
120#else
121 BOTAN_UNUSED(ctx_out, esys_ctx);
123#endif
124}
125
127#if defined(BOTAN_HAS_TPM2)
128 return BOTAN_FFI_VISIT(ctx, [=](auto& tpm2_ctx) { tpm2_ctx.use_botan_crypto_backend(safe_get_sp(rng)); });
129#else
130 BOTAN_UNUSED(ctx, rng);
132#endif
133}
134
135/**
136 * Frees all resources of a TPM2 context
137 * @param ctx TPM2 context
138 * @return 0 on success
139 */
141#if defined(BOTAN_HAS_TPM2)
142 return BOTAN_FFI_CHECKED_DELETE(ctx);
143#else
144 BOTAN_UNUSED(ctx);
146#endif
147}
148
150 ESYS_CONTEXT* esys_ctx,
151 botan_rng_t rng) {
152#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
153 return ffi_guard_thunk(__func__, [=]() -> int {
154 if(any_null_pointers(cbs_out, esys_ctx)) {
156 }
157
159 });
160#else
161 BOTAN_UNUSED(cbs_out, esys_ctx, rng);
163#endif
164}
165
167#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
168 return BOTAN_FFI_CHECKED_DELETE(cbs);
169#else
170 BOTAN_UNUSED(cbs);
172#endif
173}
174
180#if defined(BOTAN_HAS_TPM2)
181 return BOTAN_FFI_VISIT(ctx, [=](const std::shared_ptr<Botan::TPM2::Context>& tpm2_ctx) -> int {
182 if(rng_out == nullptr) {
184 }
185
186 return ffi_new_object(rng_out,
187 std::make_unique<Botan::TPM2::RandomNumberGenerator>(tpm2_ctx, sessions(s1, s2, s3)));
188 });
189#else
190 BOTAN_UNUSED(rng_out, ctx, s1, s2, s3);
192#endif
193}
194
196#if defined(BOTAN_HAS_TPM2)
197 return BOTAN_FFI_VISIT(ctx, [=](const std::shared_ptr<Botan::TPM2::Context>& tpm2_ctx) -> int {
198 if(session_out == nullptr) {
200 }
201
203 });
204#else
205 BOTAN_UNUSED(session_out, ctx);
207#endif
208}
209
211#if defined(BOTAN_HAS_TPM2)
212 return BOTAN_FFI_CHECKED_DELETE(session);
213#else
214 BOTAN_UNUSED(session);
216#endif
217}
218}
#define BOTAN_UNUSED
Definition assert.h:151
static bool supports_botan_crypto_backend() noexcept
static std::shared_ptr< Context > create(const std::string &tcti_nameconf)
static std::shared_ptr< Session > unauthenticated_session(const std::shared_ptr< Context > &ctx, std::string_view sym_algo="CFB(AES-256)", std::string_view hash_algo="SHA-256")
struct botan_tpm2_session_struct * botan_tpm2_session_t
Definition ffi.h:5206
struct botan_tpm2_ctx_struct * botan_tpm2_ctx_t
Definition ffi.h:5201
struct botan_rng_struct * botan_rng_t
Definition ffi.h:343
@ BOTAN_FFI_ERROR_NOT_IMPLEMENTED
Definition ffi.h:138
@ BOTAN_FFI_ERROR_NULL_POINTER
Definition ffi.h:131
struct botan_tpm2_crypto_backend_state_struct * botan_tpm2_crypto_backend_state_t
Definition ffi.h:5211
int botan_tpm2_ctx_enable_crypto_backend(botan_tpm2_ctx_t ctx, botan_rng_t rng)
Definition ffi_tpm2.cpp:126
int botan_tpm2_ctx_init_ex(botan_tpm2_ctx_t *ctx_out, const char *tcti_name, const char *tcti_conf)
Definition ffi_tpm2.cpp:78
int botan_tpm2_ctx_init(botan_tpm2_ctx_t *ctx_out, const char *tcti_nameconf)
Definition ffi_tpm2.cpp:61
int botan_tpm2_rng_init(botan_rng_t *rng_out, botan_tpm2_ctx_t ctx, botan_tpm2_session_t s1, botan_tpm2_session_t s2, botan_tpm2_session_t s3)
Definition ffi_tpm2.cpp:175
int botan_tpm2_ctx_from_esys(botan_tpm2_ctx_t *ctx_out, ESYS_CONTEXT *esys_ctx)
Definition ffi_tpm2.cpp:110
int botan_tpm2_ctx_destroy(botan_tpm2_ctx_t ctx)
Definition ffi_tpm2.cpp:140
int botan_tpm2_unauthenticated_session_init(botan_tpm2_session_t *session_out, botan_tpm2_ctx_t ctx)
Definition ffi_tpm2.cpp:195
int botan_tpm2_crypto_backend_state_destroy(botan_tpm2_crypto_backend_state_t cbs)
Definition ffi_tpm2.cpp:166
int botan_tpm2_session_destroy(botan_tpm2_session_t session)
Definition ffi_tpm2.cpp:210
int botan_tpm2_enable_crypto_backend(botan_tpm2_crypto_backend_state_t *cbs_out, ESYS_CONTEXT *esys_ctx, botan_rng_t rng)
Definition ffi_tpm2.cpp:149
int botan_tpm2_supports_crypto_backend()
Definition ffi_tpm2.cpp:53
#define BOTAN_FFI_VISIT(obj, lambda)
Definition ffi_util.h:327
#define BOTAN_FFI_DECLARE_SHARED_STRUCT(NAME, TYPE, MAGIC)
Definition ffi_util.h:169
#define BOTAN_FFI_CHECKED_DELETE(o)
Definition ffi_util.h:357
#define BOTAN_FFI_DECLARE_UNIQUE_STRUCT(NAME, TYPE, MAGIC)
Definition ffi_util.h:164
std::unique_ptr< CryptoCallbackState > use_botan_crypto_backend(ESYS_CONTEXT *context, const std::shared_ptr< Botan::RandomNumberGenerator > &rng)
auto safe_get_sp(T *p) -> const typename T::smart_pointer_type &
Definition ffi_util.h:212
BOTAN_FFI_ERROR ffi_new_object(T *obj, Args &&... args)
Definition ffi_util.h:347
int ffi_guard_thunk(const char *func_name, T thunk)
Definition ffi_util.h:229
bool any_null_pointers(Ptrs... ptr)
Definition mem_utils.h:54