summaryrefslogtreecommitdiffstats
path: root/comm/third_party/botan/src/lib/prov/tpm/tpm.cpp
blob: dec2316b106200f69fd8cd8b886322b88e45988f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
* TPM 1.2 interface
* (C) 2015 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/

#include <botan/tpm.h>
#include <botan/rsa.h>
#include <botan/hash.h>
#include <botan/hash_id.h>
#include <botan/der_enc.h>
#include <botan/workfactor.h>
#include <botan/pk_ops.h>
#include <sstream>

#include <tss/platform.h>
#include <tss/tspi.h>
#include <trousers/trousers.h>

// TODO: dynamically load the TPM libraries?

namespace Botan {

namespace {

void tss_error(TSS_RESULT res, const char* expr, const char* file, int line)
   {
   std::ostringstream err;
   err << "TPM error " << Trspi_Error_String(res)
       << " layer " << Trspi_Error_Layer(res)
       << " in " << expr << " at " << file << ":" << line;

   throw TPM_Error(err.str());
   }

TSS_FLAG bit_flag(size_t bits)
   {
   switch(bits)
      {
      // 512 supported, but ignored and rejected here
      case 1024:
         return TSS_KEY_SIZE_1024;
      case 2048:
         return TSS_KEY_SIZE_2048;

      // Most? v1.2 TPMs only support 1024 and 2048 bit keys ...
      case 4096:
         return TSS_KEY_SIZE_4096;
      case 8192:
         return TSS_KEY_SIZE_8192;
      case 16384:
         return TSS_KEY_SIZE_16384;
      default:
         throw Invalid_Argument("Unsupported TPM key size " + std::to_string(bits));
      }
   }

#if 0
bool is_srk_uuid(const UUID& uuid)
   {
   static const uint8_t srk[16] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 };
   const std::vector<uint8_t>& b = uuid.binary_value();
   return (b.size() == 16 && same_mem(b.data(), srk, 16));
   }
#endif

#define TSPI_CHECK_SUCCESS(expr) do {   \
   TSS_RESULT res = expr;           \
   if(res != TSS_SUCCESS)           \
      tss_error(res, #expr, __FILE__, __LINE__);         \
   } while(0)

std::vector<uint8_t> get_obj_attr(TSS_HCONTEXT ctx,
                                  TSS_HOBJECT obj,
                                  TSS_FLAG flag,
                                  TSS_FLAG sub_flag)
   {
   BYTE *data = nullptr;
   UINT32 data_len = 0;
   TSPI_CHECK_SUCCESS(::Tspi_GetAttribData(obj, flag, sub_flag, &data_len, &data));

   std::vector<uint8_t> r(data, data + data_len);

   TSPI_CHECK_SUCCESS(::Tspi_Context_FreeMemory(ctx, data));

   return r;
   }

void set_policy_secret(TSS_HPOLICY policy, const char* secret)
   {
   if(secret)
      {
      BYTE* as_b = const_cast<BYTE*>(reinterpret_cast<const BYTE*>(secret));
      TSPI_CHECK_SUCCESS(::Tspi_Policy_SetSecret(policy,
                                                 TSS_SECRET_MODE_PLAIN,
                                                 std::strlen(secret),
                                                 as_b));
      }
   else
      {
      static const uint8_t nullpass[20] = { 0 };

      TSPI_CHECK_SUCCESS(::Tspi_Policy_SetSecret(policy,
                                             TSS_SECRET_MODE_SHA1,
                                             sizeof(nullpass),
                                             const_cast<BYTE*>(nullpass)));
      }
   }

TSS_UUID to_tss_uuid(const UUID& uuid)
   {
   static_assert(sizeof(TSS_UUID) == 16, "Expected size of packed UUID");

   TSS_UUID tss_uuid;
   typecast_copy(tss_uuid, uuid.binary_value().data());
   return tss_uuid;
   }

UUID from_tss_uuid(const TSS_UUID& tss_uuid)
   {
   static_assert(sizeof(TSS_UUID) == 16, "Expected size of packed UUID");

   std::vector<uint8_t> mem(16);
   typecast_copy(mem.data(), tss_uuid);
   UUID uuid(std::move(mem));
   return uuid;
   }

TPM_Storage_Type storage_type_from_tss_flag(TSS_FLAG flag)
   {
   if(flag == TSS_PS_TYPE_USER)
      return TPM_Storage_Type::User;
   else if(flag == TSS_PS_TYPE_SYSTEM)
      return TPM_Storage_Type::System;
   else
      throw TPM_Error("Invalid storage flag " + std::to_string(flag));
   }

std::string format_url(const UUID& uuid, TPM_Storage_Type storage)
   {
   std::string storage_str = (storage == TPM_Storage_Type::User) ? "user" : "system";
   return "tpmkey:uuid=" + uuid.to_string() + ";storage=" + storage_str;
   }

std::string format_url(const TSS_UUID& tss_uuid, TSS_FLAG store_type)
   {
   UUID uuid = from_tss_uuid(tss_uuid);

   return format_url(from_tss_uuid(tss_uuid),
                     storage_type_from_tss_flag(store_type));
   }

}

