summaryrefslogtreecommitdiff
path: root/src/pluto/ike_alg.c
blob: 1c6514b4b30b4d0b64ea3fa906931693282075e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
/* IKE modular algorithm handling interface
 * Author: JuanJo Ciarlante <jjo-ipsec@mendoza.gov.ar>
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * RCSID $Id: ike_alg.c,v 1.6 2004/09/17 21:29:50 as Exp $
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/queue.h>

#include <freeswan.h>
#include <ipsec_policy.h>

#include "constants.h"
#include "defs.h"
#include "sha1.h"
#include "md5.h"
#include "crypto.h"

#include "state.h"
#include "packet.h"
#include "log.h"
#include "whack.h"
#include "spdb.h"
#include "alg_info.h"
#include "ike_alg.h"
#include "db_ops.h"
#include "connections.h"
#include "kernel.h"

#define return_on(var, val) do { var=val;goto return_out; } while(0);

/*
 * IKE algorithm list handling - registration and lookup
 */

/* Modular IKE algorithm storage structure */

static struct ike_alg *ike_alg_base[IKE_ALG_MAX+1] = {NULL, NULL};

/*
 * return ike_algo object by {type, id}
 */
static struct ike_alg *
ike_alg_find(u_int algo_type, u_int algo_id, u_int keysize __attribute__((unused)))
{
    struct ike_alg *e = ike_alg_base[algo_type];

    while (e != NULL && algo_id > e->algo_id)
    {
	e = e->algo_next;
    }
    return (e != NULL && e->algo_id == algo_id) ? e : NULL;
}

/*
 * "raw" ike_alg list adding function
 */
int
ike_alg_add(struct ike_alg* a)
{
    if (a->algo_type > IKE_ALG_MAX)
    {
	plog("ike_alg: Not added, invalid algorithm type");
	return -EINVAL;
    }

    if (ike_alg_find(a->algo_type, a->algo_id, 0) != NULL)
    {
	plog("ike_alg: Not added, algorithm already exists");
	return -EEXIST;
    }

    {
	struct ike_alg **ep = &ike_alg_base[a->algo_type];
	struct ike_alg *e = *ep;

	while (e != NULL && a->algo_id > e->algo_id)
	{
	    ep = &e->algo_next;
	    e = *ep;
	}
	*ep = a;
	a->algo_next = e;
	return 0;
    }
}

/*
 * get IKE hash algorithm
 */
struct hash_desc *ike_alg_get_hasher(u_int alg)
{
    return (struct hash_desc *) ike_alg_find(IKE_ALG_HASH, alg, 0);
}

/*
 * get IKE encryption algorithm
 */
struct encrypt_desc *ike_alg_get_encrypter(u_int alg)
{
    return (struct encrypt_desc *) ike_alg_find(IKE_ALG_ENCRYPT, alg, 0);
}

/*
 * check if IKE hash algorithm is present
 */
bool
ike_alg_hash_present(u_int halg)
{
    return ike_alg_get_hasher(halg) != NULL;
}

/*
 * check if IKE encryption algorithm is present
 */
bool
ike_alg_enc_present(u_int ealg)
{
    return ike_alg_get_encrypter(ealg) != NULL;
}

/*
 * Validate and register IKE hash algorithm object
 */
