1 /* $NetBSD: isakmp_ident.c,v 1.16 2018/05/19 19:23:15 maxv Exp $ */ 2 3 /* Id: isakmp_ident.c,v 1.21 2006/04/06 16:46:08 manubsd Exp */ 4 5 /* 6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 7 * All rights reserved. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 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. 20 * 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 31 * SUCH DAMAGE. 32 */ 33 34 /* Identity Protecion Exchange (Main Mode) */ 35 36 #include "config.h" 37 38 #include <sys/types.h> 39 #include <sys/param.h> 40 41 #include <stdlib.h> 42 #include <stdio.h> 43 #include <string.h> 44 #include <errno.h> 45 #if TIME_WITH_SYS_TIME 46 # include <sys/time.h> 47 # include <time.h> 48 #else 49 # if HAVE_SYS_TIME_H 50 # include <sys/time.h> 51 # else 52 # include <time.h> 53 # endif 54 #endif 55 56 #include "var.h" 57 #include "misc.h" 58 #include "vmbuf.h" 59 #include "plog.h" 60 #include "sockmisc.h" 61 #include "schedule.h" 62 #include "debug.h" 63 64 #include "localconf.h" 65 #include "remoteconf.h" 66 #include "isakmp_var.h" 67 #include "isakmp.h" 68 #include "evt.h" 69 #include "oakley.h" 70 #include "handler.h" 71 #include "ipsec_doi.h" 72 #include "crypto_openssl.h" 73 #include "pfkey.h" 74 #include "isakmp_ident.h" 75 #include "isakmp_inf.h" 76 #include "vendorid.h" 77 78 #ifdef ENABLE_NATT 79 #include "nattraversal.h" 80 #endif 81 #ifdef HAVE_GSSAPI 82 #include "gssapi.h" 83 #endif 84 #ifdef ENABLE_HYBRID 85 #include <resolv.h> 86 #include "isakmp_xauth.h" 87 #include "isakmp_cfg.h" 88 #endif 89 #ifdef ENABLE_FRAG 90 #include "isakmp_frag.h" 91 #endif 92 93 static vchar_t *ident_ir2mx __P((struct ph1handle *)); 94 static vchar_t *ident_ir3mx __P((struct ph1handle *)); 95 static int ident_recv_n __P((struct ph1handle *, struct isakmp_gen *)); 96 97 /* %%% 98 * begin Identity Protection Mode as initiator. 99 */ 100 /* 101 * send to responder 102 * psk: HDR, SA 103 * sig: HDR, SA 104 * rsa: HDR, SA 105 * rev: HDR, SA 106 */ 107 int 108 ident_i1send(iph1, msg) 109 struct ph1handle *iph1; 110 vchar_t *msg; /* must be null */ 111 { 112 struct payload_list *plist = NULL; 113 int error = -1; 114 #ifdef ENABLE_NATT 115 vchar_t *vid_natt[MAX_NATT_VID_COUNT] = { NULL }; 116 int i; 117 #endif 118 #ifdef ENABLE_HYBRID 119 vchar_t *vid_xauth = NULL; 120 vchar_t *vid_unity = NULL; 121 #endif 122 #ifdef ENABLE_FRAG 123 vchar_t *vid_frag = NULL; 124 #endif 125 #ifdef ENABLE_DPD 126 vchar_t *vid_dpd = NULL; 127 #endif 128 /* validity check */ 129 if (msg != NULL) { 130 plog(LLV_ERROR, LOCATION, NULL, 131 "msg has to be NULL in this function.\n"); 132 goto end; 133 } 134 if (iph1->status != PHASE1ST_START) { 135 plog(LLV_ERROR, LOCATION, NULL, 136 "status mismatched %d.\n", iph1->status); 137 goto end; 138 } 139 140 /* create isakmp index */ 141 memset(&iph1->index, 0, sizeof(iph1->index)); 142 isakmp_newcookie((caddr_t)&iph1->index, iph1->remote, iph1->local); 143 144 /* create SA payload for my proposal */ 145 iph1->sa = ipsecdoi_setph1proposal(iph1->rmconf, 146 iph1->rmconf->proposal); 147 if (iph1->sa == NULL) 148 goto end; 149 150 /* set SA payload to propose */ 151 plist = isakmp_plist_append(plist, iph1->sa, ISAKMP_NPTYPE_SA); 152 153 #ifdef ENABLE_NATT 154 /* set VID payload for NAT-T if NAT-T support allowed in the config file */ 155 if (iph1->rmconf->nat_traversal) 156 plist = isakmp_plist_append_natt_vids(plist, vid_natt); 157 #endif 158 #ifdef ENABLE_HYBRID 159 /* Do we need Xauth VID? */ 160 switch (iph1->rmconf->proposal->authmethod) { 161 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_I: 162 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I: 163 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_I: 164 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I: 165 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_I: 166 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I: 167 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I: 168 if ((vid_xauth = set_vendorid(VENDORID_XAUTH)) == NULL) 169 plog(LLV_ERROR, LOCATION, NULL, 170 "Xauth vendor ID generation failed\n"); 171 else 172 plist = isakmp_plist_append(plist, 173 vid_xauth, ISAKMP_NPTYPE_VID); 174 /*FALLTHROUGH*/ 175 case OAKLEY_ATTR_AUTH_METHOD_RSASIG: 176 if ((vid_unity = set_vendorid(VENDORID_UNITY)) == NULL) 177 plog(LLV_ERROR, LOCATION, NULL, 178 "Unity vendor ID generation failed\n"); 179 else 180 plist = isakmp_plist_append(plist, 181 vid_unity, ISAKMP_NPTYPE_VID); 182 break; 183 default: 184 break; 185 } 186 #endif 187 #ifdef ENABLE_FRAG 188 if (iph1->rmconf->ike_frag) { 189 if ((vid_frag = set_vendorid(VENDORID_FRAG)) == NULL) { 190 plog(LLV_ERROR, LOCATION, NULL, 191 "Frag vendorID construction failed\n"); 192 } else { 193 vid_frag = isakmp_frag_addcap(vid_frag, 194 VENDORID_FRAG_IDENT); 195 plist = isakmp_plist_append(plist, 196 vid_frag, ISAKMP_NPTYPE_VID); 197 } 198 } 199 #endif 200 #ifdef ENABLE_DPD 201 if(iph1->rmconf->dpd){ 202 vid_dpd = set_vendorid(VENDORID_DPD); 203 if (vid_dpd != NULL) 204 plist = isakmp_plist_append(plist, vid_dpd, 205 ISAKMP_NPTYPE_VID); 206 } 207 #endif 208 209 iph1->sendbuf = isakmp_plist_set_all (&plist, iph1); 210 211 #ifdef HAVE_PRINT_ISAKMP_C 212 isakmp_printpacket(iph1->sendbuf, iph1->local, iph1->remote, 0); 213 #endif 214 215 /* send the packet, add to the schedule to resend */ 216 if (isakmp_ph1send(iph1) == -1) 217 goto end; 218 219 iph1->status = PHASE1ST_MSG1SENT; 220 221 error = 0; 222 223 end: 224 #ifdef ENABLE_FRAG 225 if (vid_frag) 226 vfree(vid_frag); 227 #endif 228 #ifdef ENABLE_NATT 229 for (i = 0; i < MAX_NATT_VID_COUNT && vid_natt[i] != NULL; i++) 230 vfree(vid_natt[i]); 231 #endif 232 #ifdef ENABLE_HYBRID 233 if (vid_xauth != NULL) 234 vfree(vid_xauth); 235 if (vid_unity != NULL) 236 vfree(vid_unity); 237 #endif 238 #ifdef ENABLE_DPD 239 if (vid_dpd != NULL) 240 vfree(vid_dpd); 241 #endif 242 243 return error; 244 } 245 246 /* 247 * receive from responder 248 * psk: HDR, SA 249 * sig: HDR, SA 250 * rsa: HDR, SA 251 * rev: HDR, SA 252 */ 253 int 254 ident_i2recv(iph1, msg) 255 struct ph1handle *iph1; 256 vchar_t *msg; 257 { 258 vchar_t *pbuf = NULL; 259 struct isakmp_parse_t *pa; 260 vchar_t *satmp = NULL; 261 int error = -1; 262 263 /* validity check */ 264 if (iph1->status != PHASE1ST_MSG1SENT) { 265 plog(LLV_ERROR, LOCATION, NULL, 266 "status mismatched %d.\n", iph1->status); 267 goto end; 268 } 269 270 /* validate the type of next payload */ 271 /* 272 * NOTE: RedCreek(as responder) attaches N[responder-lifetime] here, 273 * if proposal-lifetime > lifetime-redcreek-wants. 274 * (see doi-08 4.5.4) 275 * => According to the seciton 4.6.3 in RFC 2407, This is illegal. 276 * NOTE: we do not really care about ordering of VID and N. 277 * does it matters? 278 * NOTE: even if there's multiple VID/N, we'll ignore them. 279 */ 280 pbuf = isakmp_parse(msg); 281 if (pbuf == NULL) 282 goto end; 283 pa = (struct isakmp_parse_t *)pbuf->v; 284 285 /* SA payload is fixed postion */ 286 if (pa->type != ISAKMP_NPTYPE_SA) { 287 plog(LLV_ERROR, LOCATION, iph1->remote, 288 "received invalid next payload type %d, " 289 "expecting %d.\n", 290 pa->type, ISAKMP_NPTYPE_SA); 291 goto end; 292 } 293 if (isakmp_p2ph(&satmp, pa->ptr) < 0) 294 goto end; 295 pa++; 296 297 for (/*nothing*/; 298 pa->type != ISAKMP_NPTYPE_NONE; 299 pa++) { 300 301 switch (pa->type) { 302 case ISAKMP_NPTYPE_VID: 303 handle_vendorid(iph1, pa->ptr); 304 break; 305 default: 306 /* don't send information, see ident_r1recv() */ 307 plog(LLV_ERROR, LOCATION, iph1->remote, 308 "ignore the packet, " 309 "received unexpecting payload type %d.\n", 310 pa->type); 311 goto end; 312 } 313 } 314 315 #ifdef ENABLE_NATT 316 if (NATT_AVAILABLE(iph1)) 317 plog(LLV_INFO, LOCATION, iph1->remote, 318 "Selected NAT-T version: %s\n", 319 vid_string_by_id(iph1->natt_options->version)); 320 #endif 321 322 /* check SA payload and set approval SA for use */ 323 if (ipsecdoi_checkph1proposal(satmp, iph1) < 0) { 324 plog(LLV_ERROR, LOCATION, iph1->remote, 325 "failed to get valid proposal.\n"); 326 /* XXX send information */ 327 goto end; 328 } 329 VPTRINIT(iph1->sa_ret); 330 331 iph1->status = PHASE1ST_MSG2RECEIVED; 332 333 error = 0; 334 335 end: 336 if (pbuf) 337 vfree(pbuf); 338 if (satmp) 339 vfree(satmp); 340 return error; 341 } 342 343 /* 344 * send to responder 345 * psk: HDR, KE, Ni 346 * sig: HDR, KE, Ni 347 * gssapi: HDR, KE, Ni, GSSi 348 * rsa: HDR, KE, [ HASH(1), ] <IDi1_b>PubKey_r, <Ni_b>PubKey_r 349 * rev: HDR, [ HASH(1), ] <Ni_b>Pubkey_r, <KE_b>Ke_i, 350 * <IDi1_b>Ke_i, [<<Cert-I_b>Ke_i] 351 */ 352 int 353 ident_i2send(iph1, msg) 354 struct ph1handle *iph1; 355 vchar_t *msg; 356 { 357 int error = -1; 358 359 /* validity check */ 360 if (iph1->status != PHASE1ST_MSG2RECEIVED) { 361 plog(LLV_ERROR, LOCATION, NULL, 362 "status mismatched %d.\n", iph1->status); 363 goto end; 364 } 365 366 /* fix isakmp index */ 367 memcpy(&iph1->index.r_ck, &((struct isakmp *)msg->v)->r_ck, 368 sizeof(cookie_t)); 369 370 /* generate DH public value */ 371 if (oakley_dh_generate(iph1->approval->dhgrp, 372 &iph1->dhpub, &iph1->dhpriv) < 0) 373 goto end; 374 375 /* generate NONCE value */ 376 iph1->nonce = eay_set_random(iph1->rmconf->nonce_size); 377 if (iph1->nonce == NULL) 378 goto end; 379 380 #ifdef HAVE_GSSAPI 381 if (iph1->approval->authmethod == OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB && 382 gssapi_get_itoken(iph1, NULL) < 0) 383 goto end; 384 #endif 385 386 /* create buffer to send isakmp payload */ 387 iph1->sendbuf = ident_ir2mx(iph1); 388 if (iph1->sendbuf == NULL) 389 goto end; 390 391 #ifdef HAVE_PRINT_ISAKMP_C 392 isakmp_printpacket(iph1->sendbuf, iph1->local, iph1->remote, 0); 393 #endif 394 395 /* send the packet, add to the schedule to resend */ 396 if (isakmp_ph1send(iph1) == -1) 397 goto end; 398 399 /* the sending message is added to the received-list. */ 400 if (add_recvdpkt(iph1->remote, iph1->local, iph1->sendbuf, msg) == -1) { 401 plog(LLV_ERROR , LOCATION, NULL, 402 "failed to add a response packet to the tree.\n"); 403 goto end; 404 } 405 406 iph1->status = PHASE1ST_MSG2SENT; 407 408 error = 0; 409 410 end: 411 return error; 412 } 413 414 /* 415 * receive from responder 416 * psk: HDR, KE, Nr 417 * sig: HDR, KE, Nr [, CR ] 418 * gssapi: HDR, KE, Nr, GSSr 419 * rsa: HDR, KE, <IDr1_b>PubKey_i, <Nr_b>PubKey_i 420 * rev: HDR, <Nr_b>PubKey_i, <KE_b>Ke_r, <IDr1_b>Ke_r, 421 */ 422 int 423 ident_i3recv(iph1, msg) 424 struct ph1handle *iph1; 425 vchar_t *msg; 426 { 427 vchar_t *pbuf = NULL; 428 struct isakmp_parse_t *pa; 429 int error = -1; 430 #ifdef HAVE_GSSAPI 431 vchar_t *gsstoken = NULL; 432 #endif 433 #ifdef ENABLE_NATT 434 vchar_t *natd_received; 435 int natd_seq = 0, natd_verified; 436 #endif 437 438 /* validity check */ 439 if (iph1->status != PHASE1ST_MSG2SENT) { 440 plog(LLV_ERROR, LOCATION, NULL, 441 "status mismatched %d.\n", iph1->status); 442 goto end; 443 } 444 445 /* validate the type of next payload */ 446 pbuf = isakmp_parse(msg); 447 if (pbuf == NULL) 448 goto end; 449 450 for (pa = (struct isakmp_parse_t *)pbuf->v; 451 pa->type != ISAKMP_NPTYPE_NONE; 452 pa++) { 453 454 switch (pa->type) { 455 case ISAKMP_NPTYPE_KE: 456 if (isakmp_p2ph(&iph1->dhpub_p, pa->ptr) < 0) 457 goto end; 458 break; 459 case ISAKMP_NPTYPE_NONCE: 460 if (isakmp_p2ph(&iph1->nonce_p, pa->ptr) < 0) 461 goto end; 462 break; 463 case ISAKMP_NPTYPE_VID: 464 handle_vendorid(iph1, pa->ptr); 465 break; 466 case ISAKMP_NPTYPE_CR: 467 if (oakley_savecr(iph1, pa->ptr) < 0) 468 goto end; 469 break; 470 #ifdef HAVE_GSSAPI 471 case ISAKMP_NPTYPE_GSS: 472 if (isakmp_p2ph(&gsstoken, pa->ptr) < 0) 473 goto end; 474 gssapi_save_received_token(iph1, gsstoken); 475 break; 476 #endif 477 478 #ifdef ENABLE_NATT 479 case ISAKMP_NPTYPE_NATD_DRAFT: 480 case ISAKMP_NPTYPE_NATD_RFC: 481 if (NATT_AVAILABLE(iph1) && iph1->natt_options != NULL && 482 pa->type == iph1->natt_options->payload_nat_d) { 483 natd_received = NULL; 484 if (isakmp_p2ph (&natd_received, pa->ptr) < 0) 485 goto end; 486 487 /* set both bits first so that we can clear them 488 upon verifying hashes */ 489 if (natd_seq == 0) 490 iph1->natt_flags |= NAT_DETECTED; 491 492 /* this function will clear appropriate bits bits 493 from iph1->natt_flags */ 494 natd_verified = natt_compare_addr_hash (iph1, 495 natd_received, natd_seq++); 496 497 plog (LLV_INFO, LOCATION, NULL, "NAT-D payload #%d %s\n", 498 natd_seq - 1, 499 natd_verified ? "verified" : "doesn't match"); 500 501 vfree (natd_received); 502 break; 503 } 504 /* passthrough to default... */ 505 #endif 506 507 default: 508 /* don't send information, see ident_r1recv() */ 509 plog(LLV_ERROR, LOCATION, iph1->remote, 510 "ignore the packet, " 511 "received unexpecting payload type %d.\n", 512 pa->type); 513 goto end; 514 } 515 } 516 517 #ifdef ENABLE_NATT 518 if (NATT_AVAILABLE(iph1)) { 519 plog (LLV_INFO, LOCATION, NULL, "NAT %s %s%s\n", 520 iph1->natt_flags & NAT_DETECTED ? 521 "detected:" : "not detected", 522 iph1->natt_flags & NAT_DETECTED_ME ? "ME " : "", 523 iph1->natt_flags & NAT_DETECTED_PEER ? "PEER" : ""); 524 if (iph1->natt_flags & NAT_DETECTED) 525 natt_float_ports (iph1); 526 } 527 #endif 528 529 /* payload existency check */ 530 if (iph1->dhpub_p == NULL || iph1->nonce_p == NULL) { 531 plog(LLV_ERROR, LOCATION, iph1->remote, 532 "few isakmp message received.\n"); 533 goto end; 534 } 535 536 if (oakley_checkcr(iph1) < 0) { 537 /* Ignore this error in order to be interoperability. */ 538 ; 539 } 540 541 iph1->status = PHASE1ST_MSG3RECEIVED; 542 543 error = 0; 544 545 end: 546 #ifdef HAVE_GSSAPI 547 if (gsstoken) 548 vfree(gsstoken); 549 #endif 550 if (pbuf) 551 vfree(pbuf); 552 if (error) { 553 VPTRINIT(iph1->dhpub_p); 554 VPTRINIT(iph1->nonce_p); 555 VPTRINIT(iph1->id_p); 556 VPTRINIT(iph1->cr_p); 557 } 558 559 return error; 560 } 561 562 /* 563 * send to responder 564 * psk: HDR*, IDi1, HASH_I 565 * sig: HDR*, IDi1, [ CR, ] [ CERT, ] SIG_I 566 * gssapi: HDR*, IDi1, < Gssi(n) | HASH_I > 567 * rsa: HDR*, HASH_I 568 * rev: HDR*, HASH_I 569 */ 570 int 571 ident_i3send(iph1, msg0) 572 struct ph1handle *iph1; 573 vchar_t *msg0; 574 { 575 int error = -1; 576 int dohash = 1; 577 #ifdef HAVE_GSSAPI 578 int len; 579 #endif 580 581 /* validity check */ 582 if (iph1->status != PHASE1ST_MSG3RECEIVED) { 583 plog(LLV_ERROR, LOCATION, NULL, 584 "status mismatched %d.\n", iph1->status); 585 goto end; 586 } 587 588 /* compute sharing secret of DH */ 589 if (oakley_dh_compute(iph1->approval->dhgrp, iph1->dhpub, 590 iph1->dhpriv, iph1->dhpub_p, &iph1->dhgxy) < 0) 591 goto end; 592 593 /* generate SKEYIDs & IV & final cipher key */ 594 if (oakley_skeyid(iph1) < 0) 595 goto end; 596 if (oakley_skeyid_dae(iph1) < 0) 597 goto end; 598 if (oakley_compute_enckey(iph1) < 0) 599 goto end; 600 if (oakley_newiv(iph1) < 0) 601 goto end; 602 603 /* make ID payload into isakmp status */ 604 if (ipsecdoi_setid1(iph1) < 0) 605 goto end; 606 607 #ifdef HAVE_GSSAPI 608 if (iph1->approval->authmethod == OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB && 609 gssapi_more_tokens(iph1)) { 610 plog(LLV_DEBUG, LOCATION, NULL, "calling get_itoken\n"); 611 if (gssapi_get_itoken(iph1, &len) < 0) 612 goto end; 613 if (len != 0) 614 dohash = 0; 615 } 616 #endif 617 618 /* generate HASH to send */ 619 if (dohash) { 620 iph1->hash = oakley_ph1hash_common(iph1, GENERATE); 621 if (iph1->hash == NULL) 622 goto end; 623 } else 624 iph1->hash = NULL; 625 626 /* set encryption flag */ 627 iph1->flags |= ISAKMP_FLAG_E; 628 629 /* create HDR;ID;HASH payload */ 630 iph1->sendbuf = ident_ir3mx(iph1); 631 if (iph1->sendbuf == NULL) 632 goto end; 633 634 /* send the packet, add to the schedule to resend */ 635 if (isakmp_ph1send(iph1) == -1) 636 goto end; 637 638 /* the sending message is added to the received-list. */ 639 if (add_recvdpkt(iph1->remote, iph1->local, iph1->sendbuf, msg0) == -1) { 640 plog(LLV_ERROR , LOCATION, NULL, 641 "failed to add a response packet to the tree.\n"); 642 goto end; 643 } 644 645 /* see handler.h about IV synchronization. */ 646 memcpy(iph1->ivm->ive->v, iph1->ivm->iv->v, iph1->ivm->iv->l); 647 648 iph1->status = PHASE1ST_MSG3SENT; 649 650 error = 0; 651 652 end: 653 return error; 654 } 655 656 /* 657 * receive from responder 658 * psk: HDR*, IDr1, HASH_R 659 * sig: HDR*, IDr1, [ CERT, ] SIG_R 660 * gssapi: HDR*, IDr1, < GSSr(n) | HASH_R > 661 * rsa: HDR*, HASH_R 662 * rev: HDR*, HASH_R 663 */ 664 int 665 ident_i4recv(iph1, msg0) 666 struct ph1handle *iph1; 667 vchar_t *msg0; 668 { 669 vchar_t *pbuf = NULL; 670 struct isakmp_parse_t *pa; 671 vchar_t *msg = NULL; 672 int error = -1; 673 int type; 674 #ifdef HAVE_GSSAPI 675 vchar_t *gsstoken = NULL; 676 #endif 677 678 /* validity check */ 679 if (iph1->status != PHASE1ST_MSG3SENT) { 680 plog(LLV_ERROR, LOCATION, NULL, 681 "status mismatched %d.\n", iph1->status); 682 goto end; 683 } 684 685 /* decrypting */ 686 if (!ISSET(((struct isakmp *)msg0->v)->flags, ISAKMP_FLAG_E)) { 687 plog(LLV_ERROR, LOCATION, iph1->remote, 688 "ignore the packet, " 689 "expecting the packet encrypted.\n"); 690 goto end; 691 } 692 msg = oakley_do_decrypt(iph1, msg0, iph1->ivm->iv, iph1->ivm->ive); 693 if (msg == NULL) 694 goto end; 695 696 /* validate the type of next payload */ 697 pbuf = isakmp_parse(msg); 698 if (pbuf == NULL) 699 goto end; 700 701 iph1->pl_hash = NULL; 702 703 for (pa = (struct isakmp_parse_t *)pbuf->v; 704 pa->type != ISAKMP_NPTYPE_NONE; 705 pa++) { 706 707 switch (pa->type) { 708 case ISAKMP_NPTYPE_ID: 709 if (isakmp_p2ph(&iph1->id_p, pa->ptr) < 0) 710 goto end; 711 break; 712 case ISAKMP_NPTYPE_HASH: 713 iph1->pl_hash = (struct isakmp_pl_hash *)pa->ptr; 714 break; 715 case ISAKMP_NPTYPE_CERT: 716 if (oakley_savecert(iph1, pa->ptr) < 0) 717 goto end; 718 break; 719 case ISAKMP_NPTYPE_SIG: 720 if (isakmp_p2ph(&iph1->sig_p, pa->ptr) < 0) 721 goto end; 722 break; 723 #ifdef HAVE_GSSAPI 724 case ISAKMP_NPTYPE_GSS: 725 if (isakmp_p2ph(&gsstoken, pa->ptr) < 0) 726 goto end; 727 gssapi_save_received_token(iph1, gsstoken); 728 break; 729 #endif 730 case ISAKMP_NPTYPE_VID: 731 handle_vendorid(iph1, pa->ptr); 732 break; 733 case ISAKMP_NPTYPE_N: 734 ident_recv_n(iph1, pa->ptr); 735 break; 736 default: 737 /* don't send information, see ident_r1recv() */ 738 plog(LLV_ERROR, LOCATION, iph1->remote, 739 "ignore the packet, " 740 "received unexpecting payload type %d.\n", 741 pa->type); 742 goto end; 743 } 744 } 745 746 /* payload existency check */ 747 748 /* verify identifier */ 749 if (ipsecdoi_checkid1(iph1) != 0) { 750 plog(LLV_ERROR, LOCATION, iph1->remote, 751 "invalid ID payload.\n"); 752 goto end; 753 } 754 755 /* validate authentication value */ 756 #ifdef HAVE_GSSAPI 757 if (gsstoken == NULL) { 758 #endif 759 type = oakley_validate_auth(iph1); 760 if (type != 0) { 761 if (type == -1) { 762 /* msg printed inner oakley_validate_auth() */ 763 goto end; 764 } 765 evt_phase1(iph1, EVT_PHASE1_AUTH_FAILED, NULL); 766 isakmp_info_send_n1(iph1, type, NULL); 767 goto end; 768 } 769 #ifdef HAVE_GSSAPI 770 } 771 #endif 772 773 /* 774 * XXX: Should we do compare two addresses, ph1handle's and ID 775 * payload's. 776 */ 777 778 plog(LLV_DEBUG, LOCATION, iph1->remote, "peer's ID:"); 779 plogdump(LLV_DEBUG, iph1->id_p->v, iph1->id_p->l); 780 781 /* see handler.h about IV synchronization. */ 782 memcpy(iph1->ivm->iv->v, iph1->ivm->ive->v, iph1->ivm->ive->l); 783 784 /* 785 * If we got a GSS token, we need to this roundtrip again. 786 */ 787 #ifdef HAVE_GSSAPI 788 iph1->status = gsstoken != 0 ? PHASE1ST_MSG3RECEIVED : 789 PHASE1ST_MSG4RECEIVED; 790 #else 791 iph1->status = PHASE1ST_MSG4RECEIVED; 792 #endif 793 794 error = 0; 795 796 end: 797 if (pbuf) 798 vfree(pbuf); 799 if (msg) 800 vfree(msg); 801 #ifdef HAVE_GSSAPI 802 if (gsstoken) 803 vfree(gsstoken); 804 #endif 805 806 if (error) { 807 VPTRINIT(iph1->id_p); 808 VPTRINIT(iph1->cert_p); 809 VPTRINIT(iph1->crl_p); 810 VPTRINIT(iph1->sig_p); 811 } 812 813 return error; 814 } 815 816 /* 817 * status update and establish isakmp sa. 818 */ 819 int 820 ident_i4send(iph1, msg) 821 struct ph1handle *iph1; 822 vchar_t *msg; 823 { 824 int error = -1; 825 826 /* validity check */ 827 if (iph1->status != PHASE1ST_MSG4RECEIVED) { 828 plog(LLV_ERROR, LOCATION, NULL, 829 "status mismatched %d.\n", iph1->status); 830 goto end; 831 } 832 833 /* see handler.h about IV synchronization. */ 834 memcpy(iph1->ivm->iv->v, iph1->ivm->ive->v, iph1->ivm->iv->l); 835 836 iph1->status = PHASE1ST_ESTABLISHED; 837 838 error = 0; 839 840 end: 841 return error; 842 } 843 844 /* 845 * receive from initiator 846 * psk: HDR, SA 847 * sig: HDR, SA 848 * rsa: HDR, SA 849 * rev: HDR, SA 850 */ 851 int 852 ident_r1recv(iph1, msg) 853 struct ph1handle *iph1; 854 vchar_t *msg; 855 { 856 vchar_t *pbuf = NULL; 857 struct isakmp_parse_t *pa; 858 int error = -1; 859 int vid_numeric; 860 861 /* validity check */ 862 if (iph1->status != PHASE1ST_START) { 863 plog(LLV_ERROR, LOCATION, NULL, 864 "status mismatched %d.\n", iph1->status); 865 goto end; 866 } 867 868 /* validate the type of next payload */ 869 /* 870 * NOTE: XXX even if multiple VID, we'll silently ignore those. 871 */ 872 pbuf = isakmp_parse(msg); 873 if (pbuf == NULL) 874 goto end; 875 pa = (struct isakmp_parse_t *)pbuf->v; 876 877 /* check the position of SA payload */ 878 if (pa->type != ISAKMP_NPTYPE_SA) { 879 plog(LLV_ERROR, LOCATION, iph1->remote, 880 "received invalid next payload type %d, " 881 "expecting %d.\n", 882 pa->type, ISAKMP_NPTYPE_SA); 883 goto end; 884 } 885 if (isakmp_p2ph(&iph1->sa, pa->ptr) < 0) 886 goto end; 887 pa++; 888 889 for (/*nothing*/; 890 pa->type != ISAKMP_NPTYPE_NONE; 891 pa++) { 892 893 switch (pa->type) { 894 case ISAKMP_NPTYPE_VID: 895 vid_numeric = handle_vendorid(iph1, pa->ptr); 896 #ifdef ENABLE_FRAG 897 if ((vid_numeric == VENDORID_FRAG) && 898 (vendorid_frag_cap(pa->ptr) & VENDORID_FRAG_IDENT)) 899 iph1->frag = 1; 900 #endif 901 break; 902 default: 903 /* 904 * We don't send information to the peer even 905 * if we received malformed packet. Because we 906 * can't distinguish the malformed packet and 907 * the re-sent packet. And we do same behavior 908 * when we expect encrypted packet. 909 */ 910 plog(LLV_ERROR, LOCATION, iph1->remote, 911 "ignore the packet, " 912 "received unexpecting payload type %d.\n", 913 pa->type); 914 goto end; 915 } 916 } 917 918 #ifdef ENABLE_NATT 919 if (NATT_AVAILABLE(iph1)) 920 plog(LLV_INFO, LOCATION, iph1->remote, 921 "Selected NAT-T version: %s\n", 922 vid_string_by_id(iph1->natt_options->version)); 923 #endif 924 925 /* check SA payload and set approval SA for use */ 926 if (ipsecdoi_checkph1proposal(iph1->sa, iph1) < 0) { 927 plog(LLV_ERROR, LOCATION, iph1->remote, 928 "failed to get valid proposal.\n"); 929 /* XXX send information */ 930 goto end; 931 } 932 933 iph1->status = PHASE1ST_MSG1RECEIVED; 934 935 error = 0; 936 937 end: 938 if (pbuf) 939 vfree(pbuf); 940 if (error) { 941 VPTRINIT(iph1->sa); 942 } 943 944 return error; 945 } 946 947 /* 948 * send to initiator 949 * psk: HDR, SA 950 * sig: HDR, SA 951 * rsa: HDR, SA 952 * rev: HDR, SA 953 */ 954 int 955 ident_r1send(iph1, msg) 956 struct ph1handle *iph1; 957 vchar_t *msg; 958 { 959 struct payload_list *plist = NULL; 960 int error = -1; 961 vchar_t *gss_sa = NULL; 962 #ifdef HAVE_GSSAPI 963 int free_gss_sa = 0; 964 #endif 965 #ifdef ENABLE_NATT 966 vchar_t *vid_natt = NULL; 967 #endif 968 #ifdef ENABLE_HYBRID 969 vchar_t *vid_xauth = NULL; 970 vchar_t *vid_unity = NULL; 971 #endif 972 #ifdef ENABLE_DPD 973 vchar_t *vid_dpd = NULL; 974 #endif 975 #ifdef ENABLE_FRAG 976 vchar_t *vid_frag = NULL; 977 #endif 978 979 /* validity check */ 980 if (iph1->status != PHASE1ST_MSG1RECEIVED) { 981 plog(LLV_ERROR, LOCATION, NULL, 982 "status mismatched %d.\n", iph1->status); 983 goto end; 984 } 985 986 /* set responder's cookie */ 987 isakmp_newcookie((caddr_t)&iph1->index.r_ck, iph1->remote, iph1->local); 988 989 #ifdef HAVE_GSSAPI 990 if (iph1->approval->gssid != NULL) { 991 gss_sa = ipsecdoi_setph1proposal(iph1->rmconf, iph1->approval); 992 if (gss_sa != iph1->sa_ret) 993 free_gss_sa = 1; 994 } else 995 #endif 996 gss_sa = iph1->sa_ret; 997 998 /* set SA payload to reply */ 999 plist = isakmp_plist_append(plist, gss_sa, ISAKMP_NPTYPE_SA); 1000 1001 #ifdef ENABLE_HYBRID 1002 if (iph1->mode_cfg->flags & ISAKMP_CFG_VENDORID_XAUTH) { 1003 plog (LLV_INFO, LOCATION, NULL, "Adding xauth VID payload.\n"); 1004 if ((vid_xauth = set_vendorid(VENDORID_XAUTH)) == NULL) { 1005 plog(LLV_ERROR, LOCATION, NULL, 1006 "Cannot create Xauth vendor ID\n"); 1007 goto end; 1008 } 1009 plist = isakmp_plist_append(plist, 1010 vid_xauth, ISAKMP_NPTYPE_VID); 1011 } 1012 1013 if (iph1->mode_cfg->flags & ISAKMP_CFG_VENDORID_UNITY) { 1014 if ((vid_unity = set_vendorid(VENDORID_UNITY)) == NULL) { 1015 plog(LLV_ERROR, LOCATION, NULL, 1016 "Cannot create Unity vendor ID\n"); 1017 goto end; 1018 } 1019 plist = isakmp_plist_append(plist, 1020 vid_unity, ISAKMP_NPTYPE_VID); 1021 } 1022 #endif 1023 #ifdef ENABLE_NATT 1024 /* Has the peer announced NAT-T? */ 1025 if (NATT_AVAILABLE(iph1)) 1026 vid_natt = set_vendorid(iph1->natt_options->version); 1027 1028 if (vid_natt) 1029 plist = isakmp_plist_append(plist, vid_natt, ISAKMP_NPTYPE_VID); 1030 #endif 1031 #ifdef ENABLE_DPD 1032 if (iph1->dpd_support) { 1033 vid_dpd = set_vendorid(VENDORID_DPD); 1034 if (vid_dpd != NULL) 1035 plist = isakmp_plist_append(plist, vid_dpd, ISAKMP_NPTYPE_VID); 1036 } 1037 #endif 1038 #ifdef ENABLE_FRAG 1039 if (iph1->frag) { 1040 vid_frag = set_vendorid(VENDORID_FRAG); 1041 if (vid_frag != NULL) 1042 vid_frag = isakmp_frag_addcap(vid_frag, 1043 VENDORID_FRAG_IDENT); 1044 if (vid_frag == NULL) 1045 plog(LLV_ERROR, LOCATION, NULL, 1046 "Frag vendorID construction failed\n"); 1047 else 1048 plist = isakmp_plist_append(plist, 1049 vid_frag, ISAKMP_NPTYPE_VID); 1050 } 1051 #endif 1052 1053 iph1->sendbuf = isakmp_plist_set_all (&plist, iph1); 1054 1055 #ifdef HAVE_PRINT_ISAKMP_C 1056 isakmp_printpacket(iph1->sendbuf, iph1->local, iph1->remote, 0); 1057 #endif 1058 1059 /* send the packet, add to the schedule to resend */ 1060 if (isakmp_ph1send(iph1) == -1) 1061 goto end; 1062 1063 /* the sending message is added to the received-list. */ 1064 if (add_recvdpkt(iph1->remote, iph1->local, iph1->sendbuf, msg) == -1) { 1065 plog(LLV_ERROR , LOCATION, NULL, 1066 "failed to add a response packet to the tree.\n"); 1067 goto end; 1068 } 1069 1070 iph1->status = PHASE1ST_MSG1SENT; 1071 1072 error = 0; 1073 1074 end: 1075 #ifdef HAVE_GSSAPI 1076 if (free_gss_sa) 1077 vfree(gss_sa); 1078 #endif 1079 #ifdef ENABLE_NATT 1080 if (vid_natt) 1081 vfree(vid_natt); 1082 #endif 1083 #ifdef ENABLE_HYBRID 1084 if (vid_xauth != NULL) 1085 vfree(vid_xauth); 1086 if (vid_unity != NULL) 1087 vfree(vid_unity); 1088 #endif 1089 #ifdef ENABLE_DPD 1090 if (vid_dpd != NULL) 1091 vfree(vid_dpd); 1092 #endif 1093 #ifdef ENABLE_FRAG 1094 if (vid_frag != NULL) 1095 vfree(vid_frag); 1096 #endif 1097 1098 return error; 1099 } 1100 1101 /* 1102 * receive from initiator 1103 * psk: HDR, KE, Ni 1104 * sig: HDR, KE, Ni 1105 * gssapi: HDR, KE, Ni, GSSi 1106 * rsa: HDR, KE, [ HASH(1), ] <IDi1_b>PubKey_r, <Ni_b>PubKey_r 1107 * rev: HDR, [ HASH(1), ] <Ni_b>Pubkey_r, <KE_b>Ke_i, 1108 * <IDi1_b>Ke_i, [<<Cert-I_b>Ke_i] 1109 */ 1110 int 1111 ident_r2recv(iph1, msg) 1112 struct ph1handle *iph1; 1113 vchar_t *msg; 1114 { 1115 vchar_t *pbuf = NULL; 1116 struct isakmp_parse_t *pa; 1117 int error = -1; 1118 #ifdef HAVE_GSSAPI 1119 vchar_t *gsstoken = NULL; 1120 #endif 1121 #ifdef ENABLE_NATT 1122 int natd_seq = 0; 1123 #endif 1124 1125 /* validity check */ 1126 if (iph1->status != PHASE1ST_MSG1SENT) { 1127 plog(LLV_ERROR, LOCATION, NULL, 1128 "status mismatched %d.\n", iph1->status); 1129 goto end; 1130 } 1131 1132 /* validate the type of next payload */ 1133 pbuf = isakmp_parse(msg); 1134 if (pbuf == NULL) 1135 goto end; 1136 1137 for (pa = (struct isakmp_parse_t *)pbuf->v; 1138 pa->type != ISAKMP_NPTYPE_NONE; 1139 pa++) { 1140 switch (pa->type) { 1141 case ISAKMP_NPTYPE_KE: 1142 if (isakmp_p2ph(&iph1->dhpub_p, pa->ptr) < 0) 1143 goto end; 1144 break; 1145 case ISAKMP_NPTYPE_NONCE: 1146 if (isakmp_p2ph(&iph1->nonce_p, pa->ptr) < 0) 1147 goto end; 1148 break; 1149 case ISAKMP_NPTYPE_VID: 1150 handle_vendorid(iph1, pa->ptr); 1151 break; 1152 case ISAKMP_NPTYPE_CR: 1153 plog(LLV_WARNING, LOCATION, iph1->remote, 1154 "CR received, ignore it. " 1155 "It should be in other exchange.\n"); 1156 break; 1157 #ifdef HAVE_GSSAPI 1158 case ISAKMP_NPTYPE_GSS: 1159 if (isakmp_p2ph(&gsstoken, pa->ptr) < 0) 1160 goto end; 1161 gssapi_save_received_token(iph1, gsstoken); 1162 break; 1163 #endif 1164 1165 #ifdef ENABLE_NATT 1166 case ISAKMP_NPTYPE_NATD_DRAFT: 1167 case ISAKMP_NPTYPE_NATD_RFC: 1168 if (NATT_AVAILABLE(iph1) && iph1->natt_options != NULL && 1169 pa->type == iph1->natt_options->payload_nat_d) 1170 { 1171 vchar_t *natd_received = NULL; 1172 int natd_verified; 1173 1174 if (isakmp_p2ph (&natd_received, pa->ptr) < 0) 1175 goto end; 1176 1177 if (natd_seq == 0) 1178 iph1->natt_flags |= NAT_DETECTED; 1179 1180 natd_verified = natt_compare_addr_hash (iph1, 1181 natd_received, natd_seq++); 1182 1183 plog (LLV_INFO, LOCATION, NULL, "NAT-D payload #%d %s\n", 1184 natd_seq - 1, 1185 natd_verified ? "verified" : "doesn't match"); 1186 1187 vfree (natd_received); 1188 break; 1189 } 1190 /* passthrough to default... */ 1191 #endif 1192 1193 default: 1194 /* don't send information, see ident_r1recv() */ 1195 plog(LLV_ERROR, LOCATION, iph1->remote, 1196 "ignore the packet, " 1197 "received unexpecting payload type %d.\n", 1198 pa->type); 1199 goto end; 1200 } 1201 } 1202 1203 #ifdef ENABLE_NATT 1204 if (NATT_AVAILABLE(iph1)) 1205 plog (LLV_INFO, LOCATION, NULL, "NAT %s %s%s\n", 1206 iph1->natt_flags & NAT_DETECTED ? 1207 "detected:" : "not detected", 1208 iph1->natt_flags & NAT_DETECTED_ME ? "ME " : "", 1209 iph1->natt_flags & NAT_DETECTED_PEER ? "PEER" : ""); 1210 #endif 1211 1212 /* payload existency check */ 1213 if (iph1->dhpub_p == NULL || iph1->nonce_p == NULL) { 1214 plog(LLV_ERROR, LOCATION, iph1->remote, 1215 "few isakmp message received.\n"); 1216 goto end; 1217 } 1218 1219 iph1->status = PHASE1ST_MSG2RECEIVED; 1220 1221 error = 0; 1222 1223 end: 1224 if (pbuf) 1225 vfree(pbuf); 1226 #ifdef HAVE_GSSAPI 1227 if (gsstoken) 1228 vfree(gsstoken); 1229 #endif 1230 1231 if (error) { 1232 VPTRINIT(iph1->dhpub_p); 1233 VPTRINIT(iph1->nonce_p); 1234 VPTRINIT(iph1->id_p); 1235 } 1236 1237 return error; 1238 } 1239 1240 /* 1241 * send to initiator 1242 * psk: HDR, KE, Nr 1243 * sig: HDR, KE, Nr [, CR ] 1244 * gssapi: HDR, KE, Nr, GSSr 1245 * rsa: HDR, KE, <IDr1_b>PubKey_i, <Nr_b>PubKey_i 1246 * rev: HDR, <Nr_b>PubKey_i, <KE_b>Ke_r, <IDr1_b>Ke_r, 1247 */ 1248 int 1249 ident_r2send(iph1, msg) 1250 struct ph1handle *iph1; 1251 vchar_t *msg; 1252 { 1253 int error = -1; 1254 1255 /* validity check */ 1256 if (iph1->status != PHASE1ST_MSG2RECEIVED) { 1257 plog(LLV_ERROR, LOCATION, NULL, 1258 "status mismatched %d.\n", iph1->status); 1259 goto end; 1260 } 1261 1262 /* generate DH public value */ 1263 if (oakley_dh_generate(iph1->approval->dhgrp, 1264 &iph1->dhpub, &iph1->dhpriv) < 0) 1265 goto end; 1266 1267 /* generate NONCE value */ 1268 iph1->nonce = eay_set_random(RMCONF_NONCE_SIZE(iph1->rmconf)); 1269 if (iph1->nonce == NULL) 1270 goto end; 1271 1272 #ifdef HAVE_GSSAPI 1273 if (iph1->approval->authmethod == OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB) 1274 gssapi_get_rtoken(iph1, NULL); 1275 #endif 1276 1277 /* create HDR;KE;NONCE payload */ 1278 iph1->sendbuf = ident_ir2mx(iph1); 1279 if (iph1->sendbuf == NULL) 1280 goto end; 1281 1282 #ifdef HAVE_PRINT_ISAKMP_C 1283 isakmp_printpacket(iph1->sendbuf, iph1->local, iph1->remote, 0); 1284 #endif 1285 1286 /* send the packet, add to the schedule to resend */ 1287 if (isakmp_ph1send(iph1) == -1) 1288 goto end; 1289 1290 /* the sending message is added to the received-list. */ 1291 if (add_recvdpkt(iph1->remote, iph1->local, iph1->sendbuf, msg) == -1) { 1292 plog(LLV_ERROR , LOCATION, NULL, 1293 "failed to add a response packet to the tree.\n"); 1294 goto end; 1295 } 1296 1297 /* compute sharing secret of DH */ 1298 if (oakley_dh_compute(iph1->approval->dhgrp, iph1->dhpub, 1299 iph1->dhpriv, iph1->dhpub_p, &iph1->dhgxy) < 0) 1300 goto end; 1301 1302 /* generate SKEYIDs & IV & final cipher key */ 1303 if (oakley_skeyid(iph1) < 0) 1304 goto end; 1305 if (oakley_skeyid_dae(iph1) < 0) 1306 goto end; 1307 if (oakley_compute_enckey(iph1) < 0) 1308 goto end; 1309 if (oakley_newiv(iph1) < 0) 1310 goto end; 1311 1312 iph1->status = PHASE1ST_MSG2SENT; 1313 1314 error = 0; 1315 1316 end: 1317 return error; 1318 } 1319 1320 /* 1321 * receive from initiator 1322 * psk: HDR*, IDi1, HASH_I 1323 * sig: HDR*, IDi1, [ CR, ] [ CERT, ] SIG_I 1324 * gssapi: HDR*, [ IDi1, ] < GSSi(n) | HASH_I > 1325 * rsa: HDR*, HASH_I 1326 * rev: HDR*, HASH_I 1327 */ 1328 int 1329 ident_r3recv(iph1, msg0) 1330 struct ph1handle *iph1; 1331 vchar_t *msg0; 1332 { 1333 vchar_t *msg = NULL; 1334 vchar_t *pbuf = NULL; 1335 struct isakmp_parse_t *pa; 1336 int error = -1; 1337 int type; 1338 #ifdef HAVE_GSSAPI 1339 vchar_t *gsstoken = NULL; 1340 #endif 1341 1342 /* validity check */ 1343 if (iph1->status != PHASE1ST_MSG2SENT) { 1344 plog(LLV_ERROR, LOCATION, NULL, 1345 "status mismatched %d.\n", iph1->status); 1346 goto end; 1347 } 1348 1349 /* decrypting */ 1350 if (!ISSET(((struct isakmp *)msg0->v)->flags, ISAKMP_FLAG_E)) { 1351 plog(LLV_ERROR, LOCATION, iph1->remote, 1352 "reject the packet, " 1353 "expecting the packet encrypted.\n"); 1354 goto end; 1355 } 1356 msg = oakley_do_decrypt(iph1, msg0, iph1->ivm->iv, iph1->ivm->ive); 1357 if (msg == NULL) 1358 goto end; 1359 1360 /* validate the type of next payload */ 1361 pbuf = isakmp_parse(msg); 1362 if (pbuf == NULL) 1363 goto end; 1364 1365 iph1->pl_hash = NULL; 1366 1367 for (pa = (struct isakmp_parse_t *)pbuf->v; 1368 pa->type != ISAKMP_NPTYPE_NONE; 1369 pa++) { 1370 1371 switch (pa->type) { 1372 case ISAKMP_NPTYPE_ID: 1373 if (isakmp_p2ph(&iph1->id_p, pa->ptr) < 0) 1374 goto end; 1375 if (resolveph1rmconf(iph1) < 0) 1376 goto end; 1377 break; 1378 case ISAKMP_NPTYPE_HASH: 1379 iph1->pl_hash = (struct isakmp_pl_hash *)pa->ptr; 1380 break; 1381 case ISAKMP_NPTYPE_CR: 1382 if (oakley_savecr(iph1, pa->ptr) < 0) 1383 goto end; 1384 break; 1385 case ISAKMP_NPTYPE_CERT: 1386 if (oakley_savecert(iph1, pa->ptr) < 0) 1387 goto end; 1388 break; 1389 case ISAKMP_NPTYPE_SIG: 1390 if (isakmp_p2ph(&iph1->sig_p, pa->ptr) < 0) 1391 goto end; 1392 break; 1393 #ifdef HAVE_GSSAPI 1394 case ISAKMP_NPTYPE_GSS: 1395 if (isakmp_p2ph(&gsstoken, pa->ptr) < 0) 1396 goto end; 1397 gssapi_save_received_token(iph1, gsstoken); 1398 break; 1399 #endif 1400 case ISAKMP_NPTYPE_VID: 1401 handle_vendorid(iph1, pa->ptr); 1402 break; 1403 case ISAKMP_NPTYPE_N: 1404 ident_recv_n(iph1, pa->ptr); 1405 break; 1406 default: 1407 /* don't send information, see ident_r1recv() */ 1408 plog(LLV_ERROR, LOCATION, iph1->remote, 1409 "ignore the packet, " 1410 "received unexpecting payload type %d.\n", 1411 pa->type); 1412 goto end; 1413 } 1414 } 1415 1416 /* payload existency check */ 1417 /* XXX same as ident_i4recv(), should be merged. */ 1418 { 1419 int ng = 0; 1420 1421 switch (iph1->approval->authmethod) { 1422 case OAKLEY_ATTR_AUTH_METHOD_PSKEY: 1423 #ifdef ENABLE_HYBRID 1424 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R: 1425 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R: 1426 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_R: 1427 #endif 1428 if (iph1->id_p == NULL || iph1->pl_hash == NULL) 1429 ng++; 1430 break; 1431 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG: 1432 case OAKLEY_ATTR_AUTH_METHOD_RSASIG: 1433 #ifdef ENABLE_HYBRID 1434 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R: 1435 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_R: 1436 #endif 1437 if (iph1->id_p == NULL || iph1->sig_p == NULL) 1438 ng++; 1439 break; 1440 case OAKLEY_ATTR_AUTH_METHOD_RSAENC: 1441 case OAKLEY_ATTR_AUTH_METHOD_RSAREV: 1442 #ifdef ENABLE_HYBRID 1443 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R: 1444 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R: 1445 #endif 1446 if (iph1->pl_hash == NULL) 1447 ng++; 1448 break; 1449 #ifdef HAVE_GSSAPI 1450 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB: 1451 if (gsstoken == NULL && iph1->pl_hash == NULL) 1452 ng++; 1453 break; 1454 #endif 1455 default: 1456 plog(LLV_ERROR, LOCATION, iph1->remote, 1457 "invalid authmethod %d why ?\n", 1458 iph1->approval->authmethod); 1459 goto end; 1460 } 1461 if (ng) { 1462 plog(LLV_ERROR, LOCATION, iph1->remote, 1463 "few isakmp message received.\n"); 1464 goto end; 1465 } 1466 } 1467 1468 /* verify identifier */ 1469 if (ipsecdoi_checkid1(iph1) != 0) { 1470 plog(LLV_ERROR, LOCATION, iph1->remote, 1471 "invalid ID payload.\n"); 1472 goto end; 1473 } 1474 1475 /* validate authentication value */ 1476 #ifdef HAVE_GSSAPI 1477 if (gsstoken == NULL) { 1478 #endif 1479 type = oakley_validate_auth(iph1); 1480 if (type != 0) { 1481 if (type == -1) { 1482 /* msg printed inner oakley_validate_auth() */ 1483 goto end; 1484 } 1485 evt_phase1(iph1, EVT_PHASE1_AUTH_FAILED, NULL); 1486 isakmp_info_send_n1(iph1, type, NULL); 1487 goto end; 1488 } 1489 #ifdef HAVE_GSSAPI 1490 } 1491 #endif 1492 1493 if (oakley_checkcr(iph1) < 0) { 1494 /* Ignore this error in order to be interoperability. */ 1495 ; 1496 } 1497 1498 /* 1499 * XXX: Should we do compare two addresses, ph1handle's and ID 1500 * payload's. 1501 */ 1502 1503 plog(LLV_DEBUG, LOCATION, iph1->remote, "peer's ID\n"); 1504 plogdump(LLV_DEBUG, iph1->id_p->v, iph1->id_p->l); 1505 1506 /* see handler.h about IV synchronization. */ 1507 memcpy(iph1->ivm->iv->v, iph1->ivm->ive->v, iph1->ivm->ive->l); 1508 1509 #ifdef HAVE_GSSAPI 1510 iph1->status = gsstoken != NULL ? PHASE1ST_MSG2RECEIVED : 1511 PHASE1ST_MSG3RECEIVED; 1512 #else 1513 iph1->status = PHASE1ST_MSG3RECEIVED; 1514 #endif 1515 1516 error = 0; 1517 1518 end: 1519 if (pbuf) 1520 vfree(pbuf); 1521 if (msg) 1522 vfree(msg); 1523 #ifdef HAVE_GSSAPI 1524 if (gsstoken) 1525 vfree(gsstoken); 1526 #endif 1527 1528 if (error) { 1529 VPTRINIT(iph1->id_p); 1530 VPTRINIT(iph1->cert_p); 1531 VPTRINIT(iph1->crl_p); 1532 VPTRINIT(iph1->sig_p); 1533 VPTRINIT(iph1->cr_p); 1534 } 1535 1536 return error; 1537 } 1538 1539 /* 1540 * send to initiator 1541 * psk: HDR*, IDr1, HASH_R 1542 * sig: HDR*, IDr1, [ CERT, ] SIG_R 1543 * gssapi: HDR*, IDr1, < GSSr(n) | HASH_R > 1544 * rsa: HDR*, HASH_R 1545 * rev: HDR*, HASH_R 1546 */ 1547 int 1548 ident_r3send(iph1, msg) 1549 struct ph1handle *iph1; 1550 vchar_t *msg; 1551 { 1552 int error = -1; 1553 int dohash = 1; 1554 #ifdef HAVE_GSSAPI 1555 int len; 1556 #endif 1557 1558 /* validity check */ 1559 if (iph1->status != PHASE1ST_MSG3RECEIVED) { 1560 plog(LLV_ERROR, LOCATION, NULL, 1561 "status mismatched %d.\n", iph1->status); 1562 goto end; 1563 } 1564 1565 /* make ID payload into isakmp status */ 1566 if (ipsecdoi_setid1(iph1) < 0) 1567 goto end; 1568 1569 #ifdef HAVE_GSSAPI 1570 if (iph1->approval->authmethod == OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB && 1571 gssapi_more_tokens(iph1)) { 1572 gssapi_get_rtoken(iph1, &len); 1573 if (len != 0) 1574 dohash = 0; 1575 } 1576 #endif 1577 1578 if (dohash) { 1579 /* generate HASH to send */ 1580 plog(LLV_DEBUG, LOCATION, NULL, "generate HASH_R\n"); 1581 iph1->hash = oakley_ph1hash_common(iph1, GENERATE); 1582 if (iph1->hash == NULL) 1583 goto end; 1584 } else 1585 iph1->hash = NULL; 1586 1587 /* set encryption flag */ 1588 iph1->flags |= ISAKMP_FLAG_E; 1589 1590 /* create HDR;ID;HASH payload */ 1591 iph1->sendbuf = ident_ir3mx(iph1); 1592 if (iph1->sendbuf == NULL) 1593 goto end; 1594 1595 /* send HDR;ID;HASH to responder */ 1596 if (isakmp_send(iph1, iph1->sendbuf) < 0) 1597 goto end; 1598 1599 /* the sending message is added to the received-list. */ 1600 if (add_recvdpkt(iph1->remote, iph1->local, iph1->sendbuf, msg) == -1) { 1601 plog(LLV_ERROR , LOCATION, NULL, 1602 "failed to add a response packet to the tree.\n"); 1603 goto end; 1604 } 1605 1606 /* see handler.h about IV synchronization. */ 1607 memcpy(iph1->ivm->ive->v, iph1->ivm->iv->v, iph1->ivm->iv->l); 1608 1609 iph1->status = PHASE1ST_ESTABLISHED; 1610 1611 error = 0; 1612 1613 end: 1614 1615 return error; 1616 } 1617 1618 /* 1619 * This is used in main mode for: 1620 * initiator's 3rd exchange send to responder 1621 * psk: HDR, KE, Ni 1622 * sig: HDR, KE, Ni 1623 * rsa: HDR, KE, [ HASH(1), ] <IDi1_b>PubKey_r, <Ni_b>PubKey_r 1624 * rev: HDR, [ HASH(1), ] <Ni_b>Pubkey_r, <KE_b>Ke_i, 1625 * <IDi1_b>Ke_i, [<<Cert-I_b>Ke_i] 1626 * responders 2nd exchnage send to initiator 1627 * psk: HDR, KE, Nr 1628 * sig: HDR, KE, Nr [, CR ] 1629 * rsa: HDR, KE, <IDr1_b>PubKey_i, <Nr_b>PubKey_i 1630 * rev: HDR, <Nr_b>PubKey_i, <KE_b>Ke_r, <IDr1_b>Ke_r, 1631 */ 1632 static vchar_t * 1633 ident_ir2mx(iph1) 1634 struct ph1handle *iph1; 1635 { 1636 vchar_t *buf = 0; 1637 struct payload_list *plist = NULL; 1638 vchar_t *vid = NULL; 1639 int error = -1; 1640 #ifdef HAVE_GSSAPI 1641 vchar_t *gsstoken = NULL; 1642 #endif 1643 #ifdef ENABLE_NATT 1644 vchar_t *natd[2] = { NULL, NULL }; 1645 #endif 1646 1647 #ifdef HAVE_GSSAPI 1648 if (iph1->approval->authmethod == OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB) { 1649 if (gssapi_get_token_to_send(iph1, &gsstoken) < 0) { 1650 plog(LLV_ERROR, LOCATION, NULL, 1651 "Failed to get gssapi token.\n"); 1652 goto end; 1653 } 1654 } 1655 #endif 1656 1657 /* create isakmp KE payload */ 1658 plist = isakmp_plist_append(plist, iph1->dhpub, ISAKMP_NPTYPE_KE); 1659 1660 /* create isakmp NONCE payload */ 1661 plist = isakmp_plist_append(plist, iph1->nonce, ISAKMP_NPTYPE_NONCE); 1662 1663 #ifdef HAVE_GSSAPI 1664 if (iph1->approval->authmethod == OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB) 1665 plist = isakmp_plist_append(plist, gsstoken, ISAKMP_NPTYPE_GSS); 1666 #endif 1667 1668 /* append vendor id, if needed */ 1669 if (vid) 1670 plist = isakmp_plist_append(plist, vid, ISAKMP_NPTYPE_VID); 1671 1672 /* create CR if need */ 1673 if (iph1->side == RESPONDER && 1674 oakley_needcr(iph1->approval->authmethod)) 1675 plist = oakley_append_cr(plist, iph1); 1676 1677 #ifdef ENABLE_NATT 1678 /* generate and append NAT-D payloads */ 1679 if (NATT_AVAILABLE(iph1) && iph1->status == PHASE1ST_MSG2RECEIVED) 1680 { 1681 if ((natd[0] = natt_hash_addr (iph1, iph1->remote)) == NULL) { 1682 plog(LLV_ERROR, LOCATION, NULL, 1683 "NAT-D hashing failed for %s\n", saddr2str(iph1->remote)); 1684 goto end; 1685 } 1686 1687 if ((natd[1] = natt_hash_addr (iph1, iph1->local)) == NULL) { 1688 plog(LLV_ERROR, LOCATION, NULL, 1689 "NAT-D hashing failed for %s\n", saddr2str(iph1->local)); 1690 goto end; 1691 } 1692 1693 plog (LLV_INFO, LOCATION, NULL, "Adding remote and local NAT-D payloads.\n"); 1694 plist = isakmp_plist_append(plist, natd[0], iph1->natt_options->payload_nat_d); 1695 plist = isakmp_plist_append(plist, natd[1], iph1->natt_options->payload_nat_d); 1696 } 1697 #endif 1698 1699 buf = isakmp_plist_set_all (&plist, iph1); 1700 1701 error = 0; 1702 1703 end: 1704 if (error && buf != NULL) { 1705 vfree(buf); 1706 buf = NULL; 1707 } 1708 #ifdef HAVE_GSSAPI 1709 if (gsstoken) 1710 vfree(gsstoken); 1711 #endif 1712 if (vid) 1713 vfree(vid); 1714 1715 #ifdef ENABLE_NATT 1716 if (natd[0]) 1717 vfree(natd[0]); 1718 if (natd[1]) 1719 vfree(natd[1]); 1720 #endif 1721 1722 return buf; 1723 } 1724 1725 /* 1726 * This is used in main mode for: 1727 * initiator's 4th exchange send to responder 1728 * psk: HDR*, IDi1, HASH_I 1729 * sig: HDR*, IDi1, [ CR, ] [ CERT, ] SIG_I 1730 * gssapi: HDR*, [ IDi1, ] < GSSi(n) | HASH_I > 1731 * rsa: HDR*, HASH_I 1732 * rev: HDR*, HASH_I 1733 * responders 3rd exchnage send to initiator 1734 * psk: HDR*, IDr1, HASH_R 1735 * sig: HDR*, IDr1, [ CERT, ] SIG_R 1736 * gssapi: HDR*, [ IDr1, ] < GSSr(n) | HASH_R > 1737 * rsa: HDR*, HASH_R 1738 * rev: HDR*, HASH_R 1739 */ 1740 static vchar_t * 1741 ident_ir3mx(iph1) 1742 struct ph1handle *iph1; 1743 { 1744 struct payload_list *plist = NULL; 1745 vchar_t *buf = NULL, *new = NULL; 1746 int need_cert = 0; 1747 int error = -1; 1748 #ifdef HAVE_GSSAPI 1749 vchar_t *gsstoken = NULL; 1750 vchar_t *gsshash = NULL; 1751 #endif 1752 1753 switch (iph1->approval->authmethod) { 1754 case OAKLEY_ATTR_AUTH_METHOD_PSKEY: 1755 #ifdef ENABLE_HYBRID 1756 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_I: 1757 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R: 1758 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I: 1759 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_I: 1760 #endif 1761 /* create isakmp ID payload */ 1762 plist = isakmp_plist_append(plist, iph1->id, ISAKMP_NPTYPE_ID); 1763 1764 /* create isakmp HASH payload */ 1765 plist = isakmp_plist_append(plist, iph1->hash, ISAKMP_NPTYPE_HASH); 1766 break; 1767 case OAKLEY_ATTR_AUTH_METHOD_DSSSIG: 1768 case OAKLEY_ATTR_AUTH_METHOD_RSASIG: 1769 #ifdef ENABLE_HYBRID 1770 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R: 1771 case OAKLEY_ATTR_AUTH_METHOD_HYBRID_DSS_R: 1772 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I: 1773 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R: 1774 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_I: 1775 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_DSSSIG_R: 1776 #endif 1777 if (oakley_getmycert(iph1) < 0) 1778 goto end; 1779 1780 if (oakley_getsign(iph1) < 0) 1781 goto end; 1782 1783 if (iph1->cert != NULL && iph1->rmconf->send_cert) 1784 need_cert = 1; 1785 1786 /* add ID payload */ 1787 plist = isakmp_plist_append(plist, iph1->id, ISAKMP_NPTYPE_ID); 1788 1789 /* add CERT payload if there */ 1790 if (need_cert) 1791 plist = isakmp_plist_append(plist, iph1->cert, 1792 ISAKMP_NPTYPE_CERT); 1793 /* add SIG payload */ 1794 plist = isakmp_plist_append(plist, iph1->sig, ISAKMP_NPTYPE_SIG); 1795 1796 /* create isakmp CR payload */ 1797 if (iph1->side == INITIATOR && 1798 oakley_needcr(iph1->approval->authmethod)) 1799 plist = oakley_append_cr(plist, iph1); 1800 break; 1801 #ifdef HAVE_GSSAPI 1802 case OAKLEY_ATTR_AUTH_METHOD_GSSAPI_KRB: 1803 if (iph1->hash != NULL) { 1804 gsshash = gssapi_wraphash(iph1); 1805 if (gsshash == NULL) 1806 goto end; 1807 } else { 1808 if (gssapi_get_token_to_send(iph1, &gsstoken) < 0) { 1809 plog(LLV_ERROR, LOCATION, NULL, 1810 "Failed to get gssapi token.\n"); 1811 goto end; 1812 } 1813 } 1814 1815 if (!gssapi_id_sent(iph1)) { 1816 /* create isakmp ID payload */ 1817 plist = isakmp_plist_append(plist, iph1->id, ISAKMP_NPTYPE_ID); 1818 gssapi_set_id_sent(iph1); 1819 } 1820 1821 if (iph1->hash != NULL) 1822 /* create isakmp HASH payload */ 1823 plist = isakmp_plist_append(plist, gsshash, ISAKMP_NPTYPE_HASH); 1824 else 1825 plist = isakmp_plist_append(plist, gsstoken, ISAKMP_NPTYPE_GSS); 1826 break; 1827 #endif 1828 case OAKLEY_ATTR_AUTH_METHOD_RSAENC: 1829 case OAKLEY_ATTR_AUTH_METHOD_RSAREV: 1830 #ifdef ENABLE_HYBRID 1831 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I: 1832 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R: 1833 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I: 1834 case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R: 1835 #endif 1836 plog(LLV_ERROR, LOCATION, NULL, 1837 "not supported authentication type %d\n", 1838 iph1->approval->authmethod); 1839 goto end; 1840 default: 1841 plog(LLV_ERROR, LOCATION, NULL, 1842 "invalid authentication type %d\n", 1843 iph1->approval->authmethod); 1844 goto end; 1845 } 1846 1847 buf = isakmp_plist_set_all (&plist, iph1); 1848 1849 #ifdef HAVE_PRINT_ISAKMP_C 1850 isakmp_printpacket(buf, iph1->local, iph1->remote, 1); 1851 #endif 1852 1853 /* encoding */ 1854 new = oakley_do_encrypt(iph1, buf, iph1->ivm->ive, iph1->ivm->iv); 1855 if (new == NULL) 1856 goto end; 1857 1858 vfree(buf); 1859 1860 buf = new; 1861 1862 error = 0; 1863 1864 end: 1865 #ifdef HAVE_GSSAPI 1866 if (gsstoken) 1867 vfree(gsstoken); 1868 #endif 1869 if (error && buf != NULL) { 1870 vfree(buf); 1871 buf = NULL; 1872 } 1873 1874 return buf; 1875 } 1876 1877 /* 1878 * handle a notification payload inside identity exchange. 1879 * called only when the packet has been verified to be encrypted. 1880 */ 1881 static int 1882 ident_recv_n(iph1, gen) 1883 struct ph1handle *iph1; 1884 struct isakmp_gen *gen; 1885 { 1886 struct isakmp_pl_n *notify = (struct isakmp_pl_n *) gen; 1887 u_int type; 1888 1889 type = ntohs(notify->type); 1890 switch (type) { 1891 case ISAKMP_NTYPE_INITIAL_CONTACT: 1892 iph1->initial_contact_received = TRUE; 1893 break; 1894 default: 1895 isakmp_log_notify(iph1, notify, "identity exchange"); 1896 break; 1897 } 1898 return 0; 1899 } 1900 1901