xref: /netbsd-src/crypto/external/cpl/trousers/dist/src/tspi/rpc/tcstp/rpc_quote2.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_Quote2_TP(struct host_table_entry * hte,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)29 RPC_Quote2_TP(struct host_table_entry *hte,
30 	      TCS_KEY_HANDLE keyHandle,	/* in */
31 	      TCPA_NONCE *antiReplay,	/* in */
32 	      UINT32 pcrDataSizeIn,	/* in */
33 	      BYTE * pcrDataIn,	/* in */
34 	      TSS_BOOL addVersion, /* in */
35 	      TPM_AUTH * privAuth,	/* in, out */
36 	      UINT32 * pcrDataSizeOut,	/* out */
37 	      BYTE ** pcrDataOut,	/* out */
38 	      UINT32* versionInfoSize, /* out */
39 	      BYTE** versionInfo, /* out */
40 	      UINT32 * sigSize,	/* out */
41 	      BYTE ** sig)	/* out */
42 {
43 	TSS_RESULT result;
44 	int i;
45 
46 	initData(&hte->comm, 7);
47 
48 	hte->comm.hdr.u.ordinal = TCSD_ORD_QUOTE2;
49 	LogDebugFn("TCS Context: 0x%x", hte->tcsContext);
50 
51 
52 	if (setData(TCSD_PACKET_TYPE_UINT32, 0, &hte->tcsContext, 0, &hte->comm))
53 		return TSPERR(TSS_E_INTERNAL_ERROR);
54 	if (setData(TCSD_PACKET_TYPE_UINT32, 1, &keyHandle, 0, &hte->comm))
55 		return TSPERR(TSS_E_INTERNAL_ERROR);
56 	if (setData(TCSD_PACKET_TYPE_NONCE, 2, antiReplay, 0, &hte->comm))
57 		return TSPERR(TSS_E_INTERNAL_ERROR);
58 	if (setData(TCSD_PACKET_TYPE_UINT32, 3, &pcrDataSizeIn, 0, &hte->comm))
59 		return TSPERR(TSS_E_INTERNAL_ERROR);
60 	if (setData(TCSD_PACKET_TYPE_PBYTE, 4, pcrDataIn, pcrDataSizeIn, &hte->comm))
61 		return TSPERR(TSS_E_INTERNAL_ERROR);
62 	if (setData(TCSD_PACKET_TYPE_BOOL, 5, &addVersion, 0, &hte->comm))
63 			return TSPERR(TSS_E_INTERNAL_ERROR);
64 	if (privAuth) {
65 		if (setData(TCSD_PACKET_TYPE_AUTH, 6, privAuth, 0, &hte->comm))
66 			return TSPERR(TSS_E_INTERNAL_ERROR);
67 	}
68 
69 	result = sendTCSDPacket(hte);
70 
71 	if (result == TSS_SUCCESS)
72 		result = hte->comm.hdr.u.result;
73 
74 	/* Takes and sets the output data */
75 	if (result == TSS_SUCCESS) {
76 		i = 0;
77 		if (privAuth) {
78 			if (getData(TCSD_PACKET_TYPE_AUTH, i++, privAuth, 0, &hte->comm)) {
79 				result = TSPERR(TSS_E_INTERNAL_ERROR);
80 				goto done;
81 			}
82 		}
83 		if (getData(TCSD_PACKET_TYPE_UINT32, i++, pcrDataSizeOut, 0, &hte->comm)) {
84 			result = TSPERR(TSS_E_INTERNAL_ERROR);
85 			goto done;
86 		}
87 
88 		*pcrDataOut = (BYTE *) malloc(*pcrDataSizeOut);
89 		if (*pcrDataOut == NULL) {
90 			LogError("malloc of %u bytes failed.", *pcrDataSizeOut);
91 			result = TSPERR(TSS_E_OUTOFMEMORY);
92 			goto done;
93 		}
94 		if (getData(TCSD_PACKET_TYPE_PBYTE, i++, *pcrDataOut, *pcrDataSizeOut, &hte->comm)) {
95 			free(*pcrDataOut);
96 			result = TSPERR(TSS_E_INTERNAL_ERROR);
97 			goto done;
98 		}
99 		/* Retrieves the versionInfo Parameters */
100 		if (getData(TCSD_PACKET_TYPE_UINT32, i++, versionInfoSize, 0, &hte->comm)) {
101 			free(*pcrDataOut);
102 			result = TSPERR(TSS_E_INTERNAL_ERROR);
103 			goto done;
104 		}
105 		if (*versionInfoSize >0){
106 			*versionInfo = (BYTE *) malloc(*versionInfoSize);
107 			if (*versionInfo == NULL) {
108 				LogError("malloc of %u bytes failed.", *versionInfoSize);
109 				free(*pcrDataOut);
110 				result = TSPERR(TSS_E_OUTOFMEMORY);
111 				goto done;
112 			}
113 			if (getData(TCSD_PACKET_TYPE_PBYTE, i++, *versionInfo, *versionInfoSize,
114 				    &hte->comm)) {
115 				free(*pcrDataOut);
116 				free(*versionInfo);
117 				result = TSPERR(TSS_E_INTERNAL_ERROR);
118 				goto done;
119 			}
120 		}
121 
122 		if (getData(TCSD_PACKET_TYPE_UINT32, i++, sigSize, 0, &hte->comm)) {
123 			free(*pcrDataOut);
124 			if (addVersion)
125 				free(*versionInfo);
126 			result = TSPERR(TSS_E_INTERNAL_ERROR);
127 			goto done;
128 		}
129 		*sig = (BYTE *) malloc(*sigSize);
130 		if (*sig == NULL) {
131 			LogError("malloc of %u bytes failed.", *sigSize);
132 			free(*pcrDataOut);
133 			if (addVersion)
134 				free(*versionInfo);
135 			result = TSPERR(TSS_E_OUTOFMEMORY);
136 			goto done;
137 		}
138 		if (getData(TCSD_PACKET_TYPE_PBYTE, i++, *sig, *sigSize, &hte->comm)) {
139 			free(*pcrDataOut);
140 			if (addVersion)
141 				free(*versionInfo);
142 			free(*sig);
143 			result = TSPERR(TSS_E_INTERNAL_ERROR);
144 		}
145 	}
146 
147 done:
148 	return result;
149 }
150