int
ike_alg_register_hash(struct hash_desc *hash_desc)
{
    const char *alg_name = NULL;
    int ret = 0;

    if (hash_desc->algo_id > OAKLEY_HASH_MAX)
    {
	plog ("ike_alg: hash alg=%d > max=%d"
		, hash_desc->algo_id, OAKLEY_HASH_MAX);
	return_on(ret,-EINVAL);
    }

    if (hash_desc->hash_ctx_size > sizeof (union hash_ctx))
    {
	plog ("ike_alg: hash alg=%d has ctx_size=%d > hash_ctx=%d"
	      , hash_desc->algo_id
	      , (int)hash_desc->hash_ctx_size
	      , (int)sizeof (union hash_ctx));
	return_on(ret,-EOVERFLOW);
    }

    if (!(hash_desc->hash_init && hash_desc->hash_update && hash_desc->hash_final))
    {
	plog ("ike_alg: hash alg=%d needs hash_init(), hash_update() and hash_final()"
	      , hash_desc->algo_id);
	return_on(ret,-EINVAL);
    }
	
    alg_name = enum_name(&oakley_hash_names, hash_desc->algo_id);
    if (!alg_name)
    {
	plog ("ike_alg: hash alg=%d not found in constants.c:oakley_hash_names"
	      , hash_desc->algo_id);
	alg_name = "<NULL>";
    }

return_out:
    if (ret == 0)
	ret = ike_alg_add((struct ike_alg *)hash_desc);

    plog("ike_alg: Activating %s hash: %s"
	,alg_name, ret == 0 ? "Ok" : "FAILED");

    return ret;
}

/*
 * Validate and register IKE encryption algorithm object
 */
int
ike_alg_register_enc(struct encrypt_desc *enc_desc)
{
    int ret = ike_alg_add((struct ike_alg *)enc_desc);

    const char *alg_name = enum_name(&oakley_enc_names, enc_desc->algo_id);

    char alg_number[20];
    
    /* algorithm is not listed in oakley_enc_names */
    if (alg_name == NULL)
    {
	snprintf(alg_number, sizeof(alg_number), "OAKLEY_ID_%d"
	    , enc_desc->algo_id);
	alg_name = alg_number;
    }

    plog("ike_alg: Activating %s encryption: %s"
	, alg_name, ret == 0 ? "Ok" : "FAILED");

    return ret;
}

/*
 * Get pfsgroup for this connection
 */
const struct oakley_group_desc *
ike_alg_pfsgroup(struct connection *c, lset_t policy)
{
    const struct oakley_group_desc * ret = NULL;

    if ((policy & POLICY_PFS)
    && c->alg_info_esp
    && c->alg_info_esp->esp_pfsgroup)
	ret = lookup_group(c->alg_info_esp->esp_pfsgroup);
    return ret;
}

/*
 * Create an OAKLEY proposal based on alg_info and policy
 */
