]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/cryptsetup/cryptsetup-tpm2.c
Merge pull request #25168 from valentindavid/valentindavid/umount-move-recursive...
[thirdparty/systemd.git] / src / cryptsetup / cryptsetup-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 "cryptsetup-tpm2.h"
6 #include "env-util.h"
7 #include "fileio.h"
8 #include "hexdecoct.h"
9 #include "json.h"
10 #include "parse-util.h"
11 #include "random-util.h"
12 #include "sha256.h"
13 #include "tpm2-util.h"
14
15 static int get_pin(usec_t until, AskPasswordFlags ask_password_flags, bool headless, char **ret_pin_str) {
16 _cleanup_free_ char *pin_str = NULL;
17 _cleanup_strv_free_erase_ char **pin = NULL;
18 int r;
19
20 assert(ret_pin_str);
21
22 r = getenv_steal_erase("PIN", &pin_str);
23 if (r < 0)
24 return log_error_errno(r, "Failed to acquire PIN from environment: %m");
25 if (!r) {
26 if (headless)
27 return log_error_errno(
28 SYNTHETIC_ERRNO(ENOPKG),
29 "PIN querying disabled via 'headless' option. "
30 "Use the '$PIN' environment variable.");
31
32 pin = strv_free_erase(pin);
33 r = ask_password_auto(
34 "Please enter TPM2 PIN:",
35 "drive-harddisk",
36 NULL,
37 "tpm2-pin",
38 "cryptsetup.tpm2-pin",
39 until,
40 ask_password_flags,
41 &pin);
42 if (r < 0)
43 return log_error_errno(r, "Failed to ask for user pin: %m");
44 assert(strv_length(pin) == 1);
45
46 pin_str = strdup(pin[0]);
47 if (!pin_str)
48 return log_oom();
49 }
50
51 *ret_pin_str = TAKE_PTR(pin_str);
52
53 return r;
54 }
55
56 int acquire_tpm2_key(
57 const char *volume_name,
58 const char *device,
59 uint32_t hash_pcr_mask,
60 uint16_t pcr_bank,
61 const void *pubkey,
62 size_t pubkey_size,
63 uint32_t pubkey_pcr_mask,
64 const char *signature_path,
65 uint16_t primary_alg,
66 const char *key_file,
67 size_t key_file_size,
68 uint64_t key_file_offset,
69 const void *key_data,
70 size_t key_data_size,
71 const void *policy_hash,
72 size_t policy_hash_size,
73 const void *salt,
74 size_t salt_size,
75 TPM2Flags flags,
76 usec_t until,
77 bool headless,
78 AskPasswordFlags ask_password_flags,
79 void **ret_decrypted_key,
80 size_t *ret_decrypted_key_size) {
81
82 _cleanup_(json_variant_unrefp) JsonVariant *signature_json = NULL;
83 _cleanup_free_ void *loaded_blob = NULL;
84 _cleanup_free_ char *auto_device = NULL;
85 size_t blob_size;
86 const void *blob;
87 int r;
88
89 if (!device) {
90 r = tpm2_find_device_auto(LOG_DEBUG, &auto_device);
91 if (r == -ENODEV)
92 return -EAGAIN; /* Tell the caller to wait for a TPM2 device to show up */
93 if (r < 0)
94 return r;
95
96 device = auto_device;
97 }
98
99 if (key_data) {
100 blob = key_data;
101 blob_size = key_data_size;
102 } else {
103 _cleanup_free_ char *bindname = NULL;
104
105 /* If we read the salt via AF_UNIX, make this client recognizable */
106 if (asprintf(&bindname, "@%" PRIx64"/cryptsetup-tpm2/%s", random_u64(), volume_name) < 0)
107 return log_oom();
108
109 r = read_full_file_full(
110 AT_FDCWD, key_file,
111 key_file_offset == 0 ? UINT64_MAX : key_file_offset,
112 key_file_size == 0 ? SIZE_MAX : key_file_size,
113 READ_FULL_FILE_CONNECT_SOCKET,
114 bindname,
115 (char**) &loaded_blob, &blob_size);
116 if (r < 0)
117 return r;
118
119 blob = loaded_blob;
120 }
121
122 if (pubkey_pcr_mask != 0) {
123 r = tpm2_load_pcr_signature(signature_path, &signature_json);
124 if (r < 0)
125 return r;
126 }
127
128 if (!(flags & TPM2_FLAGS_USE_PIN))
129 return tpm2_unseal(
130 device,
131 hash_pcr_mask,
132 pcr_bank,
133 pubkey, pubkey_size,
134 pubkey_pcr_mask,
135 signature_json,
136 /* pin= */ NULL,
137 primary_alg,
138 blob,
139 blob_size,
140 policy_hash,
141 policy_hash_size,
142 ret_decrypted_key,
143 ret_decrypted_key_size);
144
145 for (int i = 5;; i--) {
146 _cleanup_(erase_and_freep) char *pin_str = NULL, *b64_salted_pin = NULL;
147
148 if (i <= 0)
149 return -EACCES;
150
151 r = get_pin(until, ask_password_flags, headless, &pin_str);
152 if (r < 0)
153 return r;
154
155 if (salt) {
156 uint8_t salted_pin[SHA256_DIGEST_SIZE] = {};
157 CLEANUP_ERASE(salted_pin);
158
159 r = tpm2_util_pbkdf2_hmac_sha256(pin_str, strlen(pin_str), salt, salt_size, salted_pin);
160 if (r < 0)
161 return log_error_errno(r, "Failed to perform PBKDF2: %m");
162
163 r = base64mem(salted_pin, sizeof(salted_pin), &b64_salted_pin);
164 if (r < 0)
165 return log_error_errno(r, "Failed to base64 encode salted pin: %m");
166 } else
167 /* no salting needed, backwards compat with non-salted pins */
168 b64_salted_pin = TAKE_PTR(pin_str);
169
170 r = tpm2_unseal(device,
171 hash_pcr_mask,
172 pcr_bank,
173 pubkey, pubkey_size,
174 pubkey_pcr_mask,
175 signature_json,
176 b64_salted_pin,
177 primary_alg,
178 blob,
179 blob_size,
180 policy_hash,
181 policy_hash_size,
182 ret_decrypted_key,
183 ret_decrypted_key_size);
184 /* We get this error in case there is an authentication policy mismatch. This should
185 * not happen, but this avoids confusing behavior, just in case. */
186 if (IN_SET(r, -EPERM, -ENOLCK))
187 return r;
188 if (r < 0)
189 continue;
190
191 return r;
192 }
193 }
194
195 int find_tpm2_auto_data(
196 struct crypt_device *cd,
197 uint32_t search_pcr_mask,
198 int start_token,
199 uint32_t *ret_hash_pcr_mask,
200 uint16_t *ret_pcr_bank,
201 void **ret_pubkey,
202 size_t *ret_pubkey_size,
203 uint32_t *ret_pubkey_pcr_mask,
204 uint16_t *ret_primary_alg,
205 void **ret_blob,
206 size_t *ret_blob_size,
207 void **ret_policy_hash,
208 size_t *ret_policy_hash_size,
209 void **ret_salt,
210 size_t *ret_salt_size,
211 TPM2Flags *ret_flags,
212 int *ret_keyslot,
213 int *ret_token) {
214
215 int r, token;
216
217 assert(cd);
218
219 for (token = start_token; token < sym_crypt_token_max(CRYPT_LUKS2); token++) {
220 _cleanup_free_ void *blob = NULL, *policy_hash = NULL, *pubkey = NULL, *salt = NULL;
221 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
222 size_t blob_size, policy_hash_size, pubkey_size, salt_size = 0;
223 uint32_t hash_pcr_mask, pubkey_pcr_mask;
224 uint16_t pcr_bank, primary_alg;
225 TPM2Flags flags;
226 int keyslot;
227
228 r = cryptsetup_get_token_as_json(cd, token, "systemd-tpm2", &v);
229 if (IN_SET(r, -ENOENT, -EINVAL, -EMEDIUMTYPE))
230 continue;
231 if (r < 0)
232 return log_error_errno(r, "Failed to read JSON token data off disk: %m");
233
234 r = tpm2_parse_luks2_json(
235 v,
236 &keyslot,
237 &hash_pcr_mask,
238 &pcr_bank,
239 &pubkey, &pubkey_size,
240 &pubkey_pcr_mask,
241 &primary_alg,
242 &blob, &blob_size,
243 &policy_hash, &policy_hash_size,
244 &salt, &salt_size,
245 &flags);
246 if (r == -EUCLEAN) /* Gracefully handle issues in JSON fields not owned by us */
247 continue;
248 if (r < 0)
249 return log_error_errno(r, "Failed to parse TPM2 JSON data: %m");
250
251 if (search_pcr_mask == UINT32_MAX ||
252 search_pcr_mask == hash_pcr_mask) {
253
254 if (start_token <= 0)
255 log_info("Automatically discovered security TPM2 token unlocks volume.");
256
257 *ret_hash_pcr_mask = hash_pcr_mask;
258 *ret_pcr_bank = pcr_bank;
259 *ret_pubkey = TAKE_PTR(pubkey);
260 *ret_pubkey_size = pubkey_size;
261 *ret_pubkey_pcr_mask = pubkey_pcr_mask;
262 *ret_primary_alg = primary_alg;
263 *ret_blob = TAKE_PTR(blob);
264 *ret_blob_size = blob_size;
265 *ret_policy_hash = TAKE_PTR(policy_hash);
266 *ret_policy_hash_size = policy_hash_size;
267 *ret_salt = TAKE_PTR(salt);
268 *ret_salt_size = salt_size;
269 *ret_keyslot = keyslot;
270 *ret_token = token;
271 *ret_flags = flags;
272 return 0;
273 }
274
275 /* PCR mask doesn't match what is configured, ignore this entry, let's see next */
276 }
277
278 return log_error_errno(SYNTHETIC_ERRNO(ENXIO), "No valid TPM2 token data found.");
279 }