Clone chunk_t parameters in create function
[strongswan.git] / src / libpts / tcg / tcg_pts_attr_dh_nonce_params_resp.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_dh_nonce_params_resp.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_dh_nonce_params_resp_t private_tcg_pts_attr_dh_nonce_params_resp_t;
24
25 /**
26 * PTS DH Nonce Parameters Response
27 * see section 3.8.2 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 | Nonce Len |
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 * | Selected D-H Group | Hash Algorithm Set |
35 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36 * | D-H Responder Nonce ... |
37 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38 * | D-H Responder Public Value ... |
39 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40 *
41 */
42
43 #define PTS_DH_NONCE_PARAMS_RESP_SIZE 16
44 #define PTS_DH_NONCE_PARAMS_RESP_RESERVED 0x0000
45
46 /**
47 * Private data of an tcg_pts_attr_dh_nonce_params_resp_t object.
48 */
49 struct private_tcg_pts_attr_dh_nonce_params_resp_t {
50
51 /**
52 * Public members of tcg_pts_attr_dh_nonce_params_resp_t
53 */
54 tcg_pts_attr_dh_nonce_params_resp_t public;
55
56 /**
57 * Attribute vendor ID
58 */
59 pen_t vendor_id;
60
61 /**
62 * Attribute type
63 */
64 u_int32_t type;
65
66 /**
67 * Attribute value
68 */
69 chunk_t value;
70
71 /**
72 * Noskip flag
73 */
74 bool noskip_flag;
75
76 /**
77 * Length of nonce
78 */
79 u_int8_t nonce_len;
80
81 /**
82 * Selected Diffie Hellman group
83 */
84 pts_dh_group_t dh_group;
85
86 /**
87 * Supported Hashing Algorithms
88 */
89 pts_meas_algorithms_t hash_algo_set;
90
91 /**
92 * DH Responder Nonce
93 */
94 chunk_t responder_nonce;
95
96 /**
97 * DH Responder Public Value
98 */
99 chunk_t responder_pub_val;
100
101 };
102
103 METHOD(pa_tnc_attr_t, get_vendor_id, pen_t,
104 private_tcg_pts_attr_dh_nonce_params_resp_t *this)
105 {
106 return this->vendor_id;
107 }
108
109 METHOD(pa_tnc_attr_t, get_type, u_int32_t,
110 private_tcg_pts_attr_dh_nonce_params_resp_t *this)
111 {
112 return this->type;
113 }
114
115 METHOD(pa_tnc_attr_t, get_value, chunk_t,
116 private_tcg_pts_attr_dh_nonce_params_resp_t *this)
117 {
118 return this->value;
119 }
120
121 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
122 private_tcg_pts_attr_dh_nonce_params_resp_t *this)
123 {
124 return this->noskip_flag;
125 }
126
127 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
128 private_tcg_pts_attr_dh_nonce_params_resp_t *this, bool noskip)
129 {
130 this->noskip_flag = noskip;
131 }
132
133 METHOD(pa_tnc_attr_t, build, void,
134 private_tcg_pts_attr_dh_nonce_params_resp_t *this)
135 {
136 bio_writer_t *writer;
137
138 writer = bio_writer_create(PTS_DH_NONCE_PARAMS_RESP_SIZE);
139 writer->write_uint24(writer, PTS_DH_NONCE_PARAMS_RESP_RESERVED);
140 writer->write_uint8 (writer, this->nonce_len);
141 writer->write_uint16(writer, this->dh_group);
142 writer->write_uint16(writer, this->hash_algo_set);
143 writer->write_data (writer, this->responder_nonce);
144 writer->write_data (writer, this->responder_pub_val);
145
146 this->value = chunk_clone(writer->get_buf(writer));
147 writer->destroy(writer);
148 }
149
150 METHOD(pa_tnc_attr_t, process, status_t,
151 private_tcg_pts_attr_dh_nonce_params_resp_t *this, u_int32_t *offset)
152 {
153 bio_reader_t *reader;
154 u_int32_t reserved;
155 u_int16_t dh_group, hash_algo_set;
156
157 if (this->value.len < PTS_DH_NONCE_PARAMS_RESP_SIZE)
158 {
159 DBG1(DBG_TNC, "insufficient data for PTS DH Nonce Parameters Response");
160 *offset = 0;
161 return FAILED;
162 }
163 reader = bio_reader_create(this->value);
164 reader->read_uint24(reader, &reserved);
165 reader->read_uint8 (reader, &this->nonce_len);
166 reader->read_uint16(reader, &dh_group);
167 this->dh_group = dh_group;
168 reader->read_uint16(reader, &hash_algo_set);
169 this->hash_algo_set = hash_algo_set;
170 reader->read_data(reader, this->nonce_len, &this->responder_nonce);
171 this->responder_nonce = chunk_clone(this->responder_nonce);
172 reader->read_data(reader, reader->remaining(reader), &this->responder_pub_val);
173 this->responder_pub_val = chunk_clone(this->responder_pub_val);
174 reader->destroy(reader);
175
176 return SUCCESS;
177 }
178
179 METHOD(pa_tnc_attr_t, destroy, void,
180 private_tcg_pts_attr_dh_nonce_params_resp_t *this)
181 {
182 free(this->value.ptr);
183 free(this->responder_nonce.ptr);
184 free(this->responder_pub_val.ptr);
185 free(this);
186 }
187
188 METHOD(tcg_pts_attr_dh_nonce_params_resp_t, get_nonce_len, u_int8_t,
189 private_tcg_pts_attr_dh_nonce_params_resp_t *this)
190 {
191 return this->nonce_len;
192 }
193
194 METHOD(tcg_pts_attr_dh_nonce_params_resp_t, get_dh_group, pts_dh_group_t,
195 private_tcg_pts_attr_dh_nonce_params_resp_t *this)
196 {
197 return this->dh_group;
198 }
199
200 METHOD(tcg_pts_attr_dh_nonce_params_resp_t, get_hash_algo_set, pts_meas_algorithms_t,
201 private_tcg_pts_attr_dh_nonce_params_resp_t *this)
202 {
203 return this->hash_algo_set;
204 }
205
206 METHOD(tcg_pts_attr_dh_nonce_params_resp_t, get_responder_nonce, chunk_t,
207 private_tcg_pts_attr_dh_nonce_params_resp_t *this)
208 {
209 return this->responder_nonce;
210 }
211
212 METHOD(tcg_pts_attr_dh_nonce_params_resp_t, get_responder_pub_val, chunk_t,
213 private_tcg_pts_attr_dh_nonce_params_resp_t *this)
214 {
215 return this->responder_pub_val;
216 }
217
218 /**
219 * Described in header.
220 */
221 pa_tnc_attr_t *tcg_pts_attr_dh_nonce_params_resp_create(u_int8_t nonce_len,
222 pts_dh_group_t dh_group,
223 pts_meas_algorithms_t hash_algo_set,
224 chunk_t responder_nonce,
225 chunk_t responder_pub_val)
226 {
227 private_tcg_pts_attr_dh_nonce_params_resp_t *this;
228
229 INIT(this,
230 .public = {
231 .pa_tnc_attribute = {
232 .get_vendor_id = _get_vendor_id,
233 .get_type = _get_type,
234 .get_value = _get_value,
235 .get_noskip_flag = _get_noskip_flag,
236 .set_noskip_flag = _set_noskip_flag,
237 .build = _build,
238 .process = _process,
239 .destroy = _destroy,
240 },
241 .get_nonce_len = _get_nonce_len,
242 .get_dh_group = _get_dh_group,
243 .get_hash_algo_set = _get_hash_algo_set,
244 .get_responder_nonce = _get_responder_nonce,
245 .get_responder_pub_val = _get_responder_pub_val,
246 },
247 .vendor_id = PEN_TCG,
248 .type = TCG_PTS_DH_NONCE_PARAMS_RESP,
249 .nonce_len = nonce_len,
250 .dh_group = dh_group,
251 .hash_algo_set = hash_algo_set,
252 .responder_nonce = chunk_clone(responder_nonce),
253 .responder_pub_val = chunk_clone(responder_pub_val),
254 );
255
256 return &this->public.pa_tnc_attribute;
257 }
258
259 /**
260 * Described in header.
261 */
262 pa_tnc_attr_t *tcg_pts_attr_dh_nonce_params_resp_create_from_data(chunk_t value)
263 {
264 private_tcg_pts_attr_dh_nonce_params_resp_t *this;
265
266 INIT(this,
267 .public = {
268 .pa_tnc_attribute = {
269 .get_vendor_id = _get_vendor_id,
270 .get_type = _get_type,
271 .get_value = _get_value,
272 .get_noskip_flag = _get_noskip_flag,
273 .set_noskip_flag = _set_noskip_flag,
274 .build = _build,
275 .process = _process,
276 .destroy = _destroy,
277 },
278 .get_nonce_len = _get_nonce_len,
279 .get_dh_group = _get_dh_group,
280 .get_hash_algo_set = _get_hash_algo_set,
281 .get_responder_nonce = _get_responder_nonce,
282 .get_responder_pub_val = _get_responder_pub_val,
283 },
284 .vendor_id = PEN_TCG,
285 .type = TCG_PTS_DH_NONCE_PARAMS_RESP,
286 .value = chunk_clone(value),
287 );
288
289 return &this->public.pa_tnc_attribute;
290 }