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
16 #include "tss2_fapi.h"
17
18 #define LOGMODULE test
19 #include "util/log.h"
20 #include "util/aux_util.h"
21
22 #define NV_SIZE 32
23
24 #define PASSWORD "abc"
25
26 static char *password;
27
28 static TSS2_RC
auth_callback(FAPI_CONTEXT * context,char const * description,char ** auth,void * userData)29 auth_callback(
30 FAPI_CONTEXT *context,
31 char const *description,
32 char **auth,
33 void *userData)
34 {
35 (void)description;
36 (void)userData;
37 *auth = strdup(password);
38 return_if_null(*auth, "Out of memory.", TSS2_FAPI_RC_MEMORY);
39 return TSS2_RC_SUCCESS;
40 }
41
42
43 /** Test the FAPI function FAPI_NvExtend.
44 *
45 * Tested FAPI commands:
46 * - Fapi_Provision()
47 * - Fapi_CreateNv()
48 * - Fapi_NvExtend()
49 * - Fapi_Delete()
50 * - Fapi_SetAuthCB()
51 *
52 * @param[in,out] context The FAPI_CONTEXT.
53 * @retval EXIT_FAILURE
54 * @retval EXIT_SUCCESS
55 */
56 int
test_fapi_nv_extend(FAPI_CONTEXT * context)57 test_fapi_nv_extend(FAPI_CONTEXT *context)
58 {
59 TSS2_RC r;
60 char *nvPathExtend = "/nv/Owner/myNVextend";
61 uint8_t *data_dest = NULL;
62 char *log = NULL;
63 size_t dest_size;
64
65 r = Fapi_Provision(context, NULL, NULL, NULL);
66 goto_if_error(r, "Error Fapi_Provision", error);
67
68 /* Test no password, noda set */
69 r = Fapi_CreateNv(context, nvPathExtend, "pcr, noda", 0, "", "");
70 goto_if_error(r, "Error Fapi_CreateNv", error);
71
72 uint8_t data_src[NV_SIZE] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
73 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
74 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
75 0, 1
76 };
77
78
79 r = Fapi_NvExtend(context, nvPathExtend, &data_src[0], NV_SIZE, "{ \"test\": \"myfile\" }");
80 goto_if_error(r, "Error Fapi_NV_EXTEND", error);
81
82 r = Fapi_NvRead(context, nvPathExtend, &data_dest, &dest_size, &log);
83 goto_if_error(r, "Error Fapi_NvRead", error);
84
85 fprintf(stderr, "\nLog:\n%s\n", log);
86 SAFE_FREE(data_dest);
87
88 r = Fapi_NvExtend(context, nvPathExtend, &data_src[0], NV_SIZE, "{ \"test\": \"myfile\" }");
89 goto_if_error(r, "Error Fapi_NV_EXTEND", error);
90
91 SAFE_FREE(log);
92 r = Fapi_NvRead(context, nvPathExtend, &data_dest, &dest_size, &log);
93 goto_if_error(r, "Error Fapi_NvRead", error);
94
95 fprintf(stderr, "\nLog:\n%s\n", log);
96
97 r = Fapi_Delete(context, nvPathExtend);
98 goto_if_error(r, "Error Fapi_NV_Undefine", error);
99
100 r = Fapi_SetAuthCB(context, auth_callback, "");
101 goto_if_error(r, "Error SetPolicyAuthCallback", error);
102
103 /* Test with password noda set */
104 password = PASSWORD;
105 r = Fapi_CreateNv(context, nvPathExtend, "pcr, noda", 0, "", PASSWORD);
106 goto_if_error(r, "Error Fapi_CreateNv", error);
107
108 r = Fapi_SetAuthCB(context, auth_callback, "");
109 goto_if_error(r, "Error SetPolicyAuthCallback", error);
110
111 r = Fapi_NvExtend(context, nvPathExtend, &data_src[0], NV_SIZE, "{ \"test\": \"myfile\" }");
112 goto_if_error(r, "Error Fapi_NV_EXTEN", error);
113
114 r = Fapi_Delete(context, nvPathExtend);
115 goto_if_error(r, "Error Fapi_NV_Undefine", error);
116
117 /* Test no password, noda clear */
118 password = "";
119 r = Fapi_CreateNv(context, nvPathExtend, "pcr", 0, "", "");
120 goto_if_error(r, "Error Fapi_CreateNv", error);
121
122 r = Fapi_NvExtend(context, nvPathExtend, &data_src[0], NV_SIZE, "{ \"test\": \"myfile\" }");
123 goto_if_error(r, "Error Fapi_NV_EXTEN", error);
124
125 r = Fapi_Delete(context, nvPathExtend);
126 goto_if_error(r, "Error Fapi_NV_Undefine", error);
127
128 /* Test with password noda clear */
129 password = PASSWORD;
130 r = Fapi_CreateNv(context, nvPathExtend, "pcr", 0, "", PASSWORD);
131 goto_if_error(r, "Error Fapi_CreateNv", error);
132
133 r = Fapi_SetAuthCB(context, auth_callback, "");
134 goto_if_error(r, "Error SetPolicyAuthCallback", error);
135
136 r = Fapi_NvExtend(context, nvPathExtend, &data_src[0], NV_SIZE, "{ \"test\": \"myfile\" }");
137 goto_if_error(r, "Error Fapi_NV_EXTEN", error);
138
139 r = Fapi_Delete(context, nvPathExtend);
140 goto_if_error(r, "Error Fapi_NV_Undefine", error);
141
142 r = Fapi_Delete(context, "/");
143 goto_if_error(r, "Error Fapi_Delete", error);
144
145 SAFE_FREE(log);
146 SAFE_FREE(data_dest);
147 return EXIT_SUCCESS;
148
149 error:
150 SAFE_FREE(log);
151 SAFE_FREE(data_dest);
152 return EXIT_FAILURE;
153 }
154
155 int
test_invoke_fapi(FAPI_CONTEXT * context)156 test_invoke_fapi(FAPI_CONTEXT *context)
157 {
158 return test_fapi_nv_extend(context);
159 }
160