Implemented Diffie Hellman Nonce attributes
[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 reader->read_data(reader, reader->remaining(reader), &this->responder_pub_val);
172 reader->destroy(reader);
173
174 return SUCCESS;
175 }
176
177 METHOD(pa_tnc_attr_t, destroy, void,
178 private_tcg_pts_attr_dh_nonce_params_resp_t *this)
179 {
180 free(this->value.ptr);
181 free(this->responder_nonce.ptr);
182 free(this->responder_pub_val.ptr);
183 free(this);
184 }
185
186 METHOD(tcg_pts_attr_dh_nonce_params_resp_t, get_nonce_len, u_int8_t,
187 private_tcg_pts_attr_dh_nonce_params_resp_t *this)
188 {
189 return this->nonce_len;
190 }
191
192 METHOD(tcg_pts_attr_dh_nonce_params_resp_t, get_dh_group, pts_dh_group_t,
193 private_tcg_pts_attr_dh_nonce_params_resp_t *this)
194 {
195 return this->dh_group;
196 }
197
198 METHOD(tcg_pts_attr_dh_nonce_params_resp_t, get_hash_algo_set, pts_meas_algorithms_t,
199 private_tcg_pts_attr_dh_nonce_params_resp_t *this)
200 {
201 return this->hash_algo_set;
202 }
203
204 METHOD(tcg_pts_attr_dh_nonce_params_resp_t, get_responder_nonce, chunk_t,
205 private_tcg_pts_attr_dh_nonce_params_resp_t *this)
206 {
207 return this->responder_nonce;
208 }
209
210 METHOD(tcg_pts_attr_dh_nonce_params_resp_t, get_responder_pub_val, chunk_t,
211 private_tcg_pts_attr_dh_nonce_params_resp_t *this)
212 {
213 return this->responder_pub_val;
214 }
215
216 /**
217 * Described in header.
218 */
219 pa_tnc_attr_t *tcg_pts_attr_dh_nonce_params_resp_create(u_int8_t nonce_len,
220 pts_dh_group_t dh_group,
221 pts_meas_algorithms_t hash_algo_set,
222 chunk_t responder_nonce,
223 chunk_t responder_pub_val)
224 {
225 private_tcg_pts_attr_dh_nonce_params_resp_t *this;
226
227 INIT(this,
228 .public = {
229 .pa_tnc_attribute = {
230 .get_vendor_id = _get_vendor_id,
231 .get_type = _get_type,
232 .get_value = _get_value,
233 .get_noskip_flag = _get_noskip_flag,
234 .set_noskip_flag = _set_noskip_flag,
235 .build = _build,
236 .process = _process,
237 .destroy = _destroy,
238 },
239 .get_nonce_len = _get_nonce_len,
240 .get_dh_group = _get_dh_group,
241 .get_hash_algo_set = _get_hash_algo_set,
242 .get_responder_nonce = _get_responder_nonce,
243 .get_responder_pub_val = _get_responder_pub_val,
244 },
245 .vendor_id = PEN_TCG,
246 .type = TCG_PTS_DH_NONCE_PARAMS_RESP,
247 .nonce_len = nonce_len,
248 .dh_group = dh_group,
249 .hash_algo_set = hash_algo_set,
250 .responder_nonce = responder_nonce,
251 .responder_pub_val = responder_pub_val,
252 );
253
254 return &this->public.pa_tnc_attribute;
255 }
256
257 /**
258 * Described in header.
259 */
260 pa_tnc_attr_t *tcg_pts_attr_dh_nonce_params_resp_create_from_data(chunk_t value)
261 {
262 private_tcg_pts_attr_dh_nonce_params_resp_t *this;
263
264 INIT(this,
265 .public = {
266 .pa_tnc_attribute = {
267 .get_vendor_id = _get_vendor_id,
268 .get_type = _get_type,
269 .get_value = _get_value,
270 .get_noskip_flag = _get_noskip_flag,
271 .set_noskip_flag = _set_noskip_flag,
272 .build = _build,
273 .process = _process,
274 .destroy = _destroy,
275 },
276 .get_nonce_len = _get_nonce_len,
277 .get_dh_group = _get_dh_group,
278 .get_hash_algo_set = _get_hash_algo_set,
279 .get_responder_nonce = _get_responder_nonce,
280 .get_responder_pub_val = _get_responder_pub_val,
281 },
282 .vendor_id = PEN_TCG,
283 .type = TCG_PTS_DH_NONCE_PARAMS_RESP,
284 .value = chunk_clone(value),
285 );
286
287 return &this->public.pa_tnc_attribute;
288 }