1 /* $KAME: isakmp_agg.c,v 1.55 2001/12/12 15:29:13 sakane 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
32 /* Aggressive Exchange (Aggressive Mode) */
34 #include <sys/types.h>
35 #include <sys/param.h>
41 #if TIME_WITH_SYS_TIME
42 # include <sys/time.h>
46 # include <sys/time.h>
51 #include <netinet/in.h>
61 #include "localconf.h"
62 #include "remoteconf.h"
63 #include "isakmp_var.h"
67 #include "ipsec_doi.h"
68 #include "crypto_openssl.h"
70 #include "isakmp_agg.h"
71 #include "isakmp_inf.h"
72 #include "isakmp_natd.h"
81 * begin Aggressive Mode as initiator.
85 * psk: HDR, SA, KE, Ni, IDi1
86 * sig: HDR, SA, KE, Ni, IDi1 [, CR ]
87 * gssapi: HDR, SA, KE, Ni, IDi1, GSSi
88 * rsa: HDR, SA, [ HASH(1),] KE, <IDi1_b>Pubkey_r, <Ni_b>Pubkey_r
89 * rev: HDR, SA, [ HASH(1),] <Ni_b>Pubkey_r, <KE_b>Ke_i,
90 * <IDii_b>Ke_i [, <Cert-I_b>Ke_i ]
94 struct ph1handle
*iph1
;
95 vchar_t
*msg
; /* must be null */
97 struct isakmp_gen
*gen
;
101 vchar_t
*cr
= NULL
, *gsstoken
= NULL
;
111 plog(LLV_ERROR
, LOCATION
, NULL
,
112 "msg has to be NULL in this function.\n");
115 if (iph1
->status
!= PHASE1ST_START
) {
116 plog(LLV_ERROR
, LOCATION
, NULL
,
117 "status mismatched %d.\n", iph1
->status
);
121 /* create isakmp index */
122 memset(&iph1
->index
, 0, sizeof(iph1
->index
));
123 isakmp_newcookie((caddr_t
)&iph1
->index
, iph1
->remote
, iph1
->local
);
125 /* make ID payload into isakmp status */
126 if (ipsecdoi_setid1(iph1
) < 0)
129 /* create SA payload for my proposal */
130 iph1
->sa
= ipsecdoi_setph1proposal(iph1
->rmconf
->proposal
);
131 if (iph1
->sa
== NULL
)
134 /* consistency check of proposals */
135 if (iph1
->rmconf
->dhgrp
== NULL
) {
136 plog(LLV_ERROR
, LOCATION
, NULL
,
137 "configuration failure about DH group.\n");
141 /* generate DH public value */
142 if (oakley_dh_generate(iph1
->rmconf
->dhgrp
,
143 &iph1
->dhpub
, &iph1
->dhpriv
) < 0)
146 /* generate NONCE value */
147 iph1
->nonce
= eay_set_random(iph1
->rmconf
->nonce_size
);
148 if (iph1
->nonce
== NULL
)
152 vid
= set_vendorid(VENDORID_NATT
);
155 #ifdef HAVE_SIGNING_C
156 /* create CR if need */
157 if (iph1
->rmconf
->send_cr
158 && oakley_needcr(iph1
->rmconf
->proposal
->authmethod
)
159 && iph1
->rmconf
->peerscertfile
== NULL
) {
161 cr
= oakley_getcr(iph1
);
163 plog(LLV_ERROR
, LOCATION
, NULL
,
164 "failed to get cr buffer.\n");
169 plog(LLV_DEBUG
, LOCATION
, NULL
, "authmethod is %s\n",
170 s_oakley_attr_method(iph1
->rmconf
->proposal
->authmethod
));
171 /* create buffer to send isakmp payload */
172 tlen
= sizeof(struct isakmp
)
173 + sizeof(*gen
) + iph1
->sa
->l
174 + sizeof(*gen
) + iph1
->dhpub
->l
175 + sizeof(*gen
) + iph1
->nonce
->l
176 + sizeof(*gen
) + iph1
->id
->l
;
178 tlen
+= sizeof(*gen
) + cr
->l
;
180 if (iph1
->rmconf
->proposal
->authmethod
==
181 OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
) {
182 gssapi_get_itoken(iph1
, &len
);
183 tlen
+= sizeof (*gen
) + len
;
187 tlen
+= sizeof(*gen
) + vid
->l
;
189 iph1
->sendbuf
= vmalloc(tlen
);
190 if (iph1
->sendbuf
== NULL
) {
191 plog(LLV_ERROR
, LOCATION
, NULL
,
192 "failed to get buffer to send.\n");
196 /* set isakmp header */
197 p
= set_isakmp_header(iph1
->sendbuf
, iph1
, ISAKMP_NPTYPE_SA
);
201 /* set SA payload to propose */
202 p
= set_isakmp_payload(p
, iph1
->sa
, ISAKMP_NPTYPE_KE
);
204 /* create isakmp KE payload */
205 p
= set_isakmp_payload(p
, iph1
->dhpub
, ISAKMP_NPTYPE_NONCE
);
207 /* create isakmp NONCE payload */
208 p
= set_isakmp_payload(p
, iph1
->nonce
, ISAKMP_NPTYPE_ID
);
210 /* create isakmp ID payload */
212 if (iph1
->rmconf
->proposal
->authmethod
==
213 OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
)
214 nptype
= ISAKMP_NPTYPE_GSS
;
218 nptype
= ISAKMP_NPTYPE_CR
;
220 nptype
= vid
? ISAKMP_NPTYPE_VID
: ISAKMP_NPTYPE_NONE
;
222 p
= set_isakmp_payload(p
, iph1
->id
, nptype
);
225 if (iph1
->rmconf
->proposal
->authmethod
==
226 OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
) {
227 gssapi_get_token_to_send(iph1
, &gsstoken
);
228 p
= set_isakmp_payload(p
, gsstoken
, vid
? ISAKMP_NPTYPE_VID
: ISAKMP_NPTYPE_NONE
);
232 /* create isakmp CR payload */
233 p
= set_isakmp_payload(p
, cr
, vid
? ISAKMP_NPTYPE_VID
: ISAKMP_NPTYPE_NONE
);
236 p
= set_isakmp_payload(p
, vid
, ISAKMP_NPTYPE_NONE
);
238 #ifdef HAVE_PRINT_ISAKMP_C
239 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
242 /* send the packet, add to the schedule to resend */
243 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
244 if (isakmp_ph1resend(iph1
) == -1)
247 iph1
->status
= PHASE1ST_MSG1SENT
;
263 * receive from responder
264 * psk: HDR, SA, KE, Nr, IDr1, HASH_R
265 * sig: HDR, SA, KE, Nr, IDr1, [ CR, ] [ CERT, ] SIG_R
266 * gssapi: HDR, SA, KE, Nr, IDr1, GSSr, HASH_R
267 * rsa: HDR, SA, KE, <IDr1_b>PubKey_i, <Nr_b>PubKey_i, HASH_R
268 * rev: HDR, SA, <Nr_b>PubKey_i, <KE_b>Ke_r, <IDir_b>Ke_r, HASH_R
271 agg_i2recv(iph1
, msg
)
272 struct ph1handle
*iph1
;
275 vchar_t
*pbuf
= NULL
;
276 struct isakmp_parse_t
*pa
;
277 vchar_t
*satmp
= NULL
;
280 vchar_t
*gsstoken
= NULL
;
284 if (iph1
->status
!= PHASE1ST_MSG1SENT
) {
285 plog(LLV_ERROR
, LOCATION
, NULL
,
286 "status mismatched %d.\n", iph1
->status
);
290 /* validate the type of next payload */
291 pbuf
= isakmp_parse(msg
);
294 pa
= (struct isakmp_parse_t
*)pbuf
->v
;
296 iph1
->pl_hash
= NULL
;
298 /* SA payload is fixed postion */
299 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
300 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
301 "received invalid next payload type %d, "
303 pa
->type
, ISAKMP_NPTYPE_SA
);
306 if (isakmp_p2ph(&satmp
, pa
->ptr
) < 0)
311 pa
->type
!= ISAKMP_NPTYPE_NONE
;
315 case ISAKMP_NPTYPE_KE
:
316 if (isakmp_p2ph(&iph1
->dhpub_p
, pa
->ptr
) < 0)
319 case ISAKMP_NPTYPE_NONCE
:
320 if (isakmp_p2ph(&iph1
->nonce_p
, pa
->ptr
) < 0)
323 case ISAKMP_NPTYPE_ID
:
324 if (isakmp_p2ph(&iph1
->id_p
, pa
->ptr
) < 0)
327 case ISAKMP_NPTYPE_HASH
:
328 iph1
->pl_hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
330 #ifdef HAVE_SIGNING_C
331 case ISAKMP_NPTYPE_CR
:
332 if (oakley_savecr(iph1
, pa
->ptr
) < 0)
335 case ISAKMP_NPTYPE_CERT
:
336 if (oakley_savecert(iph1
, pa
->ptr
) < 0)
339 case ISAKMP_NPTYPE_SIG
:
340 if (isakmp_p2ph(&iph1
->sig_p
, pa
->ptr
) < 0)
344 case ISAKMP_NPTYPE_VID
:
345 if (check_vendorid(pa
->ptr
) == VENDORID_NATT
)
348 iph1
->natt_flags
|= natt_remote_support
;
352 case ISAKMP_NPTYPE_N
:
353 isakmp_check_notify(pa
->ptr
, iph1
);
356 case ISAKMP_NPTYPE_GSS
:
357 if (isakmp_p2ph(&gsstoken
, pa
->ptr
) < 0)
359 gssapi_save_received_token(iph1
, gsstoken
);
362 case ISAKMP_NPTYPE_NATD
:
364 * ignored for now, we need to know the hash
365 * algorithm before we can evaluate the natd
370 /* don't send information, see isakmp_ident_r1() */
371 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
372 "ignore the packet, "
373 "received unexpecting payload type %d.\n",
379 /* payload existency check */
380 /* XXX to be checked each authentication method. */
382 /* verify identifier */
383 if (ipsecdoi_checkid1(iph1
) != 0) {
384 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
385 "invalid ID payload.\n");
389 /* check SA payload and set approval SA for use */
390 if (ipsecdoi_checkph1proposal(satmp
, iph1
) < 0) {
391 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
392 "failed to get valid proposal.\n");
393 /* XXX send information */
401 /* fix isakmp index */
402 memcpy(&iph1
->index
.r_ck
, &((struct isakmp
*)msg
->v
)->r_ck
,
405 /* check natd payloads */
407 for (pa
= (struct isakmp_parse_t
*)pbuf
->v
;
408 pa
->type
!= ISAKMP_NPTYPE_NONE
;
411 if (pa
->type
== ISAKMP_NPTYPE_NATD
)
413 natd_match_t match
= natd_matches(iph1
, pa
->ptr
);
414 iph1
->natt_flags
|= natt_natd_received
;
415 if ((match
& natd_match_local
) != 0)
416 iph1
->natt_flags
|= natt_no_local_nat
;
417 if ((match
& natd_match_remote
) != 0)
418 iph1
->natt_flags
|= natt_no_remote_nat
;
423 /* compute sharing secret of DH */
424 if (oakley_dh_compute(iph1
->rmconf
->dhgrp
, iph1
->dhpub
,
425 iph1
->dhpriv
, iph1
->dhpub_p
, &iph1
->dhgxy
) < 0)
428 /* generate SKEYIDs & IV & final cipher key */
429 if (oakley_skeyid(iph1
) < 0)
431 if (oakley_skeyid_dae(iph1
) < 0)
433 if (oakley_compute_enckey(iph1
) < 0)
435 if (oakley_newiv(iph1
) < 0)
439 /* Determine if we need to switch to port 4500 */
440 if (natd_hasnat(iph1
))
442 /* There is a NAT between us! Switch to port 4500. */
443 if (iph1
->remote
->sa_family
== AF_INET
)
445 struct sockaddr_in
*sin
= (struct sockaddr_in
*)iph1
->remote
;
446 plog(LLV_INFO
, LOCATION
, NULL
,
447 "detected NAT, switching to port %d for %s",
448 PORT_ISAKMP_NATT
, saddr2str(iph1
->remote
));
449 sin
->sin_port
= htons(PORT_ISAKMP_NATT
);
450 sin
= (struct sockaddr_in
*)iph1
->local
;
451 sin
->sin_port
= htons(PORT_ISAKMP_NATT
);
456 /* validate authentication value */
459 type
= oakley_validate_auth(iph1
);
462 /* message printed inner oakley_validate_auth() */
465 isakmp_info_send_n1(iph1
, type
, NULL
);
470 #ifdef HAVE_SIGNING_C
471 if (oakley_checkcr(iph1
) < 0) {
472 /* Ignore this error in order to be interoperability. */
477 /* change status of isakmp status entry */
478 iph1
->status
= PHASE1ST_MSG2RECEIVED
;
488 VPTRINIT(iph1
->dhpub_p
);
489 VPTRINIT(iph1
->nonce_p
);
490 VPTRINIT(iph1
->id_p
);
491 oakley_delcert(iph1
->cert_p
);
493 oakley_delcert(iph1
->crl_p
);
495 VPTRINIT(iph1
->sig_p
);
496 oakley_delcert(iph1
->cr_p
);
506 * gssapi: HDR, HASH_I
507 * sig: HDR, [ CERT, ] SIG_I
512 agg_i2send(iph1
, msg
)
513 struct ph1handle
*iph1
;
516 struct isakmp_gen
*gen
;
521 vchar_t
*gsshash
= NULL
;
525 if (iph1
->status
!= PHASE1ST_MSG2RECEIVED
) {
526 plog(LLV_ERROR
, LOCATION
, NULL
,
527 "status mismatched %d.\n", iph1
->status
);
531 /* generate HASH to send */
532 plog(LLV_DEBUG
, LOCATION
, NULL
, "generate HASH_I\n");
533 iph1
->hash
= oakley_ph1hash_common(iph1
, GENERATE
);
534 if (iph1
->hash
== NULL
) {
536 if (gssapi_more_tokens(iph1
))
537 isakmp_info_send_n1(iph1
,
538 ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
, NULL
);
543 tlen
= sizeof(struct isakmp
);
546 if ((iph1
->natt_flags
& natt_remote_support
) != 0) {
549 if (iph1
->local_natd
)
550 tlen
+= sizeof(*gen
) + iph1
->local_natd
->l
;
551 if (iph1
->remote_natd
)
552 tlen
+= sizeof(*gen
) + iph1
->remote_natd
->l
;
556 switch (iph1
->approval
->authmethod
) {
557 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
558 tlen
+= sizeof(*gen
) + iph1
->hash
->l
;
560 iph1
->sendbuf
= vmalloc(tlen
);
561 if (iph1
->sendbuf
== NULL
) {
562 plog(LLV_ERROR
, LOCATION
, NULL
,
563 "failed to get buffer to send.\n");
567 /* set isakmp header */
568 p
= set_isakmp_header(iph1
->sendbuf
, iph1
, ISAKMP_NPTYPE_HASH
);
572 /* set HASH payload */
573 p
= set_isakmp_payload(p
, iph1
->hash
,
574 need_natd
? ISAKMP_NPTYPE_NATD
575 : ISAKMP_NPTYPE_NONE
);
577 #ifdef HAVE_SIGNING_C
578 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
579 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
580 /* XXX if there is CR or not ? */
582 if (oakley_getmycert(iph1
) < 0)
585 if (oakley_getsign(iph1
) < 0)
588 if (iph1
->cert
!= NULL
&& iph1
->rmconf
->send_cert
)
591 tlen
+= sizeof(*gen
) + iph1
->sig
->l
;
593 tlen
+= sizeof(*gen
) + iph1
->cert
->pl
->l
;
595 iph1
->sendbuf
= vmalloc(tlen
);
596 if (iph1
->sendbuf
== NULL
) {
597 plog(LLV_ERROR
, LOCATION
, NULL
,
598 "failed to get buffer to send.\n");
602 /* set isakmp header */
603 p
= set_isakmp_header(iph1
->sendbuf
, iph1
, need_cert
605 : ISAKMP_NPTYPE_SIG
);
609 /* add CERT payload if there */
611 p
= set_isakmp_payload(p
, iph1
->cert
->pl
, ISAKMP_NPTYPE_SIG
);
612 /* add SIG payload */
613 p
= set_isakmp_payload(p
, iph1
->sig
,
614 need_natd
? ISAKMP_NPTYPE_NATD
615 : ISAKMP_NPTYPE_NONE
);
618 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
619 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
620 tlen
+= sizeof(*gen
) + iph1
->hash
->l
;
623 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
624 gsshash
= gssapi_wraphash(iph1
);
625 if (gsshash
== NULL
) {
626 plog(LLV_ERROR
, LOCATION
, NULL
,
627 "failed to wrap hash\n");
628 isakmp_info_send_n1(iph1
,
629 ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
, NULL
);
632 tlen
+= sizeof(*gen
) + gsshash
->l
;
634 iph1
->sendbuf
= vmalloc(tlen
);
635 if (iph1
->sendbuf
== NULL
) {
636 plog(LLV_ERROR
, LOCATION
, NULL
,
637 "failed to get buffer to send.\n");
640 /* set isakmp header */
641 p
= set_isakmp_header(iph1
->sendbuf
, iph1
, ISAKMP_NPTYPE_HASH
);
644 p
= set_isakmp_payload(p
, gsshash
,
645 need_natd
? ISAKMP_NPTYPE_NATD
646 : ISAKMP_NPTYPE_NONE
);
653 if (iph1
->local_natd
)
654 p
= set_isakmp_payload(p
, iph1
->local_natd
, ISAKMP_NPTYPE_NATD
);
655 if (iph1
->remote_natd
)
656 p
= set_isakmp_payload(p
, iph1
->remote_natd
, ISAKMP_NPTYPE_NONE
);
660 #ifdef HAVE_PRINT_ISAKMP_C
661 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
664 /* send to responder */
665 if (isakmp_send(iph1
, iph1
->sendbuf
) < 0)
668 /* the sending message is added to the received-list. */
669 if (add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
) == -1) {
670 plog(LLV_ERROR
, LOCATION
, NULL
,
671 "failed to add a response packet to the tree.\n");
675 /* set encryption flag */
676 iph1
->flags
|= ISAKMP_FLAG_E
;
678 iph1
->status
= PHASE1ST_ESTABLISHED
;
689 * receive from initiator
690 * psk: HDR, SA, KE, Ni, IDi1
691 * sig: HDR, SA, KE, Ni, IDi1 [, CR ]
692 * gssapi: HDR, SA, KE, Ni, IDi1 , GSSi
693 * rsa: HDR, SA, [ HASH(1),] KE, <IDi1_b>Pubkey_r, <Ni_b>Pubkey_r
694 * rev: HDR, SA, [ HASH(1),] <Ni_b>Pubkey_r, <KE_b>Ke_i,
695 * <IDii_b>Ke_i [, <Cert-I_b>Ke_i ]
698 agg_r1recv(iph1
, msg
)
699 struct ph1handle
*iph1
;
703 vchar_t
*pbuf
= NULL
;
704 struct isakmp_parse_t
*pa
;
706 vchar_t
*gsstoken
= NULL
;
710 if (iph1
->status
!= PHASE1ST_START
) {
711 plog(LLV_ERROR
, LOCATION
, NULL
,
712 "status mismatched %d.\n", iph1
->status
);
716 /* validate the type of next payload */
717 pbuf
= isakmp_parse(msg
);
720 pa
= (struct isakmp_parse_t
*)pbuf
->v
;
722 /* SA payload is fixed postion */
723 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
724 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
725 "received invalid next payload type %d, "
727 pa
->type
, ISAKMP_NPTYPE_SA
);
730 if (isakmp_p2ph(&iph1
->sa
, pa
->ptr
) < 0)
735 pa
->type
!= ISAKMP_NPTYPE_NONE
;
738 plog(LLV_DEBUG
, LOCATION
, NULL
,
739 "received payload of type %s\n",
740 s_isakmp_nptype(pa
->type
));
743 case ISAKMP_NPTYPE_KE
:
744 if (isakmp_p2ph(&iph1
->dhpub_p
, pa
->ptr
) < 0)
747 case ISAKMP_NPTYPE_NONCE
:
748 if (isakmp_p2ph(&iph1
->nonce_p
, pa
->ptr
) < 0)
751 case ISAKMP_NPTYPE_ID
:
752 if (isakmp_p2ph(&iph1
->id_p
, pa
->ptr
) < 0)
755 case ISAKMP_NPTYPE_VID
:
756 if (check_vendorid(pa
->ptr
) == VENDORID_NATT
)
759 iph1
->natt_flags
|= natt_remote_support
;
763 #ifdef HAVE_SIGNING_C
764 case ISAKMP_NPTYPE_CR
:
765 if (oakley_savecr(iph1
, pa
->ptr
) < 0)
770 case ISAKMP_NPTYPE_GSS
:
771 if (isakmp_p2ph(&gsstoken
, pa
->ptr
) < 0)
773 gssapi_save_received_token(iph1
, gsstoken
);
777 /* don't send information, see isakmp_ident_r1() */
778 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
779 "ignore the packet, "
780 "received unexpecting payload type %d.\n",
786 /* payload existency check */
787 /* XXX to be checked each authentication method. */
789 /* verify identifier */
790 if (ipsecdoi_checkid1(iph1
) != 0) {
791 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
792 "invalid ID payload.\n");
796 /* check SA payload and set approval SA for use */
797 if (ipsecdoi_checkph1proposal(iph1
->sa
, iph1
) < 0) {
798 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
799 "failed to get valid proposal.\n");
800 /* XXX send information */
804 #ifdef HAVE_SIGNING_C
805 if (oakley_checkcr(iph1
) < 0) {
806 /* Ignore this error in order to be interoperability. */
811 iph1
->status
= PHASE1ST_MSG1RECEIVED
;
820 VPTRINIT(iph1
->dhpub_p
);
821 VPTRINIT(iph1
->nonce_p
);
822 VPTRINIT(iph1
->id_p
);
823 oakley_delcert(iph1
->cr_p
);
832 * psk: HDR, SA, KE, Nr, IDr1, HASH_R
833 * sig: HDR, SA, KE, Nr, IDr1, [ CR, ] [ CERT, ] SIG_R
834 * gssapi: HDR, SA, KE, Nr, IDr1, GSSr, HASH_R
835 * rsa: HDR, SA, KE, <IDr1_b>PubKey_i, <Nr_b>PubKey_i, HASH_R
836 * rev: HDR, SA, <Nr_b>PubKey_i, <KE_b>Ke_r, <IDir_b>Ke_r, HASH_R
839 agg_r1send(iph1
, msg
)
840 struct ph1handle
*iph1
;
843 struct isakmp_gen
*gen
;
853 vchar_t
*gsstoken
= NULL
, *gsshash
= NULL
;
854 vchar_t
*gss_sa
= NULL
;
856 vchar_t
*nattvid
= NULL
;
859 if (iph1
->status
!= PHASE1ST_MSG1RECEIVED
) {
860 plog(LLV_ERROR
, LOCATION
, NULL
,
861 "status mismatched %d.\n", iph1
->status
);
865 /* set responder's cookie */
866 isakmp_newcookie((caddr_t
)&iph1
->index
.r_ck
, iph1
->remote
, iph1
->local
);
868 /* make ID payload into isakmp status */
869 if (ipsecdoi_setid1(iph1
) < 0)
872 /* generate DH public value */
873 if (oakley_dh_generate(iph1
->rmconf
->dhgrp
,
874 &iph1
->dhpub
, &iph1
->dhpriv
) < 0)
877 /* generate NONCE value */
878 iph1
->nonce
= eay_set_random(iph1
->rmconf
->nonce_size
);
879 if (iph1
->nonce
== NULL
)
882 /* compute sharing secret of DH */
883 if (oakley_dh_compute(iph1
->approval
->dhgrp
, iph1
->dhpub
,
884 iph1
->dhpriv
, iph1
->dhpub_p
, &iph1
->dhgxy
) < 0)
887 /* generate SKEYIDs & IV & final cipher key */
888 if (oakley_skeyid(iph1
) < 0)
890 if (oakley_skeyid_dae(iph1
) < 0)
892 if (oakley_compute_enckey(iph1
) < 0)
894 if (oakley_newiv(iph1
) < 0)
898 if (iph1
->rmconf
->proposal
->authmethod
==
899 OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
)
900 gssapi_get_rtoken(iph1
, &gsslen
);
903 /* generate HASH to send */
904 plog(LLV_DEBUG
, LOCATION
, NULL
, "generate HASH_R\n");
905 iph1
->hash
= oakley_ph1hash_common(iph1
, GENERATE
);
906 if (iph1
->hash
== NULL
) {
908 if (gssapi_more_tokens(iph1
))
909 isakmp_info_send_n1(iph1
,
910 ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
, NULL
);
915 #ifdef HAVE_SIGNING_C
916 /* create CR if need */
917 if (iph1
->rmconf
->send_cr
918 && oakley_needcr(iph1
->approval
->authmethod
)
919 && iph1
->rmconf
->peerscertfile
== NULL
) {
921 cr
= oakley_getcr(iph1
);
923 plog(LLV_ERROR
, LOCATION
, NULL
,
924 "failed to get cr buffer.\n");
930 tlen
= sizeof(struct isakmp
);
933 if ((iph1
->natt_flags
& natt_remote_support
) != 0) {
934 nattvid
= set_vendorid(VENDORID_NATT
);
937 tlen
+= sizeof(*gen
) + nattvid
->l
;
938 if (iph1
->local_natd
)
939 tlen
+= sizeof(*gen
) + iph1
->local_natd
->l
;
940 if (iph1
->remote_natd
)
941 tlen
+= sizeof(*gen
) + iph1
->remote_natd
->l
;
946 switch (iph1
->approval
->authmethod
) {
947 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
948 /* create buffer to send isakmp payload */
949 tlen
+= sizeof(*gen
) + iph1
->sa_ret
->l
950 + sizeof(*gen
) + iph1
->dhpub
->l
951 + sizeof(*gen
) + iph1
->nonce
->l
952 + sizeof(*gen
) + iph1
->id
->l
953 + sizeof(*gen
) + iph1
->hash
->l
;
954 if ((vid
= set_vendorid(iph1
->approval
->vendorid
)) != NULL
)
955 tlen
+= sizeof(*gen
) + vid
->l
;
957 tlen
+= sizeof(*gen
) + cr
->l
;
959 iph1
->sendbuf
= vmalloc(tlen
);
960 if (iph1
->sendbuf
== NULL
) {
961 plog(LLV_ERROR
, LOCATION
, NULL
,
962 "failed to get buffer to send\n");
966 /* set isakmp header */
967 p
= set_isakmp_header(iph1
->sendbuf
, iph1
, ISAKMP_NPTYPE_SA
);
971 /* set SA payload to reply */
972 p
= set_isakmp_payload(p
, iph1
->sa_ret
, ISAKMP_NPTYPE_KE
);
974 /* create isakmp KE payload */
975 p
= set_isakmp_payload(p
, iph1
->dhpub
, ISAKMP_NPTYPE_NONCE
);
977 /* create isakmp NONCE payload */
978 p
= set_isakmp_payload(p
, iph1
->nonce
, ISAKMP_NPTYPE_ID
);
980 /* create isakmp ID payload */
981 p
= set_isakmp_payload(p
, iph1
->id
, ISAKMP_NPTYPE_HASH
);
983 /* create isakmp HASH payload */
984 p
= set_isakmp_payload(p
, iph1
->hash
,
985 vid
? ISAKMP_NPTYPE_VID
986 : (need_cr
? ISAKMP_NPTYPE_CR
987 : (nattvid
? ISAKMP_NPTYPE_VID
988 : ISAKMP_NPTYPE_NONE
)));
990 /* append vendor id, if needed */
992 p
= set_isakmp_payload(p
, vid
,
993 need_cr
? ISAKMP_NPTYPE_CR
994 : (nattvid
? ISAKMP_NPTYPE_VID
995 : ISAKMP_NPTYPE_NONE
));
997 /* create isakmp CR payload if needed */
999 p
= set_isakmp_payload(p
, cr
,
1000 nattvid
? ISAKMP_NPTYPE_VID
1001 : ISAKMP_NPTYPE_NONE
);
1003 #ifdef HAVE_SIGNING_C
1004 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
1005 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
1006 /* XXX if there is CR or not ? */
1008 if (oakley_getmycert(iph1
) < 0)
1011 if (oakley_getsign(iph1
) < 0)
1014 if (iph1
->cert
!= NULL
&& iph1
->rmconf
->send_cert
)
1017 tlen
+= sizeof(*gen
) + iph1
->sa_ret
->l
1018 + sizeof(*gen
) + iph1
->dhpub
->l
1019 + sizeof(*gen
) + iph1
->nonce
->l
1020 + sizeof(*gen
) + iph1
->id
->l
1021 + sizeof(*gen
) + iph1
->sig
->l
;
1023 tlen
+= sizeof(*gen
) + iph1
->cert
->pl
->l
;
1024 if ((vid
= set_vendorid(iph1
->approval
->vendorid
)) != NULL
)
1025 tlen
+= sizeof(*gen
) + vid
->l
;
1027 tlen
+= sizeof(*gen
) + cr
->l
;
1029 iph1
->sendbuf
= vmalloc(tlen
);
1030 if (iph1
->sendbuf
== NULL
) {
1031 plog(LLV_ERROR
, LOCATION
, NULL
,
1032 "failed to get buffer to send.\n");
1036 /* set isakmp header */
1037 p
= set_isakmp_header(iph1
->sendbuf
, iph1
, ISAKMP_NPTYPE_SA
);
1041 /* set SA payload to reply */
1042 p
= set_isakmp_payload(p
, iph1
->sa_ret
, ISAKMP_NPTYPE_KE
);
1044 /* create isakmp KE payload */
1045 p
= set_isakmp_payload(p
, iph1
->dhpub
, ISAKMP_NPTYPE_NONCE
);
1047 /* create isakmp NONCE payload */
1048 p
= set_isakmp_payload(p
, iph1
->nonce
, ISAKMP_NPTYPE_ID
);
1050 /* add ID payload */
1051 p
= set_isakmp_payload(p
, iph1
->id
, need_cert
1052 ? ISAKMP_NPTYPE_CERT
1053 : ISAKMP_NPTYPE_SIG
);
1055 /* add CERT payload if there */
1057 p
= set_isakmp_payload(p
, iph1
->cert
->pl
, ISAKMP_NPTYPE_SIG
);
1058 /* add SIG payload */
1059 p
= set_isakmp_payload(p
, iph1
->sig
,
1060 vid
? ISAKMP_NPTYPE_VID
1061 : (need_cr
? ISAKMP_NPTYPE_CR
1062 : ISAKMP_NPTYPE_NONE
));
1064 /* append vendor id, if needed */
1066 p
= set_isakmp_payload(p
, vid
,
1067 need_cr
? ISAKMP_NPTYPE_CR
1068 : (nattvid
? ISAKMP_NPTYPE_VID
1069 : ISAKMP_NPTYPE_NONE
));
1071 /* create isakmp CR payload if needed */
1073 p
= set_isakmp_payload(p
, cr
,
1074 nattvid
? ISAKMP_NPTYPE_VID
1075 : ISAKMP_NPTYPE_NONE
);
1079 p
= set_isakmp_payload(p
, nattvid
, ISAKMP_NPTYPE_NATD
);
1080 if (iph1
->local_natd
)
1081 p
= set_isakmp_payload(p
, iph1
->local_natd
, ISAKMP_NPTYPE_NATD
);
1082 if (iph1
->remote_natd
)
1083 p
= set_isakmp_payload(p
, iph1
->remote_natd
, ISAKMP_NPTYPE_NONE
);
1088 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
1089 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
1090 tlen
+= sizeof(*gen
) + iph1
->hash
->l
;
1093 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
1094 /* create buffer to send isakmp payload */
1095 gsshash
= gssapi_wraphash(iph1
);
1096 if (gsshash
== NULL
) {
1097 plog(LLV_ERROR
, LOCATION
, NULL
,
1098 "failed to wrap hash\n");
1100 * This is probably due to the GSS roundtrips not
1101 * being finished yet. Return this error in
1102 * the hope that a fallback to main mode will
1105 isakmp_info_send_n1(iph1
,
1106 ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
, NULL
);
1109 if (iph1
->approval
->gssid
!= NULL
)
1110 gss_sa
= ipsecdoi_setph1proposal(iph1
->approval
);
1112 gss_sa
= iph1
->sa_ret
;
1114 tlen
+= sizeof(*gen
) + gss_sa
->l
1115 + sizeof(*gen
) + iph1
->dhpub
->l
1116 + sizeof(*gen
) + iph1
->nonce
->l
1117 + sizeof(*gen
) + iph1
->id
->l
1118 + sizeof(*gen
) + gsslen
1119 + sizeof(*gen
) + gsshash
->l
;
1120 if ((vid
= set_vendorid(iph1
->approval
->vendorid
)) != NULL
)
1121 tlen
+= sizeof(*gen
) + vid
->l
;
1122 iph1
->sendbuf
= vmalloc(tlen
);
1123 if (iph1
->sendbuf
== NULL
) {
1124 plog(LLV_ERROR
, LOCATION
, NULL
,
1125 "failed to get buffer to send\n");
1129 /* set isakmp header */
1130 p
= set_isakmp_header(iph1
->sendbuf
, iph1
, ISAKMP_NPTYPE_SA
);
1134 /* set SA payload to reply */
1135 p
= set_isakmp_payload(p
, gss_sa
, ISAKMP_NPTYPE_KE
);
1137 /* create isakmp KE payload */
1138 p
= set_isakmp_payload(p
, iph1
->dhpub
, ISAKMP_NPTYPE_NONCE
);
1140 /* create isakmp NONCE payload */
1141 p
= set_isakmp_payload(p
, iph1
->nonce
, ISAKMP_NPTYPE_ID
);
1143 /* create isakmp ID payload */
1144 p
= set_isakmp_payload(p
, iph1
->id
, ISAKMP_NPTYPE_GSS
);
1146 /* create GSS payload */
1147 gssapi_get_token_to_send(iph1
, &gsstoken
);
1148 p
= set_isakmp_payload(p
, gsstoken
, ISAKMP_NPTYPE_HASH
);
1150 /* create isakmp HASH payload */
1151 p
= set_isakmp_payload(p
, gsshash
,
1152 vid
!= NULL
|| nattvid
!= NULL
? ISAKMP_NPTYPE_VID
1153 : ISAKMP_NPTYPE_NONE
);
1155 /* append vendor id, if needed */
1157 p
= set_isakmp_payload(p
, vid
,
1158 nattvid
!= NULL
? ISAKMP_NPTYPE_VID
1159 : ISAKMP_NPTYPE_NONE
);
1166 p
= set_isakmp_payload(p
, nattvid
, ISAKMP_NPTYPE_NATD
);
1167 if (iph1
->local_natd
)
1168 p
= set_isakmp_payload(p
, iph1
->local_natd
, ISAKMP_NPTYPE_NATD
);
1169 if (iph1
->remote_natd
)
1170 p
= set_isakmp_payload(p
, iph1
->remote_natd
, ISAKMP_NPTYPE_NONE
);
1175 #ifdef HAVE_PRINT_ISAKMP_C
1176 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 1);
1179 /* send the packet, add to the schedule to resend */
1180 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
1181 if (isakmp_ph1resend(iph1
) == -1)
1184 /* the sending message is added to the received-list. */
1185 if (add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
) == -1) {
1186 plog(LLV_ERROR
, LOCATION
, NULL
,
1187 "failed to add a response packet to the tree.\n");
1191 iph1
->status
= PHASE1ST_MSG1SENT
;
1207 if (gss_sa
!= iph1
->sa_ret
)
1215 * receive from initiator
1217 * gssapi: HDR, HASH_I
1218 * sig: HDR, [ CERT, ] SIG_I
1223 agg_r2recv(iph1
, msg0
)
1224 struct ph1handle
*iph1
;
1227 vchar_t
*msg
= NULL
;
1228 vchar_t
*pbuf
= NULL
;
1229 struct isakmp_parse_t
*pa
;
1232 /* validity check */
1233 if (iph1
->status
!= PHASE1ST_MSG1SENT
) {
1234 plog(LLV_ERROR
, LOCATION
, NULL
,
1235 "status mismatched %d.\n", iph1
->status
);
1239 /* decrypting if need. */
1240 /* XXX configurable ? */
1241 if (ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
1242 msg
= oakley_do_decrypt(iph1
, msg0
,
1243 iph1
->ivm
->iv
, iph1
->ivm
->ive
);
1249 /* validate the type of next payload */
1250 pbuf
= isakmp_parse(msg
);
1254 iph1
->pl_hash
= NULL
;
1256 for (pa
= (struct isakmp_parse_t
*)pbuf
->v
;
1257 pa
->type
!= ISAKMP_NPTYPE_NONE
;
1261 case ISAKMP_NPTYPE_HASH
:
1262 iph1
->pl_hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
1264 case ISAKMP_NPTYPE_VID
:
1265 (void)check_vendorid(pa
->ptr
);
1267 #ifdef HAVE_SIGNING_C
1268 case ISAKMP_NPTYPE_CERT
:
1269 if (oakley_savecert(iph1
, pa
->ptr
) < 0)
1272 case ISAKMP_NPTYPE_SIG
:
1273 if (isakmp_p2ph(&iph1
->sig_p
, pa
->ptr
) < 0)
1277 case ISAKMP_NPTYPE_N
:
1278 isakmp_check_notify(pa
->ptr
, iph1
);
1280 case ISAKMP_NPTYPE_NATD
:
1283 natd_match_t match
= natd_matches(iph1
, pa
->ptr
);
1284 iph1
->natt_flags
|= natt_natd_received
;
1285 if ((match
& natd_match_local
) != 0)
1286 iph1
->natt_flags
|= natt_no_local_nat
;
1287 if ((match
& natd_match_remote
) != 0)
1288 iph1
->natt_flags
|= natt_no_remote_nat
;
1293 /* don't send information, see isakmp_ident_r1() */
1294 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1295 "ignore the packet, "
1296 "received unexpecting payload type %d.\n",
1302 /* validate authentication value */
1305 type
= oakley_validate_auth(iph1
);
1308 /* message printed inner oakley_validate_auth() */
1311 isakmp_info_send_n1(iph1
, type
, NULL
);
1316 iph1
->status
= PHASE1ST_MSG2RECEIVED
;
1326 oakley_delcert(iph1
->cert_p
);
1327 iph1
->cert_p
= NULL
;
1328 oakley_delcert(iph1
->crl_p
);
1330 VPTRINIT(iph1
->sig_p
);
1337 * status update and establish isakmp sa.
1340 agg_r2send(iph1
, msg
)
1341 struct ph1handle
*iph1
;
1346 /* validity check */
1347 if (iph1
->status
!= PHASE1ST_MSG2RECEIVED
) {
1348 plog(LLV_ERROR
, LOCATION
, NULL
,
1349 "status mismatched %d.\n", iph1
->status
);
1353 /* IV synchronized when packet encrypted. */
1354 /* see handler.h about IV synchronization. */
1355 if (ISSET(((struct isakmp
*)msg
->v
)->flags
, ISAKMP_FLAG_E
))
1356 memcpy(iph1
->ivm
->iv
->v
, iph1
->ivm
->ive
->v
, iph1
->ivm
->iv
->l
);
1358 /* set encryption flag */
1359 iph1
->flags
|= ISAKMP_FLAG_E
;
1361 iph1
->status
= PHASE1ST_ESTABLISHED
;