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 <encoding/payloads/notify_payload.h>
33 #include <transforms/diffie_hellman.h>
36 typedef struct private_responder_init_t private_responder_init_t
;
39 * Private data of a responder_init_t object.
42 struct private_responder_init_t
{
44 * Methods of the state_t interface.
46 responder_init_t
public;
51 protected_ike_sa_t
*ike_sa
;
54 * Diffie Hellman object used to compute shared secret.
56 * After processing of incoming IKE_SA_INIT-Request the shared key is
57 * passed to the next state of type ike_sa_init_responded_t.
59 diffie_hellman_t
*diffie_hellman
;
62 * Diffie Hellman group number.
64 u_int16_t dh_group_number
;
67 * Priority used to get matching dh_group number.
69 u_int16_t dh_group_priority
;
74 * This value is passed to the next state of type ike_sa_init_responded_t.
79 * Received nonce value
81 * This value is passed to the next state of type ike_sa_init_responded_t.
83 chunk_t received_nonce
;
86 * Logger used to log data
88 * Is logger of ike_sa!
93 * Selected proposal from suggested ones.
95 ike_proposal_t selected_proposal
;
98 * Builds the IKE_SA_INIT reply message
100 * @param this calling object
101 * @param message the message will be written to this location.
103 void (*build_ike_sa_init_reply
) (private_responder_init_t
*this, message_t
**message
);
106 * Builds the SA payload for this state.
108 * @param this calling object
109 * @param payload The generated SA payload object of type ke_payload_t is
110 * stored at this location.
112 void (*build_sa_payload
) (private_responder_init_t
*this, payload_t
**payload
);
115 * Builds the KE payload for this state.
117 * @param this calling object
118 * @param payload The generated KE payload object of type ke_payload_t is
119 * stored at this location.
121 void (*build_ke_payload
) (private_responder_init_t
*this, payload_t
**payload
);
124 * Builds the NONCE payload for this state.
126 * @param this calling object
127 * @param payload The generated NONCE payload object of type ke_payload_t is
128 * stored at this location.
130 void (*build_nonce_payload
) (private_responder_init_t
*this, payload_t
**payload
);
133 * Destroy function called internally of this class after state change succeeded.
135 * This destroy function does not destroy objects which were passed to the new state.
137 * @param this calling object
139 void (*destroy_after_state_change
) (private_responder_init_t
*this);
142 * Sends a IKE_SA_INIT reply with a notify payload.
144 * @param this calling object
145 * @param type type of notify message
146 * @param data data of notify message
148 void (*send_notify_reply
) (private_responder_init_t
*this,notify_message_type_t type
, chunk_t data
);
153 * Implements state_t.get_state
155 static status_t
process_message(private_responder_init_t
*this, message_t
*message
)
157 ike_sa_init_responded_t
*next_state
;
158 exchange_type_t exchange_type
;
159 host_t
*source
, *destination
;
160 init_config_t
*init_config
;
161 randomizer_t
*randomizer
;
162 chunk_t shared_secret
;
163 iterator_t
*payloads
;
170 exchange_type
= message
->get_exchange_type(message
);
171 if (exchange_type
!= IKE_SA_INIT
)
173 this->logger
->log(this->logger
, ERROR
| MORE
, "Message of type %s not supported in state responder_init",mapping_find(exchange_type_m
,exchange_type
));
176 if (!message
->get_request(message
))
178 this->logger
->log(this->logger
, ERROR
| MORE
, "Only requests of type IKE_SA_INIT supported in state responder_init");
181 /* this is the first message we process, so get host infos */
182 source
= message
->get_source(message
);
183 destination
= message
->get_destination(message
);
185 status
= charon
->configuration_manager
->get_init_config_for_host(charon
->configuration_manager
,destination
,source
,&init_config
);
186 if (status
!= SUCCESS
)
188 /* no configuration matches given host */
189 this->logger
->log(this->logger
, ERROR
| MORE
, "No INIT configuration found for given remote and local hosts");
193 this->ike_sa
->set_init_config(this->ike_sa
,init_config
);
195 /* we need to clone them, since we destroy the message later */
196 my_host
= destination
->clone(destination
);
197 other_host
= source
->clone(source
);
199 this->ike_sa
->set_my_host(this->ike_sa
, my_host
);
200 this->ike_sa
->set_other_host(this->ike_sa
, other_host
);
202 /* parse incoming message */
203 status
= message
->parse_body(message
, NULL
, NULL
);
204 if (status
!= SUCCESS
)
206 this->logger
->log(this->logger
, ERROR
| MORE
, "Could not parse body of request message");
210 /* iterate over incoming payloads. We can be sure, the message contains only accepted payloads! */
211 payloads
= message
->get_payload_iterator(message
);
213 while (payloads
->has_next(payloads
))
217 /* get current payload */
218 payloads
->current(payloads
, (void**)&payload
);
220 this->logger
->log(this->logger
, CONTROL
|MORE
, "Processing payload of type %s", mapping_find(payload_type_m
, payload
->get_type(payload
)));
221 switch (payload
->get_type(payload
))
223 case SECURITY_ASSOCIATION
:
225 sa_payload_t
*sa_payload
= (sa_payload_t
*)payload
;
226 ike_proposal_t
*ike_proposals
;
227 size_t proposal_count
;
229 /* get the list of suggested proposals */
230 status
= sa_payload
->get_ike_proposals (sa_payload
, &ike_proposals
,&proposal_count
);
231 if (status
!= SUCCESS
)
233 this->logger
->log(this->logger
, ERROR
| MORE
, "SA payload does not contain IKE proposals");
234 payloads
->destroy(payloads
);
238 status
= init_config
->select_proposal(init_config
, ike_proposals
,proposal_count
,&(this->selected_proposal
));
239 allocator_free(ike_proposals
);
240 if (status
!= SUCCESS
)
242 this->logger
->log(this->logger
, ERROR
| MORE
, "No proposal of suggested proposals selected");
243 payloads
->destroy(payloads
);
244 this->send_notify_reply(this,NO_PROPOSAL_CHOSEN
,CHUNK_INITIALIZER
);
248 this->dh_group_number
= this->selected_proposal
.diffie_hellman_group
;
250 status
= this->ike_sa
->create_transforms_from_proposal(this->ike_sa
,&(this->selected_proposal
));
251 if (status
!= SUCCESS
)
253 this->logger
->log(this->logger
, ERROR
| MORE
, "Transform objects could not be created from selected proposal");
254 payloads
->destroy(payloads
);
258 this->logger
->log(this->logger
, CONTROL
| MORE
, "SA Payload processed");
259 /* ok, we have what we need for sa_payload (proposals are stored in this->proposals)*/
264 ke_payload_t
*ke_payload
= (ke_payload_t
*)payload
;
265 diffie_hellman_group_t group
;
266 diffie_hellman_t
*dh
;
268 group
= ke_payload
->get_dh_group_number(ke_payload
);
270 if (group
== MODP_UNDEFINED
)
272 this->logger
->log(this->logger
, ERROR
| MORE
, "Diffie hellman group set to undefined!");
273 payloads
->destroy(payloads
);
276 if (this->dh_group_number
!= group
)
278 u_int16_t accepted_group
;
279 chunk_t accepted_group_chunk
;
280 /* group not same as selected one
281 * Maybe key exchange payload is before SA payload */
282 this->logger
->log(this->logger
, ERROR
| MORE
, "Diffie hellman group not as in selected proposal!");
283 payloads
->destroy(payloads
);
285 accepted_group
= htons(this->dh_group_number
);
286 accepted_group_chunk
.ptr
= (u_int8_t
*) &(accepted_group
);
287 accepted_group_chunk
.len
= 2;
288 this->send_notify_reply(this,INVALID_KE_PAYLOAD
,accepted_group_chunk
);
292 /* create diffie hellman object to handle DH exchange */
293 dh
= diffie_hellman_create(group
);
296 this->logger
->log(this->logger
, ERROR
, "Could not generate DH object with group %d",mapping_find(diffie_hellman_group_m
,group
) );
297 payloads
->destroy(payloads
);
300 this->logger
->log(this->logger
, CONTROL
| MORE
, "Set other DH public value");
302 dh
->set_other_public_value(dh
, ke_payload
->get_key_exchange_data(ke_payload
));
304 this->diffie_hellman
= dh
;
306 this->logger
->log(this->logger
, CONTROL
| MORE
, "KE Payload processed.");
311 nonce_payload_t
*nonce_payload
= (nonce_payload_t
*)payload
;
313 allocator_free(this->received_nonce
.ptr
);
314 this->received_nonce
= CHUNK_INITIALIZER
;
316 this->logger
->log(this->logger
, CONTROL
| MORE
, "Get nonce value and store it");
317 nonce_payload
->get_nonce(nonce_payload
, &(this->received_nonce
));
319 this->logger
->log(this->logger
, CONTROL
| MORE
, "Nonce Payload processed");
324 this->logger
->log(this->logger
, ERROR
| MORE
, "Payload type not supported!");
325 payloads
->destroy(payloads
);
330 /* iterator can be destroyed */
331 payloads
->destroy(payloads
);
333 this->logger
->log(this->logger
, CONTROL
| MORE
, "Request successfully handled. Going to create reply.");
335 this->logger
->log(this->logger
, CONTROL
| MOST
, "Going to create nonce.");
337 randomizer
= this->ike_sa
->get_randomizer(this->ike_sa
);
338 randomizer
->allocate_pseudo_random_bytes(randomizer
, NONCE_SIZE
, &(this->sent_nonce
));
340 /* store shared secret */
341 this->logger
->log(this->logger
, CONTROL
| MOST
, "Retrieve shared secret and store it.");
342 status
= this->diffie_hellman
->get_shared_secret(this->diffie_hellman
, &shared_secret
);
343 this->logger
->log_chunk(this->logger
, PRIVATE
, "Shared secret", &shared_secret
);
345 this->ike_sa
->compute_secrets(this->ike_sa
,shared_secret
,this->received_nonce
, this->sent_nonce
);
347 this->build_ike_sa_init_reply(this,&response
);
349 /* generate packet */
350 this->logger
->log(this->logger
, CONTROL
|MOST
, "generate packet from message");
351 status
= response
->generate(response
, NULL
, NULL
, &packet
);
352 if (status
!= SUCCESS
)
354 this->logger
->log(this->logger
, ERROR
, "could not generate packet from message");
358 this->logger
->log(this->logger
, CONTROL
|MOST
, "Add packet to global send queue");
359 charon
->send_queue
->add(charon
->send_queue
, packet
);
361 /* state can now be changed */
362 this->logger
->log(this->logger
, CONTROL
|MOST
, "Create next state object");
364 next_state
= ike_sa_init_responded_create(this->ike_sa
, shared_secret
, this->received_nonce
, this->sent_nonce
);
366 /* last message can now be set */
367 status
= this->ike_sa
->set_last_responded_message(this->ike_sa
, response
);
369 if (status
!= SUCCESS
)
371 this->logger
->log(this->logger
, ERROR
, "Could not set last responded message");
372 response
->destroy(response
);
373 (next_state
->state_interface
).destroy(&(next_state
->state_interface
));
377 /* state can now be changed */
378 this->ike_sa
->set_new_state(this->ike_sa
, (state_t
*) next_state
);
379 /* state has NOW changed :-) */
380 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
) );
382 this->logger
->log(this->logger
, CONTROL
|MOST
, "Destroy old sate object");
383 this->destroy_after_state_change(this);
390 * implements private_responder_init_t.build_ike_sa_init_reply
392 static void build_ike_sa_init_reply (private_responder_init_t
*this, message_t
**message
)
397 this->logger
->log(this->logger
, CONTROL
|MOST
, "Going to build message");
398 /* set up the reply */
399 this->ike_sa
->build_message(this->ike_sa
, IKE_SA_INIT
, FALSE
, &response
);
401 /* build SA payload */
402 this->build_sa_payload(this, &payload
);
403 this->logger
->log(this->logger
, CONTROL
|MOST
, "add SA payload to message");
404 response
->add_payload(response
, payload
);
406 /* build KE payload */
407 this->build_ke_payload(this,&payload
);
408 this->logger
->log(this->logger
, CONTROL
|MOST
, "add KE payload to message");
409 response
->add_payload(response
, payload
);
411 /* build Nonce payload */
412 this->build_nonce_payload(this, &payload
);
413 this->logger
->log(this->logger
, CONTROL
|MOST
, "add nonce payload to message");
414 response
->add_payload(response
, payload
);
420 * implements private_initiator_init_t.build_sa_payload
422 static void build_sa_payload(private_responder_init_t
*this, payload_t
**payload
)
424 sa_payload_t
* sa_payload
;
426 this->logger
->log(this->logger
, CONTROL
|MORE
, "building sa payload");
427 sa_payload
= sa_payload_create_from_ike_proposals(&(this->selected_proposal
),1);
429 *payload
= (payload_t
*) sa_payload
;
433 * implements private_initiator_init_t.build_ke_payload
435 static void build_ke_payload(private_responder_init_t
*this, payload_t
**payload
)
437 ke_payload_t
*ke_payload
;
440 this->logger
->log(this->logger
, CONTROL
|MORE
, "building ke payload");
441 this->diffie_hellman
->get_my_public_value(this->diffie_hellman
,&key_data
);
443 ke_payload
= ke_payload_create();
444 ke_payload
->set_key_exchange_data(ke_payload
,key_data
);
445 ke_payload
->set_dh_group_number(ke_payload
, this->dh_group_number
);
446 allocator_free_chunk(&key_data
);
448 *payload
= (payload_t
*) ke_payload
;
452 * implements private_initiator_init_t.build_nonce_payload
454 static void build_nonce_payload(private_responder_init_t
*this, payload_t
**payload
)
456 nonce_payload_t
*nonce_payload
;
458 this->logger
->log(this->logger
, CONTROL
|MORE
, "building nonce payload");
459 nonce_payload
= nonce_payload_create();
460 nonce_payload
->set_nonce(nonce_payload
, this->sent_nonce
);
462 *payload
= (payload_t
*) nonce_payload
;
467 * Implements state_t.get_state
469 static ike_sa_state_t
get_state(private_responder_init_t
*this)
471 return RESPONDER_INIT
;
475 * Implementation of private_initiator_init_t.send_notify_reply.
477 static void send_notify_reply (private_responder_init_t
*this,notify_message_type_t type
, chunk_t data
)
479 notify_payload_t
*payload
;
484 this->logger
->log(this->logger
, CONTROL
|MOST
, "Going to build message with notify payload");
485 /* set up the reply */
486 this->ike_sa
->build_message(this->ike_sa
, IKE_SA_INIT
, FALSE
, &response
);
487 payload
= notify_payload_create_from_protocol_and_type(IKE
,type
);
488 if ((data
.ptr
!= NULL
) && (data
.len
> 0))
490 this->logger
->log(this->logger
, CONTROL
|MOST
, "Add Data to notify payload");
491 payload
->set_notification_data(payload
,data
);
494 this->logger
->log(this->logger
, CONTROL
|MOST
, "Add Notify payload to message");
495 response
->add_payload(response
,(payload_t
*) payload
);
497 /* generate packet */
498 this->logger
->log(this->logger
, CONTROL
|MOST
, "Gnerate packet from message");
499 status
= response
->generate(response
, NULL
, NULL
, &packet
);
500 if (status
!= SUCCESS
)
502 this->logger
->log(this->logger
, ERROR
, "Could not generate packet from message");
506 this->logger
->log(this->logger
, CONTROL
|MOST
, "Add packet to global send queue");
507 charon
->send_queue
->add(charon
->send_queue
, packet
);
508 this->logger
->log(this->logger
, CONTROL
|MOST
, "Destroy message");
509 response
->destroy(response
);
513 * Implements state_t.get_state
515 static void destroy(private_responder_init_t
*this)
517 this->logger
->log(this->logger
, CONTROL
| MORE
, "Going to destroy responder init state object");
519 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy sent nonce");
520 allocator_free(this->sent_nonce
.ptr
);
521 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy received nonce");
522 allocator_free(this->received_nonce
.ptr
);
524 if (this->diffie_hellman
!= NULL
)
526 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy diffie_hellman_t hellman object");
527 this->diffie_hellman
->destroy(this->diffie_hellman
);
529 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy object");
530 allocator_free(this);
534 * Implements private_responder_init_t.destroy_after_state_change
536 static void destroy_after_state_change (private_responder_init_t
*this)
538 this->logger
->log(this->logger
, CONTROL
| MORE
, "Going to destroy responder_init_t state object");
540 /* destroy diffie hellman object */
541 if (this->diffie_hellman
!= NULL
)
543 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy diffie_hellman_t object");
544 this->diffie_hellman
->destroy(this->diffie_hellman
);
547 this->logger
->log(this->logger
, CONTROL
| MOST
, "Destroy object");
548 allocator_free(this);
552 * Described in header.
554 responder_init_t
*responder_init_create(protected_ike_sa_t
*ike_sa
)
556 private_responder_init_t
*this = allocator_alloc_thing(private_responder_init_t
);
558 /* interface functions */
559 this->public.state_interface
.process_message
= (status_t (*) (state_t
*,message_t
*)) process_message
;
560 this->public.state_interface
.get_state
= (ike_sa_state_t (*) (state_t
*)) get_state
;
561 this->public.state_interface
.destroy
= (void (*) (state_t
*)) destroy
;
563 /* private functions */
564 this->build_ike_sa_init_reply
= build_ike_sa_init_reply
;
565 this->build_sa_payload
= build_sa_payload
;
566 this->build_ke_payload
= build_ke_payload
;
567 this->build_nonce_payload
= build_nonce_payload
;
568 this->destroy_after_state_change
= destroy_after_state_change
;
569 this->send_notify_reply
= send_notify_reply
;
572 this->ike_sa
= ike_sa
;
573 this->logger
= this->ike_sa
->get_logger(this->ike_sa
);
574 this->sent_nonce
= CHUNK_INITIALIZER
;
575 this->received_nonce
= CHUNK_INITIALIZER
;
576 this->dh_group_number
= MODP_UNDEFINED
;
578 return &(this->public);