Debugged TCG PTS attributes implementations
[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 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 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, u_int32_t *offset)
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 *offset = 0;
127 return FAILED;
128 }
129 reader = bio_reader_create(this->value);
130 reader->read_data (reader, sizeof(this->value), &this->tpm_version_info);
131 this->tpm_version_info = chunk_clone(this->tpm_version_info);
132 reader->destroy(reader);
133
134 return SUCCESS;
135 }
136
137 METHOD(pa_tnc_attr_t, destroy, void,
138 private_tcg_pts_attr_tpm_version_info_t *this)
139 {
140 free(this->value.ptr);
141 free(this->tpm_version_info.ptr);
142 free(this);
143 }
144
145 METHOD(tcg_pts_attr_tpm_version_info_t, get_tpm_version_info, chunk_t,
146 private_tcg_pts_attr_tpm_version_info_t *this)
147 {
148 return this->tpm_version_info;
149 }
150
151 METHOD(tcg_pts_attr_tpm_version_info_t, set_tpm_version_info, void,
152 private_tcg_pts_attr_tpm_version_info_t *this,
153 chunk_t tpm_version_info)
154 {
155 this->tpm_version_info = tpm_version_info;
156 }
157
158 /**
159 * Described in header.
160 */
161 pa_tnc_attr_t *tcg_pts_attr_tpm_version_info_create(chunk_t tpm_version_info)
162 {
163 private_tcg_pts_attr_tpm_version_info_t *this;
164
165 INIT(this,
166 .public = {
167 .pa_tnc_attribute = {
168 .get_vendor_id = _get_vendor_id,
169 .get_type = _get_type,
170 .get_value = _get_value,
171 .get_noskip_flag = _get_noskip_flag,
172 .set_noskip_flag = _set_noskip_flag,
173 .build = _build,
174 .process = _process,
175 .destroy = _destroy,
176 },
177 .get_tpm_version_info = _get_tpm_version_info,
178 .set_tpm_version_info = _set_tpm_version_info,
179 },
180 .vendor_id = PEN_TCG,
181 .type = TCG_PTS_TPM_VERSION_INFO,
182 .tpm_version_info = tpm_version_info,
183 );
184
185 return &this->public.pa_tnc_attribute;
186 }
187
188
189 /**
190 * Described in header.
191 */
192 pa_tnc_attr_t *tcg_pts_attr_tpm_version_info_create_from_data(chunk_t data)
193 {
194 private_tcg_pts_attr_tpm_version_info_t *this;
195
196 INIT(this,
197 .public = {
198 .pa_tnc_attribute = {
199 .get_vendor_id = _get_vendor_id,
200 .get_type = _get_type,
201 .get_value = _get_value,
202 .get_noskip_flag = _get_noskip_flag,
203 .set_noskip_flag = _set_noskip_flag,
204 .build = _build,
205 .process = _process,
206 .destroy = _destroy,
207 },
208 .get_tpm_version_info = _get_tpm_version_info,
209 .set_tpm_version_info = _set_tpm_version_info,
210 },
211 .vendor_id = PEN_TCG,
212 .type = TCG_PTS_TPM_VERSION_INFO,
213 .value = chunk_clone(data),
214 );
215
216 return &this->public.pa_tnc_attribute;
217 }