]>
Commit | Line | Data |
---|---|---|
d1e348cf A |
1 | /* $NetBSD: isakmp_agg.c,v 1.9 2006/09/30 21:49:37 manu Exp $ */ |
2 | ||
3 | /* Id: isakmp_agg.c,v 1.28 2006/04/06 16:46:08 manubsd Exp */ | |
52b7d2ce A |
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 | /* Aggressive Exchange (Aggressive 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 | ||
d1e348cf A |
64 | #ifdef ENABLE_HYBRID |
65 | #include <resolv.h> | |
66 | #endif | |
67 | ||
65c25746 | 68 | #include "fsm.h" |
52b7d2ce A |
69 | #include "localconf.h" |
70 | #include "remoteconf.h" | |
71 | #include "isakmp_var.h" | |
72 | #include "isakmp.h" | |
52b7d2ce A |
73 | #include "oakley.h" |
74 | #include "handler.h" | |
75 | #include "ipsec_doi.h" | |
76 | #include "crypto_openssl.h" | |
77 | #include "pfkey.h" | |
78 | #include "isakmp_agg.h" | |
79 | #include "isakmp_inf.h" | |
80 | #ifdef ENABLE_HYBRID | |
81 | #include "isakmp_xauth.h" | |
82 | #include "isakmp_cfg.h" | |
83 | #endif | |
84 | #ifdef ENABLE_FRAG | |
85 | #include "isakmp_frag.h" | |
86 | #endif | |
87 | #include "vendorid.h" | |
88 | #include "strnames.h" | |
89 | ||
90 | #ifdef ENABLE_NATT | |
91 | #include "nattraversal.h" | |
92 | #endif | |
93 | ||
52b7d2ce A |
94 | #include "vpn_control.h" |
95 | #include "vpn_control_var.h" | |
d1e348cf A |
96 | #include "ipsecSessionTracer.h" |
97 | #include "ipsecMessageTracer.h" | |
e8d9021d A |
98 | #ifndef HAVE_OPENSSL |
99 | #include <Security/SecDH.h> | |
100 | #endif | |
52b7d2ce A |
101 | |
102 | /* | |
103 | * begin Aggressive Mode as initiator. | |
104 | */ | |
105 | /* | |
106 | * send to responder | |
107 | * psk: HDR, SA, KE, Ni, IDi1 | |
108 | * sig: HDR, SA, KE, Ni, IDi1 [, CR ] | |
109 | * gssapi: HDR, SA, KE, Ni, IDi1, GSSi | |
110 | * rsa: HDR, SA, [ HASH(1),] KE, <IDi1_b>Pubkey_r, <Ni_b>Pubkey_r | |
111 | * rev: HDR, SA, [ HASH(1),] <Ni_b>Pubkey_r, <KE_b>Ke_i, | |
112 | * <IDii_b>Ke_i [, <Cert-I_b>Ke_i ] | |
113 | */ | |
114 | int | |
115 | agg_i1send(iph1, msg) | |
65c25746 | 116 | phase1_handle_t *iph1; |
52b7d2ce A |
117 | vchar_t *msg; /* must be null */ |
118 | { | |
119 | struct payload_list *plist = NULL; | |
120 | int need_cr = 0; | |
d1e348cf | 121 | vchar_t *cr = NULL; |
52b7d2ce A |
122 | int error = -1; |
123 | #ifdef ENABLE_NATT | |
124 | vchar_t *vid_natt[MAX_NATT_VID_COUNT] = { NULL }; | |
125 | int i; | |
126 | #endif | |
127 | #ifdef ENABLE_HYBRID | |
128 | vchar_t *vid_xauth = NULL; | |
129 | vchar_t *vid_unity = NULL; | |
130 | #endif | |
131 | #ifdef ENABLE_FRAG | |
132 | vchar_t *vid_frag = NULL; | |
133 | #endif | |
52b7d2ce A |
134 | #ifdef ENABLE_DPD |
135 | vchar_t *vid_dpd = NULL; | |
136 | #endif | |
137 | ||
65c25746 A |
138 | /* validity check */ |
139 | if (iph1->status != IKEV1_STATE_AGG_I_START) { | |
140 | plog(ASL_LEVEL_ERR, | |
141 | "status mismatched %d.\n", iph1->status); | |
142 | goto end; | |
143 | } | |
52b7d2ce A |
144 | |
145 | /* validity check */ | |
146 | if (msg != NULL) { | |
65c25746 | 147 | plog(ASL_LEVEL_ERR, |
52b7d2ce A |
148 | "msg has to be NULL in this function.\n"); |
149 | goto end; | |
150 | } | |
52b7d2ce A |
151 | |
152 | /* create isakmp index */ | |
153 | memset(&iph1->index, 0, sizeof(iph1->index)); | |
154 | isakmp_newcookie((caddr_t)&iph1->index, iph1->remote, iph1->local); | |
155 | ||
156 | /* make ID payload into isakmp status */ | |
d1e348cf | 157 | if (ipsecdoi_setid1(iph1) < 0) { |
65c25746 | 158 | plog(ASL_LEVEL_ERR, |
d1e348cf | 159 | "failed to set ID"); |
52b7d2ce | 160 | goto end; |
d1e348cf | 161 | } |
52b7d2ce A |
162 | |
163 | /* create SA payload for my proposal */ | |
65c25746 | 164 | iph1->sa = ipsecdoi_setph1proposal(iph1); |
d1e348cf | 165 | if (iph1->sa == NULL) { |
65c25746 | 166 | plog(ASL_LEVEL_ERR, |
d1e348cf | 167 | "failed to set proposal"); |
52b7d2ce | 168 | goto end; |
d1e348cf | 169 | } |
52b7d2ce A |
170 | |
171 | /* consistency check of proposals */ | |
172 | if (iph1->rmconf->dhgrp == NULL) { | |
65c25746 | 173 | plog(ASL_LEVEL_ERR, |
52b7d2ce A |
174 | "configuration failure about DH group.\n"); |
175 | goto end; | |
176 | } | |
177 | ||
178 | /* generate DH public value */ | |
e8d9021d A |
179 | #ifdef HAVE_OPENSSL |
180 | if (oakley_dh_generate(iph1->rmconf->dhgrp, | |
181 | &iph1->dhpub, &iph1->dhpriv) < 0) { | |
182 | #else | |
52b7d2ce | 183 | if (oakley_dh_generate(iph1->rmconf->dhgrp, |
e8d9021d A |
184 | &iph1->dhpub, &iph1->publicKeySize, &iph1->dhC) < 0) { |
185 | #endif | |
65c25746 | 186 | plog(ASL_LEVEL_ERR, |
d1e348cf | 187 | "failed to generate DH"); |
52b7d2ce | 188 | goto end; |
d1e348cf | 189 | } |
52b7d2ce A |
190 | |
191 | /* generate NONCE value */ | |
192 | iph1->nonce = eay_set_random(iph1->rmconf->nonce_size); | |
d1e348cf | 193 | if (iph1->nonce == NULL) { |
65c25746 | 194 | plog(ASL_LEVEL_ERR, |
d1e348cf | 195 | "failed to generate NONCE"); |
52b7d2ce | 196 | goto end; |
d1e348cf | 197 | } |
52b7d2ce A |
198 | |
199 | #ifdef ENABLE_HYBRID | |
200 | /* Do we need Xauth VID? */ | |
d1e348cf A |
201 | switch (RMAUTHMETHOD(iph1)) { |
202 | case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I: | |
203 | case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I: | |
d1e348cf | 204 | case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I: |
d1e348cf A |
205 | case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I: |
206 | case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I: | |
52b7d2ce | 207 | if ((vid_xauth = set_vendorid(VENDORID_XAUTH)) == NULL) |
65c25746 | 208 | plog(ASL_LEVEL_ERR, |
52b7d2ce A |
209 | "Xauth vendor ID generation failed\n"); |
210 | if ((vid_unity = set_vendorid(VENDORID_UNITY)) == NULL) | |
65c25746 | 211 | plog(ASL_LEVEL_ERR, |
52b7d2ce A |
212 | "Unity vendor ID generation failed\n"); |
213 | break; | |
214 | default: | |
215 | break; | |
216 | } | |
217 | #endif | |
218 | ||
219 | #ifdef ENABLE_FRAG | |
220 | if (iph1->rmconf->ike_frag) { | |
221 | vid_frag = set_vendorid(VENDORID_FRAG); | |
222 | if (vid_frag != NULL) | |
223 | vid_frag = isakmp_frag_addcap(vid_frag, | |
224 | VENDORID_FRAG_AGG); | |
225 | if (vid_frag == NULL) | |
65c25746 | 226 | plog(ASL_LEVEL_ERR, |
52b7d2ce A |
227 | "Frag vendorID construction failed\n"); |
228 | } | |
229 | #endif | |
230 | ||
231 | /* create CR if need */ | |
232 | if (iph1->rmconf->send_cr | |
65c25746 | 233 | && oakley_needcr(iph1->rmconf->proposal->authmethod)) { |
52b7d2ce A |
234 | need_cr = 1; |
235 | cr = oakley_getcr(iph1); | |
236 | if (cr == NULL) { | |
65c25746 | 237 | plog(ASL_LEVEL_ERR, |
d1e348cf | 238 | "failed to get CR"); |
52b7d2ce A |
239 | goto end; |
240 | } | |
241 | } | |
242 | ||
65c25746 | 243 | plog(ASL_LEVEL_DEBUG, "authmethod is %s\n", |
52b7d2ce | 244 | s_oakley_attr_method(iph1->rmconf->proposal->authmethod)); |
52b7d2ce A |
245 | |
246 | /* set SA payload to propose */ | |
247 | plist = isakmp_plist_append(plist, iph1->sa, ISAKMP_NPTYPE_SA); | |
248 | ||
249 | /* create isakmp KE payload */ | |
250 | plist = isakmp_plist_append(plist, iph1->dhpub, ISAKMP_NPTYPE_KE); | |
251 | ||
252 | /* create isakmp NONCE payload */ | |
253 | plist = isakmp_plist_append(plist, iph1->nonce, ISAKMP_NPTYPE_NONCE); | |
254 | ||
255 | /* create isakmp ID payload */ | |
256 | plist = isakmp_plist_append(plist, iph1->id, ISAKMP_NPTYPE_ID); | |
257 | ||
52b7d2ce A |
258 | /* create isakmp CR payload */ |
259 | if (need_cr) | |
260 | plist = isakmp_plist_append(plist, cr, ISAKMP_NPTYPE_CR); | |
261 | ||
262 | #ifdef ENABLE_FRAG | |
263 | if (vid_frag) | |
264 | plist = isakmp_plist_append(plist, vid_frag, ISAKMP_NPTYPE_VID); | |
265 | #endif | |
266 | #ifdef ENABLE_NATT | |
267 | /* | |
268 | * set VID payload for NAT-T if NAT-T | |
269 | * support allowed in the config file | |
270 | */ | |
271 | if (iph1->rmconf->nat_traversal) | |
272 | plist = isakmp_plist_append_natt_vids(plist, vid_natt); | |
273 | #endif | |
274 | #ifdef ENABLE_HYBRID | |
275 | if (vid_xauth) | |
276 | plist = isakmp_plist_append(plist, | |
277 | vid_xauth, ISAKMP_NPTYPE_VID); | |
278 | if (vid_unity) | |
279 | plist = isakmp_plist_append(plist, | |
280 | vid_unity, ISAKMP_NPTYPE_VID); | |
281 | #endif | |
282 | #ifdef ENABLE_DPD | |
283 | if(iph1->rmconf->dpd){ | |
284 | vid_dpd = set_vendorid(VENDORID_DPD); | |
285 | if (vid_dpd != NULL) | |
286 | plist = isakmp_plist_append(plist, vid_dpd, ISAKMP_NPTYPE_VID); | |
287 | } | |
288 | #endif | |
289 | ||
290 | iph1->sendbuf = isakmp_plist_set_all (&plist, iph1); | |
291 | ||
292 | #ifdef HAVE_PRINT_ISAKMP_C | |
293 | isakmp_printpacket(iph1->sendbuf, iph1->local, iph1->remote, 0); | |
294 | #endif | |
295 | ||
296 | /* send the packet, add to the schedule to resend */ | |
297 | iph1->retry_counter = iph1->rmconf->retry_counter; | |
d1e348cf | 298 | if (isakmp_ph1resend(iph1) == -1) { |
65c25746 | 299 | plog(ASL_LEVEL_ERR, |
d1e348cf | 300 | "failed to send packet"); |
52b7d2ce | 301 | goto end; |
d1e348cf | 302 | } |
52b7d2ce | 303 | |
65c25746 | 304 | fsm_set_state(&iph1->status, IKEV1_STATE_AGG_I_MSG1SENT); |
52b7d2ce A |
305 | |
306 | error = 0; | |
307 | ||
d1e348cf A |
308 | IPSECSESSIONTRACEREVENT(iph1->parent_session, |
309 | IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC, | |
310 | CONSTSTR("Initiator, Aggressive-Mode message 1"), | |
311 | CONSTSTR(NULL)); | |
312 | ||
52b7d2ce | 313 | end: |
d1e348cf A |
314 | if (error) { |
315 | IPSECSESSIONTRACEREVENT(iph1->parent_session, | |
316 | IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL, | |
317 | CONSTSTR("Initiator, Aggressive-Mode Message 1"), | |
318 | CONSTSTR("Failed to transmit Aggressive-Mode Message 1")); | |
319 | } | |
52b7d2ce A |
320 | if (cr) |
321 | vfree(cr); | |
52b7d2ce A |
322 | #ifdef ENABLE_FRAG |
323 | if (vid_frag) | |
324 | vfree(vid_frag); | |
325 | #endif | |
326 | #ifdef ENABLE_NATT | |
327 | for (i = 0; i < MAX_NATT_VID_COUNT && vid_natt[i] != NULL; i++) | |
328 | vfree(vid_natt[i]); | |
329 | #endif | |
52b7d2ce A |
330 | #ifdef ENABLE_HYBRID |
331 | if (vid_xauth != NULL) | |
332 | vfree(vid_xauth); | |
333 | if (vid_unity != NULL) | |
334 | vfree(vid_unity); | |
335 | #endif | |
d1e348cf A |
336 | #ifdef ENABLE_DPD |
337 | if (vid_dpd != NULL) | |
338 | vfree(vid_dpd); | |
339 | #endif | |
52b7d2ce A |
340 | |
341 | return error; | |
342 | } | |
343 | ||
344 | /* | |
345 | * receive from responder | |
346 | * psk: HDR, SA, KE, Nr, IDr1, HASH_R | |
347 | * sig: HDR, SA, KE, Nr, IDr1, [ CR, ] [ CERT, ] SIG_R | |
348 | * gssapi: HDR, SA, KE, Nr, IDr1, GSSr, HASH_R | |
349 | * rsa: HDR, SA, KE, <IDr1_b>PubKey_i, <Nr_b>PubKey_i, HASH_R | |
350 | * rev: HDR, SA, <Nr_b>PubKey_i, <KE_b>Ke_r, <IDir_b>Ke_r, HASH_R | |
351 | */ | |
352 | int | |
353 | agg_i2recv(iph1, msg) | |
65c25746 | 354 | phase1_handle_t *iph1; |
52b7d2ce A |
355 | vchar_t *msg; |
356 | { | |
357 | vchar_t *pbuf = NULL; | |
358 | struct isakmp_parse_t *pa; | |
359 | vchar_t *satmp = NULL; | |
360 | int error = -1; | |
361 | int vid_numeric; | |
362 | int ptype; | |
e8d9021d | 363 | int received_cert = 0; |
52b7d2ce A |
364 | |
365 | #ifdef ENABLE_NATT | |
366 | int natd_seq = 0; | |
367 | struct natd_payload { | |
368 | int seq; | |
369 | vchar_t *payload; | |
370 | TAILQ_ENTRY(natd_payload) chain; | |
371 | }; | |
372 | TAILQ_HEAD(_natd_payload, natd_payload) natd_tree; | |
373 | TAILQ_INIT(&natd_tree); | |
374 | #endif | |
375 | ||
65c25746 A |
376 | /* validity check */ |
377 | if (iph1->status != IKEV1_STATE_AGG_I_MSG1SENT) { | |
378 | plog(ASL_LEVEL_ERR, | |
379 | "status mismatched %d.\n", iph1->status); | |
52b7d2ce A |
380 | goto end; |
381 | } | |
382 | ||
383 | /* validate the type of next payload */ | |
384 | pbuf = isakmp_parse(msg); | |
d1e348cf | 385 | if (pbuf == NULL) { |
65c25746 | 386 | plog(ASL_LEVEL_ERR, |
d1e348cf | 387 | "failed to parse msg"); |
52b7d2ce | 388 | goto end; |
d1e348cf | 389 | } |
85f41bec | 390 | pa = ALIGNED_CAST(struct isakmp_parse_t *)pbuf->v; |
52b7d2ce A |
391 | |
392 | iph1->pl_hash = NULL; | |
393 | ||
394 | /* SA payload is fixed postion */ | |
395 | if (pa->type != ISAKMP_NPTYPE_SA) { | |
65c25746 | 396 | plog(ASL_LEVEL_ERR, |
52b7d2ce A |
397 | "received invalid next payload type %d, " |
398 | "expecting %d.\n", | |
399 | pa->type, ISAKMP_NPTYPE_SA); | |
400 | goto end; | |
401 | } | |
402 | ||
d1e348cf | 403 | if (isakmp_p2ph(&satmp, pa->ptr) < 0) { |
65c25746 | 404 | plog(ASL_LEVEL_ERR, |
d1e348cf | 405 | "failed to process SA payload"); |
52b7d2ce | 406 | goto end; |
d1e348cf | 407 | } |
52b7d2ce A |
408 | pa++; |
409 | ||
410 | for (/*nothing*/; | |
411 | pa->type != ISAKMP_NPTYPE_NONE; | |
412 | pa++) { | |
413 | ||
414 | switch (pa->type) { | |
415 | case ISAKMP_NPTYPE_KE: | |
d1e348cf | 416 | if (isakmp_p2ph(&iph1->dhpub_p, pa->ptr) < 0) { |
65c25746 | 417 | plog(ASL_LEVEL_ERR, |
d1e348cf | 418 | "failed to process KE payload"); |
52b7d2ce | 419 | goto end; |
d1e348cf | 420 | } |
52b7d2ce A |
421 | break; |
422 | case ISAKMP_NPTYPE_NONCE: | |
d1e348cf | 423 | if (isakmp_p2ph(&iph1->nonce_p, pa->ptr) < 0) { |
65c25746 | 424 | plog(ASL_LEVEL_ERR, |
d1e348cf | 425 | "failed to process NONCE payload"); |
52b7d2ce | 426 | goto end; |
d1e348cf | 427 | } |
52b7d2ce A |
428 | break; |
429 | case ISAKMP_NPTYPE_ID: | |
d1e348cf | 430 | if (isakmp_p2ph(&iph1->id_p, pa->ptr) < 0) { |
65c25746 | 431 | plog(ASL_LEVEL_ERR, |
d1e348cf | 432 | "failed to process ID payload"); |
52b7d2ce | 433 | goto end; |
d1e348cf | 434 | } |
52b7d2ce A |
435 | break; |
436 | case ISAKMP_NPTYPE_HASH: | |
437 | iph1->pl_hash = (struct isakmp_pl_hash *)pa->ptr; | |
438 | break; | |
439 | case ISAKMP_NPTYPE_CR: | |
d1e348cf | 440 | if (oakley_savecr(iph1, pa->ptr) < 0) { |
65c25746 | 441 | plog(ASL_LEVEL_ERR, |
d1e348cf | 442 | "failed to process CR payload"); |
52b7d2ce | 443 | goto end; |
d1e348cf | 444 | } |
52b7d2ce A |
445 | break; |
446 | case ISAKMP_NPTYPE_CERT: | |
d1e348cf | 447 | if (oakley_savecert(iph1, pa->ptr) < 0) { |
65c25746 | 448 | plog(ASL_LEVEL_ERR, |
d1e348cf | 449 | "failed to process CERT payload"); |
52b7d2ce | 450 | goto end; |
d1e348cf | 451 | } |
e8d9021d | 452 | received_cert = 1; |
52b7d2ce A |
453 | break; |
454 | case ISAKMP_NPTYPE_SIG: | |
d1e348cf | 455 | if (isakmp_p2ph(&iph1->sig_p, pa->ptr) < 0) { |
65c25746 | 456 | plog(ASL_LEVEL_ERR, |
d1e348cf | 457 | "failed to process SIG payload"); |
52b7d2ce | 458 | goto end; |
d1e348cf | 459 | } |
52b7d2ce A |
460 | break; |
461 | case ISAKMP_NPTYPE_VID: | |
462 | vid_numeric = check_vendorid(pa->ptr); | |
463 | #ifdef ENABLE_NATT | |
464 | if (iph1->rmconf->nat_traversal && | |
465 | natt_vendorid(vid_numeric)) | |
466 | natt_handle_vendorid(iph1, vid_numeric); | |
467 | #endif | |
468 | #ifdef ENABLE_HYBRID | |
469 | switch (vid_numeric) { | |
470 | case VENDORID_XAUTH: | |
471 | iph1->mode_cfg->flags |= | |
472 | ISAKMP_CFG_VENDORID_XAUTH; | |
473 | break; | |
474 | ||
475 | case VENDORID_UNITY: | |
476 | iph1->mode_cfg->flags |= | |
477 | ISAKMP_CFG_VENDORID_UNITY; | |
478 | break; | |
479 | default: | |
480 | break; | |
481 | } | |
482 | #endif | |
483 | #ifdef ENABLE_DPD | |
484 | if (vid_numeric == VENDORID_DPD && iph1->rmconf->dpd) { | |
485 | iph1->dpd_support=1; | |
65c25746 | 486 | plog(ASL_LEVEL_DEBUG, |
52b7d2ce A |
487 | "remote supports DPD\n"); |
488 | } | |
e8d9021d A |
489 | #endif |
490 | #ifdef ENABLE_FRAG | |
491 | if ((vid_numeric == VENDORID_FRAG) && | |
492 | (vendorid_frag_cap(pa->ptr) & VENDORID_FRAG_AGG)) { | |
65c25746 | 493 | plog(ASL_LEVEL_DEBUG, |
e8d9021d A |
494 | "remote supports FRAGMENTATION\n"); |
495 | iph1->frag = 1; | |
496 | } | |
52b7d2ce A |
497 | #endif |
498 | break; | |
499 | case ISAKMP_NPTYPE_N: | |
500 | isakmp_check_notify(pa->ptr, iph1); | |
501 | break; | |
52b7d2ce A |
502 | |
503 | #ifdef ENABLE_NATT | |
504 | case ISAKMP_NPTYPE_NATD_DRAFT: | |
505 | case ISAKMP_NPTYPE_NATD_RFC: | |
52b7d2ce | 506 | case ISAKMP_NPTYPE_NATD_BADDRAFT: |
52b7d2ce A |
507 | if (NATT_AVAILABLE(iph1) && iph1->natt_options != NULL && |
508 | pa->type == iph1->natt_options->payload_nat_d) { | |
509 | struct natd_payload *natd; | |
510 | natd = (struct natd_payload *)racoon_malloc(sizeof(*natd)); | |
d1e348cf | 511 | if (!natd) { |
65c25746 | 512 | plog(ASL_LEVEL_ERR, |
d1e348cf | 513 | "failed to pre-process NATD payload"); |
52b7d2ce | 514 | goto end; |
d1e348cf | 515 | } |
52b7d2ce A |
516 | |
517 | natd->payload = NULL; | |
518 | ||
d1e348cf | 519 | if (isakmp_p2ph (&natd->payload, pa->ptr) < 0) { |
65c25746 | 520 | plog(ASL_LEVEL_ERR, |
d1e348cf | 521 | "failed to process NATD payload"); |
52b7d2ce | 522 | goto end; |
d1e348cf | 523 | } |
52b7d2ce A |
524 | |
525 | natd->seq = natd_seq++; | |
526 | ||
527 | TAILQ_INSERT_TAIL(&natd_tree, natd, chain); | |
528 | break; | |
529 | } | |
530 | /* %%% Be lenient here - some servers send natd payloads */ | |
531 | /* when nat not detected */ | |
532 | break; | |
533 | #endif | |
534 | ||
535 | default: | |
536 | /* don't send information, see isakmp_ident_r1() */ | |
65c25746 | 537 | plog(ASL_LEVEL_ERR, |
52b7d2ce A |
538 | "ignore the packet, " |
539 | "received unexpecting payload type %d.\n", | |
540 | pa->type); | |
541 | goto end; | |
542 | } | |
543 | } | |
544 | ||
e8d9021d A |
545 | if (received_cert) { |
546 | oakley_verify_certid(iph1); | |
547 | } | |
548 | ||
52b7d2ce A |
549 | /* payload existency check */ |
550 | if (iph1->dhpub_p == NULL || iph1->nonce_p == NULL) { | |
65c25746 | 551 | plog(ASL_LEVEL_ERR, |
52b7d2ce A |
552 | "few isakmp message received.\n"); |
553 | goto end; | |
554 | } | |
555 | ||
556 | /* verify identifier */ | |
557 | if (ipsecdoi_checkid1(iph1) != 0) { | |
65c25746 | 558 | plog(ASL_LEVEL_ERR, |
52b7d2ce A |
559 | "invalid ID payload.\n"); |
560 | goto end; | |
561 | } | |
562 | ||
563 | /* check SA payload and set approval SA for use */ | |
564 | if (ipsecdoi_checkph1proposal(satmp, iph1) < 0) { | |
65c25746 | 565 | plog(ASL_LEVEL_ERR, |
52b7d2ce A |
566 | "failed to get valid proposal.\n"); |
567 | /* XXX send information */ | |
568 | goto end; | |
569 | } | |
570 | VPTRINIT(iph1->sa_ret); | |
571 | ||
572 | /* fix isakmp index */ | |
573 | memcpy(&iph1->index.r_ck, &((struct isakmp *)msg->v)->r_ck, | |
574 | sizeof(cookie_t)); | |
575 | ||
576 | #ifdef ENABLE_NATT | |
577 | if (NATT_AVAILABLE(iph1)) { | |
578 | struct natd_payload *natd = NULL; | |
579 | int natd_verified; | |
580 | ||
65c25746 | 581 | plog(ASL_LEVEL_INFO, |
52b7d2ce A |
582 | "Selected NAT-T version: %s\n", |
583 | vid_string_by_id(iph1->natt_options->version)); | |
584 | ||
585 | /* set both bits first so that we can clear them | |
586 | upon verifying hashes */ | |
587 | iph1->natt_flags |= NAT_DETECTED; | |
588 | ||
589 | while ((natd = TAILQ_FIRST(&natd_tree)) != NULL) { | |
590 | /* this function will clear appropriate bits bits | |
591 | from iph1->natt_flags */ | |
592 | natd_verified = natt_compare_addr_hash (iph1, | |
593 | natd->payload, natd->seq); | |
594 | ||
65c25746 | 595 | plog (ASL_LEVEL_INFO, "NAT-D payload #%d %s\n", |
52b7d2ce A |
596 | natd->seq - 1, |
597 | natd_verified ? "verified" : "doesn't match"); | |
598 | ||
599 | vfree (natd->payload); | |
600 | ||
601 | TAILQ_REMOVE(&natd_tree, natd, chain); | |
602 | racoon_free (natd); | |
603 | } | |
604 | ||
65c25746 | 605 | plog (ASL_LEVEL_INFO, "NAT %s %s%s\n", |
52b7d2ce A |
606 | iph1->natt_flags & NAT_DETECTED ? |
607 | "detected:" : "not detected", | |
608 | iph1->natt_flags & NAT_DETECTED_ME ? "ME " : "", | |
609 | iph1->natt_flags & NAT_DETECTED_PEER ? "PEER" : ""); | |
610 | ||
611 | if (iph1->natt_flags & NAT_DETECTED) | |
612 | natt_float_ports (iph1); | |
80318cb7 | 613 | ike_session_update_natt_version(iph1); |
52b7d2ce A |
614 | } |
615 | #endif | |
616 | ||
617 | /* compute sharing secret of DH */ | |
e8d9021d | 618 | #ifdef HAVE_OPENSSL |
52b7d2ce | 619 | if (oakley_dh_compute(iph1->rmconf->dhgrp, iph1->dhpub, |
d1e348cf | 620 | iph1->dhpriv, iph1->dhpub_p, &iph1->dhgxy) < 0) { |
e8d9021d | 621 | #else |
65c25746 | 622 | if (oakley_dh_compute(iph1->rmconf->dhgrp, iph1->dhpub_p, iph1->publicKeySize, &iph1->dhgxy, &iph1->dhC) < 0) { |
e8d9021d | 623 | #endif |
65c25746 | 624 | plog(ASL_LEVEL_ERR, |
d1e348cf | 625 | "failed to compute DH"); |
52b7d2ce | 626 | goto end; |
d1e348cf | 627 | } |
52b7d2ce A |
628 | |
629 | /* generate SKEYIDs & IV & final cipher key */ | |
d1e348cf | 630 | if (oakley_skeyid(iph1) < 0) { |
65c25746 | 631 | plog(ASL_LEVEL_ERR, |
d1e348cf | 632 | "failed to generate SKEYID"); |
52b7d2ce | 633 | goto end; |
d1e348cf A |
634 | } |
635 | if (oakley_skeyid_dae(iph1) < 0) { | |
65c25746 | 636 | plog(ASL_LEVEL_ERR, |
d1e348cf | 637 | "failed to generate SKEYID-DAE"); |
52b7d2ce | 638 | goto end; |
d1e348cf A |
639 | } |
640 | if (oakley_compute_enckey(iph1) < 0) { | |
65c25746 | 641 | plog(ASL_LEVEL_ERR, |
d1e348cf | 642 | "failed to generate ENCKEY"); |
52b7d2ce | 643 | goto end; |
d1e348cf A |
644 | } |
645 | if (oakley_newiv(iph1) < 0) { | |
65c25746 | 646 | plog(ASL_LEVEL_ERR, |
d1e348cf | 647 | "failed to generate IV"); |
52b7d2ce | 648 | goto end; |
d1e348cf | 649 | } |
52b7d2ce A |
650 | |
651 | /* validate authentication value */ | |
652 | ptype = oakley_validate_auth(iph1); | |
653 | if (ptype != 0) { | |
d1e348cf A |
654 | IPSECSESSIONTRACEREVENT(iph1->parent_session, |
655 | IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_FAIL, | |
656 | CONSTSTR("Initiator, Aggressive-Mode Message 2"), | |
657 | CONSTSTR("Failed to authenticate, Aggressive-Mode Message 2")); | |
52b7d2ce A |
658 | if (ptype == -1) { |
659 | /* message printed inner oakley_validate_auth() */ | |
660 | goto end; | |
661 | } | |
52b7d2ce A |
662 | isakmp_info_send_n1(iph1, ptype, NULL); |
663 | goto end; | |
664 | } | |
d1e348cf A |
665 | IPSECSESSIONTRACEREVENT(iph1->parent_session, |
666 | IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_SUCC, | |
667 | CONSTSTR("Initiator, Aggressive-Mode Message 2"), | |
668 | CONSTSTR(NULL)); | |
669 | ||
52b7d2ce A |
670 | if (oakley_checkcr(iph1) < 0) { |
671 | /* Ignore this error in order to be interoperability. */ | |
672 | ; | |
673 | } | |
674 | ||
675 | /* change status of isakmp status entry */ | |
65c25746 | 676 | fsm_set_state(&iph1->status, IKEV1_STATE_AGG_I_MSG2RCVD); |
52b7d2ce A |
677 | |
678 | #ifdef ENABLE_VPNCONTROL_PORT | |
679 | vpncontrol_notify_phase_change(1, FROM_REMOTE, iph1, NULL); | |
680 | #endif | |
681 | ||
682 | error = 0; | |
683 | ||
d1e348cf A |
684 | IPSECSESSIONTRACEREVENT(iph1->parent_session, |
685 | IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC, | |
686 | CONSTSTR("Initiator, Aggressive-Mode message 2"), | |
687 | CONSTSTR(NULL)); | |
688 | ||
52b7d2ce | 689 | end: |
d1e348cf A |
690 | if (error) { |
691 | IPSECSESSIONTRACEREVENT(iph1->parent_session, | |
692 | IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL, | |
693 | CONSTSTR("Initiator, Aggressive-Mode Message 2"), | |
694 | CONSTSTR("Failure processing Aggressive-Mode Message 2")); | |
695 | } | |
65c25746 | 696 | |
52b7d2ce A |
697 | if (pbuf) |
698 | vfree(pbuf); | |
699 | if (satmp) | |
700 | vfree(satmp); | |
701 | if (error) { | |
702 | VPTRINIT(iph1->dhpub_p); | |
703 | VPTRINIT(iph1->nonce_p); | |
704 | VPTRINIT(iph1->id_p); | |
705 | oakley_delcert(iph1->cert_p); | |
706 | iph1->cert_p = NULL; | |
707 | oakley_delcert(iph1->crl_p); | |
708 | iph1->crl_p = NULL; | |
709 | VPTRINIT(iph1->sig_p); | |
710 | oakley_delcert(iph1->cr_p); | |
711 | iph1->cr_p = NULL; | |
712 | } | |
713 | ||
714 | return error; | |
715 | } | |
716 | ||
717 | /* | |
718 | * send to responder | |
719 | * psk: HDR, HASH_I | |
720 | * gssapi: HDR, HASH_I | |
721 | * sig: HDR, [ CERT, ] SIG_I | |
722 | * rsa: HDR, HASH_I | |
723 | * rev: HDR, HASH_I | |
724 | */ | |
725 | int | |
65c25746 A |
726 | agg_i3send(iph1, msg) |
727 | phase1_handle_t *iph1; | |
52b7d2ce A |
728 | vchar_t *msg; |
729 | { | |
730 | struct payload_list *plist = NULL; | |
731 | int need_cert = 0; | |
732 | int error = -1; | |
733 | vchar_t *gsshash = NULL; | |
d1e348cf A |
734 | #ifdef ENABLE_NATT |
735 | vchar_t *natd[2] = { NULL, NULL }; | |
736 | #endif | |
737 | vchar_t *notp_unity = NULL; | |
738 | vchar_t *notp_ini = NULL; | |
52b7d2ce | 739 | |
65c25746 A |
740 | /* validity check */ |
741 | if (iph1->status != IKEV1_STATE_AGG_I_MSG2RCVD) { | |
742 | plog(ASL_LEVEL_ERR, | |
743 | "status mismatched %d.\n", iph1->status); | |
52b7d2ce A |
744 | goto end; |
745 | } | |
746 | ||
747 | /* generate HASH to send */ | |
65c25746 | 748 | plog(ASL_LEVEL_DEBUG, "generate HASH_I\n"); |
52b7d2ce A |
749 | iph1->hash = oakley_ph1hash_common(iph1, GENERATE); |
750 | if (iph1->hash == NULL) { | |
65c25746 | 751 | plog(ASL_LEVEL_ERR, |
d1e348cf | 752 | "failed to generate HASH"); |
52b7d2ce A |
753 | goto end; |
754 | } | |
755 | ||
d1e348cf | 756 | switch (AUTHMETHOD(iph1)) { |
52b7d2ce A |
757 | case OAKLEY_ATTR_AUTH_METHOD_PSKEY: |
758 | #ifdef ENABLE_HYBRID | |
d1e348cf A |
759 | case FICTIVE_AUTH_METHOD_XAUTH_PSKEY_I: |
760 | case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_I: | |
52b7d2ce A |
761 | #endif |
762 | /* set HASH payload */ | |
d1e348cf A |
763 | plist = isakmp_plist_append(plist, |
764 | iph1->hash, ISAKMP_NPTYPE_HASH); | |
52b7d2ce | 765 | break; |
d1e348cf | 766 | |
52b7d2ce A |
767 | case OAKLEY_ATTR_AUTH_METHOD_RSASIG: |
768 | #ifdef ENABLE_HYBRID | |
d1e348cf | 769 | case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_I: |
d1e348cf | 770 | #endif |
52b7d2ce A |
771 | /* XXX if there is CR or not ? */ |
772 | ||
d1e348cf | 773 | if (oakley_getmycert(iph1) < 0) { |
65c25746 | 774 | plog(ASL_LEVEL_ERR, |
d1e348cf | 775 | "failed to get mycert"); |
52b7d2ce | 776 | goto end; |
d1e348cf | 777 | } |
52b7d2ce | 778 | |
d1e348cf | 779 | if (oakley_getsign(iph1) < 0) { |
65c25746 | 780 | plog(ASL_LEVEL_ERR, |
d1e348cf | 781 | "failed to get sign"); |
52b7d2ce | 782 | goto end; |
d1e348cf | 783 | } |
52b7d2ce A |
784 | |
785 | if (iph1->cert != NULL && iph1->rmconf->send_cert) | |
786 | need_cert = 1; | |
787 | ||
788 | /* add CERT payload if there */ | |
e8d9021d | 789 | // we don't support sending of certchains |
52b7d2ce A |
790 | if (need_cert) |
791 | plist = isakmp_plist_append(plist, iph1->cert->pl, ISAKMP_NPTYPE_CERT); | |
792 | ||
793 | /* add SIG payload */ | |
794 | plist = isakmp_plist_append(plist, iph1->sig, ISAKMP_NPTYPE_SIG); | |
795 | break; | |
796 | ||
797 | case OAKLEY_ATTR_AUTH_METHOD_RSAENC: | |
798 | case OAKLEY_ATTR_AUTH_METHOD_RSAREV: | |
d1e348cf A |
799 | #ifdef ENABLE_HYBRID |
800 | case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_I: | |
801 | case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_I: | |
802 | #endif | |
52b7d2ce | 803 | break; |
52b7d2ce A |
804 | } |
805 | ||
806 | #ifdef ENABLE_NATT | |
807 | /* generate NAT-D payloads */ | |
d1e348cf | 808 | if (NATT_AVAILABLE(iph1)) { |
65c25746 | 809 | plog (ASL_LEVEL_INFO, "Adding remote and local NAT-D payloads.\n"); |
52b7d2ce | 810 | if ((natd[0] = natt_hash_addr (iph1, iph1->remote)) == NULL) { |
65c25746 | 811 | plog(ASL_LEVEL_ERR, |
85f41bec | 812 | "NAT-D hashing failed for %s\n", saddr2str((struct sockaddr *)iph1->remote)); |
52b7d2ce A |
813 | goto end; |
814 | } | |
815 | ||
816 | if ((natd[1] = natt_hash_addr (iph1, iph1->local)) == NULL) { | |
65c25746 | 817 | plog(ASL_LEVEL_ERR, |
85f41bec | 818 | "NAT-D hashing failed for %s\n", saddr2str((struct sockaddr *)iph1->local)); |
52b7d2ce A |
819 | goto end; |
820 | } | |
52b7d2ce A |
821 | /* old Apple version sends natd payloads in the wrong order */ |
822 | if (iph1->natt_options->version == VENDORID_NATT_APPLE) { | |
823 | plist = isakmp_plist_append(plist, natd[1], iph1->natt_options->payload_nat_d); | |
824 | plist = isakmp_plist_append(plist, natd[0], iph1->natt_options->payload_nat_d); | |
825 | } else | |
52b7d2ce A |
826 | { |
827 | plist = isakmp_plist_append(plist, natd[0], iph1->natt_options->payload_nat_d); | |
828 | plist = isakmp_plist_append(plist, natd[1], iph1->natt_options->payload_nat_d); | |
829 | } | |
830 | } | |
831 | #endif | |
832 | ||
52b7d2ce | 833 | |
d1e348cf A |
834 | iph1->sendbuf = isakmp_plist_set_all (&plist, iph1); |
835 | ||
52b7d2ce A |
836 | #ifdef HAVE_PRINT_ISAKMP_C |
837 | isakmp_printpacket(iph1->sendbuf, iph1->local, iph1->remote, 0); | |
838 | #endif | |
839 | ||
d1e348cf | 840 | |
52b7d2ce | 841 | /* send to responder */ |
d1e348cf | 842 | if (isakmp_send(iph1, iph1->sendbuf) < 0) { |
65c25746 | 843 | plog(ASL_LEVEL_ERR, |
d1e348cf | 844 | "failed to send packet"); |
52b7d2ce | 845 | goto end; |
d1e348cf | 846 | } |
52b7d2ce A |
847 | |
848 | /* the sending message is added to the received-list. */ | |
65c25746 A |
849 | if (ike_session_add_recvdpkt(iph1->remote, iph1->local, iph1->sendbuf, msg, |
850 | PH1_NON_ESP_EXTRA_LEN(iph1, iph1->sendbuf), PH1_FRAG_FLAGS(iph1)) == -1) { | |
851 | plog(ASL_LEVEL_ERR , | |
52b7d2ce A |
852 | "failed to add a response packet to the tree.\n"); |
853 | goto end; | |
854 | } | |
855 | ||
856 | /* set encryption flag */ | |
857 | iph1->flags |= ISAKMP_FLAG_E; | |
858 | ||
65c25746 | 859 | fsm_set_state(&iph1->status, IKEV1_STATE_PHASE1_ESTABLISHED); |
52b7d2ce | 860 | |
d1e348cf A |
861 | IPSECSESSIONTRACEREVENT(iph1->parent_session, |
862 | IPSECSESSIONEVENTCODE_IKEV1_PH1_INIT_SUCC, | |
863 | CONSTSTR("Initiator, Aggressive-Mode"), | |
864 | CONSTSTR(NULL)); | |
865 | ||
52b7d2ce A |
866 | error = 0; |
867 | ||
d1e348cf A |
868 | IPSECSESSIONTRACEREVENT(iph1->parent_session, |
869 | IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC, | |
870 | CONSTSTR("Initiator, Aggressive-Mode message 3"), | |
871 | CONSTSTR(NULL)); | |
872 | ||
52b7d2ce | 873 | end: |
d1e348cf A |
874 | if (error) { |
875 | IPSECSESSIONTRACEREVENT(iph1->parent_session, | |
876 | IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL, | |
877 | CONSTSTR("Initiator, Aggressive-Mode Message 3"), | |
878 | CONSTSTR("Failed to transmit Aggressive-Mode Message 3")); | |
879 | } | |
880 | #ifdef ENABLE_NATT | |
881 | if (natd[0]) | |
882 | vfree(natd[0]); | |
883 | if (natd[1]) | |
884 | vfree(natd[1]); | |
885 | #endif | |
886 | if (notp_unity) | |
887 | vfree(notp_unity); | |
888 | if (notp_ini) | |
889 | vfree(notp_ini); | |
52b7d2ce A |
890 | if (gsshash) |
891 | vfree(gsshash); | |
892 | return error; | |
893 | } | |
894 | ||
895 | /* | |
896 | * receive from initiator | |
897 | * psk: HDR, SA, KE, Ni, IDi1 | |
898 | * sig: HDR, SA, KE, Ni, IDi1 [, CR ] | |
899 | * gssapi: HDR, SA, KE, Ni, IDi1 , GSSi | |
900 | * rsa: HDR, SA, [ HASH(1),] KE, <IDi1_b>Pubkey_r, <Ni_b>Pubkey_r | |
901 | * rev: HDR, SA, [ HASH(1),] <Ni_b>Pubkey_r, <KE_b>Ke_i, | |
902 | * <IDii_b>Ke_i [, <Cert-I_b>Ke_i ] | |
903 | */ | |
904 | int | |
905 | agg_r1recv(iph1, msg) | |
65c25746 | 906 | phase1_handle_t *iph1; |
52b7d2ce A |
907 | vchar_t *msg; |
908 | { | |
909 | int error = -1; | |
910 | vchar_t *pbuf = NULL; | |
911 | struct isakmp_parse_t *pa; | |
912 | int vid_numeric; | |
52b7d2ce | 913 | |
65c25746 A |
914 | /* validity check */ |
915 | if (iph1->status != IKEV1_STATE_AGG_R_START) { | |
916 | plog(ASL_LEVEL_ERR, | |
917 | "status mismatched %d.\n", iph1->status); | |
52b7d2ce A |
918 | goto end; |
919 | } | |
920 | ||
921 | /* validate the type of next payload */ | |
922 | pbuf = isakmp_parse(msg); | |
d1e348cf | 923 | if (pbuf == NULL) { |
65c25746 | 924 | plog(ASL_LEVEL_ERR, |
d1e348cf | 925 | "failed to parse msg"); |
52b7d2ce | 926 | goto end; |
d1e348cf | 927 | } |
85f41bec | 928 | pa = ALIGNED_CAST(struct isakmp_parse_t *)pbuf->v; |
52b7d2ce A |
929 | |
930 | /* SA payload is fixed postion */ | |
931 | if (pa->type != ISAKMP_NPTYPE_SA) { | |
65c25746 | 932 | plog(ASL_LEVEL_ERR, |
52b7d2ce A |
933 | "received invalid next payload type %d, " |
934 | "expecting %d.\n", | |
935 | pa->type, ISAKMP_NPTYPE_SA); | |
936 | goto end; | |
937 | } | |
d1e348cf | 938 | if (isakmp_p2ph(&iph1->sa, pa->ptr) < 0) { |
65c25746 | 939 | plog(ASL_LEVEL_ERR, |
d1e348cf | 940 | "failed to process SA payload"); |
52b7d2ce | 941 | goto end; |
d1e348cf | 942 | } |
52b7d2ce A |
943 | pa++; |
944 | ||
945 | for (/*nothing*/; | |
946 | pa->type != ISAKMP_NPTYPE_NONE; | |
947 | pa++) { | |
948 | ||
65c25746 | 949 | plog(ASL_LEVEL_DEBUG, |
52b7d2ce A |
950 | "received payload of type %s\n", |
951 | s_isakmp_nptype(pa->type)); | |
952 | ||
953 | switch (pa->type) { | |
954 | case ISAKMP_NPTYPE_KE: | |
d1e348cf | 955 | if (isakmp_p2ph(&iph1->dhpub_p, pa->ptr) < 0) { |
65c25746 | 956 | plog(ASL_LEVEL_ERR, |
d1e348cf | 957 | "failed to process KE payload"); |
52b7d2ce | 958 | goto end; |
d1e348cf | 959 | } |
52b7d2ce A |
960 | break; |
961 | case ISAKMP_NPTYPE_NONCE: | |
d1e348cf | 962 | if (isakmp_p2ph(&iph1->nonce_p, pa->ptr) < 0) { |
65c25746 | 963 | plog(ASL_LEVEL_ERR, |
d1e348cf | 964 | "failed to process NONCE payload"); |
52b7d2ce | 965 | goto end; |
d1e348cf | 966 | } |
52b7d2ce A |
967 | break; |
968 | case ISAKMP_NPTYPE_ID: | |
d1e348cf | 969 | if (isakmp_p2ph(&iph1->id_p, pa->ptr) < 0) { |
65c25746 | 970 | plog(ASL_LEVEL_ERR, |
d1e348cf | 971 | "failed to process ID payload"); |
52b7d2ce | 972 | goto end; |
d1e348cf | 973 | } |
52b7d2ce A |
974 | break; |
975 | case ISAKMP_NPTYPE_VID: | |
976 | vid_numeric = check_vendorid(pa->ptr); | |
977 | ||
978 | #ifdef ENABLE_NATT | |
979 | if (iph1->rmconf->nat_traversal && | |
980 | natt_vendorid(vid_numeric)) { | |
981 | natt_handle_vendorid(iph1, vid_numeric); | |
982 | break; | |
983 | } | |
984 | #endif | |
985 | #ifdef ENABLE_HYBRID | |
986 | switch (vid_numeric) { | |
987 | case VENDORID_XAUTH: | |
988 | iph1->mode_cfg->flags |= | |
989 | ISAKMP_CFG_VENDORID_XAUTH; | |
990 | break; | |
991 | ||
992 | case VENDORID_UNITY: | |
993 | iph1->mode_cfg->flags |= | |
994 | ISAKMP_CFG_VENDORID_UNITY; | |
995 | break; | |
996 | default: | |
997 | break; | |
998 | } | |
999 | #endif | |
1000 | #ifdef ENABLE_DPD | |
1001 | if (vid_numeric == VENDORID_DPD && iph1->rmconf->dpd) { | |
1002 | iph1->dpd_support=1; | |
65c25746 | 1003 | plog(ASL_LEVEL_DEBUG, |
52b7d2ce A |
1004 | "remote supports DPD\n"); |
1005 | } | |
1006 | #endif | |
1007 | #ifdef ENABLE_FRAG | |
1008 | if ((vid_numeric == VENDORID_FRAG) && | |
e8d9021d | 1009 | (vendorid_frag_cap(pa->ptr) & VENDORID_FRAG_AGG)) { |
65c25746 | 1010 | plog(ASL_LEVEL_DEBUG, |
e8d9021d | 1011 | "remote supports FRAGMENTATION\n"); |
52b7d2ce | 1012 | iph1->frag = 1; |
e8d9021d | 1013 | } |
52b7d2ce A |
1014 | #endif |
1015 | break; | |
1016 | ||
1017 | case ISAKMP_NPTYPE_CR: | |
d1e348cf | 1018 | if (oakley_savecr(iph1, pa->ptr) < 0) { |
65c25746 | 1019 | plog(ASL_LEVEL_ERR, |
d1e348cf | 1020 | "failed to process CR payload"); |
52b7d2ce | 1021 | goto end; |
d1e348cf | 1022 | } |
52b7d2ce A |
1023 | break; |
1024 | ||
52b7d2ce A |
1025 | default: |
1026 | /* don't send information, see isakmp_ident_r1() */ | |
65c25746 | 1027 | plog(ASL_LEVEL_ERR, |
52b7d2ce A |
1028 | "ignore the packet, " |
1029 | "received unexpecting payload type %d.\n", | |
1030 | pa->type); | |
1031 | goto end; | |
1032 | } | |
1033 | } | |
1034 | ||
1035 | /* payload existency check */ | |
1036 | if (iph1->dhpub_p == NULL || iph1->nonce_p == NULL) { | |
65c25746 | 1037 | plog(ASL_LEVEL_ERR, |
52b7d2ce A |
1038 | "few isakmp message received.\n"); |
1039 | goto end; | |
1040 | } | |
1041 | ||
1042 | /* verify identifier */ | |
1043 | if (ipsecdoi_checkid1(iph1) != 0) { | |
65c25746 | 1044 | plog(ASL_LEVEL_ERR, |
52b7d2ce A |
1045 | "invalid ID payload.\n"); |
1046 | goto end; | |
1047 | } | |
1048 | ||
1049 | #ifdef ENABLE_NATT | |
80318cb7 | 1050 | if (NATT_AVAILABLE(iph1)) { |
65c25746 | 1051 | plog(ASL_LEVEL_INFO, |
52b7d2ce A |
1052 | "Selected NAT-T version: %s\n", |
1053 | vid_string_by_id(iph1->natt_options->version)); | |
80318cb7 A |
1054 | ike_session_update_natt_version(iph1); |
1055 | } | |
52b7d2ce A |
1056 | #endif |
1057 | ||
1058 | /* check SA payload and set approval SA for use */ | |
1059 | if (ipsecdoi_checkph1proposal(iph1->sa, iph1) < 0) { | |
65c25746 | 1060 | plog(ASL_LEVEL_ERR, |
52b7d2ce A |
1061 | "failed to get valid proposal.\n"); |
1062 | /* XXX send information */ | |
1063 | goto end; | |
1064 | } | |
1065 | ||
1066 | if (oakley_checkcr(iph1) < 0) { | |
1067 | /* Ignore this error in order to be interoperability. */ | |
1068 | ; | |
1069 | } | |
1070 | ||
65c25746 | 1071 | fsm_set_state(&iph1->status, IKEV1_STATE_AGG_R_MSG1RCVD); |
52b7d2ce A |
1072 | |
1073 | error = 0; | |
1074 | ||
d1e348cf A |
1075 | IPSECSESSIONTRACEREVENT(iph1->parent_session, |
1076 | IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC, | |
1077 | CONSTSTR("Responder, Aggressive-Mode message 1"), | |
1078 | CONSTSTR(NULL)); | |
1079 | ||
52b7d2ce | 1080 | end: |
d1e348cf A |
1081 | if (error) { |
1082 | IPSECSESSIONTRACEREVENT(iph1->parent_session, | |
1083 | IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL, | |
1084 | CONSTSTR("Responder, Aggressive-Mode Message 1"), | |
1085 | CONSTSTR("Failed to process Aggressive-Mode Message 1")); | |
1086 | } | |
65c25746 | 1087 | |
52b7d2ce A |
1088 | if (pbuf) |
1089 | vfree(pbuf); | |
1090 | if (error) { | |
1091 | VPTRINIT(iph1->sa); | |
1092 | VPTRINIT(iph1->dhpub_p); | |
1093 | VPTRINIT(iph1->nonce_p); | |
1094 | VPTRINIT(iph1->id_p); | |
1095 | oakley_delcert(iph1->cr_p); | |
1096 | iph1->cr_p = NULL; | |
1097 | } | |
1098 | ||
1099 | return error; | |
1100 | } | |
1101 | ||
1102 | /* | |
1103 | * send to initiator | |
1104 | * psk: HDR, SA, KE, Nr, IDr1, HASH_R | |
1105 | * sig: HDR, SA, KE, Nr, IDr1, [ CR, ] [ CERT, ] SIG_R | |
1106 | * gssapi: HDR, SA, KE, Nr, IDr1, GSSr, HASH_R | |
1107 | * rsa: HDR, SA, KE, <IDr1_b>PubKey_i, <Nr_b>PubKey_i, HASH_R | |
1108 | * rev: HDR, SA, <Nr_b>PubKey_i, <KE_b>Ke_r, <IDir_b>Ke_r, HASH_R | |
1109 | */ | |
1110 | int | |
65c25746 A |
1111 | agg_r2send(iph1, msg) |
1112 | phase1_handle_t *iph1; | |
52b7d2ce A |
1113 | vchar_t *msg; |
1114 | { | |
1115 | struct payload_list *plist = NULL; | |
1116 | int need_cr = 0; | |
1117 | int need_cert = 0; | |
1118 | vchar_t *cr = NULL; | |
52b7d2ce A |
1119 | int error = -1; |
1120 | #ifdef ENABLE_HYBRID | |
1121 | vchar_t *xauth_vid = NULL; | |
1122 | vchar_t *unity_vid = NULL; | |
1123 | #endif | |
1124 | #ifdef ENABLE_NATT | |
1125 | vchar_t *vid_natt = NULL; | |
1126 | vchar_t *natd[2] = { NULL, NULL }; | |
1127 | #endif | |
1128 | #ifdef ENABLE_DPD | |
1129 | vchar_t *vid_dpd = NULL; | |
1130 | #endif | |
d1e348cf A |
1131 | #ifdef ENABLE_FRAG |
1132 | vchar_t *vid_frag = NULL; | |
1133 | #endif | |
52b7d2ce | 1134 | |
65c25746 A |
1135 | /* validity check */ |
1136 | if (iph1->status != IKEV1_STATE_AGG_R_MSG1RCVD) { | |
1137 | plog(ASL_LEVEL_ERR, | |
1138 | "status mismatched %d.\n", iph1->status); | |
52b7d2ce A |
1139 | goto end; |
1140 | } | |
1141 | ||
1142 | /* set responder's cookie */ | |
1143 | isakmp_newcookie((caddr_t)&iph1->index.r_ck, iph1->remote, iph1->local); | |
1144 | ||
1145 | /* make ID payload into isakmp status */ | |
d1e348cf | 1146 | if (ipsecdoi_setid1(iph1) < 0) { |
65c25746 | 1147 | plog(ASL_LEVEL_ERR, |
d1e348cf | 1148 | "failed to set ID"); |
52b7d2ce | 1149 | goto end; |
d1e348cf | 1150 | } |
52b7d2ce A |
1151 | |
1152 | /* generate DH public value */ | |
e8d9021d | 1153 | #ifdef HAVE_OPENSSL |
52b7d2ce | 1154 | if (oakley_dh_generate(iph1->rmconf->dhgrp, |
e8d9021d A |
1155 | &iph1->dhpub, &iph1->dhpriv) < 0) { |
1156 | #else | |
1157 | if (oakley_dh_generate(iph1->rmconf->dhgrp, | |
1158 | &iph1->dhpub, &iph1->publicKeySize, &iph1->dhC) < 0) { | |
1159 | #endif | |
65c25746 | 1160 | plog(ASL_LEVEL_ERR, |
d1e348cf | 1161 | "failed to generate DH"); |
52b7d2ce | 1162 | goto end; |
d1e348cf | 1163 | } |
52b7d2ce A |
1164 | |
1165 | /* generate NONCE value */ | |
1166 | iph1->nonce = eay_set_random(iph1->rmconf->nonce_size); | |
d1e348cf | 1167 | if (iph1->nonce == NULL) { |
65c25746 | 1168 | plog(ASL_LEVEL_ERR, |
d1e348cf | 1169 | "failed to generate NONCE"); |
52b7d2ce | 1170 | goto end; |
d1e348cf | 1171 | } |
52b7d2ce A |
1172 | |
1173 | /* compute sharing secret of DH */ | |
e8d9021d A |
1174 | #ifdef HAVE_OPENSSL |
1175 | if (oakley_dh_compute(iph1->approval->dhgrp, iph1->dhpub, | |
1176 | iph1->dhpriv, iph1->dhpub_p, &iph1->dhgxy) < 0) { | |
1177 | #else | |
65c25746 | 1178 | if (oakley_dh_compute(iph1->approval->dhgrp, iph1->dhpub_p, iph1->publicKeySize, &iph1->dhgxy, &iph1->dhC) < 0) { |
e8d9021d | 1179 | #endif |
65c25746 | 1180 | plog(ASL_LEVEL_ERR, |
d1e348cf | 1181 | "failed to compute DH"); |
52b7d2ce | 1182 | goto end; |
d1e348cf | 1183 | } |
52b7d2ce A |
1184 | |
1185 | /* generate SKEYIDs & IV & final cipher key */ | |
d1e348cf | 1186 | if (oakley_skeyid(iph1) < 0) { |
65c25746 | 1187 | plog(ASL_LEVEL_ERR, |
d1e348cf | 1188 | "failed to generate SKEYID"); |
52b7d2ce | 1189 | goto end; |
d1e348cf A |
1190 | } |
1191 | if (oakley_skeyid_dae(iph1) < 0) { | |
65c25746 | 1192 | plog(ASL_LEVEL_ERR, |
d1e348cf | 1193 | "failed to generate SKEYID-DAE"); |
52b7d2ce | 1194 | goto end; |
d1e348cf A |
1195 | } |
1196 | if (oakley_compute_enckey(iph1) < 0) { | |
65c25746 | 1197 | plog(ASL_LEVEL_ERR, |
d1e348cf | 1198 | "failed to generate ENCKEY"); |
52b7d2ce | 1199 | goto end; |
d1e348cf A |
1200 | } |
1201 | if (oakley_newiv(iph1) < 0) { | |
65c25746 | 1202 | plog(ASL_LEVEL_ERR, |
d1e348cf | 1203 | "failed to generate IV"); |
52b7d2ce | 1204 | goto end; |
d1e348cf | 1205 | } |
52b7d2ce | 1206 | |
52b7d2ce | 1207 | /* generate HASH to send */ |
65c25746 | 1208 | plog(ASL_LEVEL_DEBUG, "generate HASH_R\n"); |
52b7d2ce A |
1209 | iph1->hash = oakley_ph1hash_common(iph1, GENERATE); |
1210 | if (iph1->hash == NULL) { | |
65c25746 | 1211 | plog(ASL_LEVEL_ERR, |
d1e348cf | 1212 | "failed to generate GSS HASH"); |
52b7d2ce A |
1213 | goto end; |
1214 | } | |
1215 | ||
1216 | /* create CR if need */ | |
1217 | if (iph1->rmconf->send_cr | |
65c25746 | 1218 | && oakley_needcr(iph1->approval->authmethod)) { |
52b7d2ce A |
1219 | need_cr = 1; |
1220 | cr = oakley_getcr(iph1); | |
1221 | if (cr == NULL) { | |
65c25746 | 1222 | plog(ASL_LEVEL_ERR, |
d1e348cf | 1223 | "failed to get CR.\n"); |
52b7d2ce A |
1224 | goto end; |
1225 | } | |
1226 | } | |
1227 | ||
1228 | #ifdef ENABLE_NATT | |
1229 | /* Has the peer announced NAT-T? */ | |
1230 | if (NATT_AVAILABLE(iph1)) { | |
1231 | /* set chosen VID */ | |
1232 | vid_natt = set_vendorid(iph1->natt_options->version); | |
1233 | ||
1234 | /* generate NAT-D payloads */ | |
65c25746 | 1235 | plog (ASL_LEVEL_INFO, "Adding remote and local NAT-D payloads.\n"); |
52b7d2ce | 1236 | if ((natd[0] = natt_hash_addr (iph1, iph1->remote)) == NULL) { |
65c25746 | 1237 | plog(ASL_LEVEL_ERR, |
85f41bec | 1238 | "NAT-D hashing failed for %s\n", saddr2str((struct sockaddr *)iph1->remote)); |
52b7d2ce A |
1239 | goto end; |
1240 | } | |
1241 | ||
1242 | if ((natd[1] = natt_hash_addr (iph1, iph1->local)) == NULL) { | |
65c25746 | 1243 | plog(ASL_LEVEL_ERR, |
85f41bec | 1244 | "NAT-D hashing failed for %s\n", saddr2str((struct sockaddr *)iph1->local)); |
52b7d2ce A |
1245 | goto end; |
1246 | } | |
1247 | } | |
1248 | #endif | |
1249 | #ifdef ENABLE_DPD | |
1250 | /* Only send DPD support if remote announced DPD and if DPD support is active */ | |
1251 | if (iph1->dpd_support && iph1->rmconf->dpd) | |
1252 | vid_dpd = set_vendorid(VENDORID_DPD); | |
1253 | #endif | |
d1e348cf A |
1254 | #ifdef ENABLE_FRAG |
1255 | if (iph1->frag) { | |
1256 | vid_frag = set_vendorid(VENDORID_FRAG); | |
1257 | if (vid_frag != NULL) | |
1258 | vid_frag = isakmp_frag_addcap(vid_frag, | |
1259 | VENDORID_FRAG_AGG); | |
1260 | if (vid_frag == NULL) | |
65c25746 | 1261 | plog(ASL_LEVEL_ERR, |
d1e348cf A |
1262 | "Frag vendorID construction failed\n"); |
1263 | } | |
1264 | #endif | |
52b7d2ce | 1265 | |
d1e348cf | 1266 | switch (AUTHMETHOD(iph1)) { |
52b7d2ce | 1267 | case OAKLEY_ATTR_AUTH_METHOD_PSKEY: |
d1e348cf A |
1268 | #ifdef ENABLE_HYBRID |
1269 | case OAKLEY_ATTR_AUTH_METHOD_XAUTH_PSKEY_R: | |
1270 | #endif | |
52b7d2ce A |
1271 | /* set SA payload to reply */ |
1272 | plist = isakmp_plist_append(plist, iph1->sa_ret, ISAKMP_NPTYPE_SA); | |
1273 | ||
1274 | /* create isakmp KE payload */ | |
1275 | plist = isakmp_plist_append(plist, iph1->dhpub, ISAKMP_NPTYPE_KE); | |
1276 | ||
1277 | /* create isakmp NONCE payload */ | |
1278 | plist = isakmp_plist_append(plist, iph1->nonce, ISAKMP_NPTYPE_NONCE); | |
1279 | ||
1280 | /* create isakmp ID payload */ | |
1281 | plist = isakmp_plist_append(plist, iph1->id, ISAKMP_NPTYPE_ID); | |
1282 | ||
1283 | /* create isakmp HASH payload */ | |
d1e348cf A |
1284 | plist = isakmp_plist_append(plist, |
1285 | iph1->hash, ISAKMP_NPTYPE_HASH); | |
52b7d2ce A |
1286 | |
1287 | /* create isakmp CR payload if needed */ | |
1288 | if (need_cr) | |
1289 | plist = isakmp_plist_append(plist, cr, ISAKMP_NPTYPE_CR); | |
1290 | break; | |
52b7d2ce A |
1291 | case OAKLEY_ATTR_AUTH_METHOD_RSASIG: |
1292 | #ifdef ENABLE_HYBRID | |
d1e348cf | 1293 | case OAKLEY_ATTR_AUTH_METHOD_HYBRID_RSA_R: |
d1e348cf | 1294 | case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSASIG_R: |
52b7d2ce A |
1295 | #endif |
1296 | /* XXX if there is CR or not ? */ | |
1297 | ||
d1e348cf | 1298 | if (oakley_getmycert(iph1) < 0) { |
65c25746 | 1299 | plog(ASL_LEVEL_ERR, |
d1e348cf | 1300 | "failed to get mycert"); |
52b7d2ce | 1301 | goto end; |
d1e348cf | 1302 | } |
52b7d2ce | 1303 | |
d1e348cf | 1304 | if (oakley_getsign(iph1) < 0) { |
65c25746 | 1305 | plog(ASL_LEVEL_ERR, |
d1e348cf | 1306 | "failed to get sign"); |
52b7d2ce | 1307 | goto end; |
d1e348cf | 1308 | } |
52b7d2ce A |
1309 | |
1310 | if (iph1->cert != NULL && iph1->rmconf->send_cert) | |
1311 | need_cert = 1; | |
1312 | ||
1313 | /* set SA payload to reply */ | |
1314 | plist = isakmp_plist_append(plist, iph1->sa_ret, ISAKMP_NPTYPE_SA); | |
1315 | ||
1316 | /* create isakmp KE payload */ | |
1317 | plist = isakmp_plist_append(plist, iph1->dhpub, ISAKMP_NPTYPE_KE); | |
1318 | ||
1319 | /* create isakmp NONCE payload */ | |
1320 | plist = isakmp_plist_append(plist, iph1->nonce, ISAKMP_NPTYPE_NONCE); | |
1321 | ||
1322 | /* add ID payload */ | |
1323 | plist = isakmp_plist_append(plist, iph1->id, ISAKMP_NPTYPE_ID); | |
1324 | ||
1325 | /* add CERT payload if there */ | |
1326 | if (need_cert) | |
1327 | plist = isakmp_plist_append(plist, iph1->cert->pl, ISAKMP_NPTYPE_CERT); | |
1328 | ||
1329 | /* add SIG payload */ | |
1330 | plist = isakmp_plist_append(plist, iph1->sig, ISAKMP_NPTYPE_SIG); | |
1331 | ||
d1e348cf A |
1332 | /* create isakmp CR payload if needed */ |
1333 | if (need_cr) | |
1334 | plist = isakmp_plist_append(plist, | |
1335 | cr, ISAKMP_NPTYPE_CR); | |
1336 | break; | |
1337 | ||
1338 | case OAKLEY_ATTR_AUTH_METHOD_RSAENC: | |
1339 | case OAKLEY_ATTR_AUTH_METHOD_RSAREV: | |
1340 | #ifdef ENABLE_HYBRID | |
1341 | case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAENC_R: | |
1342 | case OAKLEY_ATTR_AUTH_METHOD_XAUTH_RSAREV_R: | |
1343 | #endif | |
1344 | break; | |
d1e348cf | 1345 | } |
52b7d2ce A |
1346 | |
1347 | #ifdef ENABLE_HYBRID | |
1348 | if (iph1->mode_cfg->flags & ISAKMP_CFG_VENDORID_XAUTH) { | |
65c25746 | 1349 | plog (ASL_LEVEL_INFO, "Adding xauth VID payload.\n"); |
52b7d2ce | 1350 | if ((xauth_vid = set_vendorid(VENDORID_XAUTH)) == NULL) { |
65c25746 | 1351 | plog(ASL_LEVEL_ERR, |
52b7d2ce A |
1352 | "Cannot create Xauth vendor ID\n"); |
1353 | goto end; | |
1354 | } | |
1355 | plist = isakmp_plist_append(plist, | |
1356 | xauth_vid, ISAKMP_NPTYPE_VID); | |
1357 | } | |
1358 | ||
1359 | if (iph1->mode_cfg->flags & ISAKMP_CFG_VENDORID_UNITY) { | |
1360 | if ((unity_vid = set_vendorid(VENDORID_UNITY)) == NULL) { | |
65c25746 | 1361 | plog(ASL_LEVEL_ERR, |
52b7d2ce A |
1362 | "Cannot create Unity vendor ID\n"); |
1363 | goto end; | |
1364 | } | |
1365 | plist = isakmp_plist_append(plist, | |
1366 | unity_vid, ISAKMP_NPTYPE_VID); | |
1367 | } | |
1368 | #endif | |
1369 | ||
52b7d2ce A |
1370 | #ifdef ENABLE_NATT |
1371 | /* append NAT-T payloads */ | |
1372 | if (vid_natt) { | |
1373 | /* chosen VID */ | |
1374 | plist = isakmp_plist_append(plist, vid_natt, ISAKMP_NPTYPE_VID); | |
1375 | /* NAT-D */ | |
52b7d2ce A |
1376 | /* old Apple version sends natd payloads in the wrong order */ |
1377 | if (iph1->natt_options->version == VENDORID_NATT_APPLE) { | |
1378 | plist = isakmp_plist_append(plist, natd[1], iph1->natt_options->payload_nat_d); | |
1379 | plist = isakmp_plist_append(plist, natd[0], iph1->natt_options->payload_nat_d); | |
1380 | } else | |
52b7d2ce A |
1381 | { |
1382 | plist = isakmp_plist_append(plist, natd[0], iph1->natt_options->payload_nat_d); | |
1383 | plist = isakmp_plist_append(plist, natd[1], iph1->natt_options->payload_nat_d); | |
1384 | } | |
1385 | } | |
1386 | #endif | |
d1e348cf A |
1387 | |
1388 | #ifdef ENABLE_FRAG | |
1389 | if (vid_frag) | |
1390 | plist = isakmp_plist_append(plist, vid_frag, ISAKMP_NPTYPE_VID); | |
1391 | #endif | |
1392 | ||
52b7d2ce A |
1393 | #ifdef ENABLE_DPD |
1394 | if (vid_dpd) | |
1395 | plist = isakmp_plist_append(plist, vid_dpd, ISAKMP_NPTYPE_VID); | |
1396 | #endif | |
1397 | ||
1398 | iph1->sendbuf = isakmp_plist_set_all (&plist, iph1); | |
1399 | ||
1400 | #ifdef HAVE_PRINT_ISAKMP_C | |
1401 | isakmp_printpacket(iph1->sendbuf, iph1->local, iph1->remote, 1); | |
1402 | #endif | |
1403 | ||
1404 | /* send the packet, add to the schedule to resend */ | |
1405 | iph1->retry_counter = iph1->rmconf->retry_counter; | |
d1e348cf | 1406 | if (isakmp_ph1resend(iph1) == -1) { |
65c25746 | 1407 | plog(ASL_LEVEL_ERR , |
d1e348cf | 1408 | "failed to send packet"); |
52b7d2ce | 1409 | goto end; |
d1e348cf | 1410 | } |
52b7d2ce A |
1411 | |
1412 | /* the sending message is added to the received-list. */ | |
65c25746 A |
1413 | if (ike_session_add_recvdpkt(iph1->remote, iph1->local, iph1->sendbuf, msg, |
1414 | PH1_NON_ESP_EXTRA_LEN(iph1, iph1->sendbuf), PH1_FRAG_FLAGS(iph1)) == -1) { | |
1415 | plog(ASL_LEVEL_ERR , | |
52b7d2ce A |
1416 | "failed to add a response packet to the tree.\n"); |
1417 | goto end; | |
1418 | } | |
1419 | ||
65c25746 | 1420 | fsm_set_state(&iph1->status, IKEV1_STATE_AGG_R_MSG2SENT); |
52b7d2ce A |
1421 | |
1422 | #ifdef ENABLE_VPNCONTROL_PORT | |
1423 | vpncontrol_notify_phase_change(1, FROM_LOCAL, iph1, NULL); | |
1424 | #endif | |
1425 | ||
1426 | error = 0; | |
1427 | ||
d1e348cf A |
1428 | IPSECSESSIONTRACEREVENT(iph1->parent_session, |
1429 | IPSECSESSIONEVENTCODE_IKE_PACKET_TX_SUCC, | |
1430 | CONSTSTR("Responder, Aggressive-Mode message 2"), | |
1431 | CONSTSTR(NULL)); | |
1432 | ||
52b7d2ce | 1433 | end: |
d1e348cf A |
1434 | if (error) { |
1435 | IPSECSESSIONTRACEREVENT(iph1->parent_session, | |
1436 | IPSECSESSIONEVENTCODE_IKE_PACKET_TX_FAIL, | |
1437 | CONSTSTR("Responder, Aggressive-Mode Message 2"), | |
1438 | CONSTSTR("Failed to process Aggressive-Mode Message 2")); | |
1439 | } | |
52b7d2ce A |
1440 | if (cr) |
1441 | vfree(cr); | |
52b7d2ce A |
1442 | #ifdef ENABLE_HYBRID |
1443 | if (xauth_vid) | |
1444 | vfree(xauth_vid); | |
1445 | if (unity_vid) | |
1446 | vfree(unity_vid); | |
1447 | #endif | |
d1e348cf A |
1448 | #ifdef ENABLE_NATT |
1449 | if (vid_natt) | |
1450 | vfree(vid_natt); | |
1451 | if (natd[0]) | |
1452 | vfree(natd[0]); | |
1453 | if (natd[1]) | |
1454 | vfree(natd[1]); | |
1455 | #endif | |
52b7d2ce A |
1456 | #ifdef ENABLE_DPD |
1457 | if (vid_dpd) | |
1458 | vfree(vid_dpd); | |
1459 | #endif | |
d1e348cf A |
1460 | #ifdef ENABLE_FRAG |
1461 | if (vid_frag) | |
1462 | vfree(vid_frag); | |
1463 | #endif | |
52b7d2ce A |
1464 | |
1465 | return error; | |
1466 | } | |
1467 | ||
1468 | /* | |
1469 | * receive from initiator | |
1470 | * psk: HDR, HASH_I | |
1471 | * gssapi: HDR, HASH_I | |
1472 | * sig: HDR, [ CERT, ] SIG_I | |
1473 | * rsa: HDR, HASH_I | |
1474 | * rev: HDR, HASH_I | |
1475 | */ | |
1476 | int | |
65c25746 A |
1477 | agg_r3recv(iph1, msg0) |
1478 | phase1_handle_t *iph1; | |
52b7d2ce A |
1479 | vchar_t *msg0; |
1480 | { | |
1481 | vchar_t *msg = NULL; | |
1482 | vchar_t *pbuf = NULL; | |
1483 | struct isakmp_parse_t *pa; | |
1484 | int error = -1; | |
1485 | int ptype; | |
1486 | ||
1487 | #ifdef ENABLE_NATT | |
1488 | int natd_seq = 0; | |
1489 | #endif | |
e8d9021d | 1490 | int received_cert = 0; |
52b7d2ce | 1491 | |
65c25746 A |
1492 | /* validity check */ |
1493 | if (iph1->status != IKEV1_STATE_AGG_R_MSG2SENT) { | |
1494 | plog(ASL_LEVEL_ERR, | |
1495 | "status mismatched %d.\n", iph1->status); | |
52b7d2ce A |
1496 | goto end; |
1497 | } | |
1498 | ||
1499 | /* decrypting if need. */ | |
1500 | /* XXX configurable ? */ | |
1501 | if (ISSET(((struct isakmp *)msg0->v)->flags, ISAKMP_FLAG_E)) { | |
1502 | msg = oakley_do_decrypt(iph1, msg0, | |
1503 | iph1->ivm->iv, iph1->ivm->ive); | |
d1e348cf | 1504 | if (msg == NULL) { |
65c25746 | 1505 | plog(ASL_LEVEL_ERR, |
d1e348cf | 1506 | "failed to decrypt msg"); |
52b7d2ce | 1507 | goto end; |
d1e348cf | 1508 | } |
52b7d2ce A |
1509 | } else |
1510 | msg = vdup(msg0); | |
1511 | ||
1512 | /* validate the type of next payload */ | |
1513 | pbuf = isakmp_parse(msg); | |
d1e348cf | 1514 | if (pbuf == NULL) { |
65c25746 | 1515 | plog(ASL_LEVEL_ERR, |
d1e348cf | 1516 | "failed to parse msg"); |
52b7d2ce | 1517 | goto end; |
d1e348cf | 1518 | } |
52b7d2ce A |
1519 | |
1520 | iph1->pl_hash = NULL; | |
1521 | ||
85f41bec | 1522 | for (pa = ALIGNED_CAST(struct isakmp_parse_t *)pbuf->v; |
52b7d2ce A |
1523 | pa->type != ISAKMP_NPTYPE_NONE; |
1524 | pa++) { | |
1525 | ||
1526 | switch (pa->type) { | |
1527 | case ISAKMP_NPTYPE_HASH: | |
1528 | iph1->pl_hash = (struct isakmp_pl_hash *)pa->ptr; | |
1529 | break; | |
1530 | case ISAKMP_NPTYPE_VID: | |
1531 | (void)check_vendorid(pa->ptr); | |
1532 | break; | |
1533 | case ISAKMP_NPTYPE_CERT: | |
d1e348cf | 1534 | if (oakley_savecert(iph1, pa->ptr) < 0) { |
65c25746 | 1535 | plog(ASL_LEVEL_ERR, |
d1e348cf | 1536 | "failed to process CERT payload"); |
52b7d2ce | 1537 | goto end; |
d1e348cf | 1538 | } |
e8d9021d | 1539 | received_cert = 1; |
52b7d2ce A |
1540 | break; |
1541 | case ISAKMP_NPTYPE_SIG: | |
d1e348cf | 1542 | if (isakmp_p2ph(&iph1->sig_p, pa->ptr) < 0) { |
65c25746 | 1543 | plog(ASL_LEVEL_ERR, |
d1e348cf | 1544 | "failed to process SIG payload"); |
52b7d2ce | 1545 | goto end; |
d1e348cf | 1546 | } |
52b7d2ce A |
1547 | break; |
1548 | case ISAKMP_NPTYPE_N: | |
1549 | isakmp_check_notify(pa->ptr, iph1); | |
1550 | break; | |
1551 | ||
1552 | #ifdef ENABLE_NATT | |
1553 | case ISAKMP_NPTYPE_NATD_DRAFT: | |
1554 | case ISAKMP_NPTYPE_NATD_RFC: | |
52b7d2ce A |
1555 | if (NATT_AVAILABLE(iph1) && iph1->natt_options != NULL && |
1556 | pa->type == iph1->natt_options->payload_nat_d) | |
1557 | { | |
1558 | vchar_t *natd_received = NULL; | |
1559 | int natd_verified; | |
1560 | ||
d1e348cf | 1561 | if (isakmp_p2ph (&natd_received, pa->ptr) < 0) { |
65c25746 | 1562 | plog(ASL_LEVEL_ERR, |
d1e348cf | 1563 | "failed to process NATD payload"); |
52b7d2ce | 1564 | goto end; |
d1e348cf | 1565 | } |
52b7d2ce A |
1566 | |
1567 | if (natd_seq == 0) | |
1568 | iph1->natt_flags |= NAT_DETECTED; | |
1569 | ||
1570 | natd_verified = natt_compare_addr_hash (iph1, | |
1571 | natd_received, natd_seq++); | |
1572 | ||
65c25746 | 1573 | plog (ASL_LEVEL_INFO, "NAT-D payload #%d %s\n", |
52b7d2ce A |
1574 | natd_seq - 1, |
1575 | natd_verified ? "verified" : "doesn't match"); | |
1576 | ||
1577 | vfree (natd_received); | |
1578 | break; | |
1579 | } | |
1580 | /* %%%% Be lenient here - some servers send natd payloads */ | |
1581 | /* when no nat is detected */ | |
1582 | break; | |
1583 | #endif | |
1584 | ||
1585 | default: | |
1586 | /* don't send information, see isakmp_ident_r1() */ | |
65c25746 | 1587 | plog(ASL_LEVEL_ERR, |
52b7d2ce A |
1588 | "ignore the packet, " |
1589 | "received unexpecting payload type %d.\n", | |
1590 | pa->type); | |
1591 | goto end; | |
1592 | } | |
1593 | } | |
1594 | ||
1595 | #ifdef ENABLE_NATT | |
1596 | if (NATT_AVAILABLE(iph1)) | |
65c25746 | 1597 | plog (ASL_LEVEL_INFO, "NAT %s %s%s\n", |
52b7d2ce A |
1598 | iph1->natt_flags & NAT_DETECTED ? |
1599 | "detected:" : "not detected", | |
1600 | iph1->natt_flags & NAT_DETECTED_ME ? "ME " : "", | |
1601 | iph1->natt_flags & NAT_DETECTED_PEER ? "PEER" : ""); | |
1602 | #endif | |
1603 | ||
e8d9021d A |
1604 | if (received_cert) { |
1605 | oakley_verify_certid(iph1); | |
1606 | } | |
1607 | ||
52b7d2ce A |
1608 | /* validate authentication value */ |
1609 | ptype = oakley_validate_auth(iph1); | |
1610 | if (ptype != 0) { | |
d1e348cf A |
1611 | IPSECSESSIONTRACEREVENT(iph1->parent_session, |
1612 | IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_FAIL, | |
1613 | CONSTSTR("Responder, Aggressive-Mode Message 3"), | |
1614 | CONSTSTR("Failed to authenticate Aggressive-Mode Message 3")); | |
52b7d2ce A |
1615 | if (ptype == -1) { |
1616 | /* message printed inner oakley_validate_auth() */ | |
1617 | goto end; | |
1618 | } | |
52b7d2ce A |
1619 | isakmp_info_send_n1(iph1, ptype, NULL); |
1620 | goto end; | |
1621 | } | |
d1e348cf A |
1622 | IPSECSESSIONTRACEREVENT(iph1->parent_session, |
1623 | IPSECSESSIONEVENTCODE_IKEV1_PH1_AUTH_SUCC, | |
1624 | CONSTSTR("Responder, Aggressive-Mode Message 3"), | |
1625 | CONSTSTR(NULL)); | |
52b7d2ce | 1626 | |
65c25746 | 1627 | fsm_set_state(&iph1->status, IKEV1_STATE_AGG_R_MSG3RCVD); |
52b7d2ce A |
1628 | |
1629 | error = 0; | |
1630 | ||
d1e348cf A |
1631 | IPSECSESSIONTRACEREVENT(iph1->parent_session, |
1632 | IPSECSESSIONEVENTCODE_IKE_PACKET_RX_SUCC, | |
1633 | CONSTSTR("Responder, Aggressive-Mode message 3"), | |
1634 | CONSTSTR(NULL)); | |
1635 | ||
52b7d2ce | 1636 | end: |
d1e348cf A |
1637 | if (error) { |
1638 | IPSECSESSIONTRACEREVENT(iph1->parent_session, | |
1639 | IPSECSESSIONEVENTCODE_IKE_PACKET_RX_FAIL, | |
1640 | CONSTSTR("Responder, Aggressive-Mode Message 3"), | |
1641 | CONSTSTR("Failed to process Aggressive-Mode Message 3")); | |
1642 | } | |
52b7d2ce A |
1643 | if (pbuf) |
1644 | vfree(pbuf); | |
1645 | if (msg) | |
1646 | vfree(msg); | |
1647 | if (error) { | |
1648 | oakley_delcert(iph1->cert_p); | |
1649 | iph1->cert_p = NULL; | |
1650 | oakley_delcert(iph1->crl_p); | |
1651 | iph1->crl_p = NULL; | |
1652 | VPTRINIT(iph1->sig_p); | |
1653 | } | |
1654 | ||
1655 | return error; | |
1656 | } | |
1657 | ||
1658 | /* | |
1659 | * status update and establish isakmp sa. | |
1660 | */ | |
1661 | int | |
65c25746 A |
1662 | agg_rfinalize(iph1, msg) |
1663 | phase1_handle_t *iph1; | |
52b7d2ce A |
1664 | vchar_t *msg; |
1665 | { | |
1666 | int error = -1; | |
1667 | ||
65c25746 A |
1668 | /* validity check */ |
1669 | if (iph1->status != IKEV1_STATE_AGG_R_MSG3RCVD) { | |
1670 | plog(ASL_LEVEL_ERR, | |
1671 | "status mismatched %d.\n", iph1->status); | |
52b7d2ce A |
1672 | goto end; |
1673 | } | |
1674 | ||
1675 | /* IV synchronized when packet encrypted. */ | |
1676 | /* see handler.h about IV synchronization. */ | |
1677 | if (ISSET(((struct isakmp *)msg->v)->flags, ISAKMP_FLAG_E)) | |
1678 | memcpy(iph1->ivm->iv->v, iph1->ivm->ive->v, iph1->ivm->iv->l); | |
1679 | ||
1680 | /* set encryption flag */ | |
1681 | iph1->flags |= ISAKMP_FLAG_E; | |
1682 | ||
65c25746 | 1683 | fsm_set_state(&iph1->status, IKEV1_STATE_PHASE1_ESTABLISHED); |
52b7d2ce | 1684 | |
d1e348cf A |
1685 | IPSECSESSIONTRACEREVENT(iph1->parent_session, |
1686 | IPSECSESSIONEVENTCODE_IKEV1_PH1_RESP_SUCC, | |
1687 | CONSTSTR("Responder, Aggressive-Mode"), | |
1688 | CONSTSTR(NULL)); | |
1689 | ||
52b7d2ce A |
1690 | error = 0; |
1691 | ||
1692 | end: | |
1693 | return error; | |
1694 | } |