| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <libcryptsetup.h> |
| 4 |
#include <syslog.h> |
| 5 |
|
| 6 |
#include "sd-json.h" |
| 7 |
|
| 8 |
#include "alloc-util.h" |
| 9 |
#include "cryptsetup-token.h" |
| 10 |
#include "cryptsetup-token-util.h" |
| 11 |
#include "luks2-pkcs11.h" |
| 12 |
#include "memory-util.h" |
| 13 |
#include "pkcs11-util.h" |
| 14 |
#include "version.h" |
| 15 |
|
| 16 |
#define TOKEN_NAME "systemd-pkcs11" |
| 17 |
#define TOKEN_VERSION_MAJOR "1" |
| 18 |
#define TOKEN_VERSION_MINOR "0" |
| 19 |
|
| 20 |
/* for libcryptsetup debug purpose */ |
| 21 |
_public_ const char *cryptsetup_token_version(void) { |
| 22 |
return TOKEN_VERSION_MAJOR "." TOKEN_VERSION_MINOR " systemd-v" PROJECT_VERSION_FULL " (" GIT_VERSION ")"; |
| 23 |
} |
| 24 |
|
| 25 |
_public_ int cryptsetup_token_open_pin( |
| 26 |
struct crypt_device *cd, /* is always LUKS2 context */ |
| 27 |
int token /* is always >= 0 */, |
| 28 |
const char *pin, |
| 29 |
size_t pin_size, |
| 30 |
char **password, /* freed by cryptsetup_token_buffer_free */ |
| 31 |
size_t *password_len, |
| 32 |
void *usrptr /* plugin defined parameter passed to crypt_activate_by_token*() API */) { |
| 33 |
|
| 34 |
const char *json; |
| 35 |
int r; |
| 36 |
|
| 37 |
assert(pin || pin_size == 0); |
| 38 |
assert(token >= 0); |
| 39 |
|
| 40 |
r = DLOPEN_CRYPTSETUP(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 41 |
if (r < 0) |
| 42 |
return r; |
| 43 |
|
| 44 |
/* This must not fail at this moment (internal error) */ |
| 45 |
r = sym_crypt_token_json_get(cd, token, &json); |
| 46 |
/* Use assert_se() here to avoid emitting warning with -DNDEBUG */ |
| 47 |
assert_se(token == r); |
| 48 |
assert(json); |
| 49 |
|
| 50 |
return acquire_luks2_key(cd, json, usrptr, pin, pin_size, password, password_len); |
| 51 |
} |
| 52 |
|
| 53 |
/* |
| 54 |
* This function is called from within following libcryptsetup calls |
| 55 |
* provided conditions further below are met: |
| 56 |
* |
| 57 |
* crypt_activate_by_token(), crypt_activate_by_token_type(type == 'systemd-pkcs11'): |
| 58 |
* |
| 59 |
* - token is assigned to at least one luks2 keyslot eligible to activate LUKS2 device |
| 60 |
* (alternatively: name is set to null, flags contains CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY |
| 61 |
* and token is assigned to at least single keyslot). |
| 62 |
* |
| 63 |
* - if plugin defines validate function (see cryptsetup_token_validate below) it must have |
| 64 |
* passed the check (aka return 0) |
| 65 |
*/ |
| 66 |
_public_ int cryptsetup_token_open( |
| 67 |
struct crypt_device *cd, /* is always LUKS2 context */ |
| 68 |
int token /* is always >= 0 */, |
| 69 |
char **password, /* freed by cryptsetup_token_buffer_free */ |
| 70 |
size_t *password_len, |
| 71 |
void *usrptr /* plugin defined parameter passed to crypt_activate_by_token*() API */) { |
| 72 |
|
| 73 |
return cryptsetup_token_open_pin(cd, token, NULL, 0, password, password_len, usrptr); |
| 74 |
} |
| 75 |
|
| 76 |
/* |
| 77 |
* libcryptsetup callback for memory deallocation of 'password' parameter passed in |
| 78 |
* any crypt_token_open_* plugin function |
| 79 |
*/ |
| 80 |
_public_ void cryptsetup_token_buffer_free(void *buffer, size_t buffer_len) { |
| 81 |
erase_and_free(buffer); |
| 82 |
} |
| 83 |
|
| 84 |
/* |
| 85 |
* prints systemd-pkcs11 token content in crypt_dump(). |
| 86 |
* 'type' and 'keyslots' fields are printed by libcryptsetup |
| 87 |
*/ |
| 88 |
_public_ void cryptsetup_token_dump( |
| 89 |
struct crypt_device *cd /* is always LUKS2 context */, |
| 90 |
const char *json /* validated 'systemd-pkcs11' token if cryptsetup_token_validate is defined */) { |
| 91 |
|
| 92 |
int r; |
| 93 |
size_t pkcs11_key_size; |
| 94 |
_cleanup_free_ char *pkcs11_uri = NULL, *key_str = NULL; |
| 95 |
_cleanup_free_ void *pkcs11_key = NULL; |
| 96 |
Pkcs11RsaPadding rsa_padding = PKCS11_RSA_PADDING_PKCS1V15; |
| 97 |
|
| 98 |
if (DLOPEN_CRYPTSETUP(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED) < 0) |
| 99 |
return; |
| 100 |
|
| 101 |
r = parse_luks2_pkcs11_data(cd, json, &pkcs11_uri, &pkcs11_key, &pkcs11_key_size, &rsa_padding); |
| 102 |
if (r < 0) |
| 103 |
return (void) crypt_log_debug_errno(cd, r, "Failed to parse " TOKEN_NAME " metadata: %m."); |
| 104 |
|
| 105 |
r = crypt_dump_buffer_to_hex_string(pkcs11_key, pkcs11_key_size, &key_str); |
| 106 |
if (r < 0) |
| 107 |
return (void) crypt_log_debug_errno(cd, r, "Cannot dump " TOKEN_NAME " content: %m"); |
| 108 |
|
| 109 |
crypt_log(cd, "\tpkcs11-uri: %s\n", pkcs11_uri); |
| 110 |
crypt_log(cd, "\tpkcs11-key: %s\n", key_str); |
| 111 |
crypt_log(cd, "\tpkcs11-padding: %s\n", pkcs11_rsa_padding_to_string(rsa_padding)); |
| 112 |
} |
| 113 |
|
| 114 |
/* |
| 115 |
* Note: |
| 116 |
* If plugin is available in library path, it's called in before following libcryptsetup calls: |
| 117 |
* |
| 118 |
* crypt_token_json_set, crypt_dump, any crypt_activate_by_token_* flavour |
| 119 |
*/ |
| 120 |
_public_ int cryptsetup_token_validate( |
| 121 |
struct crypt_device *cd, /* is always LUKS2 context */ |
| 122 |
const char *json /* contains valid 'type' and 'keyslots' fields. 'type' is 'systemd-pkcs11' */) { |
| 123 |
|
| 124 |
int r; |
| 125 |
sd_json_variant *w; |
| 126 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL; |
| 127 |
|
| 128 |
r = DLOPEN_CRYPTSETUP(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED); |
| 129 |
if (r < 0) |
| 130 |
return r; |
| 131 |
|
| 132 |
r = sd_json_parse(json, SD_JSON_PARSE_MUST_BE_OBJECT, &v, /* reterr_line= */ NULL, /* reterr_column= */ NULL); |
| 133 |
if (r < 0) |
| 134 |
return crypt_log_debug_errno(cd, r, "Could not parse " TOKEN_NAME " json object: %m."); |
| 135 |
|
| 136 |
w = sd_json_variant_by_key(v, "pkcs11-uri"); |
| 137 |
if (!w || !sd_json_variant_is_string(w)) { |
| 138 |
crypt_log_debug(cd, "PKCS#11 token data lacks 'pkcs11-uri' field."); |
| 139 |
return 1; |
| 140 |
} |
| 141 |
|
| 142 |
if (!pkcs11_uri_valid(sd_json_variant_string(w))) { |
| 143 |
crypt_log_debug(cd, "PKCS#11 token data contains invalid PKCS#11 URI."); |
| 144 |
return 1; |
| 145 |
} |
| 146 |
|
| 147 |
w = sd_json_variant_by_key(v, "pkcs11-key"); |
| 148 |
if (!w) { |
| 149 |
crypt_log_debug(cd, "PKCS#11 token data lacks 'pkcs11-key' field."); |
| 150 |
return 1; |
| 151 |
} |
| 152 |
|
| 153 |
r = sd_json_variant_unbase64(w, NULL, NULL); |
| 154 |
if (r < 0) |
| 155 |
return crypt_log_debug_errno(cd, r, "Failed to decode base64 encoded key: %m."); |
| 156 |
|
| 157 |
/* Optional 'pkcs11-padding' field: must be a known padding scheme string if present. Older systemd |
| 158 |
* versions will just ignore this field entirely and assume PKCS#1 v1.5. */ |
| 159 |
w = sd_json_variant_by_key(v, "pkcs11-padding"); |
| 160 |
if (w) { |
| 161 |
if (!sd_json_variant_is_string(w)) { |
| 162 |
crypt_log_debug(cd, "PKCS#11 token field 'pkcs11-padding' is not a string."); |
| 163 |
return 1; |
| 164 |
} |
| 165 |
if (pkcs11_rsa_padding_from_string(sd_json_variant_string(w)) < 0) { |
| 166 |
crypt_log_debug(cd, "PKCS#11 token field 'pkcs11-padding' has unsupported value."); |
| 167 |
return 1; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
return 0; |
| 172 |
} |