TPM_Context::TPM_Context(pin_cb cb, const char* srk_password) : 
   m_pin_cb(cb),
   m_srk_policy(0)
   {
   TSPI_CHECK_SUCCESS(::Tspi_Context_Create(&m_ctx));
   TSPI_CHECK_SUCCESS(::Tspi_Context_Connect(m_ctx, nullptr));

   TSPI_CHECK_SUCCESS(::Tspi_Context_GetTpmObject(m_ctx, &m_tpm));

   const TSS_UUID SRK_UUID = TSS_UUID_SRK;

   TSPI_CHECK_SUCCESS(::Tspi_Context_LoadKeyByUUID(m_ctx, TSS_PS_TYPE_SYSTEM, SRK_UUID, &m_srk));

   TSPI_CHECK_SUCCESS(::Tspi_GetPolicyObject(m_srk, TSS_POLICY_USAGE, &m_srk_policy));
   set_policy_secret(m_srk_policy, srk_password);

   // TODO: do we have to cache it?
   // TODO: try to use SRK with null, if it fails call the pin cb?
   }

TPM_Context::~TPM_Context()
   {
   TSPI_CHECK_SUCCESS(::Tspi_Context_CloseObject(m_ctx, m_srk));
   //TSPI_CHECK_SUCCESS(::Tspi_Context_CloseObject(m_ctx, m_tpm));
   TSPI_CHECK_SUCCESS(::Tspi_Context_Close(m_srk_policy));
   TSPI_CHECK_SUCCESS(::Tspi_Context_Close(m_ctx));
   }

uint32_t TPM_Context::current_counter()
   {
   uint32_t r = 0;
   TSPI_CHECK_SUCCESS(::Tspi_TPM_ReadCounter(m_tpm, &r));
   return r;
   }

void TPM_Context::gen_random(uint8_t out[], size_t out_len)
   {
   BYTE* mem;
   TSPI_CHECK_SUCCESS(::Tspi_TPM_GetRandom(m_tpm, out_len, &mem));
   copy_mem(out, reinterpret_cast<const uint8_t*>(mem), out_len);
   TSPI_CHECK_SUCCESS(::Tspi_Context_FreeMemory(m_ctx, mem));
   }

void TPM_Context::stir_random(const uint8_t in[], size_t in_len)
   {
   TSPI_CHECK_SUCCESS(::Tspi_TPM_StirRandom(m_tpm, in_len, const_cast<BYTE*>(in)));
   }

TPM_PrivateKey::TPM_PrivateKey(TPM_Context& ctx, size_t bits,
                               const char* key_password) : m_ctx(ctx)
   {
   // TODO: can also do OAEP decryption via binding keys
   // TODO: offer signing, binding (decrypt), or legacy (sign + decrypt) keys?

   TSS_FLAG key_flags = bit_flag(bits) | TSS_KEY_VOLATILE | TSS_KEY_TYPE_SIGNING;

   TSS_HKEY key;
   TSPI_CHECK_SUCCESS(::Tspi_Context_CreateObject(m_ctx.handle(), TSS_OBJECT_TYPE_RSAKEY, key_flags, &key));

   TSPI_CHECK_SUCCESS(::Tspi_SetAttribUint32(key, TSS_TSPATTRIB_KEY_INFO,
                                         TSS_TSPATTRIB_KEYINFO_SIGSCHEME,
                                         TSS_SS_RSASSAPKCS1V15_DER));

   TSS_HPOLICY policy;
   TSPI_CHECK_SUCCESS(::Tspi_Context_CreateObject(m_ctx.handle(), TSS_OBJECT_TYPE_POLICY, TSS_POLICY_USAGE, &policy));
   set_policy_secret(policy, key_password);
   TSPI_CHECK_SUCCESS(::Tspi_Policy_AssignToObject(policy, key));

   TSPI_CHECK_SUCCESS(::Tspi_Key_CreateKey(key, ctx.srk(), 0));
   m_key = key;
   }

