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 #ifdef HAVE_CONFIG_H
7 #include <config.h>
8 #endif
9
10 #include <stdlib.h>
11
12 #include "tss2_esys.h"
13
14 #include "esys_iutil.h"
15 #define LOGMODULE test
16 #include "util/log.h"
17 #include "util/aux_util.h"
18
19 /** This tests the Esys_TR_ToTPMPublic function by
20 * creating an NV index object and then attempting to retrieve
21 * the TPM2_HANDLE for it and validating that the handle is correct for the
22 * expected object type.
23 *
24 * Tested ESAPI commands:
25 * - Esys_NV_DefineSpace() (M)
26 * - Esys_NV_UndefineSpace() (M)
27 * - Esys_TR_ToTPMPublic() (M)
28 *
29 * @param[in,out] ectx The ESYS_CONTEXT.
30 * @retval EXIT_FAILURE
31 * @retval EXIT_SUCCESS
32 */
33
34 int
test_esys_tr_toTpmPublic_nv(ESYS_CONTEXT * ectx)35 test_esys_tr_toTpmPublic_nv(ESYS_CONTEXT * ectx)
36 {
37 int rc = EXIT_FAILURE;
38
39 TSS2_RC r;
40 ESYS_TR nvHandle = ESYS_TR_NONE;
41
42 TPM2B_AUTH auth = {.size = 20,
43 .buffer={10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
44 20, 21, 22, 23, 24, 25, 26, 27, 28, 29}};
45
46 TPM2B_NV_PUBLIC publicInfo = {
47 .size = 0,
48 .nvPublic = {
49 .nvIndex =TPM2_NV_INDEX_FIRST,
50 .nameAlg = TPM2_ALG_SHA1,
51 .attributes = TPMA_NV_AUTHWRITE | TPMA_NV_AUTHREAD,
52 .authPolicy = {
53 .size = 0,
54 .buffer = {},
55 },
56 .dataSize = 1,
57 }
58 };
59
60 r = Esys_NV_DefineSpace(ectx, ESYS_TR_RH_OWNER,
61 ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE,
62 &auth, &publicInfo, &nvHandle);
63 goto_if_error(r, "NV define space", out);
64
65 /* the handle should be NV */
66 TPM2_HANDLE tpmHandle = ESYS_TR_NONE;
67 r = Esys_TR_GetTpmHandle(ectx, nvHandle, &tpmHandle);
68 goto_if_error(r, "Esys_TR_ToTPMPublic", error);
69
70 if (!(tpmHandle & TPM2_HR_NV_INDEX)) {
71 LOG_ERROR("Retrieved handle should be NV, got: 0x%x", tpmHandle);
72 goto error;
73 }
74
75 rc = EXIT_SUCCESS;
76
77 error:
78 r = Esys_NV_UndefineSpace(ectx, ESYS_TR_RH_OWNER, nvHandle,
79 ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE);
80 if (r != TSS2_RC_SUCCESS) {
81 LOG_ERROR("NV UndefineSpace");
82 rc = EXIT_FAILURE;
83 }
84 out:
85 return rc;
86 }
87
88 int
test_invoke_esapi(ESYS_CONTEXT * esys_context)89 test_invoke_esapi(ESYS_CONTEXT * esys_context) {
90 return test_esys_tr_toTpmPublic_nv(esys_context);
91 }
92