Implemented TCG PTS Attributes
[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 private_tcg_pts_attr_req_proto_caps_t object.
54 */
55 struct private_tcg_pts_attr_meas_algo_t {
56
57 /**
58 * Public members of private_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)
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 return FAILED;
149 }
150 reader = bio_reader_create(this->value);
151 reader->read_uint16 (reader, &reserved);
152 reader->read_uint16(reader, &algorithms);
153
154 if((algorithms >> 13) & 1) this->algorithms |= PTS_MEAS_ALGO_SHA384;
155 if((algorithms >> 14) & 1) this->algorithms |= PTS_MEAS_ALGO_SHA256;
156 if((algorithms >> 15) & 1) this->algorithms |= PTS_MEAS_ALGO_SHA1;
157
158 reader->destroy(reader);
159
160 return SUCCESS;
161 }
162
163 METHOD(pa_tnc_attr_t, destroy, void,
164 private_tcg_pts_attr_meas_algo_t *this)
165 {
166 free(this->value.ptr);
167 free(this);
168 }
169
170 METHOD(tcg_pts_attr_meas_algo_t, get_algorithms, pts_attr_meas_algorithms_t,
171 private_tcg_pts_attr_meas_algo_t *this)
172 {
173 return this->algorithms;
174 }
175
176 METHOD(tcg_pts_attr_meas_algo_t, set_algorithms, void,
177 private_tcg_pts_attr_meas_algo_t *this,
178 pts_attr_meas_algorithms_t algorithms)
179 {
180 return this->algorithms = algorithms;
181 }
182
183 /**
184 * Described in header.
185 */
186 pa_tnc_attr_t *tcg_pts_attr_meas_algo_create(pts_attr_meas_algorithms_t algorithms)
187 {
188 private_tcg_pts_attr_meas_algo_t *this;
189
190 INIT(this,
191 .public = {
192 .pa_tnc_attribute = {
193 .get_vendor_id = _get_vendor_id,
194 .get_type = _get_type,
195 .get_value = _get_value,
196 .get_noskip_flag = _get_noskip_flag,
197 .set_noskip_flag = _set_noskip_flag,
198 .build = _build,
199 .process = _process,
200 .destroy = _destroy,
201 },
202 .get_algorithms = get_algorithms,
203 .set_algorithms = set_algorithms,
204 },
205 .vendor_id = PEN_TCG,
206 .type = TCG_PTS_MEAS_ALGO,
207 .algorithms = algorithms,
208 );
209
210 return &this->public.pa_tnc_attribute;
211 }
212
213
214 /**
215 * Described in header.
216 */
217 pa_tnc_attr_t *tcg_pts_attr_meas_algo_create_from_data(chunk_t data)
218 {
219 private_tcg_pts_attr_meas_algo_t *this;
220
221 INIT(this,
222 .public = {
223 .pa_tnc_attribute = {
224 .get_vendor_id = _get_vendor_id,
225 .get_type = _get_type,
226 .get_value = _get_value,
227 .get_noskip_flag = _get_noskip_flag,
228 .set_noskip_flag = _set_noskip_flag,
229 .build = _build,
230 .process = _process,
231 .destroy = _destroy,
232 },
233 .get_algorithms = get_algorithms,
234 .set_algorithms = set_algorithms,
235 },
236 .vendor_id = PEN_TCG,
237 .type = TCG_PTS_MEAS_ALGO,
238 .value = chunk_clone(data),
239 );
240
241 return &this->public.pa_tnc_attribute;
242 }