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 #define LOGMODULE test
20 #include "util/log.h"
21 #include "util/aux_util.h"
22
23 #define NV_SIZE 10
24
25 /** Test the FAPI policy PolicyNvWritten.
26 *
27 * Tested FAPI commands:
28 * - Fapi_Provision()
29 * - Fapi_CreateNv()
30 * - Fapi_SetAppData()
31 * - Fapi_GetAppData()
32 * - Fapi_NvWrite()
33 * - Fapi_Delete()
34 *
35 * Tested Policies:
36 * - PolicyNvWritten
37 *
38 * @param[in,out] context The FAPI_CONTEXT.
39 * @retval EXIT_FAILURE
40 * @retval EXIT_SUCCESS
41 */
42 int
test_fapi_nv_written_policy(FAPI_CONTEXT * context)43 test_fapi_nv_written_policy(FAPI_CONTEXT *context)
44 {
45 TSS2_RC r;
46 char *nvPathOrdinary = "/nv/Owner/myNV";
47 char *policy_name = "/policy/pol_nv_written";
48 char *policy_file = TOP_SOURCEDIR "/test/data/fapi/policy/pol_nv_written.json";
49 FILE *stream = NULL;
50 char *json_policy = NULL;
51 long policy_size;
52 uint8_t *appData = NULL;
53 size_t appDataSize;
54
55 r = Fapi_Provision(context, NULL, NULL, NULL);
56 goto_if_error(r, "Error Fapi_Provision", error);
57
58 uint8_t data_src[NV_SIZE] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
59
60 stream = fopen(policy_file, "r");
61 if (!stream) {
62 LOG_ERROR("File %s does not exist", policy_file);
63 goto error;
64 }
65 fseek(stream, 0L, SEEK_END);
66 policy_size = ftell(stream);
67 fclose(stream);
68 json_policy = malloc(policy_size + 1);
69 goto_if_null(json_policy,
70 "Could not allocate memory for the JSON policy",
71 TSS2_FAPI_RC_MEMORY, error);
72
73 stream = fopen(policy_file, "r");
74 ssize_t ret = read(fileno(stream), json_policy, policy_size);
75 if (ret != policy_size) {
76 LOG_ERROR("IO error %s.", policy_file);
77 goto error;
78 }
79 json_policy[policy_size] = '\0';
80
81 r = Fapi_Import(context, policy_name, json_policy);
82 goto_if_error(r, "Error Fapi_Import", error);
83
84 /* Empty auth noda set */
85 r = Fapi_CreateNv(context, nvPathOrdinary, "noda", 10, policy_name, "");
86 goto_if_error(r, "Error Fapi_CreateNv", error);
87
88 r = Fapi_SetAppData(context, nvPathOrdinary, data_src, NV_SIZE);
89 goto_if_error(r, "Error Fapi_SetAppData", error);
90
91 r = Fapi_GetAppData(context, nvPathOrdinary, &appData, &appDataSize);
92 goto_if_error(r, "Error Fapi_GetAppData", error);
93
94 if (NV_SIZE != appDataSize ||
95 memcmp(appData, &data_src[0], appDataSize) != 0) {
96 LOG_ERROR("Error: AppData equal to origin");
97 goto error;
98 }
99
100 r = Fapi_NvWrite(context, nvPathOrdinary, &data_src[0], NV_SIZE);
101 goto_if_error(r, "Error Fapi_NvWrite", error);
102
103 r = Fapi_Delete(context, nvPathOrdinary);
104 goto_if_error(r, "Error Fapi_NV_Undefine", error);
105
106 r = Fapi_Delete(context, "/");
107 goto_if_error(r, "Error Fapi_Delete", error);
108
109
110 SAFE_FREE(json_policy);
111 SAFE_FREE(appData);
112
113 return EXIT_SUCCESS;
114
115 error:
116 SAFE_FREE(json_policy);
117 SAFE_FREE(appData);
118
119 return EXIT_FAILURE;
120 }
121
122 int
test_invoke_fapi(FAPI_CONTEXT * context)123 test_invoke_fapi(FAPI_CONTEXT *context)
124 {
125 return test_fapi_nv_written_policy(context);
126 }
127