removing svn keyword $Id$ from all files
[strongswan.git] / src / 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
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <errno.h>
19 #include <sys/queue.h>
20
21 #include <freeswan.h>
22 #include <ipsec_policy.h>
23
24 #include "constants.h"
25 #include "defs.h"
26 #include "sha1.h"
27 #include "md5.h"
28 #include "crypto.h"
29
30 #include "state.h"
31 #include "packet.h"
32 #include "log.h"
33 #include "whack.h"
34 #include "spdb.h"
35 #include "alg_info.h"
36 #include "ike_alg.h"
37 #include "db_ops.h"
38 #include "connections.h"
39 #include "kernel.h"
40
41 #define return_on(var, val) do { var=val;goto return_out; } while(0);
42
43 /*
44 * IKE algorithm list handling - registration and lookup
45 */
46
47 /* Modular IKE algorithm storage structure */
48
49 static struct ike_alg *ike_alg_base[IKE_ALG_MAX+1] = {NULL, NULL};
50
51 /*
52 * return ike_algo object by {type, id}
53 */
54 static struct ike_alg *
55 ike_alg_find(u_int algo_type, u_int algo_id, u_int keysize __attribute__((unused)))
56 {
57 struct ike_alg *e = ike_alg_base[algo_type];
58
59 while (e != NULL && algo_id > e->algo_id)
60 {
61 e = e->algo_next;
62 }
63 return (e != NULL && e->algo_id == algo_id) ? e : NULL;
64 }
65
66 /*
67 * "raw" ike_alg list adding function
68 */
69 int
70 ike_alg_add(struct ike_alg* a)
71 {
72 if (a->algo_type > IKE_ALG_MAX)
73 {
74 plog("ike_alg: Not added, invalid algorithm type");
75 return -EINVAL;
76 }
77
78 if (ike_alg_find(a->algo_type, a->algo_id, 0) != NULL)
79 {
80 plog("ike_alg: Not added, algorithm already exists");
81 return -EEXIST;
82 }
83
84 {
85 struct ike_alg **ep = &ike_alg_base[a->algo_type];
86 struct ike_alg *e = *ep;
87
88 while (e != NULL && a->algo_id > e->algo_id)
89 {
90 ep = &e->algo_next;
91 e = *ep;
92 }
93 *ep = a;
94 a->algo_next = e;
95 return 0;
96 }
97 }
98
99 /*
100 * get IKE hash algorithm
101 */
102 struct hash_desc *ike_alg_get_hasher(u_int alg)
103 {
104 return (struct hash_desc *) ike_alg_find(IKE_ALG_HASH, alg, 0);
105 }
106
107 /*
108 * get IKE encryption algorithm
109 */
110 struct encrypt_desc *ike_alg_get_encrypter(u_int alg)
111 {
112 return (struct encrypt_desc *) ike_alg_find(IKE_ALG_ENCRYPT, alg, 0);
113 }
114
115 /*
116 * check if IKE hash algorithm is present
117 */
118 bool
119 ike_alg_hash_present(u_int halg)
120 {
121 return ike_alg_get_hasher(halg) != NULL;
122 }
123
124 /*
125 * check if IKE encryption algorithm is present
126 */
127 bool
128 ike_alg_enc_present(u_int ealg)
129 {
130 return ike_alg_get_encrypter(ealg) != NULL;
131 }
132
133 /*
134 * Validate and register IKE hash algorithm object
135 */
136 int
137 ike_alg_register_hash(struct hash_desc *hash_desc)
138 {
139 const char *alg_name = NULL;
140 int ret = 0;
141
142 if (hash_desc->algo_id > OAKLEY_HASH_MAX)
143 {
144 plog ("ike_alg: hash alg=%d > max=%d"
145 , hash_desc->algo_id, OAKLEY_HASH_MAX);
146 return_on(ret,-EINVAL);
147 }
148
149 if (hash_desc->hash_ctx_size > sizeof (union hash_ctx))
150 {
151 plog ("ike_alg: hash alg=%d has ctx_size=%d > hash_ctx=%d"
152 , hash_desc->algo_id
153 , (int)hash_desc->hash_ctx_size
154 , (int)sizeof (union hash_ctx));
155 return_on(ret,-EOVERFLOW);
156 }
157
158 if (!(hash_desc->hash_init && hash_desc->hash_update && hash_desc->hash_final))
159 {
160 plog ("ike_alg: hash alg=%d needs hash_init(), hash_update() and hash_final()"
161 , hash_desc->algo_id);
162 return_on(ret,-EINVAL);
163 }
164
165 alg_name = enum_name(&oakley_hash_names, hash_desc->algo_id);
166 if (!alg_name)
167 {
168 plog ("ike_alg: hash alg=%d not found in constants.c:oakley_hash_names"
169 , hash_desc->algo_id);
170 alg_name = "<NULL>";
171 }
172
173 return_out:
174 if (ret == 0)
175 ret = ike_alg_add((struct ike_alg *)hash_desc);
176
177 plog("ike_alg: Activating %s hash: %s"
178 ,alg_name, ret == 0 ? "Ok" : "FAILED");
179
180 return ret;
181 }
182
183 /*
184 * Validate and register IKE encryption algorithm object
185 */
186 int
187 ike_alg_register_enc(struct encrypt_desc *enc_desc)
188 {
189 int ret = ike_alg_add((struct ike_alg *)enc_desc);
190
191 const char *alg_name = enum_name(&oakley_enc_names, enc_desc->algo_id);
192
193 char alg_number[20];
194
195 /* algorithm is not listed in oakley_enc_names */
196 if (alg_name == NULL)
197 {
198 snprintf(alg_number, sizeof(alg_number), "OAKLEY_ID_%d"
199 , enc_desc->algo_id);
200 alg_name = alg_number;
201 }
202
203 plog("ike_alg: Activating %s encryption: %s"
204 , alg_name, ret == 0 ? "Ok" : "FAILED");
205
206 return ret;
207 }
208
209 /*
210 * Get pfsgroup for this connection
211 */
212 const struct oakley_group_desc *
213 ike_alg_pfsgroup(struct connection *c, lset_t policy)
214 {
215 const struct oakley_group_desc * ret = NULL;
216
217 if ((policy & POLICY_PFS)
218 && c->alg_info_esp
219 && c->alg_info_esp->esp_pfsgroup)
220 ret = lookup_group(c->alg_info_esp->esp_pfsgroup);
221 return ret;
222 }
223
224 /*
225 * Create an OAKLEY proposal based on alg_info and policy
226 */
227 struct db_context *
228 ike_alg_db_new(struct alg_info_ike *ai , lset_t policy)
229 {
230 struct db_context *db_ctx = NULL;
231 struct ike_info *ike_info;
232 struct encrypt_desc *enc_desc;
233 u_int ealg, halg, modp, eklen = 0;
234 int i;
235
236 bool is_xauth_server = (policy & POLICY_XAUTH_SERVER) != LEMPTY;
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 (eklen)
302 db_attr_add_values(db_ctx, OAKLEY_KEY_LENGTH, 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 if (policy & POLICY_XAUTH_RSASIG)
308 {
309 db_trans_add(db_ctx, KEY_IKE);
310 db_attr_add_values(db_ctx, OAKLEY_ENCRYPTION_ALGORITHM, ealg);
311 db_attr_add_values(db_ctx, OAKLEY_HASH_ALGORITHM, halg);
312 if (eklen)
313 db_attr_add_values(db_ctx, OAKLEY_KEY_LENGTH, eklen);
314 db_attr_add_values(db_ctx, OAKLEY_AUTHENTICATION_METHOD
315 , is_xauth_server ? XAUTHRespRSA : XAUTHInitRSA);
316 db_attr_add_values(db_ctx, OAKLEY_GROUP_DESCRIPTION, modp);
317 }
318
319 if (policy & POLICY_XAUTH_PSK)
320 {
321 db_trans_add(db_ctx, KEY_IKE);
322 db_attr_add_values(db_ctx, OAKLEY_ENCRYPTION_ALGORITHM, ealg);
323 db_attr_add_values(db_ctx, OAKLEY_HASH_ALGORITHM, halg);
324 if (eklen)
325 db_attr_add_values(db_ctx, OAKLEY_KEY_LENGTH, eklen);
326 db_attr_add_values(db_ctx, OAKLEY_AUTHENTICATION_METHOD
327 , is_xauth_server ? XAUTHRespPreShared : XAUTHInitPreShared);
328 db_attr_add_values(db_ctx, OAKLEY_GROUP_DESCRIPTION, modp);
329 }
330 }
331 fail:
332 return db_ctx;
333 }
334
335 /*
336 * Show registered IKE algorithms
337 */
338 void
339 ike_alg_list(void)
340 {
341 u_int i;
342 struct ike_alg *a;
343
344 whack_log(RC_COMMENT, " ");
345 whack_log(RC_COMMENT, "List of registered IKE Encryption Algorithms:");
346 whack_log(RC_COMMENT, " ");
347
348 for (a = ike_alg_base[IKE_ALG_ENCRYPT]; a != NULL; a = a->algo_next)
349 {
350 struct encrypt_desc *desc = (struct encrypt_desc*)a;
351
352 whack_log(RC_COMMENT, "#%-5d %s, blocksize: %d, keylen: %d-%d-%d"
353 , a->algo_id
354 , enum_name(&oakley_enc_names, a->algo_id)
355 , (int)desc->enc_blocksize*BITS_PER_BYTE
356 , desc->keyminlen
357 , desc->keydeflen
358 , desc->keymaxlen
359 );
360 }
361
362 whack_log(RC_COMMENT, " ");
363 whack_log(RC_COMMENT, "List of registered IKE Hash Algorithms:");
364 whack_log(RC_COMMENT, " ");
365
366 for (a = ike_alg_base[IKE_ALG_HASH]; a != NULL; a = a->algo_next)
367 {
368 whack_log(RC_COMMENT, "#%-5d %s, hashsize: %d"
369 , a->algo_id
370 , enum_name(&oakley_hash_names, a->algo_id)
371 , (int)((struct hash_desc *)a)->hash_digest_size*BITS_PER_BYTE
372 );
373 }
374
375 whack_log(RC_COMMENT, " ");
376 whack_log(RC_COMMENT, "List of registered IKE DH Groups:");
377 whack_log(RC_COMMENT, " ");
378
379 for (i = 0; i < countof(oakley_group); i++)
380 {
381 const struct oakley_group_desc *gdesc=oakley_group + i;
382
383 whack_log(RC_COMMENT, "#%-5d %s, groupsize: %d"
384 , gdesc->group
385 , enum_name(&oakley_group_names, gdesc->group)
386 , (int)gdesc->bytes*BITS_PER_BYTE
387 );
388 }
389 }
390
391 /* Show IKE algorithms for
392 * - this connection (result from ike= string)
393 * - newest SA
394 */
395 void
396 ike_alg_show_connection(struct connection *c, const char *instance)
397 {
398 char buf[256];
399 struct state *st;
400
401 if (c->alg_info_ike)
402 {
403 alg_info_snprint(buf, sizeof(buf)-1, (struct alg_info *)c->alg_info_ike);
404 whack_log(RC_COMMENT
405 , "\"%s\"%s: IKE algorithms wanted: %s"
406 , c->name
407 , instance
408 , buf
409 );
410
411 alg_info_snprint_ike(buf, sizeof(buf)-1, c->alg_info_ike);
412 whack_log(RC_COMMENT
413 , "\"%s\"%s: IKE algorithms found: %s"
414 , c->name
415 , instance
416 , buf
417 );
418 }
419
420 st = state_with_serialno(c->newest_isakmp_sa);
421 if (st)
422 whack_log(RC_COMMENT
423 , "\"%s\"%s: IKE algorithm newest: %s_%d-%s-%s"
424 , c->name
425 , instance
426 , enum_show(&oakley_enc_names, st->st_oakley.encrypt)
427 +7 /* strlen("OAKLEY_") */
428 /* , st->st_oakley.encrypter->keydeflen */
429 , st->st_oakley.enckeylen
430 , enum_show(&oakley_hash_names, st->st_oakley.hash)
431 +7 /* strlen("OAKLEY_") */
432 , enum_show(&oakley_group_names, st->st_oakley.group->group)
433 +13 /* strlen("OAKLEY_GROUP_") */
434 );
435 }
436
437 /*
438 * Apply a suite of testvectors to a hash algorithm
439 */
440 static bool
441 ike_hash_test(const struct hash_desc *desc)
442 {
443 bool hash_results = TRUE;
444 bool hmac_results = TRUE;
445
446 if (desc->hash_testvectors == NULL)
447 {
448 plog(" %s hash self-test not available", enum_name(&oakley_hash_names, desc->algo_id));
449 }
450 else
451 {
452 int i;
453
454 for (i = 0; desc->hash_testvectors[i].msg_digest != NULL; i++)
455 {
456 u_char digest[MAX_DIGEST_LEN];
457 bool result;
458
459 union hash_ctx ctx;
460
461 desc->hash_init(&ctx);
462 desc->hash_update(&ctx, desc->hash_testvectors[i].msg
463 ,desc->hash_testvectors[i].msg_size);
464 desc->hash_final(digest, &ctx);
465 result = memeq(digest, desc->hash_testvectors[i].msg_digest
466 , desc->hash_digest_size);
467 DBG(DBG_CRYPT,
468 DBG_log(" hash testvector %d: %s", i, result ? "ok":"failed")
469 )
470 hash_results &= result;
471 }
472 plog(" %s hash self-test %s", enum_name(&oakley_hash_names, desc->algo_id)
473 , hash_results ? "passed":"failed");
474 }
475
476 if (desc->hmac_testvectors == NULL)
477 {
478 plog(" %s hmac self-test not available", enum_name(&oakley_hash_names, desc->algo_id));
479 }
480 else
481 {
482 int i;
483
484 for (i = 0; desc->hmac_testvectors[i].hmac != NULL; i++)
485 {
486 u_char digest[MAX_DIGEST_LEN];
487 bool result;
488
489 struct hmac_ctx ctx;
490
491 hmac_init(&ctx, desc, desc->hmac_testvectors[i].key
492 , desc->hmac_testvectors[i].key_size);
493 hmac_update(&ctx, desc->hmac_testvectors[i].msg
494 ,desc->hmac_testvectors[i].msg_size);
495 hmac_final(digest, &ctx);
496 result = memeq(digest, desc->hmac_testvectors[i].hmac
497 , desc->hash_digest_size);
498 DBG(DBG_CRYPT,
499 DBG_log(" hmac testvector %d: %s", i, result ? "ok":"failed")
500 )
501 hmac_results &= result;
502 }
503 plog(" %s hmac self-test %s", enum_name(&oakley_hash_names, desc->algo_id)
504 , hmac_results ? "passed":"failed");
505 }
506 return hash_results && hmac_results;
507 }
508
509 /*
510 * Apply test vectors to registered encryption and hash algorithms
511 */
512 bool
513 ike_alg_test(void)
514 {
515 bool all_results = TRUE;
516 struct ike_alg *a;
517
518 plog("Testing registered IKE encryption algorithms:");
519
520 for (a = ike_alg_base[IKE_ALG_ENCRYPT]; a != NULL; a = a->algo_next)
521 {
522 plog(" %s self-test not available", enum_name(&oakley_enc_names, a->algo_id));
523 }
524
525 plog("Testing registered IKE hash algorithms:");
526
527 for (a = ike_alg_base[IKE_ALG_HASH]; a != NULL; a = a->algo_next)
528 {
529 struct hash_desc *desc = (struct hash_desc*)a;
530
531 all_results &= ike_hash_test(desc);
532 }
533
534 if (all_results)
535 plog("All crypto self-tests passed");
536 else
537 plog("Some crypto self-tests failed");
538 return all_results;
539 }
540
541 /*
542 * ML: make F_STRICT logic consider enc,hash/auth,modp algorithms
543 */
544 bool
545 ike_alg_ok_final(u_int ealg, u_int key_len, u_int aalg, u_int group
546 , struct alg_info_ike *alg_info_ike)
547 {
548 /*
549 * simple test to discard low key_len, will accept it only
550 * if specified in "esp" string
551 */
552 bool ealg_insecure = (key_len < 128);
553
554 if (ealg_insecure
555 || (alg_info_ike && alg_info_ike->alg_info_flags & ALG_INFO_F_STRICT))
556 {
557 int i;
558 struct ike_info *ike_info;
559
560 if (alg_info_ike)
561 {
562 ALG_INFO_IKE_FOREACH(alg_info_ike, ike_info, i)
563 {
564 if (ike_info->ike_ealg == ealg
565 && (ike_info->ike_eklen == 0 || key_len == 0 || ike_info->ike_eklen == key_len)
566 && ike_info->ike_halg == aalg
567 && ike_info->ike_modp == group)
568 {
569 if (ealg_insecure)
570 loglog(RC_LOG_SERIOUS, "You should NOT use insecure IKE algorithms (%s)!"
571 , enum_name(&oakley_enc_names, ealg));
572 return TRUE;
573 }
574 }
575 }
576 plog("Oakley Transform [%s (%d), %s, %s] refused due to %s"
577 , enum_name(&oakley_enc_names, ealg), key_len
578 , enum_name(&oakley_hash_names, aalg)
579 , enum_name(&oakley_group_names, group)
580 , ealg_insecure ?
581 "insecure key_len and enc. alg. not listed in \"ike\" string" : "strict flag"
582 );
583 return FALSE;
584 }
585 return TRUE;
586 }
587