1:255.13-alt1
[systemd_ALT.git] / src / analyze / analyze-srk.c
blob6faf2c29a34f283f208d8e3e55e3cbb2fd560a65
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
3 #include "analyze.h"
4 #include "analyze-srk.h"
5 #include "fileio.h"
6 #include "tpm2-util.h"
8 int verb_srk(int argc, char *argv[], void *userdata) {
9 #if HAVE_TPM2
10 _cleanup_(tpm2_context_unrefp) Tpm2Context *c = NULL;
11 _cleanup_(Esys_Freep) TPM2B_PUBLIC *public = NULL;
12 int r;
14 r = tpm2_context_new_or_warn(/* device= */ NULL, &c);
15 if (r < 0)
16 return r;
18 r = tpm2_get_srk(
20 /* session= */ NULL,
21 &public,
22 /* ret_name= */ NULL,
23 /* ret_qname= */ NULL,
24 /* ret_handle= */ NULL);
25 if (r < 0)
26 return log_error_errno(r, "Failed to get SRK: %m");
27 if (r == 0)
28 return log_error_errno(SYNTHETIC_ERRNO(ENOENT), "No SRK stored so far.");
30 _cleanup_free_ void *marshalled = NULL;
31 size_t marshalled_size = 0;
32 r = tpm2_marshal_public(public, &marshalled, &marshalled_size);
33 if (r < 0)
34 return log_error_errno(r, "Failed to marshal SRK: %m");
36 if (isatty(STDOUT_FILENO))
37 return log_error_errno(SYNTHETIC_ERRNO(EIO),
38 "Refusing to write binary data to TTY, please redirect output to file.");
40 if (fwrite(marshalled, 1, marshalled_size, stdout) != marshalled_size)
41 return log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to write SRK to stdout: %m");
43 r = fflush_and_check(stdout);
44 if (r < 0)
45 return log_error_errno(r, "Failed to write SRK to stdout: %m");
47 return EXIT_SUCCESS;
48 #else
49 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "TPM2 support not available.");
50 #endif