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 <string.h>
13
14 #include "tss2_fapi.h"
15
16 #include "test-fapi.h"
17 #define LOGMODULE test
18 #include "util/log.h"
19 #include "util/aux_util.h"
20
21 #define PASSWORD NULL
22 #define SIGN_TEMPLATE "sign,noDa"
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 char *pw = PASSWORD;
34 if (!pw)
35 return TSS2_FAPI_RC_GENERAL_FAILURE;
36
37 *auth = strdup(pw);
38 return TSS2_RC_SUCCESS;
39 }
40
41 /** Test the FAPI functions for key creation and usage.
42 *
43 * Tested FAPI commands:
44 * - Fapi_Provision()
45 * - Fapi_SetAuthCB()
46 * - Fapi_CreateKey()
47 * - Fapi_GetTpmBlobs()
48 * - Fapi_Sign()
49 * - Fapi_SetCertificate()
50 * - Fapi_List()
51 * - Fapi_Delete()
52 *
53 * @param[in,out] context The FAPI_CONTEXT.
54 * @retval EXIT_FAILURE
55 * @retval EXIT_SUCCESS
56 */
57 int
test_fapi_key_create_sign_policy_provision(FAPI_CONTEXT * context)58 test_fapi_key_create_sign_policy_provision(FAPI_CONTEXT *context)
59 {
60 TSS2_RC r;
61 char *sigscheme = NULL;
62 uint8_t *publicblob = NULL;
63 uint8_t *privateblob = NULL;
64 uint8_t *signature = NULL;
65 char *publicKey = NULL;
66 char *path_list = NULL;
67
68 const char *cert =
69 "-----BEGIN CERTIFICATE-----\n"
70 "MIIDBjCCAe4CCQDcvXBOEVM0UTANBgkqhkiG9w0BAQsFADBFMQswCQYDVQQGEwJE\n"
71 "RTETMBEGA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\n"
87 "-----END CERTIFICATE-----\n";
88
89 if (strcmp("P_ECC", fapi_profile) != 0)
90 sigscheme = "RSA_PSS";
91
92 /* We need to reset the passwords again, in order to not brick physical TPMs */
93 r = Fapi_Provision(context, NULL, PASSWORD, NULL);
94 goto_if_error(r, "Error Fapi_Provision", error);
95
96 r = Fapi_SetAuthCB(context, auth_callback, NULL);
97 goto_if_error(r, "Error SetPolicyAuthCallback", error);
98
99 r = Fapi_CreateKey(context, "HS/SRK/mySignKey", SIGN_TEMPLATE, "",
100 PASSWORD);
101
102 goto_if_error(r, "Error Fapi_CreateKey", error);
103 size_t signatureSize = 0;
104
105 TPM2B_DIGEST digest = {
106 .size = 20,
107 .buffer = {
108 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
109 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f
110 }
111 };
112
113 size_t publicsize;
114 size_t privatesize;
115
116 r = Fapi_GetTpmBlobs(context, "HS/SRK/mySignKey", &publicblob,
117 &publicsize,
118 &privateblob, &privatesize, NULL);
119 goto_if_error(r, "Error Fapi_GetTpmBlobs", error);
120
121 r = Fapi_Sign(context, "HS/SRK/mySignKey", sigscheme,
122 &digest.buffer[0], digest.size, &signature, &signatureSize,
123 &publicKey, NULL);
124 goto_if_error(r, "Error Fapi_Sign", error);
125
126
127 r = Fapi_SetCertificate(context, "HS/SRK/mySignKey", cert);
128 goto_if_error(r, "Error Fapi_SetCertificate", error);
129
130 r = Fapi_List(context, "/", &path_list);
131 goto_if_error(r, "Error Fapi_Delete", error);
132
133 LOG_INFO("\nPathList:\n%s\n", path_list);
134
135 r = Fapi_Delete(context, "/");
136 goto_if_error(r, "Error Fapi_Delete", error);
137
138 SAFE_FREE(publicblob);
139 SAFE_FREE(privateblob);
140 SAFE_FREE(signature);
141 SAFE_FREE(publicKey);
142 SAFE_FREE(path_list);
143 return EXIT_SUCCESS;
144
145 error:
146 Fapi_Delete(context, "/HS/SRK");
147 SAFE_FREE(publicblob);
148 SAFE_FREE(privateblob);
149 SAFE_FREE(signature);
150 SAFE_FREE(publicKey);
151 SAFE_FREE(path_list);
152 return EXIT_FAILURE;
153 }
154
155 int
test_invoke_fapi(FAPI_CONTEXT * fapi_context)156 test_invoke_fapi(FAPI_CONTEXT *fapi_context)
157 {
158 return test_fapi_key_create_sign_policy_provision(fapi_context);
159 }
160