1 /* SPDX-License-Identifier: BSD-2 */
2 /*******************************************************************************
3 * Copyright 2017-2018, Fraunhofer SIT sponsored by Infineon Technologies AG
4 * All rights reserved.
5 *******************************************************************************/
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <unistd.h>
12
13 #include "tss2_fapi.h"
14 #include "fapi_util.h"
15 #include "fapi_int.h"
16
17 #include "esys_iutil.h"
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 TSS2_RC_SUCCESS;
35 }
36 #define SIGN_TEMPLATE "T_RSA_SIGN"
37 #define PROFILE "P_RSA"
38 #define PROFILE_DIR ""
39
40
41 /** Test the FAPI functions for PolicyImport and PolicyPassword callbacks.
42 *
43 * Tested FAPI commands:
44 * - Fapi_Provision()
45 * - Fapi_PolicyImport()
46 * - Fapi_Key_Create()
47 * - Fapi_SetPolicyAuthCallback()
48 * - Fapi_Key_Sign()
49 * - Fapi_Entity_Delete()
50 * - Fapi_Entities_List()
51 *
52 * Tested Policies:
53 * - PolicyPassword
54 *
55 * @param[in,out] context The FAPI_CONTEXT.
56 * @retval EXIT_FAILURE
57 * @retval EXIT_SUCCESS
58 */
59 int
test_fapi_key_create_policy_password_sign(FAPI_CONTEXT * context)60 test_fapi_key_create_policy_password_sign(FAPI_CONTEXT *context)
61 {
62 TSS2_RC r;
63 char* policy_name = "policies/pol_password";
64 char* policy_file = NULL;
65 FILE *stream = NULL;
66 char *json_policy = NULL;
67 long policy_size;
68
69 r = Fapi_Provision(context, PROFILE, NULL, NULL, NULL, NULL, NULL);
70 goto_if_error(r, "Error Fapi_Provision", error);
71
72 r = ifapi_asprintf(&policy_file, "%s/%s.json", context->config.profile_dir,
73 policy_name);
74 goto_if_error(r, "Create file name", error);
75
76 r = Fapi_PCR_Reset(context, 16);
77 goto_if_error(r, "Reset PCR 16", error);
78
79 stream = fopen(policy_file, "r");
80 if (!stream) {
81 LOG_ERROR("File %s does not exist", policy_file);
82 goto error;
83 }
84 fseek(stream, 0L, SEEK_END);
85 policy_size = ftell(stream);
86 fclose(stream);
87 json_policy = malloc(policy_size + 1);
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_PolicyImport(context, policy_name, json_policy);
97 goto_if_error(r, "Error Fapi_Entities_List", error);
98
99 r = Fapi_Key_Create(context, PROFILE_DIR "HS/SNK/mySignKey", SIGN_TEMPLATE,
100 policy_name, PASSWORD);
101 goto_if_error(r, "Error Fapi_Key_Create", error);
102 size_t signatureSize = 0;
103 size_t publicKeySize = 0;
104 size_t certificateSize = 0;
105
106 TPM2B_DIGEST digest = {
107 .size = 20,
108 .buffer = { 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
109 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f }
110 };
111
112 uint8_t *signature;
113 uint8_t *publicKey;
114
115 r = Fapi_SetPolicyAuthCallback(context, auth_callback, "");
116 goto_if_error(r, "Error SetPolicyAuthCallback", error);
117
118 r = Fapi_Key_Sign(context, PROFILE_DIR "HS/SNK/mySignKey",
119 &digest.buffer[0], digest.size, &signature, &signatureSize,
120 &publicKey, &publicKeySize, NULL, &certificateSize);
121 goto_if_error(r, "Error Fapi_Key_Sign", error);
122
123 r = Fapi_Entity_Delete(context, PROFILE "/HE/EK");
124 goto_if_error(r, "Error Fapi_Entity_Delete", error);
125
126 size_t numPaths;
127 char **pathlist;
128
129 r = Fapi_Entities_List(context, PROFILE "/", &pathlist ,&numPaths);
130 goto_if_error(r, "Error Fapi_Entities_List", error);
131
132 /* The two storage keys should remain */
133 if (numPaths != 2) {
134 LOG_ERROR("Wrong number of objects in key store");
135 goto error;
136 }
137
138 return EXIT_SUCCESS;
139
140 error:
141 return EXIT_FAILURE;
142 }
143
144 int
test_invoke_fapi(FAPI_CONTEXT * fapi_context)145 test_invoke_fapi(FAPI_CONTEXT * fapi_context) {
146 return test_fapi_key_create_policy_password_sign(fapi_context);
147 }
148