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