f1ff1c830b778b1450d3cd104fba0287c2608d6e
[strongswan.git] / src / libimcv / ietf / ietf_attr_product_info.c
1 /*
2 * Copyright (C) 2011-2014 Andreas Steffen
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 "ietf_attr_product_info.h"
17
18 #include <pa_tnc/pa_tnc_msg.h>
19 #include <bio/bio_writer.h>
20 #include <bio/bio_reader.h>
21 #include <utils/debug.h>
22
23 typedef struct private_ietf_attr_product_info_t private_ietf_attr_product_info_t;
24
25 /**
26 * PA-TNC Product Information type (see section 4.2.2 of RFC 5792)
27 *
28 * 1 2 3
29 * 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
30 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
31 * | Product Vendor ID | Product ID |
32 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
33 * | Product ID | Product Name (Variable Length) |
34 * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
35 */
36
37 #define PRODUCT_INFO_MIN_SIZE 5
38
39 /**
40 * Private data of an ietf_attr_product_info_t object.
41 */
42 struct private_ietf_attr_product_info_t {
43
44 /**
45 * Public members of ietf_attr_product_info_t
46 */
47 ietf_attr_product_info_t public;
48
49 /**
50 * Vendor-specific attribute type
51 */
52 pen_type_t type;
53
54 /**
55 * Length of attribute value
56 */
57 size_t length;
58
59 /**
60 * Attribute value or segment
61 */
62 chunk_t value;
63
64 /**
65 * Noskip flag
66 */
67 bool noskip_flag;
68
69 /**
70 * Product vendor ID
71 */
72 pen_t product_vendor_id;
73
74 /**
75 * Product ID
76 */
77 u_int16_t product_id;
78
79 /**
80 * Product Name
81 */
82 chunk_t product_name;
83
84 /**
85 * Reference count
86 */
87 refcount_t ref;
88 };
89
90 METHOD(pa_tnc_attr_t, get_type, pen_type_t,
91 private_ietf_attr_product_info_t *this)
92 {
93 return this->type;
94 }
95
96 METHOD(pa_tnc_attr_t, get_value, chunk_t,
97 private_ietf_attr_product_info_t *this)
98 {
99 return this->value;
100 }
101
102 METHOD(pa_tnc_attr_t, get_noskip_flag, bool,
103 private_ietf_attr_product_info_t *this)
104 {
105 return this->noskip_flag;
106 }
107
108 METHOD(pa_tnc_attr_t, set_noskip_flag,void,
109 private_ietf_attr_product_info_t *this, bool noskip)
110 {
111 this->noskip_flag = noskip;
112 }
113
114 METHOD(pa_tnc_attr_t, build, void,
115 private_ietf_attr_product_info_t *this)
116 {
117 bio_writer_t *writer;
118
119 if (this->value.ptr)
120 {
121 return;
122 }
123 writer = bio_writer_create(PRODUCT_INFO_MIN_SIZE);
124 writer->write_uint24(writer, this->product_vendor_id);
125 writer->write_uint16(writer, this->product_id);
126 writer->write_data (writer, this->product_name);
127
128 this->value = writer->extract_buf(writer);
129 this->length = this->value.len;
130 writer->destroy(writer);
131 }
132
133 METHOD(pa_tnc_attr_t, process, status_t,
134 private_ietf_attr_product_info_t *this, u_int32_t *offset)
135 {
136 bio_reader_t *reader;
137 chunk_t product_name;
138
139 *offset = 0;
140
141 if (this->value.len < this->length)
142 {
143 return NEED_MORE;
144 }
145 if (this->value.len < PRODUCT_INFO_MIN_SIZE)
146 {
147 DBG1(DBG_TNC, "insufficient data for IETF product information");
148 return FAILED;
149 }
150 reader = bio_reader_create(this->value);
151 reader->read_uint24(reader, &this->product_vendor_id);
152 reader->read_uint16(reader, &this->product_id);
153 reader->read_data (reader, reader->remaining(reader), &product_name);
154 reader->destroy(reader);
155
156 if (!this->product_vendor_id && this->product_id)
157 {
158 DBG1(DBG_TNC, "IETF product information vendor ID is 0 "
159 "but product ID is not 0");
160 *offset = 3;
161 return FAILED;
162 }
163 this->product_name = chunk_clone(product_name);
164
165 return SUCCESS;
166 }
167
168 METHOD(pa_tnc_attr_t, get_ref, pa_tnc_attr_t*,
169 private_ietf_attr_product_info_t *this)
170 {
171 ref_get(&this->ref);
172 return &this->public.pa_tnc_attribute;
173 }
174
175 METHOD(pa_tnc_attr_t, destroy, void,
176 private_ietf_attr_product_info_t *this)
177 {
178 if (ref_put(&this->ref))
179 {
180 free(this->product_name.ptr);
181 free(this->value.ptr);
182 free(this);
183 }
184 }
185
186 METHOD(ietf_attr_product_info_t, get_info, chunk_t,
187 private_ietf_attr_product_info_t *this, pen_t *vendor_id, u_int16_t *id)
188 {
189 if (vendor_id)
190 {
191 *vendor_id = this->product_vendor_id;
192 }
193 if (id)
194 {
195 *id = this->product_id;
196 }
197 return this->product_name;
198 }
199
200 /**
201 * Described in header.
202 */
203 pa_tnc_attr_t *ietf_attr_product_info_create(pen_t vendor_id, u_int16_t id,
204 chunk_t name)
205 {
206 private_ietf_attr_product_info_t *this;
207
208 INIT(this,
209 .public = {
210 .pa_tnc_attribute = {
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 .get_ref = _get_ref,
218 .destroy = _destroy,
219 },
220 .get_info = _get_info,
221 },
222 .type = { PEN_IETF, IETF_ATTR_PRODUCT_INFORMATION },
223 .product_vendor_id = vendor_id,
224 .product_id = id,
225 .product_name = chunk_clone(name),
226 .ref = 1,
227 );
228
229 return &this->public.pa_tnc_attribute;
230 }
231
232 /**
233 * Described in header.
234 */
235 pa_tnc_attr_t *ietf_attr_product_info_create_from_data(size_t length,
236 chunk_t data)
237 {
238 private_ietf_attr_product_info_t *this;
239
240 INIT(this,
241 .public = {
242 .pa_tnc_attribute = {
243 .get_type = _get_type,
244 .get_value = _get_value,
245 .get_noskip_flag = _get_noskip_flag,
246 .set_noskip_flag = _set_noskip_flag,
247 .build = _build,
248 .process = _process,
249 .get_ref = _get_ref,
250 .destroy = _destroy,
251 },
252 .get_info = _get_info,
253 },
254 .type = { PEN_IETF, IETF_ATTR_PRODUCT_INFORMATION },
255 .length = length,
256 .value = chunk_clone(data),
257 .ref = 1,
258 );
259
260 return &this->public.pa_tnc_attribute;
261 }
262