- key derivation for child_sa works
[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 <sa/child_sa.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 <transforms/signers/signer.h>
35 #include <transforms/crypters/crypter.h>
36 #include <sa/states/ike_sa_established.h>
37
38
39 typedef struct private_ike_sa_init_responded_t private_ike_sa_init_responded_t;
40
41 /**
42 * Private data of a ike_sa_init_responded_t object.
43 *
44 */
45 struct private_ike_sa_init_responded_t {
46 /**
47 * Public interface of ike_sa_init_responded_t.
48 */
49 ike_sa_init_responded_t public;
50
51 /**
52 * Assigned IKE_SA.
53 */
54 protected_ike_sa_t *ike_sa;
55
56 /**
57 * Received nonce.
58 */
59 chunk_t received_nonce;
60
61 /**
62 * Sent nonce.
63 */
64 chunk_t sent_nonce;
65
66 /**
67 * Binary representation of the IKE_SA_INIT response.
68 */
69 chunk_t ike_sa_init_response_data;
70
71 /**
72 * Binary representation of the IKE_SA_INIT request.
73 */
74 chunk_t ike_sa_init_request_data;
75
76 /**
77 * SA config to use.
78 */
79 sa_config_t *sa_config;
80
81 /**
82 * Assigned logger.
83 *
84 * Is logger of ike_sa!
85 */
86 logger_t *logger;
87
88 /**
89 * Process received IDi and IDr payload and build IDr payload for IKE_AUTH response.
90 *
91 * @param this calling object
92 * @param request_idi ID payload representing initiator
93 * @param request_idr ID payload representing responder (May be zero)
94 * @param response The created IDr payload is added to this message_t object
95 * @param response_idr The created IDr payload is also written to this location
96 */
97 status_t (*build_idr_payload) (private_ike_sa_init_responded_t *this,
98 id_payload_t *request_idi,
99 id_payload_t *request_idr,
100 message_t *response,
101 id_payload_t **response_idr);
102
103 /**
104 * Process received SA payload and build SA payload for IKE_AUTH response.
105 *
106 * @param this calling object
107 * @param request SA payload received in IKE_AUTH request
108 * @param response The created SA payload is added to this message_t object
109 */
110 status_t (*build_sa_payload) (private_ike_sa_init_responded_t *this, sa_payload_t *request, message_t *response);
111
112 /**
113 * Process received AUTH payload and build AUTH payload for IKE_AUTH response.
114 *
115 * @param this calling object
116 * @param request AUTH payload received in IKE_AUTH request
117 * @param other_id_payload other ID payload needed to verify AUTH data
118 * @param my_id_payload my ID payload needed to compute AUTH data
119 * @param response The created AUTH payload is added to this message_t object
120 */
121 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);
122
123 /**
124 * Process received TS payload and build TS payload for IKE_AUTH response.
125 *
126 * @param this calling object
127 * @param is_initiator type of TS payload. TRUE for TSi, FALSE for TSr
128 * @param request TS payload received in IKE_AUTH request
129 * @param response the created TS payload is added to this message_t object
130 */
131 status_t (*build_ts_payload) (private_ike_sa_init_responded_t *this, bool ts_initiator, ts_payload_t *request, message_t *response);
132
133 /**
134 * Sends a IKE_AUTH reply containing a notify payload.
135 *
136 * @param this calling object
137 * @param notify_payload payload to process
138 * @return
139 * - DELETE_ME if IKE_SA should be deleted
140 * - SUCCSS if processed successfull
141 */
142 status_t (*process_notify_payload) (private_ike_sa_init_responded_t *this, notify_payload_t* notify_payload);
143 };
144
145 /**
146 * Implements state_t.get_state
147 */
148 static status_t process_message(private_ike_sa_init_responded_t *this, message_t *request)
149 {
150 id_payload_t *idi_request = NULL, *idr_request = NULL,*idr_response;
151 ts_payload_t *tsi_request = NULL, *tsr_request = NULL;
152 auth_payload_t *auth_request = NULL;
153 sa_payload_t *sa_request = NULL;
154 iterator_t *payloads;
155 message_t *response;
156 crypter_t *crypter;
157 signer_t *signer;
158 status_t status;
159 host_t *my_host, *other_host;
160
161
162 if (request->get_exchange_type(request) != IKE_AUTH)
163 {
164 this->logger->log(this->logger, ERROR | LEVEL1, "Message of type %s not supported in state ike_sa_init_responded",
165 mapping_find(exchange_type_m,request->get_exchange_type(request)));
166 return FAILED;
167 }
168
169 if (!request->get_request(request))
170 {
171 this->logger->log(this->logger, ERROR | LEVEL1, "IKE_AUTH responses not allowed state ike_sa_init_responded");
172 return FAILED;
173 }
174
175 /* get signer for verification and crypter for decryption */
176 signer = this->ike_sa->get_signer_initiator(this->ike_sa);
177 crypter = this->ike_sa->get_crypter_initiator(this->ike_sa);
178
179 status = request->parse_body(request, crypter, signer);
180 if (status != SUCCESS)
181 {
182 if (status == NOT_SUPPORTED)
183 {
184 this->logger->log(this->logger, ERROR | LEVEL1, "IKE_AUTH request contains unsupported payload with critical flag set."
185 "Deleting IKE_SA");
186 this->ike_sa->send_notify(this->ike_sa, IKE_AUTH, UNSUPPORTED_CRITICAL_PAYLOAD, CHUNK_INITIALIZER);
187 return DELETE_ME;
188 }
189 else
190 {
191 this->logger->log(this->logger, AUDIT, "IKE_AUTH request decryption faild. Ignoring message");
192 }
193 return status;
194 }
195
196 /* iterate over incoming payloads. Message is verified, we can be sure there are the required payloads */
197 payloads = request->get_payload_iterator(request);
198 while (payloads->has_next(payloads))
199 {
200 payload_t *payload;
201 payloads->current(payloads, (void**)&payload);
202
203 switch (payload->get_type(payload))
204 {
205 case ID_INITIATOR:
206 {
207 idi_request = (id_payload_t*)payload;
208 break;
209 }
210 case AUTHENTICATION:
211 {
212 auth_request = (auth_payload_t*)payload;
213 break;
214 }
215 case ID_RESPONDER:
216 {
217 idr_request = (id_payload_t*)payload;
218 break;
219 }
220 case SECURITY_ASSOCIATION:
221 {
222 sa_request = (sa_payload_t*)payload;
223 break;
224 }
225
226 case TRAFFIC_SELECTOR_INITIATOR:
227 {
228 tsi_request = (ts_payload_t*)payload;
229 break;
230 }
231 case TRAFFIC_SELECTOR_RESPONDER:
232 {
233 tsr_request = (ts_payload_t*)payload;
234 break;
235 }
236 case NOTIFY:
237 {
238 notify_payload_t *notify_payload = (notify_payload_t *) payload;
239 status = this->process_notify_payload(this, notify_payload);
240 if (status != SUCCESS)
241 {
242 payloads->destroy(payloads);
243 return status;
244 }
245 }
246 case CERTIFICATE:
247 {
248 /* TODO handle cert payloads */
249 }
250 case CERTIFICATE_REQUEST:
251 {
252 /* TODO handle certrequest payloads */
253 }
254 default:
255 {
256 this->logger->log(this->logger, ERROR|LEVEL1, "Ignoring payload %s (%d)",
257 mapping_find(payload_type_m, payload->get_type(payload)), payload->get_type(payload));
258 break;
259 }
260 }
261 }
262 /* iterator can be destroyed */
263 payloads->destroy(payloads);
264
265 /* check if we have all payloads */
266 if (!(idi_request && sa_request && auth_request && tsi_request && tsr_request))
267 {
268 this->logger->log(this->logger, AUDIT, "IKE_AUTH reply did not contain all required payloads. Deleting IKE_SA");
269 return DELETE_ME;
270 }
271
272 /* build response */
273 this->ike_sa->build_message(this->ike_sa, IKE_AUTH, FALSE, &response);
274
275 /* add payloads to it */
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 */my_host = this->ike_sa->get_my_host(this->ike_sa);
317 other_host = this->ike_sa->get_other_host(this->ike_sa);
318 this->logger->log(this->logger, AUDIT, "IKE_SA established between %s - %s, authenticated peer with %s",
319 my_host->get_address(my_host), other_host->get_address(other_host),
320 mapping_find(auth_method_m, auth_request->get_auth_method(auth_request)));
321
322 this->ike_sa->create_delete_established_ike_sa_job(this->ike_sa,this->sa_config->get_ike_sa_lifetime(this->sa_config));
323 this->ike_sa->set_new_state(this->ike_sa, (state_t*)ike_sa_established_create(this->ike_sa));
324 this->public.state_interface.destroy(&(this->public.state_interface));
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 *proposal, *proposal_tmp;
391 linked_list_t *proposal_list;
392 sa_payload_t *sa_response;
393 child_sa_t *child_sa;
394 prf_plus_t *prf_plus;
395 chunk_t seed;
396
397 /* TODO: child sa stuff */
398
399 /* get proposals from request */
400 proposal_list = request->get_child_proposals(request);
401 if (proposal_list->get_count(proposal_list) == 0)
402 {
403 /* if the other side did not offer any proposals, we do not create child sa's */
404 this->logger->log(this->logger, AUDIT, "IKE_AUH request did not contain any proposals. No CHILD_SA created");
405 sa_response = sa_payload_create();
406 response->add_payload(response, (payload_t*)sa_response);
407 proposal_list->destroy(proposal_list);
408 return SUCCESS;
409 }
410
411 /* now select a proposal */
412 this->logger->log(this->logger, CONTROL|LEVEL1, "Selecting proposals:");
413 proposal = this->sa_config->select_proposal(this->sa_config, proposal_list);
414 /* list is not needed anymore */
415 while (proposal_list->remove_last(proposal_list, (void**)&proposal_tmp) == SUCCESS)
416 {
417 proposal_tmp->destroy(proposal_tmp);
418 }
419 proposal_list->destroy(proposal_list);
420 /* do we have a proposal */
421 if (proposal == NULL)
422 {
423 this->logger->log(this->logger, AUDIT, "IKE_AUTH request did not contain any proposals we accept. Deleting IKE_SA");
424 this->ike_sa->send_notify(this->ike_sa, IKE_AUTH, NO_PROPOSAL_CHOSEN, CHUNK_INITIALIZER);
425 return DELETE_ME;
426 }
427
428 /* create payload with selected propsal */
429 sa_response = sa_payload_create_from_child_proposal(proposal);
430 response->add_payload(response, (payload_t*)sa_response);
431
432 /* install child SAs for AH and esp */
433 seed = allocator_alloc_as_chunk(this->received_nonce.len + this->sent_nonce.len);
434 memcpy(seed.ptr, this->received_nonce.ptr, this->received_nonce.len);
435 memcpy(seed.ptr + this->received_nonce.len, this->sent_nonce.ptr, this->sent_nonce.len);
436 prf_plus = prf_plus_create(this->ike_sa->get_child_prf(this->ike_sa), seed);
437 allocator_free_chunk(&seed);
438
439 child_sa = child_sa_create(proposal, prf_plus);
440 prf_plus->destroy(prf_plus);
441 child_sa->destroy(child_sa);
442
443 proposal->destroy(proposal);
444 return SUCCESS;
445 }
446
447 /**
448 * Implementation of private_ike_sa_init_responded_t.build_auth_payload.
449 */
450 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)
451 {
452 authenticator_t *authenticator;
453 auth_payload_t *auth_reply;
454 status_t status;
455
456 authenticator = authenticator_create(this->ike_sa);
457
458
459 status = authenticator->verify_auth_data(authenticator,auth_request, this->ike_sa_init_request_data,this->sent_nonce,other_id_payload,TRUE);
460
461 if (status != SUCCESS)
462 {
463 this->logger->log(this->logger, AUDIT, "IKE_AUTH request verification failed. Deleting IKE_SA");
464 this->ike_sa->send_notify(this->ike_sa, IKE_AUTH, AUTHENTICATION_FAILED, CHUNK_INITIALIZER);
465 authenticator->destroy(authenticator);
466 return DELETE_ME;
467 }
468
469
470 status = authenticator->compute_auth_data(authenticator,&auth_reply, this->ike_sa_init_response_data,this->received_nonce,my_id_payload,FALSE);
471 authenticator->destroy(authenticator);
472 if (status != SUCCESS)
473 {
474 this->logger->log(this->logger, AUDIT, "Unable to build authentication data for IKE_AUTH reply. Deleting IKE_SA");
475 return DELETE_ME;
476
477 }
478
479 response->add_payload(response, (payload_t *)auth_reply);
480 return SUCCESS;
481 }
482
483 /**
484 * Implementation of private_ike_sa_init_responded_t.build_ts_payload.
485 */
486 static status_t build_ts_payload(private_ike_sa_init_responded_t *this, bool ts_initiator, ts_payload_t *request, message_t* response)
487 {
488 traffic_selector_t **ts_received, **ts_selected;
489 size_t ts_received_count, ts_selected_count;
490 status_t status = SUCCESS;
491 ts_payload_t *ts_response;
492
493 /* build a reply payload with selected traffic selectors */
494 ts_received_count = request->get_traffic_selectors(request, &ts_received);
495 /* select ts depending on payload type */
496 if (ts_initiator)
497 {
498 ts_selected_count = this->sa_config->select_traffic_selectors_initiator(this->sa_config, ts_received, ts_received_count, &ts_selected);
499 }
500 else
501 {
502 ts_selected_count = this->sa_config->select_traffic_selectors_responder(this->sa_config, ts_received, ts_received_count, &ts_selected);
503 }
504 if(ts_selected_count == 0)
505 {
506 this->logger->log(this->logger, AUDIT, "IKE_AUH request did not contain any traffic selectors.");
507 ts_response = ts_payload_create(ts_initiator);
508 response->add_payload(response, (payload_t*)ts_response);
509 }
510 else
511 {
512 ts_response = ts_payload_create_from_traffic_selectors(ts_initiator, ts_selected, ts_selected_count);
513 response->add_payload(response, (payload_t*)ts_response);
514 }
515
516 /* cleanup */
517 while(ts_received_count--)
518 {
519 traffic_selector_t *ts = *ts_received + ts_received_count;
520 ts->destroy(ts);
521 }
522 allocator_free(ts_received);
523 while(ts_selected_count--)
524 {
525 traffic_selector_t *ts = *ts_selected + ts_selected_count;
526 ts->destroy(ts);
527 }
528 allocator_free(ts_selected);
529 return status;
530 }
531
532 static status_t process_notify_payload(private_ike_sa_init_responded_t *this, notify_payload_t *notify_payload)
533 {
534 notify_message_type_t notify_message_type = notify_payload->get_notify_message_type(notify_payload);
535
536 this->logger->log(this->logger, CONTROL|LEVEL1, "Process notify type %s for protocol %s",
537 mapping_find(notify_message_type_m, notify_message_type),
538 mapping_find(protocol_id_m, notify_payload->get_protocol_id(notify_payload)));
539
540 switch (notify_message_type)
541 {
542 case SET_WINDOW_SIZE:
543 /*
544 * TODO Increase window size.
545 */
546 case INITIAL_CONTACT:
547 /*
548 * TODO Delete existing IKE_SA's with other Identity.
549 */
550 default:
551 {
552 this->logger->log(this->logger, AUDIT, "IKE_AUTH request contained an unknown notify (%d), ignored.", notify_message_type);
553 }
554 }
555
556 return SUCCESS;
557 }
558
559 /**
560 * Implementation of state_t.get_state.
561 */
562 static ike_sa_state_t get_state(private_ike_sa_init_responded_t *this)
563 {
564 return IKE_SA_INIT_RESPONDED;
565 }
566
567 /**
568 * Implementation of state_t.get_state.
569 */
570 static void destroy(private_ike_sa_init_responded_t *this)
571 {
572 this->logger->log(this->logger, CONTROL | LEVEL3, "Going to destroy ike_sa_init_responded_t state object");
573
574 this->logger->log(this->logger, CONTROL | LEVEL3, "Destroy received nonce");
575 allocator_free_chunk(&(this->received_nonce));
576 this->logger->log(this->logger, CONTROL | LEVEL3, "Destroy sent nonce");
577 allocator_free_chunk(&(this->sent_nonce));
578 this->logger->log(this->logger, CONTROL | LEVEL3, "Destroy IKE_SA_INIT response octets");
579 allocator_free_chunk(&(this->ike_sa_init_response_data));
580 this->logger->log(this->logger, CONTROL | LEVEL3, "Destroy IKE_SA_INIT request octets");
581 allocator_free_chunk(&(this->ike_sa_init_request_data));
582
583 allocator_free(this);
584 }
585
586 /*
587 * Described in header.
588 */
589 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)
590 {
591 private_ike_sa_init_responded_t *this = allocator_alloc_thing(private_ike_sa_init_responded_t);
592
593 /* interface functions */
594 this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
595 this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
596 this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
597
598 /* private functions */
599 this->build_idr_payload = build_idr_payload;
600 this->build_sa_payload = build_sa_payload;
601 this->build_auth_payload = build_auth_payload;
602 this->build_ts_payload = build_ts_payload;
603 this->process_notify_payload = process_notify_payload;
604
605 /* private data */
606 this->ike_sa = ike_sa;
607 this->received_nonce = received_nonce;
608 this->sent_nonce = sent_nonce;
609 this->ike_sa_init_response_data = ike_sa_init_response_data;
610 this->ike_sa_init_request_data = ike_sa_init_request_data;
611 this->logger = this->ike_sa->get_logger(this->ike_sa);
612
613 return &(this->public);
614 }