2 * Copyright (C) 2011 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
5 * Copyright (C) 2011 Martin Willi
6 * Copyright (C) 2011 revosec AG
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>.
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
19 #include "main_mode.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>
34 typedef struct private_main_mode_t private_main_mode_t
;
37 * Private members of a main_mode_t task.
39 struct private_main_mode_t
{
42 * Public methods and task_t interface.
52 * Are we the initiator?
57 * Common phase 1 helper class
62 * IKE config to establish
72 * selected IKE proposal
77 * Negotiated SA lifetime
82 * Negotiated authentication method
86 /** states of main mode */
96 * Set IKE_SA to established state
98 static bool establish(private_main_mode_t
*this)
100 if (!charon
->bus
->authorize(charon
->bus
, TRUE
))
102 DBG1(DBG_IKE
, "final authorization hook forbids IKE_SA, cancelling");
106 DBG0(DBG_IKE
, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
107 this->ike_sa
->get_name(this->ike_sa
),
108 this->ike_sa
->get_unique_id(this->ike_sa
),
109 this->ike_sa
->get_my_host(this->ike_sa
),
110 this->ike_sa
->get_my_id(this->ike_sa
),
111 this->ike_sa
->get_other_host(this->ike_sa
),
112 this->ike_sa
->get_other_id(this->ike_sa
));
114 this->ike_sa
->set_state(this->ike_sa
, IKE_ESTABLISHED
);
115 charon
->bus
->ike_updown(charon
->bus
, this->ike_sa
, TRUE
);
121 * Check for notify errors, return TRUE if error found
123 static bool has_notify_errors(private_main_mode_t
*this, message_t
*message
)
125 enumerator_t
*enumerator
;
129 enumerator
= message
->create_payload_enumerator(message
);
130 while (enumerator
->enumerate(enumerator
, &payload
))
132 if (payload
->get_type(payload
) == NOTIFY_V1
)
134 notify_payload_t
*notify
;
137 notify
= (notify_payload_t
*)payload
;
138 type
= notify
->get_notify_type(notify
);
141 DBG1(DBG_IKE
, "received %N error notify",
142 notify_type_names
, type
);
145 else if (type
== INITIAL_CONTACT_IKEV1
)
147 if (!this->initiator
&& this->state
== MM_AUTH
)
149 /* If authenticated and received INITIAL_CONTACT,
150 * delete any existing IKE_SAs with that peer.
151 * The delete takes place when the SA is checked in due
152 * to other id not known until the 3rd message.*/
153 this->ike_sa
->set_condition(this->ike_sa
,
154 COND_INIT_CONTACT_SEEN
, TRUE
);
159 DBG1(DBG_IKE
, "received %N notify", notify_type_names
, type
);
163 enumerator
->destroy(enumerator
);
169 * Queue a task sending a notify in an INFORMATIONAL exchange
171 static status_t
send_notify(private_main_mode_t
*this, notify_type_t type
)
173 notify_payload_t
*notify
;
174 ike_sa_id_t
*ike_sa_id
;
175 u_int64_t spi_i
, spi_r
;
178 notify
= notify_payload_create_from_protocol_and_type(NOTIFY_V1
,
180 ike_sa_id
= this->ike_sa
->get_id(this->ike_sa
);
181 spi_i
= ike_sa_id
->get_initiator_spi(ike_sa_id
);
182 spi_r
= ike_sa_id
->get_responder_spi(ike_sa_id
);
183 spi
= chunk_cata("cc", chunk_from_thing(spi_i
), chunk_from_thing(spi_r
));
184 notify
->set_spi_data(notify
, spi
);
186 this->ike_sa
->queue_task(this->ike_sa
,
187 (task_t
*)informational_create(this->ike_sa
, notify
));
188 /* cancel all active/passive tasks in favour of informational */
189 this->ike_sa
->flush_queue(this->ike_sa
,
190 this->initiator ? TASK_QUEUE_ACTIVE
: TASK_QUEUE_PASSIVE
);
195 * Queue a delete task if authentication failed as initiator
197 static status_t
send_delete(private_main_mode_t
*this)
199 this->ike_sa
->queue_task(this->ike_sa
,
200 (task_t
*)isakmp_delete_create(this->ike_sa
, TRUE
));
201 /* cancel all active tasks in favour of informational */
202 this->ike_sa
->flush_queue(this->ike_sa
,
203 this->initiator ? TASK_QUEUE_ACTIVE
: TASK_QUEUE_PASSIVE
);
207 METHOD(task_t
, build_i
, status_t
,
208 private_main_mode_t
*this, message_t
*message
)
214 sa_payload_t
*sa_payload
;
215 linked_list_t
*proposals
;
218 DBG0(DBG_IKE
, "initiating Main Mode IKE_SA %s[%d] to %H",
219 this->ike_sa
->get_name(this->ike_sa
),
220 this->ike_sa
->get_unique_id(this->ike_sa
),
221 this->ike_sa
->get_other_host(this->ike_sa
));
222 this->ike_sa
->set_state(this->ike_sa
, IKE_CONNECTING
);
224 this->ike_cfg
= this->ike_sa
->get_ike_cfg(this->ike_sa
);
225 this->peer_cfg
= this->ike_sa
->get_peer_cfg(this->ike_sa
);
226 this->peer_cfg
->get_ref(this->peer_cfg
);
228 this->method
= this->ph1
->get_auth_method(this->ph1
, this->peer_cfg
);
229 if (this->method
== AUTH_NONE
)
231 DBG1(DBG_CFG
, "configuration uses unsupported authentication");
234 this->lifetime
= this->peer_cfg
->get_reauth_time(this->peer_cfg
,
237 { /* fall back to rekey time of no rekey time configured */
238 this->lifetime
= this->peer_cfg
->get_rekey_time(this->peer_cfg
,
241 this->lifetime
+= this->peer_cfg
->get_over_time(this->peer_cfg
);
242 proposals
= this->ike_cfg
->get_proposals(this->ike_cfg
);
243 sa_payload
= sa_payload_create_from_proposals_v1(proposals
,
244 this->lifetime
, 0, this->method
, MODE_NONE
, FALSE
);
245 proposals
->destroy_offset(proposals
, offsetof(proposal_t
, destroy
));
247 message
->add_payload(message
, &sa_payload
->payload_interface
);
249 /* pregenerate message to store SA payload */
250 if (this->ike_sa
->generate_message(this->ike_sa
, message
,
253 DBG1(DBG_IKE
, "pregenerating SA payload failed");
256 packet
->destroy(packet
);
257 if (!this->ph1
->save_sa_payload(this->ph1
, message
))
269 if (!this->ph1
->create_hasher(this->ph1
))
271 return send_notify(this, NO_PROPOSAL_CHOSEN
);
273 if (!this->proposal
->get_algorithm(this->proposal
,
274 DIFFIE_HELLMAN_GROUP
, &group
, NULL
))
276 DBG1(DBG_IKE
, "DH group selection failed");
277 return send_notify(this, NO_PROPOSAL_CHOSEN
);
279 if (!this->ph1
->create_dh(this->ph1
, group
))
281 DBG1(DBG_IKE
, "negotiated DH group not supported");
282 return send_notify(this, INVALID_KEY_INFORMATION
);
284 if (!this->ph1
->add_nonce_ke(this->ph1
, message
))
286 return send_notify(this, INVALID_KEY_INFORMATION
);
293 id_payload_t
*id_payload
;
294 identification_t
*id
;
296 id
= this->ph1
->get_id(this->ph1
, this->peer_cfg
, TRUE
);
299 DBG1(DBG_CFG
, "own identity not known");
300 return send_notify(this, INVALID_ID_INFORMATION
);
302 this->ike_sa
->set_my_id(this->ike_sa
, id
->clone(id
));
303 id_payload
= id_payload_create_from_identification(ID_V1
, id
);
304 message
->add_payload(message
, &id_payload
->payload_interface
);
306 if (!this->ph1
->build_auth(this->ph1
, this->method
, message
,
307 id_payload
->get_encoded(id_payload
)))
309 return send_notify(this, AUTHENTICATION_FAILED
);
312 this->state
= MM_AUTH
;
320 METHOD(task_t
, process_r
, status_t
,
321 private_main_mode_t
*this, message_t
*message
)
328 sa_payload_t
*sa_payload
;
331 this->ike_cfg
= this->ike_sa
->get_ike_cfg(this->ike_sa
);
332 DBG0(DBG_IKE
, "%H is initiating a Main Mode IKE_SA",
333 message
->get_source(message
));
334 this->ike_sa
->set_state(this->ike_sa
, IKE_CONNECTING
);
336 this->ike_sa
->update_hosts(this->ike_sa
,
337 message
->get_destination(message
),
338 message
->get_source(message
), TRUE
);
340 sa_payload
= (sa_payload_t
*)message
->get_payload(message
,
341 SECURITY_ASSOCIATION_V1
);
344 DBG1(DBG_IKE
, "SA payload missing");
345 return send_notify(this, INVALID_PAYLOAD_TYPE
);
347 if (!this->ph1
->save_sa_payload(this->ph1
, message
))
349 return send_notify(this, INVALID_PAYLOAD_TYPE
);
352 list
= sa_payload
->get_proposals(sa_payload
);
353 private = this->ike_sa
->supports_extension(this->ike_sa
,
355 this->proposal
= this->ike_cfg
->select_proposal(this->ike_cfg
,
357 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
360 DBG1(DBG_IKE
, "no proposal found");
361 return send_notify(this, NO_PROPOSAL_CHOSEN
);
363 this->ike_sa
->set_proposal(this->ike_sa
, this->proposal
);
365 this->method
= sa_payload
->get_auth_method(sa_payload
);
366 this->lifetime
= sa_payload
->get_lifetime(sa_payload
);
375 if (!this->ph1
->create_hasher(this->ph1
))
377 return send_notify(this, INVALID_KEY_INFORMATION
);
379 if (!this->proposal
->get_algorithm(this->proposal
,
380 DIFFIE_HELLMAN_GROUP
, &group
, NULL
))
382 DBG1(DBG_IKE
, "DH group selection failed");
383 return send_notify(this, INVALID_KEY_INFORMATION
);
385 if (!this->ph1
->create_dh(this->ph1
, group
))
387 DBG1(DBG_IKE
, "negotiated DH group not supported");
388 return send_notify(this, INVALID_KEY_INFORMATION
);
390 if (!this->ph1
->get_nonce_ke(this->ph1
, message
))
392 return send_notify(this, INVALID_PAYLOAD_TYPE
);
399 id_payload_t
*id_payload
;
400 identification_t
*id
;
402 id_payload
= (id_payload_t
*)message
->get_payload(message
, ID_V1
);
405 DBG1(DBG_IKE
, "IDii payload missing");
406 return send_notify(this, INVALID_PAYLOAD_TYPE
);
409 id
= id_payload
->get_identification(id_payload
);
410 this->ike_sa
->set_other_id(this->ike_sa
, id
);
411 this->peer_cfg
= this->ph1
->select_config(this->ph1
,
412 this->method
, FALSE
, id
);
415 DBG1(DBG_IKE
, "no peer config found");
416 return send_notify(this, AUTHENTICATION_FAILED
);
418 this->ike_sa
->set_peer_cfg(this->ike_sa
, this->peer_cfg
);
420 if (!this->ph1
->verify_auth(this->ph1
, this->method
, message
,
421 id_payload
->get_encoded(id_payload
)))
423 return send_notify(this, AUTHENTICATION_FAILED
);
425 if (!charon
->bus
->authorize(charon
->bus
, FALSE
))
427 DBG1(DBG_IKE
, "Main Mode authorization hook forbids IKE_SA, "
429 return send_notify(this, AUTHENTICATION_FAILED
);
432 this->state
= MM_AUTH
;
433 if (has_notify_errors(this, message
))
444 METHOD(task_t
, build_r
, status_t
,
445 private_main_mode_t
*this, message_t
*message
)
451 sa_payload_t
*sa_payload
;
453 sa_payload
= sa_payload_create_from_proposal_v1(this->proposal
,
454 this->lifetime
, 0, this->method
, MODE_NONE
, FALSE
);
455 message
->add_payload(message
, &sa_payload
->payload_interface
);
461 if (!this->ph1
->add_nonce_ke(this->ph1
, message
))
463 return send_notify(this, INVALID_KEY_INFORMATION
);
465 if (!this->ph1
->derive_keys(this->ph1
, this->peer_cfg
, this->method
))
467 return send_notify(this, INVALID_KEY_INFORMATION
);
473 id_payload_t
*id_payload
;
474 identification_t
*id
;
476 id
= this->ph1
->get_id(this->ph1
, this->peer_cfg
, TRUE
);
479 DBG1(DBG_CFG
, "own identity not known");
480 return send_notify(this, INVALID_ID_INFORMATION
);
482 this->ike_sa
->set_my_id(this->ike_sa
, id
->clone(id
));
484 id_payload
= id_payload_create_from_identification(ID_V1
, id
);
485 message
->add_payload(message
, &id_payload
->payload_interface
);
487 if (!this->ph1
->build_auth(this->ph1
, this->method
, message
,
488 id_payload
->get_encoded(id_payload
)))
490 return send_notify(this, AUTHENTICATION_FAILED
);
493 switch (this->method
)
495 case AUTH_XAUTH_INIT_PSK
:
496 case AUTH_XAUTH_INIT_RSA
:
497 case AUTH_HYBRID_INIT_RSA
:
498 this->ike_sa
->queue_task(this->ike_sa
,
499 (task_t
*)xauth_create(this->ike_sa
, TRUE
));
501 case AUTH_XAUTH_RESP_PSK
:
502 case AUTH_XAUTH_RESP_RSA
:
503 case AUTH_HYBRID_RESP_RSA
:
504 /* TODO-IKEv1: not yet supported */
507 if (!establish(this))
509 return send_notify(this, AUTHENTICATION_FAILED
);
511 lib
->processor
->queue_job(lib
->processor
, (job_t
*)
512 adopt_children_job_create(
513 this->ike_sa
->get_id(this->ike_sa
)));
522 METHOD(task_t
, process_i
, status_t
,
523 private_main_mode_t
*this, message_t
*message
)
530 sa_payload_t
*sa_payload
;
531 auth_method_t method
;
535 sa_payload
= (sa_payload_t
*)message
->get_payload(message
,
536 SECURITY_ASSOCIATION_V1
);
539 DBG1(DBG_IKE
, "SA payload missing");
540 return send_notify(this, INVALID_PAYLOAD_TYPE
);
542 list
= sa_payload
->get_proposals(sa_payload
);
543 private = this->ike_sa
->supports_extension(this->ike_sa
,
545 this->proposal
= this->ike_cfg
->select_proposal(this->ike_cfg
,
547 list
->destroy_offset(list
, offsetof(proposal_t
, destroy
));
550 DBG1(DBG_IKE
, "no proposal found");
551 return send_notify(this, NO_PROPOSAL_CHOSEN
);
553 this->ike_sa
->set_proposal(this->ike_sa
, this->proposal
);
555 lifetime
= sa_payload
->get_lifetime(sa_payload
);
556 if (lifetime
!= this->lifetime
)
558 DBG1(DBG_IKE
, "received lifetime %us does not match configured "
559 "lifetime %us", lifetime
, this->lifetime
);
561 this->lifetime
= lifetime
;
562 method
= sa_payload
->get_auth_method(sa_payload
);
563 if (method
!= this->method
)
565 DBG1(DBG_IKE
, "received %N authentication, but configured %N, "
566 "continue with configured", auth_method_names
, method
,
567 auth_method_names
, this->method
);
573 if (!this->ph1
->get_nonce_ke(this->ph1
, message
))
575 return send_notify(this, INVALID_PAYLOAD_TYPE
);
577 if (!this->ph1
->derive_keys(this->ph1
, this->peer_cfg
, this->method
))
579 return send_notify(this, INVALID_KEY_INFORMATION
);
585 id_payload_t
*id_payload
;
586 identification_t
*id
, *cid
;
588 id_payload
= (id_payload_t
*)message
->get_payload(message
, ID_V1
);
591 DBG1(DBG_IKE
, "IDir payload missing");
592 return send_delete(this);
594 id
= id_payload
->get_identification(id_payload
);
595 cid
= this->ph1
->get_id(this->ph1
, this->peer_cfg
, FALSE
);
596 if (cid
&& !id
->matches(id
, cid
))
598 DBG1(DBG_IKE
, "IDir '%Y' does not match to '%Y'", id
, cid
);
600 return send_delete(this);
602 this->ike_sa
->set_other_id(this->ike_sa
, id
);
604 if (!this->ph1
->verify_auth(this->ph1
, this->method
, message
,
605 id_payload
->get_encoded(id_payload
)))
607 return send_delete(this);
609 if (!charon
->bus
->authorize(charon
->bus
, FALSE
))
611 DBG1(DBG_IKE
, "Main Mode authorization hook forbids IKE_SA, "
613 return send_delete(this);
615 switch (this->method
)
617 case AUTH_XAUTH_INIT_PSK
:
618 case AUTH_XAUTH_INIT_RSA
:
619 case AUTH_HYBRID_INIT_RSA
:
620 /* wait for XAUTH request */
622 case AUTH_XAUTH_RESP_PSK
:
623 case AUTH_XAUTH_RESP_RSA
:
624 case AUTH_HYBRID_RESP_RSA
:
625 /* TODO-IKEv1: not yet */
628 if (!establish(this))
630 return send_delete(this);
634 if (this->peer_cfg
->get_virtual_ip(this->peer_cfg
))
636 this->ike_sa
->queue_task(this->ike_sa
,
637 (task_t
*)mode_config_create(this->ike_sa
, TRUE
));
646 METHOD(task_t
, get_type
, task_type_t
,
647 private_main_mode_t
*this)
649 return TASK_MAIN_MODE
;
652 METHOD(task_t
, migrate
, void,
653 private_main_mode_t
*this, ike_sa_t
*ike_sa
)
655 DESTROY_IF(this->peer_cfg
);
656 DESTROY_IF(this->proposal
);
657 this->ph1
->destroy(this->ph1
);
659 this->ike_sa
= ike_sa
;
660 this->state
= MM_INIT
;
661 this->peer_cfg
= NULL
;
662 this->proposal
= NULL
;
663 this->ph1
= phase1_create(ike_sa
, this->initiator
);
666 METHOD(task_t
, destroy
, void,
667 private_main_mode_t
*this)
669 DESTROY_IF(this->peer_cfg
);
670 DESTROY_IF(this->proposal
);
671 this->ph1
->destroy(this->ph1
);
676 * Described in header.
678 main_mode_t
*main_mode_create(ike_sa_t
*ike_sa
, bool initiator
)
680 private_main_mode_t
*this;
685 .get_type
= _get_type
,
691 .ph1
= phase1_create(ike_sa
, initiator
),
692 .initiator
= initiator
,
698 this->public.task
.build
= _build_i
;
699 this->public.task
.process
= _process_i
;
703 this->public.task
.build
= _build_r
;
704 this->public.task
.process
= _process_r
;
707 return &this->public;