• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifdef HAVE_CONFIG_H
2 #include <config.h>
3 #endif
4 
5 #include <stdbool.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <inttypes.h>
9 
10 #include "tss2_sys.h"
11 #include "tss2_mu.h"
12 
13 #define LOGMODULE test
14 #include "util/log.h"
15 #include "test-options.h"
16 #include "context-util.h"
17 
18 #define TAB_SIZE(x) (sizeof(x)/sizeof(x[0]))
19 
20 /* NOTE: CAP_PCRS and CAP_HANDLES->HR_PCR do not change until a reboot is
21   triggered. This should be improved if an approach is found. */
22 struct {
23     TPM2_CAP cap;
24     UINT32 prop;
25     UINT32 count;
26 } capabilities[] = {
27     { TPM2_CAP_PCRS, 0, 10 },
28     { TPM2_CAP_HANDLES, TPM2_HR_PCR, TPM2_MAX_CAP_HANDLES },
29     { TPM2_CAP_HANDLES, TPM2_HR_HMAC_SESSION, TPM2_MAX_CAP_HANDLES },
30     { TPM2_CAP_HANDLES, TPM2_HR_POLICY_SESSION, TPM2_MAX_CAP_HANDLES },
31     { TPM2_CAP_HANDLES, TPM2_HR_TRANSIENT, TPM2_MAX_CAP_HANDLES },
32     { TPM2_CAP_HANDLES, TPM2_HR_PERSISTENT, TPM2_MAX_CAP_HANDLES },
33     { TPM2_CAP_HANDLES, TPM2_HR_NV_INDEX, TPM2_MAX_CAP_HANDLES },
34 };
35 
36 int
main(int argc,char * argv[])37 main (int argc, char *argv[])
38 {
39     TSS2_RC rc;
40     TSS2_SYS_CONTEXT *sapi_context;
41 
42     test_opts_t opts = {
43         .tcti_type      = TCTI_DEFAULT,
44         .device_file    = DEVICE_PATH_DEFAULT,
45         .socket_address = HOSTNAME_DEFAULT,
46         .socket_port    = PORT_DEFAULT,
47     };
48 
49     get_test_opts_from_env (&opts);
50     if (sanity_check_test_opts (&opts) != 0)
51         exit (1);
52 
53     sapi_context = sapi_init_from_opts (&opts);
54     if (sapi_context == NULL)
55         exit (1);
56 
57     for (size_t i = 0; i < TAB_SIZE(capabilities); i++) {
58         TPMS_CAPABILITY_DATA caps;
59         uint8_t buffer[sizeof(caps)];
60         size_t off = 0;
61 
62         rc = Tss2_Sys_GetCapability(sapi_context, NULL, capabilities[i].cap,
63                                     capabilities[i].prop,
64                                     capabilities[i].count, NULL,
65                                     &caps, NULL);
66         if (rc != TSS2_RC_SUCCESS) {
67             LOG_ERROR("TPM GetCapabilities FAILED: 0x%"PRIx32, rc);
68             exit(1);
69         }
70 
71         rc = Tss2_MU_TPMS_CAPABILITY_DATA_Marshal(&caps, &buffer[off],
72                                                   sizeof(buffer) - off - 1,
73                                                   &off);
74         if (rc != TSS2_RC_SUCCESS) {
75             LOG_ERROR("Marshaling FAILED: 0x%"PRIx32, rc);
76             exit(1);
77         }
78 
79         buffer[off++] = '\0';
80 
81         printf("cap%zi: ", i);
82         for (size_t j = 0; j < off; j++)
83             printf("%02"PRIx8, buffer[j]);
84         printf("\n");
85     }
86 
87     sapi_teardown_full (sapi_context);
88 
89     return 0;
90 }
91