e6de0679fbd1f758cc7f332d420acc701b87c3eb
[strongswan.git] / Source / charon / ike_sa.c
1 /**
2 * @file ike_sa.c
3 *
4 * @brief Class ike_sa_t. An object of this type is managed by an
5 * ike_sa_manager_t object and represents an IKE_SA
6 *
7 */
8
9 /*
10 * Copyright (C) 2005 Jan Hutter, Martin Willi
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include "ike_sa.h"
25
26 #include "types.h"
27 #include "globals.h"
28 #include "utils/allocator.h"
29 #include "utils/linked_list.h"
30 #include "utils/logger_manager.h"
31 #include "utils/randomizer.h"
32 #include "payloads/sa_payload.h"
33 #include "payloads/nonce_payload.h"
34 #include "payloads/ke_payload.h"
35 #include "payloads/transform_substructure.h"
36 #include "payloads/transform_attribute.h"
37
38
39 /**
40 * States in which a IKE_SA can actually be
41 */
42 typedef enum ike_sa_state_e ike_sa_state_t;
43
44 enum ike_sa_state_e {
45
46 /**
47 * IKE_SA is is not in a state
48 */
49 NO_STATE = 1,
50
51 /**
52 * A IKE_SA_INIT-message was sent: role initiator
53 */
54 IKE_SA_INIT_REQUESTED = 2,
55
56 /**
57 * A IKE_SA_INIT-message was replied: role responder
58 */
59 IKE_SA_INIT_RESPONDED = 3,
60
61 /**
62 * An IKE_AUTH-message was sent after a successful
63 * IKE_SA_INIT-exchange: role initiator
64 */
65 IKE_AUTH_REQUESTED = 4,
66
67 /**
68 * An IKE_AUTH-message was replied: role responder.
69 * In this state, all the informations for an IKE_SA
70 * and one CHILD_SA are known.
71 */
72 IKE_SA_INITIALIZED = 5
73 };
74
75
76 /**
77 * Private data of an message_t object
78 */
79 typedef struct private_ike_sa_s private_ike_sa_t;
80
81 struct private_ike_sa_s {
82
83 /**
84 * Public part of a ike_sa_t object
85 */
86 ike_sa_t public;
87
88 status_t (*build_sa_payload) (private_ike_sa_t *this, sa_payload_t **payload);
89 status_t (*build_nonce_payload) (private_ike_sa_t *this, nonce_payload_t **payload);
90 status_t (*build_ke_payload) (private_ike_sa_t *this, ke_payload_t **payload);
91
92 /* Private values */
93 /**
94 * Identifier for the current IKE_SA
95 */
96 ike_sa_id_t *ike_sa_id;
97
98 /**
99 * Linked List containing the child sa's of the current IKE_SA
100 */
101 linked_list_t *child_sas;
102
103 /**
104 * Current state of the IKE_SA
105 */
106 ike_sa_state_t current_state;
107
108 /**
109 * is this IKE_SA the original initiator of this IKE_SA
110 */
111 bool original_initiator;
112
113 /**
114 * this SA's source for random data
115 */
116 randomizer_t *randomizer;
117
118 linked_list_t *sent_messages;
119
120 struct {
121 host_t *host;
122 } me;
123
124 struct {
125 host_t *host;
126 } other;
127
128 /**
129 * a logger for this IKE_SA
130 */
131 logger_t *logger;
132 };
133
134 /**
135 * @brief implements function process_message of private_ike_sa_t
136 */
137 static status_t process_message (private_ike_sa_t *this, message_t *message)
138 {
139 status_t status;
140 /* @TODO Add Message Processing here */
141
142 this->logger->log(this->logger, CONTROL_MORE, "Process message of exchange type %s",mapping_find(exchange_type_m,message->get_exchange_type(message)));
143
144 status = message->parse_body(message);
145 switch (status)
146 {
147 case SUCCESS:
148 {
149 break;
150 }
151 default:
152 {
153 this->logger->log(this->logger, CONTROL_MORE, "Error of type %s while parsing message body",mapping_find(status_m,status));
154 return FAILED;
155 }
156 }
157
158 return status;
159 }
160
161 /**
162 * @brief implements function process_configuration of private_ike_sa_t
163 */
164 static status_t initialize_connection(private_ike_sa_t *this, char *name)
165 {
166 message_t *message;
167 payload_t *payload;
168 packet_t *packet;
169 host_t *source, *destination;
170 status_t status;
171
172 this->logger->log(this->logger, CONTROL, "initializing connection");
173
174 this->original_initiator = TRUE;
175
176 status = global_configuration_manager->get_local_host(global_configuration_manager, name, &source);
177 if (status != SUCCESS)
178 {
179 return INVALID_ARG;
180 }
181
182 status = global_configuration_manager->get_remote_host(global_configuration_manager, name, &destination);
183 if (status != SUCCESS)
184 {
185 return INVALID_ARG;
186 }
187
188 message = message_create();
189
190 if (message == NULL)
191 {
192 return OUT_OF_RES;
193 }
194
195
196 message->set_source(message, source);
197 message->set_destination(message, destination);
198
199 message->set_exchange_type(message, IKE_SA_INIT);
200 message->set_original_initiator(message, this->original_initiator);
201 message->set_message_id(message, 0);
202 message->set_ike_sa_id(message, this->ike_sa_id);
203 message->set_request(message, TRUE);
204
205 status = this->build_sa_payload(this, (sa_payload_t**)&payload);
206 if (status != SUCCESS)
207 {
208 this->logger->log(this->logger, ERROR, "Could not build SA payload");
209 message->destroy(message);
210 return status;
211 }
212 payload->set_next_type(payload, KEY_EXCHANGE);
213 message->add_payload(message, payload);
214
215 status = this->build_ke_payload(this, (ke_payload_t**)&payload);
216 if (status != SUCCESS)
217 {
218 this->logger->log(this->logger, ERROR, "Could not build KE payload");
219 message->destroy(message);
220 return status;
221 }
222 payload->set_next_type(payload, NONCE);
223 message->add_payload(message, payload);
224
225 status = this->build_nonce_payload(this, (nonce_payload_t**)&payload);
226 if (status != SUCCESS)
227 {
228 this->logger->log(this->logger, ERROR, "Could not build NONCE payload");
229 message->destroy(message);
230 return status;
231 }
232 payload->set_next_type(payload, NO_PAYLOAD);
233 message->add_payload(message, payload);
234
235 status = message->generate(message, &packet);
236 if (status != SUCCESS)
237 {
238 this->logger->log(this->logger, ERROR, "Could not generate message");
239 message->destroy(message);
240 return status;
241 }
242
243
244 global_send_queue->add(global_send_queue, packet);
245
246 message->destroy(message);
247
248
249 return SUCCESS;
250 }
251
252 /**
253 * @brief implements function private_ike_sa_t.get_id
254 */
255 static ike_sa_id_t* get_id(private_ike_sa_t *this)
256 {
257 return this->ike_sa_id;
258 }
259
260 /**
261 * implements private_ike_sa_t.build_sa_payload
262 */
263 static status_t build_sa_payload(private_ike_sa_t *this, sa_payload_t **payload)
264 {
265 sa_payload_t *sa_payload;
266 proposal_substructure_t *proposal;
267 transform_substructure_t *transform;
268 transform_attribute_t *attribute;
269
270
271 this->logger->log(this->logger, CONTROL_MORE, "building sa payload");
272
273 sa_payload = sa_payload_create();
274 if (sa_payload == NULL)
275 {
276 return OUT_OF_RES;
277 }
278
279 do
280 { /* no loop, just to break */
281 proposal = proposal_substructure_create();
282 if (proposal == NULL)
283 {
284 break;
285 }
286 sa_payload->add_proposal_substructure(sa_payload, proposal);
287
288 /*
289 * Encryption Algorithm
290 */
291 transform = transform_substructure_create();
292 if (transform == NULL)
293 {
294 break;
295 }
296 proposal->add_transform_substructure(proposal, transform);
297 transform->set_is_last_transform(transform, FALSE);
298 transform->set_transform_type(transform, ENCRYPTION_ALGORITHM);
299 transform->set_transform_id(transform, ENCR_AES_CBC);
300
301 attribute = transform_attribute_create();
302 if (attribute == NULL)
303 {
304 break;
305 }
306 transform->add_transform_attribute(transform, attribute);
307 attribute->set_attribute_type(attribute, KEY_LENGTH);
308 attribute->set_value(attribute, 16);
309
310 /*
311 * Pseudo-random Function
312 */
313 transform = transform_substructure_create();
314 if (transform == NULL)
315 {
316 break;
317 }
318 proposal->add_transform_substructure(proposal, transform);
319 transform->set_is_last_transform(transform, FALSE);
320 transform->set_transform_type(transform, PSEUDO_RANDOM_FUNCTION);
321 transform->set_transform_id(transform, PRF_HMAC_SHA1);
322
323 attribute = transform_attribute_create();
324 if (attribute == NULL)
325 {
326 break;
327 }
328 transform->add_transform_attribute(transform, attribute);
329 attribute->set_attribute_type(attribute, KEY_LENGTH);
330 attribute->set_value(attribute, 16);
331
332
333 /*
334 * Integrity Algorithm
335 */
336 transform = transform_substructure_create();
337 if (transform == NULL)
338 {
339 break;
340 }
341 proposal->add_transform_substructure(proposal, transform);
342 transform->set_is_last_transform(transform, FALSE);
343 transform->set_transform_type(transform, INTEGRITIY_ALGORITHM);
344 transform->set_transform_id(transform, AUTH_HMAC_SHA1_96);
345
346 attribute = transform_attribute_create();
347 if (attribute == NULL)
348 {
349 break;
350 }
351 transform->add_transform_attribute(transform, attribute);
352 attribute->set_attribute_type(attribute, KEY_LENGTH);
353 attribute->set_value(attribute, 16);
354
355
356 /*
357 * Diffie-Hellman Group
358 */
359 transform = transform_substructure_create();
360 if (transform == NULL)
361 {
362 break;
363 }
364 proposal->add_transform_substructure(proposal, transform);
365 transform->set_is_last_transform(transform, FALSE);
366 transform->set_transform_type(transform, DIFFIE_HELLMAN_GROUP);
367 transform->set_transform_id(transform, MODP_1024_BIT);
368
369 *payload = sa_payload;
370
371 return SUCCESS;
372
373 } while(FALSE);
374
375 return OUT_OF_RES;
376 }
377
378 /**
379 * implements private_ike_sa_t.build_ke_payload
380 */
381 static status_t build_ke_payload(private_ike_sa_t *this, ke_payload_t **payload)
382 {
383 ke_payload_t *ke_payload;
384 chunk_t key_data;
385
386
387 this->logger->log(this->logger, CONTROL_MORE, "building ke payload");
388
389 key_data.ptr = "12345";
390 key_data.len = strlen("12345");
391
392
393 ke_payload = ke_payload_create();
394 if (ke_payload == NULL)
395 {
396 return OUT_OF_RES;
397 }
398 ke_payload->set_dh_group_number(ke_payload, MODP_1024_BIT);
399 if (ke_payload->set_key_exchange_data(ke_payload, key_data) != SUCCESS)
400 {
401 ke_payload->destroy(ke_payload);
402 return OUT_OF_RES;
403 }
404 *payload = ke_payload;
405 return SUCCESS;
406 }
407
408 /**
409 * implements private_ike_sa_t.build_nonce_payload
410 */
411 static status_t build_nonce_payload(private_ike_sa_t *this, nonce_payload_t **payload)
412 {
413 nonce_payload_t *nonce_payload;
414 chunk_t nonce;
415
416 this->logger->log(this->logger, CONTROL_MORE, "building nonce payload");
417
418 if (this->randomizer->allocate_pseudo_random_bytes(this->randomizer, 16, &nonce) != SUCCESS)
419 {
420 return OUT_OF_RES;
421 }
422
423 nonce_payload = nonce_payload_create();
424 if (nonce_payload == NULL)
425 {
426 return OUT_OF_RES;
427 }
428
429 nonce_payload->set_nonce(nonce_payload, nonce);
430
431 *payload = nonce_payload;
432
433 return SUCCESS;
434 }
435
436 /**
437 * @brief implements function destroy of private_ike_sa_t
438 */
439 static status_t destroy (private_ike_sa_t *this)
440 {
441 linked_list_iterator_t *iterator;
442
443 this->child_sas->create_iterator(this->child_sas, &iterator, TRUE);
444 while (iterator->has_next(iterator))
445 {
446 payload_t *payload;
447 iterator->current(iterator, (void**)&payload);
448 payload->destroy(payload);
449 }
450 iterator->destroy(iterator);
451 this->child_sas->destroy(this->child_sas);
452
453 this->ike_sa_id->destroy(this->ike_sa_id);
454 this->sent_messages->destroy(this->sent_messages);
455 this->randomizer->destroy(this->randomizer);
456
457 global_logger_manager->destroy_logger(global_logger_manager, this->logger);
458
459 allocator_free(this);
460
461 return SUCCESS;
462 }
463
464 /*
465 * Described in Header
466 */
467 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
468 {
469 private_ike_sa_t *this = allocator_alloc_thing(private_ike_sa_t);
470 if (this == NULL)
471 {
472 return NULL;
473 }
474
475
476 /* Public functions */
477 this->public.process_message = (status_t(*)(ike_sa_t*, message_t*)) process_message;
478 this->public.initialize_connection = (status_t(*)(ike_sa_t*, char*)) initialize_connection;
479 this->public.get_id = (ike_sa_id_t*(*)(ike_sa_t*)) get_id;
480 this->public.destroy = (status_t(*)(ike_sa_t*))destroy;
481
482 this->build_sa_payload = build_sa_payload;
483 this->build_ke_payload = build_ke_payload;
484 this->build_nonce_payload = build_nonce_payload;
485
486
487
488 /* initialize private fields */
489 if (ike_sa_id->clone(ike_sa_id,&(this->ike_sa_id)) != SUCCESS)
490 {
491 allocator_free(this);
492 return NULL;
493 }
494 this->child_sas = linked_list_create();
495 if (this->child_sas == NULL)
496 {
497 this->ike_sa_id->destroy(this->ike_sa_id);
498 allocator_free(this);
499 return NULL;
500 }
501 this->randomizer = randomizer_create();
502 if (this->randomizer == NULL)
503 {
504 this->child_sas->destroy(this->child_sas);
505 this->ike_sa_id->destroy(this->ike_sa_id);
506 allocator_free(this);
507 }
508 this->sent_messages = linked_list_create();
509 if (this->sent_messages == NULL)
510 {
511 this->randomizer->destroy(this->randomizer);
512 this->child_sas->destroy(this->child_sas);
513 this->ike_sa_id->destroy(this->ike_sa_id);
514 allocator_free(this);
515 }
516 this->logger = global_logger_manager->create_logger(global_logger_manager, IKE_SA, NULL);
517 if (this->logger == NULL)
518 {
519 this->randomizer->destroy(this->randomizer);
520 this->child_sas->destroy(this->child_sas);
521 this->ike_sa_id->destroy(this->ike_sa_id);
522 this->sent_messages->destroy(this->sent_messages);
523 allocator_free(this);
524 }
525
526 this->me.host = NULL;
527 this->other.host = NULL;
528
529
530 /* at creation time, IKE_SA isn't in a specific state */
531 this->current_state = NO_STATE;
532
533 return (&this->public);
534 }