1 /* $Id: ipsec_doi.c,v 1.26.2.16 2006/02/02 14:37:17 vanhu Exp $ */
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/socket.h>
37 #include <arpa/inet.h>
39 #include <netinet/in.h>
41 #ifndef HAVE_NETINET6_IPSEC
42 #include <netinet/ipsec.h>
44 #include <netinet6/ipsec.h>
52 #if TIME_WITH_SYS_TIME
53 # include <sys/time.h>
57 # include <sys/time.h>
69 #include "cfparse_proto.h"
70 #include "isakmp_var.h"
72 #include "ipsec_doi.h"
74 #include "remoteconf.h"
75 #include "localconf.h"
79 #include "algorithm.h"
82 #include "crypto_openssl.h"
83 #include "crypto_cssm.h"
88 #include "nattraversal.h"
92 static int switch_authmethod(int);
95 int verbose_proposal_check
= 1;
97 static vchar_t
*get_ph1approval (phase1_handle_t
*, struct prop_pair
**);
98 void print_ph1mismatched (struct prop_pair
*, struct isakmpsa
*);
99 static int t2isakmpsa (struct isakmp_pl_t
*, struct isakmpsa
*);
100 static int cmp_aproppair_i (struct prop_pair
*, struct prop_pair
*);
101 static struct prop_pair
*get_ph2approval (phase2_handle_t
*,
102 struct prop_pair
**);
103 static struct prop_pair
*get_ph2approvalx (phase2_handle_t
*,
105 static void free_proppair0 (struct prop_pair
*);
107 static int get_transform (struct isakmp_pl_p
*, struct prop_pair
**, int *);
108 static u_int32_t
ipsecdoi_set_ld (vchar_t
*);
110 static int check_doi (u_int32_t
);
111 static int check_situation (u_int32_t
);
113 static int check_prot_main (int);
114 static int check_prot_quick (int);
115 static int (*check_protocol
[]) (int) = {
116 check_prot_main
, /* IPSECDOI_TYPE_PH1 */
117 check_prot_quick
, /* IPSECDOI_TYPE_PH2 */
118 NULL
, /* IPSECDOI_TYPE_IKEV2_PH1 */
119 NULL
, /* IPSECDOI_TYPE_IKEV2_PH2 */
122 int check_spi_size (int, int);
124 static int check_trns_isakmp (int);
125 static int check_trns_ah (int);
126 static int check_trns_esp (int);
127 static int check_trns_ipcomp (int);
128 static int (*check_transform
[]) (int) = {
130 check_trns_isakmp
, /* IPSECDOI_PROTO_ISAKMP */
131 check_trns_ah
, /* IPSECDOI_PROTO_IPSEC_AH */
132 check_trns_esp
, /* IPSECDOI_PROTO_IPSEC_ESP */
133 check_trns_ipcomp
, /* IPSECDOI_PROTO_IPCOMP */
136 static int check_attr_isakmp (struct isakmp_pl_t
*);
137 static int check_attr_ah (struct isakmp_pl_t
*);
138 static int check_attr_esp (struct isakmp_pl_t
*);
139 static int check_attr_ipsec (int, struct isakmp_pl_t
*);
140 static int check_attr_ipcomp (struct isakmp_pl_t
*);
141 static int (*check_attributes
[]) (struct isakmp_pl_t
*) = {
143 check_attr_isakmp
, /* IPSECDOI_PROTO_ISAKMP */
144 check_attr_ah
, /* IPSECDOI_PROTO_IPSEC_AH */
145 check_attr_esp
, /* IPSECDOI_PROTO_IPSEC_ESP */
146 check_attr_ipcomp
, /* IPSECDOI_PROTO_IPCOMP */
149 int setph1prop (phase1_handle_t
*, caddr_t
);
150 static int setph1trns (struct isakmpsa
*, caddr_t
);
151 static int setph1attr (struct isakmpsa
*, caddr_t
);
152 static vchar_t
*setph2proposal0 (const phase2_handle_t
*,
153 const struct saprop
*, const struct saproto
*);
155 static vchar_t
*getidval (int, vchar_t
*);
160 * check phase 1 SA payload.
161 * make new SA payload to be replyed not including general header.
162 * the pointer to one of isakmpsa in proposal is set into iph1->approval.
164 * positive: the pointer to new buffer of SA payload.
165 * network byte order.
166 * NULL : error occurd.
169 ipsecdoi_checkph1proposal(sa
, iph1
)
171 phase1_handle_t
*iph1
;
173 vchar_t
*newsa
; /* new SA payload approved. */
174 struct prop_pair
**pair
;
176 /* get proposal pair */
177 pair
= get_proppair(sa
, IPSECDOI_TYPE_PH1
);
181 /* check and get one SA for use */
182 newsa
= get_ph1approval(iph1
, pair
);
189 iph1
->sa_ret
= newsa
;
195 * acceptable check for remote configuration.
196 * return a new SA payload to be reply to peer.
199 get_ph1approval(iph1
, pair
)
200 phase1_handle_t
*iph1
;
201 struct prop_pair
**pair
;
204 struct isakmpsa
*sa
, tsa
;
205 struct prop_pair
*s
, *p
;
209 if (iph1
->approval
) {
210 delisakmpsa(iph1
->approval
);
211 iph1
->approval
= NULL
;
214 for (i
= 0; i
< MAXPROPPAIRLEN
; i
++) {
217 for (s
= pair
[i
]; s
; s
= s
->next
) {
219 sizeof(struct isakmp_pl_p
) + s
->prop
->spi_size
;
221 /* compare proposal and select one */
222 for (p
= s
; p
; p
= p
->tnext
) {
223 if ((sa
= get_ph1approvalx(p
,
224 iph1
->rmconf
->proposal
, &tsa
,
225 iph1
->rmconf
->pcheck_level
)) != NULL
)
232 * if there is no suitable proposal, racoon complains about all of
233 * mismatched items in those proposal.
235 if (verbose_proposal_check
) {
236 for (i
= 0; i
< MAXPROPPAIRLEN
; i
++) {
239 for (s
= pair
[i
]; s
; s
= s
->next
) {
240 prophlen
= sizeof(struct isakmp_pl_p
)
242 for (p
= s
; p
; p
= p
->tnext
) {
243 print_ph1mismatched(p
,
244 iph1
->rmconf
->proposal
);
249 plog(ASL_LEVEL_ERR
, "no suitable proposal found.\n");
254 plog(ASL_LEVEL_DEBUG
, "an acceptable proposal found.\n");
256 /* check DH group settings */
258 if (sa
->dhgrp
->prime
&& sa
->dhgrp
->gen1
) {
262 plog(ASL_LEVEL_WARNING
,
263 "invalid DH parameter found, use default.\n");
264 oakley_dhgrp_free(sa
->dhgrp
);
268 if (oakley_setdhgroup(sa
->dh_group
, &sa
->dhgrp
) == -1) {
277 plog(ASL_LEVEL_DEBUG
, "agreed on %s auth.\n",
278 s_oakley_attr_method(iph1
->approval
->authmethod
));
281 newsa
= get_sabyproppair(p
, iph1
);
283 delisakmpsa(iph1
->approval
);
284 iph1
->approval
= NULL
;
291 * compare peer's single proposal and all of my proposal.
292 * and select one if suiatable.
293 * p : one of peer's proposal.
294 * proposal: my proposals.
297 get_ph1approvalx(p
, proposal
, sap
, check_level
)
299 struct isakmpsa
*proposal
, *sap
;
302 struct isakmp_pl_p
*prop
= p
->prop
;
303 struct isakmp_pl_t
*trns
= p
->trns
;
304 struct isakmpsa sa
, *s
, *tsap
;
308 plog(ASL_LEVEL_DEBUG
,
309 "prop#=%d, prot-id=%s, spi-size=%d, #trns=%d\n",
310 prop
->p_no
, s_ipsecdoi_proto(prop
->proto_id
),
311 prop
->spi_size
, prop
->num_t
);
313 plog(ASL_LEVEL_DEBUG
,
314 "trns#=%d, trns-id=%s\n",
316 s_ipsecdoi_trns(prop
->proto_id
, trns
->t_id
));
318 tsap
= sap
!= NULL
? sap
: &sa
;
320 memset(tsap
, 0, sizeof(*tsap
));
321 if (t2isakmpsa(trns
, tsap
) < 0)
323 for (s
= proposal
; s
!= NULL
; s
= s
->next
) {
325 authmethod
= switch_authmethod(s
->authmethod
);
326 tsap_authmethod
= switch_authmethod(tsap
->authmethod
);
328 authmethod
= s
->authmethod
;
329 tsap_authmethod
= tsap
->authmethod
;
331 plog(ASL_LEVEL_DEBUG
, "Compared: DB:Peer\n");
332 plog(ASL_LEVEL_DEBUG
, "(version = %d:%d)\n",
333 s
->version
, tsap
->version
);
334 plog(ASL_LEVEL_DEBUG
, "(lifetime = %ld:%ld)\n",
335 (long)s
->lifetime
, (long)tsap
->lifetime
);
336 plog(ASL_LEVEL_DEBUG
, "(lifebyte = %zu:%zu)\n",
337 s
->lifebyte
, tsap
->lifebyte
);
338 plog(ASL_LEVEL_DEBUG
, "enctype = %s:%s\n",
339 s_oakley_attr_v(OAKLEY_ATTR_ENC_ALG
,
341 s_oakley_attr_v(OAKLEY_ATTR_ENC_ALG
,
343 plog(ASL_LEVEL_DEBUG
, "(encklen = %d:%d)\n",
344 s
->encklen
, tsap
->encklen
);
345 plog(ASL_LEVEL_DEBUG
, "hashtype = %s:%s\n",
346 s_oakley_attr_v(OAKLEY_ATTR_HASH_ALG
,
348 s_oakley_attr_v(OAKLEY_ATTR_HASH_ALG
,
350 plog(ASL_LEVEL_DEBUG
, "authmethod = %s:%s\n",
351 s_oakley_attr_v(OAKLEY_ATTR_AUTH_METHOD
,
353 s_oakley_attr_v(OAKLEY_ATTR_AUTH_METHOD
,
355 plog(ASL_LEVEL_DEBUG
, "dh_group = %s:%s\n",
356 s_oakley_attr_v(OAKLEY_ATTR_GRP_DESC
,
358 s_oakley_attr_v(OAKLEY_ATTR_GRP_DESC
,
361 /* XXX to be considered ? */
362 if (tsap
->lifebyte
> s
->lifebyte
) ;
365 * if responder side and peer's key length in proposal
366 * is bigger than mine, it might be accepted.
368 if(tsap
->enctype
== s
->enctype
&&
369 (tsap
->authmethod
== authmethod
|| tsap_authmethod
== authmethod
) &&
370 tsap
->hashtype
== s
->hashtype
&&
371 tsap
->dh_group
== s
->dh_group
&&
372 tsap
->encklen
== s
->encklen
&&
373 tsap
->version
== s
->version
) {
374 switch(check_level
) {
375 case PROP_CHECK_OBEY
:
379 case PROP_CHECK_STRICT
:
380 if ((tsap
->lifetime
> s
->lifetime
) ||
381 (tsap
->lifebyte
> s
->lifebyte
))
386 case PROP_CHECK_CLAIM
:
387 if (tsap
->lifetime
< s
->lifetime
)
388 s
->lifetime
= tsap
->lifetime
;
389 if (tsap
->lifebyte
< s
->lifebyte
)
390 s
->lifebyte
= tsap
->lifebyte
;
394 case PROP_CHECK_EXACT
:
395 if ((tsap
->lifetime
!= s
->lifetime
) ||
396 (tsap
->lifebyte
!= s
->lifebyte
))
403 "Unexpected proposal_check value\n");
411 if (tsap
->dhgrp
!= NULL
){
412 oakley_dhgrp_free(tsap
->dhgrp
);
416 if ((s
= dupisakmpsa(s
)) != NULL
) {
417 switch(check_level
) {
418 case PROP_CHECK_OBEY
:
419 s
->lifetime
= tsap
->lifetime
;
420 s
->lifebyte
= tsap
->lifebyte
;
423 case PROP_CHECK_STRICT
:
424 s
->lifetime
= tsap
->lifetime
;
425 s
->lifebyte
= tsap
->lifebyte
;
428 case PROP_CHECK_CLAIM
:
429 if (tsap
->lifetime
< s
->lifetime
)
430 s
->lifetime
= tsap
->lifetime
;
431 if (tsap
->lifebyte
< s
->lifebyte
)
432 s
->lifebyte
= tsap
->lifebyte
;
438 // hack to get around cisco rekeys
439 if (tsap
->authmethod
!= authmethod
&& tsap_authmethod
== authmethod
) {
440 s
->authmethod
= tsap
->authmethod
;
447 * print all of items in peer's proposal which are mismatched to my proposal.
448 * p : one of peer's proposal.
449 * proposal: my proposals.
452 print_ph1mismatched(p
, proposal
)
454 struct isakmpsa
*proposal
;
456 struct isakmpsa sa
, *s
;
458 memset(&sa
, 0, sizeof(sa
));
459 if (t2isakmpsa(p
->trns
, &sa
) < 0)
461 for (s
= proposal
; s
; s
= s
->next
) {
462 if (sa
.enctype
!= s
->enctype
) {
465 "DB(prop#%d:trns#%d):Peer(prop#%d:trns#%d) = "
467 s
->prop_no
, s
->trns_no
,
468 p
->prop
->p_no
, p
->trns
->t_no
,
469 s_oakley_attr_v(OAKLEY_ATTR_ENC_ALG
,
471 s_oakley_attr_v(OAKLEY_ATTR_ENC_ALG
,
474 if (sa
.authmethod
!= s
->authmethod
) {
476 "rejected authmethod: "
477 "DB(prop#%d:trns#%d):Peer(prop#%d:trns#%d) = "
479 s
->prop_no
, s
->trns_no
,
480 p
->prop
->p_no
, p
->trns
->t_no
,
481 s_oakley_attr_v(OAKLEY_ATTR_AUTH_METHOD
,
483 s_oakley_attr_v(OAKLEY_ATTR_AUTH_METHOD
,
486 if (sa
.hashtype
!= s
->hashtype
) {
488 "rejected hashtype: "
489 "DB(prop#%d:trns#%d):Peer(prop#%d:trns#%d) = "
491 s
->prop_no
, s
->trns_no
,
492 p
->prop
->p_no
, p
->trns
->t_no
,
493 s_oakley_attr_v(OAKLEY_ATTR_HASH_ALG
,
495 s_oakley_attr_v(OAKLEY_ATTR_HASH_ALG
,
498 if (sa
.prf
!= s
->prf
||
499 sa
.prfklen
!= s
->prfklen
) {
502 "DB(prop#%d:trns#%d):Peer(prop#%d:trns#%d) = "
504 s
->prop_no
, s
->trns_no
,
505 p
->prop
->p_no
, p
->trns
->t_no
,
506 s_oakley_attr_v(OAKLEY_ATTR_HASH_ALG
,
509 s_oakley_attr_v(OAKLEY_ATTR_HASH_ALG
,
513 if (sa
.dh_group
!= s
->dh_group
) {
515 "rejected dh_group: "
516 "DB(prop#%d:trns#%d):Peer(prop#%d:trns#%d) = "
518 s
->prop_no
, s
->trns_no
,
519 p
->prop
->p_no
, p
->trns
->t_no
,
520 s_oakley_attr_v(OAKLEY_ATTR_GRP_DESC
,
522 s_oakley_attr_v(OAKLEY_ATTR_GRP_DESC
,
527 if (sa
.dhgrp
!= NULL
){
528 oakley_dhgrp_free(sa
.dhgrp
);
534 * get ISAKMP data attributes
538 struct isakmp_pl_t
*trns
;
541 struct isakmp_data
*d
, *prev
;
550 tlen
= ntohs(trns
->h
.len
) - sizeof(*trns
);
551 prev
= (struct isakmp_data
*)NULL
;
552 d
= (struct isakmp_data
*)(trns
+ 1);
555 life_t
= OAKLEY_ATTR_SA_LD_TYPE_DEFAULT
;
556 sa
->lifetime
= OAKLEY_ATTR_SA_LD_SEC_DEFAULT
;
558 sa
->dhgrp
= racoon_calloc(1, sizeof(struct dhgroup
));
564 type
= ntohs(d
->type
) & ~ISAKMP_GEN_MASK
;
565 flag
= ntohs(d
->type
) & ISAKMP_GEN_MASK
;
567 plog(ASL_LEVEL_DEBUG
,
568 "type=%s, flag=0x%04x, lorv=%s\n",
569 s_oakley_attr(type
), flag
,
570 s_oakley_attr_v(type
, ntohs(d
->lorv
)));
572 /* get variable-sized item */
574 case OAKLEY_ATTR_GRP_PI
:
575 case OAKLEY_ATTR_GRP_GEN_ONE
:
576 case OAKLEY_ATTR_GRP_GEN_TWO
:
577 case OAKLEY_ATTR_GRP_CURVE_A
:
578 case OAKLEY_ATTR_GRP_CURVE_B
:
579 case OAKLEY_ATTR_SA_LD
:
580 case OAKLEY_ATTR_GRP_ORDER
:
583 p
= (u_char
*)&d
->lorv
;
585 len
= ntohs(d
->lorv
);
588 "invalid ISAKMP-SA attr, attr-len %d, overall-len %d\n",
592 p
= (u_char
*)(d
+ 1);
597 memcpy(val
->v
, p
, len
);
605 case OAKLEY_ATTR_ENC_ALG
:
606 sa
->enctype
= (u_int16_t
)ntohs(d
->lorv
);
609 case OAKLEY_ATTR_HASH_ALG
:
610 sa
->hashtype
= (u_int16_t
)ntohs(d
->lorv
);
613 case OAKLEY_ATTR_AUTH_METHOD
:
614 sa
->authmethod
= ntohs(d
->lorv
);
617 case OAKLEY_ATTR_GRP_DESC
:
618 sa
->dh_group
= (u_int16_t
)ntohs(d
->lorv
);
621 case OAKLEY_ATTR_GRP_TYPE
:
623 int type
= (int)ntohs(d
->lorv
);
624 if (type
== OAKLEY_ATTR_GRP_TYPE_MODP
)
625 sa
->dhgrp
->type
= type
;
630 case OAKLEY_ATTR_GRP_PI
:
631 sa
->dhgrp
->prime
= val
;
634 case OAKLEY_ATTR_GRP_GEN_ONE
:
637 sa
->dhgrp
->gen1
= ntohs(d
->lorv
);
639 int len
= ntohs(d
->lorv
);
643 memcpy(&sa
->dhgrp
->gen1
, d
+ 1, len
);
644 sa
->dhgrp
->gen1
= ntohl(sa
->dhgrp
->gen1
);
648 case OAKLEY_ATTR_GRP_GEN_TWO
:
651 sa
->dhgrp
->gen2
= ntohs(d
->lorv
);
653 int len
= ntohs(d
->lorv
);
657 memcpy(&sa
->dhgrp
->gen2
, d
+ 1, len
);
658 sa
->dhgrp
->gen2
= ntohl(sa
->dhgrp
->gen2
);
662 case OAKLEY_ATTR_GRP_CURVE_A
:
663 sa
->dhgrp
->curve_a
= val
;
666 case OAKLEY_ATTR_GRP_CURVE_B
:
667 sa
->dhgrp
->curve_b
= val
;
670 case OAKLEY_ATTR_SA_LD_TYPE
:
672 int type
= (int)ntohs(d
->lorv
);
674 case OAKLEY_ATTR_SA_LD_TYPE_SEC
:
675 case OAKLEY_ATTR_SA_LD_TYPE_KB
:
679 life_t
= OAKLEY_ATTR_SA_LD_TYPE_DEFAULT
;
684 case OAKLEY_ATTR_SA_LD
:
686 || (ntohs(prev
->type
) & ~ISAKMP_GEN_MASK
) !=
687 OAKLEY_ATTR_SA_LD_TYPE
) {
689 "life duration must follow ltype\n");
694 case IPSECDOI_ATTR_SA_LD_TYPE_SEC
:
695 sa
->lifetime
= ipsecdoi_set_ld(val
);
697 if (sa
->lifetime
== 0) {
699 "invalid life duration.\n");
703 case IPSECDOI_ATTR_SA_LD_TYPE_KB
:
704 sa
->lifebyte
= ipsecdoi_set_ld(val
);
706 if (sa
->lifebyte
== 0) {
708 "invalid life duration.\n");
715 "invalid life type: %d\n", life_t
);
720 case OAKLEY_ATTR_KEY_LEN
:
722 int len
= ntohs(d
->lorv
);
725 "keylen %d: not multiple of 8\n",
729 sa
->encklen
= (u_int16_t
)len
;
733 case OAKLEY_ATTR_PRF
:
734 case OAKLEY_ATTR_FIELD_SIZE
:
738 case OAKLEY_ATTR_GRP_ORDER
:
739 sa
->dhgrp
->order
= val
;
749 d
= (struct isakmp_data
*)((char *)d
+ sizeof(*d
));
751 tlen
-= (sizeof(*d
) + ntohs(d
->lorv
));
752 d
= (struct isakmp_data
*)((char *)d
+ sizeof(*d
) + ntohs(d
->lorv
));
756 /* key length must not be specified on some algorithms */
758 if (sa
->enctype
== OAKLEY_ATTR_ENC_ALG_DES
759 || sa
->enctype
== OAKLEY_ATTR_ENC_ALG_3DES
) {
761 "keylen must not be specified "
762 "for encryption algorithm %d\n",
775 * check phase 2 SA payload and select single proposal.
776 * make new SA payload to be replyed not including general header.
777 * This function is called by responder only.
783 ipsecdoi_selectph2proposal(iph2
)
784 phase2_handle_t
*iph2
;
786 struct prop_pair
**pair
;
787 struct prop_pair
*ret
;
789 /* get proposal pair */
790 pair
= get_proppair(iph2
->sa
, IPSECDOI_TYPE_PH2
);
794 /* check and select a proposal. */
795 ret
= get_ph2approval(iph2
, pair
);
800 /* make a SA to be replayed. */
801 /* SPI must be updated later. */
802 iph2
->sa_ret
= get_sabyproppair(ret
, iph2
->ph1
);
804 if (iph2
->sa_ret
== NULL
)
811 * check phase 2 SA payload returned from responder.
812 * This function is called by initiator only.
818 ipsecdoi_checkph2proposal(iph2
)
819 phase2_handle_t
*iph2
;
821 struct prop_pair
**rpair
= NULL
, **spair
= NULL
;
825 vchar_t
*sa_ret
= NULL
;
827 /* get proposal pair of SA sent. */
828 spair
= get_proppair(iph2
->sa
, IPSECDOI_TYPE_PH2
);
831 "failed to get prop pair.\n");
835 /* XXX should check the number of transform */
837 /* get proposal pair of SA replayed */
838 rpair
= get_proppair(iph2
->sa_ret
, IPSECDOI_TYPE_PH2
);
841 "failed to get prop pair.\n");
845 /* check proposal is only one ? */
848 for (i
= 0; i
< MAXPROPPAIRLEN
; i
++) {
856 "no proposal received.\n");
861 "some proposals received.\n");
865 if (spair
[n
] == NULL
) {
866 plog(ASL_LEVEL_WARNING
,
867 "invalid proposal number:%d received.\n", i
);
871 if (rpair
[n
]->tnext
!= NULL
) {
873 "multi transforms replyed.\n");
877 if (cmp_aproppair_i(rpair
[n
], spair
[n
])) {
879 "proposal mismathed.\n");
884 * check and select a proposal.
885 * ensure that there is no modification of the proposal by
888 p
= get_ph2approval(iph2
, rpair
);
892 /* make a SA to be replayed. */
893 sa_ret
= iph2
->sa_ret
;
894 iph2
->sa_ret
= get_sabyproppair(p
, iph2
->ph1
);
896 if (iph2
->sa_ret
== NULL
)
903 free_proppair(rpair
);
905 free_proppair(spair
);
913 * compare two prop_pair which is assumed to have same proposal number.
914 * the case of bundle or single SA, NOT multi transforms.
915 * a: a proposal that is multi protocols and single transform, usually replyed.
916 * b: a proposal that is multi protocols and multi transform, usually sent.
917 * NOTE: this function is for initiator.
921 * XXX cannot understand the comment!
924 cmp_aproppair_i(a
, b
)
925 struct prop_pair
*a
, *b
;
927 struct prop_pair
*p
, *q
, *r
;
930 for (p
= a
, q
= b
; p
&& q
; p
= p
->next
, q
= q
->next
) {
931 for (r
= q
; r
; r
= r
->tnext
) {
933 if (p
->trns
->t_no
== r
->trns
->t_no
)
937 /* no suitable transform found */
939 "no suitable transform found.\n");
944 if (p
->prop
->p_no
!= r
->prop
->p_no
) {
945 plog(ASL_LEVEL_WARNING
,
946 "proposal #%d mismatched, "
948 r
->prop
->p_no
, p
->prop
->p_no
);
952 if (p
->prop
->proto_id
!= r
->prop
->proto_id
) {
954 "proto_id mismathed: my:%d peer:%d\n",
955 r
->prop
->proto_id
, p
->prop
->proto_id
);
959 if (p
->prop
->proto_id
!= r
->prop
->proto_id
) {
961 "invalid spi size: %d.\n",
966 /* check #of transforms */
967 if (p
->prop
->num_t
!= 1) {
968 plog(ASL_LEVEL_WARNING
,
969 "#of transform is %d, "
970 "but expected 1.\n", p
->prop
->num_t
);
974 if (p
->trns
->t_id
!= r
->trns
->t_id
) {
975 plog(ASL_LEVEL_WARNING
,
976 "transform number has been modified.\n");
979 if (p
->trns
->reserved
!= r
->trns
->reserved
) {
980 plog(ASL_LEVEL_WARNING
,
981 "reserved field should be zero.\n");
985 /* compare attribute */
986 len
= ntohs(r
->trns
->h
.len
) - sizeof(*p
->trns
);
987 if (memcmp(p
->trns
+ 1, r
->trns
+ 1, len
) != 0) {
988 plog(ASL_LEVEL_WARNING
,
989 "attribute has been modified.\n");
993 if ((p
&& !q
) || (!p
&& q
)) {
994 /* # of protocols mismatched */
996 "#of protocols mismatched.\n");
1004 * acceptable check for policy configuration.
1005 * return a new SA payload to be reply to peer.
1007 static struct prop_pair
*
1008 get_ph2approval(iph2
, pair
)
1009 phase2_handle_t
*iph2
;
1010 struct prop_pair
**pair
;
1012 struct prop_pair
*ret
;
1015 iph2
->approval
= NULL
;
1017 plog(ASL_LEVEL_DEBUG
,
1018 "begin compare proposals.\n");
1020 for (i
= 0; i
< MAXPROPPAIRLEN
; i
++) {
1021 if (pair
[i
] == NULL
)
1023 plog(ASL_LEVEL_DEBUG
,
1024 "pair[%d]: %p\n", i
, pair
[i
]);
1025 print_proppair(ASL_LEVEL_DEBUG
, pair
[i
]);;
1027 /* compare proposal and select one */
1028 ret
= get_ph2approvalx(iph2
, pair
[i
]);
1035 plog(ASL_LEVEL_ERR
, "no suitable policy found.\n");
1041 * compare my proposal and peers just one proposal.
1044 static struct prop_pair
*
1045 get_ph2approvalx(iph2
, pp
)
1046 phase2_handle_t
*iph2
;
1047 struct prop_pair
*pp
;
1049 struct prop_pair
*ret
= NULL
;
1050 struct saprop
*pr0
, *pr
= NULL
;
1051 struct saprop
*q1
, *q2
;
1053 pr0
= aproppair2saprop(pp
);
1057 for (q1
= pr0
; q1
; q1
= q1
->next
) {
1058 for (q2
= iph2
->proposal
; q2
; q2
= q2
->next
) {
1059 plog(ASL_LEVEL_DEBUG
,
1060 "peer's single bundle:\n");
1061 printsaprop0(ASL_LEVEL_DEBUG
, q1
);
1062 plog(ASL_LEVEL_DEBUG
,
1063 "my single bundle:\n");
1064 printsaprop0(ASL_LEVEL_DEBUG
, q2
);
1066 pr
= cmpsaprop_alloc(iph2
->ph1
, q1
, q2
, iph2
->side
);
1074 /* no proposal matching */
1087 plog(ASL_LEVEL_DEBUG
, "matched\n");
1088 iph2
->approval
= pr
;
1092 struct prop_pair
*p
, *x
;
1093 struct prop_pair
*n
= NULL
;
1097 for (p
= pp
; p
; p
= p
->next
) {
1099 * find a proposal with matching proto_id.
1100 * we have analyzed validity already, in cmpsaprop_alloc().
1102 for (sp
= pr
->head
; sp
; sp
= sp
->next
) {
1103 if (sp
->proto_id
== p
->prop
->proto_id
)
1111 for (x
= p
; x
; x
= x
->tnext
)
1112 if (sp
->head
->trns_no
== x
->trns
->t_no
)
1117 n
= racoon_calloc(1, sizeof(struct prop_pair
));
1120 "failed to get buffer.\n");
1127 /* need to preserve the order */
1128 for (x
= ret
; x
&& x
->next
; x
= x
->next
)
1130 if (x
&& x
->prop
== n
->prop
) {
1131 for (/*nothing*/; x
&& x
->tnext
; x
= x
->tnext
)
1142 /* #of transforms should be updated ? */
1151 struct prop_pair
**pair
;
1155 for (i
= 0; i
< MAXPROPPAIRLEN
; i
++) {
1156 free_proppair0(pair
[i
]);
1163 free_proppair0(pair
)
1164 struct prop_pair
*pair
;
1166 struct prop_pair
*p
, *q
, *r
, *s
;
1182 * get proposal pairs from SA payload.
1183 * tiny check for proposal payload.
1186 get_proppair(sa
, mode
)
1190 struct prop_pair
**pair
= NULL
;
1191 int num_p
= 0; /* number of proposal for use */
1196 //plogdump(ASL_LEVEL_DEBUG, sa->v, sa->l, "total SA len=%zu\n", sa->l);
1198 if (mode
== IPSECDOI_TYPE_PH1
|| mode
== IPSECDOI_TYPE_PH2
) {
1200 struct ipsecdoi_sa_b
*sab
= ALIGNED_CAST(__typeof__(sab
))sa
->v
;
1203 /* check SA payload size */
1204 if (sa
->l
< sizeof(*sab
)) {
1206 "Invalid SA length = %zu.\n", sa
->l
);
1211 if (check_doi(ntohl(sab
->doi
)) < 0)
1214 /* check SITUATION */
1215 if (check_situation(ntohl(sab
->sit
)) < 0)
1218 bp
= (caddr_t
)(sab
+ 1);
1219 tlen
= sa
->l
- sizeof(*sab
);
1221 bp
= (__typeof__(bp
))sa
->v
;
1225 pair
= racoon_calloc(1, MAXPROPPAIRLEN
* sizeof(*pair
));
1228 "failed to get buffer.\n");
1233 struct isakmp_pl_p
*prop
;
1235 vchar_t
*pbuf
= NULL
;
1236 struct isakmp_parse_t
*pa
;
1238 pbuf
= isakmp_parsewoh(ISAKMP_NPTYPE_P
, (struct isakmp_gen
*)bp
, tlen
);
1242 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
1243 pa
->type
!= ISAKMP_NPTYPE_NONE
;
1245 /* check the value of next payload */
1246 if (pa
->type
!= ISAKMP_NPTYPE_P
) {
1248 "Invalid payload type=%u\n", pa
->type
);
1253 prop
= (struct isakmp_pl_p
*)pa
->ptr
;
1256 plog(ASL_LEVEL_DEBUG
,
1257 "proposal #%u len=%d\n", prop
->p_no
, proplen
);
1261 "invalid proposal with length %d\n", proplen
);
1266 /* check Protocol ID */
1267 if (!check_protocol
[mode
]) {
1269 "unsupported mode %d\n", mode
);
1273 if (check_protocol
[mode
](prop
->proto_id
) < 0)
1276 /* check SPI length when IKE. */
1277 if (check_spi_size(prop
->proto_id
, prop
->spi_size
) < 0)
1281 if (get_transform(prop
, pair
, &num_p
) < 0) {
1292 struct prop_pair
*p
, *q
;
1294 /* check for proposals with no transforms */
1295 for (i
= 0; i
< MAXPROPPAIRLEN
; i
++) {
1299 plog(ASL_LEVEL_DEBUG
, "pair %d:\n", i
);
1300 print_proppair(ASL_LEVEL_DEBUG
, pair
[i
]);
1302 notrans
= nprop
= 0;
1303 for (p
= pair
[i
]; p
; p
= p
->next
) {
1304 if (p
->trns
== NULL
) {
1308 for (q
= p
; q
; q
= q
->tnext
)
1314 * XXX at this moment, we cannot accept proposal group
1315 * with multiple proposals. this should be fixed.
1317 if (pair
[i
]->next
) {
1318 plog(ASL_LEVEL_WARNING
,
1319 "proposal #%u ignored "
1320 "(multiple proposal not supported)\n",
1321 pair
[i
]->prop
->p_no
);
1327 for (p
= pair
[i
]; p
; p
= q
) {
1334 plog(ASL_LEVEL_DEBUG
,
1335 "proposal #%u: %d transform\n",
1336 pair
[i
]->prop
->p_no
, nprop
);
1341 /* bark if no proposal is found. */
1344 "no Proposal found.\n");
1356 * check transform payload.
1358 * positive: return the pointer to the payload of valid transform.
1359 * 0 : No valid transform found.
1362 get_transform(prop
, pair
, num_p
)
1363 struct isakmp_pl_p
*prop
;
1364 struct prop_pair
**pair
;
1367 int tlen
; /* total length of all transform in a proposal */
1369 struct isakmp_pl_t
*trns
;
1371 vchar_t
*pbuf
= NULL
;
1372 struct isakmp_parse_t
*pa
;
1373 struct prop_pair
*p
= NULL
, *q
;
1376 bp
= (caddr_t
)prop
+ sizeof(struct isakmp_pl_p
) + prop
->spi_size
;
1377 tlen
= ntohs(prop
->h
.len
)
1378 - (sizeof(struct isakmp_pl_p
) + prop
->spi_size
);
1379 pbuf
= isakmp_parsewoh(ISAKMP_NPTYPE_T
, (struct isakmp_gen
*)bp
, tlen
);
1383 /* check and get transform for use */
1385 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
1386 pa
->type
!= ISAKMP_NPTYPE_NONE
;
1391 /* check the value of next payload */
1392 if (pa
->type
!= ISAKMP_NPTYPE_T
) {
1394 "Invalid payload type=%u\n", pa
->type
);
1398 trns
= (struct isakmp_pl_t
*)pa
->ptr
;
1401 plog(ASL_LEVEL_DEBUG
,
1402 "transform #%u len=%u\n", trns
->t_no
, trnslen
);
1404 /* check transform ID */
1405 if (prop
->proto_id
>= ARRAYLEN(check_transform
)) {
1406 plog(ASL_LEVEL_WARNING
,
1407 "unsupported proto_id %u\n",
1411 if (prop
->proto_id
>= ARRAYLEN(check_attributes
)) {
1412 plog(ASL_LEVEL_WARNING
,
1413 "unsupported proto_id %u\n",
1418 if (!check_transform
[prop
->proto_id
]
1419 || !check_attributes
[prop
->proto_id
]) {
1420 plog(ASL_LEVEL_WARNING
,
1421 "unsupported proto_id %u\n",
1425 if (check_transform
[prop
->proto_id
](trns
->t_id
) < 0)
1428 /* check data attributes */
1429 if (check_attributes
[prop
->proto_id
](trns
) != 0)
1432 p
= racoon_calloc(1, sizeof(*p
));
1435 "failed to get buffer.\n");
1442 /* need to preserve the order */
1443 for (q
= pair
[prop
->p_no
]; q
&& q
->next
; q
= q
->next
)
1445 if (q
&& q
->prop
== p
->prop
) {
1446 for (/*nothing*/; q
&& q
->tnext
; q
= q
->tnext
)
1453 pair
[prop
->p_no
] = p
;
1465 * make a new SA payload from prop_pair.
1466 * NOTE: this function clears the spi value.
1469 get_sabyproppair(pair
, iph1
)
1470 struct prop_pair
*pair
;
1471 phase1_handle_t
*iph1
;
1475 u_int8_t
*np_p
= NULL
;
1476 struct prop_pair
*p
;
1477 int prophlen
, trnslen
;
1480 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
1481 newtlen
= sizeof(struct ipsecdoi_sa_b
);
1485 for (p
= pair
; p
; p
= p
->next
) {
1486 newtlen
+= sizeof(struct isakmp_pl_p
);
1487 newtlen
+= p
->prop
->spi_size
;
1488 newtlen
+= ntohs(p
->trns
->h
.len
);
1491 newsa
= vmalloc(newtlen
);
1492 if (newsa
== NULL
) {
1493 plog(ASL_LEVEL_ERR
, "failed to get newsa.\n");
1498 ((struct isakmp_gen
*)bp
)->len
= htons(newtlen
);
1500 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
1501 /* update some of values in SA header */
1502 (ALIGNED_CAST(struct ipsecdoi_sa_b
*)bp
)->doi
= htonl(iph1
->rmconf
->doitype
);
1503 (ALIGNED_CAST(struct ipsecdoi_sa_b
*)bp
)->sit
= htonl(iph1
->rmconf
->sittype
);
1504 bp
+= sizeof(struct ipsecdoi_sa_b
);
1507 /* create proposal payloads */
1508 for (p
= pair
; p
; p
= p
->next
) {
1509 prophlen
= sizeof(struct isakmp_pl_p
)
1510 + p
->prop
->spi_size
;
1511 trnslen
= ntohs(p
->trns
->h
.len
);
1514 *np_p
= ISAKMP_NPTYPE_P
;
1516 /* create proposal */
1518 memcpy(bp
, p
->prop
, prophlen
);
1519 ((struct isakmp_pl_p
*)bp
)->h
.np
= ISAKMP_NPTYPE_NONE
;
1520 ((struct isakmp_pl_p
*)bp
)->h
.len
= htons(prophlen
+ trnslen
);
1521 ((struct isakmp_pl_p
*)bp
)->num_t
= 1;
1522 np_p
= &((struct isakmp_pl_p
*)bp
)->h
.np
;
1523 memset(bp
+ sizeof(struct isakmp_pl_p
), 0, p
->prop
->spi_size
);
1526 /* create transform */
1527 memcpy(bp
, p
->trns
, trnslen
);
1528 ((struct isakmp_pl_t
*)bp
)->h
.np
= ISAKMP_NPTYPE_NONE
;
1529 ((struct isakmp_pl_t
*)bp
)->h
.len
= htons(trnslen
);
1537 * update responder's spi
1540 ipsecdoi_updatespi(iph2
)
1541 phase2_handle_t
*iph2
;
1543 struct prop_pair
**pair
, *p
;
1550 pair
= get_proppair(iph2
->sa_ret
, IPSECDOI_TYPE_PH2
);
1553 for (i
= 0; i
< MAXPROPPAIRLEN
; i
++) {
1557 if (i
== MAXPROPPAIRLEN
|| pair
[i
]->tnext
) {
1558 /* multiple transform must be filtered by selectph2proposal.*/
1562 pp
= iph2
->approval
;
1564 /* create proposal payloads */
1565 for (p
= pair
[i
]; p
; p
= p
->next
) {
1567 * find a proposal/transform with matching proto_id/t_id.
1568 * we have analyzed validity already, in cmpsaprop_alloc().
1570 for (pr
= pp
->head
; pr
; pr
= pr
->next
) {
1571 if (p
->prop
->proto_id
== pr
->proto_id
&&
1572 p
->trns
->t_id
== pr
->head
->trns_id
) {
1580 * XXX SPI bits are left-filled, for use with IPComp.
1581 * we should be switching to variable-length spi field...
1583 spi
= (u_int8_t
*)&pr
->spi
;
1584 spi
+= sizeof(pr
->spi
);
1586 memcpy((caddr_t
)p
->prop
+ sizeof(*p
->prop
), spi
, pr
->spisize
);
1591 free_proppair(pair
);
1596 * make a new SA payload from prop_pair.
1599 get_sabysaprop(pp0
, sa0
)
1603 struct prop_pair
**pair
= NULL
;
1604 vchar_t
*newsa
= NULL
;
1606 u_int8_t
*np_p
= NULL
;
1607 struct prop_pair
*p
= NULL
;
1611 int prophlen
, trnslen
;
1615 /* get proposal pair */
1616 pair
= get_proppair(sa0
, IPSECDOI_TYPE_PH2
);
1620 newtlen
= sizeof(struct ipsecdoi_sa_b
);
1621 for (pp
= pp0
; pp
; pp
= pp
->next
) {
1623 if (pair
[pp
->prop_no
] == NULL
)
1626 for (pr
= pp
->head
; pr
; pr
= pr
->next
) {
1627 newtlen
+= (sizeof(struct isakmp_pl_p
)
1630 for (tr
= pr
->head
; tr
; tr
= tr
->next
) {
1631 for (p
= pair
[pp
->prop_no
]; p
; p
= p
->tnext
) {
1632 if (tr
->trns_no
== p
->trns
->t_no
)
1638 newtlen
+= ntohs(p
->trns
->h
.len
);
1643 newsa
= vmalloc(newtlen
);
1644 if (newsa
== NULL
) {
1645 plog(ASL_LEVEL_ERR
, "failed to get newsa.\n");
1650 /* some of values of SA must be updated in the out of this function */
1651 ((struct isakmp_gen
*)bp
)->len
= htons(newtlen
);
1652 bp
+= sizeof(struct ipsecdoi_sa_b
);
1654 /* create proposal payloads */
1655 for (pp
= pp0
; pp
; pp
= pp
->next
) {
1657 for (pr
= pp
->head
; pr
; pr
= pr
->next
) {
1658 prophlen
= sizeof(struct isakmp_pl_p
)
1659 + p
->prop
->spi_size
;
1661 for (tr
= pr
->head
; tr
; tr
= tr
->next
) {
1662 for (p
= pair
[pp
->prop_no
]; p
; p
= p
->tnext
) {
1663 if (tr
->trns_no
== p
->trns
->t_no
)
1669 trnslen
= ntohs(p
->trns
->h
.len
);
1672 *np_p
= ISAKMP_NPTYPE_P
;
1674 /* create proposal */
1676 memcpy(bp
, p
->prop
, prophlen
);
1677 ((struct isakmp_pl_p
*)bp
)->h
.np
= ISAKMP_NPTYPE_NONE
;
1678 ((struct isakmp_pl_p
*)bp
)->h
.len
= htons(prophlen
+ trnslen
);
1679 ((struct isakmp_pl_p
*)bp
)->num_t
= 1;
1680 np_p
= &((struct isakmp_pl_p
*)bp
)->h
.np
;
1683 /* create transform */
1684 memcpy(bp
, p
->trns
, trnslen
);
1685 ((struct isakmp_pl_t
*)bp
)->h
.np
= ISAKMP_NPTYPE_NONE
;
1686 ((struct isakmp_pl_t
*)bp
)->h
.len
= htons(trnslen
);
1698 if (newsa
!= NULL
) {
1708 * If some error happens then return 0. Although 0 means that lifetime is zero,
1709 * such a value should not be accepted.
1710 * Also 0 of lifebyte should not be included in a packet although 0 means not
1714 ipsecdoi_set_ld(buf
)
1724 ld
= ntohs(*ALIGNED_CAST(u_int16_t
*)buf
->v
);
1727 ld
= ntohl(*ALIGNED_CAST(u_int32_t
*)buf
->v
);
1731 "length %zu of life duration "
1732 "isn't supported.\n", buf
->l
);
1752 "invalid value of DOI 0x%08x.\n", doi
);
1762 check_situation(sit
)
1766 case IPSECDOI_SIT_IDENTITY_ONLY
:
1769 case IPSECDOI_SIT_SECRECY
:
1770 case IPSECDOI_SIT_INTEGRITY
:
1772 "situation 0x%08x unsupported yet.\n", sit
);
1777 "invalid situation 0x%08x.\n", sit
);
1784 * check protocol id in main mode
1787 check_prot_main(proto_id
)
1791 case IPSECDOI_PROTO_ISAKMP
:
1796 "Illegal protocol id=%u.\n", proto_id
);
1803 * check protocol id in quick mode
1806 check_prot_quick(proto_id
)
1810 case IPSECDOI_PROTO_IPSEC_AH
:
1811 case IPSECDOI_PROTO_IPSEC_ESP
:
1814 case IPSECDOI_PROTO_IPCOMP
:
1819 "invalid protocol id %d.\n", proto_id
);
1826 check_spi_size(proto_id
, size
)
1830 case IPSECDOI_PROTO_ISAKMP
:
1833 plog(ASL_LEVEL_WARNING
,
1834 "SPI size isn't zero, but IKE proposal.\n");
1838 case IPSECDOI_PROTO_IPSEC_AH
:
1839 case IPSECDOI_PROTO_IPSEC_ESP
:
1842 "invalid SPI size=%d for IPSEC proposal.\n",
1848 case IPSECDOI_PROTO_IPCOMP
:
1849 if (size
!= 2 && size
!= 4) {
1851 "invalid SPI size=%d for IPCOMP proposal.\n",
1865 * check transform ID in ISAKMP.
1868 check_trns_isakmp(t_id
)
1872 case IPSECDOI_KEY_IKE
:
1876 "invalid transform-id=%u in proto_id=%u.\n",
1877 t_id
, IPSECDOI_KEY_IKE
);
1884 * check transform ID in AH.
1891 case IPSECDOI_AH_MD5
:
1892 case IPSECDOI_AH_SHA
:
1893 case IPSECDOI_AH_SHA256
:
1894 case IPSECDOI_AH_SHA384
:
1895 case IPSECDOI_AH_SHA512
:
1897 case IPSECDOI_AH_DES
:
1899 "not support transform-id=%u in AH.\n", t_id
);
1903 "invalid transform-id=%u in AH.\n", t_id
);
1910 * check transform ID in ESP.
1913 check_trns_esp(t_id
)
1917 case IPSECDOI_ESP_DES
:
1918 case IPSECDOI_ESP_3DES
:
1919 case IPSECDOI_ESP_NULL
:
1920 case IPSECDOI_ESP_RC5
:
1921 case IPSECDOI_ESP_CAST
:
1922 case IPSECDOI_ESP_BLOWFISH
:
1923 case IPSECDOI_ESP_AES
:
1924 case IPSECDOI_ESP_TWOFISH
:
1926 case IPSECDOI_ESP_DES_IV32
:
1927 case IPSECDOI_ESP_DES_IV64
:
1928 case IPSECDOI_ESP_IDEA
:
1929 case IPSECDOI_ESP_3IDEA
:
1930 case IPSECDOI_ESP_RC4
:
1932 "not support transform-id=%u in ESP.\n", t_id
);
1936 "invalid transform-id=%u in ESP.\n", t_id
);
1943 * check transform ID in IPCOMP.
1946 check_trns_ipcomp(t_id
)
1950 case IPSECDOI_IPCOMP_OUI
:
1951 case IPSECDOI_IPCOMP_DEFLATE
:
1952 case IPSECDOI_IPCOMP_LZS
:
1956 "invalid transform-id=%u in IPCOMP.\n", t_id
);
1963 * check data attributes in IKE.
1966 check_attr_isakmp(trns
)
1967 struct isakmp_pl_t
*trns
;
1969 struct isakmp_data
*d
;
1974 tlen
= ntohs(trns
->h
.len
) - sizeof(struct isakmp_pl_t
);
1975 d
= (struct isakmp_data
*)((caddr_t
)trns
+ sizeof(struct isakmp_pl_t
));
1978 type
= ntohs(d
->type
) & ~ISAKMP_GEN_MASK
;
1979 flag
= ntohs(d
->type
) & ISAKMP_GEN_MASK
;
1980 lorv
= ntohs(d
->lorv
);
1982 plog(ASL_LEVEL_DEBUG
,
1983 "type=%s, flag=0x%04x, lorv=%s\n",
1984 s_oakley_attr(type
), flag
,
1985 s_oakley_attr_v(type
, lorv
));
1988 * some of the attributes must be encoded in TV.
1989 * see RFC2409 Appendix A "Attribute Classes".
1992 case OAKLEY_ATTR_ENC_ALG
:
1993 case OAKLEY_ATTR_HASH_ALG
:
1994 case OAKLEY_ATTR_AUTH_METHOD
:
1995 case OAKLEY_ATTR_GRP_DESC
:
1996 case OAKLEY_ATTR_GRP_TYPE
:
1997 case OAKLEY_ATTR_SA_LD_TYPE
:
1998 case OAKLEY_ATTR_PRF
:
1999 case OAKLEY_ATTR_KEY_LEN
:
2000 case OAKLEY_ATTR_FIELD_SIZE
:
2001 if (!flag
) { /* TLV*/
2003 "oakley attribute %d must be TV.\n",
2010 /* sanity check for TLV. length must be specified. */
2011 if (!flag
&& lorv
== 0) { /*TLV*/
2013 "invalid length %d for TLV attribute %d.\n",
2019 case OAKLEY_ATTR_ENC_ALG
:
2020 if (!alg_oakley_encdef_ok(lorv
)) {
2022 "invalied encryption algorithm=%d.\n",
2028 case OAKLEY_ATTR_HASH_ALG
:
2029 if (!alg_oakley_hashdef_ok(lorv
)) {
2031 "invalied hash algorithm=%d.\n",
2037 case OAKLEY_ATTR_AUTH_METHOD
:
2039 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
2040 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
2041 #ifdef ENABLE_HYBRID
2042 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
2043 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
2044 #if 0 /* Clashes with OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB */
2045 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_I
:
2047 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
2049 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
2051 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
2052 #ifdef ENABLE_HYBRID
2053 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
2054 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
2055 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_I
:
2056 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_R
:
2057 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_I
:
2058 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_R
:
2059 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
:
2060 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
2061 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
:
2062 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
2064 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
2065 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
2067 "auth method %s isn't supported.\n",
2068 s_oakley_attr_method(lorv
));
2072 "invalid auth method %d.\n",
2078 case OAKLEY_ATTR_GRP_DESC
:
2079 if (!alg_oakley_dhdef_ok(lorv
)) {
2081 "invalid DH group %d.\n",
2087 case OAKLEY_ATTR_GRP_TYPE
:
2089 case OAKLEY_ATTR_GRP_TYPE_MODP
:
2093 "unsupported DH group type %d.\n",
2099 case OAKLEY_ATTR_GRP_PI
:
2100 case OAKLEY_ATTR_GRP_GEN_ONE
:
2101 /* sanity checks? */
2104 case OAKLEY_ATTR_GRP_GEN_TWO
:
2105 case OAKLEY_ATTR_GRP_CURVE_A
:
2106 case OAKLEY_ATTR_GRP_CURVE_B
:
2108 "attr type=%u isn't supported.\n", type
);
2111 case OAKLEY_ATTR_SA_LD_TYPE
:
2113 case OAKLEY_ATTR_SA_LD_TYPE_SEC
:
2114 case OAKLEY_ATTR_SA_LD_TYPE_KB
:
2118 "invalid life type %d.\n", lorv
);
2123 case OAKLEY_ATTR_SA_LD
:
2124 /* should check the value */
2127 case OAKLEY_ATTR_PRF
:
2128 case OAKLEY_ATTR_KEY_LEN
:
2131 case OAKLEY_ATTR_FIELD_SIZE
:
2133 "attr type=%u isn't supported.\n", type
);
2136 case OAKLEY_ATTR_GRP_ORDER
:
2139 case OAKLEY_ATTR_GSS_ID
:
2144 "invalid attribute type %d.\n", type
);
2150 d
= (struct isakmp_data
*)((char *)d
2153 tlen
-= (sizeof(*d
) + lorv
);
2154 d
= (struct isakmp_data
*)((char *)d
2155 + sizeof(*d
) + lorv
);
2163 * check data attributes in IPSEC AH/ESP.
2167 struct isakmp_pl_t
*trns
;
2169 return check_attr_ipsec(IPSECDOI_PROTO_IPSEC_AH
, trns
);
2173 check_attr_esp(trns
)
2174 struct isakmp_pl_t
*trns
;
2176 return check_attr_ipsec(IPSECDOI_PROTO_IPSEC_ESP
, trns
);
2180 check_attr_ipsec(proto_id
, trns
)
2182 struct isakmp_pl_t
*trns
;
2184 struct isakmp_data
*d
;
2188 int attrseen
[16]; /* XXX magic number */
2190 tlen
= ntohs(trns
->h
.len
) - sizeof(struct isakmp_pl_t
);
2191 d
= (struct isakmp_data
*)((caddr_t
)trns
+ sizeof(struct isakmp_pl_t
));
2192 memset(attrseen
, 0, sizeof(attrseen
));
2195 type
= ntohs(d
->type
) & ~ISAKMP_GEN_MASK
;
2196 flag
= ntohs(d
->type
) & ISAKMP_GEN_MASK
;
2197 lorv
= ntohs(d
->lorv
);
2199 plog(ASL_LEVEL_DEBUG
,
2200 "type=%s, flag=0x%04x, lorv=%s\n",
2201 s_ipsecdoi_attr(type
), flag
,
2202 s_ipsecdoi_attr_v(type
, lorv
));
2204 if (type
< sizeof(attrseen
)/sizeof(attrseen
[0]))
2208 case IPSECDOI_ATTR_ENC_MODE
:
2211 "must be TV when ENC_MODE.\n");
2216 case IPSECDOI_ATTR_ENC_MODE_TUNNEL
:
2217 case IPSECDOI_ATTR_ENC_MODE_TRNS
:
2220 case IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_RFC
:
2221 case IPSECDOI_ATTR_ENC_MODE_UDPTRNS_RFC
:
2222 case IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_DRAFT
:
2223 case IPSECDOI_ATTR_ENC_MODE_UDPTRNS_DRAFT
:
2224 plog(ASL_LEVEL_DEBUG
,
2225 "UDP encapsulation requested\n");
2230 "invalid encryption mode=%u.\n",
2236 case IPSECDOI_ATTR_AUTH
:
2239 "must be TV when AUTH.\n");
2244 case IPSECDOI_ATTR_AUTH_HMAC_MD5
:
2245 if (proto_id
== IPSECDOI_PROTO_IPSEC_AH
&&
2246 trns
->t_id
!= IPSECDOI_AH_MD5
) {
2249 "auth algorithm %u conflicts "
2250 "with transform %u.\n",
2255 case IPSECDOI_ATTR_AUTH_HMAC_SHA1
:
2256 if (proto_id
== IPSECDOI_PROTO_IPSEC_AH
) {
2257 if (trns
->t_id
!= IPSECDOI_AH_SHA
)
2261 case IPSECDOI_ATTR_AUTH_HMAC_SHA2_256
:
2262 if (proto_id
== IPSECDOI_PROTO_IPSEC_AH
) {
2263 if (trns
->t_id
!= IPSECDOI_AH_SHA256
)
2267 case IPSECDOI_ATTR_AUTH_HMAC_SHA2_384
:
2268 if (proto_id
== IPSECDOI_PROTO_IPSEC_AH
) {
2269 if (trns
->t_id
!= IPSECDOI_AH_SHA384
)
2273 case IPSECDOI_ATTR_AUTH_HMAC_SHA2_512
:
2274 if (proto_id
== IPSECDOI_PROTO_IPSEC_AH
) {
2275 if (trns
->t_id
!= IPSECDOI_AH_SHA512
)
2279 case IPSECDOI_ATTR_AUTH_DES_MAC
:
2280 case IPSECDOI_ATTR_AUTH_KPDK
:
2282 "auth algorithm %u isn't supported.\n",
2287 "invalid auth algorithm=%u.\n",
2293 case IPSECDOI_ATTR_SA_LD_TYPE
:
2296 "must be TV when LD_TYPE.\n");
2301 case IPSECDOI_ATTR_SA_LD_TYPE_SEC
:
2302 case IPSECDOI_ATTR_SA_LD_TYPE_KB
:
2306 "invalid life type %d.\n", lorv
);
2311 case IPSECDOI_ATTR_SA_LD
:
2313 /* i.e. ISAKMP_GEN_TV */
2314 plog(ASL_LEVEL_DEBUG
,
2315 "life duration was in TLV.\n");
2317 /* i.e. ISAKMP_GEN_TLV */
2320 "invalid length of LD\n");
2326 case IPSECDOI_ATTR_GRP_DESC
:
2329 "must be TV when GRP_DESC.\n");
2333 if (!alg_oakley_dhdef_ok(lorv
)) {
2335 "invalid group description=%u.\n",
2341 case IPSECDOI_ATTR_KEY_LENGTH
:
2344 "must be TV when KEY_LENGTH.\n");
2349 case IPSECDOI_ATTR_KEY_ROUNDS
:
2350 case IPSECDOI_ATTR_COMP_DICT_SIZE
:
2351 case IPSECDOI_ATTR_COMP_PRIVALG
:
2353 "attr type=%u isn't supported.\n", type
);
2358 "invalid attribute type %d.\n", type
);
2364 d
= (struct isakmp_data
*)((char *)d
2367 tlen
-= (sizeof(*d
) + lorv
);
2368 d
= (struct isakmp_data
*)((caddr_t
)d
2369 + sizeof(*d
) + lorv
);
2373 if (proto_id
== IPSECDOI_PROTO_IPSEC_AH
&&
2374 !attrseen
[IPSECDOI_ATTR_AUTH
]) {
2376 "attr AUTH must be present for AH.\n");
2380 if (proto_id
== IPSECDOI_PROTO_IPSEC_ESP
&&
2381 trns
->t_id
== IPSECDOI_ESP_NULL
&&
2382 !attrseen
[IPSECDOI_ATTR_AUTH
]) {
2384 "attr AUTH must be present for ESP NULL encryption.\n");
2392 check_attr_ipcomp(trns
)
2393 struct isakmp_pl_t
*trns
;
2395 struct isakmp_data
*d
;
2399 int attrseen
[16]; /* XXX magic number */
2401 tlen
= ntohs(trns
->h
.len
) - sizeof(struct isakmp_pl_t
);
2402 d
= (struct isakmp_data
*)((caddr_t
)trns
+ sizeof(struct isakmp_pl_t
));
2403 memset(attrseen
, 0, sizeof(attrseen
));
2406 type
= ntohs(d
->type
) & ~ISAKMP_GEN_MASK
;
2407 flag
= ntohs(d
->type
) & ISAKMP_GEN_MASK
;
2408 lorv
= ntohs(d
->lorv
);
2410 plog(ASL_LEVEL_DEBUG
,
2411 "type=%d, flag=0x%04x, lorv=0x%04x\n",
2414 if (type
< sizeof(attrseen
)/sizeof(attrseen
[0]))
2418 case IPSECDOI_ATTR_ENC_MODE
:
2421 "must be TV when ENC_MODE.\n");
2426 case IPSECDOI_ATTR_ENC_MODE_TUNNEL
:
2427 case IPSECDOI_ATTR_ENC_MODE_TRNS
:
2430 case IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_RFC
:
2431 case IPSECDOI_ATTR_ENC_MODE_UDPTRNS_RFC
:
2432 case IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_DRAFT
:
2433 case IPSECDOI_ATTR_ENC_MODE_UDPTRNS_DRAFT
:
2434 plog(ASL_LEVEL_DEBUG
,
2435 "UDP encapsulation requested\n");
2440 "invalid encryption mode=%u.\n",
2446 case IPSECDOI_ATTR_SA_LD_TYPE
:
2449 "must be TV when LD_TYPE.\n");
2454 case IPSECDOI_ATTR_SA_LD_TYPE_SEC
:
2455 case IPSECDOI_ATTR_SA_LD_TYPE_KB
:
2459 "invalid life type %d.\n", lorv
);
2464 case IPSECDOI_ATTR_SA_LD
:
2466 /* i.e. ISAKMP_GEN_TV */
2467 plog(ASL_LEVEL_DEBUG
,
2468 "life duration was in TLV.\n");
2470 /* i.e. ISAKMP_GEN_TLV */
2473 "invalid length of LD\n");
2479 case IPSECDOI_ATTR_GRP_DESC
:
2482 "must be TV when GRP_DESC.\n");
2486 if (!alg_oakley_dhdef_ok(lorv
)) {
2488 "invalid group description=%u.\n",
2494 case IPSECDOI_ATTR_AUTH
:
2496 "invalid attr type=%u.\n", type
);
2499 case IPSECDOI_ATTR_KEY_LENGTH
:
2500 case IPSECDOI_ATTR_KEY_ROUNDS
:
2501 case IPSECDOI_ATTR_COMP_DICT_SIZE
:
2502 case IPSECDOI_ATTR_COMP_PRIVALG
:
2504 "attr type=%u isn't supported.\n", type
);
2509 "invalid attribute type %d.\n", type
);
2515 d
= (struct isakmp_data
*)((char *)d
2518 tlen
-= (sizeof(*d
) + lorv
);
2519 d
= (struct isakmp_data
*)((caddr_t
)d
2520 + sizeof(*d
) + lorv
);
2525 if (proto_id
== IPSECDOI_PROTO_IPCOMP
&&
2526 !attrseen
[IPSECDOI_ATTR_AUTH
]) {
2528 "attr AUTH must be present for AH.\n", type
);
2538 * create phase1 proposal from remote configuration.
2539 * NOT INCLUDING isakmp general header of SA payload
2542 ipsecdoi_setph1proposal (phase1_handle_t
*iph1
)
2547 if (!iph1
) return NULL
;
2549 struct isakmpsa
*props
= iph1
->rmconf
->proposal
;
2550 unsigned int version
= iph1
->version
;
2552 /* count total size of SA minus isakmp general header */
2553 /* not including isakmp general header of SA payload */
2554 if (version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
2555 sablen
= sizeof(struct ipsecdoi_sa_b
);
2559 sablen
+= setph1prop(iph1
, NULL
);
2561 mysa
= vmalloc(sablen
);
2564 "failed to allocate my sa buffer\n");
2568 /* create SA payload */
2569 if (version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
2570 /* not including isakmp general header */
2571 (ALIGNED_CAST(struct ipsecdoi_sa_b
*)mysa
->v
)->doi
= htonl(props
->rmconf
->doitype
);
2572 (ALIGNED_CAST(struct ipsecdoi_sa_b
*)mysa
->v
)->sit
= htonl(props
->rmconf
->sittype
);
2574 (void)setph1prop(iph1
, mysa
->v
+ sizeof(struct ipsecdoi_sa_b
));
2581 setph1prop (phase1_handle_t
*iph1
,
2584 struct isakmpsa
*props
= iph1
->rmconf
->proposal
;
2586 struct isakmp_pl_p
*prop
= NULL
;
2587 struct isakmpsa
*s
= NULL
;
2588 int proplen
, trnslen
;
2589 u_int8_t
*np_t
; /* pointer next trns type in previous header */
2594 proplen
= sizeof(*prop
) + spi_size
;
2596 /* create proposal */
2597 prop
= (struct isakmp_pl_p
*)p
;
2598 prop
->h
.np
= ISAKMP_NPTYPE_NONE
;
2599 prop
->h
.reserved
= 0;
2600 prop
->p_no
= props
->prop_no
;
2601 prop
->proto_id
= IPSECDOI_PROTO_ISAKMP
;
2602 prop
->spi_size
= spi_size
;
2609 for (s
= props
; s
!= NULL
; s
= s
->next
) {
2612 *np_t
= ISAKMP_NPTYPE_T
;
2614 trnslen
= setph1trns(s
, p
);
2617 /* save buffer to pre-next payload */
2618 np_t
= &((struct isakmp_pl_t
*)p
)->h
.np
;
2621 /* count up transform length */
2627 /* update proposal length */
2629 prop
->h
.len
= htons(proplen
);
2630 prop
->num_t
= trns_num
;
2637 setph1trns (struct isakmpsa
*sa
,
2640 struct isakmp_pl_t
*trns
= NULL
;
2641 int trnslen
, attrlen
;
2644 trnslen
= sizeof(*trns
);
2646 /* create transform */
2647 trns
= (struct isakmp_pl_t
*)p
;
2648 trns
->h
.np
= ISAKMP_NPTYPE_NONE
;
2649 trns
->t_no
= sa
->trns_no
;
2650 trns
->t_id
= IPSECDOI_KEY_IKE
;
2654 attrlen
= setph1attr(sa
, p
);
2660 trns
->h
.len
= htons(trnslen
);
2666 setph1attr (struct isakmpsa
*sa
,
2673 u_int32_t lifetime
= htonl((u_int32_t
)sa
->lifetime
);
2675 attrlen
+= sizeof(struct isakmp_data
)
2676 + sizeof(struct isakmp_data
);
2677 if (sa
->lifetime
> 0xffff)
2678 attrlen
+= sizeof(lifetime
);
2680 p
= isakmp_set_attr_l(p
, OAKLEY_ATTR_SA_LD_TYPE
,
2681 OAKLEY_ATTR_SA_LD_TYPE_SEC
);
2682 if (sa
->lifetime
> 0xffff) {
2683 p
= isakmp_set_attr_v(p
, OAKLEY_ATTR_SA_LD
,
2687 p
= isakmp_set_attr_l(p
, OAKLEY_ATTR_SA_LD
,
2694 u_int32_t lifebyte
= htonl((u_int32_t
)sa
->lifebyte
);
2696 attrlen
+= sizeof(struct isakmp_data
)
2697 + sizeof(struct isakmp_data
);
2698 if (sa
->lifebyte
> 0xffff)
2699 attrlen
+= sizeof(lifebyte
);
2701 p
= isakmp_set_attr_l(p
, OAKLEY_ATTR_SA_LD_TYPE
,
2702 OAKLEY_ATTR_SA_LD_TYPE_KB
);
2703 if (sa
->lifebyte
> 0xffff) {
2704 p
= isakmp_set_attr_v(p
, OAKLEY_ATTR_SA_LD
,
2708 p
= isakmp_set_attr_l(p
, OAKLEY_ATTR_SA_LD
,
2715 attrlen
+= sizeof(struct isakmp_data
);
2717 p
= isakmp_set_attr_l(p
, OAKLEY_ATTR_ENC_ALG
, sa
->enctype
);
2720 attrlen
+= sizeof(struct isakmp_data
);
2722 p
= isakmp_set_attr_l(p
, OAKLEY_ATTR_KEY_LEN
, sa
->encklen
);
2724 if (sa
->authmethod
) {
2727 #ifdef ENABLE_HYBRID
2728 authmethod
= switch_authmethod(sa
->authmethod
);
2730 authmethod
= sa
->authmethod
;
2732 attrlen
+= sizeof(struct isakmp_data
);
2734 p
= isakmp_set_attr_l(p
, OAKLEY_ATTR_AUTH_METHOD
, authmethod
);
2737 attrlen
+= sizeof(struct isakmp_data
);
2739 p
= isakmp_set_attr_l(p
, OAKLEY_ATTR_HASH_ALG
, sa
->hashtype
);
2741 switch (sa
->dh_group
) {
2742 case OAKLEY_ATTR_GRP_DESC_MODP768
:
2743 case OAKLEY_ATTR_GRP_DESC_MODP1024
:
2744 case OAKLEY_ATTR_GRP_DESC_MODP1536
:
2745 case OAKLEY_ATTR_GRP_DESC_MODP2048
:
2746 case OAKLEY_ATTR_GRP_DESC_MODP3072
:
2747 case OAKLEY_ATTR_GRP_DESC_MODP4096
:
2748 case OAKLEY_ATTR_GRP_DESC_MODP6144
:
2749 case OAKLEY_ATTR_GRP_DESC_MODP8192
:
2750 /* don't attach group type for known groups */
2751 attrlen
+= sizeof(struct isakmp_data
);
2753 p
= isakmp_set_attr_l(p
, OAKLEY_ATTR_GRP_DESC
,
2757 case OAKLEY_ATTR_GRP_DESC_EC2N155
:
2758 case OAKLEY_ATTR_GRP_DESC_EC2N185
:
2759 /* don't attach group type for known groups */
2760 attrlen
+= sizeof(struct isakmp_data
);
2762 p
= isakmp_set_attr_l(p
, OAKLEY_ATTR_GRP_TYPE
,
2763 OAKLEY_ATTR_GRP_TYPE_EC2N
);
2775 setph2proposal0(iph2
, pp
, pr
)
2776 const phase2_handle_t
*iph2
;
2777 const struct saprop
*pp
;
2778 const struct saproto
*pr
;
2781 struct isakmp_pl_p
*prop
;
2782 struct isakmp_pl_t
*trns
;
2787 u_int8_t
*np_t
; /* pointer next trns type in previous header */
2788 const u_int8_t
*spi
;
2790 p
= vmalloc(sizeof(*prop
) + sizeof(pr
->spi
));
2794 /* create proposal */
2795 prop
= (struct isakmp_pl_p
*)p
->v
;
2796 prop
->h
.np
= ISAKMP_NPTYPE_NONE
;
2797 prop
->p_no
= pp
->prop_no
;
2798 prop
->proto_id
= pr
->proto_id
;
2801 spi
= (const u_int8_t
*)&pr
->spi
;
2802 switch (pr
->proto_id
) {
2803 case IPSECDOI_PROTO_IPCOMP
:
2805 * draft-shacham-ippcp-rfc2393bis-05.txt:
2806 * construct 16bit SPI (CPI).
2807 * XXX we may need to provide a configuration option to
2808 * generate 32bit SPI. otherwise we cannot interoeprate
2809 * with nodes that uses 32bit SPI, in case we are initiator.
2811 prop
->spi_size
= sizeof(u_int16_t
);
2812 spi
+= sizeof(pr
->spi
) - sizeof(u_int16_t
);
2813 p
->l
-= sizeof(pr
->spi
);
2814 p
->l
+= sizeof(u_int16_t
);
2817 prop
->spi_size
= sizeof(pr
->spi
);
2820 memcpy(prop
+ 1, spi
, prop
->spi_size
);
2822 /* create transform */
2823 trnsoff
= sizeof(*prop
) + prop
->spi_size
;
2826 for (tr
= pr
->head
; tr
; tr
= tr
->next
) {
2828 switch (pr
->proto_id
) {
2829 case IPSECDOI_PROTO_IPSEC_ESP
:
2831 * don't build a null encryption
2832 * with no authentication transform.
2834 if (tr
->trns_id
== IPSECDOI_ESP_NULL
&&
2835 tr
->authtype
== IPSECDOI_ATTR_AUTH_NONE
)
2841 *np_t
= ISAKMP_NPTYPE_T
;
2845 /* get attribute length */
2848 attrlen
+= sizeof(struct isakmp_data
)
2849 + sizeof(struct isakmp_data
);
2850 if (pp
->lifetime
> 0xffff)
2851 attrlen
+= sizeof(u_int32_t
);
2853 if (pp
->lifebyte
&& pp
->lifebyte
!= IPSECDOI_ATTR_SA_LD_KB_MAX
) {
2854 attrlen
+= sizeof(struct isakmp_data
)
2855 + sizeof(struct isakmp_data
);
2856 if (pp
->lifebyte
> 0xffff)
2857 attrlen
+= sizeof(u_int32_t
);
2859 attrlen
+= sizeof(struct isakmp_data
); /* enc mode */
2861 attrlen
+= sizeof(struct isakmp_data
);
2863 switch (pr
->proto_id
) {
2864 case IPSECDOI_PROTO_IPSEC_ESP
:
2865 /* non authentication mode ? */
2866 if (tr
->authtype
!= IPSECDOI_ATTR_AUTH_NONE
)
2867 attrlen
+= sizeof(struct isakmp_data
);
2869 case IPSECDOI_PROTO_IPSEC_AH
:
2870 if (tr
->authtype
== IPSECDOI_ATTR_AUTH_NONE
) {
2872 "no authentication algorithm found "
2873 "but protocol is AH.\n");
2877 attrlen
+= sizeof(struct isakmp_data
);
2879 case IPSECDOI_PROTO_IPCOMP
:
2883 "invalid protocol: %d\n", pr
->proto_id
);
2888 if (alg_oakley_dhdef_ok(iph2
->sainfo
->pfs_group
))
2889 attrlen
+= sizeof(struct isakmp_data
);
2891 p
= vrealloc(p
, p
->l
+ sizeof(*trns
) + attrlen
);
2894 prop
= (struct isakmp_pl_p
*)p
->v
;
2896 /* set transform's values */
2897 trns
= (struct isakmp_pl_t
*)(p
->v
+ trnsoff
);
2898 trns
->h
.np
= ISAKMP_NPTYPE_NONE
;
2899 trns
->t_no
= tr
->trns_no
;
2900 trns
->t_id
= tr
->trns_id
;
2902 /* set attributes */
2903 x
= x0
= p
->v
+ trnsoff
+ sizeof(*trns
);
2906 x
= isakmp_set_attr_l(x
, IPSECDOI_ATTR_SA_LD_TYPE
,
2907 IPSECDOI_ATTR_SA_LD_TYPE_SEC
);
2908 if (pp
->lifetime
> 0xffff) {
2909 u_int32_t v
= htonl((u_int32_t
)pp
->lifetime
);
2910 x
= isakmp_set_attr_v(x
, IPSECDOI_ATTR_SA_LD
,
2911 (caddr_t
)&v
, sizeof(v
));
2913 x
= isakmp_set_attr_l(x
, IPSECDOI_ATTR_SA_LD
,
2918 if (pp
->lifebyte
&& pp
->lifebyte
!= IPSECDOI_ATTR_SA_LD_KB_MAX
) {
2919 x
= isakmp_set_attr_l(x
, IPSECDOI_ATTR_SA_LD_TYPE
,
2920 IPSECDOI_ATTR_SA_LD_TYPE_KB
);
2921 if (pp
->lifebyte
> 0xffff) {
2922 u_int32_t v
= htonl((u_int32_t
)pp
->lifebyte
);
2923 x
= isakmp_set_attr_v(x
, IPSECDOI_ATTR_SA_LD
,
2924 (caddr_t
)&v
, sizeof(v
));
2926 x
= isakmp_set_attr_l(x
, IPSECDOI_ATTR_SA_LD
,
2931 x
= isakmp_set_attr_l(x
, IPSECDOI_ATTR_ENC_MODE
, pr
->encmode
);
2934 x
= isakmp_set_attr_l(x
, IPSECDOI_ATTR_KEY_LENGTH
, tr
->encklen
);
2936 /* mandatory check has done above. */
2937 if ((pr
->proto_id
== IPSECDOI_PROTO_IPSEC_ESP
&& tr
->authtype
!= IPSECDOI_ATTR_AUTH_NONE
)
2938 || pr
->proto_id
== IPSECDOI_PROTO_IPSEC_AH
)
2939 x
= isakmp_set_attr_l(x
, IPSECDOI_ATTR_AUTH
, tr
->authtype
);
2941 if (alg_oakley_dhdef_ok(iph2
->sainfo
->pfs_group
))
2942 x
= isakmp_set_attr_l(x
, IPSECDOI_ATTR_GRP_DESC
,
2943 iph2
->sainfo
->pfs_group
);
2945 /* update length of this transform. */
2946 trns
= (struct isakmp_pl_t
*)(p
->v
+ trnsoff
);
2947 trns
->h
.len
= htons(sizeof(*trns
) + attrlen
);
2949 /* save buffer to pre-next payload */
2952 trnsoff
+= (sizeof(*trns
) + attrlen
);
2957 "no suitable proposal was created.\n");
2961 /* update length of this protocol. */
2962 prop
->h
.len
= htons(p
->l
);
2968 * create phase2 proposal from policy configuration.
2969 * NOT INCLUDING isakmp general header of SA payload.
2970 * This function is called by initiator only.
2973 ipsecdoi_setph2proposal(phase2_handle_t
*iph2
, int return_sa
)
2975 struct saprop
*proposal
, *a
;
2976 struct saproto
*b
= NULL
;
2977 vchar_t
*q
, *sa
= NULL
;
2978 struct isakmp_pl_p
*prop
;
2979 size_t propoff
; /* for previous field of type of next payload. */
2982 proposal
= iph2
->approval
;
2984 proposal
= iph2
->proposal
;
2986 if (iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
2987 struct ipsecdoi_sa_b
*sab
;
2989 sa
= vmalloc(sizeof(*sab
));
2992 "failed to allocate my sa buffer\n");
2996 /* create SA payload */
2997 sab
= ALIGNED_CAST(struct ipsecdoi_sa_b
*)sa
->v
;
2998 sab
->doi
= htonl(IPSEC_DOI
);
2999 sab
->sit
= htonl(IPSECDOI_SIT_IDENTITY_ONLY
); /* XXX configurable ? */
3005 for (a
= proposal
; a
; a
= a
->next
) {
3006 for (b
= a
->head
; b
; b
= b
->next
) {
3007 if (b
->proto_id
== IPSECDOI_PROTO_IPCOMP
) {
3008 // skip this - not specified in the SA
3009 // Need to set this in iph2 ???
3012 // IKEv1 sends encode mode in SA - uses diferent codes when NATT being used
3014 if (iph2
->ph1
->natt_flags
& NAT_DETECTED
) {
3015 plog (ASL_LEVEL_NOTICE
, "NAT detected -> UDP encapsulation\n");
3017 if (iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
3018 int udp_diff
= iph2
->ph1
->natt_options
->mode_udp_diff
;
3019 /* Tunnel -> UDP-Tunnel, Transport -> UDP_Transport */
3020 b
->encmode
+= udp_diff
;
3024 switch (iph2
->version
) {
3025 case ISAKMP_VERSION_NUMBER_IKEV1
:
3026 q
= setph2proposal0(iph2
, a
, b
);
3029 plog(ASL_LEVEL_ERR
, "Invalid IKE version detected\n");
3038 sa
= vrealloc(sa
, sa
->l
+ q
->l
);
3044 "failed to allocate my sa buffer\n");
3049 memcpy(sa
->v
+ sa
->l
- q
->l
, q
->v
, q
->l
);
3051 prop
= (struct isakmp_pl_p
*)(sa
->v
+
3053 if (iph2
->version
== ISAKMP_VERSION_NUMBER_IKEV1
)
3054 prop
->h
.np
= ISAKMP_NPTYPE_P
;
3056 propoff
= sa
->l
- q
->l
;
3069 * return 1 if all of the given protocols are tunnel mode.
3072 ipsecdoi_tunnelmode(iph2
)
3073 phase2_handle_t
*iph2
;
3076 struct saproto
*pr
= NULL
;
3078 for (pp
= iph2
->proposal
; pp
; pp
= pp
->next
) {
3079 for (pr
= pp
->head
; pr
; pr
= pr
->next
) {
3080 if (pr
->encmode
!= IPSECDOI_ATTR_ENC_MODE_TUNNEL
&&
3081 pr
->encmode
!= IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_RFC
&&
3082 pr
->encmode
!= IPSECDOI_ATTR_ENC_MODE_UDPTUNNEL_DRAFT
)
3091 * return 1 if any of the given protocols are transport mode.
3094 ipsecdoi_any_transportmode(pp
)
3097 struct saproto
*pr
= NULL
;
3099 for (; pp
; pp
= pp
->next
) {
3100 for (pr
= pp
->head
; pr
; pr
= pr
->next
) {
3101 if (pr
->encmode
== IPSECDOI_ATTR_ENC_MODE_TRNS
||
3102 pr
->encmode
== IPSECDOI_ATTR_ENC_MODE_UDPTRNS_RFC
||
3103 pr
->encmode
== IPSECDOI_ATTR_ENC_MODE_UDPTRNS_DRAFT
)
3112 * return 1 if all of the given protocols are transport mode.
3115 ipsecdoi_transportmode(pp
)
3118 struct saproto
*pr
= NULL
;
3120 for (; pp
; pp
= pp
->next
) {
3121 for (pr
= pp
->head
; pr
; pr
= pr
->next
) {
3122 if (pr
->encmode
!= IPSECDOI_ATTR_ENC_MODE_TRNS
)
3131 ipsecdoi_get_defaultlifetime()
3133 return IPSECDOI_ATTR_SA_LD_SEC_DEFAULT
;
3137 ipsecdoi_checkalgtypes(proto_id
, enc
, auth
, comp
)
3138 int proto_id
, enc
, auth
, comp
;
3140 #define TMPALGTYPE2STR(n) s_algtype(algclass_ipsec_##n, n)
3142 case IPSECDOI_PROTO_IPSEC_ESP
:
3143 if (enc
== 0 || comp
!= 0) {
3145 "illegal algorithm defined "
3146 "ESP enc=%s auth=%s comp=%s.\n",
3147 TMPALGTYPE2STR(enc
),
3148 TMPALGTYPE2STR(auth
),
3149 TMPALGTYPE2STR(comp
));
3153 case IPSECDOI_PROTO_IPSEC_AH
:
3154 if (enc
!= 0 || auth
== 0 || comp
!= 0) {
3156 "illegal algorithm defined "
3157 "AH enc=%s auth=%s comp=%s.\n",
3158 TMPALGTYPE2STR(enc
),
3159 TMPALGTYPE2STR(auth
),
3160 TMPALGTYPE2STR(comp
));
3164 case IPSECDOI_PROTO_IPCOMP
:
3165 if (enc
!= 0 || auth
!= 0 || comp
== 0) {
3167 "illegal algorithm defined "
3168 "IPcomp enc=%s auth=%s comp=%s.\n",
3169 TMPALGTYPE2STR(enc
),
3170 TMPALGTYPE2STR(auth
),
3171 TMPALGTYPE2STR(comp
));
3177 "invalid ipsec protocol %d\n", proto_id
);
3180 #undef TMPALGTYPE2STR
3190 return IPSECDOI_PROTO_IPSEC_AH
;
3192 return IPSECDOI_PROTO_IPSEC_ESP
;
3193 case IPPROTO_IPCOMP
:
3194 return IPSECDOI_PROTO_IPCOMP
;
3196 return -1; /* XXX */
3204 case IPSECDOI_PROTO_IPSEC_AH
:
3206 case IPSECDOI_PROTO_IPSEC_ESP
:
3208 case IPSECDOI_PROTO_IPCOMP
:
3209 return IPPROTO_IPCOMP
;
3211 return -1; /* XXX */
3215 * Check if a subnet id is valid for comparison
3216 * with an address id ( address length mask )
3224 ipsecdoi_subnetisaddr_v4( subnet
, address
)
3225 const vchar_t
*subnet
;
3226 const vchar_t
*address
;
3228 struct in_addr
*mask
;
3230 if (address
->l
!= sizeof(struct in_addr
))
3233 if (subnet
->l
!= (sizeof(struct in_addr
)*2))
3236 mask
= ALIGNED_CAST(struct in_addr
*)(subnet
->v
+ sizeof(struct in_addr
));
3238 if (mask
->s_addr
!=0xffffffff)
3241 return memcmp(subnet
->v
,address
->v
,address
->l
);
3247 ipsecdoi_subnetisaddr_v6( subnet
, address
)
3248 const vchar_t
*subnet
;
3249 const vchar_t
*address
;
3251 struct in6_addr
*mask
;
3254 if (address
->l
!= sizeof(struct in6_addr
))
3257 if (subnet
->l
!= (sizeof(struct in6_addr
)*2))
3260 mask
= ALIGNED_CAST(struct in6_addr
*)(subnet
->v
+ sizeof(struct in6_addr
));
3262 for (i
=0; i
<16; i
++)
3263 if(mask
->s6_addr
[i
]!=0xff)
3266 return memcmp(subnet
->v
,address
->v
,address
->l
);
3273 * Check and Compare two IDs
3274 * - specify 0 for exact if wildcards are allowed
3278 * = -1 for integrity error
3282 ipsecdoi_chkcmpids( idt
, ids
, exact
)
3283 const vchar_t
*idt
; /* id cmp target */
3284 const vchar_t
*ids
; /* id cmp source */
3287 struct ipsecdoi_id_b
*id_bt
;
3288 struct ipsecdoi_id_b
*id_bs
;
3293 /* handle wildcard IDs */
3295 if (idt
== NULL
|| ids
== NULL
)
3299 plog(ASL_LEVEL_DEBUG
,
3300 "check and compare ids : values matched (ANONYMOUS)\n" );
3305 plog(ASL_LEVEL_DEBUG
,
3306 "check and compare ids : value mismatch (ANONYMOUS)\n" );
3311 /* make sure the ids are of the same type */
3313 id_bt
= (struct ipsecdoi_id_b
*) idt
->v
;
3314 id_bs
= (struct ipsecdoi_id_b
*) ids
->v
;
3316 ident_t
.v
= idt
->v
+ sizeof(*id_bt
);
3317 ident_t
.l
= idt
->l
- sizeof(*id_bt
);
3318 ident_s
.v
= ids
->v
+ sizeof(*id_bs
);
3319 ident_s
.l
= ids
->l
- sizeof(*id_bs
);
3321 if (id_bs
->type
!= id_bt
->type
)
3324 * special exception for comparing
3325 * address to subnet id types when
3326 * the netmask is address length
3329 if ((id_bs
->type
== IPSECDOI_ID_IPV4_ADDR
)&&
3330 (id_bt
->type
== IPSECDOI_ID_IPV4_ADDR_SUBNET
)) {
3331 result
= ipsecdoi_subnetisaddr_v4(&ident_t
,&ident_s
);
3335 if ((id_bs
->type
== IPSECDOI_ID_IPV4_ADDR_SUBNET
)&&
3336 (id_bt
->type
== IPSECDOI_ID_IPV4_ADDR
)) {
3337 result
= ipsecdoi_subnetisaddr_v4(&ident_s
,&ident_t
);
3342 if ((id_bs
->type
== IPSECDOI_ID_IPV6_ADDR
)&&
3343 (id_bt
->type
== IPSECDOI_ID_IPV6_ADDR_SUBNET
)) {
3344 result
= ipsecdoi_subnetisaddr_v6(&ident_t
,&ident_s
);
3348 if ((id_bs
->type
== IPSECDOI_ID_IPV6_ADDR_SUBNET
)&&
3349 (id_bt
->type
== IPSECDOI_ID_IPV6_ADDR
)) {
3350 result
= ipsecdoi_subnetisaddr_v6(&ident_s
,&ident_t
);
3354 plog(ASL_LEVEL_DEBUG
,
3355 "check and compare ids : id type mismatch %s != %s\n",
3356 s_ipsecdoi_ident(id_bs
->type
),
3357 s_ipsecdoi_ident(id_bt
->type
));
3362 if(id_bs
->proto_id
!= id_bt
->proto_id
){
3363 plog(ASL_LEVEL_DEBUG
,
3364 "check and compare ids : proto_id mismatch %d != %d\n",
3365 id_bs
->proto_id
, id_bt
->proto_id
);
3370 /* compare the ID data. */
3372 switch (id_bt
->type
) {
3373 case IPSECDOI_ID_DER_ASN1_DN
:
3374 case IPSECDOI_ID_DER_ASN1_GN
:
3375 /* compare asn1 ids */
3376 result
= eay_cmp_asn1dn(&ident_t
, &ident_s
);
3379 case IPSECDOI_ID_IPV4_ADDR
:
3380 /* validate lengths */
3381 if ((ident_t
.l
!= sizeof(struct in_addr
))||
3382 (ident_s
.l
!= sizeof(struct in_addr
)))
3386 case IPSECDOI_ID_IPV4_ADDR_SUBNET
:
3387 case IPSECDOI_ID_IPV4_ADDR_RANGE
:
3388 /* validate lengths */
3389 if ((ident_t
.l
!= (sizeof(struct in_addr
)*2))||
3390 (ident_s
.l
!= (sizeof(struct in_addr
)*2)))
3395 case IPSECDOI_ID_IPV6_ADDR
:
3396 /* validate lengths */
3397 if ((ident_t
.l
!= sizeof(struct in6_addr
))||
3398 (ident_s
.l
!= sizeof(struct in6_addr
)))
3402 case IPSECDOI_ID_IPV6_ADDR_SUBNET
:
3403 case IPSECDOI_ID_IPV6_ADDR_RANGE
:
3404 /* validate lengths */
3405 if ((ident_t
.l
!= (sizeof(struct in6_addr
)*2))||
3406 (ident_s
.l
!= (sizeof(struct in6_addr
)*2)))
3410 case IPSECDOI_ID_FQDN
:
3411 case IPSECDOI_ID_USER_FQDN
:
3412 case IPSECDOI_ID_KEY_ID
:
3417 "Unhandled id type %i specified for comparison\n",
3422 /* validate matching data and length */
3423 if (ident_t
.l
== ident_s
.l
)
3424 result
= memcmp(ident_t
.v
,ident_s
.v
,ident_t
.l
);
3430 /* debug level output */
3431 if(loglevel
>= ASL_LEVEL_DEBUG
) {
3432 char *idstrt
= ipsecdoi_id2str(idt
);
3433 char *idstrs
= ipsecdoi_id2str(ids
);
3436 plog(ASL_LEVEL_DEBUG
,
3437 "check and compare ids : values matched (%s)\n",
3438 s_ipsecdoi_ident(id_bs
->type
) );
3440 plog(ASL_LEVEL_DEBUG
,
3441 "check and compare ids : value mismatch (%s)\n",
3442 s_ipsecdoi_ident(id_bs
->type
));
3444 plog(ASL_LEVEL_DEBUG
, "cmpid target: \'%s\'\n", idstrt
);
3445 plog(ASL_LEVEL_DEBUG
, "cmpid source: \'%s\'\n", idstrs
);
3447 racoon_free(idstrs
);
3448 racoon_free(idstrt
);
3459 /* id integrity error */
3460 plog(ASL_LEVEL_DEBUG
, "check and compare ids : %s integrity error\n",
3461 s_ipsecdoi_ident(id_bs
->type
));
3462 plog(ASL_LEVEL_DEBUG
, "cmpid target: length = \'%zu\'\n", ident_t
.l
);
3463 plog(ASL_LEVEL_DEBUG
, "cmpid source: length = \'%zu\'\n", ident_s
.l
);
3470 * check the following:
3471 * - In main mode with pre-shared key, only address type can be used.
3472 * - if proper type for phase 1 ?
3473 * - if phase 1 ID payload conformed RFC2407 4.6.2.
3474 * (proto, port) must be (0, 0), (udp, 500) or (udp, [specified]).
3475 * - if ID payload sent from peer is equal to the ID expected by me.
3477 * both of "id" and "id_p" should be ID payload without general header,
3480 ipsecdoi_checkid1(iph1
)
3481 phase1_handle_t
*iph1
;
3483 struct ipsecdoi_id_b
*id_b
;
3484 struct sockaddr_storage
*sa
;
3487 if (iph1
->id_p
== NULL
) {
3489 "invalid iph1 passed id_p == NULL\n");
3490 return ISAKMP_INTERNAL_ERROR
;
3492 if (iph1
->id_p
->l
< sizeof(*id_b
)) {
3494 "invalid value passed as \"ident\" (len=%lu)\n",
3495 (u_long
)iph1
->id_p
->l
);
3496 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
3499 id_b
= ALIGNED_CAST(struct ipsecdoi_id_b
*)iph1
->id_p
->v
;
3501 /* In main mode with pre-shared key, only address type can be used.
3502 * If NAT Traversal being used and peer is behind nat and
3503 * natt version = 02 - allow non-address ID type.
3505 if (iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
3506 && iph1
->etype
== ISAKMP_ETYPE_IDENT
3507 && iph1
->approval
->authmethod
== OAKLEY_ATTR_AUTH_METHOD_PSKEY
3509 && (iph1
->natt_flags
& NAT_DETECTED_PEER
) == 0
3512 if (id_b
->type
!= IPSECDOI_ID_IPV4_ADDR
3513 && id_b
->type
!= IPSECDOI_ID_IPV6_ADDR
) {
3515 "Expecting IP address type in main mode, "
3516 "but %s.\n", s_ipsecdoi_ident(id_b
->type
));
3517 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
3521 /* if proper type for phase 1 ? */
3522 switch (id_b
->type
) {
3523 case IPSECDOI_ID_IPV4_ADDR_SUBNET
:
3524 case IPSECDOI_ID_IPV6_ADDR_SUBNET
:
3525 case IPSECDOI_ID_IPV4_ADDR_RANGE
:
3526 case IPSECDOI_ID_IPV6_ADDR_RANGE
:
3527 plog(ASL_LEVEL_WARNING
,
3528 "such ID type %s is not proper.\n",
3529 s_ipsecdoi_ident(id_b
->type
));
3533 /* if phase 1 ID payload conformed RFC2407 4.6.2. */
3534 if (id_b
->type
== IPSECDOI_ID_IPV4_ADDR
||
3535 id_b
->type
== IPSECDOI_ID_IPV6_ADDR
) {
3537 if (id_b
->proto_id
== 0 && ntohs(id_b
->port
) != 0) {
3538 plog(ASL_LEVEL_WARNING
,
3539 "protocol ID and Port mismatched. "
3540 "proto_id:%d port:%d\n",
3541 id_b
->proto_id
, ntohs(id_b
->port
));
3544 } else if (id_b
->proto_id
== IPPROTO_UDP
) {
3546 * copmaring with expected port.
3547 * always permit if port is equal to PORT_ISAKMP
3549 if (ntohs(id_b
->port
) != PORT_ISAKMP
) {
3553 switch (iph1
->remote
->ss_family
) {
3555 port
= ((struct sockaddr_in
*)iph1
->remote
)->sin_port
;
3559 port
= ((struct sockaddr_in6
*)iph1
->remote
)->sin6_port
;
3564 "invalid family: %d\n",
3565 iph1
->remote
->ss_family
);
3566 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
3568 if (ntohs(id_b
->port
) != port
) {
3569 plog(ASL_LEVEL_WARNING
,
3570 "port %d expected, but %d\n",
3571 port
, ntohs(id_b
->port
));
3578 /* compare with the ID if specified. */
3579 if (genlist_next(iph1
->rmconf
->idvl_p
, 0)) {
3580 vchar_t
*ident0
= NULL
;
3585 struct genlist_entry
*gpb
;
3587 for (id
= genlist_next (iph1
->rmconf
->idvl_p
, &gpb
); id
; id
= genlist_next (0, &gpb
)) {
3588 /* check the type of both IDs */
3589 if (id
->idtype
!= doi2idtype(id_b
->type
))
3590 continue; /* ID type mismatch */
3594 /* compare defined ID with the ID sent by peer. */
3597 ident0
= getidval(id
->idtype
, id
->id
);
3599 switch (id
->idtype
) {
3602 ident
.v
= iph1
->id_p
->v
+ sizeof(*id_b
);
3603 ident
.l
= iph1
->id_p
->l
- sizeof(*id_b
);
3604 if (eay_cmp_asn1dn(ident0
, &ident
) == 0)
3607 plog(ASL_LEVEL_WARNING
, "ASN1DN ID matching not implemented - passed.\n");
3608 goto matched
; //%%%%%% hack for now until we have code to do this.
3611 case IDTYPE_ADDRESS
:
3612 sa
= ALIGNED_CAST(struct sockaddr_storage
*)ident0
->v
;
3613 sa2
= (caddr_t
)(id_b
+ 1);
3614 switch (sa
->ss_family
) {
3616 if (iph1
->id_p
->l
- sizeof(*id_b
) != sizeof(struct in_addr
))
3617 continue; /* ID value mismatch */
3618 sa1
= (caddr_t
)&((struct sockaddr_in
*)sa
)->sin_addr
;
3619 if (memcmp(sa1
, sa2
, sizeof(struct in_addr
)) == 0)
3624 if (iph1
->id_p
->l
- sizeof(*id_b
) != sizeof(struct in6_addr
))
3625 continue; /* ID value mismatch */
3626 sa1
= (caddr_t
)&((struct sockaddr_in6
*)sa
)->sin6_addr
;
3627 if (memcmp(sa1
, sa2
, sizeof(struct in6_addr
)) == 0)
3636 if (memcmp(ident0
->v
, id_b
+ 1, ident0
->l
) == 0)
3641 if (ident0
!= NULL
) {
3645 plog(ASL_LEVEL_DEBUG
, "No ID match.\n");
3646 if (iph1
->rmconf
->verify_identifier
)
3647 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
3648 matched
: /* ID value match */
3656 /* HACK!!! - temporary until this prototype gets moved */
3657 extern CFDataRef
SecCertificateCopySubjectSequence( SecCertificateRef certificate
);
3660 * create ID payload for phase 1 and set into iph1->id.
3661 * NOT INCLUDING isakmp general header.
3662 * see, RFC2407 4.6.2.1
3665 ipsecdoi_setid1(iph1
)
3666 phase1_handle_t
*iph1
;
3668 vchar_t
*ret
= NULL
;
3669 struct ipsecdoi_id_b id_b
;
3670 vchar_t
*ident
= NULL
;
3671 struct sockaddr_in v4_address
;
3672 struct sockaddr_storage
*ipid
= NULL
;
3675 bzero(&id_b
, sizeof(id_b
));
3677 switch (iph1
->rmconf
->idvtype
) {
3679 id_b
.type
= IPSECDOI_ID_FQDN
;
3680 ident
= getidval(iph1
->rmconf
->idvtype
, iph1
->rmconf
->idv
);
3682 case IDTYPE_USERFQDN
:
3683 id_b
.type
= IPSECDOI_ID_USER_FQDN
;
3684 ident
= getidval(iph1
->rmconf
->idvtype
, iph1
->rmconf
->idv
);
3687 case IDTYPE_KEYIDUSE
:
3688 id_b
.type
= IPSECDOI_ID_KEY_ID
;
3689 ident
= getidval(iph1
->rmconf
->idvtype
, iph1
->rmconf
->idv
);
3692 id_b
.type
= IPSECDOI_ID_DER_ASN1_DN
;
3693 if (iph1
->rmconf
->idv
) {
3694 /* XXX it must be encoded to asn1dn. */
3695 ident
= vdup(iph1
->rmconf
->idv
);
3697 if (oakley_getmycert(iph1
) < 0) {
3699 "failed to get own CERT.\n");
3703 SecCertificateRef certificate
;
3708 certificate
= crypto_cssm_x509cert_CreateSecCertificateRef(&iph1
->cert
->cert
);
3709 if (certificate
== NULL
) {
3711 "failed to get SecCertificateRef\n");
3714 subject
= crypto_cssm_CopySubjectSequence(certificate
);
3715 if (subject
== NULL
) {
3717 "failed to get subjectName\n");
3718 CFRelease(certificate
);
3721 len
= CFDataGetLength(subject
);
3722 namePtr
= (UInt8
*)CFDataGetBytePtr(subject
);
3723 ident
= vmalloc(len
);
3724 if (ident
== NULL
) {
3726 "failed to get subjectName\n");
3727 CFRelease(certificate
);
3731 memcpy(ident
->v
, namePtr
, len
);
3732 CFRelease(certificate
);
3736 case IDTYPE_ADDRESS
:
3738 * if the value of the id type was set by the configuration
3739 * file, then use it. otherwise the value is get from local
3740 * ip address by using ike negotiation.
3742 if (iph1
->rmconf
->idv
)
3743 ipid
= ALIGNED_CAST(struct sockaddr_storage
*)iph1
->rmconf
->idv
->v
;
3754 if (ipid
->ss_family
== AF_INET6
&&
3755 iph1
->nat64_prefix
.length
) {
3756 memset(&v4_address
, 0, sizeof(v4_address
));
3757 v4_address
.sin_len
= sizeof(struct sockaddr_in
);
3758 v4_address
.sin_family
= AF_INET
;
3759 v4_address
.sin_port
= ((struct sockaddr_in6
*)ipid
)->sin6_port
;
3760 v4_address
.sin_addr
.s_addr
= 0;
3762 ipid
= ALIGNED_CAST(struct sockaddr_storage
*)&v4_address
;
3766 /* use IP address */
3767 switch (ipid
->ss_family
) {
3769 id_b
.type
= IPSECDOI_ID_IPV4_ADDR
;
3770 l
= sizeof(struct in_addr
);
3771 p
= (caddr_t
)&((struct sockaddr_in
*)ipid
)->sin_addr
;
3775 id_b
.type
= IPSECDOI_ID_IPV6_ADDR
;
3776 l
= sizeof(struct in6_addr
);
3777 p
= (caddr_t
)&((struct sockaddr_in6
*)ipid
)->sin6_addr
;
3782 "invalid address family.\n");
3785 if(iph1
->version
== ISAKMP_VERSION_NUMBER_IKEV1
){
3786 id_b
.proto_id
= IPPROTO_UDP
;
3787 id_b
.port
= htons(PORT_ISAKMP
);
3793 "failed to get ID buffer.\n");
3796 memcpy(ident
->v
, p
, ident
->l
);
3801 "failed to get ID buffer.\n");
3805 ret
= vmalloc(sizeof(id_b
) + ident
->l
);
3808 "failed to get ID buffer.\n");
3812 memcpy(ret
->v
, &id_b
, sizeof(id_b
));
3813 memcpy(ret
->v
+ sizeof(id_b
), ident
->v
, ident
->l
);
3817 plogdump(ASL_LEVEL_DEBUG
, iph1
->id
->v
, iph1
->id
->l
, "use ID type of %s\n", s_ipsecdoi_ident(id_b
.type
));
3825 plog(ASL_LEVEL_ERR
, "failed get my ID\n");
3834 vchar_t
*new = NULL
;
3838 else if (lcconf
->ident
[type
])
3839 new = vdup(lcconf
->ident
[type
]);
3844 /* it's only called by cfparse.y. */
3846 set_identifier(vpp
, type
, value
)
3847 vchar_t
**vpp
, *value
;
3850 return set_identifier_qual(vpp
, type
, value
, IDQUAL_UNSPEC
);
3854 set_identifier_qual(vpp
, type
, value
, qual
)
3855 vchar_t
**vpp
, *value
;
3859 vchar_t
*new = NULL
;
3861 /* simply return if value is null. */
3863 if( type
== IDTYPE_FQDN
|| type
== IDTYPE_USERFQDN
){
3865 "No %s\n", type
== IDTYPE_FQDN
? "fqdn":"user fqdn");
3873 case IDTYPE_USERFQDN
:
3876 "Empty %s\n", type
== IDTYPE_FQDN
? "fqdn":"user fqdn");
3879 case IDTYPE_KEYIDUSE
:
3880 #ifdef ENABLE_HYBRID
3883 /* length is adjusted since QUOTEDSTRING teminates NULL. */
3884 new = vmalloc(value
->l
- 1);
3887 memcpy(new->v
, value
->v
, new->l
);
3891 * If no qualifier is specified: IDQUAL_UNSPEC. It means
3892 * to use a file for backward compatibility sake.
3896 case IDQUAL_UNSPEC
: {
3901 fp
= fopen(value
->v
, "r");
3904 "can not open %s\n", value
->v
);
3908 while ((len
= fread(b
, 1, sizeof(b
), fp
)) != 0) {
3909 new = vrealloc(new, tlen
+ len
);
3914 memcpy(new->v
+ tlen
, b
, len
);
3922 new = vmalloc(value
->l
- 1);
3925 "can not allocate memory");
3928 memcpy(new->v
, value
->v
, new->l
);
3933 "unknown qualifier");
3938 case IDTYPE_ADDRESS
: {
3939 struct sockaddr_storage
*sa
;
3941 /* length is adjusted since QUOTEDSTRING teminates NULL. */
3945 sa
= str2saddr(value
->v
, NULL
);
3948 "invalid ip address %s\n", value
->v
);
3952 new = vmalloc(sysdep_sa_len((struct sockaddr
*)sa
));
3957 memcpy(new->v
, sa
, new->l
);
3962 plog(ASL_LEVEL_DEBUG
, "Setting ID type ASN1DN from string not supported\n");
3974 * create ID payload for phase 2, and set into iph2->id and id_p. There are
3975 * NOT INCLUDING isakmp general header.
3976 * this function is for initiator. responder will get to copy from payload.
3977 * responder ID type is always address type.
3978 * see, RFC2407 4.6.2.1
3981 ipsecdoi_setid2(iph2
)
3982 phase2_handle_t
*iph2
;
3984 struct secpolicy
*sp
;
3986 /* check there is phase 2 handler ? */
3987 sp
= getspbyspid(iph2
->spid
);
3990 "no policy found for spid:%u.\n", iph2
->spid
);
3994 struct sockaddr_in local_v4_address
;
3995 struct sockaddr_storage
*srcaddr
= &sp
->spidx
.src
;
3996 u_int8_t prefs
= sp
->spidx
.prefs
;
3997 if (sp
->spidx
.dst
.ss_family
== AF_INET6
&&
3998 iph2
->nat64_prefix
.length
) {
3999 memset(&local_v4_address
, 0, sizeof(local_v4_address
));
4000 local_v4_address
.sin_len
= sizeof(struct sockaddr_in
);
4001 local_v4_address
.sin_family
= AF_INET
;
4002 local_v4_address
.sin_port
= ((struct sockaddr_in6
*)&sp
->spidx
.src
)->sin6_port
;
4003 // Setting a fixed IPv4 address to avoid FATAL-ID issue with 0.0.0.0 IPv4 address
4004 inet_pton(AF_INET
, "192.168.2.2", &local_v4_address
.sin_addr
);
4005 srcaddr
= ALIGNED_CAST(struct sockaddr_storage
*)&local_v4_address
;
4008 iph2
->id
= ipsecdoi_sockaddr2id(srcaddr
,
4009 prefs
, sp
->spidx
.ul_proto
);
4010 if (iph2
->id
== NULL
) {
4012 "failed to get ID for %s\n",
4013 spidx2str(&sp
->spidx
));
4017 if (((ALIGNED_CAST(struct ipsecdoi_id_b
*)iph2
->id
->v
)->type
== IPSECDOI_ID_IPV4_ADDR
||
4018 (ALIGNED_CAST(struct ipsecdoi_id_b
*)iph2
->id
->v
)->type
== IPSECDOI_ID_IPV4_ADDR_SUBNET
) &&
4019 iph2
->side
== RESPONDER
&&
4020 iph2
->ph1
&& (iph2
->ph1
->natt_flags
& NAT_DETECTED_ME
) &&
4021 lcconf
->ext_nat_id
) {
4023 if (!(iph2
->id
= vdup(lcconf
->ext_nat_id
))) {
4028 plogdump(ASL_LEVEL_DEBUG
, iph2
->id
->v
, iph2
->id
->l
, "use local ID type %s\n",
4029 s_ipsecdoi_ident((ALIGNED_CAST(struct ipsecdoi_id_b
*)iph2
->id
->v
)->type
));
4032 struct sockaddr_in v4_address
;
4033 struct sockaddr_storage
*dstaddr
= &sp
->spidx
.dst
;
4034 u_int8_t prefd
= sp
->spidx
.prefd
;
4035 if (sp
->spidx
.dst
.ss_family
== AF_INET6
&&
4036 iph2
->nat64_prefix
.length
) {
4037 memset(&v4_address
, 0, sizeof(v4_address
));
4038 v4_address
.sin_len
= sizeof(struct sockaddr_in
);
4039 v4_address
.sin_family
= AF_INET
;
4040 v4_address
.sin_port
= ((struct sockaddr_in6
*)&sp
->spidx
.dst
)->sin6_port
;
4041 nw_nat64_extract_v4(&iph2
->nat64_prefix
, &((struct sockaddr_in6
*)&sp
->spidx
.dst
)->sin6_addr
, &v4_address
.sin_addr
);
4043 dstaddr
= ALIGNED_CAST(struct sockaddr_storage
*)&v4_address
;
4046 iph2
->id_p
= ipsecdoi_sockaddr2id(dstaddr
,
4047 prefd
, sp
->spidx
.ul_proto
);
4048 if (iph2
->id_p
== NULL
) {
4050 "failed to get ID for %s\n",
4051 spidx2str(&sp
->spidx
));
4055 plogdump(ASL_LEVEL_DEBUG
, iph2
->id_p
->v
, iph2
->id_p
->l
, "use remote ID type %s\n",
4056 s_ipsecdoi_ident((ALIGNED_CAST(struct ipsecdoi_id_b
*)iph2
->id_p
->v
)->type
));
4062 * set address type of ID.
4063 * NOT INCLUDING general header.
4066 ipsecdoi_sockaddr2id(saddr
, prefixlen
, ul_proto
)
4067 struct sockaddr_storage
*saddr
;
4072 int type
, len1
, len2
;
4077 * Q. When type is SUBNET, is it allowed to be ::1/128.
4078 * A. Yes. (consensus at bake-off)
4080 switch (saddr
->ss_family
) {
4082 len1
= sizeof(struct in_addr
);
4083 if (prefixlen
== (sizeof(struct in_addr
) << 3)) {
4084 type
= IPSECDOI_ID_IPV4_ADDR
;
4086 } else if (prefixlen
< (sizeof(struct in_addr
) << 3)) {
4087 type
= IPSECDOI_ID_IPV4_ADDR_SUBNET
;
4088 len2
= sizeof(struct in_addr
);
4091 "invalid prefix length: %d.\n", prefixlen
);
4094 sa
= (caddr_t
)&((struct sockaddr_in
*)(saddr
))->sin_addr
;
4095 port
= ((struct sockaddr_in
*)(saddr
))->sin_port
;
4099 len1
= sizeof(struct in6_addr
);
4100 if (prefixlen
== (sizeof(struct in6_addr
) << 3)) {
4101 type
= IPSECDOI_ID_IPV6_ADDR
;
4103 } else if (prefixlen
< (sizeof(struct in6_addr
) << 3)) {
4104 type
= IPSECDOI_ID_IPV6_ADDR_SUBNET
;
4105 len2
= sizeof(struct in6_addr
);
4108 "invalid prefix length: %d.\n", prefixlen
);
4111 sa
= (caddr_t
)&((struct sockaddr_in6
*)(saddr
))->sin6_addr
;
4112 port
= ((struct sockaddr_in6
*)(saddr
))->sin6_port
;
4117 "invalid family: %d.\n", saddr
->ss_family
);
4122 new = vmalloc(sizeof(struct ipsecdoi_id_b
) + len1
+ len2
);
4125 "failed to get ID buffer.\n");
4129 memset(new->v
, 0, new->l
);
4131 /* set the part of header. */
4132 (ALIGNED_CAST(struct ipsecdoi_id_b
*)new->v
)->type
= type
;
4134 /* set ul_proto and port */
4136 * NOTE: we use both IPSEC_ULPROTO_ANY and IPSEC_PORT_ANY as wild card
4137 * because 0 means port number of 0. Instead of 0, we use IPSEC_*_ANY.
4139 (ALIGNED_CAST(struct ipsecdoi_id_b
*)new->v
)->proto_id
=
4140 ul_proto
== IPSEC_ULPROTO_ANY
? 0 : ul_proto
;
4141 (ALIGNED_CAST(struct ipsecdoi_id_b
*)new->v
)->port
=
4142 port
== IPSEC_PORT_ANY
? 0 : port
;
4143 memcpy(new->v
+ sizeof(struct ipsecdoi_id_b
), sa
, len1
);
4149 u_char
*p
= (unsigned char *) new->v
+
4150 sizeof(struct ipsecdoi_id_b
) + len1
;
4151 u_int bits
= prefixlen
;
4159 *p
= ~((1 << (8 - bits
)) - 1);
4166 ipsecdoi_sockrange2id(laddr
, haddr
, ul_proto
)
4167 struct sockaddr_storage
*laddr
, *haddr
;
4171 int type
, len1
, len2
;
4174 if (laddr
->ss_family
!= haddr
->ss_family
) {
4175 plog(ASL_LEVEL_ERR
, "Address family mismatch\n");
4179 switch (laddr
->ss_family
) {
4181 type
= IPSECDOI_ID_IPV4_ADDR_RANGE
;
4182 len1
= sizeof(struct in_addr
);
4183 len2
= sizeof(struct in_addr
);
4187 type
= IPSECDOI_ID_IPV6_ADDR_RANGE
;
4188 len1
= sizeof(struct in6_addr
);
4189 len2
= sizeof(struct in6_addr
);
4194 "invalid family: %d.\n", laddr
->ss_family
);
4199 new = vmalloc(sizeof(struct ipsecdoi_id_b
) + len1
+ len2
);
4202 "failed to get ID buffer.\n");
4206 memset(new->v
, 0, new->l
);
4207 /* set the part of header. */
4208 (ALIGNED_CAST(struct ipsecdoi_id_b
*)new->v
)->type
= type
;
4210 /* set ul_proto and port */
4212 * NOTE: we use both IPSEC_ULPROTO_ANY and IPSEC_PORT_ANY as wild card
4213 * because 0 means port number of 0. Instead of 0, we use IPSEC_*_ANY.
4215 (ALIGNED_CAST(struct ipsecdoi_id_b
*)new->v
)->proto_id
=
4216 ul_proto
== IPSEC_ULPROTO_ANY
? 0 : ul_proto
;
4217 port
= ((struct sockaddr_in
*)(laddr
))->sin_port
;
4218 (ALIGNED_CAST(struct ipsecdoi_id_b
*)new->v
)->port
=
4219 port
== IPSEC_PORT_ANY
? 0 : port
;
4220 memcpy(new->v
+ sizeof(struct ipsecdoi_id_b
),
4221 (caddr_t
)&((struct sockaddr_in
*)(laddr
))->sin_addr
,
4223 memcpy(new->v
+ sizeof(struct ipsecdoi_id_b
) + len1
,
4224 (caddr_t
)&((struct sockaddr_in
*)haddr
)->sin_addr
,
4231 * create sockaddr_storage structure from ID payload (buf).
4232 * buffers (saddr, prefixlen, ul_proto) must be allocated.
4233 * see, RFC2407 4.6.2.1
4236 ipsecdoi_id2sockaddr(vchar_t
*buf
,
4237 struct sockaddr_storage
*saddr
,
4238 u_int8_t
*prefixlen
,
4239 u_int16_t
*ul_proto
,
4242 struct ipsecdoi_id_b
*id_b
= ALIGNED_CAST(struct ipsecdoi_id_b
*)buf
->v
;
4246 * When a ID payload of subnet type with a IP address of full bit
4247 * masked, it has to be processed as host address.
4248 * e.g. below 2 type are same.
4249 * type = ipv6 subnet, data = 2001::1/128
4250 * type = ipv6 address, data = 2001::1
4252 switch (id_b
->type
) {
4253 case IPSECDOI_ID_IPV4_ADDR
:
4254 case IPSECDOI_ID_IPV4_ADDR_SUBNET
:
4255 saddr
->ss_len
= sizeof(struct sockaddr_in
);
4256 saddr
->ss_family
= AF_INET
;
4257 ((struct sockaddr_in
*)saddr
)->sin_port
=
4260 : id_b
->port
); /* see sockaddr2id() */
4261 memcpy(&((struct sockaddr_in
*)saddr
)->sin_addr
,
4262 buf
->v
+ sizeof(*id_b
), sizeof(struct in_addr
));
4265 case IPSECDOI_ID_IPV6_ADDR
:
4266 case IPSECDOI_ID_IPV6_ADDR_SUBNET
:
4267 saddr
->ss_len
= sizeof(struct sockaddr_in6
);
4268 saddr
->ss_family
= AF_INET6
;
4269 ((struct sockaddr_in6
*)saddr
)->sin6_port
=
4272 : id_b
->port
); /* see sockaddr2id() */
4273 memcpy(&((struct sockaddr_in6
*)saddr
)->sin6_addr
,
4274 buf
->v
+ sizeof(*id_b
), sizeof(struct in6_addr
));
4279 "unsupported ID type %d\n", id_b
->type
);
4280 return ISAKMP_NTYPE_INVALID_ID_INFORMATION
;
4283 /* get prefix length */
4284 switch (id_b
->type
) {
4285 case IPSECDOI_ID_IPV4_ADDR
:
4286 plen
= sizeof(struct in_addr
) << 3;
4289 case IPSECDOI_ID_IPV6_ADDR
:
4290 plen
= sizeof(struct in6_addr
) << 3;
4293 case IPSECDOI_ID_IPV4_ADDR_SUBNET
:
4295 case IPSECDOI_ID_IPV6_ADDR_SUBNET
:
4300 int alen
= sizeof(struct in_addr
);
4302 switch (id_b
->type
) {
4303 case IPSECDOI_ID_IPV4_ADDR_SUBNET
:
4304 alen
= sizeof(struct in_addr
);
4307 case IPSECDOI_ID_IPV6_ADDR_SUBNET
:
4308 alen
= sizeof(struct in6_addr
);
4315 return ISAKMP_INTERNAL_ERROR
;
4317 /* get subnet mask length */
4321 p
= (unsigned char *) buf
->v
4322 + sizeof(struct ipsecdoi_id_b
)
4325 for (; *p
== 0xff; p
++) {
4348 if (version
== ISAKMP_VERSION_NUMBER_IKEV1
) {
4349 *ul_proto
= id_b
->proto_id
== 0 ? IPSEC_ULPROTO_ANY
: id_b
->proto_id
; /* see sockaddr2id() */
4356 * make printable string from ID payload except of general header.
4366 static char buf
[BUFLEN
];
4367 struct ipsecdoi_id_b
*id_b
= ALIGNED_CAST(struct ipsecdoi_id_b
*)id
->v
;
4368 struct sockaddr_storage saddr
;
4371 bzero(&saddr
, sizeof(saddr
));
4373 switch (id_b
->type
) {
4374 case IPSECDOI_ID_IPV4_ADDR
:
4375 case IPSECDOI_ID_IPV4_ADDR_SUBNET
:
4376 case IPSECDOI_ID_IPV4_ADDR_RANGE
:
4378 saddr
.ss_len
= sizeof(struct sockaddr_in
);
4379 saddr
.ss_family
= AF_INET
;
4380 ((struct sockaddr_in
*)&saddr
)->sin_port
= IPSEC_PORT_ANY
;
4381 memcpy(&((struct sockaddr_in
*)&saddr
)->sin_addr
,
4382 id
->v
+ sizeof(*id_b
), sizeof(struct in_addr
));
4385 case IPSECDOI_ID_IPV6_ADDR
:
4386 case IPSECDOI_ID_IPV6_ADDR_SUBNET
:
4387 case IPSECDOI_ID_IPV6_ADDR_RANGE
:
4388 saddr
.ss_len
= sizeof(struct sockaddr_in6
);
4389 saddr
.ss_family
= AF_INET6
;
4390 ((struct sockaddr_in6
*)&saddr
)->sin6_port
= IPSEC_PORT_ANY
;
4391 memcpy(&((struct sockaddr_in6
*)&saddr
)->sin6_addr
,
4392 id
->v
+ sizeof(*id_b
), sizeof(struct in6_addr
));
4393 ((struct sockaddr_in6
*)&saddr
)->sin6_scope_id
=
4394 (IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6
*)&saddr
)->sin6_addr
)
4395 ? (ALIGNED_CAST(struct sockaddr_in6
*)id_b
)->sin6_scope_id
4401 switch (id_b
->type
) {
4402 case IPSECDOI_ID_IPV4_ADDR
:
4404 case IPSECDOI_ID_IPV6_ADDR
:
4406 len
= snprintf( buf
, sizeof(buf
), "%s", saddrwop2str((struct sockaddr
*)&saddr
));
4409 case IPSECDOI_ID_IPV4_ADDR_SUBNET
:
4411 case IPSECDOI_ID_IPV6_ADDR_SUBNET
:
4416 int alen
= sizeof(struct in_addr
);
4418 switch (id_b
->type
) {
4419 case IPSECDOI_ID_IPV4_ADDR_SUBNET
:
4420 alen
= sizeof(struct in_addr
);
4423 case IPSECDOI_ID_IPV6_ADDR_SUBNET
:
4424 alen
= sizeof(struct in6_addr
);
4435 /* get subnet mask length */
4439 p
= (unsigned char *) id
->v
4440 + sizeof(struct ipsecdoi_id_b
)
4443 for (; *p
== 0xff; p
++) {
4462 len
= snprintf( buf
, sizeof(buf
), "%s/%i", saddrwop2str((struct sockaddr
*)&saddr
), plen
);
4466 case IPSECDOI_ID_IPV4_ADDR_RANGE
:
4468 len
= snprintf( buf
, sizeof(buf
), "%s-", saddrwop2str((struct sockaddr
*)&saddr
));
4470 saddr
.ss_len
= sizeof(struct sockaddr_in
);
4471 saddr
.ss_family
= AF_INET
;
4472 ((struct sockaddr_in
*)&saddr
)->sin_port
= IPSEC_PORT_ANY
;
4473 memcpy(&((struct sockaddr_in
*)&saddr
)->sin_addr
,
4474 id
->v
+ sizeof(*id_b
) + sizeof(struct in_addr
),
4475 sizeof(struct in_addr
));
4478 len
+= snprintf( buf
+ len
, sizeof(buf
) - len
, "%s", saddrwop2str((struct sockaddr
*)&saddr
));
4484 case IPSECDOI_ID_IPV6_ADDR_RANGE
:
4486 len
= snprintf( buf
, sizeof(buf
), "%s-", saddrwop2str((struct sockaddr
*)&saddr
));
4488 saddr
.ss_len
= sizeof(struct sockaddr_in6
);
4489 saddr
.ss_family
= AF_INET6
;
4490 ((struct sockaddr_in6
*)&saddr
)->sin6_port
= IPSEC_PORT_ANY
;
4491 memcpy(&((struct sockaddr_in6
*)&saddr
)->sin6_addr
,
4492 id
->v
+ sizeof(*id_b
) + sizeof(struct in6_addr
),
4493 sizeof(struct in6_addr
));
4494 ((struct sockaddr_in6
*)&saddr
)->sin6_scope_id
=
4495 (IN6_IS_ADDR_LINKLOCAL(&((struct sockaddr_in6
*)&saddr
)->sin6_addr
)
4496 ? (ALIGNED_CAST(struct sockaddr_in6
*)id_b
)->sin6_scope_id
4500 len
+= snprintf( buf
+ len
, sizeof(buf
) - len
, "%s", saddrwop2str((struct sockaddr
*)&saddr
));
4506 case IPSECDOI_ID_FQDN
:
4507 case IPSECDOI_ID_USER_FQDN
:
4508 len
= id
->l
- sizeof(*id_b
);
4511 memcpy(buf
, id
->v
+ sizeof(*id_b
), len
);
4514 case IPSECDOI_ID_DER_ASN1_DN
:
4515 case IPSECDOI_ID_DER_ASN1_GN
:
4518 X509_NAME
*xn
= NULL
;
4521 dat
= id
->v
+ sizeof(*id_b
);
4522 len
= id
->l
- sizeof(*id_b
);
4524 if (d2i_X509_NAME(&xn
, (void*) &dat
, len
) != NULL
) {
4525 BIO
*bio
= BIO_new(BIO_s_mem());
4526 X509_NAME_print_ex(bio
, xn
, 0, 0);
4527 len
= BIO_get_mem_data(bio
, &dat
);
4530 memcpy(buf
,dat
,len
);
4538 "unable to extract asn1dn from id\n");
4540 len
= snprintf(buf
, sizeof(buf
), "<ASN1-DN>");
4546 /* currently unhandled id types */
4547 case IPSECDOI_ID_KEY_ID
:
4548 len
= snprintf( buf
, sizeof(buf
), "<KEY-ID>");
4553 "unknown ID type %d\n", id_b
->type
);
4557 len
= snprintf( buf
, sizeof(buf
), "<?>");
4559 ret
= racoon_malloc(len
+1);
4561 memcpy(ret
,buf
,len
);
4569 * set IPsec data attributes into a proposal.
4570 * NOTE: MUST called per a transform.
4573 ipsecdoi_t2satrns(t
, pp
, pr
, tr
)
4574 struct isakmp_pl_t
*t
;
4579 struct isakmp_data
*d
, *prev
;
4585 tr
->trns_no
= t
->t_no
;
4586 tr
->trns_id
= t
->t_id
;
4588 tlen
= ntohs(t
->h
.len
) - sizeof(*t
);
4589 prev
= (struct isakmp_data
*)NULL
;
4590 d
= (struct isakmp_data
*)(t
+ 1);
4593 life_t
= IPSECDOI_ATTR_SA_LD_TYPE_DEFAULT
;
4594 pp
->lifetime
= IPSECDOI_ATTR_SA_LD_SEC_DEFAULT
;
4596 tr
->authtype
= IPSECDOI_ATTR_AUTH_NONE
;
4600 type
= ntohs(d
->type
) & ~ISAKMP_GEN_MASK
;
4601 flag
= ntohs(d
->type
) & ISAKMP_GEN_MASK
;
4603 plog(ASL_LEVEL_DEBUG
,
4604 "type=%s, flag=0x%04x, lorv=%s\n",
4605 s_ipsecdoi_attr(type
), flag
,
4606 s_ipsecdoi_attr_v(type
, ntohs(d
->lorv
)));
4609 case IPSECDOI_ATTR_SA_LD_TYPE
:
4611 int type
= ntohs(d
->lorv
);
4613 case IPSECDOI_ATTR_SA_LD_TYPE_SEC
:
4614 case IPSECDOI_ATTR_SA_LD_TYPE_KB
:
4618 plog(ASL_LEVEL_WARNING
,
4619 "invalid life duration type. "
4621 life_t
= IPSECDOI_ATTR_SA_LD_TYPE_DEFAULT
;
4626 case IPSECDOI_ATTR_SA_LD
:
4628 || (ntohs(prev
->type
) & ~ISAKMP_GEN_MASK
) !=
4629 IPSECDOI_ATTR_SA_LD_TYPE
) {
4631 "life duration must follow ltype\n");
4637 vchar_t
*ld_buf
= NULL
;
4640 /* i.e. ISAKMP_GEN_TV */
4641 ld_buf
= vmalloc(sizeof(d
->lorv
));
4642 if (ld_buf
== NULL
) {
4644 "failed to get LD buffer.\n");
4647 memcpy(ld_buf
->v
, &d
->lorv
, sizeof(d
->lorv
));
4649 int len
= ntohs(d
->lorv
);
4650 /* i.e. ISAKMP_GEN_TLV */
4651 ld_buf
= vmalloc(len
);
4652 if (ld_buf
== NULL
) {
4654 "failed to get LD buffer.\n");
4657 memcpy(ld_buf
->v
, d
+ 1, len
);
4660 case IPSECDOI_ATTR_SA_LD_TYPE_SEC
:
4661 t
= ipsecdoi_set_ld(ld_buf
);
4665 "invalid life duration.\n");
4668 /* lifetime must be equal in a proposal. */
4669 if (pp
->lifetime
== IPSECDOI_ATTR_SA_LD_SEC_DEFAULT
)
4671 else if (pp
->lifetime
!= t
) {
4673 "lifetime mismatched "
4675 "prev:%ld curr:%u.\n",
4676 (long)pp
->lifetime
, t
);
4680 case IPSECDOI_ATTR_SA_LD_TYPE_KB
:
4681 t
= ipsecdoi_set_ld(ld_buf
);
4685 "invalid life duration.\n");
4688 /* lifebyte must be equal in a proposal. */
4689 if (pp
->lifebyte
== 0)
4691 else if (pp
->lifebyte
!= t
) {
4693 "lifebyte mismatched "
4695 "prev:%d curr:%u.\n",
4703 "invalid life type: %d\n", life_t
);
4709 case IPSECDOI_ATTR_GRP_DESC
:
4711 * RFC2407: 4.5 IPSEC Security Association Attributes
4712 * Specifies the Oakley Group to be used in a PFS QM
4713 * negotiation. For a list of supported values, see
4714 * Appendix A of [IKE].
4716 if (pp
->pfs_group
== 0)
4717 pp
->pfs_group
= (u_int16_t
)ntohs(d
->lorv
);
4718 else if (pp
->pfs_group
!= (u_int16_t
)ntohs(d
->lorv
)) {
4720 "pfs_group mismatched "
4721 "in a proposal.\n");
4726 case IPSECDOI_ATTR_ENC_MODE
:
4728 pr
->encmode
!= (u_int16_t
)ntohs(d
->lorv
)) {
4730 "multiple encmode exist "
4731 "in a transform.\n");
4734 pr
->encmode
= (u_int16_t
)ntohs(d
->lorv
);
4737 case IPSECDOI_ATTR_AUTH
:
4738 if (tr
->authtype
!= IPSECDOI_ATTR_AUTH_NONE
) {
4740 "multiple authtype exist "
4741 "in a transform.\n");
4744 tr
->authtype
= (u_int16_t
)ntohs(d
->lorv
);
4747 case IPSECDOI_ATTR_KEY_LENGTH
:
4748 if (pr
->proto_id
!= IPSECDOI_PROTO_IPSEC_ESP
) {
4750 "key length defined but not ESP");
4753 tr
->encklen
= ntohs(d
->lorv
);
4756 case IPSECDOI_ATTR_KEY_ROUNDS
:
4757 case IPSECDOI_ATTR_COMP_DICT_SIZE
:
4758 case IPSECDOI_ATTR_COMP_PRIVALG
:
4766 d
= (struct isakmp_data
*)((char *)d
+ sizeof(*d
));
4768 tlen
-= (sizeof(*d
) + ntohs(d
->lorv
));
4769 d
= (struct isakmp_data
*)((caddr_t
)d
+ sizeof(*d
) + ntohs(d
->lorv
));
4779 ipsecdoi_authalg2trnsid(alg
)
4783 case IPSECDOI_ATTR_AUTH_HMAC_MD5
:
4784 return IPSECDOI_AH_MD5
;
4785 case IPSECDOI_ATTR_AUTH_HMAC_SHA1
:
4786 return IPSECDOI_AH_SHA
;
4787 case IPSECDOI_ATTR_AUTH_HMAC_SHA2_256
:
4788 return IPSECDOI_AH_SHA256
;
4789 case IPSECDOI_ATTR_AUTH_HMAC_SHA2_384
:
4790 return IPSECDOI_AH_SHA384
;
4791 case IPSECDOI_ATTR_AUTH_HMAC_SHA2_512
:
4792 return IPSECDOI_AH_SHA512
;
4793 case IPSECDOI_ATTR_AUTH_DES_MAC
:
4794 return IPSECDOI_AH_DES
;
4795 case IPSECDOI_ATTR_AUTH_KPDK
:
4796 return IPSECDOI_AH_MD5
; /* XXX */
4799 "invalid authentication algorithm:%d\n", alg
);
4804 static int rm_idtype2doi
[] = {
4805 255, /* IDTYPE_UNDEFINED, 0 */
4806 IPSECDOI_ID_FQDN
, /* IDTYPE_FQDN, 1 */
4807 IPSECDOI_ID_USER_FQDN
, /* IDTYPE_USERFQDN, 2 */
4808 IPSECDOI_ID_KEY_ID
, /* IDTYPE_KEYID, 3 */
4809 255, /* IDTYPE_ADDRESS, 4
4810 * it expands into 4 types by another function. */
4811 IPSECDOI_ID_DER_ASN1_DN
, /* IDTYPE_ASN1DN, 5 */
4815 * convert idtype to DOI value.
4823 if (ARRAYLEN(rm_idtype2doi
) > idtype
)
4824 return rm_idtype2doi
[idtype
];
4833 case IPSECDOI_ID_FQDN
:
4834 return(IDTYPE_FQDN
);
4835 case IPSECDOI_ID_USER_FQDN
:
4836 return(IDTYPE_USERFQDN
);
4837 case IPSECDOI_ID_KEY_ID
:
4838 return(IDTYPE_KEYID
);
4839 case IPSECDOI_ID_DER_ASN1_DN
:
4840 return(IDTYPE_ASN1DN
);
4841 case IPSECDOI_ID_IPV4_ADDR
:
4842 case IPSECDOI_ID_IPV4_ADDR_SUBNET
:
4843 case IPSECDOI_ID_IPV6_ADDR
:
4844 case IPSECDOI_ID_IPV6_ADDR_SUBNET
:
4845 return(IDTYPE_ADDRESS
);
4847 plog(ASL_LEVEL_WARNING
,
4848 "Inproper idtype:%s in this function.\n",
4849 s_ipsecdoi_ident(doi
));
4850 return(IDTYPE_ADDRESS
); /* XXX */
4855 #ifdef ENABLE_HYBRID
4857 switch_authmethod(authmethod
)
4860 switch(authmethod
) {
4861 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
4862 authmethod
= OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
;
4864 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_R
:
4865 authmethod
= OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_I
;
4867 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
4868 authmethod
= OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_I
;
4870 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
4871 authmethod
= OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
;
4873 /* Those are not implemented */
4874 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_R
:
4875 authmethod
= OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_I
;
4877 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
4878 authmethod
= OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
;
4880 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
4881 authmethod
= OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
;