moved TSS into pts object
[strongswan.git] / src / libimcv / tcg / 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
20 #include <trousers/tss.h>
21 #include <trousers/trousers.h>
22
23 typedef struct private_pts_t private_pts_t;
24
25 /**
26 * Private data of a pts_t object.
27 *
28 */
29 struct private_pts_t {
30
31 /**
32 * Public pts_t interface.
33 */
34 pts_t public;
35
36 /**
37 * Contains a TPM_CAP_VERSION_INFO struct
38 */
39 chunk_t tpm_version_info;
40 };
41
42 /**
43 * Print TPM 1.2 Version Info
44 */
45 static void print_tpm_version_info(private_pts_t *this)
46 {
47 TPM_CAP_VERSION_INFO versionInfo;
48 UINT64 offset = 0;
49 TSS_RESULT result;
50
51 result = Trspi_UnloadBlob_CAP_VERSION_INFO(&offset,
52 this->tpm_version_info.ptr, &versionInfo);
53 if (result != TSS_SUCCESS)
54 {
55 DBG1(DBG_TNC, "could not parse tpm version info: tss error 0x%x",
56 result);
57 }
58 else
59 {
60 DBG2(DBG_TNC, "TPM 1.2 Version Info: Chip Version: %hhu.%hhu.%hhu.%hhu,"
61 " Spec Level: %hu, Errata Rev: %hhu, Vendor ID: %.4s",
62 versionInfo.version.major, versionInfo.version.minor,
63 versionInfo.version.revMajor, versionInfo.version.revMinor,
64 versionInfo.specLevel, versionInfo.errataRev,
65 versionInfo.tpmVendorID);
66 }
67 }
68
69 METHOD(pts_t, get_tpm_version_info, bool,
70 private_pts_t *this, chunk_t *info)
71 {
72 TSS_HCONTEXT hContext;
73 TSS_HTPM hTPM;
74 TSS_RESULT result;
75
76 if (!this->tpm_version_info.ptr)
77 {
78 result = Tspi_Context_Create(&hContext);
79 if (result != TSS_SUCCESS)
80 {
81 goto err;
82 }
83 result = Tspi_Context_Connect(hContext, NULL);
84 if (result != TSS_SUCCESS)
85 {
86 goto err;
87 }
88 result = Tspi_Context_GetTpmObject (hContext, &hTPM);
89 if (result != TSS_SUCCESS)
90 {
91 goto err;
92 }
93 result = Tspi_TPM_GetCapability(hTPM, TSS_TPMCAP_VERSION_VAL, 0, NULL,
94 &this->tpm_version_info.len,
95 &this->tpm_version_info.ptr);
96 if (result != TSS_SUCCESS)
97 {
98 goto err;
99 }
100 this->tpm_version_info = chunk_clone(this->tpm_version_info);
101 }
102 *info = this->tpm_version_info;
103 print_tpm_version_info(this);
104 return TRUE;
105
106 err:
107 DBG1(DBG_TNC, "could not get tpm version info: tss error 0x%x", result);
108 return FALSE;
109 }
110
111 METHOD(pts_t, set_tpm_version_info, void,
112 private_pts_t *this, chunk_t info)
113 {
114 this->tpm_version_info = chunk_clone(info);
115 print_tpm_version_info(this);
116 }
117
118 METHOD(pts_t, destroy, void,
119 private_pts_t *this)
120 {
121 free(this->tpm_version_info.ptr);
122 free(this);
123 }
124
125 /**
126 * See header
127 */
128 pts_t *pts_create(void)
129 {
130 private_pts_t *this;
131
132 INIT(this,
133 .public = {
134 .get_tpm_version_info = _get_tpm_version_info,
135 .set_tpm_version_info = _set_tpm_version_info,
136 .destroy = _destroy,
137 },
138 );
139
140 return &this->public;
141 }
142