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