Debian Codegraph @

src/shared/tpm2-util.h

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include "sd-dlopen.h"
5
6 #include "bitfield.h"
7 #include "iovec-util.h"
8 #include "shared-forward.h"
9 #include "sha256.h"
10
11 typedef enum TPM2Flags {
12 TPM2_FLAGS_USE_PIN = 1 << 0,
13 TPM2_FLAGS_USE_PCRLOCK = 1 << 1,
14 } TPM2Flags;
15
16 /* As per https://trustedcomputinggroup.org/wp-content/uploads/TCG_PCClient_PFP_r1p05_v23_pub.pdf a
17 * TPM2 on a Client PC must have at least 24 PCRs. This hardcodes our expectation of 24. */
18 #define TPM2_PCRS_MAX 24U
19 #define TPM2_PCRS_MASK ((UINT32_C(1) << TPM2_PCRS_MAX) - 1)
20
21 /* The SRK handle is defined in the Provisioning Guidance document (see above) in the table "Reserved Handles
22 * for TPM Provisioning Fundamental Elements". The SRK is useful because it is "shared", meaning it has no
23 * authValue nor authPolicy set, and thus may be used by anyone on the system to generate derived keys or
24 * seal secrets. This is useful if the TPM has an auth (password) set for the 'owner hierarchy', which would
25 * prevent users from generating primary transient keys, unless they knew the owner hierarchy auth. See
26 * the Provisioning Guidance document for more details. */
27 #define TPM2_SRK_HANDLE UINT32_C(0x81000001)
28
29 /* The TPM specification limits sealed data to MAX_SYM_DATA. Unfortunately, tpm2-tss incorrectly
30 * defines this value as 256; the TPM specification Part 2 ("Structures") section
31 * "TPMU_SENSITIVE_CREATE" states "For interoperability, MAX_SYM_DATA should be 128." */
32 #define TPM2_MAX_SEALED_DATA UINT16_C(128)
33
34 static inline bool TPM2_PCR_INDEX_VALID(unsigned pcr) {
35 return pcr < TPM2_PCRS_MAX;
36 }
37 static inline bool TPM2_PCR_MASK_VALID(uint32_t pcr_mask) {
38 return pcr_mask <= TPM2_PCRS_MASK;
39 }
40
41 #define FOREACH_PCR_IN_MASK(pcr, mask) BIT_FOREACH(pcr, mask)
42
43 #define TPM2_N_HASH_ALGORITHMS 4U
44
45 int dlopen_tpm2(int log_level);
46
47 #if HAVE_TPM2
48 #define TPM2_ESYS_NOTE(priority) \
49 SD_ELF_NOTE_DLOPEN("tpm", "Support for TPM", priority, "libtss2-esys.so.0")
50 #define TPM2_RC_NOTE(priority) \
51 SD_ELF_NOTE_DLOPEN("tpm", "Support for TPM", priority, "libtss2-rc.so.0")
52 #define TPM2_MU_NOTE(priority) \
53 SD_ELF_NOTE_DLOPEN("tpm", "Support for TPM", priority, "libtss2-mu.so.0")
54 #define TPM2_TCTI_DEVICE_NOTE(priority) \
55 SD_ELF_NOTE_DLOPEN("tpm", "Support for TPM", priority, "libtss2-tcti-device.so.0")
56
57 #define TPM2_NOTE(priority) \
58 ({ \
59 TPM2_ESYS_NOTE(priority); \
60 TPM2_RC_NOTE(priority); \
61 TPM2_MU_NOTE(priority); \
62 TPM2_TCTI_DEVICE_NOTE(priority); \
63 })
64
65 #define DLOPEN_TPM2(log_level, priority) \
66 ({ \
67 TPM2_NOTE(priority); \
68 dlopen_tpm2(log_level); \
69 })
70
71 #include <tss2/tss2_esys.h> /* IWYU pragma: export */
72 #include <tss2/tss2_mu.h> /* IWYU pragma: export */
73 #include <tss2/tss2_rc.h> /* IWYU pragma: export */
74
75 typedef struct Tpm2Context {
76 unsigned n_ref;
77
78 void *tcti_dl;
79 TSS2_TCTI_CONTEXT *tcti_context;
80 ESYS_CONTEXT *esys_context;
81 char *tcti_driver;
82 char *tcti_param;
83
84 /* Some selected cached capabilities of the TPM */
85 TPMS_ALG_PROPERTY *capability_algorithms;
86 size_t n_capability_algorithms;
87 TPMA_CC *capability_commands;
88 size_t n_capability_commands;
89 TPM2_ECC_CURVE *capability_ecc_curves;
90 size_t n_capability_ecc_curves;
91 TPML_PCR_SELECTION capability_pcrs;
92 } Tpm2Context;
93
94 int tpm2_context_new(const char *device, Tpm2Context **ret_context);
95 int tpm2_context_new_or_warn(const char *device, Tpm2Context **ret_context);
96 DECLARE_TRIVIAL_REF_UNREF_FUNC(Tpm2Context, tpm2_context);
97 DEFINE_TRIVIAL_CLEANUP_FUNC(Tpm2Context*, tpm2_context_unref);
98
99 typedef struct Tpm2Handle {
100 Tpm2Context *tpm2_context;
101 ESYS_TR esys_handle;
102
103 bool flush;
104 } Tpm2Handle;
105
106 #define _tpm2_handle(c, h) { .tpm2_context = (c), .esys_handle = (h), }
107 static const Tpm2Handle TPM2_HANDLE_NONE = _tpm2_handle(NULL, ESYS_TR_NONE);
108
109 void Esys_Freep(void *p);
110
111 int tpm2_handle_new(Tpm2Context *context, Tpm2Handle **ret_handle);
112 Tpm2Handle *tpm2_handle_free(Tpm2Handle *handle);
113 DEFINE_TRIVIAL_CLEANUP_FUNC(Tpm2Handle*, tpm2_handle_free);
114
115 typedef struct Tpm2PCRValue {
116 unsigned index;
117 TPMI_ALG_HASH hash;
118 TPM2B_DIGEST value;
119 } Tpm2PCRValue;
120
121 #define TPM2_PCR_VALUE_MAKE(i, h, v) \
122 (Tpm2PCRValue) { \
123 .index = (i), \
124 .hash = (h), \
125 .value = ((TPM2B_DIGEST) v), \
126 }
127
128 bool tpm2_pcr_value_valid(const Tpm2PCRValue *pcr_value);
129 bool tpm2_pcr_values_has_any_values(const Tpm2PCRValue *pcr_values, size_t n_pcr_values);
130 bool tpm2_pcr_values_has_all_values(const Tpm2PCRValue *pcr_values, size_t n_pcr_values);
131 int tpm2_pcr_value_from_string(const char *arg, Tpm2PCRValue *ret_pcr_value);
132 char* tpm2_pcr_value_to_string(const Tpm2PCRValue *pcr_value);
133
134 bool tpm2_pcr_values_valid(const Tpm2PCRValue *pcr_values, size_t n_pcr_values);
135 void tpm2_sort_pcr_values(Tpm2PCRValue *pcr_values, size_t n_pcr_values);
136 int tpm2_pcr_values_to_mask(const Tpm2PCRValue *pcr_values, size_t n_pcr_values, TPMI_ALG_HASH hash, uint32_t *ret_mask);
137 int tpm2_pcr_values_from_string(const char *arg, Tpm2PCRValue **ret_pcr_values, size_t *ret_n_pcr_values);
138 int tpm2_pcr_values_hash_count(const Tpm2PCRValue *pcr_values, size_t n_pcr_values, size_t *ret_count);
139 int tpm2_tpml_pcr_selection_from_pcr_values(const Tpm2PCRValue *pcr_values, size_t n_pcr_values, TPML_PCR_SELECTION *ret_selection, TPM2B_DIGEST **ret_values, size_t *ret_n_values);
140
141 int tpm2_make_encryption_session(Tpm2Context *c, const Tpm2Handle *primary, const Tpm2Handle *bind_key, Tpm2Handle **ret_session);
142
143 int tpm2_create_primary(Tpm2Context *c, const Tpm2Handle *session, const TPM2B_PUBLIC *template, const TPM2B_SENSITIVE_CREATE *sensitive, TPM2B_PUBLIC **ret_public, Tpm2Handle **ret_handle);
144 int tpm2_create(Tpm2Context *c, const Tpm2Handle *parent, const Tpm2Handle *session, const TPMT_PUBLIC *template, const TPMS_SENSITIVE_CREATE *sensitive, TPM2B_PUBLIC **ret_public, TPM2B_PRIVATE **ret_private);
145 int tpm2_load(Tpm2Context *c, const Tpm2Handle *parent, const Tpm2Handle *session, const TPM2B_PUBLIC *public, const TPM2B_PRIVATE *private, Tpm2Handle **ret_handle);
146 int tpm2_marshal_public(const TPM2B_PUBLIC *public, void **ret, size_t *ret_size);
147 int tpm2_marshal_nv_public(const TPM2B_NV_PUBLIC *nv_public, void **ret, size_t *ret_size);
148 int tpm2_unmarshal_nv_public(const void *data, size_t size, TPM2B_NV_PUBLIC *ret_nv_public);
149 int tpm2_marshal_blob(const TPM2B_PUBLIC *public, const TPM2B_PRIVATE *private, const TPM2B_ENCRYPTED_SECRET *seed, void **ret_blob, size_t *ret_blob_size);
150 int tpm2_unmarshal_blob(const void *blob, size_t blob_size, TPM2B_PUBLIC *ret_public, TPM2B_PRIVATE *ret_private, TPM2B_ENCRYPTED_SECRET *ret_seed);
151 int tpm2_get_name(Tpm2Context *c, const Tpm2Handle *handle, TPM2B_NAME **ret_name);
152
153 bool tpm2_supports_alg(Tpm2Context *c, TPM2_ALG_ID alg);
154 bool tpm2_supports_command(Tpm2Context *c, TPM2_CC command);
155 bool tpm2_supports_ecc_curve(Tpm2Context *c, TPM2_ECC_CURVE ecc_curve);
156
157 bool tpm2_test_parms(Tpm2Context *c, TPMI_ALG_PUBLIC alg, const TPMU_PUBLIC_PARMS *parms);
158
159 int tpm2_get_good_pcr_banks(Tpm2Context *c, uint32_t pcr_mask, TPMI_ALG_HASH **ret_banks);
160 int tpm2_get_good_pcr_banks_strv(Tpm2Context *c, uint32_t pcr_mask, char ***ret);
161 int tpm2_get_best_pcr_bank(Tpm2Context *c, uint32_t pcr_mask, TPMI_ALG_HASH *ret);
162
163 const char* tpm2_userspace_log_path(void);
164 const char* tpm2_firmware_log_path(void);
165
166 typedef enum Tpm2UserspaceEventType {
167 TPM2_EVENT_PHASE,
168 TPM2_EVENT_FILESYSTEM,
169 TPM2_EVENT_VOLUME_KEY,
170 TPM2_EVENT_MACHINE_ID,
171 TPM2_EVENT_PRODUCT_ID,
172 TPM2_EVENT_KEYSLOT,
173 TPM2_EVENT_NVPCR_INIT,
174 TPM2_EVENT_NVPCR_SEPARATOR,
175 TPM2_EVENT_DM_VERITY,
176 TPM2_EVENT_IMDS_USERDATA,
177 TPM2_EVENT_OS_SEPARATOR,
178 TPM2_EVENT_LOGIN,
179 _TPM2_USERSPACE_EVENT_TYPE_MAX,
180 _TPM2_USERSPACE_EVENT_TYPE_INVALID = -EINVAL,
181 } Tpm2UserspaceEventType;
182
183 DECLARE_STRING_TABLE_LOOKUP(tpm2_userspace_event_type, Tpm2UserspaceEventType);
184
185 int tpm2_pcr_extend_bytes(Tpm2Context *c, char **banks, unsigned pcr_index, const struct iovec *data, const struct iovec *secret, Tpm2UserspaceEventType event_type, const char *description);
186
187 /* Default allocation priority for NvPCRs that do not specify one explicitly. Lower values are more
188 * important and are allocated first when the TPM's NV index space is constrained. */
189 #define TPM2_NVPCR_PRIORITY_DEFAULT UINT64_C(1000)
190
191 int tpm2_nvpcr_get_index(const char *name, uint32_t *ret_nv_index, uint64_t *ret_priority);
192 int tpm2_nvpcr_extend_bytes(Tpm2Context *c, const Tpm2Handle *session, const char *name, const struct iovec *data, const struct iovec *secret, Tpm2UserspaceEventType event_type, const char *description);
193 int tpm2_nvpcr_acquire_anchor_secret(struct iovec *ret, bool sync_secondary);
194 int tpm2_nvpcr_initialize(Tpm2Context *c, const Tpm2Handle *session, const char *name, const struct iovec *anchor_secret);
195 int tpm2_nvpcr_read(Tpm2Context *c, const Tpm2Handle *session, const char *name, struct iovec *ret, uint32_t *ret_nv_index, uint64_t *ret_priority);
196
197 uint32_t tpm2_tpms_pcr_selection_to_mask(const TPMS_PCR_SELECTION *s);
198 void tpm2_tpms_pcr_selection_from_mask(uint32_t mask, TPMI_ALG_HASH hash, TPMS_PCR_SELECTION *ret);
199 bool tpm2_tpms_pcr_selection_has_mask(const TPMS_PCR_SELECTION *s, uint32_t mask);
200 void tpm2_tpms_pcr_selection_add_mask(TPMS_PCR_SELECTION *s, uint32_t mask);
201 void tpm2_tpms_pcr_selection_sub_mask(TPMS_PCR_SELECTION *s, uint32_t mask);
202 void tpm2_tpms_pcr_selection_add(TPMS_PCR_SELECTION *a, const TPMS_PCR_SELECTION *b);
203 void tpm2_tpms_pcr_selection_sub(TPMS_PCR_SELECTION *a, const TPMS_PCR_SELECTION *b);
204 void tpm2_tpms_pcr_selection_move(TPMS_PCR_SELECTION *a, TPMS_PCR_SELECTION *b);
205 char* tpm2_tpms_pcr_selection_to_string(const TPMS_PCR_SELECTION *s);
206 size_t tpm2_tpms_pcr_selection_weight(const TPMS_PCR_SELECTION *s);
207 #define tpm2_tpms_pcr_selection_is_empty(s) (tpm2_tpms_pcr_selection_weight(s) == 0)
208
209 uint32_t tpm2_tpml_pcr_selection_to_mask(const TPML_PCR_SELECTION *l, TPMI_ALG_HASH hash);
210 void tpm2_tpml_pcr_selection_from_mask(uint32_t mask, TPMI_ALG_HASH hash, TPML_PCR_SELECTION *ret);
211 bool tpm2_tpml_pcr_selection_has_mask(const TPML_PCR_SELECTION *l, TPMI_ALG_HASH hash, uint32_t mask);
212 void tpm2_tpml_pcr_selection_add_mask(TPML_PCR_SELECTION *l, TPMI_ALG_HASH hash, uint32_t mask);
213 void tpm2_tpml_pcr_selection_add_tpms_pcr_selection(TPML_PCR_SELECTION *l, const TPMS_PCR_SELECTION *s);
214 void tpm2_tpml_pcr_selection_sub_tpms_pcr_selection(TPML_PCR_SELECTION *l, const TPMS_PCR_SELECTION *s);
215 void tpm2_tpml_pcr_selection_add(TPML_PCR_SELECTION *a, const TPML_PCR_SELECTION *b);
216 void tpm2_tpml_pcr_selection_sub(TPML_PCR_SELECTION *a, const TPML_PCR_SELECTION *b);
217 char* tpm2_tpml_pcr_selection_to_string(const TPML_PCR_SELECTION *l);
218 size_t tpm2_tpml_pcr_selection_weight(const TPML_PCR_SELECTION *l);
219 #define tpm2_tpml_pcr_selection_is_empty(l) (tpm2_tpml_pcr_selection_weight(l) == 0)
220
221 int tpm2_digest_many(TPMI_ALG_HASH alg, TPM2B_DIGEST *digest, const struct iovec data[], size_t n_data, bool extend);
222 static inline int tpm2_digest_buffer(TPMI_ALG_HASH alg, TPM2B_DIGEST *digest, const void *data, size_t len, bool extend) {
223 return tpm2_digest_many(alg, digest, &IOVEC_MAKE((void*) data, len), 1, extend);
224 }
225 int tpm2_digest_many_digests(TPMI_ALG_HASH alg, TPM2B_DIGEST *digest, const TPM2B_DIGEST data[], size_t n_data, bool extend);
226 static inline int tpm2_digest_rehash(TPMI_ALG_HASH alg, TPM2B_DIGEST *digest) {
227 return tpm2_digest_many(alg, digest, NULL, 0, true);
228 }
229 static inline int tpm2_digest_init(TPMI_ALG_HASH alg, TPM2B_DIGEST *digest) {
230 return tpm2_digest_many(alg, digest, NULL, 0, false);
231 }
232
233 typedef struct Tpm2VendorInfo {
234 uint32_t level;
235 uint32_t revision_major;
236 uint32_t revision_minor;
237 uint32_t day_of_year;
238 uint32_t year;
239 uint32_t vendor_tpm_type;
240 uint16_t firmware_version_major;
241 uint16_t firmware_version_minor;
242 uint32_t firmware_version2;
243 char family_indicator[4+1];
244 char manufacturer[4+1];
245 char vendor_string[4*4+1];
246 } Tpm2VendorInfo;
247
248 int tpm2_vendor_info_to_modalias(const Tpm2VendorInfo *info, char **ret);
249 int tpm2_get_vendor_info(Tpm2Context *c, Tpm2VendorInfo *ret);
250
251 void tpm2_log_debug_tpml_pcr_selection(const TPML_PCR_SELECTION *l, const char *msg);
252 void tpm2_log_debug_pcr_value(const Tpm2PCRValue *pcr_value, const char *msg);
253 void tpm2_log_debug_buffer(const void *buffer, size_t size, const char *msg);
254 void tpm2_log_debug_digest(const TPM2B_DIGEST *digest, const char *msg);
255 void tpm2_log_debug_name(const TPM2B_NAME *name, const char *msg);
256
257 typedef struct Tpm2PCRPredictionResult {
258 TPM2B_DIGEST hash[TPM2_N_HASH_ALGORITHMS]; /* a hash for each potential algorithm */
259 } Tpm2PCRPredictionResult;
260
261 TPM2B_DIGEST *tpm2_pcr_prediction_result_get_hash(Tpm2PCRPredictionResult *result, uint16_t alg);
262
263 /* A structure encapsulating a full set of PCR predictions with alternatives. This can be converted into a
264 * series of PolicyOR + PolicyPCR items for the TPM. */
265 typedef struct Tpm2PCRPrediction {
266 uint32_t pcrs; /* A mask of pcrs included */
267 OrderedSet* results[TPM2_PCRS_MAX]; /* set of Tpm2PCRPredictionResult objects, one for each PCR */
268 } Tpm2PCRPrediction;
269
270 void tpm2_pcr_prediction_done(Tpm2PCRPrediction *p);
271
272 extern const struct hash_ops tpm2_pcr_prediction_result_hash_ops;
273
274 bool tpm2_pcr_prediction_equal(Tpm2PCRPrediction *a, Tpm2PCRPrediction *b, uint16_t algorithm);
275
276 int tpm2_pcr_prediction_to_json(const Tpm2PCRPrediction *prediction, uint16_t algorithm, sd_json_variant **ret);
277 int tpm2_pcr_prediction_from_json(Tpm2PCRPrediction *prediction, uint16_t algorithm, sd_json_variant *aj);
278
279 /* As structure encapsulating all metadata stored for a pcrlock policy on disk */
280 typedef struct Tpm2PCRLockPolicy {
281 /* The below is the fixed metadata encoding information about the NV index we store the
282 * PolicyAuthorizeNV policy in, as well as a pinned SRK, and the encrypted PIN to use for writing to
283 * the NV Index. */
284 uint16_t algorithm;
285 uint32_t nv_index;
286 struct iovec nv_handle;
287 struct iovec nv_public;
288 struct iovec srk_handle;
289 struct iovec pin_public;
290 struct iovec pin_private;
291
292 /* The below contains the current prediction whose resulting policy is stored in the NV
293 * index. Once in JSON and once in parsed form. When the policy is updated the fields below are
294 * changed, the fields above remain fixed. */
295 sd_json_variant *prediction_json;
296 Tpm2PCRPrediction prediction;
297 } Tpm2PCRLockPolicy;
298
299 void tpm2_pcrlock_policy_done(Tpm2PCRLockPolicy *data);
300 int tpm2_pcrlock_policy_from_json(sd_json_variant *v, Tpm2PCRLockPolicy *ret_policy);
301 int tpm2_pcrlock_search_file(const char *path, FILE **ret_file, char **ret_path);
302 int tpm2_pcrlock_policy_load(const char *path, Tpm2PCRLockPolicy *ret_policy);
303 int tpm2_pcrlock_policy_from_credentials(const struct iovec *srk, const struct iovec *nv, Tpm2PCRLockPolicy *ret);
304
305 int tpm2_index_to_handle(Tpm2Context *c, TPM2_HANDLE index, const Tpm2Handle *session, TPM2B_PUBLIC **ret_public, TPM2B_NAME **ret_name, TPM2B_NAME **ret_qname, Tpm2Handle **ret_handle);
306 int tpm2_index_from_handle(Tpm2Context *c, const Tpm2Handle *handle, TPM2_HANDLE *ret_index);
307
308 int tpm2_pcr_read(Tpm2Context *c, const TPML_PCR_SELECTION *pcr_selection, Tpm2PCRValue **ret_pcr_values, size_t *ret_n_pcr_values);
309 int tpm2_pcr_read_missing_values(Tpm2Context *c, Tpm2PCRValue *pcr_values, size_t n_pcr_values);
310
311 int tpm2_auth_value_from_pin(TPMI_ALG_HASH hash, const char *pin, TPM2B_AUTH *ret_auth);
312 int tpm2_set_auth(Tpm2Context *c, const Tpm2Handle *handle, const char *pin);
313 int tpm2_set_auth_binary(Tpm2Context *c, const Tpm2Handle *handle, const TPM2B_AUTH *auth);
314
315 int tpm2_make_policy_session(Tpm2Context *c, const Tpm2Handle *primary, const Tpm2Handle *encryption_session, Tpm2Handle **ret_session);
316
317 int tpm2_policy_auth_value(Tpm2Context *c, const Tpm2Handle *session, TPM2B_DIGEST **ret_policy_digest);
318 int tpm2_policy_authorize_nv(Tpm2Context *c, const Tpm2Handle *session, const Tpm2Handle *nv_handle, TPM2B_DIGEST **ret_policy_digest);
319 int tpm2_policy_pcr(Tpm2Context *c, const Tpm2Handle *session, const TPML_PCR_SELECTION *pcr_selection, TPM2B_DIGEST **ret_policy_digest);
320 int tpm2_policy_or(Tpm2Context *c, const Tpm2Handle *session, const TPM2B_DIGEST *branches, size_t n_branches, TPM2B_DIGEST **ret_policy_digest);
321 int tpm2_policy_super_pcr(Tpm2Context *c, const Tpm2Handle *session, const Tpm2PCRPrediction *prediction, uint16_t algorithm);
322 int tpm2_policy_signed_hmac_sha256(Tpm2Context *c, const Tpm2Handle *session, const Tpm2Handle *hmac_key_handle, const struct iovec *hmac_key, TPM2B_DIGEST **ret_policy_digest);
323
324 int tpm2_calculate_pubkey_name(const TPMT_PUBLIC *public, TPM2B_NAME *ret_name);
325 int tpm2_calculate_nv_index_name(const TPMS_NV_PUBLIC *nvpublic, TPM2B_NAME *ret_name);
326
327 int tpm2_calculate_policy_auth_value(TPM2B_DIGEST *digest);
328 int tpm2_calculate_policy_authorize(const TPM2B_PUBLIC *public, const TPM2B_DIGEST *policy_ref, TPM2B_DIGEST *digest);
329 int tpm2_calculate_policy_authorize_nv(const TPM2B_NV_PUBLIC *public, TPM2B_DIGEST *digest);
330 int tpm2_calculate_policy_pcr(const Tpm2PCRValue *pcr_values, size_t n_pcr_values, TPM2B_DIGEST *digest);
331 int tpm2_calculate_policy_or(const TPM2B_DIGEST *branches, size_t n_branches, TPM2B_DIGEST *digest);
332 int tpm2_calculate_policy_super_pcr(Tpm2PCRPrediction *prediction, uint16_t algorithm, TPM2B_DIGEST *pcr_policy);
333 int tpm2_calculate_policy_signed(TPM2B_DIGEST *digest, const TPM2B_NAME *name);
334 int tpm2_calculate_serialize(TPM2_HANDLE handle, const TPM2B_NAME *name, const TPM2B_PUBLIC *public, void **ret_serialized, size_t *ret_serialized_size);
335 int tpm2_calculate_sealing_policy(const Tpm2PCRValue *pcr_values, size_t n_pcr_values, const TPM2B_PUBLIC *public, bool use_pin, const Tpm2PCRLockPolicy *pcrlock_policy, TPM2B_DIGEST *digest);
336 int tpm2_calculate_seal(TPM2_HANDLE parent_handle, const TPM2B_PUBLIC *parent_public, const TPMA_OBJECT *attributes, const struct iovec *secret, const TPM2B_DIGEST *policy, const char *pin, struct iovec *ret_secret, struct iovec *ret_blob, struct iovec *ret_serialized_parent);
337
338 int tpm2_get_srk_template(TPMI_ALG_PUBLIC alg, TPMT_PUBLIC *ret_template);
339 int tpm2_get_best_srk_template(Tpm2Context *c, TPMT_PUBLIC *ret_template);
340
341 int tpm2_get_srk(Tpm2Context *c, const Tpm2Handle *session, TPM2B_PUBLIC **ret_public, TPM2B_NAME **ret_name, TPM2B_NAME **ret_qname, Tpm2Handle **ret_handle);
342 int tpm2_get_or_create_srk(Tpm2Context *c, const Tpm2Handle *session, TPM2B_PUBLIC **ret_public, TPM2B_NAME **ret_name, TPM2B_NAME **ret_qname, Tpm2Handle **ret_handle);
343
344 int tpm2_seal(Tpm2Context *c, uint32_t seal_key_handle, const TPM2B_DIGEST policy_hash[], size_t n_policy, const char *pin, struct iovec *ret_secret, struct iovec **ret_blobs, size_t *ret_n_blobs, uint16_t *ret_primary_alg, struct iovec *ret_srk);
345 int tpm2_unseal(Tpm2Context *c, uint32_t hash_pcr_mask, uint16_t pcr_bank, const struct iovec *pubkey, uint32_t pubkey_pcr_mask, sd_json_variant *signature, const char *pin, const Tpm2PCRLockPolicy *pcrlock_policy, uint16_t primary_alg, const struct iovec blobs[], size_t n_blobs, const struct iovec known_policy_hash[], size_t n_known_policy_hash, const struct iovec *srk, struct iovec *ret_secret);
346
347 /* tpm2_unseal() returns a bunch of different errors for various flavours of PCR issues, let's group them */
348 #define ERRNO_IS_NEG_TPM2_UNSEAL_BAD_PCR(r) IN_SET(r, -EREMCHG, -ENOANO, -EUCLEAN, -EPERM)
349
350 #if HAVE_OPENSSL
351 int tpm2_tpm2b_public_to_openssl_pkey(const TPM2B_PUBLIC *public, EVP_PKEY **ret);
352 int tpm2_tpm2b_public_from_openssl_pkey(const EVP_PKEY *pkey, TPM2B_PUBLIC *ret);
353 #endif
354
355 int tpm2_tpm2b_public_from_pem(const void *pem, size_t pem_size, TPM2B_PUBLIC *ret);
356 int tpm2_tpm2b_public_to_fingerprint(const TPM2B_PUBLIC *public, void **ret_fingerprint, size_t *ret_fingerprint_size);
357
358 int tpm2_define_policy_nv_index(Tpm2Context *c, const Tpm2Handle *session, TPM2_HANDLE requested_nv_index, const TPM2B_DIGEST *write_policy, TPM2_HANDLE *ret_nv_index, Tpm2Handle **ret_nv_handle, TPM2B_NV_PUBLIC *ret_nv_public);
359 int tpm2_write_policy_nv_index(Tpm2Context *c, const Tpm2Handle *policy_session, TPM2_HANDLE nv_index, const Tpm2Handle *nv_handle, const TPM2B_DIGEST *policy_digest);
360 int tpm2_undefine_nv_index(Tpm2Context *c, const Tpm2Handle *session, TPM2_HANDLE nv_index, const Tpm2Handle *nv_handle);
361 int tpm2_read_nv_index(Tpm2Context *c, const Tpm2Handle *session, TPM2_HANDLE nv_index, const Tpm2Handle *nv_handle, struct iovec *ret_value);
362
363 int tpm2_seal_data(Tpm2Context *c, const struct iovec *data, const Tpm2Handle *primary_handle, const Tpm2Handle *encryption_session, const TPM2B_DIGEST *policy, struct iovec *ret_public, struct iovec *ret_private);
364 int tpm2_unseal_data(Tpm2Context *c, const struct iovec *public, const struct iovec *private, const Tpm2Handle *primary_handle, const Tpm2Handle *policy_session, const Tpm2Handle *encryption_session, struct iovec *ret_data);
365
366 int tpm2_serialize(Tpm2Context *c, const Tpm2Handle *handle, struct iovec *ret);
367 int tpm2_deserialize(Tpm2Context *c, const struct iovec *serialized, Tpm2Handle **ret_handle);
368
369 int tpm2_load_public_key_file(const char *path, TPM2B_PUBLIC *ret);
370
371 int tpm2_hmac_key_from_pin(Tpm2Context *c, const Tpm2Handle *session, const TPM2B_AUTH *pin, Tpm2Handle **ret);
372
373 /* The tpm2-tss library has many structs that are simply a combination of an array (or object) and
374 * size. These macros allow easily initializing or assigning instances of such structs from an existing
375 * buffer/object and size, while also checking the size for safety with the struct buffer/object size. If the
376 * provided buffer/object is NULL, the resulting struct's buffer/object will be 0s. If the provided size is
377 * larger than the struct's buffer/object size, this results in assertion failure; to check the size, use one
378 * of the TPM2B_*_CHECK_SIZE() macros. */
379 #define TPM2B_AUTH_MAKE(b, s) TPM2B_BUF_SIZE_STRUCT_MAKE(b, s, TPM2B_AUTH, buffer, size)
380 #define TPM2B_DATA_MAKE(b, s) TPM2B_BUF_SIZE_STRUCT_MAKE(b, s, TPM2B_DATA, buffer, size)
381 #define TPM2B_DIGEST_MAKE(b, s) TPM2B_BUF_SIZE_STRUCT_MAKE(b, s, TPM2B_DIGEST, buffer, size)
382 #define TPM2B_ECC_PARAMETER_MAKE(b, s) TPM2B_BUF_SIZE_STRUCT_MAKE(b, s, TPM2B_ECC_PARAMETER, buffer, size)
383 #define TPM2B_ENCRYPTED_SECRET_MAKE(b, s) TPM2B_BUF_SIZE_STRUCT_MAKE(b, s, TPM2B_ENCRYPTED_SECRET, secret, size)
384 #define TPM2B_MAX_BUFFER_MAKE(b, s) TPM2B_BUF_SIZE_STRUCT_MAKE(b, s, TPM2B_MAX_BUFFER, buffer, size)
385 #define TPM2B_NAME_MAKE(b, s) TPM2B_BUF_SIZE_STRUCT_MAKE(b, s, TPM2B_NAME, name, size)
386 #define TPM2B_PRIVATE_MAKE(b, s) TPM2B_BUF_SIZE_STRUCT_MAKE(b, s, TPM2B_PRIVATE, buffer, size)
387 #define TPM2B_PRIVATE_KEY_RSA_MAKE(b, s) TPM2B_BUF_SIZE_STRUCT_MAKE(b, s, TPM2B_PRIVATE_KEY_RSA, buffer, size)
388 #define TPM2B_PUBLIC_KEY_RSA_MAKE(b, s) TPM2B_BUF_SIZE_STRUCT_MAKE(b, s, TPM2B_PUBLIC_KEY_RSA, buffer, size)
389 #define TPM2B_SENSITIVE_DATA_MAKE(b, s) TPM2B_BUF_SIZE_STRUCT_MAKE(b, s, TPM2B_SENSITIVE_DATA, buffer, size)
390 #define TPM2B_BUF_SIZE_STRUCT_MAKE(buf, size, struct_type, buffer_field, size_field) \
391 _TPM2B_BUF_SIZE_STRUCT_MAKE(buf, size, UNIQ, struct_type, buffer_field, size_field)
392 #define _TPM2B_BUF_SIZE_STRUCT_MAKE(buf, size, uniq, struct_type, buffer_field, size_field) \
393 ({ \
394 typeof(buf) UNIQ_T(BUF, uniq) = (buf); \
395 typeof(size) UNIQ_T(SIZE, uniq) = (size); \
396 struct_type UNIQ_T(STRUCT, uniq) = { .size_field = UNIQ_T(SIZE, uniq), }; \
397 assert(sizeof(UNIQ_T(STRUCT, uniq).buffer_field) >= (size_t) UNIQ_T(SIZE, uniq)); \
398 if (UNIQ_T(BUF, uniq)) \
399 memcpy_safe(UNIQ_T(STRUCT, uniq).buffer_field, UNIQ_T(BUF, uniq), UNIQ_T(SIZE, uniq)); \
400 UNIQ_T(STRUCT, uniq); \
401 })
402
403 /* Check if the size will fit in the TPM2B struct buffer. Returns 0 if the size will fit, otherwise this logs
404 * a debug message and returns < 0. */
405 #define TPM2B_AUTH_CHECK_SIZE(s) TPM2B_BUF_SIZE_STRUCT_CHECK_SIZE(s, TPM2B_AUTH, buffer)
406 #define TPM2B_DATA_CHECK_SIZE(s) TPM2B_BUF_SIZE_STRUCT_CHECK_SIZE(s, TPM2B_DATA, buffer)
407 #define TPM2B_DIGEST_CHECK_SIZE(s) TPM2B_BUF_SIZE_STRUCT_CHECK_SIZE(s, TPM2B_DIGEST, buffer)
408 #define TPM2B_ECC_PARAMETER_CHECK_SIZE(s) TPM2B_BUF_SIZE_STRUCT_CHECK_SIZE(s, TPM2B_ECC_PARAMETER, buffer)
409 #define TPM2B_ENCRYPTED_SECRET_CHECK_SIZE(s) TPM2B_BUF_SIZE_STRUCT_CHECK_SIZE(s, TPM2B_ENCRYPTED_SECRET, buffer)
410 #define TPM2B_MAX_BUFFER_CHECK_SIZE(s) TPM2B_BUF_SIZE_STRUCT_CHECK_SIZE(s, TPM2B_MAX_BUFFER, buffer)
411 #define TPM2B_NAME_CHECK_SIZE(s) TPM2B_BUF_SIZE_STRUCT_CHECK_SIZE(s, TPM2B_NAME, name)
412 #define TPM2B_PRIVATE_CHECK_SIZE(s) TPM2B_BUF_SIZE_STRUCT_CHECK_SIZE(s, TPM2B_PRIVATE, buffer)
413 #define TPM2B_PRIVATE_KEY_RSA_CHECK_SIZE(s) TPM2B_BUF_SIZE_STRUCT_CHECK_SIZE(s, TPM2B_PRIVATE_KEY_RSA, buffer)
414 #define TPM2B_PUBLIC_KEY_RSA_CHECK_SIZE(s) TPM2B_BUF_SIZE_STRUCT_CHECK_SIZE(s, TPM2B_PUBLIC_KEY_RSA, buffer)
415 #define TPM2B_SENSITIVE_DATA_CHECK_SIZE(s) TPM2B_BUF_SIZE_STRUCT_CHECK_SIZE(s, TPM2B_SENSITIVE_DATA, buffer)
416 #define TPM2B_BUF_SIZE_STRUCT_CHECK_SIZE(size, struct_type, buffer_field) \
417 _TPM2B_BUF_SIZE_STRUCT_CHECK_SIZE(size, UNIQ, struct_type, buffer_field)
418 #define _TPM2B_BUF_SIZE_STRUCT_CHECK_SIZE(size, uniq, struct_type, buffer_field) \
419 ({ \
420 size_t UNIQ_T(SIZE, uniq) = (size_t) (size); \
421 size_t UNIQ_T(BUFSIZE, uniq) = sizeof_field(struct_type, buffer_field); \
422 UNIQ_T(BUFSIZE, uniq) < UNIQ_T(SIZE, uniq) ? \
423 log_debug_errno(SYNTHETIC_ERRNO(EINVAL), \
424 "Size %zu larger than " #struct_type " buffer size %zu.", \
425 UNIQ_T(SIZE, uniq), UNIQ_T(BUFSIZE, uniq)) : \
426 0; \
427 })
428
429 #else /* HAVE_TPM2 */
430 typedef struct Tpm2Context {} Tpm2Context;
431 typedef struct Tpm2Handle {} Tpm2Handle;
432 typedef struct Tpm2PCRValue {} Tpm2PCRValue;
433
434 #define TPM2_PCR_VALUE_MAKE(i, h, v) (Tpm2PCRValue) {}
435
436 static inline int tpm2_pcrlock_search_file(const char *path, FILE **ret_file, char **ret_path) {
437 return -ENOENT;
438 }
439
440 #define DLOPEN_TPM2(log_level, priority) dlopen_tpm2(log_level)
441 #endif /* HAVE_TPM2 */
442
443 int tpm2_list_devices(bool legend, bool quiet);
444 int tpm2_find_device_auto(char **ret);
445
446 int tpm2_make_pcr_json_array(uint32_t pcr_mask, sd_json_variant **ret);
447 int tpm2_parse_pcr_json_array(sd_json_variant *v, uint32_t *ret);
448
449 int tpm2_make_luks2_json(int keyslot, uint32_t hash_pcr_mask, uint16_t pcr_bank, const struct iovec *pubkey, uint32_t pubkey_pcr_mask, uint16_t primary_alg, const struct iovec blobs[], size_t n_blobs, const struct iovec policy_hash[], size_t n_policy_hash, const struct iovec *salt, const struct iovec *srk, const struct iovec *pcrlock_nv, TPM2Flags flags, sd_json_variant **ret);
450 int tpm2_parse_luks2_json(sd_json_variant *v, int *ret_keyslot, uint32_t *ret_hash_pcr_mask, uint16_t *ret_pcr_bank, struct iovec *ret_pubkey, uint32_t *ret_pubkey_pcr_mask, uint16_t *ret_primary_alg, struct iovec **ret_blobs, size_t *ret_n_blobs, struct iovec **ret_policy_hash, size_t *ret_n_policy_hash, struct iovec *ret_salt, struct iovec *ret_srk, struct iovec *ret_pcrlock_nv, TPM2Flags *ret_flags);
451
452 /* Before v258 we used to bind to PCR 7 by default at various places if no explicit PCR mask was set. With
453 * v258 we stopped doing that (since the SecureBoot DB is as much subject to regular updates by tools such as
454 * fwupd as the firmware itself), but when unlocking to maintain compatibility when no mask is specified we
455 * still need to default to PCR 7. */
456 #define TPM2_PCR_INDEX_DEFAULT_LEGACY TPM2_PCR_SECURE_BOOT_POLICY
457 #define TPM2_PCR_MASK_DEFAULT_LEGACY INDEX_TO_MASK(uint32_t, TPM2_PCR_INDEX_DEFAULT_LEGACY)
458
459 /* We want the helpers below to work also if TPM2 libs are not available, hence define these four defines if
460 * they are missing. */
461 #ifndef TPM2_ALG_SHA1
462 #define TPM2_ALG_SHA1 0x4
463 #endif
464
465 #ifndef TPM2_ALG_SHA256
466 #define TPM2_ALG_SHA256 0xB
467 #endif
468
469 #ifndef TPM2_ALG_SHA384
470 #define TPM2_ALG_SHA384 0xC
471 #endif
472
473 #ifndef TPM2_ALG_SHA512
474 #define TPM2_ALG_SHA512 0xD
475 #endif
476
477 #ifndef TPM2_ALG_ECC
478 #define TPM2_ALG_ECC 0x23
479 #endif
480
481 #ifndef TPM2_ALG_RSA
482 #define TPM2_ALG_RSA 0x1
483 #endif
484
485 int tpm2_hash_alg_to_size(uint16_t alg);
486
487 const char* tpm2_hash_alg_to_string(uint16_t alg) _const_;
488 int tpm2_hash_alg_from_string(const char *alg) _pure_;
489
490 const char* tpm2_asym_alg_to_string(uint16_t alg) _const_;
491 int tpm2_asym_alg_from_string(const char *alg) _pure_;
492
493 const char* tpm2_sym_alg_to_string(uint16_t alg) _const_;
494 const char* tpm2_sym_mode_to_string(uint16_t mode) _const_;
495 int tpm2_sym_mode_from_string(const char *mode) _pure_;
496
497 char* tpm2_pcr_mask_to_string(uint32_t mask);
498
499 extern const uint16_t tpm2_hash_algorithms[];
500
501 typedef struct systemd_tpm2_plugin_params {
502 uint32_t search_pcr_mask;
503 const char *device;
504 const char *signature_path;
505 const char *pcrlock_path;
506 } systemd_tpm2_plugin_params;
507
508 typedef enum Tpm2Support {
509 /* NOTE! The systemd-analyze has-tpm2 command returns these flags 1:1 as exit status. Hence these
510 * flags are pretty much ABI! Hence, be extra careful when changing/extending these definitions. */
511 TPM2_SUPPORT_NONE = 0, /* no support */
512 TPM2_SUPPORT_FIRMWARE = 1 << 0, /* firmware reports TPM2 was used */
513 TPM2_SUPPORT_DRIVER = 1 << 1, /* the kernel has a driver loaded for it */
514 TPM2_SUPPORT_SYSTEM = 1 << 2, /* we support it ourselves */
515 TPM2_SUPPORT_SUBSYSTEM = 1 << 3, /* the kernel has the tpm subsystem enabled */
516 TPM2_SUPPORT_LIBRARIES = 1 << 4, /* we can dlopen the tpm2 libraries */
517 TPM2_SUPPORT_API = TPM2_SUPPORT_FIRMWARE|TPM2_SUPPORT_DRIVER|TPM2_SUPPORT_SYSTEM|TPM2_SUPPORT_SUBSYSTEM|TPM2_SUPPORT_LIBRARIES,
518
519 /* Flags below are used by pcrlock, to indicate hardware specific features. It's not used by systemd-analyze has-tpm2. */
520 TPM2_SUPPORT_AUTHORIZE_NV = 1 << 5, /* chip supports PolicyAuthorizeNV */
521 TPM2_SUPPORT_SHA256 = 1 << 6, /* chip supports SHA-256 */
522 TPM2_SUPPORT_API_PCRLOCK = TPM2_SUPPORT_API | TPM2_SUPPORT_AUTHORIZE_NV | TPM2_SUPPORT_SHA256,
523
524 /* Flags below are not returned by systemd-analyze has-tpm2 nor by systemd-pcrlock as exit status. */
525 TPM2_SUPPORT_LIBTSS2_ESYS = 1 << 7, /* we can dlopen libtss2-esys.so.0 */
526 TPM2_SUPPORT_LIBTSS2_RC = 1 << 8, /* we can dlopen libtss2-rc.so.0 */
527 TPM2_SUPPORT_LIBTSS2_MU = 1 << 9, /* we can dlopen libtss2-mu.so.0 */
528 TPM2_SUPPORT_LIBTSS2_ALL = TPM2_SUPPORT_LIBTSS2_ESYS|TPM2_SUPPORT_LIBTSS2_RC|TPM2_SUPPORT_LIBTSS2_MU,
529
530 /* Combined flags for generic (i.e. not tool-specific) support */
531 TPM2_SUPPORT_FULL = TPM2_SUPPORT_API|TPM2_SUPPORT_LIBTSS2_ALL,
532 TPM2_SUPPORT_SOFTWARE = TPM2_SUPPORT_FULL & ~TPM2_SUPPORT_FIRMWARE, /* Same, just without PC firmware support */
533 } Tpm2Support;
534
535 Tpm2Support tpm2_support_full(Tpm2Support mask);
536 static inline Tpm2Support tpm2_support(void) {
537 return tpm2_support_full(TPM2_SUPPORT_FULL);
538 }
539 static inline bool tpm2_is_fully_supported(void) {
540 return tpm2_support() == TPM2_SUPPORT_FULL;
541 }
542 static inline bool tpm2_is_mostly_supported(void) {
543 return (tpm2_support() & TPM2_SUPPORT_SOFTWARE) == TPM2_SUPPORT_SOFTWARE;
544 }
545
546 int verb_has_tpm2_generic(bool quiet);
547
548 int tpm2_parse_pcr_argument(const char *arg, Tpm2PCRValue **ret_pcr_values, size_t *ret_n_pcr_values);
549 int tpm2_parse_pcr_argument_append(const char *arg, Tpm2PCRValue **pcr_values, size_t *n_pcr_values);
550 int tpm2_parse_pcr_argument_to_mask(const char *arg, uint32_t *mask);
551
552 int tpm2_load_pcr_signature(const char *path, sd_json_variant **ret);
553 int tpm2_load_pcr_public_key(const char *path, void **ret_pubkey, size_t *ret_pubkey_size);
554
555 int tpm2_util_pbkdf2_hmac_sha256(const void *pass,
556 size_t passlen,
557 const void *salt,
558 size_t saltlen,
559 uint8_t ret[static SHA256_DIGEST_SIZE]);
560
561 enum {
562 /* Additional defines for the PCR index naming enum from "fundamental/tpm2-pcr.h" */
563 _TPM2_PCR_INDEX_MAX_DEFINED = TPM2_PCRS_MAX,
564 _TPM2_PCR_INDEX_INVALID = -EINVAL,
565 };
566
567 DECLARE_STRING_TABLE_LOOKUP(tpm2_pcr_index, int);
568
569 /* The first and last NV index handle that is not registered to any company, as per TCG's "Registry of
570 * Reserved TPM 2.0 Handles and Localities", section 2.2.2. */
571 #define TPM2_NV_INDEX_UNASSIGNED_FIRST UINT32_C(0x01800000)
572 #define TPM2_NV_INDEX_UNASSIGNED_LAST UINT32_C(0x01BFFFFF)
573
574 #if HAVE_TPM2
575 /* Verify that the above is indeed a subset of the general NV Index range */
576 assert_cc(TPM2_NV_INDEX_UNASSIGNED_FIRST >= TPM2_NV_INDEX_FIRST);
577 assert_cc(TPM2_NV_INDEX_UNASSIGNED_LAST <= TPM2_NV_INDEX_LAST);
578 #endif
579
580 bool tpm2_nvpcr_name_is_valid(const char *name);