// reference a registered TPM key
TPM_PrivateKey::TPM_PrivateKey(TPM_Context& ctx, const std::string& uuid_str,
                               TPM_Storage_Type storage_type) :
   m_ctx(ctx),
   m_uuid(uuid_str),
   m_storage(storage_type)
   {
   const TSS_FLAG key_ps_type =
      (m_storage == TPM_Storage_Type::User) ? TSS_PS_TYPE_USER : TSS_PS_TYPE_SYSTEM;

   TSPI_CHECK_SUCCESS(::Tspi_Context_LoadKeyByUUID(m_ctx.handle(),
                                               key_ps_type,
                                               to_tss_uuid(m_uuid),
                                               &m_key));
   }

TPM_PrivateKey::TPM_PrivateKey(TPM_Context& ctx,
                               const std::vector<uint8_t>& blob) : m_ctx(ctx)
   {
   TSPI_CHECK_SUCCESS(::Tspi_Context_LoadKeyByBlob(m_ctx.handle(), m_ctx.srk(), blob.size(),
                                               const_cast<uint8_t*>(blob.data()),
                                               &m_key));

   //TSPI_CHECK_SUCCESS(::Tspi_Key_LoadKey(m_key, m_ctx.srk()));
   }

std::string TPM_PrivateKey::register_key(TPM_Storage_Type storage_type)
   {
   if(!m_uuid.is_valid())
      {
      TPM_RNG rng(ctx()); // use system_rng or arg RNG& instead?
      m_uuid = UUID(rng);
      m_storage = storage_type;

      const TSS_UUID key_uuid = to_tss_uuid(m_uuid);
      const TSS_FLAG key_ps_type =
         (storage_type == TPM_Storage_Type::User) ? TSS_PS_TYPE_USER : TSS_PS_TYPE_SYSTEM;

      const TSS_UUID srk_uuid = TSS_UUID_SRK;

      TSPI_CHECK_SUCCESS(::Tspi_Context_RegisterKey(m_ctx.handle(),
                                                m_key,
                                                key_ps_type,
                                                key_uuid,
                                                TSS_PS_TYPE_SYSTEM,
                                                srk_uuid));

      }

   // Presumably we could re-register in the other store and same UUID
   // Doesn't seem like what is desired most of the time here
   if(storage_type != m_storage)
      {
      throw TPM_Error("TPM key " + m_uuid.to_string() +
                      " already registered with different storage type");
      }

   return format_url(m_uuid, m_storage);
   }

std::vector<std::string> TPM_PrivateKey::registered_keys(TPM_Context& ctx)
   {
   TSS_KM_KEYINFO2* key_info;
   UINT32 key_info_size;

   // TODO: does the PS type matter here at all?
   TSPI_CHECK_SUCCESS(::Tspi_Context_GetRegisteredKeysByUUID2(ctx.handle(),
                                                          TSS_PS_TYPE_SYSTEM,
                                                          nullptr,
                                                          &key_info_size,
                                                          &key_info));

   std::vector<std::string> r(key_info_size);

   for(size_t i = 0; i != key_info_size; ++i)
      {
      r[i] = format_url(key_info[i].keyUUID, key_info[i].persistentStorageType);
      }

   // TODO: are we supposed to free this memory and if so how?
   //TSPI_CHECK_SUCCESS(::Tspi_Context_FreeMemory(ctx.handle(), key_info));

   return r;
   }

BigInt TPM_PrivateKey::get_n() const
   {
   if(m_n == 0)
      {
      m_n = BigInt::decode(get_obj_attr(m_ctx.handle(), m_key,
                                        TSS_TSPATTRIB_RSAKEY_INFO,
                                        TSS_TSPATTRIB_KEYINFO_RSA_MODULUS));
      }

   return m_n;
   }

BigInt TPM_PrivateKey::get_e() const
   {
   if(m_e == 0)
      {
      m_e = BigInt::decode(get_obj_attr(m_ctx.handle(), m_key,
                                        TSS_TSPATTRIB_RSAKEY_INFO,
                                        TSS_TSPATTRIB_KEYINFO_RSA_EXPONENT));
      }

   return m_e;
   }

size_t TPM_PrivateKey::estimated_strength() const
   {
   return if_work_factor(key_length());
   }

