VirtualBox

source: vbox/trunk/src/libs/libtpms-0.10.0/tests/tpm2_createprimary.c@ 109006

Last change on this file since 109006 was 108932, checked in by vboxsync, 13 days ago

libtpms-0.10.0: Applied and adjusted our libtpms changes to 0.9.6. jiraref:VBP-1320

File size: 7.2 KB
Line 
1/* SPDX-License-Identifier: BSD-3-Clause */
2
3#include <stdio.h>
4#include <string.h>
5#include <assert.h>
6#include <stdlib.h>
7
8#include <libtpms/tpm_library.h>
9#include <libtpms/tpm_error.h>
10#include <libtpms/tpm_memory.h>
11
12int main(void)
13{
14 unsigned char *rbuffer = NULL;
15 uint32_t rlength;
16 uint32_t rtotal = 0;
17 TPM_RESULT res;
18 int ret = 1;
19 unsigned char *perm = NULL;
20 uint32_t permlen = 0;
21 unsigned char *vol = NULL;
22 uint32_t vollen = 0;
23 unsigned char startup[] = {
24 0x80, 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00,
25 0x01, 0x44, 0x00, 0x00
26 };
27
28 res = TPMLIB_ChooseTPMVersion(TPMLIB_TPM_VERSION_2);
29 if (res) {
30 fprintf(stderr, "TPMLIB_ChooseTPMVersion() failed: 0x%02x\n", res);
31 goto exit;
32 }
33
34 res = TPMLIB_MainInit();
35 if (res) {
36 fprintf(stderr, "TPMLIB_MainInit() failed: 0x%02x\n", res);
37 goto exit;
38 }
39
40 res = TPMLIB_Process(&rbuffer, &rlength, &rtotal, startup, sizeof(startup));
41 if (res) {
42 fprintf(stderr, "TPMLIB_Process(Startup) failed: 0x%02x\n",
43 res);
44 goto exit;
45 }
46
47 unsigned char tpm2_createprimary[] = {
48 0x80, 0x02, 0x00, 0x00, 0x00, 0x43, 0x00, 0x00,
49 0x01, 0x31, 0x40, 0x00, 0x00, 0x01, 0x00, 0x00,
50 0x00, 0x09, 0x40, 0x00, 0x00, 0x09, 0x00, 0x00,
51 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
52 0x00, 0x00, 0x1a, 0x00, 0x01, 0x00, 0x0b, 0x00,
53 0x03, 0x04, 0x72, 0x00, 0x00, 0x00, 0x06, 0x00,
54 0x80, 0x00, 0x43, 0x00, 0x10, 0x08, 0x00, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00
57 };
58
59 res = TPMLIB_Process(&rbuffer, &rlength, &rtotal,
60 tpm2_createprimary, sizeof(tpm2_createprimary));
61 if (res) {
62 fprintf(stderr, "TPMLIB_Process(TPM2_CreatePrimary) failed: 0x%02x\n",
63 res);
64 goto exit;
65 }
66
67 if (rlength != 506) {
68 fprintf(stderr, "Expected response is %u bytes, but got %u.\n",
69 506, rlength);
70 goto exit;
71 }
72
73 unsigned char tpm2_evictcontrol[] = {
74 0x80, 0x02, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00,
75 0x01, 0x20, 0x40, 0x00, 0x00, 0x01, 0x80, 0x00,
76 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x40, 0x00,
77 0x00, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x81,
78 0x00, 0x00, 0x00
79 };
80
81 const unsigned char tpm2_evictcontrol_exp_resp[] = {
82 0x80, 0x02, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00,
83 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
84 0x01, 0x00, 0x00
85 };
86
87 res = TPMLIB_Process(&rbuffer, &rlength, &rtotal,
88 tpm2_evictcontrol, sizeof(tpm2_evictcontrol));
89 if (res) {
90 fprintf(stderr, "TPMLIB_Process(TPM2_EvictControl) failed: %02x\n",
91 res);
92 goto exit;
93 }
94
95 if (rlength != sizeof(tpm2_evictcontrol_exp_resp)) {
96 fprintf(stderr, "Expected TPM2_EvictControl response is %zu bytes, "
97 "but got %u.\n",
98 sizeof(tpm2_evictcontrol_exp_resp), rlength);
99 goto exit;
100 }
101
102 if (memcmp(rbuffer, tpm2_evictcontrol_exp_resp, rlength)) {
103 fprintf(stderr,
104 "Expected TPM2_EvictControl response is different than "
105 "received one.\n");
106 goto exit;
107 }
108
109 /* Expecting a handle 0x81000000 for the persisted key now */
110 unsigned char tpm2_getcapability[] = {
111 0x80, 0x01, 0x00, 0x00, 0x00, 0x16, 0x00, 0x00,
112 0x01, 0x7a, 0x00, 0x00, 0x00, 0x01, 0x81, 0x00,
113 0x00, 0x00, 0x00, 0x00, 0x00, 0x40
114 };
115 const unsigned char tpm2_getcapability_exp_resp[] = {
116 0x80, 0x01, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00,
117 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
118 0x00, 0x00, 0x01, 0x81, 0x00, 0x00, 0x00
119 };
120
121 res = TPMLIB_Process(&rbuffer, &rlength, &rtotal,
122 tpm2_getcapability, sizeof(tpm2_getcapability));
123 if (res) {
124 fprintf(stderr, "TPMLIB_Process(TPM2_GetCapability) failed: 0x%02x\n",
125 res);
126 goto exit;
127 }
128
129 if (rlength != sizeof(tpm2_getcapability_exp_resp)) {
130 fprintf(stderr, "Expected TPM2_GetCapability response is %zu bytes, "
131 "but got %u.\n", sizeof(tpm2_getcapability_exp_resp), rlength);
132 goto exit;
133 }
134
135 if (memcmp(rbuffer, tpm2_getcapability_exp_resp, rlength)) {
136 fprintf(stderr,
137 "Expected TPM2_GetCapability response is different than "
138 "received one.\n");
139 goto exit;
140 }
141
142 /* save permanent and volatile state */
143 res = TPMLIB_GetState(TPMLIB_STATE_PERMANENT, &perm, &permlen);
144 if (res) {
145 fprintf(stderr, "TPMLIB_GetState(PERMANENT) failed: 0x%02x\n", res);
146 goto exit;
147 }
148
149 res = TPMLIB_GetState(TPMLIB_STATE_VOLATILE, &vol, &vollen);
150 if (res) {
151 fprintf(stderr, "TPMLIB_GetState(VOLATILE) failed: 0x%02x\n", res);
152 goto exit;
153 }
154
155 /* terminate and resume where we left off */
156 TPMLIB_Terminate();
157
158 res = TPMLIB_SetState(TPMLIB_STATE_PERMANENT, perm, permlen);
159 if (res) {
160 fprintf(stderr, "TPMLIB_SetState(PERMANENT) failed: 0x%02x\n", res);
161 goto exit;
162 }
163
164 res = TPMLIB_SetState(TPMLIB_STATE_VOLATILE, vol, vollen);
165 if (res) {
166 fprintf(stderr, "TPMLIB_SetState(VOLATILE) failed: 0x%02x\n", res);
167 goto exit;
168 }
169
170 res = TPMLIB_MainInit();
171 if (res) {
172 fprintf(stderr, "TPMLIB_MainInit() after SetState failed: 0x%02x\n",
173 res);
174 goto exit;
175 }
176
177 /* Again expecting the handle 0x81000000 for the persisted key */
178 res = TPMLIB_Process(&rbuffer, &rlength, &rtotal,
179 tpm2_getcapability, sizeof(tpm2_getcapability));
180 if (res) {
181 fprintf(stderr,
182 "TPMLIB_Process(TPM2_GetCapability) failed: 0x%02x\n",
183 res);
184 goto exit;
185 }
186
187 if (rlength != sizeof(tpm2_getcapability_exp_resp)) {
188 fprintf(stderr,
189 "Expected TPM2_GetCapability response is %zu bytes,"
190 "but got %u.\n",
191 sizeof(tpm2_getcapability_exp_resp), rlength);
192 goto exit;
193 }
194
195 if (memcmp(rbuffer, tpm2_getcapability_exp_resp, rlength)) {
196 fprintf(stderr,
197 "Expected TPM2_GetCapability response is different than "
198 "received one.\n");
199 goto exit;
200 }
201
202 /* Shutdown */
203 unsigned char tpm2_shutdown[] = {
204 0x80, 0x01, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x00,
205 0x01, 0x45, 0x00, 0x00
206 };
207
208 res = TPMLIB_Process(&rbuffer, &rlength, &rtotal,
209 tpm2_shutdown, sizeof(tpm2_shutdown));
210 if (res) {
211 fprintf(stderr,
212 "TPMLIB_Process(Shutdown) after SetState failed: 0x%02x\n",
213 res);
214 goto exit;
215 }
216
217 unsigned char tpm2_shutdown_resp[] = {
218 0x80, 0x01, 0x00, 0x00, 0x00, 0x0a, 0x00, 0x00,
219 0x00, 0x00
220 };
221
222 if (memcmp(tpm2_shutdown_resp, rbuffer, rlength)) {
223 fprintf(stderr,
224 "TPM2_PCRRead(Shutdown) after SetState did not return "
225 "expected result\n");
226 goto exit;
227 }
228
229 ret = 0;
230
231 fprintf(stdout, "OK\n");
232
233exit:
234 free(perm);
235 free(vol);
236 TPMLIB_Terminate();
237 TPM_Free(rbuffer);
238
239 return ret;
240}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette