encoding: Verify the length of KE payload data for known groups
[strongswan.git] / src / libcharon / encoding / payloads / ke_payload.c
1 /*
2 * Copyright (C) 2005-2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include <stddef.h>
19
20 #include "ke_payload.h"
21
22 #include <encoding/payloads/encodings.h>
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 struct private_ke_payload_t {
30
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 * Reserved bits
48 */
49 bool reserved_bit[7];
50
51 /**
52 * Reserved bytes
53 */
54 u_int8_t reserved_byte[2];
55
56 /**
57 * Length of this payload.
58 */
59 u_int16_t payload_length;
60
61 /**
62 * DH Group Number.
63 */
64 u_int16_t dh_group_number;
65
66 /**
67 * Key Exchange Data of this KE payload.
68 */
69 chunk_t key_exchange_data;
70
71 /**
72 * Payload type, PLV2_KEY_EXCHANGE or PLV1_KEY_EXCHANGE
73 */
74 payload_type_t type;
75 };
76
77 /**
78 * Encoding rules for IKEv2 key exchange payload.
79 */
80 static encoding_rule_t encodings_v2[] = {
81 /* 1 Byte next payload type, stored in the field next_payload */
82 { U_INT_8, offsetof(private_ke_payload_t, next_payload) },
83 /* the critical bit */
84 { FLAG, offsetof(private_ke_payload_t, critical) },
85 /* 7 Bit reserved bits */
86 { RESERVED_BIT, offsetof(private_ke_payload_t, reserved_bit[0]) },
87 { RESERVED_BIT, offsetof(private_ke_payload_t, reserved_bit[1]) },
88 { RESERVED_BIT, offsetof(private_ke_payload_t, reserved_bit[2]) },
89 { RESERVED_BIT, offsetof(private_ke_payload_t, reserved_bit[3]) },
90 { RESERVED_BIT, offsetof(private_ke_payload_t, reserved_bit[4]) },
91 { RESERVED_BIT, offsetof(private_ke_payload_t, reserved_bit[5]) },
92 { RESERVED_BIT, offsetof(private_ke_payload_t, reserved_bit[6]) },
93 /* Length of the whole payload*/
94 { PAYLOAD_LENGTH, offsetof(private_ke_payload_t, payload_length) },
95 /* DH Group number as 16 bit field*/
96 { U_INT_16, offsetof(private_ke_payload_t, dh_group_number) },
97 /* 2 reserved bytes */
98 { RESERVED_BYTE, offsetof(private_ke_payload_t, reserved_byte[0])},
99 { RESERVED_BYTE, offsetof(private_ke_payload_t, reserved_byte[1])},
100 /* Key Exchange Data is from variable size */
101 { CHUNK_DATA, offsetof(private_ke_payload_t, key_exchange_data)},
102 };
103
104 /*
105 1 2 3
106 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
107 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
108 ! Next Payload !C! RESERVED ! Payload Length !
109 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
110 ! DH Group # ! RESERVED !
111 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
112 ! !
113 ~ Key Exchange Data ~
114 ! !
115 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
116 */
117
118 static encoding_rule_t encodings_v1[] = {
119 /* 1 Byte next payload type, stored in the field next_payload */
120 { U_INT_8, offsetof(private_ke_payload_t, next_payload) },
121 /* Reserved Byte */
122 { RESERVED_BYTE, offsetof(private_ke_payload_t, reserved_byte[0])},
123 /* Length of the whole payload*/
124 { PAYLOAD_LENGTH, offsetof(private_ke_payload_t, payload_length) },
125 /* Key Exchange Data is from variable size */
126 { CHUNK_DATA, offsetof(private_ke_payload_t, key_exchange_data)},
127 };
128
129 /*
130 1 2 3
131 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
132 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
133 ! Next Payload ! RESERVED ! Payload Length !
134 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
135 ! !
136 ~ Key Exchange Data ~
137 ! !
138 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
139 */
140
141
142 METHOD(payload_t, verify, status_t,
143 private_ke_payload_t *this)
144 {
145 diffie_hellman_params_t *params;
146 diffie_hellman_group_t g = this->dh_group_number;
147 bool valid = TRUE;
148
149 switch (g)
150 {
151 case MODP_NONE:
152 valid = FALSE;
153 break;
154 case MODP_768_BIT:
155 case MODP_1024_BIT:
156 case MODP_1536_BIT:
157 case MODP_2048_BIT:
158 case MODP_3072_BIT:
159 case MODP_4096_BIT:
160 case MODP_6144_BIT:
161 case MODP_8192_BIT:
162 case MODP_1024_160:
163 case MODP_2048_224:
164 case MODP_2048_256:
165 params = diffie_hellman_get_params(g);
166 if (params)
167 {
168 valid = this->key_exchange_data.len == params->prime.len;
169 }
170 break;
171 case ECP_192_BIT:
172 valid = this->key_exchange_data.len == 48;
173 break;
174 case ECP_224_BIT:
175 case ECP_224_BP:
176 valid = this->key_exchange_data.len == 56;
177 break;
178 case ECP_256_BIT:
179 case ECP_256_BP:
180 valid = this->key_exchange_data.len == 64;
181 break;
182 case ECP_384_BIT:
183 case ECP_384_BP:
184 valid = this->key_exchange_data.len == 96;
185 break;
186 case ECP_512_BP:
187 valid = this->key_exchange_data.len == 128;
188 break;
189 case ECP_521_BIT:
190 valid = this->key_exchange_data.len == 132;
191 break;
192 case NTRU_112_BIT:
193 case NTRU_128_BIT:
194 case NTRU_192_BIT:
195 case NTRU_256_BIT:
196 /* NTRU public key size depends on the parameter set, but is
197 * at least 512 bytes */
198 valid = this->key_exchange_data.len > 512;
199 break;
200 case MODP_NULL:
201 case MODP_CUSTOM:
202 break;
203 /* compile-warn unhandled groups, but accept them so we can negotiate
204 * a different group that we support. */
205 }
206 if (!valid)
207 {
208 DBG1(DBG_ENC, "invalid KE data size (%zu bytes) for %N",
209 this->key_exchange_data.len, diffie_hellman_group_names, g);
210 return FAILED;
211 }
212 return SUCCESS;
213 }
214
215 METHOD(payload_t, get_encoding_rules, int,
216 private_ke_payload_t *this, encoding_rule_t **rules)
217 {
218 if (this->type == PLV2_KEY_EXCHANGE)
219 {
220 *rules = encodings_v2;
221 return countof(encodings_v2);
222 }
223 *rules = encodings_v1;
224 return countof(encodings_v1);
225 }
226
227 METHOD(payload_t, get_header_length, int,
228 private_ke_payload_t *this)
229 {
230 if (this->type == PLV2_KEY_EXCHANGE)
231 {
232 return 8;
233 }
234 return 4;
235 }
236
237 METHOD(payload_t, get_type, payload_type_t,
238 private_ke_payload_t *this)
239 {
240 return this->type;
241 }
242
243 METHOD(payload_t, get_next_type, payload_type_t,
244 private_ke_payload_t *this)
245 {
246 return this->next_payload;
247 }
248
249 METHOD(payload_t, set_next_type, void,
250 private_ke_payload_t *this,payload_type_t type)
251 {
252 this->next_payload = type;
253 }
254
255 METHOD(payload_t, get_length, size_t,
256 private_ke_payload_t *this)
257 {
258 return this->payload_length;
259 }
260
261 METHOD(ke_payload_t, get_key_exchange_data, chunk_t,
262 private_ke_payload_t *this)
263 {
264 return this->key_exchange_data;
265 }
266
267 METHOD(ke_payload_t, get_dh_group_number, diffie_hellman_group_t,
268 private_ke_payload_t *this)
269 {
270 return this->dh_group_number;
271 }
272
273 METHOD2(payload_t, ke_payload_t, destroy, void,
274 private_ke_payload_t *this)
275 {
276 free(this->key_exchange_data.ptr);
277 free(this);
278 }
279
280 /*
281 * Described in header
282 */
283 ke_payload_t *ke_payload_create(payload_type_t type)
284 {
285 private_ke_payload_t *this;
286
287 INIT(this,
288 .public = {
289 .payload_interface = {
290 .verify = _verify,
291 .get_encoding_rules = _get_encoding_rules,
292 .get_header_length = _get_header_length,
293 .get_length = _get_length,
294 .get_next_type = _get_next_type,
295 .set_next_type = _set_next_type,
296 .get_type = _get_type,
297 .destroy = _destroy,
298 },
299 .get_key_exchange_data = _get_key_exchange_data,
300 .get_dh_group_number = _get_dh_group_number,
301 .destroy = _destroy,
302 },
303 .next_payload = PL_NONE,
304 .dh_group_number = MODP_NONE,
305 .type = type,
306 );
307 this->payload_length = get_header_length(this);
308 return &this->public;
309 }
310
311 /*
312 * Described in header
313 */
314 ke_payload_t *ke_payload_create_from_diffie_hellman(payload_type_t type,
315 diffie_hellman_t *dh)
316 {
317 private_ke_payload_t *this = (private_ke_payload_t*)ke_payload_create(type);
318
319 dh->get_my_public_value(dh, &this->key_exchange_data);
320 this->dh_group_number = dh->get_dh_group(dh);
321 this->payload_length += this->key_exchange_data.len;
322
323 return &this->public;
324 }