]> git.ipfire.org Git - people/ms/strongswan.git/blob - Source/charon/encoding/payloads/ke_payload.c
- renamed get_block_size of hasher
[people/ms/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 #include <stddef.h>
24
25 #include "ke_payload.h"
26
27 #include <encoding/payloads/encodings.h>
28
29
30 typedef struct private_ke_payload_t private_ke_payload_t;
31
32 /**
33 * Private data of an ke_payload_t object.
34 *
35 */
36 struct private_ke_payload_t {
37 /**
38 * Public ke_payload_t interface.
39 */
40 ke_payload_t public;
41
42 /**
43 * Next payload type.
44 */
45 u_int8_t next_payload;
46
47 /**
48 * Critical flag.
49 */
50 bool critical;
51
52 /**
53 * Length of this payload.
54 */
55 u_int16_t payload_length;
56
57 /**
58 * DH Group Number.
59 */
60 diffie_hellman_group_t dh_group_number;
61
62 /**
63 * Key Exchange Data of this KE payload.
64 */
65 chunk_t key_exchange_data;
66
67 /**
68 * @brief Computes the length of this payload.
69 *
70 * @param this calling private_ke_payload_t object
71 */
72 void (*compute_length) (private_ke_payload_t *this);
73 };
74
75 /**
76 * Encoding rules to parse or generate a IKEv2-KE Payload.
77 *
78 * The defined offsets are the positions in a object of type
79 * private_ke_payload_t.
80 *
81 */
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 */
88 { RESERVED_BIT, 0 },
89 { RESERVED_BIT, 0 },
90 { RESERVED_BIT, 0 },
91 { RESERVED_BIT, 0 },
92 { RESERVED_BIT, 0 },
93 { RESERVED_BIT, 0 },
94 { RESERVED_BIT, 0 },
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) },
99 { RESERVED_BYTE, 0 },
100 { RESERVED_BYTE, 0 },
101 /* Key Exchange Data is from variable size */
102 { KEY_EXCHANGE_DATA, offsetof(private_ke_payload_t, key_exchange_data)}
103 };
104
105 /*
106 1 2 3
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 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
113 ! !
114 ~ Key Exchange Data ~
115 ! !
116 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
117 */
118
119 /**
120 * Implementation of payload_t.verify.
121 */
122 static status_t verify(private_ke_payload_t *this)
123 {
124 /* dh group is not verified in here */
125 return SUCCESS;
126 }
127
128 /**
129 * Implementation of payload_t.destroy.
130 */
131 static void destroy(private_ke_payload_t *this)
132 {
133 if (this->key_exchange_data.ptr != NULL)
134 {
135 free(this->key_exchange_data.ptr);
136 }
137 free(this);
138 }
139
140 /**
141 * Implementation of payload_t.get_encoding_rules.
142 */
143 static void get_encoding_rules(private_ke_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
144 {
145 *rules = ke_payload_encodings;
146 *rule_count = sizeof(ke_payload_encodings) / sizeof(encoding_rule_t);
147 }
148
149 /**
150 * Implementation of payload_t.get_type.
151 */
152 static payload_type_t get_type(private_ke_payload_t *this)
153 {
154 return KEY_EXCHANGE;
155 }
156
157 /**
158 * Implementation of payload_t.get_next_type.
159 */
160 static payload_type_t get_next_type(private_ke_payload_t *this)
161 {
162 return (this->next_payload);
163 }
164
165 /**
166 * Implementation of payload_t.set_next_type.
167 */
168 static void set_next_type(private_ke_payload_t *this,payload_type_t type)
169 {
170 this->next_payload = type;
171 }
172
173 /**
174 * Implementation of payload_t.get_length.
175 */
176 static size_t get_length(private_ke_payload_t *this)
177 {
178 this->compute_length(this);
179 return this->payload_length;
180 }
181
182 /**
183 * Implementation of private_ke_payload_t.compute_length.
184 */
185 static void compute_length (private_ke_payload_t *this)
186 {
187 size_t length = KE_PAYLOAD_HEADER_LENGTH;
188 if (this->key_exchange_data.ptr != NULL)
189 {
190 length += this->key_exchange_data.len;
191 }
192 this->payload_length = length;
193 }
194
195
196 /**
197 * Implementation of ke_payload_t.get_key_exchange_data.
198 */
199 static chunk_t get_key_exchange_data(private_ke_payload_t *this)
200 {
201 return (this->key_exchange_data);
202 }
203
204 /**
205 * Implementation of ke_payload_t.set_key_exchange_data.
206 */
207 static void set_key_exchange_data(private_ke_payload_t *this, chunk_t key_exchange_data)
208 {
209 /* destroy existing data first */
210 if (this->key_exchange_data.ptr != NULL)
211 {
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;
216
217 }
218
219 this->key_exchange_data.ptr = clalloc(key_exchange_data.ptr,key_exchange_data.len);
220
221 this->key_exchange_data.len = key_exchange_data.len;
222 this->compute_length(this);
223 }
224
225 /**
226 * Implementation of ke_payload_t.get_dh_group_number.
227 */
228 static diffie_hellman_group_t get_dh_group_number(private_ke_payload_t *this)
229 {
230 return this->dh_group_number;
231 }
232
233 /**
234 * Implementation of ke_payload_t.set_dh_group_number.
235 */
236 static void set_dh_group_number(private_ke_payload_t *this, diffie_hellman_group_t dh_group_number)
237 {
238 this->dh_group_number = dh_group_number;
239 }
240
241 /*
242 * Described in header
243 */
244 ke_payload_t *ke_payload_create()
245 {
246 private_ke_payload_t *this = malloc_thing(private_ke_payload_t);
247
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;
256
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;
263
264 /* private functions */
265 this->compute_length = compute_length;
266
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;
274
275 return (&(this->public));
276 }