| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <libcryptsetup.h> |
| 4 |
#include <syslog.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "cryptsetup-token.h" |
| 8 |
#include "cryptsetup-token-util.h" |
| 9 |
#include "hexdecoct.h" |
| 10 |
#include "json-util.h" |
| 11 |
#include "luks2-tpm2.h" |
| 12 |
#include "memory-util.h" |
| 13 |
#include "string-util.h" |
| 14 |
#include "tpm2-util.h" |
| 15 |
#include "version.h" |
| 16 |
|
| 17 |
#define TOKEN_NAME "systemd-tpm2" |
| 18 |
#define TOKEN_VERSION_MAJOR "1" |
| 19 |
#define TOKEN_VERSION_MINOR "0" |
| 20 |
|
| 21 |
/* for libcryptsetup debug purpose */ |
| 22 |
_public_ const char *cryptsetup_token_version(void) { |
| 23 |
|
| 24 |
return TOKEN_VERSION_MAJOR "." TOKEN_VERSION_MINOR " systemd-v" PROJECT_VERSION_FULL " (" GIT_VERSION ")"; |
| 25 |
} |
| 26 |
|
| 27 |
static int log_debug_open_error(struct crypt_device *cd, int r) { |
| 28 |
if (r == -EAGAIN) |
| 29 |
return crypt_log_debug_errno(cd, r, "TPM2 device not found."); |
| 30 |
if (r == -ENXIO) |
| 31 |
return crypt_log_debug_errno(cd, r, "No matching TPM2 token data found."); |
| 32 |
|
| 33 |
return crypt_log_debug_errno(cd, r, TOKEN_NAME " open failed: %m."); |
| 34 |
} |
| 35 |
|
| 36 |
_public_ int cryptsetup_token_open_pin( |
| 37 |
struct crypt_device *cd, /* is always LUKS2 context */ |
| 38 |
int token /* is always >= 0 */, |
| 39 |
const char *pin, |
| 40 |
size_t pin_size, |
| 41 |
char **ret_password, /* freed by cryptsetup_token_buffer_free */ |
| 42 |
size_t *ret_password_len, |
| 43 |
void *usrptr /* plugin defined parameter passed to crypt_activate_by_token*() API */) { |
| 44 |
|
| 45 |
_cleanup_(erase_and_freep) char *base64_encoded = NULL, *pin_string = NULL; |
| 46 |
_cleanup_(iovec_done) struct iovec pubkey = {}, salt = {}, srk = {}, pcrlock_nv = {}; |
| 47 |
_cleanup_(iovec_done_erase) struct iovec decrypted_key = {}; |
| 48 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 49 |
uint32_t hash_pcr_mask, pubkey_pcr_mask; |
| 50 |
systemd_tpm2_plugin_params params = { |
| 51 |
.search_pcr_mask = UINT32_MAX |
| 52 |
}; |
| 53 |
uint16_t pcr_bank, primary_alg; |
| 54 |
ssize_t base64_encoded_size; |
| 55 |
TPM2Flags flags = 0; |
| 56 |
const char *json; |
| 57 |
int r; |
| 58 |
|
| 59 |
assert(token >= 0); |
| 60 |
assert(pin || pin_size == 0); |
| 61 |
assert(ret_password); |
| 62 |
assert(ret_password_len); |
| 63 |
|
| 64 |
r = DLOPEN_CRYPTSETUP(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 65 |
if (r < 0) |
| 66 |
return r; |
| 67 |
|
| 68 |
/* This must not fail at this moment (internal error) */ |
| 69 |
r = sym_crypt_token_json_get(cd, token, &json); |
| 70 |
assert(token == r); |
| 71 |
assert(json); |
| 72 |
|
| 73 |
r = crypt_normalize_pin(pin, pin_size, &pin_string); |
| 74 |
if (r < 0) |
| 75 |
return crypt_log_debug_errno(cd, r, "Cannot normalize PIN: %m"); |
| 76 |
|
| 77 |
if (usrptr) |
| 78 |
params = *(systemd_tpm2_plugin_params *)usrptr; |
| 79 |
|
| 80 |
r = sd_json_parse(json, SD_JSON_PARSE_MUST_BE_OBJECT, &v, /* reterr_line= */ NULL, /* reterr_column= */ NULL); |
| 81 |
if (r < 0) |
| 82 |
return crypt_log_debug_errno(cd, r, "Failed to parse token JSON data: %m"); |
| 83 |
|
| 84 |
struct iovec *blobs = NULL, *policy_hash = NULL; |
| 85 |
size_t n_blobs = 0, n_policy_hash = 0; |
| 86 |
CLEANUP_ARRAY(blobs, n_blobs, iovec_array_free); |
| 87 |
CLEANUP_ARRAY(policy_hash, n_policy_hash, iovec_array_free); |
| 88 |
|
| 89 |
r = tpm2_parse_luks2_json( |
| 90 |
v, |
| 91 |
/* ret_keyslot= */ NULL, |
| 92 |
&hash_pcr_mask, |
| 93 |
&pcr_bank, |
| 94 |
&pubkey, |
| 95 |
&pubkey_pcr_mask, |
| 96 |
&primary_alg, |
| 97 |
&blobs, |
| 98 |
&n_blobs, |
| 99 |
&policy_hash, |
| 100 |
&n_policy_hash, |
| 101 |
&salt, |
| 102 |
&srk, |
| 103 |
&pcrlock_nv, |
| 104 |
&flags); |
| 105 |
if (r < 0) |
| 106 |
return log_debug_open_error(cd, r); |
| 107 |
|
| 108 |
if (params.search_pcr_mask != UINT32_MAX && hash_pcr_mask != params.search_pcr_mask) |
| 109 |
return crypt_log_debug_errno(cd, ENXIO, "PCR mask doesn't match expectation (%" PRIu32 " vs. %" PRIu32 ")", hash_pcr_mask, params.search_pcr_mask); |
| 110 |
|
| 111 |
r = acquire_luks2_key( |
| 112 |
params.device, |
| 113 |
hash_pcr_mask, |
| 114 |
pcr_bank, |
| 115 |
&pubkey, |
| 116 |
pubkey_pcr_mask, |
| 117 |
params.signature_path, |
| 118 |
pin_string, |
| 119 |
params.pcrlock_path, |
| 120 |
primary_alg, |
| 121 |
blobs, |
| 122 |
n_blobs, |
| 123 |
policy_hash, |
| 124 |
n_policy_hash, |
| 125 |
&salt, |
| 126 |
&srk, |
| 127 |
&pcrlock_nv, |
| 128 |
flags, |
| 129 |
&decrypted_key); |
| 130 |
if (r < 0) |
| 131 |
return log_debug_open_error(cd, r); |
| 132 |
|
| 133 |
/* Before using this key as passphrase we base64 encode it, for compat with homed */ |
| 134 |
base64_encoded_size = base64mem(decrypted_key.iov_base, decrypted_key.iov_len, &base64_encoded); |
| 135 |
if (base64_encoded_size < 0) |
| 136 |
return log_debug_open_error(cd, base64_encoded_size); |
| 137 |
|
| 138 |
/* free'd automatically by libcryptsetup */ |
| 139 |
*ret_password = TAKE_PTR(base64_encoded); |
| 140 |
*ret_password_len = base64_encoded_size; |
| 141 |
|
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
/* |
| 146 |
* This function is called from within following libcryptsetup calls |
| 147 |
* provided conditions further below are met: |
| 148 |
* |
| 149 |
* crypt_activate_by_token(), crypt_activate_by_token_type(type == 'systemd-tpm2'): |
| 150 |
* |
| 151 |
* - token is assigned to at least one luks2 keyslot eligible to activate LUKS2 device |
| 152 |
* (alternatively: name is set to null, flags contains CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY |
| 153 |
* and token is assigned to at least single keyslot). |
| 154 |
* |
| 155 |
* - if plugin defines validate function (see cryptsetup_token_validate below) it must have |
| 156 |
* passed the check (aka return 0) |
| 157 |
*/ |
| 158 |
_public_ int cryptsetup_token_open( |
| 159 |
struct crypt_device *cd, /* is always LUKS2 context */ |
| 160 |
int token /* is always >= 0 */, |
| 161 |
char **ret_password, /* freed by cryptsetup_token_buffer_free */ |
| 162 |
size_t *ret_password_len, |
| 163 |
void *usrptr /* plugin defined parameter passed to crypt_activate_by_token*() API */) { |
| 164 |
|
| 165 |
return cryptsetup_token_open_pin(cd, token, NULL, 0, ret_password, ret_password_len, usrptr); |
| 166 |
} |
| 167 |
|
| 168 |
/* |
| 169 |
* libcryptsetup callback for memory deallocation of 'password' parameter passed in |
| 170 |
* any crypt_token_open_* plugin function |
| 171 |
*/ |
| 172 |
_public_ void cryptsetup_token_buffer_free(void *buffer, size_t buffer_len) { |
| 173 |
erase_and_free(buffer); |
| 174 |
} |
| 175 |
|
| 176 |
/* |
| 177 |
* prints systemd-tpm2 token content in crypt_dump(). |
| 178 |
* 'type' and 'keyslots' fields are printed by libcryptsetup |
| 179 |
*/ |
| 180 |
_public_ void cryptsetup_token_dump( |
| 181 |
struct crypt_device *cd /* is always LUKS2 context */, |
| 182 |
const char *json /* validated 'systemd-tpm2' token if cryptsetup_token_validate is defined */) { |
| 183 |
|
| 184 |
_cleanup_free_ char *hash_pcrs_str = NULL, *pubkey_pcrs_str = NULL, *pubkey_str = NULL; |
| 185 |
_cleanup_(iovec_done) struct iovec pubkey = {}, salt = {}, srk = {}, pcrlock_nv = {}; |
| 186 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 187 |
uint32_t hash_pcr_mask, pubkey_pcr_mask; |
| 188 |
uint16_t pcr_bank, primary_alg; |
| 189 |
TPM2Flags flags = 0; |
| 190 |
int r; |
| 191 |
|
| 192 |
assert(json); |
| 193 |
|
| 194 |
if (DLOPEN_CRYPTSETUP(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED) < 0) |
| 195 |
return; |
| 196 |
|
| 197 |
r = sd_json_parse(json, SD_JSON_PARSE_MUST_BE_OBJECT, &v, /* reterr_line= */ NULL, /* reterr_column= */ NULL); |
| 198 |
if (r < 0) |
| 199 |
return (void) crypt_log_debug_errno(cd, r, "Failed to parse " TOKEN_NAME " JSON object: %m"); |
| 200 |
|
| 201 |
struct iovec *blobs = NULL, *policy_hash = NULL; |
| 202 |
size_t n_blobs = 0, n_policy_hash = 0; |
| 203 |
CLEANUP_ARRAY(blobs, n_blobs, iovec_array_free); |
| 204 |
CLEANUP_ARRAY(policy_hash, n_policy_hash, iovec_array_free); |
| 205 |
|
| 206 |
r = tpm2_parse_luks2_json( |
| 207 |
v, |
| 208 |
NULL, |
| 209 |
&hash_pcr_mask, |
| 210 |
&pcr_bank, |
| 211 |
&pubkey, |
| 212 |
&pubkey_pcr_mask, |
| 213 |
&primary_alg, |
| 214 |
&blobs, |
| 215 |
&n_blobs, |
| 216 |
&policy_hash, |
| 217 |
&n_policy_hash, |
| 218 |
&salt, |
| 219 |
&srk, |
| 220 |
&pcrlock_nv, |
| 221 |
&flags); |
| 222 |
if (r < 0) |
| 223 |
return (void) crypt_log_debug_errno(cd, r, "Failed to parse " TOKEN_NAME " JSON fields: %m"); |
| 224 |
|
| 225 |
hash_pcrs_str = tpm2_pcr_mask_to_string(hash_pcr_mask); |
| 226 |
if (!hash_pcrs_str) |
| 227 |
return (void) crypt_log_debug_errno(cd, ENOMEM, "Cannot format PCR hash mask: %m"); |
| 228 |
|
| 229 |
pubkey_pcrs_str = tpm2_pcr_mask_to_string(pubkey_pcr_mask); |
| 230 |
if (!pubkey_pcrs_str) |
| 231 |
return (void) crypt_log_debug_errno(cd, ENOMEM, "Cannot format PCR hash mask: %m"); |
| 232 |
|
| 233 |
r = crypt_dump_buffer_to_hex_string(pubkey.iov_base, pubkey.iov_len, &pubkey_str); |
| 234 |
if (r < 0) |
| 235 |
return (void) crypt_log_debug_errno(cd, r, "Cannot dump " TOKEN_NAME " content: %m"); |
| 236 |
|
| 237 |
crypt_log(cd, "\ttpm2-hash-pcrs: %s\n", strna(hash_pcrs_str)); |
| 238 |
crypt_log(cd, "\ttpm2-pcr-bank: %s\n", strna(tpm2_hash_alg_to_string(pcr_bank))); |
| 239 |
crypt_log(cd, "\ttpm2-pubkey:" CRYPT_DUMP_LINE_SEP "%s\n", pubkey_str); |
| 240 |
crypt_log(cd, "\ttpm2-pubkey-pcrs: %s\n", strna(pubkey_pcrs_str)); |
| 241 |
if (primary_alg != 0) |
| 242 |
crypt_log(cd, "\ttpm2-primary-alg: %s\n", strna(tpm2_asym_alg_to_string(primary_alg))); |
| 243 |
crypt_log(cd, "\ttpm2-pin: %s\n", true_false(flags & TPM2_FLAGS_USE_PIN)); |
| 244 |
crypt_log(cd, "\ttpm2-pcrlock: %s\n", true_false(flags & TPM2_FLAGS_USE_PCRLOCK)); |
| 245 |
crypt_log(cd, "\ttpm2-salt: %s\n", true_false(iovec_is_set(&salt))); |
| 246 |
crypt_log(cd, "\ttpm2-srk: %s\n", true_false(iovec_is_set(&srk))); |
| 247 |
crypt_log(cd, "\ttpm2-pcrlock-nv: %s\n", true_false(iovec_is_set(&pcrlock_nv))); |
| 248 |
|
| 249 |
FOREACH_ARRAY(p, policy_hash, n_policy_hash) { |
| 250 |
_cleanup_free_ char *policy_hash_str = NULL; |
| 251 |
|
| 252 |
r = crypt_dump_buffer_to_hex_string(p->iov_base, p->iov_len, &policy_hash_str); |
| 253 |
if (r < 0) |
| 254 |
return (void) crypt_log_debug_errno(cd, r, "Cannot dump " TOKEN_NAME " content: %m"); |
| 255 |
|
| 256 |
crypt_log(cd, "\ttpm2-policy-hash:" CRYPT_DUMP_LINE_SEP "%s\n", policy_hash_str); |
| 257 |
} |
| 258 |
|
| 259 |
FOREACH_ARRAY(b, blobs, n_blobs) { |
| 260 |
_cleanup_free_ char *blob_str = NULL; |
| 261 |
|
| 262 |
r = crypt_dump_buffer_to_hex_string(b->iov_base, b->iov_len, &blob_str); |
| 263 |
if (r < 0) |
| 264 |
return (void) crypt_log_debug_errno(cd, r, "Cannot dump " TOKEN_NAME " content: %m"); |
| 265 |
|
| 266 |
crypt_log(cd, "\ttpm2-blob: %s\n", blob_str); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
/* |
| 271 |
* Note: |
| 272 |
* If plugin is available in library path, it's called in before following libcryptsetup calls: |
| 273 |
* |
| 274 |
* crypt_token_json_set, crypt_dump, any crypt_activate_by_token_* flavour |
| 275 |
*/ |
| 276 |
_public_ int cryptsetup_token_validate( |
| 277 |
struct crypt_device *cd, /* is always LUKS2 context */ |
| 278 |
const char *json /* contains valid 'type' and 'keyslots' fields. 'type' is 'systemd-tpm2' */) { |
| 279 |
|
| 280 |
int r; |
| 281 |
sd_json_variant *w, *e; |
| 282 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 283 |
|
| 284 |
assert(json); |
| 285 |
|
| 286 |
r = DLOPEN_CRYPTSETUP(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 287 |
if (r < 0) |
| 288 |
return r; |
| 289 |
|
| 290 |
r = sd_json_parse(json, SD_JSON_PARSE_MUST_BE_OBJECT, &v, /* reterr_line= */ NULL, /* reterr_column= */ NULL); |
| 291 |
if (r < 0) |
| 292 |
return crypt_log_debug_errno(cd, r, "Could not parse " TOKEN_NAME " json object: %m"); |
| 293 |
|
| 294 |
w = sd_json_variant_by_key(v, "tpm2-pcrs"); |
| 295 |
if (!w || !sd_json_variant_is_array(w)) { |
| 296 |
crypt_log_debug(cd, "TPM2 token data lacks 'tpm2-pcrs' field."); |
| 297 |
return 1; |
| 298 |
} |
| 299 |
|
| 300 |
JSON_VARIANT_ARRAY_FOREACH(e, w) { |
| 301 |
uint64_t u; |
| 302 |
|
| 303 |
if (!sd_json_variant_is_number(e)) { |
| 304 |
crypt_log_debug(cd, "TPM2 PCR is not a number."); |
| 305 |
return 1; |
| 306 |
} |
| 307 |
|
| 308 |
u = sd_json_variant_unsigned(e); |
| 309 |
if (!TPM2_PCR_INDEX_VALID(u)) { |
| 310 |
crypt_log_debug(cd, "TPM2 PCR number out of range."); |
| 311 |
return 1; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
/* The bank field is optional, since it was added in systemd 250 only. Before the bank was hardcoded |
| 316 |
* to SHA256. */ |
| 317 |
w = sd_json_variant_by_key(v, "tpm2-pcr-bank"); |
| 318 |
if (w) { |
| 319 |
/* The PCR bank field is optional */ |
| 320 |
|
| 321 |
if (!sd_json_variant_is_string(w)) { |
| 322 |
crypt_log_debug(cd, "TPM2 PCR bank is not a string."); |
| 323 |
return 1; |
| 324 |
} |
| 325 |
|
| 326 |
if (tpm2_hash_alg_from_string(sd_json_variant_string(w)) < 0) { |
| 327 |
crypt_log_debug(cd, "TPM2 PCR bank invalid or not supported: %s.", sd_json_variant_string(w)); |
| 328 |
return 1; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
/* The primary key algorithm field is optional, since it was also added in systemd 250 only. Before |
| 333 |
* the algorithm was hardcoded to ECC. */ |
| 334 |
w = sd_json_variant_by_key(v, "tpm2-primary-alg"); |
| 335 |
if (w) { |
| 336 |
/* The primary key algorithm is optional */ |
| 337 |
|
| 338 |
if (!sd_json_variant_is_string(w)) { |
| 339 |
crypt_log_debug(cd, "TPM2 primary key algorithm is not a string."); |
| 340 |
return 1; |
| 341 |
} |
| 342 |
|
| 343 |
if (tpm2_asym_alg_from_string(sd_json_variant_string(w)) < 0) { |
| 344 |
crypt_log_debug(cd, "TPM2 primary key algorithm invalid or not supported: %s", sd_json_variant_string(w)); |
| 345 |
return 1; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
w = sd_json_variant_by_key(v, "tpm2-blob"); |
| 350 |
if (!w) { |
| 351 |
crypt_log_debug(cd, "TPM2 token data lacks 'tpm2-blob' field."); |
| 352 |
return 1; |
| 353 |
} |
| 354 |
|
| 355 |
if (sd_json_variant_is_array(w)) { |
| 356 |
sd_json_variant *i; |
| 357 |
JSON_VARIANT_ARRAY_FOREACH(i, w) { |
| 358 |
r = sd_json_variant_unbase64(i, /* ret= */ NULL, /* ret_size= */ NULL); |
| 359 |
if (r < 0) |
| 360 |
return crypt_log_debug_errno(cd, r, "Invalid base64 data in 'tpm2-blob' field: %m"); |
| 361 |
} |
| 362 |
} else { |
| 363 |
r = sd_json_variant_unbase64(w, /* ret= */ NULL, /* ret_size= */ NULL); |
| 364 |
if (r < 0) |
| 365 |
return crypt_log_debug_errno(cd, r, "Invalid base64 data in 'tpm2-blob' field: %m"); |
| 366 |
} |
| 367 |
|
| 368 |
w = sd_json_variant_by_key(v, "tpm2-policy-hash"); |
| 369 |
if (!w) { |
| 370 |
crypt_log_debug(cd, "TPM2 token data lacks 'tpm2-policy-hash' field."); |
| 371 |
return 1; |
| 372 |
} |
| 373 |
|
| 374 |
if (sd_json_variant_is_array(w)) { |
| 375 |
sd_json_variant *i; |
| 376 |
JSON_VARIANT_ARRAY_FOREACH(i, w) { |
| 377 |
r = sd_json_variant_unhex(i, /* ret= */ NULL, /* ret_size= */ NULL); |
| 378 |
if (r < 0) |
| 379 |
return crypt_log_debug_errno(cd, r, "Invalid hex data in 'tpm2-policy-hash' field: %m"); |
| 380 |
} |
| 381 |
} else { |
| 382 |
r = sd_json_variant_unhex(w, /* ret= */ NULL, /* ret_size= */ NULL); |
| 383 |
if (r < 0) |
| 384 |
return crypt_log_debug_errno(cd, r, "Invalid hex data in 'tpm2-policy-hash' field: %m"); |
| 385 |
} |
| 386 |
|
| 387 |
w = sd_json_variant_by_key(v, "tpm2-pin"); |
| 388 |
if (w) { |
| 389 |
if (!sd_json_variant_is_boolean(w)) { |
| 390 |
crypt_log_debug(cd, "TPM2 PIN policy is not a boolean."); |
| 391 |
return 1; |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
return 0; |
| 396 |
} |