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