responder_init does return DELETE_ME in case of error
[strongswan.git] / Source / charon / encoding / payloads / ke_payload.c
1 /**
2 * @file ke_payload.c
3 *
4 * @brief Implementation of ke_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 "ke_payload.h"
27
28 #include <encoding/payloads/encodings.h>
29 #include <utils/allocator.h>
30
31
32 typedef struct private_ke_payload_t private_ke_payload_t;
33
34 /**
35 * Private data of an ke_payload_t object.
36 *
37 */
38 struct private_ke_payload_t {
39 /**
40 * Public ke_payload_t interface.
41 */
42 ke_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 /**
61 * DH Group Number.
62 */
63 diffie_hellman_group_t dh_group_number;
64
65 /**
66 * Key Exchange Data of this KE payload.
67 */
68 chunk_t key_exchange_data;
69
70 /**
71 * @brief Computes the length of this payload.
72 *
73 * @param this calling private_ke_payload_t object
74 */
75 void (*compute_length) (private_ke_payload_t *this);
76 };
77
78 /**
79 * Encoding rules to parse or generate a IKEv2-KE Payload.
80 *
81 * The defined offsets are the positions in a object of type
82 * private_ke_payload_t.
83 *
84 */
85 encoding_rule_t ke_payload_encodings[] = {
86 /* 1 Byte next payload type, stored in the field next_payload */
87 { U_INT_8, offsetof(private_ke_payload_t, next_payload) },
88 /* the critical bit */
89 { FLAG, offsetof(private_ke_payload_t, critical) },
90 /* 7 Bit reserved bits, nowhere stored */
91 { RESERVED_BIT, 0 },
92 { RESERVED_BIT, 0 },
93 { RESERVED_BIT, 0 },
94 { RESERVED_BIT, 0 },
95 { RESERVED_BIT, 0 },
96 { RESERVED_BIT, 0 },
97 { RESERVED_BIT, 0 },
98 /* Length of the whole payload*/
99 { PAYLOAD_LENGTH, offsetof(private_ke_payload_t, payload_length) },
100 /* DH Group number as 16 bit field*/
101 { U_INT_16, offsetof(private_ke_payload_t, dh_group_number) },
102 { RESERVED_BYTE, 0 },
103 { RESERVED_BYTE, 0 },
104 /* Key Exchange Data is from variable size */
105 { KEY_EXCHANGE_DATA, offsetof(private_ke_payload_t, key_exchange_data)}
106 };
107
108 /*
109 1 2 3
110 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
111 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112 ! Next Payload !C! RESERVED ! Payload Length !
113 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114 ! DH Group # ! RESERVED !
115 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116 ! !
117 ~ Key Exchange Data ~
118 ! !
119 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
120 */
121
122 /**
123 * Implementation of payload_t.verify.
124 */
125 static status_t verify(private_ke_payload_t *this)
126 {
127 if (this->critical)
128 {
129 /* critical bit is set! */
130 return FAILED;
131 }
132
133 /* dh group is not verified in here */
134 return SUCCESS;
135 }
136
137 /**
138 * Implementation of payload_t.destroy.
139 */
140 static void destroy(private_ke_payload_t *this)
141 {
142 if (this->key_exchange_data.ptr != NULL)
143 {
144 allocator_free(this->key_exchange_data.ptr);
145 }
146 allocator_free(this);
147 }
148
149 /**
150 * Implementation of payload_t.get_encoding_rules.
151 */
152 static void get_encoding_rules(private_ke_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
153 {
154 *rules = ke_payload_encodings;
155 *rule_count = sizeof(ke_payload_encodings) / sizeof(encoding_rule_t);
156 }
157
158 /**
159 * Implementation of payload_t.get_type.
160 */
161 static payload_type_t get_type(private_ke_payload_t *this)
162 {
163 return KEY_EXCHANGE;
164 }
165
166 /**
167 * Implementation of payload_t.get_next_type.
168 */
169 static payload_type_t get_next_type(private_ke_payload_t *this)
170 {
171 return (this->next_payload);
172 }
173
174 /**
175 * Implementation of payload_t.set_next_type.
176 */
177 static void set_next_type(private_ke_payload_t *this,payload_type_t type)
178 {
179 this->next_payload = type;
180 }
181
182 /**
183 * Implementation of payload_t.get_length.
184 */
185 static size_t get_length(private_ke_payload_t *this)
186 {
187 this->compute_length(this);
188 return this->payload_length;
189 }
190
191 /**
192 * Implementation of private_ke_payload_t.compute_length.
193 */
194 static void compute_length (private_ke_payload_t *this)
195 {
196 size_t length = KE_PAYLOAD_HEADER_LENGTH;
197 if (this->key_exchange_data.ptr != NULL)
198 {
199 length += this->key_exchange_data.len;
200 }
201 this->payload_length = length;
202 }
203
204
205 /**
206 * Implementation of ke_payload_t.get_key_exchange_data.
207 */
208 static chunk_t get_key_exchange_data(private_ke_payload_t *this)
209 {
210 return (this->key_exchange_data);
211 }
212
213 /**
214 * Implementation of ke_payload_t.set_key_exchange_data.
215 */
216 static void set_key_exchange_data(private_ke_payload_t *this, chunk_t key_exchange_data)
217 {
218 /* destroy existing data first */
219 if (this->key_exchange_data.ptr != NULL)
220 {
221 /* free existing value */
222 allocator_free(this->key_exchange_data.ptr);
223 this->key_exchange_data.ptr = NULL;
224 this->key_exchange_data.len = 0;
225
226 }
227
228 this->key_exchange_data.ptr = allocator_clone_bytes(key_exchange_data.ptr,key_exchange_data.len);
229
230 this->key_exchange_data.len = key_exchange_data.len;
231 this->compute_length(this);
232 }
233
234 /**
235 * Implementation of ke_payload_t.get_dh_group_number.
236 */
237 static diffie_hellman_group_t get_dh_group_number(private_ke_payload_t *this)
238 {
239 return this->dh_group_number;
240 }
241
242 /**
243 * Implementation of ke_payload_t.set_dh_group_number.
244 */
245 static void set_dh_group_number(private_ke_payload_t *this, diffie_hellman_group_t dh_group_number)
246 {
247 this->dh_group_number = dh_group_number;
248 }
249
250 /*
251 * Described in header
252 */
253 ke_payload_t *ke_payload_create()
254 {
255 private_ke_payload_t *this = allocator_alloc_thing(private_ke_payload_t);
256
257 /* interface functions */
258 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
259 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
260 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
261 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
262 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
263 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
264 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
265
266 /* public functions */
267 this->public.get_key_exchange_data = (chunk_t (*) (ke_payload_t *)) get_key_exchange_data;
268 this->public.set_key_exchange_data = (void (*) (ke_payload_t *,chunk_t)) set_key_exchange_data;
269 this->public.get_dh_group_number = (diffie_hellman_group_t (*) (ke_payload_t *)) get_dh_group_number;
270 this->public.set_dh_group_number =(void (*) (ke_payload_t *,diffie_hellman_group_t)) set_dh_group_number;
271 this->public.destroy = (void (*) (ke_payload_t *)) destroy;
272
273 /* private functions */
274 this->compute_length = compute_length;
275
276 /* set default values of the fields */
277 this->critical = KE_PAYLOAD_CRITICAL_FLAG;
278 this->next_payload = NO_PAYLOAD;
279 this->payload_length = KE_PAYLOAD_HEADER_LENGTH;
280 this->key_exchange_data.ptr = NULL;
281 this->key_exchange_data.len = 0;
282 this->dh_group_number = 0;
283
284 return (&(this->public));
285 }