size_t TPM_PrivateKey::key_length() const
   {
   return get_n().bits();
   }

AlgorithmIdentifier TPM_PrivateKey::algorithm_identifier() const
   {
   return AlgorithmIdentifier(get_oid(),
                              AlgorithmIdentifier::USE_NULL_PARAM);
   }

std::vector<uint8_t> TPM_PrivateKey::public_key_bits() const
   {
   std::vector<uint8_t> bits;
   DER_Encoder(bits)
      .start_cons(SEQUENCE)
        .encode(get_n())
        .encode(get_e())
      .end_cons();
   return bits;
   }

secure_vector<uint8_t> TPM_PrivateKey::private_key_bits() const
   {
   throw TPM_Error("Private key export not supported for TPM keys");
   }

std::vector<uint8_t> TPM_PrivateKey::export_blob() const
   {
   return get_obj_attr(m_ctx.handle(), m_key,
                       TSS_TSPATTRIB_KEY_BLOB,
                       TSS_TSPATTRIB_KEYBLOB_BLOB);
   }

std::unique_ptr<Public_Key> TPM_PrivateKey::public_key() const
   {
   return std::unique_ptr<Public_Key>(new RSA_PublicKey(get_n(), get_e()));
   }

bool TPM_PrivateKey::check_key(RandomNumberGenerator&, bool) const
   {
   return true; // TODO do a kat or pairwise check
   }

namespace {

class TPM_Signing_Operation final : public PK_Ops::Signature
   {
   public:
      TPM_Signing_Operation(const TPM_PrivateKey& key,
                            const std::string& hash_name) :
         m_key(key),
         m_hash(HashFunction::create(hash_name)),
         m_hash_id(pkcs_hash_id(hash_name))
         {
         }

      size_t signature_length() const override
         {
         return m_key.get_n().bytes();
         }

      void update(const uint8_t msg[], size_t msg_len) override
         {
         m_hash->update(msg, msg_len);
         }

      secure_vector<uint8_t> sign(RandomNumberGenerator&) override
         {
         /*
         * v1.2 TPMs will only sign with PKCS #1 v1.5 padding. SHA-1 is built
         * in, all other hash inputs (TSS_HASH_OTHER) are treated as the
         * concatenation of the hash OID and hash value and signed with just the
         * 01FFFF... prefix. Even when using SHA-1 we compute the hash locally
         * since it is going to be much faster than pushing data over the LPC bus.
         */
         secure_vector<uint8_t> msg_hash = m_hash->final();

         std::vector<uint8_t> id_and_msg;
         id_and_msg.reserve(m_hash_id.size() + msg_hash.size());
         id_and_msg.insert(id_and_msg.end(), m_hash_id.begin(), m_hash_id.end());
         id_and_msg.insert(id_and_msg.end(), msg_hash.begin(), msg_hash.end());

         TSS_HCONTEXT ctx = m_key.ctx().handle();
         TSS_HHASH tpm_hash;
         TSPI_CHECK_SUCCESS(::Tspi_Context_CreateObject(ctx, TSS_OBJECT_TYPE_HASH, TSS_HASH_OTHER, &tpm_hash));
         TSPI_CHECK_SUCCESS(::Tspi_Hash_SetHashValue(tpm_hash, id_and_msg.size(), id_and_msg.data()));

         BYTE* sig_bytes = nullptr;
         UINT32 sig_len = 0;
         TSPI_CHECK_SUCCESS(::Tspi_Hash_Sign(tpm_hash, m_key.handle(), &sig_len, &sig_bytes));
         secure_vector<uint8_t> sig(sig_bytes, sig_bytes + sig_len);

         // TODO: RAII for Context_FreeMemory
         TSPI_CHECK_SUCCESS(::Tspi_Context_FreeMemory(ctx, sig_bytes));

         // TODO: RAII for Context_CloseObject
         TSPI_CHECK_SUCCESS(::Tspi_Context_CloseObject(ctx, tpm_hash));

         return sig;
         }

   private:
      const TPM_PrivateKey& m_key;
      std::unique_ptr<HashFunction> m_hash;
      std::vector<uint8_t> m_hash_id;
   };

}

std::unique_ptr<PK_Ops::Signature>
TPM_PrivateKey::create_signature_op(RandomNumberGenerator& /*rng*/,
                                    const std::string& params,
                                    const std::string& /*provider*/) const
   {
   return std::unique_ptr<PK_Ops::Signature>(new TPM_Signing_Operation(*this, params));
   }

}