1 
2 /*
3  * Licensed Materials - Property of IBM
4  *
5  * trousers - An open source TCG Software Stack
6  *
7  * (C) Copyright International Business Machines Corp. 2004-2007
8  *
9  */
10 
11 
12 #include <stdlib.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <unistd.h>
16 #include <sys/types.h>
17 #include <sys/mman.h>
18 #include <langinfo.h>
19 #include <iconv.h>
20 #include <wchar.h>
21 #include <errno.h>
22 
23 #include "trousers/tss.h"
24 #include "trousers_types.h"
25 #include "trousers/trousers.h"
26 #include "trousers_types.h"
27 #include "spi_utils.h"
28 #include "capabilities.h"
29 #include "tsplog.h"
30 #include "obj.h"
31 #include "tcs_tsp.h"
32 
33 void
Trspi_UnloadBlob_NONCE(UINT64 * offset,BYTE * blob,TPM_NONCE * n)34 Trspi_UnloadBlob_NONCE(UINT64 *offset, BYTE *blob, TPM_NONCE *n)
35 {
36           if (!n) {
37                     (*offset) += TPM_SHA1_160_HASH_LEN;
38                     return;
39           }
40 
41           Trspi_UnloadBlob(offset, TPM_SHA1_160_HASH_LEN, blob, n->nonce);
42 }
43 
44 void
Trspi_LoadBlob_NONCE(UINT64 * offset,BYTE * blob,TPM_NONCE * n)45 Trspi_LoadBlob_NONCE(UINT64 *offset, BYTE *blob, TPM_NONCE *n)
46 {
47           Trspi_LoadBlob(offset, TPM_SHA1_160_HASH_LEN, blob, n->nonce);
48 }
49 
50 void
Trspi_LoadBlob_DIGEST(UINT64 * offset,BYTE * blob,TPM_DIGEST * digest)51 Trspi_LoadBlob_DIGEST(UINT64 *offset, BYTE *blob, TPM_DIGEST *digest)
52 {
53           Trspi_LoadBlob(offset, TPM_SHA1_160_HASH_LEN, blob, digest->digest);
54 }
55 
56 void
Trspi_UnloadBlob_DIGEST(UINT64 * offset,BYTE * blob,TPM_DIGEST * digest)57 Trspi_UnloadBlob_DIGEST(UINT64 *offset, BYTE *blob, TPM_DIGEST *digest)
58 {
59           if (!digest) {
60                     (*offset) += TPM_SHA1_160_HASH_LEN;
61                     return;
62           }
63 
64           Trspi_UnloadBlob(offset, TPM_SHA1_160_HASH_LEN, blob, digest->digest);
65 }
66 
67 void
Trspi_LoadBlob_PUBKEY(UINT64 * offset,BYTE * blob,TCPA_PUBKEY * pubKey)68 Trspi_LoadBlob_PUBKEY(UINT64 *offset, BYTE *blob, TCPA_PUBKEY *pubKey)
69 {
70           Trspi_LoadBlob_KEY_PARMS(offset, blob, &pubKey->algorithmParms);
71           Trspi_LoadBlob_STORE_PUBKEY(offset, blob, &pubKey->pubKey);
72 }
73 
74 TSS_RESULT
Trspi_UnloadBlob_PUBKEY(UINT64 * offset,BYTE * blob,TCPA_PUBKEY * pubKey)75 Trspi_UnloadBlob_PUBKEY(UINT64 *offset, BYTE *blob, TCPA_PUBKEY *pubKey)
76 {
77           TSS_RESULT result;
78 
79           if (!pubKey) {
80                     (void)Trspi_UnloadBlob_KEY_PARMS(offset, blob, NULL);
81                     (void)Trspi_UnloadBlob_STORE_PUBKEY(offset, blob, NULL);
82 
83                     return TSS_SUCCESS;
84           }
85 
86           if ((result = Trspi_UnloadBlob_KEY_PARMS(offset, blob, &pubKey->algorithmParms)))
87                     return result;
88           if ((result = Trspi_UnloadBlob_STORE_PUBKEY(offset, blob, &pubKey->pubKey))) {
89                     free(pubKey->pubKey.key);
90                     free(pubKey->algorithmParms.parms);
91                     pubKey->pubKey.key = NULL;
92                     pubKey->pubKey.keyLength = 0;
93                     pubKey->algorithmParms.parms = NULL;
94                     pubKey->algorithmParms.parmSize = 0;
95                     return result;
96           }
97 
98           return TSS_SUCCESS;
99 }
100 
101 void
Trspi_LoadBlob(UINT64 * offset,size_t size,BYTE * to,BYTE * from)102 Trspi_LoadBlob(UINT64 *offset, size_t size, BYTE *to, BYTE *from)
103 {
104           if (size == 0)
105                     return;
106 
107           if (to)
108                     memcpy(&to[(*offset)], from, size);
109           (*offset) += size;
110 }
111 
112 void
Trspi_UnloadBlob(UINT64 * offset,size_t size,BYTE * from,BYTE * to)113 Trspi_UnloadBlob(UINT64 *offset, size_t size, BYTE *from, BYTE *to)
114 {
115           if (size <= 0)
116                     return;
117 
118           if (to)
119                     memcpy(to, &from[*offset], size);
120           (*offset) += size;
121 }
122 
123 void
Trspi_LoadBlob_BYTE(UINT64 * offset,BYTE data,BYTE * blob)124 Trspi_LoadBlob_BYTE(UINT64 *offset, BYTE data, BYTE *blob)
125 {
126           if (blob)
127                     blob[*offset] = data;
128           (*offset)++;
129 }
130 
131 void
Trspi_UnloadBlob_BYTE(UINT64 * offset,BYTE * dataOut,BYTE * blob)132 Trspi_UnloadBlob_BYTE(UINT64 *offset, BYTE *dataOut, BYTE *blob)
133 {
134           if (dataOut)
135                     *dataOut = blob[*offset];
136           (*offset)++;
137 }
138 
139 void
Trspi_LoadBlob_BOOL(UINT64 * offset,TSS_BOOL data,BYTE * blob)140 Trspi_LoadBlob_BOOL(UINT64 *offset, TSS_BOOL data, BYTE *blob)
141 {
142           if (blob)
143                     blob[*offset] = (BYTE) data;
144           (*offset)++;
145 }
146 
147 void
Trspi_UnloadBlob_BOOL(UINT64 * offset,TSS_BOOL * dataOut,BYTE * blob)148 Trspi_UnloadBlob_BOOL(UINT64 *offset, TSS_BOOL *dataOut, BYTE *blob)
149 {
150           if (dataOut)
151                     *dataOut = blob[*offset];
152           (*offset)++;
153 }
154 
155 void
Trspi_LoadBlob_UINT64(UINT64 * offset,UINT64 in,BYTE * blob)156 Trspi_LoadBlob_UINT64(UINT64 *offset, UINT64 in, BYTE *blob)
157 {
158           if (blob)
159                     UINT64ToArray(in, &blob[*offset]);
160           (*offset) += sizeof(UINT64);
161 }
162 
163 void
Trspi_LoadBlob_UINT32(UINT64 * offset,UINT32 in,BYTE * blob)164 Trspi_LoadBlob_UINT32(UINT64 *offset, UINT32 in, BYTE *blob)
165 {
166           if (blob)
167                     UINT32ToArray(in, &blob[*offset]);
168           (*offset) += sizeof(UINT32);
169 }
170 
171 void
Trspi_LoadBlob_UINT16(UINT64 * offset,UINT16 in,BYTE * blob)172 Trspi_LoadBlob_UINT16(UINT64 *offset, UINT16 in, BYTE *blob)
173 {
174           if (blob)
175                     UINT16ToArray(in, &blob[*offset]);
176           (*offset) += sizeof(UINT16);
177 }
178 
179 void
Trspi_UnloadBlob_UINT64(UINT64 * offset,UINT64 * out,BYTE * blob)180 Trspi_UnloadBlob_UINT64(UINT64 *offset, UINT64 *out, BYTE *blob)
181 {
182           if (out)
183                     *out = Decode_UINT64(&blob[*offset]);
184           (*offset) += sizeof(UINT64);
185 }
186 
187 void
Trspi_UnloadBlob_UINT32(UINT64 * offset,UINT32 * out,BYTE * blob)188 Trspi_UnloadBlob_UINT32(UINT64 *offset, UINT32 *out, BYTE *blob)
189 {
190           if (out)
191                     *out = Decode_UINT32(&blob[*offset]);
192           (*offset) += sizeof(UINT32);
193 }
194 
195 void
Trspi_UnloadBlob_UINT16(UINT64 * offset,UINT16 * out,BYTE * blob)196 Trspi_UnloadBlob_UINT16(UINT64 *offset, UINT16 *out, BYTE *blob)
197 {
198           if (out)
199                     *out = Decode_UINT16(&blob[*offset]);
200           (*offset) += sizeof(UINT16);
201 }
202 
203 void
Trspi_LoadBlob_RSA_KEY_PARMS(UINT64 * offset,BYTE * blob,TCPA_RSA_KEY_PARMS * parms)204 Trspi_LoadBlob_RSA_KEY_PARMS(UINT64 *offset, BYTE *blob, TCPA_RSA_KEY_PARMS *parms)
205 {
206           Trspi_LoadBlob_UINT32(offset, parms->keyLength, blob);
207           Trspi_LoadBlob_UINT32(offset, parms->numPrimes, blob);
208           Trspi_LoadBlob_UINT32(offset, parms->exponentSize, blob);
209 
210           if (parms->exponentSize > 0)
211                     Trspi_LoadBlob(offset, parms->exponentSize, blob, parms->exponent);
212 }
213 
214 void
Trspi_UnloadBlob_TSS_VERSION(UINT64 * offset,BYTE * blob,TSS_VERSION * out)215 Trspi_UnloadBlob_TSS_VERSION(UINT64 *offset, BYTE *blob, TSS_VERSION *out)
216 {
217           if (!out) {
218                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
219                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
220                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
221                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
222 
223                     return;
224           }
225 
226           Trspi_UnloadBlob_BYTE(offset, &out->bMajor, blob);
227           Trspi_UnloadBlob_BYTE(offset, &out->bMinor, blob);
228           Trspi_UnloadBlob_BYTE(offset, &out->bRevMajor, blob);
229           Trspi_UnloadBlob_BYTE(offset, &out->bRevMinor, blob);
230 }
231 
232 void
Trspi_LoadBlob_TSS_VERSION(UINT64 * offset,BYTE * blob,TSS_VERSION version)233 Trspi_LoadBlob_TSS_VERSION(UINT64 *offset, BYTE *blob, TSS_VERSION version)
234 {
235           Trspi_LoadBlob_BYTE(offset, version.bMajor, blob);
236           Trspi_LoadBlob_BYTE(offset, version.bMinor, blob);
237           Trspi_LoadBlob_BYTE(offset, version.bRevMajor, blob);
238           Trspi_LoadBlob_BYTE(offset, version.bRevMinor, blob);
239 }
240 
241 void
Trspi_UnloadBlob_TCPA_VERSION(UINT64 * offset,BYTE * blob,TCPA_VERSION * out)242 Trspi_UnloadBlob_TCPA_VERSION(UINT64 *offset, BYTE *blob, TCPA_VERSION *out)
243 {
244           if (!out) {
245                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
246                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
247                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
248                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
249 
250                     return;
251           }
252 
253           Trspi_UnloadBlob_BYTE(offset, &out->major, blob);
254           Trspi_UnloadBlob_BYTE(offset, &out->minor, blob);
255           Trspi_UnloadBlob_BYTE(offset, &out->revMajor, blob);
256           Trspi_UnloadBlob_BYTE(offset, &out->revMinor, blob);
257 }
258 
259 void
Trspi_LoadBlob_TCPA_VERSION(UINT64 * offset,BYTE * blob,TCPA_VERSION version)260 Trspi_LoadBlob_TCPA_VERSION(UINT64 *offset, BYTE *blob, TCPA_VERSION version)
261 {
262           Trspi_LoadBlob_BYTE(offset, version.major, blob);
263           Trspi_LoadBlob_BYTE(offset, version.minor, blob);
264           Trspi_LoadBlob_BYTE(offset, version.revMajor, blob);
265           Trspi_LoadBlob_BYTE(offset, version.revMinor, blob);
266 }
267 
268 TSS_RESULT
Trspi_UnloadBlob_PCR_INFO(UINT64 * offset,BYTE * blob,TCPA_PCR_INFO * pcr)269 Trspi_UnloadBlob_PCR_INFO(UINT64 *offset, BYTE *blob, TCPA_PCR_INFO *pcr)
270 {
271           TSS_RESULT result;
272 
273           if (!pcr) {
274                     (void)Trspi_UnloadBlob_PCR_SELECTION(offset, blob, NULL);
275                     Trspi_UnloadBlob_DIGEST(offset, blob, NULL);
276                     Trspi_UnloadBlob_DIGEST(offset, blob, NULL);
277 
278                     return TSS_SUCCESS;
279           }
280 
281           if ((result = Trspi_UnloadBlob_PCR_SELECTION(offset, blob, &pcr->pcrSelection)))
282                     return result;
283           Trspi_UnloadBlob_DIGEST(offset, blob, &pcr->digestAtRelease);
284           Trspi_UnloadBlob_DIGEST(offset, blob, &pcr->digestAtCreation);
285 
286           return TSS_SUCCESS;
287 }
288 
289 void
Trspi_LoadBlob_PCR_INFO(UINT64 * offset,BYTE * blob,TCPA_PCR_INFO * pcr)290 Trspi_LoadBlob_PCR_INFO(UINT64 *offset, BYTE *blob, TCPA_PCR_INFO *pcr)
291 {
292           Trspi_LoadBlob_PCR_SELECTION(offset, blob, &pcr->pcrSelection);
293           Trspi_LoadBlob_DIGEST(offset, blob, &pcr->digestAtRelease);
294           Trspi_LoadBlob_DIGEST(offset, blob, &pcr->digestAtCreation);
295 }
296 
297 TSS_RESULT
Trspi_UnloadBlob_PCR_INFO_LONG(UINT64 * offset,BYTE * blob,TPM_PCR_INFO_LONG * pcr)298 Trspi_UnloadBlob_PCR_INFO_LONG(UINT64 *offset, BYTE *blob, TPM_PCR_INFO_LONG *pcr)
299 {
300           TSS_RESULT result;
301 
302           if (!pcr) {
303                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
304                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
305                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
306                     Trspi_UnloadBlob_PCR_SELECTION(offset, blob, NULL);
307                     Trspi_UnloadBlob_PCR_SELECTION(offset, blob, NULL);
308                     Trspi_UnloadBlob_DIGEST(offset, blob, NULL);
309                     Trspi_UnloadBlob_DIGEST(offset, blob, NULL);
310 
311                     return TSS_SUCCESS;
312           }
313 
314           Trspi_UnloadBlob_UINT16(offset, &pcr->tag, blob);
315           Trspi_UnloadBlob_BYTE(offset, &pcr->localityAtCreation, blob);
316           Trspi_UnloadBlob_BYTE(offset, &pcr->localityAtRelease, blob);
317           if ((result = Trspi_UnloadBlob_PCR_SELECTION(offset, blob, &pcr->creationPCRSelection)))
318                     return result;
319           if ((result = Trspi_UnloadBlob_PCR_SELECTION(offset, blob, &pcr->releasePCRSelection)))
320                     return result;
321           Trspi_UnloadBlob_DIGEST(offset, blob, &pcr->digestAtCreation);
322           Trspi_UnloadBlob_DIGEST(offset, blob, &pcr->digestAtRelease);
323 
324           return TSS_SUCCESS;
325 }
326 
327 void
Trspi_LoadBlob_PCR_INFO_LONG(UINT64 * offset,BYTE * blob,TPM_PCR_INFO_LONG * pcr)328 Trspi_LoadBlob_PCR_INFO_LONG(UINT64 *offset, BYTE *blob, TPM_PCR_INFO_LONG *pcr)
329 {
330           Trspi_LoadBlob_UINT16(offset, pcr->tag, blob);
331           Trspi_LoadBlob_BYTE(offset, pcr->localityAtCreation, blob);
332           Trspi_LoadBlob_BYTE(offset, pcr->localityAtRelease, blob);
333           Trspi_LoadBlob_PCR_SELECTION(offset, blob, &pcr->creationPCRSelection);
334           Trspi_LoadBlob_PCR_SELECTION(offset, blob, &pcr->releasePCRSelection);
335           Trspi_LoadBlob_DIGEST(offset, blob, &pcr->digestAtCreation);
336           Trspi_LoadBlob_DIGEST(offset, blob, &pcr->digestAtRelease);
337 }
338 
339 TSS_RESULT
Trspi_UnloadBlob_PCR_INFO_SHORT(UINT64 * offset,BYTE * blob,TPM_PCR_INFO_SHORT * pcr)340 Trspi_UnloadBlob_PCR_INFO_SHORT(UINT64 *offset, BYTE *blob, TPM_PCR_INFO_SHORT *pcr)
341 {
342           TSS_RESULT result;
343 
344           if (!pcr) {
345                     Trspi_UnloadBlob_PCR_SELECTION(offset, blob, NULL);
346                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
347                     Trspi_UnloadBlob_DIGEST(offset, blob, NULL);
348 
349                     return TSS_SUCCESS;
350           }
351 
352           if ((result = Trspi_UnloadBlob_PCR_SELECTION(offset, blob, &pcr->pcrSelection)))
353                     return result;
354           Trspi_UnloadBlob_BYTE(offset, &pcr->localityAtRelease, blob);
355           Trspi_UnloadBlob_DIGEST(offset, blob, &pcr->digestAtRelease);
356 
357           return TSS_SUCCESS;
358 }
359 
360 void
Trspi_LoadBlob_PCR_INFO_SHORT(UINT64 * offset,BYTE * blob,TPM_PCR_INFO_SHORT * pcr)361 Trspi_LoadBlob_PCR_INFO_SHORT(UINT64 *offset, BYTE *blob, TPM_PCR_INFO_SHORT *pcr)
362 {
363           Trspi_LoadBlob_PCR_SELECTION(offset, blob, &pcr->pcrSelection);
364           Trspi_LoadBlob_BYTE(offset, pcr->localityAtRelease, blob);
365           Trspi_LoadBlob_DIGEST(offset, blob, &pcr->digestAtRelease);
366 }
367 
368 TSS_RESULT
Trspi_UnloadBlob_PCR_SELECTION(UINT64 * offset,BYTE * blob,TCPA_PCR_SELECTION * pcr)369 Trspi_UnloadBlob_PCR_SELECTION(UINT64 *offset, BYTE *blob, TCPA_PCR_SELECTION *pcr)
370 {
371           if (!pcr) {
372                     UINT16 sizeOfSelect;
373 
374                     Trspi_UnloadBlob_UINT16(offset, &sizeOfSelect, blob);
375                     Trspi_UnloadBlob(offset, sizeOfSelect, blob, NULL);
376 
377                     return TSS_SUCCESS;
378           }
379 
380           Trspi_UnloadBlob_UINT16(offset, &pcr->sizeOfSelect, blob);
381 
382           if (pcr->sizeOfSelect > 0) {
383                     pcr->pcrSelect = calloc(1, pcr->sizeOfSelect);
384                     if (pcr->pcrSelect == NULL) {
385                               LogError("malloc of %u bytes failed.", pcr->sizeOfSelect);
386                               return TSPERR(TSS_E_OUTOFMEMORY);
387                     }
388 
389                     Trspi_UnloadBlob(offset, pcr->sizeOfSelect, blob, pcr->pcrSelect);
390           } else {
391                     pcr->pcrSelect = NULL;
392           }
393 
394           return TSS_SUCCESS;
395 }
396 
397 void
Trspi_LoadBlob_PCR_SELECTION(UINT64 * offset,BYTE * blob,TCPA_PCR_SELECTION * pcr)398 Trspi_LoadBlob_PCR_SELECTION(UINT64 *offset, BYTE *blob, TCPA_PCR_SELECTION *pcr)
399 {
400           UINT16 i;
401 
402           Trspi_LoadBlob_UINT16(offset, pcr->sizeOfSelect, blob);
403           for (i = 0; i < pcr->sizeOfSelect; i++)
404                     Trspi_LoadBlob_BYTE(offset, pcr->pcrSelect[i], blob);
405 }
406 
407 void
Trspi_LoadBlob_KEY12(UINT64 * offset,BYTE * blob,TPM_KEY12 * key)408 Trspi_LoadBlob_KEY12(UINT64 *offset, BYTE *blob, TPM_KEY12 *key)
409 {
410           Trspi_LoadBlob_UINT16(offset, key->tag, blob);
411           Trspi_LoadBlob_UINT16(offset, key->fill, blob);
412           Trspi_LoadBlob_UINT16(offset, key->keyUsage, blob);
413           Trspi_LoadBlob_KEY_FLAGS(offset, blob, &key->keyFlags);
414           Trspi_LoadBlob_BYTE(offset, key->authDataUsage, blob);
415           Trspi_LoadBlob_KEY_PARMS(offset, blob, &key->algorithmParms);
416           Trspi_LoadBlob_UINT32(offset, key->PCRInfoSize, blob);
417           Trspi_LoadBlob(offset, key->PCRInfoSize, blob, key->PCRInfo);
418           Trspi_LoadBlob_STORE_PUBKEY(offset, blob, &key->pubKey);
419           Trspi_LoadBlob_UINT32(offset, key->encSize, blob);
420           Trspi_LoadBlob(offset, key->encSize, blob, key->encData);
421 }
422 
423 void
Trspi_LoadBlob_KEY(UINT64 * offset,BYTE * blob,TCPA_KEY * key)424 Trspi_LoadBlob_KEY(UINT64 *offset, BYTE *blob, TCPA_KEY *key)
425 {
426           Trspi_LoadBlob_TCPA_VERSION(offset, blob, key->ver);
427           Trspi_LoadBlob_UINT16(offset, key->keyUsage, blob);
428           Trspi_LoadBlob_KEY_FLAGS(offset, blob, &key->keyFlags);
429           Trspi_LoadBlob_BYTE(offset, key->authDataUsage, blob);
430           Trspi_LoadBlob_KEY_PARMS(offset, blob, &key->algorithmParms);
431           Trspi_LoadBlob_UINT32(offset, key->PCRInfoSize, blob);
432           Trspi_LoadBlob(offset, key->PCRInfoSize, blob, key->PCRInfo);
433           Trspi_LoadBlob_STORE_PUBKEY(offset, blob, &key->pubKey);
434           Trspi_LoadBlob_UINT32(offset, key->encSize, blob);
435           Trspi_LoadBlob(offset, key->encSize, blob, key->encData);
436 }
437 
438 void
Trspi_LoadBlob_KEY_FLAGS(UINT64 * offset,BYTE * blob,TCPA_KEY_FLAGS * flags)439 Trspi_LoadBlob_KEY_FLAGS(UINT64 *offset, BYTE *blob, TCPA_KEY_FLAGS *flags)
440 {
441           Trspi_LoadBlob_UINT32(offset, *flags, blob);
442 }
443 
444 void
Trspi_UnloadBlob_KEY_FLAGS(UINT64 * offset,BYTE * blob,TCPA_KEY_FLAGS * flags)445 Trspi_UnloadBlob_KEY_FLAGS(UINT64 *offset, BYTE *blob, TCPA_KEY_FLAGS *flags)
446 {
447           Trspi_UnloadBlob_UINT32(offset, flags, blob);
448 }
449 
450 void
Trspi_LoadBlob_KEY_PARMS(UINT64 * offset,BYTE * blob,TCPA_KEY_PARMS * keyInfo)451 Trspi_LoadBlob_KEY_PARMS(UINT64 *offset, BYTE *blob, TCPA_KEY_PARMS *keyInfo)
452 {
453           Trspi_LoadBlob_UINT32(offset, keyInfo->algorithmID, blob);
454           Trspi_LoadBlob_UINT16(offset, keyInfo->encScheme, blob);
455           Trspi_LoadBlob_UINT16(offset, keyInfo->sigScheme, blob);
456           Trspi_LoadBlob_UINT32(offset, keyInfo->parmSize, blob);
457 
458           if (keyInfo->parmSize > 0)
459                     Trspi_LoadBlob(offset, keyInfo->parmSize, blob, keyInfo->parms);
460 }
461 
462 void
Trspi_LoadBlob_STORE_PUBKEY(UINT64 * offset,BYTE * blob,TCPA_STORE_PUBKEY * store)463 Trspi_LoadBlob_STORE_PUBKEY(UINT64 *offset, BYTE *blob, TCPA_STORE_PUBKEY *store)
464 {
465           Trspi_LoadBlob_UINT32(offset, store->keyLength, blob);
466           Trspi_LoadBlob(offset, store->keyLength, blob, store->key);
467 }
468 
469 void
Trspi_LoadBlob_UUID(UINT64 * offset,BYTE * blob,TSS_UUID uuid)470 Trspi_LoadBlob_UUID(UINT64 *offset, BYTE *blob, TSS_UUID uuid)
471 {
472           Trspi_LoadBlob_UINT32(offset, uuid.ulTimeLow, blob);
473           Trspi_LoadBlob_UINT16(offset, uuid.usTimeMid, blob);
474           Trspi_LoadBlob_UINT16(offset, uuid.usTimeHigh, blob);
475           Trspi_LoadBlob_BYTE(offset, uuid.bClockSeqHigh, blob);
476           Trspi_LoadBlob_BYTE(offset, uuid.bClockSeqLow, blob);
477           Trspi_LoadBlob(offset, 6, blob, uuid.rgbNode);
478 }
479 
480 void
Trspi_UnloadBlob_UUID(UINT64 * offset,BYTE * blob,TSS_UUID * uuid)481 Trspi_UnloadBlob_UUID(UINT64 *offset, BYTE *blob, TSS_UUID *uuid)
482 {
483           if (!uuid) {
484                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
485                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
486                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
487                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
488                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
489                     Trspi_UnloadBlob(offset, 6, blob, NULL);
490 
491                     return;
492           }
493 
494           memset(uuid, 0, sizeof(TSS_UUID));
495           Trspi_UnloadBlob_UINT32(offset, &uuid->ulTimeLow, blob);
496           Trspi_UnloadBlob_UINT16(offset, &uuid->usTimeMid, blob);
497           Trspi_UnloadBlob_UINT16(offset, &uuid->usTimeHigh, blob);
498           Trspi_UnloadBlob_BYTE(offset, &uuid->bClockSeqHigh, blob);
499           Trspi_UnloadBlob_BYTE(offset, &uuid->bClockSeqLow, blob);
500           Trspi_UnloadBlob(offset, 6, blob, uuid->rgbNode);
501 }
502 
503 TSS_RESULT
Trspi_UnloadBlob_KEY_PARMS(UINT64 * offset,BYTE * blob,TCPA_KEY_PARMS * keyParms)504 Trspi_UnloadBlob_KEY_PARMS(UINT64 *offset, BYTE *blob, TCPA_KEY_PARMS *keyParms)
505 {
506           if (!keyParms) {
507                     UINT32 parmSize;
508 
509                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
510                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
511                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
512                     Trspi_UnloadBlob_UINT32(offset, &parmSize, blob);
513 
514                     (*offset) += parmSize;
515 
516                     return TSS_SUCCESS;
517           }
518 
519           Trspi_UnloadBlob_UINT32(offset, &keyParms->algorithmID, blob);
520           Trspi_UnloadBlob_UINT16(offset, &keyParms->encScheme, blob);
521           Trspi_UnloadBlob_UINT16(offset, &keyParms->sigScheme, blob);
522           Trspi_UnloadBlob_UINT32(offset, &keyParms->parmSize, blob);
523 
524           if (keyParms->parmSize > 0) {
525                     keyParms->parms = malloc(keyParms->parmSize);
526                     if (keyParms->parms == NULL) {
527                               LogError("malloc of %u bytes failed.", keyParms->parmSize);
528                               return TSPERR(TSS_E_OUTOFMEMORY);
529                     }
530                     Trspi_UnloadBlob(offset, keyParms->parmSize, blob, keyParms->parms);
531           } else {
532                     keyParms->parms = NULL;
533           }
534 
535           return TSS_SUCCESS;
536 }
537 
538 TSS_RESULT
Trspi_UnloadBlob_KEY12(UINT64 * offset,BYTE * blob,TPM_KEY12 * key)539 Trspi_UnloadBlob_KEY12(UINT64 *offset, BYTE *blob, TPM_KEY12 *key)
540 {
541           TSS_RESULT result;
542 
543           if (!key) {
544                     UINT32 PCRInfoSize, encSize;
545 
546                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
547                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
548                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
549                     Trspi_UnloadBlob_KEY_FLAGS(offset, blob, NULL);
550                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
551                     Trspi_UnloadBlob_KEY_PARMS(offset, blob, NULL);
552                     Trspi_UnloadBlob_UINT32(offset, &PCRInfoSize, blob);
553                     Trspi_UnloadBlob(offset, PCRInfoSize, blob, NULL);
554                     Trspi_UnloadBlob_STORE_PUBKEY(offset, blob, NULL);
555                     Trspi_UnloadBlob_UINT32(offset, &encSize, blob);
556                     Trspi_UnloadBlob(offset, encSize, blob, NULL);
557 
558                     return TSS_SUCCESS;
559           }
560 
561           Trspi_UnloadBlob_UINT16(offset, &key->tag, blob);
562           Trspi_UnloadBlob_UINT16(offset, &key->fill, blob);
563           Trspi_UnloadBlob_UINT16(offset, &key->keyUsage, blob);
564           Trspi_UnloadBlob_KEY_FLAGS(offset, blob, &key->keyFlags);
565           Trspi_UnloadBlob_BYTE(offset, &key->authDataUsage, blob);
566           if ((result = Trspi_UnloadBlob_KEY_PARMS(offset, (BYTE *) blob, &key->algorithmParms)))
567                     return result;
568           Trspi_UnloadBlob_UINT32(offset, &key->PCRInfoSize, blob);
569 
570           if (key->PCRInfoSize > 0) {
571                     key->PCRInfo = malloc(key->PCRInfoSize);
572                     if (key->PCRInfo == NULL) {
573                               LogError("malloc of %d bytes failed.", key->PCRInfoSize);
574                               return TSPERR(TSS_E_OUTOFMEMORY);
575                     }
576                     Trspi_UnloadBlob(offset, key->PCRInfoSize, blob, key->PCRInfo);
577           } else {
578                     key->PCRInfo = NULL;
579           }
580 
581           if ((result = Trspi_UnloadBlob_STORE_PUBKEY(offset, blob, &key->pubKey)))
582                     return result;
583           Trspi_UnloadBlob_UINT32(offset, &key->encSize, blob);
584 
585           if (key->encSize > 0) {
586                     key->encData = malloc(key->encSize);
587                     if (key->encData == NULL) {
588                               LogError("malloc of %d bytes failed.", key->encSize);
589                               return TSPERR(TSS_E_OUTOFMEMORY);
590                     }
591                     Trspi_UnloadBlob(offset, key->encSize, blob, key->encData);
592           } else {
593                     key->encData = NULL;
594           }
595 
596           return result;
597 }
598 
599 TSS_RESULT
Trspi_UnloadBlob_KEY(UINT64 * offset,BYTE * blob,TCPA_KEY * key)600 Trspi_UnloadBlob_KEY(UINT64 *offset, BYTE *blob, TCPA_KEY *key)
601 {
602           TSS_RESULT result;
603 
604           if (!key) {
605                     UINT32 PCRInfoSize, encSize;
606 
607                     Trspi_UnloadBlob_TCPA_VERSION(offset, blob, NULL);
608                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
609                     Trspi_UnloadBlob_KEY_FLAGS(offset, blob, NULL);
610                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
611                     Trspi_UnloadBlob_KEY_PARMS(offset, blob, NULL);
612                     Trspi_UnloadBlob_UINT32(offset, &PCRInfoSize, blob);
613                     Trspi_UnloadBlob(offset, PCRInfoSize, blob, NULL);
614                     Trspi_UnloadBlob_STORE_PUBKEY(offset, blob, NULL);
615                     Trspi_UnloadBlob_UINT32(offset, &encSize, blob);
616                     Trspi_UnloadBlob(offset, encSize, blob, NULL);
617 
618                     return TSS_SUCCESS;
619           }
620 
621           Trspi_UnloadBlob_TCPA_VERSION(offset, blob, &key->ver);
622           Trspi_UnloadBlob_UINT16(offset, &key->keyUsage, blob);
623           Trspi_UnloadBlob_KEY_FLAGS(offset, blob, &key->keyFlags);
624           Trspi_UnloadBlob_BYTE(offset, &key->authDataUsage, blob);
625           if ((result = Trspi_UnloadBlob_KEY_PARMS(offset, (BYTE *) blob, &key->algorithmParms)))
626                     return result;
627           Trspi_UnloadBlob_UINT32(offset, &key->PCRInfoSize, blob);
628 
629           if (key->PCRInfoSize > 0) {
630                     key->PCRInfo = malloc(key->PCRInfoSize);
631                     if (key->PCRInfo == NULL) {
632                               LogError("malloc of %d bytes failed.", key->PCRInfoSize);
633                               return TSPERR(TSS_E_OUTOFMEMORY);
634                     }
635                     Trspi_UnloadBlob(offset, key->PCRInfoSize, blob, key->PCRInfo);
636           } else {
637                     key->PCRInfo = NULL;
638           }
639 
640           if ((result = Trspi_UnloadBlob_STORE_PUBKEY(offset, blob, &key->pubKey)))
641                     return result;
642           Trspi_UnloadBlob_UINT32(offset, &key->encSize, blob);
643 
644           if (key->encSize > 0) {
645                     key->encData = malloc(key->encSize);
646                     if (key->encData == NULL) {
647                               LogError("malloc of %d bytes failed.", key->encSize);
648                               return TSPERR(TSS_E_OUTOFMEMORY);
649                     }
650                     Trspi_UnloadBlob(offset, key->encSize, blob, key->encData);
651           } else {
652                     key->encData = NULL;
653           }
654 
655           return result;
656 }
657 
658 TSS_RESULT
Trspi_UnloadBlob_STORE_PUBKEY(UINT64 * offset,BYTE * blob,TCPA_STORE_PUBKEY * store)659 Trspi_UnloadBlob_STORE_PUBKEY(UINT64 *offset, BYTE *blob, TCPA_STORE_PUBKEY *store)
660 {
661           if (!store) {
662                     UINT32 keyLength;
663 
664                     Trspi_UnloadBlob_UINT32(offset, &keyLength, blob);
665                     Trspi_UnloadBlob(offset, keyLength, blob, NULL);
666 
667                     return TSS_SUCCESS;
668           }
669 
670           Trspi_UnloadBlob_UINT32(offset, &store->keyLength, blob);
671 
672           if (store->keyLength > 0) {
673                     store->key = malloc(store->keyLength);
674                     if (store->key == NULL) {
675                               LogError("malloc of %d bytes failed.", store->keyLength);
676                               return TSPERR(TSS_E_OUTOFMEMORY);
677                     }
678                     Trspi_UnloadBlob(offset, store->keyLength, blob, store->key);
679           } else {
680                     store->key = NULL;
681           }
682 
683           return TSS_SUCCESS;
684 }
685 
686 void
Trspi_UnloadBlob_VERSION(UINT64 * offset,BYTE * blob,TCPA_VERSION * out)687 Trspi_UnloadBlob_VERSION(UINT64 *offset, BYTE *blob, TCPA_VERSION *out)
688 {
689           if (!out) {
690                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
691                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
692                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
693                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
694 
695                     return;
696           }
697 
698           Trspi_UnloadBlob_BYTE(offset, &out->major, blob);
699           Trspi_UnloadBlob_BYTE(offset, &out->minor, blob);
700           Trspi_UnloadBlob_BYTE(offset, &out->revMajor, blob);
701           Trspi_UnloadBlob_BYTE(offset, &out->revMinor, blob);
702 }
703 
704 TSS_RESULT
Trspi_UnloadBlob_KM_KEYINFO(UINT64 * offset,BYTE * blob,TSS_KM_KEYINFO * info)705 Trspi_UnloadBlob_KM_KEYINFO(UINT64 *offset, BYTE *blob, TSS_KM_KEYINFO *info)
706 {
707           if (!info) {
708                     UINT32 ulVendorDataLength;
709 
710                     Trspi_UnloadBlob_TSS_VERSION(offset, blob, NULL);
711                     Trspi_UnloadBlob_UUID(offset, blob, NULL);
712                     Trspi_UnloadBlob_UUID(offset, blob, NULL);
713                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
714                     Trspi_UnloadBlob_BOOL(offset, NULL, blob);
715                     Trspi_UnloadBlob_UINT32(offset, &ulVendorDataLength, blob);
716 
717                     (*offset) += ulVendorDataLength;
718 
719                     return TSS_SUCCESS;
720           }
721 
722           Trspi_UnloadBlob_TSS_VERSION(offset, blob, &info->versionInfo);
723           Trspi_UnloadBlob_UUID(offset, blob, &info->keyUUID);
724           Trspi_UnloadBlob_UUID(offset, blob, &info->parentKeyUUID);
725           Trspi_UnloadBlob_BYTE(offset, &info->bAuthDataUsage, blob);
726           Trspi_UnloadBlob_BOOL(offset, &info->fIsLoaded, blob);
727           Trspi_UnloadBlob_UINT32(offset, &info->ulVendorDataLength, blob);
728           if (info->ulVendorDataLength > 0){
729                     /* allocate space for vendor data */
730                     info->rgbVendorData = malloc(info->ulVendorDataLength);
731                     if (info->rgbVendorData == NULL) {
732                               LogError("malloc of %u bytes failed.", info->ulVendorDataLength);
733                               return TSPERR(TSS_E_OUTOFMEMORY);
734                     }
735 
736                     Trspi_UnloadBlob(offset, info->ulVendorDataLength, blob, info->rgbVendorData);
737           } else
738                     info->rgbVendorData = NULL;
739 
740           return TSS_SUCCESS;
741 }
742 
743 TSS_RESULT
Trspi_UnloadBlob_KM_KEYINFO2(UINT64 * offset,BYTE * blob,TSS_KM_KEYINFO2 * info)744 Trspi_UnloadBlob_KM_KEYINFO2(UINT64 *offset, BYTE *blob, TSS_KM_KEYINFO2 *info)
745 {
746           if (!info) {
747                     UINT32 ulVendorDataLength;
748 
749                     Trspi_UnloadBlob_TSS_VERSION(offset, blob, NULL);
750                     Trspi_UnloadBlob_UUID(offset, blob, NULL);
751                     Trspi_UnloadBlob_UUID(offset, blob, NULL);
752                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
753                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
754                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
755                     Trspi_UnloadBlob_BOOL(offset, NULL, blob);
756                     Trspi_UnloadBlob_UINT32(offset, &ulVendorDataLength, blob);
757 
758                     (*offset) += ulVendorDataLength;
759 
760                     return TSS_SUCCESS;
761           }
762 
763           Trspi_UnloadBlob_TSS_VERSION(offset, blob, &info->versionInfo);
764           Trspi_UnloadBlob_UUID(offset, blob, &info->keyUUID);
765           Trspi_UnloadBlob_UUID(offset, blob, &info->parentKeyUUID);
766           Trspi_UnloadBlob_BYTE(offset, &info->bAuthDataUsage, blob);
767           /* Takes data regarding the new 2 fields of TSS_KM_KEYINFO2 */
768           Trspi_UnloadBlob_UINT32(offset, &info->persistentStorageType, blob);
769           Trspi_UnloadBlob_UINT32(offset, &info->persistentStorageTypeParent, blob);
770           Trspi_UnloadBlob_BOOL(offset, &info->fIsLoaded, blob);
771           Trspi_UnloadBlob_UINT32(offset, &info->ulVendorDataLength, blob);
772           if (info->ulVendorDataLength > 0) {
773                     /* allocate space for vendor data */
774                     info->rgbVendorData = malloc(info->ulVendorDataLength);
775                     if (info->rgbVendorData == NULL) {
776                               LogError("malloc of %u bytes failed.", info->ulVendorDataLength);
777                               return TSPERR(TSS_E_OUTOFMEMORY);
778                     }
779 
780                     Trspi_UnloadBlob(offset, info->ulVendorDataLength, blob, info->rgbVendorData);
781           } else
782                     info->rgbVendorData = NULL;
783 
784           return TSS_SUCCESS;
785 }
786 
787 void
Trspi_LoadBlob_PCR_EVENT(UINT64 * offset,BYTE * blob,TSS_PCR_EVENT * event)788 Trspi_LoadBlob_PCR_EVENT(UINT64 *offset, BYTE *blob, TSS_PCR_EVENT *event)
789 {
790           Trspi_LoadBlob_TCPA_VERSION(offset, blob, *(TCPA_VERSION *)(&event->versionInfo));
791           Trspi_LoadBlob_UINT32(offset, event->ulPcrIndex, blob);
792           Trspi_LoadBlob_UINT32(offset, event->eventType, blob);
793 
794           Trspi_LoadBlob_UINT32(offset, event->ulPcrValueLength, blob);
795           if (event->ulPcrValueLength > 0)
796                     Trspi_LoadBlob(offset, event->ulPcrValueLength, blob, event->rgbPcrValue);
797 
798           Trspi_LoadBlob_UINT32(offset, event->ulEventLength, blob);
799           if (event->ulEventLength > 0)
800                     Trspi_LoadBlob(offset, event->ulEventLength, blob, event->rgbEvent);
801 
802 }
803 
804 TSS_RESULT
Trspi_UnloadBlob_PCR_EVENT(UINT64 * offset,BYTE * blob,TSS_PCR_EVENT * event)805 Trspi_UnloadBlob_PCR_EVENT(UINT64 *offset, BYTE *blob, TSS_PCR_EVENT *event)
806 {
807           if (!event) {
808                     UINT32 ulPcrValueLength, ulEventLength;
809 
810                     Trspi_UnloadBlob_VERSION(offset, blob, NULL);
811                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
812                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
813 
814                     Trspi_UnloadBlob_UINT32(offset, &ulPcrValueLength, blob);
815                     (*offset) += ulPcrValueLength;
816 
817                     Trspi_UnloadBlob_UINT32(offset, &ulEventLength, blob);
818                     (*offset) += ulEventLength;
819 
820                     return TSS_SUCCESS;
821           }
822 
823           Trspi_UnloadBlob_VERSION(offset, blob, (TCPA_VERSION *)&(event->versionInfo));
824           Trspi_UnloadBlob_UINT32(offset, &event->ulPcrIndex, blob);
825           Trspi_UnloadBlob_UINT32(offset, &event->eventType, blob);
826 
827           Trspi_UnloadBlob_UINT32(offset, &event->ulPcrValueLength, blob);
828           if (event->ulPcrValueLength > 0) {
829                     event->rgbPcrValue = malloc(event->ulPcrValueLength);
830                     if (event->rgbPcrValue == NULL) {
831                               LogError("malloc of %u bytes failed.", event->ulPcrValueLength);
832                               return TSPERR(TSS_E_OUTOFMEMORY);
833                     }
834 
835                     Trspi_UnloadBlob(offset, event->ulPcrValueLength, blob, event->rgbPcrValue);
836           } else {
837                     event->rgbPcrValue = NULL;
838           }
839 
840           Trspi_UnloadBlob_UINT32(offset, &event->ulEventLength, blob);
841           if (event->ulEventLength > 0) {
842                     event->rgbEvent = malloc(event->ulEventLength);
843                     if (event->rgbEvent == NULL) {
844                               LogError("malloc of %d bytes failed.", event->ulEventLength);
845                               return TSPERR(TSS_E_OUTOFMEMORY);
846                     }
847 
848                     Trspi_UnloadBlob(offset, event->ulEventLength, blob, event->rgbEvent);
849           } else {
850                     event->rgbEvent = NULL;
851           }
852 
853           return TSS_SUCCESS;
854 }
855 
856 /* loads a blob with the info needed to hash when creating the private key area
857  * of a TPM_KEY(12) from an external source
858  */
859 void
Trspi_LoadBlob_PRIVKEY_DIGEST12(UINT64 * offset,BYTE * blob,TPM_KEY12 * key)860 Trspi_LoadBlob_PRIVKEY_DIGEST12(UINT64 *offset, BYTE *blob, TPM_KEY12 *key)
861 {
862           Trspi_LoadBlob_UINT16(offset, key->tag, blob);
863           Trspi_LoadBlob_UINT16(offset, key->fill, blob);
864           Trspi_LoadBlob_UINT16(offset, key->keyUsage, blob);
865           Trspi_LoadBlob_KEY_FLAGS(offset, blob, &key->keyFlags);
866           Trspi_LoadBlob_BYTE(offset, key->authDataUsage, blob);
867           Trspi_LoadBlob_KEY_PARMS(offset, blob, &key->algorithmParms);
868 
869           Trspi_LoadBlob_UINT32(offset, key->PCRInfoSize, blob);
870           /* exclude pcrInfo when PCRInfoSize is 0 as spec'd in TPM 1.1b spec p.71 */
871           if (key->PCRInfoSize != 0)
872                     Trspi_LoadBlob(offset, key->PCRInfoSize, blob, key->PCRInfo);
873 
874           Trspi_LoadBlob_STORE_PUBKEY(offset, blob, &key->pubKey);
875           /* exclude encSize, encData as spec'd in TPM 1.1b spec p.71 */
876 }
877 
878 void
Trspi_LoadBlob_PRIVKEY_DIGEST(UINT64 * offset,BYTE * blob,TCPA_KEY * key)879 Trspi_LoadBlob_PRIVKEY_DIGEST(UINT64 *offset, BYTE *blob, TCPA_KEY *key)
880 {
881           Trspi_LoadBlob_TCPA_VERSION(offset, blob, key->ver);
882           Trspi_LoadBlob_UINT16(offset, key->keyUsage, blob);
883           Trspi_LoadBlob_KEY_FLAGS(offset, blob, &key->keyFlags);
884           Trspi_LoadBlob_BYTE(offset, key->authDataUsage, blob);
885           Trspi_LoadBlob_KEY_PARMS(offset, blob, &key->algorithmParms);
886 
887           Trspi_LoadBlob_UINT32(offset, key->PCRInfoSize, blob);
888           /* exclude pcrInfo when PCRInfoSize is 0 as spec'd in TPM 1.1b spec p.71 */
889           if (key->PCRInfoSize != 0)
890                     Trspi_LoadBlob(offset, key->PCRInfoSize, blob, key->PCRInfo);
891 
892           Trspi_LoadBlob_STORE_PUBKEY(offset, blob, &key->pubKey);
893           /* exclude encSize, encData as spec'd in TPM 1.1b spec p.71 */
894 }
895 
896 void
Trspi_LoadBlob_SYMMETRIC_KEY(UINT64 * offset,BYTE * blob,TCPA_SYMMETRIC_KEY * key)897 Trspi_LoadBlob_SYMMETRIC_KEY(UINT64 *offset, BYTE *blob, TCPA_SYMMETRIC_KEY *key)
898 {
899           Trspi_LoadBlob_UINT32(offset, key->algId, blob);
900           Trspi_LoadBlob_UINT16(offset, key->encScheme, blob);
901           Trspi_LoadBlob_UINT16(offset, key->size, blob);
902 
903           if (key->size > 0)
904                     Trspi_LoadBlob(offset, key->size, blob, key->data);
905 }
906 
907 TSS_RESULT
Trspi_UnloadBlob_SYMMETRIC_KEY(UINT64 * offset,BYTE * blob,TCPA_SYMMETRIC_KEY * key)908 Trspi_UnloadBlob_SYMMETRIC_KEY(UINT64 *offset, BYTE *blob, TCPA_SYMMETRIC_KEY *key)
909 {
910           if (!key) {
911                     UINT16 size;
912 
913                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
914                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
915                     Trspi_UnloadBlob_UINT16(offset, &size, blob);
916                     (*offset) += size;
917 
918                     return TSS_SUCCESS;
919           }
920 
921           Trspi_UnloadBlob_UINT32(offset, &key->algId, blob);
922           Trspi_UnloadBlob_UINT16(offset, &key->encScheme, blob);
923           Trspi_UnloadBlob_UINT16(offset, &key->size, blob);
924 
925           if (key->size > 0) {
926                     key->data = malloc(key->size);
927                     if (key->data == NULL) {
928                               key->size = 0;
929                               return TSPERR(TSS_E_OUTOFMEMORY);
930                     }
931                     Trspi_UnloadBlob(offset, key->size, blob, key->data);
932           } else {
933                     key->data = NULL;
934           }
935 
936           return TSS_SUCCESS;
937 }
938 
939 void
Trspi_LoadBlob_IDENTITY_REQ(UINT64 * offset,BYTE * blob,TCPA_IDENTITY_REQ * req)940 Trspi_LoadBlob_IDENTITY_REQ(UINT64 *offset, BYTE *blob, TCPA_IDENTITY_REQ *req)
941 {
942           Trspi_LoadBlob_UINT32(offset, req->asymSize, blob);
943           Trspi_LoadBlob_UINT32(offset, req->symSize, blob);
944           Trspi_LoadBlob_KEY_PARMS(offset, blob, &req->asymAlgorithm);
945           Trspi_LoadBlob_KEY_PARMS(offset, blob, &req->symAlgorithm);
946           Trspi_LoadBlob(offset, req->asymSize, blob, req->asymBlob);
947           Trspi_LoadBlob(offset, req->symSize, blob, req->symBlob);
948 }
949 
950 void
Trspi_LoadBlob_CHANGEAUTH_VALIDATE(UINT64 * offset,BYTE * blob,TPM_CHANGEAUTH_VALIDATE * caValidate)951 Trspi_LoadBlob_CHANGEAUTH_VALIDATE(UINT64 *offset, BYTE *blob, TPM_CHANGEAUTH_VALIDATE *caValidate)
952 {
953           Trspi_LoadBlob(offset, TCPA_SHA1_160_HASH_LEN, blob, caValidate->newAuthSecret.authdata);
954           Trspi_LoadBlob(offset, TCPA_SHA1_160_HASH_LEN, blob, caValidate->n1.nonce);
955 }
956 
957 TSS_RESULT
Trspi_UnloadBlob_IDENTITY_REQ(UINT64 * offset,BYTE * blob,TCPA_IDENTITY_REQ * req)958 Trspi_UnloadBlob_IDENTITY_REQ(UINT64 *offset, BYTE *blob, TCPA_IDENTITY_REQ *req)
959 {
960           TSS_RESULT result;
961 
962           if (!req) {
963                     UINT32 asymSize, symSize;
964 
965                     Trspi_UnloadBlob_UINT32(offset, &asymSize, blob);
966                     Trspi_UnloadBlob_UINT32(offset, &symSize, blob);
967                     (void)Trspi_UnloadBlob_KEY_PARMS(offset, blob, NULL);
968                     (void)Trspi_UnloadBlob_KEY_PARMS(offset, blob, NULL);
969 
970                     (*offset) += asymSize;
971                     (*offset) += symSize;
972 
973                     return TSS_SUCCESS;
974           }
975 
976           Trspi_UnloadBlob_UINT32(offset, &req->asymSize, blob);
977           Trspi_UnloadBlob_UINT32(offset, &req->symSize, blob);
978           if ((result = Trspi_UnloadBlob_KEY_PARMS(offset, blob, &req->asymAlgorithm)))
979                     return result;
980           if ((Trspi_UnloadBlob_KEY_PARMS(offset, blob, &req->symAlgorithm))) {
981                     free(req->asymAlgorithm.parms);
982                     req->asymAlgorithm.parmSize = 0;
983                     return result;
984           }
985 
986           if (req->asymSize > 0) {
987                     req->asymBlob = malloc(req->asymSize);
988                     if (req->asymBlob == NULL) {
989                               req->asymSize = 0;
990                               req->asymAlgorithm.parmSize = 0;
991                               free(req->asymAlgorithm.parms);
992                               req->symAlgorithm.parmSize = 0;
993                               free(req->symAlgorithm.parms);
994                               return TSPERR(TSS_E_OUTOFMEMORY);
995                     }
996                     Trspi_UnloadBlob(offset, req->asymSize, blob, req->asymBlob);
997           } else {
998                     req->asymBlob = NULL;
999           }
1000 
1001           if (req->symSize > 0) {
1002                     req->symBlob = malloc(req->symSize);
1003                     if (req->symBlob == NULL) {
1004                               req->symSize = 0;
1005                               req->asymSize = 0;
1006                               free(req->asymBlob);
1007                               req->asymBlob = NULL;
1008                               req->asymAlgorithm.parmSize = 0;
1009                               free(req->asymAlgorithm.parms);
1010                               req->symAlgorithm.parmSize = 0;
1011                               free(req->symAlgorithm.parms);
1012                               return TSPERR(TSS_E_OUTOFMEMORY);
1013                     }
1014                     Trspi_UnloadBlob(offset, req->symSize, blob, req->symBlob);
1015           } else {
1016                     req->symBlob = NULL;
1017           }
1018 
1019           return TSS_SUCCESS;
1020 }
1021 
1022 TSS_RESULT
Trspi_UnloadBlob_IDENTITY_PROOF(UINT64 * offset,BYTE * blob,TCPA_IDENTITY_PROOF * proof)1023 Trspi_UnloadBlob_IDENTITY_PROOF(UINT64 *offset, BYTE *blob, TCPA_IDENTITY_PROOF *proof)
1024 {
1025           TSS_RESULT result;
1026 
1027           if (!proof) {
1028                     UINT32 labelSize, identityBindingSize, endorsementSize, platformSize;
1029                     UINT32 conformanceSize;
1030 
1031                     Trspi_UnloadBlob_VERSION(offset, blob, NULL);
1032                     Trspi_UnloadBlob_UINT32(offset, &labelSize, blob);
1033                     Trspi_UnloadBlob_UINT32(offset, &identityBindingSize, blob);
1034                     Trspi_UnloadBlob_UINT32(offset, &endorsementSize, blob);
1035                     Trspi_UnloadBlob_UINT32(offset, &platformSize, blob);
1036                     Trspi_UnloadBlob_UINT32(offset, &conformanceSize, blob);
1037 
1038                     (void)Trspi_UnloadBlob_PUBKEY(offset, blob, NULL);
1039 
1040                     (*offset) += labelSize;
1041                     (*offset) += identityBindingSize;
1042                     (*offset) += endorsementSize;
1043                     (*offset) += platformSize;
1044                     (*offset) += conformanceSize;
1045 
1046                     return TSS_SUCCESS;
1047           }
1048 
1049           /* helps when an error occurs */
1050           memset(proof, 0, sizeof(TCPA_IDENTITY_PROOF));
1051 
1052           Trspi_UnloadBlob_VERSION(offset, blob, (TCPA_VERSION *)&proof->ver);
1053           Trspi_UnloadBlob_UINT32(offset, &proof->labelSize, blob);
1054           Trspi_UnloadBlob_UINT32(offset, &proof->identityBindingSize, blob);
1055           Trspi_UnloadBlob_UINT32(offset, &proof->endorsementSize, blob);
1056           Trspi_UnloadBlob_UINT32(offset, &proof->platformSize, blob);
1057           Trspi_UnloadBlob_UINT32(offset, &proof->conformanceSize, blob);
1058 
1059           if ((result = Trspi_UnloadBlob_PUBKEY(offset, blob,
1060                                                         &proof->identityKey))) {
1061                     proof->labelSize = 0;
1062                     proof->identityBindingSize = 0;
1063                     proof->endorsementSize = 0;
1064                     proof->platformSize = 0;
1065                     proof->conformanceSize = 0;
1066                     return result;
1067           }
1068 
1069           if (proof->labelSize > 0) {
1070                     proof->labelArea = malloc(proof->labelSize);
1071                     if (proof->labelArea == NULL) {
1072                               result = TSPERR(TSS_E_OUTOFMEMORY);
1073                               goto error;
1074                     }
1075                     Trspi_UnloadBlob(offset, proof->labelSize, blob, proof->labelArea);
1076           } else {
1077                     proof->labelArea = NULL;
1078           }
1079 
1080           if (proof->identityBindingSize > 0) {
1081                     proof->identityBinding = malloc(proof->identityBindingSize);
1082                     if (proof->identityBinding == NULL) {
1083                               result = TSPERR(TSS_E_OUTOFMEMORY);
1084                               goto error;
1085                     }
1086                     Trspi_UnloadBlob(offset, proof->identityBindingSize, blob,
1087                                          proof->identityBinding);
1088           } else {
1089                     proof->identityBinding = NULL;
1090           }
1091 
1092           if (proof->endorsementSize > 0) {
1093                     proof->endorsementCredential = malloc(proof->endorsementSize);
1094                     if (proof->endorsementCredential == NULL) {
1095                               result = TSPERR(TSS_E_OUTOFMEMORY);
1096                               goto error;
1097                     }
1098                     Trspi_UnloadBlob(offset, proof->endorsementSize, blob,
1099                                          proof->endorsementCredential);
1100           } else {
1101                     proof->endorsementCredential = NULL;
1102           }
1103 
1104           if (proof->platformSize > 0) {
1105                     proof->platformCredential = malloc(proof->platformSize);
1106                     if (proof->platformCredential == NULL) {
1107                               result = TSPERR(TSS_E_OUTOFMEMORY);
1108                               goto error;
1109                     }
1110                     Trspi_UnloadBlob(offset, proof->platformSize, blob,
1111                                          proof->platformCredential);
1112           } else {
1113                     proof->platformCredential = NULL;
1114           }
1115 
1116           if (proof->conformanceSize > 0) {
1117                     proof->conformanceCredential = malloc(proof->conformanceSize);
1118                     if (proof->conformanceCredential == NULL) {
1119                               result = TSPERR(TSS_E_OUTOFMEMORY);
1120                               goto error;
1121                     }
1122                     Trspi_UnloadBlob(offset, proof->conformanceSize, blob,
1123                                          proof->conformanceCredential);
1124           } else {
1125                     proof->conformanceCredential = NULL;
1126           }
1127 
1128           return TSS_SUCCESS;
1129 error:
1130           proof->labelSize = 0;
1131           proof->identityBindingSize = 0;
1132           proof->endorsementSize = 0;
1133           proof->platformSize = 0;
1134           proof->conformanceSize = 0;
1135           free(proof->labelArea);
1136           proof->labelArea = NULL;
1137           free(proof->identityBinding);
1138           proof->identityBinding = NULL;
1139           free(proof->endorsementCredential);
1140           proof->endorsementCredential = NULL;
1141           free(proof->conformanceCredential);
1142           proof->conformanceCredential = NULL;
1143           /* free identityKey */
1144           free(proof->identityKey.pubKey.key);
1145           free(proof->identityKey.algorithmParms.parms);
1146           proof->identityKey.pubKey.key = NULL;
1147           proof->identityKey.pubKey.keyLength = 0;
1148           proof->identityKey.algorithmParms.parms = NULL;
1149           proof->identityKey.algorithmParms.parmSize = 0;
1150 
1151           return result;
1152 }
1153 
1154 void
Trspi_LoadBlob_SYM_CA_ATTESTATION(UINT64 * offset,BYTE * blob,TCPA_SYM_CA_ATTESTATION * sym)1155 Trspi_LoadBlob_SYM_CA_ATTESTATION(UINT64 *offset, BYTE *blob, TCPA_SYM_CA_ATTESTATION *sym)
1156 {
1157           Trspi_LoadBlob_UINT32(offset, sym->credSize, blob);
1158           Trspi_LoadBlob_KEY_PARMS(offset, blob, &sym->algorithm);
1159           Trspi_LoadBlob(offset, sym->credSize, blob, sym->credential);
1160 }
1161 
1162 TSS_RESULT
Trspi_UnloadBlob_SYM_CA_ATTESTATION(UINT64 * offset,BYTE * blob,TCPA_SYM_CA_ATTESTATION * sym)1163 Trspi_UnloadBlob_SYM_CA_ATTESTATION(UINT64 *offset, BYTE *blob, TCPA_SYM_CA_ATTESTATION *sym)
1164 {
1165           TSS_RESULT result;
1166 
1167           if (!sym) {
1168                     UINT32 credSize;
1169 
1170                     Trspi_UnloadBlob_UINT32(offset, &credSize, blob);
1171                     (void)Trspi_UnloadBlob_KEY_PARMS(offset, blob, NULL);
1172 
1173                     (*offset) += credSize;
1174 
1175                     return TSS_SUCCESS;
1176           }
1177 
1178           Trspi_UnloadBlob_UINT32(offset, &sym->credSize, blob);
1179           if ((result = Trspi_UnloadBlob_KEY_PARMS(offset, blob, &sym->algorithm))) {
1180                     sym->credSize = 0;
1181                     return result;
1182           }
1183 
1184           if (sym->credSize > 0) {
1185                     if ((sym->credential = malloc(sym->credSize)) == NULL) {
1186                               free(sym->algorithm.parms);
1187                               sym->algorithm.parmSize = 0;
1188                               sym->credSize = 0;
1189                               return TSPERR(TSS_E_OUTOFMEMORY);
1190                     }
1191                     Trspi_UnloadBlob(offset, sym->credSize, blob, sym->credential);
1192           } else {
1193                     sym->credential = NULL;
1194           }
1195 
1196           return TSS_SUCCESS;
1197 }
1198 
1199 void
Trspi_LoadBlob_ASYM_CA_CONTENTS(UINT64 * offset,BYTE * blob,TCPA_ASYM_CA_CONTENTS * asym)1200 Trspi_LoadBlob_ASYM_CA_CONTENTS(UINT64 *offset, BYTE *blob, TCPA_ASYM_CA_CONTENTS *asym)
1201 {
1202           Trspi_LoadBlob_SYMMETRIC_KEY(offset, blob, &asym->sessionKey);
1203           Trspi_LoadBlob(offset, TCPA_SHA1_160_HASH_LEN, blob,
1204                            (BYTE *)&asym->idDigest);
1205 }
1206 
1207 TSS_RESULT
Trspi_UnloadBlob_ASYM_CA_CONTENTS(UINT64 * offset,BYTE * blob,TCPA_ASYM_CA_CONTENTS * asym)1208 Trspi_UnloadBlob_ASYM_CA_CONTENTS(UINT64 *offset, BYTE *blob, TCPA_ASYM_CA_CONTENTS *asym)
1209 {
1210           TSS_RESULT result;
1211 
1212           if (!asym) {
1213                     (void)Trspi_UnloadBlob_SYMMETRIC_KEY(offset, blob, NULL);
1214                     Trspi_UnloadBlob(offset, TCPA_SHA1_160_HASH_LEN, blob, NULL);
1215 
1216                     return TSS_SUCCESS;
1217           }
1218 
1219           if ((result = Trspi_UnloadBlob_SYMMETRIC_KEY(offset, blob, &asym->sessionKey)))
1220                     return result;
1221 
1222           Trspi_UnloadBlob(offset, TCPA_SHA1_160_HASH_LEN, blob, (BYTE *)&asym->idDigest);
1223 
1224           return TSS_SUCCESS;
1225 }
1226 
1227 void
Trspi_LoadBlob_BOUND_DATA(UINT64 * offset,TCPA_BOUND_DATA bd,UINT32 payloadLength,BYTE * blob)1228 Trspi_LoadBlob_BOUND_DATA(UINT64 *offset, TCPA_BOUND_DATA bd, UINT32 payloadLength, BYTE *blob)
1229 {
1230           Trspi_LoadBlob_TCPA_VERSION(offset, blob, bd.ver);
1231           Trspi_LoadBlob(offset, 1, blob, &bd.payload);
1232           Trspi_LoadBlob(offset, payloadLength, blob, bd.payloadData);
1233 }
1234 
1235 /* function to mimic strerror with TSS error codes */
1236 char *
Trspi_Error_String(TSS_RESULT r)1237 Trspi_Error_String(TSS_RESULT r)
1238 {
1239           /* Check the return code to see if it is common to all layers.
1240            * If so, return it.
1241            */
1242           switch (TSS_ERROR_CODE(r)) {
1243                     case TSS_SUCCESS:                       return "Success";
1244                     default:
1245                               break;
1246           }
1247 
1248           /* The return code is either unknown, or specific to a layer */
1249           if (TSS_ERROR_LAYER(r) == TSS_LAYER_TPM) {
1250                     switch (TSS_ERROR_CODE(r)) {
1251                               case TPM_E_AUTHFAIL:                              return "Authentication failed";
1252                               case TPM_E_BAD_PARAMETER:               return "Bad Parameter";
1253                               case TPM_E_BADINDEX:                              return "Bad memory index";
1254                               case TPM_E_AUDITFAILURE:                return "Audit failure";
1255                               case TPM_E_CLEAR_DISABLED:              return "Clear has been disabled";
1256                               case TPM_E_DEACTIVATED:                           return "TPM is deactivated";
1257                               case TPM_E_DISABLED:                              return "TPM is disabled";
1258                               case TPM_E_FAIL:                        return "Operation failed";
1259                               case TPM_E_BAD_ORDINAL:                           return "Ordinal was unknown or inconsistent";
1260                               case TPM_E_INSTALL_DISABLED:            return "Owner install disabled";
1261                               case TPM_E_INVALID_KEYHANDLE:           return "Invalid keyhandle";
1262                               case TPM_E_KEYNOTFOUND:                           return "Key not found";
1263                               case TPM_E_INAPPROPRIATE_ENC:           return "Bad encryption scheme";
1264                               case TPM_E_MIGRATEFAIL:                           return "Migration authorization failed";
1265                               case TPM_E_INVALID_PCR_INFO:            return "PCR information uninterpretable";
1266                               case TPM_E_NOSPACE:                     return "No space to load key";
1267                               case TPM_E_NOSRK:                       return "No SRK";
1268                               case TPM_E_NOTSEALED_BLOB:              return "Encrypted blob invalid";
1269                               case TPM_E_OWNER_SET:                             return "Owner already set";
1270                               case TPM_E_RESOURCES:                             return "Insufficient TPM resources";
1271                               case TPM_E_SHORTRANDOM:                           return "Random string too short";
1272                               case TPM_E_SIZE:                        return "TPM out of space";
1273                               case TPM_E_WRONGPCRVAL:                           return "Wrong PCR value";
1274                               case TPM_E_BAD_PARAM_SIZE:              return "Bad input size";
1275                               case TPM_E_SHA_THREAD:                            return "No existing SHA-1 thread";
1276                               case TPM_E_SHA_ERROR:                             return "SHA-1 error";
1277                               case TPM_E_FAILEDSELFTEST:              return "Self-test failed, TPM shutdown";
1278                               case TPM_E_AUTH2FAIL:                             return "Second authorization session failed";
1279                               case TPM_E_BADTAG:                      return "Invalid tag";
1280                               case TPM_E_IOERROR:                     return "I/O error";
1281                               case TPM_E_ENCRYPT_ERROR:               return "Encryption error";
1282                               case TPM_E_DECRYPT_ERROR:               return "Decryption error";
1283                               case TPM_E_INVALID_AUTHHANDLE:                    return "Invalid authorization handle";
1284                               case TPM_E_NO_ENDORSEMENT:              return "No EK";
1285                               case TPM_E_INVALID_KEYUSAGE:            return "Invalid key usage";
1286                               case TPM_E_WRONG_ENTITYTYPE:            return "Invalid entity type";
1287                               case TPM_E_INVALID_POSTINIT:            return "Invalid POST init sequence";
1288                               case TPM_E_INAPPROPRIATE_SIG:           return "Invalid signature format";
1289                               case TPM_E_BAD_KEY_PROPERTY:            return "Unsupported key parameters";
1290                               case TPM_E_BAD_MIGRATION:               return "Invalid migration properties";
1291                               case TPM_E_BAD_SCHEME:                            return "Invalid signature or encryption scheme";
1292                               case TPM_E_BAD_DATASIZE:                return "Invalid data size";
1293                               case TPM_E_BAD_MODE:                              return "Bad mode parameter";
1294                               case TPM_E_BAD_PRESENCE:                return "Bad physical presence value";
1295                               case TPM_E_BAD_VERSION:                           return "Invalid version";
1296                               case TPM_E_NO_WRAP_TRANSPORT:           return "TPM does not allow for wrapped transport sessions";
1297                               case TPM_E_AUDITFAIL_UNSUCCESSFUL:      return "TPM audit construction failed and the underlying command was returning a failure code also";
1298                               case TPM_E_AUDITFAIL_SUCCESSFUL:        return "TPM audit construction failed and the underlying command was returning success";
1299                               case TPM_E_NOTRESETABLE:                return "Attempt to reset a PCR register that does not have the resettable attribute";
1300                               case TPM_E_NOTLOCAL:                              return "Attempt to reset a PCR register that requires locality and locality modifier not part of command transport";
1301                               case TPM_E_BAD_TYPE:                              return "Make identity blob not properly typed";
1302                               case TPM_E_INVALID_RESOURCE:            return "When saving context identified resource type does not match actual resource";
1303                               case TPM_E_NOTFIPS:                     return "TPM is attempting to execute a command only available when in FIPS mode";
1304                               case TPM_E_INVALID_FAMILY:              return "Command is attempting to use an invalid family ID";
1305                               case TPM_E_NO_NV_PERMISSION:            return "Permission to manipulate the NV storage is not available";
1306                               case TPM_E_REQUIRES_SIGN:               return "Operation requires a signed command";
1307                               case TPM_E_KEY_NOTSUPPORTED:            return "Wrong operation to load an NV key";
1308                               case TPM_E_AUTH_CONFLICT:               return "NV_LoadKey blob requires both owner and blob authorization";
1309                               case TPM_E_AREA_LOCKED:                           return "NV area is locked and not writable";
1310                               case TPM_E_BAD_LOCALITY:                return "Locality is incorrect for attempted operation";
1311                               case TPM_E_READ_ONLY:                             return "NV area is read only and can't be written to";
1312                               case TPM_E_PER_NOWRITE:                           return "There is no protection on write to NV area";
1313                               case TPM_E_FAMILYCOUNT:                           return "Family count value does not match";
1314                               case TPM_E_WRITE_LOCKED:                return "NV area has already been written to";
1315                               case TPM_E_BAD_ATTRIBUTES:              return "NV area attributes conflict";
1316                               case TPM_E_INVALID_STRUCTURE:           return "Structure tag and version are invalid or inconsistent";
1317                               case TPM_E_KEY_OWNER_CONTROL:           return "Key is under control of TPM Owner and can only be evicted by TPM Owner";
1318                               case TPM_E_BAD_COUNTER:                           return "Counter handle is incorrect";
1319                               case TPM_E_NOT_FULLWRITE:               return "Write is not a complete write of area";
1320                               case TPM_E_CONTEXT_GAP:                           return "Gap between saved context counts is too large";
1321                               case TPM_E_MAXNVWRITES:                           return "Maximum number of NV writes without an owner has been exceeded";
1322                               case TPM_E_NOOPERATOR:                            return "No operator AuthData value is set";
1323                               case TPM_E_RESOURCEMISSING:             return "Resource pointed to by context is not loaded";
1324                               case TPM_E_DELEGATE_LOCK:               return "Delegate administration is locked";
1325                               case TPM_E_DELEGATE_FAMILY:             return "Attempt to manage a family other then delegated family";
1326                               case TPM_E_DELEGATE_ADMIN:              return "Delegation table management not enabled";
1327                               case TPM_E_TRANSPORT_NOTEXCLUSIVE:      return "A command was executed outside of an exclusive transport session";
1328                               case TPM_E_OWNER_CONTROL:               return "Attempt to context save an owner evict-controlled key";
1329                               case TPM_E_DAA_RESOURCES:               return "DAA command has no resources available to execute command";
1330                               case TPM_E_DAA_INPUT_DATA0:             return "Consistency check on DAA parameter inputData0 has failed";
1331                               case TPM_E_DAA_INPUT_DATA1:             return "Consistency check on DAA parameter inputData1 has failed";
1332                               case TPM_E_DAA_ISSUER_SETTINGS:                   return "Consistency check on DAA_issuerSettings has failed";
1333                               case TPM_E_DAA_TPM_SETTINGS:            return "Consistency check on DAA_tpmSpecific has failed";
1334                               case TPM_E_DAA_STAGE:                             return "Atomic process indicated by submitted DAA command is not expected process";
1335                               case TPM_E_DAA_ISSUER_VALIDITY:                   return "Issuer's validity check has detected an inconsistency";
1336                               case TPM_E_DAA_WRONG_W:                           return "Consistency check on w has failed";
1337                               case TPM_E_BAD_HANDLE:                            return "Handle is incorrect";
1338                               case TPM_E_BAD_DELEGATE:                return "Delegation is not correct";
1339                               case TPM_E_BADCONTEXT:                            return "Context blob is invalid";
1340                               case TPM_E_TOOMANYCONTEXTS:             return "Too many contexts held by TPM";
1341                               case TPM_E_MA_TICKET_SIGNATURE:                   return "Migration authority signature validation failure";
1342                               case TPM_E_MA_DESTINATION:              return "Migration destination not authenticated";
1343                               case TPM_E_MA_SOURCE:                             return "Migration source incorrect";
1344                               case TPM_E_MA_AUTHORITY:                return "Incorrect migration authority";
1345                               case TPM_E_PERMANENTEK:                           return "Attempt to revoke EK but EK is not revocable";
1346                               case TPM_E_BAD_SIGNATURE:               return "Bad signature of CMK ticket";
1347                               case TPM_E_NOCONTEXTSPACE:              return "No room in context list for additional contexts";
1348                               case TPM_E_RETRY:                       return "TPM busy: Retry command at a later time";
1349                               case TPM_E_NEEDS_SELFTEST:              return "SelfTestFull has not been run";
1350                               case TPM_E_DOING_SELFTEST:              return "TPM is currently executing a full selftest";
1351                               case TPM_E_DEFEND_LOCK_RUNNING:                   return "TPM is defending against dictionary attacks and is in some time-out period";
1352                               case TPM_E_DISABLED_CMD:                return "The TPM target command has been disabled";
1353                               default:                                return "Unknown error";
1354                     }
1355           } else if (TSS_ERROR_LAYER(r) == TSS_LAYER_TDDL) {
1356                     switch (TSS_ERROR_CODE(r)) {
1357                               case TSS_E_FAIL:                        return "General failure";
1358                               case TSS_E_BAD_PARAMETER:               return "Bad parameter";
1359                               case TSS_E_INTERNAL_ERROR:              return "Internal software error";
1360                               case TSS_E_NOTIMPL:                     return "Not implemented";
1361                               case TSS_E_PS_KEY_NOTFOUND:             return "Key not found in persistent storage";
1362                               case TSS_E_KEY_ALREADY_REGISTERED:      return "UUID already registered";
1363                               case TSS_E_CANCELED:                              return "The action was cancelled by request";
1364                               case TSS_E_TIMEOUT:                     return "The operation has timed out";
1365                               case TSS_E_OUTOFMEMORY:                           return "Out of memory";
1366                               case TSS_E_TPM_UNEXPECTED:              return "Unexpected TPM output";
1367                               case TSS_E_COMM_FAILURE:                return "Communication failure";
1368                               case TSS_E_TPM_UNSUPPORTED_FEATURE:     return "Unsupported feature";
1369                               case TDDL_E_COMPONENT_NOT_FOUND:        return "Connection to TPM device failed";
1370                               case TDDL_E_ALREADY_OPENED:             return "Device already opened";
1371                               case TDDL_E_BADTAG:                     return "Invalid or unsupported capability";
1372                               case TDDL_E_INSUFFICIENT_BUFFER:        return "Receive buffer too small";
1373                               case TDDL_E_COMMAND_COMPLETED:                    return "Command has already completed";
1374                               case TDDL_E_COMMAND_ABORTED:            return "TPM aborted processing of command";
1375                               case TDDL_E_ALREADY_CLOSED:             return "Device driver already closed";
1376                               case TDDL_E_IOERROR:                              return "I/O error";
1377                               default:                                return "Unknown";
1378                     }
1379           } else if (TSS_ERROR_LAYER(r) == TSS_LAYER_TCS) {
1380                     switch (TSS_ERROR_CODE(r)) {
1381                               case TSS_E_FAIL:                        return "General failure";
1382                               case TSS_E_BAD_PARAMETER:               return "Bad parameter";
1383                               case TSS_E_INTERNAL_ERROR:              return "Internal software error";
1384                               case TSS_E_NOTIMPL:                     return "Not implemented";
1385                               case TSS_E_PS_KEY_NOTFOUND:             return "Key not found in persistent storage";
1386                               case TSS_E_KEY_ALREADY_REGISTERED:      return "UUID already registered";
1387                               case TSS_E_CANCELED:                              return "The action was cancelled by request";
1388                               case TSS_E_TIMEOUT:                     return "The operation has timed out";
1389                               case TSS_E_OUTOFMEMORY:                           return "Out of memory";
1390                               case TSS_E_TPM_UNEXPECTED:              return "Unexpected TPM output";
1391                               case TSS_E_COMM_FAILURE:                return "Communication failure";
1392                               case TSS_E_TPM_UNSUPPORTED_FEATURE:     return "Unsupported feature";
1393                               case TCS_E_KEY_MISMATCH:                return "UUID does not match key handle";
1394                               case TCS_E_KM_LOADFAILED:               return "Key load failed: parent key requires authorization";
1395                               case TCS_E_KEY_CONTEXT_RELOAD:                    return "Reload of key context failed";
1396                               case TCS_E_BAD_INDEX:                             return "Bad memory index";
1397                               case TCS_E_INVALID_CONTEXTHANDLE:       return "Invalid context handle";
1398                               case TCS_E_INVALID_KEYHANDLE:           return "Invalid key handle";
1399                               case TCS_E_INVALID_AUTHHANDLE:                    return "Invalid authorization session handle";
1400                               case TCS_E_INVALID_AUTHSESSION:                   return "Authorization session has been closed by TPM";
1401                               case TCS_E_INVALID_KEY:                           return "Invalid key";
1402                               default:                                return "Unknown";
1403                     }
1404           } else {
1405                     switch (TSS_ERROR_CODE(r)) {
1406                               case TSS_E_FAIL:                        return "General failure";
1407                               case TSS_E_BAD_PARAMETER:               return "Bad parameter";
1408                               case TSS_E_INTERNAL_ERROR:              return "Internal software error";
1409                               case TSS_E_NOTIMPL:                     return "Not implemented";
1410                               case TSS_E_PS_KEY_NOTFOUND:             return "Key not found in persistent storage";
1411                               case TSS_E_KEY_ALREADY_REGISTERED:      return "UUID already registered";
1412                               case TSS_E_CANCELED:                              return "The action was cancelled by request";
1413                               case TSS_E_TIMEOUT:                     return "The operation has timed out";
1414                               case TSS_E_OUTOFMEMORY:                           return "Out of memory";
1415                               case TSS_E_TPM_UNEXPECTED:              return "Unexpected TPM output";
1416                               case TSS_E_COMM_FAILURE:                return "Communication failure";
1417                               case TSS_E_TPM_UNSUPPORTED_FEATURE:     return "Unsupported feature";
1418                               case TSS_E_INVALID_OBJECT_TYPE:                   return "Object type not valid for this operation";
1419                               case TSS_E_INVALID_OBJECT_INITFLAG:     return "Wrong flag information for object creation";
1420                               case TSS_E_INVALID_HANDLE:              return "Invalid handle";
1421                               case TSS_E_NO_CONNECTION:               return "Core service connection doesn't exist";
1422                               case TSS_E_CONNECTION_FAILED:           return "Core service connection failed";
1423                               case TSS_E_CONNECTION_BROKEN:           return "Communication with core services failed";
1424                               case TSS_E_HASH_INVALID_ALG:            return "Invalid hash algorithm";
1425                               case TSS_E_HASH_INVALID_LENGTH:                   return "Hash length is inconsistent with algorithm";
1426                               case TSS_E_HASH_NO_DATA:                return "Hash object has no internal hash value";
1427                               case TSS_E_SILENT_CONTEXT:              return "A silent context requires user input";
1428                               case TSS_E_INVALID_ATTRIB_FLAG:                   return "Flag value for attrib-functions inconsistent";
1429                               case TSS_E_INVALID_ATTRIB_SUBFLAG:      return "Sub-flag value for attrib-functions inconsistent";
1430                               case TSS_E_INVALID_ATTRIB_DATA:                   return "Data for attrib-functions invalid";
1431                               case TSS_E_NO_PCRS_SET:                           return "No PCR registers are selected or set";
1432                               case TSS_E_KEY_NOT_LOADED:              return "The addressed key is not currently loaded";
1433                               case TSS_E_KEY_NOT_SET:                           return "No key informatio is currently available";
1434                               case TSS_E_VALIDATION_FAILED:           return "Internal validation of data failed";
1435                               case TSS_E_TSP_AUTHREQUIRED:            return "Authorization is required";
1436                               case TSS_E_TSP_AUTH2REQUIRED:           return "Multiple authorizations are required";
1437                               case TSS_E_TSP_AUTHFAIL:                return "Authorization failed";
1438                               case TSS_E_TSP_AUTH2FAIL:               return "Multiple authorization failed";
1439                               case TSS_E_KEY_NO_MIGRATION_POLICY:     return "Addressed key has no migration policy";
1440                               case TSS_E_POLICY_NO_SECRET:            return "No secret information available for the address policy";
1441                               case TSS_E_INVALID_OBJ_ACCESS:                    return "Accessed object is in an inconsistent state";
1442                               case TSS_E_INVALID_ENCSCHEME:           return "Invalid encryption scheme";
1443                               case TSS_E_INVALID_SIGSCHEME:           return "Invalid signature scheme";
1444                               case TSS_E_ENC_INVALID_LENGTH:                    return "Invalid length for encrypted data object";
1445                               case TSS_E_ENC_NO_DATA:                           return "Encrypted data object contains no data";
1446                               case TSS_E_ENC_INVALID_TYPE:            return "Invalid type for encrypted data object";
1447                               case TSS_E_INVALID_KEYUSAGE:            return "Invalid usage of key";
1448                               case TSS_E_VERIFICATION_FAILED:                   return "Internal validation of data failed";
1449                               case TSS_E_HASH_NO_IDENTIFIER:                    return "Hash algorithm identifier not set";
1450                               case TSS_E_NV_AREA_EXIST:               return "NVRAM area already exists";
1451                               case TSS_E_NV_AREA_NOT_EXIST:           return "NVRAM area does not exist";
1452                               default:                                return "Unknown";
1453                     }
1454           }
1455 }
1456 
1457 char *
Trspi_Error_Layer(TSS_RESULT r)1458 Trspi_Error_Layer(TSS_RESULT r)
1459 {
1460           switch (TSS_ERROR_LAYER(r)) {
1461                     case TSS_LAYER_TPM: return "tpm";
1462                     case TSS_LAYER_TDDL:          return "tddl";
1463                     case TSS_LAYER_TCS: return "tcs";
1464                     case TSS_LAYER_TSP: return "tsp";
1465                     default:            return "unknown";
1466           }
1467 }
1468 
1469 TSS_RESULT
Trspi_Error_Code(TSS_RESULT r)1470 Trspi_Error_Code(TSS_RESULT r)
1471 {
1472           return TSS_ERROR_CODE(r);
1473 }
1474 
1475 static int
hacky_strlen(char * codeset,BYTE * string)1476 hacky_strlen(char *codeset, BYTE *string)
1477 {
1478           BYTE *ptr = string;
1479           int len = 0;
1480 
1481           if (strcmp("UTF-16", codeset) == 0) {
1482                     while (!(ptr[0] == '\0' && ptr[1] == '\0')) {
1483                               len += 2;
1484                               ptr += 2;
1485                     }
1486           } else if (strcmp("UTF-32", codeset) == 0) {
1487                     while (!(ptr[0] == '\0' && ptr[1] == '\0' &&
1488                                ptr[2] == '\0' && ptr[3] == '\0')) {
1489                               len += 4;
1490                               ptr += 4;
1491                     }
1492           } else {
1493                     /* default to 8bit chars */
1494                     while (*ptr++ != '\0') {
1495                               len++;
1496                     }
1497           }
1498 
1499           return len;
1500 }
1501 
1502 static inline int
char_width(char * codeset)1503 char_width(char *codeset)
1504 {
1505           if (strcmp("UTF-16", codeset) == 0) {
1506                     return 2;
1507           } else if (strcmp("UTF-32", codeset) == 0) {
1508                     return 4;
1509           }
1510 
1511           return 1;
1512 }
1513 
1514 #define MAX_BUF_SIZE          4096
1515 
1516 BYTE *
Trspi_Native_To_UNICODE(BYTE * string,unsigned * size)1517 Trspi_Native_To_UNICODE(BYTE *string, unsigned *size)
1518 {
1519           char *ret, *outbuf, tmpbuf[MAX_BUF_SIZE] = { 0, };
1520           BSD_CONST char *ptr;
1521           unsigned len = 0, tmplen;
1522           iconv_t cd = 0;
1523           size_t rc, outbytesleft, inbytesleft;
1524 
1525           if (string == NULL)
1526                     goto alloc_string;
1527 
1528           if ((cd = iconv_open("UTF-16LE", nl_langinfo(CODESET))) == (iconv_t)-1) {
1529                     LogDebug("iconv_open: %s", strerror(errno));
1530                     return NULL;
1531           }
1532 
1533           if ((tmplen = hacky_strlen(nl_langinfo(CODESET), string)) == 0) {
1534                     LogDebug("hacky_strlen returned 0");
1535                     goto alloc_string;
1536           }
1537 
1538           do {
1539                     len++;
1540                     outbytesleft = len;
1541                     inbytesleft = tmplen;
1542                     outbuf = tmpbuf;
1543                     ptr = (char *)string;
1544                     errno = 0;
1545 
1546                     rc = iconv(cd, (BSD_CONST char **)&ptr, &inbytesleft, &outbuf, &outbytesleft);
1547           } while (rc == (size_t)-1 && errno == E2BIG);
1548 
1549           if (len > MAX_BUF_SIZE) {
1550                     LogDebug("string too long.");
1551                     iconv_close(cd);
1552                     return NULL;
1553           }
1554 
1555 alloc_string:
1556           /* add terminating bytes of the correct width */
1557           len += char_width("UTF-16");
1558           if ((ret = calloc(1, len)) == NULL) {
1559                     LogDebug("malloc of %u bytes failed.", len);
1560                     iconv_close(cd);
1561                     return NULL;
1562           }
1563 
1564           memcpy(ret, &tmpbuf, len);
1565           if (size)
1566                     *size = len;
1567 
1568           if (cd)
1569                     iconv_close(cd);
1570 
1571           return (BYTE *)ret;
1572 
1573 }
1574 
1575 BYTE *
Trspi_UNICODE_To_Native(BYTE * string,unsigned * size)1576 Trspi_UNICODE_To_Native(BYTE *string, unsigned *size)
1577 {
1578           char *ret, *outbuf, tmpbuf[MAX_BUF_SIZE] = { 0, };
1579           BSD_CONST char *ptr;
1580           unsigned len = 0, tmplen;
1581           iconv_t cd;
1582           size_t rc, outbytesleft, inbytesleft;
1583 
1584           if (string == NULL) {
1585                     if (size)
1586                               *size = 0;
1587                     return NULL;
1588           }
1589 
1590           if ((cd = iconv_open(nl_langinfo(CODESET), "UTF-16LE")) == (iconv_t)-1) {
1591                     LogDebug("iconv_open: %s", strerror(errno));
1592                     return NULL;
1593           }
1594 
1595           if ((tmplen = hacky_strlen("UTF-16", string)) == 0) {
1596                     LogDebug("hacky_strlen returned 0");
1597                     iconv_close(cd);
1598                     return 0;
1599           }
1600 
1601           do {
1602                     len++;
1603                     outbytesleft = len;
1604                     inbytesleft = tmplen;
1605                     outbuf = tmpbuf;
1606                     ptr = (char *)string;
1607                     errno = 0;
1608 
1609                     rc = iconv(cd, (BSD_CONST char **)&ptr, &inbytesleft, &outbuf, &outbytesleft);
1610           } while (rc == (size_t)-1 && errno == E2BIG);
1611 
1612           /* add terminating bytes of the correct width */
1613           len += char_width(nl_langinfo(CODESET));
1614           if (len > MAX_BUF_SIZE) {
1615                     LogDebug("string too long.");
1616                     iconv_close(cd);
1617                     return NULL;
1618           }
1619 
1620           if ((ret = calloc(1, len)) == NULL) {
1621                     LogDebug("malloc of %d bytes failed.", len);
1622                     iconv_close(cd);
1623                     return NULL;
1624           }
1625 
1626           memcpy(ret, &tmpbuf, len);
1627           if (size)
1628                     *size = len;
1629           iconv_close(cd);
1630 
1631           return (BYTE *)ret;
1632 }
1633 
1634 /* Functions to support incremental hashing */
1635 TSS_RESULT
Trspi_Hash_UINT16(Trspi_HashCtx * c,UINT16 i)1636 Trspi_Hash_UINT16(Trspi_HashCtx *c, UINT16 i)
1637 {
1638           BYTE bytes[sizeof(UINT16)];
1639 
1640           UINT16ToArray(i, bytes);
1641           return Trspi_HashUpdate(c, sizeof(UINT16), bytes);
1642 }
1643 
1644 TSS_RESULT
Trspi_Hash_UINT32(Trspi_HashCtx * c,UINT32 i)1645 Trspi_Hash_UINT32(Trspi_HashCtx *c, UINT32 i)
1646 {
1647           BYTE bytes[sizeof(UINT32)];
1648 
1649           UINT32ToArray(i, bytes);
1650           return Trspi_HashUpdate(c, sizeof(UINT32), bytes);
1651 }
1652 
1653 TSS_RESULT
Trspi_Hash_UINT64(Trspi_HashCtx * c,UINT64 i)1654 Trspi_Hash_UINT64(Trspi_HashCtx *c, UINT64 i)
1655 {
1656           BYTE bytes[sizeof(UINT64)];
1657 
1658           UINT64ToArray(i, bytes);
1659           return Trspi_HashUpdate(c, sizeof(UINT64), bytes);
1660 }
1661 
1662 TSS_RESULT
Trspi_Hash_BYTE(Trspi_HashCtx * c,BYTE data)1663 Trspi_Hash_BYTE(Trspi_HashCtx *c, BYTE data)
1664 {
1665           return Trspi_HashUpdate(c, sizeof(BYTE), &data);
1666 }
1667 
1668 TSS_RESULT
Trspi_Hash_BOOL(Trspi_HashCtx * c,TSS_BOOL data)1669 Trspi_Hash_BOOL(Trspi_HashCtx *c, TSS_BOOL data)
1670 {
1671           return Trspi_HashUpdate(c, (UINT32)sizeof(TSS_BOOL), (BYTE *)&data);
1672 }
1673 
1674 TSS_RESULT
Trspi_Hash_VERSION(Trspi_HashCtx * c,TSS_VERSION * version)1675 Trspi_Hash_VERSION(Trspi_HashCtx *c, TSS_VERSION *version)
1676 {
1677           TSS_RESULT result;
1678 
1679           result = Trspi_Hash_BYTE(c, version->bMajor);
1680           result |= Trspi_Hash_BYTE(c, version->bMinor);
1681           result |= Trspi_Hash_BYTE(c, version->bRevMajor);
1682           result |= Trspi_Hash_BYTE(c, version->bRevMinor);
1683 
1684           return result;
1685 }
1686 
1687 TSS_RESULT
Trspi_Hash_DAA_PK(Trspi_HashCtx * c,TSS_DAA_PK * pk)1688 Trspi_Hash_DAA_PK(Trspi_HashCtx *c, TSS_DAA_PK *pk)
1689 {
1690           UINT32 i;
1691           TSS_RESULT result;
1692 
1693           result = Trspi_Hash_VERSION(c, &pk->versionInfo);
1694 
1695           result |= Trspi_Hash_UINT32(c, pk->modulusLength);
1696           result |= Trspi_HashUpdate(c, pk->modulusLength, pk->modulus);
1697 
1698           result |= Trspi_Hash_UINT32(c, pk->capitalSLength);
1699           result |= Trspi_HashUpdate(c, pk->capitalSLength, pk->capitalS);
1700 
1701           result |= Trspi_Hash_UINT32(c, pk->capitalZLength);
1702           result |= Trspi_HashUpdate(c, pk->capitalZLength, pk->capitalZ);
1703 
1704           result |= Trspi_Hash_UINT32(c, pk->capitalR0Length);
1705           result |= Trspi_HashUpdate(c, pk->capitalR0Length, pk->capitalR0);
1706 
1707           result |= Trspi_Hash_UINT32(c, pk->capitalR1Length);
1708           result |= Trspi_HashUpdate(c, pk->capitalR1Length, pk->capitalR1);
1709 
1710           result |= Trspi_Hash_UINT32(c, pk->gammaLength);
1711           result |= Trspi_HashUpdate(c, pk->gammaLength, pk->gamma);
1712 
1713           result |= Trspi_Hash_UINT32(c, pk->capitalGammaLength);
1714           result |= Trspi_HashUpdate(c, pk->capitalGammaLength, pk->capitalGamma);
1715 
1716           result |= Trspi_Hash_UINT32(c, pk->rhoLength);
1717           result |= Trspi_HashUpdate(c, pk->rhoLength, pk->rho);
1718 
1719           for (i = 0; i < pk->capitalYLength; i++)
1720                     result |= Trspi_HashUpdate(c, pk->capitalYLength2, pk->capitalY[i]);
1721 
1722           result |= Trspi_Hash_UINT32(c, pk->capitalYPlatformLength);
1723 
1724           result |= Trspi_Hash_UINT32(c, pk->issuerBaseNameLength);
1725           result |= Trspi_HashUpdate(c, pk->issuerBaseNameLength, pk->issuerBaseName);
1726 
1727           return result;
1728 }
1729 
1730 TSS_RESULT
Trspi_Hash_RSA_KEY_PARMS(Trspi_HashCtx * c,TCPA_RSA_KEY_PARMS * parms)1731 Trspi_Hash_RSA_KEY_PARMS(Trspi_HashCtx *c, TCPA_RSA_KEY_PARMS *parms)
1732 {
1733           TSS_RESULT result;
1734 
1735           result = Trspi_Hash_UINT32(c, parms->keyLength);
1736           result |= Trspi_Hash_UINT32(c, parms->numPrimes);
1737           result |= Trspi_Hash_UINT32(c, parms->exponentSize);
1738 
1739           if (parms->exponentSize > 0)
1740                     result |= Trspi_HashUpdate(c, parms->exponentSize, parms->exponent);
1741 
1742           return result;
1743 }
1744 
1745 TSS_RESULT
Trspi_Hash_STORE_PUBKEY(Trspi_HashCtx * c,TCPA_STORE_PUBKEY * store)1746 Trspi_Hash_STORE_PUBKEY(Trspi_HashCtx *c, TCPA_STORE_PUBKEY *store)
1747 {
1748           TSS_RESULT result;
1749 
1750           result = Trspi_Hash_UINT32(c, store->keyLength);
1751           result |= Trspi_HashUpdate(c, store->keyLength, store->key);
1752 
1753           return result;
1754 }
1755 
1756 TSS_RESULT
Trspi_Hash_KEY_PARMS(Trspi_HashCtx * c,TCPA_KEY_PARMS * keyInfo)1757 Trspi_Hash_KEY_PARMS(Trspi_HashCtx *c, TCPA_KEY_PARMS *keyInfo)
1758 {
1759           TSS_RESULT result;
1760 
1761           result = Trspi_Hash_UINT32(c, keyInfo->algorithmID);
1762           result |= Trspi_Hash_UINT16(c, keyInfo->encScheme);
1763           result |= Trspi_Hash_UINT16(c, keyInfo->sigScheme);
1764           result |= Trspi_Hash_UINT32(c, keyInfo->parmSize);
1765 
1766           if (keyInfo->parmSize > 0)
1767                     result |= Trspi_HashUpdate(c, keyInfo->parmSize, keyInfo->parms);
1768 
1769           return result;
1770 }
1771 
1772 TSS_RESULT
Trspi_Hash_PUBKEY(Trspi_HashCtx * c,TCPA_PUBKEY * pubKey)1773 Trspi_Hash_PUBKEY(Trspi_HashCtx *c, TCPA_PUBKEY *pubKey)
1774 {
1775           TSS_RESULT result;
1776 
1777           result = Trspi_Hash_KEY_PARMS(c, &pubKey->algorithmParms);
1778           result |= Trspi_Hash_STORE_PUBKEY(c, &pubKey->pubKey);
1779 
1780           return result;
1781 }
1782 
1783 TSS_RESULT
Trspi_Hash_STORED_DATA(Trspi_HashCtx * c,TCPA_STORED_DATA * data)1784 Trspi_Hash_STORED_DATA(Trspi_HashCtx *c, TCPA_STORED_DATA *data)
1785 {
1786           TSS_RESULT result;
1787 
1788           result = Trspi_Hash_VERSION(c, (TSS_VERSION *)&data->ver);
1789           result |= Trspi_Hash_UINT32(c, data->sealInfoSize);
1790           result |= Trspi_HashUpdate(c, data->sealInfoSize, data->sealInfo);
1791           result |= Trspi_Hash_UINT32(c, data->encDataSize);
1792           result |= Trspi_HashUpdate(c, data->encDataSize, data->encData);
1793 
1794           return result;
1795 }
1796 
1797 TSS_RESULT
Trspi_Hash_PCR_SELECTION(Trspi_HashCtx * c,TCPA_PCR_SELECTION * pcr)1798 Trspi_Hash_PCR_SELECTION(Trspi_HashCtx *c, TCPA_PCR_SELECTION *pcr)
1799 {
1800           TSS_RESULT result;
1801           UINT16 i;
1802 
1803           result = Trspi_Hash_UINT16(c, pcr->sizeOfSelect);
1804 
1805           for (i = 0; i < pcr->sizeOfSelect; i++)
1806                     result |= Trspi_Hash_BYTE(c, pcr->pcrSelect[i]);
1807 
1808           return result;
1809 }
1810 
1811 TSS_RESULT
Trspi_Hash_KEY_FLAGS(Trspi_HashCtx * c,TCPA_KEY_FLAGS * flags)1812 Trspi_Hash_KEY_FLAGS(Trspi_HashCtx *c, TCPA_KEY_FLAGS *flags)
1813 {
1814           return Trspi_Hash_UINT32(c, *flags);
1815 }
1816 
1817 TSS_RESULT
Trspi_Hash_KEY12(Trspi_HashCtx * c,TPM_KEY12 * key)1818 Trspi_Hash_KEY12(Trspi_HashCtx *c, TPM_KEY12 *key)
1819 {
1820           TSS_RESULT result;
1821 
1822           result = Trspi_Hash_UINT16(c, key->tag);
1823           result |= Trspi_Hash_UINT16(c, key->fill);
1824           result |= Trspi_Hash_UINT16(c, key->keyUsage);
1825           result |= Trspi_Hash_KEY_FLAGS(c, &key->keyFlags);
1826           result |= Trspi_Hash_BYTE(c, key->authDataUsage);
1827           result |= Trspi_Hash_KEY_PARMS(c, &key->algorithmParms);
1828           result |= Trspi_Hash_UINT32(c, key->PCRInfoSize);
1829           result |= Trspi_HashUpdate(c, key->PCRInfoSize, key->PCRInfo);
1830           result |= Trspi_Hash_STORE_PUBKEY(c, &key->pubKey);
1831           result |= Trspi_Hash_UINT32(c, key->encSize);
1832           result |= Trspi_HashUpdate(c, key->encSize, key->encData);
1833 
1834           return result;
1835 }
1836 
1837 TSS_RESULT
Trspi_Hash_KEY(Trspi_HashCtx * c,TCPA_KEY * key)1838 Trspi_Hash_KEY(Trspi_HashCtx *c, TCPA_KEY *key)
1839 {
1840           TSS_RESULT result;
1841 
1842           result = Trspi_Hash_VERSION(c, (TSS_VERSION *)&key->ver);
1843           result |= Trspi_Hash_UINT16(c, key->keyUsage);
1844           result |= Trspi_Hash_KEY_FLAGS(c, &key->keyFlags);
1845           result |= Trspi_Hash_BYTE(c, key->authDataUsage);
1846           result |= Trspi_Hash_KEY_PARMS(c, &key->algorithmParms);
1847           result |= Trspi_Hash_UINT32(c, key->PCRInfoSize);
1848           result |= Trspi_HashUpdate(c, key->PCRInfoSize, key->PCRInfo);
1849           result |= Trspi_Hash_STORE_PUBKEY(c, &key->pubKey);
1850           result |= Trspi_Hash_UINT32(c, key->encSize);
1851           result |= Trspi_HashUpdate(c, key->encSize, key->encData);
1852 
1853           return result;
1854 }
1855 
1856 TSS_RESULT
Trspi_Hash_UUID(Trspi_HashCtx * c,TSS_UUID uuid)1857 Trspi_Hash_UUID(Trspi_HashCtx *c, TSS_UUID uuid)
1858 {
1859           TSS_RESULT result;
1860 
1861           result = Trspi_Hash_UINT32(c, uuid.ulTimeLow);
1862           result |= Trspi_Hash_UINT16(c, uuid.usTimeMid);
1863           result |= Trspi_Hash_UINT16(c, uuid.usTimeHigh);
1864           result |= Trspi_Hash_BYTE(c, uuid.bClockSeqHigh);
1865           result |= Trspi_Hash_BYTE(c, uuid.bClockSeqLow);
1866           result |= Trspi_HashUpdate(c, sizeof(uuid.rgbNode), uuid.rgbNode);
1867 
1868           return result;
1869 }
1870 
1871 TSS_RESULT
Trspi_Hash_PCR_EVENT(Trspi_HashCtx * c,TSS_PCR_EVENT * event)1872 Trspi_Hash_PCR_EVENT(Trspi_HashCtx *c, TSS_PCR_EVENT *event)
1873 {
1874           TSS_RESULT result;
1875 
1876           result = Trspi_Hash_VERSION(c, &event->versionInfo);
1877           result |= Trspi_Hash_UINT32(c, event->ulPcrIndex);
1878           result |= Trspi_Hash_UINT32(c, event->eventType);
1879 
1880           Trspi_Hash_UINT32(c, event->ulPcrValueLength);
1881           if (event->ulPcrValueLength > 0)
1882                     result |= Trspi_HashUpdate(c, event->ulPcrValueLength, event->rgbPcrValue);
1883 
1884           result |= Trspi_Hash_UINT32(c, event->ulEventLength);
1885           if (event->ulEventLength > 0)
1886                     result |= Trspi_HashUpdate(c, event->ulEventLength, event->rgbEvent);
1887 
1888 
1889           return result;
1890 }
1891 
1892 TSS_RESULT
Trspi_Hash_PRIVKEY_DIGEST12(Trspi_HashCtx * c,TPM_KEY12 * key)1893 Trspi_Hash_PRIVKEY_DIGEST12(Trspi_HashCtx *c, TPM_KEY12 *key)
1894 {
1895           TSS_RESULT result;
1896 
1897           result = Trspi_Hash_UINT16(c, key->tag);
1898           result |= Trspi_Hash_UINT16(c, key->fill);
1899           result |= Trspi_Hash_UINT16(c, key->keyUsage);
1900           result |= Trspi_Hash_KEY_FLAGS(c, &key->keyFlags);
1901           result |= Trspi_Hash_BYTE(c, key->authDataUsage);
1902           result |= Trspi_Hash_KEY_PARMS(c, &key->algorithmParms);
1903 
1904           result |= Trspi_Hash_UINT32(c, key->PCRInfoSize);
1905           /* exclude pcrInfo when PCRInfoSize is 0 as spec'd in TPM 1.1b spec p.71 */
1906           if (key->PCRInfoSize != 0)
1907                     result |= Trspi_HashUpdate(c, key->PCRInfoSize, key->PCRInfo);
1908 
1909           Trspi_Hash_STORE_PUBKEY(c, &key->pubKey);
1910           /* exclude encSize, encData as spec'd in TPM 1.1b spec p.71 */
1911 
1912           return result;
1913 }
1914 
1915 TSS_RESULT
Trspi_Hash_PRIVKEY_DIGEST(Trspi_HashCtx * c,TCPA_KEY * key)1916 Trspi_Hash_PRIVKEY_DIGEST(Trspi_HashCtx *c, TCPA_KEY *key)
1917 {
1918           TSS_RESULT result;
1919 
1920           result = Trspi_Hash_VERSION(c, (TSS_VERSION *)&key->ver);
1921           result |= Trspi_Hash_UINT16(c, key->keyUsage);
1922           result |= Trspi_Hash_KEY_FLAGS(c, &key->keyFlags);
1923           result |= Trspi_Hash_BYTE(c, key->authDataUsage);
1924           result |= Trspi_Hash_KEY_PARMS(c, &key->algorithmParms);
1925 
1926           result |= Trspi_Hash_UINT32(c, key->PCRInfoSize);
1927           /* exclude pcrInfo when PCRInfoSize is 0 as spec'd in TPM 1.1b spec p.71 */
1928           if (key->PCRInfoSize != 0)
1929                     result |= Trspi_HashUpdate(c, key->PCRInfoSize, key->PCRInfo);
1930 
1931           Trspi_Hash_STORE_PUBKEY(c, &key->pubKey);
1932           /* exclude encSize, encData as spec'd in TPM 1.1b spec p.71 */
1933 
1934           return result;
1935 }
1936 
1937 TSS_RESULT
Trspi_Hash_SYMMETRIC_KEY(Trspi_HashCtx * c,TCPA_SYMMETRIC_KEY * key)1938 Trspi_Hash_SYMMETRIC_KEY(Trspi_HashCtx *c, TCPA_SYMMETRIC_KEY *key)
1939 {
1940           TSS_RESULT result;
1941 
1942           result = Trspi_Hash_UINT32(c, key->algId);
1943           result |= Trspi_Hash_UINT16(c, key->encScheme);
1944           result |= Trspi_Hash_UINT16(c, key->size);
1945 
1946           if (key->size > 0)
1947                     result |= Trspi_HashUpdate(c, key->size, key->data);
1948 
1949           return result;
1950 }
1951 
1952 TSS_RESULT
Trspi_Hash_IDENTITY_REQ(Trspi_HashCtx * c,TCPA_IDENTITY_REQ * req)1953 Trspi_Hash_IDENTITY_REQ(Trspi_HashCtx *c, TCPA_IDENTITY_REQ *req)
1954 {
1955           TSS_RESULT result;
1956 
1957           result = Trspi_Hash_UINT32(c, req->asymSize);
1958           result |= Trspi_Hash_UINT32(c, req->symSize);
1959           result |= Trspi_Hash_KEY_PARMS(c, &req->asymAlgorithm);
1960           result |= Trspi_Hash_KEY_PARMS(c, &req->symAlgorithm);
1961           result |= Trspi_HashUpdate(c, req->asymSize, req->asymBlob);
1962           result |= Trspi_HashUpdate(c, req->symSize, req->symBlob);
1963 
1964           return result;
1965 }
1966 
1967 TSS_RESULT
Trspi_Hash_CHANGEAUTH_VALIDATE(Trspi_HashCtx * c,TPM_CHANGEAUTH_VALIDATE * caValidate)1968 Trspi_Hash_CHANGEAUTH_VALIDATE(Trspi_HashCtx *c, TPM_CHANGEAUTH_VALIDATE *caValidate)
1969 {
1970           TSS_RESULT result;
1971 
1972           result = Trspi_HashUpdate(c, TCPA_SHA1_160_HASH_LEN, caValidate->newAuthSecret.authdata);
1973           result |= Trspi_HashUpdate(c, TCPA_SHA1_160_HASH_LEN, caValidate->n1.nonce);
1974 
1975           return result;
1976 }
1977 
1978 TSS_RESULT
Trspi_Hash_SYM_CA_ATTESTATION(Trspi_HashCtx * c,TCPA_SYM_CA_ATTESTATION * sym)1979 Trspi_Hash_SYM_CA_ATTESTATION(Trspi_HashCtx *c, TCPA_SYM_CA_ATTESTATION *sym)
1980 {
1981           TSS_RESULT result;
1982 
1983           result = Trspi_Hash_UINT32(c, sym->credSize);
1984           result |= Trspi_Hash_KEY_PARMS(c, &sym->algorithm);
1985           result |= Trspi_HashUpdate(c, sym->credSize, sym->credential);
1986 
1987           return result;
1988 }
1989 
1990 TSS_RESULT
Trspi_Hash_ASYM_CA_CONTENTS(Trspi_HashCtx * c,TCPA_ASYM_CA_CONTENTS * asym)1991 Trspi_Hash_ASYM_CA_CONTENTS(Trspi_HashCtx *c, TCPA_ASYM_CA_CONTENTS *asym)
1992 {
1993           TSS_RESULT result;
1994 
1995           result = Trspi_Hash_SYMMETRIC_KEY(c, &asym->sessionKey);
1996           result |= Trspi_HashUpdate(c, TCPA_SHA1_160_HASH_LEN, (BYTE *)&asym->idDigest);
1997 
1998           return result;
1999 }
2000 
2001 TSS_RESULT
Trspi_Hash_BOUND_DATA(Trspi_HashCtx * c,TCPA_BOUND_DATA * bd,UINT32 payloadLength)2002 Trspi_Hash_BOUND_DATA(Trspi_HashCtx *c, TCPA_BOUND_DATA *bd, UINT32 payloadLength)
2003 {
2004           TSS_RESULT result;
2005 
2006           result = Trspi_Hash_VERSION(c, (TSS_VERSION *)&bd->ver);
2007           result |= Trspi_Hash_BYTE(c, bd->payload);
2008           result |= Trspi_HashUpdate(c, payloadLength, bd->payloadData);
2009 
2010           return result;
2011 }
2012 
2013 TSS_RESULT
Trspi_Hash_TRANSPORT_AUTH(Trspi_HashCtx * c,TPM_TRANSPORT_AUTH * a)2014 Trspi_Hash_TRANSPORT_AUTH(Trspi_HashCtx *c, TPM_TRANSPORT_AUTH *a)
2015 {
2016           TSS_RESULT result;
2017 
2018           result = Trspi_Hash_UINT16(c, a->tag);
2019           result |= Trspi_HashUpdate(c, TPM_SHA1_160_HASH_LEN, a->authData.authdata);
2020 
2021           return result;
2022 }
2023 
2024 TSS_RESULT
Trspi_Hash_TRANSPORT_LOG_IN(Trspi_HashCtx * c,TPM_TRANSPORT_LOG_IN * l)2025 Trspi_Hash_TRANSPORT_LOG_IN(Trspi_HashCtx *c, TPM_TRANSPORT_LOG_IN *l)
2026 {
2027           TSS_RESULT result;
2028 
2029           result = Trspi_Hash_UINT16(c, l->tag);
2030           result |= Trspi_Hash_DIGEST(c, l->parameters.digest);
2031           result |= Trspi_Hash_DIGEST(c, l->pubKeyHash.digest);
2032 
2033           return result;
2034 }
2035 
2036 TSS_RESULT
Trspi_Hash_TRANSPORT_LOG_OUT(Trspi_HashCtx * c,TPM_TRANSPORT_LOG_OUT * l)2037 Trspi_Hash_TRANSPORT_LOG_OUT(Trspi_HashCtx *c, TPM_TRANSPORT_LOG_OUT *l)
2038 {
2039           TSS_RESULT result;
2040 
2041           result = Trspi_Hash_UINT16(c, l->tag);
2042           result |= Trspi_Hash_CURRENT_TICKS(c, &l->currentTicks);
2043           result |= Trspi_Hash_DIGEST(c, l->parameters.digest);
2044           result |= Trspi_Hash_UINT32(c, l->locality);
2045 
2046           return result;
2047 }
2048 
2049 TSS_RESULT
Trspi_Hash_CURRENT_TICKS(Trspi_HashCtx * c,TPM_CURRENT_TICKS * t)2050 Trspi_Hash_CURRENT_TICKS(Trspi_HashCtx *c, TPM_CURRENT_TICKS *t)
2051 {
2052           TSS_RESULT result;
2053 
2054           result = Trspi_Hash_UINT16(c, t->tag);
2055           result |= Trspi_Hash_UINT64(c, t->currentTicks);
2056           result |= Trspi_Hash_UINT16(c, t->tickRate);
2057           result |= Trspi_Hash_NONCE(c, t->tickNonce.nonce);
2058 
2059           return result;
2060 }
2061 
2062 TSS_RESULT
Trspi_Hash_SIGN_INFO(Trspi_HashCtx * c,TPM_SIGN_INFO * s)2063 Trspi_Hash_SIGN_INFO(Trspi_HashCtx *c, TPM_SIGN_INFO *s)
2064 {
2065           TSS_RESULT result;
2066 
2067           result = Trspi_Hash_UINT16(c, s->tag);
2068           result |= Trspi_HashUpdate(c, 4, s->fixed);
2069           result |= Trspi_Hash_NONCE(c, s->replay.nonce);
2070           result |= Trspi_Hash_UINT32(c, s->dataLen);
2071           result |= Trspi_HashUpdate(c, s->dataLen, s->data);
2072 
2073           return result;
2074 }
2075 
2076 void
Trspi_UnloadBlob_COUNTER_VALUE(UINT64 * offset,BYTE * blob,TPM_COUNTER_VALUE * ctr)2077 Trspi_UnloadBlob_COUNTER_VALUE(UINT64 *offset, BYTE *blob, TPM_COUNTER_VALUE *ctr)
2078 {
2079           if (!ctr) {
2080                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
2081                     /* '4' is hard-coded in the spec */
2082                     Trspi_UnloadBlob(offset, 4, blob, NULL);
2083                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2084 
2085                     return;
2086           }
2087 
2088           Trspi_UnloadBlob_UINT16(offset, &ctr->tag, blob);
2089           /* '4' is hard-coded in the spec */
2090           Trspi_UnloadBlob(offset, 4, blob, (BYTE *)&ctr->label);
2091           Trspi_UnloadBlob_UINT32(offset, &ctr->counter, blob);
2092 }
2093 
2094 void
Trspi_LoadBlob_COUNTER_VALUE(UINT64 * offset,BYTE * blob,TPM_COUNTER_VALUE * ctr)2095 Trspi_LoadBlob_COUNTER_VALUE(UINT64 *offset, BYTE *blob, TPM_COUNTER_VALUE *ctr)
2096 {
2097           Trspi_LoadBlob_UINT16(offset, ctr->tag, blob);
2098           Trspi_LoadBlob(offset, 4, blob, (BYTE *)&ctr->label);
2099           Trspi_LoadBlob_UINT32(offset, ctr->counter, blob);
2100 }
2101 
2102 void
Trspi_UnloadBlob_CURRENT_TICKS(UINT64 * offset,BYTE * blob,TPM_CURRENT_TICKS * ticks)2103 Trspi_UnloadBlob_CURRENT_TICKS(UINT64 *offset, BYTE *blob, TPM_CURRENT_TICKS *ticks)
2104 {
2105           if (!ticks) {
2106                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
2107                     Trspi_UnloadBlob_UINT64(offset, NULL, blob);
2108                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
2109                     Trspi_UnloadBlob(offset, sizeof(TPM_NONCE), blob, NULL);
2110 
2111                     return;
2112           }
2113 
2114           Trspi_UnloadBlob_UINT16(offset, &ticks->tag, blob);
2115           Trspi_UnloadBlob_UINT64(offset, &ticks->currentTicks, blob);
2116           Trspi_UnloadBlob_UINT16(offset, &ticks->tickRate, blob);
2117           Trspi_UnloadBlob(offset, sizeof(TPM_NONCE), blob, (BYTE *)&ticks->tickNonce);
2118 }
2119 
2120 void
Trspi_UnloadBlob_TRANSPORT_PUBLIC(UINT64 * offset,BYTE * blob,TPM_TRANSPORT_PUBLIC * t)2121 Trspi_UnloadBlob_TRANSPORT_PUBLIC(UINT64 *offset, BYTE *blob, TPM_TRANSPORT_PUBLIC *t)
2122 {
2123           Trspi_UnloadBlob_UINT16(offset, &t->tag, blob);
2124           Trspi_UnloadBlob_UINT32(offset, &t->transAttributes, blob);
2125           Trspi_UnloadBlob_UINT32(offset, &t->algId, blob);
2126           Trspi_UnloadBlob_UINT16(offset, &t->encScheme, blob);
2127 }
2128 
2129 void
Trspi_LoadBlob_TRANSPORT_PUBLIC(UINT64 * offset,BYTE * blob,TPM_TRANSPORT_PUBLIC * t)2130 Trspi_LoadBlob_TRANSPORT_PUBLIC(UINT64 *offset, BYTE *blob, TPM_TRANSPORT_PUBLIC *t)
2131 {
2132           Trspi_LoadBlob_UINT16(offset, t->tag, blob);
2133           Trspi_LoadBlob_UINT32(offset, t->transAttributes, blob);
2134           Trspi_LoadBlob_UINT32(offset, t->algId, blob);
2135           Trspi_LoadBlob_UINT16(offset, t->encScheme, blob);
2136 }
2137 
2138 void
Trspi_LoadBlob_TRANSPORT_AUTH(UINT64 * offset,BYTE * blob,TPM_TRANSPORT_AUTH * t)2139 Trspi_LoadBlob_TRANSPORT_AUTH(UINT64 *offset, BYTE *blob, TPM_TRANSPORT_AUTH *t)
2140 {
2141           Trspi_LoadBlob_UINT16(offset, t->tag, blob);
2142           Trspi_LoadBlob(offset, TPM_SHA1_160_HASH_LEN, blob, t->authData.authdata);
2143 }
2144 
2145 void
Trspi_LoadBlob_SIGN_INFO(UINT64 * offset,BYTE * blob,TPM_SIGN_INFO * s)2146 Trspi_LoadBlob_SIGN_INFO(UINT64 *offset, BYTE *blob, TPM_SIGN_INFO *s)
2147 {
2148           Trspi_LoadBlob_UINT16(offset, s->tag, blob);
2149           Trspi_LoadBlob(offset, 4, blob, s->fixed);
2150           Trspi_LoadBlob(offset, TPM_SHA1_160_HASH_LEN, blob, s->replay.nonce);
2151           Trspi_LoadBlob_UINT32(offset, s->dataLen, blob);
2152           Trspi_LoadBlob(offset, s->dataLen, blob, s->data);
2153 }
2154 
2155 TSS_RESULT
Trspi_UnloadBlob_CERTIFY_INFO(UINT64 * offset,BYTE * blob,TPM_CERTIFY_INFO * c)2156 Trspi_UnloadBlob_CERTIFY_INFO(UINT64 *offset, BYTE *blob, TPM_CERTIFY_INFO *c)
2157 {
2158           TSS_RESULT result;
2159 
2160           if (!c) {
2161                     UINT32 pcrInfoSize;
2162 
2163                     Trspi_UnloadBlob_VERSION(offset, blob, NULL);
2164                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
2165                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2166                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
2167                     Trspi_UnloadBlob_KEY_PARMS(offset, blob, NULL);
2168                     Trspi_UnloadBlob_DIGEST(offset, blob, NULL);
2169                     Trspi_UnloadBlob_NONCE(offset, blob, NULL);
2170                     Trspi_UnloadBlob_BOOL(offset, NULL, blob);
2171                     Trspi_UnloadBlob_UINT32(offset, &pcrInfoSize, blob);
2172 
2173                     (*offset) += pcrInfoSize;
2174 
2175                     return TSS_SUCCESS;
2176           }
2177 
2178           Trspi_UnloadBlob_VERSION(offset, blob, &c->version);
2179           Trspi_UnloadBlob_UINT16(offset, &c->keyUsage, blob);
2180           Trspi_UnloadBlob_UINT32(offset, &c->keyFlags, blob);
2181           Trspi_UnloadBlob_BYTE(offset, &c->authDataUsage, blob);
2182           if ((result = Trspi_UnloadBlob_KEY_PARMS(offset, blob, &c->algorithmParms)))
2183                     return result;
2184           Trspi_UnloadBlob_DIGEST(offset, blob, &c->pubkeyDigest);
2185           Trspi_UnloadBlob_NONCE(offset, blob, &c->data);
2186           Trspi_UnloadBlob_BOOL(offset, (TSS_BOOL *)&c->parentPCRStatus, blob);
2187           Trspi_UnloadBlob_UINT32(offset, &c->PCRInfoSize, blob);
2188         if (c->PCRInfoSize != 0) {
2189                 c->PCRInfo = malloc(sizeof(TPM_PCR_INFO));
2190                 if (c->PCRInfo == NULL) {
2191                         LogError("malloc of %zd bytes failed.", sizeof(TPM_PCR_INFO));
2192                         return TSPERR(TSS_E_OUTOFMEMORY);
2193                 }
2194         } else {
2195                 c->PCRInfo = NULL;
2196         }
2197         Trspi_UnloadBlob_PCR_INFO(offset, blob, (TPM_PCR_INFO *)c->PCRInfo);
2198 
2199           return TSS_SUCCESS;
2200 }
2201 
2202 void
Trspi_UnloadBlob_TPM_FAMILY_LABEL(UINT64 * offset,BYTE * blob,TPM_FAMILY_LABEL * label)2203 Trspi_UnloadBlob_TPM_FAMILY_LABEL(UINT64 *offset, BYTE *blob, TPM_FAMILY_LABEL *label)
2204 {
2205           if (!label) {
2206                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
2207 
2208                     return;
2209           }
2210 
2211           Trspi_UnloadBlob_BYTE(offset, &label->label, blob);
2212 }
2213 
2214 void
Trspi_LoadBlob_TPM_FAMILY_LABEL(UINT64 * offset,BYTE * blob,TPM_FAMILY_LABEL * label)2215 Trspi_LoadBlob_TPM_FAMILY_LABEL(UINT64 *offset, BYTE *blob, TPM_FAMILY_LABEL *label)
2216 {
2217           Trspi_LoadBlob_BYTE(offset, label->label, blob);
2218 }
2219 
2220 void
Trspi_UnloadBlob_TPM_FAMILY_TABLE_ENTRY(UINT64 * offset,BYTE * blob,TPM_FAMILY_TABLE_ENTRY * entry)2221 Trspi_UnloadBlob_TPM_FAMILY_TABLE_ENTRY(UINT64 *offset, BYTE *blob, TPM_FAMILY_TABLE_ENTRY *entry)
2222 {
2223           if (!entry) {
2224                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
2225                     Trspi_UnloadBlob_TPM_FAMILY_LABEL(offset, blob, NULL);
2226                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2227                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2228                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2229 
2230                     return;
2231           }
2232 
2233           Trspi_UnloadBlob_UINT16(offset, &entry->tag, blob);
2234           Trspi_UnloadBlob_TPM_FAMILY_LABEL(offset, blob, &entry->label);
2235           Trspi_UnloadBlob_UINT32(offset, &entry->familyID, blob);
2236           Trspi_UnloadBlob_UINT32(offset, &entry->verificationCount, blob);
2237           Trspi_UnloadBlob_UINT32(offset, &entry->flags, blob);
2238 }
2239 
2240 void
Trspi_LoadBlob_TPM_FAMILY_TABLE_ENTRY(UINT64 * offset,BYTE * blob,TPM_FAMILY_TABLE_ENTRY * entry)2241 Trspi_LoadBlob_TPM_FAMILY_TABLE_ENTRY(UINT64 *offset, BYTE *blob, TPM_FAMILY_TABLE_ENTRY *entry)
2242 {
2243           Trspi_LoadBlob_UINT16(offset, entry->tag, blob);
2244           Trspi_LoadBlob_TPM_FAMILY_LABEL(offset, blob, &entry->label);
2245           Trspi_LoadBlob_UINT32(offset, entry->familyID, blob);
2246           Trspi_LoadBlob_UINT32(offset, entry->verificationCount, blob);
2247           Trspi_LoadBlob_UINT32(offset, entry->flags, blob);
2248 }
2249 
2250 void
Trspi_UnloadBlob_TPM_DELEGATE_LABEL(UINT64 * offset,BYTE * blob,TPM_DELEGATE_LABEL * label)2251 Trspi_UnloadBlob_TPM_DELEGATE_LABEL(UINT64 *offset, BYTE *blob, TPM_DELEGATE_LABEL *label)
2252 {
2253           if (!label) {
2254                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
2255 
2256                     return;
2257           }
2258 
2259           Trspi_UnloadBlob_BYTE(offset, &label->label, blob);
2260 }
2261 
2262 void
Trspi_LoadBlob_TPM_DELEGATE_LABEL(UINT64 * offset,BYTE * blob,TPM_DELEGATE_LABEL * label)2263 Trspi_LoadBlob_TPM_DELEGATE_LABEL(UINT64 *offset, BYTE *blob, TPM_DELEGATE_LABEL *label)
2264 {
2265           Trspi_LoadBlob_BYTE(offset, label->label, blob);
2266 }
2267 
2268 void
Trspi_UnloadBlob_TPM_DELEGATIONS(UINT64 * offset,BYTE * blob,TPM_DELEGATIONS * delegations)2269 Trspi_UnloadBlob_TPM_DELEGATIONS(UINT64 *offset, BYTE *blob, TPM_DELEGATIONS *delegations)
2270 {
2271           if (!delegations) {
2272                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
2273                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2274                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2275                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2276 
2277                     return;
2278           }
2279 
2280           Trspi_UnloadBlob_UINT16(offset, &delegations->tag, blob);
2281           Trspi_UnloadBlob_UINT32(offset, &delegations->delegateType, blob);
2282           Trspi_UnloadBlob_UINT32(offset, &delegations->per1, blob);
2283           Trspi_UnloadBlob_UINT32(offset, &delegations->per2, blob);
2284 }
2285 
2286 void
Trspi_LoadBlob_TPM_DELEGATIONS(UINT64 * offset,BYTE * blob,TPM_DELEGATIONS * delegations)2287 Trspi_LoadBlob_TPM_DELEGATIONS(UINT64 *offset, BYTE *blob, TPM_DELEGATIONS *delegations)
2288 {
2289           Trspi_LoadBlob_UINT16(offset, delegations->tag, blob);
2290           Trspi_LoadBlob_UINT32(offset, delegations->delegateType, blob);
2291           Trspi_LoadBlob_UINT32(offset, delegations->per1, blob);
2292           Trspi_LoadBlob_UINT32(offset, delegations->per2, blob);
2293 }
2294 
2295 TSS_RESULT
Trspi_UnloadBlob_TPM_DELEGATE_PUBLIC(UINT64 * offset,BYTE * blob,TPM_DELEGATE_PUBLIC * pub)2296 Trspi_UnloadBlob_TPM_DELEGATE_PUBLIC(UINT64 *offset, BYTE *blob, TPM_DELEGATE_PUBLIC *pub)
2297 {
2298           TSS_RESULT result;
2299 
2300           if (!pub) {
2301                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
2302                     Trspi_UnloadBlob_TPM_DELEGATE_LABEL(offset, blob, NULL);
2303                     (void)Trspi_UnloadBlob_PCR_INFO_SHORT(offset, blob, NULL);
2304                     Trspi_UnloadBlob_TPM_DELEGATIONS(offset, blob, NULL);
2305                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2306                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2307 
2308                     return TSS_SUCCESS;
2309           }
2310 
2311           Trspi_UnloadBlob_UINT16(offset, &pub->tag, blob);
2312           Trspi_UnloadBlob_TPM_DELEGATE_LABEL(offset, blob, &pub->label);
2313           if ((result = Trspi_UnloadBlob_PCR_INFO_SHORT(offset, blob, &pub->pcrInfo)))
2314                     return result;
2315           Trspi_UnloadBlob_TPM_DELEGATIONS(offset, blob, &pub->permissions);
2316           Trspi_UnloadBlob_UINT32(offset, &pub->familyID, blob);
2317           Trspi_UnloadBlob_UINT32(offset, &pub->verificationCount, blob);
2318 
2319           return TSS_SUCCESS;
2320 }
2321 
2322 void
Trspi_LoadBlob_TPM_DELEGATE_PUBLIC(UINT64 * offset,BYTE * blob,TPM_DELEGATE_PUBLIC * pub)2323 Trspi_LoadBlob_TPM_DELEGATE_PUBLIC(UINT64 *offset, BYTE *blob, TPM_DELEGATE_PUBLIC *pub)
2324 {
2325           Trspi_LoadBlob_UINT16(offset, pub->tag, blob);
2326           Trspi_LoadBlob_TPM_DELEGATE_LABEL(offset, blob, &pub->label);
2327           Trspi_LoadBlob_PCR_INFO_SHORT(offset, blob, &pub->pcrInfo);
2328           Trspi_LoadBlob_TPM_DELEGATIONS(offset, blob, &pub->permissions);
2329           Trspi_LoadBlob_UINT32(offset, pub->familyID, blob);
2330           Trspi_LoadBlob_UINT32(offset, pub->verificationCount, blob);
2331 }
2332 
2333 TSS_RESULT
Trspi_UnloadBlob_TPM_DELEGATE_OWNER_BLOB(UINT64 * offset,BYTE * blob,TPM_DELEGATE_OWNER_BLOB * owner)2334 Trspi_UnloadBlob_TPM_DELEGATE_OWNER_BLOB(UINT64 *offset, BYTE *blob, TPM_DELEGATE_OWNER_BLOB *owner)
2335 {
2336           TSS_RESULT result;
2337 
2338           if (!owner) {
2339                     UINT32 additionalSize, sensitiveSize;
2340 
2341                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
2342                     (void)Trspi_UnloadBlob_TPM_DELEGATE_PUBLIC(offset, blob, NULL);
2343                     Trspi_UnloadBlob_DIGEST(offset, blob, NULL);
2344                     Trspi_UnloadBlob_UINT32(offset, &additionalSize, blob);
2345                     (void)Trspi_UnloadBlob(offset, additionalSize, blob, NULL);
2346                     Trspi_UnloadBlob_UINT32(offset, &sensitiveSize, blob);
2347                     (void)Trspi_UnloadBlob(offset, sensitiveSize, blob, NULL);
2348 
2349                     return TSS_SUCCESS;
2350           }
2351 
2352           Trspi_UnloadBlob_UINT16(offset, &owner->tag, blob);
2353           if ((result = Trspi_UnloadBlob_TPM_DELEGATE_PUBLIC(offset, blob, &owner->pub)))
2354                     return result;
2355           Trspi_UnloadBlob_DIGEST(offset, blob, &owner->integrityDigest);
2356           Trspi_UnloadBlob_UINT32(offset, &owner->additionalSize, blob);
2357           if (owner->additionalSize > 0) {
2358                     owner->additionalArea = malloc(owner->additionalSize);
2359                     if (owner->additionalArea == NULL) {
2360                               LogError("malloc of %u bytes failed.", owner->additionalSize);
2361                               free(owner->pub.pcrInfo.pcrSelection.pcrSelect);
2362                               return TSPERR(TSS_E_OUTOFMEMORY);
2363                     }
2364                     Trspi_UnloadBlob(offset, owner->additionalSize, blob, owner->additionalArea);
2365           }
2366           Trspi_UnloadBlob_UINT32(offset, &owner->sensitiveSize, blob);
2367           if (owner->sensitiveSize > 0) {
2368                     owner->sensitiveArea = malloc(owner->sensitiveSize);
2369                     if (owner->sensitiveArea == NULL) {
2370                               LogError("malloc of %u bytes failed.", owner->sensitiveSize);
2371                               free(owner->pub.pcrInfo.pcrSelection.pcrSelect);
2372                               free(owner->additionalArea);
2373                               return TSPERR(TSS_E_OUTOFMEMORY);
2374                     }
2375                     Trspi_UnloadBlob(offset, owner->sensitiveSize, blob, owner->sensitiveArea);
2376           }
2377 
2378           return TSS_SUCCESS;
2379 }
2380 
2381 void
Trspi_LoadBlob_TPM_DELEGATE_OWNER_BLOB(UINT64 * offset,BYTE * blob,TPM_DELEGATE_OWNER_BLOB * owner)2382 Trspi_LoadBlob_TPM_DELEGATE_OWNER_BLOB(UINT64 *offset, BYTE *blob, TPM_DELEGATE_OWNER_BLOB *owner)
2383 {
2384           Trspi_LoadBlob_UINT16(offset, owner->tag, blob);
2385           Trspi_LoadBlob_TPM_DELEGATE_PUBLIC(offset, blob, &owner->pub);
2386           Trspi_LoadBlob_DIGEST(offset, blob, &owner->integrityDigest);
2387           Trspi_LoadBlob_UINT32(offset, owner->additionalSize, blob);
2388           Trspi_LoadBlob(offset, owner->additionalSize, blob, owner->additionalArea);
2389           Trspi_LoadBlob_UINT32(offset, owner->sensitiveSize, blob);
2390           Trspi_LoadBlob(offset, owner->sensitiveSize, blob, owner->sensitiveArea);
2391 }
2392 
2393 TSS_RESULT
Trspi_UnloadBlob_TPM_DELEGATE_KEY_BLOB(UINT64 * offset,BYTE * blob,TPM_DELEGATE_KEY_BLOB * key)2394 Trspi_UnloadBlob_TPM_DELEGATE_KEY_BLOB(UINT64 *offset, BYTE *blob, TPM_DELEGATE_KEY_BLOB *key)
2395 {
2396           TSS_RESULT result;
2397 
2398           if (!key) {
2399                     UINT32 additionalSize, sensitiveSize;
2400 
2401                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
2402                     (void)Trspi_UnloadBlob_TPM_DELEGATE_PUBLIC(offset, blob, NULL);
2403                     Trspi_UnloadBlob_DIGEST(offset, blob, NULL);
2404                     Trspi_UnloadBlob_DIGEST(offset, blob, NULL);
2405                     Trspi_UnloadBlob_UINT32(offset, &additionalSize, blob);
2406                     (void)Trspi_UnloadBlob(offset, additionalSize, blob, NULL);
2407                     Trspi_UnloadBlob_UINT32(offset, &sensitiveSize, blob);
2408                     (void)Trspi_UnloadBlob(offset, sensitiveSize, blob, NULL);
2409 
2410                     return TSS_SUCCESS;
2411           }
2412 
2413           Trspi_UnloadBlob_UINT16(offset, &key->tag, blob);
2414           if ((result = Trspi_UnloadBlob_TPM_DELEGATE_PUBLIC(offset, blob, &key->pub)))
2415                     return result;
2416           Trspi_UnloadBlob_DIGEST(offset, blob, &key->integrityDigest);
2417           Trspi_UnloadBlob_DIGEST(offset, blob, &key->pubKeyDigest);
2418           Trspi_UnloadBlob_UINT32(offset, &key->additionalSize, blob);
2419           if (key->additionalSize > 0) {
2420                     key->additionalArea = malloc(key->additionalSize);
2421                     if (key->additionalArea == NULL) {
2422                               LogError("malloc of %u bytes failed.", key->additionalSize);
2423                               free(key->pub.pcrInfo.pcrSelection.pcrSelect);
2424                               return TSPERR(TSS_E_OUTOFMEMORY);
2425                     }
2426                     Trspi_UnloadBlob(offset, key->additionalSize, blob, key->additionalArea);
2427           }
2428           Trspi_UnloadBlob_UINT32(offset, &key->sensitiveSize, blob);
2429           if (key->sensitiveSize > 0) {
2430                     key->sensitiveArea = malloc(key->sensitiveSize);
2431                     if (key->sensitiveArea == NULL) {
2432                               LogError("malloc of %u bytes failed.", key->sensitiveSize);
2433                               free(key->pub.pcrInfo.pcrSelection.pcrSelect);
2434                               free(key->additionalArea);
2435                               return TSPERR(TSS_E_OUTOFMEMORY);
2436                     }
2437                     Trspi_UnloadBlob(offset, key->sensitiveSize, blob, key->sensitiveArea);
2438           }
2439 
2440           return TSS_SUCCESS;
2441 }
2442 
2443 void
Trspi_LoadBlob_TPM_DELEGATE_KEY_BLOB(UINT64 * offset,BYTE * blob,TPM_DELEGATE_KEY_BLOB * key)2444 Trspi_LoadBlob_TPM_DELEGATE_KEY_BLOB(UINT64 *offset, BYTE *blob, TPM_DELEGATE_KEY_BLOB *key)
2445 {
2446           Trspi_LoadBlob_UINT16(offset, key->tag, blob);
2447           Trspi_LoadBlob_TPM_DELEGATE_PUBLIC(offset, blob, &key->pub);
2448           Trspi_LoadBlob_DIGEST(offset, blob, &key->integrityDigest);
2449           Trspi_LoadBlob_DIGEST(offset, blob, &key->pubKeyDigest);
2450           Trspi_LoadBlob_UINT32(offset, key->additionalSize, blob);
2451           Trspi_LoadBlob(offset, key->additionalSize, blob, key->additionalArea);
2452           Trspi_LoadBlob_UINT32(offset, key->sensitiveSize, blob);
2453           Trspi_LoadBlob(offset, key->sensitiveSize, blob, key->sensitiveArea);
2454 }
2455 
2456 void
Trspi_UnloadBlob_TSS_FAMILY_TABLE_ENTRY(UINT64 * offset,BYTE * blob,TSS_FAMILY_TABLE_ENTRY * entry)2457 Trspi_UnloadBlob_TSS_FAMILY_TABLE_ENTRY(UINT64 *offset, BYTE *blob, TSS_FAMILY_TABLE_ENTRY *entry)
2458 {
2459           if (!entry) {
2460                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2461                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
2462                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2463                     Trspi_UnloadBlob_BOOL(offset, NULL, blob);
2464                     Trspi_UnloadBlob_BOOL(offset, NULL, blob);
2465 
2466                     return;
2467           }
2468 
2469           Trspi_UnloadBlob_UINT32(offset, &entry->familyID, blob);
2470           Trspi_UnloadBlob_BYTE(offset, &entry->label, blob);
2471           Trspi_UnloadBlob_UINT32(offset, &entry->verificationCount, blob);
2472           Trspi_UnloadBlob_BOOL(offset, &entry->enabled, blob);
2473           Trspi_UnloadBlob_BOOL(offset, &entry->locked, blob);
2474 }
2475 
2476 void
Trspi_LoadBlob_TSS_FAMILY_TABLE_ENTRY(UINT64 * offset,BYTE * blob,TSS_FAMILY_TABLE_ENTRY * entry)2477 Trspi_LoadBlob_TSS_FAMILY_TABLE_ENTRY(UINT64 *offset, BYTE *blob, TSS_FAMILY_TABLE_ENTRY *entry)
2478 {
2479           Trspi_LoadBlob_UINT32(offset, entry->familyID, blob);
2480           Trspi_LoadBlob_BYTE(offset, entry->label, blob);
2481           Trspi_LoadBlob_UINT32(offset, entry->verificationCount, blob);
2482           Trspi_LoadBlob_BOOL(offset, entry->enabled, blob);
2483           Trspi_LoadBlob_BOOL(offset, entry->locked, blob);
2484 }
2485 
2486 TSS_RESULT
Trspi_UnloadBlob_TSS_PCR_INFO_SHORT(UINT64 * offset,BYTE * blob,TSS_PCR_INFO_SHORT * pcr)2487 Trspi_UnloadBlob_TSS_PCR_INFO_SHORT(UINT64 *offset, BYTE *blob, TSS_PCR_INFO_SHORT *pcr)
2488 {
2489           if (!pcr) {
2490                     UINT32 sizeOfSelect, sizeOfDigestAtRelease;
2491 
2492                     Trspi_UnloadBlob_UINT32(offset, &sizeOfSelect, blob);
2493                     (void)Trspi_UnloadBlob(offset, sizeOfSelect, blob, NULL);
2494                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
2495                     Trspi_UnloadBlob_UINT32(offset, &sizeOfDigestAtRelease, blob);
2496                     (void)Trspi_UnloadBlob(offset, sizeOfDigestAtRelease, blob, NULL);
2497 
2498                     return TSS_SUCCESS;
2499           }
2500 
2501           Trspi_UnloadBlob_UINT32(offset, &pcr->sizeOfSelect, blob);
2502           if (pcr->sizeOfSelect > 0) {
2503                     pcr->selection = malloc(pcr->sizeOfSelect);
2504                     if (pcr->selection == NULL) {
2505                               LogError("malloc of %u bytes failed.", pcr->sizeOfSelect);
2506                               return TSPERR(TSS_E_OUTOFMEMORY);
2507                     }
2508                     Trspi_UnloadBlob(offset, pcr->sizeOfSelect, blob, pcr->selection);
2509           } else {
2510                     pcr->selection = NULL;
2511           }
2512           Trspi_UnloadBlob_BYTE(offset, &pcr->localityAtRelease, blob);
2513           Trspi_UnloadBlob_UINT32(offset, &pcr->sizeOfDigestAtRelease, blob);
2514           if (pcr->sizeOfDigestAtRelease > 0) {
2515                     pcr->digestAtRelease = malloc(pcr->sizeOfDigestAtRelease);
2516                     if (pcr->digestAtRelease == NULL) {
2517                               LogError("malloc of %u bytes failed.", pcr->sizeOfDigestAtRelease);
2518                               free(pcr->selection);
2519                               return TSPERR(TSS_E_OUTOFMEMORY);
2520                     }
2521                     Trspi_UnloadBlob(offset, pcr->sizeOfDigestAtRelease, blob, pcr->digestAtRelease);
2522           } else {
2523                     pcr->digestAtRelease = NULL;
2524           }
2525 
2526           return TSS_SUCCESS;
2527 }
2528 
2529 void
Trspi_LoadBlob_TSS_PCR_INFO_SHORT(UINT64 * offset,BYTE * blob,TSS_PCR_INFO_SHORT * pcr)2530 Trspi_LoadBlob_TSS_PCR_INFO_SHORT(UINT64 *offset, BYTE *blob, TSS_PCR_INFO_SHORT *pcr)
2531 {
2532           Trspi_LoadBlob_UINT32(offset, pcr->sizeOfSelect, blob);
2533           Trspi_LoadBlob(offset, pcr->sizeOfSelect, blob, pcr->selection);
2534           Trspi_LoadBlob_BYTE(offset, pcr->localityAtRelease, blob);
2535           Trspi_LoadBlob_UINT32(offset, pcr->sizeOfDigestAtRelease, blob);
2536           Trspi_LoadBlob(offset, pcr->sizeOfDigestAtRelease, blob, pcr->digestAtRelease);
2537 }
2538 
2539 TSS_RESULT
Trspi_UnloadBlob_TSS_DELEGATION_TABLE_ENTRY(UINT64 * offset,BYTE * blob,TSS_DELEGATION_TABLE_ENTRY * entry)2540 Trspi_UnloadBlob_TSS_DELEGATION_TABLE_ENTRY(UINT64 *offset, BYTE *blob,
2541                                                       TSS_DELEGATION_TABLE_ENTRY *entry)
2542 {
2543           TSS_RESULT result;
2544 
2545           if (!entry) {
2546                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2547                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
2548                     (void)Trspi_UnloadBlob_TSS_PCR_INFO_SHORT(offset, blob, NULL);
2549                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2550                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2551                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2552                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2553 
2554                     return TSS_SUCCESS;
2555           }
2556 
2557           Trspi_UnloadBlob_UINT32(offset, &entry->tableIndex, blob);
2558           Trspi_UnloadBlob_BYTE(offset, &entry->label, blob);
2559           if ((result = Trspi_UnloadBlob_TSS_PCR_INFO_SHORT(offset, blob, &entry->pcrInfo)))
2560                     return result;
2561           Trspi_UnloadBlob_UINT32(offset, &entry->per1, blob);
2562           Trspi_UnloadBlob_UINT32(offset, &entry->per2, blob);
2563           Trspi_UnloadBlob_UINT32(offset, &entry->familyID, blob);
2564           Trspi_UnloadBlob_UINT32(offset, &entry->verificationCount, blob);
2565 
2566           return TSS_SUCCESS;
2567 }
2568 
2569 void
Trspi_LoadBlob_TSS_DELEGATION_TABLE_ENTRY(UINT64 * offset,BYTE * blob,TSS_DELEGATION_TABLE_ENTRY * entry)2570 Trspi_LoadBlob_TSS_DELEGATION_TABLE_ENTRY(UINT64 *offset, BYTE *blob,
2571                                                     TSS_DELEGATION_TABLE_ENTRY *entry)
2572 {
2573           Trspi_LoadBlob_UINT32(offset, entry->tableIndex, blob);
2574           Trspi_LoadBlob_BYTE(offset, entry->label, blob);
2575           Trspi_LoadBlob_TSS_PCR_INFO_SHORT(offset, blob, &entry->pcrInfo);
2576           Trspi_LoadBlob_UINT32(offset, entry->per1, blob);
2577           Trspi_LoadBlob_UINT32(offset, entry->per2, blob);
2578           Trspi_LoadBlob_UINT32(offset, entry->familyID, blob);
2579           Trspi_LoadBlob_UINT32(offset, entry->verificationCount, blob);
2580 }
2581 
2582 TSS_RESULT
Trspi_UnloadBlob_PCR_COMPOSITE(UINT64 * offset,BYTE * blob,TCPA_PCR_COMPOSITE * out)2583 Trspi_UnloadBlob_PCR_COMPOSITE(UINT64 *offset, BYTE *blob, TCPA_PCR_COMPOSITE *out)
2584 {
2585           TSS_RESULT result;
2586 
2587           if (!out) {
2588                     UINT32 valueSize;
2589 
2590                     Trspi_UnloadBlob_PCR_SELECTION(offset, blob, NULL);
2591                     Trspi_UnloadBlob_UINT32(offset, &valueSize, blob);
2592                     Trspi_UnloadBlob(offset, valueSize, blob, NULL);
2593 
2594                     return TSS_SUCCESS;
2595           }
2596 
2597           if ((result = Trspi_UnloadBlob_PCR_SELECTION(offset, blob, &out->select)))
2598                     return result;
2599 
2600           Trspi_UnloadBlob_UINT32(offset, &out->valueSize, blob);
2601           out->pcrValue = malloc(out->valueSize);
2602           if (out->pcrValue == NULL) {
2603                     LogError("malloc of %u bytes failed.", out->valueSize);
2604                     return TSPERR(TSS_E_OUTOFMEMORY);
2605           }
2606           Trspi_UnloadBlob(offset, out->valueSize, blob, (BYTE *)out->pcrValue);
2607 
2608           return TSS_SUCCESS;
2609 }
2610 
2611 TSS_RESULT
Trspi_UnloadBlob_MIGRATIONKEYAUTH(UINT64 * offset,BYTE * blob,TPM_MIGRATIONKEYAUTH * migAuth)2612 Trspi_UnloadBlob_MIGRATIONKEYAUTH(UINT64 *offset, BYTE *blob, TPM_MIGRATIONKEYAUTH *migAuth)
2613 {
2614           TSS_RESULT result;
2615 
2616           if (!migAuth) {
2617                     (void)Trspi_UnloadBlob_PUBKEY(offset, blob, NULL);
2618                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
2619                     Trspi_UnloadBlob_DIGEST(offset, blob, NULL);
2620 
2621                     return TSS_SUCCESS;
2622           }
2623 
2624           if ((result = Trspi_UnloadBlob_PUBKEY(offset, blob, &migAuth->migrationKey)))
2625                     return result;
2626 
2627           Trspi_UnloadBlob_UINT16(offset, &migAuth->migrationScheme, blob);
2628           Trspi_UnloadBlob_DIGEST(offset, blob, &migAuth->digest);
2629 
2630           return TSS_SUCCESS;
2631 }
2632 
2633 void
Trspi_LoadBlob_MIGRATIONKEYAUTH(UINT64 * offset,BYTE * blob,TPM_MIGRATIONKEYAUTH * migAuth)2634 Trspi_LoadBlob_MIGRATIONKEYAUTH(UINT64 *offset, BYTE *blob, TPM_MIGRATIONKEYAUTH *migAuth)
2635 {
2636           Trspi_LoadBlob_PUBKEY(offset, blob, &migAuth->migrationKey);
2637           Trspi_LoadBlob_UINT16(offset, migAuth->migrationScheme, blob);
2638           Trspi_LoadBlob_DIGEST(offset, blob, &migAuth->digest);
2639 }
2640 
2641 void
Trspi_LoadBlob_MSA_COMPOSITE(UINT64 * offset,BYTE * blob,TPM_MSA_COMPOSITE * msaComp)2642 Trspi_LoadBlob_MSA_COMPOSITE(UINT64 *offset, BYTE *blob, TPM_MSA_COMPOSITE *msaComp)
2643 {
2644           UINT32 i;
2645 
2646           Trspi_LoadBlob_UINT32(offset, msaComp->MSAlist, blob);
2647           for (i = 0; i < msaComp->MSAlist; i++)
2648                     Trspi_LoadBlob_DIGEST(offset, blob, &msaComp->migAuthDigest[i]);
2649 }
2650 
2651 void
Trspi_LoadBlob_CMK_AUTH(UINT64 * offset,BYTE * blob,TPM_CMK_AUTH * cmkAuth)2652 Trspi_LoadBlob_CMK_AUTH(UINT64 *offset, BYTE *blob, TPM_CMK_AUTH *cmkAuth)
2653 {
2654           Trspi_LoadBlob_DIGEST(offset, blob, &cmkAuth->migrationAuthorityDigest);
2655           Trspi_LoadBlob_DIGEST(offset, blob, &cmkAuth->destinationKeyDigest);
2656           Trspi_LoadBlob_DIGEST(offset, blob, &cmkAuth->sourceKeyDigest);
2657 }
2658 
2659 TSS_RESULT
Trspi_Hash_MSA_COMPOSITE(Trspi_HashCtx * c,TPM_MSA_COMPOSITE * m)2660 Trspi_Hash_MSA_COMPOSITE(Trspi_HashCtx *c, TPM_MSA_COMPOSITE *m)
2661 {
2662           UINT32 i;
2663           TPM_DIGEST *digest;
2664           TSS_RESULT result;
2665 
2666           result = Trspi_Hash_UINT32(c, m->MSAlist);
2667           digest = m->migAuthDigest;
2668           for (i = 0; i < m->MSAlist; i++) {
2669                     result |= Trspi_Hash_DIGEST(c, digest->digest);
2670                     digest++;
2671           }
2672 
2673           return result;
2674 }
2675 
2676 TSS_RESULT
Trspi_UnloadBlob_TSS_PLATFORM_CLASS(UINT64 * offset,BYTE * blob,TSS_PLATFORM_CLASS * platClass)2677 Trspi_UnloadBlob_TSS_PLATFORM_CLASS(UINT64 *offset, BYTE *blob, TSS_PLATFORM_CLASS *platClass)
2678 {
2679           if (!platClass){
2680                     UINT32 classURISize;
2681 
2682                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2683                     Trspi_UnloadBlob_UINT32(offset, &classURISize, blob);
2684                     Trspi_UnloadBlob(offset, classURISize, blob, NULL);
2685 
2686                     return TSS_SUCCESS;
2687           }
2688           Trspi_UnloadBlob_UINT32(offset, &platClass->platformClassSimpleIdentifier, blob);
2689           Trspi_UnloadBlob_UINT32(offset, &platClass->platformClassURISize, blob);
2690 
2691           platClass->pPlatformClassURI = malloc(platClass->platformClassURISize);
2692           if (platClass->pPlatformClassURI == NULL) {
2693                     LogError("malloc of %u bytes failed.", platClass->platformClassURISize);
2694                     return TSPERR(TSS_E_OUTOFMEMORY);
2695           }
2696           Trspi_UnloadBlob(offset, platClass->platformClassURISize, blob,
2697                                (BYTE *)platClass->pPlatformClassURI);
2698 
2699           return TSS_SUCCESS;
2700 }
2701 
2702 void
Trspi_LoadBlob_CAP_VERSION_INFO(UINT64 * offset,BYTE * blob,TPM_CAP_VERSION_INFO * v)2703 Trspi_LoadBlob_CAP_VERSION_INFO(UINT64 *offset, BYTE *blob, TPM_CAP_VERSION_INFO *v)
2704 {
2705           Trspi_LoadBlob_UINT16(offset, v->tag, blob);
2706           Trspi_LoadBlob_TCPA_VERSION(offset, blob, *(TCPA_VERSION *)(&v->version));
2707           Trspi_LoadBlob_UINT16(offset, v->specLevel, blob);
2708           Trspi_LoadBlob_BYTE(offset, v->errataRev, blob);
2709           Trspi_LoadBlob(offset, sizeof(v->tpmVendorID), blob, v->tpmVendorID);
2710           Trspi_LoadBlob_UINT16(offset, v->vendorSpecificSize, blob);
2711           Trspi_LoadBlob(offset, v->vendorSpecificSize, blob, v->vendorSpecific);
2712 }
2713 
2714 TSS_RESULT
Trspi_UnloadBlob_CAP_VERSION_INFO(UINT64 * offset,BYTE * blob,TPM_CAP_VERSION_INFO * v)2715 Trspi_UnloadBlob_CAP_VERSION_INFO(UINT64 *offset, BYTE *blob, TPM_CAP_VERSION_INFO *v)
2716 {
2717           if (!v) {
2718                     UINT16 vendorSpecificSize;
2719 
2720                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
2721                     Trspi_UnloadBlob_VERSION(offset, blob, NULL);
2722                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
2723                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
2724                     Trspi_UnloadBlob(offset, 4, blob, NULL);
2725                     Trspi_UnloadBlob_UINT16(offset, &vendorSpecificSize, blob);
2726 
2727                     (*offset) += vendorSpecificSize;
2728 
2729                     return TSS_SUCCESS;
2730           }
2731 
2732           Trspi_UnloadBlob_UINT16(offset, &v->tag, blob);
2733           Trspi_UnloadBlob_VERSION(offset, blob, (TCPA_VERSION *)&v->version);
2734           Trspi_UnloadBlob_UINT16(offset, &v->specLevel, blob);
2735           Trspi_UnloadBlob_BYTE(offset, &v->errataRev, blob);
2736           Trspi_UnloadBlob(offset, sizeof(v->tpmVendorID), blob, v->tpmVendorID);
2737           Trspi_UnloadBlob_UINT16(offset, &v->vendorSpecificSize, blob);
2738 
2739           if (v->vendorSpecificSize > 0) {
2740                     if ((v->vendorSpecific = malloc(v->vendorSpecificSize)) == NULL) {
2741                               LogError("malloc of %u bytes failed.", v->vendorSpecificSize);
2742                               return TSPERR(TSS_E_OUTOFMEMORY);
2743                     }
2744 
2745                     Trspi_UnloadBlob(offset, v->vendorSpecificSize, blob, v->vendorSpecific);
2746           } else {
2747                     v->vendorSpecific = NULL;
2748           }
2749 
2750           return TSS_SUCCESS;
2751 }
2752 
2753 TSS_RESULT
Trspi_UnloadBlob_NV_INDEX(UINT64 * offset,BYTE * blob,TPM_NV_INDEX * v)2754 Trspi_UnloadBlob_NV_INDEX(UINT64 *offset, BYTE *blob, TPM_NV_INDEX *v)
2755 {
2756           if (!v) {
2757                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2758 
2759                     return TSS_SUCCESS;
2760           }
2761 
2762           Trspi_UnloadBlob_UINT32(offset, v, blob);
2763 
2764           return TSS_SUCCESS;
2765 }
2766 
2767 TSS_RESULT
Trspi_UnloadBlob_NV_ATTRIBUTES(UINT64 * offset,BYTE * blob,TPM_NV_ATTRIBUTES * v)2768 Trspi_UnloadBlob_NV_ATTRIBUTES(UINT64 *offset, BYTE *blob, TPM_NV_ATTRIBUTES *v)
2769 {
2770           if (!v) {
2771                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
2772                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2773 
2774                     return TSS_SUCCESS;
2775           }
2776 
2777           Trspi_UnloadBlob_UINT16(offset, &v->tag, blob);
2778           Trspi_UnloadBlob_UINT32(offset, &v->attributes, blob);
2779 
2780           return TSS_SUCCESS;
2781 }
2782 
2783 TSS_RESULT
Trspi_UnloadBlob_NV_DATA_PUBLIC(UINT64 * offset,BYTE * blob,TPM_NV_DATA_PUBLIC * v)2784 Trspi_UnloadBlob_NV_DATA_PUBLIC(UINT64 *offset, BYTE *blob, TPM_NV_DATA_PUBLIC *v)
2785 {
2786           if (!v) {
2787                     Trspi_UnloadBlob_UINT16(offset, NULL, blob);
2788                     Trspi_UnloadBlob_NV_INDEX(offset, blob, NULL);
2789                     Trspi_UnloadBlob_PCR_INFO_SHORT(offset, blob, NULL);
2790                     Trspi_UnloadBlob_PCR_INFO_SHORT(offset, blob, NULL);
2791                     Trspi_UnloadBlob_NV_ATTRIBUTES(offset, blob, NULL);
2792                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
2793                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
2794                     Trspi_UnloadBlob_BYTE(offset, NULL, blob);
2795                     Trspi_UnloadBlob_UINT32(offset, NULL, blob);
2796 
2797                     return TSS_SUCCESS;
2798           }
2799 
2800           Trspi_UnloadBlob_UINT16(offset, &v->tag, blob);
2801           Trspi_UnloadBlob_NV_INDEX(offset, blob, &v->nvIndex);
2802           Trspi_UnloadBlob_PCR_INFO_SHORT(offset, blob, &v->pcrInfoRead);
2803           Trspi_UnloadBlob_PCR_INFO_SHORT(offset, blob, &v->pcrInfoWrite);
2804           Trspi_UnloadBlob_NV_ATTRIBUTES(offset, blob, &v->permission);
2805           Trspi_UnloadBlob_BYTE(offset, &v->bReadSTClear, blob);
2806           Trspi_UnloadBlob_BYTE(offset, &v->bWriteSTClear, blob);
2807           Trspi_UnloadBlob_BYTE(offset, &v->bWriteDefine, blob);
2808           Trspi_UnloadBlob_UINT32(offset, &v->dataSize, blob);
2809 
2810           return TSS_SUCCESS;
2811 }
2812