- added verify function to all payload types
[strongswan.git] / Source / charon / payloads / ke_payload.c
1 /**
2 * @file ke_payload.c
3 *
4 * @brief Declaration of the class ke_payload_t.
5 *
6 * An object of this type represents an IKEv2 KE-Payload.
7 *
8 * See section 3.4 of RFC for details of this payload type.
9 *
10 */
11
12 /*
13 * Copyright (C) 2005 Jan Hutter, Martin Willi
14 * Hochschule fuer Technik Rapperswil
15 *
16 * This program is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 2 of the License, or (at your
19 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
20 *
21 * This program is distributed in the hope that it will be useful, but
22 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
23 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
24 * for more details.
25 */
26
27 /* offsetof macro */
28 #include <stddef.h>
29
30 #include "ke_payload.h"
31
32 #include "encodings.h"
33 #include "../utils/allocator.h"
34
35
36 /**
37 * Private data of an ke_payload_t Object
38 *
39 */
40 typedef struct private_ke_payload_s private_ke_payload_t;
41
42 struct private_ke_payload_s {
43 /**
44 * public ke_payload_t interface
45 */
46 ke_payload_t public;
47
48 /**
49 * next payload type
50 */
51 u_int8_t next_payload;
52
53 /**
54 * Critical flag
55 */
56 bool critical;
57
58 /**
59 * Length of this payload
60 */
61 u_int16_t payload_length;
62
63
64 /**
65 * DH Group Number
66 */
67 u_int16_t dh_group_number;
68
69 /**
70 * Key Exchange Data of this KE payload
71 */
72 chunk_t key_exchange_data;
73
74 /**
75 * @brief Computes the length of this payload.
76 *
77 * @param this calling private_ke_payload_t object
78 * @return
79 * SUCCESS in any case
80 */
81 status_t (*compute_length) (private_ke_payload_t *this);
82 };
83
84 /**
85 * Encoding rules to parse or generate a IKEv2-KE Payload
86 *
87 * The defined offsets are the positions in a object of type
88 * private_ke_payload_t.
89 *
90 */
91 encoding_rule_t ke_payload_encodings[] = {
92 /* 1 Byte next payload type, stored in the field next_payload */
93 { U_INT_8, offsetof(private_ke_payload_t, next_payload) },
94 /* the critical bit */
95 { FLAG, offsetof(private_ke_payload_t, critical) },
96 /* 7 Bit reserved bits, nowhere stored */
97 { RESERVED_BIT, 0 },
98 { RESERVED_BIT, 0 },
99 { RESERVED_BIT, 0 },
100 { RESERVED_BIT, 0 },
101 { RESERVED_BIT, 0 },
102 { RESERVED_BIT, 0 },
103 { RESERVED_BIT, 0 },
104 /* Length of the whole payload*/
105 { PAYLOAD_LENGTH, offsetof(private_ke_payload_t, payload_length) },
106 /* DH Group number as 16 bit field*/
107 { U_INT_16, offsetof(private_ke_payload_t, dh_group_number) },
108 { RESERVED_BYTE, 0 },
109 { RESERVED_BYTE, 0 },
110 /* Key Exchange Data is from variable size */
111 { KEY_EXCHANGE_DATA, offsetof(private_ke_payload_t, key_exchange_data) }
112 };
113
114 /*
115 1 2 3
116 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
117 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
118 ! Next Payload !C! RESERVED ! Payload Length !
119 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
120 ! DH Group # ! RESERVED !
121 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
122 ! !
123 ~ Key Exchange Data ~
124 ! !
125 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
126 */
127
128 /**
129 * Implements payload_t's verify function.
130 * See #payload_s.verify for description.
131 */
132 static status_t verify(private_ke_payload_t *this)
133 {
134 if (this->critical)
135 {
136 /* critical bit is set! */
137 return FAILED;
138 }
139
140 /* dh group is not verified in here */
141 return SUCCESS;
142 }
143
144 /**
145 * Implements payload_t's and ke_payload_t's destroy function.
146 * See #payload_s.destroy or ke_payload_s.destroy for description.
147 */
148 static status_t destroy(private_ke_payload_t *this)
149 {
150 if (this->key_exchange_data.ptr != NULL)
151 {
152 allocator_free(this->key_exchange_data.ptr);
153 }
154 allocator_free(this);
155 return SUCCESS;
156 }
157
158 /**
159 * Implements payload_t's get_encoding_rules function.
160 * See #payload_s.get_encoding_rules for description.
161 */
162 static status_t get_encoding_rules(private_ke_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
163 {
164 *rules = ke_payload_encodings;
165 *rule_count = sizeof(ke_payload_encodings) / sizeof(encoding_rule_t);
166
167 return SUCCESS;
168 }
169
170 /**
171 * Implements payload_t's get_type function.
172 * See #payload_s.get_type for description.
173 */
174 static payload_type_t get_type(private_ke_payload_t *this)
175 {
176 return KEY_EXCHANGE;
177 }
178
179 /**
180 * Implements payload_t's get_next_type function.
181 * See #payload_s.get_next_type for description.
182 */
183 static payload_type_t get_next_type(private_ke_payload_t *this)
184 {
185 return (this->next_payload);
186 }
187
188 /**
189 * Implements payload_t's set_next_type function.
190 * See #payload_s.set_next_type for description.
191 */
192 static status_t set_next_type(private_ke_payload_t *this,payload_type_t type)
193 {
194 this->next_payload = type;
195 return SUCCESS;
196 }
197
198 /**
199 * Implements payload_t's get_length function.
200 * See #payload_s.get_length for description.
201 */
202 static size_t get_length(private_ke_payload_t *this)
203 {
204 this->compute_length(this);
205 return this->payload_length;
206 }
207
208 /**
209 * Implements private_ke_payload_t's compute_length function.
210 * See #private_ke_payload_s.compute_length for description.
211 */
212 static status_t compute_length (private_ke_payload_t *this)
213 {
214 size_t length = KE_PAYLOAD_HEADER_LENGTH;
215 if (this->key_exchange_data.ptr != NULL)
216 {
217 length += this->key_exchange_data.len;
218 }
219
220 this->payload_length = length;
221
222 return SUCCESS;
223 }
224
225
226 /**
227 * Implements ke_payload_t's get_key_exchange_data function.
228 * See #ke_payload_t.get_key_exchange_data for description.
229 */
230 chunk_t get_key_exchange_data(private_ke_payload_t *this)
231 {
232 return (this->key_exchange_data);
233 }
234
235 /**
236 * Implements ke_payload_t's set_key_exchange_data function.
237 * See #ke_payload_t.set_key_exchange_data for description.
238 */
239 status_t set_key_exchange_data(private_ke_payload_t *this, chunk_t key_exchange_data)
240 {
241 /* destroy existing data first */
242 if (this->key_exchange_data.ptr != NULL)
243 {
244 /* free existing value */
245 allocator_free(this->key_exchange_data.ptr);
246 this->key_exchange_data.ptr = NULL;
247 this->key_exchange_data.len = 0;
248
249 }
250
251 this->key_exchange_data.ptr = allocator_clone_bytes(key_exchange_data.ptr,key_exchange_data.len);
252 if (this->key_exchange_data.ptr == NULL)
253 {
254 return OUT_OF_RES;
255 }
256 this->key_exchange_data.len = key_exchange_data.len;
257 this->compute_length(this);
258
259 return SUCCESS;
260 }
261
262 /**
263 * Implements ke_payload_t's get_dh_group_number function.
264 * See #ke_payload_t.get_dh_group_number for description.
265 */
266 u_int16_t get_dh_group_number(private_ke_payload_t *this)
267 {
268 return this->dh_group_number;
269 }
270
271 /**
272 * Implements ke_payload_t's set_dh_group_number function.
273 * See #ke_payload_t.set_dh_group_number for description.
274 */
275 status_t set_dh_group_number(private_ke_payload_t *this, u_int16_t dh_group_number)
276 {
277 this->dh_group_number = dh_group_number;
278 return SUCCESS;
279 }
280
281 /*
282 * Described in header
283 */
284 ke_payload_t *ke_payload_create()
285 {
286 private_ke_payload_t *this = allocator_alloc_thing(private_ke_payload_t);
287 if (this == NULL)
288 {
289 return NULL;
290 }
291 /* interface functions */
292 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
293 this->public.payload_interface.get_encoding_rules = (status_t (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
294 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
295 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
296 this->public.payload_interface.set_next_type = (status_t (*) (payload_t *,payload_type_t)) set_next_type;
297 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
298 this->public.payload_interface.destroy = (status_t (*) (payload_t *))destroy;
299
300 /* public functions */
301 this->public.get_key_exchange_data = (chunk_t (*) (ke_payload_t *)) get_key_exchange_data;
302 this->public.set_key_exchange_data = (status_t (*) (ke_payload_t *,chunk_t)) set_key_exchange_data;
303 this->public.get_dh_group_number = (u_int16_t (*) (ke_payload_t *)) get_dh_group_number;
304 this->public.set_dh_group_number =(status_t (*) (ke_payload_t *,u_int16_t)) set_dh_group_number;
305 this->public.destroy = (status_t (*) (ke_payload_t *)) destroy;
306
307 /* private functions */
308 this->compute_length = compute_length;
309
310 /* set default values of the fields */
311 this->critical = KE_PAYLOAD_CRITICAL_FLAG;
312 this->next_payload = NO_PAYLOAD;
313 this->payload_length = KE_PAYLOAD_HEADER_LENGTH;
314 this->key_exchange_data.ptr = NULL;
315 this->key_exchange_data.len = 0;
316 this->dh_group_number = 0;
317
318 return (&(this->public));
319 }