69d0d92ae0b646427951d3d2d747885a604de9b0
[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 * reqid to use for new CHILD_SA
74 */
75 u_int32_t reqid;
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 /**
134 * Implementation of transaction_t.get_message_id.
135 */
136 static u_int32_t get_message_id(private_create_child_sa_t *this)
137 {
138 return this->message_id;
139 }
140
141 /**
142 * Implementation of transaction_t.requested.
143 */
144 static u_int32_t requested(private_create_child_sa_t *this)
145 {
146 return this->requested++;
147 }
148
149 /**
150 * Implementation of create_child_sa_t.set_policy.
151 */
152 static void set_policy(private_create_child_sa_t *this, policy_t *policy)
153 {
154 this->policy = policy;
155 }
156
157 /**
158 * Implementation of create_child_sa_t.set_reqid.
159 */
160 static void set_reqid(private_create_child_sa_t *this, u_int32_t reqid)
161 {
162 this->reqid = reqid;
163 }
164
165 /**
166 * Implementation of create_child_sa_t.rekeys_child.
167 */
168 static void rekeys_child(private_create_child_sa_t *this, child_sa_t *child_sa)
169 {
170 this->rekeyed_sa = child_sa;
171 }
172
173 /**
174 * Implementation of create_child_sa_t.cancel.
175 */
176 static void cancel(private_create_child_sa_t *this)
177 {
178 this->rekeyed_sa = NULL;
179 this->lost = TRUE;
180 }
181
182 /**
183 * Implementation of transaction_t.get_request.
184 */
185 static status_t get_request(private_create_child_sa_t *this, message_t **result)
186 {
187 message_t *request;
188 host_t *me, *other;
189 identification_t *my_id, *other_id;
190
191 /* check if we already have built a message (retransmission) */
192 if (this->message)
193 {
194 *result = this->message;
195 return SUCCESS;
196 }
197
198 /* check if we are not already rekeying */
199 if (this->rekeyed_sa)
200 {
201 switch (this->rekeyed_sa->get_state(this->rekeyed_sa))
202 {
203 case CHILD_REKEYING:
204 DBG1(SIG_DBG_IKE,
205 "rekeying a CHILD_SA which is already rekeying, aborted");
206 return FAILED;
207 case CHILD_DELETING:
208 DBG1(SIG_DBG_IKE,
209 "rekeying a CHILD_SA which is deleting, aborted");
210 return FAILED;
211 default:
212 break;
213 }
214 this->rekeyed_sa->set_state(this->rekeyed_sa, CHILD_REKEYING);
215 }
216
217 me = this->ike_sa->get_my_host(this->ike_sa);
218 other = this->ike_sa->get_other_host(this->ike_sa);
219 my_id = this->ike_sa->get_my_id(this->ike_sa);
220 other_id = this->ike_sa->get_other_id(this->ike_sa);
221
222 /* build the request */
223 request = message_create();
224 request->set_source(request, me->clone(me));
225 request->set_destination(request, other->clone(other));
226 request->set_exchange_type(request, CREATE_CHILD_SA);
227 request->set_request(request, TRUE);
228 request->set_ike_sa_id(request, this->ike_sa->get_id(this->ike_sa));
229 *result = request;
230 this->message = request;
231
232 { /* build SA payload */
233 sa_payload_t *sa_payload;
234 linked_list_t *proposals;
235 bool use_natt;
236
237 /* get a policy, if we are rekeying */
238 if (this->rekeyed_sa)
239 {
240 linked_list_t *my_ts, *other_ts;
241 identification_t *my_id, *other_id;
242
243 my_ts = this->rekeyed_sa->get_my_traffic_selectors(this->rekeyed_sa);
244 other_ts = this->rekeyed_sa->get_other_traffic_selectors(this->rekeyed_sa);
245 my_id = this->ike_sa->get_my_id(this->ike_sa);
246 other_id = this->ike_sa->get_other_id(this->ike_sa);
247
248 this->policy = charon->policies->get_policy(charon->policies,
249 my_id, other_id,
250 my_ts, other_ts,
251 me, other);
252
253 this->reqid = this->rekeyed_sa->get_reqid(this->rekeyed_sa);
254
255 if (this->policy == NULL)
256 {
257 DBG1(SIG_DBG_IKE, "no policy found to rekey "
258 "CHILD_SA with reqid %d", this->reqid);
259 return FAILED;
260 }
261 }
262
263 proposals = this->policy->get_proposals(this->policy);
264 use_natt = this->ike_sa->is_natt_enabled(this->ike_sa);
265 this->child_sa = child_sa_create(this->reqid, me, other, my_id, other_id,
266 this->policy->get_soft_lifetime(this->policy),
267 this->policy->get_hard_lifetime(this->policy),
268 this->policy->get_updown(this->policy),
269 this->policy->get_hostaccess(this->policy),
270 use_natt);
271 this->child_sa->set_name(this->child_sa, this->policy->get_name(this->policy));
272 if (this->child_sa->alloc(this->child_sa, proposals) != SUCCESS)
273 {
274 DBG1(SIG_DBG_IKE, "could not install CHILD_SA, CHILD_SA creation aborted");
275 return FAILED;
276 }
277 sa_payload = sa_payload_create_from_proposal_list(proposals);
278 proposals->destroy_offset(proposals, offsetof(proposal_t, destroy));
279 request->add_payload(request, (payload_t*)sa_payload);
280 }
281
282 { /* build the NONCE payload for us (initiator) */
283 nonce_payload_t *nonce_payload;
284
285 if (this->randomizer->allocate_pseudo_random_bytes(this->randomizer,
286 NONCE_SIZE, &this->nonce_i) != SUCCESS)
287 {
288 return FAILED;
289 }
290 nonce_payload = nonce_payload_create();
291 nonce_payload->set_nonce(nonce_payload, this->nonce_i);
292 request->add_payload(request, (payload_t*)nonce_payload);
293 }
294
295 { /* build TSi payload */
296 linked_list_t *ts_list;
297 ts_payload_t *ts_payload;
298
299 ts_list = this->policy->get_my_traffic_selectors(this->policy, me);
300 ts_payload = ts_payload_create_from_traffic_selectors(TRUE, ts_list);
301 ts_list->destroy_offset(ts_list, offsetof(traffic_selector_t, destroy));
302 request->add_payload(request, (payload_t*)ts_payload);
303 }
304
305 { /* build TSr payload */
306 linked_list_t *ts_list;
307 ts_payload_t *ts_payload;
308
309 ts_list = this->policy->get_other_traffic_selectors(this->policy, other);
310 ts_payload = ts_payload_create_from_traffic_selectors(FALSE, ts_list);
311 ts_list->destroy_offset(ts_list, offsetof(traffic_selector_t, destroy));
312 request->add_payload(request, (payload_t*)ts_payload);
313 }
314
315 if (this->rekeyed_sa)
316 { /* add REKEY_SA notify if we are rekeying */
317 notify_payload_t *notify;
318 protocol_id_t protocol;
319
320 protocol = this->rekeyed_sa->get_protocol(this->rekeyed_sa);
321 notify = notify_payload_create_from_protocol_and_type(protocol, REKEY_SA);
322 notify->set_spi(notify, this->rekeyed_sa->get_spi(this->rekeyed_sa, TRUE));
323 request->add_payload(request, (payload_t*)notify);
324
325 /* register us as rekeying to detect multiple rekeying */
326 this->rekeyed_sa->set_rekeying_transaction(this->rekeyed_sa, &this->public);
327 }
328
329 this->message_id = this->ike_sa->get_next_message_id(this->ike_sa);
330 request->set_message_id(request, this->message_id);
331
332 return SUCCESS;
333 }
334
335 /**
336 * Handle all kind of notifys
337 */
338 static status_t process_notifys(private_create_child_sa_t *this, notify_payload_t *notify_payload)
339 {
340 notify_type_t notify_type = notify_payload->get_notify_type(notify_payload);
341
342 DBG2(SIG_DBG_IKE, "process notify type %N", notify_type_names, notify_type);
343
344 switch (notify_type)
345 {
346 case SINGLE_PAIR_REQUIRED:
347 {
348 DBG1(SIG_DBG_IKE, "received a SINGLE_PAIR_REQUIRED notify");
349 return FAILED;
350 }
351 case TS_UNACCEPTABLE:
352 {
353 DBG1(SIG_DBG_IKE, "received TS_UNACCEPTABLE notify");
354 return FAILED;
355 }
356 case NO_PROPOSAL_CHOSEN:
357 {
358 DBG1(SIG_DBG_IKE, "received NO_PROPOSAL_CHOSEN notify");
359 return FAILED;
360 }
361 case REKEY_SA:
362 {
363 u_int32_t spi;
364 protocol_id_t protocol;
365
366 protocol = notify_payload->get_protocol_id(notify_payload);
367 switch (protocol)
368 {
369 case PROTO_AH:
370 case PROTO_ESP:
371 spi = notify_payload->get_spi(notify_payload);
372 this->rekeyed_sa = this->ike_sa->get_child_sa(this->ike_sa,
373 protocol, spi,
374 FALSE);
375 break;
376 default:
377 break;
378 }
379 return SUCCESS;
380 }
381 default:
382 {
383 if (notify_type < 16383)
384 {
385 DBG1(SIG_DBG_IKE, "received %N notify error, CHILD_SA "
386 "creation failed", notify_type_names, notify_type);
387 return FAILED;
388 }
389 else
390 {
391 DBG1(SIG_DBG_IKE, "received %N notify, ignored",
392 notify_type_names, notify_type);
393 return SUCCESS;
394 }
395 }
396 }
397 }
398
399 /**
400 * Build a notify message.
401 */
402 static void build_notify(notify_type_t type, chunk_t data, message_t *message, bool flush_message)
403 {
404 notify_payload_t *notify;
405
406 if (flush_message)
407 {
408 payload_t *payload;
409 iterator_t *iterator = message->get_payload_iterator(message);
410 while (iterator->iterate(iterator, (void**)&payload))
411 {
412 payload->destroy(payload);
413 iterator->remove(iterator);
414 }
415 iterator->destroy(iterator);
416 }
417
418 notify = notify_payload_create();
419 notify->set_notify_type(notify, type);
420 notify->set_notification_data(notify, data);
421 message->add_payload(message, (payload_t*)notify);
422 }
423
424 /**
425 * Install a CHILD_SA for usage
426 */
427 static status_t install_child_sa(private_create_child_sa_t *this, bool initiator)
428 {
429 prf_plus_t *prf_plus;
430 chunk_t seed;
431 status_t status;
432
433 seed = chunk_alloc(this->nonce_i.len + this->nonce_r.len);
434 memcpy(seed.ptr, this->nonce_i.ptr, this->nonce_i.len);
435 memcpy(seed.ptr + this->nonce_i.len, this->nonce_r.ptr, this->nonce_r.len);
436 prf_plus = prf_plus_create(this->ike_sa->get_child_prf(this->ike_sa), seed);
437 chunk_free(&seed);
438
439 if (initiator)
440 {
441 status = this->child_sa->update(this->child_sa, this->proposal, prf_plus);
442 }
443 else
444 {
445 status = this->child_sa->add(this->child_sa, this->proposal, prf_plus);
446 }
447 prf_plus->destroy(prf_plus);
448 if (status != SUCCESS)
449 {
450 return DESTROY_ME;
451 }
452 if (initiator)
453 {
454 status = this->child_sa->add_policies(this->child_sa, this->tsi, this->tsr);
455 }
456 else
457 {
458 status = this->child_sa->add_policies(this->child_sa, this->tsr, this->tsi);
459 }
460 if (status != SUCCESS)
461 {
462 return DESTROY_ME;
463 }
464
465 /* add to IKE_SA, and remove from transaction */
466 this->child_sa->set_state(this->child_sa, CHILD_INSTALLED);
467 this->ike_sa->add_child_sa(this->ike_sa, this->child_sa);
468 this->child_sa = NULL;
469 return SUCCESS;
470 }
471
472 /**
473 * Implementation of transaction_t.get_response.
474 */
475 static status_t get_response(private_create_child_sa_t *this, message_t *request,
476 message_t **result, transaction_t **next)
477 {
478 host_t *me, *other;
479 identification_t *my_id, *other_id;
480 message_t *response;
481 status_t status;
482 iterator_t *payloads;
483 sa_payload_t *sa_request = NULL;
484 nonce_payload_t *nonce_request = NULL;
485 ts_payload_t *tsi_request = NULL;
486 ts_payload_t *tsr_request = NULL;
487 nonce_payload_t *nonce_response;
488
489 /* check if we already have built a response (retransmission) */
490 if (this->message)
491 {
492 *result = this->message;
493 return SUCCESS;
494 }
495
496 me = this->ike_sa->get_my_host(this->ike_sa);
497 other = this->ike_sa->get_other_host(this->ike_sa);
498 my_id = this->ike_sa->get_my_id(this->ike_sa);
499 other_id = this->ike_sa->get_other_id(this->ike_sa);
500 this->message_id = request->get_message_id(request);
501
502 /* set up response */
503 response = message_create();
504 response->set_source(response, me->clone(me));
505 response->set_destination(response, other->clone(other));
506 response->set_exchange_type(response, CREATE_CHILD_SA);
507 response->set_request(response, FALSE);
508 response->set_message_id(response, this->message_id);
509 response->set_ike_sa_id(response, this->ike_sa->get_id(this->ike_sa));
510 this->message = response;
511 *result = response;
512
513 /* check message type */
514 if (request->get_exchange_type(request) != CREATE_CHILD_SA)
515 {
516 DBG1(SIG_DBG_IKE, "CREATE_CHILD_SA response of invalid type, aborted");
517 return FAILED;
518 }
519
520 /* we do not allow the creation of new CHILDren/rekeying when IKE_SA is
521 * rekeying */
522 if (this->ike_sa->get_state(this->ike_sa) == IKE_REKEYING ||
523 this->ike_sa->get_state(this->ike_sa) == IKE_DELETING)
524 {
525 build_notify(NO_ADDITIONAL_SAS, CHUNK_INITIALIZER, response, TRUE);
526 DBG1(SIG_DBG_IKE, "unable to create new CHILD_SAs, as rekeying in progress");
527 return FAILED;
528 }
529
530 /* Iterate over all payloads. */
531 payloads = request->get_payload_iterator(request);
532 while (payloads->has_next(payloads))
533 {
534 payload_t *payload;
535 payloads->current(payloads, (void**)&payload);
536 switch (payload->get_type(payload))
537 {
538 case SECURITY_ASSOCIATION:
539 sa_request = (sa_payload_t*)payload;
540 break;
541 case NONCE:
542 nonce_request = (nonce_payload_t*)payload;
543 break;
544 case TRAFFIC_SELECTOR_INITIATOR:
545 tsi_request = (ts_payload_t*)payload;
546 break;
547 case TRAFFIC_SELECTOR_RESPONDER:
548 tsr_request = (ts_payload_t*)payload;
549 break;
550 case KEY_EXCHANGE:
551 {
552 u_int8_t dh_buffer[] = {0x00, 0x00}; /* MODP_NONE */
553 chunk_t group = chunk_from_buf(dh_buffer);
554 build_notify(INVALID_KE_PAYLOAD, group, response, TRUE);
555 DBG1(SIG_DBG_IKE, "CREATE_CHILD_SA used PFS, sending INVALID_KE_PAYLOAD");
556 return FAILED;
557 }
558 case NOTIFY:
559 {
560 status = process_notifys(this, (notify_payload_t*)payload);
561 if (status != SUCCESS)
562 {
563 payloads->destroy(payloads);
564 return status;
565 }
566 break;
567 }
568 default:
569 {
570 DBG1(SIG_DBG_IKE, "ignoring %N payload",
571 payload_type_names, payload->get_type(payload));
572 break;
573 }
574 }
575 }
576 payloads->destroy(payloads);
577
578 /* check if we have all payloads */
579 if (!(sa_request && nonce_request && tsi_request && tsr_request))
580 {
581 build_notify(INVALID_SYNTAX, CHUNK_INITIALIZER, response, TRUE);
582 DBG1(SIG_DBG_IKE, "request message incomplete, no CHILD_SA created");
583 return FAILED;
584 }
585
586 { /* process nonce payload */
587 this->nonce_i = nonce_request->get_nonce(nonce_request);
588 if (this->randomizer->allocate_pseudo_random_bytes(this->randomizer,
589 NONCE_SIZE, &this->nonce_r) != SUCCESS)
590 {
591 build_notify(NO_PROPOSAL_CHOSEN, CHUNK_INITIALIZER, response, TRUE);
592 return FAILED;
593 }
594 nonce_response = nonce_payload_create();
595 nonce_response->set_nonce(nonce_response, this->nonce_r);
596 }
597
598 { /* get a policy and process traffic selectors */
599 identification_t *my_id, *other_id;
600 linked_list_t *my_ts, *other_ts;
601
602 my_id = this->ike_sa->get_my_id(this->ike_sa);
603 other_id = this->ike_sa->get_other_id(this->ike_sa);
604
605 my_ts = tsr_request->get_traffic_selectors(tsr_request);
606 other_ts = tsi_request->get_traffic_selectors(tsi_request);
607
608 this->policy = charon->policies->get_policy(charon->policies,
609 my_id, other_id,
610 my_ts, other_ts,
611 me, other);
612 if (this->policy)
613 {
614 this->tsr = this->policy->select_my_traffic_selectors(this->policy, my_ts, me);
615 this->tsi = this->policy->select_other_traffic_selectors(this->policy, other_ts, other);
616 }
617 my_ts->destroy_offset(my_ts, offsetof(traffic_selector_t, destroy));
618 other_ts->destroy_offset(other_ts, offsetof(traffic_selector_t, destroy));
619
620 if (this->policy == NULL)
621 {
622 DBG1(SIG_DBG_IKE, "no acceptable policy found, adding TS_UNACCEPTABLE notify");
623 build_notify(TS_UNACCEPTABLE, CHUNK_INITIALIZER, response, TRUE);
624 return FAILED;
625 }
626 }
627
628 { /* process SA payload */
629 linked_list_t *proposal_list;
630 sa_payload_t *sa_response;
631 ts_payload_t *ts_response;
632 bool use_natt;
633 u_int32_t soft_lifetime, hard_lifetime;
634
635 sa_response = sa_payload_create();
636 /* get proposals from request, and select one with ours */
637 proposal_list = sa_request->get_proposals(sa_request);
638 DBG2(SIG_DBG_IKE, "selecting proposals:");
639 this->proposal = this->policy->select_proposal(this->policy, proposal_list);
640 proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy));
641
642 /* do we have a proposal? */
643 if (this->proposal == NULL)
644 {
645 DBG1(SIG_DBG_IKE, "CHILD_SA proposals unacceptable, adding NO_PROPOSAL_CHOSEN notify");
646 build_notify(NO_PROPOSAL_CHOSEN, CHUNK_INITIALIZER, response, TRUE);
647 return FAILED;
648 }
649 /* do we have traffic selectors? */
650 else if (this->tsi->get_count(this->tsi) == 0 || this->tsr->get_count(this->tsr) == 0)
651 {
652 DBG1(SIG_DBG_IKE, "CHILD_SA traffic selectors unacceptable, adding TS_UNACCEPTABLE notify");
653 build_notify(TS_UNACCEPTABLE, CHUNK_INITIALIZER, response, TRUE);
654 return FAILED;
655 }
656 else
657 { /* create child sa */
658 if (this->rekeyed_sa)
659 {
660 this->reqid = this->rekeyed_sa->get_reqid(this->rekeyed_sa);
661 }
662 soft_lifetime = this->policy->get_soft_lifetime(this->policy);
663 hard_lifetime = this->policy->get_hard_lifetime(this->policy);
664 use_natt = this->ike_sa->is_natt_enabled(this->ike_sa);
665 this->child_sa = child_sa_create(this->reqid, me, other, my_id, other_id,
666 soft_lifetime, hard_lifetime,
667 this->policy->get_updown(this->policy),
668 this->policy->get_hostaccess(this->policy),
669 use_natt);
670 this->child_sa->set_name(this->child_sa, this->policy->get_name(this->policy));
671 if (install_child_sa(this, FALSE) != SUCCESS)
672 {
673 DBG1(SIG_DBG_IKE, "installing CHILD_SA failed, adding NO_PROPOSAL_CHOSEN notify");
674 build_notify(NO_PROPOSAL_CHOSEN, CHUNK_INITIALIZER, response, TRUE);
675 return FAILED;
676 }
677 /* add proposal to sa payload */
678 sa_response->add_proposal(sa_response, this->proposal);
679 }
680 response->add_payload(response, (payload_t*)sa_response);
681
682 /* add nonce/ts payload after sa payload */
683 response->add_payload(response, (payload_t *)nonce_response);
684 ts_response = ts_payload_create_from_traffic_selectors(TRUE, this->tsi);
685 response->add_payload(response, (payload_t*)ts_response);
686 ts_response = ts_payload_create_from_traffic_selectors(FALSE, this->tsr);
687 response->add_payload(response, (payload_t*)ts_response);
688 }
689 /* CHILD_SA successfully created. If another transaction is already rekeying
690 * this SA, our lower nonce must be registered for a later nonce compare. */
691 if (this->rekeyed_sa)
692 {
693 private_create_child_sa_t *other;
694
695 other = this->rekeyed_sa->get_rekeying_transaction(this->rekeyed_sa);
696 if (other)
697 {
698 /* store our lower nonce in the simultaneus transaction, it
699 * will later compare it against his nonces when it calls conclude().
700 */
701 if (memcmp(this->nonce_i.ptr, this->nonce_r.ptr,
702 min(this->nonce_i.len, this->nonce_r.len)) < 0)
703 {
704 other->nonce_s = chunk_clone(this->nonce_i);
705 }
706 else
707 {
708 other->nonce_s = chunk_clone(this->nonce_r);
709 }
710 }
711 this->rekeyed_sa->set_state(this->rekeyed_sa, CHILD_REKEYING);
712 }
713 return SUCCESS;
714 }
715
716 /**
717 * Implementation of transaction_t.conclude
718 */
719 static status_t conclude(private_create_child_sa_t *this, message_t *response,
720 transaction_t **next)
721 {
722 iterator_t *payloads;
723 host_t *me, *other;
724 sa_payload_t *sa_payload = NULL;
725 nonce_payload_t *nonce_payload = NULL;
726 ts_payload_t *tsi_payload = NULL;
727 ts_payload_t *tsr_payload = NULL;
728 status_t status;
729 child_sa_t *new_child = NULL;
730 delete_child_sa_t *delete_child_sa;
731
732 /* check message type */
733 if (response->get_exchange_type(response) != CREATE_CHILD_SA)
734 {
735 DBG1(SIG_DBG_IKE, "CREATE_CHILD_SA response of invalid type, aborting");
736 return FAILED;
737 }
738
739 me = this->ike_sa->get_my_host(this->ike_sa);
740 other = this->ike_sa->get_other_host(this->ike_sa);
741
742 /* Iterate over all payloads to collect them */
743 payloads = response->get_payload_iterator(response);
744 while (payloads->has_next(payloads))
745 {
746 payload_t *payload;
747 payloads->current(payloads, (void**)&payload);
748 switch (payload->get_type(payload))
749 {
750 case SECURITY_ASSOCIATION:
751 sa_payload = (sa_payload_t*)payload;
752 break;
753 case NONCE:
754 nonce_payload = (nonce_payload_t*)payload;
755 break;
756 case TRAFFIC_SELECTOR_INITIATOR:
757 tsi_payload = (ts_payload_t*)payload;
758 break;
759 case TRAFFIC_SELECTOR_RESPONDER:
760 tsr_payload = (ts_payload_t*)payload;
761 break;
762 case NOTIFY:
763 {
764 status = process_notifys(this, (notify_payload_t*)payload);
765 if (status != SUCCESS)
766 {
767 payloads->destroy(payloads);
768 return status;
769 }
770 break;
771 }
772 default:
773 {
774 DBG1(SIG_DBG_IKE, "ignoring %N payload",
775 payload_type_names, payload->get_type(payload));
776 break;
777 }
778 }
779 }
780 payloads->destroy(payloads);
781
782 if (!(sa_payload && nonce_payload && tsi_payload && tsr_payload))
783 {
784 DBG1(SIG_DBG_IKE, "response message incomplete, no CHILD_SA built");
785 return FAILED;
786 }
787
788 { /* process NONCE payload */
789 this->nonce_r = nonce_payload->get_nonce(nonce_payload);
790 }
791
792 { /* process traffic selectors for us */
793 linked_list_t *ts_received = tsi_payload->get_traffic_selectors(tsi_payload);
794 this->tsi = this->policy->select_my_traffic_selectors(this->policy, ts_received, me);
795 ts_received->destroy_offset(ts_received, offsetof(traffic_selector_t, destroy));
796 }
797
798 { /* process traffic selectors for other */
799 linked_list_t *ts_received = tsr_payload->get_traffic_selectors(tsr_payload);
800 this->tsr = this->policy->select_other_traffic_selectors(this->policy, ts_received, other);
801 ts_received->destroy_offset(ts_received, offsetof(traffic_selector_t, destroy));
802 }
803
804 { /* process sa payload */
805 linked_list_t *proposal_list;
806
807 proposal_list = sa_payload->get_proposals(sa_payload);
808 /* we have to re-check here if other's selection is valid */
809 this->proposal = this->policy->select_proposal(this->policy, proposal_list);
810 proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy));
811
812 /* everything fine to create CHILD? */
813 if (this->proposal == NULL ||
814 this->tsi->get_count(this->tsi) == 0 ||
815 this->tsr->get_count(this->tsr) == 0)
816 {
817 DBG1(SIG_DBG_IKE, "CHILD_SA creation failed");
818 return FAILED;
819 }
820 new_child = this->child_sa;
821 if (install_child_sa(this, TRUE) != SUCCESS)
822 {
823 DBG1(SIG_DBG_IKE, "installing CHILD_SA failed, no CHILD_SA built");
824 return FAILED;
825 }
826 }
827 /* CHILD_SA successfully created. If the other peer initiated rekeying
828 * in the meantime, we detect this by comparing the rekeying_transaction
829 * of the SA. If it changed, we are not alone. Then we must compare the nonces.
830 * If no simultaneous rekeying is going on, we just initiate the delete of
831 * the superseded SA. */
832 if (this->rekeyed_sa)
833 {
834 /* rekeying finished, update SA status */
835 this->rekeyed_sa->set_rekeying_transaction(this->rekeyed_sa, NULL);
836
837 if (this->nonce_s.ptr)
838 { /* simlutaneous rekeying is going on, not so good */
839 chunk_t this_lowest;
840
841 /* first get our lowest nonce */
842 if (memcmp(this->nonce_i.ptr, this->nonce_r.ptr,
843 min(this->nonce_i.len, this->nonce_r.len)) < 0)
844 {
845 this_lowest = this->nonce_i;
846 }
847 else
848 {
849 this_lowest = this->nonce_r;
850 }
851 /* then compare against other lowest nonce */
852 if (memcmp(this_lowest.ptr, this->nonce_s.ptr,
853 min(this_lowest.len, this->nonce_s.len)) < 0)
854 {
855 DBG1(SIG_DBG_IKE, "detected simultaneous CHILD_SA rekeying, deleting ours");
856 this->lost = TRUE;
857 }
858 else
859 {
860 DBG1(SIG_DBG_IKE, "detected simultaneous CHILD_SA rekeying, but ours is preferred");
861 }
862 }
863 /* delete the old SA if we have won the rekeying nonce compare*/
864 if (!this->lost)
865 {
866 delete_child_sa = delete_child_sa_create(this->ike_sa);
867 delete_child_sa->set_child_sa(delete_child_sa, this->rekeyed_sa);
868 *next = (transaction_t*)delete_child_sa;
869 }
870 }
871 if (this->lost)
872 {
873 /* we have lost simlutaneous rekeying, delete the CHILD_SA we just have created */
874 delete_child_sa = delete_child_sa_create(this->ike_sa);
875 delete_child_sa->set_child_sa(delete_child_sa, new_child);
876 *next = (transaction_t*)delete_child_sa;
877 }
878 return SUCCESS;
879 }
880
881 /**
882 * implements transaction_t.destroy
883 */
884 static void destroy(private_create_child_sa_t *this)
885 {
886 DESTROY_IF(this->message);
887 DESTROY_IF(this->proposal);
888 DESTROY_IF(this->child_sa);
889 DESTROY_IF(this->policy);
890 this->tsi->destroy_offset(this->tsi, offsetof(traffic_selector_t, destroy));
891 this->tsr->destroy_offset(this->tsr, offsetof(traffic_selector_t, destroy));
892 chunk_free(&this->nonce_i);
893 chunk_free(&this->nonce_r);
894 chunk_free(&this->nonce_s);
895 this->randomizer->destroy(this->randomizer);
896 free(this);
897 }
898
899 /*
900 * Described in header.
901 */
902 create_child_sa_t *create_child_sa_create(ike_sa_t *ike_sa)
903 {
904 private_create_child_sa_t *this = malloc_thing(private_create_child_sa_t);
905
906 /* transaction interface functions */
907 this->public.transaction.get_request = (status_t(*)(transaction_t*,message_t**))get_request;
908 this->public.transaction.get_response = (status_t(*)(transaction_t*,message_t*,message_t**,transaction_t**))get_response;
909 this->public.transaction.conclude = (status_t(*)(transaction_t*,message_t*,transaction_t**))conclude;
910 this->public.transaction.get_message_id = (u_int32_t(*)(transaction_t*))get_message_id;
911 this->public.transaction.requested = (u_int32_t(*)(transaction_t*))requested;
912 this->public.transaction.destroy = (void(*)(transaction_t*))destroy;
913
914 /* public functions */
915 this->public.set_policy = (void(*)(create_child_sa_t*,policy_t*))set_policy;
916 this->public.set_reqid = (void(*)(create_child_sa_t*,u_int32_t))set_reqid;
917 this->public.rekeys_child = (void(*)(create_child_sa_t*,child_sa_t*))rekeys_child;
918 this->public.cancel = (void(*)(create_child_sa_t*))cancel;
919
920 /* private data */
921 this->ike_sa = ike_sa;
922 this->message_id = 0;
923 this->message = NULL;
924 this->requested = 0;
925 this->rekey_spi = 0;
926 this->reqid = 0;
927 this->nonce_i = CHUNK_INITIALIZER;
928 this->nonce_r = CHUNK_INITIALIZER;
929 this->nonce_s = CHUNK_INITIALIZER;
930 this->child_sa = NULL;
931 this->rekeyed_sa = NULL;
932 this->lost = FALSE;
933 this->proposal = NULL;
934 this->policy = NULL;
935 this->tsi = NULL;
936 this->tsr = NULL;
937 this->randomizer = randomizer_create();
938
939 return &this->public;
940 }