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 PASSWORD "abc"
23
24 static TSS2_RC
auth_callback(FAPI_CONTEXT * context,char const * description,char ** auth,void * userData)25 auth_callback(
26 FAPI_CONTEXT *context,
27 char const *description,
28 char **auth,
29 void *userData)
30 {
31 (void)description;
32 (void)userData;
33 *auth = strdup(PASSWORD);
34 return_if_null(*auth, "Out of memory.", TSS2_FAPI_RC_MEMORY);
35 return TSS2_RC_SUCCESS;
36 }
37
38 /** Test the FAPI function Fapi_NvSetBits.
39 *
40 * Tested FAPI commands:
41 * - Fapi_Provision()
42 * - Fapi_CreateNv()
43 * - Fapi_NvSetBits()
44 * - Fapi_Delete()
45 * - Fapi_SetAuthCB()
46 *
47 * @param[in,out] context The FAPI_CONTEXT.
48 * @retval EXIT_FAILURE
49 * @retval EXIT_SUCCESS
50 */
51 int
test_fapi_nv_set_bits(FAPI_CONTEXT * context)52 test_fapi_nv_set_bits(FAPI_CONTEXT *context)
53 {
54 TSS2_RC r;
55 char *nvPathBitMap = "/nv/Owner/myNV_BitMap";
56
57 r = Fapi_Provision(context, NULL, NULL, NULL);
58 goto_if_error(r, "Error Fapi_Provision", error);
59
60 /* Test no password, noda set */
61 r = Fapi_CreateNv(context, nvPathBitMap, "bitfield, noda", 0, "", "");
62 goto_if_error(r, "Error Fapi_CreateNv", error);
63
64 uint64_t bitmap = 0x0102030405060608;
65
66 r = Fapi_NvSetBits(context, nvPathBitMap, bitmap);
67 goto_if_error(r, "Error Fapi_SetBits", error);
68
69 r = Fapi_Delete(context, nvPathBitMap);
70 goto_if_error(r, "Error Fapi_Delete", error);
71
72 /* Test with password noda set */
73 r = Fapi_CreateNv(context, nvPathBitMap, "bitfield, noda", 0, "", PASSWORD);
74 goto_if_error(r, "Error Fapi_CreateNv", error);
75
76 r = Fapi_SetAuthCB(context, auth_callback, "");
77 goto_if_error(r, "Error SetPolicyAuthCallback", error);
78
79 r = Fapi_NvSetBits(context, nvPathBitMap, bitmap);
80 goto_if_error(r, "Error Fapi_CreateNv", error);
81
82 r = Fapi_Delete(context, nvPathBitMap);
83 goto_if_error(r, "Error Fapi_Delete", error);
84
85 /* Cleanup */
86 r = Fapi_Delete(context, "/HS/SRK");
87 goto_if_error(r, "Error Fapi_Delete", error);
88
89 r = Fapi_Provision(context, NULL, NULL, NULL);
90 goto_if_error(r, "Error Fapi_Provision", error);
91
92 /* Test no password, noda set */
93 r = Fapi_CreateNv(context, nvPathBitMap, "bitfield, noda", 0, "", "");
94 goto_if_error(r, "Error Fapi_CreateNv", error);
95
96 r = Fapi_NvSetBits(context, nvPathBitMap, bitmap);
97 goto_if_error(r, "Error Fapi_NvSetbits", error);
98
99 r = Fapi_Delete(context, nvPathBitMap);
100 goto_if_error(r, "Error Fapi_Delete", error);
101
102 /* Test with password noda set */
103 r = Fapi_CreateNv(context, nvPathBitMap, "bitfield, noda", 0, "", PASSWORD);
104 goto_if_error(r, "Error Fapi_CreateNv", error);
105
106 r = Fapi_SetAuthCB(context, auth_callback, "");
107 goto_if_error(r, "Error SetPolicyAuthCallback", error);
108
109 r = Fapi_NvSetBits(context, nvPathBitMap, bitmap);
110 goto_if_error(r, "Error Fapi_SetBits", error);
111
112 r = Fapi_Delete(context, nvPathBitMap);
113 goto_if_error(r, "Error Fapi_Delete", error);
114
115
116 r = Fapi_Delete(context, "/");
117
118 goto_if_error(r, "Error Fapi_Delete", error);
119
120 return EXIT_SUCCESS;
121
122 error:
123 Fapi_Delete(context, "/HS/SRK");
124 return EXIT_FAILURE;
125 }
126
127 int
test_invoke_fapi(FAPI_CONTEXT * context)128 test_invoke_fapi(FAPI_CONTEXT *context)
129 {
130 return test_fapi_nv_set_bits(context);
131 }
132