xref: /aosp_15_r20/external/tpm2-tss/test/integration/fapi-key-change-auth.int.c (revision 758e9fba6fc9adbf15340f70c73baee7b168b1c9)
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 #define PASSWORD "abc"
21 
22 static TSS2_RC
auth_callback(FAPI_CONTEXT * context,char const * description,char ** auth,void * userData)23 auth_callback(
24     FAPI_CONTEXT *context,
25     char const *description,
26     char **auth,
27     void *userData)
28 {
29     (void)description;
30     (void)userData;
31     *auth = strdup(PASSWORD);
32     return_if_null(*auth, "Out of memory.", TSS2_FAPI_RC_MEMORY);
33     return TSS2_RC_SUCCESS;
34 }
35 
36 
37 /** Test the FAPI function for changing key authorizations.
38  *
39  * The setting of the authorization callback and usage of the
40  * key with Fapi_Sign afterwards is also tested.
41  *
42  * Tested FAPI commands:
43  *  - Fapi_Provision()
44  *  - Fapi_CreateKey()
45  *  - Fapi_ChangeAuth()
46  *  - Fapi_SetAuthCB()
47  *  - Fapi_Sign()
48  *  - Fapi_Delete()
49  *
50  * @param[in,out] context The FAPI_CONTEXT.
51  * @retval EXIT_FAILURE
52  * @retval EXIT_SUCCESS
53  */
54 int
test_fapi_key_change_auth(FAPI_CONTEXT * context)55 test_fapi_key_change_auth(FAPI_CONTEXT *context)
56 {
57 
58     TSS2_RC r;
59     uint8_t *signature = NULL;
60     char    *publicKey = NULL;
61 
62     r = Fapi_Provision(context, NULL, NULL, NULL);
63 
64     goto_if_error(r, "Error Fapi_Provision", error);
65 
66     r = Fapi_CreateKey(context, "HS/SRK/mySignKey", "sign,noDa", "", NULL);
67     goto_if_error(r, "Error Fapi_CreateKey", error);
68     size_t signatureSize = 0;
69 
70     TPM2B_DIGEST digest = {
71         .size = 20,
72         .buffer = {
73             0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
74             0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f
75         }
76     };
77 
78     r = Fapi_ChangeAuth(context, "HS/SRK/mySignKey", PASSWORD);
79     goto_if_error(r, "Error Fapi_Provision", error);
80 
81     r = Fapi_SetAuthCB(context, auth_callback, "");
82     goto_if_error(r, "Error SetPolicyAuthCallback", error);
83 
84     r = Fapi_Sign(context, "HS/SRK/mySignKey", NULL,
85                   &digest.buffer[0], digest.size, &signature, &signatureSize,
86                   &publicKey, NULL);
87     goto_if_error(r, "Error Fapi_Provision", error);
88 
89     Fapi_Free(publicKey);
90 
91     r = Fapi_Delete(context, "/HS/SRK");
92     goto_if_error(r, "Error Fapi_Delete", error);
93 
94     SAFE_FREE(signature);
95     return EXIT_SUCCESS;
96 
97 error:
98     Fapi_Delete(context, "/HS/SRK");
99     SAFE_FREE(signature);
100     return EXIT_FAILURE;
101 }
102 
103 int
test_invoke_fapi(FAPI_CONTEXT * fapi_context)104 test_invoke_fapi(FAPI_CONTEXT *fapi_context)
105 {
106     return test_fapi_key_change_auth(fapi_context);
107 }
108