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