a79d6aefbcf30bf08a021e604b9c0bf5f8d98d37
[strongswan.git] / src / charon / sa / transactions / create_child_sa.c
1 /**
2 * @file create_child_sa.c
3 *
4 * @brief Implementation of create_child_sa_t transaction.
5 *
6 */
7
8 /*
9 * Copyright (C) 2006 Martin Willi
10 * Hochschule fuer Technik Rapperswil
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License as published by the
14 * Free Software Foundation; either version 2 of the License, or (at your
15 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
21 */
22
23 #include "create_child_sa.h"
24
25 #include <string.h>
26
27 #include <daemon.h>
28 #include <encoding/payloads/sa_payload.h>
29 #include <encoding/payloads/nonce_payload.h>
30 #include <encoding/payloads/ts_payload.h>
31 #include <sa/transactions/delete_child_sa.h>
32 #include <utils/randomizer.h>
33
34
35 typedef struct private_create_child_sa_t private_create_child_sa_t;
36
37 /**
38 * Private members of a create_child_sa_t object..
39 */
40 struct private_create_child_sa_t {
41
42 /**
43 * Public methods and transaction_t interface.
44 */
45 create_child_sa_t public;
46
47 /**
48 * Assigned IKE_SA.
49 */
50 ike_sa_t *ike_sa;
51
52 /**
53 * Message sent by our peer, if already generated
54 */
55 message_t *message;
56
57 /**
58 * Message ID this transaction uses
59 */
60 u_int32_t message_id;
61
62 /**
63 * Times we did send the request
64 */
65 u_int32_t requested;
66
67 /**
68 * initiators inbound SPI of the CHILD_SA which gets rekeyed
69 */
70 u_int32_t rekey_spi;
71
72 /**
73 * connection of IKE_SA
74 */
75 connection_t *connection;
76
77 /**
78 * policy definition used
79 */
80 policy_t *policy;
81
82 /**
83 * Negotiated proposal used for CHILD_SA
84 */
85 proposal_t *proposal;
86
87 /**
88 * initiator chosen nonce
89 */
90 chunk_t nonce_i;
91
92 /**
93 * responder chosen nonce
94 */
95 chunk_t nonce_r;
96
97 /**
98 * lower of the nonces of a simultaneus rekeying request
99 */
100 chunk_t nonce_s;
101
102 /**
103 * Negotiated traffic selectors for initiator
104 */
105 linked_list_t *tsi;
106
107 /**
108 * Negotiated traffic selectors for responder
109 */
110 linked_list_t *tsr;
111
112 /**
113 * CHILD_SA created by this transaction
114 */
115 child_sa_t *child_sa;
116
117 /**
118 * CHILD_SA rekeyed if we are rekeying
119 */
120 child_sa_t *rekeyed_sa;
121
122 /**
123 * Have we lost the simultaneous rekeying nonce compare?
124 */
125 bool lost;
126
127 /**
128 * source of randomness
129 */
130 randomizer_t *randomizer;
131
132 /**
133 * Assigned logger.
134 */
135 logger_t *logger;
136 };
137
138 /**
139 * Implementation of transaction_t.get_message_id.
140 */
141 static u_int32_t get_message_id(private_create_child_sa_t *this)
142 {
143 return this->message_id;
144 }
145
146 /**
147 * Implementation of transaction_t.requested.
148 */
149 static u_int32_t requested(private_create_child_sa_t *this)
150 {
151 return this->requested++;
152 }
153
154 /**
155 * Implementation of create_child_sa_t.rekeys_child.
156 */
157 static void rekeys_child(private_create_child_sa_t *this, child_sa_t *child_sa)
158 {
159 this->rekeyed_sa = child_sa;
160 }
161
162 /**
163 * Implementation of create_child_sa_t.cancel.
164 */
165 static void cancel(private_create_child_sa_t *this)
166 {
167 this->rekeyed_sa = NULL;
168 this->lost = TRUE;
169 }
170
171 /**
172 * Implementation of transaction_t.get_request.
173 */
174 static status_t get_request(private_create_child_sa_t *this, message_t **result)
175 {
176 message_t *request;
177 host_t *me, *other;
178
179 /* check if we are not already rekeying */
180 if (this->rekeyed_sa)
181 {
182 switch (this->rekeyed_sa->get_state(this->rekeyed_sa))
183 {
184 case CHILD_REKEYING:
185 this->logger->log(this->logger, ERROR,
186 "rekeying a CHILD_SA which is already rekeying, aborted");
187 return FAILED;
188 case CHILD_DELETING:
189 this->logger->log(this->logger, ERROR,
190 "rekeying a CHILD_SA which is deleting, aborted");
191 return FAILED;
192 default:
193 break;
194 }
195 this->rekeyed_sa->set_state(this->rekeyed_sa, CHILD_REKEYING);
196 }
197
198 /* check if we already have built a message (retransmission) */
199 if (this->message)
200 {
201 *result = this->message;
202 return SUCCESS;
203 }
204
205 this->connection = this->ike_sa->get_connection(this->ike_sa);
206 me = this->connection->get_my_host(this->connection);
207 other = this->connection->get_other_host(this->connection);
208 this->policy = this->ike_sa->get_policy(this->ike_sa);
209
210 /* build the request */
211 request = message_create();
212 request->set_source(request, me->clone(me));
213 request->set_destination(request, other->clone(other));
214 request->set_exchange_type(request, CREATE_CHILD_SA);
215 request->set_request(request, TRUE);
216 request->set_ike_sa_id(request, this->ike_sa->get_id(this->ike_sa));
217 *result = request;
218 this->message = request;
219
220 { /* build SA payload */
221 sa_payload_t *sa_payload;
222 linked_list_t *proposals;
223 bool use_natt;
224 u_int32_t reqid = 0;
225
226 if (this->rekeyed_sa)
227 {
228 reqid = this->rekeyed_sa->get_reqid(this->rekeyed_sa);
229 }
230
231 proposals = this->policy->get_proposals(this->policy);
232 use_natt = this->ike_sa->is_natt_enabled(this->ike_sa);
233 this->child_sa = child_sa_create(reqid, me, other,
234 this->policy->get_soft_lifetime(this->policy),
235 this->policy->get_hard_lifetime(this->policy),
236 use_natt);
237 if (this->child_sa->alloc(this->child_sa, proposals) != SUCCESS)
238 {
239 this->logger->log(this->logger, ERROR,
240 "could not install CHILD_SA, CHILD_SA creation aborted");
241 return FAILED;
242 }
243 sa_payload = sa_payload_create_from_proposal_list(proposals);
244 request->add_payload(request, (payload_t*)sa_payload);
245 }
246
247 { /* build the NONCE payload for us (initiator) */
248 nonce_payload_t *nonce_payload;
249
250 if (this->randomizer->allocate_pseudo_random_bytes(this->randomizer,
251 NONCE_SIZE, &this->nonce_i) != SUCCESS)
252 {
253 return FAILED;
254 }
255 nonce_payload = nonce_payload_create();
256 nonce_payload->set_nonce(nonce_payload, this->nonce_i);
257 request->add_payload(request, (payload_t*)nonce_payload);
258 }
259
260 { /* build TSi payload */
261 linked_list_t *ts_list;
262 ts_payload_t *ts_payload;
263
264 ts_list = this->policy->get_my_traffic_selectors(this->policy);
265 ts_payload = ts_payload_create_from_traffic_selectors(TRUE, ts_list);
266 request->add_payload(request, (payload_t*)ts_payload);
267 }
268
269 { /* build TSr payload */
270 linked_list_t *ts_list;
271 ts_payload_t *ts_payload;
272
273 ts_list = this->policy->get_other_traffic_selectors(this->policy);
274 ts_payload = ts_payload_create_from_traffic_selectors(FALSE, ts_list);
275 request->add_payload(request, (payload_t*)ts_payload);
276 }
277
278 if (this->rekeyed_sa)
279 { /* add REKEY_SA notify if we are rekeying */
280 notify_payload_t *notify;
281 protocol_id_t protocol;
282
283 protocol = this->rekeyed_sa->get_protocol(this->rekeyed_sa);
284 notify = notify_payload_create_from_protocol_and_type(protocol, REKEY_SA);
285 notify->set_spi(notify, this->rekeyed_sa->get_spi(this->rekeyed_sa, TRUE));
286 request->add_payload(request, (payload_t*)notify);
287
288 /* register us as rekeying to detect multiple rekeying */
289 this->rekeyed_sa->set_rekeying_transaction(this->rekeyed_sa, &this->public);
290 }
291
292 this->message_id = this->ike_sa->get_next_message_id(this->ike_sa);
293 request->set_message_id(request, this->message_id);
294
295 return SUCCESS;
296 }
297
298 /**
299 * Handle all kind of notifys
300 */
301 static status_t process_notifys(private_create_child_sa_t *this, notify_payload_t *notify_payload)
302 {
303 notify_type_t notify_type = notify_payload->get_notify_type(notify_payload);
304
305 this->logger->log(this->logger, CONTROL|LEVEL1, "process notify type %s",
306 mapping_find(notify_type_m, notify_type));
307
308 switch (notify_type)
309 {
310 case SINGLE_PAIR_REQUIRED:
311 {
312 this->logger->log(this->logger, AUDIT,
313 "received a SINGLE_PAIR_REQUIRED notify");
314 return FAILED;
315 }
316 case TS_UNACCEPTABLE:
317 {
318 this->logger->log(this->logger, CONTROL,
319 "received TS_UNACCEPTABLE notify");
320 return FAILED;
321 }
322 case NO_PROPOSAL_CHOSEN:
323 {
324 this->logger->log(this->logger, CONTROL,
325 "received NO_PROPOSAL_CHOSEN notify");
326 return FAILED;
327 }
328 case REKEY_SA:
329 {
330 u_int32_t spi;
331 protocol_id_t protocol;
332
333 protocol = notify_payload->get_protocol_id(notify_payload);
334 switch (protocol)
335 {
336 case PROTO_AH:
337 case PROTO_ESP:
338 spi = notify_payload->get_spi(notify_payload);
339 this->rekeyed_sa = this->ike_sa->get_child_sa(this->ike_sa,
340 protocol, spi,
341 FALSE);
342 break;
343 default:
344 break;
345 }
346 return SUCCESS;
347 }
348 default:
349 {
350 if (notify_type < 16383)
351 {
352 this->logger->log(this->logger, AUDIT,
353 "received %s notify error (%d), deleting IKE_SA",
354 mapping_find(notify_type_m, notify_type),
355 notify_type);
356 return FAILED;
357 }
358 else
359 {
360 this->logger->log(this->logger, CONTROL,
361 "received %s notify (%d), ignored",
362 mapping_find(notify_type_m, notify_type),
363 notify_type);
364 return SUCCESS;
365 }
366 }
367 }
368 }
369
370 /**
371 * Build a notify message.
372 */
373 static void build_notify(notify_type_t type, message_t *message, bool flush_message)
374 {
375 notify_payload_t *notify;
376
377 if (flush_message)
378 {
379 payload_t *payload;
380 iterator_t *iterator = message->get_payload_iterator(message);
381 while (iterator->iterate(iterator, (void**)&payload))
382 {
383 payload->destroy(payload);
384 iterator->remove(iterator);
385 }
386 iterator->destroy(iterator);
387 }
388
389 notify = notify_payload_create();
390 notify->set_notify_type(notify, type);
391 message->add_payload(message, (payload_t*)notify);
392 }
393
394 /**
395 * Install a CHILD_SA for usage
396 */
397 static status_t install_child_sa(private_create_child_sa_t *this, bool initiator)
398 {
399 prf_plus_t *prf_plus;
400 chunk_t seed;
401 status_t status;
402
403 seed = chunk_alloc(this->nonce_i.len + this->nonce_r.len);
404 memcpy(seed.ptr, this->nonce_i.ptr, this->nonce_i.len);
405 memcpy(seed.ptr + this->nonce_i.len, this->nonce_r.ptr, this->nonce_r.len);
406 prf_plus = prf_plus_create(this->ike_sa->get_child_prf(this->ike_sa), seed);
407 chunk_free(&seed);
408
409 if (initiator)
410 {
411 status = this->child_sa->update(this->child_sa, this->proposal, prf_plus);
412 }
413 else
414 {
415 status = this->child_sa->add(this->child_sa, this->proposal, prf_plus);
416 }
417 prf_plus->destroy(prf_plus);
418 if (status != SUCCESS)
419 {
420 return DESTROY_ME;
421 }
422 if (initiator)
423 {
424 status = this->child_sa->add_policies(this->child_sa, this->tsi, this->tsr);
425 }
426 else
427 {
428 status = this->child_sa->add_policies(this->child_sa, this->tsr, this->tsi);
429 }
430 if (status != SUCCESS)
431 {
432 return DESTROY_ME;
433 }
434 /* add to IKE_SA, and remove from transaction */
435 this->ike_sa->add_child_sa(this->ike_sa, this->child_sa);
436 this->child_sa = NULL;
437 return SUCCESS;
438 }
439
440 /**
441 * destroy a list of traffic selectors
442 */
443 static void destroy_ts_list(linked_list_t *list)
444 {
445 if (list)
446 {
447 traffic_selector_t *ts;
448 while (list->remove_last(list, (void**)&ts) == SUCCESS)
449 {
450 ts->destroy(ts);
451 }
452 list->destroy(list);
453 }
454 }
455
456 /**
457 * Implementation of transaction_t.get_response.
458 */
459 static status_t get_response(private_create_child_sa_t *this, message_t *request,
460 message_t **result, transaction_t **next)
461 {
462 host_t *me, *other;
463 message_t *response;
464 status_t status;
465 iterator_t *payloads;
466 sa_payload_t *sa_request = NULL;
467 nonce_payload_t *nonce_request = NULL;
468 ts_payload_t *tsi_request = NULL;
469 ts_payload_t *tsr_request = NULL;
470 nonce_payload_t *nonce_response;
471
472 /* check if we already have built a response (retransmission) */
473 if (this->message)
474 {
475 *result = this->message;
476 return SUCCESS;
477 }
478
479 this->connection = this->ike_sa->get_connection(this->ike_sa);
480 me = this->connection->get_my_host(this->connection);
481 other = this->connection->get_other_host(this->connection);
482 this->policy = this->ike_sa->get_policy(this->ike_sa);
483 this->message_id = request->get_message_id(request);
484
485 /* set up response */
486 response = message_create();
487 response->set_source(response, me->clone(me));
488 response->set_destination(response, other->clone(other));
489 response->set_exchange_type(response, CREATE_CHILD_SA);
490 response->set_request(response, FALSE);
491 response->set_message_id(response, this->message_id);
492 response->set_ike_sa_id(response, this->ike_sa->get_id(this->ike_sa));
493 this->message = response;
494 *result = response;
495
496 /* check message type */
497 if (request->get_exchange_type(request) != CREATE_CHILD_SA)
498 {
499 this->logger->log(this->logger, ERROR,
500 "CREATE_CHILD_SA response of invalid type, aborted");
501 return FAILED;
502 }
503
504 /* Iterate over all payloads. */
505 payloads = request->get_payload_iterator(request);
506 while (payloads->has_next(payloads))
507 {
508 payload_t *payload;
509 payloads->current(payloads, (void**)&payload);
510 switch (payload->get_type(payload))
511 {
512 case SECURITY_ASSOCIATION:
513 sa_request = (sa_payload_t*)payload;
514 break;
515 case NONCE:
516 nonce_request = (nonce_payload_t*)payload;
517 break;
518 case TRAFFIC_SELECTOR_INITIATOR:
519 tsi_request = (ts_payload_t*)payload;
520 break;
521 case TRAFFIC_SELECTOR_RESPONDER:
522 tsr_request = (ts_payload_t*)payload;
523 break;
524 case NOTIFY:
525 {
526 status = process_notifys(this, (notify_payload_t*)payload);
527 if (status != SUCCESS)
528 {
529 payloads->destroy(payloads);
530 return status;
531 }
532 break;
533 }
534 default:
535 {
536 this->logger->log(this->logger, ERROR, "ignoring %s payload (%d)",
537 mapping_find(payload_type_m, payload->get_type(payload)),
538 payload->get_type(payload));
539 break;
540 }
541 }
542 }
543 payloads->destroy(payloads);
544
545 /* check if we have all payloads */
546 if (!(sa_request && nonce_request && tsi_request && tsr_request))
547 {
548 build_notify(INVALID_SYNTAX, response, TRUE);
549 this->logger->log(this->logger, AUDIT,
550 "request message incomplete, no CHILD_SA created");
551 return FAILED;
552 }
553
554 { /* process nonce payload */
555 this->nonce_i = nonce_request->get_nonce(nonce_request);
556 if (this->randomizer->allocate_pseudo_random_bytes(this->randomizer,
557 NONCE_SIZE, &this->nonce_r) != SUCCESS)
558 {
559 build_notify(NO_PROPOSAL_CHOSEN, response, TRUE);
560 return FAILED;
561 }
562 nonce_response = nonce_payload_create();
563 nonce_response->set_nonce(nonce_response, this->nonce_r);
564 }
565
566 { /* process traffic selectors for other */
567 linked_list_t *ts_received = tsi_request->get_traffic_selectors(tsi_request);
568 this->tsi = this->policy->select_other_traffic_selectors(this->policy, ts_received);
569 destroy_ts_list(ts_received);
570 }
571
572 { /* process traffic selectors for us */
573 linked_list_t *ts_received = ts_received = tsr_request->get_traffic_selectors(tsr_request);
574 this->tsr = this->policy->select_my_traffic_selectors(this->policy, ts_received);
575 destroy_ts_list(ts_received);
576 }
577
578 { /* process SA payload */
579 proposal_t *proposal;
580 linked_list_t *proposal_list;
581 sa_payload_t *sa_response;
582 ts_payload_t *ts_response;
583 bool use_natt;
584 u_int32_t soft_lifetime, hard_lifetime;
585
586 sa_response = sa_payload_create();
587 /* get proposals from request, and select one with ours */
588 proposal_list = sa_request->get_proposals(sa_request);
589 this->logger->log(this->logger, CONTROL|LEVEL1, "selecting proposals:");
590 this->proposal = this->policy->select_proposal(this->policy, proposal_list);
591 /* list is not needed anymore */
592 while (proposal_list->remove_last(proposal_list, (void**)&proposal) == SUCCESS)
593 {
594 proposal->destroy(proposal);
595 }
596 proposal_list->destroy(proposal_list);
597
598 /* do we have a proposal? */
599 if (this->proposal == NULL)
600 {
601 this->logger->log(this->logger, AUDIT,
602 "CHILD_SA proposals unacceptable, adding NO_PROPOSAL_CHOSEN notify");
603 build_notify(NO_PROPOSAL_CHOSEN, response, TRUE);
604 return FAILED;
605 }
606 /* do we have traffic selectors? */
607 else if (this->tsi->get_count(this->tsi) == 0 || this->tsr->get_count(this->tsr) == 0)
608 {
609 this->logger->log(this->logger, AUDIT,
610 "CHILD_SA traffic selectors unacceptable, adding TS_UNACCEPTABLE notify");
611 build_notify(TS_UNACCEPTABLE, response, TRUE);
612 return FAILED;
613 }
614 else
615 { /* create child sa */
616 u_int32_t reqid = 0;
617
618 if (this->rekeyed_sa)
619 {
620 reqid = this->rekeyed_sa->get_reqid(this->rekeyed_sa);
621 }
622 soft_lifetime = this->policy->get_soft_lifetime(this->policy);
623 hard_lifetime = this->policy->get_hard_lifetime(this->policy);
624 use_natt = this->ike_sa->is_natt_enabled(this->ike_sa);
625 this->child_sa = child_sa_create(reqid, me, other,
626 soft_lifetime, hard_lifetime,
627 use_natt);
628 if (install_child_sa(this, FALSE) != SUCCESS)
629 {
630 this->logger->log(this->logger, ERROR,
631 "installing CHILD_SA failed, adding NO_PROPOSAL_CHOSEN notify");
632 build_notify(NO_PROPOSAL_CHOSEN, response, TRUE);
633 return FAILED;
634 }
635 /* add proposal to sa payload */
636 sa_response->add_proposal(sa_response, this->proposal);
637 }
638 response->add_payload(response, (payload_t*)sa_response);
639
640 /* add nonce/ts payload after sa payload */
641 response->add_payload(response, (payload_t *)nonce_response);
642 ts_response = ts_payload_create_from_traffic_selectors(TRUE, this->tsi);
643 response->add_payload(response, (payload_t*)ts_response);
644 ts_response = ts_payload_create_from_traffic_selectors(FALSE, this->tsr);
645 response->add_payload(response, (payload_t*)ts_response);
646 }
647 /* CHILD_SA successfully created. If another transaction is already rekeying
648 * this SA, our lower nonce must be registered for a later nonce compare. */
649 if (this->rekeyed_sa)
650 {
651 private_create_child_sa_t *other;
652
653 other = this->rekeyed_sa->get_rekeying_transaction(this->rekeyed_sa);
654 if (other)
655 {
656 /* store our lower nonce in the simultaneus transaction, it
657 * will later compare it against his nonces when it calls conclude().
658 */
659 if (memcmp(this->nonce_i.ptr, this->nonce_r.ptr,
660 min(this->nonce_i.len, this->nonce_r.len)) < 0)
661 {
662 other->nonce_s = chunk_clone(this->nonce_i);
663 }
664 else
665 {
666 other->nonce_s = chunk_clone(this->nonce_r);
667 }
668 }
669 this->rekeyed_sa->set_state(this->rekeyed_sa, CHILD_REKEYING);
670 }
671 return SUCCESS;
672 }
673
674 /**
675 * Implementation of transaction_t.conclude
676 */
677 static status_t conclude(private_create_child_sa_t *this, message_t *response,
678 transaction_t **next)
679 {
680 iterator_t *payloads;
681 host_t *me, *other;
682 sa_payload_t *sa_payload = NULL;
683 nonce_payload_t *nonce_payload = NULL;
684 ts_payload_t *tsi_payload = NULL;
685 ts_payload_t *tsr_payload = NULL;
686 status_t status;
687 child_sa_t *new_child = NULL;
688 delete_child_sa_t *delete_child_sa;
689
690 /* check message type */
691 if (response->get_exchange_type(response) != CREATE_CHILD_SA)
692 {
693 this->logger->log(this->logger, ERROR,
694 "CREATE_CHILD_SA response of invalid type, aborting");
695 return FAILED;
696 }
697
698 me = this->connection->get_my_host(this->connection);
699 other = this->connection->get_other_host(this->connection);
700
701 /* Iterate over all payloads to collect them */
702 payloads = response->get_payload_iterator(response);
703 while (payloads->has_next(payloads))
704 {
705 payload_t *payload;
706 payloads->current(payloads, (void**)&payload);
707 switch (payload->get_type(payload))
708 {
709 case SECURITY_ASSOCIATION:
710 sa_payload = (sa_payload_t*)payload;
711 break;
712 case NONCE:
713 nonce_payload = (nonce_payload_t*)payload;
714 break;
715 case TRAFFIC_SELECTOR_INITIATOR:
716 tsi_payload = (ts_payload_t*)payload;
717 break;
718 case TRAFFIC_SELECTOR_RESPONDER:
719 tsr_payload = (ts_payload_t*)payload;
720 break;
721 case NOTIFY:
722 {
723 status = process_notifys(this, (notify_payload_t*)payload);
724 if (status != SUCCESS)
725 {
726 payloads->destroy(payloads);
727 return status;
728 }
729 break;
730 }
731 default:
732 {
733 this->logger->log(this->logger, ERROR, "ignoring %s payload (%d)",
734 mapping_find(payload_type_m, payload->get_type(payload)),
735 payload->get_type(payload));
736 break;
737 }
738 }
739 }
740 payloads->destroy(payloads);
741
742 if (!(sa_payload && nonce_payload && tsi_payload && tsr_payload))
743 {
744 this->logger->log(this->logger, AUDIT, "response message incomplete, no CHILD_SA built");
745 return FAILED;
746 }
747
748 { /* process NONCE payload */
749 this->nonce_r = nonce_payload->get_nonce(nonce_payload);
750 }
751
752 { /* process traffic selectors for us */
753 linked_list_t *ts_received = tsi_payload->get_traffic_selectors(tsi_payload);
754 this->tsi = this->policy->select_my_traffic_selectors(this->policy, ts_received);
755 destroy_ts_list(ts_received);
756 }
757
758 { /* process traffic selectors for other */
759 linked_list_t *ts_received = tsr_payload->get_traffic_selectors(tsr_payload);
760 this->tsr = this->policy->select_other_traffic_selectors(this->policy, ts_received);
761 destroy_ts_list(ts_received);
762 }
763
764 { /* process sa payload */
765 proposal_t *proposal;
766 linked_list_t *proposal_list;
767
768 proposal_list = sa_payload->get_proposals(sa_payload);
769 /* we have to re-check here if other's selection is valid */
770 this->proposal = this->policy->select_proposal(this->policy, proposal_list);
771 /* list not needed anymore */
772 while (proposal_list->remove_last(proposal_list, (void**)&proposal) == SUCCESS)
773 {
774 proposal->destroy(proposal);
775 }
776 proposal_list->destroy(proposal_list);
777
778 /* everything fine to create CHILD? */
779 if (this->proposal == NULL ||
780 this->tsi->get_count(this->tsi) == 0 ||
781 this->tsr->get_count(this->tsr) == 0)
782 {
783 this->logger->log(this->logger, AUDIT,
784 "CHILD_SA creation failed");
785 return FAILED;
786 }
787 new_child = this->child_sa;
788 if (install_child_sa(this, TRUE) != SUCCESS)
789 {
790 this->logger->log(this->logger, ERROR,
791 "installing CHILD_SA failed, no CHILD_SA built");
792 return FAILED;
793 }
794 }
795 /* CHILD_SA successfully created. If the other peer initiated rekeying
796 * in the meantime, we detect this by comparing the rekeying_transaction
797 * of the SA. If it changed, we are not alone. Then we must compare the nonces.
798 * If no simultaneous rekeying is going on, we just initiate the delete of
799 * the superseded SA. */
800 if (this->rekeyed_sa)
801 {
802 /* rekeying finished, update SA status */
803 this->rekeyed_sa->set_rekeying_transaction(this->rekeyed_sa, NULL);
804
805 if (this->nonce_s.ptr)
806 { /* simlutaneous rekeying is going on, not so good */
807 chunk_t this_lowest;
808
809 /* first get our lowest nonce */
810 if (memcmp(this->nonce_i.ptr, this->nonce_r.ptr,
811 min(this->nonce_i.len, this->nonce_r.len)) < 0)
812 {
813 this_lowest = this->nonce_i;
814 }
815 else
816 {
817 this_lowest = this->nonce_r;
818 }
819 /* then compare against other lowest nonce */
820 if (memcmp(this_lowest.ptr, this->nonce_s.ptr,
821 min(this_lowest.len, this->nonce_s.len)) < 0)
822 {
823 this->logger->log(this->logger, ERROR,
824 "detected simultaneous CHILD_SA rekeying, deleting ours");
825 this->lost = TRUE;
826 }
827 else
828 {
829 this->logger->log(this->logger, ERROR,
830 "detected simultaneous CHILD_SA rekeying, but ours is preferred");
831 }
832 }
833 /* delete the old SA if we have won the rekeying nonce compare*/
834 if (!this->lost)
835 {
836 delete_child_sa = delete_child_sa_create(this->ike_sa);
837 delete_child_sa->set_child_sa(delete_child_sa, this->rekeyed_sa);
838 *next = (transaction_t*)delete_child_sa;
839 }
840 }
841 if (this->lost)
842 {
843 /* we have lost simlutaneous rekeying, delete the CHILD_SA we just have created */
844 delete_child_sa = delete_child_sa_create(this->ike_sa);
845 delete_child_sa->set_child_sa(delete_child_sa, new_child);
846 *next = (transaction_t*)delete_child_sa;
847 }
848 return SUCCESS;
849 }
850
851 /**
852 * implements transaction_t.destroy
853 */
854 static void destroy(private_create_child_sa_t *this)
855 {
856 if (this->message)
857 {
858 this->message->destroy(this->message);
859 }
860 if (this->proposal)
861 {
862 this->proposal->destroy(this->proposal);
863 }
864 if (this->child_sa)
865 {
866 this->child_sa->destroy(this->child_sa);
867 }
868 destroy_ts_list(this->tsi);
869 destroy_ts_list(this->tsr);
870 chunk_free(&this->nonce_i);
871 chunk_free(&this->nonce_r);
872 chunk_free(&this->nonce_s);
873 this->randomizer->destroy(this->randomizer);
874 free(this);
875 }
876
877 /*
878 * Described in header.
879 */
880 create_child_sa_t *create_child_sa_create(ike_sa_t *ike_sa)
881 {
882 private_create_child_sa_t *this = malloc_thing(private_create_child_sa_t);
883
884 /* transaction interface functions */
885 this->public.transaction.get_request = (status_t(*)(transaction_t*,message_t**))get_request;
886 this->public.transaction.get_response = (status_t(*)(transaction_t*,message_t*,message_t**,transaction_t**))get_response;
887 this->public.transaction.conclude = (status_t(*)(transaction_t*,message_t*,transaction_t**))conclude;
888 this->public.transaction.get_message_id = (u_int32_t(*)(transaction_t*))get_message_id;
889 this->public.transaction.requested = (u_int32_t(*)(transaction_t*))requested;
890 this->public.transaction.destroy = (void(*)(transaction_t*))destroy;
891
892 /* public functions */
893 this->public.rekeys_child = (void(*)(create_child_sa_t*,child_sa_t*))rekeys_child;
894 this->public.cancel = (void(*)(create_child_sa_t*))cancel;
895
896 /* private data */
897 this->ike_sa = ike_sa;
898 this->message_id = 0;
899 this->message = NULL;
900 this->requested = 0;
901 this->rekey_spi = 0;
902 this->nonce_i = CHUNK_INITIALIZER;
903 this->nonce_r = CHUNK_INITIALIZER;
904 this->nonce_s = CHUNK_INITIALIZER;
905 this->child_sa = NULL;
906 this->rekeyed_sa = NULL;
907 this->lost = FALSE;
908 this->proposal = NULL;
909 this->tsi = NULL;
910 this->tsr = NULL;
911 this->randomizer = randomizer_create();
912 this->logger = logger_manager->get_logger(logger_manager, IKE_SA);
913
914 return &this->public;
915 }