31e02d2d6350f359b2552bc1b8d93d21d26ff757
[strongswan.git] / Source / charon / encoding / payloads / notify_payload.c
1 /**
2 * @file notify_payload.c
3 *
4 * @brief Implementation of notify_payload_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 /* offsetof macro */
24 #include <stddef.h>
25
26 #include "notify_payload.h"
27
28 #include <encoding/payloads/encodings.h>
29 #include <utils/allocator.h>
30
31
32 typedef struct private_notify_payload_t private_notify_payload_t;
33
34 /**
35 * Private data of an notify_payload_t object.
36 *
37 */
38 struct private_notify_payload_t {
39 /**
40 * Public notify_payload_t interface.
41 */
42 notify_payload_t public;
43
44 /**
45 * Next payload type.
46 */
47 u_int8_t next_payload;
48
49 /**
50 * Critical flag.
51 */
52 bool critical;
53
54 /**
55 * Length of this payload.
56 */
57 u_int16_t payload_length;
58
59 /**
60 * Protocol id.
61 */
62 u_int8_t protocol_id;
63
64 /**
65 * Spi size.
66 */
67 u_int8_t spi_size;
68
69 /**
70 * Notify message type.
71 */
72 u_int16_t notify_message_type;
73
74 /**
75 * Security parameter index (spi).
76 */
77 chunk_t spi;
78
79 /**
80 * Notification data.
81 */
82 chunk_t notification_data;
83
84 /**
85 * @brief Computes the length of this payload.
86 *
87 * @param this calling private_ke_payload_t object
88 */
89 void (*compute_length) (private_notify_payload_t *this);
90 };
91
92 /**
93 * Encoding rules to parse or generate a IKEv2-Notify Payload.
94 *
95 * The defined offsets are the positions in a object of type
96 * private_notify_payload_t.
97 *
98 */
99 encoding_rule_t notify_payload_encodings[] = {
100 /* 1 Byte next payload type, stored in the field next_payload */
101 { U_INT_8, offsetof(private_notify_payload_t, next_payload) },
102 /* the critical bit */
103 { FLAG, offsetof(private_notify_payload_t, critical) },
104 /* 7 Bit reserved bits, nowhere stored */
105 { RESERVED_BIT, 0 },
106 { RESERVED_BIT, 0 },
107 { RESERVED_BIT, 0 },
108 { RESERVED_BIT, 0 },
109 { RESERVED_BIT, 0 },
110 { RESERVED_BIT, 0 },
111 { RESERVED_BIT, 0 },
112 /* Length of the whole payload*/
113 { PAYLOAD_LENGTH, offsetof(private_notify_payload_t, payload_length) },
114 /* Protocol ID as 8 bit field*/
115 { U_INT_8, offsetof(private_notify_payload_t, protocol_id) },
116 /* SPI Size as 8 bit field*/
117 { SPI_SIZE, offsetof(private_notify_payload_t, spi_size) },
118 /* Notify message type as 16 bit field*/
119 { U_INT_16, offsetof(private_notify_payload_t, notify_message_type) },
120 /* SPI as variable length field*/
121 { SPI, offsetof(private_notify_payload_t, spi) },
122 /* Key Exchange Data is from variable size */
123 { NOTIFICATION_DATA, offsetof(private_notify_payload_t, notification_data) }
124 };
125
126 /*
127 1 2 3
128 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
129 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
130 ! Next Payload !C! RESERVED ! Payload Length !
131 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
132 ! Protocol ID ! SPI Size ! Notify Message Type !
133 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
134 ! !
135 ~ Security Parameter Index (SPI) ~
136 ! !
137 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
138 ! !
139 ~ Notification Data ~
140 ! !
141 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
142 */
143
144 /**
145 * Implementation of payload_t.verify.
146 */
147 static status_t verify(private_notify_payload_t *this)
148 {
149 if (this->critical)
150 {
151 /* critical bit is set! */
152 return FAILED;
153 }
154 if (this->protocol_id > 3)
155 {
156 /* reserved for future use */
157 return FAILED;
158 }
159
160 /* notify message types and data is not getting checked in here */
161
162 return SUCCESS;
163 }
164
165 /**
166 * Implementation of payload_t.get_encoding_rules.
167 */
168 static void get_encoding_rules(private_notify_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
169 {
170 *rules = notify_payload_encodings;
171 *rule_count = sizeof(notify_payload_encodings) / sizeof(encoding_rule_t);
172 }
173
174 /**
175 * Implementation of payload_t.get_type.
176 */
177 static payload_type_t get_type(private_notify_payload_t *this)
178 {
179 return KEY_EXCHANGE;
180 }
181
182 /**
183 * Implementation of payload_t.get_next_type.
184 */
185 static payload_type_t get_next_type(private_notify_payload_t *this)
186 {
187 return (this->next_payload);
188 }
189
190 /**
191 * Implementation of payload_t.set_next_type.
192 */
193 static void set_next_type(private_notify_payload_t *this,payload_type_t type)
194 {
195 this->next_payload = type;
196 }
197
198 /**
199 * Implementation of payload_t.get_length.
200 */
201 static size_t get_length(private_notify_payload_t *this)
202 {
203 this->compute_length(this);
204 return this->payload_length;
205 }
206
207 /**
208 * Implementation of private_notify_payload_t.compute_length.
209 */
210 static void compute_length (private_notify_payload_t *this)
211 {
212 size_t length = NOTIFY_PAYLOAD_HEADER_LENGTH;
213 if (this->notification_data.ptr != NULL)
214 {
215 length += this->notification_data.len;
216 }
217 if (this->spi.ptr != NULL)
218 {
219 length += this->spi.len;
220 }
221
222 this->payload_length = length;
223
224 }
225
226 /**
227 * Implementation of notify_payload_t.get_protocol_id.
228 */
229 static u_int8_t get_protocol_id(private_notify_payload_t *this)
230 {
231 return this->protocol_id;
232 }
233
234 /**
235 * Implementation of notify_payload_t.set_protocol_id.
236 */
237 static void set_protocol_id(private_notify_payload_t *this, u_int8_t protocol_id)
238 {
239 this->protocol_id = protocol_id;
240 }
241
242 /**
243 * Implementation of notify_payload_t.get_notify_message_type.
244 */
245 static u_int16_t get_notify_message_type(private_notify_payload_t *this)
246 {
247 return this->notify_message_type;
248 }
249
250 /**
251 * Implementation of notify_payload_t.set_notify_message_type.
252 */
253 static void set_notify_message_type(private_notify_payload_t *this, u_int16_t notify_message_type)
254 {
255 this->notify_message_type = notify_message_type;
256 }
257
258 /**
259 * Implementation of notify_payload_t.get_spi.
260 */
261 static chunk_t get_spi(private_notify_payload_t *this)
262 {
263 return (this->spi);
264 }
265
266 /**
267 * Implementation of notify_payload_t.set_spi.
268 */
269 static void set_spi(private_notify_payload_t *this, chunk_t spi)
270 {
271 /* destroy existing data first */
272 if (this->spi.ptr != NULL)
273 {
274 /* free existing value */
275 allocator_free(this->spi.ptr);
276 this->spi.ptr = NULL;
277 this->spi.len = 0;
278
279 }
280
281 this->spi.ptr = allocator_clone_bytes(spi.ptr,spi.len);
282
283 this->spi.len = spi.len;
284 this->spi_size = spi.len;
285 this->compute_length(this);
286
287 }
288
289 /**
290 * Implementation of notify_payload_t.get_notification_data.
291 */
292 static chunk_t get_notification_data(private_notify_payload_t *this)
293 {
294 return (this->notification_data);
295 }
296
297 /**
298 * Implementation of notify_payload_t.set_notification_data.
299 */
300 static status_t set_notification_data(private_notify_payload_t *this, chunk_t notification_data)
301 {
302 /* destroy existing data first */
303 if (this->notification_data.ptr != NULL)
304 {
305 /* free existing value */
306 allocator_free(this->notification_data.ptr);
307 this->notification_data.ptr = NULL;
308 this->notification_data.len = 0;
309
310 }
311
312 this->notification_data.ptr = allocator_clone_bytes(notification_data.ptr,notification_data.len);
313 this->notification_data.len = notification_data.len;
314 this->compute_length(this);
315
316 return SUCCESS;
317 }
318
319 /**
320 * Implementation of notify_payload_t.destroy and notify_payload_t.destroy.
321 */
322 static status_t destroy(private_notify_payload_t *this)
323 {
324 if (this->notification_data.ptr != NULL)
325 {
326 allocator_free(this->notification_data.ptr);
327 }
328 if (this->spi.ptr != NULL)
329 {
330 allocator_free(this->spi.ptr);
331 }
332
333 allocator_free(this);
334 return SUCCESS;
335 }
336
337 /*
338 * Described in header
339 */
340 notify_payload_t *notify_payload_create()
341 {
342 private_notify_payload_t *this = allocator_alloc_thing(private_notify_payload_t);
343
344 /* interface functions */
345 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
346 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
347 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
348 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
349 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
350 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
351 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
352
353 /* public functions */
354 this->public.get_protocol_id = (u_int8_t (*) (notify_payload_t *)) get_protocol_id;
355 this->public.set_protocol_id = (void (*) (notify_payload_t *,u_int8_t)) set_protocol_id;
356 this->public.get_notify_message_type = (u_int16_t (*) (notify_payload_t *)) get_notify_message_type;
357 this->public.set_notify_message_type = (void (*) (notify_payload_t *,u_int16_t)) set_notify_message_type;
358 this->public.get_spi = (chunk_t (*) (notify_payload_t *)) get_spi;
359 this->public.set_spi = (void (*) (notify_payload_t *,chunk_t)) set_spi;
360 this->public.get_notification_data = (chunk_t (*) (notify_payload_t *)) get_notification_data;
361 this->public.set_notification_data = (void (*) (notify_payload_t *,chunk_t)) set_notification_data;
362 this->public.destroy = (void (*) (notify_payload_t *)) destroy;
363
364 /* private functions */
365 this->compute_length = compute_length;
366
367 /* set default values of the fields */
368 this->critical = NOTIFY_PAYLOAD_CRITICAL_FLAG;
369 this->next_payload = NO_PAYLOAD;
370 this->payload_length = NOTIFY_PAYLOAD_HEADER_LENGTH;
371 this->protocol_id = 0;
372 this->notify_message_type = 0;
373 this->spi.ptr = NULL;
374 this->spi.len = 0;
375 this->notification_data.ptr = NULL;
376 this->notification_data.len = 0;
377
378 return (&(this->public));
379 }
380