10#include <botan/der_enc.h>
11#include <botan/hash.h>
12#include <botan/pk_ops.h>
14#include <botan/internal/fmt.h>
15#include <botan/internal/hash_id.h>
16#include <botan/internal/workfactor.h>
19#include <trousers/trousers.h>
20#include <tss/platform.h>
29void tss_error(TSS_RESULT res,
const char* expr,
const char* file,
int line) {
30 std::string err =
fmt(
"TPM error {} in layer {} executing {} at {}:{}",
31 Trspi_Error_String(res),
32 Trspi_Error_Layer(res),
40TSS_FLAG bit_flag(
size_t bits) {
44 return TSS_KEY_SIZE_1024;
46 return TSS_KEY_SIZE_2048;
50 return TSS_KEY_SIZE_4096;
52 return TSS_KEY_SIZE_8192;
54 return TSS_KEY_SIZE_16384;
56 throw Invalid_Argument(
"Unsupported TPM key size " + std::to_string(bits));
61uint32_t to_uint32(
T v) {
62 BOTAN_ARG_CHECK(v > std::numeric_limits<uint32_t>::max(),
"Value too large for 32bit unsigned integer");
63 return static_cast<uint32_t
>(v);
66#define TSPI_CHECK_SUCCESS(expr) \
68 TSS_RESULT res = expr; \
69 if(res != TSS_SUCCESS) \
70 tss_error(res, #expr, __FILE__, __LINE__); \
73std::vector<uint8_t> get_obj_attr(TSS_HCONTEXT ctx, TSS_HOBJECT obj, TSS_FLAG flag, TSS_FLAG sub_flag) {
78 std::vector<uint8_t> r(data, data + data_len);
85void set_policy_secret(TSS_HPOLICY policy,
const char* secret) {
87 BYTE* as_b =
const_cast<BYTE*
>(
reinterpret_cast<const BYTE*
>(secret));
88 TSPI_CHECK_SUCCESS(::Tspi_Policy_SetSecret(policy, TSS_SECRET_MODE_PLAIN, to_uint32(std::strlen(secret)), as_b));
90 static const uint8_t nullpass[20] = {0};
93 ::Tspi_Policy_SetSecret(policy, TSS_SECRET_MODE_SHA1,
sizeof(nullpass),
const_cast<BYTE*
>(nullpass)));
97TSS_UUID to_tss_uuid(
const UUID& uuid) {
98 static_assert(
sizeof(TSS_UUID) == 16,
"Expected size of packed UUID");
105UUID from_tss_uuid(
const TSS_UUID& tss_uuid) {
106 static_assert(
sizeof(TSS_UUID) == 16,
"Expected size of packed UUID");
108 std::vector<uint8_t> mem(16);
110 UUID uuid(std::move(mem));
115 if(flag == TSS_PS_TYPE_USER)
117 else if(flag == TSS_PS_TYPE_SYSTEM)
120 throw TPM_Error(
"Invalid storage flag " + std::to_string(flag));
125 return "tpmkey:uuid=" + uuid.to_string() +
";storage=" + storage_str;
128std::string format_url(
const TSS_UUID& tss_uuid, TSS_FLAG store_type) {
129 UUID uuid = from_tss_uuid(tss_uuid);
131 return format_url(from_tss_uuid(tss_uuid), storage_type_from_tss_flag(store_type));
142 const TSS_UUID SRK_UUID = TSS_UUID_SRK;
144 TSPI_CHECK_SUCCESS(::Tspi_Context_LoadKeyByUUID(m_ctx, TSS_PS_TYPE_SYSTEM, SRK_UUID, &m_srk));
147 set_policy_secret(m_srk_policy, srk_password);
169 copy_mem(out,
reinterpret_cast<const uint8_t*
>(mem), out_len);
174 TSPI_CHECK_SUCCESS(::Tspi_TPM_StirRandom(m_tpm, to_uint32(in_len),
const_cast<BYTE*
>(in)));
181 TSS_FLAG key_flags = bit_flag(bits) | TSS_KEY_VOLATILE | TSS_KEY_TYPE_SIGNING;
187 ::Tspi_SetAttribUint32(key, TSS_TSPATTRIB_KEY_INFO, TSS_TSPATTRIB_KEYINFO_SIGSCHEME, TSS_SS_RSASSAPKCS1V15_DER));
191 set_policy_secret(policy, key_password);
200 m_ctx(ctx), m_uuid(uuid_str), m_storage(storage_type) {
201 const TSS_FLAG key_ps_type = (m_storage ==
TPM_Storage_Type::User) ? TSS_PS_TYPE_USER : TSS_PS_TYPE_SYSTEM;
208 m_ctx.
handle(), m_ctx.
srk(), to_uint32(blob.size()),
const_cast<uint8_t*
>(blob.data()), &m_key));
217 m_storage = storage_type;
219 const TSS_UUID key_uuid = to_tss_uuid(m_uuid);
220 const TSS_FLAG key_ps_type = (storage_type ==
TPM_Storage_Type::User) ? TSS_PS_TYPE_USER : TSS_PS_TYPE_SYSTEM;
222 const TSS_UUID srk_uuid = TSS_UUID_SRK;
225 ::Tspi_Context_RegisterKey(m_ctx.
handle(), m_key, key_ps_type, key_uuid, TSS_PS_TYPE_SYSTEM, srk_uuid));
230 if(storage_type != m_storage) {
231 throw TPM_Error(
"TPM key " + m_uuid.
to_string() +
" already registered with different storage type");
234 return format_url(m_uuid, m_storage);
238 TSS_KM_KEYINFO2* key_info;
239 UINT32 key_info_size;
243 ::Tspi_Context_GetRegisteredKeysByUUID2(
ctx.
handle(), TSS_PS_TYPE_SYSTEM,
nullptr, &key_info_size, &key_info));
245 std::vector<std::string> r(key_info_size);
247 for(
size_t i = 0; i != key_info_size; ++i) {
248 r[i] = format_url(key_info[i].keyUUID, key_info[i].persistentStorageType);
260 get_obj_attr(m_ctx.
handle(), m_key, TSS_TSPATTRIB_RSAKEY_INFO, TSS_TSPATTRIB_KEYINFO_RSA_MODULUS));
269 get_obj_attr(m_ctx.
handle(), m_key, TSS_TSPATTRIB_RSAKEY_INFO, TSS_TSPATTRIB_KEYINFO_RSA_EXPONENT));
288 std::vector<uint8_t> bits;
294 throw TPM_Error(
"Raw public key export not supported for RSA TPM keys");
298 throw TPM_Error(
"Private key export not supported for TPM keys");
302 return get_obj_attr(m_ctx.
handle(), m_key, TSS_TSPATTRIB_KEY_BLOB, TSS_TSPATTRIB_KEYBLOB_BLOB);
306 return std::make_unique<RSA_PublicKey>(
get_n(),
get_e());
317 TPM_Signing_Operation(
const TPM_PrivateKey& key, std::string_view hash_name) :
320 std::string hash_function()
const override {
return m_hash->name(); }
322 size_t signature_length()
const override {
return m_key.get_n().bytes(); }
324 void update(std::span<const uint8_t> msg)
override { m_hash->update(msg); }
326 AlgorithmIdentifier algorithm_identifier()
const override {
327 const std::string full_name =
"RSA/EMSA3(" + m_hash->name() +
")";
332 std::vector<uint8_t> sign(RandomNumberGenerator&)
override {
342 std::vector<uint8_t> id_and_msg;
343 id_and_msg.reserve(m_hash_id.size() + msg_hash.size());
344 id_and_msg.insert(id_and_msg.end(), m_hash_id.begin(), m_hash_id.end());
345 id_and_msg.insert(id_and_msg.end(), msg_hash.begin(), msg_hash.end());
347 TSS_HCONTEXT ctx = m_key.ctx().handle();
349 TSPI_CHECK_SUCCESS(::Tspi_Context_CreateObject(ctx, TSS_OBJECT_TYPE_HASH, TSS_HASH_OTHER, &tpm_hash));
350 TSPI_CHECK_SUCCESS(::Tspi_Hash_SetHashValue(tpm_hash, to_uint32(id_and_msg.size()), id_and_msg.data()));
352 BYTE* sig_bytes =
nullptr;
355 std::vector<uint8_t> sig(sig_bytes, sig_bytes + sig_len);
367 const TPM_PrivateKey m_key;
368 std::unique_ptr<HashFunction> m_hash;
369 std::vector<uint8_t> m_hash_id;
375 std::string_view params,
376 std::string_view )
const {
377 return std::make_unique<TPM_Signing_Operation>(*
this, params);
#define BOTAN_ARG_CHECK(expr, msg)
virtual OID object_identifier() const
static BigInt from_bytes(std::span< const uint8_t > bytes)
DER_Encoder & start_sequence()
DER_Encoder & encode(bool b)
static OID from_string(std::string_view str)
TSS_HCONTEXT handle() const
TPM_Context(pin_cb cb, const char *srk_password)
std::function< std::string(std::string)> pin_cb
void gen_random(uint8_t out[], size_t out_len)
uint32_t current_counter()
void stir_random(const uint8_t in[], size_t in_len)
std::vector< uint8_t > export_blob() const
std::vector< uint8_t > raw_public_key_bits() const override
std::string register_key(TPM_Storage_Type storage_type)
TPM_PrivateKey(TPM_Context &ctx, size_t bits, const char *key_password)
secure_vector< uint8_t > private_key_bits() const override
static std::vector< std::string > registered_keys(TPM_Context &ctx)
bool check_key(RandomNumberGenerator &rng, bool) const override
TPM_Context & ctx() const
std::unique_ptr< PK_Ops::Signature > create_signature_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
size_t key_length() const override
std::vector< uint8_t > public_key_bits() const override
size_t estimated_strength() const override
std::unique_ptr< Public_Key > public_key() const override
AlgorithmIdentifier algorithm_identifier() const override
std::string to_string() const
int(* update)(CTX *, const void *, CC_LONG len)
int(* final)(unsigned char *, CTX *)
std::string fmt(std::string_view format, const T &... args)
constexpr void typecast_copy(ToR &&out, FromR &&in)
std::vector< uint8_t > pkcs_hash_id(std::string_view name)
std::vector< T, secure_allocator< T > > secure_vector
size_t if_work_factor(size_t bits)
constexpr void copy_mem(T *out, const T *in, size_t n)
#define TSPI_CHECK_SUCCESS(expr)