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 #define LOGMODULE test
17 #include "util/log.h"
18 #include "util/aux_util.h"
19
20 #ifdef FAPI_PASSWORD
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 #else /*FAPI_PASSWORD */
38 #define PASSWORD NULL
39 #endif /* FAPI_PASSWORD */
40
41 #ifdef FAPI_DA
42 #define SIGN_TEMPLATE "sign"
43 #else
44 #define SIGN_TEMPLATE "sign, noDa"
45 #endif
46
47 /** Test the FAPI functions for key creation and usage with noda and da flag.
48 *
49 * Tested FAPI commands:
50 * - Fapi_Provision()
51 * - Fapi_CreateKey()
52 * - Fapi_SetAuthCB()
53 * - Fapi_Sign()
54 * - Fapi_Delete()
55 *
56 * @param[in,out] context The FAPI_CONTEXT.
57 * @retval EXIT_FAILURE
58 * @retval EXIT_SUCCESS
59 */
60 int
test_fapi_key_create_ckda_sign(FAPI_CONTEXT * context)61 test_fapi_key_create_ckda_sign(FAPI_CONTEXT *context)
62 {
63 TSS2_RC r;
64
65 uint8_t *signature = NULL;
66 char *publicKey = NULL;
67
68 r = Fapi_Provision(context, NULL, NULL, NULL);
69 goto_if_error(r, "Error Fapi_Provision", error);
70
71 r = Fapi_CreateKey(context, "HS/SRK/mySignKey", SIGN_TEMPLATE, "",
72 PASSWORD);
73 goto_if_error(r, "Error Fapi_CreateKey", error);
74 size_t signatureSize = 0;
75
76 TPM2B_DIGEST digest = {
77 .size = 20,
78 .buffer = {
79 0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
80 0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f
81 }
82 };
83
84 #ifdef FAPI_PASSWORD
85 r = Fapi_SetAuthCB(context, auth_callback, "");
86 goto_if_error(r, "Error SetPolicyAuthCallback", error);
87 #endif
88
89 r = Fapi_Sign(context, "HS/SRK/mySignKey", NULL,
90 &digest.buffer[0], digest.size, &signature, &signatureSize,
91 &publicKey, NULL);
92 goto_if_error(r, "Error Fapi_Sign", error);
93
94 r = Fapi_Delete(context, "/HS/SRK");
95 goto_if_error(r, "Error Fapi_Delete", error);
96
97 SAFE_FREE(signature);
98 SAFE_FREE(publicKey);
99
100 return EXIT_SUCCESS;
101
102 error:
103 SAFE_FREE(signature);
104 SAFE_FREE(publicKey);
105 return EXIT_FAILURE;
106 }
107
108 int
test_invoke_fapi(FAPI_CONTEXT * fapi_context)109 test_invoke_fapi(FAPI_CONTEXT *fapi_context)
110 {
111 return test_fapi_key_create_ckda_sign(fapi_context);
112 }
113