Debugged TCG PTS attributes implementations
[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 (see section 3.9.1 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_SIZE 4
50 #define PTS_MEAS_ALGO_RESERVED 0x00
51
52 /**
53 * Private data of an tcg_pts_attr_meas_algo_t object.
54 */
55 struct private_tcg_pts_attr_meas_algo_t {
56
57 /**
58 * Public members of tcg_pts_attr_meas_algo_t
59 */
60 tcg_pts_attr_meas_algo_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 * Set of algorithms
84 */
85 pts_attr_meas_algorithms_t algorithms;
86
87 };
88
89 METHOD(pa_tnc_attr_t, get_vendor_id, pen_t,
90 private_tcg_pts_attr_meas_algo_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_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_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_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_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_t *this)
121 {
122 bio_writer_t *writer;
123 u_int16_t algorithms = 0;
124
125 writer = bio_writer_create(PTS_MEAS_ALGO_SIZE);
126 writer->write_uint16 (writer, PTS_MEAS_ALGO_RESERVED);
127
128 /* Determine the hash algorithms to set*/
129 if(this->algorithms & PTS_MEAS_ALGO_SHA384) algorithms += 8192;
130 if(this->algorithms & PTS_MEAS_ALGO_SHA256) algorithms += 16384;
131 if(this->algorithms & PTS_MEAS_ALGO_SHA1) algorithms += 32768;
132 writer->write_uint16(writer, algorithms);
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_t *this, u_int32_t *offset)
140 {
141 bio_reader_t *reader;
142 u_int16_t reserved;
143 u_int16_t algorithms;
144
145 if (this->value.len < PTS_MEAS_ALGO_SIZE)
146 {
147 DBG1(DBG_TNC, "insufficient data for PTS Measurement Algorithm");
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, &algorithms);
154
155 if((algorithms >> 13) & 1) this->algorithms |= PTS_MEAS_ALGO_SHA384;
156 if((algorithms >> 14) & 1) this->algorithms |= PTS_MEAS_ALGO_SHA256;
157 if((algorithms >> 15) & 1) this->algorithms |= 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_t *this)
166 {
167 free(this->value.ptr);
168 free(this);
169 }
170
171 METHOD(tcg_pts_attr_meas_algo_t, get_algorithms, pts_attr_meas_algorithms_t,
172 private_tcg_pts_attr_meas_algo_t *this)
173 {
174 return this->algorithms;
175 }
176
177 METHOD(tcg_pts_attr_meas_algo_t, set_algorithms, void,
178 private_tcg_pts_attr_meas_algo_t *this,
179 pts_attr_meas_algorithms_t algorithms)
180 {
181 this->algorithms = algorithms;
182 }
183
184 /**
185 * Described in header.
186 */
187 pa_tnc_attr_t *tcg_pts_attr_meas_algo_create(pts_attr_meas_algorithms_t algorithms)
188 {
189 private_tcg_pts_attr_meas_algo_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_algorithms = _get_algorithms,
204 .set_algorithms = _set_algorithms,
205 },
206 .vendor_id = PEN_TCG,
207 .type = TCG_PTS_MEAS_ALGO,
208 .algorithms = algorithms,
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_create_from_data(chunk_t data)
219 {
220 private_tcg_pts_attr_meas_algo_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_algorithms = _get_algorithms,
235 .set_algorithms = _set_algorithms,
236 },
237 .vendor_id = PEN_TCG,
238 .type = TCG_PTS_MEAS_ALGO,
239 .value = chunk_clone(data),
240 );
241
242 return &this->public.pa_tnc_attribute;
243 }