aboutsummaryrefslogtreecommitdiffstats
path: root/src/libimcv/pts/pts.c
blob: e21f2d51c3ad8c318eeddfe1f326d65d5f15f444 (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
/*
 * Copyright (C) 2011-2012 Sansar Choinyambuu
 * Copyright (C) 2012-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 "pts.h"

#include <utils/debug.h>
#include <crypto/hashers/hasher.h>
#include <bio/bio_writer.h>
#include <bio/bio_reader.h>

#include <tpm_tss.h>
#include <tpm_tss_trousers.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <libgen.h>
#include <unistd.h>
#include <errno.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_pts_t private_pts_t;

/**
 * Private data of a pts_t object.
 *
 */
struct private_pts_t {

	/**
	 * Public pts_t interface.
	 */
	pts_t public;

	/**
	 * PTS Protocol Capabilities
	 */
	pts_proto_caps_flag_t proto_caps;

	/**
	 * PTS Measurement Algorithm
	 */
	pts_meas_algorithms_t algorithm;

	/**
	 * DH Hash Algorithm
	 */
	pts_meas_algorithms_t dh_hash_algorithm;

	/**
	 * PTS Diffie-Hellman Secret
	 */
	diffie_hellman_t *dh;

	/**
	 * PTS Diffie-Hellman Initiator Nonce
	 */
	chunk_t initiator_nonce;

	/**
	 * PTS Diffie-Hellman Responder Nonce
	 */
	chunk_t responder_nonce;

	/**
	 * Secret assessment value to be used for TPM Quote as an external data
	 */
	chunk_t secret;

	/**
	 * Primary key of platform entry in database
	 */
	int platform_id;

	/**
	 * TRUE if IMC-PTS, FALSE if IMV-PTS
	 */
	bool is_imc;

	/**
	 * Active TPM
	 */
	tpm_tss_t *tpm;

	/**
	 * Contains a TPM_CAP_VERSION_INFO struct
	 */
	chunk_t tpm_version_info;

	/**
	 * AIK object handle
	 */
	uint32_t aik_handle;

	/**
	 * Contains an Attestation Identity Key Certificate
	 */
	certificate_t *aik_cert;

	/**
	 * Primary key referening AIK in database
	 */
	int aik_id;

	/**
	 * Shadow PCR set
	 */
	pts_pcr_t *pcrs;

};

METHOD(pts_t, get_proto_caps, pts_proto_caps_flag_t,
	   private_pts_t *this)
{
	return this->proto_caps;
}

METHOD(pts_t, set_proto_caps, void,
	private_pts_t *this, pts_proto_caps_flag_t flags)
{
	this->proto_caps = flags;
	DBG2(DBG_PTS, "supported PTS protocol capabilities: %s%s%s%s%s",
		 flags & PTS_PROTO_CAPS_C ? "C" : ".",
		 flags & PTS_PROTO_CAPS_V ? "V" : ".",
		 flags & PTS_PROTO_CAPS_D ? "D" : ".",
		 flags & PTS_PROTO_CAPS_T ? "T" : ".",
		 flags & PTS_PROTO_CAPS_X ? "X" : ".");
}

METHOD(pts_t, get_meas_algorithm, pts_meas_algorithms_t,
	private_pts_t *this)
{
	return this->algorithm;
}

METHOD(pts_t, set_meas_algorithm, void,
	private_pts_t *this, pts_meas_algorithms_t algorithm)
{
	hash_algorithm_t hash_alg;

	hash_alg = pts_meas_algo_to_hash(algorithm);
	DBG2(DBG_PTS, "selected PTS measurement algorithm is %N",
				   hash_algorithm_names, hash_alg);
	if (hash_alg != HASH_UNKNOWN)
	{
		this->algorithm = algorithm;
	}
}

METHOD(pts_t, get_dh_hash_algorithm, pts_meas_algorithms_t,
	private_pts_t *this)
{
	return this->dh_hash_algorithm;
}

METHOD(pts_t, set_dh_hash_algorithm, void,
	private_pts_t *this, pts_meas_algorithms_t algorithm)
{
	hash_algorithm_t hash_alg;

	hash_alg = pts_meas_algo_to_hash(algorithm);
	DBG2(DBG_PTS, "selected DH hash algorithm is %N",
				   hash_algorithm_names, hash_alg);
	if (hash_alg != HASH_UNKNOWN)
	{
		this->dh_hash_algorithm = algorithm;
	}
}

METHOD(pts_t, create_dh_nonce, bool,
	private_pts_t *this, pts_dh_group_t group, int nonce_len)
{
	diffie_hellman_group_t dh_group;
	chunk_t *nonce;
	rng_t *rng;

	dh_group = pts_dh_group_to_ike(group);
	DBG2(DBG_PTS, "selected PTS DH group is %N",
				   diffie_hellman_group_names, dh_group);
	DESTROY_IF(this->dh);
	this->dh = lib->crypto->create_dh(lib->crypto, dh_group);

	rng = lib->crypto->create_rng(lib->crypto, RNG_STRONG);
	if (!rng)
	{
		DBG1(DBG_PTS, "no rng available");
		return FALSE;
	}
	DBG2(DBG_PTS, "nonce length is %d", nonce_len);
	nonce = this->is_imc ? &this->responder_nonce : &this->initiator_nonce;
	chunk_free(nonce);
	if (!rng->allocate_bytes(rng, nonce_len, nonce))
	{
		DBG1(DBG_PTS, "failed to allocate nonce");
		rng->destroy(rng);
		return FALSE;
	}
	rng->destroy(rng);
	return TRUE;
}

METHOD(pts_t, get_my_public_value, bool,
	private_pts_t *this, chunk_t *value, chunk_t *nonce)
{
	if (!this->dh->get_my_public_value(this->dh, value))
	{
		return FALSE;
	}
	*nonce = this->is_imc ? this->responder_nonce : this->initiator_nonce;
	return TRUE;
}

METHOD(pts_t, set_peer_public_value, bool,
	private_pts_t *this, chunk_t value, chunk_t nonce)
{
	if (!this->dh->set_other_public_value(this->dh, value))
	{
		return FALSE;
	}

	nonce = chunk_clone(nonce);
	if (this->is_imc)
	{
		this->initiator_nonce = nonce;
	}
	else
	{
		this->responder_nonce = nonce;
	}
	return TRUE;
}

METHOD(pts_t, calculate_secret, bool,
	private_pts_t *this)
{
	hasher_t *hasher;
	hash_algorithm_t hash_alg;
	chunk_t shared_secret;

	/* Check presence of nonces */
	if (!this->initiator_nonce.len || !this->responder_nonce.len)
	{
		DBG1(DBG_PTS, "initiator and/or responder nonce is not available");
		return FALSE;
	}
	DBG3(DBG_PTS, "initiator nonce: %B", &this->initiator_nonce);
	DBG3(DBG_PTS, "responder nonce: %B", &this->responder_nonce);

	/* Calculate the DH secret */
	if (!this->dh->get_shared_secret(this->dh, &shared_secret))
	{
		DBG1(DBG_PTS, "shared DH secret computation failed");
		return FALSE;
	}
	DBG3(DBG_PTS, "shared DH secret: %B", &shared_secret);

	/* Calculate the secret assessment value */
	hash_alg = pts_meas_algo_to_hash(this->dh_hash_algorithm);
	hasher = lib->crypto->create_hasher(lib->crypto, hash_alg);

	if (!hasher ||
		!hasher->get_hash(hasher, chunk_from_chars('1'), NULL) ||
		!hasher->get_hash(hasher, this->initiator_nonce, NULL) ||
		!hasher->get_hash(hasher, this->responder_nonce, NULL) ||
		!hasher->allocate_hash(hasher, shared_secret, &this->secret))
	{
		DESTROY_IF(hasher);
		return FALSE;
	}
	hasher->destroy(hasher);

	/* The DH secret must be destroyed */
	chunk_clear(&shared_secret);

	/*
	 * Truncate the hash to 20 bytes to fit the ExternalData
	 * argument of the TPM Quote command
	 */
	this->secret.len = min(this->secret.len, 20);
	DBG3(DBG_PTS, "secret assessment value: %B", &this->secret);
	return TRUE;
}

METHOD(pts_t, get_platform_id, int,
	private_pts_t *this)
{
	return this->platform_id;
}

METHOD(pts_t, set_platform_id, void,
	private_pts_t *this, int pid)
{
	this->platform_id = pid;
}

METHOD(pts_t, get_tpm_version_info, bool,
	private_pts_t *this, chunk_t *info)
{
	*info = this->tpm ? this->tpm->get_version_info(this->tpm) :
						this->tpm_version_info;
	return info->len > 0;
}

METHOD(pts_t, set_tpm_version_info, void,
	private_pts_t *this, chunk_t info)
{
	this->tpm_version_info = chunk_clone(info);
	/* print_tpm_version_info(this); */
}

/**
 * Load an AIK handle and an optional AIK certificate and
 * in the case of a TPM 1.2 an AIK private key blob plus matching public key,
 * the certificate having precedence over the public key if both are present
 */
static void load_aik(private_pts_t *this)
{
	char *handle_str, *cert_path, *key_path, *blob_path;
	chunk_t aik_pubkey = chunk_empty;

	handle_str = lib->settings->get_str(lib->settings,
						"%s.plugins.imc-attestation.aik_handle", NULL, lib->ns);
	cert_path = lib->settings->get_str(lib->settings,
						"%s.plugins.imc-attestation.aik_cert", NULL, lib->ns);
	key_path = lib->settings->get_str(lib->settings,
						"%s.plugins.imc-attestation.aik_pubkey", NULL, lib->ns);
	blob_path = lib->settings->get_str(lib->settings,
						"%s.plugins.imc-attestation.aik_blob", NULL, lib->ns);

	if (handle_str)
	{
		this->aik_handle = strtoll(handle_str, NULL, 16);
	}
	if (cert_path)
	{
		this->aik_cert = lib->creds->create(lib->creds, CRED_CERTIFICATE,
									   CERT_X509, BUILD_FROM_FILE,
									   cert_path, BUILD_END);
		if (this->aik_cert)
		{
			DBG2(DBG_PTS, "loaded AIK certificate from '%s'", cert_path);
		}
	}

	if (this->tpm->get_version(this->tpm) == TPM_VERSION_1_2)
	{
		tpm_tss_trousers_t *tpm_12;
		chunk_t aik_blob = chunk_empty;
		chunk_t *map;

		/* get AIK private key blob */
		if (blob_path)
		{
			map = chunk_map(blob_path, FALSE);
			if (map)
			{
				DBG2(DBG_PTS, "loaded AIK Blob from '%s'", blob_path);
				DBG3(DBG_PTS, "AIK Blob: %B", map);
				aik_blob = chunk_clone(*map);
				chunk_unmap(map);
			}
			else
			{
				DBG1(DBG_PTS, "unable to map AIK Blob file '%s': %s",
							   blob_path, strerror(errno));
			}
		}
		else
		{
			DBG1(DBG_PTS, "AIK Blob is not available");
		}

		/* get AIK public key */
		if (key_path)
		{
			map = chunk_map(key_path, FALSE);
			if (map)
			{
				DBG2(DBG_PTS, "loaded AIK public key from '%s'", key_path);
				aik_pubkey = chunk_clone(*map);
				chunk_unmap(map);
			}
			else
			{
				DBG1(DBG_PTS, "unable to map AIK public key file '%s': %s",
							   key_path, strerror(errno));
			}
		}
		else
		{
			DBG1(DBG_PTS, "AIK public key is not available");
		}

		/* Load AIK item into TPM 1.2 object */
		tpm_12 = (tpm_tss_trousers_t *)this->tpm;
		tpm_12->load_aik(tpm_12, aik_blob, aik_pubkey, this->aik_handle);
	}

	/* if no signed X.509 AIK certificate is available use public key instead */
	if (!this->aik_cert)
	{
		aik_pubkey = this->tpm->get_public(this->tpm, this->aik_handle);
		if (aik_pubkey.len > 0)
		{
			this->aik_cert = lib->creds->create(lib->creds, CRED_CERTIFICATE,
									   CERT_TRUSTED_PUBKEY, BUILD_BLOB,
									   aik_pubkey, BUILD_END);
			chunk_free(&aik_pubkey);
		}
		else
		{
			DBG1(DBG_PTS, "neither AIK certificate nor public key is available");
		}
	}
}

METHOD(pts_t, get_aik, certificate_t*,
	private_pts_t *this)
{
	return this->aik_cert;
}

METHOD(pts_t, set_aik, void,
	private_pts_t *this, certificate_t *aik, int aik_id)
{
	DESTROY_IF(this->aik_cert);
	this->aik_cert = aik->get_ref(aik);
	this->aik_id = aik_id;
}

METHOD(pts_t, get_aik_id, int,
	private_pts_t *this)
{
	return this->aik_id;
}

METHOD(pts_t, is_path_valid, bool,
	private_pts_t *this, char *path, pts_error_code_t *error_code)
{
	struct stat st;

	*error_code = 0;

	if (!stat(path, &st))
	{
		return TRUE;
	}
	else if (errno == ENOENT || errno == ENOTDIR)
	{
		DBG1(DBG_PTS, "file/directory does not exist %s", path);
		*error_code = TCG_PTS_FILE_NOT_FOUND;
	}
	else if (errno == EFAULT)
	{
		DBG1(DBG_PTS, "bad address %s", path);
		*error_code = TCG_PTS_INVALID_PATH;
	}
	else
	{
		DBG1(DBG_PTS, "error: %s occurred while validating path: %s",
			 		   strerror(errno), path);
		return FALSE;
	}

	return TRUE;
}

/**
 * Obtain statistical information describing a file
 */
static bool file_metadata(char *pathname, pts_file_metadata_t **entry)
{
	struct stat st;
	pts_file_metadata_t *this;

	this = malloc_thing(pts_file_metadata_t);

	if (stat(pathname, &st))
	{
		DBG1(DBG_PTS, "unable to obtain statistics about '%s'", pathname);
		free(this);
		return FALSE;
	}

	if (S_ISREG(st.st_mode))
	{
		this->type = PTS_FILE_REGULAR;
	}
	else if (S_ISDIR(st.st_mode))
	{
		this->type = PTS_FILE_DIRECTORY;
	}
	else if (S_ISCHR(st.st_mode))
	{
		this->type = PTS_FILE_CHAR_SPEC;
	}
	else if (S_ISBLK(st.st_mode))
	{
		this->type = PTS_FILE_BLOCK_SPEC;
	}
	else if (S_ISFIFO(st.st_mode))
	{
		this->type = PTS_FILE_FIFO;
	}
#ifndef WIN32
	else if (S_ISLNK(st.st_mode))
	{
		this->type = PTS_FILE_SYM_LINK;
	}
	else if (S_ISSOCK(st.st_mode))
	{
		this->type = PTS_FILE_SOCKET;
	}
#endif /* WIN32 */
	else
	{
		this->type = PTS_FILE_OTHER;
	}

	this->filesize = st.st_size;
	this->created =  st.st_ctime;
	this->modified = st.st_mtime;
	this->accessed = st.st_atime;
	this->owner =    st.st_uid;
	this->group =    st.st_gid;

	*entry = this;
	return TRUE;
}

METHOD(pts_t, get_metadata, pts_file_meta_t*,
	private_pts_t *this, char *pathname, bool is_directory)
{
	pts_file_meta_t *metadata;
	pts_file_metadata_t *entry;

	/* Create a metadata object */
	metadata = pts_file_meta_create();

	if (is_directory)
	{
		enumerator_t *enumerator;
		char *rel_name, *abs_name;
		struct stat st;

		enumerator = enumerator_create_directory(pathname);
		if (!enumerator)
		{
			DBG1(DBG_PTS,"  directory '%s' can not be opened, %s", pathname,
				 strerror(errno));
			metadata->destroy(metadata);
			return NULL;
		}
		while (enumerator->enumerate(enumerator, &rel_name, &abs_name, &st))
		{
			/* measure regular files only */
			if (S_ISREG(st.st_mode) && *rel_name != '.')
			{
				if (!file_metadata(abs_name, &entry))
				{
					enumerator->destroy(enumerator);
					metadata->destroy(metadata);
					return NULL;
				}
				entry->filename = strdup(rel_name);
				metadata->add(metadata, entry);
			}
		}
		enumerator->destroy(enumerator);
	}
	else
	{
		if (!file_metadata(pathname, &entry))
		{
			metadata->destroy(metadata);
			return NULL;
		}
		entry->filename = path_basename(pathname);
		metadata->add(metadata, entry);
	}

	return metadata;
}

METHOD(pts_t, read_pcr, bool,
	private_pts_t *this, uint32_t pcr_num, chunk_t *pcr_value,
	hash_algorithm_t alg)
{
	return this->tpm ? this->tpm->read_pcr(this->tpm, pcr_num, pcr_value, alg)
				     : FALSE;
}

METHOD(pts_t, extend_pcr, bool,
	private_pts_t *this, uint32_t pcr_num, chunk_t *pcr_value, chunk_t data,
	hash_algorithm_t alg)
{
	if (!this->tpm->extend_pcr(this->tpm, pcr_num, pcr_value, data, alg))
	{
		return FALSE;
	}
	DBG3(DBG_PTS, "PCR %d extended with:   %#B", pcr_num, &data);
	DBG3(DBG_PTS, "PCR %d after extension: %#B", pcr_num, pcr_value);

	return TRUE;
}

METHOD(pts_t, quote_tpm, bool,
	private_pts_t *this, bool use_quote2, bool use_version_info,
	chunk_t *pcr_comp, chunk_t *quote_sig)
{
	chunk_t pcr_value;
	uint32_t pcr, pcr_sel = 0;
	enumerator_t *enumerator;
	tpm_quote_mode_t quote_mode;

	/* select PCRs */
	DBG2(DBG_PTS, "PCR values hashed into PCR Composite:");
	enumerator = this->pcrs->create_enumerator(this->pcrs);
	while (enumerator->enumerate(enumerator, &pcr))
	{
		if (this->tpm->read_pcr(this->tpm, pcr, &pcr_value, HASH_SHA1))
		{
			DBG2(DBG_PTS, "PCR %2d %#B", pcr, &pcr_value);
			chunk_free(&pcr_value);
		};

		/* add PCR to selection list */
		pcr_sel |= (1 << pcr);
	}
	enumerator->destroy(enumerator);

	quote_mode = use_quote2 ? (use_version_info ? TPM_QUOTE2_VERSION_INFO :
												  TPM_QUOTE2) : TPM_QUOTE;

	/* TPM Quote */
	return this->tpm->quote(this->tpm, this->aik_handle, pcr_sel, HASH_SHA1,
							this->secret, quote_mode, pcr_comp, quote_sig);
}

/**
 * TPM_QUOTE_INFO structure:
 *	4 bytes of version
 *	4 bytes 'Q' 'U' 'O' 'T'
 *	20 byte SHA1 of TCPA_PCR_COMPOSITE
 *	20 byte nonce
 *
 * TPM_QUOTE_INFO2 structure:
 * 2 bytes Tag 0x0036 TPM_Tag_Quote_info2
 * 4 bytes 'Q' 'U' 'T' '2'
 * 20 bytes nonce
 * 26 bytes PCR_INFO_SHORT
 */

METHOD(pts_t, get_quote_info, bool,
	private_pts_t *this, bool use_quote2, bool use_version_info,
	pts_meas_algorithms_t comp_hash_algo,
	chunk_t *out_pcr_comp, chunk_t *out_quote_info)
{
	chunk_t selection, pcr_comp, hash_pcr_comp;
	bio_writer_t *writer;
	hasher_t *hasher;

	if (!this->pcrs->get_count(this->pcrs))
	{
		DBG1(DBG_PTS, "No extended PCR entries available, "
					  "unable to construct TPM Quote Info");
		return FALSE;
	}
	if (!this->secret.ptr)
	{
		DBG1(DBG_PTS, "Secret assessment value unavailable, ",
					  "unable to construct TPM Quote Info");
		return FALSE;
	}
	if (use_quote2 && use_version_info && !this->tpm_version_info.ptr)
	{
		DBG1(DBG_PTS, "TPM Version Information unavailable, ",
					  "unable to construct TPM Quote Info2");
		return FALSE;
	}

	pcr_comp = this->pcrs->get_composite(this->pcrs);


	/* Output the TPM_PCR_COMPOSITE expected from IMC */
	if (comp_hash_algo)
	{
		hash_algorithm_t algo;

		algo = pts_meas_algo_to_hash(comp_hash_algo);
		hasher = lib->crypto->create_hasher(lib->crypto, algo);

		/* Hash the PCR Composite Structure */
		if (!hasher || !hasher->allocate_hash(hasher, pcr_comp, out_pcr_comp))
		{
			DESTROY_IF(hasher);
			free(pcr_comp.ptr);
			return FALSE;
		}
		DBG3(DBG_PTS, "constructed PCR Composite hash: %#B", out_pcr_comp);
		hasher->destroy(hasher);
	}
	else
	{
		*out_pcr_comp = chunk_clone(pcr_comp);
	}

	/* SHA1 hash of PCR Composite to construct TPM_QUOTE_INFO */
	hasher = lib->crypto->create_hasher(lib->crypto, HASH_SHA1);
	if (!hasher || !hasher->allocate_hash(hasher, pcr_comp, &hash_pcr_comp))
	{
		DESTROY_IF(hasher);
		chunk_free(out_pcr_comp);
		free(pcr_comp.ptr);
		return FALSE;
	}
	hasher->destroy(hasher);

	/* Construct TPM_QUOTE_INFO/TPM_QUOTE_INFO2 structure */
	writer = bio_writer_create(TPM_QUOTE_INFO_LEN);

	if (use_quote2)
	{
		/* TPM Structure Tag */
		writer->write_uint16(writer, TPM_TAG_QUOTE_INFO2);

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

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

		/* PCR selection */
		selection.ptr = pcr_comp.ptr;
		selection.len = 2 + this->pcrs->get_selection_size(this->pcrs);
		writer->write_data(writer, selection);

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

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

		if (use_version_info)
		{
			/* TPM version Info */
			writer->write_data(writer, this->tpm_version_info);
		}
	}
	else
	{
		/* Version number */
		writer->write_data(writer, chunk_from_chars(1, 1, 0, 0));

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

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

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

	/* TPM Quote Info */
	*out_quote_info = writer->extract_buf(writer);
	DBG3(DBG_PTS, "constructed TPM Quote Info: %B", out_quote_info);

	writer->destroy(writer);
	free(pcr_comp.ptr);
	free(hash_pcr_comp.ptr);

	return TRUE;
}

METHOD(pts_t, verify_quote_signature, bool,
				private_pts_t *this, chunk_t data, chunk_t signature)
{
	public_key_t *aik_pubkey;

	aik_pubkey = this->aik_cert->get_public_key(this->aik_cert);
	if (!aik_pubkey)
	{
		DBG1(DBG_PTS, "failed to get public key from AIK certificate");
		return FALSE;
	}

	if (!aik_pubkey->verify(aik_pubkey, SIGN_RSA_EMSA_PKCS1_SHA1,
		data, signature))
	{
		DBG1(DBG_PTS, "signature verification failed for TPM Quote Info");
		DESTROY_IF(aik_pubkey);
		return FALSE;
	}

	aik_pubkey->destroy(aik_pubkey);
	return TRUE;
}

METHOD(pts_t, get_pcrs, pts_pcr_t*,
	private_pts_t *this)
{
	return this->pcrs;
}

METHOD(pts_t, destroy, void,
	private_pts_t *this)
{
	DESTROY_IF(this->tpm);
	DESTROY_IF(this->pcrs);
	DESTROY_IF(this->aik_cert);
	DESTROY_IF(this->dh);
	free(this->initiator_nonce.ptr);
	free(this->responder_nonce.ptr);
	free(this->secret.ptr);
	free(this->tpm_version_info.ptr);
	free(this);
}

/**
 * See header
 */
pts_t *pts_create(bool is_imc)
{
	private_pts_t *this;
	pts_pcr_t *pcrs;

	pcrs = pts_pcr_create();
	if (!pcrs)
	{
		DBG1(DBG_PTS, "shadow PCR set could not be created");
		return NULL;
	}

	INIT(this,
		.public = {
			.get_proto_caps = _get_proto_caps,
			.set_proto_caps = _set_proto_caps,
			.get_meas_algorithm = _get_meas_algorithm,
			.set_meas_algorithm = _set_meas_algorithm,
			.get_dh_hash_algorithm = _get_dh_hash_algorithm,
			.set_dh_hash_algorithm = _set_dh_hash_algorithm,
			.create_dh_nonce = _create_dh_nonce,
			.get_my_public_value = _get_my_public_value,
			.set_peer_public_value = _set_peer_public_value,
			.calculate_secret = _calculate_secret,
			.get_platform_id = _get_platform_id,
			.set_platform_id = _set_platform_id,
			.get_tpm_version_info = _get_tpm_version_info,
			.set_tpm_version_info = _set_tpm_version_info,
			.get_aik = _get_aik,
			.set_aik = _set_aik,
			.get_aik_id = _get_aik_id,
			.is_path_valid = _is_path_valid,
			.get_metadata = _get_metadata,
			.read_pcr = _read_pcr,
			.extend_pcr = _extend_pcr,
			.quote_tpm = _quote_tpm,
			.get_pcrs = _get_pcrs,
			.get_quote_info = _get_quote_info,
			.verify_quote_signature  = _verify_quote_signature,
			.destroy = _destroy,
		},
		.is_imc = is_imc,
		.proto_caps = PTS_PROTO_CAPS_V,
		.algorithm = PTS_MEAS_ALGO_SHA256,
		.dh_hash_algorithm = PTS_MEAS_ALGO_SHA256,
		.pcrs = pcrs,
	);

	if (is_imc)
	{
		this->tpm = tpm_tss_probe(TPM_VERSION_ANY);
		if (this->tpm)
		{
			this->proto_caps |= PTS_PROTO_CAPS_T | PTS_PROTO_CAPS_D;
			load_aik(this);
		}
	}
	else
	{
		this->proto_caps |= PTS_PROTO_CAPS_T | PTS_PROTO_CAPS_D;
	}

	return &this->public;
}