7ade885d4621836ef22a7623460ebfbd1306f8a4
[strongswan.git] / Source / charon / sa / states / initiator_init.c
1 /**
2 * @file initiator_init.c
3 *
4 * @brief Start state of a IKE_SA as initiator
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 <transforms/diffie_hellman.h>
31 #include <encoding/payloads/sa_payload.h>
32 #include <encoding/payloads/ke_payload.h>
33 #include <encoding/payloads/nonce_payload.h>
34
35
36 typedef struct private_initiator_init_t private_initiator_init_t;
37
38 /**
39 * Private data of a initiator_init_t object.
40 *
41 */
42 struct private_initiator_init_t {
43 /**
44 * Methods of the state_t interface.
45 */
46 initiator_init_t public;
47
48 /**
49 * Assigned IKE_SA.
50 */
51 protected_ike_sa_t *ike_sa;
52
53 /**
54 * Diffie hellman object used to generate public DH value.
55 * This objet is passed to the next state of type ike_sa_init_requested_t.
56 */
57 diffie_hellman_t *diffie_hellman;
58
59 /**
60 * DH group number.
61 */
62 u_int16_t dh_group_number;
63
64 /**
65 * DH group priority used to get dh_group_number from configuration manager.
66 * This priority is passed to the next state of type ike_sa_init_requested_t.
67 */
68 u_int16_t dh_group_priority;
69
70 /**
71 * Sent nonce.
72 * This nonce is passed to the next state of type ike_sa_init_requested_t.
73 */
74 chunk_t sent_nonce;
75
76 /**
77 * Proposals used to initiate connection.
78 *
79 */
80 linked_list_t *proposals;
81
82 /**
83 * Logger used to log :-)
84 *
85 * Is logger of ike_sa!
86 */
87 logger_t *logger;
88
89 /**
90 * Builds the IKE_SA_INIT request message.
91 *
92 * @param this calling object
93 * @param message the created message will be stored at this location
94 */
95 void (*build_ike_sa_init_request) (private_initiator_init_t *this, message_t **message);
96
97 /**
98 * Builds the SA payload for this state.
99 *
100 * @param this calling object
101 * @param payload The generated SA payload object of type ke_payload_t is
102 * stored at this location.
103 */
104 void (*build_sa_payload) (private_initiator_init_t *this, payload_t **payload);
105
106 /**
107 * Builds the KE payload for this state.
108 *
109 * @param this calling object
110 * @param payload The generated KE payload object of type ke_payload_t is
111 * stored at this location.
112 */
113 void (*build_ke_payload) (private_initiator_init_t *this, payload_t **payload);
114
115 /**
116 * Builds the NONCE payload for this state.
117 *
118 * @param this calling object
119 * @param payload The generated NONCE payload object of type ke_payload_t is
120 * stored at this location.
121 */
122 void (*build_nonce_payload) (private_initiator_init_t *this, payload_t **payload);
123
124 /**
125 * Destroy function called internally of this class after state change succeeded.
126 *
127 * This destroy function does not destroy objects which were passed to the new state.
128 *
129 * @param this calling object
130 */
131 void (*destroy_after_state_change) (private_initiator_init_t *this);
132 };
133
134 /**
135 * Implements function initiator_init_t.initiate_connection.
136 */
137 static status_t initiate_connection (private_initiator_init_t *this, char *name)
138 {
139 iterator_t *proposal_iterator;
140 ike_sa_init_requested_t *next_state;
141 message_t *message;
142 packet_t *packet;
143 status_t status;
144 host_t *my_host;
145 host_t *other_host;
146 randomizer_t *randomizer;
147
148 this->logger->log(this->logger, CONTROL, "Initializing connection %s",name);
149
150 /* get local host */
151 status = charon->configuration_manager->get_local_host(charon->configuration_manager, name, &my_host);
152 if (status != SUCCESS)
153 {
154 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve local host configuration information for %s",name);
155 return INVALID_ARG;
156 }
157 this->ike_sa->set_my_host(this->ike_sa,my_host);
158
159 /* get remote host */
160 status = charon->configuration_manager->get_remote_host(charon->configuration_manager, name, &other_host);
161 if (status != SUCCESS)
162 {
163 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve remote host configuration information for %s",name);
164 return INVALID_ARG;
165 }
166 this->ike_sa->set_other_host(this->ike_sa,other_host);
167
168 /* get dh group */
169 status = charon->configuration_manager->get_dh_group_number(charon->configuration_manager, name, &(this->dh_group_number), this->dh_group_priority);
170 if (status != SUCCESS)
171 {
172 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve DH group number configuration for %s",name);
173 return INVALID_ARG;
174 }
175
176 /* get proposals */
177 proposal_iterator = this->proposals->create_iterator(this->proposals, FALSE);
178 status = charon->configuration_manager->get_proposals_for_host(charon->configuration_manager, this->ike_sa->get_other_host(this->ike_sa), proposal_iterator);
179
180 proposal_iterator->destroy(proposal_iterator);
181 if (status != SUCCESS)
182 {
183 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve Proposals for %s",name);
184 return status;
185 }
186
187 /* a diffie hellman object could allready exist caused by an failed initiate_connection call */
188 if (this->diffie_hellman == NULL)
189 {
190 this->diffie_hellman = diffie_hellman_create(this->dh_group_number);
191 }
192
193 if (this->diffie_hellman == NULL)
194 {
195 this->logger->log(this->logger, ERROR, "Object of type diffie_hellman_t could not be created!");
196 return FAILED;
197 }
198
199 if (this->sent_nonce.ptr != NULL)
200 {
201 allocator_free(this->sent_nonce.ptr);
202 this->sent_nonce = CHUNK_INITIALIZER;
203 }
204
205 this->logger->log(this->logger, CONTROL|MOST, "Get pseudo random bytes for nonce");
206 randomizer = this->ike_sa->get_randomizer(this->ike_sa);
207 randomizer->allocate_pseudo_random_bytes(randomizer, NONCE_SIZE, &(this->sent_nonce));
208
209 this->logger->log(this->logger, RAW|MOST, "Nonce",&(this->sent_nonce));
210
211 this->build_ike_sa_init_request (this,&message);
212
213 /* generate packet */
214 this->logger->log(this->logger, CONTROL|MOST, "generate packet from message");
215 status = message->generate(message, NULL, NULL, &packet);
216 if (status != SUCCESS)
217 {
218 this->logger->log(this->logger, ERROR, "could not generate packet from message");
219 message->destroy(message);
220 return status;
221 }
222
223 this->logger->log(this->logger, CONTROL|MOST, "Add packet to global send queue");
224 charon->send_queue->add(charon->send_queue, packet);
225
226 /* state can now be changed */
227 this->logger->log(this->logger, CONTROL|MOST, "Create next state object");
228 next_state = ike_sa_init_requested_create(this->ike_sa, this->dh_group_number, this->diffie_hellman, this->sent_nonce);
229
230 /* last message can now be set */
231 status = this->ike_sa->set_last_requested_message(this->ike_sa, message);
232
233 if (status != SUCCESS)
234 {
235 this->logger->log(this->logger, ERROR, "Could not set last requested message");
236 (next_state->state_interface).destroy(&(next_state->state_interface));
237 message->destroy(message);
238 return status;
239 }
240
241 /* state can now be changed */
242 this->ike_sa->set_new_state(this->ike_sa,(state_t *) next_state);
243
244 /* state has NOW changed :-) */
245 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) );
246
247 this->logger->log(this->logger, CONTROL|MOST, "Destroy old sate object");
248 this->destroy_after_state_change(this);
249
250 return SUCCESS;
251 }
252
253 /**
254 * implements private_initiator_init_t.build_ike_sa_init_request
255 */
256 static void build_ike_sa_init_request (private_initiator_init_t *this, message_t **request)
257 {
258 payload_t *payload;
259 message_t *message;
260
261 /* going to build message */
262 this->logger->log(this->logger, CONTROL|MOST, "Going to build message");
263 this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, TRUE, &message);
264
265 /* build SA payload */
266 this->build_sa_payload(this, &payload);
267 this->logger->log(this->logger, CONTROL|MOST, "add SA payload to message");
268 message->add_payload(message, payload);
269
270 /* build KE payload */
271 this->build_ke_payload(this, &payload);
272 this->logger->log(this->logger, CONTROL|MOST, "add KE payload to message");
273 message->add_payload(message, payload);
274
275 /* build Nonce payload */
276 this->build_nonce_payload(this, &payload);
277 this->logger->log(this->logger, CONTROL|MOST, "add nonce payload to message");
278 message->add_payload(message, payload);
279
280 *request = message;
281 }
282
283 /**
284 * implements private_initiator_init_t.build_sa_payload
285 */
286 static void build_sa_payload(private_initiator_init_t *this, payload_t **payload)
287 {
288 sa_payload_t* sa_payload;
289 iterator_t *proposal_iterator;
290
291 /* SA payload takes proposals from this->ike_sa_init_data.proposals
292 * and writes them to the created sa_payload
293 */
294
295 this->logger->log(this->logger, CONTROL|MORE, "building sa payload");
296 proposal_iterator = this->proposals->create_iterator(this->proposals, FALSE);
297
298 sa_payload = sa_payload_create();
299
300 while (proposal_iterator->has_next(proposal_iterator))
301 {
302 proposal_substructure_t *current_proposal;
303 proposal_substructure_t *current_proposal_clone;
304 proposal_iterator->current(proposal_iterator,(void **) &current_proposal);
305
306 current_proposal_clone = current_proposal->clone(current_proposal);
307
308 sa_payload->add_proposal_substructure(sa_payload,current_proposal_clone);
309 }
310 proposal_iterator->destroy(proposal_iterator);
311
312 *payload = (payload_t *) sa_payload;
313 }
314
315 /**
316 * implements private_initiator_init_t.build_ke_payload
317 */
318 static void build_ke_payload(private_initiator_init_t *this, payload_t **payload)
319 {
320 ke_payload_t *ke_payload;
321 chunk_t key_data;
322
323 this->logger->log(this->logger, CONTROL|MORE, "building ke payload");
324
325 this->diffie_hellman->get_my_public_value(this->diffie_hellman,&key_data);
326
327 ke_payload = ke_payload_create();
328 ke_payload->set_dh_group_number(ke_payload, this->dh_group_number);
329 ke_payload->set_key_exchange_data(ke_payload, key_data);
330
331 allocator_free_chunk(&key_data);
332 *payload = (payload_t *) ke_payload;
333 }
334
335 /**
336 * implements private_initiator_init_t.build_nonce_payload
337 */
338 static void build_nonce_payload(private_initiator_init_t *this, payload_t **payload)
339 {
340 nonce_payload_t *nonce_payload;
341
342 this->logger->log(this->logger, CONTROL|MORE, "building nonce payload");
343
344 nonce_payload = nonce_payload_create();
345
346 nonce_payload->set_nonce(nonce_payload, this->sent_nonce);
347
348 *payload = (payload_t *) nonce_payload;
349 }
350
351 /**
352 * Implements state_t.get_state
353 */
354 static status_t process_message(private_initiator_init_t *this, message_t *message, state_t **new_state)
355 {
356 *new_state = (state_t *) this;
357 this->logger->log(this->logger, ERROR|MORE, "In state INITIATOR_INIT no message is processed");
358 return FAILED;
359 }
360
361 /**
362 * Implements state_t.get_state
363 */
364 static ike_sa_state_t get_state(private_initiator_init_t *this)
365 {
366 return INITIATOR_INIT;
367 }
368
369 /**
370 * Implements state_t.get_state
371 */
372 static void destroy(private_initiator_init_t *this)
373 {
374 this->logger->log(this->logger, CONTROL | MORE, "Going to destroy initiator_init_t state object");
375
376 /* destroy stored proposal */
377 this->logger->log(this->logger, CONTROL | MOST, "Destroy stored proposals");
378 while (this->proposals->get_count(this->proposals) > 0)
379 {
380 proposal_substructure_t *current_proposal;
381 this->proposals->remove_first(this->proposals,(void **)&current_proposal);
382 current_proposal->destroy(current_proposal);
383 }
384 this->proposals->destroy(this->proposals);
385
386 /* destroy diffie hellman object */
387 if (this->diffie_hellman != NULL)
388 {
389 this->logger->log(this->logger, CONTROL | MOST, "Destroy diffie_hellman_t object");
390 this->diffie_hellman->destroy(this->diffie_hellman);
391 }
392 if (this->sent_nonce.ptr != NULL)
393 {
394 this->logger->log(this->logger, CONTROL | MOST, "Free memory of sent nonce");
395 allocator_free(this->sent_nonce.ptr);
396 }
397 allocator_free(this);
398 }
399
400 /**
401 * Implements private_initiator_init_t.destroy_after_state_change
402 */
403 static void destroy_after_state_change (private_initiator_init_t *this)
404 {
405 this->logger->log(this->logger, CONTROL | MORE, "Going to destroy initiator_init_t state object");
406
407 /* destroy stored proposal */
408 this->logger->log(this->logger, CONTROL | MOST, "Destroy stored proposals");
409 while (this->proposals->get_count(this->proposals) > 0)
410 {
411 proposal_substructure_t *current_proposal;
412 this->proposals->remove_first(this->proposals,(void **)&current_proposal);
413 current_proposal->destroy(current_proposal);
414 }
415 this->proposals->destroy(this->proposals);
416 allocator_free(this);
417 }
418
419 /*
420 * Described in header.
421 */
422 initiator_init_t *initiator_init_create(protected_ike_sa_t *ike_sa)
423 {
424 private_initiator_init_t *this = allocator_alloc_thing(private_initiator_init_t);
425
426 /* interface functions */
427 this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *)) process_message;
428 this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
429 this->public.state_interface.destroy = (void (*) (state_t *)) destroy;
430
431 /* public functions */
432 this->public.initiate_connection = (status_t (*)(initiator_init_t *, char *)) initiate_connection;
433
434 /* private functions */
435 this->destroy_after_state_change = destroy_after_state_change;
436 this->build_ike_sa_init_request = build_ike_sa_init_request;
437 this->build_nonce_payload = build_nonce_payload;
438 this->build_sa_payload = build_sa_payload;
439 this->build_ke_payload = build_ke_payload;
440
441 /* private data */
442 this->ike_sa = ike_sa;
443 this->dh_group_priority = 1;
444 this->logger = this->ike_sa->get_logger(this->ike_sa);
445 this->proposals = linked_list_create();
446 this->sent_nonce = CHUNK_INITIALIZER;
447
448 return &(this->public);
449 }