Debian Codegraph @

src/cryptsetup/cryptsetup-tokens/luks2-pkcs11.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <p11-kit/p11-kit.h>
4 #include <p11-kit/uri.h>
5
6 #include "sd-json.h"
7
8 #include "alloc-util.h"
9 #include "cryptsetup-token-util.h"
10 #include "escape.h"
11 #include "hexdecoct.h"
12 #include "luks2-pkcs11.h"
13 #include "memory-util.h"
14 #include "pkcs11-util.h"
15 #include "time-util.h"
16
17 struct luks2_pkcs11_callback_data {
18 struct crypt_device *cd;
19 const char *pin;
20 size_t pin_size;
21 void *encrypted_key;
22 size_t encrypted_key_size;
23 Pkcs11RsaPadding rsa_padding;
24 void *decrypted_key;
25 size_t decrypted_key_size;
26 };
27
28 static int luks2_pkcs11_callback(
29 CK_FUNCTION_LIST *m,
30 CK_SESSION_HANDLE session,
31 CK_SLOT_ID slot_id,
32 const CK_SLOT_INFO *slot_info,
33 const CK_TOKEN_INFO *token_info,
34 P11KitUri *uri,
35 void *userdata) {
36
37 CK_OBJECT_HANDLE object;
38 CK_RV rv;
39 CK_TOKEN_INFO updated_token_info;
40 int r;
41 _cleanup_free_ char *token_label = NULL;
42 struct luks2_pkcs11_callback_data *data = ASSERT_PTR(userdata);
43
44 assert(m);
45 assert(slot_info);
46 assert(token_info);
47 assert(uri);
48
49 token_label = pkcs11_token_label(token_info);
50 if (!token_label)
51 return -ENOMEM;
52
53 /* Called for every token matching our URI */
54 r = pkcs11_token_login_by_pin(m, session, token_info, token_label, data->pin, data->pin_size);
55 if (r == -ENOLCK) {
56 /* Refresh the token info, so that we can prompt knowing the new flags if they changed. */
57 rv = m->C_GetTokenInfo(slot_id, &updated_token_info);
58 if (rv != CKR_OK) {
59 crypt_log_error(data->cd,
60 "Failed to acquire updated security token information for slot %lu: %s",
61 slot_id, sym_p11_kit_strerror(rv));
62 return -EIO;
63 }
64 token_info = &updated_token_info;
65 r = -ENOANO;
66 }
67
68 if (r == -ENOANO) {
69 if (FLAGS_SET(token_info->flags, CKF_USER_PIN_FINAL_TRY))
70 crypt_log_error(data->cd, "Please enter correct PIN for security token "
71 "'%s' in order to unlock it (final try).", token_label);
72 else if (FLAGS_SET(token_info->flags, CKF_USER_PIN_COUNT_LOW))
73 crypt_log_error(data->cd, "PIN has been entered incorrectly previously, "
74 "please enter correct PIN for security token '%s' in order to unlock it.",
75 token_label);
76 }
77
78 if (r == -EPERM) /* pin is locked, but map it to -ENOANO anyway */
79 r = -ENOANO;
80
81 if (r < 0)
82 return r;
83
84 r = pkcs11_token_find_private_key(m, session, uri, &object);
85 if (r < 0)
86 return r;
87
88 r = pkcs11_token_decrypt_data(
89 m,
90 session,
91 object,
92 data->encrypted_key,
93 data->encrypted_key_size,
94 data->rsa_padding,
95 &data->decrypted_key,
96 &data->decrypted_key_size);
97 if (r < 0)
98 return r;
99
100 return 0;
101 }
102
103 static void luks2_pkcs11_callback_data_release(struct luks2_pkcs11_callback_data *data) {
104 erase_and_free(data->decrypted_key);
105 }
106
107 static int acquire_luks2_key_by_pin(
108 struct crypt_device *cd,
109 const char *pkcs11_uri,
110 const void *pin,
111 size_t pin_size,
112 void *encrypted_key,
113 size_t encrypted_key_size,
114 Pkcs11RsaPadding rsa_padding,
115 void **ret_decrypted_key,
116 size_t *ret_decrypted_key_size) {
117
118 int r;
119 _cleanup_(luks2_pkcs11_callback_data_release) struct luks2_pkcs11_callback_data data = {
120 .cd = cd,
121 .pin = pin,
122 .pin_size = pin_size,
123 .encrypted_key = encrypted_key,
124 .encrypted_key_size = encrypted_key_size,
125 .rsa_padding = rsa_padding,
126 };
127
128 assert(pkcs11_uri);
129 assert(encrypted_key);
130 assert(ret_decrypted_key);
131 assert(ret_decrypted_key_size);
132
133 r = pkcs11_find_token(pkcs11_uri, luks2_pkcs11_callback, &data);
134 if (r < 0)
135 return r;
136
137 *ret_decrypted_key = TAKE_PTR(data.decrypted_key);
138 *ret_decrypted_key_size = data.decrypted_key_size;
139
140 return 0;
141 }
142
143 /* called from within systemd utilities */
144 static int acquire_luks2_key_systemd(
145 const char *pkcs11_uri,
146 systemd_pkcs11_plugin_params *params,
147 void *encrypted_key,
148 size_t encrypted_key_size,
149 Pkcs11RsaPadding rsa_padding,
150 void **ret_decrypted_key,
151 size_t *ret_decrypted_key_size) {
152
153 int r;
154 _cleanup_(pkcs11_crypt_device_callback_data_release) pkcs11_crypt_device_callback_data data = {
155 .encrypted_key = encrypted_key,
156 .encrypted_key_size = encrypted_key_size,
157 .free_encrypted_key = false,
158 .rsa_padding = rsa_padding,
159 };
160
161 assert(pkcs11_uri);
162 assert(encrypted_key);
163 assert(ret_decrypted_key);
164 assert(ret_decrypted_key_size);
165 assert(params);
166
167 data.friendly_name = params->friendly_name;
168 data.askpw_credential = params->askpw_credential;
169 data.askpw_flags = params->askpw_flags;
170 data.until = params->until;
171
172 /* The functions called here log about all errors, except for EAGAIN which means "token not found right now" */
173 r = pkcs11_find_token(pkcs11_uri, pkcs11_crypt_device_callback, &data);
174 if (r < 0)
175 return r;
176
177 *ret_decrypted_key = TAKE_PTR(data.decrypted_key);
178 *ret_decrypted_key_size = data.decrypted_key_size;
179
180 return 0;
181 }
182
183 int acquire_luks2_key(
184 struct crypt_device *cd,
185 const char *json,
186 void *userdata,
187 const void *pin,
188 size_t pin_size,
189 char **ret_password,
190 size_t *ret_password_size) {
191
192 int r;
193 size_t decrypted_key_size, encrypted_key_size;
194 _cleanup_(erase_and_freep) void *decrypted_key = NULL;
195 _cleanup_(erase_and_freep) char *base64_encoded = NULL;
196 _cleanup_free_ char *pkcs11_uri = NULL;
197 _cleanup_free_ void *encrypted_key = NULL;
198 Pkcs11RsaPadding rsa_padding = PKCS11_RSA_PADDING_PKCS1V15;
199 systemd_pkcs11_plugin_params *pkcs11_params = userdata;
200 ssize_t base64_encoded_size;
201
202 assert(json);
203 assert(ret_password);
204 assert(ret_password_size);
205
206 r = parse_luks2_pkcs11_data(cd, json, &pkcs11_uri, &encrypted_key, &encrypted_key_size, &rsa_padding);
207 if (r < 0)
208 return r;
209
210 if (pkcs11_params && pin)
211 crypt_log_verbose(cd, "PIN parameter ignored in interactive mode.");
212
213 if (pkcs11_params) /* systemd based activation with interactive pin query callbacks */
214 r = acquire_luks2_key_systemd(
215 pkcs11_uri,
216 pkcs11_params,
217 encrypted_key, encrypted_key_size,
218 rsa_padding,
219 &decrypted_key, &decrypted_key_size);
220 else /* default activation that provides single PIN if needed */
221 r = acquire_luks2_key_by_pin(
222 cd, pkcs11_uri, pin, pin_size,
223 encrypted_key, encrypted_key_size,
224 rsa_padding,
225 &decrypted_key, &decrypted_key_size);
226 if (r < 0)
227 return r;
228
229 base64_encoded_size = base64mem(decrypted_key, decrypted_key_size, &base64_encoded);
230 if (base64_encoded_size < 0)
231 return crypt_log_error_errno(cd, (int) base64_encoded_size, "Cannot base64 encode key: %m");
232
233 *ret_password = TAKE_PTR(base64_encoded);
234 *ret_password_size = base64_encoded_size;
235
236 return 0;
237 }
238
239 int parse_luks2_pkcs11_data(
240 struct crypt_device *cd,
241 const char *json,
242 char **ret_uri,
243 void **ret_encrypted_key,
244 size_t *ret_encrypted_key_size,
245 Pkcs11RsaPadding *ret_rsa_padding) {
246
247 int r;
248 size_t key_size;
249 _cleanup_free_ char *uri = NULL;
250 _cleanup_free_ void *key = NULL;
251 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
252 sd_json_variant *w;
253 Pkcs11RsaPadding rsa_padding = PKCS11_RSA_PADDING_PKCS1V15;
254
255 assert(json);
256 assert(ret_uri);
257 assert(ret_encrypted_key);
258 assert(ret_encrypted_key_size);
259
260 r = sd_json_parse(json, SD_JSON_PARSE_MUST_BE_OBJECT, &v, /* reterr_line= */ NULL, /* reterr_column= */ NULL);
261 if (r < 0)
262 return r;
263
264 w = sd_json_variant_by_key(v, "pkcs11-uri");
265 if (!w)
266 return -EINVAL;
267
268 uri = strdup(sd_json_variant_string(w));
269 if (!uri)
270 return -ENOMEM;
271
272 w = sd_json_variant_by_key(v, "pkcs11-key");
273 if (!w)
274 return -EINVAL;
275
276 r = sd_json_variant_unbase64(w, &key, &key_size);
277 if (r < 0)
278 return crypt_log_debug_errno(cd, r, "Failed to decode base64 encoded key: %m.");
279
280 /* Optional padding-scheme tag. Absent in legacy tokens (which used PKCS#1 v1.5). */
281 w = sd_json_variant_by_key(v, "pkcs11-padding");
282 if (w) {
283 Pkcs11RsaPadding p;
284
285 if (!sd_json_variant_is_string(w))
286 return crypt_log_debug_errno(cd, -EINVAL,
287 "LUKS2 token field 'pkcs11-padding' is not a string.");
288 p = pkcs11_rsa_padding_from_string(sd_json_variant_string(w));
289 if (p < 0)
290 return crypt_log_debug_errno(cd, p,
291 "LUKS2 token field 'pkcs11-padding' has unsupported value '%s'.",
292 sd_json_variant_string(w));
293 rsa_padding = p;
294 }
295
296 *ret_uri = TAKE_PTR(uri);
297 *ret_encrypted_key = TAKE_PTR(key);
298 *ret_encrypted_key_size = key_size;
299 if (ret_rsa_padding)
300 *ret_rsa_padding = rsa_padding;
301
302 return 0;
303 }