• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 23273450309

18 Mar 2026 11:04PM UTC coverage: 72.618% (+0.04%) from 72.575%
23273450309

push

github

web-flow
userdb: add birthDate field to JSON user records (#40954)

Add an optional field that can be used to store a user's birth date.
userdb already stores personal metadata (`emailAddress`, `realName`,
`location`) so `birthDate` is a natural fit.

80 of 96 new or added lines in 8 files covered. (83.33%)

5187 existing lines in 82 files now uncovered.

316431 of 435750 relevant lines covered (72.62%)

1196544.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.35
/src/shared/cryptsetup-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <stdlib.h>
4

5
#include "sd-dlopen.h"
6
#include "sd-json.h"
7

8
#include "alloc-util.h"
9
#include "cryptsetup-util.h"
10
#include "dlfcn-util.h"
11
#include "escape.h"
12
#include "hexdecoct.h"
13
#include "hmac.h"
14
#include "log.h"
15
#include "parse-util.h"
16
#include "string-util.h"
17
#include "strv.h"
18

19
#if HAVE_LIBCRYPTSETUP
20
static void *cryptsetup_dl = NULL;
21

22
DLSYM_PROTOTYPE(crypt_activate_by_passphrase) = NULL;
23
DLSYM_PROTOTYPE(crypt_activate_by_signed_key) = NULL;
24
DLSYM_PROTOTYPE(crypt_activate_by_volume_key) = NULL;
25
DLSYM_PROTOTYPE(crypt_deactivate_by_name) = NULL;
26
DLSYM_PROTOTYPE(crypt_format) = NULL;
27
DLSYM_PROTOTYPE(crypt_free) = NULL;
28
DLSYM_PROTOTYPE(crypt_get_cipher) = NULL;
29
DLSYM_PROTOTYPE(crypt_get_cipher_mode) = NULL;
30
DLSYM_PROTOTYPE(crypt_get_data_offset) = NULL;
31
DLSYM_PROTOTYPE(crypt_get_device_name) = NULL;
32
DLSYM_PROTOTYPE(crypt_get_dir) = NULL;
33
DLSYM_PROTOTYPE(crypt_get_type) = NULL;
34
DLSYM_PROTOTYPE(crypt_get_uuid) = NULL;
35
DLSYM_PROTOTYPE(crypt_get_verity_info) = NULL;
36
DLSYM_PROTOTYPE(crypt_get_volume_key_size) = NULL;
37
DLSYM_PROTOTYPE(crypt_header_restore) = NULL;
38
DLSYM_PROTOTYPE(crypt_init) = NULL;
39
DLSYM_PROTOTYPE(crypt_init_by_name) = NULL;
40
DLSYM_PROTOTYPE(crypt_keyslot_add_by_volume_key) = NULL;
41
DLSYM_PROTOTYPE(crypt_keyslot_destroy) = NULL;
42
DLSYM_PROTOTYPE(crypt_keyslot_max) = NULL;
43
DLSYM_PROTOTYPE(crypt_load) = NULL;
44
DLSYM_PROTOTYPE(crypt_metadata_locking) = NULL;
45
DLSYM_PROTOTYPE(crypt_reencrypt_init_by_passphrase) = NULL;
46
DLSYM_PROTOTYPE(crypt_reencrypt_run);
47
DLSYM_PROTOTYPE(crypt_resize) = NULL;
48
DLSYM_PROTOTYPE(crypt_resume_by_volume_key) = NULL;
49
DLSYM_PROTOTYPE(crypt_set_data_device) = NULL;
50
DLSYM_PROTOTYPE(crypt_set_data_offset) = NULL;
51
DLSYM_PROTOTYPE(crypt_set_debug_level) = NULL;
52
DLSYM_PROTOTYPE(crypt_set_log_callback) = NULL;
53
DLSYM_PROTOTYPE(crypt_set_metadata_size) = NULL;
54
DLSYM_PROTOTYPE(crypt_set_pbkdf_type) = NULL;
55
DLSYM_PROTOTYPE(crypt_suspend) = NULL;
56
DLSYM_PROTOTYPE(crypt_token_json_get) = NULL;
57
DLSYM_PROTOTYPE(crypt_token_json_set) = NULL;
58
DLSYM_PROTOTYPE(crypt_token_max) = NULL;
59
#if HAVE_CRYPT_TOKEN_SET_EXTERNAL_PATH
60
DLSYM_PROTOTYPE(crypt_token_set_external_path) = NULL;
61
#endif
62
DLSYM_PROTOTYPE(crypt_token_status) = NULL;
63
DLSYM_PROTOTYPE(crypt_volume_key_get) = NULL;
64
DLSYM_PROTOTYPE(crypt_volume_key_keyring) = NULL;
65
DLSYM_PROTOTYPE(crypt_wipe) = NULL;
66
DLSYM_PROTOTYPE(crypt_get_integrity_info) = NULL;
67

68
static void cryptsetup_log_glue(int level, const char *msg, void *usrptr) {
34,643✔
69

70
        switch (level) {
34,643✔
71
        case CRYPT_LOG_NORMAL:
72
                level = LOG_NOTICE;
73
                break;
74
        case CRYPT_LOG_ERROR:
41✔
75
                level = LOG_ERR;
41✔
76
                break;
41✔
77
        case CRYPT_LOG_VERBOSE:
×
78
                level = LOG_INFO;
×
UNCOV
79
                break;
×
80
        case CRYPT_LOG_DEBUG:
34,602✔
81
                level = LOG_DEBUG;
34,602✔
82
                break;
34,602✔
83
        default:
UNCOV
84
                log_error("Unknown libcryptsetup log level: %d", level);
×
85
                level = LOG_ERR;
86
        }
87

88
        log_full(level, "%s", msg);
34,643✔
89
}
34,643✔
90

91
void cryptsetup_enable_logging(struct crypt_device *cd) {
12,125✔
92
        /* It's OK to call this with a NULL parameter, in which case libcryptsetup will set the default log
93
         * function.
94
         *
95
         * Note that this is also called from dlopen_cryptsetup(), which we call here too. Sounds like an
96
         * endless loop, but isn't because we break it via the check for 'cryptsetup_dl' early in
97
         * dlopen_cryptsetup(). */
98

99
        if (dlopen_cryptsetup() < 0)
12,125✔
100
                return; /* If this fails, let's gracefully ignore the issue, this is just debug logging after
101
                         * all, and if this failed we already generated a debug log message that should help
102
                         * to track things down. */
103

104
        sym_crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
12,125✔
105
        sym_crypt_set_debug_level(DEBUG_LOGGING ? CRYPT_DEBUG_ALL : CRYPT_DEBUG_NONE);
12,195✔
106
}
107

108
int cryptsetup_set_minimal_pbkdf(struct crypt_device *cd) {
45✔
109

110
        /* With CRYPT_PBKDF_NO_BENCHMARK flag set .time_ms member is ignored
111
         * while .iterations must be set at least to recommended minimum value. */
112

113
        static const struct crypt_pbkdf_type minimal_pbkdf = {
45✔
114
                .hash = "sha512",
115
                .type = CRYPT_KDF_PBKDF2,
116
                .iterations = 1000, /* recommended minimum count for pbkdf2
117
                                     * according to NIST SP 800-132, ch. 5.2 */
118
                .flags = CRYPT_PBKDF_NO_BENCHMARK
119
        };
120

121
        int r;
45✔
122

123
        /* Sets a minimal PKBDF in case we already have a high entropy key. */
124

125
        r = dlopen_cryptsetup();
45✔
126
        if (r < 0)
45✔
127
                return r;
128

129
        r = sym_crypt_set_pbkdf_type(cd, &minimal_pbkdf);
45✔
130
        if (r < 0)
45✔
UNCOV
131
                return r;
×
132

133
        return 0;
134
}
135

136
int cryptsetup_get_token_as_json(
5,621✔
137
                struct crypt_device *cd,
138
                int idx,
139
                const char *verify_type,
140
                sd_json_variant **ret) {
141

142
        _cleanup_(sd_json_variant_unrefp) sd_json_variant *v = NULL;
5,621✔
143
        const char *text;
5,621✔
144
        int r;
5,621✔
145

146
        assert(cd);
5,621✔
147

148
        /* Extracts and parses the LUKS2 JSON token data from a LUKS2 device. Optionally verifies the type of
149
         * the token. Returns:
150
         *
151
         *      -EINVAL → token index out of range or "type" field missing
152
         *      -ENOENT → token doesn't exist
153
         * -EMEDIUMTYPE → "verify_type" specified and doesn't match token's type
154
         */
155

156
        r = dlopen_cryptsetup();
5,621✔
157
        if (r < 0)
5,621✔
158
                return r;
159

160
        r = sym_crypt_token_json_get(cd, idx, &text);
5,621✔
161
        if (r < 0)
5,621✔
162
                return r;
163

164
        r = sd_json_parse(text, 0, &v, NULL, NULL);
403✔
165
        if (r < 0)
403✔
166
                return r;
167

168
        if (verify_type) {
403✔
169
                sd_json_variant *w;
240✔
170

171
                w = sd_json_variant_by_key(v, "type");
240✔
172
                if (!w)
240✔
173
                        return -EINVAL;
174

175
                if (!streq_ptr(sd_json_variant_string(w), verify_type))
240✔
176
                        return -EMEDIUMTYPE;
177
        }
178

179
        if (ret)
179✔
180
                *ret = TAKE_PTR(v);
179✔
181

182
        return 0;
183
}
184

185
int cryptsetup_add_token_json(struct crypt_device *cd, sd_json_variant *v) {
45✔
186
        _cleanup_free_ char *text = NULL;
45✔
187
        int r;
45✔
188

189
        r = dlopen_cryptsetup();
45✔
190
        if (r < 0)
45✔
191
                return r;
192

193
        r = sd_json_variant_format(v, 0, &text);
45✔
194
        if (r < 0)
45✔
UNCOV
195
                return log_debug_errno(r, "Failed to format token data for LUKS: %m");
×
196

197
        log_debug("Adding token text <%s>", text);
45✔
198

199
        r = sym_crypt_token_json_set(cd, CRYPT_ANY_TOKEN, text);
45✔
200
        if (r < 0)
45✔
UNCOV
201
                return log_debug_errno(r, "Failed to write token data to LUKS: %m");
×
202

203
        return 0;
204
}
205

206
int cryptsetup_get_volume_key_prefix(
7✔
207
                struct crypt_device *cd,
208
                const char *volume_name,
209
                char **ret) {
210

211
        _cleanup_free_ char *volume = NULL;
7✔
212
        const char *uuid;
7✔
213
        char *s;
7✔
214

215
        uuid = sym_crypt_get_uuid(cd);
7✔
216
        if (!uuid)
7✔
UNCOV
217
                return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to get LUKS UUID.");
×
218

219
        if (volume_name)
7✔
220
                /* avoid ambiguity around ":" once we join things below */
221
                volume = xescape(volume_name, ":");
7✔
222
        else
UNCOV
223
                volume = strjoin("luks-", uuid);
×
224
        if (!volume)
7✔
UNCOV
225
                return log_oom_debug();
×
226

227
        s = strjoin("cryptsetup:", volume, ":", uuid);
7✔
228
        if (!s)
7✔
UNCOV
229
                return log_oom_debug();
×
230

231
        *ret = s;
7✔
232

233
        return 0;
7✔
234
}
235

236
/* The hash must match what measure_volume_key() extends to the SHA256 bank of the TPM2. */
237
int cryptsetup_get_volume_key_id(
7✔
238
                struct crypt_device *cd,
239
                const char *volume_name,
240
                const void *volume_key,
241
                size_t volume_key_size,
242
                char **ret) {
243

244
        _cleanup_free_ char *prefix = NULL;
7✔
245
        uint8_t digest[SHA256_DIGEST_SIZE];
7✔
246
        char *hex;
7✔
247
        int r;
7✔
248

249
        r = cryptsetup_get_volume_key_prefix(cd, volume_name, &prefix);
7✔
250
        if (r < 0)
7✔
UNCOV
251
                return log_debug_errno(r, "Failed to get LUKS volume key prefix.");
×
252

253
        hmac_sha256(volume_key, volume_key_size, prefix, strlen(prefix), digest);
7✔
254

255
        hex = hexmem(digest, sizeof(digest));
7✔
256
        if (!hex)
7✔
UNCOV
257
                return log_oom_debug();
×
258

259
        *ret = hex;
7✔
260

261
        return 0;
7✔
262
}
263
#endif
264

265
int dlopen_cryptsetup(void) {
30,762✔
266
#if HAVE_LIBCRYPTSETUP
267
        int r;
30,762✔
268

269
        /* libcryptsetup added crypt_reencrypt() in 2.2.0, and marked it obsolete in 2.4.0, replacing it with
270
         * crypt_reencrypt_run(), which takes one extra argument but is otherwise identical. The old call is
271
         * still available though, and given we want to support 2.2.0 for a while longer, we'll use the old
272
         * symbol if the new one is not available. */
273

274
        SD_ELF_NOTE_DLOPEN(
30,762✔
275
                        "cryptsetup",
276
                        "Support for disk encryption, integrity, and authentication",
277
                        SD_ELF_NOTE_DLOPEN_PRIORITY_SUGGESTED,
278
                        "libcryptsetup.so.12");
279

280
        r = dlopen_many_sym_or_warn(
30,762✔
281
                        &cryptsetup_dl, "libcryptsetup.so.12", LOG_DEBUG,
282
                        DLSYM_ARG(crypt_activate_by_passphrase),
283
                        DLSYM_ARG(crypt_activate_by_signed_key),
284
                        DLSYM_ARG(crypt_activate_by_volume_key),
285
                        DLSYM_ARG(crypt_deactivate_by_name),
286
                        DLSYM_ARG(crypt_format),
287
                        DLSYM_ARG(crypt_free),
288
                        DLSYM_ARG(crypt_get_cipher),
289
                        DLSYM_ARG(crypt_get_cipher_mode),
290
                        DLSYM_ARG(crypt_get_data_offset),
291
                        DLSYM_ARG(crypt_get_device_name),
292
                        DLSYM_ARG(crypt_get_dir),
293
                        DLSYM_ARG(crypt_get_type),
294
                        DLSYM_ARG(crypt_get_uuid),
295
                        DLSYM_ARG(crypt_get_verity_info),
296
                        DLSYM_ARG(crypt_get_volume_key_size),
297
                        DLSYM_ARG(crypt_header_restore),
298
                        DLSYM_ARG(crypt_init),
299
                        DLSYM_ARG(crypt_init_by_name),
300
                        DLSYM_ARG(crypt_keyslot_add_by_volume_key),
301
                        DLSYM_ARG(crypt_keyslot_destroy),
302
                        DLSYM_ARG(crypt_keyslot_max),
303
                        DLSYM_ARG(crypt_load),
304
                        DLSYM_ARG(crypt_metadata_locking),
305
                        DLSYM_ARG(crypt_reencrypt_init_by_passphrase),
306
                        DLSYM_ARG(crypt_reencrypt_run),
307
                        DLSYM_ARG(crypt_resize),
308
                        DLSYM_ARG(crypt_resume_by_volume_key),
309
                        DLSYM_ARG(crypt_set_data_device),
310
                        DLSYM_ARG(crypt_set_data_offset),
311
                        DLSYM_ARG(crypt_set_debug_level),
312
                        DLSYM_ARG(crypt_set_log_callback),
313
                        DLSYM_ARG(crypt_set_metadata_size),
314
                        DLSYM_ARG(crypt_set_pbkdf_type),
315
                        DLSYM_ARG(crypt_suspend),
316
                        DLSYM_ARG(crypt_token_json_get),
317
                        DLSYM_ARG(crypt_token_json_set),
318
                        DLSYM_ARG(crypt_token_max),
319
#if HAVE_CRYPT_TOKEN_SET_EXTERNAL_PATH
320
                        DLSYM_ARG(crypt_token_set_external_path),
321
#endif
322
                        DLSYM_ARG(crypt_token_status),
323
                        DLSYM_ARG(crypt_volume_key_get),
324
                        DLSYM_ARG(crypt_volume_key_keyring),
325
                        DLSYM_ARG(crypt_wipe),
326
                        DLSYM_ARG(crypt_get_integrity_info));
327
        if (r <= 0)
30,762✔
328
                return r;
329

330
        /* Redirect the default logging calls of libcryptsetup to our own logging infra. (Note that
331
         * libcryptsetup also maintains per-"struct crypt_device" log functions, which we'll also set
332
         * whenever allocating a "struct crypt_device" context. Why set both? To be defensive: maybe some
333
         * other code loaded into this process also changes the global log functions of libcryptsetup, who
334
         * knows? And if so, we still want our own objects to log via our own infra, at the very least.) */
335
        cryptsetup_enable_logging(NULL);
11,195✔
336

337
        const char *e = secure_getenv("SYSTEMD_CRYPTSETUP_TOKEN_PATH");
11,195✔
338
        if (e) {
11,195✔
339
#if HAVE_CRYPT_TOKEN_SET_EXTERNAL_PATH
340
                r = sym_crypt_token_set_external_path(e);
×
UNCOV
341
                if (r < 0)
×
UNCOV
342
                        log_debug_errno(r, "Failed to set the libcryptsetup external token path to '%s', ignoring: %m", e);
×
343
#else
344
                log_debug("libcryptsetup version does not support setting the external token path, not setting it to '%s'.", e);
345
#endif
346
        }
347

348
        return 1;
349
#else
350
        return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "cryptsetup support is not compiled in.");
351
#endif
352
}
353

