/
ipsec_doi.c
3584 lines (3181 loc) · 77.9 KB
/
ipsec_doi.c
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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* $KAME: ipsec_doi.c,v 1.114 2000/10/03 23:44:41 itojun Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* YIPS @(#)$Id: ipsec_doi.c,v 1.114 2000/10/03 23:44:41 itojun Exp $ */
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netkey/key_var.h>
#include <netinet/in.h>
#ifdef IPV6_INRIA_VERSION
#include <netinet/ipsec.h>
#else
#include <netinet6/ipsec.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <netdb.h>
#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
# include <sys/time.h>
# else
# include <time.h>
# endif
#endif
#include "var.h"
#include "vmbuf.h"
#include "misc.h"
#include "plog.h"
#include "debug.h"
#include "cfparse.h"
#include "isakmp_var.h"
#include "isakmp.h"
#include "ipsec_doi.h"
#include "oakley.h"
#include "remoteconf.h"
#include "localconf.h"
#include "sockmisc.h"
#include "handler.h"
#include "policy.h"
#include "algorithm.h"
#include "sainfo.h"
#include "proposal.h"
#include "crypto_openssl.h"
#include "strnames.h"
static vchar_t *get_ph1approval __P((struct ph1handle *, struct prop_pair **));
static struct isakmpsa *get_ph1approvalx __P((struct prop_pair *,
struct isakmpsa *));
static int t2isakmpsa __P((struct isakmp_pl_t *, struct isakmpsa *));
static int cmp_aproppair_i __P((struct prop_pair *, struct prop_pair *));
static struct prop_pair *get_ph2approval __P((struct ph2handle *,
struct prop_pair **));
static struct prop_pair *get_ph2approvalx __P((struct ph2handle *,
struct prop_pair *));
static void free_proppair0 __P((struct prop_pair *));
static int get_transform
__P((struct isakmp_pl_p *, struct prop_pair **, int *));
static vchar_t *get_sabyproppair __P((struct prop_pair *, struct ph1handle *));
static u_int32_t ipsecdoi_set_ld __P((vchar_t *));
static int check_doi __P((u_int32_t));
static int check_situation __P((u_int32_t));
static int check_prot_main __P((int));
static int check_prot_quick __P((int));
static int (*check_protocol[]) __P((int)) = {
check_prot_main, /* IPSECDOI_TYPE_PH1 */
check_prot_quick, /* IPSECDOI_TYPE_PH2 */
};
static int check_spi_size __P((int, int));
static int check_trns_isakmp __P((int));
static int check_trns_ah __P((int));
static int check_trns_esp __P((int));
static int check_trns_ipcomp __P((int));
static int (*check_transform[]) __P((int)) = {
0,
check_trns_isakmp, /* IPSECDOI_PROTO_ISAKMP */
check_trns_ah, /* IPSECDOI_PROTO_IPSEC_AH */
check_trns_esp, /* IPSECDOI_PROTO_IPSEC_ESP */
check_trns_ipcomp, /* IPSECDOI_PROTO_IPCOMP */
};
static int check_attr_isakmp __P((struct isakmp_pl_t *));
static int check_attr_ah __P((struct isakmp_pl_t *));
static int check_attr_esp __P((struct isakmp_pl_t *));
static int check_attr_ipsec __P((int, struct isakmp_pl_t *));
static int check_attr_ipcomp __P((struct isakmp_pl_t *));
static int (*check_attributes[]) __P((struct isakmp_pl_t *)) = {
0,
check_attr_isakmp, /* IPSECDOI_PROTO_ISAKMP */
check_attr_ah, /* IPSECDOI_PROTO_IPSEC_AH */
check_attr_esp, /* IPSECDOI_PROTO_IPSEC_ESP */
check_attr_ipcomp, /* IPSECDOI_PROTO_IPCOMP */
};
static int setph1prop __P((struct isakmpsa *, caddr_t));
static int setph1trns __P((struct isakmpsa *, caddr_t));
static int setph1attr __P((struct isakmpsa *, caddr_t));
static vchar_t *setph2proposal0 __P((const struct ph2handle *,
const struct saprop *, const struct saproto *));
/*%%%*/
/*
* check phase 1 SA payload.
* make new SA payload to be replyed not including general header.
* the pointer to one of isakmpsa in proposal is set into iph1->approval.
* OUT:
* positive: the pointer to new buffer of SA payload.
* network byte order.
* NULL : error occurd.
*/
int
ipsecdoi_checkph1proposal(sa, iph1)
vchar_t *sa;
struct ph1handle *iph1;
{
vchar_t *newsa; /* new SA payload approved. */
struct prop_pair **pair;
/* get proposal pair */
pair = get_proppair(sa, IPSECDOI_TYPE_PH1);
if (pair == NULL)
return -1;
/* check and get one SA for use */
newsa = get_ph1approval(iph1, pair);
free_proppair(pair);
if (newsa == NULL)
return -1;
iph1->sa_ret = newsa;
return 0;
}
/*
* acceptable check for remote configuration.
* return a new SA payload to be reply to peer.
*/
static vchar_t *
get_ph1approval(iph1, pair)
struct ph1handle *iph1;
struct prop_pair **pair;
{
vchar_t *newsa;
struct isakmpsa *sa;
struct prop_pair *s, *p;
int prophlen;
int i;
iph1->approval = NULL;
for (i = 0; i < MAXPROPPAIRLEN; i++) {
if (pair[i] == NULL)
continue;
for (s = pair[i]; s; s = s->next) {
prophlen = sizeof(struct isakmp_pl_p)
+ s->prop->spi_size;
/* compare proposal and select one */
for (p = s; p; p = p->tnext) {
sa = get_ph1approvalx(p, iph1->rmconf->proposal);
if (sa != NULL)
goto found;
}
}
}
return NULL;
found:
/* check DH group settings */
if (sa->dhgrp) {
if (sa->dhgrp->prime && sa->dhgrp->gen1) {
/* it's ok */
goto saok;
}
plog(logp, LOCATION, NULL,
"invalid DH parameter found, use default.\n");
oakley_dhgrp_free(sa->dhgrp);
}
sa->dhgrp = CALLOC(sizeof(struct dhgroup), struct dhgroup *);
if (!sa->dhgrp) {
plog(logp, LOCATION, NULL,
"failed to get buffer\n");
return NULL;
}
switch (sa->dh_group) {
case OAKLEY_ATTR_GRP_DESC_MODP768:
case OAKLEY_ATTR_GRP_DESC_MODP1024:
case OAKLEY_ATTR_GRP_DESC_MODP1536:
if (sa->dh_group > ARRAYLEN(dhgroup)
|| dhgroup[sa->dh_group].type == 0) {
plog(logp, LOCATION, NULL,
"invalid DH parameter grp=%d.\n",
sa->dh_group);
free(sa->dhgrp);
sa->dhgrp = NULL;
return NULL;
break;
}
/* set defined dh vlaues */
memcpy(sa->dhgrp, &dhgroup[sa->dh_group],
sizeof(dhgroup[sa->dh_group]));
sa->dhgrp->prime = vdup(dhgroup[sa->dh_group].prime);
break;
default:
if (!sa->dhgrp->type || !sa->dhgrp->prime || !sa->dhgrp->gen1) {
break;
}
}
saok:
iph1->approval = sa;
newsa = get_sabyproppair(p, iph1);
if (newsa == NULL)
iph1->approval = NULL;
return newsa;
}
/* compare proposal and select one */
static struct isakmpsa *
get_ph1approvalx(p, proposal)
struct prop_pair *p;
struct isakmpsa *proposal;
{
#ifdef YIPS_DEBUG
struct isakmp_pl_p *prop = p->prop;
#endif
struct isakmp_pl_t *trns = p->trns;
struct isakmpsa sa, *s;
YIPSDEBUG(DEBUG_SA,
plog(logp, LOCATION, NULL,
"prop#=%d, prot-id=%s, spi-size=%d, #trns=%d\n",
prop->p_no, s_ipsecdoi_proto(prop->proto_id),
prop->spi_size, prop->num_t));
YIPSDEBUG(DEBUG_SA,
plog(logp, LOCATION, NULL,
"trns#=%d, trns-id=%s\n",
trns->t_no,
s_ipsecdoi_trns(prop->proto_id, trns->t_id)));
memset(&sa, 0, sizeof(sa));
if (t2isakmpsa(trns, &sa) < 0)
return NULL;
for (s = proposal; s != NULL; s = s->next) {
YIPSDEBUG(DEBUG_SA,
plog(logp, LOCATION, NULL, "Compared: DB:Peer\n");
plog(logp, LOCATION, NULL, "(lifetime = %ld:%ld)\n",
s->lifetime, sa.lifetime);
plog(logp, LOCATION, NULL, "(lifebyte = %ld:%ld)\n",
s->lifebyte, sa.lifebyte);
plog(logp, LOCATION, NULL, "enctype = %s:%s\n",
s_oakley_attr_v(OAKLEY_ATTR_ENC_ALG,
s->enctype),
s_oakley_attr_v(OAKLEY_ATTR_ENC_ALG,
sa.enctype));
plog(logp, LOCATION, NULL, "(encklen = %d:%d)\n",
s->encklen, sa.encklen);
plog(logp, LOCATION, NULL, "hashtype = %s:%s\n",
s_oakley_attr_v(OAKLEY_ATTR_HASH_ALG,
s->hashtype),
s_oakley_attr_v(OAKLEY_ATTR_HASH_ALG,
sa.hashtype));
plog(logp, LOCATION, NULL, "authmethod = %s:%s\n",
s_oakley_attr_v(OAKLEY_ATTR_AUTH_METHOD,
s->authmethod),
s_oakley_attr_v(OAKLEY_ATTR_AUTH_METHOD,
sa.authmethod));
plog(logp, LOCATION, NULL, "dh_group = %s:%s\n",
s_oakley_attr_v(OAKLEY_ATTR_GRP_DESC,
s->dh_group),
s_oakley_attr_v(OAKLEY_ATTR_GRP_DESC,
sa.dh_group));
);
#if 0
/* XXX to be considered */
if (sa.lifetime > s->lifetime) ;
if (sa.lifebyte > s->lifebyte) ;
if (sa.encklen >= s->encklen) ;
#endif
if (sa.enctype == s->enctype
&& sa.authmethod == s->authmethod
&& sa.hashtype == s->hashtype
&& sa.dh_group == s->dh_group)
break;
}
YIPSDEBUG(DEBUG_SA,
if (s == NULL) {
plog(logp, LOCATION, NULL,
"unacceptable proposal.\n");
} else {
plog(logp, LOCATION, NULL,
"acceptable proposal found.\n");
});
if (sa.dhgrp != NULL)
oakley_dhgrp_free(sa.dhgrp);
return s;
}
/*
* get ISAKMP data attributes
*/
static int
t2isakmpsa(trns, sa)
struct isakmp_pl_t *trns;
struct isakmpsa *sa;
{
struct isakmp_data *d, *prev;
int flag, type;
int error = -1;
int life_t;
int keylen = 0;
vchar_t *val = NULL;
int len, tlen;
u_char *p;
tlen = ntohs(trns->h.len) - sizeof(*trns);
prev = (struct isakmp_data *)NULL;
d = (struct isakmp_data *)(trns + 1);
/* default */
life_t = OAKLEY_ATTR_SA_LD_TYPE_DEFAULT;
sa->lifetime = OAKLEY_ATTR_SA_LD_SEC_DEFAULT;
sa->lifebyte = 0;
sa->dhgrp = CALLOC(sizeof(struct dhgroup), struct dhgroup *);
if (!sa->dhgrp)
goto err;
while (tlen > 0) {
type = ntohs(d->type) & ~ISAKMP_GEN_MASK;
flag = ntohs(d->type) & ISAKMP_GEN_MASK;
YIPSDEBUG(DEBUG_DSA,
plog(logp, LOCATION, NULL,
"type=%s, flag=0x%04x, lorv=%s\n",
s_oakley_attr(type), flag,
s_oakley_attr_v(type, ntohs(d->lorv))));
/* get variable-sized item */
switch (type) {
case OAKLEY_ATTR_GRP_PI:
case OAKLEY_ATTR_GRP_GEN_ONE:
case OAKLEY_ATTR_GRP_GEN_TWO:
case OAKLEY_ATTR_GRP_CURVE_A:
case OAKLEY_ATTR_GRP_CURVE_B:
case OAKLEY_ATTR_SA_LD:
case OAKLEY_ATTR_GRP_ORDER:
if (flag) { /*TV*/
len = 2;
p = (u_char *)&d->lorv;
} else { /*TLV*/
len = ntohs(d->lorv);
p = (u_char *)(d + 1);
}
val = vmalloc(len);
if (!val)
return -1;
memcpy(val->v, p, len);
break;
default:
break;
}
switch (type) {
case OAKLEY_ATTR_ENC_ALG:
sa->enctype = (u_int8_t)ntohs(d->lorv);
break;
case OAKLEY_ATTR_HASH_ALG:
sa->hashtype = (u_int8_t)ntohs(d->lorv);
break;
case OAKLEY_ATTR_AUTH_METHOD:
sa->authmethod = (u_int8_t)ntohs(d->lorv);
break;
case OAKLEY_ATTR_GRP_DESC:
sa->dh_group = (u_int8_t)ntohs(d->lorv);
break;
case OAKLEY_ATTR_GRP_TYPE:
{
int type = (int)ntohs(d->lorv);
if (type == OAKLEY_ATTR_GRP_TYPE_MODP)
sa->dhgrp->type = type;
else
return -1;
break;
}
case OAKLEY_ATTR_GRP_PI:
sa->dhgrp->prime = val;
break;
case OAKLEY_ATTR_GRP_GEN_ONE:
vfree(val);
if (!flag)
sa->dhgrp->gen1 = ntohs(d->lorv);
else {
int len = ntohs(d->lorv);
sa->dhgrp->gen1 = 0;
if (len > 4)
return -1;
memcpy(&sa->dhgrp->gen1, d + 1, len);
sa->dhgrp->gen1 = ntohl(sa->dhgrp->gen1);
}
break;
case OAKLEY_ATTR_GRP_GEN_TWO:
vfree(val);
if (!flag)
sa->dhgrp->gen2 = ntohs(d->lorv);
else {
int len = ntohs(d->lorv);
sa->dhgrp->gen2 = 0;
if (len > 4)
return -1;
memcpy(&sa->dhgrp->gen2, d + 1, len);
sa->dhgrp->gen2 = ntohl(sa->dhgrp->gen2);
}
break;
case OAKLEY_ATTR_GRP_CURVE_A:
sa->dhgrp->curve_a = val;
break;
case OAKLEY_ATTR_GRP_CURVE_B:
sa->dhgrp->curve_b = val;
break;
case OAKLEY_ATTR_SA_LD_TYPE:
{
int type = (int)ntohs(d->lorv);
switch (type) {
case OAKLEY_ATTR_SA_LD_TYPE_SEC:
case OAKLEY_ATTR_SA_LD_TYPE_KB:
life_t = type;
break;
default:
life_t = OAKLEY_ATTR_SA_LD_TYPE_DEFAULT;
break;
}
break;
}
case OAKLEY_ATTR_SA_LD:
if (!prev
|| (ntohs(prev->type) & ~ISAKMP_GEN_MASK) !=
OAKLEY_ATTR_SA_LD_TYPE) {
plog(logp, LOCATION, NULL,
"life duration must follow ltype\n");
break;
}
switch (life_t) {
case IPSECDOI_ATTR_SA_LD_TYPE_SEC:
sa->lifetime = ipsecdoi_set_ld(val);
vfree(val);
if (sa->lifetime == 0) {
plog(logp, LOCATION, NULL,
"invalid life duration.\n");
goto err;
}
break;
case IPSECDOI_ATTR_SA_LD_TYPE_KB:
sa->lifebyte = ipsecdoi_set_ld(val);
vfree(val);
if (sa->lifetime == 0) {
plog(logp, LOCATION, NULL,
"invalid life duration.\n");
goto err;
}
break;
default:
vfree(val);
plog(logp, LOCATION, NULL,
"invalid life type: %d\n", life_t);
goto err;
}
break;
case OAKLEY_ATTR_KEY_LEN:
{
int len = ntohs(d->lorv);
if (len % 8 != 0) {
plog(logp, LOCATION, NULL,
"keylen %d: not multiple of 8\n",
len);
goto err;
}
sa->encklen = (u_int8_t)len;
keylen++;
break;
}
case OAKLEY_ATTR_PRF:
case OAKLEY_ATTR_FIELD_SIZE:
/* unsupported */
break;
case OAKLEY_ATTR_GRP_ORDER:
sa->dhgrp->order = val;
break;
default:
break;
}
prev = d;
if (flag) {
tlen -= sizeof(*d);
d = (struct isakmp_data *)((char *)d + sizeof(*d));
} else {
tlen -= (sizeof(*d) + ntohs(d->lorv));
d = (struct isakmp_data *)((char *)d + sizeof(*d) + ntohs(d->lorv));
}
}
/* key length must not be specified on some algorithms */
if (keylen) {
switch (sa->enctype) {
case OAKLEY_ATTR_ENC_ALG_DES:
#ifdef HAVE_OPENSSL_IDEA_H
case OAKLEY_ATTR_ENC_ALG_IDEA:
#endif
case OAKLEY_ATTR_ENC_ALG_3DES:
plog(logp, LOCATION, NULL,
"keylen must not be specified for encryption algorithm %d\n",
sa->enctype);
goto err;
case OAKLEY_ATTR_ENC_ALG_BLOWFISH:
#ifdef HAVE_OPENSSL_RC5_H
case OAKLEY_ATTR_ENC_ALG_RC5:
#endif
case OAKLEY_ATTR_ENC_ALG_CAST:
break;
default:
plog(logp, LOCATION, NULL,
"unknown encryption algorithm %d\n",
sa->enctype);
goto err;
}
}
return 0;
err:
return error;
}
/*%%%*/
/*
* check phase 2 SA payload and select single proposal.
* make new SA payload to be replyed not including general header.
* This function is called by responder only.
* OUT:
* 0: succeed.
* -1: error occured.
*/
int
ipsecdoi_selectph2proposal(iph2)
struct ph2handle *iph2;
{
struct prop_pair **pair;
struct prop_pair *ret;
/* get proposal pair */
pair = get_proppair(iph2->sa, IPSECDOI_TYPE_PH2);
if (pair == NULL)
return -1;
/* check and select a proposal. */
ret = get_ph2approval(iph2, pair);
free_proppair(pair);
if (ret == NULL)
return -1;
/* make a SA to be replayed. */
/* SPI must be updated later. */
iph2->sa_ret = get_sabyproppair(ret, iph2->ph1);
free_proppair0(ret);
if (iph2->sa_ret == NULL)
return -1;
return 0;
}
/*
* check phase 2 SA payload returned from responder.
* This function is called by initiator only.
* OUT:
* 0: valid.
* -1: invalid.
*/
int
ipsecdoi_checkph2proposal(iph2)
struct ph2handle *iph2;
{
struct prop_pair **rpair = NULL, **spair = NULL;
struct prop_pair *p;
int i, n, num;
int error = -1;
/* get proposal pair of SA sent. */
spair = get_proppair(iph2->sa, IPSECDOI_TYPE_PH2);
if (spair == NULL) {
plog(logp, LOCATION, NULL,
"failed to get prop pair.\n");
goto end;
}
/* XXX should check the number of transform */
/* get proposal pair of SA replyed */
rpair = get_proppair(iph2->sa_ret, IPSECDOI_TYPE_PH2);
if (rpair == NULL) {
plog(logp, LOCATION, NULL,
"failed to get prop pair.\n");
goto end;
}
/* check proposal is only one ? */
n = 0;
num = 0;
for (i = 0; i < MAXPROPPAIRLEN; i++) {
if (rpair[i]) {
n = i;
num++;
}
}
if (num == 0) {
YIPSDEBUG(DEBUG_SA,
plog(logp, LOCATION, NULL,
"ERROR: no proposal received.\n"));
goto end;
}
if (num != 1) {
YIPSDEBUG(DEBUG_SA,
plog(logp, LOCATION, NULL,
"ERROR: some proposals received.\n"));
goto end;
}
if (spair[n] == NULL) {
YIPSDEBUG(DEBUG_SA,
plog(logp, LOCATION, NULL,
"ERROR: invalid proposal number received: %d.\n",
i));
}
if (rpair[n]->tnext != NULL) {
YIPSDEBUG(DEBUG_SA,
plog(logp, LOCATION, NULL,
"ERROR: multi transforms replyed.\n"));
goto end;
}
if (cmp_aproppair_i(rpair[n], spair[n])) {
YIPSDEBUG(DEBUG_SA,
plog(logp, LOCATION, NULL,
"ERROR: proposal mismathed.\n"));
goto end;
}
/*
* check and select a proposal.
* ensure that there is no modification of the proposal by
* cmp_aproppair_i()
*/
p = get_ph2approval(iph2, rpair);
if (p == NULL)
goto end;
/* make a SA to be replayed. */
iph2->sa_ret = get_sabyproppair(p, iph2->ph1);
free_proppair0(p);
if (iph2->sa_ret == NULL)
goto end;
error = 0;
end:
if (rpair)
free_proppair(rpair);
if (spair)
free_proppair(spair);
return error;
}
/*
* compare two prop_pair which is assumed to have same proposal number.
* the case of bundle or single SA, NOT multi transforms.
* a: a proposal that is multi protocols and single transform, usually replyed.
* b: a proposal that is multi protocols and multi transform, usually sent.
* NOTE: this function is for initiator.
* OUT
* 0: equal
* 1: not equal
* XXX cannot understand the comment!
*/
static int
cmp_aproppair_i(a, b)
struct prop_pair *a, *b;
{
struct prop_pair *p, *q, *r;
int len;
for (p = a, q = b; p && q; p = p->next, q = q->next) {
for (r = q; r; r = r->tnext) {
/* compare trns */
if (p->trns->t_no == r->trns->t_no)
break;
}
if (!r) {
/* no suitable transform found */
plog(logp, LOCATION, NULL,
"ERROR: no suitable transform found.\n");
return -1;
}
/* compare prop */
if (p->prop->p_no != r->prop->p_no) {
YIPSDEBUG(DEBUG_NOTIFY,
plog(logp, LOCATION, NULL,
"WARNING: proposal #%d mismatched, "
"expected #%d.\n",
r->prop->p_no, p->prop->p_no));
/*FALLTHROUGH*/
}
if (p->prop->proto_id != r->prop->proto_id) {
plog(logp, LOCATION, NULL,
"ERROR: proto_id mismathed: my:%d peer:%d\n",
r->prop->proto_id, p->prop->proto_id);
return -1;
}
if (p->prop->proto_id != r->prop->proto_id) {
plog(logp, LOCATION, NULL,
"ERROR: invalid spi size: %d.\n",
p->prop->proto_id);
return -1;
}
/* check #of transforms */
if (p->prop->num_t != 1) {
YIPSDEBUG(DEBUG_NOTIFY,
plog(logp, LOCATION, NULL,
"NOTICE: #of transform is %d, "
"but expected 1.\n", p->prop->num_t));
/*FALLTHROUGH*/
}
if (p->trns->t_id != r->trns->t_id) {
YIPSDEBUG(DEBUG_NOTIFY,
plog(logp, LOCATION, NULL,
"WARNING: transform number "
"has been modified.\n"));
/*FALLTHROUGH*/
}
if (p->trns->reserved != r->trns->reserved) {
YIPSDEBUG(DEBUG_NOTIFY,
plog(logp, LOCATION, NULL,
"WARNING: invalid reserved field.\n"));
/*FALLTHROUGH*/
}
/* compare attribute */
len = ntohs(r->trns->h.len) - sizeof(*p->trns);
if (memcmp(p->trns + 1, r->trns + 1, len) != 0) {
YIPSDEBUG(DEBUG_NOTIFY,
plog(logp, LOCATION, NULL,
"WARNING: attribute has been modified.\n"));
/*FALLTHROUGH*/
}
}
if ((p && !q) || (!p && q)) {
/* # of protocols mismatched */
plog(logp, LOCATION, NULL,
"ERROR: #of protocols mismatched.\n");
return -1;
}
return 0;
}
/*
* acceptable check for policy configuration.
* return a new SA payload to be reply to peer.
*/
static struct prop_pair *
get_ph2approval(iph2, pair)
struct ph2handle *iph2;
struct prop_pair **pair;
{
struct prop_pair *ret;
int i;
iph2->approval = NULL;
YIPSDEBUG(DEBUG_SA, plog(logp, LOCATION, NULL,
"begin compare proposals.\n"));
for (i = 0; i < MAXPROPPAIRLEN; i++) {
if (pair[i] == NULL)
continue;
YIPSDEBUG(DEBUG_SA,
plog(logp, LOCATION, NULL,
"pair[%d]: %p\n", i, pair[i]);
print_proppair(pair[i]););
/* compare proposal and select one */
ret = get_ph2approvalx(iph2, pair[i]);
if (ret != NULL) {
/* found */
return ret;
}
}
plog(logp, LOCATION, NULL, "no suitable policy found.\n");
return NULL;
}
/*
* compare my proposal and peers just one proposal.
* set a approval.
*/
static struct prop_pair *
get_ph2approvalx(iph2, pp)
struct ph2handle *iph2;
struct prop_pair *pp;
{
struct prop_pair *ret = NULL;
struct saprop *pr0, *pr = NULL;
struct saprop *q1, *q2;
pr0 = aproppair2saprop(pp);
if (pr0 == NULL)
return NULL;
for (q1 = pr0; q1; q1 = q1->next) {
for (q2 = iph2->proposal; q2; q2 = q2->next) {
YIPSDEBUG(DEBUG_DSA,
plog(logp, LOCATION, NULL,
"peer's single bundle:\n");
printsaprop0(q1););
YIPSDEBUG(DEBUG_DSA,
plog(logp, LOCATION, NULL,
"my single bundle:\n");
printsaprop0(q2););
pr = cmpsaprop_alloc(iph2->ph1, q1, q2, iph2->side);
if (pr != NULL)
goto found;
YIPSDEBUG(DEBUG_DSA,
plog(logp, LOCATION, NULL, "not matched\n"););
}
}
/* no proposal matching */
err:
flushsaprop(pr0);
return NULL;
found:
YIPSDEBUG(DEBUG_DSA, plog(logp, LOCATION, NULL, "matched\n"););
iph2->approval = pr;
{
struct saproto *sp;
struct prop_pair *p, *n, *x;
ret = NULL;
for (p = pp; p; p = p->next) {
/*
* find a proposal with matching proto_id.
* we have analyzed validity already, in cmpsaprop_alloc().
*/
for (sp = pr->head; sp; sp = sp->next) {
if (sp->proto_id == p->prop->proto_id)
break;
}
if (!sp)
goto err;
if (sp->head->next)
goto err; /* XXX */
for (x = p; x; x = x->tnext)
if (sp->head->trns_no == x->trns->t_no)
break;
if (!x)
goto err; /* XXX */
n = CALLOC(sizeof(struct prop_pair), struct prop_pair *);
if (!n) {
plog(logp, LOCATION, NULL,
"failed to get buffer.\n");
goto err;
}
n->prop = x->prop;
n->trns = x->trns;
/* need to preserve the order */
for (x = ret; x && x->next; x = x->next)
;
if (x && x->prop == n->prop) {
for (/*nothing*/; x && x->tnext; x = x->tnext)
;
x->tnext = n;
} else {
if (x)
x->next = n;
else {
ret = n;
}
}
/* #of transforms should be updated ? */
}
}
return ret;
}
void
free_proppair(pair)
struct prop_pair **pair;
{
int i;
for (i = 0; i < MAXPROPPAIRLEN; i++) {
free_proppair0(pair[i]);
pair[i] = NULL;
}
free(pair);
}
static void
free_proppair0(pair)
struct prop_pair *pair;
{
struct prop_pair *p, *q;
for (p = pair; p; p = q) {
q = p->next;
free(p);
}