- cleanup of states ike_auth_requested and ike_sa_init_responded
[strongswan.git] / Source / charon / sa / states / ike_sa_init_responded.c
1 /**
2 * @file ike_sa_init_responded.c
3 *
4 * @brief State of a IKE_SA after responding to an IKE_SA_INIT request
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 "ike_sa_init_responded.h"
24
25 #include <daemon.h>
26 #include <utils/allocator.h>
27 #include <sa/authenticator.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 <transforms/signers/signer.h>
34 #include <transforms/crypters/crypter.h>
35 #include <sa/states/ike_sa_established.h>
36
37
38 typedef struct private_ike_sa_init_responded_t private_ike_sa_init_responded_t;
39
40 /**
41 * Private data of a ike_sa_init_responded_t object.
42 *
43 */
44 struct private_ike_sa_init_responded_t {
45 /**
46 * Public interface of ike_sa_init_responded_t.
47 */
48 ike_sa_init_responded_t public;
49
50 /**
51 * Assigned IKE_SA.
52 */
53 protected_ike_sa_t *ike_sa;
54
55 /**
56 * Received nonce.
57 */
58 chunk_t received_nonce;
59
60 /**
61 * Sent nonce.
62 */
63 chunk_t sent_nonce;
64
65 /**
66 * Binary representation of the IKE_SA_INIT response.
67 */
68 chunk_t ike_sa_init_response_data;
69
70 /**
71 * Binary representation of the IKE_SA_INIT request.
72 */
73 chunk_t ike_sa_init_request_data;
74
75 /**
76 * SA config to use.
77 */
78 sa_config_t *sa_config;
79
80 /**
81 * Assigned logger.
82 *
83 * Is logger of ike_sa!
84 */
85 logger_t *logger;
86
87 /**
88 * Process received IDi and IDr payload and build IDr payload for IKE_AUTH response.
89 *
90 * @param this calling object
91 * @param request_idi ID payload representing initiator
92 * @param request_idr ID payload representing responder (May be zero)
93 * @param response The created IDr payload is added to this message_t object
94 * @param response_idr The created IDr payload is also written to this location
95 */
96 status_t (*build_idr_payload) (private_ike_sa_init_responded_t *this,
97 id_payload_t *request_idi,
98 id_payload_t *request_idr,
99 message_t *response,
100 id_payload_t **response_idr);
101
102 /**
103 * Process received SA payload and build SA payload for IKE_AUTH response.
104 *
105 * @param this calling object
106 * @param request SA payload received in IKE_AUTH request
107 * @param response The created SA payload is added to this message_t object
108 */
109 status_t (*build_sa_payload) (private_ike_sa_init_responded_t *this, sa_payload_t *request, message_t *response);
110
111 /**
112 * Process received AUTH payload and build AUTH payload for IKE_AUTH response.
113 *
114 * @param this calling object
115 * @param request AUTH payload received in IKE_AUTH request
116 * @param other_id_payload other ID payload needed to verify AUTH data
117 * @param my_id_payload my ID payload needed to compute AUTH data
118 * @param response The created AUTH payload is added to this message_t object
119 */
120 status_t (*build_auth_payload) (private_ike_sa_init_responded_t *this, auth_payload_t *request,id_payload_t *other_id_payload,id_payload_t *my_id_payload, message_t* response);
121
122 /**
123 * Process received TS payload and build TS payload for IKE_AUTH response.
124 *
125 * @param this calling object
126 * @param is_initiator type of TS payload. TRUE for TSi, FALSE for TSr
127 * @param request TS payload received in IKE_AUTH request
128 * @param response the created TS payload is added to this message_t object
129 */
130 status_t (*build_ts_payload) (private_ike_sa_init_responded_t *this, bool ts_initiator, ts_payload_t *request, message_t *response);
131
132 /**
133 * Sends a IKE_AUTH reply containing a notify payload.
134 *
135 * @param this calling object
136 * @param notify_payload payload to process
137 * @return
138 * - DELETE_ME if IKE_SA should be deleted
139 * - SUCCSS if processed successfull
140 */
141 status_t (*process_notify_payload) (private_ike_sa_init_responded_t *this, notify_payload_t* notify_payload);
142 };
143
144 /**
145 * Implements state_t.get_state
146 */
147 static status_t process_message(private_ike_sa_init_responded_t *this, message_t *request)
148 {
149 id_payload_t *idi_request = NULL, *idr_request = NULL,*idr_response;
150 ts_payload_t *tsi_request = NULL, *tsr_request = NULL;
151 auth_payload_t *auth_request = NULL;
152 sa_payload_t *sa_request = NULL;
153 iterator_t *payloads;
154 message_t *response;
155 crypter_t *crypter;
156 signer_t *signer;
157 status_t status;
158 host_t *my_host, *other_host;
159
160
161 if (request->get_exchange_type(request) != IKE_AUTH)
162 {
163 this->logger->log(this->logger, ERROR | LEVEL1, "Message of type %s not supported in state ike_sa_init_responded",
164 mapping_find(exchange_type_m,request->get_exchange_type(request)));
165 return FAILED;
166 }
167
168 if (!request->get_request(request))
169 {
170 this->logger->log(this->logger, ERROR | LEVEL1, "IKE_AUTH responses not allowed state ike_sa_init_responded");
171 return FAILED;
172 }
173
174 /* get signer for verification and crypter for decryption */
175 signer = this->ike_sa->get_signer_initiator(this->ike_sa);
176 crypter = this->ike_sa->get_crypter_initiator(this->ike_sa);
177
178 status = request->parse_body(request, crypter, signer);
179 if (status != SUCCESS)
180 {
181 if (status == NOT_SUPPORTED)
182 {
183 this->logger->log(this->logger, ERROR | LEVEL1, "IKE_AUTH request contains unsupported payload with critical flag set."
184 "Deleting IKE_SA");
185 this->ike_sa->send_notify(this->ike_sa, IKE_AUTH, UNSUPPORTED_CRITICAL_PAYLOAD, CHUNK_INITIALIZER);
186 return DELETE_ME;
187 }
188 else
189 {
190 this->logger->log(this->logger, AUDIT, "IKE_AUTH request decryption faild. Ignoring message");
191 }
192 return status;
193 }
194
195 /* iterate over incoming payloads. Message is verified, we can be sure there are the required payloads */
196 payloads = request->get_payload_iterator(request);
197 while (payloads->has_next(payloads))
198 {
199 payload_t *payload;
200 payloads->current(payloads, (void**)&payload);
201
202 switch (payload->get_type(payload))
203 {
204 case ID_INITIATOR:
205 {
206 idi_request = (id_payload_t*)payload;
207 break;
208 }
209 case AUTHENTICATION:
210 {
211 auth_request = (auth_payload_t*)payload;
212 break;
213 }
214 case ID_RESPONDER:
215 {
216 idr_request = (id_payload_t*)payload;
217 break;
218 }
219 case SECURITY_ASSOCIATION:
220 {
221 sa_request = (sa_payload_t*)payload;
222 break;
223 }
224
225 case TRAFFIC_SELECTOR_INITIATOR:
226 {
227 tsi_request = (ts_payload_t*)payload;
228 break;
229 }
230 case TRAFFIC_SELECTOR_RESPONDER:
231 {
232 tsr_request = (ts_payload_t*)payload;
233 break;
234 }
235 case NOTIFY:
236 {
237 notify_payload_t *notify_payload = (notify_payload_t *) payload;
238 status = this->process_notify_payload(this, notify_payload);
239 if (status != SUCCESS)
240 {
241 payloads->destroy(payloads);
242 return status;
243 }
244 }
245 case CERTIFICATE:
246 {
247 /* TODO handle cert payloads */
248 }
249 case CERTIFICATE_REQUEST:
250 {
251 /* TODO handle certrequest payloads */
252 }
253 default:
254 {
255 this->logger->log(this->logger, ERROR|LEVEL1, "Ignoring Payload %s (%d)",
256 mapping_find(payload_type_m, payload->get_type(payload)), payload->get_type(payload));
257 break;
258 }
259 }
260 }
261 /* iterator can be destroyed */
262 payloads->destroy(payloads);
263
264 /* check if we have all payloads */
265 if (!(idi_request && sa_request && auth_request && tsi_request && tsr_request))
266 {
267 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply did not contain all required payloads. Deleting IKE_SA");
268 return DELETE_ME;
269 }
270
271 /* build response */
272 this->ike_sa->build_message(this->ike_sa, IKE_AUTH, FALSE, &response);
273
274 /* add payloads to it */
275
276 status = this->build_idr_payload(this, idi_request, idr_request, response,&idr_response);
277 if (status != SUCCESS)
278 {
279 response->destroy(response);
280 return status;
281 }
282 status = this->build_sa_payload(this, sa_request, response);
283 if (status != SUCCESS)
284 {
285 response->destroy(response);
286 return status;
287 }
288 status = this->build_auth_payload(this, auth_request,idi_request, idr_response,response);
289 if (status != SUCCESS)
290 {
291 response->destroy(response);
292 return status;
293 }
294 status = this->build_ts_payload(this, TRUE, tsi_request, response);
295 if (status != SUCCESS)
296 {
297 response->destroy(response);
298 return status;
299 }
300 status = this->build_ts_payload(this, FALSE, tsr_request, response);
301 if (status != SUCCESS)
302 {
303 response->destroy(response);
304 return status;
305 }
306
307 status = this->ike_sa->send_response(this->ike_sa, response);
308 /* message can now be sent (must not be destroyed) */
309 if (status != SUCCESS)
310 {
311 this->logger->log(this->logger, AUDIT, "Unable to send IKE_AUTH reply. Deleting IKE_SA");
312 response->destroy(response);
313 return DELETE_ME;
314 }
315
316 /* create new state */
317 this->ike_sa->set_new_state(this->ike_sa, (state_t*)ike_sa_established_create(this->ike_sa));
318 this->ike_sa->create_delete_established_ike_sa_job(this->ike_sa,this->sa_config->get_ike_sa_lifetime(this->sa_config));
319 this->public.state_interface.destroy(&(this->public.state_interface));
320
321 my_host = this->ike_sa->get_my_host(this->ike_sa);
322 other_host = this->ike_sa->get_other_host(this->ike_sa);
323 this->logger->log(this->logger, AUDIT, "IKE_SA established between %s - %s",
324 my_host->get_address(my_host), other_host->get_address(other_host));
325
326 return SUCCESS;
327 }
328
329 /**
330 * Implementation of private_ike_sa_init_responded_t.build_idr_payload.
331 */
332 static status_t build_idr_payload(private_ike_sa_init_responded_t *this, id_payload_t *request_idi, id_payload_t *request_idr, message_t *response,id_payload_t **response_idr)
333 {
334 identification_t *other_id, *my_id = NULL;
335 init_config_t *init_config;
336 status_t status;
337 id_payload_t *idr_response;
338
339 other_id = request_idi->get_identification(request_idi);
340 if (request_idr)
341 {
342 my_id = request_idr->get_identification(request_idr);
343 }
344
345 /* build new sa config */
346 init_config = this->ike_sa->get_init_config(this->ike_sa);
347 status = charon->configuration_manager->get_sa_config_for_init_config_and_id(charon->configuration_manager,init_config, other_id,my_id, &(this->sa_config));
348 if (status != SUCCESS)
349 {
350 if (my_id)
351 {
352 this->logger->log(this->logger, AUDIT, "IKE_AUTH request uses IDs %s to %s, which we have no config for",
353 other_id->get_string(other_id),my_id->get_string(my_id));
354 my_id->destroy(my_id);
355 }
356 else
357 {
358 this->logger->log(this->logger, AUDIT, "IKE_AUTH request uses ID %s, which we have no config for",
359 other_id->get_string(other_id));
360 }
361 other_id->destroy(other_id);
362 return DELETE_ME;
363 }
364
365 if (my_id)
366 {
367 my_id->destroy(my_id);
368 }
369 other_id->destroy(other_id);
370
371 /* get my id, if not requested */
372 my_id = this->sa_config->get_my_id(this->sa_config);
373
374 /* set sa_config in ike_sa for other states */
375 this->ike_sa->set_sa_config(this->ike_sa, this->sa_config);
376
377 /* build response */
378 idr_response = id_payload_create_from_identification(FALSE, my_id);
379 response->add_payload(response, (payload_t*)idr_response);
380 *response_idr = idr_response;
381
382 return SUCCESS;
383 }
384
385 /**
386 * Implementation of private_ike_sa_init_responded_t.build_sa_payload.
387 */
388 static status_t build_sa_payload(private_ike_sa_init_responded_t *this, sa_payload_t *request, message_t *response)
389 {
390 child_proposal_t *proposals, *proposal_chosen;
391 size_t proposal_count;
392 status_t status;
393 sa_payload_t *sa_response;
394
395 /* dummy spis, until we have a child sa to request them */
396 u_int8_t ah_spi[4] = {0x01, 0x02, 0x03, 0x04};
397 u_int8_t esp_spi[4] = {0x05, 0x06, 0x07, 0x08};
398
399 status = request->get_child_proposals(request, &proposals, &proposal_count);
400 if (status == SUCCESS)
401 {
402 proposal_chosen = this->sa_config->select_proposal(this->sa_config, ah_spi, esp_spi, proposals, proposal_count);
403 if (proposal_chosen != NULL)
404 {
405 sa_response = sa_payload_create_from_child_proposals(proposal_chosen, 1);
406 response->add_payload(response, (payload_t*)sa_response);
407 }
408 else
409 {
410 this->logger->log(this->logger, AUDIT, "IKE_AUTH request did not contain any proposals we accept. Deleting IKE_SA");
411 this->ike_sa->send_notify(this->ike_sa, IKE_AUTH, NO_PROPOSAL_CHOSEN, CHUNK_INITIALIZER);
412 status = DELETE_ME;
413 }
414 }
415 else
416 {
417 this->logger->log(this->logger, AUDIT, "IKE_AUH request did not contain any proposals. Deleting IKE_SA");
418 this->ike_sa->send_notify(this->ike_sa, IKE_AUTH, NO_PROPOSAL_CHOSEN, CHUNK_INITIALIZER);
419 status = DELETE_ME;
420 }
421
422
423 allocator_free(proposal_chosen);
424 allocator_free(proposals);
425
426 return status;
427 }
428
429 /**
430 * Implementation of private_ike_sa_init_responded_t.build_auth_payload.
431 */
432 static status_t build_auth_payload(private_ike_sa_init_responded_t *this, auth_payload_t *auth_request,id_payload_t *other_id_payload,id_payload_t *my_id_payload, message_t* response)
433 {
434 authenticator_t *authenticator;
435 auth_payload_t *auth_reply;
436 status_t status;
437
438 authenticator = authenticator_create(this->ike_sa);
439
440
441 status = authenticator->verify_auth_data(authenticator,auth_request, this->ike_sa_init_request_data,this->sent_nonce,other_id_payload,TRUE);
442
443 if (status != SUCCESS)
444 {
445 this->logger->log(this->logger, AUDIT, "IKE_AUTH request verification failed. Deleting IKE_SA");
446 this->ike_sa->send_notify(this->ike_sa, IKE_AUTH, AUTHENTICATION_FAILED, CHUNK_INITIALIZER);
447 return DELETE_ME;
448 }
449
450
451 status = authenticator->compute_auth_data(authenticator,&auth_reply, this->ike_sa_init_response_data,this->received_nonce,my_id_payload,FALSE);
452 authenticator->destroy(authenticator);
453 if (status != SUCCESS)
454 {
455 this->logger->log(this->logger, AUDIT, "Unable to build authentication data for IKE_AUTH reply. Deleting IKE_SA");
456 return DELETE_ME;
457
458 }
459
460 response->add_payload(response, (payload_t *)auth_reply);
461 return SUCCESS;
462 }
463
464 /**
465 * Implementation of private_ike_sa_init_responded_t.build_ts_payload.
466 */
467 static status_t build_ts_payload(private_ike_sa_init_responded_t *this, bool ts_initiator, ts_payload_t *request, message_t* response)
468 {
469 traffic_selector_t **ts_received, **ts_selected;
470 size_t ts_received_count, ts_selected_count;
471 status_t status = SUCCESS;
472 ts_payload_t *ts_response;
473
474 /* build a reply payload with selected traffic selectors */
475 ts_received_count = request->get_traffic_selectors(request, &ts_received);
476 /* select ts depending on payload type */
477 if (ts_initiator)
478 {
479 ts_selected_count = this->sa_config->select_traffic_selectors_initiator(this->sa_config, ts_received, ts_received_count, &ts_selected);
480 }
481 else
482 {
483 ts_selected_count = this->sa_config->select_traffic_selectors_responder(this->sa_config, ts_received, ts_received_count, &ts_selected);
484 }
485 if(ts_selected_count == 0)
486 {
487 status = DELETE_ME;
488 }
489 else
490 {
491 ts_response = ts_payload_create_from_traffic_selectors(ts_initiator, ts_selected, ts_selected_count);
492 response->add_payload(response, (payload_t*)ts_response);
493 }
494
495 /* cleanup */
496 while(ts_received_count--)
497 {
498 traffic_selector_t *ts = *ts_received + ts_received_count;
499 ts->destroy(ts);
500 }
501 allocator_free(ts_received);
502 while(ts_selected_count--)
503 {
504 traffic_selector_t *ts = *ts_selected + ts_selected_count;
505 ts->destroy(ts);
506 }
507 allocator_free(ts_selected);
508 return status;
509 }
510
511 static status_t process_notify_payload(private_ike_sa_init_responded_t *this, notify_payload_t *notify_payload)
512 {
513 notify_message_type_t notify_message_type = notify_payload->get_notify_message_type(notify_payload);
514
515 this->logger->log(this->logger, CONTROL|LEVEL1, "Process notify type %s for protocol %s",
516 mapping_find(notify_message_type_m, notify_message_type),
517 mapping_find(protocol_id_m, notify_payload->get_protocol_id(notify_payload)));
518
519 if (notify_payload->get_protocol_id(notify_payload) != IKE)
520 {
521 this->logger->log(this->logger, AUDIT, "IKE_AUTH request contained a notify for an invalid protocol.");
522 return DELETE_ME;
523 }
524 switch (notify_message_type)
525 {
526 case SET_WINDOW_SIZE:
527 /*
528 * TODO Increase window size.
529 */
530 case INITIAL_CONTACT:
531 /*
532 * TODO Delete existing IKE_SA's with other Identity.
533 */
534 default:
535 {
536 this->logger->log(this->logger, AUDIT, "IKE_AUTH request contained an unknown notify (%d), ignored.", notify_message_type);
537 }
538 }
539
540 return SUCCESS;
541 }
542
543 /**
544 * Implementation of state_t.get_state.
545 */
546 static ike_sa_state_t get_state(private_ike_sa_init_responded_t *this)
547 {
548 return IKE_SA_INIT_RESPONDED;
549 }
550
551 /**
552 * Implementation of state_t.get_state.
553 */
554 static void destroy(private_ike_sa_init_responded_t *this)
555 {
556 this->logger->log(this->logger, CONTROL | LEVEL3, "Going to destroy ike_sa_init_responded_t state object");
557
558 this->logger->log(this->logger, CONTROL | LEVEL3, "Destroy received nonce");
559 allocator_free_chunk(&(this->received_nonce));
560 this->logger->log(this->logger, CONTROL | LEVEL3, "Destroy sent nonce");
561 allocator_free_chunk(&(this->sent_nonce));
562 this->logger->log(this->logger, CONTROL | LEVEL3, "Destroy IKE_SA_INIT response octets");
563 allocator_free_chunk(&(this->ike_sa_init_response_data));
564 this->logger->log(this->logger, CONTROL | LEVEL3, "Destroy IKE_SA_INIT request octets");
565 allocator_free_chunk(&(this->ike_sa_init_request_data));
566
567 allocator_free(this);
568 }
569
570 /*
571 * Described in header.
572 */
573 ike_sa_init_responded_t *ike_sa_init_responded_create(protected_ike_sa_t *ike_sa, chunk_t received_nonce, chunk_t sent_nonce,chunk_t ike_sa_init_request_data, chunk_t ike_sa_init_response_data)
574 {
575 private_ike_sa_init_responded_t *this = allocator_alloc_thing(private_ike_sa_init_responded_t);
576
577 /* interface functions */
578 this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
579 this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
580 this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
581
582 /* private functions */
583 this->build_idr_payload = build_idr_payload;
584 this->build_sa_payload = build_sa_payload;
585 this->build_auth_payload = build_auth_payload;
586 this->build_ts_payload = build_ts_payload;
587 this->process_notify_payload = process_notify_payload;
588
589 /* private data */
590 this->ike_sa = ike_sa;
591 this->received_nonce = received_nonce;
592 this->sent_nonce = sent_nonce;
593 this->ike_sa_init_response_data = ike_sa_init_response_data;
594 this->ike_sa_init_request_data = ike_sa_init_request_data;
595 this->logger = this->ike_sa->get_logger(this->ike_sa);
596
597 return &(this->public);
598 }