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