Botan 3.6.1
Crypto and TLS for C&
tpm2_object.cpp
Go to the documentation of this file.
1/*
2* TPM 2.0 Base Object handling
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_object.h>
10
11#include <botan/tpm2_session.h>
12
13#include <botan/internal/stl_util.h>
14#include <botan/internal/tpm2_util.h>
15
16#include <tss2/tss2_esys.h>
17
18namespace Botan::TPM2 {
19
20namespace {
21
22using ObjectAttributesWrapper =
23 AttributeWrapper<TPMA_OBJECT,
24 ObjectAttributes,
25 PropMap{&ObjectAttributes::fixed_tpm, TPMA_OBJECT_FIXEDTPM},
26 PropMap{&ObjectAttributes::st_clear, TPMA_OBJECT_STCLEAR},
27 PropMap{&ObjectAttributes::fixed_parent, TPMA_OBJECT_FIXEDPARENT},
28 PropMap{&ObjectAttributes::sensitive_data_origin, TPMA_OBJECT_SENSITIVEDATAORIGIN},
29 PropMap{&ObjectAttributes::user_with_auth, TPMA_OBJECT_USERWITHAUTH},
30 PropMap{&ObjectAttributes::admin_with_policy, TPMA_OBJECT_ADMINWITHPOLICY},
31 PropMap{&ObjectAttributes::no_da, TPMA_OBJECT_NODA},
32 PropMap{&ObjectAttributes::encrypted_duplication, TPMA_OBJECT_ENCRYPTEDDUPLICATION},
33 PropMap{&ObjectAttributes::restricted, TPMA_OBJECT_RESTRICTED},
34 PropMap{&ObjectAttributes::decrypt, TPMA_OBJECT_DECRYPT},
35 PropMap{&ObjectAttributes::sign_encrypt, TPMA_OBJECT_SIGN_ENCRYPT},
36 PropMap{&ObjectAttributes::x509sign, TPMA_OBJECT_X509SIGN}>;
37
38} // namespace
39
41 return ObjectAttributesWrapper::read(attributes);
42}
43
45 return ObjectAttributesWrapper::render(attributes);
46}
47
48Object::Object(std::shared_ptr<Context> ctx) : m_ctx(std::move(ctx)), m_handles(std::make_unique<ObjectHandles>()) {
50}
51
52Object::Object(std::shared_ptr<Context> ctx, ESYS_TR handle) : Object(std::move(ctx)) {
53 m_handles->transient = handle;
54}
55
56Object::Object(Object&& other) noexcept :
57 m_ctx(std::move(other.m_ctx)),
58 m_handles(std::move(other.m_handles)),
59 m_public_info(std::move(other.m_public_info)) {
60 other.scrub();
61}
62
64 if(m_handles) {
65 flush();
66 }
67}
68
69Object& Object::operator=(Object&& other) noexcept {
70 if(this != &other) {
71 flush();
72 m_ctx = std::move(other.m_ctx);
73 m_handles = std::move(other.m_handles);
74 m_public_info = std::move(other.m_public_info);
75 other.scrub();
76 }
77 return *this;
78}
79
80/// Flush the object's TPM handles as necessary
81void Object::flush() const noexcept {
82 // Only purely transient objects have to be flushed
85 Esys_TR_Close(*m_ctx, &m_handles->transient);
86 } else {
87 Esys_FlushContext(*m_ctx, m_handles->transient);
88 }
89 }
90}
91
92/// Destroy the object's internal state, making the destructor a no-op.
93/// No more operations except the destructor must be performed on that object.
94void Object::scrub() {
95 m_ctx.reset();
96 m_handles.reset();
97 m_public_info.reset();
98}
99
100/// Flush the object's TPM handles and reset its internal state
101void Object::_reset() noexcept {
102 flush();
103 _disengage();
104}
105
106/// Reset the object's internal state without flushing its TPM handles
107void Object::_disengage() noexcept {
108 m_handles = std::make_unique<ObjectHandles>();
109 m_public_info.reset();
110}
111
113 return m_handles->persistent.has_value();
114}
115
117 return m_handles->transient != ESYS_TR_NONE;
118}
119
122 return *m_handles->persistent;
123}
124
126 return m_handles->transient;
127}
128
130 const auto attrs = _public_info(sessions).pub->publicArea.objectAttributes;
131 return ObjectAttributes::read(attrs);
132}
133
134PublicInfo& Object::_public_info(const SessionBundle& sessions, std::optional<TPMI_ALG_PUBLIC> expected_type) const {
135 if(!m_public_info) {
136 m_public_info = std::make_unique<PublicInfo>();
137
138 check_rc("Esys_ReadPublic",
139 Esys_ReadPublic(*m_ctx,
140 m_handles->transient,
141 sessions[0],
142 sessions[1],
143 sessions[2],
144 out_ptr(m_public_info->pub),
145 out_ptr(m_public_info->name),
146 out_ptr(m_public_info->qualified_name)));
147 BOTAN_ASSERT_NONNULL(m_public_info->pub);
148
149 if(expected_type) {
150 BOTAN_STATE_CHECK(m_public_info->pub->publicArea.type == *expected_type);
151 }
152 }
153
154 return *m_public_info;
155}
156
157ObjectHandles& Object::handles() {
158 return *m_handles;
159}
160
161} // namespace Botan::TPM2
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:41
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:86
bool has_transient_handle() const
void _reset() noexcept
Flush the object's TPM handles and reset its internal state.
Object(std::shared_ptr< Context > ctx)
bool has_persistent_handle() const
PublicInfo & _public_info(const SessionBundle &sessions, std::optional< TPMI_ALG_PUBLIC > expected_type={}) const
ObjectAttributes attributes(const SessionBundle &sessions) const
ESYS_TR transient_handle() const noexcept
void _disengage() noexcept
Reset the object's internal state without flushing its TPM handles.
Object & operator=(const Object &)=delete
TPM2_HANDLE persistent_handle() const
PropMap(MaskT, FieldPointerT) -> PropMap< MaskT, FieldPointerT >
Deduction guide to simplify the creation of PropMap instances.
constexpr void check_rc(std::string_view location, TSS2_RC rc)
Definition tpm2_util.h:54
constexpr auto out_ptr(T &outptr) noexcept
Definition stl_util.h:420
bool no_da
If set, the object is not subject to dictionary attack protection.
Definition tpm2_object.h:53
bool fixed_tpm
The hierarchy of the object may or may not change (i.e. when keys are duplicated)
Definition tpm2_object.h:35
bool fixed_parent
The parent of the object may or may not change.
Definition tpm2_object.h:41
static ObjectAttributes read(TPMA_OBJECT attributes)
bool admin_with_policy
ADMIN role actions may or may not require a policy session.
Definition tpm2_object.h:50
bool user_with_auth
USER role actions may or may not be performed without authorization (HMAC or password)
Definition tpm2_object.h:47
bool sensitive_data_origin
Indicates that the TPM generated all of the sensitive data other than the authValue.
Definition tpm2_object.h:44
static TPMA_OBJECT render(ObjectAttributes attributes)
bool st_clear
Saved contexts of this object may or may not be loaded after Startup(CLEAR)
Definition tpm2_object.h:38
bool decrypt
The private portion of the key might be used for data decryption.
Definition tpm2_object.h:64
unique_esys_ptr< TPM2B_PUBLIC > pub
Definition tpm2_util.h:157
uint32_t ESYS_TR
Forward declaration of TSS2 type for convenience.
uint32_t TPM2_HANDLE
Forward declaration of TSS2 type for convenience.
uint32_t TPMA_OBJECT
Forward declaration of TSS2 type for convenience.
Definition tpm2_object.h:15