1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
6 #include <openssl/err.h>
11 #include "blockdev-util.h"
12 #include "chattr-util.h"
13 #include "creds-util.h"
20 #include "memory-util.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"
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
);
36 int get_credentials_dir(const char **ret
) {
41 e
= secure_getenv("CREDENTIALS_DIRECTORY");
45 if (!path_is_absolute(e
) || !path_is_normalized(e
))
52 int read_credential(const char *name
, void **ret
, size_t *ret_size
) {
53 _cleanup_free_
char *fn
= NULL
;
59 if (!credential_name_valid(name
))
62 r
= get_credentials_dir(&d
);
66 fn
= path_join(d
, name
);
70 return read_full_file_full(
73 READ_FULL_FILE_SECURE
,
75 (char**) ret
, ret_size
);
80 #define CREDENTIAL_HOST_SECRET_SIZE 4096
82 static const sd_id128_t credential_app_id
=
83 SD_ID128_MAKE(d3
,ac
,ec
,ba
,0d
,ad
,4c
,df
,b8
,c9
,38,15,28,93,6c
,58);
85 struct credential_host_secret_format
{
86 /* The hashed machine ID of the machine this belongs to. Why? We want to ensure that each machine
87 * gets its own secret, even if people forget to flush out this secret file. Hence we bind it to the
88 * machine ID, for which there's hopefully a better chance it will be flushed out. We use a hashed
89 * machine ID instead of the literal one, because it's trivial to, and it might be a good idea not
90 * being able to directly associate a secret key file with a host. */
91 sd_id128_t machine_id
;
93 /* The actual secret key */
94 uint8_t data
[CREDENTIAL_HOST_SECRET_SIZE
];
97 static void warn_not_encrypted(int fd
, CredentialSecretFlags flags
, const char *dirname
, const char *filename
) {
104 if (!FLAGS_SET(flags
, CREDENTIAL_SECRET_WARN_NOT_ENCRYPTED
))
107 r
= fd_is_encrypted(fd
);
109 log_debug_errno(r
, "Failed to determine if credential secret file '%s/%s' is encrypted.",
112 log_warning("Credential secret file '%s/%s' is not located on encrypted media, using anyway.",
116 static int make_credential_host_secret(
118 const sd_id128_t machine_id
,
119 CredentialSecretFlags flags
,
125 struct credential_host_secret_format buf
;
126 _cleanup_free_
char *t
= NULL
;
127 _cleanup_close_
int fd
= -1;
133 fd
= openat(dfd
, ".", O_CLOEXEC
|O_WRONLY
|O_TMPFILE
, 0400);
135 log_debug_errno(errno
, "Failed to create temporary credential file with O_TMPFILE, proceeding without: %m");
137 if (asprintf(&t
, "credential.secret.%016" PRIx64
, random_u64()) < 0)
140 fd
= openat(dfd
, t
, O_CLOEXEC
|O_WRONLY
|O_CREAT
|O_EXCL
|O_NOFOLLOW
, 0400);
145 r
= chattr_secret(fd
, 0);
147 log_debug_errno(r
, "Failed to set file attributes for secrets file, ignoring: %m");
149 buf
= (struct credential_host_secret_format
) {
150 .machine_id
= machine_id
,
153 r
= genuine_random_bytes(buf
.data
, sizeof(buf
.data
), RANDOM_BLOCK
);
157 r
= loop_write(fd
, &buf
, sizeof(buf
), false);
166 warn_not_encrypted(fd
, flags
, dirname
, fn
);
169 r
= rename_noreplace(dfd
, t
, dfd
, fn
);
174 } else if (linkat(fd
, "", dfd
, fn
, AT_EMPTY_PATH
) < 0) {
179 if (fsync(dfd
) < 0) {
187 copy
= memdup(buf
.data
, sizeof(buf
.data
));
197 *ret_size
= sizeof(buf
.data
);
202 if (t
&& unlinkat(dfd
, t
, 0) < 0)
203 log_debug_errno(errno
, "Failed to remove temporary credential key: %m");
205 explicit_bzero_safe(&buf
, sizeof(buf
));
209 int get_credential_host_secret(CredentialSecretFlags flags
, void **ret
, size_t *ret_size
) {
210 _cleanup_free_
char *_dirname
= NULL
, *_filename
= NULL
;
211 _cleanup_close_
int dfd
= -1;
212 sd_id128_t machine_id
;
213 const char *dirname
, *filename
;
216 r
= sd_id128_get_machine_app_specific(credential_app_id
, &machine_id
);
220 const char *e
= secure_getenv("SYSTEMD_CREDENTIAL_SECRET");
222 if (!path_is_normalized(e
))
224 if (!path_is_absolute(e
))
227 r
= path_extract_directory(e
, &_dirname
);
231 r
= path_extract_filename(e
, &_filename
);
236 filename
= _filename
;
238 dirname
= "/var/lib/systemd";
239 filename
= "credential.secret";
242 mkdir_parents(dirname
, 0755);
243 dfd
= open_mkdir_at(AT_FDCWD
, dirname
, O_CLOEXEC
, 0755);
245 return log_debug_errno(dfd
, "Failed to create or open directory '%s': %m", dirname
);
247 if (FLAGS_SET(flags
, CREDENTIAL_SECRET_FAIL_ON_TEMPORARY_FS
)) {
248 r
= fd_is_temporary_fs(dfd
);
250 return log_debug_errno(r
, "Failed to check directory '%s': %m", dirname
);
252 return log_debug_errno(SYNTHETIC_ERRNO(ENOMEDIUM
),
253 "Directory '%s' is on a temporary file system, refusing.", dirname
);
256 for (unsigned attempt
= 0;; attempt
++) {
257 _cleanup_(erase_and_freep
) struct credential_host_secret_format
*f
= NULL
;
258 _cleanup_close_
int fd
= -1;
263 if (attempt
>= 3) /* Somebody is playing games with us */
264 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
265 "All attempts to create secret store in %s failed.", dirname
);
267 fd
= openat(dfd
, filename
, O_CLOEXEC
|O_RDONLY
|O_NOCTTY
|O_NOFOLLOW
);
269 if (errno
!= ENOENT
|| !FLAGS_SET(flags
, CREDENTIAL_SECRET_GENERATE
))
270 return log_debug_errno(errno
,
271 "Failed to open %s/%s: %m", dirname
, filename
);
274 r
= make_credential_host_secret(dfd
, machine_id
, flags
, dirname
, filename
, ret
, ret_size
);
276 log_debug_errno(r
, "Credential secret %s/%s appeared while we were creating it, rereading.",
281 return log_debug_errno(r
, "Failed to create credential secret %s/%s: %m",
286 if (fstat(fd
, &st
) < 0)
287 return log_debug_errno(errno
, "Failed to stat %s/%s: %m", dirname
, filename
);
289 r
= stat_verify_regular(&st
);
291 return log_debug_errno(r
, "%s/%s is not a regular file: %m", dirname
, filename
);
292 if (st
.st_nlink
== 0) /* Deleted by now, try again */
295 /* Our deletion check won't work if hardlinked somewhere else */
296 return log_debug_errno(SYNTHETIC_ERRNO(EPERM
),
297 "%s/%s has too many links, refusing.",
299 if ((st
.st_mode
& 07777) != 0400)
300 /* Don't use file if not 0400 access mode */
301 return log_debug_errno(SYNTHETIC_ERRNO(EPERM
),
302 "%s/%s has permissive access mode, refusing.",
305 if (l
< offsetof(struct credential_host_secret_format
, data
) + 1)
306 return log_debug_errno(SYNTHETIC_ERRNO(EINVAL
),
307 "%s/%s is too small, refusing.", dirname
, filename
);
308 if (l
> 16*1024*1024)
309 return log_debug_errno(SYNTHETIC_ERRNO(E2BIG
),
310 "%s/%s is too big, refusing.", dirname
, filename
);
314 return log_oom_debug();
316 n
= read(fd
, f
, l
+1);
318 return log_debug_errno(errno
,
319 "Failed to read %s/%s: %m", dirname
, filename
);
320 if ((size_t) n
!= l
) /* What? The size changed? */
321 return log_debug_errno(SYNTHETIC_ERRNO(EIO
),
322 "Failed to read %s/%s: %m", dirname
, filename
);
324 if (sd_id128_equal(machine_id
, f
->machine_id
)) {
327 warn_not_encrypted(fd
, flags
, dirname
, filename
);
329 sz
= l
- offsetof(struct credential_host_secret_format
, data
);
335 assert(sz
<= sizeof(f
->data
)); /* Ensure we don't read past f->data bounds */
337 copy
= memdup(f
->data
, sz
);
339 return log_oom_debug();
350 /* Hmm, this secret is from somewhere else. Let's delete the file. Let's first acquire a lock
351 * to ensure we are the only ones accessing the file while we delete it. */
353 if (flock(fd
, LOCK_EX
) < 0)
354 return log_debug_errno(errno
,
355 "Failed to flock %s/%s: %m", dirname
, filename
);
357 /* Before we delete it check that the file is still linked into the file system */
358 if (fstat(fd
, &st
) < 0)
359 return log_debug_errno(errno
, "Failed to stat %s/%s: %m", dirname
, filename
);
360 if (st
.st_nlink
== 0) /* Already deleted by now? */
362 if (st
.st_nlink
!= 1) /* Safety check, someone is playing games with us */
363 return log_debug_errno(SYNTHETIC_ERRNO(EPERM
),
364 "%s/%s unexpectedly has too many links.",
366 if (unlinkat(dfd
, filename
, 0) < 0)
367 return log_debug_errno(errno
, "Failed to unlink %s/%s: %m", dirname
, filename
);
369 /* And now try again */
373 /* Construction is like this:
375 * A symmetric encryption key is derived from:
377 * 1. Either the "host" key (a key stored in /var/lib/credential.secret)
379 * 2. A key generated by letting the TPM2 calculate an HMAC hash of some nonce we pass to it, keyed
380 * by a key derived from its internal seed key.
382 * 3. The concatenation of the above.
384 * 4. Or a fixed "empty" key. This will not provide confidentiality or authenticity, of course, but is
385 * useful to encode credentials for the initrd on TPM-less systems, where we simply have no better
386 * concept to bind things to. Note that decryption of a key set up like this will be refused on
387 * systems that have a TPM and have SecureBoot enabled.
389 * The above is hashed with SHA256 which is then used as encryption key for AES256-GCM. The encrypted
390 * credential is a short (unencrypted) header describing which of the three keys to use, the IV to use for
391 * AES256-GCM and some more meta information (sizes of certain objects) that is strictly speaking redundant,
392 * but kinda nice to have since we can have a more generic parser. If the TPM2 key is used this is followed
393 * by another (unencrypted) header, with information about the TPM2 policy used (specifically: the PCR mask
394 * to bind against, and a hash of the resulting policy — the latter being redundant, but speeding up things a
395 * bit, since we can more quickly refuse PCR state), followed by a sealed/exported TPM2 HMAC key. This is
396 * then followed by the encrypted data, which begins with a metadata header (which contains validity
397 * timestamps as well as the credential name), followed by the actual credential payload. The file ends in
398 * the AES256-GCM tag. To make things simple, the AES256-GCM AAD covers the main and the TPM2 header in
399 * full. This means the whole file is either protected by AAD, or is ciphertext, or is the tag. No
400 * unprotected data is included.
403 struct _packed_ encrypted_credential_header
{
410 /* Followed by NUL bytes until next 8 byte boundary */
413 struct _packed_ tpm2_credential_header
{
414 le64_t pcr_mask
; /* Note that the spec for PC Clients only mandates 24 PCRs, and that's what systems
415 * generally have. But keep the door open for more. */
416 le16_t pcr_bank
; /* For now, either TPM2_ALG_SHA256 or TPM2_ALG_SHA1 */
417 le16_t primary_alg
; /* Primary key algorithm (either TPM2_ALG_RSA or TPM2_ALG_ECC for now) */
419 le32_t policy_hash_size
;
420 uint8_t policy_hash_and_blob
[];
421 /* Followed by NUL bytes until next 8 byte boundary */
424 struct _packed_ metadata_credential_header
{
429 /* Followed by NUL bytes until next 8 byte boundary */
432 /* Some generic limit for parts of the encrypted credential for which we don't know the right size ahead of
433 * time, but where we are really sure it won't be larger than this. Should be larger than any possible IV,
434 * padding, tag size and so on. This is purely used for early filtering out of invalid sizes. */
435 #define CREDENTIAL_FIELD_SIZE_MAX (16U*1024U)
437 static int sha256_hash_host_and_tpm2_key(
438 const void *host_key
,
439 size_t host_key_size
,
440 const void *tpm2_key
,
441 size_t tpm2_key_size
,
442 uint8_t ret
[static SHA256_DIGEST_LENGTH
]) {
444 _cleanup_(EVP_MD_CTX_freep
) EVP_MD_CTX
*md
= NULL
;
447 assert(host_key_size
== 0 || host_key
);
448 assert(tpm2_key_size
== 0 || tpm2_key
);
451 /* Combines the host key and the TPM2 HMAC hash into a SHA256 hash value we'll use as symmetric encryption key. */
453 md
= EVP_MD_CTX_new();
457 if (EVP_DigestInit_ex(md
, EVP_sha256(), NULL
) != 1)
458 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to initial SHA256 context.");
460 if (host_key
&& EVP_DigestUpdate(md
, host_key
, host_key_size
) != 1)
461 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to hash host key.");
463 if (tpm2_key
&& EVP_DigestUpdate(md
, tpm2_key
, tpm2_key_size
) != 1)
464 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to hash TPM2 key.");
466 assert(EVP_MD_CTX_size(md
) == SHA256_DIGEST_LENGTH
);
468 if (EVP_DigestFinal_ex(md
, ret
, &l
) != 1)
469 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to finalize SHA256 hash.");
471 assert(l
== SHA256_DIGEST_LENGTH
);
475 int encrypt_credential_and_warn(
480 const char *tpm2_device
,
481 uint32_t tpm2_pcr_mask
,
487 _cleanup_(EVP_CIPHER_CTX_freep
) EVP_CIPHER_CTX
*context
= NULL
;
488 _cleanup_(erase_and_freep
) void *host_key
= NULL
, *tpm2_key
= NULL
;
489 size_t host_key_size
= 0, tpm2_key_size
= 0, tpm2_blob_size
= 0, tpm2_policy_hash_size
= 0, output_size
, p
, ml
;
490 _cleanup_free_
void *tpm2_blob
= NULL
, *tpm2_policy_hash
= NULL
, *iv
= NULL
, *output
= NULL
;
491 _cleanup_free_
struct metadata_credential_header
*m
= NULL
;
492 uint16_t tpm2_pcr_bank
= 0, tpm2_primary_alg
= 0;
493 struct encrypted_credential_header
*h
;
494 int ksz
, bsz
, ivsz
, tsz
, added
, r
;
495 uint8_t md
[SHA256_DIGEST_LENGTH
];
496 const EVP_CIPHER
*cc
;
499 assert(input
|| input_size
== 0);
503 if (!sd_id128_in_set(with_key
,
506 CRED_AES256_GCM_BY_HOST
,
507 CRED_AES256_GCM_BY_TPM2_HMAC
,
508 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC
,
509 CRED_AES256_GCM_BY_TPM2_ABSENT
))
510 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid key type: " SD_ID128_FORMAT_STR
, SD_ID128_FORMAT_VAL(with_key
));
512 if (name
&& !credential_name_valid(name
))
513 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Invalid credential name: %s", name
);
515 if (not_after
!= USEC_INFINITY
&& timestamp
!= USEC_INFINITY
&& not_after
< timestamp
)
516 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Credential is invalidated before it is valid (" USEC_FMT
" < " USEC_FMT
").", not_after
, timestamp
);
519 char buf
[FORMAT_TIMESTAMP_MAX
];
522 log_debug("Including credential name '%s' in encrypted credential.", name
);
523 if (timestamp
!= USEC_INFINITY
)
524 log_debug("Including timestamp '%s' in encrypted credential.", format_timestamp(buf
, sizeof(buf
), timestamp
));
525 if (not_after
!= USEC_INFINITY
)
526 log_debug("Including not-after timestamp '%s' in encrypted credential.", format_timestamp(buf
, sizeof(buf
), not_after
));
529 if (sd_id128_in_set(with_key
,
531 CRED_AES256_GCM_BY_HOST
,
532 CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC
)) {
534 r
= get_credential_host_secret(
535 CREDENTIAL_SECRET_GENERATE
|
536 CREDENTIAL_SECRET_WARN_NOT_ENCRYPTED
|
537 (sd_id128_equal(with_key
, _CRED_AUTO
) ? CREDENTIAL_SECRET_FAIL_ON_TEMPORARY_FS
: 0),
540 if (r
== -ENOMEDIUM
&& sd_id128_equal(with_key
, _CRED_AUTO
))
541 log_debug_errno(r
, "Credential host secret location on temporary file system, not using.");
543 return log_error_errno(r
, "Failed to determine local credential host secret: %m");
548 if (sd_id128_equal(with_key
, _CRED_AUTO
)) {
549 /* If automatic mode is selected and we are running in a container, let's not try TPM2. OTOH
550 * if user picks TPM2 explicitly, let's always honour the request and try. */
552 r
= detect_container();
554 log_debug_errno(r
, "Failed to determine whether we are running in a container, ignoring: %m");
556 log_debug("Running in container, not attempting to use TPM2.");
559 } else if (sd_id128_equal(with_key
, _CRED_AUTO_INITRD
)) {
560 /* If automatic mode for initrds is selected, we'll use the TPM2 key if the firmware does it,
561 * otherwise we'll use a fixed key */
563 try_tpm2
= efi_has_tpm2();
565 log_debug("Firmware lacks TPM2 support, not attempting to use TPM2.");
567 try_tpm2
= sd_id128_in_set(with_key
, CRED_AES256_GCM_BY_TPM2_HMAC
, CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC
);
570 r
= tpm2_seal(tpm2_device
,
578 &tpm2_policy_hash_size
,
582 if (sd_id128_equal(with_key
, _CRED_AUTO_INITRD
))
583 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.");
584 else if (!sd_id128_equal(with_key
, _CRED_AUTO
))
587 log_notice_errno(r
, "TPM2 sealing didn't work, continuing without TPM2: %m");
590 assert(tpm2_blob_size
<= CREDENTIAL_FIELD_SIZE_MAX
);
591 assert(tpm2_policy_hash_size
<= CREDENTIAL_FIELD_SIZE_MAX
);
595 if (sd_id128_in_set(with_key
, _CRED_AUTO
, _CRED_AUTO_INITRD
)) {
596 /* Let's settle the key type in auto mode now. */
598 if (host_key
&& tpm2_key
)
599 id
= CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC
;
601 id
= CRED_AES256_GCM_BY_TPM2_HMAC
;
603 id
= CRED_AES256_GCM_BY_HOST
;
604 else if (sd_id128_equal(with_key
, _CRED_AUTO_INITRD
))
605 id
= CRED_AES256_GCM_BY_TPM2_ABSENT
;
607 return log_error_errno(SYNTHETIC_ERRNO(ENOTRECOVERABLE
),
608 "TPM2 not available and host key located on temporary file system, no encryption key available.");
612 if (sd_id128_equal(id
, CRED_AES256_GCM_BY_TPM2_ABSENT
))
613 log_warning("Using a null key for encryption and signing. Confidentiality or authenticity will not be provided.");
615 /* Let's now take the host key and the TPM2 key and hash it together, to use as encryption key for the data */
616 r
= sha256_hash_host_and_tpm2_key(host_key
, host_key_size
, tpm2_key
, tpm2_key_size
, md
);
620 assert_se(cc
= EVP_aes_256_gcm());
622 ksz
= EVP_CIPHER_key_length(cc
);
623 assert(ksz
== sizeof(md
));
625 bsz
= EVP_CIPHER_block_size(cc
);
627 assert((size_t) bsz
<= CREDENTIAL_FIELD_SIZE_MAX
);
629 ivsz
= EVP_CIPHER_iv_length(cc
);
631 assert((size_t) ivsz
<= CREDENTIAL_FIELD_SIZE_MAX
);
637 r
= genuine_random_bytes(iv
, ivsz
, RANDOM_BLOCK
);
639 return log_error_errno(r
, "Failed to acquired randomized IV: %m");
642 tsz
= 16; /* FIXME: On OpenSSL 3 there is EVP_CIPHER_CTX_get_tag_length(), until then let's hardcode this */
644 context
= EVP_CIPHER_CTX_new();
646 return log_error_errno(SYNTHETIC_ERRNO(ENOMEM
), "Failed to allocate encryption object: %s",
647 ERR_error_string(ERR_get_error(), NULL
));
649 if (EVP_EncryptInit_ex(context
, cc
, NULL
, md
, iv
) != 1)
650 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to initialize encryption context: %s",
651 ERR_error_string(ERR_get_error(), NULL
));
653 /* Just an upper estimate */
655 ALIGN8(offsetof(struct encrypted_credential_header
, iv
) + ivsz
) +
656 ALIGN8(tpm2_key
? offsetof(struct tpm2_credential_header
, policy_hash_and_blob
) + tpm2_blob_size
+ tpm2_policy_hash_size
: 0) +
657 ALIGN8(offsetof(struct metadata_credential_header
, name
) + strlen_ptr(name
)) +
658 input_size
+ 2U * (size_t) bsz
+
661 output
= malloc0(output_size
);
665 h
= (struct encrypted_credential_header
*) output
;
667 h
->block_size
= htole32(bsz
);
668 h
->key_size
= htole32(ksz
);
669 h
->tag_size
= htole32(tsz
);
670 h
->iv_size
= htole32(ivsz
);
671 memcpy(h
->iv
, iv
, ivsz
);
673 p
= ALIGN8(offsetof(struct encrypted_credential_header
, iv
) + ivsz
);
676 struct tpm2_credential_header
*t
;
678 t
= (struct tpm2_credential_header
*) ((uint8_t*) output
+ p
);
679 t
->pcr_mask
= htole64(tpm2_pcr_mask
);
680 t
->pcr_bank
= htole16(tpm2_pcr_bank
);
681 t
->primary_alg
= htole16(tpm2_primary_alg
);
682 t
->blob_size
= htole32(tpm2_blob_size
);
683 t
->policy_hash_size
= htole32(tpm2_policy_hash_size
);
684 memcpy(t
->policy_hash_and_blob
, tpm2_blob
, tpm2_blob_size
);
685 memcpy(t
->policy_hash_and_blob
+ tpm2_blob_size
, tpm2_policy_hash
, tpm2_policy_hash_size
);
687 p
+= ALIGN8(offsetof(struct tpm2_credential_header
, policy_hash_and_blob
) + tpm2_blob_size
+ tpm2_policy_hash_size
);
690 /* Pass the encrypted + TPM2 header as AAD */
691 if (EVP_EncryptUpdate(context
, NULL
, &added
, output
, p
) != 1)
692 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to write AAD data: %s",
693 ERR_error_string(ERR_get_error(), NULL
));
695 /* Now construct the metadata header */
696 ml
= strlen_ptr(name
);
697 m
= malloc0(ALIGN8(offsetof(struct metadata_credential_header
, name
) + ml
));
701 m
->timestamp
= htole64(timestamp
);
702 m
->not_after
= htole64(not_after
);
703 m
->name_size
= htole32(ml
);
704 memcpy_safe(m
->name
, name
, ml
);
706 /* And encrypt the metadata header */
707 if (EVP_EncryptUpdate(context
, (uint8_t*) output
+ p
, &added
, (const unsigned char*) m
, ALIGN8(offsetof(struct metadata_credential_header
, name
) + ml
)) != 1)
708 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to encrypt metadata header: %s",
709 ERR_error_string(ERR_get_error(), NULL
));
712 assert((size_t) added
<= output_size
- p
);
715 /* Then encrypt the plaintext */
716 if (EVP_EncryptUpdate(context
, (uint8_t*) output
+ p
, &added
, input
, input_size
) != 1)
717 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to encrypt data: %s",
718 ERR_error_string(ERR_get_error(), NULL
));
721 assert((size_t) added
<= output_size
- p
);
725 if (EVP_EncryptFinal_ex(context
, (uint8_t*) output
+ p
, &added
) != 1)
726 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to finalize data encryption: %s",
727 ERR_error_string(ERR_get_error(), NULL
));
730 assert((size_t) added
<= output_size
- p
);
733 assert(p
<= output_size
- tsz
);
736 if (EVP_CIPHER_CTX_ctrl(context
, EVP_CTRL_GCM_GET_TAG
, tsz
, (uint8_t*) output
+ p
) != 1)
737 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to get tag: %s",
738 ERR_error_string(ERR_get_error(), NULL
));
741 assert(p
<= output_size
);
743 if (DEBUG_LOGGING
&& input_size
> 0) {
746 base64_size
= DIV_ROUND_UP(p
* 4, 3); /* Include base64 size increase in debug output */
747 assert(base64_size
>= input_size
);
748 log_debug("Input of %zu bytes grew to output of %zu bytes (+%2zu%%).", input_size
, base64_size
, base64_size
* 100 / input_size
- 100);
751 *ret
= TAKE_PTR(output
);
757 int decrypt_credential_and_warn(
758 const char *validate_name
,
759 usec_t validate_timestamp
,
760 const char *tpm2_device
,
766 _cleanup_(erase_and_freep
) void *host_key
= NULL
, *tpm2_key
= NULL
, *plaintext
= NULL
;
767 _cleanup_(EVP_CIPHER_CTX_freep
) EVP_CIPHER_CTX
*context
= NULL
;
768 size_t host_key_size
= 0, tpm2_key_size
= 0, plaintext_size
, p
, hs
;
769 struct encrypted_credential_header
*h
;
770 struct metadata_credential_header
*m
;
771 uint8_t md
[SHA256_DIGEST_LENGTH
];
772 bool with_tpm2
, with_host_key
, is_tpm2_absent
;
773 const EVP_CIPHER
*cc
;
776 assert(input
|| input_size
== 0);
780 h
= (struct encrypted_credential_header
*) input
;
782 /* The ID must fit in, for the current and all future formats */
783 if (input_size
< sizeof(h
->id
))
784 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Encrypted file too short.");
786 with_host_key
= sd_id128_in_set(h
->id
, CRED_AES256_GCM_BY_HOST
, CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC
);
787 with_tpm2
= sd_id128_in_set(h
->id
, CRED_AES256_GCM_BY_TPM2_HMAC
, CRED_AES256_GCM_BY_HOST_AND_TPM2_HMAC
);
788 is_tpm2_absent
= sd_id128_equal(h
->id
, CRED_AES256_GCM_BY_TPM2_ABSENT
);
790 if (!with_host_key
&& !with_tpm2
&& !is_tpm2_absent
)
791 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "Unknown encryption format, or corrupted data: %m");
793 if (is_tpm2_absent
) {
794 /* So this is a credential encrypted with a zero length key. We support this to cover for the
795 * case where neither a host key not a TPM2 are available (specifically: initrd environments
796 * where the host key is not yet accessible and no TPM2 chip exists at all), to minimize
797 * different codeflow for TPM2 and non-TPM2 codepaths. Of course, credentials encoded this
798 * way offer no confidentiality nor authenticity. Because of that it's important we refuse to
799 * use them on systems that actually *do* have a TPM2 chip – if we are in SecureBoot
800 * mode. Otherwise an attacker could hand us credentials like this and we'd use them thinking
801 * they are trusted, even though they are not. */
803 if (efi_has_tpm2()) {
804 if (is_efi_secure_boot())
805 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
),
806 "Credential uses fixed key for fallback use when TPM2 is absent — but TPM2 is present, and SecureBoot is enabled, refusing.");
808 log_warning("Credential uses fixed key for use when TPM2 is absent, but TPM2 is present! Accepting anyway, since SecureBoot is disabled.");
810 log_debug("Credential uses fixed key for use when TPM2 is absent, and TPM2 indeed is absent. Accepting.");
813 /* Now we know the minimum header size */
814 if (input_size
< offsetof(struct encrypted_credential_header
, iv
))
815 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Encrypted file too short.");
817 /* Verify some basic header values */
818 if (le32toh(h
->key_size
) != sizeof(md
))
819 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Unexpected key size in header.");
820 if (le32toh(h
->block_size
) <= 0 || le32toh(h
->block_size
) > CREDENTIAL_FIELD_SIZE_MAX
)
821 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Unexpected block size in header.");
822 if (le32toh(h
->iv_size
) > CREDENTIAL_FIELD_SIZE_MAX
)
823 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "IV size too large.");
824 if (le32toh(h
->tag_size
) != 16) /* FIXME: On OpenSSL 3, let's verify via EVP_CIPHER_CTX_get_tag_length() */
825 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Unexpected tag size in header.");
827 /* Ensure we have space for the full header now (we don't know the size of the name hence this is a
828 * lower limit only) */
830 ALIGN8(offsetof(struct encrypted_credential_header
, iv
) + le32toh(h
->iv_size
)) +
831 ALIGN8((with_tpm2
? offsetof(struct tpm2_credential_header
, policy_hash_and_blob
) : 0)) +
832 ALIGN8(offsetof(struct metadata_credential_header
, name
)) +
833 le32toh(h
->tag_size
))
834 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Encrypted file too short.");
836 p
= ALIGN8(offsetof(struct encrypted_credential_header
, iv
) + le32toh(h
->iv_size
));
840 struct tpm2_credential_header
* t
= (struct tpm2_credential_header
*) ((uint8_t*) input
+ p
);
842 if (le64toh(t
->pcr_mask
) >= (UINT64_C(1) << TPM2_PCRS_MAX
))
843 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "TPM2 PCR mask out of range.");
844 if (!tpm2_pcr_bank_to_string(le16toh(t
->pcr_bank
)))
845 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "TPM2 PCR bank invalid or not supported");
846 if (!tpm2_primary_alg_to_string(le16toh(t
->primary_alg
)))
847 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "TPM2 primary key algorithm invalid or not supported.");
848 if (le32toh(t
->blob_size
) > CREDENTIAL_FIELD_SIZE_MAX
)
849 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Unexpected TPM2 blob size.");
850 if (le32toh(t
->policy_hash_size
) > CREDENTIAL_FIELD_SIZE_MAX
)
851 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Unexpected TPM2 policy hash size.");
853 /* Ensure we have space for the full TPM2 header now (still don't know the name, and its size
854 * though, hence still just a lower limit test only) */
856 ALIGN8(offsetof(struct encrypted_credential_header
, iv
) + le32toh(h
->iv_size
)) +
857 ALIGN8(offsetof(struct tpm2_credential_header
, policy_hash_and_blob
) + le32toh(t
->blob_size
) + le32toh(t
->policy_hash_size
)) +
858 ALIGN8(offsetof(struct metadata_credential_header
, name
)) +
859 le32toh(h
->tag_size
))
860 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Encrypted file too short.");
862 r
= tpm2_unseal(tpm2_device
,
863 le64toh(t
->pcr_mask
),
864 le16toh(t
->pcr_bank
),
865 le16toh(t
->primary_alg
),
866 t
->policy_hash_and_blob
,
867 le32toh(t
->blob_size
),
868 t
->policy_hash_and_blob
+ le32toh(t
->blob_size
),
869 le32toh(t
->policy_hash_size
),
876 p
+= ALIGN8(offsetof(struct tpm2_credential_header
, policy_hash_and_blob
) +
877 le32toh(t
->blob_size
) +
878 le32toh(t
->policy_hash_size
));
880 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "Credential requires TPM2 support, but TPM2 support not available.");
885 r
= get_credential_host_secret(
890 return log_error_errno(r
, "Failed to determine local credential key: %m");
894 log_warning("Warning: using a null key for decryption and authentication. Confidentiality or authenticity are not provided.");
896 sha256_hash_host_and_tpm2_key(host_key
, host_key_size
, tpm2_key
, tpm2_key_size
, md
);
898 assert_se(cc
= EVP_aes_256_gcm());
900 /* Make sure cipher expectations match the header */
901 if (EVP_CIPHER_key_length(cc
) != (int) le32toh(h
->key_size
))
902 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Unexpected key size in header.");
903 if (EVP_CIPHER_block_size(cc
) != (int) le32toh(h
->block_size
))
904 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Unexpected block size in header.");
906 context
= EVP_CIPHER_CTX_new();
908 return log_error_errno(SYNTHETIC_ERRNO(ENOMEM
), "Failed to allocate decryption object: %s",
909 ERR_error_string(ERR_get_error(), NULL
));
911 if (EVP_DecryptInit_ex(context
, cc
, NULL
, NULL
, NULL
) != 1)
912 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to initialize decryption context: %s",
913 ERR_error_string(ERR_get_error(), NULL
));
915 if (EVP_CIPHER_CTX_ctrl(context
, EVP_CTRL_GCM_SET_IVLEN
, le32toh(h
->iv_size
), NULL
) != 1)
916 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to set IV size on decryption context: %s",
917 ERR_error_string(ERR_get_error(), NULL
));
919 if (EVP_DecryptInit_ex(context
, NULL
, NULL
, md
, h
->iv
) != 1)
920 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to set IV and key: %s",
921 ERR_error_string(ERR_get_error(), NULL
));
923 if (EVP_DecryptUpdate(context
, NULL
, &added
, input
, p
) != 1)
924 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to write AAD data: %s",
925 ERR_error_string(ERR_get_error(), NULL
));
927 plaintext
= malloc(input_size
- p
- le32toh(h
->tag_size
));
931 if (EVP_DecryptUpdate(
935 (uint8_t*) input
+ p
,
936 input_size
- p
- le32toh(h
->tag_size
)) != 1)
937 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to decrypt data: %s",
938 ERR_error_string(ERR_get_error(), NULL
));
941 assert((size_t) added
<= input_size
- p
- le32toh(h
->tag_size
));
942 plaintext_size
= added
;
944 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)
945 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
), "Failed to set tag: %s",
946 ERR_error_string(ERR_get_error(), NULL
));
948 if (EVP_DecryptFinal_ex(context
, (uint8_t*) plaintext
+ plaintext_size
, &added
) != 1)
949 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Decryption failed (incorrect key?): %s",
950 ERR_error_string(ERR_get_error(), NULL
));
952 plaintext_size
+= added
;
954 if (plaintext_size
< ALIGN8(offsetof(struct metadata_credential_header
, name
)))
955 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Metadata header incomplete.");
959 if (le64toh(m
->timestamp
) != USEC_INFINITY
&&
960 le64toh(m
->not_after
) != USEC_INFINITY
&&
961 le64toh(m
->timestamp
) >= le64toh(m
->not_after
))
962 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Timestamps of credential are not in order, refusing.");
964 if (le32toh(m
->name_size
) > CREDENTIAL_NAME_MAX
)
965 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Embedded credential name too long, refusing.");
967 hs
= ALIGN8(offsetof(struct metadata_credential_header
, name
) + le32toh(m
->name_size
));
968 if (plaintext_size
< hs
)
969 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Metadata header incomplete.");
971 if (le32toh(m
->name_size
) > 0) {
972 _cleanup_free_
char *embedded_name
= NULL
;
974 if (memchr(m
->name
, 0, le32toh(m
->name_size
)))
975 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Embedded credential name contains NUL byte, refusing.");
977 embedded_name
= memdup_suffix0(m
->name
, le32toh(m
->name_size
));
981 if (!credential_name_valid(embedded_name
))
982 return log_error_errno(SYNTHETIC_ERRNO(EBADMSG
), "Embedded credential name is not valid, refusing.");
984 if (validate_name
&& !streq(embedded_name
, validate_name
)) {
986 r
= getenv_bool_secure("SYSTEMD_CREDENTIAL_VALIDATE_NAME");
987 if (r
< 0 && r
!= -ENXIO
)
988 log_debug_errno(r
, "Failed to parse $SYSTEMD_CREDENTIAL_VALIDATE_NAME: %m");
990 return log_error_errno(SYNTHETIC_ERRNO(EREMOTE
), "Embedded credential name '%s' does not match filename '%s', refusing.", embedded_name
, validate_name
);
992 log_debug("Embedded credential name '%s' does not match expected name '%s', but configured to use credential anyway.", embedded_name
, validate_name
);
996 if (validate_timestamp
!= USEC_INFINITY
) {
997 if (le64toh(m
->timestamp
) != USEC_INFINITY
&& le64toh(m
->timestamp
) > validate_timestamp
)
998 log_debug("Credential timestamp is from the future, assuming clock skew.");
1000 if (le64toh(m
->not_after
) != USEC_INFINITY
&& le64toh(m
->not_after
) < validate_timestamp
) {
1002 r
= getenv_bool_secure("SYSTEMD_CREDENTIAL_VALIDATE_NOT_AFTER");
1003 if (r
< 0 && r
!= -ENXIO
)
1004 log_debug_errno(r
, "Failed to parse $SYSTEMD_CREDENTIAL_VALIDATE_NOT_AFTER: %m");
1006 return log_error_errno(SYNTHETIC_ERRNO(ESTALE
), "Credential's time passed, refusing to use.");
1008 log_debug("Credential not-after timestamp has passed, but configured to use credential anyway.");
1013 char *without_metadata
;
1015 without_metadata
= memdup((uint8_t*) plaintext
+ hs
, plaintext_size
- hs
);
1016 if (!without_metadata
)
1019 *ret
= without_metadata
;
1023 *ret_size
= plaintext_size
- hs
;
1030 int get_credential_host_secret(CredentialSecretFlags flags
, void **ret
, size_t *ret_size
) {
1031 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "Support for encrypted credentials not available.");
1034 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
) {
1035 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "Support for encrypted credentials not available.");
1038 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
) {
1039 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "Support for encrypted credentials not available.");