- fixed host-host tunnel traffic selection, host-host works now
[strongswan.git] / src / charon / sa / states / ike_auth_requested.c
1 /**
2 * @file ike_auth_requested.c
3 *
4 * @brief Implementation of ike_auth_requested_t.
5 *
6 */
7
8 /*
9 * Copyright (C) 2005 Jan Hutter, 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 <string.h>
24
25 #include "ike_auth_requested.h"
26
27 #include <daemon.h>
28 #include <encoding/payloads/ts_payload.h>
29 #include <encoding/payloads/sa_payload.h>
30 #include <encoding/payloads/id_payload.h>
31 #include <encoding/payloads/auth_payload.h>
32 #include <encoding/payloads/notify_payload.h>
33 #include <crypto/signers/signer.h>
34 #include <crypto/crypters/crypter.h>
35 #include <sa/states/ike_sa_established.h>
36 #include <sa/authenticator.h>
37 #include <sa/child_sa.h>
38
39 typedef struct private_ike_auth_requested_t private_ike_auth_requested_t;
40
41 /**
42 * Private data of a ike_auth_requested_t object.
43 *
44 */
45 struct private_ike_auth_requested_t {
46 /**
47 * Public interface of ike_auth_requested_t.
48 */
49 ike_auth_requested_t public;
50
51 /**
52 * Assigned IKE_SA.
53 */
54 protected_ike_sa_t *ike_sa;
55
56 /**
57 * SA config, just a copy of the one stored in the ike_sa.
58 */
59 policy_t *policy;
60
61 /**
62 * Received nonce from responder.
63 */
64 chunk_t received_nonce;
65
66 /**
67 * Sent nonce in IKE_SA_INIT request.
68 */
69 chunk_t sent_nonce;
70
71 /**
72 * IKE_SA_INIT-Request in binary form.
73 */
74 chunk_t ike_sa_init_reply_data;
75
76 /**
77 * Proposal to setup CHILD_SA
78 */
79 proposal_t *proposal;
80
81 /**
82 * Traffic selectors applicable at our site
83 */
84 linked_list_t *my_ts;
85
86 /**
87 * Traffic selectors applicable at remote site
88 */
89 linked_list_t *other_ts;
90
91 /**
92 * Child sa created in ike_sa_init_requested
93 */
94 child_sa_t *child_sa;
95
96 /**
97 * Assigned Logger.
98 *
99 * Is logger of ike_sa!
100 */
101 logger_t *logger;
102
103 /**
104 * Process the IDr payload (check if other id is valid)
105 *
106 * @param this calling object
107 * @param idr_payload ID payload of responder
108 * @return
109 * - SUCCESS
110 * - DESTROY_ME
111 */
112 status_t (*process_idr_payload) (private_ike_auth_requested_t *this, id_payload_t *idr_payload);
113
114 /**
115 * Process the SA payload (check if selected proposals are valid, setup child sa)
116 *
117 * @param this calling object
118 * @param sa_payload SA payload of responder
119 *
120 * - SUCCESS
121 * - DESTROY_ME
122 */
123 status_t (*process_sa_payload) (private_ike_auth_requested_t *this, sa_payload_t *sa_payload);
124
125 /**
126 * Process the AUTH payload (check authenticity of message)
127 *
128 * @param this calling object
129 * @param auth_payload AUTH payload of responder
130 * @param other_id_payload ID payload of responder
131 *
132 * - SUCCESS
133 * - DESTROY_ME
134 */
135 status_t (*process_auth_payload) (private_ike_auth_requested_t *this, auth_payload_t *auth_payload, id_payload_t *other_id_payload);
136
137 /**
138 * Process the TS payload (check if selected traffic selectors are valid)
139 *
140 * @param this calling object
141 * @param ts_initiator TRUE if TS payload is TSi, FALSE for TSr
142 * @param ts_payload TS payload of responder
143 *
144 * - SUCCESS
145 * - DESTROY_ME
146 */
147 status_t (*process_ts_payload) (private_ike_auth_requested_t *this, bool ts_initiator, ts_payload_t *ts_payload);
148
149 /**
150 * Process a notify payload
151 *
152 * @param this calling object
153 * @param notify_payload notify payload
154 *
155 * - SUCCESS
156 * - FAILED
157 * - DESTROY_ME
158 */
159 status_t (*process_notify_payload) (private_ike_auth_requested_t *this, notify_payload_t *notify_payload);
160
161 /**
162 * Destroy function called internally of this class after state change to
163 * state IKE_SA_ESTABLISHED succeeded.
164 *
165 * This destroy function does not destroy objects which were passed to the new state.
166 *
167 * @param this calling object
168 */
169 void (*destroy_after_state_change) (private_ike_auth_requested_t *this);
170 };
171
172
173 /**
174 * Implements state_t.process_message
175 */
176 static status_t process_message(private_ike_auth_requested_t *this, message_t *ike_auth_reply)
177 {
178 ts_payload_t *tsi_payload = NULL, *tsr_payload = NULL;
179 id_payload_t *idr_payload = NULL;
180 auth_payload_t *auth_payload = NULL;
181 sa_payload_t *sa_payload = NULL;
182 iterator_t *payloads = NULL;
183 crypter_t *crypter = NULL;
184 signer_t *signer = NULL;
185 status_t status;
186 host_t *my_host, *other_host;
187 identification_t *my_id, *other_id;
188 chunk_t seed;
189 prf_plus_t *prf_plus;
190 connection_t *connection;
191 policy_t *policy;
192
193 if (ike_auth_reply->get_exchange_type(ike_auth_reply) != IKE_AUTH)
194 {
195 this->logger->log(this->logger, ERROR | LEVEL1, "Message of type %s not supported in state ike_auth_requested",
196 mapping_find(exchange_type_m,ike_auth_reply->get_exchange_type(ike_auth_reply)));
197 return FAILED;
198 }
199
200 if (ike_auth_reply->get_request(ike_auth_reply))
201 {
202 this->logger->log(this->logger, ERROR | LEVEL1, "IKE_AUTH requests not allowed state ike_sa_init_responded");
203 return FAILED;
204 }
205
206 /* get signer for verification and crypter for decryption */
207 signer = this->ike_sa->get_signer_responder(this->ike_sa);
208 crypter = this->ike_sa->get_crypter_responder(this->ike_sa);
209
210 /* parse incoming message */
211 status = ike_auth_reply->parse_body(ike_auth_reply, crypter, signer);
212 if (status != SUCCESS)
213 {
214 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply decryption failed. Ignoring message");
215 return status;
216 }
217
218 this->policy = this->ike_sa->get_policy(this->ike_sa);
219
220 /* we collect all payloads, which are processed later. Notify's are processed
221 * in place, since we don't know how may are there.
222 */
223 payloads = ike_auth_reply->get_payload_iterator(ike_auth_reply);
224 while (payloads->has_next(payloads))
225 {
226 payload_t *payload;
227 payloads->current(payloads, (void**)&payload);
228
229 switch (payload->get_type(payload))
230 {
231 case AUTHENTICATION:
232 {
233 auth_payload = (auth_payload_t*)payload;
234 break;
235 }
236 case ID_RESPONDER:
237 {
238 idr_payload = (id_payload_t*)payload;
239 break;
240 }
241 case SECURITY_ASSOCIATION:
242 {
243 sa_payload = (sa_payload_t*)payload;
244 break;
245 }
246 case TRAFFIC_SELECTOR_INITIATOR:
247 {
248 tsi_payload = (ts_payload_t*)payload;
249 break;
250 }
251 case TRAFFIC_SELECTOR_RESPONDER:
252 {
253 tsr_payload = (ts_payload_t*)payload;
254 break;
255 }
256 case NOTIFY:
257 {
258 notify_payload_t *notify_payload = (notify_payload_t *) payload;
259 /* handle the notify directly, abort if no further processing required */
260 status = this->process_notify_payload(this, notify_payload);
261 if (status != SUCCESS)
262 {
263 payloads->destroy(payloads);
264 return status;
265 }
266 }
267 case CERTIFICATE:
268 {
269 /* TODO handle cert payloads */
270 }
271 default:
272 {
273 this->logger->log(this->logger, ERROR|LEVEL1, "Ignoring Payload %s (%d)",
274 mapping_find(payload_type_m, payload->get_type(payload)), payload->get_type(payload));
275 break;
276 }
277 }
278 }
279 /* iterator can be destroyed */
280 payloads->destroy(payloads);
281
282 /* check if we have all payloads */
283 if (!(idr_payload && sa_payload && auth_payload && tsi_payload && tsr_payload))
284 {
285 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply did not contain all required payloads. Deleting IKE_SA");
286 return DESTROY_ME;
287 }
288
289 /* process all payloads */
290 status = this->process_idr_payload(this, idr_payload);
291 if (status != SUCCESS)
292 {
293 return status;
294 }
295 status = this->process_auth_payload(this, auth_payload,idr_payload);
296 if (status != SUCCESS)
297 {
298 return status;
299 }
300 status = this->process_sa_payload(this, sa_payload);
301 if (status != SUCCESS)
302 {
303 return status;
304 }
305 status = this->process_ts_payload(this, TRUE, tsi_payload);
306 if (status != SUCCESS)
307 {
308 return status;
309 }
310 status = this->process_ts_payload(this, FALSE, tsr_payload);
311 if (status != SUCCESS)
312 {
313 return status;
314 }
315
316 /* install child SAs for AH and esp */
317 if (!this->child_sa)
318 {
319 this->logger->log(this->logger, CONTROL, "No CHILD_SA requested, no CHILD_SA built");
320 }
321 else if (!this->proposal)
322 {
323 this->logger->log(this->logger, CONTROL, "Proposal negotiation failed, no CHILD_SA built");
324 this->child_sa->destroy(this->child_sa);
325 this->child_sa = NULL;
326 }
327 else if (this->my_ts->get_count(this->my_ts) == 0 || this->other_ts->get_count(this->other_ts) == 0)
328 {
329 this->logger->log(this->logger, CONTROL, "Traffic selector negotiation failed, no CHILD_SA built");
330 this->child_sa->destroy(this->child_sa);
331 this->child_sa = NULL;
332 }
333 else
334 {
335 seed = chunk_alloc(this->sent_nonce.len + this->received_nonce.len);
336 memcpy(seed.ptr, this->sent_nonce.ptr, this->sent_nonce.len);
337 memcpy(seed.ptr + this->sent_nonce.len, this->received_nonce.ptr, this->received_nonce.len);
338 prf_plus = prf_plus_create(this->ike_sa->get_child_prf(this->ike_sa), seed);
339 chunk_free(&seed);
340
341 status = this->child_sa->update(this->child_sa, this->proposal, prf_plus);
342 prf_plus->destroy(prf_plus);
343 if (status != SUCCESS)
344 {
345 this->logger->log(this->logger, AUDIT, "Could not install CHILD_SA! Deleting IKE_SA");
346 return DESTROY_ME;
347 }
348 status = this->child_sa->add_policies(this->child_sa, this->my_ts, this->other_ts);
349 if (status != SUCCESS)
350 {
351 this->logger->log(this->logger, AUDIT, "Could not install CHILD_SA policy! Deleting IKE_SA");
352 return DESTROY_ME;
353 }
354 this->ike_sa->add_child_sa(this->ike_sa, this->child_sa);
355 }
356
357 this->ike_sa->set_last_replied_message_id(this->ike_sa,ike_auth_reply->get_message_id(ike_auth_reply));
358
359 /* create new state */
360 this->ike_sa->set_new_state(this->ike_sa, (state_t*)ike_sa_established_create(this->ike_sa));
361 this->destroy_after_state_change(this);
362
363 connection = this->ike_sa->get_connection(this->ike_sa);
364 my_host = connection->get_my_host(connection);
365 other_host = connection->get_other_host(connection);
366 policy = this->ike_sa->get_policy(this->ike_sa);
367 my_id = policy->get_my_id(policy);
368 other_id = policy->get_other_id(policy);
369 this->logger->log(this->logger, AUDIT, "IKE_SA established %s[%s]...%s[%s]",
370 my_host->get_address(my_host), my_id->get_string(my_id),
371 other_host->get_address(other_host), other_id->get_string(other_id));
372
373 return SUCCESS;
374 }
375
376 /**
377 * Implements private_ike_auth_requested_t.process_idr_payload
378 */
379 static status_t process_idr_payload(private_ike_auth_requested_t *this, id_payload_t *idr_payload)
380 {
381 identification_t *other_id, *configured_other_id;
382 connection_t *connection;
383
384 other_id = idr_payload->get_identification(idr_payload);
385 configured_other_id = this->policy->get_other_id(this->policy);
386
387 this->logger->log(this->logger, CONTROL|LEVEL1, "configured ID: %s, ID of responder: %s",
388 configured_other_id->get_string(configured_other_id),
389 other_id->get_string(other_id));
390
391 if (!other_id->belongs_to(other_id, configured_other_id))
392 {
393 other_id->destroy(other_id);
394 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained a not acceptable ID. Deleting IKE_SA");
395 return DESTROY_ME;
396 }
397
398 this->policy->update_other_id(this->policy, other_id);
399 return SUCCESS;
400 }
401
402 /**
403 * Implements private_ike_auth_requested_t.process_sa_payload
404 */
405 static status_t process_sa_payload(private_ike_auth_requested_t *this, sa_payload_t *sa_payload)
406 {
407 proposal_t *proposal, *proposal_tmp;
408 linked_list_t *proposal_list;
409
410 /* get his selected proposal */
411 proposal_list = sa_payload->get_proposals(sa_payload);
412 /* check count of proposals */
413 if (proposal_list->get_count(proposal_list) == 0)
414 {
415 /* no proposal? we accept this, but no child sa is built */
416 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply's SA_PAYLOAD didn't contain any proposals. No CHILD_SA created",
417 proposal_list->get_count(proposal_list));
418 proposal_list->destroy(proposal_list);
419 return SUCCESS;
420 }
421 if (proposal_list->get_count(proposal_list) > 1)
422 {
423 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply's SA_PAYLOAD contained %d proposal. Deleting IKE_SA",
424 proposal_list->get_count(proposal_list));
425 while (proposal_list->remove_last(proposal_list, (void**)&proposal) == SUCCESS)
426 {
427 proposal->destroy(proposal);
428 }
429 proposal_list->destroy(proposal_list);
430 return DESTROY_ME;
431 }
432
433 /* we have to re-check here if other's selection is valid */
434 proposal = this->policy->select_proposal(this->policy, proposal_list);
435 /* list not needed anymore */
436 while (proposal_list->remove_last(proposal_list, (void**)&proposal_tmp) == SUCCESS)
437 {
438 proposal_tmp->destroy(proposal_tmp);
439 }
440 proposal_list->destroy(proposal_list);
441 /* got a match? */
442 if (proposal == NULL)
443 {
444 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained a not offered proposal. Deleting IKE_SA");
445 return DESTROY_ME;
446 }
447
448 /* apply proposal */
449 this->proposal = proposal;
450
451 return SUCCESS;
452 }
453
454 /**
455 * Implements private_ike_auth_requested_t.process_auth_payload
456 */
457 static status_t process_auth_payload(private_ike_auth_requested_t *this, auth_payload_t *auth_payload, id_payload_t *other_id_payload)
458 {
459 authenticator_t *authenticator;
460 status_t status;
461
462 authenticator = authenticator_create(this->ike_sa);
463 status = authenticator->verify_auth_data(authenticator,auth_payload,this->ike_sa_init_reply_data,this->sent_nonce,other_id_payload,FALSE);
464 authenticator->destroy(authenticator);
465 if (status != SUCCESS)
466 {
467 this->logger->log(this->logger, AUDIT, "Verification of IKE_AUTH reply failed. Deleting IKE_SA");
468 return DESTROY_ME;
469 }
470
471 this->logger->log(this->logger, CONTROL|LEVEL1, "AUTH data verified successfully");
472 return SUCCESS;
473 }
474
475 /**
476 * Implements private_ike_auth_requested_t.process_ts_payload
477 */
478 static status_t process_ts_payload(private_ike_auth_requested_t *this, bool ts_initiator, ts_payload_t *ts_payload)
479 {
480 linked_list_t *ts_received, *ts_selected;
481 traffic_selector_t *ts;
482
483 /* get ts form payload */
484 ts_received = ts_payload->get_traffic_selectors(ts_payload);
485 /* select ts depending on payload type */
486 if (ts_initiator)
487 {
488 ts_selected = this->policy->select_my_traffic_selectors(this->policy, ts_received);
489 this->my_ts = ts_selected;
490 }
491 else
492 {
493 ts_selected = this->policy->select_other_traffic_selectors(this->policy, ts_received);
494 this->other_ts = ts_selected;
495 }
496 /* check if the responder selected valid proposals */
497 if (ts_selected->get_count(ts_selected) != ts_received->get_count(ts_received))
498 {
499 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained not offered traffic selectors.");
500 }
501
502 /* cleanup */
503 while (ts_received->remove_last(ts_received, (void**)&ts) == SUCCESS)
504 {
505 ts->destroy(ts);
506 }
507 ts_received->destroy(ts_received);
508
509 return SUCCESS;
510 }
511
512 /**
513 * Implements private_ike_auth_requested_t.process_notify_payload
514 */
515 static status_t process_notify_payload(private_ike_auth_requested_t *this, notify_payload_t *notify_payload)
516 {
517 notify_message_type_t notify_message_type = notify_payload->get_notify_message_type(notify_payload);
518
519 this->logger->log(this->logger, CONTROL|LEVEL1, "Process notify type %s",
520 mapping_find(notify_message_type_m, notify_message_type));
521
522 switch (notify_message_type)
523 {
524 case INVALID_SYNTAX:
525 {
526 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained an INVALID_SYNTAX notify. Deleting IKE_SA");
527 return DESTROY_ME;
528
529 }
530 case AUTHENTICATION_FAILED:
531 {
532 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained an AUTHENTICATION_FAILED notify. Deleting IKE_SA");
533 return DESTROY_ME;
534
535 }
536 case SINGLE_PAIR_REQUIRED:
537 {
538 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained a SINGLE_PAIR_REQUIRED notify. Deleting IKE_SA");
539 return DESTROY_ME;
540 }
541 case TS_UNACCEPTABLE:
542 {
543 /* TODO: We currently check only the replied TS payloads, which should be empty. Should
544 * we interpret the notify additionaly? */
545 this->logger->log(this->logger, CONTROL, "IKE_AUTH reply contained a TS_UNACCEPTABLE notify. Ignored");
546 return SUCCESS;
547 }
548 case NO_PROPOSAL_CHOSEN:
549 {
550 /* TODO: We currently check only the replied SA payload, which should be empty. Should
551 * we interpret the notify additionaly? */
552 this->logger->log(this->logger, CONTROL, "IKE_AUTH reply contained a NO_PROPOSAL_CHOSEN notify. Ignored");
553 return SUCCESS;
554 }
555 default:
556 {
557 /*
558 * - In case of unknown error: IKE_SA gets destroyed.
559 * - In case of unknown status: logging
560 */
561
562 if (notify_message_type < 16383)
563 {
564 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply contained an unknown notify error (%d). Deleting IKE_SA",
565 notify_message_type);
566 return DESTROY_ME;
567
568 }
569 else
570 {
571 this->logger->log(this->logger, CONTROL, "IKE_AUTH reply contained an unknown notify (%d), ignored.",
572 notify_message_type);
573 return SUCCESS;
574 }
575 }
576 }
577 }
578
579 /**
580 * Implements state_t.get_state
581 */
582 static ike_sa_state_t get_state(private_ike_auth_requested_t *this)
583 {
584 return IKE_AUTH_REQUESTED;
585 }
586
587 /**
588 * Implements state_t.get_state
589 */
590 static void destroy(private_ike_auth_requested_t *this)
591 {
592 chunk_free(&(this->received_nonce));
593 chunk_free(&(this->sent_nonce));
594 chunk_free(&(this->ike_sa_init_reply_data));
595 if (this->child_sa)
596 {
597 this->child_sa->destroy(this->child_sa);
598 }
599 if (this->my_ts)
600 {
601 traffic_selector_t *ts;
602 while (this->my_ts->remove_last(this->my_ts, (void**)&ts) == SUCCESS)
603 {
604 ts->destroy(ts);
605 }
606 this->my_ts->destroy(this->my_ts);
607 }
608 if (this->other_ts)
609 {
610 traffic_selector_t *ts;
611 while (this->other_ts->remove_last(this->other_ts, (void**)&ts) == SUCCESS)
612 {
613 ts->destroy(ts);
614 }
615 this->other_ts->destroy(this->other_ts);
616 }
617 if (this->proposal)
618 {
619 this->proposal->destroy(this->proposal);
620 }
621 free(this);
622 }
623 /**
624 * Implements protected_ike_sa_t.destroy_after_state_change
625 */
626 static void destroy_after_state_change(private_ike_auth_requested_t *this)
627 {
628 chunk_free(&(this->received_nonce));
629 chunk_free(&(this->sent_nonce));
630 chunk_free(&(this->ike_sa_init_reply_data));
631 if (this->my_ts)
632 {
633 traffic_selector_t *ts;
634 while (this->my_ts->remove_last(this->my_ts, (void**)&ts) == SUCCESS)
635 {
636 ts->destroy(ts);
637 }
638 this->my_ts->destroy(this->my_ts);
639 }
640 if (this->other_ts)
641 {
642 traffic_selector_t *ts;
643 while (this->other_ts->remove_last(this->other_ts, (void**)&ts) == SUCCESS)
644 {
645 ts->destroy(ts);
646 }
647 this->other_ts->destroy(this->other_ts);
648 }
649 if (this->proposal)
650 {
651 this->proposal->destroy(this->proposal);
652 }
653 free(this);
654 }
655
656 /*
657 * Described in header.
658 */
659 ike_auth_requested_t *ike_auth_requested_create(protected_ike_sa_t *ike_sa,chunk_t sent_nonce,chunk_t received_nonce,chunk_t ike_sa_init_reply_data, child_sa_t *child_sa)
660 {
661 private_ike_auth_requested_t *this = malloc_thing(private_ike_auth_requested_t);
662
663 /* interface functions */
664 this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
665 this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
666 this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
667
668 /* private functions */
669 this->process_idr_payload = process_idr_payload;
670 this->process_sa_payload = process_sa_payload;
671 this->process_auth_payload = process_auth_payload;
672 this->process_ts_payload = process_ts_payload;
673 this->process_notify_payload = process_notify_payload;
674 this->destroy_after_state_change = destroy_after_state_change;
675
676 /* private data */
677 this->ike_sa = ike_sa;
678 this->received_nonce = received_nonce;
679 this->sent_nonce = sent_nonce;
680 this->ike_sa_init_reply_data = ike_sa_init_reply_data;
681 this->logger = logger_manager->get_logger(logger_manager, IKE_SA);
682 this->my_ts = NULL;
683 this->other_ts = NULL;
684 this->proposal = NULL;
685 this->child_sa = child_sa;
686
687 return &(this->public);
688 }