xref: /aosp_15_r20/external/tpm2-tss/test/integration/fapi-key-create-policy-nv-sign.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 <stdio.h>
13 #include <errno.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 
17 #include "tss2_fapi.h"
18 
19 #define LOGMODULE test
20 #include "util/log.h"
21 #include "util/aux_util.h"
22 
23 #define SIGN_TEMPLATE  "sign,noDa"
24 #define PASSWORD NULL
25 
26 #define NV_SIZE 4
27 
28 /** Test the FAPI functions for NV writing and key usage.
29  *
30  * Tested FAPI commands:
31  *  - Fapi_Provision()
32  *  - Fapi_CreateKey()
33  *  - Fapi_NvWrite()
34  *  - Fapi_Import()
35  *  - Fapi_Sign()
36  *  - Fapi_Delete()
37  *
38  * Tested Policies:
39  *  - PolicyNv
40  *
41  * @param[in,out] context The FAPI_CONTEXT.
42  * @retval EXIT_FAILURE
43  * @retval EXIT_SUCCESS
44  */
45 int
test_fapi_key_create_policy_nv_sign(FAPI_CONTEXT * context)46 test_fapi_key_create_policy_nv_sign(FAPI_CONTEXT *context)
47 {
48     TSS2_RC r;
49     char *policy_name = "/policy/pol_nv";
50     char *policy_file = TOP_SOURCEDIR "/test/data/fapi/policy/pol_nv.json";;
51     FILE *stream = NULL;
52     char *json_policy = NULL;
53     uint8_t *signature = NULL;
54     char    *publicKey = NULL;
55     long policy_size;
56     char *nvPathOrdinary = "/nv/Owner/myNV";
57     uint8_t data_nv[NV_SIZE] = { 1, 2, 3, 4 };
58 
59     r = Fapi_Provision(context, NULL, NULL, NULL);
60     goto_if_error(r, "Error Fapi_Provision", error);
61 
62     r = Fapi_CreateNv(context, nvPathOrdinary, "noda", 4,  "", "");
63     goto_if_error(r, "Error Fapi_CreateNv", error);
64 
65     r = Fapi_NvWrite(context, nvPathOrdinary, &data_nv[0], NV_SIZE);
66     goto_if_error(r, "Error Fapi_NvWrite", error);
67 
68     stream = fopen(policy_file, "r");
69     if (!stream) {
70         LOG_ERROR("File %s does not exist", policy_file);
71         goto error;
72     }
73     fseek(stream, 0L, SEEK_END);
74     policy_size = ftell(stream);
75     fclose(stream);
76     json_policy = malloc(policy_size + 1);
77     goto_if_null(json_policy,
78             "Could not allocate memory for the JSON policy",
79             TSS2_FAPI_RC_MEMORY, error);
80     stream = fopen(policy_file, "r");
81     ssize_t ret = read(fileno(stream), json_policy, policy_size);
82     if (ret != policy_size) {
83         LOG_ERROR("IO error %s.", policy_file);
84         goto error;
85     }
86     json_policy[policy_size] = '\0';
87 
88     r = Fapi_Import(context, policy_name, json_policy);
89     goto_if_error(r, "Error Fapi_Import", error);
90 
91     r = Fapi_CreateKey(context, "HS/SRK/mySignKey", SIGN_TEMPLATE,
92                        policy_name, PASSWORD);
93     goto_if_error(r, "Error Fapi_CreateKey", error);
94     size_t signatureSize = 0;
95 
96     TPM2B_DIGEST digest = {
97         .size = 20,
98         .buffer = {
99             0x67, 0x68, 0x03, 0x3e, 0x21, 0x64, 0x68, 0x24, 0x7b, 0xd0,
100             0x31, 0xa0, 0xa2, 0xd9, 0x87, 0x6d, 0x79, 0x81, 0x8f, 0x8f
101         }
102     };
103 
104     r = Fapi_Sign(context, "HS/SRK/mySignKey", NULL,
105                   &digest.buffer[0], digest.size, &signature, &signatureSize,
106                   &publicKey, NULL);
107     goto_if_error(r, "Error Fapi_Sign", error);
108 
109     r = Fapi_Delete(context, nvPathOrdinary);
110     goto_if_error(r, "Error Fapi_NV_Undefine", error);
111 
112     r = Fapi_Delete(context, "/HS/SRK");
113     goto_if_error(r, "Error Fapi_Delete", error);
114 
115     SAFE_FREE(signature);
116     SAFE_FREE(publicKey);
117     SAFE_FREE(json_policy);
118     return EXIT_SUCCESS;
119 
120 error:
121     SAFE_FREE(signature);
122     SAFE_FREE(publicKey);
123     SAFE_FREE(json_policy);
124     return EXIT_FAILURE;
125 }
126 
127 int
test_invoke_fapi(FAPI_CONTEXT * fapi_context)128 test_invoke_fapi(FAPI_CONTEXT *fapi_context)
129 {
130     return test_fapi_key_create_policy_nv_sign(fapi_context);
131 }
132