Debian Codegraph @

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

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <libcryptsetup.h>
4
5 #include "alloc-util.h"
6 #include "cryptsetup-token-util.h"
7 #include "hexdecoct.h"
8 #include "json-util.h"
9 #include "luks2-fido2.h"
10 #include "memory-util.h"
11 #include "strv.h"
12
13 int acquire_luks2_key(
14 struct crypt_device *cd,
15 const char *json,
16 const char *device,
17 const char *pin,
18 char **ret_keyslot_passphrase,
19 size_t *ret_keyslot_passphrase_size) {
20
21 int r;
22 Fido2EnrollFlags required;
23 size_t cid_size, salt_size, decrypted_key_size;
24 _cleanup_free_ void *cid = NULL, *salt = NULL;
25 _cleanup_free_ char *rp_id = NULL;
26 _cleanup_(erase_and_freep) void *decrypted_key = NULL;
27 _cleanup_(erase_and_freep) char *base64_encoded = NULL;
28 _cleanup_strv_free_erase_ char **pins = NULL;
29 ssize_t base64_encoded_size;
30
31 assert(ret_keyslot_passphrase);
32 assert(ret_keyslot_passphrase_size);
33
34 r = parse_luks2_fido2_data(cd, json, &rp_id, &salt, &salt_size, &cid, &cid_size, &required);
35 if (r < 0)
36 return r;
37
38 if (pin) {
39 pins = strv_new(pin);
40 if (!pins)
41 return crypt_log_oom(cd);
42 }
43
44 /* configured to use pin but none was provided */
45 if ((required & FIDO2ENROLL_PIN) && strv_isempty(pins))
46 return -ENOANO;
47
48 r = fido2_use_hmac_hash(
49 device,
50 rp_id ?: "io.systemd.cryptsetup",
51 salt, salt_size,
52 cid, cid_size,
53 pins,
54 required,
55 &decrypted_key,
56 &decrypted_key_size);
57 if (r == -ENOLCK) /* libcryptsetup returns -ENOANO also on wrong PIN */
58 r = -ENOANO;
59 if (r < 0)
60 return r;
61
62 /* Before using this key as passphrase we base64 encode it, for compat with homed */
63 base64_encoded_size = base64mem(decrypted_key, decrypted_key_size, &base64_encoded);
64 if (base64_encoded_size < 0)
65 return crypt_log_error_errno(cd, (int) base64_encoded_size, "Failed to base64 encode key: %m");
66
67 *ret_keyslot_passphrase = TAKE_PTR(base64_encoded);
68 *ret_keyslot_passphrase_size = base64_encoded_size;
69
70 return 0;
71 }
72
73 /* this function expects valid "systemd-fido2" in json */
74 int parse_luks2_fido2_data(
75 struct crypt_device *cd,
76 const char *json,
77 char **ret_rp_id,
78 void **ret_salt,
79 size_t *ret_salt_size,
80 void **ret_cid,
81 size_t *ret_cid_size,
82 Fido2EnrollFlags *ret_required) {
83
84 _cleanup_free_ void *cid = NULL, *salt = NULL;
85 size_t cid_size = 0, salt_size = 0;
86 _cleanup_free_ char *rp = NULL;
87 int r;
88 _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
89 sd_json_variant *w;
90 Fido2EnrollFlags required = 0;
91
92 assert(json);
93 assert(ret_rp_id);
94 assert(ret_salt);
95 assert(ret_salt_size);
96 assert(ret_cid);
97 assert(ret_cid_size);
98 assert(ret_required);
99
100 r = sd_json_parse(json, SD_JSON_PARSE_MUST_BE_OBJECT, &v, /* reterr_line= */ NULL, /* reterr_column= */ NULL);
101 if (r < 0)
102 return crypt_log_error_errno(cd, r, "Failed to parse JSON token data: %m");
103
104 w = sd_json_variant_by_key(v, "fido2-credential");
105 if (!w)
106 return -EINVAL;
107
108 r = sd_json_variant_unbase64(w, &cid, &cid_size);
109 if (r < 0)
110 return crypt_log_error_errno(cd, r, "Failed to parse 'fido2-credentials' field: %m");
111
112 w = sd_json_variant_by_key(v, "fido2-salt");
113 if (!w)
114 return -EINVAL;
115
116 r = sd_json_variant_unbase64(w, &salt, &salt_size);
117 if (r < 0)
118 return crypt_log_error_errno(cd, r, "Failed to parse 'fido2-salt' field: %m");
119
120 w = sd_json_variant_by_key(v, "fido2-rp");
121 if (w) {
122 /* The "rp" field is optional. */
123 rp = strdup(sd_json_variant_string(w));
124 if (!rp) {
125 crypt_log_error(cd, "Not enough memory.");
126 return -ENOMEM;
127 }
128 }
129
130 w = sd_json_variant_by_key(v, "fido2-clientPin-required");
131 if (w)
132 /* The "fido2-clientPin-required" field is optional. */
133 SET_FLAG(required, FIDO2ENROLL_PIN, sd_json_variant_boolean(w));
134 else
135 required |= FIDO2ENROLL_PIN_IF_NEEDED; /* compat with 248, where the field was unset */
136
137 w = sd_json_variant_by_key(v, "fido2-up-required");
138 if (w)
139 /* The "fido2-up-required" field is optional. */
140 SET_FLAG(required, FIDO2ENROLL_UP, sd_json_variant_boolean(w));
141 else
142 required |= FIDO2ENROLL_UP_IF_NEEDED; /* compat with 248 */
143
144 w = sd_json_variant_by_key(v, "fido2-uv-required");
145 if (w)
146 /* The "fido2-uv-required" field is optional. */
147 SET_FLAG(required, FIDO2ENROLL_UV, sd_json_variant_boolean(w));
148 else
149 required |= FIDO2ENROLL_UV_OMIT; /* compat with 248 */
150
151 *ret_rp_id = TAKE_PTR(rp);
152 *ret_cid = TAKE_PTR(cid);
153 *ret_cid_size = cid_size;
154 *ret_salt = TAKE_PTR(salt);
155 *ret_salt_size = salt_size;
156 *ret_required = required;
157
158 return 0;
159 }