- code cleaned
[strongswan.git] / Source / charon / sa / states / initiator_init.c
1 /**
2 * @file initiator_init.c
3 *
4 * @brief Implementation of initiator_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 "initiator_init.h"
24
25
26 #include <daemon.h>
27 #include <sa/states/state.h>
28 #include <sa/states/ike_sa_init_requested.h>
29 #include <utils/allocator.h>
30 #include <queues/jobs/retransmit_request_job.h>
31 #include <transforms/diffie_hellman.h>
32 #include <encoding/payloads/sa_payload.h>
33 #include <encoding/payloads/ke_payload.h>
34 #include <encoding/payloads/nonce_payload.h>
35
36
37 typedef struct private_initiator_init_t private_initiator_init_t;
38
39 /**
40 * Private data of a initiator_init_t object..
41 *
42 */
43 struct private_initiator_init_t {
44 /**
45 * Methods of the state_t interface.
46 */
47 initiator_init_t public;
48
49 /**
50 * Assigned IKE_SA.
51 */
52 protected_ike_sa_t *ike_sa;
53
54 /**
55 * Diffie hellman object used to generate public DH value.
56 * This objet is passed to the next state of type IKE_SA_INIT_REQUESTED.
57 */
58 diffie_hellman_t *diffie_hellman;
59
60 /**
61 * DH group number.
62 */
63 u_int16_t dh_group_number;
64
65 /**
66 * DH group priority used to get dh_group_number from configuration manager.
67 * This priority is passed to the next state of type IKE_SA_INIT_REQUESTED.
68 */
69 u_int16_t dh_group_priority;
70
71 /**
72 * Sent nonce.
73 * This nonce is passed to the next state of type IKE_SA_INIT_REQUESTED.
74 */
75 chunk_t sent_nonce;
76
77 /**
78 * Assigned logger.
79 *
80 * Is logger of ike_sa!
81 */
82 logger_t *logger;
83
84 /**
85 * Builds the SA payload for this state.
86 *
87 * @param this calling object
88 * @param request message_t object to add the SA payload
89 */
90 void (*build_sa_payload) (private_initiator_init_t *this, message_t *request);
91
92 /**
93 * Builds the KE payload for this state.
94 *
95 * @param this calling object
96 * @param request message_t object to add the KE payload
97 */
98 void (*build_ke_payload) (private_initiator_init_t *this, message_t *request);
99
100 /**
101 * Builds the NONCE payload for this state.
102 *
103 * @param this calling object
104 * @param request message_t object to add the NONCE payload
105 */
106 void (*build_nonce_payload) (private_initiator_init_t *this,message_t *request);
107
108 /**
109 * Destroy function called internally of this class after state change to state
110 * IKE_SA_INIT_REQUESTED succeeded.
111 *
112 * This destroy function does not destroy objects which were passed to the new state.
113 *
114 * @param this calling object
115 */
116 void (*destroy_after_state_change) (private_initiator_init_t *this);
117 };
118
119 /**
120 * Implementation of initiator_init_t.initiate_connection.
121 */
122 static status_t initiate_connection (private_initiator_init_t *this, char *name)
123 {
124 init_config_t *init_config;
125 sa_config_t *sa_config;
126 status_t status;
127
128
129 this->logger->log(this->logger, CONTROL, "Initializing connection %s",name);
130
131 status = charon->configuration_manager->get_init_config_for_name(charon->configuration_manager,name,&init_config);
132 if (status != SUCCESS)
133 {
134 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve INIT configuration informations for %s",name);
135 return DELETE_ME;
136 }
137
138 this->ike_sa->set_init_config(this->ike_sa,init_config);
139
140 status = charon->configuration_manager->get_sa_config_for_name(charon->configuration_manager,name,&sa_config);
141
142 if (status != SUCCESS)
143 {
144 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve SA configuration informations for %s",name);
145 return DELETE_ME;
146 }
147
148 this->ike_sa->set_sa_config(this->ike_sa,sa_config);
149
150 /* host informations are read from configuration */
151 this->ike_sa->set_other_host(this->ike_sa,init_config->get_other_host_clone(init_config));
152 this->ike_sa->set_my_host(this->ike_sa,init_config->get_my_host_clone(init_config));
153
154 this->dh_group_number = init_config->get_dh_group_number(init_config,this->dh_group_priority);
155 if (this->dh_group_number == MODP_UNDEFINED)
156 {
157 this->logger->log(this->logger, ERROR | MORE, "Diffie hellman group could not be retrieved with priority %d", this->dh_group_priority);
158 return DELETE_ME;
159 }
160
161 /* next step is done in retry_initiate_connection */
162 return this->public.retry_initiate_connection(&(this->public),this->dh_group_priority);
163 }
164
165 /**
166 * Implementation of initiator_init_t.retry_initiate_connection.
167 */
168 status_t retry_initiate_connection (private_initiator_init_t *this, int dh_group_priority)
169 {
170 ike_sa_init_requested_t *next_state;
171 chunk_t ike_sa_init_request_data;
172 init_config_t *init_config;
173 ike_sa_id_t *ike_sa_id;
174 message_t *message;
175 status_t status;
176
177
178 this->dh_group_priority = dh_group_priority;
179
180 init_config = this->ike_sa->get_init_config(this->ike_sa);
181
182 ike_sa_id = this->ike_sa->public.get_id(&(this->ike_sa->public));
183 ike_sa_id->set_responder_spi(ike_sa_id,0);
184
185 this->dh_group_number = init_config->get_dh_group_number(init_config,dh_group_priority);
186 if (this->dh_group_number == MODP_UNDEFINED)
187 {
188 this->logger->log(this->logger, ERROR | MORE, "Diffie hellman group could not be retrieved with priority %d", dh_group_priority);
189 return DELETE_ME;
190 }
191
192 this->diffie_hellman = diffie_hellman_create(this->dh_group_number);
193
194 /* going to build message */
195 this->logger->log(this->logger, CONTROL|MOST, "Going to build message");
196 this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, TRUE, &message);
197
198 /* build SA payload */
199 this->build_sa_payload(this, message);
200
201 /* build KE payload */
202 this->build_ke_payload(this, message);
203
204 /* build Nonce payload */
205 this->build_nonce_payload(this,message);
206
207
208 /* message can now be sent (must not be destroyed) */
209 status = this->ike_sa->send_request(this->ike_sa, message);
210 if (status != SUCCESS)
211 {
212 this->logger->log(this->logger, ERROR, "Could not send request message");
213 message->destroy(message);
214 return DELETE_ME;
215 }
216
217 message = this->ike_sa->get_last_requested_message(this->ike_sa);
218
219 ike_sa_init_request_data = message->get_packet_data(message);
220
221 /* state can now be changed */
222 this->logger->log(this->logger, CONTROL|MOST, "Create next state object");
223 next_state = ike_sa_init_requested_create(this->ike_sa, this->dh_group_priority, this->diffie_hellman, this->sent_nonce,ike_sa_init_request_data);
224
225 /* state can now be changed */
226 this->ike_sa->set_new_state(this->ike_sa,(state_t *) next_state);
227
228 /* state has NOW changed :-) */
229 this->logger->log(this->logger, CONTROL|MORE, "Changed state of IKE_SA from %s to %s", mapping_find(ike_sa_state_m,INITIATOR_INIT),mapping_find(ike_sa_state_m,IKE_SA_INIT_REQUESTED) );
230
231 this->logger->log(this->logger, CONTROL|MOST, "Destroy old sate object");
232 this->destroy_after_state_change(this);
233 return SUCCESS;
234 }
235
236 /**
237 * Implementation of private_initiator_init_t.build_sa_payload.
238 */
239 static void build_sa_payload(private_initiator_init_t *this, message_t *request)
240 {
241 sa_payload_t* sa_payload;
242 size_t proposal_count;
243 ike_proposal_t *proposals;
244 init_config_t *init_config;
245
246 this->logger->log(this->logger, CONTROL|MORE, "Building SA payload");
247
248 init_config = this->ike_sa->get_init_config(this->ike_sa);
249
250 proposal_count = init_config->get_proposals(init_config,&proposals);
251
252 sa_payload = sa_payload_create_from_ike_proposals(proposals,proposal_count);
253
254 allocator_free(proposals);
255
256 this->logger->log(this->logger, CONTROL|MOST, "Add SA payload to message");
257 request->add_payload(request, (payload_t *) sa_payload);
258 }
259
260 /**
261 * Implementation of private_initiator_init_t.build_ke_payload.
262 */
263 static void build_ke_payload(private_initiator_init_t *this, message_t *request)
264 {
265 ke_payload_t *ke_payload;
266 chunk_t key_data;
267
268 this->logger->log(this->logger, CONTROL|MORE, "Building KE payload");
269
270 this->diffie_hellman->get_my_public_value(this->diffie_hellman,&key_data);
271
272 ke_payload = ke_payload_create();
273 ke_payload->set_dh_group_number(ke_payload, this->dh_group_number);
274 ke_payload->set_key_exchange_data(ke_payload, key_data);
275
276 allocator_free_chunk(&key_data);
277
278 this->logger->log(this->logger, CONTROL|MOST, "Add KE payload to message");
279 request->add_payload(request, (payload_t *) ke_payload);
280 }
281
282 /**
283 * Implementation of private_initiator_init_t.build_nonce_payload.
284 */
285 static void build_nonce_payload(private_initiator_init_t *this, message_t *request)
286 {
287 nonce_payload_t *nonce_payload;
288 randomizer_t *randomizer;
289
290 this->logger->log(this->logger, CONTROL|MORE, "Building NONCE payload");
291
292 this->logger->log(this->logger, CONTROL|MOST, "Get pseudo random bytes for NONCE");
293 randomizer = this->ike_sa->get_randomizer(this->ike_sa);
294
295 randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE, &(this->sent_nonce));
296
297 this->logger->log(this->logger, RAW|MOST, "Initiator NONCE",&(this->sent_nonce));
298
299 nonce_payload = nonce_payload_create();
300
301 nonce_payload->set_nonce(nonce_payload, this->sent_nonce);
302
303 this->logger->log(this->logger, CONTROL|MOST, "Add NONCE payload to message");
304 request->add_payload(request, (payload_t *) nonce_payload);
305 }
306
307 /**
308 * Implementation of state_t.process_message.
309 */
310 static status_t process_message(private_initiator_init_t *this, message_t *message)
311 {
312 this->logger->log(this->logger, ERROR|MORE, "In state INITIATOR_INIT no message is processed");
313 return FAILED;
314 }
315
316 /**
317 * Implementation of state_t.get_state.
318 */
319 static ike_sa_state_t get_state(private_initiator_init_t *this)
320 {
321 return INITIATOR_INIT;
322 }
323
324 /**
325 * Implementation of state_t.destroy.
326 */
327 static void destroy(private_initiator_init_t *this)
328 {
329 this->logger->log(this->logger, CONTROL | MORE, "Going to destroy initiator_init_t state object");
330
331 /* destroy diffie hellman object */
332 if (this->diffie_hellman != NULL)
333 {
334 this->logger->log(this->logger, CONTROL | MOST, "Destroy diffie_hellman_t object");
335 this->diffie_hellman->destroy(this->diffie_hellman);
336 }
337 if (this->sent_nonce.ptr != NULL)
338 {
339 this->logger->log(this->logger, CONTROL | MOST, "Free memory of sent nonce");
340 allocator_free(this->sent_nonce.ptr);
341 }
342 allocator_free(this);
343 }
344
345 /**
346 * Implementation of private_initiator_init_t.destroy_after_state_change
347 */
348 static void destroy_after_state_change (private_initiator_init_t *this)
349 {
350 this->logger->log(this->logger, CONTROL | MORE, "Going to destroy initiator_init_t state object");
351 allocator_free(this);
352 }
353
354 /*
355 * Described in header.
356 */
357 initiator_init_t *initiator_init_create(protected_ike_sa_t *ike_sa)
358 {
359 private_initiator_init_t *this = allocator_alloc_thing(private_initiator_init_t);
360
361 /* interface functions */
362 this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
363 this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
364 this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
365
366 /* public functions */
367 this->public.initiate_connection = (status_t (*)(initiator_init_t *, char *)) initiate_connection;
368 this->public.retry_initiate_connection = (status_t (*)(initiator_init_t *, int )) retry_initiate_connection;
369
370 /* private functions */
371 this->destroy_after_state_change = destroy_after_state_change;
372 this->build_nonce_payload = build_nonce_payload;
373 this->build_sa_payload = build_sa_payload;
374 this->build_ke_payload = build_ke_payload;
375
376 /* private data */
377 this->ike_sa = ike_sa;
378 this->dh_group_priority = 1;
379 this->logger = this->ike_sa->get_logger(this->ike_sa);
380 this->sent_nonce = CHUNK_INITIALIZER;
381 this->diffie_hellman = NULL;
382
383 return &(this->public);
384 }