struct db_context *
ike_alg_db_new(struct alg_info_ike *ai , lset_t policy)
{
    struct db_context *db_ctx = NULL;
    struct ike_info *ike_info;
    struct encrypt_desc *enc_desc;
    u_int ealg, halg, modp, eklen = 0;
    int i;

    bool is_xauth_server = (policy & POLICY_XAUTH_SERVER) != LEMPTY;

    if (!ai)
    {
	whack_log(RC_LOG_SERIOUS, "no IKE algorithms "
				  "for this connection "
				  "(check ike algorithm string)");
	goto fail;
    }
    policy &= POLICY_ID_AUTH_MASK;
    db_ctx = db_prop_new(PROTO_ISAKMP, 8, 8 * 5);

    /* for each group */
    ALG_INFO_IKE_FOREACH(ai, ike_info, i)
    {
	ealg = ike_info->ike_ealg;
	halg = ike_info->ike_halg;
	modp = ike_info->ike_modp;
	eklen= ike_info->ike_eklen;

	if (!ike_alg_enc_present(ealg))
	{
	    DBG_log("ike_alg: ike enc ealg=%d not present"
		    , ealg);
	    continue;
	}

	if (!ike_alg_hash_present(halg)) 
	{
	    DBG_log("ike_alg: ike hash halg=%d not present"
		    , halg);
	    continue;
	}

	enc_desc = ike_alg_get_encrypter(ealg);
	passert(enc_desc != NULL);

	if (eklen
	&& (eklen < enc_desc->keyminlen || eklen >  enc_desc->keymaxlen))
	{
	    DBG_log("ike_alg: ealg=%d (specified) keylen:%d, not valid min=%d, max=%d"
		    , ealg
		    , eklen
		    , enc_desc->keyminlen
		    , enc_desc->keymaxlen
	    );
	    continue;
	}

	if (policy & POLICY_RSASIG)
	{
	    db_trans_add(db_ctx, KEY_IKE);
	    db_attr_add_values(db_ctx, OAKLEY_ENCRYPTION_ALGORITHM, ealg);
	    db_attr_add_values(db_ctx, OAKLEY_HASH_ALGORITHM, halg);
	    if (eklen)
		db_attr_add_values(db_ctx, OAKLEY_KEY_LENGTH, eklen);
	    db_attr_add_values(db_ctx, OAKLEY_AUTHENTICATION_METHOD, OAKLEY_RSA_SIG);
	    db_attr_add_values(db_ctx, OAKLEY_GROUP_DESCRIPTION, modp);
	}

	if (policy & POLICY_PSK)
	{
	    db_trans_add(db_ctx, KEY_IKE);
	    db_attr_add_values(db_ctx, OAKLEY_ENCRYPTION_ALGORITHM, ealg);
	    db_attr_add_values(db_ctx, OAKLEY_HASH_ALGORITHM, halg);
	    if (eklen)
		db_attr_add_values(db_ctx, OAKLEY_KEY_LENGTH, eklen);
	    db_attr_add_values(db_ctx, OAKLEY_AUTHENTICATION_METHOD, OAKLEY_PRESHARED_KEY);
	    db_attr_add_values(db_ctx, OAKLEY_GROUP_DESCRIPTION, modp);
	}

	if (policy & POLICY_XAUTH_RSASIG)
	{
	    db_trans_add(db_ctx, KEY_IKE);
	    db_attr_add_values(db_ctx, OAKLEY_ENCRYPTION_ALGORITHM, ealg);
	    db_attr_add_values(db_ctx, OAKLEY_HASH_ALGORITHM, halg);
	    if (eklen)
		db_attr_add_values(db_ctx, OAKLEY_KEY_LENGTH, eklen);
	    db_attr_add_values(db_ctx, OAKLEY_AUTHENTICATION_METHOD
		, is_xauth_server ? XAUTHRespRSA : XAUTHInitRSA);
	    db_attr_add_values(db_ctx, OAKLEY_GROUP_DESCRIPTION, modp);
	}

	if (policy & POLICY_XAUTH_PSK)
	{
	    db_trans_add(db_ctx, KEY_IKE);
	    db_attr_add_values(db_ctx, OAKLEY_ENCRYPTION_ALGORITHM, ealg);
	    db_attr_add_values(db_ctx, OAKLEY_HASH_ALGORITHM, halg);
	    if (eklen)
		db_attr_add_values(db_ctx, OAKLEY_KEY_LENGTH, eklen);
	    db_attr_add_values(db_ctx, OAKLEY_AUTHENTICATION_METHOD
		, is_xauth_server ? XAUTHRespPreShared : XAUTHInitPreShared);
	    db_attr_add_values(db_ctx, OAKLEY_GROUP_DESCRIPTION, modp);
	}
    }
fail:
    return db_ctx;
}

/*
 * Show registered IKE algorithms
 */
