1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /***********************************************************************
3 * Copyright (c) 2017-2018, Intel Corporation
4 *
5 * All rights reserved.
6 ***********************************************************************/
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10
11 #include <inttypes.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include "tss2_mu.h"
16 #include "tss2_sys.h"
17
18 #define LOGMODULE test
19 #include "util/log.h"
20 #include "sapi-util.h"
21
22 TSS2_RC
test_invoke(TSS2_SYS_CONTEXT * sapi_context)23 test_invoke (TSS2_SYS_CONTEXT *sapi_context)
24 {
25 TSS2_RC rc = TSS2_RC_SUCCESS;
26 TPM2B_SENSITIVE_CREATE in_sensitive = { 0 };
27 TPMT_PUBLIC in_public = { 0 };
28 TPM2B_TEMPLATE public_template = { 0 };
29 TPM2B_PRIVATE out_private = { 0 };
30 TPM2B_PUBLIC out_public = { 0 };
31 TPM2B_NAME name = TPM2B_NAME_INIT;
32 TPM2B_NAME qualified_name = TPM2B_NAME_INIT;
33 TPM2_HANDLE object_handle = 0;
34 TSS2L_SYS_AUTH_COMMAND auth_cmd = {
35 .auths = {{ .sessionHandle = TPM2_RS_PW }},
36 .count = 1
37 };
38 TSS2L_SYS_AUTH_RESPONSE auth_rsp = {
39 .count = 0
40 };
41
42 if (sapi_context == NULL)
43 return TSS2_RC_LAYER_MASK | TSS2_BASE_RC_BAD_REFERENCE;
44
45 in_public.type = TPM2_ALG_RSA;
46 in_public.nameAlg = TPM2_ALG_SHA256;
47 in_public.objectAttributes |= TPMA_OBJECT_RESTRICTED;
48 in_public.objectAttributes |= TPMA_OBJECT_USERWITHAUTH;
49 in_public.objectAttributes |= TPMA_OBJECT_DECRYPT;
50 in_public.objectAttributes |= TPMA_OBJECT_FIXEDTPM;
51 in_public.objectAttributes |= TPMA_OBJECT_FIXEDPARENT;
52 in_public.objectAttributes |= TPMA_OBJECT_SENSITIVEDATAORIGIN;
53 in_public.parameters.rsaDetail.symmetric.algorithm = TPM2_ALG_AES;
54 in_public.parameters.rsaDetail.symmetric.keyBits.aes = 128;
55 in_public.parameters.rsaDetail.symmetric.mode.aes = TPM2_ALG_CFB;
56 in_public.parameters.rsaDetail.scheme.scheme = TPM2_ALG_NULL;
57 in_public.parameters.rsaDetail.keyBits = 2048;
58
59 uint8_t public_buf[sizeof(in_public)] = {0};
60 size_t offset = 0;
61
62 rc = Tss2_MU_TPMT_PUBLIC_Marshal(&in_public, public_buf,
63 sizeof(in_public), &offset);
64 if (rc != TPM2_RC_SUCCESS) {
65 LOG_ERROR("Tss2_MU_TPMT_PUBLIC_Marshal FAILED! Response Code: 0x%x", rc);
66 exit(1);
67 }
68 public_template.size = offset;
69 memcpy(public_template.buffer, public_buf, offset);
70 /* Create an object using CreateLoaded.
71 * The result should be that the created object
72 * stays in the TPM
73 */
74 LOG_INFO("Calling CreateLoaded");
75 rc = Tss2_Sys_CreateLoaded (sapi_context,
76 TPM2_RH_OWNER,
77 &auth_cmd,
78 &in_sensitive,
79 &public_template,
80 &object_handle,
81 &out_private,
82 &out_public,
83 &name,
84 &auth_rsp);
85 if (rc == TPM2_RC_SUCCESS) {
86 LOG_INFO("success object handle: 0x%x", object_handle);
87 } else {
88 LOG_ERROR("CreateLoaded FAILED! Response Code : 0x%x", rc);
89 exit(1);
90 }
91
92 memset(&out_public, '\0', sizeof(out_public));
93 memset(&name, '\0', sizeof(name));
94
95 /* Check if the object is really loaded by accessing its
96 * public area */
97 LOG_INFO("Calling ReadPublic");
98 rc = Tss2_Sys_ReadPublic (sapi_context,
99 object_handle,
100 NULL,
101 &out_public,
102 &name,
103 &qualified_name,
104 NULL);
105 if (rc == TPM2_RC_SUCCESS) {
106 LOG_INFO("success! Object's qualified name is:");
107 LOGBLOB_INFO(qualified_name.name, qualified_name.size, "%s", "name:");
108 } else {
109 LOG_ERROR("Tss2_Sys_ReadPublic FAILED! Response Code : 0x%x", rc);
110 exit(1);
111 }
112
113 rc = Tss2_Sys_FlushContext (sapi_context, object_handle);
114 if (rc != TSS2_RC_SUCCESS) {
115 LOG_ERROR("Tss2_Sys_FlushContext failed: 0x%" PRIx32, rc);
116 exit(1);
117 }
118
119 return rc;
120 }
121