1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
4 #include "analyze-srk.h"
8 int verb_srk(int argc
, char *argv
[], void *userdata
) {
10 _cleanup_(tpm2_context_unrefp
) Tpm2Context
*c
= NULL
;
11 _cleanup_(Esys_Freep
) TPM2B_PUBLIC
*public = NULL
;
14 r
= tpm2_context_new_or_warn(/* device= */ NULL
, &c
);
23 /* ret_qname= */ NULL
,
24 /* ret_handle= */ NULL
);
26 return log_error_errno(r
, "Failed to get SRK: %m");
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
);
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
);
45 return log_error_errno(r
, "Failed to write SRK to stdout: %m");
49 return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP
), "TPM2 support not available.");