Implemented TCG PTS Attributes
[strongswan.git] / src / libimcv / tcg / tcg_pts_attr_tpm_version_info.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 "tcg_pts_attr_tpm_version_info.h"
17
18 #include <pa_tnc/pa_tnc_msg.h>
19 #include <bio/bio_writer.h>
20 #include <bio/bio_reader.h>
21 #include <debug.h>
22
23 typedef struct private_tcg_pts_attr_tpm_version_info_t private_tcg_pts_attr_tpm_version_info_t;
24
25 /**
26 * TPM Version Information (see section 3.11 of PTS Protocol: Binding to TNC IF-M Specification)
27 *
28 * 1 2 3
29 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
30 *
31 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32 * | TPM Version Information (Variable Lenght) |
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 *
35 * see TPM Structure Specification Part 2, section 21.6: TPM_CAP_VERSION_INFO
36 */
37
38 #define PTS_TPM_VER_INFO_SIZE 4
39
40 /**
41 * Private data of an private_tcg_pts_attr_tpm_version_info_t object.
42 */
43 struct private_tcg_pts_attr_tpm_version_info_t {
44
45 /**
46 * Public members of private_tcg_pts_attr_tpm_version_info_t
47 */
48 tcg_pts_attr_tpm_version_info_t public;
49
50 /**
51 * Attribute vendor ID
52 */
53 pen_t vendor_id;
54
55 /**
56 * Attribute type
57 */
58 u_int32_t type;
59
60 /**
61 * Attribute value
62 */
63 chunk_t value;
64
65 /**
66 * Noskip flag
67 */
68 bool noskip_flag;
69
70 /**
71 * TPM Version Information
72 */
73 chunk_t tpm_version_info;
74 };
75
76 METHOD(pa_tnc_attr_t, get_vendor_id, pen_t,
77 private_tcg_pts_attr_tpm_version_info_t *this)
78 {
79 return this->vendor_id;
80 }
81
82 METHOD(pa_tnc_attr_t, get_type, u_int32_t,
83 private_tcg_pts_attr_tpm_version_info_t *this)
84 {
85 return this->type;
86 }
87
88 METHOD(pa_tnc_attr_t, get_value, chunk_t,
89 private_tcg_pts_attr_tpm_version_info_t *this)
90 {
91 return this->value;
92 }
93
94 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
95 private_tcg_pts_attr_tpm_version_info_t *this)
96 {
97 return this->noskip_flag;
98 }
99
100 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
101 private_tcg_pts_attr_tpm_version_info_t *this, bool noskip)
102 {
103 this->noskip_flag = noskip;
104 }
105
106 METHOD(pa_tnc_attr_t, build, void,
107 private_tcg_pts_attr_tpm_version_info_t *this)
108 {
109 bio_writer_t *writer;
110
111 writer = bio_writer_create(PTS_TPM_VER_INFO_SIZE);
112 writer->write_data(writer, this->tpm_version_info);
113
114 this->value = chunk_clone(writer->get_buf(writer));
115 writer->destroy(writer);
116 }
117
118 METHOD(pa_tnc_attr_t, process, status_t,
119 private_tcg_pts_attr_tpm_version_info_t *this)
120 {
121 bio_reader_t *reader;
122
123 if (this->value.len < PTS_TPM_VER_INFO_SIZE)
124 {
125 DBG1(DBG_TNC, "insufficient data for TPM Version Information");
126 return FAILED;
127 }
128 reader = bio_reader_create(this->value);
129 reader->read_data (reader, sizeof(this->value), &this->tpm_version_info);
130 this->tpm_version_info = chunk_clone(this->tpm_version_info);
131 reader->destroy(reader);
132
133 return SUCCESS;
134 }
135
136 METHOD(pa_tnc_attr_t, destroy, void,
137 private_tcg_pts_attr_tpm_version_info_t *this)
138 {
139 free(this->value.ptr);
140 free(this->tpm_version_info.ptr);
141 free(this);
142 }
143
144 METHOD(tcg_pts_attr_tpm_version_info_t, get_tpm_version_info, chunk_t,
145 private_tcg_pts_attr_tpm_version_info_t *this)
146 {
147 return this->tpm_version_info;
148 }
149
150 METHOD(tcg_pts_attr_tpm_version_info_t, set_tpm_version_info, void,
151 private_tcg_pts_attr_tpm_version_info_t *this,
152 chunk_t tpm_version_info)
153 {
154 return this->tpm_version_info = tpm_version_info;
155 }
156
157 /**
158 * Described in header.
159 */
160 pa_tnc_attr_t *tcg_pts_attr_tpm_version_info_create(chunk_t tpm_version_info)
161 {
162 private_tcg_pts_attr_tpm_version_info_t *this;
163
164 INIT(this,
165 .public = {
166 .pa_tnc_attribute = {
167 .get_vendor_id = _get_vendor_id,
168 .get_type = _get_type,
169 .get_value = _get_value,
170 .get_noskip_flag = _get_noskip_flag,
171 .set_noskip_flag = _set_noskip_flag,
172 .build = _build,
173 .process = _process,
174 .destroy = _destroy,
175 },
176 .get_tpm_version_info = get_tpm_version_info,
177 .set_tpm_version_info = set_tpm_version_info,
178 },
179 .vendor_id = PEN_TCG,
180 .type = TCG_PTS_TPM_VERSION_INFO,
181 .tpm_version_info = tpm_version_info,
182 );
183
184 return &this->public.pa_tnc_attribute;
185 }
186
187
188 /**
189 * Described in header.
190 */
191 pa_tnc_attr_t *tcg_pts_attr_tpm_version_info_create_from_data(chunk_t data)
192 {
193 private_tcg_pts_attr_tpm_version_info_t *this;
194
195 INIT(this,
196 .public = {
197 .pa_tnc_attribute = {
198 .get_vendor_id = _get_vendor_id,
199 .get_type = _get_type,
200 .get_value = _get_value,
201 .get_noskip_flag = _get_noskip_flag,
202 .set_noskip_flag = _set_noskip_flag,
203 .build = _build,
204 .process = _process,
205 .destroy = _destroy,
206 },
207 .get_tpm_version_info = get_tpm_version_info,
208 .set_tpm_version_info = set_tpm_version_info,
209 },
210 .vendor_id = PEN_TCG,
211 .type = TCG_PTS_TPM_VERSION_INFO,
212 .value = chunk_clone(data),
213 );
214
215 return &this->public.pa_tnc_attribute;
216 }