cosmetics
[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 #define PTS_MEAS_ALGO_SIZE 4
38 #define PTS_MEAS_ALGO_RESERVED 0x0000
39
40 /**
41 * Private data of an tcg_pts_attr_meas_algo_t object.
42 */
43 struct private_tcg_pts_attr_meas_algo_t {
44
45 /**
46 * Public members of tcg_pts_attr_meas_algo_t
47 */
48 tcg_pts_attr_meas_algo_t public;
49
50 /**
51 * Attribute vendor ID
52 */
53 pen_t vendor_id;
54
55 /**
56 * Attribute type
57 */
58 u_int32_t type;
59
60 /**
61 * Attribute value
62 */
63 chunk_t value;
64
65 /**
66 * Noskip flag
67 */
68 bool noskip_flag;
69
70 /**
71 * Set of algorithms
72 */
73 pts_meas_algorithms_t algorithms;
74
75 };
76
77 METHOD(pa_tnc_attr_t, get_vendor_id, pen_t,
78 private_tcg_pts_attr_meas_algo_t *this)
79 {
80 return this->vendor_id;
81 }
82
83 METHOD(pa_tnc_attr_t, get_type, u_int32_t,
84 private_tcg_pts_attr_meas_algo_t *this)
85 {
86 return this->type;
87 }
88
89 METHOD(pa_tnc_attr_t, get_value, chunk_t,
90 private_tcg_pts_attr_meas_algo_t *this)
91 {
92 return this->value;
93 }
94
95 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
96 private_tcg_pts_attr_meas_algo_t *this)
97 {
98 return this->noskip_flag;
99 }
100
101 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
102 private_tcg_pts_attr_meas_algo_t *this, bool noskip)
103 {
104 this->noskip_flag = noskip;
105 }
106
107 METHOD(pa_tnc_attr_t, build, void,
108 private_tcg_pts_attr_meas_algo_t *this)
109 {
110 bio_writer_t *writer;
111
112 writer = bio_writer_create(PTS_MEAS_ALGO_SIZE);
113 writer->write_uint16(writer, PTS_MEAS_ALGO_RESERVED);
114 writer->write_uint16(writer, this->algorithms);
115 this->value = chunk_clone(writer->get_buf(writer));
116 writer->destroy(writer);
117 }
118
119 METHOD(pa_tnc_attr_t, process, status_t,
120 private_tcg_pts_attr_meas_algo_t *this, u_int32_t *offset)
121 {
122 bio_reader_t *reader;
123 u_int16_t reserved, algorithms;
124
125 if (this->value.len < PTS_MEAS_ALGO_SIZE)
126 {
127 DBG1(DBG_TNC, "insufficient data for PTS Measurement Algorithm");
128 *offset = 0;
129 return FAILED;
130 }
131 reader = bio_reader_create(this->value);
132 reader->read_uint16(reader, &reserved);
133 reader->read_uint16(reader, &algorithms);
134 this->algorithms = algorithms;
135 reader->destroy(reader);
136
137 return SUCCESS;
138 }
139
140 METHOD(pa_tnc_attr_t, destroy, void,
141 private_tcg_pts_attr_meas_algo_t *this)
142 {
143 free(this->value.ptr);
144 free(this);
145 }
146
147 METHOD(tcg_pts_attr_meas_algo_t, get_algorithms, pts_meas_algorithms_t,
148 private_tcg_pts_attr_meas_algo_t *this)
149 {
150 return this->algorithms;
151 }
152
153 /**
154 * Described in header.
155 */
156 pa_tnc_attr_t *tcg_pts_attr_meas_algo_create(pts_meas_algorithms_t algorithms,
157 bool selection)
158 {
159 private_tcg_pts_attr_meas_algo_t *this;
160
161 INIT(this,
162 .public = {
163 .pa_tnc_attribute = {
164 .get_vendor_id = _get_vendor_id,
165 .get_type = _get_type,
166 .get_value = _get_value,
167 .get_noskip_flag = _get_noskip_flag,
168 .set_noskip_flag = _set_noskip_flag,
169 .build = _build,
170 .process = _process,
171 .destroy = _destroy,
172 },
173 .get_algorithms = _get_algorithms,
174 },
175 .vendor_id = PEN_TCG,
176 .type = selection ? TCG_PTS_MEAS_ALGO_SELECTION : TCG_PTS_MEAS_ALGO,
177 .algorithms = algorithms,
178 );
179
180 return &this->public.pa_tnc_attribute;
181 }
182
183
184 /**
185 * Described in header.
186 */
187 pa_tnc_attr_t *tcg_pts_attr_meas_algo_create_from_data(chunk_t data,
188 bool selection)
189 {
190 private_tcg_pts_attr_meas_algo_t *this;
191
192 INIT(this,
193 .public = {
194 .pa_tnc_attribute = {
195 .get_vendor_id = _get_vendor_id,
196 .get_type = _get_type,
197 .get_value = _get_value,
198 .get_noskip_flag = _get_noskip_flag,
199 .set_noskip_flag = _set_noskip_flag,
200 .build = _build,
201 .process = _process,
202 .destroy = _destroy,
203 },
204 .get_algorithms = _get_algorithms,
205 },
206 .vendor_id = PEN_TCG,
207 .type = selection ? TCG_PTS_MEAS_ALGO_SELECTION : TCG_PTS_MEAS_ALGO,
208 .value = chunk_clone(data),
209 );
210
211 return &this->public.pa_tnc_attribute;
212 }