| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-json.h" |
| 4 |
|
| 5 |
#include "alloc-util.h" |
| 6 |
#include "ask-password-api.h" |
| 7 |
#include "cryptsetup-pkcs11.h" |
| 8 |
#include "cryptsetup-util.h" |
| 9 |
#include "fileio.h" |
| 10 |
#include "iovec-util.h" |
| 11 |
#include "log.h" |
| 12 |
#include "pkcs11-util.h" |
| 13 |
#include "random-util.h" |
| 14 |
|
| 15 |
int decrypt_pkcs11_key( |
| 16 |
const char *volume_name, |
| 17 |
const char *friendly_name, |
| 18 |
const char *pkcs11_uri, |
| 19 |
Pkcs11RsaPadding rsa_padding, |
| 20 |
const char *key_file, /* We either expect key_file and associated parameters to be set (for file keys) … */ |
| 21 |
size_t key_file_size, |
| 22 |
uint64_t key_file_offset, |
| 23 |
const struct iovec *key_data, /* … or literal keys via key_data */ |
| 24 |
usec_t until, |
| 25 |
AskPasswordFlags askpw_flags, |
| 26 |
void **ret_decrypted_key, |
| 27 |
size_t *ret_decrypted_key_size) { |
| 28 |
#if HAVE_P11KIT |
| 29 |
_cleanup_(pkcs11_crypt_device_callback_data_release) pkcs11_crypt_device_callback_data data = { |
| 30 |
.friendly_name = friendly_name, |
| 31 |
.askpw_flags = askpw_flags, |
| 32 |
.until = until, |
| 33 |
.rsa_padding = rsa_padding, |
| 34 |
}; |
| 35 |
int r; |
| 36 |
|
| 37 |
assert(friendly_name); |
| 38 |
assert(pkcs11_uri); |
| 39 |
assert(key_file || iovec_is_set(key_data)); |
| 40 |
assert(ret_decrypted_key); |
| 41 |
assert(ret_decrypted_key_size); |
| 42 |
|
| 43 |
/* The functions called here log about all errors, except for EAGAIN which means "token not found right now" */ |
| 44 |
|
| 45 |
if (iovec_is_set(key_data)) { |
| 46 |
data.encrypted_key = (void*) key_data->iov_base; |
| 47 |
data.encrypted_key_size = key_data->iov_len; |
| 48 |
|
| 49 |
data.free_encrypted_key = false; |
| 50 |
} else { |
| 51 |
_cleanup_free_ char *bindname = NULL; |
| 52 |
|
| 53 |
/* If we read the key via AF_UNIX, make this client recognizable */ |
| 54 |
if (asprintf(&bindname, "@%" PRIx64"/cryptsetup-pkcs11/%s", random_u64(), volume_name) < 0) |
| 55 |
return log_oom(); |
| 56 |
|
| 57 |
r = read_full_file_full( |
| 58 |
AT_FDCWD, key_file, |
| 59 |
key_file_offset == 0 ? UINT64_MAX : key_file_offset, |
| 60 |
key_file_size == 0 ? SIZE_MAX : key_file_size, |
| 61 |
READ_FULL_FILE_CONNECT_SOCKET, |
| 62 |
bindname, |
| 63 |
(char**) &data.encrypted_key, &data.encrypted_key_size); |
| 64 |
if (r < 0) |
| 65 |
return r; |
| 66 |
|
| 67 |
data.free_encrypted_key = true; |
| 68 |
} |
| 69 |
|
| 70 |
r = pkcs11_find_token(pkcs11_uri, pkcs11_crypt_device_callback, &data); |
| 71 |
if (r < 0) |
| 72 |
return r; |
| 73 |
|
| 74 |
*ret_decrypted_key = TAKE_PTR(data.decrypted_key); |
| 75 |
*ret_decrypted_key_size = data.decrypted_key_size; |
| 76 |
|
| 77 |
return 0; |
| 78 |
#else |
| 79 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "PKCS#11 Token support not available."); |
| 80 |
#endif |
| 81 |
} |
| 82 |
|
| 83 |
int find_pkcs11_auto_data( |
| 84 |
struct crypt_device *cd, |
| 85 |
char **ret_uri, |
| 86 |
void **ret_encrypted_key, |
| 87 |
size_t *ret_encrypted_key_size, |
| 88 |
Pkcs11RsaPadding *ret_rsa_padding, |
| 89 |
int *ret_keyslot) { |
| 90 |
|
| 91 |
#if HAVE_P11KIT |
| 92 |
_cleanup_free_ char *uri = NULL; |
| 93 |
_cleanup_free_ void *key = NULL; |
| 94 |
int r, keyslot = -1; |
| 95 |
size_t key_size = 0; |
| 96 |
Pkcs11RsaPadding rsa_padding = PKCS11_RSA_PADDING_PKCS1V15; |
| 97 |
|
| 98 |
assert(cd); |
| 99 |
assert(ret_uri); |
| 100 |
assert(ret_encrypted_key); |
| 101 |
assert(ret_encrypted_key_size); |
| 102 |
assert(ret_rsa_padding); |
| 103 |
assert(ret_keyslot); |
| 104 |
|
| 105 |
/* Loads PKCS#11 metadata from LUKS2 JSON token headers. */ |
| 106 |
|
| 107 |
for (int token = 0; token < sym_crypt_token_max(CRYPT_LUKS2); token++) { |
| 108 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 109 |
sd_json_variant *w; |
| 110 |
int ks; |
| 111 |
|
| 112 |
r = cryptsetup_get_token_as_json(cd, token, "systemd-pkcs11", &v); |
| 113 |
if (IN_SET(r, -ENOENT, -EINVAL, -EMEDIUMTYPE)) |
| 114 |
continue; |
| 115 |
if (r < 0) |
| 116 |
return log_error_errno(r, "Failed to read JSON token data off disk: %m"); |
| 117 |
|
| 118 |
ks = cryptsetup_get_keyslot_from_token(v); |
| 119 |
if (ks < 0) { |
| 120 |
/* Handle parsing errors of the keyslots field gracefully, since it's not 'owned' by |
| 121 |
* us, but by the LUKS2 spec */ |
| 122 |
log_warning_errno(ks, "Failed to extract keyslot index from PKCS#11 JSON data token %i, skipping: %m", token); |
| 123 |
continue; |
| 124 |
} |
| 125 |
|
| 126 |
if (uri) |
| 127 |
return log_error_errno(SYNTHETIC_ERRNO(ENOTUNIQ), |
| 128 |
"Multiple PKCS#11 tokens enrolled, cannot automatically determine token."); |
| 129 |
|
| 130 |
assert(keyslot < 0); |
| 131 |
keyslot = ks; |
| 132 |
|
| 133 |
w = sd_json_variant_by_key(v, "pkcs11-uri"); |
| 134 |
if (!w || !sd_json_variant_is_string(w)) |
| 135 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 136 |
"PKCS#11 token data lacks 'pkcs11-uri' field."); |
| 137 |
|
| 138 |
uri = strdup(sd_json_variant_string(w)); |
| 139 |
if (!uri) |
| 140 |
return log_oom(); |
| 141 |
|
| 142 |
if (!pkcs11_uri_valid(uri)) |
| 143 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 144 |
"PKCS#11 token data contains invalid PKCS#11 URI."); |
| 145 |
|
| 146 |
w = sd_json_variant_by_key(v, "pkcs11-key"); |
| 147 |
if (!w) |
| 148 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 149 |
"PKCS#11 token data lacks 'pkcs11-key' field."); |
| 150 |
|
| 151 |
assert(!key); |
| 152 |
assert(key_size == 0); |
| 153 |
r = sd_json_variant_unbase64(w, &key, &key_size); |
| 154 |
if (r < 0) |
| 155 |
return log_error_errno(r, "Failed to decode base64 encoded key: %m"); |
| 156 |
|
| 157 |
/* Optional padding-scheme tag. Absent in legacy tokens (which used PKCS#1 v1.5). */ |
| 158 |
w = sd_json_variant_by_key(v, "pkcs11-padding"); |
| 159 |
if (w) { |
| 160 |
Pkcs11RsaPadding p; |
| 161 |
|
| 162 |
if (!sd_json_variant_is_string(w)) |
| 163 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 164 |
"PKCS#11 token field 'pkcs11-padding' is not a string."); |
| 165 |
p = pkcs11_rsa_padding_from_string(sd_json_variant_string(w)); |
| 166 |
if (p < 0) |
| 167 |
return log_error_errno(p, |
| 168 |
"PKCS#11 token field 'pkcs11-padding' has unsupported value '%s'.", |
| 169 |
sd_json_variant_string(w)); |
| 170 |
rsa_padding = p; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
if (!uri) |
| 175 |
return log_error_errno(SYNTHETIC_ERRNO(ENXIO), |
| 176 |
"No valid PKCS#11 token data found."); |
| 177 |
|
| 178 |
log_info("Automatically discovered security PKCS#11 token '%s' unlocks volume.", uri); |
| 179 |
|
| 180 |
*ret_uri = TAKE_PTR(uri); |
| 181 |
*ret_encrypted_key = TAKE_PTR(key); |
| 182 |
*ret_encrypted_key_size = key_size; |
| 183 |
*ret_rsa_padding = rsa_padding; |
| 184 |
*ret_keyslot = keyslot; |
| 185 |
return 0; |
| 186 |
#else |
| 187 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "PKCS#11 Token support not available."); |
| 188 |
#endif |
| 189 |
} |