xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tspi/rpc/tcstp/rpc_audit.c (revision 1023804e3833a0bd94414f2545512128f6502c74)
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 <assert.h>
15 
16 #include "trousers/tss.h"
17 #include "trousers/trousers.h"
18 #include "trousers_types.h"
19 #include "spi_utils.h"
20 #include "tsplog.h"
21 #include "hosttable.h"
22 #include "tcsd_wrap.h"
23 #include "rpc_tcstp_tsp.h"
24 
25 
26 TSS_RESULT
RPC_SetOrdinalAuditStatus_TP(struct host_table_entry * hte,TPM_AUTH * ownerAuth,UINT32 ulOrdinal,TSS_BOOL bAuditState)27 RPC_SetOrdinalAuditStatus_TP(struct host_table_entry *hte,
28 			     TPM_AUTH *ownerAuth,	/* in/out */
29 			     UINT32 ulOrdinal,		/* in */
30 			     TSS_BOOL bAuditState)	/* in */
31 {
32 	TSS_RESULT result;
33 
34 	initData(&hte->comm, 4);
35 	hte->comm.hdr.u.ordinal = TCSD_ORD_SETORDINALAUDITSTATUS;
36 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
37 
38 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
39 		return TSPERR(TSS_E_INTERNAL_ERROR);
40 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &ulOrdinal, 0, &hte->comm))
41 		return TSPERR(TSS_E_INTERNAL_ERROR);
42 	if (setData(TCSD_PACKET_TYPE_BOOL, 2, &bAuditState, 0, &hte->comm))
43 		return TSPERR(TSS_E_INTERNAL_ERROR);
44 	if (setData(TCSD_PACKET_TYPE_AUTH, 3, ownerAuth, 0, &hte->comm))
45 		return TSPERR(TSS_E_INTERNAL_ERROR);
46 
47 	result = sendTCSDPacket(hte);
48 
49 	if (result == TSS_SUCCESS)
50 		result = hte->comm.hdr.u.result;
51 
52 	if (result == TSS_SUCCESS) {
53 		if (getData(TCSD_PACKET_TYPE_AUTH, 0, ownerAuth, 0, &hte->comm))
54 			result = TSPERR(TSS_E_INTERNAL_ERROR);
55 	}
56 
57 	return result;
58 }
59 
60 TSS_RESULT
RPC_GetAuditDigest_TP(struct host_table_entry * hte,UINT32 startOrdinal,TPM_DIGEST * auditDigest,UINT32 * counterValueSize,BYTE ** counterValue,TSS_BOOL * more,UINT32 * ordSize,UINT32 ** ordList)61 RPC_GetAuditDigest_TP(struct host_table_entry *hte,
62 		      UINT32 startOrdinal,		/* in */
63 		      TPM_DIGEST *auditDigest,		/* out */
64 		      UINT32 *counterValueSize,	/* out */
65 		      BYTE **counterValue,		/* out */
66 		      TSS_BOOL *more,			/* out */
67 		      UINT32 *ordSize,			/* out */
68 		      UINT32 **ordList)		/* out */
69 {
70 	TSS_RESULT result;
71 
72 	initData(&hte->comm, 2);
73 	hte->comm.hdr.u.ordinal = TCSD_ORD_GETAUDITDIGEST;
74 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
75 
76 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
77 		return TSPERR(TSS_E_INTERNAL_ERROR);
78 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &startOrdinal, 0, &hte->comm))
79 		return TSPERR(TSS_E_INTERNAL_ERROR);
80 
81 	result = sendTCSDPacket(hte);
82 
83 	if (result == TSS_SUCCESS)
84 		result = hte->comm.hdr.u.result;
85 
86 	if (result == TSS_SUCCESS) {
87 		if (getData(TCSD_PACKET_TYPE_DIGEST, 0, auditDigest, 0, &hte->comm)) {
88 			result = TSPERR(TSS_E_INTERNAL_ERROR);
89 			goto done;
90 		}
91 		if (getData(TCSD_PACKET_TYPE_UINT32, 1, counterValueSize, 0, &hte->comm)) {
92 			result = TSPERR(TSS_E_INTERNAL_ERROR);
93 			goto done;
94 		}
95 		*counterValue = (BYTE *)malloc(*counterValueSize);
96 		if (*counterValue == NULL) {
97 			LogError("malloc of %u bytes failed.", *counterValueSize);
98 			result = TSPERR(TSS_E_OUTOFMEMORY);
99 			goto done;
100 		}
101 		if (getData(TCSD_PACKET_TYPE_PBYTE, 2, *counterValue, *counterValueSize, &hte->comm)) {
102 			free(*counterValue);
103 			*counterValue = NULL;
104 			result = TSPERR(TSS_E_INTERNAL_ERROR);
105 			goto done;
106 		}
107 		if (getData(TCSD_PACKET_TYPE_BOOL, 3, more, 0, &hte->comm)) {
108 			free(*counterValue);
109 			*counterValue = NULL;
110 			result = TSPERR(TSS_E_INTERNAL_ERROR);
111 			goto done;
112 		}
113 		if (getData(TCSD_PACKET_TYPE_UINT32, 4, ordSize, 0, &hte->comm)) {
114 			free(*counterValue);
115 			*counterValue = NULL;
116 			result = TSPERR(TSS_E_INTERNAL_ERROR);
117 			goto done;
118 		}
119 		*ordList = (UINT32 *)malloc(*ordSize * sizeof(UINT32));
120 		if (*ordList == NULL) {
121 			LogError("malloc of %u bytes failed.", *ordSize);
122 			free(*counterValue);
123 			*counterValue = NULL;
124 			result = TSPERR(TSS_E_OUTOFMEMORY);
125 			goto done;
126 		}
127 		if (getData(TCSD_PACKET_TYPE_PBYTE, 5, *ordList, *ordSize * sizeof(UINT32), &hte->comm)) {
128 			free(*counterValue);
129 			*counterValue = NULL;
130 			free(*ordList);
131 			*ordList = NULL;
132 			result = TSPERR(TSS_E_INTERNAL_ERROR);
133 			goto done;
134 		}
135 	}
136 
137 done:
138 	return result;
139 }
140 
141 TSS_RESULT
RPC_GetAuditDigestSigned_TP(struct host_table_entry * hte,TCS_KEY_HANDLE keyHandle,TSS_BOOL closeAudit,TPM_NONCE * antiReplay,TPM_AUTH * privAuth,UINT32 * counterValueSize,BYTE ** counterValue,TPM_DIGEST * auditDigest,TPM_DIGEST * ordinalDigest,UINT32 * sigSize,BYTE ** sig)142 RPC_GetAuditDigestSigned_TP(struct host_table_entry *hte,
143 			    TCS_KEY_HANDLE keyHandle,		/* in */
144 			    TSS_BOOL closeAudit,		/* in */
145 			    TPM_NONCE *antiReplay,		/* in */
146 			    TPM_AUTH *privAuth,		/* in/out */
147 			    UINT32 *counterValueSize,		/* out */
148 			    BYTE **counterValue,		/* out */
149 			    TPM_DIGEST *auditDigest,		/* out */
150 			    TPM_DIGEST *ordinalDigest,		/* out */
151 			    UINT32 *sigSize,			/* out */
152 			    BYTE **sig)			/* out */
153 {
154 	TSS_RESULT result;
155 	TPM_AUTH null_auth;
156 	int i;
157 
158 	initData(&hte->comm, 5);
159 	hte->comm.hdr.u.ordinal = TCSD_ORD_GETAUDITDIGESTSIGNED;
160 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
161 
162 	__tspi_memset(&null_auth, 0, sizeof(TPM_AUTH));
163 
164 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
165 		return TSPERR(TSS_E_INTERNAL_ERROR);
166 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &keyHandle, 0, &hte->comm))
167 		return TSPERR(TSS_E_INTERNAL_ERROR);
168 	if (setData(TCSD_PACKET_TYPE_BOOL, 2, &closeAudit, 0, &hte->comm))
169 		return TSPERR(TSS_E_INTERNAL_ERROR);
170 	if (setData(TCSD_PACKET_TYPE_NONCE, 3, antiReplay, 0, &hte->comm))
171 		return TSPERR(TSS_E_INTERNAL_ERROR);
172 	if (privAuth) {
173 		if (setData(TCSD_PACKET_TYPE_AUTH, 4, privAuth, 0, &hte->comm))
174 			return TSPERR(TSS_E_INTERNAL_ERROR);
175 	}
176 	else {
177 		if (setData(TCSD_PACKET_TYPE_AUTH, 4, &null_auth, 0, &hte->comm))
178 			return TSPERR(TSS_E_INTERNAL_ERROR);
179 	}
180 
181 	result = sendTCSDPacket(hte);
182 
183 	if (result == TSS_SUCCESS)
184 		result = hte->comm.hdr.u.result;
185 
186 	if (result == TSS_SUCCESS) {
187 		i = 0;
188 		if (privAuth) {
189 			if (getData(TCSD_PACKET_TYPE_AUTH, i++, privAuth, 0, &hte->comm)) {
190 				result = TSPERR(TSS_E_INTERNAL_ERROR);
191 				goto done;
192 			}
193 		}
194 		if (getData(TCSD_PACKET_TYPE_UINT32, i++, counterValueSize, 0, &hte->comm)) {
195 			result = TSPERR(TSS_E_INTERNAL_ERROR);
196 			goto done;
197 		}
198 		*counterValue = (BYTE *)malloc(*counterValueSize);
199 		if (*counterValue == NULL) {
200 			LogError("malloc of %u bytes failed.", *counterValueSize);
201 			result = TSPERR(TSS_E_OUTOFMEMORY);
202 			goto done;
203 		}
204 		if (getData(TCSD_PACKET_TYPE_PBYTE, i++, *counterValue, *counterValueSize, &hte->comm)) {
205 			free(*counterValue);
206 			result = TSPERR(TSS_E_INTERNAL_ERROR);
207 			goto done;
208 		}
209 		if (getData(TCSD_PACKET_TYPE_DIGEST, i++, auditDigest, 0, &hte->comm)) {
210 			free(*counterValue);
211 			result = TSPERR(TSS_E_INTERNAL_ERROR);
212 			goto done;
213 		}
214 		if (getData(TCSD_PACKET_TYPE_DIGEST, i++, ordinalDigest, 0, &hte->comm)) {
215 			free(*counterValue);
216 			result = TSPERR(TSS_E_INTERNAL_ERROR);
217 			goto done;
218 		}
219 		if (getData(TCSD_PACKET_TYPE_UINT32, i++, sigSize, 0, &hte->comm)) {
220 			free(*counterValue);
221 			result = TSPERR(TSS_E_INTERNAL_ERROR);
222 			goto done;
223 		}
224 		*sig = (BYTE *)malloc(*sigSize);
225 		if (*sig == NULL) {
226 			LogError("malloc of %u bytes failed.", *sigSize);
227 			free(*counterValue);
228 			result = TSPERR(TSS_E_OUTOFMEMORY);
229 			goto done;
230 		}
231 		if (getData(TCSD_PACKET_TYPE_PBYTE, i++, *sig, *sigSize, &hte->comm)) {
232 			free(*counterValue);
233 			free(*sig);
234 			result = TSPERR(TSS_E_INTERNAL_ERROR);
235 			goto done;
236 		}
237 	}
238 
239 done:
240 	return result;
241 }
242