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