void
ike_alg_list(void)
{
    u_int i;
    struct ike_alg *a;

    whack_log(RC_COMMENT, " ");
    whack_log(RC_COMMENT, "List of registered IKE Encryption Algorithms:");
    whack_log(RC_COMMENT, " ");

    for (a = ike_alg_base[IKE_ALG_ENCRYPT]; a != NULL; a = a->algo_next)
    {
        struct encrypt_desc *desc = (struct encrypt_desc*)a;

	whack_log(RC_COMMENT, "#%-5d %s, blocksize: %d, keylen: %d-%d-%d"
	    , a->algo_id
	    , enum_name(&oakley_enc_names, a->algo_id)
	    , (int)desc->enc_blocksize*BITS_PER_BYTE
	    , desc->keyminlen
	    , desc->keydeflen
	    , desc->keymaxlen
	);
    }

    whack_log(RC_COMMENT, " ");
    whack_log(RC_COMMENT, "List of registered IKE Hash Algorithms:");
    whack_log(RC_COMMENT, " ");

    for (a = ike_alg_base[IKE_ALG_HASH]; a != NULL; a = a->algo_next)
    {
	whack_log(RC_COMMENT, "#%-5d %s, hashsize: %d"
	    , a->algo_id
	    , enum_name(&oakley_hash_names, a->algo_id)
	    , (int)((struct hash_desc *)a)->hash_digest_size*BITS_PER_BYTE
	);
    }

    whack_log(RC_COMMENT, " ");
    whack_log(RC_COMMENT, "List of registered IKE DH Groups:");
    whack_log(RC_COMMENT, " ");

    for (i = 0; i < elemsof(oakley_group); i++)
    {
	const struct oakley_group_desc *gdesc=oakley_group + i;

	whack_log(RC_COMMENT, "#%-5d %s, groupsize: %d"
	    , gdesc->group
	    , enum_name(&oakley_group_names, gdesc->group)
	    , (int)gdesc->bytes*BITS_PER_BYTE
	);
    }
}

/*  Show IKE algorithms for
 *    - this connection (result from ike= string)
 *    - newest SA
 */
void
ike_alg_show_connection(struct connection *c, const char *instance)
{
    char buf[256];
    struct state *st;

    if (c->alg_info_ike)
    {
	alg_info_snprint(buf, sizeof(buf)-1, (struct alg_info *)c->alg_info_ike);
	whack_log(RC_COMMENT
		, "\"%s\"%s:   IKE algorithms wanted: %s"
		, c->name
		, instance
		, buf
	);

	alg_info_snprint_ike(buf, sizeof(buf)-1, c->alg_info_ike);
	whack_log(RC_COMMENT
		, "\"%s\"%s:   IKE algorithms found:  %s"
		, c->name
		, instance
		, buf
	);
    }

    st = state_with_serialno(c->newest_isakmp_sa);
    if (st)
	whack_log(RC_COMMENT
		, "\"%s\"%s:   IKE algorithm newest: %s_%d-%s-%s"
		, c->name
		, instance
		, enum_show(&oakley_enc_names, st->st_oakley.encrypt)
		+7 /* strlen("OAKLEY_") */
		/* , st->st_oakley.encrypter->keydeflen */
		, st->st_oakley.enckeylen
		, enum_show(&oakley_hash_names, st->st_oakley.hash)
		+7 /* strlen("OAKLEY_") */
		, enum_show(&oakley_group_names, st->st_oakley.group->group)
		+13 /* strlen("OAKLEY_GROUP_") */
	);
}

/*
 * Apply a suite of testvectors to a hash algorithm
 */
