""
[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,
50
51 /**
52 * A IKE_SA_INIT-message was sent: role initiator
53 */
54 IKE_SA_INIT_REQUESTED,
55
56 /**
57 * A IKE_SA_INIT-message was replied: role responder
58 */
59 IKE_SA_INIT_RESPONDED,
60
61 /**
62 * An IKE_AUTH-message was sent after a successful
63 * IKE_SA_INIT-exchange: role initiator
64 */
65 IKE_AUTH_REQUESTED,
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
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 /* @TODO Add Message Processing here */
140 return SUCCESS;
141 }
142
143 /**
144 * @brief implements function process_configuration of private_ike_sa_t
145 */
146 static status_t initialize_connection(private_ike_sa_t *this, char *name)
147 {
148 message_t *message;
149 payload_t *payload;
150 packet_t *packet;
151 status_t status;
152
153 this->logger->log(this->logger, CONTROL, "initializing connection");
154
155 this->original_initiator = TRUE;
156
157 message = message_create();
158
159 if (message == NULL)
160 {
161 return OUT_OF_RES;
162 }
163
164
165 message->set_exchange_type(message, IKE_SA_INIT);
166 message->set_original_initiator(message, this->original_initiator);
167 message->set_message_id(message, 0);
168 message->set_ike_sa_id(message, this->ike_sa_id);
169 message->set_request(message, TRUE);
170
171 status = this->build_sa_payload(this, (sa_payload_t**)&payload);
172 if (status != SUCCESS)
173 {
174 message->destroy(message);
175 return status;
176 }
177 payload->set_next_type(payload, KEY_EXCHANGE);
178 message->add_payload(message, payload);
179
180 status = this->build_ke_payload(this, (ke_payload_t**)&payload);
181 if (status != SUCCESS)
182 {
183 message->destroy(message);
184 return status;
185 }
186 payload->set_next_type(payload, NONCE);
187 message->add_payload(message, payload);
188
189 status = this->build_nonce_payload(this, (nonce_payload_t**)&payload);
190 if (status != SUCCESS)
191 {
192 message->destroy(message);
193 return status;
194 }
195 payload->set_next_type(payload, NO_PAYLOAD);
196 message->add_payload(message, payload);
197
198 status = message->generate(message, &packet);
199 if (status != SUCCESS)
200 {
201 message->destroy(message);
202 return status;
203 }
204
205 global_send_queue->add(global_send_queue, packet);
206
207
208 message->destroy(message);
209
210
211 return OUT_OF_RES;
212 }
213
214 /**
215 * @brief implements function private_ike_sa_t.get_id
216 */
217 static ike_sa_id_t* get_id(private_ike_sa_t *this)
218 {
219 return this->ike_sa_id;
220 }
221
222 /**
223 * implements private_ike_sa_t.build_sa_payload
224 */
225 static status_t build_sa_payload(private_ike_sa_t *this, sa_payload_t **payload)
226 {
227 sa_payload_t *sa_payload;
228 proposal_substructure_t *proposal;
229 transform_substructure_t *transform;
230 transform_attribute_t *attribute;
231
232
233 this->logger->log(this->logger, CONTROL_MORE, "building sa payload");
234
235 sa_payload = sa_payload_create();
236 if (sa_payload == NULL)
237 {
238 return OUT_OF_RES;
239 }
240
241 do
242 { /* no loop, just to break */
243 proposal = proposal_substructure_create();
244 if (proposal == NULL)
245 {
246 break;
247 }
248 sa_payload->add_proposal_substructure(sa_payload, proposal);
249
250 /*
251 * Encryption Algorithm
252 */
253 transform = transform_substructure_create();
254 if (transform == NULL)
255 {
256 break;
257 }
258 proposal->add_transform_substructure(proposal, transform);
259 transform->set_is_last_transform(transform, FALSE);
260 transform->set_transform_type(transform, ENCRYPTION_ALGORITHM);
261 transform->set_transform_id(transform, ENCR_AES_CBC);
262
263 attribute = transform_attribute_create();
264 if (attribute == NULL)
265 {
266 break;
267 }
268 transform->add_transform_attribute(transform, attribute);
269 attribute->set_attribute_type(attribute, KEY_LENGTH);
270 attribute->set_value(attribute, 16);
271
272 /*
273 * Pseudo-random Function
274 */
275 transform = transform_substructure_create();
276 if (transform == NULL)
277 {
278 break;
279 }
280 proposal->add_transform_substructure(proposal, transform);
281 transform->set_is_last_transform(transform, FALSE);
282 transform->set_transform_type(transform, PSEUDO_RANDOM_FUNCTION);
283 transform->set_transform_id(transform, PRF_HMAC_SHA1);
284
285 attribute = transform_attribute_create();
286 if (attribute == NULL)
287 {
288 break;
289 }
290 transform->add_transform_attribute(transform, attribute);
291 attribute->set_attribute_type(attribute, KEY_LENGTH);
292 attribute->set_value(attribute, 16);
293
294
295 /*
296 * Integrity Algorithm
297 */
298 transform = transform_substructure_create();
299 if (transform == NULL)
300 {
301 break;
302 }
303 proposal->add_transform_substructure(proposal, transform);
304 transform->set_is_last_transform(transform, FALSE);
305 transform->set_transform_type(transform, INTEGRITIY_ALGORITHM);
306 transform->set_transform_id(transform, AUTH_HMAC_SHA1_96);
307
308 attribute = transform_attribute_create();
309 if (attribute == NULL)
310 {
311 break;
312 }
313 transform->add_transform_attribute(transform, attribute);
314 attribute->set_attribute_type(attribute, KEY_LENGTH);
315 attribute->set_value(attribute, 16);
316
317
318 /*
319 * Diffie-Hellman Group
320 */
321 transform = transform_substructure_create();
322 if (transform == NULL)
323 {
324 break;
325 }
326 proposal->add_transform_substructure(proposal, transform);
327 transform->set_is_last_transform(transform, FALSE);
328 transform->set_transform_type(transform, DIFFIE_HELLMAN_GROUP);
329 transform->set_transform_id(transform, MODP_1024_BIT);
330
331 *payload = sa_payload;
332
333 return SUCCESS;
334
335 } while(FALSE);
336
337 return OUT_OF_RES;
338 }
339
340 /**
341 * implements private_ike_sa_t.build_ke_payload
342 */
343 static status_t build_ke_payload(private_ike_sa_t *this, ke_payload_t **payload)
344 {
345 ke_payload_t *ke_payload;
346 chunk_t key_data;
347
348
349 this->logger->log(this->logger, CONTROL_MORE, "building ke payload");
350
351 key_data.ptr = "12345";
352 key_data.len = strlen("12345");
353
354
355 ke_payload = ke_payload_create();
356 if (ke_payload == NULL)
357 {
358 return OUT_OF_RES;
359 }
360 ke_payload->set_dh_group_number(ke_payload, MODP_1024_BIT);
361 if (ke_payload->set_key_exchange_data(ke_payload, key_data) != SUCCESS)
362 {
363 ke_payload->destroy(ke_payload);
364 return OUT_OF_RES;
365 }
366 *payload = ke_payload;
367 return SUCCESS;
368 }
369
370 /**
371 * implements private_ike_sa_t.build_nonce_payload
372 */
373 static status_t build_nonce_payload(private_ike_sa_t *this, nonce_payload_t **payload)
374 {
375 nonce_payload_t *nonce_payload;
376 chunk_t nonce;
377
378 this->logger->log(this->logger, CONTROL_MORE, "building nonce payload");
379
380 if (this->randomizer->allocate_pseudo_random_bytes(this->randomizer, 16, &nonce) != SUCCESS)
381 {
382 return OUT_OF_RES;
383 }
384
385 nonce_payload = nonce_payload_create();
386 if (nonce_payload == NULL)
387 {
388 return OUT_OF_RES;
389 }
390
391 nonce_payload->set_nonce(nonce_payload, nonce);
392
393 *payload = nonce_payload;
394
395 return SUCCESS;
396 }
397
398 /**
399 * @brief implements function destroy of private_ike_sa_t
400 */
401 static status_t destroy (private_ike_sa_t *this)
402 {
403 linked_list_iterator_t *iterator;
404
405 this->child_sas->create_iterator(this->child_sas, &iterator, TRUE);
406 while (iterator->has_next(iterator))
407 {
408 payload_t *payload;
409 iterator->current(iterator, (void**)&payload);
410 payload->destroy(payload);
411 }
412 iterator->destroy(iterator);
413 this->child_sas->destroy(this->child_sas);
414
415 this->ike_sa_id->destroy(this->ike_sa_id);
416
417 this->randomizer->destroy(this->randomizer);
418
419 global_logger_manager->destroy_logger(global_logger_manager, this->logger);
420
421 allocator_free(this);
422
423 return SUCCESS;
424 }
425
426 /*
427 * Described in Header
428 */
429 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
430 {
431 private_ike_sa_t *this = allocator_alloc_thing(private_ike_sa_t);
432 if (this == NULL)
433 {
434 return NULL;
435 }
436
437
438 /* Public functions */
439 this->public.process_message = (status_t(*)(ike_sa_t*, message_t*)) process_message;
440 this->public.initialize_connection = (status_t(*)(ike_sa_t*, char*)) initialize_connection;
441 this->public.get_id = (ike_sa_id_t*(*)(ike_sa_t*)) get_id;
442 this->public.destroy = (status_t(*)(ike_sa_t*))destroy;
443
444 this->build_sa_payload = build_sa_payload;
445 this->build_ke_payload = build_ke_payload;
446 this->build_nonce_payload = build_nonce_payload;
447
448
449
450 /* initialize private fields */
451 if (ike_sa_id->clone(ike_sa_id,&(this->ike_sa_id)) != SUCCESS)
452 {
453 allocator_free(this);
454 return NULL;
455 }
456 this->child_sas = linked_list_create();
457 if (this->child_sas == NULL)
458 {
459 this->ike_sa_id->destroy(this->ike_sa_id);
460 allocator_free(this);
461 return NULL;
462 }
463 this->randomizer = randomizer_create();
464 if (this->randomizer == NULL)
465 {
466 this->child_sas->destroy(this->child_sas);
467 this->ike_sa_id->destroy(this->ike_sa_id);
468 allocator_free(this);
469 }
470 this->sent_messages = linked_list_create();
471 if (this->sent_messages == NULL)
472 {
473 this->randomizer->destroy(this->randomizer);
474 this->child_sas->destroy(this->child_sas);
475 this->ike_sa_id->destroy(this->ike_sa_id);
476 allocator_free(this);
477 }
478 this->logger = global_logger_manager->create_logger(global_logger_manager, IKE_SA, NULL);
479 if (this->logger == NULL)
480 {
481 this->randomizer->destroy(this->randomizer);
482 this->child_sas->destroy(this->child_sas);
483 this->ike_sa_id->destroy(this->ike_sa_id);
484 this->sent_messages->destroy(this->sent_messages);
485 allocator_free(this);
486 }
487
488 this->me.host = NULL;
489 this->other.host = NULL;
490
491
492 /* at creation time, IKE_SA isn't in a specific state */
493 this->current_state = NO_STATE;
494
495 return (&this->public);
496 }