Skip to content

Commit 63a4253

Browse files
committed
Enforce explicit conversion in Session classes - Solve clang-tidy suppression
1 parent ea34eb7 commit 63a4253

File tree

10 files changed

+168
-128
lines changed

10 files changed

+168
-128
lines changed

src/examples/pkcs10_csr_on_tpm2.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ int main() {
6464

6565
// Create a private key and persist it into the TPM
6666
auto cert_private_key = Botan::TPM2::RSA_PrivateKey::create_unrestricted_transient(
67-
ctx, session, as_byteview(private_key_auth), *storage_root_key, key_length);
68-
const auto persistent_handle = ctx->persist(*cert_private_key, session, as_byteview(private_key_auth));
67+
ctx, Botan::TPM2::SessionBundle(session), as_byteview(private_key_auth), *storage_root_key, key_length);
68+
const auto persistent_handle =
69+
ctx->persist(*cert_private_key, Botan::TPM2::SessionBundle(session), as_byteview(private_key_auth));
6970
std::cout << "New private key created\n";
7071
std::cout << " Persistent handle: 0x" << std::hex << persistent_handle << '\n';
7172

src/lib/prov/tpm2/tpm2_context.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -355,9 +355,9 @@ TPM2_HANDLE Context::persist(TPM2::PrivateKey& key,
355355
Esys_EvictControl(m_impl->m_ctx,
356356
ESYS_TR_RH_OWNER /*TODO: hierarchy*/,
357357
handles.transient_handle(),
358-
sessions[0],
359-
sessions[1],
360-
sessions[2],
358+
static_cast<ESYS_TR>(sessions[0]),
359+
static_cast<ESYS_TR>(sessions[1]),
360+
static_cast<ESYS_TR>(sessions[2]),
361361
*new_persistent_handle,
362362
out_transient_handle(handles)));
363363
BOTAN_ASSERT_NOMSG(handles.has_transient_handle());
@@ -396,9 +396,9 @@ void Context::evict(std::unique_ptr<TPM2::PrivateKey> key, const SessionBundle&
396396
Esys_EvictControl(m_impl->m_ctx,
397397
ESYS_TR_RH_OWNER /*TODO: hierarchy*/,
398398
handles.transient_handle(),
399-
sessions[0],
400-
sessions[1],
401-
sessions[2],
399+
static_cast<ESYS_TR>(sessions[0]),
400+
static_cast<ESYS_TR>(sessions[1]),
401+
static_cast<ESYS_TR>(sessions[2]),
402402
0,
403403
&no_new_handle));
404404
BOTAN_ASSERT(no_new_handle == ESYS_TR_NONE, "When deleting a key, no new handle is returned");

