4 * @brief Implementation of ke_payload_t.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
25 #include "ke_payload.h"
27 #include <encoding/payloads/encodings.h>
30 typedef struct private_ke_payload_t private_ke_payload_t
;
33 * Private data of an ke_payload_t object.
36 struct private_ke_payload_t
{
38 * Public ke_payload_t interface.
45 u_int8_t next_payload
;
53 * Length of this payload.
55 u_int16_t payload_length
;
60 diffie_hellman_group_t dh_group_number
;
63 * Key Exchange Data of this KE payload.
65 chunk_t key_exchange_data
;
68 * @brief Computes the length of this payload.
70 * @param this calling private_ke_payload_t object
72 void (*compute_length
) (private_ke_payload_t
*this);
76 * Encoding rules to parse or generate a IKEv2-KE Payload.
78 * The defined offsets are the positions in a object of type
79 * private_ke_payload_t.
82 encoding_rule_t ke_payload_encodings
[] = {
83 /* 1 Byte next payload type, stored in the field next_payload */
84 { U_INT_8
, offsetof(private_ke_payload_t
, next_payload
) },
85 /* the critical bit */
86 { FLAG
, offsetof(private_ke_payload_t
, critical
) },
87 /* 7 Bit reserved bits, nowhere stored */
95 /* Length of the whole payload*/
96 { PAYLOAD_LENGTH
, offsetof(private_ke_payload_t
, payload_length
) },
97 /* DH Group number as 16 bit field*/
98 { U_INT_16
, offsetof(private_ke_payload_t
, dh_group_number
) },
100 { RESERVED_BYTE
, 0 },
101 /* Key Exchange Data is from variable size */
102 { KEY_EXCHANGE_DATA
, offsetof(private_ke_payload_t
, key_exchange_data
)}
107 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
108 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
109 ! Next Payload !C! RESERVED ! Payload Length !
110 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111 ! DH Group # ! RESERVED !
112 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
114 ~ Key Exchange Data ~
116 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
120 * Implementation of payload_t.verify.
122 static status_t
verify(private_ke_payload_t
*this)
124 /* dh group is not verified in here */
129 * Implementation of payload_t.destroy.
131 static void destroy(private_ke_payload_t
*this)
133 if (this->key_exchange_data
.ptr
!= NULL
)
135 free(this->key_exchange_data
.ptr
);
141 * Implementation of payload_t.get_encoding_rules.
143 static void get_encoding_rules(private_ke_payload_t
*this, encoding_rule_t
**rules
, size_t *rule_count
)
145 *rules
= ke_payload_encodings
;
146 *rule_count
= sizeof(ke_payload_encodings
) / sizeof(encoding_rule_t
);
150 * Implementation of payload_t.get_type.
152 static payload_type_t
get_type(private_ke_payload_t
*this)
158 * Implementation of payload_t.get_next_type.
160 static payload_type_t
get_next_type(private_ke_payload_t
*this)
162 return (this->next_payload
);
166 * Implementation of payload_t.set_next_type.
168 static void set_next_type(private_ke_payload_t
*this,payload_type_t type
)
170 this->next_payload
= type
;
174 * Implementation of payload_t.get_length.
176 static size_t get_length(private_ke_payload_t
*this)
178 this->compute_length(this);
179 return this->payload_length
;
183 * Implementation of private_ke_payload_t.compute_length.
185 static void compute_length (private_ke_payload_t
*this)
187 size_t length
= KE_PAYLOAD_HEADER_LENGTH
;
188 if (this->key_exchange_data
.ptr
!= NULL
)
190 length
+= this->key_exchange_data
.len
;
192 this->payload_length
= length
;
197 * Implementation of ke_payload_t.get_key_exchange_data.
199 static chunk_t
get_key_exchange_data(private_ke_payload_t
*this)
201 return (this->key_exchange_data
);
205 * Implementation of ke_payload_t.set_key_exchange_data.
207 static void set_key_exchange_data(private_ke_payload_t
*this, chunk_t key_exchange_data
)
209 /* destroy existing data first */
210 if (this->key_exchange_data
.ptr
!= NULL
)
212 /* free existing value */
213 free(this->key_exchange_data
.ptr
);
214 this->key_exchange_data
.ptr
= NULL
;
215 this->key_exchange_data
.len
= 0;
219 this->key_exchange_data
.ptr
= clalloc(key_exchange_data
.ptr
,key_exchange_data
.len
);
221 this->key_exchange_data
.len
= key_exchange_data
.len
;
222 this->compute_length(this);
226 * Implementation of ke_payload_t.get_dh_group_number.
228 static diffie_hellman_group_t
get_dh_group_number(private_ke_payload_t
*this)
230 return this->dh_group_number
;
234 * Implementation of ke_payload_t.set_dh_group_number.
236 static void set_dh_group_number(private_ke_payload_t
*this, diffie_hellman_group_t dh_group_number
)
238 this->dh_group_number
= dh_group_number
;
242 * Described in header
244 ke_payload_t
*ke_payload_create()
246 private_ke_payload_t
*this = malloc_thing(private_ke_payload_t
);
248 /* interface functions */
249 this->public.payload_interface
.verify
= (status_t (*) (payload_t
*))verify
;
250 this->public.payload_interface
.get_encoding_rules
= (void (*) (payload_t
*, encoding_rule_t
**, size_t *) ) get_encoding_rules
;
251 this->public.payload_interface
.get_length
= (size_t (*) (payload_t
*)) get_length
;
252 this->public.payload_interface
.get_next_type
= (payload_type_t (*) (payload_t
*)) get_next_type
;
253 this->public.payload_interface
.set_next_type
= (void (*) (payload_t
*,payload_type_t
)) set_next_type
;
254 this->public.payload_interface
.get_type
= (payload_type_t (*) (payload_t
*)) get_type
;
255 this->public.payload_interface
.destroy
= (void (*) (payload_t
*))destroy
;
257 /* public functions */
258 this->public.get_key_exchange_data
= (chunk_t (*) (ke_payload_t
*)) get_key_exchange_data
;
259 this->public.set_key_exchange_data
= (void (*) (ke_payload_t
*,chunk_t
)) set_key_exchange_data
;
260 this->public.get_dh_group_number
= (diffie_hellman_group_t (*) (ke_payload_t
*)) get_dh_group_number
;
261 this->public.set_dh_group_number
=(void (*) (ke_payload_t
*,diffie_hellman_group_t
)) set_dh_group_number
;
262 this->public.destroy
= (void (*) (ke_payload_t
*)) destroy
;
264 /* private functions */
265 this->compute_length
= compute_length
;
267 /* set default values of the fields */
268 this->critical
= FALSE
;
269 this->next_payload
= NO_PAYLOAD
;
270 this->payload_length
= KE_PAYLOAD_HEADER_LENGTH
;
271 this->key_exchange_data
.ptr
= NULL
;
272 this->key_exchange_data
.len
= 0;
273 this->dh_group_number
= 0;
275 return (&(this->public));