static bool
ike_hash_test(const struct hash_desc *desc)
{
    bool hash_results = TRUE;
    bool hmac_results = TRUE;

    if (desc->hash_testvectors == NULL)
    {
	plog("  %s hash self-test not available", enum_name(&oakley_hash_names, desc->algo_id));
    }
    else
    {
	int i;

	for (i = 0; desc->hash_testvectors[i].msg_digest != NULL; i++)
	{
	    u_char digest[MAX_DIGEST_LEN];
	    bool result;

	    union hash_ctx ctx;

	    desc->hash_init(&ctx);
	    desc->hash_update(&ctx, desc->hash_testvectors[i].msg
				   ,desc->hash_testvectors[i].msg_size);
	    desc->hash_final(digest, &ctx);
	    result = memcmp(digest, desc->hash_testvectors[i].msg_digest
				  , desc->hash_digest_size) == 0;
	    DBG(DBG_CRYPT,
		DBG_log("  hash testvector %d: %s", i, result ? "ok":"failed")
	    )
	    hash_results &= result;
	}
	plog("  %s hash self-test %s", enum_name(&oakley_hash_names, desc->algo_id)
				, hash_results ? "passed":"failed");
    }

    if (desc->hmac_testvectors == NULL)
    {
	plog("  %s hmac self-test not available", enum_name(&oakley_hash_names, desc->algo_id));
    }
    else
    {
	int i;

	for (i = 0; desc->hmac_testvectors[i].hmac != NULL; i++)
	{
	    u_char digest[MAX_DIGEST_LEN];
	    bool result;

	    struct hmac_ctx ctx;

	    hmac_init(&ctx, desc, desc->hmac_testvectors[i].key
				, desc->hmac_testvectors[i].key_size);
	    hmac_update(&ctx, desc->hmac_testvectors[i].msg
			     ,desc->hmac_testvectors[i].msg_size);
	    hmac_final(digest, &ctx);
	    result = memcmp(digest, desc->hmac_testvectors[i].hmac
				  , desc->hash_digest_size) == 0;
	    DBG(DBG_CRYPT,
		DBG_log("  hmac testvector %d: %s", i, result ? "ok":"failed")
	    )
	    hmac_results &= result;
	}
	plog("  %s hmac self-test %s", enum_name(&oakley_hash_names, desc->algo_id)
				, hmac_results ? "passed":"failed");
    }
    return hash_results && hmac_results;
}

/*
 * Apply test vectors to registered encryption and hash algorithms
 */
bool
ike_alg_test(void)
{
    bool all_results = TRUE;
    struct ike_alg *a;

    plog("Testing registered IKE encryption algorithms:");

    for (a = ike_alg_base[IKE_ALG_ENCRYPT]; a != NULL; a = a->algo_next)
    {
	
        struct encrypt_desc *desc = (struct encrypt_desc*)a;

	plog("  %s self-test not available", enum_name(&oakley_enc_names, a->algo_id));
    }

    plog("Testing registered IKE hash algorithms:");

    for (a = ike_alg_base[IKE_ALG_HASH]; a != NULL; a = a->algo_next)
    {
        struct hash_desc *desc = (struct hash_desc*)a;

	all_results &= ike_hash_test(desc);
    }

    if (all_results)
	plog("All crypto self-tests passed");
    else
	plog("Some crypto self-tests failed");
    return all_results;
}

/*
 * ML: make F_STRICT logic consider enc,hash/auth,modp algorithms
 */
bool
ike_alg_ok_final(u_int ealg, u_int key_len, u_int aalg, u_int group
, struct alg_info_ike *alg_info_ike)
{
    /*
     * simple test to discard low key_len, will accept it only
     * if specified in "esp" string
     */
    bool ealg_insecure = (key_len < 128);

    if (ealg_insecure
    || (alg_info_ike && alg_info_ike->alg_info_flags & ALG_INFO_F_STRICT))
    {
	int i;
	struct ike_info *ike_info;

	if (alg_info_ike)
	{
	    ALG_INFO_IKE_FOREACH(alg_info_ike, ike_info, i)
	    {
		if (ike_info->ike_ealg == ealg
		&& (ike_info->ike_eklen == 0 || key_len == 0 || ike_info->ike_eklen == key_len)
		&& ike_info->ike_halg == aalg
		&& ike_info->ike_modp == group)
		{
		    if (ealg_insecure)
			loglog(RC_LOG_SERIOUS, "You should NOT use insecure IKE algorithms (%s)!"
				, enum_name(&oakley_enc_names, ealg));
		    return TRUE;
		}
	    }
	}
	plog("Oakley Transform [%s (%d), %s, %s] refused due to %s"
		, enum_name(&oakley_enc_names, ealg), key_len
		, enum_name(&oakley_hash_names, aalg)
		, enum_name(&oakley_group_names, group)
		, ealg_insecure ?
		    "insecure key_len and enc. alg. not listed in \"ike\" string" : "strict flag"
	);
	return FALSE;
    }
    return TRUE;
}