4 * @brief Declaration of the class ke_payload_t.
6 * An object of this type represents an IKEv2 KE-Payload.
8 * See section 3.4 of RFC for details of this payload type.
13 * Copyright (C) 2005 Jan Hutter, Martin Willi
14 * Hochschule fuer Technik Rapperswil
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>.
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
30 #include "ke_payload.h"
32 #include "encodings.h"
33 #include "../utils/allocator.h"
37 * Private data of an ke_payload_t Object
40 typedef struct private_ke_payload_s private_ke_payload_t
;
42 struct private_ke_payload_s
{
44 * public ke_payload_t interface
51 u_int8_t next_payload
;
59 * Length of this payload
61 u_int16_t payload_length
;
67 u_int16_t dh_group_number
;
70 * Key Exchange Data of this KE payload
72 chunk_t key_exchange_data
;
75 * @brief Computes the length of this payload.
77 * @param this calling private_ke_payload_t object
81 status_t (*compute_length
) (private_ke_payload_t
*this);
85 * Encoding rules to parse or generate a IKEv2-KE Payload
87 * The defined offsets are the positions in a object of type
88 * private_ke_payload_t.
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 */
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
) }
115 * Implements payload_t's and ke_payload_t's destroy function.
116 * See #payload_s.destroy or ke_payload_s.destroy for description.
118 static status_t
destroy(private_ke_payload_t
*this)
120 if (this->key_exchange_data
.ptr
!= NULL
)
122 allocator_free(this->key_exchange_data
.ptr
);
124 allocator_free(this);
129 * Implements payload_t's get_encoding_rules function.
130 * See #payload_s.get_encoding_rules for description.
132 static status_t
get_encoding_rules(private_ke_payload_t
*this, encoding_rule_t
**rules
, size_t *rule_count
)
134 *rules
= ke_payload_encodings
;
135 *rule_count
= sizeof(ke_payload_encodings
) / sizeof(encoding_rule_t
);
141 * Implements payload_t's get_type function.
142 * See #payload_s.get_type for description.
144 static payload_type_t
get_type(private_ke_payload_t
*this)
150 * Implements payload_t's get_next_type function.
151 * See #payload_s.get_next_type for description.
153 static payload_type_t
get_next_type(private_ke_payload_t
*this)
155 return (this->next_payload
);
159 * Implements payload_t's set_next_type function.
160 * See #payload_s.set_next_type for description.
162 static status_t
set_next_type(private_ke_payload_t
*this,payload_type_t type
)
164 this->next_payload
= type
;
169 * Implements payload_t's get_length function.
170 * See #payload_s.get_length for description.
172 static size_t get_length(private_ke_payload_t
*this)
174 this->compute_length(this);
175 return this->payload_length
;
179 * Implements private_ke_payload_t's compute_length function.
180 * See #private_ke_payload_s.compute_length for description.
182 static status_t
compute_length (private_ke_payload_t
*this)
184 size_t length
= KE_PAYLOAD_HEADER_LENGTH
;
185 if (this->key_exchange_data
.ptr
!= NULL
)
187 length
+= this->key_exchange_data
.len
;
190 this->payload_length
= length
;
197 * Implements ke_payload_t's get_key_exchange_data function.
198 * See #ke_payload_t.get_key_exchange_data for description.
200 chunk_t
get_key_exchange_data(private_ke_payload_t
*this)
202 return (this->key_exchange_data
);
206 * Implements ke_payload_t's set_key_exchange_data function.
207 * See #ke_payload_t.set_key_exchange_data for description.
209 status_t
set_key_exchange_data(private_ke_payload_t
*this, chunk_t key_exchange_data
)
211 /* destroy existing data first */
212 if (this->key_exchange_data
.ptr
!= NULL
)
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;
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
)
226 this->key_exchange_data
.len
= key_exchange_data
.len
;
227 this->compute_length(this);
233 * Implements ke_payload_t's get_dh_group_number function.
234 * See #ke_payload_t.get_dh_group_number for description.
236 u_int16_t
get_dh_group_number(private_ke_payload_t
*this)
238 return this->dh_group_number
;
242 * Implements ke_payload_t's set_dh_group_number function.
243 * See #ke_payload_t.set_dh_group_number for description.
245 status_t
set_dh_group_number(private_ke_payload_t
*this, u_int16_t dh_group_number
)
247 this->dh_group_number
= dh_group_number
;
252 * Described in header
254 ke_payload_t
*ke_payload_create()
256 private_ke_payload_t
*this = allocator_alloc_thing(private_ke_payload_t
);
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
;
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
;
276 /* private functions */
277 this->compute_length
= compute_length
;
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;
287 return (&(this->public));