Debian Codegraph @

src/cryptsetup/cryptsetup-tokens/cryptsetup-token-systemd-fido2.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <libcryptsetup.h>
4 #include <string.h>
5 #include <syslog.h>
6
7 #include "sd-json.h"
8
9 #include "alloc-util.h"
10 #include "cryptsetup-token.h"
11 #include "cryptsetup-token-util.h"
12 #include "luks2-fido2.h"
13 #include "memory-util.h"
14 #include "version.h"
15
16 #define TOKEN_NAME "systemd-fido2"
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 int r;
35 const char *json;
36 _cleanup_(erase_and_freep) char *pin_string = NULL;
37
38 assert(pin || pin_size == 0);
39 assert(token >= 0);
40
41 r = DLOPEN_CRYPTSETUP(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED);
42 if (r < 0)
43 return r;
44
45 /* This must not fail at this moment (internal error) */
46 r = sym_crypt_token_json_get(cd, token, &json);
47 /* Use assert_se() here to avoid emitting warning with -DNDEBUG */
48 assert_se(token == r);
49 assert(json);
50
51 r = crypt_normalize_pin(pin, pin_size, &pin_string);
52 if (r < 0)
53 return crypt_log_debug_errno(cd, r, "Cannot normalize PIN: %m");
54
55 return acquire_luks2_key(cd, json, (const char *)usrptr, pin_string, password, password_len);
56 }
57
58 /*
59 * This function is called from within following libcryptsetup calls
60 * provided conditions further below are met:
61 *
62 * crypt_activate_by_token(), crypt_activate_by_token_type(type == 'systemd-fido2'):
63 *
64 * - token is assigned to at least one luks2 keyslot eligible to activate LUKS2 device
65 * (alternatively: name is set to null, flags contains CRYPT_ACTIVATE_ALLOW_UNBOUND_KEY
66 * and token is assigned to at least single keyslot).
67 *
68 * - if plugin defines validate function (see cryptsetup_token_validate below) it must have
69 * passed the check (aka return 0)
70 */
71 _public_ int cryptsetup_token_open(
72 struct crypt_device *cd, /* is always LUKS2 context */
73 int token /* is always >= 0 */,
74 char **password, /* freed by cryptsetup_token_buffer_free */
75 size_t *password_len,
76 void *usrptr /* plugin defined parameter passed to crypt_activate_by_token*() API */) {
77
78 return cryptsetup_token_open_pin(cd, token, NULL, 0, password, password_len, usrptr);
79 }
80
81 /*
82 * libcryptsetup callback for memory deallocation of 'password' parameter passed in
83 * any crypt_token_open_* plugin function
84 */
85 _public_ void cryptsetup_token_buffer_free(void *buffer, size_t buffer_len) {
86 erase_and_free(buffer);
87 }
88
89 /*
90 * prints systemd-fido2 token content in crypt_dump().
91 * 'type' and 'keyslots' fields are printed by libcryptsetup
92 */
93 _public_ void cryptsetup_token_dump(
94 struct crypt_device *cd /* is always LUKS2 context */,
95 const char *json /* validated 'systemd-fido2' token if cryptsetup_token_validate is defined */) {
96
97 int r;
98 Fido2EnrollFlags required;
99 size_t cid_size, salt_size;
100 const char *client_pin_req_str, *up_req_str, *uv_req_str;
101 _cleanup_free_ void *cid = NULL, *salt = NULL;
102 _cleanup_free_ char *rp_id = NULL, *cid_str = NULL, *salt_str = NULL;
103
104 assert(json);
105
106 if (DLOPEN_CRYPTSETUP(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED) < 0)
107 return;
108
109 r = parse_luks2_fido2_data(cd, json, &rp_id, &salt, &salt_size, &cid, &cid_size, &required);
110 if (r < 0)
111 return (void) crypt_log_debug_errno(cd, r, "Failed to parse " TOKEN_NAME " metadata: %m.");
112
113 r = crypt_dump_buffer_to_hex_string(cid, cid_size, &cid_str);
114 if (r < 0)
115 return (void) crypt_log_debug_errno(cd, r, "Cannot dump " TOKEN_NAME " content: %m");
116
117 r = crypt_dump_buffer_to_hex_string(salt, salt_size, &salt_str);
118 if (r < 0)
119 return (void) crypt_log_debug_errno(cd, r, "Cannot dump " TOKEN_NAME " content: %m");
120
121 if (required & FIDO2ENROLL_PIN)
122 client_pin_req_str = "true";
123 else if (required & FIDO2ENROLL_PIN_IF_NEEDED)
124 client_pin_req_str = NULL;
125 else
126 client_pin_req_str = "false";
127
128 if (required & FIDO2ENROLL_UP)
129 up_req_str = "true";
130 else if (required & FIDO2ENROLL_UP_IF_NEEDED)
131 up_req_str = NULL;
132 else
133 up_req_str = "false";
134
135 if (required & FIDO2ENROLL_UV)
136 uv_req_str = "true";
137 else if (required & FIDO2ENROLL_UV_OMIT)
138 uv_req_str = NULL;
139 else
140 uv_req_str = "false";
141
142 crypt_log(cd, "\tfido2-credential:" CRYPT_DUMP_LINE_SEP "%s\n", cid_str);
143 crypt_log(cd, "\tfido2-salt: %s\n", salt_str);
144
145 /* optional fields */
146 if (rp_id)
147 crypt_log(cd, "\tfido2-rp: %s\n", rp_id);
148 if (client_pin_req_str)
149 crypt_log(cd, "\tfido2-clientPin-required:" CRYPT_DUMP_LINE_SEP "%s\n",
150 client_pin_req_str);
151 if (up_req_str)
152 crypt_log(cd, "\tfido2-up-required:" CRYPT_DUMP_LINE_SEP "%s\n", up_req_str);
153 if (uv_req_str)
154 crypt_log(cd, "\tfido2-uv-required:" CRYPT_DUMP_LINE_SEP "%s\n", uv_req_str);
155 }
156
157 /*
158 * Note:
159 * If plugin is available in library path, it's called in before following libcryptsetup calls:
160 *
161 * crypt_token_json_set, crypt_dump, any crypt_activate_by_token_* flavour
162 */
163 _public_ int cryptsetup_token_validate(
164 struct crypt_device *cd, /* is always LUKS2 context */
165 const char *json /* contains valid 'type' and 'keyslots' fields. 'type' is 'systemd-fido2' */) {
166
167 int r;
168 sd_json_variant *w;
169 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
170
171 assert(json);
172
173 r = DLOPEN_CRYPTSETUP(LOG_DEBUG, SD_ELF_NOTE_DLOPEN_PRIORITY_REQUIRED);
174 if (r < 0)
175 return r;
176
177 r = sd_json_parse(json, SD_JSON_PARSE_MUST_BE_OBJECT, &v, /* reterr_line= */ NULL, /* reterr_column= */ NULL);
178 if (r < 0)
179 return crypt_log_debug_errno(cd, r, "Could not parse " TOKEN_NAME " json object: %m.");
180
181 w = sd_json_variant_by_key(v, "fido2-credential");
182 if (!w) {
183 crypt_log_debug(cd, "FIDO2 token data lacks 'fido2-credential' field.");
184 return 1;
185 }
186
187 r = sd_json_variant_unbase64(w, NULL, NULL);
188 if (r < 0)
189 return crypt_log_debug_errno(cd, r, "Invalid base64 data in 'fido2-credential' field: %m");
190
191 w = sd_json_variant_by_key(v, "fido2-salt");
192 if (!w) {
193 crypt_log_debug(cd, "FIDO2 token data lacks 'fido2-salt' field.");
194 return 1;
195 }
196
197 r = sd_json_variant_unbase64(w, NULL, NULL);
198 if (r < 0)
199 return crypt_log_debug_errno(cd, r, "Failed to decode base64 encoded salt: %m.");
200
201 /* The "rp" field is optional. */
202 w = sd_json_variant_by_key(v, "fido2-rp");
203 if (w && !sd_json_variant_is_string(w)) {
204 crypt_log_debug(cd, "FIDO2 token data's 'fido2-rp' field is not a string.");
205 return 1;
206 }
207
208 /* The "fido2-clientPin-required" field is optional. */
209 w = sd_json_variant_by_key(v, "fido2-clientPin-required");
210 if (w && !sd_json_variant_is_boolean(w)) {
211 crypt_log_debug(cd, "FIDO2 token data's 'fido2-clientPin-required' field is not a boolean.");
212 return 1;
213 }
214
215 /* The "fido2-up-required" field is optional. */
216 w = sd_json_variant_by_key(v, "fido2-up-required");
217 if (w && !sd_json_variant_is_boolean(w)) {
218 crypt_log_debug(cd, "FIDO2 token data's 'fido2-up-required' field is not a boolean.");
219 return 1;
220 }
221
222 /* The "fido2-uv-required" field is optional. */
223 w = sd_json_variant_by_key(v, "fido2-uv-required");
224 if (w && !sd_json_variant_is_boolean(w)) {
225 crypt_log_debug(cd, "FIDO2 token data's 'fido2-uv-required' field is not a boolean.");
226 return 1;
227 }
228
229 return 0;
230 }