• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_FromTPMPublic and Esys_TR_GetName functions by
20  *  creating an NV Index and then attempting to retrieve an ESYS_TR object for
21  *  it.
22  *  Then we call Esys_TR_GetName to see if the correct public name has been
23  * retrieved.
24  *
25  * Tested ESAPI commands:
26  *  - Esys_NV_DefineSpace() (M)
27  *  - Esys_NV_ReadPublic() (M)
28  *  - Esys_NV_UndefineSpace() (M)
29  *
30  * @param[in,out] ectx The ESYS_CONTEXT.
31  * @retval EXIT_FAILURE
32  * @retval EXIT_SUCCESS
33  */
34 
35 int
test_esys_tr_fromTpmPublic_nv(ESYS_CONTEXT * ectx)36 test_esys_tr_fromTpmPublic_nv(ESYS_CONTEXT * ectx)
37 {
38     TSS2_RC r;
39     ESYS_TR nvHandle = ESYS_TR_NONE;
40 
41     TPM2B_NAME *name1, *name2;
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", error);
64 
65     r = Esys_NV_ReadPublic(ectx, nvHandle,
66                            ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
67                            NULL, &name1);
68     goto_if_error(r, "NV read public", error);
69 
70     r = Esys_TR_Close(ectx, &nvHandle);
71     goto_if_error(r, "TR close on nv object", error_name1);
72 
73     r = Esys_TR_FromTPMPublic(ectx, TPM2_NV_INDEX_FIRST,
74                               ESYS_TR_NONE, ESYS_TR_NONE, ESYS_TR_NONE,
75                               &nvHandle);
76     goto_if_error(r, "TR from TPM public", error_name1);
77 
78     r = Esys_TR_GetName(ectx, nvHandle, &name2);
79     goto_if_error(r, "TR get name", error_name1);
80 
81     r = Esys_NV_UndefineSpace(ectx, ESYS_TR_RH_OWNER, nvHandle,
82                               ESYS_TR_PASSWORD, ESYS_TR_NONE, ESYS_TR_NONE);
83     goto_if_error(r, "NV UndefineSpace", error_name2);
84 
85     if (name1->size != name2->size ||
86         memcmp(&name1->name[0], &name2->name[0], name1->size) != 0)
87     {
88         LOG_ERROR("Names mismatch between NV_GetPublic and TR_GetName");
89         goto error_name2;
90     }
91 
92     free(name1);
93     free(name2);
94 
95     return EXIT_SUCCESS;
96 
97 error_name2:
98     free(name2);
99 error_name1:
100     free(name1);
101 error:
102 
103     if (nvHandle != ESYS_TR_NONE) {
104         if (Esys_NV_UndefineSpace(ectx,
105                                   ESYS_TR_RH_OWNER,
106                                   nvHandle,
107                                   ESYS_TR_PASSWORD,
108                                   ESYS_TR_NONE,
109                                   ESYS_TR_NONE) != TSS2_RC_SUCCESS) {
110              LOG_ERROR("Cleanup nvHandle failed.");
111         }
112     }
113 
114 
115     return EXIT_FAILURE;
116 }
117 
118 int
test_invoke_esapi(ESYS_CONTEXT * esys_context)119 test_invoke_esapi(ESYS_CONTEXT * esys_context) {
120     return test_esys_tr_fromTpmPublic_nv(esys_context);
121 }
122