f71c2834f3aae63f227d10543cc63ef2c7ce78c8
[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 <globals.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 * @return
95 * - SUCCESS
96 * - OUT_OF_RES
97 */
98 status_t (*build_ike_sa_init_request) (private_initiator_init_t *this, message_t **message);
99
100 /**
101 * Builds the SA payload for this state.
102 *
103 * @param this calling object
104 * @param payload The generated SA payload object of type ke_payload_t is
105 * stored at this location.
106 * @return
107 * - SUCCESS
108 * - OUT_OF_RES
109 */
110 status_t (*build_sa_payload) (private_initiator_init_t *this, payload_t **payload);
111
112 /**
113 * Builds the KE payload for this state.
114 *
115 * @param this calling object
116 * @param payload The generated KE payload object of type ke_payload_t is
117 * stored at this location.
118 * @return
119 * - SUCCESS
120 * - OUT_OF_RES
121 */
122 status_t (*build_ke_payload) (private_initiator_init_t *this, payload_t **payload);
123 /**
124 * Builds the NONCE payload for this state.
125 *
126 * @param this calling object
127 * @param payload The generated NONCE payload object of type ke_payload_t is
128 * stored at this location.
129 * @return
130 * - SUCCESS
131 * - OUT_OF_RES
132 */
133 status_t (*build_nonce_payload) (private_initiator_init_t *this, payload_t **payload);
134
135 /**
136 * Destroy function called internally of this class after state change succeeded.
137 *
138 * This destroy function does not destroy objects which were passed to the new state.
139 *
140 * @param this calling object
141 * @return SUCCESS in any case
142 */
143 status_t (*destroy_after_state_change) (private_initiator_init_t *this);
144 };
145
146 /**
147 * Implements function initiator_init_t.initiate_connection.
148 */
149 static status_t initiate_connection (private_initiator_init_t *this, char *name, state_t **new_state)
150 {
151 iterator_t *proposal_iterator;
152 ike_sa_init_requested_t *next_state;
153 message_t *message;
154 packet_t *packet;
155 status_t status;
156
157 this->logger->log(this->logger, CONTROL, "Initializing connection %s",name);
158
159 status = global_configuration_manager->get_local_host(global_configuration_manager, name, &(this->ike_sa->me.host));
160 if (status != SUCCESS)
161 {
162 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve local host configuration information for %s",name);
163 return INVALID_ARG;
164 }
165
166 status = global_configuration_manager->get_remote_host(global_configuration_manager, name, &(this->ike_sa->other.host));
167 if (status != SUCCESS)
168 {
169 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve remote host configuration information for %s",name);
170 return INVALID_ARG;
171 }
172
173 status = global_configuration_manager->get_dh_group_number(global_configuration_manager, name, &(this->dh_group_number), this->dh_group_priority);
174 if (status != SUCCESS)
175 {
176 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve DH group number configuration for %s",name);
177 return INVALID_ARG;
178 }
179
180 status = this->proposals->create_iterator(this->proposals, &proposal_iterator, FALSE);
181 if (status != SUCCESS)
182 {
183 this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on list for proposals");
184 return status;
185 }
186
187 status = global_configuration_manager->get_proposals_for_host(global_configuration_manager, this->ike_sa->other.host, proposal_iterator);
188 /* not needed anymore */
189 proposal_iterator->destroy(proposal_iterator);
190 if (status != SUCCESS)
191 {
192 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve Proposals for %s",name);
193 return status;
194 }
195
196 /* a diffie hellman object could allready exist caused by an failed initiate_connection call */
197 if (this->diffie_hellman == NULL)
198 {
199 this ->logger->log(this->logger, CONTROL|MOST, "create diffie hellman object");
200 this->diffie_hellman = diffie_hellman_create(this->dh_group_number);
201 }
202
203 if (this->diffie_hellman == NULL)
204 {
205 this->logger->log(this->logger, ERROR, "Object of type diffie_hellman_t could not be created!");
206 return FAILED;
207 }
208
209 if (this->sent_nonce.ptr != NULL)
210 {
211 this->logger->log(this->logger, ERROR, "Free existing sent nonce!");
212 allocator_free(this->sent_nonce.ptr);
213 this->sent_nonce = CHUNK_INITIALIZER;
214 }
215
216 this ->logger->log(this->logger, CONTROL|MOST, "Get pseudo random bytes for nonce");
217 if (this->ike_sa->randomizer->allocate_pseudo_random_bytes(this->ike_sa->randomizer, NONCE_SIZE, &(this->sent_nonce)) != SUCCESS)
218 {
219 this->logger->log(this->logger, ERROR, "Could not create nonce!");
220 return OUT_OF_RES;
221 }
222 this ->logger->log(this->logger, RAW|MOST, "Nonce",&(this->sent_nonce));
223
224
225
226 status = this->build_ike_sa_init_request (this,&message);
227 if (status != SUCCESS)
228 {
229 this->logger->log(this->logger, ERROR, "Fatal error: could not build IKE_SA_INIT request message");
230 return status;
231 }
232
233 /* generate packet */
234 this ->logger->log(this->logger, CONTROL|MOST, "generate packet from message");
235 status = message->generate(message, &packet);
236 if (status != SUCCESS)
237 {
238 this->logger->log(this->logger, ERROR, "Fatal error: could not generate packet from message");
239 message->destroy(message);
240 return status;
241 }
242
243 this ->logger->log(this->logger, CONTROL|MOST, "Add packet to global send queue");
244 status = global_send_queue->add(global_send_queue, packet);
245 if (status != SUCCESS)
246 {
247 this->logger->log(this->logger, ERROR, "Could not add packet to send queue");
248 packet->destroy(packet);
249 message->destroy(message);
250 return status;
251 }
252
253 /* state can now be changed */
254 this ->logger->log(this->logger, CONTROL|MOST, "Create next state object");
255 next_state = ike_sa_init_requested_create(this->ike_sa, this->dh_group_number, this->diffie_hellman, this->sent_nonce);
256
257 if (next_state == NULL)
258 {
259 this ->logger->log(this->logger, ERROR, "Fatal error: could not create next state object of type ike_sa_init_requested_t");
260 return FAILED;
261 }
262
263 if ( this->ike_sa->last_requested_message != NULL)
264 {
265 /* destroy message */
266 this ->logger->log(this->logger, CONTROL|MOST, "Destroy stored last requested message");
267 this->ike_sa->last_requested_message->destroy(this->ike_sa->last_requested_message);
268 }
269
270 /* message is set after state create */
271 this ->logger->log(this->logger, CONTROL|MOST, "replace last requested message with current one");
272 this->ike_sa->last_requested_message = message;
273
274 /* message counter can now be increased */
275 this ->logger->log(this->logger, CONTROL|MOST, "Increate message counter for outgoing messages");
276 this->ike_sa->message_id_out++;
277
278 *new_state = (state_t *) next_state;
279 /* state has NOW changed :-) */
280 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) );
281
282 this ->logger->log(this->logger, CONTROL|MOST, "Destroy old sate object");
283 this->destroy_after_state_change(this);
284
285 return SUCCESS;
286 }
287
288 /**
289 * implements private_initiator_init_t.build_ike_sa_init_request
290 */
291 static status_t build_ike_sa_init_request (private_initiator_init_t *this, message_t **request)
292 {
293 status_t status;
294 payload_t *payload;
295 message_t *message;
296
297 /* going to build message */
298 this ->logger->log(this->logger, CONTROL|MOST, "Going to build message");
299 status = this->ike_sa->build_message(this->ike_sa, IKE_SA_INIT, TRUE, &message);
300 if (status != SUCCESS)
301 {
302 this->logger->log(this->logger, ERROR, "Could not build empty message");
303 return status;
304 }
305
306 /* build SA payload */
307 status = this->build_sa_payload(this, &payload);
308 if (status != SUCCESS)
309 {
310 this->logger->log(this->logger, ERROR, "Could not build SA payload");
311 message->destroy(message);
312 return status;
313 }
314
315 this ->logger->log(this->logger, CONTROL|MOST, "add SA payload to message");
316 status = message->add_payload(message, payload);
317 if (status != SUCCESS)
318 {
319 this->logger->log(this->logger, ERROR, "Could not add SA payload to message");
320 payload->destroy(payload);
321 message->destroy(message);
322 return status;
323 }
324
325 /* build KE payload */
326 status = this->build_ke_payload(this, &payload);
327 if (status != SUCCESS)
328 {
329 this->logger->log(this->logger, ERROR, "Could not build KE payload");
330 message->destroy(message);
331 return status;
332 }
333
334 this ->logger->log(this->logger, CONTROL|MOST, "add KE payload to message");
335 status = message->add_payload(message, payload);
336 if (status != SUCCESS)
337 {
338 this->logger->log(this->logger, ERROR, "Could not add KE payload to message");
339 payload->destroy(payload);
340 message->destroy(message);
341 return status;
342 }
343
344 /* build Nonce payload */
345 status = this->build_nonce_payload(this, &payload);
346 if (status != SUCCESS)
347 {
348 this->logger->log(this->logger, ERROR, "Could not build NONCE payload");
349 message->destroy(message);
350 return status;
351 }
352
353 this ->logger->log(this->logger, CONTROL|MOST, "add nonce payload to message");
354 status = message->add_payload(message, payload);
355 if (status != SUCCESS)
356 {
357 this->logger->log(this->logger, ERROR, "Could not add nonce payload to message");
358 payload->destroy(payload);
359 message->destroy(message);
360 return status;
361 }
362
363 *request = message;
364 return SUCCESS;
365 }
366
367 /**
368 * implements private_initiator_init_t.build_sa_payload
369 */
370 static status_t build_sa_payload(private_initiator_init_t *this, payload_t **payload)
371 {
372 sa_payload_t* sa_payload;
373 iterator_t *proposal_iterator;
374 status_t status;
375
376 /* SA payload takes proposals from this->ike_sa_init_data.proposals and writes them to the created sa_payload */
377
378 this->logger->log(this->logger, CONTROL|MORE, "building sa payload");
379 status = this->proposals->create_iterator(this->proposals, &proposal_iterator, FALSE);
380 if (status != SUCCESS)
381 {
382 this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on list for proposals");
383 return status;
384 }
385
386 sa_payload = sa_payload_create();
387 if (sa_payload == NULL)
388 {
389 this->logger->log(this->logger, ERROR, "Fatal error: Could not create SA payload object");
390 return OUT_OF_RES;
391 }
392
393 while (proposal_iterator->has_next(proposal_iterator))
394 {
395 proposal_substructure_t *current_proposal;
396 proposal_substructure_t *current_proposal_clone;
397 status = proposal_iterator->current(proposal_iterator,(void **) &current_proposal);
398 if (status != SUCCESS)
399 {
400 this->logger->log(this->logger, ERROR, "Could not get current proposal needed to copy");
401 proposal_iterator->destroy(proposal_iterator);
402 sa_payload->destroy(sa_payload);
403 return status;
404 }
405 status = current_proposal->clone(current_proposal,&current_proposal_clone);
406 if (status != SUCCESS)
407 {
408 this->logger->log(this->logger, ERROR, "Could not clone current proposal");
409 proposal_iterator->destroy(proposal_iterator);
410 sa_payload->destroy(sa_payload);
411 return status;
412 }
413
414 status = sa_payload->add_proposal_substructure(sa_payload,current_proposal_clone);
415 if (status != SUCCESS)
416 {
417 this->logger->log(this->logger, ERROR, "Could not add cloned proposal to SA payload");
418 proposal_iterator->destroy(proposal_iterator);
419 sa_payload->destroy(sa_payload);
420 return status;
421 }
422
423 }
424 proposal_iterator->destroy(proposal_iterator);
425
426 this->logger->log(this->logger, CONTROL|MORE, "sa payload builded");
427
428 *payload = (payload_t *) sa_payload;
429 return SUCCESS;
430 }
431
432 /**
433 * implements private_initiator_init_t.build_ke_payload
434 */
435 static status_t build_ke_payload(private_initiator_init_t *this, payload_t **payload)
436 {
437 ke_payload_t *ke_payload;
438 chunk_t key_data;
439 status_t status;
440
441 this->logger->log(this->logger, CONTROL|MORE, "building ke payload");
442
443
444 this ->logger->log(this->logger, CONTROL|MORE, "get public dh value to send in ke payload");
445 status = this->diffie_hellman->get_my_public_value(this->diffie_hellman,&key_data);
446 if (status != SUCCESS)
447 {
448 this->logger->log(this->logger, ERROR, "Could not get my DH public value");
449 return status;
450 }
451
452 ke_payload = ke_payload_create();
453 if (ke_payload == NULL)
454 {
455 this->logger->log(this->logger, ERROR, "Could not create KE payload");
456 allocator_free_chunk(key_data);
457 return OUT_OF_RES;
458 }
459 ke_payload->set_dh_group_number(ke_payload, this->dh_group_number);
460 if (ke_payload->set_key_exchange_data(ke_payload, key_data) != SUCCESS)
461 {
462 this->logger->log(this->logger, ERROR, "Could not set key exchange data of KE payload");
463 ke_payload->destroy(ke_payload);
464 allocator_free_chunk(key_data);
465 return OUT_OF_RES;
466 }
467 allocator_free_chunk(key_data);
468
469 this->logger->log(this->logger, CONTROL|MORE, "ke payload builded");
470
471 *payload = (payload_t *) ke_payload;
472 return SUCCESS;
473 }
474
475 /**
476 * implements private_initiator_init_t.build_nonce_payload
477 */
478 static status_t build_nonce_payload(private_initiator_init_t *this, payload_t **payload)
479 {
480 nonce_payload_t *nonce_payload;
481 status_t status;
482
483 this->logger->log(this->logger, CONTROL|MORE, "building nonce payload");
484
485 nonce_payload = nonce_payload_create();
486 if (nonce_payload == NULL)
487 {
488 this->logger->log(this->logger, ERROR, "Fatal error: could not create nonce payload object");
489 return OUT_OF_RES;
490 }
491
492 status = nonce_payload->set_nonce(nonce_payload, this->sent_nonce);
493
494 if (status != SUCCESS)
495 {
496 this->logger->log(this->logger, ERROR, "Fatal error: could not set nonce data of payload");
497 nonce_payload->destroy(nonce_payload);
498 return status;
499 }
500
501 *payload = (payload_t *) nonce_payload;
502
503 this->logger->log(this->logger, CONTROL|MORE, "nonce payload builded");
504
505 return SUCCESS;
506 }
507
508 /**
509 * Implements state_t.get_state
510 */
511 static status_t process_message(private_initiator_init_t *this, message_t *message, state_t **new_state)
512 {
513 *new_state = (state_t *) this;
514 this->logger->log(this->logger, ERROR|MORE, "In state INITIATOR_INIT no message is processed");
515 return FAILED;
516 }
517
518 /**
519 * Implements state_t.get_state
520 */
521 static ike_sa_state_t get_state(private_initiator_init_t *this)
522 {
523 return INITIATOR_INIT;
524 }
525
526 /**
527 * Implements state_t.get_state
528 */
529 static status_t destroy(private_initiator_init_t *this)
530 {
531 this->logger->log(this->logger, CONTROL | MORE, "Going to destroy initiator_init_t state object");
532
533 /* destroy stored proposal */
534 this->logger->log(this->logger, CONTROL | MOST, "Destroy stored proposals");
535 while (this->proposals->get_count(this->proposals) > 0)
536 {
537 proposal_substructure_t *current_proposal;
538 this->proposals->remove_first(this->proposals,(void **)&current_proposal);
539 current_proposal->destroy(current_proposal);
540 }
541 this->proposals->destroy(this->proposals);
542
543 /* destroy diffie hellman object */
544 if (this->diffie_hellman != NULL)
545 {
546 this->logger->log(this->logger, CONTROL | MOST, "Destroy diffie_hellman_t object");
547 this->diffie_hellman->destroy(this->diffie_hellman);
548 }
549 if (this->sent_nonce.ptr != NULL)
550 {
551 this->logger->log(this->logger, CONTROL | MOST, "Free memory of sent nonce");
552 allocator_free(this->sent_nonce.ptr);
553 }
554
555 allocator_free(this);
556 return SUCCESS;
557 }
558
559 /**
560 * Implements private_initiator_init_t.destroy_after_state_change
561 */
562 static status_t destroy_after_state_change (private_initiator_init_t *this)
563 {
564 this->logger->log(this->logger, CONTROL | MORE, "Going to destroy initiator_init_t state object");
565
566 /* destroy stored proposal */
567 this->logger->log(this->logger, CONTROL | MOST, "Destroy stored proposals");
568 while (this->proposals->get_count(this->proposals) > 0)
569 {
570 proposal_substructure_t *current_proposal;
571 this->proposals->remove_first(this->proposals,(void **)&current_proposal);
572 current_proposal->destroy(current_proposal);
573 }
574 this->proposals->destroy(this->proposals);
575 allocator_free(this);
576 return SUCCESS;
577 }
578
579 /*
580 * Described in header.
581 */
582 initiator_init_t *initiator_init_create(protected_ike_sa_t *ike_sa)
583 {
584 private_initiator_init_t *this = allocator_alloc_thing(private_initiator_init_t);
585
586 if (this == NULL)
587 {
588 return NULL;
589 }
590
591 /* interface functions */
592 this->public.state_interface.process_message = (status_t (*) (state_t *,message_t *,state_t **)) process_message;
593 this->public.state_interface.get_state = (ike_sa_state_t (*) (state_t *)) get_state;
594 this->public.state_interface.destroy = (status_t (*) (state_t *)) destroy;
595
596 /* public functions */
597 this->public.initiate_connection = (status_t (*)(initiator_init_t *, char *, state_t **)) initiate_connection;
598
599 /* private functions */
600 this->destroy_after_state_change = destroy_after_state_change;
601 this->build_ike_sa_init_request = build_ike_sa_init_request;
602 this->build_nonce_payload = build_nonce_payload;
603 this->build_sa_payload = build_sa_payload;
604 this->build_ke_payload = build_ke_payload;
605
606 /* private data */
607 this->ike_sa = ike_sa;
608 this->dh_group_priority = 1;
609 this->logger = this->ike_sa->logger;
610 this->proposals = linked_list_create();
611 this->sent_nonce = CHUNK_INITIALIZER;
612 if (this->proposals == NULL)
613 {
614 allocator_free(this);
615 return NULL;
616 }
617
618 return &(this->public);
619 }