fc875517cc370347e4dbd6da49e1f3a8909b0dfb
[strongswan.git] / src / 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-2006 Martin Willi
10 * Copyright (C) 2005 Jan Hutter
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include <stddef.h>
25
26 #include "ke_payload.h"
27
28 #include <encoding/payloads/encodings.h>
29
30
31 typedef struct private_ke_payload_t private_ke_payload_t;
32
33 /**
34 * Private data of an ke_payload_t object.
35 *
36 */
37 struct private_ke_payload_t {
38 /**
39 * Public ke_payload_t interface.
40 */
41 ke_payload_t public;
42
43 /**
44 * Next payload type.
45 */
46 u_int8_t next_payload;
47
48 /**
49 * Critical flag.
50 */
51 bool critical;
52
53 /**
54 * Length of this payload.
55 */
56 u_int16_t payload_length;
57
58 /**
59 * DH Group Number.
60 */
61 diffie_hellman_group_t dh_group_number;
62
63 /**
64 * Key Exchange Data of this KE payload.
65 */
66 chunk_t key_exchange_data;
67 };
68
69 /**
70 * Encoding rules to parse or generate a IKEv2-KE Payload.
71 *
72 * The defined offsets are the positions in a object of type
73 * private_ke_payload_t.
74 *
75 */
76 encoding_rule_t ke_payload_encodings[] = {
77 /* 1 Byte next payload type, stored in the field next_payload */
78 { U_INT_8, offsetof(private_ke_payload_t, next_payload) },
79 /* the critical bit */
80 { FLAG, offsetof(private_ke_payload_t, critical) },
81 /* 7 Bit reserved bits, nowhere stored */
82 { RESERVED_BIT, 0 },
83 { RESERVED_BIT, 0 },
84 { RESERVED_BIT, 0 },
85 { RESERVED_BIT, 0 },
86 { RESERVED_BIT, 0 },
87 { RESERVED_BIT, 0 },
88 { RESERVED_BIT, 0 },
89 /* Length of the whole payload*/
90 { PAYLOAD_LENGTH, offsetof(private_ke_payload_t, payload_length) },
91 /* DH Group number as 16 bit field*/
92 { U_INT_16, offsetof(private_ke_payload_t, dh_group_number) },
93 { RESERVED_BYTE, 0 },
94 { RESERVED_BYTE, 0 },
95 /* Key Exchange Data is from variable size */
96 { KEY_EXCHANGE_DATA, offsetof(private_ke_payload_t, key_exchange_data)}
97 };
98
99 /*
100 1 2 3
101 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
102 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
103 ! Next Payload !C! RESERVED ! Payload Length !
104 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
105 ! DH Group # ! RESERVED !
106 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
107 ! !
108 ~ Key Exchange Data ~
109 ! !
110 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
111 */
112
113 /**
114 * Implementation of payload_t.verify.
115 */
116 static status_t verify(private_ke_payload_t *this)
117 {
118 /* dh group is not verified in here */
119 return SUCCESS;
120 }
121
122 /**
123 * Implementation of payload_t.destroy.
124 */
125 static void destroy(private_ke_payload_t *this)
126 {
127 if (this->key_exchange_data.ptr != NULL)
128 {
129 free(this->key_exchange_data.ptr);
130 }
131 free(this);
132 }
133
134 /**
135 * Implementation of payload_t.get_encoding_rules.
136 */
137 static void get_encoding_rules(private_ke_payload_t *this, encoding_rule_t **rules, size_t *rule_count)
138 {
139 *rules = ke_payload_encodings;
140 *rule_count = sizeof(ke_payload_encodings) / sizeof(encoding_rule_t);
141 }
142
143 /**
144 * Implementation of payload_t.get_type.
145 */
146 static payload_type_t get_type(private_ke_payload_t *this)
147 {
148 return KEY_EXCHANGE;
149 }
150
151 /**
152 * Implementation of payload_t.get_next_type.
153 */
154 static payload_type_t get_next_type(private_ke_payload_t *this)
155 {
156 return (this->next_payload);
157 }
158
159 /**
160 * Implementation of payload_t.set_next_type.
161 */
162 static void set_next_type(private_ke_payload_t *this,payload_type_t type)
163 {
164 this->next_payload = type;
165 }
166
167 /**
168 * recompute the length of the payload.
169 */
170 static void compute_length(private_ke_payload_t *this)
171 {
172 size_t length = KE_PAYLOAD_HEADER_LENGTH;
173 if (this->key_exchange_data.ptr != NULL)
174 {
175 length += this->key_exchange_data.len;
176 }
177 this->payload_length = length;
178 }
179
180 /**
181 * Implementation of payload_t.get_length.
182 */
183 static size_t get_length(private_ke_payload_t *this)
184 {
185 compute_length(this);
186 return this->payload_length;
187 }
188
189 /**
190 * Implementation of ke_payload_t.get_key_exchange_data.
191 */
192 static chunk_t get_key_exchange_data(private_ke_payload_t *this)
193 {
194 return (this->key_exchange_data);
195 }
196
197 /**
198 * Implementation of ke_payload_t.set_key_exchange_data.
199 */
200 static void set_key_exchange_data(private_ke_payload_t *this, chunk_t key_exchange_data)
201 {
202 /* destroy existing data first */
203 if (this->key_exchange_data.ptr != NULL)
204 {
205 /* free existing value */
206 free(this->key_exchange_data.ptr);
207 this->key_exchange_data.ptr = NULL;
208 this->key_exchange_data.len = 0;
209
210 }
211
212 this->key_exchange_data = chunk_clone(key_exchange_data);
213 compute_length(this);
214 }
215
216 /**
217 * Implementation of ke_payload_t.get_dh_group_number.
218 */
219 static diffie_hellman_group_t get_dh_group_number(private_ke_payload_t *this)
220 {
221 return this->dh_group_number;
222 }
223
224 /**
225 * Implementation of ke_payload_t.set_dh_group_number.
226 */
227 static void set_dh_group_number(private_ke_payload_t *this, diffie_hellman_group_t dh_group_number)
228 {
229 this->dh_group_number = dh_group_number;
230 }
231
232 /*
233 * Described in header
234 */
235 ke_payload_t *ke_payload_create()
236 {
237 private_ke_payload_t *this = malloc_thing(private_ke_payload_t);
238
239 /* interface functions */
240 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
241 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
242 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
243 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
244 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
245 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
246 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
247
248 /* public functions */
249 this->public.get_key_exchange_data = (chunk_t (*) (ke_payload_t *)) get_key_exchange_data;
250 this->public.set_key_exchange_data = (void (*) (ke_payload_t *,chunk_t)) set_key_exchange_data;
251 this->public.get_dh_group_number = (diffie_hellman_group_t (*) (ke_payload_t *)) get_dh_group_number;
252 this->public.set_dh_group_number =(void (*) (ke_payload_t *,diffie_hellman_group_t)) set_dh_group_number;
253 this->public.destroy = (void (*) (ke_payload_t *)) destroy;
254
255 /* set default values of the fields */
256 this->critical = FALSE;
257 this->next_payload = NO_PAYLOAD;
258 this->payload_length = KE_PAYLOAD_HEADER_LENGTH;
259 this->key_exchange_data = CHUNK_INITIALIZER;
260 this->dh_group_number = MODP_NONE;
261
262 return &this->public;
263 }
264
265 /*
266 * Described in header
267 */
268 ke_payload_t *ke_payload_create_from_diffie_hellman(diffie_hellman_t *dh)
269 {
270 private_ke_payload_t *this = (private_ke_payload_t*)ke_payload_create();
271
272 dh->get_my_public_value(dh, &this->key_exchange_data);
273 this->dh_group_number = dh->get_dh_group(dh);
274 compute_length(this);
275
276 return &this->public;
277 }