]> git.ipfire.org Git - people/ms/strongswan.git/blob - programs/pluto/ike_alg.c
- import of strongswan-2.7.0
[people/ms/strongswan.git] / programs / pluto / ike_alg.c
1 /* IKE modular algorithm handling interface
2 * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * for more details.
13 *
14 * RCSID $Id: ike_alg.c,v 1.6 2004/09/17 21:29:50 as Exp $
15 */
16
17 #include <stdio.h>
18 #include <string.h>
19 #include <stdlib.h>
20 #include <errno.h>
21 #include <sys/queue.h>
22
23 #include <freeswan.h>
24 #include <freeswan/ipsec_policy.h>
25
26 #include "constants.h"
27 #include "defs.h"
28 #include "sha1.h"
29 #include "md5.h"
30 #include "crypto.h"
31
32 #include "state.h"
33 #include "packet.h"
34 #include "log.h"
35 #include "whack.h"
36 #include "spdb.h"
37 #include "alg_info.h"
38 #include "ike_alg.h"
39 #include "db_ops.h"
40 #include "connections.h"
41 #include "kernel.h"
42
43 #define return_on(var, val) do { var=val;goto return_out; } while(0);
44
45 /*
46 * IKE algorithm list handling - registration and lookup
47 */
48
49 /* Modular IKE algorithm storage structure */
50
51 static struct ike_alg *ike_alg_base[IKE_ALG_MAX+1] = {NULL, NULL};
52
53 /*
54 * return ike_algo object by {type, id}
55 */
56 static struct ike_alg *
57 ike_alg_find(u_int algo_type, u_int algo_id, u_int keysize __attribute__((unused)))
58 {
59 struct ike_alg *e = ike_alg_base[algo_type];
60
61 while (e != NULL && algo_id > e->algo_id)
62 {
63 e = e->algo_next;
64 }
65 return (e != NULL && e->algo_id == algo_id) ? e : NULL;
66 }
67
68 /*
69 * "raw" ike_alg list adding function
70 */
71 int
72 ike_alg_add(struct ike_alg* a)
73 {
74 if (a->algo_type > IKE_ALG_MAX)
75 {
76 plog("ike_alg: Not added, invalid algorithm type");
77 return -EINVAL;
78 }
79
80 if (ike_alg_find(a->algo_type, a->algo_id, 0) != NULL)
81 {
82 plog("ike_alg: Not added, algorithm already exists");
83 return -EEXIST;
84 }
85
86 {
87 struct ike_alg **ep = &ike_alg_base[a->algo_type];
88 struct ike_alg *e = *ep;
89
90 while (e != NULL && a->algo_id > e->algo_id)
91 {
92 ep = &e->algo_next;
93 e = *ep;
94 }
95 *ep = a;
96 a->algo_next = e;
97 return 0;
98 }
99 }
100
101 /*
102 * get IKE hash algorithm
103 */
104 struct hash_desc *ike_alg_get_hasher(u_int alg)
105 {
106 return (struct hash_desc *) ike_alg_find(IKE_ALG_HASH, alg, 0);
107 }
108
109 /*
110 * get IKE encryption algorithm
111 */
112 struct encrypt_desc *ike_alg_get_encrypter(u_int alg)
113 {
114 return (struct encrypt_desc *) ike_alg_find(IKE_ALG_ENCRYPT, alg, 0);
115 }
116
117 /*
118 * check if IKE hash algorithm is present
119 */
120 bool
121 ike_alg_hash_present(u_int halg)
122 {
123 return ike_alg_get_hasher(halg) != NULL;
124 }
125
126 /*
127 * check if IKE encryption algorithm is present
128 */
129 bool
130 ike_alg_enc_present(u_int ealg)
131 {
132 return ike_alg_get_encrypter(ealg) != NULL;
133 }
134
135 /*
136 * Validate and register IKE hash algorithm object
137 */
138 int
139 ike_alg_register_hash(struct hash_desc *hash_desc)
140 {
141 const char *alg_name = NULL;
142 int ret = 0;
143
144 if (hash_desc->algo_id > OAKLEY_HASH_MAX)
145 {
146 plog ("ike_alg: hash alg=%d > max=%d"
147 , hash_desc->algo_id, OAKLEY_HASH_MAX);
148 return_on(ret,-EINVAL);
149 }
150
151 if (hash_desc->hash_ctx_size > sizeof (union hash_ctx))
152 {
153 plog ("ike_alg: hash alg=%d has ctx_size=%d > hash_ctx=%d"
154 , hash_desc->algo_id
155 , (int)hash_desc->hash_ctx_size
156 , (int)sizeof (union hash_ctx));
157 return_on(ret,-EOVERFLOW);
158 }
159
160 if (!(hash_desc->hash_init && hash_desc->hash_update && hash_desc->hash_final))
161 {
162 plog ("ike_alg: hash alg=%d needs hash_init(), hash_update() and hash_final()"
163 , hash_desc->algo_id);
164 return_on(ret,-EINVAL);
165 }
166
167 alg_name = enum_name(&oakley_hash_names, hash_desc->algo_id);
168 if (!alg_name)
169 {
170 plog ("ike_alg: hash alg=%d not found in constants.c:oakley_hash_names"
171 , hash_desc->algo_id);
172 alg_name = "<NULL>";
173 }
174
175 return_out:
176 if (ret == 0)
177 ret = ike_alg_add((struct ike_alg *)hash_desc);
178
179 plog("ike_alg: Activating %s hash: %s"
180 ,alg_name, ret == 0 ? "Ok" : "FAILED");
181
182 return ret;
183 }
184
185 /*
186 * Validate and register IKE encryption algorithm object
187 */
188 int
189 ike_alg_register_enc(struct encrypt_desc *enc_desc)
190 {
191 int ret = ike_alg_add((struct ike_alg *)enc_desc);
192
193 const char *alg_name = enum_name(&oakley_enc_names, enc_desc->algo_id);
194
195 char alg_number[20];
196
197 /* algorithm is not listed in oakley_enc_names */
198 if (alg_name == NULL)
199 {
200 snprintf(alg_number, sizeof(alg_number), "OAKLEY_ID_%d"
201 , enc_desc->algo_id);
202 alg_name = alg_number;
203 }
204
205 plog("ike_alg: Activating %s encryption: %s"
206 , alg_name, ret == 0 ? "Ok" : "FAILED");
207
208 return ret;
209 }
210
211 /*
212 * Get pfsgroup for this connection
213 */
214 const struct oakley_group_desc *
215 ike_alg_pfsgroup(struct connection *c, lset_t policy)
216 {
217 const struct oakley_group_desc * ret = NULL;
218
219 if ((policy & POLICY_PFS)
220 && c->alg_info_esp
221 && c->alg_info_esp->esp_pfsgroup)
222 ret = lookup_group(c->alg_info_esp->esp_pfsgroup);
223 return ret;
224 }
225
226 /*
227 * Create an OAKLEY proposal based on alg_info and policy
228 */
229 struct db_context *
230 ike_alg_db_new(struct alg_info_ike *ai , lset_t policy)
231 {
232 struct db_context *db_ctx = NULL;
233 struct ike_info *ike_info;
234 u_int ealg, halg, modp, eklen = 0;
235 struct encrypt_desc *enc_desc;
236 int i;
237
238 if (!ai)
239 {
240 whack_log(RC_LOG_SERIOUS, "no IKE algorithms "
241 "for this connection "
242 "(check ike algorithm string)");
243 goto fail;
244 }
245 policy &= POLICY_ID_AUTH_MASK;
246 db_ctx = db_prop_new(PROTO_ISAKMP, 8, 8 * 5);
247
248 /* for each group */
249 ALG_INFO_IKE_FOREACH(ai, ike_info, i)
250 {
251 ealg = ike_info->ike_ealg;
252 halg = ike_info->ike_halg;
253 modp = ike_info->ike_modp;
254 eklen= ike_info->ike_eklen;
255
256 if (!ike_alg_enc_present(ealg))
257 {
258 DBG_log("ike_alg: ike enc ealg=%d not present"
259 , ealg);
260 continue;
261 }
262
263 if (!ike_alg_hash_present(halg))
264 {
265 DBG_log("ike_alg: ike hash halg=%d not present"
266 , halg);
267 continue;
268 }
269
270 enc_desc = ike_alg_get_encrypter(ealg);
271 passert(enc_desc != NULL);
272
273 if (eklen
274 && (eklen < enc_desc->keyminlen || eklen > enc_desc->keymaxlen))
275 {
276 DBG_log("ike_alg: ealg=%d (specified) keylen:%d, not valid min=%d, max=%d"
277 , ealg
278 , eklen
279 , enc_desc->keyminlen
280 , enc_desc->keymaxlen
281 );
282 continue;
283 }
284
285 if (policy & POLICY_RSASIG)
286 {
287 db_trans_add(db_ctx, KEY_IKE);
288 db_attr_add_values(db_ctx, OAKLEY_ENCRYPTION_ALGORITHM, ealg);
289 db_attr_add_values(db_ctx, OAKLEY_HASH_ALGORITHM, halg);
290 if (eklen)
291 db_attr_add_values(db_ctx, OAKLEY_KEY_LENGTH, eklen);
292 db_attr_add_values(db_ctx, OAKLEY_AUTHENTICATION_METHOD, OAKLEY_RSA_SIG);
293 db_attr_add_values(db_ctx, OAKLEY_GROUP_DESCRIPTION, modp);
294 }
295
296 if (policy & POLICY_PSK)
297 {
298 db_trans_add(db_ctx, KEY_IKE);
299 db_attr_add_values(db_ctx, OAKLEY_ENCRYPTION_ALGORITHM, ealg);
300 db_attr_add_values(db_ctx, OAKLEY_HASH_ALGORITHM, halg);
301 if (ike_info->ike_eklen)
302 db_attr_add_values(db_ctx, OAKLEY_KEY_LENGTH, ike_info->ike_eklen);
303 db_attr_add_values(db_ctx, OAKLEY_AUTHENTICATION_METHOD, OAKLEY_PRESHARED_KEY);
304 db_attr_add_values(db_ctx, OAKLEY_GROUP_DESCRIPTION, modp);
305 }
306 }
307 fail:
308 return db_ctx;
309 }
310
311 /*
312 * Show registered IKE algorithms
313 */
314 void
315 ike_alg_list(void)
316 {
317 u_int i;
318 struct ike_alg *a;
319
320 whack_log(RC_COMMENT, " ");
321 whack_log(RC_COMMENT, "List of registered IKE Encryption Algorithms:");
322 whack_log(RC_COMMENT, " ");
323
324 for (a = ike_alg_base[IKE_ALG_ENCRYPT]; a != NULL; a = a->algo_next)
325 {
326 struct encrypt_desc *desc = (struct encrypt_desc*)a;
327
328 whack_log(RC_COMMENT, "#%-5d %s, blocksize: %d, keylen: %d-%d-%d"
329 , a->algo_id
330 , enum_name(&oakley_enc_names, a->algo_id)
331 , (int)desc->enc_blocksize*BITS_PER_BYTE
332 , desc->keyminlen
333 , desc->keydeflen
334 , desc->keymaxlen
335 );
336 }
337
338 whack_log(RC_COMMENT, " ");
339 whack_log(RC_COMMENT, "List of registered IKE Hash Algorithms:");
340 whack_log(RC_COMMENT, " ");
341
342 for (a = ike_alg_base[IKE_ALG_HASH]; a != NULL; a = a->algo_next)
343 {
344 whack_log(RC_COMMENT, "#%-5d %s, hashsize: %d"
345 , a->algo_id
346 , enum_name(&oakley_hash_names, a->algo_id)
347 , (int)((struct hash_desc *)a)->hash_digest_size*BITS_PER_BYTE
348 );
349 }
350
351 whack_log(RC_COMMENT, " ");
352 whack_log(RC_COMMENT, "List of registered IKE DH Groups:");
353 whack_log(RC_COMMENT, " ");
354
355 for (i = 0; i < elemsof(oakley_group); i++)
356 {
357 const struct oakley_group_desc *gdesc=oakley_group + i;
358
359 whack_log(RC_COMMENT, "#%-5d %s, groupsize: %d"
360 , gdesc->group
361 , enum_name(&oakley_group_names, gdesc->group)
362 , (int)gdesc->bytes*BITS_PER_BYTE
363 );
364 }
365 }
366
367 /* Show IKE algorithms for
368 * - this connection (result from ike= string)
369 * - newest SA
370 */
371 void
372 ike_alg_show_connection(struct connection *c, const char *instance)
373 {
374 char buf[256];
375 struct state *st;
376
377 if (c->alg_info_ike)
378 {
379 alg_info_snprint(buf, sizeof(buf)-1, (struct alg_info *)c->alg_info_ike);
380 whack_log(RC_COMMENT
381 , "\"%s\"%s: IKE algorithms wanted: %s"
382 , c->name
383 , instance
384 , buf
385 );
386
387 alg_info_snprint_ike(buf, sizeof(buf)-1, c->alg_info_ike);
388 whack_log(RC_COMMENT
389 , "\"%s\"%s: IKE algorithms found: %s"
390 , c->name
391 , instance
392 , buf
393 );
394 }
395
396 st = state_with_serialno(c->newest_isakmp_sa);
397 if (st)
398 whack_log(RC_COMMENT
399 , "\"%s\"%s: IKE algorithm newest: %s_%d-%s-%s"
400 , c->name
401 , instance
402 , enum_show(&oakley_enc_names, st->st_oakley.encrypt)
403 +7 /* strlen("OAKLEY_") */
404 /* , st->st_oakley.encrypter->keydeflen */
405 , st->st_oakley.enckeylen
406 , enum_show(&oakley_hash_names, st->st_oakley.hash)
407 +7 /* strlen("OAKLEY_") */
408 , enum_show(&oakley_group_names, st->st_oakley.group->group)
409 +13 /* strlen("OAKLEY_GROUP_") */
410 );
411 }
412
413 /*
414 * ML: make F_STRICT logic consider enc,hash/auth,modp algorithms
415 */
416 bool
417 ike_alg_ok_final(u_int ealg, u_int key_len, u_int aalg, u_int group
418 , struct alg_info_ike *alg_info_ike)
419 {
420 /*
421 * simple test to discard low key_len, will accept it only
422 * if specified in "esp" string
423 */
424 bool ealg_insecure = (key_len < 128);
425
426 if (ealg_insecure
427 || (alg_info_ike && alg_info_ike->alg_info_flags & ALG_INFO_F_STRICT))
428 {
429 int i;
430 struct ike_info *ike_info;
431
432 if (alg_info_ike)
433 {
434 ALG_INFO_IKE_FOREACH(alg_info_ike, ike_info, i)
435 {
436 if (ike_info->ike_ealg == ealg
437 && (ike_info->ike_eklen == 0 || key_len == 0 || ike_info->ike_eklen == key_len)
438 && ike_info->ike_halg == aalg
439 && ike_info->ike_modp == group)
440 {
441 if (ealg_insecure)
442 loglog(RC_LOG_SERIOUS, "You should NOT use insecure IKE algorithms (%s)!"
443 , enum_name(&oakley_enc_names, ealg));
444 return TRUE;
445 }
446 }
447 }
448 plog("Oakley Transform [%s (%d), %s, %s] refused due to %s"
449 , enum_name(&oakley_enc_names, ealg), key_len
450 , enum_name(&oakley_hash_names, aalg)
451 , enum_name(&oakley_group_names, group)
452 , ealg_insecure ?
453 "insecure key_len and enc. alg. not listed in \"ike\" string" : "strict flag"
454 );
455 return FALSE;
456 }
457 return TRUE;
458 }
459