Botan 3.6.1
Crypto and TLS for C&
tpm2_context.h
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#ifndef BOTAN_TPM2_CONTEXT_H_
10#define BOTAN_TPM2_CONTEXT_H_
11
12#include <botan/exceptn.h>
13#include <botan/rng.h>
14
15#include <memory>
16#include <optional>
17#include <vector>
18
19/// Forward declaration of TSS2 type for convenience
20using TPM2_HANDLE = uint32_t;
21
22/// Forward declaration of TSS2 type for convenience
23using ESYS_TR = uint32_t;
24
25struct ESYS_CONTEXT;
26
27namespace Botan::TPM2 {
28
29struct CryptoCallbackState;
30
31class PrivateKey;
32class SessionBundle;
33
34/**
35 * Central class for interacting with a TPM2. Additional to managing the
36 * connection to the TPM, this provides authorative information about the TPM's
37 * capabilities. Also, it allows to persist and evict keys generated by the TPM.
38 */
39class BOTAN_PUBLIC_API(3, 6) Context final : public std::enable_shared_from_this<Context> {
40 public:
41 /**
42 * @param tcti_nameconf this is passed to Tss2_TctiLdr_Initialize verbatim
43 */
44 static std::shared_ptr<Context> create(const std::string& tcti_nameconf);
45
46 /**
47 * @param tcti if set this is passed to Tss2_TctiLdr_Initialize_Ex verbatim
48 * otherwise a nullptr is passed.
49 * @param conf if set this is passed to Tss2_TctiLdr_Initialize_Ex verbatim
50 * otherwise a nullptr is passed.
51 */
52 static std::shared_ptr<Context> create(std::optional<std::string> tcti = {},
53 std::optional<std::string> conf = {});
54
55 Context(const Context&) = delete;
56 Context(Context&& ctx) noexcept = default;
57 ~Context();
58
59 Context& operator=(const Context&) = delete;
60 Context& operator=(Context&& ctx) noexcept = default;
61
62 /**
63 * Overrides the TSS2's crypto callbacks with Botan's functionality.
64 *
65 * This replaces all cryptographic functionality required for the
66 * communication with the TPM by botan's implementations. The TSS2
67 * would otherwise use OpenSSL or mbedTLS.
68 *
69 * Note that the provided @p rng should not be dependent on the TPM.
70 *
71 * @param rng the RNG to use for the crypto operations
72 * @throws Not_Implemented if the TPM2-TSS does not support crypto callbacks
73 * @sa supports_botan_crypto_backend()
74 */
75 void use_botan_crypto_backend(const std::shared_ptr<Botan::RandomNumberGenerator>& rng);
76
77 /**
78 * Checks if the TSS2 supports registering Botan's crypto backend at runtime.
79 * Older versions of the TSS2 do not support this feature ( 4.0.0), also
80 * Botan may be compiled without support for TSS' crypto backend.
81 * @return true if the TSS2 supports Botan's crypto backend
82 */
83 static bool supports_botan_crypto_backend() noexcept;
84
85 /// @returns true if botan is used for the TSS' crypto functions
86 bool uses_botan_crypto_backend() const noexcept;
87
88 /// @return an ESYS_CONTEXT* for use in other TPM2 functions.
89 ESYS_CONTEXT* esys_context() noexcept;
90
91 operator ESYS_CONTEXT*() noexcept { return esys_context(); }
92
93 /// @return the Vendor of the TPM2
94 std::string vendor() const;
95
96 /// @returns the Manufacturer of the TPM2
97 std::string manufacturer() const;
98
99 /**
100 * The @p algo_name can be any of the string algorithm specifiers used
101 * elsewhere. For example, "RSA", "AES-128", "SHA-1", "CTR(3DES)", etc.
102 *
103 * @returns true if the specified algorithm is supported by the TPM
104 */
105 bool supports_algorithm(std::string_view algo_name) const;
106
107 /// @returns the maximum number of random bytes to be requested at once
108 size_t max_random_bytes_per_request() const;
109
110 std::vector<ESYS_TR> transient_handles() const;
111
112 /// @returns a persistent handle that is currently not in use
113 /// or std::nullopt if no such handle is available
114 std::optional<TPM2_HANDLE> find_free_persistent_handle() const;
115
116 std::vector<TPM2_HANDLE> persistent_handles() const;
117
118 /// Makes @p key persistent at location @p persistent_handle or any free
119 TPM2_HANDLE persist(TPM2::PrivateKey& key,
120 const SessionBundle& sessions,
121 std::span<const uint8_t> auth_value = {},
122 std::optional<TPM2_HANDLE> persistent_handle = std::nullopt);
123
124 /// Evicts a persistent @p key from the TPM. The key cannot be used after.
125 void evict(std::unique_ptr<TPM2::PrivateKey> key, const SessionBundle& sessions);
126
127 // TODO: Currently this assumes that the SRK is a persistent object,
128 // this assumption may not hold forever.
129 std::unique_ptr<TPM2::PrivateKey> storage_root_key(std::span<const uint8_t> auth_value,
130 const SessionBundle& sessions);
131
132 private:
133 Context(const char* tcti_nameconf);
134 Context(const char* tcti_name, const char* tcti_conf);
135
136#if defined(BOTAN_HAS_TPM2_CRYPTO_BACKEND)
137 friend void enable_crypto_callbacks(const std::shared_ptr<Context>&);
138 CryptoCallbackState& crypto_callback_state();
139#endif
140
141 private:
142 struct Impl; // PImpl to avoid TPM2-TSS includes in this header
143 std::unique_ptr<Impl> m_impl;
144};
145
146} // namespace Botan::TPM2
147
148#endif
Context & operator=(const Context &)=delete
Context(const Context &)=delete
Context & operator=(Context &&ctx) noexcept=default
Context(Context &&ctx) noexcept=default
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
uint32_t ESYS_TR
Forward declaration of TSS2 type for convenience.
uint32_t TPM2_HANDLE
Forward declaration of TSS2 type for convenience.