Changed encoding type to ASN.1 DER
[strongswan.git] / src / libpts / pts / pts.c
1 /*
2 * Copyright (C) 2011 Sansar Choinyambuu
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "pts.h"
17
18 #include <debug.h>
19 #include <crypto/hashers/hasher.h>
20
21 #include <trousers/tss.h>
22 #include <trousers/trousers.h>
23
24 #include <sys/stat.h>
25 #include <sys/utsname.h>
26 #include <errno.h>
27
28 #define PTS_BUF_SIZE 4096
29
30 typedef struct private_pts_t private_pts_t;
31
32 /**
33 * Private data of a pts_t object.
34 *
35 */
36 struct private_pts_t {
37
38 /**
39 * Public pts_t interface.
40 */
41 pts_t public;
42
43 /**
44 * PTS Protocol Capabilities
45 */
46 pts_proto_caps_flag_t proto_caps;
47
48 /**
49 * PTS Measurement Algorithm
50 */
51 pts_meas_algorithms_t algorithm;
52
53 /**
54 * PTS Diffie Hellman Group
55 */
56 pts_dh_group_t dh_group;
57
58 /**
59 * Contains a Diffie Hellman object
60 */
61 diffie_hellman_t *dh;
62
63 /**
64 * Secret assessment value to be used for TPM Quote as an external data
65 */
66 chunk_t secret;
67
68 /**
69 * Platform and OS Info
70 */
71 char *platform_info;
72
73 /**
74 * Do we have an activated TPM
75 */
76 bool has_tpm;
77
78 /**
79 * Contains a TPM_CAP_VERSION_INFO struct
80 */
81 chunk_t tpm_version_info;
82
83 /**
84 * Contains a Attestation Identity Key or Certificate
85 */
86 certificate_t *aik;
87
88 };
89
90 METHOD(pts_t, get_proto_caps, pts_proto_caps_flag_t,
91 private_pts_t *this)
92 {
93 return this->proto_caps;
94 }
95
96 METHOD(pts_t, set_proto_caps, void,
97 private_pts_t *this, pts_proto_caps_flag_t flags)
98 {
99 this->proto_caps = flags;
100 DBG2(DBG_PTS, "supported PTS protocol capabilities: %s%s%s%s%s",
101 flags & PTS_PROTO_CAPS_C ? "C" : ".",
102 flags & PTS_PROTO_CAPS_V ? "V" : ".",
103 flags & PTS_PROTO_CAPS_D ? "D" : ".",
104 flags & PTS_PROTO_CAPS_T ? "T" : ".",
105 flags & PTS_PROTO_CAPS_X ? "X" : ".");
106 }
107
108 METHOD(pts_t, get_meas_algorithm, pts_meas_algorithms_t,
109 private_pts_t *this)
110 {
111 return this->algorithm;
112 }
113
114 METHOD(pts_t, set_meas_algorithm, void,
115 private_pts_t *this, pts_meas_algorithms_t algorithm)
116 {
117 hash_algorithm_t hash_alg;
118
119 hash_alg = pts_meas_to_hash_algorithm(algorithm);
120 DBG2(DBG_PTS, "selected PTS measurement algorithm is %N",
121 hash_algorithm_names, hash_alg);
122 if (hash_alg != HASH_UNKNOWN)
123 {
124 this->algorithm = algorithm;
125 }
126 }
127
128 METHOD(pts_t, get_dh_group, pts_dh_group_t,
129 private_pts_t *this)
130 {
131 return this->dh_group;
132 }
133
134 METHOD(pts_t, set_dh_group, void,
135 private_pts_t *this, pts_dh_group_t group)
136 {
137 diffie_hellman_group_t dh_group;
138
139 dh_group = pts_dh_group_to_strongswan_dh_group(group);
140 DBG2(DBG_PTS, "selected PTS Diffie Hellman Group is %N",
141 diffie_hellman_group_names, dh_group);
142 if (dh_group != MODP_NONE)
143 {
144 this->dh_group = group;
145 }
146 }
147
148 METHOD(pts_t, create_dh, bool,
149 private_pts_t *this, pts_dh_group_t group)
150 {
151 diffie_hellman_group_t dh_group;
152
153 dh_group = pts_dh_group_to_strongswan_dh_group(group);
154 if (dh_group != MODP_NONE)
155 {
156 this->dh = lib->crypto->create_dh(lib->crypto, dh_group);
157 return TRUE;
158 }
159 DBG1(DBG_PTS, "Unable to create Diffie Hellman object with group %N",
160 diffie_hellman_group_names, dh_group);
161 return FALSE;
162 }
163
164 METHOD(pts_t, get_my_pub_val, void,
165 private_pts_t *this, chunk_t *pub_value)
166 {
167 this->dh->get_my_public_value(this->dh, pub_value);
168 DBG3(DBG_PTS, "My Public value:%B", pub_value);
169 }
170
171 METHOD(pts_t, set_other_pub_val, void,
172 private_pts_t *this, chunk_t value)
173 {
174 DBG3(DBG_PTS, "Partner's Public value:%B", &value);
175 this->dh->set_other_public_value(this->dh, value);
176 }
177
178 METHOD(pts_t, calculate_secret, bool,
179 private_pts_t *this, chunk_t initiator_nonce, chunk_t responder_nonce,
180 pts_meas_algorithms_t algorithm)
181 {
182 hasher_t *hasher;
183 hash_algorithm_t hash_alg;
184 u_char output[HASH_SIZE_SHA384];
185 chunk_t shared_secret;
186
187 /* Create a hasher */
188 hash_alg = pts_meas_to_hash_algorithm(algorithm);
189 hasher = lib->crypto->create_hasher(lib->crypto, hash_alg);
190 if (!hasher)
191 {
192 DBG1(DBG_PTS, " hasher %N not available", hash_algorithm_names, hash_alg);
193 return FALSE;
194 }
195
196 if (this->dh->get_shared_secret(this->dh, &shared_secret) != SUCCESS)
197 {
198 DBG1(DBG_PTS, "Shared secret couldn't be calculated");
199 hasher->destroy(hasher);
200 return FALSE;
201 }
202
203 hasher->get_hash(hasher, chunk_create("1", sizeof("1")), NULL);
204 hasher->get_hash(hasher, initiator_nonce, NULL);
205 hasher->get_hash(hasher, responder_nonce, NULL);
206 hasher->get_hash(hasher, shared_secret, output);
207
208 /**
209 * Link the hash output to the secret and set the length
210 * Truncate the output to 20 bytes to fit ExternalDate argument of TPM Quote
211 */
212 this->secret = chunk_create(output, HASH_SIZE_SHA1);
213 DBG3(DBG_PTS, "Secret assessment value: %B", &this->secret);
214
215 chunk_free(&shared_secret);
216 hasher->destroy(hasher);
217 return TRUE;
218 }
219
220 METHOD(pts_t, get_secret, chunk_t,
221 private_pts_t *this)
222 {
223 return this->secret;
224 }
225
226 /**
227 * Print TPM 1.2 Version Info
228 */
229 static void print_tpm_version_info(private_pts_t *this)
230 {
231 TPM_CAP_VERSION_INFO versionInfo;
232 UINT64 offset = 0;
233 TSS_RESULT result;
234
235 result = Trspi_UnloadBlob_CAP_VERSION_INFO(&offset,
236 this->tpm_version_info.ptr, &versionInfo);
237 if (result != TSS_SUCCESS)
238 {
239 DBG1(DBG_PTS, "could not parse tpm version info: tss error 0x%x",
240 result);
241 }
242 else
243 {
244 DBG2(DBG_PTS, "TPM 1.2 Version Info: Chip Version: %hhu.%hhu.%hhu.%hhu,"
245 " Spec Level: %hu, Errata Rev: %hhu, Vendor ID: %.4s",
246 versionInfo.version.major, versionInfo.version.minor,
247 versionInfo.version.revMajor, versionInfo.version.revMinor,
248 versionInfo.specLevel, versionInfo.errataRev,
249 versionInfo.tpmVendorID);
250 }
251 }
252
253 METHOD(pts_t, get_platform_info, char*,
254 private_pts_t *this)
255 {
256 return this->platform_info;
257 }
258
259 METHOD(pts_t, set_platform_info, void,
260 private_pts_t *this, char *info)
261 {
262 free(this->platform_info);
263 this->platform_info = strdup(info);
264 }
265
266 METHOD(pts_t, get_tpm_version_info, bool,
267 private_pts_t *this, chunk_t *info)
268 {
269 if (!this->has_tpm)
270 {
271 return FALSE;
272 }
273 *info = this->tpm_version_info;
274 print_tpm_version_info(this);
275 return TRUE;
276 }
277
278 METHOD(pts_t, set_tpm_version_info, void,
279 private_pts_t *this, chunk_t info)
280 {
281 this->tpm_version_info = chunk_clone(info);
282 print_tpm_version_info(this);
283 }
284
285 /**
286 * Load an AIK certificate or public key,
287 * the certificate having precedence over the public key if both are present
288 */
289 static void load_aik(private_pts_t *this)
290 {
291 char *cert_path, *key_path;
292
293 cert_path = lib->settings->get_str(lib->settings,
294 "libimcv.plugins.imc-attestation.aik_cert", NULL);
295 key_path = lib->settings->get_str(lib->settings,
296 "libimcv.plugins.imc-attestation.aik_key", NULL);
297
298 if (cert_path)
299 {
300 this->aik = lib->creds->create(lib->creds, CRED_CERTIFICATE,
301 CERT_X509, BUILD_FROM_FILE,
302 cert_path, BUILD_END);
303 if (this->aik)
304 {
305 DBG2(DBG_PTS, "loaded AIK certificate from '%s'", cert_path);
306 return;
307 }
308 }
309 if (key_path)
310 {
311 this->aik = lib->creds->create(lib->creds, CRED_CERTIFICATE,
312 CERT_TRUSTED_PUBKEY, BUILD_FROM_FILE,
313 key_path, BUILD_END);
314 if (this->aik)
315 {
316 DBG2(DBG_PTS, "loaded AIK public key from '%s'", key_path);
317 return;
318 }
319 }
320 DBG1(DBG_PTS, "neither AIK certificate nor public key is available");
321 }
322
323 METHOD(pts_t, get_aik, certificate_t*,
324 private_pts_t *this)
325 {
326 return this->aik;
327 }
328
329 METHOD(pts_t, set_aik, void,
330 private_pts_t *this, certificate_t *aik)
331 {
332 DESTROY_IF(this->aik);
333 this->aik = aik->get_ref(aik);
334 }
335
336 METHOD(pts_t, hash_file, bool,
337 private_pts_t *this, hasher_t *hasher, char *pathname, u_char *hash)
338 {
339 u_char buffer[PTS_BUF_SIZE];
340 FILE *file;
341 int bytes_read;
342
343 file = fopen(pathname, "rb");
344 if (!file)
345 {
346 DBG1(DBG_PTS," file '%s' can not be opened, %s", pathname,
347 strerror(errno));
348 return FALSE;
349 }
350 while (TRUE)
351 {
352 bytes_read = fread(buffer, 1, sizeof(buffer), file);
353 if (bytes_read > 0)
354 {
355 hasher->get_hash(hasher, chunk_create(buffer, bytes_read), NULL);
356 }
357 else
358 {
359 hasher->get_hash(hasher, chunk_empty, hash);
360 break;
361 }
362 }
363 fclose(file);
364
365 return TRUE;
366 }
367
368 /**
369 * Get the relative filename of a fully qualified file pathname
370 */
371 static char* get_filename(char *pathname)
372 {
373 char *pos, *filename;
374
375 pos = filename = pathname;
376 while (pos && *(++pos) != '\0')
377 {
378 filename = pos;
379 pos = strchr(filename, '/');
380 }
381 return filename;
382 }
383
384 METHOD(pts_t, is_path_valid, bool, private_pts_t *this, char *path,
385 pts_error_code_t *error_code)
386 {
387 struct stat st;
388
389 *error_code = 0;
390
391 if (!stat(path, &st))
392 {
393 return TRUE;
394 }
395 else if (errno == ENOENT || errno == ENOTDIR)
396 {
397 DBG1(DBG_PTS, "file/directory does not exist %s", path);
398 *error_code = TCG_PTS_FILE_NOT_FOUND;
399 }
400 else if (errno == EFAULT)
401 {
402 DBG1(DBG_PTS, "bad address %s", path);
403 *error_code = TCG_PTS_INVALID_PATH;
404 }
405 else
406 {
407 DBG1(DBG_PTS, "error: %s occured while validating path: %s",
408 strerror(errno), path);
409 return FALSE;
410 }
411
412 return TRUE;
413 }
414
415 METHOD(pts_t, do_measurements, pts_file_meas_t*,
416 private_pts_t *this, u_int16_t request_id, char *pathname, bool is_directory)
417 {
418 hasher_t *hasher;
419 hash_algorithm_t hash_alg;
420 u_char hash[HASH_SIZE_SHA384];
421 chunk_t measurement;
422 pts_file_meas_t *measurements;
423
424 /* Create a hasher */
425 hash_alg = pts_meas_to_hash_algorithm(this->algorithm);
426 hasher = lib->crypto->create_hasher(lib->crypto, hash_alg);
427 if (!hasher)
428 {
429 DBG1(DBG_PTS, " hasher %N not available", hash_algorithm_names, hash_alg);
430 return NULL;
431 }
432
433 /* Create a measurement object */
434 measurements = pts_file_meas_create(request_id);
435
436 /* Link the hash to the measurement and set the measurement length */
437 measurement = chunk_create(hash, hasher->get_hash_size(hasher));
438
439 if (is_directory)
440 {
441 enumerator_t *enumerator;
442 char *rel_name, *abs_name;
443 struct stat st;
444
445 enumerator = enumerator_create_directory(pathname);
446 if (!enumerator)
447 {
448 DBG1(DBG_PTS," directory '%s' can not be opened, %s", pathname,
449 strerror(errno));
450 hasher->destroy(hasher);
451 measurements->destroy(measurements);
452 return NULL;
453 }
454 while (enumerator->enumerate(enumerator, &rel_name, &abs_name, &st))
455 {
456 /* measure regular files only */
457 if (S_ISREG(st.st_mode) && *rel_name != '.')
458 {
459 if (!hash_file(this, hasher, abs_name, hash))
460 {
461 enumerator->destroy(enumerator);
462 hasher->destroy(hasher);
463 measurements->destroy(measurements);
464 return NULL;
465 }
466 DBG2(DBG_PTS, " %#B for '%s'", &measurement, rel_name);
467 measurements->add(measurements, rel_name, measurement);
468 }
469 }
470 enumerator->destroy(enumerator);
471 }
472 else
473 {
474 char *filename;
475
476 if (!hash_file(this, hasher, pathname, hash))
477 {
478 hasher->destroy(hasher);
479 measurements->destroy(measurements);
480 return NULL;
481 }
482 filename = get_filename(pathname);
483 DBG2(DBG_PTS, " %#B for '%s'", &measurement, filename);
484 measurements->add(measurements, filename, measurement);
485 }
486 hasher->destroy(hasher);
487
488 return measurements;
489 }
490
491 /**
492 * Obtain statistical information describing a file
493 */
494 static bool file_metadata(char *pathname, pts_file_metadata_t **entry)
495 {
496 struct stat st;
497 pts_file_metadata_t *tmp;
498
499 tmp = malloc_thing(pts_file_metadata_t);
500
501 if (stat(pathname, &st))
502 {
503 DBG1(DBG_PTS, "Unable to obtain statistical information about %s", pathname);
504 return FALSE;
505 }
506
507 tmp->filename = strdup(pathname);
508 tmp->meta_length = PTS_FILE_METADATA_SIZE + strlen(tmp->filename);
509
510 if (S_ISREG(st.st_mode))
511 {
512 tmp->type = PTS_FILE_REGULAR;
513 }
514 else if (S_ISDIR(st.st_mode))
515 {
516 tmp->type = PTS_FILE_DIRECTORY;
517 }
518 else if (S_ISCHR(st.st_mode))
519 {
520 tmp->type = PTS_FILE_CHAR_SPEC;
521 }
522 else if (S_ISBLK(st.st_mode))
523 {
524 tmp->type = PTS_FILE_BLOCK_SPEC;
525 }
526 else if (S_ISFIFO(st.st_mode))
527 {
528 tmp->type = PTS_FILE_FIFO;
529 }
530 else if (S_ISLNK(st.st_mode))
531 {
532 tmp->type = PTS_FILE_SYM_LINK;
533 }
534 else if (S_ISSOCK(st.st_mode))
535 {
536 tmp->type = PTS_FILE_SOCKET;
537 }
538 else
539 {
540 tmp->type = PTS_FILE_OTHER;
541 }
542
543 tmp->filesize = (u_int64_t)st.st_size;
544 tmp->create_time = st.st_ctime;
545 tmp->last_modify_time = st.st_mtime;
546 tmp->last_access_time = st.st_atime;
547 tmp->owner_id = (u_int64_t)st.st_uid;
548 tmp->group_id = (u_int64_t)st.st_gid;
549
550 *entry = tmp;
551
552 return TRUE;
553 }
554
555 METHOD(pts_t, get_metadata, pts_file_meta_t*,
556 private_pts_t *this, char *pathname, bool is_directory)
557 {
558 pts_file_meta_t *metadata;
559 pts_file_metadata_t *entry;
560
561 /* Create a metadata object */
562 metadata = pts_file_meta_create();
563
564 if (is_directory)
565 {
566 enumerator_t *enumerator;
567 char *rel_name, *abs_name;
568 struct stat st;
569
570 enumerator = enumerator_create_directory(pathname);
571 if (!enumerator)
572 {
573 DBG1(DBG_PTS," directory '%s' can not be opened, %s", pathname,
574 strerror(errno));
575 metadata->destroy(metadata);
576 return NULL;
577 }
578 while (enumerator->enumerate(enumerator, &rel_name, &abs_name, &st))
579 {
580 /* measure regular files only */
581 if (S_ISREG(st.st_mode) && *rel_name != '.')
582 {
583 if (!file_metadata(abs_name, &entry))
584 {
585 enumerator->destroy(enumerator);
586 metadata->destroy(metadata);
587 return NULL;
588 }
589 DBG3(DBG_PTS, "File name: %s", entry->filename);
590 DBG3(DBG_PTS, " type: %d", entry->type);
591 DBG3(DBG_PTS, " size: %d", entry->filesize);
592 DBG3(DBG_PTS, " create time: %s", ctime(&entry->create_time));
593 DBG3(DBG_PTS, " last modified: %s", ctime(&entry->last_modify_time));
594 DBG3(DBG_PTS, " last accessed: %s", ctime(&entry->last_access_time));
595 DBG3(DBG_PTS, " owner id: %d", entry->owner_id);
596 DBG3(DBG_PTS, " group id: %d", entry->group_id);
597
598 metadata->add(metadata, entry);
599 }
600 }
601 enumerator->destroy(enumerator);
602 }
603 else
604 {
605 char *filename;
606
607 if (!file_metadata(pathname, &entry))
608 {
609 metadata->destroy(metadata);
610 return NULL;
611 }
612 filename = get_filename(pathname);
613 DBG3(DBG_PTS, "File name: %s", entry->filename);
614 DBG3(DBG_PTS, " type: %d", entry->type);
615 DBG3(DBG_PTS, " size: %d", entry->filesize);
616 DBG3(DBG_PTS, " create time: %s", ctime(&entry->create_time));
617 DBG3(DBG_PTS, " last modified: %s", ctime(&entry->last_modify_time));
618 DBG3(DBG_PTS, " last accessed: %s", ctime(&entry->last_access_time));
619 DBG3(DBG_PTS, " owner id: %d", entry->owner_id);
620 DBG3(DBG_PTS, " group id: %d", entry->group_id);
621 metadata->add(metadata, entry);
622 }
623
624 return metadata;
625 }
626
627 METHOD(pts_t, read_pcr, bool,
628 private_pts_t *this, u_int32_t pcr_num, chunk_t *output)
629 {
630 TSS_HCONTEXT hContext;
631 TSS_HTPM hTPM;
632 TSS_RESULT result;
633 u_int32_t pcr_length;
634 chunk_t pcr_value;
635
636 result = Tspi_Context_Create(&hContext);
637 if (result != TSS_SUCCESS)
638 {
639 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x", result);
640 return FALSE;
641 }
642
643 result = Tspi_Context_Connect(hContext, NULL);
644 if (result != TSS_SUCCESS)
645 {
646 goto err;
647 }
648 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
649 if (result != TSS_SUCCESS)
650 {
651 goto err;
652 }
653 pcr_value = chunk_alloc(PCR_LEN);
654 result = Tspi_TPM_PcrRead(hTPM, pcr_num, &pcr_length, &pcr_value.ptr);
655 if (result != TSS_SUCCESS)
656 {
657 goto err;
658 }
659
660 *output = pcr_value;
661 *output = chunk_clone(*output);
662
663 chunk_clear(&pcr_value);
664 Tspi_Context_Close(hContext);
665 DBG3(DBG_PTS, "PCR %d value:%B", pcr_num, output);
666 return TRUE;
667
668 err:
669 chunk_clear(&pcr_value);
670 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
671 Tspi_Context_Close(hContext);
672 return FALSE;
673 }
674
675 METHOD(pts_t, extend_pcr, bool,
676 private_pts_t *this, u_int32_t pcr_num, chunk_t input, chunk_t *output)
677 {
678 TSS_HCONTEXT hContext;
679 TSS_HTPM hTPM;
680 TSS_RESULT result;
681 u_int32_t pcr_length;
682 chunk_t pcr_value;
683
684 result = Tspi_Context_Create(&hContext);
685 if (result != TSS_SUCCESS)
686 {
687 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x", result);
688 return FALSE;
689 }
690 result = Tspi_Context_Connect(hContext, NULL);
691 if (result != TSS_SUCCESS)
692 {
693 goto err;
694 }
695 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
696 if (result != TSS_SUCCESS)
697 {
698 goto err;
699 }
700
701 pcr_value = chunk_alloc(PCR_LEN);
702 result = Tspi_TPM_PcrExtend(hTPM, pcr_num, PCR_LEN, input.ptr,
703 NULL, &pcr_length, &pcr_value.ptr);
704 if (result != TSS_SUCCESS)
705 {
706 goto err;
707 }
708
709 *output = pcr_value;
710 *output = chunk_clone(*output);
711
712 chunk_clear(&pcr_value);
713 Tspi_Context_Close(hContext);
714 DBG3(DBG_PTS, "PCR %d extended with: %B", pcr_num, &input);
715 DBG3(DBG_PTS, "PCR %d value after extend: %B", pcr_num, output);
716 return TRUE;
717
718 err:
719 chunk_clear(&pcr_value);
720 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
721 Tspi_Context_Close(hContext);
722 return FALSE;
723 }
724
725 METHOD(pts_t, quote_tpm, bool,
726 private_pts_t *this, linked_list_t *pcrs,
727 chunk_t *pcr_composite, chunk_t *quote_signature)
728 {
729 TSS_HCONTEXT hContext;
730 TSS_HTPM hTPM;
731 TSS_HKEY hAIK;
732 TSS_HKEY hSRK;
733 TSS_HPOLICY srkUsagePolicy;
734 TSS_UUID SRK_UUID = TSS_UUID_SRK;
735 BYTE secret[] = TSS_WELL_KNOWN_SECRET;
736 TSS_HPCRS hPcrComposite;
737 TSS_VALIDATION valData;
738 TPM_QUOTE_INFO *quoteInfo;
739 u_int32_t i, pcr;
740 TSS_RESULT result;
741 chunk_t aik_key_encoding;
742 chunk_t pcr_composite_without_nonce;
743 enumerator_t *enumerator;
744
745 result = Tspi_Context_Create(&hContext);
746 if (result != TSS_SUCCESS)
747 {
748 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x", result);
749 return FALSE;
750 }
751 result = Tspi_Context_Connect(hContext, NULL);
752 if (result != TSS_SUCCESS)
753 {
754 goto err1;
755 }
756 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
757 if (result != TSS_SUCCESS)
758 {
759 goto err1;
760 }
761
762 /* Retrieve SRK from TPM and set the authentication data as well known secret*/
763 result = Tspi_Context_LoadKeyByUUID(hContext, TSS_PS_TYPE_SYSTEM,
764 SRK_UUID, &hSRK);
765 if (result != TSS_SUCCESS)
766 {
767 goto err1;
768 }
769
770 result = Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &srkUsagePolicy);
771 if (result != TSS_SUCCESS)
772 {
773 goto err1;
774 }
775 result = Tspi_Policy_SetSecret(srkUsagePolicy, TSS_SECRET_MODE_SHA1,
776 20, secret);
777 if (result != TSS_SUCCESS)
778 {
779 goto err1;
780 }
781
782 /* Create from AIK public key a HKEY object to sign Quote operation output*/
783 if (this->aik->get_type(this->aik) == CERT_TRUSTED_PUBKEY)
784 {
785 if (!this->aik->get_encoding(this->aik, CERT_ASN1_DER, &aik_key_encoding))
786 {
787 DBG1(DBG_PTS, "encoding AIK certificate for quote operation failed");
788 goto err1;
789 }
790 }
791 else if (this->aik->get_type(this->aik) == CERT_X509)
792 {
793 public_key_t *key = this->aik->get_public_key(this->aik);
794
795 if (key == NULL)
796 {
797 DBG1(DBG_PTS, "unable to retrieve public key from AIK certificate");
798 goto err1;
799 }
800 if (!key->get_encoding(key, PUBKEY_ASN1_DER, &aik_key_encoding))
801 {
802 DBG1(DBG_PTS, "encoding AIK Public Key for quote operation failed");
803 goto err1;
804 }
805 }
806 else
807 {
808 DBG1(DBG_PTS, "AIK is neither X509 certificate nor Public Key");
809 goto err1;
810 }
811
812 result = Tspi_Context_LoadKeyByBlob (hContext, hSRK, aik_key_encoding.len,
813 (BYTE*)aik_key_encoding.ptr, &hAIK);
814 if (result != TSS_SUCCESS)
815 {
816 goto err1;
817 }
818
819 /* Create PCR composite object */
820 result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_PCRS, 0, &hPcrComposite);
821 if (result != TSS_SUCCESS)
822 {
823 goto err2;
824 }
825
826 /* Select PCR's */
827 enumerator = pcrs->create_enumerator(pcrs);
828 while (enumerator->enumerate(enumerator, &pcr))
829 {
830 if (pcr < 0 || pcr >= MAX_NUM_PCR )
831 {
832 DBG1(DBG_PTS, "Invalid PCR number: %d", pcr);
833 goto err3;
834 }
835 result = Tspi_PcrComposite_SelectPcrIndex(hPcrComposite, pcr);
836 if (result != TSS_SUCCESS)
837 {
838 goto err3;
839 }
840 }
841 enumerator->destroy(enumerator);
842
843 /* Set the Validation Data */
844 valData.ulExternalDataLength = this->secret.len;
845 valData.rgbExternalData = (BYTE *)this->secret.ptr;
846
847 /* TPM Quote */
848 result = Tspi_TPM_Quote(hTPM, hAIK, hPcrComposite, &valData);
849 if (result != TSS_SUCCESS)
850 {
851 goto err4;
852 }
853
854 quoteInfo = (TPM_QUOTE_INFO *)valData.rgbData;
855
856 /* Display quote info */
857 DBG3(DBG_PTS, "version:");
858 for(i = 0 ; i < 4 ; i++)
859 {
860 DBG3(DBG_PTS, "%02x ",valData.rgbData[i]);
861 }
862 DBG3(DBG_PTS, "fixed value:");
863 for(i = 4 ; i < 8 ; i++)
864 {
865 DBG3(DBG_PTS, "%c",valData.rgbData[i]);
866 }
867 DBG3(DBG_PTS, "pcr digest:");
868 for(i = 8 ; i < 28 ; i++)
869 {
870 DBG3(DBG_PTS, "%02x ",valData.rgbData[i]);
871 }
872 DBG3(DBG_PTS, "nonce:");
873 for(i = 28 ; i < valData.ulDataLength ; i++)
874 {
875 DBG3(DBG_PTS, "%c",valData.rgbData[i]);
876 }
877
878 /* Set output chunks */
879 pcr_composite_without_nonce = chunk_alloc(
880 valData.ulDataLength - ASSESSMENT_SECRET_LEN);
881 memcpy(pcr_composite_without_nonce.ptr, valData.rgbData,
882 valData.ulDataLength - ASSESSMENT_SECRET_LEN);
883 *pcr_composite = pcr_composite_without_nonce;
884 *pcr_composite = chunk_clone(*pcr_composite);
885 free(pcr_composite_without_nonce.ptr);
886
887 *quote_signature = chunk_from_thing(valData.rgbValidationData);
888 *quote_signature = chunk_clone(*quote_signature);
889
890 Tspi_Context_FreeMemory(hContext, NULL);
891 Tspi_Context_CloseObject(hContext, hPcrComposite);
892 Tspi_Context_CloseObject(hContext, hAIK);
893 Tspi_Context_Close(hContext);
894 return TRUE;
895
896 /* Cleanup */
897 err4:
898 Tspi_Context_FreeMemory(hContext, NULL);
899
900 err3:
901 Tspi_Context_CloseObject(hContext, hPcrComposite);
902
903 err2:
904 Tspi_Context_CloseObject(hContext, hAIK);
905
906 err1:
907 Tspi_Context_Close(hContext);
908 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
909 return FALSE;
910 }
911
912 METHOD(pts_t, destroy, void,
913 private_pts_t *this)
914 {
915 DESTROY_IF(this->aik);
916 DESTROY_IF(this->dh);
917 free(this->platform_info);
918 free(this->tpm_version_info.ptr);
919 free(this);
920 }
921
922 /**
923 * Determine Linux distribution and hardware platform
924 */
925 static char* extract_platform_info(void)
926 {
927 FILE *file;
928 char buf[BUF_LEN], *pos, *value = NULL;
929 int i, len;
930 struct utsname uninfo;
931
932 /* Linux/Unix distribution release info (from http://linuxmafia.com) */
933 const char* releases[] = {
934 "/etc/lsb-release", "/etc/debian_version",
935 "/etc/SuSE-release", "/etc/novell-release",
936 "/etc/sles-release", "/etc/redhat-release",
937 "/etc/fedora-release", "/etc/gentoo-release",
938 "/etc/slackware-version", "/etc/annvix-release",
939 "/etc/arch-release", "/etc/arklinux-release",
940 "/etc/aurox-release", "/etc/blackcat-release",
941 "/etc/cobalt-release", "/etc/conectiva-release",
942 "/etc/debian_release", "/etc/immunix-release",
943 "/etc/lfs-release", "/etc/linuxppc-release",
944 "/etc/mandrake-release", "/etc/mandriva-release",
945 "/etc/mandrakelinux-release", "/etc/mklinux-release",
946 "/etc/pld-release", "/etc/redhat_version",
947 "/etc/slackware-release", "/etc/e-smith-release",
948 "/etc/release", "/etc/sun-release",
949 "/etc/tinysofa-release", "/etc/turbolinux-release",
950 "/etc/ultrapenguin-release", "/etc/UnitedLinux-release",
951 "/etc/va-release", "/etc/yellowdog-release"
952 };
953
954 const char description[] = "DISTRIB_DESCRIPTION=\"";
955
956 for (i = 0; i < countof(releases); i++)
957 {
958 file = fopen(releases[i], "r");
959 if (!file)
960 {
961 continue;
962 }
963 fseek(file, 0, SEEK_END);
964 len = min(ftell(file), sizeof(buf)-1);
965 rewind(file);
966 buf[len] = '\0';
967 if (fread(buf, 1, len, file) != len)
968 {
969 DBG1(DBG_PTS, "failed to read file '%s'", releases[i]);
970 fclose(file);
971 return NULL;
972 }
973 fclose(file);
974
975 if (i == 0) /* LSB release */
976 {
977 pos = strstr(buf, description);
978 if (!pos)
979 {
980 DBG1(DBG_PTS, "failed to find begin of lsb-release "
981 "DESCRIPTION field");
982 return NULL;
983 }
984 value = pos + strlen(description);
985 pos = strchr(value, '"');
986 if (!pos)
987 {
988 DBG1(DBG_PTS, "failed to find end of lsb-release "
989 "DESCRIPTION field");
990 return NULL;
991 }
992 }
993 else
994 {
995 value = buf;
996 pos = strchr(value, '\n');
997 if (!pos)
998 {
999 DBG1(DBG_PTS, "failed to find end of release string");
1000 return NULL;
1001 }
1002 }
1003 break;
1004 }
1005
1006 if (!value)
1007 {
1008 DBG1(DBG_PTS, "no distribution release file found");
1009 return NULL;
1010 }
1011
1012 if (uname(&uninfo) < 0)
1013 {
1014 DBG1(DBG_PTS, "could not retrieve machine architecture");
1015 return NULL;
1016 }
1017
1018 *pos++ = ' ';
1019 len = sizeof(buf)-1 + (pos - buf);
1020 strncpy(pos, uninfo.machine, len);
1021
1022 DBG1(DBG_PTS, "platform is '%s'", value);
1023 return strdup(value);
1024 }
1025
1026 /**
1027 * Check for a TPM by querying for TPM Version Info
1028 */
1029 static bool has_tpm(private_pts_t *this)
1030 {
1031 TSS_HCONTEXT hContext;
1032 TSS_HTPM hTPM;
1033 TSS_RESULT result;
1034 u_int32_t version_info_len;
1035
1036 result = Tspi_Context_Create(&hContext);
1037 if (result != TSS_SUCCESS)
1038 {
1039 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x", result);
1040 return FALSE;
1041 }
1042 result = Tspi_Context_Connect(hContext, NULL);
1043 if (result != TSS_SUCCESS)
1044 {
1045 goto err;
1046 }
1047 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
1048 if (result != TSS_SUCCESS)
1049 {
1050 goto err;
1051 }
1052 result = Tspi_TPM_GetCapability(hTPM, TSS_TPMCAP_VERSION_VAL, 0, NULL,
1053 &version_info_len,
1054 &this->tpm_version_info.ptr);
1055 this->tpm_version_info.len = version_info_len;
1056 if (result != TSS_SUCCESS)
1057 {
1058 goto err;
1059 }
1060 this->tpm_version_info = chunk_clone(this->tpm_version_info);
1061 Tspi_Context_Close(hContext);
1062 return TRUE;
1063
1064 err:
1065 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
1066 Tspi_Context_Close(hContext);
1067 return FALSE;
1068 }
1069
1070 /**
1071 * See header
1072 */
1073 pts_t *pts_create(bool is_imc)
1074 {
1075 private_pts_t *this;
1076
1077 INIT(this,
1078 .public = {
1079 .get_proto_caps = _get_proto_caps,
1080 .set_proto_caps = _set_proto_caps,
1081 .get_meas_algorithm = _get_meas_algorithm,
1082 .set_meas_algorithm = _set_meas_algorithm,
1083 .get_dh_group = _get_dh_group,
1084 .set_dh_group = _set_dh_group,
1085 .create_dh = _create_dh,
1086 .get_my_pub_val = _get_my_pub_val,
1087 .set_other_pub_val = _set_other_pub_val,
1088 .calculate_secret = _calculate_secret,
1089 .get_secret = _get_secret,
1090 .get_platform_info = _get_platform_info,
1091 .set_platform_info = _set_platform_info,
1092 .get_tpm_version_info = _get_tpm_version_info,
1093 .set_tpm_version_info = _set_tpm_version_info,
1094 .get_aik = _get_aik,
1095 .set_aik = _set_aik,
1096 .is_path_valid = _is_path_valid,
1097 .hash_file = _hash_file,
1098 .do_measurements = _do_measurements,
1099 .get_metadata = _get_metadata,
1100 .read_pcr = _read_pcr,
1101 .extend_pcr = _extend_pcr,
1102 .quote_tpm = _quote_tpm,
1103 .destroy = _destroy,
1104 },
1105 .proto_caps = PTS_PROTO_CAPS_V,
1106 .algorithm = PTS_MEAS_ALGO_SHA256,
1107 .dh_group = PTS_DH_GROUP_IKE19,
1108 );
1109
1110 if (is_imc)
1111 {
1112 this->platform_info = extract_platform_info();
1113
1114 if (has_tpm(this))
1115 {
1116 this->has_tpm = TRUE;
1117 this->proto_caps |= PTS_PROTO_CAPS_T;
1118 load_aik(this);
1119 }
1120 }
1121 else
1122 {
1123 this->proto_caps |= PTS_PROTO_CAPS_T | PTS_PROTO_CAPS_C;
1124 }
1125
1126 return &this->public;
1127 }
1128