ike-cfg: Add option to prefer supplied proposals over locally configured ones
[strongswan.git] / src / libcharon / sa / ikev1 / tasks / main_mode.c
1 /*
2 * Copyright (C) 2011-2012 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2011 Martin Willi
6 * Copyright (C) 2011 revosec AG
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include "main_mode.h"
20
21 #include <string.h>
22
23 #include <daemon.h>
24 #include <sa/ikev1/phase1.h>
25 #include <encoding/payloads/sa_payload.h>
26 #include <encoding/payloads/id_payload.h>
27 #include <encoding/payloads/hash_payload.h>
28 #include <sa/ikev1/tasks/xauth.h>
29 #include <sa/ikev1/tasks/mode_config.h>
30 #include <sa/ikev1/tasks/informational.h>
31 #include <sa/ikev1/tasks/isakmp_delete.h>
32 #include <processing/jobs/adopt_children_job.h>
33 #include <processing/jobs/delete_ike_sa_job.h>
34
35 typedef struct private_main_mode_t private_main_mode_t;
36
37 /**
38 * Private members of a main_mode_t task.
39 */
40 struct private_main_mode_t {
41
42 /**
43 * Public methods and task_t interface.
44 */
45 main_mode_t public;
46
47 /**
48 * Assigned IKE_SA.
49 */
50 ike_sa_t *ike_sa;
51
52 /**
53 * Are we the initiator?
54 */
55 bool initiator;
56
57 /**
58 * Common phase 1 helper class
59 */
60 phase1_t *ph1;
61
62 /**
63 * IKE config to establish
64 */
65 ike_cfg_t *ike_cfg;
66
67 /**
68 * Peer config to use
69 */
70 peer_cfg_t *peer_cfg;
71
72 /**
73 * selected IKE proposal
74 */
75 proposal_t *proposal;
76
77 /**
78 * Negotiated SA lifetime
79 */
80 uint32_t lifetime;
81
82 /**
83 * Negotiated authentication method
84 */
85 auth_method_t method;
86
87 /** states of main mode */
88 enum {
89 MM_INIT,
90 MM_SA,
91 MM_KE,
92 MM_AUTH,
93 } state;
94 };
95
96 /**
97 * Set IKE_SA to established state
98 */
99 static bool establish(private_main_mode_t *this)
100 {
101 if (!charon->bus->authorize(charon->bus, TRUE))
102 {
103 DBG1(DBG_IKE, "final authorization hook forbids IKE_SA, cancelling");
104 return FALSE;
105 }
106
107 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
108 this->ike_sa->get_name(this->ike_sa),
109 this->ike_sa->get_unique_id(this->ike_sa),
110 this->ike_sa->get_my_host(this->ike_sa),
111 this->ike_sa->get_my_id(this->ike_sa),
112 this->ike_sa->get_other_host(this->ike_sa),
113 this->ike_sa->get_other_id(this->ike_sa));
114
115 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
116 charon->bus->ike_updown(charon->bus, this->ike_sa, TRUE);
117
118 return TRUE;
119 }
120
121 /**
122 * Check for notify errors, return TRUE if error found
123 */
124 static bool has_notify_errors(private_main_mode_t *this, message_t *message)
125 {
126 enumerator_t *enumerator;
127 payload_t *payload;
128 bool err = FALSE;
129
130 enumerator = message->create_payload_enumerator(message);
131 while (enumerator->enumerate(enumerator, &payload))
132 {
133 if (payload->get_type(payload) == PLV1_NOTIFY)
134 {
135 notify_payload_t *notify;
136 notify_type_t type;
137
138 notify = (notify_payload_t*)payload;
139 type = notify->get_notify_type(notify);
140 if (type < 16384)
141 {
142 DBG1(DBG_IKE, "received %N error notify",
143 notify_type_names, type);
144 err = TRUE;
145 }
146 else if (type == INITIAL_CONTACT_IKEV1)
147 {
148 if (!this->initiator && this->state == MM_AUTH)
149 {
150 /* If authenticated and received INITIAL_CONTACT,
151 * delete any existing IKE_SAs with that peer.
152 * The delete takes place when the SA is checked in due
153 * to other id not known until the 3rd message.*/
154 this->ike_sa->set_condition(this->ike_sa,
155 COND_INIT_CONTACT_SEEN, TRUE);
156 }
157 }
158 else
159 {
160 DBG1(DBG_IKE, "received %N notify", notify_type_names, type);
161 }
162 }
163 }
164 enumerator->destroy(enumerator);
165
166 return err;
167 }
168
169 /**
170 * Queue a task sending a notify in an INFORMATIONAL exchange
171 */
172 static status_t send_notify(private_main_mode_t *this, notify_type_t type)
173 {
174 notify_payload_t *notify;
175 ike_sa_id_t *ike_sa_id;
176 uint64_t spi_i, spi_r;
177 chunk_t spi;
178
179 notify = notify_payload_create_from_protocol_and_type(PLV1_NOTIFY,
180 PROTO_IKE, type);
181 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
182 spi_i = ike_sa_id->get_initiator_spi(ike_sa_id);
183 spi_r = ike_sa_id->get_responder_spi(ike_sa_id);
184 spi = chunk_cata("cc", chunk_from_thing(spi_i), chunk_from_thing(spi_r));
185 notify->set_spi_data(notify, spi);
186
187 this->ike_sa->queue_task(this->ike_sa,
188 (task_t*)informational_create(this->ike_sa, notify));
189 /* cancel all active/passive tasks in favour of informational */
190 this->ike_sa->flush_queue(this->ike_sa,
191 this->initiator ? TASK_QUEUE_ACTIVE : TASK_QUEUE_PASSIVE);
192 return ALREADY_DONE;
193 }
194
195 /**
196 * Queue a delete task if authentication failed as initiator
197 */
198 static status_t send_delete(private_main_mode_t *this)
199 {
200 this->ike_sa->queue_task(this->ike_sa,
201 (task_t*)isakmp_delete_create(this->ike_sa, TRUE));
202 /* cancel all active tasks in favour of informational */
203 this->ike_sa->flush_queue(this->ike_sa,
204 this->initiator ? TASK_QUEUE_ACTIVE : TASK_QUEUE_PASSIVE);
205 return ALREADY_DONE;
206 }
207
208 /**
209 * Add an INITIAL_CONTACT notify if first contact with peer
210 */
211 static void add_initial_contact(private_main_mode_t *this, message_t *message,
212 identification_t *idi)
213 {
214 identification_t *idr;
215 host_t *host;
216 notify_payload_t *notify;
217 ike_sa_id_t *ike_sa_id;
218 uint64_t spi_i, spi_r;
219 chunk_t spi;
220
221 idr = this->ph1->get_id(this->ph1, this->peer_cfg, FALSE);
222 if (idr && !idr->contains_wildcards(idr))
223 {
224 if (this->peer_cfg->get_unique_policy(this->peer_cfg) != UNIQUE_NO &&
225 this->peer_cfg->get_unique_policy(this->peer_cfg) != UNIQUE_NEVER)
226 {
227 host = this->ike_sa->get_other_host(this->ike_sa);
228 if (!charon->ike_sa_manager->has_contact(charon->ike_sa_manager,
229 idi, idr, host->get_family(host)))
230 {
231 notify = notify_payload_create_from_protocol_and_type(
232 PLV1_NOTIFY, PROTO_IKE, INITIAL_CONTACT_IKEV1);
233 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
234 spi_i = ike_sa_id->get_initiator_spi(ike_sa_id);
235 spi_r = ike_sa_id->get_responder_spi(ike_sa_id);
236 spi = chunk_cata("cc", chunk_from_thing(spi_i),
237 chunk_from_thing(spi_r));
238 notify->set_spi_data(notify, spi);
239 message->add_payload(message, (payload_t*)notify);
240 }
241 }
242 }
243 }
244
245 METHOD(task_t, build_i, status_t,
246 private_main_mode_t *this, message_t *message)
247 {
248 switch (this->state)
249 {
250 case MM_INIT:
251 {
252 sa_payload_t *sa_payload;
253 linked_list_t *proposals;
254 packet_t *packet;
255
256 DBG0(DBG_IKE, "initiating Main Mode IKE_SA %s[%d] to %H",
257 this->ike_sa->get_name(this->ike_sa),
258 this->ike_sa->get_unique_id(this->ike_sa),
259 this->ike_sa->get_other_host(this->ike_sa));
260 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
261
262 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
263 this->peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
264 this->peer_cfg->get_ref(this->peer_cfg);
265
266 this->method = this->ph1->get_auth_method(this->ph1, this->peer_cfg);
267 if (this->method == AUTH_NONE)
268 {
269 DBG1(DBG_CFG, "configuration uses unsupported authentication");
270 return FAILED;
271 }
272 this->lifetime = this->peer_cfg->get_reauth_time(this->peer_cfg,
273 FALSE);
274 if (!this->lifetime)
275 { /* fall back to rekey time of no rekey time configured */
276 this->lifetime = this->peer_cfg->get_rekey_time(this->peer_cfg,
277 FALSE);
278 }
279 this->lifetime += this->peer_cfg->get_over_time(this->peer_cfg);
280 proposals = this->ike_cfg->get_proposals(this->ike_cfg);
281 sa_payload = sa_payload_create_from_proposals_v1(proposals,
282 this->lifetime, 0, this->method, MODE_NONE,
283 ENCAP_NONE, 0);
284 proposals->destroy_offset(proposals, offsetof(proposal_t, destroy));
285
286 message->add_payload(message, &sa_payload->payload_interface);
287
288 /* pregenerate message to store SA payload */
289 if (this->ike_sa->generate_message(this->ike_sa, message,
290 &packet) != SUCCESS)
291 {
292 DBG1(DBG_IKE, "pregenerating SA payload failed");
293 return FAILED;
294 }
295 packet->destroy(packet);
296 if (!this->ph1->save_sa_payload(this->ph1, message))
297 {
298 return FAILED;
299 }
300
301 this->state = MM_SA;
302 return NEED_MORE;
303 }
304 case MM_SA:
305 {
306 uint16_t group;
307
308 if (!this->ph1->create_hasher(this->ph1))
309 {
310 return send_notify(this, NO_PROPOSAL_CHOSEN);
311 }
312 if (!this->proposal->get_algorithm(this->proposal,
313 DIFFIE_HELLMAN_GROUP, &group, NULL))
314 {
315 DBG1(DBG_IKE, "DH group selection failed");
316 return send_notify(this, NO_PROPOSAL_CHOSEN);
317 }
318 if (!this->ph1->create_dh(this->ph1, group))
319 {
320 DBG1(DBG_IKE, "negotiated DH group not supported");
321 return send_notify(this, INVALID_KEY_INFORMATION);
322 }
323 if (!this->ph1->add_nonce_ke(this->ph1, message))
324 {
325 return send_notify(this, INVALID_KEY_INFORMATION);
326 }
327 this->state = MM_KE;
328 return NEED_MORE;
329 }
330 case MM_KE:
331 {
332 id_payload_t *id_payload;
333 identification_t *id;
334
335 id = this->ph1->get_id(this->ph1, this->peer_cfg, TRUE);
336 if (!id)
337 {
338 DBG1(DBG_CFG, "own identity not known");
339 return send_notify(this, INVALID_ID_INFORMATION);
340 }
341 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
342 id_payload = id_payload_create_from_identification(PLV1_ID, id);
343 message->add_payload(message, &id_payload->payload_interface);
344
345 if (!this->ph1->build_auth(this->ph1, this->method, message,
346 id_payload->get_encoded(id_payload)))
347 {
348 return send_notify(this, AUTHENTICATION_FAILED);
349 }
350
351 add_initial_contact(this, message, id);
352
353 this->state = MM_AUTH;
354 return NEED_MORE;
355 }
356 default:
357 return FAILED;
358 }
359 }
360
361 METHOD(task_t, process_r, status_t,
362 private_main_mode_t *this, message_t *message)
363 {
364 switch (this->state)
365 {
366 case MM_INIT:
367 {
368 linked_list_t *list;
369 sa_payload_t *sa_payload;
370 bool private;
371
372 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
373 DBG0(DBG_IKE, "%H is initiating a Main Mode IKE_SA",
374 message->get_source(message));
375 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
376
377 this->ike_sa->update_hosts(this->ike_sa,
378 message->get_destination(message),
379 message->get_source(message), TRUE);
380
381 sa_payload = (sa_payload_t*)message->get_payload(message,
382 PLV1_SECURITY_ASSOCIATION);
383 if (!sa_payload)
384 {
385 DBG1(DBG_IKE, "SA payload missing");
386 return send_notify(this, INVALID_PAYLOAD_TYPE);
387 }
388 if (!this->ph1->save_sa_payload(this->ph1, message))
389 {
390 return send_notify(this, INVALID_PAYLOAD_TYPE);
391 }
392
393 list = sa_payload->get_proposals(sa_payload);
394 private = this->ike_sa->supports_extension(this->ike_sa,
395 EXT_STRONGSWAN);
396 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
397 list, private, TRUE);
398 list->destroy_offset(list, offsetof(proposal_t, destroy));
399 if (!this->proposal)
400 {
401 DBG1(DBG_IKE, "no proposal found");
402 return send_notify(this, NO_PROPOSAL_CHOSEN);
403 }
404 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
405
406 this->method = sa_payload->get_auth_method(sa_payload);
407 this->lifetime = sa_payload->get_lifetime(sa_payload);
408
409 this->state = MM_SA;
410 return NEED_MORE;
411 }
412 case MM_SA:
413 {
414 uint16_t group;
415
416 if (!this->ph1->create_hasher(this->ph1))
417 {
418 return send_notify(this, INVALID_KEY_INFORMATION);
419 }
420 if (!this->proposal->get_algorithm(this->proposal,
421 DIFFIE_HELLMAN_GROUP, &group, NULL))
422 {
423 DBG1(DBG_IKE, "DH group selection failed");
424 return send_notify(this, INVALID_KEY_INFORMATION);
425 }
426 if (!this->ph1->create_dh(this->ph1, group))
427 {
428 DBG1(DBG_IKE, "negotiated DH group not supported");
429 return send_notify(this, INVALID_KEY_INFORMATION);
430 }
431 if (!this->ph1->get_nonce_ke(this->ph1, message))
432 {
433 return send_notify(this, INVALID_PAYLOAD_TYPE);
434 }
435 this->state = MM_KE;
436 return NEED_MORE;
437 }
438 case MM_KE:
439 {
440 id_payload_t *id_payload;
441 identification_t *id;
442
443 id_payload = (id_payload_t*)message->get_payload(message, PLV1_ID);
444 if (!id_payload)
445 {
446 DBG1(DBG_IKE, "IDii payload missing");
447 return send_notify(this, INVALID_PAYLOAD_TYPE);
448 }
449 id = id_payload->get_identification(id_payload);
450 this->ike_sa->set_other_id(this->ike_sa, id);
451
452 while (TRUE)
453 {
454 DESTROY_IF(this->peer_cfg);
455 this->peer_cfg = this->ph1->select_config(this->ph1,
456 this->method, FALSE, id);
457 if (!this->peer_cfg)
458 {
459 return send_notify(this, AUTHENTICATION_FAILED);
460 }
461 this->ike_sa->set_peer_cfg(this->ike_sa, this->peer_cfg);
462
463 if (this->ph1->verify_auth(this->ph1, this->method, message,
464 id_payload->get_encoded(id_payload)))
465 {
466 break;
467 }
468 }
469
470 if (!charon->bus->authorize(charon->bus, FALSE))
471 {
472 DBG1(DBG_IKE, "Main Mode authorization hook forbids IKE_SA, "
473 "cancelling");
474 return send_notify(this, AUTHENTICATION_FAILED);
475 }
476
477 this->state = MM_AUTH;
478 if (has_notify_errors(this, message))
479 {
480 return FAILED;
481 }
482 return NEED_MORE;
483 }
484 default:
485 return FAILED;
486 }
487 }
488
489 METHOD(task_t, build_r, status_t,
490 private_main_mode_t *this, message_t *message)
491 {
492 switch (this->state)
493 {
494 case MM_SA:
495 {
496 sa_payload_t *sa_payload;
497
498 sa_payload = sa_payload_create_from_proposal_v1(this->proposal,
499 this->lifetime, 0, this->method, MODE_NONE,
500 ENCAP_NONE, 0);
501 message->add_payload(message, &sa_payload->payload_interface);
502
503 return NEED_MORE;
504 }
505 case MM_KE:
506 {
507 if (!this->ph1->add_nonce_ke(this->ph1, message))
508 {
509 return send_notify(this, INVALID_KEY_INFORMATION);
510 }
511 if (!this->ph1->derive_keys(this->ph1, this->peer_cfg, this->method))
512 {
513 return send_notify(this, INVALID_KEY_INFORMATION);
514 }
515 return NEED_MORE;
516 }
517 case MM_AUTH:
518 {
519 id_payload_t *id_payload;
520 identification_t *id;
521 adopt_children_job_t *job = NULL;
522 xauth_t *xauth = NULL;
523
524 id = this->ph1->get_id(this->ph1, this->peer_cfg, TRUE);
525 if (!id)
526 {
527 DBG1(DBG_CFG, "own identity not known");
528 return send_notify(this, INVALID_ID_INFORMATION);
529 }
530 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
531
532 id_payload = id_payload_create_from_identification(PLV1_ID, id);
533 message->add_payload(message, &id_payload->payload_interface);
534
535 if (!this->ph1->build_auth(this->ph1, this->method, message,
536 id_payload->get_encoded(id_payload)))
537 {
538 return send_notify(this, AUTHENTICATION_FAILED);
539 }
540
541 switch (this->method)
542 {
543 case AUTH_XAUTH_INIT_PSK:
544 case AUTH_XAUTH_INIT_RSA:
545 case AUTH_HYBRID_INIT_RSA:
546 xauth = xauth_create(this->ike_sa, TRUE);
547 this->ike_sa->queue_task(this->ike_sa, (task_t*)xauth);
548 break;
549 case AUTH_XAUTH_RESP_PSK:
550 case AUTH_XAUTH_RESP_RSA:
551 case AUTH_HYBRID_RESP_RSA:
552 /* wait for XAUTH request */
553 break;
554 default:
555 if (charon->ike_sa_manager->check_uniqueness(
556 charon->ike_sa_manager, this->ike_sa, FALSE))
557 {
558 DBG1(DBG_IKE, "cancelling Main Mode due to uniqueness "
559 "policy");
560 return send_notify(this, AUTHENTICATION_FAILED);
561 }
562 if (!establish(this))
563 {
564 return send_notify(this, AUTHENTICATION_FAILED);
565 }
566 job = adopt_children_job_create(
567 this->ike_sa->get_id(this->ike_sa));
568 break;
569 }
570 if (this->ph1->has_virtual_ip(this->ph1, this->peer_cfg))
571 {
572 if (this->peer_cfg->use_pull_mode(this->peer_cfg))
573 {
574 this->ike_sa->queue_task(this->ike_sa,
575 (task_t*)mode_config_create(this->ike_sa, TRUE, TRUE));
576 }
577 }
578 else if (this->ph1->has_pool(this->ph1, this->peer_cfg))
579 {
580 if (!this->peer_cfg->use_pull_mode(this->peer_cfg))
581 {
582 if (job)
583 {
584 job->queue_task(job, (task_t*)
585 mode_config_create(this->ike_sa, TRUE, FALSE));
586 }
587 else if (xauth)
588 {
589 xauth->queue_mode_config_push(xauth);
590 }
591 else
592 {
593 this->ike_sa->queue_task(this->ike_sa, (task_t*)
594 mode_config_create(this->ike_sa, TRUE, FALSE));
595 }
596 }
597 }
598 if (job)
599 {
600 lib->processor->queue_job(lib->processor, (job_t*)job);
601 }
602 return SUCCESS;
603 }
604 default:
605 return FAILED;
606 }
607 }
608
609 /**
610 * Schedule a timeout for the IKE_SA should it not establish
611 */
612 static void schedule_timeout(ike_sa_t *ike_sa)
613 {
614 job_t *job;
615
616 job = (job_t*)delete_ike_sa_job_create(ike_sa->get_id(ike_sa), FALSE);
617 lib->scheduler->schedule_job(lib->scheduler, job, HALF_OPEN_IKE_SA_TIMEOUT);
618 }
619
620 METHOD(task_t, process_i, status_t,
621 private_main_mode_t *this, message_t *message)
622 {
623 switch (this->state)
624 {
625 case MM_SA:
626 {
627 linked_list_t *list;
628 sa_payload_t *sa_payload;
629 auth_method_t method;
630 uint32_t lifetime;
631 bool private;
632
633 sa_payload = (sa_payload_t*)message->get_payload(message,
634 PLV1_SECURITY_ASSOCIATION);
635 if (!sa_payload)
636 {
637 DBG1(DBG_IKE, "SA payload missing");
638 return send_notify(this, INVALID_PAYLOAD_TYPE);
639 }
640 list = sa_payload->get_proposals(sa_payload);
641 private = this->ike_sa->supports_extension(this->ike_sa,
642 EXT_STRONGSWAN);
643 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
644 list, private, TRUE);
645 list->destroy_offset(list, offsetof(proposal_t, destroy));
646 if (!this->proposal)
647 {
648 DBG1(DBG_IKE, "no proposal found");
649 return send_notify(this, NO_PROPOSAL_CHOSEN);
650 }
651 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
652
653 lifetime = sa_payload->get_lifetime(sa_payload);
654 if (lifetime != this->lifetime)
655 {
656 DBG1(DBG_IKE, "received lifetime %us does not match configured "
657 "lifetime %us", lifetime, this->lifetime);
658 }
659 this->lifetime = lifetime;
660 method = sa_payload->get_auth_method(sa_payload);
661 if (method != this->method)
662 {
663 DBG1(DBG_IKE, "received %N authentication, but configured %N, "
664 "continue with configured", auth_method_names, method,
665 auth_method_names, this->method);
666 }
667 return NEED_MORE;
668 }
669 case MM_KE:
670 {
671 if (!this->ph1->get_nonce_ke(this->ph1, message))
672 {
673 return send_notify(this, INVALID_PAYLOAD_TYPE);
674 }
675 if (!this->ph1->derive_keys(this->ph1, this->peer_cfg, this->method))
676 {
677 return send_notify(this, INVALID_KEY_INFORMATION);
678 }
679 return NEED_MORE;
680 }
681 case MM_AUTH:
682 {
683 id_payload_t *id_payload;
684 identification_t *id, *cid;
685
686 id_payload = (id_payload_t*)message->get_payload(message, PLV1_ID);
687 if (!id_payload)
688 {
689 DBG1(DBG_IKE, "IDir payload missing");
690 return send_delete(this);
691 }
692 id = id_payload->get_identification(id_payload);
693 cid = this->ph1->get_id(this->ph1, this->peer_cfg, FALSE);
694 if (cid && !id->matches(id, cid))
695 {
696 DBG1(DBG_IKE, "IDir '%Y' does not match to '%Y'", id, cid);
697 id->destroy(id);
698 return send_delete(this);
699 }
700 this->ike_sa->set_other_id(this->ike_sa, id);
701
702 if (!this->ph1->verify_auth(this->ph1, this->method, message,
703 id_payload->get_encoded(id_payload)))
704 {
705 return send_delete(this);
706 }
707 if (!charon->bus->authorize(charon->bus, FALSE))
708 {
709 DBG1(DBG_IKE, "Main Mode authorization hook forbids IKE_SA, "
710 "cancelling");
711 return send_delete(this);
712 }
713
714 switch (this->method)
715 {
716 case AUTH_XAUTH_INIT_PSK:
717 case AUTH_XAUTH_INIT_RSA:
718 case AUTH_HYBRID_INIT_RSA:
719 /* wait for XAUTH request */
720 schedule_timeout(this->ike_sa);
721 break;
722 case AUTH_XAUTH_RESP_PSK:
723 case AUTH_XAUTH_RESP_RSA:
724 case AUTH_HYBRID_RESP_RSA:
725 this->ike_sa->queue_task(this->ike_sa,
726 (task_t*)xauth_create(this->ike_sa, TRUE));
727 break;
728 default:
729 if (charon->ike_sa_manager->check_uniqueness(
730 charon->ike_sa_manager, this->ike_sa, FALSE))
731 {
732 DBG1(DBG_IKE, "cancelling Main Mode due to uniqueness "
733 "policy");
734 return send_delete(this);
735 }
736 if (!establish(this))
737 {
738 return send_delete(this);
739 }
740 break;
741 }
742 /* check for and prepare mode config push/pull */
743 if (this->ph1->has_virtual_ip(this->ph1, this->peer_cfg))
744 {
745 if (this->peer_cfg->use_pull_mode(this->peer_cfg))
746 {
747 this->ike_sa->queue_task(this->ike_sa,
748 (task_t*)mode_config_create(this->ike_sa, TRUE, TRUE));
749 }
750 else
751 {
752 schedule_timeout(this->ike_sa);
753 }
754 }
755 else if (this->ph1->has_pool(this->ph1, this->peer_cfg))
756 {
757 if (this->peer_cfg->use_pull_mode(this->peer_cfg))
758 {
759 schedule_timeout(this->ike_sa);
760 }
761 else
762 {
763 this->ike_sa->queue_task(this->ike_sa,
764 (task_t*)mode_config_create(this->ike_sa, TRUE, FALSE));
765 }
766 }
767 return SUCCESS;
768 }
769 default:
770 return FAILED;
771 }
772 }
773
774 METHOD(task_t, get_type, task_type_t,
775 private_main_mode_t *this)
776 {
777 return TASK_MAIN_MODE;
778 }
779
780 METHOD(task_t, migrate, void,
781 private_main_mode_t *this, ike_sa_t *ike_sa)
782 {
783 DESTROY_IF(this->peer_cfg);
784 DESTROY_IF(this->proposal);
785 this->ph1->destroy(this->ph1);
786
787 this->ike_sa = ike_sa;
788 this->state = MM_INIT;
789 this->peer_cfg = NULL;
790 this->proposal = NULL;
791 this->ph1 = phase1_create(ike_sa, this->initiator);
792 }
793
794 METHOD(task_t, destroy, void,
795 private_main_mode_t *this)
796 {
797 DESTROY_IF(this->peer_cfg);
798 DESTROY_IF(this->proposal);
799 this->ph1->destroy(this->ph1);
800 free(this);
801 }
802
803 /*
804 * Described in header.
805 */
806 main_mode_t *main_mode_create(ike_sa_t *ike_sa, bool initiator)
807 {
808 private_main_mode_t *this;
809
810 INIT(this,
811 .public = {
812 .task = {
813 .get_type = _get_type,
814 .migrate = _migrate,
815 .destroy = _destroy,
816 },
817 },
818 .ike_sa = ike_sa,
819 .ph1 = phase1_create(ike_sa, initiator),
820 .initiator = initiator,
821 .state = MM_INIT,
822 );
823
824 if (initiator)
825 {
826 this->public.task.build = _build_i;
827 this->public.task.process = _process_i;
828 }
829 else
830 {
831 this->public.task.build = _build_r;
832 this->public.task.process = _process_r;
833 }
834
835 return &this->public;
836 }