aboutsummaryrefslogtreecommitdiffstats
path: root/src/libtpmtss/tpm_tss_quote_info.c
blob: 6a58448ee54bf004d59bae6574e57b22a99ba4cb (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/*
 * Copyright (C) 2016 Andreas Steffen
 * HSR Hochschule fuer Technik Rapperswil
 *
* This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include <tpm_tss_quote_info.h>

#include <bio/bio_writer.h>

#ifndef TPM_TAG_QUOTE_INFO2
#define TPM_TAG_QUOTE_INFO2 0x0036
#endif
#ifndef TPM_LOC_ZERO
#define TPM_LOC_ZERO 0x01
#endif

typedef struct private_tpm_tss_quote_info_t private_tpm_tss_quote_info_t;

/**
 * Private data of an tpm_tss_quote_info_t object.
 */
struct private_tpm_tss_quote_info_t {

	/**
	 * Public tpm_tss_quote_info_t interface.
	 */
	tpm_tss_quote_info_t public;

	/**
	 * TPM Quote Mode
	 */
	tpm_quote_mode_t quote_mode;

	/**
	 * TPM Qualified Signer
	 */
	chunk_t qualified_signer;

	/**
	 * TPM Clock Info
	 */
	chunk_t clock_info;

	/**
	 * TPM Version Info
	 */
	chunk_t	version_info;

	/**
	 * TPM PCR Selection
	 */
	chunk_t pcr_select;

	/**
	 * TPM PCR Composite Hash
	 */
	chunk_t pcr_digest;

	/**
	 * TPM PCR Composite Hash algoritm
	 */
	hash_algorithm_t pcr_digest_alg;

	/**
	 * Reference count
	 */
	refcount_t ref;

};

METHOD(tpm_tss_quote_info_t, get_quote_mode, tpm_quote_mode_t,
	private_tpm_tss_quote_info_t *this)
{
	return this->quote_mode;
}

METHOD(tpm_tss_quote_info_t, get_pcr_digest_alg, hash_algorithm_t,
	private_tpm_tss_quote_info_t *this)
{
	return this->pcr_digest_alg;
}

METHOD(tpm_tss_quote_info_t, get_pcr_digest, chunk_t,
	private_tpm_tss_quote_info_t *this)
{
	return this->pcr_digest;
}

METHOD(tpm_tss_quote_info_t, get_quote, bool,
	private_tpm_tss_quote_info_t *this, chunk_t nonce,
	tpm_tss_pcr_composite_t *composite, chunk_t *quoted)
{
	chunk_t pcr_composite, pcr_digest;
	bio_writer_t *writer;
	hasher_t *hasher;
	bool equal_digests;

	/* Construct PCR Composite */
	writer = bio_writer_create(32);

	switch (this->quote_mode)
	{
		case TPM_QUOTE:
		case TPM_QUOTE2:
		case TPM_QUOTE2_VERSION_INFO:
			writer->write_data16(writer, composite->pcr_select);
			writer->write_data32(writer, composite->pcr_composite);

			break;
		case TPM_QUOTE_TPM2:
			writer->write_data(writer, composite->pcr_composite);
			break;
		case TPM_QUOTE_NONE:
			break;
	}

	pcr_composite = writer->extract_buf(writer);
	writer->destroy(writer);

	DBG2(DBG_PTS, "constructed PCR Composite: %B", &pcr_composite);

	/* Compute PCR Composite Hash */
	hasher = lib->crypto->create_hasher(lib->crypto, this->pcr_digest_alg);
	if (!hasher || !hasher->allocate_hash(hasher, pcr_composite, &pcr_digest))
	{
		DESTROY_IF(hasher);
		chunk_free(&pcr_composite);
		return FALSE;
	}
	hasher->destroy(hasher);
	chunk_free(&pcr_composite);

	DBG2(DBG_PTS, "constructed PCR Composite digest: %B", &pcr_digest);

	equal_digests = chunk_equals(pcr_digest, this->pcr_digest);

	/* Construct Quote Info */
	writer = bio_writer_create(32);

	switch (this->quote_mode)
	{
		case TPM_QUOTE:
			/* Version number */
			writer->write_data(writer, chunk_from_chars(1, 1, 0, 0));

			/* Magic QUOT value */
			writer->write_data(writer, chunk_from_str("QUOT"));

			/* PCR Composite Hash */
			writer->write_data(writer, pcr_digest);

			/* Secret assessment value 20 bytes (nonce) */
			writer->write_data(writer, nonce);
			break;
		case TPM_QUOTE2:
		case TPM_QUOTE2_VERSION_INFO:
			/* TPM Structure Tag */
			writer->write_uint16(writer, TPM_TAG_QUOTE_INFO2);

			/* Magic QUT2 value */
			writer->write_data(writer, chunk_from_str("QUT2"));

			/* Secret assessment value 20 bytes (nonce) */
			writer->write_data(writer, nonce);

			/* PCR selection */
			writer->write_data16(writer, composite->pcr_select);

			/* TPM Locality Selection */
			writer->write_uint8(writer, TPM_LOC_ZERO);

			/* PCR Composite Hash */
			writer->write_data(writer, pcr_digest);

			if (this->quote_mode == TPM_QUOTE2_VERSION_INFO)
			{
				/* TPM version Info */
				writer->write_data(writer, this->version_info);
			}
			break;
		case TPM_QUOTE_TPM2:
			/* Magic */
			writer->write_data(writer, chunk_from_chars(0xff,0x54,0x43,0x47));

			/* Type */
			writer->write_uint16(writer, 0x8018);

			/* Qualified Signer */
			writer->write_data16(writer, this->qualified_signer);

			/* Extra Data */
			writer->write_data16(writer, nonce);

			/* Clock Info */
			writer->write_data(writer, this->clock_info);

			/* Firmware Version */
			writer->write_data(writer, this->version_info);		

			/* PCR Selection */
			writer->write_data(writer, this->pcr_select);

			/* PCR Composite Hash */
			writer->write_data16(writer, pcr_digest);
			break;
		case TPM_QUOTE_NONE:
			break;
	}
	chunk_free(&pcr_digest);
	*quoted = writer->extract_buf(writer);
	writer->destroy(writer);

	DBG2(DBG_PTS, "constructed TPM Quote Info: %B", quoted);

	if (!equal_digests)
	{
		DBG1(DBG_IMV, "received PCR Composite digest does not match "
					  "constructed one");
		chunk_free(quoted);
	}
	return equal_digests;
}

METHOD(tpm_tss_quote_info_t, set_version_info, void,
	private_tpm_tss_quote_info_t *this, chunk_t version_info)
{
	chunk_free(&this->version_info);
	this->version_info = chunk_clone(version_info);
}

METHOD(tpm_tss_quote_info_t, get_version_info, chunk_t,
	private_tpm_tss_quote_info_t *this)
{
	return this->version_info;
}

METHOD(tpm_tss_quote_info_t, set_tpm2_info, void,
	private_tpm_tss_quote_info_t *this, chunk_t qualified_signer,
	chunk_t clock_info, chunk_t pcr_select)
{
	chunk_free(&this->qualified_signer);
	this->qualified_signer = chunk_clone(qualified_signer);

	chunk_free(&this->clock_info);
	this->clock_info = chunk_clone(clock_info);

	chunk_free(&this->pcr_select);
	this->pcr_select = chunk_clone(pcr_select);
}

METHOD(tpm_tss_quote_info_t, get_tpm2_info, void,
	private_tpm_tss_quote_info_t *this, chunk_t *qualified_signer,
	chunk_t *clock_info, chunk_t *pcr_select)
{
	if (qualified_signer)
	{
		*qualified_signer = this->qualified_signer;
	}
	if (clock_info)
	{
		*clock_info = this->clock_info;
	}
	if (pcr_select)
	{
		*pcr_select = this->pcr_select;
	}
}

METHOD(tpm_tss_quote_info_t, get_ref, tpm_tss_quote_info_t*,
	private_tpm_tss_quote_info_t *this)
{
	ref_get(&this->ref);

	return &this->public;
}

METHOD(tpm_tss_quote_info_t, destroy, void,
	private_tpm_tss_quote_info_t *this)
{
	if (ref_put(&this->ref))
	{
		chunk_free(&this->qualified_signer);
		chunk_free(&this->clock_info);
		chunk_free(&this->version_info);
		chunk_free(&this->pcr_select);
		chunk_free(&this->pcr_digest);
		free(this);
	}
}

/**
 * See header
 */
tpm_tss_quote_info_t *tpm_tss_quote_info_create(tpm_quote_mode_t quote_mode,
						hash_algorithm_t pcr_digest_alg, chunk_t pcr_digest)

{
	private_tpm_tss_quote_info_t *this;

	INIT(this,
		.public = {
			.get_quote_mode = _get_quote_mode,
			.get_pcr_digest_alg = _get_pcr_digest_alg,
			.get_pcr_digest = _get_pcr_digest,
			.get_quote = _get_quote,
			.set_version_info = _set_version_info,
			.get_version_info = _get_version_info,
			.set_tpm2_info = _set_tpm2_info,
			.get_tpm2_info = _get_tpm2_info,
			.get_ref = _get_ref,
			.destroy = _destroy,
		},
		.quote_mode = quote_mode,
		.pcr_digest_alg = pcr_digest_alg,
		.pcr_digest = chunk_clone(pcr_digest),
		.ref = 1,
	);

	return &this->public;
}