Debugged TCG PTS attributes implementations
[strongswan.git] / src / libimcv / tcg / tcg_pts_attr_meas_algo_selection.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_meas_algo_selection.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_meas_algo_selection_t private_tcg_pts_attr_meas_algo_selection_t;
24
25 /**
26 * PTS Measurement Algorithm Selection (see section 3.9.2 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 * | Reserved | Hash Algorithm Set |
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 *
35 */
36
37 /**
38 * Diffie-Hellman Hash Algorithm Values (see section 3.8.5 of PTS Protocol: Binding to TNC IF-M Specification)
39 *
40 * 1
41 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
42 *
43 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
44 * |1|2|3|R|R|R|R|R|R|R|R|R|R|R|R|R|
45 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
46 *
47 */
48
49 #define PTS_MEAS_ALGO_SEL_SIZE 4
50 #define PTS_MEAS_ALGO_SEL_RESERVED 0x00
51
52 /**
53 * Private data of an tcg_pts_attr_meas_algo_selection_t object.
54 */
55 struct private_tcg_pts_attr_meas_algo_selection_t {
56
57 /**
58 * Public members of tcg_pts_attr_meas_algo_selection_t
59 */
60 tcg_pts_attr_meas_algo_selection_t public;
61
62 /**
63 * Attribute vendor ID
64 */
65 pen_t vendor_id;
66
67 /**
68 * Attribute type
69 */
70 u_int32_t type;
71
72 /**
73 * Attribute value
74 */
75 chunk_t value;
76
77 /**
78 * Noskip flag
79 */
80 bool noskip_flag;
81
82 /**
83 * A Selected Measurement Algorithm
84 */
85 pts_attr_meas_algorithms_t algorithm;
86
87 };
88
89 METHOD(pa_tnc_attr_t, get_vendor_id, pen_t,
90 private_tcg_pts_attr_meas_algo_selection_t *this)
91 {
92 return this->vendor_id;
93 }
94
95 METHOD(pa_tnc_attr_t, get_type, u_int32_t,
96 private_tcg_pts_attr_meas_algo_selection_t *this)
97 {
98 return this->type;
99 }
100
101 METHOD(pa_tnc_attr_t, get_value, chunk_t,
102 private_tcg_pts_attr_meas_algo_selection_t *this)
103 {
104 return this->value;
105 }
106
107 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
108 private_tcg_pts_attr_meas_algo_selection_t *this)
109 {
110 return this->noskip_flag;
111 }
112
113 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
114 private_tcg_pts_attr_meas_algo_selection_t *this, bool noskip)
115 {
116 this->noskip_flag = noskip;
117 }
118
119 METHOD(pa_tnc_attr_t, build, void,
120 private_tcg_pts_attr_meas_algo_selection_t *this)
121 {
122 bio_writer_t *writer;
123 u_int16_t algorithm = 0;
124
125 writer = bio_writer_create(PTS_MEAS_ALGO_SEL_SIZE);
126 writer->write_uint16 (writer, PTS_MEAS_ALGO_SEL_RESERVED);
127
128 /* Determine the hash algorithm to set*/
129 if(this->algorithm & PTS_MEAS_ALGO_SHA384) algorithm = 8192;
130 else if(this->algorithm & PTS_MEAS_ALGO_SHA256) algorithm = 16384;
131 else if(this->algorithm & PTS_MEAS_ALGO_SHA1) algorithm = 32768;
132 writer->write_uint16(writer, algorithm);
133
134 this->value = chunk_clone(writer->get_buf(writer));
135 writer->destroy(writer);
136 }
137
138 METHOD(pa_tnc_attr_t, process, status_t,
139 private_tcg_pts_attr_meas_algo_selection_t *this, u_int32_t *offset)
140 {
141 bio_reader_t *reader;
142 u_int16_t reserved;
143 u_int16_t algorithm;
144
145 if (this->value.len < PTS_MEAS_ALGO_SEL_SIZE)
146 {
147 DBG1(DBG_TNC, "insufficient data for PTS Measurement Algorithm Selection");
148 *offset = 0;
149 return FAILED;
150 }
151 reader = bio_reader_create(this->value);
152 reader->read_uint16 (reader, &reserved);
153 reader->read_uint16(reader, &algorithm);
154
155 if((algorithm >> 13) & 1) this->algorithm = PTS_MEAS_ALGO_SHA384;
156 else if((algorithm >> 14) & 1) this->algorithm = PTS_MEAS_ALGO_SHA256;
157 else if((algorithm >> 15) & 1) this->algorithm = PTS_MEAS_ALGO_SHA1;
158
159 reader->destroy(reader);
160
161 return SUCCESS;
162 }
163
164 METHOD(pa_tnc_attr_t, destroy, void,
165 private_tcg_pts_attr_meas_algo_selection_t *this)
166 {
167 free(this->value.ptr);
168 free(this);
169 }
170
171 METHOD(tcg_pts_attr_meas_algo_selection_t, get_algorithm, pts_attr_meas_algorithms_t,
172 private_tcg_pts_attr_meas_algo_selection_t *this)
173 {
174 return this->algorithm;
175 }
176
177 METHOD(tcg_pts_attr_meas_algo_selection_t, set_algorithm, void,
178 private_tcg_pts_attr_meas_algo_selection_t *this,
179 pts_attr_meas_algorithms_t algorithm)
180 {
181 this->algorithm = algorithm;
182 }
183
184 /**
185 * Described in header.
186 */
187 pa_tnc_attr_t *tcg_pts_attr_meas_algo_selection_create(pts_attr_meas_algorithms_t algorithm)
188 {
189 private_tcg_pts_attr_meas_algo_selection_t *this;
190
191 INIT(this,
192 .public = {
193 .pa_tnc_attribute = {
194 .get_vendor_id = _get_vendor_id,
195 .get_type = _get_type,
196 .get_value = _get_value,
197 .get_noskip_flag = _get_noskip_flag,
198 .set_noskip_flag = _set_noskip_flag,
199 .build = _build,
200 .process = _process,
201 .destroy = _destroy,
202 },
203 .get_algorithm = _get_algorithm,
204 .set_algorithm = _set_algorithm,
205 },
206 .vendor_id = PEN_TCG,
207 .type = TCG_PTS_MEAS_ALGO_SELECTION,
208 .algorithm = algorithm,
209 );
210
211 return &this->public.pa_tnc_attribute;
212 }
213
214
215 /**
216 * Described in header.
217 */
218 pa_tnc_attr_t *tcg_pts_attr_meas_algo_selection_create_from_data(chunk_t data)
219 {
220 private_tcg_pts_attr_meas_algo_selection_t *this;
221
222 INIT(this,
223 .public = {
224 .pa_tnc_attribute = {
225 .get_vendor_id = _get_vendor_id,
226 .get_type = _get_type,
227 .get_value = _get_value,
228 .get_noskip_flag = _get_noskip_flag,
229 .set_noskip_flag = _set_noskip_flag,
230 .build = _build,
231 .process = _process,
232 .destroy = _destroy,
233 },
234 .get_algorithm = _get_algorithm,
235 .set_algorithm = _set_algorithm,
236 },
237 .vendor_id = PEN_TCG,
238 .type = TCG_PTS_MEAS_ALGO_SELECTION,
239 .value = chunk_clone(data),
240 );
241
242 return &this->public.pa_tnc_attribute;
243 }