354
int cryptsetup_get_keyslot_from_token(sd_json_variant *v) {
16✔
355
        int keyslot, r;
16✔
356
        sd_json_variant *w;
16✔
357

358
        /* Parses the "keyslots" field of a LUKS2 token object. The field can be an array, but here we assume
359
         * that it contains a single element only, since that's the only way we ever generate it
360
         * ourselves. */
361

362
        w = sd_json_variant_by_key(v, "keyslots");
16✔
363
        if (!w)
16✔
364
                return -ENOENT;
16✔
365
        if (!sd_json_variant_is_array(w) || sd_json_variant_elements(w) != 1)
16✔
UNCOV
366
                return -EMEDIUMTYPE;
×
367

368
        w = sd_json_variant_by_index(w, 0);
16✔
369
        if (!w)
16✔
370
                return -ENOENT;
371
        if (!sd_json_variant_is_string(w))
16✔
372
                return -EMEDIUMTYPE;
373

374
        r = safe_atoi(sd_json_variant_string(w), &keyslot);
16✔
375
        if (r < 0)
16✔
376
                return r;
377
        if (keyslot < 0)
16✔
UNCOV
378
                return -EINVAL;
×
379

380
        return keyslot;
381
}
382

383
const char* mangle_none(const char *s) {
200✔
384
        /* A helper that turns cryptsetup/integritysetup/veritysetup "options" strings into NULL if they are effectively empty */
385
        return isempty(s) || STR_IN_SET(s, "-", "none") ? NULL : s;
400✔
386
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc