- new configuration support added to ike_sa and states
[strongswan.git] / Source / charon / sa / states / responder_init.c
1 /**
2 * @file responder_init.c
3 *
4 * @brief Implementation of responder_init_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 "responder_init.h"
24
25 #include <daemon.h>
26 #include <sa/states/state.h>
27 #include <sa/states/ike_sa_init_responded.h>
28 #include <utils/allocator.h>
29 #include <encoding/payloads/sa_payload.h>
30 #include <encoding/payloads/ke_payload.h>
31 #include <encoding/payloads/nonce_payload.h>
32 #include <transforms/diffie_hellman.h>
33
34
35 typedef struct private_responder_init_t private_responder_init_t;
36
37 /**
38 * Private data of a responder_init_t object.
39 *
40 */
41 struct private_responder_init_t {
42 /**
43 * Methods of the state_t interface.
44 */
45 responder_init_t public;
46
47 /**
48 * Assigned IKE_SA.
49 */
50 protected_ike_sa_t *ike_sa;
51
52 /**
53 * Diffie Hellman object used to compute shared secret.
54 *
55 * After processing of incoming IKE_SA_INIT-Request the shared key is
56 * passed to the next state of type ike_sa_init_responded_t.
57 */
58 diffie_hellman_t *diffie_hellman;
59
60 /**
61 * Diffie Hellman group number.
62 */
63 u_int16_t dh_group_number;
64
65 /**
66 * Priority used to get matching dh_group number.
67 */
68 u_int16_t dh_group_priority;
69
70 /**
71 * Sent nonce value.
72 *
73 * This value is passed to the next state of type ike_sa_init_responded_t.
74 */
75 chunk_t sent_nonce;
76
77 /**
78 * Received nonce value
79 *
80 * This value is passed to the next state of type ike_sa_init_responded_t.
81 */
82 chunk_t received_nonce;
83
84 /**
85 * Logger used to log data
86 *
87 * Is logger of ike_sa!
88 */
89 logger_t *logger;
90
91 /**
92 * Selected proposal from suggested ones.
93 */
94 ike_proposal_t selected_proposal;
95
96 /**
97 * Builds the SA payload for this state.
98 *
99 * @param this calling object
100 * @param payload The generated SA payload object of type ke_payload_t is
101 * stored at this location.
102 */
103 void (*build_sa_payload) (private_responder_init_t *this, payload_t **payload);
104
105 /**
106 * Builds the KE payload for this state.
107 *
108 * @param this calling object
109 * @param payload The generated KE payload object of type ke_payload_t is
110 * stored at this location.
111 */
112 void (*build_ke_payload) (private_responder_init_t *this, payload_t **payload);
113
114 /**
115 * Builds the NONCE payload for this state.
116 *
117 * @param this calling object
118 * @param payload The generated NONCE payload object of type ke_payload_t is
119 * stored at this location.
120 */
121 void (*build_nonce_payload) (private_responder_init_t *this, payload_t **payload);
122
123 /**
124 * Destroy function called internally of this class after state change succeeded.
125 *
126 * This destroy function does not destroy objects which were passed to the new state.
127 *
128 * @param this calling object
129 */
130 void (*destroy_after_state_change) (private_responder_init_t *this);
131 };
132
133 /**
134 * Implements state_t.get_state
135 */
136 static status_t process_message(private_responder_init_t *this, message_t *message)
137 {
138 iterator_t *payloads;
139 host_t *source, *destination;
140 status_t status;
141 message_t *response;
142 payload_t *payload;
143 packet_t *packet;
144 chunk_t shared_secret;
145 exchange_type_t exchange_type;
146 ike_sa_init_responded_t *next_state;
147 host_t *my_host;
148 host_t *other_host;
149 randomizer_t *randomizer;
150 init_config_t *init_config;
151 diffie_hellman_group_t dh_group = MODP_UNDEFINED;
152
153 exchange_type = message->get_exchange_type(message);
154 if (exchange_type != IKE_SA_INIT)
155 {
156 this->logger->log(this->logger, ERROR | MORE, "Message of type %s not supported in state responder_init",mapping_find(exchange_type_m,exchange_type));
157 return FAILED;
158 }
159 if (!message->get_request(message))
160 {
161 this->logger->log(this->logger, ERROR | MORE, "Only requests of type IKE_SA_INIT supported in state responder_init");
162 return FAILED;
163 }
164 /* this is the first message we process, so copy host infos */
165 source = message->get_source(message);
166 destination = message->get_destination(message);
167
168 status = charon->configuration_manager->get_init_config_for_host(charon->configuration_manager,destination,source,&init_config);
169
170 if (status != SUCCESS)
171 {
172 /* no configuration matches given host */
173 this->logger->log(this->logger, ERROR | MORE, "No INIT configuration found for given remote and local hosts");
174 return FAILED;
175 }
176
177 this->ike_sa->set_init_config(this->ike_sa,init_config);
178
179
180 /* we need to clone them, since we destroy the message later */
181 my_host = destination->clone(destination);
182 other_host = source->clone(source);
183
184 this->ike_sa->set_my_host(this->ike_sa, my_host);
185 this->ike_sa->set_other_host(this->ike_sa, other_host);
186
187 /* parse incoming message */
188 status = message->parse_body(message, NULL, NULL);
189 if (status != SUCCESS)
190 {
191 this->logger->log(this->logger, ERROR | MORE, "Could not parse body of request message");
192 return status;
193 }
194 /* iterate over incoming payloads. We can be sure, the message contains only accepted payloads! */
195 payloads = message->get_payload_iterator(message);
196
197 while (payloads->has_next(payloads))
198 {
199 payload_t *payload;
200
201 /* get current payload */
202 payloads->current(payloads, (void**)&payload);
203
204 this->logger->log(this->logger, CONTROL|MORE, "Processing payload of type %s", mapping_find(payload_type_m, payload->get_type(payload)));
205 switch (payload->get_type(payload))
206 {
207 case SECURITY_ASSOCIATION:
208 {
209 sa_payload_t *sa_payload = (sa_payload_t*)payload;
210 ike_proposal_t *ike_proposals;
211 size_t proposal_count;
212
213 /* get the list of suggested proposals */
214 status = sa_payload->get_ike_proposals (sa_payload, &ike_proposals,&proposal_count);
215 if (status != SUCCESS)
216 {
217 this->logger->log(this->logger, ERROR | MORE, "SA payload does not contain IKE proposals");
218 payloads->destroy(payloads);
219 return status;
220 }
221
222 status = init_config->select_proposal(init_config, ike_proposals,proposal_count,&(this->selected_proposal));
223 allocator_free(ike_proposals);
224 if (status != SUCCESS)
225 {
226 this->logger->log(this->logger, ERROR | MORE, "No proposal of suggested proposals selected");
227 payloads->destroy(payloads);
228 return status;
229 }
230
231 dh_group = this->selected_proposal.diffie_hellman_group;
232
233 status = this->ike_sa->create_transforms_from_proposal(this->ike_sa,&(this->selected_proposal));
234 if (status != SUCCESS)
235 {
236 this->logger->log(this->logger, ERROR | MORE, "Transform objects could not be created from selected proposal");
237 payloads->destroy(payloads);
238 return status;
239 }
240
241 this->logger->log(this->logger, CONTROL | MORE, "SA Payload processed");
242 /* ok, we have what we need for sa_payload (proposals are stored in this->proposals)*/
243 break;
244 }
245 case KEY_EXCHANGE:
246 {
247 ke_payload_t *ke_payload = (ke_payload_t*)payload;
248 diffie_hellman_group_t group;
249 diffie_hellman_t *dh;
250 bool allowed_group;
251
252 group = ke_payload->get_dh_group_number(ke_payload);
253
254 if (dh_group == MODP_UNDEFINED)
255 {
256 this->logger->log(this->logger, ERROR | MORE, "Could not get informations about DH group. SA payload before KE payload?");
257 payloads->destroy(payloads);
258 return status;
259 }
260 if (!allowed_group)
261 {
262 /** @todo Send info reply */
263 }
264
265 /* create diffie hellman object to handle DH exchange */
266 dh = diffie_hellman_create(group);
267 if (dh == NULL)
268 {
269 this->logger->log(this->logger, ERROR, "Could not generate DH object");
270 payloads->destroy(payloads);
271 return NOT_SUPPORTED;
272 }
273 this->logger->log(this->logger, CONTROL | MORE, "Set other DH public value");
274
275 dh->set_other_public_value(dh, ke_payload->get_key_exchange_data(ke_payload));
276
277 this->diffie_hellman = dh;
278
279 this->logger->log(this->logger, CONTROL | MORE, "KE Payload processed");
280 break;
281 }
282 case NONCE:
283 {
284 nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
285
286 allocator_free(this->received_nonce.ptr);
287 this->received_nonce = CHUNK_INITIALIZER;
288
289 this->logger->log(this->logger, CONTROL | MORE, "Get nonce value and store it");
290 nonce_payload->get_nonce(nonce_payload, &(this->received_nonce));
291
292 this->logger->log(this->logger, CONTROL | MORE, "Nonce Payload processed");
293 break;
294 }
295 default:
296 {
297 this->logger->log(this->logger, ERROR | MORE, "Payload type not supported!");
298 payloads->destroy(payloads);
299 return NOT_SUPPORTED;
300 }
301 }
302 }
303 /* iterator can be destroyed */
304 payloads->destroy(payloads);
305
306 this->logger->log(this->logger, CONTROL | MORE, "Request successfully handled. Going to create reply.");
307
308 this->logger->log(this->logger, CONTROL | MOST, "Going to create nonce.");
309
310 randomizer = this->ike_sa->get_randomizer(this->ike_sa);
311
312 randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE, &(this->sent_nonce));
313
314 /* store shared secret */
315 this->logger->log(this->logger, CONTROL | MOST, "Retrieve shared secret and store it");
316 status = this->diffie_hellman->get_shared_secret(this->diffie_hellman, &shared_secret);
317 this->logger->log_chunk(this->logger, PRIVATE, "Shared secret", &shared_secret);
318
319 this->ike_sa->compute_secrets(this->ike_sa,shared_secret,this->received_nonce, this->sent_nonce);
320
321 /* set up the reply */
322 this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, FALSE, &response);
323
324
325 /* build SA payload */
326 this->build_sa_payload(this, &payload);
327 this->logger->log(this->logger, CONTROL|MOST, "add SA payload to message");
328 response->add_payload(response, payload);
329
330 /* build KE payload */
331 this->build_ke_payload(this,&payload);
332 this->logger->log(this->logger, CONTROL|MOST, "add KE payload to message");
333 response->add_payload(response, payload);
334
335 /* build Nonce payload */
336 this->build_nonce_payload(this, &payload);
337 this->logger->log(this->logger, CONTROL|MOST, "add nonce payload to message");
338 response->add_payload(response, payload);
339
340 /* generate packet */
341 this->logger->log(this->logger, CONTROL|MOST, "generate packet from message");
342 status = response->generate(response, NULL, NULL, &packet);
343 if (status != SUCCESS)
344 {
345 this->logger->log(this->logger, ERROR, "could not generate packet from message");
346 return status;
347 }
348
349 this->logger->log(this->logger, CONTROL|MOST, "Add packet to global send queue");
350 charon->send_queue->add(charon->send_queue, packet);
351
352 /* state can now be changed */
353 this->logger->log(this->logger, CONTROL|MOST, "Create next state object");
354
355 next_state = ike_sa_init_responded_create(this->ike_sa, shared_secret, this->received_nonce, this->sent_nonce);
356
357 /* last message can now be set */
358 status = this->ike_sa->set_last_responded_message(this->ike_sa, response);
359
360 if (status != SUCCESS)
361 {
362 this->logger->log(this->logger, ERROR, "Could not set last responded message");
363 response->destroy(response);
364 (next_state->state_interface).destroy(&(next_state->state_interface));
365 return status;
366 }
367
368 /* state can now be changed */
369 this->ike_sa->set_new_state(this->ike_sa, (state_t *) next_state);
370 /* state has NOW changed :-) */
371 this->logger->log(this->logger, CONTROL|MORE, "Changed state of IKE_SA from %s to %s",mapping_find(ike_sa_state_m,RESPONDER_INIT),mapping_find(ike_sa_state_m,IKE_SA_INIT_RESPONDED) );
372
373 this->logger->log(this->logger, CONTROL|MOST, "Destroy old sate object");
374 this->destroy_after_state_change(this);
375
376 return SUCCESS;
377 }
378
379 /**
380 * implements private_initiator_init_t.build_sa_payload
381 */
382 static void build_sa_payload(private_responder_init_t *this, payload_t **payload)
383 {
384 sa_payload_t* sa_payload;
385
386 /* SA payload takes proposals from this->ike_sa_init_data.proposals and writes them to the created sa_payload */
387
388 this->logger->log(this->logger, CONTROL|MORE, "building sa payload");
389
390 sa_payload = sa_payload_create_from_ike_proposals(&(this->selected_proposal),1);
391
392 *payload = (payload_t *) sa_payload;
393 }
394
395 /**
396 * implements private_initiator_init_t.build_ke_payload
397 */
398 static void build_ke_payload(private_responder_init_t *this, payload_t **payload)
399 {
400 ke_payload_t *ke_payload;
401 chunk_t key_data;
402
403 this->logger->log(this->logger, CONTROL|MORE, "building ke payload");
404 this->diffie_hellman->get_my_public_value(this->diffie_hellman,&key_data);
405
406 ke_payload = ke_payload_create();
407 ke_payload->set_key_exchange_data(ke_payload,key_data);
408 ke_payload->set_dh_group_number(ke_payload, MODP_1024_BIT);
409
410 allocator_free_chunk(&key_data);
411 *payload = (payload_t *) ke_payload;
412 }
413
414 /**
415 * implements private_initiator_init_t.build_nonce_payload
416 */
417 static void build_nonce_payload(private_responder_init_t *this, payload_t **payload)
418 {
419 nonce_payload_t *nonce_payload;
420 status_t status;
421
422 this->logger->log(this->logger, CONTROL|MORE, "building nonce payload");
423
424 nonce_payload = nonce_payload_create();
425
426 status = nonce_payload->set_nonce(nonce_payload, this->sent_nonce);
427
428 *payload = (payload_t *) nonce_payload;
429 }
430
431
432 /**
433 * Implements state_t.get_state
434 */
435 static ike_sa_state_t get_state(private_responder_init_t *this)
436 {
437 return RESPONDER_INIT;
438 }
439
440 /**
441 * Implements state_t.get_state
442 */
443 static void destroy(private_responder_init_t *this)
444 {
445 this->logger->log(this->logger, CONTROL | MORE, "Going to destroy responder init state object");
446
447 /* destroy stored proposal */
448 this->logger->log(this->logger, CONTROL | MOST, "Destroy stored proposals");
449
450 allocator_free(this->sent_nonce.ptr);
451 allocator_free(this->received_nonce.ptr);
452
453 if (this->diffie_hellman != NULL)
454 {
455 this->diffie_hellman->destroy(this->diffie_hellman);
456 }
457 allocator_free(this);
458 }
459
460 /**
461 * Implements private_responder_init_t.destroy_after_state_change
462 */
463 static void destroy_after_state_change (private_responder_init_t *this)
464 {
465 this->logger->log(this->logger, CONTROL | MORE, "Going to destroy responder_init_t state object");
466
467 /* destroy stored proposal */
468 this->logger->log(this->logger, CONTROL | MOST, "Destroy stored proposals");
469
470 /* destroy diffie hellman object */
471 if (this->diffie_hellman != NULL)
472 {
473 this->logger->log(this->logger, CONTROL | MOST, "Destroy diffie_hellman_t object");
474 this->diffie_hellman->destroy(this->diffie_hellman);
475 }
476
477 allocator_free(this);
478 }
479
480 /*
481 * Described in header.
482 */
483 responder_init_t *responder_init_create(protected_ike_sa_t *ike_sa)
484 {
485 private_responder_init_t *this = allocator_alloc_thing(private_responder_init_t);
486
487 /* interface functions */
488 this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
489 this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
490 this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
491
492 /* private functions */
493 this->build_sa_payload = build_sa_payload;
494 this->build_ke_payload = build_ke_payload;
495 this->build_nonce_payload = build_nonce_payload;
496 this->destroy_after_state_change = destroy_after_state_change;
497
498 /* private data */
499 this->ike_sa = ike_sa;
500 this->logger = this->ike_sa->get_logger(this->ike_sa);
501 this->sent_nonce = CHUNK_INITIALIZER;
502 this->received_nonce = CHUNK_INITIALIZER;
503
504 return &(this->public);
505 }