]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/creds-util.c
Merge pull request #23575 from keszybz/logind-wall-message-cleanup
[thirdparty/systemd.git] / src / shared / creds-util.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <sys/file.h>
4
5 #if HAVE_OPENSSL
6 #include <openssl/err.h>
7 #endif
8
9 #include "sd-id128.h"
10
11 #include "blockdev-util.h"
12 #include "chattr-util.h"
13 #include "creds-util.h"
14 #include "efi-api.h"
15 #include "env-util.h"
16 #include "fd-util.h"
17 #include "fileio.h"
18 #include "fs-util.h"
19 #include "io-util.h"
20 #include "memory-util.h"
21 #include "mkdir.h"
22 #include "openssl-util.h"
23 #include "path-util.h"
24 #include "random-util.h"
25 #include "sparse-endian.h"
26 #include "stat-util.h"
27 #include "tpm2-util.h"
28 #include "virt.h"
29
30 bool credential_name_valid(const char *s) {
31 /* We want that credential names are both valid in filenames (since that's our primary way to pass
32 * them around) and as fdnames (which is how we might want to pass them around eventually) */
33 return filename_is_valid(s) && fdname_is_valid(s);
34 }
35
36 static int get_credentials_dir_internal(const char *envvar, const char **ret) {
37 const char *e;
38
39 assert(ret);
40
41 e = secure_getenv(envvar);
42 if (!e)
43 return -ENXIO;
44
45 if (!path_is_absolute(e) || !path_is_normalized(e))
46 return -EINVAL;
47
48 *ret = e;
49 return 0;
50 }
51
52 int get_credentials_dir(const char **ret) {
53 return get_credentials_dir_internal("CREDENTIALS_DIRECTORY", ret);
54 }
55
56 int get_encrypted_credentials_dir(const char **ret) {
57 return get_credentials_dir_internal("ENCRYPTED_CREDENTIALS_DIRECTORY", ret);
58 }
59
60 int read_credential(const char *name, void **ret, size_t *ret_size) {
61 _cleanup_free_ char *fn = NULL;
62 const char *d;
63 int r;
64
65 assert(ret);
66
67 if (!credential_name_valid(name))
68 return -EINVAL;
69
70 r = get_credentials_dir(&d);
71 if (r < 0)
72 return r;
73
74 fn = path_join(d, name);
75 if (!fn)
76 return -ENOMEM;
77
78 return read_full_file_full(
79 AT_FDCWD, fn,
80 UINT64_MAX, SIZE_MAX,
81 READ_FULL_FILE_SECURE,
82 NULL,
83 (char**) ret, ret_size);
84 }
85
86 #if HAVE_OPENSSL
87
88 #define CREDENTIAL_HOST_SECRET_SIZE 4096
89
90 static const sd_id128_t credential_app_id =
91 SD_ID128_MAKE(d3,ac,ec,ba,0d,ad,4c,df,b8,c9,38,15,28,93,6c,58);
92
93 struct credential_host_secret_format {
94 /* The hashed machine ID of the machine this belongs to. Why? We want to ensure that each machine
95 * gets its own secret, even if people forget to flush out this secret file. Hence we bind it to the
96 * machine ID, for which there's hopefully a better chance it will be flushed out. We use a hashed
97 * machine ID instead of the literal one, because it's trivial to, and it might be a good idea not
98 * being able to directly associate a secret key file with a host. */
99 sd_id128_t machine_id;
100
101 /* The actual secret key */
102 uint8_t data[CREDENTIAL_HOST_SECRET_SIZE];
103 } _packed_;
104
105 static void warn_not_encrypted(int fd, CredentialSecretFlags flags, const char *dirname, const char *filename) {
106 int r;
107
108 assert(fd >= 0);
109 assert(dirname);
110 assert(filename);
111
112 if (!FLAGS_SET(flags, CREDENTIAL_SECRET_WARN_NOT_ENCRYPTED))
113 return;
114
115 r = fd_is_encrypted(fd);
116 if (r < 0)
117 log_debug_errno(r, "Failed to determine if credential secret file '%s/%s' is encrypted.",
118 dirname, filename);
119 else if (r == 0)
120 log_warning("Credential secret file '%s/%s' is not located on encrypted media, using anyway.",
121 dirname, filename);
122 }
123
124 static int make_credential_host_secret(
125 int dfd,
126 const sd_id128_t machine_id,
127 CredentialSecretFlags flags,
128 const char *dirname,
129 const char *fn,
130 void **ret_data,
131 size_t *ret_size) {
132
133 struct credential_host_secret_format buf;
134 _cleanup_free_ char *t = NULL;
135 _cleanup_close_ int fd = -1;
136 int r;
137
138 assert(dfd >= 0);
139 assert(fn);
140
141 fd = openat(dfd, ".", O_CLOEXEC|O_WRONLY|O_TMPFILE, 0400);
142 if (fd < 0) {
143 log_debug_errno(errno, "Failed to create temporary credential file with O_TMPFILE, proceeding without: %m");
144
145 if (asprintf(&t, "credential.secret.%016" PRIx64, random_u64()) < 0)
146 return -ENOMEM;
147
148 fd = openat(dfd, t, O_CLOEXEC|O_WRONLY|O_CREAT|O_EXCL|O_NOFOLLOW, 0400);
149 if (fd < 0)
150 return -errno;
151 }
152
153 r = chattr_secret(fd, 0);
154 if (r < 0)
155 log_debug_errno(r, "Failed to set file attributes for secrets file, ignoring: %m");
156
157 buf = (struct credential_host_secret_format) {
158 .machine_id = machine_id,
159 };
160
161 r = crypto_random_bytes(buf.data, sizeof(buf.data));
162 if (r < 0)
163 goto finish;
164
165 r = loop_write(fd, &buf, sizeof(buf), false);
166 if (r < 0)
167 goto finish;
168
169 if (fsync(fd) < 0) {
170 r = -errno;
171 goto finish;
172 }
173
174 warn_not_encrypted(fd, flags, dirname, fn);
175
176 if (t) {
177 r = rename_noreplace(dfd, t, dfd, fn);
178 if (r < 0)
179 goto finish;
180
181 t = mfree(t);
182 } else if (linkat(fd, "", dfd, fn, AT_EMPTY_PATH) < 0) {
183 r = -errno;
184 goto finish;
185 }
186
187 if (fsync(dfd) < 0) {
188 r = -errno;
189 goto finish;
190 }
191
192 if (ret_data) {
193 void *copy;
194
195 copy = memdup(buf.data, sizeof(buf.data));
196 if (!copy) {
197 r = -ENOMEM;
198 goto finish;
199 }
200
201 *ret_data = copy;
202 }
203
204 if (ret_size)
205 *ret_size = sizeof(buf.data);
206
207 r = 0;
208
209 finish:
210 if (t && unlinkat(dfd, t, 0) < 0)
211 log_debug_errno(errno, "Failed to remove temporary credential key: %m");
212
213 explicit_bzero_safe(&buf, sizeof(buf));
214 return r;
215 }
216
217 int get_credential_host_secret(CredentialSecretFlags flags, void **ret, size_t *ret_size) {
218 _cleanup_free_ char *_dirname = NULL, *_filename = NULL;
219 _cleanup_close_ int dfd = -1;
220 sd_id128_t machine_id;
221 const char *dirname, *filename;
222 int r;
223
224 r = sd_id128_get_machine_app_specific(credential_app_id, &machine_id);
225 if (r < 0)
226 return r;
227
228 const char *e = secure_getenv("SYSTEMD_CREDENTIAL_SECRET");
229 if (e) {
230 if (!path_is_normalized(e))
231 return -EINVAL;
232 if (!path_is_absolute(e))
233 return -EINVAL;
234
235 r = path_extract_directory(e, &_dirname);
236 if (r < 0)
237 return r;
238
239 r = path_extract_filename(e, &_filename);
240 if (r < 0)
241 return r;
242
243 dirname = _dirname;
244 filename = _filename;
245 } else {
246 dirname = "/var/lib/systemd";
247 filename = "credential.secret";
248 }
249
250 mkdir_parents(dirname, 0755);
251 dfd = open_mkdir_at(AT_FDCWD, dirname, O_CLOEXEC, 0755);
252 if (dfd < 0)
253 return log_debug_errno(dfd, "Failed to create or open directory '%s': %m", dirname);
254
255 if (FLAGS_SET(flags, CREDENTIAL_SECRET_FAIL_ON_TEMPORARY_FS)) {
256 r = fd_is_temporary_fs(dfd);
257 if (r < 0)
258 return log_debug_errno(r, "Failed to check directory '%s': %m", dirname);
259 if (r > 0)
260 return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM),
261 "Directory '%s' is on a temporary file system, refusing.", dirname);
262 }
263
264 for (unsigned attempt = 0;; attempt++) {
265 _cleanup_(erase_and_freep) struct credential_host_secret_format *f = NULL;
266 _cleanup_close_ int fd = -1;
267 size_t l = 0;
268 ssize_t n = 0;
269 struct stat st;
270
271 if (attempt >= 3) /* Somebody is playing games with us */
272 return log_debug_errno(SYNTHETIC_ERRNO(EIO),
273 "All attempts to create secret store in %s failed.", dirname);
274
275 fd = openat(dfd, filename, O_CLOEXEC|O_RDONLY|O_NOCTTY|O_NOFOLLOW);
276 if (fd < 0) {
277 if (errno != ENOENT || !FLAGS_SET(flags, CREDENTIAL_SECRET_GENERATE))
278 return log_debug_errno(errno,
279 "Failed to open %s/%s: %m", dirname, filename);
280
281
282 r = make_credential_host_secret(dfd, machine_id, flags, dirname, filename, ret, ret_size);
283 if (r == -EEXIST) {
284 log_debug_errno(r, "Credential secret %s/%s appeared while we were creating it, rereading.",
285 dirname, filename);
286 continue;
287 }
288 if (r < 0)
289 return log_debug_errno(r, "Failed to create credential secret %s/%s: %m",
290 dirname, filename);
291 return 0;
292 }
293
294 if (fstat(fd, &st) < 0)
295 return log_debug_errno(errno, "Failed to stat %s/%s: %m", dirname, filename);
296
297 r = stat_verify_regular(&st);
298 if (r < 0)
299 return log_debug_errno(r, "%s/%s is not a regular file: %m", dirname, filename);
300 if (st.st_nlink == 0) /* Deleted by now, try again */
301 continue;
302 if (st.st_nlink > 1)
303 /* Our deletion check won't work if hardlinked somewhere else */
304 return log_debug_errno(SYNTHETIC_ERRNO(EPERM),
305 "%s/%s has too many links, refusing.",
306 dirname, filename);
307 if ((st.st_mode & 07777) != 0400)
308 /* Don't use file if not 0400 access mode */
309 return log_debug_errno(SYNTHETIC_ERRNO(EPERM),
310 "%s/%s has permissive access mode, refusing.",
311 dirname, filename);
312 l = st.st_size;
313 if (l < offsetof(struct credential_host_secret_format, data) + 1)
314 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL),
315 "%s/%s is too small, refusing.", dirname, filename);
316 if (l > 16*1024*1024)
317 return log_debug_errno(SYNTHETIC_ERRNO(E2BIG),
318 "%s/%s is too big, refusing.", dirname, filename);
319
320 f = malloc(l+1);
321 if (!f)
322 return log_oom_debug();
323
324 n = read(fd, f, l+1);
325 if (n < 0)
326 return log_debug_errno(errno,
327 "Failed to read %s/%s: %m", dirname, filename);
328 if ((size_t) n != l) /* What? The size changed? */
329 return log_debug_errno(SYNTHETIC_ERRNO(EIO),
330 "Failed to read %s/%s: %m", dirname, filename);
331
332 if (sd_id128_equal(machine_id, f->machine_id)) {
333 size_t sz;
334
335 warn_not_encrypted(fd, flags, dirname, filename);
336
337 sz = l - offsetof(struct credential_host_secret_format, data);
338 assert(sz > 0);
339
340 if (ret) {
341 void *copy;
342
343 assert(sz <= sizeof(f->data)); /* Ensure we don't read past f->data bounds */
344
345 copy = memdup(f->data, sz);
346 if (!copy)
347 return log_oom_debug();
348
349 *ret = copy;
350 }
351
352 if (ret_size)
353 *ret_size = sz;
354
355 return 0;
356 }
357
358 /* Hmm, this secret is from somewhere else. Let's delete the file. Let's first acquire a lock
359 * to ensure we are the only ones accessing the file while we delete it. */
360
361 if (flock(fd, LOCK_EX) < 0)
362 return log_debug_errno(errno,
363 "Failed to flock %s/%s: %m", dirname, filename);
364
365 /* Before we delete it check that the file is still linked into the file system */
366 if (fstat(fd, &st) < 0)
367 return log_debug_errno(errno, "Failed to stat %s/%s: %m", dirname, filename);
368 if (st.st_nlink == 0) /* Already deleted by now? */
369 continue;
370 if (st.st_nlink != 1) /* Safety check, someone is playing games with us */
371 return log_debug_errno(SYNTHETIC_ERRNO(EPERM),
372 "%s/%s unexpectedly has too many links.",
373 dirname, filename);
374 if (unlinkat(dfd, filename, 0) < 0)
375 return log_debug_errno(errno, "Failed to unlink %s/%s: %m", dirname, filename);
376
377 /* And now try again */
378 }
379 }
380
381 /* Construction is like this:
382 *
383 * A symmetric encryption key is derived from:
384 *
385 * 1. Either the "host" key (a key stored in /var/lib/credential.secret)
386 *
387 * 2. A key generated by letting the TPM2 calculate an HMAC hash of some nonce we pass to it, keyed
388 * by a key derived from its internal seed key.
389 *
390 * 3. The concatenation of the above.
391 *
392 * 4. Or a fixed "empty" key. This will not provide confidentiality or authenticity, of course, but is
393 * useful to encode credentials for the initrd on TPM-less systems, where we simply have no better
394 * concept to bind things to. Note that decryption of a key set up like this will be refused on
395 * systems that have a TPM and have SecureBoot enabled.
396 *
397 * The above is hashed with SHA256 which is then used as encryption key for AES256-GCM. The encrypted
398 * credential is a short (unencrypted) header describing which of the three keys to use, the IV to use for
399 * AES256-GCM and some more meta information (sizes of certain objects) that is strictly speaking redundant,
400 * but kinda nice to have since we can have a more generic parser. If the TPM2 key is used this is followed
401 * by another (unencrypted) header, with information about the TPM2 policy used (specifically: the PCR mask
402 * to bind against, and a hash of the resulting policy — the latter being redundant, but speeding up things a
403 * bit, since we can more quickly refuse PCR state), followed by a sealed/exported TPM2 HMAC key. This is
404 * then followed by the encrypted data, which begins with a metadata header (which contains validity
405 * timestamps as well as the credential name), followed by the actual credential payload. The file ends in
406 * the AES256-GCM tag. To make things simple, the AES256-GCM AAD covers the main and the TPM2 header in
407 * full. This means the whole file is either protected by AAD, or is ciphertext, or is the tag. No
408 * unprotected data is included.
409 */
410
411 struct _packed_ encrypted_credential_header {
412 sd_id128_t id;
413 le32_t key_size;
414 le32_t block_size;
415 le32_t iv_size;
416 le32_t tag_size;
417 uint8_t iv[];
418 /* Followed by NUL bytes until next 8 byte boundary */
419 };
420
421 struct _packed_ tpm2_credential_header {
422 le64_t pcr_mask; /* Note that the spec for PC Clients only mandates 24 PCRs, and that's what systems
423 * generally have. But keep the door open for more. */
424 le16_t pcr_bank; /* For now, either TPM2_ALG_SHA256 or TPM2_ALG_SHA1 */
425 le16_t primary_alg; /* Primary key algorithm (either TPM2_ALG_RSA or TPM2_ALG_ECC for now) */
426 le32_t blob_size;
427 le32_t policy_hash_size;
428 uint8_t policy_hash_and_blob[];
429 /* Followed by NUL bytes until next 8 byte boundary */
430 };
431
432 struct _packed_ metadata_credential_header {
433 le64_t timestamp;
434 le64_t not_after;
435 le32_t name_size;
436 char name[];
437 /* Followed by NUL bytes until next 8 byte boundary */
438 };
439
440 /* Some generic limit for parts of the encrypted credential for which we don't know the right size ahead of
441 * time, but where we are really sure it won't be larger than this. Should be larger than any possible IV,
442 * padding, tag size and so on. This is purely used for early filtering out of invalid sizes. */
443 #define CREDENTIAL_FIELD_SIZE_MAX (16U*1024U)
444
445 static int sha256_hash_host_and_tpm2_key(
446 const void *host_key,
447 size_t host_key_size,
448 const void *tpm2_key,
449 size_t tpm2_key_size,
450 uint8_t ret[static SHA256_DIGEST_LENGTH]) {
451
452 _cleanup_(EVP_MD_CTX_freep) EVP_MD_CTX *md = NULL;
453 unsigned l;
454
455 assert(host_key_size == 0 || host_key);
456 assert(tpm2_key_size == 0 || tpm2_key);
457 assert(ret);
458
459 /* Combines the host key and the TPM2 HMAC hash into a SHA256 hash value we'll use as symmetric encryption key. */
460
461 md = EVP_MD_CTX_new();
462 if (!md)
463 return log_oom();
464
465 if (EVP_DigestInit_ex(md, EVP_sha256(), NULL) != 1)
466 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to initial SHA256 context.");
467
468 if (host_key && EVP_DigestUpdate(md, host_key, host_key_size) != 1)
469 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to hash host key.");
470
471 if (tpm2_key && EVP_DigestUpdate(md, tpm2_key, tpm2_key_size) != 1)
472 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to hash TPM2 key.");
473
474 assert(EVP_MD_CTX_size(md) == SHA256_DIGEST_LENGTH);
475
476 if (EVP_DigestFinal_ex(md, ret, &l) != 1)
477 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to finalize SHA256 hash.");
478
479 assert(l == SHA256_DIGEST_LENGTH);
480 return 0;
481 }
482
483 int encrypt_credential_and_warn(
484 sd_id128_t with_key,
485 const char *name,
486 usec_t timestamp,
487 usec_t not_after,
488 const char *tpm2_device,
489 uint32_t tpm2_pcr_mask,
490 const void *input,
491 size_t input_size,
492 void **ret,
493 size_t *ret_size) {
494
495 _cleanup_(EVP_CIPHER_CTX_freep) EVP_CIPHER_CTX *context = NULL;
496 _cleanup_(erase_and_freep) void *host_key = NULL, *tpm2_key = NULL;
497 size_t host_key_size = 0, tpm2_key_size = 0, tpm2_blob_size = 0, tpm2_policy_hash_size = 0, output_size, p, ml;
498 _cleanup_free_ void *tpm2_blob = NULL, *tpm2_policy_hash = NULL, *iv = NULL, *output = NULL;
499 _cleanup_free_ struct metadata_credential_header *m = NULL;
500 uint16_t tpm2_pcr_bank = 0, tpm2_primary_alg = 0;
501 struct encrypted_credential_header *h;
502 int ksz, bsz, ivsz, tsz, added, r;
503 uint8_t md[SHA256_DIGEST_LENGTH];
504 const EVP_CIPHER *cc;
505 sd_id128_t id;
506
507 assert(input || input_size == 0);
508 assert(ret);
509 assert(ret_size);
510
511 if (!sd_id128_in_set(with_key,
512 _CRED_AUTO,
513 _CRED_AUTO_INITRD,
514 CRED_AES256_GCM_BY_HOST,
515 CRED_AES256_GCM_BY_TPM2_HMAC,
516 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC,
517 CRED_AES256_GCM_BY_TPM2_ABSENT))
518 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid key type: " SD_ID128_FORMAT_STR, SD_ID128_FORMAT_VAL(with_key));
519
520 if (name && !credential_name_valid(name))
521 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Invalid credential name: %s", name);
522
523 if (not_after != USEC_INFINITY && timestamp != USEC_INFINITY && not_after < timestamp)
524 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Credential is invalidated before it is valid (" USEC_FMT " < " USEC_FMT ").", not_after, timestamp);
525
526 if (DEBUG_LOGGING) {
527 char buf[FORMAT_TIMESTAMP_MAX];
528
529 if (name)
530 log_debug("Including credential name '%s' in encrypted credential.", name);
531 if (timestamp != USEC_INFINITY)
532 log_debug("Including timestamp '%s' in encrypted credential.", format_timestamp(buf, sizeof(buf), timestamp));
533 if (not_after != USEC_INFINITY)
534 log_debug("Including not-after timestamp '%s' in encrypted credential.", format_timestamp(buf, sizeof(buf), not_after));
535 }
536
537 if (sd_id128_in_set(with_key,
538 _CRED_AUTO,
539 CRED_AES256_GCM_BY_HOST,
540 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC)) {
541
542 r = get_credential_host_secret(
543 CREDENTIAL_SECRET_GENERATE|
544 CREDENTIAL_SECRET_WARN_NOT_ENCRYPTED|
545 (sd_id128_equal(with_key, _CRED_AUTO) ? CREDENTIAL_SECRET_FAIL_ON_TEMPORARY_FS : 0),
546 &host_key,
547 &host_key_size);
548 if (r == -ENOMEDIUM && sd_id128_equal(with_key, _CRED_AUTO))
549 log_debug_errno(r, "Credential host secret location on temporary file system, not using.");
550 else if (r < 0)
551 return log_error_errno(r, "Failed to determine local credential host secret: %m");
552 }
553
554 #if HAVE_TPM2
555 bool try_tpm2;
556 if (sd_id128_equal(with_key, _CRED_AUTO)) {
557 /* If automatic mode is selected and we are running in a container, let's not try TPM2. OTOH
558 * if user picks TPM2 explicitly, let's always honour the request and try. */
559
560 r = detect_container();
561 if (r < 0)
562 log_debug_errno(r, "Failed to determine whether we are running in a container, ignoring: %m");
563 else if (r > 0)
564 log_debug("Running in container, not attempting to use TPM2.");
565
566 try_tpm2 = r <= 0;
567 } else if (sd_id128_equal(with_key, _CRED_AUTO_INITRD)) {
568 /* If automatic mode for initrds is selected, we'll use the TPM2 key if the firmware does it,
569 * otherwise we'll use a fixed key */
570
571 try_tpm2 = efi_has_tpm2();
572 if (!try_tpm2)
573 log_debug("Firmware lacks TPM2 support, not attempting to use TPM2.");
574 } else
575 try_tpm2 = sd_id128_in_set(with_key, CRED_AES256_GCM_BY_TPM2_HMAC, CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC);
576
577 if (try_tpm2) {
578 r = tpm2_seal(tpm2_device,
579 tpm2_pcr_mask,
580 NULL,
581 &tpm2_key,
582 &tpm2_key_size,
583 &tpm2_blob,
584 &tpm2_blob_size,
585 &tpm2_policy_hash,
586 &tpm2_policy_hash_size,
587 &tpm2_pcr_bank,
588 &tpm2_primary_alg);
589 if (r < 0) {
590 if (sd_id128_equal(with_key, _CRED_AUTO_INITRD))
591 log_warning("Firmware reported a TPM2 being present and used, but we didn't manage to talk to it. Credential will be refused if SecureBoot is enabled.");
592 else if (!sd_id128_equal(with_key, _CRED_AUTO))
593 return r;
594
595 log_notice_errno(r, "TPM2 sealing didn't work, continuing without TPM2: %m");
596 }
597
598 assert(tpm2_blob_size <= CREDENTIAL_FIELD_SIZE_MAX);
599 assert(tpm2_policy_hash_size <= CREDENTIAL_FIELD_SIZE_MAX);
600 }
601 #endif
602
603 if (sd_id128_in_set(with_key, _CRED_AUTO, _CRED_AUTO_INITRD)) {
604 /* Let's settle the key type in auto mode now. */
605
606 if (host_key && tpm2_key)
607 id = CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC;
608 else if (tpm2_key)
609 id = CRED_AES256_GCM_BY_TPM2_HMAC;
610 else if (host_key)
611 id = CRED_AES256_GCM_BY_HOST;
612 else if (sd_id128_equal(with_key, _CRED_AUTO_INITRD))
613 id = CRED_AES256_GCM_BY_TPM2_ABSENT;
614 else
615 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE),
616 "TPM2 not available and host key located on temporary file system, no encryption key available.");
617 } else
618 id = with_key;
619
620 if (sd_id128_equal(id, CRED_AES256_GCM_BY_TPM2_ABSENT))
621 log_warning("Using a null key for encryption and signing. Confidentiality or authenticity will not be provided.");
622
623 /* Let's now take the host key and the TPM2 key and hash it together, to use as encryption key for the data */
624 r = sha256_hash_host_and_tpm2_key(host_key, host_key_size, tpm2_key, tpm2_key_size, md);
625 if (r < 0)
626 return r;
627
628 assert_se(cc = EVP_aes_256_gcm());
629
630 ksz = EVP_CIPHER_key_length(cc);
631 assert(ksz == sizeof(md));
632
633 bsz = EVP_CIPHER_block_size(cc);
634 assert(bsz > 0);
635 assert((size_t) bsz <= CREDENTIAL_FIELD_SIZE_MAX);
636
637 ivsz = EVP_CIPHER_iv_length(cc);
638 if (ivsz > 0) {
639 assert((size_t) ivsz <= CREDENTIAL_FIELD_SIZE_MAX);
640
641 iv = malloc(ivsz);
642 if (!iv)
643 return log_oom();
644
645 r = crypto_random_bytes(iv, ivsz);
646 if (r < 0)
647 return log_error_errno(r, "Failed to acquired randomized IV: %m");
648 }
649
650 tsz = 16; /* FIXME: On OpenSSL 3 there is EVP_CIPHER_CTX_get_tag_length(), until then let's hardcode this */
651
652 context = EVP_CIPHER_CTX_new();
653 if (!context)
654 return log_error_errno(SYNTHETIC_ERRNO(ENOMEM), "Failed to allocate encryption object: %s",
655 ERR_error_string(ERR_get_error(), NULL));
656
657 if (EVP_EncryptInit_ex(context, cc, NULL, md, iv) != 1)
658 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to initialize encryption context: %s",
659 ERR_error_string(ERR_get_error(), NULL));
660
661 /* Just an upper estimate */
662 output_size =
663 ALIGN8(offsetof(struct encrypted_credential_header, iv) + ivsz) +
664 ALIGN8(tpm2_key ? offsetof(struct tpm2_credential_header, policy_hash_and_blob) + tpm2_blob_size + tpm2_policy_hash_size : 0) +
665 ALIGN8(offsetof(struct metadata_credential_header, name) + strlen_ptr(name)) +
666 input_size + 2U * (size_t) bsz +
667 tsz;
668
669 output = malloc0(output_size);
670 if (!output)
671 return log_oom();
672
673 h = (struct encrypted_credential_header*) output;
674 h->id = id;
675 h->block_size = htole32(bsz);
676 h->key_size = htole32(ksz);
677 h->tag_size = htole32(tsz);
678 h->iv_size = htole32(ivsz);
679 memcpy(h->iv, iv, ivsz);
680
681 p = ALIGN8(offsetof(struct encrypted_credential_header, iv) + ivsz);
682
683 if (tpm2_key) {
684 struct tpm2_credential_header *t;
685
686 t = (struct tpm2_credential_header*) ((uint8_t*) output + p);
687 t->pcr_mask = htole64(tpm2_pcr_mask);
688 t->pcr_bank = htole16(tpm2_pcr_bank);
689 t->primary_alg = htole16(tpm2_primary_alg);
690 t->blob_size = htole32(tpm2_blob_size);
691 t->policy_hash_size = htole32(tpm2_policy_hash_size);
692 memcpy(t->policy_hash_and_blob, tpm2_blob, tpm2_blob_size);
693 memcpy(t->policy_hash_and_blob + tpm2_blob_size, tpm2_policy_hash, tpm2_policy_hash_size);
694
695 p += ALIGN8(offsetof(struct tpm2_credential_header, policy_hash_and_blob) + tpm2_blob_size + tpm2_policy_hash_size);
696 }
697
698 /* Pass the encrypted + TPM2 header as AAD */
699 if (EVP_EncryptUpdate(context, NULL, &added, output, p) != 1)
700 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to write AAD data: %s",
701 ERR_error_string(ERR_get_error(), NULL));
702
703 /* Now construct the metadata header */
704 ml = strlen_ptr(name);
705 m = malloc0(ALIGN8(offsetof(struct metadata_credential_header, name) + ml));
706 if (!m)
707 return log_oom();
708
709 m->timestamp = htole64(timestamp);
710 m->not_after = htole64(not_after);
711 m->name_size = htole32(ml);
712 memcpy_safe(m->name, name, ml);
713
714 /* And encrypt the metadata header */
715 if (EVP_EncryptUpdate(context, (uint8_t*) output + p, &added, (const unsigned char*) m, ALIGN8(offsetof(struct metadata_credential_header, name) + ml)) != 1)
716 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to encrypt metadata header: %s",
717 ERR_error_string(ERR_get_error(), NULL));
718
719 assert(added >= 0);
720 assert((size_t) added <= output_size - p);
721 p += added;
722
723 /* Then encrypt the plaintext */
724 if (EVP_EncryptUpdate(context, (uint8_t*) output + p, &added, input, input_size) != 1)
725 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to encrypt data: %s",
726 ERR_error_string(ERR_get_error(), NULL));
727
728 assert(added >= 0);
729 assert((size_t) added <= output_size - p);
730 p += added;
731
732 /* Finalize */
733 if (EVP_EncryptFinal_ex(context, (uint8_t*) output + p, &added) != 1)
734 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to finalize data encryption: %s",
735 ERR_error_string(ERR_get_error(), NULL));
736
737 assert(added >= 0);
738 assert((size_t) added <= output_size - p);
739 p += added;
740
741 assert(p <= output_size - tsz);
742
743 /* Append tag */
744 if (EVP_CIPHER_CTX_ctrl(context, EVP_CTRL_GCM_GET_TAG, tsz, (uint8_t*) output + p) != 1)
745 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to get tag: %s",
746 ERR_error_string(ERR_get_error(), NULL));
747
748 p += tsz;
749 assert(p <= output_size);
750
751 if (DEBUG_LOGGING && input_size > 0) {
752 size_t base64_size;
753
754 base64_size = DIV_ROUND_UP(p * 4, 3); /* Include base64 size increase in debug output */
755 assert(base64_size >= input_size);
756 log_debug("Input of %zu bytes grew to output of %zu bytes (+%2zu%%).", input_size, base64_size, base64_size * 100 / input_size - 100);
757 }
758
759 *ret = TAKE_PTR(output);
760 *ret_size = p;
761
762 return 0;
763 }
764
765 int decrypt_credential_and_warn(
766 const char *validate_name,
767 usec_t validate_timestamp,
768 const char *tpm2_device,
769 const void *input,
770 size_t input_size,
771 void **ret,
772 size_t *ret_size) {
773
774 _cleanup_(erase_and_freep) void *host_key = NULL, *tpm2_key = NULL, *plaintext = NULL;
775 _cleanup_(EVP_CIPHER_CTX_freep) EVP_CIPHER_CTX *context = NULL;
776 size_t host_key_size = 0, tpm2_key_size = 0, plaintext_size, p, hs;
777 struct encrypted_credential_header *h;
778 struct metadata_credential_header *m;
779 uint8_t md[SHA256_DIGEST_LENGTH];
780 bool with_tpm2, with_host_key, is_tpm2_absent;
781 const EVP_CIPHER *cc;
782 int r, added;
783
784 assert(input || input_size == 0);
785 assert(ret);
786 assert(ret_size);
787
788 h = (struct encrypted_credential_header*) input;
789
790 /* The ID must fit in, for the current and all future formats */
791 if (input_size < sizeof(h->id))
792 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Encrypted file too short.");
793
794 with_host_key = sd_id128_in_set(h->id, CRED_AES256_GCM_BY_HOST, CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC);
795 with_tpm2 = sd_id128_in_set(h->id, CRED_AES256_GCM_BY_TPM2_HMAC, CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC);
796 is_tpm2_absent = sd_id128_equal(h->id, CRED_AES256_GCM_BY_TPM2_ABSENT);
797
798 if (!with_host_key && !with_tpm2 && !is_tpm2_absent)
799 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Unknown encryption format, or corrupted data: %m");
800
801 if (is_tpm2_absent) {
802 /* So this is a credential encrypted with a zero length key. We support this to cover for the
803 * case where neither a host key not a TPM2 are available (specifically: initrd environments
804 * where the host key is not yet accessible and no TPM2 chip exists at all), to minimize
805 * different codeflow for TPM2 and non-TPM2 codepaths. Of course, credentials encoded this
806 * way offer no confidentiality nor authenticity. Because of that it's important we refuse to
807 * use them on systems that actually *do* have a TPM2 chip – if we are in SecureBoot
808 * mode. Otherwise an attacker could hand us credentials like this and we'd use them thinking
809 * they are trusted, even though they are not. */
810
811 if (efi_has_tpm2()) {
812 if (is_efi_secure_boot())
813 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG),
814 "Credential uses fixed key for fallback use when TPM2 is absent — but TPM2 is present, and SecureBoot is enabled, refusing.");
815
816 log_warning("Credential uses fixed key for use when TPM2 is absent, but TPM2 is present! Accepting anyway, since SecureBoot is disabled.");
817 } else
818 log_debug("Credential uses fixed key for use when TPM2 is absent, and TPM2 indeed is absent. Accepting.");
819 }
820
821 /* Now we know the minimum header size */
822 if (input_size < offsetof(struct encrypted_credential_header, iv))
823 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Encrypted file too short.");
824
825 /* Verify some basic header values */
826 if (le32toh(h->key_size) != sizeof(md))
827 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Unexpected key size in header.");
828 if (le32toh(h->block_size) <= 0 || le32toh(h->block_size) > CREDENTIAL_FIELD_SIZE_MAX)
829 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Unexpected block size in header.");
830 if (le32toh(h->iv_size) > CREDENTIAL_FIELD_SIZE_MAX)
831 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "IV size too large.");
832 if (le32toh(h->tag_size) != 16) /* FIXME: On OpenSSL 3, let's verify via EVP_CIPHER_CTX_get_tag_length() */
833 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Unexpected tag size in header.");
834
835 /* Ensure we have space for the full header now (we don't know the size of the name hence this is a
836 * lower limit only) */
837 if (input_size <
838 ALIGN8(offsetof(struct encrypted_credential_header, iv) + le32toh(h->iv_size)) +
839 ALIGN8((with_tpm2 ? offsetof(struct tpm2_credential_header, policy_hash_and_blob) : 0)) +
840 ALIGN8(offsetof(struct metadata_credential_header, name)) +
841 le32toh(h->tag_size))
842 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Encrypted file too short.");
843
844 p = ALIGN8(offsetof(struct encrypted_credential_header, iv) + le32toh(h->iv_size));
845
846 if (with_tpm2) {
847 #if HAVE_TPM2
848 struct tpm2_credential_header* t = (struct tpm2_credential_header*) ((uint8_t*) input + p);
849
850 if (le64toh(t->pcr_mask) >= (UINT64_C(1) << TPM2_PCRS_MAX))
851 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "TPM2 PCR mask out of range.");
852 if (!tpm2_pcr_bank_to_string(le16toh(t->pcr_bank)))
853 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "TPM2 PCR bank invalid or not supported");
854 if (!tpm2_primary_alg_to_string(le16toh(t->primary_alg)))
855 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "TPM2 primary key algorithm invalid or not supported.");
856 if (le32toh(t->blob_size) > CREDENTIAL_FIELD_SIZE_MAX)
857 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Unexpected TPM2 blob size.");
858 if (le32toh(t->policy_hash_size) > CREDENTIAL_FIELD_SIZE_MAX)
859 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Unexpected TPM2 policy hash size.");
860
861 /* Ensure we have space for the full TPM2 header now (still don't know the name, and its size
862 * though, hence still just a lower limit test only) */
863 if (input_size <
864 ALIGN8(offsetof(struct encrypted_credential_header, iv) + le32toh(h->iv_size)) +
865 ALIGN8(offsetof(struct tpm2_credential_header, policy_hash_and_blob) + le32toh(t->blob_size) + le32toh(t->policy_hash_size)) +
866 ALIGN8(offsetof(struct metadata_credential_header, name)) +
867 le32toh(h->tag_size))
868 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Encrypted file too short.");
869
870 r = tpm2_unseal(tpm2_device,
871 le64toh(t->pcr_mask),
872 le16toh(t->pcr_bank),
873 le16toh(t->primary_alg),
874 t->policy_hash_and_blob,
875 le32toh(t->blob_size),
876 t->policy_hash_and_blob + le32toh(t->blob_size),
877 le32toh(t->policy_hash_size),
878 NULL,
879 &tpm2_key,
880 &tpm2_key_size);
881 if (r < 0)
882 return r;
883
884 p += ALIGN8(offsetof(struct tpm2_credential_header, policy_hash_and_blob) +
885 le32toh(t->blob_size) +
886 le32toh(t->policy_hash_size));
887 #else
888 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Credential requires TPM2 support, but TPM2 support not available.");
889 #endif
890 }
891
892 if (with_host_key) {
893 r = get_credential_host_secret(
894 0,
895 &host_key,
896 &host_key_size);
897 if (r < 0)
898 return log_error_errno(r, "Failed to determine local credential key: %m");
899 }
900
901 if (is_tpm2_absent)
902 log_warning("Warning: using a null key for decryption and authentication. Confidentiality or authenticity are not provided.");
903
904 sha256_hash_host_and_tpm2_key(host_key, host_key_size, tpm2_key, tpm2_key_size, md);
905
906 assert_se(cc = EVP_aes_256_gcm());
907
908 /* Make sure cipher expectations match the header */
909 if (EVP_CIPHER_key_length(cc) != (int) le32toh(h->key_size))
910 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Unexpected key size in header.");
911 if (EVP_CIPHER_block_size(cc) != (int) le32toh(h->block_size))
912 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Unexpected block size in header.");
913
914 context = EVP_CIPHER_CTX_new();
915 if (!context)
916 return log_error_errno(SYNTHETIC_ERRNO(ENOMEM), "Failed to allocate decryption object: %s",
917 ERR_error_string(ERR_get_error(), NULL));
918
919 if (EVP_DecryptInit_ex(context, cc, NULL, NULL, NULL) != 1)
920 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to initialize decryption context: %s",
921 ERR_error_string(ERR_get_error(), NULL));
922
923 if (EVP_CIPHER_CTX_ctrl(context, EVP_CTRL_GCM_SET_IVLEN, le32toh(h->iv_size), NULL) != 1)
924 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to set IV size on decryption context: %s",
925 ERR_error_string(ERR_get_error(), NULL));
926
927 if (EVP_DecryptInit_ex(context, NULL, NULL, md, h->iv) != 1)
928 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to set IV and key: %s",
929 ERR_error_string(ERR_get_error(), NULL));
930
931 if (EVP_DecryptUpdate(context, NULL, &added, input, p) != 1)
932 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to write AAD data: %s",
933 ERR_error_string(ERR_get_error(), NULL));
934
935 plaintext = malloc(input_size - p - le32toh(h->tag_size));
936 if (!plaintext)
937 return -ENOMEM;
938
939 if (EVP_DecryptUpdate(
940 context,
941 plaintext,
942 &added,
943 (uint8_t*) input + p,
944 input_size - p - le32toh(h->tag_size)) != 1)
945 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to decrypt data: %s",
946 ERR_error_string(ERR_get_error(), NULL));
947
948 assert(added >= 0);
949 assert((size_t) added <= input_size - p - le32toh(h->tag_size));
950 plaintext_size = added;
951
952 if (EVP_CIPHER_CTX_ctrl(context, EVP_CTRL_GCM_SET_TAG, le32toh(h->tag_size), (uint8_t*) input + input_size - le32toh(h->tag_size)) != 1)
953 return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Failed to set tag: %s",
954 ERR_error_string(ERR_get_error(), NULL));
955
956 if (EVP_DecryptFinal_ex(context, (uint8_t*) plaintext + plaintext_size, &added) != 1)
957 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Decryption failed (incorrect key?): %s",
958 ERR_error_string(ERR_get_error(), NULL));
959
960 plaintext_size += added;
961
962 if (plaintext_size < ALIGN8(offsetof(struct metadata_credential_header, name)))
963 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Metadata header incomplete.");
964
965 m = plaintext;
966
967 if (le64toh(m->timestamp) != USEC_INFINITY &&
968 le64toh(m->not_after) != USEC_INFINITY &&
969 le64toh(m->timestamp) >= le64toh(m->not_after))
970 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Timestamps of credential are not in order, refusing.");
971
972 if (le32toh(m->name_size) > CREDENTIAL_NAME_MAX)
973 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Embedded credential name too long, refusing.");
974
975 hs = ALIGN8(offsetof(struct metadata_credential_header, name) + le32toh(m->name_size));
976 if (plaintext_size < hs)
977 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Metadata header incomplete.");
978
979 if (le32toh(m->name_size) > 0) {
980 _cleanup_free_ char *embedded_name = NULL;
981
982 if (memchr(m->name, 0, le32toh(m->name_size)))
983 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Embedded credential name contains NUL byte, refusing.");
984
985 embedded_name = memdup_suffix0(m->name, le32toh(m->name_size));
986 if (!embedded_name)
987 return log_oom();
988
989 if (!credential_name_valid(embedded_name))
990 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG), "Embedded credential name is not valid, refusing.");
991
992 if (validate_name && !streq(embedded_name, validate_name)) {
993
994 r = getenv_bool_secure("SYSTEMD_CREDENTIAL_VALIDATE_NAME");
995 if (r < 0 && r != -ENXIO)
996 log_debug_errno(r, "Failed to parse $SYSTEMD_CREDENTIAL_VALIDATE_NAME: %m");
997 if (r != 0)
998 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE), "Embedded credential name '%s' does not match filename '%s', refusing.", embedded_name, validate_name);
999
1000 log_debug("Embedded credential name '%s' does not match expected name '%s', but configured to use credential anyway.", embedded_name, validate_name);
1001 }
1002 }
1003
1004 if (validate_timestamp != USEC_INFINITY) {
1005 if (le64toh(m->timestamp) != USEC_INFINITY && le64toh(m->timestamp) > validate_timestamp)
1006 log_debug("Credential timestamp is from the future, assuming clock skew.");
1007
1008 if (le64toh(m->not_after) != USEC_INFINITY && le64toh(m->not_after) < validate_timestamp) {
1009
1010 r = getenv_bool_secure("SYSTEMD_CREDENTIAL_VALIDATE_NOT_AFTER");
1011 if (r < 0 && r != -ENXIO)
1012 log_debug_errno(r, "Failed to parse $SYSTEMD_CREDENTIAL_VALIDATE_NOT_AFTER: %m");
1013 if (r != 0)
1014 return log_error_errno(SYNTHETIC_ERRNO(ESTALE), "Credential's time passed, refusing to use.");
1015
1016 log_debug("Credential not-after timestamp has passed, but configured to use credential anyway.");
1017 }
1018 }
1019
1020 if (ret) {
1021 char *without_metadata;
1022
1023 without_metadata = memdup((uint8_t*) plaintext + hs, plaintext_size - hs);
1024 if (!without_metadata)
1025 return log_oom();
1026
1027 *ret = without_metadata;
1028 }
1029
1030 if (ret_size)
1031 *ret_size = plaintext_size - hs;
1032
1033 return 0;
1034 }
1035
1036 #else
1037
1038 int get_credential_host_secret(CredentialSecretFlags flags, void **ret, size_t *ret_size) {
1039 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Support for encrypted credentials not available.");
1040 }
1041
1042 int encrypt_credential_and_warn(sd_id128_t with_key, const char *name, usec_t timestamp, usec_t not_after, const char *tpm2_device, uint32_t tpm2_pcr_mask, const void *input, size_t input_size, void **ret, size_t *ret_size) {
1043 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Support for encrypted credentials not available.");
1044 }
1045
1046 int decrypt_credential_and_warn(const char *validate_name, usec_t validate_timestamp, const char *tpm2_device, const void *input, size_t input_size, void **ret, size_t *ret_size) {
1047 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Support for encrypted credentials not available.");
1048 }
1049
1050 #endif