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. 2007
8  *
9  */
10 
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14 #include <inttypes.h>
15 
16 #include "trousers/tss.h"
17 #include "trousers/trousers.h"
18 #include "trousers_types.h"
19 #include "spi_utils.h"
20 #include "capabilities.h"
21 #include "tsplog.h"
22 #include "obj.h"
23 
24 
25 #ifdef TSS_BUILD_TRANSPORT
26 TSS_RESULT
Transport_Quote2(TSS_HCONTEXT tspContext,TCS_KEY_HANDLE keyHandle,TCPA_NONCE * antiReplay,UINT32 pcrDataSizeIn,BYTE * pcrDataIn,TSS_BOOL addVersion,TPM_AUTH * privAuth,UINT32 * pcrDataSizeOut,BYTE ** pcrDataOut,UINT32 * versionInfoSize,BYTE ** versionInfo,UINT32 * sigSize,BYTE ** sig)27 Transport_Quote2(TSS_HCONTEXT tspContext, /* in */
28 		 TCS_KEY_HANDLE keyHandle, /* in */
29 		 TCPA_NONCE *antiReplay, /* in */
30 		 UINT32 pcrDataSizeIn, /* in */
31 		 BYTE * pcrDataIn, /* in */
32 		 TSS_BOOL addVersion, /* in */
33 		 TPM_AUTH * privAuth, /* in,out */
34 		 UINT32 * pcrDataSizeOut, /* out */
35 		 BYTE ** pcrDataOut, /* out */
36 		 UINT32 * versionInfoSize, /* out */
37 		 BYTE ** versionInfo, /* out */
38 		 UINT32 * sigSize, /* out */
39 		 BYTE ** sig) /* out */
40 {
41 	TSS_RESULT result;
42 	UINT32 handlesLen, dataLen, decLen;
43 	TCS_HANDLE *handles, handle;
44 	BYTE *dec = NULL;
45 	TPM_DIGEST pubKeyHash;
46 	Trspi_HashCtx hashCtx;
47 	UINT64 offset;
48 	BYTE *data;
49 
50 
51 	if ((result = obj_context_transport_init(tspContext)))
52 		return result;
53 
54 	LogDebugFn("Executing in a transport session");
55 
56 	if ((result = obj_tcskey_get_pubkeyhash(keyHandle, pubKeyHash.digest)))
57 		return result;
58 
59 	result = Trspi_HashInit(&hashCtx, TSS_HASH_SHA1);
60 	result |= Trspi_Hash_DIGEST(&hashCtx, pubKeyHash.digest);
61 	if ((result |= Trspi_HashFinal(&hashCtx, pubKeyHash.digest)))
62 		return result;
63 
64 	handlesLen = 1;
65 	handle = keyHandle;
66 	handles = &handle;
67 
68 	dataLen = sizeof(TCPA_NONCE) + pcrDataSizeIn + sizeof(TSS_BOOL);
69 	if ((data = malloc(dataLen)) == NULL) {
70 		LogError("malloc of %u bytes failed", dataLen);
71 		return TSPERR(TSS_E_OUTOFMEMORY);
72 	}
73 
74 	offset = 0;
75 	Trspi_LoadBlob_NONCE(&offset, data, antiReplay);
76 	Trspi_LoadBlob(&offset, pcrDataSizeIn, data, pcrDataIn);
77 	Trspi_LoadBlob_BOOL(&offset, addVersion, data);
78 
79 	if ((result = obj_context_transport_execute(tspContext, TPM_ORD_Quote2, dataLen, data,
80 						    &pubKeyHash, &handlesLen, &handles,
81 						    privAuth, NULL, &decLen, &dec))) {
82 		free(data);
83 		return result;
84 	}
85 	free(data);
86 
87 	offset = 0;
88 	Trspi_UnloadBlob_PCR_INFO_SHORT(&offset, dec, NULL);
89 	*pcrDataSizeOut = offset;
90 
91 	if ((*pcrDataOut = malloc(*pcrDataSizeOut)) == NULL) {
92 		free(dec);
93 		LogError("malloc of %u bytes failed", *pcrDataSizeOut);
94 		*pcrDataSizeOut = 0;
95 		return TSPERR(TSS_E_OUTOFMEMORY);
96 	}
97 
98 	offset = 0;
99 	Trspi_UnloadBlob(&offset, *pcrDataSizeOut, dec, *pcrDataOut);
100 	Trspi_UnloadBlob_UINT32(&offset, versionInfoSize, dec);
101 
102 	if ((*versionInfo = malloc(*versionInfoSize)) == NULL) {
103 		free(*pcrDataOut);
104 		*pcrDataOut = NULL;
105 		*pcrDataSizeOut = 0;
106 		free(dec);
107 		LogError("malloc of %u bytes failed", *versionInfoSize);
108 		*versionInfoSize = 0;
109 		return TSPERR(TSS_E_OUTOFMEMORY);
110 	}
111 	Trspi_UnloadBlob(&offset, *versionInfoSize, dec, *versionInfo);
112 
113 	Trspi_UnloadBlob_UINT32(&offset, sigSize, dec);
114 
115 	if ((*sig = malloc(*sigSize)) == NULL) {
116 		free(*versionInfo);
117 		*versionInfo = NULL;
118 		*versionInfoSize = 0;
119 		free(*pcrDataOut);
120 		*pcrDataOut = NULL;
121 		*pcrDataSizeOut = 0;
122 		free(dec);
123 		LogError("malloc of %u bytes failed", *sigSize);
124 		*sigSize = 0;
125 		return TSPERR(TSS_E_OUTOFMEMORY);
126 	}
127 	Trspi_UnloadBlob(&offset, *sigSize, dec, *sig);
128 	free(dec);
129 
130 	return result;
131 }
132 #endif
133 
134