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
));
404 ike_session_update_natt_version(iph1
);
408 /* check SA payload and set approval SA for use */
409 if (ipsecdoi_checkph1proposal(satmp
, iph1
) < 0) {
410 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
411 "failed to get valid proposal.\n");
412 /* XXX send information */
415 VPTRINIT(iph1
->sa_ret
);
417 iph1
->status
= PHASE1ST_MSG2RECEIVED
;
419 #ifdef ENABLE_VPNCONTROL_PORT
420 vpncontrol_notify_phase_change(1, FROM_REMOTE
, iph1
, NULL
);
432 VPTRINIT(iph1
->nonce_p
);
433 VPTRINIT(iph1
->id_p
);
441 * psk: HDR, KE, HASH_I
442 * sig: HDR, KE, [ CR, ] [CERT,] SIG_I
443 * rsa: HDR, KE, HASH_I
444 * rev: HDR, <KE>Ke_i, HASH_I
447 base_i2send(iph1
, msg
)
448 struct ph1handle
*iph1
;
451 struct payload_list
*plist
= NULL
;
457 if (iph1
->status
!= PHASE1ST_MSG2RECEIVED
) {
458 plog(LLV_ERROR
, LOCATION
, NULL
,
459 "status mismatched %d.\n", iph1
->status
);
463 /* fix isakmp index */
464 memcpy(&iph1
->index
.r_ck
, &((struct isakmp
*)msg
->v
)->r_ck
,
467 /* generate DH public value */
468 if (oakley_dh_generate(iph1
->approval
->dhgrp
,
469 &iph1
->dhpub
, &iph1
->dhpriv
) < 0)
472 /* generate SKEYID to compute hash if not signature mode */
473 switch (AUTHMETHOD(iph1
)) {
474 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
475 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
477 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_I
:
478 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
479 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_I
:
480 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
481 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_I
:
485 if (oakley_skeyid(iph1
) < 0)
490 /* generate HASH to send */
491 plog(LLV_DEBUG
, LOCATION
, NULL
, "generate HASH_I\n");
492 iph1
->hash
= oakley_ph1hash_base_i(iph1
, GENERATE
);
493 if (iph1
->hash
== NULL
)
495 switch (AUTHMETHOD(iph1
)) {
496 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
498 case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I
:
499 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
500 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_I
:
502 vid
= set_vendorid(iph1
->approval
->vendorid
);
504 /* create isakmp KE payload */
505 plist
= isakmp_plist_append(plist
, iph1
->dhpub
, ISAKMP_NPTYPE_KE
);
507 /* create isakmp HASH payload */
508 plist
= isakmp_plist_append(plist
, iph1
->hash
, ISAKMP_NPTYPE_HASH
);
510 /* append vendor id, if needed */
512 plist
= isakmp_plist_append(plist
, vid
, ISAKMP_NPTYPE_VID
);
514 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
515 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
517 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
518 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_I
:
520 /* XXX if there is CR or not ? */
522 if (oakley_getmycert(iph1
) < 0)
525 if (oakley_getsign(iph1
) < 0)
528 if (iph1
->cert
&& iph1
->rmconf
->send_cert
)
531 /* create isakmp KE payload */
532 plist
= isakmp_plist_append(plist
, iph1
->dhpub
, ISAKMP_NPTYPE_KE
);
534 /* add CERT payload if there */
536 plist
= isakmp_plist_append(plist
, iph1
->cert
->pl
, ISAKMP_NPTYPE_CERT
);
538 /* add SIG payload */
539 plist
= isakmp_plist_append(plist
, iph1
->sig
, ISAKMP_NPTYPE_SIG
);
542 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
546 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
547 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
549 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I
:
550 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I
:
556 /* generate NAT-D payloads */
557 if (NATT_AVAILABLE(iph1
))
559 vchar_t
*natd
[2] = { NULL
, NULL
};
561 plog (LLV_INFO
, LOCATION
, NULL
, "Adding remote and local NAT-D payloads.\n");
562 if ((natd
[0] = natt_hash_addr (iph1
, iph1
->remote
)) == NULL
) {
563 plog(LLV_ERROR
, LOCATION
, NULL
,
564 "NAT-D hashing failed for %s\n", saddr2str(iph1
->remote
));
568 if ((natd
[1] = natt_hash_addr (iph1
, iph1
->local
)) == NULL
) {
569 plog(LLV_ERROR
, LOCATION
, NULL
,
570 "NAT-D hashing failed for %s\n", saddr2str(iph1
->local
));
575 /* old Apple version sends natd payloads in the wrong order */
576 if (iph1
->natt_options
->version
== VENDORID_NATT_APPLE
) {
577 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
578 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
582 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
583 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
588 iph1
->sendbuf
= isakmp_plist_set_all (&plist
, iph1
);
590 #ifdef HAVE_PRINT_ISAKMP_C
591 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
594 /* send the packet, add to the schedule to resend */
595 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
596 if (isakmp_ph1resend(iph1
) == -1)
599 /* the sending message is added to the received-list. */
600 if (add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
,
601 PH1_NON_ESP_EXTRA_LEN(iph1
)) == -1) {
602 plog(LLV_ERROR
, LOCATION
, NULL
,
603 "failed to add a response packet to the tree.\n");
607 iph1
->status
= PHASE1ST_MSG2SENT
;
618 * receive from responder
619 * psk: HDR, KE, HASH_R
620 * sig: HDR, KE, [CERT,] SIG_R
621 * rsa: HDR, KE, HASH_R
622 * rev: HDR, <KE>_Ke_r, HASH_R
625 base_i3recv(iph1
, msg
)
626 struct ph1handle
*iph1
;
629 vchar_t
*pbuf
= NULL
;
630 struct isakmp_parse_t
*pa
;
634 vchar_t
*natd_received
;
635 int natd_seq
= 0, natd_verified
;
639 if (iph1
->status
!= PHASE1ST_MSG2SENT
) {
640 plog(LLV_ERROR
, LOCATION
, NULL
,
641 "status mismatched %d.\n", iph1
->status
);
645 /* validate the type of next payload */
646 pbuf
= isakmp_parse(msg
);
650 for (pa
= (struct isakmp_parse_t
*)pbuf
->v
;
651 pa
->type
!= ISAKMP_NPTYPE_NONE
;
655 case ISAKMP_NPTYPE_KE
:
656 if (isakmp_p2ph(&iph1
->dhpub_p
, pa
->ptr
) < 0)
659 case ISAKMP_NPTYPE_HASH
:
660 iph1
->pl_hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
662 case ISAKMP_NPTYPE_CERT
:
663 if (oakley_savecert(iph1
, pa
->ptr
) < 0)
666 case ISAKMP_NPTYPE_SIG
:
667 if (isakmp_p2ph(&iph1
->sig_p
, pa
->ptr
) < 0)
670 case ISAKMP_NPTYPE_VID
:
671 (void)check_vendorid(pa
->ptr
);
675 case ISAKMP_NPTYPE_NATD_DRAFT
:
676 case ISAKMP_NPTYPE_NATD_RFC
:
678 case ISAKMP_NPTYPE_NATD_BADDRAFT
:
680 if (NATT_AVAILABLE(iph1
) && iph1
->natt_options
&&
681 pa
->type
== iph1
->natt_options
->payload_nat_d
) {
682 natd_received
= NULL
;
683 if (isakmp_p2ph (&natd_received
, pa
->ptr
) < 0)
686 /* set both bits first so that we can clear them
687 upon verifying hashes */
689 iph1
->natt_flags
|= NAT_DETECTED
;
691 /* this function will clear appropriate bits bits
692 from iph1->natt_flags */
693 natd_verified
= natt_compare_addr_hash (iph1
,
694 natd_received
, natd_seq
++);
696 plog (LLV_INFO
, LOCATION
, NULL
, "NAT-D payload #%d %s\n",
698 natd_verified
? "verified" : "doesn't match");
700 vfree (natd_received
);
703 /* %%%% Be lenient here - some servers send natd payloads */
704 /* when no nat is detected */
709 /* don't send information, see ident_r1recv() */
710 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
711 "ignore the packet, "
712 "received unexpecting payload type %d.\n",
719 if (NATT_AVAILABLE(iph1
)) {
720 plog (LLV_INFO
, LOCATION
, NULL
, "NAT %s %s%s\n",
721 iph1
->natt_flags
& NAT_DETECTED
?
722 "detected:" : "not detected",
723 iph1
->natt_flags
& NAT_DETECTED_ME
? "ME " : "",
724 iph1
->natt_flags
& NAT_DETECTED_PEER
? "PEER" : "");
725 if (iph1
->natt_flags
& NAT_DETECTED
)
726 natt_float_ports (iph1
);
730 /* payload existency check */
731 /* validate authentication value */
732 ptype
= oakley_validate_auth(iph1
);
735 /* message printed inner oakley_validate_auth() */
738 EVT_PUSH(iph1
->local
, iph1
->remote
,
739 EVTT_PEERPH1AUTH_FAILED
, NULL
);
740 isakmp_info_send_n1(iph1
, ptype
, NULL
);
744 /* compute sharing secret of DH */
745 if (oakley_dh_compute(iph1
->approval
->dhgrp
, iph1
->dhpub
,
746 iph1
->dhpriv
, iph1
->dhpub_p
, &iph1
->dhgxy
) < 0)
749 /* generate SKEYID to compute hash if signature mode */
750 switch (AUTHMETHOD(iph1
)) {
751 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
752 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
754 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_I
:
755 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I
:
756 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_I
:
757 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I
:
758 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_I
:
760 if (oakley_skeyid(iph1
) < 0)
767 /* generate SKEYIDs & IV & final cipher key */
768 if (oakley_skeyid_dae(iph1
) < 0)
770 if (oakley_compute_enckey(iph1
) < 0)
772 if (oakley_newiv(iph1
) < 0)
775 /* see handler.h about IV synchronization. */
776 memcpy(iph1
->ivm
->iv
->v
, iph1
->ivm
->ive
->v
, iph1
->ivm
->iv
->l
);
778 /* set encryption flag */
779 iph1
->flags
|= ISAKMP_FLAG_E
;
781 iph1
->status
= PHASE1ST_MSG3RECEIVED
;
790 VPTRINIT(iph1
->dhpub_p
);
791 oakley_delcert(iph1
->cert_p
);
793 oakley_delcert(iph1
->crl_p
);
795 VPTRINIT(iph1
->sig_p
);
802 * status update and establish isakmp sa.
805 base_i3send(iph1
, msg
)
806 struct ph1handle
*iph1
;
812 if (iph1
->status
!= PHASE1ST_MSG3RECEIVED
) {
813 plog(LLV_ERROR
, LOCATION
, NULL
,
814 "status mismatched %d.\n", iph1
->status
);
818 iph1
->status
= PHASE1ST_ESTABLISHED
;
827 * receive from initiator
828 * psk: HDR, SA, Idii, Ni_b
829 * sig: HDR, SA, Idii, Ni_b
830 * rsa: HDR, SA, [HASH(1),] <IDii_b>Pubkey_r, <Ni_b>Pubkey_r
831 * rev: HDR, SA, [HASH(1),] <Ni_b>Pubkey_r, <IDii_b>Ke_i
834 base_r1recv(iph1
, msg
)
835 struct ph1handle
*iph1
;
838 vchar_t
*pbuf
= NULL
;
839 struct isakmp_parse_t
*pa
;
844 if (iph1
->status
!= PHASE1ST_START
) {
845 plog(LLV_ERROR
, LOCATION
, NULL
,
846 "status mismatched %d.\n", iph1
->status
);
850 /* validate the type of next payload */
852 * NOTE: XXX even if multiple VID, we'll silently ignore those.
854 pbuf
= isakmp_parse(msg
);
857 pa
= (struct isakmp_parse_t
*)pbuf
->v
;
859 /* check the position of SA payload */
860 if (pa
->type
!= ISAKMP_NPTYPE_SA
) {
861 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
862 "received invalid next payload type %d, "
864 pa
->type
, ISAKMP_NPTYPE_SA
);
867 if (isakmp_p2ph(&iph1
->sa
, pa
->ptr
) < 0)
872 pa
->type
!= ISAKMP_NPTYPE_NONE
;
876 case ISAKMP_NPTYPE_NONCE
:
877 if (isakmp_p2ph(&iph1
->nonce_p
, pa
->ptr
) < 0)
880 case ISAKMP_NPTYPE_ID
:
881 if (isakmp_p2ph(&iph1
->id_p
, pa
->ptr
) < 0)
884 case ISAKMP_NPTYPE_VID
:
885 vid_numeric
= check_vendorid(pa
->ptr
);
887 if (iph1
->rmconf
->nat_traversal
&& natt_vendorid(vid_numeric
))
888 natt_handle_vendorid(iph1
, vid_numeric
);
891 if ((vid_numeric
== VENDORID_FRAG
) &&
892 (vendorid_frag_cap(pa
->ptr
) & VENDORID_FRAG_BASE
))
896 switch (vid_numeric
) {
898 iph1
->mode_cfg
->flags
|=
899 ISAKMP_CFG_VENDORID_XAUTH
;
903 iph1
->mode_cfg
->flags
|=
904 ISAKMP_CFG_VENDORID_UNITY
;
912 if (vid_numeric
== VENDORID_DPD
&& iph1
->rmconf
->dpd
) {
914 plog(LLV_DEBUG
, LOCATION
, NULL
,
915 "remote supports DPD\n");
920 /* don't send information, see ident_r1recv() */
921 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
922 "ignore the packet, "
923 "received unexpecting payload type %d.\n",
929 if (iph1
->nonce_p
== NULL
|| iph1
->id_p
== NULL
) {
930 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
931 "few isakmp message received.\n");
935 /* verify identifier */
936 if (ipsecdoi_checkid1(iph1
) != 0) {
937 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
938 "invalid ID payload.\n");
943 if (NATT_AVAILABLE(iph1
)) {
944 plog(LLV_INFO
, LOCATION
, iph1
->remote
,
945 "Selected NAT-T version: %s\n",
946 vid_string_by_id(iph1
->natt_options
->version
));
947 ike_session_update_natt_version(iph1
);
951 /* check SA payload and set approval SA for use */
952 if (ipsecdoi_checkph1proposal(iph1
->sa
, iph1
) < 0) {
953 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
954 "failed to get valid proposal.\n");
955 /* XXX send information */
959 iph1
->status
= PHASE1ST_MSG1RECEIVED
;
969 VPTRINIT(iph1
->nonce_p
);
970 VPTRINIT(iph1
->id_p
);
978 * psk: HDR, SA, Idir, Nr_b
979 * sig: HDR, SA, Idir, Nr_b, [ CR ]
980 * rsa: HDR, SA, <IDir_b>PubKey_i, <Nr_b>PubKey_i
981 * rev: HDR, SA, <Nr_b>PubKey_i, <IDir_b>Ke_r
984 base_r1send(iph1
, msg
)
985 struct ph1handle
*iph1
;
988 struct payload_list
*plist
= NULL
;
991 vchar_t
*vid_natt
= NULL
;
994 vchar_t
*vid_xauth
= NULL
;
995 vchar_t
*vid_unity
= NULL
;
998 vchar_t
*vid_frag
= NULL
;
1001 vchar_t
*vid_dpd
= NULL
;
1004 /* validity check */
1005 if (iph1
->status
!= PHASE1ST_MSG1RECEIVED
) {
1006 plog(LLV_ERROR
, LOCATION
, NULL
,
1007 "status mismatched %d.\n", iph1
->status
);
1011 /* set responder's cookie */
1012 isakmp_newcookie((caddr_t
)&iph1
->index
.r_ck
, iph1
->remote
, iph1
->local
);
1014 /* make ID payload into isakmp status */
1015 if (ipsecdoi_setid1(iph1
) < 0)
1018 /* generate NONCE value */
1019 iph1
->nonce
= eay_set_random(iph1
->rmconf
->nonce_size
);
1020 if (iph1
->nonce
== NULL
)
1023 /* set SA payload to reply */
1024 plist
= isakmp_plist_append(plist
, iph1
->sa_ret
, ISAKMP_NPTYPE_SA
);
1026 /* create isakmp ID payload */
1027 plist
= isakmp_plist_append(plist
, iph1
->id
, ISAKMP_NPTYPE_ID
);
1029 /* create isakmp NONCE payload */
1030 plist
= isakmp_plist_append(plist
, iph1
->nonce
, ISAKMP_NPTYPE_NONCE
);
1033 /* has the peer announced nat-t? */
1034 if (NATT_AVAILABLE(iph1
))
1035 vid_natt
= set_vendorid(iph1
->natt_options
->version
);
1037 plist
= isakmp_plist_append(plist
, vid_natt
, ISAKMP_NPTYPE_VID
);
1039 #ifdef ENABLE_HYBRID
1040 if (iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_XAUTH
) {
1041 plog (LLV_INFO
, LOCATION
, NULL
, "Adding xauth VID payload.\n");
1042 if ((vid_xauth
= set_vendorid(VENDORID_XAUTH
)) == NULL
) {
1043 plog(LLV_ERROR
, LOCATION
, NULL
,
1044 "Cannot create Xauth vendor ID\n");
1047 plist
= isakmp_plist_append(plist
,
1048 vid_xauth
, ISAKMP_NPTYPE_VID
);
1051 if (iph1
->mode_cfg
->flags
& ISAKMP_CFG_VENDORID_UNITY
) {
1052 if ((vid_unity
= set_vendorid(VENDORID_UNITY
)) == NULL
) {
1053 plog(LLV_ERROR
, LOCATION
, NULL
,
1054 "Cannot create Unity vendor ID\n");
1057 plist
= isakmp_plist_append(plist
,
1058 vid_unity
, ISAKMP_NPTYPE_VID
);
1063 * Only send DPD support if remote announced DPD
1064 * and if DPD support is active
1066 if (iph1
->dpd_support
&& iph1
->rmconf
->dpd
) {
1067 if ((vid_dpd
= set_vendorid(VENDORID_DPD
)) == NULL
) {
1068 plog(LLV_ERROR
, LOCATION
, NULL
,
1069 "DPD vendorID construction failed\n");
1071 plist
= isakmp_plist_append(plist
, vid_dpd
,
1077 if (iph1
->rmconf
->ike_frag
) {
1078 if ((vid_frag
= set_vendorid(VENDORID_FRAG
)) == NULL
) {
1079 plog(LLV_ERROR
, LOCATION
, NULL
,
1080 "Frag vendorID construction failed\n");
1082 vid_frag
= isakmp_frag_addcap(vid_frag
,
1083 VENDORID_FRAG_BASE
);
1084 plist
= isakmp_plist_append(plist
,
1085 vid_frag
, ISAKMP_NPTYPE_VID
);
1090 iph1
->sendbuf
= isakmp_plist_set_all (&plist
, iph1
);
1092 #ifdef HAVE_PRINT_ISAKMP_C
1093 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
1096 /* send the packet, add to the schedule to resend */
1097 iph1
->retry_counter
= iph1
->rmconf
->retry_counter
;
1098 if (isakmp_ph1resend(iph1
) == -1) {
1103 /* the sending message is added to the received-list. */
1104 if (add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
,
1105 PH1_NON_ESP_EXTRA_LEN(iph1
)) == -1) {
1106 plog(LLV_ERROR
, LOCATION
, NULL
,
1107 "failed to add a response packet to the tree.\n");
1111 iph1
->status
= PHASE1ST_MSG1SENT
;
1113 #ifdef ENABLE_VPNCONTROL_PORT
1114 vpncontrol_notify_phase_change(1, FROM_LOCAL
, iph1
, NULL
);
1124 #ifdef ENABLE_HYBRID
1125 if (vid_xauth
!= NULL
)
1127 if (vid_unity
!= NULL
)
1140 VPTRINIT(iph1
->sa_ret
);
1146 * receive from initiator
1147 * psk: HDR, KE, HASH_I
1148 * sig: HDR, KE, [ CR, ] [CERT,] SIG_I
1149 * rsa: HDR, KE, HASH_I
1150 * rev: HDR, <KE>Ke_i, HASH_I
1153 base_r2recv(iph1
, msg
)
1154 struct ph1handle
*iph1
;
1157 vchar_t
*pbuf
= NULL
;
1158 struct isakmp_parse_t
*pa
;
1165 /* validity check */
1166 if (iph1
->status
!= PHASE1ST_MSG1SENT
) {
1167 plog(LLV_ERROR
, LOCATION
, NULL
,
1168 "status mismatched %d.\n", iph1
->status
);
1172 /* validate the type of next payload */
1173 pbuf
= isakmp_parse(msg
);
1177 iph1
->pl_hash
= NULL
;
1179 for (pa
= (struct isakmp_parse_t
*)pbuf
->v
;
1180 pa
->type
!= ISAKMP_NPTYPE_NONE
;
1184 case ISAKMP_NPTYPE_KE
:
1185 if (isakmp_p2ph(&iph1
->dhpub_p
, pa
->ptr
) < 0)
1188 case ISAKMP_NPTYPE_HASH
:
1189 iph1
->pl_hash
= (struct isakmp_pl_hash
*)pa
->ptr
;
1191 case ISAKMP_NPTYPE_CERT
:
1192 if (oakley_savecert(iph1
, pa
->ptr
) < 0)
1195 case ISAKMP_NPTYPE_SIG
:
1196 if (isakmp_p2ph(&iph1
->sig_p
, pa
->ptr
) < 0)
1199 case ISAKMP_NPTYPE_VID
:
1200 (void)check_vendorid(pa
->ptr
);
1204 case ISAKMP_NPTYPE_NATD_DRAFT
:
1205 case ISAKMP_NPTYPE_NATD_RFC
:
1207 case ISAKMP_NPTYPE_NATD_BADDRAFT
:
1209 if (pa
->type
== iph1
->natt_options
->payload_nat_d
)
1211 vchar_t
*natd_received
= NULL
;
1214 if (isakmp_p2ph (&natd_received
, pa
->ptr
) < 0)
1218 iph1
->natt_flags
|= NAT_DETECTED
;
1220 natd_verified
= natt_compare_addr_hash (iph1
,
1221 natd_received
, natd_seq
++);
1223 plog (LLV_INFO
, LOCATION
, NULL
, "NAT-D payload #%d %s\n",
1225 natd_verified
? "verified" : "doesn't match");
1227 vfree (natd_received
);
1230 /* %%%% Be lenient here - some servers send natd payloads */
1231 /* when no nat is detected */
1236 /* don't send information, see ident_r1recv() */
1237 plog(LLV_ERROR
, LOCATION
, iph1
->remote
,
1238 "ignore the packet, "
1239 "received unexpecting payload type %d.\n",
1245 /* generate DH public value */
1246 if (oakley_dh_generate(iph1
->approval
->dhgrp
,
1247 &iph1
->dhpub
, &iph1
->dhpriv
) < 0)
1250 /* compute sharing secret of DH */
1251 if (oakley_dh_compute(iph1
->approval
->dhgrp
, iph1
->dhpub
,
1252 iph1
->dhpriv
, iph1
->dhpub_p
, &iph1
->dhgxy
) < 0)
1255 /* generate SKEYID */
1256 if (oakley_skeyid(iph1
) < 0)
1260 if (NATT_AVAILABLE(iph1
))
1261 plog (LLV_INFO
, LOCATION
, NULL
, "NAT %s %s%s\n",
1262 iph1
->natt_flags
& NAT_DETECTED
?
1263 "detected:" : "not detected",
1264 iph1
->natt_flags
& NAT_DETECTED_ME
? "ME " : "",
1265 iph1
->natt_flags
& NAT_DETECTED_PEER
? "PEER" : "");
1268 /* payload existency check */
1269 /* validate authentication value */
1270 ptype
= oakley_validate_auth(iph1
);
1273 /* message printed inner oakley_validate_auth() */
1276 EVT_PUSH(iph1
->local
, iph1
->remote
,
1277 EVTT_PEERPH1AUTH_FAILED
, NULL
);
1278 isakmp_info_send_n1(iph1
, ptype
, NULL
);
1282 iph1
->status
= PHASE1ST_MSG2RECEIVED
;
1291 VPTRINIT(iph1
->dhpub_p
);
1292 oakley_delcert(iph1
->cert_p
);
1293 iph1
->cert_p
= NULL
;
1294 oakley_delcert(iph1
->crl_p
);
1296 VPTRINIT(iph1
->sig_p
);
1304 * psk: HDR, KE, HASH_R
1305 * sig: HDR, KE, [CERT,] SIG_R
1306 * rsa: HDR, KE, HASH_R
1307 * rev: HDR, <KE>_Ke_r, HASH_R
1310 base_r2send(iph1
, msg
)
1311 struct ph1handle
*iph1
;
1314 struct payload_list
*plist
= NULL
;
1315 vchar_t
*vid
= NULL
;
1319 /* validity check */
1320 if (iph1
->status
!= PHASE1ST_MSG2RECEIVED
) {
1321 plog(LLV_ERROR
, LOCATION
, NULL
,
1322 "status mismatched %d.\n", iph1
->status
);
1326 /* generate HASH to send */
1327 plog(LLV_DEBUG
, LOCATION
, NULL
, "generate HASH_I\n");
1328 switch (AUTHMETHOD(iph1
)) {
1329 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
1330 #ifdef ENABLE_HYBRID
1331 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1333 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
1334 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
1335 #ifdef ENABLE_HYBRID
1336 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1337 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1339 iph1
->hash
= oakley_ph1hash_common(iph1
, GENERATE
);
1341 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
1342 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
1343 #ifdef ENABLE_HYBRID
1344 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_R
:
1345 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1346 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_R
:
1347 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1350 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
1352 iph1
->hash
= oakley_ph1hash_base_r(iph1
, GENERATE
);
1355 plog(LLV_ERROR
, LOCATION
, NULL
,
1356 "invalid authentication method %d\n",
1357 iph1
->approval
->authmethod
);
1360 if (iph1
->hash
== NULL
)
1363 switch (AUTHMETHOD(iph1
)) {
1364 case OAKLEY_ATTR_AUTH_METHOD_PSKEY
:
1365 #ifdef ENABLE_HYBRID
1366 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R
:
1368 vid
= set_vendorid(iph1
->approval
->vendorid
);
1370 /* create isakmp KE payload */
1371 plist
= isakmp_plist_append(plist
, iph1
->dhpub
, ISAKMP_NPTYPE_KE
);
1373 /* create isakmp HASH payload */
1374 plist
= isakmp_plist_append(plist
, iph1
->hash
, ISAKMP_NPTYPE_HASH
);
1376 /* append vendor id, if needed */
1378 plist
= isakmp_plist_append(plist
, vid
, ISAKMP_NPTYPE_VID
);
1380 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG
:
1381 case OAKLEY_ATTR_AUTH_METHOD_RSASIG
:
1382 #ifdef ENABLE_HYBRID
1383 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_R
:
1384 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R
:
1385 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_R
:
1386 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R
:
1388 /* XXX if there is CR or not ? */
1390 if (oakley_getmycert(iph1
) < 0)
1393 if (oakley_getsign(iph1
) < 0)
1396 if (iph1
->cert
&& iph1
->rmconf
->send_cert
)
1399 /* create isakmp KE payload */
1400 plist
= isakmp_plist_append(plist
, iph1
->dhpub
, ISAKMP_NPTYPE_KE
);
1402 /* add CERT payload if there */
1404 plist
= isakmp_plist_append(plist
, iph1
->cert
->pl
, ISAKMP_NPTYPE_CERT
);
1405 /* add SIG payload */
1406 plist
= isakmp_plist_append(plist
, iph1
->sig
, ISAKMP_NPTYPE_SIG
);
1409 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB
:
1413 case OAKLEY_ATTR_AUTH_METHOD_RSAENC
:
1414 case OAKLEY_ATTR_AUTH_METHOD_RSAREV
:
1415 #ifdef ENABLE_HYBRID
1416 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R
:
1417 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R
:
1423 /* generate NAT-D payloads */
1424 if (NATT_AVAILABLE(iph1
)) {
1425 vchar_t
*natd
[2] = { NULL
, NULL
};
1427 plog (LLV_INFO
, LOCATION
, NULL
, "Adding remote and local NAT-D payloads.\n");
1428 if ((natd
[0] = natt_hash_addr (iph1
, iph1
->remote
)) == NULL
) {
1429 plog(LLV_ERROR
, LOCATION
, NULL
,
1430 "NAT-D hashing failed for %s\n", saddr2str(iph1
->remote
));
1434 if ((natd
[1] = natt_hash_addr (iph1
, iph1
->local
)) == NULL
) {
1435 plog(LLV_ERROR
, LOCATION
, NULL
,
1436 "NAT-D hashing failed for %s\n", saddr2str(iph1
->local
));
1441 /* old Apple version sends natd payloads in the wrong order */
1442 if (iph1
->natt_options
->version
== VENDORID_NATT_APPLE
) {
1443 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
1444 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
1448 plist
= isakmp_plist_append(plist
, natd
[0], iph1
->natt_options
->payload_nat_d
);
1449 plist
= isakmp_plist_append(plist
, natd
[1], iph1
->natt_options
->payload_nat_d
);
1454 iph1
->sendbuf
= isakmp_plist_set_all(&plist
, iph1
);
1456 #ifdef HAVE_PRINT_ISAKMP_C
1457 isakmp_printpacket(iph1
->sendbuf
, iph1
->local
, iph1
->remote
, 0);
1460 /* send HDR;KE;NONCE to responder */
1461 if (isakmp_send(iph1
, iph1
->sendbuf
) < 0)
1464 /* the sending message is added to the received-list. */
1465 if (add_recvdpkt(iph1
->remote
, iph1
->local
, iph1
->sendbuf
, msg
,
1466 PH1_NON_ESP_EXTRA_LEN(iph1
)) == -1) {
1467 plog(LLV_ERROR
, LOCATION
, NULL
,
1468 "failed to add a response packet to the tree.\n");
1472 /* generate SKEYIDs & IV & final cipher key */
1473 if (oakley_skeyid_dae(iph1
) < 0)
1475 if (oakley_compute_enckey(iph1
) < 0)
1477 if (oakley_newiv(iph1
) < 0)
1480 /* set encryption flag */
1481 iph1
->flags
|= ISAKMP_FLAG_E
;
1483 iph1
->status
= PHASE1ST_ESTABLISHED
;