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 <encoding/payloads/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 diffie_hellman_group_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
) }
116 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
117 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
118 ! Next Payload !C! RESERVED ! Payload Length !
119 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
120 ! DH Group # ! RESERVED !
121 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
123 ~ Key Exchange Data ~
125 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
129 * Implements payload_t's verify function.
130 * See #payload_s.verify for description.
132 static status_t
verify(private_ke_payload_t
*this)
136 /* critical bit is set! */
140 /* dh group is not verified in here */
145 * Implements payload_t's and ke_payload_t's destroy function.
146 * See #payload_s.destroy or ke_payload_s.destroy for description.
148 static status_t
destroy(private_ke_payload_t
*this)
150 if (this->key_exchange_data
.ptr
!= NULL
)
152 allocator_free(this->key_exchange_data
.ptr
);
154 allocator_free(this);
159 * Implements payload_t's get_encoding_rules function.
160 * See #payload_s.get_encoding_rules for description.
162 static status_t
get_encoding_rules(private_ke_payload_t
*this, encoding_rule_t
**rules
, size_t *rule_count
)
164 *rules
= ke_payload_encodings
;
165 *rule_count
= sizeof(ke_payload_encodings
) / sizeof(encoding_rule_t
);
171 * Implements payload_t's get_type function.
172 * See #payload_s.get_type for description.
174 static payload_type_t
get_type(private_ke_payload_t
*this)
180 * Implements payload_t's get_next_type function.
181 * See #payload_s.get_next_type for description.
183 static payload_type_t
get_next_type(private_ke_payload_t
*this)
185 return (this->next_payload
);
189 * Implements payload_t's set_next_type function.
190 * See #payload_s.set_next_type for description.
192 static status_t
set_next_type(private_ke_payload_t
*this,payload_type_t type
)
194 this->next_payload
= type
;
199 * Implements payload_t's get_length function.
200 * See #payload_s.get_length for description.
202 static size_t get_length(private_ke_payload_t
*this)
204 this->compute_length(this);
205 return this->payload_length
;
209 * Implements private_ke_payload_t's compute_length function.
210 * See #private_ke_payload_s.compute_length for description.
212 static status_t
compute_length (private_ke_payload_t
*this)
214 size_t length
= KE_PAYLOAD_HEADER_LENGTH
;
215 if (this->key_exchange_data
.ptr
!= NULL
)
217 length
+= this->key_exchange_data
.len
;
220 this->payload_length
= length
;
227 * Implements ke_payload_t's get_key_exchange_data function.
228 * See #ke_payload_t.get_key_exchange_data for description.
230 chunk_t
get_key_exchange_data(private_ke_payload_t
*this)
232 return (this->key_exchange_data
);
236 * Implements ke_payload_t's set_key_exchange_data function.
237 * See #ke_payload_t.set_key_exchange_data for description.
239 status_t
set_key_exchange_data(private_ke_payload_t
*this, chunk_t key_exchange_data
)
241 /* destroy existing data first */
242 if (this->key_exchange_data
.ptr
!= NULL
)
244 /* free existing value */
245 allocator_free(this->key_exchange_data
.ptr
);
246 this->key_exchange_data
.ptr
= NULL
;
247 this->key_exchange_data
.len
= 0;
251 this->key_exchange_data
.ptr
= allocator_clone_bytes(key_exchange_data
.ptr
,key_exchange_data
.len
);
252 if (this->key_exchange_data
.ptr
== NULL
)
256 this->key_exchange_data
.len
= key_exchange_data
.len
;
257 this->compute_length(this);
263 * Implements ke_payload_t's get_dh_group_number function.
264 * See #ke_payload_t.get_dh_group_number for description.
266 diffie_hellman_group_t
get_dh_group_number(private_ke_payload_t
*this)
268 return this->dh_group_number
;
272 * Implements ke_payload_t's set_dh_group_number function.
273 * See #ke_payload_t.set_dh_group_number for description.
275 status_t
set_dh_group_number(private_ke_payload_t
*this, diffie_hellman_group_t dh_group_number
)
277 this->dh_group_number
= dh_group_number
;
282 * Described in header
284 ke_payload_t
*ke_payload_create()
286 private_ke_payload_t
*this = allocator_alloc_thing(private_ke_payload_t
);
291 /* interface functions */
292 this->public.payload_interface
.verify
= (status_t (*) (payload_t
*))verify
;
293 this->public.payload_interface
.get_encoding_rules
= (status_t (*) (payload_t
*, encoding_rule_t
**, size_t *) ) get_encoding_rules
;
294 this->public.payload_interface
.get_length
= (size_t (*) (payload_t
*)) get_length
;
295 this->public.payload_interface
.get_next_type
= (payload_type_t (*) (payload_t
*)) get_next_type
;
296 this->public.payload_interface
.set_next_type
= (status_t (*) (payload_t
*,payload_type_t
)) set_next_type
;
297 this->public.payload_interface
.get_type
= (payload_type_t (*) (payload_t
*)) get_type
;
298 this->public.payload_interface
.destroy
= (status_t (*) (payload_t
*))destroy
;
300 /* public functions */
301 this->public.get_key_exchange_data
= (chunk_t (*) (ke_payload_t
*)) get_key_exchange_data
;
302 this->public.set_key_exchange_data
= (status_t (*) (ke_payload_t
*,chunk_t
)) set_key_exchange_data
;
303 this->public.get_dh_group_number
= (diffie_hellman_group_t (*) (ke_payload_t
*)) get_dh_group_number
;
304 this->public.set_dh_group_number
=(status_t (*) (ke_payload_t
*,diffie_hellman_group_t
)) set_dh_group_number
;
305 this->public.destroy
= (status_t (*) (ke_payload_t
*)) destroy
;
307 /* private functions */
308 this->compute_length
= compute_length
;
310 /* set default values of the fields */
311 this->critical
= KE_PAYLOAD_CRITICAL_FLAG
;
312 this->next_payload
= NO_PAYLOAD
;
313 this->payload_length
= KE_PAYLOAD_HEADER_LENGTH
;
314 this->key_exchange_data
.ptr
= NULL
;
315 this->key_exchange_data
.len
= 0;
316 this->dh_group_number
= 0;
318 return (&(this->public));