refactored PTS measurement algorithms
[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 * | Reserved | Hash Algorithm Set |
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 *
35 */
36
37 /**
38 * Diffie-Hellman Hash Algorithm Values
39 * see section 3.8.5 of PTS Protocol: Binding to TNC IF-M Specification
40 *
41 * 1
42 * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
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_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
124 writer = bio_writer_create(PTS_MEAS_ALGO_SIZE);
125 writer->write_uint16 (writer, PTS_MEAS_ALGO_RESERVED);
126 writer->write_uint16(writer, this->algorithms);
127 this->value = chunk_clone(writer->get_buf(writer));
128 writer->destroy(writer);
129 }
130
131 METHOD(pa_tnc_attr_t, process, status_t,
132 private_tcg_pts_attr_meas_algo_t *this, u_int32_t *offset)
133 {
134 bio_reader_t *reader;
135 u_int16_t reserved, algorithms;
136
137 if (this->value.len < PTS_MEAS_ALGO_SIZE)
138 {
139 DBG1(DBG_TNC, "insufficient data for PTS Measurement Algorithm");
140 *offset = 0;
141 return FAILED;
142 }
143 reader = bio_reader_create(this->value);
144 reader->read_uint16 (reader, &reserved);
145 reader->read_uint16(reader, &algorithms);
146 this->algorithms = algorithms;
147 reader->destroy(reader);
148
149 return SUCCESS;
150 }
151
152 METHOD(pa_tnc_attr_t, destroy, void,
153 private_tcg_pts_attr_meas_algo_t *this)
154 {
155 free(this->value.ptr);
156 free(this);
157 }
158
159 METHOD(tcg_pts_attr_meas_algo_t, get_algorithms, pts_meas_algorithms_t,
160 private_tcg_pts_attr_meas_algo_t *this)
161 {
162 return this->algorithms;
163 }
164
165 METHOD(tcg_pts_attr_meas_algo_t, set_algorithms, void,
166 private_tcg_pts_attr_meas_algo_t *this,
167 pts_meas_algorithms_t algorithms)
168 {
169 this->algorithms = algorithms;
170 }
171
172 /**
173 * Described in header.
174 */
175 pa_tnc_attr_t *tcg_pts_attr_meas_algo_create(pts_meas_algorithms_t algorithms,
176 bool selection)
177 {
178 private_tcg_pts_attr_meas_algo_t *this;
179
180 INIT(this,
181 .public = {
182 .pa_tnc_attribute = {
183 .get_vendor_id = _get_vendor_id,
184 .get_type = _get_type,
185 .get_value = _get_value,
186 .get_noskip_flag = _get_noskip_flag,
187 .set_noskip_flag = _set_noskip_flag,
188 .build = _build,
189 .process = _process,
190 .destroy = _destroy,
191 },
192 .get_algorithms = _get_algorithms,
193 .set_algorithms = _set_algorithms,
194 },
195 .vendor_id = PEN_TCG,
196 .type = selection ? TCG_PTS_MEAS_ALGO_SELECTION : TCG_PTS_MEAS_ALGO,
197 .algorithms = algorithms,
198 );
199
200 return &this->public.pa_tnc_attribute;
201 }
202
203
204 /**
205 * Described in header.
206 */
207 pa_tnc_attr_t *tcg_pts_attr_meas_algo_create_from_data(chunk_t data,
208 bool selection)
209 {
210 private_tcg_pts_attr_meas_algo_t *this;
211
212 INIT(this,
213 .public = {
214 .pa_tnc_attribute = {
215 .get_vendor_id = _get_vendor_id,
216 .get_type = _get_type,
217 .get_value = _get_value,
218 .get_noskip_flag = _get_noskip_flag,
219 .set_noskip_flag = _set_noskip_flag,
220 .build = _build,
221 .process = _process,
222 .destroy = _destroy,
223 },
224 .get_algorithms = _get_algorithms,
225 .set_algorithms = _set_algorithms,
226 },
227 .vendor_id = PEN_TCG,
228 .type = selection ? TCG_PTS_MEAS_ALGO_SELECTION : TCG_PTS_MEAS_ALGO,
229 .value = chunk_clone(data),
230 );
231
232 return &this->public.pa_tnc_attribute;
233 }