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