moved key derivation and management into keymat object
[strongswan.git] / src / charon / sa / tasks / child_create.c
1 /*
2 * Copyright (C) 2008 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * $Id$
18 */
19
20 #include "child_create.h"
21
22 #include <daemon.h>
23 #include <crypto/diffie_hellman.h>
24 #include <encoding/payloads/sa_payload.h>
25 #include <encoding/payloads/ke_payload.h>
26 #include <encoding/payloads/ts_payload.h>
27 #include <encoding/payloads/nonce_payload.h>
28 #include <encoding/payloads/notify_payload.h>
29 #include <processing/jobs/delete_ike_sa_job.h>
30
31
32 typedef struct private_child_create_t private_child_create_t;
33
34 /**
35 * Private members of a child_create_t task.
36 */
37 struct private_child_create_t {
38
39 /**
40 * Public methods and task_t interface.
41 */
42 child_create_t public;
43
44 /**
45 * Assigned IKE_SA.
46 */
47 ike_sa_t *ike_sa;
48
49 /**
50 * Are we the initiator?
51 */
52 bool initiator;
53
54 /**
55 * nonce chosen by us
56 */
57 chunk_t my_nonce;
58
59 /**
60 * nonce chosen by peer
61 */
62 chunk_t other_nonce;
63
64 /**
65 * config to create the CHILD_SA from
66 */
67 child_cfg_t *config;
68
69 /**
70 * list of proposal candidates
71 */
72 linked_list_t *proposals;
73
74 /**
75 * selected proposal to use for CHILD_SA
76 */
77 proposal_t *proposal;
78
79 /**
80 * traffic selectors for initiators side
81 */
82 linked_list_t *tsi;
83
84 /**
85 * traffic selectors for responders side
86 */
87 linked_list_t *tsr;
88
89 /**
90 * optional diffie hellman exchange
91 */
92 diffie_hellman_t *dh;
93
94 /**
95 * group used for DH exchange
96 */
97 diffie_hellman_group_t dh_group;
98
99 /**
100 * mode the new CHILD_SA uses (transport/tunnel/beet)
101 */
102 ipsec_mode_t mode;
103
104 /**
105 * IPComp transform to use
106 */
107 ipcomp_transform_t ipcomp;
108
109 /**
110 * IPComp transform proposed or accepted by the other peer
111 */
112 ipcomp_transform_t ipcomp_received;
113
114 /**
115 * Other Compression Parameter Index (CPI)
116 */
117 u_int16_t other_cpi;
118
119 /**
120 * reqid to use if we are rekeying
121 */
122 u_int32_t reqid;
123
124 /**
125 * CHILD_SA which gets established
126 */
127 child_sa_t *child_sa;
128
129 /**
130 * successfully established the CHILD?
131 */
132 bool established;
133 };
134
135 /**
136 * get the nonce from a message
137 */
138 static status_t get_nonce(message_t *message, chunk_t *nonce)
139 {
140 nonce_payload_t *payload;
141
142 payload = (nonce_payload_t*)message->get_payload(message, NONCE);
143 if (payload == NULL)
144 {
145 return FAILED;
146 }
147 *nonce = payload->get_nonce(payload);
148 return NEED_MORE;
149 }
150
151 /**
152 * generate a new nonce to include in a CREATE_CHILD_SA message
153 */
154 static status_t generate_nonce(chunk_t *nonce)
155 {
156 rng_t *rng;
157
158 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
159 if (!rng)
160 {
161 DBG1(DBG_IKE, "error generating nonce value, no RNG found");
162 return FAILED;
163 }
164 rng->allocate_bytes(rng, NONCE_SIZE, nonce);
165 rng->destroy(rng);
166 return SUCCESS;
167 }
168
169 /**
170 * Check a list of traffic selectors if any selector belongs to host
171 */
172 static bool ts_list_is_host(linked_list_t *list, host_t *host)
173 {
174 traffic_selector_t *ts;
175 bool is_host = TRUE;
176 iterator_t *iterator = list->create_iterator(list, TRUE);
177
178 while (is_host && iterator->iterate(iterator, (void**)&ts))
179 {
180 is_host = is_host && ts->is_host(ts, host);
181 }
182 iterator->destroy(iterator);
183 return is_host;
184 }
185
186 /**
187 * Install a CHILD_SA for usage, return value:
188 * - FAILED: no acceptable proposal
189 * - INVALID_ARG: diffie hellman group inacceptable
190 * - NOT_FOUND: TS inacceptable
191 */
192 static status_t select_and_install(private_child_create_t *this, bool no_dh)
193 {
194 prf_plus_t *prf_plus;
195 status_t status;
196 chunk_t nonce_i, nonce_r, secret, seed;
197 linked_list_t *my_ts, *other_ts;
198 host_t *me, *other, *other_vip, *my_vip;
199 keymat_t *keymat;
200
201 if (this->proposals == NULL)
202 {
203 DBG1(DBG_IKE, "SA payload missing in message");
204 return FAILED;
205 }
206 if (this->tsi == NULL || this->tsr == NULL)
207 {
208 DBG1(DBG_IKE, "TS payloads missing in message");
209 return NOT_FOUND;
210 }
211
212 if (this->initiator)
213 {
214 nonce_i = this->my_nonce;
215 nonce_r = this->other_nonce;
216 my_ts = this->tsi;
217 other_ts = this->tsr;
218 }
219 else
220 {
221 nonce_r = this->my_nonce;
222 nonce_i = this->other_nonce;
223 my_ts = this->tsr;
224 other_ts = this->tsi;
225 }
226
227 me = this->ike_sa->get_my_host(this->ike_sa);
228 other = this->ike_sa->get_other_host(this->ike_sa);
229 my_vip = this->ike_sa->get_virtual_ip(this->ike_sa, TRUE);
230 other_vip = this->ike_sa->get_virtual_ip(this->ike_sa, FALSE);
231
232 this->proposal = this->config->select_proposal(this->config, this->proposals,
233 no_dh);
234 if (this->proposal == NULL)
235 {
236 DBG1(DBG_IKE, "no acceptable proposal found");
237 return FAILED;
238 }
239
240 if (!this->proposal->has_dh_group(this->proposal, this->dh_group))
241 {
242 u_int16_t group;
243
244 if (this->proposal->get_algorithm(this->proposal, DIFFIE_HELLMAN_GROUP,
245 &group, NULL))
246 {
247 DBG1(DBG_IKE, "DH group %N inacceptable, requesting %N",
248 diffie_hellman_group_names, this->dh_group,
249 diffie_hellman_group_names, group);
250 this->dh_group = group;
251 return INVALID_ARG;
252 }
253 else
254 {
255 DBG1(DBG_IKE, "no acceptable proposal found");
256 return FAILED;
257 }
258 }
259
260 if (my_vip == NULL)
261 {
262 my_vip = me;
263 }
264 if (other_vip == NULL)
265 {
266 other_vip = other;
267 }
268
269 my_ts = this->config->get_traffic_selectors(this->config, TRUE, my_ts,
270 my_vip);
271 other_ts = this->config->get_traffic_selectors(this->config, FALSE, other_ts,
272 other_vip);
273
274 if (my_ts->get_count(my_ts) == 0 || other_ts->get_count(other_ts) == 0)
275 {
276 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
277 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
278 DBG1(DBG_IKE, "no acceptable traffic selectors found");
279 return NOT_FOUND;
280 }
281
282 this->tsr->destroy_offset(this->tsr, offsetof(traffic_selector_t, destroy));
283 this->tsi->destroy_offset(this->tsi, offsetof(traffic_selector_t, destroy));
284 if (this->initiator)
285 {
286 this->tsi = my_ts;
287 this->tsr = other_ts;
288 }
289 else
290 {
291 this->tsr = my_ts;
292 this->tsi = other_ts;
293 }
294
295 if (!this->initiator)
296 {
297 /* check if requested mode is acceptable, downgrade if required */
298 switch (this->mode)
299 {
300 case MODE_TRANSPORT:
301 if (!ts_list_is_host(this->tsi, other) ||
302 !ts_list_is_host(this->tsr, me))
303 {
304 this->mode = MODE_TUNNEL;
305 DBG1(DBG_IKE, "not using tranport mode, not host-to-host");
306 }
307 else if (this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY))
308 {
309 this->mode = MODE_TUNNEL;
310 DBG1(DBG_IKE, "not using tranport mode, connection NATed");
311 }
312 break;
313 case MODE_BEET:
314 if (!ts_list_is_host(this->tsi, NULL) ||
315 !ts_list_is_host(this->tsr, NULL))
316 {
317 this->mode = MODE_TUNNEL;
318 DBG1(DBG_IKE, "not using BEET mode, not host-to-host");
319 }
320 break;
321 default:
322 break;
323 }
324 }
325
326 if (this->dh)
327 {
328 if (this->dh->get_shared_secret(this->dh, &secret) != SUCCESS)
329 {
330 DBG1(DBG_IKE, "DH exchange incomplete");
331 return FAILED;
332 }
333 DBG3(DBG_IKE, "DH secret %B", &secret);
334 seed = chunk_cata("mcc", secret, nonce_i, nonce_r);
335 }
336 else
337 {
338 seed = chunk_cata("cc", nonce_i, nonce_r);
339 }
340
341 if (this->ipcomp != IPCOMP_NONE)
342 {
343 this->child_sa->activate_ipcomp(this->child_sa, this->ipcomp,
344 this->other_cpi);
345 }
346
347 status = this->child_sa->add_policies(this->child_sa, my_ts, other_ts,
348 this->mode, this->proposal->get_protocol(this->proposal));
349 if (status != SUCCESS)
350 {
351 DBG1(DBG_IKE, "unable to install IPsec policies (SPD) in kernel");
352 return NOT_FOUND;
353 }
354
355 keymat = this->ike_sa->get_keymat(this->ike_sa);
356 prf_plus = prf_plus_create(keymat->get_child_prf(keymat), seed);
357 if (this->initiator)
358 {
359 status = this->child_sa->update(this->child_sa, this->proposal,
360 this->mode, prf_plus);
361 }
362 else
363 {
364 status = this->child_sa->add(this->child_sa, this->proposal,
365 this->mode, prf_plus);
366 }
367 prf_plus->destroy(prf_plus);
368
369 if (status != SUCCESS)
370 {
371 DBG1(DBG_IKE, "unable to install IPsec SA (SAD) in kernel");
372 return FAILED;
373 }
374 /* add to IKE_SA, and remove from task */
375 this->child_sa->set_state(this->child_sa, CHILD_INSTALLED);
376 this->ike_sa->add_child_sa(this->ike_sa, this->child_sa);
377 this->established = TRUE;
378 return SUCCESS;
379 }
380
381 /**
382 * build the payloads for the message
383 */
384 static void build_payloads(private_child_create_t *this, message_t *message)
385 {
386 sa_payload_t *sa_payload;
387 nonce_payload_t *nonce_payload;
388 ke_payload_t *ke_payload;
389 ts_payload_t *ts_payload;
390
391 /* add SA payload */
392 if (this->initiator)
393 {
394 sa_payload = sa_payload_create_from_proposal_list(this->proposals);
395 }
396 else
397 {
398 sa_payload = sa_payload_create_from_proposal(this->proposal);
399 }
400 message->add_payload(message, (payload_t*)sa_payload);
401
402 /* add nonce payload if not in IKE_AUTH */
403 if (message->get_exchange_type(message) == CREATE_CHILD_SA)
404 {
405 nonce_payload = nonce_payload_create();
406 nonce_payload->set_nonce(nonce_payload, this->my_nonce);
407 message->add_payload(message, (payload_t*)nonce_payload);
408 }
409
410 /* diffie hellman exchange, if PFS enabled */
411 if (this->dh)
412 {
413 ke_payload = ke_payload_create_from_diffie_hellman(this->dh);
414 message->add_payload(message, (payload_t*)ke_payload);
415 }
416
417 /* add TSi/TSr payloads */
418 ts_payload = ts_payload_create_from_traffic_selectors(TRUE, this->tsi);
419 message->add_payload(message, (payload_t*)ts_payload);
420 ts_payload = ts_payload_create_from_traffic_selectors(FALSE, this->tsr);
421 message->add_payload(message, (payload_t*)ts_payload);
422
423 /* add a notify if we are not in tunnel mode */
424 switch (this->mode)
425 {
426 case MODE_TRANSPORT:
427 message->add_notify(message, FALSE, USE_TRANSPORT_MODE, chunk_empty);
428 break;
429 case MODE_BEET:
430 message->add_notify(message, FALSE, USE_BEET_MODE, chunk_empty);
431 break;
432 default:
433 break;
434 }
435 }
436
437 /**
438 * Adds an IPCOMP_SUPPORTED notify to the message, if possible
439 */
440 static void build_ipcomp_supported_notify(private_child_create_t *this,
441 message_t *message)
442 {
443 u_int16_t cpi;
444 u_int8_t tid;
445
446 if (this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY))
447 {
448 DBG1(DBG_IKE, "IPComp is not supported if either peer is natted, "
449 "IPComp disabled");
450 this->ipcomp = IPCOMP_NONE;
451 return;
452 }
453
454 cpi = this->child_sa->allocate_cpi(this->child_sa);
455 tid = this->ipcomp;
456 if (cpi)
457 {
458 message->add_notify(message, FALSE, IPCOMP_SUPPORTED,
459 chunk_cata("cc", chunk_from_thing(cpi), chunk_from_thing(tid)));
460 }
461 else
462 {
463 DBG1(DBG_IKE, "unable to allocate a CPI from kernel, IPComp disabled");
464 this->ipcomp = IPCOMP_NONE;
465 }
466 }
467
468 /**
469 * Read payloads from message
470 */
471 static void process_payloads(private_child_create_t *this, message_t *message)
472 {
473 iterator_t *iterator;
474 payload_t *payload;
475 sa_payload_t *sa_payload;
476 ke_payload_t *ke_payload;
477 ts_payload_t *ts_payload;
478 notify_payload_t *notify_payload;
479
480 /* defaults to TUNNEL mode */
481 this->mode = MODE_TUNNEL;
482
483 iterator = message->get_payload_iterator(message);
484 while (iterator->iterate(iterator, (void**)&payload))
485 {
486 switch (payload->get_type(payload))
487 {
488 case SECURITY_ASSOCIATION:
489 sa_payload = (sa_payload_t*)payload;
490 this->proposals = sa_payload->get_proposals(sa_payload);
491 break;
492 case KEY_EXCHANGE:
493 ke_payload = (ke_payload_t*)payload;
494 if (!this->initiator)
495 {
496 this->dh_group = ke_payload->get_dh_group_number(ke_payload);
497 this->dh = lib->crypto->create_dh(lib->crypto, this->dh_group);
498 }
499 if (this->dh)
500 {
501 this->dh->set_other_public_value(this->dh,
502 ke_payload->get_key_exchange_data(ke_payload));
503 }
504 break;
505 case TRAFFIC_SELECTOR_INITIATOR:
506 ts_payload = (ts_payload_t*)payload;
507 this->tsi = ts_payload->get_traffic_selectors(ts_payload);
508 break;
509 case TRAFFIC_SELECTOR_RESPONDER:
510 ts_payload = (ts_payload_t*)payload;
511 this->tsr = ts_payload->get_traffic_selectors(ts_payload);
512 break;
513 case NOTIFY:
514 notify_payload = (notify_payload_t*)payload;
515 switch (notify_payload ->get_notify_type(notify_payload ))
516 {
517 case USE_TRANSPORT_MODE:
518 this->mode = MODE_TRANSPORT;
519 break;
520 case USE_BEET_MODE:
521 this->mode = MODE_BEET;
522 break;
523 case IPCOMP_SUPPORTED:
524 {
525 chunk_t data = notify_payload->get_notification_data(notify_payload);
526 u_int16_t cpi = *(u_int16_t*)data.ptr;
527 ipcomp_transform_t ipcomp = (ipcomp_transform_t)(*(data.ptr + 2));
528 switch(ipcomp)
529 {
530 case IPCOMP_DEFLATE:
531 this->other_cpi = cpi;
532 this->ipcomp_received = ipcomp;
533 break;
534 case IPCOMP_LZS:
535 case IPCOMP_LZJH:
536 default:
537 DBG1(DBG_IKE, "received IPCOMP_SUPPORTED notify with a transform"
538 " ID we don't support %N", ipcomp_transform_names, ipcomp);
539 break;
540 }
541 }
542 default:
543 break;
544 }
545 break;
546 default:
547 break;
548 }
549 }
550 iterator->destroy(iterator);
551 }
552
553 /**
554 * Implementation of task_t.build for initiator
555 */
556 static status_t build_i(private_child_create_t *this, message_t *message)
557 {
558 host_t *me, *other, *vip;
559 bool propose_all = FALSE;
560 peer_cfg_t *peer_cfg;
561
562 switch (message->get_exchange_type(message))
563 {
564 case IKE_SA_INIT:
565 return get_nonce(message, &this->my_nonce);
566 case CREATE_CHILD_SA:
567 if (generate_nonce(&this->my_nonce) != SUCCESS)
568 {
569 message->add_notify(message, FALSE, NO_PROPOSAL_CHOSEN, chunk_empty);
570 return SUCCESS;
571 }
572 if (this->dh_group == MODP_NONE)
573 {
574 this->dh_group = this->config->get_dh_group(this->config);
575 }
576 break;
577 case IKE_AUTH:
578 if (!message->get_payload(message, ID_INITIATOR))
579 {
580 /* send only in the first request, not in subsequent EAP */
581 return NEED_MORE;
582 }
583 break;
584 default:
585 break;
586 }
587
588 if (this->reqid)
589 {
590 DBG0(DBG_IKE, "establishing CHILD_SA %s{%d}",
591 this->config->get_name(this->config), this->reqid);
592 }
593 else
594 {
595 DBG0(DBG_IKE, "establishing CHILD_SA %s",
596 this->config->get_name(this->config));
597 }
598
599 /* reuse virtual IP if we already have one */
600 me = this->ike_sa->get_virtual_ip(this->ike_sa, TRUE);
601 if (me == NULL)
602 {
603 me = this->ike_sa->get_my_host(this->ike_sa);
604 }
605 other = this->ike_sa->get_virtual_ip(this->ike_sa, FALSE);
606 if (other == NULL)
607 {
608 other = this->ike_sa->get_other_host(this->ike_sa);
609 }
610
611 /* check if we want a virtual IP, but don't have one */
612 if (!this->reqid)
613 {
614 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
615 vip = peer_cfg->get_virtual_ip(peer_cfg);
616 if (vip)
617 {
618 propose_all = TRUE;
619 }
620 }
621
622 if (propose_all)
623 { /* propose a 0.0.0.0/0 subnet when we use virtual ip */
624 this->tsi = this->config->get_traffic_selectors(this->config, TRUE,
625 NULL, NULL);
626 }
627 else
628 { /* but shorten a 0.0.0.0/0 subnet for host2host/we already have a vip */
629 this->tsi = this->config->get_traffic_selectors(this->config, TRUE,
630 NULL, me);
631 }
632 this->tsr = this->config->get_traffic_selectors(this->config, FALSE,
633 NULL, other);
634
635 this->proposals = this->config->get_proposals(this->config,
636 this->dh_group == MODP_NONE);
637 this->mode = this->config->get_mode(this->config);
638
639 this->child_sa = child_sa_create(this->ike_sa->get_my_host(this->ike_sa),
640 this->ike_sa->get_other_host(this->ike_sa), this->config, this->reqid,
641 this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY));
642
643 if (this->child_sa->alloc(this->child_sa, this->proposals) != SUCCESS)
644 {
645 DBG1(DBG_IKE, "unable to allocate SPIs from kernel");
646 return FAILED;
647 }
648
649 if (this->dh_group != MODP_NONE)
650 {
651 this->dh = lib->crypto->create_dh(lib->crypto, this->dh_group);
652 }
653
654 if (this->config->use_ipcomp(this->config)) {
655 /* IPCOMP_DEFLATE is the only transform we support at the moment */
656 this->ipcomp = IPCOMP_DEFLATE;
657 build_ipcomp_supported_notify(this, message);
658 }
659
660 build_payloads(this, message);
661
662 this->tsi->destroy_offset(this->tsi, offsetof(traffic_selector_t, destroy));
663 this->tsr->destroy_offset(this->tsr, offsetof(traffic_selector_t, destroy));
664 this->proposals->destroy_offset(this->proposals, offsetof(proposal_t, destroy));
665 this->tsi = NULL;
666 this->tsr = NULL;
667 this->proposals = NULL;
668
669 return NEED_MORE;
670 }
671
672 /**
673 * Implementation of task_t.process for initiator
674 */
675 static status_t process_r(private_child_create_t *this, message_t *message)
676 {
677 peer_cfg_t *peer_cfg;
678
679 switch (message->get_exchange_type(message))
680 {
681 case IKE_SA_INIT:
682 return get_nonce(message, &this->other_nonce);
683 case CREATE_CHILD_SA:
684 get_nonce(message, &this->other_nonce);
685 break;
686 case IKE_AUTH:
687 if (message->get_payload(message, ID_INITIATOR) == NULL)
688 {
689 /* wait until extensible authentication completed, if used */
690 return NEED_MORE;
691 }
692 default:
693 break;
694 }
695
696 process_payloads(this, message);
697
698 if (this->tsi == NULL || this->tsr == NULL)
699 {
700 DBG1(DBG_IKE, "TS payload missing in message");
701 return NEED_MORE;
702 }
703
704 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
705 if (peer_cfg)
706 {
707 host_t *me, *other;
708
709 me = this->ike_sa->get_virtual_ip(this->ike_sa, TRUE);
710 if (me == NULL)
711 {
712 me = this->ike_sa->get_my_host(this->ike_sa);
713 }
714 other = this->ike_sa->get_virtual_ip(this->ike_sa, FALSE);
715 if (other == NULL)
716 {
717 other = this->ike_sa->get_other_host(this->ike_sa);
718 }
719
720 this->config = peer_cfg->select_child_cfg(peer_cfg, this->tsr,
721 this->tsi, me, other);
722 }
723 return NEED_MORE;
724 }
725
726 /**
727 * handle CHILD_SA setup failure
728 */
729 static void handle_child_sa_failure(private_child_create_t *this,
730 message_t *message)
731 {
732 if (message->get_exchange_type(message) == IKE_AUTH &&
733 lib->settings->get_bool(lib->settings,
734 "charon.close_ike_on_child_failure", FALSE))
735 {
736 /* we delay the delete for 100ms, as the IKE_AUTH response must arrive
737 * first */
738 DBG1(DBG_IKE, "closing IKE_SA due CHILD_SA setup failure");
739 charon->scheduler->schedule_job(charon->scheduler, (job_t*)
740 delete_ike_sa_job_create(this->ike_sa->get_id(this->ike_sa), TRUE),
741 100);
742 }
743 }
744
745 /**
746 * Implementation of task_t.build for responder
747 */
748 static status_t build_r(private_child_create_t *this, message_t *message)
749 {
750 payload_t *payload;
751 iterator_t *iterator;
752 bool no_dh = TRUE;
753
754 switch (message->get_exchange_type(message))
755 {
756 case IKE_SA_INIT:
757 return get_nonce(message, &this->my_nonce);
758 case CREATE_CHILD_SA:
759 if (generate_nonce(&this->my_nonce) != SUCCESS)
760 {
761 message->add_notify(message, FALSE, NO_PROPOSAL_CHOSEN,
762 chunk_empty);
763 return SUCCESS;
764 }
765 no_dh = FALSE;
766 break;
767 case IKE_AUTH:
768 if (message->get_payload(message, EXTENSIBLE_AUTHENTICATION))
769 {
770 /* wait until extensible authentication completed, if used */
771 return NEED_MORE;
772 }
773 default:
774 break;
775 }
776
777 if (this->ike_sa->get_state(this->ike_sa) == IKE_REKEYING)
778 {
779 DBG1(DBG_IKE, "unable to create CHILD_SA while rekeying IKE_SA");
780 message->add_notify(message, TRUE, NO_ADDITIONAL_SAS, chunk_empty);
781 return SUCCESS;
782 }
783
784 if (this->config == NULL)
785 {
786 DBG1(DBG_IKE, "traffic selectors %#R=== %#R inacceptable",
787 this->tsr, this->tsi);
788 message->add_notify(message, FALSE, TS_UNACCEPTABLE, chunk_empty);
789 handle_child_sa_failure(this, message);
790 return SUCCESS;
791 }
792
793 /* check if ike_config_t included non-critical error notifies */
794 iterator = message->get_payload_iterator(message);
795 while (iterator->iterate(iterator, (void**)&payload))
796 {
797 if (payload->get_type(payload) == NOTIFY)
798 {
799 notify_payload_t *notify = (notify_payload_t*)payload;
800
801 switch (notify->get_notify_type(notify))
802 {
803 case INTERNAL_ADDRESS_FAILURE:
804 case FAILED_CP_REQUIRED:
805 {
806 DBG1(DBG_IKE,"configuration payload negotation "
807 "failed, no CHILD_SA built");
808 iterator->destroy(iterator);
809 handle_child_sa_failure(this, message);
810 return SUCCESS;
811 }
812 default:
813 break;
814 }
815 }
816 }
817 iterator->destroy(iterator);
818
819 this->child_sa = child_sa_create(this->ike_sa->get_my_host(this->ike_sa),
820 this->ike_sa->get_other_host(this->ike_sa), this->config, this->reqid,
821 this->ike_sa->has_condition(this->ike_sa, COND_NAT_ANY));
822
823 if (this->config->use_ipcomp(this->config) &&
824 this->ipcomp_received != IPCOMP_NONE)
825 {
826 this->ipcomp = this->ipcomp_received;
827 build_ipcomp_supported_notify(this, message);
828 }
829 else if (this->ipcomp_received != IPCOMP_NONE)
830 {
831 DBG1(DBG_IKE, "received %N notify but IPComp is disabled, ignoring",
832 notify_type_names, IPCOMP_SUPPORTED);
833 }
834
835 switch (select_and_install(this, no_dh))
836 {
837 case SUCCESS:
838 break;
839 case NOT_FOUND:
840 message->add_notify(message, FALSE, TS_UNACCEPTABLE, chunk_empty);
841 handle_child_sa_failure(this, message);
842 return SUCCESS;
843 case INVALID_ARG:
844 {
845 u_int16_t group = htons(this->dh_group);
846 message->add_notify(message, FALSE, INVALID_KE_PAYLOAD,
847 chunk_from_thing(group));
848 handle_child_sa_failure(this, message);
849 return SUCCESS;
850 }
851 case FAILED:
852 default:
853 message->add_notify(message, FALSE, NO_PROPOSAL_CHOSEN, chunk_empty);
854 handle_child_sa_failure(this, message);
855 return SUCCESS;
856 }
857
858 build_payloads(this, message);
859
860 DBG0(DBG_IKE, "CHILD_SA %s{%d} established "
861 "with SPIs %.8x_i %.8x_o and TS %#R=== %#R",
862 this->child_sa->get_name(this->child_sa),
863 this->child_sa->get_reqid(this->child_sa),
864 ntohl(this->child_sa->get_spi(this->child_sa, TRUE)),
865 ntohl(this->child_sa->get_spi(this->child_sa, FALSE)),
866 this->child_sa->get_traffic_selectors(this->child_sa, TRUE),
867 this->child_sa->get_traffic_selectors(this->child_sa, FALSE));
868
869 return SUCCESS;
870 }
871
872 /**
873 * Implementation of task_t.process for initiator
874 */
875 static status_t process_i(private_child_create_t *this, message_t *message)
876 {
877 iterator_t *iterator;
878 payload_t *payload;
879 bool no_dh = TRUE;
880
881 switch (message->get_exchange_type(message))
882 {
883 case IKE_SA_INIT:
884 return get_nonce(message, &this->other_nonce);
885 case CREATE_CHILD_SA:
886 get_nonce(message, &this->other_nonce);
887 no_dh = FALSE;
888 break;
889 case IKE_AUTH:
890 if (message->get_payload(message, EXTENSIBLE_AUTHENTICATION))
891 {
892 /* wait until extensible authentication completed, if used */
893 return NEED_MORE;
894 }
895 default:
896 break;
897 }
898
899 /* check for erronous notifies */
900 iterator = message->get_payload_iterator(message);
901 while (iterator->iterate(iterator, (void**)&payload))
902 {
903 if (payload->get_type(payload) == NOTIFY)
904 {
905 notify_payload_t *notify = (notify_payload_t*)payload;
906 notify_type_t type = notify->get_notify_type(notify);
907
908 switch (type)
909 {
910 /* handle notify errors related to CHILD_SA only */
911 case NO_PROPOSAL_CHOSEN:
912 case SINGLE_PAIR_REQUIRED:
913 case NO_ADDITIONAL_SAS:
914 case INTERNAL_ADDRESS_FAILURE:
915 case FAILED_CP_REQUIRED:
916 case TS_UNACCEPTABLE:
917 case INVALID_SELECTORS:
918 {
919 DBG1(DBG_IKE, "received %N notify, no CHILD_SA built",
920 notify_type_names, type);
921 iterator->destroy(iterator);
922 handle_child_sa_failure(this, message);
923 /* an error in CHILD_SA creation is not critical */
924 return SUCCESS;
925 }
926 case INVALID_KE_PAYLOAD:
927 {
928 chunk_t data;
929 diffie_hellman_group_t bad_group;
930
931 bad_group = this->dh_group;
932 data = notify->get_notification_data(notify);
933 this->dh_group = ntohs(*((u_int16_t*)data.ptr));
934 DBG1(DBG_IKE, "peer didn't accept DH group %N, "
935 "it requested %N", diffie_hellman_group_names,
936 bad_group, diffie_hellman_group_names, this->dh_group);
937
938 this->public.task.migrate(&this->public.task, this->ike_sa);
939 iterator->destroy(iterator);
940 return NEED_MORE;
941 }
942 default:
943 break;
944 }
945 }
946 }
947 iterator->destroy(iterator);
948
949 process_payloads(this, message);
950
951 if (this->ipcomp == IPCOMP_NONE && this->ipcomp_received != IPCOMP_NONE)
952 {
953 DBG1(DBG_IKE, "received an IPCOMP_SUPPORTED notify without requesting"
954 " one, no CHILD_SA built");
955 handle_child_sa_failure(this, message);
956 return SUCCESS;
957 }
958 else if (this->ipcomp != IPCOMP_NONE && this->ipcomp_received == IPCOMP_NONE)
959 {
960 DBG1(DBG_IKE, "peer didn't accept our proposed IPComp transforms, "
961 "IPComp is disabled");
962 this->ipcomp = IPCOMP_NONE;
963 }
964 else if (this->ipcomp != IPCOMP_NONE && this->ipcomp != this->ipcomp_received)
965 {
966 DBG1(DBG_IKE, "received an IPCOMP_SUPPORTED notify we didn't propose, "
967 "no CHILD_SA built");
968 handle_child_sa_failure(this, message);
969 return SUCCESS;
970 }
971
972 if (select_and_install(this, no_dh) == SUCCESS)
973 {
974 DBG0(DBG_IKE, "CHILD_SA %s{%d} established "
975 "with SPIs %.8x_i %.8x_o and TS %#R=== %#R",
976 this->child_sa->get_name(this->child_sa),
977 this->child_sa->get_reqid(this->child_sa),
978 ntohl(this->child_sa->get_spi(this->child_sa, TRUE)),
979 ntohl(this->child_sa->get_spi(this->child_sa, FALSE)),
980 this->child_sa->get_traffic_selectors(this->child_sa, TRUE),
981 this->child_sa->get_traffic_selectors(this->child_sa, FALSE));
982 }
983 else
984 {
985 handle_child_sa_failure(this, message);
986 }
987 return SUCCESS;
988 }
989
990 /**
991 * Implementation of task_t.get_type
992 */
993 static task_type_t get_type(private_child_create_t *this)
994 {
995 return CHILD_CREATE;
996 }
997
998 /**
999 * Implementation of child_create_t.use_reqid
1000 */
1001 static void use_reqid(private_child_create_t *this, u_int32_t reqid)
1002 {
1003 this->reqid = reqid;
1004 }
1005
1006 /**
1007 * Implementation of child_create_t.get_child
1008 */
1009 static child_sa_t* get_child(private_child_create_t *this)
1010 {
1011 return this->child_sa;
1012 }
1013
1014 /**
1015 * Implementation of child_create_t.get_lower_nonce
1016 */
1017 static chunk_t get_lower_nonce(private_child_create_t *this)
1018 {
1019 if (memcmp(this->my_nonce.ptr, this->other_nonce.ptr,
1020 min(this->my_nonce.len, this->other_nonce.len)) < 0)
1021 {
1022 return this->my_nonce;
1023 }
1024 else
1025 {
1026 return this->other_nonce;
1027 }
1028 }
1029
1030 /**
1031 * Implementation of task_t.migrate
1032 */
1033 static void migrate(private_child_create_t *this, ike_sa_t *ike_sa)
1034 {
1035 chunk_free(&this->my_nonce);
1036 chunk_free(&this->other_nonce);
1037 if (this->tsi)
1038 {
1039 this->tsr->destroy_offset(this->tsr, offsetof(traffic_selector_t, destroy));
1040 }
1041 if (this->tsr)
1042 {
1043 this->tsi->destroy_offset(this->tsi, offsetof(traffic_selector_t, destroy));
1044 }
1045 DESTROY_IF(this->child_sa);
1046 DESTROY_IF(this->proposal);
1047 DESTROY_IF(this->dh);
1048 if (this->proposals)
1049 {
1050 this->proposals->destroy_offset(this->proposals, offsetof(proposal_t, destroy));
1051 }
1052
1053 this->ike_sa = ike_sa;
1054 this->proposals = NULL;
1055 this->tsi = NULL;
1056 this->tsr = NULL;
1057 this->dh = NULL;
1058 this->child_sa = NULL;
1059 this->mode = MODE_TUNNEL;
1060 this->ipcomp = IPCOMP_NONE;
1061 this->ipcomp_received = IPCOMP_NONE;
1062 this->other_cpi = 0;
1063 this->reqid = 0;
1064 this->established = FALSE;
1065 }
1066
1067 /**
1068 * Implementation of task_t.destroy
1069 */
1070 static void destroy(private_child_create_t *this)
1071 {
1072 chunk_free(&this->my_nonce);
1073 chunk_free(&this->other_nonce);
1074 if (this->tsi)
1075 {
1076 this->tsr->destroy_offset(this->tsr, offsetof(traffic_selector_t, destroy));
1077 }
1078 if (this->tsr)
1079 {
1080 this->tsi->destroy_offset(this->tsi, offsetof(traffic_selector_t, destroy));
1081 }
1082 if (!this->established)
1083 {
1084 DESTROY_IF(this->child_sa);
1085 }
1086 DESTROY_IF(this->proposal);
1087 DESTROY_IF(this->dh);
1088 if (this->proposals)
1089 {
1090 this->proposals->destroy_offset(this->proposals, offsetof(proposal_t, destroy));
1091 }
1092
1093 DESTROY_IF(this->config);
1094 free(this);
1095 }
1096
1097 /*
1098 * Described in header.
1099 */
1100 child_create_t *child_create_create(ike_sa_t *ike_sa, child_cfg_t *config)
1101 {
1102 private_child_create_t *this = malloc_thing(private_child_create_t);
1103
1104 this->public.get_child = (child_sa_t*(*)(child_create_t*))get_child;
1105 this->public.get_lower_nonce = (chunk_t(*)(child_create_t*))get_lower_nonce;
1106 this->public.use_reqid = (void(*)(child_create_t*,u_int32_t))use_reqid;
1107 this->public.task.get_type = (task_type_t(*)(task_t*))get_type;
1108 this->public.task.migrate = (void(*)(task_t*,ike_sa_t*))migrate;
1109 this->public.task.destroy = (void(*)(task_t*))destroy;
1110 if (config)
1111 {
1112 this->public.task.build = (status_t(*)(task_t*,message_t*))build_i;
1113 this->public.task.process = (status_t(*)(task_t*,message_t*))process_i;
1114 this->initiator = TRUE;
1115 config->get_ref(config);
1116 }
1117 else
1118 {
1119 this->public.task.build = (status_t(*)(task_t*,message_t*))build_r;
1120 this->public.task.process = (status_t(*)(task_t*,message_t*))process_r;
1121 this->initiator = FALSE;
1122 }
1123
1124 this->ike_sa = ike_sa;
1125 this->config = config;
1126 this->my_nonce = chunk_empty;
1127 this->other_nonce = chunk_empty;
1128 this->proposals = NULL;
1129 this->proposal = NULL;
1130 this->tsi = NULL;
1131 this->tsr = NULL;
1132 this->dh = NULL;
1133 this->dh_group = MODP_NONE;
1134 this->child_sa = NULL;
1135 this->mode = MODE_TUNNEL;
1136 this->ipcomp = IPCOMP_NONE;
1137 this->ipcomp_received = IPCOMP_NONE;
1138 this->other_cpi = 0;
1139 this->reqid = 0;
1140 this->established = FALSE;
1141
1142 return &this->public;
1143 }