Changed definition of output and set them in quote_tpm function
[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 result = Tspi_TPM_PcrRead(hTPM, pcr_num, &pcr_length, &pcr_value.ptr);
654 if (result != TSS_SUCCESS)
655 {
656 goto err;
657 }
658
659 *output = pcr_value;
660 *output = chunk_clone(*output);
661
662 Tspi_Context_Close(hContext);
663 DBG3(DBG_PTS, "PCR %d value:%B", pcr_num, output);
664 return TRUE;
665
666 err:
667 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
668 Tspi_Context_Close(hContext);
669 return FALSE;
670 }
671
672 METHOD(pts_t, extend_pcr, bool,
673 private_pts_t *this, u_int32_t pcr_num, chunk_t input, chunk_t *output)
674 {
675 TSS_HCONTEXT hContext;
676 TSS_HTPM hTPM;
677 TSS_RESULT result;
678 u_int32_t pcr_length;
679 chunk_t pcr_value;
680
681 result = Tspi_Context_Create(&hContext);
682 if (result != TSS_SUCCESS)
683 {
684 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x", result);
685 return FALSE;
686 }
687 result = Tspi_Context_Connect(hContext, NULL);
688 if (result != TSS_SUCCESS)
689 {
690 goto err;
691 }
692 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
693 if (result != TSS_SUCCESS)
694 {
695 goto err;
696 }
697 result = Tspi_TPM_PcrExtend(hTPM, pcr_num, 20, input.ptr, NULL, &pcr_length, &pcr_value.ptr);
698 if (result != TSS_SUCCESS)
699 {
700 goto err;
701 }
702
703 *output = pcr_value;
704 *output = chunk_clone(*output);
705
706 Tspi_Context_Close(hContext);
707 DBG3(DBG_PTS, "PCR %d extended with: %B", pcr_num, &input);
708 DBG3(DBG_PTS, "PCR %d value after extend: %B", pcr_num, output);
709
710 return TRUE;
711
712 err:
713 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
714 Tspi_Context_Close(hContext);
715 return FALSE;
716 }
717
718 METHOD(pts_t, quote_tpm, bool,
719 private_pts_t *this, u_int32_t *pcrs, u_int32_t num_of_pcrs,
720 chunk_t *pcr_composite, chunk_t *quote_signature)
721 {
722 TSS_HCONTEXT hContext;
723 TSS_HTPM hTPM;
724 TSS_HKEY hAIK;
725 TSS_HKEY hSRK;
726 TSS_HPOLICY srkUsagePolicy;
727 TSS_UUID SRK_UUID = TSS_UUID_SRK;
728 BYTE secret[] = TSS_WELL_KNOWN_SECRET;
729 TSS_HPCRS hPcrComposite;
730 TSS_VALIDATION valData;
731 TPM_QUOTE_INFO *quoteInfo;
732 u_int32_t i;
733 TSS_RESULT result;
734 chunk_t aik_key_encoding;
735 chunk_t pcr_composite_without_nonce;
736
737 result = Tspi_Context_Create(&hContext);
738 if (result != TSS_SUCCESS)
739 {
740 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x", result);
741 return FALSE;
742 }
743 result = Tspi_Context_Connect(hContext, NULL);
744 if (result != TSS_SUCCESS)
745 {
746 goto err1;
747 }
748 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
749 if (result != TSS_SUCCESS)
750 {
751 goto err1;
752 }
753
754 /* Retrieve SRK from TPM and set the authentication data as well known secret*/
755 result = Tspi_Context_LoadKeyByUUID(hContext, TSS_PS_TYPE_SYSTEM,
756 SRK_UUID, &hSRK);
757 if (result != TSS_SUCCESS)
758 {
759 goto err1;
760 }
761
762 result = Tspi_GetPolicyObject(hSRK, TSS_POLICY_USAGE, &srkUsagePolicy);
763 if (result != TSS_SUCCESS)
764 {
765 goto err1;
766 }
767 result = Tspi_Policy_SetSecret(srkUsagePolicy, TSS_SECRET_MODE_SHA1,
768 20, secret);
769 if (result != TSS_SUCCESS)
770 {
771 goto err1;
772 }
773
774 /* Create from AIK public key a HKEY object to sign Quote operation output*/
775 if (this->aik->get_type(this->aik) == CERT_TRUSTED_PUBKEY)
776 {
777 if (!this->aik->get_encoding(this->aik, CERT_ASN1_DER, &aik_key_encoding))
778 {
779 DBG1(DBG_PTS, "encoding AIK certificate for quote operation failed");
780 goto err1;
781 }
782 }
783 else if (this->aik->get_type(this->aik) == CERT_X509)
784 {
785 public_key_t *key = this->aik->get_public_key(this->aik);
786
787 if (key == NULL)
788 {
789 DBG1(DBG_PTS, "unable to retrieve public key from AIK certificate");
790 goto err1;
791 }
792 if (!key->get_encoding(key, PUBKEY_ASN1_DER, &aik_key_encoding))
793 {
794 DBG1(DBG_PTS, "encoding AIK Public Key for quote operation failed");
795 goto err1;
796 }
797 }
798 else
799 {
800 DBG1(DBG_PTS, "AIK is neither X509 certificate nor Public Key");
801 goto err1;
802 }
803
804 result = Tspi_Context_LoadKeyByBlob (hContext, hSRK, aik_key_encoding.len,
805 (BYTE*)aik_key_encoding.ptr, &hAIK);
806 if (result != TSS_SUCCESS)
807 {
808 goto err1;
809 }
810
811 /* Create PCR composite object */
812 result = Tspi_Context_CreateObject(hContext, TSS_OBJECT_TYPE_PCRS, 0, &hPcrComposite);
813 if (result != TSS_SUCCESS)
814 {
815 goto err2;
816 }
817
818 /* Select PCR's */
819 for (i = 0; i < num_of_pcrs; i++)
820 {
821 u_int32_t pcr = pcrs[i];
822 if (pcr < 0 || pcr >= MAX_NUM_PCR )
823 {
824 DBG1(DBG_PTS, "Invalid PCR number: %d", pcr);
825 goto err3;
826 }
827 result = Tspi_PcrComposite_SelectPcrIndex(hPcrComposite, pcr);
828 if (result != TSS_SUCCESS)
829 {
830 goto err3;
831 }
832 }
833
834 /* Set the Validation Data */
835 valData.ulExternalDataLength = this->secret.len;
836 valData.rgbExternalData = (BYTE *)this->secret.ptr;
837
838 /* TPM Quote */
839 result = Tspi_TPM_Quote(hTPM, hAIK, hPcrComposite, &valData);
840 if (result != TSS_SUCCESS)
841 {
842 goto err4;
843 }
844
845 quoteInfo = (TPM_QUOTE_INFO *)valData.rgbData;
846
847 /* Display quote info */
848 DBG3(DBG_PTS, "version:");
849 for(i = 0 ; i < 4 ; i++)
850 {
851 DBG3(DBG_PTS, "%02x ",valData.rgbData[i]);
852 }
853 DBG3(DBG_PTS, "fixed value:");
854 for(i = 4 ; i < 8 ; i++)
855 {
856 DBG3(DBG_PTS, "%c",valData.rgbData[i]);
857 }
858 DBG3(DBG_PTS, "pcr digest:");
859 for(i = 8 ; i < 28 ; i++)
860 {
861 DBG3(DBG_PTS, "%02x ",valData.rgbData[i]);
862 }
863 DBG3(DBG_PTS, "nonce:");
864 for(i = 28 ; i < valData.ulDataLength ; i++)
865 {
866 DBG3(DBG_PTS, "%c",valData.rgbData[i]);
867 }
868
869 /* Set output chunks */
870 pcr_composite_without_nonce = chunk_alloc(
871 valData.ulDataLength - ASSESSMENT_SECRET_LEN);
872 memcpy(pcr_composite_without_nonce.ptr, valData.rgbData,
873 valData.ulDataLength - ASSESSMENT_SECRET_LEN);
874 *pcr_composite = pcr_composite_without_nonce;
875 *quote_signature = chunk_from_thing(valData.rgbValidationData);
876 *quote_signature = chunk_clone(*quote_signature);
877
878 Tspi_Context_FreeMemory(hContext, NULL);
879 Tspi_Context_CloseObject(hContext, hPcrComposite);
880 Tspi_Context_CloseObject(hContext, hAIK);
881 Tspi_Context_Close(hContext);
882 return TRUE;
883
884 /* Cleanup */
885 err4:
886 Tspi_Context_FreeMemory(hContext, NULL);
887
888 err3:
889 Tspi_Context_CloseObject(hContext, hPcrComposite);
890
891 err2:
892 Tspi_Context_CloseObject(hContext, hAIK);
893
894 err1:
895 Tspi_Context_Close(hContext);
896 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
897 return FALSE;
898 }
899
900 METHOD(pts_t, destroy, void,
901 private_pts_t *this)
902 {
903 DESTROY_IF(this->aik);
904 DESTROY_IF(this->dh);
905 free(this->platform_info);
906 free(this->tpm_version_info.ptr);
907 free(this);
908 }
909
910 /**
911 * Determine Linux distribution and hardware platform
912 */
913 static char* extract_platform_info(void)
914 {
915 FILE *file;
916 char buf[BUF_LEN], *pos, *value = NULL;
917 int i, len;
918 struct utsname uninfo;
919
920 /* Linux/Unix distribution release info (from http://linuxmafia.com) */
921 const char* releases[] = {
922 "/etc/lsb-release", "/etc/debian_version",
923 "/etc/SuSE-release", "/etc/novell-release",
924 "/etc/sles-release", "/etc/redhat-release",
925 "/etc/fedora-release", "/etc/gentoo-release",
926 "/etc/slackware-version", "/etc/annvix-release",
927 "/etc/arch-release", "/etc/arklinux-release",
928 "/etc/aurox-release", "/etc/blackcat-release",
929 "/etc/cobalt-release", "/etc/conectiva-release",
930 "/etc/debian_release", "/etc/immunix-release",
931 "/etc/lfs-release", "/etc/linuxppc-release",
932 "/etc/mandrake-release", "/etc/mandriva-release",
933 "/etc/mandrakelinux-release", "/etc/mklinux-release",
934 "/etc/pld-release", "/etc/redhat_version",
935 "/etc/slackware-release", "/etc/e-smith-release",
936 "/etc/release", "/etc/sun-release",
937 "/etc/tinysofa-release", "/etc/turbolinux-release",
938 "/etc/ultrapenguin-release", "/etc/UnitedLinux-release",
939 "/etc/va-release", "/etc/yellowdog-release"
940 };
941
942 const char description[] = "DISTRIB_DESCRIPTION=\"";
943
944 for (i = 0; i < countof(releases); i++)
945 {
946 file = fopen(releases[i], "r");
947 if (!file)
948 {
949 continue;
950 }
951 fseek(file, 0, SEEK_END);
952 len = min(ftell(file), sizeof(buf)-1);
953 rewind(file);
954 buf[len] = '\0';
955 if (fread(buf, 1, len, file) != len)
956 {
957 DBG1(DBG_PTS, "failed to read file '%s'", releases[i]);
958 fclose(file);
959 return NULL;
960 }
961 fclose(file);
962
963 if (i == 0) /* LSB release */
964 {
965 pos = strstr(buf, description);
966 if (!pos)
967 {
968 DBG1(DBG_PTS, "failed to find begin of lsb-release "
969 "DESCRIPTION field");
970 return NULL;
971 }
972 value = pos + strlen(description);
973 pos = strchr(value, '"');
974 if (!pos)
975 {
976 DBG1(DBG_PTS, "failed to find end of lsb-release "
977 "DESCRIPTION field");
978 return NULL;
979 }
980 }
981 else
982 {
983 value = buf;
984 pos = strchr(value, '\n');
985 if (!pos)
986 {
987 DBG1(DBG_PTS, "failed to find end of release string");
988 return NULL;
989 }
990 }
991 break;
992 }
993
994 if (!value)
995 {
996 DBG1(DBG_PTS, "no distribution release file found");
997 return NULL;
998 }
999
1000 if (uname(&uninfo) < 0)
1001 {
1002 DBG1(DBG_PTS, "could not retrieve machine architecture");
1003 return NULL;
1004 }
1005
1006 *pos++ = ' ';
1007 len = sizeof(buf)-1 + (pos - buf);
1008 strncpy(pos, uninfo.machine, len);
1009
1010 DBG1(DBG_PTS, "platform is '%s'", value);
1011 return strdup(value);
1012 }
1013
1014 /**
1015 * Check for a TPM by querying for TPM Version Info
1016 */
1017 static bool has_tpm(private_pts_t *this)
1018 {
1019 TSS_HCONTEXT hContext;
1020 TSS_HTPM hTPM;
1021 TSS_RESULT result;
1022 u_int32_t version_info_len;
1023
1024 result = Tspi_Context_Create(&hContext);
1025 if (result != TSS_SUCCESS)
1026 {
1027 DBG1(DBG_PTS, "TPM context could not be created: tss error 0x%x", result);
1028 return FALSE;
1029 }
1030 result = Tspi_Context_Connect(hContext, NULL);
1031 if (result != TSS_SUCCESS)
1032 {
1033 goto err;
1034 }
1035 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
1036 if (result != TSS_SUCCESS)
1037 {
1038 goto err;
1039 }
1040 result = Tspi_TPM_GetCapability(hTPM, TSS_TPMCAP_VERSION_VAL, 0, NULL,
1041 &version_info_len,
1042 &this->tpm_version_info.ptr);
1043 this->tpm_version_info.len = version_info_len;
1044 if (result != TSS_SUCCESS)
1045 {
1046 goto err;
1047 }
1048 this->tpm_version_info = chunk_clone(this->tpm_version_info);
1049 Tspi_Context_Close(hContext);
1050 return TRUE;
1051
1052 err:
1053 DBG1(DBG_PTS, "TPM not available: tss error 0x%x", result);
1054 Tspi_Context_Close(hContext);
1055 return FALSE;
1056 }
1057
1058 /**
1059 * See header
1060 */
1061 pts_t *pts_create(bool is_imc)
1062 {
1063 private_pts_t *this;
1064
1065 INIT(this,
1066 .public = {
1067 .get_proto_caps = _get_proto_caps,
1068 .set_proto_caps = _set_proto_caps,
1069 .get_meas_algorithm = _get_meas_algorithm,
1070 .set_meas_algorithm = _set_meas_algorithm,
1071 .get_dh_group = _get_dh_group,
1072 .set_dh_group = _set_dh_group,
1073 .create_dh = _create_dh,
1074 .get_my_pub_val = _get_my_pub_val,
1075 .set_other_pub_val = _set_other_pub_val,
1076 .calculate_secret = _calculate_secret,
1077 .get_secret = _get_secret,
1078 .get_platform_info = _get_platform_info,
1079 .set_platform_info = _set_platform_info,
1080 .get_tpm_version_info = _get_tpm_version_info,
1081 .set_tpm_version_info = _set_tpm_version_info,
1082 .get_aik = _get_aik,
1083 .set_aik = _set_aik,
1084 .is_path_valid = _is_path_valid,
1085 .hash_file = _hash_file,
1086 .do_measurements = _do_measurements,
1087 .get_metadata = _get_metadata,
1088 .read_pcr = _read_pcr,
1089 .extend_pcr = _extend_pcr,
1090 .quote_tpm = _quote_tpm,
1091 .destroy = _destroy,
1092 },
1093 .proto_caps = PTS_PROTO_CAPS_V,
1094 .algorithm = PTS_MEAS_ALGO_SHA256,
1095 .dh_group = PTS_DH_GROUP_IKE19,
1096 );
1097
1098 if (is_imc)
1099 {
1100 this->platform_info = extract_platform_info();
1101
1102 if (has_tpm(this))
1103 {
1104 this->has_tpm = TRUE;
1105 this->proto_caps |= PTS_PROTO_CAPS_T;
1106 load_aik(this);
1107 }
1108 }
1109 else
1110 {
1111 this->proto_caps |= PTS_PROTO_CAPS_T | PTS_PROTO_CAPS_C;
1112 }
1113
1114 return &this->public;
1115 }
1116