Botan 3.4.0
Crypto and TLS for C&
tpm.h
Go to the documentation of this file.
1
2/*
3* TPM 1.2 interface
4* (C) 2015 Jack Lloyd
5*
6* Botan is released under the Simplified BSD License (see license.txt)
7*/
8
9#ifndef BOTAN_TPM_H_
10#define BOTAN_TPM_H_
11
12#include <botan/bigint.h>
13#include <botan/exceptn.h>
14#include <botan/pk_keys.h>
15#include <botan/rng.h>
16#include <botan/uuid.h>
17#include <functional>
18
19//TODO remove this
20#include <tss/tspi.h>
21
22namespace Botan {
23
25 public:
26 TPM_Error(std::string_view err) : Exception(err) {}
27
28 ErrorType error_type() const noexcept override { return ErrorType::TPMError; }
29};
30
31/**
32* Creates a connection to the TPM. All other TPM types take and hold
33* a TPM_Context reference, so all other objects must be deallocated
34* before ~TPM_Context runs.
35*
36* Use nullptr for the srk_password to indicate the well known secret
37* (ie, an unencrypted SRK). This is usually what you want.
38*
39* TODO: handling owner password?
40*/
42 public:
43 /**
44 * User callback for getting the PIN. Will be passed the best available
45 * description of what we are attempting to load.
46 */
47 typedef std::function<std::string(std::string)> pin_cb;
48
49 TPM_Context(pin_cb cb, const char* srk_password);
50
52
53 // Get data from the TPM's RNG, whatever that is
54 void gen_random(uint8_t out[], size_t out_len);
55
56 // Uses Tspi_TPM_StirRandom to add data to TPM's internal pool
57 void stir_random(const uint8_t in[], size_t in_len);
58
59 std::string get_user_pin(const std::string& who) { return m_pin_cb(who); }
60
61 uint32_t current_counter();
62
63 TSS_HCONTEXT handle() const { return m_ctx; }
64
65 TSS_HKEY srk() const { return m_srk; }
66
67 private:
68 std::function<std::string(std::string)> m_pin_cb;
69 TSS_HCONTEXT m_ctx;
70 TSS_HKEY m_srk;
71 TSS_HTPM m_tpm;
72 TSS_HPOLICY m_srk_policy;
73};
74
76 public:
77 TPM_RNG(TPM_Context& ctx) : m_ctx(ctx) {}
78
79 bool accepts_input() const override { return true; }
80
81 std::string name() const override { return "TPM_RNG"; }
82
83 bool is_seeded() const override { return true; }
84
85 private:
86 void fill_bytes_with_input(std::span<uint8_t> output, std::span<const uint8_t> input) override {
87 if(!input.empty()) {
88 m_ctx.stir_random(input.data(), input.size());
89 }
90
91 if(!output.empty()) {
92 m_ctx.gen_random(output.data(), output.size());
93 }
94 }
95
96 private:
97 TPM_Context& m_ctx;
98};
99
101
102/*
103* Also implements the public interface, but does not have usable
104* TODO: derive from RSA_PublicKey???
105*/
107 public:
108 // TODO: key import?
109
110 /*
111 * Create a new key on the TPM parented to the SRK
112 * @param bits must be 1024 or 2048
113 */
114 TPM_PrivateKey(TPM_Context& ctx, size_t bits, const char* key_password);
115
116 // reference an existing TPM key using URL syntax from GnuTLS
117 // "tpmkey:uuid=79f07ca9-73ac-478a-9093-11ca6702e774;storage=user"
118 //TPM_PrivateKey(TPM_Context& ctx, std::string_view tpm_url);
119
120 TPM_PrivateKey(TPM_Context& ctx, std::string_view uuid, TPM_Storage_Type storage_type);
121
122 TPM_PrivateKey(TPM_Context& ctx, const std::vector<uint8_t>& blob);
123
124 /**
125 * If the key is not currently registered under a known UUID,
126 * generates a new random UUID and registers the key.
127 * Returns the access URL.
128 */
129 std::string register_key(TPM_Storage_Type storage_type);
130
131 /**
132 * Returns a copy of the public key
133 */
134 std::unique_ptr<Public_Key> public_key() const override;
135
136 std::vector<uint8_t> export_blob() const;
137
138 TPM_Context& ctx() const { return m_ctx; }
139
140 TSS_HKEY handle() const { return m_key; }
141
142 /*
143 * Returns the list of all keys (in URL format) registered with the system
144 */
145 static std::vector<std::string> registered_keys(TPM_Context& ctx);
146
147 size_t estimated_strength() const override;
148
149 size_t key_length() const override;
150
151 AlgorithmIdentifier algorithm_identifier() const override;
152
153 std::vector<uint8_t> public_key_bits() const override;
154
155 secure_vector<uint8_t> private_key_bits() const override;
156
157 bool check_key(RandomNumberGenerator& rng, bool) const override;
158
159 BigInt get_n() const;
160
161 BigInt get_e() const;
162
163 std::string algo_name() const override { return "RSA"; } // ???
164
165 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator&) const override {
166 throw Not_Implemented("Cannot generate a new TPM-based keypair from this asymmetric key");
167 }
168
169 bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::Signature); }
170
171 std::unique_ptr<PK_Ops::Signature> create_signature_op(RandomNumberGenerator& rng,
172 std::string_view params,
173 std::string_view provider) const override;
174
175 private:
176 TPM_Context& m_ctx;
177 TSS_HKEY m_key;
178
179 // Only set for registered keys
180 UUID m_uuid;
181 TPM_Storage_Type m_storage;
182
183 // Lazily computed in get_n, get_e
184 mutable BigInt m_n, m_e;
185};
186
187// TODO: NVRAM interface
188// TODO: PCR measurement, writing, key locking
189
190} // namespace Botan
191
192#endif
TSS_HCONTEXT handle() const
Definition tpm.h:63
TSS_HKEY srk() const
Definition tpm.h:65
std::string get_user_pin(const std::string &who)
Definition tpm.h:59
std::function< std::string(std::string)> pin_cb
Definition tpm.h:47
ErrorType error_type() const noexcept override
Definition tpm.h:28
TPM_Error(std::string_view err)
Definition tpm.h:26
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &) const override
Definition tpm.h:165
bool supports_operation(PublicKeyOperation op) const override
Definition tpm.h:169
TPM_Context & ctx() const
Definition tpm.h:138
TSS_HKEY handle() const
Definition tpm.h:140
std::string algo_name() const override
Definition tpm.h:163
bool accepts_input() const override
Definition tpm.h:79
bool is_seeded() const override
Definition tpm.h:83
std::string name() const override
Definition tpm.h:81
TPM_RNG(TPM_Context &ctx)
Definition tpm.h:77
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition compiler.h:31
PublicKeyOperation
Definition pk_keys.h:45
TPM_Storage_Type
Definition tpm.h:100
ErrorType
Definition exceptn.h:20
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61