2 * LUKS - Linux Unified Key Setup v2, keyslot handling
4 * Copyright (C) 2015-2023 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2015-2023 Milan Broz
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "luks2_internal.h"
24 /* Internal implementations */
25 extern const keyslot_handler luks2_keyslot;
26 extern const keyslot_handler reenc_keyslot;
28 static const keyslot_handler *keyslot_handlers[LUKS2_KEYSLOTS_MAX] = {
30 #if USE_LUKS2_REENCRYPTION
36 static const keyslot_handler
37 *LUKS2_keyslot_handler_type(const char *type)
41 for (i = 0; i < LUKS2_KEYSLOTS_MAX && keyslot_handlers[i]; i++) {
42 if (!strcmp(keyslot_handlers[i]->name, type))
43 return keyslot_handlers[i];
49 static const keyslot_handler
50 *LUKS2_keyslot_handler(struct crypt_device *cd, int keyslot)
52 struct luks2_hdr *hdr;
53 json_object *jobj1, *jobj2;
58 if (!(hdr = crypt_get_hdr(cd, CRYPT_LUKS2)))
61 if (!(jobj1 = LUKS2_get_keyslot_jobj(hdr, keyslot)))
64 if (!json_object_object_get_ex(jobj1, "type", &jobj2))
67 return LUKS2_keyslot_handler_type(json_object_get_string(jobj2));
70 int LUKS2_keyslot_find_empty(struct crypt_device *cd, struct luks2_hdr *hdr, size_t keylength)
74 for (i = 0; i < LUKS2_KEYSLOTS_MAX; i++)
75 if (!LUKS2_get_keyslot_jobj(hdr, i))
78 if (i == LUKS2_KEYSLOTS_MAX)
81 /* Check also there is a space for the key in keyslots area */
82 if (keylength && LUKS2_find_area_gap(cd, hdr, keylength, NULL, NULL) < 0)
88 /* Check if a keyslot is assigned to specific segment */
89 static int _keyslot_for_segment(struct luks2_hdr *hdr, int keyslot, int segment)
91 int keyslot_digest, count = 0;
94 keyslot_digest = LUKS2_digest_by_keyslot(hdr, keyslot);
95 if (keyslot_digest < 0)
96 return keyslot_digest;
99 return keyslot_digest == LUKS2_digest_by_segment(hdr, segment);
101 for (s = 0; s < json_segments_count(LUKS2_get_segments_jobj(hdr)); s++) {
102 if (keyslot_digest == LUKS2_digest_by_segment(hdr, s))
109 static int _keyslot_for_digest(struct luks2_hdr *hdr, int keyslot, int digest)
113 r = LUKS2_digest_by_keyslot(hdr, keyslot);
116 return r == digest ? 0 : -ENOENT;
119 int LUKS2_keyslot_for_segment(struct luks2_hdr *hdr, int keyslot, int segment)
123 /* no need to check anything */
124 if (segment == CRYPT_ANY_SEGMENT)
126 if (segment == CRYPT_DEFAULT_SEGMENT) {
127 segment = LUKS2_get_default_segment(hdr);
132 r = _keyslot_for_segment(hdr, keyslot, segment);
136 return r >= 1 ? 0 : -ENOENT;
139 /* Number of keyslots assigned to a segment or all keyslots for CRYPT_ANY_SEGMENT */
140 int LUKS2_keyslot_active_count(struct luks2_hdr *hdr, int segment)
143 json_object *jobj_keyslots;
145 json_object_object_get_ex(hdr->jobj, "keyslots", &jobj_keyslots);
147 json_object_object_foreach(jobj_keyslots, slot, val) {
149 if (!LUKS2_keyslot_for_segment(hdr, atoi(slot), segment))
156 int LUKS2_keyslot_cipher_incompatible(struct crypt_device *cd, const char *cipher_spec)
158 char cipher[MAX_CIPHER_LEN], cipher_mode[MAX_CIPHER_LEN];
160 if (!cipher_spec || crypt_is_cipher_null(cipher_spec))
163 if (crypt_parse_name_and_mode(cipher_spec, cipher, NULL, cipher_mode) < 0)
166 /* Keyslot is already authenticated; we cannot use integrity tags here */
167 if (crypt_get_integrity_tag_size(cd))
170 /* Wrapped key schemes cannot be used for keyslot encryption */
171 if (crypt_cipher_wrapped_key(cipher, cipher_mode))
174 /* Check if crypto backend can use the cipher */
175 if (crypt_cipher_ivsize(cipher, cipher_mode) < 0)
181 int LUKS2_keyslot_params_default(struct crypt_device *cd, struct luks2_hdr *hdr,
182 struct luks2_keyslot_params *params)
184 const struct crypt_pbkdf_type *pbkdf = crypt_get_pbkdf_type(cd);
185 const char *cipher_spec;
189 if (!hdr || !pbkdf || !params)
193 * set keyslot area encryption parameters
195 params->area_type = LUKS2_KEYSLOT_AREA_RAW;
196 cipher_spec = crypt_keyslot_get_encryption(cd, CRYPT_ANY_SLOT, &key_size);
197 if (!cipher_spec || !key_size)
200 params->area.raw.key_size = key_size;
201 r = snprintf(params->area.raw.encryption, sizeof(params->area.raw.encryption), "%s", cipher_spec);
202 if (r < 0 || (size_t)r >= sizeof(params->area.raw.encryption))
206 * set keyslot AF parameters
208 params->af_type = LUKS2_KEYSLOT_AF_LUKS1;
209 /* currently we use hash for AF from pbkdf settings */
210 r = snprintf(params->af.luks1.hash, sizeof(params->af.luks1.hash), "%s", pbkdf->hash ?: DEFAULT_LUKS1_HASH);
211 if (r < 0 || (size_t)r >= sizeof(params->af.luks1.hash))
213 params->af.luks1.stripes = 4000;
218 int LUKS2_keyslot_pbkdf(struct luks2_hdr *hdr, int keyslot, struct crypt_pbkdf_type *pbkdf)
220 json_object *jobj_keyslot, *jobj_kdf, *jobj;
225 if (LUKS2_keyslot_info(hdr, keyslot) == CRYPT_SLOT_INVALID)
228 jobj_keyslot = LUKS2_get_keyslot_jobj(hdr, keyslot);
232 if (!json_object_object_get_ex(jobj_keyslot, "kdf", &jobj_kdf))
235 if (!json_object_object_get_ex(jobj_kdf, "type", &jobj))
238 memset(pbkdf, 0, sizeof(*pbkdf));
240 pbkdf->type = json_object_get_string(jobj);
241 if (json_object_object_get_ex(jobj_kdf, "hash", &jobj))
242 pbkdf->hash = json_object_get_string(jobj);
243 if (json_object_object_get_ex(jobj_kdf, "iterations", &jobj))
244 pbkdf->iterations = json_object_get_int(jobj);
245 if (json_object_object_get_ex(jobj_kdf, "time", &jobj))
246 pbkdf->iterations = json_object_get_int(jobj);
247 if (json_object_object_get_ex(jobj_kdf, "memory", &jobj))
248 pbkdf->max_memory_kb = json_object_get_int(jobj);
249 if (json_object_object_get_ex(jobj_kdf, "cpus", &jobj))
250 pbkdf->parallel_threads = json_object_get_int(jobj);
255 static int LUKS2_keyslot_unbound(struct luks2_hdr *hdr, int keyslot)
257 json_object *jobj_digest, *jobj_segments;
258 int digest = LUKS2_digest_by_keyslot(hdr, keyslot);
263 if (!(jobj_digest = LUKS2_get_digest_jobj(hdr, digest)))
266 json_object_object_get_ex(jobj_digest, "segments", &jobj_segments);
267 if (!jobj_segments || !json_object_is_type(jobj_segments, json_type_array) ||
268 json_object_array_length(jobj_segments) == 0)
274 crypt_keyslot_info LUKS2_keyslot_info(struct luks2_hdr *hdr, int keyslot)
276 if(keyslot >= LUKS2_KEYSLOTS_MAX || keyslot < 0)
277 return CRYPT_SLOT_INVALID;
279 if (!LUKS2_get_keyslot_jobj(hdr, keyslot))
280 return CRYPT_SLOT_INACTIVE;
282 if (LUKS2_digest_by_keyslot(hdr, keyslot) < 0 ||
283 LUKS2_keyslot_unbound(hdr, keyslot))
284 return CRYPT_SLOT_UNBOUND;
286 if (LUKS2_keyslot_active_count(hdr, CRYPT_DEFAULT_SEGMENT) == 1 &&
287 !LUKS2_keyslot_for_segment(hdr, keyslot, CRYPT_DEFAULT_SEGMENT))
288 return CRYPT_SLOT_ACTIVE_LAST;
290 return CRYPT_SLOT_ACTIVE;
293 int LUKS2_keyslot_jobj_area(json_object *jobj_keyslot, uint64_t *offset, uint64_t *length)
295 json_object *jobj_area, *jobj;
297 if (!json_object_object_get_ex(jobj_keyslot, "area", &jobj_area))
300 if (!json_object_object_get_ex(jobj_area, "offset", &jobj))
302 *offset = crypt_jobj_get_uint64(jobj);
304 if (!json_object_object_get_ex(jobj_area, "size", &jobj))
306 *length = crypt_jobj_get_uint64(jobj);
311 int LUKS2_keyslot_area(struct luks2_hdr *hdr,
316 json_object *jobj_keyslot;
318 if (LUKS2_keyslot_info(hdr, keyslot) == CRYPT_SLOT_INVALID)
321 jobj_keyslot = LUKS2_get_keyslot_jobj(hdr, keyslot);
325 return LUKS2_keyslot_jobj_area(jobj_keyslot, offset, length);
328 static int _open_and_verify(struct crypt_device *cd,
329 struct luks2_hdr *hdr,
330 const keyslot_handler *h,
332 const char *password,
334 struct volume_key **vk)
336 int r, key_size = LUKS2_get_keyslot_stored_key_size(hdr, keyslot);
341 *vk = crypt_alloc_volume_key(key_size, NULL);
345 r = h->open(cd, keyslot, password, password_len, (*vk)->key, (*vk)->keylength);
347 log_dbg(cd, "Keyslot %d (%s) open failed with %d.", keyslot, h->name, r);
349 r = LUKS2_digest_verify(cd, hdr, *vk, keyslot);
352 crypt_free_volume_key(*vk);
356 crypt_volume_key_set_id(*vk, r);
358 return r < 0 ? r : keyslot;
361 static int LUKS2_open_and_verify_by_digest(struct crypt_device *cd,
362 struct luks2_hdr *hdr,
365 const char *password,
367 struct volume_key **vk)
369 const keyslot_handler *h;
372 if (!(h = LUKS2_keyslot_handler(cd, keyslot)))
375 r = h->validate(cd, LUKS2_get_keyslot_jobj(hdr, keyslot));
377 log_dbg(cd, "Keyslot %d validation failed.", keyslot);
381 r = _keyslot_for_digest(hdr, keyslot, digest);
384 log_dbg(cd, "Keyslot %d unusable for digest %d.", keyslot, digest);
388 return _open_and_verify(cd, hdr, h, keyslot, password, password_len, vk);
391 static int LUKS2_open_and_verify(struct crypt_device *cd,
392 struct luks2_hdr *hdr,
395 const char *password,
397 struct volume_key **vk)
399 const keyslot_handler *h;
402 if (!(h = LUKS2_keyslot_handler(cd, keyslot)))
405 r = h->validate(cd, LUKS2_get_keyslot_jobj(hdr, keyslot));
407 log_dbg(cd, "Keyslot %d validation failed.", keyslot);
411 r = LUKS2_keyslot_for_segment(hdr, keyslot, segment);
414 log_dbg(cd, "Keyslot %d unusable for segment %d.", keyslot, segment);
418 return _open_and_verify(cd, hdr, h, keyslot, password, password_len, vk);
421 static int LUKS2_keyslot_open_priority_digest(struct crypt_device *cd,
422 struct luks2_hdr *hdr,
423 crypt_keyslot_priority priority,
424 const char *password,
427 struct volume_key **vk)
429 json_object *jobj_keyslots, *jobj;
430 crypt_keyslot_priority slot_priority;
431 int keyslot, r = -ENOENT;
433 json_object_object_get_ex(hdr->jobj, "keyslots", &jobj_keyslots);
435 json_object_object_foreach(jobj_keyslots, slot, val) {
436 if (!json_object_object_get_ex(val, "priority", &jobj))
437 slot_priority = CRYPT_SLOT_PRIORITY_NORMAL;
439 slot_priority = json_object_get_int(jobj);
441 keyslot = atoi(slot);
442 if (slot_priority != priority) {
443 log_dbg(cd, "Keyslot %d priority %d != %d (required), skipped.",
444 keyslot, slot_priority, priority);
448 r = LUKS2_open_and_verify_by_digest(cd, hdr, keyslot, digest, password, password_len, vk);
450 /* Do not retry for errors that are no -EPERM or -ENOENT,
451 former meaning password wrong, latter key slot unusable for segment */
452 if ((r != -EPERM) && (r != -ENOENT))
459 static int LUKS2_keyslot_open_priority(struct crypt_device *cd,
460 struct luks2_hdr *hdr,
461 crypt_keyslot_priority priority,
462 const char *password,
465 struct volume_key **vk)
467 json_object *jobj_keyslots, *jobj;
468 crypt_keyslot_priority slot_priority;
469 int keyslot, r = -ENOENT;
471 json_object_object_get_ex(hdr->jobj, "keyslots", &jobj_keyslots);
473 json_object_object_foreach(jobj_keyslots, slot, val) {
474 if (!json_object_object_get_ex(val, "priority", &jobj))
475 slot_priority = CRYPT_SLOT_PRIORITY_NORMAL;
477 slot_priority = json_object_get_int(jobj);
479 keyslot = atoi(slot);
480 if (slot_priority != priority) {
481 log_dbg(cd, "Keyslot %d priority %d != %d (required), skipped.",
482 keyslot, slot_priority, priority);
486 r = LUKS2_open_and_verify(cd, hdr, keyslot, segment, password, password_len, vk);
488 /* Do not retry for errors that are no -EPERM or -ENOENT,
489 former meaning password wrong, latter key slot unusable for segment */
490 if ((r != -EPERM) && (r != -ENOENT))
497 static int LUKS2_keyslot_open_by_digest(struct crypt_device *cd,
498 struct luks2_hdr *hdr,
501 const char *password,
503 struct volume_key **vk)
505 int r_prio, r = -EINVAL;
510 if (keyslot == CRYPT_ANY_SLOT) {
511 r_prio = LUKS2_keyslot_open_priority_digest(cd, hdr, CRYPT_SLOT_PRIORITY_PREFER,
512 password, password_len, digest, vk);
515 else if (r_prio != -EPERM && r_prio != -ENOENT)
518 r = LUKS2_keyslot_open_priority_digest(cd, hdr, CRYPT_SLOT_PRIORITY_NORMAL,
519 password, password_len, digest, vk);
520 /* Prefer password wrong to no entry from priority slot */
521 if (r_prio == -EPERM && r == -ENOENT)
524 r = LUKS2_open_and_verify_by_digest(cd, hdr, keyslot, digest, password, password_len, vk);
529 int LUKS2_keyslot_open_all_segments(struct crypt_device *cd,
532 const char *password,
534 struct volume_key **vks)
536 struct volume_key *vk = NULL;
537 int digest_old, digest_new, r = -EINVAL;
538 struct luks2_hdr *hdr = crypt_get_hdr(cd, CRYPT_LUKS2);
540 digest_old = LUKS2_reencrypt_digest_old(hdr);
541 if (digest_old >= 0) {
542 log_dbg(cd, "Trying to unlock volume key (digest: %d) using keyslot %d.", digest_old, keyslot_old);
543 r = LUKS2_keyslot_open_by_digest(cd, hdr, keyslot_old, digest_old, password, password_len, &vk);
546 crypt_volume_key_add_next(vks, vk);
549 digest_new = LUKS2_reencrypt_digest_new(hdr);
550 if (digest_new >= 0 && digest_old != digest_new) {
551 log_dbg(cd, "Trying to unlock volume key (digest: %d) using keyslot %d.", digest_new, keyslot_new);
552 r = LUKS2_keyslot_open_by_digest(cd, hdr, keyslot_new, digest_new, password, password_len, &vk);
555 crypt_volume_key_add_next(vks, vk);
559 crypt_free_volume_key(*vks);
563 log_err(cd, _("Not enough available memory to open a keyslot."));
564 else if (r != -EPERM && r != -ENOENT)
565 log_err(cd, _("Keyslot open failed."));
570 int LUKS2_keyslot_open(struct crypt_device *cd,
573 const char *password,
575 struct volume_key **vk)
577 struct luks2_hdr *hdr;
578 int r_prio, r = -EINVAL;
580 hdr = crypt_get_hdr(cd, CRYPT_LUKS2);
582 if (keyslot == CRYPT_ANY_SLOT) {
583 r_prio = LUKS2_keyslot_open_priority(cd, hdr, CRYPT_SLOT_PRIORITY_PREFER,
584 password, password_len, segment, vk);
587 else if (r_prio != -EPERM && r_prio != -ENOENT)
590 r = LUKS2_keyslot_open_priority(cd, hdr, CRYPT_SLOT_PRIORITY_NORMAL,
591 password, password_len, segment, vk);
592 /* Prefer password wrong to no entry from priority slot */
593 if (r_prio == -EPERM && r == -ENOENT)
596 r = LUKS2_open_and_verify(cd, hdr, keyslot, segment, password, password_len, vk);
600 log_err(cd, _("Not enough available memory to open a keyslot."));
601 else if (r != -EPERM && r != -ENOENT)
602 log_err(cd, _("Keyslot open failed."));
608 int LUKS2_keyslot_reencrypt_store(struct crypt_device *cd,
609 struct luks2_hdr *hdr,
612 size_t buffer_length)
614 const keyslot_handler *h;
617 if (!(h = LUKS2_keyslot_handler(cd, keyslot)) || strcmp(h->name, "reencrypt"))
620 r = h->validate(cd, LUKS2_get_keyslot_jobj(hdr, keyslot));
622 log_dbg(cd, "Keyslot validation failed.");
626 return h->store(cd, keyslot, NULL, 0,
627 buffer, buffer_length);
630 int LUKS2_keyslot_store(struct crypt_device *cd,
631 struct luks2_hdr *hdr,
633 const char *password,
635 const struct volume_key *vk,
636 const struct luks2_keyslot_params *params)
638 const keyslot_handler *h;
641 if (keyslot == CRYPT_ANY_SLOT)
644 if (!LUKS2_get_keyslot_jobj(hdr, keyslot)) {
645 /* Try to allocate default and empty keyslot type */
646 h = LUKS2_keyslot_handler_type("luks2");
650 r = h->alloc(cd, keyslot, vk->keylength, params);
654 if (!(h = LUKS2_keyslot_handler(cd, keyslot)))
657 r = h->update(cd, keyslot, params);
659 log_dbg(cd, "Failed to update keyslot %d json.", keyslot);
664 r = h->validate(cd, LUKS2_get_keyslot_jobj(hdr, keyslot));
666 log_dbg(cd, "Keyslot validation failed.");
670 if (LUKS2_hdr_validate(cd, hdr->jobj, hdr->hdr_size - LUKS2_HDR_BIN_LEN))
673 return h->store(cd, keyslot, password, password_len,
674 vk->key, vk->keylength);
677 int LUKS2_keyslot_wipe(struct crypt_device *cd,
678 struct luks2_hdr *hdr,
682 struct device *device = crypt_metadata_device(cd);
683 uint64_t area_offset, area_length;
685 json_object *jobj_keyslot, *jobj_keyslots;
686 const keyslot_handler *h;
688 h = LUKS2_keyslot_handler(cd, keyslot);
690 if (!json_object_object_get_ex(hdr->jobj, "keyslots", &jobj_keyslots))
693 jobj_keyslot = LUKS2_get_keyslot_jobj(hdr, keyslot);
698 log_dbg(cd, "Wiping keyslot %d area only.", keyslot);
700 r = LUKS2_device_write_lock(cd, hdr, device);
704 /* secure deletion of possible key material in keyslot area */
705 r = crypt_keyslot_area(cd, keyslot, &area_offset, &area_length);
706 if (r && r != -ENOENT)
710 r = crypt_wipe_device(cd, device, CRYPT_WIPE_SPECIAL, area_offset,
711 area_length, area_length, NULL, NULL);
714 log_err(cd, _("Cannot write to device %s, permission denied."),
715 device_path(device));
718 log_err(cd, _("Cannot wipe device %s."), device_path(device));
726 /* Slot specific wipe */
728 r = h->wipe(cd, keyslot);
732 log_dbg(cd, "Wiping keyslot %d without specific-slot handler loaded.", keyslot);
734 json_object_object_del_by_uint(jobj_keyslots, keyslot);
736 r = LUKS2_hdr_write(cd, hdr);
738 device_write_unlock(cd, crypt_metadata_device(cd));
742 int LUKS2_keyslot_dump(struct crypt_device *cd, int keyslot)
744 const keyslot_handler *h;
746 if (!(h = LUKS2_keyslot_handler(cd, keyslot)))
749 return h->dump(cd, keyslot);
752 crypt_keyslot_priority LUKS2_keyslot_priority_get(struct luks2_hdr *hdr, int keyslot)
754 json_object *jobj_keyslot, *jobj_priority;
756 jobj_keyslot = LUKS2_get_keyslot_jobj(hdr, keyslot);
758 return CRYPT_SLOT_PRIORITY_INVALID;
760 if (!json_object_object_get_ex(jobj_keyslot, "priority", &jobj_priority))
761 return CRYPT_SLOT_PRIORITY_NORMAL;
763 return json_object_get_int(jobj_priority);
766 int LUKS2_keyslot_priority_set(struct crypt_device *cd, struct luks2_hdr *hdr,
767 int keyslot, crypt_keyslot_priority priority, int commit)
769 json_object *jobj_keyslot;
771 jobj_keyslot = LUKS2_get_keyslot_jobj(hdr, keyslot);
775 if (priority == CRYPT_SLOT_PRIORITY_NORMAL)
776 json_object_object_del(jobj_keyslot, "priority");
778 json_object_object_add(jobj_keyslot, "priority", json_object_new_int(priority));
780 return commit ? LUKS2_hdr_write(cd, hdr) : 0;
783 int placeholder_keyslot_alloc(struct crypt_device *cd,
785 uint64_t area_offset,
786 uint64_t area_length)
788 struct luks2_hdr *hdr;
789 json_object *jobj_keyslots, *jobj_keyslot, *jobj_area;
791 log_dbg(cd, "Allocating placeholder keyslot %d for LUKS1 down conversion.", keyslot);
793 if (!(hdr = crypt_get_hdr(cd, CRYPT_LUKS2)))
796 if (keyslot < 0 || keyslot >= LUKS2_KEYSLOTS_MAX)
799 if (LUKS2_get_keyslot_jobj(hdr, keyslot))
802 if (!json_object_object_get_ex(hdr->jobj, "keyslots", &jobj_keyslots))
805 jobj_keyslot = json_object_new_object();
806 json_object_object_add(jobj_keyslot, "type", json_object_new_string("placeholder"));
808 * key_size = -1 makes placeholder keyslot impossible to pass validation.
809 * It's a safeguard against accidentally storing temporary conversion
812 json_object_object_add(jobj_keyslot, "key_size", json_object_new_int(-1));
815 jobj_area = json_object_new_object();
816 json_object_object_add(jobj_area, "offset", crypt_jobj_new_uint64(area_offset));
817 json_object_object_add(jobj_area, "size", crypt_jobj_new_uint64(area_length));
818 json_object_object_add(jobj_keyslot, "area", jobj_area);
820 json_object_object_add_by_uint(jobj_keyslots, keyslot, jobj_keyslot);
825 static unsigned LUKS2_get_keyslot_digests_count(json_object *hdr_jobj, int keyslot)
828 json_object *jobj_digests, *jobj_keyslots;
831 if (!json_object_object_get_ex(hdr_jobj, "digests", &jobj_digests))
834 if (snprintf(num, sizeof(num), "%u", keyslot) < 0)
837 json_object_object_foreach(jobj_digests, key, val) {
839 json_object_object_get_ex(val, "keyslots", &jobj_keyslots);
840 if (LUKS2_array_jobj(jobj_keyslots, num))
847 /* run only on header that passed basic format validation */
848 int LUKS2_keyslots_validate(struct crypt_device *cd, json_object *hdr_jobj)
850 const keyslot_handler *h;
852 json_object *jobj_keyslots, *jobj_type;
853 uint32_t reqs, reencrypt_count = 0;
854 struct luks2_hdr dummy = {
858 if (!json_object_object_get_ex(hdr_jobj, "keyslots", &jobj_keyslots))
861 if (LUKS2_config_get_requirements(cd, &dummy, &reqs))
864 json_object_object_foreach(jobj_keyslots, slot, val) {
865 keyslot = atoi(slot);
866 json_object_object_get_ex(val, "type", &jobj_type);
867 h = LUKS2_keyslot_handler_type(json_object_get_string(jobj_type));
870 if (h->validate && h->validate(cd, val)) {
871 log_dbg(cd, "Keyslot type %s validation failed on keyslot %d.", h->name, keyslot);
875 if (!strcmp(h->name, "luks2") && LUKS2_get_keyslot_digests_count(hdr_jobj, keyslot) != 1) {
876 log_dbg(cd, "Keyslot %d is not assigned to exactly 1 digest.", keyslot);
880 if (!strcmp(h->name, "reencrypt"))
884 if ((reqs & CRYPT_REQUIREMENT_ONLINE_REENCRYPT) && reencrypt_count == 0) {
885 log_dbg(cd, "Missing reencryption keyslot.");
889 if (reencrypt_count && !LUKS2_reencrypt_requirement_candidate(&dummy)) {
890 log_dbg(cd, "Missing reencryption requirement flag.");
894 if (reencrypt_count > 1) {
895 log_dbg(cd, "Too many reencryption keyslots.");
902 void LUKS2_keyslots_repair(struct crypt_device *cd, json_object *jobj_keyslots)
904 const keyslot_handler *h;
905 json_object *jobj_type;
907 json_object_object_foreach(jobj_keyslots, slot, val) {
909 if (!json_object_is_type(val, json_type_object) ||
910 !json_object_object_get_ex(val, "type", &jobj_type) ||
911 !json_object_is_type(jobj_type, json_type_string))
914 h = LUKS2_keyslot_handler_type(json_object_get_string(jobj_type));
920 /* assumes valid header */
921 int LUKS2_find_keyslot(struct luks2_hdr *hdr, const char *type)
924 json_object *jobj_keyslot, *jobj_type;
929 for (i = 0; i < LUKS2_KEYSLOTS_MAX; i++) {
930 jobj_keyslot = LUKS2_get_keyslot_jobj(hdr, i);
934 json_object_object_get_ex(jobj_keyslot, "type", &jobj_type);
935 if (!strcmp(json_object_get_string(jobj_type), type))
942 /* assumes valid header, it does not move references in tokens/digests etc! */
943 int LUKS2_keyslot_swap(struct crypt_device *cd, struct luks2_hdr *hdr,
944 int keyslot, int keyslot2)
946 json_object *jobj_keyslots, *jobj_keyslot, *jobj_keyslot2;
949 if (!json_object_object_get_ex(hdr->jobj, "keyslots", &jobj_keyslots))
952 jobj_keyslot = LUKS2_get_keyslot_jobj(hdr, keyslot);
956 jobj_keyslot2 = LUKS2_get_keyslot_jobj(hdr, keyslot2);
960 /* This transfer owner of object, no need for json_object_put */
961 json_object_get(jobj_keyslot);
962 json_object_get(jobj_keyslot2);
964 json_object_object_del_by_uint(jobj_keyslots, keyslot);
965 r = json_object_object_add_by_uint(jobj_keyslots, keyslot, jobj_keyslot2);
967 log_dbg(cd, "Failed to swap keyslot %d.", keyslot);
971 json_object_object_del_by_uint(jobj_keyslots, keyslot2);
972 r = json_object_object_add_by_uint(jobj_keyslots, keyslot2, jobj_keyslot);
974 log_dbg(cd, "Failed to swap keyslot2 %d.", keyslot2);