- added verify function to all payload types
[strongswan.git] / Source / charon / payloads / notify_payload.c
1 /**
2 * @file notify_payload.c
3 *
4 * @brief Declaration of the class notify_payload_t.
5 *
6 * An object of this type represents an IKEv2 Notify-Payload.
7 *
8 * See section 3.10 of Draft 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 "notify_payload.h"
31
32 #include "encodings.h"
33 #include "../utils/allocator.h"
34
35 /**
36 * Private data of an notify_payload_t Object
37 *
38 */
39 typedef struct private_notify_payload_s private_notify_payload_t;
40
41 struct private_notify_payload_s {
42 /**
43 * public notify_payload_t interface
44 */
45 notify_payload_t public;
46
47 /**
48 * next payload type
49 */
50 u_int8_t next_payload;
51
52 /**
53 * Critical flag
54 */
55 bool critical;
56
57 /**
58 * Length of this payload
59 */
60 u_int16_t payload_length;
61
62 /**
63 * protocol id
64 */
65 u_int8_t protocol_id;
66
67 /**
68 * spi size
69 */
70 u_int8_t spi_size;
71
72 /**
73 * notify message type
74 */
75 u_int16_t notify_message_type;
76
77 /**
78 * Security parameter index (spi)
79 */
80 chunk_t spi;
81
82 /**
83 * Notification data
84 */
85 chunk_t notification_data;
86
87 /**
88 * @brief Computes the length of this payload.
89 *
90 * @param this calling private_ke_payload_t object
91 * @return
92 * SUCCESS in any case
93 */
94 status_t (*compute_length) (private_notify_payload_t *this);
95 };
96
97 /**
98 * Encoding rules to parse or generate a IKEv2-Notify Payload
99 *
100 * The defined offsets are the positions in a object of type
101 * private_notify_payload_t.
102 *
103 */
104 encoding_rule_t notify_payload_encodings[] = {
105 /* 1 Byte next payload type, stored in the field next_payload */
106 { U_INT_8, offsetof(private_notify_payload_t, next_payload) },
107 /* the critical bit */
108 { FLAG, offsetof(private_notify_payload_t, critical) },
109 /* 7 Bit reserved bits, nowhere stored */
110 { RESERVED_BIT, 0 },
111 { RESERVED_BIT, 0 },
112 { RESERVED_BIT, 0 },
113 { RESERVED_BIT, 0 },
114 { RESERVED_BIT, 0 },
115 { RESERVED_BIT, 0 },
116 { RESERVED_BIT, 0 },
117 /* Length of the whole payload*/
118 { PAYLOAD_LENGTH, offsetof(private_notify_payload_t, payload_length) },
119 /* Protocol ID as 8 bit field*/
120 { U_INT_8, offsetof(private_notify_payload_t, protocol_id) },
121 /* SPI Size as 8 bit field*/
122 { SPI_SIZE, offsetof(private_notify_payload_t, spi_size) },
123 /* Notify message type as 16 bit field*/
124 { U_INT_16, offsetof(private_notify_payload_t, notify_message_type) },
125 /* SPI as variable length field*/
126 { SPI, offsetof(private_notify_payload_t, spi) },
127 /* Key Exchange Data is from variable size */
128 { NOTIFICATION_DATA, offsetof(private_notify_payload_t, notification_data) }
129 };
130
131 /*
132 1 2 3
133 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
134 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
135 ! Next Payload !C! RESERVED ! Payload Length !
136 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
137 ! Protocol ID ! SPI Size ! Notify Message Type !
138 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139 ! !
140 ~ Security Parameter Index (SPI) ~
141 ! !
142 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
143 ! !
144 ~ Notification Data ~
145 ! !
146 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
147 */
148
149 /**
150 * Implements payload_t's verify function.
151 * See #payload_s.verify for description.
152 */
153 static status_t verify(private_notify_payload_t *this)
154 {
155 if (this->critical)
156 {
157 /* critical bit is set! */
158 return FAILED;
159 }
160 if (this->protocol_id > 3)
161 {
162 /* reserved for future use */
163 return FAILED;
164 }
165
166 /* notify message types and data is not getting checked in here */
167
168 return SUCCESS;
169 }
170
171 /**
172 * Implements payload_t's and notify_payload_t's destroy function.
173 * See #payload_s.destroy or notify_payload_s.destroy for description.
174 */
175 static status_t destroy(private_notify_payload_t *this)
176 {
177 if (this->notification_data.ptr != NULL)
178 {
179 allocator_free(this->notification_data.ptr);
180 }
181 if (this->spi.ptr != NULL)
182 {
183 allocator_free(this->spi.ptr);
184 }
185
186 allocator_free(this);
187 return SUCCESS;
188 }
189
190 /**
191 * Implements payload_t's get_encoding_rules function.
192 * See #payload_s.get_encoding_rules for description.
193 */
194 static status_t get_encoding_rules(private_notify_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
195 {
196 *rules = notify_payload_encodings;
197 *rule_count = sizeof(notify_payload_encodings) / sizeof(encoding_rule_t);
198 return SUCCESS;
199 }
200
201 /**
202 * Implements payload_t's get_type function.
203 * See #payload_s.get_type for description.
204 */
205 static payload_type_t get_type(private_notify_payload_t *this)
206 {
207 return KEY_EXCHANGE;
208 }
209
210 /**
211 * Implements payload_t's get_next_type function.
212 * See #payload_s.get_next_type for description.
213 */
214 static payload_type_t get_next_type(private_notify_payload_t *this)
215 {
216 return (this->next_payload);
217 }
218
219 /**
220 * Implements payload_t's set_next_type function.
221 * See #payload_s.set_next_type for description.
222 */
223 static status_t set_next_type(private_notify_payload_t *this,payload_type_t type)
224 {
225 this->next_payload = type;
226 return SUCCESS;
227 }
228
229 /**
230 * Implements payload_t's get_length function.
231 * See #payload_s.get_length for description.
232 */
233 static size_t get_length(private_notify_payload_t *this)
234 {
235 this->compute_length(this);
236 return this->payload_length;
237 }
238
239 /**
240 * Implements private_ke_payload_t's compute_length function.
241 * See #private_ke_payload_s.compute_length for description.
242 */
243 static status_t compute_length (private_notify_payload_t *this)
244 {
245 size_t length = NOTIFY_PAYLOAD_HEADER_LENGTH;
246 if (this->notification_data.ptr != NULL)
247 {
248 length += this->notification_data.len;
249 }
250 if (this->spi.ptr != NULL)
251 {
252 length += this->spi.len;
253 }
254
255 this->payload_length = length;
256
257 return SUCCESS;
258 }
259
260
261 /**
262 * Implements notify_payload_t's get_protocol_id function.
263 * See #notify_payload_s.get_protocol_id for description.
264 */
265 u_int8_t get_protocol_id(private_notify_payload_t *this)
266 {
267 return this->protocol_id;
268 }
269
270 /**
271 * Implements notify_payload_t's set_protocol_id function.
272 * See #notify_payload_s.set_protocol_id for description.
273 */
274 status_t set_protocol_id(private_notify_payload_t *this, u_int8_t protocol_id)
275 {
276 this->protocol_id = protocol_id;
277 return SUCCESS;
278 }
279
280 /**
281 * Implements notify_payload_t's get_notification_data function.
282 * See #notify_payload_s.get_notification_data for description.
283 */
284 u_int16_t get_notify_message_type(private_notify_payload_t *this)
285 {
286 return this->notify_message_type;
287 }
288
289 /**
290 * Implements notify_payload_t's get_notification_data function.
291 * See #notify_payload_s.get_notification_data for description.
292 */
293 status_t set_notify_message_type(private_notify_payload_t *this, u_int16_t notify_message_type)
294 {
295 this->notify_message_type = notify_message_type;
296 return SUCCESS;
297 }
298
299 /**
300 * Implements notify_payload_t's get_spi function.
301 * See #notify_payload_s.get_spi for description.
302 */
303 chunk_t get_spi(private_notify_payload_t *this)
304 {
305 return (this->spi);
306 }
307
308 /**
309 * Implements notify_payload_t's set_spi function.
310 * See #notify_payload_s.set_spi for description.
311 */
312 status_t set_spi(private_notify_payload_t *this, chunk_t spi)
313 {
314 /* destroy existing data first */
315 if (this->spi.ptr != NULL)
316 {
317 /* free existing value */
318 allocator_free(this->spi.ptr);
319 this->spi.ptr = NULL;
320 this->spi.len = 0;
321
322 }
323
324 this->spi.ptr = allocator_clone_bytes(spi.ptr,spi.len);
325 if (this->spi.ptr == NULL)
326 {
327 return OUT_OF_RES;
328 }
329 this->spi.len = spi.len;
330 this->spi_size = spi.len;
331 this->compute_length(this);
332
333 return SUCCESS;
334 }
335
336
337 /**
338 * Implements notify_payload_t's get_notification_data function.
339 * See #notify_payload_s.get_notification_data for description.
340 */
341 chunk_t get_notification_data(private_notify_payload_t *this)
342 {
343 return (this->notification_data);
344 }
345
346 /**
347 * Implements notify_payload_t's get_notification_data function.
348 * See #notify_payload_s.get_notification_data for description.
349 */
350 status_t set_notification_data(private_notify_payload_t *this, chunk_t notification_data)
351 {
352 /* destroy existing data first */
353 if (this->notification_data.ptr != NULL)
354 {
355 /* free existing value */
356 allocator_free(this->notification_data.ptr);
357 this->notification_data.ptr = NULL;
358 this->notification_data.len = 0;
359
360 }
361
362 this->notification_data.ptr = allocator_clone_bytes(notification_data.ptr,notification_data.len);
363 if (this->notification_data.ptr == NULL)
364 {
365 return OUT_OF_RES;
366 }
367 this->notification_data.len = notification_data.len;
368 this->compute_length(this);
369
370 return SUCCESS;
371 }
372
373 /*
374 * Described in header
375 */
376 notify_payload_t *notify_payload_create()
377 {
378 private_notify_payload_t *this = allocator_alloc_thing(private_notify_payload_t);
379 if (this == NULL)
380 {
381 return NULL;
382 }
383 /* interface functions */
384 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
385 this->public.payload_interface.get_encoding_rules = (status_t (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
386 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
387 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
388 this->public.payload_interface.set_next_type = (status_t (*) (payload_t *,payload_type_t)) set_next_type;
389 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
390 this->public.payload_interface.destroy = (status_t (*) (payload_t *))destroy;
391
392 /* public functions */
393 this->public.get_protocol_id = (u_int8_t (*) (notify_payload_t *)) get_protocol_id;
394 this->public.set_protocol_id = (status_t (*) (notify_payload_t *,u_int8_t)) set_protocol_id;
395 this->public.get_notify_message_type = (u_int16_t (*) (notify_payload_t *)) get_notify_message_type;
396 this->public.set_notify_message_type = (status_t (*) (notify_payload_t *,u_int16_t)) set_notify_message_type;
397 this->public.get_spi = (chunk_t (*) (notify_payload_t *)) get_spi;
398 this->public.set_spi = (status_t (*) (notify_payload_t *,chunk_t)) set_spi;
399 this->public.get_notification_data = (chunk_t (*) (notify_payload_t *)) get_notification_data;
400 this->public.set_notification_data = (status_t (*) (notify_payload_t *,chunk_t)) set_notification_data;
401 this->public.destroy = (status_t (*) (notify_payload_t *)) destroy;
402
403 /* private functions */
404 this->compute_length = compute_length;
405
406 /* set default values of the fields */
407 this->critical = NOTIFY_PAYLOAD_CRITICAL_FLAG;
408 this->next_payload = NO_PAYLOAD;
409 this->payload_length = NOTIFY_PAYLOAD_HEADER_LENGTH;
410 this->protocol_id = 0;
411 this->notify_message_type = 0;
412 this->spi.ptr = NULL;
413 this->spi.len = 0;
414 this->notification_data.ptr = NULL;
415 this->notification_data.len = 0;
416
417 return (&(this->public));
418 }
419