xref: /aosp_15_r20/external/tpm2-tss/test/integration/fapi-nv-ordinary.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 #include <stdio.h>
13 #include <inttypes.h>
14 #include <string.h>
15 #include <unistd.h>
16 
17 #include "tss2_fapi.h"
18 
19 #include "test-fapi.h"
20 #define LOGMODULE test
21 #include "util/log.h"
22 #include "util/aux_util.h"
23 
24 #define NV_SIZE 1200
25 
26 #define PASSWORD "abc"
27 
28 static char *password;
29 
30 static TSS2_RC
auth_callback(FAPI_CONTEXT * context,char const * description,char ** auth,void * userData)31 auth_callback(
32     FAPI_CONTEXT *context,
33     char const *description,
34     char **auth,
35     void *userData)
36 {
37     (void)(context);
38     (void)description;
39     (void)userData;
40     *auth = strdup(PASSWORD);
41     return_if_null(*auth, "Out of memory.", TSS2_FAPI_RC_MEMORY);
42     return TSS2_RC_SUCCESS;
43 }
44 
45 static TSS2_RC
action_callback(FAPI_CONTEXT * context,const char * action,void * userData)46 action_callback(
47     FAPI_CONTEXT *context,
48     const char *action,
49     void *userData)
50 {
51     (void)(context);
52     (void)(userData);
53     if (strcmp(action, "myaction")) {
54         LOG_ERROR("Bad action: %s", action);
55         return TSS2_FAPI_RC_GENERAL_FAILURE;
56     }
57     return TSS2_RC_SUCCESS;
58 }
59 
60 /** Test the FAPI NV functions.
61  *
62  * Tested FAPI commands:
63  *  - Fapi_Provision()
64  *  - Fapi_Import()
65  *  - Fapi_SetPolicyActionCB()
66  *  - Fapi_CreateNv()
67  *  - Fapi_NvWrite()
68  *  - Fapi_NvRead()
69  *  - Fapi_Delete()
70  *  - Fapi_SetDescription()
71  *  - Fapi_GetDescription()
72  *  - Fapi_SetAuthCB()
73  *
74  * Tested Policies:
75  *  - PolicyAction
76  *
77  * @param[in,out] context The FAPI_CONTEXT.
78  * @retval EXIT_FAILURE
79  * @retval EXIT_SUCCESS
80  */
81 int
test_fapi_nv_ordinary(FAPI_CONTEXT * context)82 test_fapi_nv_ordinary(FAPI_CONTEXT *context)
83 {
84     TSS2_RC r;
85     char *nvPathOrdinary = "/nv/Owner/myNV";
86     uint8_t data_src[NV_SIZE];
87     uint8_t *data_dest = NULL;
88     size_t dest_size = NV_SIZE;
89     char *description1 = "nvDescription";
90     char *description2 = NULL;
91     char *policy_name = "/policy/pol_action";
92     char *policy_file = TOP_SOURCEDIR "/test/data/fapi/policy/pol_action.json";
93     FILE *stream = NULL;
94     char *json_policy = NULL;
95     long policy_size;
96 
97     for (int i = 0; i < NV_SIZE; i++) {
98         data_src[i] = (i % 10) + 1;
99     }
100 
101     r = Fapi_Provision(context, NULL, NULL, NULL);
102     goto_if_error(r, "Error Fapi_Provision", error);
103 
104     r = pcr_reset(context, 16);
105     goto_if_error(r, "Error pcr_reset", error);
106 
107     stream = fopen(policy_file, "r");
108     if (!stream) {
109         LOG_ERROR("File %s does not exist", policy_file);
110         goto error;
111     }
112     fseek(stream, 0L, SEEK_END);
113     policy_size = ftell(stream);
114     fclose(stream);
115     json_policy = malloc(policy_size + 1);
116     goto_if_null(json_policy,
117             "Could not allocate memory for the JSON policy",
118             TSS2_FAPI_RC_MEMORY, error);
119     stream = fopen(policy_file, "r");
120     ssize_t ret = read(fileno(stream), json_policy, policy_size);
121     if (ret != policy_size) {
122         LOG_ERROR("IO error %s.", policy_file);
123         goto error;
124     }
125     json_policy[policy_size] = '\0';
126 
127     r = Fapi_Import(context, policy_name, json_policy);
128     goto_if_error(r, "Error Fapi_Import", error);
129 
130     r = Fapi_SetPolicyActionCB(context, action_callback, "");
131     goto_if_error(r, "Error Fapi_SetPolicyActionCB", error);
132 
133     /* Test with policy */
134     r = Fapi_CreateNv(context, nvPathOrdinary, "noda", NV_SIZE, policy_name, "");
135     goto_if_error(r, "Error Fapi_CreateNv", error);
136 
137     r = Fapi_NvWrite(context, nvPathOrdinary, &data_src[0], NV_SIZE);
138     goto_if_error(r, "Error Fapi_NvWrite", error);
139 
140     r = Fapi_NvRead(context, nvPathOrdinary, &data_dest, &dest_size, NULL);
141     goto_if_error(r, "Error Fapi_NvRead", error);
142 
143     if (dest_size != NV_SIZE ||
144         memcmp(data_src, data_dest, dest_size) != 0) {
145         LOG_ERROR("Error: result of nv read is wrong.");
146         goto error;
147     }
148 
149      r = Fapi_Delete(context, nvPathOrdinary);
150     goto_if_error(r, "Error Fapi_NV_Undefine", error);
151     SAFE_FREE(data_dest);
152 
153     /* Empty auth noda set */
154     r = Fapi_CreateNv(context, nvPathOrdinary, "noda", NV_SIZE, "", "");
155     goto_if_error(r, "Error Fapi_CreateNv", error);
156 
157     r = Fapi_NvWrite(context, nvPathOrdinary, &data_src[0], NV_SIZE);
158     goto_if_error(r, "Error Fapi_NvWrite", error);
159 
160     r = Fapi_NvRead(context, nvPathOrdinary, &data_dest, &dest_size, NULL);
161     goto_if_error(r, "Error Fapi_NvRead", error);
162 
163     if (dest_size != NV_SIZE ||
164         memcmp(data_src, data_dest, dest_size) != 0) {
165         LOG_ERROR("Error: result of nv read is wrong.");
166         goto error;
167     }
168 
169     r = Fapi_Delete(context, nvPathOrdinary);
170     goto_if_error(r, "Error Fapi_NV_Undefine", error);
171     SAFE_FREE(data_dest);
172 
173     r = Fapi_SetAuthCB(context, auth_callback, "");
174     goto_if_error(r, "Error Fapi_SetAuthCB", error);
175 
176     /* Password set and noda set */
177     password = PASSWORD;
178     r = Fapi_CreateNv(context, nvPathOrdinary, "", NV_SIZE, "", password);
179     goto_if_error(r, "Error Fapi_CreateNv", error);
180 
181     r = Fapi_SetAuthCB(context, auth_callback, "");
182     goto_if_error(r, "Error SetPolicyAuthCallback", error);
183 
184     r = Fapi_NvWrite(context, nvPathOrdinary, &data_src[0], NV_SIZE);
185     goto_if_error(r, "Error Fapi_NvWrite", error);
186 
187     r = Fapi_NvRead(context, nvPathOrdinary, &data_dest, &dest_size, NULL);
188     goto_if_error(r, "Error Fapi_NvRead", error);
189 
190     if (dest_size != NV_SIZE ||
191         memcmp(data_src, data_dest, dest_size) != 0) {
192         LOG_ERROR("Error: result of nv read is wrong.");
193         goto error;
194     }
195 
196     r = Fapi_Delete(context, nvPathOrdinary);
197     goto_if_error(r, "Error Fapi_NV_Undefine", error);
198     SAFE_FREE(data_dest);
199 
200     /* Empty auth noda clear */
201     password = "";
202     r = Fapi_CreateNv(context, nvPathOrdinary, "", NV_SIZE, "", "");
203     goto_if_error(r, "Error Fapi_CreateNv", error);
204 
205     r = Fapi_SetDescription(context, nvPathOrdinary, description1);
206     goto_if_error(r, "Error Fapi_SetDescription", error);
207 
208     r = Fapi_GetDescription(context, nvPathOrdinary, &description2);
209     goto_if_error(r, "Error Fapi_GetDescription", error);
210 
211     if (strcmp(description1, description2) != 0) {
212         goto_if_error(r, "Different descriptions", error);
213     }
214 
215     r = Fapi_NvWrite(context, nvPathOrdinary, &data_src[0], NV_SIZE);
216     goto_if_error(r, "Error Fapi_NvWrite", error);
217 
218     r = Fapi_NvRead(context, nvPathOrdinary, &data_dest, &dest_size, NULL);
219     goto_if_error(r, "Error Fapi_NvRead", error);
220 
221     if (dest_size != NV_SIZE ||
222         memcmp(data_src, data_dest, dest_size) != 0) {
223         LOG_ERROR("Error: result of nv read is wrong.");
224         goto error;
225     }
226 
227     r = Fapi_Delete(context, nvPathOrdinary);
228     goto_if_error(r, "Error Fapi_NV_Undefine", error);
229     SAFE_FREE(data_dest);
230 
231     /* Password set and noda clear  */
232     password = PASSWORD;
233     r = Fapi_CreateNv(context, nvPathOrdinary, "", NV_SIZE, "", password);
234     goto_if_error(r, "Error Fapi_CreateNv", error);
235 
236     r = Fapi_SetAuthCB(context, auth_callback, "");
237     goto_if_error(r, "Error SetPolicyAuthCallback", error);
238 
239     r = Fapi_NvWrite(context, nvPathOrdinary, &data_src[0], NV_SIZE);
240     goto_if_error(r, "Error Fapi_NvWrite", error);
241 
242     r = Fapi_NvRead(context, nvPathOrdinary, &data_dest, &dest_size, NULL);
243     goto_if_error(r, "Error Fapi_NvRead", error);
244 
245     if (dest_size != NV_SIZE ||
246         memcmp(data_src, data_dest, dest_size) != 0) {
247         LOG_ERROR("Error: result of nv read is wrong.");
248         goto error;
249     }
250     r = Fapi_Delete(context, nvPathOrdinary);
251     goto_if_error(r, "Error Fapi_NV_Undefine", error);
252 
253     r = Fapi_Delete(context, "/");
254     goto_if_error(r, "Error Fapi_Delete", error);
255 
256     SAFE_FREE(data_dest);
257     SAFE_FREE(description2);
258     SAFE_FREE(json_policy);
259     return EXIT_SUCCESS;
260 
261 error:
262     SAFE_FREE(data_dest);
263     SAFE_FREE(description2);
264     SAFE_FREE(json_policy);
265     return EXIT_FAILURE;
266 }
267 
268 int
test_invoke_fapi(FAPI_CONTEXT * context)269 test_invoke_fapi(FAPI_CONTEXT *context)
270 {
271     return test_fapi_nv_ordinary(context);
272 }
273