2 * @file responder_init.c
4 * @brief Implementation of responder_init_t.
9 * Copyright (C) 2005 Jan Hutter, Martin Willi
10 * Hochschule fuer Technik Rapperswil
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>.
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
23 #include "responder_init.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>
35 typedef struct private_responder_init_t private_responder_init_t
;
38 * Private data of a responder_init_t object.
41 struct private_responder_init_t
{
43 * Methods of the state_t interface.
45 responder_init_t
public;
50 protected_ike_sa_t
*ike_sa
;
53 * Diffie Hellman object used to compute shared secret.
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.
58 diffie_hellman_t
*diffie_hellman
;
61 * Diffie Hellman group number.
63 u_int16_t dh_group_number
;
66 * Priority used to get matching dh_group number.
68 u_int16_t dh_group_priority
;
73 * This value is passed to the next state of type ike_sa_init_responded_t.
78 * Received nonce value
80 * This value is passed to the next state of type ike_sa_init_responded_t.
82 chunk_t received_nonce
;
85 * Logger used to log data
87 * Is logger of ike_sa!
92 * Selected proposal from suggested ones.
94 ike_proposal_t selected_proposal
;
97 * Builds the SA payload for this state.
99 * @param this calling object
100 * @param payload The generated SA payload object of type ke_payload_t is
101 * stored at this location.
103 void (*build_sa_payload
) (private_responder_init_t
*this, payload_t
**payload
);
106 * Builds the KE payload for this state.
108 * @param this calling object
109 * @param payload The generated KE payload object of type ke_payload_t is
110 * stored at this location.
112 void (*build_ke_payload
) (private_responder_init_t
*this, payload_t
**payload
);
115 * Builds the NONCE payload for this state.
117 * @param this calling object
118 * @param payload The generated NONCE payload object of type ke_payload_t is
119 * stored at this location.
121 void (*build_nonce_payload
) (private_responder_init_t
*this, payload_t
**payload
);
124 * Destroy function called internally of this class after state change succeeded.
126 * This destroy function does not destroy objects which were passed to the new state.
128 * @param this calling object
130 void (*destroy_after_state_change
) (private_responder_init_t
*this);
134 * Implements state_t.get_state
136 static status_t
process_message(private_responder_init_t
*this, message_t
*message
)
138 iterator_t
*payloads
;
139 host_t
*source
, *destination
;
144 chunk_t shared_secret
;
145 exchange_type_t exchange_type
;
146 ike_sa_init_responded_t
*next_state
;
149 randomizer_t
*randomizer
;
150 init_config_t
*init_config
;
151 diffie_hellman_group_t dh_group
= MODP_UNDEFINED
;
153 exchange_type
= message
->get_exchange_type(message
);
154 if (exchange_type
!= IKE_SA_INIT
)
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
));
159 if (!message
->get_request(message
))
161 this->logger
->log(this->logger
, ERROR
| MORE
, "Only requests of type IKE_SA_INIT supported in state responder_init");
164 /* this is the first message we process, so copy host infos */
165 source
= message
->get_source(message
);
166 destination
= message
->get_destination(message
);
168 status
= charon
->configuration_manager
->get_init_config_for_host(charon
->configuration_manager
,destination
,source
,&init_config
);
170 if (status
!= SUCCESS
)
172 /* no configuration matches given host */
173 this->logger
->log(this->logger
, ERROR
| MORE
, "No INIT configuration found for given remote and local hosts");
177 this->ike_sa
->set_init_config(this->ike_sa
,init_config
);
180 /* we need to clone them, since we destroy the message later */
181 my_host
= destination
->clone(destination
);
182 other_host
= source
->clone(source
);
184 this->ike_sa
->set_my_host(this->ike_sa
, my_host
);
185 this->ike_sa
->set_other_host(this->ike_sa
, other_host
);
187 /* parse incoming message */
188 status
= message
->parse_body(message
, NULL
, NULL
);
189 if (status
!= SUCCESS
)
191 this->logger
->log(this->logger
, ERROR
| MORE
, "Could not parse body of request message");
194 /* iterate over incoming payloads. We can be sure, the message contains only accepted payloads! */
195 payloads
= message
->get_payload_iterator(message
);
197 while (payloads
->has_next(payloads
))
201 /* get current payload */
202 payloads
->current(payloads
, (void**)&payload
);
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
))
207 case SECURITY_ASSOCIATION
:
209 sa_payload_t
*sa_payload
= (sa_payload_t
*)payload
;
210 ike_proposal_t
*ike_proposals
;
211 size_t proposal_count
;
213 /* get the list of suggested proposals */
214 status
= sa_payload
->get_ike_proposals (sa_payload
, &ike_proposals
,&proposal_count
);
215 if (status
!= SUCCESS
)
217 this->logger
->log(this->logger
, ERROR
| MORE
, "SA payload does not contain IKE proposals");
218 payloads
->destroy(payloads
);
222 status
= init_config
->select_proposal(init_config
, ike_proposals
,proposal_count
,&(this->selected_proposal
));
223 allocator_free(ike_proposals
);
224 if (status
!= SUCCESS
)
226 this->logger
->log(this->logger
, ERROR
| MORE
, "No proposal of suggested proposals selected");
227 payloads
->destroy(payloads
);
231 dh_group
= this->selected_proposal
.diffie_hellman_group
;
233 status
= this->ike_sa
->create_transforms_from_proposal(this->ike_sa
,&(this->selected_proposal
));
234 if (status
!= SUCCESS
)
236 this->logger
->log(this->logger
, ERROR
| MORE
, "Transform objects could not be created from selected proposal");
237 payloads
->destroy(payloads
);
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)*/
247 ke_payload_t
*ke_payload
= (ke_payload_t
*)payload
;
248 diffie_hellman_group_t group
;
249 diffie_hellman_t
*dh
;
252 group
= ke_payload
->get_dh_group_number(ke_payload
);
254 if (dh_group
== MODP_UNDEFINED
)
256 this->logger
->log(this->logger
, ERROR
| MORE
, "Could not get informations about DH group. SA payload before KE payload?");
257 payloads
->destroy(payloads
);
262 /** @todo Send info reply */
265 /* create diffie hellman object to handle DH exchange */
266 dh
= diffie_hellman_create(group
);
269 this->logger
->log(this->logger
, ERROR
, "Could not generate DH object");
270 payloads
->destroy(payloads
);
271 return NOT_SUPPORTED
;
273 this->logger
->log(this->logger
, CONTROL
| MORE
, "Set other DH public value");
275 dh
->set_other_public_value(dh
, ke_payload
->get_key_exchange_data(ke_payload
));
277 this->diffie_hellman
= dh
;
279 this->logger
->log(this->logger
, CONTROL
| MORE
, "KE Payload processed");
284 nonce_payload_t
*nonce_payload
= (nonce_payload_t
*)payload
;
286 allocator_free(this->received_nonce
.ptr
);
287 this->received_nonce
= CHUNK_INITIALIZER
;
289 this->logger
->log(this->logger
, CONTROL
| MORE
, "Get nonce value and store it");
290 nonce_payload
->get_nonce(nonce_payload
, &(this->received_nonce
));
292 this->logger
->log(this->logger
, CONTROL
| MORE
, "Nonce Payload processed");
297 this->logger
->log(this->logger
, ERROR
| MORE
, "Payload type not supported!");
298 payloads
->destroy(payloads
);
299 return NOT_SUPPORTED
;
303 /* iterator can be destroyed */
304 payloads
->destroy(payloads
);
306 this->logger
->log(this->logger
, CONTROL
| MORE
, "Request successfully handled. Going to create reply.");
308 this->logger
->log(this->logger
, CONTROL
| MOST
, "Going to create nonce.");
310 randomizer
= this->ike_sa
->get_randomizer(this->ike_sa
);
312 randomizer
->allocate_pseudo_random_bytes(randomizer
, NONCE_SIZE
, &(this->sent_nonce
));
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
);
319 this->ike_sa
->compute_secrets(this->ike_sa
,shared_secret
,this->received_nonce
, this->sent_nonce
);
321 /* set up the reply */
322 this->ike_sa
->build_message(this->ike_sa
, IKE_SA_INIT
, FALSE
, &response
);
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
);
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
);
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
);
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
)
345 this->logger
->log(this->logger
, ERROR
, "could not generate packet from message");
349 this->logger
->log(this->logger
, CONTROL
|MOST
, "Add packet to global send queue");
350 charon
->send_queue
->add(charon
->send_queue
, packet
);
352 /* state can now be changed */
353 this->logger
->log(this->logger
, CONTROL
|MOST
, "Create next state object");
355 next_state
= ike_sa_init_responded_create(this->ike_sa
, shared_secret
, this->received_nonce
, this->sent_nonce
);
357 /* last message can now be set */
358 status
= this->ike_sa
->set_last_responded_message(this->ike_sa
, response
);
360 if (status
!= SUCCESS
)
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
));
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
) );
373 this->logger
->log(this->logger
, CONTROL
|MOST
, "Destroy old sate object");
374 this->destroy_after_state_change(this);
380 * implements private_initiator_init_t.build_sa_payload
382 static void build_sa_payload(private_responder_init_t
*this, payload_t
**payload
)
384 sa_payload_t
* sa_payload
;
386 /* SA payload takes proposals from this->ike_sa_init_data.proposals and writes them to the created sa_payload */
388 this->logger
->log(this->logger
, CONTROL
|MORE
, "building sa payload");
390 sa_payload
= sa_payload_create_from_ike_proposals(&(this->selected_proposal
),1);
392 *payload
= (payload_t
*) sa_payload
;
396 * implements private_initiator_init_t.build_ke_payload
398 static void build_ke_payload(private_responder_init_t
*this, payload_t
**payload
)
400 ke_payload_t
*ke_payload
;
403 this->logger
->log(this->logger
, CONTROL
|MORE
, "building ke payload");
404 this->diffie_hellman
->get_my_public_value(this->diffie_hellman
,&key_data
);
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
);
410 allocator_free_chunk(&key_data
);
411 *payload
= (payload_t
*) ke_payload
;
415 * implements private_initiator_init_t.build_nonce_payload
417 static void build_nonce_payload(private_responder_init_t
*this, payload_t
**payload
)
419 nonce_payload_t
*nonce_payload
;
422 this->logger
->log(this->logger
, CONTROL
|MORE
, "building nonce payload");
424 nonce_payload
= nonce_payload_create();
426 status
= nonce_payload
->set_nonce(nonce_payload
, this->sent_nonce
);
428 *payload
= (payload_t
*) nonce_payload
;
433 * Implements state_t.get_state
435 static ike_sa_state_t
get_state(private_responder_init_t
*this)
437 return RESPONDER_INIT
;
441 * Implements state_t.get_state
443 static void destroy(private_responder_init_t
*this)
445 this->logger
->log(this->logger
, CONTROL
| MORE
, "Going to destroy responder init state object");
447 /* destroy stored proposal */
448 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy stored proposals");
450 allocator_free(this->sent_nonce
.ptr
);
451 allocator_free(this->received_nonce
.ptr
);
453 if (this->diffie_hellman
!= NULL
)
455 this->diffie_hellman
->destroy(this->diffie_hellman
);
457 allocator_free(this);
461 * Implements private_responder_init_t.destroy_after_state_change
463 static void destroy_after_state_change (private_responder_init_t
*this)
465 this->logger
->log(this->logger
, CONTROL
| MORE
, "Going to destroy responder_init_t state object");
467 /* destroy stored proposal */
468 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy stored proposals");
470 /* destroy diffie hellman object */
471 if (this->diffie_hellman
!= NULL
)
473 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy diffie_hellman_t object");
474 this->diffie_hellman
->destroy(this->diffie_hellman
);
477 allocator_free(this);
481 * Described in header.
483 responder_init_t
*responder_init_create(protected_ike_sa_t
*ike_sa
)
485 private_responder_init_t
*this = allocator_alloc_thing(private_responder_init_t
);
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
;
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
;
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
;
504 return &(this->public);