]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/cryptsetup/cryptsetup-tokens/luks2-tpm2.c
tpm2-util: more iovec'ification
[thirdparty/systemd.git] / src / cryptsetup / cryptsetup-tokens / luks2-tpm2.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "ask-password-api.h"
5 #include "env-util.h"
6 #include "hexdecoct.h"
7 #include "json.h"
8 #include "log.h"
9 #include "luks2-tpm2.h"
10 #include "parse-util.h"
11 #include "random-util.h"
12 #include "sha256.h"
13 #include "strv.h"
14 #include "tpm2-util.h"
15
16 int acquire_luks2_key(
17 const char *device,
18 uint32_t hash_pcr_mask,
19 uint16_t pcr_bank,
20 const struct iovec *pubkey,
21 uint32_t pubkey_pcr_mask,
22 const char *signature_path,
23 const char *pin,
24 const char *pcrlock_path,
25 uint16_t primary_alg,
26 const struct iovec *blob,
27 const struct iovec *policy_hash,
28 const struct iovec *salt,
29 const struct iovec *srk,
30 TPM2Flags flags,
31 struct iovec *ret_decrypted_key) {
32
33 _cleanup_(json_variant_unrefp) JsonVariant *signature_json = NULL;
34 _cleanup_free_ char *auto_device = NULL;
35 _cleanup_(erase_and_freep) char *b64_salted_pin = NULL;
36 int r;
37
38 assert(iovec_is_valid(salt));
39 assert(ret_decrypted_key);
40
41 if (!device) {
42 r = tpm2_find_device_auto(&auto_device);
43 if (r == -ENODEV)
44 return -EAGAIN; /* Tell the caller to wait for a TPM2 device to show up */
45 if (r < 0)
46 return log_error_errno(r, "Could not find TPM2 device: %m");
47
48 device = auto_device;
49 }
50
51 if ((flags & TPM2_FLAGS_USE_PIN) && !pin)
52 return -ENOANO;
53
54 if (pin && iovec_is_set(salt)) {
55 uint8_t salted_pin[SHA256_DIGEST_SIZE] = {};
56 CLEANUP_ERASE(salted_pin);
57 r = tpm2_util_pbkdf2_hmac_sha256(pin, strlen(pin), salt->iov_base, salt->iov_len, salted_pin);
58 if (r < 0)
59 return log_error_errno(r, "Failed to perform PBKDF2: %m");
60
61 r = base64mem(salted_pin, sizeof(salted_pin), &b64_salted_pin);
62 if (r < 0)
63 return log_error_errno(r, "Failed to base64 encode salted pin: %m");
64 pin = b64_salted_pin;
65 }
66
67 if (pubkey_pcr_mask != 0) {
68 r = tpm2_load_pcr_signature(signature_path, &signature_json);
69 if (r < 0)
70 return log_error_errno(r, "Failed to load PCR signature: %m");
71 }
72
73 _cleanup_(tpm2_pcrlock_policy_done) Tpm2PCRLockPolicy pcrlock_policy = {};
74 if (FLAGS_SET(flags, TPM2_FLAGS_USE_PCRLOCK)) {
75 r = tpm2_pcrlock_policy_load(pcrlock_path, &pcrlock_policy);
76 if (r < 0)
77 return r;
78 }
79
80 _cleanup_(tpm2_context_unrefp) Tpm2Context *tpm2_context = NULL;
81 r = tpm2_context_new(device, &tpm2_context);
82 if (r < 0)
83 return log_error_errno(r, "Failed to create TPM2 context: %m");
84
85 r = tpm2_unseal(tpm2_context,
86 hash_pcr_mask,
87 pcr_bank,
88 pubkey,
89 pubkey_pcr_mask,
90 signature_json,
91 pin,
92 FLAGS_SET(flags, TPM2_FLAGS_USE_PCRLOCK) ? &pcrlock_policy : NULL,
93 primary_alg,
94 blob,
95 policy_hash,
96 srk,
97 ret_decrypted_key);
98 if (r < 0)
99 return log_error_errno(r, "Failed to unseal secret using TPM2: %m");
100
101 return r;
102 }