xref: /aosp_15_r20/external/tpm2-tss/test/integration/esys-quote.int.c (revision 758e9fba6fc9adbf15340f70c73baee7b168b1c9)
1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*******************************************************************************
3  * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
4  * All rights reserved.
5  *******************************************************************************/
6 
7 #ifdef HAVE_CONFIG_H
8 #include <config.h>
9 #endif
10 
11 #include <stdlib.h>
12 
13 #include "tss2_esys.h"
14 
15 #include "esys_iutil.h"
16 #define LOGMODULE test
17 #include "util/log.h"
18 #include "util/aux_util.h"
19 
20 /** This test is intended to test the quote command with password
21  *  authentication.
22  *
23  * We create a RSA primary signing key which will be used
24  * for signing.
25  *
26  * Tested ESAPI commands:
27  *  - Esys_CreatePrimary() (M)
28  *  - Esys_FlushContext() (M)
29  *  - Esys_Quote() (M)
30  *
31  * @param[in,out] esys_context The ESYS_CONTEXT.
32  * @retval EXIT_FAILURE
33  * @retval EXIT_SUCCESS
34  */
35 
36 int
test_esys_quote(ESYS_CONTEXT * esys_context)37 test_esys_quote(ESYS_CONTEXT * esys_context)
38 {
39     TSS2_RC r;
40     ESYS_TR primaryHandle = ESYS_TR_NONE;
41 
42     TPM2B_PUBLIC *outPublic = NULL;
43     TPM2B_CREATION_DATA *creationData = NULL;
44     TPM2B_DIGEST *creationHash = NULL;
45     TPMT_TK_CREATION *creationTicket = NULL;
46 
47     TPM2B_ATTEST *attest = NULL;
48     TPMT_SIGNATURE *signature = NULL;
49 
50     TPM2B_AUTH authValuePrimary = {
51         .size = 5,
52         .buffer = {1, 2, 3, 4, 5}
53     };
54 
55     TPM2B_SENSITIVE_CREATE inSensitivePrimary = {
56         .size = 0,
57         .sensitive = {
58             .userAuth = {
59                  .size = 0,
60                  .buffer = {0},
61              },
62             .data = {
63                  .size = 0,
64                  .buffer = {0},
65              },
66         },
67     };
68 
69     inSensitivePrimary.sensitive.userAuth = authValuePrimary;
70 
71     TPM2B_PUBLIC inPublic = {
72             .size = 0,
73             .publicArea = {
74                 .type = TPM2_ALG_RSA,
75                 .nameAlg = TPM2_ALG_SHA1,
76                 .objectAttributes = (
77                     TPMA_OBJECT_USERWITHAUTH |
78                     TPMA_OBJECT_RESTRICTED |
79                     TPMA_OBJECT_SIGN_ENCRYPT |
80                     TPMA_OBJECT_FIXEDTPM |
81                     TPMA_OBJECT_FIXEDPARENT |
82                     TPMA_OBJECT_SENSITIVEDATAORIGIN
83                     ),
84                 .authPolicy = {
85                         .size = 0,
86                     },
87                 .parameters.rsaDetail = {
88                     .symmetric = {
89                         .algorithm = TPM2_ALG_NULL,
90                         .keyBits.aes = 128,
91                         .mode.aes = TPM2_ALG_CFB,
92                         },
93                     .scheme = {
94                          .scheme = TPM2_ALG_RSASSA,
95                          .details = { .rsassa = { .hashAlg = TPM2_ALG_SHA1 }},
96 
97                     },
98                     .keyBits = 2048,
99                     .exponent = 0,
100                 },
101                 .unique.rsa = {
102                         .size = 0,
103                         .buffer = {},
104                     },
105             },
106         };
107 
108     TPM2B_AUTH authValue = {
109                 .size = 0,
110                 .buffer = {}
111     };
112 
113 
114     TPM2B_DATA outsideInfo = {
115             .size = 0,
116             .buffer = {},
117     };
118 
119 
120     TPML_PCR_SELECTION creationPCR = {
121         .count = 0,
122     };
123 
124     LOG_INFO("\nRSA key will be created.");
125 
126     r = Esys_TR_SetAuth(esys_context, ESYS_TR_RH_OWNER, &authValue);
127     goto_if_error(r, "Error: TR_SetAuth", error);
128 
129     RSRC_NODE_T *primaryHandle_node;
130 
131     r = Esys_CreatePrimary(esys_context, ESYS_TR_RH_OWNER, ESYS_TR_PASSWORD,
132                            ESYS_TR_NONE, ESYS_TR_NONE, &inSensitivePrimary,
133                            &inPublic, &outsideInfo, &creationPCR,
134                            &primaryHandle, &outPublic, &creationData,
135                            &creationHash, &creationTicket);
136     goto_if_error(r, "Error esys create primary", error);
137 
138     r = esys_GetResourceObject(esys_context, primaryHandle,
139                                &primaryHandle_node);
140     goto_if_error(r, "Error Esys GetResourceObject", error);
141 
142     LOG_INFO("Created Primary with handle 0x%08x...",
143              primaryHandle_node->rsrc.handle);
144 
145     r = Esys_TR_SetAuth(esys_context, primaryHandle,
146                         &authValuePrimary);
147     goto_if_error(r, "Error: TR_SetAuth", error);
148 
149     TPM2B_DATA qualifyingData  = { .size = 0, .buffer = {}};
150     TPMT_SIG_SCHEME sig_scheme = { .scheme = TPM2_ALG_NULL };
151 
152     TPML_PCR_SELECTION pcr_selection = {
153         .count = 2,
154         .pcrSelections = {
155             {
156                 .hash = TPM2_ALG_SHA1,
157                 .sizeofSelect = 3,
158                 .pcrSelect = { 0,4,0 } },
159             {
160                 .hash = TPM2_ALG_SHA256,
161                 .sizeofSelect = 3,
162                 .pcrSelect = { 0,4,0 } },
163         }};
164 
165     r = Esys_Quote(esys_context, primaryHandle,
166                    ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
167                    &qualifyingData, &sig_scheme, &pcr_selection,
168                    &attest, &signature);
169     goto_if_error(r, "Error Esys Quote", error);
170 
171     r = Esys_FlushContext(esys_context, primaryHandle);
172     goto_if_error(r, "Error: FlushContext", error);
173 
174     Esys_Free(outPublic);
175     Esys_Free(creationData);
176     Esys_Free(creationHash);
177     Esys_Free(creationTicket);
178 
179     Esys_Free(attest);
180     Esys_Free(signature);
181     return EXIT_SUCCESS;
182 
183  error:
184 
185     if (primaryHandle != ESYS_TR_NONE) {
186         if (Esys_FlushContext(esys_context, primaryHandle) != TSS2_RC_SUCCESS) {
187             LOG_ERROR("Cleanup primaryHandle failed.");
188         }
189     }
190 
191     return EXIT_FAILURE;
192 }
193 
194 int
test_invoke_esapi(ESYS_CONTEXT * esys_context)195 test_invoke_esapi(ESYS_CONTEXT * esys_context) {
196     return test_esys_quote(esys_context);
197 }
198