1 /* $NetBSD: isakmp_agg.c,v 1.9 2006/09/30 21:49:37 manu Exp $ */
3 /* Id: isakmp_agg.c,v 1.28 2006/04/06 16:46:08 manubsd Exp */
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 /* Aggressive Exchange (Aggressive Mode) */
38 #include <sys/types.h>
39 #include <sys/param.h>
45 #if TIME_WITH_SYS_TIME
46 # include <sys/time.h>
50 # include <sys/time.h>
68 #include "localconf.h"
69 #include "remoteconf.h"
70 #include "isakmp_var.h"
75 #include "ipsec_doi.h"
76 #include "crypto_openssl.h"
78 #include "isakmp_agg.h"
79 #include "isakmp_inf.h"
81 #include "isakmp_xauth.h"
82 #include "isakmp_cfg.h"
85 #include "isakmp_frag.h"
91 #include "nattraversal.h"
98 #include "vpn_control.h"
99 #include "vpn_control_var.h"
100 #include "ipsecSessionTracer.h"
101 #include "ipsecMessageTracer.h"
103 #include <Security/SecDH.h>
107 * begin Aggressive Mode as initiator.
111 * psk: HDR, SA, KE, Ni, IDi1
112 * sig: HDR, SA, KE, Ni, IDi1 [, CR ]
113 * gssapi: HDR, SA, KE, Ni, IDi1, GSSi
114 * rsa: HDR, SA, [ HASH(1),] KE, <IDi1_b>Pubkey_r, <Ni_b>Pubkey_r
115 * rev: HDR, SA, [ HASH(1),] <Ni_b>Pubkey_r, <KE_b>Ke_i,
116 * <IDii_b>Ke_i [, <Cert-I_b>Ke_i ]
119 agg_i1send(iph1
, msg
)
120 struct ph1handle
*iph1
;
121 vchar_t
*msg
; /* must be null */
123 struct payload_list
*plist
= NULL
;
128 vchar_t
*vid_natt
[MAX_NATT_VID_COUNT
] = { NULL
};
132 vchar_t
*vid_xauth
= NULL
;
133 vchar_t
*vid_unity
= NULL
;
136 vchar_t
*vid_frag
= NULL
;
139 vchar_t
*gsstoken
= NULL
;
143 vchar_t
*vid_dpd
= NULL
;
149 plog(LLV_ERROR
, LOCATION
, NULL
,
150 "msg has to be NULL in this function.\n");
153 if (iph1
->status
!= PHASE1ST_START
) {
154 plog(LLV_ERROR
, LOCATION
, NULL
,
155 "status mismatched %d.\n", iph1
->status
);
159 /* create isakmp index */
160 memset(&iph1
->index
, 0, sizeof(iph1
->index
));
161 isakmp_newcookie((caddr_t
)&iph1
->index
, iph1
->remote
, iph1
->local
);
163 /* make ID payload into isakmp status */
164 if (ipsecdoi_setid1(iph1
) < 0) {
165 plog(LLV_ERROR
, LOCATION
, NULL
,
170 /* create SA payload for my proposal */
171 iph1
->sa
= ipsecdoi_setph1proposal(iph1
->rmconf
->proposal
);
172 if (iph1
->sa
== NULL
) {
173 plog(LLV_ERROR
, LOCATION
, NULL
,
174 "failed to set proposal");
178 /* consistency check of proposals */
179 if (iph1
->rmconf
->dhgrp
== NULL
) {
180 plog(LLV_ERROR
, LOCATION
, NULL
,
181 "configuration failure about DH group.\n");
185 /* generate DH public value */
187 if (oakley_dh_generate(iph1
->rmconf
->dhgrp
,
188 &iph1
->dhpub
, &iph1
->dhpriv
) < 0) {
190 if (oakley_dh_generate(iph1
->rmconf
->dhgrp
,
191 &iph1
->dhpub
, &iph1
->publicKeySize
, &iph1
->dhC
) < 0) {
193 plog(LLV_ERROR
, LOCATION
, NULL
,
194 "failed to generate DH");
198 /* generate NONCE value */
199 iph1
->nonce
= eay_set_random(iph1
->rmconf
->nonce_size
);
200 if (iph1
->nonce
== NULL
) {
201 plog(LLV_ERROR
, LOCATION
, NULL
,
202 "failed to generate NONCE");
207 /* Do we need Xauth VID? */
208 switch (RMAUTHMETHOD(iph1
)) {
209 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
210 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
211 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_I
:
212 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
213 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_I
:
214 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
:
215 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
:
216 if ((vid_xauth
= set_vendorid(VENDORID_XAUTH
)) == NULL
)
217 plog(LLV_ERROR
, LOCATION
, NULL
,
218 "Xauth vendor ID generation failed\n");
219 if ((vid_unity
= set_vendorid(VENDORID_UNITY
)) == NULL
)
220 plog(LLV_ERROR
, LOCATION
, NULL
,
221 "Unity vendor ID generation failed\n");
229 if (iph1
->rmconf
->ike_frag
) {
230 vid_frag
= set_vendorid(VENDORID_FRAG
);
231 if (vid_frag
!= NULL
)
232 vid_frag
= isakmp_frag_addcap(vid_frag
,
234 if (vid_frag
== NULL
)
235 plog(LLV_ERROR
, LOCATION
, NULL
,
236 "Frag vendorID construction failed\n");
240 /* create CR if need */
241 if (iph1
->rmconf
->send_cr
242 && oakley_needcr(iph1
->rmconf
->proposal
->authmethod
)
243 && iph1
->rmconf
->peerscertfile
== NULL
) {
245 cr
= oakley_getcr(iph1
);
247 plog(LLV_ERROR
, LOCATION
, NULL
,
253 plog(LLV_DEBUG
, LOCATION
, NULL
, "authmethod is %s\n",
254 s_oakley_attr_method(iph1
->rmconf
->proposal
->authmethod
));
256 if (RMAUTHMETHOD(iph1
) == OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
)
257 gssapi_get_itoken(iph1
, &len
);
260 /* set SA payload to propose */
261 plist
= isakmp_plist_append(plist
, iph1
->sa
, ISAKMP_NPTYPE_SA
);
263 /* create isakmp KE payload */
264 plist
= isakmp_plist_append(plist
, iph1
->dhpub
, ISAKMP_NPTYPE_KE
);
266 /* create isakmp NONCE payload */
267 plist
= isakmp_plist_append(plist
, iph1
->nonce
, ISAKMP_NPTYPE_NONCE
);
269 /* create isakmp ID payload */
270 plist
= isakmp_plist_append(plist
, iph1
->id
, ISAKMP_NPTYPE_ID
);
273 if (RMAUTHMETHOD(iph1
) == OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
) {
274 gssapi_get_token_to_send(iph1
, &gsstoken
);
275 plist
= isakmp_plist_append(plist
, gsstoken
, ISAKMP_NPTYPE_GSS
);
278 /* create isakmp CR payload */
280 plist
= isakmp_plist_append(plist
, cr
, ISAKMP_NPTYPE_CR
);
284 plist
= isakmp_plist_append(plist
, vid_frag
, ISAKMP_NPTYPE_VID
);
288 * set VID payload for NAT-T if NAT-T
289 * support allowed in the config file
291 if (iph1
->rmconf
->nat_traversal
)
292 plist
= isakmp_plist_append_natt_vids(plist
, vid_natt
);
296 plist
= isakmp_plist_append(plist
,
297 vid_xauth
, ISAKMP_NPTYPE_VID
);
299 plist
= isakmp_plist_append(plist
,
300 vid_unity
, ISAKMP_NPTYPE_VID
);
303 if(iph1
->rmconf
->dpd
){
304 vid_dpd
= set_vendorid(VENDORID_DPD
);
306 plist
= isakmp_plist_append(plist
, vid_dpd
, ISAKMP_NPTYPE_VID
);
310 iph1
->sendbuf
= isakmp_plist_set_all (&plist
, iph1
);
312 #ifdef HAVE_PRINT_ISAKMP_C
313 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
316 /* send the packet, add to the schedule to resend */
317 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
318 if (isakmp_ph1resend(iph1
) == -1) {
319 plog(LLV_ERROR
, LOCATION
, NULL
,
320 "failed to send packet");
324 iph1
->status
= PHASE1ST_MSG1SENT
;
328 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
329 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
330 CONSTSTR("Initiator, Aggressive-Mode message 1"),
335 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
336 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
337 CONSTSTR("Initiator, Aggressive-Mode Message 1"),
338 CONSTSTR("Failed to transmit Aggressive-Mode Message 1"));
351 for (i
= 0; i
< MAX_NATT_VID_COUNT
&& vid_natt
[i
] != NULL
; i
++)
355 if (vid_xauth
!= NULL
)
357 if (vid_unity
!= NULL
)
369 * receive from responder
370 * psk: HDR, SA, KE, Nr, IDr1, HASH_R
371 * sig: HDR, SA, KE, Nr, IDr1, [ CR, ] [ CERT, ] SIG_R
372 * gssapi: HDR, SA, KE, Nr, IDr1, GSSr, HASH_R
373 * rsa: HDR, SA, KE, <IDr1_b>PubKey_i, <Nr_b>PubKey_i, HASH_R
374 * rev: HDR, SA, <Nr_b>PubKey_i, <KE_b>Ke_r, <IDir_b>Ke_r, HASH_R
377 agg_i2recv(iph1
, msg
)
378 struct ph1handle
*iph1
;
381 vchar_t
*pbuf
= NULL
;
382 struct isakmp_parse_t
*pa
;
383 vchar_t
*satmp
= NULL
;
392 vchar_t
*gsstoken
= NULL
;
394 int received_cert
= 0;
398 struct natd_payload
{
401 TAILQ_ENTRY(natd_payload
) chain
;
403 TAILQ_HEAD(_natd_payload
, natd_payload
) natd_tree
;
404 TAILQ_INIT(&natd_tree
);
408 if (iph1
->status
!= PHASE1ST_MSG1SENT
) {
409 plog(LLV_ERROR
, LOCATION
, NULL
,
410 "status mismatched %d.\n", iph1
->status
);
414 /* validate the type of next payload */
415 pbuf
= isakmp_parse(msg
);
417 plog(LLV_ERROR
, LOCATION
, NULL
,
418 "failed to parse msg");
421 pa
= (struct isakmp_parse_t
*)pbuf
->v
;
423 iph1
->pl_hash
= NULL
;
425 /* SA payload is fixed postion */
426 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
427 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
428 "received invalid next payload type %d, "
430 pa
->type
, ISAKMP_NPTYPE_SA
);
434 if (isakmp_p2ph(&satmp
, pa
->ptr
) < 0) {
435 plog(LLV_ERROR
, LOCATION
, NULL
,
436 "failed to process SA payload");
442 pa
->type
!= ISAKMP_NPTYPE_NONE
;
446 case ISAKMP_NPTYPE_KE
:
447 if (isakmp_p2ph(&iph1
->dhpub_p
, pa
->ptr
) < 0) {
448 plog(LLV_ERROR
, LOCATION
, NULL
,
449 "failed to process KE payload");
453 case ISAKMP_NPTYPE_NONCE
:
454 if (isakmp_p2ph(&iph1
->nonce_p
, pa
->ptr
) < 0) {
455 plog(LLV_ERROR
, LOCATION
, NULL
,
456 "failed to process NONCE payload");
460 case ISAKMP_NPTYPE_ID
:
461 if (isakmp_p2ph(&iph1
->id_p
, pa
->ptr
) < 0) {
462 plog(LLV_ERROR
, LOCATION
, NULL
,
463 "failed to process ID payload");
467 case ISAKMP_NPTYPE_HASH
:
468 iph1
->pl_hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
470 case ISAKMP_NPTYPE_CR
:
471 if (oakley_savecr(iph1
, pa
->ptr
) < 0) {
472 plog(LLV_ERROR
, LOCATION
, NULL
,
473 "failed to process CR payload");
477 case ISAKMP_NPTYPE_CERT
:
478 if (oakley_savecert(iph1
, pa
->ptr
) < 0) {
479 plog(LLV_ERROR
, LOCATION
, NULL
,
480 "failed to process CERT payload");
485 case ISAKMP_NPTYPE_SIG
:
486 if (isakmp_p2ph(&iph1
->sig_p
, pa
->ptr
) < 0) {
487 plog(LLV_ERROR
, LOCATION
, NULL
,
488 "failed to process SIG payload");
492 case ISAKMP_NPTYPE_VID
:
493 vid_numeric
= check_vendorid(pa
->ptr
);
495 if (iph1
->rmconf
->nat_traversal
&&
496 natt_vendorid(vid_numeric
))
497 natt_handle_vendorid(iph1
, vid_numeric
);
500 switch (vid_numeric
) {
502 iph1
->mode_cfg
->flags
|=
503 ISAKMP_CFG_VENDORID_XAUTH
;
507 iph1
->mode_cfg
->flags
|=
508 ISAKMP_CFG_VENDORID_UNITY
;
515 if (vid_numeric
== VENDORID_DPD
&& iph1
->rmconf
->dpd
) {
517 plog(LLV_DEBUG
, LOCATION
, NULL
,
518 "remote supports DPD\n");
522 if ((vid_numeric
== VENDORID_FRAG
) &&
523 (vendorid_frag_cap(pa
->ptr
) & VENDORID_FRAG_AGG
)) {
524 plog(LLV_DEBUG
, LOCATION
, NULL
,
525 "remote supports FRAGMENTATION\n");
530 case ISAKMP_NPTYPE_N
:
531 isakmp_check_notify(pa
->ptr
, iph1
);
534 case ISAKMP_NPTYPE_GSS
:
535 if (isakmp_p2ph(&gsstoken
, pa
->ptr
) < 0) {
536 plog(LLV_ERROR
, LOCATION
, NULL
,
537 "failed to process GSS payload");
540 gssapi_save_received_token(iph1
, gsstoken
);
545 case ISAKMP_NPTYPE_NATD_DRAFT
:
546 case ISAKMP_NPTYPE_NATD_RFC
:
547 case ISAKMP_NPTYPE_NATD_BADDRAFT
:
548 if (NATT_AVAILABLE(iph1
) && iph1
->natt_options
!= NULL
&&
549 pa
->type
== iph1
->natt_options
->payload_nat_d
) {
550 struct natd_payload
*natd
;
551 natd
= (struct natd_payload
*)racoon_malloc(sizeof(*natd
));
553 plog(LLV_ERROR
, LOCATION
, NULL
,
554 "failed to pre-process NATD payload");
558 natd
->payload
= NULL
;
560 if (isakmp_p2ph (&natd
->payload
, pa
->ptr
) < 0) {
561 plog(LLV_ERROR
, LOCATION
, NULL
,
562 "failed to process NATD payload");
566 natd
->seq
= natd_seq
++;
568 TAILQ_INSERT_TAIL(&natd_tree
, natd
, chain
);
571 /* %%% Be lenient here - some servers send natd payloads */
572 /* when nat not detected */
577 /* don't send information, see isakmp_ident_r1() */
578 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
579 "ignore the packet, "
580 "received unexpecting payload type %d.\n",
587 oakley_verify_certid(iph1
);
590 /* payload existency check */
591 if (iph1
->dhpub_p
== NULL
|| iph1
->nonce_p
== NULL
) {
592 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
593 "few isakmp message received.\n");
597 /* verify identifier */
598 if (ipsecdoi_checkid1(iph1
) != 0) {
599 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
600 "invalid ID payload.\n");
604 /* check SA payload and set approval SA for use */
605 if (ipsecdoi_checkph1proposal(satmp
, iph1
) < 0) {
606 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
607 "failed to get valid proposal.\n");
608 /* XXX send information */
611 VPTRINIT(iph1
->sa_ret
);
613 /* fix isakmp index */
614 memcpy(&iph1
->index
.r_ck
, &((struct isakmp
*)msg
->v
)->r_ck
,
618 if (NATT_AVAILABLE(iph1
)) {
619 struct natd_payload
*natd
= NULL
;
622 plog(LLV_INFO
, LOCATION
, iph1
->remote
,
623 "Selected NAT-T version: %s\n",
624 vid_string_by_id(iph1
->natt_options
->version
));
626 /* set both bits first so that we can clear them
627 upon verifying hashes */
628 iph1
->natt_flags
|= NAT_DETECTED
;
630 while ((natd
= TAILQ_FIRST(&natd_tree
)) != NULL
) {
631 /* this function will clear appropriate bits bits
632 from iph1->natt_flags */
633 natd_verified
= natt_compare_addr_hash (iph1
,
634 natd
->payload
, natd
->seq
);
636 plog (LLV_INFO
, LOCATION
, NULL
, "NAT-D payload #%d %s\n",
638 natd_verified
? "verified" : "doesn't match");
640 vfree (natd
->payload
);
642 TAILQ_REMOVE(&natd_tree
, natd
, chain
);
646 plog (LLV_INFO
, LOCATION
, NULL
, "NAT %s %s%s\n",
647 iph1
->natt_flags
& NAT_DETECTED
?
648 "detected:" : "not detected",
649 iph1
->natt_flags
& NAT_DETECTED_ME
? "ME " : "",
650 iph1
->natt_flags
& NAT_DETECTED_PEER
? "PEER" : "");
652 if (iph1
->natt_flags
& NAT_DETECTED
)
653 natt_float_ports (iph1
);
654 ike_session_update_natt_version(iph1
);
658 /* compute sharing secret of DH */
660 if (oakley_dh_compute(iph1
->rmconf
->dhgrp
, iph1
->dhpub
,
661 iph1
->dhpriv
, iph1
->dhpub_p
, &iph1
->dhgxy
) < 0) {
663 if (oakley_dh_compute(iph1
->rmconf
->dhgrp
, iph1
->dhpub_p
, iph1
->publicKeySize
, &iph1
->dhgxy
, iph1
->dhC
) < 0) {
665 plog(LLV_ERROR
, LOCATION
, NULL
,
666 "failed to compute DH");
670 /* generate SKEYIDs & IV & final cipher key */
671 if (oakley_skeyid(iph1
) < 0) {
672 plog(LLV_ERROR
, LOCATION
, NULL
,
673 "failed to generate SKEYID");
676 if (oakley_skeyid_dae(iph1
) < 0) {
677 plog(LLV_ERROR
, LOCATION
, NULL
,
678 "failed to generate SKEYID-DAE");
681 if (oakley_compute_enckey(iph1
) < 0) {
682 plog(LLV_ERROR
, LOCATION
, NULL
,
683 "failed to generate ENCKEY");
686 if (oakley_newiv(iph1
) < 0) {
687 plog(LLV_ERROR
, LOCATION
, NULL
,
688 "failed to generate IV");
692 /* validate authentication value */
693 ptype
= oakley_validate_auth(iph1
);
695 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
696 IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_FAIL
,
697 CONSTSTR("Initiator, Aggressive-Mode Message 2"),
698 CONSTSTR("Failed to authenticate, Aggressive-Mode Message 2"));
700 /* message printed inner oakley_validate_auth() */
703 EVT_PUSH(iph1
->local
, iph1
->remote
,
704 EVTT_PEERPH1AUTH_FAILED
, NULL
);
705 isakmp_info_send_n1(iph1
, ptype
, NULL
);
708 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
709 IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_SUCC
,
710 CONSTSTR("Initiator, Aggressive-Mode Message 2"),
713 if (oakley_checkcr(iph1
) < 0) {
714 /* Ignore this error in order to be interoperability. */
718 /* change status of isakmp status entry */
719 iph1
->status
= PHASE1ST_MSG2RECEIVED
;
721 #ifdef ENABLE_VPNCONTROL_PORT
722 vpncontrol_notify_phase_change(1, FROM_REMOTE
, iph1
, NULL
);
727 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
728 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
729 CONSTSTR("Initiator, Aggressive-Mode message 2"),
734 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
735 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
736 CONSTSTR("Initiator, Aggressive-Mode Message 2"),
737 CONSTSTR("Failure processing Aggressive-Mode Message 2"));
748 VPTRINIT(iph1
->dhpub_p
);
749 VPTRINIT(iph1
->nonce_p
);
750 VPTRINIT(iph1
->id_p
);
751 oakley_delcert(iph1
->cert_p
);
753 oakley_delcert(iph1
->crl_p
);
755 VPTRINIT(iph1
->sig_p
);
756 oakley_delcert(iph1
->cr_p
);
766 * gssapi: HDR, HASH_I
767 * sig: HDR, [ CERT, ] SIG_I
772 agg_i2send(iph1
, msg
)
773 struct ph1handle
*iph1
;
776 struct payload_list
*plist
= NULL
;
779 vchar_t
*gsshash
= NULL
;
781 vchar_t
*natd
[2] = { NULL
, NULL
};
783 vchar_t
*notp_unity
= NULL
;
784 vchar_t
*notp_ini
= NULL
;
787 if (iph1
->status
!= PHASE1ST_MSG2RECEIVED
) {
788 plog(LLV_ERROR
, LOCATION
, NULL
,
789 "status mismatched %d.\n", iph1
->status
);
793 /* generate HASH to send */
794 plog(LLV_DEBUG
, LOCATION
, NULL
, "generate HASH_I\n");
795 iph1
->hash
= oakley_ph1hash_common(iph1
, GENERATE
);
796 if (iph1
->hash
== NULL
) {
798 if (gssapi_more_tokens(iph1
) &&
800 !iph1
->rmconf
->xauth
&&
803 isakmp_info_send_n1(iph1
,
804 ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
, NULL
);
806 plog(LLV_ERROR
, LOCATION
, NULL
,
807 "failed to generate HASH");
811 switch (AUTHMETHOD(iph1
)) {
812 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
814 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
815 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
816 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_I
:
818 /* set HASH payload */
819 plist
= isakmp_plist_append(plist
,
820 iph1
->hash
, ISAKMP_NPTYPE_HASH
);
823 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
824 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
826 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
827 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_I
:
829 /* XXX if there is CR or not ? */
831 if (oakley_getmycert(iph1
) < 0) {
832 plog(LLV_ERROR
, LOCATION
, NULL
,
833 "failed to get mycert");
837 if (oakley_getsign(iph1
) < 0) {
838 plog(LLV_ERROR
, LOCATION
, NULL
,
839 "failed to get sign");
843 if (iph1
->cert
!= NULL
&& iph1
->rmconf
->send_cert
)
846 /* add CERT payload if there */
847 // we don't support sending of certchains
849 plist
= isakmp_plist_append(plist
, iph1
->cert
->pl
, ISAKMP_NPTYPE_CERT
);
851 /* add SIG payload */
852 plist
= isakmp_plist_append(plist
, iph1
->sig
, ISAKMP_NPTYPE_SIG
);
855 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
856 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
858 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
:
859 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
:
863 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
864 gsshash
= gssapi_wraphash(iph1
);
865 if (gsshash
== NULL
) {
866 plog(LLV_ERROR
, LOCATION
, NULL
,
867 "failed to get GSS hash\n");
868 isakmp_info_send_n1(iph1
,
869 ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
, NULL
);
873 plist
= isakmp_plist_append(plist
, gsshash
, ISAKMP_NPTYPE_HASH
);
879 /* generate NAT-D payloads */
880 if (NATT_AVAILABLE(iph1
)) {
881 plog (LLV_INFO
, LOCATION
, NULL
, "Adding remote and local NAT-D payloads.\n");
882 if ((natd
[0] = natt_hash_addr (iph1
, iph1
->remote
)) == NULL
) {
883 plog(LLV_ERROR
, LOCATION
, NULL
,
884 "NAT-D hashing failed for %s\n", saddr2str(iph1
->remote
));
888 if ((natd
[1] = natt_hash_addr (iph1
, iph1
->local
)) == NULL
) {
889 plog(LLV_ERROR
, LOCATION
, NULL
,
890 "NAT-D hashing failed for %s\n", saddr2str(iph1
->local
));
893 /* old Apple version sends natd payloads in the wrong order */
894 if (iph1
->natt_options
->version
== VENDORID_NATT_APPLE
) {
895 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
896 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
899 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
900 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
906 iph1
->sendbuf
= isakmp_plist_set_all (&plist
, iph1
);
908 #ifdef HAVE_PRINT_ISAKMP_C
909 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
913 /* send to responder */
914 if (isakmp_send(iph1
, iph1
->sendbuf
) < 0) {
915 plog(LLV_ERROR
, LOCATION
, NULL
,
916 "failed to send packet");
920 /* the sending message is added to the received-list. */
921 if (add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
,
922 PH1_NON_ESP_EXTRA_LEN(iph1
), PH1_FRAG_FLAGS(iph1
)) == -1) {
923 plog(LLV_ERROR
, LOCATION
, NULL
,
924 "failed to add a response packet to the tree.\n");
928 /* set encryption flag */
929 iph1
->flags
|= ISAKMP_FLAG_E
;
931 iph1
->status
= PHASE1ST_ESTABLISHED
;
933 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
934 IPSECSESSIONEVENTCODE_IKEV1_PH1_INIT_SUCC
,
935 CONSTSTR("Initiator, Aggressive-Mode"),
940 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
941 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
942 CONSTSTR("Initiator, Aggressive-Mode message 3"),
947 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
948 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
949 CONSTSTR("Initiator, Aggressive-Mode Message 3"),
950 CONSTSTR("Failed to transmit Aggressive-Mode Message 3"));
968 * receive from initiator
969 * psk: HDR, SA, KE, Ni, IDi1
970 * sig: HDR, SA, KE, Ni, IDi1 [, CR ]
971 * gssapi: HDR, SA, KE, Ni, IDi1 , GSSi
972 * rsa: HDR, SA, [ HASH(1),] KE, <IDi1_b>Pubkey_r, <Ni_b>Pubkey_r
973 * rev: HDR, SA, [ HASH(1),] <Ni_b>Pubkey_r, <KE_b>Ke_i,
974 * <IDii_b>Ke_i [, <Cert-I_b>Ke_i ]
977 agg_r1recv(iph1
, msg
)
978 struct ph1handle
*iph1
;
982 vchar_t
*pbuf
= NULL
;
983 struct isakmp_parse_t
*pa
;
986 vchar_t
*gsstoken
= NULL
;
990 if (iph1
->status
!= PHASE1ST_START
) {
991 plog(LLV_ERROR
, LOCATION
, NULL
,
992 "status mismatched %d.\n", iph1
->status
);
996 /* validate the type of next payload */
997 pbuf
= isakmp_parse(msg
);
999 plog(LLV_ERROR
, LOCATION
, NULL
,
1000 "failed to parse msg");
1003 pa
= (struct isakmp_parse_t
*)pbuf
->v
;
1005 /* SA payload is fixed postion */
1006 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
1007 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1008 "received invalid next payload type %d, "
1010 pa
->type
, ISAKMP_NPTYPE_SA
);
1013 if (isakmp_p2ph(&iph1
->sa
, pa
->ptr
) < 0) {
1014 plog(LLV_ERROR
, LOCATION
, NULL
,
1015 "failed to process SA payload");
1021 pa
->type
!= ISAKMP_NPTYPE_NONE
;
1024 plog(LLV_DEBUG
, LOCATION
, NULL
,
1025 "received payload of type %s\n",
1026 s_isakmp_nptype(pa
->type
));
1029 case ISAKMP_NPTYPE_KE
:
1030 if (isakmp_p2ph(&iph1
->dhpub_p
, pa
->ptr
) < 0) {
1031 plog(LLV_ERROR
, LOCATION
, NULL
,
1032 "failed to process KE payload");
1036 case ISAKMP_NPTYPE_NONCE
:
1037 if (isakmp_p2ph(&iph1
->nonce_p
, pa
->ptr
) < 0) {
1038 plog(LLV_ERROR
, LOCATION
, NULL
,
1039 "failed to process NONCE payload");
1043 case ISAKMP_NPTYPE_ID
:
1044 if (isakmp_p2ph(&iph1
->id_p
, pa
->ptr
) < 0) {
1045 plog(LLV_ERROR
, LOCATION
, NULL
,
1046 "failed to process ID payload");
1050 case ISAKMP_NPTYPE_VID
:
1051 vid_numeric
= check_vendorid(pa
->ptr
);
1054 if (iph1
->rmconf
->nat_traversal
&&
1055 natt_vendorid(vid_numeric
)) {
1056 natt_handle_vendorid(iph1
, vid_numeric
);
1060 #ifdef ENABLE_HYBRID
1061 switch (vid_numeric
) {
1062 case VENDORID_XAUTH
:
1063 iph1
->mode_cfg
->flags
|=
1064 ISAKMP_CFG_VENDORID_XAUTH
;
1067 case VENDORID_UNITY
:
1068 iph1
->mode_cfg
->flags
|=
1069 ISAKMP_CFG_VENDORID_UNITY
;
1076 if (vid_numeric
== VENDORID_DPD
&& iph1
->rmconf
->dpd
) {
1077 iph1
->dpd_support
=1;
1078 plog(LLV_DEBUG
, LOCATION
, NULL
,
1079 "remote supports DPD\n");
1083 if ((vid_numeric
== VENDORID_FRAG
) &&
1084 (vendorid_frag_cap(pa
->ptr
) & VENDORID_FRAG_AGG
)) {
1085 plog(LLV_DEBUG
, LOCATION
, NULL
,
1086 "remote supports FRAGMENTATION\n");
1092 case ISAKMP_NPTYPE_CR
:
1093 if (oakley_savecr(iph1
, pa
->ptr
) < 0) {
1094 plog(LLV_ERROR
, LOCATION
, NULL
,
1095 "failed to process CR payload");
1101 case ISAKMP_NPTYPE_GSS
:
1102 if (isakmp_p2ph(&gsstoken
, pa
->ptr
) < 0) {
1103 plog(LLV_ERROR
, LOCATION
, NULL
,
1104 "failed to process GSS payload");
1107 gssapi_save_received_token(iph1
, gsstoken
);
1111 /* don't send information, see isakmp_ident_r1() */
1112 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1113 "ignore the packet, "
1114 "received unexpecting payload type %d.\n",
1120 /* payload existency check */
1121 if (iph1
->dhpub_p
== NULL
|| iph1
->nonce_p
== NULL
) {
1122 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1123 "few isakmp message received.\n");
1127 /* verify identifier */
1128 if (ipsecdoi_checkid1(iph1
) != 0) {
1129 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1130 "invalid ID payload.\n");
1135 if (NATT_AVAILABLE(iph1
)) {
1136 plog(LLV_INFO
, LOCATION
, iph1
->remote
,
1137 "Selected NAT-T version: %s\n",
1138 vid_string_by_id(iph1
->natt_options
->version
));
1139 ike_session_update_natt_version(iph1
);
1143 /* check SA payload and set approval SA for use */
1144 if (ipsecdoi_checkph1proposal(iph1
->sa
, iph1
) < 0) {
1145 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1146 "failed to get valid proposal.\n");
1147 /* XXX send information */
1151 if (oakley_checkcr(iph1
) < 0) {
1152 /* Ignore this error in order to be interoperability. */
1156 iph1
->status
= PHASE1ST_MSG1RECEIVED
;
1160 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1161 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
1162 CONSTSTR("Responder, Aggressive-Mode message 1"),
1167 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1168 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
1169 CONSTSTR("Responder, Aggressive-Mode Message 1"),
1170 CONSTSTR("Failed to process Aggressive-Mode Message 1"));
1180 VPTRINIT(iph1
->dhpub_p
);
1181 VPTRINIT(iph1
->nonce_p
);
1182 VPTRINIT(iph1
->id_p
);
1183 oakley_delcert(iph1
->cr_p
);
1192 * psk: HDR, SA, KE, Nr, IDr1, HASH_R
1193 * sig: HDR, SA, KE, Nr, IDr1, [ CR, ] [ CERT, ] SIG_R
1194 * gssapi: HDR, SA, KE, Nr, IDr1, GSSr, HASH_R
1195 * rsa: HDR, SA, KE, <IDr1_b>PubKey_i, <Nr_b>PubKey_i, HASH_R
1196 * rev: HDR, SA, <Nr_b>PubKey_i, <KE_b>Ke_r, <IDir_b>Ke_r, HASH_R
1199 agg_r1send(iph1
, msg
)
1200 struct ph1handle
*iph1
;
1203 struct payload_list
*plist
= NULL
;
1208 #ifdef ENABLE_HYBRID
1209 vchar_t
*xauth_vid
= NULL
;
1210 vchar_t
*unity_vid
= NULL
;
1213 vchar_t
*vid_natt
= NULL
;
1214 vchar_t
*natd
[2] = { NULL
, NULL
};
1217 vchar_t
*vid_dpd
= NULL
;
1220 vchar_t
*vid_frag
= NULL
;
1225 vchar_t
*gsstoken
= NULL
, *gsshash
= NULL
;
1226 vchar_t
*gss_sa
= NULL
;
1227 int free_gss_sa
= 0;
1230 /* validity check */
1231 if (iph1
->status
!= PHASE1ST_MSG1RECEIVED
) {
1232 plog(LLV_ERROR
, LOCATION
, NULL
,
1233 "status mismatched %d.\n", iph1
->status
);
1237 /* set responder's cookie */
1238 isakmp_newcookie((caddr_t
)&iph1
->index
.r_ck
, iph1
->remote
, iph1
->local
);
1240 /* make ID payload into isakmp status */
1241 if (ipsecdoi_setid1(iph1
) < 0) {
1242 plog(LLV_ERROR
, LOCATION
, NULL
,
1243 "failed to set ID");
1247 /* generate DH public value */
1249 if (oakley_dh_generate(iph1
->rmconf
->dhgrp
,
1250 &iph1
->dhpub
, &iph1
->dhpriv
) < 0) {
1252 if (oakley_dh_generate(iph1
->rmconf
->dhgrp
,
1253 &iph1
->dhpub
, &iph1
->publicKeySize
, &iph1
->dhC
) < 0) {
1255 plog(LLV_ERROR
, LOCATION
, NULL
,
1256 "failed to generate DH");
1260 /* generate NONCE value */
1261 iph1
->nonce
= eay_set_random(iph1
->rmconf
->nonce_size
);
1262 if (iph1
->nonce
== NULL
) {
1263 plog(LLV_ERROR
, LOCATION
, NULL
,
1264 "failed to generate NONCE");
1268 /* compute sharing secret of DH */
1270 if (oakley_dh_compute(iph1
->approval
->dhgrp
, iph1
->dhpub
,
1271 iph1
->dhpriv
, iph1
->dhpub_p
, &iph1
->dhgxy
) < 0) {
1273 if (oakley_dh_compute(iph1
->approval
->dhgrp
, iph1
->dhpub_p
, iph1
->publicKeySize
, &iph1
->dhgxy
, iph1
->dhC
) < 0) {
1275 plog(LLV_ERROR
, LOCATION
, NULL
,
1276 "failed to compute DH");
1280 /* generate SKEYIDs & IV & final cipher key */
1281 if (oakley_skeyid(iph1
) < 0) {
1282 plog(LLV_ERROR
, LOCATION
, NULL
,
1283 "failed to generate SKEYID");
1286 if (oakley_skeyid_dae(iph1
) < 0) {
1287 plog(LLV_ERROR
, LOCATION
, NULL
,
1288 "failed to generate SKEYID-DAE");
1291 if (oakley_compute_enckey(iph1
) < 0) {
1292 plog(LLV_ERROR
, LOCATION
, NULL
,
1293 "failed to generate ENCKEY");
1296 if (oakley_newiv(iph1
) < 0) {
1297 plog(LLV_ERROR
, LOCATION
, NULL
,
1298 "failed to generate IV");
1303 if (RMAUTHMETHOD(iph1
) == OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
)
1304 gssapi_get_rtoken(iph1
, &gsslen
);
1307 /* generate HASH to send */
1308 plog(LLV_DEBUG
, LOCATION
, NULL
, "generate HASH_R\n");
1309 iph1
->hash
= oakley_ph1hash_common(iph1
, GENERATE
);
1310 if (iph1
->hash
== NULL
) {
1312 if (gssapi_more_tokens(iph1
))
1313 isakmp_info_send_n1(iph1
,
1314 ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
, NULL
);
1316 plog(LLV_ERROR
, LOCATION
, NULL
,
1317 "failed to generate GSS HASH");
1321 /* create CR if need */
1322 if (iph1
->rmconf
->send_cr
1323 && oakley_needcr(iph1
->approval
->authmethod
)
1324 && iph1
->rmconf
->peerscertfile
== NULL
) {
1326 cr
= oakley_getcr(iph1
);
1328 plog(LLV_ERROR
, LOCATION
, NULL
,
1329 "failed to get CR.\n");
1335 /* Has the peer announced NAT-T? */
1336 if (NATT_AVAILABLE(iph1
)) {
1337 /* set chosen VID */
1338 vid_natt
= set_vendorid(iph1
->natt_options
->version
);
1340 /* generate NAT-D payloads */
1341 plog (LLV_INFO
, LOCATION
, NULL
, "Adding remote and local NAT-D payloads.\n");
1342 if ((natd
[0] = natt_hash_addr (iph1
, iph1
->remote
)) == NULL
) {
1343 plog(LLV_ERROR
, LOCATION
, NULL
,
1344 "NAT-D hashing failed for %s\n", saddr2str(iph1
->remote
));
1348 if ((natd
[1] = natt_hash_addr (iph1
, iph1
->local
)) == NULL
) {
1349 plog(LLV_ERROR
, LOCATION
, NULL
,
1350 "NAT-D hashing failed for %s\n", saddr2str(iph1
->local
));
1356 /* Only send DPD support if remote announced DPD and if DPD support is active */
1357 if (iph1
->dpd_support
&& iph1
->rmconf
->dpd
)
1358 vid_dpd
= set_vendorid(VENDORID_DPD
);
1362 vid_frag
= set_vendorid(VENDORID_FRAG
);
1363 if (vid_frag
!= NULL
)
1364 vid_frag
= isakmp_frag_addcap(vid_frag
,
1366 if (vid_frag
== NULL
)
1367 plog(LLV_ERROR
, LOCATION
, NULL
,
1368 "Frag vendorID construction failed\n");
1372 switch (AUTHMETHOD(iph1
)) {
1373 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
1374 #ifdef ENABLE_HYBRID
1375 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1377 /* set SA payload to reply */
1378 plist
= isakmp_plist_append(plist
, iph1
->sa_ret
, ISAKMP_NPTYPE_SA
);
1380 /* create isakmp KE payload */
1381 plist
= isakmp_plist_append(plist
, iph1
->dhpub
, ISAKMP_NPTYPE_KE
);
1383 /* create isakmp NONCE payload */
1384 plist
= isakmp_plist_append(plist
, iph1
->nonce
, ISAKMP_NPTYPE_NONCE
);
1386 /* create isakmp ID payload */
1387 plist
= isakmp_plist_append(plist
, iph1
->id
, ISAKMP_NPTYPE_ID
);
1389 /* create isakmp HASH payload */
1390 plist
= isakmp_plist_append(plist
,
1391 iph1
->hash
, ISAKMP_NPTYPE_HASH
);
1393 /* create isakmp CR payload if needed */
1395 plist
= isakmp_plist_append(plist
, cr
, ISAKMP_NPTYPE_CR
);
1397 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
1398 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
1399 #ifdef ENABLE_HYBRID
1400 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1401 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_R
:
1402 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1403 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_R
:
1405 /* XXX if there is CR or not ? */
1407 if (oakley_getmycert(iph1
) < 0) {
1408 plog(LLV_ERROR
, LOCATION
, NULL
,
1409 "failed to get mycert");
1413 if (oakley_getsign(iph1
) < 0) {
1414 plog(LLV_ERROR
, LOCATION
, NULL
,
1415 "failed to get sign");
1419 if (iph1
->cert
!= NULL
&& iph1
->rmconf
->send_cert
)
1422 /* set SA payload to reply */
1423 plist
= isakmp_plist_append(plist
, iph1
->sa_ret
, ISAKMP_NPTYPE_SA
);
1425 /* create isakmp KE payload */
1426 plist
= isakmp_plist_append(plist
, iph1
->dhpub
, ISAKMP_NPTYPE_KE
);
1428 /* create isakmp NONCE payload */
1429 plist
= isakmp_plist_append(plist
, iph1
->nonce
, ISAKMP_NPTYPE_NONCE
);
1431 /* add ID payload */
1432 plist
= isakmp_plist_append(plist
, iph1
->id
, ISAKMP_NPTYPE_ID
);
1434 /* add CERT payload if there */
1436 plist
= isakmp_plist_append(plist
, iph1
->cert
->pl
, ISAKMP_NPTYPE_CERT
);
1438 /* add SIG payload */
1439 plist
= isakmp_plist_append(plist
, iph1
->sig
, ISAKMP_NPTYPE_SIG
);
1441 /* create isakmp CR payload if needed */
1443 plist
= isakmp_plist_append(plist
,
1444 cr
, ISAKMP_NPTYPE_CR
);
1447 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
1448 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
1449 #ifdef ENABLE_HYBRID
1450 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1451 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1455 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
1456 /* create buffer to send isakmp payload */
1457 gsshash
= gssapi_wraphash(iph1
);
1458 if (gsshash
== NULL
) {
1459 plog(LLV_ERROR
, LOCATION
, NULL
,
1460 "failed to generate GSS HASH\n");
1462 * This is probably due to the GSS
1463 * roundtrips not being finished yet.
1464 * Return this error in the hope that
1465 * a fallback to main mode will be done.
1467 isakmp_info_send_n1(iph1
,
1468 ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
, NULL
);
1471 if (iph1
->approval
->gssid
!= NULL
)
1473 ipsecdoi_setph1proposal(iph1
->approval
);
1475 gss_sa
= iph1
->sa_ret
;
1477 if (gss_sa
!= iph1
->sa_ret
)
1480 /* set SA payload to reply */
1481 plist
= isakmp_plist_append(plist
,
1482 gss_sa
, ISAKMP_NPTYPE_SA
);
1484 /* create isakmp KE payload */
1485 plist
= isakmp_plist_append(plist
,
1486 iph1
->dhpub
, ISAKMP_NPTYPE_KE
);
1488 /* create isakmp NONCE payload */
1489 plist
= isakmp_plist_append(plist
,
1490 iph1
->nonce
, ISAKMP_NPTYPE_NONCE
);
1492 /* create isakmp ID payload */
1493 plist
= isakmp_plist_append(plist
,
1494 iph1
->id
, ISAKMP_NPTYPE_ID
);
1496 /* create GSS payload */
1497 gssapi_get_token_to_send(iph1
, &gsstoken
);
1498 plist
= isakmp_plist_append(plist
,
1499 gsstoken
, ISAKMP_NPTYPE_GSS
);
1501 /* create isakmp HASH payload */
1502 plist
= isakmp_plist_append(plist
,
1503 gsshash
, ISAKMP_NPTYPE_HASH
);
1505 /* append vendor id, if needed */
1510 #ifdef ENABLE_HYBRID
1511 if (iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) {
1512 plog (LLV_INFO
, LOCATION
, NULL
, "Adding xauth VID payload.\n");
1513 if ((xauth_vid
= set_vendorid(VENDORID_XAUTH
)) == NULL
) {
1514 plog(LLV_ERROR
, LOCATION
, NULL
,
1515 "Cannot create Xauth vendor ID\n");
1518 plist
= isakmp_plist_append(plist
,
1519 xauth_vid
, ISAKMP_NPTYPE_VID
);
1522 if (iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_UNITY
) {
1523 if ((unity_vid
= set_vendorid(VENDORID_UNITY
)) == NULL
) {
1524 plog(LLV_ERROR
, LOCATION
, NULL
,
1525 "Cannot create Unity vendor ID\n");
1528 plist
= isakmp_plist_append(plist
,
1529 unity_vid
, ISAKMP_NPTYPE_VID
);
1534 /* append NAT-T payloads */
1537 plist
= isakmp_plist_append(plist
, vid_natt
, ISAKMP_NPTYPE_VID
);
1539 /* old Apple version sends natd payloads in the wrong order */
1540 if (iph1
->natt_options
->version
== VENDORID_NATT_APPLE
) {
1541 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
1542 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
1545 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
1546 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
1553 plist
= isakmp_plist_append(plist
, vid_frag
, ISAKMP_NPTYPE_VID
);
1558 plist
= isakmp_plist_append(plist
, vid_dpd
, ISAKMP_NPTYPE_VID
);
1561 iph1
->sendbuf
= isakmp_plist_set_all (&plist
, iph1
);
1563 #ifdef HAVE_PRINT_ISAKMP_C
1564 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 1);
1567 /* send the packet, add to the schedule to resend */
1568 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
1569 if (isakmp_ph1resend(iph1
) == -1) {
1570 plog(LLV_ERROR
, LOCATION
, NULL
,
1571 "failed to send packet");
1575 /* the sending message is added to the received-list. */
1576 if (add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
,
1577 PH1_NON_ESP_EXTRA_LEN(iph1
), PH1_FRAG_FLAGS(iph1
)) == -1) {
1578 plog(LLV_ERROR
, LOCATION
, NULL
,
1579 "failed to add a response packet to the tree.\n");
1583 iph1
->status
= PHASE1ST_MSG1SENT
;
1585 #ifdef ENABLE_VPNCONTROL_PORT
1586 vpncontrol_notify_phase_change(1, FROM_LOCAL
, iph1
, NULL
);
1591 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1592 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1593 CONSTSTR("Responder, Aggressive-Mode message 2"),
1598 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1599 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1600 CONSTSTR("Responder, Aggressive-Mode Message 2"),
1601 CONSTSTR("Failed to process Aggressive-Mode Message 2"));
1605 #ifdef ENABLE_HYBRID
1640 * receive from initiator
1642 * gssapi: HDR, HASH_I
1643 * sig: HDR, [ CERT, ] SIG_I
1648 agg_r2recv(iph1
, msg0
)
1649 struct ph1handle
*iph1
;
1652 vchar_t
*msg
= NULL
;
1653 vchar_t
*pbuf
= NULL
;
1654 struct isakmp_parse_t
*pa
;
1661 int received_cert
= 0;
1663 /* validity check */
1664 if (iph1
->status
!= PHASE1ST_MSG1SENT
) {
1665 plog(LLV_ERROR
, LOCATION
, NULL
,
1666 "status mismatched %d.\n", iph1
->status
);
1670 /* decrypting if need. */
1671 /* XXX configurable ? */
1672 if (ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
1673 msg
= oakley_do_decrypt(iph1
, msg0
,
1674 iph1
->ivm
->iv
, iph1
->ivm
->ive
);
1676 plog(LLV_ERROR
, LOCATION
, NULL
,
1677 "failed to decrypt msg");
1683 /* validate the type of next payload */
1684 pbuf
= isakmp_parse(msg
);
1686 plog(LLV_ERROR
, LOCATION
, NULL
,
1687 "failed to parse msg");
1691 iph1
->pl_hash
= NULL
;
1693 for (pa
= (struct isakmp_parse_t
*)pbuf
->v
;
1694 pa
->type
!= ISAKMP_NPTYPE_NONE
;
1698 case ISAKMP_NPTYPE_HASH
:
1699 iph1
->pl_hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
1701 case ISAKMP_NPTYPE_VID
:
1702 (void)check_vendorid(pa
->ptr
);
1704 case ISAKMP_NPTYPE_CERT
:
1705 if (oakley_savecert(iph1
, pa
->ptr
) < 0) {
1706 plog(LLV_ERROR
, LOCATION
, NULL
,
1707 "failed to process CERT payload");
1712 case ISAKMP_NPTYPE_SIG
:
1713 if (isakmp_p2ph(&iph1
->sig_p
, pa
->ptr
) < 0) {
1714 plog(LLV_ERROR
, LOCATION
, NULL
,
1715 "failed to process SIG payload");
1719 case ISAKMP_NPTYPE_N
:
1720 isakmp_check_notify(pa
->ptr
, iph1
);
1724 case ISAKMP_NPTYPE_NATD_DRAFT
:
1725 case ISAKMP_NPTYPE_NATD_RFC
:
1726 if (NATT_AVAILABLE(iph1
) && iph1
->natt_options
!= NULL
&&
1727 pa
->type
== iph1
->natt_options
->payload_nat_d
)
1729 vchar_t
*natd_received
= NULL
;
1732 if (isakmp_p2ph (&natd_received
, pa
->ptr
) < 0) {
1733 plog(LLV_ERROR
, LOCATION
, NULL
,
1734 "failed to process NATD payload");
1739 iph1
->natt_flags
|= NAT_DETECTED
;
1741 natd_verified
= natt_compare_addr_hash (iph1
,
1742 natd_received
, natd_seq
++);
1744 plog (LLV_INFO
, LOCATION
, NULL
, "NAT-D payload #%d %s\n",
1746 natd_verified
? "verified" : "doesn't match");
1748 vfree (natd_received
);
1751 /* %%%% Be lenient here - some servers send natd payloads */
1752 /* when no nat is detected */
1757 /* don't send information, see isakmp_ident_r1() */
1758 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1759 "ignore the packet, "
1760 "received unexpecting payload type %d.\n",
1767 if (NATT_AVAILABLE(iph1
))
1768 plog (LLV_INFO
, LOCATION
, NULL
, "NAT %s %s%s\n",
1769 iph1
->natt_flags
& NAT_DETECTED
?
1770 "detected:" : "not detected",
1771 iph1
->natt_flags
& NAT_DETECTED_ME
? "ME " : "",
1772 iph1
->natt_flags
& NAT_DETECTED_PEER
? "PEER" : "");
1775 if (received_cert
) {
1776 oakley_verify_certid(iph1
);
1779 /* validate authentication value */
1780 ptype
= oakley_validate_auth(iph1
);
1782 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1783 IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_FAIL
,
1784 CONSTSTR("Responder, Aggressive-Mode Message 3"),
1785 CONSTSTR("Failed to authenticate Aggressive-Mode Message 3"));
1787 /* message printed inner oakley_validate_auth() */
1790 EVT_PUSH(iph1
->local
, iph1
->remote
,
1791 EVTT_PEERPH1AUTH_FAILED
, NULL
);
1792 isakmp_info_send_n1(iph1
, ptype
, NULL
);
1795 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1796 IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_SUCC
,
1797 CONSTSTR("Responder, Aggressive-Mode Message 3"),
1800 iph1
->status
= PHASE1ST_MSG2RECEIVED
;
1804 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1805 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
1806 CONSTSTR("Responder, Aggressive-Mode message 3"),
1811 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1812 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
1813 CONSTSTR("Responder, Aggressive-Mode Message 3"),
1814 CONSTSTR("Failed to process Aggressive-Mode Message 3"));
1821 oakley_delcert(iph1
->cert_p
);
1822 iph1
->cert_p
= NULL
;
1823 oakley_delcert(iph1
->crl_p
);
1825 VPTRINIT(iph1
->sig_p
);
1832 * status update and establish isakmp sa.
1835 agg_r2send(iph1
, msg
)
1836 struct ph1handle
*iph1
;
1841 /* validity check */
1842 if (iph1
->status
!= PHASE1ST_MSG2RECEIVED
) {
1843 plog(LLV_ERROR
, LOCATION
, NULL
,
1844 "status mismatched %d.\n", iph1
->status
);
1848 /* IV synchronized when packet encrypted. */
1849 /* see handler.h about IV synchronization. */
1850 if (ISSET(((struct isakmp
*)msg
->v
)->flags
, ISAKMP_FLAG_E
))
1851 memcpy(iph1
->ivm
->iv
->v
, iph1
->ivm
->ive
->v
, iph1
->ivm
->iv
->l
);
1853 /* set encryption flag */
1854 iph1
->flags
|= ISAKMP_FLAG_E
;
1856 iph1
->status
= PHASE1ST_ESTABLISHED
;
1858 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1859 IPSECSESSIONEVENTCODE_IKEV1_PH1_RESP_SUCC
,
1860 CONSTSTR("Responder, Aggressive-Mode"),