VirtualBox

source: vbox/trunk/src/libs/libtpms-0.9.6/tests/tpm2_createprimary.c

Last change on this file was 91612, checked in by vboxsync, 3 years ago

src/libs: Export libtpms-0.9.0, bugref:10078

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

© 2023 Oracle
ContactPrivacy policyTerms of Use