6ab698f89713e046d43495bd1ccc7ed293efa7f9
[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 | LEVEL1, "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 | LEVEL1, "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, AUDIT, "Could not find a matching diffie hellman group after %d. try. Aborting.",
158 this->dh_group_priority);
159 return DELETE_ME;
160 }
161
162 /* next step is done in retry_initiate_connection */
163 return this->public.retry_initiate_connection(&(this->public),this->dh_group_priority);
164 }
165
166 /**
167 * Implementation of initiator_init_t.retry_initiate_connection.
168 */
169 status_t retry_initiate_connection (private_initiator_init_t *this, int dh_group_priority)
170 {
171 ike_sa_init_requested_t *next_state;
172 chunk_t ike_sa_init_request_data;
173 init_config_t *init_config;
174 ike_sa_id_t *ike_sa_id;
175 message_t *message;
176 status_t status;
177
178
179 this->dh_group_priority = dh_group_priority;
180
181 init_config = this->ike_sa->get_init_config(this->ike_sa);
182
183 ike_sa_id = this->ike_sa->public.get_id(&(this->ike_sa->public));
184 ike_sa_id->set_responder_spi(ike_sa_id,0);
185
186 this->dh_group_number = init_config->get_dh_group_number(init_config,dh_group_priority);
187 if (this->dh_group_number == MODP_UNDEFINED)
188 {
189 this->logger->log(this->logger, AUDIT, "Could not find a matching diffie hellman group after %d. try. Aborting.",
190 this->dh_group_priority);
191 return DELETE_ME;
192 }
193
194 this->diffie_hellman = diffie_hellman_create(this->dh_group_number);
195
196 /* going to build message */
197 this->logger->log(this->logger, CONTROL|LEVEL2, "Going to build message");
198 this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, TRUE, &message);
199
200 /* build SA payload */
201 this->build_sa_payload(this, message);
202
203 /* build KE payload */
204 this->build_ke_payload(this, message);
205
206 /* build Nonce payload */
207 this->build_nonce_payload(this,message);
208
209
210 /* message can now be sent (must not be destroyed) */
211 status = this->ike_sa->send_request(this->ike_sa, message);
212 if (status != SUCCESS)
213 {
214 this->logger->log(this->logger, AUDIT, "Unable to initiate connection, could not send message. Aborting");
215 message->destroy(message);
216 return DELETE_ME;
217 }
218
219 message = this->ike_sa->get_last_requested_message(this->ike_sa);
220
221 ike_sa_init_request_data = message->get_packet_data(message);
222
223 /* state can now be changed */
224 this->logger->log(this->logger, CONTROL|LEVEL2, "Create next state object");
225 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);
226 this->ike_sa->set_new_state(this->ike_sa,(state_t *) next_state);
227
228 this->logger->log(this->logger, CONTROL|LEVEL2, "Destroy old sate object");
229 this->destroy_after_state_change(this);
230 return SUCCESS;
231 }
232
233 /**
234 * Implementation of private_initiator_init_t.build_sa_payload.
235 */
236 static void build_sa_payload(private_initiator_init_t *this, message_t *request)
237 {
238 sa_payload_t* sa_payload;
239 size_t proposal_count;
240 ike_proposal_t *proposals;
241 init_config_t *init_config;
242
243 this->logger->log(this->logger, CONTROL|LEVEL1, "Building SA payload");
244
245 init_config = this->ike_sa->get_init_config(this->ike_sa);
246
247 proposal_count = init_config->get_proposals(init_config,&proposals);
248
249 sa_payload = sa_payload_create_from_ike_proposals(proposals,proposal_count);
250
251 allocator_free(proposals);
252
253 this->logger->log(this->logger, CONTROL|LEVEL2, "Add SA payload to message");
254 request->add_payload(request, (payload_t *) sa_payload);
255 }
256
257 /**
258 * Implementation of private_initiator_init_t.build_ke_payload.
259 */
260 static void build_ke_payload(private_initiator_init_t *this, message_t *request)
261 {
262 ke_payload_t *ke_payload;
263 chunk_t key_data;
264
265 this->logger->log(this->logger, CONTROL|LEVEL1, "Building KE payload");
266
267 this->diffie_hellman->get_my_public_value(this->diffie_hellman,&key_data);
268
269 ke_payload = ke_payload_create();
270 ke_payload->set_dh_group_number(ke_payload, this->dh_group_number);
271 ke_payload->set_key_exchange_data(ke_payload, key_data);
272
273 allocator_free_chunk(&key_data);
274
275 this->logger->log(this->logger, CONTROL|LEVEL2, "Add KE payload to message");
276 request->add_payload(request, (payload_t *) ke_payload);
277 }
278
279 /**
280 * Implementation of private_initiator_init_t.build_nonce_payload.
281 */
282 static void build_nonce_payload(private_initiator_init_t *this, message_t *request)
283 {
284 nonce_payload_t *nonce_payload;
285 randomizer_t *randomizer;
286
287 this->logger->log(this->logger, CONTROL|LEVEL1, "Building NONCE payload");
288
289 this->logger->log(this->logger, CONTROL|LEVEL2, "Get pseudo random bytes for NONCE");
290 randomizer = this->ike_sa->get_randomizer(this->ike_sa);
291
292 randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE, &(this->sent_nonce));
293
294 this->logger->log(this->logger, RAW|LEVEL2, "Initiator NONCE",&(this->sent_nonce));
295
296 nonce_payload = nonce_payload_create();
297
298 nonce_payload->set_nonce(nonce_payload, this->sent_nonce);
299
300 this->logger->log(this->logger, CONTROL|LEVEL2, "Add NONCE payload to message");
301 request->add_payload(request, (payload_t *) nonce_payload);
302 }
303
304 /**
305 * Implementation of state_t.process_message.
306 */
307 static status_t process_message(private_initiator_init_t *this, message_t *message)
308 {
309 this->logger->log(this->logger, ERROR, "In state INITIATOR_INIT no message is processed");
310 return FAILED;
311 }
312
313 /**
314 * Implementation of state_t.get_state.
315 */
316 static ike_sa_state_t get_state(private_initiator_init_t *this)
317 {
318 return INITIATOR_INIT;
319 }
320
321 /**
322 * Implementation of state_t.destroy.
323 */
324 static void destroy(private_initiator_init_t *this)
325 {
326 this->logger->log(this->logger, CONTROL | LEVEL3, "Going to destroy initiator_init_t state object");
327
328 /* destroy diffie hellman object */
329 if (this->diffie_hellman != NULL)
330 {
331 this->logger->log(this->logger, CONTROL | LEVEL3, "Destroy diffie_hellman_t object");
332 this->diffie_hellman->destroy(this->diffie_hellman);
333 }
334 if (this->sent_nonce.ptr != NULL)
335 {
336 this->logger->log(this->logger, CONTROL | LEVEL3, "Free memory of sent nonce");
337 allocator_free(this->sent_nonce.ptr);
338 }
339 allocator_free(this);
340 }
341
342 /**
343 * Implementation of private_initiator_init_t.destroy_after_state_change
344 */
345 static void destroy_after_state_change (private_initiator_init_t *this)
346 {
347 this->logger->log(this->logger, CONTROL | LEVEL3, "Going to destroy initiator_init_t state object");
348 allocator_free(this);
349 }
350
351 /*
352 * Described in header.
353 */
354 initiator_init_t *initiator_init_create(protected_ike_sa_t *ike_sa)
355 {
356 private_initiator_init_t *this = allocator_alloc_thing(private_initiator_init_t);
357
358 /* interface functions */
359 this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
360 this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
361 this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
362
363 /* public functions */
364 this->public.initiate_connection = (status_t (*)(initiator_init_t *, char *)) initiate_connection;
365 this->public.retry_initiate_connection = (status_t (*)(initiator_init_t *, int )) retry_initiate_connection;
366
367 /* private functions */
368 this->destroy_after_state_change = destroy_after_state_change;
369 this->build_nonce_payload = build_nonce_payload;
370 this->build_sa_payload = build_sa_payload;
371 this->build_ke_payload = build_ke_payload;
372
373 /* private data */
374 this->ike_sa = ike_sa;
375 this->dh_group_priority = 1;
376 this->logger = this->ike_sa->get_logger(this->ike_sa);
377 this->sent_nonce = CHUNK_INITIALIZER;
378 this->diffie_hellman = NULL;
379
380 return &(this->public);
381 }