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
;
388 vchar_t
*gsstoken
= NULL
;
390 int received_cert
= 0;
394 struct natd_payload
{
397 TAILQ_ENTRY(natd_payload
) chain
;
399 TAILQ_HEAD(_natd_payload
, natd_payload
) natd_tree
;
400 TAILQ_INIT(&natd_tree
);
404 if (iph1
->status
!= PHASE1ST_MSG1SENT
) {
405 plog(LLV_ERROR
, LOCATION
, NULL
,
406 "status mismatched %d.\n", iph1
->status
);
410 /* validate the type of next payload */
411 pbuf
= isakmp_parse(msg
);
413 plog(LLV_ERROR
, LOCATION
, NULL
,
414 "failed to parse msg");
417 pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
419 iph1
->pl_hash
= NULL
;
421 /* SA payload is fixed postion */
422 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
423 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
424 "received invalid next payload type %d, "
426 pa
->type
, ISAKMP_NPTYPE_SA
);
430 if (isakmp_p2ph(&satmp
, pa
->ptr
) < 0) {
431 plog(LLV_ERROR
, LOCATION
, NULL
,
432 "failed to process SA payload");
438 pa
->type
!= ISAKMP_NPTYPE_NONE
;
442 case ISAKMP_NPTYPE_KE
:
443 if (isakmp_p2ph(&iph1
->dhpub_p
, pa
->ptr
) < 0) {
444 plog(LLV_ERROR
, LOCATION
, NULL
,
445 "failed to process KE payload");
449 case ISAKMP_NPTYPE_NONCE
:
450 if (isakmp_p2ph(&iph1
->nonce_p
, pa
->ptr
) < 0) {
451 plog(LLV_ERROR
, LOCATION
, NULL
,
452 "failed to process NONCE payload");
456 case ISAKMP_NPTYPE_ID
:
457 if (isakmp_p2ph(&iph1
->id_p
, pa
->ptr
) < 0) {
458 plog(LLV_ERROR
, LOCATION
, NULL
,
459 "failed to process ID payload");
463 case ISAKMP_NPTYPE_HASH
:
464 iph1
->pl_hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
466 case ISAKMP_NPTYPE_CR
:
467 if (oakley_savecr(iph1
, pa
->ptr
) < 0) {
468 plog(LLV_ERROR
, LOCATION
, NULL
,
469 "failed to process CR payload");
473 case ISAKMP_NPTYPE_CERT
:
474 if (oakley_savecert(iph1
, pa
->ptr
) < 0) {
475 plog(LLV_ERROR
, LOCATION
, NULL
,
476 "failed to process CERT payload");
481 case ISAKMP_NPTYPE_SIG
:
482 if (isakmp_p2ph(&iph1
->sig_p
, pa
->ptr
) < 0) {
483 plog(LLV_ERROR
, LOCATION
, NULL
,
484 "failed to process SIG payload");
488 case ISAKMP_NPTYPE_VID
:
489 vid_numeric
= check_vendorid(pa
->ptr
);
491 if (iph1
->rmconf
->nat_traversal
&&
492 natt_vendorid(vid_numeric
))
493 natt_handle_vendorid(iph1
, vid_numeric
);
496 switch (vid_numeric
) {
498 iph1
->mode_cfg
->flags
|=
499 ISAKMP_CFG_VENDORID_XAUTH
;
503 iph1
->mode_cfg
->flags
|=
504 ISAKMP_CFG_VENDORID_UNITY
;
511 if (vid_numeric
== VENDORID_DPD
&& iph1
->rmconf
->dpd
) {
513 plog(LLV_DEBUG
, LOCATION
, NULL
,
514 "remote supports DPD\n");
518 if ((vid_numeric
== VENDORID_FRAG
) &&
519 (vendorid_frag_cap(pa
->ptr
) & VENDORID_FRAG_AGG
)) {
520 plog(LLV_DEBUG
, LOCATION
, NULL
,
521 "remote supports FRAGMENTATION\n");
526 case ISAKMP_NPTYPE_N
:
527 isakmp_check_notify(pa
->ptr
, iph1
);
530 case ISAKMP_NPTYPE_GSS
:
531 if (isakmp_p2ph(&gsstoken
, pa
->ptr
) < 0) {
532 plog(LLV_ERROR
, LOCATION
, NULL
,
533 "failed to process GSS payload");
536 gssapi_save_received_token(iph1
, gsstoken
);
541 case ISAKMP_NPTYPE_NATD_DRAFT
:
542 case ISAKMP_NPTYPE_NATD_RFC
:
543 case ISAKMP_NPTYPE_NATD_BADDRAFT
:
544 if (NATT_AVAILABLE(iph1
) && iph1
->natt_options
!= NULL
&&
545 pa
->type
== iph1
->natt_options
->payload_nat_d
) {
546 struct natd_payload
*natd
;
547 natd
= (struct natd_payload
*)racoon_malloc(sizeof(*natd
));
549 plog(LLV_ERROR
, LOCATION
, NULL
,
550 "failed to pre-process NATD payload");
554 natd
->payload
= NULL
;
556 if (isakmp_p2ph (&natd
->payload
, pa
->ptr
) < 0) {
557 plog(LLV_ERROR
, LOCATION
, NULL
,
558 "failed to process NATD payload");
562 natd
->seq
= natd_seq
++;
564 TAILQ_INSERT_TAIL(&natd_tree
, natd
, chain
);
567 /* %%% Be lenient here - some servers send natd payloads */
568 /* when nat not detected */
573 /* don't send information, see isakmp_ident_r1() */
574 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
575 "ignore the packet, "
576 "received unexpecting payload type %d.\n",
583 oakley_verify_certid(iph1
);
586 /* payload existency check */
587 if (iph1
->dhpub_p
== NULL
|| iph1
->nonce_p
== NULL
) {
588 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
589 "few isakmp message received.\n");
593 /* verify identifier */
594 if (ipsecdoi_checkid1(iph1
) != 0) {
595 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
596 "invalid ID payload.\n");
600 /* check SA payload and set approval SA for use */
601 if (ipsecdoi_checkph1proposal(satmp
, iph1
) < 0) {
602 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
603 "failed to get valid proposal.\n");
604 /* XXX send information */
607 VPTRINIT(iph1
->sa_ret
);
609 /* fix isakmp index */
610 memcpy(&iph1
->index
.r_ck
, &((struct isakmp
*)msg
->v
)->r_ck
,
614 if (NATT_AVAILABLE(iph1
)) {
615 struct natd_payload
*natd
= NULL
;
618 plog(LLV_INFO
, LOCATION
, iph1
->remote
,
619 "Selected NAT-T version: %s\n",
620 vid_string_by_id(iph1
->natt_options
->version
));
622 /* set both bits first so that we can clear them
623 upon verifying hashes */
624 iph1
->natt_flags
|= NAT_DETECTED
;
626 while ((natd
= TAILQ_FIRST(&natd_tree
)) != NULL
) {
627 /* this function will clear appropriate bits bits
628 from iph1->natt_flags */
629 natd_verified
= natt_compare_addr_hash (iph1
,
630 natd
->payload
, natd
->seq
);
632 plog (LLV_INFO
, LOCATION
, NULL
, "NAT-D payload #%d %s\n",
634 natd_verified
? "verified" : "doesn't match");
636 vfree (natd
->payload
);
638 TAILQ_REMOVE(&natd_tree
, natd
, chain
);
642 plog (LLV_INFO
, LOCATION
, NULL
, "NAT %s %s%s\n",
643 iph1
->natt_flags
& NAT_DETECTED
?
644 "detected:" : "not detected",
645 iph1
->natt_flags
& NAT_DETECTED_ME
? "ME " : "",
646 iph1
->natt_flags
& NAT_DETECTED_PEER
? "PEER" : "");
648 if (iph1
->natt_flags
& NAT_DETECTED
)
649 natt_float_ports (iph1
);
650 ike_session_update_natt_version(iph1
);
654 /* compute sharing secret of DH */
656 if (oakley_dh_compute(iph1
->rmconf
->dhgrp
, iph1
->dhpub
,
657 iph1
->dhpriv
, iph1
->dhpub_p
, &iph1
->dhgxy
) < 0) {
659 if (oakley_dh_compute(iph1
->rmconf
->dhgrp
, iph1
->dhpub_p
, iph1
->publicKeySize
, &iph1
->dhgxy
, iph1
->dhC
) < 0) {
661 plog(LLV_ERROR
, LOCATION
, NULL
,
662 "failed to compute DH");
666 /* generate SKEYIDs & IV & final cipher key */
667 if (oakley_skeyid(iph1
) < 0) {
668 plog(LLV_ERROR
, LOCATION
, NULL
,
669 "failed to generate SKEYID");
672 if (oakley_skeyid_dae(iph1
) < 0) {
673 plog(LLV_ERROR
, LOCATION
, NULL
,
674 "failed to generate SKEYID-DAE");
677 if (oakley_compute_enckey(iph1
) < 0) {
678 plog(LLV_ERROR
, LOCATION
, NULL
,
679 "failed to generate ENCKEY");
682 if (oakley_newiv(iph1
) < 0) {
683 plog(LLV_ERROR
, LOCATION
, NULL
,
684 "failed to generate IV");
688 /* validate authentication value */
689 ptype
= oakley_validate_auth(iph1
);
691 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
692 IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_FAIL
,
693 CONSTSTR("Initiator, Aggressive-Mode Message 2"),
694 CONSTSTR("Failed to authenticate, Aggressive-Mode Message 2"));
696 /* message printed inner oakley_validate_auth() */
699 EVT_PUSH(iph1
->local
, iph1
->remote
,
700 EVTT_PEERPH1AUTH_FAILED
, NULL
);
701 isakmp_info_send_n1(iph1
, ptype
, NULL
);
704 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
705 IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_SUCC
,
706 CONSTSTR("Initiator, Aggressive-Mode Message 2"),
709 if (oakley_checkcr(iph1
) < 0) {
710 /* Ignore this error in order to be interoperability. */
714 /* change status of isakmp status entry */
715 iph1
->status
= PHASE1ST_MSG2RECEIVED
;
717 #ifdef ENABLE_VPNCONTROL_PORT
718 vpncontrol_notify_phase_change(1, FROM_REMOTE
, iph1
, NULL
);
723 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
724 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
725 CONSTSTR("Initiator, Aggressive-Mode message 2"),
730 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
731 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
732 CONSTSTR("Initiator, Aggressive-Mode Message 2"),
733 CONSTSTR("Failure processing Aggressive-Mode Message 2"));
744 VPTRINIT(iph1
->dhpub_p
);
745 VPTRINIT(iph1
->nonce_p
);
746 VPTRINIT(iph1
->id_p
);
747 oakley_delcert(iph1
->cert_p
);
749 oakley_delcert(iph1
->crl_p
);
751 VPTRINIT(iph1
->sig_p
);
752 oakley_delcert(iph1
->cr_p
);
762 * gssapi: HDR, HASH_I
763 * sig: HDR, [ CERT, ] SIG_I
768 agg_i2send(iph1
, msg
)
769 struct ph1handle
*iph1
;
772 struct payload_list
*plist
= NULL
;
775 vchar_t
*gsshash
= NULL
;
777 vchar_t
*natd
[2] = { NULL
, NULL
};
779 vchar_t
*notp_unity
= NULL
;
780 vchar_t
*notp_ini
= NULL
;
783 if (iph1
->status
!= PHASE1ST_MSG2RECEIVED
) {
784 plog(LLV_ERROR
, LOCATION
, NULL
,
785 "status mismatched %d.\n", iph1
->status
);
789 /* generate HASH to send */
790 plog(LLV_DEBUG
, LOCATION
, NULL
, "generate HASH_I\n");
791 iph1
->hash
= oakley_ph1hash_common(iph1
, GENERATE
);
792 if (iph1
->hash
== NULL
) {
794 if (gssapi_more_tokens(iph1
) &&
796 !iph1
->rmconf
->xauth
&&
799 isakmp_info_send_n1(iph1
,
800 ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
, NULL
);
802 plog(LLV_ERROR
, LOCATION
, NULL
,
803 "failed to generate HASH");
807 switch (AUTHMETHOD(iph1
)) {
808 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
810 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
811 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
812 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_I
:
814 /* set HASH payload */
815 plist
= isakmp_plist_append(plist
,
816 iph1
->hash
, ISAKMP_NPTYPE_HASH
);
819 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
820 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
822 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
823 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_I
:
825 /* XXX if there is CR or not ? */
827 if (oakley_getmycert(iph1
) < 0) {
828 plog(LLV_ERROR
, LOCATION
, NULL
,
829 "failed to get mycert");
833 if (oakley_getsign(iph1
) < 0) {
834 plog(LLV_ERROR
, LOCATION
, NULL
,
835 "failed to get sign");
839 if (iph1
->cert
!= NULL
&& iph1
->rmconf
->send_cert
)
842 /* add CERT payload if there */
843 // we don't support sending of certchains
845 plist
= isakmp_plist_append(plist
, iph1
->cert
->pl
, ISAKMP_NPTYPE_CERT
);
847 /* add SIG payload */
848 plist
= isakmp_plist_append(plist
, iph1
->sig
, ISAKMP_NPTYPE_SIG
);
851 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
852 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
854 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
:
855 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
:
859 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
860 gsshash
= gssapi_wraphash(iph1
);
861 if (gsshash
== NULL
) {
862 plog(LLV_ERROR
, LOCATION
, NULL
,
863 "failed to get GSS hash\n");
864 isakmp_info_send_n1(iph1
,
865 ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
, NULL
);
869 plist
= isakmp_plist_append(plist
, gsshash
, ISAKMP_NPTYPE_HASH
);
875 /* generate NAT-D payloads */
876 if (NATT_AVAILABLE(iph1
)) {
877 plog (LLV_INFO
, LOCATION
, NULL
, "Adding remote and local NAT-D payloads.\n");
878 if ((natd
[0] = natt_hash_addr (iph1
, iph1
->remote
)) == NULL
) {
879 plog(LLV_ERROR
, LOCATION
, NULL
,
880 "NAT-D hashing failed for %s\n", saddr2str((struct sockaddr
*)iph1
->remote
));
884 if ((natd
[1] = natt_hash_addr (iph1
, iph1
->local
)) == NULL
) {
885 plog(LLV_ERROR
, LOCATION
, NULL
,
886 "NAT-D hashing failed for %s\n", saddr2str((struct sockaddr
*)iph1
->local
));
889 /* old Apple version sends natd payloads in the wrong order */
890 if (iph1
->natt_options
->version
== VENDORID_NATT_APPLE
) {
891 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
892 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
895 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
896 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
902 iph1
->sendbuf
= isakmp_plist_set_all (&plist
, iph1
);
904 #ifdef HAVE_PRINT_ISAKMP_C
905 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
909 /* send to responder */
910 if (isakmp_send(iph1
, iph1
->sendbuf
) < 0) {
911 plog(LLV_ERROR
, LOCATION
, NULL
,
912 "failed to send packet");
916 /* the sending message is added to the received-list. */
917 if (add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
,
918 PH1_NON_ESP_EXTRA_LEN(iph1
), PH1_FRAG_FLAGS(iph1
)) == -1) {
919 plog(LLV_ERROR
, LOCATION
, NULL
,
920 "failed to add a response packet to the tree.\n");
924 /* set encryption flag */
925 iph1
->flags
|= ISAKMP_FLAG_E
;
927 iph1
->status
= PHASE1ST_ESTABLISHED
;
929 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
930 IPSECSESSIONEVENTCODE_IKEV1_PH1_INIT_SUCC
,
931 CONSTSTR("Initiator, Aggressive-Mode"),
936 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
937 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
938 CONSTSTR("Initiator, Aggressive-Mode message 3"),
943 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
944 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
945 CONSTSTR("Initiator, Aggressive-Mode Message 3"),
946 CONSTSTR("Failed to transmit Aggressive-Mode Message 3"));
964 * receive from initiator
965 * psk: HDR, SA, KE, Ni, IDi1
966 * sig: HDR, SA, KE, Ni, IDi1 [, CR ]
967 * gssapi: HDR, SA, KE, Ni, IDi1 , GSSi
968 * rsa: HDR, SA, [ HASH(1),] KE, <IDi1_b>Pubkey_r, <Ni_b>Pubkey_r
969 * rev: HDR, SA, [ HASH(1),] <Ni_b>Pubkey_r, <KE_b>Ke_i,
970 * <IDii_b>Ke_i [, <Cert-I_b>Ke_i ]
973 agg_r1recv(iph1
, msg
)
974 struct ph1handle
*iph1
;
978 vchar_t
*pbuf
= NULL
;
979 struct isakmp_parse_t
*pa
;
982 vchar_t
*gsstoken
= NULL
;
986 if (iph1
->status
!= PHASE1ST_START
) {
987 plog(LLV_ERROR
, LOCATION
, NULL
,
988 "status mismatched %d.\n", iph1
->status
);
992 /* validate the type of next payload */
993 pbuf
= isakmp_parse(msg
);
995 plog(LLV_ERROR
, LOCATION
, NULL
,
996 "failed to parse msg");
999 pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
1001 /* SA payload is fixed postion */
1002 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
1003 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1004 "received invalid next payload type %d, "
1006 pa
->type
, ISAKMP_NPTYPE_SA
);
1009 if (isakmp_p2ph(&iph1
->sa
, pa
->ptr
) < 0) {
1010 plog(LLV_ERROR
, LOCATION
, NULL
,
1011 "failed to process SA payload");
1017 pa
->type
!= ISAKMP_NPTYPE_NONE
;
1020 plog(LLV_DEBUG
, LOCATION
, NULL
,
1021 "received payload of type %s\n",
1022 s_isakmp_nptype(pa
->type
));
1025 case ISAKMP_NPTYPE_KE
:
1026 if (isakmp_p2ph(&iph1
->dhpub_p
, pa
->ptr
) < 0) {
1027 plog(LLV_ERROR
, LOCATION
, NULL
,
1028 "failed to process KE payload");
1032 case ISAKMP_NPTYPE_NONCE
:
1033 if (isakmp_p2ph(&iph1
->nonce_p
, pa
->ptr
) < 0) {
1034 plog(LLV_ERROR
, LOCATION
, NULL
,
1035 "failed to process NONCE payload");
1039 case ISAKMP_NPTYPE_ID
:
1040 if (isakmp_p2ph(&iph1
->id_p
, pa
->ptr
) < 0) {
1041 plog(LLV_ERROR
, LOCATION
, NULL
,
1042 "failed to process ID payload");
1046 case ISAKMP_NPTYPE_VID
:
1047 vid_numeric
= check_vendorid(pa
->ptr
);
1050 if (iph1
->rmconf
->nat_traversal
&&
1051 natt_vendorid(vid_numeric
)) {
1052 natt_handle_vendorid(iph1
, vid_numeric
);
1056 #ifdef ENABLE_HYBRID
1057 switch (vid_numeric
) {
1058 case VENDORID_XAUTH
:
1059 iph1
->mode_cfg
->flags
|=
1060 ISAKMP_CFG_VENDORID_XAUTH
;
1063 case VENDORID_UNITY
:
1064 iph1
->mode_cfg
->flags
|=
1065 ISAKMP_CFG_VENDORID_UNITY
;
1072 if (vid_numeric
== VENDORID_DPD
&& iph1
->rmconf
->dpd
) {
1073 iph1
->dpd_support
=1;
1074 plog(LLV_DEBUG
, LOCATION
, NULL
,
1075 "remote supports DPD\n");
1079 if ((vid_numeric
== VENDORID_FRAG
) &&
1080 (vendorid_frag_cap(pa
->ptr
) & VENDORID_FRAG_AGG
)) {
1081 plog(LLV_DEBUG
, LOCATION
, NULL
,
1082 "remote supports FRAGMENTATION\n");
1088 case ISAKMP_NPTYPE_CR
:
1089 if (oakley_savecr(iph1
, pa
->ptr
) < 0) {
1090 plog(LLV_ERROR
, LOCATION
, NULL
,
1091 "failed to process CR payload");
1097 case ISAKMP_NPTYPE_GSS
:
1098 if (isakmp_p2ph(&gsstoken
, pa
->ptr
) < 0) {
1099 plog(LLV_ERROR
, LOCATION
, NULL
,
1100 "failed to process GSS payload");
1103 gssapi_save_received_token(iph1
, gsstoken
);
1107 /* don't send information, see isakmp_ident_r1() */
1108 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1109 "ignore the packet, "
1110 "received unexpecting payload type %d.\n",
1116 /* payload existency check */
1117 if (iph1
->dhpub_p
== NULL
|| iph1
->nonce_p
== NULL
) {
1118 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1119 "few isakmp message received.\n");
1123 /* verify identifier */
1124 if (ipsecdoi_checkid1(iph1
) != 0) {
1125 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1126 "invalid ID payload.\n");
1131 if (NATT_AVAILABLE(iph1
)) {
1132 plog(LLV_INFO
, LOCATION
, iph1
->remote
,
1133 "Selected NAT-T version: %s\n",
1134 vid_string_by_id(iph1
->natt_options
->version
));
1135 ike_session_update_natt_version(iph1
);
1139 /* check SA payload and set approval SA for use */
1140 if (ipsecdoi_checkph1proposal(iph1
->sa
, iph1
) < 0) {
1141 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1142 "failed to get valid proposal.\n");
1143 /* XXX send information */
1147 if (oakley_checkcr(iph1
) < 0) {
1148 /* Ignore this error in order to be interoperability. */
1152 iph1
->status
= PHASE1ST_MSG1RECEIVED
;
1156 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1157 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
1158 CONSTSTR("Responder, Aggressive-Mode message 1"),
1163 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1164 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
1165 CONSTSTR("Responder, Aggressive-Mode Message 1"),
1166 CONSTSTR("Failed to process Aggressive-Mode Message 1"));
1176 VPTRINIT(iph1
->dhpub_p
);
1177 VPTRINIT(iph1
->nonce_p
);
1178 VPTRINIT(iph1
->id_p
);
1179 oakley_delcert(iph1
->cr_p
);
1188 * psk: HDR, SA, KE, Nr, IDr1, HASH_R
1189 * sig: HDR, SA, KE, Nr, IDr1, [ CR, ] [ CERT, ] SIG_R
1190 * gssapi: HDR, SA, KE, Nr, IDr1, GSSr, HASH_R
1191 * rsa: HDR, SA, KE, <IDr1_b>PubKey_i, <Nr_b>PubKey_i, HASH_R
1192 * rev: HDR, SA, <Nr_b>PubKey_i, <KE_b>Ke_r, <IDir_b>Ke_r, HASH_R
1195 agg_r1send(iph1
, msg
)
1196 struct ph1handle
*iph1
;
1199 struct payload_list
*plist
= NULL
;
1204 #ifdef ENABLE_HYBRID
1205 vchar_t
*xauth_vid
= NULL
;
1206 vchar_t
*unity_vid
= NULL
;
1209 vchar_t
*vid_natt
= NULL
;
1210 vchar_t
*natd
[2] = { NULL
, NULL
};
1213 vchar_t
*vid_dpd
= NULL
;
1216 vchar_t
*vid_frag
= NULL
;
1221 vchar_t
*gsstoken
= NULL
, *gsshash
= NULL
;
1222 vchar_t
*gss_sa
= NULL
;
1223 int free_gss_sa
= 0;
1226 /* validity check */
1227 if (iph1
->status
!= PHASE1ST_MSG1RECEIVED
) {
1228 plog(LLV_ERROR
, LOCATION
, NULL
,
1229 "status mismatched %d.\n", iph1
->status
);
1233 /* set responder's cookie */
1234 isakmp_newcookie((caddr_t
)&iph1
->index
.r_ck
, iph1
->remote
, iph1
->local
);
1236 /* make ID payload into isakmp status */
1237 if (ipsecdoi_setid1(iph1
) < 0) {
1238 plog(LLV_ERROR
, LOCATION
, NULL
,
1239 "failed to set ID");
1243 /* generate DH public value */
1245 if (oakley_dh_generate(iph1
->rmconf
->dhgrp
,
1246 &iph1
->dhpub
, &iph1
->dhpriv
) < 0) {
1248 if (oakley_dh_generate(iph1
->rmconf
->dhgrp
,
1249 &iph1
->dhpub
, &iph1
->publicKeySize
, &iph1
->dhC
) < 0) {
1251 plog(LLV_ERROR
, LOCATION
, NULL
,
1252 "failed to generate DH");
1256 /* generate NONCE value */
1257 iph1
->nonce
= eay_set_random(iph1
->rmconf
->nonce_size
);
1258 if (iph1
->nonce
== NULL
) {
1259 plog(LLV_ERROR
, LOCATION
, NULL
,
1260 "failed to generate NONCE");
1264 /* compute sharing secret of DH */
1266 if (oakley_dh_compute(iph1
->approval
->dhgrp
, iph1
->dhpub
,
1267 iph1
->dhpriv
, iph1
->dhpub_p
, &iph1
->dhgxy
) < 0) {
1269 if (oakley_dh_compute(iph1
->approval
->dhgrp
, iph1
->dhpub_p
, iph1
->publicKeySize
, &iph1
->dhgxy
, iph1
->dhC
) < 0) {
1271 plog(LLV_ERROR
, LOCATION
, NULL
,
1272 "failed to compute DH");
1276 /* generate SKEYIDs & IV & final cipher key */
1277 if (oakley_skeyid(iph1
) < 0) {
1278 plog(LLV_ERROR
, LOCATION
, NULL
,
1279 "failed to generate SKEYID");
1282 if (oakley_skeyid_dae(iph1
) < 0) {
1283 plog(LLV_ERROR
, LOCATION
, NULL
,
1284 "failed to generate SKEYID-DAE");
1287 if (oakley_compute_enckey(iph1
) < 0) {
1288 plog(LLV_ERROR
, LOCATION
, NULL
,
1289 "failed to generate ENCKEY");
1292 if (oakley_newiv(iph1
) < 0) {
1293 plog(LLV_ERROR
, LOCATION
, NULL
,
1294 "failed to generate IV");
1299 if (RMAUTHMETHOD(iph1
) == OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
)
1300 gssapi_get_rtoken(iph1
, &gsslen
);
1303 /* generate HASH to send */
1304 plog(LLV_DEBUG
, LOCATION
, NULL
, "generate HASH_R\n");
1305 iph1
->hash
= oakley_ph1hash_common(iph1
, GENERATE
);
1306 if (iph1
->hash
== NULL
) {
1308 if (gssapi_more_tokens(iph1
))
1309 isakmp_info_send_n1(iph1
,
1310 ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
, NULL
);
1312 plog(LLV_ERROR
, LOCATION
, NULL
,
1313 "failed to generate GSS HASH");
1317 /* create CR if need */
1318 if (iph1
->rmconf
->send_cr
1319 && oakley_needcr(iph1
->approval
->authmethod
)
1320 && iph1
->rmconf
->peerscertfile
== NULL
) {
1322 cr
= oakley_getcr(iph1
);
1324 plog(LLV_ERROR
, LOCATION
, NULL
,
1325 "failed to get CR.\n");
1331 /* Has the peer announced NAT-T? */
1332 if (NATT_AVAILABLE(iph1
)) {
1333 /* set chosen VID */
1334 vid_natt
= set_vendorid(iph1
->natt_options
->version
);
1336 /* generate NAT-D payloads */
1337 plog (LLV_INFO
, LOCATION
, NULL
, "Adding remote and local NAT-D payloads.\n");
1338 if ((natd
[0] = natt_hash_addr (iph1
, iph1
->remote
)) == NULL
) {
1339 plog(LLV_ERROR
, LOCATION
, NULL
,
1340 "NAT-D hashing failed for %s\n", saddr2str((struct sockaddr
*)iph1
->remote
));
1344 if ((natd
[1] = natt_hash_addr (iph1
, iph1
->local
)) == NULL
) {
1345 plog(LLV_ERROR
, LOCATION
, NULL
,
1346 "NAT-D hashing failed for %s\n", saddr2str((struct sockaddr
*)iph1
->local
));
1352 /* Only send DPD support if remote announced DPD and if DPD support is active */
1353 if (iph1
->dpd_support
&& iph1
->rmconf
->dpd
)
1354 vid_dpd
= set_vendorid(VENDORID_DPD
);
1358 vid_frag
= set_vendorid(VENDORID_FRAG
);
1359 if (vid_frag
!= NULL
)
1360 vid_frag
= isakmp_frag_addcap(vid_frag
,
1362 if (vid_frag
== NULL
)
1363 plog(LLV_ERROR
, LOCATION
, NULL
,
1364 "Frag vendorID construction failed\n");
1368 switch (AUTHMETHOD(iph1
)) {
1369 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
1370 #ifdef ENABLE_HYBRID
1371 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1373 /* set SA payload to reply */
1374 plist
= isakmp_plist_append(plist
, iph1
->sa_ret
, ISAKMP_NPTYPE_SA
);
1376 /* create isakmp KE payload */
1377 plist
= isakmp_plist_append(plist
, iph1
->dhpub
, ISAKMP_NPTYPE_KE
);
1379 /* create isakmp NONCE payload */
1380 plist
= isakmp_plist_append(plist
, iph1
->nonce
, ISAKMP_NPTYPE_NONCE
);
1382 /* create isakmp ID payload */
1383 plist
= isakmp_plist_append(plist
, iph1
->id
, ISAKMP_NPTYPE_ID
);
1385 /* create isakmp HASH payload */
1386 plist
= isakmp_plist_append(plist
,
1387 iph1
->hash
, ISAKMP_NPTYPE_HASH
);
1389 /* create isakmp CR payload if needed */
1391 plist
= isakmp_plist_append(plist
, cr
, ISAKMP_NPTYPE_CR
);
1393 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
1394 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
1395 #ifdef ENABLE_HYBRID
1396 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1397 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_R
:
1398 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1399 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_R
:
1401 /* XXX if there is CR or not ? */
1403 if (oakley_getmycert(iph1
) < 0) {
1404 plog(LLV_ERROR
, LOCATION
, NULL
,
1405 "failed to get mycert");
1409 if (oakley_getsign(iph1
) < 0) {
1410 plog(LLV_ERROR
, LOCATION
, NULL
,
1411 "failed to get sign");
1415 if (iph1
->cert
!= NULL
&& iph1
->rmconf
->send_cert
)
1418 /* set SA payload to reply */
1419 plist
= isakmp_plist_append(plist
, iph1
->sa_ret
, ISAKMP_NPTYPE_SA
);
1421 /* create isakmp KE payload */
1422 plist
= isakmp_plist_append(plist
, iph1
->dhpub
, ISAKMP_NPTYPE_KE
);
1424 /* create isakmp NONCE payload */
1425 plist
= isakmp_plist_append(plist
, iph1
->nonce
, ISAKMP_NPTYPE_NONCE
);
1427 /* add ID payload */
1428 plist
= isakmp_plist_append(plist
, iph1
->id
, ISAKMP_NPTYPE_ID
);
1430 /* add CERT payload if there */
1432 plist
= isakmp_plist_append(plist
, iph1
->cert
->pl
, ISAKMP_NPTYPE_CERT
);
1434 /* add SIG payload */
1435 plist
= isakmp_plist_append(plist
, iph1
->sig
, ISAKMP_NPTYPE_SIG
);
1437 /* create isakmp CR payload if needed */
1439 plist
= isakmp_plist_append(plist
,
1440 cr
, ISAKMP_NPTYPE_CR
);
1443 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
1444 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
1445 #ifdef ENABLE_HYBRID
1446 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1447 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1451 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
1452 /* create buffer to send isakmp payload */
1453 gsshash
= gssapi_wraphash(iph1
);
1454 if (gsshash
== NULL
) {
1455 plog(LLV_ERROR
, LOCATION
, NULL
,
1456 "failed to generate GSS HASH\n");
1458 * This is probably due to the GSS
1459 * roundtrips not being finished yet.
1460 * Return this error in the hope that
1461 * a fallback to main mode will be done.
1463 isakmp_info_send_n1(iph1
,
1464 ISAKMP_NTYPE_INVALID_EXCHANGE_TYPE
, NULL
);
1467 if (iph1
->approval
->gssid
!= NULL
)
1469 ipsecdoi_setph1proposal(iph1
->approval
);
1471 gss_sa
= iph1
->sa_ret
;
1473 if (gss_sa
!= iph1
->sa_ret
)
1476 /* set SA payload to reply */
1477 plist
= isakmp_plist_append(plist
,
1478 gss_sa
, ISAKMP_NPTYPE_SA
);
1480 /* create isakmp KE payload */
1481 plist
= isakmp_plist_append(plist
,
1482 iph1
->dhpub
, ISAKMP_NPTYPE_KE
);
1484 /* create isakmp NONCE payload */
1485 plist
= isakmp_plist_append(plist
,
1486 iph1
->nonce
, ISAKMP_NPTYPE_NONCE
);
1488 /* create isakmp ID payload */
1489 plist
= isakmp_plist_append(plist
,
1490 iph1
->id
, ISAKMP_NPTYPE_ID
);
1492 /* create GSS payload */
1493 gssapi_get_token_to_send(iph1
, &gsstoken
);
1494 plist
= isakmp_plist_append(plist
,
1495 gsstoken
, ISAKMP_NPTYPE_GSS
);
1497 /* create isakmp HASH payload */
1498 plist
= isakmp_plist_append(plist
,
1499 gsshash
, ISAKMP_NPTYPE_HASH
);
1501 /* append vendor id, if needed */
1506 #ifdef ENABLE_HYBRID
1507 if (iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) {
1508 plog (LLV_INFO
, LOCATION
, NULL
, "Adding xauth VID payload.\n");
1509 if ((xauth_vid
= set_vendorid(VENDORID_XAUTH
)) == NULL
) {
1510 plog(LLV_ERROR
, LOCATION
, NULL
,
1511 "Cannot create Xauth vendor ID\n");
1514 plist
= isakmp_plist_append(plist
,
1515 xauth_vid
, ISAKMP_NPTYPE_VID
);
1518 if (iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_UNITY
) {
1519 if ((unity_vid
= set_vendorid(VENDORID_UNITY
)) == NULL
) {
1520 plog(LLV_ERROR
, LOCATION
, NULL
,
1521 "Cannot create Unity vendor ID\n");
1524 plist
= isakmp_plist_append(plist
,
1525 unity_vid
, ISAKMP_NPTYPE_VID
);
1530 /* append NAT-T payloads */
1533 plist
= isakmp_plist_append(plist
, vid_natt
, ISAKMP_NPTYPE_VID
);
1535 /* old Apple version sends natd payloads in the wrong order */
1536 if (iph1
->natt_options
->version
== VENDORID_NATT_APPLE
) {
1537 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
1538 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
1541 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
1542 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
1549 plist
= isakmp_plist_append(plist
, vid_frag
, ISAKMP_NPTYPE_VID
);
1554 plist
= isakmp_plist_append(plist
, vid_dpd
, ISAKMP_NPTYPE_VID
);
1557 iph1
->sendbuf
= isakmp_plist_set_all (&plist
, iph1
);
1559 #ifdef HAVE_PRINT_ISAKMP_C
1560 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 1);
1563 /* send the packet, add to the schedule to resend */
1564 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
1565 if (isakmp_ph1resend(iph1
) == -1) {
1566 plog(LLV_ERROR
, LOCATION
, NULL
,
1567 "failed to send packet");
1571 /* the sending message is added to the received-list. */
1572 if (add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
,
1573 PH1_NON_ESP_EXTRA_LEN(iph1
), PH1_FRAG_FLAGS(iph1
)) == -1) {
1574 plog(LLV_ERROR
, LOCATION
, NULL
,
1575 "failed to add a response packet to the tree.\n");
1579 iph1
->status
= PHASE1ST_MSG1SENT
;
1581 #ifdef ENABLE_VPNCONTROL_PORT
1582 vpncontrol_notify_phase_change(1, FROM_LOCAL
, iph1
, NULL
);
1587 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1588 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1589 CONSTSTR("Responder, Aggressive-Mode message 2"),
1594 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1595 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1596 CONSTSTR("Responder, Aggressive-Mode Message 2"),
1597 CONSTSTR("Failed to process Aggressive-Mode Message 2"));
1601 #ifdef ENABLE_HYBRID
1636 * receive from initiator
1638 * gssapi: HDR, HASH_I
1639 * sig: HDR, [ CERT, ] SIG_I
1644 agg_r2recv(iph1
, msg0
)
1645 struct ph1handle
*iph1
;
1648 vchar_t
*msg
= NULL
;
1649 vchar_t
*pbuf
= NULL
;
1650 struct isakmp_parse_t
*pa
;
1657 int received_cert
= 0;
1659 /* validity check */
1660 if (iph1
->status
!= PHASE1ST_MSG1SENT
) {
1661 plog(LLV_ERROR
, LOCATION
, NULL
,
1662 "status mismatched %d.\n", iph1
->status
);
1666 /* decrypting if need. */
1667 /* XXX configurable ? */
1668 if (ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
1669 msg
= oakley_do_decrypt(iph1
, msg0
,
1670 iph1
->ivm
->iv
, iph1
->ivm
->ive
);
1672 plog(LLV_ERROR
, LOCATION
, NULL
,
1673 "failed to decrypt msg");
1679 /* validate the type of next payload */
1680 pbuf
= isakmp_parse(msg
);
1682 plog(LLV_ERROR
, LOCATION
, NULL
,
1683 "failed to parse msg");
1687 iph1
->pl_hash
= NULL
;
1689 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
1690 pa
->type
!= ISAKMP_NPTYPE_NONE
;
1694 case ISAKMP_NPTYPE_HASH
:
1695 iph1
->pl_hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
1697 case ISAKMP_NPTYPE_VID
:
1698 (void)check_vendorid(pa
->ptr
);
1700 case ISAKMP_NPTYPE_CERT
:
1701 if (oakley_savecert(iph1
, pa
->ptr
) < 0) {
1702 plog(LLV_ERROR
, LOCATION
, NULL
,
1703 "failed to process CERT payload");
1708 case ISAKMP_NPTYPE_SIG
:
1709 if (isakmp_p2ph(&iph1
->sig_p
, pa
->ptr
) < 0) {
1710 plog(LLV_ERROR
, LOCATION
, NULL
,
1711 "failed to process SIG payload");
1715 case ISAKMP_NPTYPE_N
:
1716 isakmp_check_notify(pa
->ptr
, iph1
);
1720 case ISAKMP_NPTYPE_NATD_DRAFT
:
1721 case ISAKMP_NPTYPE_NATD_RFC
:
1722 if (NATT_AVAILABLE(iph1
) && iph1
->natt_options
!= NULL
&&
1723 pa
->type
== iph1
->natt_options
->payload_nat_d
)
1725 vchar_t
*natd_received
= NULL
;
1728 if (isakmp_p2ph (&natd_received
, pa
->ptr
) < 0) {
1729 plog(LLV_ERROR
, LOCATION
, NULL
,
1730 "failed to process NATD payload");
1735 iph1
->natt_flags
|= NAT_DETECTED
;
1737 natd_verified
= natt_compare_addr_hash (iph1
,
1738 natd_received
, natd_seq
++);
1740 plog (LLV_INFO
, LOCATION
, NULL
, "NAT-D payload #%d %s\n",
1742 natd_verified
? "verified" : "doesn't match");
1744 vfree (natd_received
);
1747 /* %%%% Be lenient here - some servers send natd payloads */
1748 /* when no nat is detected */
1753 /* don't send information, see isakmp_ident_r1() */
1754 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1755 "ignore the packet, "
1756 "received unexpecting payload type %d.\n",
1763 if (NATT_AVAILABLE(iph1
))
1764 plog (LLV_INFO
, LOCATION
, NULL
, "NAT %s %s%s\n",
1765 iph1
->natt_flags
& NAT_DETECTED
?
1766 "detected:" : "not detected",
1767 iph1
->natt_flags
& NAT_DETECTED_ME
? "ME " : "",
1768 iph1
->natt_flags
& NAT_DETECTED_PEER
? "PEER" : "");
1771 if (received_cert
) {
1772 oakley_verify_certid(iph1
);
1775 /* validate authentication value */
1776 ptype
= oakley_validate_auth(iph1
);
1778 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1779 IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_FAIL
,
1780 CONSTSTR("Responder, Aggressive-Mode Message 3"),
1781 CONSTSTR("Failed to authenticate Aggressive-Mode Message 3"));
1783 /* message printed inner oakley_validate_auth() */
1786 EVT_PUSH(iph1
->local
, iph1
->remote
,
1787 EVTT_PEERPH1AUTH_FAILED
, NULL
);
1788 isakmp_info_send_n1(iph1
, ptype
, NULL
);
1791 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1792 IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_SUCC
,
1793 CONSTSTR("Responder, Aggressive-Mode Message 3"),
1796 iph1
->status
= PHASE1ST_MSG2RECEIVED
;
1800 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1801 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
1802 CONSTSTR("Responder, Aggressive-Mode message 3"),
1807 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1808 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
1809 CONSTSTR("Responder, Aggressive-Mode Message 3"),
1810 CONSTSTR("Failed to process Aggressive-Mode Message 3"));
1817 oakley_delcert(iph1
->cert_p
);
1818 iph1
->cert_p
= NULL
;
1819 oakley_delcert(iph1
->crl_p
);
1821 VPTRINIT(iph1
->sig_p
);
1828 * status update and establish isakmp sa.
1831 agg_r2send(iph1
, msg
)
1832 struct ph1handle
*iph1
;
1837 /* validity check */
1838 if (iph1
->status
!= PHASE1ST_MSG2RECEIVED
) {
1839 plog(LLV_ERROR
, LOCATION
, NULL
,
1840 "status mismatched %d.\n", iph1
->status
);
1844 /* IV synchronized when packet encrypted. */
1845 /* see handler.h about IV synchronization. */
1846 if (ISSET(((struct isakmp
*)msg
->v
)->flags
, ISAKMP_FLAG_E
))
1847 memcpy(iph1
->ivm
->iv
->v
, iph1
->ivm
->ive
->v
, iph1
->ivm
->iv
->l
);
1849 /* set encryption flag */
1850 iph1
->flags
|= ISAKMP_FLAG_E
;
1852 iph1
->status
= PHASE1ST_ESTABLISHED
;
1854 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1855 IPSECSESSIONEVENTCODE_IKEV1_PH1_RESP_SUCC
,
1856 CONSTSTR("Responder, Aggressive-Mode"),