Do not update payload length during generation, allows hooks override payload length
[strongswan.git] / src / libcharon / 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
167 if (this->key_exchange_data.ptr != NULL)
168 {
169 length += this->key_exchange_data.len;
170 }
171 this->payload_length = length;
172 }
173
174 /**
175 * Implementation of payload_t.get_length.
176 */
177 static size_t get_length(private_ke_payload_t *this)
178 {
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.get_dh_group_number.
192 */
193 static diffie_hellman_group_t get_dh_group_number(private_ke_payload_t *this)
194 {
195 return this->dh_group_number;
196 }
197
198 /**
199 * Implementation of ke_payload_t.set_dh_group_number.
200 */
201 static void set_dh_group_number(private_ke_payload_t *this, diffie_hellman_group_t dh_group_number)
202 {
203 this->dh_group_number = dh_group_number;
204 }
205
206 /*
207 * Described in header
208 */
209 ke_payload_t *ke_payload_create()
210 {
211 private_ke_payload_t *this = malloc_thing(private_ke_payload_t);
212
213 /* interface functions */
214 this->public.payload_interface.verify = (status_t (*) (payload_t *))verify;
215 this->public.payload_interface.get_encoding_rules = (void (*) (payload_t *, encoding_rule_t **, size_t *) ) get_encoding_rules;
216 this->public.payload_interface.get_length = (size_t (*) (payload_t *)) get_length;
217 this->public.payload_interface.get_next_type = (payload_type_t (*) (payload_t *)) get_next_type;
218 this->public.payload_interface.set_next_type = (void (*) (payload_t *,payload_type_t)) set_next_type;
219 this->public.payload_interface.get_type = (payload_type_t (*) (payload_t *)) get_type;
220 this->public.payload_interface.destroy = (void (*) (payload_t *))destroy;
221
222 /* public functions */
223 this->public.get_key_exchange_data = (chunk_t (*) (ke_payload_t *)) get_key_exchange_data;
224 this->public.get_dh_group_number = (diffie_hellman_group_t (*) (ke_payload_t *)) get_dh_group_number;
225 this->public.set_dh_group_number =(void (*) (ke_payload_t *,diffie_hellman_group_t)) set_dh_group_number;
226 this->public.destroy = (void (*) (ke_payload_t *)) destroy;
227
228 /* set default values of the fields */
229 this->critical = FALSE;
230 this->next_payload = NO_PAYLOAD;
231 this->payload_length = KE_PAYLOAD_HEADER_LENGTH;
232 this->key_exchange_data = chunk_empty;
233 this->dh_group_number = MODP_NONE;
234
235 return &this->public;
236 }
237
238 /*
239 * Described in header
240 */
241 ke_payload_t *ke_payload_create_from_diffie_hellman(diffie_hellman_t *dh)
242 {
243 private_ke_payload_t *this = (private_ke_payload_t*)ke_payload_create();
244
245 dh->get_my_public_value(dh, &this->key_exchange_data);
246 this->dh_group_number = dh->get_dh_group(dh);
247 compute_length(this);
248
249 return &this->public;
250 }