• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 PASSWORD "abc"
25 
26 static TSS2_RC
auth_callback(FAPI_CONTEXT * context,char const * description,char ** auth,void * userData)27 auth_callback(
28     FAPI_CONTEXT *context,
29     char const *description,
30     char **auth,
31     void *userData)
32 {
33     (void)description;
34     (void)userData;
35     *auth = strdup(PASSWORD);
36     return_if_null(*auth, "Out of memory.", TSS2_FAPI_RC_MEMORY);
37     return TSS2_RC_SUCCESS;
38 }
39 
40 /** Test the FAPI function FAPI_NvIncrement.
41  *
42  * Tested FAPI commands:
43  *  - Fapi_Provision()
44  *  - Fapi_Import()
45  *  - Fapi_CreateNv()
46  *  - Fapi_SetAuthCB()
47  *  - Fapi_ChangeAuth()
48  *  - Fapi_Delete()
49  *  - Fapi_NvIncrement()
50  *
51  * Tested Policies:
52  *  - PolicyAuthValue
53  *  - PolicyCommandCode
54  *
55  * @param[in,out] context The FAPI_CONTEXT.
56  * @retval EXIT_FAILURE
57  * @retval EXIT_SUCCESS
58  */
59 int
test_fapi_nv_increment(FAPI_CONTEXT * context)60 test_fapi_nv_increment(FAPI_CONTEXT *context)
61 {
62     TSS2_RC r;
63     char *nvPathCounter = "nv/Owner/myNV_Counter";
64     char *policy_name = "/policy/pol_nv_change_auth";
65     char *policy_file = TOP_SOURCEDIR "/test/data/fapi/policy/pol_nv_change_auth.json";
66     FILE *stream = NULL;
67     char *json_policy = NULL;
68     long policy_size;
69 
70     r = Fapi_Provision(context, NULL, NULL, NULL);
71     goto_if_error(r, "Error Fapi_Provision", error);
72 
73     r = pcr_reset(context, 16);
74     goto_if_error(r, "Error pcr_reset", error);
75 
76     stream = fopen(policy_file, "r");
77     if (!stream) {
78         LOG_ERROR("File %s does not exist", policy_file);
79         goto error;
80     }
81     fseek(stream, 0L, SEEK_END);
82     policy_size = ftell(stream);
83     fclose(stream);
84     json_policy = malloc(policy_size + 1);
85     goto_if_null(json_policy,
86             "Could not allocate memory for the JSON policy",
87             TSS2_FAPI_RC_MEMORY, error);
88     stream = fopen(policy_file, "r");
89     ssize_t ret = read(fileno(stream), json_policy, policy_size);
90     if (ret != policy_size) {
91         LOG_ERROR("IO error %s.", policy_file);
92         goto error;
93     }
94     json_policy[policy_size] = '\0';
95 
96     r = Fapi_Import(context, policy_name, json_policy);
97     goto_if_error(r, "Error Fapi_Import", error);
98 
99     /* Test no password, noda set */
100     r = Fapi_CreateNv(context, nvPathCounter, "counter, noda", 0, policy_name, "abc");
101     goto_if_error(r, "Error Fapi_CreateNv", error);
102 
103     r = Fapi_SetAuthCB(context, auth_callback, "");
104     goto_if_error(r, "Error SetPolicyAuthCallback", error);
105 
106     r = Fapi_ChangeAuth(context, nvPathCounter, "abc");
107     goto_if_error(r, "Error Fapi_CreateNv", error);
108 
109     r = Fapi_Delete(context, nvPathCounter);
110     goto_if_error(r, "Error Fapi_NV_Undefine", error);
111 
112     /* Test no password, noda set */
113     r = Fapi_CreateNv(context, nvPathCounter, "counter, noda", 0, "", "");
114     goto_if_error(r, "Error Fapi_CreateNv", error);
115 
116     r = Fapi_NvIncrement(context, nvPathCounter);
117     goto_if_error(r, "Error Fapi_CreateNv", error);
118 
119        r = Fapi_Delete(context, nvPathCounter);
120     goto_if_error(r, "Error Fapi_NV_Undefine", error);
121 
122     /* Test with password noda set */
123     r = Fapi_CreateNv(context, nvPathCounter, "counter, noda", 0, "", PASSWORD);
124     goto_if_error(r, "Error Fapi_CreateNv", error);
125 
126     r = Fapi_SetAuthCB(context, auth_callback, "");
127     goto_if_error(r, "Error SetPolicyAuthCallback", error);
128 
129     r = Fapi_NvIncrement(context, nvPathCounter);
130     goto_if_error(r, "Error Fapi_CreateNv", error);
131 
132     r = Fapi_Delete(context, nvPathCounter);
133     goto_if_error(r, "Error Fapi_NV_Undefine", error);
134 
135     /* Test no password, noda clear */
136     r = Fapi_CreateNv(context, nvPathCounter, "counter", 0, "", "");
137     goto_if_error(r, "Error Fapi_CreateNv", error);
138 
139     r = Fapi_NvIncrement(context, nvPathCounter);
140     goto_if_error(r, "Error Fapi_CreateNv", error);
141 
142     r = Fapi_Delete(context, nvPathCounter);
143     goto_if_error(r, "Error Fapi_NV_Undefine", error);
144 
145     /* Test with password noda clear */
146     r = Fapi_CreateNv(context, nvPathCounter, "counter", 0, "", PASSWORD);
147     goto_if_error(r, "Error Fapi_CreateNv", error);
148 
149     r = Fapi_SetAuthCB(context, auth_callback, "");
150     goto_if_error(r, "Error SetPolicyAuthCallback", error);
151 
152     r = Fapi_NvIncrement(context, nvPathCounter);
153     goto_if_error(r, "Error Fapi_CreateNv", error);
154 
155     r = Fapi_Delete(context, nvPathCounter);
156     goto_if_error(r, "Error Fapi_NV_Undefine", error);
157 
158 
159     r = Fapi_Delete(context, "/");
160     goto_if_error(r, "Error Fapi_Delete", error);
161 
162     SAFE_FREE(json_policy);
163     return EXIT_SUCCESS;
164 
165 error:
166     SAFE_FREE(json_policy);
167     return EXIT_FAILURE;
168 }
169 
170 int
test_invoke_fapi(FAPI_CONTEXT * context)171 test_invoke_fapi(FAPI_CONTEXT *context)
172 {
173     return test_fapi_nv_increment(context);
174 }
175