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