xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tspi/tspi_pcr_events.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 <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 TSS_RESULT
Tspi_TPM_GetEvent(TSS_HTPM hTPM,UINT32 ulPcrIndex,UINT32 ulEventNumber,TSS_PCR_EVENT * pPcrEvent)26 Tspi_TPM_GetEvent(TSS_HTPM hTPM,		/* in */
27 		  UINT32 ulPcrIndex,		/* in */
28 		  UINT32 ulEventNumber,		/* in */
29 		  TSS_PCR_EVENT * pPcrEvent)	/* out */
30 {
31 	TSS_HCONTEXT tspContext;
32 	TSS_RESULT result;
33 	TSS_PCR_EVENT *event = NULL;
34 
35 	if (pPcrEvent == NULL)
36 		return TSPERR(TSS_E_BAD_PARAMETER);
37 
38 	if ((result = obj_tpm_get_tsp_context(hTPM, &tspContext)))
39 		return result;
40 
41 	if ((result = RPC_GetPcrEvent(tspContext, ulPcrIndex, &ulEventNumber, &event)))
42 		return result;
43 
44 	memcpy(pPcrEvent, event, sizeof(TSS_PCR_EVENT));
45 	free(event);
46 
47 	return TSS_SUCCESS;
48 }
49 
50 TSS_RESULT
Tspi_TPM_GetEvents(TSS_HTPM hTPM,UINT32 ulPcrIndex,UINT32 ulStartNumber,UINT32 * pulEventNumber,TSS_PCR_EVENT ** prgbPcrEvents)51 Tspi_TPM_GetEvents(TSS_HTPM hTPM,			/* in */
52 		   UINT32 ulPcrIndex,			/* in */
53 		   UINT32 ulStartNumber,		/* in */
54 		   UINT32 * pulEventNumber,		/* in, out */
55 		   TSS_PCR_EVENT ** prgbPcrEvents)	/* out */
56 {
57 	TSS_HCONTEXT tspContext;
58 	TSS_RESULT result;
59 	TSS_PCR_EVENT *events = NULL;
60 
61 	if (pulEventNumber == NULL)
62 		return TSPERR(TSS_E_BAD_PARAMETER);
63 
64 	if ((result = obj_tpm_get_tsp_context(hTPM, &tspContext)))
65 		return result;
66 
67 	if (prgbPcrEvents) {
68 		if ((result = RPC_GetPcrEventsByPcr(tspContext, ulPcrIndex, ulStartNumber,
69 						    pulEventNumber, &events)))
70 			return result;
71 
72 		*prgbPcrEvents = events;
73 	} else {
74 		/* if the pointer to receive events is NULL, the app only
75 		 * wants a total number of events for this PCR. */
76 		if ((result = RPC_GetPcrEvent(tspContext, ulPcrIndex, pulEventNumber, NULL)))
77 			return result;
78 	}
79 
80 	return TSS_SUCCESS;
81 }
82 
83 TSS_RESULT
Tspi_TPM_GetEventLog(TSS_HTPM hTPM,UINT32 * pulEventNumber,TSS_PCR_EVENT ** prgbPcrEvents)84 Tspi_TPM_GetEventLog(TSS_HTPM hTPM,			/* in */
85 		     UINT32 * pulEventNumber,		/* out */
86 		     TSS_PCR_EVENT ** prgbPcrEvents)	/* out */
87 {
88 	TSS_HCONTEXT tspContext;
89 	TSS_RESULT result;
90 
91 	if (pulEventNumber == NULL)
92 		return TSPERR(TSS_E_BAD_PARAMETER);
93 
94 	if ((result = obj_tpm_get_tsp_context(hTPM, &tspContext)))
95 		return result;
96 
97 	/* if the pointer to receive events is NULL, the app only wants a
98 	 * total number of events for all PCRs. */
99 	if (prgbPcrEvents == NULL) {
100 		UINT16 numPcrs = get_num_pcrs(tspContext);
101 		UINT32 i, numEvents = 0;
102 
103 		if (numPcrs == 0) {
104 			LogDebugFn("Error querying the TPM for its number of PCRs");
105 			return TSPERR(TSS_E_INTERNAL_ERROR);
106 		}
107 
108 		*pulEventNumber = 0;
109 		for (i = 0; i < numPcrs; i++) {
110 			if ((result = RPC_GetPcrEvent(tspContext, i, &numEvents, NULL)))
111 				return result;
112 
113 			*pulEventNumber += numEvents;
114 		}
115 	} else
116 		return RPC_GetPcrEventLog(tspContext, pulEventNumber, prgbPcrEvents);
117 
118 	return TSS_SUCCESS;
119 }
120 
121