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