1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* cryptsetup volume key implementation
*
* Copyright (C) 2004-2006 Clemens Fruhwirth <clemens@endorphin.org>
* Copyright (C) 2010-2025 Red Hat, Inc. All rights reserved.
*/
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
#include <errno.h>
#include "internal.h"
struct volume_key {
int id;
size_t keylength; /* length in bytes */
const char *key_description; /* keyring key name/description */
key_type_t keyring_key_type; /* kernel keyring key type */
key_serial_t key_id; /* kernel key id of volume key representation linked in thread keyring */
struct volume_key *next;
char *key;
};
struct volume_key *crypt_alloc_volume_key(size_t keylength, const char *key)
{
struct volume_key *vk;
if (keylength > (SIZE_MAX - sizeof(*vk)))
return NULL;
vk = crypt_zalloc(sizeof(*vk));
if (!vk)
return NULL;
vk->keyring_key_type = INVALID_KEY;
vk->key_id = -1;
vk->keylength = keylength;
vk->id = KEY_NOT_VERIFIED;
/* keylength 0 is valid => no key */
if (vk->keylength && key) {
vk->key = crypt_safe_alloc(keylength);
if (!vk->key) {
free(vk);
return NULL;
}
crypt_safe_memcpy(vk->key, key, keylength);
}
return vk;
}
struct volume_key *crypt_alloc_volume_key_by_safe_alloc(void **safe_alloc)
{
size_t keylength;
struct volume_key *vk;
if (!safe_alloc)
return NULL;
keylength = crypt_safe_alloc_size(*safe_alloc);
if (!keylength)
return NULL;
vk = crypt_alloc_volume_key(keylength, NULL);
if (!vk)
return NULL;
vk->key = *safe_alloc;
*safe_alloc = NULL;
return vk;
}
void crypt_volume_key_pass_safe_alloc(struct volume_key *vk, void **safe_alloc)
{
assert(vk);
assert(vk->keylength);
assert(safe_alloc);
assert(crypt_safe_alloc_size(*safe_alloc) == vk->keylength);
crypt_safe_free(vk->key);
vk->key = *safe_alloc;
*safe_alloc = NULL;
}
const char *crypt_volume_key_get_key(const struct volume_key *vk)
{
assert(vk && vk->key);
return vk->key;
}
size_t crypt_volume_key_length(const struct volume_key *vk)
{
assert(vk);
return vk->keylength;
}
int crypt_volume_key_set_description(struct volume_key *vk,
const char *key_description, key_type_t keyring_key_type)
{
if (!vk)
return -EINVAL;
free(CONST_CAST(void*)vk->key_description);
vk->key_description = NULL;
vk->keyring_key_type = keyring_key_type;
if (key_description && !(vk->key_description = strdup(key_description)))
return -ENOMEM;
return 0;
}
int crypt_volume_key_set_description_by_name(struct volume_key *vk, const char *key_name)
{
const char *key_description = NULL;
key_type_t keyring_key_type = keyring_type_and_name(key_name, &key_description);
if (keyring_key_type == INVALID_KEY)
return -EINVAL;
return crypt_volume_key_set_description(vk, key_description, keyring_key_type);
}
const char *crypt_volume_key_description(const struct volume_key *vk)
{
assert(vk);
return vk->key_description;
}
key_type_t crypt_volume_key_kernel_key_type(const struct volume_key *vk)
{
assert(vk);
return vk->keyring_key_type;
}
void crypt_volume_key_set_id(struct volume_key *vk, int id)
{
if (vk && id >= 0)
vk->id = id;
}
int crypt_volume_key_get_id(const struct volume_key *vk)
{
return vk ? vk->id : -1;
}
struct volume_key *crypt_volume_key_by_id(struct volume_key *vks, int id)
{
struct volume_key *vk = vks;
if (id < 0)
return NULL;
while (vk && vk->id != id)
vk = vk->next;
return vk;
}
void crypt_volume_key_add_next(struct volume_key **vks, struct volume_key *vk)
{
struct volume_key *tmp;
if (!vks)
return;
if (!*vks) {
*vks = vk;
return;
}
tmp = *vks;
while (tmp->next)
tmp = tmp->next;
tmp->next = vk;
}
struct volume_key *crypt_volume_key_next(struct volume_key *vk)
{
return vk ? vk->next : NULL;
}
void crypt_free_volume_key(struct volume_key *vk)
{
struct volume_key *vk_next;
while (vk) {
free(CONST_CAST(void*)vk->key_description);
crypt_safe_free(vk->key);
vk_next = vk->next;
free(vk);
vk = vk_next;
}
}
struct volume_key *crypt_generate_volume_key(struct crypt_device *cd, size_t keylength,
key_quality_info quality)
{
int r;
void *key;
struct volume_key *vk = NULL;
key = crypt_safe_alloc(keylength);
if (!key)
return NULL;
switch (quality) {
case KEY_QUALITY_KEY:
r = crypt_random_get(cd, key, keylength, CRYPT_RND_KEY);
break;
case KEY_QUALITY_NORMAL:
r = crypt_random_get(cd, key, keylength, CRYPT_RND_NORMAL);
break;
case KEY_QUALITY_EMPTY:
r = 0;
break;
default:
abort();
}
if (!r)
vk = crypt_alloc_volume_key(keylength, NULL);
if (vk)
vk->key = key;
else
crypt_safe_free(key);
return vk;
}
bool crypt_volume_key_is_set(const struct volume_key *vk)
{
return vk && vk->key;
}
bool crypt_volume_key_upload_kernel_key(struct volume_key *vk)
{
key_serial_t kid;
assert(vk && vk->key && vk->key_description && vk->keyring_key_type != INVALID_KEY);
kid = keyring_add_key_in_thread_keyring(vk->keyring_key_type, vk->key_description,
vk->key, vk->keylength);
if (kid >= 0) {
vk->key_id = kid;
return true;
}
return false;
}
void crypt_volume_key_drop_kernel_key(struct crypt_device *cd, struct volume_key *vk)
{
assert(vk);
assert(vk->key_description || vk->keyring_key_type == INVALID_KEY);
assert(!vk->key_description || vk->keyring_key_type != INVALID_KEY);
crypt_unlink_key_by_description_from_thread_keyring(cd,
vk->key_description,
vk->keyring_key_type);
}
void crypt_volume_key_drop_uploaded_kernel_key(struct crypt_device *cd, struct volume_key *vk)
{
assert(vk);
if (vk->key_id < 0)
return;
crypt_unlink_key_from_thread_keyring(cd, vk->key_id);
vk->key_id = -1;
}
|