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