- fully implemented
[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 used to generate public DH value.
54 * This objet is passed to the next state of type ike_sa_init_requested_t.
55 */
56 diffie_hellman_t *diffie_hellman;
57
58 /**
59 * DH group number.
60 */
61 u_int16_t dh_group_number;
62
63 /**
64 * DH group priority used to get dh_group_number from configuration manager.
65 * This priority is passed to the next state of type ike_sa_init_requested_t.
66 */
67 u_int16_t dh_group_priority;
68
69 /**
70 * Sent nonce.
71 * This nonce is passed to the next state of type ike_sa_init_requested_t.
72 */
73 chunk_t sent_nonce;
74
75 /**
76 * Proposals used to initiate connection.
77 *
78 */
79 linked_list_t *proposals;
80
81 /**
82 * Logger used to log :-)
83 *
84 * Is logger of ike_sa!
85 */
86 logger_t *logger;
87
88 /**
89 * Builds the IKE_SA_INIT request message.
90 *
91 * @param this calling object
92 * @param message the created message will be stored at this location
93 * @return
94 * - SUCCESS
95 * - OUT_OF_RES
96 */
97 status_t (*build_ike_sa_init_request) (private_initiator_init_t *this, message_t **message);
98
99 /**
100 * Builds the SA payload for this state.
101 *
102 * @param this calling object
103 * @param payload The generated SA payload object of type ke_payload_t is
104 * stored at this location.
105 * @return
106 * - SUCCESS
107 * - OUT_OF_RES
108 */
109 status_t (*build_sa_payload) (private_initiator_init_t *this, payload_t **payload);
110
111 /**
112 * Builds the KE payload for this state.
113 *
114 * @param this calling object
115 * @param payload The generated KE payload object of type ke_payload_t is
116 * stored at this location.
117 * @return
118 * - SUCCESS
119 * - OUT_OF_RES
120 */
121 status_t (*build_ke_payload) (private_initiator_init_t *this, payload_t **payload);
122 /**
123 * Builds the NONCE payload for this state.
124 *
125 * @param this calling object
126 * @param payload The generated NONCE payload object of type ke_payload_t is
127 * stored at this location.
128 * @return
129 * - SUCCESS
130 * - OUT_OF_RES
131 */
132 status_t (*build_nonce_payload) (private_initiator_init_t *this, payload_t **payload);
133
134 /**
135 * Destroy function called internally of this class after state change succeeded.
136 *
137 * This destroy function does not destroy objects which were passed to the new state.
138 *
139 * @param this calling object
140 * @return SUCCESS in any case
141 */
142 status_t (*destroy_after_state_change) (private_initiator_init_t *this);
143 };
144
145 /**
146 * Implements function initiator_init_t.initiate_connection.
147 */
148 static status_t initiate_connection (private_initiator_init_t *this, char *name, state_t **new_state)
149 {
150 message_t *message;
151 packet_t *packet;
152 status_t status;
153 linked_list_iterator_t *proposal_iterator;
154 ike_sa_init_requested_t *next_state;
155
156 this->logger->log(this->logger, CONTROL, "Initializing connection %s",name);
157
158 status = global_configuration_manager->get_local_host(global_configuration_manager, name, &(this->ike_sa->me.host));
159 if (status != SUCCESS)
160 {
161 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve local host configuration information for %s",name);
162 return INVALID_ARG;
163 }
164
165 status = global_configuration_manager->get_remote_host(global_configuration_manager, name, &(this->ike_sa->other.host));
166 if (status != SUCCESS)
167 {
168 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve remote host configuration information for %s",name);
169 return INVALID_ARG;
170 }
171
172 status = global_configuration_manager->get_dh_group_number(global_configuration_manager, name, &(this->dh_group_number), this->dh_group_priority);
173 if (status != SUCCESS)
174 {
175 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve DH group number configuration for %s",name);
176 return INVALID_ARG;
177 }
178
179 status = this->proposals->create_iterator(this->proposals, &proposal_iterator, FALSE);
180 if (status != SUCCESS)
181 {
182 this->logger->log(this->logger, ERROR, "Fatal error: Could not create iterator on list for proposals");
183 return status;
184 }
185
186 status = global_configuration_manager->get_proposals_for_host(global_configuration_manager, this->ike_sa->other.host, proposal_iterator);
187 if (status != SUCCESS)
188 {
189 this->logger->log(this->logger, ERROR | MORE, "Could not retrieve Proposals for %s",name);
190 proposal_iterator->destroy(proposal_iterator);
191 return status;
192 }
193 /* not needed anymore */
194 proposal_iterator->destroy(proposal_iterator);
195
196 if (this->diffie_hellman == NULL)
197 {
198 this ->logger->log(this->logger, CONTROL|MOST, "create diffie hellman object");
199 this->diffie_hellman = diffie_hellman_create(this->dh_group_number);
200 }
201
202 if (this->diffie_hellman == NULL)
203 {
204 this->logger->log(this->logger, ERROR, "Object of type diffie_hellman_t could not be created!");
205 return FAILED;
206 }
207
208 if (this->sent_nonce.ptr != NULL)
209 {
210 this->logger->log(this->logger, ERROR, "Free existing sent nonce!");
211 allocator_free(this->sent_nonce.ptr);
212 this->sent_nonce.ptr = NULL;
213 this->sent_nonce.len = 0;
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 linked_list_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, MODP_1024_BIT);
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.ptr = NULL;
612 this->sent_nonce.len = 0;
613 if (this->proposals == NULL)
614 {
615 allocator_free(this);
616 return NULL;
617 }
618
619 return &(this->public);
620 }