1 /* $NetBSD: isakmp_ident.c,v 1.6 2006/10/02 21:41:59 manu Exp $ */
3 /* Id: isakmp_ident.c,v 1.21 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 /* Identity Protecion Exchange (Main 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>
65 #include "localconf.h"
66 #include "remoteconf.h"
67 #include "isakmp_var.h"
71 #include "ipsec_doi.h"
72 #include "crypto_openssl.h"
74 #include "isakmp_ident.h"
75 #include "isakmp_inf.h"
79 #include "nattraversal.h"
83 #include "isakmp_xauth.h"
84 #include "isakmp_cfg.h"
87 #include "isakmp_frag.h"
90 #include "vpn_control.h"
91 #include "vpn_control_var.h"
92 #include "ipsecSessionTracer.h"
93 #include "ipsecMessageTracer.h"
95 #include <Security/SecDH.h>
98 static vchar_t
*ident_ir2mx (phase1_handle_t
*);
99 static vchar_t
*ident_ir3mx (phase1_handle_t
*);
102 * begin Identity Protection Mode as initiator.
112 ident_i1send(iph1
, msg
)
113 phase1_handle_t
*iph1
;
114 vchar_t
*msg
; /* must be null */
116 struct payload_list
*plist
= NULL
;
119 vchar_t
*vid_natt
[MAX_NATT_VID_COUNT
] = { NULL
};
123 vchar_t
*vid_xauth
= NULL
;
124 vchar_t
*vid_unity
= NULL
;
127 vchar_t
*vid_frag
= NULL
;
130 vchar_t
*vid_dpd
= NULL
;
134 if (iph1
->status
!= IKEV1_STATE_IDENT_I_START
) {
136 "status mismatched %d.\n", iph1
->status
);
143 "msg has to be NULL in this function.\n");
147 /* create isakmp index */
148 memset(&iph1
->index
, 0, sizeof(iph1
->index
));
149 isakmp_newcookie((caddr_t
)&iph1
->index
, iph1
->remote
, iph1
->local
);
151 /* create SA payload for my proposal */
152 iph1
->sa
= ipsecdoi_setph1proposal(iph1
);
153 if (iph1
->sa
== NULL
) {
155 "failed to set proposal");
159 /* set SA payload to propose */
160 plist
= isakmp_plist_append(plist
, iph1
->sa
, ISAKMP_NPTYPE_SA
);
163 /* set VID payload for NAT-T if NAT-T support allowed in the config file */
164 if (iph1
->rmconf
->nat_traversal
)
165 plist
= isakmp_plist_append_natt_vids(plist
, vid_natt
);
168 /* Do we need Xauth VID? */
169 switch (RMAUTHMETHOD(iph1
)) {
170 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
171 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
172 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
173 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
:
174 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
:
175 if ((vid_xauth
= set_vendorid(VENDORID_XAUTH
)) == NULL
)
177 "Xauth vendor ID generation failed\n");
179 plist
= isakmp_plist_append(plist
,
180 vid_xauth
, ISAKMP_NPTYPE_VID
);
182 if ((vid_unity
= set_vendorid(VENDORID_UNITY
)) == NULL
)
184 "Unity vendor ID generation failed\n");
186 plist
= isakmp_plist_append(plist
,
187 vid_unity
, ISAKMP_NPTYPE_VID
);
194 if (iph1
->rmconf
->ike_frag
) {
195 if ((vid_frag
= set_vendorid(VENDORID_FRAG
)) == NULL
) {
197 "Frag vendorID construction failed\n");
199 vid_frag
= isakmp_frag_addcap(vid_frag
,
200 VENDORID_FRAG_IDENT
);
201 plist
= isakmp_plist_append(plist
,
202 vid_frag
, ISAKMP_NPTYPE_VID
);
207 if(iph1
->rmconf
->dpd
){
208 vid_dpd
= set_vendorid(VENDORID_DPD
);
210 plist
= isakmp_plist_append(plist
, vid_dpd
,
215 iph1
->sendbuf
= isakmp_plist_set_all (&plist
, iph1
);
217 #ifdef HAVE_PRINT_ISAKMP_C
218 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
221 /* send the packet, add to the schedule to resend */
222 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
223 if (isakmp_ph1resend(iph1
) == -1) {
225 "failed to send packet");
229 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_I_MSG1SENT
);
233 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
234 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
235 CONSTSTR("Initiator, Main-Mode message 1"),
240 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
241 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
242 CONSTSTR("Initiator, Main-Mode Message 1"),
243 CONSTSTR("Failed to transmit Main-Mode Message 1"));
250 for (i
= 0; i
< MAX_NATT_VID_COUNT
&& vid_natt
[i
] != NULL
; i
++)
254 if (vid_xauth
!= NULL
)
256 if (vid_unity
!= NULL
)
268 * receive from responder
275 ident_i2recv(iph1
, msg
)
276 phase1_handle_t
*iph1
;
279 vchar_t
*pbuf
= NULL
;
280 struct isakmp_parse_t
*pa
;
281 vchar_t
*satmp
= NULL
;
286 if (iph1
->status
!= IKEV1_STATE_IDENT_I_MSG1SENT
) {
288 "status mismatched %d.\n", iph1
->status
);
292 /* validate the type of next payload */
294 * NOTE: RedCreek(as responder) attaches N[responder-lifetime] here,
295 * if proposal-lifetime > lifetime-redcreek-wants.
297 * => According to the seciton 4.6.3 in RFC 2407, This is illegal.
298 * NOTE: we do not really care about ordering of VID and N.
300 * NOTE: even if there's multiple VID/N, we'll ignore them.
302 pbuf
= isakmp_parse(msg
);
305 "failed to parse msg");
308 pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
310 /* SA payload is fixed postion */
311 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
313 "received invalid next payload type %d, "
315 pa
->type
, ISAKMP_NPTYPE_SA
);
318 if (isakmp_p2ph(&satmp
, pa
->ptr
) < 0) {
320 "failed to process SA payload");
326 pa
->type
!= ISAKMP_NPTYPE_NONE
;
330 case ISAKMP_NPTYPE_VID
:
331 vid_numeric
= check_vendorid(pa
->ptr
);
333 if (iph1
->rmconf
->nat_traversal
&& natt_vendorid(vid_numeric
))
334 natt_handle_vendorid(iph1
, vid_numeric
);
337 switch (vid_numeric
) {
339 iph1
->mode_cfg
->flags
|=
340 ISAKMP_CFG_VENDORID_XAUTH
;
344 iph1
->mode_cfg
->flags
|=
345 ISAKMP_CFG_VENDORID_UNITY
;
353 if (vid_numeric
== VENDORID_DPD
&& iph1
->rmconf
->dpd
)
357 if ((vid_numeric
== VENDORID_FRAG
) &&
358 (vendorid_frag_cap(pa
->ptr
) & VENDORID_FRAG_IDENT
)) {
359 plog(ASL_LEVEL_DEBUG
,
360 "remote supports FRAGMENTATION\n");
366 /* don't send information, see ident_r1recv() */
368 "ignore the packet, "
369 "received unexpecting payload type %d.\n",
376 if (NATT_AVAILABLE(iph1
)) {
378 "Selected NAT-T version: %s\n",
379 vid_string_by_id(iph1
->natt_options
->version
));
380 ike_session_update_natt_version(iph1
);
384 /* check SA payload and set approval SA for use */
385 if (ipsecdoi_checkph1proposal(satmp
, iph1
) < 0) {
387 "failed to get valid proposal.\n");
388 /* XXX send information */
391 VPTRINIT(iph1
->sa_ret
);
393 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_I_MSG2RCVD
);
395 #ifdef ENABLE_VPNCONTROL_PORT
396 vpncontrol_notify_phase_change(1, FROM_REMOTE
, iph1
, NULL
);
401 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
402 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
403 CONSTSTR("Initiator, Main-Mode message 2"),
408 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
409 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
410 CONSTSTR("Initiator, Main-Mode Message 2"),
411 CONSTSTR("Failed to process Main-Mode Message 2"));
424 * gssapi: HDR, KE, Ni, GSSi
425 * rsa: HDR, KE, [ HASH(1), ] <IDi1_b>PubKey_r, <Ni_b>PubKey_r
426 * rev: HDR, [ HASH(1), ] <Ni_b>Pubkey_r, <KE_b>Ke_i,
427 * <IDi1_b>Ke_i, [<<Cert-I_b>Ke_i]
430 ident_i3send(iph1
, msg
)
431 phase1_handle_t
*iph1
;
437 if (iph1
->status
!= IKEV1_STATE_IDENT_I_MSG2RCVD
) {
439 "status mismatched %d.\n", iph1
->status
);
443 /* fix isakmp index */
444 memcpy(&iph1
->index
.r_ck
, &((struct isakmp
*)msg
->v
)->r_ck
,
447 /* generate DH public value */
449 if (oakley_dh_generate(iph1
->approval
->dhgrp
,
450 &iph1
->dhpub
, &iph1
->dhpriv
) < 0) {
452 if (oakley_dh_generate(iph1
->approval
->dhgrp
,
453 &iph1
->dhpub
, &iph1
->publicKeySize
, &iph1
->dhC
) < 0) {
456 "failed to generate DH");
460 /* generate NONCE value */
461 iph1
->nonce
= eay_set_random(iph1
->rmconf
->nonce_size
);
462 if (iph1
->nonce
== NULL
) {
464 "failed to generate NONCE");
468 /* create buffer to send isakmp payload */
469 iph1
->sendbuf
= ident_ir2mx(iph1
);
470 if (iph1
->sendbuf
== NULL
) {
472 "failed to create send buffer");
476 #ifdef HAVE_PRINT_ISAKMP_C
477 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
480 /* send the packet, add to the schedule to resend */
481 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
482 if (isakmp_ph1resend(iph1
) == -1) {
484 "failed to send packet");
488 /* the sending message is added to the received-list. */
489 if (ike_session_add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
,
490 PH1_NON_ESP_EXTRA_LEN(iph1
, iph1
->sendbuf
), PH1_FRAG_FLAGS(iph1
)) == -1) {
492 "failed to add a response packet to the tree.\n");
496 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_I_MSG3SENT
);
500 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
501 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
502 CONSTSTR("Initiator, Main-Mode message 3"),
507 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
508 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
509 CONSTSTR("Initiator, Main-Mode Message 3"),
510 CONSTSTR("Failed to transmit Main-Mode Message 3"));
516 * receive from responder
518 * sig: HDR, KE, Nr [, CR ]
519 * gssapi: HDR, KE, Nr, GSSr
520 * rsa: HDR, KE, <IDr1_b>PubKey_i, <Nr_b>PubKey_i
521 * rev: HDR, <Nr_b>PubKey_i, <KE_b>Ke_r, <IDr1_b>Ke_r,
524 ident_i4recv(iph1
, msg
)
525 phase1_handle_t
*iph1
;
528 vchar_t
*pbuf
= NULL
;
529 struct isakmp_parse_t
*pa
;
533 vchar_t
*natd_received
;
534 int natd_seq
= 0, natd_verified
;
538 if (iph1
->status
!= IKEV1_STATE_IDENT_I_MSG3SENT
) {
540 "status mismatched %d.\n", iph1
->status
);
544 /* validate the type of next payload */
545 pbuf
= isakmp_parse(msg
);
548 "failed to parse msg");
552 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
553 pa
->type
!= ISAKMP_NPTYPE_NONE
;
557 case ISAKMP_NPTYPE_KE
:
558 if (isakmp_p2ph(&iph1
->dhpub_p
, pa
->ptr
) < 0) {
560 "failed to process KE payload");
564 case ISAKMP_NPTYPE_NONCE
:
565 if (isakmp_p2ph(&iph1
->nonce_p
, pa
->ptr
) < 0) {
567 "failed to process NONCE payload");
571 case ISAKMP_NPTYPE_VID
:
572 vid_numeric
= check_vendorid(pa
->ptr
);
574 switch (vid_numeric
) {
576 iph1
->mode_cfg
->flags
|=
577 ISAKMP_CFG_VENDORID_XAUTH
;
581 iph1
->mode_cfg
->flags
|=
582 ISAKMP_CFG_VENDORID_UNITY
;
590 if (vid_numeric
== VENDORID_DPD
&& iph1
->rmconf
->dpd
)
595 case ISAKMP_NPTYPE_CR
:
596 if (oakley_savecr(iph1
, pa
->ptr
) < 0) {
598 "failed to process CR payload");
604 case ISAKMP_NPTYPE_NATD_DRAFT
:
605 case ISAKMP_NPTYPE_NATD_RFC
:
606 case ISAKMP_NPTYPE_NATD_BADDRAFT
:
607 if (NATT_AVAILABLE(iph1
) && iph1
->natt_options
!= NULL
&&
608 pa
->type
== iph1
->natt_options
->payload_nat_d
) {
609 natd_received
= NULL
;
610 if (isakmp_p2ph (&natd_received
, pa
->ptr
) < 0) {
612 "failed to process NATD payload");
616 /* set both bits first so that we can clear them
617 upon verifying hashes */
619 iph1
->natt_flags
|= NAT_DETECTED
;
621 /* this function will clear appropriate bits bits
622 from iph1->natt_flags */
623 natd_verified
= natt_compare_addr_hash (iph1
,
624 natd_received
, natd_seq
++);
626 plog (ASL_LEVEL_INFO
, "NAT-D payload #%d %s\n",
628 natd_verified
? "verified" : "doesn't match");
630 vfree (natd_received
);
633 /* %%%% Be lenient here - some servers send natd payloads */
634 /* when no nat is detected */
639 /* don't send information, see ident_r1recv() */
641 "ignore the packet, "
642 "received unexpecting payload type %d.\n",
649 if (NATT_AVAILABLE(iph1
)) {
650 plog (ASL_LEVEL_INFO
, "NAT %s %s%s\n",
651 iph1
->natt_flags
& NAT_DETECTED
?
652 "detected:" : "not detected",
653 iph1
->natt_flags
& NAT_DETECTED_ME
? "ME " : "",
654 iph1
->natt_flags
& NAT_DETECTED_PEER
? "PEER" : "");
655 if (iph1
->natt_flags
& NAT_DETECTED
)
656 natt_float_ports (iph1
);
660 /* payload existency check */
661 if (iph1
->dhpub_p
== NULL
|| iph1
->nonce_p
== NULL
) {
663 "few isakmp message received.\n");
667 if (oakley_checkcr(iph1
) < 0) {
668 /* Ignore this error in order to be interoperability. */
672 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_I_MSG4RCVD
);
676 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
677 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
678 CONSTSTR("Initiator, Main-Mode message 4"),
683 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
684 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
685 CONSTSTR("Initiator, Main-Mode Message 4"),
686 CONSTSTR("Failed to process Main-Mode Message 4"));
691 VPTRINIT(iph1
->dhpub_p
);
692 VPTRINIT(iph1
->nonce_p
);
693 VPTRINIT(iph1
->id_p
);
694 oakley_delcert(iph1
->cr_p
);
703 * psk: HDR*, IDi1, HASH_I
704 * sig: HDR*, IDi1, [ CR, ] [ CERT, ] SIG_I
705 * gssapi: HDR*, IDi1, < Gssi(n) | HASH_I >
710 ident_i5send(iph1
, msg0
)
711 phase1_handle_t
*iph1
;
718 if (iph1
->status
!= IKEV1_STATE_IDENT_I_MSG4RCVD
) {
720 "status mismatched %d.\n", iph1
->status
);
724 /* compute sharing secret of DH */
726 if (oakley_dh_compute(iph1
->approval
->dhgrp
, iph1
->dhpub
,
727 iph1
->dhpriv
, iph1
->dhpub_p
, &iph1
->dhgxy
) < 0) {
729 if (oakley_dh_compute(iph1
->approval
->dhgrp
, iph1
->dhpub_p
, iph1
->publicKeySize
, &iph1
->dhgxy
, &iph1
->dhC
) < 0) {
732 "failed to compute DH");
736 /* generate SKEYIDs & IV & final cipher key */
737 if (oakley_skeyid(iph1
) < 0) {
739 "failed to generate SKEYID");
742 if (oakley_skeyid_dae(iph1
) < 0) {
744 "failed to generate SKEYID-DAE");
747 if (oakley_compute_enckey(iph1
) < 0) {
749 "failed to generate ENCKEY");
752 if (oakley_newiv(iph1
) < 0) {
754 "failed to generate IV");
758 /* make ID payload into isakmp status */
759 if (ipsecdoi_setid1(iph1
) < 0) {
765 /* generate HASH to send */
767 iph1
->hash
= oakley_ph1hash_common(iph1
, GENERATE
);
768 if (iph1
->hash
== NULL
) {
770 "failed to generate HASH");
776 /* set encryption flag */
777 iph1
->flags
|= ISAKMP_FLAG_E
;
779 /* create HDR;ID;HASH payload */
780 iph1
->sendbuf
= ident_ir3mx(iph1
);
781 if (iph1
->sendbuf
== NULL
) {
783 "failed to allocate send buffer");
787 /* send the packet, add to the schedule to resend */
788 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
789 if (isakmp_ph1resend(iph1
) == -1) {
791 "failed to send packet");
795 /* the sending message is added to the received-list. */
796 if (ike_session_add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg0
,
797 PH1_NON_ESP_EXTRA_LEN(iph1
, iph1
->sendbuf
), PH1_FRAG_FLAGS(iph1
)) == -1) {
799 "failed to add a response packet to the tree.\n");
803 /* see handler.h about IV synchronization. */
804 memcpy(iph1
->ivm
->ive
->v
, iph1
->ivm
->iv
->v
, iph1
->ivm
->iv
->l
);
806 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_I_MSG5SENT
);
810 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
811 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
812 CONSTSTR("Initiator, Main-Mode message 5"),
817 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
818 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
819 CONSTSTR("Initiator, Main-Mode Message 5"),
820 CONSTSTR("Failed to transmit Main-Mode Message 5"));
826 * receive from responder
827 * psk: HDR*, IDr1, HASH_R
828 * sig: HDR*, IDr1, [ CERT, ] SIG_R
829 * gssapi: HDR*, IDr1, < GSSr(n) | HASH_R >
834 ident_i6recv(iph1
, msg0
)
835 phase1_handle_t
*iph1
;
838 vchar_t
*pbuf
= NULL
;
839 struct isakmp_parse_t
*pa
;
844 int received_cert
= 0;
847 if (iph1
->status
!= IKEV1_STATE_IDENT_I_MSG5SENT
) {
849 "status mismatched %d.\n", iph1
->status
);
854 if (!ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
856 "ignore the packet, "
857 "expecting the packet encrypted.\n");
860 msg
= oakley_do_decrypt(iph1
, msg0
, iph1
->ivm
->iv
, iph1
->ivm
->ive
);
863 "failed to decrypt");
867 /* validate the type of next payload */
868 pbuf
= isakmp_parse(msg
);
871 "failed to parse msg");
875 iph1
->pl_hash
= NULL
;
877 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
878 pa
->type
!= ISAKMP_NPTYPE_NONE
;
882 case ISAKMP_NPTYPE_ID
:
883 if (isakmp_p2ph(&iph1
->id_p
, pa
->ptr
) < 0) {
885 "failed to process ID payload");
889 case ISAKMP_NPTYPE_HASH
:
890 iph1
->pl_hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
892 case ISAKMP_NPTYPE_CERT
:
893 if (oakley_savecert(iph1
, pa
->ptr
) < 0) {
895 "failed to process CERT payload");
900 case ISAKMP_NPTYPE_SIG
:
901 if (isakmp_p2ph(&iph1
->sig_p
, pa
->ptr
) < 0) {
903 "failed to process SIG payload");
908 case ISAKMP_NPTYPE_VID
:
909 vid_numeric
= check_vendorid(pa
->ptr
);
911 if (vid_numeric
== VENDORID_DPD
&& iph1
->rmconf
->dpd
)
915 case ISAKMP_NPTYPE_N
:
916 isakmp_check_notify(pa
->ptr
, iph1
);
919 /* don't send information, see ident_r1recv() */
921 "ignore the packet, "
922 "received unexpecting payload type %d.\n",
929 oakley_verify_certid(iph1
);
932 /* payload existency check */
934 /* verify identifier */
935 if (ipsecdoi_checkid1(iph1
) != 0) {
937 "invalid ID payload.\n");
941 /* validate authentication value */
942 type
= oakley_validate_auth(iph1
);
944 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
945 IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_FAIL
,
946 CONSTSTR("Initiator, Main-Mode Message 6"),
947 CONSTSTR("Failed to authenticate Main-Mode Message 6"));
949 /* msg printed inner oakley_validate_auth() */
952 isakmp_info_send_n1(iph1
, type
, NULL
);
955 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
956 IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_SUCC
,
957 CONSTSTR("Initiator, Main-Mode Message 6"),
962 * XXX: Should we do compare two addresses, ph1handle's and ID
966 plogdump(ASL_LEVEL_DEBUG
, iph1
->id_p
->v
, iph1
->id_p
->l
, "peer's ID:");
968 /* see handler.h about IV synchronization. */
969 memcpy(iph1
->ivm
->iv
->v
, iph1
->ivm
->ive
->v
, iph1
->ivm
->ive
->l
);
972 * If we got a GSS token, we need to this roundtrip again.
974 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_I_MSG6RCVD
);
978 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
979 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
980 CONSTSTR("Initiator, Main-Mode message 6"),
985 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
986 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
987 CONSTSTR("Initiator, Main-Mode Message 6"),
988 CONSTSTR("Failed to transmit Main-Mode Message 6"));
996 VPTRINIT(iph1
->id_p
);
997 oakley_delcert(iph1
->cert_p
);
999 oakley_delcert(iph1
->crl_p
);
1001 VPTRINIT(iph1
->sig_p
);
1008 * status update and establish isakmp sa.
1011 ident_ifinalize(iph1
, msg
)
1012 phase1_handle_t
*iph1
;
1017 /* validity check */
1018 if (iph1
->status
!= IKEV1_STATE_IDENT_I_MSG6RCVD
) {
1020 "status mismatched %d.\n", iph1
->status
);
1024 /* see handler.h about IV synchronization. */
1025 memcpy(iph1
->ivm
->iv
->v
, iph1
->ivm
->ive
->v
, iph1
->ivm
->iv
->l
);
1027 fsm_set_state(&iph1
->status
, IKEV1_STATE_PHASE1_ESTABLISHED
);
1029 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1030 IPSECSESSIONEVENTCODE_IKEV1_PH1_INIT_SUCC
,
1031 CONSTSTR("Initiator, Main-Mode"),
1041 * receive from initiator
1048 ident_r1recv(iph1
, msg
)
1049 phase1_handle_t
*iph1
;
1052 vchar_t
*pbuf
= NULL
;
1053 struct isakmp_parse_t
*pa
;
1057 /* validity check */
1058 if (iph1
->status
!= IKEV1_STATE_IDENT_R_START
) {
1060 "status mismatched %d.\n", iph1
->status
);
1064 /* validate the type of next payload */
1066 * NOTE: XXX even if multiple VID, we'll silently ignore those.
1068 pbuf
= isakmp_parse(msg
);
1071 "failed to parse msg");
1074 pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
1076 /* check the position of SA payload */
1077 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
1079 "received invalid next payload type %d, "
1081 pa
->type
, ISAKMP_NPTYPE_SA
);
1084 if (isakmp_p2ph(&iph1
->sa
, pa
->ptr
) < 0) {
1086 "failed to process SA payload");
1092 pa
->type
!= ISAKMP_NPTYPE_NONE
;
1096 case ISAKMP_NPTYPE_VID
:
1097 vid_numeric
= check_vendorid(pa
->ptr
);
1099 if (iph1
->rmconf
->nat_traversal
&& natt_vendorid(vid_numeric
))
1100 natt_handle_vendorid(iph1
, vid_numeric
);
1102 #ifdef ENABLE_HYBRID
1103 switch (vid_numeric
) {
1104 case VENDORID_XAUTH
:
1105 iph1
->mode_cfg
->flags
|=
1106 ISAKMP_CFG_VENDORID_XAUTH
;
1109 case VENDORID_UNITY
:
1110 iph1
->mode_cfg
->flags
|=
1111 ISAKMP_CFG_VENDORID_UNITY
;
1119 if (vid_numeric
== VENDORID_DPD
&& iph1
->rmconf
->dpd
)
1120 iph1
->dpd_support
=1;
1123 if ((vid_numeric
== VENDORID_FRAG
) &&
1124 (vendorid_frag_cap(pa
->ptr
) & VENDORID_FRAG_IDENT
)) {
1125 plog(ASL_LEVEL_DEBUG
,
1126 "remote supports FRAGMENTATION\n");
1133 * We don't send information to the peer even
1134 * if we received malformed packet. Because we
1135 * can't distinguish the malformed packet and
1136 * the re-sent packet. And we do same behavior
1137 * when we expect encrypted packet.
1140 "ignore the packet, "
1141 "received unexpecting payload type %d.\n",
1148 if (NATT_AVAILABLE(iph1
)) {
1149 plog(ASL_LEVEL_INFO
,
1150 "Selected NAT-T version: %s\n",
1151 vid_string_by_id(iph1
->natt_options
->version
));
1152 ike_session_update_natt_version(iph1
);
1156 /* check SA payload and set approval SA for use */
1157 if (ipsecdoi_checkph1proposal(iph1
->sa
, iph1
) < 0) {
1159 "failed to get valid proposal.\n");
1160 /* XXX send information */
1164 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_R_MSG1RCVD
);
1168 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1169 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
1170 CONSTSTR("Responder, Main-Mode message 1"),
1175 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1176 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
1177 CONSTSTR("Responder, Main-Mode Message 1"),
1178 CONSTSTR("Failed to process Main-Mode Message 1"));
1197 ident_r2send(iph1
, msg
)
1198 phase1_handle_t
*iph1
;
1201 struct payload_list
*plist
= NULL
;
1203 vchar_t
*gss_sa
= NULL
;
1205 vchar_t
*vid_natt
= NULL
;
1207 #ifdef ENABLE_HYBRID
1208 vchar_t
*vid_xauth
= NULL
;
1209 vchar_t
*vid_unity
= NULL
;
1212 vchar_t
*vid_dpd
= NULL
;
1215 vchar_t
*vid_frag
= NULL
;
1218 /* validity check */
1219 if (iph1
->status
!= IKEV1_STATE_IDENT_R_MSG1RCVD
) {
1221 "status mismatched %d.\n", iph1
->status
);
1225 /* set responder's cookie */
1226 isakmp_newcookie((caddr_t
)&iph1
->index
.r_ck
, iph1
->remote
, iph1
->local
);
1227 gss_sa
= iph1
->sa_ret
;
1229 /* set SA payload to reply */
1230 plist
= isakmp_plist_append(plist
, gss_sa
, ISAKMP_NPTYPE_SA
);
1232 #ifdef ENABLE_HYBRID
1233 if (iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) {
1234 plog (ASL_LEVEL_INFO
, "Adding xauth VID payload.\n");
1235 if ((vid_xauth
= set_vendorid(VENDORID_XAUTH
)) == NULL
) {
1237 "Cannot create Xauth vendor ID\n");
1240 plist
= isakmp_plist_append(plist
,
1241 vid_xauth
, ISAKMP_NPTYPE_VID
);
1244 if (iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_UNITY
) {
1245 if ((vid_unity
= set_vendorid(VENDORID_UNITY
)) == NULL
) {
1247 "Cannot create Unity vendor ID\n");
1250 plist
= isakmp_plist_append(plist
,
1251 vid_unity
, ISAKMP_NPTYPE_VID
);
1255 /* Has the peer announced NAT-T? */
1256 if (NATT_AVAILABLE(iph1
))
1257 vid_natt
= set_vendorid(iph1
->natt_options
->version
);
1260 plist
= isakmp_plist_append(plist
, vid_natt
, ISAKMP_NPTYPE_VID
);
1263 /* XXX only send DPD VID if remote sent it ? */
1264 if(iph1
->rmconf
->dpd
){
1265 vid_dpd
= set_vendorid(VENDORID_DPD
);
1266 if (vid_dpd
!= NULL
)
1267 plist
= isakmp_plist_append(plist
, vid_dpd
, ISAKMP_NPTYPE_VID
);
1272 vid_frag
= set_vendorid(VENDORID_FRAG
);
1273 if (vid_frag
!= NULL
)
1274 vid_frag
= isakmp_frag_addcap(vid_frag
,
1275 VENDORID_FRAG_IDENT
);
1276 if (vid_frag
== NULL
)
1278 "Frag vendorID construction failed\n");
1280 plist
= isakmp_plist_append(plist
,
1281 vid_frag
, ISAKMP_NPTYPE_VID
);
1285 iph1
->sendbuf
= isakmp_plist_set_all (&plist
, iph1
);
1287 #ifdef HAVE_PRINT_ISAKMP_C
1288 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
1291 /* send the packet, add to the schedule to resend */
1292 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
1293 if (isakmp_ph1resend(iph1
) == -1) {
1295 "failed to send packet");
1299 /* the sending message is added to the received-list. */
1300 if (ike_session_add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
,
1301 PH1_NON_ESP_EXTRA_LEN(iph1
, iph1
->sendbuf
), PH1_FRAG_FLAGS(iph1
)) == -1) {
1302 plog(ASL_LEVEL_ERR
,
1303 "failed to add a response packet to the tree.\n");
1307 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_R_MSG2SENT
);
1309 #ifdef ENABLE_VPNCONTROL_PORT
1310 vpncontrol_notify_phase_change(1, FROM_LOCAL
, iph1
, NULL
);
1315 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1316 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1317 CONSTSTR("Responder, Main-Mode message 2"),
1322 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1323 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1324 CONSTSTR("Responder, Main-Mode Message 2"),
1325 CONSTSTR("Failed to transmit Main-Mode Message 2"));
1331 #ifdef ENABLE_HYBRID
1332 if (vid_xauth
!= NULL
)
1334 if (vid_unity
!= NULL
)
1338 if (vid_dpd
!= NULL
)
1342 if (vid_frag
!= NULL
)
1350 * receive from initiator
1353 * gssapi: HDR, KE, Ni, GSSi
1354 * rsa: HDR, KE, [ HASH(1), ] <IDi1_b>PubKey_r, <Ni_b>PubKey_r
1355 * rev: HDR, [ HASH(1), ] <Ni_b>Pubkey_r, <KE_b>Ke_i,
1356 * <IDi1_b>Ke_i, [<<Cert-I_b>Ke_i]
1359 ident_r3recv(iph1
, msg
)
1360 phase1_handle_t
*iph1
;
1363 vchar_t
*pbuf
= NULL
;
1364 struct isakmp_parse_t
*pa
;
1370 /* validity check */
1371 if (iph1
->status
!= IKEV1_STATE_IDENT_R_MSG2SENT
) {
1373 "status mismatched %d.\n", iph1
->status
);
1377 /* validate the type of next payload */
1378 pbuf
= isakmp_parse(msg
);
1381 "failed to parse msg");
1385 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
1386 pa
->type
!= ISAKMP_NPTYPE_NONE
;
1389 case ISAKMP_NPTYPE_KE
:
1390 if (isakmp_p2ph(&iph1
->dhpub_p
, pa
->ptr
) < 0) {
1392 "failed to process KE payload");
1396 case ISAKMP_NPTYPE_NONCE
:
1397 if (isakmp_p2ph(&iph1
->nonce_p
, pa
->ptr
) < 0) {
1399 "failed to process NONCE payload");
1403 case ISAKMP_NPTYPE_VID
:
1404 (void)check_vendorid(pa
->ptr
);
1406 case ISAKMP_NPTYPE_CR
:
1407 plog(ASL_LEVEL_WARNING
,
1408 "CR received, ignore it. "
1409 "It should be in other exchange.\n");
1413 case ISAKMP_NPTYPE_NATD_DRAFT
:
1414 case ISAKMP_NPTYPE_NATD_RFC
:
1415 case ISAKMP_NPTYPE_NATD_BADDRAFT
:
1416 if (NATT_AVAILABLE(iph1
) && iph1
->natt_options
!= NULL
&&
1417 pa
->type
== iph1
->natt_options
->payload_nat_d
)
1419 vchar_t
*natd_received
= NULL
;
1422 if (isakmp_p2ph (&natd_received
, pa
->ptr
) < 0) {
1424 "failed to process NATD payload");
1429 iph1
->natt_flags
|= NAT_DETECTED
;
1431 natd_verified
= natt_compare_addr_hash (iph1
,
1432 natd_received
, natd_seq
++);
1434 plog (ASL_LEVEL_INFO
, "NAT-D payload #%d %s\n",
1436 natd_verified
? "verified" : "doesn't match");
1438 vfree (natd_received
);
1441 /* %%%% Be lenient here - some servers send natd payloads */
1442 /* when no nat is detected */
1447 /* don't send information, see ident_r1recv() */
1449 "ignore the packet, "
1450 "received unexpecting payload type %d.\n",
1457 if (NATT_AVAILABLE(iph1
))
1458 plog (ASL_LEVEL_INFO
, "NAT %s %s%s\n",
1459 iph1
->natt_flags
& NAT_DETECTED
?
1460 "detected:" : "not detected",
1461 iph1
->natt_flags
& NAT_DETECTED_ME
? "ME " : "",
1462 iph1
->natt_flags
& NAT_DETECTED_PEER
? "PEER" : "");
1465 /* payload existency check */
1466 if (iph1
->dhpub_p
== NULL
|| iph1
->nonce_p
== NULL
) {
1468 "few isakmp message received.\n");
1472 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_R_MSG3RCVD
);
1476 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1477 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
1478 CONSTSTR("Responder, Main-Mode message 3"),
1483 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1484 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
1485 CONSTSTR("Responder, Main-Mode Message 3"),
1486 CONSTSTR("Failed to process Main-Mode Message 3"));
1492 VPTRINIT(iph1
->dhpub_p
);
1493 VPTRINIT(iph1
->nonce_p
);
1494 VPTRINIT(iph1
->id_p
);
1503 * sig: HDR, KE, Nr [, CR ]
1504 * gssapi: HDR, KE, Nr, GSSr
1505 * rsa: HDR, KE, <IDr1_b>PubKey_i, <Nr_b>PubKey_i
1506 * rev: HDR, <Nr_b>PubKey_i, <KE_b>Ke_r, <IDr1_b>Ke_r,
1509 ident_r4send(iph1
, msg
)
1510 phase1_handle_t
*iph1
;
1515 /* validity check */
1516 if (iph1
->status
!= IKEV1_STATE_IDENT_R_MSG3RCVD
) {
1518 "status mismatched %d.\n", iph1
->status
);
1522 /* generate DH public value */
1524 if (oakley_dh_generate(iph1
->approval
->dhgrp
,
1525 &iph1
->dhpub
, &iph1
->dhpriv
) < 0) {
1527 if (oakley_dh_generate(iph1
->approval
->dhgrp
,
1528 &iph1
->dhpub
, &iph1
->publicKeySize
, &iph1
->dhC
) < 0) {
1531 "failed to generate DH");
1535 /* generate NONCE value */
1536 iph1
->nonce
= eay_set_random(iph1
->rmconf
->nonce_size
);
1537 if (iph1
->nonce
== NULL
) {
1539 "failed to generate NONCE");
1543 /* create HDR;KE;NONCE payload */
1544 iph1
->sendbuf
= ident_ir2mx(iph1
);
1545 if (iph1
->sendbuf
== NULL
) {
1547 "failed to allocate send buffer");
1551 #ifdef HAVE_PRINT_ISAKMP_C
1552 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
1555 /* send the packet, add to the schedule to resend */
1556 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
1557 if (isakmp_ph1resend(iph1
) == -1) {
1559 "failed to send packet");
1563 /* the sending message is added to the received-list. */
1564 if (ike_session_add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
,
1565 PH1_NON_ESP_EXTRA_LEN(iph1
, iph1
->sendbuf
), PH1_FRAG_FLAGS(iph1
)) == -1) {
1566 plog(ASL_LEVEL_ERR
,
1567 "failed to add a response packet to the tree.\n");
1571 /* compute sharing secret of DH */
1573 if (oakley_dh_compute(iph1
->approval
->dhgrp
, iph1
->dhpub
,
1574 iph1
->dhpriv
, iph1
->dhpub_p
, &iph1
->dhgxy
) < 0) {
1576 if (oakley_dh_compute(iph1
->approval
->dhgrp
, iph1
->dhpub_p
, iph1
->publicKeySize
, &iph1
->dhgxy
, &iph1
->dhC
) < 0) {
1579 "failed to compute DH");
1583 /* generate SKEYIDs & IV & final cipher key */
1584 if (oakley_skeyid(iph1
) < 0) {
1586 "failed to generate SKEYID");
1589 if (oakley_skeyid_dae(iph1
) < 0) {
1591 "failed to generate SKEYID-DAE");
1594 if (oakley_compute_enckey(iph1
) < 0) {
1596 "failed to generate ENCKEY");
1599 if (oakley_newiv(iph1
) < 0) {
1601 "failed to generate IV");
1605 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_R_MSG4SENT
);
1609 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1610 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1611 CONSTSTR("Responder, Main-Mode message 4"),
1616 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1617 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1618 CONSTSTR("Responder, Main-Mode Message 4"),
1619 CONSTSTR("Failed to transmit Main-Mode Message 4"));
1625 * receive from initiator
1626 * psk: HDR*, IDi1, HASH_I
1627 * sig: HDR*, IDi1, [ CR, ] [ CERT, ] SIG_I
1628 * gssapi: HDR*, [ IDi1, ] < GSSi(n) | HASH_I >
1633 ident_r5recv(iph1
, msg0
)
1634 phase1_handle_t
*iph1
;
1637 vchar_t
*msg
= NULL
;
1638 vchar_t
*pbuf
= NULL
;
1639 struct isakmp_parse_t
*pa
;
1642 int received_cert
= 0;
1644 /* validity check */
1645 if (iph1
->status
!= IKEV1_STATE_IDENT_R_MSG4SENT
) {
1647 "status mismatched %d.\n", iph1
->status
);
1652 if (!ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
1654 "reject the packet, "
1655 "expecting the packet encrypted.\n");
1658 msg
= oakley_do_decrypt(iph1
, msg0
, iph1
->ivm
->iv
, iph1
->ivm
->ive
);
1661 "failed to decrypt");
1665 /* validate the type of next payload */
1666 pbuf
= isakmp_parse(msg
);
1669 "failed to parse msg");
1673 iph1
->pl_hash
= NULL
;
1675 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
1676 pa
->type
!= ISAKMP_NPTYPE_NONE
;
1680 case ISAKMP_NPTYPE_ID
:
1681 if (isakmp_p2ph(&iph1
->id_p
, pa
->ptr
) < 0) {
1683 "failed to process ID payload");
1687 case ISAKMP_NPTYPE_HASH
:
1688 iph1
->pl_hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
1690 case ISAKMP_NPTYPE_CR
:
1691 if (oakley_savecr(iph1
, pa
->ptr
) < 0) {
1693 "failed to process CR payload");
1697 case ISAKMP_NPTYPE_CERT
:
1698 if (oakley_savecert(iph1
, pa
->ptr
) < 0) {
1700 "failed to process CERT payload");
1705 case ISAKMP_NPTYPE_SIG
:
1706 if (isakmp_p2ph(&iph1
->sig_p
, pa
->ptr
) < 0) {
1708 "failed to process SIG payload");
1712 case ISAKMP_NPTYPE_VID
:
1713 (void)check_vendorid(pa
->ptr
);
1715 case ISAKMP_NPTYPE_N
:
1716 isakmp_check_notify(pa
->ptr
, iph1
);
1719 /* don't send information, see ident_r1recv() */
1721 "ignore the packet, "
1722 "received unexpecting payload type %d.\n",
1728 if (received_cert
) {
1729 oakley_verify_certid(iph1
);
1732 /* payload existency check */
1733 /* XXX same as ident_i4recv(), should be merged. */
1737 switch (AUTHMETHOD(iph1
)) {
1738 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
1739 #ifdef ENABLE_HYBRID
1740 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1741 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1743 if (iph1
->id_p
== NULL
|| iph1
->pl_hash
== NULL
)
1746 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
1747 #ifdef ENABLE_HYBRID
1748 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1750 if (iph1
->id_p
== NULL
|| iph1
->sig_p
== NULL
)
1753 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
1754 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
1755 #ifdef ENABLE_HYBRID
1756 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1757 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1759 if (iph1
->pl_hash
== NULL
)
1764 "invalid authmethod %d why ?\n",
1765 iph1
->approval
->authmethod
);
1770 "few isakmp message received.\n");
1775 /* verify identifier */
1776 if (ipsecdoi_checkid1(iph1
) != 0) {
1778 "invalid ID payload.\n");
1782 /* validate authentication value */
1784 type
= oakley_validate_auth(iph1
);
1786 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1787 IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_FAIL
,
1788 CONSTSTR("Responder, Main-Mode Message 5"),
1789 CONSTSTR("Failed to authenticate Main-Mode Message 5"));
1791 /* msg printed inner oakley_validate_auth() */
1794 isakmp_info_send_n1(iph1
, type
, NULL
);
1797 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1798 IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_SUCC
,
1799 CONSTSTR("Responder, Main-Mode Message 5"),
1802 if (oakley_checkcr(iph1
) < 0) {
1803 /* Ignore this error in order to be interoperability. */
1808 * XXX: Should we do compare two addresses, ph1handle's and ID
1812 plogdump(ASL_LEVEL_DEBUG
, iph1
->id_p
->v
, iph1
->id_p
->l
, "peer's ID\n");
1814 /* see handler.h about IV synchronization. */
1815 memcpy(iph1
->ivm
->iv
->v
, iph1
->ivm
->ive
->v
, iph1
->ivm
->ive
->l
);
1817 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_R_MSG5RCVD
);
1820 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1821 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
1822 CONSTSTR("Responder, Main-Mode message 5"),
1827 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1828 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
1829 CONSTSTR("Responder, Main-Mode Message 5"),
1830 CONSTSTR("Failed to process Main-Mode Message 5"));
1838 VPTRINIT(iph1
->id_p
);
1839 oakley_delcert(iph1
->cert_p
);
1840 iph1
->cert_p
= NULL
;
1841 oakley_delcert(iph1
->crl_p
);
1843 VPTRINIT(iph1
->sig_p
);
1844 oakley_delcert(iph1
->cr_p
);
1853 * psk: HDR*, IDr1, HASH_R
1854 * sig: HDR*, IDr1, [ CERT, ] SIG_R
1855 * gssapi: HDR*, IDr1, < GSSr(n) | HASH_R >
1860 ident_r6send(iph1
, msg
)
1861 phase1_handle_t
*iph1
;
1867 /* validity check */
1868 if (iph1
->status
!= IKEV1_STATE_IDENT_R_MSG5RCVD
) {
1870 "status mismatched %d.\n", iph1
->status
);
1874 /* make ID payload into isakmp status */
1875 if (ipsecdoi_setid1(iph1
) < 0) {
1877 "failed to set ID");
1882 /* generate HASH to send */
1883 plog(ASL_LEVEL_DEBUG
, "generate HASH_R\n");
1884 iph1
->hash
= oakley_ph1hash_common(iph1
, GENERATE
);
1885 if (iph1
->hash
== NULL
) {
1887 "failed to generate HASH");
1893 /* set encryption flag */
1894 iph1
->flags
|= ISAKMP_FLAG_E
;
1896 /* create HDR;ID;HASH payload */
1897 iph1
->sendbuf
= ident_ir3mx(iph1
);
1898 if (iph1
->sendbuf
== NULL
) {
1900 "failed to create send buffer");
1904 /* send HDR;ID;HASH to responder */
1905 if (isakmp_send(iph1
, iph1
->sendbuf
) < 0) {
1907 "failed to send packet");
1911 /* the sending message is added to the received-list. */
1912 if (ike_session_add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
,
1913 PH1_NON_ESP_EXTRA_LEN(iph1
, iph1
->sendbuf
), PH1_FRAG_FLAGS(iph1
)) == -1) {
1914 plog(ASL_LEVEL_ERR
,
1915 "failed to add a response packet to the tree.\n");
1919 /* see handler.h about IV synchronization. */
1920 memcpy(iph1
->ivm
->ive
->v
, iph1
->ivm
->iv
->v
, iph1
->ivm
->iv
->l
);
1922 fsm_set_state(&iph1
->status
, IKEV1_STATE_PHASE1_ESTABLISHED
);
1924 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1925 IPSECSESSIONEVENTCODE_IKEV1_PH1_RESP_SUCC
,
1926 CONSTSTR("Responder, Main-Mode"),
1931 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1932 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1933 CONSTSTR("Responder, Main-Mode message 6"),
1938 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1939 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1940 CONSTSTR("Responder, Main-Mode Message 6"),
1941 CONSTSTR("Failed to process Main-Mode Message 6"));
1948 * This is used in main mode for:
1949 * initiator's 3rd exchange send to responder
1952 * rsa: HDR, KE, [ HASH(1), ] <IDi1_b>PubKey_r, <Ni_b>PubKey_r
1953 * rev: HDR, [ HASH(1), ] <Ni_b>Pubkey_r, <KE_b>Ke_i,
1954 * <IDi1_b>Ke_i, [<<Cert-I_b>Ke_i]
1955 * responders 2nd exchnage send to initiator
1957 * sig: HDR, KE, Nr [, CR ]
1958 * rsa: HDR, KE, <IDr1_b>PubKey_i, <Nr_b>PubKey_i
1959 * rev: HDR, <Nr_b>PubKey_i, <KE_b>Ke_r, <IDr1_b>Ke_r,
1963 phase1_handle_t
*iph1
;
1966 struct payload_list
*plist
= NULL
;
1969 vchar_t
*vid
= NULL
;
1972 vchar_t
*natd
[2] = { NULL
, NULL
};
1975 /* create CR if need */
1976 if (iph1
->side
== RESPONDER
1977 && iph1
->rmconf
->send_cr
1978 && oakley_needcr(iph1
->approval
->authmethod
)) {
1980 cr
= oakley_getcr(iph1
);
1983 "failed to get cr buffer.\n");
1988 /* create isakmp KE payload */
1989 plist
= isakmp_plist_append(plist
, iph1
->dhpub
, ISAKMP_NPTYPE_KE
);
1991 /* create isakmp NONCE payload */
1992 plist
= isakmp_plist_append(plist
, iph1
->nonce
, ISAKMP_NPTYPE_NONCE
);
1994 /* append vendor id, if needed */
1996 plist
= isakmp_plist_append(plist
, vid
, ISAKMP_NPTYPE_VID
);
1998 /* create isakmp CR payload if needed */
2000 plist
= isakmp_plist_append(plist
, cr
, ISAKMP_NPTYPE_CR
);
2003 /* generate and append NAT-D payloads */
2004 if (NATT_AVAILABLE(iph1
))
2006 if ((natd
[0] = natt_hash_addr (iph1
, iph1
->remote
)) == NULL
) {
2008 "NAT-D hashing failed for %s\n", saddr2str((struct sockaddr
*)iph1
->remote
));
2012 if ((natd
[1] = natt_hash_addr (iph1
, iph1
->local
)) == NULL
) {
2014 "NAT-D hashing failed for %s\n", saddr2str((struct sockaddr
*)iph1
->local
));
2018 plog (ASL_LEVEL_INFO
, "Adding remote and local NAT-D payloads.\n");
2019 /* old Apple version sends natd payloads in the wrong order */
2020 if (iph1
->natt_options
->version
== VENDORID_NATT_APPLE
) {
2021 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
2022 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
2025 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
2026 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
2031 buf
= isakmp_plist_set_all (&plist
, iph1
);
2036 if (error
&& buf
!= NULL
) {
2056 * This is used in main mode for:
2057 * initiator's 4th exchange send to responder
2058 * psk: HDR*, IDi1, HASH_I
2059 * sig: HDR*, IDi1, [ CR, ] [ CERT, ] SIG_I
2060 * gssapi: HDR*, [ IDi1, ] < GSSi(n) | HASH_I >
2063 * responders 3rd exchnage send to initiator
2064 * psk: HDR*, IDr1, HASH_R
2065 * sig: HDR*, IDr1, [ CERT, ] SIG_R
2066 * gssapi: HDR*, [ IDr1, ] < GSSr(n) | HASH_R >
2072 phase1_handle_t
*iph1
;
2074 struct payload_list
*plist
= NULL
;
2075 vchar_t
*buf
= NULL
, *new = NULL
;
2080 vchar_t
*notp_ini
= NULL
;
2082 switch (AUTHMETHOD(iph1
)) {
2083 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
2084 #ifdef ENABLE_HYBRID
2085 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
2086 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
2087 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
2089 /* create isakmp ID payload */
2090 plist
= isakmp_plist_append(plist
, iph1
->id
, ISAKMP_NPTYPE_ID
);
2092 /* create isakmp HASH payload */
2093 plist
= isakmp_plist_append(plist
, iph1
->hash
, ISAKMP_NPTYPE_HASH
);
2095 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
2096 #ifdef ENABLE_HYBRID
2097 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
2098 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
2099 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
2101 if (oakley_getmycert(iph1
) < 0) {
2103 "failed to get mycert");
2107 if (oakley_getsign(iph1
) < 0) {
2109 "failed to get sign");
2113 /* create CR if need */
2114 if (iph1
->side
== INITIATOR
2115 && iph1
->rmconf
->send_cr
2116 && oakley_needcr(iph1
->approval
->authmethod
)) {
2118 cr
= oakley_getcr(iph1
);
2121 "failed to get CR");
2126 if (iph1
->cert
!= NULL
&& iph1
->rmconf
->send_cert
)
2129 /* add ID payload */
2130 plist
= isakmp_plist_append(plist
, iph1
->id
, ISAKMP_NPTYPE_ID
);
2132 /* add CERT payload if there */
2133 // we don't support sending of certchains
2135 plist
= isakmp_plist_append(plist
, iph1
->cert
->pl
, ISAKMP_NPTYPE_CERT
);
2136 /* add SIG payload */
2137 plist
= isakmp_plist_append(plist
, iph1
->sig
, ISAKMP_NPTYPE_SIG
);
2139 /* create isakmp CR payload */
2141 plist
= isakmp_plist_append(plist
, cr
, ISAKMP_NPTYPE_CR
);
2144 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
2145 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
2146 #ifdef ENABLE_HYBRID
2147 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
:
2148 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
2149 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
:
2150 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
2153 "not supported authentication type %d\n",
2154 iph1
->approval
->authmethod
);
2158 "invalid authentication type %d\n",
2159 iph1
->approval
->authmethod
);
2163 if (iph1
->side
== INITIATOR
) {
2164 notp_ini
= isakmp_plist_append_initial_contact(iph1
, plist
);
2167 buf
= isakmp_plist_set_all (&plist
, iph1
);
2169 #ifdef HAVE_PRINT_ISAKMP_C
2170 isakmp_printpacket(buf
, iph1
->local
, iph1
->remote
, 1);
2174 new = oakley_do_encrypt(iph1
, buf
, iph1
->ivm
->ive
, iph1
->ivm
->iv
);
2177 "failed to encrypt");
2190 if (error
&& buf
!= NULL
) {