]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/cryptsetup-util.c
Merge pull request #25385 from drvink/main
[thirdparty/systemd.git] / src / shared / cryptsetup-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "alloc-util.h"
4 #include "cryptsetup-util.h"
5 #include "dlfcn-util.h"
6 #include "log.h"
7 #include "parse-util.h"
8
9 #if HAVE_LIBCRYPTSETUP
10 static void *cryptsetup_dl = NULL;
11
12 int (*sym_crypt_activate_by_passphrase)(struct crypt_device *cd, const char *name, int keyslot, const char *passphrase, size_t passphrase_size, uint32_t flags);
13 #if HAVE_CRYPT_ACTIVATE_BY_SIGNED_KEY
14 int (*sym_crypt_activate_by_signed_key)(struct crypt_device *cd, const char *name, const char *volume_key, size_t volume_key_size, const char *signature, size_t signature_size, uint32_t flags);
15 #endif
16 int (*sym_crypt_activate_by_volume_key)(struct crypt_device *cd, const char *name, const char *volume_key, size_t volume_key_size, uint32_t flags);
17 int (*sym_crypt_deactivate_by_name)(struct crypt_device *cd, const char *name, uint32_t flags);
18 int (*sym_crypt_format)(struct crypt_device *cd, const char *type, const char *cipher, const char *cipher_mode, const char *uuid, const char *volume_key, size_t volume_key_size, void *params);
19 void (*sym_crypt_free)(struct crypt_device *cd);
20 const char *(*sym_crypt_get_cipher)(struct crypt_device *cd);
21 const char *(*sym_crypt_get_cipher_mode)(struct crypt_device *cd);
22 uint64_t (*sym_crypt_get_data_offset)(struct crypt_device *cd);
23 const char *(*sym_crypt_get_device_name)(struct crypt_device *cd);
24 const char *(*sym_crypt_get_dir)(void);
25 const char *(*sym_crypt_get_type)(struct crypt_device *cd);
26 const char *(*sym_crypt_get_uuid)(struct crypt_device *cd);
27 int (*sym_crypt_get_verity_info)(struct crypt_device *cd, struct crypt_params_verity *vp);
28 int (*sym_crypt_get_volume_key_size)(struct crypt_device *cd);
29 int (*sym_crypt_init)(struct crypt_device **cd, const char *device);
30 int (*sym_crypt_init_by_name)(struct crypt_device **cd, const char *name);
31 int (*sym_crypt_keyslot_add_by_volume_key)(struct crypt_device *cd, int keyslot, const char *volume_key, size_t volume_key_size, const char *passphrase, size_t passphrase_size);
32 int (*sym_crypt_keyslot_destroy)(struct crypt_device *cd, int keyslot);
33 int (*sym_crypt_keyslot_max)(const char *type);
34 int (*sym_crypt_load)(struct crypt_device *cd, const char *requested_type, void *params);
35 int (*sym_crypt_resize)(struct crypt_device *cd, const char *name, uint64_t new_size);
36 int (*sym_crypt_resume_by_passphrase)(struct crypt_device *cd, const char *name, int keyslot, const char *passphrase, size_t passphrase_size);
37 int (*sym_crypt_set_data_device)(struct crypt_device *cd, const char *device);
38 void (*sym_crypt_set_debug_level)(int level);
39 void (*sym_crypt_set_log_callback)(struct crypt_device *cd, void (*log)(int level, const char *msg, void *usrptr), void *usrptr);
40 #if HAVE_CRYPT_SET_METADATA_SIZE
41 int (*sym_crypt_set_metadata_size)(struct crypt_device *cd, uint64_t metadata_size, uint64_t keyslots_size);
42 #endif
43 int (*sym_crypt_set_pbkdf_type)(struct crypt_device *cd, const struct crypt_pbkdf_type *pbkdf);
44 int (*sym_crypt_suspend)(struct crypt_device *cd, const char *name);
45 int (*sym_crypt_token_json_get)(struct crypt_device *cd, int token, const char **json);
46 int (*sym_crypt_token_json_set)(struct crypt_device *cd, int token, const char *json);
47 #if HAVE_CRYPT_TOKEN_MAX
48 int (*sym_crypt_token_max)(const char *type);
49 #endif
50 crypt_token_info (*sym_crypt_token_status)(struct crypt_device *cd, int token, const char **type);
51 int (*sym_crypt_volume_key_get)(struct crypt_device *cd, int keyslot, char *volume_key, size_t *volume_key_size, const char *passphrase, size_t passphrase_size);
52 #if HAVE_CRYPT_REENCRYPT_INIT_BY_PASSPHRASE
53 int (*sym_crypt_reencrypt_init_by_passphrase)(struct crypt_device *cd, const char *name, const char *passphrase, size_t passphrase_size, int keyslot_old, int keyslot_new, const char *cipher, const char *cipher_mode, const struct crypt_params_reencrypt *params);
54 #endif
55 #if HAVE_CRYPT_REENCRYPT
56 int (*sym_crypt_reencrypt)(struct crypt_device *cd, int (*progress)(uint64_t size, uint64_t offset, void *usrptr));
57 #endif
58 int (*sym_crypt_metadata_locking)(struct crypt_device *cd, int enable);
59 #if HAVE_CRYPT_SET_DATA_OFFSET
60 int (*sym_crypt_set_data_offset)(struct crypt_device *cd, uint64_t data_offset);
61 #endif
62 int (*sym_crypt_header_restore)(struct crypt_device *cd, const char *requested_type, const char *backup_file);
63 int (*sym_crypt_volume_key_keyring)(struct crypt_device *cd, int enable);
64
65 static void cryptsetup_log_glue(int level, const char *msg, void *usrptr) {
66
67 switch (level) {
68 case CRYPT_LOG_NORMAL:
69 level = LOG_NOTICE;
70 break;
71 case CRYPT_LOG_ERROR:
72 level = LOG_ERR;
73 break;
74 case CRYPT_LOG_VERBOSE:
75 level = LOG_INFO;
76 break;
77 case CRYPT_LOG_DEBUG:
78 level = LOG_DEBUG;
79 break;
80 default:
81 log_error("Unknown libcryptsetup log level: %d", level);
82 level = LOG_ERR;
83 }
84
85 log_full(level, "%s", msg);
86 }
87
88 void cryptsetup_enable_logging(struct crypt_device *cd) {
89 /* It's OK to call this with a NULL parameter, in which case libcryptsetup will set the default log
90 * function.
91 *
92 * Note that this is also called from dlopen_cryptsetup(), which we call here too. Sounds like an
93 * endless loop, but isn't because we break it via the check for 'cryptsetup_dl' early in
94 * dlopen_cryptsetup(). */
95
96 if (dlopen_cryptsetup() < 0)
97 return; /* If this fails, let's gracefully ignore the issue, this is just debug logging after
98 * all, and if this failed we already generated a debug log message that should help
99 * to track things down. */
100
101 sym_crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
102 sym_crypt_set_debug_level(DEBUG_LOGGING ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
103 }
104
105 int cryptsetup_set_minimal_pbkdf(struct crypt_device *cd) {
106
107 /* With CRYPT_PBKDF_NO_BENCHMARK flag set .time_ms member is ignored
108 * while .iterations must be set at least to recommended minimum value. */
109
110 static const struct crypt_pbkdf_type minimal_pbkdf = {
111 .hash = "sha512",
112 .type = CRYPT_KDF_PBKDF2,
113 .iterations = 1000, /* recommended minimum count for pbkdf2
114 * according to NIST SP 800-132, ch. 5.2 */
115 .flags = CRYPT_PBKDF_NO_BENCHMARK
116 };
117
118 int r;
119
120 /* Sets a minimal PKBDF in case we already have a high entropy key. */
121
122 r = dlopen_cryptsetup();
123 if (r < 0)
124 return r;
125
126 r = sym_crypt_set_pbkdf_type(cd, &minimal_pbkdf);
127 if (r < 0)
128 return r;
129
130 return 0;
131 }
132
133 int cryptsetup_get_token_as_json(
134 struct crypt_device *cd,
135 int idx,
136 const char *verify_type,
137 JsonVariant **ret) {
138
139 _cleanup_(json_variant_unrefp) JsonVariant *v = NULL;
140 const char *text;
141 int r;
142
143 assert(cd);
144
145 /* Extracts and parses the LUKS2 JSON token data from a LUKS2 device. Optionally verifies the type of
146 * the token. Returns:
147 *
148 * -EINVAL → token index out of range or "type" field missing
149 * -ENOENT → token doesn't exist
150 * -EMEDIUMTYPE → "verify_type" specified and doesn't match token's type
151 */
152
153 r = dlopen_cryptsetup();
154 if (r < 0)
155 return r;
156
157 r = sym_crypt_token_json_get(cd, idx, &text);
158 if (r < 0)
159 return r;
160
161 r = json_parse(text, 0, &v, NULL, NULL);
162 if (r < 0)
163 return r;
164
165 if (verify_type) {
166 JsonVariant *w;
167
168 w = json_variant_by_key(v, "type");
169 if (!w)
170 return -EINVAL;
171
172 if (!streq_ptr(json_variant_string(w), verify_type))
173 return -EMEDIUMTYPE;
174 }
175
176 if (ret)
177 *ret = TAKE_PTR(v);
178
179 return 0;
180 }
181
182 int cryptsetup_add_token_json(struct crypt_device *cd, JsonVariant *v) {
183 _cleanup_free_ char *text = NULL;
184 int r;
185
186 r = dlopen_cryptsetup();
187 if (r < 0)
188 return r;
189
190 r = json_variant_format(v, 0, &text);
191 if (r < 0)
192 return log_debug_errno(r, "Failed to format token data for LUKS: %m");
193
194 log_debug("Adding token text <%s>", text);
195
196 r = sym_crypt_token_json_set(cd, CRYPT_ANY_TOKEN, text);
197 if (r < 0)
198 return log_debug_errno(r, "Failed to write token data to LUKS: %m");
199
200 return 0;
201 }
202 #endif
203
204 int dlopen_cryptsetup(void) {
205 #if HAVE_LIBCRYPTSETUP
206 int r;
207
208 /* libcryptsetup added crypt_reencrypt() in 2.2.0, and marked it obsolete in 2.4.0, replacing it with
209 * crypt_reencrypt_run(), which takes one extra argument but is otherwise identical. The old call is
210 * still available though, and given we want to support 2.2.0 for a while longer, we'll stick to the
211 * old symbol. Howerver, the old symbols now has a GCC deprecation decorator, hence let's turn off
212 * warnings about this for now. */
213
214 DISABLE_WARNING_DEPRECATED_DECLARATIONS;
215
216 r = dlopen_many_sym_or_warn(
217 &cryptsetup_dl, "libcryptsetup.so.12", LOG_DEBUG,
218 DLSYM_ARG(crypt_activate_by_passphrase),
219 #if HAVE_CRYPT_ACTIVATE_BY_SIGNED_KEY
220 DLSYM_ARG(crypt_activate_by_signed_key),
221 #endif
222 DLSYM_ARG(crypt_activate_by_volume_key),
223 DLSYM_ARG(crypt_deactivate_by_name),
224 DLSYM_ARG(crypt_format),
225 DLSYM_ARG(crypt_free),
226 DLSYM_ARG(crypt_get_cipher),
227 DLSYM_ARG(crypt_get_cipher_mode),
228 DLSYM_ARG(crypt_get_data_offset),
229 DLSYM_ARG(crypt_get_device_name),
230 DLSYM_ARG(crypt_get_dir),
231 DLSYM_ARG(crypt_get_type),
232 DLSYM_ARG(crypt_get_uuid),
233 DLSYM_ARG(crypt_get_verity_info),
234 DLSYM_ARG(crypt_get_volume_key_size),
235 DLSYM_ARG(crypt_init),
236 DLSYM_ARG(crypt_init_by_name),
237 DLSYM_ARG(crypt_keyslot_add_by_volume_key),
238 DLSYM_ARG(crypt_keyslot_destroy),
239 DLSYM_ARG(crypt_keyslot_max),
240 DLSYM_ARG(crypt_load),
241 DLSYM_ARG(crypt_resize),
242 DLSYM_ARG(crypt_resume_by_passphrase),
243 DLSYM_ARG(crypt_set_data_device),
244 DLSYM_ARG(crypt_set_debug_level),
245 DLSYM_ARG(crypt_set_log_callback),
246 #if HAVE_CRYPT_SET_METADATA_SIZE
247 DLSYM_ARG(crypt_set_metadata_size),
248 #endif
249 DLSYM_ARG(crypt_set_pbkdf_type),
250 DLSYM_ARG(crypt_suspend),
251 DLSYM_ARG(crypt_token_json_get),
252 DLSYM_ARG(crypt_token_json_set),
253 #if HAVE_CRYPT_TOKEN_MAX
254 DLSYM_ARG(crypt_token_max),
255 #endif
256 DLSYM_ARG(crypt_token_status),
257 DLSYM_ARG(crypt_volume_key_get),
258 #if HAVE_CRYPT_REENCRYPT_INIT_BY_PASSPHRASE
259 DLSYM_ARG(crypt_reencrypt_init_by_passphrase),
260 #endif
261 #if HAVE_CRYPT_REENCRYPT
262 DLSYM_ARG(crypt_reencrypt),
263 #endif
264 DLSYM_ARG(crypt_metadata_locking),
265 #if HAVE_CRYPT_SET_DATA_OFFSET
266 DLSYM_ARG(crypt_set_data_offset),
267 #endif
268 DLSYM_ARG(crypt_header_restore),
269 DLSYM_ARG(crypt_volume_key_keyring));
270 if (r <= 0)
271 return r;
272
273 REENABLE_WARNING;
274
275 /* Redirect the default logging calls of libcryptsetup to our own logging infra. (Note that
276 * libcryptsetup also maintains per-"struct crypt_device" log functions, which we'll also set
277 * whenever allocating a "struct crypt_device" context. Why set both? To be defensive: maybe some
278 * other code loaded into this process also changes the global log functions of libcryptsetup, who
279 * knows? And if so, we still want our own objects to log via our own infra, at the very least.) */
280 cryptsetup_enable_logging(NULL);
281 return 1;
282 #else
283 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "cryptsetup support is not compiled in.");
284 #endif
285 }
286
287 int cryptsetup_get_keyslot_from_token(JsonVariant *v) {
288 int keyslot, r;
289 JsonVariant *w;
290
291 /* Parses the "keyslots" field of a LUKS2 token object. The field can be an array, but here we assume
292 * that it contains a single element only, since that's the only way we ever generate it
293 * ourselves. */
294
295 w = json_variant_by_key(v, "keyslots");
296 if (!w)
297 return -ENOENT;
298 if (!json_variant_is_array(w) || json_variant_elements(w) != 1)
299 return -EMEDIUMTYPE;
300
301 w = json_variant_by_index(w, 0);
302 if (!w)
303 return -ENOENT;
304 if (!json_variant_is_string(w))
305 return -EMEDIUMTYPE;
306
307 r = safe_atoi(json_variant_string(w), &keyslot);
308 if (r < 0)
309 return r;
310 if (keyslot < 0)
311 return -EINVAL;
312
313 return keyslot;
314 }