Botan 3.13.0
Crypto and TLS for C&
tpm2_key.cpp
Go to the documentation of this file.
1/*
2* TPM 2.0 Key Wrappers' Base Class
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_key.h>
10
11#if defined(BOTAN_HAS_TPM2_RSA_ADAPTER)
12 #include <botan/tpm2_rsa.h>
13#endif
14#if defined(BOTAN_HAS_TPM2_ECC_ADAPTER)
15 #include <botan/tpm2_ecc.h>
16#endif
17
18#include <botan/internal/concat_util.h>
19#include <botan/internal/fmt.h>
20#include <botan/internal/stl_util.h>
21#include <botan/internal/tpm2_algo_mappings.h>
22#include <botan/internal/tpm2_hash.h>
23#include <botan/internal/tpm2_util.h>
24
25#include <tss2/tss2_esys.h>
26#include <tss2/tss2_mu.h>
27
28namespace Botan::TPM2 {
29
30#if defined(BOTAN_HAS_RSA)
31Botan::RSA_PublicKey rsa_pubkey_from_tss2_public(const TPM2B_PUBLIC* public_area) {
32 // TODO: this RSA_PublicKey currently takes const-refs, so we cannot benefit
33 // from moving them in place. This should be fixed in the future.
34 return std::apply([](const BigInt& n, const BigInt& e) { return Botan::RSA_PublicKey(n, e); },
35 rsa_pubkey_components_from_tss2_public(public_area));
36}
37#endif
38
39#if defined(BOTAN_HAS_ECC_GROUP)
40std::pair<EC_Group, EC_AffinePoint> ecc_pubkey_from_tss2_public(const TPM2B_PUBLIC* public_blob) {
41 BOTAN_ASSERT_NONNULL(public_blob);
42 BOTAN_ARG_CHECK(public_blob->publicArea.type == TPM2_ALG_ECC, "Public blob is not an ECC key");
43
44 const auto curve_id = public_blob->publicArea.parameters.eccDetail.curveID;
45 const auto curve_name = curve_id_tss2_to_botan(curve_id);
46 if(!curve_name) {
47 throw Invalid_Argument(Botan::fmt("Unsupported ECC curve: {}", curve_id));
48 }
49
50 auto curve = Botan::EC_Group::from_name(curve_name.value());
51 // Create an EC_AffinePoint from the x and y coordinates as SEC1 uncompressed point.
52 // According to the TPM2.0 specification Part 1 C.8, each coordinate is already padded to the curve's size.
53
55 curve,
56 concat<std::vector<uint8_t>>(std::array<uint8_t, 1>{0x04},
57 as_span(public_blob->publicArea.unique.ecc.x),
58 as_span(public_blob->publicArea.unique.ecc.y)));
59
60 if(!point) {
61 throw Invalid_Argument("Invalid ECC Point");
62 }
63 return {std::move(curve), std::move(point.value())};
64}
65#endif
66
67namespace {
68
69Object load_persistent_object(const std::shared_ptr<Context>& ctx,
70 TPM2_HANDLE persistent_object_handle,
71 std::span<const uint8_t> auth_value,
72 const SessionBundle& sessions) {
74 TPM2_PERSISTENT_FIRST <= persistent_object_handle && persistent_object_handle <= TPM2_PERSISTENT_LAST,
75 "persistent_object_handle out of range");
77 const bool is_persistent = value_exists(ctx->persistent_handles(), persistent_object_handle);
78 BOTAN_STATE_CHECK(is_persistent);
79
80 Object object(ctx);
81
82 check_rc("Esys_TR_FromTPMPublic",
83 Esys_TR_FromTPMPublic(ctx->esys_context(),
84 persistent_object_handle,
85 sessions[0],
86 sessions[1],
87 sessions[2],
88 out_transient_handle(object)));
89
90 if(!auth_value.empty()) {
91 const auto user_auth = copy_into<TPM2B_AUTH>(auth_value);
92 check_rc("Esys_TR_SetAuth", Esys_TR_SetAuth(ctx->esys_context(), object.transient_handle(), &user_auth));
93 }
94
95 check_rc("Esys_TR_GetTpmHandle",
96 Esys_TR_GetTpmHandle(ctx->esys_context(), object.transient_handle(), out_persistent_handle(object)));
97
98 const auto key_type = object._public_info(sessions).pub->publicArea.type;
99 BOTAN_ARG_CHECK(key_type == TPM2_ALG_RSA || key_type == TPM2_ALG_ECC,
100 "persistent object is neither RSA nor ECC public key");
101
102 return object;
103}
104
105std::vector<uint8_t> marshal_public_blob(const TPM2B_PUBLIC* public_data) {
106 size_t bytes_required = 0;
107 std::vector<uint8_t> marshalled_blob(sizeof(TPM2B_PUBLIC));
108 check_rc("Tss2_MU_TPM2B_PUBLIC_Marshal",
109 Tss2_MU_TPM2B_PUBLIC_Marshal(public_data, marshalled_blob.data(), marshalled_blob.size(), &bytes_required));
110 marshalled_blob.resize(bytes_required);
111 marshalled_blob.shrink_to_fit();
112 return marshalled_blob;
113}
114
115TPM2B_PUBLIC unmarshal_public_blob(std::span<const uint8_t> marshalled_blob) {
116 TPM2B_PUBLIC public_data{};
117 size_t offset = 0;
118 check_rc("Tss2_MU_TPM2B_PUBLIC_Unmarshal",
119 Tss2_MU_TPM2B_PUBLIC_Unmarshal(marshalled_blob.data(), marshalled_blob.size(), &offset, &public_data));
120 BOTAN_ASSERT_NOMSG(offset == marshalled_blob.size());
121 return public_data;
122}
123
124TPM2B_TEMPLATE marshal_template(const TPMT_PUBLIC& key_template) {
125 TPM2B_TEMPLATE result = {};
126 size_t offset = 0;
127 check_rc("Tss2_MU_TPMT_PUBLIC_Marshal",
128 Tss2_MU_TPMT_PUBLIC_Marshal(&key_template, result.buffer, sizeof(TPMT_PUBLIC), &offset));
129 BOTAN_ASSERT_NOMSG(offset <= sizeof(result.buffer));
130 result.size = static_cast<uint16_t>(offset);
131 return result;
132}
133
134} // namespace
135
136std::unique_ptr<PublicKey> PublicKey::load_persistent(const std::shared_ptr<Context>& ctx,
137 TPM2_HANDLE persistent_object_handle,
138 const SessionBundle& sessions) {
139 return create(load_persistent_object(ctx, persistent_object_handle, {}, sessions), sessions);
140}
141
142std::unique_ptr<PublicKey> PublicKey::load_transient(const std::shared_ptr<Context>& ctx,
143 std::span<const uint8_t> public_blob,
144 const SessionBundle& sessions) {
145 const auto public_data = unmarshal_public_blob(public_blob);
146
148
149 Object handle(ctx);
150 check_rc("Esys_LoadExternal",
151 Esys_LoadExternal(ctx->esys_context(),
152 sessions[0],
153 sessions[1],
154 sessions[2],
155 nullptr /* no private data to be loaded */,
156 &public_data,
157 TPM2_RH_NULL,
158 out_transient_handle(handle)));
159 return create(std::move(handle), sessions);
160}
161
162std::vector<uint8_t> PublicKey::raw_public_key_bits() const {
163 return marshal_public_blob(m_handle._public_info(m_sessions).pub.get());
164}
165
166std::unique_ptr<PublicKey> PublicKey::create(Object handles, const SessionBundle& sessions) {
167 [[maybe_unused]] const auto* pubinfo = handles._public_info(sessions).pub.get();
168#if defined(BOTAN_HAS_TPM2_RSA_ADAPTER)
169 if(pubinfo->publicArea.type == TPM2_ALG_RSA) {
170 return std::unique_ptr<PublicKey>(new RSA_PublicKey(std::move(handles), sessions, pubinfo));
171 }
172#endif
173#if defined(BOTAN_HAS_TPM2_ECC_ADAPTER)
174 if(pubinfo->publicArea.type == TPM2_ALG_ECC) {
175 return std::unique_ptr<PublicKey>(new EC_PublicKey(std::move(handles), sessions, pubinfo));
176 }
177#endif
178
179 throw Not_Implemented(Botan::fmt("Loaded a {} public key of an unsupported type",
180 handles.has_persistent_handle() ? "persistent" : "transient"));
181}
182
183// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
184
185std::unique_ptr<PrivateKey> PrivateKey::load_persistent(const std::shared_ptr<Context>& ctx,
186 TPM2_HANDLE persistent_object_handle,
187 std::span<const uint8_t> auth_value,
188 const SessionBundle& sessions) {
189 return create(load_persistent_object(ctx, persistent_object_handle, auth_value, sessions),
190 sessions,
191 nullptr /* pull public info from handle */,
192 {} /* persistent keys don't have an encrypted private blob */);
193}
194
195std::unique_ptr<PrivateKey> PrivateKey::load_transient(const std::shared_ptr<Context>& ctx,
196 std::span<const uint8_t> auth_value,
197 const TPM2::PrivateKey& parent,
198 std::span<const uint8_t> public_blob,
199 std::span<const uint8_t> private_blob,
200 const SessionBundle& sessions) {
202 Object handle(ctx);
203
204 const auto public_data = unmarshal_public_blob(public_blob);
205 const auto private_data = copy_into<TPM2B_PRIVATE>(private_blob);
206
207 check_rc("Esys_Load",
208 Esys_Load(ctx->esys_context(),
209 parent.handles().transient_handle(),
210 sessions[0],
211 sessions[1],
212 sessions[2],
213 &private_data,
214 &public_data,
215 out_transient_handle(handle)));
216
217 if(!auth_value.empty()) {
218 const auto user_auth = copy_into<TPM2B_AUTH>(auth_value);
219 check_rc("Esys_TR_SetAuth", Esys_TR_SetAuth(ctx->esys_context(), handle.transient_handle(), &user_auth));
220 }
221
222 return create(std::move(handle), sessions, nullptr /* pull public info from handle */, private_blob);
223}
224
225std::unique_ptr<PrivateKey> PrivateKey::create_transient_from_template(const std::shared_ptr<Context>& ctx,
226 const SessionBundle& sessions,
227 ESYS_TR parent,
228 const TPMT_PUBLIC& key_template,
229 const TPM2B_SENSITIVE_CREATE& sensitive_data) {
231
232 // NOLINTBEGIN(*-branch-clone)
233
234 switch(key_template.type) {
235 case TPM2_ALG_RSA:
236#if not defined(BOTAN_HAS_TPM2_RSA_ADAPTER)
237 throw Not_Implemented("TPM2-based RSA keys are not supported in this build");
238#endif
239 break;
240 case TPM2_ALG_ECC:
241#if not defined(BOTAN_HAS_TPM2_ECC_ADAPTER)
242 throw Not_Implemented("TPM2-based ECC keys are not supported in this build");
243#endif
244 break;
245 default:
246 throw Invalid_Argument("Unsupported key type");
247 }
248
249 // NOLINTEND(*-branch-clone)
250
251 const auto marshalled_template = marshal_template(key_template);
252
253 Object handle(ctx);
254 unique_esys_ptr<TPM2B_PRIVATE> private_bytes;
256
257 // Esys_CreateLoaded can create different object types depending on the type
258 // of the parent passed in. Namely, this will create a Primary object if the
259 // parent is referencing a Primary Seed; an Ordinary Object if the parent is
260 // referencing a Storage Parent; and a Derived Object if the parent is
261 // referencing a Derivation Parent.
262 //
263 // See the Architecture Document, Section 27.1.
264 check_rc("Esys_CreateLoaded",
265 Esys_CreateLoaded(ctx->esys_context(),
266 parent,
267 sessions[0],
268 sessions[1],
269 sessions[2],
270 &sensitive_data,
271 &marshalled_template,
272 out_transient_handle(handle),
273 out_ptr(private_bytes),
274 out_ptr(public_info)));
275 BOTAN_ASSERT_NONNULL(private_bytes);
276 BOTAN_ASSERT_NOMSG(public_info->publicArea.type == key_template.type);
278
279 return create(std::move(handle), sessions, public_info.get(), as_span(*private_bytes));
280}
281
283 BOTAN_STATE_CHECK(!m_handle.has_persistent_handle());
284 BOTAN_ASSERT_NOMSG(!m_private_blob.empty());
285 return Botan::lock(m_private_blob);
286}
287
288std::vector<uint8_t> PrivateKey::raw_public_key_bits() const {
289 return marshal_public_blob(m_handle._public_info(m_sessions).pub.get());
290}
291
293 // Architectural Document, Section 4.54
294 // any object with the decrypt and restricted attributes SET and the sign
295 // attribute CLEAR
296 const auto attrs = m_handle.attributes(m_sessions);
297 return attrs.decrypt && attrs.restricted && !attrs.sign_encrypt;
298}
299
300std::unique_ptr<PrivateKey> PrivateKey::create(Object handles,
301 [[maybe_unused]] const SessionBundle& sessions,
302 [[maybe_unused]] const TPM2B_PUBLIC* public_info,
303 [[maybe_unused]] std::span<const uint8_t> private_blob) {
304 if(public_info == nullptr) {
305 public_info = handles._public_info(sessions).pub.get();
306 }
307
308#if defined(BOTAN_HAS_TPM2_RSA_ADAPTER)
309 if(public_info->publicArea.type == TPM2_ALG_RSA) {
310 return std::unique_ptr<RSA_PrivateKey>(
311 new RSA_PrivateKey(std::move(handles), sessions, public_info, private_blob));
312 }
313#endif
314
315#if defined(BOTAN_HAS_TPM2_ECC_ADAPTER)
316 if(public_info->publicArea.type == TPM2_ALG_ECC) {
317 return std::unique_ptr<EC_PrivateKey>(new EC_PrivateKey(std::move(handles), sessions, public_info, private_blob));
318 }
319#endif
320
321 throw Not_Implemented(Botan::fmt("Loaded a {} private key of an unsupported type",
322 handles.has_persistent_handle() ? "persistent" : "transient"));
323}
324
325} // namespace Botan::TPM2
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:114
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
static std::optional< EC_AffinePoint > deserialize_uncompressed(const EC_Group &group, std::span< const uint8_t > bytes)
static EC_Group from_name(std::string_view name)
Definition ec_group.cpp:478
bool has_transient_handle() const
ESYS_TR transient_handle() const noexcept
static std::unique_ptr< PrivateKey > create_transient_from_template(const std::shared_ptr< Context > &ctx, const SessionBundle &sessions, ESYS_TR parent, const TPMT_PUBLIC &key_template, const TPM2B_SENSITIVE_CREATE &sensitive_data)
Definition tpm2_key.cpp:225
std::vector< uint8_t > raw_public_key_bits() const override
Definition tpm2_key.cpp:288
static std::unique_ptr< PrivateKey > create(Object handles, const SessionBundle &sessions, const TPM2B_PUBLIC *public_info, std::span< const uint8_t > private_blob)
Definition tpm2_key.cpp:300
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:185
static std::unique_ptr< PrivateKey > load_transient(const std::shared_ptr< Context > &ctx, std::span< const uint8_t > auth_value, const TPM2::PrivateKey &parent, std::span< const uint8_t > public_blob, std::span< const uint8_t > private_blob, const SessionBundle &sessions)
Definition tpm2_key.cpp:195
const SessionBundle & sessions() const
Definition tpm2_key.h:224
secure_vector< uint8_t > raw_private_key_bits() const override
Definition tpm2_key.cpp:282
static std::unique_ptr< PublicKey > create(Object handles, const SessionBundle &sessions)
Definition tpm2_key.cpp:166
std::vector< uint8_t > raw_public_key_bits() const override
Definition tpm2_key.cpp:162
static std::unique_ptr< PublicKey > load_persistent(const std::shared_ptr< Context > &ctx, TPM2_HANDLE persistent_object_handle, const SessionBundle &sessions={})
Definition tpm2_key.cpp:136
const Object & handles() const
Definition tpm2_key.h:99
const SessionBundle & sessions() const
Definition tpm2_key.h:101
static std::unique_ptr< PublicKey > load_transient(const std::shared_ptr< Context > &ctx, std::span< const uint8_t > public_blob, const SessionBundle &sessions)
Definition tpm2_key.cpp:142
constexpr void check_rc(std::string_view location, TSS2_RC rc)
Definition tpm2_util.h:55
constexpr auto out_persistent_handle(Object &object)
Definition tpm2_util.h:225
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:163
std::optional< std::string > curve_id_tss2_to_botan(TPMI_ECC_CURVE mode_id)
constexpr auto out_transient_handle(Object &object)
Definition tpm2_util.h:219
constexpr void copy_into(T &dest, std::span< const uint8_t > data)
Definition tpm2_util.h:118
constexpr auto as_span(tpm2_buffer auto &data)
Construct a std::span as a view into a TPM2 buffer.
Definition tpm2_util.h:103
constexpr auto out_ptr(T &outptr) noexcept
Definition stl_util.h:148
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
secure_vector< T > lock(const std::vector< T > &in)
Definition secmem.h:145
constexpr auto concat(Rs &&... ranges)
Definition concat_util.h:90
bool value_exists(const std::vector< T > &vec, const V &val)
Definition stl_util.h:44
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
uint32_t ESYS_TR
Forward declaration of TSS2 type for convenience.
uint32_t TPM2_HANDLE
Forward declaration of TSS2 type for convenience.