2 * @file configuration.c
4 * @brief Configuration class used to store IKE_SA-configurations.
6 * Object of this type represents the configuration for all IKE_SA's and their child_sa's.
11 * Copyright (C) 2005 Jan Hutter, Martin Willi
12 * Hochschule fuer Technik Rapperswil
14 * This program is free software; you can redistribute it and/or modify it
15 * under the terms of the GNU General Public License as published by the
16 * Free Software Foundation; either version 2 of the License, or (at your
17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
27 #include "configuration_manager.h"
31 #include <utils/allocator.h>
32 #include <encoding/payloads/nonce_payload.h>
33 #include <encoding/payloads/proposal_substructure.h>
34 #include <encoding/payloads/ke_payload.h>
35 #include <encoding/payloads/transform_attribute.h>
37 typedef struct private_configuration_manager_t private_configuration_manager_t
;
40 * Private data of an configuration_t object
42 struct private_configuration_manager_t
{
47 configuration_manager_t
public;
50 * Assigned logger object
56 * Implements function configuration_manager_t.get_remote_host.
58 static status_t
get_remote_host(private_configuration_manager_t
*this, char *name
, host_t
**host
)
61 * For testing purposes, hard coded host informations for two configurations are returned.
63 * Further improvements could store them in a linked list or hash table.
67 status_t status
= SUCCESS
;
69 if (strcmp(name
, "pinflb30") == 0)
71 remote
= host_create(AF_INET
, "152.96.193.130", 500);
73 else if (strcmp(name
, "pinflb31") == 0)
75 remote
= host_create(AF_INET
, "152.96.193.131", 500);
77 else if (strcmp(name
, "localhost") == 0)
79 remote
= host_create(AF_INET
, "127.0.0.1", 500);
85 if ((status
!= NOT_FOUND
) && (remote
== NULL
))
95 * Implements function configuration_manager_t.get_local_host.
97 static status_t
get_local_host(private_configuration_manager_t
*this, char *name
, host_t
**host
)
100 * For testing purposes, only the default route is returned for each configuration.
102 * Further improvements could store different local host informations in a linked list or hash table.
105 local
= host_create(AF_INET
, "0.0.0.0", 0);
115 * Implements function configuration_manager_t.get_dh_group_number.
117 static status_t
get_dh_group_number(private_configuration_manager_t
*this,char *name
, u_int16_t
*dh_group_number
, u_int16_t priority
)
119 /* Currently only two dh_group_numbers are supported for each configuration*/
123 *dh_group_number
= MODP_1024_BIT
;
127 *dh_group_number
= MODP_768_BIT
;
133 * Implements function configuration_manager_t.get_proposals_for_host.
135 static status_t
get_proposals_for_host(private_configuration_manager_t
*this, host_t
*host
, iterator_t
*iterator
)
138 * Currently the following hard coded proposal is created and returned for all hosts:
139 * - ENCR_AES_CBC 128Bit
140 * - PRF_HMAC_MD5 128Bit
141 * - AUTH_HMAC_MD5_96 128Bit
144 proposal_substructure_t
*proposal
;
145 transform_substructure_t
*transform
;
146 transform_attribute_t
*attribute
;
149 proposal
= proposal_substructure_create();
150 if (proposal
== NULL
)
155 proposal
->set_proposal_number(proposal
, 1);
156 proposal
->set_protocol_id(proposal
, 1);
159 * Encryption Algorithm
161 transform
= transform_substructure_create();
162 if (transform
== NULL
)
164 proposal
->destroy(proposal
);
167 status
= proposal
->add_transform_substructure(proposal
, transform
);
168 if (status
!= SUCCESS
)
170 proposal
->destroy(proposal
);
173 transform
->set_transform_type(transform
, ENCRYPTION_ALGORITHM
);
174 transform
->set_transform_id(transform
, ENCR_AES_CBC
);
176 attribute
= transform_attribute_create();
177 if (attribute
== NULL
)
179 proposal
->destroy(proposal
);
182 status
= transform
->add_transform_attribute(transform
, attribute
);
183 if (status
!= SUCCESS
)
185 proposal
->destroy(proposal
);
188 attribute
->set_attribute_type(attribute
, KEY_LENGTH
);
189 attribute
->set_value(attribute
, 16);
192 * Pseudo-random Function
194 transform
= transform_substructure_create();
195 if (transform
== NULL
)
197 proposal
->destroy(proposal
);
200 status
= proposal
->add_transform_substructure(proposal
, transform
);
201 if (status
!= SUCCESS
)
203 proposal
->destroy(proposal
);
206 transform
->set_transform_type(transform
, PSEUDO_RANDOM_FUNCTION
);
207 transform
->set_transform_id(transform
, PRF_HMAC_MD5
);
209 attribute
= transform_attribute_create();
210 if (attribute
== NULL
)
212 proposal
->destroy(proposal
);
215 status
= transform
->add_transform_attribute(transform
, attribute
);
216 if (status
!= SUCCESS
)
218 proposal
->destroy(proposal
);
221 attribute
->set_attribute_type(attribute
, KEY_LENGTH
);
222 attribute
->set_value(attribute
, 16);
226 * Integrity Algorithm
228 transform
= transform_substructure_create();
229 if (transform
== NULL
)
231 proposal
->destroy(proposal
);
234 status
= proposal
->add_transform_substructure(proposal
, transform
);
235 if (status
!= SUCCESS
)
237 proposal
->destroy(proposal
);
240 transform
->set_transform_type(transform
, INTEGRITIY_ALGORITHM
);
241 transform
->set_transform_id(transform
, AUTH_HMAC_MD5_96
);
243 attribute
= transform_attribute_create();
244 if (attribute
== NULL
)
246 proposal
->destroy(proposal
);
249 status
= transform
->add_transform_attribute(transform
, attribute
);
250 if (status
!= SUCCESS
)
252 proposal
->destroy(proposal
);
255 attribute
->set_attribute_type(attribute
, KEY_LENGTH
);
256 attribute
->set_value(attribute
, 16);
260 * Diffie-Hellman Group
262 transform
= transform_substructure_create();
263 if (transform
== NULL
)
265 proposal
->destroy(proposal
);
268 status
= proposal
->add_transform_substructure(proposal
, transform
);
269 if (status
!= SUCCESS
)
271 proposal
->destroy(proposal
);
274 transform
->set_transform_type(transform
, DIFFIE_HELLMAN_GROUP
);
275 transform
->set_transform_id(transform
, MODP_1024_BIT
);
277 iterator
->insert_after(iterator
, (void*)proposal
);
283 * Implements function configuration_manager_t.select_proposals_for_host.
285 static status_t
select_proposals_for_host(private_configuration_manager_t
*this, host_t
*host
, iterator_t
*in
, iterator_t
*out
)
287 /* Currently the first suggested proposal is selected, cloned and then returned*/
289 proposal_substructure_t
*first_suggested_proposal
;
290 proposal_substructure_t
*selected_proposal
;
292 this->logger
->log(this->logger
,CONTROL
| MORE
, "Going to select first suggested proposal");
293 if (!in
->has_next(in
))
295 this->logger
->log(this->logger
,ERROR
| MORE
, "No proposal suggested");
296 /* no suggested proposal! */
300 status
= in
->current(in
,(void **) &first_suggested_proposal
);
301 if (status
!= SUCCESS
)
303 this->logger
->log(this->logger
,ERROR
, "Fatal error: could not get first proposal from iterator");
306 status
= first_suggested_proposal
->clone(first_suggested_proposal
,&selected_proposal
);
307 if (status
!= SUCCESS
)
309 this->logger
->log(this->logger
,ERROR
, "Fatal error: could not clone proposal");
310 /* could not clone proposal */
314 status
= out
->insert_after(out
,selected_proposal
);
315 if (status
!= SUCCESS
)
317 this->logger
->log(this->logger
,ERROR
, "Fatal error: could not insert selected proposal in out iterator");
323 * Implements function configuration_manager_t.get_transforms_for_host_and_proposals.
325 static status_t
get_transforms_for_host_and_proposals (private_configuration_manager_t
*this, host_t
*host
, iterator_t
*proposals
,encryption_algorithm_t
*encryption_algorithm
,pseudo_random_function_t
*pseudo_random_function
, integrity_algorithm_t
*integrity_algorithm
)
328 * Currently the given proposals are not checked if they are valid for specific host!
330 * The first proposal is taken and the appropriate transform objects are created (only if they are supported)
333 encryption_algorithm_t selected_encryption_algorithm
= ENCR_UNDEFINED
;
334 pseudo_random_function_t selected_pseudo_random_function
= PRF_UNDEFINED
;
335 integrity_algorithm_t selected_integrity_algorithm
= AUTH_UNDEFINED
;
336 proposal_substructure_t
*proposal
;
337 iterator_t
*transforms
;
340 this->logger
->log(this->logger
,ERROR
, "Going to get transforms for given proposal");
342 if (!proposals
->has_next(proposals
))
344 this->logger
->log(this->logger
,ERROR
| MORE
, "No proposal available");
348 status
= proposals
->current(proposals
,(void **) &(proposal
));
349 if (status
!= SUCCESS
)
351 this->logger
->log(this->logger
,ERROR
, "Fatal error: could not get first proposal from iterator");
355 status
= proposal
->create_transform_substructure_iterator(proposal
,&transforms
,TRUE
);
356 if (status
!= SUCCESS
)
358 this->logger
->log(this->logger
,ERROR
, "Fatal error: could not create iterator of transforms");
362 while (transforms
->has_next(transforms
))
364 transform_substructure_t
*current_transform
;
365 transform_type_t transform_type
;
366 u_int16_t transform_id
;
368 status
= transforms
->current(transforms
,(void **) &(current_transform
));
369 if (status
!= SUCCESS
)
371 this->logger
->log(this->logger
,ERROR
, "Fatal error: could not get current transform substructure object");
372 transforms
->destroy(transforms
);
376 transform_type
= current_transform
->get_transform_type(current_transform
);
377 transform_id
= current_transform
->get_transform_id(current_transform
);
379 this->logger
->log(this->logger
,CONTROL
| MOST
, "Going to process transform of type %s",mapping_find(transform_type_m
,transform_type
));
380 switch (transform_type
)
382 case ENCRYPTION_ALGORITHM
:
384 this->logger
->log(this->logger
,CONTROL
| MORE
, "Encryption algorithm: %s",mapping_find(encryption_algorithm_m
,transform_id
));
385 selected_encryption_algorithm
= transform_id
;
388 case PSEUDO_RANDOM_FUNCTION
:
390 this->logger
->log(this->logger
,CONTROL
| MORE
, "Create transform object for PRF of type %s",mapping_find(pseudo_random_function_m
,transform_id
));
391 selected_pseudo_random_function
= transform_id
;
394 case INTEGRITIY_ALGORITHM
:
396 this->logger
->log(this->logger
,CONTROL
| MORE
, "Integrity algorithm: %s",mapping_find(integrity_algorithm_m
,transform_id
));
397 selected_integrity_algorithm
= transform_id
;
400 case DIFFIE_HELLMAN_GROUP
:
402 this->logger
->log(this->logger
,CONTROL
| MORE
, "DH Group: %s",mapping_find(diffie_hellman_group_m
,transform_id
));
407 this->logger
->log(this->logger
,ERROR
| MORE
, "Transform type not supported!");
408 transforms
->destroy(transforms
);
414 transforms
->destroy(transforms
);
416 *encryption_algorithm
= selected_encryption_algorithm
;
417 *pseudo_random_function
= selected_pseudo_random_function
;
418 *integrity_algorithm
= selected_integrity_algorithm
;
423 * Implements function configuration_manager_t.is_dh_group_allowed_for_host.
425 static status_t
is_dh_group_allowed_for_host(private_configuration_manager_t
*this, host_t
*host
, diffie_hellman_group_t group
, bool *allowed
)
428 * Only the two DH groups 768 and 1024 are supported for each configuration
431 if (group
== MODP_768_BIT
|| group
== MODP_1024_BIT
)
437 this->logger
->log(this->logger
,CONTROL
| MORE
, "DH group %s is %s",mapping_find(diffie_hellman_group_m
, group
),(allowed
)?
"allowed" : "not allowed");
443 * Implements function destroy of configuration_t.
444 * See #configuration_s.destroy for description.
446 static status_t
destroy(private_configuration_manager_t
*this)
448 this->logger
->log(this->logger
,CONTROL
| MORE
, "Going to destroy configuration manager ");
450 this->logger
->log(this->logger
,CONTROL
| MOST
, "Destroy assigned logger");
451 global_logger_manager
->destroy_logger(global_logger_manager
,this->logger
);
452 allocator_free(this);
457 * Described in header-file
459 configuration_manager_t
*configuration_manager_create()
461 private_configuration_manager_t
*this = allocator_alloc_thing(private_configuration_manager_t
);
468 /* public functions */
469 this->public.destroy
= (status_t(*)(configuration_manager_t
*))destroy
;
470 this->public.get_remote_host
= (status_t(*)(configuration_manager_t
*,char*,host_t
**))get_remote_host
;
471 this->public.get_local_host
= (status_t(*)(configuration_manager_t
*,char*,host_t
**))get_local_host
;
472 this->public.get_dh_group_number
= (status_t(*)(configuration_manager_t
*,char*,u_int16_t
*, u_int16_t
))get_dh_group_number
;
473 this->public.get_proposals_for_host
= (status_t(*)(configuration_manager_t
*,host_t
*,iterator_t
*))get_proposals_for_host
;
474 this->public.select_proposals_for_host
= (status_t(*)(configuration_manager_t
*,host_t
*,iterator_t
*,iterator_t
*))select_proposals_for_host
;
475 this->public.get_transforms_for_host_and_proposals
= (status_t (*) (configuration_manager_t
*, host_t
*, iterator_t
*,encryption_algorithm_t
*,pseudo_random_function_t
*, integrity_algorithm_t
*)) get_transforms_for_host_and_proposals
;
476 this->public.is_dh_group_allowed_for_host
= (status_t(*)(configuration_manager_t
*,host_t
*,diffie_hellman_group_t
,bool*)) is_dh_group_allowed_for_host
;
478 /* private variables */
479 this->logger
= global_logger_manager
->create_logger(global_logger_manager
,CONFIGURATION_MANAGER
,NULL
);
481 if (this->logger
== NULL
)
483 allocator_free(this);
487 return (&this->public);