0c4e18c1c823fba211bfdbd40c978440f01c71bc
[strongswan.git] / src / libcharon / sa / tasks / quick_mode.c
1 /*
2 * Copyright (C) 2011 Martin Willi
3 * Copyright (C) 2011 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "quick_mode.h"
17
18 #include <string.h>
19
20 #include <daemon.h>
21 #include <sa/keymat_v1.h>
22 #include <encoding/payloads/sa_payload.h>
23 #include <encoding/payloads/nonce_payload.h>
24 #include <encoding/payloads/id_payload.h>
25
26 typedef struct private_quick_mode_t private_quick_mode_t;
27
28 /**
29 * Private members of a quick_mode_t task.
30 */
31 struct private_quick_mode_t {
32
33 /**
34 * Public methods and task_t interface.
35 */
36 quick_mode_t public;
37
38 /**
39 * Assigned IKE_SA.
40 */
41 ike_sa_t *ike_sa;
42
43 /**
44 * TRUE if we are initiating quick mode
45 */
46 bool initiator;
47
48 /**
49 * Traffic selector of initiator
50 */
51 traffic_selector_t *tsi;
52
53 /**
54 * Traffic selector of responder
55 */
56 traffic_selector_t *tsr;
57
58 /**
59 * Initiators nonce
60 */
61 chunk_t nonce_i;
62
63 /**
64 * Responder nonce
65 */
66 chunk_t nonce_r;
67
68 /**
69 * Initiators ESP SPI
70 */
71 u_int32_t spi_i;
72
73 /**
74 * Responder ESP SPI
75 */
76 u_int32_t spi_r;
77
78 /**
79 * selected CHILD_SA proposal
80 */
81 proposal_t *proposal;
82
83 /**
84 * Config of CHILD_SA to establish
85 */
86 child_cfg_t *config;
87
88 /**
89 * CHILD_SA we are about to establish
90 */
91 child_sa_t *child_sa;
92
93 /**
94 * IKEv1 keymat
95 */
96 keymat_v1_t *keymat;
97
98 /** states of quick mode */
99 enum {
100 QM_INIT,
101 QM_NEGOTIATED,
102 } state;
103 };
104
105 /**
106 * Install negotiated CHILD_SA
107 */
108 static bool install(private_quick_mode_t *this)
109 {
110 status_t status, status_i, status_o;
111 chunk_t encr_i, encr_r, integ_i, integ_r;
112 linked_list_t *tsi, *tsr;
113
114 this->child_sa->set_proposal(this->child_sa, this->proposal);
115 this->child_sa->set_state(this->child_sa, CHILD_INSTALLING);
116 this->child_sa->set_mode(this->child_sa, MODE_TUNNEL);
117 this->child_sa->set_protocol(this->child_sa,
118 this->proposal->get_protocol(this->proposal));
119
120 status_i = status_o = FAILED;
121 encr_i = encr_r = integ_i = integ_r = chunk_empty;
122 tsi = linked_list_create();
123 tsr = linked_list_create();
124 tsi->insert_last(tsi, this->tsi);
125 tsr->insert_last(tsr, this->tsr);
126 if (this->keymat->derive_child_keys(this->keymat, this->proposal, NULL,
127 this->spi_i, this->spi_r, this->nonce_i, this->nonce_r,
128 &encr_i, &integ_i, &encr_r, &integ_r))
129 {
130 if (this->initiator)
131 {
132 status_i = this->child_sa->install(this->child_sa, encr_r, integ_r,
133 this->spi_i, 0, TRUE, FALSE, tsi, tsr);
134 status_o = this->child_sa->install(this->child_sa, encr_i, integ_i,
135 this->spi_r, 0, FALSE, FALSE, tsi, tsr);
136 }
137 else
138 {
139 status_i = this->child_sa->install(this->child_sa, encr_i, integ_i,
140 this->spi_r, 0, TRUE, FALSE, tsr, tsi);
141 status_o = this->child_sa->install(this->child_sa, encr_r, integ_r,
142 this->spi_i, 0, FALSE, FALSE, tsr, tsi);
143 }
144 }
145 chunk_clear(&integ_i);
146 chunk_clear(&integ_r);
147 chunk_clear(&encr_i);
148 chunk_clear(&encr_r);
149
150 if (status_i != SUCCESS || status_o != SUCCESS)
151 {
152 DBG1(DBG_IKE, "unable to install %s%s%sIPsec SA (SAD) in kernel",
153 (status_i != SUCCESS) ? "inbound " : "",
154 (status_i != SUCCESS && status_o != SUCCESS) ? "and ": "",
155 (status_o != SUCCESS) ? "outbound " : "");
156 tsi->destroy(tsi);
157 tsr->destroy(tsr);
158 return FALSE;
159 }
160
161 if (this->initiator)
162 {
163 status = this->child_sa->add_policies(this->child_sa, tsi, tsr);
164 }
165 else
166 {
167 status = this->child_sa->add_policies(this->child_sa, tsr, tsi);
168 }
169 tsi->destroy(tsi);
170 tsr->destroy(tsr);
171 if (status != SUCCESS)
172 {
173 DBG1(DBG_IKE, "unable to install IPsec policies (SPD) in kernel");
174 return FALSE;
175 }
176
177 charon->bus->child_keys(charon->bus, this->child_sa, this->initiator,
178 NULL, this->nonce_i, this->nonce_r);
179
180 /* add to IKE_SA, and remove from task */
181 this->child_sa->set_state(this->child_sa, CHILD_INSTALLED);
182 this->ike_sa->add_child_sa(this->ike_sa, this->child_sa);
183
184 DBG0(DBG_IKE, "CHILD_SA %s{%d} established "
185 "with SPIs %.8x_i %.8x_o and TS %#R=== %#R",
186 this->child_sa->get_name(this->child_sa),
187 this->child_sa->get_reqid(this->child_sa),
188 ntohl(this->child_sa->get_spi(this->child_sa, TRUE)),
189 ntohl(this->child_sa->get_spi(this->child_sa, FALSE)),
190 this->child_sa->get_traffic_selectors(this->child_sa, TRUE),
191 this->child_sa->get_traffic_selectors(this->child_sa, FALSE));
192
193 charon->bus->child_updown(charon->bus, this->child_sa, TRUE);
194
195 this->child_sa = NULL;
196
197 return TRUE;
198 }
199
200 METHOD(task_t, build_i, status_t,
201 private_quick_mode_t *this, message_t *message)
202 {
203 switch (this->state)
204 {
205 case QM_INIT:
206 {
207 enumerator_t *enumerator;
208 sa_payload_t *sa_payload;
209 nonce_payload_t *nonce_payload;
210 id_payload_t *id_payload;
211 traffic_selector_t *ts;
212 linked_list_t *list;
213 proposal_t *proposal;
214 rng_t *rng;
215
216 this->child_sa = child_sa_create(
217 this->ike_sa->get_my_host(this->ike_sa),
218 this->ike_sa->get_other_host(this->ike_sa),
219 this->config, 0, FALSE);
220
221 list = this->config->get_proposals(this->config, TRUE);
222
223 this->spi_i = this->child_sa->alloc_spi(this->child_sa, PROTO_ESP);
224 if (!this->spi_i)
225 {
226 DBG1(DBG_IKE, "allocating SPI from kernel failed");
227 return FAILED;
228 }
229 enumerator = list->create_enumerator(list);
230 while (enumerator->enumerate(enumerator, &proposal))
231 {
232 proposal->set_spi(proposal, this->spi_i);
233 }
234 enumerator->destroy(enumerator);
235
236 sa_payload = sa_payload_create_from_proposal_list(
237 SECURITY_ASSOCIATION_V1, list);
238 list->destroy_offset(list, offsetof(proposal_t, destroy));
239 message->add_payload(message, &sa_payload->payload_interface);
240
241 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
242 if (!rng)
243 {
244 DBG1(DBG_IKE, "no RNG found to create nonce");
245 return FAILED;
246 }
247 rng->allocate_bytes(rng, NONCE_SIZE, &this->nonce_i);
248 rng->destroy(rng);
249 nonce_payload = nonce_payload_create(NONCE_V1);
250 nonce_payload->set_nonce(nonce_payload, this->nonce_i);
251 message->add_payload(message, &nonce_payload->payload_interface);
252
253 list = this->config->get_traffic_selectors(this->config, TRUE, NULL,
254 this->ike_sa->get_my_host(this->ike_sa));
255 if (list->get_first(list, (void**)&ts) != SUCCESS)
256 {
257 list->destroy_offset(list, offsetof(traffic_selector_t, destroy));
258 DBG1(DBG_IKE, "traffic selector missing");
259 return FAILED;
260 }
261 id_payload = id_payload_create_from_ts(ts);
262 this->tsi = ts->clone(ts);
263 list->destroy_offset(list, offsetof(traffic_selector_t, destroy));
264 message->add_payload(message, &id_payload->payload_interface);
265
266 list = this->config->get_traffic_selectors(this->config, FALSE, NULL,
267 this->ike_sa->get_other_host(this->ike_sa));
268 if (list->get_first(list, (void**)&ts) != SUCCESS)
269 {
270 list->destroy_offset(list, offsetof(traffic_selector_t, destroy));
271 DBG1(DBG_IKE, "traffic selector missing");
272 return FAILED;
273 }
274 id_payload = id_payload_create_from_ts(ts);
275 this->tsr = ts->clone(ts);
276 list->destroy_offset(list, offsetof(traffic_selector_t, destroy));
277 message->add_payload(message, &id_payload->payload_interface);
278
279 /* TODO-IKEv1: Add HASH(1) */
280
281 return NEED_MORE;
282 }
283 case QM_NEGOTIATED:
284 {
285 /* TODO-IKEv1: Send HASH(3) */
286 return SUCCESS;
287 }
288 default:
289 return FAILED;
290 }
291 }
292
293 METHOD(task_t, process_r, status_t,
294 private_quick_mode_t *this, message_t *message)
295 {
296 switch (this->state)
297 {
298 case QM_INIT:
299 {
300 sa_payload_t *sa_payload;
301 nonce_payload_t *nonce_payload;
302 id_payload_t *id_payload;
303 payload_t *payload;
304 linked_list_t *tsi, *tsr, *list;
305 peer_cfg_t *peer_cfg;
306 host_t *me, *other, *host;
307 enumerator_t *enumerator;
308 bool first = TRUE;
309
310 enumerator = message->create_payload_enumerator(message);
311 while (enumerator->enumerate(enumerator, &payload))
312 {
313 if (payload->get_type(payload) == ID_V1)
314 {
315 id_payload = (id_payload_t*)payload;
316
317 if (first)
318 {
319 this->tsi = id_payload->get_ts(id_payload);
320 first = FALSE;
321 }
322 else
323 {
324 this->tsr = id_payload->get_ts(id_payload);
325 break;
326 }
327 }
328 }
329 enumerator->destroy(enumerator);
330
331 if (!this->tsi)
332 {
333 host = this->ike_sa->get_other_host(this->ike_sa);
334 this->tsi = traffic_selector_create_from_subnet(host->clone(host),
335 host->get_family(host) == AF_INET ? 32 : 128, 0, 0);
336 }
337 if (!this->tsr)
338 {
339 host = this->ike_sa->get_my_host(this->ike_sa);
340 this->tsr = traffic_selector_create_from_subnet(host->clone(host),
341 host->get_family(host) == AF_INET ? 32 : 128, 0, 0);
342 }
343
344 me = this->ike_sa->get_virtual_ip(this->ike_sa, TRUE);
345 if (!me)
346 {
347 me = this->ike_sa->get_my_host(this->ike_sa);
348 }
349 other = this->ike_sa->get_virtual_ip(this->ike_sa, FALSE);
350 if (!other)
351 {
352 other = this->ike_sa->get_other_host(this->ike_sa);
353 }
354 peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
355 tsi = linked_list_create();
356 tsr = linked_list_create();
357 tsi->insert_last(tsi, this->tsi);
358 tsr->insert_last(tsr, this->tsr);
359 this->config = peer_cfg->select_child_cfg(peer_cfg, tsr, tsi,
360 me, other);
361 tsi->destroy(tsi);
362 tsr->destroy(tsr);
363 if (!this->config)
364 {
365 DBG1(DBG_IKE, "no child config found");
366 return FAILED;
367 }
368
369 sa_payload = (sa_payload_t*)message->get_payload(message,
370 SECURITY_ASSOCIATION_V1);
371 if (!sa_payload)
372 {
373 DBG1(DBG_IKE, "sa payload missing");
374 return FAILED;
375 }
376 list = sa_payload->get_proposals(sa_payload);
377 this->proposal = this->config->select_proposal(this->config,
378 list, TRUE, FALSE);
379 list->destroy_offset(list, offsetof(proposal_t, destroy));
380 if (!this->proposal)
381 {
382 DBG1(DBG_IKE, "no matching proposal found");
383 return FAILED;
384 }
385 this->spi_i = this->proposal->get_spi(this->proposal);
386
387 nonce_payload = (nonce_payload_t*)message->get_payload(message,
388 NONCE_V1);
389 if (!nonce_payload)
390 {
391 DBG1(DBG_IKE, "Nonce payload missing");
392 return FAILED;
393 }
394 this->nonce_i = nonce_payload->get_nonce(nonce_payload);
395
396 /* TODO-IKEv1: verify HASH(1) */
397
398 this->child_sa = child_sa_create(
399 this->ike_sa->get_my_host(this->ike_sa),
400 this->ike_sa->get_other_host(this->ike_sa),
401 this->config, 0, FALSE);
402 return NEED_MORE;
403 }
404 case QM_NEGOTIATED:
405 {
406 /* TODO-IKEv1: verify HASH(3) */
407
408 if (!install(this))
409 {
410 return FAILED;
411 }
412
413 return SUCCESS;
414 }
415 default:
416 return FAILED;
417 }
418 }
419
420 METHOD(task_t, build_r, status_t,
421 private_quick_mode_t *this, message_t *message)
422 {
423 switch (this->state)
424 {
425 case QM_INIT:
426 {
427 sa_payload_t *sa_payload;
428 nonce_payload_t *nonce_payload;
429 id_payload_t *id_payload;
430 rng_t *rng;
431
432 this->spi_r = this->child_sa->alloc_spi(this->child_sa, PROTO_ESP);
433 if (!this->spi_r)
434 {
435 DBG1(DBG_IKE, "allocating SPI from kernel failed");
436 return FAILED;
437 }
438 this->proposal->set_spi(this->proposal, this->spi_r);
439
440 sa_payload = sa_payload_create_from_proposal(
441 SECURITY_ASSOCIATION_V1, this->proposal);
442 message->add_payload(message, &sa_payload->payload_interface);
443
444 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
445 if (!rng)
446 {
447 DBG1(DBG_IKE, "no RNG found to create nonce");
448 return FAILED;
449 }
450 rng->allocate_bytes(rng, NONCE_SIZE, &this->nonce_r);
451 rng->destroy(rng);
452 nonce_payload = nonce_payload_create(NONCE_V1);
453 nonce_payload->set_nonce(nonce_payload, this->nonce_r);
454 message->add_payload(message, &nonce_payload->payload_interface);
455
456 id_payload = id_payload_create_from_ts(this->tsi);
457 message->add_payload(message, &id_payload->payload_interface);
458 id_payload = id_payload_create_from_ts(this->tsr);
459 message->add_payload(message, &id_payload->payload_interface);
460
461 /* TODO-IKEv1: add HASH(2) */
462
463 this->state = QM_NEGOTIATED;
464 return NEED_MORE;
465 }
466 default:
467 return FAILED;
468 }
469 }
470
471 METHOD(task_t, process_i, status_t,
472 private_quick_mode_t *this, message_t *message)
473 {
474 switch (this->state)
475 {
476 case QM_INIT:
477 {
478 sa_payload_t *sa_payload;
479 nonce_payload_t *nonce_payload;
480 id_payload_t *id_payload;
481 payload_t *payload;
482 traffic_selector_t *tsi = NULL, *tsr = NULL;
483 linked_list_t *list;
484 enumerator_t *enumerator;
485 host_t *host;
486 bool first = TRUE;
487
488 enumerator = message->create_payload_enumerator(message);
489 while (enumerator->enumerate(enumerator, &payload))
490 {
491 if (payload->get_type(payload) == ID_V1)
492 {
493 id_payload = (id_payload_t*)payload;
494
495 if (first)
496 {
497 tsi = id_payload->get_ts(id_payload);
498 first = FALSE;
499 }
500 else
501 {
502 tsr = id_payload->get_ts(id_payload);
503 break;
504 }
505 }
506 }
507 enumerator->destroy(enumerator);
508
509 if (!tsr)
510 {
511 host = this->ike_sa->get_other_host(this->ike_sa);
512 tsr = traffic_selector_create_from_subnet(host->clone(host),
513 host->get_family(host) == AF_INET ? 32 : 128, 0, 0);
514 }
515 if (!tsi)
516 {
517 host = this->ike_sa->get_my_host(this->ike_sa);
518 tsi = traffic_selector_create_from_subnet(host->clone(host),
519 host->get_family(host) == AF_INET ? 32 : 128, 0, 0);
520 }
521
522 if (!tsr->is_contained_in(tsr, this->tsr) ||
523 !tsi->is_contained_in(tsi, this->tsi))
524 {
525 tsi->destroy(tsi);
526 tsr->destroy(tsr);
527 DBG1(DBG_IKE, "TS mismatch");
528 return FAILED;
529 }
530 this->tsi->destroy(this->tsi);
531 this->tsr->destroy(this->tsr);
532 this->tsi = tsi;
533 this->tsr = tsr;
534
535 sa_payload = (sa_payload_t*)message->get_payload(message,
536 SECURITY_ASSOCIATION_V1);
537 if (!sa_payload)
538 {
539 DBG1(DBG_IKE, "sa payload missing");
540 return FAILED;
541 }
542 list = sa_payload->get_proposals(sa_payload);
543 this->proposal = this->config->select_proposal(this->config,
544 list, TRUE, FALSE);
545 list->destroy_offset(list, offsetof(proposal_t, destroy));
546 if (!this->proposal)
547 {
548 DBG1(DBG_IKE, "no matching proposal found");
549 return FAILED;
550 }
551 this->spi_r = this->proposal->get_spi(this->proposal);
552
553 nonce_payload = (nonce_payload_t*)message->get_payload(message,
554 NONCE_V1);
555 if (!nonce_payload)
556 {
557 DBG1(DBG_IKE, "Nonce payload missing");
558 return FAILED;
559 }
560 this->nonce_r = nonce_payload->get_nonce(nonce_payload);
561
562 /* TODO-IKEv1: verify HASH(2) */
563
564 if (!install(this))
565 {
566 return FAILED;
567 }
568
569 this->state = QM_NEGOTIATED;
570 return NEED_MORE;
571 }
572 default:
573 return FAILED;
574 }
575 }
576
577 METHOD(task_t, get_type, task_type_t,
578 private_quick_mode_t *this)
579 {
580 return TASK_QUICK_MODE;
581 }
582
583 METHOD(task_t, migrate, void,
584 private_quick_mode_t *this, ike_sa_t *ike_sa)
585 {
586 this->ike_sa = ike_sa;
587 }
588
589 METHOD(task_t, destroy, void,
590 private_quick_mode_t *this)
591 {
592 chunk_free(&this->nonce_i);
593 chunk_free(&this->nonce_r);
594 DESTROY_IF(this->tsi);
595 DESTROY_IF(this->tsr);
596 DESTROY_IF(this->proposal);
597 DESTROY_IF(this->child_sa);
598 DESTROY_IF(this->config);
599 free(this);
600 }
601
602 /*
603 * Described in header.
604 */
605 quick_mode_t *quick_mode_create(ike_sa_t *ike_sa, child_cfg_t *config,
606 traffic_selector_t *tsi, traffic_selector_t *tsr)
607 {
608 private_quick_mode_t *this;
609
610 INIT(this,
611 .public = {
612 .task = {
613 .get_type = _get_type,
614 .migrate = _migrate,
615 .destroy = _destroy,
616 },
617 },
618 .ike_sa = ike_sa,
619 .initiator = config != NULL,
620 .config = config,
621 .keymat = (keymat_v1_t*)ike_sa->get_keymat(ike_sa),
622 .state = QM_INIT,
623 );
624
625 if (config)
626 {
627 this->public.task.build = _build_i;
628 this->public.task.process = _process_i;
629 }
630 else
631 {
632 this->public.task.build = _build_r;
633 this->public.task.process = _process_r;
634 }
635
636 return &this->public;
637 }