summaryrefslogtreecommitdiff
path: root/src/tspi/tsp_maint.c
blob: 3e50dddd67d30b90f4295dd5847fbf0add9c81b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

/*
 * Licensed Materials - Property of IBM
 *
 * trousers - An open source TCG Software Stack
 *
 * (C) Copyright International Business Machines Corp. 2004-2007
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>

#include "trousers/tss.h"
#include "trousers/trousers.h"
#include "trousers_types.h"
#include "spi_utils.h"
#include "capabilities.h"
#include "tsplog.h"
#include "obj.h"

#ifdef TSS_BUILD_TRANSPORT
TSS_RESULT
Transport_CreateMaintenanceArchive(TSS_HCONTEXT tspContext,	/* in */
				   TSS_BOOL generateRandom,	/* in */
				   TPM_AUTH * ownerAuth,	/* in, out */
				   UINT32 * randomSize,	/* out */
				   BYTE ** random,	/* out */
				   UINT32 * archiveSize,	/* out */
				   BYTE ** archive)	/* out */
{
	UINT64 offset;
	TSS_RESULT result;
	UINT32 handlesLen = 0, decLen;
	BYTE *dec;

	if ((result = obj_context_transport_init(tspContext)))
		return result;

	LogDebugFn("Executing in a transport session");

	if ((result = obj_context_transport_execute(tspContext, TPM_ORD_CreateMaintenanceArchive,
						    sizeof(TSS_BOOL), (BYTE *)&generateRandom, NULL,
						    &handlesLen, NULL, ownerAuth, NULL, &decLen,
						    &dec)))
		return result;

	offset = 0;
	Trspi_UnloadBlob_UINT32(&offset, randomSize, dec);
	if (*randomSize > 0) {
		if ((*random = malloc(*randomSize)) == NULL) {
			*randomSize = 0;
			free(dec);
			LogError("malloc of %u bytes failed", *randomSize);
			return TSPERR(TSS_E_OUTOFMEMORY);
		}
		Trspi_UnloadBlob(&offset, *randomSize, dec, *random);
	}

	Trspi_UnloadBlob_UINT32(&offset, archiveSize, dec);
	if ((*archive = malloc(*archiveSize)) == NULL) {
		free(*random);
		*random = NULL;
		*randomSize = 0;
		free(dec);
		LogError("malloc of %u bytes failed", *archiveSize);
		*archiveSize = 0;
		return TSPERR(TSS_E_OUTOFMEMORY);
	}
	Trspi_UnloadBlob(&offset, *archiveSize, dec, *archive);
	free(dec);

	return result;
}

TSS_RESULT
Transport_LoadMaintenanceArchive(TSS_HCONTEXT tspContext,	/* in */
				 UINT32 dataInSize,	/* in */
				 BYTE * dataIn, /* in */
				 TPM_AUTH * ownerAuth,	/* in, out */
				 UINT32 * dataOutSize,	/* out */
				 BYTE ** dataOut)	/* out */
{
	UINT64 offset;
	TSS_RESULT result;
	UINT32 handlesLen = 0, decLen;
	BYTE *dec;


	if ((result = obj_context_transport_init(tspContext)))
		return result;

	LogDebugFn("Executing in a transport session");

	if ((result = obj_context_transport_execute(tspContext, TPM_ORD_LoadMaintenanceArchive,
						    dataInSize, dataIn, NULL, &handlesLen, NULL,
						    ownerAuth, NULL, &decLen, &dec)))
		return result;

	offset = 0;
	Trspi_UnloadBlob_UINT32(&offset, dataOutSize, dec);

	/* sacrifice 4 bytes */
	*dataOut = &dec[offset];

	return result;
}

TSS_RESULT
Transport_KillMaintenanceFeature(TSS_HCONTEXT tspContext,	/* in */
				 TPM_AUTH * ownerAuth)	/* in, out */
{
	TSS_RESULT result;
	UINT32 handlesLen = 0;

	if ((result = obj_context_transport_init(tspContext)))
		return result;

	LogDebugFn("Executing in a transport session");

	return obj_context_transport_execute(tspContext, TPM_ORD_KillMaintenanceFeature, 0, NULL,
					     NULL, &handlesLen, NULL, ownerAuth, NULL, NULL, NULL);
}

TSS_RESULT
Transport_LoadManuMaintPub(TSS_HCONTEXT tspContext,	/* in */
			   TCPA_NONCE antiReplay,	/* in */
			   UINT32 PubKeySize,	/* in */
			   BYTE * PubKey,	/* in */
			   TCPA_DIGEST * checksum)	/* out */
{
	UINT64 offset;
	TSS_RESULT result;
	UINT32 handlesLen = 0, dataLen, decLen;
	BYTE *data, *dec;

	if ((result = obj_context_transport_init(tspContext)))
		return result;

	LogDebugFn("Executing in a transport session");

	dataLen = sizeof(TCPA_NONCE) + PubKeySize;
	if ((data = malloc(dataLen)) == NULL) {
		LogError("malloc of %u bytes failed", dataLen);
		return TSPERR(TSS_E_OUTOFMEMORY);
	}

	offset = 0;
	Trspi_LoadBlob(&offset, TPM_SHA1_160_HASH_LEN, data, antiReplay.nonce);
	Trspi_LoadBlob(&offset, PubKeySize, data, PubKey);

	if ((result = obj_context_transport_execute(tspContext, TPM_ORD_LoadManuMaintPub,
						    dataLen, data, NULL, &handlesLen, NULL, NULL,
						    NULL, &decLen, &dec))) {
		free(data);
		return result;
	}
	free(data);

	offset = 0;
	Trspi_UnloadBlob_DIGEST(&offset, dec, checksum);
	free(dec);

	return result;
}

TSS_RESULT
Transport_ReadManuMaintPub(TSS_HCONTEXT tspContext,	/* in */
			   TCPA_NONCE antiReplay,	/* in */
			   TCPA_DIGEST * checksum)	/* out */
{
	UINT64 offset;
	TSS_RESULT result;
	UINT32 handlesLen = 0, decLen;
	BYTE *dec;

	if ((result = obj_context_transport_init(tspContext)))
		return result;

	LogDebugFn("Executing in a transport session");

	if ((result = obj_context_transport_execute(tspContext, TPM_ORD_ReadManuMaintPub,
						    sizeof(TCPA_NONCE), antiReplay.nonce, NULL,
						    &handlesLen, NULL, NULL, NULL, &decLen,
						    &dec)))
		return result;

	offset = 0;
	Trspi_UnloadBlob_DIGEST(&offset, dec, checksum);
	free(dec);

	return result;
}
#endif