cfbd536c4b03196ce179a07f0c00c47abb48e83b
[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 * Implements payload_t's and ke_payload_t's destroy function.
116 * See #payload_s.destroy or ke_payload_s.destroy for description.
117 */
118 static status_t destroy(private_ke_payload_t *this)
119 {
120 if (this->key_exchange_data.ptr != NULL)
121 {
122 allocator_free(this->key_exchange_data.ptr);
123 }
124 allocator_free(this);
125 return SUCCESS;
126 }
127
128 /**
129 * Implements payload_t's get_encoding_rules function.
130 * See #payload_s.get_encoding_rules for description.
131 */
132 static status_t get_encoding_rules(private_ke_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
133 {
134 *rules = ke_payload_encodings;
135 *rule_count = sizeof(ke_payload_encodings) / sizeof(encoding_rule_t);
136
137 return SUCCESS;
138 }
139
140 /**
141 * Implements payload_t's get_type function.
142 * See #payload_s.get_type for description.
143 */
144 static payload_type_t get_type(private_ke_payload_t *this)
145 {
146 return KEY_EXCHANGE;
147 }
148
149 /**
150 * Implements payload_t's get_next_type function.
151 * See #payload_s.get_next_type for description.
152 */
153 static payload_type_t get_next_type(private_ke_payload_t *this)
154 {
155 return (this->next_payload);
156 }
157
158 /**
159 * Implements payload_t's set_next_type function.
160 * See #payload_s.set_next_type for description.
161 */
162 static status_t set_next_type(private_ke_payload_t *this,payload_type_t type)
163 {
164 this->next_payload = type;
165 return SUCCESS;
166 }
167
168 /**
169 * Implements payload_t's get_length function.
170 * See #payload_s.get_length for description.
171 */
172 static size_t get_length(private_ke_payload_t *this)
173 {
174 this->compute_length(this);
175 return this->payload_length;
176 }
177
178 /**
179 * Implements private_ke_payload_t's compute_length function.
180 * See #private_ke_payload_s.compute_length for description.
181 */
182 static status_t compute_length (private_ke_payload_t *this)
183 {
184 size_t length = KE_PAYLOAD_HEADER_LENGTH;
185 if (this->key_exchange_data.ptr != NULL)
186 {
187 length += this->key_exchange_data.len;
188 }
189
190 this->payload_length = length;
191
192 return SUCCESS;
193 }
194
195
196 /**
197 * Implements ke_payload_t's get_key_exchange_data function.
198 * See #ke_payload_t.get_key_exchange_data for description.
199 */
200 chunk_t get_key_exchange_data(private_ke_payload_t *this)
201 {
202 return (this->key_exchange_data);
203 }
204
205 /**
206 * Implements ke_payload_t's set_key_exchange_data function.
207 * See #ke_payload_t.set_key_exchange_data for description.
208 */
209 status_t set_key_exchange_data(private_ke_payload_t *this, chunk_t key_exchange_data)
210 {
211 /* destroy existing data first */
212 if (this->key_exchange_data.ptr != NULL)
213 {
214 /* free existing value */
215 allocator_free(this->key_exchange_data.ptr);
216 this->key_exchange_data.ptr = NULL;
217 this->key_exchange_data.len = 0;
218
219 }
220
221 this->key_exchange_data.ptr = allocator_clone_bytes(key_exchange_data.ptr,key_exchange_data.len);
222 if (this->key_exchange_data.ptr == NULL)
223 {
224 return OUT_OF_RES;
225 }
226 this->key_exchange_data.len = key_exchange_data.len;
227 this->compute_length(this);
228
229 return SUCCESS;
230 }
231
232 /**
233 * Implements ke_payload_t's get_dh_group_number function.
234 * See #ke_payload_t.get_dh_group_number for description.
235 */
236 u_int16_t get_dh_group_number(private_ke_payload_t *this)
237 {
238 return this->dh_group_number;
239 }
240
241 /**
242 * Implements ke_payload_t's set_dh_group_number function.
243 * See #ke_payload_t.set_dh_group_number for description.
244 */
245 status_t set_dh_group_number(private_ke_payload_t *this, u_int16_t dh_group_number)
246 {
247 this->dh_group_number = dh_group_number;
248 return SUCCESS;
249 }
250
251 /*
252 * Described in header
253 */
254 ke_payload_t *ke_payload_create()
255 {
256 private_ke_payload_t *this = allocator_alloc_thing(private_ke_payload_t);
257 if (this == NULL)
258 {
259 return NULL;
260 }
261 /* interface functions */
262 this->public.payload_interface.get_encoding_rules = (status_t (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
263 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
264 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
265 this->public.payload_interface.set_next_type = (status_t (*) (payload_t *,payload_type_t)) set_next_type;
266 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
267 this->public.payload_interface.destroy = (status_t (*) (payload_t *))destroy;
268
269 /* public functions */
270 this->public.get_key_exchange_data = (chunk_t (*) (ke_payload_t *)) get_key_exchange_data;
271 this->public.set_key_exchange_data = (status_t (*) (ke_payload_t *,chunk_t)) set_key_exchange_data;
272 this->public.get_dh_group_number = (u_int16_t (*) (ke_payload_t *)) get_dh_group_number;
273 this->public.set_dh_group_number =(status_t (*) (ke_payload_t *,u_int16_t)) set_dh_group_number;
274 this->public.destroy = (status_t (*) (ke_payload_t *)) destroy;
275
276 /* private functions */
277 this->compute_length = compute_length;
278
279 /* set default values of the fields */
280 this->critical = KE_PAYLOAD_CRITICAL_FLAG;
281 this->next_payload = NO_PAYLOAD;
282 this->payload_length = KE_PAYLOAD_HEADER_LENGTH;
283 this->key_exchange_data.ptr = NULL;
284 this->key_exchange_data.len = 0;
285 this->dh_group_number = 0;
286
287 return (&(this->public));
288 }