Implemented Diffie Hellman Nonce attributes
[strongswan.git] / src / libpts / tcg / tcg_pts_attr_dh_nonce_params_req.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_req.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_req_t private_tcg_pts_attr_dh_nonce_params_req_t;
24
25 /**
26 * PTS DH Nonce Parameters Request
27 * see section 3.8.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 | Min. Nonce Len | D-H Group Set |
33 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34 *
35 */
36
37 #define PTS_DH_NONCE_PARAMS_REQ_SIZE 4
38 #define PTS_DH_NONCE_PARAMS_REQ_RESERVED 0x00
39
40 /**
41 * Private data of an tcg_pts_attr_dh_nonce_params_req_t object.
42 */
43 struct private_tcg_pts_attr_dh_nonce_params_req_t {
44
45 /**
46 * Public members of tcg_pts_attr_dh_nonce_params_req_t
47 */
48 tcg_pts_attr_dh_nonce_params_req_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 * Minimum acceptable length of nonce
72 */
73 u_int8_t min_nonce_len;
74
75 /**
76 * Diffie Hellman group set
77 */
78 pts_dh_group_t dh_groups;
79
80 };
81
82 METHOD(pa_tnc_attr_t, get_vendor_id, pen_t,
83 private_tcg_pts_attr_dh_nonce_params_req_t *this)
84 {
85 return this->vendor_id;
86 }
87
88 METHOD(pa_tnc_attr_t, get_type, u_int32_t,
89 private_tcg_pts_attr_dh_nonce_params_req_t *this)
90 {
91 return this->type;
92 }
93
94 METHOD(pa_tnc_attr_t, get_value, chunk_t,
95 private_tcg_pts_attr_dh_nonce_params_req_t *this)
96 {
97 return this->value;
98 }
99
100 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
101 private_tcg_pts_attr_dh_nonce_params_req_t *this)
102 {
103 return this->noskip_flag;
104 }
105
106 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
107 private_tcg_pts_attr_dh_nonce_params_req_t *this, bool noskip)
108 {
109 this->noskip_flag = noskip;
110 }
111
112 METHOD(pa_tnc_attr_t, build, void,
113 private_tcg_pts_attr_dh_nonce_params_req_t *this)
114 {
115 bio_writer_t *writer;
116
117 writer = bio_writer_create(PTS_DH_NONCE_PARAMS_REQ_SIZE);
118 writer->write_uint8(writer, PTS_DH_NONCE_PARAMS_REQ_RESERVED);
119 writer->write_uint8(writer, this->min_nonce_len);
120 writer->write_uint8(writer, this->dh_groups);
121
122 this->value = chunk_clone(writer->get_buf(writer));
123 writer->destroy(writer);
124 }
125
126 METHOD(pa_tnc_attr_t, process, status_t,
127 private_tcg_pts_attr_dh_nonce_params_req_t *this, u_int32_t *offset)
128 {
129 bio_reader_t *reader;
130 u_int8_t reserved;
131 u_int16_t dh_groups;
132
133 if (this->value.len < PTS_DH_NONCE_PARAMS_REQ_SIZE)
134 {
135 DBG1(DBG_TNC, "insufficient data for PTS DH Nonce Parameters Request");
136 *offset = 0;
137 return FAILED;
138 }
139 reader = bio_reader_create(this->value);
140 reader->read_uint8(reader, &reserved);
141 reader->read_uint8(reader, &this->min_nonce_len);
142 reader->read_uint16(reader, &dh_groups);
143 this->dh_groups = dh_groups;
144 reader->destroy(reader);
145
146 return SUCCESS;
147 }
148
149 METHOD(pa_tnc_attr_t, destroy, void,
150 private_tcg_pts_attr_dh_nonce_params_req_t *this)
151 {
152 free(this->value.ptr);
153 free(this);
154 }
155
156 METHOD(tcg_pts_attr_dh_nonce_params_req_t, get_min_nonce_len, u_int8_t,
157 private_tcg_pts_attr_dh_nonce_params_req_t *this)
158 {
159 return this->min_nonce_len;
160 }
161
162 METHOD(tcg_pts_attr_dh_nonce_params_req_t, get_dh_groups, pts_dh_group_t,
163 private_tcg_pts_attr_dh_nonce_params_req_t *this)
164 {
165 return this->dh_groups;
166 }
167
168 /**
169 * Described in header.
170 */
171 pa_tnc_attr_t *tcg_pts_attr_dh_nonce_params_req_create(u_int8_t min_nonce_len,
172 pts_dh_group_t dh_groups)
173 {
174 private_tcg_pts_attr_dh_nonce_params_req_t *this;
175
176 INIT(this,
177 .public = {
178 .pa_tnc_attribute = {
179 .get_vendor_id = _get_vendor_id,
180 .get_type = _get_type,
181 .get_value = _get_value,
182 .get_noskip_flag = _get_noskip_flag,
183 .set_noskip_flag = _set_noskip_flag,
184 .build = _build,
185 .process = _process,
186 .destroy = _destroy,
187 },
188 .get_min_nonce_len = _get_min_nonce_len,
189 .get_dh_groups = _get_dh_groups,
190 },
191 .vendor_id = PEN_TCG,
192 .type = TCG_PTS_DH_NONCE_PARAMS_REQ,
193 .min_nonce_len = min_nonce_len,
194 .dh_groups = dh_groups,
195 );
196
197 return &this->public.pa_tnc_attribute;
198 }
199
200 /**
201 * Described in header.
202 */
203 pa_tnc_attr_t *tcg_pts_attr_dh_nonce_params_req_create_from_data(chunk_t value)
204 {
205 private_tcg_pts_attr_dh_nonce_params_req_t *this;
206
207 INIT(this,
208 .public = {
209 .pa_tnc_attribute = {
210 .get_vendor_id = _get_vendor_id,
211 .get_type = _get_type,
212 .get_value = _get_value,
213 .get_noskip_flag = _get_noskip_flag,
214 .set_noskip_flag = _set_noskip_flag,
215 .build = _build,
216 .process = _process,
217 .destroy = _destroy,
218 },
219 .get_min_nonce_len = _get_min_nonce_len,
220 .get_dh_groups = _get_dh_groups,
221 },
222 .vendor_id = PEN_TCG,
223 .type = TCG_PTS_DH_NONCE_PARAMS_REQ,
224 .value = chunk_clone(value),
225 );
226
227 return &this->public.pa_tnc_attribute;
228 }