]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/cryptenroll/cryptenroll-tpm2.c
tree-wide: fix return value handling of base64mem()
[thirdparty/systemd.git] / src / cryptenroll / cryptenroll-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 "cryptenroll-tpm2.h"
6 #include "env-util.h"
7 #include "fileio.h"
8 #include "hexdecoct.h"
9 #include "json.h"
10 #include "memory-util.h"
11 #include "tpm2-util.h"
12
13 static int search_policy_hash(
14 struct crypt_device *cd,
15 const void *hash,
16 size_t hash_size) {
17
18 int r;
19
20 assert(cd);
21 assert(hash || hash_size == 0);
22
23 if (hash_size == 0)
24 return 0;
25
26 for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token ++) {
27 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
28 _cleanup_free_ void *thash = NULL;
29 size_t thash_size = 0;
30 int keyslot;
31 JsonVariant *w;
32
33 r = cryptsetup_get_token_as_json(cd, token, "systemd-tpm2", &v);
34 if (IN_SET(r, -ENOENT, -EINVAL, -EMEDIUMTYPE))
35 continue;
36 if (r < 0)
37 return log_error_errno(r, "Failed to read JSON token data off disk: %m");
38
39 keyslot = cryptsetup_get_keyslot_from_token(v);
40 if (keyslot < 0) {
41 /* Handle parsing errors of the keyslots field gracefully, since it's not 'owned' by
42 * us, but by the LUKS2 spec */
43 log_warning_errno(keyslot, "Failed to determine keyslot of JSON token %i, skipping: %m", token);
44 continue;
45 }
46
47 w = json_variant_by_key(v, "tpm2-policy-hash");
48 if (!w || !json_variant_is_string(w))
49 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
50 "TPM2 token data lacks 'tpm2-policy-hash' field.");
51
52 r = unhexmem(json_variant_string(w), SIZE_MAX, &thash, &thash_size);
53 if (r < 0)
54 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
55 "Invalid base64 data in 'tpm2-policy-hash' field.");
56
57 if (memcmp_nn(hash, hash_size, thash, thash_size) == 0)
58 return keyslot; /* Found entry with same hash. */
59 }
60
61 return -ENOENT; /* Not found */
62 }
63
64 static int get_pin(char **ret_pin_str, TPM2Flags *ret_flags) {
65 _cleanup_free_ char *pin_str = NULL;
66 int r;
67 TPM2Flags flags = 0;
68
69 assert(ret_pin_str);
70 assert(ret_flags);
71
72 r = getenv_steal_erase("NEWPIN", &pin_str);
73 if (r < 0)
74 return log_error_errno(r, "Failed to acquire PIN from environment: %m");
75 if (r > 0)
76 flags |= TPM2_FLAGS_USE_PIN;
77 else {
78 for (size_t i = 5;; i--) {
79 _cleanup_strv_free_erase_ char **pin = NULL, **pin2 = NULL;
80
81 if (i <= 0)
82 return log_error_errno(
83 SYNTHETIC_ERRNO(ENOKEY), "Too many attempts, giving up.");
84
85 pin = strv_free_erase(pin);
86 r = ask_password_auto(
87 "Please enter TPM2 PIN:",
88 "drive-harddisk",
89 NULL,
90 "tpm2-pin",
91 "cryptenroll.tpm2-pin",
92 USEC_INFINITY,
93 0,
94 &pin);
95 if (r < 0)
96 return log_error_errno(r, "Failed to ask for user pin: %m");
97 assert(strv_length(pin) == 1);
98
99 r = ask_password_auto(
100 "Please enter TPM2 PIN (repeat):",
101 "drive-harddisk",
102 NULL,
103 "tpm2-pin",
104 "cryptenroll.tpm2-pin",
105 USEC_INFINITY,
106 0,
107 &pin2);
108 if (r < 0)
109 return log_error_errno(r, "Failed to ask for user pin: %m");
110 assert(strv_length(pin) == 1);
111
112 if (strv_equal(pin, pin2)) {
113 pin_str = strdup(*pin);
114 if (!pin_str)
115 return log_oom();
116 flags |= TPM2_FLAGS_USE_PIN;
117 break;
118 }
119
120 log_error("PINs didn't match, please try again!");
121 }
122 }
123
124 *ret_flags = flags;
125 *ret_pin_str = TAKE_PTR(pin_str);
126
127 return 0;
128 }
129
130 int enroll_tpm2(struct crypt_device *cd,
131 const void *volume_key,
132 size_t volume_key_size,
133 const char *device,
134 uint32_t hash_pcr_mask,
135 const char *pubkey_path,
136 uint32_t pubkey_pcr_mask,
137 const char *signature_path,
138 bool use_pin) {
139
140 _cleanup_(erase_and_freep) void *secret = NULL;
141 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *signature_json = NULL;
142 _cleanup_(erase_and_freep) char *base64_encoded = NULL;
143 size_t secret_size, blob_size, hash_size, pubkey_size = 0;
144 _cleanup_free_ void *blob = NULL, *hash = NULL, *pubkey = NULL;
145 uint16_t pcr_bank, primary_alg;
146 const char *node;
147 _cleanup_(erase_and_freep) char *pin_str = NULL;
148 ssize_t base64_encoded_size;
149 int r, keyslot;
150 TPM2Flags flags = 0;
151
152 assert(cd);
153 assert(volume_key);
154 assert(volume_key_size > 0);
155 assert(TPM2_PCR_MASK_VALID(hash_pcr_mask));
156 assert(TPM2_PCR_MASK_VALID(pubkey_pcr_mask));
157
158 assert_se(node = crypt_get_device_name(cd));
159
160 if (use_pin) {
161 r = get_pin(&pin_str, &flags);
162 if (r < 0)
163 return r;
164 }
165
166 r = tpm2_load_pcr_public_key(pubkey_path, &pubkey, &pubkey_size);
167 if (r < 0) {
168 if (pubkey_path || signature_path || r != -ENOENT)
169 return log_error_errno(r, "Failed read TPM PCR public key: %m");
170
171 log_debug_errno(r, "Failed to read TPM2 PCR public key, proceeding without: %m");
172 pubkey_pcr_mask = 0;
173 } else {
174 /* Also try to load the signature JSON object, to verify that our enrollment will work. This is optional however. */
175
176 r = tpm2_load_pcr_signature(signature_path, &signature_json);
177 if (r < 0) {
178 if (signature_path || r != -ENOENT)
179 return log_debug_errno(r, "Failed to read TPM PCR signature: %m");
180
181 log_debug_errno(r, "Failed to read TPM2 PCR signature, proceeding without: %m");
182 }
183 }
184
185 r = tpm2_seal(device,
186 hash_pcr_mask,
187 pubkey, pubkey_size,
188 pubkey_pcr_mask,
189 pin_str,
190 &secret, &secret_size,
191 &blob, &blob_size,
192 &hash, &hash_size,
193 &pcr_bank,
194 &primary_alg);
195 if (r < 0)
196 return r;
197
198 /* Let's see if we already have this specific PCR policy hash enrolled, if so, exit early. */
199 r = search_policy_hash(cd, hash, hash_size);
200 if (r == -ENOENT)
201 log_debug_errno(r, "PCR policy hash not yet enrolled, enrolling now.");
202 else if (r < 0)
203 return r;
204 else {
205 log_info("This PCR set is already enrolled, executing no operation.");
206 return r; /* return existing keyslot, so that wiping won't kill it */
207 }
208
209 /* Quick verification that everything is in order, we are not in a hurry after all.*/
210 if (!pubkey || signature_json) {
211 _cleanup_(erase_and_freep) void *secret2 = NULL;
212 size_t secret2_size;
213
214 log_debug("Unsealing for verification...");
215 r = tpm2_unseal(device,
216 hash_pcr_mask,
217 pcr_bank,
218 pubkey, pubkey_size,
219 pubkey_pcr_mask,
220 signature_json,
221 pin_str,
222 primary_alg,
223 blob, blob_size,
224 hash, hash_size,
225 &secret2, &secret2_size);
226 if (r < 0)
227 return r;
228
229 if (memcmp_nn(secret, secret_size, secret2, secret2_size) != 0)
230 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE), "TPM2 seal/unseal verification failed.");
231 }
232
233 /* let's base64 encode the key to use, for compat with homed (and it's easier to every type it in by keyboard, if that might end up being necessary. */
234 base64_encoded_size = base64mem(secret, secret_size, &base64_encoded);
235 if (base64_encoded_size < 0)
236 return log_error_errno(base64_encoded_size, "Failed to base64 encode secret key: %m");
237
238 r = cryptsetup_set_minimal_pbkdf(cd);
239 if (r < 0)
240 return log_error_errno(r, "Failed to set minimal PBKDF: %m");
241
242 keyslot = crypt_keyslot_add_by_volume_key(
243 cd,
244 CRYPT_ANY_SLOT,
245 volume_key,
246 volume_key_size,
247 base64_encoded,
248 base64_encoded_size);
249 if (keyslot < 0)
250 return log_error_errno(keyslot, "Failed to add new TPM2 key to %s: %m", node);
251
252 r = tpm2_make_luks2_json(
253 keyslot,
254 hash_pcr_mask,
255 pcr_bank,
256 pubkey, pubkey_size,
257 pubkey_pcr_mask,
258 primary_alg,
259 blob, blob_size,
260 hash, hash_size,
261 flags,
262 &v);
263 if (r < 0)
264 return log_error_errno(r, "Failed to prepare TPM2 JSON token object: %m");
265
266 r = cryptsetup_add_token_json(cd, v);
267 if (r < 0)
268 return log_error_errno(r, "Failed to add TPM2 JSON token to LUKS2 header: %m");
269
270 log_info("New TPM2 token enrolled as key slot %i.", keyslot);
271 return keyslot;
272 }