current state
[strongswan.git] / Source / charon / 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 "state.h"
27 #include "ike_sa_init_requested.h"
28 #include "../globals.h"
29 #include "../utils/allocator.h"
30 #include "../transforms/diffie_hellman.h"
31 #include "../payloads/sa_payload.h"
32 #include "../payloads/ke_payload.h"
33 #include "../payloads/nonce_payload.h"
34
35
36 /**
37 * Private data of a initiator_init_t object.
38 *
39 */
40 typedef struct private_initiator_init_s private_initiator_init_t;
41 struct private_initiator_init_s {
42 /**
43 * methods of the state_t interface
44 */
45 initiator_init_t public;
46
47 /**
48 * Assigned IKE_SA
49 */
50 protected_ike_sa_t *ike_sa;
51
52 /**
53 * Diffie hellman object
54 */
55 diffie_hellman_t *diffie_hellman;
56
57 /**
58 * DH group number
59 */
60 u_int16_t dh_group_number;
61
62 /**
63 * DH group priority
64 */
65 u_int16_t dh_group_priority;
66
67 /**
68 * Sent nonce
69 */
70 chunk_t sent_nonce;
71
72 /**
73 * Proposals used to initiate connection
74 */
75 linked_list_t *proposals;
76
77 /**
78 * Logger used to log data
79 *
80 * Is logger of ike_sa!
81 */
82 logger_t *logger;
83
84 status_t (*build_sa_payload) (private_initiator_init_t *this, payload_t **payload);
85 status_t (*build_ke_payload) (private_initiator_init_t *this, payload_t **payload);
86 status_t (*build_nonce_payload) (private_initiator_init_t *this, payload_t **payload);
87
88 status_t (*destroy_after_state_change) (private_initiator_init_t *this);
89 };
90
91 static status_t initiate_connection (private_initiator_init_t *this, char *name, state_t **new_state)
92 {
93 message_t *message;
94 payload_t *payload;
95 packet_t *packet;
96 status_t status;
97 linked_list_iterator_t *proposal_iterator;
98 ike_sa_init_requested_t *next_state;
99
100
101 this->logger->log(this->logger, CONTROL, "Initializing connection %s",name);
102
103 status = global_configuration_manager->get_local_host(global_configuration_manager, name, &(this->ike_sa->me.host));
104 if (status != SUCCESS)
105 {
106 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve local host configuration information for %s",name);
107 return INVALID_ARG;
108 }
109
110 status = global_configuration_manager->get_remote_host(global_configuration_manager, name, &(this->ike_sa->other.host));
111 if (status != SUCCESS)
112 {
113 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve remote host configuration information for %s",name);
114 return INVALID_ARG;
115 }
116
117 status = global_configuration_manager->get_dh_group_number(global_configuration_manager, name, &(this->dh_group_number), this->dh_group_priority);
118 if (status != SUCCESS)
119 {
120 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve DH group number for %s",name);
121 return INVALID_ARG;
122 }
123
124 status = this->proposals->create_iterator(this->proposals, &proposal_iterator, FALSE);
125 if (status != SUCCESS)
126 {
127 this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on list for proposals");
128 return status;
129 }
130
131 status = global_configuration_manager->get_proposals_for_host(global_configuration_manager, this->ike_sa->other.host, proposal_iterator);
132 if (status != SUCCESS)
133 {
134 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve Proposals for %s",name);
135 return status;
136 }
137 /* not needed anymore */
138 proposal_iterator->destroy(proposal_iterator);
139
140 this ->logger->log(this->logger, CONTROL|MOST, "create diffie hellman object");
141 this->diffie_hellman = diffie_hellman_create(this->dh_group_number);
142
143 if (this->diffie_hellman == NULL)
144 {
145 this->logger->log(this->logger, ERROR, "Object of type diffie_hellman_t could not be created!");
146 return FAILED;
147 }
148
149
150 if (this->ike_sa->randomizer->allocate_pseudo_random_bytes(this->ike_sa->randomizer, NONCE_SIZE, &(this->sent_nonce)) != SUCCESS)
151 {
152 this->logger->log(this->logger, ERROR, "Could not create nonce!");
153 return OUT_OF_RES;
154 }
155
156
157 /* going to build message */
158 status = this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, TRUE, &message);
159 if (status != SUCCESS)
160 {
161 this->logger->log(this->logger, ERROR, "Could not build message");
162 return status;
163 }
164
165 /* build SA payload */
166 status = this->build_sa_payload(this, &payload);
167 if (status != SUCCESS)
168 {
169 this->logger->log(this->logger, ERROR, "Could not build SA payload");
170 message->destroy(message);
171 return status;
172 }
173
174 this ->logger->log(this->logger, CONTROL|MOST, "add SA payload to message");
175 status = message->add_payload(message, payload);
176 if (status != SUCCESS)
177 {
178 this->logger->log(this->logger, ERROR, "Could not add SA payload to message");
179 message->destroy(message);
180 return status;
181 }
182
183
184 /* build KE payload */
185 status = this->build_ke_payload(this, &payload);
186 if (status != SUCCESS)
187 {
188 this->logger->log(this->logger, ERROR, "Could not build KE payload");
189 message->destroy(message);
190 return status;
191 }
192
193 this ->logger->log(this->logger, CONTROL|MOST, "add KE payload to message");
194 status = message->add_payload(message, payload);
195 if (status != SUCCESS)
196 {
197 this->logger->log(this->logger, ERROR, "Could not add KE payload to message");
198 message->destroy(message);
199 return status;
200 }
201
202 /* build Nonce payload */
203 status = this->build_nonce_payload(this, &payload);
204 if (status != SUCCESS)
205 {
206 this->logger->log(this->logger, ERROR, "Could not build NONCE payload");
207 message->destroy(message);
208 return status;
209 }
210
211 this ->logger->log(this->logger, CONTROL|MOST, "add nonce payload to message");
212 status = message->add_payload(message, payload);
213 if (status != SUCCESS)
214 {
215 this->logger->log(this->logger, ERROR, "Could not add nonce payload to message");
216 message->destroy(message);
217 return status;
218 }
219
220 /* generate packet */
221 this ->logger->log(this->logger, CONTROL|MOST, "generate packet from message");
222 status = message->generate(message, &packet);
223 if (status != SUCCESS)
224 {
225 this->logger->log(this->logger, ERROR, "Fatal error: could not generate packet from message");
226 message->destroy(message);
227 return status;
228 }
229
230 this ->logger->log(this->logger, CONTROL|MOST, "Add packet to global send queue");
231 status = global_send_queue->add(global_send_queue, packet);
232 if (status != SUCCESS)
233 {
234 this->logger->log(this->logger, ERROR, "Could not add packet to send queue");
235 message->destroy(message);
236 return status;
237 }
238
239 if ( this->ike_sa->last_requested_message != NULL)
240 {
241 /* destroy message */
242 this ->logger->log(this->logger, CONTROL|MOST, "Destroy stored last requested message");
243 this->ike_sa->last_requested_message->destroy(this->ike_sa->last_requested_message);
244 }
245
246 this->ike_sa->last_requested_message = message;
247
248 /* message counter can now be increased */
249 this->ike_sa->message_id_out++;
250
251
252 /* state can now be changed */
253 next_state = ike_sa_init_requested_create(this->ike_sa,this->diffie_hellman,this->sent_nonce);
254
255 if (next_state == NULL)
256 {
257 this ->logger->log(this->logger, ERROR, "Fatal error: could not create next state object of type ike_sa_init_requested_t");
258 return FAILED;
259 }
260
261 /* state has NOW changed :-) */
262 this ->logger->log(this->logger, CONTROL|MORE, "Change 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) );
263
264 this->destroy_after_state_change(this);
265
266 *new_state = (state_t *) next_state;
267
268 return SUCCESS;
269 }
270
271 /**
272 * implements private_initiator_init_t.build_sa_payload
273 */
274 static status_t build_sa_payload(private_initiator_init_t *this, payload_t **payload)
275 {
276 sa_payload_t* sa_payload;
277 linked_list_iterator_t *proposal_iterator;
278 status_t status;
279
280
281 /* SA payload takes proposals from this->ike_sa_init_data.proposals and writes them to the created sa_payload */
282
283 this->logger->log(this->logger, CONTROL|MORE, "building sa payload");
284
285 status = this->proposals->create_iterator(this->proposals, &proposal_iterator, FALSE);
286 if (status != SUCCESS)
287 {
288 this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on list for proposals");
289 return status;
290 }
291
292 sa_payload = sa_payload_create();
293 if (sa_payload == NULL)
294 {
295 this->logger->log(this->logger, ERROR, "Fatal error: Could not create SA payload object");
296 return OUT_OF_RES;
297 }
298
299 while (proposal_iterator->has_next(proposal_iterator))
300 {
301 proposal_substructure_t *current_proposal;
302 proposal_substructure_t *current_proposal_clone;
303 status = proposal_iterator->current(proposal_iterator,(void **) &current_proposal);
304 if (status != SUCCESS)
305 {
306 this->logger->log(this->logger, ERROR, "Could not get current proposal needed to copy");
307 sa_payload->destroy(sa_payload);
308 return status;
309 }
310 status = current_proposal->clone(current_proposal,&current_proposal_clone);
311 if (status != SUCCESS)
312 {
313 this->logger->log(this->logger, ERROR, "Could not clone current proposal");
314 sa_payload->destroy(sa_payload);
315 return status;
316 }
317
318 status = sa_payload->add_proposal_substructure(sa_payload,current_proposal_clone);
319 if (status != SUCCESS)
320 {
321 this->logger->log(this->logger, ERROR, "Could not add cloned proposal to SA payload");
322 sa_payload->destroy(sa_payload);
323 return status;
324 }
325
326 }
327
328 this->logger->log(this->logger, CONTROL|MORE, "sa payload builded");
329
330 *payload = (payload_t *) sa_payload;
331
332 return SUCCESS;
333 }
334
335 /**
336 * implements private_initiator_init_t.build_ke_payload
337 */
338 static status_t build_ke_payload(private_initiator_init_t *this, payload_t **payload)
339 {
340 ke_payload_t *ke_payload;
341 chunk_t key_data;
342 status_t status;
343
344 this->logger->log(this->logger, CONTROL|MORE, "building ke payload");
345
346
347 this ->logger->log(this->logger, CONTROL|MORE, "get public dh value to send in ke payload");
348 status = this->diffie_hellman->get_my_public_value(this->diffie_hellman,&key_data);
349 if (status != SUCCESS)
350 {
351 this->logger->log(this->logger, ERROR, "Could not get my DH public value");
352 return status;
353 }
354
355 ke_payload = ke_payload_create();
356 if (ke_payload == NULL)
357 {
358 this->logger->log(this->logger, ERROR, "Could not create KE payload");
359 allocator_free_chunk(key_data);
360 return OUT_OF_RES;
361 }
362 ke_payload->set_dh_group_number(ke_payload, MODP_1024_BIT);
363 if (ke_payload->set_key_exchange_data(ke_payload, key_data) != SUCCESS)
364 {
365 this->logger->log(this->logger, ERROR, "Could not set key exchange data of KE payload");
366 ke_payload->destroy(ke_payload);
367 allocator_free_chunk(key_data);
368 return OUT_OF_RES;
369 }
370 allocator_free_chunk(key_data);
371
372 *payload = (payload_t *) ke_payload;
373 return SUCCESS;
374 }
375
376 /**
377 * implements private_initiator_init_t.build_nonce_payload
378 */
379 static status_t build_nonce_payload(private_initiator_init_t *this, payload_t **payload)
380 {
381 nonce_payload_t *nonce_payload;
382 status_t status;
383
384 this->logger->log(this->logger, CONTROL|MORE, "building nonce payload");
385
386 nonce_payload = nonce_payload_create();
387 if (nonce_payload == NULL)
388 {
389 this->logger->log(this->logger, ERROR, "Fatal error: could not create nonce payload object");
390 return OUT_OF_RES;
391 }
392
393 status = nonce_payload->set_nonce(nonce_payload, this->sent_nonce);
394
395 if (status != SUCCESS)
396 {
397 this->logger->log(this->logger, ERROR, "Fatal error: could not set nonce data of payload");
398 nonce_payload->destroy(nonce_payload);
399 return status;
400 }
401
402 *payload = (payload_t *) nonce_payload;
403
404 return SUCCESS;
405 }
406
407 /**
408 * Implements state_t.get_state
409 */
410 static status_t process_message(private_initiator_init_t *this, message_t *message, state_t **new_state)
411 {
412 *new_state = (state_t *) this;
413 return FAILED;
414 }
415
416 /**
417 * Implements state_t.get_state
418 */
419 static ike_sa_state_t get_state(private_initiator_init_t *this)
420 {
421 return INITIATOR_INIT;
422 }
423
424 /**
425 * Implements state_t.get_state
426 */
427 static status_t destroy(private_initiator_init_t *this)
428 {
429 /* destroy stored proposal */
430 this->logger->log(this->logger, CONTROL | MOST, "Destroy stored proposals");
431 while (this->proposals->get_count(this->proposals) > 0)
432 {
433 proposal_substructure_t *current_proposal;
434 this->proposals->remove_first(this->proposals,(void **)&current_proposal);
435 current_proposal->destroy(current_proposal);
436 }
437 this->proposals->destroy(this->proposals);
438
439 /* destroy diffie hellman object */
440 if (this->diffie_hellman != NULL)
441 {
442 this->logger->log(this->logger, CONTROL | MOST, "Destroy diffie_hellman_t object");
443 this->diffie_hellman->destroy(this->diffie_hellman);
444 }
445
446 allocator_free(this);
447 return SUCCESS;
448 }
449
450 /**
451 * Implements private_initiator_init_t.destroy_after_state_change
452 */
453 static status_t destroy_after_state_change (private_initiator_init_t *this)
454 {
455 /* destroy stored proposal */
456 this->logger->log(this->logger, CONTROL | MOST, "Destroy stored proposals");
457 while (this->proposals->get_count(this->proposals) > 0)
458 {
459 proposal_substructure_t *current_proposal;
460 this->proposals->remove_first(this->proposals,(void **)&current_proposal);
461 current_proposal->destroy(current_proposal);
462 }
463 this->proposals->destroy(this->proposals);
464 allocator_free(this);
465 return SUCCESS;
466 }
467
468 /*
469 * Described in header.
470 */
471 initiator_init_t *initiator_init_create(protected_ike_sa_t *ike_sa)
472 {
473 private_initiator_init_t *this = allocator_alloc_thing(private_initiator_init_t);
474
475 if (this == NULL)
476 {
477 return NULL;
478 }
479
480 /* interface functions */
481 this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *,state_t **)) process_message;
482 this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
483 this->public.state_interface.destroy = (status_t (*) (state_t *)) destroy;
484
485 /* public functions */
486 this->public.initiate_connection = (status_t (*)(initiator_init_t *, char *, state_t **)) initiate_connection;
487
488 /* private functions */
489 this->build_sa_payload = build_sa_payload;
490 this->build_ke_payload = build_ke_payload;
491 this->build_nonce_payload = build_nonce_payload;
492 this->destroy_after_state_change = destroy_after_state_change;
493
494 /* private data */
495 this->ike_sa = ike_sa;
496 this->logger = this->ike_sa->logger;
497 this->dh_group_priority = 1;
498 this->proposals = linked_list_create();
499 this->sent_nonce.ptr = NULL;
500 this->sent_nonce.len = 0;
501 if (this->proposals == NULL)
502 {
503 allocator_free(this);
504 return NULL;
505 }
506
507 return &(this->public);
508 }