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"
82 #include "isakmp_xauth.h"
83 #include "isakmp_cfg.h"
86 #include "isakmp_frag.h"
89 #include "vpn_control.h"
90 #include "vpn_control_var.h"
91 #include "ipsecSessionTracer.h"
92 #include "ipsecMessageTracer.h"
94 #include <Security/SecDH.h>
97 static vchar_t
*ident_ir2mx (phase1_handle_t
*);
98 static vchar_t
*ident_ir3mx (phase1_handle_t
*);
101 * begin Identity Protection Mode as initiator.
111 ident_i1send(iph1
, msg
)
112 phase1_handle_t
*iph1
;
113 vchar_t
*msg
; /* must be null */
115 struct payload_list
*plist
= NULL
;
118 vchar_t
*vid_natt
[MAX_NATT_VID_COUNT
] = { NULL
};
122 vchar_t
*vid_xauth
= NULL
;
123 vchar_t
*vid_unity
= NULL
;
126 vchar_t
*vid_frag
= NULL
;
129 vchar_t
*vid_dpd
= NULL
;
133 if (iph1
->status
!= IKEV1_STATE_IDENT_I_START
) {
135 "status mismatched %d.\n", iph1
->status
);
142 "msg has to be NULL in this function.\n");
146 /* create isakmp index */
147 memset(&iph1
->index
, 0, sizeof(iph1
->index
));
148 isakmp_newcookie((caddr_t
)&iph1
->index
, iph1
->remote
, iph1
->local
);
150 /* create SA payload for my proposal */
151 iph1
->sa
= ipsecdoi_setph1proposal(iph1
);
152 if (iph1
->sa
== NULL
) {
154 "failed to set proposal");
158 /* set SA payload to propose */
159 plist
= isakmp_plist_append(plist
, iph1
->sa
, ISAKMP_NPTYPE_SA
);
162 /* set VID payload for NAT-T if NAT-T support allowed in the config file */
163 if (iph1
->rmconf
->nat_traversal
)
164 plist
= isakmp_plist_append_natt_vids(plist
, vid_natt
);
167 /* Do we need Xauth VID? */
168 switch (RMAUTHMETHOD(iph1
)) {
169 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
170 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
171 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
172 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
:
173 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
:
174 if ((vid_xauth
= set_vendorid(VENDORID_XAUTH
)) == NULL
)
176 "Xauth vendor ID generation failed\n");
178 plist
= isakmp_plist_append(plist
,
179 vid_xauth
, ISAKMP_NPTYPE_VID
);
181 if ((vid_unity
= set_vendorid(VENDORID_UNITY
)) == NULL
)
183 "Unity vendor ID generation failed\n");
185 plist
= isakmp_plist_append(plist
,
186 vid_unity
, ISAKMP_NPTYPE_VID
);
193 if (iph1
->rmconf
->ike_frag
) {
194 if ((vid_frag
= set_vendorid(VENDORID_FRAG
)) == NULL
) {
196 "Frag vendorID construction failed\n");
198 vid_frag
= isakmp_frag_addcap(vid_frag
,
199 VENDORID_FRAG_IDENT
);
200 plist
= isakmp_plist_append(plist
,
201 vid_frag
, ISAKMP_NPTYPE_VID
);
206 if(iph1
->rmconf
->dpd
){
207 vid_dpd
= set_vendorid(VENDORID_DPD
);
209 plist
= isakmp_plist_append(plist
, vid_dpd
,
214 iph1
->sendbuf
= isakmp_plist_set_all (&plist
, iph1
);
216 #ifdef HAVE_PRINT_ISAKMP_C
217 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
220 /* send the packet, add to the schedule to resend */
221 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
222 if (isakmp_ph1resend(iph1
) == -1) {
224 "failed to send packet");
228 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_I_MSG1SENT
);
232 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
233 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
234 CONSTSTR("Initiator, Main-Mode message 1"),
239 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
240 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
241 CONSTSTR("Initiator, Main-Mode Message 1"),
242 CONSTSTR("Failed to transmit Main-Mode Message 1"));
249 for (i
= 0; i
< MAX_NATT_VID_COUNT
&& vid_natt
[i
] != NULL
; i
++)
253 if (vid_xauth
!= NULL
)
255 if (vid_unity
!= NULL
)
267 * receive from responder
274 ident_i2recv(iph1
, msg
)
275 phase1_handle_t
*iph1
;
278 vchar_t
*pbuf
= NULL
;
279 struct isakmp_parse_t
*pa
;
280 vchar_t
*satmp
= NULL
;
285 if (iph1
->status
!= IKEV1_STATE_IDENT_I_MSG1SENT
) {
287 "status mismatched %d.\n", iph1
->status
);
291 /* validate the type of next payload */
293 * NOTE: RedCreek(as responder) attaches N[responder-lifetime] here,
294 * if proposal-lifetime > lifetime-redcreek-wants.
296 * => According to the seciton 4.6.3 in RFC 2407, This is illegal.
297 * NOTE: we do not really care about ordering of VID and N.
299 * NOTE: even if there's multiple VID/N, we'll ignore them.
301 pbuf
= isakmp_parse(msg
);
304 "failed to parse msg");
307 pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
309 /* SA payload is fixed postion */
310 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
312 "received invalid next payload type %d, "
314 pa
->type
, ISAKMP_NPTYPE_SA
);
317 if (isakmp_p2ph(&satmp
, pa
->ptr
) < 0) {
319 "failed to process SA payload");
325 pa
->type
!= ISAKMP_NPTYPE_NONE
;
329 case ISAKMP_NPTYPE_VID
:
330 vid_numeric
= check_vendorid(pa
->ptr
);
332 if (iph1
->rmconf
->nat_traversal
&& natt_vendorid(vid_numeric
))
333 natt_handle_vendorid(iph1
, vid_numeric
);
336 switch (vid_numeric
) {
338 iph1
->mode_cfg
->flags
|=
339 ISAKMP_CFG_VENDORID_XAUTH
;
343 iph1
->mode_cfg
->flags
|=
344 ISAKMP_CFG_VENDORID_UNITY
;
352 if (vid_numeric
== VENDORID_DPD
&& iph1
->rmconf
->dpd
)
356 if ((vid_numeric
== VENDORID_FRAG
) &&
357 (vendorid_frag_cap(pa
->ptr
) & VENDORID_FRAG_IDENT
)) {
358 plog(ASL_LEVEL_DEBUG
,
359 "remote supports FRAGMENTATION\n");
365 /* don't send information, see ident_r1recv() */
367 "ignore the packet, "
368 "received unexpecting payload type %d.\n",
375 if (NATT_AVAILABLE(iph1
)) {
376 plog(ASL_LEVEL_NOTICE
,
377 "Selected NAT-T version: %s\n",
378 vid_string_by_id(iph1
->natt_options
->version
));
379 ike_session_update_natt_version(iph1
);
383 /* check SA payload and set approval SA for use */
384 if (ipsecdoi_checkph1proposal(satmp
, iph1
) < 0) {
386 "failed to get valid proposal.\n");
387 /* XXX send information */
390 VPTRINIT(iph1
->sa_ret
);
392 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_I_MSG2RCVD
);
394 #ifdef ENABLE_VPNCONTROL_PORT
395 vpncontrol_notify_phase_change(1, FROM_REMOTE
, iph1
, NULL
);
400 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
401 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
402 CONSTSTR("Initiator, Main-Mode message 2"),
407 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
408 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
409 CONSTSTR("Initiator, Main-Mode Message 2"),
410 CONSTSTR("Failed to process Main-Mode Message 2"));
423 * gssapi: HDR, KE, Ni, GSSi
424 * rsa: HDR, KE, [ HASH(1), ] <IDi1_b>PubKey_r, <Ni_b>PubKey_r
425 * rev: HDR, [ HASH(1), ] <Ni_b>Pubkey_r, <KE_b>Ke_i,
426 * <IDi1_b>Ke_i, [<<Cert-I_b>Ke_i]
429 ident_i3send(iph1
, msg
)
430 phase1_handle_t
*iph1
;
436 if (iph1
->status
!= IKEV1_STATE_IDENT_I_MSG2RCVD
) {
438 "status mismatched %d.\n", iph1
->status
);
442 /* fix isakmp index */
443 memcpy(&iph1
->index
.r_ck
, &((struct isakmp
*)msg
->v
)->r_ck
,
446 /* generate DH public value */
448 if (oakley_dh_generate(iph1
->approval
->dhgrp
,
449 &iph1
->dhpub
, &iph1
->dhpriv
) < 0) {
451 if (oakley_dh_generate(iph1
->approval
->dhgrp
,
452 &iph1
->dhpub
, &iph1
->publicKeySize
, &iph1
->dhC
) < 0) {
455 "failed to generate DH");
459 /* generate NONCE value */
460 iph1
->nonce
= eay_set_random(iph1
->rmconf
->nonce_size
);
461 if (iph1
->nonce
== NULL
) {
463 "failed to generate NONCE");
467 /* create buffer to send isakmp payload */
468 iph1
->sendbuf
= ident_ir2mx(iph1
);
469 if (iph1
->sendbuf
== NULL
) {
471 "failed to create send buffer");
475 #ifdef HAVE_PRINT_ISAKMP_C
476 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
479 /* send the packet, add to the schedule to resend */
480 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
481 if (isakmp_ph1resend(iph1
) == -1) {
483 "failed to send packet");
487 /* the sending message is added to the received-list. */
488 if (ike_session_add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
,
489 PH1_NON_ESP_EXTRA_LEN(iph1
, iph1
->sendbuf
), PH1_FRAG_FLAGS(iph1
)) == -1) {
491 "failed to add a response packet to the tree.\n");
495 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_I_MSG3SENT
);
499 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
500 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
501 CONSTSTR("Initiator, Main-Mode message 3"),
506 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
507 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
508 CONSTSTR("Initiator, Main-Mode Message 3"),
509 CONSTSTR("Failed to transmit Main-Mode Message 3"));
515 * receive from responder
517 * sig: HDR, KE, Nr [, CR ]
518 * gssapi: HDR, KE, Nr, GSSr
519 * rsa: HDR, KE, <IDr1_b>PubKey_i, <Nr_b>PubKey_i
520 * rev: HDR, <Nr_b>PubKey_i, <KE_b>Ke_r, <IDr1_b>Ke_r,
523 ident_i4recv(iph1
, msg
)
524 phase1_handle_t
*iph1
;
527 vchar_t
*pbuf
= NULL
;
528 struct isakmp_parse_t
*pa
;
532 vchar_t
*natd_received
;
533 int natd_seq
= 0, natd_verified
;
537 if (iph1
->status
!= IKEV1_STATE_IDENT_I_MSG3SENT
) {
539 "status mismatched %d.\n", iph1
->status
);
543 /* validate the type of next payload */
544 pbuf
= isakmp_parse(msg
);
547 "failed to parse msg");
551 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
552 pa
->type
!= ISAKMP_NPTYPE_NONE
;
556 case ISAKMP_NPTYPE_KE
:
557 if (isakmp_p2ph(&iph1
->dhpub_p
, pa
->ptr
) < 0) {
559 "failed to process KE payload");
563 case ISAKMP_NPTYPE_NONCE
:
564 if (isakmp_p2ph(&iph1
->nonce_p
, pa
->ptr
) < 0) {
566 "failed to process NONCE payload");
570 case ISAKMP_NPTYPE_VID
:
571 vid_numeric
= check_vendorid(pa
->ptr
);
573 switch (vid_numeric
) {
575 iph1
->mode_cfg
->flags
|=
576 ISAKMP_CFG_VENDORID_XAUTH
;
580 iph1
->mode_cfg
->flags
|=
581 ISAKMP_CFG_VENDORID_UNITY
;
589 if (vid_numeric
== VENDORID_DPD
&& iph1
->rmconf
->dpd
)
594 case ISAKMP_NPTYPE_CR
:
595 if (oakley_savecr(iph1
, pa
->ptr
) < 0) {
597 "failed to process CR payload");
603 case ISAKMP_NPTYPE_NATD_DRAFT
:
604 case ISAKMP_NPTYPE_NATD_RFC
:
605 case ISAKMP_NPTYPE_NATD_BADDRAFT
:
606 if (NATT_AVAILABLE(iph1
) && iph1
->natt_options
!= NULL
&&
607 pa
->type
== iph1
->natt_options
->payload_nat_d
) {
608 natd_received
= NULL
;
609 if (isakmp_p2ph (&natd_received
, pa
->ptr
) < 0) {
611 "failed to process NATD payload");
615 /* set both bits first so that we can clear them
616 upon verifying hashes */
618 iph1
->natt_flags
|= NAT_DETECTED
;
620 /* this function will clear appropriate bits bits
621 from iph1->natt_flags */
622 natd_verified
= natt_compare_addr_hash (iph1
,
623 natd_received
, natd_seq
++);
625 plog (ASL_LEVEL_NOTICE
, "NAT-D payload #%d %s\n",
627 natd_verified
? "verified" : "doesn't match");
629 vfree (natd_received
);
632 /* %%%% Be lenient here - some servers send natd payloads */
633 /* when no nat is detected */
638 /* don't send information, see ident_r1recv() */
640 "ignore the packet, "
641 "received unexpecting payload type %d.\n",
648 if (NATT_AVAILABLE(iph1
)) {
649 plog (ASL_LEVEL_NOTICE
, "NAT %s %s%s\n",
650 iph1
->natt_flags
& NAT_DETECTED
?
651 "detected:" : "not detected",
652 iph1
->natt_flags
& NAT_DETECTED_ME
? "ME " : "",
653 iph1
->natt_flags
& NAT_DETECTED_PEER
? "PEER" : "");
654 if (iph1
->natt_flags
& NAT_DETECTED
)
655 natt_float_ports (iph1
);
659 /* payload existency check */
660 if (iph1
->dhpub_p
== NULL
|| iph1
->nonce_p
== NULL
) {
662 "few isakmp message received.\n");
666 if (oakley_checkcr(iph1
) < 0) {
667 /* Ignore this error in order to be interoperability. */
671 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_I_MSG4RCVD
);
675 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
676 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
677 CONSTSTR("Initiator, Main-Mode message 4"),
682 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
683 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
684 CONSTSTR("Initiator, Main-Mode Message 4"),
685 CONSTSTR("Failed to process Main-Mode Message 4"));
690 VPTRINIT(iph1
->dhpub_p
);
691 VPTRINIT(iph1
->nonce_p
);
692 VPTRINIT(iph1
->id_p
);
693 oakley_delcert(iph1
->cr_p
);
702 * psk: HDR*, IDi1, HASH_I
703 * sig: HDR*, IDi1, [ CR, ] [ CERT, ] SIG_I
704 * gssapi: HDR*, IDi1, < Gssi(n) | HASH_I >
709 ident_i5send(iph1
, msg0
)
710 phase1_handle_t
*iph1
;
717 if (iph1
->status
!= IKEV1_STATE_IDENT_I_MSG4RCVD
) {
719 "status mismatched %d.\n", iph1
->status
);
723 /* compute sharing secret of DH */
725 if (oakley_dh_compute(iph1
->approval
->dhgrp
, iph1
->dhpub
,
726 iph1
->dhpriv
, iph1
->dhpub_p
, &iph1
->dhgxy
) < 0) {
728 if (oakley_dh_compute(iph1
->approval
->dhgrp
, iph1
->dhpub_p
, iph1
->publicKeySize
, &iph1
->dhgxy
, &iph1
->dhC
) < 0) {
731 "failed to compute DH");
735 /* generate SKEYIDs & IV & final cipher key */
736 if (oakley_skeyid(iph1
) < 0) {
738 "failed to generate SKEYID");
741 if (oakley_skeyid_dae(iph1
) < 0) {
743 "failed to generate SKEYID-DAE");
746 if (oakley_compute_enckey(iph1
) < 0) {
748 "failed to generate ENCKEY");
751 if (oakley_newiv(iph1
) < 0) {
753 "failed to generate IV");
757 /* make ID payload into isakmp status */
758 if (ipsecdoi_setid1(iph1
) < 0) {
764 /* generate HASH to send */
766 iph1
->hash
= oakley_ph1hash_common(iph1
, GENERATE
);
767 if (iph1
->hash
== NULL
) {
769 "failed to generate HASH");
775 /* set encryption flag */
776 iph1
->flags
|= ISAKMP_FLAG_E
;
778 /* create HDR;ID;HASH payload */
779 iph1
->sendbuf
= ident_ir3mx(iph1
);
780 if (iph1
->sendbuf
== NULL
) {
782 "failed to allocate send buffer");
786 /* send the packet, add to the schedule to resend */
787 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
788 if (isakmp_ph1resend(iph1
) == -1) {
790 "failed to send packet");
794 /* the sending message is added to the received-list. */
795 if (ike_session_add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg0
,
796 PH1_NON_ESP_EXTRA_LEN(iph1
, iph1
->sendbuf
), PH1_FRAG_FLAGS(iph1
)) == -1) {
798 "failed to add a response packet to the tree.\n");
802 /* see handler.h about IV synchronization. */
803 memcpy(iph1
->ivm
->ive
->v
, iph1
->ivm
->iv
->v
, iph1
->ivm
->iv
->l
);
805 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_I_MSG5SENT
);
809 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
810 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
811 CONSTSTR("Initiator, Main-Mode message 5"),
816 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
817 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
818 CONSTSTR("Initiator, Main-Mode Message 5"),
819 CONSTSTR("Failed to transmit Main-Mode Message 5"));
825 * receive from responder
826 * psk: HDR*, IDr1, HASH_R
827 * sig: HDR*, IDr1, [ CERT, ] SIG_R
828 * gssapi: HDR*, IDr1, < GSSr(n) | HASH_R >
833 ident_i6recv(iph1
, msg0
)
834 phase1_handle_t
*iph1
;
837 vchar_t
*pbuf
= NULL
;
838 struct isakmp_parse_t
*pa
;
843 int received_cert
= 0;
846 if (iph1
->status
!= IKEV1_STATE_IDENT_I_MSG5SENT
) {
848 "status mismatched %d.\n", iph1
->status
);
853 if (!ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
855 "ignore the packet, "
856 "expecting the packet encrypted.\n");
859 msg
= oakley_do_decrypt(iph1
, msg0
, iph1
->ivm
->iv
, iph1
->ivm
->ive
);
862 "failed to decrypt");
866 /* validate the type of next payload */
867 pbuf
= isakmp_parse(msg
);
870 "failed to parse msg");
874 iph1
->pl_hash
= NULL
;
876 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
877 pa
->type
!= ISAKMP_NPTYPE_NONE
;
881 case ISAKMP_NPTYPE_ID
:
882 if (isakmp_p2ph(&iph1
->id_p
, pa
->ptr
) < 0) {
884 "failed to process ID payload");
888 case ISAKMP_NPTYPE_HASH
:
889 iph1
->pl_hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
891 case ISAKMP_NPTYPE_CERT
:
892 if (oakley_savecert(iph1
, pa
->ptr
) < 0) {
894 "failed to process CERT payload");
899 case ISAKMP_NPTYPE_SIG
:
900 if (isakmp_p2ph(&iph1
->sig_p
, pa
->ptr
) < 0) {
902 "failed to process SIG payload");
907 case ISAKMP_NPTYPE_VID
:
908 vid_numeric
= check_vendorid(pa
->ptr
);
910 if (vid_numeric
== VENDORID_DPD
&& iph1
->rmconf
->dpd
)
914 case ISAKMP_NPTYPE_N
:
915 isakmp_check_notify(pa
->ptr
, iph1
);
918 /* don't send information, see ident_r1recv() */
920 "ignore the packet, "
921 "received unexpecting payload type %d.\n",
928 oakley_verify_certid(iph1
);
931 /* payload existency check */
933 /* verify identifier */
934 if (ipsecdoi_checkid1(iph1
) != 0) {
936 "invalid ID payload.\n");
940 /* validate authentication value */
941 type
= oakley_validate_auth(iph1
);
943 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
944 IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_FAIL
,
945 CONSTSTR("Initiator, Main-Mode Message 6"),
946 CONSTSTR("Failed to authenticate Main-Mode Message 6"));
948 /* msg printed inner oakley_validate_auth() */
951 isakmp_info_send_n1(iph1
, type
, NULL
);
954 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
955 IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_SUCC
,
956 CONSTSTR("Initiator, Main-Mode Message 6"),
961 * XXX: Should we do compare two addresses, ph1handle's and ID
965 plogdump(ASL_LEVEL_DEBUG
, iph1
->id_p
->v
, iph1
->id_p
->l
, "peer's ID:");
967 /* see handler.h about IV synchronization. */
968 memcpy(iph1
->ivm
->iv
->v
, iph1
->ivm
->ive
->v
, iph1
->ivm
->ive
->l
);
971 * If we got a GSS token, we need to this roundtrip again.
973 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_I_MSG6RCVD
);
977 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
978 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
979 CONSTSTR("Initiator, Main-Mode message 6"),
984 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
985 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
986 CONSTSTR("Initiator, Main-Mode Message 6"),
987 CONSTSTR("Failed to transmit Main-Mode Message 6"));
995 VPTRINIT(iph1
->id_p
);
996 oakley_delcert(iph1
->cert_p
);
998 oakley_delcert(iph1
->crl_p
);
1000 VPTRINIT(iph1
->sig_p
);
1007 * status update and establish isakmp sa.
1010 ident_ifinalize(iph1
, msg
)
1011 phase1_handle_t
*iph1
;
1016 /* validity check */
1017 if (iph1
->status
!= IKEV1_STATE_IDENT_I_MSG6RCVD
) {
1019 "status mismatched %d.\n", iph1
->status
);
1023 /* see handler.h about IV synchronization. */
1024 memcpy(iph1
->ivm
->iv
->v
, iph1
->ivm
->ive
->v
, iph1
->ivm
->iv
->l
);
1026 fsm_set_state(&iph1
->status
, IKEV1_STATE_PHASE1_ESTABLISHED
);
1028 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1029 IPSECSESSIONEVENTCODE_IKEV1_PH1_INIT_SUCC
,
1030 CONSTSTR("Initiator, Main-Mode"),
1040 * receive from initiator
1047 ident_r1recv(iph1
, msg
)
1048 phase1_handle_t
*iph1
;
1051 vchar_t
*pbuf
= NULL
;
1052 struct isakmp_parse_t
*pa
;
1056 /* validity check */
1057 if (iph1
->status
!= IKEV1_STATE_IDENT_R_START
) {
1059 "status mismatched %d.\n", iph1
->status
);
1063 /* validate the type of next payload */
1065 * NOTE: XXX even if multiple VID, we'll silently ignore those.
1067 pbuf
= isakmp_parse(msg
);
1070 "failed to parse msg");
1073 pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
1075 /* check the position of SA payload */
1076 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
1078 "received invalid next payload type %d, "
1080 pa
->type
, ISAKMP_NPTYPE_SA
);
1083 if (isakmp_p2ph(&iph1
->sa
, pa
->ptr
) < 0) {
1085 "failed to process SA payload");
1091 pa
->type
!= ISAKMP_NPTYPE_NONE
;
1095 case ISAKMP_NPTYPE_VID
:
1096 vid_numeric
= check_vendorid(pa
->ptr
);
1098 if (iph1
->rmconf
->nat_traversal
&& natt_vendorid(vid_numeric
))
1099 natt_handle_vendorid(iph1
, vid_numeric
);
1101 #ifdef ENABLE_HYBRID
1102 switch (vid_numeric
) {
1103 case VENDORID_XAUTH
:
1104 iph1
->mode_cfg
->flags
|=
1105 ISAKMP_CFG_VENDORID_XAUTH
;
1108 case VENDORID_UNITY
:
1109 iph1
->mode_cfg
->flags
|=
1110 ISAKMP_CFG_VENDORID_UNITY
;
1118 if (vid_numeric
== VENDORID_DPD
&& iph1
->rmconf
->dpd
)
1119 iph1
->dpd_support
=1;
1122 if ((vid_numeric
== VENDORID_FRAG
) &&
1123 (vendorid_frag_cap(pa
->ptr
) & VENDORID_FRAG_IDENT
)) {
1124 plog(ASL_LEVEL_DEBUG
,
1125 "remote supports FRAGMENTATION\n");
1132 * We don't send information to the peer even
1133 * if we received malformed packet. Because we
1134 * can't distinguish the malformed packet and
1135 * the re-sent packet. And we do same behavior
1136 * when we expect encrypted packet.
1139 "ignore the packet, "
1140 "received unexpecting payload type %d.\n",
1147 if (NATT_AVAILABLE(iph1
)) {
1148 plog(ASL_LEVEL_NOTICE
,
1149 "Selected NAT-T version: %s\n",
1150 vid_string_by_id(iph1
->natt_options
->version
));
1151 ike_session_update_natt_version(iph1
);
1155 /* check SA payload and set approval SA for use */
1156 if (ipsecdoi_checkph1proposal(iph1
->sa
, iph1
) < 0) {
1158 "failed to get valid proposal.\n");
1159 /* XXX send information */
1163 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_R_MSG1RCVD
);
1167 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1168 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
1169 CONSTSTR("Responder, Main-Mode message 1"),
1174 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1175 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
1176 CONSTSTR("Responder, Main-Mode Message 1"),
1177 CONSTSTR("Failed to process Main-Mode Message 1"));
1196 ident_r2send(iph1
, msg
)
1197 phase1_handle_t
*iph1
;
1200 struct payload_list
*plist
= NULL
;
1202 vchar_t
*gss_sa
= NULL
;
1204 vchar_t
*vid_natt
= NULL
;
1206 #ifdef ENABLE_HYBRID
1207 vchar_t
*vid_xauth
= NULL
;
1208 vchar_t
*vid_unity
= NULL
;
1211 vchar_t
*vid_dpd
= NULL
;
1214 vchar_t
*vid_frag
= NULL
;
1217 /* validity check */
1218 if (iph1
->status
!= IKEV1_STATE_IDENT_R_MSG1RCVD
) {
1220 "status mismatched %d.\n", iph1
->status
);
1224 /* set responder's cookie */
1225 isakmp_newcookie((caddr_t
)&iph1
->index
.r_ck
, iph1
->remote
, iph1
->local
);
1226 gss_sa
= iph1
->sa_ret
;
1228 /* set SA payload to reply */
1229 plist
= isakmp_plist_append(plist
, gss_sa
, ISAKMP_NPTYPE_SA
);
1231 #ifdef ENABLE_HYBRID
1232 if (iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) {
1233 plog (ASL_LEVEL_NOTICE
, "Adding xauth VID payload.\n");
1234 if ((vid_xauth
= set_vendorid(VENDORID_XAUTH
)) == NULL
) {
1236 "Cannot create Xauth vendor ID\n");
1239 plist
= isakmp_plist_append(plist
,
1240 vid_xauth
, ISAKMP_NPTYPE_VID
);
1243 if (iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_UNITY
) {
1244 if ((vid_unity
= set_vendorid(VENDORID_UNITY
)) == NULL
) {
1246 "Cannot create Unity vendor ID\n");
1249 plist
= isakmp_plist_append(plist
,
1250 vid_unity
, ISAKMP_NPTYPE_VID
);
1254 /* Has the peer announced NAT-T? */
1255 if (NATT_AVAILABLE(iph1
))
1256 vid_natt
= set_vendorid(iph1
->natt_options
->version
);
1259 plist
= isakmp_plist_append(plist
, vid_natt
, ISAKMP_NPTYPE_VID
);
1262 /* XXX only send DPD VID if remote sent it ? */
1263 if(iph1
->rmconf
->dpd
){
1264 vid_dpd
= set_vendorid(VENDORID_DPD
);
1265 if (vid_dpd
!= NULL
)
1266 plist
= isakmp_plist_append(plist
, vid_dpd
, ISAKMP_NPTYPE_VID
);
1271 vid_frag
= set_vendorid(VENDORID_FRAG
);
1272 if (vid_frag
!= NULL
)
1273 vid_frag
= isakmp_frag_addcap(vid_frag
,
1274 VENDORID_FRAG_IDENT
);
1275 if (vid_frag
== NULL
)
1277 "Frag vendorID construction failed\n");
1279 plist
= isakmp_plist_append(plist
,
1280 vid_frag
, ISAKMP_NPTYPE_VID
);
1284 iph1
->sendbuf
= isakmp_plist_set_all (&plist
, iph1
);
1286 #ifdef HAVE_PRINT_ISAKMP_C
1287 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
1290 /* send the packet, add to the schedule to resend */
1291 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
1292 if (isakmp_ph1resend(iph1
) == -1) {
1294 "failed to send packet");
1298 /* the sending message is added to the received-list. */
1299 if (ike_session_add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
,
1300 PH1_NON_ESP_EXTRA_LEN(iph1
, iph1
->sendbuf
), PH1_FRAG_FLAGS(iph1
)) == -1) {
1301 plog(ASL_LEVEL_ERR
,
1302 "failed to add a response packet to the tree.\n");
1306 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_R_MSG2SENT
);
1308 #ifdef ENABLE_VPNCONTROL_PORT
1309 vpncontrol_notify_phase_change(1, FROM_LOCAL
, iph1
, NULL
);
1314 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1315 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1316 CONSTSTR("Responder, Main-Mode message 2"),
1321 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1322 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1323 CONSTSTR("Responder, Main-Mode Message 2"),
1324 CONSTSTR("Failed to transmit Main-Mode Message 2"));
1330 #ifdef ENABLE_HYBRID
1331 if (vid_xauth
!= NULL
)
1333 if (vid_unity
!= NULL
)
1337 if (vid_dpd
!= NULL
)
1341 if (vid_frag
!= NULL
)
1349 * receive from initiator
1352 * gssapi: HDR, KE, Ni, GSSi
1353 * rsa: HDR, KE, [ HASH(1), ] <IDi1_b>PubKey_r, <Ni_b>PubKey_r
1354 * rev: HDR, [ HASH(1), ] <Ni_b>Pubkey_r, <KE_b>Ke_i,
1355 * <IDi1_b>Ke_i, [<<Cert-I_b>Ke_i]
1358 ident_r3recv(iph1
, msg
)
1359 phase1_handle_t
*iph1
;
1362 vchar_t
*pbuf
= NULL
;
1363 struct isakmp_parse_t
*pa
;
1369 /* validity check */
1370 if (iph1
->status
!= IKEV1_STATE_IDENT_R_MSG2SENT
) {
1372 "status mismatched %d.\n", iph1
->status
);
1376 /* validate the type of next payload */
1377 pbuf
= isakmp_parse(msg
);
1380 "failed to parse msg");
1384 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
1385 pa
->type
!= ISAKMP_NPTYPE_NONE
;
1388 case ISAKMP_NPTYPE_KE
:
1389 if (isakmp_p2ph(&iph1
->dhpub_p
, pa
->ptr
) < 0) {
1391 "failed to process KE payload");
1395 case ISAKMP_NPTYPE_NONCE
:
1396 if (isakmp_p2ph(&iph1
->nonce_p
, pa
->ptr
) < 0) {
1398 "failed to process NONCE payload");
1402 case ISAKMP_NPTYPE_VID
:
1403 (void)check_vendorid(pa
->ptr
);
1405 case ISAKMP_NPTYPE_CR
:
1406 plog(ASL_LEVEL_WARNING
,
1407 "CR received, ignore it. "
1408 "It should be in other exchange.\n");
1412 case ISAKMP_NPTYPE_NATD_DRAFT
:
1413 case ISAKMP_NPTYPE_NATD_RFC
:
1414 case ISAKMP_NPTYPE_NATD_BADDRAFT
:
1415 if (NATT_AVAILABLE(iph1
) && iph1
->natt_options
!= NULL
&&
1416 pa
->type
== iph1
->natt_options
->payload_nat_d
)
1418 vchar_t
*natd_received
= NULL
;
1421 if (isakmp_p2ph (&natd_received
, pa
->ptr
) < 0) {
1423 "failed to process NATD payload");
1428 iph1
->natt_flags
|= NAT_DETECTED
;
1430 natd_verified
= natt_compare_addr_hash (iph1
,
1431 natd_received
, natd_seq
++);
1433 plog (ASL_LEVEL_NOTICE
, "NAT-D payload #%d %s\n",
1435 natd_verified
? "verified" : "doesn't match");
1437 vfree (natd_received
);
1440 /* %%%% Be lenient here - some servers send natd payloads */
1441 /* when no nat is detected */
1446 /* don't send information, see ident_r1recv() */
1448 "ignore the packet, "
1449 "received unexpecting payload type %d.\n",
1456 if (NATT_AVAILABLE(iph1
))
1457 plog (ASL_LEVEL_NOTICE
, "NAT %s %s%s\n",
1458 iph1
->natt_flags
& NAT_DETECTED
?
1459 "detected:" : "not detected",
1460 iph1
->natt_flags
& NAT_DETECTED_ME
? "ME " : "",
1461 iph1
->natt_flags
& NAT_DETECTED_PEER
? "PEER" : "");
1464 /* payload existency check */
1465 if (iph1
->dhpub_p
== NULL
|| iph1
->nonce_p
== NULL
) {
1467 "few isakmp message received.\n");
1471 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_R_MSG3RCVD
);
1475 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1476 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
1477 CONSTSTR("Responder, Main-Mode message 3"),
1482 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1483 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
1484 CONSTSTR("Responder, Main-Mode Message 3"),
1485 CONSTSTR("Failed to process Main-Mode Message 3"));
1491 VPTRINIT(iph1
->dhpub_p
);
1492 VPTRINIT(iph1
->nonce_p
);
1493 VPTRINIT(iph1
->id_p
);
1502 * sig: HDR, KE, Nr [, CR ]
1503 * gssapi: HDR, KE, Nr, GSSr
1504 * rsa: HDR, KE, <IDr1_b>PubKey_i, <Nr_b>PubKey_i
1505 * rev: HDR, <Nr_b>PubKey_i, <KE_b>Ke_r, <IDr1_b>Ke_r,
1508 ident_r4send(iph1
, msg
)
1509 phase1_handle_t
*iph1
;
1514 /* validity check */
1515 if (iph1
->status
!= IKEV1_STATE_IDENT_R_MSG3RCVD
) {
1517 "status mismatched %d.\n", iph1
->status
);
1521 /* generate DH public value */
1523 if (oakley_dh_generate(iph1
->approval
->dhgrp
,
1524 &iph1
->dhpub
, &iph1
->dhpriv
) < 0) {
1526 if (oakley_dh_generate(iph1
->approval
->dhgrp
,
1527 &iph1
->dhpub
, &iph1
->publicKeySize
, &iph1
->dhC
) < 0) {
1530 "failed to generate DH");
1534 /* generate NONCE value */
1535 iph1
->nonce
= eay_set_random(iph1
->rmconf
->nonce_size
);
1536 if (iph1
->nonce
== NULL
) {
1538 "failed to generate NONCE");
1542 /* create HDR;KE;NONCE payload */
1543 iph1
->sendbuf
= ident_ir2mx(iph1
);
1544 if (iph1
->sendbuf
== NULL
) {
1546 "failed to allocate send buffer");
1550 #ifdef HAVE_PRINT_ISAKMP_C
1551 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
1554 /* send the packet, add to the schedule to resend */
1555 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
1556 if (isakmp_ph1resend(iph1
) == -1) {
1558 "failed to send packet");
1562 /* the sending message is added to the received-list. */
1563 if (ike_session_add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
,
1564 PH1_NON_ESP_EXTRA_LEN(iph1
, iph1
->sendbuf
), PH1_FRAG_FLAGS(iph1
)) == -1) {
1565 plog(ASL_LEVEL_ERR
,
1566 "failed to add a response packet to the tree.\n");
1570 /* compute sharing secret of DH */
1572 if (oakley_dh_compute(iph1
->approval
->dhgrp
, iph1
->dhpub
,
1573 iph1
->dhpriv
, iph1
->dhpub_p
, &iph1
->dhgxy
) < 0) {
1575 if (oakley_dh_compute(iph1
->approval
->dhgrp
, iph1
->dhpub_p
, iph1
->publicKeySize
, &iph1
->dhgxy
, &iph1
->dhC
) < 0) {
1578 "failed to compute DH");
1582 /* generate SKEYIDs & IV & final cipher key */
1583 if (oakley_skeyid(iph1
) < 0) {
1585 "failed to generate SKEYID");
1588 if (oakley_skeyid_dae(iph1
) < 0) {
1590 "failed to generate SKEYID-DAE");
1593 if (oakley_compute_enckey(iph1
) < 0) {
1595 "failed to generate ENCKEY");
1598 if (oakley_newiv(iph1
) < 0) {
1600 "failed to generate IV");
1604 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_R_MSG4SENT
);
1608 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1609 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1610 CONSTSTR("Responder, Main-Mode message 4"),
1615 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1616 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1617 CONSTSTR("Responder, Main-Mode Message 4"),
1618 CONSTSTR("Failed to transmit Main-Mode Message 4"));
1624 * receive from initiator
1625 * psk: HDR*, IDi1, HASH_I
1626 * sig: HDR*, IDi1, [ CR, ] [ CERT, ] SIG_I
1627 * gssapi: HDR*, [ IDi1, ] < GSSi(n) | HASH_I >
1632 ident_r5recv(iph1
, msg0
)
1633 phase1_handle_t
*iph1
;
1636 vchar_t
*msg
= NULL
;
1637 vchar_t
*pbuf
= NULL
;
1638 struct isakmp_parse_t
*pa
;
1641 int received_cert
= 0;
1643 /* validity check */
1644 if (iph1
->status
!= IKEV1_STATE_IDENT_R_MSG4SENT
) {
1646 "status mismatched %d.\n", iph1
->status
);
1651 if (!ISSET(((struct isakmp
*)msg0
->v
)->flags
, ISAKMP_FLAG_E
)) {
1653 "reject the packet, "
1654 "expecting the packet encrypted.\n");
1657 msg
= oakley_do_decrypt(iph1
, msg0
, iph1
->ivm
->iv
, iph1
->ivm
->ive
);
1660 "failed to decrypt");
1664 /* validate the type of next payload */
1665 pbuf
= isakmp_parse(msg
);
1668 "failed to parse msg");
1672 iph1
->pl_hash
= NULL
;
1674 for (pa
= ALIGNED_CAST(struct isakmp_parse_t
*)pbuf
->v
;
1675 pa
->type
!= ISAKMP_NPTYPE_NONE
;
1679 case ISAKMP_NPTYPE_ID
:
1680 if (isakmp_p2ph(&iph1
->id_p
, pa
->ptr
) < 0) {
1682 "failed to process ID payload");
1686 case ISAKMP_NPTYPE_HASH
:
1687 iph1
->pl_hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
1689 case ISAKMP_NPTYPE_CR
:
1690 if (oakley_savecr(iph1
, pa
->ptr
) < 0) {
1692 "failed to process CR payload");
1696 case ISAKMP_NPTYPE_CERT
:
1697 if (oakley_savecert(iph1
, pa
->ptr
) < 0) {
1699 "failed to process CERT payload");
1704 case ISAKMP_NPTYPE_SIG
:
1705 if (isakmp_p2ph(&iph1
->sig_p
, pa
->ptr
) < 0) {
1707 "failed to process SIG payload");
1711 case ISAKMP_NPTYPE_VID
:
1712 (void)check_vendorid(pa
->ptr
);
1714 case ISAKMP_NPTYPE_N
:
1715 isakmp_check_notify(pa
->ptr
, iph1
);
1718 /* don't send information, see ident_r1recv() */
1720 "ignore the packet, "
1721 "received unexpecting payload type %d.\n",
1727 if (received_cert
) {
1728 oakley_verify_certid(iph1
);
1731 /* payload existency check */
1732 /* XXX same as ident_i4recv(), should be merged. */
1736 switch (AUTHMETHOD(iph1
)) {
1737 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
1738 #ifdef ENABLE_HYBRID
1739 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1740 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1742 if (iph1
->id_p
== NULL
|| iph1
->pl_hash
== NULL
)
1745 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
1746 #ifdef ENABLE_HYBRID
1747 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1749 if (iph1
->id_p
== NULL
|| iph1
->sig_p
== NULL
)
1752 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
1753 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
1754 #ifdef ENABLE_HYBRID
1755 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1756 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1758 if (iph1
->pl_hash
== NULL
)
1763 "invalid authmethod %d why ?\n",
1764 iph1
->approval
->authmethod
);
1769 "few isakmp message received.\n");
1774 /* verify identifier */
1775 if (ipsecdoi_checkid1(iph1
) != 0) {
1777 "invalid ID payload.\n");
1781 /* validate authentication value */
1783 type
= oakley_validate_auth(iph1
);
1785 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1786 IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_FAIL
,
1787 CONSTSTR("Responder, Main-Mode Message 5"),
1788 CONSTSTR("Failed to authenticate Main-Mode Message 5"));
1790 /* msg printed inner oakley_validate_auth() */
1793 isakmp_info_send_n1(iph1
, type
, NULL
);
1796 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1797 IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_SUCC
,
1798 CONSTSTR("Responder, Main-Mode Message 5"),
1801 if (oakley_checkcr(iph1
) < 0) {
1802 /* Ignore this error in order to be interoperability. */
1807 * XXX: Should we do compare two addresses, ph1handle's and ID
1811 plogdump(ASL_LEVEL_DEBUG
, iph1
->id_p
->v
, iph1
->id_p
->l
, "peer's ID\n");
1813 /* see handler.h about IV synchronization. */
1814 memcpy(iph1
->ivm
->iv
->v
, iph1
->ivm
->ive
->v
, iph1
->ivm
->ive
->l
);
1816 fsm_set_state(&iph1
->status
, IKEV1_STATE_IDENT_R_MSG5RCVD
);
1819 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1820 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC
,
1821 CONSTSTR("Responder, Main-Mode message 5"),
1826 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1827 IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL
,
1828 CONSTSTR("Responder, Main-Mode Message 5"),
1829 CONSTSTR("Failed to process Main-Mode Message 5"));
1837 VPTRINIT(iph1
->id_p
);
1838 oakley_delcert(iph1
->cert_p
);
1839 iph1
->cert_p
= NULL
;
1840 oakley_delcert(iph1
->crl_p
);
1842 VPTRINIT(iph1
->sig_p
);
1843 oakley_delcert(iph1
->cr_p
);
1852 * psk: HDR*, IDr1, HASH_R
1853 * sig: HDR*, IDr1, [ CERT, ] SIG_R
1854 * gssapi: HDR*, IDr1, < GSSr(n) | HASH_R >
1859 ident_r6send(iph1
, msg
)
1860 phase1_handle_t
*iph1
;
1866 /* validity check */
1867 if (iph1
->status
!= IKEV1_STATE_IDENT_R_MSG5RCVD
) {
1869 "status mismatched %d.\n", iph1
->status
);
1873 /* make ID payload into isakmp status */
1874 if (ipsecdoi_setid1(iph1
) < 0) {
1876 "failed to set ID");
1881 /* generate HASH to send */
1882 plog(ASL_LEVEL_DEBUG
, "generate HASH_R\n");
1883 iph1
->hash
= oakley_ph1hash_common(iph1
, GENERATE
);
1884 if (iph1
->hash
== NULL
) {
1886 "failed to generate HASH");
1892 /* set encryption flag */
1893 iph1
->flags
|= ISAKMP_FLAG_E
;
1895 /* create HDR;ID;HASH payload */
1896 iph1
->sendbuf
= ident_ir3mx(iph1
);
1897 if (iph1
->sendbuf
== NULL
) {
1899 "failed to create send buffer");
1903 /* send HDR;ID;HASH to responder */
1904 if (isakmp_send(iph1
, iph1
->sendbuf
) < 0) {
1906 "failed to send packet");
1910 /* the sending message is added to the received-list. */
1911 if (ike_session_add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
,
1912 PH1_NON_ESP_EXTRA_LEN(iph1
, iph1
->sendbuf
), PH1_FRAG_FLAGS(iph1
)) == -1) {
1913 plog(ASL_LEVEL_ERR
,
1914 "failed to add a response packet to the tree.\n");
1918 /* see handler.h about IV synchronization. */
1919 memcpy(iph1
->ivm
->ive
->v
, iph1
->ivm
->iv
->v
, iph1
->ivm
->iv
->l
);
1921 fsm_set_state(&iph1
->status
, IKEV1_STATE_PHASE1_ESTABLISHED
);
1923 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1924 IPSECSESSIONEVENTCODE_IKEV1_PH1_RESP_SUCC
,
1925 CONSTSTR("Responder, Main-Mode"),
1930 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1931 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC
,
1932 CONSTSTR("Responder, Main-Mode message 6"),
1937 IPSECSESSIONTRACEREVENT(iph1
->parent_session
,
1938 IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL
,
1939 CONSTSTR("Responder, Main-Mode Message 6"),
1940 CONSTSTR("Failed to process Main-Mode Message 6"));
1947 * This is used in main mode for:
1948 * initiator's 3rd exchange send to responder
1951 * rsa: HDR, KE, [ HASH(1), ] <IDi1_b>PubKey_r, <Ni_b>PubKey_r
1952 * rev: HDR, [ HASH(1), ] <Ni_b>Pubkey_r, <KE_b>Ke_i,
1953 * <IDi1_b>Ke_i, [<<Cert-I_b>Ke_i]
1954 * responders 2nd exchnage send to initiator
1956 * sig: HDR, KE, Nr [, CR ]
1957 * rsa: HDR, KE, <IDr1_b>PubKey_i, <Nr_b>PubKey_i
1958 * rev: HDR, <Nr_b>PubKey_i, <KE_b>Ke_r, <IDr1_b>Ke_r,
1962 phase1_handle_t
*iph1
;
1965 struct payload_list
*plist
= NULL
;
1968 vchar_t
*vid
= NULL
;
1971 vchar_t
*natd
[2] = { NULL
, NULL
};
1974 /* create CR if need */
1975 if (iph1
->side
== RESPONDER
1976 && iph1
->rmconf
->send_cr
1977 && oakley_needcr(iph1
->approval
->authmethod
)) {
1979 cr
= oakley_getcr(iph1
);
1982 "failed to get cr buffer.\n");
1987 /* create isakmp KE payload */
1988 plist
= isakmp_plist_append(plist
, iph1
->dhpub
, ISAKMP_NPTYPE_KE
);
1990 /* create isakmp NONCE payload */
1991 plist
= isakmp_plist_append(plist
, iph1
->nonce
, ISAKMP_NPTYPE_NONCE
);
1993 /* append vendor id, if needed */
1995 plist
= isakmp_plist_append(plist
, vid
, ISAKMP_NPTYPE_VID
);
1997 /* create isakmp CR payload if needed */
1999 plist
= isakmp_plist_append(plist
, cr
, ISAKMP_NPTYPE_CR
);
2002 /* generate and append NAT-D payloads */
2003 if (NATT_AVAILABLE(iph1
))
2005 if ((natd
[0] = natt_hash_addr (iph1
, iph1
->remote
)) == NULL
) {
2007 "NAT-D hashing failed for %s\n", saddr2str((struct sockaddr
*)iph1
->remote
));
2011 if ((natd
[1] = natt_hash_addr (iph1
, iph1
->local
)) == NULL
) {
2013 "NAT-D hashing failed for %s\n", saddr2str((struct sockaddr
*)iph1
->local
));
2017 plog (ASL_LEVEL_NOTICE
, "Adding remote and local NAT-D payloads.\n");
2018 /* old Apple version sends natd payloads in the wrong order */
2019 if (iph1
->natt_options
->version
== VENDORID_NATT_APPLE
) {
2020 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
2021 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
2024 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
2025 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
2030 buf
= isakmp_plist_set_all (&plist
, iph1
);
2035 if (error
&& buf
!= NULL
) {
2055 * This is used in main mode for:
2056 * initiator's 4th exchange send to responder
2057 * psk: HDR*, IDi1, HASH_I
2058 * sig: HDR*, IDi1, [ CR, ] [ CERT, ] SIG_I
2059 * gssapi: HDR*, [ IDi1, ] < GSSi(n) | HASH_I >
2062 * responders 3rd exchnage send to initiator
2063 * psk: HDR*, IDr1, HASH_R
2064 * sig: HDR*, IDr1, [ CERT, ] SIG_R
2065 * gssapi: HDR*, [ IDr1, ] < GSSr(n) | HASH_R >
2071 phase1_handle_t
*iph1
;
2073 struct payload_list
*plist
= NULL
;
2074 vchar_t
*buf
= NULL
, *new = NULL
;
2079 vchar_t
*notp_ini
= NULL
;
2081 switch (AUTHMETHOD(iph1
)) {
2082 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
2083 #ifdef ENABLE_HYBRID
2084 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
2085 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
2086 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
2088 /* create isakmp ID payload */
2089 plist
= isakmp_plist_append(plist
, iph1
->id
, ISAKMP_NPTYPE_ID
);
2091 /* create isakmp HASH payload */
2092 plist
= isakmp_plist_append(plist
, iph1
->hash
, ISAKMP_NPTYPE_HASH
);
2094 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
2095 #ifdef ENABLE_HYBRID
2096 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
2097 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
2098 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
2100 if (oakley_getmycert(iph1
) < 0) {
2102 "failed to get mycert");
2106 if (oakley_getsign(iph1
) < 0) {
2108 "failed to get sign");
2112 /* create CR if need */
2113 if (iph1
->side
== INITIATOR
2114 && iph1
->rmconf
->send_cr
2115 && oakley_needcr(iph1
->approval
->authmethod
)) {
2117 cr
= oakley_getcr(iph1
);
2120 "failed to get CR");
2125 if (iph1
->cert
!= NULL
&& iph1
->rmconf
->send_cert
)
2128 /* add ID payload */
2129 plist
= isakmp_plist_append(plist
, iph1
->id
, ISAKMP_NPTYPE_ID
);
2131 /* add CERT payload if there */
2132 // we don't support sending of certchains
2134 plist
= isakmp_plist_append(plist
, iph1
->cert
->pl
, ISAKMP_NPTYPE_CERT
);
2135 /* add SIG payload */
2136 plist
= isakmp_plist_append(plist
, iph1
->sig
, ISAKMP_NPTYPE_SIG
);
2138 /* create isakmp CR payload */
2140 plist
= isakmp_plist_append(plist
, cr
, ISAKMP_NPTYPE_CR
);
2143 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
2144 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
2145 #ifdef ENABLE_HYBRID
2146 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
:
2147 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
2148 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
:
2149 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
2152 "not supported authentication type %d\n",
2153 iph1
->approval
->authmethod
);
2157 "invalid authentication type %d\n",
2158 iph1
->approval
->authmethod
);
2162 if (iph1
->side
== INITIATOR
) {
2163 notp_ini
= isakmp_plist_append_initial_contact(iph1
, plist
);
2166 buf
= isakmp_plist_set_all (&plist
, iph1
);
2168 #ifdef HAVE_PRINT_ISAKMP_C
2169 isakmp_printpacket(buf
, iph1
->local
, iph1
->remote
, 1);
2173 new = oakley_do_encrypt(iph1
, buf
, iph1
->ivm
->ive
, iph1
->ivm
->iv
);
2176 "failed to encrypt");
2189 if (error
&& buf
!= NULL
) {