Implemented Diffie Hellman Nonce attributes
[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 reader->read_data(reader, this->nonce_len, &this->initiator_nonce);
162
163 reader->destroy(reader);
164
165 return SUCCESS;
166 }
167
168 METHOD(pa_tnc_attr_t, destroy, void,
169 private_tcg_pts_attr_dh_nonce_finish_t *this)
170 {
171 free(this->value.ptr);
172 free(this->initiator_pub_val.ptr);
173 free(this->initiator_nonce.ptr);
174 free(this);
175 }
176
177 METHOD(tcg_pts_attr_dh_nonce_finish_t, get_nonce_len, u_int8_t,
178 private_tcg_pts_attr_dh_nonce_finish_t *this)
179 {
180 return this->nonce_len;
181 }
182
183 METHOD(tcg_pts_attr_dh_nonce_finish_t, get_hash_algo, pts_meas_algorithms_t,
184 private_tcg_pts_attr_dh_nonce_finish_t *this)
185 {
186 return this->hash_algo;
187 }
188
189 METHOD(tcg_pts_attr_dh_nonce_finish_t, get_initiator_pub_val, chunk_t,
190 private_tcg_pts_attr_dh_nonce_finish_t *this)
191 {
192 return this->initiator_pub_val;
193 }
194
195 METHOD(tcg_pts_attr_dh_nonce_finish_t, get_initiator_nonce, chunk_t,
196 private_tcg_pts_attr_dh_nonce_finish_t *this)
197 {
198 return this->initiator_nonce;
199 }
200
201 /**
202 * Described in header.
203 */
204 pa_tnc_attr_t *tcg_pts_attr_dh_nonce_finish_create(u_int8_t nonce_len,
205 pts_meas_algorithms_t hash_algo,
206 chunk_t initiator_nonce,
207 chunk_t initiator_pub_val)
208 {
209 private_tcg_pts_attr_dh_nonce_finish_t *this;
210
211 INIT(this,
212 .public = {
213 .pa_tnc_attribute = {
214 .get_vendor_id = _get_vendor_id,
215 .get_type = _get_type,
216 .get_value = _get_value,
217 .get_noskip_flag = _get_noskip_flag,
218 .set_noskip_flag = _set_noskip_flag,
219 .build = _build,
220 .process = _process,
221 .destroy = _destroy,
222 },
223 .get_nonce_len = _get_nonce_len,
224 .get_hash_algo = _get_hash_algo,
225 .get_initiator_nonce = _get_initiator_nonce,
226 .get_initiator_pub_val = _get_initiator_pub_val,
227 },
228 .vendor_id = PEN_TCG,
229 .type = TCG_PTS_DH_NONCE_FINISH,
230 .nonce_len = nonce_len,
231 .hash_algo = hash_algo,
232 .initiator_nonce = initiator_nonce,
233 .initiator_pub_val = initiator_pub_val,
234 );
235
236 return &this->public.pa_tnc_attribute;
237 }
238
239 /**
240 * Described in header.
241 */
242 pa_tnc_attr_t *tcg_pts_attr_dh_nonce_finish_create_from_data(chunk_t value)
243 {
244 private_tcg_pts_attr_dh_nonce_finish_t *this;
245
246 INIT(this,
247 .public = {
248 .pa_tnc_attribute = {
249 .get_vendor_id = _get_vendor_id,
250 .get_type = _get_type,
251 .get_value = _get_value,
252 .get_noskip_flag = _get_noskip_flag,
253 .set_noskip_flag = _set_noskip_flag,
254 .build = _build,
255 .process = _process,
256 .destroy = _destroy,
257 },
258 .get_nonce_len = _get_nonce_len,
259 .get_hash_algo = _get_hash_algo,
260 .get_initiator_nonce = _get_initiator_nonce,
261 .get_initiator_pub_val = _get_initiator_pub_val,
262 },
263 .vendor_id = PEN_TCG,
264 .type = TCG_PTS_DH_NONCE_FINISH,
265 .value = chunk_clone(value),
266 );
267
268 return &this->public.pa_tnc_attribute;
269 }