src/lib/prov/tpm2/tpm2_hash.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ void HashFunction::lazy_setup() {
7272
const auto auth = init_empty<TPM2B_AUTH>();
7373
const auto rc = check_rc_expecting<TPM2_RC_HASH>("Esys_HashSequenceStart",
7474
Esys_HashSequenceStart(*m_handle.context(),
75-
m_sessions[0],
76-
m_sessions[1],
77-
m_sessions[2],
75+
static_cast<ESYS_TR>(m_sessions[0]),
76+
static_cast<ESYS_TR>(m_sessions[1]),
77+
static_cast<ESYS_TR>(m_sessions[2]),
7878
&auth,
7979
m_hash_type,
8080
out_transient_handle(m_handle)));
@@ -91,10 +91,13 @@ void HashFunction::add_data(std::span<const uint8_t> input) {
9191
while(slicer.remaining() > 0) {
9292
const size_t chunk = std::min(slicer.remaining(), size_t(TPM2_MAX_DIGEST_BUFFER));
9393
const auto data = copy_into<TPM2B_MAX_BUFFER>(slicer.take(chunk));
94-
check_rc(
95-
"Esys_SequenceUpdate",
96-
Esys_SequenceUpdate(
97-
*m_handle.context(), m_handle.transient_handle(), m_sessions[0], m_sessions[1], m_sessions[2], &data));
94+
check_rc("Esys_SequenceUpdate",
95+
Esys_SequenceUpdate(*m_handle.context(),
96+
m_handle.transient_handle(),
97+
static_cast<ESYS_TR>(m_sessions[0]),
98+
static_cast<ESYS_TR>(m_sessions[1]),
99+
static_cast<ESYS_TR>(m_sessions[2]),
100+
&data));
98101
}
99102
BOTAN_ASSERT_NOMSG(slicer.empty());
100103
}
@@ -108,9 +111,9 @@ std::pair<unique_esys_ptr<TPM2B_DIGEST>, unique_esys_ptr<TPMT_TK_HASHCHECK>> Has
108111
check_rc("Esys_SequenceComplete",
109112
Esys_SequenceComplete(*m_handle.context(),
110113
m_handle.transient_handle(),
111-
m_sessions[0],
112-
m_sessions[1],
113-
m_sessions[2],
114+
static_cast<ESYS_TR>(m_sessions[0]),
115+
static_cast<ESYS_TR>(m_sessions[1]),
116+
static_cast<ESYS_TR>(m_sessions[2]),
114117
&nodata,
115118
m_hierarchy,
116119
out_ptr(result.first),

src/lib/prov/tpm2/tpm2_key.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,12 @@ Object load_persistent_object(const std::shared_ptr<Context>& ctx,
7676
Object object(ctx);
7777

7878
check_rc("Esys_TR_FromTPMPublic",
79-
Esys_TR_FromTPMPublic(
80-
*ctx, persistent_object_handle, sessions[0], sessions[1], sessions[2], out_transient_handle(object)));
79+
Esys_TR_FromTPMPublic(*ctx,
80+
persistent_object_handle,
81+
static_cast<ESYS_TR>(sessions[0]),
82+
static_cast<ESYS_TR>(sessions[1]),
83+
static_cast<ESYS_TR>(sessions[2]),
84+
out_transient_handle(object)));
8185

8286
if(!auth_value.empty()) {
8387
const auto user_auth = copy_into<TPM2B_AUTH>(auth_value);
@@ -141,9 +145,9 @@ std::unique_ptr<PublicKey> PublicKey::load_transient(const std::shared_ptr<Conte
141145
Object handle(ctx);
142146
check_rc("Esys_LoadExternal",
143147
Esys_LoadExternal(*ctx,
144-
sessions[0],
145-
sessions[1],
146-
sessions[2],
148+
static_cast<ESYS_TR>(sessions[0]),
149+
static_cast<ESYS_TR>(sessions[1]),
150+
static_cast<ESYS_TR>(sessions[2]),
147151
nullptr /* no private data to be loaded */,
148152
&public_data,
149153
TPM2_RH_NULL,
@@ -199,9 +203,9 @@ std::unique_ptr<PrivateKey> PrivateKey::load_transient(const std::shared_ptr<Con
199203
check_rc("Esys_Load",
200204
Esys_Load(*ctx,
201205
parent.handles().transient_handle(),
202-
sessions[0],
203-
sessions[1],
204-
sessions[2],
206+
static_cast<ESYS_TR>(sessions[0]),
207+
static_cast<ESYS_TR>(sessions[1]),
208+
static_cast<ESYS_TR>(sessions[2]),
205209
&private_data,
206210
&public_data,
207211
out_transient_handle(handle)));
@@ -256,9 +260,9 @@ std::unique_ptr<PrivateKey> PrivateKey::create_transient_from_template(const std
256260
check_rc("Esys_CreateLoaded",
257261
Esys_CreateLoaded(*ctx,
258262
parent,
259-
sessions[0],
260-
sessions[1],
261-
sessions[2],
263+
static_cast<ESYS_TR>(sessions[0]),
264+
static_cast<ESYS_TR>(sessions[1]),
265+
static_cast<ESYS_TR>(sessions[2]),
262266
&sensitive_data,
263267
&marshalled_template,
264268
out_transient_handle(handle),

src/lib/prov/tpm2/tpm2_object.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ PublicInfo& Object::_public_info(const SessionBundle& sessions, std::optional<TP
138138
check_rc("Esys_ReadPublic",
139139
Esys_ReadPublic(*m_ctx,
140140
m_handles->transient,
141-
sessions[0],
142-
sessions[1],
143-
sessions[2],
141+
static_cast<ESYS_TR>(sessions[0]),
142+
static_cast<ESYS_TR>(sessions[1]),
143+
static_cast<ESYS_TR>(sessions[2]),
144144
out_ptr(m_public_info->pub),
145145
out_ptr(m_public_info->name),
146146
out_ptr(m_public_info->qualified_name)));

src/lib/prov/tpm2/tpm2_pkops.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ std::vector<uint8_t> Signature_Operation::sign(Botan::RandomNumberGenerator& rng
5656
check_rc("Esys_Sign",
5757
Esys_Sign(*key_handle().context(),
5858
key_handle().transient_handle(),
59-
sessions()[0],
60-
sessions()[1],
61-
sessions()[2],
59+
static_cast<ESYS_TR>(sessions()[0]),
60+
static_cast<ESYS_TR>(sessions()[1]),
61+
static_cast<ESYS_TR>(sessions()[2]),
6262
&digest,
6363
&scheme(),
6464
&validation,
@@ -110,9 +110,9 @@ bool Verification_Operation::is_valid_signature(std::span<const uint8_t> sig_dat
110110
const auto rc = check_rc_expecting<TPM2_RC_SIGNATURE>("Esys_VerifySignature",
111111
Esys_VerifySignature(*key_handle().context(),
112112
key_handle().transient_handle(),
113-
sessions()[0],
114-
sessions()[1],
115-
sessions()[2],
113+
static_cast<ESYS_TR>(sessions()[0]),
114+
static_cast<ESYS_TR>(sessions()[1]),
115+
static_cast<ESYS_TR>(sessions()[2]),
116116
&digest,
117117
&signature,
118118
nullptr /* validation */));

src/lib/prov/tpm2/tpm2_rng.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,12 @@ void RandomNumberGenerator::fill_bytes_with_input(std::span<uint8_t> output, std
2929
const size_t chunk = std::min(in.remaining(), MAX_STIR_RANDOM_SIZE);
3030
const auto data = copy_into<TPM2B_SENSITIVE_DATA>(in.take(chunk));
3131

32-
check_rc("Esys_StirRandom", Esys_StirRandom(*m_ctx, m_sessions[0], m_sessions[1], m_sessions[2], &data));
32+
check_rc("Esys_StirRandom",
33+
Esys_StirRandom(*m_ctx,
34+
static_cast<ESYS_TR>(m_sessions[0]),
35+
static_cast<ESYS_TR>(m_sessions[1]),
36+
static_cast<ESYS_TR>(m_sessions[2]),
37+
&data));
3338
}
3439
BOTAN_ASSERT_NOMSG(in.empty());
3540

@@ -39,9 +44,9 @@ void RandomNumberGenerator::fill_bytes_with_input(std::span<uint8_t> output, std
3944
const auto requested_bytes = std::min(out.remaining_capacity(), m_max_tpm2_rng_bytes);
4045
check_rc("Esys_GetRandom",
4146
Esys_GetRandom(*m_ctx,
42-
m_sessions[0],
43-
m_sessions[1],
44-
m_sessions[2],
47+
static_cast<ESYS_TR>(m_sessions[0]),
48+
static_cast<ESYS_TR>(m_sessions[1]),
49+
static_cast<ESYS_TR>(m_sessions[2]),
4550
static_cast<uint16_t>(requested_bytes),
4651
out_ptr(digest)));
4752

src/lib/prov/tpm2/tpm2_rsa/tpm2_rsa.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,9 @@ class RSA_Encryption_Operation final : public PK_Ops::Encryption {
269269
check_rc("Esys_RSA_Encrypt",
270270
Esys_RSA_Encrypt(*m_key_handle.context(),
271271
m_key_handle.transient_handle(),
272-
m_sessions[0],
273-
m_sessions[1],
274-
m_sessions[2],
272+
static_cast<ESYS_TR>(m_sessions[0]),
273+
static_cast<ESYS_TR>(m_sessions[1]),
274+
static_cast<ESYS_TR>(m_sessions[2]),
275275
&plaintext,
276276
&m_scheme,
277277
&label,
@@ -348,9 +348,9 @@ class RSA_Decryption_Operation final : public PK_Ops::Decryption {
348348
auto rc = check_rc_expecting<TPM2_RC_FAILURE>("Esys_RSA_Decrypt",
349349
Esys_RSA_Decrypt(*m_key_handle.context(),
350350
m_key_handle.transient_handle(),
351-
m_sessions[0],
352-
m_sessions[1],
353-
m_sessions[2],
351+
static_cast<ESYS_TR>(m_sessions[0]),
352+
static_cast<ESYS_TR>(m_sessions[1]),
353+
static_cast<ESYS_TR>(m_sessions[2]),
354354
&ciphertext,
355355
&m_scheme,
356356
&label,

src/lib/prov/tpm2/tpm2_session.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ class BOTAN_UNSTABLE_API SessionHandle final {
7070

7171
~SessionHandle();
7272

73-
// NOLINTNEXTLINE(*-explicit-conversions) FIXME
74-
[[nodiscard]] operator ESYS_TR() && noexcept;
73+
[[nodiscard]] explicit operator ESYS_TR() && noexcept;
7574

7675
private:
7776
friend class Botan::TPM2::Session;
@@ -165,10 +164,11 @@ class BOTAN_PUBLIC_API(3, 6) Session {
165164
*/
166165
class SessionBundle {
167166
public:
168-
// NOLINTNEXTLINE(*-explicit-conversions) FIXME
169-
SessionBundle(std::shared_ptr<Session> s1 = nullptr,
170-
std::shared_ptr<Session> s2 = nullptr,
171-
std::shared_ptr<Session> s3 = nullptr) :
167+
SessionBundle() = default;
168+
169+
explicit SessionBundle(std::shared_ptr<Session> s1,
170+
std::shared_ptr<Session> s2 = nullptr,
171+
std::shared_ptr<Session> s3 = nullptr) :
172172
m_sessions({std::move(s1), std::move(s2), std::move(s3)}) {}
173173

174174
[[nodiscard]] detail::SessionHandle operator[](size_t i) const noexcept {
@@ -180,7 +180,7 @@ class SessionBundle {
180180
}
181181

182182
private:
183-
std::array<std::shared_ptr<Session>, 3> m_sessions;
183+
std::array<std::shared_ptr<Session>, 3> m_sessions{};
184184
};
185185

186186
} // namespace Botan::TPM2

0 commit comments

Comments
 (0)