1 /* $NetBSD: isakmp_base.c,v 1.7 2006/10/02 21:51:33 manu Exp $ */
3 /* $KAME: isakmp_base.c,v 1.49 2003/11/13 02:30:20 sakane 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 /* Base Exchange (Base 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_base.h"
79 #include "isakmp_inf.h"
82 #include "nattraversal.h"
85 #include "isakmp_frag.h"
88 #include "isakmp_xauth.h"
89 #include "isakmp_cfg.h"
91 #include "vpn_control.h"
92 #include "vpn_control_var.h"
95 * begin Identity Protection Mode as initiator.
99 * psk: HDR, SA, Idii, Ni_b
100 * sig: HDR, SA, Idii, Ni_b
101 * rsa: HDR, SA, [HASH(1),] <IDii_b>Pubkey_r, <Ni_b>Pubkey_r
102 * rev: HDR, SA, [HASH(1),] <Ni_b>Pubkey_r, <IDii_b>Ke_i
105 base_i1send(iph1
, msg
)
106 struct ph1handle
*iph1
;
107 vchar_t
*msg
; /* must be null */
109 struct payload_list
*plist
= NULL
;
112 vchar_t
*vid_natt
[MAX_NATT_VID_COUNT
] = { NULL
};
113 int i
, vid_natt_i
= 0;
116 vchar_t
*vid_frag
= NULL
;
119 vchar_t
*vid_xauth
= NULL
;
120 vchar_t
*vid_unity
= NULL
;
123 vchar_t
*vid_dpd
= NULL
;
129 plog(LLV_ERROR
, LOCATION
, NULL
,
130 "msg has to be NULL in this function.\n");
133 if (iph1
->status
!= PHASE1ST_START
) {
134 plog(LLV_ERROR
, LOCATION
, NULL
,
135 "status mismatched %d.\n", iph1
->status
);
139 /* create isakmp index */
140 memset(&iph1
->index
, 0, sizeof(iph1
->index
));
141 isakmp_newcookie((caddr_t
)&iph1
->index
, iph1
->remote
, iph1
->local
);
143 /* make ID payload into isakmp status */
144 if (ipsecdoi_setid1(iph1
) < 0)
147 /* create SA payload for my proposal */
148 iph1
->sa
= ipsecdoi_setph1proposal(iph1
->rmconf
->proposal
);
149 if (iph1
->sa
== NULL
)
152 /* generate NONCE value */
153 iph1
->nonce
= eay_set_random(iph1
->rmconf
->nonce_size
);
154 if (iph1
->nonce
== NULL
)
158 /* Do we need Xauth VID? */
159 switch (RMAUTHMETHOD(iph1
)) {
160 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
161 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
162 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_I
:
163 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
164 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_I
:
165 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
:
166 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
:
167 if ((vid_xauth
= set_vendorid(VENDORID_XAUTH
)) == NULL
)
168 plog(LLV_ERROR
, LOCATION
, NULL
,
169 "Xauth vendor ID generation failed\n");
171 if ((vid_unity
= set_vendorid(VENDORID_UNITY
)) == NULL
)
172 plog(LLV_ERROR
, LOCATION
, NULL
,
173 "Unity vendor ID generation failed\n");
180 if (iph1
->rmconf
->ike_frag
) {
181 vid_frag
= set_vendorid(VENDORID_FRAG
);
182 if (vid_frag
!= NULL
)
183 vid_frag
= isakmp_frag_addcap(vid_frag
,
185 if (vid_frag
== NULL
)
186 plog(LLV_ERROR
, LOCATION
, NULL
,
187 "Frag vendorID construction failed\n");
191 /* Is NAT-T support allowed in the config file? */
192 if (iph1
->rmconf
->nat_traversal
) {
193 /* Advertise NAT-T capability */
194 memset (vid_natt
, 0, sizeof (vid_natt
));
195 #ifdef VENDORID_NATT_00
196 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_00
)) != NULL
)
199 #ifdef VENDORID_NATT_02
200 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_02
)) != NULL
)
203 #ifdef VENDORID_NATT_02_N
204 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_02_N
)) != NULL
)
207 #ifdef VENDORID_NATT_RFC
208 if ((vid_natt
[vid_natt_i
] = set_vendorid(VENDORID_NATT_RFC
)) != NULL
)
214 /* set SA payload to propose */
215 plist
= isakmp_plist_append(plist
, iph1
->sa
, ISAKMP_NPTYPE_SA
);
217 /* create isakmp ID payload */
218 plist
= isakmp_plist_append(plist
, iph1
->id
, ISAKMP_NPTYPE_ID
);
220 /* create isakmp NONCE payload */
221 plist
= isakmp_plist_append(plist
, iph1
->nonce
, ISAKMP_NPTYPE_NONCE
);
225 plist
= isakmp_plist_append(plist
, vid_frag
, ISAKMP_NPTYPE_VID
);
229 plist
= isakmp_plist_append(plist
,
230 vid_xauth
, ISAKMP_NPTYPE_VID
);
232 plist
= isakmp_plist_append(plist
,
233 vid_unity
, ISAKMP_NPTYPE_VID
);
236 if (iph1
->rmconf
->dpd
) {
237 vid_dpd
= set_vendorid(VENDORID_DPD
);
239 plist
= isakmp_plist_append(plist
, vid_dpd
, ISAKMP_NPTYPE_VID
);
243 /* set VID payload for NAT-T */
244 for (i
= 0; i
< vid_natt_i
; i
++)
245 plist
= isakmp_plist_append(plist
, vid_natt
[i
], ISAKMP_NPTYPE_VID
);
247 iph1
->sendbuf
= isakmp_plist_set_all (&plist
, iph1
);
250 #ifdef HAVE_PRINT_ISAKMP_C
251 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
254 /* send the packet, add to the schedule to resend */
255 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
256 if (isakmp_ph1resend(iph1
) == -1)
259 iph1
->status
= PHASE1ST_MSG1SENT
;
269 for (i
= 0; i
< vid_natt_i
; i
++)
273 if (vid_xauth
!= NULL
)
275 if (vid_unity
!= NULL
)
287 * receive from responder
288 * psk: HDR, SA, Idir, Nr_b
289 * sig: HDR, SA, Idir, Nr_b, [ CR ]
290 * rsa: HDR, SA, <IDir_b>PubKey_i, <Nr_b>PubKey_i
291 * rev: HDR, SA, <Nr_b>PubKey_i, <IDir_b>Ke_r
294 base_i2recv(iph1
, msg
)
295 struct ph1handle
*iph1
;
298 vchar_t
*pbuf
= NULL
;
299 struct isakmp_parse_t
*pa
;
300 vchar_t
*satmp
= NULL
;
309 if (iph1
->status
!= PHASE1ST_MSG1SENT
) {
310 plog(LLV_ERROR
, LOCATION
, NULL
,
311 "status mismatched %d.\n", iph1
->status
);
315 /* validate the type of next payload */
316 pbuf
= isakmp_parse(msg
);
319 pa
= (struct isakmp_parse_t
*)pbuf
->v
;
321 /* SA payload is fixed postion */
322 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
323 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
324 "received invalid next payload type %d, "
326 pa
->type
, ISAKMP_NPTYPE_SA
);
329 if (isakmp_p2ph(&satmp
, pa
->ptr
) < 0)
334 pa
->type
!= ISAKMP_NPTYPE_NONE
;
338 case ISAKMP_NPTYPE_NONCE
:
339 if (isakmp_p2ph(&iph1
->nonce_p
, pa
->ptr
) < 0)
342 case ISAKMP_NPTYPE_ID
:
343 if (isakmp_p2ph(&iph1
->id_p
, pa
->ptr
) < 0)
346 case ISAKMP_NPTYPE_VID
:
347 vid_numeric
= check_vendorid(pa
->ptr
);
349 if (iph1
->rmconf
->nat_traversal
&& natt_vendorid(vid_numeric
))
350 natt_handle_vendorid(iph1
, vid_numeric
);
353 switch (vid_numeric
) {
355 iph1
->mode_cfg
->flags
|=
356 ISAKMP_CFG_VENDORID_XAUTH
;
360 iph1
->mode_cfg
->flags
|=
361 ISAKMP_CFG_VENDORID_UNITY
;
369 if (vid_numeric
== VENDORID_DPD
&& iph1
->rmconf
->dpd
) {
371 plog(LLV_DEBUG
, LOCATION
, NULL
,
372 "remote supports DPD\n");
377 /* don't send information, see ident_r1recv() */
378 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
379 "ignore the packet, "
380 "received unexpecting payload type %d.\n",
386 if (iph1
->nonce_p
== NULL
|| iph1
->id_p
== NULL
) {
387 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
388 "few isakmp message received.\n");
392 /* verify identifier */
393 if (ipsecdoi_checkid1(iph1
) != 0) {
394 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
395 "invalid ID payload.\n");
400 if (NATT_AVAILABLE(iph1
))
401 plog(LLV_INFO
, LOCATION
, iph1
->remote
,
402 "Selected NAT-T version: %s\n",
403 vid_string_by_id(iph1
->natt_options
->version
));
406 /* check SA payload and set approval SA for use */
407 if (ipsecdoi_checkph1proposal(satmp
, iph1
) < 0) {
408 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
409 "failed to get valid proposal.\n");
410 /* XXX send information */
413 VPTRINIT(iph1
->sa_ret
);
415 iph1
->status
= PHASE1ST_MSG2RECEIVED
;
417 #ifdef ENABLE_VPNCONTROL_PORT
418 vpncontrol_notify_phase_change(1, FROM_REMOTE
, iph1
, NULL
);
430 VPTRINIT(iph1
->nonce_p
);
431 VPTRINIT(iph1
->id_p
);
439 * psk: HDR, KE, HASH_I
440 * sig: HDR, KE, [ CR, ] [CERT,] SIG_I
441 * rsa: HDR, KE, HASH_I
442 * rev: HDR, <KE>Ke_i, HASH_I
445 base_i2send(iph1
, msg
)
446 struct ph1handle
*iph1
;
449 struct payload_list
*plist
= NULL
;
455 if (iph1
->status
!= PHASE1ST_MSG2RECEIVED
) {
456 plog(LLV_ERROR
, LOCATION
, NULL
,
457 "status mismatched %d.\n", iph1
->status
);
461 /* fix isakmp index */
462 memcpy(&iph1
->index
.r_ck
, &((struct isakmp
*)msg
->v
)->r_ck
,
465 /* generate DH public value */
466 if (oakley_dh_generate(iph1
->approval
->dhgrp
,
467 &iph1
->dhpub
, &iph1
->dhpriv
) < 0)
470 /* generate SKEYID to compute hash if not signature mode */
471 switch (AUTHMETHOD(iph1
)) {
472 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
473 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
475 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_I
:
476 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
477 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_I
:
478 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
479 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_I
:
483 if (oakley_skeyid(iph1
) < 0)
488 /* generate HASH to send */
489 plog(LLV_DEBUG
, LOCATION
, NULL
, "generate HASH_I\n");
490 iph1
->hash
= oakley_ph1hash_base_i(iph1
, GENERATE
);
491 if (iph1
->hash
== NULL
)
493 switch (AUTHMETHOD(iph1
)) {
494 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
496 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
497 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
498 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_I
:
500 vid
= set_vendorid(iph1
->approval
->vendorid
);
502 /* create isakmp KE payload */
503 plist
= isakmp_plist_append(plist
, iph1
->dhpub
, ISAKMP_NPTYPE_KE
);
505 /* create isakmp HASH payload */
506 plist
= isakmp_plist_append(plist
, iph1
->hash
, ISAKMP_NPTYPE_HASH
);
508 /* append vendor id, if needed */
510 plist
= isakmp_plist_append(plist
, vid
, ISAKMP_NPTYPE_VID
);
512 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
513 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
515 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
516 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_I
:
518 /* XXX if there is CR or not ? */
520 if (oakley_getmycert(iph1
) < 0)
523 if (oakley_getsign(iph1
) < 0)
526 if (iph1
->cert
&& iph1
->rmconf
->send_cert
)
529 /* create isakmp KE payload */
530 plist
= isakmp_plist_append(plist
, iph1
->dhpub
, ISAKMP_NPTYPE_KE
);
532 /* add CERT payload if there */
534 plist
= isakmp_plist_append(plist
, iph1
->cert
->pl
, ISAKMP_NPTYPE_CERT
);
536 /* add SIG payload */
537 plist
= isakmp_plist_append(plist
, iph1
->sig
, ISAKMP_NPTYPE_SIG
);
540 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
544 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
545 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
547 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
:
548 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
:
554 /* generate NAT-D payloads */
555 if (NATT_AVAILABLE(iph1
))
557 vchar_t
*natd
[2] = { NULL
, NULL
};
559 plog (LLV_INFO
, LOCATION
, NULL
, "Adding remote and local NAT-D payloads.\n");
560 if ((natd
[0] = natt_hash_addr (iph1
, iph1
->remote
)) == NULL
) {
561 plog(LLV_ERROR
, LOCATION
, NULL
,
562 "NAT-D hashing failed for %s\n", saddr2str(iph1
->remote
));
566 if ((natd
[1] = natt_hash_addr (iph1
, iph1
->local
)) == NULL
) {
567 plog(LLV_ERROR
, LOCATION
, NULL
,
568 "NAT-D hashing failed for %s\n", saddr2str(iph1
->local
));
573 /* old Apple version sends natd payloads in the wrong order */
574 if (iph1
->natt_options
->version
== VENDORID_NATT_APPLE
) {
575 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
576 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
580 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
581 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
586 iph1
->sendbuf
= isakmp_plist_set_all (&plist
, iph1
);
588 #ifdef HAVE_PRINT_ISAKMP_C
589 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
592 /* send the packet, add to the schedule to resend */
593 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
594 if (isakmp_ph1resend(iph1
) == -1)
597 /* the sending message is added to the received-list. */
598 if (add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
,
599 PH1_NON_ESP_EXTRA_LEN(iph1
)) == -1) {
600 plog(LLV_ERROR
, LOCATION
, NULL
,
601 "failed to add a response packet to the tree.\n");
605 iph1
->status
= PHASE1ST_MSG2SENT
;
616 * receive from responder
617 * psk: HDR, KE, HASH_R
618 * sig: HDR, KE, [CERT,] SIG_R
619 * rsa: HDR, KE, HASH_R
620 * rev: HDR, <KE>_Ke_r, HASH_R
623 base_i3recv(iph1
, msg
)
624 struct ph1handle
*iph1
;
627 vchar_t
*pbuf
= NULL
;
628 struct isakmp_parse_t
*pa
;
632 vchar_t
*natd_received
;
633 int natd_seq
= 0, natd_verified
;
637 if (iph1
->status
!= PHASE1ST_MSG2SENT
) {
638 plog(LLV_ERROR
, LOCATION
, NULL
,
639 "status mismatched %d.\n", iph1
->status
);
643 /* validate the type of next payload */
644 pbuf
= isakmp_parse(msg
);
648 for (pa
= (struct isakmp_parse_t
*)pbuf
->v
;
649 pa
->type
!= ISAKMP_NPTYPE_NONE
;
653 case ISAKMP_NPTYPE_KE
:
654 if (isakmp_p2ph(&iph1
->dhpub_p
, pa
->ptr
) < 0)
657 case ISAKMP_NPTYPE_HASH
:
658 iph1
->pl_hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
660 case ISAKMP_NPTYPE_CERT
:
661 if (oakley_savecert(iph1
, pa
->ptr
) < 0)
664 case ISAKMP_NPTYPE_SIG
:
665 if (isakmp_p2ph(&iph1
->sig_p
, pa
->ptr
) < 0)
668 case ISAKMP_NPTYPE_VID
:
669 (void)check_vendorid(pa
->ptr
);
673 case ISAKMP_NPTYPE_NATD_DRAFT
:
674 case ISAKMP_NPTYPE_NATD_RFC
:
676 case ISAKMP_NPTYPE_NATD_BADDRAFT
:
678 if (NATT_AVAILABLE(iph1
) && iph1
->natt_options
&&
679 pa
->type
== iph1
->natt_options
->payload_nat_d
) {
680 natd_received
= NULL
;
681 if (isakmp_p2ph (&natd_received
, pa
->ptr
) < 0)
684 /* set both bits first so that we can clear them
685 upon verifying hashes */
687 iph1
->natt_flags
|= NAT_DETECTED
;
689 /* this function will clear appropriate bits bits
690 from iph1->natt_flags */
691 natd_verified
= natt_compare_addr_hash (iph1
,
692 natd_received
, natd_seq
++);
694 plog (LLV_INFO
, LOCATION
, NULL
, "NAT-D payload #%d %s\n",
696 natd_verified
? "verified" : "doesn't match");
698 vfree (natd_received
);
701 /* %%%% Be lenient here - some servers send natd payloads */
702 /* when no nat is detected */
707 /* don't send information, see ident_r1recv() */
708 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
709 "ignore the packet, "
710 "received unexpecting payload type %d.\n",
717 if (NATT_AVAILABLE(iph1
)) {
718 plog (LLV_INFO
, LOCATION
, NULL
, "NAT %s %s%s\n",
719 iph1
->natt_flags
& NAT_DETECTED
?
720 "detected:" : "not detected",
721 iph1
->natt_flags
& NAT_DETECTED_ME
? "ME " : "",
722 iph1
->natt_flags
& NAT_DETECTED_PEER
? "PEER" : "");
723 if (iph1
->natt_flags
& NAT_DETECTED
)
724 natt_float_ports (iph1
);
728 /* payload existency check */
729 /* validate authentication value */
730 ptype
= oakley_validate_auth(iph1
);
733 /* message printed inner oakley_validate_auth() */
736 EVT_PUSH(iph1
->local
, iph1
->remote
,
737 EVTT_PEERPH1AUTH_FAILED
, NULL
);
738 isakmp_info_send_n1(iph1
, ptype
, NULL
);
742 /* compute sharing secret of DH */
743 if (oakley_dh_compute(iph1
->approval
->dhgrp
, iph1
->dhpub
,
744 iph1
->dhpriv
, iph1
->dhpub_p
, &iph1
->dhgxy
) < 0)
747 /* generate SKEYID to compute hash if signature mode */
748 switch (AUTHMETHOD(iph1
)) {
749 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
750 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
752 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_I
:
753 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
754 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_I
:
755 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
756 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_I
:
758 if (oakley_skeyid(iph1
) < 0)
765 /* generate SKEYIDs & IV & final cipher key */
766 if (oakley_skeyid_dae(iph1
) < 0)
768 if (oakley_compute_enckey(iph1
) < 0)
770 if (oakley_newiv(iph1
) < 0)
773 /* see handler.h about IV synchronization. */
774 memcpy(iph1
->ivm
->iv
->v
, iph1
->ivm
->ive
->v
, iph1
->ivm
->iv
->l
);
776 /* set encryption flag */
777 iph1
->flags
|= ISAKMP_FLAG_E
;
779 iph1
->status
= PHASE1ST_MSG3RECEIVED
;
788 VPTRINIT(iph1
->dhpub_p
);
789 oakley_delcert(iph1
->cert_p
);
791 oakley_delcert(iph1
->crl_p
);
793 VPTRINIT(iph1
->sig_p
);
800 * status update and establish isakmp sa.
803 base_i3send(iph1
, msg
)
804 struct ph1handle
*iph1
;
810 if (iph1
->status
!= PHASE1ST_MSG3RECEIVED
) {
811 plog(LLV_ERROR
, LOCATION
, NULL
,
812 "status mismatched %d.\n", iph1
->status
);
816 iph1
->status
= PHASE1ST_ESTABLISHED
;
825 * receive from initiator
826 * psk: HDR, SA, Idii, Ni_b
827 * sig: HDR, SA, Idii, Ni_b
828 * rsa: HDR, SA, [HASH(1),] <IDii_b>Pubkey_r, <Ni_b>Pubkey_r
829 * rev: HDR, SA, [HASH(1),] <Ni_b>Pubkey_r, <IDii_b>Ke_i
832 base_r1recv(iph1
, msg
)
833 struct ph1handle
*iph1
;
836 vchar_t
*pbuf
= NULL
;
837 struct isakmp_parse_t
*pa
;
842 if (iph1
->status
!= PHASE1ST_START
) {
843 plog(LLV_ERROR
, LOCATION
, NULL
,
844 "status mismatched %d.\n", iph1
->status
);
848 /* validate the type of next payload */
850 * NOTE: XXX even if multiple VID, we'll silently ignore those.
852 pbuf
= isakmp_parse(msg
);
855 pa
= (struct isakmp_parse_t
*)pbuf
->v
;
857 /* check the position of SA payload */
858 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
859 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
860 "received invalid next payload type %d, "
862 pa
->type
, ISAKMP_NPTYPE_SA
);
865 if (isakmp_p2ph(&iph1
->sa
, pa
->ptr
) < 0)
870 pa
->type
!= ISAKMP_NPTYPE_NONE
;
874 case ISAKMP_NPTYPE_NONCE
:
875 if (isakmp_p2ph(&iph1
->nonce_p
, pa
->ptr
) < 0)
878 case ISAKMP_NPTYPE_ID
:
879 if (isakmp_p2ph(&iph1
->id_p
, pa
->ptr
) < 0)
882 case ISAKMP_NPTYPE_VID
:
883 vid_numeric
= check_vendorid(pa
->ptr
);
885 if (iph1
->rmconf
->nat_traversal
&& natt_vendorid(vid_numeric
))
886 natt_handle_vendorid(iph1
, vid_numeric
);
889 if ((vid_numeric
== VENDORID_FRAG
) &&
890 (vendorid_frag_cap(pa
->ptr
) & VENDORID_FRAG_BASE
))
894 switch (vid_numeric
) {
896 iph1
->mode_cfg
->flags
|=
897 ISAKMP_CFG_VENDORID_XAUTH
;
901 iph1
->mode_cfg
->flags
|=
902 ISAKMP_CFG_VENDORID_UNITY
;
910 if (vid_numeric
== VENDORID_DPD
&& iph1
->rmconf
->dpd
) {
912 plog(LLV_DEBUG
, LOCATION
, NULL
,
913 "remote supports DPD\n");
918 /* don't send information, see ident_r1recv() */
919 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
920 "ignore the packet, "
921 "received unexpecting payload type %d.\n",
927 if (iph1
->nonce_p
== NULL
|| iph1
->id_p
== NULL
) {
928 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
929 "few isakmp message received.\n");
933 /* verify identifier */
934 if (ipsecdoi_checkid1(iph1
) != 0) {
935 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
936 "invalid ID payload.\n");
941 if (NATT_AVAILABLE(iph1
))
942 plog(LLV_INFO
, LOCATION
, iph1
->remote
,
943 "Selected NAT-T version: %s\n",
944 vid_string_by_id(iph1
->natt_options
->version
));
947 /* check SA payload and set approval SA for use */
948 if (ipsecdoi_checkph1proposal(iph1
->sa
, iph1
) < 0) {
949 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
950 "failed to get valid proposal.\n");
951 /* XXX send information */
955 iph1
->status
= PHASE1ST_MSG1RECEIVED
;
965 VPTRINIT(iph1
->nonce_p
);
966 VPTRINIT(iph1
->id_p
);
974 * psk: HDR, SA, Idir, Nr_b
975 * sig: HDR, SA, Idir, Nr_b, [ CR ]
976 * rsa: HDR, SA, <IDir_b>PubKey_i, <Nr_b>PubKey_i
977 * rev: HDR, SA, <Nr_b>PubKey_i, <IDir_b>Ke_r
980 base_r1send(iph1
, msg
)
981 struct ph1handle
*iph1
;
984 struct payload_list
*plist
= NULL
;
987 vchar_t
*vid_natt
= NULL
;
990 vchar_t
*vid_xauth
= NULL
;
991 vchar_t
*vid_unity
= NULL
;
994 vchar_t
*vid_frag
= NULL
;
997 vchar_t
*vid_dpd
= NULL
;
1000 /* validity check */
1001 if (iph1
->status
!= PHASE1ST_MSG1RECEIVED
) {
1002 plog(LLV_ERROR
, LOCATION
, NULL
,
1003 "status mismatched %d.\n", iph1
->status
);
1007 /* set responder's cookie */
1008 isakmp_newcookie((caddr_t
)&iph1
->index
.r_ck
, iph1
->remote
, iph1
->local
);
1010 /* make ID payload into isakmp status */
1011 if (ipsecdoi_setid1(iph1
) < 0)
1014 /* generate NONCE value */
1015 iph1
->nonce
= eay_set_random(iph1
->rmconf
->nonce_size
);
1016 if (iph1
->nonce
== NULL
)
1019 /* set SA payload to reply */
1020 plist
= isakmp_plist_append(plist
, iph1
->sa_ret
, ISAKMP_NPTYPE_SA
);
1022 /* create isakmp ID payload */
1023 plist
= isakmp_plist_append(plist
, iph1
->id
, ISAKMP_NPTYPE_ID
);
1025 /* create isakmp NONCE payload */
1026 plist
= isakmp_plist_append(plist
, iph1
->nonce
, ISAKMP_NPTYPE_NONCE
);
1029 /* has the peer announced nat-t? */
1030 if (NATT_AVAILABLE(iph1
))
1031 vid_natt
= set_vendorid(iph1
->natt_options
->version
);
1033 plist
= isakmp_plist_append(plist
, vid_natt
, ISAKMP_NPTYPE_VID
);
1035 #ifdef ENABLE_HYBRID
1036 if (iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) {
1037 plog (LLV_INFO
, LOCATION
, NULL
, "Adding xauth VID payload.\n");
1038 if ((vid_xauth
= set_vendorid(VENDORID_XAUTH
)) == NULL
) {
1039 plog(LLV_ERROR
, LOCATION
, NULL
,
1040 "Cannot create Xauth vendor ID\n");
1043 plist
= isakmp_plist_append(plist
,
1044 vid_xauth
, ISAKMP_NPTYPE_VID
);
1047 if (iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_UNITY
) {
1048 if ((vid_unity
= set_vendorid(VENDORID_UNITY
)) == NULL
) {
1049 plog(LLV_ERROR
, LOCATION
, NULL
,
1050 "Cannot create Unity vendor ID\n");
1053 plist
= isakmp_plist_append(plist
,
1054 vid_unity
, ISAKMP_NPTYPE_VID
);
1059 * Only send DPD support if remote announced DPD
1060 * and if DPD support is active
1062 if (iph1
->dpd_support
&& iph1
->rmconf
->dpd
) {
1063 if ((vid_dpd
= set_vendorid(VENDORID_DPD
)) == NULL
) {
1064 plog(LLV_ERROR
, LOCATION
, NULL
,
1065 "DPD vendorID construction failed\n");
1067 plist
= isakmp_plist_append(plist
, vid_dpd
,
1073 if (iph1
->rmconf
->ike_frag
) {
1074 if ((vid_frag
= set_vendorid(VENDORID_FRAG
)) == NULL
) {
1075 plog(LLV_ERROR
, LOCATION
, NULL
,
1076 "Frag vendorID construction failed\n");
1078 vid_frag
= isakmp_frag_addcap(vid_frag
,
1079 VENDORID_FRAG_BASE
);
1080 plist
= isakmp_plist_append(plist
,
1081 vid_frag
, ISAKMP_NPTYPE_VID
);
1086 iph1
->sendbuf
= isakmp_plist_set_all (&plist
, iph1
);
1088 #ifdef HAVE_PRINT_ISAKMP_C
1089 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
1092 /* send the packet, add to the schedule to resend */
1093 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
1094 if (isakmp_ph1resend(iph1
) == -1) {
1099 /* the sending message is added to the received-list. */
1100 if (add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
,
1101 PH1_NON_ESP_EXTRA_LEN(iph1
)) == -1) {
1102 plog(LLV_ERROR
, LOCATION
, NULL
,
1103 "failed to add a response packet to the tree.\n");
1107 iph1
->status
= PHASE1ST_MSG1SENT
;
1109 #ifdef ENABLE_VPNCONTROL_PORT
1110 vpncontrol_notify_phase_change(1, FROM_LOCAL
, iph1
, NULL
);
1120 #ifdef ENABLE_HYBRID
1121 if (vid_xauth
!= NULL
)
1123 if (vid_unity
!= NULL
)
1136 VPTRINIT(iph1
->sa_ret
);
1142 * receive from initiator
1143 * psk: HDR, KE, HASH_I
1144 * sig: HDR, KE, [ CR, ] [CERT,] SIG_I
1145 * rsa: HDR, KE, HASH_I
1146 * rev: HDR, <KE>Ke_i, HASH_I
1149 base_r2recv(iph1
, msg
)
1150 struct ph1handle
*iph1
;
1153 vchar_t
*pbuf
= NULL
;
1154 struct isakmp_parse_t
*pa
;
1161 /* validity check */
1162 if (iph1
->status
!= PHASE1ST_MSG1SENT
) {
1163 plog(LLV_ERROR
, LOCATION
, NULL
,
1164 "status mismatched %d.\n", iph1
->status
);
1168 /* validate the type of next payload */
1169 pbuf
= isakmp_parse(msg
);
1173 iph1
->pl_hash
= NULL
;
1175 for (pa
= (struct isakmp_parse_t
*)pbuf
->v
;
1176 pa
->type
!= ISAKMP_NPTYPE_NONE
;
1180 case ISAKMP_NPTYPE_KE
:
1181 if (isakmp_p2ph(&iph1
->dhpub_p
, pa
->ptr
) < 0)
1184 case ISAKMP_NPTYPE_HASH
:
1185 iph1
->pl_hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
1187 case ISAKMP_NPTYPE_CERT
:
1188 if (oakley_savecert(iph1
, pa
->ptr
) < 0)
1191 case ISAKMP_NPTYPE_SIG
:
1192 if (isakmp_p2ph(&iph1
->sig_p
, pa
->ptr
) < 0)
1195 case ISAKMP_NPTYPE_VID
:
1196 (void)check_vendorid(pa
->ptr
);
1200 case ISAKMP_NPTYPE_NATD_DRAFT
:
1201 case ISAKMP_NPTYPE_NATD_RFC
:
1203 case ISAKMP_NPTYPE_NATD_BADDRAFT
:
1205 if (pa
->type
== iph1
->natt_options
->payload_nat_d
)
1207 vchar_t
*natd_received
= NULL
;
1210 if (isakmp_p2ph (&natd_received
, pa
->ptr
) < 0)
1214 iph1
->natt_flags
|= NAT_DETECTED
;
1216 natd_verified
= natt_compare_addr_hash (iph1
,
1217 natd_received
, natd_seq
++);
1219 plog (LLV_INFO
, LOCATION
, NULL
, "NAT-D payload #%d %s\n",
1221 natd_verified
? "verified" : "doesn't match");
1223 vfree (natd_received
);
1226 /* %%%% Be lenient here - some servers send natd payloads */
1227 /* when no nat is detected */
1232 /* don't send information, see ident_r1recv() */
1233 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1234 "ignore the packet, "
1235 "received unexpecting payload type %d.\n",
1241 /* generate DH public value */
1242 if (oakley_dh_generate(iph1
->approval
->dhgrp
,
1243 &iph1
->dhpub
, &iph1
->dhpriv
) < 0)
1246 /* compute sharing secret of DH */
1247 if (oakley_dh_compute(iph1
->approval
->dhgrp
, iph1
->dhpub
,
1248 iph1
->dhpriv
, iph1
->dhpub_p
, &iph1
->dhgxy
) < 0)
1251 /* generate SKEYID */
1252 if (oakley_skeyid(iph1
) < 0)
1256 if (NATT_AVAILABLE(iph1
))
1257 plog (LLV_INFO
, LOCATION
, NULL
, "NAT %s %s%s\n",
1258 iph1
->natt_flags
& NAT_DETECTED
?
1259 "detected:" : "not detected",
1260 iph1
->natt_flags
& NAT_DETECTED_ME
? "ME " : "",
1261 iph1
->natt_flags
& NAT_DETECTED_PEER
? "PEER" : "");
1264 /* payload existency check */
1265 /* validate authentication value */
1266 ptype
= oakley_validate_auth(iph1
);
1269 /* message printed inner oakley_validate_auth() */
1272 EVT_PUSH(iph1
->local
, iph1
->remote
,
1273 EVTT_PEERPH1AUTH_FAILED
, NULL
);
1274 isakmp_info_send_n1(iph1
, ptype
, NULL
);
1278 iph1
->status
= PHASE1ST_MSG2RECEIVED
;
1287 VPTRINIT(iph1
->dhpub_p
);
1288 oakley_delcert(iph1
->cert_p
);
1289 iph1
->cert_p
= NULL
;
1290 oakley_delcert(iph1
->crl_p
);
1292 VPTRINIT(iph1
->sig_p
);
1300 * psk: HDR, KE, HASH_R
1301 * sig: HDR, KE, [CERT,] SIG_R
1302 * rsa: HDR, KE, HASH_R
1303 * rev: HDR, <KE>_Ke_r, HASH_R
1306 base_r2send(iph1
, msg
)
1307 struct ph1handle
*iph1
;
1310 struct payload_list
*plist
= NULL
;
1311 vchar_t
*vid
= NULL
;
1315 /* validity check */
1316 if (iph1
->status
!= PHASE1ST_MSG2RECEIVED
) {
1317 plog(LLV_ERROR
, LOCATION
, NULL
,
1318 "status mismatched %d.\n", iph1
->status
);
1322 /* generate HASH to send */
1323 plog(LLV_DEBUG
, LOCATION
, NULL
, "generate HASH_I\n");
1324 switch (AUTHMETHOD(iph1
)) {
1325 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
1326 #ifdef ENABLE_HYBRID
1327 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1329 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
1330 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
1331 #ifdef ENABLE_HYBRID
1332 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1333 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1335 iph1
->hash
= oakley_ph1hash_common(iph1
, GENERATE
);
1337 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
1338 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
1339 #ifdef ENABLE_HYBRID
1340 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_R
:
1341 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1342 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_R
:
1343 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1346 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
1348 iph1
->hash
= oakley_ph1hash_base_r(iph1
, GENERATE
);
1351 plog(LLV_ERROR
, LOCATION
, NULL
,
1352 "invalid authentication method %d\n",
1353 iph1
->approval
->authmethod
);
1356 if (iph1
->hash
== NULL
)
1359 switch (AUTHMETHOD(iph1
)) {
1360 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
1361 #ifdef ENABLE_HYBRID
1362 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1364 vid
= set_vendorid(iph1
->approval
->vendorid
);
1366 /* create isakmp KE payload */
1367 plist
= isakmp_plist_append(plist
, iph1
->dhpub
, ISAKMP_NPTYPE_KE
);
1369 /* create isakmp HASH payload */
1370 plist
= isakmp_plist_append(plist
, iph1
->hash
, ISAKMP_NPTYPE_HASH
);
1372 /* append vendor id, if needed */
1374 plist
= isakmp_plist_append(plist
, vid
, ISAKMP_NPTYPE_VID
);
1376 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
1377 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
1378 #ifdef ENABLE_HYBRID
1379 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_R
:
1380 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1381 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_R
:
1382 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1384 /* XXX if there is CR or not ? */
1386 if (oakley_getmycert(iph1
) < 0)
1389 if (oakley_getsign(iph1
) < 0)
1392 if (iph1
->cert
&& iph1
->rmconf
->send_cert
)
1395 /* create isakmp KE payload */
1396 plist
= isakmp_plist_append(plist
, iph1
->dhpub
, ISAKMP_NPTYPE_KE
);
1398 /* add CERT payload if there */
1400 plist
= isakmp_plist_append(plist
, iph1
->cert
->pl
, ISAKMP_NPTYPE_CERT
);
1401 /* add SIG payload */
1402 plist
= isakmp_plist_append(plist
, iph1
->sig
, ISAKMP_NPTYPE_SIG
);
1405 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
1409 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
1410 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
1411 #ifdef ENABLE_HYBRID
1412 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1413 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1419 /* generate NAT-D payloads */
1420 if (NATT_AVAILABLE(iph1
)) {
1421 vchar_t
*natd
[2] = { NULL
, NULL
};
1423 plog (LLV_INFO
, LOCATION
, NULL
, "Adding remote and local NAT-D payloads.\n");
1424 if ((natd
[0] = natt_hash_addr (iph1
, iph1
->remote
)) == NULL
) {
1425 plog(LLV_ERROR
, LOCATION
, NULL
,
1426 "NAT-D hashing failed for %s\n", saddr2str(iph1
->remote
));
1430 if ((natd
[1] = natt_hash_addr (iph1
, iph1
->local
)) == NULL
) {
1431 plog(LLV_ERROR
, LOCATION
, NULL
,
1432 "NAT-D hashing failed for %s\n", saddr2str(iph1
->local
));
1437 /* old Apple version sends natd payloads in the wrong order */
1438 if (iph1
->natt_options
->version
== VENDORID_NATT_APPLE
) {
1439 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
1440 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
1444 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
1445 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
1450 iph1
->sendbuf
= isakmp_plist_set_all(&plist
, iph1
);
1452 #ifdef HAVE_PRINT_ISAKMP_C
1453 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
1456 /* send HDR;KE;NONCE to responder */
1457 if (isakmp_send(iph1
, iph1
->sendbuf
) < 0)
1460 /* the sending message is added to the received-list. */
1461 if (add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
,
1462 PH1_NON_ESP_EXTRA_LEN(iph1
)) == -1) {
1463 plog(LLV_ERROR
, LOCATION
, NULL
,
1464 "failed to add a response packet to the tree.\n");
1468 /* generate SKEYIDs & IV & final cipher key */
1469 if (oakley_skeyid_dae(iph1
) < 0)
1471 if (oakley_compute_enckey(iph1
) < 0)
1473 if (oakley_newiv(iph1
) < 0)
1476 /* set encryption flag */
1477 iph1
->flags
|= ISAKMP_FLAG_E
;
1479 iph1
->status
= PHASE1ST_ESTABLISHED
;