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