xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tspi/rpc/tcstp/rpc_evlog.c (revision 2d5f7628c5531eb583b9313ac2fd1cf8582b4479)
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-2006
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 "capabilities.h"
21 #include "tsplog.h"
22 #include "hosttable.h"
23 #include "tcsd_wrap.h"
24 #include "obj.h"
25 #include "rpc_tcstp_tsp.h"
26 
27 
28 TSS_RESULT
RPC_LogPcrEvent_TP(struct host_table_entry * hte,TSS_PCR_EVENT Event,UINT32 * pNumber)29 RPC_LogPcrEvent_TP(struct host_table_entry *hte,
30 			       TSS_PCR_EVENT Event,	/*  in  */
31 			       UINT32 * pNumber	/*  out */
32     ) {
33 	TSS_RESULT result;
34 
35 	initData(&hte->comm, 2);
36 	hte->comm.hdr.u.ordinal = TCSD_ORD_LOGPCREVENT;
37 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
38 
39 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
40 		return TSPERR(TSS_E_INTERNAL_ERROR);
41 
42 	if (setData(TCSD_PACKET_TYPE_PCR_EVENT, 1, &Event, 0, &hte->comm))
43 		return TSPERR(TSS_E_INTERNAL_ERROR);
44 
45 	result = sendTCSDPacket(hte);
46 
47 	if (result == TSS_SUCCESS)
48 		result = hte->comm.hdr.u.result;
49 
50 	if (result == TSS_SUCCESS) {
51 		if (getData(TCSD_PACKET_TYPE_UINT32, 0, pNumber, 0, &hte->comm))
52 			result = TSPERR(TSS_E_INTERNAL_ERROR);
53 	}
54 
55 	return result;
56 }
57 
58 TSS_RESULT
RPC_GetPcrEvent_TP(struct host_table_entry * hte,UINT32 PcrIndex,UINT32 * pNumber,TSS_PCR_EVENT ** ppEvent)59 RPC_GetPcrEvent_TP(struct host_table_entry *hte,
60 			       UINT32 PcrIndex,	/* in */
61 			       UINT32 * pNumber,	/* in, out */
62 			       TSS_PCR_EVENT ** ppEvent	/* out */
63     ) {
64 	TSS_RESULT result;
65 	BYTE lengthOnly = (ppEvent == NULL) ? TRUE : FALSE;
66 
67 	initData(&hte->comm, 4);
68 	hte->comm.hdr.u.ordinal = TCSD_ORD_GETPCREVENT;
69 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
70 
71 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
72 		return TSPERR(TSS_E_INTERNAL_ERROR);
73 
74 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &PcrIndex, 0, &hte->comm))
75 		return TSPERR(TSS_E_INTERNAL_ERROR);
76 
77 	if (setData(TCSD_PACKET_TYPE_UINT32, 2, pNumber, 0, &hte->comm))
78 		return TSPERR(TSS_E_INTERNAL_ERROR);
79 
80 	if (setData(TCSD_PACKET_TYPE_BYTE, 3, &lengthOnly, 0, &hte->comm))
81 		return TSPERR(TSS_E_INTERNAL_ERROR);
82 
83 	result = sendTCSDPacket(hte);
84 
85 	if (result == TSS_SUCCESS)
86 		result = hte->comm.hdr.u.result;
87 
88 	if (result == TSS_SUCCESS) {
89 		if (getData(TCSD_PACKET_TYPE_UINT32, 0, pNumber, 0, &hte->comm)) {
90 			result = TSPERR(TSS_E_INTERNAL_ERROR);
91 			goto done;
92 		}
93 
94 		if (ppEvent) {
95 			*ppEvent = malloc(sizeof(TSS_PCR_EVENT));
96 			if (*ppEvent == NULL) {
97 				LogError("malloc of %zd bytes failed.",
98 					 sizeof(TSS_PCR_EVENT));
99 				result = TSPERR(TSS_E_OUTOFMEMORY);
100 				goto done;
101 			}
102 
103 			if (getData(TCSD_PACKET_TYPE_PCR_EVENT, 1, *ppEvent, 0, &hte->comm)) {
104 				free(*ppEvent);
105 				*ppEvent = NULL;
106 				result = TSPERR(TSS_E_INTERNAL_ERROR);
107 			}
108 		}
109 	}
110 
111 done:
112 	return result;
113 }
114 
115 TSS_RESULT
RPC_GetPcrEventsByPcr_TP(struct host_table_entry * hte,UINT32 PcrIndex,UINT32 FirstEvent,UINT32 * pEventCount,TSS_PCR_EVENT ** ppEvents)116 RPC_GetPcrEventsByPcr_TP(struct host_table_entry *hte,
117 				     UINT32 PcrIndex,	/* in */
118 				     UINT32 FirstEvent,	/* in */
119 				     UINT32 * pEventCount,	/* in, out */
120 				     TSS_PCR_EVENT ** ppEvents	/* out */
121     ) {
122 	TSS_RESULT result;
123 	UINT32 i, j;
124 
125 	initData(&hte->comm, 4);
126 	hte->comm.hdr.u.ordinal = TCSD_ORD_GETPCREVENTBYPCR;
127 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
128 
129 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
130 		return TSPERR(TSS_E_INTERNAL_ERROR);
131 
132 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &PcrIndex, 0, &hte->comm))
133 		return TSPERR(TSS_E_INTERNAL_ERROR);
134 
135 	if (setData(TCSD_PACKET_TYPE_UINT32, 2, &FirstEvent, 0, &hte->comm))
136 		return TSPERR(TSS_E_INTERNAL_ERROR);
137 
138 	if (setData(TCSD_PACKET_TYPE_UINT32, 3, pEventCount, 0, &hte->comm))
139 		return TSPERR(TSS_E_INTERNAL_ERROR);
140 
141 	result = sendTCSDPacket(hte);
142 
143 	if (result == TSS_SUCCESS)
144 		result = hte->comm.hdr.u.result;
145 
146 	if (result == TSS_SUCCESS) {
147 		if (getData(TCSD_PACKET_TYPE_UINT32, 0, pEventCount, 0, &hte->comm)) {
148 			result = TSPERR(TSS_E_INTERNAL_ERROR);
149 			goto done;
150 		}
151 
152 		if (*pEventCount > 0) {
153 			*ppEvents = calloc_tspi(hte->tspContext,
154 						sizeof(TSS_PCR_EVENT) * (*pEventCount));
155 			if (*ppEvents == NULL) {
156 				LogError("malloc of %zd bytes failed.", sizeof(TSS_PCR_EVENT) * (*pEventCount));
157 				result = TSPERR(TSS_E_OUTOFMEMORY);
158 				goto done;
159 			}
160 
161 			i = 1;
162 			for (j = 0; j < (*pEventCount); j++) {
163 				if (getData(TCSD_PACKET_TYPE_PCR_EVENT, i++, &((*ppEvents)[j]), 0, &hte->comm)) {
164 					free(*ppEvents);
165 					*ppEvents = NULL;
166 					result = TSPERR(TSS_E_INTERNAL_ERROR);
167 					goto done;
168 				}
169 			}
170 		} else {
171 			*ppEvents = NULL;
172 		}
173 	}
174 
175 done:
176 	return result;
177 }
178 
179 TSS_RESULT
RPC_GetPcrEventLog_TP(struct host_table_entry * hte,UINT32 * pEventCount,TSS_PCR_EVENT ** ppEvents)180 RPC_GetPcrEventLog_TP(struct host_table_entry *hte,
181 				  UINT32 * pEventCount,	/* out */
182 				  TSS_PCR_EVENT ** ppEvents	/* out */
183     ) {
184 	TSS_RESULT result;
185 	int i, j;
186 
187 	initData(&hte->comm, 1);
188 	hte->comm.hdr.u.ordinal = TCSD_ORD_GETPCREVENTLOG;
189 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
190 
191 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
192 		return TSPERR(TSS_E_INTERNAL_ERROR);
193 
194 	result = sendTCSDPacket(hte);
195 
196 	if (result == TSS_SUCCESS)
197 		result = hte->comm.hdr.u.result;
198 
199 	if (result == TSS_SUCCESS) {
200 		if (getData(TCSD_PACKET_TYPE_UINT32, 0, pEventCount, 0, &hte->comm)) {
201 			result = TSPERR(TSS_E_INTERNAL_ERROR);
202 			goto done;
203 		}
204 
205 		if (*pEventCount > 0) {
206 			*ppEvents = calloc_tspi(hte->tspContext,
207 						sizeof(TSS_PCR_EVENT) * (*pEventCount));
208 			if (*ppEvents == NULL) {
209 				LogError("malloc of %zd bytes failed.",
210 					 sizeof(TSS_PCR_EVENT) * (*pEventCount));
211 				result = TSPERR(TSS_E_OUTOFMEMORY);
212 				goto done;
213 			}
214 
215 			i = 1;
216 			for (j = 0; (UINT32)j < (*pEventCount); j++) {
217 				if (getData(TCSD_PACKET_TYPE_PCR_EVENT, i++, &((*ppEvents)[j]), 0, &hte->comm)) {
218 					free(*ppEvents);
219 					*ppEvents = NULL;
220 					result = TSPERR(TSS_E_INTERNAL_ERROR);
221 					goto done;
222 				}
223 			}
224 		} else {
225 			*ppEvents = NULL;
226 		}
227 	}
228 
229 done:
230 	return result;
231 }
232